Cython has moved to github.
cython-devel
view Cython/Compiler/Nodes.py @ 1776:9e0b4fa897e3
Fix type conversion for from...import statement.
| author | Robert Bradshaw <robertwb@math.washington.edu> |
|---|---|
| date | Wed Feb 25 21:12:49 2009 -0800 (3 years ago) |
| parents | 252fd73af822 |
| children | a9b9b6e52360 |
line source
1 #
2 # Pyrex - Parse tree nodes
3 #
5 import string, sys, os, time, copy
7 import Code
8 import Builtin
9 from Errors import error, warning, InternalError
10 import Naming
11 import PyrexTypes
12 import TypeSlots
13 from PyrexTypes import py_object_type, error_type, CTypedefType, CFuncType
14 from Symtab import ModuleScope, LocalScope, GeneratorLocalScope, \
15 StructOrUnionScope, PyClassScope, CClassScope
16 from Cython.Utils import open_new_file, replace_suffix, UtilityCode
17 from StringEncoding import EncodedString, escape_byte_string, split_docstring
18 import Options
19 import ControlFlow
20 import DebugFlags
22 from DebugFlags import debug_disposal_code
24 absolute_path_length = 0
26 def relative_position(pos):
27 """
28 We embed the relative filename in the generated C file, since we
29 don't want to have to regnerate and compile all the source code
30 whenever the Python install directory moves (which could happen,
31 e.g,. when distributing binaries.)
33 INPUT:
34 a position tuple -- (absolute filename, line number column position)
36 OUTPUT:
37 relative filename
38 line number
40 AUTHOR: William Stein
41 """
42 global absolute_path_length
43 if absolute_path_length==0:
44 absolute_path_length = len(os.path.abspath(os.getcwd()))
45 return (pos[0].get_filenametable_entry()[absolute_path_length+1:], pos[1])
47 def embed_position(pos, docstring):
48 if not Options.embed_pos_in_docstring:
49 return docstring
50 pos_line = u'File: %s (starting at line %s)' % relative_position(pos)
51 if docstring is None:
52 # unicode string
53 return EncodedString(pos_line)
55 # make sure we can encode the filename in the docstring encoding
56 # otherwise make the docstring a unicode string
57 encoding = docstring.encoding
58 if encoding is not None:
59 try:
60 encoded_bytes = pos_line.encode(encoding)
61 except UnicodeEncodeError:
62 encoding = None
64 if not docstring:
65 # reuse the string encoding of the original docstring
66 doc = EncodedString(pos_line)
67 else:
68 doc = EncodedString(pos_line + u'\n' + docstring)
69 doc.encoding = encoding
70 return doc
73 from Code import CCodeWriter
74 from types import FunctionType
76 def write_func_call(func):
77 def f(*args, **kwds):
78 if len(args) > 1 and isinstance(args[1], CCodeWriter):
79 # here we annotate the code with this function call
80 # but only if new code is generated
81 node, code = args[:2]
82 marker = ' /* %s -> %s.%s %s */' % (
83 ' ' * code.call_level,
84 node.__class__.__name__,
85 func.__name__,
86 node.pos[1:])
87 pristine = code.buffer.stream.tell()
88 code.putln(marker)
89 start = code.buffer.stream.tell()
90 code.call_level += 4
91 res = func(*args, **kwds)
92 code.call_level -= 4
93 if start == code.buffer.stream.tell():
94 code.buffer.stream.seek(pristine)
95 else:
96 marker = marker.replace('->', '<-')
97 code.putln(marker)
98 return res
99 else:
100 return func(*args, **kwds)
101 return f
103 class VerboseCodeWriter(type):
104 # Set this as a metaclass to trace function calls in code.
105 # This slows down code generation and makes much larger files.
106 def __new__(cls, name, bases, attrs):
107 attrs = dict(attrs)
108 for mname, m in attrs.items():
109 if isinstance(m, FunctionType):
110 attrs[mname] = write_func_call(m)
111 return super(VerboseCodeWriter, cls).__new__(cls, name, bases, attrs)
114 class Node(object):
115 # pos (string, int, int) Source file position
116 # is_name boolean Is a NameNode
117 # is_literal boolean Is a ConstNode
119 # Uncomment this for debugging.
120 # __metaclass__ = VerboseCodeWriter
122 is_name = 0
123 is_literal = 0
124 temps = None
126 # All descandants should set child_attrs to a list of the attributes
127 # containing nodes considered "children" in the tree. Each such attribute
128 # can either contain a single node or a list of nodes. See Visitor.py.
129 child_attrs = None
131 def __init__(self, pos, **kw):
132 self.pos = pos
133 self.__dict__.update(kw)
135 gil_message = "Operation"
137 def gil_check(self, env):
138 if env.nogil:
139 self.gil_error()
141 def gil_error(self):
142 error(self.pos, "%s not allowed without gil" % self.gil_message)
144 def clone_node(self):
145 """Clone the node. This is defined as a shallow copy, except for member lists
146 amongst the child attributes (from get_child_accessors) which are also
147 copied. Lists containing child nodes are thus seen as a way for the node
148 to hold multiple children directly; the list is not treated as a seperate
149 level in the tree."""
150 result = copy.copy(self)
151 for attrname in result.child_attrs:
152 value = getattr(result, attrname)
153 if isinstance(value, list):
154 setattr(result, attrname, [x for x in value])
155 return result
158 #
159 # There are 4 phases of parse tree processing, applied in order to
160 # all the statements in a given scope-block:
161 #
162 # (0) analyse_control_flow
163 # Create the control flow tree into which state can be asserted and
164 # queried.
165 #
166 # (1) analyse_declarations
167 # Make symbol table entries for all declarations at the current
168 # level, both explicit (def, cdef, etc.) and implicit (assignment
169 # to an otherwise undeclared name).
170 #
171 # (2) analyse_expressions
172 # Determine the result types of expressions and fill in the
173 # 'type' attribute of each ExprNode. Insert coercion nodes into the
174 # tree where needed to convert to and from Python objects.
175 # Allocate temporary locals for intermediate results. Fill
176 # in the 'result_code' attribute of each ExprNode with a C code
177 # fragment.
178 #
179 # (3) generate_code
180 # Emit C code for all declarations, statements and expressions.
181 # Recursively applies the 3 processing phases to the bodies of
182 # functions.
183 #
185 def analyse_control_flow(self, env):
186 pass
188 def analyse_declarations(self, env):
189 pass
191 def analyse_expressions(self, env):
192 raise InternalError("analyse_expressions not implemented for %s" % \
193 self.__class__.__name__)
195 def generate_code(self, code):
196 raise InternalError("generate_code not implemented for %s" % \
197 self.__class__.__name__)
199 def annotate(self, code):
200 # mro does the wrong thing
201 if isinstance(self, BlockNode):
202 self.body.annotate(code)
204 def end_pos(self):
205 try:
206 return self._end_pos
207 except AttributeError:
208 pos = self.pos
209 if not self.child_attrs:
210 self._end_pos = pos
211 return pos
212 for attr in self.child_attrs:
213 child = getattr(self, attr)
214 # Sometimes lists, sometimes nodes
215 if child is None:
216 pass
217 elif isinstance(child, list):
218 for c in child:
219 pos = max(pos, c.end_pos())
220 else:
221 pos = max(pos, child.end_pos())
222 self._end_pos = pos
223 return pos
225 def dump(self, level=0, filter_out=("pos",), cutoff=100, encountered=None):
226 if cutoff == 0:
227 return "<...nesting level cutoff...>"
228 if encountered is None:
229 encountered = set()
230 if id(self) in encountered:
231 return "<%s (%d) -- already output>" % (self.__class__.__name__, id(self))
232 encountered.add(id(self))
234 def dump_child(x, level):
235 if isinstance(x, Node):
236 return x.dump(level, filter_out, cutoff-1, encountered)
237 elif isinstance(x, list):
238 return "[%s]" % ", ".join([dump_child(item, level) for item in x])
239 else:
240 return repr(x)
243 attrs = [(key, value) for key, value in self.__dict__.iteritems() if key not in filter_out]
244 if len(attrs) == 0:
245 return "<%s (%d)>" % (self.__class__.__name__, id(self))
246 else:
247 indent = " " * level
248 res = "<%s (%d)\n" % (self.__class__.__name__, id(self))
249 for key, value in attrs:
250 res += "%s %s: %s\n" % (indent, key, dump_child(value, level + 1))
251 res += "%s>" % indent
252 return res
254 class CompilerDirectivesNode(Node):
255 """
256 Sets compiler directives for the children nodes
257 """
258 # directives {string:value} A dictionary holding the right value for
259 # *all* possible directives.
260 # body Node
261 child_attrs = ["body"]
263 def analyse_control_flow(self, env):
264 old = env.directives
265 env.directives = self.directives
266 self.body.analyse_control_flow(env)
267 env.directives = old
269 def analyse_declarations(self, env):
270 old = env.directives
271 env.directives = self.directives
272 self.body.analyse_declarations(env)
273 env.directives = old
275 def analyse_expressions(self, env):
276 old = env.directives
277 env.directives = self.directives
278 self.body.analyse_expressions(env)
279 env.directives = old
281 def generate_function_definitions(self, env, code):
282 env_old = env.directives
283 code_old = code.globalstate.directives
284 code.globalstate.directives = self.directives
285 self.body.generate_function_definitions(env, code)
286 env.directives = env_old
287 code.globalstate.directives = code_old
289 def generate_execution_code(self, code):
290 old = code.globalstate.directives
291 code.globalstate.directives = self.directives
292 self.body.generate_execution_code(code)
293 code.globalstate.directives = old
295 def annotate(self, code):
296 old = code.globalstate.directives
297 code.globalstate.directives = self.directives
298 self.body.annotate(code)
299 code.globalstate.directives = old
301 class BlockNode(object):
302 # Mixin class for nodes representing a declaration block.
304 def generate_const_definitions(self, env, code):
305 if env.const_entries:
306 for entry in env.const_entries:
307 if not entry.is_interned:
308 code.globalstate.add_const_definition(entry)
310 def generate_interned_string_decls(self, env, code):
311 entries = env.global_scope().new_interned_string_entries
312 if entries:
313 for entry in entries:
314 code.globalstate.add_interned_string_decl(entry)
315 del entries[:]
317 def generate_py_string_decls(self, env, code):
318 if env is None:
319 return # earlier error
320 entries = env.pystring_entries
321 if entries:
322 for entry in entries:
323 if not entry.is_interned:
324 code.globalstate.add_py_string_decl(entry)
326 def generate_interned_num_decls(self, env, code):
327 # Flush accumulated interned nums from the global scope
328 # and generate declarations for them.
329 genv = env.global_scope()
330 entries = genv.interned_nums
331 if entries:
332 for entry in entries:
333 code.globalstate.add_interned_num_decl(entry)
334 del entries[:]
336 def generate_cached_builtins_decls(self, env, code):
337 entries = env.global_scope().undeclared_cached_builtins
338 for entry in entries:
339 code.globalstate.add_cached_builtin_decl(entry)
340 del entries[:]
343 class StatListNode(Node):
344 # stats a list of StatNode
346 child_attrs = ["stats"]
348 def create_analysed(pos, env, *args, **kw):
349 node = StatListNode(pos, *args, **kw)
350 return node # No node-specific analysis necesarry
351 create_analysed = staticmethod(create_analysed)
353 def analyse_control_flow(self, env):
354 for stat in self.stats:
355 stat.analyse_control_flow(env)
357 def analyse_declarations(self, env):
358 #print "StatListNode.analyse_declarations" ###
359 for stat in self.stats:
360 stat.analyse_declarations(env)
362 def analyse_expressions(self, env):
363 #print "StatListNode.analyse_expressions" ###
364 for stat in self.stats:
365 stat.analyse_expressions(env)
367 def generate_function_definitions(self, env, code):
368 #print "StatListNode.generate_function_definitions" ###
369 for stat in self.stats:
370 stat.generate_function_definitions(env, code)
372 def generate_execution_code(self, code):
373 #print "StatListNode.generate_execution_code" ###
374 for stat in self.stats:
375 code.mark_pos(stat.pos)
376 stat.generate_execution_code(code)
378 def annotate(self, code):
379 for stat in self.stats:
380 stat.annotate(code)
383 class StatNode(Node):
384 #
385 # Code generation for statements is split into the following subphases:
386 #
387 # (1) generate_function_definitions
388 # Emit C code for the definitions of any structs,
389 # unions, enums and functions defined in the current
390 # scope-block.
391 #
392 # (2) generate_execution_code
393 # Emit C code for executable statements.
394 #
396 def generate_function_definitions(self, env, code):
397 pass
399 def generate_execution_code(self, code):
400 raise InternalError("generate_execution_code not implemented for %s" % \
401 self.__class__.__name__)
404 class CDefExternNode(StatNode):
405 # include_file string or None
406 # body StatNode
408 child_attrs = ["body"]
410 def analyse_declarations(self, env):
411 if self.include_file:
412 env.add_include_file(self.include_file)
413 old_cinclude_flag = env.in_cinclude
414 env.in_cinclude = 1
415 self.body.analyse_declarations(env)
416 env.in_cinclude = old_cinclude_flag
418 def analyse_expressions(self, env):
419 pass
421 def generate_execution_code(self, code):
422 pass
424 def annotate(self, code):
425 self.body.annotate(code)
428 class CDeclaratorNode(Node):
429 # Part of a C declaration.
430 #
431 # Processing during analyse_declarations phase:
432 #
433 # analyse
434 # Returns (name, type) pair where name is the
435 # CNameDeclaratorNode of the name being declared
436 # and type is the type it is being declared as.
437 #
438 # calling_convention string Calling convention of CFuncDeclaratorNode
439 # for which this is a base
441 child_attrs = []
443 calling_convention = ""
446 class CNameDeclaratorNode(CDeclaratorNode):
447 # name string The Pyrex name being declared
448 # cname string or None C name, if specified
449 # default ExprNode or None the value assigned on declaration
451 child_attrs = ['default']
453 default = None
455 def analyse(self, base_type, env, nonempty = 0):
456 if nonempty and self.name == '':
457 # May have mistaken the name for the type.
458 if base_type.is_ptr or base_type.is_array or base_type.is_buffer:
459 error(self.pos, "Missing argument name")
460 elif base_type.is_void:
461 error(self.pos, "Use spam() rather than spam(void) to declare a function with no arguments.")
462 else:
463 self.name = base_type.declaration_code("", for_display=1, pyrex=1)
464 base_type = py_object_type
465 self.type = base_type
466 return self, base_type
468 class CPtrDeclaratorNode(CDeclaratorNode):
469 # base CDeclaratorNode
471 child_attrs = ["base"]
473 def analyse(self, base_type, env, nonempty = 0):
474 if base_type.is_pyobject:
475 error(self.pos,
476 "Pointer base type cannot be a Python object")
477 ptr_type = PyrexTypes.c_ptr_type(base_type)
478 return self.base.analyse(ptr_type, env, nonempty = nonempty)
480 class CArrayDeclaratorNode(CDeclaratorNode):
481 # base CDeclaratorNode
482 # dimension ExprNode
484 child_attrs = ["base", "dimension"]
486 def analyse(self, base_type, env, nonempty = 0):
487 if self.dimension:
488 self.dimension.analyse_const_expression(env)
489 if not self.dimension.type.is_int:
490 error(self.dimension.pos, "Array dimension not integer")
491 size = self.dimension.result()
492 try:
493 size = int(size)
494 except ValueError:
495 # runtime constant?
496 pass
497 else:
498 size = None
499 if not base_type.is_complete():
500 error(self.pos,
501 "Array element type '%s' is incomplete" % base_type)
502 if base_type.is_pyobject:
503 error(self.pos,
504 "Array element cannot be a Python object")
505 if base_type.is_cfunction:
506 error(self.pos,
507 "Array element cannot be a function")
508 array_type = PyrexTypes.c_array_type(base_type, size)
509 return self.base.analyse(array_type, env, nonempty = nonempty)
512 class CFuncDeclaratorNode(CDeclaratorNode):
513 # base CDeclaratorNode
514 # args [CArgDeclNode]
515 # has_varargs boolean
516 # exception_value ConstNode
517 # exception_check boolean True if PyErr_Occurred check needed
518 # nogil boolean Can be called without gil
519 # with_gil boolean Acquire gil around function body
521 child_attrs = ["base", "args", "exception_value"]
523 overridable = 0
524 optional_arg_count = 0
526 def analyse(self, return_type, env, nonempty = 0):
527 if nonempty:
528 nonempty -= 1
529 func_type_args = []
530 for arg_node in self.args:
531 name_declarator, type = arg_node.analyse(env, nonempty = nonempty)
532 name = name_declarator.name
533 if name_declarator.cname:
534 error(self.pos,
535 "Function argument cannot have C name specification")
536 # Turn *[] argument into **
537 if type.is_array:
538 type = PyrexTypes.c_ptr_type(type.base_type)
539 # Catch attempted C-style func(void) decl
540 if type.is_void:
541 error(arg_node.pos, "Use spam() rather than spam(void) to declare a function with no arguments.")
542 # if type.is_pyobject and self.nogil:
543 # error(self.pos,
544 # "Function with Python argument cannot be declared nogil")
545 func_type_args.append(
546 PyrexTypes.CFuncTypeArg(name, type, arg_node.pos))
547 if arg_node.default:
548 self.optional_arg_count += 1
549 elif self.optional_arg_count:
550 error(self.pos, "Non-default argument follows default argument")
552 if self.optional_arg_count:
553 scope = StructOrUnionScope()
554 scope.declare_var('%sn' % Naming.pyrex_prefix, PyrexTypes.c_int_type, self.pos)
555 for arg in func_type_args[len(func_type_args)-self.optional_arg_count:]:
556 scope.declare_var(arg.name, arg.type, arg.pos, allow_pyobject = 1)
557 struct_cname = env.mangle(Naming.opt_arg_prefix, self.base.name)
558 self.op_args_struct = env.global_scope().declare_struct_or_union(name = struct_cname,
559 kind = 'struct',
560 scope = scope,
561 typedef_flag = 0,
562 pos = self.pos,
563 cname = struct_cname)
564 self.op_args_struct.defined_in_pxd = 1
565 self.op_args_struct.used = 1
567 exc_val = None
568 exc_check = 0
569 if return_type.is_pyobject \
570 and (self.exception_value or self.exception_check) \
571 and self.exception_check != '+':
572 error(self.pos,
573 "Exception clause not allowed for function returning Python object")
574 else:
575 if self.exception_value:
576 self.exception_value.analyse_const_expression(env)
577 if self.exception_check == '+':
578 exc_val_type = self.exception_value.type
579 if not exc_val_type.is_error and \
580 not exc_val_type.is_pyobject and \
581 not (exc_val_type.is_cfunction and not exc_val_type.return_type.is_pyobject and len(exc_val_type.args)==0):
582 error(self.exception_value.pos,
583 "Exception value must be a Python exception or cdef function with no arguments.")
584 exc_val = self.exception_value
585 else:
586 exc_val = self.exception_value.result()
587 if not return_type.assignable_from(self.exception_value.type):
588 error(self.exception_value.pos,
589 "Exception value incompatible with function return type")
590 exc_check = self.exception_check
591 if return_type.is_array:
592 error(self.pos,
593 "Function cannot return an array")
594 if return_type.is_cfunction:
595 error(self.pos,
596 "Function cannot return a function")
597 func_type = PyrexTypes.CFuncType(
598 return_type, func_type_args, self.has_varargs,
599 optional_arg_count = self.optional_arg_count,
600 exception_value = exc_val, exception_check = exc_check,
601 calling_convention = self.base.calling_convention,
602 nogil = self.nogil, with_gil = self.with_gil, is_overridable = self.overridable)
603 if self.optional_arg_count:
604 func_type.op_arg_struct = PyrexTypes.c_ptr_type(self.op_args_struct.type)
605 return self.base.analyse(func_type, env)
608 class CArgDeclNode(Node):
609 # Item in a function declaration argument list.
610 #
611 # base_type CBaseTypeNode
612 # declarator CDeclaratorNode
613 # not_none boolean Tagged with 'not None'
614 # default ExprNode or None
615 # default_entry Symtab.Entry Entry for the variable holding the default value
616 # default_result_code string cname or code fragment for default value
617 # is_self_arg boolean Is the "self" arg of an extension type method
618 # is_kw_only boolean Is a keyword-only argument
620 child_attrs = ["base_type", "declarator", "default"]
622 is_self_arg = 0
623 is_generic = 1
624 type = None
625 name_declarator = None
627 def analyse(self, env, nonempty = 0):
628 #print "CArgDeclNode.analyse: is_self_arg =", self.is_self_arg ###
629 if self.type is None:
630 # The parser may missinterpret names as types...
631 # We fix that here.
632 if isinstance(self.declarator, CNameDeclaratorNode) and self.declarator.name == '':
633 if nonempty:
634 self.declarator.name = self.base_type.name
635 self.base_type.name = None
636 self.base_type.is_basic_c_type = False
637 could_be_name = True
638 else:
639 could_be_name = False
640 base_type = self.base_type.analyse(env, could_be_name = could_be_name)
641 if hasattr(self.base_type, 'arg_name') and self.base_type.arg_name:
642 self.declarator.name = self.base_type.arg_name
643 return self.declarator.analyse(base_type, env, nonempty = nonempty)
644 else:
645 return self.name_declarator, self.type
647 def annotate(self, code):
648 if self.default:
649 self.default.annotate(code)
652 class CBaseTypeNode(Node):
653 # Abstract base class for C base type nodes.
654 #
655 # Processing during analyse_declarations phase:
656 #
657 # analyse
658 # Returns the type.
660 pass
662 class CAnalysedBaseTypeNode(Node):
663 # type type
665 child_attrs = []
667 def analyse(self, env, could_be_name = False):
668 return self.type
670 class CSimpleBaseTypeNode(CBaseTypeNode):
671 # name string
672 # module_path [string] Qualifying name components
673 # is_basic_c_type boolean
674 # signed boolean
675 # longness integer
676 # is_self_arg boolean Is self argument of C method
678 child_attrs = []
679 arg_name = None # in case the argument name was interpreted as a type
681 def analyse(self, env, could_be_name = False):
682 # Return type descriptor.
683 #print "CSimpleBaseTypeNode.analyse: is_self_arg =", self.is_self_arg ###
684 type = None
685 if self.is_basic_c_type:
686 type = PyrexTypes.simple_c_type(self.signed, self.longness, self.name)
687 if not type:
688 error(self.pos, "Unrecognised type modifier combination")
689 elif self.name == "object" and not self.module_path:
690 type = py_object_type
691 elif self.name is None:
692 if self.is_self_arg and env.is_c_class_scope:
693 #print "CSimpleBaseTypeNode.analyse: defaulting to parent type" ###
694 type = env.parent_type
695 else:
696 type = py_object_type
697 else:
698 if self.module_path:
699 scope = env.find_imported_module(self.module_path, self.pos)
700 else:
701 scope = env
702 if scope:
703 if scope.is_c_class_scope:
704 scope = scope.global_scope()
705 entry = scope.lookup(self.name)
706 if entry and entry.is_type:
707 type = entry.type
708 elif could_be_name:
709 if self.is_self_arg and env.is_c_class_scope:
710 type = env.parent_type
711 else:
712 type = py_object_type
713 self.arg_name = self.name
714 else:
715 error(self.pos, "'%s' is not a type identifier" % self.name)
716 if type:
717 return type
718 else:
719 return PyrexTypes.error_type
721 class CBufferAccessTypeNode(CBaseTypeNode):
722 # After parsing:
723 # positional_args [ExprNode] List of positional arguments
724 # keyword_args DictNode Keyword arguments
725 # base_type_node CBaseTypeNode
727 # After analysis:
728 # type PyrexType.BufferType ...containing the right options
731 child_attrs = ["base_type_node", "positional_args",
732 "keyword_args", "dtype_node"]
734 dtype_node = None
736 name = None
738 def analyse(self, env, could_be_name = False):
739 base_type = self.base_type_node.analyse(env)
740 if base_type.is_error: return base_type
741 import Buffer
743 options = Buffer.analyse_buffer_options(
744 self.pos,
745 env,
746 self.positional_args,
747 self.keyword_args,
748 base_type.buffer_defaults)
750 self.type = PyrexTypes.BufferType(base_type, **options)
751 return self.type
753 class CComplexBaseTypeNode(CBaseTypeNode):
754 # base_type CBaseTypeNode
755 # declarator CDeclaratorNode
757 child_attrs = ["base_type", "declarator"]
759 def analyse(self, env, could_be_name = False):
760 base = self.base_type.analyse(env, could_be_name)
761 _, type = self.declarator.analyse(base, env)
762 return type
765 class CVarDefNode(StatNode):
766 # C variable definition or forward/extern function declaration.
767 #
768 # visibility 'private' or 'public' or 'extern'
769 # base_type CBaseTypeNode
770 # declarators [CDeclaratorNode]
771 # in_pxd boolean
772 # api boolean
773 # need_properties [entry]
774 # pxd_locals [CVarDefNode] (used for functions declared in pxd)
776 child_attrs = ["base_type", "declarators"]
777 need_properties = ()
778 pxd_locals = []
780 def analyse_declarations(self, env, dest_scope = None):
781 if not dest_scope:
782 dest_scope = env
783 self.dest_scope = dest_scope
784 base_type = self.base_type.analyse(env)
785 if (dest_scope.is_c_class_scope
786 and self.visibility == 'public'
787 and base_type.is_pyobject
788 and (base_type.is_builtin_type or base_type.is_extension_type)):
789 self.need_properties = []
790 need_property = True
791 visibility = 'private'
792 else:
793 need_property = False
794 visibility = self.visibility
796 for declarator in self.declarators:
797 name_declarator, type = declarator.analyse(base_type, env)
798 if not type.is_complete():
799 if not (self.visibility == 'extern' and type.is_array):
800 error(declarator.pos,
801 "Variable type '%s' is incomplete" % type)
802 if self.visibility == 'extern' and type.is_pyobject:
803 error(declarator.pos,
804 "Python object cannot be declared extern")
805 name = name_declarator.name
806 cname = name_declarator.cname
807 if name == '':
808 error(declarator.pos, "Missing name in declaration.")
809 return
810 if type.is_cfunction:
811 entry = dest_scope.declare_cfunction(name, type, declarator.pos,
812 cname = cname, visibility = self.visibility, in_pxd = self.in_pxd,
813 api = self.api)
814 if entry is not None:
815 entry.pxd_locals = self.pxd_locals
816 else:
817 if self.in_pxd and self.visibility != 'extern':
818 error(self.pos,
819 "Only 'extern' C variable declaration allowed in .pxd file")
820 entry = dest_scope.declare_var(name, type, declarator.pos,
821 cname = cname, visibility = visibility, is_cdef = 1)
822 if need_property:
823 self.need_properties.append(entry)
824 entry.needs_property = 1
827 class CStructOrUnionDefNode(StatNode):
828 # name string
829 # cname string or None
830 # kind "struct" or "union"
831 # typedef_flag boolean
832 # visibility "public" or "private"
833 # in_pxd boolean
834 # attributes [CVarDefNode] or None
835 # entry Entry
837 child_attrs = ["attributes"]
839 def analyse_declarations(self, env):
840 scope = None
841 if self.attributes is not None:
842 scope = StructOrUnionScope(self.name)
843 self.entry = env.declare_struct_or_union(
844 self.name, self.kind, scope, self.typedef_flag, self.pos,
845 self.cname, visibility = self.visibility)
846 if self.attributes is not None:
847 if self.in_pxd and not env.in_cinclude:
848 self.entry.defined_in_pxd = 1
849 for attr in self.attributes:
850 attr.analyse_declarations(env, scope)
851 if self.visibility != 'extern':
852 need_typedef_indirection = False
853 for attr in scope.var_entries:
854 type = attr.type
855 while type.is_array:
856 type = type.base_type
857 if type == self.entry.type:
858 error(attr.pos, "Struct cannot contain itself as a member.")
859 if self.typedef_flag:
860 while type.is_ptr:
861 type = type.base_type
862 if type == self.entry.type:
863 need_typedef_indirection = True
864 if need_typedef_indirection:
865 # C can't handle typedef structs that refer to themselves.
866 struct_entry = self.entry
867 cname = env.new_const_cname()
868 self.entry = env.declare_typedef(self.name, struct_entry.type, self.pos, cname = self.cname, visibility='ignore')
869 struct_entry.type.typedef_flag = False
870 struct_entry.cname = struct_entry.type.cname = env.new_const_cname()
872 def analyse_expressions(self, env):
873 pass
875 def generate_execution_code(self, code):
876 pass
879 class CEnumDefNode(StatNode):
880 # name string or None
881 # cname string or None
882 # items [CEnumDefItemNode]
883 # typedef_flag boolean
884 # visibility "public" or "private"
885 # in_pxd boolean
886 # entry Entry
888 child_attrs = ["items"]
890 def analyse_declarations(self, env):
891 self.entry = env.declare_enum(self.name, self.pos,
892 cname = self.cname, typedef_flag = self.typedef_flag,
893 visibility = self.visibility)
894 if self.items is not None:
895 if self.in_pxd and not env.in_cinclude:
896 self.entry.defined_in_pxd = 1
897 for item in self.items:
898 item.analyse_declarations(env, self.entry)
900 def analyse_expressions(self, env):
901 if self.visibility == 'public':
902 self.temp = env.allocate_temp_pyobject()
903 env.release_temp(self.temp)
905 def generate_execution_code(self, code):
906 if self.visibility == 'public':
907 for item in self.entry.enum_values:
908 code.putln("%s = PyInt_FromLong(%s); %s" % (
909 self.temp,
910 item.cname,
911 code.error_goto_if_null(self.temp, item.pos)))
912 code.putln('if (__Pyx_SetAttrString(%s, "%s", %s) < 0) %s' % (
913 Naming.module_cname,
914 item.name,
915 self.temp,
916 code.error_goto(item.pos)))
917 code.putln("%s = 0;" % self.temp)
920 class CEnumDefItemNode(StatNode):
921 # name string
922 # cname string or None
923 # value ExprNode or None
925 child_attrs = ["value"]
927 def analyse_declarations(self, env, enum_entry):
928 if self.value:
929 self.value.analyse_const_expression(env)
930 if not self.value.type.is_int:
931 self.value = self.value.coerce_to(PyrexTypes.c_int_type, env)
932 self.value.analyse_const_expression(env)
933 value = self.value.result()
934 else:
935 value = self.name
936 entry = env.declare_const(self.name, enum_entry.type,
937 value, self.pos, cname = self.cname, visibility = enum_entry.visibility)
938 enum_entry.enum_values.append(entry)
941 class CTypeDefNode(StatNode):
942 # base_type CBaseTypeNode
943 # declarator CDeclaratorNode
944 # visibility "public" or "private"
945 # in_pxd boolean
947 child_attrs = ["base_type", "declarator"]
949 def analyse_declarations(self, env):
950 base = self.base_type.analyse(env)
951 name_declarator, type = self.declarator.analyse(base, env)
952 name = name_declarator.name
953 cname = name_declarator.cname
954 entry = env.declare_typedef(name, type, self.pos,
955 cname = cname, visibility = self.visibility)
956 if self.in_pxd and not env.in_cinclude:
957 entry.defined_in_pxd = 1
959 def analyse_expressions(self, env):
960 pass
961 def generate_execution_code(self, code):
962 pass
965 class FuncDefNode(StatNode, BlockNode):
966 # Base class for function definition nodes.
967 #
968 # return_type PyrexType
969 # #filename string C name of filename string const
970 # entry Symtab.Entry
971 # needs_closure boolean Whether or not this function has inner functions/classes/yield
972 # pxd_locals [CVarDefNode] locals defined in the pxd
974 py_func = None
975 assmt = None
976 needs_closure = False
977 pxd_locals = []
979 def analyse_default_values(self, env):
980 genv = env.global_scope()
981 for arg in self.args:
982 if arg.default:
983 if arg.is_generic:
984 if not hasattr(arg, 'default_entry'):
985 arg.default.analyse_types(env)
986 arg.default = arg.default.coerce_to(arg.type, genv)
987 if arg.default.is_literal:
988 arg.default_entry = arg.default
989 arg.default_result_code = arg.default.calculate_result_code()
990 if arg.default.type != arg.type and not arg.type.is_int:
991 arg.default_result_code = arg.type.cast_code(arg.default_result_code)
992 else:
993 arg.default.allocate_temps(genv)
994 arg.default_entry = genv.add_default_value(arg.type)
995 if arg.type.is_pyobject:
996 arg.default_entry.init = 0
997 arg.default_entry.used = 1
998 arg.default_result_code = arg.default_entry.cname
999 else:
1000 error(arg.pos,
1001 "This argument cannot have a default value")
1002 arg.default = None
1004 def need_gil_acquisition(self, lenv):
1005 return 0
1007 def create_local_scope(self, env):
1008 genv = env
1009 while env.is_py_class_scope or env.is_c_class_scope:
1010 env = env.outer_scope
1011 if self.needs_closure:
1012 lenv = GeneratorLocalScope(name = self.entry.name, outer_scope = genv)
1013 else:
1014 lenv = LocalScope(name = self.entry.name, outer_scope = genv)
1015 lenv.return_type = self.return_type
1016 type = self.entry.type
1017 if type.is_cfunction:
1018 lenv.nogil = type.nogil and not type.with_gil
1019 self.local_scope = lenv
1020 return lenv
1022 def generate_function_definitions(self, env, code):
1023 import Buffer
1025 lenv = self.local_scope
1027 is_getbuffer_slot = (self.entry.name == "__getbuffer__" and
1028 self.entry.scope.is_c_class_scope)
1030 # Generate C code for header and body of function
1031 code.enter_cfunc_scope()
1032 code.return_from_error_cleanup_label = code.new_label()
1034 # ----- Top-level constants used by this function
1035 code.mark_pos(self.pos)
1036 self.generate_interned_num_decls(lenv, code)
1037 self.generate_interned_string_decls(lenv, code)
1038 self.generate_py_string_decls(lenv, code)
1039 self.generate_cached_builtins_decls(lenv, code)
1040 #code.putln("")
1041 #code.put_var_declarations(lenv.const_entries, static = 1)
1042 self.generate_const_definitions(lenv, code)
1043 # ----- Function header
1044 code.putln("")
1045 if self.py_func:
1046 self.py_func.generate_function_header(code,
1047 with_pymethdef = env.is_py_class_scope,
1048 proto_only=True)
1049 self.generate_function_header(code,
1050 with_pymethdef = env.is_py_class_scope)
1051 # ----- Local variable declarations
1052 lenv.mangle_closure_cnames(Naming.cur_scope_cname)
1053 self.generate_argument_declarations(lenv, code)
1054 if self.needs_closure:
1055 code.putln("/* TODO: declare and create scope object */")
1056 code.put_var_declarations(lenv.var_entries)
1057 init = ""
1058 if not self.return_type.is_void:
1059 if self.return_type.is_pyobject:
1060 init = " = NULL"
1061 code.putln(
1062 "%s%s;" %
1063 (self.return_type.declaration_code(
1064 Naming.retval_cname),
1065 init))
1066 tempvardecl_code = code.insertion_point()
1067 self.generate_keyword_list(code)
1068 # ----- Extern library function declarations
1069 lenv.generate_library_function_declarations(code)
1070 # ----- GIL acquisition
1071 acquire_gil = self.need_gil_acquisition(lenv)
1072 if acquire_gil:
1073 code.putln("PyGILState_STATE _save = PyGILState_Ensure();")
1074 # ----- Automatic lead-ins for certain special functions
1075 if not lenv.nogil:
1076 code.put_setup_refcount_context(self.entry.name)
1077 if is_getbuffer_slot:
1078 self.getbuffer_init(code)
1079 # ----- Fetch arguments
1080 self.generate_argument_parsing_code(env, code)
1081 # If an argument is assigned to in the body, we must
1082 # incref it to properly keep track of refcounts.
1083 for entry in lenv.arg_entries:
1084 if entry.type.is_pyobject and lenv.control_flow.get_state((entry.name, 'source')) != 'arg':
1085 code.put_var_incref(entry)
1086 # ----- Initialise local variables
1087 for entry in lenv.var_entries:
1088 if entry.type.is_pyobject and entry.init_to_none and entry.used:
1089 code.put_init_var_to_py_none(entry)
1090 # ----- Initialise local buffer auxiliary variables
1091 for entry in lenv.var_entries + lenv.arg_entries:
1092 if entry.type.is_buffer and entry.buffer_aux.buffer_info_var.used:
1093 code.putln("%s.buf = NULL;" % entry.buffer_aux.buffer_info_var.cname)
1094 # ----- Check and convert arguments
1095 self.generate_argument_type_tests(code)
1096 # ----- Acquire buffer arguments
1097 for entry in lenv.arg_entries:
1098 if entry.type.is_buffer:
1099 Buffer.put_acquire_arg_buffer(entry, code, self.pos)
1100 # ----- Function body
1101 self.body.generate_execution_code(code)
1102 # ----- Default return value
1103 code.putln("")
1104 if self.return_type.is_pyobject:
1105 #if self.return_type.is_extension_type:
1106 # lhs = "(PyObject *)%s" % Naming.retval_cname
1107 #else:
1108 lhs = Naming.retval_cname
1109 code.put_init_to_py_none(lhs, self.return_type)
1110 else:
1111 val = self.return_type.default_value
1112 if val:
1113 code.putln("%s = %s;" % (Naming.retval_cname, val))
1114 # ----- Error cleanup
1115 if code.error_label in code.labels_used:
1116 code.put_goto(code.return_label)
1117 code.put_label(code.error_label)
1118 # cleanup temps the old way
1119 code.put_var_xdecrefs(lenv.temp_entries)
1120 # cleanup temps the new way
1121 for cname, type in code.funcstate.all_managed_temps():
1122 code.put_xdecref(cname, type)
1124 # Clean up buffers -- this calls a Python function
1125 # so need to save and restore error state
1126 buffers_present = len(lenv.buffer_entries) > 0
1127 if buffers_present:
1128 code.globalstate.use_utility_code(restore_exception_utility_code)
1129 code.putln("{ PyObject *__pyx_type, *__pyx_value, *__pyx_tb;")
1130 code.putln("__Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb);")
1131 for entry in lenv.buffer_entries:
1132 code.putln("%s;" % Buffer.get_release_buffer_code(entry))
1133 #code.putln("%s = 0;" % entry.cname)
1134 code.putln("__Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}")
1136 err_val = self.error_value()
1137 exc_check = self.caller_will_check_exceptions()
1138 if err_val is not None or exc_check:
1139 code.putln('__Pyx_AddTraceback("%s");' % self.entry.qualified_name)
1140 else:
1141 warning(self.entry.pos, "Unraisable exception in function '%s'." \
1142 % self.entry.qualified_name, 0)
1143 code.putln(
1144 '__Pyx_WriteUnraisable("%s");' %
1145 self.entry.qualified_name)
1146 env.use_utility_code(unraisable_exception_utility_code)
1147 env.use_utility_code(restore_exception_utility_code)
1148 default_retval = self.return_type.default_value
1149 if err_val is None and default_retval:
1150 err_val = default_retval
1151 if err_val is not None:
1152 code.putln(
1153 "%s = %s;" % (
1154 Naming.retval_cname,
1155 err_val))
1157 if is_getbuffer_slot:
1158 self.getbuffer_error_cleanup(code)
1160 # If we are using the non-error cleanup section we should
1161 # jump past it if we have an error. The if-test below determine
1162 # whether this section is used.
1163 if buffers_present or is_getbuffer_slot:
1164 code.put_goto(code.return_from_error_cleanup_label)
1167 # ----- Non-error return cleanup
1168 # If you add anything here, remember to add a condition to the
1169 # if-test above in the error block (so that it can jump past this
1170 # block).
1171 code.put_label(code.return_label)
1172 for entry in lenv.buffer_entries:
1173 if entry.used:
1174 code.putln("%s;" % Buffer.get_release_buffer_code(entry))
1175 if is_getbuffer_slot:
1176 self.getbuffer_normal_cleanup(code)
1177 # ----- Return cleanup for both error and no-error return
1178 code.put_label(code.return_from_error_cleanup_label)
1179 if not Options.init_local_none:
1180 for entry in lenv.var_entries:
1181 if lenv.control_flow.get_state((entry.name, 'initalized')) is not True:
1182 entry.xdecref_cleanup = 1
1183 code.put_var_decrefs(lenv.var_entries, used_only = 1)
1184 # Decref any increfed args
1185 for entry in lenv.arg_entries:
1186 if entry.type.is_pyobject and lenv.control_flow.get_state((entry.name, 'source')) != 'arg':
1187 code.put_var_decref(entry)
1189 # code.putln("/* TODO: decref scope object */")
1190 # ----- Return
1191 # This code is duplicated in ModuleNode.generate_module_init_func
1192 if not lenv.nogil:
1193 default_retval = self.return_type.default_value
1194 err_val = self.error_value()
1195 if err_val is None and default_retval:
1196 err_val = default_retval
1197 if self.return_type.is_pyobject:
1198 code.put_xgiveref(self.return_type.as_pyobject(Naming.retval_cname))
1200 code.put_finish_refcount_context()
1202 if acquire_gil:
1203 code.putln("PyGILState_Release(_save);")
1205 if not self.return_type.is_void:
1206 code.putln("return %s;" % Naming.retval_cname)
1208 code.putln("}")
1209 # ----- Go back and insert temp variable declarations
1210 tempvardecl_code.put_var_declarations(lenv.temp_entries)
1211 tempvardecl_code.put_temp_declarations(code.funcstate)
1212 # ----- Python version
1213 code.exit_cfunc_scope()
1214 if self.py_func:
1215 self.py_func.generate_function_definitions(env, code)
1216 self.generate_wrapper_functions(code)
1218 def declare_argument(self, env, arg):
1219 if arg.type.is_void:
1220 error(arg.pos, "Invalid use of 'void'")
1221 elif not arg.type.is_complete() and not arg.type.is_array:
1222 error(arg.pos,
1223 "Argument type '%s' is incomplete" % arg.type)
1224 return env.declare_arg(arg.name, arg.type, arg.pos)
1226 def generate_wrapper_functions(self, code):
1227 pass
1229 def generate_execution_code(self, code):
1230 # Evaluate and store argument default values
1231 for arg in self.args:
1232 default = arg.default
1233 if default:
1234 if not default.is_literal:
1235 default.generate_evaluation_code(code)
1236 default.make_owned_reference(code)
1237 code.putln(
1238 "%s = %s;" % (
1239 arg.default_entry.cname,
1240 default.result_as(arg.default_entry.type)))
1241 if default.is_temp and default.type.is_pyobject:
1242 code.putln("%s = 0;" % default.result())
1243 default.free_temps(code)
1244 code.put_var_giveref(arg.default_entry)
1245 # For Python class methods, create and store function object
1246 if self.assmt:
1247 self.assmt.generate_execution_code(code)
1249 #
1250 # Special code for the __getbuffer__ function
1251 #
1252 def getbuffer_init(self, code):
1253 info = self.local_scope.arg_entries[1].cname
1254 # Python 3.0 betas have a bug in memoryview which makes it call
1255 # getbuffer with a NULL parameter. For now we work around this;
1256 # the following line should be removed when this bug is fixed.
1257 code.putln("if (%s == NULL) return 0;" % info)
1258 code.putln("%s->obj = Py_None; __Pyx_INCREF(Py_None);" % info)
1259 code.put_giveref("%s->obj" % info) # Do not refnanny object within structs
1261 def getbuffer_error_cleanup(self, code):
1262 info = self.local_scope.arg_entries[1].cname
1263 code.put_gotref("%s->obj" % info)
1264 code.putln("__Pyx_DECREF(%s->obj); %s->obj = NULL;" %
1265 (info, info))
1267 def getbuffer_normal_cleanup(self, code):
1268 info = self.local_scope.arg_entries[1].cname
1269 code.putln("if (%s->obj == Py_None) {" % info)
1270 code.put_gotref("Py_None")
1271 code.putln("__Pyx_DECREF(Py_None); %s->obj = NULL;" % info)
1272 code.putln("}")
1274 class CFuncDefNode(FuncDefNode):
1275 # C function definition.
1276 #
1277 # modifiers ['inline']
1278 # visibility 'private' or 'public' or 'extern'
1279 # base_type CBaseTypeNode
1280 # declarator CDeclaratorNode
1281 # body StatListNode
1282 # api boolean
1283 #
1284 # with_gil boolean Acquire GIL around body
1285 # type CFuncType
1286 # py_func wrapper for calling from Python
1287 # overridable whether or not this is a cpdef function
1288 # inline_in_pxd whether this is an inline function in a pxd file
1290 child_attrs = ["base_type", "declarator", "body", "py_func"]
1292 inline_in_pxd = False
1294 def unqualified_name(self):
1295 return self.entry.name
1297 def analyse_declarations(self, env):
1298 if 'locals' in env.directives:
1299 directive_locals = env.directives['locals']
1300 else:
1301 directive_locals = {}
1302 self.directive_locals = directive_locals
1303 base_type = self.base_type.analyse(env)
1304 # The 2 here is because we need both function and argument names.
1305 name_declarator, type = self.declarator.analyse(base_type, env, nonempty = 2 * (self.body is not None))
1306 if not type.is_cfunction:
1307 error(self.pos,
1308 "Suite attached to non-function declaration")
1309 # Remember the actual type according to the function header
1310 # written here, because the type in the symbol table entry
1311 # may be different if we're overriding a C method inherited
1312 # from the base type of an extension type.
1313 self.type = type
1314 type.is_overridable = self.overridable
1315 declarator = self.declarator
1316 while not hasattr(declarator, 'args'):
1317 declarator = declarator.base
1318 self.args = declarator.args
1319 for formal_arg, type_arg in zip(self.args, type.args):
1320 formal_arg.type = type_arg.type
1321 formal_arg.name = type_arg.name
1322 formal_arg.cname = type_arg.cname
1323 name = name_declarator.name
1324 cname = name_declarator.cname
1325 self.entry = env.declare_cfunction(
1326 name, type, self.pos,
1327 cname = cname, visibility = self.visibility,
1328 defining = self.body is not None,
1329 api = self.api, modifiers = self.modifiers)
1330 self.entry.inline_func_in_pxd = self.inline_in_pxd
1331 self.return_type = type.return_type
1333 if self.overridable:
1334 import ExprNodes
1335 py_func_body = self.call_self_node(is_module_scope = env.is_module_scope)
1336 self.py_func = DefNode(pos = self.pos,
1337 name = self.entry.name,
1338 args = self.args,
1339 star_arg = None,
1340 starstar_arg = None,
1341 doc = self.doc,
1342 body = py_func_body,
1343 is_wrapper = 1)
1344 self.py_func.is_module_scope = env.is_module_scope
1345 self.py_func.analyse_declarations(env)
1346 self.entry.as_variable = self.py_func.entry
1347 # Reset scope entry the above cfunction
1348 env.entries[name] = self.entry
1349 self.py_func.interned_attr_cname = env.intern_identifier(
1350 self.py_func.entry.name)
1351 if not env.is_module_scope or Options.lookup_module_cpdef:
1352 self.override = OverrideCheckNode(self.pos, py_func = self.py_func)
1353 self.body = StatListNode(self.pos, stats=[self.override, self.body])
1355 def call_self_node(self, omit_optional_args=0, is_module_scope=0):
1356 import ExprNodes
1357 args = self.type.args
1358 if omit_optional_args:
1359 args = args[:len(args) - self.type.optional_arg_count]
1360 arg_names = [arg.name for arg in args]
1361 if is_module_scope:
1362 cfunc = ExprNodes.NameNode(self.pos, name=self.entry.name)
1363 else:
1364 self_arg = ExprNodes.NameNode(self.pos, name=arg_names[0])
1365 cfunc = ExprNodes.AttributeNode(self.pos, obj=self_arg, attribute=self.entry.name)
1366 skip_dispatch = not is_module_scope or Options.lookup_module_cpdef
1367 c_call = ExprNodes.SimpleCallNode(self.pos, function=cfunc, args=[ExprNodes.NameNode(self.pos, name=n) for n in arg_names[1-is_module_scope:]], wrapper_call=skip_dispatch)
1368 return ReturnStatNode(pos=self.pos, return_type=PyrexTypes.py_object_type, value=c_call)
1370 def declare_arguments(self, env):
1371 for arg in self.type.args:
1372 if not arg.name:
1373 error(arg.pos, "Missing argument name")
1374 self.declare_argument(env, arg)
1376 def need_gil_acquisition(self, lenv):
1377 type = self.type
1378 with_gil = self.type.with_gil
1379 if type.nogil and not with_gil:
1380 if type.return_type.is_pyobject:
1381 error(self.pos,
1382 "Function with Python return type cannot be declared nogil")
1383 for entry in lenv.var_entries + lenv.temp_entries:
1384 if entry.type.is_pyobject:
1385 error(self.pos, "Function declared nogil has Python locals or temporaries")
1386 return with_gil
1388 def analyse_expressions(self, env):
1389 self.analyse_default_values(env)
1390 if self.overridable:
1391 self.py_func.analyse_expressions(env)
1393 def generate_function_header(self, code, with_pymethdef, with_opt_args = 1, with_dispatch = 1, cname = None):
1394 arg_decls = []
1395 type = self.type
1396 visibility = self.entry.visibility
1397 for arg in type.args[:len(type.args)-type.optional_arg_count]:
1398 arg_decls.append(arg.declaration_code())
1399 if with_dispatch and self.overridable:
1400 arg_decls.append(PyrexTypes.c_int_type.declaration_code(Naming.skip_dispatch_cname))
1401 if type.optional_arg_count and with_opt_args:
1402 arg_decls.append(type.op_arg_struct.declaration_code(Naming.optional_args_cname))
1403 if type.has_varargs:
1404 arg_decls.append("...")
1405 if not arg_decls:
1406 arg_decls = ["void"]
1407 if cname is None:
1408 cname = self.entry.func_cname
1409 entity = type.function_header_code(cname, string.join(arg_decls, ", "))
1410 if visibility == 'public':
1411 dll_linkage = "DL_EXPORT"
1412 else:
1413 dll_linkage = None
1414 header = self.return_type.declaration_code(entity,
1415 dll_linkage = dll_linkage)
1416 if visibility == 'extern':
1417 storage_class = "%s " % Naming.extern_c_macro
1418 elif visibility == 'public':
1419 storage_class = ""
1420 else:
1421 storage_class = "static "
1422 code.putln("%s%s %s {" % (
1423 storage_class,
1424 ' '.join(self.modifiers).upper(), # macro forms
1425 header))
1427 def generate_argument_declarations(self, env, code):
1428 for arg in self.args:
1429 if arg.default:
1430 code.putln('%s = %s;' % (arg.type.declaration_code(arg.cname), arg.default_result_code))
1432 def generate_keyword_list(self, code):
1433 pass
1435 def generate_argument_parsing_code(self, env, code):
1436 i = 0
1437 if self.type.optional_arg_count:
1438 code.putln('if (%s) {' % Naming.optional_args_cname)
1439 for arg in self.args:
1440 if arg.default:
1441 code.putln('if (%s->%sn > %s) {' % (Naming.optional_args_cname, Naming.pyrex_prefix, i))
1442 declarator = arg.declarator
1443 while not hasattr(declarator, 'name'):
1444 declarator = declarator.base
1445 code.putln('%s = %s->%s;' % (arg.cname, Naming.optional_args_cname, declarator.name))
1446 i += 1
1447 for _ in range(self.type.optional_arg_count):
1448 code.putln('}')
1449 code.putln('}')
1451 def generate_argument_conversion_code(self, code):
1452 pass
1454 def generate_argument_type_tests(self, code):
1455 # Generate type tests for args whose type in a parent
1456 # class is a supertype of the declared type.
1457 for arg in self.type.args:
1458 if arg.needs_type_test:
1459 self.generate_arg_type_test(arg, code)
1461 def generate_arg_type_test(self, arg, code):
1462 # Generate type test for one argument.
1463 if arg.type.typeobj_is_available():
1464 typeptr_cname = arg.type.typeptr_cname
1465 arg_code = "((PyObject *)%s)" % arg.cname
1466 code.putln(
1467 'if (unlikely(!__Pyx_ArgTypeTest(%s, %s, %d, "%s", %s))) %s' % (
1468 arg_code,
1469 typeptr_cname,
1470 not arg.not_none,
1471 arg.name,
1472 type.is_builtin_type,
1473 code.error_goto(arg.pos)))
1474 else:
1475 error(arg.pos, "Cannot test type of extern C class "
1476 "without type object name specification")
1478 def error_value(self):
1479 if self.return_type.is_pyobject:
1480 return "0"
1481 else:
1482 #return None
1483 return self.entry.type.exception_value
1485 def caller_will_check_exceptions(self):
1486 return self.entry.type.exception_check
1488 def generate_wrapper_functions(self, code):
1489 # If the C signature of a function has changed, we need to generate
1490 # wrappers to put in the slots here.
1491 k = 0
1492 entry = self.entry
1493 func_type = entry.type
1494 while entry.prev_entry is not None:
1495 k += 1
1496 entry = entry.prev_entry
1497 entry.func_cname = "%s%swrap_%s" % (self.entry.func_cname, Naming.pyrex_prefix, k)
1498 code.putln()
1499 self.generate_function_header(code,
1500 0,
1501 with_dispatch = entry.type.is_overridable,
1502 with_opt_args = entry.type.optional_arg_count,
1503 cname = entry.func_cname)
1504 if not self.return_type.is_void:
1505 code.put('return ')
1506 args = self.type.args
1507 arglist = [arg.cname for arg in args[:len(args)-self.type.optional_arg_count]]
1508 if entry.type.is_overridable:
1509 arglist.append(Naming.skip_dispatch_cname)
1510 elif func_type.is_overridable:
1511 arglist.append('0')
1512 if entry.type.optional_arg_count:
1513 arglist.append(Naming.optional_args_cname)
1514 elif func_type.optional_arg_count:
1515 arglist.append('NULL')
1516 code.putln('%s(%s);' % (self.entry.func_cname, ', '.join(arglist)))
1517 code.putln('}')
1520 class PyArgDeclNode(Node):
1521 # Argument which must be a Python object (used
1522 # for * and ** arguments).
1523 #
1524 # name string
1525 # entry Symtab.Entry
1526 child_attrs = []
1529 class DecoratorNode(Node):
1530 # A decorator
1531 #
1532 # decorator NameNode or CallNode
1533 child_attrs = ['decorator']
1536 class DefNode(FuncDefNode):
1537 # A Python function definition.
1538 #
1539 # name string the Python name of the function
1540 # decorators [DecoratorNode] list of decorators
1541 # args [CArgDeclNode] formal arguments
1542 # star_arg PyArgDeclNode or None * argument
1543 # starstar_arg PyArgDeclNode or None ** argument
1544 # doc EncodedString or None
1545 # body StatListNode
1546 #
1547 # The following subnode is constructed internally
1548 # when the def statement is inside a Python class definition.
1549 #
1550 # assmt AssignmentNode Function construction/assignment
1552 child_attrs = ["args", "star_arg", "starstar_arg", "body", "decorators"]
1554 assmt = None
1555 num_kwonly_args = 0
1556 num_required_kw_args = 0
1557 reqd_kw_flags_cname = "0"
1558 is_wrapper = 0
1559 decorators = None
1560 entry = None
1563 def __init__(self, pos, **kwds):
1564 FuncDefNode.__init__(self, pos, **kwds)
1565 k = rk = r = 0
1566 for arg in self.args:
1567 if arg.kw_only:
1568 k += 1
1569 if not arg.default:
1570 rk += 1
1571 if not arg.default:
1572 r += 1
1573 self.num_kwonly_args = k
1574 self.num_required_kw_args = rk
1575 self.num_required_args = r
1577 def as_cfunction(self, cfunc=None, scope=None):
1578 if self.star_arg:
1579 error(self.star_arg.pos, "cdef function cannot have star argument")
1580 if self.starstar_arg:
1581 error(self.starstar_arg.pos, "cdef function cannot have starstar argument")
1582 if cfunc is None:
1583 cfunc_args = []
1584 for formal_arg in self.args:
1585 name_declarator, type = formal_arg.analyse(scope, nonempty=1)
1586 cfunc_args.append(PyrexTypes.CFuncTypeArg(name = name_declarator.name,
1587 cname = None,
1588 type = py_object_type,
1589 pos = formal_arg.pos))
1590 cfunc_type = PyrexTypes.CFuncType(return_type = py_object_type,
1591 args = cfunc_args,
1592 has_varargs = False,
1593 exception_value = None,
1594 exception_check = False,
1595 nogil = False,
1596 with_gil = False,
1597 is_overridable = True)
1598 cfunc = CVarDefNode(self.pos, type=cfunc_type, pxd_locals=[])
1599 else:
1600 cfunc_type = cfunc.type
1601 if len(self.args) != len(cfunc_type.args) or cfunc_type.has_varargs:
1602 error(self.pos, "wrong number of arguments")
1603 error(declarator.pos, "previous declaration here")
1604 for formal_arg, type_arg in zip(self.args, cfunc_type.args):
1605 name_declarator, type = formal_arg.analyse(cfunc.scope, nonempty=1)
1606 if type is None or type is PyrexTypes.py_object_type or formal_arg.is_self:
1607 formal_arg.type = type_arg.type
1608 formal_arg.name_declarator = name_declarator
1609 import ExprNodes
1610 if cfunc_type.exception_value is None:
1611 exception_value = None
1612 else:
1613 exception_value = ExprNodes.ConstNode(self.pos, value=cfunc_type.exception_value, type=cfunc_type.return_type)
1614 declarator = CFuncDeclaratorNode(self.pos,
1615 base = CNameDeclaratorNode(self.pos, name=self.name, cname=None),
1616 args = self.args,
1617 has_varargs = False,
1618 exception_check = cfunc_type.exception_check,
1619 exception_value = exception_value,
1620 with_gil = cfunc_type.with_gil,
1621 nogil = cfunc_type.nogil)
1622 return CFuncDefNode(self.pos,
1623 modifiers = [],
1624 base_type = CAnalysedBaseTypeNode(self.pos, type=cfunc_type.return_type),
1625 declarator = declarator,
1626 body = self.body,
1627 doc = self.doc,
1628 overridable = cfunc_type.is_overridable,
1629 type = cfunc_type,
1630 with_gil = cfunc_type.with_gil,
1631 nogil = cfunc_type.nogil,
1632 visibility = 'private',
1633 api = False,
1634 pxd_locals = cfunc.pxd_locals)
1636 def analyse_declarations(self, env):
1637 if 'locals' in env.directives:
1638 directive_locals = env.directives['locals']
1639 else:
1640 directive_locals = {}
1641 self.directive_locals = directive_locals
1642 for arg in self.args:
1643 if hasattr(arg, 'name'):
1644 type = arg.type
1645 name_declarator = None
1646 else:
1647 base_type = arg.base_type.analyse(env)
1648 name_declarator, type = \
1649 arg.declarator.analyse(base_type, env)
1650 arg.name = name_declarator.name
1651 if arg.name in directive_locals:
1652 type_node = directive_locals[arg.name]
1653 other_type = type_node.analyse_as_type(env)
1654 if other_type is None:
1655 error(type_node.pos, "Not a type")
1656 elif (type is not PyrexTypes.py_object_type
1657 and not type.same_as(other_type)):
1658 error(arg.base_type.pos, "Signature does not agree with previous declaration")
1659 error(type_node.pos, "Previous declaration here")
1660 else:
1661 type = other_type
1662 if name_declarator and name_declarator.cname:
1663 error(self.pos,
1664 "Python function argument cannot have C name specification")
1665 arg.type = type.as_argument_type()
1666 arg.hdr_type = None
1667 arg.needs_conversion = 0
1668 arg.needs_type_test = 0
1669 arg.is_generic = 1
1670 if arg.not_none and not arg.type.is_extension_type:
1671 error(self.pos,
1672 "Only extension type arguments can have 'not None'")
1673 self.declare_pyfunction(env)
1674 self.analyse_signature(env)
1675 self.return_type = self.entry.signature.return_type()
1677 def analyse_signature(self, env):
1678 any_type_tests_needed = 0
1679 # Use the simpler calling signature for zero- and one-argument functions.
1680 if not self.entry.is_special and not self.star_arg and not self.starstar_arg:
1681 if self.entry.signature is TypeSlots.pyfunction_signature and Options.optimize_simple_methods:
1682 if len(self.args) == 0:
1683 self.entry.signature = TypeSlots.pyfunction_noargs
1684 elif len(self.args) == 1:
1685 if self.args[0].default is None and not self.args[0].kw_only:
1686 self.entry.signature = TypeSlots.pyfunction_onearg
1687 elif self.entry.signature is TypeSlots.pymethod_signature:
1688 if len(self.args) == 1:
1689 self.entry.signature = TypeSlots.unaryfunc
1690 elif len(self.args) == 2:
1691 if self.args[1].default is None and not self.args[1].kw_only:
1692 self.entry.signature = TypeSlots.ibinaryfunc
1693 elif self.entry.is_special:
1694 self.entry.trivial_signature = len(self.args) == 1 and not (self.star_arg or self.starstar_arg)
1695 sig = self.entry.signature
1696 nfixed = sig.num_fixed_args()
1697 for i in range(nfixed):
1698 if i < len(self.args):
1699 arg = self.args[i]
1700 arg.is_generic = 0
1701 if sig.is_self_arg(i):
1702 arg.is_self_arg = 1
1703 arg.hdr_type = arg.type = env.parent_type
1704 arg.needs_conversion = 0
1705 else:
1706 arg.hdr_type = sig.fixed_arg_type(i)
1707 if not arg.type.same_as(arg.hdr_type):
1708 if arg.hdr_type.is_pyobject and arg.type.is_pyobject:
1709 arg.needs_type_test = 1
1710 any_type_tests_needed = 1
1711 else:
1712 arg.needs_conversion = 1
1713 if arg.needs_conversion:
1714 arg.hdr_cname = Naming.arg_prefix + arg.name
1715 else:
1716 arg.hdr_cname = Naming.var_prefix + arg.name
1717 else:
1718 self.bad_signature()
1719 return
1720 if nfixed < len(self.args):
1721 if not sig.has_generic_args:
1722 self.bad_signature()
1723 for arg in self.args:
1724 if arg.is_generic and \
1725 (arg.type.is_extension_type or arg.type.is_builtin_type):
1726 arg.needs_type_test = 1
1727 any_type_tests_needed = 1
1728 elif (arg.type is PyrexTypes.c_py_ssize_t_type
1729 or arg.type is PyrexTypes.c_size_t_type):
1730 # Don't use PyArg_ParseTupleAndKeywords's parsing
1731 # Py_ssize_t: want to use __index__ rather than __int__
1732 # size_t: no Python format char
1733 arg.needs_conversion = 1
1734 arg.hdr_type = PyrexTypes.py_object_type
1735 arg.hdr_cname = Naming.arg_prefix + arg.name
1736 if any_type_tests_needed:
1737 env.use_utility_code(arg_type_test_utility_code)
1739 def bad_signature(self):
1740 sig = self.entry.signature
1741 expected_str = "%d" % sig.num_fixed_args()
1742 if sig.has_generic_args:
1743 expected_str = expected_str + " or more"
1744 name = self.name
1745 if name.startswith("__") and name.endswith("__"):
1746 desc = "Special method"
1747 else:
1748 desc = "Method"
1749 error(self.pos,
1750 "%s %s has wrong number of arguments "
1751 "(%d declared, %s expected)" % (
1752 desc, self.name, len(self.args), expected_str))
1754 def signature_has_nongeneric_args(self):
1755 argcount = len(self.args)
1756 if argcount == 0 or (argcount == 1 and self.args[0].is_self_arg):
1757 return 0
1758 return 1
1760 def signature_has_generic_args(self):
1761 return self.entry.signature.has_generic_args
1763 def declare_pyfunction(self, env):
1764 #print "DefNode.declare_pyfunction:", self.name, "in", env ###
1765 name = self.name
1766 entry = env.lookup_here(self.name)
1767 if entry and entry.type.is_cfunction and not self.is_wrapper:
1768 warning(self.pos, "Overriding cdef method with def method.", 5)
1769 entry = env.declare_pyfunction(self.name, self.pos)
1770 self.entry = entry
1771 prefix = env.scope_prefix
1772 entry.func_cname = \
1773 Naming.pyfunc_prefix + prefix + name
1774 entry.pymethdef_cname = \
1775 Naming.pymethdef_prefix + prefix + name
1776 if Options.docstrings:
1777 entry.doc = embed_position(self.pos, self.doc)
1778 entry.doc_cname = \
1779 Naming.funcdoc_prefix + prefix + name
1780 else:
1781 entry.doc = None
1783 def declare_arguments(self, env):
1784 for arg in self.args:
1785 if not arg.name:
1786 error(arg.pos, "Missing argument name")
1787 if arg.needs_conversion:
1788 arg.entry = env.declare_var(arg.name, arg.type, arg.pos)
1789 env.control_flow.set_state((), (arg.name, 'source'), 'arg')
1790 env.control_flow.set_state((), (arg.name, 'initalized'), True)
1791 if arg.type.is_pyobject:
1792 arg.entry.init = "0"
1793 arg.entry.init_to_none = 0
1794 else:
1795 arg.entry = self.declare_argument(env, arg)
1796 arg.entry.used = 1
1797 arg.entry.is_self_arg = arg.is_self_arg
1798 if not arg.is_self_arg:
1799 arg.name_entry = env.get_string_const(
1800 arg.name, identifier = True)
1801 env.add_py_string(arg.name_entry, identifier = True)
1802 if arg.hdr_type:
1803 if arg.is_self_arg or \
1804 (arg.type.is_extension_type and not arg.hdr_type.is_extension_type):
1805 arg.entry.is_declared_generic = 1
1806 self.declare_python_arg(env, self.star_arg)
1807 self.declare_python_arg(env, self.starstar_arg)
1809 def declare_python_arg(self, env, arg):
1810 if arg:
1811 entry = env.declare_var(arg.name,
1812 PyrexTypes.py_object_type, arg.pos)
1813 entry.used = 1
1814 entry.init = "0"
1815 entry.init_to_none = 0
1816 entry.xdecref_cleanup = 1
1817 arg.entry = entry
1818 env.control_flow.set_state((), (arg.name, 'initalized'), True)
1820 def analyse_expressions(self, env):
1821 self.analyse_default_values(env)
1822 if env.is_py_class_scope:
1823 self.synthesize_assignment_node(env)
1825 def synthesize_assignment_node(self, env):
1826 import ExprNodes
1827 self.assmt = SingleAssignmentNode(self.pos,
1828 lhs = ExprNodes.NameNode(self.pos, name = self.name),
1829 rhs = ExprNodes.UnboundMethodNode(self.pos,
1830 class_cname = env.class_obj_cname,
1831 function = ExprNodes.PyCFunctionNode(self.pos,
1832 pymethdef_cname = self.entry.pymethdef_cname)))
1833 self.assmt.analyse_declarations(env)
1834 self.assmt.analyse_expressions(env)
1836 def generate_function_header(self, code, with_pymethdef, proto_only=0):
1837 arg_code_list = []
1838 sig = self.entry.signature
1839 if sig.has_dummy_arg:
1840 arg_code_list.append(
1841 "PyObject *%s" % Naming.self_cname)
1842 for arg in self.args:
1843 if not arg.is_generic:
1844 if arg.is_self_arg:
1845 arg_code_list.append("PyObject *%s" % arg.hdr_cname)
1846 else:
1847 arg_code_list.append(
1848 arg.hdr_type.declaration_code(arg.hdr_cname))
1849 if not self.entry.is_special and sig.method_flags() == [TypeSlots.method_noargs]:
1850 arg_code_list.append("PyObject *unused")
1851 if sig.has_generic_args:
1852 arg_code_list.append(
1853 "PyObject *%s, PyObject *%s"
1854 % (Naming.args_cname, Naming.kwds_cname))
1855 arg_code = ", ".join(arg_code_list)
1856 dc = self.return_type.declaration_code(self.entry.func_cname)
1857 header = "static %s(%s)" % (dc, arg_code)
1858 code.putln("%s; /*proto*/" % header)
1859 if proto_only:
1860 return
1861 if self.entry.doc and Options.docstrings:
1862 docstr = self.entry.doc
1863 if not isinstance(docstr, str):
1864 docstr = docstr.utf8encode()
1865 code.putln(
1866 'static char %s[] = "%s";' % (
1867 self.entry.doc_cname,
1868 split_docstring(escape_byte_string(docstr))))
1869 if with_pymethdef:
1870 code.put(
1871 "static PyMethodDef %s = " %
1872 self.entry.pymethdef_cname)
1873 code.put_pymethoddef(self.entry, ";")
1874 code.putln("%s {" % header)
1876 def generate_argument_declarations(self, env, code):
1877 for arg in self.args:
1878 if arg.is_generic: # or arg.needs_conversion:
1879 if arg.needs_conversion:
1880 code.putln("PyObject *%s = 0;" % arg.hdr_cname)
1881 else:
1882 code.put_var_declaration(arg.entry)
1884 def generate_keyword_list(self, code):
1885 if self.signature_has_generic_args() and \
1886 self.signature_has_nongeneric_args():
1887 code.put(
1888 "static PyObject **%s[] = {" %
1889 Naming.pykwdlist_cname)
1890 for arg in self.args:
1891 if arg.is_generic:
1892 code.put('&%s,' % arg.name_entry.pystring_cname)
1893 code.putln("0};")
1895 def generate_argument_parsing_code(self, env, code):
1896 # Generate PyArg_ParseTuple call for generic
1897 # arguments, if any.
1898 if self.entry.signature.has_dummy_arg:
1899 # get rid of unused argument warning
1900 code.putln("%s = %s;" % (Naming.self_cname, Naming.self_cname))
1902 old_error_label = code.new_error_label()
1903 our_error_label = code.error_label
1904 end_label = code.new_label("argument_unpacking_done")
1906 has_kwonly_args = self.num_kwonly_args > 0
1907 has_star_or_kw_args = self.star_arg is not None \
1908 or self.starstar_arg is not None or has_kwonly_args
1910 if not self.signature_has_generic_args():
1911 if has_star_or_kw_args:
1912 error(self.pos, "This method cannot have * or keyword arguments")
1913 self.generate_argument_conversion_code(code)
1915 elif not self.signature_has_nongeneric_args():
1916 # func(*args) or func(**kw) or func(*args, **kw)
1917 self.generate_stararg_copy_code(code)
1919 else:
1920 positional_args = []
1921 kw_only_args = []
1922 default_seen = 0
1923 for arg in self.args:
1924 arg_entry = arg.entry
1925 if arg.is_generic:
1926 if arg.default:
1927 default_seen = 1
1928 if not arg.is_self_arg:
1929 if arg.kw_only:
1930 kw_only_args.append(arg)
1931 else:
1932 positional_args.append(arg)
1933 elif arg.kw_only:
1934 kw_only_args.append(arg)
1935 default_seen = 1
1936 elif default_seen:
1937 error(arg.pos, "Non-default argument following default argument")
1938 elif not arg.is_self_arg:
1939 positional_args.append(arg)
1940 if arg.needs_conversion:
1941 format = arg.hdr_type.parsetuple_format
1942 else:
1943 format = arg_entry.type.parsetuple_format
1944 if not format:
1945 error(arg.pos,
1946 "Cannot convert Python object argument to type '%s' (when parsing input arguments)"
1947 % arg.type)
1949 self.generate_tuple_and_keyword_parsing_code(
1950 positional_args, kw_only_args, end_label, code)
1952 code.error_label = old_error_label
1953 if code.label_used(our_error_label):
1954 if not code.label_used(end_label):
1955 code.put_goto(end_label)
1956 code.put_label(our_error_label)
1957 if has_star_or_kw_args:
1958 self.generate_arg_decref(self.star_arg, code)
1959 if self.starstar_arg:
1960 if self.starstar_arg.entry.xdecref_cleanup:
1961 code.put_var_xdecref(self.starstar_arg.entry)
1962 else:
1963 code.put_var_decref(self.starstar_arg.entry)
1964 code.putln('__Pyx_AddTraceback("%s");' % self.entry.qualified_name)
1965 code.putln("return %s;" % self.error_value())
1966 if code.label_used(end_label):
1967 code.put_label(end_label)
1969 def generate_arg_assignment(self, arg, item, code):
1970 if arg.type.is_pyobject:
1971 if arg.is_generic:
1972 item = PyrexTypes.typecast(arg.type, PyrexTypes.py_object_type, item)
1973 code.putln("%s = %s;" % (arg.entry.cname, item))
1974 else:
1975 func = arg.type.from_py_function
1976 if func:
1977 code.putln("%s = %s(%s); %s" % (
1978 arg.entry.cname,
1979 func,
1980 item,
1981 code.error_goto_if(arg.type.error_condition(arg.entry.cname), arg.pos)))
1982 else:
1983 error(arg.pos, "Cannot convert Python object argument to type '%s'" % arg.type)
1985 def generate_arg_xdecref(self, arg, code):
1986 if arg:
1987 code.put_var_xdecref(arg.entry)
1989 def generate_arg_decref(self, arg, code):
1990 if arg:
1991 code.put_var_decref(arg.entry)
1993 def generate_stararg_copy_code(self, code):
1994 if not self.star_arg:
1995 code.globalstate.use_utility_code(raise_argtuple_invalid_utility_code)
1996 code.putln("if (unlikely(PyTuple_GET_SIZE(%s) > 0)) {" %
1997 Naming.args_cname)
1998 code.put('__Pyx_RaiseArgtupleInvalid("%s", 1, 0, 0, PyTuple_GET_SIZE(%s)); return %s;' % (
1999 self.name.utf8encode(), Naming.args_cname, self.error_value()))
2000 code.putln("}")
2002 code.globalstate.use_utility_code(keyword_string_check_utility_code)
2004 if self.starstar_arg:
2005 if self.star_arg:
2006 kwarg_check = "unlikely(%s)" % Naming.kwds_cname
2007 else:
2008 kwarg_check = "%s" % Naming.kwds_cname
2009 else:
2010 kwarg_check = "unlikely(%s) && unlikely(PyDict_Size(%s) > 0)" % (
2011 Naming.kwds_cname, Naming.kwds_cname)
2012 code.putln(
2013 "if (%s && unlikely(!__Pyx_CheckKeywordStrings(%s, \"%s\", %d))) return %s;" % (
2014 kwarg_check, Naming.kwds_cname, self.name,
2015 bool(self.starstar_arg), self.error_value()))
2017 if self.starstar_arg:
2018 code.putln("%s = (%s) ? PyDict_Copy(%s) : PyDict_New();" % (
2019 self.starstar_arg.entry.cname,
2020 Naming.kwds_cname,
2021 Naming.kwds_cname))
2022 code.putln("if (unlikely(!%s)) return %s;" % (
2023 self.starstar_arg.entry.cname, self.error_value()))
2024 self.starstar_arg.entry.xdecref_cleanup = 0
2025 code.put_gotref(self.starstar_arg.entry.cname)
2028 if self.star_arg:
2029 code.put_incref(Naming.args_cname, py_object_type)
2030 code.putln("%s = %s;" % (
2031 self.star_arg.entry.cname,
2032 Naming.args_cname))
2033 self.star_arg.entry.xdecref_cleanup = 0
2035 def generate_tuple_and_keyword_parsing_code(self, positional_args,
2036 kw_only_args, success_label, code):
2037 argtuple_error_label = code.new_label("argtuple_error")
2039 min_positional_args = self.num_required_args - self.num_required_kw_args
2040 if len(self.args) > 0 and self.args[0].is_self_arg:
2041 min_positional_args -= 1
2042 max_positional_args = len(positional_args)
2043 has_fixed_positional_count = not self.star_arg and \
2044 min_positional_args == max_positional_args
2046 code.globalstate.use_utility_code(raise_double_keywords_utility_code)
2047 code.globalstate.use_utility_code(raise_argtuple_invalid_utility_code)
2048 if self.num_required_kw_args:
2049 code.globalstate.use_utility_code(raise_keyword_required_utility_code)
2051 if self.starstar_arg or self.star_arg:
2052 self.generate_stararg_init_code(max_positional_args, code)
2054 # --- optimised code when we receive keyword arguments
2055 if self.num_required_kw_args:
2056 likely_hint = "likely"
2057 else:
2058 likely_hint = "unlikely"
2059 code.putln("if (%s(%s)) {" % (likely_hint, Naming.kwds_cname))
2060 self.generate_keyword_unpacking_code(
2061 min_positional_args, max_positional_args,
2062 has_fixed_positional_count,
2063 positional_args, kw_only_args, argtuple_error_label, code)
2065 # --- optimised code when we do not receive any keyword arguments
2066 if (self.num_required_kw_args and min_positional_args > 0) or min_positional_args == max_positional_args:
2067 # Python raises arg tuple related errors first, so we must
2068 # check the length here
2069 if min_positional_args == max_positional_args and not self.star_arg:
2070 compare = '!='
2071 else:
2072 compare = '<'
2073 code.putln('} else if (PyTuple_GET_SIZE(%s) %s %d) {' % (
2074 Naming.args_cname, compare, min_positional_args))
2075 code.put_goto(argtuple_error_label)
2077 if self.num_required_kw_args:
2078 # pure error case: keywords required but not passed
2079 if max_positional_args > min_positional_args and not self.star_arg:
2080 code.putln('} else if (PyTuple_GET_SIZE(%s) > %d) {' % (
2081 Naming.args_cname, max_positional_args))
2082 code.put_goto(argtuple_error_label)
2083 code.putln('} else {')
2084 for i, arg in enumerate(kw_only_args):
2085 if not arg.default:
2086 # required keyword-only argument missing
2087 code.put('__Pyx_RaiseKeywordRequired("%s", %s); ' % (
2088 self.name.utf8encode(),
2089 arg.name_entry.pystring_cname))
2090 code.putln(code.error_goto(self.pos))
2091 break
2093 elif min_positional_args == max_positional_args:
2094 # parse the exact number of positional arguments from the
2095 # args tuple
2096 code.putln('} else {')
2097 for i, arg in enumerate(positional_args):
2098 item = "PyTuple_GET_ITEM(%s, %d)" % (Naming.args_cname, i)
2099 self.generate_arg_assignment(arg, item, code)
2100 self.generate_arg_default_assignments(code)
2102 else:
2103 # parse the positional arguments from the variable length
2104 # args tuple
2105 code.putln('} else {')
2106 self.generate_arg_default_assignments(code)
2107 code.putln('switch (PyTuple_GET_SIZE(%s)) {' % Naming.args_cname)
2108 if self.star_arg:
2109 code.putln('default:')
2110 reversed_args = list(enumerate(positional_args))[::-1]
2111 for i, arg in reversed_args:
2112 if i >= min_positional_args-1:
2113 if min_positional_args > 1:
2114 code.putln('case %2d:' % (i+1)) # pure code beautification
2115 else:
2116 code.put('case %2d: ' % (i+1))
2117 item = "PyTuple_GET_ITEM(%s, %d)" % (Naming.args_cname, i)
2118 self.generate_arg_assignment(arg, item, code)
2119 if min_positional_args == 0:
2120 code.put('case 0: ')
2121 code.putln('break;')
2122 if self.star_arg:
2123 if min_positional_args:
2124 for i in range(min_positional_args-1, -1, -1):
2125 code.putln('case %2d:' % i)
2126 code.put_goto(argtuple_error_label)
2127 else:
2128 code.put('default: ')
2129 code.put_goto(argtuple_error_label)
2130 code.putln('}')
2132 code.putln('}')
2134 if code.label_used(argtuple_error_label):
2135 code.put_goto(success_label)
2136 code.put_label(argtuple_error_label)
2137 code.put('__Pyx_RaiseArgtupleInvalid("%s", %d, %d, %d, PyTuple_GET_SIZE(%s)); ' % (
2138 self.name.utf8encode(), has_fixed_positional_count,
2139 min_positional_args, max_positional_args,
2140 Naming.args_cname))
2141 code.putln(code.error_goto(self.pos))
2143 def generate_arg_default_assignments(self, code):
2144 for arg in self.args:
2145 if arg.is_generic and arg.default:
2146 code.putln(
2147 "%s = %s;" % (
2148 arg.entry.cname,
2149 arg.default_result_code))
2151 def generate_stararg_init_code(self, max_positional_args, code):
2152 if self.starstar_arg:
2153 self.starstar_arg.entry.xdecref_cleanup = 0
2154 code.putln('%s = PyDict_New(); if (unlikely(!%s)) return %s;' % (
2155 self.starstar_arg.entry.cname,
2156 self.starstar_arg.entry.cname,
2157 self.error_value()))
2158 code.put_gotref(self.starstar_arg.entry.cname)
2159 if self.star_arg:
2160 self.star_arg.entry.xdecref_cleanup = 0
2161 code.putln('if (PyTuple_GET_SIZE(%s) > %d) {' % (
2162 Naming.args_cname,
2163 max_positional_args))
2164 code.put('%s = PyTuple_GetSlice(%s, %d, PyTuple_GET_SIZE(%s)); ' % (
2165 self.star_arg.entry.cname, Naming.args_cname,
2166 max_positional_args, Naming.args_cname))
2167 code.put_gotref(self.star_arg.entry.cname)
2168 if self.starstar_arg:
2169 code.putln("")
2170 code.putln("if (unlikely(!%s)) {" % self.star_arg.entry.cname)
2171 code.put_decref(self.starstar_arg.entry.cname, py_object_type)
2172 code.putln('return %s;' % self.error_value())
2173 code.putln('}')
2174 else:
2175 code.putln("if (unlikely(!%s)) return %s;" % (
2176 self.star_arg.entry.cname, self.error_value()))
2177 code.putln('} else {')
2178 code.put("%s = %s; " % (self.star_arg.entry.cname, Naming.empty_tuple))
2179 code.put_incref(Naming.empty_tuple, py_object_type)
2180 code.putln('}')
2182 def generate_keyword_unpacking_code(self, min_positional_args, max_positional_args,
2183 has_fixed_positional_count, positional_args,
2184 kw_only_args, argtuple_error_label, code):
2185 all_args = tuple(positional_args) + tuple(kw_only_args)
2186 max_args = len(all_args)
2188 default_args = []
2189 for arg in all_args:
2190 if arg.default and arg.type.is_pyobject:
2191 default_value = arg.default_result_code
2192 if arg.type is not PyrexTypes.py_object_type:
2193 default_value = "(PyObject*)"+default_value
2194 default_args.append(default_value)
2195 else:
2196 default_args.append('0')
2197 code.putln("PyObject* values[%d] = {%s};" % (
2198 max_args, ', '.join(default_args)))
2199 code.putln("Py_ssize_t kw_args = PyDict_Size(%s);" %
2200 Naming.kwds_cname)
2202 # parse the tuple and check that it's not too long
2203 code.putln('switch (PyTuple_GET_SIZE(%s)) {' % Naming.args_cname)
2204 if self.star_arg:
2205 code.putln('default:')
2206 for i in range(max_positional_args-1, -1, -1):
2207 code.put('case %2d: ' % (i+1))
2208 code.putln("values[%d] = PyTuple_GET_ITEM(%s, %d);" % (
2209 i, Naming.args_cname, i))
2210 code.putln('case 0: break;')
2211 if not self.star_arg:
2212 code.put('default: ') # more arguments than allowed
2213 code.put_goto(argtuple_error_label)
2214 code.putln('}')
2216 # now fill up the required arguments with values from the kw dict
2217 if self.num_required_args:
2218 last_required_arg = -1
2219 for i, arg in enumerate(all_args):
2220 if not arg.default:
2221 last_required_arg = i
2222 if max_positional_args > 0:
2223 code.putln('switch (PyTuple_GET_SIZE(%s)) {' % Naming.args_cname)
2224 for i, arg in enumerate(all_args[:last_required_arg+1]):
2225 if max_positional_args > 0 and i <= max_positional_args:
2226 if self.star_arg and i == max_positional_args:
2227 code.putln('default:')
2228 else:
2229 code.putln('case %2d:' % i)
2230 if arg.default:
2231 # handled in ParseOptionalKeywords() below
2232 continue
2233 code.putln('values[%d] = PyDict_GetItem(%s, %s);' % (
2234 i, Naming.kwds_cname, arg.name_entry.pystring_cname))
2235 code.putln('if (likely(values[%d])) kw_args--;' % i);
2236 if i < min_positional_args:
2237 if i == 0:
2238 # special case: we know arg 0 is missing
2239 code.put('else ')
2240 code.put_goto(argtuple_error_label)
2241 else:
2242 # print the correct number of values (args or
2243 # kwargs) that were passed into positional
2244 # arguments up to this point
2245 code.putln('else {')
2246 code.put('__Pyx_RaiseArgtupleInvalid("%s", %d, %d, %d, %d); ' % (
2247 self.name.utf8encode(), has_fixed_positional_count,
2248 min_positional_args, max_positional_args, i))
2249 code.putln(code.error_goto(self.pos))
2250 code.putln('}')
2251 elif arg.kw_only:
2252 code.putln('else {')
2253 code.put('__Pyx_RaiseKeywordRequired("%s", %s); ' %(
2254 self.name.utf8encode(), arg.name_entry.pystring_cname))
2255 code.putln(code.error_goto(self.pos))
2256 code.putln('}')
2257 if max_positional_args > 0:
2258 code.putln('}')
2260 code.putln('if (unlikely(kw_args > 0)) {')
2261 # non-positional/-required kw args left in dict: default args, **kwargs or error
2262 if max_positional_args == 0:
2263 pos_arg_count = "0"
2264 elif self.star_arg:
2265 code.putln("const Py_ssize_t used_pos_args = (PyTuple_GET_SIZE(%s) < %d) ? PyTuple_GET_SIZE(%s) : %d;" % (
2266 Naming.args_cname, max_positional_args,
2267 Naming.args_cname, max_positional_args))
2268 pos_arg_count = "used_pos_args"
2269 else:
2270 pos_arg_count = "PyTuple_GET_SIZE(%s)" % Naming.args_cname
2271 code.globalstate.use_utility_code(parse_keywords_utility_code)
2272 code.put(
2273 'if (unlikely(__Pyx_ParseOptionalKeywords(%s, %s, %s, values, %s, "%s") < 0)) ' % (
2274 Naming.kwds_cname,
2275 Naming.pykwdlist_cname,
2276 self.starstar_arg and self.starstar_arg.entry.cname or '0',
2277 pos_arg_count,
2278 self.name.utf8encode()))
2279 code.putln(code.error_goto(self.pos))
2280 code.putln('}')
2282 # convert arg values to their final type and assign them
2283 for i, arg in enumerate(all_args):
2284 if arg.default and not arg.type.is_pyobject:
2285 code.putln("if (values[%d]) {" % i)
2286 self.generate_arg_assignment(arg, "values[%d]" % i, code)
2287 if arg.default and not arg.type.is_pyobject:
2288 code.putln('} else {')
2289 code.putln(
2290 "%s = %s;" % (
2291 arg.entry.cname,
2292 arg.default_result_code))
2293 code.putln('}')
2295 def generate_argument_conversion_code(self, code):
2296 # Generate code to convert arguments from
2297 # signature type to declared type, if needed.
2298 for arg in self.args:
2299 if arg.needs_conversion:
2300 self.generate_arg_conversion(arg, code)
2302 def generate_arg_conversion(self, arg, code):
2303 # Generate conversion code for one argument.
2304 old_type = arg.hdr_type
2305 new_type = arg.type
2306 if old_type.is_pyobject:
2307 if arg.default:
2308 code.putln("if (%s) {" % arg.hdr_cname)
2309 else:
2310 code.putln("assert(%s); {" % arg.hdr_cname)
2311 self.generate_arg_conversion_from_pyobject(arg, code)
2312 code.putln("}")
2313 elif new_type.is_pyobject:
2314 self.generate_arg_conversion_to_pyobject(arg, code)
2315 else:
2316 if new_type.assignable_from(old_type):
2317 code.putln(
2318 "%s = %s;" % (arg.entry.cname, arg.hdr_cname))
2319 else:
2320 error(arg.pos,
2321 "Cannot convert 1 argument from '%s' to '%s'" %
2322 (old_type, new_type))
2324 def generate_arg_conversion_from_pyobject(self, arg, code):
2325 new_type = arg.type
2326 func = new_type.from_py_function
2327 # copied from CoerceFromPyTypeNode
2328 if func:
2329 code.putln("%s = %s(%s); %s" % (
2330 arg.entry.cname,
2331 func,
2332 arg.hdr_cname,
2333 code.error_goto_if(new_type.error_condition(arg.entry.cname), arg.pos)))
2334 else:
2335 error(arg.pos,
2336 "Cannot convert Python object argument to type '%s'"
2337 % new_type)
2339 def generate_arg_conversion_to_pyobject(self, arg, code):
2340 old_type = arg.hdr_type
2341 func = old_type.to_py_function
2342 if func:
2343 code.putln("%s = %s(%s); %s" % (
2344 arg.entry.cname,
2345 func,
2346 arg.hdr_cname,
2347 code.error_goto_if_null(arg.entry.cname, arg.pos)))
2348 code.put_var_gotref(arg.entry)
2349 else:
2350 error(arg.pos,
2351 "Cannot convert argument of type '%s' to Python object"
2352 % old_type)
2354 def generate_argument_type_tests(self, code):
2355 # Generate type tests for args whose signature
2356 # type is PyObject * and whose declared type is
2357 # a subtype thereof.
2358 for arg in self.args:
2359 if arg.needs_type_test:
2360 self.generate_arg_type_test(arg, code)
2362 def generate_arg_type_test(self, arg, code):
2363 # Generate type test for one argument.
2364 if arg.type.typeobj_is_available():
2365 typeptr_cname = arg.type.typeptr_cname
2366 arg_code = "((PyObject *)%s)" % arg.entry.cname
2367 code.putln(
2368 'if (unlikely(!__Pyx_ArgTypeTest(%s, %s, %d, "%s", %s))) %s' % (
2369 arg_code,
2370 typeptr_cname,
2371 not arg.not_none,
2372 arg.name,
2373 arg.type.is_builtin_type,
2374 code.error_goto(arg.pos)))
2375 else:
2376 error(arg.pos, "Cannot test type of extern C class "
2377 "without type object name specification")
2379 def error_value(self):
2380 return self.entry.signature.error_value
2382 def caller_will_check_exceptions(self):
2383 return 1
2385 class OverrideCheckNode(StatNode):
2386 # A Node for dispatching to the def method if it
2387 # is overriden.
2388 #
2389 # py_func
2390 #
2391 # args
2392 # func_temp
2393 # body
2395 child_attrs = ['body']
2397 body = None
2399 def analyse_expressions(self, env):
2400 self.args = env.arg_entries
2401 if self.py_func.is_module_scope:
2402 first_arg = 0
2403 else:
2404 first_arg = 1
2405 import ExprNodes
2406 self.func_node = ExprNodes.PyTempNode(self.pos, env)
2407 call_tuple = ExprNodes.TupleNode(self.pos, args=[ExprNodes.NameNode(self.pos, name=arg.name) for arg in self.args[first_arg:]])
2408 call_node = ExprNodes.SimpleCallNode(self.pos,
2409 function=self.func_node,
2410 args=[ExprNodes.NameNode(self.pos, name=arg.name) for arg in self.args[first_arg:]])
2411 self.body = ReturnStatNode(self.pos, value=call_node)
2412 self.body.analyse_expressions(env)
2414 def generate_execution_code(self, code):
2415 # Check to see if we are an extension type
2416 if self.py_func.is_module_scope:
2417 self_arg = "((PyObject *)%s)" % Naming.module_cname
2418 else:
2419 self_arg = "((PyObject *)%s)" % self.args[0].cname
2420 code.putln("/* Check if called by wrapper */")
2421 code.putln("if (unlikely(%s)) ;" % Naming.skip_dispatch_cname)
2422 code.putln("/* Check if overriden in Python */")
2423 if self.py_func.is_module_scope:
2424 code.putln("else {")
2425 else:
2426 code.putln("else if (unlikely(Py_TYPE(%s)->tp_dictoffset != 0)) {" % self_arg)
2427 err = code.error_goto_if_null(self.func_node.result(), self.pos)
2428 # need to get attribute manually--scope would return cdef method
2429 code.putln("%s = PyObject_GetAttr(%s, %s); %s" % (self.func_node.result(), self_arg, self.py_func.interned_attr_cname, err))
2430 code.put_gotref(self.func_node.py_result())
2431 # It appears that this type is not anywhere exposed in the Python/C API
2432 is_builtin_function_or_method = '(strcmp(Py_TYPE(%s)->tp_name, "builtin_function_or_method") == 0)' % self.func_node.result()
2433 is_overridden = '(PyCFunction_GET_FUNCTION(%s) != (void *)&%s)' % (self.func_node.result(), self.py_func.entry.func_cname)
2434 code.putln('if (!%s || %s) {' % (is_builtin_function_or_method, is_overridden))
2435 self.body.generate_execution_code(code)
2436 code.putln('}')
2437 code.put_decref_clear(self.func_node.result(), PyrexTypes.py_object_type)
2438 code.putln("}")
2440 class ClassDefNode(StatNode, BlockNode):
2441 pass
2443 class PyClassDefNode(ClassDefNode):
2444 # A Python class definition.
2445 #
2446 # name EncodedString Name of the class
2447 # doc string or None
2448 # body StatNode Attribute definition code
2449 # entry Symtab.Entry
2450 # scope PyClassScope
2451 #
2452 # The following subnodes are constructed internally:
2453 #
2454 # dict DictNode Class dictionary
2455 # classobj ClassNode Class object
2456 # target NameNode Variable to assign class object to
2458 child_attrs = ["body", "dict", "classobj", "target"]
2460 def __init__(self, pos, name, bases, doc, body):
2461 StatNode.__init__(self, pos)
2462 self.name = name
2463 self.doc = doc
2464 self.body = body
2465 import ExprNodes
2466 self.dict = ExprNodes.DictNode(pos, key_value_pairs = [])
2467 if self.doc and Options.docstrings:
2468 doc = embed_position(self.pos, self.doc)
2469 doc_node = ExprNodes.StringNode(pos, value = doc)
2470 else:
2471 doc_node = None
2472 self.classobj = ExprNodes.ClassNode(pos, name = name,
2473 bases = bases, dict = self.dict, doc = doc_node)
2474 self.target = ExprNodes.NameNode(pos, name = name)
2476 def as_cclass(self):
2477 """
2478 Return this node as if it were declared as an extension class
2479 """
2480 bases = self.classobj.bases.args
2481 if len(bases) == 0:
2482 base_class_name = None
2483 base_class_module = None
2484 elif len(bases) == 1:
2485 base = bases[0]
2486 path = []
2487 from ExprNodes import AttributeNode, NameNode
2488 while isinstance(base, AttributeNode):
2489 path.insert(0, base.attribute)
2490 base = base.obj
2491 if isinstance(base, NameNode):
2492 path.insert(0, base.name)
2493 base_class_name = path[-1]
2494 if len(path) > 1:
2495 base_class_module = u'.'.join(path[:-1])
2496 else:
2497 base_class_module = None
2498 else:
2499 error(self.classobj.bases.args.pos, "Invalid base class")
2500 else:
2501 error(self.classobj.bases.args.pos, "C class may only have one base class")
2502 return None
2504 return CClassDefNode(self.pos,
2505 visibility = 'private',
2506 module_name = None,
2507 class_name = self.name,
2508 base_class_module = base_class_module,
2509 base_class_name = base_class_name,
2510 body = self.body,
2511 in_pxd = False,
2512 doc = self.doc)
2514 def create_scope(self, env):
2515 genv = env
2516 while env.is_py_class_scope or env.is_c_class_scope:
2517 env = env.outer_scope
2518 cenv = self.scope = PyClassScope(name = self.name, outer_scope = genv)
2519 return cenv
2521 def analyse_declarations(self, env):
2522 self.target.analyse_target_declaration(env)
2523 cenv = self.create_scope(env)
2524 cenv.class_obj_cname = self.target.entry.cname
2525 self.body.analyse_declarations(cenv)
2527 def analyse_expressions(self, env):
2528 self.dict.analyse_expressions(env)
2529 self.classobj.analyse_expressions(env)
2530 genv = env.global_scope()
2531 cenv = self.scope
2532 cenv.class_dict_cname = self.dict.result()
2533 cenv.namespace_cname = cenv.class_obj_cname = self.classobj.result()
2534 self.body.analyse_expressions(cenv)
2535 self.target.analyse_target_expression(env, self.classobj)
2536 self.dict.release_temp(env)
2537 #self.classobj.release_temp(env)
2538 #self.target.release_target_temp(env)
2540 def generate_function_definitions(self, env, code):
2541 self.generate_py_string_decls(self.scope, code)
2542 self.body.generate_function_definitions(self.scope, code)
2544 def generate_execution_code(self, code):
2545 self.dict.generate_evaluation_code(code)
2546 self.classobj.generate_evaluation_code(code)
2547 self.body.generate_execution_code(code)
2548 self.target.generate_assignment_code(self.classobj, code)
2549 self.dict.generate_disposal_code(code)
2550 self.dict.free_temps(code)
2553 class CClassDefNode(ClassDefNode):
2554 # An extension type definition.
2555 #
2556 # visibility 'private' or 'public' or 'extern'
2557 # typedef_flag boolean
2558 # api boolean
2559 # module_name string or None For import of extern type objects
2560 # class_name string Unqualified name of class
2561 # as_name string or None Name to declare as in this scope
2562 # base_class_module string or None Module containing the base class
2563 # base_class_name string or None Name of the base class
2564 # objstruct_name string or None Specified C name of object struct
2565 # typeobj_name string or None Specified C name of type object
2566 # in_pxd boolean Is in a .pxd file
2567 # doc string or None
2568 # body StatNode or None
2569 # entry Symtab.Entry
2570 # base_type PyExtensionType or None
2571 # buffer_defaults_node DictNode or None Declares defaults for a buffer
2572 # buffer_defaults_pos
2574 child_attrs = ["body"]
2575 buffer_defaults_node = None
2576 buffer_defaults_pos = None
2577 typedef_flag = False
2578 api = False
2579 objstruct_name = None
2580 typeobj_name = None
2582 def analyse_declarations(self, env):
2583 #print "CClassDefNode.analyse_declarations:", self.class_name
2584 #print "...visibility =", self.visibility
2585 #print "...module_name =", self.module_name
2587 import Buffer
2588 if self.buffer_defaults_node:
2589 buffer_defaults = Buffer.analyse_buffer_options(self.buffer_defaults_pos,
2590 env, [], self.buffer_defaults_node,
2591 need_complete=False)
2592 else:
2593 buffer_defaults = None
2595 if env.in_cinclude and not self.objstruct_name:
2596 error(self.pos, "Object struct name specification required for "
2597 "C class defined in 'extern from' block")
2598 self.base_type = None
2599 # Now that module imports are cached, we need to
2600 # import the modules for extern classes.
2601 if self.module_name:
2602 self.module = None
2603 for module in env.cimported_modules:
2604 if module.name == self.module_name:
2605 self.module = module
2606 if self.module is None:
2607 self.module = ModuleScope(self.module_name, None, env.context)
2608 self.module.has_extern_class = 1
2609 env.add_imported_module(self.module)
2611 if self.base_class_name:
2612 if self.base_class_module:
2613 base_class_scope = env.find_module(self.base_class_module, self.pos)
2614 else:
2615 base_class_scope = env
2616 if self.base_class_name == 'object':
2617 # extension classes are special and don't need to inherit from object
2618 if base_class_scope is None or base_class_scope.lookup('object') is None:
2619 self.base_class_name = None
2620 self.base_class_module = None
2621 base_class_scope = None
2622 if base_class_scope:
2623 base_class_entry = base_class_scope.find(self.base_class_name, self.pos)
2624 if base_class_entry:
2625 if not base_class_entry.is_type:
2626 error(self.pos, "'%s' is not a type name" % self.base_class_name)
2627 elif not base_class_entry.type.is_extension_type:
2628 error(self.pos, "'%s' is not an extension type" % self.base_class_name)
2629 elif not base_class_entry.type.is_complete():
2630 error(self.pos, "Base class '%s' is incomplete" % self.base_class_name)
2631 else:
2632 self.base_type = base_class_entry.type
2633 has_body = self.body is not None
2634 if self.module_name and self.visibility != 'extern':
2635 module_path = self.module_name.split(".")
2636 home_scope = env.find_imported_module(module_path, self.pos)
2637 if not home_scope:
2638 return
2639 else:
2640 home_scope = env
2641 self.entry = home_scope.declare_c_class(
2642 name = self.class_name,
2643 pos = self.pos,
2644 defining = has_body and self.in_pxd,
2645 implementing = has_body and not self.in_pxd,
2646 module_name = self.module_name,
2647 base_type = self.base_type,
2648 objstruct_cname = self.objstruct_name,
2649 typeobj_cname = self.typeobj_name,
2650 visibility = self.visibility,
2651 typedef_flag = self.typedef_flag,
2652 api = self.api,
2653 buffer_defaults = buffer_defaults)
2654 if home_scope is not env and self.visibility == 'extern':
2655 env.add_imported_entry(self.class_name, self.entry, pos)
2656 scope = self.entry.type.scope
2658 if self.doc and Options.docstrings:
2659 scope.doc = embed_position(self.pos, self.doc)
2661 if has_body:
2662 self.body.analyse_declarations(scope)
2663 if self.in_pxd:
2664 scope.defined = 1
2665 else:
2666 scope.implemented = 1
2667 env.allocate_vtable_names(self.entry)
2669 def analyse_expressions(self, env):
2670 if self.body:
2671 scope = self.entry.type.scope
2672 self.body.analyse_expressions(scope)
2674 def generate_function_definitions(self, env, code):
2675 self.generate_py_string_decls(self.entry.type.scope, code)
2676 if self.body:
2677 self.body.generate_function_definitions(
2678 self.entry.type.scope, code)
2680 def generate_execution_code(self, code):
2681 # This is needed to generate evaluation code for
2682 # default values of method arguments.
2683 if self.body:
2684 self.body.generate_execution_code(code)
2686 def annotate(self, code):
2687 if self.body:
2688 self.body.annotate(code)
2691 class PropertyNode(StatNode):
2692 # Definition of a property in an extension type.
2693 #
2694 # name string
2695 # doc EncodedString or None Doc string
2696 # body StatListNode
2698 child_attrs = ["body"]
2700 def analyse_declarations(self, env):
2701 entry = env.declare_property(self.name, self.doc, self.pos)
2702 if entry:
2703 if self.doc and Options.docstrings:
2704 doc_entry = env.get_string_const(
2705 self.doc, identifier = False)
2706 entry.doc_cname = doc_entry.cname
2707 self.body.analyse_declarations(entry.scope)
2709 def analyse_expressions(self, env):
2710 self.body.analyse_expressions(env)
2712 def generate_function_definitions(self, env, code):
2713 self.body.generate_function_definitions(env, code)
2715 def generate_execution_code(self, code):
2716 pass
2718 def annotate(self, code):
2719 self.body.annotate(code)
2722 class GlobalNode(StatNode):
2723 # Global variable declaration.
2724 #
2725 # names [string]
2727 child_attrs = []
2729 def analyse_declarations(self, env):
2730 for name in self.names:
2731 env.declare_global(name, self.pos)
2733 def analyse_expressions(self, env):
2734 pass
2736 def generate_execution_code(self, code):
2737 pass
2740 class ExprStatNode(StatNode):
2741 # Expression used as a statement.
2742 #
2743 # expr ExprNode
2745 child_attrs = ["expr"]
2747 def analyse_declarations(self, env):
2748 import ExprNodes
2749 if isinstance(self.expr, ExprNodes.GeneralCallNode):
2750 func = self.expr.function.as_cython_attribute()
2751 if func == u'declare':
2752 args, kwds = self.expr.explicit_args_kwds()
2753 if len(args):
2754 error(self.expr.pos, "Variable names must be specified.")
2755 for var, type_node in kwds.key_value_pairs:
2756 type = type_node.analyse_as_type(env)
2757 if type is None:
2758 error(type_node.pos, "Unknown type")
2759 else:
2760 env.declare_var(var.value, type, var.pos, is_cdef = True)
2761 self.__class__ = PassStatNode
2763 def analyse_expressions(self, env):
2764 self.expr.analyse_expressions(env)
2765 self.expr.release_temp(env)
2767 def generate_execution_code(self, code):
2768 self.expr.generate_evaluation_code(code)
2769 if not self.expr.is_temp and self.expr.result():
2770 code.putln("%s;" % self.expr.result())
2771 self.expr.generate_disposal_code(code)
2772 self.expr.free_temps(code)
2774 def annotate(self, code):
2775 self.expr.annotate(code)
2778 class AssignmentNode(StatNode):
2779 # Abstract base class for assignment nodes.
2780 #
2781 # The analyse_expressions and generate_execution_code
2782 # phases of assignments are split into two sub-phases
2783 # each, to enable all the right hand sides of a
2784 # parallel assignment to be evaluated before assigning
2785 # to any of the left hand sides.
2787 def analyse_expressions(self, env):
2788 self.analyse_types(env)
2789 self.allocate_rhs_temps(env)
2790 self.allocate_lhs_temps(env)
2792 # def analyse_expressions(self, env):
2793 # self.analyse_expressions_1(env)
2794 # self.analyse_expressions_2(env)
2796 def generate_execution_code(self, code):
2797 self.generate_rhs_evaluation_code(code)
2798 self.generate_assignment_code(code)
2801 class SingleAssignmentNode(AssignmentNode):
2802 # The simplest case:
2803 #
2804 # a = b
2805 #
2806 # lhs ExprNode Left hand side
2807 # rhs ExprNode Right hand side
2808 # first bool Is this guaranteed the first assignment to lhs?
2810 child_attrs = ["lhs", "rhs"]
2811 first = False
2812 declaration_only = False
2814 def analyse_declarations(self, env):
2815 import ExprNodes
2817 # handle declarations of the form x = cython.foo()
2818 if isinstance(self.rhs, ExprNodes.CallNode):
2819 func_name = self.rhs.function.as_cython_attribute()
2820 if func_name:
2821 args, kwds = self.rhs.explicit_args_kwds()
2823 if func_name in ['declare', 'typedef']:
2824 if len(args) > 2 or kwds is not None:
2825 error(rhs.pos, "Can only declare one type at a time.")
2826 return
2827 type = args[0].analyse_as_type(env)
2828 if type is None:
2829 error(args[0].pos, "Unknown type")
2830 return
2831 lhs = self.lhs
2832 if func_name == 'declare':
2833 if isinstance(lhs, ExprNodes.NameNode):
2834 vars = [(lhs.name, lhs.pos)]
2835 elif isinstance(lhs, ExprNodes.TupleNode):
2836 vars = [(var.name, var.pos) for var in lhs.args]
2837 else:
2838 error(lhs.pos, "Invalid declaration")
2839 return
2840 for var, pos in vars:
2841 env.declare_var(var, type, pos, is_cdef = True)
2842 if len(args) == 2:
2843 # we have a value
2844 self.rhs = args[1]
2845 else:
2846 self.declaration_only = True
2847 else:
2848 self.declaration_only = True
2849 if not isinstance(lhs, ExprNodes.NameNode):
2850 error(lhs.pos, "Invalid declaration.")
2851 env.declare_typedef(lhs.name, type, self.pos, visibility='private')
2853 elif func_name in ['struct', 'union']:
2854 self.declaration_only = True
2855 if len(args) > 0 or kwds is None:
2856 error(rhs.pos, "Struct or union members must be given by name.")
2857 return
2858 members = []
2859 for member, type_node in kwds.key_value_pairs:
2860 type = type_node.analyse_as_type(env)
2861 if type is None:
2862 error(type_node.pos, "Unknown type")
2863 else:
2864 members.append((member.value, type, member.pos))
2865 if len(members) < len(kwds.key_value_pairs):
2866 return
2867 if not isinstance(self.lhs, ExprNodes.NameNode):
2868 error(self.lhs.pos, "Invalid declaration.")
2869 name = self.lhs.name
2870 scope = StructOrUnionScope(name)
2871 env.declare_struct_or_union(name, func_name, scope, False, self.rhs.pos)
2872 for member, type, pos in members:
2873 scope.declare_var(member, type, pos)
2875 if self.declaration_only:
2876 return
2877 else:
2878 self.lhs.analyse_target_declaration(env)
2880 def analyse_types(self, env, use_temp = 0):
2881 self.rhs.analyse_types(env)
2882 self.lhs.analyse_target_types(env)
2883 self.lhs.gil_assignment_check(env)
2884 self.rhs = self.rhs.coerce_to(self.lhs.type, env)
2885 if use_temp:
2886 self.rhs = self.rhs.coerce_to_temp(env)
2888 def allocate_rhs_temps(self, env):
2889 self.rhs.allocate_temps(env)
2891 def allocate_lhs_temps(self, env):
2892 self.lhs.allocate_target_temps(env, self.rhs)
2893 #self.lhs.release_target_temp(env)
2894 #self.rhs.release_temp(env)
2896 def generate_rhs_evaluation_code(self, code):
2897 self.rhs.generate_evaluation_code(code)
2899 def generate_assignment_code(self, code):
2900 self.lhs.generate_assignment_code(self.rhs, code)
2902 def annotate(self, code):
2903 self.lhs.annotate(code)
2904 self.rhs.annotate(code)
2907 class CascadedAssignmentNode(AssignmentNode):
2908 # An assignment with multiple left hand sides:
2909 #
2910 # a = b = c
2911 #
2912 # lhs_list [ExprNode] Left hand sides
2913 # rhs ExprNode Right hand sides
2914 #
2915 # Used internally:
2916 #
2917 # coerced_rhs_list [ExprNode] RHS coerced to type of each LHS
2919 child_attrs = ["lhs_list", "rhs", "coerced_rhs_list"]
2920 coerced_rhs_list = None
2922 def analyse_declarations(self, env):
2923 for lhs in self.lhs_list:
2924 lhs.analyse_target_declaration(env)
2926 def analyse_types(self, env, use_temp = 0):
2927 self.rhs.analyse_types(env)
2928 if use_temp:
2929 self.rhs = self.rhs.coerce_to_temp(env)
2930 else:
2931 self.rhs = self.rhs.coerce_to_simple(env)
2932 from ExprNodes import CloneNode
2933 self.coerced_rhs_list = []
2934 for lhs in self.lhs_list:
2935 lhs.analyse_target_types(env)
2936 lhs.gil_assignment_check(env)
2937 rhs = CloneNode(self.rhs)
2938 rhs = rhs.coerce_to(lhs.type, env)
2939 self.coerced_rhs_list.append(rhs)
2941 def allocate_rhs_temps(self, env):
2942 self.rhs.allocate_temps(env)
2944 def allocate_lhs_temps(self, env):
2945 for lhs, rhs in zip(self.lhs_list, self.coerced_rhs_list):
2946 rhs.allocate_temps(env)
2947 lhs.allocate_target_temps(env, rhs)
2948 #lhs.release_target_temp(env)
2949 #rhs.release_temp(env)
2950 self.rhs.release_temp(env)
2952 def generate_rhs_evaluation_code(self, code):
2953 self.rhs.generate_evaluation_code(code)
2955 def generate_assignment_code(self, code):
2956 for i in range(len(self.lhs_list)):
2957 lhs = self.lhs_list[i]
2958 rhs = self.coerced_rhs_list[i]
2959 rhs.generate_evaluation_code(code)
2960 lhs.generate_assignment_code(rhs, code)
2961 # Assignment has disposed of the cloned RHS
2962 self.rhs.generate_disposal_code(code)
2963 self.rhs.free_temps(code)
2965 def annotate(self, code):
2966 for i in range(len(self.lhs_list)):
2967 lhs = self.lhs_list[i].annotate(code)
2968 rhs = self.coerced_rhs_list[i].annotate(code)
2969 self.rhs.annotate(code)
2972 class ParallelAssignmentNode(AssignmentNode):
2973 # A combined packing/unpacking assignment:
2974 #
2975 # a, b, c = d, e, f
2976 #
2977 # This has been rearranged by the parser into
2978 #
2979 # a = d ; b = e ; c = f
2980 #
2981 # but we must evaluate all the right hand sides
2982 # before assigning to any of the left hand sides.
2983 #
2984 # stats [AssignmentNode] The constituent assignments
2986 child_attrs = ["stats"]
2988 def analyse_declarations(self, env):
2989 for stat in self.stats:
2990 stat.analyse_declarations(env)
2992 def analyse_expressions(self, env):
2993 for stat in self.stats:
2994 stat.analyse_types(env, use_temp = 1)
2995 stat.allocate_rhs_temps(env)
2996 for stat in self.stats:
2997 stat.allocate_lhs_temps(env)
2999 # def analyse_expressions(self, env):
3000 # for stat in self.stats:
3001 # stat.analyse_expressions_1(env, use_temp = 1)
3002 # for stat in self.stats:
3003 # stat.analyse_expressions_2(env)
3005 def generate_execution_code(self, code):
3006 for stat in self.stats:
3007 stat.generate_rhs_evaluation_code(code)
3008 for stat in self.stats:
3009 stat.generate_assignment_code(code)
3011 def annotate(self, code):
3012 for stat in self.stats:
3013 stat.annotate(code)
3016 class InPlaceAssignmentNode(AssignmentNode):
3017 # An in place arithmatic operand:
3018 #
3019 # a += b
3020 # a -= b
3021 # ...
3022 #
3023 # lhs ExprNode Left hand side
3024 # rhs ExprNode Right hand side
3025 # op char one of "+-*/%^&|"
3026 # dup (ExprNode) copy of lhs used for operation (auto-generated)
3027 #
3028 # This code is a bit tricky because in order to obey Python
3029 # semantics the sub-expressions (e.g. indices) of the lhs must
3030 # not be evaluated twice. So we must re-use the values calculated
3031 # in evaluation phase for the assignment phase as well.
3032 # Fortunately, the type of the lhs node is fairly constrained
3033 # (it must be a NameNode, AttributeNode, or IndexNode).
3035 child_attrs = ["lhs", "rhs"]
3036 dup = None
3038 def analyse_declarations(self, env):
3039 self.lhs.analyse_target_declaration(env)
3041 def analyse_types(self, env):
3042 self.dup = self.create_dup_node(env) # re-assigns lhs to a shallow copy
3043 self.rhs.analyse_types(env)
3044 self.lhs.analyse_target_types(env)
3045 if Options.incref_local_binop and self.dup.type.is_pyobject:
3046 self.dup = self.dup.coerce_to_temp(env)
3048 def allocate_rhs_temps(self, env):
3049 import ExprNodes
3050 if self.lhs.type.is_pyobject:
3051 self.rhs = self.rhs.coerce_to_pyobject(env)
3052 elif self.rhs.type.is_pyobject:
3053 self.rhs = self.rhs.coerce_to(self.lhs.type, env)
3054 if self.lhs.type.is_pyobject:
3055 self.result_value = ExprNodes.PyTempNode(self.pos, env).coerce_to(self.lhs.type, env)
3056 self.result_value.allocate_temps(env)
3057 # if use_temp:
3058 # self.rhs = self.rhs.coerce_to_temp(env)
3059 self.rhs.allocate_temps(env)
3060 self.dup.allocate_subexpr_temps(env)
3061 self.dup.allocate_temp(env)
3063 def allocate_lhs_temps(self, env):
3064 self.lhs.allocate_target_temps(env, self.rhs)
3065 # self.lhs.release_target_temp(env)
3066 self.dup.release_temp(env)
3067 if self.dup.is_temp:
3068 self.dup.release_subexpr_temps(env)
3069 # self.rhs.release_temp(env)
3070 if self.lhs.type.is_pyobject:
3071 self.result_value.release_temp(env)
3073 def generate_execution_code(self, code):
3074 import ExprNodes
3075 self.rhs.generate_evaluation_code(code)
3076 self.dup.generate_subexpr_evaluation_code(code)
3077 if isinstance(self.dup, ExprNodes.NewTempExprNode):
3078 # This is because we're manually messing with subexpr nodes
3079 if self.dup.is_temp:
3080 self.dup.allocate_temp_result(code)
3081 # self.dup.generate_result_code is run only if it is not buffer access
3082 if self.operator == "**":
3083 extra = ", Py_None"
3084 else:
3085 extra = ""
3086 if self.lhs.type.is_pyobject:
3087 if isinstance(self.lhs, ExprNodes.IndexNode) and self.lhs.is_buffer_access:
3088 error(self.pos, "In-place operators not allowed on object buffers in this release.")
3089 self.dup.generate_result_code(code)
3090 code.putln(
3091 "%s = %s(%s, %s%s); %s" % (
3092 self.result_value.result(),
3093 self.py_operation_function(),
3094 self.dup.py_result(),
3095 self.rhs.py_result(),
3096 extra,
3097 code.error_goto_if_null(self.result_value.py_result(), self.pos)))
3098 code.put_gotref(self.result_value.py_result())
3099 self.result_value.generate_evaluation_code(code) # May be a type check...
3100 self.rhs.generate_disposal_code(code)
3101 self.rhs.free_temps(code)
3102 self.dup.generate_disposal_code(code)
3103 self.dup.free_temps(code)
3104 self.lhs.generate_assignment_code(self.result_value, code)
3105 else:
3106 c_op = self.operator
3107 if c_op == "//":
3108 c_op = "/"
3109 elif c_op == "**":
3110 if self.lhs.type.is_int and self.rhs.type.is_int:
3111 error(self.pos, "** with two C int types is ambiguous")
3112 else:
3113 error(self.pos, "No C inplace power operator")
3114 # have to do assignment directly to avoid side-effects
3115 if isinstance(self.lhs, ExprNodes.IndexNode) and self.lhs.is_buffer_access:
3116 self.lhs.generate_buffer_setitem_code(self.rhs, code, c_op)
3117 else:
3118 self.dup.generate_result_code(code)
3119 code.putln("%s %s= %s;" % (self.lhs.result(), c_op, self.rhs.result()) )
3120 self.rhs.generate_disposal_code(code)
3121 self.rhs.free_temps(code)
3122 if self.dup.is_temp:
3123 self.dup.generate_subexpr_disposal_code(code)
3124 self.dup.free_subexpr_temps(code)
3126 def create_dup_node(self, env):
3127 import ExprNodes
3128 self.dup = self.lhs
3129 self.dup.analyse_types(env)
3130 if isinstance(self.lhs, ExprNodes.NameNode):
3131 target_lhs = ExprNodes.NameNode(self.dup.pos,
3132 name = self.dup.name,
3133 is_temp = self.dup.is_temp,
3134 entry = self.dup.entry)
3135 elif isinstance(self.lhs, ExprNodes.AttributeNode):
3136 target_lhs = ExprNodes.AttributeNode(self.dup.pos,
3137 obj = ExprNodes.CloneNode(self.lhs.obj),
3138 attribute = self.dup.attribute,
3139 is_temp = self.dup.is_temp)
3140 elif isinstance(self.lhs, ExprNodes.IndexNode):
3141 if self.lhs.index:
3142 index = ExprNodes.CloneNode(self.lhs.index)
3143 else:
3144 index = None
3145 if self.lhs.indices:
3146 indices = [ExprNodes.CloneNode(x) for x in self.lhs.indices]
3147 else:
3148 indices = []
3149 target_lhs = ExprNodes.IndexNode(self.dup.pos,
3150 base = ExprNodes.CloneNode(self.dup.base),
3151 index = index,
3152 indices = indices,
3153 is_temp = self.dup.is_temp)
3154 else:
3155 assert False
3156 self.lhs = target_lhs
3157 return self.dup
3159 def py_operation_function(self):
3160 return self.py_functions[self.operator]
3162 py_functions = {
3163 "|": "PyNumber_InPlaceOr",
3164 "^": "PyNumber_InPlaceXor",
3165 "&": "PyNumber_InPlaceAnd",
3166 "+": "PyNumber_InPlaceAdd",
3167 "-": "PyNumber_InPlaceSubtract",
3168 "*": "PyNumber_InPlaceMultiply",
3169 "/": "PyNumber_InPlaceDivide",
3170 "%": "PyNumber_InPlaceRemainder",
3171 "<<": "PyNumber_InPlaceLshift",
3172 ">>": "PyNumber_InPlaceRshift",
3173 "**": "PyNumber_InPlacePower",
3174 "//": "PyNumber_InPlaceFloorDivide",
3175 }
3177 def annotate(self, code):
3178 self.lhs.annotate(code)
3179 self.rhs.annotate(code)
3180 self.dup.annotate(code)
3183 class PrintStatNode(StatNode):
3184 # print statement
3185 #
3186 # arg_tuple TupleNode
3187 # append_newline boolean
3189 child_attrs = ["arg_tuple"]
3191 def analyse_expressions(self, env):
3192 self.arg_tuple.analyse_expressions(env)
3193 self.arg_tuple = self.arg_tuple.coerce_to_pyobject(env)
3194 self.arg_tuple.release_temp(env)
3195 env.use_utility_code(printing_utility_code)
3196 self.gil_check(env)
3198 gil_message = "Python print statement"
3200 def generate_execution_code(self, code):
3201 self.arg_tuple.generate_evaluation_code(code)
3202 code.putln(
3203 "if (__Pyx_Print(%s, %d) < 0) %s" % (
3204 self.arg_tuple.py_result(),
3205 self.append_newline,
3206 code.error_goto(self.pos)))
3207 self.arg_tuple.generate_disposal_code(code)
3208 self.arg_tuple.free_temps(code)
3210 def annotate(self, code):
3211 self.arg_tuple.annotate(code)
3214 class ExecStatNode(StatNode):
3215 # exec statement
3216 #
3217 # args [ExprNode]
3219 child_attrs = ["args"]
3221 def analyse_expressions(self, env):
3222 for i, arg in enumerate(self.args):
3223 arg.analyse_expressions(env)
3224 arg = arg.coerce_to_pyobject(env)
3225 arg.release_temp(env)
3226 self.args[i] = arg
3227 self.temp_result = env.allocate_temp_pyobject()
3228 env.release_temp(self.temp_result)
3229 env.use_utility_code(Builtin.pyexec_utility_code)
3230 self.gil_check(env)
3232 gil_message = "Python exec statement"
3234 def generate_execution_code(self, code):
3235 args = []
3236 for arg in self.args:
3237 arg.generate_evaluation_code(code)
3238 args.append( arg.py_result() )
3239 args = tuple(args + ['0', '0'][:3-len(args)])
3240 code.putln("%s = __Pyx_PyRun(%s, %s, %s);" % (
3241 (self.temp_result,) + args))
3242 for arg in self.args:
3243 arg.generate_disposal_code(code)
3244 arg.free_temps(code)
3245 code.putln(
3246 code.error_goto_if_null(self.temp_result, self.pos))
3247 code.put_gotref(self.temp_result)
3248 code.put_decref_clear(self.temp_result, py_object_type)
3250 def annotate(self, code):
3251 for arg in self.args:
3252 arg.annotate(code)
3255 class DelStatNode(StatNode):
3256 # del statement
3257 #
3258 # args [ExprNode]
3260 child_attrs = ["args"]
3262 def analyse_declarations(self, env):
3263 for arg in self.args:
3264 arg.analyse_target_declaration(env)
3266 def analyse_expressions(self, env):
3267 for arg in self.args:
3268 arg.analyse_target_expression(env, None)
3269 if arg.type.is_pyobject:
3270 self.gil_check(env)
3271 else:
3272 error(arg.pos, "Deletion of non-Python object")
3273 #arg.release_target_temp(env)
3275 gil_message = "Deleting Python object"
3277 def generate_execution_code(self, code):
3278 for arg in self.args:
3279 if arg.type.is_pyobject:
3280 arg.generate_deletion_code(code)
3281 # else error reported earlier
3283 def annotate(self, code):
3284 for arg in self.args:
3285 arg.annotate(code)
3288 class PassStatNode(StatNode):
3289 # pass statement
3291 child_attrs = []
3293 def analyse_expressions(self, env):
3294 pass
3296 def generate_execution_code(self, code):
3297 pass
3300 class BreakStatNode(StatNode):
3302 child_attrs = []
3304 def analyse_expressions(self, env):
3305 pass
3307 def generate_execution_code(self, code):
3308 if not code.break_label:
3309 error(self.pos, "break statement not inside loop")
3310 else:
3311 code.put_goto(code.break_label)
3314 class ContinueStatNode(StatNode):
3316 child_attrs = []
3318 def analyse_expressions(self, env):
3319 pass
3321 def generate_execution_code(self, code):
3322 if code.funcstate.in_try_finally:
3323 error(self.pos, "continue statement inside try of try...finally")
3324 elif not code.continue_label:
3325 error(self.pos, "continue statement not inside loop")
3326 else:
3327 code.put_goto(code.continue_label)
3330 class ReturnStatNode(StatNode):
3331 # return statement
3332 #
3333 # value ExprNode or None
3334 # return_type PyrexType
3335 # temps_in_use [Entry] Temps in use at time of return
3337 child_attrs = ["value"]
3339 def analyse_expressions(self, env):
3340 return_type = env.return_type
3341 self.return_type = return_type
3342 self.temps_in_use = env.temps_in_use()
3343 if not return_type:
3344 error(self.pos, "Return not inside a function body")
3345 return
3346 if self.value:
3347 self.value.analyse_types(env)
3348 if return_type.is_void or return_type.is_returncode:
3349 error(self.value.pos,
3350 "Return with value in void function")
3351 else:
3352 self.value = self.value.coerce_to(env.return_type, env)
3353 self.value.allocate_temps(env)
3354 self.value.release_temp(env)
3355 else:
3356 if (not return_type.is_void
3357 and not return_type.is_pyobject
3358 and not return_type.is_returncode):
3359 error(self.pos, "Return value required")
3360 if return_type.is_pyobject:
3361 self.gil_check(env)
3363 gil_message = "Returning Python object"
3365 def generate_execution_code(self, code):
3366 code.mark_pos(self.pos)
3367 if not self.return_type:
3368 # error reported earlier
3369 return
3370 if self.return_type.is_pyobject:
3371 code.put_xdecref(Naming.retval_cname,
3372 self.return_type)
3373 if self.value:
3374 self.value.generate_evaluation_code(code)
3375 self.value.make_owned_reference(code)
3376 code.putln(
3377 "%s = %s;" % (
3378 Naming.retval_cname,
3379 self.value.result_as(self.return_type)))
3380 self.value.generate_post_assignment_code(code)
3381 self.value.free_temps(code)
3382 else:
3383 if self.return_type.is_pyobject:
3384 code.put_init_to_py_none(Naming.retval_cname, self.return_type)
3385 elif self.return_type.is_returncode:
3386 code.putln(
3387 "%s = %s;" % (
3388 Naming.retval_cname,
3389 self.return_type.default_value))
3390 # free temps the old way
3391 for entry in self.temps_in_use:
3392 code.put_var_decref_clear(entry)
3393 # free temps the new way
3394 for cname, type in code.funcstate.temps_holding_reference():
3395 code.put_decref_clear(cname, type)
3396 #code.putln(
3397 # "goto %s;" %
3398 # code.return_label)
3399 code.put_goto(code.return_label)
3401 def annotate(self, code):
3402 if self.value:
3403 self.value.annotate(code)
3406 class RaiseStatNode(StatNode):
3407 # raise statement
3408 #
3409 # exc_type ExprNode or None
3410 # exc_value ExprNode or None
3411 # exc_tb ExprNode or None
3413 child_attrs = ["exc_type", "exc_value", "exc_tb"]
3415 def analyse_expressions(self, env):
3416 if self.exc_type:
3417 self.exc_type.analyse_types(env)
3418 self.exc_type = self.exc_type.coerce_to_pyobject(env)
3419 self.exc_type.allocate_temps(env)
3420 if self.exc_value:
3421 self.exc_value.analyse_types(env)
3422 self.exc_value = self.exc_value.coerce_to_pyobject(env)
3423 self.exc_value.allocate_temps(env)
3424 if self.exc_tb:
3425 self.exc_tb.analyse_types(env)
3426 self.exc_tb = self.exc_tb.coerce_to_pyobject(env)
3427 self.exc_tb.allocate_temps(env)
3428 if self.exc_type:
3429 self.exc_type.release_temp(env)
3430 if self.exc_value:
3431 self.exc_value.release_temp(env)
3432 if self.exc_tb:
3433 self.exc_tb.release_temp(env)
3434 env.use_utility_code(raise_utility_code)
3435 env.use_utility_code(restore_exception_utility_code)
3436 self.gil_check(env)
3438 gil_message = "Raising exception"
3440 def generate_execution_code(self, code):
3441 if self.exc_type:
3442 self.exc_type.generate_evaluation_code(code)
3443 type_code = self.exc_type.py_result()
3444 else:
3445 type_code = "0"
3446 if self.exc_value:
3447 self.exc_value.generate_evaluation_code(code)
3448 value_code = self.exc_value.py_result()
3449 else:
3450 value_code = "0"
3451 if self.exc_tb:
3452 self.exc_tb.generate_evaluation_code(code)
3453 tb_code = self.exc_tb.py_result()
3454 else:
3455 tb_code = "0"
3456 if self.exc_type or self.exc_value or self.exc_tb:
3457 code.putln(
3458 "__Pyx_Raise(%s, %s, %s);" % (
3459 type_code,
3460 value_code,
3461 tb_code))
3462 else:
3463 code.putln(
3464 "__Pyx_ReRaise();")
3465 for obj in (self.exc_type, self.exc_value, self.exc_tb):
3466 if obj:
3467 obj.generate_disposal_code(code)
3468 obj.free_temps(code)
3469 code.putln(
3470 code.error_goto(self.pos))
3472 def annotate(self, code):
3473 if self.exc_type:
3474 self.exc_type.annotate(code)
3475 if self.exc_value:
3476 self.exc_value.annotate(code)
3477 if self.exc_tb:
3478 self.exc_tb.annotate(code)
3481 class ReraiseStatNode(StatNode):
3483 child_attrs = []
3485 def analyse_expressions(self, env):
3486 self.gil_check(env)
3487 env.use_utility_code(raise_utility_code)
3488 env.use_utility_code(restore_exception_utility_code)
3490 gil_message = "Raising exception"
3492 def generate_execution_code(self, code):
3493 vars = code.funcstate.exc_vars
3494 if vars:
3495 code.putln("__Pyx_Raise(%s, %s, %s);" % tuple(vars))
3496 code.putln(code.error_goto(self.pos))
3497 else:
3498 error(self.pos, "Reraise not inside except clause")
3501 class AssertStatNode(StatNode):
3502 # assert statement
3503 #
3504 # cond ExprNode
3505 # value ExprNode or None
3507 child_attrs = ["cond", "value"]
3509 def analyse_expressions(self, env):
3510 self.cond = self.cond.analyse_boolean_expression(env)
3511 if self.value:
3512 self.value.analyse_types(env)
3513 self.value = self.value.coerce_to_pyobject(env)
3514 self.value.allocate_temps(env)
3515 self.cond.release_temp(env)
3516 if self.value:
3517 self.value.release_temp(env)
3518 self.gil_check(env)
3519 #env.recycle_pending_temps() # TEMPORARY
3521 gil_message = "Raising exception"
3523 def generate_execution_code(self, code):
3524 code.putln("#ifndef PYREX_WITHOUT_ASSERTIONS")
3525 self.cond.generate_evaluation_code(code)
3526 code.putln(
3527 "if (unlikely(!%s)) {" %
3528 self.cond.result())
3529 if self.value:
3530 self.value.generate_evaluation_code(code)
3531 code.putln(
3532 "PyErr_SetObject(PyExc_AssertionError, %s);" %
3533 self.value.py_result())
3534 self.value.generate_disposal_code(code)
3535 self.value.free_temps(code)
3536 else:
3537 code.putln(
3538 "PyErr_SetNone(PyExc_AssertionError);")
3539 code.putln(
3540 code.error_goto(self.pos))
3541 code.putln(
3542 "}")
3543 self.cond.generate_disposal_code(code)
3544 self.cond.free_temps(code)
3545 code.putln("#endif")
3547 def annotate(self, code):
3548 self.cond.annotate(code)
3549 if self.value:
3550 self.value.annotate(code)
3553 class IfStatNode(StatNode):
3554 # if statement
3555 #
3556 # if_clauses [IfClauseNode]
3557 # else_clause StatNode or None
3559 child_attrs = ["if_clauses", "else_clause"]
3561 def analyse_control_flow(self, env):
3562 env.start_branching(self.pos)
3563 for if_clause in self.if_clauses:
3564 if_clause.analyse_control_flow(env)
3565 env.next_branch(if_clause.end_pos())
3566 if self.else_clause:
3567 self.else_clause.analyse_control_flow(env)
3568 env.finish_branching(self.end_pos())
3570 def analyse_declarations(self, env):
3571 for if_clause in self.if_clauses:
3572 if_clause.analyse_declarations(env)
3573 if self.else_clause:
3574 self.else_clause.analyse_declarations(env)
3576 def analyse_expressions(self, env):
3577 for if_clause in self.if_clauses:
3578 if_clause.analyse_expressions(env)
3579 if self.else_clause:
3580 self.else_clause.analyse_expressions(env)
3582 def generate_execution_code(self, code):
3583 code.mark_pos(self.pos)
3584 end_label = code.new_label()
3585 for if_clause in self.if_clauses:
3586 if_clause.generate_execution_code(code, end_label)
3587 if self.else_clause:
3588 code.putln("/*else*/ {")
3589 self.else_clause.generate_execution_code(code)
3590 code.putln("}")
3591 code.put_label(end_label)
3593 def annotate(self, code):
3594 for if_clause in self.if_clauses:
3595 if_clause.annotate(code)
3596 if self.else_clause:
3597 self.else_clause.annotate(code)
3600 class IfClauseNode(Node):
3601 # if or elif clause in an if statement
3602 #
3603 # condition ExprNode
3604 # body StatNode
3606 child_attrs = ["condition", "body"]
3608 def analyse_control_flow(self, env):
3609 self.body.analyse_control_flow(env)
3611 def analyse_declarations(self, env):
3612 self.condition.analyse_declarations(env)
3613 self.body.analyse_declarations(env)
3615 def analyse_expressions(self, env):
3616 self.condition = \
3617 self.condition.analyse_temp_boolean_expression(env)
3618 self.condition.release_temp(env)
3619 self.body.analyse_expressions(env)
3621 def generate_execution_code(self, code, end_label):
3622 self.condition.generate_evaluation_code(code)
3623 code.putln(
3624 "if (%s) {" %
3625 self.condition.result())
3626 self.condition.generate_disposal_code(code)
3627 self.condition.free_temps(code)
3628 self.body.generate_execution_code(code)
3629 #code.putln(
3630 # "goto %s;" %
3631 # end_label)
3632 code.put_goto(end_label)
3633 code.putln("}")
3635 def annotate(self, code):
3636 self.condition.annotate(code)
3637 self.body.annotate(code)
3640 class SwitchCaseNode(StatNode):
3641 # Generated in the optimization of an if-elif-else node
3642 #
3643 # conditions [ExprNode]
3644 # body StatNode
3646 child_attrs = ['conditions', 'body']
3648 def generate_execution_code(self, code):
3649 for cond in self.conditions:
3650 code.putln("case %s:" % cond.calculate_result_code())
3651 self.body.generate_execution_code(code)
3652 code.putln("break;")
3654 def annotate(self, code):
3655 for cond in self.conditions:
3656 cond.annotate(code)
3657 self.body.annotate(code)
3659 class SwitchStatNode(StatNode):
3660 # Generated in the optimization of an if-elif-else node
3661 #
3662 # test ExprNode
3663 # cases [SwitchCaseNode]
3664 # else_clause StatNode or None
3666 child_attrs = ['test', 'cases', 'else_clause']
3668 def generate_execution_code(self, code):
3669 code.putln("switch (%s) {" % self.test.calculate_result_code())
3670 for case in self.cases:
3671 case.generate_execution_code(code)
3672 if self.else_clause is not None:
3673 code.putln("default:")
3674 self.else_clause.generate_execution_code(code)
3675 code.putln("break;")
3676 code.putln("}")
3678 def annotate(self, code):
3679 self.test.annotate(code)
3680 for case in self.cases:
3681 case.annotate(code)
3682 if self.else_clause is not None:
3683 self.else_clause.annotate(code)
3685 class LoopNode:
3687 def analyse_control_flow(self, env):
3688 env.start_branching(self.pos)
3689 self.body.analyse_control_flow(env)
3690 env.next_branch(self.body.end_pos())
3691 if self.else_clause:
3692 self.else_clause.analyse_control_flow(env)
3693 env.finish_branching(self.end_pos())
3696 class WhileStatNode(LoopNode, StatNode):
3697 # while statement
3698 #
3699 # condition ExprNode
3700 # body StatNode
3701 # else_clause StatNode
3703 child_attrs = ["condition", "body", "else_clause"]
3705 def analyse_declarations(self, env):
3706 self.body.analyse_declarations(env)
3707 if self.else_clause:
3708 self.else_clause.analyse_declarations(env)
3710 def analyse_expressions(self, env):
3711 self.condition = \
3712 self.condition.analyse_temp_boolean_expression(env)
3713 self.condition.release_temp(env)
3714 #env.recycle_pending_temps() # TEMPORARY
3715 self.body.analyse_expressions(env)
3716 if self.else_clause:
3717 self.else_clause.analyse_expressions(env)
3719 def generate_execution_code(self, code):
3720 old_loop_labels = code.new_loop_labels()
3721 code.putln(
3722 "while (1) {")
3723 self.condition.generate_evaluation_code(code)
3724 self.condition.generate_disposal_code(code)
3725 code.putln(
3726 "if (!%s) break;" %
3727 self.condition.result())
3728 self.condition.free_temps(code)
3729 self.body.generate_execution_code(code)
3730 code.put_label(code.continue_label)
3731 code.putln("}")
3732 break_label = code.break_label
3733 code.set_loop_labels(old_loop_labels)
3734 if self.else_clause:
3735 code.putln("/*else*/ {")
3736 self.else_clause.generate_execution_code(code)
3737 code.putln("}")
3738 code.put_label(break_label)
3740 def annotate(self, code):
3741 self.condition.annotate(code)
3742 self.body.annotate(code)
3743 if self.else_clause:
3744 self.else_clause.annotate(code)
3747 def ForStatNode(pos, **kw):
3748 if kw.has_key('iterator'):
3749 return ForInStatNode(pos, **kw)
3750 else:
3751 return ForFromStatNode(pos, **kw)
3753 class ForInStatNode(LoopNode, StatNode):
3754 # for statement
3755 #
3756 # target ExprNode
3757 # iterator IteratorNode
3758 # body StatNode
3759 # else_clause StatNode
3760 # item NextNode used internally
3762 child_attrs = ["target", "iterator", "body", "else_clause"]
3763 item = None
3765 def analyse_declarations(self, env):
3766 self.target.analyse_target_declaration(env)
3767 self.body.analyse_declarations(env)
3768 if self.else_clause:
3769 self.else_clause.analyse_declarations(env)
3771 def analyse_expressions(self, env):
3772 import ExprNodes
3773 self.target.analyse_target_types(env)
3774 self.iterator.analyse_expressions(env)
3775 self.item = ExprNodes.NextNode(self.iterator, env)
3776 self.item = self.item.coerce_to(self.target.type, env)
3777 self.item.allocate_temps(env)
3778 self.target.allocate_target_temps(env, self.item)
3779 #self.item.release_temp(env)
3780 #self.target.release_target_temp(env)
3781 self.body.analyse_expressions(env)
3782 if self.else_clause:
3783 self.else_clause.analyse_expressions(env)
3784 self.iterator.release_temp(env)
3786 def generate_execution_code(self, code):
3787 old_loop_labels = code.new_loop_labels()
3788 self.iterator.allocate_counter_temp(code)
3789 self.iterator.generate_evaluation_code(code)
3790 code.putln(
3791 "for (;;) {")
3792 self.item.generate_evaluation_code(code)
3793 self.target.generate_assignment_code(self.item, code)
3794 self.body.generate_execution_code(code)
3795 code.put_label(code.continue_label)
3796 code.putln(
3797 "}")
3798 break_label = code.break_label
3799 code.set_loop_labels(old_loop_labels)
3800 if self.else_clause:
3801 code.putln("/*else*/ {")
3802 self.else_clause.generate_execution_code(code)
3803 code.putln("}")
3804 code.put_label(break_label)
3805 self.iterator.release_counter_temp(code)
3806 self.iterator.generate_disposal_code(code)
3807 self.iterator.free_temps(code)
3809 def annotate(self, code):
3810 self.target.annotate(code)
3811 self.iterator.annotate(code)
3812 self.body.annotate(code)
3813 if self.else_clause:
3814 self.else_clause.annotate(code)
3815 self.item.annotate(code)
3818 class ForFromStatNode(LoopNode, StatNode):
3819 # for name from expr rel name rel expr
3820 #
3821 # target NameNode
3822 # bound1 ExprNode
3823 # relation1 string
3824 # relation2 string
3825 # bound2 ExprNode
3826 # step ExprNode or None
3827 # body StatNode
3828 # else_clause StatNode or None
3829 #
3830 # Used internally:
3831 #
3832 # is_py_target bool
3833 # loopvar_node ExprNode (usually a NameNode or temp node)
3834 # py_loopvar_node PyTempNode or None
3835 child_attrs = ["target", "bound1", "bound2", "step", "body", "else_clause"]
3837 is_py_target = False
3838 loopvar_node = None
3839 py_loopvar_node = None
3841 def analyse_declarations(self, env):
3842 self.target.analyse_target_declaration(env)
3843 self.body.analyse_declarations(env)
3844 if self.else_clause:
3845 self.else_clause.analyse_declarations(env)
3847 def analyse_expressions(self, env):
3848 import ExprNodes
3849 self.target.analyse_target_types(env)
3850 self.bound1.analyse_types(env)
3851 self.bound2.analyse_types(env)
3852 if self.target.type.is_numeric:
3853 self.bound1 = self.bound1.coerce_to(self.target.type, env)
3854 self.bound2 = self.bound2.coerce_to(self.target.type, env)
3855 else:
3856 self.bound1 = self.bound1.coerce_to_integer(env)
3857 self.bound2 = self.bound2.coerce_to_integer(env)
3858 if self.step is not None:
3859 if isinstance(self.step, ExprNodes.UnaryMinusNode):
3860 warning(self.step.pos, "Probable infinite loop in for-from-by statment. Consider switching the directions of the relations.", 2)
3861 self.step.analyse_types(env)
3862 self.step = self.step.coerce_to_integer(env)
3863 if not (self.bound2.is_name or self.bound2.is_literal):
3864 self.bound2 = self.bound2.coerce_to_temp(env)
3865 target_type = self.target.type
3866 if not (target_type.is_pyobject or target_type.is_numeric):
3867 error(self.target.pos,
3868 "Integer for-loop variable must be of type int or Python object")
3869 #if not (target_type.is_pyobject
3870 # or target_type.assignable_from(PyrexTypes.c_int_type)):
3871 # error(self.target.pos,
3872 # "Cannot assign integer to variable of type '%s'" % target_type)
3873 if target_type.is_numeric:
3874 self.is_py_target = 0
3875 if isinstance(self.target, ExprNodes.IndexNode) and self.target.is_buffer_access:
3876 raise error(self.pos, "Buffer indexing not allowed as for loop target.")
3877 self.loopvar_node = self.target
3878 self.py_loopvar_node = None
3879 else:
3880 self.is_py_target = 1
3881 c_loopvar_node = ExprNodes.TempNode(self.pos,
3882 PyrexTypes.c_long_type, env)
3883 c_loopvar_node.allocate_temps(env)
3884 self.loopvar_node = c_loopvar_node
3885 self.py_loopvar_node = \
3886 ExprNodes.CloneNode(c_loopvar_node).coerce_to_pyobject(env)
3887 self.bound1.allocate_temps(env)
3888 self.bound2.allocate_temps(env)
3889 if self.step is not None:
3890 self.step.allocate_temps(env)
3891 if self.is_py_target:
3892 self.py_loopvar_node.allocate_temps(env)
3893 self.target.allocate_target_temps(env, self.py_loopvar_node)
3894 #self.target.release_target_temp(env)
3895 #self.py_loopvar_node.release_temp(env)
3896 self.body.analyse_expressions(env)
3897 if self.is_py_target:
3898 c_loopvar_node.release_temp(env)
3899 if self.else_clause:
3900 self.else_clause.analyse_expressions(env)
3901 self.bound1.release_temp(env)
3902 self.bound2.release_temp(env)
3903 if self.step is not None:
3904 self.step.release_temp(env)
3906 def generate_execution_code(self, code):
3907 old_loop_labels = code.new_loop_labels()
3908 self.bound1.generate_evaluation_code(code)
3909 self.bound2.generate_evaluation_code(code)
3910 offset, incop = self.relation_table[self.relation1]
3911 if incop == "++":
3912 decop = "--"
3913 else:
3914 decop = "++"
3915 if self.step is not None:
3916 self.step.generate_evaluation_code(code)
3917 step = self.step.result()
3918 incop = "%s=%s" % (incop[0], step)
3919 decop = "%s=%s" % (decop[0], step)
3920 loopvar_name = self.loopvar_node.result()
3921 code.putln(
3922 "for (%s = %s%s; %s %s %s; %s%s) {" % (
3923 loopvar_name,
3924 self.bound1.result(), offset,
3925 loopvar_name, self.relation2, self.bound2.result(),
3926 loopvar_name, incop))
3927 if self.py_loopvar_node:
3928 self.py_loopvar_node.generate_evaluation_code(code)
3929 self.target.generate_assignment_code(self.py_loopvar_node, code)
3930 self.body.generate_execution_code(code)
3931 code.put_label(code.continue_label)
3932 if getattr(self, "from_range", False):
3933 # Undo last increment to maintain Python semantics:
3934 code.putln("} %s%s;" % (loopvar_name, decop))
3935 else:
3936 code.putln("}")
3937 break_label = code.break_label
3938 code.set_loop_labels(old_loop_labels)
3939 if self.else_clause:
3940 code.putln("/*else*/ {")
3941 self.else_clause.generate_execution_code(code)
3942 code.putln("}")
3943 code.put_label(break_label)
3944 self.bound1.generate_disposal_code(code)
3945 self.bound1.free_temps(code)
3946 self.bound2.generate_disposal_code(code)
3947 self.bound2.free_temps(code)
3948 if self.step is not None:
3949 self.step.generate_disposal_code(code)
3950 self.step.free_temps(code)
3952 relation_table = {
3953 # {relop : (initial offset, increment op)}
3954 '<=': ("", "++"),
3955 '<' : ("+1", "++"),
3956 '>=': ("", "--"),
3957 '>' : ("-1", "--")
3958 }
3960 def annotate(self, code):
3961 self.target.annotate(code)
3962 self.bound1.annotate(code)
3963 self.bound2.annotate(code)
3964 if self.step:
3965 self.bound2.annotate(code)
3966 self.body.annotate(code)
3967 if self.else_clause:
3968 self.else_clause.annotate(code)
3971 class WithStatNode(StatNode):
3972 """
3973 Represents a Python with statement.
3975 This is only used at parse tree level; and is not present in
3976 analysis or generation phases.
3977 """
3978 # manager The with statement manager object
3979 # target Node (lhs expression)
3980 # body StatNode
3981 child_attrs = ["manager", "target", "body"]
3983 class TryExceptStatNode(StatNode):
3984 # try .. except statement
3985 #
3986 # body StatNode
3987 # except_clauses [ExceptClauseNode]
3988 # else_clause StatNode or None
3989 # cleanup_list [Entry] old style temps to clean up on error
3991 child_attrs = ["body", "except_clauses", "else_clause"]
3993 def analyse_control_flow(self, env):
3994 env.start_branching(self.pos)
3995 self.body.analyse_control_flow(env)
3996 successful_try = env.control_flow # grab this for later
3997 env.next_branch(self.body.end_pos())
3998 env.finish_branching(self.body.end_pos())
4000 env.start_branching(self.except_clauses[0].pos)
4001 for except_clause in self.except_clauses:
4002 except_clause.analyse_control_flow(env)
4003 env.next_branch(except_clause.end_pos())
4005 # the else cause it executed only when the try clause finishes
4006 env.control_flow.incoming = successful_try
4007 if self.else_clause:
4008 self.else_clause.analyse_control_flow(env)
4009 env.finish_branching(self.end_pos())
4011 def analyse_declarations(self, env):
4012 self.body.analyse_declarations(env)
4013 for except_clause in self.except_clauses:
4014 except_clause.analyse_declarations(env)
4015 if self.else_clause:
4016 self.else_clause.analyse_declarations(env)
4017 self.gil_check(env)
4018 env.use_utility_code(reset_exception_utility_code)
4020 def analyse_expressions(self, env):
4021 self.body.analyse_expressions(env)
4022 self.cleanup_list = env.free_temp_entries[:]
4023 default_clause_seen = 0
4024 for except_clause in self.except_clauses:
4025 except_clause.analyse_expressions(env)
4026 if default_clause_seen:
4027 error(except_clause.pos, "default 'except:' must be last")
4028 if not except_clause.pattern:
4029 default_clause_seen = 1
4030 self.has_default_clause = default_clause_seen
4031 if self.else_clause:
4032 self.else_clause.analyse_expressions(env)
4033 self.gil_check(env)
4035 gil_message = "Try-except statement"
4037 def generate_execution_code(self, code):
4038 old_return_label = code.return_label
4039 old_break_label = code.break_label
4040 old_error_label = code.new_error_label()
4041 our_error_label = code.error_label
4042 except_end_label = code.new_label('exception_handled')
4043 except_error_label = code.new_label('except_error')
4044 except_return_label = code.new_label('except_return')
4045 try_return_label = code.new_label('try_return')
4046 try_break_label = code.new_label('try_break')
4047 try_end_label = code.new_label('try_end')
4049 code.putln("{")
4050 code.putln("PyObject %s;" %
4051 ', '.join(['*%s' % var for var in Naming.exc_save_vars]))
4052 code.putln("__Pyx_ExceptionSave(%s);" %
4053 ', '.join(['&%s' % var for var in Naming.exc_save_vars]))
4054 for var in Naming.exc_save_vars:
4055 code.put_xgotref(var)
4056 code.putln(
4057 "/*try:*/ {")
4058 code.return_label = try_return_label
4059 code.break_label = try_break_label
4060 self.body.generate_execution_code(code)
4061 code.putln(
4062 "}")
4063 temps_to_clean_up = code.funcstate.all_free_managed_temps()
4064 code.error_label = except_error_label
4065 code.return_label = except_return_label
4066 if self.else_clause:
4067 code.putln(
4068 "/*else:*/ {")
4069 self.else_clause.generate_execution_code(code)
4070 code.putln(
4071 "}")
4072 for var in Naming.exc_save_vars:
4073 code.put_xdecref_clear(var, py_object_type)
4074 code.put_goto(try_end_label)
4075 if code.label_used(try_return_label):
4076 code.put_label(try_return_label)
4077 for var in Naming.exc_save_vars:
4078 code.put_xdecref_clear(var, py_object_type)
4079 code.put_goto(old_return_label)
4080 code.put_label(our_error_label)
4081 code.put_var_xdecrefs_clear(self.cleanup_list)
4082 for temp_name, type in temps_to_clean_up:
4083 code.put_xdecref_clear(temp_name, type)
4084 for except_clause in self.except_clauses:
4085 except_clause.generate_handling_code(code, except_end_label)
4087 error_label_used = code.label_used(except_error_label)
4088 if error_label_used or not self.has_default_clause:
4089 if error_label_used:
4090 code.put_label(except_error_label)
4091 for var in Naming.exc_save_vars:
4092 code.put_xdecref(var, py_object_type)
4093 code.put_goto(old_error_label)
4095 if code.label_used(try_break_label):
4096 code.put_label(try_break_label)
4097 for var in Naming.exc_save_vars: code.put_xgiveref(var)
4098 code.putln("__Pyx_ExceptionReset(%s);" %
4099 ', '.join(Naming.exc_save_vars))
4100 code.put_goto(old_break_label)
4102 if code.label_used(except_return_label):
4103 code.put_label(except_return_label)
4104 for var in Naming.exc_save_vars: code.put_xgiveref(var)
4105 code.putln("__Pyx_ExceptionReset(%s);" %
4106 ', '.join(Naming.exc_save_vars))
4107 code.put_goto(old_return_label)
4109 if code.label_used(except_end_label):
4110 code.put_label(except_end_label)
4111 for var in Naming.exc_save_vars: code.put_xgiveref(var)
4112 code.putln("__Pyx_ExceptionReset(%s);" %
4113 ', '.join(Naming.exc_save_vars))
4114 code.put_label(try_end_label)
4115 code.putln("}")
4117 code.return_label = old_return_label
4118 code.break_label = old_break_label
4119 code.error_label = old_error_label
4121 def annotate(self, code):
4122 self.body.annotate(code)
4123 for except_node in self.except_clauses:
4124 except_node.annotate(code)
4125 if self.else_clause:
4126 self.else_clause.annotate(code)
4129 class ExceptClauseNode(Node):
4130 # Part of try ... except statement.
4131 #
4132 # pattern ExprNode
4133 # target ExprNode or None
4134 # body StatNode
4135 # excinfo_target NameNode or None optional target for exception info
4136 # match_flag string result of exception match
4137 # exc_value ExcValueNode used internally
4138 # function_name string qualified name of enclosing function
4139 # exc_vars (string * 3) local exception variables
4141 # excinfo_target is never set by the parser, but can be set by a transform
4142 # in order to extract more extensive information about the exception as a
4143 # sys.exc_info()-style tuple into a target variable
4145 child_attrs = ["pattern", "target", "body", "exc_value", "excinfo_target"]
4147 exc_value = None
4148 excinfo_target = None
4150 def analyse_declarations(self, env):
4151 if self.target:
4152 self.target.analyse_target_declaration(env)
4153 if self.excinfo_target is not None:
4154 self.excinfo_target.analyse_target_declaration(env)
4155 self.body.analyse_declarations(env)
4157 def analyse_expressions(self, env):
4158 import ExprNodes
4159 genv = env.global_scope()
4160 self.function_name = env.qualified_name
4161 if self.pattern:
4162 self.pattern.analyse_expressions(env)
4163 self.pattern = self.pattern.coerce_to_pyobject(env)
4164 self.match_flag = env.allocate_temp(PyrexTypes.c_int_type)
4165 self.pattern.release_temp(env)
4166 env.release_temp(self.match_flag)
4167 self.exc_vars = [env.allocate_temp(py_object_type) for i in xrange(3)]
4168 if self.target:
4169 self.exc_value = ExprNodes.ExcValueNode(self.pos, env, self.exc_vars[1])
4170 self.exc_value.allocate_temps(env)
4171 self.target.analyse_target_expression(env, self.exc_value)
4172 if self.excinfo_target is not None:
4173 import ExprNodes
4174 self.excinfo_tuple = ExprNodes.TupleNode(pos=self.pos, args=[
4175 ExprNodes.ExcValueNode(pos=self.pos, env=env, var=self.exc_vars[0]),
4176 ExprNodes.ExcValueNode(pos=self.pos, env=env, var=self.exc_vars[1]),
4177 ExprNodes.ExcValueNode(pos=self.pos, env=env, var=self.exc_vars[2])
4178 ])
4179 self.excinfo_tuple.analyse_expressions(env)
4180 self.excinfo_tuple.allocate_temps(env)
4181 self.excinfo_target.analyse_target_expression(env, self.excinfo_tuple)
4183 self.body.analyse_expressions(env)
4184 for var in self.exc_vars:
4185 env.release_temp(var)
4186 env.use_utility_code(get_exception_utility_code)
4187 env.use_utility_code(restore_exception_utility_code)
4189 def generate_handling_code(self, code, end_label):
4190 code.mark_pos(self.pos)
4191 if self.pattern:
4192 self.pattern.generate_evaluation_code(code)
4193 code.putln(
4194 "%s = PyErr_ExceptionMatches(%s);" % (
4195 self.match_flag,
4196 self.pattern.py_result()))
4197 self.pattern.generate_disposal_code(code)
4198 self.pattern.free_temps(code)
4199 code.putln(
4200 "if (%s) {" %
4201 self.match_flag)
4202 else:
4203 code.putln("/*except:*/ {")
4204 code.putln('__Pyx_AddTraceback("%s");' % self.function_name)
4205 # We always have to fetch the exception value even if
4206 # there is no target, because this also normalises the
4207 # exception and stores it in the thread state.
4208 exc_args = "&%s, &%s, &%s" % tuple(self.exc_vars)
4209 code.putln("if (__Pyx_GetException(%s) < 0) %s" % (exc_args,
4210 code.error_goto(self.pos)))
4211 for x in self.exc_vars:
4212 code.put_gotref(x)
4213 if self.target:
4214 self.exc_value.generate_evaluation_code(code)
4215 self.target.generate_assignment_code(self.exc_value, code)
4216 if self.excinfo_target is not None:
4217 self.excinfo_tuple.generate_evaluation_code(code)
4218 self.excinfo_target.generate_assignment_code(self.excinfo_tuple, code)
4220 old_exc_vars = code.funcstate.exc_vars
4221 code.funcstate.exc_vars = self.exc_vars
4222 self.body.generate_execution_code(code)
4223 code.funcstate.exc_vars = old_exc_vars
4224 for var in self.exc_vars:
4225 code.putln("__Pyx_DECREF(%s); %s = 0;" % (var, var))
4226 code.put_goto(end_label)
4227 code.putln(
4228 "}")
4230 def annotate(self, code):
4231 if self.pattern:
4232 self.pattern.annotate(code)
4233 if self.target:
4234 self.target.annotate(code)
4235 self.body.annotate(code)
4238 class TryFinallyStatNode(StatNode):
4239 # try ... finally statement
4240 #
4241 # body StatNode
4242 # finally_clause StatNode
4243 #
4244 # cleanup_list [Entry] old_style temps to clean up on error
4245 #
4246 # The plan is that we funnel all continue, break
4247 # return and error gotos into the beginning of the
4248 # finally block, setting a variable to remember which
4249 # one we're doing. At the end of the finally block, we
4250 # switch on the variable to figure out where to go.
4251 # In addition, if we're doing an error, we save the
4252 # exception on entry to the finally block and restore
4253 # it on exit.
4255 child_attrs = ["body", "finally_clause"]
4257 preserve_exception = 1
4259 disallow_continue_in_try_finally = 0
4260 # There doesn't seem to be any point in disallowing
4261 # continue in the try block, since we have no problem
4262 # handling it.
4264 def create_analysed(pos, env, body, finally_clause):
4265 node = TryFinallyStatNode(pos, body=body, finally_clause=finally_clause)
4266 node.cleanup_list = []
4267 return node
4268 create_analysed = staticmethod(create_analysed)
4270 def analyse_control_flow(self, env):
4271 env.start_branching(self.pos)
4272 self.body.analyse_control_flow(env)
4273 env.next_branch(self.body.end_pos())
4274 env.finish_branching(self.body.end_pos())
4275 self.finally_clause.analyse_control_flow(env)
4277 def analyse_declarations(self, env):
4278 self.body.analyse_declarations(env)
4279 self.finally_clause.analyse_declarations(env)
4281 def analyse_expressions(self, env):
4282 self.body.analyse_expressions(env)
4283 self.cleanup_list = env.free_temp_entries[:]
4284 self.finally_clause.analyse_expressions(env)
4285 self.gil_check(env)
4287 gil_message = "Try-finally statement"
4289 def generate_execution_code(self, code):
4290 old_error_label = code.error_label
4291 old_labels = code.all_new_labels()
4292 new_labels = code.get_all_labels()
4293 new_error_label = code.error_label
4294 catch_label = code.new_label()
4295 code.putln(
4296 "/*try:*/ {")
4297 if self.disallow_continue_in_try_finally:
4298 was_in_try_finally = code.funcstate.in_try_finally
4299 code.funcstate.in_try_finally = 1
4300 self.body.generate_execution_code(code)
4301 if self.disallow_continue_in_try_finally:
4302 code.funcstate.in_try_finally = was_in_try_finally
4303 code.putln(
4304 "}")
4305 temps_to_clean_up = code.funcstate.all_free_managed_temps()
4306 code.putln(
4307 "/*finally:*/ {")
4308 cases_used = []
4309 error_label_used = 0
4310 for i, new_label in enumerate(new_labels):
4311 if new_label in code.labels_used:
4312 cases_used.append(i)
4313 if new_label == new_error_label:
4314 error_label_used = 1
4315 error_label_case = i
4316 if cases_used:
4317 code.putln(
4318 "int __pyx_why;")
4319 if error_label_used and self.preserve_exception:
4320 code.putln(
4321 "PyObject *%s, *%s, *%s;" % Naming.exc_vars)
4322 code.putln(
4323 "int %s;" % Naming.exc_lineno_name)
4324 exc_var_init_zero = ''.join(["%s = 0; " % var for var in Naming.exc_vars])
4325 exc_var_init_zero += '%s = 0;' % Naming.exc_lineno_name
4326 code.putln(exc_var_init_zero)
4327 else:
4328 exc_var_init_zero = None
4329 code.use_label(catch_label)
4330 code.putln(
4331 "__pyx_why = 0; goto %s;" % catch_label)
4332 for i in cases_used:
4333 new_label = new_labels[i]
4334 #if new_label and new_label != "<try>":
4335 if new_label == new_error_label and self.preserve_exception:
4336 self.put_error_catcher(code,
4337 new_error_label, i+1, catch_label, temps_to_clean_up)
4338 else:
4339 code.put('%s: ' % new_label)
4340 if exc_var_init_zero:
4341 code.putln(exc_var_init_zero)
4342 code.putln("__pyx_why = %s; goto %s;" % (
4343 i+1,
4344 catch_label))
4345 code.put_label(catch_label)
4346 code.set_all_labels(old_labels)
4347 if error_label_used:
4348 code.new_error_label()
4349 finally_error_label = code.error_label
4350 self.finally_clause.generate_execution_code(code)
4351 if error_label_used:
4352 if finally_error_label in code.labels_used and self.preserve_exception:
4353 over_label = code.new_label()
4354 code.put_goto(over_label);
4355 code.put_label(finally_error_label)
4356 code.putln("if (__pyx_why == %d) {" % (error_label_case + 1))
4357 for var in Naming.exc_vars:
4358 code.putln("Py_XDECREF(%s);" % var)
4359 code.putln("}")
4360 code.put_goto(old_error_label)
4361 code.put_label(over_label)
4362 code.error_label = old_error_label
4363 if cases_used:
4364 code.putln(
4365 "switch (__pyx_why) {")
4366 for i in cases_used:
4367 old_label = old_labels[i]
4368 if old_label == old_error_label and self.preserve_exception:
4369 self.put_error_uncatcher(code, i+1, old_error_label)
4370 else:
4371 code.use_label(old_label)
4372 code.putln(
4373 "case %s: goto %s;" % (
4374 i+1,
4375 old_label))
4376 code.putln(
4377 "}")
4378 code.putln(
4379 "}")
4381 def put_error_catcher(self, code, error_label, i, catch_label, temps_to_clean_up):
4382 code.globalstate.use_utility_code(restore_exception_utility_code)
4383 code.putln(
4384 "%s: {" %
4385 error_label)
4386 code.putln(
4387 "__pyx_why = %s;" %
4388 i)
4389 code.put_var_xdecrefs_clear(self.cleanup_list)
4390 for temp_name, type in temps_to_clean_up:
4391 code.put_xdecref_clear(temp_name, type)
4392 code.putln(
4393 "__Pyx_ErrFetch(&%s, &%s, &%s);" %
4394 Naming.exc_vars)
4395 code.putln(
4396 "%s = %s;" % (
4397 Naming.exc_lineno_name, Naming.lineno_cname))
4398 #code.putln(
4399 # "goto %s;" %
4400 # catch_label)
4401 code.put_goto(catch_label)
4402 code.putln(
4403 "}")
4405 def put_error_uncatcher(self, code, i, error_label):
4406 code.globalstate.use_utility_code(restore_exception_utility_code)
4407 code.putln(
4408 "case %s: {" %
4409 i)
4410 code.putln(
4411 "__Pyx_ErrRestore(%s, %s, %s);" %
4412 Naming.exc_vars)
4413 code.putln(
4414 "%s = %s;" % (
4415 Naming.lineno_cname, Naming.exc_lineno_name))
4416 for var in Naming.exc_vars:
4417 code.putln(
4418 "%s = 0;" %
4419 var)
4420 code.put_goto(error_label)
4421 code.putln(
4422 "}")
4424 def annotate(self, code):
4425 self.body.annotate(code)
4426 self.finally_clause.annotate(code)
4429 class GILStatNode(TryFinallyStatNode):
4430 # 'with gil' or 'with nogil' statement
4431 #
4432 # state string 'gil' or 'nogil'
4434 child_attrs = []
4436 preserve_exception = 0
4438 def __init__(self, pos, state, body):
4439 self.state = state
4440 TryFinallyStatNode.__init__(self, pos,
4441 body = body,
4442 finally_clause = GILExitNode(pos, state = state))
4444 def analyse_expressions(self, env):
4445 was_nogil = env.nogil
4446 env.nogil = 1
4447 TryFinallyStatNode.analyse_expressions(self, env)
4448 env.nogil = was_nogil
4450 def gil_check(self, env):
4451 pass
4453 def generate_execution_code(self, code):
4454 code.putln("/*with %s:*/ {" % self.state)
4455 if self.state == 'gil':
4456 code.putln("PyGILState_STATE _save = PyGILState_Ensure();")
4457 else:
4458 code.putln("PyThreadState *_save;")
4459 code.putln("Py_UNBLOCK_THREADS")
4460 TryFinallyStatNode.generate_execution_code(self, code)
4461 code.putln("}")
4464 class GILExitNode(StatNode):
4465 # Used as the 'finally' block in a GILStatNode
4466 #
4467 # state string 'gil' or 'nogil'
4469 child_attrs = []
4471 def analyse_expressions(self, env):
4472 pass
4474 def generate_execution_code(self, code):
4475 if self.state == 'gil':
4476 code.putln("PyGILState_Release();")
4477 else:
4478 code.putln("Py_BLOCK_THREADS")
4481 class CImportStatNode(StatNode):
4482 # cimport statement
4483 #
4484 # module_name string Qualified name of module being imported
4485 # as_name string or None Name specified in "as" clause, if any
4487 child_attrs = []
4489 def analyse_declarations(self, env):
4490 if not env.is_module_scope:
4491 error(self.pos, "cimport only allowed at module level")
4492 return
4493 module_scope = env.find_module(self.module_name, self.pos)
4494 if "." in self.module_name:
4495 names = [EncodedString(name) for name in self.module_name.split(".")]
4496 top_name = names[0]
4497 top_module_scope = env.context.find_submodule(top_name)
4498 module_scope = top_module_scope
4499 for name in names[1:]:
4500 submodule_scope = module_scope.find_submodule(name)
4501 module_scope.declare_module(name, submodule_scope, self.pos)
4502 module_scope = submodule_scope
4503 if self.as_name:
4504 env.declare_module(self.as_name, module_scope, self.pos)
4505 else:
4506 env.declare_module(top_name, top_module_scope, self.pos)
4507 else:
4508 name = self.as_name or self.module_name
4509 env.declare_module(name, module_scope, self.pos)
4511 def analyse_expressions(self, env):
4512 pass
4514 def generate_execution_code(self, code):
4515 pass
4518 class FromCImportStatNode(StatNode):
4519 # from ... cimport statement
4520 #
4521 # module_name string Qualified name of module
4522 # imported_names [(pos, name, as_name, kind)] Names to be imported
4524 child_attrs = []
4526 def analyse_declarations(self, env):
4527 if not env.is_module_scope:
4528 error(self.pos, "cimport only allowed at module level")
4529 return
4530 module_scope = env.find_module(self.module_name, self.pos)
4531 env.add_imported_module(module_scope)
4532 for pos, name, as_name, kind in self.imported_names:
4533 if name == "*":
4534 for local_name, entry in module_scope.entries.items():
4535 env.add_imported_entry(local_name, entry, pos)
4536 else:
4537 entry = module_scope.lookup(name)
4538 if entry:
4539 if kind and not self.declaration_matches(entry, kind):
4540 entry.redeclared(pos)
4541 else:
4542 if kind == 'struct' or kind == 'union':
4543 entry = module_scope.declare_struct_or_union(name,
4544 kind = kind, scope = None, typedef_flag = 0, pos = pos)
4545 elif kind == 'class':
4546 entry = module_scope.declare_c_class(name, pos = pos,
4547 module_name = self.module_name)
4548 else:
4549 error(pos, "Name '%s' not declared in module '%s'"
4550 % (name, self.module_name))
4552 if entry:
4553 local_name = as_name or name
4554 env.add_imported_entry(local_name, entry, pos)
4556 def declaration_matches(self, entry, kind):
4557 if not entry.is_type:
4558 return 0
4559 type = entry.type
4560 if kind == 'class':
4561 if not type.is_extension_type:
4562 return 0
4563 else:
4564 if not type.is_struct_or_union:
4565 return 0
4566 if kind != type.kind:
4567 return 0
4568 return 1
4570 def analyse_expressions(self, env):
4571 pass
4573 def generate_execution_code(self, code):
4574 pass
4577 class FromImportStatNode(StatNode):
4578 # from ... import statement
4579 #
4580 # module ImportNode
4581 # items [(string, NameNode)]
4582 # interned_items [(string, NameNode, ExprNode)]
4583 # item PyTempNode used internally
4584 # import_star boolean used internally
4586 child_attrs = ["module"]
4587 import_star = 0
4589 def analyse_declarations(self, env):
4590 for name, target in self.items:
4591 if name == "*":
4592 if not env.is_module_scope:
4593 error(self.pos, "import * only allowed at module level")
4594 return
4595 env.has_import_star = 1
4596 self.import_star = 1
4597 else:
4598 target.analyse_target_declaration(env)
4600 def analyse_expressions(self, env):
4601 import ExprNodes
4602 self.module.analyse_expressions(env)
4603 self.item = ExprNodes.PyTempNode(self.pos, env)
4604 self.item.allocate_temp(env)
4605 self.interned_items = []
4606 for name, target in self.items:
4607 if name == '*':
4608 for _, entry in env.entries.items():
4609 if not entry.is_type and entry.type.is_extension_type:
4610 env.use_utility_code(ExprNodes.type_test_utility_code)
4611 break
4612 else:
4613 entry = env.lookup(target.name)
4614 if entry.is_type and entry.type.name == name and entry.type.module_name == self.module.module_name.value:
4615 continue # already cimported
4616 target.analyse_target_expression(env, None)
4617 if target.type is py_object_type:
4618 coerced_item = None
4619 else:
4620 coerced_item = self.item.coerce_to(target.type, env)
4621 self.interned_items.append(
4622 (env.intern_identifier(name), target, coerced_item))
4623 #target.release_target_temp(env) # was release_temp ?!?
4624 self.module.release_temp(env)
4625 self.item.release_temp(env)
4627 def generate_execution_code(self, code):
4628 self.module.generate_evaluation_code(code)
4629 if self.import_star:
4630 code.putln(
4631 'if (%s(%s) < 0) %s;' % (
4632 Naming.import_star,
4633 self.module.py_result(),
4634 code.error_goto(self.pos)))
4635 for cname, target, coerced_item in self.interned_items:
4636 code.putln(
4637 '%s = PyObject_GetAttr(%s, %s); %s' % (
4638 self.item.result(),
4639 self.module.py_result(),
4640 cname,
4641 code.error_goto_if_null(self.item.result(), self.pos)))
4642 code.put_gotref(self.item.py_result())
4643 if coerced_item is None:
4644 target.generate_assignment_code(self.item, code)
4645 else:
4646 coerced_item.allocate_temp_result(code)
4647 coerced_item.generate_result_code(code)
4648 target.generate_assignment_code(coerced_item, code)
4649 if self.item.result() != coerced_item.result():
4650 code.put_decref_clear(self.item.result(), self.item.type)
4651 self.module.generate_disposal_code(code)
4652 self.module.free_temps(code)
4656 #------------------------------------------------------------------------------------
4657 #
4658 # Runtime support code
4659 #
4660 #------------------------------------------------------------------------------------
4662 utility_function_predeclarations = \
4663 """
4664 #ifdef __GNUC__
4665 #define INLINE __inline__
4666 #elif _WIN32
4667 #define INLINE __inline
4668 #else
4669 #define INLINE
4670 #endif
4672 typedef struct {PyObject **p; char *s; long n; char is_unicode; char intern; char is_identifier;} __Pyx_StringTabEntry; /*proto*/
4674 """ + """
4676 static int %(skip_dispatch_cname)s = 0;
4678 """ % { 'skip_dispatch_cname': Naming.skip_dispatch_cname }
4680 if Options.gcc_branch_hints:
4681 branch_prediction_macros = \
4682 """
4683 #ifdef __GNUC__
4684 /* Test for GCC > 2.95 */
4685 #if __GNUC__ > 2 || \
4686 (__GNUC__ == 2 && (__GNUC_MINOR__ > 95))
4687 #define likely(x) __builtin_expect(!!(x), 1)
4688 #define unlikely(x) __builtin_expect(!!(x), 0)
4689 #else /* __GNUC__ > 2 ... */
4690 #define likely(x) (x)
4691 #define unlikely(x) (x)
4692 #endif /* __GNUC__ > 2 ... */
4693 #else /* __GNUC__ */
4694 #define likely(x) (x)
4695 #define unlikely(x) (x)
4696 #endif /* __GNUC__ */
4697 """
4698 else:
4699 branch_prediction_macros = \
4700 """
4701 #define likely(x) (x)
4702 #define unlikely(x) (x)
4703 """
4705 #get_name_predeclaration = \
4706 #"static PyObject *__Pyx_GetName(PyObject *dict, char *name); /*proto*/"
4708 #get_name_interned_predeclaration = \
4709 #"static PyObject *__Pyx_GetName(PyObject *dict, PyObject *name); /*proto*/"
4711 #------------------------------------------------------------------------------------
4713 printing_utility_code = UtilityCode(
4714 proto = """
4715 static int __Pyx_Print(PyObject *, int); /*proto*/
4716 #if PY_MAJOR_VERSION >= 3
4717 static PyObject* %s = 0;
4718 static PyObject* %s = 0;
4719 #endif
4720 """ % (Naming.print_function, Naming.print_function_kwargs),
4721 impl = r"""
4722 #if PY_MAJOR_VERSION < 3
4723 static PyObject *__Pyx_GetStdout(void) {
4724 PyObject *f = PySys_GetObject((char *)"stdout");
4725 if (!f) {
4726 PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
4727 }
4728 return f;
4729 }
4731 static int __Pyx_Print(PyObject *arg_tuple, int newline) {
4732 PyObject *f;
4733 PyObject* v;
4734 int i;
4736 if (!(f = __Pyx_GetStdout()))
4737 return -1;
4738 for (i=0; i < PyTuple_GET_SIZE(arg_tuple); i++) {
4739 if (PyFile_SoftSpace(f, 1)) {
4740 if (PyFile_WriteString(" ", f) < 0)
4741 return -1;
4742 }
4743 v = PyTuple_GET_ITEM(arg_tuple, i);
4744 if (PyFile_WriteObject(v, f, Py_PRINT_RAW) < 0)
4745 return -1;
4746 if (PyString_Check(v)) {
4747 char *s = PyString_AsString(v);
4748 Py_ssize_t len = PyString_Size(v);
4749 if (len > 0 &&
4750 isspace(Py_CHARMASK(s[len-1])) &&
4751 s[len-1] != ' ')
4752 PyFile_SoftSpace(f, 0);
4753 }
4754 }
4755 if (newline) {
4756 if (PyFile_WriteString("\n", f) < 0)
4757 return -1;
4758 PyFile_SoftSpace(f, 0);
4759 }
4760 return 0;
4761 }
4763 #else /* Python 3 has a print function */
4764 static int __Pyx_Print(PyObject *arg_tuple, int newline) {
4765 PyObject* kwargs = 0;
4766 PyObject* result = 0;
4767 PyObject* end_string;
4768 if (!%(PRINT_FUNCTION)s) {
4769 %(PRINT_FUNCTION)s = __Pyx_GetAttrString(%(BUILTINS)s, "print");
4770 if (!%(PRINT_FUNCTION)s)
4771 return -1;
4772 }
4773 if (!newline) {
4774 if (!%(PRINT_KWARGS)s) {
4775 %(PRINT_KWARGS)s = PyDict_New();
4776 if (!%(PRINT_KWARGS)s)
4777 return -1;
4778 end_string = PyUnicode_FromStringAndSize(" ", 1);
4779 if (!end_string)
4780 return -1;
4781 if (PyDict_SetItemString(%(PRINT_KWARGS)s, "end", end_string) < 0) {
4782 Py_DECREF(end_string);
4783 return -1;
4784 }
4785 Py_DECREF(end_string);
4786 }
4787 kwargs = %(PRINT_KWARGS)s;
4788 }
4789 result = PyObject_Call(%(PRINT_FUNCTION)s, arg_tuple, kwargs);
4790 if (!result)
4791 return -1;
4792 Py_DECREF(result);
4793 return 0;
4794 }
4795 #endif
4796 """ % {'BUILTINS' : Naming.builtins_cname,
4797 'PRINT_FUNCTION' : Naming.print_function,
4798 'PRINT_KWARGS' : Naming.print_function_kwargs}
4799 )
4801 #------------------------------------------------------------------------------------
4803 # The following function is based on do_raise() from ceval.c.
4805 raise_utility_code = UtilityCode(
4806 proto = """
4807 static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb); /*proto*/
4808 """,
4809 impl = """
4810 static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb) {
4811 Py_XINCREF(type);
4812 Py_XINCREF(value);
4813 Py_XINCREF(tb);
4814 /* First, check the traceback argument, replacing None with NULL. */
4815 if (tb == Py_None) {
4816 Py_DECREF(tb);
4817 tb = 0;
4818 }
4819 else if (tb != NULL && !PyTraceBack_Check(tb)) {
4820 PyErr_SetString(PyExc_TypeError,
4821 "raise: arg 3 must be a traceback or None");
4822 goto raise_error;
4823 }
4824 /* Next, replace a missing value with None */
4825 if (value == NULL) {
4826 value = Py_None;
4827 Py_INCREF(value);
4828 }
4829 #if PY_VERSION_HEX < 0x02050000
4830 if (!PyClass_Check(type))
4831 #else
4832 if (!PyType_Check(type))
4833 #endif
4834 {
4835 /* Raising an instance. The value should be a dummy. */
4836 if (value != Py_None) {
4837 PyErr_SetString(PyExc_TypeError,
4838 "instance exception may not have a separate value");
4839 goto raise_error;
4840 }
4841 /* Normalize to raise <class>, <instance> */
4842 Py_DECREF(value);
4843 value = type;
4844 #if PY_VERSION_HEX < 0x02050000
4845 if (PyInstance_Check(type)) {
4846 type = (PyObject*) ((PyInstanceObject*)type)->in_class;
4847 Py_INCREF(type);
4848 }
4849 else {
4850 type = 0;
4851 PyErr_SetString(PyExc_TypeError,
4852 "raise: exception must be an old-style class or instance");
4853 goto raise_error;
4854 }
4855 #else
4856 type = (PyObject*) Py_TYPE(type);
4857 Py_INCREF(type);
4858 if (!PyType_IsSubtype((PyTypeObject *)type, (PyTypeObject *)PyExc_BaseException)) {
4859 PyErr_SetString(PyExc_TypeError,
4860 "raise: exception class must be a subclass of BaseException");
4861 goto raise_error;
4862 }
4863 #endif
4864 }
4865 __Pyx_ErrRestore(type, value, tb);
4866 return;
4867 raise_error:
4868 Py_XDECREF(value);
4869 Py_XDECREF(type);
4870 Py_XDECREF(tb);
4871 return;
4872 }
4873 """)
4875 #------------------------------------------------------------------------------------
4877 reraise_utility_code = UtilityCode(
4878 proto = """
4879 static void __Pyx_ReRaise(void); /*proto*/
4880 """,
4881 impl = """
4882 static void __Pyx_ReRaise(void) {
4883 PyThreadState *tstate = PyThreadState_GET();
4884 PyObject* tmp_type = tstate->curexc_type;
4885 PyObject* tmp_value = tstate->curexc_value;
4886 PyObject* tmp_tb = tstate->curexc_traceback;
4887 tstate->curexc_type = tstate->exc_type;
4888 tstate->curexc_value = tstate->exc_value;
4889 tstate->curexc_traceback = tstate->exc_traceback;
4890 tstate->exc_type = 0;
4891 tstate->exc_value = 0;
4892 tstate->exc_traceback = 0;
4893 Py_XDECREF(tmp_type);
4894 Py_XDECREF(tmp_value);
4895 Py_XDECREF(tmp_tb);
4896 }
4897 """)
4899 #------------------------------------------------------------------------------------
4901 arg_type_test_utility_code = UtilityCode(
4902 proto = """
4903 static int __Pyx_ArgTypeTest(PyObject *obj, PyTypeObject *type, int none_allowed,
4904 const char *name, int exact); /*proto*/
4905 """,
4906 impl = """
4907 static int __Pyx_ArgTypeTest(PyObject *obj, PyTypeObject *type, int none_allowed,
4908 const char *name, int exact)
4909 {
4910 if (!type) {
4911 PyErr_Format(PyExc_SystemError, "Missing type object");
4912 return 0;
4913 }
4914 if (none_allowed && obj == Py_None) return 1;
4915 else if (exact) {
4916 if (Py_TYPE(obj) == type) return 1;
4917 }
4918 else {
4919 if (PyObject_TypeCheck(obj, type)) return 1;
4920 }
4921 PyErr_Format(PyExc_TypeError,
4922 "Argument '%s' has incorrect type (expected %s, got %s)",
4923 name, type->tp_name, Py_TYPE(obj)->tp_name);
4924 return 0;
4925 }
4926 """)
4928 #------------------------------------------------------------------------------------
4929 #
4930 # __Pyx_RaiseArgtupleInvalid raises the correct exception when too
4931 # many or too few positional arguments were found. This handles
4932 # Py_ssize_t formatting correctly.
4934 raise_argtuple_invalid_utility_code = UtilityCode(
4935 proto = """
4936 static void __Pyx_RaiseArgtupleInvalid(const char* func_name, int exact,
4937 Py_ssize_t num_min, Py_ssize_t num_max, Py_ssize_t num_found); /*proto*/
4938 """,
4939 impl = """
4940 static void __Pyx_RaiseArgtupleInvalid(
4941 const char* func_name,
4942 int exact,
4943 Py_ssize_t num_min,
4944 Py_ssize_t num_max,
4945 Py_ssize_t num_found)
4946 {
4947 Py_ssize_t num_expected;
4948 const char *number, *more_or_less;
4950 if (num_found < num_min) {
4951 num_expected = num_min;
4952 more_or_less = "at least";
4953 } else {
4954 num_expected = num_max;
4955 more_or_less = "at most";
4956 }
4957 if (exact) {
4958 more_or_less = "exactly";
4959 }
4960 number = (num_expected == 1) ? "" : "s";
4961 PyErr_Format(PyExc_TypeError,
4962 #if PY_VERSION_HEX < 0x02050000
4963 "%s() takes %s %d positional argument%s (%d given)",
4964 #else
4965 "%s() takes %s %zd positional argument%s (%zd given)",
4966 #endif
4967 func_name, more_or_less, num_expected, number, num_found);
4968 }
4969 """)
4971 raise_keyword_required_utility_code = UtilityCode(
4972 proto = """
4973 static INLINE void __Pyx_RaiseKeywordRequired(const char* func_name, PyObject* kw_name); /*proto*/
4974 """,
4975 impl = """
4976 static INLINE void __Pyx_RaiseKeywordRequired(
4977 const char* func_name,
4978 PyObject* kw_name)
4979 {
4980 PyErr_Format(PyExc_TypeError,
4981 #if PY_MAJOR_VERSION >= 3
4982 "%s() needs keyword-only argument %U", func_name, kw_name);
4983 #else
4984 "%s() needs keyword-only argument %s", func_name,
4985 PyString_AS_STRING(kw_name));
4986 #endif
4987 }
4988 """)
4990 raise_double_keywords_utility_code = UtilityCode(
4991 proto = """
4992 static void __Pyx_RaiseDoubleKeywordsError(
4993 const char* func_name, PyObject* kw_name); /*proto*/
4994 """,
4995 impl = """
4996 static void __Pyx_RaiseDoubleKeywordsError(
4997 const char* func_name,
4998 PyObject* kw_name)
4999 {
5000 PyErr_Format(PyExc_TypeError,
5001 #if PY_MAJOR_VERSION >= 3
5002 "%s() got multiple values for keyword argument '%U'", func_name, kw_name);
5003 #else
5004 "%s() got multiple values for keyword argument '%s'", func_name,
5005 PyString_AS_STRING(kw_name));
5006 #endif
5007 }
5008 """)
5010 #------------------------------------------------------------------------------------
5011 #
5012 # __Pyx_CheckKeywordStrings raises an error if non-string keywords
5013 # were passed to a function, or if any keywords were passed to a
5014 # function that does not accept them.
5016 keyword_string_check_utility_code = UtilityCode(
5017 proto = """
5018 static INLINE int __Pyx_CheckKeywordStrings(PyObject *kwdict,
5019 const char* function_name, int kw_allowed); /*proto*/
5020 """,
5021 impl = """
5022 static INLINE int __Pyx_CheckKeywordStrings(
5023 PyObject *kwdict,
5024 const char* function_name,
5025 int kw_allowed)
5026 {
5027 PyObject* key = 0;
5028 Py_ssize_t pos = 0;
5029 while (PyDict_Next(kwdict, &pos, &key, 0)) {
5030 #if PY_MAJOR_VERSION < 3
5031 if (unlikely(!PyString_CheckExact(key)) && unlikely(!PyString_Check(key)))
5032 #else
5033 if (unlikely(!PyUnicode_CheckExact(key)) && unlikely(!PyUnicode_Check(key)))
5034 #endif
5035 goto invalid_keyword_type;
5036 }
5037 if ((!kw_allowed) && unlikely(key))
5038 goto invalid_keyword;
5039 return 1;
5040 invalid_keyword_type:
5041 PyErr_Format(PyExc_TypeError,
5042 "%s() keywords must be strings", function_name);
5043 return 0;
5044 invalid_keyword:
5045 PyErr_Format(PyExc_TypeError,
5046 #if PY_MAJOR_VERSION < 3
5047 "%s() got an unexpected keyword argument '%s'",
5048 function_name, PyString_AsString(key));
5049 #else
5050 "%s() got an unexpected keyword argument '%U'",
5051 function_name, key);
5052 #endif
5053 return 0;
5054 }
5055 """)
5057 #------------------------------------------------------------------------------------
5058 #
5059 # __Pyx_ParseOptionalKeywords copies the optional/unknown keyword
5060 # arguments from the kwds dict into kwds2. If kwds2 is NULL, unknown
5061 # keywords will raise an invalid keyword error.
5062 #
5063 # Three kinds of errors are checked: 1) non-string keywords, 2)
5064 # unexpected keywords and 3) overlap with positional arguments.
5065 #
5066 # If num_posargs is greater 0, it denotes the number of positional
5067 # arguments that were passed and that must therefore not appear
5068 # amongst the keywords as well.
5069 #
5070 # This method does not check for required keyword arguments.
5071 #
5073 parse_keywords_utility_code = UtilityCode(
5074 proto = """
5075 static int __Pyx_ParseOptionalKeywords(PyObject *kwds, PyObject **argnames[], \
5076 PyObject *kwds2, PyObject *values[], Py_ssize_t num_pos_args, \
5077 const char* function_name); /*proto*/
5078 """,
5079 impl = """
5080 static int __Pyx_ParseOptionalKeywords(
5081 PyObject *kwds,
5082 PyObject **argnames[],
5083 PyObject *kwds2,
5084 PyObject *values[],
5085 Py_ssize_t num_pos_args,
5086 const char* function_name)
5087 {
5088 PyObject *key = 0, *value = 0;
5089 Py_ssize_t pos = 0;
5090 PyObject*** name;
5091 PyObject*** first_kw_arg = argnames + num_pos_args;
5093 while (PyDict_Next(kwds, &pos, &key, &value)) {
5094 name = first_kw_arg;
5095 while (*name && (**name != key)) name++;
5096 if (*name) {
5097 values[name-argnames] = value;
5098 } else {
5099 #if PY_MAJOR_VERSION < 3
5100 if (unlikely(!PyString_CheckExact(key)) && unlikely(!PyString_Check(key))) {
5101 #else
5102 if (unlikely(!PyUnicode_CheckExact(key)) && unlikely(!PyUnicode_Check(key))) {
5103 #endif
5104 goto invalid_keyword_type;
5105 } else {
5106 for (name = first_kw_arg; *name; name++) {
5107 #if PY_MAJOR_VERSION >= 3
5108 if (PyUnicode_GET_SIZE(**name) == PyUnicode_GET_SIZE(key) &&
5109 PyUnicode_Compare(**name, key) == 0) break;
5110 #else
5111 if (PyString_GET_SIZE(**name) == PyString_GET_SIZE(key) &&
5112 strcmp(PyString_AS_STRING(**name),
5113 PyString_AS_STRING(key)) == 0) break;
5114 #endif
5115 }
5116 if (*name) {
5117 values[name-argnames] = value;
5118 } else {
5119 /* unexpected keyword found */
5120 for (name=argnames; name != first_kw_arg; name++) {
5121 if (**name == key) goto arg_passed_twice;
5122 #if PY_MAJOR_VERSION >= 3
5123 if (PyUnicode_GET_SIZE(**name) == PyUnicode_GET_SIZE(key) &&
5124 PyUnicode_Compare(**name, key) == 0) goto arg_passed_twice;
5125 #else
5126 if (PyString_GET_SIZE(**name) == PyString_GET_SIZE(key) &&
5127 strcmp(PyString_AS_STRING(**name),
5128 PyString_AS_STRING(key)) == 0) goto arg_passed_twice;
5129 #endif
5130 }
5131 if (kwds2) {
5132 if (unlikely(PyDict_SetItem(kwds2, key, value))) goto bad;
5133 } else {
5134 goto invalid_keyword;
5135 }
5136 }
5137 }
5138 }
5139 }
5140 return 0;
5141 arg_passed_twice:
5142 __Pyx_RaiseDoubleKeywordsError(function_name, **name);
5143 goto bad;
5144 invalid_keyword_type:
5145 PyErr_Format(PyExc_TypeError,
5146 "%s() keywords must be strings", function_name);
5147 goto bad;
5148 invalid_keyword:
5149 PyErr_Format(PyExc_TypeError,
5150 #if PY_MAJOR_VERSION < 3
5151 "%s() got an unexpected keyword argument '%s'",
5152 function_name, PyString_AsString(key));
5153 #else
5154 "%s() got an unexpected keyword argument '%U'",
5155 function_name, key);
5156 #endif
5157 bad:
5158 return -1;
5159 }
5160 """)
5162 #------------------------------------------------------------------------------------
5164 unraisable_exception_utility_code = UtilityCode(
5165 proto = """
5166 static void __Pyx_WriteUnraisable(const char *name); /*proto*/
5167 """,
5168 impl = """
5169 static void __Pyx_WriteUnraisable(const char *name) {
5170 PyObject *old_exc, *old_val, *old_tb;
5171 PyObject *ctx;
5172 __Pyx_ErrFetch(&old_exc, &old_val, &old_tb);
5173 #if PY_MAJOR_VERSION < 3
5174 ctx = PyString_FromString(name);
5175 #else
5176 ctx = PyUnicode_FromString(name);
5177 #endif
5178 __Pyx_ErrRestore(old_exc, old_val, old_tb);
5179 if (!ctx) {
5180 PyErr_WriteUnraisable(Py_None);
5181 } else {
5182 PyErr_WriteUnraisable(ctx);
5183 Py_DECREF(ctx);
5184 }
5185 }
5186 """)
5188 #------------------------------------------------------------------------------------
5190 traceback_utility_code = UtilityCode(
5191 proto = """
5192 static void __Pyx_AddTraceback(const char *funcname); /*proto*/
5193 """,
5194 impl = """
5195 #include "compile.h"
5196 #include "frameobject.h"
5197 #include "traceback.h"
5199 static void __Pyx_AddTraceback(const char *funcname) {
5200 PyObject *py_srcfile = 0;
5201 PyObject *py_funcname = 0;
5202 PyObject *py_globals = 0;
5203 PyObject *empty_string = 0;
5204 PyCodeObject *py_code = 0;
5205 PyFrameObject *py_frame = 0;
5207 #if PY_MAJOR_VERSION < 3
5208 py_srcfile = PyString_FromString(%(FILENAME)s);
5209 #else
5210 py_srcfile = PyUnicode_FromString(%(FILENAME)s);
5211 #endif
5212 if (!py_srcfile) goto bad;
5213 if (%(CLINENO)s) {
5214 #if PY_MAJOR_VERSION < 3
5215 py_funcname = PyString_FromFormat( "%%s (%%s:%%d)", funcname, %(CFILENAME)s, %(CLINENO)s);
5216 #else
5217 py_funcname = PyUnicode_FromFormat( "%%s (%%s:%%d)", funcname, %(CFILENAME)s, %(CLINENO)s);
5218 #endif
5219 }
5220 else {
5221 #if PY_MAJOR_VERSION < 3
5222 py_funcname = PyString_FromString(funcname);
5223 #else
5224 py_funcname = PyUnicode_FromString(funcname);
5225 #endif
5226 }
5227 if (!py_funcname) goto bad;
5228 py_globals = PyModule_GetDict(%(GLOBALS)s);
5229 if (!py_globals) goto bad;
5230 #if PY_MAJOR_VERSION < 3
5231 empty_string = PyString_FromStringAndSize("", 0);
5232 #else
5233 empty_string = PyBytes_FromStringAndSize("", 0);
5234 #endif
5235 if (!empty_string) goto bad;
5236 py_code = PyCode_New(
5237 0, /*int argcount,*/
5238 #if PY_MAJOR_VERSION >= 3
5239 0, /*int kwonlyargcount,*/
5240 #endif
5241 0, /*int nlocals,*/
5242 0, /*int stacksize,*/
5243 0, /*int flags,*/
5244 empty_string, /*PyObject *code,*/
5245 %(EMPTY_TUPLE)s, /*PyObject *consts,*/
5246 %(EMPTY_TUPLE)s, /*PyObject *names,*/
5247 %(EMPTY_TUPLE)s, /*PyObject *varnames,*/
5248 %(EMPTY_TUPLE)s, /*PyObject *freevars,*/
5249 %(EMPTY_TUPLE)s, /*PyObject *cellvars,*/
5250 py_srcfile, /*PyObject *filename,*/
5251 py_funcname, /*PyObject *name,*/
5252 %(LINENO)s, /*int firstlineno,*/
5253 empty_string /*PyObject *lnotab*/
5254 );
5255 if (!py_code) goto bad;
5256 py_frame = PyFrame_New(
5257 PyThreadState_GET(), /*PyThreadState *tstate,*/
5258 py_code, /*PyCodeObject *code,*/
5259 py_globals, /*PyObject *globals,*/
5260 0 /*PyObject *locals*/
5261 );
5262 if (!py_frame) goto bad;
5263 py_frame->f_lineno = %(LINENO)s;
5264 PyTraceBack_Here(py_frame);
5265 bad:
5266 Py_XDECREF(py_srcfile);
5267 Py_XDECREF(py_funcname);
5268 Py_XDECREF(empty_string);
5269 Py_XDECREF(py_code);
5270 Py_XDECREF(py_frame);
5271 }
5272 """ % {
5273 'FILENAME': Naming.filename_cname,
5274 'LINENO': Naming.lineno_cname,
5275 'CFILENAME': Naming.cfilenm_cname,
5276 'CLINENO': Naming.clineno_cname,
5277 'GLOBALS': Naming.module_cname,
5278 'EMPTY_TUPLE' : Naming.empty_tuple,
5279 })
5281 restore_exception_utility_code = UtilityCode(
5282 proto = """
5283 static INLINE void __Pyx_ErrRestore(PyObject *type, PyObject *value, PyObject *tb); /*proto*/
5284 static INLINE void __Pyx_ErrFetch(PyObject **type, PyObject **value, PyObject **tb); /*proto*/
5285 """,
5286 impl = """
5287 static INLINE void __Pyx_ErrRestore(PyObject *type, PyObject *value, PyObject *tb) {
5288 PyObject *tmp_type, *tmp_value, *tmp_tb;
5289 PyThreadState *tstate = PyThreadState_GET();
5291 tmp_type = tstate->exc_type;
5292 tmp_value = tstate->exc_value;
5293 tmp_tb = tstate->exc_traceback;
5294 tstate->exc_type = 0;
5295 tstate->exc_value = 0;
5296 tstate->exc_traceback = 0;
5297 Py_XDECREF(tmp_type);
5298 Py_XDECREF(tmp_value);
5299 Py_XDECREF(tmp_tb);
5301 tmp_type = tstate->curexc_type;
5302 tmp_value = tstate->curexc_value;
5303 tmp_tb = tstate->curexc_traceback;
5304 tstate->curexc_type = type;
5305 tstate->curexc_value = value;
5306 tstate->curexc_traceback = tb;
5307 Py_XDECREF(tmp_type);
5308 Py_XDECREF(tmp_value);
5309 Py_XDECREF(tmp_tb);
5310 }
5312 static INLINE void __Pyx_ErrFetch(PyObject **type, PyObject **value, PyObject **tb) {
5313 PyThreadState *tstate = PyThreadState_GET();
5314 *type = tstate->curexc_type;
5315 *value = tstate->curexc_value;
5316 *tb = tstate->curexc_traceback;
5318 tstate->curexc_type = 0;
5319 tstate->curexc_value = 0;
5320 tstate->curexc_traceback = 0;
5321 }
5323 """)
5325 #------------------------------------------------------------------------------------
5327 set_vtable_utility_code = UtilityCode(
5328 proto = """
5329 static int __Pyx_SetVtable(PyObject *dict, void *vtable); /*proto*/
5330 """,
5331 impl = """
5332 static int __Pyx_SetVtable(PyObject *dict, void *vtable) {
5333 PyObject *pycobj = 0;
5334 int result;
5336 pycobj = PyCObject_FromVoidPtr(vtable, 0);
5337 if (!pycobj)
5338 goto bad;
5339 if (PyDict_SetItemString(dict, "__pyx_vtable__", pycobj) < 0)
5340 goto bad;
5341 result = 0;
5342 goto done;
5344 bad:
5345 result = -1;
5346 done:
5347 Py_XDECREF(pycobj);
5348 return result;
5349 }
5350 """)
5352 #------------------------------------------------------------------------------------
5354 get_vtable_utility_code = UtilityCode(
5355 proto = """
5356 static int __Pyx_GetVtable(PyObject *dict, void *vtabptr); /*proto*/
5357 """,
5358 impl = r"""
5359 static int __Pyx_GetVtable(PyObject *dict, void *vtabptr) {
5360 int result;
5361 PyObject *pycobj;
5363 pycobj = PyMapping_GetItemString(dict, (char *)"__pyx_vtable__");
5364 if (!pycobj)
5365 goto bad;
5366 *(void **)vtabptr = PyCObject_AsVoidPtr(pycobj);
5367 if (!*(void **)vtabptr)
5368 goto bad;
5369 result = 0;
5370 goto done;
5372 bad:
5373 result = -1;
5374 done:
5375 Py_XDECREF(pycobj);
5376 return result;
5377 }
5378 """)
5380 #------------------------------------------------------------------------------------
5382 init_string_tab_utility_code = UtilityCode(
5383 proto = """
5384 static int __Pyx_InitStrings(__Pyx_StringTabEntry *t); /*proto*/
5385 """,
5386 impl = """
5387 static int __Pyx_InitStrings(__Pyx_StringTabEntry *t) {
5388 while (t->p) {
5389 #if PY_MAJOR_VERSION < 3
5390 if (t->is_unicode && (!t->is_identifier)) {
5391 *t->p = PyUnicode_DecodeUTF8(t->s, t->n - 1, NULL);
5392 } else if (t->intern) {
5393 *t->p = PyString_InternFromString(t->s);
5394 } else {
5395 *t->p = PyString_FromStringAndSize(t->s, t->n - 1);
5396 }
5397 #else /* Python 3+ has unicode identifiers */
5398 if (t->is_identifier || (t->is_unicode && t->intern)) {
5399 *t->p = PyUnicode_InternFromString(t->s);
5400 } else if (t->is_unicode) {
5401 *t->p = PyUnicode_FromStringAndSize(t->s, t->n - 1);
5402 } else {
5403 *t->p = PyBytes_FromStringAndSize(t->s, t->n - 1);
5404 }
5405 #endif
5406 if (!*t->p)
5407 return -1;
5408 ++t;
5409 }
5410 return 0;
5411 }
5412 """)
5414 #------------------------------------------------------------------------------------
5416 get_exception_utility_code = UtilityCode(
5417 proto = """
5418 static int __Pyx_GetException(PyObject **type, PyObject **value, PyObject **tb); /*proto*/
5419 """,
5420 impl = """
5421 static int __Pyx_GetException(PyObject **type, PyObject **value, PyObject **tb) {
5422 PyObject *tmp_type, *tmp_value, *tmp_tb;
5423 PyThreadState *tstate = PyThreadState_GET();
5424 __Pyx_ErrFetch(type, value, tb);
5425 PyErr_NormalizeException(type, value, tb);
5426 if (PyErr_Occurred())
5427 goto bad;
5428 Py_INCREF(*type);
5429 Py_INCREF(*value);
5430 Py_INCREF(*tb);
5431 tmp_type = tstate->exc_type;
5432 tmp_value = tstate->exc_value;
5433 tmp_tb = tstate->exc_traceback;
5434 tstate->exc_type = *type;
5435 tstate->exc_value = *value;
5436 tstate->exc_traceback = *tb;
5437 /* Make sure tstate is in a consistent state when we XDECREF
5438 these objects (XDECREF may run arbitrary code). */
5439 Py_XDECREF(tmp_type);
5440 Py_XDECREF(tmp_value);
5441 Py_XDECREF(tmp_tb);
5442 return 0;
5443 bad:
5444 Py_XDECREF(*type);
5445 Py_XDECREF(*value);
5446 Py_XDECREF(*tb);
5447 return -1;
5448 }
5450 """)
5452 #------------------------------------------------------------------------------------
5454 reset_exception_utility_code = UtilityCode(
5455 proto = """
5456 static INLINE void __Pyx_ExceptionSave(PyObject **type, PyObject **value, PyObject **tb); /*proto*/
5457 static void __Pyx_ExceptionReset(PyObject *type, PyObject *value, PyObject *tb); /*proto*/
5458 """,
5459 impl = """
5460 static INLINE void __Pyx_ExceptionSave(PyObject **type, PyObject **value, PyObject **tb) {
5461 PyThreadState *tstate = PyThreadState_GET();
5462 *type = tstate->exc_type;
5463 *value = tstate->exc_value;
5464 *tb = tstate->exc_traceback;
5465 Py_XINCREF(*type);
5466 Py_XINCREF(*value);
5467 Py_XINCREF(*tb);
5468 }
5470 static void __Pyx_ExceptionReset(PyObject *type, PyObject *value, PyObject *tb) {
5471 PyObject *tmp_type, *tmp_value, *tmp_tb;
5472 PyThreadState *tstate = PyThreadState_GET();
5473 tmp_type = tstate->exc_type;
5474 tmp_value = tstate->exc_value;
5475 tmp_tb = tstate->exc_traceback;
5476 tstate->exc_type = type;
5477 tstate->exc_value = value;
5478 tstate->exc_traceback = tb;
5479 Py_XDECREF(tmp_type);
5480 Py_XDECREF(tmp_value);
5481 Py_XDECREF(tmp_tb);
5482 }
5483 """)
5485 #------------------------------------------------------------------------------------
