]> granicus.if.org Git - python/commitdiff
Printing objects to a real file still wasn't done right: if the
authorGuido van Rossum <guido@python.org>
Tue, 1 May 2001 16:53:37 +0000 (16:53 +0000)
committerGuido van Rossum <guido@python.org>
Tue, 1 May 2001 16:53:37 +0000 (16:53 +0000)
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.

Objects/object.c

index 1ace8f5b26c7b477c963661e39d3b31d896241db..04f75e95c1070a2ebfa84473ecf9689e942c9023 100644 (file)
@@ -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)) {