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

Skip to content

Commit 7963a35

Browse files
committed
correctly lookup __dir__
1 parent 2cca057 commit 7963a35

3 files changed

Lines changed: 10 additions & 5 deletions

File tree

Lib/test/test_descr.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,6 +1595,7 @@ def some_number(self_, key):
15951595
# probably not worth it.
15961596
# ("__enter__", run_context, iden),
15971597
# ("__exit__", run_context, iden),
1598+
("__dir__", dir, empty_seq, set(), {}),
15981599
]
15991600

16001601
class Checker(object):

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.1.4?
1010
Core and Builtins
1111
-----------------
1212

13+
- Correct lookup of __dir__ on objects. Among other things, this causes errors
14+
besides AttributeError found on lookup to be propagated.
15+
1316
- Issue #12060: Use sig_atomic_t type and volatile keyword in the signal
1417
module. Patch written by Charles-François Natali.
1518

Objects/object.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,14 +1348,15 @@ _generic_dir(PyObject *obj)
13481348
static PyObject *
13491349
_dir_object(PyObject *obj)
13501350
{
1351-
PyObject * result = NULL;
1352-
PyObject * dirfunc = PyObject_GetAttrString((PyObject*)obj->ob_type,
1353-
"__dir__");
1351+
PyObject *result = NULL;
1352+
static PyObject *dir_str = NULL;
1353+
PyObject *dirfunc = _PyObject_LookupSpecial(obj, "__dir__", &dir_str);
13541354

13551355
assert(obj);
13561356
if (dirfunc == NULL) {
1357+
if (PyErr_Occurred())
1358+
return NULL;
13571359
/* use default implementation */
1358-
PyErr_Clear();
13591360
if (PyModule_Check(obj))
13601361
result = _specialized_dir_module(obj);
13611362
else if (PyType_Check(obj))
@@ -1365,7 +1366,7 @@ _dir_object(PyObject *obj)
13651366
}
13661367
else {
13671368
/* use __dir__ */
1368-
result = PyObject_CallFunctionObjArgs(dirfunc, obj, NULL);
1369+
result = PyObject_CallFunctionObjArgs(dirfunc, NULL);
13691370
Py_DECREF(dirfunc);
13701371
if (result == NULL)
13711372
return NULL;

0 commit comments

Comments
 (0)