diff Python/bltinmodule.c @ 27681:e5454fe1aafd legacy-trunk

Make sure filter() never returns tuple, str or unicode subclasses. (Discussed in SF patch #665835)
author Walter Dörwald <walter@livinglogic.de>
date Tue, 04 Feb 2003 20:24:45 +0000
parents 8f8448fb58d6
children cc01fb654ce3
line wrap: on
line diff
--- a/Python/bltinmodule.c	Tue Feb 04 19:35:03 2003 +0000
+++ b/Python/bltinmodule.c	Tue Feb 04 20:24:45 2003 +0000
@@ -1838,7 +1838,10 @@
 	int len = PyTuple_Size(tuple);
 
 	if (len == 0) {
-		Py_INCREF(tuple);
+		if (PyTuple_CheckExact(tuple))
+			Py_INCREF(tuple);
+		else
+			tuple = PyTuple_New(0);
 		return tuple;
 	}
 
@@ -1895,8 +1898,15 @@
 	int outlen = len;
 
 	if (func == Py_None) {
-		/* No character is ever false -- share input string */
-		Py_INCREF(strobj);
+		/* 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)
@@ -1980,8 +1990,15 @@
 	int outlen = len;
 
 	if (func == Py_None) {
-		/* No character is ever false -- share input string */
-		Py_INCREF(strobj);
+		/* 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)