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