Commit dcc84ee5 authored by Stefan Behnel's avatar Stefan Behnel

clean up and fix docstring serialisation (some are const, some are not)

parent 8dc18957
...@@ -1180,11 +1180,6 @@ class GlobalState(object): ...@@ -1180,11 +1180,6 @@ class GlobalState(object):
def get_interned_identifier(self, text): def get_interned_identifier(self, text):
return self.get_py_string_const(text, identifier=True) return self.get_py_string_const(text, identifier=True)
def as_c_string_literal(self, byte_string):
value = StringEncoding.split_string_literal(
StringEncoding.escape_byte_string(byte_string.byteencode()))
return '"%s"' % value
def new_string_const(self, text, byte_string): def new_string_const(self, text, byte_string):
cname = self.new_string_const_cname(byte_string) cname = self.new_string_const_cname(byte_string)
c = StringConst(cname, text, byte_string) c = StringConst(cname, text, byte_string)
...@@ -1644,9 +1639,6 @@ class CCodeWriter(object): ...@@ -1644,9 +1639,6 @@ class CCodeWriter(object):
return self.globalstate.get_py_string_const( return self.globalstate.get_py_string_const(
text, identifier, is_str, unicode_value).cname text, identifier, is_str, unicode_value).cname
def as_c_string_literal(self, text):
return self.globalstate.as_c_string_literal(text)
def get_argument_default_const(self, type): def get_argument_default_const(self, type):
return self.globalstate.get_py_const(type).cname return self.globalstate.get_py_const(type).cname
......
...@@ -1344,7 +1344,7 @@ class BytesNode(ConstNode): ...@@ -1344,7 +1344,7 @@ class BytesNode(ConstNode):
result = code.get_string_const(self.value) result = code.get_string_const(self.value)
else: else:
# not const => use plain C string literal and cast to mutable type # not const => use plain C string literal and cast to mutable type
literal = code.as_c_string_literal(self.value) literal = self.value.as_c_string_literal()
# C++ may require a cast # C++ may require a cast
result = typecast(self.type, PyrexTypes.c_void_ptr_type, literal) result = typecast(self.type, PyrexTypes.c_void_ptr_type, literal)
self.result_code = result self.result_code = result
......
...@@ -1949,12 +1949,15 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): ...@@ -1949,12 +1949,15 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
"static struct PyGetSetDef %s[] = {" % "static struct PyGetSetDef %s[] = {" %
env.getset_table_cname) env.getset_table_cname)
for entry in env.property_entries: for entry in env.property_entries:
if entry.doc: doc = entry.doc
doc_code = "%s" % code.get_string_const(entry.doc) if doc:
if doc.is_unicode:
doc = doc.as_utf8_string()
doc_code = doc.as_c_string_literal()
else: else:
doc_code = "0" doc_code = "0"
code.putln( code.putln(
'{(char *)"%s", %s, %s, %s, 0},' % ( '{(char *)"%s", %s, %s, (char *)%s, 0},' % (
entry.name, entry.name,
entry.getter_cname or "0", entry.getter_cname or "0",
entry.setter_cname or "0", entry.setter_cname or "0",
......
...@@ -29,7 +29,7 @@ from .PyrexTypes import py_object_type, error_type ...@@ -29,7 +29,7 @@ from .PyrexTypes import py_object_type, error_type
from .Symtab import (ModuleScope, LocalScope, ClosureScope, from .Symtab import (ModuleScope, LocalScope, ClosureScope,
StructOrUnionScope, PyClassScope, CppClassScope, TemplateScope) StructOrUnionScope, PyClassScope, CppClassScope, TemplateScope)
from .Code import UtilityCode from .Code import UtilityCode
from .StringEncoding import EncodedString, escape_byte_string, split_string_literal from .StringEncoding import EncodedString
from . import Future from . import Future
from . import Options from . import Options
from . import DebugFlags from . import DebugFlags
...@@ -3318,12 +3318,12 @@ class DefNodeWrapper(FuncDefNode): ...@@ -3318,12 +3318,12 @@ class DefNodeWrapper(FuncDefNode):
docstr = entry.doc docstr = entry.doc
if docstr.is_unicode: if docstr.is_unicode:
docstr = docstr.utf8encode() docstr = docstr.as_utf8_string()
code.putln( code.putln(
'static char %s[] = "%s";' % ( 'static char %s[] = %s;' % (
entry.doc_cname, entry.doc_cname,
split_string_literal(escape_byte_string(docstr)))) docstr.as_c_string_literal()))
if entry.is_special: if entry.is_special:
code.putln('#if CYTHON_COMPILING_IN_CPYTHON') code.putln('#if CYTHON_COMPILING_IN_CPYTHON')
......
...@@ -136,6 +136,9 @@ class EncodedString(_unicode): ...@@ -136,6 +136,9 @@ class EncodedString(_unicode):
def contains_surrogates(self): def contains_surrogates(self):
return string_contains_surrogates(self) return string_contains_surrogates(self)
def as_utf8_string(self):
return BytesLiteral(self.utf8encode())
def string_contains_surrogates(ustring): def string_contains_surrogates(ustring):
""" """
...@@ -177,6 +180,10 @@ class BytesLiteral(_bytes): ...@@ -177,6 +180,10 @@ class BytesLiteral(_bytes):
is_unicode = False is_unicode = False
def as_c_string_literal(self):
value = split_string_literal(escape_byte_string(self))
return '"%s"' % value
char_from_escape_sequence = { char_from_escape_sequence = {
r'\a' : u'\a', r'\a' : u'\a',
......
...@@ -416,14 +416,12 @@ class DocStringSlot(SlotDescriptor): ...@@ -416,14 +416,12 @@ class DocStringSlot(SlotDescriptor):
# Descriptor for the docstring slot. # Descriptor for the docstring slot.
def slot_code(self, scope): def slot_code(self, scope):
if scope.doc is not None: doc = scope.doc
if scope.doc.is_unicode: if doc is None:
doc = scope.doc.utf8encode()
else:
doc = scope.doc.byteencode()
return '"%s"' % StringEncoding.escape_byte_string(doc)
else:
return "0" return "0"
if doc.is_unicode:
doc = doc.as_utf8_string()
return doc.as_c_string_literal()
class SuiteSlot(SlotDescriptor): class SuiteSlot(SlotDescriptor):
......
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