summaryrefslogtreecommitdiffstats
path: root/Objects/object.c
diff options
context:
space:
mode:
authorTim Peters <tim.peters@gmail.com>2001-09-17 02:38:46 (GMT)
committerTim Peters <tim.peters@gmail.com>2001-09-17 02:38:46 (GMT)
commit305b5857f6bef93e0fb21a821f7a5b995cd0889b (patch)
treea9bc5d4951946f2c7d4264755016c921b3daaf67 /Objects/object.c
parentbc7e863ce2aa9d3b9e5077865647f5db0ee050de (diff)
downloadcpython-305b5857f6bef93e0fb21a821f7a5b995cd0889b.zip
cpython-305b5857f6bef93e0fb21a821f7a5b995cd0889b.tar.gz
cpython-305b5857f6bef93e0fb21a821f7a5b995cd0889b.tar.bz2
PyObject_Dir(): Merge in __members__ and __methods__ too (if they exist,
and are lists, and then just the string elements (if any)). There are good and bad reasons for this. The good reason is to support dir() "like before" on objects of extension types that haven't migrated to the class introspection API yet. The bad reason is that Python's own method objects are such a type, and this is the quickest way to get their im_self etc attrs to "show up" via dir(). It looks much messier to move them to the new scheme, as their current getattr implementation presents a view of their attrs that's a untion of their own attrs plus their im_func's attrs. In particular, methodobject.__dict__ actually returns methodobject.im_func.__dict__, and if that's important to preserve it doesn't seem to fit the class introspection model at all.
Diffstat (limited to 'Objects/object.c')
-rw-r--r--Objects/object.c45
1 files changed, 45 insertions, 0 deletions
diff --git a/Objects/object.c b/Objects/object.c
index fd31e51..c56c3be 100644
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -1417,6 +1417,43 @@ merge_class_dict(PyObject* dict, PyObject* aclass)
return 0;
}
+/* Helper for PyObject_Dir.
+ If obj has an attr named attrname that's a list, merge its string
+ elements into keys of dict.
+ Return 0 on success, -1 on error. Errors due to not finding the attr,
+ or the attr not being a list, are suppressed.
+*/
+
+static int
+merge_list_attr(PyObject* dict, PyObject* obj, char *attrname)
+{
+ PyObject *list;
+ int result = 0;
+
+ assert(PyDict_Check(dict));
+ assert(obj);
+ assert(attrname);
+
+ list = PyObject_GetAttrString(obj, attrname);
+ if (list == NULL)
+ PyErr_Clear();
+
+ else if (PyList_Check(list)) {
+ int i;
+ for (i = 0; i < PyList_GET_SIZE(list); ++i) {
+ PyObject *item = PyList_GET_ITEM(list, i);
+ if (PyString_Check(item)) {
+ result = PyDict_SetItem(dict, item, Py_None);
+ if (result < 0)
+ break;
+ }
+ }
+ }
+
+ Py_XDECREF(list);
+ return result;
+}
+
/* Like __builtin__.dir(arg). See bltinmodule.c's builtin_dir for the
docstring, which should be kept in synch with this implementation. */
@@ -1484,6 +1521,14 @@ PyObject_Dir(PyObject *arg)
if (masterdict == NULL)
goto error;
+ /* Merge in __members__ and __methods__ (if any).
+ XXX Would like this to go away someday; for now, it's
+ XXX needed to get at im_self etc of method objects. */
+ if (merge_list_attr(masterdict, arg, "__members__") < 0)
+ goto error;
+ if (merge_list_attr(masterdict, arg, "__methods__") < 0)
+ goto error;
+
/* Merge in attrs reachable from its class.
CAUTION: Not all objects have a __class__ attr. */
itsclass = PyObject_GetAttrString(arg, "__class__");