]> granicus.if.org Git - python/commitdiff
Change the default repr() and str() of class instance objects to look
authorGuido van Rossum <guido@python.org>
Wed, 3 Dec 1997 00:06:02 +0000 (00:06 +0000)
committerGuido van Rossum <guido@python.org>
Wed, 3 Dec 1997 00:06:02 +0000 (00:06 +0000)
like <modulename.classname instance at ...> (to match the repr() of
class objects.

Objects/classobject.c

index b67265b1c353723fa997e82b4a520c54d06e3e4b..b6052a07583b6de55d7c0f2002cb4c241d974eda 100644 (file)
@@ -655,13 +655,21 @@ instance_repr(inst)
        if (func == NULL) {
                char buf[140];
                PyObject *classname = inst->in_class->cl_name;
+               PyObject *mod = PyDict_GetItemString(
+                       inst->in_class->cl_dict, "__module__");
                char *cname;
                if (classname != NULL && PyString_Check(classname))
                        cname = PyString_AsString(classname);
                else
                        cname = "?";
                PyErr_Clear();
-               sprintf(buf, "<%.100s instance at %lx>", cname, (long)inst);
+               if (mod == NULL || !PyString_Check(mod))
+                       sprintf(buf, "<?.%.100s instance at %lx>",
+                               cname, (long)inst);
+               else
+                       sprintf(buf, "<%.50s.%.50s instance at %lx>",
+                               PyString_AsString(mod),
+                               cname, (long)inst);
                return PyString_FromString(buf);
        }
        res = PyEval_CallObject(func, (PyObject *)NULL);