From efb4bde97220ed68f8b23d19da11175e14aa5d00 Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Tue, 12 Aug 2008 14:28:16 +0200 Subject: [PATCH] use correct byte encoding for char values, some escaping on char literals --- Cython/Compiler/ExprNodes.py | 11 +++-------- Cython/Utils.py | 13 ++++++++++++- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py index e9dbdd64..b833824c 100644 --- a/Cython/Compiler/ExprNodes.py +++ b/Cython/Compiler/ExprNodes.py @@ -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): diff --git a/Cython/Utils.py b/Cython/Utils.py index 83ca46b5..480ed6d9 100644 --- a/Cython/Utils.py +++ b/Cython/Utils.py @@ -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): -- 2.26.2