summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Wouters <thomas@python.org>2001-05-23 13:03:13 (GMT)
committerThomas Wouters <thomas@python.org>2001-05-23 13:03:13 (GMT)
commitc922bfa4cd5beffafb7b912b01da8460b5005116 (patch)
tree398a9e67f4093d145968eb8d1fea9797bbffa639
parenta29e4f3d7375c193978dc77e73853b6aca729564 (diff)
downloadcpython-c922bfa4cd5beffafb7b912b01da8460b5005116.zip
cpython-c922bfa4cd5beffafb7b912b01da8460b5005116.tar.gz
cpython-c922bfa4cd5beffafb7b912b01da8460b5005116.tar.bz2
Net result of Guido's checkins of object.c (2.125 and 2.126), classobject.c
(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.
-rw-r--r--Objects/classobject.c21
-rw-r--r--Objects/object.c43
-rw-r--r--Objects/stringobject.c9
3 files changed, 42 insertions, 31 deletions
diff --git a/Objects/classobject.c b/Objects/classobject.c
index fa71c4e..6c09ce4 100644
--- a/Objects/classobject.c
+++ b/Objects/classobject.c
@@ -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 */
diff --git a/Objects/object.c b/Objects/object.c
index ba6c88b..2c033f8 100644
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -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)) {
diff --git a/Objects/stringobject.c b/Objects/stringobject.c
index a3cc782..7fccd1b 100644
--- a/Objects/stringobject.c
+++ b/Objects/stringobject.c
@@ -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*/