| 1 |
from test.test_importlib import util |
| 2 |
|
| 3 |
from importlib import machinery |
| 4 |
import sys |
| 5 |
import types |
| 6 |
import unittest |
| 7 |
import warnings |
| 8 |
|
| 9 |
PKG_NAME = 'fine' |
| 10 |
SUBMOD_NAME = 'fine.bogus' |
| 11 |
|
| 12 |
|
| 13 |
class BadSpecFinderLoader: |
| 14 |
@classmethod |
| 15 |
def find_spec(cls, fullname, path=None, target=None): |
| 16 |
if fullname == SUBMOD_NAME: |
| 17 |
spec = machinery.ModuleSpec(fullname, cls) |
| 18 |
return spec |
| 19 |
|
| 20 |
@staticmethod |
| 21 |
def create_module(spec): |
| 22 |
return None |
| 23 |
|
| 24 |
@staticmethod |
| 25 |
def exec_module(module): |
| 26 |
if module.__name__ == SUBMOD_NAME: |
| 27 |
raise ImportError('I cannot be loaded!') |
| 28 |
|
| 29 |
|
| 30 |
class BadLoaderFinder: |
| 31 |
@classmethod |
| 32 |
def load_module(cls, fullname): |
| 33 |
if fullname == SUBMOD_NAME: |
| 34 |
raise ImportError('I cannot be loaded!') |
| 35 |
|
| 36 |
|
| 37 |
class APITest: |
| 38 |
|
| 39 |
"""Test API-specific details for __import__ (e.g. raising the right |
| 40 |
exception when passing in an int for the module name).""" |
| 41 |
|
| 42 |
def test_raises_ModuleNotFoundError(self): |
| 43 |
with self.assertRaises(ModuleNotFoundError): |
| 44 |
util.import_importlib('some module that does not exist') |
| 45 |
|
| 46 |
def test_name_requires_rparition(self): |
| 47 |
# Raise TypeError if a non-string is passed in for the module name. |
| 48 |
with self.assertRaises(TypeError): |
| 49 |
self.__import__(42) |
| 50 |
|
| 51 |
def test_negative_level(self): |
| 52 |
# Raise ValueError when a negative level is specified. |
| 53 |
# PEP 328 did away with sys.module None entries and the ambiguity of |
| 54 |
# absolute/relative imports. |
| 55 |
with self.assertRaises(ValueError): |
| 56 |
self.__import__('os', globals(), level=-1) |
| 57 |
|
| 58 |
def test_nonexistent_fromlist_entry(self): |
| 59 |
# If something in fromlist doesn't exist, that's okay. |
| 60 |
# issue15715 |
| 61 |
mod = types.ModuleType(PKG_NAME) |
| 62 |
mod.__path__ = ['XXX'] |
| 63 |
with util.import_state(meta_path=[self.bad_finder_loader]): |
| 64 |
with util.uncache(PKG_NAME): |
| 65 |
sys.modules[PKG_NAME] = mod |
| 66 |
self.__import__(PKG_NAME, fromlist=['not here']) |
| 67 |
|
| 68 |
def test_fromlist_load_error_propagates(self): |
| 69 |
# If something in fromlist triggers an exception not related to not |
| 70 |
# existing, let that exception propagate. |
| 71 |
# issue15316 |
| 72 |
mod = types.ModuleType(PKG_NAME) |
| 73 |
mod.__path__ = ['XXX'] |
| 74 |
with util.import_state(meta_path=[self.bad_finder_loader]): |
| 75 |
with util.uncache(PKG_NAME): |
| 76 |
sys.modules[PKG_NAME] = mod |
| 77 |
with self.assertRaises(ImportError): |
| 78 |
self.__import__(PKG_NAME, |
| 79 |
fromlist=[SUBMOD_NAME.rpartition('.')[-1]]) |
| 80 |
|
| 81 |
def test_blocked_fromlist(self): |
| 82 |
# If fromlist entry is None, let a ModuleNotFoundError propagate. |
| 83 |
# issue31642 |
| 84 |
mod = types.ModuleType(PKG_NAME) |
| 85 |
mod.__path__ = [] |
| 86 |
with util.import_state(meta_path=[self.bad_finder_loader]): |
| 87 |
with util.uncache(PKG_NAME, SUBMOD_NAME): |
| 88 |
sys.modules[PKG_NAME] = mod |
| 89 |
sys.modules[SUBMOD_NAME] = None |
| 90 |
with self.assertRaises(ModuleNotFoundError) as cm: |
| 91 |
self.__import__(PKG_NAME, |
| 92 |
fromlist=[SUBMOD_NAME.rpartition('.')[-1]]) |
| 93 |
self.assertEqual(cm.exception.name, SUBMOD_NAME) |
| 94 |
|
| 95 |
|
| 96 |
class OldAPITests(APITest): |
| 97 |
bad_finder_loader = BadLoaderFinder |
| 98 |
|
| 99 |
def test_raises_ModuleNotFoundError(self): |
| 100 |
with warnings.catch_warnings(): |
| 101 |
warnings.simplefilter("ignore", ImportWarning) |
| 102 |
super().test_raises_ModuleNotFoundError() |
| 103 |
|
| 104 |
def test_name_requires_rparition(self): |
| 105 |
with warnings.catch_warnings(): |
| 106 |
warnings.simplefilter("ignore", ImportWarning) |
| 107 |
super().test_name_requires_rparition() |
| 108 |
|
| 109 |
def test_negative_level(self): |
| 110 |
with warnings.catch_warnings(): |
| 111 |
warnings.simplefilter("ignore", ImportWarning) |
| 112 |
super().test_negative_level() |
| 113 |
|
| 114 |
def test_nonexistent_fromlist_entry(self): |
| 115 |
with warnings.catch_warnings(): |
| 116 |
warnings.simplefilter("ignore", ImportWarning) |
| 117 |
super().test_nonexistent_fromlist_entry() |
| 118 |
|
| 119 |
def test_fromlist_load_error_propagates(self): |
| 120 |
with warnings.catch_warnings(): |
| 121 |
warnings.simplefilter("ignore", ImportWarning) |
| 122 |
super().test_fromlist_load_error_propagates |
| 123 |
|
| 124 |
def test_blocked_fromlist(self): |
| 125 |
with warnings.catch_warnings(): |
| 126 |
warnings.simplefilter("ignore", ImportWarning) |
| 127 |
super().test_blocked_fromlist() |
| 128 |
|
| 129 |
|
| 130 |
(Frozen_OldAPITests, |
| 131 |
Source_OldAPITests |
| 132 |
) = util.test_both(OldAPITests, __import__=util.__import__) |
| 133 |
|
| 134 |
|
| 135 |
class SpecAPITests(APITest): |
| 136 |
bad_finder_loader = BadSpecFinderLoader |
| 137 |
|
| 138 |
|
| 139 |
(Frozen_SpecAPITests, |
| 140 |
Source_SpecAPITests |
| 141 |
) = util.test_both(SpecAPITests, __import__=util.__import__) |
| 142 |
|
| 143 |
|
| 144 |
if __name__ == '__main__': |
| 145 |
unittest.main() |