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