From: Stefan Behnel Date: Thu, 15 May 2008 09:13:23 +0000 (+0200) Subject: byte encode docstrings correctly X-Git-Tag: 0.9.8rc1~37^2~58 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=825a2cd3d3d9e324b9d23d6b2d00492915acfcba;p=cython.git byte encode docstrings correctly --- diff --git a/Cython/Compiler/PyrexTypes.py b/Cython/Compiler/PyrexTypes.py index e44de190..31181064 100644 --- a/Cython/Compiler/PyrexTypes.py +++ b/Cython/Compiler/PyrexTypes.py @@ -2,6 +2,7 @@ # Pyrex - Types # +from Cython import Utils import Naming class BaseType: @@ -922,23 +923,6 @@ class CEnumType(CType): return self.base_declaration_code(public_decl(base, dll_linkage), entity_code) -def _escape_byte_string(s): - s = s.replace('\0', r'\x00') - try: - s.decode("ASCII") - return s - except UnicodeDecodeError: - pass - l = [] - append = l.append - for c in s: - o = ord(c) - if o >= 128: - append('\\x%X' % o) - else: - append(c) - return ''.join(l) - class CStringType: # Mixin class for C string types. @@ -951,7 +935,7 @@ class CStringType: def literal_code(self, value): assert isinstance(value, str) - return '"%s"' % _escape_byte_string(value) + return '"%s"' % Utils.escape_byte_string(value) class CUTF8StringType: @@ -965,7 +949,7 @@ class CUTF8StringType: def literal_code(self, value): assert isinstance(value, str) - return '"%s"' % _escape_byte_string(value) + return '"%s"' % Utils.escape_byte_string(value) class CCharArrayType(CStringType, CArrayType): diff --git a/Cython/Compiler/TypeSlots.py b/Cython/Compiler/TypeSlots.py index d05edb61..9f06a445 100644 --- a/Cython/Compiler/TypeSlots.py +++ b/Cython/Compiler/TypeSlots.py @@ -3,6 +3,7 @@ # and associated know-how. # +from Cython import Utils import Naming import PyrexTypes import sys @@ -298,7 +299,11 @@ class DocStringSlot(SlotDescriptor): def slot_code(self, scope): if scope.doc is not None: - return '"%s"' % scope.doc + if scope.doc.is_unicode: + doc = scope.doc.utf8encode() + else: + doc = scope.doc.byteencode() + return '"%s"' % Utils.escape_byte_string(doc) else: return "0" diff --git a/Cython/Utils.py b/Cython/Utils.py index 20125e24..de22a6a9 100644 --- a/Cython/Utils.py +++ b/Cython/Utils.py @@ -91,3 +91,20 @@ class EncodedString(unicode): # def __eq__(self, other): # return unicode.__eq__(self, other) and \ # getattr(other, 'encoding', '') == self.encoding + +def escape_byte_string(s): + s = s.replace('\0', r'\x00') + try: + s.decode("ASCII") + return s + except UnicodeDecodeError: + pass + l = [] + append = l.append + for c in s: + o = ord(c) + if o >= 128: + append('\\x%X' % o) + else: + append(c) + return ''.join(l)