From 808ed5c27ea16382043f0fb6cd207cf198ae5c1d Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Mon, 9 Aug 2010 11:39:02 +0200 Subject: [PATCH] prevent control characters in unicode literals (ord<32) from sneaking into the C source --- Cython/Compiler/StringEncoding.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Cython/Compiler/StringEncoding.py b/Cython/Compiler/StringEncoding.py index 09ca2079..b5bd45f0 100644 --- a/Cython/Compiler/StringEncoding.py +++ b/Cython/Compiler/StringEncoding.py @@ -135,7 +135,7 @@ def _to_escape_sequence(s): # 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', '??', '"') +_c_special = ('\\', '??', '"') + tuple(map(chr, range(32))) _c_special_replacements = [(orig.encode('ASCII'), _to_escape_sequence(orig).encode('ASCII')) for orig in _c_special ] @@ -171,7 +171,8 @@ def escape_byte_string(s): """ if _has_specials(s): for special, replacement in _c_special_replacements: - s = s.replace(special, replacement) + if special in s: + s = s.replace(special, replacement) try: return s.decode("ASCII") # trial decoding: plain ASCII => done except UnicodeDecodeError: -- 2.26.2