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