10000 Issue #16803: test.test_importlib.import_ now tests frozen and source… · python/cpython@330f71b · GitHub
[go: up one dir, main page]

Skip to content

Commit 330f71b

Browse files
committed
Issue #16803: test.test_importlib.import_ now tests frozen and source code
1 parent a3c6963 commit 330f71b

File tree

11 files changed

+146
-154
lines changed

11 files changed

+146
-154
lines changed

Lib/test/test_importlib/__main__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,4 @@
1515
parser.add_argument('-b', '--builtin', action='store_true', default=False,
1616
help='use builtins.__import__() instead of importlib')
1717
args = parser.parse_args()
18-
if args.builtin:
19-
util.using___import__ = True
2018
test_main()

Lib/test/test_importlib/import_/test___loader__.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def load_module(self, fullname):
1616
return self.module
1717

1818

19-
class LoaderAttributeTests(unittest.TestCase):
19+
class LoaderAttributeTests:
2020

2121
def test___loader___missing(self):
2222
module = types.ModuleType('blah')
@@ -27,7 +27,7 @@ def test___loader___missing(self):
2727
loader = LoaderMock()
2828
loader.module = module
2929
with util.uncache('blah'), util.import_state(meta_path=[loader]):
30-
module = import_util.import_('blah')
30+
module = self.__import__('blah')
3131
self.assertEqual(loader, module.__loader__)
3232

3333
def test___loader___is_None(self):
@@ -36,9 +36,13 @@ def test___loader___is_None(self):
3636
loader = LoaderMock()
3737
loader.module = module
3838
with util.uncache('blah'), util.import_state(meta_path=[loader]):
39-
returned_module = import_util.import_('blah')
39+
returned_module = self.__import__('blah')
4040
self.assertEqual(loader, module.__loader__)
4141

4242

43+
Frozen_Tests, Source_Tests = util.test_both(LoaderAttributeTests,
44+
__import__=import_util.__import__)
45+
46+
4347
if __name__ == '__main__':
4448
unittest.main()

Lib/test/test_importlib/import_/test___package__.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from . import util as import_util
1010

1111

12-
class Using__package__(unittest.TestCase):
12+
class Using__package__:
1313

1414
"""Use of __package__ supercedes the use of __name__/__path__ to calculate
1515
what package a module belongs to. The basic algorithm is [__package__]::
@@ -38,8 +38,8 @@ def test_using___package__(self):
3838
# [__package__]
3939
with util.mock_modules('pkg.__init__', 'pkg.fake') as importer:
4040
with util.import_state(meta_path=[importer]):
41-
import_util.import_('pkg.fake')
42-
module = import_util.import_('',
41+
self.__import__('pkg.fake')
42+
module = self.__import__('',
4343
globals={'__package__': 'pkg.fake'},
4444
fromlist=['attr'], level=2)
4545
self.assertEqual(module.__name__, 'pkg')
@@ -51,8 +51,8 @@ def test_using___name__(self, package_as_None=False):
5151
globals_['__package__'] = None
5252
with util.mock_modules('pkg.__init__', 'pkg.fake') as importer:
5353
with util.import_state(meta_path=[importer]):
54-
import_util.import_('pkg.fake')
55-
module = import_util.import_('', globals= globals_,
54+
self.__import__('pkg.fake')
55+
module = self.__import__('', globals= globals_,
5656
fromlist=['attr'], level=2)
5757
self.assertEqual(module.__name__, 'pkg')
5858

@@ -63,15 +63,17 @@ def test_None_as___package__(self):
6363
def test_bad__package__(self):
6464
globals = {'__package__': '<not real>'}
6565
with self.assertRaises(SystemError):
66-
import_util.import_('', globals, {}, ['relimport'], 1)
66+
self.__import__('', globals, {}, ['relimport'], 1)
6767

6868
def test_bunk__package__(self):
6969
globals = {'__package__': 42}
7070
with self.assertRaises(TypeError):
71-
import_util.import_('', globals, {}, ['relimport'], 1)
71+
self.__import__('', globals, {}, ['relimport'], 1)
72+
73+
Frozen_UsingPackage, Source_UsingPackage = util.test_both(
74+
Using__package__, __import__=import_util.__import__)
7275

7376

74-
@import_util.importlib_only
7577
class Setting__package__(unittest.TestCase):
7678

7779
"""Because __package__ is a new feature, it is not always set by a loader.
@@ -84,36 +86,33 @@ class Setting__package__(unittest.TestCase):
8486
8587
"""
8688

89+
__import__ = import_util.__import__[1]
90+
8791
# [top-level]
8892
def test_top_level(self):
8993
with util.mock_modules('top_level') as mock:
9094
with util.import_state(meta_path=[mock]):
9195
del mock['top_level'].__package__
92-
module = import_util.import_('top_level')
96+
module = self.__import__('top_level')
9397
self.assertEqual(module.__package__, '')
9498

9599
# [package]
96100
def test_package(self):
97101
with util.mock_modules('pkg.__init__') as mock:
98102
with util.import_state(meta_path=[mock]):
99103
del mock['pkg'].__package__
100-
module = import_util.import_('pkg')
104+
module = self.__import__('pkg')
101105
self.assertEqual(module.__package__, 'pkg')
102106

103107
# [submodule]
104108
def test_submodule(self):
105109
with util.mock_modules('pkg.__init__', 'pkg.mod') as mock:
106110
with util.import_state(meta_path=[mock]):
107111
del mock['pkg.mod'].__package__
108-
pkg = import_util.import_('pkg.mod')
112+
pkg = self.__import__('pkg.mod')
109113
module = getattr(pkg, 'mod')
110114
self.assertEqual(module.__package__, 'pkg')
111115

112116

113-
def test_main():
114-
from test.support import run_unittest
115-
run_unittest(Using__package__, Setting__package__)
116-
117-
118117
if __name__ == '__main__':
119-
test_main()
118+
unittest.main()
Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from .. import util as importlib_test_util
2-
from . import util
1+
from .. import util
2+
from . import util as import_util
33
import sys
44
import types
55
import unittest
@@ -17,51 +17,48 @@ def load_module(cls, fullname):
1717
raise ImportError('I cannot be loaded!')
1818

1919

20-
class APITest(unittest.TestCase):
20+
class APITest:
2121

2222
"""Test API-specific details for __import__ (e.g. raising the right
2323
exception when passing in an int for the module name)."""
2424

2525
def test_name_requires_rparition(self):
2626
# Raise TypeError if a non-string is passed in for the module name.
2727
with self.assertRaises(TypeError):
28-
util.import_(42)
28+
self.__import__(42)
2929

3030
def test_negative_level(self):
3131
# Raise ValueError when a negative level is specified.
3232
# PEP 328 did away with sys.module None entries and the ambiguity of
3333
# absolute/relative imports.
3434
with self.assertRaises(ValueError):
35-
util.import_('os', globals(), level=-1)
35+
self.__import__('os', globals(), level=-1)
3636

3737
def test_nonexistent_fromlist_entry(self):
3838
# If something in fromlist doesn't exist, that's okay.
3939
# issue15715
4040
mod = types.ModuleType('fine')
4141
mod.__path__ = ['XXX']
42-
with importlib_test_util.import_state(meta_path=[BadLoaderFinder]):
43-
with importlib_test_util.uncache('fine'):
42+
with util.import_state(meta_path=[BadLoaderFinder]):
43+
with util.uncache('fine'):
4444
sys.modules['fine'] = mod
45-
util.import_('fine', fromlist=['not here'])
45+
self.__import__('fine', fromlist=['not here'])
4646

4747
def test_fromlist_load_error_propagates(self):
4848
# If something in fromlist triggers an exception not related to not
4949
# existing, let that exception propagate.
5050
# issue15316
5151
mod = types.ModuleType('fine')
5252
mod.__path__ = ['XXX']
53-
with importlib_test_util.import_state(meta_path=[BadLoaderFinder]):
54-
with importlib_test_util.uncache('fine'):
53+
with util.import_state(meta_path=[BadLoaderFinder]):
54+
with util.uncache('fine'):
5555
sys.modules['fine'] = mod
5656
with self.assertRaises(ImportError):
57-
util.import_('fine', fromlist=['bogus'])
57+
self.__import__('fine', fromlist=['bogus'])
5858

59-
60-
61-
def test_main():
62-
from test.support import run_unittest
63-
run_unittest(APITest)
59+
Frozen_APITests, Source_APITests = util.test_both(
60+
APITest, __import__=import_util.__import__)
6461

6562

6663
if __name__ == '__main__':
67-
test_main()
64+
unittest.main()

Lib/test/test_importlib/import_/test_caching.py

Lines changed: 16 additions & 14 deletions
< 1241 td data-grid-cell-id="diff-c5c92c953006bc4938f7d13090510ef6dcd4366f5fac6159e3f2c7cc7b26ccde-12-12-0" data-selected="false" role="gridcell" style="background-color:var(--bgColor-default);text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative diff-line-number-neutral left-side">12
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import unittest
77

88

9-
class UseCache(unittest.TestCase):
9+
class UseCache:
1010

1111
"""When it comes to sys.modules, import prefers it over anything else.
12
@@ -21,12 +21,13 @@ class UseCache(unittest.TestCase):
2121
ImportError is raised [None in cache].
2222
2323
"""
24+
2425
def test_using_cache(self):
2526
# [use cache]
2627
module_to_use = "some module found!"
2728
with util.uncache('some_module'):
2829
sys.modules['some_module'] = module_to_use
29-
module = import_util.import_('some_module')
30+
module = self.__import__('some_module')
3031
self.assertEqual(id(module_to_use), id(module))
3132

3233
def test_None_in_cache(self):
@@ -35,7 +36,7 @@ def test_None_in_cache(self):
3536
with util.uncache(name):
3637
sys.modules[name] = None
3738
with self.assertRaises(ImportError) as cm:
38-
import_util.import_(name)
39+
self.__import__(name)
3940
self.assertEqual(cm.exception.name, name)
4041

4142
def create_mock(self, *names, return_=None):
@@ -47,42 +48,43 @@ def load_module(self, fullname):
4748
mock.load_module = MethodType(load_module, mock)
4849
return mock
4950

51+
Frozen_UseCache, Source_UseCache = util.test_both(
52+
UseCache, __import__=import_util.__import__)
53+
54+
55+
class ImportlibUseCache(UseCache, unittest.TestCase):
56+
57+
__import__ = import_util.__import__[1]
58+
5059
# __import__ inconsistent between loaders and built-in import when it comes
5160
# to when to use the module in sys.modules and when not to.
52-
@import_util.importlib_only
5361
def test_using_cache_after_loader(self):
5462
# [from cache on return]
5563
with self.create_mock('module') as mock:
5664
with util.import_state(meta_path=[mock]):
57-
module = import_util.import_('module')
65+
module = self.__import__('module')
5866
self.assertEqual(id(module), id(sys.modules['module']))
5967

6068
# See test_using_cache_after_loader() for reasoning.
61-
@import_util.importlib_only
6269
def test_using_cache_for_assigning_to_attribute(self):
6370
# [from cache to attribute]
6471
with self.create_mock('pkg.__init__', 'pkg.module') as importer:
6572
with util.import_state(meta_path=[importer]):
66-
module = import_util.import_('pkg.module')
73+
module = self.__import__('pkg.module')
6774
self.assertTrue(hasattr(module, 'module'))
6875
self.assertEqual(id(module.module),
6976
id(sys.modules['pkg.module']))
7077

7178
# See test_using_cache_after_loader() for reasoning.
72-
@import_util.importlib_only
7379
def test_using_cache_for_fromlist(self):
7480
# [from cache for fromlist]
7581
with self.create_mock('pkg.__init__', 'pkg.module') as importer:
7682
with util.import_state(meta_path=[importer]):
77-
module = import_util.import_('pkg', fromlist=['module'])
83+
module = self.__import__('pkg', fromlist=['module'])
7884
self.assertTrue(hasattr(module, 'module'))
7985
self.assertEqual(id(module.module),
8086
id(sys.modules['pkg.module']))
8187

8288

83-
def test_main():
84-
from test.support import run_unittest
85-
run_unittest(UseCache)
86-
8789
if __name__ == '__main__':
88-
test_main()
90+
unittest.main()

0 commit comments

Comments
 (0)
0