11from importlib import machinery
22import importlib
33import importlib .abc
4+ import importlib .util
45from .. import abc
56from .. import util
67from . import util as source_util
1314import shutil
1415import stat
1516import sys
17+ import types
1618import unittest
1719
1820from test .support import make_legacy_pyc , unload
@@ -112,7 +114,7 @@ def test_state_after_failure(self):
112114 value = '<test>'
113115 name = '_temp'
114116 with source_util .create_modules (name ) as mapping :
115- orig_module = imp . new_module (name )
117+ orig_module = types . ModuleType (name )
116118 for attr in attributes :
117119 setattr (orig_module , attr , value )
118120 with open (mapping [name ], 'w' ) as file :
@@ -144,11 +146,11 @@ def test_file_from_empty_string_dir(self):
144146 loader = machinery .SourceFileLoader ('_temp' , file_path )
145147 mod = loader .load_module ('_temp' )
146148 self .assertEqual (file_path , mod .__file__ )
147- self .assertEqual (imp .cache_from_source (file_path ),
149+ self .assertEqual (importlib . util .cache_from_source (file_path ),
148150 mod .__cached__ )
149151 finally :
150152 os .unlink (file_path )
151- pycache = os .path .dirname (imp .cache_from_source (file_path ))
153+ pycache = os .path .dirname (importlib . util .cache_from_source (file_path ))
152154 if os .path .exists (pycache ):
153155 shutil .rmtree (pycache )
154156
@@ -157,7 +159,7 @@ def test_timestamp_overflow(self):
157159 # truncated rather than raise an OverflowError.
158160 with source_util .create_modules ('_temp' ) as mapping :
159161 source = mapping ['_temp' ]
160- compiled = imp .cache_from_source (source )
162+ compiled = importlib . util .cache_from_source (source )
161163 with open (source , 'w' ) as f :
162164 f .write ("x = 5" )
163165 try :
@@ -194,7 +196,7 @@ def manipulate_bytecode(self, name, mapping, manipulator, *,
194196 pass
195197 py_compile .compile (mapping [name ])
196198 if not del_source :
197- bytecode_path = imp .cache_from_source (mapping [name ])
199+ bytecode_path = importlib . util .cache_from_source (mapping [name ])
198200 else :
199201 os .unlink (mapping [name ])
200202 bytecode_path = make_legacy_pyc (mapping [name ])
@@ -322,7 +324,8 @@ def test_bad_magic(self):
322324 def test (name , mapping , bytecode_path ):
323325 self .import_ (mapping [name ], name )
324326 with open (bytecode_path , 'rb' ) as bytecode_file :
325- self .assertEqual (bytecode_file .read (4 ), imp .get_magic ())
327+ self .assertEqual (bytecode_file .read (4 ),
328+ importlib .util .MAGIC_NUMBER )
326329
327330 self ._test_bad_magic (test )
328331
@@ -372,7 +375,7 @@ def test_old_timestamp(self):
372375 zeros = b'\x00 \x00 \x00 \x00 '
373376 with source_util .create_modules ('_temp' ) as mapping :
374377 py_compile .compile (mapping ['_temp' ])
375- bytecode_path = imp .cache_from_source (mapping ['_temp' ])
378+ bytecode_path = importlib . util .cache_from_source (mapping ['_temp' ])
376379 with open (bytecode_path , 'r+b' ) as bytecode_file :
377380 bytecode_file .seek (4 )
378381 bytecode_file .write (zeros )
@@ -390,7 +393,7 @@ def test_read_only_bytecode(self):
390393 with source_util .create_modules ('_temp' ) as mapping :
391394 # Create bytecode that will need to be re-created.
392395 py_compile .compile (mapping ['_temp' ])
393- bytecode_path = imp .cache_from_source (mapping ['_temp' ])
396+ bytecode_path = importlib . util .cache_from_source (mapping ['_temp' ])
394397 with open (bytecode_path , 'r+b' ) as bytecode_file :
395398 bytecode_file .seek (0 )
396399 bytecode_file .write (b'\x00 \x00 \x00 \x00 ' )
0 commit comments