3030 STDLIB_DIR , swap_attr , swap_item , cpython_only , is_emscripten ,
3131 is_wasi , run_in_subinterp , run_in_subinterp_with_config )
3232from test .support .import_helper import (
33- forget , make_legacy_pyc , unlink , unload , DirsOnSysPath , CleanImport )
33+ forget , make_legacy_pyc , unlink , unload , ready_to_import ,
34+ DirsOnSysPath , CleanImport )
3435from test .support .os_helper import (
35- TESTFN , rmtree , temp_umask , TESTFN_UNENCODABLE , temp_dir )
36+ TESTFN , rmtree , temp_umask , TESTFN_UNENCODABLE )
3637from test .support import script_helper
3738from test .support import threading_helper
3839from test .test_importlib .util import uncache
@@ -125,27 +126,6 @@ def wrapper(self):
125126 return deco
126127
127128
128- @contextlib .contextmanager
129- def _ready_to_import (name = None , source = "" ):
130- # sets up a temporary directory and removes it
131- # creates the module file
132- # temporarily clears the module from sys.modules (if any)
133- # reverts or removes the module when cleaning up
134- name = name or "spam"
135- with temp_dir () as tempdir :
136- path = script_helper .make_script (tempdir , name , source )
137- old_module = sys .modules .pop (name , None )
138- try :
139- sys .path .insert (0 , tempdir )
140- yield name , path
141- sys .path .remove (tempdir )
142- finally :
143- if old_module is not None :
144- sys .modules [name ] = old_module
145- elif name in sys .modules :
146- del sys .modules [name ]
147-
148-
149129if _testsinglephase is not None :
150130 def restore__testsinglephase (* , _orig = _testsinglephase ):
151131 # We started with the module imported and want to restore
@@ -401,7 +381,7 @@ def test_from_import_missing_attr_path_is_canonical(self):
401381
402382 def test_from_import_star_invalid_type (self ):
403383 import re
404- with _ready_to_import () as (name , path ):
384+ with ready_to_import () as (name , path ):
405385 with open (path , 'w' , encoding = 'utf-8' ) as f :
406386 f .write ("__all__ = [b'invalid_type']" )
407387 globals = {}
@@ -410,7 +390,7 @@ def test_from_import_star_invalid_type(self):
410390 ):
411391 exec (f"from { name } import *" , globals )
412392 self .assertNotIn (b"invalid_type" , globals )
413- with _ready_to_import () as (name , path ):
393+ with ready_to_import () as (name , path ):
414394 with open (path , 'w' , encoding = 'utf-8' ) as f :
415395 f .write ("globals()[b'invalid_type'] = object()" )
416396 globals = {}
@@ -818,7 +798,7 @@ class FilePermissionTests(unittest.TestCase):
818798 )
819799 def test_creation_mode (self ):
820800 mask = 0o022
821- with temp_umask (mask ), _ready_to_import () as (name , path ):
801+ with temp_umask (mask ), ready_to_import () as (name , path ):
822802 cached_path = importlib .util .cache_from_source (path )
823803 module = __import__ (name )
824804 if not os .path .exists (cached_path ):
@@ -837,7 +817,7 @@ def test_creation_mode(self):
837817 def test_cached_mode_issue_2051 (self ):
838818 # permissions of .pyc should match those of .py, regardless of mask
839819 mode = 0o600
840- with temp_umask (0o022 ), _ready_to_import () as (name , path ):
820+ with temp_umask (0o022 ), ready_to_import () as (name , path ):
841821 cached_path = importlib .util .cache_from_source (path )
842822 os .chmod (path , mode )
843823 __import__ (name )
@@ -853,7 +833,7 @@ def test_cached_mode_issue_2051(self):
853833 @os_helper .skip_unless_working_chmod
854834 def test_cached_readonly (self ):
855835 mode = 0o400
856- with temp_umask (0o022 ), _ready_to_import () as (name , path ):
836+ with temp_umask (0o022 ), ready_to_import () as (name , path ):
857837 cached_path = importlib .util .cache_from_source (path )
858838 os .chmod (path , mode )
859839 __import__ (name )
@@ -868,7 +848,7 @@ def test_cached_readonly(self):
868848 def test_pyc_always_writable (self ):
869849 # Initially read-only .pyc files on Windows used to cause problems
870850 # with later updates, see issue #6074 for details
871- with _ready_to_import () as (name , path ):
851+ with ready_to_import () as (name , path ):
872852 # Write a Python file, make it read-only and import it
873853 with open (path , 'w' , encoding = 'utf-8' ) as f :
874854 f .write ("x = 'original'\n " )
0 commit comments