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