cpython: dad74ff75a6b (original) (raw)
Mercurial > cpython
changeset 87640:dad74ff75a6b
Issue #19712: Port test.test_importlib.import_ tests to use PEP 451 that don't require changing test.test_importlib.util.mock_modules(). [#19712]
Brett Cannon brett@python.org | |
---|---|
date | Fri, 29 Nov 2013 16:17:05 -0500 |
parents | a1a936a3b2f6 |
children | 312137dd7ece |
files | Lib/test/test_importlib/import_/test___loader__.py Lib/test/test_importlib/import_/test_api.py Lib/test/test_importlib/import_/test_path.py |
diffstat | 3 files changed, 68 insertions(+), 16 deletions(-)[+] [-] Lib/test/test_importlib/import_/test___loader__.py 22 Lib/test/test_importlib/import_/test_api.py 60 Lib/test/test_importlib/import_/test_path.py 2 |
line wrap: on
line diff
--- a/Lib/test/test_importlib/import_/test___loader__.py +++ b/Lib/test/test_importlib/import_/test___loader__.py @@ -1,3 +1,4 @@ +from importlib import machinery import sys import types import unittest @@ -6,6 +7,27 @@ from .. import util from . import util as import_util +class SpecLoaderMock: +
- def find_spec(self, fullname, path=None, target=None):
return machinery.ModuleSpec(fullname, self)[](#l1.15)
+ + +class SpecLoaderAttributeTests: +
- def test___loader__(self):
loader = SpecLoaderMock()[](#l1.24)
with util.uncache('blah'), util.import_state(meta_path=[loader]):[](#l1.25)
module = self.__import__('blah')[](#l1.26)
self.assertEqual(loader, module.__loader__)[](#l1.27)
+ +Frozen_SpecTests, Source_SpecTests = util.test_both(
SpecLoaderAttributeTests, __import__=import_util.__import__)[](#l1.30)
+ + class LoaderMock: def find_module(self, fullname, path=None):
--- a/Lib/test/test_importlib/import_/test_api.py +++ b/Lib/test/test_importlib/import_/test_api.py @@ -1,19 +1,37 @@ from .. import util from . import util as import_util + +from importlib import machinery import sys import types import unittest +PKG_NAME = 'fine' +SUBMOD_NAME = 'fine.bogus' + + +class BadSpecFinderLoader:
- @classmethod
- def find_spec(cls, fullname, path=None, target=None):
if fullname == SUBMOD_NAME:[](#l2.19)
spec = machinery.ModuleSpec(fullname, cls)[](#l2.20)
return spec[](#l2.21)
- @staticmethod
- def exec_module(module):
if module.__name__ == SUBMOD_NAME:[](#l2.25)
raise ImportError('I cannot be loaded!')[](#l2.26)
- bad = 'fine.bogus' @classmethod def find_module(cls, fullname, path):
if fullname == cls.bad:[](#l2.33)
if fullname == SUBMOD_NAME:[](#l2.34) return cls[](#l2.35)
+ @classmethod def load_module(cls, fullname):
if fullname == cls.bad:[](#l2.39)
if fullname == SUBMOD_NAME:[](#l2.40) raise ImportError('I cannot be loaded!')[](#l2.41)
@@ -37,27 +55,39 @@ class APITest: def test_nonexistent_fromlist_entry(self): # If something in fromlist doesn't exist, that's okay. # issue15715
mod = types.ModuleType('fine')[](#l2.48)
mod = types.ModuleType(PKG_NAME)[](#l2.49) mod.__path__ = ['XXX'][](#l2.50)
with util.import_state(meta_path=[BadLoaderFinder]):[](#l2.51)
with util.uncache('fine'):[](#l2.52)
sys.modules['fine'] = mod[](#l2.53)
self.__import__('fine', fromlist=['not here'])[](#l2.54)
with util.import_state(meta_path=[self.bad_finder_loader]):[](#l2.55)
with util.uncache(PKG_NAME):[](#l2.56)
sys.modules[PKG_NAME] = mod[](#l2.57)
self.__import__(PKG_NAME, fromlist=['not here'])[](#l2.58)
def test_fromlist_load_error_propagates(self): # If something in fromlist triggers an exception not related to not # existing, let that exception propagate. # issue15316
mod = types.ModuleType('fine')[](#l2.64)
mod = types.ModuleType(PKG_NAME)[](#l2.65) mod.__path__ = ['XXX'][](#l2.66)
with util.import_state(meta_path=[BadLoaderFinder]):[](#l2.67)
with util.uncache('fine'):[](#l2.68)
sys.modules['fine'] = mod[](#l2.69)
with util.import_state(meta_path=[self.bad_finder_loader]):[](#l2.70)
with util.uncache(PKG_NAME):[](#l2.71)
sys.modules[PKG_NAME] = mod[](#l2.72) with self.assertRaises(ImportError):[](#l2.73)
self.__import__('fine', fromlist=['bogus'])[](#l2.74)
self.__import__(PKG_NAME,[](#l2.75)
fromlist=[SUBMOD_NAME.rpartition('.')[-1]])[](#l2.76)
+ + +class OldAPITests(APITest):
-Frozen_APITests, Source_APITests = util.test_both(
APITest, __import__=import_util.__import__)[](#l2.83)
+Frozen_OldAPITests, Source_OldAPITests = util.test_both(
OldAPITests, __import__=import_util.__import__)[](#l2.85)
+ + +class SpecAPITests(APITest):
+ +Frozen_SpecAPITests, Source_SpecAPITests = util.test_both(
SpecAPITests, __import__=import_util.__import__)[](#l2.92)
--- a/Lib/test/test_importlib/import_/test_path.py +++ b/Lib/test/test_importlib/import_/test_path.py @@ -17,7 +17,7 @@ class FinderTests: """Tests for PathFinder.""" def test_failure(self):
# Test None returned upon not finding a suitable finder.[](#l3.7)
# Test None returned upon not finding a suitable loader.[](#l3.8) module = '<test module>'[](#l3.9) with util.import_state():[](#l3.10) self.assertIsNone(self.machinery.PathFinder.find_module(module))[](#l3.11)