diff options
author | Guido van Rossum <guido@python.org> | 2001-05-01 16:53:37 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2001-05-01 16:53:37 (GMT) |
commit | 4f288ab7d6b386e223a187523daf894e012f8cfd (patch) | |
tree | baf04dfbdeaab91767af7ddbc3e146cc1fdbac2c /Objects | |
parent | 189f1df3018885335bc9e719a96e891e3812ce1a (diff) | |
download | cpython-4f288ab7d6b386e223a187523daf894e012f8cfd.zip cpython-4f288ab7d6b386e223a187523daf894e012f8cfd.tar.gz cpython-4f288ab7d6b386e223a187523daf894e012f8cfd.tar.bz2 |
Printing objects to a real file still wasn't done right: if the
object's type didn't define tp_print, there were still cases where the
full "print uses str() which falls back to repr()" semantics weren't
honored. This resulted in
>>> print None
<None object at 0x80bd674>
>>> print type(u'')
<type object at 0x80c0a80>
Fixed this by always using the appropriate PyObject_Repr() or
PyObject_Str() call, rather than trying to emulate what they would do.
Also simplified PyObject_Str() to always fall back on PyObject_Repr()
when tp_str is not defined (rather than making an extra check for
instances with a __str__ method). And got rid of the special case for
strings.
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/object.c | 46 |
1 files changed, 14 insertions, 32 deletions
diff --git a/Objects/object.c b/Objects/object.c index 1ace8f5..04f75e9 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -196,27 +196,17 @@ PyObject_Print(PyObject *op, FILE *fp, int flags) fprintf(fp, "<refcnt %u at %p>", op->ob_refcnt, op); else if (op->ob_type->tp_print == NULL) { - if ((flags & Py_PRINT_RAW) - ? (op->ob_type->tp_str == NULL) - : (op->ob_type->tp_repr == NULL)) - { - fprintf(fp, "<%s object at %p>", - op->ob_type->tp_name, op); - } + PyObject *s; + if (flags & Py_PRINT_RAW) + s = PyObject_Str(op); + else + s = PyObject_Repr(op); + if (s == NULL) + ret = -1; else { - PyObject *s; - if (flags & Py_PRINT_RAW) - s = PyObject_Str(op); - else - s = PyObject_Repr(op); - if (s == NULL) - ret = -1; - else { - ret = PyObject_Print(s, fp, - Py_PRINT_RAW); - } - Py_XDECREF(s); + ret = PyObject_Print(s, fp, Py_PRINT_RAW); } + Py_XDECREF(s); } else ret = (*op->ob_type->tp_print)(op, fp, flags); @@ -301,22 +291,14 @@ PyObject_Str(PyObject *v) if (v == NULL) return PyString_FromString("<NULL>"); - else if (PyString_Check(v)) { + if (PyString_Check(v)) { Py_INCREF(v); return v; } - else if (v->ob_type->tp_str != NULL) - res = (*v->ob_type->tp_str)(v); - else { - PyObject *func; - if (!PyInstance_Check(v) || - (func = PyObject_GetAttrString(v, "__str__")) == NULL) { - PyErr_Clear(); - return PyObject_Repr(v); - } - res = PyEval_CallObject(func, (PyObject *)NULL); - Py_DECREF(func); - } + if (v->ob_type->tp_str == NULL) + return PyObject_Repr(v); + + res = (*v->ob_type->tp_str)(v); if (res == NULL) return NULL; if (PyUnicode_Check(res)) { |