[pypy-svn] r55972 - in pypy/dist/pypy/module/_sre: . test

fijal at codespeak.net fijal at codespeak.net
Thu Jun 19 20:56:59 CEST 2008


Author: fijal
Date: Thu Jun 19 20:56:56 2008
New Revision: 55972

Modified:
   pypy/dist/pypy/module/_sre/app_sre.py
   pypy/dist/pypy/module/_sre/test/test_app_sre.py
Log:
Fix for #issue379


Modified: pypy/dist/pypy/module/_sre/app_sre.py
==============================================================================
--- pypy/dist/pypy/module/_sre/app_sre.py	(original)
+++ pypy/dist/pypy/module/_sre/app_sre.py	Thu Jun 19 20:56:56 2008
@@ -79,7 +79,9 @@
             filter = sre._subx(self, repl)
         state = _sre._State(string, 0, sys.maxint, self.flags)
         sublist = []
-        
+
+        need_unicode = (isinstance(string, unicode) or
+                        isinstance(self.pattern, unicode))
         n = last_pos = 0
         while not count or n < count:
             state.reset()
@@ -91,9 +93,12 @@
                                 last_pos == state.string_position and n > 0):
                 # the above ignores empty matches on latest position
                 if callable(filter):
-                    sublist.append(filter(SRE_Match(self, state)))
+                    to_app = filter(SRE_Match(self, state))
                 else:
-                    sublist.append(filter)
+                    to_app = filter
+                if isinstance(to_app, unicode):
+                    need_unicode = True
+                sublist.append(to_app)
                 last_pos = state.string_position
                 n += 1
             if state.string_position == state.start:
@@ -103,7 +108,10 @@
 
         if last_pos < state.end:
             sublist.append(string[last_pos:state.end])
-        item = "".join(sublist)
+        if need_unicode:
+            item = u"".join(sublist)
+        else:
+            item = "".join(sublist)
         return item, n
 
     def sub(self, repl, string, count=0):

Modified: pypy/dist/pypy/module/_sre/test/test_app_sre.py
==============================================================================
--- pypy/dist/pypy/module/_sre/test/test_app_sre.py	(original)
+++ pypy/dist/pypy/module/_sre/test/test_app_sre.py	Thu Jun 19 20:56:56 2008
@@ -179,6 +179,10 @@
         assert ("rbd\nbr\n", 2) == re.subn("a(.)", r"b\1\n", "radar")
         assert ("bbbba", 2) == re.subn("a", "b", "ababa", 2)
 
+    def test_sub_unicode(self):
+        import re
+        assert isinstance(re.sub(u"a", u"b", u""), unicode)
+
     def test_sub_callable(self):
         import re
         def call_me(match):



More information about the Pypy-commit mailing list