Commit ef888024 authored by Brett Cannon's avatar Brett Cannon

Issue #17177: stop using imp in test_importlib

parent 53e600c2
import imp
import sys
from test import support
import unittest
from importlib import _bootstrap
from importlib import machinery
from .. import util
from . import util as ext_util
......@@ -14,9 +15,9 @@ class ExtensionModuleCaseSensitivityTest(unittest.TestCase):
good_name = ext_util.NAME
bad_name = good_name.upper()
assert good_name != bad_name
finder = _bootstrap.FileFinder(ext_util.PATH,
(_bootstrap.ExtensionFileLoader,
_bootstrap.EXTENSION_SUFFIXES))
finder = machinery.FileFinder(ext_util.PATH,
(machinery.ExtensionFileLoader,
machinery.EXTENSION_SUFFIXES))
return finder.find_module(bad_name)
def test_case_sensitive(self):
......
......@@ -2,7 +2,6 @@ from importlib import machinery
from . import util
import collections
import imp
import sys
import unittest
......
import imp
from importlib import machinery
import os
import sys
......
from importlib import machinery
import imp
import unittest
from .. import abc
from .. import util
from importlib import machinery
import unittest
from test.support import captured_stdout
import types
class LoaderTests(abc.LoaderTests):
......@@ -85,7 +87,7 @@ class InspectLoaderTests(unittest.TestCase):
name = '__hello__'
with captured_stdout() as stdout:
code = machinery.FrozenImporter.get_code(name)
mod = imp.new_module(name)
mod = types.ModuleType(name)
exec(code, mod.__dict__)
self.assertTrue(hasattr(mod, 'initialized'))
self.assertEqual(stdout.getvalue(), 'Hello world!\n')
......
import imp
import sys
import types
import unittest
from .. import util
......@@ -19,7 +19,7 @@ class LoaderMock:
class LoaderAttributeTests(unittest.TestCase):
def test___loader___missing(self):
module = imp.new_module('blah')
module = types.ModuleType('blah')
try:
del module.__loader__
except AttributeError:
......@@ -31,7 +31,7 @@ class LoaderAttributeTests(unittest.TestCase):
self.assertEqual(loader, module.__loader__)
def test___loader___is_None(self):
module = imp.new_module('blah')
module = types.ModuleType('blah')
module.__loader__ = None
loader = LoaderMock()
loader.module = module
......
......@@ -2,6 +2,7 @@ from .. import util as importlib_test_util
from . import util
import imp
import sys
import types
import unittest
......@@ -48,7 +49,7 @@ class APITest(unittest.TestCase):
def test_nonexistent_fromlist_entry(self):
# If something in fromlist doesn't exist, that's okay.
# issue15715
mod = imp.new_module('fine')
mod = types.ModuleType('fine')
mod.__path__ = ['XXX']
with importlib_test_util.import_state(meta_path=[BadLoaderFinder]):
with importlib_test_util.uncache('fine'):
......@@ -59,7 +60,7 @@ class APITest(unittest.TestCase):
# If something in fromlist triggers an exception not related to not
# existing, let that exception propagate.
# issue15316
mod = imp.new_module('fine')
mod = types.ModuleType('fine')
mod.__path__ = ['XXX']
with importlib_test_util.import_state(meta_path=[BadLoaderFinder]):
with importlib_test_util.uncache('fine'):
......
"""Test that the semantics relating to the 'fromlist' argument are correct."""
from .. import util
from . import util as import_util
import imp
import unittest
class ReturnValue(unittest.TestCase):
......
"""Test case-sensitivity (PEP 235)."""
from importlib import _bootstrap
from importlib import machinery
from .. import util
from . import util as source_util
import imp
from importlib import _bootstrap
from importlib import machinery
import os
import sys
from test import support as test_support
......
from importlib import machinery
import importlib
import importlib.abc
import importlib.util
from .. import abc
from .. import util
from . import util as source_util
......@@ -13,6 +14,7 @@ import py_compile
import shutil
import stat
import sys
import types
import unittest
from test.support import make_legacy_pyc, unload
......@@ -112,7 +114,7 @@ class SimpleTest(unittest.TestCase):
value = '<test>'
name = '_temp'
with source_util.create_modules(name) as mapping:
orig_module = imp.new_module(name)
orig_module = types.ModuleType(name)
for attr in attributes:
setattr(orig_module, attr, value)
with open(mapping[name], 'w') as file:
......@@ -144,11 +146,11 @@ class SimpleTest(unittest.TestCase):
loader = machinery.SourceFileLoader('_temp', file_path)
mod = loader.load_module('_temp')
self.assertEqual(file_path, mod.__file__)
self.assertEqual(imp.cache_from_source(file_path),
self.assertEqual(importlib.util.cache_from_source(file_path),
mod.__cached__)
finally:
os.unlink(file_path)
pycache = os.path.dirname(imp.cache_from_source(file_path))
pycache = os.path.dirname(importlib.util.cache_from_source(file_path))
if os.path.exists(pycache):
shutil.rmtree(pycache)
......@@ -157,7 +159,7 @@ class SimpleTest(unittest.TestCase):
# truncated rather than raise an OverflowError.
with source_util.create_modules('_temp') as mapping:
source = mapping['_temp']
compiled = imp.cache_from_source(source)
compiled = importlib.util.cache_from_source(source)
with open(source, 'w') as f:
f.write("x = 5")
try:
......@@ -194,7 +196,7 @@ class BadBytecodeTest(unittest.TestCase):
pass
py_compile.compile(mapping[name])
if not del_source:
bytecode_path = imp.cache_from_source(mapping[name])
bytecode_path = importlib.util.cache_from_source(mapping[name])
else:
os.unlink(mapping[name])
bytecode_path = make_legacy_pyc(mapping[name])
......@@ -322,7 +324,8 @@ class SourceLoaderBadBytecodeTest(BadBytecodeTest):
def test(name, mapping, bytecode_path):
self.import_(mapping[name], name)
with open(bytecode_path, 'rb') as bytecode_file:
self.assertEqual(bytecode_file.read(4), imp.get_magic())
self.assertEqual(bytecode_file.read(4),
importlib.util.MAGIC_NUMBER)
self._test_bad_magic(test)
......@@ -372,7 +375,7 @@ class SourceLoaderBadBytecodeTest(BadBytecodeTest):
zeros = b'\x00\x00\x00\x00'
with source_util.create_modules('_temp') as mapping:
py_compile.compile(mapping['_temp'])
bytecode_path = imp.cache_from_source(mapping['_temp'])
bytecode_path = importlib.util.cache_from_source(mapping['_temp'])
with open(bytecode_path, 'r+b') as bytecode_file:
bytecode_file.seek(4)
bytecode_file.write(zeros)
......@@ -390,7 +393,7 @@ class SourceLoaderBadBytecodeTest(BadBytecodeTest):
with source_util.create_modules('_temp') as mapping:
# Create bytecode that will need to be re-created.
py_compile.compile(mapping['_temp'])
bytecode_path = imp.cache_from_source(mapping['_temp'])
bytecode_path = importlib.util.cache_from_source(mapping['_temp'])
with open(bytecode_path, 'r+b') as bytecode_file:
bytecode_file.seek(0)
bytecode_file.write(b'\x00\x00\x00\x00')
......
......@@ -3,7 +3,6 @@ from . import util as source_util
from importlib import machinery
import errno
import imp
import os
import py_compile
import stat
......
from . import util as source_util
from importlib import machinery
import imp
import unittest
......
......@@ -2,7 +2,6 @@ from .. import util
import contextlib
import errno
import functools
import imp
import os
import os.path
import sys
......
import importlib
import importlib.util
from importlib import abc
from importlib import machinery
import contextlib
import imp
import inspect
import io
import marshal
import os
import sys
from test import support
import types
import unittest
from unittest import mock
......@@ -140,7 +141,7 @@ class LoaderDefaultsTests(unittest.TestCase):
self.ins.load_module('something')
def test_module_repr(self):
mod = imp.new_module('blah')
mod = types.ModuleType('blah')
with self.assertRaises(NotImplementedError):
self.ins.module_repr(mod)
original_repr = repr(mod)
......@@ -205,7 +206,7 @@ class LoaderConcreteMethodTests(unittest.TestCase):
def test_init_module_attrs(self):
loader = LoaderSubclass()
module = imp.new_module('blah')
module = types.ModuleType('blah')
loader.init_module_attrs(module)
self.assertEqual(module.__loader__, loader)
......@@ -215,7 +216,7 @@ class InspectLoaderSourceToCodeTests(unittest.TestCase):
def source_to_module(self, data, path=None):
"""Help with source_to_code() tests."""
module = imp.new_module('blah')
module = types.ModuleType('blah')
loader = InspectLoaderSubclass()
if path is None:
code = loader.source_to_code(data)
......@@ -257,7 +258,7 @@ class InspectLoaderGetCodeTests(unittest.TestCase):
def test_get_code(self):
# Test success.
module = imp.new_module('blah')
module = types.ModuleType('blah')
with mock.patch.object(InspectLoaderSubclass, 'get_source') as mocked:
mocked.return_value = 'attr = 42'
loader = InspectLoaderSubclass()
......@@ -289,7 +290,7 @@ class InspectLoaderInitModuleTests(unittest.TestCase):
def init_module_attrs(self, name):
loader = InspectLoaderSubclass()
module = imp.new_module(name)
module = types.ModuleType(name)
loader.init_module_attrs(module)
self.assertEqual(module.__loader__, loader)
return module
......@@ -390,7 +391,7 @@ class ExecutionLoaderGetCodeTests(unittest.TestCase):
loader = ExecutionLoaderSubclass()
code = loader.get_code('blah')
self.assertEqual(code.co_filename, path)
module = imp.new_module('blah')
module = types.ModuleType('blah')
exec(code, module.__dict__)
self.assertEqual(module.attr, 42)
......@@ -420,7 +421,7 @@ class ExecutionLoaderGetCodeTests(unittest.TestCase):
loader = ExecutionLoaderSubclass()
code = loader.get_code('blah')
self.assertEqual(code.co_filename, '<string>')
module = imp.new_module('blah')
module = types.ModuleType('blah')
exec(code, module.__dict__)
self.assertEqual(module.attr, 42)
......@@ -444,7 +445,7 @@ class ExecutionLoaderInitModuleTests(unittest.TestCase):
path = os.path.join('some', 'path', '{}.py'.format(name))
with self.mock_methods(False, path):
loader = ExecutionLoaderSubclass()
module = imp.new_module(name)
module = types.ModuleType(name)
loader.init_module_attrs(module)
self.assertIs(module.__loader__, loader)
self.assertEqual(module.__file__, path)
......@@ -457,7 +458,7 @@ class ExecutionLoaderInitModuleTests(unittest.TestCase):
path = os.path.join('some', 'pkg', '__init__.py')
with self.mock_methods(True, path):
loader = ExecutionLoaderSubclass()
module = imp.new_module(name)
module = types.ModuleType(name)
loader.init_module_attrs(module)
self.assertIs(module.__loader__, loader)
self.assertEqual(module.__file__, path)
......@@ -471,7 +472,7 @@ class ExecutionLoaderInitModuleTests(unittest.TestCase):
path = os.path.join('some', 'pkg', 'submodule.py')
with self.mock_methods(False, path):
loader = ExecutionLoaderSubclass()
module = imp.new_module(name)
module = types.ModuleType(name)
loader.init_module_attrs(module)
self.assertEqual(module.__package__, 'pkg')
self.assertEqual(module.__file__, path)
......@@ -484,7 +485,7 @@ class ExecutionLoaderInitModuleTests(unittest.TestCase):
with self.mock_methods(False, path) as mocked_methods:
mocked_methods['get_filename'].side_effect = ImportError
loader = ExecutionLoaderSubclass()
module = imp.new_module(name)
module = types.ModuleType(name)
loader.init_module_attrs(module)
self.assertFalse(hasattr(module, '__file__'))
......@@ -515,9 +516,9 @@ class SourceLoaderMock(SourceOnlyLoaderMock):
source_mtime = 1
def __init__(self, path, magic=imp.get_magic()):
def __init__(self, path, magic=importlib.util.MAGIC_NUMBER):
super().__init__(path)
self.bytecode_path = imp.cache_from_source(self.path)
self.bytecode_path = importlib.util.cache_from_source(self.path)
self.source_size = len(self.source)
data = bytearray(magic)
data.extend(importlib._w_long(self.source_mtime))
......@@ -557,7 +558,7 @@ class SourceLoaderTestHarness(unittest.TestCase):
module_name = 'mod'
self.path = os.path.join(self.package, '.'.join(['mod', 'py']))
self.name = '.'.join([self.package, module_name])
self.cached = imp.cache_from_source(self.path)
self.cached = importlib.util.cache_from_source(self.path)
self.loader = self.loader_mock(self.path, **kwargs)
def verify_module(self, module):
......@@ -574,7 +575,7 @@ class SourceLoaderTestHarness(unittest.TestCase):
self.assertEqual(values[4], repr(self.loader))
def verify_code(self, code_object):
module = imp.new_module(self.name)
module = types.ModuleType(self.name)
module.__file__ = self.path
module.__cached__ = self.cached
module.__package__ = self.package
......@@ -673,7 +674,7 @@ class SourceLoaderBytecodeTests(SourceLoaderTestHarness):
super().verify_code(code_object)
if bytecode_written:
self.assertIn(self.cached, self.loader.written)
data = bytearray(imp.get_magic())
data = bytearray(importlib.util.MAGIC_NUMBER)
data.extend(importlib._w_long(self.loader.source_mtime))
data.extend(importlib._w_long(self.loader.source_size))
data.extend(marshal.dumps(code_object))
......@@ -689,7 +690,7 @@ class SourceLoaderBytecodeTests(SourceLoaderTestHarness):
self.loader.bytecode_path = "<does not exist>"
# Sanity check
with self.assertRaises(OSError):
bytecode_path = imp.cache_from_source(self.path)
bytecode_path = importlib.util.cache_from_source(self.path)
self.loader.get_data(bytecode_path)
code_object = self.loader.get_code(self.name)
self.verify_code(code_object, bytecode_written=True)
......@@ -787,26 +788,26 @@ class SourceLoaderInitModuleAttrTests(unittest.TestCase):
"""Tests for importlib.abc.SourceLoader.init_module_attrs()."""
def test_init_module_attrs(self):
# If __file__ set, __cached__ == imp.cached_from_source(__file__).
# If __file__ set, __cached__ == importlib.util.cached_from_source(__file__).
name = 'blah'
path = 'blah.py'
loader = SourceOnlyLoaderMock(path)
module = imp.new_module(name)
module = types.ModuleType(name)
loader.init_module_attrs(module)
self.assertEqual(module.__loader__, loader)
self.assertEqual(module.__package__, '')
self.assertEqual(module.__file__, path)
self.assertEqual(module.__cached__, imp.cache_from_source(path))
self.assertEqual(module.__cached__, importlib.util.cache_from_source(path))
@mock.patch('importlib._bootstrap.cache_from_source')
def test_cache_from_source_NotImplementedError(self, mock_cache_from_source):
# If imp.cache_from_source() raises NotImplementedError don't set
# If importlib.util.cache_from_source() raises NotImplementedError don't set
# __cached__.
mock_cache_from_source.side_effect = NotImplementedError
name = 'blah'
path = 'blah.py'
loader = SourceOnlyLoaderMock(path)
module = imp.new_module(name)
module = types.ModuleType(name)
loader.init_module_attrs(module)
self.assertEqual(module.__file__, path)
self.assertFalse(hasattr(module, '__cached__'))
......@@ -817,7 +818,7 @@ class SourceLoaderInitModuleAttrTests(unittest.TestCase):
mocked.side_effect = ImportError
name = 'blah'
loader = SourceOnlyLoaderMock('blah.py')
module = imp.new_module(name)
module = types.ModuleType(name)
loader.init_module_attrs(module)
self.assertFalse(hasattr(module, '__file__'))
self.assertFalse(hasattr(module, '__cached__'))
......
from . import util
import imp
import importlib
from importlib import _bootstrap
from importlib import machinery
......@@ -99,7 +99,7 @@ class FindLoaderTests(unittest.TestCase):
# If a module with __loader__ is in sys.modules, then return it.
name = 'some_mod'
with util.uncache(name):
module = imp.new_module(name)
module = types.ModuleType(name)
loader = 'a loader!'
module.__loader__ = loader
sys.modules[name] = module
......@@ -110,7 +110,7 @@ class FindLoaderTests(unittest.TestCase):
# If sys.modules[name].__loader__ is None, raise ValueError.
name = 'some_mod'
with util.uncache(name):
module = imp.new_module(name)
module = types.ModuleType(name)
module.__loader__ = None
sys.modules[name] = module
with self.assertRaises(ValueError):
......@@ -121,7 +121,7 @@ class FindLoaderTests(unittest.TestCase):
# Issue #17099
name = 'some_mod'
with util.uncache(name):
module = imp.new_module(name)
module = types.ModuleType(name)
try:
del module.__loader__
except AttributeError:
......@@ -189,7 +189,7 @@ class InvalidateCacheTests(unittest.TestCase):
def test_method_lacking(self):
# There should be no issues if the method is not defined.
key = 'gobbledeegook'
sys.path_importer_cache[key] = imp.NullImporter('abc')
sys.path_importer_cache[key] = None
self.addCleanup(lambda: sys.path_importer_cache.__delitem__(key))
importlib.invalidate_caches() # Shouldn't trigger an exception.
......
from importlib import util
from . import util as test_util
import imp
import os
import sys
from test import support
......@@ -40,14 +40,14 @@ class ModuleToLoadTests(unittest.TestCase):
def test_reload(self):
# Test that the same module is in sys.modules.
created_module = imp.new_module(self.module_name)
created_module = types.ModuleType(self.module_name)
sys.modules[self.module_name] = created_module
with util.module_to_load(self.module_name) as module:
self.assertIs(module, created_module)
def test_reload_failed(self):
# Test that the module was left in sys.modules.
created_module = imp.new_module(self.module_name)
created_module = types.ModuleType(self.module_name)
sys.modules[self.module_name] = created_module
try:
with util.module_to_load(self.module_name) as module:
......@@ -60,7 +60,7 @@ class ModuleToLoadTests(unittest.TestCase):
def test_reset_name(self):
# If reset_name is true then module.__name__ = name, else leave it be.
odd_name = 'not your typical name'
created_module = imp.new_module(self.module_name)
created_module = types.ModuleType(self.module_name)
created_module.__name__ = odd_name
sys.modules[self.module_name] = created_module
with util.module_to_load(self.module_name) as module:
......@@ -119,7 +119,7 @@ class ModuleForLoaderTests(unittest.TestCase):
def load_module(self, module):
return module
name = 'a.b.c'
module = imp.new_module('a.b.c')
module = types.ModuleType('a.b.c')
module.__loader__ = 42
module.__package__ = 42
with test_util.uncache(name):
......@@ -141,7 +141,7 @@ class ModuleForLoaderTests(unittest.TestCase):
def test_reload_failure(self):
# Test that a failure on reload leaves the module in-place.
name = 'a.b.c'
module = imp.new_module(name)
module = types.ModuleType(name)
with test_util.uncache(name):
sys.modules[name] = module
self.raise_exception(name)
......@@ -212,26 +212,26 @@ class SetPackageTests(unittest.TestCase):
def test_top_level(self):
# __package__ should be set to the empty string if a top-level module.
# Implicitly tests when package is set to None.
module = imp.new_module('module')
module = types.ModuleType('module')
module.__package__ = None
self.verify(module, '')
def test_package(self):
# Test setting __package__ for a package.
module = imp.new_module('pkg')
module = types.ModuleType('pkg')
module.__path__ = ['<path>']
module.__package__ = None
self.verify(module, 'pkg')
def test_submodule(self):
# Test __package__ for a module in a package.
module = imp.new_module('pkg.mod')
module = types.ModuleType('pkg.mod')
module.__package__ = None
self.verify(module, 'pkg')
def test_setting_if_missing(self):
# __package__ should be set if it is missing.
module = imp.new_module('mod')
module = types.ModuleType('mod')
if hasattr(module, '__package__'):
delattr(module, '__package__')
self.verify(module, '')
......@@ -239,7 +239,7 @@ class SetPackageTests(unittest.TestCase):
def test_leaving_alone(self):
# If __package__ is set and not None then leave it alone.
for value in (True, False):
module = imp.new_module('mod')
module = types.ModuleType('mod')
module.__package__ = value
self.verify(module, value)
......@@ -261,7 +261,7 @@ class SetLoaderTests(unittest.TestCase):
def test_no_attribute(self):
loader = self.DummyLoader()
loader.module = imp.new_module('blah')
loader.module = types.ModuleType('blah')
try:
del loader.module.__loader__
except AttributeError:
......@@ -270,13 +270,13 @@ class SetLoaderTests(unittest.TestCase):
def test_attribute_is_None(self):
loader = self.DummyLoader()
loader.module = imp.new_module('blah')
loader.module = types.ModuleType('blah')
loader.module.__loader__ = None
self.assertEqual(loader, loader.load_module('blah').__loader__)
def test_not_reset(self):
loader = self.DummyLoader()
loader.module = imp.new_module('blah')
loader.module = types.ModuleType('blah')
loader.module.__loader__ = 42
self.assertEqual(42, loader.load_module('blah').__loader__)
......@@ -331,7 +331,7 @@ class PEP3147Tests(unittest.TestCase):
"""
tag = imp.get_tag()
tag = sys.implementation.cache_tag
@unittest.skipUnless(sys.implementation.cache_tag is not None,
'requires sys.implementation.cache_tag not be None')
......
from contextlib import contextmanager
import imp
import os.path
from test import support
import unittest
import sys
import types
CASE_INSENSITIVE_FS = True
......@@ -98,7 +98,7 @@ class mock_modules:
package = name.rsplit('.', 1)[0]
else:
package = import_name
module = imp.new_module(import_name)
module = types.ModuleType(import_name)
module.__loader__ = self
module.__file__ = '<mock __file__>'
module.__package__ = package
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment