diff -r e266525c9294 Lib/test/test_doctest.py --- a/Lib/test/test_doctest.py Fri Mar 21 06:38:46 2014 -0700 +++ b/Lib/test/test_doctest.py Wed Mar 26 22:14:03 2014 -0700 @@ -2161,21 +2161,21 @@ def test_DocTestSuite(): ... setUp=setUp, tearDown=tearDown) >>> suite.run(unittest.TestResult()) But the tearDown restores sanity: >>> import test.test_doctest >>> test.test_doctest.sillySetup Traceback (most recent call last): ... - AttributeError: 'module' object has no attribute 'sillySetup' + AttributeError: module 'test.test_doctest' has no attribute 'sillySetup' The setUp and tearDown funtions are passed test objects. Here we'll use the setUp function to supply the missing variable y: >>> def setUp(test): ... test.globs['y'] = 1 >>> suite = doctest.DocTestSuite('test.sample_doctest', setUp=setUp) >>> suite.run(unittest.TestResult()) @@ -2307,21 +2307,21 @@ def test_DocFileSuite(): ... setUp=setUp, tearDown=tearDown) >>> suite.run(unittest.TestResult()) But the tearDown restores sanity: >>> import test.test_doctest >>> test.test_doctest.sillySetup Traceback (most recent call last): ... - AttributeError: 'module' object has no attribute 'sillySetup' + AttributeError: module 'test.test_doctest' has no attribute 'sillySetup' The setUp and tearDown funtions are passed test objects. Here, we'll use a setUp function to set the favorite color in test_doctest.txt: >>> def setUp(test): ... test.globs['favorite_color'] = 'blue' >>> suite = doctest.DocFileSuite('test_doctest.txt', setUp=setUp) >>> suite.run(unittest.TestResult()) diff -r e266525c9294 Lib/test/test_module.py --- a/Lib/test/test_module.py Fri Mar 21 06:38:46 2014 -0700 +++ b/Lib/test/test_module.py Wed Mar 26 22:14:03 2014 -0700 @@ -23,20 +23,36 @@ class ModuleTests(unittest.TestCase): foo = ModuleType.__new__(ModuleType) self.assertTrue(foo.__dict__ is None) self.assertRaises(SystemError, dir, foo) try: s = foo.__name__ self.fail("__name__ = %s" % repr(s)) except AttributeError: pass self.assertEqual(foo.__doc__, ModuleType.__doc__) + def test_unintialized_missing_getattr(self): + # Issue 8297 + # test the text in the AttributeError of an uninitialized module + foo = ModuleType.__new__(ModuleType) + self.assertRaisesRegex( + AttributeError, "module has no attribute 'not_here'", + getattr, foo, "not_here") + + def test_missing_getattr(self): + # Issue 8297 + # test the text in the AttributeError + foo = ModuleType("foo") + self.assertRaisesRegex( + AttributeError, "module 'foo' has no attribute 'not_here'", + getattr, foo, "not_here") + def test_no_docstring(self): # Regularly initialized module, no docstring foo = ModuleType("foo") self.assertEqual(foo.__name__, "foo") self.assertEqual(foo.__doc__, None) self.assertIs(foo.__loader__, None) self.assertIs(foo.__package__, None) self.assertIs(foo.__spec__, None) self.assertEqual(foo.__dict__, {"__name__": "foo", "__doc__": None, "__loader__": None, "__package__": None, diff -r e266525c9294 Lib/unittest/test/test_loader.py --- a/Lib/unittest/test/test_loader.py Fri Mar 21 06:38:46 2014 -0700 +++ b/Lib/unittest/test/test_loader.py Wed Mar 26 22:14:03 2014 -0700 @@ -248,38 +248,38 @@ class Test_TestLoader(unittest.TestCase) # within a test case class, or a callable object which returns a # TestCase or TestSuite instance." # # What happens when the module is found, but the attribute can't? def test_loadTestsFromName__unknown_attr_name(self): loader = unittest.TestLoader() try: loader.loadTestsFromName('unittest.sdasfasfasdf') except AttributeError as e: - self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'") + self.assertEqual(str(e), "module 'unittest' has no attribute 'sdasfasfasdf'") else: self.fail("TestLoader.loadTestsFromName failed to raise AttributeError") # "The specifier name is a ``dotted name'' that may resolve either to # a module, a test case class, a TestSuite instance, a test method # within a test case class, or a callable object which returns a # TestCase or TestSuite instance." # # What happens when we provide the module, but the attribute can't be # found? def test_loadTestsFromName__relative_unknown_name(self): loader = unittest.TestLoader() try: loader.loadTestsFromName('sdasfasfasdf', unittest) except AttributeError as e: - self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'") + self.assertEqual(str(e), "module 'unittest' has no attribute 'sdasfasfasdf'") else: self.fail("TestLoader.loadTestsFromName failed to raise AttributeError") # "The specifier name is a ``dotted name'' that may resolve either to # a module, a test case class, a TestSuite instance, a test method # within a test case class, or a callable object which returns a # TestCase or TestSuite instance." # ... # "The method optionally resolves name relative to the given module" # @@ -628,59 +628,59 @@ class Test_TestLoader(unittest.TestCase) # within a test case class, or a callable object which returns a # TestCase or TestSuite instance." # # What happens when the module can be found, but not the attribute? def test_loadTestsFromNames__unknown_attr_name(self): loader = unittest.TestLoader() try: loader.loadTestsFromNames(['unittest.sdasfasfasdf', 'unittest']) except AttributeError as e: - self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'") + self.assertEqual(str(e), "module 'unittest' has no attribute 'sdasfasfasdf'") else: self.fail("TestLoader.loadTestsFromNames failed to raise AttributeError") # "The specifier name is a ``dotted name'' that may resolve either to # a module, a test case class, a TestSuite instance, a test method # within a test case class, or a callable object which returns a # TestCase or TestSuite instance." # ... # "The method optionally resolves name relative to the given module" # # What happens when given an unknown attribute on a specified `module` # argument? def test_loadTestsFromNames__unknown_name_relative_1(self): loader = unittest.TestLoader() try: loader.loadTestsFromNames(['sdasfasfasdf'], unittest) except AttributeError as e: - self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'") + self.assertEqual(str(e), "module 'unittest' has no attribute 'sdasfasfasdf'") else: self.fail("TestLoader.loadTestsFromName failed to raise AttributeError") # "The specifier name is a ``dotted name'' that may resolve either to # a module, a test case class, a TestSuite instance, a test method # within a test case class, or a callable object which returns a # TestCase or TestSuite instance." # ... # "The method optionally resolves name relative to the given module" # # Do unknown attributes (relative to a provided module) still raise an # exception even in the presence of valid attribute names? def test_loadTestsFromNames__unknown_name_relative_2(self): loader = unittest.TestLoader() try: loader.loadTestsFromNames(['TestCase', 'sdasfasfasdf'], unittest) except AttributeError as e: - self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'") + self.assertEqual(str(e), "module 'unittest' has no attribute 'sdasfasfasdf'") else: self.fail("TestLoader.loadTestsFromName failed to raise AttributeError") # "The specifier name is a ``dotted name'' that may resolve either to # a module, a test case class, a TestSuite instance, a test method # within a test case class, or a callable object which returns a # TestCase or TestSuite instance." # ... # "The method optionally resolves name relative to the given module" # diff -r e266525c9294 Misc/NEWS --- a/Misc/NEWS Fri Mar 21 06:38:46 2014 -0700 +++ b/Misc/NEWS Wed Mar 26 22:14:03 2014 -0700 @@ -13,20 +13,23 @@ Core and Builtins - Issue #20929: Add a type cast to avoid shifting a negative number. - Issue #20731: Properly position in source code files even if they are opened in text mode. Patch by Serhiy Storchaka. - Issue #20637: Key-sharing now also works for instance dictionaries of subclasses. Patch by Peter Ingebretson. - Issue #19995: %c, %o, %x, and %X now raise TypeError on non-integer input. +- Issue #8297: Attributes missing from modules now include the module name + in the error text. Original patch by ysj.ray. + Library ------- - Issue #13936: Remove the ability of datetime.time instances to be considered false in boolean contexts. - Issue 18931: selectors module now supports /dev/poll on Solaris. Patch by Giampaolo Rodola'. - Issue #19977: When the ``LC_TYPE`` locale is the POSIX locale (``C`` locale), diff -r e266525c9294 Objects/moduleobject.c --- a/Objects/moduleobject.c Fri Mar 21 06:38:46 2014 -0700 +++ b/Objects/moduleobject.c Wed Mar 26 22:14:03 2014 -0700 @@ -404,20 +404,45 @@ module_dealloc(PyModuleObject *m) static PyObject * module_repr(PyModuleObject *m) { PyThreadState *tstate = PyThreadState_GET(); PyInterpreterState *interp = tstate->interp; return PyObject_CallMethod(interp->importlib, "_module_repr", "O", m); } +static PyObject* +module_getattr(PyObject *m, PyObject *name) +{ + PyModuleObject *module; + PyObject *attr, *mod_name; + attr = PyObject_GenericGetAttr(m, name); + if (attr != NULL) + return attr; + PyErr_Clear(); + module = (PyModuleObject*)m; + if (module->md_dict != NULL) { + mod_name = PyDict_GetItemString(module->md_dict, "__name__"); + if (mod_name != NULL) { + PyErr_Format(PyExc_AttributeError, + "module '%U' has no attribute '%U'", mod_name, name); + return NULL; + } + else if (PyErr_Occurred()) + PyErr_Clear(); + } + PyErr_Format(PyExc_AttributeError, + "module has no attribute '%U'", name); + return NULL; +} + static int module_traverse(PyModuleObject *m, visitproc visit, void *arg) { if (m->md_def && m->md_def->m_traverse) { int res = m->md_def->m_traverse((PyObject*)m, visit, arg); if (res) return res; } Py_VISIT(m->md_dict); return 0; @@ -481,21 +506,21 @@ PyTypeObject PyModule_Type = { 0, /* tp_getattr */ 0, /* tp_setattr */ 0, /* tp_reserved */ (reprfunc)module_repr, /* tp_repr */ 0, /* tp_as_number */ 0, /* tp_as_sequence */ 0, /* tp_as_mapping */ 0, /* tp_hash */ 0, /* tp_call */ 0, /* tp_str */ - PyObject_GenericGetAttr, /* tp_getattro */ + module_getattr, /* tp_getattro */ PyObject_GenericSetAttr, /* tp_setattro */ 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, /* tp_flags */ module_doc, /* tp_doc */ (traverseproc)module_traverse, /* tp_traverse */ (inquiry)module_clear, /* tp_clear */ 0, /* tp_richcompare */ offsetof(PyModuleObject, md_weaklist), /* tp_weaklistoffset */ 0, /* tp_iter */