[Python-checkins] python/dist/src/Python bltinmodule.c,2.277,2.278

doerwalter@users.sourceforge.net doerwalter@users.sourceforge.net
Mon, 10 Feb 2003 05:19:15 -0800


Update of /cvsroot/python/python/dist/src/Python
In directory sc8-pr-cvs1:/tmp/cvs-serv29110/Python

Modified Files:
	bltinmodule.c 
Log Message:
Change filterstring() and filterunicode(): If the
object is not a real str or unicode but an instance
of a subclass, construct the output via looping
over __getitem__. This guarantees that the result
is the same for function==None and function==lambda x:x

This doesn't happen for tuples, because filtertuple()
uses PyTuple_GetItem().

(This was discussed on SF bug #665835).


Index: bltinmodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/bltinmodule.c,v
retrieving revision 2.277
retrieving revision 2.278
diff -C2 -d -r2.277 -r2.278
*** bltinmodule.c	10 Feb 2003 09:22:01 -0000	2.277
--- bltinmodule.c	10 Feb 2003 13:19:12 -0000	2.278
***************
*** 1935,1948 ****
  
  	if (func == Py_None) {
! 		/* No character is ever false -- share input string
! 		 * (if it's not a subclass) */
! 		if (PyString_CheckExact(strobj))
  			Py_INCREF(strobj);
! 		else
! 			strobj = PyString_FromStringAndSize(
! 				PyString_AS_STRING(strobj),
! 				len
! 			);
! 		return strobj;
  	}
  	if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
--- 1935,1946 ----
  
  	if (func == Py_None) {
! 		/* If it's a real string we can return the original,
! 		 * as no character is ever false and __getitem__
! 		 * does return this character. If it's a subclass
! 		 * we must go through the __getitem__ loop */
! 		if (PyString_CheckExact(strobj)) {
  			Py_INCREF(strobj);
! 			return strobj;
! 		}
  	}
  	if ((result = PyString_FromStringAndSize(NULL, len)) == NULL)
***************
*** 1950,1954 ****
  
  	for (i = j = 0; i < len; ++i) {
! 		PyObject *item, *arg, *good;
  		int ok;
  
--- 1948,1952 ----
  
  	for (i = j = 0; i < len; ++i) {
! 		PyObject *item;
  		int ok;
  
***************
*** 1956,1972 ****
  		if (item == NULL)
  			goto Fail_1;
! 		arg = Py_BuildValue("(O)", item);
! 		if (arg == NULL) {
! 			Py_DECREF(item);
! 			goto Fail_1;
! 		}
! 		good = PyEval_CallObject(func, arg);
! 		Py_DECREF(arg);
! 		if (good == NULL) {
! 			Py_DECREF(item);
! 			goto Fail_1;
  		}
- 		ok = PyObject_IsTrue(good);
- 		Py_DECREF(good);
  		if (ok) {
  			int reslen;
--- 1954,1975 ----
  		if (item == NULL)
  			goto Fail_1;
! 		if (func==Py_None) {
! 			ok = 1;
! 		} else {
! 			PyObject *arg, *good;
! 			arg = Py_BuildValue("(O)", item);
! 			if (arg == NULL) {
! 				Py_DECREF(item);
! 				goto Fail_1;
! 			}
! 			good = PyEval_CallObject(func, arg);
! 			Py_DECREF(arg);
! 			if (good == NULL) {
! 				Py_DECREF(item);
! 				goto Fail_1;
! 			}
! 			ok = PyObject_IsTrue(good);
! 			Py_DECREF(good);
  		}
  		if (ok) {
  			int reslen;
***************
*** 2027,2040 ****
  
  	if (func == Py_None) {
! 		/* No character is ever false -- share input string
! 		 * (it if's not a subclass) */
! 		if (PyUnicode_CheckExact(strobj))
  			Py_INCREF(strobj);
! 		else
! 			strobj = PyUnicode_FromUnicode(
! 				PyUnicode_AS_UNICODE(strobj),
! 				len
! 			);
! 		return strobj;
  	}
  	if ((result = PyUnicode_FromUnicode(NULL, len)) == NULL)
--- 2030,2041 ----
  
  	if (func == Py_None) {
! 		/* If it's a real string we can return the original,
! 		 * as no character is ever false and __getitem__
! 		 * does return this character. If it's a subclass
! 		 * we must go through the __getitem__ loop */
! 		if (PyUnicode_CheckExact(strobj)) {
  			Py_INCREF(strobj);
! 			return strobj;
! 		}
  	}
  	if ((result = PyUnicode_FromUnicode(NULL, len)) == NULL)
***************
*** 2048,2064 ****
  		if (item == NULL)
  			goto Fail_1;
! 		arg = Py_BuildValue("(O)", item);
! 		if (arg == NULL) {
! 			Py_DECREF(item);
! 			goto Fail_1;
! 		}
! 		good = PyEval_CallObject(func, arg);
! 		Py_DECREF(arg);
! 		if (good == NULL) {
! 			Py_DECREF(item);
! 			goto Fail_1;
  		}
- 		ok = PyObject_IsTrue(good);
- 		Py_DECREF(good);
  		if (ok) {
  			int reslen;
--- 2049,2069 ----
  		if (item == NULL)
  			goto Fail_1;
! 		if (func == Py_None) {
! 			ok = 1;
! 		} else {
! 			arg = Py_BuildValue("(O)", item);
! 			if (arg == NULL) {
! 				Py_DECREF(item);
! 				goto Fail_1;
! 			}
! 			good = PyEval_CallObject(func, arg);
! 			Py_DECREF(arg);
! 			if (good == NULL) {
! 				Py_DECREF(item);
! 				goto Fail_1;
! 			}
! 			ok = PyObject_IsTrue(good);
! 			Py_DECREF(good);
  		}
  		if (ok) {
  			int reslen;