Cython has moved to github.

cython-devel

view Cython/Compiler/ModuleNode.py @ 1879:07c1e0dc312b

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