summaryrefslogtreecommitdiffstats
path: root/Objects/classobject.c
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1997-10-20 23:26:11 (GMT)
committerGuido van Rossum <guido@python.org>1997-10-20 23:26:11 (GMT)
commit4a2a621907679f74d2c8f255902f0565e997f333 (patch)
treea7118158066ffb8df4ef5123737172af0ee1173b /Objects/classobject.c
parented1100f3b63c79cc3da075b5c23f3d4fe25270c5 (diff)
downloadcpython-4a2a621907679f74d2c8f255902f0565e997f333.zip
cpython-4a2a621907679f74d2c8f255902f0565e997f333.tar.gz
cpython-4a2a621907679f74d2c8f255902f0565e997f333.tar.bz2
Write a str() function for class objects that returns
"modulename.classname" instead of returning the same as repr().
Diffstat (limited to 'Objects/classobject.c')
-rw-r--r--Objects/classobject.c38
1 files changed, 36 insertions, 2 deletions
diff --git a/Objects/classobject.c b/Objects/classobject.c
index d10cbe6..1e8be01 100644
--- a/Objects/classobject.c
+++ b/Objects/classobject.c
@@ -261,16 +261,50 @@ static PyObject *
class_repr(op)
PyClassObject *op;
{
+ PyObject *mod = PyDict_GetItemString(op->cl_dict, "__module__");
char buf[140];
char *name;
if (op->cl_name == NULL || !PyString_Check(op->cl_name))
name = "?";
else
name = PyString_AsString(op->cl_name);
- sprintf(buf, "<class %.100s at %lx>", name, (long)op);
+ if (mod == NULL || !PyString_Check(mod))
+ sprintf(buf, "<class ?.%.100s at %lx>", name, (long)op);
+ else
+ sprintf(buf, "<class %.50s.%.50s at %lx>",
+ PyString_AsString(mod),
+ name, (long)op);
return PyString_FromString(buf);
}
+static PyObject *
+class_str(op)
+ PyClassObject *op;
+{
+ PyObject *mod = PyDict_GetItemString(op->cl_dict, "__module__");
+ PyObject *name = op->cl_name;
+ PyObject *res;
+ int m, n;
+
+ if (name == NULL || !PyString_Check(name))
+ return class_repr(op);
+ if (mod == NULL || !PyString_Check(mod)) {
+ Py_INCREF(name);
+ return name;
+ }
+ m = PyString_Size(mod);
+ n = PyString_Size(name);
+ res = PyString_FromStringAndSize((char *)NULL, m+1+n);
+ if (res != NULL) {
+ char *s = PyString_AsString(res);
+ memcpy(s, PyString_AsString(mod), m);
+ s += m;
+ *s++ = '.';
+ memcpy(s, PyString_AsString(name), n);
+ }
+ return res;
+}
+
PyTypeObject PyClass_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0,
@@ -288,7 +322,7 @@ PyTypeObject PyClass_Type = {
0, /*tp_as_mapping*/
0, /*tp_hash*/
0, /*tp_call*/
- 0, /*tp_str*/
+ (reprfunc)class_str, /*tp_str*/
(getattrofunc)class_getattr, /*tp_getattro*/
(setattrofunc)class_setattr, /*tp_setattro*/
};