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,
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 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, api=0, pxd=0):
100 return [entry for entry in entries
101 if ((entry.visibility == 'public') or
102 (api and entry.api) or
103 (pxd and entry.defined_in_pxd))]
104 h_types = h_entries(env.type_entries, api=1)
105 h_vars = h_entries(env.var_entries)
106 h_funcs = h_entries(env.cfunc_entries)
107 h_extension_types = h_entries(env.c_class_entries)
108 if (h_types or h_vars or h_funcs or h_extension_types):
109 result.h_file = replace_suffix(result.c_file, ".h")
110 h_code = Code.CCodeWriter()
111 Code.GlobalState(h_code)
112 if options.generate_pxi:
113 result.i_file = replace_suffix(result.c_file, ".pxi")
114 i_code = Code.PyrexCodeWriter(result.i_file)
118 h_guard = Naming.h_guard_prefix + self.api_name(env)
119 h_code.put_h_guard(h_guard)
121 self.generate_type_header_code(h_types, h_code)
123 api_guard = Naming.api_guard_prefix + self.api_name(env)
124 h_code.putln("#ifndef %s" % api_guard)
126 self.generate_extern_c_macro_definition(h_code)
127 if h_extension_types:
129 for entry in h_extension_types:
130 self.generate_cclass_header_code(entry.type, h_code)
132 self.generate_cclass_include_code(entry.type, i_code)
135 for entry in h_funcs:
136 self.generate_public_declaration(entry, h_code, i_code)
140 self.generate_public_declaration(entry, h_code, i_code)
142 h_code.putln("#endif /* !%s */" % api_guard)
144 h_code.putln("#if PY_MAJOR_VERSION < 3")
145 h_code.putln("PyMODINIT_FUNC init%s(void);" % env.module_name)
146 h_code.putln("#else")
147 h_code.putln("PyMODINIT_FUNC PyInit_%s(void);" % env.module_name)
148 h_code.putln("#endif")
150 h_code.putln("#endif /* !%s */" % h_guard)
152 f = open_new_file(result.h_file)
158 def generate_public_declaration(self, entry, h_code, i_code):
159 h_code.putln("%s %s;" % (
160 Naming.extern_c_macro,
161 entry.type.declaration_code(
162 entry.cname, dll_linkage = "DL_IMPORT")))
164 i_code.putln("cdef extern %s" %
165 entry.type.declaration_code(entry.cname, pyrex = 1))
167 def api_name(self, env):
168 return env.qualified_name.replace(".", "__")
170 def generate_api_code(self, env, result):
171 def api_entries(entries, pxd=0):
172 return [entry for entry in entries
173 if entry.api or (pxd and entry.defined_in_pxd)]
174 api_vars = api_entries(env.var_entries)
175 api_funcs = api_entries(env.cfunc_entries)
176 api_extension_types = api_entries(env.c_class_entries)
177 if api_vars or api_funcs or api_extension_types:
178 result.api_file = replace_suffix(result.c_file, "_api.h")
179 h_code = Code.CCodeWriter()
180 Code.GlobalState(h_code)
181 api_guard = Naming.api_guard_prefix + self.api_name(env)
182 h_code.put_h_guard(api_guard)
183 h_code.putln('#include "Python.h"')
185 h_code.putln('#include "%s"' % os.path.basename(result.h_file))
186 if api_extension_types:
188 for entry in api_extension_types:
190 h_code.putln("static PyTypeObject *%s = 0;" % type.typeptr_cname)
191 h_code.putln("#define %s (*%s)" % (
192 type.typeobj_cname, type.typeptr_cname))
195 for entry in api_funcs:
196 type = CPtrType(entry.type)
197 cname = env.mangle(Naming.func_prefix, entry.name)
198 h_code.putln("static %s = 0;" % type.declaration_code(cname))
199 h_code.putln("#define %s %s" % (entry.name, cname))
202 for entry in api_vars:
203 type = CPtrType(entry.type)
204 cname = env.mangle(Naming.var_prefix, entry.name)
205 h_code.putln("static %s = 0;" % type.declaration_code(cname))
206 h_code.putln("#define %s (*%s)" % (entry.name, cname))
207 h_code.put(import_module_utility_code.impl)
209 h_code.put(voidptr_import_utility_code.impl)
211 h_code.put(function_import_utility_code.impl)
212 if api_extension_types:
213 h_code.put(type_import_utility_code.impl)
215 h_code.putln("static int import_%s(void) {" % self.api_name(env))
216 h_code.putln("PyObject *module = 0;")
217 h_code.putln('module = __Pyx_ImportModule("%s");' % env.qualified_name)
218 h_code.putln("if (!module) goto bad;")
219 for entry in api_funcs:
220 cname = env.mangle(Naming.func_prefix, entry.name)
221 sig = entry.type.signature_string()
223 'if (__Pyx_ImportFunction(module, "%s", (void (**)(void))&%s, "%s") < 0) goto bad;'
224 % (entry.name, cname, sig))
225 for entry in api_vars:
226 cname = env.mangle(Naming.var_prefix, entry.name)
227 sig = entry.type.declaration_code("")
229 'if (__Pyx_ImportVoidPtr(module, "%s", (void **)&%s, "%s") < 0) goto bad;'
230 % (entry.name, cname, sig))
231 h_code.putln("Py_DECREF(module); module = 0;")
232 for entry in api_extension_types:
233 self.generate_type_import_call(
235 "if (!%s) goto bad;" % entry.type.typeptr_cname)
236 h_code.putln("return 0;")
238 h_code.putln("Py_XDECREF(module);")
239 h_code.putln("return -1;")
242 h_code.putln("#endif /* !%s */" % api_guard)
244 f = open_new_file(result.api_file)
250 def generate_cclass_header_code(self, type, h_code):
251 h_code.putln("%s %s %s;" % (
252 Naming.extern_c_macro,
253 PyrexTypes.public_decl("PyTypeObject", "DL_IMPORT"),
256 def generate_cclass_include_code(self, type, i_code):
257 i_code.putln("cdef extern class %s.%s:" % (
258 type.module_name, type.name))
260 var_entries = type.scope.var_entries
262 for entry in var_entries:
263 i_code.putln("cdef %s" %
264 entry.type.declaration_code(entry.cname, pyrex = 1))
269 def generate_c_code(self, env, options, result):
270 modules = self.referenced_modules
272 if Options.annotate or options.annotate:
273 emit_linenums = False
274 rootwriter = Annotate.AnnotationCCodeWriter()
276 emit_linenums = options.emit_linenums
277 rootwriter = Code.CCodeWriter(emit_linenums=emit_linenums, c_line_in_traceback=options.c_line_in_traceback)
278 globalstate = Code.GlobalState(rootwriter, emit_linenums)
279 globalstate.initialize_main_c_code()
280 h_code = globalstate['h_code']
282 self.generate_module_preamble(env, modules, h_code)
284 globalstate.module_pos = self.pos
285 globalstate.directives = self.directives
287 globalstate.use_utility_code(refnanny_utility_code)
289 code = globalstate['before_global_var']
290 code.putln('#define __Pyx_MODULE_NAME "%s"' % self.full_module_name)
291 code.putln("int %s%s = 0;" % (Naming.module_is_main, self.full_module_name.replace('.', '__')))
293 code.putln("/* Implementation of %s */" % env.qualified_name)
295 code = globalstate['all_the_rest']
297 self.generate_cached_builtins_decls(env, code)
298 self.generate_lambda_definitions(env, code)
299 # generate normal function definitions
300 self.body.generate_function_definitions(env, code)
302 self.generate_typeobj_definitions(env, code)
303 self.generate_method_table(env, code)
304 if env.has_import_star:
305 self.generate_import_star(env, code)
306 self.generate_pymoduledef_struct(env, code)
308 # init_globals is inserted before this
309 self.generate_module_init_func(modules[:-1], env, globalstate['init_module'])
310 self.generate_module_cleanup_func(env, globalstate['cleanup_module'])
312 self.generate_main_method(env, globalstate['main_method'])
313 self.generate_filename_table(globalstate['filename_table'])
315 self.generate_declarations_for_modules(env, modules, globalstate)
318 for utilcode in env.utility_code_list:
319 globalstate.use_utility_code(utilcode)
320 globalstate.finalize_main_c_code()
322 f = open_new_file(result.c_file)
324 if options.gdb_debug:
325 self._serialize_lineno_map(env, rootwriter)
327 result.c_file_generated = 1
328 if Options.annotate or options.annotate:
329 self.annotate(rootwriter)
330 rootwriter.save_annotation(result.main_source_file, result.c_file)
332 def _serialize_lineno_map(self, env, ccodewriter):
333 tb = env.context.gdb_debug_outputwriter
334 markers = ccodewriter.buffer.allmarkers()
337 for c_lineno, cython_lineno in enumerate(markers):
338 if cython_lineno > 0:
339 d.setdefault(cython_lineno, []).append(c_lineno + 1)
341 tb.start('LineNumberMapping')
342 for cython_lineno, c_linenos in sorted(d.iteritems()):
344 'c_linenos': ' '.join(map(str, c_linenos)),
345 'cython_lineno': str(cython_lineno),
347 tb.start('LineNumber', attrs)
349 tb.end('LineNumberMapping')
352 def find_referenced_modules(self, env, module_list, modules_seen):
353 if env not in modules_seen:
354 modules_seen[env] = 1
355 for imported_module in env.cimported_modules:
356 self.find_referenced_modules(imported_module, module_list, modules_seen)
357 module_list.append(env)
359 def sort_types_by_inheritance(self, type_dict, getkey):
360 # copy the types into a list moving each parent type before
362 type_items = type_dict.items()
364 for i, item in enumerate(type_items):
365 key, new_entry = item
367 # collect all base classes to check for children
371 base_type = base.type.base_type
374 base_key = getkey(base_type)
375 hierarchy.add(base_key)
376 base = type_dict.get(base_key)
377 new_entry.base_keys = hierarchy
379 # find the first (sub-)subclass and insert before that
382 if key in entry.base_keys:
383 type_list.insert(j, new_entry)
386 type_list.append(new_entry)
389 def sort_type_hierarchy(self, module_list, env):
392 for module in module_list:
393 for entry in module.c_class_entries:
394 if not entry.in_cinclude:
396 if type.vtabstruct_cname:
397 vtab_dict[type.vtabstruct_cname] = entry
398 all_defined_here = module is env
399 for entry in module.type_entries:
400 if all_defined_here or entry.defined_in_pxd:
402 if type.is_extension_type and not entry.in_cinclude:
404 vtabslot_dict[type.objstruct_cname] = entry
406 def vtabstruct_cname(entry_type):
407 return entry_type.vtabstruct_cname
408 vtab_list = self.sort_types_by_inheritance(
409 vtab_dict, vtabstruct_cname)
411 def objstruct_cname(entry_type):
412 return entry_type.objstruct_cname
413 vtabslot_list = self.sort_types_by_inheritance(
414 vtabslot_dict, objstruct_cname)
416 return (vtab_list, vtabslot_list)
418 def generate_type_definitions(self, env, modules, vtab_list, vtabslot_list, code):
419 vtabslot_entries = set(vtabslot_list)
420 for module in modules:
421 definition = module is env
423 type_entries = module.type_entries
426 for entry in module.type_entries:
427 if entry.defined_in_pxd:
428 type_entries.append(entry)
429 for entry in type_entries:
430 if not entry.in_cinclude:
431 #print "generate_type_header_code:", entry.name, repr(entry.type) ###
433 if type.is_typedef: # Must test this first!
434 self.generate_typedef(entry, code)
435 elif type.is_struct_or_union:
436 self.generate_struct_union_definition(entry, code)
438 self.generate_enum_definition(entry, code)
439 elif type.is_extension_type and entry not in vtabslot_entries:
440 self.generate_objstruct_definition(type, code)
441 for entry in vtabslot_list:
442 self.generate_objstruct_definition(entry.type, code)
443 self.generate_typeobj_predeclaration(entry, code)
444 for entry in vtab_list:
445 self.generate_typeobj_predeclaration(entry, code)
446 self.generate_exttype_vtable_struct(entry, code)
447 self.generate_exttype_vtabptr_declaration(entry, code)
449 def generate_declarations_for_modules(self, env, modules, globalstate):
450 typecode = globalstate['type_declarations']
452 typecode.putln("/* Type declarations */")
453 vtab_list, vtabslot_list = self.sort_type_hierarchy(modules, env)
454 self.generate_type_definitions(
455 env, modules, vtab_list, vtabslot_list, typecode)
456 modulecode = globalstate['module_declarations']
457 for module in modules:
458 defined_here = module is env
459 modulecode.putln("/* Module declarations from %s */" %
460 module.qualified_name)
461 self.generate_global_declarations(module, modulecode, defined_here)
462 self.generate_cfunction_predeclarations(module, modulecode, defined_here)
464 def generate_module_preamble(self, env, cimported_modules, code):
465 code.putln("/* Generated by Cython %s on %s */" % (
466 Version.version, time.asctime()))
468 code.putln("#define PY_SSIZE_T_CLEAN")
469 for filename in env.python_include_files:
470 code.putln('#include "%s"' % filename)
471 code.putln("#ifndef Py_PYTHON_H")
472 code.putln(" #error Python headers needed to compile C extensions, please install development version of Python.")
474 code.globalstate["end"].putln("#endif /* Py_PYTHON_H */")
477 #include <stddef.h> /* For offsetof */
479 #define offsetof(type, member) ( (size_t) & ((type*)0) -> member )
482 #if !defined(WIN32) && !defined(MS_WINDOWS)
495 #define DL_IMPORT(t) t
498 #define DL_EXPORT(t) t
502 #define PY_LONG_LONG LONG_LONG
505 #if PY_VERSION_HEX < 0x02040000
506 #define METH_COEXIST 0
507 #define PyDict_CheckExact(op) (Py_TYPE(op) == &PyDict_Type)
508 #define PyDict_Contains(d,o) PySequence_Contains(d,o)
511 #if PY_VERSION_HEX < 0x02050000
512 typedef int Py_ssize_t;
513 #define PY_SSIZE_T_MAX INT_MAX
514 #define PY_SSIZE_T_MIN INT_MIN
515 #define PY_FORMAT_SIZE_T \"\"
516 #define PyInt_FromSsize_t(z) PyInt_FromLong(z)
517 #define PyInt_AsSsize_t(o) PyInt_AsLong(o)
518 #define PyNumber_Index(o) PyNumber_Int(o)
519 #define PyIndex_Check(o) PyNumber_Check(o)
520 #define PyErr_WarnEx(category, message, stacklevel) PyErr_Warn(category, message)
523 #if PY_VERSION_HEX < 0x02060000
524 #define Py_REFCNT(ob) (((PyObject*)(ob))->ob_refcnt)
525 #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
526 #define Py_SIZE(ob) (((PyVarObject*)(ob))->ob_size)
527 #define PyVarObject_HEAD_INIT(type, size) \\
528 PyObject_HEAD_INIT(type) size,
529 #define PyType_Modified(t)
541 Py_ssize_t *suboffsets;
545 #define PyBUF_SIMPLE 0
546 #define PyBUF_WRITABLE 0x0001
547 #define PyBUF_FORMAT 0x0004
548 #define PyBUF_ND 0x0008
549 #define PyBUF_STRIDES (0x0010 | PyBUF_ND)
550 #define PyBUF_C_CONTIGUOUS (0x0020 | PyBUF_STRIDES)
551 #define PyBUF_F_CONTIGUOUS (0x0040 | PyBUF_STRIDES)
552 #define PyBUF_ANY_CONTIGUOUS (0x0080 | PyBUF_STRIDES)
553 #define PyBUF_INDIRECT (0x0100 | PyBUF_STRIDES)
557 #if PY_MAJOR_VERSION < 3
558 #define __Pyx_BUILTIN_MODULE_NAME "__builtin__"
560 #define __Pyx_BUILTIN_MODULE_NAME "builtins"
563 #if PY_MAJOR_VERSION >= 3
564 #define Py_TPFLAGS_CHECKTYPES 0
565 #define Py_TPFLAGS_HAVE_INDEX 0
568 #if (PY_VERSION_HEX < 0x02060000) || (PY_MAJOR_VERSION >= 3)
569 #define Py_TPFLAGS_HAVE_NEWBUFFER 0
572 #if PY_MAJOR_VERSION >= 3
573 #define PyBaseString_Type PyUnicode_Type
574 #define PyStringObject PyUnicodeObject
575 #define PyString_Type PyUnicode_Type
576 #define PyString_Check PyUnicode_Check
577 #define PyString_CheckExact PyUnicode_CheckExact
580 #if PY_VERSION_HEX < 0x02060000
581 #define PyBytesObject PyStringObject
582 #define PyBytes_Type PyString_Type
583 #define PyBytes_Check PyString_Check
584 #define PyBytes_CheckExact PyString_CheckExact
585 #define PyBytes_FromString PyString_FromString
586 #define PyBytes_FromStringAndSize PyString_FromStringAndSize
587 #define PyBytes_FromFormat PyString_FromFormat
588 #define PyBytes_DecodeEscape PyString_DecodeEscape
589 #define PyBytes_AsString PyString_AsString
590 #define PyBytes_AsStringAndSize PyString_AsStringAndSize
591 #define PyBytes_Size PyString_Size
592 #define PyBytes_AS_STRING PyString_AS_STRING
593 #define PyBytes_GET_SIZE PyString_GET_SIZE
594 #define PyBytes_Repr PyString_Repr
595 #define PyBytes_Concat PyString_Concat
596 #define PyBytes_ConcatAndDel PyString_ConcatAndDel
599 #if PY_VERSION_HEX < 0x02060000
600 #define PySet_Check(obj) PyObject_TypeCheck(obj, &PySet_Type)
601 #define PyFrozenSet_Check(obj) PyObject_TypeCheck(obj, &PyFrozenSet_Type)
603 #ifndef PySet_CheckExact
604 #define PySet_CheckExact(obj) (Py_TYPE(obj) == &PySet_Type)
607 #define __Pyx_TypeCheck(obj, type) PyObject_TypeCheck(obj, (PyTypeObject *)type)
609 #if PY_MAJOR_VERSION >= 3
610 #define PyIntObject PyLongObject
611 #define PyInt_Type PyLong_Type
612 #define PyInt_Check(op) PyLong_Check(op)
613 #define PyInt_CheckExact(op) PyLong_CheckExact(op)
614 #define PyInt_FromString PyLong_FromString
615 #define PyInt_FromUnicode PyLong_FromUnicode
616 #define PyInt_FromLong PyLong_FromLong
617 #define PyInt_FromSize_t PyLong_FromSize_t
618 #define PyInt_FromSsize_t PyLong_FromSsize_t
619 #define PyInt_AsLong PyLong_AsLong
620 #define PyInt_AS_LONG PyLong_AS_LONG
621 #define PyInt_AsSsize_t PyLong_AsSsize_t
622 #define PyInt_AsUnsignedLongMask PyLong_AsUnsignedLongMask
623 #define PyInt_AsUnsignedLongLongMask PyLong_AsUnsignedLongLongMask
626 #if PY_MAJOR_VERSION >= 3
627 #define PyBoolObject PyLongObject
630 #if PY_VERSION_HEX < 0x03020000
631 typedef long Py_hash_t;
632 #define __Pyx_PyInt_FromHash_t PyInt_FromLong
633 #define __Pyx_PyInt_AsHash_t PyInt_AsLong
635 #define __Pyx_PyInt_FromHash_t PyInt_FromSsize_t
636 #define __Pyx_PyInt_AsHash_t PyInt_AsSsize_t
642 #if PY_MAJOR_VERSION >= 3
643 #define __Pyx_PyNumber_Divide(x,y) PyNumber_TrueDivide(x,y)
644 #define __Pyx_PyNumber_InPlaceDivide(x,y) PyNumber_InPlaceTrueDivide(x,y)
647 if Future.division in env.context.future_directives:
648 code.putln(" #define __Pyx_PyNumber_Divide(x,y) PyNumber_TrueDivide(x,y)")
649 code.putln(" #define __Pyx_PyNumber_InPlaceDivide(x,y) PyNumber_InPlaceTrueDivide(x,y)")
651 code.putln(" #define __Pyx_PyNumber_Divide(x,y) PyNumber_Divide(x,y)")
652 code.putln(" #define __Pyx_PyNumber_InPlaceDivide(x,y) PyNumber_InPlaceDivide(x,y)")
656 #if (PY_MAJOR_VERSION < 3) || (PY_VERSION_HEX >= 0x03010300)
657 #define __Pyx_PySequence_GetSlice(obj, a, b) PySequence_GetSlice(obj, a, b)
658 #define __Pyx_PySequence_SetSlice(obj, a, b, value) PySequence_SetSlice(obj, a, b, value)
659 #define __Pyx_PySequence_DelSlice(obj, a, b) PySequence_DelSlice(obj, a, b)
661 #define __Pyx_PySequence_GetSlice(obj, a, b) (unlikely(!(obj)) ? \\
662 (PyErr_SetString(PyExc_SystemError, "null argument to internal routine"), (PyObject*)0) : \\
663 (likely((obj)->ob_type->tp_as_mapping) ? (PySequence_GetSlice(obj, a, b)) : \\
664 (PyErr_Format(PyExc_TypeError, "'%.200s' object is unsliceable", (obj)->ob_type->tp_name), (PyObject*)0)))
665 #define __Pyx_PySequence_SetSlice(obj, a, b, value) (unlikely(!(obj)) ? \\
666 (PyErr_SetString(PyExc_SystemError, "null argument to internal routine"), -1) : \\
667 (likely((obj)->ob_type->tp_as_mapping) ? (PySequence_SetSlice(obj, a, b, value)) : \\
668 (PyErr_Format(PyExc_TypeError, "'%.200s' object doesn't support slice assignment", (obj)->ob_type->tp_name), -1)))
669 #define __Pyx_PySequence_DelSlice(obj, a, b) (unlikely(!(obj)) ? \\
670 (PyErr_SetString(PyExc_SystemError, "null argument to internal routine"), -1) : \\
671 (likely((obj)->ob_type->tp_as_mapping) ? (PySequence_DelSlice(obj, a, b)) : \\
672 (PyErr_Format(PyExc_TypeError, "'%.200s' object doesn't support slice deletion", (obj)->ob_type->tp_name), -1)))
675 #if PY_MAJOR_VERSION >= 3
676 #define PyMethod_New(func, self, klass) ((self) ? PyMethod_New(func, self) : PyInstanceMethod_New(func))
679 #if PY_VERSION_HEX < 0x02050000
680 #define __Pyx_GetAttrString(o,n) PyObject_GetAttrString((o),((char *)(n)))
681 #define __Pyx_SetAttrString(o,n,a) PyObject_SetAttrString((o),((char *)(n)),(a))
682 #define __Pyx_DelAttrString(o,n) PyObject_DelAttrString((o),((char *)(n)))
684 #define __Pyx_GetAttrString(o,n) PyObject_GetAttrString((o),(n))
685 #define __Pyx_SetAttrString(o,n,a) PyObject_SetAttrString((o),(n),(a))
686 #define __Pyx_DelAttrString(o,n) PyObject_DelAttrString((o),(n))
689 #if PY_VERSION_HEX < 0x02050000
690 #define __Pyx_NAMESTR(n) ((char *)(n))
691 #define __Pyx_DOCSTR(n) ((char *)(n))
693 #define __Pyx_NAMESTR(n) (n)
694 #define __Pyx_DOCSTR(n) (n)
699 self.generate_extern_c_macro_definition(code)
701 code.putln("#if defined(WIN32) || defined(MS_WINDOWS)")
702 code.putln("#define _USE_MATH_DEFINES")
704 code.putln("#include <math.h>")
705 code.putln("#define %s" % Naming.h_guard_prefix + self.api_name(env))
706 code.putln("#define %s" % Naming.api_guard_prefix + self.api_name(env))
707 self.generate_includes(env, cimported_modules, code)
709 code.putln("#ifdef PYREX_WITHOUT_ASSERTIONS")
710 code.putln("#define CYTHON_WITHOUT_ASSERTIONS")
713 if env.directives['ccomplex']:
715 code.putln("#if !defined(CYTHON_CCOMPLEX)")
716 code.putln("#define CYTHON_CCOMPLEX 1")
719 code.put(Nodes.utility_function_predeclarations)
720 code.put(PyrexTypes.type_conversion_predeclarations)
721 code.put(Nodes.branch_prediction_macros)
723 code.putln('static PyObject *%s;' % env.module_cname)
724 code.putln('static PyObject *%s;' % Naming.builtins_cname)
725 code.putln('static PyObject *%s;' % Naming.empty_tuple)
726 code.putln('static PyObject *%s;' % Naming.empty_bytes)
727 if Options.pre_import is not None:
728 code.putln('static PyObject *%s;' % Naming.preimport_cname)
729 code.putln('static int %s;' % Naming.lineno_cname)
730 code.putln('static int %s = 0;' % Naming.clineno_cname)
731 code.putln('static const char * %s= %s;' % (Naming.cfilenm_cname, Naming.file_c_macro))
732 code.putln('static const char *%s;' % Naming.filename_cname)
735 for utility_code in PyrexTypes.c_int_from_py_function.specialize_list:
736 env.use_utility_code(utility_code)
737 for utility_code in PyrexTypes.c_long_from_py_function.specialize_list:
738 env.use_utility_code(utility_code)
740 def generate_extern_c_macro_definition(self, code):
741 name = Naming.extern_c_macro
742 code.putln("#ifndef %s" % name)
743 code.putln(" #ifdef __cplusplus")
744 code.putln(' #define %s extern "C"' % name)
746 code.putln(" #define %s extern" % name)
747 code.putln(" #endif")
750 def generate_includes(self, env, cimported_modules, code):
752 for filename in env.include_files:
753 byte_decoded_filenname = str(filename)
754 if byte_decoded_filenname[0] == '<' and byte_decoded_filenname[-1] == '>':
755 code.putln('#include %s' % byte_decoded_filenname)
757 code.putln('#include "%s"' % byte_decoded_filenname)
759 def generate_filename_table(self, code):
761 code.putln("static const char *%s[] = {" % Naming.filetable_cname)
762 if code.globalstate.filename_list:
763 for source_desc in code.globalstate.filename_list:
764 filename = os.path.basename(source_desc.get_filenametable_entry())
765 escaped_filename = filename.replace("\\", "\\\\").replace('"', r'\"')
766 code.putln('"%s",' % escaped_filename)
768 # Some C compilers don't like an empty array
772 def generate_type_predeclarations(self, env, code):
775 def generate_type_header_code(self, type_entries, code):
776 # Generate definitions of structs/unions/enums/typedefs/objstructs.
777 #self.generate_gcc33_hack(env, code) # Is this still needed?
778 #for entry in env.type_entries:
779 for entry in type_entries:
780 if not entry.in_cinclude:
781 #print "generate_type_header_code:", entry.name, repr(entry.type) ###
783 if type.is_typedef: # Must test this first!
784 self.generate_typedef(entry, code)
785 elif type.is_struct_or_union:
786 self.generate_struct_union_definition(entry, code)
788 self.generate_enum_definition(entry, code)
789 elif type.is_extension_type:
790 self.generate_objstruct_definition(type, code)
792 def generate_gcc33_hack(self, env, code):
793 # Workaround for spurious warning generation in gcc 3.3
795 for entry in env.c_class_entries:
797 if not type.typedef_flag:
798 name = type.objstruct_cname
799 if name.startswith("__pyx_"):
803 code.putln("typedef struct %s __pyx_gcc33_%s;" % (
806 def generate_typedef(self, entry, code):
807 base_type = entry.type.typedef_base_type
808 if base_type.is_numeric:
810 writer = code.globalstate['numeric_typedefs']
815 writer.mark_pos(entry.pos)
816 writer.putln("typedef %s;" % base_type.declaration_code(entry.cname))
818 def sue_header_footer(self, type, kind, name):
819 if type.typedef_flag:
820 header = "typedef %s {" % kind
821 footer = "} %s;" % name
823 header = "%s %s {" % (kind, name)
825 return header, footer
827 def generate_struct_union_definition(self, entry, code):
828 code.mark_pos(entry.pos)
833 packed = type.is_struct and type.packed
835 kind = "%s %s" % (type.kind, "__Pyx_PACKED")
836 code.globalstate.use_utility_code(packed_struct_utility_code)
838 self.sue_header_footer(type, kind, type.cname)
840 code.putln("#if defined(__SUNPRO_C)")
841 code.putln(" #pragma pack(1)")
842 code.putln("#elif !defined(__GNUC__)")
843 code.putln(" #pragma pack(push, 1)")
846 var_entries = scope.var_entries
849 "Empty struct or union definition not allowed outside a"
850 " 'cdef extern from' block")
851 for attr in var_entries:
854 attr.type.declaration_code(attr.cname))
857 code.putln("#if defined(__SUNPRO_C)")
858 code.putln(" #pragma pack()")
859 code.putln("#elif !defined(__GNUC__)")
860 code.putln(" #pragma pack(pop)")
863 def generate_enum_definition(self, entry, code):
864 code.mark_pos(entry.pos)
866 name = entry.cname or entry.name or ""
868 self.sue_header_footer(type, "enum", name)
870 enum_values = entry.enum_values
873 "Empty enum definition not allowed outside a"
874 " 'cdef extern from' block")
876 last_entry = enum_values[-1]
877 # this does not really generate code, just builds the result value
878 for value_entry in enum_values:
879 if value_entry.value_node is not None:
880 value_entry.value_node.generate_evaluation_code(code)
882 for value_entry in enum_values:
883 if value_entry.value_node is None:
884 value_code = value_entry.cname
886 value_code = ("%s = %s" % (
888 value_entry.value_node.result()))
889 if value_entry is not last_entry:
891 code.putln(value_code)
894 def generate_typeobj_predeclaration(self, entry, code):
896 name = entry.type.typeobj_cname
898 if entry.visibility == 'extern' and not entry.in_cinclude:
899 code.putln("%s %s %s;" % (
900 Naming.extern_c_macro,
901 PyrexTypes.public_decl("PyTypeObject", "DL_IMPORT"),
903 elif entry.visibility == 'public':
904 code.putln("%s %s %s;" % (
905 Naming.extern_c_macro,
906 PyrexTypes.public_decl("PyTypeObject", "DL_EXPORT"),
908 # ??? Do we really need the rest of this? ???
910 # code.putln("static PyTypeObject %s;" % name)
912 def generate_exttype_vtable_struct(self, entry, code):
913 code.mark_pos(entry.pos)
914 # Generate struct declaration for an extension type's vtable.
917 if type.vtabstruct_cname:
921 type.vtabstruct_cname)
922 if type.base_type and type.base_type.vtabstruct_cname:
923 code.putln("struct %s %s;" % (
924 type.base_type.vtabstruct_cname,
925 Naming.obj_base_cname))
926 for method_entry in scope.cfunc_entries:
927 if not method_entry.is_inherited:
929 "%s;" % method_entry.type.declaration_code("(*%s)" % method_entry.name))
933 def generate_exttype_vtabptr_declaration(self, entry, code):
934 code.mark_pos(entry.pos)
935 # Generate declaration of pointer to an extension type's vtable.
937 if type.vtabptr_cname:
938 code.putln("static struct %s *%s;" % (
939 type.vtabstruct_cname,
942 def generate_objstruct_definition(self, type, code):
943 code.mark_pos(type.pos)
944 # Generate object struct definition for an
947 return # Forward declared but never defined
949 self.sue_header_footer(type, "struct", type.objstruct_cname)
951 base_type = type.base_type
955 ("struct ", "")[base_type.typedef_flag],
956 base_type.objstruct_cname,
957 Naming.obj_base_cname))
961 if type.vtabslot_cname and not (type.base_type and type.base_type.vtabslot_cname):
964 type.vtabstruct_cname,
965 type.vtabslot_cname))
966 for attr in type.scope.var_entries:
967 if attr.is_declared_generic:
968 attr_type = py_object_type
970 attr_type = attr.type
973 attr_type.declaration_code(attr.cname))
975 if type.objtypedef_cname is not None:
976 # Only for exposing public typedef name.
977 code.putln("typedef struct %s %s;" % (type.objstruct_cname, type.objtypedef_cname))
979 def generate_global_declarations(self, env, code, definition):
981 for entry in env.c_class_entries:
982 if definition or entry.defined_in_pxd:
983 code.putln("static PyTypeObject *%s = 0;" %
984 entry.type.typeptr_cname)
985 code.put_var_declarations(env.var_entries, static = 1,
986 dll_linkage = "DL_EXPORT", definition = definition)
988 def generate_cfunction_predeclarations(self, env, code, definition):
989 for entry in env.cfunc_entries:
990 if entry.inline_func_in_pxd or (not entry.in_cinclude and (definition
991 or entry.defined_in_pxd or entry.visibility == 'extern')):
992 if entry.visibility == 'public':
993 storage_class = "%s " % Naming.extern_c_macro
994 dll_linkage = "DL_EXPORT"
995 elif entry.visibility == 'extern':
996 storage_class = "%s " % Naming.extern_c_macro
997 dll_linkage = "DL_IMPORT"
998 elif entry.visibility == 'private':
999 storage_class = "static "
1002 storage_class = "static "
1006 if not definition and entry.defined_in_pxd:
1007 type = CPtrType(type)
1008 header = type.declaration_code(entry.cname,
1009 dll_linkage = dll_linkage)
1010 if entry.func_modifiers:
1011 modifiers = "%s " % ' '.join(entry.func_modifiers).upper()
1014 code.putln("%s%s%s; /*proto*/" % (
1019 def generate_typeobj_definitions(self, env, code):
1020 full_module_name = env.qualified_name
1021 for entry in env.c_class_entries:
1022 #print "generate_typeobj_definitions:", entry.name
1023 #print "...visibility =", entry.visibility
1024 if entry.visibility != 'extern':
1027 if scope: # could be None if there was an error
1028 self.generate_exttype_vtable(scope, code)
1029 self.generate_new_function(scope, code)
1030 self.generate_dealloc_function(scope, code)
1031 if scope.needs_gc():
1032 self.generate_traverse_function(scope, code)
1033 self.generate_clear_function(scope, code)
1034 if scope.defines_any(["__getitem__"]):
1035 self.generate_getitem_int_function(scope, code)
1036 if scope.defines_any(["__setitem__", "__delitem__"]):
1037 self.generate_ass_subscript_function(scope, code)
1038 if scope.defines_any(["__getslice__", "__setslice__", "__delslice__"]):
1039 warning(self.pos, "__getslice__, __setslice__, and __delslice__ are not supported by Python 3, use __getitem__, __setitem__, and __delitem__ instead", 1)
1040 code.putln("#if PY_MAJOR_VERSION >= 3")
1041 code.putln("#error __getslice__, __setslice__, and __delslice__ not supported in Python 3.")
1042 code.putln("#endif")
1043 if scope.defines_any(["__setslice__", "__delslice__"]):
1044 self.generate_ass_slice_function(scope, code)
1045 if scope.defines_any(["__getattr__","__getattribute__"]):
1046 self.generate_getattro_function(scope, code)
1047 if scope.defines_any(["__setattr__", "__delattr__"]):
1048 self.generate_setattro_function(scope, code)
1049 if scope.defines_any(["__get__"]):
1050 self.generate_descr_get_function(scope, code)
1051 if scope.defines_any(["__set__", "__delete__"]):
1052 self.generate_descr_set_function(scope, code)
1053 self.generate_property_accessors(scope, code)
1054 self.generate_method_table(scope, code)
1055 self.generate_getset_table(scope, code)
1056 self.generate_typeobj_definition(full_module_name, entry, code)
1058 def generate_exttype_vtable(self, scope, code):
1059 # Generate the definition of an extension type's vtable.
1060 type = scope.parent_type
1061 if type.vtable_cname:
1062 code.putln("static struct %s %s;" % (
1063 type.vtabstruct_cname,
1066 def generate_self_cast(self, scope, code):
1067 type = scope.parent_type
1070 type.declaration_code("p"),
1071 type.declaration_code("")))
1073 def generate_new_function(self, scope, code):
1074 tp_slot = TypeSlots.ConstructorSlot("tp_new", '__new__')
1075 slot_func = scope.mangle_internal("tp_new")
1076 type = scope.parent_type
1077 base_type = type.base_type
1079 for entry in scope.var_entries:
1080 if entry.type.is_pyobject:
1081 py_attrs.append(entry)
1082 need_self_cast = type.vtabslot_cname or py_attrs
1085 "static PyObject *%s(PyTypeObject *t, PyObject *a, PyObject *k) {"
1086 % scope.mangle_internal("tp_new"))
1090 % scope.parent_type.declaration_code("p"))
1092 tp_new = TypeSlots.get_base_slot_function(scope, tp_slot)
1094 tp_new = "%s->tp_new" % base_type.typeptr_cname
1096 "PyObject *o = %s(t, a, k);" % tp_new)
1099 "PyObject *o = (*t->tp_alloc)(t, 0);")
1101 "if (!o) return 0;")
1105 % type.cast_code("o"))
1107 # self.generate_self_cast(scope, code)
1108 if type.vtabslot_cname:
1109 vtab_base_type = type
1110 while vtab_base_type.base_type and vtab_base_type.base_type.vtabstruct_cname:
1111 vtab_base_type = vtab_base_type.base_type
1112 if vtab_base_type is not type:
1113 struct_type_cast = "(struct %s*)" % vtab_base_type.vtabstruct_cname
1115 struct_type_cast = ""
1116 code.putln("p->%s = %s%s;" % (
1117 type.vtabslot_cname,
1118 struct_type_cast, type.vtabptr_cname))
1119 for entry in py_attrs:
1120 if scope.is_internal or entry.name == "__weakref__":
1121 # internal classes do not need None inits
1122 code.putln("p->%s = 0;" % entry.cname)
1124 code.put_init_var_to_py_none(entry, "p->%s", nanny=False)
1125 entry = scope.lookup_here("__new__")
1126 if entry and entry.is_special:
1127 if entry.trivial_signature:
1128 cinit_args = "o, %s, NULL" % Naming.empty_tuple
1130 cinit_args = "o, a, k"
1132 "if (%s(%s) < 0) {" %
1133 (entry.func_cname, cinit_args))
1134 code.put_decref_clear("o", py_object_type, nanny=False);
1142 def generate_dealloc_function(self, scope, code):
1143 tp_slot = TypeSlots.ConstructorSlot("tp_dealloc", '__dealloc__')
1144 slot_func = scope.mangle_internal("tp_dealloc")
1145 base_type = scope.parent_type.base_type
1146 if tp_slot.slot_code(scope) != slot_func:
1150 "static void %s(PyObject *o) {"
1151 % scope.mangle_internal("tp_dealloc"))
1153 weakref_slot = scope.lookup_here("__weakref__")
1154 for entry in scope.var_entries:
1155 if entry.type.is_pyobject and entry is not weakref_slot:
1156 py_attrs.append(entry)
1157 if py_attrs or weakref_slot in scope.var_entries:
1158 self.generate_self_cast(scope, code)
1159 self.generate_usr_dealloc_call(scope, code)
1160 if weakref_slot in scope.var_entries:
1161 code.putln("if (p->__weakref__) PyObject_ClearWeakRefs(o);")
1162 for entry in py_attrs:
1163 code.put_xdecref("p->%s" % entry.cname, entry.type, nanny=False)
1165 tp_dealloc = TypeSlots.get_base_slot_function(scope, tp_slot)
1166 if tp_dealloc is None:
1167 tp_dealloc = "%s->tp_dealloc" % base_type.typeptr_cname
1169 "%s(o);" % tp_dealloc)
1172 "(*Py_TYPE(o)->tp_free)(o);")
1176 def generate_usr_dealloc_call(self, scope, code):
1177 entry = scope.lookup_here("__dealloc__")
1182 "PyObject *etype, *eval, *etb;")
1184 "PyErr_Fetch(&etype, &eval, &etb);")
1191 "if (PyErr_Occurred()) PyErr_WriteUnraisable(o);")
1195 "PyErr_Restore(etype, eval, etb);")
1199 def generate_traverse_function(self, scope, code):
1200 tp_slot = TypeSlots.GCDependentSlot("tp_traverse")
1201 slot_func = scope.mangle_internal("tp_traverse")
1202 base_type = scope.parent_type.base_type
1203 if tp_slot.slot_code(scope) != slot_func:
1207 "static int %s(PyObject *o, visitproc v, void *a) {"
1210 for entry in scope.var_entries:
1211 if entry.type.is_pyobject and entry.name != "__weakref__":
1212 py_attrs.append(entry)
1213 if base_type or py_attrs:
1214 code.putln("int e;")
1216 self.generate_self_cast(scope, code)
1218 # want to call it explicitly if possible so inlining can be performed
1219 static_call = TypeSlots.get_base_slot_function(scope, tp_slot)
1221 code.putln("e = %s(o, v, a); if (e) return e;" % static_call)
1223 code.putln("if (%s->tp_traverse) {" % base_type.typeptr_cname)
1225 "e = %s->tp_traverse(o, v, a); if (e) return e;" %
1226 base_type.typeptr_cname)
1228 for entry in py_attrs:
1229 var_code = "p->%s" % entry.cname
1233 if entry.type.is_extension_type:
1234 var_code = "((PyObject*)%s)" % var_code
1236 "e = (*v)(%s, a); if (e) return e;"
1245 def generate_clear_function(self, scope, code):
1246 tp_slot = TypeSlots.GCDependentSlot("tp_clear")
1247 slot_func = scope.mangle_internal("tp_clear")
1248 base_type = scope.parent_type.base_type
1249 if tp_slot.slot_code(scope) != slot_func:
1252 code.putln("static int %s(PyObject *o) {" % slot_func)
1254 for entry in scope.var_entries:
1255 if entry.type.is_pyobject and entry.name != "__weakref__":
1256 py_attrs.append(entry)
1258 self.generate_self_cast(scope, code)
1259 code.putln("PyObject* tmp;")
1261 # want to call it explicitly if possible so inlining can be performed
1262 static_call = TypeSlots.get_base_slot_function(scope, tp_slot)
1264 code.putln("%s(o);" % static_call)
1266 code.putln("if (%s->tp_clear) {" % base_type.typeptr_cname)
1267 code.putln("%s->tp_clear(o);" % base_type.typeptr_cname)
1269 for entry in py_attrs:
1270 name = "p->%s" % entry.cname
1271 code.putln("tmp = ((PyObject*)%s);" % name)
1272 if entry.is_declared_generic:
1273 code.put_init_to_py_none(name, py_object_type, nanny=False)
1275 code.put_init_to_py_none(name, entry.type, nanny=False)
1276 code.putln("Py_XDECREF(tmp);")
1282 def generate_getitem_int_function(self, scope, code):
1283 # This function is put into the sq_item slot when
1284 # a __getitem__ method is present. It converts its
1285 # argument to a Python integer and calls mp_subscript.
1287 "static PyObject *%s(PyObject *o, Py_ssize_t i) {" %
1288 scope.mangle_internal("sq_item"))
1292 "PyObject *x = PyInt_FromSsize_t(i); if(!x) return 0;")
1294 "r = Py_TYPE(o)->tp_as_mapping->mp_subscript(o, x);")
1302 def generate_ass_subscript_function(self, scope, code):
1303 # Setting and deleting an item are both done through
1304 # the ass_subscript method, so we dispatch to user's __setitem__
1305 # or __delitem__, or raise an exception.
1306 base_type = scope.parent_type.base_type
1307 set_entry = scope.lookup_here("__setitem__")
1308 del_entry = scope.lookup_here("__delitem__")
1311 "static int %s(PyObject *o, PyObject *i, PyObject *v) {" %
1312 scope.mangle_internal("mp_ass_subscript"))
1317 "return %s(o, i, v);" %
1318 set_entry.func_cname)
1320 self.generate_guarded_basetype_call(
1321 base_type, "tp_as_mapping", "mp_ass_subscript", "o, i, v", code)
1323 "PyErr_Format(PyExc_NotImplementedError,")
1325 ' "Subscript assignment not supported by %s", Py_TYPE(o)->tp_name);')
1334 "return %s(o, i);" %
1335 del_entry.func_cname)
1337 self.generate_guarded_basetype_call(
1338 base_type, "tp_as_mapping", "mp_ass_subscript", "o, i, v", code)
1340 "PyErr_Format(PyExc_NotImplementedError,")
1342 ' "Subscript deletion not supported by %s", Py_TYPE(o)->tp_name);')
1350 def generate_guarded_basetype_call(
1351 self, base_type, substructure, slot, args, code):
1353 base_tpname = base_type.typeptr_cname
1356 "if (%s->%s && %s->%s->%s)" % (
1357 base_tpname, substructure, base_tpname, substructure, slot))
1359 " return %s->%s->%s(%s);" % (
1360 base_tpname, substructure, slot, args))
1366 " return %s->%s(%s);" % (
1367 base_tpname, slot, args))
1369 def generate_ass_slice_function(self, scope, code):
1370 # Setting and deleting a slice are both done through
1371 # the ass_slice method, so we dispatch to user's __setslice__
1372 # or __delslice__, or raise an exception.
1373 base_type = scope.parent_type.base_type
1374 set_entry = scope.lookup_here("__setslice__")
1375 del_entry = scope.lookup_here("__delslice__")
1378 "static int %s(PyObject *o, Py_ssize_t i, Py_ssize_t j, PyObject *v) {" %
1379 scope.mangle_internal("sq_ass_slice"))
1384 "return %s(o, i, j, v);" %
1385 set_entry.func_cname)
1387 self.generate_guarded_basetype_call(
1388 base_type, "tp_as_sequence", "sq_ass_slice", "o, i, j, v", code)
1390 "PyErr_Format(PyExc_NotImplementedError,")
1392 ' "2-element slice assignment not supported by %s", Py_TYPE(o)->tp_name);')
1401 "return %s(o, i, j);" %
1402 del_entry.func_cname)
1404 self.generate_guarded_basetype_call(
1405 base_type, "tp_as_sequence", "sq_ass_slice", "o, i, j, v", code)
1407 "PyErr_Format(PyExc_NotImplementedError,")
1409 ' "2-element slice deletion not supported by %s", Py_TYPE(o)->tp_name);')
1417 def generate_getattro_function(self, scope, code):
1418 # First try to get the attribute using __getattribute__, if defined, or
1419 # PyObject_GenericGetAttr.
1421 # If that raises an AttributeError, call the __getattr__ if defined.
1423 # In both cases, defined can be in this class, or any base class.
1424 def lookup_here_or_base(n,type=None):
1427 type = scope.parent_type
1428 r = type.scope.lookup_here(n)
1430 type.base_type is not None:
1431 return lookup_here_or_base(n,type.base_type)
1434 getattr_entry = lookup_here_or_base("__getattr__")
1435 getattribute_entry = lookup_here_or_base("__getattribute__")
1438 "static PyObject *%s(PyObject *o, PyObject *n) {"
1439 % scope.mangle_internal("tp_getattro"))
1440 if getattribute_entry is not None:
1442 "PyObject *v = %s(o, n);" %
1443 getattribute_entry.func_cname)
1446 "PyObject *v = PyObject_GenericGetAttr(o, n);")
1447 if getattr_entry is not None:
1449 "if (!v && PyErr_ExceptionMatches(PyExc_AttributeError)) {")
1454 getattr_entry.func_cname)
1462 def generate_setattro_function(self, scope, code):
1463 # Setting and deleting an attribute are both done through
1464 # the setattro method, so we dispatch to user's __setattr__
1465 # or __delattr__ or fall back on PyObject_GenericSetAttr.
1466 base_type = scope.parent_type.base_type
1467 set_entry = scope.lookup_here("__setattr__")
1468 del_entry = scope.lookup_here("__delattr__")
1471 "static int %s(PyObject *o, PyObject *n, PyObject *v) {" %
1472 scope.mangle_internal("tp_setattro"))
1477 "return %s(o, n, v);" %
1478 set_entry.func_cname)
1480 self.generate_guarded_basetype_call(
1481 base_type, None, "tp_setattro", "o, n, v", code)
1483 "return PyObject_GenericSetAttr(o, n, v);")
1490 "return %s(o, n);" %
1491 del_entry.func_cname)
1493 self.generate_guarded_basetype_call(
1494 base_type, None, "tp_setattro", "o, n, v", code)
1496 "return PyObject_GenericSetAttr(o, n, 0);")
1502 def generate_descr_get_function(self, scope, code):
1503 # The __get__ function of a descriptor object can be
1504 # called with NULL for the second or third arguments
1505 # under some circumstances, so we replace them with
1506 # None in that case.
1507 user_get_entry = scope.lookup_here("__get__")
1510 "static PyObject *%s(PyObject *o, PyObject *i, PyObject *c) {" %
1511 scope.mangle_internal("tp_descr_get"))
1515 "if (!i) i = Py_None;")
1517 "if (!c) c = Py_None;")
1518 #code.put_incref("i", py_object_type)
1519 #code.put_incref("c", py_object_type)
1521 "r = %s(o, i, c);" %
1522 user_get_entry.func_cname)
1523 #code.put_decref("i", py_object_type)
1524 #code.put_decref("c", py_object_type)
1530 def generate_descr_set_function(self, scope, code):
1531 # Setting and deleting are both done through the __set__
1532 # method of a descriptor, so we dispatch to user's __set__
1533 # or __delete__ or raise an exception.
1534 base_type = scope.parent_type.base_type
1535 user_set_entry = scope.lookup_here("__set__")
1536 user_del_entry = scope.lookup_here("__delete__")
1539 "static int %s(PyObject *o, PyObject *i, PyObject *v) {" %
1540 scope.mangle_internal("tp_descr_set"))
1545 "return %s(o, i, v);" %
1546 user_set_entry.func_cname)
1548 self.generate_guarded_basetype_call(
1549 base_type, None, "tp_descr_set", "o, i, v", code)
1551 'PyErr_SetString(PyExc_NotImplementedError, "__set__");')
1560 "return %s(o, i);" %
1561 user_del_entry.func_cname)
1563 self.generate_guarded_basetype_call(
1564 base_type, None, "tp_descr_set", "o, i, v", code)
1566 'PyErr_SetString(PyExc_NotImplementedError, "__delete__");')
1574 def generate_property_accessors(self, cclass_scope, code):
1575 for entry in cclass_scope.property_entries:
1576 property_scope = entry.scope
1577 if property_scope.defines_any(["__get__"]):
1578 self.generate_property_get_function(entry, code)
1579 if property_scope.defines_any(["__set__", "__del__"]):
1580 self.generate_property_set_function(entry, code)
1582 def generate_property_get_function(self, property_entry, code):
1583 property_scope = property_entry.scope
1584 property_entry.getter_cname = property_scope.parent_scope.mangle(
1585 Naming.prop_get_prefix, property_entry.name)
1586 get_entry = property_scope.lookup_here("__get__")
1589 "static PyObject *%s(PyObject *o, void *x) {" %
1590 property_entry.getter_cname)
1593 get_entry.func_cname)
1597 def generate_property_set_function(self, property_entry, code):
1598 property_scope = property_entry.scope
1599 property_entry.setter_cname = property_scope.parent_scope.mangle(
1600 Naming.prop_set_prefix, property_entry.name)
1601 set_entry = property_scope.lookup_here("__set__")
1602 del_entry = property_scope.lookup_here("__del__")
1605 "static int %s(PyObject *o, PyObject *v, void *x) {" %
1606 property_entry.setter_cname)
1611 "return %s(o, v);" %
1612 set_entry.func_cname)
1615 'PyErr_SetString(PyExc_NotImplementedError, "__set__");')
1625 del_entry.func_cname)
1628 'PyErr_SetString(PyExc_NotImplementedError, "__del__");')
1636 def generate_typeobj_definition(self, modname, entry, code):
1639 for suite in TypeSlots.substructures:
1640 suite.generate_substructure(scope, code)
1642 if entry.visibility == 'public':
1643 header = "DL_EXPORT(PyTypeObject) %s = {"
1645 header = "static PyTypeObject %s = {"
1646 #code.putln(header % scope.parent_type.typeobj_cname)
1647 code.putln(header % type.typeobj_cname)
1649 "PyVarObject_HEAD_INIT(0, 0)")
1651 '__Pyx_NAMESTR("%s.%s"), /*tp_name*/' % (
1652 self.full_module_name, scope.class_name))
1653 if type.typedef_flag:
1654 objstruct = type.objstruct_cname
1656 objstruct = "struct %s" % type.objstruct_cname
1658 "sizeof(%s), /*tp_basicsize*/" %
1661 "0, /*tp_itemsize*/")
1662 for slot in TypeSlots.slot_table:
1663 slot.generate(scope, code)
1667 def generate_method_table(self, env, code):
1670 "static PyMethodDef %s[] = {" %
1671 env.method_table_cname)
1672 for entry in env.pyfunc_entries:
1673 code.put_pymethoddef(entry, ",")
1679 def generate_getset_table(self, env, code):
1680 if env.property_entries:
1683 "static struct PyGetSetDef %s[] = {" %
1684 env.getset_table_cname)
1685 for entry in env.property_entries:
1687 doc_code = "__Pyx_DOCSTR(%s)" % code.get_string_const(entry.doc)
1691 '{(char *)"%s", %s, %s, %s, 0},' % (
1693 entry.getter_cname or "0",
1694 entry.setter_cname or "0",
1701 def generate_import_star(self, env, code):
1702 env.use_utility_code(streq_utility_code)
1704 code.putln("char* %s_type_names[] = {" % Naming.import_star)
1705 for name, entry in env.entries.items():
1707 code.putln('"%s",' % name)
1711 code.enter_cfunc_scope() # as we need labels
1712 code.putln("static int %s(PyObject *o, PyObject* py_name, char *name) {" % Naming.import_star_set)
1713 code.putln("char** type_name = %s_type_names;" % Naming.import_star)
1714 code.putln("while (*type_name) {")
1715 code.putln("if (__Pyx_StrEq(name, *type_name)) {")
1716 code.putln('PyErr_Format(PyExc_TypeError, "Cannot overwrite C type %s", name);')
1717 code.putln('goto bad;')
1719 code.putln("type_name++;")
1721 old_error_label = code.new_error_label()
1722 code.putln("if (0);") # so the first one can be "else if"
1723 for name, entry in env.entries.items():
1724 if entry.is_cglobal and entry.used:
1725 code.putln('else if (__Pyx_StrEq(name, "%s")) {' % name)
1726 if entry.type.is_pyobject:
1727 if entry.type.is_extension_type or entry.type.is_builtin_type:
1728 code.putln("if (!(%s)) %s;" % (
1729 entry.type.type_test_code("o"),
1730 code.error_goto(entry.pos)))
1731 code.putln("Py_INCREF(o);")
1732 code.put_decref(entry.cname, entry.type, nanny=False)
1733 code.putln("%s = %s;" % (
1735 PyrexTypes.typecast(entry.type, py_object_type, "o")))
1736 elif entry.type.from_py_function:
1737 rhs = "%s(o)" % entry.type.from_py_function
1738 if entry.type.is_enum:
1739 rhs = PyrexTypes.typecast(entry.type, PyrexTypes.c_long_type, rhs)
1740 code.putln("%s = %s; if (%s) %s;" % (
1743 entry.type.error_condition(entry.cname),
1744 code.error_goto(entry.pos)))
1746 code.putln('PyErr_Format(PyExc_TypeError, "Cannot convert Python object %s to %s");' % (name, entry.type))
1747 code.putln(code.error_goto(entry.pos))
1749 code.putln("else {")
1750 code.putln("if (PyObject_SetAttr(%s, py_name, o) < 0) goto bad;" % Naming.module_cname)
1752 code.putln("return 0;")
1753 if code.label_used(code.error_label):
1754 code.put_label(code.error_label)
1755 # This helps locate the offending name.
1756 code.putln('__Pyx_AddTraceback("%s");' % self.full_module_name);
1757 code.error_label = old_error_label
1759 code.putln("return -1;")
1761 code.putln(import_star_utility_code)
1762 code.exit_cfunc_scope() # done with labels
1764 def generate_module_init_func(self, imported_modules, env, code):
1765 code.enter_cfunc_scope()
1767 header2 = "PyMODINIT_FUNC init%s(void)" % env.module_name
1768 header3 = "PyMODINIT_FUNC PyInit_%s(void)" % env.module_name
1769 code.putln("#if PY_MAJOR_VERSION < 3")
1770 code.putln("%s; /*proto*/" % header2)
1773 code.putln("%s; /*proto*/" % header3)
1775 code.putln("#endif")
1777 tempdecl_code = code.insertion_point()
1779 code.put_declare_refcount_context()
1780 code.putln("#if CYTHON_REFNANNY")
1781 code.putln("__Pyx_RefNanny = __Pyx_RefNannyImportAPI(\"refnanny\");")
1782 code.putln("if (!__Pyx_RefNanny) {")
1783 code.putln(" PyErr_Clear();")
1784 code.putln(" __Pyx_RefNanny = __Pyx_RefNannyImportAPI(\"Cython.Runtime.refnanny\");")
1785 code.putln(" if (!__Pyx_RefNanny)")
1786 code.putln(" Py_FatalError(\"failed to import 'refnanny' module\");")
1788 code.putln("#endif")
1789 code.put_setup_refcount_context(header3)
1791 env.use_utility_code(check_binary_version_utility_code)
1792 code.putln("if ( __Pyx_check_binary_version() < 0) %s" % code.error_goto(self.pos))
1794 code.putln("%s = PyTuple_New(0); %s" % (Naming.empty_tuple, code.error_goto_if_null(Naming.empty_tuple, self.pos)));
1795 code.putln("%s = PyBytes_FromStringAndSize(\"\", 0); %s" % (Naming.empty_bytes, code.error_goto_if_null(Naming.empty_bytes, self.pos)));
1797 code.putln("#ifdef %s_USED" % Naming.binding_cfunc)
1798 code.putln("if (%s_init() < 0) %s" % (Naming.binding_cfunc, code.error_goto(self.pos)))
1799 code.putln("#endif")
1801 code.putln("/*--- Library function declarations ---*/")
1802 env.generate_library_function_declarations(code)
1804 code.putln("/*--- Threads initialization code ---*/")
1805 code.putln("#if defined(__PYX_FORCE_INIT_THREADS) && __PYX_FORCE_INIT_THREADS")
1806 code.putln("#ifdef WITH_THREAD /* Python build with threading support? */")
1807 code.putln("PyEval_InitThreads();")
1808 code.putln("#endif")
1809 code.putln("#endif")
1811 code.putln("/*--- Module creation code ---*/")
1812 self.generate_module_creation_code(env, code)
1814 code.putln("/*--- Initialize various global constants etc. ---*/")
1815 code.putln(code.error_goto_if_neg("__Pyx_InitGlobals()", self.pos))
1817 __main__name = code.globalstate.get_py_string_const(
1818 EncodedString("__main__"), identifier=True)
1819 code.putln("if (%s%s) {" % (Naming.module_is_main, self.full_module_name.replace('.', '__')))
1821 'if (__Pyx_SetAttrString(%s, "__name__", %s) < 0) %s;' % (
1824 code.error_goto(self.pos)))
1827 if Options.cache_builtins:
1828 code.putln("/*--- Builtin init code ---*/")
1829 code.putln(code.error_goto_if_neg("__Pyx_InitCachedBuiltins()", self.pos))
1831 code.putln("/*--- Constants init code ---*/")
1832 code.putln(code.error_goto_if_neg("__Pyx_InitCachedConstants()", self.pos))
1834 code.putln("/*--- Global init code ---*/")
1835 self.generate_global_init_code(env, code)
1837 code.putln("/*--- Variable export code ---*/")
1838 self.generate_c_variable_export_code(env, code)
1840 code.putln("/*--- Function export code ---*/")
1841 self.generate_c_function_export_code(env, code)
1843 code.putln("/*--- Type init code ---*/")
1844 self.generate_type_init_code(env, code)
1846 code.putln("/*--- Type import code ---*/")
1847 for module in imported_modules:
1848 self.generate_type_import_code_for_module(module, env, code)
1850 code.putln("/*--- Function import code ---*/")
1851 for module in imported_modules:
1852 self.generate_c_function_import_code_for_module(module, env, code)
1854 code.putln("/*--- Execution code ---*/")
1857 self.body.generate_execution_code(code)
1859 if Options.generate_cleanup_code:
1860 # this should be replaced by the module's tp_clear in Py3
1861 env.use_utility_code(import_module_utility_code)
1862 code.putln("if (__Pyx_RegisterCleanup()) %s;" % code.error_goto(self.pos))
1864 code.put_goto(code.return_label)
1865 code.put_label(code.error_label)
1866 for cname, type in code.funcstate.all_managed_temps():
1867 code.put_xdecref(cname, type)
1868 code.putln('if (%s) {' % env.module_cname)
1869 code.putln('__Pyx_AddTraceback("init %s");' % env.qualified_name)
1870 env.use_utility_code(Nodes.traceback_utility_code)
1871 code.put_decref_clear(env.module_cname, py_object_type, nanny=False)
1872 code.putln('} else if (!PyErr_Occurred()) {')
1873 code.putln('PyErr_SetString(PyExc_ImportError, "init %s");' % env.qualified_name)
1875 code.put_label(code.return_label)
1877 code.put_finish_refcount_context()
1879 code.putln("#if PY_MAJOR_VERSION < 3")
1880 code.putln("return;")
1882 code.putln("return %s;" % env.module_cname)
1883 code.putln("#endif")
1886 tempdecl_code.put_temp_declarations(code.funcstate)
1888 code.exit_cfunc_scope()
1890 def generate_module_cleanup_func(self, env, code):
1891 if not Options.generate_cleanup_code:
1893 code.globalstate.use_utility_code(register_cleanup_utility_code)
1894 code.putln('static PyObject *%s(CYTHON_UNUSED PyObject *self, CYTHON_UNUSED PyObject *unused) {' %
1895 Naming.cleanup_cname)
1896 if Options.generate_cleanup_code >= 2:
1897 code.putln("/*--- Global cleanup code ---*/")
1898 rev_entries = list(env.var_entries)
1899 rev_entries.reverse()
1900 for entry in rev_entries:
1901 if entry.visibility != 'extern':
1902 if entry.type.is_pyobject and entry.used:
1903 code.putln("Py_DECREF(%s); %s = 0;" % (
1904 code.entry_as_pyobject(entry), entry.cname))
1905 code.putln("__Pyx_CleanupGlobals();")
1906 if Options.generate_cleanup_code >= 3:
1907 code.putln("/*--- Type import cleanup code ---*/")
1908 for type, _ in env.types_imported.items():
1909 code.putln("Py_DECREF((PyObject *)%s);" % type.typeptr_cname)
1910 if Options.cache_builtins:
1911 code.putln("/*--- Builtin cleanup code ---*/")
1912 for entry in env.cached_builtins:
1913 code.put_decref_clear(entry.cname,
1914 PyrexTypes.py_object_type,
1916 code.putln("/*--- Intern cleanup code ---*/")
1917 code.put_decref_clear(Naming.empty_tuple,
1918 PyrexTypes.py_object_type,
1920 # for entry in env.pynum_entries:
1921 # code.put_decref_clear(entry.cname,
1922 # PyrexTypes.py_object_type,
1924 # for entry in env.all_pystring_entries:
1925 # if entry.is_interned:
1926 # code.put_decref_clear(entry.pystring_cname,
1927 # PyrexTypes.py_object_type,
1929 # for entry in env.default_entries:
1930 # if entry.type.is_pyobject and entry.used:
1931 # code.putln("Py_DECREF(%s); %s = 0;" % (
1932 # code.entry_as_pyobject(entry), entry.cname))
1933 code.putln("Py_INCREF(Py_None); return Py_None;")
1935 def generate_main_method(self, env, code):
1936 module_is_main = "%s%s" % (Naming.module_is_main, self.full_module_name.replace('.', '__'))
1937 if Options.embed == "main":
1940 wmain = Options.embed
1941 code.globalstate.use_utility_code(
1942 main_method.specialize(
1943 module_name = env.module_name,
1944 module_is_main = module_is_main,
1945 main_method = Options.embed,
1946 wmain_method = wmain))
1948 def generate_pymoduledef_struct(self, env, code):
1950 doc = "__Pyx_DOCSTR(%s)" % code.get_string_const(env.doc)
1954 code.putln("#if PY_MAJOR_VERSION >= 3")
1955 code.putln("static struct PyModuleDef %s = {" % Naming.pymoduledef_cname)
1956 code.putln(" PyModuleDef_HEAD_INIT,")
1957 code.putln(' __Pyx_NAMESTR("%s"),' % env.module_name)
1958 code.putln(" %s, /* m_doc */" % doc)
1959 code.putln(" -1, /* m_size */")
1960 code.putln(" %s /* m_methods */," % env.method_table_cname)
1961 code.putln(" NULL, /* m_reload */")
1962 code.putln(" NULL, /* m_traverse */")
1963 code.putln(" NULL, /* m_clear */")
1964 code.putln(" NULL /* m_free */")
1966 code.putln("#endif")
1968 def generate_module_creation_code(self, env, code):
1969 # Generate code to create the module object and
1970 # install the builtins.
1972 doc = "__Pyx_DOCSTR(%s)" % code.get_string_const(env.doc)
1975 code.putln("#if PY_MAJOR_VERSION < 3")
1977 '%s = Py_InitModule4(__Pyx_NAMESTR("%s"), %s, %s, 0, PYTHON_API_VERSION);' % (
1980 env.method_table_cname,
1984 "%s = PyModule_Create(&%s);" % (
1986 Naming.pymoduledef_cname))
1987 code.putln("#endif")
1991 code.error_goto(self.pos)));
1992 code.putln("#if PY_MAJOR_VERSION < 3")
1996 code.putln("#endif")
1998 '%s = PyImport_AddModule(__Pyx_NAMESTR(__Pyx_BUILTIN_MODULE_NAME));' %
1999 Naming.builtins_cname)
2002 Naming.builtins_cname,
2003 code.error_goto(self.pos)));
2005 'if (__Pyx_SetAttrString(%s, "__builtins__", %s) < 0) %s;' % (
2007 Naming.builtins_cname,
2008 code.error_goto(self.pos)))
2009 if Options.pre_import is not None:
2011 '%s = PyImport_AddModule(__Pyx_NAMESTR("%s"));' % (
2012 Naming.preimport_cname,
2013 Options.pre_import))
2016 Naming.preimport_cname,
2017 code.error_goto(self.pos)));
2019 def generate_global_init_code(self, env, code):
2020 # Generate code to initialise global PyObject *
2021 # variables to None.
2022 for entry in env.var_entries:
2023 if entry.visibility != 'extern':
2024 if entry.type.is_pyobject and entry.used:
2025 code.put_init_var_to_py_none(entry, nanny=False)
2027 def generate_c_variable_export_code(self, env, code):
2028 # Generate code to create PyCFunction wrappers for exported C functions.
2029 for entry in env.var_entries:
2030 if entry.api or entry.defined_in_pxd:
2031 env.use_utility_code(voidptr_export_utility_code)
2032 signature = entry.type.declaration_code("")
2033 code.putln('if (__Pyx_ExportVoidPtr("%s", (void *)&%s, "%s") < 0) %s' % (
2037 code.error_goto(self.pos)))
2039 def generate_c_function_export_code(self, env, code):
2040 # Generate code to create PyCFunction wrappers for exported C functions.
2041 for entry in env.cfunc_entries:
2042 if entry.api or entry.defined_in_pxd:
2043 env.use_utility_code(function_export_utility_code)
2044 signature = entry.type.signature_string()
2045 code.putln('if (__Pyx_ExportFunction("%s", (void (*)(void))%s, "%s") < 0) %s' % (
2049 code.error_goto(self.pos)))
2051 def generate_type_import_code_for_module(self, module, env, code):
2052 # Generate type import code for all exported extension types in
2053 # an imported module.
2054 #if module.c_class_entries:
2055 for entry in module.c_class_entries:
2056 if entry.defined_in_pxd:
2057 self.generate_type_import_code(env, entry.type, entry.pos, code)
2059 def generate_c_function_import_code_for_module(self, module, env, code):
2060 # Generate import code for all exported C functions in a cimported module.
2062 for entry in module.cfunc_entries:
2063 if entry.defined_in_pxd:
2064 entries.append(entry)
2066 env.use_utility_code(import_module_utility_code)
2067 env.use_utility_code(function_import_utility_code)
2068 temp = code.funcstate.allocate_temp(py_object_type, manage_ref=True)
2070 '%s = __Pyx_ImportModule("%s"); if (!%s) %s' % (
2072 module.qualified_name,
2074 code.error_goto(self.pos)))
2075 for entry in entries:
2077 'if (__Pyx_ImportFunction(%s, "%s", (void (**)(void))&%s, "%s") < 0) %s' % (
2081 entry.type.signature_string(),
2082 code.error_goto(self.pos)))
2083 code.putln("Py_DECREF(%s); %s = 0;" % (temp, temp))
2085 def generate_type_init_code(self, env, code):
2086 # Generate type import code for extern extension types
2087 # and type ready code for non-extern ones.
2088 for entry in env.c_class_entries:
2089 if entry.visibility == 'extern':
2090 self.generate_type_import_code(env, entry.type, entry.pos, code)
2092 self.generate_base_type_import_code(env, entry, code)
2093 self.generate_exttype_vtable_init_code(entry, code)
2094 self.generate_type_ready_code(env, entry, code)
2095 self.generate_typeptr_assignment_code(entry, code)
2097 def generate_base_type_import_code(self, env, entry, code):
2098 base_type = entry.type.base_type
2099 if base_type and base_type.module_name != env.qualified_name \
2100 and not base_type.is_builtin_type:
2101 self.generate_type_import_code(env, base_type, self.pos, code)
2103 def use_type_import_utility_code(self, env):
2104 env.use_utility_code(type_import_utility_code)
2105 env.use_utility_code(import_module_utility_code)
2107 def generate_type_import_code(self, env, type, pos, code):
2108 # If not already done, generate code to import the typeobject of an
2109 # extension type defined in another module, and extract its C method
2110 # table pointer if any.
2111 if type in env.types_imported:
2113 if type.typedef_flag:
2114 objstruct = type.objstruct_cname
2116 objstruct = "struct %s" % type.objstruct_cname
2117 self.generate_type_import_call(type, code,
2118 code.error_goto_if_null(type.typeptr_cname, pos))
2119 self.use_type_import_utility_code(env)
2120 if type.vtabptr_cname:
2121 env.use_utility_code(Nodes.get_vtable_utility_code)
2122 code.putln("%s = (struct %s*)__Pyx_GetVtable(%s->tp_dict); %s" % (
2124 type.vtabstruct_cname,
2126 code.error_goto_if_null(type.vtabptr_cname, pos)))
2127 env.types_imported[type] = 1
2129 py3_type_name_map = {'str' : 'bytes', 'unicode' : 'str'}
2131 def generate_type_import_call(self, type, code, error_code):
2132 if type.typedef_flag:
2133 objstruct = type.objstruct_cname
2135 objstruct = "struct %s" % type.objstruct_cname
2136 module_name = type.module_name
2137 if module_name not in ('__builtin__', 'builtins'):
2138 module_name = '"%s"' % module_name
2140 module_name = '__Pyx_BUILTIN_MODULE_NAME'
2141 if type.name in self.py3_type_name_map:
2142 code.putln("#if PY_MAJOR_VERSION >= 3")
2143 code.putln('%s = __Pyx_ImportType(%s, "%s", sizeof(%s), 1); %s' % (
2146 self.py3_type_name_map[type.name],
2150 code.putln('%s = __Pyx_ImportType(%s, "%s", sizeof(%s), %i); %s' % (
2155 not type.is_external or type.is_subclassed,
2157 if type.name in self.py3_type_name_map:
2158 code.putln("#endif")
2160 def generate_type_ready_code(self, env, entry, code):
2161 # Generate a call to PyType_Ready for an extension
2162 # type defined in this module.
2164 typeobj_cname = type.typeobj_cname
2166 if scope: # could be None if there was an error
2167 if entry.visibility != 'extern':
2168 for slot in TypeSlots.slot_table:
2169 slot.generate_dynamic_init_code(scope, code)
2171 "if (PyType_Ready(&%s) < 0) %s" % (
2173 code.error_goto(entry.pos)))
2174 # Fix special method docstrings. This is a bit of a hack, but
2175 # unless we let PyType_Ready create the slot wrappers we have
2176 # a significant performance hit. (See trac #561.)
2177 for func in entry.type.scope.pyfunc_entries:
2178 if func.is_special and Options.docstrings and func.wrapperbase_cname:
2181 'PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&%s, "%s"); %s' % (
2184 code.error_goto_if_null('wrapper', entry.pos)))
2186 "if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) {")
2188 "%s = *((PyWrapperDescrObject *)wrapper)->d_base;" % (
2189 func.wrapperbase_cname))
2191 "%s.doc = %s;" % (func.wrapperbase_cname, func.doc_cname))
2193 "((PyWrapperDescrObject *)wrapper)->d_base = &%s;" % (
2194 func.wrapperbase_cname))
2197 if type.vtable_cname:
2199 "if (__Pyx_SetVtable(%s.tp_dict, %s) < 0) %s" % (
2202 code.error_goto(entry.pos)))
2203 env.use_utility_code(Nodes.set_vtable_utility_code)
2204 if not type.scope.is_internal and not type.scope.directives['internal']:
2205 # scope.is_internal is set for types defined by
2206 # Cython (such as closures), the 'internal'
2207 # directive is set by users
2209 'if (__Pyx_SetAttrString(%s, "%s", (PyObject *)&%s) < 0) %s' % (
2210 Naming.module_cname,
2213 code.error_goto(entry.pos)))
2214 weakref_entry = scope.lookup_here("__weakref__")
2216 if weakref_entry.type is py_object_type:
2217 tp_weaklistoffset = "%s.tp_weaklistoffset" % typeobj_cname
2218 if type.typedef_flag:
2219 objstruct = type.objstruct_cname
2221 objstruct = "struct %s" % type.objstruct_cname
2222 code.putln("if (%s == 0) %s = offsetof(%s, %s);" % (
2226 weakref_entry.cname))
2228 error(weakref_entry.pos, "__weakref__ slot must be of type 'object'")
2230 def generate_exttype_vtable_init_code(self, entry, code):
2231 # Generate code to initialise the C method table of an
2234 if type.vtable_cname:
2239 if type.base_type and type.base_type.vtabptr_cname:
2243 Naming.obj_base_cname,
2244 type.base_type.vtabptr_cname))
2246 c_method_entries = [
2247 entry for entry in type.scope.cfunc_entries
2248 if entry.func_cname ]
2249 if c_method_entries:
2250 for meth_entry in c_method_entries:
2251 cast = meth_entry.type.signature_cast_string()
2257 meth_entry.func_cname))
2259 def generate_typeptr_assignment_code(self, entry, code):
2260 # Generate code to initialise the typeptr of an extension
2261 # type defined in this module to point to its type object.
2263 if type.typeobj_cname:
2266 type.typeptr_cname, type.typeobj_cname))
2268 #------------------------------------------------------------------------------------
2270 # Runtime support code
2272 #------------------------------------------------------------------------------------
2274 streq_utility_code = UtilityCode(
2276 static CYTHON_INLINE int __Pyx_StrEq(const char *, const char *); /*proto*/
2279 static CYTHON_INLINE int __Pyx_StrEq(const char *s1, const char *s2) {
2280 while (*s1 != '\\0' && *s1 == *s2) { s1++; s2++; }
2285 #------------------------------------------------------------------------------------
2287 import_module_utility_code = UtilityCode(
2289 static PyObject *__Pyx_ImportModule(const char *name); /*proto*/
2292 #ifndef __PYX_HAVE_RT_ImportModule
2293 #define __PYX_HAVE_RT_ImportModule
2294 static PyObject *__Pyx_ImportModule(const char *name) {
2295 PyObject *py_name = 0;
2296 PyObject *py_module = 0;
2298 #if PY_MAJOR_VERSION < 3
2299 py_name = PyString_FromString(name);
2301 py_name = PyUnicode_FromString(name);
2305 py_module = PyImport_Import(py_name);
2309 Py_XDECREF(py_name);
2315 #------------------------------------------------------------------------------------
2317 type_import_utility_code = UtilityCode(
2319 static PyTypeObject *__Pyx_ImportType(const char *module_name, const char *class_name, long size, int strict); /*proto*/
2322 #ifndef __PYX_HAVE_RT_ImportType
2323 #define __PYX_HAVE_RT_ImportType
2324 static PyTypeObject *__Pyx_ImportType(const char *module_name, const char *class_name,
2325 long size, int strict)
2327 PyObject *py_module = 0;
2328 PyObject *result = 0;
2329 PyObject *py_name = 0;
2332 py_module = __Pyx_ImportModule(module_name);
2335 #if PY_MAJOR_VERSION < 3
2336 py_name = PyString_FromString(class_name);
2338 py_name = PyUnicode_FromString(class_name);
2342 result = PyObject_GetAttr(py_module, py_name);
2345 Py_DECREF(py_module);
2349 if (!PyType_Check(result)) {
2350 PyErr_Format(PyExc_TypeError,
2351 "%s.%s is not a type object",
2352 module_name, class_name);
2355 if (!strict && ((PyTypeObject *)result)->tp_basicsize > size) {
2356 PyOS_snprintf(warning, sizeof(warning),
2357 "%s.%s size changed, may indicate binary incompatibility",
2358 module_name, class_name);
2359 #if PY_VERSION_HEX < 0x02050000
2360 PyErr_Warn(NULL, warning);
2362 PyErr_WarnEx(NULL, warning, 0);
2365 else if (((PyTypeObject *)result)->tp_basicsize != size) {
2366 PyErr_Format(PyExc_ValueError,
2367 "%s.%s has the wrong size, try recompiling",
2368 module_name, class_name);
2371 return (PyTypeObject *)result;
2373 Py_XDECREF(py_module);
2380 #------------------------------------------------------------------------------------
2382 voidptr_export_utility_code = UtilityCode(
2384 static int __Pyx_ExportVoidPtr(const char *name, void *p, const char *sig); /*proto*/
2387 static int __Pyx_ExportVoidPtr(const char *name, void *p, const char *sig) {
2391 d = PyObject_GetAttrString(%(MODULE)s, (char *)"%(API)s");
2398 if (PyModule_AddObject(%(MODULE)s, (char *)"%(API)s", d) < 0)
2401 #if PY_VERSION_HEX >= 0x02070000 && !(PY_MAJOR_VERSION==3&&PY_MINOR_VERSION==0)
2402 cobj = PyCapsule_New(p, sig, 0);
2404 cobj = PyCObject_FromVoidPtrAndDesc(p, (void *)sig, 0);
2408 if (PyDict_SetItemString(d, name, cobj) < 0)
2418 """ % {'MODULE': Naming.module_cname, 'API': Naming.api_name}
2421 function_export_utility_code = UtilityCode(
2423 static int __Pyx_ExportFunction(const char *name, void (*f)(void), const char *sig); /*proto*/
2426 static int __Pyx_ExportFunction(const char *name, void (*f)(void), const char *sig) {
2434 d = PyObject_GetAttrString(%(MODULE)s, (char *)"%(API)s");
2441 if (PyModule_AddObject(%(MODULE)s, (char *)"%(API)s", d) < 0)
2445 #if PY_VERSION_HEX >= 0x02070000 && !(PY_MAJOR_VERSION==3&&PY_MINOR_VERSION==0)
2446 cobj = PyCapsule_New(tmp.p, sig, 0);
2448 cobj = PyCObject_FromVoidPtrAndDesc(tmp.p, (void *)sig, 0);
2452 if (PyDict_SetItemString(d, name, cobj) < 0)
2462 """ % {'MODULE': Naming.module_cname, 'API': Naming.api_name}
2465 voidptr_import_utility_code = UtilityCode(
2467 static int __Pyx_ImportVoidPtr(PyObject *module, const char *name, void **p, const char *sig); /*proto*/
2470 #ifndef __PYX_HAVE_RT_ImportVoidPtr
2471 #define __PYX_HAVE_RT_ImportVoidPtr
2472 static int __Pyx_ImportVoidPtr(PyObject *module, const char *name, void **p, const char *sig) {
2476 d = PyObject_GetAttrString(module, (char *)"%(API)s");
2479 cobj = PyDict_GetItemString(d, name);
2481 PyErr_Format(PyExc_ImportError,
2482 "%%s does not export expected C variable %%s",
2483 PyModule_GetName(module), name);
2486 #if PY_VERSION_HEX >= 0x02070000 && !(PY_MAJOR_VERSION==3&&PY_MINOR_VERSION==0)
2487 if (!PyCapsule_IsValid(cobj, sig)) {
2488 PyErr_Format(PyExc_TypeError,
2489 "C variable %%s.%%s has wrong signature (expected %%s, got %%s)",
2490 PyModule_GetName(module), name, sig, PyCapsule_GetName(cobj));
2493 *p = PyCapsule_GetPointer(cobj, sig);
2495 {const char *desc, *s1, *s2;
2496 desc = (const char *)PyCObject_GetDesc(cobj);
2499 s1 = desc; s2 = sig;
2500 while (*s1 != '\\0' && *s1 == *s2) { s1++; s2++; }
2502 PyErr_Format(PyExc_TypeError,
2503 "C variable %%s.%%s has wrong signature (expected %%s, got %%s)",
2504 PyModule_GetName(module), name, sig, desc);
2507 *p = PyCObject_AsVoidPtr(cobj);}
2518 """ % dict(API = Naming.api_name)
2521 function_import_utility_code = UtilityCode(
2523 static int __Pyx_ImportFunction(PyObject *module, const char *funcname, void (**f)(void), const char *sig); /*proto*/
2526 #ifndef __PYX_HAVE_RT_ImportFunction
2527 #define __PYX_HAVE_RT_ImportFunction
2528 static int __Pyx_ImportFunction(PyObject *module, const char *funcname, void (**f)(void), const char *sig) {
2536 d = PyObject_GetAttrString(module, (char *)"%(API)s");
2539 cobj = PyDict_GetItemString(d, funcname);
2541 PyErr_Format(PyExc_ImportError,
2542 "%%s does not export expected C function %%s",
2543 PyModule_GetName(module), funcname);
2546 #if PY_VERSION_HEX >= 0x02070000 && !(PY_MAJOR_VERSION==3&&PY_MINOR_VERSION==0)
2547 if (!PyCapsule_IsValid(cobj, sig)) {
2548 PyErr_Format(PyExc_TypeError,
2549 "C function %%s.%%s has wrong signature (expected %%s, got %%s)",
2550 PyModule_GetName(module), funcname, sig, PyCapsule_GetName(cobj));
2553 tmp.p = PyCapsule_GetPointer(cobj, sig);
2555 {const char *desc, *s1, *s2;
2556 desc = (const char *)PyCObject_GetDesc(cobj);
2559 s1 = desc; s2 = sig;
2560 while (*s1 != '\\0' && *s1 == *s2) { s1++; s2++; }
2562 PyErr_Format(PyExc_TypeError,
2563 "C function %%s.%%s has wrong signature (expected %%s, got %%s)",
2564 PyModule_GetName(module), funcname, sig, desc);
2567 tmp.p = PyCObject_AsVoidPtr(cobj);}
2579 """ % dict(API = Naming.api_name)
2582 #------------------------------------------------------------------------------------
2584 register_cleanup_utility_code = UtilityCode(
2586 static int __Pyx_RegisterCleanup(void); /*proto*/
2587 static PyObject* %(module_cleanup)s(CYTHON_UNUSED PyObject *self, CYTHON_UNUSED PyObject *unused); /*proto*/
2588 static PyMethodDef cleanup_def = {__Pyx_NAMESTR("__cleanup"), (PyCFunction)&%(module_cleanup)s, METH_NOARGS, 0};
2589 """ % {'module_cleanup': Naming.cleanup_cname},
2591 static int __Pyx_RegisterCleanup(void) {
2592 /* Don't use Py_AtExit because that has a 32-call limit
2593 * and is called after python finalization.
2596 PyObject *cleanup_func = 0;
2597 PyObject *atexit = 0;
2603 cleanup_func = PyCFunction_New(&cleanup_def, 0);
2604 args = PyTuple_New(1);
2605 if (!cleanup_func || !args)
2607 PyTuple_SET_ITEM(args, 0, cleanup_func);
2610 atexit = __Pyx_ImportModule("atexit");
2613 reg = __Pyx_GetAttrString(atexit, "register");
2616 res = PyObject_CallObject(reg, args);
2621 Py_XDECREF(cleanup_func);
2630 import_star_utility_code = """
2632 /* import_all_from is an unexposed function from ceval.c */
2635 __Pyx_import_all_from(PyObject *locals, PyObject *v)
2637 PyObject *all = __Pyx_GetAttrString(v, "__all__");
2638 PyObject *dict, *name, *value;
2639 int skip_leading_underscores = 0;
2643 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
2644 return -1; /* Unexpected error */
2646 dict = __Pyx_GetAttrString(v, "__dict__");
2648 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
2650 PyErr_SetString(PyExc_ImportError,
2651 "from-import-* object has no __dict__ and no __all__");
2654 all = PyMapping_Keys(dict);
2658 skip_leading_underscores = 1;
2661 for (pos = 0, err = 0; ; pos++) {
2662 name = PySequence_GetItem(all, pos);
2664 if (!PyErr_ExceptionMatches(PyExc_IndexError))
2670 if (skip_leading_underscores &&
2671 #if PY_MAJOR_VERSION < 3
2672 PyString_Check(name) &&
2673 PyString_AS_STRING(name)[0] == '_')
2675 PyUnicode_Check(name) &&
2676 PyUnicode_AS_UNICODE(name)[0] == '_')
2682 value = PyObject_GetAttr(v, name);
2685 else if (PyDict_CheckExact(locals))
2686 err = PyDict_SetItem(locals, name, value);
2688 err = PyObject_SetItem(locals, name, value);
2699 static int %(IMPORT_STAR)s(PyObject* m) {
2704 PyObject *locals = 0;
2706 #if PY_MAJOR_VERSION >= 3
2707 PyObject *utf8_name = 0;
2712 locals = PyDict_New(); if (!locals) goto bad;
2713 if (__Pyx_import_all_from(locals, m) < 0) goto bad;
2714 list = PyDict_Items(locals); if (!list) goto bad;
2716 for(i=0; i<PyList_GET_SIZE(list); i++) {
2717 name = PyTuple_GET_ITEM(PyList_GET_ITEM(list, i), 0);
2718 item = PyTuple_GET_ITEM(PyList_GET_ITEM(list, i), 1);
2719 #if PY_MAJOR_VERSION >= 3
2720 utf8_name = PyUnicode_AsUTF8String(name);
2721 if (!utf8_name) goto bad;
2722 s = PyBytes_AS_STRING(utf8_name);
2723 if (%(IMPORT_STAR_SET)s(item, name, s) < 0) goto bad;
2724 Py_DECREF(utf8_name); utf8_name = 0;
2726 s = PyString_AsString(name);
2728 if (%(IMPORT_STAR_SET)s(item, name, s) < 0) goto bad;
2736 #if PY_MAJOR_VERSION >= 3
2737 Py_XDECREF(utf8_name);
2741 """ % {'IMPORT_STAR' : Naming.import_star,
2742 'IMPORT_STAR_SET' : Naming.import_star_set }
2744 refnanny_utility_code = UtilityCode(
2746 #ifndef CYTHON_REFNANNY
2747 #define CYTHON_REFNANNY 0
2752 void (*INCREF)(void*, PyObject*, int);
2753 void (*DECREF)(void*, PyObject*, int);
2754 void (*GOTREF)(void*, PyObject*, int);
2755 void (*GIVEREF)(void*, PyObject*, int);
2756 void* (*SetupContext)(const char*, int, const char*);
2757 void (*FinishContext)(void**);
2758 } __Pyx_RefNannyAPIStruct;
2759 static __Pyx_RefNannyAPIStruct *__Pyx_RefNanny = NULL;
2760 static __Pyx_RefNannyAPIStruct *__Pyx_RefNannyImportAPI(const char *modname); /*proto*/
2761 #define __Pyx_RefNannyDeclarations void *__pyx_refnanny = NULL
2762 #define __Pyx_RefNannySetupContext(name) \
2763 __pyx_refnanny = __Pyx_RefNanny->SetupContext((name), __LINE__, __FILE__)
2764 #define __Pyx_RefNannyFinishContext() \
2765 __Pyx_RefNanny->FinishContext(&__pyx_refnanny)
2766 #define __Pyx_INCREF(r) __Pyx_RefNanny->INCREF(__pyx_refnanny, (PyObject *)(r), __LINE__)
2767 #define __Pyx_DECREF(r) __Pyx_RefNanny->DECREF(__pyx_refnanny, (PyObject *)(r), __LINE__)
2768 #define __Pyx_GOTREF(r) __Pyx_RefNanny->GOTREF(__pyx_refnanny, (PyObject *)(r), __LINE__)
2769 #define __Pyx_GIVEREF(r) __Pyx_RefNanny->GIVEREF(__pyx_refnanny, (PyObject *)(r), __LINE__)
2770 #define __Pyx_XINCREF(r) do { if((r) != NULL) {__Pyx_INCREF(r); }} while(0)
2771 #define __Pyx_XDECREF(r) do { if((r) != NULL) {__Pyx_DECREF(r); }} while(0)
2772 #define __Pyx_XGOTREF(r) do { if((r) != NULL) {__Pyx_GOTREF(r); }} while(0)
2773 #define __Pyx_XGIVEREF(r) do { if((r) != NULL) {__Pyx_GIVEREF(r);}} while(0)
2775 #define __Pyx_RefNannyDeclarations
2776 #define __Pyx_RefNannySetupContext(name)
2777 #define __Pyx_RefNannyFinishContext()
2778 #define __Pyx_INCREF(r) Py_INCREF(r)
2779 #define __Pyx_DECREF(r) Py_DECREF(r)
2780 #define __Pyx_GOTREF(r)
2781 #define __Pyx_GIVEREF(r)
2782 #define __Pyx_XINCREF(r) Py_XINCREF(r)
2783 #define __Pyx_XDECREF(r) Py_XDECREF(r)
2784 #define __Pyx_XGOTREF(r)
2785 #define __Pyx_XGIVEREF(r)
2786 #endif /* CYTHON_REFNANNY */
2790 static __Pyx_RefNannyAPIStruct *__Pyx_RefNannyImportAPI(const char *modname) {
2791 PyObject *m = NULL, *p = NULL;
2793 m = PyImport_ImportModule((char *)modname);
2795 p = PyObject_GetAttrString(m, (char *)\"RefNannyAPI\");
2797 r = PyLong_AsVoidPtr(p);
2801 return (__Pyx_RefNannyAPIStruct *)r;
2803 #endif /* CYTHON_REFNANNY */
2807 main_method = UtilityCode(
2810 #include <floatingpoint.h>
2813 #if PY_MAJOR_VERSION < 3
2814 int %(main_method)s(int argc, char** argv) {
2815 #elif defined(WIN32) || defined(MS_WINDOWS)
2816 int %(wmain_method)s(int argc, wchar_t **argv) {
2818 static int __Pyx_main(int argc, wchar_t **argv) {
2820 /* 754 requires that FP exceptions run in "no stop" mode by default,
2821 * and until C vendors implement C99's ways to control FP exceptions,
2822 * Python requires non-stop mode. Alas, some platforms enable FP
2823 * exceptions by default. Here we disable them.
2829 fpsetmask(m & ~FP_X_OFL);
2832 Py_SetProgramName(argv[0]);
2835 PySys_SetArgv(argc, argv);
2836 { /* init module '%(module_name)s' as '__main__' */
2838 %(module_is_main)s = 1;
2839 #if PY_MAJOR_VERSION < 3
2840 init%(module_name)s();
2842 m = PyInit_%(module_name)s();
2844 if (PyErr_Occurred()) {
2845 PyErr_Print(); /* This exits with the right code if SystemExit. */
2846 #if PY_MAJOR_VERSION < 3
2847 if (Py_FlushLine()) PyErr_Clear();
2858 #if PY_MAJOR_VERSION >= 3 && !defined(WIN32) && !defined(MS_WINDOWS)
2862 __Pyx_char2wchar(char* arg)
2865 #ifdef HAVE_BROKEN_MBSTOWCS
2866 /* Some platforms have a broken implementation of
2867 * mbstowcs which does not count the characters that
2868 * would result from conversion. Use an upper bound.
2870 size_t argsize = strlen(arg);
2872 size_t argsize = mbstowcs(NULL, arg, 0);
2880 if (argsize != (size_t)-1) {
2881 res = (wchar_t *)malloc((argsize+1)*sizeof(wchar_t));
2884 count = mbstowcs(res, arg, argsize+1);
2885 if (count != (size_t)-1) {
2887 /* Only use the result if it contains no
2888 surrogate characters. */
2889 for (tmp = res; *tmp != 0 &&
2890 (*tmp < 0xd800 || *tmp > 0xdfff); tmp++)
2897 /* Conversion failed. Fall back to escaping with surrogateescape. */
2899 /* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */
2901 /* Overallocate; as multi-byte characters are in the argument, the
2902 actual output could use less memory. */
2903 argsize = strlen(arg) + 1;
2904 res = malloc(argsize*sizeof(wchar_t));
2906 in = (unsigned char*)arg;
2908 memset(&mbs, 0, sizeof mbs);
2910 size_t converted = mbrtowc(out, (char*)in, argsize, &mbs);
2912 /* Reached end of string; null char stored. */
2914 if (converted == (size_t)-2) {
2915 /* Incomplete character. This should never happen,
2916 since we provide everything that we have -
2917 unless there is a bug in the C library, or I
2918 misunderstood how mbrtowc works. */
2919 fprintf(stderr, "unexpected mbrtowc result -2\\n");
2922 if (converted == (size_t)-1) {
2923 /* Conversion error. Escape as UTF-8b, and start over
2924 in the initial shift state. */
2925 *out++ = 0xdc00 + *in++;
2927 memset(&mbs, 0, sizeof mbs);
2930 if (*out >= 0xd800 && *out <= 0xdfff) {
2931 /* Surrogate character. Escape the original
2932 byte sequence with surrogateescape. */
2933 argsize -= converted;
2935 *out++ = 0xdc00 + *in++;
2938 /* successfully converted some bytes */
2940 argsize -= converted;
2944 /* Cannot use C locale for escaping; manually escape as if charset
2945 is ASCII (i.e. escape all bytes > 128. This will still roundtrip
2946 correctly in the locale's charset, which must be an ASCII superset. */
2947 res = malloc((strlen(arg)+1)*sizeof(wchar_t));
2949 in = (unsigned char*)arg;
2955 *out++ = 0xdc00 + *in++;
2960 fprintf(stderr, "out of memory\\n");
2965 %(main_method)s(int argc, char **argv)
2968 return __Pyx_main(0, NULL);
2971 wchar_t **argv_copy = (wchar_t **)malloc(sizeof(wchar_t*)*argc);
2972 /* We need a second copies, as Python might modify the first one. */
2973 wchar_t **argv_copy2 = (wchar_t **)malloc(sizeof(wchar_t*)*argc);
2976 if (!argv_copy || !argv_copy2) {
2977 fprintf(stderr, "out of memory\\n");
2980 oldloc = strdup(setlocale(LC_ALL, NULL));
2981 setlocale(LC_ALL, "");
2982 for (i = 0; i < argc; i++) {
2983 argv_copy2[i] = argv_copy[i] = __Pyx_char2wchar(argv[i]);
2987 setlocale(LC_ALL, oldloc);
2989 res = __Pyx_main(argc, argv_copy);
2990 for (i = 0; i < argc; i++) {
2991 free(argv_copy2[i]);
3001 packed_struct_utility_code = UtilityCode(proto="""
3002 #if defined(__GNUC__)
3003 #define __Pyx_PACKED __attribute__((__packed__))
3005 #define __Pyx_PACKED
3007 """, impl="", proto_block='utility_code_proto_before_types')
3009 check_binary_version_utility_code = UtilityCode(proto="""
3010 static int __Pyx_check_binary_version(void);
3012 static int __Pyx_check_binary_version(void) {
3013 char ctversion[4], rtversion[4];
3014 PyOS_snprintf(ctversion, 4, "%d.%d", PY_MAJOR_VERSION, PY_MINOR_VERSION);
3015 PyOS_snprintf(rtversion, 4, "%s", Py_GetVersion());
3016 if (ctversion[0] != rtversion[0] || ctversion[2] != rtversion[2]) {
3018 PyOS_snprintf(message, sizeof(message),
3019 "compiletime version %s of module '%.100s' "
3020 "does not match runtime version %s",
3021 ctversion, __Pyx_MODULE_NAME, rtversion);
3022 #if PY_VERSION_HEX < 0x02050000
3023 return PyErr_Warn(NULL, message);
3025 return PyErr_WarnEx(NULL, message, 1);