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