remove trailing whitespace
[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             if attr.is_declared_generic:
968                 attr_type = py_object_type
969             else:
970                 attr_type = attr.type
971             code.putln(
972                 "%s;" %
973                     attr_type.declaration_code(attr.cname))
974         code.putln(footer)
975         if type.objtypedef_cname is not None:
976             # Only for exposing public typedef name.
977             code.putln("typedef struct %s %s;" % (type.objstruct_cname, type.objtypedef_cname))
978
979     def generate_global_declarations(self, env, code, definition):
980         code.putln("")
981         for entry in env.c_class_entries:
982             if definition or entry.defined_in_pxd:
983                 code.putln("static PyTypeObject *%s = 0;" %
984                     entry.type.typeptr_cname)
985         code.put_var_declarations(env.var_entries, static = 1,
986             dll_linkage = "DL_EXPORT", definition = definition)
987
988     def generate_cfunction_predeclarations(self, env, code, definition):
989         for entry in env.cfunc_entries:
990             if entry.inline_func_in_pxd or (not entry.in_cinclude and (definition
991                     or entry.defined_in_pxd or entry.visibility == 'extern')):
992                 if entry.visibility == 'public':
993                     storage_class = "%s " % Naming.extern_c_macro
994                     dll_linkage = "DL_EXPORT"
995                 elif entry.visibility == 'extern':
996                     storage_class = "%s " % Naming.extern_c_macro
997                     dll_linkage = "DL_IMPORT"
998                 elif entry.visibility == 'private':
999                     storage_class = "static "
1000                     dll_linkage = None
1001                 else:
1002                     storage_class = "static "
1003                     dll_linkage = None
1004                 type = entry.type
1005
1006                 if not definition and entry.defined_in_pxd:
1007                     type = CPtrType(type)
1008                 header = type.declaration_code(entry.cname,
1009                                                dll_linkage = dll_linkage)
1010                 if entry.func_modifiers:
1011                     modifiers = "%s " % ' '.join(entry.func_modifiers).upper()
1012                 else:
1013                     modifiers = ''
1014                 code.putln("%s%s%s; /*proto*/" % (
1015                     storage_class,
1016                     modifiers,
1017                     header))
1018
1019     def generate_typeobj_definitions(self, env, code):
1020         full_module_name = env.qualified_name
1021         for entry in env.c_class_entries:
1022             #print "generate_typeobj_definitions:", entry.name
1023             #print "...visibility =", entry.visibility
1024             if entry.visibility != 'extern':
1025                 type = entry.type
1026                 scope = type.scope
1027                 if scope: # could be None if there was an error
1028                     self.generate_exttype_vtable(scope, code)
1029                     self.generate_new_function(scope, code)
1030                     self.generate_dealloc_function(scope, code)
1031                     if scope.needs_gc():
1032                         self.generate_traverse_function(scope, code)
1033                         self.generate_clear_function(scope, code)
1034                     if scope.defines_any(["__getitem__"]):
1035                         self.generate_getitem_int_function(scope, code)
1036                     if scope.defines_any(["__setitem__", "__delitem__"]):
1037                         self.generate_ass_subscript_function(scope, code)
1038                     if scope.defines_any(["__getslice__", "__setslice__", "__delslice__"]):
1039                         warning(self.pos, "__getslice__, __setslice__, and __delslice__ are not supported by Python 3, use __getitem__, __setitem__, and __delitem__ instead", 1)
1040                         code.putln("#if PY_MAJOR_VERSION >= 3")
1041                         code.putln("#error __getslice__, __setslice__, and __delslice__ not supported in Python 3.")
1042                         code.putln("#endif")
1043                     if scope.defines_any(["__setslice__", "__delslice__"]):
1044                         self.generate_ass_slice_function(scope, code)
1045                     if scope.defines_any(["__getattr__","__getattribute__"]):
1046                         self.generate_getattro_function(scope, code)
1047                     if scope.defines_any(["__setattr__", "__delattr__"]):
1048                         self.generate_setattro_function(scope, code)
1049                     if scope.defines_any(["__get__"]):
1050                         self.generate_descr_get_function(scope, code)
1051                     if scope.defines_any(["__set__", "__delete__"]):
1052                         self.generate_descr_set_function(scope, code)
1053                     self.generate_property_accessors(scope, code)
1054                     self.generate_method_table(scope, code)
1055                     self.generate_getset_table(scope, code)
1056                     self.generate_typeobj_definition(full_module_name, entry, code)
1057
1058     def generate_exttype_vtable(self, scope, code):
1059         # Generate the definition of an extension type's vtable.
1060         type = scope.parent_type
1061         if type.vtable_cname:
1062             code.putln("static struct %s %s;" % (
1063                 type.vtabstruct_cname,
1064                 type.vtable_cname))
1065
1066     def generate_self_cast(self, scope, code):
1067         type = scope.parent_type
1068         code.putln(
1069             "%s = (%s)o;" % (
1070                 type.declaration_code("p"),
1071                 type.declaration_code("")))
1072
1073     def generate_new_function(self, scope, code):
1074         tp_slot = TypeSlots.ConstructorSlot("tp_new", '__new__')
1075         slot_func = scope.mangle_internal("tp_new")
1076         type = scope.parent_type
1077         base_type = type.base_type
1078         py_attrs = []
1079         for entry in scope.var_entries:
1080             if entry.type.is_pyobject:
1081                 py_attrs.append(entry)
1082         need_self_cast = type.vtabslot_cname or py_attrs
1083         code.putln("")
1084         code.putln(
1085             "static PyObject *%s(PyTypeObject *t, PyObject *a, PyObject *k) {"
1086                 % scope.mangle_internal("tp_new"))
1087         if need_self_cast:
1088             code.putln(
1089                 "%s;"
1090                     % scope.parent_type.declaration_code("p"))
1091         if base_type:
1092             tp_new = TypeSlots.get_base_slot_function(scope, tp_slot)
1093             if tp_new is None:
1094                 tp_new = "%s->tp_new" % base_type.typeptr_cname
1095             code.putln(
1096                 "PyObject *o = %s(t, a, k);" % tp_new)
1097         else:
1098             code.putln(
1099                 "PyObject *o = (*t->tp_alloc)(t, 0);")
1100         code.putln(
1101                 "if (!o) return 0;")
1102         if need_self_cast:
1103             code.putln(
1104                 "p = %s;"
1105                     % type.cast_code("o"))
1106         #if need_self_cast:
1107         #    self.generate_self_cast(scope, code)
1108         if type.vtabslot_cname:
1109             vtab_base_type = type
1110             while vtab_base_type.base_type and vtab_base_type.base_type.vtabstruct_cname:
1111                 vtab_base_type = vtab_base_type.base_type
1112             if vtab_base_type is not type:
1113                 struct_type_cast = "(struct %s*)" % vtab_base_type.vtabstruct_cname
1114             else:
1115                 struct_type_cast = ""
1116             code.putln("p->%s = %s%s;" % (
1117                 type.vtabslot_cname,
1118                 struct_type_cast, type.vtabptr_cname))
1119         for entry in py_attrs:
1120             if scope.is_internal or entry.name == "__weakref__":
1121                 # internal classes do not need None inits
1122                 code.putln("p->%s = 0;" % entry.cname)
1123             else:
1124                 code.put_init_var_to_py_none(entry, "p->%s", nanny=False)
1125         entry = scope.lookup_here("__new__")
1126         if entry and entry.is_special:
1127             if entry.trivial_signature:
1128                 cinit_args = "o, %s, NULL" % Naming.empty_tuple
1129             else:
1130                 cinit_args = "o, a, k"
1131             code.putln(
1132                 "if (%s(%s) < 0) {" %
1133                     (entry.func_cname, cinit_args))
1134             code.put_decref_clear("o", py_object_type, nanny=False);
1135             code.putln(
1136                 "}")
1137         code.putln(
1138             "return o;")
1139         code.putln(
1140             "}")
1141
1142     def generate_dealloc_function(self, scope, code):
1143         tp_slot = TypeSlots.ConstructorSlot("tp_dealloc", '__dealloc__')
1144         slot_func = scope.mangle_internal("tp_dealloc")
1145         base_type = scope.parent_type.base_type
1146         if tp_slot.slot_code(scope) != slot_func:
1147             return # never used
1148         code.putln("")
1149         code.putln(
1150             "static void %s(PyObject *o) {"
1151                 % scope.mangle_internal("tp_dealloc"))
1152         py_attrs = []
1153         weakref_slot = scope.lookup_here("__weakref__")
1154         for entry in scope.var_entries:
1155             if entry.type.is_pyobject and entry is not weakref_slot:
1156                 py_attrs.append(entry)
1157         if py_attrs or weakref_slot in scope.var_entries:
1158             self.generate_self_cast(scope, code)
1159         self.generate_usr_dealloc_call(scope, code)
1160         if weakref_slot in scope.var_entries:
1161             code.putln("if (p->__weakref__) PyObject_ClearWeakRefs(o);")
1162         for entry in py_attrs:
1163             code.put_xdecref("p->%s" % entry.cname, entry.type, nanny=False)
1164         if base_type:
1165             tp_dealloc = TypeSlots.get_base_slot_function(scope, tp_slot)
1166             if tp_dealloc is None:
1167                 tp_dealloc = "%s->tp_dealloc" % base_type.typeptr_cname
1168             code.putln(
1169                     "%s(o);" % tp_dealloc)
1170         else:
1171             code.putln(
1172                     "(*Py_TYPE(o)->tp_free)(o);")
1173         code.putln(
1174             "}")
1175
1176     def generate_usr_dealloc_call(self, scope, code):
1177         entry = scope.lookup_here("__dealloc__")
1178         if entry:
1179             code.putln(
1180                 "{")
1181             code.putln(
1182                     "PyObject *etype, *eval, *etb;")
1183             code.putln(
1184                     "PyErr_Fetch(&etype, &eval, &etb);")
1185             code.putln(
1186                     "++Py_REFCNT(o);")
1187             code.putln(
1188                     "%s(o);" %
1189                         entry.func_cname)
1190             code.putln(
1191                     "if (PyErr_Occurred()) PyErr_WriteUnraisable(o);")
1192             code.putln(
1193                     "--Py_REFCNT(o);")
1194             code.putln(
1195                     "PyErr_Restore(etype, eval, etb);")
1196             code.putln(
1197                 "}")
1198
1199     def generate_traverse_function(self, scope, code):
1200         tp_slot = TypeSlots.GCDependentSlot("tp_traverse")
1201         slot_func = scope.mangle_internal("tp_traverse")
1202         base_type = scope.parent_type.base_type
1203         if tp_slot.slot_code(scope) != slot_func:
1204             return # never used
1205         code.putln("")
1206         code.putln(
1207             "static int %s(PyObject *o, visitproc v, void *a) {"
1208                 % slot_func)
1209         py_attrs = []
1210         for entry in scope.var_entries:
1211             if entry.type.is_pyobject and entry.name != "__weakref__":
1212                 py_attrs.append(entry)
1213         if base_type or py_attrs:
1214             code.putln("int e;")
1215         if py_attrs:
1216             self.generate_self_cast(scope, code)
1217         if base_type:
1218             # want to call it explicitly if possible so inlining can be performed
1219             static_call = TypeSlots.get_base_slot_function(scope, tp_slot)
1220             if static_call:
1221                 code.putln("e = %s(o, v, a); if (e) return e;" % static_call)
1222             else:
1223                 code.putln("if (%s->tp_traverse) {" % base_type.typeptr_cname)
1224                 code.putln(
1225                         "e = %s->tp_traverse(o, v, a); if (e) return e;" %
1226                             base_type.typeptr_cname)
1227                 code.putln("}")
1228         for entry in py_attrs:
1229             var_code = "p->%s" % entry.cname
1230             code.putln(
1231                     "if (%s) {"
1232                         % var_code)
1233             if entry.type.is_extension_type:
1234                 var_code = "((PyObject*)%s)" % var_code
1235             code.putln(
1236                         "e = (*v)(%s, a); if (e) return e;"
1237                             % var_code)
1238             code.putln(
1239                     "}")
1240         code.putln(
1241                 "return 0;")
1242         code.putln(
1243             "}")
1244
1245     def generate_clear_function(self, scope, code):
1246         tp_slot = TypeSlots.GCDependentSlot("tp_clear")
1247         slot_func = scope.mangle_internal("tp_clear")
1248         base_type = scope.parent_type.base_type
1249         if tp_slot.slot_code(scope) != slot_func:
1250             return # never used
1251         code.putln("")
1252         code.putln("static int %s(PyObject *o) {" % slot_func)
1253         py_attrs = []
1254         for entry in scope.var_entries:
1255             if entry.type.is_pyobject and entry.name != "__weakref__":
1256                 py_attrs.append(entry)
1257         if py_attrs:
1258             self.generate_self_cast(scope, code)
1259             code.putln("PyObject* tmp;")
1260         if base_type:
1261             # want to call it explicitly if possible so inlining can be performed
1262             static_call = TypeSlots.get_base_slot_function(scope, tp_slot)
1263             if static_call:
1264                 code.putln("%s(o);" % static_call)
1265             else:
1266                 code.putln("if (%s->tp_clear) {" % base_type.typeptr_cname)
1267                 code.putln("%s->tp_clear(o);" % base_type.typeptr_cname)
1268                 code.putln("}")
1269         for entry in py_attrs:
1270             name = "p->%s" % entry.cname
1271             code.putln("tmp = ((PyObject*)%s);" % name)
1272             if entry.is_declared_generic:
1273                 code.put_init_to_py_none(name, py_object_type, nanny=False)
1274             else:
1275                 code.put_init_to_py_none(name, entry.type, nanny=False)
1276             code.putln("Py_XDECREF(tmp);")
1277         code.putln(
1278             "return 0;")
1279         code.putln(
1280             "}")
1281
1282     def generate_getitem_int_function(self, scope, code):
1283         # This function is put into the sq_item slot when
1284         # a __getitem__ method is present. It converts its
1285         # argument to a Python integer and calls mp_subscript.
1286         code.putln(
1287             "static PyObject *%s(PyObject *o, Py_ssize_t i) {" %
1288                 scope.mangle_internal("sq_item"))
1289         code.putln(
1290                 "PyObject *r;")
1291         code.putln(
1292                 "PyObject *x = PyInt_FromSsize_t(i); if(!x) return 0;")
1293         code.putln(
1294                 "r = Py_TYPE(o)->tp_as_mapping->mp_subscript(o, x);")
1295         code.putln(
1296                 "Py_DECREF(x);")
1297         code.putln(
1298                 "return r;")
1299         code.putln(
1300             "}")
1301
1302     def generate_ass_subscript_function(self, scope, code):
1303         # Setting and deleting an item are both done through
1304         # the ass_subscript method, so we dispatch to user's __setitem__
1305         # or __delitem__, or raise an exception.
1306         base_type = scope.parent_type.base_type
1307         set_entry = scope.lookup_here("__setitem__")
1308         del_entry = scope.lookup_here("__delitem__")
1309         code.putln("")
1310         code.putln(
1311             "static int %s(PyObject *o, PyObject *i, PyObject *v) {" %
1312                 scope.mangle_internal("mp_ass_subscript"))
1313         code.putln(
1314                 "if (v) {")
1315         if set_entry:
1316             code.putln(
1317                     "return %s(o, i, v);" %
1318                         set_entry.func_cname)
1319         else:
1320             self.generate_guarded_basetype_call(
1321                 base_type, "tp_as_mapping", "mp_ass_subscript", "o, i, v", code)
1322             code.putln(
1323                     "PyErr_Format(PyExc_NotImplementedError,")
1324             code.putln(
1325                     '  "Subscript assignment not supported by %s", Py_TYPE(o)->tp_name);')
1326             code.putln(
1327                     "return -1;")
1328         code.putln(
1329                 "}")
1330         code.putln(
1331                 "else {")
1332         if del_entry:
1333             code.putln(
1334                     "return %s(o, i);" %
1335                         del_entry.func_cname)
1336         else:
1337             self.generate_guarded_basetype_call(
1338                 base_type, "tp_as_mapping", "mp_ass_subscript", "o, i, v", code)
1339             code.putln(
1340                     "PyErr_Format(PyExc_NotImplementedError,")
1341             code.putln(
1342                     '  "Subscript deletion not supported by %s", Py_TYPE(o)->tp_name);')
1343             code.putln(
1344                     "return -1;")
1345         code.putln(
1346                 "}")
1347         code.putln(
1348             "}")
1349
1350     def generate_guarded_basetype_call(
1351             self, base_type, substructure, slot, args, code):
1352         if base_type:
1353             base_tpname = base_type.typeptr_cname
1354             if substructure:
1355                 code.putln(
1356                     "if (%s->%s && %s->%s->%s)" % (
1357                         base_tpname, substructure, base_tpname, substructure, slot))
1358                 code.putln(
1359                     "  return %s->%s->%s(%s);" % (
1360                         base_tpname, substructure, slot, args))
1361             else:
1362                 code.putln(
1363                     "if (%s->%s)" % (
1364                         base_tpname, slot))
1365                 code.putln(
1366                     "  return %s->%s(%s);" % (
1367                         base_tpname, slot, args))
1368
1369     def generate_ass_slice_function(self, scope, code):
1370         # Setting and deleting a slice are both done through
1371         # the ass_slice method, so we dispatch to user's __setslice__
1372         # or __delslice__, or raise an exception.
1373         base_type = scope.parent_type.base_type
1374         set_entry = scope.lookup_here("__setslice__")
1375         del_entry = scope.lookup_here("__delslice__")
1376         code.putln("")
1377         code.putln(
1378             "static int %s(PyObject *o, Py_ssize_t i, Py_ssize_t j, PyObject *v) {" %
1379                 scope.mangle_internal("sq_ass_slice"))
1380         code.putln(
1381                 "if (v) {")
1382         if set_entry:
1383             code.putln(
1384                     "return %s(o, i, j, v);" %
1385                         set_entry.func_cname)
1386         else:
1387             self.generate_guarded_basetype_call(
1388                 base_type, "tp_as_sequence", "sq_ass_slice", "o, i, j, v", code)
1389             code.putln(
1390                     "PyErr_Format(PyExc_NotImplementedError,")
1391             code.putln(
1392                     '  "2-element slice assignment not supported by %s", Py_TYPE(o)->tp_name);')
1393             code.putln(
1394                     "return -1;")
1395         code.putln(
1396                 "}")
1397         code.putln(
1398                 "else {")
1399         if del_entry:
1400             code.putln(
1401                     "return %s(o, i, j);" %
1402                         del_entry.func_cname)
1403         else:
1404             self.generate_guarded_basetype_call(
1405                 base_type, "tp_as_sequence", "sq_ass_slice", "o, i, j, v", code)
1406             code.putln(
1407                     "PyErr_Format(PyExc_NotImplementedError,")
1408             code.putln(
1409                     '  "2-element slice deletion not supported by %s", Py_TYPE(o)->tp_name);')
1410             code.putln(
1411                     "return -1;")
1412         code.putln(
1413                 "}")
1414         code.putln(
1415             "}")
1416
1417     def generate_getattro_function(self, scope, code):
1418         # First try to get the attribute using __getattribute__, if defined, or
1419         # PyObject_GenericGetAttr.
1420         #
1421         # If that raises an AttributeError, call the __getattr__ if defined.
1422         #
1423         # In both cases, defined can be in this class, or any base class.
1424         def lookup_here_or_base(n,type=None):
1425             # Recursive lookup
1426             if type is None:
1427                 type = scope.parent_type
1428             r = type.scope.lookup_here(n)
1429             if r is None and \
1430                type.base_type is not None:
1431                 return lookup_here_or_base(n,type.base_type)
1432             else:
1433                 return r
1434         getattr_entry = lookup_here_or_base("__getattr__")
1435         getattribute_entry = lookup_here_or_base("__getattribute__")
1436         code.putln("")
1437         code.putln(
1438             "static PyObject *%s(PyObject *o, PyObject *n) {"
1439                 % scope.mangle_internal("tp_getattro"))
1440         if getattribute_entry is not None:
1441             code.putln(
1442                 "PyObject *v = %s(o, n);" %
1443                     getattribute_entry.func_cname)
1444         else:
1445             code.putln(
1446                 "PyObject *v = PyObject_GenericGetAttr(o, n);")
1447         if getattr_entry is not None:
1448             code.putln(
1449                 "if (!v && PyErr_ExceptionMatches(PyExc_AttributeError)) {")
1450             code.putln(
1451                 "PyErr_Clear();")
1452             code.putln(
1453                 "v = %s(o, n);" %
1454                     getattr_entry.func_cname)
1455             code.putln(
1456                 "}")
1457         code.putln(
1458             "return v;")
1459         code.putln(
1460             "}")
1461
1462     def generate_setattro_function(self, scope, code):
1463         # Setting and deleting an attribute are both done through
1464         # the setattro method, so we dispatch to user's __setattr__
1465         # or __delattr__ or fall back on PyObject_GenericSetAttr.
1466         base_type = scope.parent_type.base_type
1467         set_entry = scope.lookup_here("__setattr__")
1468         del_entry = scope.lookup_here("__delattr__")
1469         code.putln("")
1470         code.putln(
1471             "static int %s(PyObject *o, PyObject *n, PyObject *v) {" %
1472                 scope.mangle_internal("tp_setattro"))
1473         code.putln(
1474                 "if (v) {")
1475         if set_entry:
1476             code.putln(
1477                     "return %s(o, n, v);" %
1478                         set_entry.func_cname)
1479         else:
1480             self.generate_guarded_basetype_call(
1481                 base_type, None, "tp_setattro", "o, n, v", code)
1482             code.putln(
1483                     "return PyObject_GenericSetAttr(o, n, v);")
1484         code.putln(
1485                 "}")
1486         code.putln(
1487                 "else {")
1488         if del_entry:
1489             code.putln(
1490                     "return %s(o, n);" %
1491                         del_entry.func_cname)
1492         else:
1493             self.generate_guarded_basetype_call(
1494                 base_type, None, "tp_setattro", "o, n, v", code)
1495             code.putln(
1496                     "return PyObject_GenericSetAttr(o, n, 0);")
1497         code.putln(
1498                 "}")
1499         code.putln(
1500             "}")
1501
1502     def generate_descr_get_function(self, scope, code):
1503         # The __get__ function of a descriptor object can be
1504         # called with NULL for the second or third arguments
1505         # under some circumstances, so we replace them with
1506         # None in that case.
1507         user_get_entry = scope.lookup_here("__get__")
1508         code.putln("")
1509         code.putln(
1510             "static PyObject *%s(PyObject *o, PyObject *i, PyObject *c) {" %
1511                 scope.mangle_internal("tp_descr_get"))
1512         code.putln(
1513             "PyObject *r = 0;")
1514         code.putln(
1515             "if (!i) i = Py_None;")
1516         code.putln(
1517             "if (!c) c = Py_None;")
1518         #code.put_incref("i", py_object_type)
1519         #code.put_incref("c", py_object_type)
1520         code.putln(
1521             "r = %s(o, i, c);" %
1522                 user_get_entry.func_cname)
1523         #code.put_decref("i", py_object_type)
1524         #code.put_decref("c", py_object_type)
1525         code.putln(
1526             "return r;")
1527         code.putln(
1528             "}")
1529
1530     def generate_descr_set_function(self, scope, code):
1531         # Setting and deleting are both done through the __set__
1532         # method of a descriptor, so we dispatch to user's __set__
1533         # or __delete__ or raise an exception.
1534         base_type = scope.parent_type.base_type
1535         user_set_entry = scope.lookup_here("__set__")
1536         user_del_entry = scope.lookup_here("__delete__")
1537         code.putln("")
1538         code.putln(
1539             "static int %s(PyObject *o, PyObject *i, PyObject *v) {" %
1540                 scope.mangle_internal("tp_descr_set"))
1541         code.putln(
1542                 "if (v) {")
1543         if user_set_entry:
1544             code.putln(
1545                     "return %s(o, i, v);" %
1546                         user_set_entry.func_cname)
1547         else:
1548             self.generate_guarded_basetype_call(
1549                 base_type, None, "tp_descr_set", "o, i, v", code)
1550             code.putln(
1551                     'PyErr_SetString(PyExc_NotImplementedError, "__set__");')
1552             code.putln(
1553                     "return -1;")
1554         code.putln(
1555                 "}")
1556         code.putln(
1557                 "else {")
1558         if user_del_entry:
1559             code.putln(
1560                     "return %s(o, i);" %
1561                         user_del_entry.func_cname)
1562         else:
1563             self.generate_guarded_basetype_call(
1564                 base_type, None, "tp_descr_set", "o, i, v", code)
1565             code.putln(
1566                     'PyErr_SetString(PyExc_NotImplementedError, "__delete__");')
1567             code.putln(
1568                     "return -1;")
1569         code.putln(
1570                 "}")
1571         code.putln(
1572             "}")
1573
1574     def generate_property_accessors(self, cclass_scope, code):
1575         for entry in cclass_scope.property_entries:
1576             property_scope = entry.scope
1577             if property_scope.defines_any(["__get__"]):
1578                 self.generate_property_get_function(entry, code)
1579             if property_scope.defines_any(["__set__", "__del__"]):
1580                 self.generate_property_set_function(entry, code)
1581
1582     def generate_property_get_function(self, property_entry, code):
1583         property_scope = property_entry.scope
1584         property_entry.getter_cname = property_scope.parent_scope.mangle(
1585             Naming.prop_get_prefix, property_entry.name)
1586         get_entry = property_scope.lookup_here("__get__")
1587         code.putln("")
1588         code.putln(
1589             "static PyObject *%s(PyObject *o, void *x) {" %
1590                 property_entry.getter_cname)
1591         code.putln(
1592                 "return %s(o);" %
1593                     get_entry.func_cname)
1594         code.putln(
1595             "}")
1596
1597     def generate_property_set_function(self, property_entry, code):
1598         property_scope = property_entry.scope
1599         property_entry.setter_cname = property_scope.parent_scope.mangle(
1600             Naming.prop_set_prefix, property_entry.name)
1601         set_entry = property_scope.lookup_here("__set__")
1602         del_entry = property_scope.lookup_here("__del__")
1603         code.putln("")
1604         code.putln(
1605             "static int %s(PyObject *o, PyObject *v, void *x) {" %
1606                 property_entry.setter_cname)
1607         code.putln(
1608                 "if (v) {")
1609         if set_entry:
1610             code.putln(
1611                     "return %s(o, v);" %
1612                         set_entry.func_cname)
1613         else:
1614             code.putln(
1615                     'PyErr_SetString(PyExc_NotImplementedError, "__set__");')
1616             code.putln(
1617                     "return -1;")
1618         code.putln(
1619                 "}")
1620         code.putln(
1621                 "else {")
1622         if del_entry:
1623             code.putln(
1624                     "return %s(o);" %
1625                         del_entry.func_cname)
1626         else:
1627             code.putln(
1628                     'PyErr_SetString(PyExc_NotImplementedError, "__del__");')
1629             code.putln(
1630                     "return -1;")
1631         code.putln(
1632                 "}")
1633         code.putln(
1634             "}")
1635
1636     def generate_typeobj_definition(self, modname, entry, code):
1637         type = entry.type
1638         scope = type.scope
1639         for suite in TypeSlots.substructures:
1640             suite.generate_substructure(scope, code)
1641         code.putln("")
1642         if entry.visibility == 'public':
1643             header = "DL_EXPORT(PyTypeObject) %s = {"
1644         else:
1645             header = "static PyTypeObject %s = {"
1646         #code.putln(header % scope.parent_type.typeobj_cname)
1647         code.putln(header % type.typeobj_cname)
1648         code.putln(
1649             "PyVarObject_HEAD_INIT(0, 0)")
1650         code.putln(
1651             '__Pyx_NAMESTR("%s.%s"), /*tp_name*/' % (
1652                 self.full_module_name, scope.class_name))
1653         if type.typedef_flag:
1654             objstruct = type.objstruct_cname
1655         else:
1656             objstruct = "struct %s" % type.objstruct_cname
1657         code.putln(
1658             "sizeof(%s), /*tp_basicsize*/" %
1659                 objstruct)
1660         code.putln(
1661             "0, /*tp_itemsize*/")
1662         for slot in TypeSlots.slot_table:
1663             slot.generate(scope, code)
1664         code.putln(
1665             "};")
1666
1667     def generate_method_table(self, env, code):
1668         code.putln("")
1669         code.putln(
1670             "static PyMethodDef %s[] = {" %
1671                 env.method_table_cname)
1672         for entry in env.pyfunc_entries:
1673             code.put_pymethoddef(entry, ",")
1674         code.putln(
1675                 "{0, 0, 0, 0}")
1676         code.putln(
1677             "};")
1678
1679     def generate_getset_table(self, env, code):
1680         if env.property_entries:
1681             code.putln("")
1682             code.putln(
1683                 "static struct PyGetSetDef %s[] = {" %
1684                     env.getset_table_cname)
1685             for entry in env.property_entries:
1686                 if entry.doc:
1687                     doc_code = "__Pyx_DOCSTR(%s)" % code.get_string_const(entry.doc)
1688                 else:
1689                     doc_code = "0"
1690                 code.putln(
1691                     '{(char *)"%s", %s, %s, %s, 0},' % (
1692                         entry.name,
1693                         entry.getter_cname or "0",
1694                         entry.setter_cname or "0",
1695                         doc_code))
1696             code.putln(
1697                     "{0, 0, 0, 0, 0}")
1698             code.putln(
1699                 "};")
1700
1701     def generate_import_star(self, env, code):
1702         env.use_utility_code(streq_utility_code)
1703         code.putln()
1704         code.putln("char* %s_type_names[] = {" % Naming.import_star)
1705         for name, entry in env.entries.items():
1706             if entry.is_type:
1707                 code.putln('"%s",' % name)
1708         code.putln("0")
1709         code.putln("};")
1710         code.putln()
1711         code.enter_cfunc_scope() # as we need labels
1712         code.putln("static int %s(PyObject *o, PyObject* py_name, char *name) {" % Naming.import_star_set)
1713         code.putln("char** type_name = %s_type_names;" % Naming.import_star)
1714         code.putln("while (*type_name) {")
1715         code.putln("if (__Pyx_StrEq(name, *type_name)) {")
1716         code.putln('PyErr_Format(PyExc_TypeError, "Cannot overwrite C type %s", name);')
1717         code.putln('goto bad;')
1718         code.putln("}")
1719         code.putln("type_name++;")
1720         code.putln("}")
1721         old_error_label = code.new_error_label()
1722         code.putln("if (0);") # so the first one can be "else if"
1723         for name, entry in env.entries.items():
1724             if entry.is_cglobal and entry.used:
1725                 code.putln('else if (__Pyx_StrEq(name, "%s")) {' % name)
1726                 if entry.type.is_pyobject:
1727                     if entry.type.is_extension_type or entry.type.is_builtin_type:
1728                         code.putln("if (!(%s)) %s;" % (
1729                             entry.type.type_test_code("o"),
1730                             code.error_goto(entry.pos)))
1731                     code.putln("Py_INCREF(o);")
1732                     code.put_decref(entry.cname, entry.type, nanny=False)
1733                     code.putln("%s = %s;" % (
1734                         entry.cname,
1735                         PyrexTypes.typecast(entry.type, py_object_type, "o")))
1736                 elif entry.type.from_py_function:
1737                     rhs = "%s(o)" % entry.type.from_py_function
1738                     if entry.type.is_enum:
1739                         rhs = PyrexTypes.typecast(entry.type, PyrexTypes.c_long_type, rhs)
1740                     code.putln("%s = %s; if (%s) %s;" % (
1741                         entry.cname,
1742                         rhs,
1743                         entry.type.error_condition(entry.cname),
1744                         code.error_goto(entry.pos)))
1745                 else:
1746                     code.putln('PyErr_Format(PyExc_TypeError, "Cannot convert Python object %s to %s");' % (name, entry.type))
1747                     code.putln(code.error_goto(entry.pos))
1748                 code.putln("}")
1749         code.putln("else {")
1750         code.putln("if (PyObject_SetAttr(%s, py_name, o) < 0) goto bad;" % Naming.module_cname)
1751         code.putln("}")
1752         code.putln("return 0;")
1753         if code.label_used(code.error_label):
1754             code.put_label(code.error_label)
1755             # This helps locate the offending name.
1756             code.putln('__Pyx_AddTraceback("%s");' % self.full_module_name);
1757         code.error_label = old_error_label
1758         code.putln("bad:")
1759         code.putln("return -1;")
1760         code.putln("}")
1761         code.putln(import_star_utility_code)
1762         code.exit_cfunc_scope() # done with labels
1763
1764     def generate_module_init_func(self, imported_modules, env, code):
1765         code.enter_cfunc_scope()
1766         code.putln("")
1767         header2 = "PyMODINIT_FUNC init%s(void)" % env.module_name
1768         header3 = "PyMODINIT_FUNC PyInit_%s(void)" % env.module_name
1769         code.putln("#if PY_MAJOR_VERSION < 3")
1770         code.putln("%s; /*proto*/" % header2)
1771         code.putln(header2)
1772         code.putln("#else")
1773         code.putln("%s; /*proto*/" % header3)
1774         code.putln(header3)
1775         code.putln("#endif")
1776         code.putln("{")
1777         tempdecl_code = code.insertion_point()
1778
1779         code.put_declare_refcount_context()
1780         code.putln("#if CYTHON_REFNANNY")
1781         code.putln("__Pyx_RefNanny = __Pyx_RefNannyImportAPI(\"refnanny\");")
1782         code.putln("if (!__Pyx_RefNanny) {")
1783         code.putln("  PyErr_Clear();")
1784         code.putln("  __Pyx_RefNanny = __Pyx_RefNannyImportAPI(\"Cython.Runtime.refnanny\");")
1785         code.putln("  if (!__Pyx_RefNanny)")
1786         code.putln("      Py_FatalError(\"failed to import 'refnanny' module\");")
1787         code.putln("}")
1788         code.putln("#endif")
1789         code.put_setup_refcount_context(header3)
1790
1791         env.use_utility_code(check_binary_version_utility_code)
1792         code.putln("if ( __Pyx_check_binary_version() < 0) %s" % code.error_goto(self.pos))
1793
1794         code.putln("%s = PyTuple_New(0); %s" % (Naming.empty_tuple, code.error_goto_if_null(Naming.empty_tuple, self.pos)));
1795         code.putln("%s = PyBytes_FromStringAndSize(\"\", 0); %s" % (Naming.empty_bytes, code.error_goto_if_null(Naming.empty_bytes, self.pos)));
1796
1797         code.putln("#ifdef %s_USED" % Naming.binding_cfunc)
1798         code.putln("if (%s_init() < 0) %s" % (Naming.binding_cfunc, code.error_goto(self.pos)))
1799         code.putln("#endif")
1800
1801         code.putln("/*--- Library function declarations ---*/")
1802         env.generate_library_function_declarations(code)
1803
1804         code.putln("/*--- Threads initialization code ---*/")
1805         code.putln("#if defined(__PYX_FORCE_INIT_THREADS) && __PYX_FORCE_INIT_THREADS")
1806         code.putln("#ifdef WITH_THREAD /* Python build with threading support? */")
1807         code.putln("PyEval_InitThreads();")
1808         code.putln("#endif")
1809         code.putln("#endif")
1810
1811         code.putln("/*--- Module creation code ---*/")
1812         self.generate_module_creation_code(env, code)
1813
1814         code.putln("/*--- Initialize various global constants etc. ---*/")
1815         code.putln(code.error_goto_if_neg("__Pyx_InitGlobals()", self.pos))
1816
1817         __main__name = code.globalstate.get_py_string_const(
1818             EncodedString("__main__"), identifier=True)
1819         code.putln("if (%s%s) {" % (Naming.module_is_main, self.full_module_name.replace('.', '__')))
1820         code.putln(
1821             'if (__Pyx_SetAttrString(%s, "__name__", %s) < 0) %s;' % (
1822                 env.module_cname,
1823                 __main__name.cname,
1824                 code.error_goto(self.pos)))
1825         code.putln("}")
1826
1827         if Options.cache_builtins:
1828             code.putln("/*--- Builtin init code ---*/")
1829             code.putln(code.error_goto_if_neg("__Pyx_InitCachedBuiltins()", self.pos))
1830
1831         code.putln("/*--- Constants init code ---*/")
1832         code.putln(code.error_goto_if_neg("__Pyx_InitCachedConstants()", self.pos))
1833
1834         code.putln("/*--- Global init code ---*/")
1835         self.generate_global_init_code(env, code)
1836
1837         code.putln("/*--- Variable export code ---*/")
1838         self.generate_c_variable_export_code(env, code)
1839
1840         code.putln("/*--- Function export code ---*/")
1841         self.generate_c_function_export_code(env, code)
1842
1843         code.putln("/*--- Type init code ---*/")
1844         self.generate_type_init_code(env, code)
1845
1846         code.putln("/*--- Type import code ---*/")
1847         for module in imported_modules:
1848             self.generate_type_import_code_for_module(module, env, code)
1849
1850         code.putln("/*--- Function import code ---*/")
1851         for module in imported_modules:
1852             self.generate_c_function_import_code_for_module(module, env, code)
1853
1854         code.putln("/*--- Execution code ---*/")
1855         code.mark_pos(None)
1856
1857         self.body.generate_execution_code(code)
1858
1859         if Options.generate_cleanup_code:
1860             # this should be replaced by the module's tp_clear in Py3
1861             env.use_utility_code(import_module_utility_code)
1862             code.putln("if (__Pyx_RegisterCleanup()) %s;" % code.error_goto(self.pos))
1863
1864         code.put_goto(code.return_label)
1865         code.put_label(code.error_label)
1866         for cname, type in code.funcstate.all_managed_temps():
1867             code.put_xdecref(cname, type)
1868         code.putln('if (%s) {' % env.module_cname)
1869         code.putln('__Pyx_AddTraceback("init %s");' % env.qualified_name)
1870         env.use_utility_code(Nodes.traceback_utility_code)
1871         code.put_decref_clear(env.module_cname, py_object_type, nanny=False)
1872         code.putln('} else if (!PyErr_Occurred()) {')
1873         code.putln('PyErr_SetString(PyExc_ImportError, "init %s");' % env.qualified_name)
1874         code.putln('}')
1875         code.put_label(code.return_label)
1876
1877         code.put_finish_refcount_context()
1878
1879         code.putln("#if PY_MAJOR_VERSION < 3")
1880         code.putln("return;")
1881         code.putln("#else")
1882         code.putln("return %s;" % env.module_cname)
1883         code.putln("#endif")
1884         code.putln('}')
1885
1886         tempdecl_code.put_temp_declarations(code.funcstate)
1887
1888         code.exit_cfunc_scope()
1889
1890     def generate_module_cleanup_func(self, env, code):
1891         if not Options.generate_cleanup_code:
1892             return
1893         code.globalstate.use_utility_code(register_cleanup_utility_code)
1894         code.putln('static PyObject *%s(CYTHON_UNUSED PyObject *self, CYTHON_UNUSED PyObject *unused) {' %
1895                    Naming.cleanup_cname)
1896         if Options.generate_cleanup_code >= 2:
1897             code.putln("/*--- Global cleanup code ---*/")
1898             rev_entries = list(env.var_entries)
1899             rev_entries.reverse()
1900             for entry in rev_entries:
1901                 if entry.visibility != 'extern':
1902                     if entry.type.is_pyobject and entry.used:
1903                         code.putln("Py_DECREF(%s); %s = 0;" % (
1904                             code.entry_as_pyobject(entry), entry.cname))
1905         code.putln("__Pyx_CleanupGlobals();")
1906         if Options.generate_cleanup_code >= 3:
1907             code.putln("/*--- Type import cleanup code ---*/")
1908             for type, _ in env.types_imported.items():
1909                 code.putln("Py_DECREF((PyObject *)%s);" % type.typeptr_cname)
1910         if Options.cache_builtins:
1911             code.putln("/*--- Builtin cleanup code ---*/")
1912             for entry in env.cached_builtins:
1913                 code.put_decref_clear(entry.cname,
1914                                       PyrexTypes.py_object_type,
1915                                       nanny=False)
1916         code.putln("/*--- Intern cleanup code ---*/")
1917         code.put_decref_clear(Naming.empty_tuple,
1918                               PyrexTypes.py_object_type,
1919                               nanny=False)
1920 #        for entry in env.pynum_entries:
1921 #            code.put_decref_clear(entry.cname,
1922 #                                  PyrexTypes.py_object_type,
1923 #                                  nanny=False)
1924 #        for entry in env.all_pystring_entries:
1925 #            if entry.is_interned:
1926 #                code.put_decref_clear(entry.pystring_cname,
1927 #                                      PyrexTypes.py_object_type,
1928 #                                      nanny=False)
1929 #        for entry in env.default_entries:
1930 #            if entry.type.is_pyobject and entry.used:
1931 #                code.putln("Py_DECREF(%s); %s = 0;" % (
1932 #                    code.entry_as_pyobject(entry), entry.cname))
1933         code.putln("Py_INCREF(Py_None); return Py_None;")
1934
1935     def generate_main_method(self, env, code):
1936         module_is_main = "%s%s" % (Naming.module_is_main, self.full_module_name.replace('.', '__'))
1937         if Options.embed == "main":
1938             wmain = "wmain"
1939         else:
1940             wmain = Options.embed
1941         code.globalstate.use_utility_code(
1942             main_method.specialize(
1943                 module_name = env.module_name,
1944                 module_is_main = module_is_main,
1945                 main_method = Options.embed,
1946                 wmain_method = wmain))
1947
1948     def generate_pymoduledef_struct(self, env, code):
1949         if env.doc:
1950             doc = "__Pyx_DOCSTR(%s)" % code.get_string_const(env.doc)
1951         else:
1952             doc = "0"
1953         code.putln("")
1954         code.putln("#if PY_MAJOR_VERSION >= 3")
1955         code.putln("static struct PyModuleDef %s = {" % Naming.pymoduledef_cname)
1956         code.putln("  PyModuleDef_HEAD_INIT,")
1957         code.putln('  __Pyx_NAMESTR("%s"),' % env.module_name)
1958         code.putln("  %s, /* m_doc */" % doc)
1959         code.putln("  -1, /* m_size */")
1960         code.putln("  %s /* m_methods */," % env.method_table_cname)
1961         code.putln("  NULL, /* m_reload */")
1962         code.putln("  NULL, /* m_traverse */")
1963         code.putln("  NULL, /* m_clear */")
1964         code.putln("  NULL /* m_free */")
1965         code.putln("};")
1966         code.putln("#endif")
1967
1968     def generate_module_creation_code(self, env, code):
1969         # Generate code to create the module object and
1970         # install the builtins.
1971         if env.doc:
1972             doc = "__Pyx_DOCSTR(%s)" % code.get_string_const(env.doc)
1973         else:
1974             doc = "0"
1975         code.putln("#if PY_MAJOR_VERSION < 3")
1976         code.putln(
1977             '%s = Py_InitModule4(__Pyx_NAMESTR("%s"), %s, %s, 0, PYTHON_API_VERSION);' % (
1978                 env.module_cname,
1979                 env.module_name,
1980                 env.method_table_cname,
1981                 doc))
1982         code.putln("#else")
1983         code.putln(
1984             "%s = PyModule_Create(&%s);" % (
1985                 env.module_cname,
1986                 Naming.pymoduledef_cname))
1987         code.putln("#endif")
1988         code.putln(
1989             "if (!%s) %s;" % (
1990                 env.module_cname,
1991                 code.error_goto(self.pos)));
1992         code.putln("#if PY_MAJOR_VERSION < 3")
1993         code.putln(
1994             "Py_INCREF(%s);" %
1995                 env.module_cname)
1996         code.putln("#endif")
1997         code.putln(
1998             '%s = PyImport_AddModule(__Pyx_NAMESTR(__Pyx_BUILTIN_MODULE_NAME));' %
1999                 Naming.builtins_cname)
2000         code.putln(
2001             "if (!%s) %s;" % (
2002                 Naming.builtins_cname,
2003                 code.error_goto(self.pos)));
2004         code.putln(
2005             'if (__Pyx_SetAttrString(%s, "__builtins__", %s) < 0) %s;' % (
2006                 env.module_cname,
2007                 Naming.builtins_cname,
2008                 code.error_goto(self.pos)))
2009         if Options.pre_import is not None:
2010             code.putln(
2011                 '%s = PyImport_AddModule(__Pyx_NAMESTR("%s"));' % (
2012                     Naming.preimport_cname,
2013                     Options.pre_import))
2014             code.putln(
2015                 "if (!%s) %s;" % (
2016                     Naming.preimport_cname,
2017                     code.error_goto(self.pos)));
2018
2019     def generate_global_init_code(self, env, code):
2020         # Generate code to initialise global PyObject *
2021         # variables to None.
2022         for entry in env.var_entries:
2023             if entry.visibility != 'extern':
2024                 if entry.type.is_pyobject and entry.used:
2025                     code.put_init_var_to_py_none(entry, nanny=False)
2026
2027     def generate_c_variable_export_code(self, env, code):
2028         # Generate code to create PyCFunction wrappers for exported C functions.
2029         for entry in env.var_entries:
2030             if entry.api or entry.defined_in_pxd:
2031                 env.use_utility_code(voidptr_export_utility_code)
2032                 signature = entry.type.declaration_code("")
2033                 code.putln('if (__Pyx_ExportVoidPtr("%s", (void *)&%s, "%s") < 0) %s' % (
2034                     entry.name,
2035                     entry.cname,
2036                     signature,
2037                     code.error_goto(self.pos)))
2038
2039     def generate_c_function_export_code(self, env, code):
2040         # Generate code to create PyCFunction wrappers for exported C functions.
2041         for entry in env.cfunc_entries:
2042             if entry.api or entry.defined_in_pxd:
2043                 env.use_utility_code(function_export_utility_code)
2044                 signature = entry.type.signature_string()
2045                 code.putln('if (__Pyx_ExportFunction("%s", (void (*)(void))%s, "%s") < 0) %s' % (
2046                     entry.name,
2047                     entry.cname,
2048                     signature,
2049                     code.error_goto(self.pos)))
2050
2051     def generate_type_import_code_for_module(self, module, env, code):
2052         # Generate type import code for all exported extension types in
2053         # an imported module.
2054         #if module.c_class_entries:
2055         for entry in module.c_class_entries:
2056             if entry.defined_in_pxd:
2057                 self.generate_type_import_code(env, entry.type, entry.pos, code)
2058
2059     def generate_c_function_import_code_for_module(self, module, env, code):
2060         # Generate import code for all exported C functions in a cimported module.
2061         entries = []
2062         for entry in module.cfunc_entries:
2063             if entry.defined_in_pxd:
2064                 entries.append(entry)
2065         if entries:
2066             env.use_utility_code(import_module_utility_code)
2067             env.use_utility_code(function_import_utility_code)
2068             temp = code.funcstate.allocate_temp(py_object_type, manage_ref=True)
2069             code.putln(
2070                 '%s = __Pyx_ImportModule("%s"); if (!%s) %s' % (
2071                     temp,
2072                     module.qualified_name,
2073                     temp,
2074                     code.error_goto(self.pos)))
2075             for entry in entries:
2076                 code.putln(
2077                     'if (__Pyx_ImportFunction(%s, "%s", (void (**)(void))&%s, "%s") < 0) %s' % (
2078                         temp,
2079                         entry.name,
2080                         entry.cname,
2081                         entry.type.signature_string(),
2082                         code.error_goto(self.pos)))
2083             code.putln("Py_DECREF(%s); %s = 0;" % (temp, temp))
2084
2085     def generate_type_init_code(self, env, code):
2086         # Generate type import code for extern extension types
2087         # and type ready code for non-extern ones.
2088         for entry in env.c_class_entries:
2089             if entry.visibility == 'extern':
2090                 self.generate_type_import_code(env, entry.type, entry.pos, code)
2091             else:
2092                 self.generate_base_type_import_code(env, entry, code)
2093                 self.generate_exttype_vtable_init_code(entry, code)
2094                 self.generate_type_ready_code(env, entry, code)
2095                 self.generate_typeptr_assignment_code(entry, code)
2096
2097     def generate_base_type_import_code(self, env, entry, code):
2098         base_type = entry.type.base_type
2099         if base_type and base_type.module_name != env.qualified_name \
2100                and not base_type.is_builtin_type:
2101             self.generate_type_import_code(env, base_type, self.pos, code)
2102
2103     def use_type_import_utility_code(self, env):
2104         env.use_utility_code(type_import_utility_code)
2105         env.use_utility_code(import_module_utility_code)
2106
2107     def generate_type_import_code(self, env, type, pos, code):
2108         # If not already done, generate code to import the typeobject of an
2109         # extension type defined in another module, and extract its C method
2110         # table pointer if any.
2111         if type in env.types_imported:
2112             return
2113         if type.typedef_flag:
2114             objstruct = type.objstruct_cname
2115         else:
2116             objstruct = "struct %s" % type.objstruct_cname
2117         self.generate_type_import_call(type, code,
2118                                        code.error_goto_if_null(type.typeptr_cname, pos))
2119         self.use_type_import_utility_code(env)
2120         if type.vtabptr_cname:
2121             env.use_utility_code(Nodes.get_vtable_utility_code)
2122             code.putln("%s = (struct %s*)__Pyx_GetVtable(%s->tp_dict); %s" % (
2123                 type.vtabptr_cname,
2124                 type.vtabstruct_cname,
2125                 type.typeptr_cname,
2126                 code.error_goto_if_null(type.vtabptr_cname, pos)))
2127         env.types_imported[type] = 1
2128
2129     py3_type_name_map = {'str' : 'bytes', 'unicode' : 'str'}
2130
2131     def generate_type_import_call(self, type, code, error_code):
2132         if type.typedef_flag:
2133             objstruct = type.objstruct_cname
2134         else:
2135             objstruct = "struct %s" % type.objstruct_cname
2136         module_name = type.module_name
2137         condition = None
2138         if module_name not in ('__builtin__', 'builtins'):
2139             module_name = '"%s"' % module_name
2140         else:
2141             module_name = '__Pyx_BUILTIN_MODULE_NAME'
2142             if type.name in Code.non_portable_builtins_map:
2143                 condition, replacement = Code.non_portable_builtins_map[entry.name]
2144                 code.putln("#if %s" % condition)
2145                 code.putln('%s = __Pyx_ImportType(%s, "%s", sizeof(%s), 1); %s' % (
2146                         type.typeptr_cname,
2147                         module_name,
2148                         replacement,
2149                         objstruct,
2150                         error_code))
2151                 code.putln("#else")
2152         code.putln('%s = __Pyx_ImportType(%s, "%s", sizeof(%s), %i); %s' % (
2153                 type.typeptr_cname,
2154                 module_name,
2155                 type.name,
2156                 objstruct,
2157                 not type.is_external or type.is_subclassed,
2158                 error_code))
2159         if condition:
2160             code.putln("#endif")
2161
2162     def generate_type_ready_code(self, env, entry, code):
2163         # Generate a call to PyType_Ready for an extension
2164         # type defined in this module.
2165         type = entry.type
2166         typeobj_cname = type.typeobj_cname
2167         scope = type.scope
2168         if scope: # could be None if there was an error
2169             if entry.visibility != 'extern':
2170                 for slot in TypeSlots.slot_table:
2171                     slot.generate_dynamic_init_code(scope, code)
2172                 code.putln(
2173                     "if (PyType_Ready(&%s) < 0) %s" % (
2174                         typeobj_cname,
2175                         code.error_goto(entry.pos)))
2176                 # Fix special method docstrings. This is a bit of a hack, but
2177                 # unless we let PyType_Ready create the slot wrappers we have
2178                 # a significant performance hit. (See trac #561.)
2179                 for func in entry.type.scope.pyfunc_entries:
2180                     if func.is_special and Options.docstrings and func.wrapperbase_cname:
2181                         code.putln("{")
2182                         code.putln(
2183                             'PyObject *wrapper = __Pyx_GetAttrString((PyObject *)&%s, "%s"); %s' % (
2184                                 typeobj_cname,
2185                                 func.name,
2186                                 code.error_goto_if_null('wrapper', entry.pos)))
2187                         code.putln(
2188                             "if (Py_TYPE(wrapper) == &PyWrapperDescr_Type) {")
2189                         code.putln(
2190                             "%s = *((PyWrapperDescrObject *)wrapper)->d_base;" % (
2191                                 func.wrapperbase_cname))
2192                         code.putln(
2193                             "%s.doc = %s;" % (func.wrapperbase_cname, func.doc_cname))
2194                         code.putln(
2195                             "((PyWrapperDescrObject *)wrapper)->d_base = &%s;" % (
2196                                 func.wrapperbase_cname))
2197                         code.putln("}")
2198                         code.putln("}")
2199                 if type.vtable_cname:
2200                     code.putln(
2201                         "if (__Pyx_SetVtable(%s.tp_dict, %s) < 0) %s" % (
2202                             typeobj_cname,
2203                             type.vtabptr_cname,
2204                             code.error_goto(entry.pos)))
2205                     env.use_utility_code(Nodes.set_vtable_utility_code)
2206                 if not type.scope.is_internal and not type.scope.directives['internal']:
2207                     # scope.is_internal is set for types defined by
2208                     # Cython (such as closures), the 'internal'
2209                     # directive is set by users
2210                     code.putln(
2211                         'if (__Pyx_SetAttrString(%s, "%s", (PyObject *)&%s) < 0) %s' % (
2212                             Naming.module_cname,
2213                             scope.class_name,
2214                             typeobj_cname,
2215                             code.error_goto(entry.pos)))
2216                 weakref_entry = scope.lookup_here("__weakref__")
2217                 if weakref_entry:
2218                     if weakref_entry.type is py_object_type:
2219                         tp_weaklistoffset = "%s.tp_weaklistoffset" % typeobj_cname
2220                         if type.typedef_flag:
2221                             objstruct = type.objstruct_cname
2222                         else:
2223                             objstruct = "struct %s" % type.objstruct_cname
2224                         code.putln("if (%s == 0) %s = offsetof(%s, %s);" % (
2225                             tp_weaklistoffset,
2226                             tp_weaklistoffset,
2227                             objstruct,
2228                             weakref_entry.cname))
2229                     else:
2230                         error(weakref_entry.pos, "__weakref__ slot must be of type 'object'")
2231
2232     def generate_exttype_vtable_init_code(self, entry, code):
2233         # Generate code to initialise the C method table of an
2234         # extension type.
2235         type = entry.type
2236         if type.vtable_cname:
2237             code.putln(
2238                 "%s = &%s;" % (
2239                     type.vtabptr_cname,
2240                     type.vtable_cname))
2241             if type.base_type and type.base_type.vtabptr_cname:
2242                 code.putln(
2243                     "%s.%s = *%s;" % (
2244                         type.vtable_cname,
2245                         Naming.obj_base_cname,
2246                         type.base_type.vtabptr_cname))
2247
2248             c_method_entries = [
2249                 entry for entry in type.scope.cfunc_entries
2250                 if entry.func_cname ]
2251             if c_method_entries:
2252                 for meth_entry in c_method_entries:
2253                     cast = meth_entry.type.signature_cast_string()
2254                     code.putln(
2255                         "%s.%s = %s%s;" % (
2256                             type.vtable_cname,
2257                             meth_entry.cname,
2258                             cast,
2259                             meth_entry.func_cname))
2260
2261     def generate_typeptr_assignment_code(self, entry, code):
2262         # Generate code to initialise the typeptr of an extension
2263         # type defined in this module to point to its type object.
2264         type = entry.type
2265         if type.typeobj_cname:
2266             code.putln(
2267                 "%s = &%s;" % (
2268                     type.typeptr_cname, type.typeobj_cname))
2269
2270 #------------------------------------------------------------------------------------
2271 #
2272 #  Runtime support code
2273 #
2274 #------------------------------------------------------------------------------------
2275
2276 streq_utility_code = UtilityCode(
2277 proto = """
2278 static CYTHON_INLINE int __Pyx_StrEq(const char *, const char *); /*proto*/
2279 """,
2280 impl = """
2281 static CYTHON_INLINE int __Pyx_StrEq(const char *s1, const char *s2) {
2282      while (*s1 != '\\0' && *s1 == *s2) { s1++; s2++; }
2283      return *s1 == *s2;
2284 }
2285 """)
2286
2287 #------------------------------------------------------------------------------------
2288
2289 import_module_utility_code = UtilityCode(
2290 proto = """
2291 static PyObject *__Pyx_ImportModule(const char *name); /*proto*/
2292 """,
2293 impl = """
2294 #ifndef __PYX_HAVE_RT_ImportModule
2295 #define __PYX_HAVE_RT_ImportModule
2296 static PyObject *__Pyx_ImportModule(const char *name) {
2297     PyObject *py_name = 0;
2298     PyObject *py_module = 0;
2299
2300     #if PY_MAJOR_VERSION < 3
2301     py_name = PyString_FromString(name);
2302     #else
2303     py_name = PyUnicode_FromString(name);
2304     #endif
2305     if (!py_name)
2306         goto bad;
2307     py_module = PyImport_Import(py_name);
2308     Py_DECREF(py_name);
2309     return py_module;
2310 bad:
2311     Py_XDECREF(py_name);
2312     return 0;
2313 }
2314 #endif
2315 """)
2316
2317 #------------------------------------------------------------------------------------
2318
2319 type_import_utility_code = UtilityCode(
2320 proto = """
2321 static PyTypeObject *__Pyx_ImportType(const char *module_name, const char *class_name, long size, int strict);  /*proto*/
2322 """,
2323 impl = """
2324 #ifndef __PYX_HAVE_RT_ImportType
2325 #define __PYX_HAVE_RT_ImportType
2326 static PyTypeObject *__Pyx_ImportType(const char *module_name, const char *class_name,
2327     long size, int strict)
2328 {
2329     PyObject *py_module = 0;
2330     PyObject *result = 0;
2331     PyObject *py_name = 0;
2332     char warning[200];
2333
2334     py_module = __Pyx_ImportModule(module_name);
2335     if (!py_module)
2336         goto bad;
2337     #if PY_MAJOR_VERSION < 3
2338     py_name = PyString_FromString(class_name);
2339     #else
2340     py_name = PyUnicode_FromString(class_name);
2341     #endif
2342     if (!py_name)
2343         goto bad;
2344     result = PyObject_GetAttr(py_module, py_name);
2345     Py_DECREF(py_name);
2346     py_name = 0;
2347     Py_DECREF(py_module);
2348     py_module = 0;
2349     if (!result)
2350         goto bad;
2351     if (!PyType_Check(result)) {
2352         PyErr_Format(PyExc_TypeError,
2353             "%s.%s is not a type object",
2354             module_name, class_name);
2355         goto bad;
2356     }
2357     if (!strict && ((PyTypeObject *)result)->tp_basicsize > size) {
2358         PyOS_snprintf(warning, sizeof(warning),
2359             "%s.%s size changed, may indicate binary incompatibility",
2360             module_name, class_name);
2361         #if PY_VERSION_HEX < 0x02050000
2362         PyErr_Warn(NULL, warning);
2363         #else
2364         PyErr_WarnEx(NULL, warning, 0);
2365         #endif
2366     }
2367     else if (((PyTypeObject *)result)->tp_basicsize != size) {
2368         PyErr_Format(PyExc_ValueError,
2369             "%s.%s has the wrong size, try recompiling",
2370             module_name, class_name);
2371         goto bad;
2372     }
2373     return (PyTypeObject *)result;
2374 bad:
2375     Py_XDECREF(py_module);
2376     Py_XDECREF(result);
2377     return 0;
2378 }
2379 #endif
2380 """)
2381
2382 #------------------------------------------------------------------------------------
2383
2384 voidptr_export_utility_code = UtilityCode(
2385 proto = """
2386 static int __Pyx_ExportVoidPtr(const char *name, void *p, const char *sig); /*proto*/
2387 """,
2388 impl = r"""
2389 static int __Pyx_ExportVoidPtr(const char *name, void *p, const char *sig) {
2390     PyObject *d = 0;
2391     PyObject *cobj = 0;
2392
2393     d = PyObject_GetAttrString(%(MODULE)s, (char *)"%(API)s");
2394     if (!d) {
2395         PyErr_Clear();
2396         d = PyDict_New();
2397         if (!d)
2398             goto bad;
2399         Py_INCREF(d);
2400         if (PyModule_AddObject(%(MODULE)s, (char *)"%(API)s", d) < 0)
2401             goto bad;
2402     }
2403 #if PY_VERSION_HEX >= 0x02070000 && !(PY_MAJOR_VERSION==3&&PY_MINOR_VERSION==0)
2404     cobj = PyCapsule_New(p, sig, 0);
2405 #else
2406     cobj = PyCObject_FromVoidPtrAndDesc(p, (void *)sig, 0);
2407 #endif
2408     if (!cobj)
2409         goto bad;
2410     if (PyDict_SetItemString(d, name, cobj) < 0)
2411         goto bad;
2412     Py_DECREF(cobj);
2413     Py_DECREF(d);
2414     return 0;
2415 bad:
2416     Py_XDECREF(cobj);
2417     Py_XDECREF(d);
2418     return -1;
2419 }
2420 """ % {'MODULE': Naming.module_cname, 'API': Naming.api_name}
2421 )
2422
2423 function_export_utility_code = UtilityCode(
2424 proto = """
2425 static int __Pyx_ExportFunction(const char *name, void (*f)(void), const char *sig); /*proto*/
2426 """,
2427 impl = r"""
2428 static int __Pyx_ExportFunction(const char *name, void (*f)(void), const char *sig) {
2429     PyObject *d = 0;
2430     PyObject *cobj = 0;
2431     union {
2432         void (*fp)(void);
2433         void *p;
2434     } tmp;
2435
2436     d = PyObject_GetAttrString(%(MODULE)s, (char *)"%(API)s");
2437     if (!d) {
2438         PyErr_Clear();
2439         d = PyDict_New();
2440         if (!d)
2441             goto bad;
2442         Py_INCREF(d);
2443         if (PyModule_AddObject(%(MODULE)s, (char *)"%(API)s", d) < 0)
2444             goto bad;
2445     }
2446     tmp.fp = f;
2447 #if PY_VERSION_HEX >= 0x02070000 && !(PY_MAJOR_VERSION==3&&PY_MINOR_VERSION==0)
2448     cobj = PyCapsule_New(tmp.p, sig, 0);
2449 #else
2450     cobj = PyCObject_FromVoidPtrAndDesc(tmp.p, (void *)sig, 0);
2451 #endif
2452     if (!cobj)
2453         goto bad;
2454     if (PyDict_SetItemString(d, name, cobj) < 0)
2455         goto bad;
2456     Py_DECREF(cobj);
2457     Py_DECREF(d);
2458     return 0;
2459 bad:
2460     Py_XDECREF(cobj);
2461     Py_XDECREF(d);
2462     return -1;
2463 }
2464 """ % {'MODULE': Naming.module_cname, 'API': Naming.api_name}
2465 )
2466
2467 voidptr_import_utility_code = UtilityCode(
2468 proto = """
2469 static int __Pyx_ImportVoidPtr(PyObject *module, const char *name, void **p, const char *sig); /*proto*/
2470 """,
2471 impl = """
2472 #ifndef __PYX_HAVE_RT_ImportVoidPtr
2473 #define __PYX_HAVE_RT_ImportVoidPtr
2474 static int __Pyx_ImportVoidPtr(PyObject *module, const char *name, void **p, const char *sig) {
2475     PyObject *d = 0;
2476     PyObject *cobj = 0;
2477
2478     d = PyObject_GetAttrString(module, (char *)"%(API)s");
2479     if (!d)
2480         goto bad;
2481     cobj = PyDict_GetItemString(d, name);
2482     if (!cobj) {
2483         PyErr_Format(PyExc_ImportError,
2484             "%%s does not export expected C variable %%s",
2485                 PyModule_GetName(module), name);
2486         goto bad;
2487     }
2488 #if PY_VERSION_HEX >= 0x02070000 && !(PY_MAJOR_VERSION==3&&PY_MINOR_VERSION==0)
2489     if (!PyCapsule_IsValid(cobj, sig)) {
2490         PyErr_Format(PyExc_TypeError,
2491             "C variable %%s.%%s has wrong signature (expected %%s, got %%s)",
2492              PyModule_GetName(module), name, sig, PyCapsule_GetName(cobj));
2493         goto bad;
2494     }
2495     *p = PyCapsule_GetPointer(cobj, sig);
2496 #else
2497     {const char *desc, *s1, *s2;
2498     desc = (const char *)PyCObject_GetDesc(cobj);
2499     if (!desc)
2500         goto bad;
2501     s1 = desc; s2 = sig;
2502     while (*s1 != '\\0' && *s1 == *s2) { s1++; s2++; }
2503     if (*s1 != *s2) {
2504         PyErr_Format(PyExc_TypeError,
2505             "C variable %%s.%%s has wrong signature (expected %%s, got %%s)",
2506              PyModule_GetName(module), name, sig, desc);
2507         goto bad;
2508     }
2509     *p = PyCObject_AsVoidPtr(cobj);}
2510 #endif
2511     if (!(*p))
2512         goto bad;
2513     Py_DECREF(d);
2514     return 0;
2515 bad:
2516     Py_XDECREF(d);
2517     return -1;
2518 }
2519 #endif
2520 """ % dict(API = Naming.api_name)
2521 )
2522
2523 function_import_utility_code = UtilityCode(
2524 proto = """
2525 static int __Pyx_ImportFunction(PyObject *module, const char *funcname, void (**f)(void), const char *sig); /*proto*/
2526 """,
2527 impl = """
2528 #ifndef __PYX_HAVE_RT_ImportFunction
2529 #define __PYX_HAVE_RT_ImportFunction
2530 static int __Pyx_ImportFunction(PyObject *module, const char *funcname, void (**f)(void), const char *sig) {
2531     PyObject *d = 0;
2532     PyObject *cobj = 0;
2533     union {
2534         void (*fp)(void);
2535         void *p;
2536     } tmp;
2537
2538     d = PyObject_GetAttrString(module, (char *)"%(API)s");
2539     if (!d)
2540         goto bad;
2541     cobj = PyDict_GetItemString(d, funcname);
2542     if (!cobj) {
2543         PyErr_Format(PyExc_ImportError,
2544             "%%s does not export expected C function %%s",
2545                 PyModule_GetName(module), funcname);
2546         goto bad;
2547     }
2548 #if PY_VERSION_HEX >= 0x02070000 && !(PY_MAJOR_VERSION==3&&PY_MINOR_VERSION==0)
2549     if (!PyCapsule_IsValid(cobj, sig)) {
2550         PyErr_Format(PyExc_TypeError,
2551             "C function %%s.%%s has wrong signature (expected %%s, got %%s)",
2552              PyModule_GetName(module), funcname, sig, PyCapsule_GetName(cobj));
2553         goto bad;
2554     }
2555     tmp.p = PyCapsule_GetPointer(cobj, sig);
2556 #else
2557     {const char *desc, *s1, *s2;
2558     desc = (const char *)PyCObject_GetDesc(cobj);
2559     if (!desc)
2560         goto bad;
2561     s1 = desc; s2 = sig;
2562     while (*s1 != '\\0' && *s1 == *s2) { s1++; s2++; }
2563     if (*s1 != *s2) {
2564         PyErr_Format(PyExc_TypeError,
2565             "C function %%s.%%s has wrong signature (expected %%s, got %%s)",
2566              PyModule_GetName(module), funcname, sig, desc);
2567         goto bad;
2568     }
2569     tmp.p = PyCObject_AsVoidPtr(cobj);}
2570 #endif
2571     *f = tmp.fp;
2572     if (!(*f))
2573         goto bad;
2574     Py_DECREF(d);
2575     return 0;
2576 bad:
2577     Py_XDECREF(d);
2578     return -1;
2579 }
2580 #endif
2581 """ % dict(API = Naming.api_name)
2582 )
2583
2584 #------------------------------------------------------------------------------------
2585
2586 register_cleanup_utility_code = UtilityCode(
2587 proto = """
2588 static int __Pyx_RegisterCleanup(void); /*proto*/
2589 static PyObject* %(module_cleanup)s(CYTHON_UNUSED PyObject *self, CYTHON_UNUSED PyObject *unused); /*proto*/
2590 static PyMethodDef cleanup_def = {__Pyx_NAMESTR("__cleanup"), (PyCFunction)&%(module_cleanup)s, METH_NOARGS, 0};
2591 """ % {'module_cleanup': Naming.cleanup_cname},
2592 impl = """
2593 static int __Pyx_RegisterCleanup(void) {
2594     /* Don't use Py_AtExit because that has a 32-call limit
2595      * and is called after python finalization.
2596      */
2597
2598     PyObject *cleanup_func = 0;
2599     PyObject *atexit = 0;
2600     PyObject *reg = 0;
2601     PyObject *args = 0;
2602     PyObject *res = 0;
2603     int ret = -1;
2604
2605     cleanup_func = PyCFunction_New(&cleanup_def, 0);
2606     args = PyTuple_New(1);
2607     if (!cleanup_func || !args)
2608         goto bad;
2609     PyTuple_SET_ITEM(args, 0, cleanup_func);
2610     cleanup_func = 0;
2611
2612     atexit = __Pyx_ImportModule("atexit");
2613     if (!atexit)
2614         goto bad;
2615     reg = __Pyx_GetAttrString(atexit, "register");
2616     if (!reg)
2617         goto bad;
2618     res = PyObject_CallObject(reg, args);
2619     if (!res)
2620         goto bad;
2621     ret = 0;
2622 bad:
2623     Py_XDECREF(cleanup_func);
2624     Py_XDECREF(atexit);
2625     Py_XDECREF(reg);
2626     Py_XDECREF(args);
2627     Py_XDECREF(res);
2628     return ret;
2629 }
2630 """)
2631
2632 import_star_utility_code = """
2633
2634 /* import_all_from is an unexposed function from ceval.c */
2635
2636 static int
2637 __Pyx_import_all_from(PyObject *locals, PyObject *v)
2638 {
2639     PyObject *all = __Pyx_GetAttrString(v, "__all__");
2640     PyObject *dict, *name, *value;
2641     int skip_leading_underscores = 0;
2642     int pos, err;
2643
2644     if (all == NULL) {
2645         if (!PyErr_ExceptionMatches(PyExc_AttributeError))
2646             return -1; /* Unexpected error */
2647         PyErr_Clear();
2648         dict = __Pyx_GetAttrString(v, "__dict__");
2649         if (dict == NULL) {
2650             if (!PyErr_ExceptionMatches(PyExc_AttributeError))
2651                 return -1;
2652             PyErr_SetString(PyExc_ImportError,
2653             "from-import-* object has no __dict__ and no __all__");
2654             return -1;
2655         }
2656         all = PyMapping_Keys(dict);
2657         Py_DECREF(dict);
2658         if (all == NULL)
2659             return -1;
2660         skip_leading_underscores = 1;
2661     }
2662
2663     for (pos = 0, err = 0; ; pos++) {
2664         name = PySequence_GetItem(all, pos);
2665         if (name == NULL) {
2666             if (!PyErr_ExceptionMatches(PyExc_IndexError))
2667                 err = -1;
2668             else
2669                 PyErr_Clear();
2670             break;
2671         }
2672         if (skip_leading_underscores &&
2673 #if PY_MAJOR_VERSION < 3
2674             PyString_Check(name) &&
2675             PyString_AS_STRING(name)[0] == '_')
2676 #else
2677             PyUnicode_Check(name) &&
2678             PyUnicode_AS_UNICODE(name)[0] == '_')
2679 #endif
2680         {
2681             Py_DECREF(name);
2682             continue;
2683         }
2684         value = PyObject_GetAttr(v, name);
2685         if (value == NULL)
2686             err = -1;
2687         else if (PyDict_CheckExact(locals))
2688             err = PyDict_SetItem(locals, name, value);
2689         else
2690             err = PyObject_SetItem(locals, name, value);
2691         Py_DECREF(name);
2692         Py_XDECREF(value);
2693         if (err != 0)
2694             break;
2695     }
2696     Py_DECREF(all);
2697     return err;
2698 }
2699
2700
2701 static int %(IMPORT_STAR)s(PyObject* m) {
2702
2703     int i;
2704     int ret = -1;
2705     char* s;
2706     PyObject *locals = 0;
2707     PyObject *list = 0;
2708 #if PY_MAJOR_VERSION >= 3
2709     PyObject *utf8_name = 0;
2710 #endif
2711     PyObject *name;
2712     PyObject *item;
2713
2714     locals = PyDict_New();              if (!locals) goto bad;
2715     if (__Pyx_import_all_from(locals, m) < 0) goto bad;
2716     list = PyDict_Items(locals);        if (!list) goto bad;
2717
2718     for(i=0; i<PyList_GET_SIZE(list); i++) {
2719         name = PyTuple_GET_ITEM(PyList_GET_ITEM(list, i), 0);
2720         item = PyTuple_GET_ITEM(PyList_GET_ITEM(list, i), 1);
2721 #if PY_MAJOR_VERSION >= 3
2722         utf8_name = PyUnicode_AsUTF8String(name);
2723         if (!utf8_name) goto bad;
2724         s = PyBytes_AS_STRING(utf8_name);
2725         if (%(IMPORT_STAR_SET)s(item, name, s) < 0) goto bad;
2726         Py_DECREF(utf8_name); utf8_name = 0;
2727 #else
2728         s = PyString_AsString(name);
2729         if (!s) goto bad;
2730         if (%(IMPORT_STAR_SET)s(item, name, s) < 0) goto bad;
2731 #endif
2732     }
2733     ret = 0;
2734
2735 bad:
2736     Py_XDECREF(locals);
2737     Py_XDECREF(list);
2738 #if PY_MAJOR_VERSION >= 3
2739     Py_XDECREF(utf8_name);
2740 #endif
2741     return ret;
2742 }
2743 """ % {'IMPORT_STAR'     : Naming.import_star,
2744        'IMPORT_STAR_SET' : Naming.import_star_set }
2745
2746 refnanny_utility_code = UtilityCode(
2747 proto="""
2748 #ifndef CYTHON_REFNANNY
2749   #define CYTHON_REFNANNY 0
2750 #endif
2751
2752 #if CYTHON_REFNANNY
2753   typedef struct {
2754     void (*INCREF)(void*, PyObject*, int);
2755     void (*DECREF)(void*, PyObject*, int);
2756     void (*GOTREF)(void*, PyObject*, int);
2757     void (*GIVEREF)(void*, PyObject*, int);
2758     void* (*SetupContext)(const char*, int, const char*);
2759     void (*FinishContext)(void**);
2760   } __Pyx_RefNannyAPIStruct;
2761   static __Pyx_RefNannyAPIStruct *__Pyx_RefNanny = NULL;
2762   static __Pyx_RefNannyAPIStruct *__Pyx_RefNannyImportAPI(const char *modname); /*proto*/
2763   #define __Pyx_RefNannyDeclarations void *__pyx_refnanny = NULL;
2764   #define __Pyx_RefNannySetupContext(name) \
2765           __pyx_refnanny = __Pyx_RefNanny->SetupContext((name), __LINE__, __FILE__)
2766   #define __Pyx_RefNannyFinishContext() \
2767           __Pyx_RefNanny->FinishContext(&__pyx_refnanny)
2768   #define __Pyx_INCREF(r)  __Pyx_RefNanny->INCREF(__pyx_refnanny, (PyObject *)(r), __LINE__)
2769   #define __Pyx_DECREF(r)  __Pyx_RefNanny->DECREF(__pyx_refnanny, (PyObject *)(r), __LINE__)
2770   #define __Pyx_GOTREF(r)  __Pyx_RefNanny->GOTREF(__pyx_refnanny, (PyObject *)(r), __LINE__)
2771   #define __Pyx_GIVEREF(r) __Pyx_RefNanny->GIVEREF(__pyx_refnanny, (PyObject *)(r), __LINE__)
2772   #define __Pyx_XINCREF(r)  do { if((r) != NULL) {__Pyx_INCREF(r); }} while(0)
2773   #define __Pyx_XDECREF(r)  do { if((r) != NULL) {__Pyx_DECREF(r); }} while(0)
2774   #define __Pyx_XGOTREF(r)  do { if((r) != NULL) {__Pyx_GOTREF(r); }} while(0)
2775   #define __Pyx_XGIVEREF(r) do { if((r) != NULL) {__Pyx_GIVEREF(r);}} while(0)
2776 #else
2777   #define __Pyx_RefNannyDeclarations
2778   #define __Pyx_RefNannySetupContext(name)
2779   #define __Pyx_RefNannyFinishContext()
2780   #define __Pyx_INCREF(r) Py_INCREF(r)
2781   #define __Pyx_DECREF(r) Py_DECREF(r)
2782   #define __Pyx_GOTREF(r)
2783   #define __Pyx_GIVEREF(r)
2784   #define __Pyx_XINCREF(r) Py_XINCREF(r)
2785   #define __Pyx_XDECREF(r) Py_XDECREF(r)
2786   #define __Pyx_XGOTREF(r)
2787   #define __Pyx_XGIVEREF(r)
2788 #endif /* CYTHON_REFNANNY */
2789 """,
2790 impl="""
2791 #if CYTHON_REFNANNY
2792 static __Pyx_RefNannyAPIStruct *__Pyx_RefNannyImportAPI(const char *modname) {
2793     PyObject *m = NULL, *p = NULL;
2794     void *r = NULL;
2795     m = PyImport_ImportModule((char *)modname);
2796     if (!m) goto end;
2797     p = PyObject_GetAttrString(m, (char *)\"RefNannyAPI\");
2798     if (!p) goto end;
2799     r = PyLong_AsVoidPtr(p);
2800 end:
2801     Py_XDECREF(p);
2802     Py_XDECREF(m);
2803     return (__Pyx_RefNannyAPIStruct *)r;
2804 }
2805 #endif /* CYTHON_REFNANNY */
2806 """,
2807 )
2808
2809 main_method = UtilityCode(
2810 impl = """
2811 #ifdef __FreeBSD__
2812 #include <floatingpoint.h>
2813 #endif
2814
2815 #if PY_MAJOR_VERSION < 3
2816 int %(main_method)s(int argc, char** argv) {
2817 #elif defined(WIN32) || defined(MS_WINDOWS)
2818 int %(wmain_method)s(int argc, wchar_t **argv) {
2819 #else
2820 static int __Pyx_main(int argc, wchar_t **argv) {
2821 #endif
2822     /* 754 requires that FP exceptions run in "no stop" mode by default,
2823      * and until C vendors implement C99's ways to control FP exceptions,
2824      * Python requires non-stop mode.  Alas, some platforms enable FP
2825      * exceptions by default.  Here we disable them.
2826      */
2827 #ifdef __FreeBSD__
2828     fp_except_t m;
2829
2830     m = fpgetmask();
2831     fpsetmask(m & ~FP_X_OFL);
2832 #endif
2833     if (argc && argv)
2834         Py_SetProgramName(argv[0]);
2835     Py_Initialize();
2836     if (argc && argv)
2837         PySys_SetArgv(argc, argv);
2838     { /* init module '%(module_name)s' as '__main__' */
2839       PyObject* m = NULL;
2840       %(module_is_main)s = 1;
2841       #if PY_MAJOR_VERSION < 3
2842           init%(module_name)s();
2843       #else
2844           m = PyInit_%(module_name)s();
2845       #endif
2846       if (PyErr_Occurred()) {
2847           PyErr_Print(); /* This exits with the right code if SystemExit. */
2848           #if PY_MAJOR_VERSION < 3
2849           if (Py_FlushLine()) PyErr_Clear();
2850           #endif
2851           return 1;
2852       }
2853       Py_XDECREF(m);
2854     }
2855     Py_Finalize();
2856     return 0;
2857 }
2858
2859
2860 #if PY_MAJOR_VERSION >= 3 && !defined(WIN32) && !defined(MS_WINDOWS)
2861 #include <locale.h>
2862
2863 static wchar_t*
2864 __Pyx_char2wchar(char* arg)
2865 {
2866         wchar_t *res;
2867 #ifdef HAVE_BROKEN_MBSTOWCS
2868         /* Some platforms have a broken implementation of
2869          * mbstowcs which does not count the characters that
2870          * would result from conversion.  Use an upper bound.
2871          */
2872         size_t argsize = strlen(arg);
2873 #else
2874         size_t argsize = mbstowcs(NULL, arg, 0);
2875 #endif
2876         size_t count;
2877         unsigned char *in;
2878         wchar_t *out;
2879 #ifdef HAVE_MBRTOWC
2880         mbstate_t mbs;
2881 #endif
2882         if (argsize != (size_t)-1) {
2883                 res = (wchar_t *)malloc((argsize+1)*sizeof(wchar_t));
2884                 if (!res)
2885                         goto oom;
2886                 count = mbstowcs(res, arg, argsize+1);
2887                 if (count != (size_t)-1) {
2888                         wchar_t *tmp;
2889                         /* Only use the result if it contains no
2890                            surrogate characters. */
2891                         for (tmp = res; *tmp != 0 &&
2892                                      (*tmp < 0xd800 || *tmp > 0xdfff); tmp++)
2893                                 ;
2894                         if (*tmp == 0)
2895                                 return res;
2896                 }
2897                 free(res);
2898         }
2899         /* Conversion failed. Fall back to escaping with surrogateescape. */
2900 #ifdef HAVE_MBRTOWC
2901         /* Try conversion with mbrtwoc (C99), and escape non-decodable bytes. */
2902
2903         /* Overallocate; as multi-byte characters are in the argument, the
2904            actual output could use less memory. */
2905         argsize = strlen(arg) + 1;
2906         res = malloc(argsize*sizeof(wchar_t));
2907         if (!res) goto oom;
2908         in = (unsigned char*)arg;
2909         out = res;
2910         memset(&mbs, 0, sizeof mbs);
2911         while (argsize) {
2912                 size_t converted = mbrtowc(out, (char*)in, argsize, &mbs);
2913                 if (converted == 0)
2914                         /* Reached end of string; null char stored. */
2915                         break;
2916                 if (converted == (size_t)-2) {
2917                         /* Incomplete character. This should never happen,
2918                            since we provide everything that we have -
2919                            unless there is a bug in the C library, or I
2920                            misunderstood how mbrtowc works. */
2921                         fprintf(stderr, "unexpected mbrtowc result -2\\n");
2922                         return NULL;
2923                 }
2924                 if (converted == (size_t)-1) {
2925                         /* Conversion error. Escape as UTF-8b, and start over
2926                            in the initial shift state. */
2927                         *out++ = 0xdc00 + *in++;
2928                         argsize--;
2929                         memset(&mbs, 0, sizeof mbs);
2930                         continue;
2931                 }
2932                 if (*out >= 0xd800 && *out <= 0xdfff) {
2933                         /* Surrogate character.  Escape the original
2934                            byte sequence with surrogateescape. */
2935                         argsize -= converted;
2936                         while (converted--)
2937                                 *out++ = 0xdc00 + *in++;
2938                         continue;
2939                 }
2940                 /* successfully converted some bytes */
2941                 in += converted;
2942                 argsize -= converted;
2943                 out++;
2944         }
2945 #else
2946         /* Cannot use C locale for escaping; manually escape as if charset
2947            is ASCII (i.e. escape all bytes > 128. This will still roundtrip
2948            correctly in the locale's charset, which must be an ASCII superset. */
2949         res = malloc((strlen(arg)+1)*sizeof(wchar_t));
2950         if (!res) goto oom;
2951         in = (unsigned char*)arg;
2952         out = res;
2953         while(*in)
2954                 if(*in < 128)
2955                         *out++ = *in++;
2956                 else
2957                         *out++ = 0xdc00 + *in++;
2958         *out = 0;
2959 #endif
2960         return res;
2961 oom:
2962         fprintf(stderr, "out of memory\\n");
2963         return NULL;
2964 }
2965
2966 int
2967 %(main_method)s(int argc, char **argv)
2968 {
2969     if (!argc) {
2970         return __Pyx_main(0, NULL);
2971     }
2972     else {
2973         wchar_t **argv_copy = (wchar_t **)malloc(sizeof(wchar_t*)*argc);
2974         /* We need a second copies, as Python might modify the first one. */
2975         wchar_t **argv_copy2 = (wchar_t **)malloc(sizeof(wchar_t*)*argc);
2976         int i, res;
2977         char *oldloc;
2978         if (!argv_copy || !argv_copy2) {
2979             fprintf(stderr, "out of memory\\n");
2980             return 1;
2981         }
2982         oldloc = strdup(setlocale(LC_ALL, NULL));
2983         setlocale(LC_ALL, "");
2984         for (i = 0; i < argc; i++) {
2985             argv_copy2[i] = argv_copy[i] = __Pyx_char2wchar(argv[i]);
2986             if (!argv_copy[i])
2987                 return 1;
2988         }
2989         setlocale(LC_ALL, oldloc);
2990         free(oldloc);
2991         res = __Pyx_main(argc, argv_copy);
2992         for (i = 0; i < argc; i++) {
2993             free(argv_copy2[i]);
2994         }
2995         free(argv_copy);
2996         free(argv_copy2);
2997         return res;
2998     }
2999 }
3000 #endif
3001 """)
3002
3003 packed_struct_utility_code = UtilityCode(proto="""
3004 #if defined(__GNUC__)
3005 #define __Pyx_PACKED __attribute__((__packed__))
3006 #else
3007 #define __Pyx_PACKED
3008 #endif
3009 """, impl="", proto_block='utility_code_proto_before_types')
3010
3011 check_binary_version_utility_code = UtilityCode(proto="""
3012 static int __Pyx_check_binary_version(void);
3013 """, impl="""
3014 static int __Pyx_check_binary_version(void) {
3015     char ctversion[4], rtversion[4];
3016     PyOS_snprintf(ctversion, 4, "%d.%d", PY_MAJOR_VERSION, PY_MINOR_VERSION);
3017     PyOS_snprintf(rtversion, 4, "%s", Py_GetVersion());
3018     if (ctversion[0] != rtversion[0] || ctversion[2] != rtversion[2]) {
3019         char message[200];
3020         PyOS_snprintf(message, sizeof(message),
3021                       "compiletime version %s of module '%.100s' "
3022                       "does not match runtime version %s",
3023                       ctversion, __Pyx_MODULE_NAME, rtversion);
3024         #if PY_VERSION_HEX < 0x02050000
3025         return PyErr_Warn(NULL, message);
3026         #else
3027         return PyErr_WarnEx(NULL, message, 1);
3028         #endif
3029     }
3030     return 0;
3031 }
3032 """)