Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3a80c4a

Browse files
committed
(Adding this to the trunk as well.)
Fix a very old flaw in PyObject_Print(). Amazing! When an object type defines tp_str but not tp_repr, 'print x' to a real file object would not call the tp_str slot but rather print a default style representation: <foo object at 0x....>. This even though 'print x' to a file-like-object would correctly call the tp_str slot.
1 parent e9bcb5c commit 3a80c4a

1 file changed

Lines changed: 4 additions & 1 deletion

File tree

Objects/object.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,10 @@ PyObject_Print(PyObject *op, FILE *fp, int flags)
196196
fprintf(fp, "<refcnt %u at %p>",
197197
op->ob_refcnt, op);
198198
else if (op->ob_type->tp_print == NULL) {
199-
if (op->ob_type->tp_repr == NULL) {
199+
if ((flags & Py_PRINT_RAW)
200+
? (op->ob_type->tp_str == NULL)
201+
: (op->ob_type->tp_repr == NULL))
202+
{
200203
fprintf(fp, "<%s object at %p>",
201204
op->ob_type->tp_name, op);
202205
}

0 commit comments

Comments
 (0)