Py3 fix: make sure byte strings end up in the code as expected (not like >>b'...'<<)
authorStefan Behnel <scoder@users.berlios.de>
Mon, 6 Jul 2009 15:29:40 +0000 (17:29 +0200)
committerStefan Behnel <scoder@users.berlios.de>
Mon, 6 Jul 2009 15:29:40 +0000 (17:29 +0200)
Cython/Compiler/StringEncoding.py

index e7db555ba14c42b266b14d349db72144f583939e..22644c6e7f5177e9f3dea72de29707d53795ae42 100644 (file)
@@ -145,12 +145,16 @@ def escape_character(c):
         return c
 
 def escape_byte_string(s):
+    """Escape a byte string so that it can be written into C code.
+    Note that this returns a Unicode string instead which, when
+    encoded as ISO-8859-1, will result in the correct byte sequence
+    being written.
+    """
     if _has_specials(s):
         for special, replacement in _c_special_replacements:
             s = s.replace(special, replacement)
     try:
-        s.decode("ASCII") # trial decoding: plain ASCII => done
-        return s
+        return s.decode("ASCII") # trial decoding: plain ASCII => done
     except UnicodeDecodeError:
         pass
     if sys.version_info[0] >= 3:
@@ -161,7 +165,7 @@ def escape_byte_string(s):
                 extend(('\\%3o' % b).encode('ASCII'))
             else:
                 append(b)
-        return bytes(s_new)
+        return s_new.decode('ISO-8859-1')
     else:
         l = []
         append = l.append
@@ -171,7 +175,7 @@ def escape_byte_string(s):
                 append('\\%3o' % o)
             else:
                 append(c)
-        return join_bytes(l)
+        return join_bytes(l).decode('ISO-8859-1')
 
 def split_docstring(s):
     if len(s) < 2047: