[Python-checkins] cpython: do not override errors from descriptors on modules

benjamin.peterson python-checkins at python.org
Fri Apr 25 01:39:25 CEST 2014


http://hg.python.org/cpython/rev/01edd9116b02
changeset:   90454:01edd9116b02
user:        Benjamin Peterson <benjamin at python.org>
date:        Thu Apr 24 19:29:23 2014 -0400
summary:
  do not override errors from descriptors on modules

files:
  Lib/test/test_module.py |   8 ++++++++
  Objects/moduleobject.c  |  19 +++++++++----------
  2 files changed, 17 insertions(+), 10 deletions(-)


diff --git a/Lib/test/test_module.py b/Lib/test/test_module.py
--- a/Lib/test/test_module.py
+++ b/Lib/test/test_module.py
@@ -227,6 +227,14 @@
             b"len = len",
             b"shutil.rmtree = rmtree"})
 
+    def test_descriptor_errors_propogate(self):
+        class Descr:
+            def __get__(self, o, t):
+                raise RuntimeError
+        class M(ModuleType):
+            melon = Descr()
+        self.assertRaises(RuntimeError, getattr, M("mymod"), "melon")
+
     # frozen and namespace module reprs are tested in importlib.
 
 
diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c
--- a/Objects/moduleobject.c
+++ b/Objects/moduleobject.c
@@ -412,24 +412,23 @@
 }
 
 static PyObject*
-module_getattr(PyObject *m, PyObject *name)
+module_getattro(PyModuleObject *m, PyObject *name)
 {
-    PyModuleObject *module;
     PyObject *attr, *mod_name;
-    attr = PyObject_GenericGetAttr(m, name);
-    if (attr != NULL)
+    attr = PyObject_GenericGetAttr((PyObject *)m, name);
+    if (attr || !PyErr_ExceptionMatches(PyExc_AttributeError))
         return attr;
     PyErr_Clear();
-    module = (PyModuleObject*)m;
-    if (module->md_dict != NULL) {
-        mod_name = PyDict_GetItemString(module->md_dict, "__name__");
-        if (mod_name != NULL) {
+    if (m->md_dict) {
+        mod_name = PyDict_GetItemString(m->md_dict, "__name__");
+        if (mod_name) {
             PyErr_Format(PyExc_AttributeError,
                         "module '%U' has no attribute '%U'", mod_name, name);
             return NULL;
         }
-        else if (PyErr_Occurred())
+        else if (PyErr_Occurred()) {
             PyErr_Clear();
+        }
     }
     PyErr_Format(PyExc_AttributeError,
                 "module has no attribute '%U'", name);
@@ -512,7 +511,7 @@
     0,                                          /* tp_hash */
     0,                                          /* tp_call */
     0,                                          /* tp_str */
-    module_getattr,                             /* tp_getattro */
+    (getattrofunc)module_getattro,              /* tp_getattro */
     PyObject_GenericSetAttr,                    /* tp_setattro */
     0,                                          /* tp_as_buffer */
     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list