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')

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')

def test_universal_newlines(self): source = '\r\n'.join([self.source, self.source]) source_bytes = source.encode('utf-8')

+Frozen_DecodeSourceBytesTests, Source_DecodeSourceBytesTests = test_util.test_both(

-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.

@@ -51,7 +55,7 @@ class ModuleToLoadTests(unittest.TestCas def test_new_module_failed(self): # Test the module is removed from sys.modules. try:

@@ -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

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:

@@ -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

+Frozen_ModuleToLoadTests, Source_ModuleToLoadTests = test_util.test_both(

-class ModuleForLoaderTests(unittest.TestCase): + +class ModuleForLoaderTests: """Tests for importlib.util.module_for_loader."""

def test_warning(self): # Should raise a PendingDeprecationWarning when used. with warnings.catch_warnings(): warnings.simplefilter('error', PendingDeprecationWarning) with self.assertRaises(PendingDeprecationWarning):

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(

-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

@@ -266,12 +277,15 @@ class SetPackageTests(unittest.TestCase) def test_decorator_attrs(self): def fxn(module): pass

+Frozen_SetPackageTests, Source_SetPackageTests = test_util.test_both(

-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 ResolveNameTests(unittest.TestCase): +class Source_SetLoaderTests(SetLoaderTests, unittest.TestCase):

+ + +class ResolveNameTests: """Tests importlib.util.resolve_name().""" def test_absolute(self): # bacon

def test_aboslute_within_package(self): # bacon in spam

def test_no_package(self): # .bacon in '' with self.assertRaises(ValueError):

def test_in_package(self): # .bacon in spam self.assertEqual('spam.eggs.bacon',

def test_other_package(self): # ..bacon in spam.bacon self.assertEqual('spam.bacon',

def test_escape(self): # ..bacon in spam with self.assertRaises(ValueError):

+ +Frozen_ResolveNameTests, Source_ResolveNameTests = test_util.test_both(

-class MagicNumberTests(unittest.TestCase): +class MagicNumberTests: def test_length(self): # Should be 4 bytes.

def test_incorporates_rn(self): # The magic number uses \r\n to come out wrong when splitting on lines.

+ +Frozen_MagicNumberTests, Source_MagicNumberTests = test_util.test_both(

-class PEP3147Tests(unittest.TestCase):

+class PEP3147Tests:

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))

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):

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))

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))

def test_cache_from_source_cwd(self): path = 'foo.py' expect = os.path.join('pycache', 'foo.{}.pyc'.format(self.tag))

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))

@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(

@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')

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):

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(

def test_source_from_cache_no_slash(self): # No slashes at all in path -> ValueError self.assertRaises(

def test_source_from_cache_too_few_dots(self): # Too few dots in final path component -> ValueError self.assertRaises(

def test_source_from_cache_too_many_dots(self): # Too many dots in final path component -> ValueError self.assertRaises(

def test_source_from_cache_no__pycache__(self): # Another problem with the path -> ValueError self.assertRaises(

+Frozen_PEP3147Tests, Source_PEP3147Tests = test_util.test_both(

+ 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):

+ + CASE_INSENSITIVE_FS = True

Windows is the only OS that is always case-insensitive

(OS X can be case-sensitive).