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