Merge remote branch 'upstream/master'
[cython.git] / Cython / Compiler / ModuleNode.py
1 #
2 #   Pyrex - Module parse tree node
3 #
4
5 import cython
6 from cython import set
7 cython.declare(Naming=object, Options=object, PyrexTypes=object, TypeSlots=object,
8                error=object, warning=object, py_object_type=object, UtilityCode=object,
9                escape_byte_string=object, EncodedString=object)
10
11 import os, time
12 from PyrexTypes import CPtrType
13 import Future
14
15 import Annotate
16 import Code
17 import Naming
18 import Nodes
19 import Options
20 import PyrexTypes
21 import TypeSlots
22 import Version
23 import DebugFlags
24
25 from Errors import error, warning
26 from PyrexTypes import py_object_type
27 from Cython.Utils import open_new_file, replace_suffix
28 from Code import UtilityCode
29 from StringEncoding import escape_byte_string, EncodedString
30
31
32 def check_c_declarations_pxd(module_node):
33     module_node.scope.check_c_classes_pxd()
34     return module_node
35
36 def check_c_declarations(module_node):
37     module_node.scope.check_c_classes()
38     module_node.scope.check_c_functions()
39     return module_node
40
41 class ModuleNode(Nodes.Node, Nodes.BlockNode):
42     #  doc       string or None
43     #  body      StatListNode
44     #
45     #  referenced_modules   [ModuleScope]
46     #  full_module_name     string
47     #
48     #  scope                The module scope.
49     #  compilation_source   A CompilationSource (see Main)
50     #  directives           Top-level compiler directives
51
52     child_attrs = ["body"]
53     directives = None
54
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
61         else:
62             env.doc = self.doc
63         env.directives = self.directives
64         self.body.analyse_declarations(env)
65
66     def process_implementation(self, options, result):
67         env = self.scope
68         env.return_type = PyrexTypes.c_void_type
69         self.referenced_modules = []
70         self.find_referenced_modules(env, self.referenced_modules, {})
71         if options.recursive:
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)
76
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:
81                     return 1
82         return 0
83
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")
89             try:
90                 for module in modules:
91                     if module is not env:
92                         f.write("cimport %s\n" % module.qualified_name)
93                     for path in module.included_files:
94                         f.write("include %s\n" % path)
95             finally:
96                 f.close()
97
98     def generate_h_code(self, env, options, result):
99         def h_entries(entries, pxd = 0):
100             return [entry for entry in entries
101                 if entry.visibility == 'public' or pxd and entry.defined_in_pxd]
102         h_types = h_entries(env.type_entries)
103         h_vars = h_entries(env.var_entries)
104         h_funcs = h_entries(env.cfunc_entries)
105         h_extension_types = h_entries(env.c_class_entries)
106         if h_types or h_vars or h_funcs or h_extension_types:
107             result.h_file = replace_suffix(result.c_file, ".h")
108             h_code = Code.CCodeWriter()
109             Code.GlobalState(h_code)
110             if options.generate_pxi:
111                 result.i_file = replace_suffix(result.c_file, ".pxi")
112                 i_code = Code.PyrexCodeWriter(result.i_file)
113             else:
114                 i_code = None
115             guard = Naming.h_guard_prefix + env.qualified_name.replace(".", "__")
116             h_code.put_h_guard(guard)
117             self.generate_extern_c_macro_definition(h_code)
118             self.generate_type_header_code(h_types, h_code)
119             h_code.putln("")
120             h_code.putln("#ifndef %s" % Naming.api_guard_prefix + self.api_name(env))
121             if h_vars:
122                 h_code.putln("")
123                 for entry in h_vars:
124                     self.generate_public_declaration(entry, h_code, i_code)
125             if h_funcs:
126                 h_code.putln("")
127                 for entry in h_funcs:
128                     self.generate_public_declaration(entry, h_code, i_code)
129             if h_extension_types:
130                 h_code.putln("")
131                 for entry in h_extension_types:
132                     self.generate_cclass_header_code(entry.type, h_code)
133                     if i_code:
134                         self.generate_cclass_include_code(entry.type, i_code)
135             h_code.putln("")
136             h_code.putln("#endif")
137             h_code.putln("")
138             h_code.putln("PyMODINIT_FUNC init%s(void);" % env.module_name)
139             h_code.putln("")
140             h_code.putln("#endif")
141
142             f = open_new_file(result.h_file)
143             try:
144                 h_code.copyto(f)
145             finally:
146                 f.close()
147
148     def generate_public_declaration(self, entry, h_code, i_code):
149         h_code.putln("%s %s;" % (
150             Naming.extern_c_macro,
151             entry.type.declaration_code(
152                 entry.cname, dll_linkage = "DL_IMPORT")))
153         if i_code:
154             i_code.putln("cdef extern %s" %
155                 entry.type.declaration_code(entry.cname, pyrex = 1))
156
157     def api_name(self, env):
158         return env.qualified_name.replace(".", "__")
159
160     def generate_api_code(self, env, result):
161         api_funcs = []
162         public_extension_types = []
163         has_api_extension_types = 0
164         for entry in env.cfunc_entries:
165             if entry.api:
166                 api_funcs.append(entry)
167         for entry in env.c_class_entries:
168             if entry.visibility == 'public':
169                 public_extension_types.append(entry)
170             if entry.api:
171                 has_api_extension_types = 1
172         if api_funcs or has_api_extension_types:
173             result.api_file = replace_suffix(result.c_file, "_api.h")
174             h_code = Code.CCodeWriter()
175             Code.GlobalState(h_code)
176             name = self.api_name(env)
177             guard = Naming.api_guard_prefix + name
178             h_code.put_h_guard(guard)
179             h_code.putln('#include "Python.h"')
180             if result.h_file:
181                 h_code.putln('#include "%s"' % os.path.basename(result.h_file))
182             for entry in public_extension_types:
183                 type = entry.type
184                 h_code.putln("")
185                 h_code.putln("static PyTypeObject *%s;" % type.typeptr_cname)
186                 h_code.putln("#define %s (*%s)" % (
187                     type.typeobj_cname, type.typeptr_cname))
188             if api_funcs:
189                 h_code.putln("")
190                 for entry in api_funcs:
191                     type = CPtrType(entry.type)
192                     h_code.putln("static %s;" % type.declaration_code(entry.cname))
193             h_code.putln("")
194             h_code.put_h_guard(Naming.api_func_guard + "import_module")
195             h_code.put(import_module_utility_code.impl)
196             h_code.putln("")
197             h_code.putln("#endif")
198             if api_funcs:
199                 h_code.putln("")
200                 h_code.put(function_import_utility_code.impl)
201             if public_extension_types:
202                 h_code.putln("")
203                 h_code.put(type_import_utility_code.impl)
204             h_code.putln("")
205             h_code.putln("static int import_%s(void) {" % name)
206             h_code.putln("PyObject *module = 0;")
207             h_code.putln('module = __Pyx_ImportModule("%s");' % env.qualified_name)
208             h_code.putln("if (!module) goto bad;")
209             for entry in api_funcs:
210                 sig = entry.type.signature_string()
211                 h_code.putln(
212                     'if (__Pyx_ImportFunction(module, "%s", (void (**)(void))&%s, "%s") < 0) goto bad;' % (
213                         entry.name,
214                         entry.cname,
215                         sig))
216             h_code.putln("Py_DECREF(module); module = 0;")
217             for entry in public_extension_types:
218                 self.generate_type_import_call(
219                     entry.type, h_code,
220                     "if (!%s) goto bad;" % entry.type.typeptr_cname)
221             h_code.putln("return 0;")
222             h_code.putln("bad:")
223             h_code.putln("Py_XDECREF(module);")
224             h_code.putln("return -1;")
225             h_code.putln("}")
226             h_code.putln("")
227             h_code.putln("#endif")
228
229             f = open_new_file(result.api_file)
230             try:
231                 h_code.copyto(f)
232             finally:
233                 f.close()
234
235     def generate_cclass_header_code(self, type, h_code):
236         h_code.putln("%s DL_IMPORT(PyTypeObject) %s;" % (
237             Naming.extern_c_macro,
238             type.typeobj_cname))
239
240     def generate_cclass_include_code(self, type, i_code):
241         i_code.putln("cdef extern class %s.%s:" % (
242             type.module_name, type.name))
243         i_code.indent()
244         var_entries = type.scope.var_entries
245         if var_entries:
246             for entry in var_entries:
247                 i_code.putln("cdef %s" %
248                     entry.type.declaration_code(entry.cname, pyrex = 1))
249         else:
250             i_code.putln("pass")
251         i_code.dedent()
252
253     def generate_c_code(self, env, options, result):
254         modules = self.referenced_modules
255
256         if Options.annotate or options.annotate:
257             emit_linenums = False
258             rootwriter = Annotate.AnnotationCCodeWriter()
259         else:
260             emit_linenums = options.emit_linenums
261             rootwriter = Code.CCodeWriter(emit_linenums=emit_linenums)
262         globalstate = Code.GlobalState(rootwriter, emit_linenums)
263         globalstate.initialize_main_c_code()
264         h_code = globalstate['h_code']
265
266         self.generate_module_preamble(env, modules, h_code)
267
268         globalstate.module_pos = self.pos
269         globalstate.directives = self.directives
270
271         globalstate.use_utility_code(refnanny_utility_code)
272
273         code = globalstate['before_global_var']
274         code.putln('#define __Pyx_MODULE_NAME "%s"' % self.full_module_name)
275         code.putln("static int %s%s = 0;" % (Naming.module_is_main, self.full_module_name.replace('.', '__')))
276         code.putln("")
277         code.putln("/* Implementation of %s */" % env.qualified_name)
278
279         code = globalstate['all_the_rest']
280
281         self.generate_cached_builtins_decls(env, code)
282         self.generate_lambda_definitions(env, code)
283         # generate normal function definitions
284         self.body.generate_function_definitions(env, code)
285         code.mark_pos(None)
286         self.generate_typeobj_definitions(env, code)
287         self.generate_method_table(env, code)
288         if env.has_import_star:
289             self.generate_import_star(env, code)
290         self.generate_pymoduledef_struct(env, code)
291
292         # init_globals is inserted before this
293         self.generate_module_init_func(modules[:-1], env, globalstate['init_module'])
294         self.generate_module_cleanup_func(env, globalstate['cleanup_module'])
295         if Options.embed:
296             self.generate_main_method(env, globalstate['main_method'])
297         self.generate_filename_table(globalstate['filename_table'])
298
299         self.generate_declarations_for_modules(env, modules, globalstate)
300         h_code.write('\n')
301
302         for utilcode in env.utility_code_list:
303             globalstate.use_utility_code(utilcode)
304         globalstate.finalize_main_c_code()
305
306         f = open_new_file(result.c_file)
307         rootwriter.copyto(f)
308         if options.gdb_debug:
309             self._serialize_lineno_map(env, rootwriter)
310         f.close()
311         result.c_file_generated = 1
312         if Options.annotate or options.annotate:
313             self.annotate(rootwriter)
314             rootwriter.save_annotation(result.main_source_file, result.c_file)
315
316     def _serialize_lineno_map(self, env, ccodewriter):
317         tb = env.context.gdb_debug_outputwriter
318         markers = ccodewriter.buffer.allmarkers()
319
320         d = {}
321         for c_lineno, cython_lineno in enumerate(markers):
322             if cython_lineno > 0:
323                 d.setdefault(cython_lineno, []).append(c_lineno + 1)
324
325         tb.start('LineNumberMapping')
326         for cython_lineno, c_linenos in sorted(d.iteritems()):
327                 attrs = {
328                     'c_linenos': ' '.join(map(str, c_linenos)),
329                     'cython_lineno': str(cython_lineno),
330                 }
331                 tb.start('LineNumber', attrs)
332                 tb.end('LineNumber')
333         tb.end('LineNumberMapping')
334         tb.serialize()
335
336     def find_referenced_modules(self, env, module_list, modules_seen):
337         if env not in modules_seen:
338             modules_seen[env] = 1
339             for imported_module in env.cimported_modules:
340                 self.find_referenced_modules(imported_module, module_list, modules_seen)
341             module_list.append(env)
342
343     def sort_types_by_inheritance(self, type_dict, getkey):
344         # copy the types into a list moving each parent type before
345         # its first child
346         type_items = type_dict.items()
347         type_list = []
348         for i, item in enumerate(type_items):
349             key, new_entry = item
350
351             # collect all base classes to check for children
352             hierarchy = set()
353             base = new_entry
354             while base:
355                 base_type = base.type.base_type
356                 if not base_type:
357                     break
358                 base_key = getkey(base_type)
359                 hierarchy.add(base_key)
360                 base = type_dict.get(base_key)
361             new_entry.base_keys = hierarchy
362
363             # find the first (sub-)subclass and insert before that
364             for j in range(i):
365                 entry = type_list[j]
366                 if key in entry.base_keys:
367                     type_list.insert(j, new_entry)
368                     break
369             else:
370                 type_list.append(new_entry)
371         return type_list
372
373     def sort_type_hierarchy(self, module_list, env):
374         vtab_dict = {}
375         vtabslot_dict = {}
376         for module in module_list:
377             for entry in module.c_class_entries:
378                 if not entry.in_cinclude:
379                     type = entry.type
380                     if type.vtabstruct_cname:
381                         vtab_dict[type.vtabstruct_cname] = entry
382             all_defined_here = module is env
383             for entry in module.type_entries:
384                 if all_defined_here or entry.defined_in_pxd:
385                     type = entry.type
386                     if type.is_extension_type and not entry.in_cinclude:
387                         type = entry.type
388                         vtabslot_dict[type.objstruct_cname] = entry
389
390         def vtabstruct_cname(entry_type):
391             return entry_type.vtabstruct_cname
392         vtab_list = self.sort_types_by_inheritance(
393             vtab_dict, vtabstruct_cname)
394
395         def objstruct_cname(entry_type):
396             return entry_type.objstruct_cname
397         vtabslot_list = self.sort_types_by_inheritance(
398             vtabslot_dict, objstruct_cname)
399
400         return (vtab_list, vtabslot_list)
401
402     def generate_type_definitions(self, env, modules, vtab_list, vtabslot_list, code):
403         vtabslot_entries = set(vtabslot_list)
404         for module in modules:
405             definition = module is env
406             if definition:
407                 type_entries = module.type_entries
408             else:
409                 type_entries = []
410                 for entry in module.type_entries:
411                     if entry.defined_in_pxd:
412                         type_entries.append(entry)
413             for entry in type_entries:
414                 if not entry.in_cinclude:
415                     #print "generate_type_header_code:", entry.name, repr(entry.type) ###
416                     type = entry.type
417                     if type.is_typedef: # Must test this first!
418                         self.generate_typedef(entry, code)
419                     elif type.is_struct_or_union:
420                         self.generate_struct_union_definition(entry, code)
421                     elif type.is_enum:
422                         self.generate_enum_definition(entry, code)
423                     elif type.is_extension_type and entry not in vtabslot_entries:
424                         self.generate_objstruct_definition(type, code)
425         for entry in vtabslot_list:
426             self.generate_objstruct_definition(entry.type, code)
427         for entry in vtab_list:
428             self.generate_typeobject_predeclaration(entry, code)
429             self.generate_exttype_vtable_struct(entry, code)
430             self.generate_exttype_vtabptr_declaration(entry, code)
431
432     def generate_declarations_for_modules(self, env, modules, globalstate):
433         typecode = globalstate['type_declarations']
434         typecode.putln("")
435         typecode.putln("/* Type declarations */")
436         vtab_list, vtabslot_list = self.sort_type_hierarchy(modules, env)
437         self.generate_type_definitions(
438             env, modules, vtab_list, vtabslot_list, typecode)
439         modulecode = globalstate['module_declarations']
440         for module in modules:
441             defined_here = module is env
442             modulecode.putln("/* Module declarations from %s */" %
443                              module.qualified_name)
444             self.generate_global_declarations(module, modulecode, defined_here)
445             self.generate_cfunction_predeclarations(module, modulecode, defined_here)
446
447     def generate_module_preamble(self, env, cimported_modules, code):
448         code.putln("/* Generated by Cython %s on %s */" % (
449             Version.version, time.asctime()))
450         code.putln("")
451         code.putln("#define PY_SSIZE_T_CLEAN")
452         for filename in env.python_include_files:
453             code.putln('#include "%s"' % filename)
454         code.putln("#ifndef Py_PYTHON_H")
455         code.putln("    #error Python headers needed to compile C extensions, please install development version of Python.")
456         code.putln("#else")
457         code.globalstate["end"].putln("#endif /* Py_PYTHON_H */")
458
459         code.put("""
460 #include <stddef.h> /* For offsetof */
461 #ifndef offsetof
462 #define offsetof(type, member) ( (size_t) & ((type*)0) -> member )
463 #endif
464
465 #if !defined(WIN32) && !defined(MS_WINDOWS)
466   #ifndef __stdcall
467     #define __stdcall
468   #endif
469   #ifndef __cdecl
470     #define __cdecl
471   #endif
472   #ifndef __fastcall
473     #define __fastcall
474   #endif
475 #endif
476
477 #ifndef DL_IMPORT
478   #define DL_IMPORT(t) t
479 #endif
480 #ifndef DL_EXPORT
481   #define DL_EXPORT(t) t
482 #endif
483
484 #ifndef PY_LONG_LONG
485   #define PY_LONG_LONG LONG_LONG
486 #endif
487
488 #if PY_VERSION_HEX < 0x02040000
489   #define METH_COEXIST 0
490   #define PyDict_CheckExact(op) (Py_TYPE(op) == &PyDict_Type)
491   #define PyDict_Contains(d,o)   PySequence_Contains(d,o)
492 #endif
493
494 #if PY_VERSION_HEX < 0x02050000
495   typedef int Py_ssize_t;
496   #define PY_SSIZE_T_MAX INT_MAX
497   #define PY_SSIZE_T_MIN INT_MIN
498   #define PY_FORMAT_SIZE_T \"\"
499   #define PyInt_FromSsize_t(z) PyInt_FromLong(z)
500   #define PyInt_AsSsize_t(o)   PyInt_AsLong(o)
501   #define PyNumber_Index(o)    PyNumber_Int(o)
502   #define PyIndex_Check(o)     PyNumber_Check(o)
503   #define PyErr_WarnEx(category, message, stacklevel) PyErr_Warn(category, message)
504 #endif
505
506 #if PY_VERSION_HEX < 0x02060000
507   #define Py_REFCNT(ob) (((PyObject*)(ob))->ob_refcnt)
508   #define Py_TYPE(ob)   (((PyObject*)(ob))->ob_type)
509   #define Py_SIZE(ob)   (((PyVarObject*)(ob))->ob_size)
510   #define PyVarObject_HEAD_INIT(type, size) \\
511           PyObject_HEAD_INIT(type) size,
512   #define PyType_Modified(t)
513
514   typedef struct {
515      void *buf;
516      PyObject *obj;
517      Py_ssize_t len;
518      Py_ssize_t itemsize;
519      int readonly;
520      int ndim;
521      char *format;
522      Py_ssize_t *shape;
523      Py_ssize_t *strides;
524      Py_ssize_t *suboffsets;
525      void *internal;
526   } Py_buffer;
527
528   #define PyBUF_SIMPLE 0
529   #define PyBUF_WRITABLE 0x0001
530   #define PyBUF_FORMAT 0x0004
531   #define PyBUF_ND 0x0008
532   #define PyBUF_STRIDES (0x0010 | PyBUF_ND)
533   #define PyBUF_C_CONTIGUOUS (0x0020 | PyBUF_STRIDES)
534   #define PyBUF_F_CONTIGUOUS (0x0040 | PyBUF_STRIDES)
535   #define PyBUF_ANY_CONTIGUOUS (0x0080 | PyBUF_STRIDES)
536   #define PyBUF_INDIRECT (0x0100 | PyBUF_STRIDES)
537
538 #endif
539
540 #if PY_MAJOR_VERSION < 3
541   #define __Pyx_BUILTIN_MODULE_NAME "__builtin__"
542 #else
543   #define __Pyx_BUILTIN_MODULE_NAME "builtins"
544 #endif
545
546 #if PY_MAJOR_VERSION >= 3
547   #define Py_TPFLAGS_CHECKTYPES 0
548   #define Py_TPFLAGS_HAVE_INDEX 0
549 #endif
550
551 #if (PY_VERSION_HEX < 0x02060000) || (PY_MAJOR_VERSION >= 3)
552   #define Py_TPFLAGS_HAVE_NEWBUFFER 0
553 #endif
554
555 #if PY_MAJOR_VERSION >= 3
556   #define PyBaseString_Type            PyUnicode_Type
557   #define PyStringObject               PyUnicodeObject
558   #define PyString_Type                PyUnicode_Type
559   #define PyString_Check               PyUnicode_Check
560   #define PyString_CheckExact          PyUnicode_CheckExact
561 #endif
562
563 #if PY_VERSION_HEX < 0x02060000
564   #define PyBytesObject                PyStringObject
565   #define PyBytes_Type                 PyString_Type
566   #define PyBytes_Check                PyString_Check
567   #define PyBytes_CheckExact           PyString_CheckExact
568   #define PyBytes_FromString           PyString_FromString
569   #define PyBytes_FromStringAndSize    PyString_FromStringAndSize
570   #define PyBytes_FromFormat           PyString_FromFormat
571   #define PyBytes_DecodeEscape         PyString_DecodeEscape
572   #define PyBytes_AsString             PyString_AsString
573   #define PyBytes_AsStringAndSize      PyString_AsStringAndSize
574   #define PyBytes_Size                 PyString_Size
575   #define PyBytes_AS_STRING            PyString_AS_STRING
576   #define PyBytes_GET_SIZE             PyString_GET_SIZE
577   #define PyBytes_Repr                 PyString_Repr
578   #define PyBytes_Concat               PyString_Concat
579   #define PyBytes_ConcatAndDel         PyString_ConcatAndDel
580 #endif
581
582 #if PY_VERSION_HEX < 0x02060000
583   #define PySet_Check(obj)             PyObject_TypeCheck(obj, &PySet_Type)
584   #define PyFrozenSet_Check(obj)       PyObject_TypeCheck(obj, &PyFrozenSet_Type)
585 #endif
586 #ifndef PySet_CheckExact
587   #define PySet_CheckExact(obj)        (Py_TYPE(obj) == &PySet_Type)
588 #endif
589
590 #define __Pyx_TypeCheck(obj, type) PyObject_TypeCheck(obj, (PyTypeObject *)type)
591
592 #if PY_MAJOR_VERSION >= 3
593   #define PyIntObject                  PyLongObject
594   #define PyInt_Type                   PyLong_Type
595   #define PyInt_Check(op)              PyLong_Check(op)
596   #define PyInt_CheckExact(op)         PyLong_CheckExact(op)
597   #define PyInt_FromString             PyLong_FromString
598   #define PyInt_FromUnicode            PyLong_FromUnicode
599   #define PyInt_FromLong               PyLong_FromLong
600   #define PyInt_FromSize_t             PyLong_FromSize_t
601   #define PyInt_FromSsize_t            PyLong_FromSsize_t
602   #define PyInt_AsLong                 PyLong_AsLong
603   #define PyInt_AS_LONG                PyLong_AS_LONG
604   #define PyInt_AsSsize_t              PyLong_AsSsize_t
605   #define PyInt_AsUnsignedLongMask     PyLong_AsUnsignedLongMask
606   #define PyInt_AsUnsignedLongLongMask PyLong_AsUnsignedLongLongMask
607 #endif
608
609 #if PY_MAJOR_VERSION >= 3
610   #define PyBoolObject                 PyLongObject
611 #endif
612
613 """)
614
615         code.put("""
616 #if PY_MAJOR_VERSION >= 3
617   #define __Pyx_PyNumber_Divide(x,y)         PyNumber_TrueDivide(x,y)
618   #define __Pyx_PyNumber_InPlaceDivide(x,y)  PyNumber_InPlaceTrueDivide(x,y)
619 #else
620 """)
621         if Future.division in env.context.future_directives:
622             code.putln("  #define __Pyx_PyNumber_Divide(x,y)         PyNumber_TrueDivide(x,y)")
623             code.putln("  #define __Pyx_PyNumber_InPlaceDivide(x,y)  PyNumber_InPlaceTrueDivide(x,y)")
624         else:
625             code.putln("  #define __Pyx_PyNumber_Divide(x,y)         PyNumber_Divide(x,y)")
626             code.putln("  #define __Pyx_PyNumber_InPlaceDivide(x,y)  PyNumber_InPlaceDivide(x,y)")
627         code.putln("#endif")
628
629         code.put("""
630 #if (PY_MAJOR_VERSION < 3) || (PY_VERSION_HEX >= 0x03010300)
631   #define __Pyx_PySequence_GetSlice(obj, a, b) PySequence_GetSlice(obj, a, b)
632   #define __Pyx_PySequence_SetSlice(obj, a, b, value) PySequence_SetSlice(obj, a, b, value)
633   #define __Pyx_PySequence_DelSlice(obj, a, b) PySequence_DelSlice(obj, a, b)
634 #else
635   #define __Pyx_PySequence_GetSlice(obj, a, b) (unlikely(!(obj)) ? \\
636         (PyErr_SetString(PyExc_SystemError, "null argument to internal routine"), (PyObject*)0) : \\
637         (likely((obj)->ob_type->tp_as_mapping) ? (PySequence_GetSlice(obj, a, b)) : \\
638             (PyErr_Format(PyExc_TypeError, "'%.200s' object is unsliceable", (obj)->ob_type->tp_name), (PyObject*)0)))
639   #define __Pyx_PySequence_SetSlice(obj, a, b, value) (unlikely(!(obj)) ? \\
640         (PyErr_SetString(PyExc_SystemError, "null argument to internal routine"), -1) : \\
641         (likely((obj)->ob_type->tp_as_mapping) ? (PySequence_SetSlice(obj, a, b, value)) : \\
642             (PyErr_Format(PyExc_TypeError, "'%.200s' object doesn't support slice assignment", (obj)->ob_type->tp_name), -1)))
643   #define __Pyx_PySequence_DelSlice(obj, a, b) (unlikely(!(obj)) ? \\
644         (PyErr_SetString(PyExc_SystemError, "null argument to internal routine"), -1) : \\
645         (likely((obj)->ob_type->tp_as_mapping) ? (PySequence_DelSlice(obj, a, b)) : \\
646             (PyErr_Format(PyExc_TypeError, "'%.200s' object doesn't support slice deletion", (obj)->ob_type->tp_name), -1)))
647 #endif
648
649 #if PY_MAJOR_VERSION >= 3
650   #define PyMethod_New(func, self, klass) ((self) ? PyMethod_New(func, self) : PyInstanceMethod_New(func))
651 #endif
652
653 #if PY_VERSION_HEX < 0x02050000
654   #define __Pyx_GetAttrString(o,n)   PyObject_GetAttrString((o),((char *)(n)))
655   #define __Pyx_SetAttrString(o,n,a) PyObject_SetAttrString((o),((char *)(n)),(a))
656   #define __Pyx_DelAttrString(o,n)   PyObject_DelAttrString((o),((char *)(n)))
657 #else
658   #define __Pyx_GetAttrString(o,n)   PyObject_GetAttrString((o),(n))
659   #define __Pyx_SetAttrString(o,n,a) PyObject_SetAttrString((o),(n),(a))
660   #define __Pyx_DelAttrString(o,n)   PyObject_DelAttrString((o),(n))
661 #endif
662
663 #if PY_VERSION_HEX < 0x02050000
664   #define __Pyx_NAMESTR(n) ((char *)(n))
665   #define __Pyx_DOCSTR(n)  ((char *)(n))
666 #else
667   #define __Pyx_NAMESTR(n) (n)
668   #define __Pyx_DOCSTR(n)  (n)
669 #endif
670 """)
671
672         code.putln("")
673         self.generate_extern_c_macro_definition(code)
674         code.putln("")
675         code.putln("#if defined(WIN32) || defined(MS_WINDOWS)")
676         code.putln("#define _USE_MATH_DEFINES")
677         code.putln("#endif")
678         code.putln("#include <math.h>")
679         code.putln("#define %s" % Naming.api_guard_prefix + self.api_name(env))
680         self.generate_includes(env, cimported_modules, code)
681         code.putln("")
682         code.putln("#ifdef PYREX_WITHOUT_ASSERTIONS")
683         code.putln("#define CYTHON_WITHOUT_ASSERTIONS")
684         code.putln("#endif")
685         code.putln("")
686         if env.directives['ccomplex']:
687             code.putln("")
688             code.putln("#if !defined(CYTHON_CCOMPLEX)")
689             code.putln("#define CYTHON_CCOMPLEX 1")
690             code.putln("#endif")
691             code.putln("")
692         code.put(Nodes.utility_function_predeclarations)
693         code.put(PyrexTypes.type_conversion_predeclarations)
694         code.put(Nodes.branch_prediction_macros)
695         code.putln('')
696         code.putln('static PyObject *%s;' % env.module_cname)
697         code.putln('static PyObject *%s;' % Naming.builtins_cname)
698         code.putln('static PyObject *%s;' % Naming.empty_tuple)
699         code.putln('static PyObject *%s;' % Naming.empty_bytes)
700         if Options.pre_import is not None:
701             code.putln('static PyObject *%s;' % Naming.preimport_cname)
702         code.putln('static int %s;' % Naming.lineno_cname)
703         code.putln('static int %s = 0;' % Naming.clineno_cname)
704         code.putln('static const char * %s= %s;' % (Naming.cfilenm_cname, Naming.file_c_macro))
705         code.putln('static const char *%s;' % Naming.filename_cname)
706
707         # XXX this is a mess
708         for utility_code in PyrexTypes.c_int_from_py_function.specialize_list:
709             env.use_utility_code(utility_code)
710         for utility_code in PyrexTypes.c_long_from_py_function.specialize_list:
711             env.use_utility_code(utility_code)
712
713     def generate_extern_c_macro_definition(self, code):
714         name = Naming.extern_c_macro
715         code.putln("#ifdef __cplusplus")
716         code.putln('#define %s extern "C"' % name)
717         code.putln("#else")
718         code.putln("#define %s extern" % name)
719         code.putln("#endif")
720
721     def generate_includes(self, env, cimported_modules, code):
722         includes = []
723         for filename in env.include_files:
724             byte_decoded_filenname = str(filename)
725             if byte_decoded_filenname[0] == '<' and byte_decoded_filenname[-1] == '>':
726                 code.putln('#include %s' % byte_decoded_filenname)
727             else:
728                 code.putln('#include "%s"' % byte_decoded_filenname)
729
730     def generate_filename_table(self, code):
731         code.putln("")
732         code.putln("static const char *%s[] = {" % Naming.filetable_cname)
733         if code.globalstate.filename_list:
734             for source_desc in code.globalstate.filename_list:
735                 filename = os.path.basename(source_desc.get_filenametable_entry())
736                 escaped_filename = filename.replace("\\", "\\\\").replace('"', r'\"')
737                 code.putln('"%s",' % escaped_filename)
738         else:
739             # Some C compilers don't like an empty array
740             code.putln("0")
741         code.putln("};")
742
743     def generate_type_predeclarations(self, env, code):
744         pass
745
746     def generate_type_header_code(self, type_entries, code):
747         # Generate definitions of structs/unions/enums/typedefs/objstructs.
748         #self.generate_gcc33_hack(env, code) # Is this still needed?
749         #for entry in env.type_entries:
750         for entry in type_entries:
751             if not entry.in_cinclude:
752                 #print "generate_type_header_code:", entry.name, repr(entry.type) ###
753                 type = entry.type
754                 if type.is_typedef: # Must test this first!
755                     self.generate_typedef(entry, code)
756                 elif type.is_struct_or_union:
757                     self.generate_struct_union_definition(entry, code)
758                 elif type.is_enum:
759                     self.generate_enum_definition(entry, code)
760                 elif type.is_extension_type:
761                     self.generate_objstruct_definition(type, code)
762
763     def generate_gcc33_hack(self, env, code):
764         # Workaround for spurious warning generation in gcc 3.3
765         code.putln("")
766         for entry in env.c_class_entries:
767             type = entry.type
768             if not type.typedef_flag:
769                 name = type.objstruct_cname
770                 if name.startswith("__pyx_"):
771                     tail = name[6:]
772                 else:
773                     tail = name
774                 code.putln("typedef struct %s __pyx_gcc33_%s;" % (
775                     name, tail))
776
777     def generate_typedef(self, entry, code):
778         base_type = entry.type.typedef_base_type
779         if base_type.is_numeric:
780             writer = code.globalstate['numeric_typedefs']
781         else:
782             writer = code
783         writer.putln("")
784         writer.putln("typedef %s;" % base_type.declaration_code(entry.cname))
785
786     def sue_header_footer(self, type, kind, name):
787         if type.typedef_flag:
788             header = "typedef %s {" % kind
789             footer = "} %s;" % name
790         else:
791             header = "%s %s {" % (kind, name)
792             footer = "};"
793         return header, footer
794
795     def generate_struct_union_definition(self, entry, code):
796         code.mark_pos(entry.pos)
797         type = entry.type
798         scope = type.scope
799         if scope:
800             kind = type.kind
801             packed = type.is_struct and type.packed
802             if packed:
803                 kind = "%s %s" % (type.kind, "__Pyx_PACKED")
804                 code.globalstate.use_utility_code(packed_struct_utility_code)
805             header, footer = \
806                 self.sue_header_footer(type, kind, type.cname)
807             code.putln("")
808             if packed:
809                 code.putln("#if defined(__SUNPRO_C)")
810                 code.putln("  #pragma pack(1)")
811                 code.putln("#elif !defined(__GNUC__)")
812                 code.putln("  #pragma pack(push, 1)")
813                 code.putln("#endif")
814             code.putln(header)
815             var_entries = scope.var_entries
816             if not var_entries:
817                 error(entry.pos,
818                     "Empty struct or union definition not allowed outside a"
819                     " 'cdef extern from' block")
820             for attr in var_entries:
821                 code.putln(
822                     "%s;" %
823                         attr.type.declaration_code(attr.cname))
824             code.putln(footer)
825             if packed:
826                 code.putln("#if defined(__SUNPRO_C)")
827                 code.putln("  #pragma pack()")
828                 code.putln("#elif !defined(__GNUC__)")
829                 code.putln("  #pragma pack(pop)")
830                 code.putln("#endif")
831
832     def generate_enum_definition(self, entry, code):
833         code.mark_pos(entry.pos)
834         type = entry.type
835         name = entry.cname or entry.name or ""
836         header, footer = \
837             self.sue_header_footer(type, "enum", name)
838         code.putln("")
839         code.putln(header)
840         enum_values = entry.enum_values
841         if not enum_values:
842             error(entry.pos,
843                 "Empty enum definition not allowed outside a"
844                 " 'cdef extern from' block")
845         else:
846             last_entry = enum_values[-1]
847             # this does not really generate code, just builds the result value
848             for value_entry in enum_values:
849                 if value_entry.value_node is not None:
850                     value_entry.value_node.generate_evaluation_code(code)
851
852             for value_entry in enum_values:
853                 if value_entry.value_node is None:
854                     value_code = value_entry.cname
855                 else:
856                     value_code = ("%s = %s" % (
857                         value_entry.cname,
858                         value_entry.value_node.result()))
859                 if value_entry is not last_entry:
860                     value_code += ","
861                 code.putln(value_code)
862         code.putln(footer)
863
864     def generate_typeobject_predeclaration(self, entry, code):
865         code.putln("")
866         name = entry.type.typeobj_cname
867         if name:
868             if entry.visibility == 'extern' and not entry.in_cinclude:
869                 code.putln("%s DL_IMPORT(PyTypeObject) %s;" % (
870                     Naming.extern_c_macro,
871                     name))
872             elif entry.visibility == 'public':
873                 code.putln("%s DL_EXPORT(PyTypeObject) %s;" % (
874                     Naming.extern_c_macro,
875                     name))
876             # ??? Do we really need the rest of this? ???
877             #else:
878             #    code.putln("staticforward PyTypeObject %s;" % name)
879
880     def generate_exttype_vtable_struct(self, entry, code):
881         code.mark_pos(entry.pos)
882         # Generate struct declaration for an extension type's vtable.
883         type = entry.type
884         scope = type.scope
885         if type.vtabstruct_cname:
886             code.putln("")
887             code.putln(
888                 "struct %s {" %
889                     type.vtabstruct_cname)
890             if type.base_type and type.base_type.vtabstruct_cname:
891                 code.putln("struct %s %s;" % (
892                     type.base_type.vtabstruct_cname,
893                     Naming.obj_base_cname))
894             for method_entry in scope.cfunc_entries:
895                 if not method_entry.is_inherited:
896                     code.putln(
897                         "%s;" % method_entry.type.declaration_code("(*%s)" % method_entry.name))
898             code.putln(
899                 "};")
900
901     def generate_exttype_vtabptr_declaration(self, entry, code):
902         code.mark_pos(entry.pos)
903         # Generate declaration of pointer to an extension type's vtable.
904         type = entry.type
905         if type.vtabptr_cname:
906             code.putln("static struct %s *%s;" % (
907                 type.vtabstruct_cname,
908                 type.vtabptr_cname))
909
910     def generate_objstruct_definition(self, type, code):
911         code.mark_pos(type.pos)
912         # Generate object struct definition for an
913         # extension type.
914         if not type.scope:
915             return # Forward declared but never defined
916         header, footer = \
917             self.sue_header_footer(type, "struct", type.objstruct_cname)
918         code.putln("")
919         code.putln(header)
920         base_type = type.base_type
921         if base_type:
922             code.putln(
923                 "%s%s %s;" % (
924                     ("struct ", "")[base_type.typedef_flag],
925                     base_type.objstruct_cname,
926                     Naming.obj_base_cname))
927         else:
928             code.putln(
929                 "PyObject_HEAD")
930         if type.vtabslot_cname and not (type.base_type and type.base_type.vtabslot_cname):
931             code.putln(
932                 "struct %s *%s;" % (
933                     type.vtabstruct_cname,
934                     type.vtabslot_cname))
935         for attr in type.scope.var_entries:
936             code.putln(
937                 "%s;" %
938                     attr.type.declaration_code(attr.cname))
939         code.putln(footer)
940         if type.objtypedef_cname is not None:
941             # Only for exposing public typedef name.
942             code.putln("typedef struct %s %s;" % (type.objstruct_cname, type.objtypedef_cname))
943
944     def generate_global_declarations(self, env, code, definition):
945         code.putln("")
946         for entry in env.c_class_entries:
947             if definition or entry.defined_in_pxd:
948                 code.putln("static PyTypeObject *%s = 0;" %
949                     entry.type.typeptr_cname)
950         code.put_var_declarations(env.var_entries, static = 1,
951             dll_linkage = "DL_EXPORT", definition = definition)
952
953     def generate_cfunction_predeclarations(self, env, code, definition):
954         for entry in env.cfunc_entries:
955             if entry.inline_func_in_pxd or (not entry.in_cinclude and (definition
956                     or entry.defined_in_pxd or entry.visibility == 'extern')):
957                 if entry.visibility in ('public', 'extern'):
958                     dll_linkage = "DL_EXPORT"
959                 else:
960                     dll_linkage = None
961                 type = entry.type
962                 if not definition and entry.defined_in_pxd:
963                     type = CPtrType(type)
964                 header = type.declaration_code(entry.cname,
965                     dll_linkage = dll_linkage)
966                 if entry.visibility == 'private':
967                     storage_class = "static "
968                 elif entry.visibility == 'public':
969                     storage_class = ""
970                 else:
971                     storage_class = "%s " % Naming.extern_c_macro
972                 if entry.func_modifiers:
973                     modifiers = '%s ' % ' '.join([
974                             modifier.upper() for modifier in entry.func_modifiers])
975                 else:
976                     modifiers = ''
977                 code.putln("%s%s%s; /*proto*/" % (
978                     storage_class,
979                     modifiers,
980                     header))
981
982     def generate_typeobj_definitions(self, env, code):
983         full_module_name = env.qualified_name
984         for entry in env.c_class_entries:
985             #print "generate_typeobj_definitions:", entry.name
986             #print "...visibility =", entry.visibility
987             if entry.visibility != 'extern':
988                 type = entry.type
989                 scope = type.scope
990                 if scope: # could be None if there was an error
991                     self.generate_exttype_vtable(scope, code)
992                     self.generate_new_function(scope, code)
993                     self.generate_dealloc_function(scope, code)
994                     if scope.needs_gc():
995                         self.generate_traverse_function(scope, code)
996                         self.generate_clear_function(scope, code)
997                     if scope.defines_any(["__getitem__"]):
998                         self.generate_getitem_int_function(scope, code)
999                     if scope.defines_any(["__setitem__", "__delitem__"]):
1000                         self.generate_ass_subscript_function(scope, code)
1001                     if scope.defines_any(["__getslice__", "__setslice__", "__delslice__"]):
1002                         warning(self.pos, "__getslice__, __setslice__, and __delslice__ are not supported by Python 3, use __getitem__, __setitem__, and __delitem__ instead", 1)
1003                         code.putln("#if PY_MAJOR_VERSION >= 3")
1004                         code.putln("#error __getslice__, __setslice__, and __delslice__ not supported in Python 3.")
1005                         code.putln("#endif")
1006                     if scope.defines_any(["__setslice__", "__delslice__"]):
1007                         self.generate_ass_slice_function(scope, code)
1008                     if scope.defines_any(["__getattr__","__getattribute__"]):
1009                         self.generate_getattro_function(scope, code)
1010                     if scope.defines_any(["__setattr__", "__delattr__"]):
1011                         self.generate_setattro_function(scope, code)
1012                     if scope.defines_any(["__get__"]):
1013                         self.generate_descr_get_function(scope, code)
1014                     if scope.defines_any(["__set__", "__delete__"]):
1015                         self.generate_descr_set_function(scope, code)
1016                     self.generate_property_accessors(scope, code)
1017                     self.generate_method_table(scope, code)
1018                     self.generate_getset_table(scope, code)
1019                     self.generate_typeobj_definition(full_module_name, entry, code)
1020
1021     def generate_exttype_vtable(self, scope, code):
1022         # Generate the definition of an extension type's vtable.
1023         type = scope.parent_type
1024         if type.vtable_cname:
1025             code.putln("static struct %s %s;" % (
1026                 type.vtabstruct_cname,
1027                 type.vtable_cname))
1028
1029     def generate_self_cast(self, scope, code):
1030         type = scope.parent_type
1031         code.putln(
1032             "%s = (%s)o;" % (
1033                 type.declaration_code("p"),
1034                 type.declaration_code("")))
1035
1036     def generate_new_function(self, scope, code):
1037         tp_slot = TypeSlots.ConstructorSlot("tp_new", '__new__')
1038         slot_func = scope.mangle_internal("tp_new")
1039         type = scope.parent_type
1040         base_type = type.base_type
1041         py_attrs = []
1042         for entry in scope.var_entries:
1043             if entry.type.is_pyobject:
1044                 py_attrs.append(entry)
1045         need_self_cast = type.vtabslot_cname or py_attrs
1046         code.putln("")
1047         code.putln(
1048             "static PyObject *%s(PyTypeObject *t, PyObject *a, PyObject *k) {"
1049                 % scope.mangle_internal("tp_new"))
1050         if need_self_cast:
1051             code.putln(
1052                 "%s;"
1053                     % scope.parent_type.declaration_code("p"))
1054         if base_type:
1055             tp_new = TypeSlots.get_base_slot_function(scope, tp_slot)
1056             if tp_new is None:
1057                 tp_new = "%s->tp_new" % base_type.typeptr_cname
1058             code.putln(
1059                 "PyObject *o = %s(t, a, k);" % tp_new)
1060         else:
1061             code.putln(
1062                 "PyObject *o = (*t->tp_alloc)(t, 0);")
1063         code.putln(
1064                 "if (!o) return 0;")
1065         if need_self_cast:
1066             code.putln(
1067                 "p = %s;"
1068                     % type.cast_code("o"))
1069         #if need_self_cast:
1070         #    self.generate_self_cast(scope, code)
1071         if type.vtabslot_cname:
1072             vtab_base_type = type
1073             while vtab_base_type.base_type and vtab_base_type.base_type.vtabstruct_cname:
1074                 vtab_base_type = vtab_base_type.base_type
1075             if vtab_base_type is not type:
1076                 struct_type_cast = "(struct %s*)" % vtab_base_type.vtabstruct_cname
1077             else:
1078                 struct_type_cast = ""
1079             code.putln("p->%s = %s%s;" % (
1080                 type.vtabslot_cname,
1081                 struct_type_cast, type.vtabptr_cname))
1082         for entry in py_attrs:
1083             if scope.is_internal or entry.name == "__weakref__":
1084                 # internal classes do not need None inits
1085                 code.putln("p->%s = 0;" % entry.cname)
1086             else:
1087                 code.put_init_var_to_py_none(entry, "p->%s", nanny=False)
1088         entry = scope.lookup_here("__new__")
1089         if entry and entry.is_special:
1090             if entry.trivial_signature:
1091                 cinit_args = "o, %s, NULL" % Naming.empty_tuple
1092             else:
1093                 cinit_args = "o, a, k"
1094             code.putln(
1095                 "if (%s(%s) < 0) {" %
1096                     (entry.func_cname, cinit_args))
1097             code.put_decref_clear("o", py_object_type, nanny=False);
1098             code.putln(
1099                 "}")
1100         code.putln(
1101             "return o;")
1102         code.putln(
1103             "}")
1104
1105     def generate_dealloc_function(self, scope, code):
1106         tp_slot = TypeSlots.ConstructorSlot("tp_dealloc", '__dealloc__')
1107         slot_func = scope.mangle_internal("tp_dealloc")
1108         base_type = scope.parent_type.base_type
1109         if tp_slot.slot_code(scope) != slot_func:
1110             return # never used
1111         code.putln("")
1112         code.putln(
1113             "static void %s(PyObject *o) {"
1114                 % scope.mangle_internal("tp_dealloc"))
1115         py_attrs = []
1116         weakref_slot = scope.lookup_here("__weakref__")
1117         for entry in scope.var_entries:
1118             if entry.type.is_pyobject and entry is not weakref_slot:
1119                 py_attrs.append(entry)
1120         if py_attrs or weakref_slot in scope.var_entries:
1121             self.generate_self_cast(scope, code)
1122         self.generate_usr_dealloc_call(scope, code)
1123         if weakref_slot in scope.var_entries:
1124             code.putln("if (p->__weakref__) PyObject_ClearWeakRefs(o);")
1125         for entry in py_attrs:
1126             code.put_xdecref("p->%s" % entry.cname, entry.type, nanny=False)
1127         if base_type:
1128             tp_dealloc = TypeSlots.get_base_slot_function(scope, tp_slot)
1129             if tp_dealloc is None:
1130                 tp_dealloc = "%s->tp_dealloc" % base_type.typeptr_cname
1131             code.putln(
1132                     "%s(o);" % tp_dealloc)
1133         else:
1134             code.putln(
1135                     "(*Py_TYPE(o)->tp_free)(o);")
1136         code.putln(
1137             "}")
1138
1139     def generate_usr_dealloc_call(self, scope, code):
1140         entry = scope.lookup_here("__dealloc__")
1141         if entry:
1142             code.putln(
1143                 "{")
1144             code.putln(
1145                     "PyObject *etype, *eval, *etb;")
1146             code.putln(
1147                     "PyErr_Fetch(&etype, &eval, &etb);")
1148             code.putln(
1149                     "++Py_REFCNT(o);")
1150             code.putln(
1151                     "%s(o);" %
1152                         entry.func_cname)
1153             code.putln(
1154                     "if (PyErr_Occurred()) PyErr_WriteUnraisable(o);")
1155             code.putln(
1156                     "--Py_REFCNT(o);")
1157             code.putln(
1158                     "PyErr_Restore(etype, eval, etb);")
1159             code.putln(
1160                 "}")
1161
1162     def generate_traverse_function(self, scope, code):
1163         tp_slot = TypeSlots.GCDependentSlot("tp_traverse")
1164         slot_func = scope.mangle_internal("tp_traverse")
1165         base_type = scope.parent_type.base_type
1166         if tp_slot.slot_code(scope) != slot_func:
1167             return # never used
1168         code.putln("")
1169         code.putln(
1170             "static int %s(PyObject *o, visitproc v, void *a) {"
1171                 % slot_func)
1172         py_attrs = []
1173         for entry in scope.var_entries:
1174             if entry.type.is_pyobject and entry.name != "__weakref__":
1175                 py_attrs.append(entry)
1176         if base_type or py_attrs:
1177             code.putln("int e;")
1178         if py_attrs:
1179             self.generate_self_cast(scope, code)
1180         if base_type:
1181             # want to call it explicitly if possible so inlining can be performed
1182             static_call = TypeSlots.get_base_slot_function(scope, tp_slot)
1183             if static_call:
1184                 code.putln("e = %s(o, v, a); if (e) return e;" % static_call)
1185             else:
1186                 code.putln("if (%s->tp_traverse) {" % base_type.typeptr_cname)
1187                 code.putln(
1188                         "e = %s->tp_traverse(o, v, a); if (e) return e;" %
1189                             base_type.typeptr_cname)
1190                 code.putln("}")
1191         for entry in py_attrs:
1192             var_code = "p->%s" % entry.cname
1193             code.putln(
1194                     "if (%s) {"
1195                         % var_code)
1196             if entry.type.is_extension_type:
1197                 var_code = "((PyObject*)%s)" % var_code
1198             code.putln(
1199                         "e = (*v)(%s, a); if (e) return e;"
1200                             % var_code)
1201             code.putln(
1202                     "}")
1203         code.putln(
1204                 "return 0;")
1205         code.putln(
1206             "}")
1207
1208     def generate_clear_function(self, scope, code):
1209         tp_slot = TypeSlots.GCDependentSlot("tp_clear")
1210         slot_func = scope.mangle_internal("tp_clear")
1211         base_type = scope.parent_type.base_type
1212         if tp_slot.slot_code(scope) != slot_func:
1213             return # never used
1214         code.putln("")
1215         code.putln("static int %s(PyObject *o) {" % slot_func)
1216         py_attrs = []
1217         for entry in scope.var_entries:
1218             if entry.type.is_pyobject and entry.name != "__weakref__":
1219                 py_attrs.append(entry)
1220         if py_attrs:
1221             self.generate_self_cast(scope, code)
1222             code.putln("PyObject* tmp;")
1223         if base_type:
1224             # want to call it explicitly if possible so inlining can be performed
1225             static_call = TypeSlots.get_base_slot_function(scope, tp_slot)
1226             if static_call:
1227                 code.putln("%s(o);" % static_call)
1228             else:
1229                 code.putln("if (%s->tp_clear) {" % base_type.typeptr_cname)
1230                 code.putln("%s->tp_clear(o);" % base_type.typeptr_cname)
1231                 code.putln("}")
1232         for entry in py_attrs:
1233             name = "p->%s" % entry.cname
1234             code.putln("tmp = ((PyObject*)%s);" % name)
1235             code.put_init_to_py_none(name, entry.type, nanny=False)
1236             code.putln("Py_XDECREF(tmp);")
1237         code.putln(
1238             "return 0;")
1239         code.putln(
1240             "}")
1241
1242     def generate_getitem_int_function(self, scope, code):
1243         # This function is put into the sq_item slot when
1244         # a __getitem__ method is present. It converts its
1245         # argument to a Python integer and calls mp_subscript.
1246         code.putln(
1247             "static PyObject *%s(PyObject *o, Py_ssize_t i) {" %
1248                 scope.mangle_internal("sq_item"))
1249         code.putln(
1250                 "PyObject *r;")
1251         code.putln(
1252                 "PyObject *x = PyInt_FromSsize_t(i); if(!x) return 0;")
1253         code.putln(
1254                 "r = Py_TYPE(o)->tp_as_mapping->mp_subscript(o, x);")
1255         code.putln(
1256                 "Py_DECREF(x);")
1257         code.putln(
1258                 "return r;")
1259         code.putln(
1260             "}")
1261
1262     def generate_ass_subscript_function(self, scope, code):
1263         # Setting and deleting an item are both done through
1264         # the ass_subscript method, so we dispatch to user's __setitem__
1265         # or __delitem__, or raise an exception.
1266         base_type = scope.parent_type.base_type
1267         set_entry = scope.lookup_here("__setitem__")
1268         del_entry = scope.lookup_here("__delitem__")
1269         code.putln("")
1270         code.putln(
1271             "static int %s(PyObject *o, PyObject *i, PyObject *v) {" %
1272                 scope.mangle_internal("mp_ass_subscript"))
1273         code.putln(
1274                 "if (v) {")
1275         if set_entry:
1276             code.putln(
1277                     "return %s(o, i, v);" %
1278                         set_entry.func_cname)
1279         else:
1280             self.generate_guarded_basetype_call(
1281                 base_type, "tp_as_mapping", "mp_ass_subscript", "o, i, v", code)
1282             code.putln(
1283                     "PyErr_Format(PyExc_NotImplementedError,")
1284             code.putln(
1285                     '  "Subscript assignment not supported by %s", Py_TYPE(o)->tp_name);')
1286             code.putln(
1287                     "return -1;")
1288         code.putln(
1289                 "}")
1290         code.putln(
1291                 "else {")
1292         if del_entry:
1293             code.putln(
1294                     "return %s(o, i);" %
1295                         del_entry.func_cname)
1296         else:
1297             self.generate_guarded_basetype_call(
1298                 base_type, "tp_as_mapping", "mp_ass_subscript", "o, i, v", code)
1299             code.putln(
1300                     "PyErr_Format(PyExc_NotImplementedError,")
1301             code.putln(
1302                     '  "Subscript deletion not supported by %s", Py_TYPE(o)->tp_name);')
1303             code.putln(
1304                     "return -1;")
1305         code.putln(
1306                 "}")
1307         code.putln(
1308             "}")
1309
1310     def generate_guarded_basetype_call(
1311             self, base_type, substructure, slot, args, code):
1312         if base_type:
1313             base_tpname = base_type.typeptr_cname
1314             if substructure:
1315                 code.putln(
1316                     "if (%s->%s && %s->%s->%s)" % (
1317                         base_tpname, substructure, base_tpname, substructure, slot))
1318                 code.putln(
1319                     "  return %s->%s->%s(%s);" % (
1320                         base_tpname, substructure, slot, args))
1321             else:
1322                 code.putln(
1323                     "if (%s->%s)" % (
1324                         base_tpname, slot))
1325                 code.putln(
1326                     "  return %s->%s(%s);" % (
1327                         base_tpname, slot, args))
1328
1329     def generate_ass_slice_function(self, scope, code):
1330         # Setting and deleting a slice are both done through
1331         # the ass_slice method, so we dispatch to user's __setslice__
1332         # or __delslice__, or raise an exception.
1333         base_type = scope.parent_type.base_type
1334         set_entry = scope.lookup_here("__setslice__")
1335         del_entry = scope.lookup_here("__delslice__")
1336         code.putln("")
1337         code.putln(
1338             "static int %s(PyObject *o, Py_ssize_t i, Py_ssize_t j, PyObject *v) {" %
1339                 scope.mangle_internal("sq_ass_slice"))
1340         code.putln(
1341                 "if (v) {")
1342         if set_entry:
1343             code.putln(
1344                     "return %s(o, i, j, v);" %
1345                         set_entry.func_cname)
1346         else:
1347             self.generate_guarded_basetype_call(
1348                 base_type, "tp_as_sequence", "sq_ass_slice", "o, i, j, v", code)
1349             code.putln(
1350                     "PyErr_Format(PyExc_NotImplementedError,")
1351             code.putln(
1352                     '  "2-element slice assignment not supported by %s", Py_TYPE(o)->tp_name);')
1353             code.putln(
1354                     "return -1;")
1355         code.putln(
1356                 "}")
1357         code.putln(
1358                 "else {")
1359         if del_entry:
1360             code.putln(
1361                     "return %s(o, i, j);" %
1362                         del_entry.func_cname)
1363         else:
1364             self.generate_guarded_basetype_call(
1365                 base_type, "tp_as_sequence", "sq_ass_slice", "o, i, j, v", code)
1366             code.putln(
1367                     "PyErr_Format(PyExc_NotImplementedError,")
1368             code.putln(
1369                     '  "2-element slice deletion not supported by %s", Py_TYPE(o)->tp_name);')
1370             code.putln(
1371                     "return -1;")
1372         code.putln(
1373                 "}")
1374         code.putln(
1375             "}")
1376
1377     def generate_getattro_function(self, scope, code):
1378         # First try to get the attribute using __getattribute__, if defined, or
1379         # PyObject_GenericGetAttr.
1380         #
1381         # If that raises an AttributeError, call the __getattr__ if defined.
1382         #
1383         # In both cases, defined can be in this class, or any base class.
1384         def lookup_here_or_base(n,type=None):
1385             # Recursive lookup
1386             if type is None:
1387                 type = scope.parent_type
1388             r = type.scope.lookup_here(n)
1389             if r is None and \
1390                type.base_type is not None:
1391                 return lookup_here_or_base(n,type.base_type)
1392             else:
1393                 return r
1394         getattr_entry = lookup_here_or_base("__getattr__")
1395         getattribute_entry = lookup_here_or_base("__getattribute__")
1396         code.putln("")
1397         code.putln(
1398             "static PyObject *%s(PyObject *o, PyObject *n) {"
1399                 % scope.mangle_internal("tp_getattro"))
1400         if getattribute_entry is not None:
1401             code.putln(
1402                 "PyObject *v = %s(o, n);" %
1403                     getattribute_entry.func_cname)
1404         else:
1405             code.putln(
1406                 "PyObject *v = PyObject_GenericGetAttr(o, n);")
1407         if getattr_entry is not None:
1408             code.putln(
1409                 "if (!v && PyErr_ExceptionMatches(PyExc_AttributeError)) {")
1410             code.putln(
1411                 "PyErr_Clear();")
1412             code.putln(
1413                 "v = %s(o, n);" %
1414                     getattr_entry.func_cname)
1415             code.putln(
1416                 "}")
1417         code.putln(
1418             "return v;")
1419         code.putln(
1420             "}")
1421
1422     def generate_setattro_function(self, scope, code):
1423         # Setting and deleting an attribute are both done through
1424         # the setattro method, so we dispatch to user's __setattr__
1425         # or __delattr__ or fall back on PyObject_GenericSetAttr.
1426         base_type = scope.parent_type.base_type
1427         set_entry = scope.lookup_here("__setattr__")
1428         del_entry = scope.lookup_here("__delattr__")
1429         code.putln("")
1430         code.putln(
1431             "static int %s(PyObject *o, PyObject *n, PyObject *v) {" %
1432                 scope.mangle_internal("tp_setattro"))
1433         code.putln(
1434                 "if (v) {")
1435         if set_entry:
1436             code.putln(
1437                     "return %s(o, n, v);" %
1438                         set_entry.func_cname)
1439         else:
1440             self.generate_guarded_basetype_call(
1441                 base_type, None, "tp_setattro", "o, n, v", code)
1442             code.putln(
1443                     "return PyObject_GenericSetAttr(o, n, v);")
1444         code.putln(
1445                 "}")
1446         code.putln(
1447                 "else {")
1448         if del_entry:
1449             code.putln(
1450                     "return %s(o, n);" %
1451                         del_entry.func_cname)
1452         else:
1453             self.generate_guarded_basetype_call(
1454                 base_type, None, "tp_setattro", "o, n, v", code)
1455             code.putln(
1456                     "return PyObject_GenericSetAttr(o, n, 0);")
1457         code.putln(
1458                 "}")
1459         code.putln(
1460             "}")
1461
1462     def generate_descr_get_function(self, scope, code):
1463         # The __get__ function of a descriptor object can be
1464         # called with NULL for the second or third arguments
1465         # under some circumstances, so we replace them with
1466         # None in that case.
1467         user_get_entry = scope.lookup_here("__get__")
1468         code.putln("")
1469         code.putln(
1470             "static PyObject *%s(PyObject *o, PyObject *i, PyObject *c) {" %
1471                 scope.mangle_internal("tp_descr_get"))
1472         code.putln(
1473             "PyObject *r = 0;")
1474         code.putln(
1475             "if (!i) i = Py_None;")
1476         code.putln(
1477             "if (!c) c = Py_None;")
1478         #code.put_incref("i", py_object_type)
1479         #code.put_incref("c", py_object_type)
1480         code.putln(
1481             "r = %s(o, i, c);" %
1482                 user_get_entry.func_cname)
1483         #code.put_decref("i", py_object_type)
1484         #code.put_decref("c", py_object_type)
1485         code.putln(
1486             "return r;")
1487         code.putln(
1488             "}")
1489
1490     def generate_descr_set_function(self, scope, code):
1491         # Setting and deleting are both done through the __set__
1492         # method of a descriptor, so we dispatch to user's __set__
1493         # or __delete__ or raise an exception.
1494         base_type = scope.parent_type.base_type
1495         user_set_entry = scope.lookup_here("__set__")
1496         user_del_entry = scope.lookup_here("__delete__")
1497         code.putln("")
1498         code.putln(
1499             "static int %s(PyObject *o, PyObject *i, PyObject *v) {" %
1500                 scope.mangle_internal("tp_descr_set"))
1501         code.putln(
1502                 "if (v) {")
1503         if user_set_entry:
1504             code.putln(
1505                     "return %s(o, i, v);" %
1506                         user_set_entry.func_cname)
1507         else:
1508             self.generate_guarded_basetype_call(
1509                 base_type, None, "tp_descr_set", "o, i, v", code)
1510             code.putln(
1511                     'PyErr_SetString(PyExc_NotImplementedError, "__set__");')
1512             code.putln(
1513                     "return -1;")
1514         code.putln(
1515                 "}")
1516         code.putln(
1517                 "else {")
1518         if user_del_entry:
1519             code.putln(
1520                     "return %s(o, i);" %
1521                         user_del_entry.func_cname)
1522         else:
1523             self.generate_guarded_basetype_call(
1524                 base_type, None, "tp_descr_set", "o, i, v", code)
1525             code.putln(
1526                     'PyErr_SetString(PyExc_NotImplementedError, "__delete__");')
1527             code.putln(
1528                     "return -1;")
1529         code.putln(
1530                 "}")
1531         code.putln(
1532             "}")
1533
1534     def generate_property_accessors(self, cclass_scope, code):
1535         for entry in cclass_scope.property_entries:
1536             property_scope = entry.scope
1537             if property_scope.defines_any(["__get__"]):
1538                 self.generate_property_get_function(entry, code)
1539             if property_scope.defines_any(["__set__", "__del__"]):
1540                 self.generate_property_set_function(entry, code)
1541
1542     def generate_property_get_function(self, property_entry, code):
1543         property_scope = property_entry.scope
1544         property_entry.getter_cname = property_scope.parent_scope.mangle(
1545             Naming.prop_get_prefix, property_entry.name)
1546         get_entry = property_scope.lookup_here("__get__")
1547         code.putln("")
1548         code.putln(
1549             "static PyObject *%s(PyObject *o, void *x) {" %
1550                 property_entry.getter_cname)
1551         code.putln(
1552                 "return %s(o);" %
1553                     get_entry.func_cname)
1554         code.putln(
1555             "}")
1556
1557     def generate_property_set_function(self, property_entry, code):
1558         property_scope = property_entry.scope
1559         property_entry.setter_cname = property_scope.parent_scope.mangle(
1560             Naming.prop_set_prefix, property_entry.name)
1561         set_entry = property_scope.lookup_here("__set__")
1562         del_entry = property_scope.lookup_here("__del__")
1563         code.putln("")
1564         code.putln(
1565             "static int %s(PyObject *o, PyObject *v, void *x) {" %
1566                 property_entry.setter_cname)
1567         code.putln(
1568                 "if (v) {")
1569         if set_entry:
1570             code.putln(
1571                     "return %s(o, v);" %
1572                         set_entry.func_cname)
1573         else:
1574             code.putln(
1575                     'PyErr_SetString(PyExc_NotImplementedError, "__set__");')
1576             code.putln(
1577                     "return -1;")
1578         code.putln(
1579                 "}")
1580         code.putln(
1581                 "else {")
1582         if del_entry:
1583             code.putln(
1584                     "return %s(o);" %
1585                         del_entry.func_cname)
1586         else:
1587             code.putln(
1588                     'PyErr_SetString(PyExc_NotImplementedError, "__del__");')
1589             code.putln(
1590                     "return -1;")
1591         code.putln(
1592                 "}")
1593         code.putln(
1594             "}")
1595
1596     def generate_typeobj_definition(self, modname, entry, code):
1597         type = entry.type
1598         scope = type.scope
1599         for suite in TypeSlots.substructures:
1600             suite.generate_substructure(scope, code)
1601         code.putln("")
1602         if entry.visibility == 'public':
1603             header = "DL_EXPORT(PyTypeObject) %s = {"
1604         else:
1605             header = "static PyTypeObject %s = {"
1606         #code.putln(header % scope.parent_type.typeobj_cname)
1607         code.putln(header % type.typeobj_cname)
1608         code.putln(
1609             "PyVarObject_HEAD_INIT(0, 0)")
1610         code.putln(
1611             '__Pyx_NAMESTR("%s.%s"), /*tp_name*/' % (
1612                 self.full_module_name, scope.class_name))
1613         if type.typedef_flag:
1614             objstruct = type.objstruct_cname
1615         else:
1616             objstruct = "struct %s" % type.objstruct_cname
1617         code.putln(
1618             "sizeof(%s), /*tp_basicsize*/" %
1619                 objstruct)
1620         code.putln(
1621             "0, /*tp_itemsize*/")
1622         for slot in TypeSlots.slot_table:
1623             slot.generate(scope, code)
1624         code.putln(
1625             "};")
1626
1627     def generate_method_table(self, env, code):
1628         code.putln("")
1629         code.putln(
1630             "static PyMethodDef %s[] = {" %
1631                 env.method_table_cname)
1632         for entry in env.pyfunc_entries:
1633             code.put_pymethoddef(entry, ",")
1634         code.putln(
1635                 "{0, 0, 0, 0}")
1636         code.putln(
1637             "};")
1638
1639     def generate_getset_table(self, env, code):
1640         if env.property_entries:
1641             code.putln("")
1642             code.putln(
1643                 "static struct PyGetSetDef %s[] = {" %
1644                     env.getset_table_cname)
1645             for entry in env.property_entries:
1646                 if entry.doc:
1647                     doc_code = "__Pyx_DOCSTR(%s)" % code.get_string_const(entry.doc)
1648                 else:
1649                     doc_code = "0"
1650                 code.putln(
1651                     '{(char *)"%s", %s, %s, %s, 0},' % (
1652                         entry.name,
1653                         entry.getter_cname or "0",
1654                         entry.setter_cname or "0",
1655                         doc_code))
1656             code.putln(
1657                     "{0, 0, 0, 0, 0}")
1658             code.putln(
1659                 "};")
1660
1661     def generate_import_star(self, env, code):
1662         env.use_utility_code(streq_utility_code)
1663         code.putln()
1664         code.putln("char* %s_type_names[] = {" % Naming.import_star)
1665         for name, entry in env.entries.items():
1666             if entry.is_type:
1667                 code.putln('"%s",' % name)
1668         code.putln("0")
1669         code.putln("};")
1670         code.putln()
1671         code.enter_cfunc_scope() # as we need labels
1672         code.putln("static int %s(PyObject *o, PyObject* py_name, char *name) {" % Naming.import_star_set)
1673         code.putln("char** type_name = %s_type_names;" % Naming.import_star)
1674         code.putln("while (*type_name) {")
1675         code.putln("if (__Pyx_StrEq(name, *type_name)) {")
1676         code.putln('PyErr_Format(PyExc_TypeError, "Cannot overwrite C type %s", name);')
1677         code.putln('goto bad;')
1678         code.putln("}")
1679         code.putln("type_name++;")
1680         code.putln("}")
1681         old_error_label = code.new_error_label()
1682         code.putln("if (0);") # so the first one can be "else if"
1683         for name, entry in env.entries.items():
1684             if entry.is_cglobal and entry.used:
1685                 code.putln('else if (__Pyx_StrEq(name, "%s")) {' % name)
1686                 if entry.type.is_pyobject:
1687                     if entry.type.is_extension_type or entry.type.is_builtin_type:
1688                         code.putln("if (!(%s)) %s;" % (
1689                             entry.type.type_test_code("o"),
1690                             code.error_goto(entry.pos)))
1691                     code.putln("Py_INCREF(o);")
1692                     code.put_decref(entry.cname, entry.type, nanny=False)
1693                     code.putln("%s = %s;" % (
1694                         entry.cname,
1695                         PyrexTypes.typecast(entry.type, py_object_type, "o")))
1696                 elif entry.type.from_py_function:
1697                     rhs = "%s(o)" % entry.type.from_py_function
1698                     if entry.type.is_enum:
1699                         rhs = PyrexTypes.typecast(entry.type, PyrexTypes.c_long_type, rhs)
1700                     code.putln("%s = %s; if (%s) %s;" % (
1701                         entry.cname,
1702                         rhs,
1703                         entry.type.error_condition(entry.cname),
1704                         code.error_goto(entry.pos)))
1705                 else:
1706                     code.putln('PyErr_Format(PyExc_TypeError, "Cannot convert Python object %s to %s");' % (name, entry.type))
1707                     code.putln(code.error_goto(entry.pos))
1708                 code.putln("}")
1709         code.putln("else {")
1710         code.putln("if (PyObject_SetAttr(%s, py_name, o) < 0) goto bad;" % Naming.module_cname)
1711         code.putln("}")
1712         code.putln("return 0;")
1713         if code.label_used(code.error_label):
1714             code.put_label(code.error_label)
1715             # This helps locate the offending name.
1716             code.putln('__Pyx_AddTraceback("%s");' % self.full_module_name);
1717         code.error_label = old_error_label
1718         code.putln("bad:")
1719         code.putln("return -1;")
1720         code.putln("}")
1721         code.putln(import_star_utility_code)
1722         code.exit_cfunc_scope() # done with labels
1723
1724     def generate_module_init_func(self, imported_modules, env, code):
1725         code.enter_cfunc_scope()
1726         code.putln("")
1727         header2 = "PyMODINIT_FUNC init%s(void)" % env.module_name
1728         header3 = "PyMODINIT_FUNC PyInit_%s(void)" % env.module_name
1729         code.putln("#if PY_MAJOR_VERSION < 3")
1730         code.putln("%s; /*proto*/" % header2)
1731         code.putln(header2)
1732         code.putln("#else")
1733         code.putln("%s; /*proto*/" % header3)
1734         code.putln(header3)
1735         code.putln("#endif")
1736         code.putln("{")
1737         tempdecl_code = code.insertion_point()
1738
1739         code.putln("#if CYTHON_REFNANNY")
1740         code.putln("void* __pyx_refnanny = NULL;")
1741         code.putln("__Pyx_RefNanny = __Pyx_RefNannyImportAPI(\"refnanny\");")
1742         code.putln("if (!__Pyx_RefNanny) {")
1743         code.putln("  PyErr_Clear();")
1744         code.putln("  __Pyx_RefNanny = __Pyx_RefNannyImportAPI(\"Cython.Runtime.refnanny\");")
1745         code.putln("  if (!__Pyx_RefNanny)")
1746         code.putln("      Py_FatalError(\"failed to import 'refnanny' module\");")
1747         code.putln("}")
1748         code.putln("__pyx_refnanny = __Pyx_RefNanny->SetupContext(\"%s\", __LINE__, __FILE__);"% header3)
1749         code.putln("#endif")
1750
1751         code.putln("%s = PyTuple_New(0); %s" % (Naming.empty_tuple, code.error_goto_if_null(Naming.empty_tuple, self.pos)));
1752         code.putln("%s = PyBytes_FromStringAndSize(\"\", 0); %s" % (Naming.empty_bytes, code.error_goto_if_null(Naming.empty_bytes, self.pos)));
1753
1754         code.putln("#ifdef %s_USED" % Naming.binding_cfunc)
1755         code.putln("if (%s_init() < 0) %s" % (Naming.binding_cfunc, code.error_goto(self.pos)))
1756         code.putln("#endif")
1757
1758         code.putln("/*--- Library function declarations ---*/")
1759         env.generate_library_function_declarations(code)
1760
1761         code.putln("/*--- Threads initialization code ---*/")
1762         code.putln("#if defined(__PYX_FORCE_INIT_THREADS) && __PYX_FORCE_INIT_THREADS")
1763         code.putln("#ifdef WITH_THREAD /* Python build with threading support? */")
1764         code.putln("PyEval_InitThreads();")
1765         code.putln("#endif")
1766         code.putln("#endif")
1767
1768         code.putln("/*--- Module creation code ---*/")
1769         self.generate_module_creation_code(env, code)
1770
1771         code.putln("/*--- Initialize various global constants etc. ---*/")
1772         code.putln(code.error_goto_if_neg("__Pyx_InitGlobals()", self.pos))
1773
1774         __main__name = code.globalstate.get_py_string_const(
1775             EncodedString("__main__"), identifier=True)
1776         code.putln("if (%s%s) {" % (Naming.module_is_main, self.full_module_name.replace('.', '__')))
1777         code.putln(
1778             'if (__Pyx_SetAttrString(%s, "__name__", %s) < 0) %s;' % (
1779                 env.module_cname,
1780                 __main__name.cname,
1781                 code.error_goto(self.pos)))
1782         code.putln("}")
1783
1784         if Options.cache_builtins:
1785             code.putln("/*--- Builtin init code ---*/")
1786             code.putln(code.error_goto_if_neg("__Pyx_InitCachedBuiltins()", self.pos))
1787
1788         code.putln("/*--- Constants init code ---*/")
1789         code.putln(code.error_goto_if_neg("__Pyx_InitCachedConstants()", self.pos))
1790
1791         code.putln("/*--- Global init code ---*/")
1792         self.generate_global_init_code(env, code)
1793
1794         code.putln("/*--- Function export code ---*/")
1795         self.generate_c_function_export_code(env, code)
1796
1797         code.putln("/*--- Type init code ---*/")
1798         self.generate_type_init_code(env, code)
1799
1800         code.putln("/*--- Type import code ---*/")
1801         for module in imported_modules:
1802             self.generate_type_import_code_for_module(module, env, code)
1803
1804         code.putln("/*--- Function import code ---*/")
1805         for module in imported_modules:
1806             self.generate_c_function_import_code_for_module(module, env, code)
1807
1808         code.putln("/*--- Execution code ---*/")
1809         code.mark_pos(None)
1810
1811         self.body.generate_execution_code(code)
1812
1813         if Options.generate_cleanup_code:
1814             # this should be replaced by the module's tp_clear in Py3
1815             env.use_utility_code(import_module_utility_code)
1816             code.putln("if (__Pyx_RegisterCleanup()) %s;" % code.error_goto(self.pos))
1817
1818         code.put_goto(code.return_label)
1819         code.put_label(code.error_label)
1820         for cname, type in code.funcstate.all_managed_temps():
1821             code.put_xdecref(cname, type)
1822         code.putln('if (%s) {' % env.module_cname)
1823         code.putln('__Pyx_AddTraceback("init %s");' % env.qualified_name)
1824         env.use_utility_code(Nodes.traceback_utility_code)
1825         code.put_decref_clear(env.module_cname, py_object_type, nanny=False)
1826         code.putln('} else if (!PyErr_Occurred()) {')
1827         code.putln('PyErr_SetString(PyExc_ImportError, "init %s");' % env.qualified_name)
1828         code.putln('}')
1829         code.put_label(code.return_label)
1830
1831         code.put_finish_refcount_context()
1832
1833         code.putln("#if PY_MAJOR_VERSION < 3")
1834         code.putln("return;")
1835         code.putln("#else")
1836         code.putln("return %s;" % env.module_cname)
1837         code.putln("#endif")
1838         code.putln('}')
1839
1840         tempdecl_code.put_temp_declarations(code.funcstate)
1841
1842         code.exit_cfunc_scope()
1843
1844     def generate_module_cleanup_func(self, env, code):
1845         if not Options.generate_cleanup_code:
1846             return
1847         code.globalstate.use_utility_code(register_cleanup_utility_code)
1848         code.putln('static PyObject *%s(CYTHON_UNUSED PyObject *self, CYTHON_UNUSED PyObject *unused) {' %
1849                    Naming.cleanup_cname)
1850         if Options.generate_cleanup_code >= 2:
1851             code.putln("/*--- Global cleanup code ---*/")
1852             rev_entries = list(env.var_entries)
1853             rev_entries.reverse()
1854             for entry in rev_entries:
1855                 if entry.visibility != 'extern':
1856                     if entry.type.is_pyobject and entry.used:
1857                         code.putln("Py_DECREF(%s); %s = 0;" % (
1858                             code.entry_as_pyobject(entry), entry.cname))
1859         code.putln("__Pyx_CleanupGlobals();")
1860         if Options.generate_cleanup_code >= 3:
1861             code.putln("/*--- Type import cleanup code ---*/")
1862             for type, _ in env.types_imported.items():
1863                 code.putln("Py_DECREF((PyObject *)%s);" % type.typeptr_cname)
1864         if Options.cache_builtins:
1865             code.putln("/*--- Builtin cleanup code ---*/")
1866             for entry in env.cached_builtins:
1867                 code.put_decref_clear(entry.cname,
1868                                       PyrexTypes.py_object_type,
1869                                       nanny=False)
1870         code.putln("/*--- Intern cleanup code ---*/")
1871         code.put_decref_clear(Naming.empty_tuple,
1872                               PyrexTypes.py_object_type,
1873                               nanny=False)
1874 #        for entry in env.pynum_entries:
1875 #            code.put_decref_clear(entry.cname,
1876 #                                  PyrexTypes.py_object_type,
1877 #                                  nanny=False)
1878 #        for entry in env.all_pystring_entries:
1879 #            if entry.is_interned:
1880 #                code.put_decref_clear(entry.pystring_cname,
1881 #                                      PyrexTypes.py_object_type,
1882 #                                      nanny=False)
1883 #        for entry in env.default_entries:
1884 #            if entry.type.is_pyobject and entry.used:
1885 #                code.putln("Py_DECREF(%s); %s = 0;" % (
1886 #                    code.entry_as_pyobject(entry), entry.cname))
1887         code.putln("Py_INCREF(Py_None); return Py_None;")
1888
1889     def generate_main_method(self, env, code):
1890         module_is_main = "%s%s" % (Naming.module_is_main, self.full_module_name.replace('.', '__'))
1891         code.globalstate.use_utility_code(main_method.specialize(module_name=env.module_name, module_is_main=module_is_main))
1892
1893     def generate_pymoduledef_struct(self, env, code):
1894         if env.doc:
1895             doc = "__Pyx_DOCSTR(%s)" % code.get_string_const(env.doc)
1896         else:
1897             doc = "0"
1898         code.putln("")
1899         code.putln("#if PY_MAJOR_VERSION >= 3")
1900         code.putln("static struct PyModuleDef %s = {" % Naming.pymoduledef_cname)
1901         code.putln("  PyModuleDef_HEAD_INIT,")
1902         code.putln('  __Pyx_NAMESTR("%s"),' % env.module_name)
1903         code.putln("  %s, /* m_doc */" % doc)
1904         code.putln("  -1, /* m_size */")
1905         code.putln("  %s /* m_methods */," % env.method_table_cname)
1906         code.putln("  NULL, /* m_reload */")
1907         code.putln("  NULL, /* m_traverse */")
1908         code.putln("  NULL, /* m_clear */")
1909         code.putln("  NULL /* m_free */")
1910         code.putln("};")
1911         code.putln("#endif")
1912
1913     def generate_module_creation_code(self, env, code):
1914         # Generate code to create the module object and
1915         # install the builtins.
1916         if env.doc:
1917             doc = "__Pyx_DOCSTR(%s)" % code.get_string_const(env.doc)
1918         else:
1919             doc = "0"
1920         code.putln("#if PY_MAJOR_VERSION < 3")
1921         code.putln(
1922             '%s = Py_InitModule4(__Pyx_NAMESTR("%s"), %s, %s, 0, PYTHON_API_VERSION);' % (
1923                 env.module_cname,
1924                 env.module_name,
1925                 env.method_table_cname,
1926                 doc))
1927         code.putln("#else")
1928         code.putln(
1929             "%s = PyModule_Create(&%s);" % (
1930                 env.module_cname,
1931                 Naming.pymoduledef_cname))
1932         code.putln("#endif")
1933         code.putln(
1934             "if (!%s) %s;" % (
1935                 env.module_cname,
1936                 code.error_goto(self.pos)));
1937         code.putln("#if PY_MAJOR_VERSION < 3")
1938         code.putln(
1939             "Py_INCREF(%s);" %
1940                 env.module_cname)
1941         code.putln("#endif")
1942         code.putln(
1943             '%s = PyImport_AddModule(__Pyx_NAMESTR(__Pyx_BUILTIN_MODULE_NAME));' %
1944                 Naming.builtins_cname)
1945         code.putln(
1946             "if (!%s) %s;" % (
1947                 Naming.builtins_cname,
1948                 code.error_goto(self.pos)));
1949         code.putln(
1950             'if (__Pyx_SetAttrString(%s, "__builtins__", %s) < 0) %s;' % (
1951                 env.module_cname,
1952                 Naming.builtins_cname,
1953                 code.error_goto(self.pos)))
1954         if Options.pre_import is not None:
1955             code.putln(
1956                 '%s = PyImport_AddModule(__Pyx_NAMESTR("%s"));' % (
1957                     Naming.preimport_cname,
1958                     Options.pre_import))
1959             code.putln(
1960                 "if (!%s) %s;" % (
1961                     Naming.preimport_cname,
1962                     code.error_goto(self.pos)));
1963
1964     def generate_global_init_code(self, env, code):
1965         # Generate code to initialise global PyObject *
1966         # variables to None.
1967         for entry in env.var_entries:
1968             if entry.visibility != 'extern':
1969                 if entry.type.is_pyobject and entry.used:
1970                     code.put_init_var_to_py_none(entry, nanny=False)
1971
1972     def generate_c_function_export_code(self, env, code):
1973         # Generate code to create PyCFunction wrappers for exported C functions.
1974         for entry in env.cfunc_entries:
1975             if entry.api or entry.defined_in_pxd:
1976                 env.use_utility_code(function_export_utility_code)
1977                 signature = entry.type.signature_string()
1978                 code.putln('if (__Pyx_ExportFunction("%s", (void (*)(void))%s, "%s") < 0) %s' % (
1979                     entry.name,
1980                     entry.cname,
1981                     signature,
1982                     code.error_goto(self.pos)))
1983
1984     def generate_type_import_code_for_module(self, module, env, code):
1985         # Generate type import code for all exported extension types in
1986         # an imported module.
1987         #if module.c_class_entries:
1988         for entry in module.c_class_entries:
1989             if entry.defined_in_pxd:
1990                 self.generate_type_import_code(env, entry.type, entry.pos, code)
1991
1992     def generate_c_function_import_code_for_module(self, module, env, code):
1993         # Generate import code for all exported C functions in a cimported module.
1994         entries = []
1995         for entry in module.cfunc_entries:
1996             if entry.defined_in_pxd:
1997                 entries.append(entry)
1998         if entries:
1999             env.use_utility_code(import_module_utility_code)
2000             env.use_utility_code(function_import_utility_code)
2001             temp = code.funcstate.allocate_temp(py_object_type, manage_ref=True)
2002             code.putln(
2003                 '%s = __Pyx_ImportModule("%s"); if (!%s) %s' % (
2004                     temp,
2005                     module.qualified_name,
2006                     temp,
2007                     code.error_goto(self.pos)))
2008             for entry in entries:
2009                 code.putln(
2010                     'if (__Pyx_ImportFunction(%s, "%s", (void (**)(void))&%s, "%s") < 0) %s' % (
2011                         temp,
2012                         entry.name,
2013                         entry.cname,
2014                         entry.type.signature_string(),
2015                         code.error_goto(self.pos)))
2016             code.putln("Py_DECREF(%s); %s = 0;" % (temp, temp))
2017
2018     def generate_type_init_code(self, env, code):
2019         # Generate type import code for extern extension types
2020         # and type ready code for non-extern ones.
2021         for entry in env.c_class_entries:
2022             if entry.visibility == 'extern':
2023                 self.generate_type_import_code(env, entry.type, entry.pos, code)
2024             else:
2025                 self.generate_base_type_import_code(env, entry, code)
2026                 self.generate_exttype_vtable_init_code(entry, code)
2027                 self.generate_type_ready_code(env, entry, code)
2028                 self.generate_typeptr_assignment_code(entry, code)
2029
2030     def generate_base_type_import_code(self, env, entry, code):
2031         base_type = entry.type.base_type
2032         if base_type and base_type.module_name != env.qualified_name \
2033                and not base_type.is_builtin_type:
2034             self.generate_type_import_code(env, base_type, self.pos, code)
2035
2036     def use_type_import_utility_code(self, env):
2037         env.use_utility_code(type_import_utility_code)
2038         env.use_utility_code(import_module_utility_code)
2039
2040     def generate_type_import_code(self, env, type, pos, code):
2041         # If not already done, generate code to import the typeobject of an
2042         # extension type defined in another module, and extract its C method
2043         # table pointer if any.
2044         if type in env.types_imported:
2045             return
2046         if type.typedef_flag:
2047             objstruct = type.objstruct_cname
2048         else:
2049             objstruct = "struct %s" % type.objstruct_cname
2050         self.generate_type_import_call(type, code,
2051                                        code.error_goto_if_null(type.typeptr_cname, pos))
2052         self.use_type_import_utility_code(env)
2053         if type.vtabptr_cname:
2054             env.use_utility_code(Nodes.get_vtable_utility_code)
2055             code.putln("%s = (struct %s*)__Pyx_GetVtable(%s->tp_dict); %s" % (
2056                 type.vtabptr_cname,
2057                 type.vtabstruct_cname,
2058                 type.typeptr_cname,
2059                 code.error_goto_if_null(type.vtabptr_cname, pos)))
2060         env.types_imported[type] = 1
2061
2062     py3_type_name_map = {'str' : 'bytes', 'unicode' : 'str'}
2063
2064     def generate_type_import_call(self, type, code, error_code):
2065         if type.typedef_flag:
2066             objstruct = type.objstruct_cname
2067         else:
2068             objstruct = "struct %s" % type.objstruct_cname
2069         module_name = type.module_name
2070         if module_name not in ('__builtin__', 'builtins'):
2071             module_name = '"%s"' % module_name
2072         else:
2073             module_name = '__Pyx_BUILTIN_MODULE_NAME'
2074         if type.name in self.py3_type_name_map:
2075             code.putln("#if PY_MAJOR_VERSION >= 3")
2076             code.putln('%s = __Pyx_ImportType(%s, "%s", sizeof(%s), 1); %s' % (
2077                     type.typeptr_cname,
2078                     module_name,
2079                     self.py3_type_name_map[type.name],
2080                     objstruct,
2081                     error_code))
2082             code.putln("#else")
2083         code.putln('%s = __Pyx_ImportType(%s, "%s", sizeof(%s), %i); %s' % (
2084                 type.typeptr_cname,
2085                 module_name,
2086                 type.name,
2087                 objstruct,
2088                 not type.is_external or type.is_subclassed,
2089                 error_code))
2090         if type.name in self.py3_type_name_map:
2091             code.putln("#endif")
2092
2093     def generate_type_ready_code(self, env, entry, code):
2094         # Generate a call to PyType_Ready for an extension
2095         # type defined in this module.
2096         type = entry.type
2097         typeobj_cname = type.typeobj_cname
2098         scope = type.scope
2099         if scope: # could be None if there was an error
2100             if entry.visibility != 'extern':
2101                 for slot in TypeSlots.slot_table:
2102                     slot.generate_dynamic_init_code(scope, code)
2103                 code.putln(
2104                     "if (PyType_Ready(&%s) < 0) %s" % (
2105                         typeobj_cname,
2106                         code.error_goto(entry.pos)))
2107                 # Fix special method docstrings. This is a bit of a hack, but
2108                 # unless we let PyType_Ready create the slot wrappers we have
2109                 # a significant performance hit. (See trac #561.)
2110                 for func in entry.type.scope.pyfunc_entries:
2111                     if func.is_special and Options.docstrings and func.wrapperbase_cname:
2112                         code.putln("{")
2113                         code.putln(
2114                             'PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&%s, "%s"); %s' % (
2115                                 typeobj_cname,
2116                                 func.name,
2117                                 code.error_goto_if_null('wrapper', entry.pos)))
2118                         code.putln(
2119                             "if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) {")
2120                         code.putln(
2121                             "%s = *((PyWrapperDescrObject *)wrapper)->d_base;" % (
2122                                 func.wrapperbase_cname))
2123                         code.putln(
2124                             "%s.doc = %s;" % (func.wrapperbase_cname, func.doc_cname))
2125                         code.putln(
2126                             "((PyWrapperDescrObject *)wrapper)->d_base = &%s;" % (
2127                                 func.wrapperbase_cname))
2128                         code.putln("}")
2129                         code.putln("}")
2130                 if type.vtable_cname:
2131                     code.putln(
2132                         "if (__Pyx_SetVtable(%s.tp_dict, %s) < 0) %s" % (
2133                             typeobj_cname,
2134                             type.vtabptr_cname,
2135                             code.error_goto(entry.pos)))
2136                     env.use_utility_code(Nodes.set_vtable_utility_code)
2137                 if not type.scope.is_internal and not type.scope.directives['internal']:
2138                     # scope.is_internal is set for types defined by
2139                     # Cython (such as closures), the 'internal'
2140                     # directive is set by users
2141                     code.putln(
2142                         'if (__Pyx_SetAttrString(%s, "%s", (PyObject *)&%s) < 0) %s' % (
2143                             Naming.module_cname,
2144                             scope.class_name,
2145                             typeobj_cname,
2146                             code.error_goto(entry.pos)))
2147                 weakref_entry = scope.lookup_here("__weakref__")
2148                 if weakref_entry:
2149                     if weakref_entry.type is py_object_type:
2150                         tp_weaklistoffset = "%s.tp_weaklistoffset" % typeobj_cname
2151                         if type.typedef_flag:
2152                             objstruct = type.objstruct_cname
2153                         else:
2154                             objstruct = "struct %s" % type.objstruct_cname
2155                         code.putln("if (%s == 0) %s = offsetof(%s, %s);" % (
2156                             tp_weaklistoffset,
2157                             tp_weaklistoffset,
2158                             objstruct,
2159                             weakref_entry.cname))
2160                     else:
2161                         error(weakref_entry.pos, "__weakref__ slot must be of type 'object'")
2162
2163     def generate_exttype_vtable_init_code(self, entry, code):
2164         # Generate code to initialise the C method table of an
2165         # extension type.
2166         type = entry.type
2167         if type.vtable_cname:
2168             code.putln(
2169                 "%s = &%s;" % (
2170                     type.vtabptr_cname,
2171                     type.vtable_cname))
2172             if type.base_type and type.base_type.vtabptr_cname:
2173                 code.putln(
2174                     "%s.%s = *%s;" % (
2175                         type.vtable_cname,
2176                         Naming.obj_base_cname,
2177                         type.base_type.vtabptr_cname))
2178
2179             c_method_entries = [
2180                 entry for entry in type.scope.cfunc_entries
2181                 if entry.func_cname ]
2182             if c_method_entries:
2183                 for meth_entry in c_method_entries:
2184                     cast = meth_entry.type.signature_cast_string()
2185                     code.putln(
2186                         "%s.%s = %s%s;" % (
2187                             type.vtable_cname,
2188                             meth_entry.cname,
2189                             cast,
2190                             meth_entry.func_cname))
2191
2192     def generate_typeptr_assignment_code(self, entry, code):
2193         # Generate code to initialise the typeptr of an extension
2194         # type defined in this module to point to its type object.
2195         type = entry.type
2196         if type.typeobj_cname:
2197             code.putln(
2198                 "%s = &%s;" % (
2199                     type.typeptr_cname, type.typeobj_cname))
2200
2201 #------------------------------------------------------------------------------------
2202 #
2203 #  Runtime support code
2204 #
2205 #------------------------------------------------------------------------------------
2206
2207 streq_utility_code = UtilityCode(
2208 proto = """
2209 static CYTHON_INLINE int __Pyx_StrEq(const char *, const char *); /*proto*/
2210 """,
2211 impl = """
2212 static CYTHON_INLINE int __Pyx_StrEq(const char *s1, const char *s2) {
2213      while (*s1 != '\\0' && *s1 == *s2) { s1++; s2++; }
2214      return *s1 == *s2;
2215 }
2216 """)
2217
2218 #------------------------------------------------------------------------------------
2219
2220 import_module_utility_code = UtilityCode(
2221 proto = """
2222 static PyObject *__Pyx_ImportModule(const char *name); /*proto*/
2223 """,
2224 impl = """
2225 #ifndef __PYX_HAVE_RT_ImportModule
2226 #define __PYX_HAVE_RT_ImportModule
2227 static PyObject *__Pyx_ImportModule(const char *name) {
2228     PyObject *py_name = 0;
2229     PyObject *py_module = 0;
2230
2231     #if PY_MAJOR_VERSION < 3
2232     py_name = PyString_FromString(name);
2233     #else
2234     py_name = PyUnicode_FromString(name);
2235     #endif
2236     if (!py_name)
2237         goto bad;
2238     py_module = PyImport_Import(py_name);
2239     Py_DECREF(py_name);
2240     return py_module;
2241 bad:
2242     Py_XDECREF(py_name);
2243     return 0;
2244 }
2245 #endif
2246 """)
2247
2248 #------------------------------------------------------------------------------------
2249
2250 type_import_utility_code = UtilityCode(
2251 proto = """
2252 static PyTypeObject *__Pyx_ImportType(const char *module_name, const char *class_name, long size, int strict);  /*proto*/
2253 """,
2254 impl = """
2255 #ifndef __PYX_HAVE_RT_ImportType
2256 #define __PYX_HAVE_RT_ImportType
2257 static PyTypeObject *__Pyx_ImportType(const char *module_name, const char *class_name,
2258     long size, int strict)
2259 {
2260     PyObject *py_module = 0;
2261     PyObject *result = 0;
2262     PyObject *py_name = 0;
2263     char warning[200];
2264
2265     py_module = __Pyx_ImportModule(module_name);
2266     if (!py_module)
2267         goto bad;
2268     #if PY_MAJOR_VERSION < 3
2269     py_name = PyString_FromString(class_name);
2270     #else
2271     py_name = PyUnicode_FromString(class_name);
2272     #endif
2273     if (!py_name)
2274         goto bad;
2275     result = PyObject_GetAttr(py_module, py_name);
2276     Py_DECREF(py_name);
2277     py_name = 0;
2278     Py_DECREF(py_module);
2279     py_module = 0;
2280     if (!result)
2281         goto bad;
2282     if (!PyType_Check(result)) {
2283         PyErr_Format(PyExc_TypeError,
2284             "%s.%s is not a type object",
2285             module_name, class_name);
2286         goto bad;
2287     }
2288     if (!strict && ((PyTypeObject *)result)->tp_basicsize > size) {
2289         PyOS_snprintf(warning, sizeof(warning),
2290             "%s.%s size changed, may indicate binary incompatibility",
2291             module_name, class_name);
2292         #if PY_VERSION_HEX < 0x02050000
2293         PyErr_Warn(NULL, warning);
2294         #else
2295         PyErr_WarnEx(NULL, warning, 0);
2296         #endif
2297     }
2298     else if (((PyTypeObject *)result)->tp_basicsize != size) {
2299         PyErr_Format(PyExc_ValueError,
2300             "%s.%s has the wrong size, try recompiling",
2301             module_name, class_name);
2302         goto bad;
2303     }
2304     return (PyTypeObject *)result;
2305 bad:
2306     Py_XDECREF(py_module);
2307     Py_XDECREF(result);
2308     return 0;
2309 }
2310 #endif
2311 """)
2312
2313 #------------------------------------------------------------------------------------
2314
2315 function_export_utility_code = UtilityCode(
2316 proto = """
2317 static int __Pyx_ExportFunction(const char *name, void (*f)(void), const char *sig); /*proto*/
2318 """,
2319 impl = r"""
2320 static int __Pyx_ExportFunction(const char *name, void (*f)(void), const char *sig) {
2321     PyObject *d = 0;
2322     PyObject *cobj = 0;
2323     union {
2324         void (*fp)(void);
2325         void *p;
2326     } tmp;
2327
2328     d = PyObject_GetAttrString(%(MODULE)s, (char *)"%(API)s");
2329     if (!d) {
2330         PyErr_Clear();
2331         d = PyDict_New();
2332         if (!d)
2333             goto bad;
2334         Py_INCREF(d);
2335         if (PyModule_AddObject(%(MODULE)s, (char *)"%(API)s", d) < 0)
2336             goto bad;
2337     }
2338     tmp.fp = f;
2339 #if PY_VERSION_HEX >= 0x02070000 && !(PY_MAJOR_VERSION==3&&PY_MINOR_VERSION==0)
2340     cobj = PyCapsule_New(tmp.p, sig, 0);
2341 #else
2342     cobj = PyCObject_FromVoidPtrAndDesc(tmp.p, (void *)sig, 0);
2343 #endif
2344     if (!cobj)
2345         goto bad;
2346     if (PyDict_SetItemString(d, name, cobj) < 0)
2347         goto bad;
2348     Py_DECREF(cobj);
2349     Py_DECREF(d);
2350     return 0;
2351 bad:
2352     Py_XDECREF(cobj);
2353     Py_XDECREF(d);
2354     return -1;
2355 }
2356 """ % {'MODULE': Naming.module_cname, 'API': Naming.api_name}
2357 )
2358
2359 function_import_utility_code = UtilityCode(
2360 proto = """
2361 static int __Pyx_ImportFunction(PyObject *module, const char *funcname, void (**f)(void), const char *sig); /*proto*/
2362 """,
2363 impl = """
2364 #ifndef __PYX_HAVE_RT_ImportFunction
2365 #define __PYX_HAVE_RT_ImportFunction
2366 static int __Pyx_ImportFunction(PyObject *module, const char *funcname, void (**f)(void), const char *sig) {
2367     PyObject *d = 0;
2368     PyObject *cobj = 0;
2369     union {
2370         void (*fp)(void);
2371         void *p;
2372     } tmp;
2373
2374     d = PyObject_GetAttrString(module, (char *)"%(API)s");
2375     if (!d)
2376         goto bad;
2377     cobj = PyDict_GetItemString(d, funcname);
2378     if (!cobj) {
2379         PyErr_Format(PyExc_ImportError,
2380             "%%s does not export expected C function %%s",
2381                 PyModule_GetName(module), funcname);
2382         goto bad;
2383     }
2384 #if PY_VERSION_HEX >= 0x02070000 && !(PY_MAJOR_VERSION==3&&PY_MINOR_VERSION==0)
2385     if (!PyCapsule_IsValid(cobj, sig)) {
2386         PyErr_Format(PyExc_TypeError,
2387             "C function %%s.%%s has wrong signature (expected %%s, got %%s)",
2388              PyModule_GetName(module), funcname, sig, PyCapsule_GetName(cobj));
2389         goto bad;
2390     }
2391     tmp.p = PyCapsule_GetPointer(cobj, sig);
2392 #else
2393     {const char *desc, *s1, *s2;
2394     desc = (const char *)PyCObject_GetDesc(cobj);
2395     if (!desc)
2396         goto bad;
2397     s1 = desc; s2 = sig;
2398     while (*s1 != '\\0' && *s1 == *s2) { s1++; s2++; }
2399     if (*s1 != *s2) {
2400         PyErr_Format(PyExc_TypeError,
2401             "C function %%s.%%s has wrong signature (expected %%s, got %%s)",
2402              PyModule_GetName(module), funcname, sig, desc);
2403         goto bad;
2404     }
2405     tmp.p = PyCObject_AsVoidPtr(cobj);}
2406 #endif
2407     *f = tmp.fp;
2408     if (!(*f))
2409         goto bad;
2410     Py_DECREF(d);
2411     return 0;
2412 bad:
2413     Py_XDECREF(d);
2414     return -1;
2415 }
2416 #endif
2417 """ % dict(API = Naming.api_name)
2418 )
2419
2420 #------------------------------------------------------------------------------------
2421
2422 register_cleanup_utility_code = UtilityCode(
2423 proto = """
2424 static int __Pyx_RegisterCleanup(void); /*proto*/
2425 static PyObject* %(module_cleanup)s(CYTHON_UNUSED PyObject *self, CYTHON_UNUSED PyObject *unused); /*proto*/
2426 static PyMethodDef cleanup_def = {__Pyx_NAMESTR("__cleanup"), (PyCFunction)&%(module_cleanup)s, METH_NOARGS, 0};
2427 """ % {'module_cleanup': Naming.cleanup_cname},
2428 impl = """
2429 static int __Pyx_RegisterCleanup(void) {
2430     /* Don't use Py_AtExit because that has a 32-call limit
2431      * and is called after python finalization.
2432      */
2433
2434     PyObject *cleanup_func = 0;
2435     PyObject *atexit = 0;
2436     PyObject *reg = 0;
2437     PyObject *args = 0;
2438     PyObject *res = 0;
2439     int ret = -1;
2440
2441     cleanup_func = PyCFunction_New(&cleanup_def, 0);
2442     args = PyTuple_New(1);
2443     if (!cleanup_func || !args)
2444         goto bad;
2445     PyTuple_SET_ITEM(args, 0, cleanup_func);
2446     cleanup_func = 0;
2447
2448     atexit = __Pyx_ImportModule("atexit");
2449     if (!atexit)
2450         goto bad;
2451     reg = __Pyx_GetAttrString(atexit, "register");
2452     if (!reg)
2453         goto bad;
2454     res = PyObject_CallObject(reg, args);
2455     if (!res)
2456         goto bad;
2457     ret = 0;
2458 bad:
2459     Py_XDECREF(cleanup_func);
2460     Py_XDECREF(atexit);
2461     Py_XDECREF(reg);
2462     Py_XDECREF(args);
2463     Py_XDECREF(res);
2464     return ret;
2465 }
2466 """)
2467
2468 import_star_utility_code = """
2469
2470 /* import_all_from is an unexposed function from ceval.c */
2471
2472 static int
2473 __Pyx_import_all_from(PyObject *locals, PyObject *v)
2474 {
2475     PyObject *all = __Pyx_GetAttrString(v, "__all__");
2476     PyObject *dict, *name, *value;
2477     int skip_leading_underscores = 0;
2478     int pos, err;
2479
2480     if (all == NULL) {
2481         if (!PyErr_ExceptionMatches(PyExc_AttributeError))
2482             return -1; /* Unexpected error */
2483         PyErr_Clear();
2484         dict = __Pyx_GetAttrString(v, "__dict__");
2485         if (dict == NULL) {
2486             if (!PyErr_ExceptionMatches(PyExc_AttributeError))
2487                 return -1;
2488             PyErr_SetString(PyExc_ImportError,
2489             "from-import-* object has no __dict__ and no __all__");
2490             return -1;
2491         }
2492         all = PyMapping_Keys(dict);
2493         Py_DECREF(dict);
2494         if (all == NULL)
2495             return -1;
2496         skip_leading_underscores = 1;
2497     }
2498
2499     for (pos = 0, err = 0; ; pos++) {
2500         name = PySequence_GetItem(all, pos);
2501         if (name == NULL) {
2502             if (!PyErr_ExceptionMatches(PyExc_IndexError))
2503                 err = -1;
2504             else
2505                 PyErr_Clear();
2506             break;
2507         }
2508         if (skip_leading_underscores &&
2509 #if PY_MAJOR_VERSION < 3
2510             PyString_Check(name) &&
2511             PyString_AS_STRING(name)[0] == '_')
2512 #else
2513             PyUnicode_Check(name) &&
2514             PyUnicode_AS_UNICODE(name)[0] == '_')
2515 #endif
2516         {
2517             Py_DECREF(name);
2518             continue;
2519         }
2520         value = PyObject_GetAttr(v, name);
2521         if (value == NULL)
2522             err = -1;
2523         else if (PyDict_CheckExact(locals))
2524             err = PyDict_SetItem(locals, name, value);
2525         else
2526             err = PyObject_SetItem(locals, name, value);
2527         Py_DECREF(name);
2528         Py_XDECREF(value);
2529         if (err != 0)
2530             break;
2531     }
2532     Py_DECREF(all);
2533     return err;
2534 }
2535
2536
2537 static int %(IMPORT_STAR)s(PyObject* m) {
2538
2539     int i;
2540     int ret = -1;
2541     char* s;
2542     PyObject *locals = 0;
2543     PyObject *list = 0;
2544 #if PY_MAJOR_VERSION >= 3
2545     PyObject *utf8_name = 0;
2546 #endif
2547     PyObject *name;
2548     PyObject *item;
2549
2550     locals = PyDict_New();              if (!locals) goto bad;
2551     if (__Pyx_import_all_from(locals, m) < 0) goto bad;
2552     list = PyDict_Items(locals);        if (!list) goto bad;
2553
2554     for(i=0; i<PyList_GET_SIZE(list); i++) {
2555         name = PyTuple_GET_ITEM(PyList_GET_ITEM(list, i), 0);
2556         item = PyTuple_GET_ITEM(PyList_GET_ITEM(list, i), 1);
2557 #if PY_MAJOR_VERSION >= 3
2558         utf8_name = PyUnicode_AsUTF8String(name);
2559         if (!utf8_name) goto bad;
2560         s = PyBytes_AS_STRING(utf8_name);
2561         if (%(IMPORT_STAR_SET)s(item, name, s) < 0) goto bad;
2562         Py_DECREF(utf8_name); utf8_name = 0;
2563 #else
2564         s = PyString_AsString(name);
2565         if (!s) goto bad;
2566         if (%(IMPORT_STAR_SET)s(item, name, s) < 0) goto bad;
2567 #endif
2568     }
2569     ret = 0;
2570
2571 bad:
2572     Py_XDECREF(locals);
2573     Py_XDECREF(list);
2574 #if PY_MAJOR_VERSION >= 3
2575     Py_XDECREF(utf8_name);
2576 #endif
2577     return ret;
2578 }
2579 """ % {'IMPORT_STAR'     : Naming.import_star,
2580        'IMPORT_STAR_SET' : Naming.import_star_set }
2581
2582 refnanny_utility_code = UtilityCode(proto="""
2583 #ifndef CYTHON_REFNANNY
2584   #define CYTHON_REFNANNY 0
2585 #endif
2586
2587 #if CYTHON_REFNANNY
2588   typedef struct {
2589     void (*INCREF)(void*, PyObject*, int);
2590     void (*DECREF)(void*, PyObject*, int);
2591     void (*GOTREF)(void*, PyObject*, int);
2592     void (*GIVEREF)(void*, PyObject*, int);
2593     void* (*SetupContext)(const char*, int, const char*);
2594     void (*FinishContext)(void**);
2595   } __Pyx_RefNannyAPIStruct;
2596   static __Pyx_RefNannyAPIStruct *__Pyx_RefNanny = NULL;
2597   static __Pyx_RefNannyAPIStruct * __Pyx_RefNannyImportAPI(const char *modname) {
2598     PyObject *m = NULL, *p = NULL;
2599     void *r = NULL;
2600     m = PyImport_ImportModule((char *)modname);
2601     if (!m) goto end;
2602     p = PyObject_GetAttrString(m, (char *)\"RefNannyAPI\");
2603     if (!p) goto end;
2604     r = PyLong_AsVoidPtr(p);
2605   end:
2606     Py_XDECREF(p);
2607     Py_XDECREF(m);
2608     return (__Pyx_RefNannyAPIStruct *)r;
2609   }
2610   #define __Pyx_RefNannyDeclareContext void *__pyx_refnanny;
2611   #define __Pyx_RefNannySetupContext(name) \
2612           __pyx_refnanny = __Pyx_RefNanny->SetupContext((name), __LINE__, __FILE__)
2613   #define __Pyx_RefNannyFinishContext() \
2614           __Pyx_RefNanny->FinishContext(&__pyx_refnanny)
2615   #define __Pyx_INCREF(r) __Pyx_RefNanny->INCREF(__pyx_refnanny, (PyObject *)(r), __LINE__)
2616   #define __Pyx_DECREF(r) __Pyx_RefNanny->DECREF(__pyx_refnanny, (PyObject *)(r), __LINE__)
2617   #define __Pyx_GOTREF(r) __Pyx_RefNanny->GOTREF(__pyx_refnanny, (PyObject *)(r), __LINE__)
2618   #define __Pyx_GIVEREF(r) __Pyx_RefNanny->GIVEREF(__pyx_refnanny, (PyObject *)(r), __LINE__)
2619   #define __Pyx_XDECREF(r) do { if((r) != NULL) {__Pyx_DECREF(r);} } while(0)
2620 #else
2621   #define __Pyx_RefNannyDeclareContext
2622   #define __Pyx_RefNannySetupContext(name)
2623   #define __Pyx_RefNannyFinishContext()
2624   #define __Pyx_INCREF(r) Py_INCREF(r)
2625   #define __Pyx_DECREF(r) Py_DECREF(r)
2626   #define __Pyx_GOTREF(r)
2627   #define __Pyx_GIVEREF(r)
2628   #define __Pyx_XDECREF(r) Py_XDECREF(r)
2629 #endif /* CYTHON_REFNANNY */
2630 #define __Pyx_XGIVEREF(r) do { if((r) != NULL) {__Pyx_GIVEREF(r);} } while(0)
2631 #define __Pyx_XGOTREF(r) do { if((r) != NULL) {__Pyx_GOTREF(r);} } while(0)
2632 """)
2633
2634
2635 main_method = UtilityCode(
2636 impl = """
2637 #ifdef __FreeBSD__
2638 #include <floatingpoint.h>
2639 #endif
2640
2641 #if PY_MAJOR_VERSION < 3
2642 int main(int argc, char** argv) {
2643 #elif defined(WIN32) || defined(MS_WINDOWS)
2644 int wmain(int argc, wchar_t **argv) {
2645 #else
2646 static int __Pyx_main(int argc, wchar_t **argv) {
2647 #endif
2648     int r = 0;
2649     PyObject* m = NULL;
2650     /* 754 requires that FP exceptions run in "no stop" mode by default,
2651      * and until C vendors implement C99's ways to control FP exceptions,
2652      * Python requires non-stop mode.  Alas, some platforms enable FP
2653      * exceptions by default.  Here we disable them.
2654      */
2655 #ifdef __FreeBSD__
2656     fp_except_t m;
2657
2658     m = fpgetmask();
2659     fpsetmask(m & ~FP_X_OFL);
2660 #endif
2661     Py_SetProgramName(argv[0]);
2662     Py_Initialize();
2663     PySys_SetArgv(argc, argv);
2664     %(module_is_main)s = 1;
2665 #if PY_MAJOR_VERSION < 3
2666         init%(module_name)s();
2667 #else
2668         m = PyInit_%(module_name)s();
2669 #endif
2670     if (PyErr_Occurred() != NULL) {
2671         r = 1;
2672         PyErr_Print(); /* This exits with the right code if SystemExit. */
2673 #if PY_MAJOR_VERSION < 3
2674         if (Py_FlushLine()) PyErr_Clear();
2675 #endif
2676     }
2677     Py_XDECREF(m);
2678     Py_Finalize();
2679     return r;
2680 }
2681
2682
2683 #if PY_MAJOR_VERSION >= 3 && !defined(WIN32) && !defined(MS_WINDOWS)
2684 #include <locale.h>
2685
2686 static wchar_t*
2687 __Pyx_char2wchar(char* arg)
2688 {
2689         wchar_t *res;
2690 #ifdef HAVE_BROKEN_MBSTOWCS
2691         /* Some platforms have a broken implementation of
2692          * mbstowcs which does not count the characters that
2693          * would result from conversion.  Use an upper bound.
2694          */
2695         size_t argsize = strlen(arg);
2696 #else
2697         size_t argsize = mbstowcs(NULL, arg, 0);
2698 #endif
2699         size_t count;
2700         unsigned char *in;
2701         wchar_t *out;
2702 #ifdef HAVE_MBRTOWC
2703         mbstate_t mbs;
2704 #endif
2705         if (argsize != (size_t)-1) {
2706                 res = (wchar_t *)malloc((argsize+1)*sizeof(wchar_t));
2707                 if (!res)
2708                         goto oom;
2709                 count = mbstowcs(res, arg, argsize+1);
2710                 if (count != (size_t)-1) {
2711                         wchar_t *tmp;
2712                         /* Only use the result if it contains no
2713                            surrogate characters. */
2714                         for (tmp = res; *tmp != 0 &&
2715                                      (*tmp < 0xd800 || *tmp > 0xdfff); tmp++)
2716                                 ;
2717                         if (*tmp == 0)
2718                                 return res;
2719                 }
2720                 free(res);
2721         }
2722         /* Conversion failed. Fall back to escaping with surrogateescape. */
2723 #ifdef HAVE_MBRTOWC
2724         /* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */
2725
2726         /* Overallocate; as multi-byte characters are in the argument, the
2727            actual output could use less memory. */
2728         argsize = strlen(arg) + 1;
2729         res = malloc(argsize*sizeof(wchar_t));
2730         if (!res) goto oom;
2731         in = (unsigned char*)arg;
2732         out = res;
2733         memset(&mbs, 0, sizeof mbs);
2734         while (argsize) {
2735                 size_t converted = mbrtowc(out, (char*)in, argsize, &mbs);
2736                 if (converted == 0)
2737                         /* Reached end of string; null char stored. */
2738                         break;
2739                 if (converted == (size_t)-2) {
2740                         /* Incomplete character. This should never happen,
2741                            since we provide everything that we have -
2742                            unless there is a bug in the C library, or I
2743                            misunderstood how mbrtowc works. */
2744                         fprintf(stderr, "unexpected mbrtowc result -2\\n");
2745                         return NULL;
2746                 }
2747                 if (converted == (size_t)-1) {
2748                         /* Conversion error. Escape as UTF-8b, and start over
2749                            in the initial shift state. */
2750                         *out++ = 0xdc00 + *in++;
2751                         argsize--;
2752                         memset(&mbs, 0, sizeof mbs);
2753                         continue;
2754                 }
2755                 if (*out >= 0xd800 && *out <= 0xdfff) {
2756                         /* Surrogate character.  Escape the original
2757                            byte sequence with surrogateescape. */
2758                         argsize -= converted;
2759                         while (converted--)
2760                                 *out++ = 0xdc00 + *in++;
2761                         continue;
2762                 }
2763                 /* successfully converted some bytes */
2764                 in += converted;
2765                 argsize -= converted;
2766                 out++;
2767         }
2768 #else
2769         /* Cannot use C locale for escaping; manually escape as if charset
2770            is ASCII (i.e. escape all bytes > 128. This will still roundtrip
2771            correctly in the locale's charset, which must be an ASCII superset. */
2772         res = malloc((strlen(arg)+1)*sizeof(wchar_t));
2773         if (!res) goto oom;
2774         in = (unsigned char*)arg;
2775         out = res;
2776         while(*in)
2777                 if(*in < 128)
2778                         *out++ = *in++;
2779                 else
2780                         *out++ = 0xdc00 + *in++;
2781         *out = 0;
2782 #endif
2783         return res;
2784 oom:
2785         fprintf(stderr, "out of memory\\n");
2786         return NULL;
2787 }
2788
2789 int
2790 main(int argc, char **argv)
2791 {
2792         wchar_t **argv_copy = (wchar_t **)malloc(sizeof(wchar_t*)*argc);
2793         /* We need a second copies, as Python might modify the first one. */
2794         wchar_t **argv_copy2 = (wchar_t **)malloc(sizeof(wchar_t*)*argc);
2795         int i, res;
2796         char *oldloc;
2797         if (!argv_copy || !argv_copy2) {
2798                 fprintf(stderr, "out of memory\\n");
2799                 return 1;
2800         }
2801         oldloc = strdup(setlocale(LC_ALL, NULL));
2802         setlocale(LC_ALL, "");
2803         for (i = 0; i < argc; i++) {
2804                 argv_copy2[i] = argv_copy[i] = __Pyx_char2wchar(argv[i]);
2805                 if (!argv_copy[i])
2806                         return 1;
2807         }
2808         setlocale(LC_ALL, oldloc);
2809         free(oldloc);
2810         res = __Pyx_main(argc, argv_copy);
2811         for (i = 0; i < argc; i++) {
2812                 free(argv_copy2[i]);
2813         }
2814         free(argv_copy);
2815         free(argv_copy2);
2816         return res;
2817 }
2818 #endif
2819 """)
2820
2821 packed_struct_utility_code = UtilityCode(proto="""
2822 #if defined(__GNUC__)
2823 #define __Pyx_PACKED __attribute__((__packed__))
2824 #else
2825 #define __Pyx_PACKED
2826 #endif
2827 """, impl="", proto_block='utility_code_proto_before_types')