mirror of
https://github.com/python/cpython.git
synced 2025-09-27 18:59:43 +00:00
Raise TypeError if the name given to importlib.__import__() lacks an rpartition
attribute. Was throwing AttributeError before. Discovered when running test_builtin against importlib. This exception change is specific to importlib.__import__() and does not apply to import_module() as it is being done for compatibility reasons only.
This commit is contained in:
parent
44b28a9f32
commit
6afbaef2fd
4 changed files with 28 additions and 1 deletions
|
@ -917,6 +917,8 @@ def __import__(name, globals={}, locals={}, fromlist=[], level=0):
|
||||||
import (e.g. ``from ..pkg import mod`` would have a 'level' of 2).
|
import (e.g. ``from ..pkg import mod`` would have a 'level' of 2).
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
if not hasattr(name, 'rpartition'):
|
||||||
|
raise TypeError("module name must be str, not {}".format(type(name)))
|
||||||
if level == 0:
|
if level == 0:
|
||||||
module = _gcd_import(name)
|
module = _gcd_import(name)
|
||||||
else:
|
else:
|
||||||
|
|
22
Lib/importlib/test/import_/test_api.py
Normal file
22
Lib/importlib/test/import_/test_api.py
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
from . import util
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
|
||||||
|
class APITest(unittest.TestCase):
|
||||||
|
|
||||||
|
"""Test API-specific details for __import__ (e.g. raising the right
|
||||||
|
exception when passing in an int for the module name)."""
|
||||||
|
|
||||||
|
def test_name_requires_rparition(self):
|
||||||
|
# Raise TypeError if a non-string is passed in for the module name.
|
||||||
|
with self.assertRaises(TypeError):
|
||||||
|
util.import_(42)
|
||||||
|
|
||||||
|
|
||||||
|
def test_main():
|
||||||
|
from test.support import run_unittest
|
||||||
|
run_unittest(APITest)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
test_main()
|
|
@ -6,7 +6,6 @@ Otherwise all command-line options valid for test.regrtest are also valid for
|
||||||
this script.
|
this script.
|
||||||
|
|
||||||
XXX FAILING
|
XXX FAILING
|
||||||
test_builtin # Wanting a TypeError for an integer name
|
|
||||||
test_import # execution bit, exception name differing, file name differing
|
test_import # execution bit, exception name differing, file name differing
|
||||||
between code and module (?)
|
between code and module (?)
|
||||||
test_importhooks # package not set in _gcd_import() but level > 0
|
test_importhooks # package not set in _gcd_import() but level > 0
|
||||||
|
|
|
@ -68,6 +68,10 @@ C-API
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Raise a TypeError when the name of a module to be imported for
|
||||||
|
importlib.__import__ is not a string (was raising an
|
||||||
|
AttributeError before).
|
||||||
|
|
||||||
- Allow the fromlist passed into importlib.__import__ to be any iterable.
|
- Allow the fromlist passed into importlib.__import__ to be any iterable.
|
||||||
|
|
||||||
- Have importlib raise ImportError if None is found in sys.modules.
|
- Have importlib raise ImportError if None is found in sys.modules.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue