mirror of
https://github.com/python/cpython.git
synced 2025-08-04 17:08:35 +00:00
Write a str() function for class objects that returns
"modulename.classname" instead of returning the same as repr().
This commit is contained in:
parent
ed1100f3b6
commit
4a2a621907
1 changed files with 36 additions and 2 deletions
|
@ -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*/
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue