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