]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Net result of Guido's checkins of object.c (2.125 and 2.126), classobject.c
authorThomas Wouters <thomas@python.org>
Wed, 23 May 2001 13:03:13 +0000 (13:03 +0000)
committerThomas Wouters <thomas@python.org>
Wed, 23 May 2001 13:03:13 +0000 (13:03 +0000)
(2.128) and stringobject.c (2.105), which reworks PyObject_Str() and
PyObject_Repr() so strings and instances aren't special-cased, and
    print >> file, instance
works like expected in all cases.

Objects/classobject.c
Objects/object.c
Objects/stringobject.c

index fa71c4ea3a74fa187150a8f1146d00c8b674f060..6c09ce429f9091780d1de990642406419174738c 100644 (file)
@@ -781,6 +781,25 @@ instance_repr(PyInstanceObject *inst)
        return res;
 }
 
+static PyObject *
+instance_str(PyInstanceObject *inst)
+{
+       PyObject *func;
+       PyObject *res;
+       static PyObject *strstr;
+
+       if (strstr == NULL)
+               strstr = PyString_InternFromString("__str__");
+       func = instance_getattr(inst, strstr);
+       if (func == NULL) {
+               PyErr_Clear();
+               return instance_repr(inst);
+       }
+       res = PyEval_CallObject(func, (PyObject *)NULL);
+       Py_DECREF(func);
+       return res;
+}
+
 static long
 instance_hash(PyInstanceObject *inst)
 {
@@ -1766,7 +1785,7 @@ PyTypeObject PyInstance_Type = {
        &instance_as_mapping,                   /* tp_as_mapping */
        (hashfunc)instance_hash,                /* tp_hash */
        0,                                      /* tp_call */
-       0,                                      /* tp_str */
+       (reprfunc)instance_str,                 /* tp_str */
        (getattrofunc)instance_getattr,         /* tp_getattro */
        (setattrofunc)instance_setattr,         /* tp_setattro */
        0,                                      /* tp_as_buffer */
index ba6c88b2bcf00dc7cf1a1efcee94f8a1718c0a03..2c033f88f2f5ab3831004fdcbe0228846b8761ea 100644 (file)
@@ -188,24 +188,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 (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);
@@ -290,22 +283,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)) {
index a3cc7826feaafa8104be8dbc2a90dc1929842df1..7fccd1be96a81b86803216ebcd6e7563748e8f74 100644 (file)
@@ -401,6 +401,13 @@ string_repr(register PyStringObject *op)
        }
 }
 
+static PyObject *
+string_str(PyObject *s)
+{
+       Py_INCREF(s);
+       return s;
+}
+
 static int
 string_length(PyStringObject *a)
 {
@@ -2374,7 +2381,7 @@ PyTypeObject PyString_Type = {
        0,              /*tp_as_mapping*/
        (hashfunc)string_hash, /*tp_hash*/
        0,              /*tp_call*/
-       0,              /*tp_str*/
+       (reprfunc)string_str,   /*tp_str*/
        0,              /*tp_getattro*/
        0,              /*tp_setattro*/
        &string_as_buffer,      /*tp_as_buffer*/