cpython: 543c76769c14 (original) (raw)

Mercurial > cpython

changeset 87794:543c76769c14

Issue #19712: Update test.test_importlib.import_ to test/use PEP 451 where appropriate. [#19712]

Brett Cannon brett@python.org
date Fri, 06 Dec 2013 12:07:25 -0500
parents 0c508d87f80b
children 07ef52e751f3
files Lib/test/test_importlib/import_/test___package__.py Lib/test/test_importlib/import_/test_caching.py Lib/test/test_importlib/import_/test_fromlist.py Lib/test/test_importlib/import_/test_meta_path.py Lib/test/test_importlib/import_/test_packages.py Lib/test/test_importlib/import_/test_path.py Lib/test/test_importlib/import_/test_relative_imports.py Lib/test/test_importlib/util.py
diffstat 8 files changed, 114 insertions(+), 61 deletions(-)[+] [-] Lib/test/test_importlib/import_/test___package__.py 31 Lib/test/test_importlib/import_/test_caching.py 18 Lib/test/test_importlib/import_/test_fromlist.py 2 Lib/test/test_importlib/import_/test_meta_path.py 50 Lib/test/test_importlib/import_/test_packages.py 14 Lib/test/test_importlib/import_/test_path.py 10 Lib/test/test_importlib/import_/test_relative_imports.py 2 Lib/test/test_importlib/util.py 48

line wrap: on

line diff

--- a/Lib/test/test_importlib/import_/test___package__.py +++ b/Lib/test/test_importlib/import_/test___package__.py @@ -36,7 +36,7 @@ class Using__package__: def test_using___package__(self): # [package]

@@ -49,7 +49,7 @@ class Using__package__: globals_ = {'name': 'pkg.fake', 'path': []} if package_as_None: globals_['package'] = None

@@ -70,11 +70,20 @@ class Using__package__: with self.assertRaises(TypeError): self.import('', globals, {}, ['relimport'], 1) -Frozen_UsingPackage, Source_UsingPackage = util.test_both(

+class Using__package__PEP302(Using__package__):

+ +Frozen_UsingPackagePEP302, Source_UsingPackagePEP302 = util.test_both(

+ +class Using__package__PEP302(Using__package__):

+ +Frozen_UsingPackagePEP451, Source_UsingPackagePEP451 = util.test_both(

-class Setting__package__(unittest.TestCase): +class Setting__package__: """Because package is a new feature, it is not always set by a loader. Import will set it as needed to help with the transition to relying on @@ -90,7 +99,7 @@ class Setting__package__(unittest.TestCa # [top-level] def test_top_level(self):

@@ -98,7 +107,7 @@ class Setting__package__(unittest.TestCa # [package] def test_package(self):

@@ -106,13 +115,19 @@ class Setting__package__(unittest.TestCa # [submodule] def test_submodule(self):

+class Setting__package__PEP302(Setting__package__, unittest.TestCase):

+ +class Setting__package__PEP451(Setting__package__, unittest.TestCase):

+ if name == 'main': unittest.main()

--- a/Lib/test/test_importlib/import_/test_caching.py +++ b/Lib/test/test_importlib/import_/test_caching.py @@ -39,6 +39,16 @@ class UseCache: self.import(name) self.assertEqual(cm.exception.name, name) +Frozen_UseCache, Source_UseCache = util.test_both(

+ + +class ImportlibUseCache(UseCache, unittest.TestCase): +

+

+ def create_mock(self, *names, return_=None): mock = util.mock_modules(*names) original_load = mock.load_module @@ -48,14 +58,6 @@ class UseCache: mock.load_module = MethodType(load_module, mock) return mock -Frozen_UseCache, Source_UseCache = util.test_both(

- - -class ImportlibUseCache(UseCache, unittest.TestCase): -

- # import inconsistent between loaders and built-in import when it comes # to when to use the module in sys.modules and when not to. def test_using_cache_after_loader(self):

--- a/Lib/test/test_importlib/import_/test_fromlist.py +++ b/Lib/test/test_importlib/import_/test_fromlist.py @@ -17,7 +17,7 @@ class ReturnValue: def test_return_from_import(self): # [import return]

--- a/Lib/test/test_importlib/import_/test_meta_path.py +++ b/Lib/test/test_importlib/import_/test_meta_path.py @@ -18,23 +18,18 @@ class CallingOrder: def test_first_called(self): # [first called] mod = 'top_level'

def test_continuing(self): # [continuing] mod_name = 'for_real'

def test_empty(self): # Raise an ImportWarning if sys.meta_path is empty. @@ -61,29 +56,27 @@ class CallSignature: [no path]. Otherwise, the value for path is passed in for the 'path' argument [path set]."""

- def test_no_path(self): # [no path] mod_name = 'top_level' assert '.' not in mod_name

@@ -93,10 +86,10 @@ class CallSignature: mod_name = pkg_name + '.module' path = [42] assert '.' in mod_name

@@ -107,8 +100,19 @@ class CallSignature: self.assertEqual(args[0], mod_name) self.assertIs(args[1], path) -Frozen_CallSignature, Source_CallSignature = util.test_both(

+class CallSignaturePEP302(CallSignature):

+ +Frozen_CallSignaturePEP302, Source_CallSignaturePEP302 = util.test_both(

+ +class CallSignaturePEP451(CallSignature):

+ +Frozen_CallSignaturePEP451, Source_CallSignaturePEP451 = util.test_both(

if name == 'main':

--- a/Lib/test/test_importlib/import_/test_packages.py +++ b/Lib/test/test_importlib/import_/test_packages.py @@ -11,13 +11,13 @@ class ParentModuleTests: """Importing a submodule should import the parent modules.""" def test_import_parent(self):

def test_bad_parent(self):

@@ -27,7 +27,7 @@ class ParentModuleTests: def init(): import pkg.module 1/0

@@ -44,7 +44,7 @@ class ParentModuleTests: def init(): from . import module 1/0

@@ -63,7 +63,7 @@ class ParentModuleTests: def init(): from ..subpkg import module 1/0

@@ -93,9 +93,9 @@ class ParentModuleTests: subname = name + '.b' def module_injection(): sys.modules[subname] = 'total bunk'

--- a/Lib/test/test_importlib/import_/test_path.py +++ b/Lib/test/test_importlib/import_/test_path.py @@ -27,7 +27,7 @@ class FinderTests: # Implicitly tests that sys.path_importer_cache is used. module = '' path = ''

@@ -38,7 +38,7 @@ class FinderTests: # Implicitly tests that sys.path_importer_cache is used. module = '' path = ''

@@ -47,7 +47,7 @@ class FinderTests: # An empty list should not count as asking for sys.path. module = 'module' path = ''

@@ -57,7 +57,7 @@ class FinderTests: # Test that sys.path_importer_cache is set. module = '' path = ''

@@ -82,7 +82,7 @@ class FinderTests: # The empty string should create a finder using the cwd. path = '' module = ''

--- a/Lib/test/test_importlib/import_/test_relative_imports.py +++ b/Lib/test/test_importlib/import_/test_relative_imports.py @@ -64,7 +64,7 @@ class RelativeImports: uncache_names.append(name) else: uncache_names.append(name[:-len('.init')])

--- a/Lib/test/test_importlib/util.py +++ b/Lib/test/test_importlib/util.py @@ -1,4 +1,5 @@ from contextlib import contextmanager +from importlib import util import os.path from test import support import unittest @@ -101,9 +102,9 @@ def import_state(**kwargs): setattr(sys, attr, value) -class mock_modules: +class _ImporterMock:

def init(self, *names, module_code={}): self.modules = {} @@ -133,6 +134,19 @@ class mock_modules: def getitem(self, name): return self.modules[name]

+

+ + +class mock_modules(_ImporterMock): +

+ def find_module(self, fullname, path=None): if fullname not in self.modules: return None @@ -152,10 +166,28 @@ class mock_modules: raise return self.modules[fullname]

+class mock_spec(_ImporterMock): +

+

+