Commit 4f288ab7 authored by Guido van Rossum's avatar Guido van Rossum

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.
parent 189f1df3
......@@ -196,14 +196,6 @@ 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);
}
else {
PyObject *s;
if (flags & Py_PRINT_RAW)
s = PyObject_Str(op);
......@@ -212,12 +204,10 @@ PyObject_Print(PyObject *op, FILE *fp, int flags)
if (s == NULL)
ret = -1;
else {
ret = PyObject_Print(s, fp,
Py_PRINT_RAW);
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();
if (v->ob_type->tp_str == NULL)
return PyObject_Repr(v);
}
res = PyEval_CallObject(func, (PyObject *)NULL);
Py_DECREF(func);
}
res = (*v->ob_type->tp_str)(v);
if (res == NULL)
return NULL;
if (PyUnicode_Check(res)) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment