2 # Pyrex - Module parse tree node
7 cython.declare(Naming=object, Options=object, PyrexTypes=object, TypeSlots=object,
8 error=object, warning=object, py_object_type=object, UtilityCode=object,
9 escape_byte_string=object, EncodedString=object)
12 from PyrexTypes import CPtrType
25 from Errors import error, warning
26 from PyrexTypes import py_object_type
27 from Cython.Utils import open_new_file, replace_suffix
28 from Code import UtilityCode
29 from StringEncoding import escape_byte_string, EncodedString
32 def check_c_declarations_pxd(module_node):
33 module_node.scope.check_c_classes_pxd()
36 def check_c_declarations(module_node):
37 module_node.scope.check_c_classes()
38 module_node.scope.check_c_functions()
41 class ModuleNode(Nodes.Node, Nodes.BlockNode):
45 # referenced_modules [ModuleScope]
46 # full_module_name string
48 # scope The module scope.
49 # compilation_source A CompilationSource (see Main)
50 # directives Top-level compiler directives
52 child_attrs = ["body"]
55 def analyse_declarations(self, env):
56 if Options.embed_pos_in_docstring:
57 env.doc = EncodedString(u'File: %s (starting at line %s)' % Nodes.relative_position(self.pos))
58 if not self.doc is None:
59 env.doc = EncodedString(env.doc + u'\n' + self.doc)
60 env.doc.encoding = self.doc.encoding
63 env.directives = self.directives
64 self.body.analyse_declarations(env)
66 def process_implementation(self, options, result):
68 env.return_type = PyrexTypes.c_void_type
69 self.referenced_modules = []
70 self.find_referenced_modules(env, self.referenced_modules, {})
72 self.generate_dep_file(env, result)
73 self.generate_c_code(env, options, result)
74 self.generate_h_code(env, options, result)
75 self.generate_api_code(env, result)
77 def has_imported_c_functions(self):
78 for module in self.referenced_modules:
79 for entry in module.cfunc_entries:
80 if entry.defined_in_pxd:
84 def generate_dep_file(self, env, result):
85 modules = self.referenced_modules
86 if len(modules) > 1 or env.included_files:
87 dep_file = replace_suffix(result.c_file, ".dep")
88 f = open(dep_file, "w")
90 for module in modules:
92 f.write("cimport %s\n" % module.qualified_name)
93 for path in module.included_files:
94 f.write("include %s\n" % path)
98 def generate_h_code(self, env, options, result):
99 def h_entries(entries, pxd = 0):
100 return [entry for entry in entries
101 if entry.visibility == 'public' or pxd and entry.defined_in_pxd]
102 h_types = h_entries(env.type_entries)
103 h_vars = h_entries(env.var_entries)
104 h_funcs = h_entries(env.cfunc_entries)
105 h_extension_types = h_entries(env.c_class_entries)
106 if h_types or h_vars or h_funcs or h_extension_types:
107 result.h_file = replace_suffix(result.c_file, ".h")
108 h_code = Code.CCodeWriter()
109 Code.GlobalState(h_code)
110 if options.generate_pxi:
111 result.i_file = replace_suffix(result.c_file, ".pxi")
112 i_code = Code.PyrexCodeWriter(result.i_file)
115 guard = Naming.h_guard_prefix + env.qualified_name.replace(".", "__")
116 h_code.put_h_guard(guard)
117 self.generate_extern_c_macro_definition(h_code)
118 self.generate_type_header_code(h_types, h_code)
120 h_code.putln("#ifndef %s" % Naming.api_guard_prefix + self.api_name(env))
124 self.generate_public_declaration(entry, h_code, i_code)
127 for entry in h_funcs:
128 self.generate_public_declaration(entry, h_code, i_code)
129 if h_extension_types:
131 for entry in h_extension_types:
132 self.generate_cclass_header_code(entry.type, h_code)
134 self.generate_cclass_include_code(entry.type, i_code)
136 h_code.putln("#endif")
138 h_code.putln("PyMODINIT_FUNC init%s(void);" % env.module_name)
140 h_code.putln("#endif")
142 f = open_new_file(result.h_file)
148 def generate_public_declaration(self, entry, h_code, i_code):
149 h_code.putln("%s %s;" % (
150 Naming.extern_c_macro,
151 entry.type.declaration_code(
152 entry.cname, dll_linkage = "DL_IMPORT")))
154 i_code.putln("cdef extern %s" %
155 entry.type.declaration_code(entry.cname, pyrex = 1))
157 def api_name(self, env):
158 return env.qualified_name.replace(".", "__")
160 def generate_api_code(self, env, result):
162 public_extension_types = []
163 has_api_extension_types = 0
164 for entry in env.cfunc_entries:
166 api_funcs.append(entry)
167 for entry in env.c_class_entries:
168 if entry.visibility == 'public':
169 public_extension_types.append(entry)
171 has_api_extension_types = 1
172 if api_funcs or has_api_extension_types:
173 result.api_file = replace_suffix(result.c_file, "_api.h")
174 h_code = Code.CCodeWriter()
175 Code.GlobalState(h_code)
176 name = self.api_name(env)
177 guard = Naming.api_guard_prefix + name
178 h_code.put_h_guard(guard)
179 h_code.putln('#include "Python.h"')
181 h_code.putln('#include "%s"' % os.path.basename(result.h_file))
182 for entry in public_extension_types:
185 h_code.putln("static PyTypeObject *%s;" % type.typeptr_cname)
186 h_code.putln("#define %s (*%s)" % (
187 type.typeobj_cname, type.typeptr_cname))
190 for entry in api_funcs:
191 type = CPtrType(entry.type)
192 h_code.putln("static %s;" % type.declaration_code(entry.cname))
194 h_code.put_h_guard(Naming.api_func_guard + "import_module")
195 h_code.put(import_module_utility_code.impl)
197 h_code.putln("#endif")
200 h_code.put(function_import_utility_code.impl)
201 if public_extension_types:
203 h_code.put(type_import_utility_code.impl)
205 h_code.putln("static int import_%s(void) {" % name)
206 h_code.putln("PyObject *module = 0;")
207 h_code.putln('module = __Pyx_ImportModule("%s");' % env.qualified_name)
208 h_code.putln("if (!module) goto bad;")
209 for entry in api_funcs:
210 sig = entry.type.signature_string()
212 'if (__Pyx_ImportFunction(module, "%s", (void (**)(void))&%s, "%s") < 0) goto bad;' % (
216 h_code.putln("Py_DECREF(module); module = 0;")
217 for entry in public_extension_types:
218 self.generate_type_import_call(
220 "if (!%s) goto bad;" % entry.type.typeptr_cname)
221 h_code.putln("return 0;")
223 h_code.putln("Py_XDECREF(module);")
224 h_code.putln("return -1;")
227 h_code.putln("#endif")
229 f = open_new_file(result.api_file)
235 def generate_cclass_header_code(self, type, h_code):
236 h_code.putln("%s DL_IMPORT(PyTypeObject) %s;" % (
237 Naming.extern_c_macro,
240 def generate_cclass_include_code(self, type, i_code):
241 i_code.putln("cdef extern class %s.%s:" % (
242 type.module_name, type.name))
244 var_entries = type.scope.var_entries
246 for entry in var_entries:
247 i_code.putln("cdef %s" %
248 entry.type.declaration_code(entry.cname, pyrex = 1))
253 def generate_c_code(self, env, options, result):
254 modules = self.referenced_modules
256 if Options.annotate or options.annotate:
257 emit_linenums = False
258 rootwriter = Annotate.AnnotationCCodeWriter()
260 emit_linenums = options.emit_linenums
261 rootwriter = Code.CCodeWriter(emit_linenums=emit_linenums)
262 globalstate = Code.GlobalState(rootwriter, emit_linenums)
263 globalstate.initialize_main_c_code()
264 h_code = globalstate['h_code']
266 self.generate_module_preamble(env, modules, h_code)
268 globalstate.module_pos = self.pos
269 globalstate.directives = self.directives
271 globalstate.use_utility_code(refnanny_utility_code)
273 code = globalstate['before_global_var']
274 code.putln('#define __Pyx_MODULE_NAME "%s"' % self.full_module_name)
275 code.putln("static int %s%s = 0;" % (Naming.module_is_main, self.full_module_name.replace('.', '__')))
277 code.putln("/* Implementation of %s */" % env.qualified_name)
279 code = globalstate['all_the_rest']
281 self.generate_cached_builtins_decls(env, code)
282 self.generate_lambda_definitions(env, code)
283 # generate normal function definitions
284 self.body.generate_function_definitions(env, code)
286 self.generate_typeobj_definitions(env, code)
287 self.generate_method_table(env, code)
288 if env.has_import_star:
289 self.generate_import_star(env, code)
290 self.generate_pymoduledef_struct(env, code)
292 # init_globals is inserted before this
293 self.generate_module_init_func(modules[:-1], env, globalstate['init_module'])
294 self.generate_module_cleanup_func(env, globalstate['cleanup_module'])
296 self.generate_main_method(env, globalstate['main_method'])
297 self.generate_filename_table(globalstate['filename_table'])
299 self.generate_declarations_for_modules(env, modules, globalstate)
302 for utilcode in env.utility_code_list:
303 globalstate.use_utility_code(utilcode)
304 globalstate.finalize_main_c_code()
306 f = open_new_file(result.c_file)
308 if options.gdb_debug:
309 self._serialize_lineno_map(env, rootwriter)
311 result.c_file_generated = 1
312 if Options.annotate or options.annotate:
313 self.annotate(rootwriter)
314 rootwriter.save_annotation(result.main_source_file, result.c_file)
316 def _serialize_lineno_map(self, env, ccodewriter):
317 tb = env.context.gdb_debug_outputwriter
318 markers = ccodewriter.buffer.allmarkers()
321 for c_lineno, cython_lineno in enumerate(markers):
322 if cython_lineno > 0:
323 d.setdefault(cython_lineno, []).append(c_lineno + 1)
325 tb.start('LineNumberMapping')
326 for cython_lineno, c_linenos in sorted(d.iteritems()):
328 'c_linenos': ' '.join(map(str, c_linenos)),
329 'cython_lineno': str(cython_lineno),
331 tb.start('LineNumber', attrs)
333 tb.end('LineNumberMapping')
336 def find_referenced_modules(self, env, module_list, modules_seen):
337 if env not in modules_seen:
338 modules_seen[env] = 1
339 for imported_module in env.cimported_modules:
340 self.find_referenced_modules(imported_module, module_list, modules_seen)
341 module_list.append(env)
343 def sort_types_by_inheritance(self, type_dict, getkey):
344 # copy the types into a list moving each parent type before
346 type_items = type_dict.items()
348 for i, item in enumerate(type_items):
349 key, new_entry = item
351 # collect all base classes to check for children
355 base_type = base.type.base_type
358 base_key = getkey(base_type)
359 hierarchy.add(base_key)
360 base = type_dict.get(base_key)
361 new_entry.base_keys = hierarchy
363 # find the first (sub-)subclass and insert before that
366 if key in entry.base_keys:
367 type_list.insert(j, new_entry)
370 type_list.append(new_entry)
373 def sort_type_hierarchy(self, module_list, env):
376 for module in module_list:
377 for entry in module.c_class_entries:
378 if not entry.in_cinclude:
380 if type.vtabstruct_cname:
381 vtab_dict[type.vtabstruct_cname] = entry
382 all_defined_here = module is env
383 for entry in module.type_entries:
384 if all_defined_here or entry.defined_in_pxd:
386 if type.is_extension_type and not entry.in_cinclude:
388 vtabslot_dict[type.objstruct_cname] = entry
390 def vtabstruct_cname(entry_type):
391 return entry_type.vtabstruct_cname
392 vtab_list = self.sort_types_by_inheritance(
393 vtab_dict, vtabstruct_cname)
395 def objstruct_cname(entry_type):
396 return entry_type.objstruct_cname
397 vtabslot_list = self.sort_types_by_inheritance(
398 vtabslot_dict, objstruct_cname)
400 return (vtab_list, vtabslot_list)
402 def generate_type_definitions(self, env, modules, vtab_list, vtabslot_list, code):
403 vtabslot_entries = set(vtabslot_list)
404 for module in modules:
405 definition = module is env
407 type_entries = module.type_entries
410 for entry in module.type_entries:
411 if entry.defined_in_pxd:
412 type_entries.append(entry)
413 for entry in type_entries:
414 if not entry.in_cinclude:
415 #print "generate_type_header_code:", entry.name, repr(entry.type) ###
417 if type.is_typedef: # Must test this first!
418 self.generate_typedef(entry, code)
419 elif type.is_struct_or_union:
420 self.generate_struct_union_definition(entry, code)
422 self.generate_enum_definition(entry, code)
423 elif type.is_extension_type and entry not in vtabslot_entries:
424 self.generate_objstruct_definition(type, code)
425 for entry in vtabslot_list:
426 self.generate_objstruct_definition(entry.type, code)
427 for entry in vtab_list:
428 self.generate_typeobject_predeclaration(entry, code)
429 self.generate_exttype_vtable_struct(entry, code)
430 self.generate_exttype_vtabptr_declaration(entry, code)
432 def generate_declarations_for_modules(self, env, modules, globalstate):
433 typecode = globalstate['type_declarations']
435 typecode.putln("/* Type declarations */")
436 vtab_list, vtabslot_list = self.sort_type_hierarchy(modules, env)
437 self.generate_type_definitions(
438 env, modules, vtab_list, vtabslot_list, typecode)
439 modulecode = globalstate['module_declarations']
440 for module in modules:
441 defined_here = module is env
442 modulecode.putln("/* Module declarations from %s */" %
443 module.qualified_name)
444 self.generate_global_declarations(module, modulecode, defined_here)
445 self.generate_cfunction_predeclarations(module, modulecode, defined_here)
447 def generate_module_preamble(self, env, cimported_modules, code):
448 code.putln("/* Generated by Cython %s on %s */" % (
449 Version.version, time.asctime()))
451 code.putln("#define PY_SSIZE_T_CLEAN")
452 for filename in env.python_include_files:
453 code.putln('#include "%s"' % filename)
454 code.putln("#ifndef Py_PYTHON_H")
455 code.putln(" #error Python headers needed to compile C extensions, please install development version of Python.")
457 code.globalstate["end"].putln("#endif /* Py_PYTHON_H */")
460 #include <stddef.h> /* For offsetof */
462 #define offsetof(type, member) ( (size_t) & ((type*)0) -> member )
465 #if !defined(WIN32) && !defined(MS_WINDOWS)
478 #define DL_IMPORT(t) t
481 #define DL_EXPORT(t) t
485 #define PY_LONG_LONG LONG_LONG
488 #if PY_VERSION_HEX < 0x02040000
489 #define METH_COEXIST 0
490 #define PyDict_CheckExact(op) (Py_TYPE(op) == &PyDict_Type)
491 #define PyDict_Contains(d,o) PySequence_Contains(d,o)
494 #if PY_VERSION_HEX < 0x02050000
495 typedef int Py_ssize_t;
496 #define PY_SSIZE_T_MAX INT_MAX
497 #define PY_SSIZE_T_MIN INT_MIN
498 #define PY_FORMAT_SIZE_T \"\"
499 #define PyInt_FromSsize_t(z) PyInt_FromLong(z)
500 #define PyInt_AsSsize_t(o) PyInt_AsLong(o)
501 #define PyNumber_Index(o) PyNumber_Int(o)
502 #define PyIndex_Check(o) PyNumber_Check(o)
503 #define PyErr_WarnEx(category, message, stacklevel) PyErr_Warn(category, message)
506 #if PY_VERSION_HEX < 0x02060000
507 #define Py_REFCNT(ob) (((PyObject*)(ob))->ob_refcnt)
508 #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
509 #define Py_SIZE(ob) (((PyVarObject*)(ob))->ob_size)
510 #define PyVarObject_HEAD_INIT(type, size) \\
511 PyObject_HEAD_INIT(type) size,
512 #define PyType_Modified(t)
524 Py_ssize_t *suboffsets;
528 #define PyBUF_SIMPLE 0
529 #define PyBUF_WRITABLE 0x0001
530 #define PyBUF_FORMAT 0x0004
531 #define PyBUF_ND 0x0008
532 #define PyBUF_STRIDES (0x0010 | PyBUF_ND)
533 #define PyBUF_C_CONTIGUOUS (0x0020 | PyBUF_STRIDES)
534 #define PyBUF_F_CONTIGUOUS (0x0040 | PyBUF_STRIDES)
535 #define PyBUF_ANY_CONTIGUOUS (0x0080 | PyBUF_STRIDES)
536 #define PyBUF_INDIRECT (0x0100 | PyBUF_STRIDES)
540 #if PY_MAJOR_VERSION < 3
541 #define __Pyx_BUILTIN_MODULE_NAME "__builtin__"
543 #define __Pyx_BUILTIN_MODULE_NAME "builtins"
546 #if PY_MAJOR_VERSION >= 3
547 #define Py_TPFLAGS_CHECKTYPES 0
548 #define Py_TPFLAGS_HAVE_INDEX 0
551 #if (PY_VERSION_HEX < 0x02060000) || (PY_MAJOR_VERSION >= 3)
552 #define Py_TPFLAGS_HAVE_NEWBUFFER 0
555 #if PY_MAJOR_VERSION >= 3
556 #define PyBaseString_Type PyUnicode_Type
557 #define PyStringObject PyUnicodeObject
558 #define PyString_Type PyUnicode_Type
559 #define PyString_Check PyUnicode_Check
560 #define PyString_CheckExact PyUnicode_CheckExact
563 #if PY_VERSION_HEX < 0x02060000
564 #define PyBytesObject PyStringObject
565 #define PyBytes_Type PyString_Type
566 #define PyBytes_Check PyString_Check
567 #define PyBytes_CheckExact PyString_CheckExact
568 #define PyBytes_FromString PyString_FromString
569 #define PyBytes_FromStringAndSize PyString_FromStringAndSize
570 #define PyBytes_FromFormat PyString_FromFormat
571 #define PyBytes_DecodeEscape PyString_DecodeEscape
572 #define PyBytes_AsString PyString_AsString
573 #define PyBytes_AsStringAndSize PyString_AsStringAndSize
574 #define PyBytes_Size PyString_Size
575 #define PyBytes_AS_STRING PyString_AS_STRING
576 #define PyBytes_GET_SIZE PyString_GET_SIZE
577 #define PyBytes_Repr PyString_Repr
578 #define PyBytes_Concat PyString_Concat
579 #define PyBytes_ConcatAndDel PyString_ConcatAndDel
582 #if PY_VERSION_HEX < 0x02060000
583 #define PySet_Check(obj) PyObject_TypeCheck(obj, &PySet_Type)
584 #define PyFrozenSet_Check(obj) PyObject_TypeCheck(obj, &PyFrozenSet_Type)
586 #ifndef PySet_CheckExact
587 #define PySet_CheckExact(obj) (Py_TYPE(obj) == &PySet_Type)
590 #define __Pyx_TypeCheck(obj, type) PyObject_TypeCheck(obj, (PyTypeObject *)type)
592 #if PY_MAJOR_VERSION >= 3
593 #define PyIntObject PyLongObject
594 #define PyInt_Type PyLong_Type
595 #define PyInt_Check(op) PyLong_Check(op)
596 #define PyInt_CheckExact(op) PyLong_CheckExact(op)
597 #define PyInt_FromString PyLong_FromString
598 #define PyInt_FromUnicode PyLong_FromUnicode
599 #define PyInt_FromLong PyLong_FromLong
600 #define PyInt_FromSize_t PyLong_FromSize_t
601 #define PyInt_FromSsize_t PyLong_FromSsize_t
602 #define PyInt_AsLong PyLong_AsLong
603 #define PyInt_AS_LONG PyLong_AS_LONG
604 #define PyInt_AsSsize_t PyLong_AsSsize_t
605 #define PyInt_AsUnsignedLongMask PyLong_AsUnsignedLongMask
606 #define PyInt_AsUnsignedLongLongMask PyLong_AsUnsignedLongLongMask
609 #if PY_MAJOR_VERSION >= 3
610 #define PyBoolObject PyLongObject
616 #if PY_MAJOR_VERSION >= 3
617 #define __Pyx_PyNumber_Divide(x,y) PyNumber_TrueDivide(x,y)
618 #define __Pyx_PyNumber_InPlaceDivide(x,y) PyNumber_InPlaceTrueDivide(x,y)
621 if Future.division in env.context.future_directives:
622 code.putln(" #define __Pyx_PyNumber_Divide(x,y) PyNumber_TrueDivide(x,y)")
623 code.putln(" #define __Pyx_PyNumber_InPlaceDivide(x,y) PyNumber_InPlaceTrueDivide(x,y)")
625 code.putln(" #define __Pyx_PyNumber_Divide(x,y) PyNumber_Divide(x,y)")
626 code.putln(" #define __Pyx_PyNumber_InPlaceDivide(x,y) PyNumber_InPlaceDivide(x,y)")
630 #if (PY_MAJOR_VERSION < 3) || (PY_VERSION_HEX >= 0x03010300)
631 #define __Pyx_PySequence_GetSlice(obj, a, b) PySequence_GetSlice(obj, a, b)
632 #define __Pyx_PySequence_SetSlice(obj, a, b, value) PySequence_SetSlice(obj, a, b, value)
633 #define __Pyx_PySequence_DelSlice(obj, a, b) PySequence_DelSlice(obj, a, b)
635 #define __Pyx_PySequence_GetSlice(obj, a, b) (unlikely(!(obj)) ? \\
636 (PyErr_SetString(PyExc_SystemError, "null argument to internal routine"), (PyObject*)0) : \\
637 (likely((obj)->ob_type->tp_as_mapping) ? (PySequence_GetSlice(obj, a, b)) : \\
638 (PyErr_Format(PyExc_TypeError, "'%.200s' object is unsliceable", (obj)->ob_type->tp_name), (PyObject*)0)))
639 #define __Pyx_PySequence_SetSlice(obj, a, b, value) (unlikely(!(obj)) ? \\
640 (PyErr_SetString(PyExc_SystemError, "null argument to internal routine"), -1) : \\
641 (likely((obj)->ob_type->tp_as_mapping) ? (PySequence_SetSlice(obj, a, b, value)) : \\
642 (PyErr_Format(PyExc_TypeError, "'%.200s' object doesn't support slice assignment", (obj)->ob_type->tp_name), -1)))
643 #define __Pyx_PySequence_DelSlice(obj, a, b) (unlikely(!(obj)) ? \\
644 (PyErr_SetString(PyExc_SystemError, "null argument to internal routine"), -1) : \\
645 (likely((obj)->ob_type->tp_as_mapping) ? (PySequence_DelSlice(obj, a, b)) : \\
646 (PyErr_Format(PyExc_TypeError, "'%.200s' object doesn't support slice deletion", (obj)->ob_type->tp_name), -1)))
649 #if PY_MAJOR_VERSION >= 3
650 #define PyMethod_New(func, self, klass) ((self) ? PyMethod_New(func, self) : PyInstanceMethod_New(func))
653 #if PY_VERSION_HEX < 0x02050000
654 #define __Pyx_GetAttrString(o,n) PyObject_GetAttrString((o),((char *)(n)))
655 #define __Pyx_SetAttrString(o,n,a) PyObject_SetAttrString((o),((char *)(n)),(a))
656 #define __Pyx_DelAttrString(o,n) PyObject_DelAttrString((o),((char *)(n)))
658 #define __Pyx_GetAttrString(o,n) PyObject_GetAttrString((o),(n))
659 #define __Pyx_SetAttrString(o,n,a) PyObject_SetAttrString((o),(n),(a))
660 #define __Pyx_DelAttrString(o,n) PyObject_DelAttrString((o),(n))
663 #if PY_VERSION_HEX < 0x02050000
664 #define __Pyx_NAMESTR(n) ((char *)(n))
665 #define __Pyx_DOCSTR(n) ((char *)(n))
667 #define __Pyx_NAMESTR(n) (n)
668 #define __Pyx_DOCSTR(n) (n)
673 self.generate_extern_c_macro_definition(code)
675 code.putln("#if defined(WIN32) || defined(MS_WINDOWS)")
676 code.putln("#define _USE_MATH_DEFINES")
678 code.putln("#include <math.h>")
679 code.putln("#define %s" % Naming.api_guard_prefix + self.api_name(env))
680 self.generate_includes(env, cimported_modules, code)
682 code.putln("#ifdef PYREX_WITHOUT_ASSERTIONS")
683 code.putln("#define CYTHON_WITHOUT_ASSERTIONS")
686 if env.directives['ccomplex']:
688 code.putln("#if !defined(CYTHON_CCOMPLEX)")
689 code.putln("#define CYTHON_CCOMPLEX 1")
692 code.put(Nodes.utility_function_predeclarations)
693 code.put(PyrexTypes.type_conversion_predeclarations)
694 code.put(Nodes.branch_prediction_macros)
696 code.putln('static PyObject *%s;' % env.module_cname)
697 code.putln('static PyObject *%s;' % Naming.builtins_cname)
698 code.putln('static PyObject *%s;' % Naming.empty_tuple)
699 code.putln('static PyObject *%s;' % Naming.empty_bytes)
700 if Options.pre_import is not None:
701 code.putln('static PyObject *%s;' % Naming.preimport_cname)
702 code.putln('static int %s;' % Naming.lineno_cname)
703 code.putln('static int %s = 0;' % Naming.clineno_cname)
704 code.putln('static const char * %s= %s;' % (Naming.cfilenm_cname, Naming.file_c_macro))
705 code.putln('static const char *%s;' % Naming.filename_cname)
708 for utility_code in PyrexTypes.c_int_from_py_function.specialize_list:
709 env.use_utility_code(utility_code)
710 for utility_code in PyrexTypes.c_long_from_py_function.specialize_list:
711 env.use_utility_code(utility_code)
713 def generate_extern_c_macro_definition(self, code):
714 name = Naming.extern_c_macro
715 code.putln("#ifdef __cplusplus")
716 code.putln('#define %s extern "C"' % name)
718 code.putln("#define %s extern" % name)
721 def generate_includes(self, env, cimported_modules, code):
723 for filename in env.include_files:
724 byte_decoded_filenname = str(filename)
725 if byte_decoded_filenname[0] == '<' and byte_decoded_filenname[-1] == '>':
726 code.putln('#include %s' % byte_decoded_filenname)
728 code.putln('#include "%s"' % byte_decoded_filenname)
730 def generate_filename_table(self, code):
732 code.putln("static const char *%s[] = {" % Naming.filetable_cname)
733 if code.globalstate.filename_list:
734 for source_desc in code.globalstate.filename_list:
735 filename = os.path.basename(source_desc.get_filenametable_entry())
736 escaped_filename = filename.replace("\\", "\\\\").replace('"', r'\"')
737 code.putln('"%s",' % escaped_filename)
739 # Some C compilers don't like an empty array
743 def generate_type_predeclarations(self, env, code):
746 def generate_type_header_code(self, type_entries, code):
747 # Generate definitions of structs/unions/enums/typedefs/objstructs.
748 #self.generate_gcc33_hack(env, code) # Is this still needed?
749 #for entry in env.type_entries:
750 for entry in type_entries:
751 if not entry.in_cinclude:
752 #print "generate_type_header_code:", entry.name, repr(entry.type) ###
754 if type.is_typedef: # Must test this first!
755 self.generate_typedef(entry, code)
756 elif type.is_struct_or_union:
757 self.generate_struct_union_definition(entry, code)
759 self.generate_enum_definition(entry, code)
760 elif type.is_extension_type:
761 self.generate_objstruct_definition(type, code)
763 def generate_gcc33_hack(self, env, code):
764 # Workaround for spurious warning generation in gcc 3.3
766 for entry in env.c_class_entries:
768 if not type.typedef_flag:
769 name = type.objstruct_cname
770 if name.startswith("__pyx_"):
774 code.putln("typedef struct %s __pyx_gcc33_%s;" % (
777 def generate_typedef(self, entry, code):
778 base_type = entry.type.typedef_base_type
779 if base_type.is_numeric:
780 writer = code.globalstate['numeric_typedefs']
784 writer.putln("typedef %s;" % base_type.declaration_code(entry.cname))
786 def sue_header_footer(self, type, kind, name):
787 if type.typedef_flag:
788 header = "typedef %s {" % kind
789 footer = "} %s;" % name
791 header = "%s %s {" % (kind, name)
793 return header, footer
795 def generate_struct_union_definition(self, entry, code):
796 code.mark_pos(entry.pos)
801 packed = type.is_struct and type.packed
803 kind = "%s %s" % (type.kind, "__Pyx_PACKED")
804 code.globalstate.use_utility_code(packed_struct_utility_code)
806 self.sue_header_footer(type, kind, type.cname)
809 code.putln("#if defined(__SUNPRO_C)")
810 code.putln(" #pragma pack(1)")
811 code.putln("#elif !defined(__GNUC__)")
812 code.putln(" #pragma pack(push, 1)")
815 var_entries = scope.var_entries
818 "Empty struct or union definition not allowed outside a"
819 " 'cdef extern from' block")
820 for attr in var_entries:
823 attr.type.declaration_code(attr.cname))
826 code.putln("#if defined(__SUNPRO_C)")
827 code.putln(" #pragma pack()")
828 code.putln("#elif !defined(__GNUC__)")
829 code.putln(" #pragma pack(pop)")
832 def generate_enum_definition(self, entry, code):
833 code.mark_pos(entry.pos)
835 name = entry.cname or entry.name or ""
837 self.sue_header_footer(type, "enum", name)
840 enum_values = entry.enum_values
843 "Empty enum definition not allowed outside a"
844 " 'cdef extern from' block")
846 last_entry = enum_values[-1]
847 # this does not really generate code, just builds the result value
848 for value_entry in enum_values:
849 if value_entry.value_node is not None:
850 value_entry.value_node.generate_evaluation_code(code)
852 for value_entry in enum_values:
853 if value_entry.value_node is None:
854 value_code = value_entry.cname
856 value_code = ("%s = %s" % (
858 value_entry.value_node.result()))
859 if value_entry is not last_entry:
861 code.putln(value_code)
864 def generate_typeobject_predeclaration(self, entry, code):
866 name = entry.type.typeobj_cname
868 if entry.visibility == 'extern' and not entry.in_cinclude:
869 code.putln("%s DL_IMPORT(PyTypeObject) %s;" % (
870 Naming.extern_c_macro,
872 elif entry.visibility == 'public':
873 code.putln("%s DL_EXPORT(PyTypeObject) %s;" % (
874 Naming.extern_c_macro,
876 # ??? Do we really need the rest of this? ???
878 # code.putln("staticforward PyTypeObject %s;" % name)
880 def generate_exttype_vtable_struct(self, entry, code):
881 code.mark_pos(entry.pos)
882 # Generate struct declaration for an extension type's vtable.
885 if type.vtabstruct_cname:
889 type.vtabstruct_cname)
890 if type.base_type and type.base_type.vtabstruct_cname:
891 code.putln("struct %s %s;" % (
892 type.base_type.vtabstruct_cname,
893 Naming.obj_base_cname))
894 for method_entry in scope.cfunc_entries:
895 if not method_entry.is_inherited:
897 "%s;" % method_entry.type.declaration_code("(*%s)" % method_entry.name))
901 def generate_exttype_vtabptr_declaration(self, entry, code):
902 code.mark_pos(entry.pos)
903 # Generate declaration of pointer to an extension type's vtable.
905 if type.vtabptr_cname:
906 code.putln("static struct %s *%s;" % (
907 type.vtabstruct_cname,
910 def generate_objstruct_definition(self, type, code):
911 code.mark_pos(type.pos)
912 # Generate object struct definition for an
915 return # Forward declared but never defined
917 self.sue_header_footer(type, "struct", type.objstruct_cname)
920 base_type = type.base_type
924 ("struct ", "")[base_type.typedef_flag],
925 base_type.objstruct_cname,
926 Naming.obj_base_cname))
930 if type.vtabslot_cname and not (type.base_type and type.base_type.vtabslot_cname):
933 type.vtabstruct_cname,
934 type.vtabslot_cname))
935 for attr in type.scope.var_entries:
938 attr.type.declaration_code(attr.cname))
940 if type.objtypedef_cname is not None:
941 # Only for exposing public typedef name.
942 code.putln("typedef struct %s %s;" % (type.objstruct_cname, type.objtypedef_cname))
944 def generate_global_declarations(self, env, code, definition):
946 for entry in env.c_class_entries:
947 if definition or entry.defined_in_pxd:
948 code.putln("static PyTypeObject *%s = 0;" %
949 entry.type.typeptr_cname)
950 code.put_var_declarations(env.var_entries, static = 1,
951 dll_linkage = "DL_EXPORT", definition = definition)
953 def generate_cfunction_predeclarations(self, env, code, definition):
954 for entry in env.cfunc_entries:
955 if entry.inline_func_in_pxd or (not entry.in_cinclude and (definition
956 or entry.defined_in_pxd or entry.visibility == 'extern')):
957 if entry.visibility in ('public', 'extern'):
958 dll_linkage = "DL_EXPORT"
962 if not definition and entry.defined_in_pxd:
963 type = CPtrType(type)
964 header = type.declaration_code(entry.cname,
965 dll_linkage = dll_linkage)
966 if entry.visibility == 'private':
967 storage_class = "static "
968 elif entry.visibility == 'public':
971 storage_class = "%s " % Naming.extern_c_macro
972 if entry.func_modifiers:
973 modifiers = '%s ' % ' '.join([
974 modifier.upper() for modifier in entry.func_modifiers])
977 code.putln("%s%s%s; /*proto*/" % (
982 def generate_typeobj_definitions(self, env, code):
983 full_module_name = env.qualified_name
984 for entry in env.c_class_entries:
985 #print "generate_typeobj_definitions:", entry.name
986 #print "...visibility =", entry.visibility
987 if entry.visibility != 'extern':
990 if scope: # could be None if there was an error
991 self.generate_exttype_vtable(scope, code)
992 self.generate_new_function(scope, code)
993 self.generate_dealloc_function(scope, code)
995 self.generate_traverse_function(scope, code)
996 self.generate_clear_function(scope, code)
997 if scope.defines_any(["__getitem__"]):
998 self.generate_getitem_int_function(scope, code)
999 if scope.defines_any(["__setitem__", "__delitem__"]):
1000 self.generate_ass_subscript_function(scope, code)
1001 if scope.defines_any(["__getslice__", "__setslice__", "__delslice__"]):
1002 warning(self.pos, "__getslice__, __setslice__, and __delslice__ are not supported by Python 3, use __getitem__, __setitem__, and __delitem__ instead", 1)
1003 code.putln("#if PY_MAJOR_VERSION >= 3")
1004 code.putln("#error __getslice__, __setslice__, and __delslice__ not supported in Python 3.")
1005 code.putln("#endif")
1006 if scope.defines_any(["__setslice__", "__delslice__"]):
1007 self.generate_ass_slice_function(scope, code)
1008 if scope.defines_any(["__getattr__","__getattribute__"]):
1009 self.generate_getattro_function(scope, code)
1010 if scope.defines_any(["__setattr__", "__delattr__"]):
1011 self.generate_setattro_function(scope, code)
1012 if scope.defines_any(["__get__"]):
1013 self.generate_descr_get_function(scope, code)
1014 if scope.defines_any(["__set__", "__delete__"]):
1015 self.generate_descr_set_function(scope, code)
1016 self.generate_property_accessors(scope, code)
1017 self.generate_method_table(scope, code)
1018 self.generate_getset_table(scope, code)
1019 self.generate_typeobj_definition(full_module_name, entry, code)
1021 def generate_exttype_vtable(self, scope, code):
1022 # Generate the definition of an extension type's vtable.
1023 type = scope.parent_type
1024 if type.vtable_cname:
1025 code.putln("static struct %s %s;" % (
1026 type.vtabstruct_cname,
1029 def generate_self_cast(self, scope, code):
1030 type = scope.parent_type
1033 type.declaration_code("p"),
1034 type.declaration_code("")))
1036 def generate_new_function(self, scope, code):
1037 tp_slot = TypeSlots.ConstructorSlot("tp_new", '__new__')
1038 slot_func = scope.mangle_internal("tp_new")
1039 type = scope.parent_type
1040 base_type = type.base_type
1042 for entry in scope.var_entries:
1043 if entry.type.is_pyobject:
1044 py_attrs.append(entry)
1045 need_self_cast = type.vtabslot_cname or py_attrs
1048 "static PyObject *%s(PyTypeObject *t, PyObject *a, PyObject *k) {"
1049 % scope.mangle_internal("tp_new"))
1053 % scope.parent_type.declaration_code("p"))
1055 tp_new = TypeSlots.get_base_slot_function(scope, tp_slot)
1057 tp_new = "%s->tp_new" % base_type.typeptr_cname
1059 "PyObject *o = %s(t, a, k);" % tp_new)
1062 "PyObject *o = (*t->tp_alloc)(t, 0);")
1064 "if (!o) return 0;")
1068 % type.cast_code("o"))
1070 # self.generate_self_cast(scope, code)
1071 if type.vtabslot_cname:
1072 vtab_base_type = type
1073 while vtab_base_type.base_type and vtab_base_type.base_type.vtabstruct_cname:
1074 vtab_base_type = vtab_base_type.base_type
1075 if vtab_base_type is not type:
1076 struct_type_cast = "(struct %s*)" % vtab_base_type.vtabstruct_cname
1078 struct_type_cast = ""
1079 code.putln("p->%s = %s%s;" % (
1080 type.vtabslot_cname,
1081 struct_type_cast, type.vtabptr_cname))
1082 for entry in py_attrs:
1083 if scope.is_internal or entry.name == "__weakref__":
1084 # internal classes do not need None inits
1085 code.putln("p->%s = 0;" % entry.cname)
1087 code.put_init_var_to_py_none(entry, "p->%s", nanny=False)
1088 entry = scope.lookup_here("__new__")
1089 if entry and entry.is_special:
1090 if entry.trivial_signature:
1091 cinit_args = "o, %s, NULL" % Naming.empty_tuple
1093 cinit_args = "o, a, k"
1095 "if (%s(%s) < 0) {" %
1096 (entry.func_cname, cinit_args))
1097 code.put_decref_clear("o", py_object_type, nanny=False);
1105 def generate_dealloc_function(self, scope, code):
1106 tp_slot = TypeSlots.ConstructorSlot("tp_dealloc", '__dealloc__')
1107 slot_func = scope.mangle_internal("tp_dealloc")
1108 base_type = scope.parent_type.base_type
1109 if tp_slot.slot_code(scope) != slot_func:
1113 "static void %s(PyObject *o) {"
1114 % scope.mangle_internal("tp_dealloc"))
1116 weakref_slot = scope.lookup_here("__weakref__")
1117 for entry in scope.var_entries:
1118 if entry.type.is_pyobject and entry is not weakref_slot:
1119 py_attrs.append(entry)
1120 if py_attrs or weakref_slot in scope.var_entries:
1121 self.generate_self_cast(scope, code)
1122 self.generate_usr_dealloc_call(scope, code)
1123 if weakref_slot in scope.var_entries:
1124 code.putln("if (p->__weakref__) PyObject_ClearWeakRefs(o);")
1125 for entry in py_attrs:
1126 code.put_xdecref("p->%s" % entry.cname, entry.type, nanny=False)
1128 tp_dealloc = TypeSlots.get_base_slot_function(scope, tp_slot)
1129 if tp_dealloc is None:
1130 tp_dealloc = "%s->tp_dealloc" % base_type.typeptr_cname
1132 "%s(o);" % tp_dealloc)
1135 "(*Py_TYPE(o)->tp_free)(o);")
1139 def generate_usr_dealloc_call(self, scope, code):
1140 entry = scope.lookup_here("__dealloc__")
1145 "PyObject *etype, *eval, *etb;")
1147 "PyErr_Fetch(&etype, &eval, &etb);")
1154 "if (PyErr_Occurred()) PyErr_WriteUnraisable(o);")
1158 "PyErr_Restore(etype, eval, etb);")
1162 def generate_traverse_function(self, scope, code):
1163 tp_slot = TypeSlots.GCDependentSlot("tp_traverse")
1164 slot_func = scope.mangle_internal("tp_traverse")
1165 base_type = scope.parent_type.base_type
1166 if tp_slot.slot_code(scope) != slot_func:
1170 "static int %s(PyObject *o, visitproc v, void *a) {"
1173 for entry in scope.var_entries:
1174 if entry.type.is_pyobject and entry.name != "__weakref__":
1175 py_attrs.append(entry)
1176 if base_type or py_attrs:
1177 code.putln("int e;")
1179 self.generate_self_cast(scope, code)
1181 # want to call it explicitly if possible so inlining can be performed
1182 static_call = TypeSlots.get_base_slot_function(scope, tp_slot)
1184 code.putln("e = %s(o, v, a); if (e) return e;" % static_call)
1186 code.putln("if (%s->tp_traverse) {" % base_type.typeptr_cname)
1188 "e = %s->tp_traverse(o, v, a); if (e) return e;" %
1189 base_type.typeptr_cname)
1191 for entry in py_attrs:
1192 var_code = "p->%s" % entry.cname
1196 if entry.type.is_extension_type:
1197 var_code = "((PyObject*)%s)" % var_code
1199 "e = (*v)(%s, a); if (e) return e;"
1208 def generate_clear_function(self, scope, code):
1209 tp_slot = TypeSlots.GCDependentSlot("tp_clear")
1210 slot_func = scope.mangle_internal("tp_clear")
1211 base_type = scope.parent_type.base_type
1212 if tp_slot.slot_code(scope) != slot_func:
1215 code.putln("static int %s(PyObject *o) {" % slot_func)
1217 for entry in scope.var_entries:
1218 if entry.type.is_pyobject and entry.name != "__weakref__":
1219 py_attrs.append(entry)
1221 self.generate_self_cast(scope, code)
1222 code.putln("PyObject* tmp;")
1224 # want to call it explicitly if possible so inlining can be performed
1225 static_call = TypeSlots.get_base_slot_function(scope, tp_slot)
1227 code.putln("%s(o);" % static_call)
1229 code.putln("if (%s->tp_clear) {" % base_type.typeptr_cname)
1230 code.putln("%s->tp_clear(o);" % base_type.typeptr_cname)
1232 for entry in py_attrs:
1233 name = "p->%s" % entry.cname
1234 code.putln("tmp = ((PyObject*)%s);" % name)
1235 code.put_init_to_py_none(name, entry.type, nanny=False)
1236 code.putln("Py_XDECREF(tmp);")
1242 def generate_getitem_int_function(self, scope, code):
1243 # This function is put into the sq_item slot when
1244 # a __getitem__ method is present. It converts its
1245 # argument to a Python integer and calls mp_subscript.
1247 "static PyObject *%s(PyObject *o, Py_ssize_t i) {" %
1248 scope.mangle_internal("sq_item"))
1252 "PyObject *x = PyInt_FromSsize_t(i); if(!x) return 0;")
1254 "r = Py_TYPE(o)->tp_as_mapping->mp_subscript(o, x);")
1262 def generate_ass_subscript_function(self, scope, code):
1263 # Setting and deleting an item are both done through
1264 # the ass_subscript method, so we dispatch to user's __setitem__
1265 # or __delitem__, or raise an exception.
1266 base_type = scope.parent_type.base_type
1267 set_entry = scope.lookup_here("__setitem__")
1268 del_entry = scope.lookup_here("__delitem__")
1271 "static int %s(PyObject *o, PyObject *i, PyObject *v) {" %
1272 scope.mangle_internal("mp_ass_subscript"))
1277 "return %s(o, i, v);" %
1278 set_entry.func_cname)
1280 self.generate_guarded_basetype_call(
1281 base_type, "tp_as_mapping", "mp_ass_subscript", "o, i, v", code)
1283 "PyErr_Format(PyExc_NotImplementedError,")
1285 ' "Subscript assignment not supported by %s", Py_TYPE(o)->tp_name);')
1294 "return %s(o, i);" %
1295 del_entry.func_cname)
1297 self.generate_guarded_basetype_call(
1298 base_type, "tp_as_mapping", "mp_ass_subscript", "o, i, v", code)
1300 "PyErr_Format(PyExc_NotImplementedError,")
1302 ' "Subscript deletion not supported by %s", Py_TYPE(o)->tp_name);')
1310 def generate_guarded_basetype_call(
1311 self, base_type, substructure, slot, args, code):
1313 base_tpname = base_type.typeptr_cname
1316 "if (%s->%s && %s->%s->%s)" % (
1317 base_tpname, substructure, base_tpname, substructure, slot))
1319 " return %s->%s->%s(%s);" % (
1320 base_tpname, substructure, slot, args))
1326 " return %s->%s(%s);" % (
1327 base_tpname, slot, args))
1329 def generate_ass_slice_function(self, scope, code):
1330 # Setting and deleting a slice are both done through
1331 # the ass_slice method, so we dispatch to user's __setslice__
1332 # or __delslice__, or raise an exception.
1333 base_type = scope.parent_type.base_type
1334 set_entry = scope.lookup_here("__setslice__")
1335 del_entry = scope.lookup_here("__delslice__")
1338 "static int %s(PyObject *o, Py_ssize_t i, Py_ssize_t j, PyObject *v) {" %
1339 scope.mangle_internal("sq_ass_slice"))
1344 "return %s(o, i, j, v);" %
1345 set_entry.func_cname)
1347 self.generate_guarded_basetype_call(
1348 base_type, "tp_as_sequence", "sq_ass_slice", "o, i, j, v", code)
1350 "PyErr_Format(PyExc_NotImplementedError,")
1352 ' "2-element slice assignment not supported by %s", Py_TYPE(o)->tp_name);')
1361 "return %s(o, i, j);" %
1362 del_entry.func_cname)
1364 self.generate_guarded_basetype_call(
1365 base_type, "tp_as_sequence", "sq_ass_slice", "o, i, j, v", code)
1367 "PyErr_Format(PyExc_NotImplementedError,")
1369 ' "2-element slice deletion not supported by %s", Py_TYPE(o)->tp_name);')
1377 def generate_getattro_function(self, scope, code):
1378 # First try to get the attribute using __getattribute__, if defined, or
1379 # PyObject_GenericGetAttr.
1381 # If that raises an AttributeError, call the __getattr__ if defined.
1383 # In both cases, defined can be in this class, or any base class.
1384 def lookup_here_or_base(n,type=None):
1387 type = scope.parent_type
1388 r = type.scope.lookup_here(n)
1390 type.base_type is not None:
1391 return lookup_here_or_base(n,type.base_type)
1394 getattr_entry = lookup_here_or_base("__getattr__")
1395 getattribute_entry = lookup_here_or_base("__getattribute__")
1398 "static PyObject *%s(PyObject *o, PyObject *n) {"
1399 % scope.mangle_internal("tp_getattro"))
1400 if getattribute_entry is not None:
1402 "PyObject *v = %s(o, n);" %
1403 getattribute_entry.func_cname)
1406 "PyObject *v = PyObject_GenericGetAttr(o, n);")
1407 if getattr_entry is not None:
1409 "if (!v && PyErr_ExceptionMatches(PyExc_AttributeError)) {")
1414 getattr_entry.func_cname)
1422 def generate_setattro_function(self, scope, code):
1423 # Setting and deleting an attribute are both done through
1424 # the setattro method, so we dispatch to user's __setattr__
1425 # or __delattr__ or fall back on PyObject_GenericSetAttr.
1426 base_type = scope.parent_type.base_type
1427 set_entry = scope.lookup_here("__setattr__")
1428 del_entry = scope.lookup_here("__delattr__")
1431 "static int %s(PyObject *o, PyObject *n, PyObject *v) {" %
1432 scope.mangle_internal("tp_setattro"))
1437 "return %s(o, n, v);" %
1438 set_entry.func_cname)
1440 self.generate_guarded_basetype_call(
1441 base_type, None, "tp_setattro", "o, n, v", code)
1443 "return PyObject_GenericSetAttr(o, n, v);")
1450 "return %s(o, n);" %
1451 del_entry.func_cname)
1453 self.generate_guarded_basetype_call(
1454 base_type, None, "tp_setattro", "o, n, v", code)
1456 "return PyObject_GenericSetAttr(o, n, 0);")
1462 def generate_descr_get_function(self, scope, code):
1463 # The __get__ function of a descriptor object can be
1464 # called with NULL for the second or third arguments
1465 # under some circumstances, so we replace them with
1466 # None in that case.
1467 user_get_entry = scope.lookup_here("__get__")
1470 "static PyObject *%s(PyObject *o, PyObject *i, PyObject *c) {" %
1471 scope.mangle_internal("tp_descr_get"))
1475 "if (!i) i = Py_None;")
1477 "if (!c) c = Py_None;")
1478 #code.put_incref("i", py_object_type)
1479 #code.put_incref("c", py_object_type)
1481 "r = %s(o, i, c);" %
1482 user_get_entry.func_cname)
1483 #code.put_decref("i", py_object_type)
1484 #code.put_decref("c", py_object_type)
1490 def generate_descr_set_function(self, scope, code):
1491 # Setting and deleting are both done through the __set__
1492 # method of a descriptor, so we dispatch to user's __set__
1493 # or __delete__ or raise an exception.
1494 base_type = scope.parent_type.base_type
1495 user_set_entry = scope.lookup_here("__set__")
1496 user_del_entry = scope.lookup_here("__delete__")
1499 "static int %s(PyObject *o, PyObject *i, PyObject *v) {" %
1500 scope.mangle_internal("tp_descr_set"))
1505 "return %s(o, i, v);" %
1506 user_set_entry.func_cname)
1508 self.generate_guarded_basetype_call(
1509 base_type, None, "tp_descr_set", "o, i, v", code)
1511 'PyErr_SetString(PyExc_NotImplementedError, "__set__");')
1520 "return %s(o, i);" %
1521 user_del_entry.func_cname)
1523 self.generate_guarded_basetype_call(
1524 base_type, None, "tp_descr_set", "o, i, v", code)
1526 'PyErr_SetString(PyExc_NotImplementedError, "__delete__");')
1534 def generate_property_accessors(self, cclass_scope, code):
1535 for entry in cclass_scope.property_entries:
1536 property_scope = entry.scope
1537 if property_scope.defines_any(["__get__"]):
1538 self.generate_property_get_function(entry, code)
1539 if property_scope.defines_any(["__set__", "__del__"]):
1540 self.generate_property_set_function(entry, code)
1542 def generate_property_get_function(self, property_entry, code):
1543 property_scope = property_entry.scope
1544 property_entry.getter_cname = property_scope.parent_scope.mangle(
1545 Naming.prop_get_prefix, property_entry.name)
1546 get_entry = property_scope.lookup_here("__get__")
1549 "static PyObject *%s(PyObject *o, void *x) {" %
1550 property_entry.getter_cname)
1553 get_entry.func_cname)
1557 def generate_property_set_function(self, property_entry, code):
1558 property_scope = property_entry.scope
1559 property_entry.setter_cname = property_scope.parent_scope.mangle(
1560 Naming.prop_set_prefix, property_entry.name)
1561 set_entry = property_scope.lookup_here("__set__")
1562 del_entry = property_scope.lookup_here("__del__")
1565 "static int %s(PyObject *o, PyObject *v, void *x) {" %
1566 property_entry.setter_cname)
1571 "return %s(o, v);" %
1572 set_entry.func_cname)
1575 'PyErr_SetString(PyExc_NotImplementedError, "__set__");')
1585 del_entry.func_cname)
1588 'PyErr_SetString(PyExc_NotImplementedError, "__del__");')
1596 def generate_typeobj_definition(self, modname, entry, code):
1599 for suite in TypeSlots.substructures:
1600 suite.generate_substructure(scope, code)
1602 if entry.visibility == 'public':
1603 header = "DL_EXPORT(PyTypeObject) %s = {"
1605 header = "static PyTypeObject %s = {"
1606 #code.putln(header % scope.parent_type.typeobj_cname)
1607 code.putln(header % type.typeobj_cname)
1609 "PyVarObject_HEAD_INIT(0, 0)")
1611 '__Pyx_NAMESTR("%s.%s"), /*tp_name*/' % (
1612 self.full_module_name, scope.class_name))
1613 if type.typedef_flag:
1614 objstruct = type.objstruct_cname
1616 objstruct = "struct %s" % type.objstruct_cname
1618 "sizeof(%s), /*tp_basicsize*/" %
1621 "0, /*tp_itemsize*/")
1622 for slot in TypeSlots.slot_table:
1623 slot.generate(scope, code)
1627 def generate_method_table(self, env, code):
1630 "static PyMethodDef %s[] = {" %
1631 env.method_table_cname)
1632 for entry in env.pyfunc_entries:
1633 code.put_pymethoddef(entry, ",")
1639 def generate_getset_table(self, env, code):
1640 if env.property_entries:
1643 "static struct PyGetSetDef %s[] = {" %
1644 env.getset_table_cname)
1645 for entry in env.property_entries:
1647 doc_code = "__Pyx_DOCSTR(%s)" % code.get_string_const(entry.doc)
1651 '{(char *)"%s", %s, %s, %s, 0},' % (
1653 entry.getter_cname or "0",
1654 entry.setter_cname or "0",
1661 def generate_import_star(self, env, code):
1662 env.use_utility_code(streq_utility_code)
1664 code.putln("char* %s_type_names[] = {" % Naming.import_star)
1665 for name, entry in env.entries.items():
1667 code.putln('"%s",' % name)
1671 code.enter_cfunc_scope() # as we need labels
1672 code.putln("static int %s(PyObject *o, PyObject* py_name, char *name) {" % Naming.import_star_set)
1673 code.putln("char** type_name = %s_type_names;" % Naming.import_star)
1674 code.putln("while (*type_name) {")
1675 code.putln("if (__Pyx_StrEq(name, *type_name)) {")
1676 code.putln('PyErr_Format(PyExc_TypeError, "Cannot overwrite C type %s", name);')
1677 code.putln('goto bad;')
1679 code.putln("type_name++;")
1681 old_error_label = code.new_error_label()
1682 code.putln("if (0);") # so the first one can be "else if"
1683 for name, entry in env.entries.items():
1684 if entry.is_cglobal and entry.used:
1685 code.putln('else if (__Pyx_StrEq(name, "%s")) {' % name)
1686 if entry.type.is_pyobject:
1687 if entry.type.is_extension_type or entry.type.is_builtin_type:
1688 code.putln("if (!(%s)) %s;" % (
1689 entry.type.type_test_code("o"),
1690 code.error_goto(entry.pos)))
1691 code.putln("Py_INCREF(o);")
1692 code.put_decref(entry.cname, entry.type, nanny=False)
1693 code.putln("%s = %s;" % (
1695 PyrexTypes.typecast(entry.type, py_object_type, "o")))
1696 elif entry.type.from_py_function:
1697 rhs = "%s(o)" % entry.type.from_py_function
1698 if entry.type.is_enum:
1699 rhs = PyrexTypes.typecast(entry.type, PyrexTypes.c_long_type, rhs)
1700 code.putln("%s = %s; if (%s) %s;" % (
1703 entry.type.error_condition(entry.cname),
1704 code.error_goto(entry.pos)))
1706 code.putln('PyErr_Format(PyExc_TypeError, "Cannot convert Python object %s to %s");' % (name, entry.type))
1707 code.putln(code.error_goto(entry.pos))
1709 code.putln("else {")
1710 code.putln("if (PyObject_SetAttr(%s, py_name, o) < 0) goto bad;" % Naming.module_cname)
1712 code.putln("return 0;")
1713 if code.label_used(code.error_label):
1714 code.put_label(code.error_label)
1715 # This helps locate the offending name.
1716 code.putln('__Pyx_AddTraceback("%s");' % self.full_module_name);
1717 code.error_label = old_error_label
1719 code.putln("return -1;")
1721 code.putln(import_star_utility_code)
1722 code.exit_cfunc_scope() # done with labels
1724 def generate_module_init_func(self, imported_modules, env, code):
1725 code.enter_cfunc_scope()
1727 header2 = "PyMODINIT_FUNC init%s(void)" % env.module_name
1728 header3 = "PyMODINIT_FUNC PyInit_%s(void)" % env.module_name
1729 code.putln("#if PY_MAJOR_VERSION < 3")
1730 code.putln("%s; /*proto*/" % header2)
1733 code.putln("%s; /*proto*/" % header3)
1735 code.putln("#endif")
1737 tempdecl_code = code.insertion_point()
1739 code.putln("#if CYTHON_REFNANNY")
1740 code.putln("void* __pyx_refnanny = NULL;")
1741 code.putln("__Pyx_RefNanny = __Pyx_RefNannyImportAPI(\"refnanny\");")
1742 code.putln("if (!__Pyx_RefNanny) {")
1743 code.putln(" PyErr_Clear();")
1744 code.putln(" __Pyx_RefNanny = __Pyx_RefNannyImportAPI(\"Cython.Runtime.refnanny\");")
1745 code.putln(" if (!__Pyx_RefNanny)")
1746 code.putln(" Py_FatalError(\"failed to import 'refnanny' module\");")
1748 code.putln("__pyx_refnanny = __Pyx_RefNanny->SetupContext(\"%s\", __LINE__, __FILE__);"% header3)
1749 code.putln("#endif")
1751 code.putln("%s = PyTuple_New(0); %s" % (Naming.empty_tuple, code.error_goto_if_null(Naming.empty_tuple, self.pos)));
1752 code.putln("%s = PyBytes_FromStringAndSize(\"\", 0); %s" % (Naming.empty_bytes, code.error_goto_if_null(Naming.empty_bytes, self.pos)));
1754 code.putln("#ifdef %s_USED" % Naming.binding_cfunc)
1755 code.putln("if (%s_init() < 0) %s" % (Naming.binding_cfunc, code.error_goto(self.pos)))
1756 code.putln("#endif")
1758 code.putln("/*--- Library function declarations ---*/")
1759 env.generate_library_function_declarations(code)
1761 code.putln("/*--- Threads initialization code ---*/")
1762 code.putln("#if defined(__PYX_FORCE_INIT_THREADS) && __PYX_FORCE_INIT_THREADS")
1763 code.putln("#ifdef WITH_THREAD /* Python build with threading support? */")
1764 code.putln("PyEval_InitThreads();")
1765 code.putln("#endif")
1766 code.putln("#endif")
1768 code.putln("/*--- Module creation code ---*/")
1769 self.generate_module_creation_code(env, code)
1771 code.putln("/*--- Initialize various global constants etc. ---*/")
1772 code.putln(code.error_goto_if_neg("__Pyx_InitGlobals()", self.pos))
1774 __main__name = code.globalstate.get_py_string_const(
1775 EncodedString("__main__"), identifier=True)
1776 code.putln("if (%s%s) {" % (Naming.module_is_main, self.full_module_name.replace('.', '__')))
1778 'if (__Pyx_SetAttrString(%s, "__name__", %s) < 0) %s;' % (
1781 code.error_goto(self.pos)))
1784 if Options.cache_builtins:
1785 code.putln("/*--- Builtin init code ---*/")
1786 code.putln(code.error_goto_if_neg("__Pyx_InitCachedBuiltins()", self.pos))
1788 code.putln("/*--- Constants init code ---*/")
1789 code.putln(code.error_goto_if_neg("__Pyx_InitCachedConstants()", self.pos))
1791 code.putln("/*--- Global init code ---*/")
1792 self.generate_global_init_code(env, code)
1794 code.putln("/*--- Function export code ---*/")
1795 self.generate_c_function_export_code(env, code)
1797 code.putln("/*--- Type init code ---*/")
1798 self.generate_type_init_code(env, code)
1800 code.putln("/*--- Type import code ---*/")
1801 for module in imported_modules:
1802 self.generate_type_import_code_for_module(module, env, code)
1804 code.putln("/*--- Function import code ---*/")
1805 for module in imported_modules:
1806 self.generate_c_function_import_code_for_module(module, env, code)
1808 code.putln("/*--- Execution code ---*/")
1811 self.body.generate_execution_code(code)
1813 if Options.generate_cleanup_code:
1814 # this should be replaced by the module's tp_clear in Py3
1815 env.use_utility_code(import_module_utility_code)
1816 code.putln("if (__Pyx_RegisterCleanup()) %s;" % code.error_goto(self.pos))
1818 code.put_goto(code.return_label)
1819 code.put_label(code.error_label)
1820 for cname, type in code.funcstate.all_managed_temps():
1821 code.put_xdecref(cname, type)
1822 code.putln('if (%s) {' % env.module_cname)
1823 code.putln('__Pyx_AddTraceback("init %s");' % env.qualified_name)
1824 env.use_utility_code(Nodes.traceback_utility_code)
1825 code.put_decref_clear(env.module_cname, py_object_type, nanny=False)
1826 code.putln('} else if (!PyErr_Occurred()) {')
1827 code.putln('PyErr_SetString(PyExc_ImportError, "init %s");' % env.qualified_name)
1829 code.put_label(code.return_label)
1831 code.put_finish_refcount_context()
1833 code.putln("#if PY_MAJOR_VERSION < 3")
1834 code.putln("return;")
1836 code.putln("return %s;" % env.module_cname)
1837 code.putln("#endif")
1840 tempdecl_code.put_temp_declarations(code.funcstate)
1842 code.exit_cfunc_scope()
1844 def generate_module_cleanup_func(self, env, code):
1845 if not Options.generate_cleanup_code:
1847 code.globalstate.use_utility_code(register_cleanup_utility_code)
1848 code.putln('static PyObject *%s(CYTHON_UNUSED PyObject *self, CYTHON_UNUSED PyObject *unused) {' %
1849 Naming.cleanup_cname)
1850 if Options.generate_cleanup_code >= 2:
1851 code.putln("/*--- Global cleanup code ---*/")
1852 rev_entries = list(env.var_entries)
1853 rev_entries.reverse()
1854 for entry in rev_entries:
1855 if entry.visibility != 'extern':
1856 if entry.type.is_pyobject and entry.used:
1857 code.putln("Py_DECREF(%s); %s = 0;" % (
1858 code.entry_as_pyobject(entry), entry.cname))
1859 code.putln("__Pyx_CleanupGlobals();")
1860 if Options.generate_cleanup_code >= 3:
1861 code.putln("/*--- Type import cleanup code ---*/")
1862 for type, _ in env.types_imported.items():
1863 code.putln("Py_DECREF((PyObject *)%s);" % type.typeptr_cname)
1864 if Options.cache_builtins:
1865 code.putln("/*--- Builtin cleanup code ---*/")
1866 for entry in env.cached_builtins:
1867 code.put_decref_clear(entry.cname,
1868 PyrexTypes.py_object_type,
1870 code.putln("/*--- Intern cleanup code ---*/")
1871 code.put_decref_clear(Naming.empty_tuple,
1872 PyrexTypes.py_object_type,
1874 # for entry in env.pynum_entries:
1875 # code.put_decref_clear(entry.cname,
1876 # PyrexTypes.py_object_type,
1878 # for entry in env.all_pystring_entries:
1879 # if entry.is_interned:
1880 # code.put_decref_clear(entry.pystring_cname,
1881 # PyrexTypes.py_object_type,
1883 # for entry in env.default_entries:
1884 # if entry.type.is_pyobject and entry.used:
1885 # code.putln("Py_DECREF(%s); %s = 0;" % (
1886 # code.entry_as_pyobject(entry), entry.cname))
1887 code.putln("Py_INCREF(Py_None); return Py_None;")
1889 def generate_main_method(self, env, code):
1890 module_is_main = "%s%s" % (Naming.module_is_main, self.full_module_name.replace('.', '__'))
1891 code.globalstate.use_utility_code(main_method.specialize(module_name=env.module_name, module_is_main=module_is_main))
1893 def generate_pymoduledef_struct(self, env, code):
1895 doc = "__Pyx_DOCSTR(%s)" % code.get_string_const(env.doc)
1899 code.putln("#if PY_MAJOR_VERSION >= 3")
1900 code.putln("static struct PyModuleDef %s = {" % Naming.pymoduledef_cname)
1901 code.putln(" PyModuleDef_HEAD_INIT,")
1902 code.putln(' __Pyx_NAMESTR("%s"),' % env.module_name)
1903 code.putln(" %s, /* m_doc */" % doc)
1904 code.putln(" -1, /* m_size */")
1905 code.putln(" %s /* m_methods */," % env.method_table_cname)
1906 code.putln(" NULL, /* m_reload */")
1907 code.putln(" NULL, /* m_traverse */")
1908 code.putln(" NULL, /* m_clear */")
1909 code.putln(" NULL /* m_free */")
1911 code.putln("#endif")
1913 def generate_module_creation_code(self, env, code):
1914 # Generate code to create the module object and
1915 # install the builtins.
1917 doc = "__Pyx_DOCSTR(%s)" % code.get_string_const(env.doc)
1920 code.putln("#if PY_MAJOR_VERSION < 3")
1922 '%s = Py_InitModule4(__Pyx_NAMESTR("%s"), %s, %s, 0, PYTHON_API_VERSION);' % (
1925 env.method_table_cname,
1929 "%s = PyModule_Create(&%s);" % (
1931 Naming.pymoduledef_cname))
1932 code.putln("#endif")
1936 code.error_goto(self.pos)));
1937 code.putln("#if PY_MAJOR_VERSION < 3")
1941 code.putln("#endif")
1943 '%s = PyImport_AddModule(__Pyx_NAMESTR(__Pyx_BUILTIN_MODULE_NAME));' %
1944 Naming.builtins_cname)
1947 Naming.builtins_cname,
1948 code.error_goto(self.pos)));
1950 'if (__Pyx_SetAttrString(%s, "__builtins__", %s) < 0) %s;' % (
1952 Naming.builtins_cname,
1953 code.error_goto(self.pos)))
1954 if Options.pre_import is not None:
1956 '%s = PyImport_AddModule(__Pyx_NAMESTR("%s"));' % (
1957 Naming.preimport_cname,
1958 Options.pre_import))
1961 Naming.preimport_cname,
1962 code.error_goto(self.pos)));
1964 def generate_global_init_code(self, env, code):
1965 # Generate code to initialise global PyObject *
1966 # variables to None.
1967 for entry in env.var_entries:
1968 if entry.visibility != 'extern':
1969 if entry.type.is_pyobject and entry.used:
1970 code.put_init_var_to_py_none(entry, nanny=False)
1972 def generate_c_function_export_code(self, env, code):
1973 # Generate code to create PyCFunction wrappers for exported C functions.
1974 for entry in env.cfunc_entries:
1975 if entry.api or entry.defined_in_pxd:
1976 env.use_utility_code(function_export_utility_code)
1977 signature = entry.type.signature_string()
1978 code.putln('if (__Pyx_ExportFunction("%s", (void (*)(void))%s, "%s") < 0) %s' % (
1982 code.error_goto(self.pos)))
1984 def generate_type_import_code_for_module(self, module, env, code):
1985 # Generate type import code for all exported extension types in
1986 # an imported module.
1987 #if module.c_class_entries:
1988 for entry in module.c_class_entries:
1989 if entry.defined_in_pxd:
1990 self.generate_type_import_code(env, entry.type, entry.pos, code)
1992 def generate_c_function_import_code_for_module(self, module, env, code):
1993 # Generate import code for all exported C functions in a cimported module.
1995 for entry in module.cfunc_entries:
1996 if entry.defined_in_pxd:
1997 entries.append(entry)
1999 env.use_utility_code(import_module_utility_code)
2000 env.use_utility_code(function_import_utility_code)
2001 temp = code.funcstate.allocate_temp(py_object_type, manage_ref=True)
2003 '%s = __Pyx_ImportModule("%s"); if (!%s) %s' % (
2005 module.qualified_name,
2007 code.error_goto(self.pos)))
2008 for entry in entries:
2010 'if (__Pyx_ImportFunction(%s, "%s", (void (**)(void))&%s, "%s") < 0) %s' % (
2014 entry.type.signature_string(),
2015 code.error_goto(self.pos)))
2016 code.putln("Py_DECREF(%s); %s = 0;" % (temp, temp))
2018 def generate_type_init_code(self, env, code):
2019 # Generate type import code for extern extension types
2020 # and type ready code for non-extern ones.
2021 for entry in env.c_class_entries:
2022 if entry.visibility == 'extern':
2023 self.generate_type_import_code(env, entry.type, entry.pos, code)
2025 self.generate_base_type_import_code(env, entry, code)
2026 self.generate_exttype_vtable_init_code(entry, code)
2027 self.generate_type_ready_code(env, entry, code)
2028 self.generate_typeptr_assignment_code(entry, code)
2030 def generate_base_type_import_code(self, env, entry, code):
2031 base_type = entry.type.base_type
2032 if base_type and base_type.module_name != env.qualified_name \
2033 and not base_type.is_builtin_type:
2034 self.generate_type_import_code(env, base_type, self.pos, code)
2036 def use_type_import_utility_code(self, env):
2037 env.use_utility_code(type_import_utility_code)
2038 env.use_utility_code(import_module_utility_code)
2040 def generate_type_import_code(self, env, type, pos, code):
2041 # If not already done, generate code to import the typeobject of an
2042 # extension type defined in another module, and extract its C method
2043 # table pointer if any.
2044 if type in env.types_imported:
2046 if type.typedef_flag:
2047 objstruct = type.objstruct_cname
2049 objstruct = "struct %s" % type.objstruct_cname
2050 self.generate_type_import_call(type, code,
2051 code.error_goto_if_null(type.typeptr_cname, pos))
2052 self.use_type_import_utility_code(env)
2053 if type.vtabptr_cname:
2054 env.use_utility_code(Nodes.get_vtable_utility_code)
2055 code.putln("%s = (struct %s*)__Pyx_GetVtable(%s->tp_dict); %s" % (
2057 type.vtabstruct_cname,
2059 code.error_goto_if_null(type.vtabptr_cname, pos)))
2060 env.types_imported[type] = 1
2062 py3_type_name_map = {'str' : 'bytes', 'unicode' : 'str'}
2064 def generate_type_import_call(self, type, code, error_code):
2065 if type.typedef_flag:
2066 objstruct = type.objstruct_cname
2068 objstruct = "struct %s" % type.objstruct_cname
2069 module_name = type.module_name
2070 if module_name not in ('__builtin__', 'builtins'):
2071 module_name = '"%s"' % module_name
2073 module_name = '__Pyx_BUILTIN_MODULE_NAME'
2074 if type.name in self.py3_type_name_map:
2075 code.putln("#if PY_MAJOR_VERSION >= 3")
2076 code.putln('%s = __Pyx_ImportType(%s, "%s", sizeof(%s), 1); %s' % (
2079 self.py3_type_name_map[type.name],
2083 code.putln('%s = __Pyx_ImportType(%s, "%s", sizeof(%s), %i); %s' % (
2088 not type.is_external or type.is_subclassed,
2090 if type.name in self.py3_type_name_map:
2091 code.putln("#endif")
2093 def generate_type_ready_code(self, env, entry, code):
2094 # Generate a call to PyType_Ready for an extension
2095 # type defined in this module.
2097 typeobj_cname = type.typeobj_cname
2099 if scope: # could be None if there was an error
2100 if entry.visibility != 'extern':
2101 for slot in TypeSlots.slot_table:
2102 slot.generate_dynamic_init_code(scope, code)
2104 "if (PyType_Ready(&%s) < 0) %s" % (
2106 code.error_goto(entry.pos)))
2107 # Fix special method docstrings. This is a bit of a hack, but
2108 # unless we let PyType_Ready create the slot wrappers we have
2109 # a significant performance hit. (See trac #561.)
2110 for func in entry.type.scope.pyfunc_entries:
2111 if func.is_special and Options.docstrings and func.wrapperbase_cname:
2114 'PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&%s, "%s"); %s' % (
2117 code.error_goto_if_null('wrapper', entry.pos)))
2119 "if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) {")
2121 "%s = *((PyWrapperDescrObject *)wrapper)->d_base;" % (
2122 func.wrapperbase_cname))
2124 "%s.doc = %s;" % (func.wrapperbase_cname, func.doc_cname))
2126 "((PyWrapperDescrObject *)wrapper)->d_base = &%s;" % (
2127 func.wrapperbase_cname))
2130 if type.vtable_cname:
2132 "if (__Pyx_SetVtable(%s.tp_dict, %s) < 0) %s" % (
2135 code.error_goto(entry.pos)))
2136 env.use_utility_code(Nodes.set_vtable_utility_code)
2137 if not type.scope.is_internal and not type.scope.directives['internal']:
2138 # scope.is_internal is set for types defined by
2139 # Cython (such as closures), the 'internal'
2140 # directive is set by users
2142 'if (__Pyx_SetAttrString(%s, "%s", (PyObject *)&%s) < 0) %s' % (
2143 Naming.module_cname,
2146 code.error_goto(entry.pos)))
2147 weakref_entry = scope.lookup_here("__weakref__")
2149 if weakref_entry.type is py_object_type:
2150 tp_weaklistoffset = "%s.tp_weaklistoffset" % typeobj_cname
2151 if type.typedef_flag:
2152 objstruct = type.objstruct_cname
2154 objstruct = "struct %s" % type.objstruct_cname
2155 code.putln("if (%s == 0) %s = offsetof(%s, %s);" % (
2159 weakref_entry.cname))
2161 error(weakref_entry.pos, "__weakref__ slot must be of type 'object'")
2163 def generate_exttype_vtable_init_code(self, entry, code):
2164 # Generate code to initialise the C method table of an
2167 if type.vtable_cname:
2172 if type.base_type and type.base_type.vtabptr_cname:
2176 Naming.obj_base_cname,
2177 type.base_type.vtabptr_cname))
2179 c_method_entries = [
2180 entry for entry in type.scope.cfunc_entries
2181 if entry.func_cname ]
2182 if c_method_entries:
2183 for meth_entry in c_method_entries:
2184 cast = meth_entry.type.signature_cast_string()
2190 meth_entry.func_cname))
2192 def generate_typeptr_assignment_code(self, entry, code):
2193 # Generate code to initialise the typeptr of an extension
2194 # type defined in this module to point to its type object.
2196 if type.typeobj_cname:
2199 type.typeptr_cname, type.typeobj_cname))
2201 #------------------------------------------------------------------------------------
2203 # Runtime support code
2205 #------------------------------------------------------------------------------------
2207 streq_utility_code = UtilityCode(
2209 static CYTHON_INLINE int __Pyx_StrEq(const char *, const char *); /*proto*/
2212 static CYTHON_INLINE int __Pyx_StrEq(const char *s1, const char *s2) {
2213 while (*s1 != '\\0' && *s1 == *s2) { s1++; s2++; }
2218 #------------------------------------------------------------------------------------
2220 import_module_utility_code = UtilityCode(
2222 static PyObject *__Pyx_ImportModule(const char *name); /*proto*/
2225 #ifndef __PYX_HAVE_RT_ImportModule
2226 #define __PYX_HAVE_RT_ImportModule
2227 static PyObject *__Pyx_ImportModule(const char *name) {
2228 PyObject *py_name = 0;
2229 PyObject *py_module = 0;
2231 #if PY_MAJOR_VERSION < 3
2232 py_name = PyString_FromString(name);
2234 py_name = PyUnicode_FromString(name);
2238 py_module = PyImport_Import(py_name);
2242 Py_XDECREF(py_name);
2248 #------------------------------------------------------------------------------------
2250 type_import_utility_code = UtilityCode(
2252 static PyTypeObject *__Pyx_ImportType(const char *module_name, const char *class_name, long size, int strict); /*proto*/
2255 #ifndef __PYX_HAVE_RT_ImportType
2256 #define __PYX_HAVE_RT_ImportType
2257 static PyTypeObject *__Pyx_ImportType(const char *module_name, const char *class_name,
2258 long size, int strict)
2260 PyObject *py_module = 0;
2261 PyObject *result = 0;
2262 PyObject *py_name = 0;
2265 py_module = __Pyx_ImportModule(module_name);
2268 #if PY_MAJOR_VERSION < 3
2269 py_name = PyString_FromString(class_name);
2271 py_name = PyUnicode_FromString(class_name);
2275 result = PyObject_GetAttr(py_module, py_name);
2278 Py_DECREF(py_module);
2282 if (!PyType_Check(result)) {
2283 PyErr_Format(PyExc_TypeError,
2284 "%s.%s is not a type object",
2285 module_name, class_name);
2288 if (!strict && ((PyTypeObject *)result)->tp_basicsize > size) {
2289 PyOS_snprintf(warning, sizeof(warning),
2290 "%s.%s size changed, may indicate binary incompatibility",
2291 module_name, class_name);
2292 #if PY_VERSION_HEX < 0x02050000
2293 PyErr_Warn(NULL, warning);
2295 PyErr_WarnEx(NULL, warning, 0);
2298 else if (((PyTypeObject *)result)->tp_basicsize != size) {
2299 PyErr_Format(PyExc_ValueError,
2300 "%s.%s has the wrong size, try recompiling",
2301 module_name, class_name);
2304 return (PyTypeObject *)result;
2306 Py_XDECREF(py_module);
2313 #------------------------------------------------------------------------------------
2315 function_export_utility_code = UtilityCode(
2317 static int __Pyx_ExportFunction(const char *name, void (*f)(void), const char *sig); /*proto*/
2320 static int __Pyx_ExportFunction(const char *name, void (*f)(void), const char *sig) {
2328 d = PyObject_GetAttrString(%(MODULE)s, (char *)"%(API)s");
2335 if (PyModule_AddObject(%(MODULE)s, (char *)"%(API)s", d) < 0)
2339 #if PY_VERSION_HEX >= 0x02070000 && !(PY_MAJOR_VERSION==3&&PY_MINOR_VERSION==0)
2340 cobj = PyCapsule_New(tmp.p, sig, 0);
2342 cobj = PyCObject_FromVoidPtrAndDesc(tmp.p, (void *)sig, 0);
2346 if (PyDict_SetItemString(d, name, cobj) < 0)
2356 """ % {'MODULE': Naming.module_cname, 'API': Naming.api_name}
2359 function_import_utility_code = UtilityCode(
2361 static int __Pyx_ImportFunction(PyObject *module, const char *funcname, void (**f)(void), const char *sig); /*proto*/
2364 #ifndef __PYX_HAVE_RT_ImportFunction
2365 #define __PYX_HAVE_RT_ImportFunction
2366 static int __Pyx_ImportFunction(PyObject *module, const char *funcname, void (**f)(void), const char *sig) {
2374 d = PyObject_GetAttrString(module, (char *)"%(API)s");
2377 cobj = PyDict_GetItemString(d, funcname);
2379 PyErr_Format(PyExc_ImportError,
2380 "%%s does not export expected C function %%s",
2381 PyModule_GetName(module), funcname);
2384 #if PY_VERSION_HEX >= 0x02070000 && !(PY_MAJOR_VERSION==3&&PY_MINOR_VERSION==0)
2385 if (!PyCapsule_IsValid(cobj, sig)) {
2386 PyErr_Format(PyExc_TypeError,
2387 "C function %%s.%%s has wrong signature (expected %%s, got %%s)",
2388 PyModule_GetName(module), funcname, sig, PyCapsule_GetName(cobj));
2391 tmp.p = PyCapsule_GetPointer(cobj, sig);
2393 {const char *desc, *s1, *s2;
2394 desc = (const char *)PyCObject_GetDesc(cobj);
2397 s1 = desc; s2 = sig;
2398 while (*s1 != '\\0' && *s1 == *s2) { s1++; s2++; }
2400 PyErr_Format(PyExc_TypeError,
2401 "C function %%s.%%s has wrong signature (expected %%s, got %%s)",
2402 PyModule_GetName(module), funcname, sig, desc);
2405 tmp.p = PyCObject_AsVoidPtr(cobj);}
2417 """ % dict(API = Naming.api_name)
2420 #------------------------------------------------------------------------------------
2422 register_cleanup_utility_code = UtilityCode(
2424 static int __Pyx_RegisterCleanup(void); /*proto*/
2425 static PyObject* %(module_cleanup)s(CYTHON_UNUSED PyObject *self, CYTHON_UNUSED PyObject *unused); /*proto*/
2426 static PyMethodDef cleanup_def = {__Pyx_NAMESTR("__cleanup"), (PyCFunction)&%(module_cleanup)s, METH_NOARGS, 0};
2427 """ % {'module_cleanup': Naming.cleanup_cname},
2429 static int __Pyx_RegisterCleanup(void) {
2430 /* Don't use Py_AtExit because that has a 32-call limit
2431 * and is called after python finalization.
2434 PyObject *cleanup_func = 0;
2435 PyObject *atexit = 0;
2441 cleanup_func = PyCFunction_New(&cleanup_def, 0);
2442 args = PyTuple_New(1);
2443 if (!cleanup_func || !args)
2445 PyTuple_SET_ITEM(args, 0, cleanup_func);
2448 atexit = __Pyx_ImportModule("atexit");
2451 reg = __Pyx_GetAttrString(atexit, "register");
2454 res = PyObject_CallObject(reg, args);
2459 Py_XDECREF(cleanup_func);
2468 import_star_utility_code = """
2470 /* import_all_from is an unexposed function from ceval.c */
2473 __Pyx_import_all_from(PyObject *locals, PyObject *v)
2475 PyObject *all = __Pyx_GetAttrString(v, "__all__");
2476 PyObject *dict, *name, *value;
2477 int skip_leading_underscores = 0;
2481 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
2482 return -1; /* Unexpected error */
2484 dict = __Pyx_GetAttrString(v, "__dict__");
2486 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
2488 PyErr_SetString(PyExc_ImportError,
2489 "from-import-* object has no __dict__ and no __all__");
2492 all = PyMapping_Keys(dict);
2496 skip_leading_underscores = 1;
2499 for (pos = 0, err = 0; ; pos++) {
2500 name = PySequence_GetItem(all, pos);
2502 if (!PyErr_ExceptionMatches(PyExc_IndexError))
2508 if (skip_leading_underscores &&
2509 #if PY_MAJOR_VERSION < 3
2510 PyString_Check(name) &&
2511 PyString_AS_STRING(name)[0] == '_')
2513 PyUnicode_Check(name) &&
2514 PyUnicode_AS_UNICODE(name)[0] == '_')
2520 value = PyObject_GetAttr(v, name);
2523 else if (PyDict_CheckExact(locals))
2524 err = PyDict_SetItem(locals, name, value);
2526 err = PyObject_SetItem(locals, name, value);
2537 static int %(IMPORT_STAR)s(PyObject* m) {
2542 PyObject *locals = 0;
2544 #if PY_MAJOR_VERSION >= 3
2545 PyObject *utf8_name = 0;
2550 locals = PyDict_New(); if (!locals) goto bad;
2551 if (__Pyx_import_all_from(locals, m) < 0) goto bad;
2552 list = PyDict_Items(locals); if (!list) goto bad;
2554 for(i=0; i<PyList_GET_SIZE(list); i++) {
2555 name = PyTuple_GET_ITEM(PyList_GET_ITEM(list, i), 0);
2556 item = PyTuple_GET_ITEM(PyList_GET_ITEM(list, i), 1);
2557 #if PY_MAJOR_VERSION >= 3
2558 utf8_name = PyUnicode_AsUTF8String(name);
2559 if (!utf8_name) goto bad;
2560 s = PyBytes_AS_STRING(utf8_name);
2561 if (%(IMPORT_STAR_SET)s(item, name, s) < 0) goto bad;
2562 Py_DECREF(utf8_name); utf8_name = 0;
2564 s = PyString_AsString(name);
2566 if (%(IMPORT_STAR_SET)s(item, name, s) < 0) goto bad;
2574 #if PY_MAJOR_VERSION >= 3
2575 Py_XDECREF(utf8_name);
2579 """ % {'IMPORT_STAR' : Naming.import_star,
2580 'IMPORT_STAR_SET' : Naming.import_star_set }
2582 refnanny_utility_code = UtilityCode(proto="""
2583 #ifndef CYTHON_REFNANNY
2584 #define CYTHON_REFNANNY 0
2589 void (*INCREF)(void*, PyObject*, int);
2590 void (*DECREF)(void*, PyObject*, int);
2591 void (*GOTREF)(void*, PyObject*, int);
2592 void (*GIVEREF)(void*, PyObject*, int);
2593 void* (*SetupContext)(const char*, int, const char*);
2594 void (*FinishContext)(void**);
2595 } __Pyx_RefNannyAPIStruct;
2596 static __Pyx_RefNannyAPIStruct *__Pyx_RefNanny = NULL;
2597 static __Pyx_RefNannyAPIStruct * __Pyx_RefNannyImportAPI(const char *modname) {
2598 PyObject *m = NULL, *p = NULL;
2600 m = PyImport_ImportModule((char *)modname);
2602 p = PyObject_GetAttrString(m, (char *)\"RefNannyAPI\");
2604 r = PyLong_AsVoidPtr(p);
2608 return (__Pyx_RefNannyAPIStruct *)r;
2610 #define __Pyx_RefNannyDeclareContext void *__pyx_refnanny;
2611 #define __Pyx_RefNannySetupContext(name) \
2612 __pyx_refnanny = __Pyx_RefNanny->SetupContext((name), __LINE__, __FILE__)
2613 #define __Pyx_RefNannyFinishContext() \
2614 __Pyx_RefNanny->FinishContext(&__pyx_refnanny)
2615 #define __Pyx_INCREF(r) __Pyx_RefNanny->INCREF(__pyx_refnanny, (PyObject *)(r), __LINE__)
2616 #define __Pyx_DECREF(r) __Pyx_RefNanny->DECREF(__pyx_refnanny, (PyObject *)(r), __LINE__)
2617 #define __Pyx_GOTREF(r) __Pyx_RefNanny->GOTREF(__pyx_refnanny, (PyObject *)(r), __LINE__)
2618 #define __Pyx_GIVEREF(r) __Pyx_RefNanny->GIVEREF(__pyx_refnanny, (PyObject *)(r), __LINE__)
2619 #define __Pyx_XDECREF(r) do { if((r) != NULL) {__Pyx_DECREF(r);} } while(0)
2621 #define __Pyx_RefNannyDeclareContext
2622 #define __Pyx_RefNannySetupContext(name)
2623 #define __Pyx_RefNannyFinishContext()
2624 #define __Pyx_INCREF(r) Py_INCREF(r)
2625 #define __Pyx_DECREF(r) Py_DECREF(r)
2626 #define __Pyx_GOTREF(r)
2627 #define __Pyx_GIVEREF(r)
2628 #define __Pyx_XDECREF(r) Py_XDECREF(r)
2629 #endif /* CYTHON_REFNANNY */
2630 #define __Pyx_XGIVEREF(r) do { if((r) != NULL) {__Pyx_GIVEREF(r);} } while(0)
2631 #define __Pyx_XGOTREF(r) do { if((r) != NULL) {__Pyx_GOTREF(r);} } while(0)
2635 main_method = UtilityCode(
2638 #include <floatingpoint.h>
2641 #if PY_MAJOR_VERSION < 3
2642 int main(int argc, char** argv) {
2643 #elif defined(WIN32) || defined(MS_WINDOWS)
2644 int wmain(int argc, wchar_t **argv) {
2646 static int __Pyx_main(int argc, wchar_t **argv) {
2650 /* 754 requires that FP exceptions run in "no stop" mode by default,
2651 * and until C vendors implement C99's ways to control FP exceptions,
2652 * Python requires non-stop mode. Alas, some platforms enable FP
2653 * exceptions by default. Here we disable them.
2659 fpsetmask(m & ~FP_X_OFL);
2661 Py_SetProgramName(argv[0]);
2663 PySys_SetArgv(argc, argv);
2664 %(module_is_main)s = 1;
2665 #if PY_MAJOR_VERSION < 3
2666 init%(module_name)s();
2668 m = PyInit_%(module_name)s();
2670 if (PyErr_Occurred() != NULL) {
2672 PyErr_Print(); /* This exits with the right code if SystemExit. */
2673 #if PY_MAJOR_VERSION < 3
2674 if (Py_FlushLine()) PyErr_Clear();
2683 #if PY_MAJOR_VERSION >= 3 && !defined(WIN32) && !defined(MS_WINDOWS)
2687 __Pyx_char2wchar(char* arg)
2690 #ifdef HAVE_BROKEN_MBSTOWCS
2691 /* Some platforms have a broken implementation of
2692 * mbstowcs which does not count the characters that
2693 * would result from conversion. Use an upper bound.
2695 size_t argsize = strlen(arg);
2697 size_t argsize = mbstowcs(NULL, arg, 0);
2705 if (argsize != (size_t)-1) {
2706 res = (wchar_t *)malloc((argsize+1)*sizeof(wchar_t));
2709 count = mbstowcs(res, arg, argsize+1);
2710 if (count != (size_t)-1) {
2712 /* Only use the result if it contains no
2713 surrogate characters. */
2714 for (tmp = res; *tmp != 0 &&
2715 (*tmp < 0xd800 || *tmp > 0xdfff); tmp++)
2722 /* Conversion failed. Fall back to escaping with surrogateescape. */
2724 /* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */
2726 /* Overallocate; as multi-byte characters are in the argument, the
2727 actual output could use less memory. */
2728 argsize = strlen(arg) + 1;
2729 res = malloc(argsize*sizeof(wchar_t));
2731 in = (unsigned char*)arg;
2733 memset(&mbs, 0, sizeof mbs);
2735 size_t converted = mbrtowc(out, (char*)in, argsize, &mbs);
2737 /* Reached end of string; null char stored. */
2739 if (converted == (size_t)-2) {
2740 /* Incomplete character. This should never happen,
2741 since we provide everything that we have -
2742 unless there is a bug in the C library, or I
2743 misunderstood how mbrtowc works. */
2744 fprintf(stderr, "unexpected mbrtowc result -2\\n");
2747 if (converted == (size_t)-1) {
2748 /* Conversion error. Escape as UTF-8b, and start over
2749 in the initial shift state. */
2750 *out++ = 0xdc00 + *in++;
2752 memset(&mbs, 0, sizeof mbs);
2755 if (*out >= 0xd800 && *out <= 0xdfff) {
2756 /* Surrogate character. Escape the original
2757 byte sequence with surrogateescape. */
2758 argsize -= converted;
2760 *out++ = 0xdc00 + *in++;
2763 /* successfully converted some bytes */
2765 argsize -= converted;
2769 /* Cannot use C locale for escaping; manually escape as if charset
2770 is ASCII (i.e. escape all bytes > 128. This will still roundtrip
2771 correctly in the locale's charset, which must be an ASCII superset. */
2772 res = malloc((strlen(arg)+1)*sizeof(wchar_t));
2774 in = (unsigned char*)arg;
2780 *out++ = 0xdc00 + *in++;
2785 fprintf(stderr, "out of memory\\n");
2790 main(int argc, char **argv)
2792 wchar_t **argv_copy = (wchar_t **)malloc(sizeof(wchar_t*)*argc);
2793 /* We need a second copies, as Python might modify the first one. */
2794 wchar_t **argv_copy2 = (wchar_t **)malloc(sizeof(wchar_t*)*argc);
2797 if (!argv_copy || !argv_copy2) {
2798 fprintf(stderr, "out of memory\\n");
2801 oldloc = strdup(setlocale(LC_ALL, NULL));
2802 setlocale(LC_ALL, "");
2803 for (i = 0; i < argc; i++) {
2804 argv_copy2[i] = argv_copy[i] = __Pyx_char2wchar(argv[i]);
2808 setlocale(LC_ALL, oldloc);
2810 res = __Pyx_main(argc, argv_copy);
2811 for (i = 0; i < argc; i++) {
2812 free(argv_copy2[i]);
2821 packed_struct_utility_code = UtilityCode(proto="""
2822 #if defined(__GNUC__)
2823 #define __Pyx_PACKED __attribute__((__packed__))
2825 #define __Pyx_PACKED
2827 """, impl="", proto_block='utility_code_proto_before_types')