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