merge
[cython.git] / Cython / Compiler / Nodes.py
1
2 #
3 #   Pyrex - Parse tree nodes
4 #
5
6 import cython
7 from cython import set
8 cython.declare(sys=object, os=object, time=object, copy=object,
9                Builtin=object, error=object, warning=object, Naming=object, PyrexTypes=object,
10                py_object_type=object, ModuleScope=object, LocalScope=object, ClosureScope=object, \
11                StructOrUnionScope=object, PyClassScope=object, CClassScope=object,
12                CppClassScope=object, UtilityCode=object, EncodedString=object,
13                absolute_path_length=cython.Py_ssize_t)
14
15 import sys, os, time, copy
16
17 import Builtin
18 from Errors import error, warning, InternalError
19 import Naming
20 import PyrexTypes
21 import TypeSlots
22 from PyrexTypes import py_object_type, error_type, CFuncType
23 from Symtab import ModuleScope, LocalScope, ClosureScope, \
24     StructOrUnionScope, PyClassScope, CClassScope, CppClassScope
25 from Cython.Utils import open_new_file, replace_suffix
26 from Code import UtilityCode, ClosureTempAllocator
27 from StringEncoding import EncodedString, escape_byte_string, split_string_literal
28 import Options
29 import ControlFlow
30 import DebugFlags
31
32 absolute_path_length = 0
33
34 def relative_position(pos):
35     """
36     We embed the relative filename in the generated C file, since we
37     don't want to have to regnerate and compile all the source code
38     whenever the Python install directory moves (which could happen,
39     e.g,. when distributing binaries.)
40     
41     INPUT:
42         a position tuple -- (absolute filename, line number column position)
43
44     OUTPUT:
45         relative filename
46         line number
47
48     AUTHOR: William Stein
49     """
50     global absolute_path_length
51     if absolute_path_length==0:
52         absolute_path_length = len(os.path.abspath(os.getcwd())) 
53     return (pos[0].get_filenametable_entry()[absolute_path_length+1:], pos[1])
54
55 def embed_position(pos, docstring):
56     if not Options.embed_pos_in_docstring:
57         return docstring
58     pos_line = u'File: %s (starting at line %s)' % relative_position(pos)
59     if docstring is None:
60         # unicode string
61         return EncodedString(pos_line)
62
63     # make sure we can encode the filename in the docstring encoding
64     # otherwise make the docstring a unicode string
65     encoding = docstring.encoding
66     if encoding is not None:
67         try:
68             encoded_bytes = pos_line.encode(encoding)
69         except UnicodeEncodeError:
70             encoding = None
71
72     if not docstring:
73         # reuse the string encoding of the original docstring
74         doc = EncodedString(pos_line)
75     else:
76         doc = EncodedString(pos_line + u'\n' + docstring)
77     doc.encoding = encoding
78     return doc
79
80
81 from Code import CCodeWriter
82 from types import FunctionType
83
84 def write_func_call(func):
85     def f(*args, **kwds):
86         if len(args) > 1 and isinstance(args[1], CCodeWriter):
87             # here we annotate the code with this function call
88             # but only if new code is generated
89             node, code = args[:2]
90             marker = '                    /* %s -> %s.%s %s */' % (
91                     ' ' * code.call_level,
92                     node.__class__.__name__, 
93                     func.__name__, 
94                     node.pos[1:])
95             pristine = code.buffer.stream.tell()
96             code.putln(marker)
97             start = code.buffer.stream.tell()
98             code.call_level += 4
99             res = func(*args, **kwds)
100             code.call_level -= 4
101             if start == code.buffer.stream.tell():
102                 code.buffer.stream.seek(pristine)
103             else:
104                 marker = marker.replace('->', '<-')
105                 code.putln(marker)
106             return res
107         else:
108             return func(*args, **kwds)
109     return f
110
111 class VerboseCodeWriter(type):
112     # Set this as a metaclass to trace function calls in code.
113     # This slows down code generation and makes much larger files. 
114     def __new__(cls, name, bases, attrs):
115         attrs = dict(attrs)
116         for mname, m in attrs.items():
117             if isinstance(m, FunctionType):
118                 attrs[mname] = write_func_call(m)
119         return super(VerboseCodeWriter, cls).__new__(cls, name, bases, attrs)
120
121
122 class Node(object):
123     #  pos         (string, int, int)   Source file position
124     #  is_name     boolean              Is a NameNode
125     #  is_literal  boolean              Is a ConstNode
126
127     if DebugFlags.debug_trace_code_generation:
128         __metaclass__ = VerboseCodeWriter
129     
130     is_name = 0
131     is_literal = 0
132     temps = None
133
134     # All descandants should set child_attrs to a list of the attributes
135     # containing nodes considered "children" in the tree. Each such attribute
136     # can either contain a single node or a list of nodes. See Visitor.py.
137     child_attrs = None
138     
139     def __init__(self, pos, **kw):
140         self.pos = pos
141         self.__dict__.update(kw)
142     
143     gil_message = "Operation"
144
145     nogil_check = None
146
147     def gil_error(self, env=None):
148         error(self.pos, "%s not allowed without gil" % self.gil_message)
149     
150     cpp_message = "Operation"
151     
152     def cpp_check(self, env):
153         if not env.is_cpp():
154             self.cpp_error()
155
156     def cpp_error(self):
157         error(self.pos, "%s only allowed in c++" % self.cpp_message)
158
159     def clone_node(self):
160         """Clone the node. This is defined as a shallow copy, except for member lists
161            amongst the child attributes (from get_child_accessors) which are also
162            copied. Lists containing child nodes are thus seen as a way for the node
163            to hold multiple children directly; the list is not treated as a seperate
164            level in the tree."""
165         result = copy.copy(self)
166         for attrname in result.child_attrs:
167             value = getattr(result, attrname)
168             if isinstance(value, list):
169                 setattr(result, attrname, [x for x in value])
170         return result
171     
172     
173     #
174     #  There are 4 phases of parse tree processing, applied in order to
175     #  all the statements in a given scope-block:
176     #
177     #  (0) analyse_control_flow
178     #        Create the control flow tree into which state can be asserted and
179     #        queried.
180     #
181     #  (1) analyse_declarations
182     #        Make symbol table entries for all declarations at the current
183     #        level, both explicit (def, cdef, etc.) and implicit (assignment
184     #        to an otherwise undeclared name).
185     #
186     #  (2) analyse_expressions
187     #         Determine the result types of expressions and fill in the
188     #         'type' attribute of each ExprNode. Insert coercion nodes into the
189     #         tree where needed to convert to and from Python objects. 
190     #         Allocate temporary locals for intermediate results. Fill
191     #         in the 'result_code' attribute of each ExprNode with a C code
192     #         fragment.
193     #
194     #  (3) generate_code
195     #         Emit C code for all declarations, statements and expressions.
196     #         Recursively applies the 3 processing phases to the bodies of
197     #         functions.
198     #
199     
200     def analyse_control_flow(self, env):
201         pass
202     
203     def analyse_declarations(self, env):
204         pass
205     
206     def analyse_expressions(self, env):
207         raise InternalError("analyse_expressions not implemented for %s" % \
208             self.__class__.__name__)
209     
210     def generate_code(self, code):
211         raise InternalError("generate_code not implemented for %s" % \
212             self.__class__.__name__)
213             
214     def annotate(self, code):
215         # mro does the wrong thing
216         if isinstance(self, BlockNode):
217             self.body.annotate(code)
218             
219     def end_pos(self):
220         try:
221             return self._end_pos
222         except AttributeError:
223             pos = self.pos
224             if not self.child_attrs:
225                 self._end_pos = pos
226                 return pos
227             for attr in self.child_attrs:
228                 child = getattr(self, attr)
229                 # Sometimes lists, sometimes nodes
230                 if child is None:
231                     pass
232                 elif isinstance(child, list):
233                     for c in child:
234                         pos = max(pos, c.end_pos())
235                 else:
236                     pos = max(pos, child.end_pos())
237             self._end_pos = pos
238             return pos
239
240     def dump(self, level=0, filter_out=("pos",), cutoff=100, encountered=None):
241         if cutoff == 0:
242             return "<...nesting level cutoff...>"
243         if encountered is None:
244             encountered = set()
245         if id(self) in encountered:
246             return "<%s (0x%x) -- already output>" % (self.__class__.__name__, id(self))
247         encountered.add(id(self))
248         
249         def dump_child(x, level):
250             if isinstance(x, Node):
251                 return x.dump(level, filter_out, cutoff-1, encountered)
252             elif isinstance(x, list):
253                 return "[%s]" % ", ".join([dump_child(item, level) for item in x])
254             else:
255                 return repr(x)
256             
257         
258         attrs = [(key, value) for key, value in self.__dict__.items() if key not in filter_out]
259         if len(attrs) == 0:
260             return "<%s (0x%x)>" % (self.__class__.__name__, id(self))
261         else:
262             indent = "  " * level
263             res = "<%s (0x%x)\n" % (self.__class__.__name__, id(self))
264             for key, value in attrs:
265                 res += "%s  %s: %s\n" % (indent, key, dump_child(value, level + 1))
266             res += "%s>" % indent
267             return res
268
269 class CompilerDirectivesNode(Node):
270     """
271     Sets compiler directives for the children nodes
272     """
273     #  directives     {string:value}  A dictionary holding the right value for
274     #                                 *all* possible directives.
275     #  body           Node
276     child_attrs = ["body"]
277
278     def analyse_control_flow(self, env):
279         old = env.directives
280         env.directives = self.directives
281         self.body.analyse_control_flow(env)
282         env.directives = old
283
284     def analyse_declarations(self, env):
285         old = env.directives
286         env.directives = self.directives
287         self.body.analyse_declarations(env)
288         env.directives = old
289     
290     def analyse_expressions(self, env):
291         old = env.directives
292         env.directives = self.directives
293         self.body.analyse_expressions(env)
294         env.directives = old
295
296     def generate_function_definitions(self, env, code):
297         env_old = env.directives
298         code_old = code.globalstate.directives
299         code.globalstate.directives = self.directives
300         self.body.generate_function_definitions(env, code)
301         env.directives = env_old
302         code.globalstate.directives = code_old
303             
304     def generate_execution_code(self, code):
305         old = code.globalstate.directives
306         code.globalstate.directives = self.directives
307         self.body.generate_execution_code(code)
308         code.globalstate.directives = old
309             
310     def annotate(self, code):
311         old = code.globalstate.directives
312         code.globalstate.directives = self.directives
313         self.body.annotate(code)
314         code.globalstate.directives = old
315         
316 class BlockNode(object):
317     #  Mixin class for nodes representing a declaration block.
318
319     def generate_cached_builtins_decls(self, env, code):
320         entries = env.global_scope().undeclared_cached_builtins
321         for entry in entries:
322             code.globalstate.add_cached_builtin_decl(entry)
323         del entries[:]
324
325     def generate_lambda_definitions(self, env, code):
326         for node in env.lambda_defs:
327             node.generate_function_definitions(env, code)
328
329 class StatListNode(Node):
330     # stats     a list of StatNode
331     
332     child_attrs = ["stats"]
333
334     def create_analysed(pos, env, *args, **kw):
335         node = StatListNode(pos, *args, **kw)
336         return node # No node-specific analysis necesarry
337     create_analysed = staticmethod(create_analysed)
338     
339     def analyse_control_flow(self, env):
340         for stat in self.stats:
341             stat.analyse_control_flow(env)
342
343     def analyse_declarations(self, env):
344         #print "StatListNode.analyse_declarations" ###
345         for stat in self.stats:
346             stat.analyse_declarations(env)
347     
348     def analyse_expressions(self, env):
349         #print "StatListNode.analyse_expressions" ###
350         for stat in self.stats:
351             stat.analyse_expressions(env)
352     
353     def generate_function_definitions(self, env, code):
354         #print "StatListNode.generate_function_definitions" ###
355         for stat in self.stats:
356             stat.generate_function_definitions(env, code)
357             
358     def generate_execution_code(self, code):
359         #print "StatListNode.generate_execution_code" ###
360         for stat in self.stats:
361             code.mark_pos(stat.pos)
362             stat.generate_execution_code(code)
363             
364     def annotate(self, code):
365         for stat in self.stats:
366             stat.annotate(code)
367     
368
369 class StatNode(Node):
370     #
371     #  Code generation for statements is split into the following subphases:
372     #
373     #  (1) generate_function_definitions
374     #        Emit C code for the definitions of any structs,
375     #        unions, enums and functions defined in the current
376     #        scope-block.
377     #
378     #  (2) generate_execution_code
379     #        Emit C code for executable statements.
380     #
381     
382     def generate_function_definitions(self, env, code):
383         pass
384     
385     def generate_execution_code(self, code):
386         raise InternalError("generate_execution_code not implemented for %s" % \
387             self.__class__.__name__)
388
389
390 class CDefExternNode(StatNode):
391     #  include_file   string or None
392     #  body           StatNode
393     
394     child_attrs = ["body"]
395     
396     def analyse_declarations(self, env):
397         if self.include_file:
398             env.add_include_file(self.include_file)
399         old_cinclude_flag = env.in_cinclude
400         env.in_cinclude = 1
401         self.body.analyse_declarations(env)
402         env.in_cinclude = old_cinclude_flag
403     
404     def analyse_expressions(self, env):
405         pass
406     
407     def generate_execution_code(self, code):
408         pass
409
410     def annotate(self, code):
411         self.body.annotate(code)
412         
413
414 class CDeclaratorNode(Node):
415     # Part of a C declaration.
416     #
417     # Processing during analyse_declarations phase:
418     #
419     #   analyse
420     #      Returns (name, type) pair where name is the
421     #      CNameDeclaratorNode of the name being declared 
422     #      and type is the type it is being declared as.
423     #
424     #  calling_convention  string   Calling convention of CFuncDeclaratorNode
425     #                               for which this is a base 
426
427     child_attrs = []
428
429     calling_convention = ""
430
431
432 class CNameDeclaratorNode(CDeclaratorNode):
433     #  name    string             The Pyrex name being declared
434     #  cname   string or None     C name, if specified
435     #  default ExprNode or None   the value assigned on declaration
436     
437     child_attrs = ['default']
438     
439     default = None
440     
441     def analyse(self, base_type, env, nonempty = 0):
442         if nonempty and self.name == '':
443             # May have mistaken the name for the type. 
444             if base_type.is_ptr or base_type.is_array or base_type.is_buffer:
445                 error(self.pos, "Missing argument name")
446             elif base_type.is_void:
447                 error(self.pos, "Use spam() rather than spam(void) to declare a function with no arguments.")
448             else:
449                 self.name = base_type.declaration_code("", for_display=1, pyrex=1)
450                 base_type = py_object_type
451         self.type = base_type
452         return self, base_type
453         
454 class CPtrDeclaratorNode(CDeclaratorNode):
455     # base     CDeclaratorNode
456     
457     child_attrs = ["base"]
458
459     def analyse(self, base_type, env, nonempty = 0):
460         if base_type.is_pyobject:
461             error(self.pos,
462                 "Pointer base type cannot be a Python object")
463         ptr_type = PyrexTypes.c_ptr_type(base_type)
464         return self.base.analyse(ptr_type, env, nonempty = nonempty)
465
466 class CReferenceDeclaratorNode(CDeclaratorNode):
467     # base     CDeclaratorNode
468
469     child_attrs = ["base"]
470
471     def analyse(self, base_type, env, nonempty = 0):
472         if base_type.is_pyobject:
473             error(self.pos,
474                   "Reference base type cannot be a Python object")
475         ref_type = PyrexTypes.c_ref_type(base_type)
476         return self.base.analyse(ref_type, env, nonempty = nonempty)
477
478 class CArrayDeclaratorNode(CDeclaratorNode):
479     # base        CDeclaratorNode
480     # dimension   ExprNode
481
482     child_attrs = ["base", "dimension"]
483     
484     def analyse(self, base_type, env, nonempty = 0):
485         if base_type.is_cpp_class:
486             from ExprNodes import TupleNode
487             if isinstance(self.dimension, TupleNode):
488                 args = self.dimension.args
489             else:
490                 args = self.dimension,
491             values = [v.analyse_as_type(env) for v in args]
492             if None in values:
493                 ix = values.index(None)
494                 error(args[ix].pos, "Template parameter not a type.")
495                 return error_type
496             base_type = base_type.specialize_here(self.pos, values)
497             return self.base.analyse(base_type, env, nonempty = nonempty)
498         if self.dimension:
499             self.dimension.analyse_const_expression(env)
500             if not self.dimension.type.is_int:
501                 error(self.dimension.pos, "Array dimension not integer")
502             size = self.dimension.get_constant_c_result_code()
503             if size is not None:
504                 try:
505                     size = int(size)
506                 except ValueError:
507                     # runtime constant?
508                     pass
509         else:
510             size = None
511         if not base_type.is_complete():
512             error(self.pos,
513                 "Array element type '%s' is incomplete" % base_type)
514         if base_type.is_pyobject:
515             error(self.pos,
516                 "Array element cannot be a Python object")
517         if base_type.is_cfunction:
518             error(self.pos,
519                 "Array element cannot be a function")
520         array_type = PyrexTypes.c_array_type(base_type, size)
521         return self.base.analyse(array_type, env, nonempty = nonempty)
522
523
524 class CFuncDeclaratorNode(CDeclaratorNode):
525     # base             CDeclaratorNode
526     # args             [CArgDeclNode]
527     # has_varargs      boolean
528     # exception_value  ConstNode
529     # exception_check  boolean    True if PyErr_Occurred check needed
530     # nogil            boolean    Can be called without gil
531     # with_gil         boolean    Acquire gil around function body
532     
533     child_attrs = ["base", "args", "exception_value"]
534
535     overridable = 0
536     optional_arg_count = 0
537
538     def analyse(self, return_type, env, nonempty = 0):
539         if nonempty:
540             nonempty -= 1
541         func_type_args = []
542         for i, arg_node in enumerate(self.args):
543             name_declarator, type = arg_node.analyse(env, nonempty = nonempty,
544                                                      is_self_arg = (i == 0 and env.is_c_class_scope))
545             name = name_declarator.name
546             if name_declarator.cname:
547                 error(self.pos, 
548                     "Function argument cannot have C name specification")
549             # Turn *[] argument into **
550             if type.is_array:
551                 type = PyrexTypes.c_ptr_type(type.base_type)
552             # Catch attempted C-style func(void) decl
553             if type.is_void:
554                 error(arg_node.pos, "Use spam() rather than spam(void) to declare a function with no arguments.")
555             func_type_args.append(
556                 PyrexTypes.CFuncTypeArg(name, type, arg_node.pos))
557             if arg_node.default:
558                 self.optional_arg_count += 1
559             elif self.optional_arg_count:
560                 error(self.pos, "Non-default argument follows default argument")
561         
562         if self.optional_arg_count:
563             scope = StructOrUnionScope()
564             arg_count_member = '%sn' % Naming.pyrex_prefix
565             scope.declare_var(arg_count_member, PyrexTypes.c_int_type, self.pos)
566             for arg in func_type_args[len(func_type_args)-self.optional_arg_count:]:
567                 scope.declare_var(arg.name, arg.type, arg.pos, allow_pyobject = 1)
568             struct_cname = env.mangle(Naming.opt_arg_prefix, self.base.name)
569             self.op_args_struct = env.global_scope().declare_struct_or_union(name = struct_cname,
570                                         kind = 'struct',
571                                         scope = scope,
572                                         typedef_flag = 0,
573                                         pos = self.pos,
574                                         cname = struct_cname)
575             self.op_args_struct.defined_in_pxd = 1
576             self.op_args_struct.used = 1
577         
578         exc_val = None
579         exc_check = 0
580         if self.exception_check == '+':
581             env.add_include_file('stdexcept')
582         if return_type.is_pyobject \
583             and (self.exception_value or self.exception_check) \
584             and self.exception_check != '+':
585                 error(self.pos,
586                     "Exception clause not allowed for function returning Python object")
587         else:
588             if self.exception_value:
589                 self.exception_value.analyse_const_expression(env)
590                 if self.exception_check == '+':
591                     self.exception_value.analyse_types(env)
592                     exc_val_type = self.exception_value.type
593                     if not exc_val_type.is_error and \
594                           not exc_val_type.is_pyobject and \
595                           not (exc_val_type.is_cfunction and not exc_val_type.return_type.is_pyobject and len(exc_val_type.args)==0):
596                         error(self.exception_value.pos,
597                             "Exception value must be a Python exception or cdef function with no arguments.")
598                     exc_val = self.exception_value
599                 else:
600                     self.exception_value = self.exception_value.coerce_to(return_type, env)
601                     if self.exception_value.analyse_const_expression(env):
602                         exc_val = self.exception_value.get_constant_c_result_code()
603                         if exc_val is None:
604                             raise InternalError("get_constant_c_result_code not implemented for %s" %
605                                 self.exception_value.__class__.__name__)
606                         if not return_type.assignable_from(self.exception_value.type):
607                             error(self.exception_value.pos,
608                                   "Exception value incompatible with function return type")
609             exc_check = self.exception_check
610         if return_type.is_array:
611             error(self.pos,
612                 "Function cannot return an array")
613         if return_type.is_cfunction:
614             error(self.pos,
615                 "Function cannot return a function")
616         func_type = PyrexTypes.CFuncType(
617             return_type, func_type_args, self.has_varargs, 
618             optional_arg_count = self.optional_arg_count,
619             exception_value = exc_val, exception_check = exc_check,
620             calling_convention = self.base.calling_convention,
621             nogil = self.nogil, with_gil = self.with_gil, is_overridable = self.overridable)
622         if self.optional_arg_count:
623             func_type.op_arg_struct = PyrexTypes.c_ptr_type(self.op_args_struct.type)
624         callspec = env.directives['callspec']
625         if callspec:
626             current = func_type.calling_convention
627             if current and current != callspec:
628                 error(self.pos, "cannot have both '%s' and '%s' "
629                       "calling conventions" % (current, callspec))
630             func_type.calling_convention = callspec
631         return self.base.analyse(func_type, env)
632
633
634 class CArgDeclNode(Node):
635     # Item in a function declaration argument list.
636     #
637     # base_type      CBaseTypeNode
638     # declarator     CDeclaratorNode
639     # not_none       boolean            Tagged with 'not None'
640     # or_none        boolean            Tagged with 'or None'
641     # accept_none    boolean            Resolved boolean for not_none/or_none
642     # default        ExprNode or None
643     # default_value  PyObjectConst      constant for default value
644     # annotation     ExprNode or None   Py3 function arg annotation
645     # is_self_arg    boolean            Is the "self" arg of an extension type method
646     # is_type_arg    boolean            Is the "class" arg of an extension type classmethod
647     # is_kw_only     boolean            Is a keyword-only argument
648
649     child_attrs = ["base_type", "declarator", "default"]
650
651     is_self_arg = 0
652     is_type_arg = 0
653     is_generic = 1
654     type = None
655     name_declarator = None
656     default_value = None
657     annotation = None
658
659     def analyse(self, env, nonempty = 0, is_self_arg = False):
660         if is_self_arg:
661             self.base_type.is_self_arg = self.is_self_arg = True
662         if self.type is None:
663             # The parser may missinterpret names as types...
664             # We fix that here.
665             if isinstance(self.declarator, CNameDeclaratorNode) and self.declarator.name == '':
666                 if nonempty:
667                     self.declarator.name = self.base_type.name
668                     self.base_type.name = None
669                     self.base_type.is_basic_c_type = False
670                 could_be_name = True
671             else:
672                 could_be_name = False
673             base_type = self.base_type.analyse(env, could_be_name = could_be_name)
674             if hasattr(self.base_type, 'arg_name') and self.base_type.arg_name:
675                 self.declarator.name = self.base_type.arg_name
676             # The parser is unable to resolve the ambiguity of [] as part of the 
677             # type (e.g. in buffers) or empty declarator (as with arrays). 
678             # This is only arises for empty multi-dimensional arrays.
679             if (base_type.is_array 
680                     and isinstance(self.base_type, TemplatedTypeNode) 
681                     and isinstance(self.declarator, CArrayDeclaratorNode)):
682                 declarator = self.declarator
683                 while isinstance(declarator.base, CArrayDeclaratorNode):
684                     declarator = declarator.base
685                 declarator.base = self.base_type.array_declarator
686                 base_type = base_type.base_type
687             return self.declarator.analyse(base_type, env, nonempty = nonempty)
688         else:
689             return self.name_declarator, self.type
690
691     def calculate_default_value_code(self, code):
692         if self.default_value is None:
693             if self.default:
694                 if self.default.is_literal:
695                     # will not output any code, just assign the result_code
696                     self.default.generate_evaluation_code(code)
697                     return self.type.cast_code(self.default.result())
698                 self.default_value = code.get_argument_default_const(self.type)
699         return self.default_value
700
701     def annotate(self, code):
702         if self.default:
703             self.default.annotate(code)
704
705
706 class CBaseTypeNode(Node):
707     # Abstract base class for C base type nodes.
708     #
709     # Processing during analyse_declarations phase:
710     #
711     #   analyse
712     #     Returns the type.
713     
714     pass
715     
716     def analyse_as_type(self, env):
717         return self.analyse(env)
718     
719 class CAnalysedBaseTypeNode(Node):
720     # type            type
721     
722     child_attrs = []
723     
724     def analyse(self, env, could_be_name = False):
725         return self.type
726
727 class CSimpleBaseTypeNode(CBaseTypeNode):
728     # name             string
729     # module_path      [string]     Qualifying name components
730     # is_basic_c_type  boolean
731     # signed           boolean
732     # longness         integer
733     # complex          boolean
734     # is_self_arg      boolean      Is self argument of C method
735     # ##is_type_arg      boolean      Is type argument of class method
736
737     child_attrs = []
738     arg_name = None   # in case the argument name was interpreted as a type
739     
740     def analyse(self, env, could_be_name = False):
741         # Return type descriptor.
742         #print "CSimpleBaseTypeNode.analyse: is_self_arg =", self.is_self_arg ###
743         type = None
744         if self.is_basic_c_type:
745             type = PyrexTypes.simple_c_type(self.signed, self.longness, self.name)
746             if not type:
747                 error(self.pos, "Unrecognised type modifier combination")
748         elif self.name == "object" and not self.module_path:
749             type = py_object_type
750         elif self.name is None:
751             if self.is_self_arg and env.is_c_class_scope:
752                 #print "CSimpleBaseTypeNode.analyse: defaulting to parent type" ###
753                 type = env.parent_type
754             ## elif self.is_type_arg and env.is_c_class_scope:
755             ##     type = Builtin.type_type
756             else:
757                 type = py_object_type
758         else:
759             if self.module_path:
760                 scope = env.find_imported_module(self.module_path, self.pos)
761             else:
762                 scope = env
763             if scope:
764                 if scope.is_c_class_scope:
765                     scope = scope.global_scope()
766                 entry = scope.lookup(self.name)
767                 if entry and entry.is_type:
768                     type = entry.type
769                 elif could_be_name:
770                     if self.is_self_arg and env.is_c_class_scope:
771                         type = env.parent_type
772                     ## elif self.is_type_arg and env.is_c_class_scope:
773                     ##     type = Builtin.type_type
774                     else:
775                         type = py_object_type
776                     self.arg_name = self.name
777                 else:
778                     if self.templates:
779                         if not self.name in self.templates:
780                             error(self.pos, "'%s' is not a type identifier" % self.name)
781                         type = PyrexTypes.TemplatePlaceholderType(self.name)
782                     else:
783                         error(self.pos, "'%s' is not a type identifier" % self.name)
784         if self.complex:
785             if not type.is_numeric or type.is_complex:
786                 error(self.pos, "can only complexify c numeric types")
787             type = PyrexTypes.CComplexType(type)
788             type.create_declaration_utility_code(env)
789         elif type is Builtin.complex_type:
790             # Special case: optimise builtin complex type into C's
791             # double complex.  The parser cannot do this (as for the
792             # normal scalar types) as the user may have redeclared the
793             # 'complex' type.  Testing for the exact type here works.
794             type = PyrexTypes.c_double_complex_type
795             type.create_declaration_utility_code(env)
796             self.complex = True
797         if type:
798             return type
799         else:
800             return PyrexTypes.error_type
801
802 class CNestedBaseTypeNode(CBaseTypeNode):
803     # For C++ classes that live inside other C++ classes. 
804
805     # name             string
806     # base_type        CBaseTypeNode
807
808     child_attrs = ['base_type']
809
810     def analyse(self, env, could_be_name = None):
811         base_type = self.base_type.analyse(env)
812         if base_type is PyrexTypes.error_type:
813             return PyrexTypes.error_type
814         if not base_type.is_cpp_class:
815             error(self.pos, "'%s' is not a valid type scope" % base_type)
816             return PyrexTypes.error_type
817         type_entry = base_type.scope.lookup_here(self.name)
818         if not type_entry or not type_entry.is_type:
819             error(self.pos, "'%s.%s' is not a type identifier" % (base_type, self.name))
820             return PyrexTypes.error_type
821         return type_entry.type
822
823 class TemplatedTypeNode(CBaseTypeNode):
824     #  After parsing:
825     #  positional_args  [ExprNode]        List of positional arguments
826     #  keyword_args     DictNode          Keyword arguments
827     #  base_type_node   CBaseTypeNode
828
829     #  After analysis:
830     #  type             PyrexTypes.BufferType or PyrexTypes.CppClassType  ...containing the right options
831
832
833     child_attrs = ["base_type_node", "positional_args",
834                    "keyword_args", "dtype_node"]
835
836     dtype_node = None
837
838     name = None
839     
840     def analyse(self, env, could_be_name = False, base_type = None):
841         if base_type is None:
842             base_type = self.base_type_node.analyse(env)
843         if base_type.is_error: return base_type
844         
845         if base_type.is_cpp_class:
846             # Templated class
847             if self.keyword_args and self.keyword_args.key_value_pairs:
848                 error(self.pos, "c++ templates cannot take keyword arguments");
849                 self.type = PyrexTypes.error_type
850             else:
851                 template_types = []
852                 for template_node in self.positional_args:
853                     type = template_node.analyse_as_type(env)
854                     if type is None:
855                         error(template_node.pos, "unknown type in template argument")
856                         return error_type
857                     template_types.append(type)
858                 self.type = base_type.specialize_here(self.pos, template_types)
859         
860         elif base_type.is_pyobject:
861             # Buffer
862             import Buffer
863
864             options = Buffer.analyse_buffer_options(
865                 self.pos,
866                 env,
867                 self.positional_args,
868                 self.keyword_args,
869                 base_type.buffer_defaults)
870
871             if sys.version_info[0] < 3:
872                 # Py 2.x enforces byte strings as keyword arguments ...
873                 options = dict([ (name.encode('ASCII'), value)
874                                  for name, value in options.items() ])
875
876             self.type = PyrexTypes.BufferType(base_type, **options)
877         
878         else:
879             # Array
880             empty_declarator = CNameDeclaratorNode(self.pos, name="", cname=None)
881             if len(self.positional_args) > 1 or self.keyword_args.key_value_pairs:
882                 error(self.pos, "invalid array declaration")
883                 self.type = PyrexTypes.error_type
884             else:
885                 # It would be nice to merge this class with CArrayDeclaratorNode, 
886                 # but arrays are part of the declaration, not the type...
887                 if not self.positional_args:
888                     dimension = None
889                 else:
890                     dimension = self.positional_args[0]
891                 self.array_declarator = CArrayDeclaratorNode(self.pos, 
892                     base = empty_declarator, 
893                     dimension = dimension)
894                 self.type = self.array_declarator.analyse(base_type, env)[1]
895         
896         return self.type
897
898 class CComplexBaseTypeNode(CBaseTypeNode):
899     # base_type   CBaseTypeNode
900     # declarator  CDeclaratorNode
901     
902     child_attrs = ["base_type", "declarator"]
903
904     def analyse(self, env, could_be_name = False):
905         base = self.base_type.analyse(env, could_be_name)
906         _, type = self.declarator.analyse(base, env)
907         return type
908
909
910 class CVarDefNode(StatNode):
911     #  C variable definition or forward/extern function declaration.
912     #
913     #  visibility    'private' or 'public' or 'extern'
914     #  base_type     CBaseTypeNode
915     #  declarators   [CDeclaratorNode]
916     #  in_pxd        boolean
917     #  api           boolean
918
919     #  decorators    [cython.locals(...)] or None 
920     #  directive_locals { string : NameNode } locals defined by cython.locals(...)
921
922     child_attrs = ["base_type", "declarators"]
923     
924     decorators = None
925     directive_locals = {}
926     
927     def analyse_declarations(self, env, dest_scope = None):
928         if not dest_scope:
929             dest_scope = env
930         self.dest_scope = dest_scope
931         base_type = self.base_type.analyse(env)
932
933         # If the field is an external typedef, we cannot be sure about the type,
934         # so do conversion ourself rather than rely on the CPython mechanism (through
935         # a property; made in AnalyseDeclarationsTransform).
936         if (dest_scope.is_c_class_scope
937             and self.visibility in ('public', 'readonly')):
938             need_property = True
939         else:
940             need_property = False
941         visibility = self.visibility
942             
943         for declarator in self.declarators:
944             name_declarator, type = declarator.analyse(base_type, env)
945             if not type.is_complete():
946                 if not (self.visibility == 'extern' and type.is_array):
947                     error(declarator.pos,
948                         "Variable type '%s' is incomplete" % type)
949             if self.visibility == 'extern' and type.is_pyobject:
950                 error(declarator.pos,
951                     "Python object cannot be declared extern")
952             name = name_declarator.name
953             cname = name_declarator.cname
954             if name == '':
955                 error(declarator.pos, "Missing name in declaration.")
956                 return
957             if type.is_cfunction:
958                 entry = dest_scope.declare_cfunction(name, type, declarator.pos,
959                     cname = cname, visibility = self.visibility, in_pxd = self.in_pxd,
960                     api = self.api)
961                 if entry is not None:
962                     entry.directive_locals = self.directive_locals
963             else:
964                 if self.directive_locals:
965                     error(self.pos, "Decorators can only be followed by functions")
966                 if self.in_pxd and self.visibility != 'extern':
967                     error(self.pos, 
968                         "Only 'extern' C variable declaration allowed in .pxd file")
969                 entry = dest_scope.declare_var(name, type, declarator.pos,
970                             cname = cname, visibility = visibility, is_cdef = 1)
971                 entry.needs_property = need_property
972     
973
974 class CStructOrUnionDefNode(StatNode):
975     #  name          string
976     #  cname         string or None
977     #  kind          "struct" or "union"
978     #  typedef_flag  boolean
979     #  visibility    "public" or "private"
980     #  in_pxd        boolean
981     #  attributes    [CVarDefNode] or None
982     #  entry         Entry
983     #  packed        boolean
984     
985     child_attrs = ["attributes"]
986
987     def analyse_declarations(self, env):
988         scope = None
989         if self.visibility == 'extern' and self.packed:
990             error(self.pos, "Cannot declare extern struct as 'packed'")
991         if self.attributes is not None:
992             scope = StructOrUnionScope(self.name)
993         self.entry = env.declare_struct_or_union(
994             self.name, self.kind, scope, self.typedef_flag, self.pos,
995             self.cname, visibility = self.visibility, packed = self.packed)
996         if self.attributes is not None:
997             if self.in_pxd and not env.in_cinclude:
998                 self.entry.defined_in_pxd = 1
999             for attr in self.attributes:
1000                 attr.analyse_declarations(env, scope)
1001             if self.visibility != 'extern':
1002                 need_typedef_indirection = False
1003                 for attr in scope.var_entries:
1004                     type = attr.type
1005                     while type.is_array:
1006                         type = type.base_type
1007                     if type == self.entry.type:
1008                         error(attr.pos, "Struct cannot contain itself as a member.")
1009                     if self.typedef_flag:
1010                         while type.is_ptr:
1011                             type = type.base_type
1012                         if type == self.entry.type:
1013                             need_typedef_indirection = True
1014                 if need_typedef_indirection:
1015                     # C can't handle typedef structs that refer to themselves. 
1016                     struct_entry = self.entry
1017                     self.entry = env.declare_typedef(
1018                         self.name, struct_entry.type, self.pos,
1019                         cname = self.cname, visibility='ignore')
1020                     struct_entry.type.typedef_flag = False
1021                     # FIXME: this might be considered a hack ;-)
1022                     struct_entry.cname = struct_entry.type.cname = \
1023                                          '_' + self.entry.type.typedef_cname
1024     
1025     def analyse_expressions(self, env):
1026         pass
1027     
1028     def generate_execution_code(self, code):
1029         pass
1030
1031
1032 class CppClassNode(CStructOrUnionDefNode):
1033
1034     #  name          string
1035     #  cname         string or None
1036     #  visibility    "extern"
1037     #  in_pxd        boolean
1038     #  attributes    [CVarDefNode] or None
1039     #  entry         Entry
1040     #  base_classes  [string]
1041     #  templates     [string] or None
1042
1043     def analyse_declarations(self, env):
1044         scope = None
1045         if self.attributes is not None:
1046             scope = CppClassScope(self.name, env)
1047         base_class_types = []
1048         for base_class_name in self.base_classes:
1049             base_class_entry = env.lookup(base_class_name)
1050             if base_class_entry is None:
1051                 error(self.pos, "'%s' not found" % base_class_name)
1052             elif not base_class_entry.is_type or not base_class_entry.type.is_cpp_class:
1053                 error(self.pos, "'%s' is not a cpp class type" % base_class_name)
1054             else:
1055                 base_class_types.append(base_class_entry.type)
1056         if self.templates is None:
1057             template_types = None
1058         else:
1059             template_types = [PyrexTypes.TemplatePlaceholderType(template_name) for template_name in self.templates]
1060         self.entry = env.declare_cpp_class(
1061             self.name, scope, self.pos,
1062             self.cname, base_class_types, visibility = self.visibility, templates = template_types)
1063         self.entry.is_cpp_class = 1
1064         if self.attributes is not None:
1065             if self.in_pxd and not env.in_cinclude:
1066                 self.entry.defined_in_pxd = 1
1067             for attr in self.attributes:
1068                 attr.analyse_declarations(scope)
1069
1070 class CEnumDefNode(StatNode):
1071     #  name           string or None
1072     #  cname          string or None
1073     #  items          [CEnumDefItemNode]
1074     #  typedef_flag   boolean
1075     #  visibility     "public" or "private"
1076     #  in_pxd         boolean
1077     #  entry          Entry
1078     
1079     child_attrs = ["items"]
1080     
1081     def analyse_declarations(self, env):
1082         self.entry = env.declare_enum(self.name, self.pos,
1083             cname = self.cname, typedef_flag = self.typedef_flag,
1084             visibility = self.visibility)
1085         if self.items is not None:
1086             if self.in_pxd and not env.in_cinclude:
1087                 self.entry.defined_in_pxd = 1
1088             for item in self.items:
1089                 item.analyse_declarations(env, self.entry)
1090
1091     def analyse_expressions(self, env):
1092         pass
1093
1094     def generate_execution_code(self, code):
1095         if self.visibility == 'public':
1096             temp = code.funcstate.allocate_temp(PyrexTypes.py_object_type, manage_ref=True)
1097             for item in self.entry.enum_values:
1098                 code.putln("%s = PyInt_FromLong(%s); %s" % (
1099                         temp,
1100                         item.cname,
1101                         code.error_goto_if_null(temp, item.pos)))
1102                 code.put_gotref(temp)
1103                 code.putln('if (__Pyx_SetAttrString(%s, "%s", %s) < 0) %s' % (
1104                         Naming.module_cname, 
1105                         item.name, 
1106                         temp,
1107                         code.error_goto(item.pos)))
1108                 code.put_decref_clear(temp, PyrexTypes.py_object_type)
1109             code.funcstate.release_temp(temp)
1110
1111
1112 class CEnumDefItemNode(StatNode):
1113     #  name     string
1114     #  cname    string or None
1115     #  value    ExprNode or None
1116     
1117     child_attrs = ["value"]
1118
1119     def analyse_declarations(self, env, enum_entry):
1120         if self.value:
1121             self.value.analyse_const_expression(env)
1122             if not self.value.type.is_int:
1123                 self.value = self.value.coerce_to(PyrexTypes.c_int_type, env)
1124                 self.value.analyse_const_expression(env)
1125         entry = env.declare_const(self.name, enum_entry.type, 
1126             self.value, self.pos, cname = self.cname,
1127             visibility = enum_entry.visibility)
1128         enum_entry.enum_values.append(entry)
1129
1130
1131 class CTypeDefNode(StatNode):
1132     #  base_type    CBaseTypeNode
1133     #  declarator   CDeclaratorNode
1134     #  visibility   "public" or "private"
1135     #  in_pxd       boolean
1136
1137     child_attrs = ["base_type", "declarator"]
1138     
1139     def analyse_declarations(self, env):
1140         base = self.base_type.analyse(env)
1141         name_declarator, type = self.declarator.analyse(base, env)
1142         name = name_declarator.name
1143         cname = name_declarator.cname
1144         entry = env.declare_typedef(name, type, self.pos,
1145             cname = cname, visibility = self.visibility)
1146         if self.in_pxd and not env.in_cinclude:
1147             entry.defined_in_pxd = 1
1148     
1149     def analyse_expressions(self, env):
1150         pass
1151     def generate_execution_code(self, code):
1152         pass
1153
1154
1155 class FuncDefNode(StatNode, BlockNode):
1156     #  Base class for function definition nodes.
1157     #
1158     #  return_type     PyrexType
1159     #  #filename        string        C name of filename string const
1160     #  entry           Symtab.Entry
1161     #  needs_closure   boolean        Whether or not this function has inner functions/classes/yield
1162     #  needs_outer_scope boolean      Whether or not this function requires outer scope
1163     #  directive_locals { string : NameNode } locals defined by cython.locals(...)
1164     
1165     py_func = None
1166     assmt = None
1167     needs_closure = False
1168     needs_outer_scope = False
1169     is_generator = False
1170     modifiers = []
1171     
1172     def analyse_default_values(self, env):
1173         genv = env.global_scope()
1174         default_seen = 0
1175         for arg in self.args:
1176             if arg.default:
1177                 default_seen = 1
1178                 if arg.is_generic:
1179                     arg.default.analyse_types(env)
1180                     arg.default = arg.default.coerce_to(arg.type, genv)
1181                 else:
1182                     error(arg.pos,
1183                         "This argument cannot have a default value")
1184                     arg.default = None
1185             elif arg.kw_only:
1186                 default_seen = 1
1187             elif default_seen:
1188                 error(arg.pos, "Non-default argument following default argument")
1189
1190     def need_gil_acquisition(self, lenv):
1191         return 0
1192         
1193     def create_local_scope(self, env):
1194         genv = env
1195         while genv.is_py_class_scope or genv.is_c_class_scope:
1196             genv = genv.outer_scope
1197         if self.needs_closure:
1198             lenv = ClosureScope(name=self.entry.name,
1199                                 outer_scope = genv,
1200                                 scope_name=self.entry.cname)
1201         else:
1202             lenv = LocalScope(name=self.entry.name,
1203                               outer_scope=genv,
1204                               parent_scope=env)
1205         lenv.return_type = self.return_type
1206         type = self.entry.type
1207         if type.is_cfunction:
1208             lenv.nogil = type.nogil and not type.with_gil
1209         self.local_scope = lenv
1210         lenv.directives = env.directives
1211         return lenv
1212                 
1213     def generate_function_definitions(self, env, code):
1214         import Buffer
1215
1216         lenv = self.local_scope
1217         if lenv.is_closure_scope and not lenv.is_passthrough:
1218             outer_scope_cname = "%s->%s" % (Naming.cur_scope_cname,
1219                                             Naming.outer_scope_cname)
1220         else:
1221             outer_scope_cname = Naming.outer_scope_cname
1222         lenv.mangle_closure_cnames(outer_scope_cname)
1223         # Generate closure function definitions
1224         self.body.generate_function_definitions(lenv, code)
1225         # generate lambda function definitions
1226         self.generate_lambda_definitions(lenv, code)
1227
1228         is_getbuffer_slot = (self.entry.name == "__getbuffer__" and
1229                              self.entry.scope.is_c_class_scope)
1230         is_releasebuffer_slot = (self.entry.name == "__releasebuffer__" and
1231                                  self.entry.scope.is_c_class_scope)
1232         is_buffer_slot = is_getbuffer_slot or is_releasebuffer_slot
1233         if is_buffer_slot:
1234             if 'cython_unused' not in self.modifiers:
1235                 self.modifiers = self.modifiers + ['cython_unused']
1236
1237         preprocessor_guard = None
1238         if self.entry.is_special and not is_buffer_slot:
1239             slot = TypeSlots.method_name_to_slot.get(self.entry.name)
1240             if slot:
1241                 preprocessor_guard = slot.preprocessor_guard_code()
1242                 if (self.entry.name == '__long__' and
1243                     not self.entry.scope.lookup_here('__int__')):
1244                     preprocessor_guard = None
1245         
1246         profile = code.globalstate.directives['profile']
1247         if profile:
1248             if lenv.nogil:
1249                 error(self.pos, "Cannot profile nogil function.")
1250             code.globalstate.use_utility_code(profile_utility_code)
1251
1252         # Generate C code for header and body of function
1253         code.enter_cfunc_scope()
1254         code.return_from_error_cleanup_label = code.new_label()
1255
1256         # ----- Top-level constants used by this function
1257         code.mark_pos(self.pos)
1258         self.generate_cached_builtins_decls(lenv, code)
1259         # ----- Function header
1260         code.putln("")
1261
1262         if preprocessor_guard:
1263             code.putln(preprocessor_guard)
1264
1265         with_pymethdef = self.needs_assignment_synthesis(env, code)
1266         if self.py_func:
1267             self.py_func.generate_function_header(code, 
1268                 with_pymethdef = with_pymethdef,
1269                 proto_only=True)
1270         self.generate_function_header(code,
1271             with_pymethdef = with_pymethdef)
1272         # ----- Local variable declarations
1273         # Find function scope
1274         cenv = env
1275         while cenv.is_py_class_scope or cenv.is_c_class_scope:
1276             cenv = cenv.outer_scope
1277         if self.needs_closure:
1278             code.put(lenv.scope_class.type.declaration_code(Naming.cur_scope_cname))
1279             code.putln(";")
1280         elif self.needs_outer_scope:
1281             if lenv.is_passthrough:
1282                 code.put(lenv.scope_class.type.declaration_code(Naming.cur_scope_cname))
1283                 code.putln(";")
1284             code.put(cenv.scope_class.type.declaration_code(Naming.outer_scope_cname))
1285             code.putln(";")
1286         self.generate_argument_declarations(lenv, code)
1287         for entry in lenv.var_entries:
1288             if not entry.in_closure:
1289                 code.put_var_declaration(entry)
1290         init = ""
1291         if not self.return_type.is_void:
1292             if self.return_type.is_pyobject:
1293                 init = " = NULL"
1294             code.putln(
1295                 "%s%s;" % 
1296                     (self.return_type.declaration_code(Naming.retval_cname),
1297                      init))
1298         tempvardecl_code = code.insertion_point()
1299         code.put_declare_refcount_context()
1300         if not self.is_generator:
1301             self.generate_keyword_list(code)
1302         if profile:
1303             code.put_trace_declarations()
1304         # ----- Extern library function declarations
1305         lenv.generate_library_function_declarations(code)
1306         # ----- GIL acquisition
1307         acquire_gil = self.acquire_gil
1308         if acquire_gil:
1309             env.use_utility_code(force_init_threads_utility_code)
1310             code.putln("#ifdef WITH_THREAD")
1311             code.putln("PyGILState_STATE _save = PyGILState_Ensure();")
1312             code.putln("#endif")
1313         # ----- set up refnanny
1314         if not lenv.nogil:
1315             code.put_setup_refcount_context(self.entry.name)
1316         # ----- Automatic lead-ins for certain special functions
1317         if is_getbuffer_slot:
1318             self.getbuffer_init(code)
1319         # ----- Create closure scope object
1320         if self.is_generator:
1321             code.putln("%s = (%s) %s;" % (
1322                 Naming.cur_scope_cname,
1323                 lenv.scope_class.type.declaration_code(''),
1324                 Naming.self_cname))
1325             gotref_code = code.insertion_point()
1326
1327         elif self.needs_closure:
1328             code.putln("%s = (%s)%s->tp_new(%s, %s, NULL);" % (
1329                 Naming.cur_scope_cname,
1330                 lenv.scope_class.type.declaration_code(''),
1331                 lenv.scope_class.type.typeptr_cname, 
1332                 lenv.scope_class.type.typeptr_cname,
1333                 Naming.empty_tuple))
1334             code.putln("if (unlikely(!%s)) {" % Naming.cur_scope_cname)
1335             if is_getbuffer_slot:
1336                 self.getbuffer_error_cleanup(code)
1337             if not lenv.nogil:
1338                 code.put_finish_refcount_context()
1339             # FIXME: what if the error return value is a Python value?
1340             code.putln("return %s;" % self.error_value())
1341             code.putln("}")
1342             code.put_gotref(Naming.cur_scope_cname)
1343             # Note that it is unsafe to decref the scope at this point.
1344         if self.needs_outer_scope and not self.is_generator:
1345             code.putln("%s = (%s)%s;" % (
1346                             outer_scope_cname,
1347                             cenv.scope_class.type.declaration_code(''),
1348                             Naming.self_cname))
1349             if lenv.is_passthrough:
1350                 code.putln("%s = %s;" % (Naming.cur_scope_cname, outer_scope_cname));
1351             elif self.needs_closure:
1352                 # inner closures own a reference to their outer parent
1353                 code.put_incref(outer_scope_cname, cenv.scope_class.type)
1354                 code.put_giveref(outer_scope_cname)
1355         # ----- Trace function call
1356         if profile:
1357             # this looks a bit late, but if we don't get here due to a
1358             # fatal error before hand, it's not really worth tracing
1359             code.put_trace_call(self.entry.name, self.pos)
1360         # ----- Fetch arguments
1361         if not self.is_generator:
1362             self.generate_preamble(env, code)
1363         if self.is_generator:
1364             code.funcstate.init_closure_temps(lenv.scope_class.type.scope)
1365             resume_code = code.insertion_point()
1366             first_run_label = code.new_label('first_run')
1367             code.use_label(first_run_label)
1368             code.put_label(first_run_label)
1369             code.putln('%s' %
1370                        (code.error_goto_if_null(Naming.sent_value_cname, self.pos)))
1371         # -------------------------
1372         # ----- Function body -----
1373         # -------------------------
1374         self.body.generate_execution_code(code)
1375
1376         if self.is_generator:
1377             code.putln('PyErr_SetNone(PyExc_StopIteration); %s' % code.error_goto(self.pos))
1378
1379         # ----- Default return value
1380         code.putln("")
1381         if self.return_type.is_pyobject:
1382             #if self.return_type.is_extension_type:
1383             #    lhs = "(PyObject *)%s" % Naming.retval_cname
1384             #else:
1385             lhs = Naming.retval_cname
1386             code.put_init_to_py_none(lhs, self.return_type)
1387         else:
1388             val = self.return_type.default_value
1389             if val:
1390                 code.putln("%s = %s;" % (Naming.retval_cname, val))
1391         # ----- Error cleanup
1392         if code.error_label in code.labels_used:
1393             code.put_goto(code.return_label)
1394             code.put_label(code.error_label)
1395             for cname, type in code.funcstate.all_managed_temps():
1396                 code.put_xdecref(cname, type)
1397
1398             # Clean up buffers -- this calls a Python function
1399             # so need to save and restore error state
1400             buffers_present = len(lenv.buffer_entries) > 0
1401             if buffers_present:
1402                 code.globalstate.use_utility_code(restore_exception_utility_code)
1403                 code.putln("{ PyObject *__pyx_type, *__pyx_value, *__pyx_tb;")
1404                 code.putln("__Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb);")
1405                 for entry in lenv.buffer_entries:                    
1406                     Buffer.put_release_buffer_code(code, entry)
1407                     #code.putln("%s = 0;" % entry.cname)
1408                 code.putln("__Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}")
1409
1410             err_val = self.error_value()
1411             exc_check = self.caller_will_check_exceptions()
1412             if err_val is not None or exc_check:
1413                 # TODO: Fix exception tracing (though currently unused by cProfile). 
1414                 # code.globalstate.use_utility_code(get_exception_tuple_utility_code)
1415                 # code.put_trace_exception()
1416                 code.putln('__Pyx_AddTraceback("%s");' % self.entry.qualified_name)
1417             else:
1418                 warning(self.entry.pos, "Unraisable exception in function '%s'." \
1419                             % self.entry.qualified_name, 0)
1420                 code.putln(
1421                     '__Pyx_WriteUnraisable("%s");' % 
1422                         self.entry.qualified_name)
1423                 env.use_utility_code(unraisable_exception_utility_code)
1424                 env.use_utility_code(restore_exception_utility_code)
1425             default_retval = self.return_type.default_value
1426             if err_val is None and default_retval:
1427                 err_val = default_retval
1428             if err_val is not None:
1429                 code.putln("%s = %s;" % (Naming.retval_cname, err_val))
1430
1431             if is_getbuffer_slot:
1432                 self.getbuffer_error_cleanup(code)
1433
1434             # If we are using the non-error cleanup section we should
1435             # jump past it if we have an error. The if-test below determine
1436             # whether this section is used.
1437             if buffers_present or is_getbuffer_slot:
1438                 code.put_goto(code.return_from_error_cleanup_label)
1439
1440
1441         # ----- Non-error return cleanup
1442         code.put_label(code.return_label)
1443         for entry in lenv.buffer_entries:
1444             if entry.used:
1445                 Buffer.put_release_buffer_code(code, entry)
1446         if is_getbuffer_slot:
1447             self.getbuffer_normal_cleanup(code)
1448         # ----- Return cleanup for both error and no-error return
1449         code.put_label(code.return_from_error_cleanup_label)
1450         if not Options.init_local_none:
1451             for entry in lenv.var_entries:
1452                 if lenv.control_flow.get_state((entry.name, 'initialized')) is not True:
1453                     entry.xdecref_cleanup = 1
1454         
1455         for entry in lenv.var_entries:
1456             if entry.type.is_pyobject:
1457                 if entry.used and not entry.in_closure:
1458                     code.put_var_decref(entry)
1459         # Decref any increfed args
1460         for entry in lenv.arg_entries:
1461             if entry.type.is_pyobject:
1462                 if entry.assignments and not entry.in_closure:
1463                     code.put_var_decref(entry)
1464         if self.needs_closure and not self.is_generator:
1465             code.put_decref(Naming.cur_scope_cname, lenv.scope_class.type)
1466         if self.is_generator:
1467             code.putln('%s->%s.resume_label = -1;' % (Naming.cur_scope_cname, Naming.obj_base_cname))
1468
1469         # ----- Return
1470         # This code is duplicated in ModuleNode.generate_module_init_func
1471         if not lenv.nogil:
1472             default_retval = self.return_type.default_value
1473             err_val = self.error_value()
1474             if err_val is None and default_retval:
1475                 err_val = default_retval
1476             if self.return_type.is_pyobject:
1477                 code.put_xgiveref(self.return_type.as_pyobject(Naming.retval_cname))
1478
1479         if self.entry.is_special and self.entry.name == "__hash__":
1480             # Returning -1 for __hash__ is supposed to signal an error
1481             # We do as Python instances and coerce -1 into -2. 
1482             code.putln("if (unlikely(%s == -1) && !PyErr_Occurred()) %s = -2;" % (
1483                     Naming.retval_cname, Naming.retval_cname))
1484
1485         if profile:
1486             if self.return_type.is_pyobject:
1487                 code.put_trace_return(Naming.retval_cname)
1488             else:
1489                 code.put_trace_return("Py_None")
1490         if not lenv.nogil:
1491             code.put_finish_refcount_context()
1492         
1493         if acquire_gil:
1494             code.putln("#ifdef WITH_THREAD")
1495             code.putln("PyGILState_Release(_save);")
1496             code.putln("#endif")
1497
1498         if not self.return_type.is_void:
1499             code.putln("return %s;" % Naming.retval_cname)
1500             
1501         code.putln("}")
1502
1503         if preprocessor_guard:
1504             code.putln("#endif /*!(%s)*/" % preprocessor_guard)
1505         # ----- Go back and insert temp variable declarations
1506         tempvardecl_code.put_temp_declarations(code.funcstate)
1507         # ----- Generator resume code
1508         if self.is_generator:
1509             resume_code.putln("switch (%s->%s.resume_label) {" % (Naming.cur_scope_cname, Naming.obj_base_cname));
1510             resume_code.putln("case 0: goto %s;" % first_run_label)
1511             for yield_expr in self.yields:
1512                 resume_code.putln("case %d: goto %s;" % (yield_expr.label_num, yield_expr.label_name));
1513             resume_code.putln("default: /* CPython raises the right error here */");
1514             resume_code.putln("return NULL;");
1515             resume_code.putln("}");
1516         # ----- Python version
1517         code.exit_cfunc_scope()
1518         if self.py_func:
1519             self.py_func.generate_function_definitions(env, code)
1520         self.generate_wrapper_functions(code)
1521
1522         if self.is_generator:
1523             self.generator.generate_function_body(self.local_scope, code)
1524
1525     def generate_preamble(self, env, code):
1526         """Parse arguments and prepare scope"""
1527         import Buffer
1528
1529         lenv = self.local_scope
1530
1531         self.generate_argument_parsing_code(env, code)
1532         # If an argument is assigned to in the body, we must 
1533         # incref it to properly keep track of refcounts.
1534         for entry in lenv.arg_entries:
1535             if entry.type.is_pyobject:
1536                 if entry.assignments and not entry.in_closure:
1537                     code.put_var_incref(entry)
1538         # ----- Initialise local variables 
1539         for entry in lenv.var_entries:
1540             if entry.type.is_pyobject and entry.init_to_none and entry.used:
1541                 code.put_init_var_to_py_none(entry)
1542         # ----- Initialise local buffer auxiliary variables
1543         for entry in lenv.var_entries + lenv.arg_entries:
1544             if entry.type.is_buffer and entry.buffer_aux.buffer_info_var.used:
1545                 code.putln("%s.buf = NULL;" %
1546                            entry.buffer_aux.buffer_info_var.cname)
1547         # ----- Check and convert arguments
1548         self.generate_argument_type_tests(code)
1549         # ----- Acquire buffer arguments
1550         for entry in lenv.arg_entries:
1551             if entry.type.is_buffer:
1552                 Buffer.put_acquire_arg_buffer(entry, code, self.pos)
1553
1554     def declare_argument(self, env, arg):
1555         if arg.type.is_void:
1556             error(arg.pos, "Invalid use of 'void'")
1557         elif not arg.type.is_complete() and not arg.type.is_array:
1558             error(arg.pos,
1559                 "Argument type '%s' is incomplete" % arg.type)
1560         return env.declare_arg(arg.name, arg.type, arg.pos)
1561     
1562     def generate_arg_type_test(self, arg, code):
1563         # Generate type test for one argument.
1564         if arg.type.typeobj_is_available():
1565             code.globalstate.use_utility_code(arg_type_test_utility_code)
1566             typeptr_cname = arg.type.typeptr_cname
1567             arg_code = "((PyObject *)%s)" % arg.entry.cname
1568             code.putln(
1569                 'if (unlikely(!__Pyx_ArgTypeTest(%s, %s, %d, "%s", %s))) %s' % (
1570                     arg_code, 
1571                     typeptr_cname,
1572                     arg.accept_none,
1573                     arg.name,
1574                     arg.type.is_builtin_type,
1575                     code.error_goto(arg.pos)))
1576         else:
1577             error(arg.pos, "Cannot test type of extern C class "
1578                 "without type object name specification")
1579
1580     def generate_arg_none_check(self, arg, code):
1581         # Generate None check for one argument.
1582         code.globalstate.use_utility_code(arg_type_test_utility_code)
1583         code.putln('if (unlikely(((PyObject *)%s) == Py_None)) {' % arg.entry.cname)
1584         code.putln('''PyErr_Format(PyExc_TypeError, "Argument '%s' must not be None"); %s''' % (
1585             arg.name,
1586             code.error_goto(arg.pos)))
1587         code.putln('}')
1588         
1589     def generate_wrapper_functions(self, code):
1590         pass
1591
1592     def generate_execution_code(self, code):
1593         # Evaluate and store argument default values
1594         for arg in self.args:
1595             default = arg.default
1596             if default:
1597                 if not default.is_literal:
1598                     default.generate_evaluation_code(code)
1599                     default.make_owned_reference(code)
1600                     result = default.result_as(arg.type)
1601                     code.putln(
1602                         "%s = %s;" % (
1603                             arg.calculate_default_value_code(code),
1604                             result))
1605                     if arg.type.is_pyobject:
1606                         code.put_giveref(default.result())
1607                     default.generate_post_assignment_code(code)
1608                     default.free_temps(code)
1609         # For Python class methods, create and store function object
1610         if self.assmt:
1611             self.assmt.generate_execution_code(code)
1612
1613     #
1614     # Special code for the __getbuffer__ function
1615     #
1616     def getbuffer_init(self, code):
1617         info = self.local_scope.arg_entries[1].cname
1618         # Python 3.0 betas have a bug in memoryview which makes it call
1619         # getbuffer with a NULL parameter. For now we work around this;
1620         # the following line should be removed when this bug is fixed.
1621         code.putln("if (%s == NULL) return 0;" % info) 
1622         code.putln("%s->obj = Py_None; __Pyx_INCREF(Py_None);" % info)
1623         code.put_giveref("%s->obj" % info) # Do not refnanny object within structs
1624
1625     def getbuffer_error_cleanup(self, code):
1626         info = self.local_scope.arg_entries[1].cname
1627         code.put_gotref("%s->obj" % info)
1628         code.putln("__Pyx_DECREF(%s->obj); %s->obj = NULL;" %
1629                    (info, info))
1630
1631     def getbuffer_normal_cleanup(self, code):
1632         info = self.local_scope.arg_entries[1].cname
1633         code.putln("if (%s->obj == Py_None) {" % info)
1634         code.put_gotref("Py_None")
1635         code.putln("__Pyx_DECREF(Py_None); %s->obj = NULL;" % info)
1636         code.putln("}")
1637
1638 class CFuncDefNode(FuncDefNode):
1639     #  C function definition.
1640     #
1641     #  modifiers     ['inline']
1642     #  visibility    'private' or 'public' or 'extern'
1643     #  base_type     CBaseTypeNode
1644     #  declarator    CDeclaratorNode
1645     #  body          StatListNode
1646     #  api           boolean
1647     #  decorators    [DecoratorNode]        list of decorators
1648     #
1649     #  with_gil      boolean    Acquire GIL around body
1650     #  type          CFuncType
1651     #  py_func       wrapper for calling from Python
1652     #  overridable   whether or not this is a cpdef function
1653     #  inline_in_pxd whether this is an inline function in a pxd file
1654     
1655     child_attrs = ["base_type", "declarator", "body", "py_func"]
1656
1657     inline_in_pxd = False
1658     decorators = None
1659     directive_locals = {}
1660
1661     def unqualified_name(self):
1662         return self.entry.name
1663         
1664     def analyse_declarations(self, env):
1665         self.directive_locals.update(env.directives['locals'])
1666         base_type = self.base_type.analyse(env)
1667         # The 2 here is because we need both function and argument names.
1668         name_declarator, type = self.declarator.analyse(base_type, env, nonempty = 2 * (self.body is not None))
1669         if not type.is_cfunction:
1670             error(self.pos, 
1671                 "Suite attached to non-function declaration")
1672         # Remember the actual type according to the function header
1673         # written here, because the type in the symbol table entry
1674         # may be different if we're overriding a C method inherited
1675         # from the base type of an extension type.
1676         self.type = type
1677         type.is_overridable = self.overridable
1678         declarator = self.declarator
1679         while not hasattr(declarator, 'args'):
1680             declarator = declarator.base
1681         self.args = declarator.args
1682         for formal_arg, type_arg in zip(self.args, type.args):
1683             formal_arg.type = type_arg.type
1684             formal_arg.name = type_arg.name
1685             formal_arg.cname = type_arg.cname
1686         name = name_declarator.name
1687         cname = name_declarator.cname
1688         self.entry = env.declare_cfunction(
1689             name, type, self.pos, 
1690             cname = cname, visibility = self.visibility,
1691             defining = self.body is not None,
1692             api = self.api, modifiers = self.modifiers)
1693         self.entry.inline_func_in_pxd = self.inline_in_pxd
1694         self.return_type = type.return_type
1695         
1696         if self.overridable and not env.is_module_scope:
1697             if len(self.args) < 1 or not self.args[0].type.is_pyobject:
1698                 # An error will be produced in the cdef function
1699                 self.overridable = False
1700             
1701         if self.overridable:
1702             import ExprNodes
1703             py_func_body = self.call_self_node(is_module_scope = env.is_module_scope)
1704             self.py_func = DefNode(pos = self.pos, 
1705                                    name = self.entry.name,
1706                                    args = self.args,
1707                                    star_arg = None,
1708                                    starstar_arg = None,
1709                                    doc = self.doc,
1710                                    body = py_func_body,
1711                                    is_wrapper = 1)
1712             self.py_func.is_module_scope = env.is_module_scope
1713             self.py_func.analyse_declarations(env)
1714             self.entry.as_variable = self.py_func.entry
1715             # Reset scope entry the above cfunction
1716             env.entries[name] = self.entry
1717             if not env.is_module_scope or Options.lookup_module_cpdef:
1718                 self.override = OverrideCheckNode(self.pos, py_func = self.py_func)
1719                 self.body = StatListNode(self.pos, stats=[self.override, self.body])
1720         self.create_local_scope(env)
1721     
1722     def call_self_node(self, omit_optional_args=0, is_module_scope=0):
1723         import ExprNodes
1724         args = self.type.args
1725         if omit_optional_args:
1726             args = args[:len(args) - self.type.optional_arg_count]
1727         arg_names = [arg.name for arg in args]
1728         if is_module_scope:
1729             cfunc = ExprNodes.NameNode(self.pos, name=self.entry.name)
1730         else:
1731             self_arg = ExprNodes.NameNode(self.pos, name=arg_names[0])
1732             cfunc = ExprNodes.AttributeNode(self.pos, obj=self_arg, attribute=self.entry.name)
1733         skip_dispatch = not is_module_scope or Options.lookup_module_cpdef
1734         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)
1735         return ReturnStatNode(pos=self.pos, return_type=PyrexTypes.py_object_type, value=c_call)
1736     
1737     def declare_arguments(self, env):
1738         for arg in self.type.args:
1739             if not arg.name:
1740                 error(arg.pos, "Missing argument name")
1741             self.declare_argument(env, arg)
1742
1743     def need_gil_acquisition(self, lenv):
1744         return self.type.with_gil
1745
1746     def nogil_check(self, env):
1747         type = self.type
1748         with_gil = type.with_gil
1749         if type.nogil and not with_gil:
1750             if type.return_type.is_pyobject:
1751                 error(self.pos,
1752                       "Function with Python return type cannot be declared nogil")
1753             for entry in self.local_scope.var_entries:
1754                 if entry.type.is_pyobject:
1755                     error(self.pos, "Function declared nogil has Python locals or temporaries")
1756
1757     def analyse_expressions(self, env):
1758         self.local_scope.directives = env.directives
1759         if self.py_func is not None:
1760             # this will also analyse the default values
1761             self.py_func.analyse_expressions(env)
1762         else:
1763             self.analyse_default_values(env)
1764         self.acquire_gil = self.need_gil_acquisition(self.local_scope)
1765
1766     def needs_assignment_synthesis(self, env, code=None):
1767         return False
1768
1769     def generate_function_header(self, code, with_pymethdef, with_opt_args = 1, with_dispatch = 1, cname = None):
1770         arg_decls = []
1771         type = self.type
1772         visibility = self.entry.visibility
1773         for arg in type.args[:len(type.args)-type.optional_arg_count]:
1774             arg_decls.append(arg.declaration_code())
1775         if with_dispatch and self.overridable:
1776             arg_decls.append(PyrexTypes.c_int_type.declaration_code(Naming.skip_dispatch_cname))
1777         if type.optional_arg_count and with_opt_args:
1778             arg_decls.append(type.op_arg_struct.declaration_code(Naming.optional_args_cname))
1779         if type.has_varargs:
1780             arg_decls.append("...")
1781         if not arg_decls:
1782             arg_decls = ["void"]
1783         if cname is None:
1784             cname = self.entry.func_cname
1785         entity = type.function_header_code(cname, ', '.join(arg_decls))
1786         if visibility == 'public':
1787             dll_linkage = "DL_EXPORT"
1788         else:
1789             dll_linkage = None
1790         header = self.return_type.declaration_code(entity,
1791             dll_linkage = dll_linkage)
1792         if visibility == 'extern':
1793             storage_class = "%s " % Naming.extern_c_macro
1794         elif visibility == 'public':
1795             storage_class = ""
1796         else:
1797             storage_class = "static "
1798         if 'inline' in self.modifiers:
1799             self.modifiers[self.modifiers.index('inline')] = 'cython_inline'
1800         code.putln("%s%s %s {" % (
1801             storage_class,
1802             ' '.join(self.modifiers).upper(), # macro forms 
1803             header))
1804
1805     def generate_argument_declarations(self, env, code):
1806         for arg in self.args:
1807             if arg.default:
1808                 result = arg.calculate_default_value_code(code)
1809                 code.putln('%s = %s;' % (
1810                     arg.type.declaration_code(arg.cname), result))
1811
1812     def generate_keyword_list(self, code):
1813         pass
1814         
1815     def generate_argument_parsing_code(self, env, code):
1816         i = 0
1817         if self.type.optional_arg_count:
1818             code.putln('if (%s) {' % Naming.optional_args_cname)
1819             for arg in self.args:
1820                 if arg.default:
1821                     code.putln('if (%s->%sn > %s) {' % (Naming.optional_args_cname, Naming.pyrex_prefix, i))
1822                     declarator = arg.declarator
1823                     while not hasattr(declarator, 'name'):
1824                         declarator = declarator.base
1825                     code.putln('%s = %s->%s;' % (arg.cname, Naming.optional_args_cname, self.type.opt_arg_cname(declarator.name)))
1826                     i += 1
1827             for _ in range(self.type.optional_arg_count):
1828                 code.putln('}')
1829             code.putln('}')
1830
1831     def generate_argument_conversion_code(self, code):
1832         pass
1833     
1834     def generate_argument_type_tests(self, code):
1835         # Generate type tests for args whose type in a parent
1836         # class is a supertype of the declared type.
1837         for arg in self.type.args:
1838             if arg.needs_type_test:
1839                 self.generate_arg_type_test(arg, code)
1840             elif arg.type.is_pyobject and not arg.accept_none:
1841                 self.generate_arg_none_check(arg, code)
1842
1843     def error_value(self):
1844         if self.return_type.is_pyobject:
1845             return "0"
1846         else:
1847             #return None
1848             return self.entry.type.exception_value
1849             
1850     def caller_will_check_exceptions(self):
1851         return self.entry.type.exception_check
1852         
1853     def generate_wrapper_functions(self, code):
1854         # If the C signature of a function has changed, we need to generate
1855         # wrappers to put in the slots here. 
1856         k = 0
1857         entry = self.entry
1858         func_type = entry.type
1859         while entry.prev_entry is not None:
1860             k += 1
1861             entry = entry.prev_entry
1862             entry.func_cname = "%s%swrap_%s" % (self.entry.func_cname, Naming.pyrex_prefix, k)
1863             code.putln()
1864             self.generate_function_header(code, 
1865                                           0,
1866                                           with_dispatch = entry.type.is_overridable, 
1867                                           with_opt_args = entry.type.optional_arg_count, 
1868                                           cname = entry.func_cname)
1869             if not self.return_type.is_void:
1870                 code.put('return ')
1871             args = self.type.args
1872             arglist = [arg.cname for arg in args[:len(args)-self.type.optional_arg_count]]
1873             if entry.type.is_overridable:
1874                 arglist.append(Naming.skip_dispatch_cname)
1875             elif func_type.is_overridable:
1876                 arglist.append('0')
1877             if entry.type.optional_arg_count:
1878                 arglist.append(Naming.optional_args_cname)
1879             elif func_type.optional_arg_count:
1880                 arglist.append('NULL')
1881             code.putln('%s(%s);' % (self.entry.func_cname, ', '.join(arglist)))
1882             code.putln('}')
1883         
1884
1885 class PyArgDeclNode(Node):
1886     # Argument which must be a Python object (used
1887     # for * and ** arguments).
1888     #
1889     # name        string
1890     # entry       Symtab.Entry
1891     # annotation  ExprNode or None   Py3 argument annotation
1892     child_attrs = []
1893
1894     def generate_function_definitions(self, env, code):
1895         self.entry.generate_function_definitions(env, code)
1896
1897 class DecoratorNode(Node):
1898     # A decorator
1899     #
1900     # decorator    NameNode or CallNode or AttributeNode
1901     child_attrs = ['decorator']
1902
1903
1904 class GeneratorWrapperNode(object):
1905     # Wrapper
1906     def __init__(self, def_node, func_cname=None, body_cname=None, header=None):
1907         self.def_node = def_node
1908         self.func_cname = func_cname
1909         self.body_cname = body_cname
1910         self.header = header
1911
1912     def generate_function_body(self, env, code):
1913         code.mark_pos(self.def_node.pos)
1914         cenv = env.outer_scope # XXX: correct?
1915         while cenv.is_py_class_scope or cenv.is_c_class_scope:
1916             cenv = cenv.outer_scope
1917         lenv = self.def_node.local_scope
1918         code.enter_cfunc_scope()
1919         code.putln()
1920         code.putln('%s {' % self.header)
1921         code.put_declare_refcount_context()
1922         self.def_node.generate_keyword_list(code)
1923         code.put(lenv.scope_class.type.declaration_code(Naming.cur_scope_cname))
1924         code.putln(";")
1925         code.put_setup_refcount_context(self.def_node.entry.name)
1926         code.putln("%s = (%s)%s->tp_new(%s, %s, NULL);" % (
1927             Naming.cur_scope_cname,
1928             lenv.scope_class.type.declaration_code(''),
1929             lenv.scope_class.type.typeptr_cname,
1930             lenv.scope_class.type.typeptr_cname,
1931             Naming.empty_tuple))
1932         code.putln("if (unlikely(!%s)) {" % Naming.cur_scope_cname)
1933         code.put_finish_refcount_context()
1934         code.putln("return NULL;");
1935         code.putln("}");
1936         code.put_gotref(Naming.cur_scope_cname)
1937
1938         if self.def_node.needs_outer_scope:
1939             outer_scope_cname = '%s->%s' % (Naming.cur_scope_cname, Naming.outer_scope_cname)
1940             code.putln("%s = (%s)%s;" % (
1941                             outer_scope_cname,
1942                             cenv.scope_class.type.declaration_code(''),
1943                             Naming.self_cname))
1944             code.put_incref(outer_scope_cname, cenv.scope_class.type)
1945             code.put_giveref(outer_scope_cname)
1946
1947         self.def_node.generate_preamble(env, code)
1948
1949         generator_cname = '%s->%s' % (Naming.cur_scope_cname, Naming.obj_base_cname)
1950
1951         code.putln('%s.resume_label = 0;' % generator_cname)
1952         code.putln('%s.body = %s;' % (generator_cname, self.body_cname))
1953         code.put_giveref(Naming.cur_scope_cname)
1954         code.put_finish_refcount_context()
1955         code.putln("return (PyObject *) %s;" % Naming.cur_scope_cname);
1956         code.putln('}\n')
1957         code.exit_cfunc_scope()
1958
1959 class DefNode(FuncDefNode):
1960     # A Python function definition.
1961     #
1962     # name          string                 the Python name of the function
1963     # lambda_name   string                 the internal name of a lambda 'function'
1964     # decorators    [DecoratorNode]        list of decorators
1965     # args          [CArgDeclNode]         formal arguments
1966     # star_arg      PyArgDeclNode or None  * argument
1967     # starstar_arg  PyArgDeclNode or None  ** argument
1968     # doc           EncodedString or None
1969     # body          StatListNode
1970     # return_type_annotation
1971     #               ExprNode or None       the Py3 return type annotation
1972     #
1973     #  The following subnode is constructed internally
1974     #  when the def statement is inside a Python class definition.
1975     #
1976     #  assmt   AssignmentNode   Function construction/assignment
1977     
1978     child_attrs = ["args", "star_arg", "starstar_arg", "body", "decorators"]
1979
1980     lambda_name = None
1981     assmt = None
1982     num_kwonly_args = 0
1983     num_required_kw_args = 0
1984     reqd_kw_flags_cname = "0"
1985     is_wrapper = 0
1986     decorators = None
1987     return_type_annotation = None
1988     entry = None
1989     acquire_gil = 0
1990     self_in_stararg = 0
1991
1992     def __init__(self, pos, **kwds):
1993         FuncDefNode.__init__(self, pos, **kwds)
1994         k = rk = r = 0
1995         for arg in self.args:
1996             if arg.kw_only:
1997                 k += 1
1998                 if not arg.default:
1999                     rk += 1
2000             if not arg.default:
2001                 r += 1
2002         self.num_kwonly_args = k
2003         self.num_required_kw_args = rk
2004         self.num_required_args = r
2005         
2006     def as_cfunction(self, cfunc=None, scope=None):
2007         if self.star_arg:
2008             error(self.star_arg.pos, "cdef function cannot have star argument")
2009         if self.starstar_arg:
2010             error(self.starstar_arg.pos, "cdef function cannot have starstar argument")
2011         if cfunc is None:
2012             cfunc_args = []
2013             for formal_arg in self.args:
2014                 name_declarator, type = formal_arg.analyse(scope, nonempty=1)
2015                 cfunc_args.append(PyrexTypes.CFuncTypeArg(name = name_declarator.name,
2016                                                           cname = None,
2017                                                           type = py_object_type,
2018                                                           pos = formal_arg.pos))
2019             cfunc_type = PyrexTypes.CFuncType(return_type = py_object_type,
2020                                               args = cfunc_args,
2021                                               has_varargs = False,
2022                                               exception_value = None,
2023                                               exception_check = False,
2024                                               nogil = False,
2025                                               with_gil = False,
2026                                               is_overridable = True)
2027             cfunc = CVarDefNode(self.pos, type=cfunc_type)
2028         else:
2029             if scope is None:
2030                 scope = cfunc.scope
2031             cfunc_type = cfunc.type
2032             if len(self.args) != len(cfunc_type.args) or cfunc_type.has_varargs:
2033                 error(self.pos, "wrong number of arguments")
2034                 error(cfunc.pos, "previous declaration here")
2035             for i, (formal_arg, type_arg) in enumerate(zip(self.args, cfunc_type.args)):
2036                 name_declarator, type = formal_arg.analyse(scope, nonempty=1,
2037                                                            is_self_arg = (i == 0 and scope.is_c_class_scope))
2038                 if type is None or type is PyrexTypes.py_object_type:
2039                     formal_arg.type = type_arg.type
2040                     formal_arg.name_declarator = name_declarator
2041         import ExprNodes
2042         if cfunc_type.exception_value is None:
2043             exception_value = None
2044         else:
2045             exception_value = ExprNodes.ConstNode(self.pos, value=cfunc_type.exception_value, type=cfunc_type.return_type)
2046         declarator = CFuncDeclaratorNode(self.pos, 
2047                                          base = CNameDeclaratorNode(self.pos, name=self.name, cname=None),
2048                                          args = self.args,
2049                                          has_varargs = False,
2050                                          exception_check = cfunc_type.exception_check,
2051                                          exception_value = exception_value,
2052                                          with_gil = cfunc_type.with_gil,
2053                                          nogil = cfunc_type.nogil)
2054         return CFuncDefNode(self.pos, 
2055                             modifiers = [],
2056                             base_type = CAnalysedBaseTypeNode(self.pos, type=cfunc_type.return_type),
2057                             declarator = declarator,
2058                             body = self.body,
2059                             doc = self.doc,
2060                             overridable = cfunc_type.is_overridable,
2061                             type = cfunc_type,
2062                             with_gil = cfunc_type.with_gil,
2063                             nogil = cfunc_type.nogil,
2064                             visibility = 'private',
2065                             api = False,
2066                             directive_locals = getattr(cfunc, 'directive_locals', {}))
2067     
2068     def analyse_declarations(self, env):
2069         self.is_classmethod = self.is_staticmethod = False
2070         if self.decorators:
2071             for decorator in self.decorators:
2072                 func = decorator.decorator
2073                 if func.is_name:
2074                     self.is_classmethod |= func.name == 'classmethod'
2075                     self.is_staticmethod |= func.name == 'staticmethod'
2076
2077         if self.is_classmethod and env.lookup_here('classmethod'):
2078             # classmethod() was overridden - not much we can do here ...
2079             self.is_classmethod = False
2080         if self.is_staticmethod and env.lookup_here('staticmethod'):
2081             # staticmethod() was overridden - not much we can do here ...
2082             self.is_staticmethod = False
2083
2084         if self.name == '__new__':
2085             self.is_staticmethod = 1
2086
2087         self.analyse_argument_types(env)
2088         if self.name == '<lambda>':
2089             self.declare_lambda_function(env)
2090         else:
2091             self.declare_pyfunction(env)
2092         self.analyse_signature(env)
2093         self.return_type = self.entry.signature.return_type()
2094         self.create_local_scope(env)
2095
2096     def analyse_argument_types(self, env):
2097         directive_locals = self.directive_locals = env.directives['locals']
2098         allow_none_for_extension_args = env.directives['allow_none_for_extension_args']
2099         for arg in self.args:
2100             if hasattr(arg, 'name'):
2101                 type = arg.type
2102                 name_declarator = None
2103             else:
2104                 base_type = arg.base_type.analyse(env)
2105                 name_declarator, type = \
2106                     arg.declarator.analyse(base_type, env)
2107                 arg.name = name_declarator.name
2108             if arg.name in directive_locals:
2109                 type_node = directive_locals[arg.name]
2110                 other_type = type_node.analyse_as_type(env)
2111                 if other_type is None:
2112                     error(type_node.pos, "Not a type")
2113                 elif (type is not PyrexTypes.py_object_type 
2114                         and not type.same_as(other_type)):
2115                     error(arg.base_type.pos, "Signature does not agree with previous declaration")
2116                     error(type_node.pos, "Previous declaration here")
2117                 else:
2118                     type = other_type
2119             if name_declarator and name_declarator.cname:
2120                 error(self.pos,
2121                     "Python function argument cannot have C name specification")
2122             arg.type = type.as_argument_type()
2123             arg.hdr_type = None
2124             arg.needs_conversion = 0
2125             arg.needs_type_test = 0
2126             arg.is_generic = 1
2127             if arg.type.is_pyobject:
2128                 if arg.or_none:
2129                     arg.accept_none = True
2130                 elif arg.not_none:
2131                     arg.accept_none = False
2132                 elif arg.type.is_extension_type or arg.type.is_builtin_type:
2133                     if arg.default and arg.default.constant_result is None:
2134                         # special case: def func(MyType obj = None)
2135                         arg.accept_none = True
2136                     else:
2137                         # default depends on compiler directive
2138                         arg.accept_none = allow_none_for_extension_args
2139                 else:
2140                     # probably just a plain 'object'
2141                     arg.accept_none = True
2142             else:
2143                 arg.accept_none = True # won't be used, but must be there
2144                 if arg.not_none:
2145                     error(arg.pos, "Only Python type arguments can have 'not None'")
2146                 if arg.or_none:
2147                     error(arg.pos, "Only Python type arguments can have 'or None'")
2148
2149     def analyse_signature(self, env):
2150         if self.entry.is_special:
2151             self.entry.trivial_signature = len(self.args) == 1 and not (self.star_arg or self.starstar_arg)
2152         elif not env.directives['always_allow_keywords'] and not (self.star_arg or self.starstar_arg):
2153             # Use the simpler calling signature for zero- and one-argument functions.
2154             if self.entry.signature is TypeSlots.pyfunction_signature:
2155                 if len(self.args) == 0:
2156                     self.entry.signature = TypeSlots.pyfunction_noargs
2157                 elif len(self.args) == 1:
2158                     if self.args[0].default is None and not self.args[0].kw_only:
2159                         self.entry.signature = TypeSlots.pyfunction_onearg
2160             elif self.entry.signature is TypeSlots.pymethod_signature:
2161                 if len(self.args) == 1:
2162                     self.entry.signature = TypeSlots.unaryfunc
2163                 elif len(self.args) == 2:
2164                     if self.args[1].default is None and not self.args[1].kw_only:
2165                         self.entry.signature = TypeSlots.ibinaryfunc
2166
2167         sig = self.entry.signature
2168         nfixed = sig.num_fixed_args()
2169         if sig is TypeSlots.pymethod_signature and nfixed == 1 \
2170                and len(self.args) == 0 and self.star_arg:
2171             # this is the only case where a diverging number of
2172             # arguments is not an error - when we have no explicit
2173             # 'self' parameter as in method(*args)
2174             sig = self.entry.signature = TypeSlots.pyfunction_signature # self is not 'really' used
2175             self.self_in_stararg = 1
2176             nfixed = 0
2177
2178         for i in range(min(nfixed, len(self.args))):
2179             arg = self.args[i]
2180             arg.is_generic = 0
2181             if sig.is_self_arg(i) and not self.is_staticmethod:
2182                 if self.is_classmethod:
2183                     arg.is_type_arg = 1
2184                     arg.hdr_type = arg.type = Builtin.type_type
2185                 else:
2186                     arg.is_self_arg = 1
2187                     arg.hdr_type = arg.type = env.parent_type
2188                 arg.needs_conversion = 0
2189             else:
2190                 arg.hdr_type = sig.fixed_arg_type(i)
2191                 if not arg.type.same_as(arg.hdr_type):
2192                     if arg.hdr_type.is_pyobject and arg.type.is_pyobject:
2193                         arg.needs_type_test = 1
2194                     else:
2195                         arg.needs_conversion = 1
2196             if arg.needs_conversion:
2197                 arg.hdr_cname = Naming.arg_prefix + arg.name
2198             else:
2199                 arg.hdr_cname = Naming.var_prefix + arg.name
2200
2201         if nfixed > len(self.args):
2202             self.bad_signature()
2203             return
2204         elif nfixed < len(self.args):
2205             if not sig.has_generic_args:
2206                 self.bad_signature()
2207             for arg in self.args:
2208                 if arg.is_generic and \
2209                         (arg.type.is_extension_type or arg.type.is_builtin_type):
2210                     arg.needs_type_test = 1
2211
2212     def bad_signature(self):
2213         sig = self.entry.signature
2214         expected_str = "%d" % sig.num_fixed_args()
2215         if sig.has_generic_args:
2216             expected_str = expected_str + " or more"
2217         name = self.name
2218         if name.startswith("__") and name.endswith("__"):
2219             desc = "Special method"
2220         else:
2221             desc = "Method"
2222         error(self.pos,
2223             "%s %s has wrong number of arguments "
2224             "(%d declared, %s expected)" % (
2225                 desc, self.name, len(self.args), expected_str))
2226
2227     def signature_has_nongeneric_args(self):
2228         argcount = len(self.args)
2229         if argcount == 0 or (
2230                 argcount == 1 and (self.args[0].is_self_arg or
2231                                    self.args[0].is_type_arg)):
2232             return 0
2233         return 1
2234
2235     def signature_has_generic_args(self):
2236         return self.entry.signature.has_generic_args
2237     
2238     def declare_pyfunction(self, env):
2239         #print "DefNode.declare_pyfunction:", self.name, "in", env ###
2240         name = self.name
2241         entry = env.lookup_here(name)
2242         if entry and entry.type.is_cfunction and not self.is_wrapper:
2243             warning(self.pos, "Overriding cdef method with def method.", 5)
2244         entry = env.declare_pyfunction(name, self.pos, allow_redefine=not self.is_wrapper)
2245         self.entry = entry
2246         prefix = env.next_id(env.scope_prefix)
2247
2248         entry.func_cname = \
2249             Naming.pyfunc_prefix + prefix + name
2250         entry.pymethdef_cname = \
2251             Naming.pymethdef_prefix + prefix + name
2252
2253         if self.is_generator:
2254             self.generator_body_cname = Naming.genbody_prefix + env.next_id(env.scope_prefix) + name
2255
2256         if Options.docstrings:
2257             entry.doc = embed_position(self.pos, self.doc)
2258             entry.doc_cname = \
2259                 Naming.funcdoc_prefix + prefix + name
2260             if entry.is_special:
2261                 if entry.name in TypeSlots.invisible or not entry.doc or (entry.name in '__getattr__' and env.directives['fast_getattr']):
2262                     entry.wrapperbase_cname = None
2263                 else:
2264                     entry.wrapperbase_cname = Naming.wrapperbase_prefix + prefix + name
2265         else:
2266             entry.doc = None
2267
2268     def declare_lambda_function(self, env):
2269         name = self.name
2270         prefix = env.scope_prefix
2271         func_cname = \
2272             Naming.lambda_func_prefix + u'funcdef' + prefix + self.lambda_name
2273         entry = env.declare_lambda_function(func_cname, self.pos)
2274         entry.pymethdef_cname = \
2275             Naming.lambda_func_prefix + u'methdef' + prefix + self.lambda_name
2276         entry.qualified_name = env.qualify_name(self.lambda_name)
2277         entry.doc = None
2278         self.entry = entry
2279
2280     def declare_arguments(self, env):
2281         for arg in self.args:
2282             if not arg.name:
2283                 error(arg.pos, "Missing argument name")
2284             else:
2285                 env.control_flow.set_state((), (arg.name, 'source'), 'arg')
2286                 env.control_flow.set_state((), (arg.name, 'initialized'), True)
2287             if arg.needs_conversion:
2288                 arg.entry = env.declare_var(arg.name, arg.type, arg.pos)
2289                 if arg.type.is_pyobject:
2290                     arg.entry.init = "0"
2291                 arg.entry.init_to_none = 0
2292             else:
2293                 arg.entry = self.declare_argument(env, arg)
2294             arg.entry.used = 1
2295             arg.entry.is_self_arg = arg.is_self_arg
2296             if arg.hdr_type:
2297                 if arg.is_self_arg or arg.is_type_arg or \
2298                     (arg.type.is_extension_type and not arg.hdr_type.is_extension_type):
2299                         arg.entry.is_declared_generic = 1
2300         self.declare_python_arg(env, self.star_arg)
2301         self.declare_python_arg(env, self.starstar_arg)
2302
2303     def declare_python_arg(self, env, arg):
2304         if arg:
2305             if env.directives['infer_types'] != False:
2306                 type = PyrexTypes.unspecified_type
2307             else:
2308                 type = py_object_type
2309             entry = env.declare_var(arg.name, type, arg.pos)
2310             entry.used = 1
2311             entry.init = "0"
2312             entry.init_to_none = 0
2313             entry.xdecref_cleanup = 1
2314             arg.entry = entry
2315             env.control_flow.set_state((), (arg.name, 'initialized'), True)
2316             
2317     def analyse_expressions(self, env):
2318         self.local_scope.directives = env.directives
2319         self.analyse_default_values(env)
2320         if self.needs_assignment_synthesis(env):
2321             # Shouldn't we be doing this at the module level too?
2322             self.synthesize_assignment_node(env)
2323
2324     def needs_assignment_synthesis(self, env, code=None):
2325         # Should enable for module level as well, that will require more testing...
2326         if self.entry.is_anonymous:
2327             return True
2328         if env.is_module_scope:
2329             if code is None:
2330                 return env.directives['binding']
2331             else:
2332                 return code.globalstate.directives['binding']
2333         return env.is_py_class_scope or env.is_closure_scope
2334
2335     def synthesize_assignment_node(self, env):
2336         import ExprNodes
2337         genv = env
2338         while genv.is_py_class_scope or genv.is_c_class_scope:
2339             genv = genv.outer_scope
2340
2341         if genv.is_closure_scope:
2342             rhs = ExprNodes.InnerFunctionNode(
2343                 self.pos, pymethdef_cname = self.entry.pymethdef_cname)
2344         else:
2345             rhs = ExprNodes.PyCFunctionNode(
2346                 self.pos, pymethdef_cname = self.entry.pymethdef_cname, binding = env.directives['binding'])
2347
2348         if env.is_py_class_scope:
2349             if not self.is_staticmethod and not self.is_classmethod:
2350                 rhs.binding = True
2351
2352         self.assmt = SingleAssignmentNode(self.pos,
2353             lhs = ExprNodes.NameNode(self.pos, name = self.name),
2354             rhs = rhs)
2355         self.assmt.analyse_declarations(env)
2356         self.assmt.analyse_expressions(env)
2357             
2358     def generate_function_header(self, code, with_pymethdef, proto_only=0):
2359         arg_code_list = []
2360         sig = self.entry.signature
2361         if sig.has_dummy_arg or self.self_in_stararg:
2362             arg_code_list.append(
2363                 "PyObject *%s" % Naming.self_cname)
2364         for arg in self.args:
2365             if not arg.is_generic:
2366                 if arg.is_self_arg or arg.is_type_arg:
2367                     arg_code_list.append("PyObject *%s" % arg.hdr_cname)
2368                 else:
2369                     arg_code_list.append(
2370                         arg.hdr_type.declaration_code(arg.hdr_cname))
2371         if not self.entry.is_special and sig.method_flags() == [TypeSlots.method_noargs]:
2372             arg_code_list.append("CYTHON_UNUSED PyObject *unused")
2373         if sig.has_generic_args:
2374             arg_code_list.append(
2375                 "PyObject *%s, PyObject *%s"
2376                     % (Naming.args_cname, Naming.kwds_cname))
2377         arg_code = ", ".join(arg_code_list)
2378         dc = self.return_type.declaration_code(self.entry.func_cname)
2379         mf = " ".join(self.modifiers).upper()
2380         if mf: mf += " "
2381         header = "static %s%s(%s)" % (mf, dc, arg_code)
2382         code.putln("%s; /*proto*/" % header)
2383         if proto_only:
2384             return
2385         if (Options.docstrings and self.entry.doc and
2386                 not self.entry.scope.is_property_scope and
2387                 (not self.entry.is_special or self.entry.wrapperbase_cname)):
2388             docstr = self.entry.doc
2389             if docstr.is_unicode:
2390                 docstr = docstr.utf8encode()
2391             code.putln(
2392                 'static char %s[] = "%s";' % (
2393                     self.entry.doc_cname,
2394                     split_string_literal(escape_byte_string(docstr))))
2395             if self.entry.is_special:
2396                 code.putln(
2397                     "struct wrapperbase %s;" % self.entry.wrapperbase_cname)
2398         if with_pymethdef:
2399             code.put(
2400                 "static PyMethodDef %s = " % 
2401                     self.entry.pymethdef_cname)
2402             code.put_pymethoddef(self.entry, ";", allow_skip=False)
2403         if self.is_generator:
2404             code.putln("static PyObject *%s(PyObject *%s, PyObject *%s) /* generator body */\n{" %
2405                        (self.generator_body_cname, Naming.self_cname, Naming.sent_value_cname))
2406             self.generator = GeneratorWrapperNode(self,
2407                                                   func_cname=self.entry.func_cname,
2408                                                   body_cname=self.generator_body_cname,
2409                                                   header=header)
2410         else:
2411             code.putln("%s {" % header)
2412
2413     def generate_argument_declarations(self, env, code):
2414         for arg in self.args:
2415             if arg.is_generic: # or arg.needs_conversion:
2416                 if arg.needs_conversion:
2417                     code.putln("PyObject *%s = 0;" % arg.hdr_cname)
2418                 elif not arg.entry.in_closure:
2419                     code.put_var_declaration(arg.entry)
2420
2421     def generate_keyword_list(self, code):
2422         if self.signature_has_generic_args() and \
2423                 self.signature_has_nongeneric_args():
2424             code.put(
2425                 "static PyObject **%s[] = {" %
2426                     Naming.pykwdlist_cname)
2427             for arg in self.args:
2428                 if arg.is_generic:
2429                     pystring_cname = code.intern_identifier(arg.name)
2430                     code.put('&%s,' % pystring_cname)
2431             code.putln("0};")
2432
2433     def generate_argument_parsing_code(self, env, code):
2434         # Generate fast equivalent of PyArg_ParseTuple call for
2435         # generic arguments, if any, including args/kwargs
2436         if self.entry.signature.has_dummy_arg and not self.self_in_stararg:
2437             # get rid of unused argument warning
2438             code.putln("%s = %s;" % (Naming.self_cname, Naming.self_cname))
2439
2440         old_error_label = code.new_error_label()
2441         our_error_label = code.error_label
2442         end_label = code.new_label("argument_unpacking_done")
2443
2444         has_kwonly_args = self.num_kwonly_args > 0
2445         has_star_or_kw_args = self.star_arg is not None \
2446             or self.starstar_arg is not None or has_kwonly_args
2447
2448         for arg in self.args:
2449             if not arg.type.is_pyobject:
2450                 done = arg.type.create_from_py_utility_code(env)
2451                 if not done: pass # will fail later
2452
2453         if not self.signature_has_generic_args():
2454             if has_star_or_kw_args:
2455                 error(self.pos, "This method cannot have * or keyword arguments")
2456             self.generate_argument_conversion_code(code)
2457
2458         elif not self.signature_has_nongeneric_args():
2459             # func(*args) or func(**kw) or func(*args, **kw)
2460             self.generate_stararg_copy_code(code)
2461
2462         else:
2463             positional_args = []
2464             kw_only_args = []
2465             for arg in self.args:
2466                 arg_entry = arg.entry
2467                 if arg.is_generic:
2468                     if arg.default:
2469                         if not arg.is_self_arg and not arg.is_type_arg:
2470                             if arg.kw_only:
2471                                 kw_only_args.append(arg)
2472                             else:
2473                                 positional_args.append(arg)
2474                     elif arg.kw_only:
2475                         kw_only_args.append(arg)
2476                     elif not arg.is_self_arg and not arg.is_type_arg:
2477                         positional_args.append(arg)
2478
2479             self.generate_tuple_and_keyword_parsing_code(
2480                 positional_args, kw_only_args, end_label, code)
2481
2482         code.error_label = old_error_label
2483         if code.label_used(our_error_label):
2484             if not code.label_used(end_label):
2485                 code.put_goto(end_label)
2486             code.put_label(our_error_label)
2487             if has_star_or_kw_args:
2488                 self.generate_arg_decref(self.star_arg, code)
2489                 if self.starstar_arg:
2490                     if self.starstar_arg.entry.xdecref_cleanup:
2491                         code.put_var_xdecref_clear(self.starstar_arg.entry)
2492                     else:
2493                         code.put_var_decref_clear(self.starstar_arg.entry)
2494             code.putln('__Pyx_AddTraceback("%s");' % self.entry.qualified_name)
2495             # The arguments are put into the closure one after the
2496             # other, so when type errors are found, all references in
2497             # the closure instance must be properly ref-counted to
2498             # facilitate generic closure instance deallocation.  In
2499             # the case of an argument type error, it's best to just
2500             # DECREF+clear the already handled references, as this
2501             # frees their references as early as possible.
2502             for arg in self.args:
2503                 if arg.type.is_pyobject and arg.entry.in_closure:
2504                     code.put_var_xdecref_clear(arg.entry)
2505             if self.needs_closure:
2506                 code.put_decref(Naming.cur_scope_cname, self.local_scope.scope_class.type)
2507             code.put_finish_refcount_context()
2508             code.putln("return %s;" % self.error_value())
2509         if code.label_used(end_label):
2510             code.put_label(end_label)
2511
2512         # fix refnanny view on closure variables here, instead of
2513         # doing it separately for each arg parsing special case
2514         if self.star_arg and self.star_arg.entry.in_closure:
2515             code.put_var_giveref(self.star_arg.entry)
2516         if self.starstar_arg and self.starstar_arg.entry.in_closure:
2517             code.put_var_giveref(self.starstar_arg.entry)
2518         for arg in self.args:
2519             if arg.type.is_pyobject and arg.entry.in_closure:
2520                 code.put_var_giveref(arg.entry)
2521
2522     def generate_arg_assignment(self, arg, item, code):
2523         if arg.type.is_pyobject:
2524             if arg.is_generic:
2525                 item = PyrexTypes.typecast(arg.type, PyrexTypes.py_object_type, item)
2526             entry = arg.entry
2527             if entry.in_closure:
2528                 code.put_incref(item, PyrexTypes.py_object_type)
2529             code.putln("%s = %s;" % (entry.cname, item))
2530         else:
2531             func = arg.type.from_py_function
2532             if func:
2533                 code.putln("%s = %s(%s); %s" % (
2534                     arg.entry.cname,
2535                     func,
2536                     item,
2537                     code.error_goto_if(arg.type.error_condition(arg.entry.cname), arg.pos)))
2538             else:
2539                 error(arg.pos, "Cannot convert Python object argument to type '%s'" % arg.type)
2540     
2541     def generate_arg_xdecref(self, arg, code):
2542         if arg:
2543             code.put_var_xdecref_clear(arg.entry)
2544     
2545     def generate_arg_decref(self, arg, code):
2546         if arg:
2547             code.put_var_decref_clear(arg.entry)
2548
2549     def generate_stararg_copy_code(self, code):
2550         if not self.star_arg:
2551             code.globalstate.use_utility_code(raise_argtuple_invalid_utility_code)
2552             code.putln("if (unlikely(PyTuple_GET_SIZE(%s) > 0)) {" %
2553                        Naming.args_cname)
2554             code.put('__Pyx_RaiseArgtupleInvalid("%s", 1, 0, 0, PyTuple_GET_SIZE(%s)); return %s;' % (
2555                     self.name, Naming.args_cname, self.error_value()))
2556             code.putln("}")
2557
2558         code.globalstate.use_utility_code(keyword_string_check_utility_code)
2559
2560         if self.starstar_arg:
2561             if self.star_arg:
2562                 kwarg_check = "unlikely(%s)" % Naming.kwds_cname
2563             else:
2564                 kwarg_check = "%s" % Naming.kwds_cname
2565         else:
2566             kwarg_check = "unlikely(%s) && unlikely(PyDict_Size(%s) > 0)" % (
2567                 Naming.kwds_cname, Naming.kwds_cname)
2568         code.putln(
2569             "if (%s && unlikely(!__Pyx_CheckKeywordStrings(%s, \"%s\", %d))) return %s;" % (
2570                 kwarg_check, Naming.kwds_cname, self.name,
2571                 bool(self.starstar_arg), self.error_value()))
2572
2573         if self.starstar_arg:
2574             code.putln("%s = (%s) ? PyDict_Copy(%s) : PyDict_New();" % (
2575                     self.starstar_arg.entry.cname,
2576                     Naming.kwds_cname,
2577                     Naming.kwds_cname))
2578             code.putln("if (unlikely(!%s)) return %s;" % (
2579                     self.starstar_arg.entry.cname, self.error_value()))
2580             self.starstar_arg.entry.xdecref_cleanup = 0
2581             code.put_gotref(self.starstar_arg.entry.cname)
2582
2583         if self.self_in_stararg:
2584             # need to create a new tuple with 'self' inserted as first item
2585             code.put("%s = PyTuple_New(PyTuple_GET_SIZE(%s)+1); if (unlikely(!%s)) " % (
2586                     self.star_arg.entry.cname,
2587                     Naming.args_cname,
2588                     self.star_arg.entry.cname))
2589             if self.starstar_arg:
2590                 code.putln("{")
2591                 code.put_decref_clear(self.starstar_arg.entry.cname, py_object_type)
2592                 code.putln("return %s;" % self.error_value())
2593                 code.putln("}")
2594             else:
2595                 code.putln("return %s;" % self.error_value())
2596             code.put_gotref(self.star_arg.entry.cname)
2597             code.put_incref(Naming.self_cname, py_object_type)
2598             code.put_giveref(Naming.self_cname)
2599             code.putln("PyTuple_SET_ITEM(%s, 0, %s);" % (
2600                 self.star_arg.entry.cname, Naming.self_cname))
2601             temp = code.funcstate.allocate_temp(PyrexTypes.c_py_ssize_t_type, manage_ref=False)
2602             code.putln("for (%s=0; %s < PyTuple_GET_SIZE(%s); %s++) {" % (
2603                 temp, temp, Naming.args_cname, temp))
2604             code.putln("PyObject* item = PyTuple_GET_ITEM(%s, %s);" % (
2605                 Naming.args_cname, temp))
2606             code.put_incref("item", py_object_type)
2607             code.put_giveref("item")
2608             code.putln("PyTuple_SET_ITEM(%s, %s+1, item);" % (
2609                 self.star_arg.entry.cname, temp))
2610             code.putln("}")
2611             code.funcstate.release_temp(temp)
2612             self.star_arg.entry.xdecref_cleanup = 0
2613         elif self.star_arg:
2614             code.put_incref(Naming.args_cname, py_object_type)
2615             code.putln("%s = %s;" % (
2616                     self.star_arg.entry.cname,
2617                     Naming.args_cname))
2618             self.star_arg.entry.xdecref_cleanup = 0
2619
2620     def generate_tuple_and_keyword_parsing_code(self, positional_args,
2621                                                 kw_only_args, success_label, code):
2622         argtuple_error_label = code.new_label("argtuple_error")
2623
2624         min_positional_args = self.num_required_args - self.num_required_kw_args
2625         if len(self.args) > 0 and (self.args[0].is_self_arg or self.args[0].is_type_arg):
2626             min_positional_args -= 1
2627         max_positional_args = len(positional_args)
2628         has_fixed_positional_count = not self.star_arg and \
2629             min_positional_args == max_positional_args
2630
2631         code.globalstate.use_utility_code(raise_double_keywords_utility_code)
2632         code.globalstate.use_utility_code(raise_argtuple_invalid_utility_code)
2633         if self.num_required_kw_args:
2634             code.globalstate.use_utility_code(raise_keyword_required_utility_code)
2635
2636         if self.starstar_arg or self.star_arg:
2637             self.generate_stararg_init_code(max_positional_args, code)
2638
2639         # --- optimised code when we receive keyword arguments
2640         if self.num_required_kw_args:
2641             likely_hint = "likely"
2642         else:
2643             likely_hint = "unlikely"
2644         code.putln("if (%s(%s)) {" % (likely_hint, Naming.kwds_cname))
2645         self.generate_keyword_unpacking_code(
2646             min_positional_args, max_positional_args,
2647             has_fixed_positional_count,
2648             positional_args, kw_only_args, argtuple_error_label, code)
2649
2650         # --- optimised code when we do not receive any keyword arguments
2651         if (self.num_required_kw_args and min_positional_args > 0) or min_positional_args == max_positional_args:
2652             # Python raises arg tuple related errors first, so we must
2653             # check the length here
2654             if min_positional_args == max_positional_args and not self.star_arg:
2655                 compare = '!='
2656             else:
2657                 compare = '<'
2658             code.putln('} else if (PyTuple_GET_SIZE(%s) %s %d) {' % (
2659                     Naming.args_cname, compare, min_positional_args))
2660             code.put_goto(argtuple_error_label)
2661
2662         if self.num_required_kw_args:
2663             # pure error case: keywords required but not passed
2664             if max_positional_args > min_positional_args and not self.star_arg:
2665                 code.putln('} else if (PyTuple_GET_SIZE(%s) > %d) {' % (
2666                         Naming.args_cname, max_positional_args))
2667                 code.put_goto(argtuple_error_label)
2668             code.putln('} else {')
2669             for i, arg in enumerate(kw_only_args):
2670                 if not arg.default:
2671                     pystring_cname = code.intern_identifier(arg.name)
2672                     # required keyword-only argument missing
2673                     code.put('__Pyx_RaiseKeywordRequired("%s", %s); ' % (
2674                             self.name,
2675                             pystring_cname))
2676                     code.putln(code.error_goto(self.pos))
2677                     break
2678
2679         elif min_positional_args == max_positional_args:
2680             # parse the exact number of positional arguments from the
2681             # args tuple
2682             code.putln('} else {')
2683             for i, arg in enumerate(positional_args):
2684                 item = "PyTuple_GET_ITEM(%s, %d)" % (Naming.args_cname, i)
2685                 self.generate_arg_assignment(arg, item, code)
2686             self.generate_arg_default_assignments(code)
2687
2688         else:
2689             # parse the positional arguments from the variable length
2690             # args tuple
2691             code.putln('} else {')
2692             self.generate_arg_default_assignments(code)
2693             code.putln('switch (PyTuple_GET_SIZE(%s)) {' % Naming.args_cname)
2694             if self.star_arg:
2695                 code.putln('default:')
2696             reversed_args = list(enumerate(positional_args))[::-1]
2697             for i, arg in reversed_args:
2698                 if i >= min_positional_args-1:
2699                     if min_positional_args > 1:
2700                         code.putln('case %2d:' % (i+1)) # pure code beautification
2701                     else:
2702                         code.put('case %2d: ' % (i+1))
2703                 item = "PyTuple_GET_ITEM(%s, %d)" % (Naming.args_cname, i)
2704                 self.generate_arg_assignment(arg, item, code)
2705             if min_positional_args == 0:
2706                 code.put('case  0: ')
2707             code.putln('break;')
2708             if self.star_arg:
2709                 if min_positional_args:
2710                     for i in range(min_positional_args-1, -1, -1):
2711                         code.putln('case %2d:' % i)
2712                     code.put_goto(argtuple_error_label)
2713             else:
2714                 code.put('default: ')
2715                 code.put_goto(argtuple_error_label)
2716             code.putln('}')
2717
2718         code.putln('}')
2719
2720         if code.label_used(argtuple_error_label):
2721             code.put_goto(success_label)
2722             code.put_label(argtuple_error_label)
2723             code.put('__Pyx_RaiseArgtupleInvalid("%s", %d, %d, %d, PyTuple_GET_SIZE(%s)); ' % (
2724                     self.name, has_fixed_positional_count,
2725                     min_positional_args, max_positional_args,
2726                     Naming.args_cname))
2727             code.putln(code.error_goto(self.pos))
2728
2729     def generate_arg_default_assignments(self, code):
2730         for arg in self.args:
2731             if arg.is_generic and arg.default:
2732                 code.putln(
2733                     "%s = %s;" % (
2734                         arg.entry.cname,
2735                         arg.calculate_default_value_code(code)))
2736
2737     def generate_stararg_init_code(self, max_positional_args, code):
2738         if self.starstar_arg:
2739             self.starstar_arg.entry.xdecref_cleanup = 0
2740             code.putln('%s = PyDict_New(); if (unlikely(!%s)) return %s;' % (
2741                     self.starstar_arg.entry.cname,
2742                     self.starstar_arg.entry.cname,
2743                     self.error_value()))
2744             code.put_gotref(self.starstar_arg.entry.cname)
2745         if self.star_arg:
2746             self.star_arg.entry.xdecref_cleanup = 0
2747             code.putln('if (PyTuple_GET_SIZE(%s) > %d) {' % (
2748                     Naming.args_cname,
2749                     max_positional_args))
2750             code.putln('%s = PyTuple_GetSlice(%s, %d, PyTuple_GET_SIZE(%s));' % (
2751                     self.star_arg.entry.cname, Naming.args_cname,
2752                     max_positional_args, Naming.args_cname))
2753             code.putln("if (unlikely(!%s)) {" % self.star_arg.entry.cname)
2754             if self.starstar_arg:
2755                 code.put_decref(self.starstar_arg.entry.cname, py_object_type)
2756             if self.needs_closure:
2757                 code.put_decref(Naming.cur_scope_cname, self.local_scope.scope_class.type)
2758             code.put_finish_refcount_context()
2759             code.putln('return %s;' % self.error_value())
2760             code.putln('}')
2761             code.put_gotref(self.star_arg.entry.cname)
2762             code.putln('} else {')
2763             code.put("%s = %s; " % (self.star_arg.entry.cname, Naming.empty_tuple))
2764             code.put_incref(Naming.empty_tuple, py_object_type)
2765             code.putln('}')
2766
2767     def generate_keyword_unpacking_code(self, min_positional_args, max_positional_args,
2768                                         has_fixed_positional_count, positional_args,
2769                                         kw_only_args, argtuple_error_label, code):
2770         all_args = tuple(positional_args) + tuple(kw_only_args)
2771         max_args = len(all_args)
2772
2773         code.putln("Py_ssize_t kw_args = PyDict_Size(%s);" %
2774                    Naming.kwds_cname)
2775         # the 'values' array collects borrowed references to arguments
2776         # before doing any type coercion etc.
2777         code.putln("PyObject* values[%d] = {%s};" % (
2778             max_args, ','.join('0'*max_args)))
2779
2780         # assign borrowed Python default values to the values array,
2781         # so that they can be overwritten by received arguments below
2782         for i, arg in enumerate(all_args):
2783             if arg.default and arg.type.is_pyobject:
2784                 default_value = arg.calculate_default_value_code(code)
2785                 code.putln('values[%d] = %s;' % (i, arg.type.as_pyobject(default_value)))
2786
2787         # parse the args tuple and check that it's not too long
2788         code.putln('switch (PyTuple_GET_SIZE(%s)) {' % Naming.args_cname)
2789         if self.star_arg:
2790             code.putln('default:')
2791         for i in range(max_positional_args-1, -1, -1):
2792             code.put('case %2d: ' % (i+1))
2793             code.putln("values[%d] = PyTuple_GET_ITEM(%s, %d);" % (
2794                     i, Naming.args_cname, i))
2795         code.putln('case  0: break;')
2796         if not self.star_arg:
2797             code.put('default: ') # more arguments than allowed
2798             code.put_goto(argtuple_error_label)
2799         code.putln('}')
2800
2801         # now fill up the positional/required arguments with values
2802         # from the kw dict
2803         if self.num_required_args or max_positional_args > 0:
2804             last_required_arg = -1
2805             for i, arg in enumerate(all_args):
2806                 if not arg.default:
2807                     last_required_arg = i
2808             if last_required_arg < max_positional_args:
2809                 last_required_arg = max_positional_args-1
2810             num_required_args = self.num_required_args
2811             if max_positional_args > 0:
2812                 code.putln('switch (PyTuple_GET_SIZE(%s)) {' % Naming.args_cname)
2813             for i, arg in enumerate(all_args[:last_required_arg+1]):
2814                 if max_positional_args > 0 and i <= max_positional_args:
2815                     if self.star_arg and i == max_positional_args:
2816                         code.putln('default:')
2817                     else:
2818                         code.putln('case %2d:' % i)
2819                 pystring_cname = code.intern_identifier(arg.name)
2820                 if arg.default:
2821                     if arg.kw_only:
2822                         # handled separately below
2823                         continue
2824                     code.putln('if (kw_args > 0) {')
2825                     code.putln('PyObject* value = PyDict_GetItem(%s, %s);' % (
2826                         Naming.kwds_cname, pystring_cname))
2827                     code.putln('if (value) { values[%d] = value; kw_args--; }' % i)
2828                     code.putln('}')
2829                 else:
2830                     num_required_args -= 1
2831                     code.putln('values[%d] = PyDict_GetItem(%s, %s);' % (
2832                         i, Naming.kwds_cname, pystring_cname))
2833                     code.putln('if (likely(values[%d])) kw_args--;' % i);
2834                     if i < min_positional_args:
2835                         if i == 0:
2836                             # special case: we know arg 0 is missing
2837                             code.put('else ')
2838                             code.put_goto(argtuple_error_label)
2839                         else:
2840                             # print the correct number of values (args or
2841                             # kwargs) that were passed into positional
2842                             # arguments up to this point
2843                             code.putln('else {')
2844                             code.put('__Pyx_RaiseArgtupleInvalid("%s", %d, %d, %d, %d); ' % (
2845                                     self.name, has_fixed_positional_count,
2846                                     min_positional_args, max_positional_args, i))
2847                             code.putln(code.error_goto(self.pos))
2848                             code.putln('}')
2849                     elif arg.kw_only:
2850                         code.putln('else {')
2851                         code.put('__Pyx_RaiseKeywordRequired("%s", %s); ' %(
2852                                 self.name, pystring_cname))
2853                         code.putln(code.error_goto(self.pos))
2854                         code.putln('}')
2855             if max_positional_args > 0:
2856                 code.putln('}')
2857
2858         if kw_only_args and not self.starstar_arg:
2859             # unpack optional keyword-only arguments
2860             # checking for interned strings in a dict is faster than iterating
2861             # but it's too likely that we must iterate if we expect **kwargs
2862             optional_args = []
2863             for i, arg in enumerate(all_args[max_positional_args:]):
2864                 if not arg.kw_only or not arg.default:
2865                     continue
2866                 optional_args.append((i+max_positional_args, arg))
2867             if optional_args:
2868                 # this mimics an unrolled loop so that we can "break" out of it
2869                 code.putln('while (kw_args > 0) {')
2870                 code.putln('PyObject* value;')
2871                 for i, arg in optional_args:
2872                     pystring_cname = code.intern_identifier(arg.name)
2873                     code.putln(
2874                         'value = PyDict_GetItem(%s, %s);' % (
2875                         Naming.kwds_cname, pystring_cname))
2876                     code.putln(
2877                         'if (value) { values[%d] = value; if (!(--kw_args)) break; }' % i)
2878                 code.putln('break;')
2879                 code.putln('}')
2880
2881         code.putln('if (unlikely(kw_args > 0)) {')
2882         # non-positional/-required kw args left in dict: default args,
2883         # kw-only args, **kwargs or error
2884         #
2885         # This is sort of a catch-all: except for checking required
2886         # arguments, this will always do the right thing for unpacking
2887         # keyword arguments, so that we can concentrate on optimising
2888         # common cases above.
2889         if max_positional_args == 0:
2890             pos_arg_count = "0"
2891         elif self.star_arg:
2892             code.putln("const Py_ssize_t used_pos_args = (PyTuple_GET_SIZE(%s) < %d) ? PyTuple_GET_SIZE(%s) : %d;" % (
2893                     Naming.args_cname, max_positional_args,
2894                     Naming.args_cname, max_positional_args))
2895             pos_arg_count = "used_pos_args"
2896         else:
2897             pos_arg_count = "PyTuple_GET_SIZE(%s)" % Naming.args_cname
2898         code.globalstate.use_utility_code(parse_keywords_utility_code)
2899         code.put(
2900             'if (unlikely(__Pyx_ParseOptionalKeywords(%s, %s, %s, values, %s, "%s") < 0)) ' % (
2901                 Naming.kwds_cname,
2902                 Naming.pykwdlist_cname,
2903                 self.starstar_arg and self.starstar_arg.entry.cname or '0',
2904                 pos_arg_count,
2905                 self.name))
2906         code.putln(code.error_goto(self.pos))
2907         code.putln('}')
2908
2909         # convert arg values to their final type and assign them
2910         for i, arg in enumerate(all_args):
2911             if arg.default and not arg.type.is_pyobject:
2912                 code.putln("if (values[%d]) {" % i)
2913             self.generate_arg_assignment(arg, "values[%d]" % i, code)
2914             if arg.default and not arg.type.is_pyobject:
2915                 code.putln('} else {')
2916                 code.putln(
2917                     "%s = %s;" % (
2918                         arg.entry.cname,
2919                         arg.calculate_default_value_code(code)))
2920                 code.putln('}')
2921
2922     def generate_argument_conversion_code(self, code):
2923         # Generate code to convert arguments from signature type to
2924         # declared type, if needed.  Also copies signature arguments
2925         # into closure fields.
2926         for arg in self.args:
2927             if arg.needs_conversion:
2928                 self.generate_arg_conversion(arg, code)
2929             elif arg.entry.in_closure:
2930                 if arg.type.is_pyobject:
2931                     code.put_incref(arg.hdr_cname, py_object_type)
2932                 code.putln('%s = %s;' % (arg.entry.cname, arg.hdr_cname))
2933
2934     def generate_arg_conversion(self, arg, code):
2935         # Generate conversion code for one argument.
2936         old_type = arg.hdr_type
2937         new_type = arg.type
2938         if old_type.is_pyobject:
2939             if arg.default:
2940                 code.putln("if (%s) {" % arg.hdr_cname)
2941             else:
2942                 code.putln("assert(%s); {" % arg.hdr_cname)
2943             self.generate_arg_conversion_from_pyobject(arg, code)
2944             code.putln("}")
2945         elif new_type.is_pyobject:
2946             self.generate_arg_conversion_to_pyobject(arg, code)
2947         else:
2948             if new_type.assignable_from(old_type):
2949                 code.putln(
2950                     "%s = %s;" % (arg.entry.cname, arg.hdr_cname))
2951             else:
2952                 error(arg.pos,
2953                     "Cannot convert 1 argument from '%s' to '%s'" %
2954                         (old_type, new_type))
2955     
2956     def generate_arg_conversion_from_pyobject(self, arg, code):
2957         new_type = arg.type
2958         func = new_type.from_py_function
2959         # copied from CoerceFromPyTypeNode
2960         if func:
2961             lhs = arg.entry.cname
2962             rhs = "%s(%s)" % (func, arg.hdr_cname)
2963             if new_type.is_enum:
2964                 rhs = PyrexTypes.typecast(new_type, PyrexTypes.c_long_type, rhs)
2965             code.putln("%s = %s; %s" % (
2966                 lhs, 
2967                 rhs,
2968                 code.error_goto_if(new_type.error_condition(arg.entry.cname), arg.pos)))
2969         else:
2970             error(arg.pos, 
2971                 "Cannot convert Python object argument to type '%s'" 
2972                     % new_type)
2973     
2974     def generate_arg_conversion_to_pyobject(self, arg, code):
2975         old_type = arg.hdr_type
2976         func = old_type.to_py_function
2977         if func:
2978             code.putln("%s = %s(%s); %s" % (
2979                 arg.entry.cname,
2980                 func,
2981                 arg.hdr_cname,
2982                 code.error_goto_if_null(arg.entry.cname, arg.pos)))
2983             code.put_var_gotref(arg.entry)
2984         else:
2985             error(arg.pos,
2986                 "Cannot convert argument of type '%s' to Python object"
2987                     % old_type)
2988
2989     def generate_argument_type_tests(self, code):
2990         # Generate type tests for args whose signature
2991         # type is PyObject * and whose declared type is
2992         # a subtype thereof.
2993         for arg in self.args:
2994             if arg.needs_type_test:
2995                 self.generate_arg_type_test(arg, code)
2996             elif not arg.accept_none and arg.type.is_pyobject:
2997                 self.generate_arg_none_check(arg, code)
2998
2999     def error_value(self):
3000         return self.entry.signature.error_value
3001     
3002     def caller_will_check_exceptions(self):
3003         return 1
3004             
3005 class OverrideCheckNode(StatNode):
3006     # A Node for dispatching to the def method if it
3007     # is overriden. 
3008     #
3009     #  py_func
3010     #
3011     #  args
3012     #  func_temp
3013     #  body
3014     
3015     child_attrs = ['body']
3016     
3017     body = None
3018
3019     def analyse_expressions(self, env):
3020         self.args = env.arg_entries
3021         if self.py_func.is_module_scope:
3022             first_arg = 0
3023         else:
3024             first_arg = 1
3025         import ExprNodes
3026         self.func_node = ExprNodes.RawCNameExprNode(self.pos, py_object_type)
3027         call_tuple = ExprNodes.TupleNode(self.pos, args=[ExprNodes.NameNode(self.pos, name=arg.name) for arg in self.args[first_arg:]])
3028         call_node = ExprNodes.SimpleCallNode(self.pos,
3029                                              function=self.func_node, 
3030                                              args=[ExprNodes.NameNode(self.pos, name=arg.name) for arg in self.args[first_arg:]])
3031         self.body = ReturnStatNode(self.pos, value=call_node)
3032         self.body.analyse_expressions(env)
3033         
3034     def generate_execution_code(self, code):
3035         interned_attr_cname = code.intern_identifier(self.py_func.entry.name)
3036         # Check to see if we are an extension type
3037         if self.py_func.is_module_scope:
3038             self_arg = "((PyObject *)%s)" % Naming.module_cname
3039         else:
3040             self_arg = "((PyObject *)%s)" % self.args[0].cname
3041         code.putln("/* Check if called by wrapper */")
3042         code.putln("if (unlikely(%s)) ;" % Naming.skip_dispatch_cname)
3043         code.putln("/* Check if overriden in Python */")
3044         if self.py_func.is_module_scope:
3045             code.putln("else {")
3046         else:
3047             code.putln("else if (unlikely(Py_TYPE(%s)->tp_dictoffset != 0)) {" % self_arg)
3048         func_node_temp = code.funcstate.allocate_temp(py_object_type, manage_ref=True)
3049         self.func_node.set_cname(func_node_temp)
3050         # need to get attribute manually--scope would return cdef method
3051         err = code.error_goto_if_null(func_node_temp, self.pos)
3052         code.putln("%s = PyObject_GetAttr(%s, %s); %s" % (
3053             func_node_temp, self_arg, interned_attr_cname, err))
3054         code.put_gotref(func_node_temp)
3055         is_builtin_function_or_method = "PyCFunction_Check(%s)" % func_node_temp
3056         is_overridden = "(PyCFunction_GET_FUNCTION(%s) != (void *)&%s)" % (
3057             func_node_temp, self.py_func.entry.func_cname)
3058         code.putln("if (!%s || %s) {" % (is_builtin_function_or_method, is_overridden))
3059         self.body.generate_execution_code(code)
3060         code.putln("}")
3061         code.put_decref_clear(func_node_temp, PyrexTypes.py_object_type)
3062         code.funcstate.release_temp(func_node_temp)
3063         code.putln("}")
3064
3065 class ClassDefNode(StatNode, BlockNode):
3066     pass
3067
3068 class PyClassDefNode(ClassDefNode):
3069     #  A Python class definition.
3070     #
3071     #  name     EncodedString   Name of the class
3072     #  doc      string or None
3073     #  body     StatNode        Attribute definition code
3074     #  entry    Symtab.Entry
3075     #  scope    PyClassScope
3076     #  decorators    [DecoratorNode]        list of decorators or None
3077     #
3078     #  The following subnodes are constructed internally:
3079     #
3080     #  dict     DictNode   Class dictionary or Py3 namespace
3081     #  classobj ClassNode  Class object
3082     #  target   NameNode   Variable to assign class object to
3083
3084     child_attrs = ["body", "dict", "metaclass", "mkw", "bases", "classobj", "target"]
3085     decorators = None
3086     py3_style_class = False # Python3 style class (bases+kwargs)
3087     
3088     def __init__(self, pos, name, bases, doc, body, decorators = None,
3089                  keyword_args = None, starstar_arg = None):
3090         StatNode.__init__(self, pos)
3091         self.name = name
3092         self.doc = doc
3093         self.body = body
3094         self.decorators = decorators
3095         import ExprNodes
3096         if self.doc and Options.docstrings:
3097             doc = embed_position(self.pos, self.doc)
3098             doc_node = ExprNodes.StringNode(pos, value = doc)
3099         else:
3100             doc_node = None
3101         if keyword_args or starstar_arg:
3102             self.py3_style_class = True
3103             self.bases = bases
3104             self.metaclass = None
3105             if keyword_args and not starstar_arg:
3106                 for i, item in list(enumerate(keyword_args.key_value_pairs))[::-1]:
3107                     if item.key.value == 'metaclass':
3108                         if self.metaclass is not None:
3109                             error(item.pos, "keyword argument 'metaclass' passed multiple times")
3110                         # special case: we already know the metaclass,
3111                         # so we don't need to do the "build kwargs,
3112                         # find metaclass" dance at runtime
3113                         self.metaclass = item.value
3114                         del keyword_args.key_value_pairs[i]
3115             if starstar_arg or (keyword_args and keyword_args.key_value_pairs):
3116                 self.mkw = ExprNodes.KeywordArgsNode(
3117                     pos, keyword_args = keyword_args, starstar_arg = starstar_arg)
3118             else:
3119                 self.mkw = ExprNodes.NullNode(pos)
3120             if self.metaclass is None:
3121                 self.metaclass = ExprNodes.PyClassMetaclassNode(
3122                     pos, mkw = self.mkw, bases = self.bases)
3123             self.dict = ExprNodes.PyClassNamespaceNode(pos, name = name,
3124                         doc = doc_node, metaclass = self.metaclass, bases = self.bases,
3125                         mkw = self.mkw)
3126             self.classobj = ExprNodes.Py3ClassNode(pos, name = name,
3127                     bases = self.bases, dict = self.dict, doc = doc_node,
3128                     metaclass = self.metaclass, mkw = self.mkw)
3129         else:
3130             self.dict = ExprNodes.DictNode(pos, key_value_pairs = [])
3131             self.metaclass = None
3132             self.mkw = None
3133             self.bases = None
3134             self.classobj = ExprNodes.ClassNode(pos, name = name,
3135                     bases = bases, dict = self.dict, doc = doc_node)
3136         self.target = ExprNodes.NameNode(pos, name = name)
3137         
3138     def as_cclass(self):
3139         """
3140         Return this node as if it were declared as an extension class
3141         """
3142         if self.py3_style_class:
3143             error(self.classobj.pos, "Python3 style class could not be represented as C class")
3144             return
3145         bases = self.classobj.bases.args
3146         if len(bases) == 0:
3147             base_class_name = None
3148             base_class_module = None
3149         elif len(bases) == 1:
3150             base = bases[0]
3151             path = []
3152             from ExprNodes import AttributeNode, NameNode
3153             while isinstance(base, AttributeNode):
3154                 path.insert(0, base.attribute)
3155                 base = base.obj
3156             if isinstance(base, NameNode):
3157                 path.insert(0, base.name)
3158                 base_class_name = path[-1]
3159                 if len(path) > 1:
3160                     base_class_module = u'.'.join(path[:-1])
3161                 else:
3162                     base_class_module = None
3163             else:
3164                 error(self.classobj.bases.args.pos, "Invalid base class")
3165         else:
3166             error(self.classobj.bases.args.pos, "C class may only have one base class")
3167             return None
3168         
3169         return CClassDefNode(self.pos, 
3170                              visibility = 'private',
3171                              module_name = None,
3172                              class_name = self.name,
3173                              base_class_module = base_class_module,
3174                              base_class_name = base_class_name,
3175                              decorators = self.decorators,
3176                              body = self.body,
3177                              in_pxd = False,
3178                              doc = self.doc)
3179         
3180     def create_scope(self, env):
3181         genv = env
3182         while genv.is_py_class_scope or genv.is_c_class_scope:
3183             genv = genv.outer_scope
3184         cenv = self.scope = PyClassScope(name = self.name, outer_scope = genv)
3185         return cenv
3186     
3187     def analyse_declarations(self, env):
3188         self.target.analyse_target_declaration(env)
3189         cenv = self.create_scope(env)
3190         cenv.directives = env.directives
3191         cenv.class_obj_cname = self.target.entry.cname
3192         self.body.analyse_declarations(cenv)
3193     
3194     def analyse_expressions(self, env):
3195         if self.py3_style_class:
3196             self.bases.analyse_expressions(env)
3197             self.metaclass.analyse_expressions(env)
3198             self.mkw.analyse_expressions(env)
3199         self.dict.analyse_expressions(env)
3200         self.classobj.analyse_expressions(env)
3201         genv = env.global_scope()
3202         cenv = self.scope
3203         self.body.analyse_expressions(cenv)
3204         self.target.analyse_target_expression(env, self.classobj)
3205     
3206     def generate_function_definitions(self, env, code):
3207         self.generate_lambda_definitions(self.scope, code)
3208         self.body.generate_function_definitions(self.scope, code)
3209     
3210     def generate_execution_code(self, code):
3211         code.pyclass_stack.append(self)
3212         cenv = self.scope
3213         if self.py3_style_class:
3214             self.bases.generate_evaluation_code(code)
3215             self.mkw.generate_evaluation_code(code)
3216             self.metaclass.generate_evaluation_code(code)
3217         self.dict.generate_evaluation_code(code)
3218         cenv.namespace_cname = cenv.class_obj_cname = self.dict.result()
3219         self.body.generate_execution_code(code)
3220         self.classobj.generate_evaluation_code(code)
3221         cenv.namespace_cname = cenv.class_obj_cname = self.classobj.result()
3222         self.target.generate_assignment_code(self.classobj, code)
3223         self.dict.generate_disposal_code(code)
3224         self.dict.free_temps(code)
3225         if self.py3_style_class:
3226             self.mkw.generate_disposal_code(code)
3227             self.mkw.free_temps(code)
3228             self.metaclass.generate_disposal_code(code)
3229             self.metaclass.free_temps(code)
3230             self.bases.generate_disposal_code(code)
3231             self.bases.free_temps(code)
3232         code.pyclass_stack.pop()
3233
3234 class CClassDefNode(ClassDefNode):
3235     #  An extension type definition.
3236     #
3237     #  visibility         'private' or 'public' or 'extern'
3238     #  typedef_flag       boolean
3239     #  api                boolean
3240     #  module_name        string or None    For import of extern type objects
3241     #  class_name         string            Unqualified name of class
3242     #  as_name            string or None    Name to declare as in this scope
3243     #  base_class_module  string or None    Module containing the base class
3244     #  base_class_name    string or None    Name of the base class
3245     #  objstruct_name     string or None    Specified C name of object struct
3246     #  typeobj_name       string or None    Specified C name of type object
3247     #  in_pxd             boolean           Is in a .pxd file
3248     #  decorators         [DecoratorNode]   list of decorators or None
3249     #  doc                string or None
3250     #  body               StatNode or None
3251     #  entry              Symtab.Entry
3252     #  base_type          PyExtensionType or None
3253     #  buffer_defaults_node DictNode or None Declares defaults for a buffer
3254     #  buffer_defaults_pos
3255
3256     child_attrs = ["body"]
3257     buffer_defaults_node = None
3258     buffer_defaults_pos = None
3259     typedef_flag = False
3260     api = False
3261     objstruct_name = None
3262     typeobj_name = None
3263     decorators = None
3264
3265     def analyse_declarations(self, env):
3266         #print "CClassDefNode.analyse_declarations:", self.class_name
3267         #print "...visibility =", self.visibility
3268         #print "...module_name =", self.module_name
3269
3270         import Buffer
3271         if self.buffer_defaults_node:
3272             buffer_defaults = Buffer.analyse_buffer_options(self.buffer_defaults_pos,
3273                                                             env, [], self.buffer_defaults_node,
3274                                                             need_complete=False)
3275         else:
3276             buffer_defaults = None
3277
3278         if env.in_cinclude and not self.objstruct_name:
3279             error(self.pos, "Object struct name specification required for "
3280                 "C class defined in 'extern from' block")
3281         self.base_type = None
3282         # Now that module imports are cached, we need to 
3283         # import the modules for extern classes. 
3284         if self.module_name:
3285             self.module = None
3286             for module in env.cimported_modules:
3287                 if module.name == self.module_name:
3288                     self.module = module
3289             if self.module is None:
3290                 self.module = ModuleScope(self.module_name, None, env.context)
3291                 self.module.has_extern_class = 1
3292                 env.add_imported_module(self.module)
3293
3294         if self.base_class_name:
3295             if self.base_class_module:
3296                 base_class_scope = env.find_module(self.base_class_module, self.pos)
3297             else:
3298                 base_class_scope = env
3299             if self.base_class_name == 'object':
3300                 # extension classes are special and don't need to inherit from object
3301                 if base_class_scope is None or base_class_scope.lookup('object') is None:
3302                     self.base_class_name = None
3303                     self.base_class_module = None
3304                     base_class_scope = None
3305             if base_class_scope:
3306                 base_class_entry = base_class_scope.find(self.base_class_name, self.pos)
3307                 if base_class_entry:
3308                     if not base_class_entry.is_type:
3309                         error(self.pos, "'%s' is not a type name" % self.base_class_name)
3310                     elif not base_class_entry.type.is_extension_type and \
3311                              not (base_class_entry.type.is_builtin_type and \
3312                                   base_class_entry.type.objstruct_cname):
3313                         error(self.pos, "'%s' is not an extension type" % self.base_class_name)
3314                     elif not base_class_entry.type.is_complete():
3315                         error(self.pos, "Base class '%s' of type '%s' is incomplete" % (
3316                             self.base_class_name, self.class_name))
3317                     elif base_class_entry.type.scope and base_class_entry.type.scope.directives and \
3318                              base_class_entry.type.scope.directives['final']:
3319                         error(self.pos, "Base class '%s' of type '%s' is final" % (
3320                             self.base_class_name, self.class_name))
3321                     elif base_class_entry.type.is_builtin_type and \
3322                              base_class_entry.type.name in ('tuple', 'str', 'bytes'):
3323                         error(self.pos, "inheritance from PyVarObject types like '%s' is not currently supported"
3324                               % base_class_entry.type.name)
3325                     else:
3326                         self.base_type = base_class_entry.type
3327         has_body = self.body is not None
3328         if self.module_name and self.visibility != 'extern':
3329             module_path = self.module_name.split(".")
3330             home_scope = env.find_imported_module(module_path, self.pos)
3331             if not home_scope:
3332                 return
3333         else:
3334             home_scope = env
3335
3336         if self.visibility == 'extern':
3337             if self.module_name == '__builtin__' and self.class_name in Builtin.builtin_types:
3338                 warning(self.pos, "%s already a builtin Cython type" % self.class_name, 1)
3339
3340         self.entry = home_scope.declare_c_class(
3341             name = self.class_name, 
3342             pos = self.pos,
3343             defining = has_body and self.in_pxd,
3344             implementing = has_body and not self.in_pxd,
3345             module_name = self.module_name,
3346             base_type = self.base_type,
3347             objstruct_cname = self.objstruct_name,
3348             typeobj_cname = self.typeobj_name,
3349             visibility = self.visibility,
3350             typedef_flag = self.typedef_flag,
3351             api = self.api,
3352             buffer_defaults = buffer_defaults)
3353         if home_scope is not env and self.visibility == 'extern':
3354             env.add_imported_entry(self.class_name, self.entry, self.pos)
3355         self.scope = scope = self.entry.type.scope
3356         if scope is not None:
3357             scope.directives = env.directives
3358
3359         if self.doc and Options.docstrings:
3360             scope.doc = embed_position(self.pos, self.doc)
3361             
3362         if has_body:
3363             self.body.analyse_declarations(scope)
3364             if self.in_pxd:
3365                 scope.defined = 1
3366             else:
3367                 scope.implemented = 1
3368         env.allocate_vtable_names(self.entry)
3369         
3370     def analyse_expressions(self, env):
3371         if self.body:
3372             scope = self.entry.type.scope
3373             self.body.analyse_expressions(scope)
3374     
3375     def generate_function_definitions(self, env, code):
3376         if self.body:
3377             self.generate_lambda_definitions(self.scope, code)
3378             self.body.generate_function_definitions(self.scope, code)
3379     
3380     def generate_execution_code(self, code):
3381         # This is needed to generate evaluation code for
3382         # default values of method arguments.
3383         if self.body:
3384             self.body.generate_execution_code(code)
3385             
3386     def annotate(self, code):
3387         if self.body:
3388             self.body.annotate(code)
3389
3390
3391 class PropertyNode(StatNode):
3392     #  Definition of a property in an extension type.
3393     #
3394     #  name   string
3395     #  doc    EncodedString or None    Doc string
3396     #  body   StatListNode
3397     
3398     child_attrs = ["body"]
3399
3400     def analyse_declarations(self, env):
3401         entry = env.declare_property(self.name, self.doc, self.pos)
3402         if entry:
3403             entry.scope.directives = env.directives
3404             self.body.analyse_declarations(entry.scope)
3405
3406     def analyse_expressions(self, env):
3407         self.body.analyse_expressions(env)
3408     
3409     def generate_function_definitions(self, env, code):
3410         self.body.generate_function_definitions(env, code)
3411
3412     def generate_execution_code(self, code):
3413         pass
3414
3415     def annotate(self, code):
3416         self.body.annotate(code)
3417
3418
3419 class GlobalNode(StatNode):
3420     # Global variable declaration.
3421     #
3422     # names    [string]
3423     
3424     child_attrs = []
3425
3426     def analyse_declarations(self, env):
3427         for name in self.names:
3428             env.declare_global(name, self.pos)
3429
3430     def analyse_expressions(self, env):
3431         pass
3432     
3433     def generate_execution_code(self, code):
3434         pass
3435
3436
3437 class ExprStatNode(StatNode):
3438     #  Expression used as a statement.
3439     #
3440     #  expr   ExprNode
3441
3442     child_attrs = ["expr"]
3443     
3444     def analyse_declarations(self, env):
3445         import ExprNodes
3446         if isinstance(self.expr, ExprNodes.GeneralCallNode):
3447             func = self.expr.function.as_cython_attribute()
3448             if func == u'declare':
3449                 args, kwds = self.expr.explicit_args_kwds()
3450                 if len(args):
3451                     error(self.expr.pos, "Variable names must be specified.")
3452                 for var, type_node in kwds.key_value_pairs:
3453                     type = type_node.analyse_as_type(env)
3454                     if type is None:
3455                         error(type_node.pos, "Unknown type")
3456                     else:
3457                         env.declare_var(var.value, type, var.pos, is_cdef = True)
3458                 self.__class__ = PassStatNode
3459     
3460     def analyse_expressions(self, env):
3461         self.expr.result_is_used = False # hint that .result() may safely be left empty
3462         self.expr.analyse_expressions(env)
3463     
3464     def generate_execution_code(self, code):
3465         self.expr.generate_evaluation_code(code)
3466         if not self.expr.is_temp and self.expr.result():
3467             code.putln("%s;" % self.expr.result())
3468         self.expr.generate_disposal_code(code)
3469         self.expr.free_temps(code)
3470
3471     def generate_function_definitions(self, env, code):
3472         self.expr.generate_function_definitions(env, code)
3473
3474     def annotate(self, code):
3475         self.expr.annotate(code)
3476
3477
3478 class AssignmentNode(StatNode):
3479     #  Abstract base class for assignment nodes.
3480     #
3481     #  The analyse_expressions and generate_execution_code
3482     #  phases of assignments are split into two sub-phases
3483     #  each, to enable all the right hand sides of a
3484     #  parallel assignment to be evaluated before assigning
3485     #  to any of the left hand sides.
3486
3487     def analyse_expressions(self, env):
3488         self.analyse_types(env)
3489
3490 #       def analyse_expressions(self, env):
3491 #           self.analyse_expressions_1(env)
3492 #           self.analyse_expressions_2(env)
3493
3494     def generate_execution_code(self, code):
3495         self.generate_rhs_evaluation_code(code)
3496         self.generate_assignment_code(code)
3497         
3498
3499 class SingleAssignmentNode(AssignmentNode):
3500     #  The simplest case:
3501     #
3502     #    a = b
3503     #
3504     #  lhs      ExprNode      Left hand side
3505     #  rhs      ExprNode      Right hand side
3506     #  first    bool          Is this guaranteed the first assignment to lhs?
3507     
3508     child_attrs = ["lhs", "rhs"]
3509     first = False
3510     declaration_only = False
3511
3512     def analyse_declarations(self, env):
3513         import ExprNodes
3514         
3515         # handle declarations of the form x = cython.foo()
3516         if isinstance(self.rhs, ExprNodes.CallNode):
3517             func_name = self.rhs.function.as_cython_attribute()
3518             if func_name:
3519                 args, kwds = self.rhs.explicit_args_kwds()
3520                 
3521                 if func_name in ['declare', 'typedef']:
3522                     if len(args) > 2 or kwds is not None:
3523                         error(self.rhs.pos, "Can only declare one type at a time.")
3524                         return
3525                     type = args[0].analyse_as_type(env)
3526                     if type is None:
3527                         error(args[0].pos, "Unknown type")
3528                         return
3529                     lhs = self.lhs
3530                     if func_name == 'declare':
3531                         if isinstance(lhs, ExprNodes.NameNode):
3532                             vars = [(lhs.name, lhs.pos)]
3533                         elif isinstance(lhs, ExprNodes.TupleNode):
3534                             vars = [(var.name, var.pos) for var in lhs.args]
3535                         else:
3536                             error(lhs.pos, "Invalid declaration")
3537                             return
3538                         for var, pos in vars:
3539                             env.declare_var(var, type, pos, is_cdef = True)
3540                         if len(args) == 2:
3541                             # we have a value
3542                             self.rhs = args[1]
3543                         else:
3544                             self.declaration_only = True
3545                     else:
3546                         self.declaration_only = True
3547                         if not isinstance(lhs, ExprNodes.NameNode):
3548                             error(lhs.pos, "Invalid declaration.")
3549                         env.declare_typedef(lhs.name, type, self.pos, visibility='private')
3550                     
3551                 elif func_name in ['struct', 'union']:
3552                     self.declaration_only = True
3553                     if len(args) > 0 or kwds is None:
3554                         error(self.rhs.pos, "Struct or union members must be given by name.")
3555                         return
3556                     members = []
3557                     for member, type_node in kwds.key_value_pairs:
3558                         type = type_node.analyse_as_type(env)
3559                         if type is None:
3560                             error(type_node.pos, "Unknown type")
3561                         else:
3562                             members.append((member.value, type, member.pos))
3563                     if len(members) < len(kwds.key_value_pairs):
3564                         return
3565                     if not isinstance(self.lhs, ExprNodes.NameNode):
3566                         error(self.lhs.pos, "Invalid declaration.")
3567                     name = self.lhs.name
3568                     scope = StructOrUnionScope(name)
3569                     env.declare_struct_or_union(name, func_name, scope, False, self.rhs.pos)
3570                     for member, type, pos in members:
3571                         scope.declare_var(member, type, pos)
3572                     
3573         if self.declaration_only:
3574             return
3575         else:
3576             self.lhs.analyse_target_declaration(env)
3577     
3578     def analyse_types(self, env, use_temp = 0):
3579         self.rhs.analyse_types(env)
3580         self.lhs.analyse_target_types(env)
3581         self.lhs.gil_assignment_check(env)
3582         self.rhs = self.rhs.coerce_to(self.lhs.type, env)
3583         if use_temp:
3584             self.rhs = self.rhs.coerce_to_temp(env)
3585     
3586     def generate_rhs_evaluation_code(self, code):
3587         self.rhs.generate_evaluation_code(code)
3588     
3589     def generate_assignment_code(self, code):
3590         self.lhs.generate_assignment_code(self.rhs, code)
3591
3592     def generate_function_definitions(self, env, code):
3593         self.rhs.generate_function_definitions(env, code)
3594
3595     def annotate(self, code):
3596         self.lhs.annotate(code)
3597         self.rhs.annotate(code)
3598
3599
3600 class CascadedAssignmentNode(AssignmentNode):
3601     #  An assignment with multiple left hand sides:
3602     #
3603     #    a = b = c
3604     #
3605     #  lhs_list   [ExprNode]   Left hand sides
3606     #  rhs        ExprNode     Right hand sides
3607     #
3608     #  Used internally:
3609     #
3610     #  coerced_rhs_list   [ExprNode]   RHS coerced to type of each LHS
3611     
3612     child_attrs = ["lhs_list", "rhs", "coerced_rhs_list"]
3613     coerced_rhs_list = None
3614
3615     def analyse_declarations(self, env):
3616         for lhs in self.lhs_list:
3617             lhs.analyse_target_declaration(env)
3618     
3619     def analyse_types(self, env, use_temp = 0):
3620         self.rhs.analyse_types(env)
3621         if not self.rhs.is_simple():
3622             if use_temp:
3623                 self.rhs = self.rhs.coerce_to_temp(env)
3624             else:
3625                 self.rhs = self.rhs.coerce_to_simple(env)
3626         from ExprNodes import CloneNode
3627         self.coerced_rhs_list = []
3628         for lhs in self.lhs_list:
3629             lhs.analyse_target_types(env)
3630             lhs.gil_assignment_check(env)
3631             rhs = CloneNode(self.rhs)
3632             rhs = rhs.coerce_to(lhs.type, env)
3633             self.coerced_rhs_list.append(rhs)
3634
3635     def generate_rhs_evaluation_code(self, code):
3636         self.rhs.generate_evaluation_code(code)
3637     
3638     def generate_assignment_code(self, code):
3639         for i in range(len(self.lhs_list)):
3640             lhs = self.lhs_list[i]
3641             rhs = self.coerced_rhs_list[i]
3642             rhs.generate_evaluation_code(code)
3643             lhs.generate_assignment_code(rhs, code)
3644             # Assignment has disposed of the cloned RHS
3645         self.rhs.generate_disposal_code(code)
3646         self.rhs.free_temps(code)
3647
3648     def generate_function_definitions(self, env, code):
3649         self.rhs.generate_function_definitions(env, code)
3650
3651     def annotate(self, code):
3652         for i in range(len(self.lhs_list)):
3653             lhs = self.lhs_list[i].annotate(code)
3654             rhs = self.coerced_rhs_list[i].annotate(code)
3655         self.rhs.annotate(code)
3656         
3657
3658 class ParallelAssignmentNode(AssignmentNode):
3659     #  A combined packing/unpacking assignment:
3660     #
3661     #    a, b, c =  d, e, f
3662     #
3663     #  This has been rearranged by the parser into
3664     #
3665     #    a = d ; b = e ; c = f
3666     #
3667     #  but we must evaluate all the right hand sides
3668     #  before assigning to any of the left hand sides.
3669     #
3670     #  stats     [AssignmentNode]   The constituent assignments
3671     
3672     child_attrs = ["stats"]
3673
3674     def analyse_declarations(self, env):
3675         for stat in self.stats:
3676             stat.analyse_declarations(env)
3677     
3678     def analyse_expressions(self, env):
3679         for stat in self.stats:
3680             stat.analyse_types(env, use_temp = 1)
3681
3682 #    def analyse_expressions(self, env):
3683 #        for stat in self.stats:
3684 #            stat.analyse_expressions_1(env, use_temp = 1)
3685 #        for stat in self.stats:
3686 #            stat.analyse_expressions_2(env)
3687     
3688     def generate_execution_code(self, code):
3689         for stat in self.stats:
3690             stat.generate_rhs_evaluation_code(code)
3691         for stat in self.stats:
3692             stat.generate_assignment_code(code)
3693
3694     def generate_function_definitions(self, env, code):
3695         for stat in self.stats:
3696             stat.generate_function_definitions(env, code)
3697
3698     def annotate(self, code):
3699         for stat in self.stats:
3700             stat.annotate(code)
3701
3702
3703 class InPlaceAssignmentNode(AssignmentNode):
3704     #  An in place arithmetic operand:
3705     #
3706     #    a += b
3707     #    a -= b
3708     #    ...
3709     #
3710     #  lhs      ExprNode      Left hand side
3711     #  rhs      ExprNode      Right hand side
3712     #  op       char          one of "+-*/%^&|"
3713     #  dup     (ExprNode)     copy of lhs used for operation (auto-generated)
3714     #
3715     #  This code is a bit tricky because in order to obey Python 
3716     #  semantics the sub-expressions (e.g. indices) of the lhs must 
3717     #  not be evaluated twice. So we must re-use the values calculated 
3718     #  in evaluation phase for the assignment phase as well. 
3719     #  Fortunately, the type of the lhs node is fairly constrained 
3720     #  (it must be a NameNode, AttributeNode, or IndexNode).     
3721     
3722     child_attrs = ["lhs", "rhs"]
3723
3724     def analyse_declarations(self, env):
3725         self.lhs.analyse_target_declaration(env)
3726         
3727     def analyse_types(self, env):
3728         self.rhs.analyse_types(env)
3729         self.lhs.analyse_target_types(env)
3730
3731     def generate_execution_code(self, code):
3732         import ExprNodes
3733         self.rhs.generate_evaluation_code(code)
3734         self.lhs.generate_subexpr_evaluation_code(code)
3735         c_op = self.operator
3736         if c_op == "//":
3737             c_op = "/"
3738         elif c_op == "**":
3739             error(self.pos, "No C inplace power operator")
3740         if isinstance(self.lhs, ExprNodes.IndexNode) and self.lhs.is_buffer_access:
3741             if self.lhs.type.is_pyobject:
3742                 error(self.pos, "In-place operators not allowed on object buffers in this release.")
3743             if c_op in ('/', '%') and self.lhs.type.is_int and not code.directives['cdivision']:
3744                 error(self.pos, "In-place non-c divide operators not allowed on int buffers.")
3745             self.lhs.generate_buffer_setitem_code(self.rhs, code, c_op)
3746         else:
3747             # C++
3748             # TODO: make sure overload is declared
3749             code.putln("%s %s= %s;" % (self.lhs.result(), c_op, self.rhs.result()))
3750         self.lhs.generate_subexpr_disposal_code(code)
3751         self.lhs.free_subexpr_temps(code)
3752         self.rhs.generate_disposal_code(code)
3753         self.rhs.free_temps(code)
3754
3755     def annotate(self, code):
3756         self.lhs.annotate(code)
3757         self.rhs.annotate(code)
3758     
3759     def create_binop_node(self):
3760         import ExprNodes
3761         return ExprNodes.binop_node(self.pos, self.operator, self.lhs, self.rhs)
3762
3763
3764 class PrintStatNode(StatNode):
3765     #  print statement
3766     #
3767     #  arg_tuple         TupleNode
3768     #  stream            ExprNode or None (stdout)
3769     #  append_newline    boolean
3770
3771     child_attrs = ["arg_tuple", "stream"]
3772
3773     def analyse_expressions(self, env):
3774         if self.stream:
3775             self.stream.analyse_expressions(env)
3776             self.stream = self.stream.coerce_to_pyobject(env)
3777         self.arg_tuple.analyse_expressions(env)
3778         self.arg_tuple = self.arg_tuple.coerce_to_pyobject(env)
3779         env.use_utility_code(printing_utility_code)
3780         if len(self.arg_tuple.args) == 1 and self.append_newline:
3781             env.use_utility_code(printing_one_utility_code)
3782
3783     nogil_check = Node.gil_error
3784     gil_message = "Python print statement"
3785
3786     def generate_execution_code(self, code):
3787         if self.stream:
3788             self.stream.generate_evaluation_code(code)
3789             stream_result = self.stream.py_result()
3790         else:
3791             stream_result = '0'
3792         if len(self.arg_tuple.args) == 1 and self.append_newline:
3793             arg = self.arg_tuple.args[0]
3794             arg.generate_evaluation_code(code)
3795             
3796             code.putln(
3797                 "if (__Pyx_PrintOne(%s, %s) < 0) %s" % (
3798                     stream_result,
3799                     arg.py_result(),
3800                     code.error_goto(self.pos)))
3801             arg.generate_disposal_code(code)
3802             arg.free_temps(code)
3803         else:
3804             self.arg_tuple.generate_evaluation_code(code)
3805             code.putln(
3806                 "if (__Pyx_Print(%s, %s, %d) < 0) %s" % (
3807                     stream_result,
3808                     self.arg_tuple.py_result(),
3809                     self.append_newline,
3810                     code.error_goto(self.pos)))
3811             self.arg_tuple.generate_disposal_code(code)
3812             self.arg_tuple.free_temps(code)
3813
3814         if self.stream:
3815             self.stream.generate_disposal_code(code)
3816             self.stream.free_temps(code)
3817
3818     def generate_function_definitions(self, env, code):
3819         if self.stream:
3820             self.stream.generate_function_definitions(env, code)
3821         self.arg_tuple.generate_function_definitions(env, code)
3822
3823     def annotate(self, code):
3824         if self.stream:
3825             self.stream.annotate(code)
3826         self.arg_tuple.annotate(code)
3827
3828
3829 class ExecStatNode(StatNode):
3830     #  exec statement
3831     #
3832     #  args     [ExprNode]
3833
3834     child_attrs = ["args"]
3835
3836     def analyse_expressions(self, env):
3837         for i, arg in enumerate(self.args):
3838             arg.analyse_expressions(env)
3839             arg = arg.coerce_to_pyobject(env)
3840             self.args[i] = arg
3841         env.use_utility_code(Builtin.pyexec_utility_code)
3842
3843     nogil_check = Node.gil_error
3844     gil_message = "Python exec statement"
3845
3846     def generate_execution_code(self, code):
3847         args = []
3848         for arg in self.args:
3849             arg.generate_evaluation_code(code)
3850             args.append( arg.py_result() )
3851         args = tuple(args + ['0', '0'][:3-len(args)])
3852         temp_result = code.funcstate.allocate_temp(PyrexTypes.py_object_type, manage_ref=True)
3853         code.putln("%s = __Pyx_PyRun(%s, %s, %s);" % (
3854                 (temp_result,) + args))
3855         for arg in self.args:
3856             arg.generate_disposal_code(code)
3857             arg.free_temps(code)
3858         code.putln(
3859             code.error_goto_if_null(temp_result, self.pos))
3860         code.put_gotref(temp_result)
3861         code.put_decref_clear(temp_result, py_object_type)
3862         code.funcstate.release_temp(temp_result)
3863
3864     def annotate(self, code):
3865         for arg in self.args:
3866             arg.annotate(code)
3867
3868
3869 class DelStatNode(StatNode):
3870     #  del statement
3871     #
3872     #  args     [ExprNode]
3873     
3874     child_attrs = ["args"]
3875
3876     def analyse_declarations(self, env):
3877         for arg in self.args:
3878             arg.analyse_target_declaration(env)
3879     
3880     def analyse_expressions(self, env):
3881         for arg in self.args:
3882             arg.analyse_target_expression(env, None)
3883             if arg.type.is_pyobject:
3884                 pass
3885             elif arg.type.is_ptr and arg.type.base_type.is_cpp_class:
3886                 self.cpp_check(env)
3887             elif arg.type.is_cpp_class:
3888                 error(arg.pos, "Deletion of non-heap C++ object")
3889             else:
3890                 error(arg.pos, "Deletion of non-Python, non-C++ object")
3891             #arg.release_target_temp(env)
3892
3893     def nogil_check(self, env):
3894         for arg in self.args:
3895             if arg.type.is_pyobject:
3896                 self.gil_error()
3897
3898     gil_message = "Deleting Python object"
3899
3900     def generate_execution_code(self, code):
3901         for arg in self.args:
3902             if arg.type.is_pyobject:
3903                 arg.generate_deletion_code(code)
3904             elif arg.type.is_ptr and arg.type.base_type.is_cpp_class:
3905                 arg.generate_result_code(code)
3906                 code.putln("delete %s;" % arg.result())
3907             # else error reported earlier
3908
3909     def annotate(self, code):
3910         for arg in self.args:
3911             arg.annotate(code)
3912
3913
3914 class PassStatNode(StatNode):
3915     #  pass statement
3916
3917     child_attrs = []
3918     
3919     def analyse_expressions(self, env):
3920         pass
3921     
3922     def generate_execution_code(self, code):
3923         pass
3924
3925
3926 class BreakStatNode(StatNode):
3927
3928     child_attrs = []
3929
3930     def analyse_expressions(self, env):
3931         pass
3932     
3933     def generate_execution_code(self, code):
3934         if not code.break_label:
3935             error(self.pos, "break statement not inside loop")
3936         else:
3937             code.put_goto(code.break_label)
3938
3939
3940 class ContinueStatNode(StatNode):
3941
3942     child_attrs = []
3943
3944     def analyse_expressions(self, env):
3945         pass
3946     
3947     def generate_execution_code(self, code):
3948         if code.funcstate.in_try_finally:
3949             error(self.pos, "continue statement inside try of try...finally")
3950         elif not code.continue_label:
3951             error(self.pos, "continue statement not inside loop")
3952         else:
3953             code.put_goto(code.continue_label)
3954
3955
3956 class ReturnStatNode(StatNode):
3957     #  return statement
3958     #
3959     #  value         ExprNode or None
3960     #  return_type   PyrexType
3961     
3962     child_attrs = ["value"]
3963
3964     def analyse_expressions(self, env):
3965         return_type = env.return_type
3966         self.return_type = return_type
3967         if not return_type:
3968             error(self.pos, "Return not inside a function body")
3969             return
3970         if self.value:
3971             self.value.analyse_types(env)
3972             if return_type.is_void or return_type.is_returncode:
3973                 error(self.value.pos, 
3974                     "Return with value in void function")
3975             else:
3976                 self.value = self.value.coerce_to(env.return_type, env)
3977         else:
3978             if (not return_type.is_void
3979                 and not return_type.is_pyobject
3980                 and not return_type.is_returncode):
3981                     error(self.pos, "Return value required")
3982
3983     def nogil_check(self, env):
3984         if self.return_type.is_pyobject:
3985             self.gil_error()
3986
3987     gil_message = "Returning Python object"
3988
3989     def generate_execution_code(self, code):
3990         code.mark_pos(self.pos)
3991         if not self.return_type:
3992             # error reported earlier
3993             return
3994         if self.return_type.is_pyobject:
3995             code.put_xdecref(Naming.retval_cname,
3996                              self.return_type)
3997         if self.value:
3998             self.value.generate_evaluation_code(code)
3999             self.value.make_owned_reference(code)
4000             code.putln(
4001                 "%s = %s;" % (
4002                     Naming.retval_cname,
4003                     self.value.result_as(self.return_type)))
4004             self.value.generate_post_assignment_code(code)
4005             self.value.free_temps(code)
4006         else:
4007             if self.return_type.is_pyobject:
4008                 code.put_init_to_py_none(Naming.retval_cname, self.return_type)
4009             elif self.return_type.is_returncode:
4010                 code.putln(
4011                     "%s = %s;" % (
4012                         Naming.retval_cname,
4013                         self.return_type.default_value))
4014         for cname, type in code.funcstate.temps_holding_reference():
4015             code.put_decref_clear(cname, type)
4016         code.put_goto(code.return_label)
4017
4018     def generate_function_definitions(self, env, code):
4019         if self.value is not None:
4020             self.value.generate_function_definitions(env, code)
4021         
4022     def annotate(self, code):
4023         if self.value:
4024             self.value.annotate(code)
4025
4026
4027 class RaiseStatNode(StatNode):
4028     #  raise statement
4029     #
4030     #  exc_type    ExprNode or None
4031     #  exc_value   ExprNode or None
4032     #  exc_tb      ExprNode or None
4033     
4034     child_attrs = ["exc_type", "exc_value", "exc_tb"]
4035
4036     def analyse_expressions(self, env):
4037         if self.exc_type:
4038             self.exc_type.analyse_types(env)
4039             self.exc_type = self.exc_type.coerce_to_pyobject(env)
4040         if self.exc_value:
4041             self.exc_value.analyse_types(env)
4042             self.exc_value = self.exc_value.coerce_to_pyobject(env)
4043         if self.exc_tb:
4044             self.exc_tb.analyse_types(env)
4045             self.exc_tb = self.exc_tb.coerce_to_pyobject(env)
4046         env.use_utility_code(raise_utility_code)
4047
4048     nogil_check = Node.gil_error
4049     gil_message = "Raising exception"
4050
4051     def generate_execution_code(self, code):
4052         if self.exc_type:
4053             self.exc_type.generate_evaluation_code(code)
4054             type_code = self.exc_type.py_result()
4055         else:
4056             type_code = "0"
4057         if self.exc_value:
4058             self.exc_value.generate_evaluation_code(code)
4059             value_code = self.exc_value.py_result()
4060         else:
4061             value_code = "0"
4062         if self.exc_tb:
4063             self.exc_tb.generate_evaluation_code(code)
4064             tb_code = self.exc_tb.py_result()
4065         else:
4066             tb_code = "0"
4067         code.putln(
4068             "__Pyx_Raise(%s, %s, %s);" % (
4069                 type_code,
4070                 value_code,
4071                 tb_code))
4072         for obj in (self.exc_type, self.exc_value, self.exc_tb):
4073             if obj:
4074                 obj.generate_disposal_code(code)
4075                 obj.free_temps(code)
4076         code.putln(
4077             code.error_goto(self.pos))
4078
4079     def generate_function_definitions(self, env, code):
4080         if self.exc_type is not None:
4081             self.exc_type.generate_function_definitions(env, code)
4082         if self.exc_value is not None:
4083             self.exc_value.generate_function_definitions(env, code)
4084         if self.exc_tb is not None:
4085             self.exc_tb.generate_function_definitions(env, code)
4086
4087     def annotate(self, code):
4088         if self.exc_type:
4089             self.exc_type.annotate(code)
4090         if self.exc_value:
4091             self.exc_value.annotate(code)
4092         if self.exc_tb:
4093             self.exc_tb.annotate(code)
4094
4095
4096 class ReraiseStatNode(StatNode):
4097
4098     child_attrs = []
4099
4100     def analyse_expressions(self, env):
4101         env.use_utility_code(restore_exception_utility_code)
4102
4103     nogil_check = Node.gil_error
4104     gil_message = "Raising exception"
4105
4106     def generate_execution_code(self, code):
4107         vars = code.funcstate.exc_vars
4108         if vars:
4109             for varname in vars:
4110                 code.put_giveref(varname)
4111             code.putln("__Pyx_ErrRestore(%s, %s, %s);" % tuple(vars))
4112             for varname in vars:
4113                 code.put("%s = 0; " % varname)
4114             code.putln()
4115             code.putln(code.error_goto(self.pos))
4116         else:
4117             error(self.pos, "Reraise not inside except clause")
4118         
4119
4120 class AssertStatNode(StatNode):
4121     #  assert statement
4122     #
4123     #  cond    ExprNode
4124     #  value   ExprNode or None
4125     
4126     child_attrs = ["cond", "value"]
4127
4128     def analyse_expressions(self, env):
4129         self.cond = self.cond.analyse_boolean_expression(env)
4130         if self.value:
4131             self.value.analyse_types(env)
4132             self.value = self.value.coerce_to_pyobject(env)
4133
4134     nogil_check = Node.gil_error
4135     gil_message = "Raising exception"
4136     
4137     def generate_execution_code(self, code):
4138         code.putln("#ifndef CYTHON_WITHOUT_ASSERTIONS")
4139         self.cond.generate_evaluation_code(code)
4140         code.putln(
4141             "if (unlikely(!%s)) {" %
4142                 self.cond.result())
4143         if self.value:
4144             self.value.generate_evaluation_code(code)
4145             code.putln(
4146                 "PyErr_SetObject(PyExc_AssertionError, %s);" %
4147                     self.value.py_result())
4148             self.value.generate_disposal_code(code)
4149             self.value.free_temps(code)
4150         else:
4151             code.putln(
4152                 "PyErr_SetNone(PyExc_AssertionError);")
4153         code.putln(
4154                 code.error_goto(self.pos))
4155         code.putln(
4156             "}")
4157         self.cond.generate_disposal_code(code)
4158         self.cond.free_temps(code)
4159         code.putln("#endif")
4160
4161     def generate_function_definitions(self, env, code):
4162         self.cond.generate_function_definitions(env, code)
4163         if self.value is not None:
4164             self.value.generate_function_definitions(env, code)
4165
4166     def annotate(self, code):
4167         self.cond.annotate(code)
4168         if self.value:
4169             self.value.annotate(code)
4170
4171
4172 class IfStatNode(StatNode):
4173     #  if statement
4174     #
4175     #  if_clauses   [IfClauseNode]
4176     #  else_clause  StatNode or None
4177
4178     child_attrs = ["if_clauses", "else_clause"]
4179     
4180     def analyse_control_flow(self, env):
4181         env.start_branching(self.pos)
4182         for if_clause in self.if_clauses:
4183             if_clause.analyse_control_flow(env)
4184             env.next_branch(if_clause.end_pos())
4185         if self.else_clause:
4186             self.else_clause.analyse_control_flow(env)
4187         env.finish_branching(self.end_pos())
4188
4189     def analyse_declarations(self, env):
4190         for if_clause in self.if_clauses:
4191             if_clause.analyse_declarations(env)
4192         if self.else_clause:
4193             self.else_clause.analyse_declarations(env)
4194     
4195     def analyse_expressions(self, env):
4196         for if_clause in self.if_clauses:
4197             if_clause.analyse_expressions(env)
4198         if self.else_clause:
4199             self.else_clause.analyse_expressions(env)
4200
4201     def generate_execution_code(self, code):
4202         code.mark_pos(self.pos)
4203         end_label = code.new_label()
4204         for if_clause in self.if_clauses:
4205             if_clause.generate_execution_code(code, end_label)
4206         if self.else_clause:
4207             code.putln("/*else*/ {")
4208             self.else_clause.generate_execution_code(code)
4209             code.putln("}")
4210         code.put_label(end_label)
4211
4212     def generate_function_definitions(self, env, code):
4213         for clause in self.if_clauses:
4214             clause.generate_function_definitions(env, code)
4215         if self.else_clause is not None:
4216             self.else_clause.generate_function_definitions(env, code)
4217
4218     def annotate(self, code):
4219         for if_clause in self.if_clauses:
4220             if_clause.annotate(code)
4221         if self.else_clause:
4222             self.else_clause.annotate(code)
4223
4224
4225 class IfClauseNode(Node):
4226     #  if or elif clause in an if statement
4227     #
4228     #  condition   ExprNode
4229     #  body        StatNode
4230     
4231     child_attrs = ["condition", "body"]
4232
4233     def analyse_control_flow(self, env):
4234         self.body.analyse_control_flow(env)
4235         
4236     def analyse_declarations(self, env):
4237         self.body.analyse_declarations(env)
4238     
4239     def analyse_expressions(self, env):
4240         self.condition = \
4241             self.condition.analyse_temp_boolean_expression(env)
4242         self.body.analyse_expressions(env)
4243
4244     def get_constant_condition_result(self):
4245         if self.condition.has_constant_result():
4246             return bool(self.condition.constant_result)
4247         else:
4248             return None
4249
4250     def generate_execution_code(self, code, end_label):
4251         self.condition.generate_evaluation_code(code)
4252         code.putln(
4253             "if (%s) {" %
4254                 self.condition.result())
4255         self.condition.generate_disposal_code(code)
4256         self.condition.free_temps(code)
4257         self.body.generate_execution_code(code)
4258         code.put_goto(end_label)
4259         code.putln("}")
4260
4261     def generate_function_definitions(self, env, code):
4262         self.condition.generate_function_definitions(env, code)
4263         self.body.generate_function_definitions(env, code)
4264
4265     def annotate(self, code):
4266         self.condition.annotate(code)
4267         self.body.annotate(code)
4268         
4269
4270 class SwitchCaseNode(StatNode):
4271     # Generated in the optimization of an if-elif-else node
4272     #
4273     # conditions    [ExprNode]
4274     # body          StatNode
4275     
4276     child_attrs = ['conditions', 'body']
4277
4278     def generate_execution_code(self, code):
4279         for cond in self.conditions:
4280             code.mark_pos(cond.pos)
4281             cond.generate_evaluation_code(code)
4282             code.putln("case %s:" % cond.result())
4283         self.body.generate_execution_code(code)
4284         code.putln("break;")
4285
4286     def generate_function_definitions(self, env, code):
4287         for cond in self.conditions:
4288             cond.generate_function_definitions(env, code)
4289         self.body.generate_function_definitions(env, code)
4290         
4291     def annotate(self, code):
4292         for cond in self.conditions:
4293             cond.annotate(code)
4294         self.body.annotate(code)
4295
4296 class SwitchStatNode(StatNode):
4297     # Generated in the optimization of an if-elif-else node
4298     #
4299     # test          ExprNode
4300     # cases         [SwitchCaseNode]
4301     # else_clause   StatNode or None
4302     
4303     child_attrs = ['test', 'cases', 'else_clause']
4304     
4305     def generate_execution_code(self, code):
4306         self.test.generate_evaluation_code(code)
4307         code.putln("switch (%s) {" % self.test.result())
4308         for case in self.cases:
4309             case.generate_execution_code(code)
4310         if self.else_clause is not None:
4311             code.putln("default:")
4312             self.else_clause.generate_execution_code(code)
4313             code.putln("break;")
4314         code.putln("}")
4315
4316     def generate_function_definitions(self, env, code):
4317         self.test.generate_function_definitions(env, code)
4318         for case in self.cases:
4319             case.generate_function_definitions(env, code)
4320         if self.else_clause is not None:
4321             self.else_clause.generate_function_definitions(env, code)
4322
4323     def annotate(self, code):
4324         self.test.annotate(code)
4325         for case in self.cases:
4326             case.annotate(code)
4327         if self.else_clause is not None:
4328             self.else_clause.annotate(code)
4329             
4330 class LoopNode(object):
4331     
4332     def analyse_control_flow(self, env):
4333         env.start_branching(self.pos)
4334         self.body.analyse_control_flow(env)
4335         env.next_branch(self.body.end_pos())
4336         if self.else_clause:
4337             self.else_clause.analyse_control_flow(env)
4338         env.finish_branching(self.end_pos())
4339
4340     
4341 class WhileStatNode(LoopNode, StatNode):
4342     #  while statement
4343     #
4344     #  condition    ExprNode
4345     #  body         StatNode
4346     #  else_clause  StatNode
4347
4348     child_attrs = ["condition", "body", "else_clause"]
4349
4350     def analyse_declarations(self, env):
4351         self.body.analyse_declarations(env)
4352         if self.else_clause:
4353             self.else_clause.analyse_declarations(env)
4354     
4355     def analyse_expressions(self, env):
4356         self.condition = \
4357             self.condition.analyse_temp_boolean_expression(env)
4358         self.body.analyse_expressions(env)
4359         if self.else_clause:
4360             self.else_clause.analyse_expressions(env)
4361     
4362     def generate_execution_code(self, code):
4363         old_loop_labels = code.new_loop_labels()
4364         code.putln(
4365             "while (1) {")
4366         self.condition.generate_evaluation_code(code)
4367         self.condition.generate_disposal_code(code)
4368         code.putln(
4369             "if (!%s) break;" %
4370                 self.condition.result())
4371         self.condition.free_temps(code)
4372         self.body.generate_execution_code(code)
4373         code.put_label(code.continue_label)
4374         code.putln("}")
4375         break_label = code.break_label
4376         code.set_loop_labels(old_loop_labels)
4377         if self.else_clause:
4378             code.putln("/*else*/ {")
4379             self.else_clause.generate_execution_code(code)
4380             code.putln("}")
4381         code.put_label(break_label)
4382
4383     def generate_function_definitions(self, env, code):
4384         self.condition.generate_function_definitions(env, code)
4385         self.body.generate_function_definitions(env, code)
4386         if self.else_clause is not None:
4387             self.else_clause.generate_function_definitions(env, code)
4388
4389     def annotate(self, code):
4390         self.condition.annotate(code)
4391         self.body.annotate(code)
4392         if self.else_clause:
4393             self.else_clause.annotate(code)
4394
4395
4396 def ForStatNode(pos, **kw):
4397     if 'iterator' in kw:
4398         return ForInStatNode(pos, **kw)
4399     else:
4400         return ForFromStatNode(pos, **kw)
4401
4402 class ForInStatNode(LoopNode, StatNode):
4403     #  for statement
4404     #
4405     #  target        ExprNode
4406     #  iterator      IteratorNode
4407     #  body          StatNode
4408     #  else_clause   StatNode
4409     #  item          NextNode       used internally
4410     
4411     child_attrs = ["target", "iterator", "body", "else_clause"]
4412     item = None
4413     
4414     def analyse_declarations(self, env):
4415         self.target.analyse_target_declaration(env)
4416         self.body.analyse_declarations(env)
4417         if self.else_clause:
4418             self.else_clause.analyse_declarations(env)
4419
4420     def analyse_expressions(self, env):
4421         import ExprNodes
4422         self.target.analyse_target_types(env)
4423         self.iterator.analyse_expressions(env)
4424         self.item = ExprNodes.NextNode(self.iterator, env)
4425         self.item = self.item.coerce_to(self.target.type, env)
4426         self.body.analyse_expressions(env)
4427         if self.else_clause:
4428             self.else_clause.analyse_expressions(env)
4429
4430     def generate_execution_code(self, code):
4431         old_loop_labels = code.new_loop_labels()
4432         self.iterator.allocate_counter_temp(code)
4433         self.iterator.generate_evaluation_code(code)
4434         code.putln(
4435             "for (;;) {")
4436         self.item.generate_evaluation_code(code)
4437         self.target.generate_assignment_code(self.item, code)
4438         self.body.generate_execution_code(code)
4439         code.put_label(code.continue_label)
4440         code.putln(
4441             "}")
4442         break_label = code.break_label
4443         code.set_loop_labels(old_loop_labels)
4444
4445         if self.else_clause:
4446             # in nested loops, the 'else' block can contain a
4447             # 'continue' statement for the outer loop, but we may need
4448             # to generate cleanup code before taking that path, so we
4449             # intercept it here
4450             orig_continue_label = code.continue_label
4451             code.continue_label = code.new_label('outer_continue')
4452
4453             code.putln("/*else*/ {")
4454             self.else_clause.generate_execution_code(code)
4455             code.putln("}")
4456
4457             if code.label_used(code.continue_label):
4458                 code.put_goto(break_label)
4459                 code.put_label(code.continue_label)
4460                 self.iterator.generate_disposal_code(code)
4461                 code.put_goto(orig_continue_label)
4462             code.set_loop_labels(old_loop_labels)
4463
4464         if code.label_used(break_label):
4465             code.put_label(break_label)
4466         self.iterator.release_counter_temp(code)
4467         self.iterator.generate_disposal_code(code)
4468         self.iterator.free_temps(code)
4469
4470     def generate_function_definitions(self, env, code):
4471         self.target.generate_function_definitions(env, code)
4472         self.iterator.generate_function_definitions(env, code)
4473         self.body.generate_function_definitions(env, code)
4474         if self.else_clause is not None:
4475             self.else_clause.generate_function_definitions(env, code)
4476
4477     def annotate(self, code):
4478         self.target.annotate(code)
4479         self.iterator.annotate(code)
4480         self.body.annotate(code)
4481         if self.else_clause:
4482             self.else_clause.annotate(code)
4483         self.item.annotate(code)
4484
4485
4486 class ForFromStatNode(LoopNode, StatNode):
4487     #  for name from expr rel name rel expr
4488     #
4489     #  target        NameNode
4490     #  bound1        ExprNode
4491     #  relation1     string
4492     #  relation2     string
4493     #  bound2        ExprNode
4494     #  step          ExprNode or None
4495     #  body          StatNode
4496     #  else_clause   StatNode or None
4497     #
4498     #  Used internally:
4499     #
4500     #  from_range         bool
4501     #  is_py_target       bool
4502     #  loopvar_node       ExprNode (usually a NameNode or temp node)
4503     #  py_loopvar_node    PyTempNode or None
4504     child_attrs = ["target", "bound1", "bound2", "step", "body", "else_clause"]
4505
4506     is_py_target = False
4507     loopvar_node = None
4508     py_loopvar_node = None
4509     from_range = False
4510
4511     gil_message = "For-loop using object bounds or target"
4512
4513     def nogil_check(self, env):
4514         for x in (self.target, self.bound1, self.bound2):
4515             if x.type.is_pyobject:
4516                 self.gil_error()
4517
4518     def analyse_declarations(self, env):
4519         self.target.analyse_target_declaration(env)
4520         self.body.analyse_declarations(env)
4521         if self.else_clause:
4522             self.else_clause.analyse_declarations(env)
4523
4524     def analyse_expressions(self, env):
4525         import ExprNodes
4526         self.target.analyse_target_types(env)
4527         self.bound1.analyse_types(env)
4528         self.bound2.analyse_types(env)
4529         if self.step is not None:
4530             if isinstance(self.step, ExprNodes.UnaryMinusNode):
4531                 warning(self.step.pos, "Probable infinite loop in for-from-by statment. Consider switching the directions of the relations.", 2)
4532             self.step.analyse_types(env)
4533         
4534         target_type = self.target.type
4535         if self.target.type.is_numeric:
4536             loop_type = self.target.type
4537         else:
4538             loop_type = PyrexTypes.c_int_type
4539             if not self.bound1.type.is_pyobject:
4540                 loop_type = PyrexTypes.widest_numeric_type(loop_type, self.bound1.type)
4541             if not self.bound2.type.is_pyobject:
4542                 loop_type = PyrexTypes.widest_numeric_type(loop_type, self.bound2.type)
4543             if self.step is not None and not self.step.type.is_pyobject:
4544                 loop_type = PyrexTypes.widest_numeric_type(loop_type, self.step.type)
4545         self.bound1 = self.bound1.coerce_to(loop_type, env)
4546         self.bound2 = self.bound2.coerce_to(loop_type, env)
4547         if not self.bound2.is_literal:
4548             self.bound2 = self.bound2.coerce_to_temp(env)
4549         if self.step is not None:
4550             self.step = self.step.coerce_to(loop_type, env)            
4551             if not self.step.is_literal:
4552                 self.step = self.step.coerce_to_temp(env)
4553
4554         target_type = self.target.type
4555         if not (target_type.is_pyobject or target_type.is_numeric):
4556             error(self.target.pos,
4557                 "for-from loop variable must be c numeric type or Python object")
4558         if target_type.is_numeric:
4559             self.is_py_target = False
4560             if isinstance(self.target, ExprNodes.IndexNode) and self.target.is_buffer_access:
4561                 raise error(self.pos, "Buffer indexing not allowed as for loop target.")
4562             self.loopvar_node = self.target
4563             self.py_loopvar_node = None
4564         else:
4565             self.is_py_target = True
4566             c_loopvar_node = ExprNodes.TempNode(self.pos, loop_type, env)
4567             self.loopvar_node = c_loopvar_node
4568             self.py_loopvar_node = \
4569                 ExprNodes.CloneNode(c_loopvar_node).coerce_to_pyobject(env)
4570         self.body.analyse_expressions(env)
4571         if self.else_clause:
4572             self.else_clause.analyse_expressions(env)
4573             
4574     def generate_execution_code(self, code):
4575         old_loop_labels = code.new_loop_labels()
4576         from_range = self.from_range
4577         self.bound1.generate_evaluation_code(code)
4578         self.bound2.generate_evaluation_code(code)
4579         offset, incop = self.relation_table[self.relation1]
4580         if self.step is not None:
4581             self.step.generate_evaluation_code(code)
4582             step = self.step.result()
4583             incop = "%s=%s" % (incop[0], step)
4584         import ExprNodes
4585         if isinstance(self.loopvar_node, ExprNodes.TempNode):
4586             self.loopvar_node.allocate(code)
4587         if isinstance(self.py_loopvar_node, ExprNodes.TempNode):
4588             self.py_loopvar_node.allocate(code)
4589         if from_range:
4590             loopvar_name = code.funcstate.allocate_temp(self.target.type, False)
4591         else:
4592             loopvar_name = self.loopvar_node.result()
4593         code.putln(
4594             "for (%s = %s%s; %s %s %s; %s%s) {" % (
4595                 loopvar_name,
4596                 self.bound1.result(), offset,
4597                 loopvar_name, self.relation2, self.bound2.result(),
4598                 loopvar_name, incop))
4599         if self.py_loopvar_node:
4600             self.py_loopvar_node.generate_evaluation_code(code)
4601             self.target.generate_assignment_code(self.py_loopvar_node, code)
4602         elif from_range:
4603             code.putln("%s = %s;" % (
4604                             self.target.result(), loopvar_name))
4605         self.body.generate_execution_code(code)
4606         code.put_label(code.continue_label)
4607         if self.py_loopvar_node:
4608             # This mess is to make for..from loops with python targets behave 
4609             # exactly like those with C targets with regards to re-assignment 
4610             # of the loop variable. 
4611             import ExprNodes
4612             if self.target.entry.is_pyglobal:
4613                 # We know target is a NameNode, this is the only ugly case. 
4614                 target_node = ExprNodes.PyTempNode(self.target.pos, None)
4615                 target_node.allocate(code)
4616                 interned_cname = code.intern_identifier(self.target.entry.name)
4617                 code.globalstate.use_utility_code(ExprNodes.get_name_interned_utility_code)
4618                 code.putln("%s = __Pyx_GetName(%s, %s); %s" % (
4619                                 target_node.result(),
4620                                 Naming.module_cname, 
4621                                 interned_cname,
4622                                 code.error_goto_if_null(target_node.result(), self.target.pos)))
4623                 code.put_gotref(target_node.result())
4624             else:
4625                 target_node = self.target
4626             from_py_node = ExprNodes.CoerceFromPyTypeNode(self.loopvar_node.type, target_node, None)
4627             from_py_node.temp_code = loopvar_name
4628             from_py_node.generate_result_code(code)
4629             if self.target.entry.is_pyglobal:
4630                 code.put_decref(target_node.result(), target_node.type)
4631                 target_node.release(code)
4632         code.putln("}")
4633         if self.py_loopvar_node:
4634             # This is potentially wasteful, but we don't want the semantics to 
4635             # depend on whether or not the loop is a python type. 
4636             self.py_loopvar_node.generate_evaluation_code(code)
4637             self.target.generate_assignment_code(self.py_loopvar_node, code)
4638         if from_range:
4639             code.funcstate.release_temp(loopvar_name)
4640         break_label = code.break_label
4641         code.set_loop_labels(old_loop_labels)
4642         if self.else_clause:
4643             code.putln("/*else*/ {")
4644             self.else_clause.generate_execution_code(code)
4645             code.putln("}")
4646         code.put_label(break_label)
4647         self.bound1.generate_disposal_code(code)
4648         self.bound1.free_temps(code)
4649         self.bound2.generate_disposal_code(code)
4650         self.bound2.free_temps(code)
4651         if isinstance(self.loopvar_node, ExprNodes.TempNode):
4652             self.loopvar_node.release(code)
4653         if isinstance(self.py_loopvar_node, ExprNodes.TempNode):
4654             self.py_loopvar_node.release(code)
4655         if self.step is not None:
4656             self.step.generate_disposal_code(code)
4657             self.step.free_temps(code)
4658     
4659     relation_table = {
4660         # {relop : (initial offset, increment op)}
4661         '<=': ("",   "++"),
4662         '<' : ("+1", "++"),
4663         '>=': ("",   "--"),
4664         '>' : ("-1", "--")
4665     }
4666
4667     def generate_function_definitions(self, env, code):
4668         self.target.generate_function_definitions(env, code)
4669         self.bound1.generate_function_definitions(env, code)
4670         self.bound2.generate_function_definitions(env, code)
4671         if self.step is not None:
4672             self.step.generate_function_definitions(env, code)
4673         self.body.generate_function_definitions(env, code)
4674         if self.else_clause is not None:
4675             self.else_clause.generate_function_definitions(env, code)
4676     
4677     def annotate(self, code):
4678         self.target.annotate(code)
4679         self.bound1.annotate(code)
4680         self.bound2.annotate(code)
4681         if self.step:
4682             self.step.annotate(code)
4683         self.body.annotate(code)
4684         if self.else_clause:
4685             self.else_clause.annotate(code)
4686
4687
4688 class WithStatNode(StatNode):
4689     """
4690     Represents a Python with statement.
4691     
4692     This is only used at parse tree level; and is not present in
4693     analysis or generation phases.
4694     """
4695     #  manager          The with statement manager object
4696     #  target            Node (lhs expression)
4697     #  body             StatNode
4698     child_attrs = ["manager", "target", "body"]
4699
4700 class TryExceptStatNode(StatNode):
4701     #  try .. except statement
4702     #
4703     #  body             StatNode
4704     #  except_clauses   [ExceptClauseNode]
4705     #  else_clause      StatNode or None
4706
4707     child_attrs = ["body", "except_clauses", "else_clause"]
4708     
4709     def analyse_control_flow(self, env):
4710         env.start_branching(self.pos)
4711         self.body.analyse_control_flow(env)
4712         successful_try = env.control_flow # grab this for later
4713         env.next_branch(self.body.end_pos())
4714         env.finish_branching(self.body.end_pos())
4715         
4716         env.start_branching(self.except_clauses[0].pos)
4717         for except_clause in self.except_clauses:
4718             except_clause.analyse_control_flow(env)
4719             env.next_branch(except_clause.end_pos())
4720             
4721         # the else cause it executed only when the try clause finishes
4722         env.control_flow.incoming = successful_try
4723         if self.else_clause:
4724             self.else_clause.analyse_control_flow(env)
4725         env.finish_branching(self.end_pos())
4726
4727     def analyse_declarations(self, env):
4728         self.body.analyse_declarations(env)
4729         for except_clause in self.except_clauses:
4730             except_clause.analyse_declarations(env)
4731         if self.else_clause:
4732             self.else_clause.analyse_declarations(env)
4733         env.use_utility_code(reset_exception_utility_code)
4734
4735     def analyse_expressions(self, env):
4736         self.body.analyse_expressions(env)
4737         default_clause_seen = 0
4738         for except_clause in self.except_clauses:
4739             except_clause.analyse_expressions(env)
4740             if default_clause_seen:
4741                 error(except_clause.pos, "default 'except:' must be last")
4742             if not except_clause.pattern:
4743                 default_clause_seen = 1
4744         self.has_default_clause = default_clause_seen
4745         if self.else_clause:
4746             self.else_clause.analyse_expressions(env)
4747
4748     nogil_check = Node.gil_error
4749     gil_message = "Try-except statement"
4750
4751     def generate_execution_code(self, code):
4752         old_return_label = code.return_label
4753         old_break_label = code.break_label
4754         old_continue_label = code.continue_label
4755         old_error_label = code.new_error_label()
4756         our_error_label = code.error_label
4757         except_end_label = code.new_label('exception_handled')
4758         except_error_label = code.new_label('except_error')
4759         except_return_label = code.new_label('except_return')
4760         try_return_label = code.new_label('try_return')
4761         try_break_label = code.new_label('try_break')
4762         try_continue_label = code.new_label('try_continue')
4763         try_end_label = code.new_label('try_end')
4764
4765         exc_save_vars = [code.funcstate.allocate_temp(py_object_type, False)
4766                          for i in xrange(3)]
4767         code.putln("{")
4768         code.putln("__Pyx_ExceptionSave(%s);" %
4769                    ', '.join(['&%s' % var for var in exc_save_vars]))
4770         for var in exc_save_vars:
4771             code.put_xgotref(var)
4772         code.putln(
4773             "/*try:*/ {")
4774         code.return_label = try_return_label
4775         code.break_label = try_break_label
4776         code.continue_label = try_continue_label
4777         self.body.generate_execution_code(code)
4778         code.putln(
4779             "}")
4780         temps_to_clean_up = code.funcstate.all_free_managed_temps()
4781         code.error_label = except_error_label
4782         code.return_label = except_return_label
4783         if self.else_clause:
4784             code.putln(
4785                 "/*else:*/ {")
4786             self.else_clause.generate_execution_code(code)
4787             code.putln(
4788                 "}")
4789         for var in exc_save_vars:
4790             code.put_xdecref_clear(var, py_object_type)
4791         code.put_goto(try_end_label)
4792         if code.label_used(try_return_label):
4793             code.put_label(try_return_label)
4794             for var in exc_save_vars: code.put_xgiveref(var)
4795             code.putln("__Pyx_ExceptionReset(%s);" %
4796                        ', '.join(exc_save_vars))
4797             code.put_goto(old_return_label)
4798         code.put_label(our_error_label)
4799         for temp_name, type in temps_to_clean_up:
4800             code.put_xdecref_clear(temp_name, type)
4801         for except_clause in self.except_clauses:
4802             except_clause.generate_handling_code(code, except_end_label)
4803
4804         error_label_used = code.label_used(except_error_label)
4805         if error_label_used or not self.has_default_clause:
4806             if error_label_used:
4807                 code.put_label(except_error_label)
4808             for var in exc_save_vars: code.put_xgiveref(var)
4809             code.putln("__Pyx_ExceptionReset(%s);" %
4810                        ', '.join(exc_save_vars))
4811             code.put_goto(old_error_label)
4812
4813         for exit_label, old_label in zip(
4814             [try_break_label, try_continue_label, except_return_label],
4815             [old_break_label, old_continue_label, old_return_label]):
4816
4817             if code.label_used(exit_label):
4818                 code.put_label(exit_label)
4819                 for var in exc_save_vars: code.put_xgiveref(var)
4820                 code.putln("__Pyx_ExceptionReset(%s);" %
4821                            ', '.join(exc_save_vars))
4822                 code.put_goto(old_label)
4823
4824         if code.label_used(except_end_label):
4825             code.put_label(except_end_label)
4826             for var in exc_save_vars: code.put_xgiveref(var)
4827             code.putln("__Pyx_ExceptionReset(%s);" %
4828                        ', '.join(exc_save_vars))
4829         code.put_label(try_end_label)
4830         code.putln("}")
4831
4832         for cname in exc_save_vars:
4833             code.funcstate.release_temp(cname)
4834
4835         code.return_label = old_return_label
4836         code.break_label = old_break_label
4837         code.continue_label = old_continue_label
4838         code.error_label = old_error_label
4839
4840     def generate_function_definitions(self, env, code):
4841         self.body.generate_function_definitions(env, code)
4842         for except_clause in self.except_clauses:
4843             except_clause.generate_function_definitions(env, code)
4844         if self.else_clause is not None:
4845             self.else_clause.generate_function_definitions(env, code)
4846
4847     def annotate(self, code):
4848         self.body.annotate(code)
4849         for except_node in self.except_clauses:
4850             except_node.annotate(code)
4851         if self.else_clause:
4852             self.else_clause.annotate(code)
4853
4854
4855 class ExceptClauseNode(Node):
4856     #  Part of try ... except statement.
4857     #
4858     #  pattern        [ExprNode]
4859     #  target         ExprNode or None
4860     #  body           StatNode
4861     #  excinfo_target NameNode or None   optional target for exception info
4862     #  match_flag     string             result of exception match
4863     #  exc_value      ExcValueNode       used internally
4864     #  function_name  string             qualified name of enclosing function
4865     #  exc_vars       (string * 3)       local exception variables
4866
4867     # excinfo_target is never set by the parser, but can be set by a transform
4868     # in order to extract more extensive information about the exception as a
4869     # sys.exc_info()-style tuple into a target variable
4870     
4871     child_attrs = ["pattern", "target", "body", "exc_value", "excinfo_target"]
4872
4873     exc_value = None
4874     excinfo_target = None
4875
4876     def analyse_declarations(self, env):
4877         if self.target:
4878             self.target.analyse_target_declaration(env)
4879         if self.excinfo_target is not None:
4880             self.excinfo_target.analyse_target_declaration(env)
4881         self.body.analyse_declarations(env)
4882     
4883     def analyse_expressions(self, env):
4884         import ExprNodes
4885         genv = env.global_scope()
4886         self.function_name = env.qualified_name
4887         if self.pattern:
4888             # normalise/unpack self.pattern into a list
4889             for i, pattern in enumerate(self.pattern):
4890                 pattern.analyse_expressions(env)
4891                 self.pattern[i] = pattern.coerce_to_pyobject(env)
4892
4893         if self.target:
4894             self.exc_value = ExprNodes.ExcValueNode(self.pos, env)
4895             self.target.analyse_target_expression(env, self.exc_value)
4896         if self.excinfo_target is not None:
4897             import ExprNodes
4898             self.excinfo_tuple = ExprNodes.TupleNode(pos=self.pos, args=[
4899                 ExprNodes.ExcValueNode(pos=self.pos, env=env) for x in range(3)])
4900             self.excinfo_tuple.analyse_expressions(env)
4901             self.excinfo_target.analyse_target_expression(env, self.excinfo_tuple)
4902
4903         self.body.analyse_expressions(env)
4904
4905     def generate_handling_code(self, code, end_label):
4906         code.mark_pos(self.pos)
4907         if self.pattern:
4908             exc_tests = []
4909             for pattern in self.pattern:
4910                 pattern.generate_evaluation_code(code)
4911                 exc_tests.append("PyErr_ExceptionMatches(%s)" % pattern.py_result())
4912
4913             match_flag = code.funcstate.allocate_temp(PyrexTypes.c_int_type, False)
4914             code.putln(
4915                 "%s = %s;" % (match_flag, ' || '.join(exc_tests)))
4916             for pattern in self.pattern:
4917                 pattern.generate_disposal_code(code)
4918                 pattern.free_temps(code)
4919             code.putln(
4920                 "if (%s) {" %
4921                     match_flag)
4922             code.funcstate.release_temp(match_flag)
4923         else:
4924             code.putln("/*except:*/ {")
4925
4926         if not getattr(self.body, 'stats', True) and \
4927                 self.excinfo_target is None and self.target is None:
4928             # most simple case: no exception variable, empty body (pass)
4929             # => reset the exception state, done
4930             code.putln("PyErr_Restore(0,0,0);")
4931             code.put_goto(end_label)
4932             code.putln("}")
4933             return
4934         
4935         exc_vars = [code.funcstate.allocate_temp(py_object_type,
4936                                                  manage_ref=True)
4937                     for i in xrange(3)]
4938         code.putln('__Pyx_AddTraceback("%s");' % self.function_name)
4939         # We always have to fetch the exception value even if
4940         # there is no target, because this also normalises the 
4941         # exception and stores it in the thread state.
4942         code.globalstate.use_utility_code(get_exception_utility_code)
4943         exc_args = "&%s, &%s, &%s" % tuple(exc_vars)
4944         code.putln("if (__Pyx_GetException(%s) < 0) %s" % (exc_args,
4945             code.error_goto(self.pos)))
4946         for x in exc_vars:
4947             code.put_gotref(x)
4948         if self.target:
4949             self.exc_value.set_var(exc_vars[1])
4950             self.exc_value.generate_evaluation_code(code)
4951             self.target.generate_assignment_code(self.exc_value, code)
4952         if self.excinfo_target is not None:
4953             for tempvar, node in zip(exc_vars, self.excinfo_tuple.args):
4954                 node.set_var(tempvar)
4955             self.excinfo_tuple.generate_evaluation_code(code)
4956             self.excinfo_target.generate_assignment_code(self.excinfo_tuple, code)
4957
4958         old_break_label, old_continue_label = code.break_label, code.continue_label
4959         code.break_label = code.new_label('except_break')
4960         code.continue_label = code.new_label('except_continue')
4961
4962         old_exc_vars = code.funcstate.exc_vars
4963         code.funcstate.exc_vars = exc_vars
4964         self.body.generate_execution_code(code)
4965         code.funcstate.exc_vars = old_exc_vars
4966         for var in exc_vars:
4967             code.putln("__Pyx_DECREF(%s); %s = 0;" % (var, var))
4968         code.put_goto(end_label)
4969         
4970         if code.label_used(code.break_label):
4971             code.put_label(code.break_label)
4972             for var in exc_vars:
4973                 code.putln("__Pyx_DECREF(%s); %s = 0;" % (var, var))
4974             code.put_goto(old_break_label)
4975         code.break_label = old_break_label
4976
4977         if code.label_used(code.continue_label):
4978             code.put_label(code.continue_label)
4979             for var in exc_vars:
4980                 code.putln("__Pyx_DECREF(%s); %s = 0;" % (var, var))
4981             code.put_goto(old_continue_label)
4982         code.continue_label = old_continue_label
4983
4984         for temp in exc_vars:
4985             code.funcstate.release_temp(temp)
4986
4987         code.putln(
4988             "}")
4989
4990     def generate_function_definitions(self, env, code):
4991         if self.target is not None:
4992             self.target.generate_function_definitions(env, code)
4993         self.body.generate_function_definitions(env, code)
4994         
4995     def annotate(self, code):
4996         if self.pattern:
4997             for pattern in self.pattern:
4998                 pattern.annotate(code)
4999         if self.target:
5000             self.target.annotate(code)
5001         self.body.annotate(code)
5002
5003
5004 class TryFinallyStatNode(StatNode):
5005     #  try ... finally statement
5006     #
5007     #  body             StatNode
5008     #  finally_clause   StatNode
5009     #
5010     #  The plan is that we funnel all continue, break
5011     #  return and error gotos into the beginning of the
5012     #  finally block, setting a variable to remember which
5013     #  one we're doing. At the end of the finally block, we
5014     #  switch on the variable to figure out where to go.
5015     #  In addition, if we're doing an error, we save the
5016     #  exception on entry to the finally block and restore
5017     #  it on exit.
5018
5019     child_attrs = ["body", "finally_clause"]
5020     
5021     preserve_exception = 1
5022     
5023     disallow_continue_in_try_finally = 0
5024     # There doesn't seem to be any point in disallowing
5025     # continue in the try block, since we have no problem
5026     # handling it.
5027
5028     def create_analysed(pos, env, body, finally_clause):
5029         node = TryFinallyStatNode(pos, body=body, finally_clause=finally_clause)
5030         return node
5031     create_analysed = staticmethod(create_analysed)
5032     
5033     def analyse_control_flow(self, env):
5034         env.start_branching(self.pos)
5035         self.body.analyse_control_flow(env)
5036         env.next_branch(self.body.end_pos())
5037         env.finish_branching(self.body.end_pos())
5038         self.finally_clause.analyse_control_flow(env)
5039
5040     def analyse_declarations(self, env):
5041         self.body.analyse_declarations(env)
5042         self.finally_clause.analyse_declarations(env)
5043     
5044     def analyse_expressions(self, env):
5045         self.body.analyse_expressions(env)
5046         self.finally_clause.analyse_expressions(env)
5047
5048     nogil_check = Node.gil_error
5049     gil_message = "Try-finally statement"
5050
5051     def generate_execution_code(self, code):
5052         old_error_label = code.error_label
5053         old_labels = code.all_new_labels()
5054         new_labels = code.get_all_labels()
5055         new_error_label = code.error_label
5056         catch_label = code.new_label()
5057         code.putln(
5058             "/*try:*/ {")
5059         if self.disallow_continue_in_try_finally:
5060             was_in_try_finally = code.funcstate.in_try_finally
5061             code.funcstate.in_try_finally = 1
5062         self.body.generate_execution_code(code)
5063         if self.disallow_continue_in_try_finally:
5064             code.funcstate.in_try_finally = was_in_try_finally
5065         code.putln(
5066             "}")
5067         temps_to_clean_up = code.funcstate.all_free_managed_temps()
5068         code.mark_pos(self.finally_clause.pos)
5069         code.putln(
5070             "/*finally:*/ {")
5071         cases_used = []
5072         error_label_used = 0
5073         for i, new_label in enumerate(new_labels):
5074             if new_label in code.labels_used:
5075                 cases_used.append(i)
5076                 if new_label == new_error_label:
5077                     error_label_used = 1
5078                     error_label_case = i
5079         if cases_used:
5080             code.putln(
5081                     "int __pyx_why;")
5082             if error_label_used and self.preserve_exception:
5083                 code.putln(
5084                     "PyObject *%s, *%s, *%s;" % Naming.exc_vars)
5085                 code.putln(
5086                     "int %s;" % Naming.exc_lineno_name)
5087                 exc_var_init_zero = ''.join(["%s = 0; " % var for var in Naming.exc_vars])
5088                 exc_var_init_zero += '%s = 0;' % Naming.exc_lineno_name
5089                 code.putln(exc_var_init_zero)
5090             else:
5091                 exc_var_init_zero = None
5092             code.use_label(catch_label)
5093             code.putln(
5094                     "__pyx_why = 0; goto %s;" % catch_label)
5095             for i in cases_used:
5096                 new_label = new_labels[i]
5097                 #if new_label and new_label != "<try>":
5098                 if new_label == new_error_label and self.preserve_exception:
5099                     self.put_error_catcher(code, 
5100                         new_error_label, i+1, catch_label, temps_to_clean_up)
5101                 else:
5102                     code.put('%s: ' % new_label)
5103                     if exc_var_init_zero:
5104                         code.putln(exc_var_init_zero)
5105                     code.putln("__pyx_why = %s; goto %s;" % (
5106                             i+1,
5107                             catch_label))
5108             code.put_label(catch_label)
5109         code.set_all_labels(old_labels)
5110         if error_label_used:
5111             code.new_error_label()
5112             finally_error_label = code.error_label
5113         self.finally_clause.generate_execution_code(code)
5114         if error_label_used:
5115             if finally_error_label in code.labels_used and self.preserve_exception:
5116                 over_label = code.new_label()
5117                 code.put_goto(over_label);
5118                 code.put_label(finally_error_label)
5119                 code.putln("if (__pyx_why == %d) {" % (error_label_case + 1))
5120                 for var in Naming.exc_vars:
5121                     code.putln("Py_XDECREF(%s);" % var)
5122                 code.putln("}")
5123                 code.put_goto(old_error_label)
5124                 code.put_label(over_label)
5125             code.error_label = old_error_label
5126         if cases_used:
5127             code.putln(
5128                 "switch (__pyx_why) {")
5129             for i in cases_used:
5130                 old_label = old_labels[i]
5131                 if old_label == old_error_label and self.preserve_exception:
5132                     self.put_error_uncatcher(code, i+1, old_error_label)
5133                 else:
5134                     code.use_label(old_label)
5135                     code.putln(
5136                         "case %s: goto %s;" % (
5137                             i+1,
5138                             old_label))
5139             code.putln(
5140                 "}")
5141         code.putln(
5142             "}")
5143
5144     def generate_function_definitions(self, env, code):
5145         self.body.generate_function_definitions(env, code)
5146         self.finally_clause.generate_function_definitions(env, code)
5147
5148     def put_error_catcher(self, code, error_label, i, catch_label, temps_to_clean_up):
5149         code.globalstate.use_utility_code(restore_exception_utility_code)
5150         code.putln(
5151             "%s: {" %
5152                 error_label)
5153         code.putln(
5154                 "__pyx_why = %s;" %
5155                     i)
5156         for temp_name, type in temps_to_clean_up:
5157             code.put_xdecref_clear(temp_name, type)
5158         code.putln(
5159                 "__Pyx_ErrFetch(&%s, &%s, &%s);" %
5160                     Naming.exc_vars)
5161         code.putln(
5162                 "%s = %s;" % (
5163                     Naming.exc_lineno_name, Naming.lineno_cname))
5164         code.put_goto(catch_label)
5165         code.putln("}")
5166             
5167     def put_error_uncatcher(self, code, i, error_label):
5168         code.globalstate.use_utility_code(restore_exception_utility_code)
5169         code.putln(
5170             "case %s: {" %
5171                 i)
5172         code.putln(
5173                 "__Pyx_ErrRestore(%s, %s, %s);" %
5174                     Naming.exc_vars)
5175         code.putln(
5176                 "%s = %s;" % (
5177                     Naming.lineno_cname, Naming.exc_lineno_name))
5178         for var in Naming.exc_vars:
5179             code.putln(
5180                 "%s = 0;" %
5181                     var)
5182         code.put_goto(error_label)
5183         code.putln(
5184             "}")
5185
5186     def annotate(self, code):
5187         self.body.annotate(code)
5188         self.finally_clause.annotate(code)
5189
5190
5191 class GILStatNode(TryFinallyStatNode):
5192     #  'with gil' or 'with nogil' statement
5193     #
5194     #   state   string   'gil' or 'nogil'
5195
5196 #    child_attrs = []
5197     
5198     preserve_exception = 0
5199
5200     def __init__(self, pos, state, body):
5201         self.state = state
5202         TryFinallyStatNode.__init__(self, pos,
5203             body = body,
5204             finally_clause = GILExitNode(pos, state = state))
5205
5206     def analyse_expressions(self, env):
5207         env.use_utility_code(force_init_threads_utility_code)
5208         was_nogil = env.nogil
5209         env.nogil = 1
5210         TryFinallyStatNode.analyse_expressions(self, env)
5211         env.nogil = was_nogil
5212
5213     nogil_check = None
5214
5215     def generate_execution_code(self, code):
5216         code.mark_pos(self.pos)
5217         code.putln("{")
5218         if self.state == 'gil':
5219             code.putln("#ifdef WITH_THREAD")
5220             code.putln("PyGILState_STATE _save = PyGILState_Ensure();")
5221             code.putln("#endif")
5222         else:
5223             code.putln("#ifdef WITH_THREAD")
5224             code.putln("PyThreadState *_save;")
5225             code.putln("#endif")
5226             code.putln("Py_UNBLOCK_THREADS")
5227         TryFinallyStatNode.generate_execution_code(self, code)
5228         code.putln("}")
5229
5230
5231 class GILExitNode(StatNode):
5232     #  Used as the 'finally' block in a GILStatNode
5233     #
5234     #  state   string   'gil' or 'nogil'
5235
5236     child_attrs = []
5237
5238     def analyse_expressions(self, env):
5239         pass
5240
5241     def generate_execution_code(self, code):
5242         if self.state == 'gil':
5243             code.putln("#ifdef WITH_THREAD")
5244             code.putln("PyGILState_Release(_save);")
5245             code.putln("#endif")
5246         else:
5247             code.putln("Py_BLOCK_THREADS")
5248
5249
5250 class CImportStatNode(StatNode):
5251     #  cimport statement
5252     #
5253     #  module_name   string           Qualified name of module being imported
5254     #  as_name       string or None   Name specified in "as" clause, if any
5255
5256     child_attrs = []
5257     
5258     def analyse_declarations(self, env):
5259         if not env.is_module_scope:
5260             error(self.pos, "cimport only allowed at module level")
5261             return
5262         module_scope = env.find_module(self.module_name, self.pos)
5263         if "." in self.module_name:
5264             names = [EncodedString(name) for name in self.module_name.split(".")]
5265             top_name = names[0]
5266             top_module_scope = env.context.find_submodule(top_name)
5267             module_scope = top_module_scope
5268             for name in names[1:]:
5269                 submodule_scope = module_scope.find_submodule(name)
5270                 module_scope.declare_module(name, submodule_scope, self.pos)
5271                 module_scope = submodule_scope
5272             if self.as_name:
5273                 env.declare_module(self.as_name, module_scope, self.pos)
5274             else:
5275                 env.declare_module(top_name, top_module_scope, self.pos)
5276         else:
5277             name = self.as_name or self.module_name
5278             env.declare_module(name, module_scope, self.pos)
5279
5280     def analyse_expressions(self, env):
5281         pass
5282     
5283     def generate_execution_code(self, code):
5284         pass
5285     
5286
5287 class FromCImportStatNode(StatNode):
5288     #  from ... cimport statement
5289     #
5290     #  module_name     string                        Qualified name of module
5291     #  imported_names  [(pos, name, as_name, kind)]  Names to be imported
5292     
5293     child_attrs = []
5294
5295     def analyse_declarations(self, env):
5296         if not env.is_module_scope:
5297             error(self.pos, "cimport only allowed at module level")
5298             return
5299         module_scope = env.find_module(self.module_name, self.pos)
5300         env.add_imported_module(module_scope)
5301         for pos, name, as_name, kind in self.imported_names:
5302             if name == "*":
5303                 for local_name, entry in module_scope.entries.items():
5304                     env.add_imported_entry(local_name, entry, pos)
5305             else:
5306                 entry = module_scope.lookup(name)
5307                 if entry:
5308                     if kind and not self.declaration_matches(entry, kind):
5309                         entry.redeclared(pos)
5310                 else:
5311                     if kind == 'struct' or kind == 'union':
5312                         entry = module_scope.declare_struct_or_union(name,
5313                             kind = kind, scope = None, typedef_flag = 0, pos = pos)
5314                     elif kind == 'class':
5315                         entry = module_scope.declare_c_class(name, pos = pos,
5316                             module_name = self.module_name)
5317                     else:
5318                         submodule_scope = env.context.find_module(name, relative_to = module_scope, pos = self.pos)
5319                         if submodule_scope.parent_module is module_scope:
5320                             env.declare_module(as_name or name, submodule_scope, self.pos)
5321                         else:
5322                             error(pos, "Name '%s' not declared in module '%s'"
5323                                 % (name, self.module_name))
5324                         
5325                 if entry:
5326                     local_name = as_name or name
5327                     env.add_imported_entry(local_name, entry, pos)
5328     
5329     def declaration_matches(self, entry, kind):
5330         if not entry.is_type:
5331             return 0
5332         type = entry.type
5333         if kind == 'class':
5334             if not type.is_extension_type:
5335                 return 0
5336         else:
5337             if not type.is_struct_or_union:
5338                 return 0
5339             if kind != type.kind:
5340                 return 0
5341         return 1
5342
5343     def analyse_expressions(self, env):
5344         pass
5345     
5346     def generate_execution_code(self, code):
5347         pass
5348
5349
5350 class FromImportStatNode(StatNode):
5351     #  from ... import statement
5352     #
5353     #  module           ImportNode
5354     #  items            [(string, NameNode)]
5355     #  interned_items   [(string, NameNode, ExprNode)]
5356     #  item             PyTempNode            used internally
5357     #  import_star      boolean               used internally
5358
5359     child_attrs = ["module"]
5360     import_star = 0
5361     
5362     def analyse_declarations(self, env):
5363         for name, target in self.items:
5364             if name == "*":
5365                 if not env.is_module_scope:
5366                     error(self.pos, "import * only allowed at module level")
5367                     return
5368                 env.has_import_star = 1
5369                 self.import_star = 1
5370             else:
5371                 target.analyse_target_declaration(env)
5372     
5373     def analyse_expressions(self, env):
5374         import ExprNodes
5375         self.module.analyse_expressions(env)
5376         self.item = ExprNodes.RawCNameExprNode(self.pos, py_object_type)
5377         self.interned_items = []
5378         for name, target in self.items:
5379             if name == '*':
5380                 for _, entry in env.entries.items():
5381                     if not entry.is_type and entry.type.is_extension_type:
5382                         env.use_utility_code(ExprNodes.type_test_utility_code)
5383                         break
5384             else:
5385                 entry =  env.lookup(target.name)
5386                 # check whether or not entry is already cimported
5387                 if (entry.is_type and entry.type.name == name
5388                     and hasattr(entry.type, 'module_name')):
5389                     if entry.type.module_name == self.module.module_name.value:
5390                         # cimported with absolute name
5391                         continue
5392                     try:
5393                         # cimported with relative name
5394                         module = env.find_module(self.module.module_name.value,
5395                                                  pos=None)
5396                         if entry.type.module_name == module.qualified_name:
5397                             continue
5398                     except AttributeError:
5399                         pass 
5400                 target.analyse_target_expression(env, None)
5401                 if target.type is py_object_type:
5402                     coerced_item = None
5403                 else:
5404                     coerced_item = self.item.coerce_to(target.type, env)
5405                 self.interned_items.append((name, target, coerced_item))
5406     
5407     def generate_execution_code(self, code):
5408         self.module.generate_evaluation_code(code)
5409         if self.import_star:
5410             code.putln(
5411                 'if (%s(%s) < 0) %s;' % (
5412                     Naming.import_star,
5413                     self.module.py_result(),
5414                     code.error_goto(self.pos)))
5415         item_temp = code.funcstate.allocate_temp(py_object_type, manage_ref=True)
5416         self.item.set_cname(item_temp)
5417         for name, target, coerced_item in self.interned_items:
5418             cname = code.intern_identifier(name)
5419             code.putln(
5420                 '%s = PyObject_GetAttr(%s, %s); %s' % (
5421                     item_temp,
5422                     self.module.py_result(),
5423                     cname,
5424                     code.error_goto_if_null(item_temp, self.pos)))
5425             code.put_gotref(item_temp)
5426             if coerced_item is None:
5427                 target.generate_assignment_code(self.item, code)
5428             else:
5429                 coerced_item.allocate_temp_result(code)
5430                 coerced_item.generate_result_code(code)
5431                 target.generate_assignment_code(coerced_item, code)
5432             code.put_decref_clear(item_temp, py_object_type)
5433         code.funcstate.release_temp(item_temp)
5434         self.module.generate_disposal_code(code)
5435         self.module.free_temps(code)
5436
5437
5438
5439 #------------------------------------------------------------------------------------
5440 #
5441 #  Runtime support code
5442 #
5443 #------------------------------------------------------------------------------------
5444
5445 utility_function_predeclarations = \
5446 """
5447 /* inline attribute */
5448 #ifndef CYTHON_INLINE
5449   #if defined(__GNUC__)
5450     #define CYTHON_INLINE __inline__
5451   #elif defined(_MSC_VER)
5452     #define CYTHON_INLINE __inline
5453   #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
5454     #define CYTHON_INLINE inline
5455   #else
5456     #define CYTHON_INLINE 
5457   #endif
5458 #endif
5459
5460 /* unused attribute */
5461 #ifndef CYTHON_UNUSED
5462 # if defined(__GNUC__)
5463 #   if !(defined(__cplusplus)) || (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))
5464 #     define CYTHON_UNUSED __attribute__ ((__unused__)) 
5465 #   else
5466 #     define CYTHON_UNUSED
5467 #   endif
5468 # elif defined(__ICC) || defined(__INTEL_COMPILER)
5469 #   define CYTHON_UNUSED __attribute__ ((__unused__)) 
5470 # else
5471 #   define CYTHON_UNUSED 
5472 # endif
5473 #endif
5474
5475 typedef struct {PyObject **p; char *s; const long n; const char* encoding; const char is_unicode; const char is_str; const char intern; } __Pyx_StringTabEntry; /*proto*/
5476
5477 """
5478
5479 if Options.gcc_branch_hints:
5480     branch_prediction_macros = \
5481     """
5482 #ifdef __GNUC__
5483 /* Test for GCC > 2.95 */
5484 #if __GNUC__ > 2 || (__GNUC__ == 2 && (__GNUC_MINOR__ > 95)) 
5485 #define likely(x)   __builtin_expect(!!(x), 1)
5486 #define unlikely(x) __builtin_expect(!!(x), 0)
5487 #else /* __GNUC__ > 2 ... */
5488 #define likely(x)   (x)
5489 #define unlikely(x) (x)
5490 #endif /* __GNUC__ > 2 ... */
5491 #else /* __GNUC__ */
5492 #define likely(x)   (x)
5493 #define unlikely(x) (x)
5494 #endif /* __GNUC__ */
5495     """
5496 else:
5497     branch_prediction_macros = \
5498     """
5499 #define likely(x)   (x)
5500 #define unlikely(x) (x)
5501     """
5502
5503 #get_name_predeclaration = \
5504 #"static PyObject *__Pyx_GetName(PyObject *dict, char *name); /*proto*/"
5505
5506 #get_name_interned_predeclaration = \
5507 #"static PyObject *__Pyx_GetName(PyObject *dict, PyObject *name); /*proto*/"
5508
5509 #------------------------------------------------------------------------------------
5510
5511 printing_utility_code = UtilityCode(
5512 proto = """
5513 static int __Pyx_Print(PyObject*, PyObject *, int); /*proto*/
5514 #if PY_MAJOR_VERSION >= 3
5515 static PyObject* %s = 0;
5516 static PyObject* %s = 0;
5517 #endif
5518 """ % (Naming.print_function, Naming.print_function_kwargs),
5519 cleanup = """
5520 #if PY_MAJOR_VERSION >= 3
5521 Py_CLEAR(%s);
5522 Py_CLEAR(%s);
5523 #endif
5524 """ % (Naming.print_function, Naming.print_function_kwargs),
5525 impl = r"""
5526 #if PY_MAJOR_VERSION < 3
5527 static PyObject *__Pyx_GetStdout(void) {
5528     PyObject *f = PySys_GetObject((char *)"stdout");
5529     if (!f) {
5530         PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout");
5531     }
5532     return f;
5533 }
5534
5535 static int __Pyx_Print(PyObject* f, PyObject *arg_tuple, int newline) {
5536     PyObject* v;
5537     int i;
5538
5539     if (!f) {
5540         if (!(f = __Pyx_GetStdout()))
5541             return -1;
5542     }
5543     for (i=0; i < PyTuple_GET_SIZE(arg_tuple); i++) {
5544         if (PyFile_SoftSpace(f, 1)) {
5545             if (PyFile_WriteString(" ", f) < 0)
5546                 return -1;
5547         }
5548         v = PyTuple_GET_ITEM(arg_tuple, i);
5549         if (PyFile_WriteObject(v, f, Py_PRINT_RAW) < 0)
5550             return -1;
5551         if (PyString_Check(v)) {
5552             char *s = PyString_AsString(v);
5553             Py_ssize_t len = PyString_Size(v);
5554             if (len > 0 &&
5555                 isspace(Py_CHARMASK(s[len-1])) &&
5556                 s[len-1] != ' ')
5557                     PyFile_SoftSpace(f, 0);
5558         }
5559     }
5560     if (newline) {
5561         if (PyFile_WriteString("\n", f) < 0)
5562             return -1;
5563         PyFile_SoftSpace(f, 0);
5564     }
5565     return 0;
5566 }
5567
5568 #else /* Python 3 has a print function */
5569
5570 static int __Pyx_Print(PyObject* stream, PyObject *arg_tuple, int newline) {
5571     PyObject* kwargs = 0;
5572     PyObject* result = 0;
5573     PyObject* end_string;
5574     if (unlikely(!%(PRINT_FUNCTION)s)) {
5575         %(PRINT_FUNCTION)s = __Pyx_GetAttrString(%(BUILTINS)s, "print");
5576         if (!%(PRINT_FUNCTION)s)
5577             return -1;
5578     }
5579     if (stream) {
5580         kwargs = PyDict_New();
5581         if (unlikely(!kwargs))
5582             return -1;
5583         if (unlikely(PyDict_SetItemString(kwargs, "file", stream) < 0))
5584             goto bad;
5585         if (!newline) {
5586             end_string = PyUnicode_FromStringAndSize(" ", 1);
5587             if (unlikely(!end_string))
5588                 goto bad;
5589             if (PyDict_SetItemString(kwargs, "end", end_string) < 0) {
5590                 Py_DECREF(end_string);
5591                 goto bad;
5592             }
5593             Py_DECREF(end_string);
5594         }
5595     } else if (!newline) {
5596         if (unlikely(!%(PRINT_KWARGS)s)) {
5597             %(PRINT_KWARGS)s = PyDict_New();
5598             if (unlikely(!%(PRINT_KWARGS)s))
5599                 return -1;
5600             end_string = PyUnicode_FromStringAndSize(" ", 1);
5601             if (unlikely(!end_string))
5602                 return -1;
5603             if (PyDict_SetItemString(%(PRINT_KWARGS)s, "end", end_string) < 0) {
5604                 Py_DECREF(end_string);
5605                 return -1;
5606             }
5607             Py_DECREF(end_string);
5608         }
5609         kwargs = %(PRINT_KWARGS)s;
5610     }
5611     result = PyObject_Call(%(PRINT_FUNCTION)s, arg_tuple, kwargs);
5612     if (unlikely(kwargs) && (kwargs != %(PRINT_KWARGS)s))
5613         Py_DECREF(kwargs);
5614     if (!result)
5615         return -1;
5616     Py_DECREF(result);
5617     return 0;
5618 bad:
5619     if (kwargs != %(PRINT_KWARGS)s)
5620         Py_XDECREF(kwargs);
5621     return -1;
5622 }
5623
5624 #endif
5625 """ % {'BUILTINS'       : Naming.builtins_cname,
5626        'PRINT_FUNCTION' : Naming.print_function,
5627        'PRINT_KWARGS'   : Naming.print_function_kwargs}
5628 )
5629
5630
5631 printing_one_utility_code = UtilityCode(
5632 proto = """
5633 static int __Pyx_PrintOne(PyObject* stream, PyObject *o); /*proto*/
5634 """,
5635 impl = r"""
5636 #if PY_MAJOR_VERSION < 3
5637
5638 static int __Pyx_PrintOne(PyObject* f, PyObject *o) {
5639     if (!f) {
5640         if (!(f = __Pyx_GetStdout()))
5641             return -1;
5642     }
5643     if (PyFile_SoftSpace(f, 0)) {
5644         if (PyFile_WriteString(" ", f) < 0)
5645             return -1;
5646     }
5647     if (PyFile_WriteObject(o, f, Py_PRINT_RAW) < 0)
5648         return -1;
5649     if (PyFile_WriteString("\n", f) < 0)
5650         return -1;
5651     return 0;
5652     /* the line below is just to avoid compiler
5653      * compiler warnings about unused functions */
5654     return __Pyx_Print(f, NULL, 0);
5655 }
5656
5657 #else /* Python 3 has a print function */
5658
5659 static int __Pyx_PrintOne(PyObject* stream, PyObject *o) {
5660     int res;
5661     PyObject* arg_tuple = PyTuple_New(1);
5662     if (unlikely(!arg_tuple))
5663         return -1;
5664     Py_INCREF(o);
5665     PyTuple_SET_ITEM(arg_tuple, 0, o);
5666     res = __Pyx_Print(stream, arg_tuple, 1);
5667     Py_DECREF(arg_tuple);
5668     return res;
5669 }
5670
5671 #endif
5672 """,
5673 requires=[printing_utility_code])
5674
5675
5676
5677 #------------------------------------------------------------------------------------
5678
5679 # Exception raising code
5680 #
5681 # Exceptions are raised by __Pyx_Raise() and stored as plain
5682 # type/value/tb in PyThreadState->curexc_*.  When being caught by an
5683 # 'except' statement, curexc_* is moved over to exc_* by
5684 # __Pyx_GetException()
5685
5686 restore_exception_utility_code = UtilityCode(
5687 proto = """
5688 static CYTHON_INLINE void __Pyx_ErrRestore(PyObject *type, PyObject *value, PyObject *tb); /*proto*/
5689 static CYTHON_INLINE void __Pyx_ErrFetch(PyObject **type, PyObject **value, PyObject **tb); /*proto*/
5690 """,
5691 impl = """
5692 static CYTHON_INLINE void __Pyx_ErrRestore(PyObject *type, PyObject *value, PyObject *tb) {
5693     PyObject *tmp_type, *tmp_value, *tmp_tb;
5694     PyThreadState *tstate = PyThreadState_GET();
5695
5696     tmp_type = tstate->curexc_type;
5697     tmp_value = tstate->curexc_value;
5698     tmp_tb = tstate->curexc_traceback;
5699     tstate->curexc_type = type;
5700     tstate->curexc_value = value;
5701     tstate->curexc_traceback = tb;
5702     Py_XDECREF(tmp_type);
5703     Py_XDECREF(tmp_value);
5704     Py_XDECREF(tmp_tb);
5705 }
5706
5707 static CYTHON_INLINE void __Pyx_ErrFetch(PyObject **type, PyObject **value, PyObject **tb) {
5708     PyThreadState *tstate = PyThreadState_GET();
5709     *type = tstate->curexc_type;
5710     *value = tstate->curexc_value;
5711     *tb = tstate->curexc_traceback;
5712
5713     tstate->curexc_type = 0;
5714     tstate->curexc_value = 0;
5715     tstate->curexc_traceback = 0;
5716 }
5717
5718 """)
5719
5720 # The following function is based on do_raise() from ceval.c. There
5721 # are separate versions for Python2 and Python3 as exception handling
5722 # has changed quite a lot between the two versions.
5723
5724 raise_utility_code = UtilityCode(
5725 proto = """
5726 static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb); /*proto*/
5727 """,
5728 impl = """
5729 #if PY_MAJOR_VERSION < 3
5730 static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb) {
5731     Py_XINCREF(type);
5732     Py_XINCREF(value);
5733     Py_XINCREF(tb);
5734     /* First, check the traceback argument, replacing None with NULL. */
5735     if (tb == Py_None) {
5736         Py_DECREF(tb);
5737         tb = 0;
5738     }
5739     else if (tb != NULL && !PyTraceBack_Check(tb)) {
5740         PyErr_SetString(PyExc_TypeError,
5741             "raise: arg 3 must be a traceback or None");
5742         goto raise_error;
5743     }
5744     /* Next, replace a missing value with None */
5745     if (value == NULL) {
5746         value = Py_None;
5747         Py_INCREF(value);
5748     }
5749     #if PY_VERSION_HEX < 0x02050000
5750     if (!PyClass_Check(type))
5751     #else
5752     if (!PyType_Check(type))
5753     #endif
5754     {
5755         /* Raising an instance.  The value should be a dummy. */
5756         if (value != Py_None) {
5757             PyErr_SetString(PyExc_TypeError,
5758                 "instance exception may not have a separate value");
5759             goto raise_error;
5760         }
5761         /* Normalize to raise <class>, <instance> */
5762         Py_DECREF(value);
5763         value = type;
5764         #if PY_VERSION_HEX < 0x02050000
5765             if (PyInstance_Check(type)) {
5766                 type = (PyObject*) ((PyInstanceObject*)type)->in_class;
5767                 Py_INCREF(type);
5768             }
5769             else {
5770                 type = 0;
5771                 PyErr_SetString(PyExc_TypeError,
5772                     "raise: exception must be an old-style class or instance");
5773                 goto raise_error;
5774             }
5775         #else
5776             type = (PyObject*) Py_TYPE(type);
5777             Py_INCREF(type);
5778             if (!PyType_IsSubtype((PyTypeObject *)type, (PyTypeObject *)PyExc_BaseException)) {
5779                 PyErr_SetString(PyExc_TypeError,
5780                     "raise: exception class must be a subclass of BaseException");
5781                 goto raise_error;
5782             }
5783         #endif
5784     }
5785
5786     __Pyx_ErrRestore(type, value, tb);
5787     return;
5788 raise_error:
5789     Py_XDECREF(value);
5790     Py_XDECREF(type);
5791     Py_XDECREF(tb);
5792     return;
5793 }
5794
5795 #else /* Python 3+ */
5796
5797 static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb) {
5798     if (tb == Py_None) {
5799         tb = 0;
5800     } else if (tb && !PyTraceBack_Check(tb)) {
5801         PyErr_SetString(PyExc_TypeError,
5802             "raise: arg 3 must be a traceback or None");
5803         goto bad;
5804     }
5805     if (value == Py_None)
5806         value = 0;
5807
5808     if (PyExceptionInstance_Check(type)) {
5809         if (value) {
5810             PyErr_SetString(PyExc_TypeError,
5811                 "instance exception may not have a separate value");
5812             goto bad;
5813         }
5814         value = type;
5815         type = (PyObject*) Py_TYPE(value);
5816     } else if (!PyExceptionClass_Check(type)) {
5817         PyErr_SetString(PyExc_TypeError,
5818             "raise: exception class must be a subclass of BaseException");
5819         goto bad;
5820     }
5821
5822     PyErr_SetObject(type, value);
5823
5824     if (tb) {
5825         PyThreadState *tstate = PyThreadState_GET();
5826         PyObject* tmp_tb = tstate->curexc_traceback;
5827         if (tb != tmp_tb) {
5828             Py_INCREF(tb);
5829             tstate->curexc_traceback = tb;
5830             Py_XDECREF(tmp_tb);
5831         }
5832     }
5833
5834 bad:
5835     return;
5836 }
5837 #endif
5838 """,
5839 requires=[restore_exception_utility_code])
5840
5841 #------------------------------------------------------------------------------------
5842
5843 get_exception_utility_code = UtilityCode(
5844 proto = """
5845 static int __Pyx_GetException(PyObject **type, PyObject **value, PyObject **tb); /*proto*/
5846 """,
5847 impl = """
5848 static int __Pyx_GetException(PyObject **type, PyObject **value, PyObject **tb) {
5849     PyObject *local_type, *local_value, *local_tb;
5850     PyObject *tmp_type, *tmp_value, *tmp_tb;
5851     PyThreadState *tstate = PyThreadState_GET();
5852     local_type = tstate->curexc_type;
5853     local_value = tstate->curexc_value;
5854     local_tb = tstate->curexc_traceback;
5855     tstate->curexc_type = 0;
5856     tstate->curexc_value = 0;
5857     tstate->curexc_traceback = 0;
5858     PyErr_NormalizeException(&local_type, &local_value, &local_tb);
5859     if (unlikely(tstate->curexc_type))
5860         goto bad;
5861     #if PY_MAJOR_VERSION >= 3
5862     if (unlikely(PyException_SetTraceback(local_value, local_tb) < 0))
5863         goto bad;
5864     #endif
5865     *type = local_type;
5866     *value = local_value;
5867     *tb = local_tb;
5868     Py_INCREF(local_type);
5869     Py_INCREF(local_value);
5870     Py_INCREF(local_tb);
5871     tmp_type = tstate->exc_type;
5872     tmp_value = tstate->exc_value;
5873     tmp_tb = tstate->exc_traceback;
5874     tstate->exc_type = local_type;
5875     tstate->exc_value = local_value;
5876     tstate->exc_traceback = local_tb;
5877     /* Make sure tstate is in a consistent state when we XDECREF
5878        these objects (XDECREF may run arbitrary code). */
5879     Py_XDECREF(tmp_type);
5880     Py_XDECREF(tmp_value);
5881     Py_XDECREF(tmp_tb);
5882     return 0;
5883 bad:
5884     *type = 0;
5885     *value = 0;
5886     *tb = 0;
5887     Py_XDECREF(local_type);
5888     Py_XDECREF(local_value);
5889     Py_XDECREF(local_tb);
5890     return -1;
5891 }
5892
5893 """)
5894
5895 #------------------------------------------------------------------------------------
5896
5897 get_exception_tuple_utility_code = UtilityCode(proto="""
5898 static PyObject *__Pyx_GetExceptionTuple(void); /*proto*/
5899 """,
5900 # I doubt that calling __Pyx_GetException() here is correct as it moves
5901 # the exception from tstate->curexc_* to tstate->exc_*, which prevents
5902 # exception handlers later on from receiving it.
5903 impl = """
5904 static PyObject *__Pyx_GetExceptionTuple(void) {
5905     PyObject *type = NULL, *value = NULL, *tb = NULL;
5906     if (__Pyx_GetException(&type, &value, &tb) == 0) {
5907         PyObject* exc_info = PyTuple_New(3);
5908         if (exc_info) {
5909             Py_INCREF(type);
5910             Py_INCREF(value);
5911             Py_INCREF(tb);
5912             PyTuple_SET_ITEM(exc_info, 0, type);
5913             PyTuple_SET_ITEM(exc_info, 1, value);
5914             PyTuple_SET_ITEM(exc_info, 2, tb);
5915             return exc_info;
5916         }
5917     }
5918     return NULL;
5919 }
5920 """,
5921 requires=[get_exception_utility_code])
5922
5923 #------------------------------------------------------------------------------------
5924
5925 reset_exception_utility_code = UtilityCode(
5926 proto = """
5927 static CYTHON_INLINE void __Pyx_ExceptionSave(PyObject **type, PyObject **value, PyObject **tb); /*proto*/
5928 static void __Pyx_ExceptionReset(PyObject *type, PyObject *value, PyObject *tb); /*proto*/
5929 """,
5930 impl = """
5931 static CYTHON_INLINE void __Pyx_ExceptionSave(PyObject **type, PyObject **value, PyObject **tb) {
5932     PyThreadState *tstate = PyThreadState_GET();
5933     *type = tstate->exc_type;
5934     *value = tstate->exc_value;
5935     *tb = tstate->exc_traceback;
5936     Py_XINCREF(*type);
5937     Py_XINCREF(*value);
5938     Py_XINCREF(*tb);
5939 }
5940
5941 static void __Pyx_ExceptionReset(PyObject *type, PyObject *value, PyObject *tb) {
5942     PyObject *tmp_type, *tmp_value, *tmp_tb;
5943     PyThreadState *tstate = PyThreadState_GET();
5944     tmp_type = tstate->exc_type;
5945     tmp_value = tstate->exc_value;
5946     tmp_tb = tstate->exc_traceback;
5947     tstate->exc_type = type;
5948     tstate->exc_value = value;
5949     tstate->exc_traceback = tb;
5950     Py_XDECREF(tmp_type);
5951     Py_XDECREF(tmp_value);
5952     Py_XDECREF(tmp_tb);
5953 }
5954 """)
5955
5956 #------------------------------------------------------------------------------------
5957
5958 arg_type_test_utility_code = UtilityCode(
5959 proto = """
5960 static int __Pyx_ArgTypeTest(PyObject *obj, PyTypeObject *type, int none_allowed,
5961     const char *name, int exact); /*proto*/
5962 """,
5963 impl = """
5964 static int __Pyx_ArgTypeTest(PyObject *obj, PyTypeObject *type, int none_allowed,
5965     const char *name, int exact)
5966 {
5967     if (!type) {
5968         PyErr_Format(PyExc_SystemError, "Missing type object");
5969         return 0;
5970     }
5971     if (none_allowed && obj == Py_None) return 1;
5972     else if (exact) {
5973         if (Py_TYPE(obj) == type) return 1;
5974     }
5975     else {
5976         if (PyObject_TypeCheck(obj, type)) return 1;
5977     }
5978     PyErr_Format(PyExc_TypeError,
5979         "Argument '%s' has incorrect type (expected %s, got %s)",
5980         name, type->tp_name, Py_TYPE(obj)->tp_name);
5981     return 0;
5982 }
5983 """)
5984
5985 #------------------------------------------------------------------------------------
5986 #
5987 #  __Pyx_RaiseArgtupleInvalid raises the correct exception when too
5988 #  many or too few positional arguments were found.  This handles
5989 #  Py_ssize_t formatting correctly.
5990
5991 raise_argtuple_invalid_utility_code = UtilityCode(
5992 proto = """
5993 static void __Pyx_RaiseArgtupleInvalid(const char* func_name, int exact,
5994     Py_ssize_t num_min, Py_ssize_t num_max, Py_ssize_t num_found); /*proto*/
5995 """,
5996 impl = """
5997 static void __Pyx_RaiseArgtupleInvalid(
5998     const char* func_name,
5999     int exact,
6000     Py_ssize_t num_min,
6001     Py_ssize_t num_max,
6002     Py_ssize_t num_found)
6003 {
6004     Py_ssize_t num_expected;
6005     const char *number, *more_or_less;
6006
6007     if (num_found < num_min) {
6008         num_expected = num_min;
6009         more_or_less = "at least";
6010     } else {
6011         num_expected = num_max;
6012         more_or_less = "at most";
6013     }
6014     if (exact) {
6015         more_or_less = "exactly";
6016     }
6017     number = (num_expected == 1) ? "" : "s";
6018     PyErr_Format(PyExc_TypeError,
6019         #if PY_VERSION_HEX < 0x02050000
6020             "%s() takes %s %d positional argument%s (%d given)",
6021         #else
6022             "%s() takes %s %zd positional argument%s (%zd given)",
6023         #endif
6024         func_name, more_or_less, num_expected, number, num_found);
6025 }
6026 """)
6027
6028 raise_keyword_required_utility_code = UtilityCode(
6029 proto = """
6030 static CYTHON_INLINE void __Pyx_RaiseKeywordRequired(const char* func_name, PyObject* kw_name); /*proto*/
6031 """,
6032 impl = """
6033 static CYTHON_INLINE void __Pyx_RaiseKeywordRequired(
6034     const char* func_name,
6035     PyObject* kw_name)
6036 {
6037     PyErr_Format(PyExc_TypeError,
6038         #if PY_MAJOR_VERSION >= 3
6039         "%s() needs keyword-only argument %U", func_name, kw_name);
6040         #else
6041         "%s() needs keyword-only argument %s", func_name,
6042         PyString_AS_STRING(kw_name));
6043         #endif
6044 }
6045 """)
6046
6047 raise_double_keywords_utility_code = UtilityCode(
6048 proto = """
6049 static void __Pyx_RaiseDoubleKeywordsError(
6050     const char* func_name, PyObject* kw_name); /*proto*/
6051 """,
6052 impl = """
6053 static void __Pyx_RaiseDoubleKeywordsError(
6054     const char* func_name,
6055     PyObject* kw_name)
6056 {
6057     PyErr_Format(PyExc_TypeError,
6058         #if PY_MAJOR_VERSION >= 3
6059         "%s() got multiple values for keyword argument '%U'", func_name, kw_name);
6060         #else
6061         "%s() got multiple values for keyword argument '%s'", func_name,
6062         PyString_AS_STRING(kw_name));
6063         #endif
6064 }
6065 """)
6066
6067 #------------------------------------------------------------------------------------
6068 #
6069 #  __Pyx_CheckKeywordStrings raises an error if non-string keywords
6070 #  were passed to a function, or if any keywords were passed to a
6071 #  function that does not accept them.
6072
6073 keyword_string_check_utility_code = UtilityCode(
6074 proto = """
6075 static CYTHON_INLINE int __Pyx_CheckKeywordStrings(PyObject *kwdict,
6076     const char* function_name, int kw_allowed); /*proto*/
6077 """,
6078 impl = """
6079 static CYTHON_INLINE int __Pyx_CheckKeywordStrings(
6080     PyObject *kwdict,
6081     const char* function_name,
6082     int kw_allowed)
6083 {
6084     PyObject* key = 0;
6085     Py_ssize_t pos = 0;
6086     while (PyDict_Next(kwdict, &pos, &key, 0)) {
6087         #if PY_MAJOR_VERSION < 3
6088         if (unlikely(!PyString_CheckExact(key)) && unlikely(!PyString_Check(key)))
6089         #else
6090         if (unlikely(!PyUnicode_CheckExact(key)) && unlikely(!PyUnicode_Check(key)))
6091         #endif
6092             goto invalid_keyword_type;
6093     }
6094     if ((!kw_allowed) && unlikely(key))
6095         goto invalid_keyword;
6096     return 1;
6097 invalid_keyword_type:
6098     PyErr_Format(PyExc_TypeError,
6099         "%s() keywords must be strings", function_name);
6100     return 0;
6101 invalid_keyword:
6102     PyErr_Format(PyExc_TypeError,
6103     #if PY_MAJOR_VERSION < 3
6104         "%s() got an unexpected keyword argument '%s'",
6105         function_name, PyString_AsString(key));
6106     #else
6107         "%s() got an unexpected keyword argument '%U'",
6108         function_name, key);
6109     #endif
6110     return 0;
6111 }
6112 """)
6113
6114 #------------------------------------------------------------------------------------
6115 #
6116 #  __Pyx_ParseOptionalKeywords copies the optional/unknown keyword
6117 #  arguments from the kwds dict into kwds2.  If kwds2 is NULL, unknown
6118 #  keywords will raise an invalid keyword error.
6119 #
6120 #  Three kinds of errors are checked: 1) non-string keywords, 2)
6121 #  unexpected keywords and 3) overlap with positional arguments.
6122 #
6123 #  If num_posargs is greater 0, it denotes the number of positional
6124 #  arguments that were passed and that must therefore not appear
6125 #  amongst the keywords as well.
6126 #
6127 #  This method does not check for required keyword arguments.
6128 #
6129
6130 parse_keywords_utility_code = UtilityCode(
6131 proto = """
6132 static int __Pyx_ParseOptionalKeywords(PyObject *kwds, PyObject **argnames[], \
6133     PyObject *kwds2, PyObject *values[], Py_ssize_t num_pos_args, \
6134     const char* function_name); /*proto*/
6135 """,
6136 impl = """
6137 static int __Pyx_ParseOptionalKeywords(
6138     PyObject *kwds,
6139     PyObject **argnames[],
6140     PyObject *kwds2,
6141     PyObject *values[],
6142     Py_ssize_t num_pos_args,
6143     const char* function_name)
6144 {
6145     PyObject *key = 0, *value = 0;
6146     Py_ssize_t pos = 0;
6147     PyObject*** name;
6148     PyObject*** first_kw_arg = argnames + num_pos_args;
6149
6150     while (PyDict_Next(kwds, &pos, &key, &value)) {
6151         name = first_kw_arg;
6152         while (*name && (**name != key)) name++;
6153         if (*name) {
6154             values[name-argnames] = value;
6155         } else {
6156             #if PY_MAJOR_VERSION < 3
6157             if (unlikely(!PyString_CheckExact(key)) && unlikely(!PyString_Check(key))) {
6158             #else
6159             if (unlikely(!PyUnicode_CheckExact(key)) && unlikely(!PyUnicode_Check(key))) {
6160             #endif
6161                 goto invalid_keyword_type;
6162             } else {
6163                 for (name = first_kw_arg; *name; name++) {
6164                     #if PY_MAJOR_VERSION >= 3
6165                     if (PyUnicode_GET_SIZE(**name) == PyUnicode_GET_SIZE(key) &&
6166                         PyUnicode_Compare(**name, key) == 0) break;
6167                     #else
6168                     if (PyString_GET_SIZE(**name) == PyString_GET_SIZE(key) &&
6169                         _PyString_Eq(**name, key)) break;
6170                     #endif
6171                 }
6172                 if (*name) {
6173                     values[name-argnames] = value;
6174                 } else {
6175                     /* unexpected keyword found */
6176                     for (name=argnames; name != first_kw_arg; name++) {
6177                         if (**name == key) goto arg_passed_twice;
6178                         #if PY_MAJOR_VERSION >= 3
6179                         if (PyUnicode_GET_SIZE(**name) == PyUnicode_GET_SIZE(key) &&
6180                             PyUnicode_Compare(**name, key) == 0) goto arg_passed_twice;
6181                         #else
6182                         if (PyString_GET_SIZE(**name) == PyString_GET_SIZE(key) &&
6183                             _PyString_Eq(**name, key)) goto arg_passed_twice;
6184                         #endif
6185                     }
6186                     if (kwds2) {
6187                         if (unlikely(PyDict_SetItem(kwds2, key, value))) goto bad;
6188                     } else {
6189                         goto invalid_keyword;
6190                     }
6191                 }
6192             }
6193         }
6194     }
6195     return 0;
6196 arg_passed_twice:
6197     __Pyx_RaiseDoubleKeywordsError(function_name, **name);
6198     goto bad;
6199 invalid_keyword_type:
6200     PyErr_Format(PyExc_TypeError,
6201         "%s() keywords must be strings", function_name);
6202     goto bad;
6203 invalid_keyword:
6204     PyErr_Format(PyExc_TypeError,
6205     #if PY_MAJOR_VERSION < 3
6206         "%s() got an unexpected keyword argument '%s'",
6207         function_name, PyString_AsString(key));
6208     #else
6209         "%s() got an unexpected keyword argument '%U'",
6210         function_name, key);
6211     #endif
6212 bad:
6213     return -1;
6214 }
6215 """)
6216
6217 #------------------------------------------------------------------------------------
6218
6219 traceback_utility_code = UtilityCode(
6220 proto = """
6221 static void __Pyx_AddTraceback(const char *funcname); /*proto*/
6222 """,
6223 impl = """
6224 #include "compile.h"
6225 #include "frameobject.h"
6226 #include "traceback.h"
6227
6228 static void __Pyx_AddTraceback(const char *funcname) {
6229     PyObject *py_srcfile = 0;
6230     PyObject *py_funcname = 0;
6231     PyObject *py_globals = 0;
6232     PyCodeObject *py_code = 0;
6233     PyFrameObject *py_frame = 0;
6234
6235     #if PY_MAJOR_VERSION < 3
6236     py_srcfile = PyString_FromString(%(FILENAME)s);
6237     #else
6238     py_srcfile = PyUnicode_FromString(%(FILENAME)s);
6239     #endif
6240     if (!py_srcfile) goto bad;
6241     if (%(CLINENO)s) {
6242         #if PY_MAJOR_VERSION < 3
6243         py_funcname = PyString_FromFormat( "%%s (%%s:%%d)", funcname, %(CFILENAME)s, %(CLINENO)s);
6244         #else
6245         py_funcname = PyUnicode_FromFormat( "%%s (%%s:%%d)", funcname, %(CFILENAME)s, %(CLINENO)s);
6246         #endif
6247     }
6248     else {
6249         #if PY_MAJOR_VERSION < 3
6250         py_funcname = PyString_FromString(funcname);
6251         #else
6252         py_funcname = PyUnicode_FromString(funcname);
6253         #endif
6254     }
6255     if (!py_funcname) goto bad;
6256     py_globals = PyModule_GetDict(%(GLOBALS)s);
6257     if (!py_globals) goto bad;
6258     py_code = PyCode_New(
6259         0,            /*int argcount,*/
6260         #if PY_MAJOR_VERSION >= 3
6261         0,            /*int kwonlyargcount,*/
6262         #endif
6263         0,            /*int nlocals,*/
6264         0,            /*int stacksize,*/
6265         0,            /*int flags,*/
6266         %(EMPTY_BYTES)s, /*PyObject *code,*/
6267         %(EMPTY_TUPLE)s,  /*PyObject *consts,*/
6268         %(EMPTY_TUPLE)s,  /*PyObject *names,*/
6269         %(EMPTY_TUPLE)s,  /*PyObject *varnames,*/
6270         %(EMPTY_TUPLE)s,  /*PyObject *freevars,*/
6271         %(EMPTY_TUPLE)s,  /*PyObject *cellvars,*/
6272         py_srcfile,   /*PyObject *filename,*/
6273         py_funcname,  /*PyObject *name,*/
6274         %(LINENO)s,   /*int firstlineno,*/
6275         %(EMPTY_BYTES)s  /*PyObject *lnotab*/
6276     );
6277     if (!py_code) goto bad;
6278     py_frame = PyFrame_New(
6279         PyThreadState_GET(), /*PyThreadState *tstate,*/
6280         py_code,             /*PyCodeObject *code,*/
6281         py_globals,          /*PyObject *globals,*/
6282         0                    /*PyObject *locals*/
6283     );
6284     if (!py_frame) goto bad;
6285     py_frame->f_lineno = %(LINENO)s;
6286     PyTraceBack_Here(py_frame);
6287 bad:
6288     Py_XDECREF(py_srcfile);
6289     Py_XDECREF(py_funcname);
6290     Py_XDECREF(py_code);
6291     Py_XDECREF(py_frame);
6292 }
6293 """ % {
6294     'FILENAME': Naming.filename_cname,
6295     'LINENO':  Naming.lineno_cname,
6296     'CFILENAME': Naming.cfilenm_cname,
6297     'CLINENO':  Naming.clineno_cname,
6298     'GLOBALS': Naming.module_cname,
6299     'EMPTY_TUPLE' : Naming.empty_tuple,
6300     'EMPTY_BYTES' : Naming.empty_bytes,
6301 })
6302
6303 #------------------------------------------------------------------------------------
6304
6305 unraisable_exception_utility_code = UtilityCode(
6306 proto = """
6307 static void __Pyx_WriteUnraisable(const char *name); /*proto*/
6308 """,
6309 impl = """
6310 static void __Pyx_WriteUnraisable(const char *name) {
6311     PyObject *old_exc, *old_val, *old_tb;
6312     PyObject *ctx;
6313     __Pyx_ErrFetch(&old_exc, &old_val, &old_tb);
6314     #if PY_MAJOR_VERSION < 3
6315     ctx = PyString_FromString(name);
6316     #else
6317     ctx = PyUnicode_FromString(name);
6318     #endif
6319     __Pyx_ErrRestore(old_exc, old_val, old_tb);
6320     if (!ctx) {
6321         PyErr_WriteUnraisable(Py_None);
6322     } else {
6323         PyErr_WriteUnraisable(ctx);
6324         Py_DECREF(ctx);
6325     }
6326 }
6327 """,
6328 requires=[restore_exception_utility_code])
6329
6330 #------------------------------------------------------------------------------------
6331
6332 set_vtable_utility_code = UtilityCode(
6333 proto = """
6334 static int __Pyx_SetVtable(PyObject *dict, void *vtable); /*proto*/
6335 """,
6336 impl = """
6337 static int __Pyx_SetVtable(PyObject *dict, void *vtable) {
6338 #if PY_VERSION_HEX >= 0x02070000 && !(PY_MAJOR_VERSION==3&&PY_MINOR_VERSION==0)
6339     PyObject *ob = PyCapsule_New(vtable, 0, 0);
6340 #else
6341     PyObject *ob = PyCObject_FromVoidPtr(vtable, 0);
6342 #endif
6343     if (!ob)
6344         goto bad;
6345     if (PyDict_SetItemString(dict, "__pyx_vtable__", ob) < 0)
6346         goto bad;
6347     Py_DECREF(ob);
6348     return 0;
6349 bad:
6350     Py_XDECREF(ob);
6351     return -1;
6352 }
6353 """)
6354
6355 #------------------------------------------------------------------------------------
6356
6357 get_vtable_utility_code = UtilityCode(
6358 proto = """
6359 static int __Pyx_GetVtable(PyObject *dict, void *vtabptr); /*proto*/
6360 """,
6361 impl = r"""
6362 static int __Pyx_GetVtable(PyObject *dict, void *vtabptr) {
6363     PyObject *ob = PyMapping_GetItemString(dict, (char *)"__pyx_vtable__");
6364     if (!ob)
6365         goto bad;
6366 #if PY_VERSION_HEX >= 0x02070000 && !(PY_MAJOR_VERSION==3&&PY_MINOR_VERSION==0)
6367     *(void **)vtabptr = PyCapsule_GetPointer(ob, 0);
6368 #else
6369     *(void **)vtabptr = PyCObject_AsVoidPtr(ob);
6370 #endif
6371     if (!*(void **)vtabptr)
6372         goto bad;
6373     Py_DECREF(ob);
6374     return 0;
6375 bad:
6376     Py_XDECREF(ob);
6377     return -1;
6378 }
6379 """)
6380
6381 #------------------------------------------------------------------------------------
6382
6383 init_string_tab_utility_code = UtilityCode(
6384 proto = """
6385 static int __Pyx_InitStrings(__Pyx_StringTabEntry *t); /*proto*/
6386 """,
6387 impl = """
6388 static int __Pyx_InitStrings(__Pyx_StringTabEntry *t) {
6389     while (t->p) {
6390         #if PY_MAJOR_VERSION < 3
6391         if (t->is_unicode) {
6392             *t->p = PyUnicode_DecodeUTF8(t->s, t->n - 1, NULL);
6393         } else if (t->intern) {
6394             *t->p = PyString_InternFromString(t->s);
6395         } else {
6396             *t->p = PyString_FromStringAndSize(t->s, t->n - 1);
6397         }
6398         #else  /* Python 3+ has unicode identifiers */
6399         if (t->is_unicode | t->is_str) {
6400             if (t->intern) {
6401                 *t->p = PyUnicode_InternFromString(t->s);
6402             } else if (t->encoding) {
6403                 *t->p = PyUnicode_Decode(t->s, t->n - 1, t->encoding, NULL);
6404             } else {
6405                 *t->p = PyUnicode_FromStringAndSize(t->s, t->n - 1);
6406             }
6407         } else {
6408             *t->p = PyBytes_FromStringAndSize(t->s, t->n - 1);
6409         }
6410         #endif
6411         if (!*t->p)
6412             return -1;
6413         ++t;
6414     }
6415     return 0;
6416 }
6417 """)
6418
6419 #------------------------------------------------------------------------------------
6420
6421 force_init_threads_utility_code = UtilityCode(
6422 proto="""
6423 #ifndef __PYX_FORCE_INIT_THREADS
6424   #if PY_VERSION_HEX < 0x02040200
6425     #define __PYX_FORCE_INIT_THREADS 1
6426   #else
6427     #define __PYX_FORCE_INIT_THREADS 0
6428   #endif
6429 #endif
6430 """)
6431
6432 #------------------------------------------------------------------------------------
6433
6434 # Note that cPython ignores PyTrace_EXCEPTION, 
6435 # but maybe some other profilers don't. 
6436
6437 profile_utility_code = UtilityCode(proto="""
6438 #ifndef CYTHON_PROFILE
6439   #define CYTHON_PROFILE 1
6440 #endif
6441
6442 #ifndef CYTHON_PROFILE_REUSE_FRAME
6443   #define CYTHON_PROFILE_REUSE_FRAME 0
6444 #endif
6445
6446 #if CYTHON_PROFILE
6447
6448   #include "compile.h"
6449   #include "frameobject.h"
6450   #include "traceback.h"
6451
6452   #if CYTHON_PROFILE_REUSE_FRAME
6453     #define CYTHON_FRAME_MODIFIER static
6454     #define CYTHON_FRAME_DEL
6455   #else
6456     #define CYTHON_FRAME_MODIFIER
6457     #define CYTHON_FRAME_DEL Py_DECREF(%(FRAME)s)
6458   #endif
6459
6460   #define __Pyx_TraceDeclarations                                  \\
6461   static PyCodeObject *%(FRAME_CODE)s = NULL;                      \\
6462   CYTHON_FRAME_MODIFIER PyFrameObject *%(FRAME)s = NULL;           \\
6463   int __Pyx_use_tracing = 0;                                                         
6464
6465   #define __Pyx_TraceCall(funcname, srcfile, firstlineno)                            \\
6466   if (unlikely(PyThreadState_GET()->use_tracing && PyThreadState_GET()->c_profilefunc)) {      \\
6467       __Pyx_use_tracing = __Pyx_TraceSetupAndCall(&%(FRAME_CODE)s, &%(FRAME)s, funcname, srcfile, firstlineno);  \\
6468   }
6469
6470   #define __Pyx_TraceException()                                                           \\
6471   if (unlikely(__Pyx_use_tracing( && PyThreadState_GET()->use_tracing && PyThreadState_GET()->c_profilefunc) {  \\
6472       PyObject *exc_info = __Pyx_GetExceptionTuple();                                      \\
6473       if (exc_info) {                                                                      \\
6474           PyThreadState_GET()->c_profilefunc(                                              \\
6475               PyThreadState_GET()->c_profileobj, %(FRAME)s, PyTrace_EXCEPTION, exc_info);  \\
6476           Py_DECREF(exc_info);                                                             \\
6477       }                                                                                    \\
6478   }
6479
6480   #define __Pyx_TraceReturn(result)                                                  \\
6481   if (unlikely(__Pyx_use_tracing) && PyThreadState_GET()->use_tracing && PyThreadState_GET()->c_profilefunc) {  \\
6482       PyThreadState_GET()->c_profilefunc(                                            \\
6483           PyThreadState_GET()->c_profileobj, %(FRAME)s, PyTrace_RETURN, (PyObject*)result);     \\
6484       CYTHON_FRAME_DEL;                                                               \\
6485   }
6486
6487   static PyCodeObject *__Pyx_createFrameCodeObject(const char *funcname, const char *srcfile, int firstlineno); /*proto*/
6488   static int __Pyx_TraceSetupAndCall(PyCodeObject** code, PyFrameObject** frame, const char *funcname, const char *srcfile, int firstlineno); /*proto*/
6489
6490 #else
6491
6492   #define __Pyx_TraceDeclarations
6493   #define __Pyx_TraceCall(funcname, srcfile, firstlineno) 
6494   #define __Pyx_TraceException() 
6495   #define __Pyx_TraceReturn(result) 
6496
6497 #endif /* CYTHON_PROFILE */
6498 """ 
6499 % {
6500     "FRAME": Naming.frame_cname,
6501     "FRAME_CODE": Naming.frame_code_cname,
6502 },
6503 impl = """
6504
6505 #if CYTHON_PROFILE
6506
6507 static int __Pyx_TraceSetupAndCall(PyCodeObject** code,
6508                                    PyFrameObject** frame,
6509                                    const char *funcname,
6510                                    const char *srcfile,
6511                                    int firstlineno) {
6512     if (*frame == NULL || !CYTHON_PROFILE_REUSE_FRAME) {
6513         if (*code == NULL) {
6514             *code = __Pyx_createFrameCodeObject(funcname, srcfile, firstlineno);
6515             if (*code == NULL) return 0;
6516         }
6517         *frame = PyFrame_New(
6518             PyThreadState_GET(),            /*PyThreadState *tstate*/
6519             *code,                          /*PyCodeObject *code*/
6520             PyModule_GetDict(%(MODULE)s),      /*PyObject *globals*/
6521             0                               /*PyObject *locals*/
6522         );
6523         if (*frame == NULL) return 0;
6524     }
6525     else {
6526         (*frame)->f_tstate = PyThreadState_GET();
6527     }
6528     return PyThreadState_GET()->c_profilefunc(PyThreadState_GET()->c_profileobj, *frame, PyTrace_CALL, NULL) == 0;
6529 }
6530
6531 static PyCodeObject *__Pyx_createFrameCodeObject(const char *funcname, const char *srcfile, int firstlineno) {
6532     PyObject *py_srcfile = 0;
6533     PyObject *py_funcname = 0;
6534     PyCodeObject *py_code = 0;
6535
6536     #if PY_MAJOR_VERSION < 3
6537     py_funcname = PyString_FromString(funcname);
6538     py_srcfile = PyString_FromString(srcfile);
6539     #else
6540     py_funcname = PyUnicode_FromString(funcname);
6541     py_srcfile = PyUnicode_FromString(srcfile);
6542     #endif
6543     if (!py_funcname | !py_srcfile) goto bad;
6544
6545     py_code = PyCode_New(
6546         0,                /*int argcount,*/
6547         #if PY_MAJOR_VERSION >= 3
6548         0,                /*int kwonlyargcount,*/
6549         #endif
6550         0,                /*int nlocals,*/
6551         0,                /*int stacksize,*/
6552         0,                /*int flags,*/
6553         %(EMPTY_BYTES)s,  /*PyObject *code,*/
6554         %(EMPTY_TUPLE)s,  /*PyObject *consts,*/
6555         %(EMPTY_TUPLE)s,  /*PyObject *names,*/
6556         %(EMPTY_TUPLE)s,  /*PyObject *varnames,*/
6557         %(EMPTY_TUPLE)s,  /*PyObject *freevars,*/
6558         %(EMPTY_TUPLE)s,  /*PyObject *cellvars,*/
6559         py_srcfile,       /*PyObject *filename,*/
6560         py_funcname,      /*PyObject *name,*/
6561         firstlineno,      /*int firstlineno,*/
6562         %(EMPTY_BYTES)s   /*PyObject *lnotab*/
6563     );
6564
6565 bad: 
6566     Py_XDECREF(py_srcfile);
6567     Py_XDECREF(py_funcname);
6568     
6569     return py_code;
6570 }
6571
6572 #endif /* CYTHON_PROFILE */
6573 """ % {
6574     'EMPTY_TUPLE' : Naming.empty_tuple,
6575     'EMPTY_BYTES' : Naming.empty_bytes,
6576     "MODULE": Naming.module_cname,
6577 })