Skip to content

Conversation

lisroach
Copy link
Contributor

@lisroach lisroach commented Aug 31, 2025

Adds sys.audit to import_module.
Added updated unit tests.

Cannot populate the file argument because it forces loading of the module and breaks LazyLoader, but the typical import statement audit also populates the file argument to None so this is consistent. The only difference is with import_module the audit is called after sys.modules is populate, an import statement will call sys.audit before it is populate. I don't think this will make a difference, but could be wrong.

@lisroach lisroach requested a review from zooba August 31, 2025 22:31
@lisroach lisroach changed the title gh138310: Adds sys.audit event for import_module. gh-138310: Adds sys.audit event for import_module. Aug 31, 2025
@AA-Turner AA-Turner changed the title gh-138310: Adds sys.audit event for import_module. gh-138310: Adds sys.audit event for import_module Sep 3, 2025
@lisroach lisroach self-assigned this Sep 10, 2025
@lisroach
Copy link
Contributor Author

Sorry for the delay, added a bunch of tests that show some interesting weirdness that (I think) is just the way to import system works, but confirmed things work the way they did before my changes:

  1. The import statement logs audit events in a different order, it logs package first then the submodule. Other import styles are swapped. I believe this is because import statement resolves the package first, so this should be correct.
  2. Extension modules log twice. I am not sure why, but confirmed this behavior existed before my changes.

@erlend-aasland erlend-aasland removed their request for review September 18, 2025 13:53
Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. 👍

@lisroach lisroach merged commit 2fd43a1 into python:main Sep 19, 2025
43 checks passed
@lisroach lisroach deleted the issue-138310 branch September 19, 2025 13:21
@bedevere-bot
Copy link

⚠️⚠️⚠️ Buildbot failure ⚠️⚠️⚠️

Hi! The buildbot AMD64 Ubuntu Shared 3.x (tier-1) has failed when building commit 2fd43a1.

What do you need to do:

  1. Don't panic.
  2. Check the buildbot page in the devguide if you don't know what the buildbots are or how they work.
  3. Go to the page of the buildbot that failed (https://buildbot.python.org/#/builders/506/builds/11464) and take a look at the build logs.
  4. Check if the failure is related to this commit (2fd43a1) or if it is a false positive.
  5. If the failure is related to this commit, please, reflect that on the issue and make a new Pull Request with a fix.

You can take a look at the buildbot page here:

https://buildbot.python.org/#/builders/506/builds/11464

Failed tests:

  • test_interpreters

Failed subtests:

  • test_keyboard_interrupt_in_thread_running_interp - test.test_interpreters.test_api.InterpreterObjectTests.test_keyboard_interrupt_in_thread_running_interp

Summary of the results of the build (if available):

==

Click to see traceback logs
Traceback (most recent call last):
  File "/srv/buildbot/buildarea/3.x.bolen-ubuntu/build/Lib/test/test_interpreters/test_api.py", line 462, in test_keyboard_interrupt_in_thread_running_interp
    self.assertEqual(retcode, 0)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^
AssertionError: -2 != 0

facebook-github-bot pushed a commit to facebookincubator/cinder that referenced this pull request Sep 29, 2025
Summary:
Backport of upstream PR-138311 which adds sys.audit calls for
import_module. python/cpython#138311

Reviewed By: itamaro

Differential Revision: D83073937

fbshipit-source-id: 3831c0e8ac424142149f2a92a5d9e7dde49a6440
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants