[pypy-svn] r75723 - in pypy/branch/interplevel-codecs/pypy: module/_codecs/test rlib

afa at codespeak.net afa at codespeak.net
Thu Jul 1 18:35:55 CEST 2010


Author: afa
Date: Thu Jul  1 18:35:53 2010
New Revision: 75723

Modified:
   pypy/branch/interplevel-codecs/pypy/module/_codecs/test/test_codecs.py
   pypy/branch/interplevel-codecs/pypy/rlib/runicode.py
Log:
fix encode_charmap with 'replace': the replacement character must be translated again.


Modified: pypy/branch/interplevel-codecs/pypy/module/_codecs/test/test_codecs.py
==============================================================================
--- pypy/branch/interplevel-codecs/pypy/module/_codecs/test/test_codecs.py	(original)
+++ pypy/branch/interplevel-codecs/pypy/module/_codecs/test/test_codecs.py	Thu Jul  1 18:35:53 2010
@@ -526,6 +526,14 @@
     def test_charmap_encode(self):
         assert 'xxx'.encode('charmap') == 'xxx'
 
+    def test_charmap_encode_replace(self):
+        charmap = dict([ (ord(c), 2*c.upper()) for c in "abcdefgh"])
+        charmap[ord("?")] = "XYZ"
+        import codecs
+        sin = u"abcDEF"
+        sout = codecs.charmap_encode(sin, "replace", charmap)[0]
+        assert sout == "AABBCCXYZXYZXYZ"
+
     def test_charmap_decode_2(self):
         assert 'foo'.decode('charmap') == 'foo'
 

Modified: pypy/branch/interplevel-codecs/pypy/rlib/runicode.py
==============================================================================
--- pypy/branch/interplevel-codecs/pypy/rlib/runicode.py	(original)
+++ pypy/branch/interplevel-codecs/pypy/rlib/runicode.py	Thu Jul  1 18:35:53 2010
@@ -779,10 +779,17 @@
 
         c = mapping.get(ch, '')
         if len(c) == 0:
-            r, pos = errorhandler(errors, "charmap",
-                                  "character maps to <undefined>",
-                                  s, pos, pos + 1)
-            result.append(r)
+            res, pos = errorhandler(errors, "charmap",
+                                    "character maps to <undefined>",
+                                    s, pos, pos + 1)
+            for ch2 in res:
+                c2 = mapping.get(unichr(ord(ch2)), '')
+                if len(c2) == 0:
+                    raise_unicode_exception_encode(
+                        errors, "charmap",
+                        "character maps to <undefined>",
+                        s,  pos, pos + 1)
+                result.append(c2)
             continue
         result.append(c)
         pos += 1



More information about the Pypy-commit mailing list