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:
969 attr.type.declaration_code(attr.cname))
971 if type.objtypedef_cname is not None:
972 # Only for exposing public typedef name.
973 code.putln("typedef struct %s %s;" % (type.objstruct_cname, type.objtypedef_cname))
975 def generate_global_declarations(self, env, code, definition):
977 for entry in env.c_class_entries:
978 if definition or entry.defined_in_pxd:
979 code.putln("static PyTypeObject *%s = 0;" %
980 entry.type.typeptr_cname)
981 code.put_var_declarations(env.var_entries, static = 1,
982 dll_linkage = "DL_EXPORT", definition = definition)
984 def generate_cfunction_predeclarations(self, env, code, definition):
985 for entry in env.cfunc_entries:
986 if entry.inline_func_in_pxd or (not entry.in_cinclude and (definition
987 or entry.defined_in_pxd or entry.visibility == 'extern')):
988 if entry.visibility == 'public':
989 storage_class = "%s " % Naming.extern_c_macro
990 dll_linkage = "DL_EXPORT"
991 elif entry.visibility == 'extern':
992 storage_class = "%s " % Naming.extern_c_macro
993 dll_linkage = "DL_IMPORT"
994 elif entry.visibility == 'private':
995 storage_class = "static "
998 storage_class = "static "
1002 if not definition and entry.defined_in_pxd:
1003 type = CPtrType(type)
1004 header = type.declaration_code(entry.cname,
1005 dll_linkage = dll_linkage)
1006 if entry.func_modifiers:
1007 modifiers = "%s " % ' '.join(entry.func_modifiers).upper()
1010 code.putln("%s%s%s; /*proto*/" % (
1015 def generate_typeobj_definitions(self, env, code):
1016 full_module_name = env.qualified_name
1017 for entry in env.c_class_entries:
1018 #print "generate_typeobj_definitions:", entry.name
1019 #print "...visibility =", entry.visibility
1020 if entry.visibility != 'extern':
1023 if scope: # could be None if there was an error
1024 self.generate_exttype_vtable(scope, code)
1025 self.generate_new_function(scope, code)
1026 self.generate_dealloc_function(scope, code)
1027 if scope.needs_gc():
1028 self.generate_traverse_function(scope, code)
1029 self.generate_clear_function(scope, code)
1030 if scope.defines_any(["__getitem__"]):
1031 self.generate_getitem_int_function(scope, code)
1032 if scope.defines_any(["__setitem__", "__delitem__"]):
1033 self.generate_ass_subscript_function(scope, code)
1034 if scope.defines_any(["__getslice__", "__setslice__", "__delslice__"]):
1035 warning(self.pos, "__getslice__, __setslice__, and __delslice__ are not supported by Python 3, use __getitem__, __setitem__, and __delitem__ instead", 1)
1036 code.putln("#if PY_MAJOR_VERSION >= 3")
1037 code.putln("#error __getslice__, __setslice__, and __delslice__ not supported in Python 3.")
1038 code.putln("#endif")
1039 if scope.defines_any(["__setslice__", "__delslice__"]):
1040 self.generate_ass_slice_function(scope, code)
1041 if scope.defines_any(["__getattr__","__getattribute__"]):
1042 self.generate_getattro_function(scope, code)
1043 if scope.defines_any(["__setattr__", "__delattr__"]):
1044 self.generate_setattro_function(scope, code)
1045 if scope.defines_any(["__get__"]):
1046 self.generate_descr_get_function(scope, code)
1047 if scope.defines_any(["__set__", "__delete__"]):
1048 self.generate_descr_set_function(scope, code)
1049 self.generate_property_accessors(scope, code)
1050 self.generate_method_table(scope, code)
1051 self.generate_getset_table(scope, code)
1052 self.generate_typeobj_definition(full_module_name, entry, code)
1054 def generate_exttype_vtable(self, scope, code):
1055 # Generate the definition of an extension type's vtable.
1056 type = scope.parent_type
1057 if type.vtable_cname:
1058 code.putln("static struct %s %s;" % (
1059 type.vtabstruct_cname,
1062 def generate_self_cast(self, scope, code):
1063 type = scope.parent_type
1066 type.declaration_code("p"),
1067 type.declaration_code("")))
1069 def generate_new_function(self, scope, code):
1070 tp_slot = TypeSlots.ConstructorSlot("tp_new", '__new__')
1071 slot_func = scope.mangle_internal("tp_new")
1072 type = scope.parent_type
1073 base_type = type.base_type
1075 for entry in scope.var_entries:
1076 if entry.type.is_pyobject:
1077 py_attrs.append(entry)
1078 need_self_cast = type.vtabslot_cname or py_attrs
1081 "static PyObject *%s(PyTypeObject *t, PyObject *a, PyObject *k) {"
1082 % scope.mangle_internal("tp_new"))
1086 % scope.parent_type.declaration_code("p"))
1088 tp_new = TypeSlots.get_base_slot_function(scope, tp_slot)
1090 tp_new = "%s->tp_new" % base_type.typeptr_cname
1092 "PyObject *o = %s(t, a, k);" % tp_new)
1095 "PyObject *o = (*t->tp_alloc)(t, 0);")
1097 "if (!o) return 0;")
1101 % type.cast_code("o"))
1103 # self.generate_self_cast(scope, code)
1104 if type.vtabslot_cname:
1105 vtab_base_type = type
1106 while vtab_base_type.base_type and vtab_base_type.base_type.vtabstruct_cname:
1107 vtab_base_type = vtab_base_type.base_type
1108 if vtab_base_type is not type:
1109 struct_type_cast = "(struct %s*)" % vtab_base_type.vtabstruct_cname
1111 struct_type_cast = ""
1112 code.putln("p->%s = %s%s;" % (
1113 type.vtabslot_cname,
1114 struct_type_cast, type.vtabptr_cname))
1115 for entry in py_attrs:
1116 if scope.is_internal or entry.name == "__weakref__":
1117 # internal classes do not need None inits
1118 code.putln("p->%s = 0;" % entry.cname)
1120 code.put_init_var_to_py_none(entry, "p->%s", nanny=False)
1121 entry = scope.lookup_here("__new__")
1122 if entry and entry.is_special:
1123 if entry.trivial_signature:
1124 cinit_args = "o, %s, NULL" % Naming.empty_tuple
1126 cinit_args = "o, a, k"
1128 "if (%s(%s) < 0) {" %
1129 (entry.func_cname, cinit_args))
1130 code.put_decref_clear("o", py_object_type, nanny=False);
1138 def generate_dealloc_function(self, scope, code):
1139 tp_slot = TypeSlots.ConstructorSlot("tp_dealloc", '__dealloc__')
1140 slot_func = scope.mangle_internal("tp_dealloc")
1141 base_type = scope.parent_type.base_type
1142 if tp_slot.slot_code(scope) != slot_func:
1146 "static void %s(PyObject *o) {"
1147 % scope.mangle_internal("tp_dealloc"))
1149 weakref_slot = scope.lookup_here("__weakref__")
1150 for entry in scope.var_entries:
1151 if entry.type.is_pyobject and entry is not weakref_slot:
1152 py_attrs.append(entry)
1153 if py_attrs or weakref_slot in scope.var_entries:
1154 self.generate_self_cast(scope, code)
1155 self.generate_usr_dealloc_call(scope, code)
1156 if weakref_slot in scope.var_entries:
1157 code.putln("if (p->__weakref__) PyObject_ClearWeakRefs(o);")
1158 for entry in py_attrs:
1159 code.put_xdecref("p->%s" % entry.cname, entry.type, nanny=False)
1161 tp_dealloc = TypeSlots.get_base_slot_function(scope, tp_slot)
1162 if tp_dealloc is None:
1163 tp_dealloc = "%s->tp_dealloc" % base_type.typeptr_cname
1165 "%s(o);" % tp_dealloc)
1168 "(*Py_TYPE(o)->tp_free)(o);")
1172 def generate_usr_dealloc_call(self, scope, code):
1173 entry = scope.lookup_here("__dealloc__")
1178 "PyObject *etype, *eval, *etb;")
1180 "PyErr_Fetch(&etype, &eval, &etb);")
1187 "if (PyErr_Occurred()) PyErr_WriteUnraisable(o);")
1191 "PyErr_Restore(etype, eval, etb);")
1195 def generate_traverse_function(self, scope, code):
1196 tp_slot = TypeSlots.GCDependentSlot("tp_traverse")
1197 slot_func = scope.mangle_internal("tp_traverse")
1198 base_type = scope.parent_type.base_type
1199 if tp_slot.slot_code(scope) != slot_func:
1203 "static int %s(PyObject *o, visitproc v, void *a) {"
1206 for entry in scope.var_entries:
1207 if entry.type.is_pyobject and entry.name != "__weakref__":
1208 py_attrs.append(entry)
1209 if base_type or py_attrs:
1210 code.putln("int e;")
1212 self.generate_self_cast(scope, code)
1214 # want to call it explicitly if possible so inlining can be performed
1215 static_call = TypeSlots.get_base_slot_function(scope, tp_slot)
1217 code.putln("e = %s(o, v, a); if (e) return e;" % static_call)
1219 code.putln("if (%s->tp_traverse) {" % base_type.typeptr_cname)
1221 "e = %s->tp_traverse(o, v, a); if (e) return e;" %
1222 base_type.typeptr_cname)
1224 for entry in py_attrs:
1225 var_code = "p->%s" % entry.cname
1229 if entry.type.is_extension_type:
1230 var_code = "((PyObject*)%s)" % var_code
1232 "e = (*v)(%s, a); if (e) return e;"
1241 def generate_clear_function(self, scope, code):
1242 tp_slot = TypeSlots.GCDependentSlot("tp_clear")
1243 slot_func = scope.mangle_internal("tp_clear")
1244 base_type = scope.parent_type.base_type
1245 if tp_slot.slot_code(scope) != slot_func:
1248 code.putln("static int %s(PyObject *o) {" % slot_func)
1250 for entry in scope.var_entries:
1251 if entry.type.is_pyobject and entry.name != "__weakref__":
1252 py_attrs.append(entry)
1254 self.generate_self_cast(scope, code)
1255 code.putln("PyObject* tmp;")
1257 # want to call it explicitly if possible so inlining can be performed
1258 static_call = TypeSlots.get_base_slot_function(scope, tp_slot)
1260 code.putln("%s(o);" % static_call)
1262 code.putln("if (%s->tp_clear) {" % base_type.typeptr_cname)
1263 code.putln("%s->tp_clear(o);" % base_type.typeptr_cname)
1265 for entry in py_attrs:
1266 name = "p->%s" % entry.cname
1267 code.putln("tmp = ((PyObject*)%s);" % name)
1268 code.put_init_to_py_none(name, entry.type, nanny=False)
1269 code.putln("Py_XDECREF(tmp);")
1275 def generate_getitem_int_function(self, scope, code):
1276 # This function is put into the sq_item slot when
1277 # a __getitem__ method is present. It converts its
1278 # argument to a Python integer and calls mp_subscript.
1280 "static PyObject *%s(PyObject *o, Py_ssize_t i) {" %
1281 scope.mangle_internal("sq_item"))
1285 "PyObject *x = PyInt_FromSsize_t(i); if(!x) return 0;")
1287 "r = Py_TYPE(o)->tp_as_mapping->mp_subscript(o, x);")
1295 def generate_ass_subscript_function(self, scope, code):
1296 # Setting and deleting an item are both done through
1297 # the ass_subscript method, so we dispatch to user's __setitem__
1298 # or __delitem__, or raise an exception.
1299 base_type = scope.parent_type.base_type
1300 set_entry = scope.lookup_here("__setitem__")
1301 del_entry = scope.lookup_here("__delitem__")
1304 "static int %s(PyObject *o, PyObject *i, PyObject *v) {" %
1305 scope.mangle_internal("mp_ass_subscript"))
1310 "return %s(o, i, v);" %
1311 set_entry.func_cname)
1313 self.generate_guarded_basetype_call(
1314 base_type, "tp_as_mapping", "mp_ass_subscript", "o, i, v", code)
1316 "PyErr_Format(PyExc_NotImplementedError,")
1318 ' "Subscript assignment not supported by %s", Py_TYPE(o)->tp_name);')
1327 "return %s(o, i);" %
1328 del_entry.func_cname)
1330 self.generate_guarded_basetype_call(
1331 base_type, "tp_as_mapping", "mp_ass_subscript", "o, i, v", code)
1333 "PyErr_Format(PyExc_NotImplementedError,")
1335 ' "Subscript deletion not supported by %s", Py_TYPE(o)->tp_name);')
1343 def generate_guarded_basetype_call(
1344 self, base_type, substructure, slot, args, code):
1346 base_tpname = base_type.typeptr_cname
1349 "if (%s->%s && %s->%s->%s)" % (
1350 base_tpname, substructure, base_tpname, substructure, slot))
1352 " return %s->%s->%s(%s);" % (
1353 base_tpname, substructure, slot, args))
1359 " return %s->%s(%s);" % (
1360 base_tpname, slot, args))
1362 def generate_ass_slice_function(self, scope, code):
1363 # Setting and deleting a slice are both done through
1364 # the ass_slice method, so we dispatch to user's __setslice__
1365 # or __delslice__, or raise an exception.
1366 base_type = scope.parent_type.base_type
1367 set_entry = scope.lookup_here("__setslice__")
1368 del_entry = scope.lookup_here("__delslice__")
1371 "static int %s(PyObject *o, Py_ssize_t i, Py_ssize_t j, PyObject *v) {" %
1372 scope.mangle_internal("sq_ass_slice"))
1377 "return %s(o, i, j, v);" %
1378 set_entry.func_cname)
1380 self.generate_guarded_basetype_call(
1381 base_type, "tp_as_sequence", "sq_ass_slice", "o, i, j, v", code)
1383 "PyErr_Format(PyExc_NotImplementedError,")
1385 ' "2-element slice assignment not supported by %s", Py_TYPE(o)->tp_name);')
1394 "return %s(o, i, j);" %
1395 del_entry.func_cname)
1397 self.generate_guarded_basetype_call(
1398 base_type, "tp_as_sequence", "sq_ass_slice", "o, i, j, v", code)
1400 "PyErr_Format(PyExc_NotImplementedError,")
1402 ' "2-element slice deletion not supported by %s", Py_TYPE(o)->tp_name);')
1410 def generate_getattro_function(self, scope, code):
1411 # First try to get the attribute using __getattribute__, if defined, or
1412 # PyObject_GenericGetAttr.
1414 # If that raises an AttributeError, call the __getattr__ if defined.
1416 # In both cases, defined can be in this class, or any base class.
1417 def lookup_here_or_base(n,type=None):
1420 type = scope.parent_type
1421 r = type.scope.lookup_here(n)
1423 type.base_type is not None:
1424 return lookup_here_or_base(n,type.base_type)
1427 getattr_entry = lookup_here_or_base("__getattr__")
1428 getattribute_entry = lookup_here_or_base("__getattribute__")
1431 "static PyObject *%s(PyObject *o, PyObject *n) {"
1432 % scope.mangle_internal("tp_getattro"))
1433 if getattribute_entry is not None:
1435 "PyObject *v = %s(o, n);" %
1436 getattribute_entry.func_cname)
1439 "PyObject *v = PyObject_GenericGetAttr(o, n);")
1440 if getattr_entry is not None:
1442 "if (!v && PyErr_ExceptionMatches(PyExc_AttributeError)) {")
1447 getattr_entry.func_cname)
1455 def generate_setattro_function(self, scope, code):
1456 # Setting and deleting an attribute are both done through
1457 # the setattro method, so we dispatch to user's __setattr__
1458 # or __delattr__ or fall back on PyObject_GenericSetAttr.
1459 base_type = scope.parent_type.base_type
1460 set_entry = scope.lookup_here("__setattr__")
1461 del_entry = scope.lookup_here("__delattr__")
1464 "static int %s(PyObject *o, PyObject *n, PyObject *v) {" %
1465 scope.mangle_internal("tp_setattro"))
1470 "return %s(o, n, v);" %
1471 set_entry.func_cname)
1473 self.generate_guarded_basetype_call(
1474 base_type, None, "tp_setattro", "o, n, v", code)
1476 "return PyObject_GenericSetAttr(o, n, v);")
1483 "return %s(o, n);" %
1484 del_entry.func_cname)
1486 self.generate_guarded_basetype_call(
1487 base_type, None, "tp_setattro", "o, n, v", code)
1489 "return PyObject_GenericSetAttr(o, n, 0);")
1495 def generate_descr_get_function(self, scope, code):
1496 # The __get__ function of a descriptor object can be
1497 # called with NULL for the second or third arguments
1498 # under some circumstances, so we replace them with
1499 # None in that case.
1500 user_get_entry = scope.lookup_here("__get__")
1503 "static PyObject *%s(PyObject *o, PyObject *i, PyObject *c) {" %
1504 scope.mangle_internal("tp_descr_get"))
1508 "if (!i) i = Py_None;")
1510 "if (!c) c = Py_None;")
1511 #code.put_incref("i", py_object_type)
1512 #code.put_incref("c", py_object_type)
1514 "r = %s(o, i, c);" %
1515 user_get_entry.func_cname)
1516 #code.put_decref("i", py_object_type)
1517 #code.put_decref("c", py_object_type)
1523 def generate_descr_set_function(self, scope, code):
1524 # Setting and deleting are both done through the __set__
1525 # method of a descriptor, so we dispatch to user's __set__
1526 # or __delete__ or raise an exception.
1527 base_type = scope.parent_type.base_type
1528 user_set_entry = scope.lookup_here("__set__")
1529 user_del_entry = scope.lookup_here("__delete__")
1532 "static int %s(PyObject *o, PyObject *i, PyObject *v) {" %
1533 scope.mangle_internal("tp_descr_set"))
1538 "return %s(o, i, v);" %
1539 user_set_entry.func_cname)
1541 self.generate_guarded_basetype_call(
1542 base_type, None, "tp_descr_set", "o, i, v", code)
1544 'PyErr_SetString(PyExc_NotImplementedError, "__set__");')
1553 "return %s(o, i);" %
1554 user_del_entry.func_cname)
1556 self.generate_guarded_basetype_call(
1557 base_type, None, "tp_descr_set", "o, i, v", code)
1559 'PyErr_SetString(PyExc_NotImplementedError, "__delete__");')
1567 def generate_property_accessors(self, cclass_scope, code):
1568 for entry in cclass_scope.property_entries:
1569 property_scope = entry.scope
1570 if property_scope.defines_any(["__get__"]):
1571 self.generate_property_get_function(entry, code)
1572 if property_scope.defines_any(["__set__", "__del__"]):
1573 self.generate_property_set_function(entry, code)
1575 def generate_property_get_function(self, property_entry, code):
1576 property_scope = property_entry.scope
1577 property_entry.getter_cname = property_scope.parent_scope.mangle(
1578 Naming.prop_get_prefix, property_entry.name)
1579 get_entry = property_scope.lookup_here("__get__")
1582 "static PyObject *%s(PyObject *o, void *x) {" %
1583 property_entry.getter_cname)
1586 get_entry.func_cname)
1590 def generate_property_set_function(self, property_entry, code):
1591 property_scope = property_entry.scope
1592 property_entry.setter_cname = property_scope.parent_scope.mangle(
1593 Naming.prop_set_prefix, property_entry.name)
1594 set_entry = property_scope.lookup_here("__set__")
1595 del_entry = property_scope.lookup_here("__del__")
1598 "static int %s(PyObject *o, PyObject *v, void *x) {" %
1599 property_entry.setter_cname)
1604 "return %s(o, v);" %
1605 set_entry.func_cname)
1608 'PyErr_SetString(PyExc_NotImplementedError, "__set__");')
1618 del_entry.func_cname)
1621 'PyErr_SetString(PyExc_NotImplementedError, "__del__");')
1629 def generate_typeobj_definition(self, modname, entry, code):
1632 for suite in TypeSlots.substructures:
1633 suite.generate_substructure(scope, code)
1635 if entry.visibility == 'public':
1636 header = "DL_EXPORT(PyTypeObject) %s = {"
1638 header = "static PyTypeObject %s = {"
1639 #code.putln(header % scope.parent_type.typeobj_cname)
1640 code.putln(header % type.typeobj_cname)
1642 "PyVarObject_HEAD_INIT(0, 0)")
1644 '__Pyx_NAMESTR("%s.%s"), /*tp_name*/' % (
1645 self.full_module_name, scope.class_name))
1646 if type.typedef_flag:
1647 objstruct = type.objstruct_cname
1649 objstruct = "struct %s" % type.objstruct_cname
1651 "sizeof(%s), /*tp_basicsize*/" %
1654 "0, /*tp_itemsize*/")
1655 for slot in TypeSlots.slot_table:
1656 slot.generate(scope, code)
1660 def generate_method_table(self, env, code):
1663 "static PyMethodDef %s[] = {" %
1664 env.method_table_cname)
1665 for entry in env.pyfunc_entries:
1666 code.put_pymethoddef(entry, ",")
1672 def generate_getset_table(self, env, code):
1673 if env.property_entries:
1676 "static struct PyGetSetDef %s[] = {" %
1677 env.getset_table_cname)
1678 for entry in env.property_entries:
1680 doc_code = "__Pyx_DOCSTR(%s)" % code.get_string_const(entry.doc)
1684 '{(char *)"%s", %s, %s, %s, 0},' % (
1686 entry.getter_cname or "0",
1687 entry.setter_cname or "0",
1694 def generate_import_star(self, env, code):
1695 env.use_utility_code(streq_utility_code)
1697 code.putln("char* %s_type_names[] = {" % Naming.import_star)
1698 for name, entry in env.entries.items():
1700 code.putln('"%s",' % name)
1704 code.enter_cfunc_scope() # as we need labels
1705 code.putln("static int %s(PyObject *o, PyObject* py_name, char *name) {" % Naming.import_star_set)
1706 code.putln("char** type_name = %s_type_names;" % Naming.import_star)
1707 code.putln("while (*type_name) {")
1708 code.putln("if (__Pyx_StrEq(name, *type_name)) {")
1709 code.putln('PyErr_Format(PyExc_TypeError, "Cannot overwrite C type %s", name);')
1710 code.putln('goto bad;')
1712 code.putln("type_name++;")
1714 old_error_label = code.new_error_label()
1715 code.putln("if (0);") # so the first one can be "else if"
1716 for name, entry in env.entries.items():
1717 if entry.is_cglobal and entry.used:
1718 code.putln('else if (__Pyx_StrEq(name, "%s")) {' % name)
1719 if entry.type.is_pyobject:
1720 if entry.type.is_extension_type or entry.type.is_builtin_type:
1721 code.putln("if (!(%s)) %s;" % (
1722 entry.type.type_test_code("o"),
1723 code.error_goto(entry.pos)))
1724 code.putln("Py_INCREF(o);")
1725 code.put_decref(entry.cname, entry.type, nanny=False)
1726 code.putln("%s = %s;" % (
1728 PyrexTypes.typecast(entry.type, py_object_type, "o")))
1729 elif entry.type.from_py_function:
1730 rhs = "%s(o)" % entry.type.from_py_function
1731 if entry.type.is_enum:
1732 rhs = PyrexTypes.typecast(entry.type, PyrexTypes.c_long_type, rhs)
1733 code.putln("%s = %s; if (%s) %s;" % (
1736 entry.type.error_condition(entry.cname),
1737 code.error_goto(entry.pos)))
1739 code.putln('PyErr_Format(PyExc_TypeError, "Cannot convert Python object %s to %s");' % (name, entry.type))
1740 code.putln(code.error_goto(entry.pos))
1742 code.putln("else {")
1743 code.putln("if (PyObject_SetAttr(%s, py_name, o) < 0) goto bad;" % Naming.module_cname)
1745 code.putln("return 0;")
1746 if code.label_used(code.error_label):
1747 code.put_label(code.error_label)
1748 # This helps locate the offending name.
1749 code.putln('__Pyx_AddTraceback("%s");' % self.full_module_name);
1750 code.error_label = old_error_label
1752 code.putln("return -1;")
1754 code.putln(import_star_utility_code)
1755 code.exit_cfunc_scope() # done with labels
1757 def generate_module_init_func(self, imported_modules, env, code):
1758 code.enter_cfunc_scope()
1760 header2 = "PyMODINIT_FUNC init%s(void)" % env.module_name
1761 header3 = "PyMODINIT_FUNC PyInit_%s(void)" % env.module_name
1762 code.putln("#if PY_MAJOR_VERSION < 3")
1763 code.putln("%s; /*proto*/" % header2)
1766 code.putln("%s; /*proto*/" % header3)
1768 code.putln("#endif")
1770 tempdecl_code = code.insertion_point()
1772 code.putln("#if CYTHON_REFNANNY")
1773 code.putln("void* __pyx_refnanny = NULL;")
1774 code.putln("__Pyx_RefNanny = __Pyx_RefNannyImportAPI(\"refnanny\");")
1775 code.putln("if (!__Pyx_RefNanny) {")
1776 code.putln(" PyErr_Clear();")
1777 code.putln(" __Pyx_RefNanny = __Pyx_RefNannyImportAPI(\"Cython.Runtime.refnanny\");")
1778 code.putln(" if (!__Pyx_RefNanny)")
1779 code.putln(" Py_FatalError(\"failed to import 'refnanny' module\");")
1781 code.putln("__pyx_refnanny = __Pyx_RefNanny->SetupContext(\"%s\", __LINE__, __FILE__);"% header3)
1782 code.putln("#endif")
1784 env.use_utility_code(check_binary_version_utility_code)
1785 code.putln("if ( __Pyx_check_binary_version() < 0) %s" % code.error_goto(self.pos))
1787 code.putln("%s = PyTuple_New(0); %s" % (Naming.empty_tuple, code.error_goto_if_null(Naming.empty_tuple, self.pos)));
1788 code.putln("%s = PyBytes_FromStringAndSize(\"\", 0); %s" % (Naming.empty_bytes, code.error_goto_if_null(Naming.empty_bytes, self.pos)));
1790 code.putln("#ifdef %s_USED" % Naming.binding_cfunc)
1791 code.putln("if (%s_init() < 0) %s" % (Naming.binding_cfunc, code.error_goto(self.pos)))
1792 code.putln("#endif")
1794 code.putln("/*--- Library function declarations ---*/")
1795 env.generate_library_function_declarations(code)
1797 code.putln("/*--- Threads initialization code ---*/")
1798 code.putln("#if defined(__PYX_FORCE_INIT_THREADS) && __PYX_FORCE_INIT_THREADS")
1799 code.putln("#ifdef WITH_THREAD /* Python build with threading support? */")
1800 code.putln("PyEval_InitThreads();")
1801 code.putln("#endif")
1802 code.putln("#endif")
1804 code.putln("/*--- Module creation code ---*/")
1805 self.generate_module_creation_code(env, code)
1807 code.putln("/*--- Initialize various global constants etc. ---*/")
1808 code.putln(code.error_goto_if_neg("__Pyx_InitGlobals()", self.pos))
1810 __main__name = code.globalstate.get_py_string_const(
1811 EncodedString("__main__"), identifier=True)
1812 code.putln("if (%s%s) {" % (Naming.module_is_main, self.full_module_name.replace('.', '__')))
1814 'if (__Pyx_SetAttrString(%s, "__name__", %s) < 0) %s;' % (
1817 code.error_goto(self.pos)))
1820 if Options.cache_builtins:
1821 code.putln("/*--- Builtin init code ---*/")
1822 code.putln(code.error_goto_if_neg("__Pyx_InitCachedBuiltins()", self.pos))
1824 code.putln("/*--- Constants init code ---*/")
1825 code.putln(code.error_goto_if_neg("__Pyx_InitCachedConstants()", self.pos))
1827 code.putln("/*--- Global init code ---*/")
1828 self.generate_global_init_code(env, code)
1830 code.putln("/*--- Variable export code ---*/")
1831 self.generate_c_variable_export_code(env, code)
1833 code.putln("/*--- Function export code ---*/")
1834 self.generate_c_function_export_code(env, code)
1836 code.putln("/*--- Type init code ---*/")
1837 self.generate_type_init_code(env, code)
1839 code.putln("/*--- Type import code ---*/")
1840 for module in imported_modules:
1841 self.generate_type_import_code_for_module(module, env, code)
1843 code.putln("/*--- Function import code ---*/")
1844 for module in imported_modules:
1845 self.generate_c_function_import_code_for_module(module, env, code)
1847 code.putln("/*--- Execution code ---*/")
1850 self.body.generate_execution_code(code)
1852 if Options.generate_cleanup_code:
1853 # this should be replaced by the module's tp_clear in Py3
1854 env.use_utility_code(import_module_utility_code)
1855 code.putln("if (__Pyx_RegisterCleanup()) %s;" % code.error_goto(self.pos))
1857 code.put_goto(code.return_label)
1858 code.put_label(code.error_label)
1859 for cname, type in code.funcstate.all_managed_temps():
1860 code.put_xdecref(cname, type)
1861 code.putln('if (%s) {' % env.module_cname)
1862 code.putln('__Pyx_AddTraceback("init %s");' % env.qualified_name)
1863 env.use_utility_code(Nodes.traceback_utility_code)
1864 code.put_decref_clear(env.module_cname, py_object_type, nanny=False)
1865 code.putln('} else if (!PyErr_Occurred()) {')
1866 code.putln('PyErr_SetString(PyExc_ImportError, "init %s");' % env.qualified_name)
1868 code.put_label(code.return_label)
1870 code.put_finish_refcount_context()
1872 code.putln("#if PY_MAJOR_VERSION < 3")
1873 code.putln("return;")
1875 code.putln("return %s;" % env.module_cname)
1876 code.putln("#endif")
1879 tempdecl_code.put_temp_declarations(code.funcstate)
1881 code.exit_cfunc_scope()
1883 def generate_module_cleanup_func(self, env, code):
1884 if not Options.generate_cleanup_code:
1886 code.globalstate.use_utility_code(register_cleanup_utility_code)
1887 code.putln('static PyObject *%s(CYTHON_UNUSED PyObject *self, CYTHON_UNUSED PyObject *unused) {' %
1888 Naming.cleanup_cname)
1889 if Options.generate_cleanup_code >= 2:
1890 code.putln("/*--- Global cleanup code ---*/")
1891 rev_entries = list(env.var_entries)
1892 rev_entries.reverse()
1893 for entry in rev_entries:
1894 if entry.visibility != 'extern':
1895 if entry.type.is_pyobject and entry.used:
1896 code.putln("Py_DECREF(%s); %s = 0;" % (
1897 code.entry_as_pyobject(entry), entry.cname))
1898 code.putln("__Pyx_CleanupGlobals();")
1899 if Options.generate_cleanup_code >= 3:
1900 code.putln("/*--- Type import cleanup code ---*/")
1901 for type, _ in env.types_imported.items():
1902 code.putln("Py_DECREF((PyObject *)%s);" % type.typeptr_cname)
1903 if Options.cache_builtins:
1904 code.putln("/*--- Builtin cleanup code ---*/")
1905 for entry in env.cached_builtins:
1906 code.put_decref_clear(entry.cname,
1907 PyrexTypes.py_object_type,
1909 code.putln("/*--- Intern cleanup code ---*/")
1910 code.put_decref_clear(Naming.empty_tuple,
1911 PyrexTypes.py_object_type,
1913 # for entry in env.pynum_entries:
1914 # code.put_decref_clear(entry.cname,
1915 # PyrexTypes.py_object_type,
1917 # for entry in env.all_pystring_entries:
1918 # if entry.is_interned:
1919 # code.put_decref_clear(entry.pystring_cname,
1920 # PyrexTypes.py_object_type,
1922 # for entry in env.default_entries:
1923 # if entry.type.is_pyobject and entry.used:
1924 # code.putln("Py_DECREF(%s); %s = 0;" % (
1925 # code.entry_as_pyobject(entry), entry.cname))
1926 code.putln("Py_INCREF(Py_None); return Py_None;")
1928 def generate_main_method(self, env, code):
1929 module_is_main = "%s%s" % (Naming.module_is_main, self.full_module_name.replace('.', '__'))
1930 if Options.embed == "main":
1933 wmain = Options.embed
1934 code.globalstate.use_utility_code(
1935 main_method.specialize(
1936 module_name = env.module_name,
1937 module_is_main = module_is_main,
1938 main_method = Options.embed,
1939 wmain_method = wmain))
1941 def generate_pymoduledef_struct(self, env, code):
1943 doc = "__Pyx_DOCSTR(%s)" % code.get_string_const(env.doc)
1947 code.putln("#if PY_MAJOR_VERSION >= 3")
1948 code.putln("static struct PyModuleDef %s = {" % Naming.pymoduledef_cname)
1949 code.putln(" PyModuleDef_HEAD_INIT,")
1950 code.putln(' __Pyx_NAMESTR("%s"),' % env.module_name)
1951 code.putln(" %s, /* m_doc */" % doc)
1952 code.putln(" -1, /* m_size */")
1953 code.putln(" %s /* m_methods */," % env.method_table_cname)
1954 code.putln(" NULL, /* m_reload */")
1955 code.putln(" NULL, /* m_traverse */")
1956 code.putln(" NULL, /* m_clear */")
1957 code.putln(" NULL /* m_free */")
1959 code.putln("#endif")
1961 def generate_module_creation_code(self, env, code):
1962 # Generate code to create the module object and
1963 # install the builtins.
1965 doc = "__Pyx_DOCSTR(%s)" % code.get_string_const(env.doc)
1968 code.putln("#if PY_MAJOR_VERSION < 3")
1970 '%s = Py_InitModule4(__Pyx_NAMESTR("%s"), %s, %s, 0, PYTHON_API_VERSION);' % (
1973 env.method_table_cname,
1977 "%s = PyModule_Create(&%s);" % (
1979 Naming.pymoduledef_cname))
1980 code.putln("#endif")
1984 code.error_goto(self.pos)));
1985 code.putln("#if PY_MAJOR_VERSION < 3")
1989 code.putln("#endif")
1991 '%s = PyImport_AddModule(__Pyx_NAMESTR(__Pyx_BUILTIN_MODULE_NAME));' %
1992 Naming.builtins_cname)
1995 Naming.builtins_cname,
1996 code.error_goto(self.pos)));
1998 'if (__Pyx_SetAttrString(%s, "__builtins__", %s) < 0) %s;' % (
2000 Naming.builtins_cname,
2001 code.error_goto(self.pos)))
2002 if Options.pre_import is not None:
2004 '%s = PyImport_AddModule(__Pyx_NAMESTR("%s"));' % (
2005 Naming.preimport_cname,
2006 Options.pre_import))
2009 Naming.preimport_cname,
2010 code.error_goto(self.pos)));
2012 def generate_global_init_code(self, env, code):
2013 # Generate code to initialise global PyObject *
2014 # variables to None.
2015 for entry in env.var_entries:
2016 if entry.visibility != 'extern':
2017 if entry.type.is_pyobject and entry.used:
2018 code.put_init_var_to_py_none(entry, nanny=False)
2020 def generate_c_variable_export_code(self, env, code):
2021 # Generate code to create PyCFunction wrappers for exported C functions.
2022 for entry in env.var_entries:
2023 if entry.api or entry.defined_in_pxd:
2024 env.use_utility_code(voidptr_export_utility_code)
2025 signature = entry.type.declaration_code("")
2026 code.putln('if (__Pyx_ExportVoidPtr("%s", (void *)&%s, "%s") < 0) %s' % (
2030 code.error_goto(self.pos)))
2032 def generate_c_function_export_code(self, env, code):
2033 # Generate code to create PyCFunction wrappers for exported C functions.
2034 for entry in env.cfunc_entries:
2035 if entry.api or entry.defined_in_pxd:
2036 env.use_utility_code(function_export_utility_code)
2037 signature = entry.type.signature_string()
2038 code.putln('if (__Pyx_ExportFunction("%s", (void (*)(void))%s, "%s") < 0) %s' % (
2042 code.error_goto(self.pos)))
2044 def generate_type_import_code_for_module(self, module, env, code):
2045 # Generate type import code for all exported extension types in
2046 # an imported module.
2047 #if module.c_class_entries:
2048 for entry in module.c_class_entries:
2049 if entry.defined_in_pxd:
2050 self.generate_type_import_code(env, entry.type, entry.pos, code)
2052 def generate_c_function_import_code_for_module(self, module, env, code):
2053 # Generate import code for all exported C functions in a cimported module.
2055 for entry in module.cfunc_entries:
2056 if entry.defined_in_pxd:
2057 entries.append(entry)
2059 env.use_utility_code(import_module_utility_code)
2060 env.use_utility_code(function_import_utility_code)
2061 temp = code.funcstate.allocate_temp(py_object_type, manage_ref=True)
2063 '%s = __Pyx_ImportModule("%s"); if (!%s) %s' % (
2065 module.qualified_name,
2067 code.error_goto(self.pos)))
2068 for entry in entries:
2070 'if (__Pyx_ImportFunction(%s, "%s", (void (**)(void))&%s, "%s") < 0) %s' % (
2074 entry.type.signature_string(),
2075 code.error_goto(self.pos)))
2076 code.putln("Py_DECREF(%s); %s = 0;" % (temp, temp))
2078 def generate_type_init_code(self, env, code):
2079 # Generate type import code for extern extension types
2080 # and type ready code for non-extern ones.
2081 for entry in env.c_class_entries:
2082 if entry.visibility == 'extern':
2083 self.generate_type_import_code(env, entry.type, entry.pos, code)
2085 self.generate_base_type_import_code(env, entry, code)
2086 self.generate_exttype_vtable_init_code(entry, code)
2087 self.generate_type_ready_code(env, entry, code)
2088 self.generate_typeptr_assignment_code(entry, code)
2090 def generate_base_type_import_code(self, env, entry, code):
2091 base_type = entry.type.base_type
2092 if base_type and base_type.module_name != env.qualified_name \
2093 and not base_type.is_builtin_type:
2094 self.generate_type_import_code(env, base_type, self.pos, code)
2096 def use_type_import_utility_code(self, env):
2097 env.use_utility_code(type_import_utility_code)
2098 env.use_utility_code(import_module_utility_code)
2100 def generate_type_import_code(self, env, type, pos, code):
2101 # If not already done, generate code to import the typeobject of an
2102 # extension type defined in another module, and extract its C method
2103 # table pointer if any.
2104 if type in env.types_imported:
2106 if type.typedef_flag:
2107 objstruct = type.objstruct_cname
2109 objstruct = "struct %s" % type.objstruct_cname
2110 self.generate_type_import_call(type, code,
2111 code.error_goto_if_null(type.typeptr_cname, pos))
2112 self.use_type_import_utility_code(env)
2113 if type.vtabptr_cname:
2114 env.use_utility_code(Nodes.get_vtable_utility_code)
2115 code.putln("%s = (struct %s*)__Pyx_GetVtable(%s->tp_dict); %s" % (
2117 type.vtabstruct_cname,
2119 code.error_goto_if_null(type.vtabptr_cname, pos)))
2120 env.types_imported[type] = 1
2122 py3_type_name_map = {'str' : 'bytes', 'unicode' : 'str'}
2124 def generate_type_import_call(self, type, code, error_code):
2125 if type.typedef_flag:
2126 objstruct = type.objstruct_cname
2128 objstruct = "struct %s" % type.objstruct_cname
2129 module_name = type.module_name
2130 if module_name not in ('__builtin__', 'builtins'):
2131 module_name = '"%s"' % module_name
2133 module_name = '__Pyx_BUILTIN_MODULE_NAME'
2134 if type.name in self.py3_type_name_map:
2135 code.putln("#if PY_MAJOR_VERSION >= 3")
2136 code.putln('%s = __Pyx_ImportType(%s, "%s", sizeof(%s), 1); %s' % (
2139 self.py3_type_name_map[type.name],
2143 code.putln('%s = __Pyx_ImportType(%s, "%s", sizeof(%s), %i); %s' % (
2148 not type.is_external or type.is_subclassed,
2150 if type.name in self.py3_type_name_map:
2151 code.putln("#endif")
2153 def generate_type_ready_code(self, env, entry, code):
2154 # Generate a call to PyType_Ready for an extension
2155 # type defined in this module.
2157 typeobj_cname = type.typeobj_cname
2159 if scope: # could be None if there was an error
2160 if entry.visibility != 'extern':
2161 for slot in TypeSlots.slot_table:
2162 slot.generate_dynamic_init_code(scope, code)
2164 "if (PyType_Ready(&%s) < 0) %s" % (
2166 code.error_goto(entry.pos)))
2167 # Fix special method docstrings. This is a bit of a hack, but
2168 # unless we let PyType_Ready create the slot wrappers we have
2169 # a significant performance hit. (See trac #561.)
2170 for func in entry.type.scope.pyfunc_entries:
2171 if func.is_special and Options.docstrings and func.wrapperbase_cname:
2174 'PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&%s, "%s"); %s' % (
2177 code.error_goto_if_null('wrapper', entry.pos)))
2179 "if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) {")
2181 "%s = *((PyWrapperDescrObject *)wrapper)->d_base;" % (
2182 func.wrapperbase_cname))
2184 "%s.doc = %s;" % (func.wrapperbase_cname, func.doc_cname))
2186 "((PyWrapperDescrObject *)wrapper)->d_base = &%s;" % (
2187 func.wrapperbase_cname))
2190 if type.vtable_cname:
2192 "if (__Pyx_SetVtable(%s.tp_dict, %s) < 0) %s" % (
2195 code.error_goto(entry.pos)))
2196 env.use_utility_code(Nodes.set_vtable_utility_code)
2197 if not type.scope.is_internal and not type.scope.directives['internal']:
2198 # scope.is_internal is set for types defined by
2199 # Cython (such as closures), the 'internal'
2200 # directive is set by users
2202 'if (__Pyx_SetAttrString(%s, "%s", (PyObject *)&%s) < 0) %s' % (
2203 Naming.module_cname,
2206 code.error_goto(entry.pos)))
2207 weakref_entry = scope.lookup_here("__weakref__")
2209 if weakref_entry.type is py_object_type:
2210 tp_weaklistoffset = "%s.tp_weaklistoffset" % typeobj_cname
2211 if type.typedef_flag:
2212 objstruct = type.objstruct_cname
2214 objstruct = "struct %s" % type.objstruct_cname
2215 code.putln("if (%s == 0) %s = offsetof(%s, %s);" % (
2219 weakref_entry.cname))
2221 error(weakref_entry.pos, "__weakref__ slot must be of type 'object'")
2223 def generate_exttype_vtable_init_code(self, entry, code):
2224 # Generate code to initialise the C method table of an
2227 if type.vtable_cname:
2232 if type.base_type and type.base_type.vtabptr_cname:
2236 Naming.obj_base_cname,
2237 type.base_type.vtabptr_cname))
2239 c_method_entries = [
2240 entry for entry in type.scope.cfunc_entries
2241 if entry.func_cname ]
2242 if c_method_entries:
2243 for meth_entry in c_method_entries:
2244 cast = meth_entry.type.signature_cast_string()
2250 meth_entry.func_cname))
2252 def generate_typeptr_assignment_code(self, entry, code):
2253 # Generate code to initialise the typeptr of an extension
2254 # type defined in this module to point to its type object.
2256 if type.typeobj_cname:
2259 type.typeptr_cname, type.typeobj_cname))
2261 #------------------------------------------------------------------------------------
2263 # Runtime support code
2265 #------------------------------------------------------------------------------------
2267 streq_utility_code = UtilityCode(
2269 static CYTHON_INLINE int __Pyx_StrEq(const char *, const char *); /*proto*/
2272 static CYTHON_INLINE int __Pyx_StrEq(const char *s1, const char *s2) {
2273 while (*s1 != '\\0' && *s1 == *s2) { s1++; s2++; }
2278 #------------------------------------------------------------------------------------
2280 import_module_utility_code = UtilityCode(
2282 static PyObject *__Pyx_ImportModule(const char *name); /*proto*/
2285 #ifndef __PYX_HAVE_RT_ImportModule
2286 #define __PYX_HAVE_RT_ImportModule
2287 static PyObject *__Pyx_ImportModule(const char *name) {
2288 PyObject *py_name = 0;
2289 PyObject *py_module = 0;
2291 #if PY_MAJOR_VERSION < 3
2292 py_name = PyString_FromString(name);
2294 py_name = PyUnicode_FromString(name);
2298 py_module = PyImport_Import(py_name);
2302 Py_XDECREF(py_name);
2308 #------------------------------------------------------------------------------------
2310 type_import_utility_code = UtilityCode(
2312 static PyTypeObject *__Pyx_ImportType(const char *module_name, const char *class_name, long size, int strict); /*proto*/
2315 #ifndef __PYX_HAVE_RT_ImportType
2316 #define __PYX_HAVE_RT_ImportType
2317 static PyTypeObject *__Pyx_ImportType(const char *module_name, const char *class_name,
2318 long size, int strict)
2320 PyObject *py_module = 0;
2321 PyObject *result = 0;
2322 PyObject *py_name = 0;
2325 py_module = __Pyx_ImportModule(module_name);
2328 #if PY_MAJOR_VERSION < 3
2329 py_name = PyString_FromString(class_name);
2331 py_name = PyUnicode_FromString(class_name);
2335 result = PyObject_GetAttr(py_module, py_name);
2338 Py_DECREF(py_module);
2342 if (!PyType_Check(result)) {
2343 PyErr_Format(PyExc_TypeError,
2344 "%s.%s is not a type object",
2345 module_name, class_name);
2348 if (!strict && ((PyTypeObject *)result)->tp_basicsize > size) {
2349 PyOS_snprintf(warning, sizeof(warning),
2350 "%s.%s size changed, may indicate binary incompatibility",
2351 module_name, class_name);
2352 #if PY_VERSION_HEX < 0x02050000
2353 PyErr_Warn(NULL, warning);
2355 PyErr_WarnEx(NULL, warning, 0);
2358 else if (((PyTypeObject *)result)->tp_basicsize != size) {
2359 PyErr_Format(PyExc_ValueError,
2360 "%s.%s has the wrong size, try recompiling",
2361 module_name, class_name);
2364 return (PyTypeObject *)result;
2366 Py_XDECREF(py_module);
2373 #------------------------------------------------------------------------------------
2375 voidptr_export_utility_code = UtilityCode(
2377 static int __Pyx_ExportVoidPtr(const char *name, void *p, const char *sig); /*proto*/
2380 static int __Pyx_ExportVoidPtr(const char *name, void *p, const char *sig) {
2384 d = PyObject_GetAttrString(%(MODULE)s, (char *)"%(API)s");
2391 if (PyModule_AddObject(%(MODULE)s, (char *)"%(API)s", d) < 0)
2394 #if PY_VERSION_HEX >= 0x02070000 && !(PY_MAJOR_VERSION==3&&PY_MINOR_VERSION==0)
2395 cobj = PyCapsule_New(p, sig, 0);
2397 cobj = PyCObject_FromVoidPtrAndDesc(p, (void *)sig, 0);
2401 if (PyDict_SetItemString(d, name, cobj) < 0)
2411 """ % {'MODULE': Naming.module_cname, 'API': Naming.api_name}
2414 function_export_utility_code = UtilityCode(
2416 static int __Pyx_ExportFunction(const char *name, void (*f)(void), const char *sig); /*proto*/
2419 static int __Pyx_ExportFunction(const char *name, void (*f)(void), const char *sig) {
2427 d = PyObject_GetAttrString(%(MODULE)s, (char *)"%(API)s");
2434 if (PyModule_AddObject(%(MODULE)s, (char *)"%(API)s", d) < 0)
2438 #if PY_VERSION_HEX >= 0x02070000 && !(PY_MAJOR_VERSION==3&&PY_MINOR_VERSION==0)
2439 cobj = PyCapsule_New(tmp.p, sig, 0);
2441 cobj = PyCObject_FromVoidPtrAndDesc(tmp.p, (void *)sig, 0);
2445 if (PyDict_SetItemString(d, name, cobj) < 0)
2455 """ % {'MODULE': Naming.module_cname, 'API': Naming.api_name}
2458 voidptr_import_utility_code = UtilityCode(
2460 static int __Pyx_ImportVoidPtr(PyObject *module, const char *name, void **p, const char *sig); /*proto*/
2463 #ifndef __PYX_HAVE_RT_ImportVoidPtr
2464 #define __PYX_HAVE_RT_ImportVoidPtr
2465 static int __Pyx_ImportVoidPtr(PyObject *module, const char *name, void **p, const char *sig) {
2469 d = PyObject_GetAttrString(module, (char *)"%(API)s");
2472 cobj = PyDict_GetItemString(d, name);
2474 PyErr_Format(PyExc_ImportError,
2475 "%%s does not export expected C variable %%s",
2476 PyModule_GetName(module), name);
2479 #if PY_VERSION_HEX >= 0x02070000 && !(PY_MAJOR_VERSION==3&&PY_MINOR_VERSION==0)
2480 if (!PyCapsule_IsValid(cobj, sig)) {
2481 PyErr_Format(PyExc_TypeError,
2482 "C variable %%s.%%s has wrong signature (expected %%s, got %%s)",
2483 PyModule_GetName(module), name, sig, PyCapsule_GetName(cobj));
2486 *p = PyCapsule_GetPointer(cobj, sig);
2488 {const char *desc, *s1, *s2;
2489 desc = (const char *)PyCObject_GetDesc(cobj);
2492 s1 = desc; s2 = sig;
2493 while (*s1 != '\\0' && *s1 == *s2) { s1++; s2++; }
2495 PyErr_Format(PyExc_TypeError,
2496 "C variable %%s.%%s has wrong signature (expected %%s, got %%s)",
2497 PyModule_GetName(module), name, sig, desc);
2500 *p = PyCObject_AsVoidPtr(cobj);}
2511 """ % dict(API = Naming.api_name)
2514 function_import_utility_code = UtilityCode(
2516 static int __Pyx_ImportFunction(PyObject *module, const char *funcname, void (**f)(void), const char *sig); /*proto*/
2519 #ifndef __PYX_HAVE_RT_ImportFunction
2520 #define __PYX_HAVE_RT_ImportFunction
2521 static int __Pyx_ImportFunction(PyObject *module, const char *funcname, void (**f)(void), const char *sig) {
2529 d = PyObject_GetAttrString(module, (char *)"%(API)s");
2532 cobj = PyDict_GetItemString(d, funcname);
2534 PyErr_Format(PyExc_ImportError,
2535 "%%s does not export expected C function %%s",
2536 PyModule_GetName(module), funcname);
2539 #if PY_VERSION_HEX >= 0x02070000 && !(PY_MAJOR_VERSION==3&&PY_MINOR_VERSION==0)
2540 if (!PyCapsule_IsValid(cobj, sig)) {
2541 PyErr_Format(PyExc_TypeError,
2542 "C function %%s.%%s has wrong signature (expected %%s, got %%s)",
2543 PyModule_GetName(module), funcname, sig, PyCapsule_GetName(cobj));
2546 tmp.p = PyCapsule_GetPointer(cobj, sig);
2548 {const char *desc, *s1, *s2;
2549 desc = (const char *)PyCObject_GetDesc(cobj);
2552 s1 = desc; s2 = sig;
2553 while (*s1 != '\\0' && *s1 == *s2) { s1++; s2++; }
2555 PyErr_Format(PyExc_TypeError,
2556 "C function %%s.%%s has wrong signature (expected %%s, got %%s)",
2557 PyModule_GetName(module), funcname, sig, desc);
2560 tmp.p = PyCObject_AsVoidPtr(cobj);}
2572 """ % dict(API = Naming.api_name)
2575 #------------------------------------------------------------------------------------
2577 register_cleanup_utility_code = UtilityCode(
2579 static int __Pyx_RegisterCleanup(void); /*proto*/
2580 static PyObject* %(module_cleanup)s(CYTHON_UNUSED PyObject *self, CYTHON_UNUSED PyObject *unused); /*proto*/
2581 static PyMethodDef cleanup_def = {__Pyx_NAMESTR("__cleanup"), (PyCFunction)&%(module_cleanup)s, METH_NOARGS, 0};
2582 """ % {'module_cleanup': Naming.cleanup_cname},
2584 static int __Pyx_RegisterCleanup(void) {
2585 /* Don't use Py_AtExit because that has a 32-call limit
2586 * and is called after python finalization.
2589 PyObject *cleanup_func = 0;
2590 PyObject *atexit = 0;
2596 cleanup_func = PyCFunction_New(&cleanup_def, 0);
2597 args = PyTuple_New(1);
2598 if (!cleanup_func || !args)
2600 PyTuple_SET_ITEM(args, 0, cleanup_func);
2603 atexit = __Pyx_ImportModule("atexit");
2606 reg = __Pyx_GetAttrString(atexit, "register");
2609 res = PyObject_CallObject(reg, args);
2614 Py_XDECREF(cleanup_func);
2623 import_star_utility_code = """
2625 /* import_all_from is an unexposed function from ceval.c */
2628 __Pyx_import_all_from(PyObject *locals, PyObject *v)
2630 PyObject *all = __Pyx_GetAttrString(v, "__all__");
2631 PyObject *dict, *name, *value;
2632 int skip_leading_underscores = 0;
2636 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
2637 return -1; /* Unexpected error */
2639 dict = __Pyx_GetAttrString(v, "__dict__");
2641 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
2643 PyErr_SetString(PyExc_ImportError,
2644 "from-import-* object has no __dict__ and no __all__");
2647 all = PyMapping_Keys(dict);
2651 skip_leading_underscores = 1;
2654 for (pos = 0, err = 0; ; pos++) {
2655 name = PySequence_GetItem(all, pos);
2657 if (!PyErr_ExceptionMatches(PyExc_IndexError))
2663 if (skip_leading_underscores &&
2664 #if PY_MAJOR_VERSION < 3
2665 PyString_Check(name) &&
2666 PyString_AS_STRING(name)[0] == '_')
2668 PyUnicode_Check(name) &&
2669 PyUnicode_AS_UNICODE(name)[0] == '_')
2675 value = PyObject_GetAttr(v, name);
2678 else if (PyDict_CheckExact(locals))
2679 err = PyDict_SetItem(locals, name, value);
2681 err = PyObject_SetItem(locals, name, value);
2692 static int %(IMPORT_STAR)s(PyObject* m) {
2697 PyObject *locals = 0;
2699 #if PY_MAJOR_VERSION >= 3
2700 PyObject *utf8_name = 0;
2705 locals = PyDict_New(); if (!locals) goto bad;
2706 if (__Pyx_import_all_from(locals, m) < 0) goto bad;
2707 list = PyDict_Items(locals); if (!list) goto bad;
2709 for(i=0; i<PyList_GET_SIZE(list); i++) {
2710 name = PyTuple_GET_ITEM(PyList_GET_ITEM(list, i), 0);
2711 item = PyTuple_GET_ITEM(PyList_GET_ITEM(list, i), 1);
2712 #if PY_MAJOR_VERSION >= 3
2713 utf8_name = PyUnicode_AsUTF8String(name);
2714 if (!utf8_name) goto bad;
2715 s = PyBytes_AS_STRING(utf8_name);
2716 if (%(IMPORT_STAR_SET)s(item, name, s) < 0) goto bad;
2717 Py_DECREF(utf8_name); utf8_name = 0;
2719 s = PyString_AsString(name);
2721 if (%(IMPORT_STAR_SET)s(item, name, s) < 0) goto bad;
2729 #if PY_MAJOR_VERSION >= 3
2730 Py_XDECREF(utf8_name);
2734 """ % {'IMPORT_STAR' : Naming.import_star,
2735 'IMPORT_STAR_SET' : Naming.import_star_set }
2737 refnanny_utility_code = UtilityCode(proto="""
2738 #ifndef CYTHON_REFNANNY
2739 #define CYTHON_REFNANNY 0
2744 void (*INCREF)(void*, PyObject*, int);
2745 void (*DECREF)(void*, PyObject*, int);
2746 void (*GOTREF)(void*, PyObject*, int);
2747 void (*GIVEREF)(void*, PyObject*, int);
2748 void* (*SetupContext)(const char*, int, const char*);
2749 void (*FinishContext)(void**);
2750 } __Pyx_RefNannyAPIStruct;
2751 static __Pyx_RefNannyAPIStruct *__Pyx_RefNanny = NULL;
2752 static __Pyx_RefNannyAPIStruct * __Pyx_RefNannyImportAPI(const char *modname) {
2753 PyObject *m = NULL, *p = NULL;
2755 m = PyImport_ImportModule((char *)modname);
2757 p = PyObject_GetAttrString(m, (char *)\"RefNannyAPI\");
2759 r = PyLong_AsVoidPtr(p);
2763 return (__Pyx_RefNannyAPIStruct *)r;
2765 #define __Pyx_RefNannySetupContext(name) \
2766 void *__pyx_refnanny = __Pyx_RefNanny->SetupContext((name), __LINE__, __FILE__)
2767 #define __Pyx_RefNannyFinishContext() \
2768 __Pyx_RefNanny->FinishContext(&__pyx_refnanny)
2769 #define __Pyx_INCREF(r) __Pyx_RefNanny->INCREF(__pyx_refnanny, (PyObject *)(r), __LINE__)
2770 #define __Pyx_DECREF(r) __Pyx_RefNanny->DECREF(__pyx_refnanny, (PyObject *)(r), __LINE__)
2771 #define __Pyx_GOTREF(r) __Pyx_RefNanny->GOTREF(__pyx_refnanny, (PyObject *)(r), __LINE__)
2772 #define __Pyx_GIVEREF(r) __Pyx_RefNanny->GIVEREF(__pyx_refnanny, (PyObject *)(r), __LINE__)
2773 #define __Pyx_XDECREF(r) do { if((r) != NULL) {__Pyx_DECREF(r);} } while(0)
2775 #define __Pyx_RefNannySetupContext(name)
2776 #define __Pyx_RefNannyFinishContext()
2777 #define __Pyx_INCREF(r) Py_INCREF(r)
2778 #define __Pyx_DECREF(r) Py_DECREF(r)
2779 #define __Pyx_GOTREF(r)
2780 #define __Pyx_GIVEREF(r)
2781 #define __Pyx_XDECREF(r) Py_XDECREF(r)
2782 #endif /* CYTHON_REFNANNY */
2783 #define __Pyx_XGIVEREF(r) do { if((r) != NULL) {__Pyx_GIVEREF(r);} } while(0)
2784 #define __Pyx_XGOTREF(r) do { if((r) != NULL) {__Pyx_GOTREF(r);} } while(0)
2788 main_method = UtilityCode(
2791 #include <floatingpoint.h>
2794 #if PY_MAJOR_VERSION < 3
2795 int %(main_method)s(int argc, char** argv) {
2796 #elif defined(WIN32) || defined(MS_WINDOWS)
2797 int %(wmain_method)s(int argc, wchar_t **argv) {
2799 static int __Pyx_main(int argc, wchar_t **argv) {
2801 /* 754 requires that FP exceptions run in "no stop" mode by default,
2802 * and until C vendors implement C99's ways to control FP exceptions,
2803 * Python requires non-stop mode. Alas, some platforms enable FP
2804 * exceptions by default. Here we disable them.
2810 fpsetmask(m & ~FP_X_OFL);
2813 Py_SetProgramName(argv[0]);
2816 PySys_SetArgv(argc, argv);
2817 { /* init module '%(module_name)s' as '__main__' */
2819 %(module_is_main)s = 1;
2820 #if PY_MAJOR_VERSION < 3
2821 init%(module_name)s();
2823 m = PyInit_%(module_name)s();
2825 if (PyErr_Occurred()) {
2826 PyErr_Print(); /* This exits with the right code if SystemExit. */
2827 #if PY_MAJOR_VERSION < 3
2828 if (Py_FlushLine()) PyErr_Clear();
2839 #if PY_MAJOR_VERSION >= 3 && !defined(WIN32) && !defined(MS_WINDOWS)
2843 __Pyx_char2wchar(char* arg)
2846 #ifdef HAVE_BROKEN_MBSTOWCS
2847 /* Some platforms have a broken implementation of
2848 * mbstowcs which does not count the characters that
2849 * would result from conversion. Use an upper bound.
2851 size_t argsize = strlen(arg);
2853 size_t argsize = mbstowcs(NULL, arg, 0);
2861 if (argsize != (size_t)-1) {
2862 res = (wchar_t *)malloc((argsize+1)*sizeof(wchar_t));
2865 count = mbstowcs(res, arg, argsize+1);
2866 if (count != (size_t)-1) {
2868 /* Only use the result if it contains no
2869 surrogate characters. */
2870 for (tmp = res; *tmp != 0 &&
2871 (*tmp < 0xd800 || *tmp > 0xdfff); tmp++)
2878 /* Conversion failed. Fall back to escaping with surrogateescape. */
2880 /* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */
2882 /* Overallocate; as multi-byte characters are in the argument, the
2883 actual output could use less memory. */
2884 argsize = strlen(arg) + 1;
2885 res = malloc(argsize*sizeof(wchar_t));
2887 in = (unsigned char*)arg;
2889 memset(&mbs, 0, sizeof mbs);
2891 size_t converted = mbrtowc(out, (char*)in, argsize, &mbs);
2893 /* Reached end of string; null char stored. */
2895 if (converted == (size_t)-2) {
2896 /* Incomplete character. This should never happen,
2897 since we provide everything that we have -
2898 unless there is a bug in the C library, or I
2899 misunderstood how mbrtowc works. */
2900 fprintf(stderr, "unexpected mbrtowc result -2\\n");
2903 if (converted == (size_t)-1) {
2904 /* Conversion error. Escape as UTF-8b, and start over
2905 in the initial shift state. */
2906 *out++ = 0xdc00 + *in++;
2908 memset(&mbs, 0, sizeof mbs);
2911 if (*out >= 0xd800 && *out <= 0xdfff) {
2912 /* Surrogate character. Escape the original
2913 byte sequence with surrogateescape. */
2914 argsize -= converted;
2916 *out++ = 0xdc00 + *in++;
2919 /* successfully converted some bytes */
2921 argsize -= converted;
2925 /* Cannot use C locale for escaping; manually escape as if charset
2926 is ASCII (i.e. escape all bytes > 128. This will still roundtrip
2927 correctly in the locale's charset, which must be an ASCII superset. */
2928 res = malloc((strlen(arg)+1)*sizeof(wchar_t));
2930 in = (unsigned char*)arg;
2936 *out++ = 0xdc00 + *in++;
2941 fprintf(stderr, "out of memory\\n");
2946 %(main_method)s(int argc, char **argv)
2949 return __Pyx_main(0, NULL);
2952 wchar_t **argv_copy = (wchar_t **)malloc(sizeof(wchar_t*)*argc);
2953 /* We need a second copies, as Python might modify the first one. */
2954 wchar_t **argv_copy2 = (wchar_t **)malloc(sizeof(wchar_t*)*argc);
2957 if (!argv_copy || !argv_copy2) {
2958 fprintf(stderr, "out of memory\\n");
2961 oldloc = strdup(setlocale(LC_ALL, NULL));
2962 setlocale(LC_ALL, "");
2963 for (i = 0; i < argc; i++) {
2964 argv_copy2[i] = argv_copy[i] = __Pyx_char2wchar(argv[i]);
2968 setlocale(LC_ALL, oldloc);
2970 res = __Pyx_main(argc, argv_copy);
2971 for (i = 0; i < argc; i++) {
2972 free(argv_copy2[i]);
2982 packed_struct_utility_code = UtilityCode(proto="""
2983 #if defined(__GNUC__)
2984 #define __Pyx_PACKED __attribute__((__packed__))
2986 #define __Pyx_PACKED
2988 """, impl="", proto_block='utility_code_proto_before_types')
2990 check_binary_version_utility_code = UtilityCode(proto="""
2991 static int __Pyx_check_binary_version(void);
2993 static int __Pyx_check_binary_version(void) {
2994 char ctversion[4], rtversion[4];
2995 PyOS_snprintf(ctversion, 4, "%d.%d", PY_MAJOR_VERSION, PY_MINOR_VERSION);
2996 PyOS_snprintf(rtversion, 4, "%s", Py_GetVersion());
2997 if (ctversion[0] != rtversion[0] || ctversion[2] != rtversion[2]) {
2999 PyOS_snprintf(message, sizeof(message),
3000 "compiletime version %s of module '%.100s' "
3001 "does not match runtime version %s",
3002 ctversion, __Pyx_MODULE_NAME, rtversion);
3003 #if PY_VERSION_HEX < 0x02050000
3004 return PyErr_Warn(NULL, message);
3006 return PyErr_WarnEx(NULL, message, 1);