24
24
STDLIB_DIR , is_jython , swap_attr , swap_item , cpython_only , is_emscripten ,
25
25
is_wasi )
26
26
from 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 )
28
29
from test .support .os_helper import (
29
- TESTFN , rmtree , temp_umask , TESTFN_UNENCODABLE , temp_dir )
30
+ TESTFN , rmtree , temp_umask , TESTFN_UNENCODABLE )
30
31
from test .support import script_helper
31
32
from test .support import threading_helper
32
33
from test .test_importlib .util import uncache
@@ -46,27 +47,6 @@ def remove_files(name):
46
47
rmtree ('__pycache__' )
47
48
48
49
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
-
70
50
class ImportTests (unittest .TestCase ):
71
51
72
52
def setUp (self ):
@@ -130,7 +110,7 @@ def test_from_import_missing_attr_path_is_canonical(self):
130
110
131
111
def test_from_import_star_invalid_type (self ):
132
112
import re
133
- with _ready_to_import () as (name , path ):
113
+ with ready_to_import () as (name , path ):
134
114
with open (path , 'w' , encoding = 'utf-8' ) as f :
135
115
f .write ("__all__ = [b'invalid_type']" )
136
116
globals = {}
@@ -139,7 +119,7 @@ def test_from_import_star_invalid_type(self):
139
119
):
140
120
exec (f"from { name } import *" , globals )
141
121
self .assertNotIn (b"invalid_type" , globals )
142
- with _ready_to_import () as (name , path ):
122
+ with ready_to_import () as (name , path ):
143
123
with open (path , 'w' , encoding = 'utf-8' ) as f :
144
124
f .write ("globals()[b'invalid_type'] = object()" )
145
125
globals = {}
@@ -550,7 +530,7 @@ class FilePermissionTests(unittest.TestCase):
550
530
)
551
531
def test_creation_mode (self ):
552
532
mask = 0o022
553
- with temp_umask (mask ), _ready_to_import () as (name , path ):
533
+ with temp_umask (mask ), ready_to_import () as (name , path ):
554
534
cached_path = importlib .util .cache_from_source (path )
555
535
module = __import__ (name )
556
536
if not os .path .exists (cached_path ):
@@ -569,7 +549,7 @@ def test_creation_mode(self):
569
549
def test_cached_mode_issue_2051 (self ):
570
550
# permissions of .pyc should match those of .py, regardless of mask
571
551
mode = 0o600
572
- with temp_umask (0o022 ), _ready_to_import () as (name , path ):
552
+ with temp_umask (0o022 ), ready_to_import () as (name , path ):
573
553
cached_path = importlib .util .cache_from_source (path )
574
554
os .chmod (path , mode )
575
555
__import__ (name )
@@ -585,7 +565,7 @@ def test_cached_mode_issue_2051(self):
585
565
@os_helper .skip_unless_working_chmod
586
566
def test_cached_readonly (self ):
587
567
mode = 0o400
588
- with temp_umask (0o022 ), _ready_to_import () as (name , path ):
568
+ with temp_umask (0o022 ), ready_to_import () as (name , path ):
589
569
cached_path = importlib .util .cache_from_source (path )
590
570
os .chmod (path , mode )
591
571
__import__ (name )
@@ -600,7 +580,7 @@ def test_cached_readonly(self):
600
580
def test_pyc_always_writable (self ):
601
581
# Initially read-only .pyc files on Windows used to cause problems
602
582
# with later updates, see issue #6074 for details
603
- with _ready_to_import () as (name , path ):
583
+ with ready_to_import () as (name , path ):
604
584
# Write a Python file, make it read-only and import it
605
585
with open (path , 'w' , encoding = 'utf-8' ) as f :
606
586
f .write ("x = 'original'\n " )
0 commit comments