Commit 825a2cd3 authored by Stefan Behnel's avatar Stefan Behnel

byte encode docstrings correctly

parent 39f134d3
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
# Pyrex - Types # Pyrex - Types
# #
from Cython import Utils
import Naming import Naming
class BaseType: class BaseType:
...@@ -922,23 +923,6 @@ class CEnumType(CType): ...@@ -922,23 +923,6 @@ class CEnumType(CType):
return self.base_declaration_code(public_decl(base, dll_linkage), entity_code) 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: class CStringType:
# Mixin class for C string types. # Mixin class for C string types.
...@@ -951,7 +935,7 @@ class CStringType: ...@@ -951,7 +935,7 @@ class CStringType:
def literal_code(self, value): def literal_code(self, value):
assert isinstance(value, str) assert isinstance(value, str)
return '"%s"' % _escape_byte_string(value) return '"%s"' % Utils.escape_byte_string(value)
class CUTF8StringType: class CUTF8StringType:
...@@ -965,7 +949,7 @@ class CUTF8StringType: ...@@ -965,7 +949,7 @@ class CUTF8StringType:
def literal_code(self, value): def literal_code(self, value):
assert isinstance(value, str) assert isinstance(value, str)
return '"%s"' % _escape_byte_string(value) return '"%s"' % Utils.escape_byte_string(value)
class CCharArrayType(CStringType, CArrayType): class CCharArrayType(CStringType, CArrayType):
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
# and associated know-how. # and associated know-how.
# #
from Cython import Utils
import Naming import Naming
import PyrexTypes import PyrexTypes
import sys import sys
...@@ -298,7 +299,11 @@ class DocStringSlot(SlotDescriptor): ...@@ -298,7 +299,11 @@ class DocStringSlot(SlotDescriptor):
def slot_code(self, scope): def slot_code(self, scope):
if scope.doc is not None: 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: else:
return "0" return "0"
......
...@@ -91,3 +91,20 @@ class EncodedString(unicode): ...@@ -91,3 +91,20 @@ class EncodedString(unicode):
# def __eq__(self, other): # def __eq__(self, other):
# return unicode.__eq__(self, other) and \ # return unicode.__eq__(self, other) and \
# getattr(other, 'encoding', '') == self.encoding # 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)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment