Change repr() of a new-style class to say <class 'ClassName'> rather

than <type 'ClassName'>.  Exception: if it's a built-in type or an
extension type, continue to call it <type 'ClassName>.  Call me a
wimp, but I don't want to break more user code than necessary.
This commit is contained in:
Guido van Rossum 2001-09-25 03:56:29 +00:00
parent 5c294fb0e6
commit a4cb78874c
3 changed files with 15 additions and 8 deletions

View File

@ -37,16 +37,16 @@ test_1 = """
Here's the new type at work: Here's the new type at work:
>>> print defaultdict # show our type >>> print defaultdict # show our type
<type 'test.test_descrtut.defaultdict'> <class 'test.test_descrtut.defaultdict'>
>>> print type(defaultdict) # its metatype >>> print type(defaultdict) # its metatype
<type 'type'> <type 'type'>
>>> a = defaultdict(default=0.0) # create an instance >>> a = defaultdict(default=0.0) # create an instance
>>> print a # show the instance >>> print a # show the instance
{} {}
>>> print type(a) # show its type >>> print type(a) # show its type
<type 'test.test_descrtut.defaultdict'> <class 'test.test_descrtut.defaultdict'>
>>> print a.__class__ # show its class >>> print a.__class__ # show its class
<type 'test.test_descrtut.defaultdict'> <class 'test.test_descrtut.defaultdict'>
>>> print type(a) is a.__class__ # its type is its class >>> print type(a) is a.__class__ # its type is its class
1 1
>>> a[1] = 3.25 # modify the instance >>> a[1] = 3.25 # modify the instance

View File

@ -208,7 +208,7 @@ class foo(object):
''') ''')
from areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation import foo from areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation import foo
eq(repr(foo.foo), eq(repr(foo.foo),
"<type 'areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation.foo.foo'>") "<class 'areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation.foo.foo'>")
def test_object(self): def test_object(self):
# XXX Test the repr of a type with a really long tp_name but with no # XXX Test the repr of a type with a really long tp_name but with no

View File

@ -114,6 +114,7 @@ static PyObject *
type_repr(PyTypeObject *type) type_repr(PyTypeObject *type)
{ {
PyObject *mod, *name, *rtn; PyObject *mod, *name, *rtn;
char *kind;
mod = type_module(type, NULL); mod = type_module(type, NULL);
if (mod == NULL) if (mod == NULL)
@ -126,13 +127,19 @@ type_repr(PyTypeObject *type)
if (name == NULL) if (name == NULL)
return NULL; return NULL;
if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
kind = "class";
else
kind = "type";
if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) { if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) {
rtn = PyString_FromFormat("<type '%s.%s'>", rtn = PyString_FromFormat("<%s '%s.%s'>",
kind,
PyString_AS_STRING(mod), PyString_AS_STRING(mod),
PyString_AS_STRING(name)); PyString_AS_STRING(name));
} }
else else
rtn = PyString_FromFormat("<type '%s'>", type->tp_name); rtn = PyString_FromFormat("<%s '%s'>", kind, type->tp_name);
Py_XDECREF(mod); Py_XDECREF(mod);
Py_DECREF(name); Py_DECREF(name);
@ -3365,12 +3372,12 @@ super_repr(PyObject *self)
if (su->obj) if (su->obj)
return PyString_FromFormat( return PyString_FromFormat(
"<super: <type '%s'>, <%s object>>", "<super: <class '%s'>, <%s object>>",
su->type ? su->type->tp_name : "NULL", su->type ? su->type->tp_name : "NULL",
su->obj->ob_type->tp_name); su->obj->ob_type->tp_name);
else else
return PyString_FromFormat( return PyString_FromFormat(
"<super: <type '%s'>, NULL>", "<super: <class '%s'>, NULL>",
su->type ? su->type->tp_name : "NULL"); su->type ? su->type->tp_name : "NULL");
} }