Change the default repr() and str() of class instance objects to look

like <modulename.classname instance at ...> (to match the repr() of
class objects.
This commit is contained in:
Guido van Rossum 1997-12-03 00:06:02 +00:00
parent 5c38bf6c62
commit b7f1afe4a8
1 changed files with 9 additions and 1 deletions

View 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);