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