use correct byte encoding for char values, some escaping on char literals
authorStefan Behnel <scoder@users.berlios.de>
Tue, 12 Aug 2008 12:28:16 +0000 (14:28 +0200)
committerStefan Behnel <scoder@users.berlios.de>
Tue, 12 Aug 2008 12:28:16 +0000 (14:28 +0200)
Cython/Compiler/ExprNodes.py
Cython/Utils.py

index e9dbdd64f9fa132e63652b75816e69896e0b4295..b833824c932fef24507bd60c7a0b04265e0bc0b1 100644 (file)
@@ -14,6 +14,7 @@ from Builtin import list_type, tuple_type, dict_type
 import Symtab
 import Options
 from Annotate import AnnotationItem
+from Cython import Utils
 
 from Cython.Debugging import print_call_chain
 from DebugFlags import debug_disposal_code, debug_temp_alloc, \
@@ -639,16 +640,10 @@ class CharNode(ConstNode):
     type = PyrexTypes.c_char_type
     
     def compile_time_value(self, denv):
-        return ord(self.value)
+        return ord(self.value.byteencode())
     
     def calculate_result_code(self):
-        if self.value == "'":
-            return r"'\''"
-        char = ord(self.value)
-        if char < 32:
-            return "'\\x%02X'" % char
-        else:
-            return "'%s'" % self.value
+        return "'%s'" % Utils.escape_character(self.value.byteencode())
 
 
 class IntNode(ConstNode):
index 83ca46b501ba9024aa7c183c68d79272caf3951e..480ed6d9802757397c90ded6bbda52f42e363099 100644 (file)
@@ -115,7 +115,7 @@ def _to_escape_sequence(s):
     elif s == '"':
         return r'\"'
     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])
 
 _c_special = ('\0', '\n', '\r', '\t', '??', '"')
@@ -130,6 +130,17 @@ def _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):
     s = s.replace('\\', '\\\\')
     if _has_specials(s):