Visitar URL original
gh-101758: Add a Test For Single-Phase Init Modules in Multiple Interpreters by ericsnowcurrently · Pull Request #101920 · python/cpython · GitHub
Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Do not copy _testsinglephase.
  • Loading branch information
ericsnowcurrently committed Feb 15, 2023
commit ae709dbb50e57107c5cda736706ae9469e2c6fd7
43 changes: 21 additions & 22 deletions Lib/test/test_imp.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import gc
import importlib
import importlib.util
import shutil
import os
import os.path
import py_compile
import sys
import tempfile
from test import support
from test.support import import_helper
from test.support import os_helper
Expand All @@ -17,6 +15,7 @@
import warnings
imp = warnings_helper.import_deprecated('imp')
import _imp
import _testinternalcapi
import _xxsubinterpreters as _interpreters


Expand Down Expand Up @@ -71,17 +70,6 @@ def setUp(self):
self.test_strings = mod.test_strings
self.test_path = mod.__path__

def _copy_extension(self, name):
fileobj, pathname, _ = imp.find_module('_testsinglephase')
fileobj.close()

dirname = tempfile.mkdtemp()
self.addCleanup(os_helper.rmtree, dirname)

copied = os.path.join(dirname, os.path.basename(pathname))
shutil.copyfile(pathname, copied)
return copied

# test_import_encoded_module moved to test_source_encoding.py

def test_find_module_encoding(self):
Expand Down Expand Up @@ -274,27 +262,38 @@ def test_singlephase_multiple_interpreters(self):

# This single-phase module has global state, which is shared
# by the interpreters.
name = '_testsinglephase'
filename = self._copy_extension(name)
import _testsinglephase
name = _testsinglephase.__name__
filename = _testsinglephase.__file__

del sys.modules[name]
_testsinglephase._clear_globals()
_testinternalcapi.clear_extension(name, filename)
init_count = _testsinglephase.initialized_count()
assert init_count == -1, (init_count,)

def clean_up():
_testsinglephase._clear_globals()
_testinternalcapi.clear_extension(name, filename)
self.addCleanup(clean_up)

interp1 = _interpreters.create(isolated=False)
self.addCleanup(_interpreters.destroy, interp1)
interp2 = _interpreters.create(isolated=False)
self.addCleanup(_interpreters.destroy, interp2)

script = textwrap.dedent(f'''
from test.support import warnings_helper
imp = warnings_helper.import_deprecated('imp')
module = imp.load_dynamic({name!r}, {filename!r})
import _testsinglephase

init_count = module.initialized_count()
init_count = _testsinglephase.initialized_count()
if init_count != %d:
raise Exception(init_count)

lookedup = module.look_up_self()
if lookedup is not module:
raise Exception((module, lookedup))
lookedup = _testsinglephase.look_up_self()
if lookedup is not _testsinglephase:
raise Exception((_testsinglephase, lookedup))
''')

# Use an interpreter that gets destroyed right away.
ret = support.run_in_subinterp(script % 1)
self.assertEqual(ret, 0)
Expand Down