diff options
author | Thomas Wouters <thomas@python.org> | 2024-03-12 23:46:31 (GMT) |
---|---|---|
committer | Thomas Wouters <thomas@python.org> | 2024-03-12 23:46:31 (GMT) |
commit | 3f54d1cfe78f7c88fb0ecdbc250d9f8be092ec5a (patch) | |
tree | 04583120cbf299497dd52298e83a4d0a185a8df4 /Lib | |
parent | 128fbdf97b64901fdc45db19c10f2c71351d41e8 (diff) | |
parent | a53cc3f49463e50cb3e2b839b3a82e6bf7f73fee (diff) | |
download | cpython-3f54d1cfe78f7c88fb0ecdbc250d9f8be092ec5a.zip cpython-3f54d1cfe78f7c88fb0ecdbc250d9f8be092ec5a.tar.gz cpython-3f54d1cfe78f7c88fb0ecdbc250d9f8be092ec5a.tar.bz2 |
Merge branch 'main' of https://github.com/python/cpython
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/support/import_helper.py | 12 | ||||
-rw-r--r-- | Lib/test/test_frame.py | 65 | ||||
-rw-r--r-- | Lib/test/test_importlib/resources/test_files.py | 2 |
3 files changed, 13 insertions, 66 deletions
diff --git a/Lib/test/support/import_helper.py b/Lib/test/support/import_helper.py index 3d804f2..29c6f53 100644 --- a/Lib/test/support/import_helper.py +++ b/Lib/test/support/import_helper.py @@ -268,6 +268,18 @@ def modules_cleanup(oldmodules): sys.modules.update(oldmodules) +@contextlib.contextmanager +def isolated_modules(): + """ + Save modules on entry and cleanup on exit. + """ + (saved,) = modules_setup() + try: + yield + finally: + modules_cleanup(saved) + + def mock_register_at_fork(func): # bpo-30599: Mock os.register_at_fork() when importing the random module, # since this function doesn't allow to unregister callbacks and would leak diff --git a/Lib/test/test_frame.py b/Lib/test/test_frame.py index f8812c2..8e744a1 100644 --- a/Lib/test/test_frame.py +++ b/Lib/test/test_frame.py @@ -294,71 +294,6 @@ class TestIncompleteFrameAreInvisible(unittest.TestCase): assert_python_ok("-c", code) @support.cpython_only - @unittest.skipIf(Py_GIL_DISABLED, "test requires precise GC scheduling") - def test_sneaky_frame_object(self): - - def trace(frame, event, arg): - """ - Don't actually do anything, just force a frame object to be created. - """ - - def callback(phase, info): - """ - Yo dawg, I heard you like frames, so I'm allocating a frame while - you're allocating a frame, so you can have a frame while you have a - frame! - """ - nonlocal sneaky_frame_object - sneaky_frame_object = sys._getframe().f_back.f_back - # We're done here: - gc.callbacks.remove(callback) - - def f(): - while True: - yield - - old_threshold = gc.get_threshold() - old_callbacks = gc.callbacks[:] - old_enabled = gc.isenabled() - old_trace = sys.gettrace() - try: - # Stop the GC for a second while we set things up: - gc.disable() - # Create a paused generator: - g = f() - next(g) - # Move all objects to the oldest generation, and tell the GC to run - # on the *very next* allocation: - gc.collect() - gc.set_threshold(1, 0, 0) - sys._clear_internal_caches() - # Okay, so here's the nightmare scenario: - # - We're tracing the resumption of a generator, which creates a new - # frame object. - # - The allocation of this frame object triggers a collection - # *before* the frame object is actually created. - # - During the collection, we request the exact same frame object. - # This test does it with a GC callback, but in real code it would - # likely be a trace function, weakref callback, or finalizer. - # - The collection finishes, and the original frame object is - # created. We now have two frame objects fighting over ownership - # of the same interpreter frame! - sys.settrace(trace) - gc.callbacks.append(callback) - sneaky_frame_object = None - gc.enable() - next(g) - # g.gi_frame should be the frame object from the callback (the - # one that was *requested* second, but *created* first): - self.assertIs(g.gi_frame, sneaky_frame_object) - finally: - gc.set_threshold(*old_threshold) - gc.callbacks[:] = old_callbacks - sys.settrace(old_trace) - if old_enabled: - gc.enable() - - @support.cpython_only @threading_helper.requires_working_threading() def test_sneaky_frame_object_teardown(self): diff --git a/Lib/test/test_importlib/resources/test_files.py b/Lib/test/test_importlib/resources/test_files.py index 1450cfb..26c8b04 100644 --- a/Lib/test/test_importlib/resources/test_files.py +++ b/Lib/test/test_importlib/resources/test_files.py @@ -70,7 +70,7 @@ class SiteDir: self.addCleanup(self.fixtures.close) self.site_dir = self.fixtures.enter_context(os_helper.temp_dir()) self.fixtures.enter_context(import_helper.DirsOnSysPath(self.site_dir)) - self.fixtures.enter_context(import_helper.CleanImport()) + self.fixtures.enter_context(import_helper.isolated_modules()) class ModulesFilesTests(SiteDir, unittest.TestCase): |