Issue8297: module attribute lookup failures now include module name i… · pythoncapi/cpython@7b9ff0e · GitHub
Skip to content

Commit 7b9ff0e

Browse files
committed
Issue8297: module attribute lookup failures now include module name in error message.
1 parent 7101cb0 commit 7b9ff0e

5 files changed

Lines changed: 52 additions & 9 deletions

File tree

Lib/test/test_doctest.py

Lines changed: 2 additions & 2 deletions

Lib/test/test_module.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,22 @@ def test_uninitialized(self):
3030
pass
3131
self.assertEqual(foo.__doc__, ModuleType.__doc__)
3232

33+
def test_unintialized_missing_getattr(self):
34+
# Issue 8297
35+
# test the text in the AttributeError of an uninitialized module
36+
foo = ModuleType.__new__(ModuleType)
37+
self.assertRaisesRegex(
38+
AttributeError, "module has no attribute 'not_here'",
39+
getattr, foo, "not_here")
40+
41+
def test_missing_getattr(self):
42+
# Issue 8297
43+
# test the text in the AttributeError
44+
foo = ModuleType("foo")
45+
self.assertRaisesRegex(
46+
AttributeError, "module 'foo' has no attribute 'not_here'",
47+
getattr, foo, "not_here")
48+
3349
def test_no_docstring(self):
3450
# Regularly initialized module, no docstring
3551
foo = ModuleType("foo")

Lib/unittest/test/test_loader.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ def test_loadTestsFromName__unknown_attr_name(self):
255255
try:
256256
loader.loadTestsFromName('unittest.sdasfasfasdf')
257257
except AttributeError as e:
258-
self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
258+
self.assertEqual(str(e), "module 'unittest' has no attribute 'sdasfasfasdf'")
259259
else:
260260
self.fail("TestLoader.loadTestsFromName failed to raise AttributeError")
261261

@@ -272,7 +272,7 @@ def test_loadTestsFromName__relative_unknown_name(self):
272272
try:
273273
loader.loadTestsFromName('sdasfasfasdf', unittest)
274274
except AttributeError as e:
275-
self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
275+
self.assertEqual(str(e), "module 'unittest' has no attribute 'sdasfasfasdf'")
276276
else:
277277
self.fail("TestLoader.loadTestsFromName failed to raise AttributeError")
278278

@@ -635,7 +635,7 @@ def test_loadTestsFromNames__unknown_attr_name(self):
635635
try:
636636
loader.loadTestsFromNames(['unittest.sdasfasfasdf', 'unittest'])
637637
except AttributeError as e:
638-
self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
638+
self.assertEqual(str(e), "module 'unittest' has no attribute 'sdasfasfasdf'")
639639
else:
640640
self.fail("TestLoader.loadTestsFromNames failed to raise AttributeError")
641641

@@ -654,7 +654,7 @@ def test_loadTestsFromNames__unknown_name_relative_1(self):
654654
try:
655655
loader.loadTestsFromNames(['sdasfasfasdf'], unittest)
656656
except AttributeError as e:
657-
self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
657+
self.assertEqual(str(e), "module 'unittest' has no attribute 'sdasfasfasdf'")
658658
else:
659659
self.fail("TestLoader.loadTestsFromName failed to raise AttributeError")
660660

@@ -673,7 +673,7 @@ def test_loadTestsFromNames__unknown_name_relative_2(self):
673673
try:
674674
loader.loadTestsFromNames(['TestCase', 'sdasfasfasdf'], unittest)
675675
except AttributeError as e:
676-
self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'")
676+
self.assertEqual(str(e), "module 'unittest' has no attribute 'sdasfasfasdf'")
677677
else:
678678
self.fail("TestLoader.loadTestsFromName failed to raise AttributeError")
679679

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ Core and Builtins
3636
- Issue #20637: Key-sharing now also works for instance dictionaries of
3737
subclasses. Patch by Peter Ingebretson.
3838

39+
- Issue #8297: Attributes missing from modules now include the module name
40+
in the error text. Original patch by ysj.ray.
41+
3942
- Issue #19995: %c, %o, %x, and %X now raise TypeError on non-integer input.
4043

4144
- Issue #12546: Allow \x00 to be used as a fill character when using str, int,

Objects/moduleobject.c

Lines changed: 26 additions & 2 deletions

0 commit comments

Comments
 (0)