Skip to content

Commit ef88802

Browse files
committed
Issue python#17177: stop using imp in test_importlib
1 parent 53e600c commit ef88802

File tree

16 files changed

+78
-76
lines changed

16 files changed

+78
-76
lines changed

Lib/test/test_importlib/extension/test_case_sensitivity.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import imp
21
import sys
32
from test import support
43
import unittest
4+
55
from importlib import _bootstrap
6+
from importlib import machinery
67
from .. import util
78
from . import util as ext_util
89

@@ -14,9 +15,9 @@ def find_module(self):
1415
good_name = ext_util.NAME
1516
bad_name = good_name.upper()
1617
assert good_name != bad_name
17-
finder = _bootstrap.FileFinder(ext_util.PATH,
18-
(_bootstrap.ExtensionFileLoader,
19-
_bootstrap.EXTENSION_SUFFIXES))
18+
finder = machinery.FileFinder(ext_util.PATH,
19+
(machinery.ExtensionFileLoader,
20+
machinery.EXTENSION_SUFFIXES))
2021
return finder.find_module(bad_name)
2122

2223
def test_case_sensitive(self):

Lib/test/test_importlib/extension/test_path_hook.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from . import util
33

44
import collections
5-
import imp
65
import sys
76
import unittest
87

Lib/test/test_importlib/extension/util.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import imp
21
from importlib import machinery
32
import os
43
import sys

Lib/test/test_importlib/frozen/test_loader.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
from importlib import machinery
2-
import imp
3-
import unittest
41
from .. import abc
52
from .. import util
3+
4+
from importlib import machinery
5+
import unittest
66
from test.support import captured_stdout
7+
import types
8+
79

810
class LoaderTests(abc.LoaderTests):
911

@@ -85,7 +87,7 @@ def test_get_code(self):
8587
name = '__hello__'
8688
with captured_stdout() as stdout:
8789
code = machinery.FrozenImporter.get_code(name)
88-
mod = imp.new_module(name)
90+
mod = types.ModuleType(name)
8991
exec(code, mod.__dict__)
9092
self.assertTrue(hasattr(mod, 'initialized'))
9193
self.assertEqual(stdout.getvalue(), 'Hello world!\n')

Lib/test/test_importlib/import_/test___loader__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import imp
21
import sys
2+
import types
33
import unittest
44

55
from .. import util
@@ -19,7 +19,7 @@ def load_module(self, fullname):
1919
class LoaderAttributeTests(unittest.TestCase):
2020

2121
def test___loader___missing(self):
22-
module = imp.new_module('blah')
22+
module = types.ModuleType('blah')
2323
try:
2424
del module.__loader__
2525
except AttributeError:
@@ -31,7 +31,7 @@ def test___loader___missing(self):
3131
self.assertEqual(loader, module.__loader__)
3232

3333
def test___loader___is_None(self):
34-
module = imp.new_module('blah')
34+
module = types.ModuleType('blah')
3535
module.__loader__ = None
3636
loader = LoaderMock()
3737
loader.module = module

Lib/test/test_importlib/import_/test_api.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from . import util
33
import imp
44
import sys
5+
import types
56
import unittest
67

78

@@ -48,7 +49,7 @@ def test_negative_level(self):
4849
def test_nonexistent_fromlist_entry(self):
4950
# If something in fromlist doesn't exist, that's okay.
5051
# issue15715
51-
mod = imp.new_module('fine')
52+
mod = types.ModuleType('fine')
5253
mod.__path__ = ['XXX']
5354
with importlib_test_util.import_state(meta_path=[BadLoaderFinder]):
5455
with importlib_test_util.uncache('fine'):
@@ -59,7 +60,7 @@ def test_fromlist_load_error_propagates(self):
5960
# If something in fromlist triggers an exception not related to not
6061
# existing, let that exception propagate.
6162
# issue15316
62-
mod = imp.new_module('fine')
63+
mod = types.ModuleType('fine')
6364
mod.__path__ = ['XXX']
6465
with importlib_test_util.import_state(meta_path=[BadLoaderFinder]):
6566
with importlib_test_util.uncache('fine'):

Lib/test/test_importlib/import_/test_fromlist.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""Test that the semantics relating to the 'fromlist' argument are correct."""
22
from .. import util
33
from . import util as import_util
4-
import imp
54
import unittest
65

76
class ReturnValue(unittest.TestCase):

Lib/test/test_importlib/source/test_case_sensitivity.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""Test case-sensitivity (PEP 235)."""
2-
from importlib import _bootstrap
3-
from importlib import machinery
42
from .. import util
53
from . import util as source_util
6-
import imp
4+
5+
from importlib import _bootstrap
6+
from importlib import machinery
77
import os
88
import sys
99
from test import support as test_support

Lib/test/test_importlib/source/test_file_loader.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from importlib import machinery
22
import importlib
33
import importlib.abc
4+
import importlib.util
45
from .. import abc
56
from .. import util
67
from . import util as source_util
@@ -13,6 +14,7 @@
1314
import shutil
1415
import stat
1516
import sys
17+
import types
1618
import unittest
1719

1820
from 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')

Lib/test/test_importlib/source/test_finder.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
from importlib import machinery
55
import errno
6-
import imp
76
import os
87
import py_compile
98
import stat

0 commit comments

Comments
 (0)