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