from Cython.Compiler.ExprNodes import *
from Cython.Compiler.StringEncoding import EncodedString
from Cython.Compiler.Errors import CompileError
+from Cython.Utils import UtilityCode
import Interpreter
import PyrexTypes
Py_ssize_t __Pyx_zeros[] = {%s};
Py_ssize_t __Pyx_minusones[] = {%s};
""") % (", ".join(["0"] * max_ndim), ", ".join(["-1"] * max_ndim))
- env.use_utility_code([code, ""], "empty_bufstruct_code")
+ env.use_utility_code(UtilityCode(proto=code), "empty_bufstruct_code")
def buf_lookup_full_code(proto, defin, name, nd):
typestringchecker = get_typestringchecker(code, dtype)
dtype_name = str(dtype)
dtype_cname = dtype.declaration_code("")
- utilcode = [dedent("""
+ utilcode = UtilityCode(proto = dedent("""
static int %s(PyObject* obj, Py_buffer* buf, int flags, int nd, int cast); /*proto*/
- """) % name, dedent("""
+ """) % name, impl = dedent("""
static int %(name)s(PyObject* obj, Py_buffer* buf, int flags, int nd, int cast) {
const char* ts;
if (obj == Py_None) {
fail:;
__Pyx_ZeroBuffer(buf);
return -1;
- }""") % locals()]
+ }""") % locals())
code.globalstate.use_utility_code(utilcode, name)
return name
#endif
""")
- env.use_utility_code([dedent("""\
+ env.use_utility_code(UtilityCode(
+ proto = dedent("""\
#if PY_MAJOR_VERSION < 3
static int __Pyx_GetBuffer(PyObject *obj, Py_buffer *view, int flags);
static void __Pyx_ReleaseBuffer(Py_buffer *view);
#define __Pyx_GetBuffer PyObject_GetBuffer
#define __Pyx_ReleaseBuffer PyBuffer_Release
#endif
- """), code], codename)
+ """), impl = code), codename)
#
# Static utility code
# Utility function to set the right exception
# The caller should immediately goto_error
-raise_indexerror_code = [
-"""\
+raise_indexerror_code = UtilityCode(
+proto = """\
static void __Pyx_RaiseBufferIndexError(int axis); /*proto*/
-""","""\
+""",
+impl = """\
static void __Pyx_RaiseBufferIndexError(int axis) {
PyErr_Format(PyExc_IndexError,
"Out of bounds on buffer access (axis %d)", axis);
}
-"""]
+""")
#
# Buffer type checking. Utility code for checking that acquired
# the format string; the access mode/flags is checked by the
# exporter.
#
-acquire_utility_code = ["""\
+acquire_utility_code = UtilityCode(
+proto = """\
static INLINE void __Pyx_SafeReleaseBuffer(Py_buffer* info);
static INLINE void __Pyx_ZeroBuffer(Py_buffer* buf); /*proto*/
static INLINE const char* __Pyx_ConsumeWhitespace(const char* ts); /*proto*/
static void __Pyx_BufferNdimError(Py_buffer* buffer, int expected_ndim); /*proto*/
static const char* __Pyx_DescribeTokenInFormatString(const char* ts); /*proto*/
-""", """
+""",
+impl = """
static INLINE void __Pyx_SafeReleaseBuffer(Py_buffer* info) {
if (info->buf == NULL) return;
if (info->suboffsets == __Pyx_minusones) info->suboffsets = NULL;
}
}
-"""]
+""")
-parse_typestring_repeat_code = ["""
+parse_typestring_repeat_code = UtilityCode(
+proto = """
static INLINE const char* __Pyx_ParseTypestringRepeat(const char* ts, int* out_count); /*proto*/
-""","""
+""",
+impl = """
static INLINE const char* __Pyx_ParseTypestringRepeat(const char* ts, int* out_count) {
int count;
if (*ts < '0' || *ts > '9') {
*out_count = count;
return ts;
}
-"""]
+""")
-raise_buffer_fallback_code = ["""
+raise_buffer_fallback_code = UtilityCode(
+proto = """
static void __Pyx_RaiseBufferFallbackError(void); /*proto*/
-""","""
+""",
+impl = """
static void __Pyx_RaiseBufferFallbackError(void) {
PyErr_Format(PyExc_ValueError,
"Buffer acquisition failed on assignment; and then reacquiring the old buffer failed too!");
}
-"""]
-
+""")
#
from Symtab import BuiltinScope, StructOrUnionScope
+from Cython.Utils import UtilityCode
from TypeSlots import Signature
import PyrexTypes
import __builtin__
])
]
-getattr3_utility_code = ["""
+getattr3_utility_code = UtilityCode(
+proto = """
static PyObject *__Pyx_GetAttr3(PyObject *, PyObject *, PyObject *); /*proto*/
-""","""
+""",
+impl = """
static PyObject *__Pyx_GetAttr3(PyObject *o, PyObject *n, PyObject *d) {
PyObject *r = PyObject_GetAttr(o, n);
if (!r) {
bad:
return 0;
}
-"""]
+""")
-intern_utility_code = ["""
+intern_utility_code = UtilityCode(
+proto = """
#if PY_MAJOR_VERSION >= 3
# define __Pyx_InternFromString(s) PyUnicode_InternFromString(s)
#else
# define __Pyx_InternFromString(s) PyString_InternFromString(s)
#endif
-""","""
-"""]
+""")
-py23_set_utility_code = ["""
+def put_py23_set_init_utility_code(code, pos):
+ code.putln("#if PY_VERSION_HEX < 0x02040000")
+ code.putln(code.error_goto_if_neg("__Pyx_Py23SetsImport()", pos))
+ code.putln("#endif")
+
+py23_set_utility_code = UtilityCode(
+proto = """
#if PY_VERSION_HEX < 0x02050000
#ifndef PyAnySet_CheckExact
#endif /* !Py_SETOBJECT_H */
#endif /* < Py2.4 */
#endif /* < Py2.5 */
-""","""
-"""]
+""",
+init = put_py23_set_init_utility_code)
builtin_utility_code = {
'getattr3' : getattr3_utility_code,
# Utility code state
#
- def use_utility_code(self, codetup, name=None):
+ def use_utility_code(self, utility_code, name=None):
"""
Adds the given utility code to the C file if needed.
If name is provided, it is used as an identifier to avoid inserting
code twice. Otherwise, id(codetup) is used as such an identifier.
"""
- if name is None: name = id(codetup)
+ if name is None: name = id(utility_code)
if self.check_utility_code_needed_and_register(name):
- proto, _def = codetup
- self.utilprotowriter.put(proto)
- self.utildefwriter.put(_def)
+ if utility_code.proto:
+ self.utilprotowriter.put(utility_code.proto)
+ if utility_code.impl:
+ self.utildefwriter.put(utility_code.impl)
+ utility_code.write_init_code(self.initwriter, self.module_pos)
def has_code(self, name):
return name in self.used_utility_code
from Errors import error, warning, InternalError
from Errors import hold_errors, release_errors, held_errors, report_error
+from Cython.Utils import UtilityCode
import StringEncoding
import Naming
from Nodes import Node
#
#------------------------------------------------------------------------------------
-get_name_interned_utility_code = [
-"""
+get_name_interned_utility_code = UtilityCode(
+proto = """
static PyObject *__Pyx_GetName(PyObject *dict, PyObject *name); /*proto*/
-""","""
+""",
+impl = """
static PyObject *__Pyx_GetName(PyObject *dict, PyObject *name) {
PyObject *result;
result = PyObject_GetAttr(dict, name);
PyErr_SetObject(PyExc_NameError, name);
return result;
}
-"""]
+""")
#------------------------------------------------------------------------------------
-import_utility_code = [
-"""
+import_utility_code = UtilityCode(
+proto = """
static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list); /*proto*/
-""","""
+""",
+impl = """
static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list) {
PyObject *__import__ = 0;
PyObject *empty_list = 0;
""" % {
"BUILTINS": Naming.builtins_cname,
"GLOBALS": Naming.module_cname,
-}]
+})
#------------------------------------------------------------------------------------
-get_exception_utility_code = [
-"""
+get_exception_utility_code = UtilityCode(
+proto = """
static PyObject *__Pyx_GetExcValue(void); /*proto*/
-""","""
+""",
+impl = """
static PyObject *__Pyx_GetExcValue(void) {
PyObject *type = 0, *value = 0, *tb = 0;
PyObject *tmp_type, *tmp_value, *tmp_tb;
Py_XDECREF(tb);
return result;
}
-"""]
+""")
#------------------------------------------------------------------------------------
-unpacking_utility_code = [
-"""
+unpacking_utility_code = UtilityCode(
+proto = """
static PyObject *__Pyx_UnpackItem(PyObject *, Py_ssize_t index); /*proto*/
static int __Pyx_EndUnpack(PyObject *); /*proto*/
-""","""
+""",
+impl = """
static PyObject *__Pyx_UnpackItem(PyObject *iter, Py_ssize_t index) {
PyObject *item;
if (!(item = PyIter_Next(iter))) {
else
return -1;
}
-"""]
+""")
#------------------------------------------------------------------------------------
-type_test_utility_code = [
-"""
+type_test_utility_code = UtilityCode(
+proto = """
static int __Pyx_TypeTest(PyObject *obj, PyTypeObject *type); /*proto*/
-""","""
+""",
+impl = """
static int __Pyx_TypeTest(PyObject *obj, PyTypeObject *type) {
if (!type) {
PyErr_Format(PyExc_SystemError, "Missing type object");
Py_TYPE(obj)->tp_name, type->tp_name);
return 0;
}
-"""]
+""")
#------------------------------------------------------------------------------------
-create_class_utility_code = [
-"""
+create_class_utility_code = UtilityCode(
+proto = """
static PyObject *__Pyx_CreateClass(PyObject *bases, PyObject *dict, PyObject *name, char *modname); /*proto*/
-""","""
+""",
+impl = """
static PyObject *__Pyx_CreateClass(
PyObject *bases, PyObject *dict, PyObject *name, char *modname)
{
Py_XDECREF(py_modname);
return result;
}
-"""]
+""")
#------------------------------------------------------------------------------------
-cpp_exception_utility_code = [
-"""
+cpp_exception_utility_code = UtilityCode(
+proto = """
#ifndef __Pyx_CppExn2PyErr
static void __Pyx_CppExn2PyErr() {
try {
}
}
#endif
-""",""]
+""",
+impl = ""
+)
#------------------------------------------------------------------------------------
-append_utility_code = [
-"""
+append_utility_code = UtilityCode(
+proto = """
static INLINE PyObject* __Pyx_PyObject_Append(PyObject* L, PyObject* x) {
if (likely(PyList_CheckExact(L))) {
if (PyList_Append(L, x) < 0) return NULL;
return PyObject_CallMethod(L, "append", "(O)", x);
}
}
-""",""
-]
+""",
+impl = ""
+)
#------------------------------------------------------------------------------------
# If the is_unsigned flag is set, we need to do some extra work to make
# sure the index doesn't become negative.
-getitem_int_utility_code = [
-"""
+getitem_int_utility_code = UtilityCode(
+proto = """
static INLINE PyObject *__Pyx_GetItemInt(PyObject *o, Py_ssize_t i, int is_unsigned) {
PyObject *r;
if (PyList_CheckExact(o) && 0 <= i && i < PyList_GET_SIZE(o)) {
return r;
}
""",
-"""
-"""]
+impl = """
+""")
#------------------------------------------------------------------------------------
-setitem_int_utility_code = [
-"""
+setitem_int_utility_code = UtilityCode(
+proto = """
static INLINE int __Pyx_SetItemInt(PyObject *o, Py_ssize_t i, PyObject *v, int is_unsigned) {
int r;
if (PyList_CheckExact(o) && 0 <= i && i < PyList_GET_SIZE(o)) {
return r;
}
""",
-"""
-"""]
+impl = """
+""")
+
#------------------------------------------------------------------------------------
-raise_noneattr_error_utility_code = [
-"""
+raise_noneattr_error_utility_code = UtilityCode(
+proto = """
static INLINE void __Pyx_RaiseNoneAttributeError(char* attrname);
-""", """
+""",
+impl = """
static INLINE void __Pyx_RaiseNoneAttributeError(char* attrname) {
PyErr_Format(PyExc_AttributeError, "'NoneType' object has no attribute '%s'", attrname);
}
-"""]
+""")
-raise_noneindex_error_utility_code = [
-"""
+raise_noneindex_error_utility_code = UtilityCode(
+proto = """
static INLINE void __Pyx_RaiseNoneIndexingError();
-""", """
+""",
+impl = """
static INLINE void __Pyx_RaiseNoneIndexingError() {
PyErr_SetString(PyExc_TypeError, "'NoneType' object is unsubscriptable");
}
-"""]
+""")
from Errors import error, warning
from PyrexTypes import py_object_type
-from Cython.Utils import open_new_file, replace_suffix
+from Cython.Utils import open_new_file, replace_suffix, UtilityCode
from StringEncoding import escape_byte_string, EncodedString
h_code.putln("static %s;" % type.declaration_code(entry.cname))
h_code.putln("")
h_code.put_h_guard(Naming.api_func_guard + "import_module")
- h_code.put(import_module_utility_code[1])
+ h_code.put(import_module_utility_code.impl)
h_code.putln("")
h_code.putln("#endif")
if api_funcs:
h_code.putln("")
- h_code.put(function_import_utility_code[1])
+ h_code.put(function_import_utility_code.impl)
if public_extension_types:
h_code.putln("")
- h_code.put(type_import_utility_code[1])
+ h_code.put(type_import_utility_code.impl)
h_code.putln("")
h_code.putln("static int import_%s(void) {" % name)
h_code.putln("PyObject *module = 0;")
code.putln("")
code.putln("#endif")
- code.put(builtin_module_name_utility_code[0])
+ code.put(builtin_module_name_utility_code.proto)
code.putln("#if PY_MAJOR_VERSION >= 3")
code.putln(" #define Py_TPFLAGS_CHECKTYPES 0")
code.putln("/*--- Initialize various global constants etc. ---*/")
code.putln(code.error_goto_if_neg("__Pyx_InitGlobals()", self.pos))
- code.putln("#if PY_VERSION_HEX < 0x02040000")
- code.putln(code.error_goto_if_neg("__Pyx_Py23SetsImport()", self.pos))
- code.putln("#endif")
-
code.putln("/*--- Module creation code ---*/")
self.generate_module_creation_code(env, code)
#
#------------------------------------------------------------------------------------
-builtin_module_name_utility_code = [
-"""\
+builtin_module_name_utility_code = UtilityCode(
+proto = """\
#if PY_MAJOR_VERSION < 3
#define __Pyx_BUILTIN_MODULE_NAME "__builtin__"
#else
#define __Pyx_BUILTIN_MODULE_NAME "builtins"
#endif
-"""]
-
+""")
-import_module_utility_code = [
-"""
+import_module_utility_code = UtilityCode(
+proto = """
static PyObject *__Pyx_ImportModule(const char *name); /*proto*/
-""","""
+""",
+impl = """
#ifndef __PYX_HAVE_RT_ImportModule
#define __PYX_HAVE_RT_ImportModule
static PyObject *__Pyx_ImportModule(const char *name) {
return 0;
}
#endif
-"""]
+""")
#------------------------------------------------------------------------------------
-type_import_utility_code = [
-"""
+type_import_utility_code = UtilityCode(
+proto = """
static PyTypeObject *__Pyx_ImportType(const char *module_name, const char *class_name, long size); /*proto*/
-""","""
+""",
+impl = """
#ifndef __PYX_HAVE_RT_ImportType
#define __PYX_HAVE_RT_ImportType
static PyTypeObject *__Pyx_ImportType(const char *module_name, const char *class_name,
return 0;
}
#endif
-"""]
+""")
#------------------------------------------------------------------------------------
-function_export_utility_code = [
-"""
+function_export_utility_code = UtilityCode(
+proto = """
static int __Pyx_ExportFunction(char *name, void *f, char *sig); /*proto*/
-""",r"""
+""",
+impl = r"""
static int __Pyx_ExportFunction(char *name, void *f, char *sig) {
PyObject *d = 0;
PyObject *p = 0;
Py_XDECREF(d);
return -1;
}
-""" % {'MODULE': Naming.module_cname, 'API': Naming.api_name}]
+""" % {'MODULE': Naming.module_cname, 'API': Naming.api_name}
+)
#------------------------------------------------------------------------------------
-function_import_utility_code = [
-"""
+function_import_utility_code = UtilityCode(
+proto = """
static int __Pyx_ImportFunction(PyObject *module, char *funcname, void **f, char *sig); /*proto*/
-""","""
+""",
+impl = """
#ifndef __PYX_HAVE_RT_ImportFunction
#define __PYX_HAVE_RT_ImportFunction
static int __Pyx_ImportFunction(PyObject *module, char *funcname, void **f, char *sig) {
return -1;
}
#endif
-""" % dict(API = Naming.api_name)]
+""" % dict(API = Naming.api_name)
+)
-register_cleanup_utility_code = [
-"""
+register_cleanup_utility_code = UtilityCode(
+proto = """
static int __Pyx_RegisterCleanup(void); /*proto*/
static PyObject* __pyx_module_cleanup(PyObject *self, PyObject *unused); /*proto*/
static PyMethodDef cleanup_def = {"__cleanup", (PyCFunction)&__pyx_module_cleanup, METH_NOARGS, 0};
-""","""
+""",
+impl = """
static int __Pyx_RegisterCleanup(void) {
/* Don't use Py_AtExit because that has a 32-call limit
* and is called after python finalization.
Py_XDECREF(res);
return ret;
}
-"""]
+""")
import_star_utility_code = """
from PyrexTypes import py_object_type, error_type, CTypedefType, CFuncType
from Symtab import ModuleScope, LocalScope, GeneratorLocalScope, \
StructOrUnionScope, PyClassScope, CClassScope
-from Cython.Utils import open_new_file, replace_suffix
+from Cython.Utils import open_new_file, replace_suffix, UtilityCode
from StringEncoding import EncodedString, escape_byte_string, split_docstring
import Options
import ControlFlow
#------------------------------------------------------------------------------------
-printing_utility_code = [
-"""
+printing_utility_code = UtilityCode(
+proto = """
static int __Pyx_Print(PyObject *, int); /*proto*/
#if PY_MAJOR_VERSION >= 3
static PyObject* %s = 0;
static PyObject* %s = 0;
#endif
-""" % (Naming.print_function, Naming.print_function_kwargs), r"""
+""" % (Naming.print_function, Naming.print_function_kwargs),
+impl = r"""
#if PY_MAJOR_VERSION < 3
static PyObject *__Pyx_GetStdout(void) {
PyObject *f = PySys_GetObject("stdout");
""" % {'BUILTINS' : Naming.builtins_cname,
'PRINT_FUNCTION' : Naming.print_function,
'PRINT_KWARGS' : Naming.print_function_kwargs}
-]
+)
#------------------------------------------------------------------------------------
# The following function is based on do_raise() from ceval.c.
-raise_utility_code = [
-"""
+raise_utility_code = UtilityCode(
+proto = """
static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb); /*proto*/
-""","""
+""",
+impl = """
static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb) {
Py_XINCREF(type);
Py_XINCREF(value);
Py_XDECREF(tb);
return;
}
-"""]
+""")
#------------------------------------------------------------------------------------
-reraise_utility_code = [
-"""
+reraise_utility_code = UtilityCode(
+proto = """
static void __Pyx_ReRaise(void); /*proto*/
-""","""
+""",
+impl = """
static void __Pyx_ReRaise(void) {
PyThreadState *tstate = PyThreadState_Get();
PyObject *type = tstate->exc_type;
Py_XINCREF(tb);
__Pyx_ErrRestore(type, value, tb);
}
-"""]
+""")
#------------------------------------------------------------------------------------
-arg_type_test_utility_code = [
-"""
+arg_type_test_utility_code = UtilityCode(
+proto = """
static int __Pyx_ArgTypeTest(PyObject *obj, PyTypeObject *type, int none_allowed,
const char *name, int exact); /*proto*/
-""","""
+""",
+impl = """
static int __Pyx_ArgTypeTest(PyObject *obj, PyTypeObject *type, int none_allowed,
const char *name, int exact)
{
name, type->tp_name, Py_TYPE(obj)->tp_name);
return 0;
}
-"""]
+""")
#------------------------------------------------------------------------------------
#
# many or too few positional arguments were found. This handles
# Py_ssize_t formatting correctly.
-raise_argtuple_invalid_utility_code = [
-"""
+raise_argtuple_invalid_utility_code = UtilityCode(
+proto = """
static INLINE void __Pyx_RaiseArgtupleInvalid(const char* func_name, int exact,
Py_ssize_t num_min, Py_ssize_t num_max, Py_ssize_t num_found); /*proto*/
-""","""
+""",
+impl = """
static INLINE void __Pyx_RaiseArgtupleInvalid(
const char* func_name,
int exact,
#endif
func_name, more_or_less, num_expected, number, num_found);
}
-"""]
+""")
-raise_keyword_required_utility_code = [
-"""
+raise_keyword_required_utility_code = UtilityCode(
+proto = """
static INLINE void __Pyx_RaiseKeywordRequired(const char* func_name, PyObject* kw_name); /*proto*/
-""","""
+""",
+impl = """
static INLINE void __Pyx_RaiseKeywordRequired(
const char* func_name,
PyObject* kw_name)
PyString_AS_STRING(kw_name));
#endif
}
-"""]
+""")
-raise_double_keywords_utility_code = [
-"""
+raise_double_keywords_utility_code = UtilityCode(
+proto = """
static INLINE void __Pyx_RaiseDoubleKeywordsError(
const char* func_name, PyObject* kw_name); /*proto*/
-""","""
+""",
+impl = """
static INLINE void __Pyx_RaiseDoubleKeywordsError(
const char* func_name,
PyObject* kw_name)
PyString_AS_STRING(kw_name));
#endif
}
-"""]
+""")
#------------------------------------------------------------------------------------
#
# were passed to a function, or if any keywords were passed to a
# function that does not accept them.
-keyword_string_check_utility_code = [
-"""
+keyword_string_check_utility_code = UtilityCode(
+proto = """
static INLINE int __Pyx_CheckKeywordStrings(PyObject *kwdict,
const char* function_name, int kw_allowed); /*proto*/
-""","""
+""",
+impl = """
static INLINE int __Pyx_CheckKeywordStrings(
PyObject *kwdict,
const char* function_name,
#endif
return 0;
}
-"""]
+""")
#------------------------------------------------------------------------------------
#
# This method does not check for required keyword arguments.
#
-split_keywords_utility_code = [
-"""
+split_keywords_utility_code = UtilityCode(
+proto = """
static int __Pyx_SplitKeywords(PyObject *kwds, PyObject **argnames[], \
PyObject *kwds2, Py_ssize_t num_pos_args, const char* function_name); /*proto*/
-""","""
+""",
+impl = """
static int __Pyx_SplitKeywords(
PyObject *kwds,
PyObject **argnames[],
bad:
return -1;
}
-"""]
+""")
#------------------------------------------------------------------------------------
-unraisable_exception_utility_code = [
-"""
+unraisable_exception_utility_code = UtilityCode(
+proto = """
static void __Pyx_WriteUnraisable(const char *name); /*proto*/
-""","""
+""",
+impl = """
static void __Pyx_WriteUnraisable(const char *name) {
PyObject *old_exc, *old_val, *old_tb;
PyObject *ctx;
Py_DECREF(ctx);
}
}
-"""]
+""")
#------------------------------------------------------------------------------------
-traceback_utility_code = [
-"""
+traceback_utility_code = UtilityCode(
+proto = """
static void __Pyx_AddTraceback(const char *funcname); /*proto*/
-""","""
+""",
+impl = """
#include "compile.h"
#include "frameobject.h"
#include "traceback.h"
'CLINENO': Naming.clineno_cname,
'GLOBALS': Naming.module_cname,
'EMPTY_TUPLE' : Naming.empty_tuple,
-}]
+})
-restore_exception_utility_code = [
-"""
+restore_exception_utility_code = UtilityCode(
+proto = """
static INLINE void __Pyx_ErrRestore(PyObject *type, PyObject *value, PyObject *tb); /*proto*/
static INLINE void __Pyx_ErrFetch(PyObject **type, PyObject **value, PyObject **tb); /*proto*/
-""","""
+""",
+impl = """
static INLINE void __Pyx_ErrRestore(PyObject *type, PyObject *value, PyObject *tb) {
PyObject *tmp_type, *tmp_value, *tmp_tb;
PyThreadState *tstate = PyThreadState_GET();
tstate->curexc_traceback = 0;
}
-"""]
+""")
#------------------------------------------------------------------------------------
-set_vtable_utility_code = [
-"""
+set_vtable_utility_code = UtilityCode(
+proto = """
static int __Pyx_SetVtable(PyObject *dict, void *vtable); /*proto*/
-""","""
+""",
+impl = """
static int __Pyx_SetVtable(PyObject *dict, void *vtable) {
PyObject *pycobj = 0;
int result;
Py_XDECREF(pycobj);
return result;
}
-"""]
+""")
#------------------------------------------------------------------------------------
-get_vtable_utility_code = [
-"""
+get_vtable_utility_code = UtilityCode(
+proto = """
static int __Pyx_GetVtable(PyObject *dict, void *vtabptr); /*proto*/
-""",r"""
+""",
+impl = r"""
static int __Pyx_GetVtable(PyObject *dict, void *vtabptr) {
int result;
PyObject *pycobj;
Py_XDECREF(pycobj);
return result;
}
-"""]
+""")
#------------------------------------------------------------------------------------
-init_string_tab_utility_code = [
-"""
+init_string_tab_utility_code = UtilityCode(
+proto = """
static int __Pyx_InitStrings(__Pyx_StringTabEntry *t); /*proto*/
-""","""
+""",
+impl = """
static int __Pyx_InitStrings(__Pyx_StringTabEntry *t) {
while (t->p) {
#if PY_MAJOR_VERSION < 3
}
return 0;
}
-"""]
+""")
#------------------------------------------------------------------------------------
-get_exception_utility_code = [
-"""
+get_exception_utility_code = UtilityCode(
+proto = """
static int __Pyx_GetException(PyObject **type, PyObject **value, PyObject **tb); /*proto*/
-""","""
+""",
+impl = """
static int __Pyx_GetException(PyObject **type, PyObject **value, PyObject **tb) {
PyObject *tmp_type, *tmp_value, *tmp_tb;
PyThreadState *tstate = PyThreadState_GET();
return -1;
}
-"""]
+""")
#------------------------------------------------------------------------------------
-reset_exception_utility_code = [
-"""
+reset_exception_utility_code = UtilityCode(
+proto = """
static INLINE void __Pyx_ExceptionSave(PyObject **type, PyObject **value, PyObject **tb); /*proto*/
static void __Pyx_ExceptionReset(PyObject *type, PyObject *value, PyObject *tb); /*proto*/
-""","""
+""",
+impl = """
static INLINE void __Pyx_ExceptionSave(PyObject **type, PyObject **value, PyObject **tb) {
PyThreadState *tstate = PyThreadState_GET();
*type = tstate->exc_type;
Py_XDECREF(tmp_value);
Py_XDECREF(tmp_tb);
}
-"""]
+""")
#------------------------------------------------------------------------------------
else:
value = int(value)
return not -2**31 <= value < 2**31
+
+# a simple class that simplifies the usage of utility code
+
+class UtilityCode(object):
+ def __init__(self, proto=None, impl=None, init=None, cleanup=None):
+ self.proto = proto
+ self.impl = impl
+ self.init = init
+ self.cleanup = cleanup
+
+ def write_init_code(self, writer, pos):
+ if not self.init:
+ return
+ if callable(self.init):
+ self.init(writer, pos)
+ else:
+ writer.put(self.init)