Commit fb651ae7 authored by Stefan Behnel's avatar Stefan Behnel

Py3 fix: make sure byte strings end up in the code as expected (not like >>b'...'<<)

parent 11955ff6
...@@ -145,12 +145,16 @@ def escape_character(c): ...@@ -145,12 +145,16 @@ def escape_character(c):
return c return c
def escape_byte_string(s): 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): if _has_specials(s):
for special, replacement in _c_special_replacements: for special, replacement in _c_special_replacements:
s = s.replace(special, replacement) s = s.replace(special, replacement)
try: try:
s.decode("ASCII") # trial decoding: plain ASCII => done return s.decode("ASCII") # trial decoding: plain ASCII => done
return s
except UnicodeDecodeError: except UnicodeDecodeError:
pass pass
if sys.version_info[0] >= 3: if sys.version_info[0] >= 3:
...@@ -161,7 +165,7 @@ def escape_byte_string(s): ...@@ -161,7 +165,7 @@ def escape_byte_string(s):
extend(('\\%3o' % b).encode('ASCII')) extend(('\\%3o' % b).encode('ASCII'))
else: else:
append(b) append(b)
return bytes(s_new) return s_new.decode('ISO-8859-1')
else: else:
l = [] l = []
append = l.append append = l.append
...@@ -171,7 +175,7 @@ def escape_byte_string(s): ...@@ -171,7 +175,7 @@ def escape_byte_string(s):
append('\\%3o' % o) append('\\%3o' % o)
else: else:
append(c) append(c)
return join_bytes(l) return join_bytes(l).decode('ISO-8859-1')
def split_docstring(s): def split_docstring(s):
if len(s) < 2047: if len(s) < 2047:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment