Thanks to visit codestin.com
Credit goes to github.com

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
Original file line numberDiff line numberDiff line change
@@ -2171,7 +2171,7 @@ def test_DocTestSuite():
21712171
>>> test.test_doctest.sillySetup
21722172
Traceback (most recent call last):
21732173
...
2174-
AttributeError: 'module' object has no attribute 'sillySetup'
2174+
AttributeError: module 'test.test_doctest' has no attribute 'sillySetup'
21752175
21762176
The setUp and tearDown funtions are passed test objects. Here
21772177
we'll use the setUp function to supply the missing variable y:
@@ -2317,7 +2317,7 @@ def test_DocFileSuite():
23172317
>>> test.test_doctest.sillySetup
23182318
Traceback (most recent call last):
23192319
...
2320-
AttributeError: 'module' object has no attribute 'sillySetup'
2320+
AttributeError: module 'test.test_doctest' has no attribute 'sillySetup'
23212321
23222322
The setUp and tearDown funtions are passed test objects.
23232323
Here, we'll use a setUp function to set the favorite color in

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
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,31 @@ module_repr(PyModuleObject *m)
411411
return PyObject_CallMethod(interp->importlib, "_module_repr", "O", m);
412412
}
413413

414+
static PyObject*
415+
module_getattr(PyObject *m, PyObject *name)
416+
{
417+
PyModuleObject *module;
418+
PyObject *attr, *mod_name;
419+
attr = PyObject_GenericGetAttr(m, name);
420+
if (attr != NULL)
421+
return attr;
422+
PyErr_Clear();
423+
module = (PyModuleObject*)m;
424+
if (module->md_dict != NULL) {
425+
mod_name = PyDict_GetItemString(module->md_dict, "__name__");
426+
if (mod_name != NULL) {
427+
PyErr_Format(PyExc_AttributeError,
428+
"module '%U' has no attribute '%U'", mod_name, name);
429+
return NULL;
430+
}
431+
else if (PyErr_Occurred())
432+
PyErr_Clear();
433+
}
434+
PyErr_Format(PyExc_AttributeError,
435+
"module has no attribute '%U'", name);
436+
return NULL;
437+
}
438+
414439
static int
415440
module_traverse(PyModuleObject *m, visitproc visit, void *arg)
416441
{
@@ -464,7 +489,6 @@ static PyMethodDef module_methods[] = {
464489
{0}
465490
};
466491

467-
468492
PyDoc_STRVAR(module_doc,
469493
"module(name[, doc])\n\
470494
\n\
@@ -488,7 +512,7 @@ PyTypeObject PyModule_Type = {
488512
0, /* tp_hash */
489513
0, /* tp_call */
490514
0, /* tp_str */
491-
PyObject_GenericGetAttr, /* tp_getattro */
515+
module_getattr, /* tp_getattro */
492516
PyObject_GenericSetAttr, /* tp_setattro */
493517
0, /* tp_as_buffer */
494518
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |

0 commit comments

Comments
 (0)