diff options
author | Jeroen Demeyer <jdemeyer@cage.ugent.be> | 2015-05-23 22:52:44 +0200 |
---|---|---|
committer | Jeroen Demeyer <jdemeyer@cage.ugent.be> | 2015-05-23 22:52:44 +0200 |
commit | 88a0e89f87639c4c3cd935c5875e25791e38c358 (patch) | |
tree | 645c48a8418c252992fc53c2dbc7e515a6beddf5 /Cython/Compiler/ModuleNode.py | |
parent | e310ccb4e933ea0ca986b69dfe325c3643d5cf78 (diff) | |
parent | aac55cf61e9f6c05fb6b8c33764a7f15eb4d2708 (diff) | |
download | cython-88a0e89f87639c4c3cd935c5875e25791e38c358.tar.gz |
Merge remote-tracking branch 'origin/master'
Diffstat (limited to 'Cython/Compiler/ModuleNode.py')
-rw-r--r-- | Cython/Compiler/ModuleNode.py | 175 |
1 files changed, 14 insertions, 161 deletions
diff --git a/Cython/Compiler/ModuleNode.py b/Cython/Compiler/ModuleNode.py index 7a06009da..28ec86e78 100644 --- a/Cython/Compiler/ModuleNode.py +++ b/Cython/Compiler/ModuleNode.py @@ -571,26 +571,6 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): code.putln("") code.putln("#define PY_SSIZE_T_CLEAN") - # sizeof(PyLongObject.ob_digit[0]) may have been determined dynamically - # at compile time in CPython, in which case we can't know the correct - # storage size for an installed system. We can rely on it only if - # pyconfig.h defines it statically, i.e. if it was set by "configure". - # Once we include "Python.h", it will come up with its own idea about - # a suitable value, which may or may not match the real one. - code.putln("#ifndef CYTHON_USE_PYLONG_INTERNALS") - code.putln("#ifdef PYLONG_BITS_IN_DIGIT") - # assume it's an incorrect left-over - code.putln("#define CYTHON_USE_PYLONG_INTERNALS 0") - code.putln("#else") - code.putln('#include "pyconfig.h"') - code.putln("#ifdef PYLONG_BITS_IN_DIGIT") - code.putln("#define CYTHON_USE_PYLONG_INTERNALS 1") - code.putln("#else") - code.putln("#define CYTHON_USE_PYLONG_INTERNALS 0") - code.putln("#endif") - code.putln("#endif") - code.putln("#endif") - for filename in env.python_include_files: code.putln('#include "%s"' % filename) code.putln("#ifndef Py_PYTHON_H") @@ -1982,7 +1962,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): "};") def generate_import_star(self, env, code): - env.use_utility_code(streq_utility_code) + env.use_utility_code(UtilityCode.load_cached("CStringEquals", "StringTools.c")) code.putln() code.enter_cfunc_scope() # as we need labels code.putln("static int %s(PyObject *o, PyObject* py_name, char *name) {" % Naming.import_star_set) @@ -2044,8 +2024,9 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): code.putln("bad:") code.putln("return -1;") code.putln("}") - code.putln(import_star_utility_code) - code.exit_cfunc_scope() # done with labels + code.putln("") + code.putln(UtilityCode.load_cached("ImportStar", "ImportExport.c").impl) + code.exit_cfunc_scope() # done with labels def generate_module_init_func(self, imported_modules, env, code): code.enter_cfunc_scope() @@ -2170,6 +2151,10 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): code.putln("/*--- Execution code ---*/") code.mark_pos(None) + code.putln("#ifdef __Pyx_Generator_USED") + code.put_error_if_neg(self.pos, "__Pyx_patch_abc()") + code.putln("#endif") + if profile or linetrace: code.put_trace_call(header3, self.pos, nogil=not code.funcstate.gil_owned) code.funcstate.can_trace = True @@ -2358,12 +2343,13 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): wmain = "wmain" else: wmain = Options.embed + main_method = UtilityCode.load_cached("MainFunction", "Embed.c") code.globalstate.use_utility_code( main_method.specialize( - module_name = env.module_name, - module_is_main = module_is_main, - main_method = Options.embed, - wmain_method = wmain)) + module_name=env.module_name, + module_is_main=module_is_main, + main_method=Options.embed, + wmain_method=wmain)) def generate_pymoduledef_struct(self, env, code): if env.doc: @@ -2829,140 +2815,7 @@ def generate_cfunction_declaration(entry, env, code, definition): # #------------------------------------------------------------------------------------ -streq_utility_code = UtilityCode( -proto = """ -static CYTHON_INLINE int __Pyx_StrEq(const char *, const char *); /*proto*/ -""", -impl = """ -static CYTHON_INLINE int __Pyx_StrEq(const char *s1, const char *s2) { - while (*s1 != '\\0' && *s1 == *s2) { s1++; s2++; } - return *s1 == *s2; -} -""") - -#------------------------------------------------------------------------------------ - -import_star_utility_code = """ - -/* import_all_from is an unexposed function from ceval.c */ - -static int -__Pyx_import_all_from(PyObject *locals, PyObject *v) -{ - PyObject *all = PyObject_GetAttrString(v, "__all__"); - PyObject *dict, *name, *value; - int skip_leading_underscores = 0; - int pos, err; - - if (all == NULL) { - if (!PyErr_ExceptionMatches(PyExc_AttributeError)) - return -1; /* Unexpected error */ - PyErr_Clear(); - dict = PyObject_GetAttrString(v, "__dict__"); - if (dict == NULL) { - if (!PyErr_ExceptionMatches(PyExc_AttributeError)) - return -1; - PyErr_SetString(PyExc_ImportError, - "from-import-* object has no __dict__ and no __all__"); - return -1; - } -#if PY_MAJOR_VERSION < 3 - all = PyObject_CallMethod(dict, (char *)"keys", NULL); -#else - all = PyMapping_Keys(dict); -#endif - Py_DECREF(dict); - if (all == NULL) - return -1; - skip_leading_underscores = 1; - } - - for (pos = 0, err = 0; ; pos++) { - name = PySequence_GetItem(all, pos); - if (name == NULL) { - if (!PyErr_ExceptionMatches(PyExc_IndexError)) - err = -1; - else - PyErr_Clear(); - break; - } - if (skip_leading_underscores && -#if PY_MAJOR_VERSION < 3 - PyString_Check(name) && - PyString_AS_STRING(name)[0] == '_') -#else - PyUnicode_Check(name) && - PyUnicode_AS_UNICODE(name)[0] == '_') -#endif - { - Py_DECREF(name); - continue; - } - value = PyObject_GetAttr(v, name); - if (value == NULL) - err = -1; - else if (PyDict_CheckExact(locals)) - err = PyDict_SetItem(locals, name, value); - else - err = PyObject_SetItem(locals, name, value); - Py_DECREF(name); - Py_XDECREF(value); - if (err != 0) - break; - } - Py_DECREF(all); - return err; -} - - -static int %(IMPORT_STAR)s(PyObject* m) { - - int i; - int ret = -1; - char* s; - PyObject *locals = 0; - PyObject *list = 0; -#if PY_MAJOR_VERSION >= 3 - PyObject *utf8_name = 0; -#endif - PyObject *name; - PyObject *item; - - locals = PyDict_New(); if (!locals) goto bad; - if (__Pyx_import_all_from(locals, m) < 0) goto bad; - list = PyDict_Items(locals); if (!list) goto bad; - - for(i=0; i<PyList_GET_SIZE(list); i++) { - name = PyTuple_GET_ITEM(PyList_GET_ITEM(list, i), 0); - item = PyTuple_GET_ITEM(PyList_GET_ITEM(list, i), 1); -#if PY_MAJOR_VERSION >= 3 - utf8_name = PyUnicode_AsUTF8String(name); - if (!utf8_name) goto bad; - s = PyBytes_AS_STRING(utf8_name); - if (%(IMPORT_STAR_SET)s(item, name, s) < 0) goto bad; - Py_DECREF(utf8_name); utf8_name = 0; -#else - s = PyString_AsString(name); - if (!s) goto bad; - if (%(IMPORT_STAR_SET)s(item, name, s) < 0) goto bad; -#endif - } - ret = 0; - -bad: - Py_XDECREF(locals); - Py_XDECREF(list); -#if PY_MAJOR_VERSION >= 3 - Py_XDECREF(utf8_name); -#endif - return ret; -} -""" % {'IMPORT_STAR' : Naming.import_star, - 'IMPORT_STAR_SET' : Naming.import_star_set } - -refnanny_utility_code = UtilityCode.load_cached("Refnanny", "ModuleSetupCode.c") - -main_method = UtilityCode.load("MainFunction", "Embed.c") +refnanny_utility_code = UtilityCode.load("Refnanny", "ModuleSetupCode.c") packed_struct_utility_code = UtilityCode(proto=""" #if defined(__GNUC__) |