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