Skip to content

Commit 1340049

Browse files
committed
Issue python#16803: Move test_importlib.test_util to use both frozen and
source code.
1 parent 6a57dd8 commit 1340049

File tree

2 files changed

+96
-50
lines changed

2 files changed

+96
-50
lines changed

Lib/test/test_importlib/test_util.py

Lines changed: 85 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from importlib import util
22
from . import util as test_util
3+
frozen_util, source_util = test_util.import_importlib('importlib.util')
34

45
import os
56
import sys
@@ -9,28 +10,31 @@
910
import warnings
1011

1112

12-
class DecodeSourceBytesTests(unittest.TestCase):
13+
class DecodeSourceBytesTests:
1314

1415
source = "string ='ü'"
1516

1617
def test_ut8_default(self):
1718
source_bytes = self.source.encode('utf-8')
18-
self.assertEqual(util.decode_source(source_bytes), self.source)
19+
self.assertEqual(self.util.decode_source(source_bytes), self.source)
1920

2021
def test_specified_encoding(self):
2122
source = '# coding=latin-1\n' + self.source
2223
source_bytes = source.encode('latin-1')
2324
assert source_bytes != source.encode('utf-8')
24-
self.assertEqual(util.decode_source(source_bytes), source)
25+
self.assertEqual(self.util.decode_source(source_bytes), source)
2526

2627
def test_universal_newlines(self):
2728
source = '\r\n'.join([self.source, self.source])
2829
source_bytes = source.encode('utf-8')
29-
self.assertEqual(util.decode_source(source_bytes),
30+
self.assertEqual(self.util.decode_source(source_bytes),
3031
'\n'.join([self.source, self.source]))
3132

33+
Frozen_DecodeSourceBytesTests, Source_DecodeSourceBytesTests = test_util.test_both(
34+
DecodeSourceBytesTests, util=[frozen_util, source_util])
3235

33-
class ModuleToLoadTests(unittest.TestCase):
36+
37+
class ModuleToLoadTests:
3438

3539
module_name = 'ModuleManagerTest_module'
3640

@@ -42,7 +46,7 @@ def test_new_module(self):
4246
# Test a new module is created, inserted into sys.modules, has
4347
# __initializing__ set to True after entering the context manager,
4448
# and __initializing__ set to False after exiting.
45-
with util.module_to_load(self.module_name) as module:
49+
with self.util.module_to_load(self.module_name) as module:
4650
self.assertIn(self.module_name, sys.modules)
4751
self.assertIs(sys.modules[self.module_name], module)
4852
self.assertTrue(module.__initializing__)
@@ -51,7 +55,7 @@ def test_new_module(self):
5155
def test_new_module_failed(self):
5256
# Test the module is removed from sys.modules.
5357
try:
54-
with util.module_to_load(self.module_name) as module:
58+
with self.util.module_to_load(self.module_name) as module:
5559
self.assertIn(self.module_name, sys.modules)
5660
raise exception
5761
except Exception:
@@ -63,15 +67,15 @@ def test_reload(self):
6367
# Test that the same module is in sys.modules.
6468
created_module = types.ModuleType(self.module_name)
6569
sys.modules[self.module_name] = created_module
66-
with util.module_to_load(self.module_name) as module:
70+
with self.util.module_to_load(self.module_name) as module:
6771
self.assertIs(module, created_module)
6872

6973
def test_reload_failed(self):
7074
# Test that the module was left in sys.modules.
7175
created_module = types.ModuleType(self.module_name)
7276
sys.modules[self.module_name] = created_module
7377
try:
74-
with util.module_to_load(self.module_name) as module:
78+
with self.util.module_to_load(self.module_name) as module:
7579
raise Exception
7680
except Exception:
7781
self.assertIn(self.module_name, sys.modules)
@@ -84,29 +88,33 @@ def test_reset_name(self):
8488
created_module = types.ModuleType(self.module_name)
8589
created_module.__name__ = odd_name
8690
sys.modules[self.module_name] = created_module
87-
with util.module_to_load(self.module_name) as module:
91+
with self.util.module_to_load(self.module_name) as module:
8892
self.assertEqual(module.__name__, self.module_name)
8993
created_module.__name__ = odd_name
90-
with util.module_to_load(self.module_name, reset_name=False) as module:
94+
with self.util.module_to_load(self.module_name, reset_name=False) as module:
9195
self.assertEqual(module.__name__, odd_name)
9296

97+
Frozen_ModuleToLoadTests, Source_ModuleToLoadTests = test_util.test_both(
98+
ModuleToLoadTests,
99+
util=[frozen_util, source_util])
100+
93101

94-
class ModuleForLoaderTests(unittest.TestCase):
102+
class ModuleForLoaderTests:
95103

96104
"""Tests for importlib.util.module_for_loader."""
97105

98-
@staticmethod
99-
def module_for_loader(func):
106+
@classmethod
107+
def module_for_loader(cls, func):
100108
with warnings.catch_warnings():
101109
warnings.simplefilter('ignore', PendingDeprecationWarning)
102-
return util.module_for_loader(func)
110+
return cls.util.module_for_loader(func)
103111

104112
def test_warning(self):
105113
# Should raise a PendingDeprecationWarning when used.
106114
with warnings.catch_warnings():
107115
warnings.simplefilter('error', PendingDeprecationWarning)
108116
with self.assertRaises(PendingDeprecationWarning):
109-
func = util.module_for_loader(lambda x: x)
117+
func = self.util.module_for_loader(lambda x: x)
110118

111119
def return_module(self, name):
112120
fxn = self.module_for_loader(lambda self, module: module)
@@ -216,16 +224,19 @@ def load_module(self, module):
216224
self.assertIs(module.__loader__, loader)
217225
self.assertEqual(module.__package__, name)
218226

227+
Frozen_ModuleForLoaderTests, Source_ModuleForLoaderTests = test_util.test_both(
228+
ModuleForLoaderTests, util=[frozen_util, source_util])
219229

220-
class SetPackageTests(unittest.TestCase):
230+
231+
class SetPackageTests:
221232

222233
"""Tests for importlib.util.set_package."""
223234

224235
def verify(self, module, expect):
225236
"""Verify the module has the expected value for __package__ after
226237
passing through set_package."""
227238
fxn = lambda: module
228-
wrapped = util.set_package(fxn)
239+
wrapped = self.util.set_package(fxn)
229240
wrapped()
230241
self.assertTrue(hasattr(module, '__package__'))
231242
self.assertEqual(expect, module.__package__)
@@ -266,12 +277,15 @@ def test_leaving_alone(self):
266277

267278
def test_decorator_attrs(self):
268279
def fxn(module): pass
269-
wrapped = util.set_package(fxn)
280+
wrapped = self.util.set_package(fxn)
270281
self.assertEqual(wrapped.__name__, fxn.__name__)
271282
self.assertEqual(wrapped.__qualname__, fxn.__qualname__)
272283

284+
Frozen_SetPackageTests, Source_SetPackageTests = test_util.test_both(
285+
SetPackageTests, util=[frozen_util, source_util])
286+
273287

274-
class SetLoaderTests(unittest.TestCase):
288+
class SetLoaderTests:
275289

276290
"""Tests importlib.util.set_loader()."""
277291

@@ -301,56 +315,73 @@ def test_not_reset(self):
301315
loader.module.__loader__ = 42
302316
self.assertEqual(42, loader.load_module('blah').__loader__)
303317

318+
class Frozen_SetLoaderTests(SetLoaderTests, unittest.TestCase):
319+
class DummyLoader:
320+
@frozen_util.set_loader
321+
def load_module(self, module):
322+
return self.module
304323

305-
class ResolveNameTests(unittest.TestCase):
324+
class Source_SetLoaderTests(SetLoaderTests, unittest.TestCase):
325+
class DummyLoader:
326+
@source_util.set_loader
327+
def load_module(self, module):
328+
return self.module
329+
330+
331+
class ResolveNameTests:
306332

307333
"""Tests importlib.util.resolve_name()."""
308334

309335
def test_absolute(self):
310336
# bacon
311-
self.assertEqual('bacon', util.resolve_name('bacon', None))
337+
self.assertEqual('bacon', self.util.resolve_name('bacon', None))
312338

313339
def test_aboslute_within_package(self):
314340
# bacon in spam
315-
self.assertEqual('bacon', util.resolve_name('bacon', 'spam'))
341+
self.assertEqual('bacon', self.util.resolve_name('bacon', 'spam'))
316342

317343
def test_no_package(self):
318344
# .bacon in ''
319345
with self.assertRaises(ValueError):
320-
util.resolve_name('.bacon', '')
346+
self.util.resolve_name('.bacon', '')
321347

322348
def test_in_package(self):
323349
# .bacon in spam
324350
self.assertEqual('spam.eggs.bacon',
325-
util.resolve_name('.bacon', 'spam.eggs'))
351+
self.util.resolve_name('.bacon', 'spam.eggs'))
326352

327353
def test_other_package(self):
328354
# ..bacon in spam.bacon
329355
self.assertEqual('spam.bacon',
330-
util.resolve_name('..bacon', 'spam.eggs'))
356+
self.util.resolve_name('..bacon', 'spam.eggs'))
331357

332358
def test_escape(self):
333359
# ..bacon in spam
334360
with self.assertRaises(ValueError):
335-
util.resolve_name('..bacon', 'spam')
361+
self.util.resolve_name('..bacon', 'spam')
336362

363+
Frozen_ResolveNameTests, Source_ResolveNameTests = test_util.test_both(
364+
ResolveNameTests,
365+
util=[frozen_util, source_util])
337366

338-
class MagicNumberTests(unittest.TestCase):
367+
368+
class MagicNumberTests:
339369

340370
def test_length(self):
341371
# Should be 4 bytes.
342-
self.assertEqual(len(util.MAGIC_NUMBER), 4)
372+
self.assertEqual(len(self.util.MAGIC_NUMBER), 4)
343373

344374
def test_incorporates_rn(self):
345375
# The magic number uses \r\n to come out wrong when splitting on lines.
346-
self.assertTrue(util.MAGIC_NUMBER.endswith(b'\r\n'))
376+
self.assertTrue(self.util.MAGIC_NUMBER.endswith(b'\r\n'))
377+
378+
Frozen_MagicNumberTests, Source_MagicNumberTests = test_util.test_both(
379+
MagicNumberTests, util=[frozen_util, source_util])
347380

348381

349-
class PEP3147Tests(unittest.TestCase):
350-
"""Tests of PEP 3147-related functions:
351-
cache_from_source and source_from_cache.
382+
class PEP3147Tests:
352383

353-
"""
384+
"""Tests of PEP 3147-related functions: cache_from_source and source_from_cache."""
354385

355386
tag = sys.implementation.cache_tag
356387

@@ -362,56 +393,56 @@ def test_cache_from_source(self):
362393
path = os.path.join('foo', 'bar', 'baz', 'qux.py')
363394
expect = os.path.join('foo', 'bar', 'baz', '__pycache__',
364395
'qux.{}.pyc'.format(self.tag))
365-
self.assertEqual(util.cache_from_source(path, True), expect)
396+
self.assertEqual(self.util.cache_from_source(path, True), expect)
366397

367398
def test_cache_from_source_no_cache_tag(self):
368399
# No cache tag means NotImplementedError.
369400
with support.swap_attr(sys.implementation, 'cache_tag', None):
370401
with self.assertRaises(NotImplementedError):
371-
util.cache_from_source('whatever.py')
402+
self.util.cache_from_source('whatever.py')
372403

373404
def test_cache_from_source_no_dot(self):
374405
# Directory with a dot, filename without dot.
375406
path = os.path.join('foo.bar', 'file')
376407
expect = os.path.join('foo.bar', '__pycache__',
377408
'file{}.pyc'.format(self.tag))
378-
self.assertEqual(util.cache_from_source(path, True), expect)
409+
self.assertEqual(self.util.cache_from_source(path, True), expect)
379410

380411
def test_cache_from_source_optimized(self):
381412
# Given the path to a .py file, return the path to its PEP 3147
382413
# defined .pyo file (i.e. under __pycache__).
383414
path = os.path.join('foo', 'bar', 'baz', 'qux.py')
384415
expect = os.path.join('foo', 'bar', 'baz', '__pycache__',
385416
'qux.{}.pyo'.format(self.tag))
386-
self.assertEqual(util.cache_from_source(path, False), expect)
417+
self.assertEqual(self.util.cache_from_source(path, False), expect)
387418

388419
def test_cache_from_source_cwd(self):
389420
path = 'foo.py'
390421
expect = os.path.join('__pycache__', 'foo.{}.pyc'.format(self.tag))
391-
self.assertEqual(util.cache_from_source(path, True), expect)
422+
self.assertEqual(self.util.cache_from_source(path, True), expect)
392423

393424
def test_cache_from_source_override(self):
394425
# When debug_override is not None, it can be any true-ish or false-ish
395426
# value.
396427
path = os.path.join('foo', 'bar', 'baz.py')
397428
partial_expect = os.path.join('foo', 'bar', '__pycache__',
398429
'baz.{}.py'.format(self.tag))
399-
self.assertEqual(util.cache_from_source(path, []), partial_expect + 'o')
400-
self.assertEqual(util.cache_from_source(path, [17]),
430+
self.assertEqual(self.util.cache_from_source(path, []), partial_expect + 'o')
431+
self.assertEqual(self.util.cache_from_source(path, [17]),
401432
partial_expect + 'c')
402433
# However if the bool-ishness can't be determined, the exception
403434
# propagates.
404435
class Bearish:
405436
def __bool__(self): raise RuntimeError
406437
with self.assertRaises(RuntimeError):
407-
util.cache_from_source('/foo/bar/baz.py', Bearish())
438+
self.util.cache_from_source('/foo/bar/baz.py', Bearish())
408439

409440
@unittest.skipUnless(os.sep == '\\' and os.altsep == '/',
410441
'test meaningful only where os.altsep is defined')
411442
def test_sep_altsep_and_sep_cache_from_source(self):
412443
# Windows path and PEP 3147 where sep is right of altsep.
413444
self.assertEqual(
414-
util.cache_from_source('\\foo\\bar\\baz/qux.py', True),
445+
self.util.cache_from_source('\\foo\\bar\\baz/qux.py', True),
415446
'\\foo\\bar\\baz\\__pycache__\\qux.{}.pyc'.format(self.tag))
416447

417448
@unittest.skipUnless(sys.implementation.cache_tag is not None,
@@ -423,43 +454,47 @@ def test_source_from_cache(self):
423454
path = os.path.join('foo', 'bar', 'baz', '__pycache__',
424455
'qux.{}.pyc'.format(self.tag))
425456
expect = os.path.join('foo', 'bar', 'baz', 'qux.py')
426-
self.assertEqual(util.source_from_cache(path), expect)
457+
self.assertEqual(self.util.source_from_cache(path), expect)
427458

428459
def test_source_from_cache_no_cache_tag(self):
429460
# If sys.implementation.cache_tag is None, raise NotImplementedError.
430461
path = os.path.join('blah', '__pycache__', 'whatever.pyc')
431462
with support.swap_attr(sys.implementation, 'cache_tag', None):
432463
with self.assertRaises(NotImplementedError):
433-
util.source_from_cache(path)
464+
self.util.source_from_cache(path)
434465

435466
def test_source_from_cache_bad_path(self):
436467
# When the path to a pyc file is not in PEP 3147 format, a ValueError
437468
# is raised.
438469
self.assertRaises(
439-
ValueError, util.source_from_cache, '/foo/bar/bazqux.pyc')
470+
ValueError, self.util.source_from_cache, '/foo/bar/bazqux.pyc')
440471

441472
def test_source_from_cache_no_slash(self):
442473
# No slashes at all in path -> ValueError
443474
self.assertRaises(
444-
ValueError, util.source_from_cache, 'foo.cpython-32.pyc')
475+
ValueError, self.util.source_from_cache, 'foo.cpython-32.pyc')
445476

446477
def test_source_from_cache_too_few_dots(self):
447478
# Too few dots in final path component -> ValueError
448479
self.assertRaises(
449-
ValueError, util.source_from_cache, '__pycache__/foo.pyc')
480+
ValueError, self.util.source_from_cache, '__pycache__/foo.pyc')
450481

451482
def test_source_from_cache_too_many_dots(self):
452483
# Too many dots in final path component -> ValueError
453484
self.assertRaises(
454-
ValueError, util.source_from_cache,
485+
ValueError, self.util.source_from_cache,
455486
'__pycache__/foo.cpython-32.foo.pyc')
456487

457488
def test_source_from_cache_no__pycache__(self):
458489
# Another problem with the path -> ValueError
459490
self.assertRaises(
460-
ValueError, util.source_from_cache,
491+
ValueError, self.util.source_from_cache,
461492
'/foo/bar/foo.cpython-32.foo.pyc')
462493

494+
Frozen_PEP3147Tests, Source_PEP3147Tests = test_util.test_both(
495+
PEP3147Tests,
496+
util=[frozen_util, source_util])
497+
463498

464499
if __name__ == '__main__':
465500
unittest.main()

Lib/test/test_importlib/util.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@ def import_importlib(module_name):
1515
return frozen, source
1616

1717

18+
def test_both(test_class, **kwargs):
19+
frozen_tests = types.new_class('Frozen_'+test_class.__name__,
20+
(test_class, unittest.TestCase))
21+
source_tests = types.new_class('Source_'+test_class.__name__,
22+
(test_class, unittest.TestCase))
23+
for attr, (frozen_value, source_value) in kwargs.items():
24+
setattr(frozen_tests, attr, frozen_value)
25+
setattr(source_tests, attr, source_value)
26+
return frozen_tests, source_tests
27+
28+
1829
CASE_INSENSITIVE_FS = True
1930
# Windows is the only OS that is *always* case-insensitive
2031
# (OS X *can* be case-sensitive).

0 commit comments

Comments
 (0)