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