summaryrefslogtreecommitdiffstats
path: root/Objects/moduleobject.c
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2011-05-24 16:09:06 (GMT)
committerBenjamin Peterson <benjamin@python.org>2011-05-24 16:09:06 (GMT)
commit82b00c1d307f7fb39f388760b9ccd519fe3ef63b (patch)
treef164c4074f1093713a372ef6066fca576f68c935 /Objects/moduleobject.c
parent9bcfacd4fcf64ee7f6e0d05b5d0894c10cd43fc0 (diff)
downloadcpython-82b00c1d307f7fb39f388760b9ccd519fe3ef63b.zip
cpython-82b00c1d307f7fb39f388760b9ccd519fe3ef63b.tar.gz
cpython-82b00c1d307f7fb39f388760b9ccd519fe3ef63b.tar.bz2
move specialized dir implementations into __dir__ methods (closes #12166)
Diffstat (limited to 'Objects/moduleobject.c')
-rw-r--r--Objects/moduleobject.c30
1 files changed, 29 insertions, 1 deletions
diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c
index 06f58d8..d922249 100644
--- a/Objects/moduleobject.c
+++ b/Objects/moduleobject.c
@@ -413,6 +413,34 @@ module_clear(PyModuleObject *m)
return 0;
}
+static PyObject *
+module_dir(PyObject *self, PyObject *args)
+{
+ PyObject *result = NULL;
+ PyObject *dict = PyObject_GetAttrString(self, "__dict__");
+
+ if (dict != NULL) {
+ if (PyDict_Check(dict))
+ result = PyDict_Keys(dict);
+ else {
+ const char *name = PyModule_GetName(self);
+ if (name)
+ PyErr_Format(PyExc_TypeError,
+ "%.200s.__dict__ is not a dictionary",
+ name);
+ }
+ }
+
+ Py_XDECREF(dict);
+ return result;
+}
+
+static PyMethodDef module_methods[] = {
+ {"__dir__", module_dir, METH_NOARGS,
+ PyDoc_STR("__dir__() -> specialized dir() implementation")},
+ {0}
+};
+
PyDoc_STRVAR(module_doc,
"module(name[, doc])\n\
@@ -449,7 +477,7 @@ PyTypeObject PyModule_Type = {
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
- 0, /* tp_methods */
+ module_methods, /* tp_methods */
module_members, /* tp_members */
0, /* tp_getset */
0, /* tp_base */