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