Commit efb4bde9 authored by Stefan Behnel's avatar Stefan Behnel

use correct byte encoding for char values, some escaping on char literals

parent c3d75bde
...@@ -14,6 +14,7 @@ from Builtin import list_type, tuple_type, dict_type ...@@ -14,6 +14,7 @@ from Builtin import list_type, tuple_type, dict_type
import Symtab import Symtab
import Options import Options
from Annotate import AnnotationItem from Annotate import AnnotationItem
from Cython import Utils
from Cython.Debugging import print_call_chain from Cython.Debugging import print_call_chain
from DebugFlags import debug_disposal_code, debug_temp_alloc, \ from DebugFlags import debug_disposal_code, debug_temp_alloc, \
...@@ -639,16 +640,10 @@ class CharNode(ConstNode): ...@@ -639,16 +640,10 @@ class CharNode(ConstNode):
type = PyrexTypes.c_char_type type = PyrexTypes.c_char_type
def compile_time_value(self, denv): def compile_time_value(self, denv):
return ord(self.value) return ord(self.value.byteencode())
def calculate_result_code(self): def calculate_result_code(self):
if self.value == "'": return "'%s'" % Utils.escape_character(self.value.byteencode())
return r"'\''"
char = ord(self.value)
if char < 32:
return "'\\x%02X'" % char
else:
return "'%s'" % self.value
class IntNode(ConstNode): class IntNode(ConstNode):
......
...@@ -115,7 +115,7 @@ def _to_escape_sequence(s): ...@@ -115,7 +115,7 @@ def _to_escape_sequence(s):
elif s == '"': elif s == '"':
return r'\"' return r'\"'
else: else:
# oct passes much better than hex # within a character sequence, oct passes much better than hex
return ''.join(['\\%03o' % ord(c) for c in s]) return ''.join(['\\%03o' % ord(c) for c in s])
_c_special = ('\0', '\n', '\r', '\t', '??', '"') _c_special = ('\0', '\n', '\r', '\t', '??', '"')
...@@ -130,6 +130,17 @@ def _build_specials_test(): ...@@ -130,6 +130,17 @@ def _build_specials_test():
_has_specials = _build_specials_test() _has_specials = _build_specials_test()
def escape_character(c):
if c in '\n\r\t\\':
return repr(c)[1:-1]
elif c == "'":
return "\\'"
elif ord(c) < 32:
# hex works well for characters
return "\\x%02X" % ord(c)
else:
return c
def escape_byte_string(s): def escape_byte_string(s):
s = s.replace('\\', '\\\\') s = s.replace('\\', '\\\\')
if _has_specials(s): if _has_specials(s):
......
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