# Pyrex - Types
#
+from Cython import Utils
import Naming
class BaseType:
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.
def literal_code(self, value):
assert isinstance(value, str)
- return '"%s"' % _escape_byte_string(value)
+ return '"%s"' % Utils.escape_byte_string(value)
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):
# and associated know-how.
#
+from Cython import Utils
import Naming
import PyrexTypes
import sys
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"
# 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)