Skip to content

Commit 7456cfc

Browse files
committed
Add test for the from ... import behavior.
Make sure they name and path are set in various case.
1 parent ab2cb0e commit 7456cfc

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Lib/test/test_import/__init__.py

+19
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,25 @@ def test_from_import_missing_attr_raises_ImportError(self):
8080
with self.assertRaises(ImportError):
8181
from importlib import something_that_should_not_exist_anywhere
8282

83+
def test_from_import_missing_attr_has_name_and_path(self):
84+
with self.assertRaises(ImportError) as cm:
85+
from os import i_dont_exist
86+
self.assertEqual(cm.exception.name, 'os')
87+
self.assertEqual(cm.exception.path, os.__file__)
88+
89+
def test_from_import_missing_attr_has_name(self):
90+
with self.assertRaises(ImportError) as cm:
91+
# _warning has no path as it's a built-in module.
92+
from _warning import i_dont_exist
93+
self.assertEqual(cm.exception.name, '_warning')
94+
self.assertIsNone(cm.exception.path)
95+
96+
def test_from_import_missing_attr_path_is_canonical(self):
97+
with self.assertRaises(ImportError) as cm:
98+
from os.path import i_dont_exist
99+
self.assertIn(cm.exception.name, {'posixpath', 'ntpath'})
100+
self.assertIsNotNone(cm.exception)
101+
83102
def test_case_sensitivity(self):
84103
# Brief digression to test that import is case-sensitive: if we got
85104
# this far, we know for sure that "random" exists.

0 commit comments

Comments
 (0)