[Python-checkins] CVS: python/dist/src/Objects object.c,2.156,2.157 stringobject.c,2.138,2.139

Tim Peters tim_one@users.sourceforge.net
Tue, 16 Oct 2001 13:18:26 -0700


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

Modified Files:
	object.c stringobject.c 
Log Message:
SF bug [#468061] __str__ ignored in str subclass.

object.c, PyObject_Str:  Don't try to optimize anything except exact
string objects here; in particular, let str subclasses go thru tp_str,
same as non-str objects.  This allows overrides of tp_str to take
effect.

stringobject.c:
+ string_print (str's tp_print):  If the argument isn't an exact string
  object, get one from PyObject_Str.

+ string_str (str's tp_str):  Make a genuine-string copy of the object if
  it's of a proper str subclass type.  str() applied to a str subclass
  that doesn't override __str__ ends up here.

test_descr.py:  New str_of_str_subclass() test.


Index: object.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/object.c,v
retrieving revision 2.156
retrieving revision 2.157
diff -C2 -d -r2.156 -r2.157
*** object.c	2001/10/07 03:54:51	2.156
--- object.c	2001/10/16 20:18:24	2.157
***************
*** 262,271 ****
  		return v;
  	}
- 	if (PyString_Check(v)) {
- 		/* For a string subtype that's not a string, return a true
- 		   string with the same string data. */
- 		PyStringObject *s = (PyStringObject *)v;
- 		return PyString_FromStringAndSize(s->ob_sval, s->ob_size);
- 	}
  	if (v->ob_type->tp_str == NULL)
  		return PyObject_Repr(v);
--- 262,265 ----

Index: stringobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/stringobject.c,v
retrieving revision 2.138
retrieving revision 2.139
diff -C2 -d -r2.138 -r2.139
*** stringobject.c	2001/10/13 15:57:55	2.138
--- stringobject.c	2001/10/16 20:18:24	2.139
***************
*** 567,571 ****
--- 567,582 ----
  	char c;
  	int quote;
+ 
  	/* XXX Ought to check for interrupts when writing long strings */
+ 	if (! PyString_CheckExact(op)) {
+ 		int ret;
+ 		/* A str subclass may have its own __str__ method. */
+ 		op = (PyStringObject *) PyObject_Str((PyObject *)op);
+ 		if (op == NULL)
+ 			return -1;
+ 		ret = string_print(op, fp, flags);
+ 		Py_DECREF(op);
+ 		return ret;
+ 	}
  	if (flags & Py_PRINT_RAW) {
  		fwrite(op->ob_sval, 1, (int) op->ob_size, fp);
***************
*** 652,657 ****
  string_str(PyObject *s)
  {
! 	Py_INCREF(s);
! 	return s;
  }
  
--- 663,676 ----
  string_str(PyObject *s)
  {
! 	assert(PyString_Check(s));
! 	if (PyString_CheckExact(s)) {
! 		Py_INCREF(s);
! 		return s;
! 	}
! 	else {
! 		/* Subtype -- return genuine string with the same value. */
! 		PyStringObject *t = (PyStringObject *) s;
! 		return PyString_FromStringAndSize(t->ob_sval, t->ob_size);
! 	}
  }