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