cpython: 862043d74fae (original) (raw)
--- a/Lib/test/test_importlib/test_util.py +++ b/Lib/test/test_importlib/test_util.py @@ -1,5 +1,6 @@ from importlib import util from . import util as test_util +frozen_util, source_util = test_util.import_importlib('importlib.util') import os import sys @@ -9,28 +10,31 @@ import unittest import warnings -class DecodeSourceBytesTests(unittest.TestCase): +class DecodeSourceBytesTests: source = "string ='ΓΌ'" def test_ut8_default(self): source_bytes = self.source.encode('utf-8')
self.assertEqual(util.decode_source(source_bytes), self.source)[](#l1.21)
self.assertEqual(self.util.decode_source(source_bytes), self.source)[](#l1.22)
def test_specified_encoding(self): source = '# coding=latin-1\n' + self.source source_bytes = source.encode('latin-1') assert source_bytes != source.encode('utf-8')
self.assertEqual(util.decode_source(source_bytes), source)[](#l1.28)
self.assertEqual(self.util.decode_source(source_bytes), source)[](#l1.29)
def test_universal_newlines(self): source = '\r\n'.join([self.source, self.source]) source_bytes = source.encode('utf-8')
self.assertEqual(util.decode_source(source_bytes),[](#l1.34)
self.assertEqual(self.util.decode_source(source_bytes),[](#l1.35) '\n'.join([self.source, self.source]))[](#l1.36)
+Frozen_DecodeSourceBytesTests, Source_DecodeSourceBytesTests = test_util.test_both(
DecodeSourceBytesTests, util=[frozen_util, source_util])[](#l1.39)
-class ModuleToLoadTests(unittest.TestCase): + +class ModuleToLoadTests: module_name = 'ModuleManagerTest_module' @@ -42,7 +46,7 @@ class ModuleToLoadTests(unittest.TestCas # Test a new module is created, inserted into sys.modules, has # initializing set to True after entering the context manager, # and initializing set to False after exiting.
with util.module_to_load(self.module_name) as module:[](#l1.51)
with self.util.module_to_load(self.module_name) as module:[](#l1.52) self.assertIn(self.module_name, sys.modules)[](#l1.53) self.assertIs(sys.modules[self.module_name], module)[](#l1.54) self.assertTrue(module.__initializing__)[](#l1.55)
@@ -51,7 +55,7 @@ class ModuleToLoadTests(unittest.TestCas def test_new_module_failed(self): # Test the module is removed from sys.modules. try:
with util.module_to_load(self.module_name) as module:[](#l1.60)
with self.util.module_to_load(self.module_name) as module:[](#l1.61) self.assertIn(self.module_name, sys.modules)[](#l1.62) raise exception[](#l1.63) except Exception:[](#l1.64)
@@ -63,7 +67,7 @@ class ModuleToLoadTests(unittest.TestCas # Test that the same module is in sys.modules. created_module = types.ModuleType(self.module_name) sys.modules[self.module_name] = created_module
with util.module_to_load(self.module_name) as module:[](#l1.69)
with self.util.module_to_load(self.module_name) as module:[](#l1.70) self.assertIs(module, created_module)[](#l1.71)
def test_reload_failed(self): @@ -71,7 +75,7 @@ class ModuleToLoadTests(unittest.TestCas 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:[](#l1.78)
with self.util.module_to_load(self.module_name) as module:[](#l1.79) raise Exception[](#l1.80) except Exception:[](#l1.81) self.assertIn(self.module_name, sys.modules)[](#l1.82)
@@ -84,29 +88,33 @@ class ModuleToLoadTests(unittest.TestCas 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:[](#l1.87)
with self.util.module_to_load(self.module_name) as module:[](#l1.88) self.assertEqual(module.__name__, self.module_name)[](#l1.89) created_module.__name__ = odd_name[](#l1.90)
with util.module_to_load(self.module_name, reset_name=False) as module:[](#l1.91)
with self.util.module_to_load(self.module_name, reset_name=False) as module:[](#l1.92) self.assertEqual(module.__name__, odd_name)[](#l1.93)
+Frozen_ModuleToLoadTests, Source_ModuleToLoadTests = test_util.test_both(
ModuleToLoadTests,[](#l1.96)
util=[frozen_util, source_util])[](#l1.97)
-class ModuleForLoaderTests(unittest.TestCase): + +class ModuleForLoaderTests: """Tests for importlib.util.module_for_loader."""
- @classmethod
- def module_for_loader(cls, func): with warnings.catch_warnings(): warnings.simplefilter('ignore', PendingDeprecationWarning)
return util.module_for_loader(func)[](#l1.111)
return cls.util.module_for_loader(func)[](#l1.112)
def test_warning(self): # Should raise a PendingDeprecationWarning when used. with warnings.catch_warnings(): warnings.simplefilter('error', PendingDeprecationWarning) with self.assertRaises(PendingDeprecationWarning):
func = util.module_for_loader(lambda x: x)[](#l1.119)
func = self.util.module_for_loader(lambda x: x)[](#l1.120)
def return_module(self, name): fxn = self.module_for_loader(lambda self, module: module) @@ -216,8 +224,11 @@ class ModuleForLoaderTests(unittest.Test self.assertIs(module.loader, loader) self.assertEqual(module.package, name) +Frozen_ModuleForLoaderTests, Source_ModuleForLoaderTests = test_util.test_both(
ModuleForLoaderTests, util=[frozen_util, source_util])[](#l1.129)
-class SetPackageTests(unittest.TestCase): + +class SetPackageTests: """Tests for importlib.util.set_package.""" @@ -225,7 +236,7 @@ class SetPackageTests(unittest.TestCase) """Verify the module has the expected value for package after passing through set_package.""" fxn = lambda: module
wrapped = util.set_package(fxn)[](#l1.141)
wrapped = self.util.set_package(fxn)[](#l1.142) wrapped()[](#l1.143) self.assertTrue(hasattr(module, '__package__'))[](#l1.144) self.assertEqual(expect, module.__package__)[](#l1.145)
@@ -266,12 +277,15 @@ class SetPackageTests(unittest.TestCase) def test_decorator_attrs(self): def fxn(module): pass
wrapped = util.set_package(fxn)[](#l1.150)
wrapped = self.util.set_package(fxn)[](#l1.151) self.assertEqual(wrapped.__name__, fxn.__name__)[](#l1.152) self.assertEqual(wrapped.__qualname__, fxn.__qualname__)[](#l1.153)
+Frozen_SetPackageTests, Source_SetPackageTests = test_util.test_both(
SetPackageTests, util=[frozen_util, source_util])[](#l1.156)
-class SetLoaderTests(unittest.TestCase): + +class SetLoaderTests: """Tests importlib.util.set_loader().""" @@ -301,56 +315,73 @@ class SetLoaderTests(unittest.TestCase): loader.module.loader = 42 self.assertEqual(42, loader.load_module('blah').loader) +class Frozen_SetLoaderTests(SetLoaderTests, unittest.TestCase):
- class DummyLoader:
@frozen_util.set_loader[](#l1.170)
def load_module(self, module):[](#l1.171)
return self.module[](#l1.172)
-class ResolveNameTests(unittest.TestCase): +class Source_SetLoaderTests(SetLoaderTests, unittest.TestCase):
- class DummyLoader:
@source_util.set_loader[](#l1.177)
def load_module(self, module):[](#l1.178)
return self.module[](#l1.179)
+ + +class ResolveNameTests: """Tests importlib.util.resolve_name().""" def test_absolute(self): # bacon
self.assertEqual('bacon', util.resolve_name('bacon', None))[](#l1.188)
self.assertEqual('bacon', self.util.resolve_name('bacon', None))[](#l1.189)
def test_aboslute_within_package(self): # bacon in spam
self.assertEqual('bacon', util.resolve_name('bacon', 'spam'))[](#l1.193)
self.assertEqual('bacon', self.util.resolve_name('bacon', 'spam'))[](#l1.194)
def test_no_package(self): # .bacon in '' with self.assertRaises(ValueError):
util.resolve_name('.bacon', '')[](#l1.199)
self.util.resolve_name('.bacon', '')[](#l1.200)
def test_in_package(self): # .bacon in spam self.assertEqual('spam.eggs.bacon',
util.resolve_name('.bacon', 'spam.eggs'))[](#l1.205)
self.util.resolve_name('.bacon', 'spam.eggs'))[](#l1.206)
def test_other_package(self): # ..bacon in spam.bacon self.assertEqual('spam.bacon',
util.resolve_name('..bacon', 'spam.eggs'))[](#l1.211)
self.util.resolve_name('..bacon', 'spam.eggs'))[](#l1.212)
def test_escape(self): # ..bacon in spam with self.assertRaises(ValueError):
util.resolve_name('..bacon', 'spam')[](#l1.217)
self.util.resolve_name('..bacon', 'spam')[](#l1.218)
+ +Frozen_ResolveNameTests, Source_ResolveNameTests = test_util.test_both(
ResolveNameTests,[](#l1.221)
util=[frozen_util, source_util])[](#l1.222)
-class MagicNumberTests(unittest.TestCase): +class MagicNumberTests: def test_length(self): # Should be 4 bytes.
self.assertEqual(len(util.MAGIC_NUMBER), 4)[](#l1.230)
self.assertEqual(len(self.util.MAGIC_NUMBER), 4)[](#l1.231)
def test_incorporates_rn(self): # The magic number uses \r\n to come out wrong when splitting on lines.
self.assertTrue(util.MAGIC_NUMBER.endswith(b'\r\n'))[](#l1.235)
self.assertTrue(self.util.MAGIC_NUMBER.endswith(b'\r\n'))[](#l1.236)
+ +Frozen_MagicNumberTests, Source_MagicNumberTests = test_util.test_both(
MagicNumberTests, util=[frozen_util, source_util])[](#l1.239)
-class PEP3147Tests(unittest.TestCase):
tag = sys.implementation.cache_tag @@ -362,20 +393,20 @@ class PEP3147Tests(unittest.TestCase): path = os.path.join('foo', 'bar', 'baz', 'qux.py') expect = os.path.join('foo', 'bar', 'baz', 'pycache', 'qux.{}.pyc'.format(self.tag))
self.assertEqual(util.cache_from_source(path, True), expect)[](#l1.256)
self.assertEqual(self.util.cache_from_source(path, True), expect)[](#l1.257)
def test_cache_from_source_no_cache_tag(self): # No cache tag means NotImplementedError. with support.swap_attr(sys.implementation, 'cache_tag', None): with self.assertRaises(NotImplementedError):
util.cache_from_source('whatever.py')[](#l1.263)
self.util.cache_from_source('whatever.py')[](#l1.264)
def test_cache_from_source_no_dot(self): # Directory with a dot, filename without dot. path = os.path.join('foo.bar', 'file') expect = os.path.join('foo.bar', 'pycache', 'file{}.pyc'.format(self.tag))
self.assertEqual(util.cache_from_source(path, True), expect)[](#l1.271)
self.assertEqual(self.util.cache_from_source(path, True), expect)[](#l1.272)
def test_cache_from_source_optimized(self): # Given the path to a .py file, return the path to its PEP 3147 @@ -383,12 +414,12 @@ class PEP3147Tests(unittest.TestCase): path = os.path.join('foo', 'bar', 'baz', 'qux.py') expect = os.path.join('foo', 'bar', 'baz', 'pycache', 'qux.{}.pyo'.format(self.tag))
self.assertEqual(util.cache_from_source(path, False), expect)[](#l1.280)
self.assertEqual(self.util.cache_from_source(path, False), expect)[](#l1.281)
def test_cache_from_source_cwd(self): path = 'foo.py' expect = os.path.join('pycache', 'foo.{}.pyc'.format(self.tag))
self.assertEqual(util.cache_from_source(path, True), expect)[](#l1.286)
self.assertEqual(self.util.cache_from_source(path, True), expect)[](#l1.287)
def test_cache_from_source_override(self): # When debug_override is not None, it can be any true-ish or false-ish @@ -396,22 +427,22 @@ class PEP3147Tests(unittest.TestCase): path = os.path.join('foo', 'bar', 'baz.py') partial_expect = os.path.join('foo', 'bar', 'pycache', 'baz.{}.py'.format(self.tag))
self.assertEqual(util.cache_from_source(path, []), partial_expect + 'o')[](#l1.295)
self.assertEqual(util.cache_from_source(path, [17]),[](#l1.296)
self.assertEqual(self.util.cache_from_source(path, []), partial_expect + 'o')[](#l1.297)
self.assertEqual(self.util.cache_from_source(path, [17]),[](#l1.298) partial_expect + 'c')[](#l1.299) # However if the bool-ishness can't be determined, the exception[](#l1.300) # propagates.[](#l1.301) class Bearish:[](#l1.302) def __bool__(self): raise RuntimeError[](#l1.303) with self.assertRaises(RuntimeError):[](#l1.304)
util.cache_from_source('/foo/bar/baz.py', Bearish())[](#l1.305)
self.util.cache_from_source('/foo/bar/baz.py', Bearish())[](#l1.306)
@unittest.skipUnless(os.sep == '\' and os.altsep == '/', 'test meaningful only where os.altsep is defined') def test_sep_altsep_and_sep_cache_from_source(self): # Windows path and PEP 3147 where sep is right of altsep. self.assertEqual(
util.cache_from_source('\\foo\\bar\\baz/qux.py', True),[](#l1.313)
self.util.cache_from_source('\\foo\\bar\\baz/qux.py', True),[](#l1.314) '\\foo\\bar\\baz\\__pycache__\\qux.{}.pyc'.format(self.tag))[](#l1.315)
@unittest.skipUnless(sys.implementation.cache_tag is not None, @@ -423,43 +454,47 @@ class PEP3147Tests(unittest.TestCase): path = os.path.join('foo', 'bar', 'baz', 'pycache', 'qux.{}.pyc'.format(self.tag)) expect = os.path.join('foo', 'bar', 'baz', 'qux.py')
self.assertEqual(util.source_from_cache(path), expect)[](#l1.322)
self.assertEqual(self.util.source_from_cache(path), expect)[](#l1.323)
def test_source_from_cache_no_cache_tag(self): # If sys.implementation.cache_tag is None, raise NotImplementedError. path = os.path.join('blah', 'pycache', 'whatever.pyc') with support.swap_attr(sys.implementation, 'cache_tag', None): with self.assertRaises(NotImplementedError):
util.source_from_cache(path)[](#l1.330)
self.util.source_from_cache(path)[](#l1.331)
def test_source_from_cache_bad_path(self): # When the path to a pyc file is not in PEP 3147 format, a ValueError # is raised. self.assertRaises(
ValueError, util.source_from_cache, '/foo/bar/bazqux.pyc')[](#l1.337)
ValueError, self.util.source_from_cache, '/foo/bar/bazqux.pyc')[](#l1.338)
def test_source_from_cache_no_slash(self): # No slashes at all in path -> ValueError self.assertRaises(
ValueError, util.source_from_cache, 'foo.cpython-32.pyc')[](#l1.343)
ValueError, self.util.source_from_cache, 'foo.cpython-32.pyc')[](#l1.344)
def test_source_from_cache_too_few_dots(self): # Too few dots in final path component -> ValueError self.assertRaises(
ValueError, util.source_from_cache, '__pycache__/foo.pyc')[](#l1.349)
ValueError, self.util.source_from_cache, '__pycache__/foo.pyc')[](#l1.350)
def test_source_from_cache_too_many_dots(self): # Too many dots in final path component -> ValueError self.assertRaises(
ValueError, util.source_from_cache,[](#l1.355)
ValueError, self.util.source_from_cache,[](#l1.356) '__pycache__/foo.cpython-32.foo.pyc')[](#l1.357)
def test_source_from_cache_no__pycache__(self): # Another problem with the path -> ValueError self.assertRaises(
ValueError, util.source_from_cache,[](#l1.362)
ValueError, self.util.source_from_cache,[](#l1.363) '/foo/bar/foo.cpython-32.foo.pyc')[](#l1.364)
+Frozen_PEP3147Tests, Source_PEP3147Tests = test_util.test_both(
PEP3147Tests,[](#l1.367)
util=[frozen_util, source_util])[](#l1.368)
+ if name == 'main': unittest.main()
--- a/Lib/test/test_importlib/util.py +++ b/Lib/test/test_importlib/util.py @@ -15,6 +15,17 @@ def import_importlib(module_name): return frozen, source +def test_both(test_class, **kwargs):
- frozen_tests = types.new_class('Frozen_'+test_class.name,
(test_class, unittest.TestCase))[](#l2.9)
- source_tests = types.new_class('Source_'+test_class.name,
(test_class, unittest.TestCase))[](#l2.11)
- for attr, (frozen_value, source_value) in kwargs.items():
setattr(frozen_tests, attr, frozen_value)[](#l2.13)
setattr(source_tests, attr, source_value)[](#l2.14)
- return frozen_tests, source_tests
+ + CASE_INSENSITIVE_FS = True