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