2424 STDLIB_DIR , is_jython , swap_attr , swap_item , cpython_only , is_emscripten ,
2525 is_wasi )
2626from test .support .import_helper import (
27- forget , make_legacy_pyc , unlink , unload , DirsOnSysPath , CleanImport )
27+ forget , make_legacy_pyc , unlink , unload , ready_to_import ,
28+ DirsOnSysPath , CleanImport )
2829from test .support .os_helper import (
29- TESTFN , rmtree , temp_umask , TESTFN_UNENCODABLE , temp_dir )
30+ TESTFN , rmtree , temp_umask , TESTFN_UNENCODABLE )
3031from test .support import script_helper
3132from test .support import threading_helper
3233from test .test_importlib .util import uncache
@@ -46,27 +47,6 @@ def remove_files(name):
4647 rmtree ('__pycache__' )
4748
4849
49- @contextlib .contextmanager
50- def _ready_to_import (name = None , source = "" ):
51- # sets up a temporary directory and removes it
52- # creates the module file
53- # temporarily clears the module from sys.modules (if any)
54- # reverts or removes the module when cleaning up
55- name = name or "spam"
56- with temp_dir () as tempdir :
57- path = script_helper .make_script (tempdir , name , source )
58- old_module = sys .modules .pop (name , None )
59- try :
60- sys .path .insert (0 , tempdir )
61- yield name , path
62- sys .path .remove (tempdir )
63- finally :
64- if old_module is not None :
65- sys .modules [name ] = old_module
66- elif name in sys .modules :
67- del sys .modules [name ]
68-
69-
7050class ImportTests (unittest .TestCase ):
7151
7252 def setUp (self ):
@@ -130,7 +110,7 @@ def test_from_import_missing_attr_path_is_canonical(self):
130110
131111 def test_from_import_star_invalid_type (self ):
132112 import re
133- with _ready_to_import () as (name , path ):
113+ with ready_to_import () as (name , path ):
134114 with open (path , 'w' , encoding = 'utf-8' ) as f :
135115 f .write ("__all__ = [b'invalid_type']" )
136116 globals = {}
@@ -139,7 +119,7 @@ def test_from_import_star_invalid_type(self):
139119 ):
140120 exec (f"from { name } import *" , globals )
141121 self .assertNotIn (b"invalid_type" , globals )
142- with _ready_to_import () as (name , path ):
122+ with ready_to_import () as (name , path ):
143123 with open (path , 'w' , encoding = 'utf-8' ) as f :
144124 f .write ("globals()[b'invalid_type'] = object()" )
145125 globals = {}
@@ -550,7 +530,7 @@ class FilePermissionTests(unittest.TestCase):
550530 )
551531 def test_creation_mode (self ):
552532 mask = 0o022
553- with temp_umask (mask ), _ready_to_import () as (name , path ):
533+ with temp_umask (mask ), ready_to_import () as (name , path ):
554534 cached_path = importlib .util .cache_from_source (path )
555535 module = __import__ (name )
556536 if not os .path .exists (cached_path ):
@@ -569,7 +549,7 @@ def test_creation_mode(self):
569549 def test_cached_mode_issue_2051 (self ):
570550 # permissions of .pyc should match those of .py, regardless of mask
571551 mode = 0o600
572- with temp_umask (0o022 ), _ready_to_import () as (name , path ):
552+ with temp_umask (0o022 ), ready_to_import () as (name , path ):
573553 cached_path = importlib .util .cache_from_source (path )
574554 os .chmod (path , mode )
575555 __import__ (name )
@@ -585,7 +565,7 @@ def test_cached_mode_issue_2051(self):
585565 @os_helper .skip_unless_working_chmod
586566 def test_cached_readonly (self ):
587567 mode = 0o400
588- with temp_umask (0o022 ), _ready_to_import () as (name , path ):
568+ with temp_umask (0o022 ), ready_to_import () as (name , path ):
589569 cached_path = importlib .util .cache_from_source (path )
590570 os .chmod (path , mode )
591571 __import__ (name )
@@ -600,7 +580,7 @@ def test_cached_readonly(self):
600580 def test_pyc_always_writable (self ):
601581 # Initially read-only .pyc files on Windows used to cause problems
602582 # with later updates, see issue #6074 for details
603- with _ready_to_import () as (name , path ):
583+ with ready_to_import () as (name , path ):
604584 # Write a Python file, make it read-only and import it
605585 with open (path , 'w' , encoding = 'utf-8' ) as f :
606586 f .write ("x = 'original'\n " )
0 commit comments