[Python-checkins] CVS: python/dist/src/Objects object.c,2.125,2.126

Guido van Rossum gvanrossum@users.sourceforge.net
Tue, 01 May 2001 09:53:39 -0700


Update of /cvsroot/python/python/dist/src/Objects
In directory usw-pr-cvs1:/tmp/cvs-serv30308

Modified Files:
	object.c 
Log Message:
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.


Index: object.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/object.c,v
retrieving revision 2.125
retrieving revision 2.126
diff -C2 -r2.125 -r2.126
*** object.c	2001/04/27 21:35:01	2.125
--- object.c	2001/05/01 16:53:37	2.126
***************
*** 197,221 ****
  				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);
! 				else
! 					s = PyObject_Repr(op);
! 				if (s == NULL)
! 					ret = -1;
! 				else {
! 					ret = PyObject_Print(s, fp,
! 							     Py_PRINT_RAW);
! 				}
! 				Py_XDECREF(s);
  			}
  		}
  		else
--- 197,211 ----
  				op->ob_refcnt, op);
  		else if (op->ob_type->tp_print == NULL) {
! 			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);
  		}
  		else
***************
*** 302,321 ****
  	if (v == NULL)
  		return PyString_FromString("<NULL>");
! 	else 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 (res == NULL)
  		return NULL;
--- 292,303 ----
  	if (v == NULL)
  		return PyString_FromString("<NULL>");
! 	if (PyString_Check(v)) {
  		Py_INCREF(v);
  		return v;
  	}
+ 	if (v->ob_type->tp_str == NULL)
+ 		return PyObject_Repr(v);
+ 
+ 	res = (*v->ob_type->tp_str)(v);
  	if (res == NULL)
  		return NULL;