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, \
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):
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', '??', '"')
_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):