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