[pypy-svn] r8948 - pypy/dist/pypy/module

ac at codespeak.net ac at codespeak.net
Mon Feb 7 13:01:25 CET 2005


Author: ac
Date: Mon Feb  7 13:01:25 2005
New Revision: 8948

Modified:
   pypy/dist/pypy/module/__builtin__module.py
Log:
Special case str/unicode argument to filter as per cpython.

Modified: pypy/dist/pypy/module/__builtin__module.py
==============================================================================
--- pypy/dist/pypy/module/__builtin__module.py	(original)
+++ pypy/dist/pypy/module/__builtin__module.py	Mon Feb  7 13:01:25 2005
@@ -122,7 +122,24 @@
     """construct a list of those elements of collection for which function
        is True.  If function is None, then return the items in the sequence
        which are True."""
-
+    str_type = None
+    if isinstance(collection, str):
+        str_type = str
+    elif isinstance(collection, unicode):
+        str_type = unicode
+
+    if str_type is not None:
+        if function is None and type(collection) is str_type:
+            return collection
+        res = []
+        for i in xrange(len(collection)):
+            c = collection[i]
+            if function is None or function(c):
+                if not isinstance(c, str_type):
+                    raise TypeError("can't filter %s to %s: __getitem__ returned different type", str_type.__name__, str_type.__name__)
+                res.append(c)
+        return str_type().join(res)
+        
     if function is None:
         res = [item for item in collection if item]
     else:
@@ -130,8 +147,6 @@
 
     if type(collection) is tuple:
        return tuple(res)
-    elif type(collection) is str:
-       return "".join(res)
     else:
        return res
 



More information about the Pypy-commit mailing list