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