30
30
STDLIB_DIR , swap_attr , swap_item , cpython_only , is_emscripten ,
31
31
is_wasi , run_in_subinterp , run_in_subinterp_with_config , Py_TRACE_REFS )
32
32
from 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 )
34
35
from test .support .os_helper import (
35
- TESTFN , rmtree , temp_umask , TESTFN_UNENCODABLE , temp_dir )
36
+ TESTFN , rmtree , temp_umask , TESTFN_UNENCODABLE )
36
37
from test .support import script_helper
37
38
from test .support import threading_helper
38
39
from test .test_importlib .util import uncache
@@ -125,27 +126,6 @@ def wrapper(self):
125
126
return deco
126
127
127
128
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
-
149
129
if _testsinglephase is not None :
150
130
def restore__testsinglephase (* , _orig = _testsinglephase ):
151
131
# We started with the module imported and want to restore
@@ -401,7 +381,7 @@ def test_from_import_missing_attr_path_is_canonical(self):
401
381
402
382
def test_from_import_star_invalid_type (self ):
403
383
import re
404
- with _ready_to_import () as (name , path ):
384
+ with ready_to_import () as (name , path ):
405
385
with open (path , 'w' , encoding = 'utf-8' ) as f :
406
386
f .write ("__all__ = [b'invalid_type']" )
407
387
globals = {}
@@ -410,7 +390,7 @@ def test_from_import_star_invalid_type(self):
410
390
):
411
391
exec (f"from { name } import *" , globals )
412
392
self .assertNotIn (b"invalid_type" , globals )
413
- with _ready_to_import () as (name , path ):
393
+ with ready_to_import () as (name , path ):
414
394
with open (path , 'w' , encoding = 'utf-8' ) as f :
415
395
f .write ("globals()[b'invalid_type'] = object()" )
416
396
globals = {}
@@ -818,7 +798,7 @@ class FilePermissionTests(unittest.TestCase):
818
798
)
819
799
def test_creation_mode (self ):
820
800
mask = 0o022
821
- with temp_umask (mask ), _ready_to_import () as (name , path ):
801
+ with temp_umask (mask ), ready_to_import () as (name , path ):
822
802
cached_path = importlib .util .cache_from_source (path )
823
803
module = __import__ (name )
824
804
if not os .path .exists (cached_path ):
@@ -837,7 +817,7 @@ def test_creation_mode(self):
837
817
def test_cached_mode_issue_2051 (self ):
838
818
# permissions of .pyc should match those of .py, regardless of mask
839
819
mode = 0o600
840
- with temp_umask (0o022 ), _ready_to_import () as (name , path ):
820
+ with temp_umask (0o022 ), ready_to_import () as (name , path ):
841
821
cached_path = importlib .util .cache_from_source (path )
842
822
os .chmod (path , mode )
843
823
__import__ (name )
@@ -853,7 +833,7 @@ def test_cached_mode_issue_2051(self):
853
833
@os_helper .skip_unless_working_chmod
854
834
def test_cached_readonly (self ):
855
835
mode = 0o400
856
- with temp_umask (0o022 ), _ready_to_import () as (name , path ):
836
+ with temp_umask (0o022 ), ready_to_import () as (name , path ):
857
837
cached_path = importlib .util .cache_from_source (path )
858
838
os .chmod (path , mode )
859
839
__import__ (name )
@@ -868,7 +848,7 @@ def test_cached_readonly(self):
868
848
def test_pyc_always_writable (self ):
869
849
# Initially read-only .pyc files on Windows used to cause problems
870
850
# with later updates, see issue #6074 for details
871
- with _ready_to_import () as (name , path ):
851
+ with ready_to_import () as (name , path ):
872
852
# Write a Python file, make it read-only and import it
873
853
with open (path , 'w' , encoding = 'utf-8' ) as f :
874
854
f .write ("x = 'original'\n " )
0 commit comments