summaryrefslogtreecommitdiff
path: root/Cython/Compiler/ModuleNode.py
diff options
context:
space:
mode:
authorRobert Bradshaw <robertwb@gmail.com>2013-02-20 03:43:38 -0800
committerRobert Bradshaw <robertwb@gmail.com>2013-02-20 03:43:38 -0800
commit1e81abf95cb70a8d1d52a3e8f79bbe45811e77ea (patch)
treee2035351f8de2e6051c69cb852bde4a2c4a16379 /Cython/Compiler/ModuleNode.py
parentc06eadaaae63abe5a8e48b12e99bc7eb7b6b04d9 (diff)
downloadcython-1e81abf95cb70a8d1d52a3e8f79bbe45811e77ea.tar.gz
Allow setting a default encoding to ease str/unicode <-> c string conversion.
Diffstat (limited to 'Cython/Compiler/ModuleNode.py')
-rw-r--r--Cython/Compiler/ModuleNode.py14
1 files changed, 14 insertions, 0 deletions
diff --git a/Cython/Compiler/ModuleNode.py b/Cython/Compiler/ModuleNode.py
index f431a70de..645c34748 100644
--- a/Cython/Compiler/ModuleNode.py
+++ b/Cython/Compiler/ModuleNode.py
@@ -556,7 +556,17 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
code.putln("#endif")
code.putln("")
code.put(UtilityCode.load_as_string("UtilityFunctionPredeclarations", "ModuleSetupCode.c")[0])
+
+ c_string_type = env.directives['c_string_type']
+ c_string_encoding = env.directives['c_string_encoding']
+ if c_string_type != 'bytes' and not c_string_encoding:
+ error(self.pos, "a default encoding must be provided if c_string_type != bytes")
+ code.putln('#define __PYX_DEFAULT_STRING_ENCODING_IS_ASCII %s' % int(c_string_encoding == 'ascii'))
+ code.putln('#define __PYX_DEFAULT_STRING_ENCODING "%s"' % c_string_encoding)
+ code.putln('#define __Pyx_PyObject_FromString __Pyx_Py%s_FromString' % c_string_type.title())
+ code.putln('#define __Pyx_PyObject_FromStringAndSize __Pyx_Py%s_FromStringAndSize' % c_string_type.title())
code.put(UtilityCode.load_as_string("TypeConversions", "TypeConversion.c")[0])
+
code.put(Nodes.branch_prediction_macros)
code.putln('')
code.putln('static PyObject *%s;' % env.module_cname)
@@ -1888,6 +1898,10 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
code.putln("/*--- Initialize various global constants etc. ---*/")
code.putln(code.error_goto_if_neg("__Pyx_InitGlobals()", self.pos))
+ code.putln("#ifdef __PYX_DEFAULT_STRING_ENCODING_IS_ASCII")
+ code.putln("if (__Pyx_init_sys_getdefaultencoding_not_ascii() < 0) %s" % code.error_goto(self.pos))
+ code.putln("#endif")
+
__main__name = code.globalstate.get_py_string_const(
EncodedString("__main__"), identifier=True)
code.putln("if (%s%s) {" % (Naming.module_is_main, self.full_module_name.replace('.', '__')))