]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
correctly lookup __dir__
authorBenjamin Peterson <benjamin@python.org>
Mon, 23 May 2011 21:11:05 +0000 (16:11 -0500)
committerBenjamin Peterson <benjamin@python.org>
Mon, 23 May 2011 21:11:05 +0000 (16:11 -0500)
Lib/test/test_descr.py
Misc/NEWS
Objects/object.c

index aafe428bd0c9afe0ab9b2a2381f6e92bbcf18c3a..c74e2327769f7f5c1b62d0374ef5ea2798fbde77 100644 (file)
@@ -1595,6 +1595,7 @@ order (MRO) for bases """
             # probably not worth it.
             # ("__enter__", run_context, iden),
             # ("__exit__", run_context, iden),
+            ("__dir__", dir, empty_seq, set(), {}),
             ]
 
         class Checker(object):
index 9ae7979776ca7fa850cca59b90c46283e15a4b99..37bbbc19b31bb40139d35f5f4bad706752d441ec 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 3.1.4?
 Core and Builtins
 -----------------
 
+- Correct lookup of __dir__ on objects. Among other things, this causes errors
+  besides AttributeError found on lookup to be propagated.
+
 - Issue #12060: Use sig_atomic_t type and volatile keyword in the signal
   module. Patch written by Charles-François Natali.
 
index d5342738f48d50a82023d9c6bf5d9ed53627c6c1..ac57cd7e93c36d72cfcbfe87c5935ca0eda8f2fb 100644 (file)
@@ -1348,14 +1348,15 @@ error:
 static PyObject *
 _dir_object(PyObject *obj)
 {
-    PyObject * result = NULL;
-    PyObject * dirfunc = PyObject_GetAttrString((PyObject*)obj->ob_type,
-                                                "__dir__");
+    PyObject *result = NULL;
+    static PyObject *dir_str = NULL;
+    PyObject *dirfunc = _PyObject_LookupSpecial(obj, "__dir__", &dir_str);
 
     assert(obj);
     if (dirfunc == NULL) {
+        if (PyErr_Occurred())
+            return NULL;
         /* use default implementation */
-        PyErr_Clear();
         if (PyModule_Check(obj))
             result = _specialized_dir_module(obj);
         else if (PyType_Check(obj))
@@ -1365,7 +1366,7 @@ _dir_object(PyObject *obj)
     }
     else {
         /* use __dir__ */
-        result = PyObject_CallFunctionObjArgs(dirfunc, obj, NULL);
+        result = PyObject_CallFunctionObjArgs(dirfunc, NULL);
         Py_DECREF(dirfunc);
         if (result == NULL)
             return NULL;