when indexing 'str', it's safe to infer 'str' for the result
[cython.git] / Cython / Compiler / ExprNodes.py
1 #
2 #   Pyrex - Parse tree nodes for expressions
3 #
4
5 import cython
6 from cython import set
7 cython.declare(error=object, warning=object, warn_once=object, InternalError=object,
8                CompileError=object, UtilityCode=object, StringEncoding=object, operator=object,
9                Naming=object, Nodes=object, PyrexTypes=object, py_object_type=object,
10                list_type=object, tuple_type=object, set_type=object, dict_type=object, \
11                unicode_type=object, str_type=object, bytes_type=object, type_type=object,
12                Builtin=object, Symtab=object, Utils=object, find_coercion_error=object,
13                debug_disposal_code=object, debug_temp_alloc=object, debug_coercion=object)
14
15 import operator
16
17 from Errors import error, warning, warn_once, InternalError, CompileError
18 from Errors import hold_errors, release_errors, held_errors, report_error
19 from Code import UtilityCode
20 import StringEncoding
21 import Naming
22 import Nodes
23 from Nodes import Node
24 import PyrexTypes
25 from PyrexTypes import py_object_type, c_long_type, typecast, error_type, \
26      unspecified_type
27 from Builtin import list_type, tuple_type, set_type, dict_type, \
28      unicode_type, str_type, bytes_type, type_type
29 import Builtin
30 import Symtab
31 import Options
32 from Cython import Utils
33 from Annotate import AnnotationItem
34
35 from Cython.Debugging import print_call_chain
36 from DebugFlags import debug_disposal_code, debug_temp_alloc, \
37     debug_coercion
38
39 try:
40     from __builtin__ import basestring
41 except ImportError:
42     basestring = str # Python 3
43
44 class NotConstant(object):
45     def __repr__(self):
46         return "<NOT CONSTANT>"
47
48 not_a_constant = NotConstant()
49 constant_value_not_set = object()
50
51 # error messages when coercing from key[0] to key[1]
52 find_coercion_error = {
53     # string related errors
54     (Builtin.unicode_type, Builtin.bytes_type) : "Cannot convert Unicode string to 'bytes' implicitly, encoding required.",
55     (Builtin.unicode_type, Builtin.str_type)   : "Cannot convert Unicode string to 'str' implicitly. This is not portable and requires explicit encoding.",
56     (Builtin.unicode_type, PyrexTypes.c_char_ptr_type) : "Unicode objects do not support coercion to C types.",
57     (Builtin.bytes_type, Builtin.unicode_type) : "Cannot convert 'bytes' object to unicode implicitly, decoding required",
58     (Builtin.bytes_type, Builtin.str_type) : "Cannot convert 'bytes' object to str implicitly. This is not portable to Py3.",
59     (Builtin.str_type, Builtin.unicode_type) : "str objects do not support coercion to unicode, use a unicode string literal instead (u'')",
60     (Builtin.str_type, Builtin.bytes_type) : "Cannot convert 'str' to 'bytes' implicitly. This is not portable.",
61     (Builtin.str_type, PyrexTypes.c_char_ptr_type) : "'str' objects do not support coercion to C types (use 'bytes'?).",
62     (PyrexTypes.c_char_ptr_type, Builtin.unicode_type) : "Cannot convert 'char*' to unicode implicitly, decoding required",
63     (PyrexTypes.c_uchar_ptr_type, Builtin.unicode_type) : "Cannot convert 'char*' to unicode implicitly, decoding required",
64     }.get
65
66
67 class ExprNode(Node):
68     #  subexprs     [string]     Class var holding names of subexpr node attrs
69     #  type         PyrexType    Type of the result
70     #  result_code  string       Code fragment
71     #  result_ctype string       C type of result_code if different from type
72     #  is_temp      boolean      Result is in a temporary variable
73     #  is_sequence_constructor
74     #               boolean      Is a list or tuple constructor expression
75     #  is_starred   boolean      Is a starred expression (e.g. '*a')
76     #  saved_subexpr_nodes
77     #               [ExprNode or [ExprNode or None] or None]
78     #                            Cached result of subexpr_nodes()
79     #  use_managed_ref boolean   use ref-counted temps/assignments/etc.
80
81     result_ctype = None
82     type = None
83     temp_code = None
84     old_temp = None # error checker for multiple frees etc.
85     use_managed_ref = True # can be set by optimisation transforms
86
87     #  The Analyse Expressions phase for expressions is split
88     #  into two sub-phases:
89     #
90     #    Analyse Types
91     #      Determines the result type of the expression based
92     #      on the types of its sub-expressions, and inserts
93     #      coercion nodes into the expression tree where needed.
94     #      Marks nodes which will need to have temporary variables
95     #      allocated.
96     #
97     #    Allocate Temps
98     #      Allocates temporary variables where needed, and fills
99     #      in the result_code field of each node.
100     #
101     #  ExprNode provides some convenience routines which
102     #  perform both of the above phases. These should only
103     #  be called from statement nodes, and only when no
104     #  coercion nodes need to be added around the expression
105     #  being analysed. In that case, the above two phases
106     #  should be invoked separately.
107     #
108     #  Framework code in ExprNode provides much of the common
109     #  processing for the various phases. It makes use of the
110     #  'subexprs' class attribute of ExprNodes, which should
111     #  contain a list of the names of attributes which can
112     #  hold sub-nodes or sequences of sub-nodes.
113     #
114     #  The framework makes use of a number of abstract methods.
115     #  Their responsibilities are as follows.
116     #
117     #    Declaration Analysis phase
118     #
119     #      analyse_target_declaration
120     #        Called during the Analyse Declarations phase to analyse
121     #        the LHS of an assignment or argument of a del statement.
122     #        Nodes which cannot be the LHS of an assignment need not
123     #        implement it.
124     #
125     #    Expression Analysis phase
126     #
127     #      analyse_types
128     #        - Call analyse_types on all sub-expressions.
129     #        - Check operand types, and wrap coercion nodes around
130     #          sub-expressions where needed.
131     #        - Set the type of this node.
132     #        - If a temporary variable will be required for the
133     #          result, set the is_temp flag of this node.
134     #
135     #      analyse_target_types
136     #        Called during the Analyse Types phase to analyse
137     #        the LHS of an assignment or argument of a del
138     #        statement. Similar responsibilities to analyse_types.
139     #
140     #      target_code
141     #        Called by the default implementation of allocate_target_temps.
142     #        Should return a C lvalue for assigning to the node. The default
143     #        implementation calls calculate_result_code.
144     #
145     #      check_const
146     #        - Check that this node and its subnodes form a
147     #          legal constant expression. If so, do nothing,
148     #          otherwise call not_const.
149     #
150     #        The default implementation of check_const
151     #        assumes that the expression is not constant.
152     #
153     #      check_const_addr
154     #        - Same as check_const, except check that the
155     #          expression is a C lvalue whose address is
156     #          constant. Otherwise, call addr_not_const.
157     #
158     #        The default implementation of calc_const_addr
159     #        assumes that the expression is not a constant
160     #        lvalue.
161     #
162     #   Code Generation phase
163     #
164     #      generate_evaluation_code
165     #        - Call generate_evaluation_code for sub-expressions.
166     #        - Perform the functions of generate_result_code
167     #          (see below).
168     #        - If result is temporary, call generate_disposal_code
169     #          on all sub-expressions.
170     #
171     #        A default implementation of generate_evaluation_code
172     #        is provided which uses the following abstract methods:
173     #
174     #          generate_result_code
175     #            - Generate any C statements necessary to calculate
176     #              the result of this node from the results of its
177     #              sub-expressions.
178     #
179     #          calculate_result_code
180     #            - Should return a C code fragment evaluating to the
181     #              result. This is only called when the result is not
182     #              a temporary.
183     #
184     #      generate_assignment_code
185     #        Called on the LHS of an assignment.
186     #        - Call generate_evaluation_code for sub-expressions.
187     #        - Generate code to perform the assignment.
188     #        - If the assignment absorbed a reference, call
189     #          generate_post_assignment_code on the RHS,
190     #          otherwise call generate_disposal_code on it.
191     #
192     #      generate_deletion_code
193     #        Called on an argument of a del statement.
194     #        - Call generate_evaluation_code for sub-expressions.
195     #        - Generate code to perform the deletion.
196     #        - Call generate_disposal_code on all sub-expressions.
197     #
198     #
199
200     is_sequence_constructor = 0
201     is_attribute = 0
202
203     saved_subexpr_nodes = None
204     is_temp = 0
205     is_target = 0
206     is_starred = 0
207
208     constant_result = constant_value_not_set
209
210     try:
211         _get_child_attrs = operator.attrgetter('subexprs')
212     except AttributeError:
213         # Python 2.3
214         def __get_child_attrs(self):
215             return self.subexprs
216         _get_child_attrs = __get_child_attrs
217     child_attrs = property(fget=_get_child_attrs)
218
219     def not_implemented(self, method_name):
220         print_call_chain(method_name, "not implemented") ###
221         raise InternalError(
222             "%s.%s not implemented" %
223                 (self.__class__.__name__, method_name))
224
225     def is_lvalue(self):
226         return 0
227
228     def is_ephemeral(self):
229         #  An ephemeral node is one whose result is in
230         #  a Python temporary and we suspect there are no
231         #  other references to it. Certain operations are
232         #  disallowed on such values, since they are
233         #  likely to result in a dangling pointer.
234         return self.type.is_pyobject and self.is_temp
235
236     def subexpr_nodes(self):
237         #  Extract a list of subexpression nodes based
238         #  on the contents of the subexprs class attribute.
239         nodes = []
240         for name in self.subexprs:
241             item = getattr(self, name)
242             if item is not None:
243                 if type(item) is list:
244                     nodes.extend(item)
245                 else:
246                     nodes.append(item)
247         return nodes
248
249     def result(self):
250         if self.is_temp:
251             return self.temp_code
252         else:
253             return self.calculate_result_code()
254
255     def result_as(self, type = None):
256         #  Return the result code cast to the specified C type.
257         return typecast(type, self.ctype(), self.result())
258
259     def py_result(self):
260         #  Return the result code cast to PyObject *.
261         return self.result_as(py_object_type)
262
263     def ctype(self):
264         #  Return the native C type of the result (i.e. the
265         #  C type of the result_code expression).
266         return self.result_ctype or self.type
267
268     def get_constant_c_result_code(self):
269         # Return the constant value of this node as a result code
270         # string, or None if the node is not constant.  This method
271         # can be called when the constant result code is required
272         # before the code generation phase.
273         #
274         # The return value is a string that can represent a simple C
275         # value, a constant C name or a constant C expression.  If the
276         # node type depends on Python code, this must return None.
277         return None
278
279     def calculate_constant_result(self):
280         # Calculate the constant compile time result value of this
281         # expression and store it in ``self.constant_result``.  Does
282         # nothing by default, thus leaving ``self.constant_result``
283         # unknown.  If valid, the result can be an arbitrary Python
284         # value.
285         #
286         # This must only be called when it is assured that all
287         # sub-expressions have a valid constant_result value.  The
288         # ConstantFolding transform will do this.
289         pass
290
291     def has_constant_result(self):
292         return self.constant_result is not constant_value_not_set and \
293                self.constant_result is not not_a_constant
294
295     def compile_time_value(self, denv):
296         #  Return value of compile-time expression, or report error.
297         error(self.pos, "Invalid compile-time expression")
298
299     def compile_time_value_error(self, e):
300         error(self.pos, "Error in compile-time expression: %s: %s" % (
301             e.__class__.__name__, e))
302
303     # ------------- Declaration Analysis ----------------
304
305     def analyse_target_declaration(self, env):
306         error(self.pos, "Cannot assign to or delete this")
307
308     # ------------- Expression Analysis ----------------
309
310     def analyse_const_expression(self, env):
311         #  Called during the analyse_declarations phase of a
312         #  constant expression. Analyses the expression's type,
313         #  checks whether it is a legal const expression,
314         #  and determines its value.
315         self.analyse_types(env)
316         return self.check_const()
317
318     def analyse_expressions(self, env):
319         #  Convenience routine performing both the Type
320         #  Analysis and Temp Allocation phases for a whole
321         #  expression.
322         self.analyse_types(env)
323
324     def analyse_target_expression(self, env, rhs):
325         #  Convenience routine performing both the Type
326         #  Analysis and Temp Allocation phases for the LHS of
327         #  an assignment.
328         self.analyse_target_types(env)
329
330     def analyse_boolean_expression(self, env):
331         #  Analyse expression and coerce to a boolean.
332         self.analyse_types(env)
333         bool = self.coerce_to_boolean(env)
334         return bool
335
336     def analyse_temp_boolean_expression(self, env):
337         #  Analyse boolean expression and coerce result into
338         #  a temporary. This is used when a branch is to be
339         #  performed on the result and we won't have an
340         #  opportunity to ensure disposal code is executed
341         #  afterwards. By forcing the result into a temporary,
342         #  we ensure that all disposal has been done by the
343         #  time we get the result.
344         self.analyse_types(env)
345         return self.coerce_to_boolean(env).coerce_to_simple(env)
346
347     # --------------- Type Inference -----------------
348
349     def type_dependencies(self, env):
350         # Returns the list of entries whose types must be determined
351         # before the type of self can be infered.
352         if hasattr(self, 'type') and self.type is not None:
353             return ()
354         return sum([node.type_dependencies(env) for node in self.subexpr_nodes()], ())
355
356     def infer_type(self, env):
357         # Attempt to deduce the type of self.
358         # Differs from analyse_types as it avoids unnecessary
359         # analysis of subexpressions, but can assume everything
360         # in self.type_dependencies() has been resolved.
361         if hasattr(self, 'type') and self.type is not None:
362             return self.type
363         elif hasattr(self, 'entry') and self.entry is not None:
364             return self.entry.type
365         else:
366             self.not_implemented("infer_type")
367
368     # --------------- Type Analysis ------------------
369
370     def analyse_as_module(self, env):
371         # If this node can be interpreted as a reference to a
372         # cimported module, return its scope, else None.
373         return None
374
375     def analyse_as_type(self, env):
376         # If this node can be interpreted as a reference to a
377         # type, return that type, else None.
378         return None
379
380     def analyse_as_extension_type(self, env):
381         # If this node can be interpreted as a reference to an
382         # extension type, return its type, else None.
383         return None
384
385     def analyse_types(self, env):
386         self.not_implemented("analyse_types")
387
388     def analyse_target_types(self, env):
389         self.analyse_types(env)
390
391     def nogil_check(self, env):
392         # By default, any expression based on Python objects is
393         # prevented in nogil environments.  Subtypes must override
394         # this if they can work without the GIL.
395         if self.type.is_pyobject:
396             self.gil_error()
397
398     def gil_assignment_check(self, env):
399         if env.nogil and self.type.is_pyobject:
400             error(self.pos, "Assignment of Python object not allowed without gil")
401
402     def check_const(self):
403         self.not_const()
404         return False
405
406     def not_const(self):
407         error(self.pos, "Not allowed in a constant expression")
408
409     def check_const_addr(self):
410         self.addr_not_const()
411         return False
412
413     def addr_not_const(self):
414         error(self.pos, "Address is not constant")
415
416     # ----------------- Result Allocation -----------------
417
418     def result_in_temp(self):
419         #  Return true if result is in a temporary owned by
420         #  this node or one of its subexpressions. Overridden
421         #  by certain nodes which can share the result of
422         #  a subnode.
423         return self.is_temp
424
425     def target_code(self):
426         #  Return code fragment for use as LHS of a C assignment.
427         return self.calculate_result_code()
428
429     def calculate_result_code(self):
430         self.not_implemented("calculate_result_code")
431
432 #    def release_target_temp(self, env):
433 #        #  Release temporaries used by LHS of an assignment.
434 #        self.release_subexpr_temps(env)
435
436     def allocate_temp_result(self, code):
437         if self.temp_code:
438             raise RuntimeError("Temp allocated multiple times in %r: %r" % (self.__class__.__name__, self.pos))
439         type = self.type
440         if not type.is_void:
441             if type.is_pyobject:
442                 type = PyrexTypes.py_object_type
443             self.temp_code = code.funcstate.allocate_temp(
444                 type, manage_ref=self.use_managed_ref)
445         else:
446             self.temp_code = None
447
448     def release_temp_result(self, code):
449         if not self.temp_code:
450             if self.old_temp:
451                 raise RuntimeError("temp %s released multiple times in %s" % (
452                         self.old_temp, self.__class__.__name__))
453             else:
454                 raise RuntimeError("no temp, but release requested in %s" % (
455                         self.__class__.__name__))
456         code.funcstate.release_temp(self.temp_code)
457         self.old_temp = self.temp_code
458         self.temp_code = None
459
460     # ---------------- Code Generation -----------------
461
462     def make_owned_reference(self, code):
463         #  If result is a pyobject, make sure we own
464         #  a reference to it.
465         if self.type.is_pyobject and not self.result_in_temp():
466             code.put_incref(self.result(), self.ctype())
467
468     def generate_evaluation_code(self, code):
469         code.mark_pos(self.pos)
470
471         #  Generate code to evaluate this node and
472         #  its sub-expressions, and dispose of any
473         #  temporary results of its sub-expressions.
474         self.generate_subexpr_evaluation_code(code)
475
476         if self.is_temp:
477             self.allocate_temp_result(code)
478
479         self.generate_result_code(code)
480         if self.is_temp:
481             # If we are temp we do not need to wait until this node is disposed
482             # before disposing children.
483             self.generate_subexpr_disposal_code(code)
484             self.free_subexpr_temps(code)
485
486     def generate_subexpr_evaluation_code(self, code):
487         for node in self.subexpr_nodes():
488             node.generate_evaluation_code(code)
489
490     def generate_result_code(self, code):
491         self.not_implemented("generate_result_code")
492
493     def generate_disposal_code(self, code):
494         if self.is_temp:
495             if self.type.is_pyobject:
496                 code.put_decref_clear(self.result(), self.ctype())
497         else:
498             # Already done if self.is_temp
499             self.generate_subexpr_disposal_code(code)
500
501     def generate_subexpr_disposal_code(self, code):
502         #  Generate code to dispose of temporary results
503         #  of all sub-expressions.
504         for node in self.subexpr_nodes():
505             node.generate_disposal_code(code)
506
507     def generate_post_assignment_code(self, code):
508         if self.is_temp:
509             if self.type.is_pyobject:
510                 code.putln("%s = 0;" % self.result())
511         else:
512             self.generate_subexpr_disposal_code(code)
513
514     def generate_assignment_code(self, rhs, code):
515         #  Stub method for nodes which are not legal as
516         #  the LHS of an assignment. An error will have
517         #  been reported earlier.
518         pass
519
520     def generate_deletion_code(self, code):
521         #  Stub method for nodes that are not legal as
522         #  the argument of a del statement. An error
523         #  will have been reported earlier.
524         pass
525
526     def free_temps(self, code):
527         if self.is_temp:
528             if not self.type.is_void:
529                 self.release_temp_result(code)
530         else:
531             self.free_subexpr_temps(code)
532
533     def free_subexpr_temps(self, code):
534         for sub in self.subexpr_nodes():
535             sub.free_temps(code)
536
537     def generate_function_definitions(self, env, code):
538         pass
539
540     # ---------------- Annotation ---------------------
541
542     def annotate(self, code):
543         for node in self.subexpr_nodes():
544             node.annotate(code)
545
546     # ----------------- Coercion ----------------------
547
548     def coerce_to(self, dst_type, env):
549         #   Coerce the result so that it can be assigned to
550         #   something of type dst_type. If processing is necessary,
551         #   wraps this node in a coercion node and returns that.
552         #   Otherwise, returns this node unchanged.
553         #
554         #   This method is called during the analyse_expressions
555         #   phase of the src_node's processing.
556         #
557         #   Note that subclasses that override this (especially
558         #   ConstNodes) must not (re-)set their own .type attribute
559         #   here.  Since expression nodes may turn up in different
560         #   places in the tree (e.g. inside of CloneNodes in cascaded
561         #   assignments), this method must return a new node instance
562         #   if it changes the type.
563         #
564         src = self
565         src_type = self.type
566         src_is_py_type = src_type.is_pyobject
567         dst_is_py_type = dst_type.is_pyobject
568
569         if self.check_for_coercion_error(dst_type):
570             return self
571
572         if dst_type.is_reference:
573             dst_type = dst_type.ref_base_type
574
575         if dst_type.is_pyobject:
576             if not src.type.is_pyobject:
577                 if dst_type is bytes_type and src.type.is_int:
578                     src = CoerceIntToBytesNode(src, env)
579                 else:
580                     src = CoerceToPyTypeNode(src, env)
581             if not src.type.subtype_of(dst_type):
582                 if not isinstance(src, NoneNode):
583                     src = PyTypeTestNode(src, dst_type, env)
584         elif src.type.is_pyobject:
585             src = CoerceFromPyTypeNode(dst_type, src, env)
586         elif (dst_type.is_complex
587               and src_type != dst_type
588               and dst_type.assignable_from(src_type)):
589             src = CoerceToComplexNode(src, dst_type, env)
590         else: # neither src nor dst are py types
591             # Added the string comparison, since for c types that
592             # is enough, but Cython gets confused when the types are
593             # in different pxi files.
594             if not (str(src.type) == str(dst_type) or dst_type.assignable_from(src_type)):
595                 self.fail_assignment(dst_type)
596         return src
597
598     def fail_assignment(self, dst_type):
599         error(self.pos, "Cannot assign type '%s' to '%s'" % (self.type, dst_type))
600
601     def check_for_coercion_error(self, dst_type, fail=False, default=None):
602         if fail and not default:
603             default = "Cannot assign type '%(FROM)s' to '%(TO)s'"
604         message = find_coercion_error((self.type, dst_type), default)
605         if message is not None:
606             error(self.pos, message % {'FROM': self.type, 'TO': dst_type})
607             return True
608         if fail:
609             self.fail_assignment(dst_type)
610             return True
611         return False
612
613     def coerce_to_pyobject(self, env):
614         return self.coerce_to(PyrexTypes.py_object_type, env)
615
616     def coerce_to_boolean(self, env):
617         #  Coerce result to something acceptable as
618         #  a boolean value.
619
620         # if it's constant, calculate the result now
621         if self.has_constant_result():
622             bool_value = bool(self.constant_result)
623             return BoolNode(self.pos, value=bool_value,
624                             constant_result=bool_value)
625
626         type = self.type
627         if type.is_pyobject or type.is_ptr or type.is_float:
628             return CoerceToBooleanNode(self, env)
629         else:
630             if not (type.is_int or type.is_enum or type.is_error):
631                 error(self.pos,
632                     "Type '%s' not acceptable as a boolean" % type)
633             return self
634
635     def coerce_to_integer(self, env):
636         # If not already some C integer type, coerce to longint.
637         if self.type.is_int:
638             return self
639         else:
640             return self.coerce_to(PyrexTypes.c_long_type, env)
641
642     def coerce_to_temp(self, env):
643         #  Ensure that the result is in a temporary.
644         if self.result_in_temp():
645             return self
646         else:
647             return CoerceToTempNode(self, env)
648
649     def coerce_to_simple(self, env):
650         #  Ensure that the result is simple (see is_simple).
651         if self.is_simple():
652             return self
653         else:
654             return self.coerce_to_temp(env)
655
656     def is_simple(self):
657         #  A node is simple if its result is something that can
658         #  be referred to without performing any operations, e.g.
659         #  a constant, local var, C global var, struct member
660         #  reference, or temporary.
661         return self.result_in_temp()
662
663     def may_be_none(self):
664         if not self.type.is_pyobject:
665             return False
666         if self.constant_result not in (not_a_constant, constant_value_not_set):
667             return self.constant_result is not None
668         return True
669
670     def as_cython_attribute(self):
671         return None
672
673     def as_none_safe_node(self, message, error="PyExc_TypeError"):
674         # Wraps the node in a NoneCheckNode if it is not known to be
675         # not-None (e.g. because it is a Python literal).
676         if self.may_be_none():
677             return NoneCheckNode(self, error, message)
678         else:
679             return self
680
681
682 class AtomicExprNode(ExprNode):
683     #  Abstract base class for expression nodes which have
684     #  no sub-expressions.
685
686     subexprs = []
687
688     # Override to optimize -- we know we have no children
689     def generate_subexpr_evaluation_code(self, code):
690         pass
691     def generate_subexpr_disposal_code(self, code):
692         pass
693
694 class PyConstNode(AtomicExprNode):
695     #  Abstract base class for constant Python values.
696
697     is_literal = 1
698     type = py_object_type
699
700     def is_simple(self):
701         return 1
702
703     def may_be_none(self):
704         return False
705
706     def analyse_types(self, env):
707         pass
708
709     def calculate_result_code(self):
710         return self.value
711
712     def generate_result_code(self, code):
713         pass
714
715
716 class NoneNode(PyConstNode):
717     #  The constant value None
718
719     value = "Py_None"
720
721     constant_result = None
722
723     nogil_check = None
724
725     def compile_time_value(self, denv):
726         return None
727
728     def may_be_none(self):
729         return True
730
731
732 class EllipsisNode(PyConstNode):
733     #  '...' in a subscript list.
734
735     value = "Py_Ellipsis"
736
737     constant_result = Ellipsis
738
739     def compile_time_value(self, denv):
740         return Ellipsis
741
742
743 class ConstNode(AtomicExprNode):
744     # Abstract base type for literal constant nodes.
745     #
746     # value     string      C code fragment
747
748     is_literal = 1
749     nogil_check = None
750
751     def is_simple(self):
752         return 1
753
754     def may_be_none(self):
755         return False
756
757     def analyse_types(self, env):
758         pass # Types are held in class variables
759
760     def check_const(self):
761         return True
762
763     def get_constant_c_result_code(self):
764         return self.calculate_result_code()
765
766     def calculate_result_code(self):
767         return str(self.value)
768
769     def generate_result_code(self, code):
770         pass
771
772
773 class BoolNode(ConstNode):
774     type = PyrexTypes.c_bint_type
775     #  The constant value True or False
776
777     def calculate_constant_result(self):
778         self.constant_result = self.value
779
780     def compile_time_value(self, denv):
781         return self.value
782
783     def calculate_result_code(self):
784         return str(int(self.value))
785
786
787 class NullNode(ConstNode):
788     type = PyrexTypes.c_null_ptr_type
789     value = "NULL"
790     constant_result = 0
791
792     def get_constant_c_result_code(self):
793         return self.value
794
795
796 class CharNode(ConstNode):
797     type = PyrexTypes.c_char_type
798
799     def calculate_constant_result(self):
800         self.constant_result = ord(self.value)
801
802     def compile_time_value(self, denv):
803         return ord(self.value)
804
805     def calculate_result_code(self):
806         return "'%s'" % StringEncoding.escape_char(self.value)
807
808
809 class IntNode(ConstNode):
810
811     # unsigned     "" or "U"
812     # longness     "" or "L" or "LL"
813     # is_c_literal   True/False/None   creator considers this a C integer literal
814
815     unsigned = ""
816     longness = ""
817     is_c_literal = None # unknown
818
819     def __init__(self, pos, **kwds):
820         ExprNode.__init__(self, pos, **kwds)
821         if 'type' not in kwds:
822             self.type = self.find_suitable_type_for_value()
823
824     def find_suitable_type_for_value(self):
825         if self.constant_result is constant_value_not_set:
826             try:
827                 self.calculate_constant_result()
828             except ValueError:
829                 pass
830         # we ignore 'is_c_literal = True' and instead map signed 32bit
831         # integers as C long values
832         if self.is_c_literal or \
833                self.constant_result in (constant_value_not_set, not_a_constant) or \
834                self.unsigned or self.longness == 'LL':
835             # clearly a C literal
836             rank = (self.longness == 'LL') and 2 or 1
837             suitable_type = PyrexTypes.modifiers_and_name_to_type[not self.unsigned, rank, "int"]
838             if self.type:
839                 suitable_type = PyrexTypes.widest_numeric_type(suitable_type, self.type)
840         else:
841             # C literal or Python literal - split at 32bit boundary
842             if self.constant_result >= -2**31 and self.constant_result < 2**31:
843                 if self.type and self.type.is_int:
844                     suitable_type = self.type
845                 else:
846                     suitable_type = PyrexTypes.c_long_type
847             else:
848                 suitable_type = PyrexTypes.py_object_type
849         return suitable_type
850
851     def coerce_to(self, dst_type, env):
852         if self.type is dst_type:
853             return self
854         elif dst_type.is_float:
855             if self.constant_result is not not_a_constant:
856                 float_value = float(self.constant_result)
857                 return FloatNode(self.pos, value=repr(float_value), type=dst_type,
858                                  constant_result=float_value)
859             else:
860                 return FloatNode(self.pos, value=self.value, type=dst_type,
861                                  constant_result=not_a_constant)
862         if dst_type.is_numeric and not dst_type.is_complex:
863             node = IntNode(self.pos, value=self.value, constant_result=self.constant_result,
864                            type = dst_type, is_c_literal = True,
865                            unsigned=self.unsigned, longness=self.longness)
866             return node
867         elif dst_type.is_pyobject:
868             node = IntNode(self.pos, value=self.value, constant_result=self.constant_result,
869                            type = PyrexTypes.py_object_type, is_c_literal = False,
870                            unsigned=self.unsigned, longness=self.longness)
871         else:
872             # FIXME: not setting the type here to keep it working with
873             # complex numbers. Should they be special cased?
874             node = IntNode(self.pos, value=self.value, constant_result=self.constant_result,
875                            unsigned=self.unsigned, longness=self.longness)
876         # We still need to perform normal coerce_to processing on the
877         # result, because we might be coercing to an extension type,
878         # in which case a type test node will be needed.
879         return ConstNode.coerce_to(node, dst_type, env)
880
881     def coerce_to_boolean(self, env):
882         return IntNode(
883             self.pos, value=self.value,
884             type = PyrexTypes.c_bint_type,
885             unsigned=self.unsigned, longness=self.longness)
886
887     def generate_evaluation_code(self, code):
888         if self.type.is_pyobject:
889             # pre-allocate a Python version of the number
890             plain_integer_string = self.value_as_c_integer_string(plain_digits=True)
891             self.result_code = code.get_py_num(plain_integer_string, self.longness)
892         else:
893             self.result_code = self.get_constant_c_result_code()
894
895     def get_constant_c_result_code(self):
896         return self.value_as_c_integer_string() + self.unsigned + self.longness
897
898     def value_as_c_integer_string(self, plain_digits=False):
899         value = self.value
900         if isinstance(value, basestring) and len(value) > 2:
901             # must convert C-incompatible Py3 oct/bin notations
902             if value[1] in 'oO':
903                 if plain_digits:
904                     value = int(value[2:], 8)
905                 else:
906                     value = value[0] + value[2:] # '0o123' => '0123'
907             elif value[1] in 'bB':
908                 value = int(value[2:], 2)
909             elif plain_digits and value[1] in 'xX':
910                 value = int(value[2:], 16)
911         return str(value)
912
913     def calculate_result_code(self):
914         return self.result_code
915
916     def calculate_constant_result(self):
917         self.constant_result = Utils.str_to_number(self.value)
918
919     def compile_time_value(self, denv):
920         return Utils.str_to_number(self.value)
921
922
923 class FloatNode(ConstNode):
924     type = PyrexTypes.c_double_type
925
926     def calculate_constant_result(self):
927         self.constant_result = float(self.value)
928
929     def compile_time_value(self, denv):
930         return float(self.value)
931
932     def calculate_result_code(self):
933         strval = self.value
934         assert isinstance(strval, (str, unicode))
935         cmpval = repr(float(strval))
936         if cmpval == 'nan':
937             return "(Py_HUGE_VAL * 0)"
938         elif cmpval == 'inf':
939             return "Py_HUGE_VAL"
940         elif cmpval == '-inf':
941             return "(-Py_HUGE_VAL)"
942         else:
943             return strval
944
945
946 class BytesNode(ConstNode):
947     # A char* or bytes literal
948     #
949     # value      BytesLiteral
950
951     # start off as Python 'bytes' to support len() in O(1)
952     type = bytes_type
953
954     def compile_time_value(self, denv):
955         return self.value
956
957     def analyse_as_type(self, env):
958         type = PyrexTypes.parse_basic_type(self.value)
959         if type is not None:
960             return type
961         from TreeFragment import TreeFragment
962         pos = (self.pos[0], self.pos[1], self.pos[2]-7)
963         declaration = TreeFragment(u"sizeof(%s)" % self.value, name=pos[0].filename, initial_pos=pos)
964         sizeof_node = declaration.root.stats[0].expr
965         sizeof_node.analyse_types(env)
966         if isinstance(sizeof_node, SizeofTypeNode):
967             return sizeof_node.arg_type
968
969     def can_coerce_to_char_literal(self):
970         return len(self.value) == 1
971
972     def coerce_to_boolean(self, env):
973         # This is special because testing a C char* for truth directly
974         # would yield the wrong result.
975         return BoolNode(self.pos, value=bool(self.value))
976
977     def coerce_to(self, dst_type, env):
978         if self.type == dst_type:
979             return self
980         if dst_type.is_int:
981             if not self.can_coerce_to_char_literal():
982                 error(self.pos, "Only single-character string literals can be coerced into ints.")
983                 return self
984             if dst_type.is_unicode_char:
985                 error(self.pos, "Bytes literals cannot coerce to Py_UNICODE/Py_UCS4, use a unicode literal instead.")
986                 return self
987             return CharNode(self.pos, value=self.value)
988
989         node = BytesNode(self.pos, value=self.value)
990         if dst_type.is_pyobject:
991             if dst_type in (py_object_type, Builtin.bytes_type):
992                 node.type = Builtin.bytes_type
993             else:
994                 self.check_for_coercion_error(dst_type, fail=True)
995                 return node
996         elif dst_type == PyrexTypes.c_char_ptr_type:
997             node.type = dst_type
998             return node
999         elif dst_type == PyrexTypes.c_uchar_ptr_type:
1000             node.type = PyrexTypes.c_char_ptr_type
1001             return CastNode(node, PyrexTypes.c_uchar_ptr_type)
1002         elif dst_type.assignable_from(PyrexTypes.c_char_ptr_type):
1003             node.type = dst_type
1004             return node
1005
1006         # We still need to perform normal coerce_to processing on the
1007         # result, because we might be coercing to an extension type,
1008         # in which case a type test node will be needed.
1009         return ConstNode.coerce_to(node, dst_type, env)
1010
1011     def generate_evaluation_code(self, code):
1012         if self.type.is_pyobject:
1013             self.result_code = code.get_py_string_const(self.value)
1014         else:
1015             self.result_code = code.get_string_const(self.value)
1016
1017     def get_constant_c_result_code(self):
1018         return None # FIXME
1019
1020     def calculate_result_code(self):
1021         return self.result_code
1022
1023
1024 class UnicodeNode(PyConstNode):
1025     # A Python unicode object
1026     #
1027     # value        EncodedString
1028     # bytes_value  BytesLiteral    the literal parsed as bytes string ('-3' unicode literals only)
1029
1030     bytes_value = None
1031     type = unicode_type
1032
1033     def coerce_to(self, dst_type, env):
1034         if dst_type is self.type:
1035             pass
1036         elif dst_type.is_unicode_char:
1037             if not self.can_coerce_to_char_literal():
1038                 error(self.pos, "Only single-character Unicode string literals or surrogate pairs can be coerced into Py_UCS4/Py_UNICODE.")
1039                 return self
1040             int_value = ord(self.value)
1041             return IntNode(self.pos, type=dst_type, value=str(int_value), constant_result=int_value)
1042         elif not dst_type.is_pyobject:
1043             if dst_type.is_string and self.bytes_value is not None:
1044                 # special case: '-3' enforced unicode literal used in a C char* context
1045                 return BytesNode(self.pos, value=self.bytes_value).coerce_to(dst_type, env)
1046             error(self.pos, "Unicode literals do not support coercion to C types other than Py_UNICODE or Py_UCS4.")
1047         elif dst_type is not py_object_type:
1048             if not self.check_for_coercion_error(dst_type):
1049                 self.fail_assignment(dst_type)
1050         return self
1051
1052     def can_coerce_to_char_literal(self):
1053         return len(self.value) == 1
1054             ## or (len(self.value) == 2
1055             ##     and (0xD800 <= self.value[0] <= 0xDBFF)
1056             ##     and (0xDC00 <= self.value[1] <= 0xDFFF))
1057
1058     def contains_surrogates(self):
1059         # Check if the unicode string contains surrogate code points
1060         # on a CPython platform with wide (UCS-4) or narrow (UTF-16)
1061         # Unicode, i.e. characters that would be spelled as two
1062         # separate code units on a narrow platform.
1063         for c in map(ord, self.value):
1064             if c > 65535: # can only happen on wide platforms
1065                 return True
1066             # We only look for the first code unit (D800-DBFF) of a
1067             # surrogate pair - if we find one, the other one
1068             # (DC00-DFFF) is likely there, too.  If we don't find it,
1069             # any second code unit cannot make for a surrogate pair by
1070             # itself.
1071             if c >= 0xD800 and c <= 0xDBFF:
1072                 return True
1073         return False
1074
1075     def generate_evaluation_code(self, code):
1076         self.result_code = code.get_py_string_const(self.value)
1077
1078     def calculate_result_code(self):
1079         return self.result_code
1080
1081     def compile_time_value(self, env):
1082         return self.value
1083
1084
1085 class StringNode(PyConstNode):
1086     # A Python str object, i.e. a byte string in Python 2.x and a
1087     # unicode string in Python 3.x
1088     #
1089     # value          BytesLiteral (or EncodedString with ASCII content)
1090     # unicode_value  EncodedString or None
1091     # is_identifier  boolean
1092
1093     type = str_type
1094     is_identifier = None
1095     unicode_value = None
1096
1097     def coerce_to(self, dst_type, env):
1098         if dst_type is not py_object_type and not str_type.subtype_of(dst_type):
1099 #            if dst_type is Builtin.bytes_type:
1100 #                # special case: bytes = 'str literal'
1101 #                return BytesNode(self.pos, value=self.value)
1102             if not dst_type.is_pyobject:
1103                 return BytesNode(self.pos, value=self.value).coerce_to(dst_type, env)
1104             self.check_for_coercion_error(dst_type, fail=True)
1105
1106         # this will be a unicode string in Py3, so make sure we can decode it
1107         if self.value.encoding and isinstance(self.value, StringEncoding.BytesLiteral):
1108             try:
1109                 self.value.decode(self.value.encoding)
1110             except UnicodeDecodeError:
1111                 error(self.pos, ("Decoding unprefixed string literal from '%s' failed. Consider using"
1112                                  "a byte string or unicode string explicitly, "
1113                                  "or adjust the source code encoding.") % self.value.encoding)
1114
1115         return self
1116
1117     def can_coerce_to_char_literal(self):
1118         return not self.is_identifier and len(self.value) == 1
1119
1120     def generate_evaluation_code(self, code):
1121         self.result_code = code.get_py_string_const(
1122             self.value, identifier=self.is_identifier, is_str=True)
1123
1124     def get_constant_c_result_code(self):
1125         return None
1126
1127     def calculate_result_code(self):
1128         return self.result_code
1129
1130     def compile_time_value(self, env):
1131         return self.value
1132
1133
1134 class IdentifierStringNode(StringNode):
1135     # A special str value that represents an identifier (bytes in Py2,
1136     # unicode in Py3).
1137     is_identifier = True
1138
1139
1140 class LongNode(AtomicExprNode):
1141     #  Python long integer literal
1142     #
1143     #  value   string
1144
1145     type = py_object_type
1146
1147     def calculate_constant_result(self):
1148         self.constant_result = Utils.str_to_number(self.value)
1149
1150     def compile_time_value(self, denv):
1151         return Utils.str_to_number(self.value)
1152
1153     def analyse_types(self, env):
1154         self.is_temp = 1
1155
1156     def may_be_none(self):
1157         return False
1158
1159     gil_message = "Constructing Python long int"
1160
1161     def generate_result_code(self, code):
1162         code.putln(
1163             '%s = PyLong_FromString((char *)"%s", 0, 0); %s' % (
1164                 self.result(),
1165                 self.value,
1166                 code.error_goto_if_null(self.result(), self.pos)))
1167         code.put_gotref(self.py_result())
1168
1169
1170 class ImagNode(AtomicExprNode):
1171     #  Imaginary number literal
1172     #
1173     #  value   float    imaginary part
1174
1175     type = PyrexTypes.c_double_complex_type
1176
1177     def calculate_constant_result(self):
1178         self.constant_result = complex(0.0, self.value)
1179
1180     def compile_time_value(self, denv):
1181         return complex(0.0, self.value)
1182
1183     def analyse_types(self, env):
1184         self.type.create_declaration_utility_code(env)
1185
1186     def may_be_none(self):
1187         return False
1188
1189     def coerce_to(self, dst_type, env):
1190         if self.type is dst_type:
1191             return self
1192         node = ImagNode(self.pos, value=self.value)
1193         if dst_type.is_pyobject:
1194             node.is_temp = 1
1195             node.type = PyrexTypes.py_object_type
1196         # We still need to perform normal coerce_to processing on the
1197         # result, because we might be coercing to an extension type,
1198         # in which case a type test node will be needed.
1199         return AtomicExprNode.coerce_to(node, dst_type, env)
1200
1201     gil_message = "Constructing complex number"
1202
1203     def calculate_result_code(self):
1204         if self.type.is_pyobject:
1205             return self.result()
1206         else:
1207             return "%s(0, %r)" % (self.type.from_parts, float(self.value))
1208
1209     def generate_result_code(self, code):
1210         if self.type.is_pyobject:
1211             code.putln(
1212                 "%s = PyComplex_FromDoubles(0.0, %r); %s" % (
1213                     self.result(),
1214                     float(self.value),
1215                     code.error_goto_if_null(self.result(), self.pos)))
1216             code.put_gotref(self.py_result())
1217
1218
1219 class NewExprNode(AtomicExprNode):
1220
1221     # C++ new statement
1222     #
1223     # cppclass              node                 c++ class to create
1224
1225     type = None
1226
1227     def infer_type(self, env):
1228         type = self.cppclass.analyse_as_type(env)
1229         if type is None or not type.is_cpp_class:
1230             error(self.pos, "new operator can only be applied to a C++ class")
1231             self.type = error_type
1232             return
1233         self.cpp_check(env)
1234         constructor = type.scope.lookup(u'<init>')
1235         if constructor is None:
1236             return_type = PyrexTypes.CFuncType(type, [])
1237             return_type = PyrexTypes.CPtrType(return_type)
1238             type.scope.declare_cfunction(u'<init>', return_type, self.pos)
1239             constructor = type.scope.lookup(u'<init>')
1240         self.class_type = type
1241         self.entry = constructor
1242         self.type = constructor.type
1243         return self.type
1244
1245     def analyse_types(self, env):
1246         if self.type is None:
1247             self.infer_type(env)
1248
1249     def may_be_none(self):
1250         return False
1251
1252     def generate_result_code(self, code):
1253         pass
1254
1255     def calculate_result_code(self):
1256         return "new " + self.class_type.declaration_code("")
1257
1258
1259 class NameNode(AtomicExprNode):
1260     #  Reference to a local or global variable name.
1261     #
1262     #  name            string    Python name of the variable
1263     #  entry           Entry     Symbol table entry
1264     #  type_entry      Entry     For extension type names, the original type entry
1265
1266     is_name = True
1267     is_cython_module = False
1268     cython_attribute = None
1269     lhs_of_first_assignment = False
1270     is_used_as_rvalue = 0
1271     entry = None
1272     type_entry = None
1273
1274     def create_analysed_rvalue(pos, env, entry):
1275         node = NameNode(pos)
1276         node.analyse_types(env, entry=entry)
1277         return node
1278
1279     def as_cython_attribute(self):
1280         return self.cython_attribute
1281
1282     create_analysed_rvalue = staticmethod(create_analysed_rvalue)
1283
1284     def type_dependencies(self, env):
1285         if self.entry is None:
1286             self.entry = env.lookup(self.name)
1287         if self.entry is not None and self.entry.type.is_unspecified:
1288             return (self.entry,)
1289         else:
1290             return ()
1291
1292     def infer_type(self, env):
1293         if self.entry is None:
1294             self.entry = env.lookup(self.name)
1295         if self.entry is None:
1296             return py_object_type
1297         elif (self.entry.type.is_extension_type or self.entry.type.is_builtin_type) and \
1298                 self.name == self.entry.type.name:
1299             # Unfortunately the type attribute of type objects
1300             # is used for the pointer to the type they represent.
1301             return type_type
1302         elif self.entry.type.is_cfunction:
1303             # special case: referring to a C function must return its pointer
1304             return PyrexTypes.CPtrType(self.entry.type)
1305         else:
1306             return self.entry.type
1307
1308     def compile_time_value(self, denv):
1309         try:
1310             return denv.lookup(self.name)
1311         except KeyError:
1312             error(self.pos, "Compile-time name '%s' not defined" % self.name)
1313
1314     def get_constant_c_result_code(self):
1315         if not self.entry or self.entry.type.is_pyobject:
1316             return None
1317         return self.entry.cname
1318
1319     def coerce_to(self, dst_type, env):
1320         #  If coercing to a generic pyobject and this is a builtin
1321         #  C function with a Python equivalent, manufacture a NameNode
1322         #  referring to the Python builtin.
1323         #print "NameNode.coerce_to:", self.name, dst_type ###
1324         if dst_type is py_object_type:
1325             entry = self.entry
1326             if entry and entry.is_cfunction:
1327                 var_entry = entry.as_variable
1328                 if var_entry:
1329                     if var_entry.is_builtin and Options.cache_builtins:
1330                         var_entry = env.declare_builtin(var_entry.name, self.pos)
1331                     node = NameNode(self.pos, name = self.name)
1332                     node.entry = var_entry
1333                     node.analyse_rvalue_entry(env)
1334                     return node
1335         return super(NameNode, self).coerce_to(dst_type, env)
1336
1337     def analyse_as_module(self, env):
1338         # Try to interpret this as a reference to a cimported module.
1339         # Returns the module scope, or None.
1340         entry = self.entry
1341         if not entry:
1342             entry = env.lookup(self.name)
1343         if entry and entry.as_module:
1344             return entry.as_module
1345         return None
1346
1347     def analyse_as_type(self, env):
1348         if self.cython_attribute:
1349             type = PyrexTypes.parse_basic_type(self.cython_attribute)
1350         else:
1351             type = PyrexTypes.parse_basic_type(self.name)
1352         if type:
1353             return type
1354         entry = self.entry
1355         if not entry:
1356             entry = env.lookup(self.name)
1357         if entry and entry.is_type:
1358             return entry.type
1359         else:
1360             return None
1361
1362     def analyse_as_extension_type(self, env):
1363         # Try to interpret this as a reference to an extension type.
1364         # Returns the extension type, or None.
1365         entry = self.entry
1366         if not entry:
1367             entry = env.lookup(self.name)
1368         if entry and entry.is_type and entry.type.is_extension_type:
1369             return entry.type
1370         else:
1371             return None
1372
1373     def analyse_target_declaration(self, env):
1374         if not self.entry:
1375             self.entry = env.lookup_here(self.name)
1376         if not self.entry:
1377             if env.directives['warn.undeclared']:
1378                 warning(self.pos, "implicit declaration of '%s'" % self.name, 1)
1379             if env.directives['infer_types'] != False:
1380                 type = unspecified_type
1381             else:
1382                 type = py_object_type
1383             self.entry = env.declare_var(self.name, type, self.pos)
1384         env.control_flow.set_state(self.pos, (self.name, 'initialized'), True)
1385         env.control_flow.set_state(self.pos, (self.name, 'source'), 'assignment')
1386         if self.entry.is_declared_generic:
1387             self.result_ctype = py_object_type
1388
1389     def analyse_types(self, env):
1390         if self.entry is None:
1391             self.entry = env.lookup(self.name)
1392         if not self.entry:
1393             self.entry = env.declare_builtin(self.name, self.pos)
1394         if not self.entry:
1395             self.type = PyrexTypes.error_type
1396             return
1397         entry = self.entry
1398         if entry:
1399             entry.used = 1
1400             if entry.type.is_buffer:
1401                 import Buffer
1402                 Buffer.used_buffer_aux_vars(entry)
1403             if entry.utility_code:
1404                 env.use_utility_code(entry.utility_code)
1405         self.analyse_rvalue_entry(env)
1406
1407     def analyse_target_types(self, env):
1408         self.analyse_entry(env)
1409         if not self.is_lvalue():
1410             error(self.pos, "Assignment to non-lvalue '%s'"
1411                 % self.name)
1412             self.type = PyrexTypes.error_type
1413         self.entry.used = 1
1414         if self.entry.type.is_buffer:
1415             import Buffer
1416             Buffer.used_buffer_aux_vars(self.entry)
1417
1418     def analyse_rvalue_entry(self, env):
1419         #print "NameNode.analyse_rvalue_entry:", self.name ###
1420         #print "Entry:", self.entry.__dict__ ###
1421         self.analyse_entry(env)
1422         entry = self.entry
1423         if entry.is_declared_generic:
1424             self.result_ctype = py_object_type
1425         if entry.is_pyglobal or entry.is_builtin:
1426             if Options.cache_builtins and entry.is_builtin:
1427                 self.is_temp = 0
1428             else:
1429                 self.is_temp = 1
1430                 env.use_utility_code(get_name_interned_utility_code)
1431             self.is_used_as_rvalue = 1
1432
1433     def nogil_check(self, env):
1434         if self.is_used_as_rvalue:
1435             entry = self.entry
1436             if entry.is_builtin:
1437                 if not Options.cache_builtins: # cached builtins are ok
1438                     self.gil_error()
1439             elif entry.is_pyglobal:
1440                 self.gil_error()
1441
1442     gil_message = "Accessing Python global or builtin"
1443
1444     def analyse_entry(self, env):
1445         #print "NameNode.analyse_entry:", self.name ###
1446         self.check_identifier_kind()
1447         entry = self.entry
1448         type = entry.type
1449         self.type = type
1450
1451     def check_identifier_kind(self):
1452         # Check that this is an appropriate kind of name for use in an
1453         # expression.  Also finds the variable entry associated with
1454         # an extension type.
1455         entry = self.entry
1456         if entry.is_type and entry.type.is_extension_type:
1457             self.type_entry = entry
1458         if not (entry.is_const or entry.is_variable
1459             or entry.is_builtin or entry.is_cfunction
1460             or entry.is_cpp_class):
1461                 if self.entry.as_variable:
1462                     self.entry = self.entry.as_variable
1463                 else:
1464                     error(self.pos,
1465                           "'%s' is not a constant, variable or function identifier" % self.name)
1466
1467     def is_simple(self):
1468         #  If it's not a C variable, it'll be in a temp.
1469         return 1
1470
1471     def calculate_target_results(self, env):
1472         pass
1473
1474     def check_const(self):
1475         entry = self.entry
1476         if entry is not None and not (entry.is_const or entry.is_cfunction or entry.is_builtin):
1477             self.not_const()
1478             return False
1479         return True
1480
1481     def check_const_addr(self):
1482         entry = self.entry
1483         if not (entry.is_cglobal or entry.is_cfunction or entry.is_builtin):
1484             self.addr_not_const()
1485             return False
1486         return True
1487
1488     def is_lvalue(self):
1489         return self.entry.is_variable and \
1490             not self.entry.type.is_array and \
1491             not self.entry.is_readonly
1492
1493     def is_ephemeral(self):
1494         #  Name nodes are never ephemeral, even if the
1495         #  result is in a temporary.
1496         return 0
1497
1498     def calculate_result_code(self):
1499         entry = self.entry
1500         if not entry:
1501             return "<error>" # There was an error earlier
1502         return entry.cname
1503
1504     def generate_result_code(self, code):
1505         assert hasattr(self, 'entry')
1506         entry = self.entry
1507         if entry is None:
1508             return # There was an error earlier
1509         if entry.is_builtin and Options.cache_builtins:
1510             return # Lookup already cached
1511         elif entry.is_pyclass_attr:
1512             assert entry.type.is_pyobject, "Python global or builtin not a Python object"
1513             interned_cname = code.intern_identifier(self.entry.name)
1514             if entry.is_builtin:
1515                 namespace = Naming.builtins_cname
1516             else: # entry.is_pyglobal
1517                 namespace = entry.scope.namespace_cname
1518             code.putln(
1519                 '%s = PyObject_GetItem(%s, %s); %s' % (
1520                 self.result(),
1521                 namespace,
1522                 interned_cname,
1523                 code.error_goto_if_null(self.result(), self.pos)))
1524             code.put_gotref(self.py_result())
1525
1526         elif entry.is_pyglobal or entry.is_builtin:
1527             assert entry.type.is_pyobject, "Python global or builtin not a Python object"
1528             interned_cname = code.intern_identifier(self.entry.name)
1529             if entry.is_builtin:
1530                 namespace = Naming.builtins_cname
1531             else: # entry.is_pyglobal
1532                 namespace = entry.scope.namespace_cname
1533             code.globalstate.use_utility_code(get_name_interned_utility_code)
1534             code.putln(
1535                 '%s = __Pyx_GetName(%s, %s); %s' % (
1536                 self.result(),
1537                 namespace,
1538                 interned_cname,
1539                 code.error_goto_if_null(self.result(), self.pos)))
1540             code.put_gotref(self.py_result())
1541
1542         elif entry.is_local and False:
1543             # control flow not good enough yet
1544             assigned = entry.scope.control_flow.get_state((entry.name, 'initialized'), self.pos)
1545             if assigned is False:
1546                 error(self.pos, "local variable '%s' referenced before assignment" % entry.name)
1547             elif not Options.init_local_none and assigned is None:
1548                 code.putln('if (%s == 0) { PyErr_SetString(PyExc_UnboundLocalError, "%s"); %s }' %
1549                            (entry.cname, entry.name, code.error_goto(self.pos)))
1550                 entry.scope.control_flow.set_state(self.pos, (entry.name, 'initialized'), True)
1551
1552     def generate_assignment_code(self, rhs, code):
1553         #print "NameNode.generate_assignment_code:", self.name ###
1554         entry = self.entry
1555         if entry is None:
1556             return # There was an error earlier
1557
1558         if (self.entry.type.is_ptr and isinstance(rhs, ListNode)
1559             and not self.lhs_of_first_assignment):
1560             error(self.pos, "Literal list must be assigned to pointer at time of declaration")
1561
1562         # is_pyglobal seems to be True for module level-globals only.
1563         # We use this to access class->tp_dict if necessary.
1564         if entry.is_pyglobal:
1565             assert entry.type.is_pyobject, "Python global or builtin not a Python object"
1566             interned_cname = code.intern_identifier(self.entry.name)
1567             namespace = self.entry.scope.namespace_cname
1568             if entry.is_member:
1569                 # if the entry is a member we have to cheat: SetAttr does not work
1570                 # on types, so we create a descriptor which is then added to tp_dict
1571                 code.put_error_if_neg(self.pos,
1572                     'PyDict_SetItem(%s->tp_dict, %s, %s)' % (
1573                         namespace,
1574                         interned_cname,
1575                         rhs.py_result()))
1576                 rhs.generate_disposal_code(code)
1577                 rhs.free_temps(code)
1578                 # in Py2.6+, we need to invalidate the method cache
1579                 code.putln("PyType_Modified(%s);" %
1580                             entry.scope.parent_type.typeptr_cname)
1581             elif entry.is_pyclass_attr:
1582                 code.put_error_if_neg(self.pos,
1583                     'PyObject_SetItem(%s, %s, %s)' % (
1584                         namespace,
1585                         interned_cname,
1586                         rhs.py_result()))
1587                 rhs.generate_disposal_code(code)
1588                 rhs.free_temps(code)
1589             else:
1590                 code.put_error_if_neg(self.pos,
1591                     'PyObject_SetAttr(%s, %s, %s)' % (
1592                         namespace,
1593                         interned_cname,
1594                         rhs.py_result()))
1595                 if debug_disposal_code:
1596                     print("NameNode.generate_assignment_code:")
1597                     print("...generating disposal code for %s" % rhs)
1598                 rhs.generate_disposal_code(code)
1599                 rhs.free_temps(code)
1600         else:
1601             if self.type.is_buffer:
1602                 # Generate code for doing the buffer release/acquisition.
1603                 # This might raise an exception in which case the assignment (done
1604                 # below) will not happen.
1605                 #
1606                 # The reason this is not in a typetest-like node is because the
1607                 # variables that the acquired buffer info is stored to is allocated
1608                 # per entry and coupled with it.
1609                 self.generate_acquire_buffer(rhs, code)
1610
1611             if self.type.is_pyobject:
1612                 #print "NameNode.generate_assignment_code: to", self.name ###
1613                 #print "...from", rhs ###
1614                 #print "...LHS type", self.type, "ctype", self.ctype() ###
1615                 #print "...RHS type", rhs.type, "ctype", rhs.ctype() ###
1616                 if self.use_managed_ref:
1617                     rhs.make_owned_reference(code)
1618                     if entry.is_cglobal:
1619                         code.put_gotref(self.py_result())
1620                     if not self.lhs_of_first_assignment:
1621                         if entry.is_local and not Options.init_local_none:
1622                             initialized = entry.scope.control_flow.get_state((entry.name, 'initialized'), self.pos)
1623                             if initialized is True:
1624                                 code.put_decref(self.result(), self.ctype())
1625                             elif initialized is None:
1626                                 code.put_xdecref(self.result(), self.ctype())
1627                         else:
1628                             code.put_decref(self.result(), self.ctype())
1629                     if entry.is_cglobal:
1630                         code.put_giveref(rhs.py_result())
1631
1632             code.putln('%s = %s;' % (self.result(),
1633                                      rhs.result_as(self.ctype())))
1634             if debug_disposal_code:
1635                 print("NameNode.generate_assignment_code:")
1636                 print("...generating post-assignment code for %s" % rhs)
1637             rhs.generate_post_assignment_code(code)
1638             rhs.free_temps(code)
1639
1640     def generate_acquire_buffer(self, rhs, code):
1641         # rhstmp is only used in case the rhs is a complicated expression leading to
1642         # the object, to avoid repeating the same C expression for every reference
1643         # to the rhs. It does NOT hold a reference.
1644         pretty_rhs = isinstance(rhs, NameNode) or rhs.is_temp
1645         if pretty_rhs:
1646             rhstmp = rhs.result_as(self.ctype())
1647         else:
1648             rhstmp = code.funcstate.allocate_temp(self.entry.type, manage_ref=False)
1649             code.putln('%s = %s;' % (rhstmp, rhs.result_as(self.ctype())))
1650
1651         buffer_aux = self.entry.buffer_aux
1652         bufstruct = buffer_aux.buffer_info_var.cname
1653         import Buffer
1654         Buffer.put_assign_to_buffer(self.result(), rhstmp, buffer_aux, self.entry.type,
1655                                     is_initialized=not self.lhs_of_first_assignment,
1656                                     pos=self.pos, code=code)
1657
1658         if not pretty_rhs:
1659             code.putln("%s = 0;" % rhstmp)
1660             code.funcstate.release_temp(rhstmp)
1661
1662     def generate_deletion_code(self, code):
1663         if self.entry is None:
1664             return # There was an error earlier
1665         if not self.entry.is_pyglobal:
1666             error(self.pos, "Deletion of local or C global name not supported")
1667             return
1668         if self.entry.is_pyclass_attr:
1669             namespace = self.entry.scope.namespace_cname
1670             code.put_error_if_neg(self.pos,
1671                 'PyMapping_DelItemString(%s, "%s")' % (
1672                     namespace,
1673                     self.entry.name))
1674         else:
1675             code.put_error_if_neg(self.pos,
1676                 '__Pyx_DelAttrString(%s, "%s")' % (
1677                     Naming.module_cname,
1678                     self.entry.name))
1679
1680     def annotate(self, code):
1681         if hasattr(self, 'is_called') and self.is_called:
1682             pos = (self.pos[0], self.pos[1], self.pos[2] - len(self.name) - 1)
1683             if self.type.is_pyobject:
1684                 code.annotate(pos, AnnotationItem('py_call', 'python function', size=len(self.name)))
1685             else:
1686                 code.annotate(pos, AnnotationItem('c_call', 'c function', size=len(self.name)))
1687
1688 class BackquoteNode(ExprNode):
1689     #  `expr`
1690     #
1691     #  arg    ExprNode
1692
1693     type = py_object_type
1694
1695     subexprs = ['arg']
1696
1697     def analyse_types(self, env):
1698         self.arg.analyse_types(env)
1699         self.arg = self.arg.coerce_to_pyobject(env)
1700         self.is_temp = 1
1701
1702     gil_message = "Backquote expression"
1703
1704     def calculate_constant_result(self):
1705         self.constant_result = repr(self.arg.constant_result)
1706
1707     def generate_result_code(self, code):
1708         code.putln(
1709             "%s = PyObject_Repr(%s); %s" % (
1710                 self.result(),
1711                 self.arg.py_result(),
1712                 code.error_goto_if_null(self.result(), self.pos)))
1713         code.put_gotref(self.py_result())
1714
1715
1716
1717 class ImportNode(ExprNode):
1718     #  Used as part of import statement implementation.
1719     #  Implements result =
1720     #    __import__(module_name, globals(), None, name_list)
1721     #
1722     #  module_name   StringNode            dotted name of module
1723     #  name_list     ListNode or None      list of names to be imported
1724
1725     type = py_object_type
1726
1727     subexprs = ['module_name', 'name_list']
1728
1729     def analyse_types(self, env):
1730         self.module_name.analyse_types(env)
1731         self.module_name = self.module_name.coerce_to_pyobject(env)
1732         if self.name_list:
1733             self.name_list.analyse_types(env)
1734             self.name_list.coerce_to_pyobject(env)
1735         self.is_temp = 1
1736         env.use_utility_code(import_utility_code)
1737
1738     gil_message = "Python import"
1739
1740     def generate_result_code(self, code):
1741         if self.name_list:
1742             name_list_code = self.name_list.py_result()
1743         else:
1744             name_list_code = "0"
1745         code.putln(
1746             "%s = __Pyx_Import(%s, %s); %s" % (
1747                 self.result(),
1748                 self.module_name.py_result(),
1749                 name_list_code,
1750                 code.error_goto_if_null(self.result(), self.pos)))
1751         code.put_gotref(self.py_result())
1752
1753
1754 class IteratorNode(ExprNode):
1755     #  Used as part of for statement implementation.
1756     #
1757     #  allocate_counter_temp/release_counter_temp needs to be called
1758     #  by parent (ForInStatNode)
1759     #
1760     #  Implements result = iter(sequence)
1761     #
1762     #  sequence   ExprNode
1763
1764     type = py_object_type
1765
1766     subexprs = ['sequence']
1767
1768     def analyse_types(self, env):
1769         self.sequence.analyse_types(env)
1770         if (self.sequence.type.is_array or self.sequence.type.is_ptr) and \
1771                 not self.sequence.type.is_string:
1772             # C array iteration will be transformed later on
1773             self.type = self.sequence.type
1774         else:
1775             self.sequence = self.sequence.coerce_to_pyobject(env)
1776             if self.sequence.type is list_type or \
1777                    self.sequence.type is tuple_type:
1778                 self.sequence = self.sequence.as_none_safe_node("'NoneType' object is not iterable")
1779         self.is_temp = 1
1780
1781     gil_message = "Iterating over Python object"
1782
1783     def allocate_counter_temp(self, code):
1784         self.counter_cname = code.funcstate.allocate_temp(
1785             PyrexTypes.c_py_ssize_t_type, manage_ref=False)
1786
1787     def release_counter_temp(self, code):
1788         code.funcstate.release_temp(self.counter_cname)
1789
1790     def generate_result_code(self, code):
1791         if self.sequence.type.is_array or self.sequence.type.is_ptr:
1792             raise InternalError("for in carray slice not transformed")
1793         is_builtin_sequence = self.sequence.type is list_type or \
1794                               self.sequence.type is tuple_type
1795         may_be_a_sequence = not self.sequence.type.is_builtin_type
1796         if may_be_a_sequence:
1797             code.putln(
1798                 "if (PyList_CheckExact(%s) || PyTuple_CheckExact(%s)) {" % (
1799                     self.sequence.py_result(),
1800                     self.sequence.py_result()))
1801         if is_builtin_sequence or may_be_a_sequence:
1802             code.putln(
1803                 "%s = 0; %s = %s; __Pyx_INCREF(%s);" % (
1804                     self.counter_cname,
1805                     self.result(),
1806                     self.sequence.py_result(),
1807                     self.result()))
1808         if not is_builtin_sequence:
1809             if may_be_a_sequence:
1810                 code.putln("} else {")
1811             code.putln("%s = -1; %s = PyObject_GetIter(%s); %s" % (
1812                     self.counter_cname,
1813                     self.result(),
1814                     self.sequence.py_result(),
1815                     code.error_goto_if_null(self.result(), self.pos)))
1816             code.put_gotref(self.py_result())
1817             if may_be_a_sequence:
1818                 code.putln("}")
1819
1820
1821 class NextNode(AtomicExprNode):
1822     #  Used as part of for statement implementation.
1823     #  Implements result = iterator.next()
1824     #  Created during analyse_types phase.
1825     #  The iterator is not owned by this node.
1826     #
1827     #  iterator   ExprNode
1828
1829     type = py_object_type
1830
1831     def __init__(self, iterator, env):
1832         self.pos = iterator.pos
1833         self.iterator = iterator
1834         if iterator.type.is_ptr or iterator.type.is_array:
1835             self.type = iterator.type.base_type
1836         self.is_temp = 1
1837
1838     def generate_result_code(self, code):
1839         sequence_type = self.iterator.sequence.type
1840         if sequence_type is list_type:
1841             type_checks = [(list_type, "List")]
1842         elif sequence_type is tuple_type:
1843             type_checks = [(tuple_type, "Tuple")]
1844         elif not sequence_type.is_builtin_type:
1845             type_checks = [(list_type, "List"), (tuple_type, "Tuple")]
1846         else:
1847             type_checks = []
1848
1849         for py_type, prefix in type_checks:
1850             if len(type_checks) > 1:
1851                 code.putln(
1852                     "if (likely(Py%s_CheckExact(%s))) {" % (
1853                         prefix, self.iterator.py_result()))
1854             code.putln(
1855                 "if (%s >= Py%s_GET_SIZE(%s)) break;" % (
1856                     self.iterator.counter_cname,
1857                     prefix,
1858                     self.iterator.py_result()))
1859             code.putln(
1860                 "%s = Py%s_GET_ITEM(%s, %s); __Pyx_INCREF(%s); %s++;" % (
1861                     self.result(),
1862                     prefix,
1863                     self.iterator.py_result(),
1864                     self.iterator.counter_cname,
1865                     self.result(),
1866                     self.iterator.counter_cname))
1867             if len(type_checks) > 1:
1868                 code.put("} else ")
1869         if len(type_checks) == 1:
1870             return
1871         code.putln("{")
1872         code.putln(
1873             "%s = PyIter_Next(%s);" % (
1874                 self.result(),
1875                 self.iterator.py_result()))
1876         code.putln(
1877             "if (!%s) {" %
1878                 self.result())
1879         code.putln(code.error_goto_if_PyErr(self.pos))
1880         code.putln("break;")
1881         code.putln("}")
1882         code.put_gotref(self.py_result())
1883         code.putln("}")
1884
1885
1886 class ExcValueNode(AtomicExprNode):
1887     #  Node created during analyse_types phase
1888     #  of an ExceptClauseNode to fetch the current
1889     #  exception value.
1890
1891     type = py_object_type
1892
1893     def __init__(self, pos, env):
1894         ExprNode.__init__(self, pos)
1895
1896     def set_var(self, var):
1897         self.var = var
1898
1899     def calculate_result_code(self):
1900         return self.var
1901
1902     def generate_result_code(self, code):
1903         pass
1904
1905     def analyse_types(self, env):
1906         pass
1907
1908
1909 class TempNode(ExprNode):
1910     # Node created during analyse_types phase
1911     # of some nodes to hold a temporary value.
1912     #
1913     # Note: One must call "allocate" and "release" on
1914     # the node during code generation to get/release the temp.
1915     # This is because the temp result is often used outside of
1916     # the regular cycle.
1917
1918     subexprs = []
1919
1920     def __init__(self, pos, type, env):
1921         ExprNode.__init__(self, pos)
1922         self.type = type
1923         if type.is_pyobject:
1924             self.result_ctype = py_object_type
1925         self.is_temp = 1
1926
1927     def analyse_types(self, env):
1928         return self.type
1929
1930     def generate_result_code(self, code):
1931         pass
1932
1933     def allocate(self, code):
1934         self.temp_cname = code.funcstate.allocate_temp(self.type, manage_ref=True)
1935
1936     def release(self, code):
1937         code.funcstate.release_temp(self.temp_cname)
1938         self.temp_cname = None
1939
1940     def result(self):
1941         try:
1942             return self.temp_cname
1943         except:
1944             assert False, "Remember to call allocate/release on TempNode"
1945             raise
1946
1947     # Do not participate in normal temp alloc/dealloc:
1948     def allocate_temp_result(self, code):
1949         pass
1950
1951     def release_temp_result(self, code):
1952         pass
1953
1954 class PyTempNode(TempNode):
1955     #  TempNode holding a Python value.
1956
1957     def __init__(self, pos, env):
1958         TempNode.__init__(self, pos, PyrexTypes.py_object_type, env)
1959
1960 class RawCNameExprNode(ExprNode):
1961     subexprs = []
1962
1963     def __init__(self, pos, type=None):
1964         self.pos = pos
1965         self.type = type
1966
1967     def analyse_types(self, env):
1968         return self.type
1969
1970     def set_cname(self, cname):
1971         self.cname = cname
1972
1973     def result(self):
1974         return self.cname
1975
1976     def generate_result_code(self, code):
1977         pass
1978
1979
1980 #-------------------------------------------------------------------
1981 #
1982 #  Trailer nodes
1983 #
1984 #-------------------------------------------------------------------
1985
1986 class IndexNode(ExprNode):
1987     #  Sequence indexing.
1988     #
1989     #  base     ExprNode
1990     #  index    ExprNode
1991     #  indices  [ExprNode]
1992     #  is_buffer_access boolean Whether this is a buffer access.
1993     #
1994     #  indices is used on buffer access, index on non-buffer access.
1995     #  The former contains a clean list of index parameters, the
1996     #  latter whatever Python object is needed for index access.
1997
1998     subexprs = ['base', 'index', 'indices']
1999     indices = None
2000
2001     def __init__(self, pos, index, *args, **kw):
2002         ExprNode.__init__(self, pos, index=index, *args, **kw)
2003         self._index = index
2004
2005     def calculate_constant_result(self):
2006         self.constant_result = \
2007             self.base.constant_result[self.index.constant_result]
2008
2009     def compile_time_value(self, denv):
2010         base = self.base.compile_time_value(denv)
2011         index = self.index.compile_time_value(denv)
2012         try:
2013             return base[index]
2014         except Exception, e:
2015             self.compile_time_value_error(e)
2016
2017     def is_ephemeral(self):
2018         return self.base.is_ephemeral()
2019
2020     def is_simple(self):
2021         if self.is_buffer_access:
2022             return False
2023         base = self.base
2024         return (base.is_simple() and self.index.is_simple()
2025                 and base.type and (base.type.is_ptr or base.type.is_array))
2026
2027     def analyse_target_declaration(self, env):
2028         pass
2029
2030     def analyse_as_type(self, env):
2031         base_type = self.base.analyse_as_type(env)
2032         if base_type and not base_type.is_pyobject:
2033             if base_type.is_cpp_class:
2034                 if isinstance(self.index, TupleNode):
2035                     template_values = self.index.args
2036                 else:
2037                     template_values = [self.index]
2038                 import Nodes
2039                 type_node = Nodes.TemplatedTypeNode(
2040                     pos = self.pos,
2041                     positional_args = template_values,
2042                     keyword_args = None)
2043                 return type_node.analyse(env, base_type = base_type)
2044             else:
2045                 return PyrexTypes.CArrayType(base_type, int(self.index.compile_time_value(env)))
2046         return None
2047
2048     def type_dependencies(self, env):
2049         return self.base.type_dependencies(env) + self.index.type_dependencies(env)
2050
2051     def infer_type(self, env):
2052         base_type = self.base.infer_type(env)
2053         if isinstance(self.index, SliceNode):
2054             # slicing!
2055             if base_type.is_string:
2056                 # sliced C strings must coerce to Python
2057                 return bytes_type
2058             elif base_type in (unicode_type, bytes_type, str_type, list_type, tuple_type):
2059                 # slicing these returns the same type
2060                 return base_type
2061             else:
2062                 # TODO: Handle buffers (hopefully without too much redundancy).
2063                 return py_object_type
2064
2065         index_type = self.index.infer_type(env)
2066         if index_type and index_type.is_int or isinstance(self.index, (IntNode, LongNode)):
2067             # indexing!
2068             if base_type is unicode_type:
2069                 # Py_UCS4 will automatically coerce to a unicode string
2070                 # if required, so this is safe.  We only infer Py_UCS4
2071                 # when the index is a C integer type.  Otherwise, we may
2072                 # need to use normal Python item access, in which case
2073                 # it's faster to return the one-char unicode string than
2074                 # to receive it, throw it away, and potentially rebuild it
2075                 # on a subsequent PyObject coercion.
2076                 return PyrexTypes.c_py_ucs4_type
2077             elif base_type is str_type:
2078                 # always returns str - Py2: bytes, Py3: unicode
2079                 return base_type
2080             elif isinstance(self.base, BytesNode):
2081                 #if env.global_scope().context.language_level >= 3:
2082                 #    # infering 'char' can be made to work in Python 3 mode
2083                 #    return PyrexTypes.c_char_type
2084                 # Py2/3 return different types on indexing bytes objects
2085                 return py_object_type
2086             elif base_type.is_ptr or base_type.is_array:
2087                 return base_type.base_type
2088
2089         # may be slicing or indexing, we don't know
2090         if base_type is unicode_type:
2091             # this type always returns its own type on Python indexing/slicing
2092             return base_type
2093         else:
2094             # TODO: Handle buffers (hopefully without too much redundancy).
2095             return py_object_type
2096
2097     def analyse_types(self, env):
2098         self.analyse_base_and_index_types(env, getting = 1)
2099
2100     def analyse_target_types(self, env):
2101         self.analyse_base_and_index_types(env, setting = 1)
2102
2103     def analyse_base_and_index_types(self, env, getting = 0, setting = 0):
2104         # Note: This might be cleaned up by having IndexNode
2105         # parsed in a saner way and only construct the tuple if
2106         # needed.
2107
2108         # Note that this function must leave IndexNode in a cloneable state.
2109         # For buffers, self.index is packed out on the initial analysis, and
2110         # when cloning self.indices is copied.
2111         self.is_buffer_access = False
2112
2113         self.base.analyse_types(env)
2114         if self.base.type.is_error:
2115             # Do not visit child tree if base is undeclared to avoid confusing
2116             # error messages
2117             self.type = PyrexTypes.error_type
2118             return
2119
2120         is_slice = isinstance(self.index, SliceNode)
2121         # Potentially overflowing index value.
2122         if not is_slice and isinstance(self.index, IntNode) and Utils.long_literal(self.index.value):
2123             self.index = self.index.coerce_to_pyobject(env)
2124
2125         # Handle the case where base is a literal char* (and we expect a string, not an int)
2126         if isinstance(self.base, BytesNode) or is_slice:
2127             if self.base.type.is_string or not (self.base.type.is_ptr or self.base.type.is_array):
2128                 self.base = self.base.coerce_to_pyobject(env)
2129
2130         skip_child_analysis = False
2131         buffer_access = False
2132         if self.base.type.is_buffer:
2133             if self.indices:
2134                 indices = self.indices
2135             else:
2136                 if isinstance(self.index, TupleNode):
2137                     indices = self.index.args
2138                 else:
2139                     indices = [self.index]
2140             if len(indices) == self.base.type.ndim:
2141                 buffer_access = True
2142                 skip_child_analysis = True
2143                 for x in indices:
2144                     x.analyse_types(env)
2145                     if not x.type.is_int:
2146                         buffer_access = False
2147             if buffer_access:
2148                 assert hasattr(self.base, "entry") # Must be a NameNode-like node
2149
2150         # On cloning, indices is cloned. Otherwise, unpack index into indices
2151         assert not (buffer_access and isinstance(self.index, CloneNode))
2152
2153         if buffer_access:
2154             self.indices = indices
2155             self.index = None
2156             self.type = self.base.type.dtype
2157             self.is_buffer_access = True
2158             self.buffer_type = self.base.entry.type
2159
2160             if getting and self.type.is_pyobject:
2161                 self.is_temp = True
2162             if setting:
2163                 if not self.base.entry.type.writable:
2164                     error(self.pos, "Writing to readonly buffer")
2165                 else:
2166                     self.base.entry.buffer_aux.writable_needed = True
2167         else:
2168             base_type = self.base.type
2169             if isinstance(self.index, TupleNode):
2170                 self.index.analyse_types(env, skip_children=skip_child_analysis)
2171             elif not skip_child_analysis:
2172                 self.index.analyse_types(env)
2173             self.original_index_type = self.index.type
2174             if base_type.is_unicode_char:
2175                 # we infer Py_UNICODE/Py_UCS4 for unicode strings in some
2176                 # cases, but indexing must still work for them
2177                 if self.index.constant_result in (0, -1):
2178                     # FIXME: we know that this node is redundant -
2179                     # currently, this needs to get handled in Optimize.py
2180                     pass
2181                 self.base = self.base.coerce_to_pyobject(env)
2182                 base_type = self.base.type
2183             if base_type.is_pyobject:
2184                 if self.index.type.is_int:
2185                     if (not setting
2186                         and (base_type in (list_type, tuple_type, unicode_type))
2187                         and (not self.index.type.signed or isinstance(self.index, IntNode) and int(self.index.value) >= 0)
2188                         and not env.directives['boundscheck']):
2189                         self.is_temp = 0
2190                     else:
2191                         self.is_temp = 1
2192                     self.index = self.index.coerce_to(PyrexTypes.c_py_ssize_t_type, env).coerce_to_simple(env)
2193                 else:
2194                     self.index = self.index.coerce_to_pyobject(env)
2195                     self.is_temp = 1
2196                 if self.index.type.is_int and base_type is unicode_type:
2197                     # Py_UNICODE/Py_UCS4 will automatically coerce to a unicode string
2198                     # if required, so this is fast and safe
2199                     self.type = PyrexTypes.c_py_ucs4_type
2200                 elif is_slice and base_type in (bytes_type, str_type, unicode_type, list_type, tuple_type):
2201                     self.type = base_type
2202                 else:
2203                     self.type = py_object_type
2204             else:
2205                 if base_type.is_ptr or base_type.is_array:
2206                     self.type = base_type.base_type
2207                     if is_slice:
2208                         self.type = base_type
2209                     elif self.index.type.is_pyobject:
2210                         self.index = self.index.coerce_to(
2211                             PyrexTypes.c_py_ssize_t_type, env)
2212                     elif not self.index.type.is_int:
2213                         error(self.pos,
2214                             "Invalid index type '%s'" %
2215                                 self.index.type)
2216                 elif base_type.is_cpp_class:
2217                     function = env.lookup_operator("[]", [self.base, self.index])
2218                     if function is None:
2219                         error(self.pos, "Indexing '%s' not supported for index type '%s'" % (base_type, self.index.type))
2220                         self.type = PyrexTypes.error_type
2221                         self.result_code = "<error>"
2222                         return
2223                     func_type = function.type
2224                     if func_type.is_ptr:
2225                         func_type = func_type.base_type
2226                     self.index = self.index.coerce_to(func_type.args[0].type, env)
2227                     self.type = func_type.return_type
2228                     if setting and not func_type.return_type.is_reference:
2229                         error(self.pos, "Can't set non-reference result '%s'" % self.type)
2230                 else:
2231                     error(self.pos,
2232                         "Attempting to index non-array type '%s'" %
2233                             base_type)
2234                     self.type = PyrexTypes.error_type
2235
2236     gil_message = "Indexing Python object"
2237
2238     def nogil_check(self, env):
2239         if self.is_buffer_access:
2240             if env.directives['boundscheck']:
2241                 error(self.pos, "Cannot check buffer index bounds without gil; use boundscheck(False) directive")
2242                 return
2243             elif self.type.is_pyobject:
2244                 error(self.pos, "Cannot access buffer with object dtype without gil")
2245                 return
2246         super(IndexNode, self).nogil_check(env)
2247
2248
2249     def check_const_addr(self):
2250         return self.base.check_const_addr() and self.index.check_const()
2251
2252     def is_lvalue(self):
2253         return 1
2254
2255     def calculate_result_code(self):
2256         if self.is_buffer_access:
2257             return "(*%s)" % self.buffer_ptr_code
2258         elif self.base.type is list_type:
2259             return "PyList_GET_ITEM(%s, %s)" % (self.base.result(), self.index.result())
2260         elif self.base.type is tuple_type:
2261             return "PyTuple_GET_ITEM(%s, %s)" % (self.base.result(), self.index.result())
2262         elif self.base.type is unicode_type and self.type.is_unicode_char:
2263             return "PyUnicode_AS_UNICODE(%s)[%s]" % (self.base.result(), self.index.result())
2264         elif (self.type.is_ptr or self.type.is_array) and self.type == self.base.type:
2265             error(self.pos, "Invalid use of pointer slice")
2266         else:
2267             return "(%s[%s])" % (
2268                 self.base.result(), self.index.result())
2269
2270     def extra_index_params(self):
2271         if self.index.type.is_int:
2272             if self.original_index_type.signed:
2273                 size_adjustment = ""
2274             else:
2275                 size_adjustment = "+1"
2276             return ", sizeof(%s)%s, %s" % (self.original_index_type.declaration_code(""), size_adjustment, self.original_index_type.to_py_function)
2277         else:
2278             return ""
2279
2280     def generate_subexpr_evaluation_code(self, code):
2281         self.base.generate_evaluation_code(code)
2282         if not self.indices:
2283             self.index.generate_evaluation_code(code)
2284         else:
2285             for i in self.indices:
2286                 i.generate_evaluation_code(code)
2287
2288     def generate_subexpr_disposal_code(self, code):
2289         self.base.generate_disposal_code(code)
2290         if not self.indices:
2291             self.index.generate_disposal_code(code)
2292         else:
2293             for i in self.indices:
2294                 i.generate_disposal_code(code)
2295
2296     def free_subexpr_temps(self, code):
2297         self.base.free_temps(code)
2298         if not self.indices:
2299             self.index.free_temps(code)
2300         else:
2301             for i in self.indices:
2302                 i.free_temps(code)
2303
2304     def generate_result_code(self, code):
2305         if self.is_buffer_access:
2306             if code.globalstate.directives['nonecheck']:
2307                 self.put_nonecheck(code)
2308             self.buffer_ptr_code = self.buffer_lookup_code(code)
2309             if self.type.is_pyobject:
2310                 # is_temp is True, so must pull out value and incref it.
2311                 code.putln("%s = *%s;" % (self.result(), self.buffer_ptr_code))
2312                 code.putln("__Pyx_INCREF((PyObject*)%s);" % self.result())
2313         elif self.is_temp:
2314             if self.type.is_pyobject:
2315                 if self.index.type.is_int:
2316                     index_code = self.index.result()
2317                     if self.base.type is list_type:
2318                         function = "__Pyx_GetItemInt_List"
2319                     elif self.base.type is tuple_type:
2320                         function = "__Pyx_GetItemInt_Tuple"
2321                     else:
2322                         function = "__Pyx_GetItemInt"
2323                     code.globalstate.use_utility_code(getitem_int_utility_code)
2324                 else:
2325                     index_code = self.index.py_result()
2326                     if self.base.type is dict_type:
2327                         function = "__Pyx_PyDict_GetItem"
2328                         code.globalstate.use_utility_code(getitem_dict_utility_code)
2329                     else:
2330                         function = "PyObject_GetItem"
2331                 code.putln(
2332                     "%s = %s(%s, %s%s); if (!%s) %s" % (
2333                         self.result(),
2334                         function,
2335                         self.base.py_result(),
2336                         index_code,
2337                         self.extra_index_params(),
2338                         self.result(),
2339                         code.error_goto(self.pos)))
2340                 code.put_gotref(self.py_result())
2341             elif self.type.is_unicode_char and self.base.type is unicode_type:
2342                 assert self.index.type.is_int
2343                 index_code = self.index.result()
2344                 function = "__Pyx_GetItemInt_Unicode"
2345                 code.globalstate.use_utility_code(getitem_int_pyunicode_utility_code)
2346                 code.putln(
2347                     "%s = %s(%s, %s%s); if (unlikely(%s == (Py_UNICODE)-1)) %s;" % (
2348                         self.result(),
2349                         function,
2350                         self.base.py_result(),
2351                         index_code,
2352                         self.extra_index_params(),
2353                         self.result(),
2354                         code.error_goto(self.pos)))
2355
2356     def generate_setitem_code(self, value_code, code):
2357         if self.index.type.is_int:
2358             function = "__Pyx_SetItemInt"
2359             index_code = self.index.result()
2360             code.globalstate.use_utility_code(setitem_int_utility_code)
2361         else:
2362             index_code = self.index.py_result()
2363             if self.base.type is dict_type:
2364                 function = "PyDict_SetItem"
2365             # It would seem that we could specialized lists/tuples, but that
2366             # shouldn't happen here.
2367             # Both PyList_SetItem PyTuple_SetItem and a Py_ssize_t as input,
2368             # not a PyObject*, and bad conversion here would give the wrong
2369             # exception. Also, tuples are supposed to be immutable, and raise
2370             # TypeErrors when trying to set their entries (PyTuple_SetItem
2371             # is for creating new tuples from).
2372             else:
2373                 function = "PyObject_SetItem"
2374         code.putln(
2375             "if (%s(%s, %s, %s%s) < 0) %s" % (
2376                 function,
2377                 self.base.py_result(),
2378                 index_code,
2379                 value_code,
2380                 self.extra_index_params(),
2381                 code.error_goto(self.pos)))
2382
2383     def generate_buffer_setitem_code(self, rhs, code, op=""):
2384         # Used from generate_assignment_code and InPlaceAssignmentNode
2385         if code.globalstate.directives['nonecheck']:
2386             self.put_nonecheck(code)
2387         ptrexpr = self.buffer_lookup_code(code)
2388         if self.buffer_type.dtype.is_pyobject:
2389             # Must manage refcounts. Decref what is already there
2390             # and incref what we put in.
2391             ptr = code.funcstate.allocate_temp(self.buffer_type.buffer_ptr_type, manage_ref=False)
2392             rhs_code = rhs.result()
2393             code.putln("%s = %s;" % (ptr, ptrexpr))
2394             code.put_gotref("*%s" % ptr)
2395             code.putln("__Pyx_DECREF(*%s); __Pyx_INCREF(%s);" % (
2396                 ptr, rhs_code
2397                 ))
2398             code.putln("*%s %s= %s;" % (ptr, op, rhs_code))
2399             code.put_giveref("*%s" % ptr)
2400             code.funcstate.release_temp(ptr)
2401         else:
2402             # Simple case
2403             code.putln("*%s %s= %s;" % (ptrexpr, op, rhs.result()))
2404
2405     def generate_assignment_code(self, rhs, code):
2406         self.generate_subexpr_evaluation_code(code)
2407         if self.is_buffer_access:
2408             self.generate_buffer_setitem_code(rhs, code)
2409         elif self.type.is_pyobject:
2410             self.generate_setitem_code(rhs.py_result(), code)
2411         else:
2412             code.putln(
2413                 "%s = %s;" % (
2414                     self.result(), rhs.result()))
2415         self.generate_subexpr_disposal_code(code)
2416         self.free_subexpr_temps(code)
2417         rhs.generate_disposal_code(code)
2418         rhs.free_temps(code)
2419
2420     def generate_deletion_code(self, code):
2421         self.generate_subexpr_evaluation_code(code)
2422         #if self.type.is_pyobject:
2423         if self.index.type.is_int:
2424             function = "__Pyx_DelItemInt"
2425             index_code = self.index.result()
2426             code.globalstate.use_utility_code(delitem_int_utility_code)
2427         else:
2428             index_code = self.index.py_result()
2429             if self.base.type is dict_type:
2430                 function = "PyDict_DelItem"
2431             else:
2432                 function = "PyObject_DelItem"
2433         code.putln(
2434             "if (%s(%s, %s%s) < 0) %s" % (
2435                 function,
2436                 self.base.py_result(),
2437                 index_code,
2438                 self.extra_index_params(),
2439                 code.error_goto(self.pos)))
2440         self.generate_subexpr_disposal_code(code)
2441         self.free_subexpr_temps(code)
2442
2443     def buffer_lookup_code(self, code):
2444         # Assign indices to temps
2445         index_temps = [code.funcstate.allocate_temp(i.type, manage_ref=False) for i in self.indices]
2446         for temp, index in zip(index_temps, self.indices):
2447             code.putln("%s = %s;" % (temp, index.result()))
2448         # Generate buffer access code using these temps
2449         import Buffer
2450         # The above could happen because child_attrs is wrong somewhere so that
2451         # options are not propagated.
2452         return Buffer.put_buffer_lookup_code(entry=self.base.entry,
2453                                              index_signeds=[i.type.signed for i in self.indices],
2454                                              index_cnames=index_temps,
2455                                              directives=code.globalstate.directives,
2456                                              pos=self.pos, code=code)
2457
2458     def put_nonecheck(self, code):
2459         code.globalstate.use_utility_code(raise_noneindex_error_utility_code)
2460         code.putln("if (%s) {" % code.unlikely("%s == Py_None") % self.base.result_as(PyrexTypes.py_object_type))
2461         code.putln("__Pyx_RaiseNoneIndexingError();")
2462         code.putln(code.error_goto(self.pos))
2463         code.putln("}")
2464
2465 class SliceIndexNode(ExprNode):
2466     #  2-element slice indexing
2467     #
2468     #  base      ExprNode
2469     #  start     ExprNode or None
2470     #  stop      ExprNode or None
2471
2472     subexprs = ['base', 'start', 'stop']
2473
2474     def infer_type(self, env):
2475         base_type = self.base.infer_type(env)
2476         if base_type.is_string:
2477             return bytes_type
2478         elif base_type in (bytes_type, str_type, unicode_type,
2479                            list_type, tuple_type):
2480             return base_type
2481         return py_object_type
2482
2483     def calculate_constant_result(self):
2484         self.constant_result = self.base.constant_result[
2485             self.start.constant_result : self.stop.constant_result]
2486
2487     def compile_time_value(self, denv):
2488         base = self.base.compile_time_value(denv)
2489         if self.start is None:
2490             start = 0
2491         else:
2492             start = self.start.compile_time_value(denv)
2493         if self.stop is None:
2494             stop = None
2495         else:
2496             stop = self.stop.compile_time_value(denv)
2497         try:
2498             return base[start:stop]
2499         except Exception, e:
2500             self.compile_time_value_error(e)
2501
2502     def analyse_target_declaration(self, env):
2503         pass
2504
2505     def analyse_target_types(self, env):
2506         self.analyse_types(env)
2507         # when assigning, we must accept any Python type
2508         if self.type.is_pyobject:
2509             self.type = py_object_type
2510
2511     def analyse_types(self, env):
2512         self.base.analyse_types(env)
2513         if self.start:
2514             self.start.analyse_types(env)
2515         if self.stop:
2516             self.stop.analyse_types(env)
2517         base_type = self.base.type
2518         if base_type.is_string:
2519             self.type = bytes_type
2520         elif base_type.is_ptr:
2521             self.type = base_type
2522         elif base_type.is_array:
2523             # we need a ptr type here instead of an array type, as
2524             # array types can result in invalid type casts in the C
2525             # code
2526             self.type = PyrexTypes.CPtrType(base_type.base_type)
2527         else:
2528             self.base = self.base.coerce_to_pyobject(env)
2529             self.type = py_object_type
2530         if base_type.is_builtin_type:
2531             # slicing builtin types returns something of the same type
2532             self.type = base_type
2533         c_int = PyrexTypes.c_py_ssize_t_type
2534         if self.start:
2535             self.start = self.start.coerce_to(c_int, env)
2536         if self.stop:
2537             self.stop = self.stop.coerce_to(c_int, env)
2538         self.is_temp = 1
2539
2540     nogil_check = Node.gil_error
2541     gil_message = "Slicing Python object"
2542
2543     def generate_result_code(self, code):
2544         if not self.type.is_pyobject:
2545             error(self.pos,
2546                   "Slicing is not currently supported for '%s'." % self.type)
2547             return
2548         if self.base.type.is_string:
2549             if self.stop is None:
2550                 code.putln(
2551                     "%s = PyBytes_FromString(%s + %s); %s" % (
2552                         self.result(),
2553                         self.base.result(),
2554                         self.start_code(),
2555                         code.error_goto_if_null(self.result(), self.pos)))
2556             else:
2557                 code.putln(
2558                     "%s = PyBytes_FromStringAndSize(%s + %s, %s - %s); %s" % (
2559                         self.result(),
2560                         self.base.result(),
2561                         self.start_code(),
2562                         self.stop_code(),
2563                         self.start_code(),
2564                         code.error_goto_if_null(self.result(), self.pos)))
2565         else:
2566             code.putln(
2567                 "%s = __Pyx_PySequence_GetSlice(%s, %s, %s); %s" % (
2568                     self.result(),
2569                     self.base.py_result(),
2570                     self.start_code(),
2571                     self.stop_code(),
2572                     code.error_goto_if_null(self.result(), self.pos)))
2573         code.put_gotref(self.py_result())
2574
2575     def generate_assignment_code(self, rhs, code):
2576         self.generate_subexpr_evaluation_code(code)
2577         if self.type.is_pyobject:
2578             code.put_error_if_neg(self.pos,
2579                 "__Pyx_PySequence_SetSlice(%s, %s, %s, %s)" % (
2580                     self.base.py_result(),
2581                     self.start_code(),
2582                     self.stop_code(),
2583                     rhs.py_result()))
2584         else:
2585             start_offset = ''
2586             if self.start:
2587                 start_offset = self.start_code()
2588                 if start_offset == '0':
2589                     start_offset = ''
2590                 else:
2591                     start_offset += '+'
2592             if rhs.type.is_array:
2593                 array_length = rhs.type.size
2594                 self.generate_slice_guard_code(code, array_length)
2595             else:
2596                 error(self.pos,
2597                       "Slice assignments from pointers are not yet supported.")
2598                 # FIXME: fix the array size according to start/stop
2599                 array_length = self.base.type.size
2600             for i in range(array_length):
2601                 code.putln("%s[%s%s] = %s[%d];" % (
2602                         self.base.result(), start_offset, i,
2603                         rhs.result(), i))
2604         self.generate_subexpr_disposal_code(code)
2605         self.free_subexpr_temps(code)
2606         rhs.generate_disposal_code(code)
2607         rhs.free_temps(code)
2608
2609     def generate_deletion_code(self, code):
2610         if not self.base.type.is_pyobject:
2611             error(self.pos,
2612                   "Deleting slices is only supported for Python types, not '%s'." % self.type)
2613             return
2614         self.generate_subexpr_evaluation_code(code)
2615         code.put_error_if_neg(self.pos,
2616             "__Pyx_PySequence_DelSlice(%s, %s, %s)" % (
2617                 self.base.py_result(),
2618                 self.start_code(),
2619                 self.stop_code()))
2620         self.generate_subexpr_disposal_code(code)
2621         self.free_subexpr_temps(code)
2622
2623     def generate_slice_guard_code(self, code, target_size):
2624         if not self.base.type.is_array:
2625             return
2626         slice_size = self.base.type.size
2627         start = stop = None
2628         if self.stop:
2629             stop = self.stop.result()
2630             try:
2631                 stop = int(stop)
2632                 if stop < 0:
2633                     slice_size = self.base.type.size + stop
2634                 else:
2635                     slice_size = stop
2636                 stop = None
2637             except ValueError:
2638                 pass
2639         if self.start:
2640             start = self.start.result()
2641             try:
2642                 start = int(start)
2643                 if start < 0:
2644                     start = self.base.type.size + start
2645                 slice_size -= start
2646                 start = None
2647             except ValueError:
2648                 pass
2649         check = None
2650         if slice_size < 0:
2651             if target_size > 0:
2652                 error(self.pos, "Assignment to empty slice.")
2653         elif start is None and stop is None:
2654             # we know the exact slice length
2655             if target_size != slice_size:
2656                 error(self.pos, "Assignment to slice of wrong length, expected %d, got %d" % (
2657                         slice_size, target_size))
2658         elif start is not None:
2659             if stop is None:
2660                 stop = slice_size
2661             check = "(%s)-(%s)" % (stop, start)
2662         else: # stop is not None:
2663             check = stop
2664         if check:
2665             code.putln("if (unlikely((%s) != %d)) {" % (check, target_size))
2666             code.putln('PyErr_Format(PyExc_ValueError, "Assignment to slice of wrong length, expected %%"PY_FORMAT_SIZE_T"d, got %%"PY_FORMAT_SIZE_T"d", (Py_ssize_t)%d, (Py_ssize_t)(%s));' % (
2667                         target_size, check))
2668             code.putln(code.error_goto(self.pos))
2669             code.putln("}")
2670
2671     def start_code(self):
2672         if self.start:
2673             return self.start.result()
2674         else:
2675             return "0"
2676
2677     def stop_code(self):
2678         if self.stop:
2679             return self.stop.result()
2680         elif self.base.type.is_array:
2681             return self.base.type.size
2682         else:
2683             return "PY_SSIZE_T_MAX"
2684
2685     def calculate_result_code(self):
2686         # self.result() is not used, but this method must exist
2687         return "<unused>"
2688
2689
2690 class SliceNode(ExprNode):
2691     #  start:stop:step in subscript list
2692     #
2693     #  start     ExprNode
2694     #  stop      ExprNode
2695     #  step      ExprNode
2696
2697     subexprs = ['start', 'stop', 'step']
2698
2699     type = py_object_type
2700     is_temp = 1
2701
2702     def calculate_constant_result(self):
2703         self.constant_result = slice(
2704             self.start.constant_result,
2705             self.stop.constant_result,
2706             self.step.constant_result)
2707
2708     def compile_time_value(self, denv):
2709         start = self.start.compile_time_value(denv)
2710         stop = self.stop.compile_time_value(denv)
2711         step = self.step.compile_time_value(denv)
2712         try:
2713             return slice(start, stop, step)
2714         except Exception, e:
2715             self.compile_time_value_error(e)
2716
2717     def analyse_types(self, env):
2718         self.start.analyse_types(env)
2719         self.stop.analyse_types(env)
2720         self.step.analyse_types(env)
2721         self.start = self.start.coerce_to_pyobject(env)
2722         self.stop = self.stop.coerce_to_pyobject(env)
2723         self.step = self.step.coerce_to_pyobject(env)
2724         if self.start.is_literal and self.stop.is_literal and self.step.is_literal:
2725             self.is_literal = True
2726             self.is_temp = False
2727
2728     gil_message = "Constructing Python slice object"
2729
2730     def calculate_result_code(self):
2731         return self.result_code
2732
2733     def generate_result_code(self, code):
2734         if self.is_literal:
2735             self.result_code = code.get_py_const(py_object_type, 'slice_', cleanup_level=2)
2736             code = code.get_cached_constants_writer()
2737             code.mark_pos(self.pos)
2738
2739         code.putln(
2740             "%s = PySlice_New(%s, %s, %s); %s" % (
2741                 self.result(),
2742                 self.start.py_result(),
2743                 self.stop.py_result(),
2744                 self.step.py_result(),
2745                 code.error_goto_if_null(self.result(), self.pos)))
2746         code.put_gotref(self.py_result())
2747         if self.is_literal:
2748             code.put_giveref(self.py_result())
2749
2750
2751 class CallNode(ExprNode):
2752
2753     # allow overriding the default 'may_be_none' behaviour
2754     may_return_none = None
2755
2756     def may_be_none(self):
2757         if self.may_return_none is not None:
2758             return self.may_return_none
2759         return ExprNode.may_be_none(self)
2760
2761     def analyse_as_type_constructor(self, env):
2762         type = self.function.analyse_as_type(env)
2763         if type and type.is_struct_or_union:
2764             args, kwds = self.explicit_args_kwds()
2765             items = []
2766             for arg, member in zip(args, type.scope.var_entries):
2767                 items.append(DictItemNode(pos=arg.pos, key=StringNode(pos=arg.pos, value=member.name), value=arg))
2768             if kwds:
2769                 items += kwds.key_value_pairs
2770             self.key_value_pairs = items
2771             self.__class__ = DictNode
2772             self.analyse_types(env)
2773             self.coerce_to(type, env)
2774             return True
2775         elif type and type.is_cpp_class:
2776             for arg in self.args:
2777                 arg.analyse_types(env)
2778             constructor = type.scope.lookup("<init>")
2779             self.function = RawCNameExprNode(self.function.pos, constructor.type)
2780             self.function.entry = constructor
2781             self.function.set_cname(type.declaration_code(""))
2782             self.analyse_c_function_call(env)
2783             return True
2784
2785     def is_lvalue(self):
2786         return self.type.is_reference
2787
2788     def nogil_check(self, env):
2789         func_type = self.function_type()
2790         if func_type.is_pyobject:
2791             self.gil_error()
2792         elif not getattr(func_type, 'nogil', False):
2793             self.gil_error()
2794
2795     gil_message = "Calling gil-requiring function"
2796
2797
2798 class SimpleCallNode(CallNode):
2799     #  Function call without keyword, * or ** args.
2800     #
2801     #  function       ExprNode
2802     #  args           [ExprNode]
2803     #  arg_tuple      ExprNode or None     used internally
2804     #  self           ExprNode or None     used internally
2805     #  coerced_self   ExprNode or None     used internally
2806     #  wrapper_call   bool                 used internally
2807     #  has_optional_args   bool            used internally
2808     #  nogil          bool                 used internally
2809
2810     subexprs = ['self', 'coerced_self', 'function', 'args', 'arg_tuple']
2811
2812     self = None
2813     coerced_self = None
2814     arg_tuple = None
2815     wrapper_call = False
2816     has_optional_args = False
2817     nogil = False
2818     analysed = False
2819
2820     def compile_time_value(self, denv):
2821         function = self.function.compile_time_value(denv)
2822         args = [arg.compile_time_value(denv) for arg in self.args]
2823         try:
2824             return function(*args)
2825         except Exception, e:
2826             self.compile_time_value_error(e)
2827
2828     def type_dependencies(self, env):
2829         # TODO: Update when Danilo's C++ code merged in to handle the
2830         # the case of function overloading.
2831         return self.function.type_dependencies(env)
2832
2833     def infer_type(self, env):
2834         function = self.function
2835         func_type = function.infer_type(env)
2836         if isinstance(self.function, NewExprNode):
2837             return PyrexTypes.CPtrType(self.function.class_type)
2838         if func_type.is_ptr:
2839             func_type = func_type.base_type
2840         if func_type.is_cfunction:
2841             return func_type.return_type
2842         elif func_type is type_type:
2843             if function.is_name and function.entry and function.entry.type:
2844                 result_type = function.entry.type
2845                 if result_type.is_extension_type:
2846                     return result_type
2847                 elif result_type.is_builtin_type:
2848                     if function.entry.name == 'float':
2849                         return PyrexTypes.c_double_type
2850                     elif function.entry.name in Builtin.types_that_construct_their_instance:
2851                         return result_type
2852         return py_object_type
2853
2854     def analyse_as_type(self, env):
2855         attr = self.function.as_cython_attribute()
2856         if attr == 'pointer':
2857             if len(self.args) != 1:
2858                 error(self.args.pos, "only one type allowed.")
2859             else:
2860                 type = self.args[0].analyse_as_type(env)
2861                 if not type:
2862                     error(self.args[0].pos, "Unknown type")
2863                 else:
2864                     return PyrexTypes.CPtrType(type)
2865
2866     def explicit_args_kwds(self):
2867         return self.args, None
2868
2869     def analyse_types(self, env):
2870         if self.analyse_as_type_constructor(env):
2871             return
2872         if self.analysed:
2873             return
2874         self.analysed = True
2875         function = self.function
2876         function.is_called = 1
2877         self.function.analyse_types(env)
2878         if function.is_attribute and function.entry and function.entry.is_cmethod:
2879             # Take ownership of the object from which the attribute
2880             # was obtained, because we need to pass it as 'self'.
2881             self.self = function.obj
2882             function.obj = CloneNode(self.self)
2883         func_type = self.function_type()
2884         if func_type.is_pyobject:
2885             self.arg_tuple = TupleNode(self.pos, args = self.args)
2886             self.arg_tuple.analyse_types(env)
2887             self.args = None
2888             if func_type is Builtin.type_type and function.is_name and \
2889                    function.entry and \
2890                    function.entry.is_builtin and \
2891                    function.entry.name in Builtin.types_that_construct_their_instance:
2892                 # calling a builtin type that returns a specific object type
2893                 if function.entry.name == 'float':
2894                     # the following will come true later on in a transform
2895                     self.type = PyrexTypes.c_double_type
2896                     self.result_ctype = PyrexTypes.c_double_type
2897                 else:
2898                     self.type = Builtin.builtin_types[function.entry.name]
2899                     self.result_ctype = py_object_type
2900                 self.may_return_none = False
2901             elif function.is_name and function.type_entry:
2902                 # We are calling an extension type constructor.  As
2903                 # long as we do not support __new__(), the result type
2904                 # is clear
2905                 self.type = function.type_entry.type
2906                 self.result_ctype = py_object_type
2907                 self.may_return_none = False
2908             else:
2909                 self.type = py_object_type
2910             self.is_temp = 1
2911         else:
2912             for arg in self.args:
2913                 arg.analyse_types(env)
2914             if self.self and func_type.args:
2915                 # Coerce 'self' to the type expected by the method.
2916                 self_arg = func_type.args[0]
2917                 if self_arg.not_none: # C methods must do the None test for self at *call* time
2918                     self.self = self.self.as_none_safe_node(
2919                         "'NoneType' object has no attribute '%s'" % self.function.entry.name,
2920                         'PyExc_AttributeError')
2921                 expected_type = self_arg.type
2922                 self.coerced_self = CloneNode(self.self).coerce_to(
2923                     expected_type, env)
2924                 # Insert coerced 'self' argument into argument list.
2925                 self.args.insert(0, self.coerced_self)
2926             self.analyse_c_function_call(env)
2927
2928     def function_type(self):
2929         # Return the type of the function being called, coercing a function
2930         # pointer to a function if necessary.
2931         func_type = self.function.type
2932         if func_type.is_ptr:
2933             func_type = func_type.base_type
2934         return func_type
2935
2936     def is_simple(self):
2937         # C function calls could be considered simple, but they may
2938         # have side-effects that may hit when multiple operations must
2939         # be effected in order, e.g. when constructing the argument
2940         # sequence for a function call or comparing values.
2941         return False
2942
2943     def analyse_c_function_call(self, env):
2944         if self.function.type is error_type:
2945             self.type = error_type
2946             return
2947         if self.function.type.is_cpp_class:
2948             overloaded_entry = self.function.type.scope.lookup("operator()")
2949             if overloaded_entry is None:
2950                 self.type = PyrexTypes.error_type
2951                 self.result_code = "<error>"
2952                 return
2953         elif hasattr(self.function, 'entry'):
2954             overloaded_entry = self.function.entry
2955         else:
2956             overloaded_entry = None
2957         if overloaded_entry:
2958             entry = PyrexTypes.best_match(self.args, overloaded_entry.all_alternatives(), self.pos)
2959             if not entry:
2960                 self.type = PyrexTypes.error_type
2961                 self.result_code = "<error>"
2962                 return
2963             self.function.entry = entry
2964             self.function.type = entry.type
2965             func_type = self.function_type()
2966         else:
2967             func_type = self.function_type()
2968             if not func_type.is_cfunction:
2969                 error(self.pos, "Calling non-function type '%s'" % func_type)
2970                 self.type = PyrexTypes.error_type
2971                 self.result_code = "<error>"
2972                 return
2973         # Check no. of args
2974         max_nargs = len(func_type.args)
2975         expected_nargs = max_nargs - func_type.optional_arg_count
2976         actual_nargs = len(self.args)
2977         if func_type.optional_arg_count and expected_nargs != actual_nargs:
2978             self.has_optional_args = 1
2979             self.is_temp = 1
2980         # Coerce arguments
2981         some_args_in_temps = False
2982         for i in xrange(min(max_nargs, actual_nargs)):
2983             formal_type = func_type.args[i].type
2984             arg = self.args[i].coerce_to(formal_type, env)
2985             if arg.is_temp:
2986                 if i > 0:
2987                     # first argument in temp doesn't impact subsequent arguments
2988                     some_args_in_temps = True
2989             elif arg.type.is_pyobject and not env.nogil:
2990                 if i == 0 and self.self is not None:
2991                     # a method's cloned "self" argument is ok
2992                     pass
2993                 elif arg.is_name and arg.entry and arg.entry.is_local and not arg.entry.in_closure:
2994                     # plain local variables are ok
2995                     pass
2996                 else:
2997                     # we do not safely own the argument's reference,
2998                     # but we must make sure it cannot be collected
2999                     # before we return from the function, so we create
3000                     # an owned temp reference to it
3001                     if i > 0: # first argument doesn't matter
3002                         some_args_in_temps = True
3003                     arg = arg.coerce_to_temp(env)
3004             self.args[i] = arg
3005         # handle additional varargs parameters
3006         for i in xrange(max_nargs, actual_nargs):
3007             arg = self.args[i]
3008             if arg.type.is_pyobject:
3009                 arg_ctype = arg.type.default_coerced_ctype()
3010                 if arg_ctype is None:
3011                     error(self.args[i].pos,
3012                           "Python object cannot be passed as a varargs parameter")
3013                 else:
3014                     self.args[i] = arg = arg.coerce_to(arg_ctype, env)
3015             if arg.is_temp and i > 0:
3016                 some_args_in_temps = True
3017         if some_args_in_temps:
3018             # if some args are temps and others are not, they may get
3019             # constructed in the wrong order (temps first) => make
3020             # sure they are either all temps or all not temps (except
3021             # for the last argument, which is evaluated last in any
3022             # case)
3023             for i in xrange(actual_nargs-1):
3024                 if i == 0 and self.self is not None:
3025                     continue # self is ok
3026                 arg = self.args[i]
3027                 if arg.is_name and arg.entry and (
3028                     (arg.entry.is_local and not arg.entry.in_closure)
3029                     or arg.entry.type.is_cfunction):
3030                     # local variables and C functions are safe
3031                     pass
3032                 elif env.nogil and arg.type.is_pyobject:
3033                     # can't copy a Python reference into a temp in nogil
3034                     # env (this is safe: a construction would fail in
3035                     # nogil anyway)
3036                     pass
3037                 else:
3038                     self.args[i] = arg.coerce_to_temp(env)
3039         # Calc result type and code fragment
3040         if isinstance(self.function, NewExprNode):
3041             self.type = PyrexTypes.CPtrType(self.function.class_type)
3042         else:
3043             self.type = func_type.return_type
3044         if self.type.is_pyobject:
3045             self.result_ctype = py_object_type
3046             self.is_temp = 1
3047         elif func_type.exception_value is not None \
3048                  or func_type.exception_check:
3049             self.is_temp = 1
3050         # Called in 'nogil' context?
3051         self.nogil = env.nogil
3052         if (self.nogil and
3053             func_type.exception_check and
3054             func_type.exception_check != '+'):
3055             env.use_utility_code(pyerr_occurred_withgil_utility_code)
3056         # C++ exception handler
3057         if func_type.exception_check == '+':
3058             if func_type.exception_value is None:
3059                 env.use_utility_code(cpp_exception_utility_code)
3060
3061     def calculate_result_code(self):
3062         return self.c_call_code()
3063
3064     def c_call_code(self):
3065         func_type = self.function_type()
3066         if self.type is PyrexTypes.error_type or not func_type.is_cfunction:
3067             return "<error>"
3068         formal_args = func_type.args
3069         arg_list_code = []
3070         args = list(zip(formal_args, self.args))
3071         max_nargs = len(func_type.args)
3072         expected_nargs = max_nargs - func_type.optional_arg_count
3073         actual_nargs = len(self.args)
3074         for formal_arg, actual_arg in args[:expected_nargs]:
3075                 arg_code = actual_arg.result_as(formal_arg.type)
3076                 arg_list_code.append(arg_code)
3077
3078         if func_type.is_overridable:
3079             arg_list_code.append(str(int(self.wrapper_call or self.function.entry.is_unbound_cmethod)))
3080
3081         if func_type.optional_arg_count:
3082             if expected_nargs == actual_nargs:
3083                 optional_args = 'NULL'
3084             else:
3085                 optional_args = "&%s" % self.opt_arg_struct
3086             arg_list_code.append(optional_args)
3087
3088         for actual_arg in self.args[len(formal_args):]:
3089             arg_list_code.append(actual_arg.result())
3090         result = "%s(%s)" % (self.function.result(),
3091             ', '.join(arg_list_code))
3092         return result
3093
3094     def generate_result_code(self, code):
3095         func_type = self.function_type()
3096         if func_type.is_pyobject:
3097             arg_code = self.arg_tuple.py_result()
3098             code.putln(
3099                 "%s = PyObject_Call(%s, %s, NULL); %s" % (
3100                     self.result(),
3101                     self.function.py_result(),
3102                     arg_code,
3103                     code.error_goto_if_null(self.result(), self.pos)))
3104             code.put_gotref(self.py_result())
3105         elif func_type.is_cfunction:
3106             if self.has_optional_args:
3107                 actual_nargs = len(self.args)
3108                 expected_nargs = len(func_type.args) - func_type.optional_arg_count
3109                 self.opt_arg_struct = code.funcstate.allocate_temp(
3110                     func_type.op_arg_struct.base_type, manage_ref=True)
3111                 code.putln("%s.%s = %s;" % (
3112                         self.opt_arg_struct,
3113                         Naming.pyrex_prefix + "n",
3114                         len(self.args) - expected_nargs))
3115                 args = list(zip(func_type.args, self.args))
3116                 for formal_arg, actual_arg in args[expected_nargs:actual_nargs]:
3117                     code.putln("%s.%s = %s;" % (
3118                             self.opt_arg_struct,
3119                             func_type.opt_arg_cname(formal_arg.name),
3120                             actual_arg.result_as(formal_arg.type)))
3121             exc_checks = []
3122             if self.type.is_pyobject and self.is_temp:
3123                 exc_checks.append("!%s" % self.result())
3124             else:
3125                 exc_val = func_type.exception_value
3126                 exc_check = func_type.exception_check
3127                 if exc_val is not None:
3128                     exc_checks.append("%s == %s" % (self.result(), exc_val))
3129                 if exc_check:
3130                     if self.nogil:
3131                         exc_checks.append("__Pyx_ErrOccurredWithGIL()")
3132                     else:
3133                         exc_checks.append("PyErr_Occurred()")
3134             if self.is_temp or exc_checks:
3135                 rhs = self.c_call_code()
3136                 if self.result():
3137                     lhs = "%s = " % self.result()
3138                     if self.is_temp and self.type.is_pyobject:
3139                         #return_type = self.type # func_type.return_type
3140                         #print "SimpleCallNode.generate_result_code: casting", rhs, \
3141                         #    "from", return_type, "to pyobject" ###
3142                         rhs = typecast(py_object_type, self.type, rhs)
3143                 else:
3144                     lhs = ""
3145                 if func_type.exception_check == '+':
3146                     if func_type.exception_value is None:
3147                         raise_py_exception = "__Pyx_CppExn2PyErr()"
3148                     elif func_type.exception_value.type.is_pyobject:
3149                         raise_py_exception = ' try { throw; } catch(const std::exception& exn) { PyErr_SetString(%s, exn.what()); } catch(...) { PyErr_SetNone(%s); }' % (
3150                             func_type.exception_value.entry.cname,
3151                             func_type.exception_value.entry.cname)
3152                     else:
3153                         raise_py_exception = '%s(); if (!PyErr_Occurred()) PyErr_SetString(PyExc_RuntimeError , "Error converting c++ exception.")' % func_type.exception_value.entry.cname
3154                     if self.nogil:
3155                         raise_py_exception = 'Py_BLOCK_THREADS; %s; Py_UNBLOCK_THREADS' % raise_py_exception
3156                     code.putln(
3157                     "try {%s%s;} catch(...) {%s; %s}" % (
3158                         lhs,
3159                         rhs,
3160                         raise_py_exception,
3161                         code.error_goto(self.pos)))
3162                 else:
3163                     if exc_checks:
3164                         goto_error = code.error_goto_if(" && ".join(exc_checks), self.pos)
3165                     else:
3166                         goto_error = ""
3167                     code.putln("%s%s; %s" % (lhs, rhs, goto_error))
3168                 if self.type.is_pyobject and self.result():
3169                     code.put_gotref(self.py_result())
3170             if self.has_optional_args:
3171                 code.funcstate.release_temp(self.opt_arg_struct)
3172
3173
3174 class PythonCapiFunctionNode(ExprNode):
3175     subexprs = []
3176     def __init__(self, pos, py_name, cname, func_type, utility_code = None):
3177         self.pos = pos
3178         self.name = py_name
3179         self.cname = cname
3180         self.type = func_type
3181         self.utility_code = utility_code
3182
3183     def analyse_types(self, env):
3184         pass
3185
3186     def generate_result_code(self, code):
3187         if self.utility_code:
3188             code.globalstate.use_utility_code(self.utility_code)
3189
3190     def calculate_result_code(self):
3191         return self.cname
3192
3193 class PythonCapiCallNode(SimpleCallNode):
3194     # Python C-API Function call (only created in transforms)
3195
3196     # By default, we assume that the call never returns None, as this
3197     # is true for most C-API functions in CPython.  If this does not
3198     # apply to a call, set the following to True (or None to inherit
3199     # the default behaviour).
3200     may_return_none = False
3201
3202     def __init__(self, pos, function_name, func_type,
3203                  utility_code = None, py_name=None, **kwargs):
3204         self.type = func_type.return_type
3205         self.result_ctype = self.type
3206         self.function = PythonCapiFunctionNode(
3207             pos, py_name, function_name, func_type,
3208             utility_code = utility_code)
3209         # call this last so that we can override the constructed
3210         # attributes above with explicit keyword arguments if required
3211         SimpleCallNode.__init__(self, pos, **kwargs)
3212
3213
3214 class GeneralCallNode(CallNode):
3215     #  General Python function call, including keyword,
3216     #  * and ** arguments.
3217     #
3218     #  function         ExprNode
3219     #  positional_args  ExprNode          Tuple of positional arguments
3220     #  keyword_args     ExprNode or None  Dict of keyword arguments
3221     #  starstar_arg     ExprNode or None  Dict of extra keyword args
3222
3223     type = py_object_type
3224
3225     subexprs = ['function', 'positional_args', 'keyword_args', 'starstar_arg']
3226
3227     nogil_check = Node.gil_error
3228
3229     def compile_time_value(self, denv):
3230         function = self.function.compile_time_value(denv)
3231         positional_args = self.positional_args.compile_time_value(denv)
3232         keyword_args = self.keyword_args.compile_time_value(denv)
3233         starstar_arg = self.starstar_arg.compile_time_value(denv)
3234         try:
3235             keyword_args.update(starstar_arg)
3236             return function(*positional_args, **keyword_args)
3237         except Exception, e:
3238             self.compile_time_value_error(e)
3239
3240     def explicit_args_kwds(self):
3241         if self.starstar_arg or not isinstance(self.positional_args, TupleNode):
3242             raise CompileError(self.pos,
3243                 'Compile-time keyword arguments must be explicit.')
3244         return self.positional_args.args, self.keyword_args
3245
3246     def analyse_types(self, env):
3247         if self.analyse_as_type_constructor(env):
3248             return
3249         self.function.analyse_types(env)
3250         self.positional_args.analyse_types(env)
3251         if self.keyword_args:
3252             self.keyword_args.analyse_types(env)
3253         if self.starstar_arg:
3254             self.starstar_arg.analyse_types(env)
3255         if not self.function.type.is_pyobject:
3256             if self.function.type.is_error:
3257                 self.type = error_type
3258                 return
3259             if hasattr(self.function, 'entry') and not self.function.entry.as_variable:
3260                 error(self.pos, "Keyword and starred arguments not allowed in cdef functions.")
3261             else:
3262                 self.function = self.function.coerce_to_pyobject(env)
3263         self.positional_args = \
3264             self.positional_args.coerce_to_pyobject(env)
3265         if self.starstar_arg:
3266             self.starstar_arg = \
3267                 self.starstar_arg.coerce_to_pyobject(env)
3268         function = self.function
3269         if function.is_name and function.type_entry:
3270             # We are calling an extension type constructor.  As long
3271             # as we do not support __new__(), the result type is clear
3272             self.type = function.type_entry.type
3273             self.result_ctype = py_object_type
3274             self.may_return_none = False
3275         else:
3276             self.type = py_object_type
3277         self.is_temp = 1
3278
3279     def generate_result_code(self, code):
3280         if self.type.is_error: return
3281         kwargs_call_function = "PyEval_CallObjectWithKeywords"
3282         if self.keyword_args and self.starstar_arg:
3283             code.put_error_if_neg(self.pos,
3284                 "PyDict_Update(%s, %s)" % (
3285                     self.keyword_args.py_result(),
3286                     self.starstar_arg.py_result()))
3287             keyword_code = self.keyword_args.py_result()
3288         elif self.keyword_args:
3289             keyword_code = self.keyword_args.py_result()
3290         elif self.starstar_arg:
3291             keyword_code = self.starstar_arg.py_result()
3292             if self.starstar_arg.type is not Builtin.dict_type:
3293                 # CPython supports calling functions with non-dicts, so do we
3294                 code.globalstate.use_utility_code(kwargs_call_utility_code)
3295                 kwargs_call_function = "__Pyx_PyEval_CallObjectWithKeywords"
3296         else:
3297             keyword_code = None
3298         if not keyword_code:
3299             call_code = "PyObject_Call(%s, %s, NULL)" % (
3300                 self.function.py_result(),
3301                 self.positional_args.py_result())
3302         else:
3303             call_code = "%s(%s, %s, %s)" % (
3304                 kwargs_call_function,
3305                 self.function.py_result(),
3306                 self.positional_args.py_result(),
3307                 keyword_code)
3308         code.putln(
3309             "%s = %s; %s" % (
3310                 self.result(),
3311                 call_code,
3312                 code.error_goto_if_null(self.result(), self.pos)))
3313         code.put_gotref(self.py_result())
3314
3315
3316 class AsTupleNode(ExprNode):
3317     #  Convert argument to tuple. Used for normalising
3318     #  the * argument of a function call.
3319     #
3320     #  arg    ExprNode
3321
3322     subexprs = ['arg']
3323
3324     def calculate_constant_result(self):
3325         self.constant_result = tuple(self.base.constant_result)
3326
3327     def compile_time_value(self, denv):
3328         arg = self.arg.compile_time_value(denv)
3329         try:
3330             return tuple(arg)
3331         except Exception, e:
3332             self.compile_time_value_error(e)
3333
3334     def analyse_types(self, env):
3335         self.arg.analyse_types(env)
3336         self.arg = self.arg.coerce_to_pyobject(env)
3337         self.type = tuple_type
3338         self.is_temp = 1
3339
3340     def may_be_none(self):
3341         return False
3342
3343     nogil_check = Node.gil_error
3344     gil_message = "Constructing Python tuple"
3345
3346     def generate_result_code(self, code):
3347         code.putln(
3348             "%s = PySequence_Tuple(%s); %s" % (
3349                 self.result(),
3350                 self.arg.py_result(),
3351                 code.error_goto_if_null(self.result(), self.pos)))
3352         code.put_gotref(self.py_result())
3353
3354
3355 class AttributeNode(ExprNode):
3356     #  obj.attribute
3357     #
3358     #  obj          ExprNode
3359     #  attribute    string
3360     #  needs_none_check boolean        Used if obj is an extension type.
3361     #                                  If set to True, it is known that the type is not None.
3362     #
3363     #  Used internally:
3364     #
3365     #  is_py_attr           boolean   Is a Python getattr operation
3366     #  member               string    C name of struct member
3367     #  is_called            boolean   Function call is being done on result
3368     #  entry                Entry     Symbol table entry of attribute
3369
3370     is_attribute = 1
3371     subexprs = ['obj']
3372
3373     type = PyrexTypes.error_type
3374     entry = None
3375     is_called = 0
3376     needs_none_check = True
3377
3378     def as_cython_attribute(self):
3379         if isinstance(self.obj, NameNode) and self.obj.is_cython_module:
3380             return self.attribute
3381         cy = self.obj.as_cython_attribute()
3382         if cy:
3383             return "%s.%s" % (cy, self.attribute)
3384
3385     def coerce_to(self, dst_type, env):
3386         #  If coercing to a generic pyobject and this is a cpdef function
3387         #  we can create the corresponding attribute
3388         if dst_type is py_object_type:
3389             entry = self.entry
3390             if entry and entry.is_cfunction and entry.as_variable:
3391                 # must be a cpdef function
3392                 self.is_temp = 1
3393                 self.entry = entry.as_variable
3394                 self.analyse_as_python_attribute(env)
3395                 return self
3396         return ExprNode.coerce_to(self, dst_type, env)
3397
3398     def calculate_constant_result(self):
3399         attr = self.attribute
3400         if attr.startswith("__") and attr.endswith("__"):
3401             return
3402         self.constant_result = getattr(self.obj.constant_result, attr)
3403
3404     def compile_time_value(self, denv):
3405         attr = self.attribute
3406         if attr.startswith("__") and attr.endswith("__"):
3407             error(self.pos,
3408                   "Invalid attribute name '%s' in compile-time expression" % attr)
3409             return None
3410         obj = self.obj.compile_time_value(denv)
3411         try:
3412             return getattr(obj, attr)
3413         except Exception, e:
3414             self.compile_time_value_error(e)
3415
3416     def type_dependencies(self, env):
3417         return self.obj.type_dependencies(env)
3418
3419     def infer_type(self, env):
3420         if self.analyse_as_cimported_attribute(env, 0):
3421             return self.entry.type
3422         elif self.analyse_as_unbound_cmethod(env):
3423             return self.entry.type
3424         else:
3425             obj_type = self.obj.infer_type(env)
3426             self.analyse_attribute(env, obj_type = obj_type)
3427             if obj_type.is_builtin_type and self.type.is_cfunction:
3428                 # special case: C-API replacements for C methods of
3429                 # builtin types cannot be inferred as C functions as
3430                 # that would prevent their use as bound methods
3431                 self.type = py_object_type
3432                 return py_object_type
3433             return self.type
3434
3435     def analyse_target_declaration(self, env):
3436         pass
3437
3438     def analyse_target_types(self, env):
3439         self.analyse_types(env, target = 1)
3440
3441     def analyse_types(self, env, target = 0):
3442         if self.analyse_as_cimported_attribute(env, target):
3443             return
3444         if not target and self.analyse_as_unbound_cmethod(env):
3445             return
3446         self.analyse_as_ordinary_attribute(env, target)
3447
3448     def analyse_as_cimported_attribute(self, env, target):
3449         # Try to interpret this as a reference to an imported
3450         # C const, type, var or function. If successful, mutates
3451         # this node into a NameNode and returns 1, otherwise
3452         # returns 0.
3453         module_scope = self.obj.analyse_as_module(env)
3454         if module_scope:
3455             entry = module_scope.lookup_here(self.attribute)
3456             if entry and (
3457                 entry.is_cglobal or entry.is_cfunction
3458                 or entry.is_type or entry.is_const):
3459                     self.mutate_into_name_node(env, entry, target)
3460                     return 1
3461         return 0
3462
3463     def analyse_as_unbound_cmethod(self, env):
3464         # Try to interpret this as a reference to an unbound
3465         # C method of an extension type. If successful, mutates
3466         # this node into a NameNode and returns 1, otherwise
3467         # returns 0.
3468         type = self.obj.analyse_as_extension_type(env)
3469         if type:
3470             entry = type.scope.lookup_here(self.attribute)
3471             if entry and entry.is_cmethod:
3472                 # Create a temporary entry describing the C method
3473                 # as an ordinary function.
3474                 ubcm_entry = Symtab.Entry(entry.name,
3475                     "%s->%s" % (type.vtabptr_cname, entry.cname),
3476                     entry.type)
3477                 ubcm_entry.is_cfunction = 1
3478                 ubcm_entry.func_cname = entry.func_cname
3479                 ubcm_entry.is_unbound_cmethod = 1
3480                 self.mutate_into_name_node(env, ubcm_entry, None)
3481                 return 1
3482         return 0
3483
3484     def analyse_as_type(self, env):
3485         module_scope = self.obj.analyse_as_module(env)
3486         if module_scope:
3487             return module_scope.lookup_type(self.attribute)
3488         if not isinstance(self.obj, (UnicodeNode, StringNode, BytesNode)):
3489             base_type = self.obj.analyse_as_type(env)
3490             if base_type and hasattr(base_type, 'scope') and base_type.scope is not None:
3491                 return base_type.scope.lookup_type(self.attribute)
3492         return None
3493
3494     def analyse_as_extension_type(self, env):
3495         # Try to interpret this as a reference to an extension type
3496         # in a cimported module. Returns the extension type, or None.
3497         module_scope = self.obj.analyse_as_module(env)
3498         if module_scope:
3499             entry = module_scope.lookup_here(self.attribute)
3500             if entry and entry.is_type and entry.type.is_extension_type:
3501                 return entry.type
3502         return None
3503
3504     def analyse_as_module(self, env):
3505         # Try to interpret this as a reference to a cimported module
3506         # in another cimported module. Returns the module scope, or None.
3507         module_scope = self.obj.analyse_as_module(env)
3508         if module_scope:
3509             entry = module_scope.lookup_here(self.attribute)
3510             if entry and entry.as_module:
3511                 return entry.as_module
3512         return None
3513
3514     def mutate_into_name_node(self, env, entry, target):
3515         # Mutate this node into a NameNode and complete the
3516         # analyse_types phase.
3517         self.__class__ = NameNode
3518         self.name = self.attribute
3519         self.entry = entry
3520         del self.obj
3521         del self.attribute
3522         if target:
3523             NameNode.analyse_target_types(self, env)
3524         else:
3525             NameNode.analyse_rvalue_entry(self, env)
3526
3527     def analyse_as_ordinary_attribute(self, env, target):
3528         self.obj.analyse_types(env)
3529         self.analyse_attribute(env)
3530         if self.entry and self.entry.is_cmethod and not self.is_called:
3531 #            error(self.pos, "C method can only be called")
3532             pass
3533         ## Reference to C array turns into pointer to first element.
3534         #while self.type.is_array:
3535         #    self.type = self.type.element_ptr_type()
3536         if self.is_py_attr:
3537             if not target:
3538                 self.is_temp = 1
3539                 self.result_ctype = py_object_type
3540         elif target and self.obj.type.is_builtin_type:
3541             error(self.pos, "Assignment to an immutable object field")
3542
3543     def analyse_attribute(self, env, obj_type = None):
3544         # Look up attribute and set self.type and self.member.
3545         self.is_py_attr = 0
3546         self.member = self.attribute
3547         if obj_type is None:
3548             if self.obj.type.is_string:
3549                 self.obj = self.obj.coerce_to_pyobject(env)
3550             obj_type = self.obj.type
3551         else:
3552             if obj_type.is_string:
3553                 obj_type = py_object_type
3554         if obj_type.is_ptr or obj_type.is_array:
3555             obj_type = obj_type.base_type
3556             self.op = "->"
3557         elif obj_type.is_extension_type or obj_type.is_builtin_type:
3558             self.op = "->"
3559         else:
3560             self.op = "."
3561         if obj_type.has_attributes:
3562             entry = None
3563             if obj_type.attributes_known():
3564                 entry = obj_type.scope.lookup_here(self.attribute)
3565                 if entry and entry.is_member:
3566                     entry = None
3567             else:
3568                 error(self.pos,
3569                     "Cannot select attribute of incomplete type '%s'"
3570                     % obj_type)
3571                 self.type = PyrexTypes.error_type
3572                 return
3573             self.entry = entry
3574             if entry:
3575                 if obj_type.is_extension_type and entry.name == "__weakref__":
3576                     error(self.pos, "Illegal use of special attribute __weakref__")
3577                 # methods need the normal attribute lookup
3578                 # because they do not have struct entries
3579                 if entry.is_variable or entry.is_cmethod:
3580                     self.type = entry.type
3581                     self.member = entry.cname
3582                     return
3583                 else:
3584                     # If it's not a variable or C method, it must be a Python
3585                     # method of an extension type, so we treat it like a Python
3586                     # attribute.
3587                     pass
3588         # If we get here, the base object is not a struct/union/extension
3589         # type, or it is an extension type and the attribute is either not
3590         # declared or is declared as a Python method. Treat it as a Python
3591         # attribute reference.
3592         self.analyse_as_python_attribute(env, obj_type)
3593
3594     def analyse_as_python_attribute(self, env, obj_type = None):
3595         if obj_type is None:
3596             obj_type = self.obj.type
3597         self.member = self.attribute
3598         self.type = py_object_type
3599         self.is_py_attr = 1
3600         if not obj_type.is_pyobject and not obj_type.is_error:
3601             if obj_type.can_coerce_to_pyobject(env):
3602                 self.obj = self.obj.coerce_to_pyobject(env)
3603             else:
3604                 error(self.pos,
3605                       "Object of type '%s' has no attribute '%s'" %
3606                       (obj_type, self.attribute))
3607
3608     def nogil_check(self, env):
3609         if self.is_py_attr:
3610             self.gil_error()
3611
3612     gil_message = "Accessing Python attribute"
3613
3614     def is_simple(self):
3615         if self.obj:
3616             return self.result_in_temp() or self.obj.is_simple()
3617         else:
3618             return NameNode.is_simple(self)
3619
3620     def is_lvalue(self):
3621         if self.obj:
3622             return 1
3623         else:
3624             return NameNode.is_lvalue(self)
3625
3626     def is_ephemeral(self):
3627         if self.obj:
3628             return self.obj.is_ephemeral()
3629         else:
3630             return NameNode.is_ephemeral(self)
3631
3632     def calculate_result_code(self):
3633         #print "AttributeNode.calculate_result_code:", self.member ###
3634         #print "...obj node =", self.obj, "code", self.obj.result() ###
3635         #print "...obj type", self.obj.type, "ctype", self.obj.ctype() ###
3636         obj = self.obj
3637         obj_code = obj.result_as(obj.type)
3638         #print "...obj_code =", obj_code ###
3639         if self.entry and self.entry.is_cmethod:
3640             if obj.type.is_extension_type:
3641                 return "((struct %s *)%s%s%s)->%s" % (
3642                     obj.type.vtabstruct_cname, obj_code, self.op,
3643                     obj.type.vtabslot_cname, self.member)
3644             else:
3645                 return self.member
3646         elif obj.type.is_complex:
3647             return "__Pyx_C%s(%s)" % (self.member.upper(), obj_code)
3648         else:
3649             if obj.type.is_builtin_type and self.entry and self.entry.is_variable:
3650                 # accessing a field of a builtin type, need to cast better than result_as() does
3651                 obj_code = obj.type.cast_code(obj.result(), to_object_struct = True)
3652             return "%s%s%s" % (obj_code, self.op, self.member)
3653
3654     def generate_result_code(self, code):
3655         interned_attr_cname = code.intern_identifier(self.attribute)
3656         if self.is_py_attr:
3657             code.putln(
3658                 '%s = PyObject_GetAttr(%s, %s); %s' % (
3659                     self.result(),
3660                     self.obj.py_result(),
3661                     interned_attr_cname,
3662                     code.error_goto_if_null(self.result(), self.pos)))
3663             code.put_gotref(self.py_result())
3664         else:
3665             # result_code contains what is needed, but we may need to insert
3666             # a check and raise an exception
3667             if (self.obj.type.is_extension_type
3668                   and self.needs_none_check
3669                   and code.globalstate.directives['nonecheck']):
3670                 self.put_nonecheck(code)
3671
3672     def generate_assignment_code(self, rhs, code):
3673         interned_attr_cname = code.intern_identifier(self.attribute)
3674         self.obj.generate_evaluation_code(code)
3675         if self.is_py_attr:
3676             code.put_error_if_neg(self.pos,
3677                 'PyObject_SetAttr(%s, %s, %s)' % (
3678                     self.obj.py_result(),
3679                     interned_attr_cname,
3680                     rhs.py_result()))
3681             rhs.generate_disposal_code(code)
3682             rhs.free_temps(code)
3683         elif self.obj.type.is_complex:
3684             code.putln("__Pyx_SET_C%s(%s, %s);" % (
3685                 self.member.upper(),
3686                 self.obj.result_as(self.obj.type),
3687                 rhs.result_as(self.ctype())))
3688         else:
3689             if (self.obj.type.is_extension_type
3690                   and self.needs_none_check
3691                   and code.globalstate.directives['nonecheck']):
3692                 self.put_nonecheck(code)
3693
3694             select_code = self.result()
3695             if self.type.is_pyobject and self.use_managed_ref:
3696                 rhs.make_owned_reference(code)
3697                 code.put_giveref(rhs.py_result())
3698                 code.put_gotref(select_code)
3699                 code.put_decref(select_code, self.ctype())
3700             code.putln(
3701                 "%s = %s;" % (
3702                     select_code,
3703                     rhs.result_as(self.ctype())))
3704                     #rhs.result()))
3705             rhs.generate_post_assignment_code(code)
3706             rhs.free_temps(code)
3707         self.obj.generate_disposal_code(code)
3708         self.obj.free_temps(code)
3709
3710     def generate_deletion_code(self, code):
3711         interned_attr_cname = code.intern_identifier(self.attribute)
3712         self.obj.generate_evaluation_code(code)
3713         if self.is_py_attr or (isinstance(self.entry.scope, Symtab.PropertyScope)
3714                                and u'__del__' in self.entry.scope.entries):
3715             code.put_error_if_neg(self.pos,
3716                 'PyObject_DelAttr(%s, %s)' % (
3717                     self.obj.py_result(),
3718                     interned_attr_cname))
3719         else:
3720             error(self.pos, "Cannot delete C attribute of extension type")
3721         self.obj.generate_disposal_code(code)
3722         self.obj.free_temps(code)
3723
3724     def annotate(self, code):
3725         if self.is_py_attr:
3726             code.annotate(self.pos, AnnotationItem('py_attr', 'python attribute', size=len(self.attribute)))
3727         else:
3728             code.annotate(self.pos, AnnotationItem('c_attr', 'c attribute', size=len(self.attribute)))
3729
3730     def put_nonecheck(self, code):
3731         code.globalstate.use_utility_code(raise_noneattr_error_utility_code)
3732         code.putln("if (%s) {" % code.unlikely("%s == Py_None") % self.obj.result_as(PyrexTypes.py_object_type))
3733         code.putln("__Pyx_RaiseNoneAttributeError(\"%s\");" % self.attribute)
3734         code.putln(code.error_goto(self.pos))
3735         code.putln("}")
3736
3737
3738 #-------------------------------------------------------------------
3739 #
3740 #  Constructor nodes
3741 #
3742 #-------------------------------------------------------------------
3743
3744 class StarredTargetNode(ExprNode):
3745     #  A starred expression like "*a"
3746     #
3747     #  This is only allowed in sequence assignment targets such as
3748     #
3749     #      a, *b = (1,2,3,4)    =>     a = 1 ; b = [2,3,4]
3750     #
3751     #  and will be removed during type analysis (or generate an error
3752     #  if it's found at unexpected places).
3753     #
3754     #  target          ExprNode
3755
3756     subexprs = ['target']
3757     is_starred = 1
3758     type = py_object_type
3759     is_temp = 1
3760
3761     def __init__(self, pos, target):
3762         self.pos = pos
3763         self.target = target
3764
3765     def analyse_declarations(self, env):
3766         error(self.pos, "can use starred expression only as assignment target")
3767         self.target.analyse_declarations(env)
3768
3769     def analyse_types(self, env):
3770         error(self.pos, "can use starred expression only as assignment target")
3771         self.target.analyse_types(env)
3772         self.type = self.target.type
3773
3774     def analyse_target_declaration(self, env):
3775         self.target.analyse_target_declaration(env)
3776
3777     def analyse_target_types(self, env):
3778         self.target.analyse_target_types(env)
3779         self.type = self.target.type
3780
3781     def calculate_result_code(self):
3782         return ""
3783
3784     def generate_result_code(self, code):
3785         pass
3786
3787
3788 class SequenceNode(ExprNode):
3789     #  Base class for list and tuple constructor nodes.
3790     #  Contains common code for performing sequence unpacking.
3791     #
3792     #  args                    [ExprNode]
3793     #  iterator                ExprNode
3794     #  unpacked_items          [ExprNode] or None
3795     #  coerced_unpacked_items  [ExprNode] or None
3796
3797     subexprs = ['args']
3798
3799     is_sequence_constructor = 1
3800     unpacked_items = None
3801
3802     def compile_time_value_list(self, denv):
3803         return [arg.compile_time_value(denv) for arg in self.args]
3804
3805     def replace_starred_target_node(self):
3806         # replace a starred node in the targets by the contained expression
3807         self.starred_assignment = False
3808         args = []
3809         for arg in self.args:
3810             if arg.is_starred:
3811                 if self.starred_assignment:
3812                     error(arg.pos, "more than 1 starred expression in assignment")
3813                 self.starred_assignment = True
3814                 arg = arg.target
3815                 arg.is_starred = True
3816             args.append(arg)
3817         self.args = args
3818
3819     def analyse_target_declaration(self, env):
3820         self.replace_starred_target_node()
3821         for arg in self.args:
3822             arg.analyse_target_declaration(env)
3823
3824     def analyse_types(self, env, skip_children=False):
3825         for i in range(len(self.args)):
3826             arg = self.args[i]
3827             if not skip_children: arg.analyse_types(env)
3828             self.args[i] = arg.coerce_to_pyobject(env)
3829         self.is_temp = 1
3830         # not setting self.type here, subtypes do this
3831
3832     def may_be_none(self):
3833         return False
3834
3835     def analyse_target_types(self, env):
3836         self.iterator = PyTempNode(self.pos, env)
3837         self.unpacked_items = []
3838         self.coerced_unpacked_items = []
3839         for arg in self.args:
3840             arg.analyse_target_types(env)
3841             if arg.is_starred:
3842                 if not arg.type.assignable_from(Builtin.list_type):
3843                     error(arg.pos,
3844                           "starred target must have Python object (list) type")
3845                 if arg.type is py_object_type:
3846                     arg.type = Builtin.list_type
3847             unpacked_item = PyTempNode(self.pos, env)
3848             coerced_unpacked_item = unpacked_item.coerce_to(arg.type, env)
3849             self.unpacked_items.append(unpacked_item)
3850             self.coerced_unpacked_items.append(coerced_unpacked_item)
3851         self.type = py_object_type
3852
3853     def generate_result_code(self, code):
3854         self.generate_operation_code(code)
3855
3856     def generate_assignment_code(self, rhs, code):
3857         if self.starred_assignment:
3858             self.generate_starred_assignment_code(rhs, code)
3859         else:
3860             self.generate_parallel_assignment_code(rhs, code)
3861
3862         for item in self.unpacked_items:
3863             item.release(code)
3864         rhs.free_temps(code)
3865
3866     def generate_parallel_assignment_code(self, rhs, code):
3867         # Need to work around the fact that generate_evaluation_code
3868         # allocates the temps in a rather hacky way -- the assignment
3869         # is evaluated twice, within each if-block.
3870
3871         if rhs.type is tuple_type:
3872             tuple_check = "likely(%s != Py_None)"
3873         else:
3874             tuple_check = "PyTuple_CheckExact(%s)"
3875         code.putln(
3876             "if (%s && likely(PyTuple_GET_SIZE(%s) == %s)) {" % (
3877                 tuple_check % rhs.py_result(),
3878                 rhs.py_result(),
3879                 len(self.args)))
3880         code.putln("PyObject* tuple = %s;" % rhs.py_result())
3881         for item in self.unpacked_items:
3882             item.allocate(code)
3883         for i in range(len(self.args)):
3884             item = self.unpacked_items[i]
3885             code.put(
3886                 "%s = PyTuple_GET_ITEM(tuple, %s); " % (
3887                     item.result(),
3888                     i))
3889             code.put_incref(item.result(), item.ctype())
3890             value_node = self.coerced_unpacked_items[i]
3891             value_node.generate_evaluation_code(code)
3892         rhs.generate_disposal_code(code)
3893
3894         for i in range(len(self.args)):
3895             self.args[i].generate_assignment_code(
3896                 self.coerced_unpacked_items[i], code)
3897
3898         code.putln("} else {")
3899
3900         if rhs.type is tuple_type:
3901             code.globalstate.use_utility_code(tuple_unpacking_error_code)
3902             code.putln("__Pyx_UnpackTupleError(%s, %s);" % (
3903                         rhs.py_result(), len(self.args)))
3904             code.putln(code.error_goto(self.pos))
3905         else:
3906             code.globalstate.use_utility_code(unpacking_utility_code)
3907
3908             self.iterator.allocate(code)
3909             code.putln(
3910                 "%s = PyObject_GetIter(%s); %s" % (
3911                     self.iterator.result(),
3912                     rhs.py_result(),
3913                     code.error_goto_if_null(self.iterator.result(), self.pos)))
3914             code.put_gotref(self.iterator.py_result())
3915             rhs.generate_disposal_code(code)
3916             for i in range(len(self.args)):
3917                 item = self.unpacked_items[i]
3918                 unpack_code = "__Pyx_UnpackItem(%s, %d)" % (
3919                     self.iterator.py_result(), i)
3920                 code.putln(
3921                     "%s = %s; %s" % (
3922                         item.result(),
3923                         typecast(item.ctype(), py_object_type, unpack_code),
3924                         code.error_goto_if_null(item.result(), self.pos)))
3925                 code.put_gotref(item.py_result())
3926                 value_node = self.coerced_unpacked_items[i]
3927                 value_node.generate_evaluation_code(code)
3928             code.put_error_if_neg(self.pos, "__Pyx_EndUnpack(%s, %d)" % (
3929                 self.iterator.py_result(),
3930                 len(self.args)))
3931             if debug_disposal_code:
3932                 print("UnpackNode.generate_assignment_code:")
3933                 print("...generating disposal code for %s" % self.iterator)
3934             self.iterator.generate_disposal_code(code)
3935             self.iterator.free_temps(code)
3936             self.iterator.release(code)
3937
3938             for i in range(len(self.args)):
3939                 self.args[i].generate_assignment_code(
3940                     self.coerced_unpacked_items[i], code)
3941
3942         code.putln("}")
3943
3944     def generate_starred_assignment_code(self, rhs, code):
3945         code.globalstate.use_utility_code(unpacking_utility_code)
3946
3947         for i, arg in enumerate(self.args):
3948             if arg.is_starred:
3949                 starred_target = self.unpacked_items[i]
3950                 fixed_args_left  = self.args[:i]
3951                 fixed_args_right = self.args[i+1:]
3952                 break
3953
3954         self.iterator.allocate(code)
3955         code.putln(
3956             "%s = PyObject_GetIter(%s); %s" % (
3957                 self.iterator.result(),
3958                 rhs.py_result(),
3959                 code.error_goto_if_null(self.iterator.result(), self.pos)))
3960         code.put_gotref(self.iterator.py_result())
3961         rhs.generate_disposal_code(code)
3962
3963         for item in self.unpacked_items:
3964             item.allocate(code)
3965         for i in range(len(fixed_args_left)):
3966             item = self.unpacked_items[i]
3967             unpack_code = "__Pyx_UnpackItem(%s, %d)" % (
3968                 self.iterator.py_result(), i)
3969             code.putln(
3970                 "%s = %s; %s" % (
3971                     item.result(),
3972                     typecast(item.ctype(), py_object_type, unpack_code),
3973                     code.error_goto_if_null(item.result(), self.pos)))
3974             code.put_gotref(item.py_result())
3975             value_node = self.coerced_unpacked_items[i]
3976             value_node.generate_evaluation_code(code)
3977
3978         target_list = starred_target.result()
3979         code.putln("%s = PySequence_List(%s); %s" % (
3980             target_list, self.iterator.py_result(),
3981             code.error_goto_if_null(target_list, self.pos)))
3982         code.put_gotref(target_list)
3983         if fixed_args_right:
3984             code.globalstate.use_utility_code(raise_need_more_values_to_unpack)
3985             unpacked_right_args = self.unpacked_items[-len(fixed_args_right):]
3986             code.putln("if (unlikely(PyList_GET_SIZE(%s) < %d)) {" % (
3987                 (target_list, len(unpacked_right_args))))
3988             code.put("__Pyx_RaiseNeedMoreValuesError(%d+PyList_GET_SIZE(%s)); %s" % (
3989                      len(fixed_args_left), target_list,
3990                      code.error_goto(self.pos)))
3991             code.putln('}')
3992             for i, (arg, coerced_arg) in enumerate(zip(unpacked_right_args[::-1],
3993                                                        self.coerced_unpacked_items[::-1])):
3994                 code.putln(
3995                     "%s = PyList_GET_ITEM(%s, PyList_GET_SIZE(%s)-1); " % (
3996                         arg.py_result(),
3997                         target_list, target_list))
3998                 # resize the list the hard way
3999                 code.putln("((PyVarObject*)%s)->ob_size--;" % target_list)
4000                 code.put_gotref(arg.py_result())
4001                 coerced_arg.generate_evaluation_code(code)
4002
4003         self.iterator.generate_disposal_code(code)
4004         self.iterator.free_temps(code)
4005         self.iterator.release(code)
4006
4007         for i in range(len(self.args)):
4008             self.args[i].generate_assignment_code(
4009                 self.coerced_unpacked_items[i], code)
4010
4011     def annotate(self, code):
4012         for arg in self.args:
4013             arg.annotate(code)
4014         if self.unpacked_items:
4015             for arg in self.unpacked_items:
4016                 arg.annotate(code)
4017             for arg in self.coerced_unpacked_items:
4018                 arg.annotate(code)
4019
4020
4021 class TupleNode(SequenceNode):
4022     #  Tuple constructor.
4023
4024     type = tuple_type
4025
4026     gil_message = "Constructing Python tuple"
4027
4028     def analyse_types(self, env, skip_children=False):
4029         if len(self.args) == 0:
4030             self.is_temp = 0
4031             self.is_literal = 1
4032         else:
4033             SequenceNode.analyse_types(self, env, skip_children)
4034             for child in self.args:
4035                 if not child.is_literal:
4036                     break
4037             else:
4038                 self.is_temp = 0
4039                 self.is_literal = 1
4040
4041     def is_simple(self):
4042         # either temp or constant => always simple
4043         return True
4044
4045     def calculate_result_code(self):
4046         if len(self.args) > 0:
4047             return self.result_code
4048         else:
4049             return Naming.empty_tuple
4050
4051     def calculate_constant_result(self):
4052         self.constant_result = tuple([
4053                 arg.constant_result for arg in self.args])
4054
4055     def compile_time_value(self, denv):
4056         values = self.compile_time_value_list(denv)
4057         try:
4058             return tuple(values)
4059         except Exception, e:
4060             self.compile_time_value_error(e)
4061
4062     def generate_operation_code(self, code):
4063         if len(self.args) == 0:
4064             # result_code is Naming.empty_tuple
4065             return
4066         if self.is_literal:
4067             # non-empty cached tuple => result is global constant,
4068             # creation code goes into separate code writer
4069             self.result_code = code.get_py_const(py_object_type, 'tuple_', cleanup_level=2)
4070             code = code.get_cached_constants_writer()
4071             code.mark_pos(self.pos)
4072
4073         code.putln(
4074             "%s = PyTuple_New(%s); %s" % (
4075                 self.result(),
4076                 len(self.args),
4077                 code.error_goto_if_null(self.result(), self.pos)))
4078         code.put_gotref(self.py_result())
4079         for i in range(len(self.args)):
4080             arg = self.args[i]
4081             if not arg.result_in_temp():
4082                 code.put_incref(arg.result(), arg.ctype())
4083             code.putln(
4084                 "PyTuple_SET_ITEM(%s, %s, %s);" % (
4085                     self.result(),
4086                     i,
4087                     arg.py_result()))
4088             code.put_giveref(arg.py_result())
4089         if self.is_literal:
4090             code.put_giveref(self.py_result())
4091
4092     def generate_subexpr_disposal_code(self, code):
4093         # We call generate_post_assignment_code here instead
4094         # of generate_disposal_code, because values were stored
4095         # in the tuple using a reference-stealing operation.
4096         for arg in self.args:
4097             arg.generate_post_assignment_code(code)
4098             # Should NOT call free_temps -- this is invoked by the default
4099             # generate_evaluation_code which will do that.
4100
4101
4102 class ListNode(SequenceNode):
4103     #  List constructor.
4104
4105     # obj_conversion_errors    [PyrexError]   used internally
4106     # orignial_args            [ExprNode]     used internally
4107
4108     obj_conversion_errors = []
4109     type = list_type
4110
4111     gil_message = "Constructing Python list"
4112
4113     def type_dependencies(self, env):
4114         return ()
4115
4116     def infer_type(self, env):
4117         # TOOD: Infer non-object list arrays.
4118         return list_type
4119
4120     def analyse_expressions(self, env):
4121         SequenceNode.analyse_expressions(self, env)
4122         self.coerce_to_pyobject(env)
4123
4124     def analyse_types(self, env):
4125         hold_errors()
4126         self.original_args = list(self.args)
4127         SequenceNode.analyse_types(self, env)
4128         self.obj_conversion_errors = held_errors()
4129         release_errors(ignore=True)
4130
4131     def coerce_to(self, dst_type, env):
4132         if dst_type.is_pyobject:
4133             for err in self.obj_conversion_errors:
4134                 report_error(err)
4135             self.obj_conversion_errors = []
4136             if not self.type.subtype_of(dst_type):
4137                 error(self.pos, "Cannot coerce list to type '%s'" % dst_type)
4138         elif dst_type.is_ptr and dst_type.base_type is not PyrexTypes.c_void_type:
4139             base_type = dst_type.base_type
4140             self.type = PyrexTypes.CArrayType(base_type, len(self.args))
4141             for i in range(len(self.original_args)):
4142                 arg = self.args[i]
4143                 if isinstance(arg, CoerceToPyTypeNode):
4144                     arg = arg.arg
4145                 self.args[i] = arg.coerce_to(base_type, env)
4146         elif dst_type.is_struct:
4147             if len(self.args) > len(dst_type.scope.var_entries):
4148                 error(self.pos, "Too may members for '%s'" % dst_type)
4149             else:
4150                 if len(self.args) < len(dst_type.scope.var_entries):
4151                     warning(self.pos, "Too few members for '%s'" % dst_type, 1)
4152                 for i, (arg, member) in enumerate(zip(self.original_args, dst_type.scope.var_entries)):
4153                     if isinstance(arg, CoerceToPyTypeNode):
4154                         arg = arg.arg
4155                     self.args[i] = arg.coerce_to(member.type, env)
4156             self.type = dst_type
4157         else:
4158             self.type = error_type
4159             error(self.pos, "Cannot coerce list to type '%s'" % dst_type)
4160         return self
4161
4162     def release_temp(self, env):
4163         if self.type.is_array:
4164             # To be valid C++, we must allocate the memory on the stack
4165             # manually and be sure not to reuse it for something else.
4166             pass
4167         else:
4168             SequenceNode.release_temp(self, env)
4169
4170     def calculate_constant_result(self):
4171         self.constant_result = [
4172             arg.constant_result for arg in self.args]
4173
4174     def compile_time_value(self, denv):
4175         return self.compile_time_value_list(denv)
4176
4177     def generate_operation_code(self, code):
4178         if self.type.is_pyobject:
4179             for err in self.obj_conversion_errors:
4180                 report_error(err)
4181             code.putln("%s = PyList_New(%s); %s" %
4182                 (self.result(),
4183                 len(self.args),
4184                 code.error_goto_if_null(self.result(), self.pos)))
4185             code.put_gotref(self.py_result())
4186             for i in range(len(self.args)):
4187                 arg = self.args[i]
4188                 #if not arg.is_temp:
4189                 if not arg.result_in_temp():
4190                     code.put_incref(arg.result(), arg.ctype())
4191                 code.putln("PyList_SET_ITEM(%s, %s, %s);" %
4192                     (self.result(),
4193                     i,
4194                     arg.py_result()))
4195                 code.put_giveref(arg.py_result())
4196         elif self.type.is_array:
4197             for i, arg in enumerate(self.args):
4198                 code.putln("%s[%s] = %s;" % (
4199                                 self.result(),
4200                                 i,
4201                                 arg.result()))
4202         elif self.type.is_struct:
4203             for arg, member in zip(self.args, self.type.scope.var_entries):
4204                 code.putln("%s.%s = %s;" % (
4205                         self.result(),
4206                         member.cname,
4207                         arg.result()))
4208         else:
4209             raise InternalError("List type never specified")
4210
4211     def generate_subexpr_disposal_code(self, code):
4212         # We call generate_post_assignment_code here instead
4213         # of generate_disposal_code, because values were stored
4214         # in the list using a reference-stealing operation.
4215         for arg in self.args:
4216             arg.generate_post_assignment_code(code)
4217             # Should NOT call free_temps -- this is invoked by the default
4218             # generate_evaluation_code which will do that.
4219
4220
4221 class ScopedExprNode(ExprNode):
4222     # Abstract base class for ExprNodes that have their own local
4223     # scope, such as generator expressions.
4224     #
4225     # expr_scope    Scope  the inner scope of the expression
4226
4227     subexprs = []
4228     expr_scope = None
4229
4230     # does this node really have a local scope, e.g. does it leak loop
4231     # variables or not?  non-leaking Py3 behaviour is default, except
4232     # for list comprehensions where the behaviour differs in Py2 and
4233     # Py3 (set in Parsing.py based on parser context)
4234     has_local_scope = True
4235
4236     def init_scope(self, outer_scope, expr_scope=None):
4237         if expr_scope is not None:
4238             self.expr_scope = expr_scope
4239         elif self.has_local_scope:
4240             self.expr_scope = Symtab.GeneratorExpressionScope(outer_scope)
4241         else:
4242             self.expr_scope = None
4243
4244     def analyse_declarations(self, env):
4245         self.init_scope(env)
4246
4247     def analyse_scoped_declarations(self, env):
4248         # this is called with the expr_scope as env
4249         pass
4250
4251     def analyse_types(self, env):
4252         # no recursion here, the children will be analysed separately below
4253         pass
4254
4255     def analyse_scoped_expressions(self, env):
4256         # this is called with the expr_scope as env
4257         pass
4258
4259     def generate_evaluation_code(self, code):
4260         # set up local variables and free their references on exit
4261         generate_inner_evaluation_code = super(ScopedExprNode, self).generate_evaluation_code
4262         if not self.has_local_scope or not self.expr_scope.var_entries:
4263             # no local variables => delegate, done
4264             generate_inner_evaluation_code(code)
4265             return
4266
4267         code.putln('{ /* enter inner scope */')
4268         py_entries = []
4269         for entry in self.expr_scope.var_entries:
4270             if not entry.in_closure:
4271                 code.put_var_declaration(entry)
4272                 if entry.type.is_pyobject and entry.used:
4273                     py_entries.append(entry)
4274         if not py_entries:
4275             # no local Python references => no cleanup required
4276             generate_inner_evaluation_code(code)
4277             code.putln('} /* exit inner scope */')
4278             return
4279         for entry in py_entries:
4280             code.put_init_var_to_py_none(entry)
4281
4282         # must free all local Python references at each exit point
4283         old_loop_labels = tuple(code.new_loop_labels())
4284         old_error_label = code.new_error_label()
4285
4286         generate_inner_evaluation_code(code)
4287
4288         # normal (non-error) exit
4289         for entry in py_entries:
4290             code.put_var_decref(entry)
4291
4292         # error/loop body exit points
4293         exit_scope = code.new_label('exit_scope')
4294         code.put_goto(exit_scope)
4295         for label, old_label in ([(code.error_label, old_error_label)] +
4296                                  list(zip(code.get_loop_labels(), old_loop_labels))):
4297             if code.label_used(label):
4298                 code.put_label(label)
4299                 for entry in py_entries:
4300                     code.put_var_decref(entry)
4301                 code.put_goto(old_label)
4302         code.put_label(exit_scope)
4303         code.putln('} /* exit inner scope */')
4304
4305         code.set_loop_labels(old_loop_labels)
4306         code.error_label = old_error_label
4307
4308
4309 class ComprehensionNode(ScopedExprNode):
4310     subexprs = ["target"]
4311     child_attrs = ["loop", "append"]
4312
4313     def infer_type(self, env):
4314         return self.target.infer_type(env)
4315
4316     def analyse_declarations(self, env):
4317         self.append.target = self # this is used in the PyList_Append of the inner loop
4318         self.init_scope(env)
4319
4320     def analyse_scoped_declarations(self, env):
4321         self.loop.analyse_declarations(env)
4322
4323     def analyse_types(self, env):
4324         self.target.analyse_expressions(env)
4325         self.type = self.target.type
4326         if not self.has_local_scope:
4327             self.loop.analyse_expressions(env)
4328
4329     def analyse_scoped_expressions(self, env):
4330         if self.has_local_scope:
4331             self.loop.analyse_expressions(env)
4332
4333     def may_be_none(self):
4334         return False
4335
4336     def calculate_result_code(self):
4337         return self.target.result()
4338
4339     def generate_result_code(self, code):
4340         self.generate_operation_code(code)
4341
4342     def generate_operation_code(self, code):
4343         self.loop.generate_execution_code(code)
4344
4345     def annotate(self, code):
4346         self.loop.annotate(code)
4347
4348
4349 class ComprehensionAppendNode(Node):
4350     # Need to be careful to avoid infinite recursion:
4351     # target must not be in child_attrs/subexprs
4352
4353     child_attrs = ['expr']
4354
4355     type = PyrexTypes.c_int_type
4356
4357     def analyse_expressions(self, env):
4358         self.expr.analyse_expressions(env)
4359         if not self.expr.type.is_pyobject:
4360             self.expr = self.expr.coerce_to_pyobject(env)
4361
4362     def generate_execution_code(self, code):
4363         if self.target.type is list_type:
4364             function = "PyList_Append"
4365         elif self.target.type is set_type:
4366             function = "PySet_Add"
4367         else:
4368             raise InternalError(
4369                 "Invalid type for comprehension node: %s" % self.target.type)
4370
4371         self.expr.generate_evaluation_code(code)
4372         code.putln(code.error_goto_if("%s(%s, (PyObject*)%s)" % (
4373             function,
4374             self.target.result(),
4375             self.expr.result()
4376             ), self.pos))
4377         self.expr.generate_disposal_code(code)
4378         self.expr.free_temps(code)
4379
4380     def generate_function_definitions(self, env, code):
4381         self.expr.generate_function_definitions(env, code)
4382
4383     def annotate(self, code):
4384         self.expr.annotate(code)
4385
4386 class DictComprehensionAppendNode(ComprehensionAppendNode):
4387     child_attrs = ['key_expr', 'value_expr']
4388
4389     def analyse_expressions(self, env):
4390         self.key_expr.analyse_expressions(env)
4391         if not self.key_expr.type.is_pyobject:
4392             self.key_expr = self.key_expr.coerce_to_pyobject(env)
4393         self.value_expr.analyse_expressions(env)
4394         if not self.value_expr.type.is_pyobject:
4395             self.value_expr = self.value_expr.coerce_to_pyobject(env)
4396
4397     def generate_execution_code(self, code):
4398         self.key_expr.generate_evaluation_code(code)
4399         self.value_expr.generate_evaluation_code(code)
4400         code.putln(code.error_goto_if("PyDict_SetItem(%s, (PyObject*)%s, (PyObject*)%s)" % (
4401             self.target.result(),
4402             self.key_expr.result(),
4403             self.value_expr.result()
4404             ), self.pos))
4405         self.key_expr.generate_disposal_code(code)
4406         self.key_expr.free_temps(code)
4407         self.value_expr.generate_disposal_code(code)
4408         self.value_expr.free_temps(code)
4409
4410     def generate_function_definitions(self, env, code):
4411         self.key_expr.generate_function_definitions(env, code)
4412         self.value_expr.generate_function_definitions(env, code)
4413
4414     def annotate(self, code):
4415         self.key_expr.annotate(code)
4416         self.value_expr.annotate(code)
4417
4418
4419 class GeneratorExpressionNode(ScopedExprNode):
4420     # A generator expression, e.g.  (i for i in range(10))
4421     #
4422     # Result is a generator.
4423     #
4424     # loop      ForStatNode   the for-loop, containing a YieldExprNode
4425
4426     child_attrs = ["loop"]
4427
4428     type = py_object_type
4429
4430     def analyse_scoped_declarations(self, env):
4431         self.loop.analyse_declarations(env)
4432
4433     def analyse_types(self, env):
4434         if not self.has_local_scope:
4435             self.loop.analyse_expressions(env)
4436         self.is_temp = True
4437
4438     def analyse_scoped_expressions(self, env):
4439         if self.has_local_scope:
4440             self.loop.analyse_expressions(env)
4441
4442     def may_be_none(self):
4443         return False
4444
4445     def annotate(self, code):
4446         self.loop.annotate(code)
4447
4448
4449 class InlinedGeneratorExpressionNode(GeneratorExpressionNode):
4450     # An inlined generator expression for which the result is
4451     # calculated inside of the loop.  This will only be created by
4452     # transforms when replacing builtin calls on generator
4453     # expressions.
4454     #
4455     # loop           ForStatNode      the for-loop, not containing any YieldExprNodes
4456     # result_node    ResultRefNode    the reference to the result value temp
4457     # orig_func      String           the name of the builtin function this node replaces
4458
4459     child_attrs = ["loop"]
4460     loop_analysed = False
4461
4462     def infer_type(self, env):
4463         return self.result_node.infer_type(env)
4464
4465     def analyse_types(self, env):
4466         if not self.has_local_scope:
4467             self.loop_analysed = True
4468             self.loop.analyse_expressions(env)
4469         self.type = self.result_node.type
4470         self.is_temp = True
4471
4472     def analyse_scoped_expressions(self, env):
4473         self.loop_analysed = True
4474         GeneratorExpressionNode.analyse_scoped_expressions(self, env)
4475
4476     def coerce_to(self, dst_type, env):
4477         if self.orig_func == 'sum' and dst_type.is_numeric and not self.loop_analysed:
4478             # We can optimise by dropping the aggregation variable and
4479             # the add operations into C.  This can only be done safely
4480             # before analysing the loop body, after that, the result
4481             # reference type will have infected expressions and
4482             # assignments.
4483             self.result_node.type = self.type = dst_type
4484             return self
4485         return GeneratorExpressionNode.coerce_to(self, dst_type, env)
4486
4487     def generate_result_code(self, code):
4488         self.result_node.result_code = self.result()
4489         self.loop.generate_execution_code(code)
4490
4491
4492 class SetNode(ExprNode):
4493     #  Set constructor.
4494
4495     type = set_type
4496
4497     subexprs = ['args']
4498
4499     gil_message = "Constructing Python set"
4500
4501     def analyse_types(self, env):
4502         for i in range(len(self.args)):
4503             arg = self.args[i]
4504             arg.analyse_types(env)
4505             self.args[i] = arg.coerce_to_pyobject(env)
4506         self.type = set_type
4507         self.is_temp = 1
4508
4509     def may_be_none(self):
4510         return False
4511
4512     def calculate_constant_result(self):
4513         self.constant_result = set([
4514                 arg.constant_result for arg in self.args])
4515
4516     def compile_time_value(self, denv):
4517         values = [arg.compile_time_value(denv) for arg in self.args]
4518         try:
4519             return set(values)
4520         except Exception, e:
4521             self.compile_time_value_error(e)
4522
4523     def generate_evaluation_code(self, code):
4524         code.globalstate.use_utility_code(Builtin.py23_set_utility_code)
4525         self.allocate_temp_result(code)
4526         code.putln(
4527             "%s = PySet_New(0); %s" % (
4528                 self.result(),
4529                 code.error_goto_if_null(self.result(), self.pos)))
4530         code.put_gotref(self.py_result())
4531         for arg in self.args:
4532             arg.generate_evaluation_code(code)
4533             code.putln(
4534                 code.error_goto_if_neg(
4535                     "PySet_Add(%s, %s)" % (self.result(), arg.py_result()),
4536                     self.pos))
4537             arg.generate_disposal_code(code)
4538             arg.free_temps(code)
4539
4540
4541 class DictNode(ExprNode):
4542     #  Dictionary constructor.
4543     #
4544     #  key_value_pairs  [DictItemNode]
4545     #
4546     # obj_conversion_errors    [PyrexError]   used internally
4547
4548     subexprs = ['key_value_pairs']
4549     is_temp = 1
4550     type = dict_type
4551
4552     obj_conversion_errors = []
4553
4554     def calculate_constant_result(self):
4555         self.constant_result = dict([
4556                 item.constant_result for item in self.key_value_pairs])
4557
4558     def compile_time_value(self, denv):
4559         pairs = [(item.key.compile_time_value(denv), item.value.compile_time_value(denv))
4560             for item in self.key_value_pairs]
4561         try:
4562             return dict(pairs)
4563         except Exception, e:
4564             self.compile_time_value_error(e)
4565
4566     def type_dependencies(self, env):
4567         return ()
4568
4569     def infer_type(self, env):
4570         # TOOD: Infer struct constructors.
4571         return dict_type
4572
4573     def analyse_types(self, env):
4574         hold_errors()
4575         for item in self.key_value_pairs:
4576             item.analyse_types(env)
4577         self.obj_conversion_errors = held_errors()
4578         release_errors(ignore=True)
4579
4580     def may_be_none(self):
4581         return False
4582
4583     def coerce_to(self, dst_type, env):
4584         if dst_type.is_pyobject:
4585             self.release_errors()
4586             if not self.type.subtype_of(dst_type):
4587                 error(self.pos, "Cannot interpret dict as type '%s'" % dst_type)
4588         elif dst_type.is_struct_or_union:
4589             self.type = dst_type
4590             if not dst_type.is_struct and len(self.key_value_pairs) != 1:
4591                 error(self.pos, "Exactly one field must be specified to convert to union '%s'" % dst_type)
4592             elif dst_type.is_struct and len(self.key_value_pairs) < len(dst_type.scope.var_entries):
4593                 warning(self.pos, "Not all members given for struct '%s'" % dst_type, 1)
4594             for item in self.key_value_pairs:
4595                 if isinstance(item.key, CoerceToPyTypeNode):
4596                     item.key = item.key.arg
4597                 if not isinstance(item.key, (UnicodeNode, StringNode, BytesNode)):
4598                     error(item.key.pos, "Invalid struct field identifier")
4599                     item.key = StringNode(item.key.pos, value="<error>")
4600                 else:
4601                     key = str(item.key.value) # converts string literals to unicode in Py3
4602                     member = dst_type.scope.lookup_here(key)
4603                     if not member:
4604                         error(item.key.pos, "struct '%s' has no field '%s'" % (dst_type, key))
4605                     else:
4606                         value = item.value
4607                         if isinstance(value, CoerceToPyTypeNode):
4608                             value = value.arg
4609                         item.value = value.coerce_to(member.type, env)
4610         else:
4611             self.type = error_type
4612             error(self.pos, "Cannot interpret dict as type '%s'" % dst_type)
4613         return self
4614
4615     def release_errors(self):
4616         for err in self.obj_conversion_errors:
4617             report_error(err)
4618         self.obj_conversion_errors = []
4619
4620     gil_message = "Constructing Python dict"
4621
4622     def generate_evaluation_code(self, code):
4623         #  Custom method used here because key-value
4624         #  pairs are evaluated and used one at a time.
4625         code.mark_pos(self.pos)
4626         self.allocate_temp_result(code)
4627         if self.type.is_pyobject:
4628             self.release_errors()
4629             code.putln(
4630                 "%s = PyDict_New(); %s" % (
4631                     self.result(),
4632                     code.error_goto_if_null(self.result(), self.pos)))
4633             code.put_gotref(self.py_result())
4634         for item in self.key_value_pairs:
4635             item.generate_evaluation_code(code)
4636             if self.type.is_pyobject:
4637                 code.put_error_if_neg(self.pos,
4638                     "PyDict_SetItem(%s, %s, %s)" % (
4639                         self.result(),
4640                         item.key.py_result(),
4641                         item.value.py_result()))
4642             else:
4643                 code.putln("%s.%s = %s;" % (
4644                         self.result(),
4645                         item.key.value,
4646                         item.value.result()))
4647             item.generate_disposal_code(code)
4648             item.free_temps(code)
4649
4650     def annotate(self, code):
4651         for item in self.key_value_pairs:
4652             item.annotate(code)
4653
4654 class DictItemNode(ExprNode):
4655     # Represents a single item in a DictNode
4656     #
4657     # key          ExprNode
4658     # value        ExprNode
4659     subexprs = ['key', 'value']
4660
4661     nogil_check = None # Parent DictNode takes care of it
4662
4663     def calculate_constant_result(self):
4664         self.constant_result = (
4665             self.key.constant_result, self.value.constant_result)
4666
4667     def analyse_types(self, env):
4668         self.key.analyse_types(env)
4669         self.value.analyse_types(env)
4670         self.key = self.key.coerce_to_pyobject(env)
4671         self.value = self.value.coerce_to_pyobject(env)
4672
4673     def generate_evaluation_code(self, code):
4674         self.key.generate_evaluation_code(code)
4675         self.value.generate_evaluation_code(code)
4676
4677     def generate_disposal_code(self, code):
4678         self.key.generate_disposal_code(code)
4679         self.value.generate_disposal_code(code)
4680
4681     def free_temps(self, code):
4682         self.key.free_temps(code)
4683         self.value.free_temps(code)
4684
4685     def __iter__(self):
4686         return iter([self.key, self.value])
4687
4688 class ModuleNameMixin(object):
4689     def set_mod_name(self, env):
4690         self.module_name = env.global_scope().qualified_name
4691
4692     def get_py_mod_name(self, code):
4693         return code.get_py_string_const(
4694                  self.module_name, identifier=True)
4695
4696 class ClassNode(ExprNode, ModuleNameMixin):
4697     #  Helper class used in the implementation of Python
4698     #  class definitions. Constructs a class object given
4699     #  a name, tuple of bases and class dictionary.
4700     #
4701     #  name         EncodedString      Name of the class
4702     #  bases        ExprNode           Base class tuple
4703     #  dict         ExprNode           Class dict (not owned by this node)
4704     #  doc          ExprNode or None   Doc string
4705     #  module_name  EncodedString      Name of defining module
4706
4707     subexprs = ['bases', 'doc']
4708
4709     def analyse_types(self, env):
4710         self.bases.analyse_types(env)
4711         if self.doc:
4712             self.doc.analyse_types(env)
4713             self.doc = self.doc.coerce_to_pyobject(env)
4714         self.type = py_object_type
4715         self.is_temp = 1
4716         env.use_utility_code(create_class_utility_code);
4717         #TODO(craig,haoyu) This should be moved to a better place
4718         self.set_mod_name(env)
4719
4720     def may_be_none(self):
4721         return True
4722
4723     gil_message = "Constructing Python class"
4724
4725     def generate_result_code(self, code):
4726         cname = code.intern_identifier(self.name)
4727
4728         if self.doc:
4729             code.put_error_if_neg(self.pos,
4730                 'PyDict_SetItemString(%s, "__doc__", %s)' % (
4731                     self.dict.py_result(),
4732                     self.doc.py_result()))
4733         py_mod_name = self.get_py_mod_name(code)
4734         code.putln(
4735             '%s = __Pyx_CreateClass(%s, %s, %s, %s); %s' % (
4736                 self.result(),
4737                 self.bases.py_result(),
4738                 self.dict.py_result(),
4739                 cname,
4740                 py_mod_name,
4741                 code.error_goto_if_null(self.result(), self.pos)))
4742         code.put_gotref(self.py_result())
4743
4744
4745 class Py3ClassNode(ExprNode):
4746     #  Helper class used in the implementation of Python3+
4747     #  class definitions. Constructs a class object given
4748     #  a name, tuple of bases and class dictionary.
4749     #
4750     #  name         EncodedString      Name of the class
4751     #  dict         ExprNode           Class dict (not owned by this node)
4752     #  module_name  EncodedString      Name of defining module
4753
4754     subexprs = []
4755
4756     def analyse_types(self, env):
4757         self.type = py_object_type
4758         self.is_temp = 1
4759
4760     def may_be_none(self):
4761         return True
4762
4763     gil_message = "Constructing Python class"
4764
4765     def generate_result_code(self, code):
4766         code.globalstate.use_utility_code(create_py3class_utility_code)
4767         cname = code.intern_identifier(self.name)
4768         code.putln(
4769             '%s = __Pyx_Py3ClassCreate(%s, %s, %s, %s, %s); %s' % (
4770                 self.result(),
4771                 self.metaclass.result(),
4772                 cname,
4773                 self.bases.py_result(),
4774                 self.dict.py_result(),
4775                 self.mkw.py_result(),
4776                 code.error_goto_if_null(self.result(), self.pos)))
4777         code.put_gotref(self.py_result())
4778
4779 class KeywordArgsNode(ExprNode):
4780     # Helper class for keyword arguments
4781     #
4782     # keyword_args ExprNode or None     Keyword arguments
4783     # starstar_arg ExprNode or None     Extra arguments
4784
4785     subexprs = ['keyword_args', 'starstar_arg']
4786
4787     def analyse_types(self, env):
4788         if self.keyword_args:
4789             self.keyword_args.analyse_types(env)
4790         if self.starstar_arg:
4791             self.starstar_arg.analyse_types(env)
4792             # make sure we have a Python object as **kwargs mapping
4793             self.starstar_arg = \
4794                 self.starstar_arg.coerce_to_pyobject(env)
4795         self.type = py_object_type
4796         self.is_temp = 1
4797
4798     gil_message = "Constructing Keyword Args"
4799
4800     def generate_result_code(self, code):
4801         if self.keyword_args and self.starstar_arg:
4802             code.put_error_if_neg(self.pos,
4803                 "PyDict_Update(%s, %s)" % (
4804                     self.keyword_args.py_result(),
4805                     self.starstar_arg.py_result()))
4806         if self.keyword_args:
4807             code.putln("%s = %s;" % (self.result(), self.keyword_args.result()))
4808             code.put_incref(self.keyword_args.result(), self.keyword_args.ctype())
4809         elif self.starstar_arg:
4810             code.putln(
4811                 "%s = PyDict_Copy(%s); %s" % (
4812                     self.result(),
4813                     self.starstar_arg.py_result(),
4814                     code.error_goto_if_null(self.result(), self.pos)))
4815             code.put_gotref(self.py_result())
4816         else:
4817             code.putln(
4818                 "%s = PyDict_New(); %s" % (
4819                     self.result(),
4820                     code.error_goto_if_null(self.result(), self.pos)))
4821             code.put_gotref(self.py_result())
4822
4823 class PyClassMetaclassNode(ExprNode):
4824     # Helper class holds Python3 metaclass object
4825     #
4826     #  bases        ExprNode           Base class tuple (not owned by this node)
4827     #  mkw          ExprNode           Class keyword arguments (not owned by this node)
4828
4829     subexprs = []
4830
4831     def analyse_types(self, env):
4832         self.type = py_object_type
4833         self.is_temp = True
4834
4835     def may_be_none(self):
4836         return True
4837
4838     def generate_result_code(self, code):
4839         code.putln(
4840             "%s = __Pyx_Py3MetaclassGet(%s, %s); %s" % (
4841                 self.result(),
4842                 self.bases.result(),
4843                 self.mkw.result(),
4844                 code.error_goto_if_null(self.result(), self.pos)))
4845         code.put_gotref(self.py_result())
4846
4847 class PyClassNamespaceNode(ExprNode, ModuleNameMixin):
4848     # Helper class holds Python3 namespace object
4849     #
4850     # All this are not owned by this node
4851     #  metaclass    ExprNode           Metaclass object
4852     #  bases        ExprNode           Base class tuple
4853     #  mkw          ExprNode           Class keyword arguments
4854     #  doc          ExprNode or None   Doc string (owned)
4855
4856     subexprs = ['doc']
4857
4858     def analyse_types(self, env):
4859         self.bases.analyse_types(env)
4860         if self.doc:
4861             self.doc.analyse_types(env)
4862             self.doc = self.doc.coerce_to_pyobject(env)
4863         self.type = py_object_type
4864         self.is_temp = 1
4865         #TODO(craig,haoyu) This should be moved to a better place
4866         self.set_mod_name(env)
4867
4868     def may_be_none(self):
4869         return True
4870
4871     def generate_result_code(self, code):
4872         cname = code.intern_identifier(self.name)
4873         py_mod_name = self.get_py_mod_name(code)
4874         if self.doc:
4875             doc_code = self.doc.result()
4876         else:
4877             doc_code = '(PyObject *) NULL'
4878         code.putln(
4879             "%s = __Pyx_Py3MetaclassPrepare(%s, %s, %s, %s, %s, %s); %s" % (
4880                 self.result(),
4881                 self.metaclass.result(),
4882                 self.bases.result(),
4883                 cname,
4884                 self.mkw.result(),
4885                 py_mod_name,
4886                 doc_code,
4887                 code.error_goto_if_null(self.result(), self.pos)))
4888         code.put_gotref(self.py_result())
4889
4890 class BoundMethodNode(ExprNode):
4891     #  Helper class used in the implementation of Python
4892     #  class definitions. Constructs an bound method
4893     #  object from a class and a function.
4894     #
4895     #  function      ExprNode   Function object
4896     #  self_object   ExprNode   self object
4897
4898     subexprs = ['function']
4899
4900     def analyse_types(self, env):
4901         self.function.analyse_types(env)
4902         self.type = py_object_type
4903         self.is_temp = 1
4904
4905     gil_message = "Constructing an bound method"
4906
4907     def generate_result_code(self, code):
4908         code.putln(
4909             "%s = PyMethod_New(%s, %s, (PyObject*)%s->ob_type); %s" % (
4910                 self.result(),
4911                 self.function.py_result(),
4912                 self.self_object.py_result(),
4913                 self.self_object.py_result(),
4914                 code.error_goto_if_null(self.result(), self.pos)))
4915         code.put_gotref(self.py_result())
4916
4917 class UnboundMethodNode(ExprNode):
4918     #  Helper class used in the implementation of Python
4919     #  class definitions. Constructs an unbound method
4920     #  object from a class and a function.
4921     #
4922     #  function      ExprNode   Function object
4923
4924     type = py_object_type
4925     is_temp = 1
4926
4927     subexprs = ['function']
4928
4929     def analyse_types(self, env):
4930         self.function.analyse_types(env)
4931
4932     def may_be_none(self):
4933         return False
4934
4935     gil_message = "Constructing an unbound method"
4936
4937     def generate_result_code(self, code):
4938         class_cname = code.pyclass_stack[-1].classobj.result()
4939         code.putln(
4940             "%s = PyMethod_New(%s, 0, %s); %s" % (
4941                 self.result(),
4942                 self.function.py_result(),
4943                 class_cname,
4944                 code.error_goto_if_null(self.result(), self.pos)))
4945         code.put_gotref(self.py_result())
4946
4947
4948 class PyCFunctionNode(ExprNode, ModuleNameMixin):
4949     #  Helper class used in the implementation of Python
4950     #  class definitions. Constructs a PyCFunction object
4951     #  from a PyMethodDef struct.
4952     #
4953     #  pymethdef_cname   string             PyMethodDef structure
4954     #  self_object       ExprNode or None
4955     #  binding           bool
4956     #  module_name       EncodedString      Name of defining module
4957
4958     subexprs = []
4959     self_object = None
4960     binding = False
4961
4962     type = py_object_type
4963     is_temp = 1
4964
4965     def analyse_types(self, env):
4966         if self.binding:
4967             env.use_utility_code(binding_cfunc_utility_code)
4968
4969         #TODO(craig,haoyu) This should be moved to a better place
4970         self.set_mod_name(env)
4971
4972     def may_be_none(self):
4973         return False
4974
4975     gil_message = "Constructing Python function"
4976
4977     def self_result_code(self):
4978         if self.self_object is None:
4979             self_result = "NULL"
4980         else:
4981             self_result = self.self_object.py_result()
4982         return self_result
4983
4984     def generate_result_code(self, code):
4985         if self.binding:
4986             constructor = "%s_NewEx" % Naming.binding_cfunc
4987         else:
4988             constructor = "PyCFunction_NewEx"
4989         py_mod_name = self.get_py_mod_name(code)
4990         code.putln(
4991             '%s = %s(&%s, %s, %s); %s' % (
4992                 self.result(),
4993                 constructor,
4994                 self.pymethdef_cname,
4995                 self.self_result_code(),
4996                 py_mod_name,
4997                 code.error_goto_if_null(self.result(), self.pos)))
4998         code.put_gotref(self.py_result())
4999
5000 class InnerFunctionNode(PyCFunctionNode):
5001     # Special PyCFunctionNode that depends on a closure class
5002     #
5003
5004     binding = True
5005     needs_self_code = True
5006
5007     def self_result_code(self):
5008         if self.needs_self_code:
5009             return "((PyObject*)%s)" % (Naming.cur_scope_cname)
5010         return "NULL"
5011
5012 class LambdaNode(InnerFunctionNode):
5013     # Lambda expression node (only used as a function reference)
5014     #
5015     # args          [CArgDeclNode]         formal arguments
5016     # star_arg      PyArgDeclNode or None  * argument
5017     # starstar_arg  PyArgDeclNode or None  ** argument
5018     # lambda_name   string                 a module-globally unique lambda name
5019     # result_expr   ExprNode
5020     # def_node      DefNode                the underlying function 'def' node
5021
5022     child_attrs = ['def_node']
5023
5024     def_node = None
5025     name = StringEncoding.EncodedString('<lambda>')
5026
5027     def analyse_declarations(self, env):
5028         self.def_node.analyse_declarations(env)
5029         self.pymethdef_cname = self.def_node.entry.pymethdef_cname
5030         env.add_lambda_def(self.def_node)
5031
5032 class YieldExprNode(ExprNode):
5033     # Yield expression node
5034     #
5035     # arg         ExprNode   the value to return from the generator
5036     # label_name  string     name of the C label used for this yield
5037
5038     subexprs = ['arg']
5039     type = py_object_type
5040
5041     def analyse_types(self, env):
5042         self.is_temp = 1
5043         if self.arg is not None:
5044             self.arg.analyse_types(env)
5045             if not self.arg.type.is_pyobject:
5046                 self.arg = self.arg.coerce_to_pyobject(env)
5047         error(self.pos, "Generators are not supported")
5048
5049     def generate_result_code(self, code):
5050         self.label_name = code.new_label('resume_from_yield')
5051         code.use_label(self.label_name)
5052         code.putln("/* FIXME: save temporary variables */")
5053         code.putln("/* FIXME: return from function, yielding value */")
5054         code.put_label(self.label_name)
5055         code.putln("/* FIXME: restore temporary variables and  */")
5056         code.putln("/* FIXME: extract sent value from closure */")
5057
5058
5059 #-------------------------------------------------------------------
5060 #
5061 #  Unary operator nodes
5062 #
5063 #-------------------------------------------------------------------
5064
5065 compile_time_unary_operators = {
5066     'not': operator.not_,
5067     '~': operator.inv,
5068     '-': operator.neg,
5069     '+': operator.pos,
5070 }
5071
5072 class UnopNode(ExprNode):
5073     #  operator     string
5074     #  operand      ExprNode
5075     #
5076     #  Processing during analyse_expressions phase:
5077     #
5078     #    analyse_c_operation
5079     #      Called when the operand is not a pyobject.
5080     #      - Check operand type and coerce if needed.
5081     #      - Determine result type and result code fragment.
5082     #      - Allocate temporary for result if needed.
5083
5084     subexprs = ['operand']
5085     infix = True
5086
5087     def calculate_constant_result(self):
5088         func = compile_time_unary_operators[self.operator]
5089         self.constant_result = func(self.operand.constant_result)
5090
5091     def compile_time_value(self, denv):
5092         func = compile_time_unary_operators.get(self.operator)
5093         if not func:
5094             error(self.pos,
5095                 "Unary '%s' not supported in compile-time expression"
5096                     % self.operator)
5097         operand = self.operand.compile_time_value(denv)
5098         try:
5099             return func(operand)
5100         except Exception, e:
5101             self.compile_time_value_error(e)
5102
5103     def infer_type(self, env):
5104         operand_type = self.operand.infer_type(env)
5105         if operand_type.is_pyobject:
5106             return py_object_type
5107         else:
5108             return operand_type
5109
5110     def analyse_types(self, env):
5111         self.operand.analyse_types(env)
5112         if self.is_py_operation():
5113             self.coerce_operand_to_pyobject(env)
5114             self.type = py_object_type
5115             self.is_temp = 1
5116         elif self.is_cpp_operation():
5117             self.analyse_cpp_operation(env)
5118         else:
5119             self.analyse_c_operation(env)
5120
5121     def check_const(self):
5122         return self.operand.check_const()
5123
5124     def is_py_operation(self):
5125         return self.operand.type.is_pyobject
5126
5127     def nogil_check(self, env):
5128         if self.is_py_operation():
5129             self.gil_error()
5130
5131     def is_cpp_operation(self):
5132         type = self.operand.type
5133         return type.is_cpp_class
5134
5135     def coerce_operand_to_pyobject(self, env):
5136         self.operand = self.operand.coerce_to_pyobject(env)
5137
5138     def generate_result_code(self, code):
5139         if self.operand.type.is_pyobject:
5140             self.generate_py_operation_code(code)
5141
5142     def generate_py_operation_code(self, code):
5143         function = self.py_operation_function()
5144         code.putln(
5145             "%s = %s(%s); %s" % (
5146                 self.result(),
5147                 function,
5148                 self.operand.py_result(),
5149                 code.error_goto_if_null(self.result(), self.pos)))
5150         code.put_gotref(self.py_result())
5151
5152     def type_error(self):
5153         if not self.operand.type.is_error:
5154             error(self.pos, "Invalid operand type for '%s' (%s)" %
5155                 (self.operator, self.operand.type))
5156         self.type = PyrexTypes.error_type
5157
5158     def analyse_cpp_operation(self, env):
5159         type = self.operand.type
5160         if type.is_ptr:
5161             type = type.base_type
5162         function = type.scope.lookup("operator%s" % self.operator)
5163         if not function:
5164             error(self.pos, "'%s' operator not defined for %s"
5165                 % (self.operator, type))
5166             self.type_error()
5167             return
5168         func_type = function.type
5169         if func_type.is_ptr:
5170             func_type = func_type.base_type
5171         self.type = func_type.return_type
5172
5173
5174 class NotNode(ExprNode):
5175     #  'not' operator
5176     #
5177     #  operand   ExprNode
5178
5179     type = PyrexTypes.c_bint_type
5180
5181     subexprs = ['operand']
5182
5183     def calculate_constant_result(self):
5184         self.constant_result = not self.operand.constant_result
5185
5186     def compile_time_value(self, denv):
5187         operand = self.operand.compile_time_value(denv)
5188         try:
5189             return not operand
5190         except Exception, e:
5191             self.compile_time_value_error(e)
5192
5193     def infer_type(self, env):
5194         return PyrexTypes.c_bint_type
5195
5196     def analyse_types(self, env):
5197         self.operand.analyse_types(env)
5198         self.operand = self.operand.coerce_to_boolean(env)
5199
5200     def calculate_result_code(self):
5201         return "(!%s)" % self.operand.result()
5202
5203     def generate_result_code(self, code):
5204         pass
5205
5206
5207 class UnaryPlusNode(UnopNode):
5208     #  unary '+' operator
5209
5210     operator = '+'
5211
5212     def analyse_c_operation(self, env):
5213         self.type = self.operand.type
5214
5215     def py_operation_function(self):
5216         return "PyNumber_Positive"
5217
5218     def calculate_result_code(self):
5219         if self.is_cpp_operation():
5220             return "(+%s)" % self.operand.result()
5221         else:
5222             return self.operand.result()
5223
5224
5225 class UnaryMinusNode(UnopNode):
5226     #  unary '-' operator
5227
5228     operator = '-'
5229
5230     def analyse_c_operation(self, env):
5231         if self.operand.type.is_numeric:
5232             self.type = self.operand.type
5233         else:
5234             self.type_error()
5235         if self.type.is_complex:
5236             self.infix = False
5237
5238     def py_operation_function(self):
5239         return "PyNumber_Negative"
5240
5241     def calculate_result_code(self):
5242         if self.infix:
5243             return "(-%s)" % self.operand.result()
5244         else:
5245             return "%s(%s)" % (self.operand.type.unary_op('-'), self.operand.result())
5246
5247     def get_constant_c_result_code(self):
5248         value = self.operand.get_constant_c_result_code()
5249         if value:
5250             return "(-%s)" % (value)
5251
5252 class TildeNode(UnopNode):
5253     #  unary '~' operator
5254
5255     def analyse_c_operation(self, env):
5256         if self.operand.type.is_int:
5257             self.type = self.operand.type
5258         else:
5259             self.type_error()
5260
5261     def py_operation_function(self):
5262         return "PyNumber_Invert"
5263
5264     def calculate_result_code(self):
5265         return "(~%s)" % self.operand.result()
5266
5267
5268 class CUnopNode(UnopNode):
5269
5270     def is_py_operation(self):
5271         return False
5272
5273 class DereferenceNode(CUnopNode):
5274     #  unary * operator
5275
5276     operator = '*'
5277
5278     def analyse_c_operation(self, env):
5279         if self.operand.type.is_ptr:
5280             self.type = self.operand.type.base_type
5281         else:
5282             self.type_error()
5283
5284     def calculate_result_code(self):
5285         return "(*%s)" % self.operand.result()
5286
5287
5288 class DecrementIncrementNode(CUnopNode):
5289     #  unary ++/-- operator
5290
5291     def analyse_c_operation(self, env):
5292         if self.operand.type.is_ptr or self.operand.type.is_numeric:
5293             self.type = self.operand.type
5294         else:
5295             self.type_error()
5296
5297     def calculate_result_code(self):
5298         if self.is_prefix:
5299             return "(%s%s)" % (self.operator, self.operand.result())
5300         else:
5301             return "(%s%s)" % (self.operand.result(), self.operator)
5302
5303 def inc_dec_constructor(is_prefix, operator):
5304     return lambda pos, **kwds: DecrementIncrementNode(pos, is_prefix=is_prefix, operator=operator, **kwds)
5305
5306
5307 class AmpersandNode(ExprNode):
5308     #  The C address-of operator.
5309     #
5310     #  operand  ExprNode
5311
5312     subexprs = ['operand']
5313
5314     def infer_type(self, env):
5315         return PyrexTypes.c_ptr_type(self.operand.infer_type(env))
5316
5317     def analyse_types(self, env):
5318         self.operand.analyse_types(env)
5319         argtype = self.operand.type
5320         if not (argtype.is_cfunction or self.operand.is_lvalue()):
5321             self.error("Taking address of non-lvalue")
5322             return
5323         if argtype.is_pyobject:
5324             self.error("Cannot take address of Python variable")
5325             return
5326         self.type = PyrexTypes.c_ptr_type(argtype)
5327
5328     def check_const(self):
5329         return self.operand.check_const_addr()
5330
5331     def error(self, mess):
5332         error(self.pos, mess)
5333         self.type = PyrexTypes.error_type
5334         self.result_code = "<error>"
5335
5336     def calculate_result_code(self):
5337         return "(&%s)" % self.operand.result()
5338
5339     def generate_result_code(self, code):
5340         pass
5341
5342
5343 unop_node_classes = {
5344     "+":  UnaryPlusNode,
5345     "-":  UnaryMinusNode,
5346     "~":  TildeNode,
5347 }
5348
5349 def unop_node(pos, operator, operand):
5350     # Construct unnop node of appropriate class for
5351     # given operator.
5352     if isinstance(operand, IntNode) and operator == '-':
5353         return IntNode(pos = operand.pos, value = str(-Utils.str_to_number(operand.value)))
5354     elif isinstance(operand, UnopNode) and operand.operator == operator:
5355         warning(pos, "Python has no increment/decrement operator: %s%sx = %s(%sx) = x" % ((operator,)*4), 5)
5356     return unop_node_classes[operator](pos,
5357         operator = operator,
5358         operand = operand)
5359
5360
5361 class TypecastNode(ExprNode):
5362     #  C type cast
5363     #
5364     #  operand      ExprNode
5365     #  base_type    CBaseTypeNode
5366     #  declarator   CDeclaratorNode
5367     #
5368     #  If used from a transform, one can if wanted specify the attribute
5369     #  "type" directly and leave base_type and declarator to None
5370
5371     subexprs = ['operand']
5372     base_type = declarator = type = None
5373
5374     def type_dependencies(self, env):
5375         return ()
5376
5377     def infer_type(self, env):
5378         if self.type is None:
5379             base_type = self.base_type.analyse(env)
5380             _, self.type = self.declarator.analyse(base_type, env)
5381         return self.type
5382
5383     def analyse_types(self, env):
5384         if self.type is None:
5385             base_type = self.base_type.analyse(env)
5386             _, self.type = self.declarator.analyse(base_type, env)
5387         if self.type.is_cfunction:
5388             error(self.pos,
5389                 "Cannot cast to a function type")
5390             self.type = PyrexTypes.error_type
5391         self.operand.analyse_types(env)
5392         to_py = self.type.is_pyobject
5393         from_py = self.operand.type.is_pyobject
5394         if from_py and not to_py and self.operand.is_ephemeral() and not self.type.is_numeric:
5395             error(self.pos, "Casting temporary Python object to non-numeric non-Python type")
5396         if to_py and not from_py:
5397             if self.type is bytes_type and self.operand.type.is_int:
5398                 # FIXME: the type cast node isn't needed in this case
5399                 # and can be dropped once analyse_types() can return a
5400                 # different node
5401                 self.operand = CoerceIntToBytesNode(self.operand, env)
5402             elif self.operand.type.can_coerce_to_pyobject(env):
5403                 self.result_ctype = py_object_type
5404                 self.operand = self.operand.coerce_to_pyobject(env)
5405             else:
5406                 if self.operand.type.is_ptr:
5407                     if not (self.operand.type.base_type.is_void or self.operand.type.base_type.is_struct):
5408                         error(self.pos, "Python objects cannot be cast from pointers of primitive types")
5409                 else:
5410                     # Should this be an error?
5411                     warning(self.pos, "No conversion from %s to %s, python object pointer used." % (self.operand.type, self.type))
5412                 self.operand = self.operand.coerce_to_simple(env)
5413         elif from_py and not to_py:
5414             if self.type.create_from_py_utility_code(env):
5415                 self.operand = self.operand.coerce_to(self.type, env)
5416             elif self.type.is_ptr:
5417                 if not (self.type.base_type.is_void or self.type.base_type.is_struct):
5418                     error(self.pos, "Python objects cannot be cast to pointers of primitive types")
5419             else:
5420                 warning(self.pos, "No conversion from %s to %s, python object pointer used." % (self.type, self.operand.type))
5421         elif from_py and to_py:
5422             if self.typecheck and self.type.is_extension_type:
5423                 self.operand = PyTypeTestNode(self.operand, self.type, env, notnone=True)
5424         elif self.type.is_complex and self.operand.type.is_complex:
5425             self.operand = self.operand.coerce_to_simple(env)
5426
5427     def is_simple(self):
5428         # either temp or a C cast => no side effects
5429         return True
5430
5431     def nogil_check(self, env):
5432         if self.type and self.type.is_pyobject and self.is_temp:
5433             self.gil_error()
5434
5435     def check_const(self):
5436         return self.operand.check_const()
5437
5438     def calculate_constant_result(self):
5439         # we usually do not know the result of a type cast at code
5440         # generation time
5441         pass
5442
5443     def calculate_result_code(self):
5444         if self.type.is_complex:
5445             operand_result = self.operand.result()
5446             if self.operand.type.is_complex:
5447                 real_part = self.type.real_type.cast_code("__Pyx_CREAL(%s)" % operand_result)
5448                 imag_part = self.type.real_type.cast_code("__Pyx_CIMAG(%s)" % operand_result)
5449             else:
5450                 real_part = self.type.real_type.cast_code(operand_result)
5451                 imag_part = "0"
5452             return "%s(%s, %s)" % (
5453                     self.type.from_parts,
5454                     real_part,
5455                     imag_part)
5456         else:
5457             return self.type.cast_code(self.operand.result())
5458
5459     def get_constant_c_result_code(self):
5460         operand_result = self.operand.get_constant_c_result_code()
5461         if operand_result:
5462             return self.type.cast_code(operand_result)
5463
5464     def result_as(self, type):
5465         if self.type.is_pyobject and not self.is_temp:
5466             #  Optimise away some unnecessary casting
5467             return self.operand.result_as(type)
5468         else:
5469             return ExprNode.result_as(self, type)
5470
5471     def generate_result_code(self, code):
5472         if self.is_temp:
5473             code.putln(
5474                 "%s = (PyObject *)%s;" % (
5475                     self.result(),
5476                     self.operand.result()))
5477             code.put_incref(self.result(), self.ctype())
5478
5479
5480 class SizeofNode(ExprNode):
5481     #  Abstract base class for sizeof(x) expression nodes.
5482
5483     type = PyrexTypes.c_size_t_type
5484
5485     def check_const(self):
5486         return True
5487
5488     def generate_result_code(self, code):
5489         pass
5490
5491
5492 class SizeofTypeNode(SizeofNode):
5493     #  C sizeof function applied to a type
5494     #
5495     #  base_type   CBaseTypeNode
5496     #  declarator  CDeclaratorNode
5497
5498     subexprs = []
5499     arg_type = None
5500
5501     def analyse_types(self, env):
5502         # we may have incorrectly interpreted a dotted name as a type rather than an attribute
5503         # this could be better handled by more uniformly treating types as runtime-available objects
5504         if 0 and self.base_type.module_path:
5505             path = self.base_type.module_path
5506             obj = env.lookup(path[0])
5507             if obj.as_module is None:
5508                 operand = NameNode(pos=self.pos, name=path[0])
5509                 for attr in path[1:]:
5510                     operand = AttributeNode(pos=self.pos, obj=operand, attribute=attr)
5511                 operand = AttributeNode(pos=self.pos, obj=operand, attribute=self.base_type.name)
5512                 self.operand = operand
5513                 self.__class__ = SizeofVarNode
5514                 self.analyse_types(env)
5515                 return
5516         if self.arg_type is None:
5517             base_type = self.base_type.analyse(env)
5518             _, arg_type = self.declarator.analyse(base_type, env)
5519             self.arg_type = arg_type
5520         self.check_type()
5521
5522     def check_type(self):
5523         arg_type = self.arg_type
5524         if arg_type.is_pyobject and not arg_type.is_extension_type:
5525             error(self.pos, "Cannot take sizeof Python object")
5526         elif arg_type.is_void:
5527             error(self.pos, "Cannot take sizeof void")
5528         elif not arg_type.is_complete():
5529             error(self.pos, "Cannot take sizeof incomplete type '%s'" % arg_type)
5530
5531     def calculate_result_code(self):
5532         if self.arg_type.is_extension_type:
5533             # the size of the pointer is boring
5534             # we want the size of the actual struct
5535             arg_code = self.arg_type.declaration_code("", deref=1)
5536         else:
5537             arg_code = self.arg_type.declaration_code("")
5538         return "(sizeof(%s))" % arg_code
5539
5540
5541 class SizeofVarNode(SizeofNode):
5542     #  C sizeof function applied to a variable
5543     #
5544     #  operand   ExprNode
5545
5546     subexprs = ['operand']
5547
5548     def analyse_types(self, env):
5549         # We may actually be looking at a type rather than a variable...
5550         # If we are, traditional analysis would fail...
5551         operand_as_type = self.operand.analyse_as_type(env)
5552         if operand_as_type:
5553             self.arg_type = operand_as_type
5554             self.__class__ = SizeofTypeNode
5555             self.check_type()
5556         else:
5557             self.operand.analyse_types(env)
5558
5559     def calculate_result_code(self):
5560         return "(sizeof(%s))" % self.operand.result()
5561
5562     def generate_result_code(self, code):
5563         pass
5564
5565 class TypeofNode(ExprNode):
5566     #  Compile-time type of an expression, as a string.
5567     #
5568     #  operand   ExprNode
5569     #  literal   StringNode # internal
5570
5571     literal = None
5572     type = py_object_type
5573
5574     subexprs = ['literal'] # 'operand' will be ignored after type analysis!
5575
5576     def analyse_types(self, env):
5577         self.operand.analyse_types(env)
5578         self.literal = StringNode(
5579             self.pos, value=StringEncoding.EncodedString(str(self.operand.type)))
5580         self.literal.analyse_types(env)
5581         self.literal = self.literal.coerce_to_pyobject(env)
5582
5583     def may_be_none(self):
5584         return False
5585
5586     def generate_evaluation_code(self, code):
5587         self.literal.generate_evaluation_code(code)
5588
5589     def calculate_result_code(self):
5590         return self.literal.calculate_result_code()
5591
5592 #-------------------------------------------------------------------
5593 #
5594 #  Binary operator nodes
5595 #
5596 #-------------------------------------------------------------------
5597
5598 def _not_in(x, seq):
5599     return x not in seq
5600
5601 compile_time_binary_operators = {
5602     '<': operator.lt,
5603     '<=': operator.le,
5604     '==': operator.eq,
5605     '!=': operator.ne,
5606     '>=': operator.ge,
5607     '>': operator.gt,
5608     'is': operator.is_,
5609     'is_not': operator.is_not,
5610     '+': operator.add,
5611     '&': operator.and_,
5612     '/': operator.truediv,
5613     '//': operator.floordiv,
5614     '<<': operator.lshift,
5615     '%': operator.mod,
5616     '*': operator.mul,
5617     '|': operator.or_,
5618     '**': operator.pow,
5619     '>>': operator.rshift,
5620     '-': operator.sub,
5621     '^': operator.xor,
5622     'in': operator.contains,
5623     'not_in': _not_in,
5624 }
5625
5626 def get_compile_time_binop(node):
5627     func = compile_time_binary_operators.get(node.operator)
5628     if not func:
5629         error(node.pos,
5630             "Binary '%s' not supported in compile-time expression"
5631                 % node.operator)
5632     return func
5633
5634 class BinopNode(ExprNode):
5635     #  operator     string
5636     #  operand1     ExprNode
5637     #  operand2     ExprNode
5638     #
5639     #  Processing during analyse_expressions phase:
5640     #
5641     #    analyse_c_operation
5642     #      Called when neither operand is a pyobject.
5643     #      - Check operand types and coerce if needed.
5644     #      - Determine result type and result code fragment.
5645     #      - Allocate temporary for result if needed.
5646
5647     subexprs = ['operand1', 'operand2']
5648     inplace = False
5649
5650     def calculate_constant_result(self):
5651         func = compile_time_binary_operators[self.operator]
5652         self.constant_result = func(
5653             self.operand1.constant_result,
5654             self.operand2.constant_result)
5655
5656     def compile_time_value(self, denv):
5657         func = get_compile_time_binop(self)
5658         operand1 = self.operand1.compile_time_value(denv)
5659         operand2 = self.operand2.compile_time_value(denv)
5660         try:
5661             return func(operand1, operand2)
5662         except Exception, e:
5663             self.compile_time_value_error(e)
5664
5665     def infer_type(self, env):
5666         return self.result_type(self.operand1.infer_type(env),
5667                                 self.operand2.infer_type(env))
5668
5669     def analyse_types(self, env):
5670         self.operand1.analyse_types(env)
5671         self.operand2.analyse_types(env)
5672         self.analyse_operation(env)
5673
5674     def analyse_operation(self, env):
5675         if self.is_py_operation():
5676             self.coerce_operands_to_pyobjects(env)
5677             self.type = self.result_type(self.operand1.type,
5678                                          self.operand2.type)
5679             assert self.type.is_pyobject
5680             self.is_temp = 1
5681         elif self.is_cpp_operation():
5682             self.analyse_cpp_operation(env)
5683         else:
5684             self.analyse_c_operation(env)
5685
5686     def is_py_operation(self):
5687         return self.is_py_operation_types(self.operand1.type, self.operand2.type)
5688
5689     def is_py_operation_types(self, type1, type2):
5690         return type1.is_pyobject or type2.is_pyobject
5691
5692     def is_cpp_operation(self):
5693         return (self.operand1.type.is_cpp_class
5694             or self.operand2.type.is_cpp_class)
5695
5696     def analyse_cpp_operation(self, env):
5697         type1 = self.operand1.type
5698         type2 = self.operand2.type
5699         entry = env.lookup_operator(self.operator, [self.operand1, self.operand2])
5700         if not entry:
5701             self.type_error()
5702             return
5703         func_type = entry.type
5704         if func_type.is_ptr:
5705             func_type = func_type.base_type
5706         if len(func_type.args) == 1:
5707             self.operand2 = self.operand2.coerce_to(func_type.args[0].type, env)
5708         else:
5709             self.operand1 = self.operand1.coerce_to(func_type.args[0].type, env)
5710             self.operand2 = self.operand2.coerce_to(func_type.args[1].type, env)
5711         self.type = func_type.return_type
5712
5713     def result_type(self, type1, type2):
5714         if self.is_py_operation_types(type1, type2):
5715             if type2.is_string:
5716                 type2 = Builtin.bytes_type
5717             if type1.is_string:
5718                 type1 = Builtin.bytes_type
5719             elif self.operator == '%' \
5720                      and type1 in (Builtin.str_type, Builtin.unicode_type):
5721                 # note that  b'%s' % b'abc'  doesn't work in Py3
5722                 return type1
5723             if type1.is_builtin_type:
5724                 if type1 is type2:
5725                     if self.operator in '**%+|&^':
5726                         # FIXME: at least these operators should be safe - others?
5727                         return type1
5728                 elif self.operator == '*':
5729                     if type1 in (Builtin.bytes_type, Builtin.str_type, Builtin.unicode_type):
5730                         return type1
5731                     # multiplication of containers/numbers with an
5732                     # integer value always (?) returns the same type
5733                     if type2.is_int:
5734                         return type1
5735             elif type2.is_builtin_type and type1.is_int and self.operator == '*':
5736                 # multiplication of containers/numbers with an
5737                 # integer value always (?) returns the same type
5738                 return type2
5739             return py_object_type
5740         else:
5741             return self.compute_c_result_type(type1, type2)
5742
5743     def nogil_check(self, env):
5744         if self.is_py_operation():
5745             self.gil_error()
5746
5747     def coerce_operands_to_pyobjects(self, env):
5748         self.operand1 = self.operand1.coerce_to_pyobject(env)
5749         self.operand2 = self.operand2.coerce_to_pyobject(env)
5750
5751     def check_const(self):
5752         return self.operand1.check_const() and self.operand2.check_const()
5753
5754     def generate_result_code(self, code):
5755         #print "BinopNode.generate_result_code:", self.operand1, self.operand2 ###
5756         if self.operand1.type.is_pyobject:
5757             function = self.py_operation_function()
5758             if self.operator == '**':
5759                 extra_args = ", Py_None"
5760             else:
5761                 extra_args = ""
5762             code.putln(
5763                 "%s = %s(%s, %s%s); %s" % (
5764                     self.result(),
5765                     function,
5766                     self.operand1.py_result(),
5767                     self.operand2.py_result(),
5768                     extra_args,
5769                     code.error_goto_if_null(self.result(), self.pos)))
5770             code.put_gotref(self.py_result())
5771
5772     def type_error(self):
5773         if not (self.operand1.type.is_error
5774                 or self.operand2.type.is_error):
5775             error(self.pos, "Invalid operand types for '%s' (%s; %s)" %
5776                 (self.operator, self.operand1.type,
5777                     self.operand2.type))
5778         self.type = PyrexTypes.error_type
5779
5780
5781 class CBinopNode(BinopNode):
5782
5783     def analyse_types(self, env):
5784         BinopNode.analyse_types(self, env)
5785         if self.is_py_operation():
5786             self.type = PyrexTypes.error_type
5787
5788     def py_operation_function():
5789         return ""
5790
5791     def calculate_result_code(self):
5792         return "(%s %s %s)" % (
5793             self.operand1.result(),
5794             self.operator,
5795             self.operand2.result())
5796
5797
5798 def c_binop_constructor(operator):
5799     def make_binop_node(pos, **operands):
5800         return CBinopNode(pos, operator=operator, **operands)
5801     return make_binop_node
5802
5803 class NumBinopNode(BinopNode):
5804     #  Binary operation taking numeric arguments.
5805
5806     infix = True
5807
5808     def analyse_c_operation(self, env):
5809         type1 = self.operand1.type
5810         type2 = self.operand2.type
5811         self.type = self.compute_c_result_type(type1, type2)
5812         if not self.type:
5813             self.type_error()
5814             return
5815         if self.type.is_complex:
5816             self.infix = False
5817         if not self.infix or (type1.is_numeric and type2.is_numeric):
5818             self.operand1 = self.operand1.coerce_to(self.type, env)
5819             self.operand2 = self.operand2.coerce_to(self.type, env)
5820
5821     def compute_c_result_type(self, type1, type2):
5822         if self.c_types_okay(type1, type2):
5823             widest_type = PyrexTypes.widest_numeric_type(type1, type2)
5824             if widest_type is PyrexTypes.c_bint_type:
5825                 if self.operator not in '|^&':
5826                     # False + False == 0 # not False!
5827                     widest_type = PyrexTypes.c_int_type
5828             return widest_type
5829         else:
5830             return None
5831
5832     def get_constant_c_result_code(self):
5833         value1 = self.operand1.get_constant_c_result_code()
5834         value2 = self.operand2.get_constant_c_result_code()
5835         if value1 and value2:
5836             return "(%s %s %s)" % (value1, self.operator, value2)
5837         else:
5838             return None
5839
5840     def c_types_okay(self, type1, type2):
5841         #print "NumBinopNode.c_types_okay:", type1, type2 ###
5842         return (type1.is_numeric  or type1.is_enum) \
5843             and (type2.is_numeric  or type2.is_enum)
5844
5845     def calculate_result_code(self):
5846         if self.infix:
5847             return "(%s %s %s)" % (
5848                 self.operand1.result(),
5849                 self.operator,
5850                 self.operand2.result())
5851         else:
5852             func = self.type.binary_op(self.operator)
5853             if func is None:
5854                 error(self.pos, "binary operator %s not supported for %s" % (self.operator, self.type))
5855             return "%s(%s, %s)" % (
5856                 func,
5857                 self.operand1.result(),
5858                 self.operand2.result())
5859
5860     def is_py_operation_types(self, type1, type2):
5861         return (type1.is_unicode_char or
5862                 type2.is_unicode_char or
5863                 BinopNode.is_py_operation_types(self, type1, type2))
5864
5865     def py_operation_function(self):
5866         fuction = self.py_functions[self.operator]
5867         if self.inplace:
5868             fuction = fuction.replace('PyNumber_', 'PyNumber_InPlace')
5869         return fuction
5870
5871     py_functions = {
5872         "|":        "PyNumber_Or",
5873         "^":        "PyNumber_Xor",
5874         "&":        "PyNumber_And",
5875         "<<":       "PyNumber_Lshift",
5876         ">>":       "PyNumber_Rshift",
5877         "+":        "PyNumber_Add",
5878         "-":        "PyNumber_Subtract",
5879         "*":        "PyNumber_Multiply",
5880         "/":        "__Pyx_PyNumber_Divide",
5881         "//":       "PyNumber_FloorDivide",
5882         "%":        "PyNumber_Remainder",
5883         "**":       "PyNumber_Power"
5884     }
5885
5886 class IntBinopNode(NumBinopNode):
5887     #  Binary operation taking integer arguments.
5888
5889     def c_types_okay(self, type1, type2):
5890         #print "IntBinopNode.c_types_okay:", type1, type2 ###
5891         return (type1.is_int or type1.is_enum) \
5892             and (type2.is_int or type2.is_enum)
5893
5894
5895 class AddNode(NumBinopNode):
5896     #  '+' operator.
5897
5898     def is_py_operation_types(self, type1, type2):
5899         if type1.is_string and type2.is_string:
5900             return 1
5901         else:
5902             return NumBinopNode.is_py_operation_types(self, type1, type2)
5903
5904     def compute_c_result_type(self, type1, type2):
5905         #print "AddNode.compute_c_result_type:", type1, self.operator, type2 ###
5906         if (type1.is_ptr or type1.is_array) and (type2.is_int or type2.is_enum):
5907             return type1
5908         elif (type2.is_ptr or type2.is_array) and (type1.is_int or type1.is_enum):
5909             return type2
5910         else:
5911             return NumBinopNode.compute_c_result_type(
5912                 self, type1, type2)
5913
5914
5915 class SubNode(NumBinopNode):
5916     #  '-' operator.
5917
5918     def compute_c_result_type(self, type1, type2):
5919         if (type1.is_ptr or type1.is_array) and (type2.is_int or type2.is_enum):
5920             return type1
5921         elif (type1.is_ptr or type1.is_array) and (type2.is_ptr or type2.is_array):
5922             return PyrexTypes.c_int_type
5923         else:
5924             return NumBinopNode.compute_c_result_type(
5925                 self, type1, type2)
5926
5927
5928 class MulNode(NumBinopNode):
5929     #  '*' operator.
5930
5931     def is_py_operation_types(self, type1, type2):
5932         if (type1.is_string and type2.is_int) \
5933             or (type2.is_string and type1.is_int):
5934                 return 1
5935         else:
5936             return NumBinopNode.is_py_operation_types(self, type1, type2)
5937
5938
5939 class DivNode(NumBinopNode):
5940     #  '/' or '//' operator.
5941
5942     cdivision = None
5943     truedivision = None   # == "unknown" if operator == '/'
5944     ctruedivision = False
5945     cdivision_warnings = False
5946     zerodivision_check = None
5947
5948     def find_compile_time_binary_operator(self, op1, op2):
5949         func = compile_time_binary_operators[self.operator]
5950         if self.operator == '/' and self.truedivision is None:
5951             # => true div for floats, floor div for integers
5952             if isinstance(op1, (int,long)) and isinstance(op2, (int,long)):
5953                 func = compile_time_binary_operators['//']
5954         return func
5955
5956     def calculate_constant_result(self):
5957         op1 = self.operand1.constant_result
5958         op2 = self.operand2.constant_result
5959         func = self.find_compile_time_binary_operator(op1, op2)
5960         self.constant_result = func(
5961             self.operand1.constant_result,
5962             self.operand2.constant_result)
5963
5964     def compile_time_value(self, denv):
5965         operand1 = self.operand1.compile_time_value(denv)
5966         operand2 = self.operand2.compile_time_value(denv)
5967         try:
5968             func = self.find_compile_time_binary_operator(
5969                 self, operand1, operand2)
5970             return func(operand1, operand2)
5971         except Exception, e:
5972             self.compile_time_value_error(e)
5973
5974     def analyse_operation(self, env):
5975         if self.cdivision or env.directives['cdivision']:
5976             self.ctruedivision = False
5977         else:
5978             self.ctruedivision = self.truedivision
5979         NumBinopNode.analyse_operation(self, env)
5980         if self.is_cpp_operation():
5981             self.cdivision = True
5982         if not self.type.is_pyobject:
5983             self.zerodivision_check = (
5984                 self.cdivision is None and not env.directives['cdivision']
5985                 and (not self.operand2.has_constant_result() or
5986                      self.operand2.constant_result == 0))
5987             if self.zerodivision_check or env.directives['cdivision_warnings']:
5988                 # Need to check ahead of time to warn or raise zero division error
5989                 self.operand1 = self.operand1.coerce_to_simple(env)
5990                 self.operand2 = self.operand2.coerce_to_simple(env)
5991                 if env.nogil:
5992                     error(self.pos, "Pythonic division not allowed without gil, consider using cython.cdivision(True)")
5993
5994     def compute_c_result_type(self, type1, type2):
5995         if self.operator == '/' and self.ctruedivision:
5996             if not type1.is_float and not type2.is_float:
5997                 widest_type = PyrexTypes.widest_numeric_type(type1, PyrexTypes.c_double_type)
5998                 widest_type = PyrexTypes.widest_numeric_type(type2, widest_type)
5999                 return widest_type
6000         return NumBinopNode.compute_c_result_type(self, type1, type2)
6001
6002     def zero_division_message(self):
6003         if self.type.is_int:
6004             return "integer division or modulo by zero"
6005         else:
6006             return "float division"
6007
6008     def generate_evaluation_code(self, code):
6009         if not self.type.is_pyobject and not self.type.is_complex:
6010             if self.cdivision is None:
6011                 self.cdivision = (code.globalstate.directives['cdivision']
6012                                     or not self.type.signed
6013                                     or self.type.is_float)
6014             if not self.cdivision:
6015                 code.globalstate.use_utility_code(div_int_utility_code.specialize(self.type))
6016         NumBinopNode.generate_evaluation_code(self, code)
6017         self.generate_div_warning_code(code)
6018
6019     def generate_div_warning_code(self, code):
6020         if not self.type.is_pyobject:
6021             if self.zerodivision_check:
6022                 if not self.infix:
6023                     zero_test = "%s(%s)" % (self.type.unary_op('zero'), self.operand2.result())
6024                 else:
6025                     zero_test = "%s == 0" % self.operand2.result()
6026                 code.putln("if (unlikely(%s)) {" % zero_test)
6027                 code.putln('PyErr_Format(PyExc_ZeroDivisionError, "%s");' % self.zero_division_message())
6028                 code.putln(code.error_goto(self.pos))
6029                 code.putln("}")
6030                 if self.type.is_int and self.type.signed and self.operator != '%':
6031                     code.globalstate.use_utility_code(division_overflow_test_code)
6032                     code.putln("else if (sizeof(%s) == sizeof(long) && unlikely(%s == -1) && unlikely(UNARY_NEG_WOULD_OVERFLOW(%s))) {" % (
6033                                     self.type.declaration_code(''),
6034                                     self.operand2.result(),
6035                                     self.operand1.result()))
6036                     code.putln('PyErr_Format(PyExc_OverflowError, "value too large to perform division");')
6037                     code.putln(code.error_goto(self.pos))
6038                     code.putln("}")
6039             if code.globalstate.directives['cdivision_warnings'] and self.operator != '/':
6040                 code.globalstate.use_utility_code(cdivision_warning_utility_code)
6041                 code.putln("if ((%s < 0) ^ (%s < 0)) {" % (
6042                                 self.operand1.result(),
6043                                 self.operand2.result()))
6044                 code.putln(code.set_error_info(self.pos));
6045                 code.put("if (__Pyx_cdivision_warning()) ")
6046                 code.put_goto(code.error_label)
6047                 code.putln("}")
6048
6049     def calculate_result_code(self):
6050         if self.type.is_complex:
6051             return NumBinopNode.calculate_result_code(self)
6052         elif self.type.is_float and self.operator == '//':
6053             return "floor(%s / %s)" % (
6054                 self.operand1.result(),
6055                 self.operand2.result())
6056         elif self.truedivision or self.cdivision:
6057             op1 = self.operand1.result()
6058             op2 = self.operand2.result()
6059             if self.truedivision:
6060                 if self.type != self.operand1.type:
6061                     op1 = self.type.cast_code(op1)
6062                 if self.type != self.operand2.type:
6063                     op2 = self.type.cast_code(op2)
6064             return "(%s / %s)" % (op1, op2)
6065         else:
6066             return "__Pyx_div_%s(%s, %s)" % (
6067                     self.type.specialization_name(),
6068                     self.operand1.result(),
6069                     self.operand2.result())
6070
6071
6072 class ModNode(DivNode):
6073     #  '%' operator.
6074
6075     def is_py_operation_types(self, type1, type2):
6076         return (type1.is_string
6077             or type2.is_string
6078             or NumBinopNode.is_py_operation_types(self, type1, type2))
6079
6080     def zero_division_message(self):
6081         if self.type.is_int:
6082             return "integer division or modulo by zero"
6083         else:
6084             return "float divmod()"
6085
6086     def generate_evaluation_code(self, code):
6087         if not self.type.is_pyobject:
6088             if self.cdivision is None:
6089                 self.cdivision = code.globalstate.directives['cdivision'] or not self.type.signed
6090             if not self.cdivision:
6091                 if self.type.is_int:
6092                     code.globalstate.use_utility_code(mod_int_utility_code.specialize(self.type))
6093                 else:
6094                     code.globalstate.use_utility_code(
6095                         mod_float_utility_code.specialize(self.type, math_h_modifier=self.type.math_h_modifier))
6096         NumBinopNode.generate_evaluation_code(self, code)
6097         self.generate_div_warning_code(code)
6098
6099     def calculate_result_code(self):
6100         if self.cdivision:
6101             if self.type.is_float:
6102                 return "fmod%s(%s, %s)" % (
6103                     self.type.math_h_modifier,
6104                     self.operand1.result(),
6105                     self.operand2.result())
6106             else:
6107                 return "(%s %% %s)" % (
6108                     self.operand1.result(),
6109                     self.operand2.result())
6110         else:
6111             return "__Pyx_mod_%s(%s, %s)" % (
6112                     self.type.specialization_name(),
6113                     self.operand1.result(),
6114                     self.operand2.result())
6115
6116 class PowNode(NumBinopNode):
6117     #  '**' operator.
6118
6119     def analyse_c_operation(self, env):
6120         NumBinopNode.analyse_c_operation(self, env)
6121         if self.type.is_complex:
6122             if self.type.real_type.is_float:
6123                 self.operand1 = self.operand1.coerce_to(self.type, env)
6124                 self.operand2 = self.operand2.coerce_to(self.type, env)
6125                 self.pow_func = "__Pyx_c_pow" + self.type.real_type.math_h_modifier
6126             else:
6127                 error(self.pos, "complex int powers not supported")
6128                 self.pow_func = "<error>"
6129         elif self.type.is_float:
6130             self.pow_func = "pow" + self.type.math_h_modifier
6131         else:
6132             self.pow_func = "__Pyx_pow_%s" % self.type.declaration_code('').replace(' ', '_')
6133             env.use_utility_code(
6134                     int_pow_utility_code.specialize(func_name=self.pow_func,
6135                                                 type=self.type.declaration_code('')))
6136
6137     def calculate_result_code(self):
6138         # Work around MSVC overloading ambiguity.
6139         def typecast(operand):
6140             if self.type == operand.type:
6141                 return operand.result()
6142             else:
6143                 return self.type.cast_code(operand.result())
6144         return "%s(%s, %s)" % (
6145             self.pow_func,
6146             typecast(self.operand1),
6147             typecast(self.operand2))
6148
6149
6150 # Note: This class is temporarily "shut down" into an ineffective temp
6151 # allocation mode.
6152 #
6153 # More sophisticated temp reuse was going on before, one could have a
6154 # look at adding this again after /all/ classes are converted to the
6155 # new temp scheme. (The temp juggling cannot work otherwise).
6156 class BoolBinopNode(ExprNode):
6157     #  Short-circuiting boolean operation.
6158     #
6159     #  operator     string
6160     #  operand1     ExprNode
6161     #  operand2     ExprNode
6162
6163     subexprs = ['operand1', 'operand2']
6164
6165     def infer_type(self, env):
6166         type1 = self.operand1.infer_type(env)
6167         type2 = self.operand2.infer_type(env)
6168         return PyrexTypes.independent_spanning_type(type1, type2)
6169
6170     def may_be_none(self):
6171         if self.operator == 'or':
6172             return self.operand2.may_be_none()
6173         else:
6174             return self.operand1.may_be_none() or self.operand2.may_be_none()
6175
6176     def calculate_constant_result(self):
6177         if self.operator == 'and':
6178             self.constant_result = \
6179                 self.operand1.constant_result and \
6180                 self.operand2.constant_result
6181         else:
6182             self.constant_result = \
6183                 self.operand1.constant_result or \
6184                 self.operand2.constant_result
6185
6186     def compile_time_value(self, denv):
6187         if self.operator == 'and':
6188             return self.operand1.compile_time_value(denv) \
6189                 and self.operand2.compile_time_value(denv)
6190         else:
6191             return self.operand1.compile_time_value(denv) \
6192                 or self.operand2.compile_time_value(denv)
6193
6194     def coerce_to_boolean(self, env):
6195         return BoolBinopNode(
6196             self.pos,
6197             operator = self.operator,
6198             operand1 = self.operand1.coerce_to_boolean(env),
6199             operand2 = self.operand2.coerce_to_boolean(env),
6200             type = PyrexTypes.c_bint_type,
6201             is_temp = self.is_temp)
6202
6203     def analyse_types(self, env):
6204         self.operand1.analyse_types(env)
6205         self.operand2.analyse_types(env)
6206         self.type = PyrexTypes.independent_spanning_type(self.operand1.type, self.operand2.type)
6207         self.operand1 = self.operand1.coerce_to(self.type, env)
6208         self.operand2 = self.operand2.coerce_to(self.type, env)
6209
6210         # For what we're about to do, it's vital that
6211         # both operands be temp nodes.
6212         self.operand1 = self.operand1.coerce_to_simple(env)
6213         self.operand2 = self.operand2.coerce_to_simple(env)
6214         self.is_temp = 1
6215
6216     gil_message = "Truth-testing Python object"
6217
6218     def check_const(self):
6219         return self.operand1.check_const() and self.operand2.check_const()
6220
6221     def generate_evaluation_code(self, code):
6222         code.mark_pos(self.pos)
6223         self.operand1.generate_evaluation_code(code)
6224         test_result, uses_temp = self.generate_operand1_test(code)
6225         if self.operator == 'and':
6226             sense = ""
6227         else:
6228             sense = "!"
6229         code.putln(
6230             "if (%s%s) {" % (
6231                 sense,
6232                 test_result))
6233         if uses_temp:
6234             code.funcstate.release_temp(test_result)
6235         self.operand1.generate_disposal_code(code)
6236         self.operand2.generate_evaluation_code(code)
6237         self.allocate_temp_result(code)
6238         self.operand2.make_owned_reference(code)
6239         code.putln("%s = %s;" % (self.result(), self.operand2.result()))
6240         self.operand2.generate_post_assignment_code(code)
6241         self.operand2.free_temps(code)
6242         code.putln("} else {")
6243         self.operand1.make_owned_reference(code)
6244         code.putln("%s = %s;" % (self.result(), self.operand1.result()))
6245         self.operand1.generate_post_assignment_code(code)
6246         self.operand1.free_temps(code)
6247         code.putln("}")
6248
6249     def generate_operand1_test(self, code):
6250         #  Generate code to test the truth of the first operand.
6251         if self.type.is_pyobject:
6252             test_result = code.funcstate.allocate_temp(PyrexTypes.c_bint_type,
6253                                                        manage_ref=False)
6254             code.putln(
6255                 "%s = __Pyx_PyObject_IsTrue(%s); %s" % (
6256                     test_result,
6257                     self.operand1.py_result(),
6258                     code.error_goto_if_neg(test_result, self.pos)))
6259         else:
6260             test_result = self.operand1.result()
6261         return (test_result, self.type.is_pyobject)
6262
6263
6264 class CondExprNode(ExprNode):
6265     #  Short-circuiting conditional expression.
6266     #
6267     #  test        ExprNode
6268     #  true_val    ExprNode
6269     #  false_val   ExprNode
6270
6271     true_val = None
6272     false_val = None
6273
6274     subexprs = ['test', 'true_val', 'false_val']
6275
6276     def type_dependencies(self, env):
6277         return self.true_val.type_dependencies(env) + self.false_val.type_dependencies(env)
6278
6279     def infer_type(self, env):
6280         return PyrexTypes.independent_spanning_type(self.true_val.infer_type(env),
6281                                                     self.false_val.infer_type(env))
6282
6283     def calculate_constant_result(self):
6284         if self.test.constant_result:
6285             self.constant_result = self.true_val.constant_result
6286         else:
6287             self.constant_result = self.false_val.constant_result
6288
6289     def analyse_types(self, env):
6290         self.test.analyse_types(env)
6291         self.test = self.test.coerce_to_boolean(env)
6292         self.true_val.analyse_types(env)
6293         self.false_val.analyse_types(env)
6294         self.type = PyrexTypes.independent_spanning_type(self.true_val.type, self.false_val.type)
6295         if self.true_val.type.is_pyobject or self.false_val.type.is_pyobject:
6296             self.true_val = self.true_val.coerce_to(self.type, env)
6297             self.false_val = self.false_val.coerce_to(self.type, env)
6298         self.is_temp = 1
6299         if self.type == PyrexTypes.error_type:
6300             self.type_error()
6301
6302     def type_error(self):
6303         if not (self.true_val.type.is_error or self.false_val.type.is_error):
6304             error(self.pos, "Incompatable types in conditional expression (%s; %s)" %
6305                 (self.true_val.type, self.false_val.type))
6306         self.type = PyrexTypes.error_type
6307
6308     def check_const(self):
6309         return (self.test.check_const()
6310             and self.true_val.check_const()
6311             and self.false_val.check_const())
6312
6313     def generate_evaluation_code(self, code):
6314         # Because subexprs may not be evaluated we can use a more optimal
6315         # subexpr allocation strategy than the default, so override evaluation_code.
6316
6317         code.mark_pos(self.pos)
6318         self.allocate_temp_result(code)
6319         self.test.generate_evaluation_code(code)
6320         code.putln("if (%s) {" % self.test.result() )
6321         self.eval_and_get(code, self.true_val)
6322         code.putln("} else {")
6323         self.eval_and_get(code, self.false_val)
6324         code.putln("}")
6325         self.test.generate_disposal_code(code)
6326         self.test.free_temps(code)
6327
6328     def eval_and_get(self, code, expr):
6329         expr.generate_evaluation_code(code)
6330         expr.make_owned_reference(code)
6331         code.putln("%s = %s;" % (self.result(), expr.result()))
6332         expr.generate_post_assignment_code(code)
6333         expr.free_temps(code)
6334
6335 richcmp_constants = {
6336     "<" : "Py_LT",
6337     "<=": "Py_LE",
6338     "==": "Py_EQ",
6339     "!=": "Py_NE",
6340     "<>": "Py_NE",
6341     ">" : "Py_GT",
6342     ">=": "Py_GE",
6343 }
6344
6345 class CmpNode(object):
6346     #  Mixin class containing code common to PrimaryCmpNodes
6347     #  and CascadedCmpNodes.
6348
6349     special_bool_cmp_function = None
6350
6351     def infer_type(self, env):
6352         # TODO: Actually implement this (after merging with -unstable).
6353         return py_object_type
6354
6355     def calculate_cascaded_constant_result(self, operand1_result):
6356         func = compile_time_binary_operators[self.operator]
6357         operand2_result = self.operand2.constant_result
6358         result = func(operand1_result, operand2_result)
6359         if self.cascade:
6360             self.cascade.calculate_cascaded_constant_result(operand2_result)
6361             if self.cascade.constant_result:
6362                 self.constant_result = result and self.cascade.constant_result
6363         else:
6364             self.constant_result = result
6365
6366     def cascaded_compile_time_value(self, operand1, denv):
6367         func = get_compile_time_binop(self)
6368         operand2 = self.operand2.compile_time_value(denv)
6369         try:
6370             result = func(operand1, operand2)
6371         except Exception, e:
6372             self.compile_time_value_error(e)
6373             result = None
6374         if result:
6375             cascade = self.cascade
6376             if cascade:
6377                 # FIXME: I bet this must call cascaded_compile_time_value()
6378                 result = result and cascade.cascaded_compile_time_value(operand2, denv)
6379         return result
6380
6381     def is_cpp_comparison(self):
6382         return self.operand1.type.is_cpp_class or self.operand2.type.is_cpp_class
6383
6384     def find_common_int_type(self, env, op, operand1, operand2):
6385         # type1 != type2 and at least one of the types is not a C int
6386         type1 = operand1.type
6387         type2 = operand2.type
6388         type1_can_be_int = False
6389         type2_can_be_int = False
6390
6391         if isinstance(operand1, (StringNode, BytesNode, UnicodeNode)) \
6392                and operand1.can_coerce_to_char_literal():
6393             type1_can_be_int = True
6394         if isinstance(operand2, (StringNode, BytesNode, UnicodeNode)) \
6395                  and operand2.can_coerce_to_char_literal():
6396             type2_can_be_int = True
6397
6398         if type1.is_int:
6399             if type2_can_be_int:
6400                 return type1
6401         elif type2.is_int:
6402             if type1_can_be_int:
6403                 return type2
6404         elif type1_can_be_int:
6405             if type2_can_be_int:
6406                 return PyrexTypes.c_uchar_type
6407
6408         return None
6409
6410     def find_common_type(self, env, op, operand1, common_type=None):
6411         operand2 = self.operand2
6412         type1 = operand1.type
6413         type2 = operand2.type
6414
6415         new_common_type = None
6416
6417         # catch general errors
6418         if type1 == str_type and (type2.is_string or type2 in (bytes_type, unicode_type)) or \
6419                type2 == str_type and (type1.is_string or type1 in (bytes_type, unicode_type)):
6420             error(self.pos, "Comparisons between bytes/unicode and str are not portable to Python 3")
6421             new_common_type = error_type
6422
6423         # try to use numeric comparisons where possible
6424         elif type1.is_complex or type2.is_complex:
6425             if op not in ('==', '!='):
6426                 error(self.pos, "complex types are unordered")
6427                 new_common_type = error_type
6428             if type1.is_pyobject:
6429                 new_common_type = type1
6430             elif type2.is_pyobject:
6431                 new_common_type = type2
6432             else:
6433                 new_common_type = PyrexTypes.widest_numeric_type(type1, type2)
6434         elif type1.is_numeric and type2.is_numeric:
6435             new_common_type = PyrexTypes.widest_numeric_type(type1, type2)
6436         elif common_type is None or not common_type.is_pyobject:
6437             new_common_type = self.find_common_int_type(env, op, operand1, operand2)
6438
6439         if new_common_type is None:
6440             # fall back to generic type compatibility tests
6441             if type1 == type2:
6442                 new_common_type = type1
6443             elif type1.is_pyobject or type2.is_pyobject:
6444                 if type2.is_numeric or type2.is_string:
6445                     if operand2.check_for_coercion_error(type1):
6446                         new_common_type = error_type
6447                     else:
6448                         new_common_type = py_object_type
6449                 elif type1.is_numeric or type1.is_string:
6450                     if operand1.check_for_coercion_error(type2):
6451                         new_common_type = error_type
6452                     else:
6453                         new_common_type = py_object_type
6454                 elif py_object_type.assignable_from(type1) and py_object_type.assignable_from(type2):
6455                     new_common_type = py_object_type
6456                 else:
6457                     # one Python type and one non-Python type, not assignable
6458                     self.invalid_types_error(operand1, op, operand2)
6459                     new_common_type = error_type
6460             elif type1.assignable_from(type2):
6461                 new_common_type = type1
6462             elif type2.assignable_from(type1):
6463                 new_common_type = type2
6464             else:
6465                 # C types that we couldn't handle up to here are an error
6466                 self.invalid_types_error(operand1, op, operand2)
6467                 new_common_type = error_type
6468
6469         if new_common_type.is_string and (isinstance(operand1, BytesNode) or
6470                                           isinstance(operand2, BytesNode)):
6471             # special case when comparing char* to bytes literal: must
6472             # compare string values!
6473             new_common_type = bytes_type
6474
6475         # recursively merge types
6476         if common_type is None or new_common_type.is_error:
6477             common_type = new_common_type
6478         else:
6479             # we could do a lot better by splitting the comparison
6480             # into a non-Python part and a Python part, but this is
6481             # safer for now
6482             common_type = PyrexTypes.spanning_type(common_type, new_common_type)
6483
6484         if self.cascade:
6485             common_type = self.cascade.find_common_type(env, self.operator, operand2, common_type)
6486
6487         return common_type
6488
6489     def invalid_types_error(self, operand1, op, operand2):
6490         error(self.pos, "Invalid types for '%s' (%s, %s)" %
6491               (op, operand1.type, operand2.type))
6492
6493     def is_python_comparison(self):
6494         return (not self.is_ptr_contains()
6495             and not self.is_c_string_contains()
6496             and (self.has_python_operands()
6497                  or (self.cascade and self.cascade.is_python_comparison())
6498                  or self.operator in ('in', 'not_in')))
6499
6500     def coerce_operands_to(self, dst_type, env):
6501         operand2 = self.operand2
6502         if operand2.type != dst_type:
6503             self.operand2 = operand2.coerce_to(dst_type, env)
6504         if self.cascade:
6505             self.cascade.coerce_operands_to(dst_type, env)
6506
6507     def is_python_result(self):
6508         return ((self.has_python_operands() and
6509                  self.special_bool_cmp_function is None and
6510                  self.operator not in ('is', 'is_not', 'in', 'not_in') and
6511                  not self.is_c_string_contains() and
6512                  not self.is_ptr_contains())
6513             or (self.cascade and self.cascade.is_python_result()))
6514
6515     def is_c_string_contains(self):
6516         return self.operator in ('in', 'not_in') and \
6517                ((self.operand1.type.is_int
6518                  and (self.operand2.type.is_string or self.operand2.type is bytes_type)) or
6519                 (self.operand1.type.is_unicode_char
6520                  and self.operand2.type is unicode_type))
6521
6522     def is_ptr_contains(self):
6523         if self.operator in ('in', 'not_in'):
6524             container_type = self.operand2.type
6525             return (container_type.is_ptr or container_type.is_array) \
6526                 and not container_type.is_string
6527
6528     def find_special_bool_compare_function(self, env):
6529         if self.operator in ('==', '!='):
6530             type1, type2 = self.operand1.type, self.operand2.type
6531             if type1.is_pyobject and type2.is_pyobject:
6532                 if type1 is Builtin.unicode_type or type2 is Builtin.unicode_type:
6533                     env.use_utility_code(pyunicode_equals_utility_code)
6534                     self.special_bool_cmp_function = "__Pyx_PyUnicode_Equals"
6535                     return True
6536         return False
6537
6538     def generate_operation_code(self, code, result_code,
6539             operand1, op , operand2):
6540         if self.type.is_pyobject:
6541             coerce_result = "__Pyx_PyBool_FromLong"
6542         else:
6543             coerce_result = ""
6544         if 'not' in op:
6545             negation = "!"
6546         else:
6547             negation = ""
6548         if self.special_bool_cmp_function:
6549             if operand1.type.is_pyobject:
6550                 result1 = operand1.py_result()
6551             else:
6552                 result1 = operand1.result()
6553             if operand2.type.is_pyobject:
6554                 result2 = operand2.py_result()
6555             else:
6556                 result2 = operand2.result()
6557             code.putln("%s = %s(%s, %s, %s); %s" % (
6558                 result_code,
6559                 self.special_bool_cmp_function,
6560                 result1,
6561                 result2,
6562                 richcmp_constants[op],
6563                 code.error_goto_if_neg(result_code, self.pos)))
6564         elif op == 'in' or op == 'not_in':
6565             code.globalstate.use_utility_code(contains_utility_code)
6566             if self.type.is_pyobject:
6567                 coerce_result = "__Pyx_PyBoolOrNull_FromLong"
6568             if op == 'not_in':
6569                 negation = "__Pyx_NegateNonNeg"
6570             if operand2.type is dict_type:
6571                 method = "PyDict_Contains"
6572             else:
6573                 method = "PySequence_Contains"
6574             if self.type.is_pyobject:
6575                 error_clause = code.error_goto_if_null
6576                 got_ref = "__Pyx_XGOTREF(%s); " % result_code
6577             else:
6578                 error_clause = code.error_goto_if_neg
6579                 got_ref = ""
6580             code.putln(
6581                 "%s = %s(%s(%s(%s, %s))); %s%s" % (
6582                     result_code,
6583                     coerce_result,
6584                     negation,
6585                     method,
6586                     operand2.py_result(),
6587                     operand1.py_result(),
6588                     got_ref,
6589                     error_clause(result_code, self.pos)))
6590         elif (operand1.type.is_pyobject
6591             and op not in ('is', 'is_not')):
6592                 code.putln("%s = PyObject_RichCompare(%s, %s, %s); %s" % (
6593                         result_code,
6594                         operand1.py_result(),
6595                         operand2.py_result(),
6596                         richcmp_constants[op],
6597                         code.error_goto_if_null(result_code, self.pos)))
6598                 code.put_gotref(result_code)
6599         elif operand1.type.is_complex:
6600             if op == "!=":
6601                 negation = "!"
6602             else:
6603                 negation = ""
6604             code.putln("%s = %s(%s%s(%s, %s));" % (
6605                 result_code,
6606                 coerce_result,
6607                 negation,
6608                 operand1.type.unary_op('eq'),
6609                 operand1.result(),
6610                 operand2.result()))
6611         else:
6612             type1 = operand1.type
6613             type2 = operand2.type
6614             if (type1.is_extension_type or type2.is_extension_type) \
6615                     and not type1.same_as(type2):
6616                 common_type = py_object_type
6617             elif type1.is_numeric:
6618                 common_type = PyrexTypes.widest_numeric_type(type1, type2)
6619             else:
6620                 common_type = type1
6621             code1 = operand1.result_as(common_type)
6622             code2 = operand2.result_as(common_type)
6623             code.putln("%s = %s(%s %s %s);" % (
6624                 result_code,
6625                 coerce_result,
6626                 code1,
6627                 self.c_operator(op),
6628                 code2))
6629
6630     def c_operator(self, op):
6631         if op == 'is':
6632             return "=="
6633         elif op == 'is_not':
6634             return "!="
6635         else:
6636             return op
6637
6638 contains_utility_code = UtilityCode(
6639 proto="""
6640 static CYTHON_INLINE long __Pyx_NegateNonNeg(long b) { return unlikely(b < 0) ? b : !b; }
6641 static CYTHON_INLINE PyObject* __Pyx_PyBoolOrNull_FromLong(long b) {
6642     return unlikely(b < 0) ? NULL : __Pyx_PyBool_FromLong(b);
6643 }
6644 """)
6645
6646 char_in_bytes_utility_code = UtilityCode(
6647 proto="""
6648 static CYTHON_INLINE int __Pyx_BytesContains(PyObject* bytes, char character); /*proto*/
6649 """,
6650 impl="""
6651 static CYTHON_INLINE int __Pyx_BytesContains(PyObject* bytes, char character) {
6652     const Py_ssize_t length = PyBytes_GET_SIZE(bytes);
6653     char* char_start = PyBytes_AS_STRING(bytes);
6654     char* pos;
6655     for (pos=char_start; pos < char_start+length; pos++) {
6656         if (character == pos[0]) return 1;
6657     }
6658     return 0;
6659 }
6660 """)
6661
6662 pyunicode_in_unicode_utility_code = UtilityCode(
6663 proto="""
6664 static CYTHON_INLINE int __Pyx_UnicodeContains(PyObject* unicode, Py_UNICODE character); /*proto*/
6665 """,
6666 impl="""
6667 static CYTHON_INLINE int __Pyx_UnicodeContains(PyObject* unicode, Py_UNICODE character) {
6668     Py_UNICODE* pos;
6669     const Py_ssize_t length = PyUnicode_GET_SIZE(unicode);
6670     Py_UNICODE* char_start = PyUnicode_AS_UNICODE(unicode);
6671
6672     for (pos=char_start; pos < char_start+length; pos++) {
6673         if (unlikely(character == pos[0])) return 1;
6674     }
6675     return 0;
6676 }
6677 """)
6678
6679 py_ucs4_in_unicode_utility_code = UtilityCode(
6680 proto="""
6681 static CYTHON_INLINE int __Pyx_UnicodeContainsUCS4(PyObject* unicode, Py_UCS4 character); /*proto*/
6682 """,
6683 # additionally handles surrogate pairs in 16bit Unicode builds
6684 impl="""
6685 static CYTHON_INLINE int __Pyx_UnicodeContainsUCS4(PyObject* unicode, Py_UCS4 character) {
6686     Py_UNICODE* pos;
6687     Py_UNICODE uchar;
6688     const Py_ssize_t length = PyUnicode_GET_SIZE(unicode);
6689     Py_UNICODE* char_start = PyUnicode_AS_UNICODE(unicode);
6690
6691     #if Py_UNICODE_SIZE == 2
6692     if (unlikely(character > 65535)) {
6693         Py_UNICODE high_val, low_val;
6694         high_val = (Py_UNICODE) (0xD800 | (((character - 0x10000) >> 10) & ((1<<10)-1)));
6695         low_val  = (Py_UNICODE) (0xDC00 | ( (character - 0x10000)        & ((1<<10)-1)));
6696         for (pos=char_start; pos < char_start+length-1; pos++) {
6697             if (unlikely(high_val == pos[0]) & unlikely(low_val == pos[1])) return 1;
6698         }
6699         return 0;
6700     }
6701     #endif
6702     uchar = (Py_UNICODE) character;
6703     for (pos=char_start; pos < char_start+length; pos++) {
6704         if (unlikely(uchar == pos[0])) return 1;
6705     }
6706     return 0;
6707 }
6708 """)
6709
6710 pyunicode_equals_utility_code = UtilityCode(
6711 proto="""
6712 static CYTHON_INLINE int __Pyx_PyUnicode_Equals(PyObject* s1, PyObject* s2, int equals); /*proto*/
6713 """,
6714 impl="""
6715 static CYTHON_INLINE int __Pyx_PyUnicode_Equals(PyObject* s1, PyObject* s2, int equals) {
6716     if (s1 == s2) {   /* as done by PyObject_RichCompareBool(); also catches the (interned) empty string */
6717         return (equals == Py_EQ);
6718     } else if (PyUnicode_CheckExact(s1) & PyUnicode_CheckExact(s2)) {
6719         if (PyUnicode_GET_SIZE(s1) != PyUnicode_GET_SIZE(s2)) {
6720             return (equals == Py_NE);
6721         } else if (PyUnicode_GET_SIZE(s1) == 1) {
6722             if (equals == Py_EQ)
6723                 return (PyUnicode_AS_UNICODE(s1)[0] == PyUnicode_AS_UNICODE(s2)[0]);
6724             else
6725                 return (PyUnicode_AS_UNICODE(s1)[0] != PyUnicode_AS_UNICODE(s2)[0]);
6726         } else {
6727             int result = PyUnicode_Compare(s1, s2);
6728             if ((result == -1) && unlikely(PyErr_Occurred()))
6729                 return -1;
6730             return (equals == Py_EQ) ? (result == 0) : (result != 0);
6731         }
6732     } else if ((s1 == Py_None) & (s2 == Py_None)) {
6733         return (equals == Py_EQ);
6734     } else if ((s1 == Py_None) & PyUnicode_CheckExact(s2)) {
6735         return (equals == Py_NE);
6736     } else if ((s2 == Py_None) & PyUnicode_CheckExact(s1)) {
6737         return (equals == Py_NE);
6738     } else {
6739         int result;
6740         PyObject* py_result = PyObject_RichCompare(s1, s2, equals);
6741         if (!py_result)
6742             return -1;
6743         result = __Pyx_PyObject_IsTrue(py_result);
6744         Py_DECREF(py_result);
6745         return result;
6746     }
6747 }
6748 """)
6749
6750
6751 class PrimaryCmpNode(ExprNode, CmpNode):
6752     #  Non-cascaded comparison or first comparison of
6753     #  a cascaded sequence.
6754     #
6755     #  operator      string
6756     #  operand1      ExprNode
6757     #  operand2      ExprNode
6758     #  cascade       CascadedCmpNode
6759
6760     #  We don't use the subexprs mechanism, because
6761     #  things here are too complicated for it to handle.
6762     #  Instead, we override all the framework methods
6763     #  which use it.
6764
6765     child_attrs = ['operand1', 'operand2', 'cascade']
6766
6767     cascade = None
6768
6769     def infer_type(self, env):
6770         # TODO: Actually implement this (after merging with -unstable).
6771         return py_object_type
6772
6773     def type_dependencies(self, env):
6774         return ()
6775
6776     def calculate_constant_result(self):
6777         self.calculate_cascaded_constant_result(self.operand1.constant_result)
6778
6779     def compile_time_value(self, denv):
6780         operand1 = self.operand1.compile_time_value(denv)
6781         return self.cascaded_compile_time_value(operand1, denv)
6782
6783     def analyse_types(self, env):
6784         self.operand1.analyse_types(env)
6785         self.operand2.analyse_types(env)
6786         if self.is_cpp_comparison():
6787             self.analyse_cpp_comparison(env)
6788             if self.cascade:
6789                 error(self.pos, "Cascading comparison not yet supported for cpp types.")
6790             return
6791         if self.cascade:
6792             self.cascade.analyse_types(env)
6793
6794         if self.operator in ('in', 'not_in'):
6795             if self.is_c_string_contains():
6796                 self.is_pycmp = False
6797                 common_type = None
6798                 if self.cascade:
6799                     error(self.pos, "Cascading comparison not yet supported for 'int_val in string'.")
6800                     return
6801                 if self.operand2.type is unicode_type:
6802                     self.uchar_test_type = PyrexTypes.widest_numeric_type(
6803                         self.operand1.type, PyrexTypes.c_py_unicode_type)
6804                     if self.uchar_test_type is PyrexTypes.c_py_unicode_type:
6805                         env.use_utility_code(pyunicode_in_unicode_utility_code)
6806                     else:
6807                         env.use_utility_code(py_ucs4_in_unicode_utility_code)
6808                 else:
6809                     if self.operand1.type is PyrexTypes.c_uchar_type:
6810                         self.operand1 = self.operand1.coerce_to(PyrexTypes.c_char_type, env)
6811                     if self.operand2.type is not bytes_type:
6812                         self.operand2 = self.operand2.coerce_to(bytes_type, env)
6813                     env.use_utility_code(char_in_bytes_utility_code)
6814                 self.operand2 = self.operand2.as_none_safe_node(
6815                     "argument of type 'NoneType' is not iterable")
6816             elif self.is_ptr_contains():
6817                 if self.cascade:
6818                     error(self.pos, "Cascading comparison not yet supported for 'val in sliced pointer'.")
6819                 self.type = PyrexTypes.c_bint_type
6820                 # Will be transformed by IterationTransform
6821                 return
6822             else:
6823                 if self.operand2.type is dict_type:
6824                     self.operand2 = self.operand2.as_none_safe_node("'NoneType' object is not iterable")
6825                 common_type = py_object_type
6826                 self.is_pycmp = True
6827         elif self.find_special_bool_compare_function(env):
6828             common_type = None # if coercion needed, the method call above has already done it
6829             self.is_pycmp = False # result is bint
6830             self.is_temp = True # must check for error return
6831         else:
6832             common_type = self.find_common_type(env, self.operator, self.operand1)
6833             self.is_pycmp = common_type.is_pyobject
6834
6835         if common_type is not None and not common_type.is_error:
6836             if self.operand1.type != common_type:
6837                 self.operand1 = self.operand1.coerce_to(common_type, env)
6838             self.coerce_operands_to(common_type, env)
6839
6840         if self.cascade:
6841             self.operand2 = self.operand2.coerce_to_simple(env)
6842             self.cascade.coerce_cascaded_operands_to_temp(env)
6843         if self.is_python_result():
6844             self.type = PyrexTypes.py_object_type
6845         else:
6846             self.type = PyrexTypes.c_bint_type
6847         cdr = self.cascade
6848         while cdr:
6849             cdr.type = self.type
6850             cdr = cdr.cascade
6851         if self.is_pycmp or self.cascade:
6852             self.is_temp = 1
6853
6854     def analyse_cpp_comparison(self, env):
6855         type1 = self.operand1.type
6856         type2 = self.operand2.type
6857         entry = env.lookup_operator(self.operator, [self.operand1, self.operand2])
6858         if entry is None:
6859             error(self.pos, "Invalid types for '%s' (%s, %s)" %
6860                 (self.operator, type1, type2))
6861             self.type = PyrexTypes.error_type
6862             self.result_code = "<error>"
6863             return
6864         func_type = entry.type
6865         if func_type.is_ptr:
6866             func_type = func_type.base_type
6867         if len(func_type.args) == 1:
6868             self.operand2 = self.operand2.coerce_to(func_type.args[0].type, env)
6869         else:
6870             self.operand1 = self.operand1.coerce_to(func_type.args[0].type, env)
6871             self.operand2 = self.operand2.coerce_to(func_type.args[1].type, env)
6872         self.type = func_type.return_type
6873
6874     def has_python_operands(self):
6875         return (self.operand1.type.is_pyobject
6876             or self.operand2.type.is_pyobject)
6877
6878     def check_const(self):
6879         if self.cascade:
6880             self.not_const()
6881             return False
6882         else:
6883             return self.operand1.check_const() and self.operand2.check_const()
6884
6885     def calculate_result_code(self):
6886         if self.operand1.type.is_complex:
6887             if self.operator == "!=":
6888                 negation = "!"
6889             else:
6890                 negation = ""
6891             return "(%s%s(%s, %s))" % (
6892                 negation,
6893                 self.operand1.type.binary_op('=='),
6894                 self.operand1.result(),
6895                 self.operand2.result())
6896         elif self.is_c_string_contains():
6897             if self.operand2.type is unicode_type:
6898                 if self.uchar_test_type is PyrexTypes.c_py_unicode_type:
6899                     method = "__Pyx_UnicodeContains"
6900                 else:
6901                     method = "__Pyx_UnicodeContainsUCS4"
6902             else:
6903                 method = "__Pyx_BytesContains"
6904             if self.operator == "not_in":
6905                 negation = "!"
6906             else:
6907                 negation = ""
6908             return "(%s%s(%s, %s))" % (
6909                 negation,
6910                 method,
6911                 self.operand2.result(),
6912                 self.operand1.result())
6913         else:
6914             return "(%s %s %s)" % (
6915                 self.operand1.result(),
6916                 self.c_operator(self.operator),
6917                 self.operand2.result())
6918
6919     def generate_evaluation_code(self, code):
6920         self.operand1.generate_evaluation_code(code)
6921         self.operand2.generate_evaluation_code(code)
6922         if self.is_temp:
6923             self.allocate_temp_result(code)
6924             self.generate_operation_code(code, self.result(),
6925                 self.operand1, self.operator, self.operand2)
6926             if self.cascade:
6927                 self.cascade.generate_evaluation_code(code,
6928                     self.result(), self.operand2)
6929             self.operand1.generate_disposal_code(code)
6930             self.operand1.free_temps(code)
6931             self.operand2.generate_disposal_code(code)
6932             self.operand2.free_temps(code)
6933
6934     def generate_subexpr_disposal_code(self, code):
6935         #  If this is called, it is a non-cascaded cmp,
6936         #  so only need to dispose of the two main operands.
6937         self.operand1.generate_disposal_code(code)
6938         self.operand2.generate_disposal_code(code)
6939
6940     def free_subexpr_temps(self, code):
6941         #  If this is called, it is a non-cascaded cmp,
6942         #  so only need to dispose of the two main operands.
6943         self.operand1.free_temps(code)
6944         self.operand2.free_temps(code)
6945
6946     def annotate(self, code):
6947         self.operand1.annotate(code)
6948         self.operand2.annotate(code)
6949         if self.cascade:
6950             self.cascade.annotate(code)
6951
6952
6953 class CascadedCmpNode(Node, CmpNode):
6954     #  A CascadedCmpNode is not a complete expression node. It
6955     #  hangs off the side of another comparison node, shares
6956     #  its left operand with that node, and shares its result
6957     #  with the PrimaryCmpNode at the head of the chain.
6958     #
6959     #  operator      string
6960     #  operand2      ExprNode
6961     #  cascade       CascadedCmpNode
6962
6963     child_attrs = ['operand2', 'cascade']
6964
6965     cascade = None
6966     constant_result = constant_value_not_set # FIXME: where to calculate this?
6967
6968     def infer_type(self, env):
6969         # TODO: Actually implement this (after merging with -unstable).
6970         return py_object_type
6971
6972     def type_dependencies(self, env):
6973         return ()
6974
6975     def has_constant_result(self):
6976         return self.constant_result is not constant_value_not_set and \
6977                self.constant_result is not not_a_constant
6978
6979     def analyse_types(self, env):
6980         self.operand2.analyse_types(env)
6981         if self.cascade:
6982             self.cascade.analyse_types(env)
6983
6984     def has_python_operands(self):
6985         return self.operand2.type.is_pyobject
6986
6987     def coerce_operands_to_pyobjects(self, env):
6988         self.operand2 = self.operand2.coerce_to_pyobject(env)
6989         if self.operand2.type is dict_type and self.operator in ('in', 'not_in'):
6990             self.operand2 = self.operand2.as_none_safe_node("'NoneType' object is not iterable")
6991         if self.cascade:
6992             self.cascade.coerce_operands_to_pyobjects(env)
6993
6994     def coerce_cascaded_operands_to_temp(self, env):
6995         if self.cascade:
6996             #self.operand2 = self.operand2.coerce_to_temp(env) #CTT
6997             self.operand2 = self.operand2.coerce_to_simple(env)
6998             self.cascade.coerce_cascaded_operands_to_temp(env)
6999
7000     def generate_evaluation_code(self, code, result, operand1):
7001         if self.type.is_pyobject:
7002             code.putln("if (__Pyx_PyObject_IsTrue(%s)) {" % result)
7003             code.put_decref(result, self.type)
7004         else:
7005             code.putln("if (%s) {" % result)
7006         self.operand2.generate_evaluation_code(code)
7007         self.generate_operation_code(code, result,
7008             operand1, self.operator, self.operand2)
7009         if self.cascade:
7010             self.cascade.generate_evaluation_code(
7011                 code, result, self.operand2)
7012         # Cascaded cmp result is always temp
7013         self.operand2.generate_disposal_code(code)
7014         self.operand2.free_temps(code)
7015         code.putln("}")
7016
7017     def annotate(self, code):
7018         self.operand2.annotate(code)
7019         if self.cascade:
7020             self.cascade.annotate(code)
7021
7022
7023 binop_node_classes = {
7024     "or":       BoolBinopNode,
7025     "and":      BoolBinopNode,
7026     "|":        IntBinopNode,
7027     "^":        IntBinopNode,
7028     "&":        IntBinopNode,
7029     "<<":       IntBinopNode,
7030     ">>":       IntBinopNode,
7031     "+":        AddNode,
7032     "-":        SubNode,
7033     "*":        MulNode,
7034     "/":        DivNode,
7035     "//":       DivNode,
7036     "%":        ModNode,
7037     "**":       PowNode
7038 }
7039
7040 def binop_node(pos, operator, operand1, operand2, inplace=False):
7041     # Construct binop node of appropriate class for
7042     # given operator.
7043     return binop_node_classes[operator](pos,
7044         operator = operator,
7045         operand1 = operand1,
7046         operand2 = operand2,
7047         inplace = inplace)
7048
7049 #-------------------------------------------------------------------
7050 #
7051 #  Coercion nodes
7052 #
7053 #  Coercion nodes are special in that they are created during
7054 #  the analyse_types phase of parse tree processing.
7055 #  Their __init__ methods consequently incorporate some aspects
7056 #  of that phase.
7057 #
7058 #-------------------------------------------------------------------
7059
7060 class CoercionNode(ExprNode):
7061     #  Abstract base class for coercion nodes.
7062     #
7063     #  arg       ExprNode       node being coerced
7064
7065     subexprs = ['arg']
7066     constant_result = not_a_constant
7067
7068     def __init__(self, arg):
7069         self.pos = arg.pos
7070         self.arg = arg
7071         if debug_coercion:
7072             print("%s Coercing %s" % (self, self.arg))
7073
7074     def calculate_constant_result(self):
7075         # constant folding can break type coercion, so this is disabled
7076         pass
7077
7078     def annotate(self, code):
7079         self.arg.annotate(code)
7080         if self.arg.type != self.type:
7081             file, line, col = self.pos
7082             code.annotate((file, line, col-1), AnnotationItem(style='coerce', tag='coerce', text='[%s] to [%s]' % (self.arg.type, self.type)))
7083
7084
7085 class CastNode(CoercionNode):
7086     #  Wrap a node in a C type cast.
7087
7088     def __init__(self, arg, new_type):
7089         CoercionNode.__init__(self, arg)
7090         self.type = new_type
7091
7092     def may_be_none(self):
7093         return self.arg.may_be_none()
7094
7095     def calculate_result_code(self):
7096         return self.arg.result_as(self.type)
7097
7098     def generate_result_code(self, code):
7099         self.arg.generate_result_code(code)
7100
7101
7102 class PyTypeTestNode(CoercionNode):
7103     #  This node is used to check that a generic Python
7104     #  object is an instance of a particular extension type.
7105     #  This node borrows the result of its argument node.
7106
7107     def __init__(self, arg, dst_type, env, notnone=False):
7108         #  The arg is know to be a Python object, and
7109         #  the dst_type is known to be an extension type.
7110         assert dst_type.is_extension_type or dst_type.is_builtin_type, "PyTypeTest on non extension type"
7111         CoercionNode.__init__(self, arg)
7112         self.type = dst_type
7113         self.result_ctype = arg.ctype()
7114         self.notnone = notnone
7115
7116     nogil_check = Node.gil_error
7117     gil_message = "Python type test"
7118
7119     def analyse_types(self, env):
7120         pass
7121
7122     def may_be_none(self):
7123         if self.notnone:
7124             return False
7125         return self.arg.may_be_none()
7126
7127     def is_simple(self):
7128         return self.arg.is_simple()
7129
7130     def result_in_temp(self):
7131         return self.arg.result_in_temp()
7132
7133     def is_ephemeral(self):
7134         return self.arg.is_ephemeral()
7135
7136     def calculate_constant_result(self):
7137         # FIXME
7138         pass
7139
7140     def calculate_result_code(self):
7141         return self.arg.result()
7142
7143     def generate_result_code(self, code):
7144         if self.type.typeobj_is_available():
7145             if not self.type.is_builtin_type:
7146                 code.globalstate.use_utility_code(type_test_utility_code)
7147             code.putln(
7148                 "if (!(%s)) %s" % (
7149                     self.type.type_test_code(self.arg.py_result(), self.notnone),
7150                     code.error_goto(self.pos)))
7151         else:
7152             error(self.pos, "Cannot test type of extern C class "
7153                 "without type object name specification")
7154
7155     def generate_post_assignment_code(self, code):
7156         self.arg.generate_post_assignment_code(code)
7157
7158     def free_temps(self, code):
7159         self.arg.free_temps(code)
7160
7161
7162 class NoneCheckNode(CoercionNode):
7163     # This node is used to check that a Python object is not None and
7164     # raises an appropriate exception (as specified by the creating
7165     # transform).
7166
7167     def __init__(self, arg, exception_type_cname, exception_message):
7168         CoercionNode.__init__(self, arg)
7169         self.type = arg.type
7170         self.result_ctype = arg.ctype()
7171         self.exception_type_cname = exception_type_cname
7172         self.exception_message = exception_message
7173
7174     def analyse_types(self, env):
7175         pass
7176
7177     def may_be_none(self):
7178         return False
7179
7180     def is_simple(self):
7181         return self.arg.is_simple()
7182
7183     def result_in_temp(self):
7184         return self.arg.result_in_temp()
7185
7186     def calculate_result_code(self):
7187         return self.arg.result()
7188
7189     def generate_result_code(self, code):
7190         code.putln(
7191             "if (unlikely(%s == Py_None)) {" % self.arg.result())
7192         code.putln('PyErr_SetString(%s, "%s"); %s ' % (
7193             self.exception_type_cname,
7194             StringEncoding.escape_byte_string(
7195                 self.exception_message.encode('UTF-8')),
7196             code.error_goto(self.pos)))
7197         code.putln("}")
7198
7199     def generate_post_assignment_code(self, code):
7200         self.arg.generate_post_assignment_code(code)
7201
7202     def free_temps(self, code):
7203         self.arg.free_temps(code)
7204
7205
7206 class CoerceToPyTypeNode(CoercionNode):
7207     #  This node is used to convert a C data type
7208     #  to a Python object.
7209
7210     type = py_object_type
7211     is_temp = 1
7212
7213     def __init__(self, arg, env, type=py_object_type):
7214         CoercionNode.__init__(self, arg)
7215         if not arg.type.create_to_py_utility_code(env):
7216             error(arg.pos,
7217                   "Cannot convert '%s' to Python object" % arg.type)
7218         if type is py_object_type:
7219             # be specific about some known types
7220             if arg.type.is_string:
7221                 self.type = bytes_type
7222             elif arg.type.is_unicode_char:
7223                 self.type = unicode_type
7224             elif arg.type.is_complex:
7225                 self.type = Builtin.complex_type
7226         else:
7227             # FIXME: check that the target type and the resulting type are compatible
7228             pass
7229
7230     gil_message = "Converting to Python object"
7231
7232     def may_be_none(self):
7233         # FIXME: is this always safe?
7234         return False
7235
7236     def coerce_to_boolean(self, env):
7237         arg_type = self.arg.type
7238         if (arg_type == PyrexTypes.c_bint_type or
7239             (arg_type.is_pyobject and arg_type.name == 'bool')):
7240             return self.arg.coerce_to_temp(env)
7241         else:
7242             return CoerceToBooleanNode(self, env)
7243
7244     def coerce_to_integer(self, env):
7245         # If not already some C integer type, coerce to longint.
7246         if self.arg.type.is_int:
7247             return self.arg
7248         else:
7249             return self.arg.coerce_to(PyrexTypes.c_long_type, env)
7250
7251     def analyse_types(self, env):
7252         # The arg is always already analysed
7253         pass
7254
7255     def generate_result_code(self, code):
7256         function = self.arg.type.to_py_function
7257         code.putln('%s = %s(%s); %s' % (
7258             self.result(),
7259             function,
7260             self.arg.result(),
7261             code.error_goto_if_null(self.result(), self.pos)))
7262         code.put_gotref(self.py_result())
7263
7264
7265 class CoerceIntToBytesNode(CoerceToPyTypeNode):
7266     #  This node is used to convert a C int type to a Python bytes
7267     #  object.
7268
7269     is_temp = 1
7270
7271     def __init__(self, arg, env):
7272         arg = arg.coerce_to_simple(env)
7273         CoercionNode.__init__(self, arg)
7274         self.type = Builtin.bytes_type
7275
7276     def generate_result_code(self, code):
7277         arg = self.arg
7278         arg_result = arg.result()
7279         if arg.type not in (PyrexTypes.c_char_type,
7280                             PyrexTypes.c_uchar_type,
7281                             PyrexTypes.c_schar_type):
7282             if arg.type.signed:
7283                 code.putln("if ((%s < 0) || (%s > 255)) {" % (
7284                     arg_result, arg_result))
7285             else:
7286                 code.putln("if (%s > 255) {" % arg_result)
7287             code.putln('PyErr_Format(PyExc_OverflowError, '
7288                        '"value too large to pack into a byte"); %s' % (
7289                            code.error_goto(self.pos)))
7290             code.putln('}')
7291         temp = None
7292         if arg.type is not PyrexTypes.c_char_type:
7293             temp = code.funcstate.allocate_temp(PyrexTypes.c_char_type, manage_ref=False)
7294             code.putln("%s = (char)%s;" % (temp, arg_result))
7295             arg_result = temp
7296         code.putln('%s = PyBytes_FromStringAndSize(&%s, 1); %s' % (
7297             self.result(),
7298             arg_result,
7299             code.error_goto_if_null(self.result(), self.pos)))
7300         if temp is not None:
7301             code.funcstate.release_temp(temp)
7302         code.put_gotref(self.py_result())
7303
7304
7305 class CoerceFromPyTypeNode(CoercionNode):
7306     #  This node is used to convert a Python object
7307     #  to a C data type.
7308
7309     def __init__(self, result_type, arg, env):
7310         CoercionNode.__init__(self, arg)
7311         self.type = result_type
7312         self.is_temp = 1
7313         if not result_type.create_from_py_utility_code(env):
7314             error(arg.pos,
7315                   "Cannot convert Python object to '%s'" % result_type)
7316         if self.type.is_string and self.arg.is_ephemeral():
7317             error(arg.pos,
7318                   "Obtaining char * from temporary Python value")
7319
7320     def analyse_types(self, env):
7321         # The arg is always already analysed
7322         pass
7323
7324     def generate_result_code(self, code):
7325         function = self.type.from_py_function
7326         operand = self.arg.py_result()
7327         rhs = "%s(%s)" % (function, operand)
7328         if self.type.is_enum:
7329             rhs = typecast(self.type, c_long_type, rhs)
7330         code.putln('%s = %s; %s' % (
7331             self.result(),
7332             rhs,
7333             code.error_goto_if(self.type.error_condition(self.result()), self.pos)))
7334         if self.type.is_pyobject:
7335             code.put_gotref(self.py_result())
7336
7337
7338 class CoerceToBooleanNode(CoercionNode):
7339     #  This node is used when a result needs to be used
7340     #  in a boolean context.
7341
7342     type = PyrexTypes.c_bint_type
7343
7344     _special_builtins = {
7345         Builtin.list_type    : 'PyList_GET_SIZE',
7346         Builtin.tuple_type   : 'PyTuple_GET_SIZE',
7347         Builtin.bytes_type   : 'PyBytes_GET_SIZE',
7348         Builtin.unicode_type : 'PyUnicode_GET_SIZE',
7349         }
7350
7351     def __init__(self, arg, env):
7352         CoercionNode.__init__(self, arg)
7353         if arg.type.is_pyobject:
7354             self.is_temp = 1
7355
7356     def nogil_check(self, env):
7357         if self.arg.type.is_pyobject and self._special_builtins.get(self.arg.type) is None:
7358             self.gil_error()
7359
7360     gil_message = "Truth-testing Python object"
7361
7362     def check_const(self):
7363         if self.is_temp:
7364             self.not_const()
7365             return False
7366         return self.arg.check_const()
7367
7368     def calculate_result_code(self):
7369         return "(%s != 0)" % self.arg.result()
7370
7371     def generate_result_code(self, code):
7372         if not self.is_temp:
7373             return
7374         test_func = self._special_builtins.get(self.arg.type)
7375         if test_func is not None:
7376             code.putln("%s = (%s != Py_None) && (%s(%s) != 0);" % (
7377                        self.result(),
7378                        self.arg.py_result(),
7379                        test_func,
7380                        self.arg.py_result()))
7381         else:
7382             code.putln(
7383                 "%s = __Pyx_PyObject_IsTrue(%s); %s" % (
7384                     self.result(),
7385                     self.arg.py_result(),
7386                     code.error_goto_if_neg(self.result(), self.pos)))
7387
7388 class CoerceToComplexNode(CoercionNode):
7389
7390     def __init__(self, arg, dst_type, env):
7391         if arg.type.is_complex:
7392             arg = arg.coerce_to_simple(env)
7393         self.type = dst_type
7394         CoercionNode.__init__(self, arg)
7395         dst_type.create_declaration_utility_code(env)
7396
7397     def calculate_result_code(self):
7398         if self.arg.type.is_complex:
7399             real_part = "__Pyx_CREAL(%s)" % self.arg.result()
7400             imag_part = "__Pyx_CIMAG(%s)" % self.arg.result()
7401         else:
7402             real_part = self.arg.result()
7403             imag_part = "0"
7404         return "%s(%s, %s)" % (
7405                 self.type.from_parts,
7406                 real_part,
7407                 imag_part)
7408
7409     def generate_result_code(self, code):
7410         pass
7411
7412 class CoerceToTempNode(CoercionNode):
7413     #  This node is used to force the result of another node
7414     #  to be stored in a temporary. It is only used if the
7415     #  argument node's result is not already in a temporary.
7416
7417     def __init__(self, arg, env):
7418         CoercionNode.__init__(self, arg)
7419         self.type = self.arg.type
7420         self.constant_result = self.arg.constant_result
7421         self.is_temp = 1
7422         if self.type.is_pyobject:
7423             self.result_ctype = py_object_type
7424
7425     gil_message = "Creating temporary Python reference"
7426
7427     def analyse_types(self, env):
7428         # The arg is always already analysed
7429         pass
7430
7431     def coerce_to_boolean(self, env):
7432         self.arg = self.arg.coerce_to_boolean(env)
7433         if self.arg.is_simple():
7434             return self.arg
7435         self.type = self.arg.type
7436         self.result_ctype = self.type
7437         return self
7438
7439     def generate_result_code(self, code):
7440         #self.arg.generate_evaluation_code(code) # Already done
7441         # by generic generate_subexpr_evaluation_code!
7442         code.putln("%s = %s;" % (
7443             self.result(), self.arg.result_as(self.ctype())))
7444         if self.type.is_pyobject and self.use_managed_ref:
7445             code.put_incref(self.result(), self.ctype())
7446
7447
7448 class CloneNode(CoercionNode):
7449     #  This node is employed when the result of another node needs
7450     #  to be used multiple times. The argument node's result must
7451     #  be in a temporary. This node "borrows" the result from the
7452     #  argument node, and does not generate any evaluation or
7453     #  disposal code for it. The original owner of the argument
7454     #  node is responsible for doing those things.
7455
7456     subexprs = [] # Arg is not considered a subexpr
7457     nogil_check = None
7458
7459     def __init__(self, arg):
7460         CoercionNode.__init__(self, arg)
7461         if hasattr(arg, 'type'):
7462             self.type = arg.type
7463             self.result_ctype = arg.result_ctype
7464         if hasattr(arg, 'entry'):
7465             self.entry = arg.entry
7466
7467     def result(self):
7468         return self.arg.result()
7469
7470     def type_dependencies(self, env):
7471         return self.arg.type_dependencies(env)
7472
7473     def infer_type(self, env):
7474         return self.arg.infer_type(env)
7475
7476     def analyse_types(self, env):
7477         self.type = self.arg.type
7478         self.result_ctype = self.arg.result_ctype
7479         self.is_temp = 1
7480         if hasattr(self.arg, 'entry'):
7481             self.entry = self.arg.entry
7482
7483     def is_simple(self):
7484         return True # result is always in a temp (or a name)
7485
7486     def generate_evaluation_code(self, code):
7487         pass
7488
7489     def generate_result_code(self, code):
7490         pass
7491
7492     def generate_disposal_code(self, code):
7493         pass
7494
7495     def free_temps(self, code):
7496         pass
7497
7498
7499 class ModuleRefNode(ExprNode):
7500     # Simple returns the module object
7501
7502     type = py_object_type
7503     is_temp = False
7504     subexprs = []
7505
7506     def analyse_types(self, env):
7507         pass
7508
7509     def may_be_none(self):
7510         return False
7511
7512     def calculate_result_code(self):
7513         return Naming.module_cname
7514
7515     def generate_result_code(self, code):
7516         pass
7517
7518 class DocstringRefNode(ExprNode):
7519     # Extracts the docstring of the body element
7520
7521     subexprs = ['body']
7522     type = py_object_type
7523     is_temp = True
7524
7525     def __init__(self, pos, body):
7526         ExprNode.__init__(self, pos)
7527         assert body.type.is_pyobject
7528         self.body = body
7529
7530     def analyse_types(self, env):
7531         pass
7532
7533     def generate_result_code(self, code):
7534         code.putln('%s = __Pyx_GetAttrString(%s, "__doc__"); %s' % (
7535             self.result(), self.body.result(),
7536             code.error_goto_if_null(self.result(), self.pos)))
7537         code.put_gotref(self.result())
7538
7539
7540
7541 #------------------------------------------------------------------------------------
7542 #
7543 #  Runtime support code
7544 #
7545 #------------------------------------------------------------------------------------
7546
7547 get_name_interned_utility_code = UtilityCode(
7548 proto = """
7549 static PyObject *__Pyx_GetName(PyObject *dict, PyObject *name); /*proto*/
7550 """,
7551 impl = """
7552 static PyObject *__Pyx_GetName(PyObject *dict, PyObject *name) {
7553     PyObject *result;
7554     result = PyObject_GetAttr(dict, name);
7555     if (!result)
7556         PyErr_SetObject(PyExc_NameError, name);
7557     return result;
7558 }
7559 """)
7560
7561 #------------------------------------------------------------------------------------
7562
7563 import_utility_code = UtilityCode(
7564 proto = """
7565 static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list); /*proto*/
7566 """,
7567 impl = """
7568 static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list) {
7569     PyObject *py_import = 0;
7570     PyObject *empty_list = 0;
7571     PyObject *module = 0;
7572     PyObject *global_dict = 0;
7573     PyObject *empty_dict = 0;
7574     PyObject *list;
7575     py_import = __Pyx_GetAttrString(%(BUILTINS)s, "__import__");
7576     if (!py_import)
7577         goto bad;
7578     if (from_list)
7579         list = from_list;
7580     else {
7581         empty_list = PyList_New(0);
7582         if (!empty_list)
7583             goto bad;
7584         list = empty_list;
7585     }
7586     global_dict = PyModule_GetDict(%(GLOBALS)s);
7587     if (!global_dict)
7588         goto bad;
7589     empty_dict = PyDict_New();
7590     if (!empty_dict)
7591         goto bad;
7592     module = PyObject_CallFunctionObjArgs(py_import,
7593         name, global_dict, empty_dict, list, NULL);
7594 bad:
7595     Py_XDECREF(empty_list);
7596     Py_XDECREF(py_import);
7597     Py_XDECREF(empty_dict);
7598     return module;
7599 }
7600 """ % {
7601     "BUILTINS": Naming.builtins_cname,
7602     "GLOBALS":  Naming.module_cname,
7603 })
7604
7605 #------------------------------------------------------------------------------------
7606
7607 get_exception_utility_code = UtilityCode(
7608 proto = """
7609 static PyObject *__Pyx_GetExcValue(void); /*proto*/
7610 """,
7611 impl = """
7612 static PyObject *__Pyx_GetExcValue(void) {
7613     PyObject *type = 0, *value = 0, *tb = 0;
7614     PyObject *tmp_type, *tmp_value, *tmp_tb;
7615     PyObject *result = 0;
7616     PyThreadState *tstate = PyThreadState_Get();
7617     PyErr_Fetch(&type, &value, &tb);
7618     PyErr_NormalizeException(&type, &value, &tb);
7619     if (PyErr_Occurred())
7620         goto bad;
7621     if (!value) {
7622         value = Py_None;
7623         Py_INCREF(value);
7624     }
7625     tmp_type = tstate->exc_type;
7626     tmp_value = tstate->exc_value;
7627     tmp_tb = tstate->exc_traceback;
7628     tstate->exc_type = type;
7629     tstate->exc_value = value;
7630     tstate->exc_traceback = tb;
7631     /* Make sure tstate is in a consistent state when we XDECREF
7632     these objects (XDECREF may run arbitrary code). */
7633     Py_XDECREF(tmp_type);
7634     Py_XDECREF(tmp_value);
7635     Py_XDECREF(tmp_tb);
7636     result = value;
7637     Py_XINCREF(result);
7638     type = 0;
7639     value = 0;
7640     tb = 0;
7641 bad:
7642     Py_XDECREF(type);
7643     Py_XDECREF(value);
7644     Py_XDECREF(tb);
7645     return result;
7646 }
7647 """)
7648
7649 #------------------------------------------------------------------------------------
7650
7651 type_test_utility_code = UtilityCode(
7652 proto = """
7653 static CYTHON_INLINE int __Pyx_TypeTest(PyObject *obj, PyTypeObject *type); /*proto*/
7654 """,
7655 impl = """
7656 static CYTHON_INLINE int __Pyx_TypeTest(PyObject *obj, PyTypeObject *type) {
7657     if (unlikely(!type)) {
7658         PyErr_Format(PyExc_SystemError, "Missing type object");
7659         return 0;
7660     }
7661     if (likely(PyObject_TypeCheck(obj, type)))
7662         return 1;
7663     PyErr_Format(PyExc_TypeError, "Cannot convert %.200s to %.200s",
7664                  Py_TYPE(obj)->tp_name, type->tp_name);
7665     return 0;
7666 }
7667 """)
7668
7669 #------------------------------------------------------------------------------------
7670
7671 find_py2_metaclass_utility_code = UtilityCode(
7672 proto = '''
7673 static PyObject *__Pyx_FindPy2Metaclass(PyObject *bases); /*proto*/
7674 ''',
7675 impl = '''
7676 static PyObject *__Pyx_FindPy2Metaclass(PyObject *bases) {
7677     PyObject *metaclass;
7678     /* Default metaclass */
7679 #if PY_MAJOR_VERSION < 3
7680     if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
7681         PyObject *base = PyTuple_GET_ITEM(bases, 0);
7682         metaclass = PyObject_GetAttrString(base, "__class__");
7683         if (!metaclass) {
7684             PyErr_Clear();
7685             metaclass = (PyObject*) Py_TYPE(base);
7686         }
7687     } else {
7688         metaclass = (PyObject *) &PyClass_Type;
7689     }
7690 #else
7691     if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
7692         PyObject *base = PyTuple_GET_ITEM(bases, 0);
7693         metaclass = (PyObject*) Py_TYPE(base);
7694     } else {
7695         metaclass = (PyObject *) &PyType_Type;
7696     }
7697 #endif
7698     Py_INCREF(metaclass);
7699     return metaclass;
7700 }
7701 ''')
7702
7703 create_class_utility_code = UtilityCode(
7704 proto = """
7705 static PyObject *__Pyx_CreateClass(PyObject *bases, PyObject *dict, PyObject *name,
7706                                    PyObject *modname); /*proto*/
7707 """,
7708 impl = """
7709 static PyObject *__Pyx_CreateClass(PyObject *bases, PyObject *dict, PyObject *name,
7710                                    PyObject *modname) {
7711     PyObject *result;
7712     PyObject *metaclass;
7713
7714     if (PyDict_SetItemString(dict, "__module__", modname) < 0)
7715         return NULL;
7716
7717     /* Python2 __metaclass__ */
7718     metaclass = PyDict_GetItemString(dict, "__metaclass__");
7719     if (metaclass) {
7720         Py_INCREF(metaclass);
7721     } else {
7722         metaclass = __Pyx_FindPy2Metaclass(bases);
7723     }
7724     result = PyObject_CallFunctionObjArgs(metaclass, name, bases, dict, NULL);
7725     Py_DECREF(metaclass);
7726     return result;
7727 }
7728 """,
7729 requires = [find_py2_metaclass_utility_code])
7730
7731 #------------------------------------------------------------------------------------
7732
7733 create_py3class_utility_code = UtilityCode(
7734 proto = """
7735 static PyObject *__Pyx_Py3MetaclassGet(PyObject *bases, PyObject *mkw); /*proto*/
7736 static PyObject *__Pyx_Py3MetaclassPrepare(PyObject *metaclass, PyObject *bases, PyObject *name, PyObject *mkw, PyObject *modname, PyObject *doc); /*proto*/
7737 static PyObject *__Pyx_Py3ClassCreate(PyObject *metaclass, PyObject *name, PyObject *bases, PyObject *dict, PyObject *mkw); /*proto*/
7738 """,
7739 impl = """
7740 PyObject *__Pyx_Py3MetaclassGet(PyObject *bases, PyObject *mkw) {
7741     PyObject *metaclass = PyDict_GetItemString(mkw, "metaclass");
7742     if (metaclass) {
7743         Py_INCREF(metaclass);
7744         if (PyDict_DelItemString(mkw, "metaclass") < 0) {
7745             Py_DECREF(metaclass);
7746             return NULL;
7747         }
7748         return metaclass;
7749     }
7750     return __Pyx_FindPy2Metaclass(bases);
7751 }
7752
7753 PyObject *__Pyx_Py3MetaclassPrepare(PyObject *metaclass, PyObject *bases, PyObject *name, PyObject *mkw,
7754                                     PyObject *modname, PyObject *doc) {
7755     PyObject *prep;
7756     PyObject *pargs;
7757     PyObject *ns;
7758     PyObject *str;
7759
7760     prep = PyObject_GetAttrString(metaclass, "__prepare__");
7761     if (!prep) {
7762         if (!PyErr_ExceptionMatches(PyExc_AttributeError))
7763             return NULL;
7764         PyErr_Clear();
7765         return PyDict_New();
7766     }
7767     pargs = PyTuple_New(2);
7768     if (!pargs) {
7769         Py_DECREF(prep);
7770         return NULL;
7771     }
7772
7773     Py_INCREF(name);
7774     Py_INCREF(bases);
7775     PyTuple_SET_ITEM(pargs, 0, name);
7776     PyTuple_SET_ITEM(pargs, 1, bases);
7777
7778     ns = PyObject_Call(prep, pargs, mkw);
7779
7780     Py_DECREF(prep);
7781     Py_DECREF(pargs);
7782
7783     if (ns == NULL)
7784         return NULL;
7785
7786     /* Required here to emulate assignment order */
7787     /* XXX: use consts here */
7788     #if PY_MAJOR_VERSION >= 3
7789     str = PyUnicode_FromString("__module__");
7790     #else
7791     str = PyString_FromString("__module__");
7792     #endif
7793     if (!str) {
7794         Py_DECREF(ns);
7795         return NULL;
7796     }
7797
7798     if (PyObject_SetItem(ns, str, modname) < 0) {
7799         Py_DECREF(ns);
7800         Py_DECREF(str);
7801         return NULL;
7802     }
7803     Py_DECREF(str);
7804     if (doc) {
7805         #if PY_MAJOR_VERSION >= 3
7806         str = PyUnicode_FromString("__doc__");
7807         #else
7808         str = PyString_FromString("__doc__");
7809         #endif
7810         if (!str) {
7811             Py_DECREF(ns);
7812             return NULL;
7813         }
7814         if (PyObject_SetItem(ns, str, doc) < 0) {
7815             Py_DECREF(ns);
7816             Py_DECREF(str);
7817             return NULL;
7818         }
7819         Py_DECREF(str);
7820     }
7821     return ns;
7822 }
7823
7824 PyObject *__Pyx_Py3ClassCreate(PyObject *metaclass, PyObject *name, PyObject *bases, PyObject *dict, PyObject *mkw) {
7825     PyObject *result;
7826     PyObject *margs = PyTuple_New(3);
7827     if (!margs)
7828         return NULL;
7829     Py_INCREF(name);
7830     Py_INCREF(bases);
7831     Py_INCREF(dict);
7832     PyTuple_SET_ITEM(margs, 0, name);
7833     PyTuple_SET_ITEM(margs, 1, bases);
7834     PyTuple_SET_ITEM(margs, 2, dict);
7835     result = PyObject_Call(metaclass, margs, mkw);
7836     Py_DECREF(margs);
7837     return result;
7838 }
7839 """,
7840 requires = [find_py2_metaclass_utility_code])
7841
7842 #------------------------------------------------------------------------------------
7843
7844 cpp_exception_utility_code = UtilityCode(
7845 proto = """
7846 #ifndef __Pyx_CppExn2PyErr
7847 static void __Pyx_CppExn2PyErr() {
7848   try {
7849     if (PyErr_Occurred())
7850       ; // let the latest Python exn pass through and ignore the current one
7851     else
7852       throw;
7853   } catch (const std::invalid_argument& exn) {
7854     // Catch a handful of different errors here and turn them into the
7855     // equivalent Python errors.
7856     // Change invalid_argument to ValueError
7857     PyErr_SetString(PyExc_ValueError, exn.what());
7858   } catch (const std::out_of_range& exn) {
7859     // Change out_of_range to IndexError
7860     PyErr_SetString(PyExc_IndexError, exn.what());
7861   } catch (const std::exception& exn) {
7862     PyErr_SetString(PyExc_RuntimeError, exn.what());
7863   }
7864   catch (...)
7865   {
7866     PyErr_SetString(PyExc_RuntimeError, "Unknown exception");
7867   }
7868 }
7869 #endif
7870 """,
7871 impl = ""
7872 )
7873
7874 pyerr_occurred_withgil_utility_code= UtilityCode(
7875 proto = """
7876 static CYTHON_INLINE int __Pyx_ErrOccurredWithGIL(void); /* proto */
7877 """,
7878 impl = """
7879 static CYTHON_INLINE int __Pyx_ErrOccurredWithGIL(void) {
7880   int err;
7881   #ifdef WITH_THREAD
7882   PyGILState_STATE _save = PyGILState_Ensure();
7883   #endif
7884   err = !!PyErr_Occurred();
7885   #ifdef WITH_THREAD
7886   PyGILState_Release(_save);
7887   #endif
7888   return err;
7889 }
7890 """
7891 )
7892
7893 #------------------------------------------------------------------------------------
7894
7895 raise_noneattr_error_utility_code = UtilityCode(
7896 proto = """
7897 static CYTHON_INLINE void __Pyx_RaiseNoneAttributeError(const char* attrname);
7898 """,
7899 impl = '''
7900 static CYTHON_INLINE void __Pyx_RaiseNoneAttributeError(const char* attrname) {
7901     PyErr_Format(PyExc_AttributeError, "'NoneType' object has no attribute '%s'", attrname);
7902 }
7903 ''')
7904
7905 raise_noneindex_error_utility_code = UtilityCode(
7906 proto = """
7907 static CYTHON_INLINE void __Pyx_RaiseNoneIndexingError(void);
7908 """,
7909 impl = '''
7910 static CYTHON_INLINE void __Pyx_RaiseNoneIndexingError(void) {
7911     PyErr_SetString(PyExc_TypeError, "'NoneType' object is unsubscriptable");
7912 }
7913 ''')
7914
7915 raise_none_iter_error_utility_code = UtilityCode(
7916 proto = """
7917 static CYTHON_INLINE void __Pyx_RaiseNoneNotIterableError(void);
7918 """,
7919 impl = '''
7920 static CYTHON_INLINE void __Pyx_RaiseNoneNotIterableError(void) {
7921     PyErr_SetString(PyExc_TypeError, "'NoneType' object is not iterable");
7922 }
7923 ''')
7924
7925 #------------------------------------------------------------------------------------
7926
7927 getitem_dict_utility_code = UtilityCode(
7928 proto = """
7929 #if PY_MAJOR_VERSION >= 3
7930 static PyObject *__Pyx_PyDict_GetItem(PyObject *d, PyObject* key) {
7931     PyObject *value;
7932     if (unlikely(d == Py_None)) {
7933         __Pyx_RaiseNoneIndexingError();
7934         return NULL;
7935     }
7936     value = PyDict_GetItemWithError(d, key);
7937     if (unlikely(!value)) {
7938         if (!PyErr_Occurred())
7939             PyErr_SetObject(PyExc_KeyError, key);
7940         return NULL;
7941     }
7942     Py_INCREF(value);
7943     return value;
7944 }
7945 #else
7946     #define __Pyx_PyDict_GetItem(d, key) PyObject_GetItem(d, key)
7947 #endif
7948 """,
7949 requires = [raise_noneindex_error_utility_code])
7950
7951 #------------------------------------------------------------------------------------
7952
7953 getitem_int_pyunicode_utility_code = UtilityCode(
7954 proto = '''
7955 #define __Pyx_GetItemInt_Unicode(o, i, size, to_py_func) (((size) <= sizeof(Py_ssize_t)) ? \\
7956                                                __Pyx_GetItemInt_Unicode_Fast(o, i) : \\
7957                                                __Pyx_GetItemInt_Unicode_Generic(o, to_py_func(i)))
7958
7959 static CYTHON_INLINE Py_UNICODE __Pyx_GetItemInt_Unicode_Fast(PyObject* ustring, Py_ssize_t i) {
7960     if (likely((0 <= i) & (i < PyUnicode_GET_SIZE(ustring)))) {
7961         return PyUnicode_AS_UNICODE(ustring)[i];
7962     } else if ((-PyUnicode_GET_SIZE(ustring) <= i) & (i < 0)) {
7963         i += PyUnicode_GET_SIZE(ustring);
7964         return PyUnicode_AS_UNICODE(ustring)[i];
7965     } else {
7966         PyErr_SetString(PyExc_IndexError, "string index out of range");
7967         return (Py_UNICODE)-1;
7968     }
7969 }
7970
7971 static CYTHON_INLINE Py_UNICODE __Pyx_GetItemInt_Unicode_Generic(PyObject* ustring, PyObject* j) {
7972     Py_UNICODE uchar;
7973     PyObject *uchar_string;
7974     if (!j) return (Py_UNICODE)-1;
7975     uchar_string = PyObject_GetItem(ustring, j);
7976     Py_DECREF(j);
7977     if (!uchar_string) return (Py_UNICODE)-1;
7978     uchar = PyUnicode_AS_UNICODE(uchar_string)[0];
7979     Py_DECREF(uchar_string);
7980     return uchar;
7981 }
7982 ''')
7983
7984 getitem_int_utility_code = UtilityCode(
7985 proto = """
7986
7987 static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Generic(PyObject *o, PyObject* j) {
7988     PyObject *r;
7989     if (!j) return NULL;
7990     r = PyObject_GetItem(o, j);
7991     Py_DECREF(j);
7992     return r;
7993 }
7994
7995 """ + ''.join([
7996 """
7997 #define __Pyx_GetItemInt_%(type)s(o, i, size, to_py_func) (((size) <= sizeof(Py_ssize_t)) ? \\
7998                                                     __Pyx_GetItemInt_%(type)s_Fast(o, i) : \\
7999                                                     __Pyx_GetItemInt_Generic(o, to_py_func(i)))
8000
8001 static CYTHON_INLINE PyObject *__Pyx_GetItemInt_%(type)s_Fast(PyObject *o, Py_ssize_t i) {
8002     if (likely(o != Py_None)) {
8003         if (likely((0 <= i) & (i < Py%(type)s_GET_SIZE(o)))) {
8004             PyObject *r = Py%(type)s_GET_ITEM(o, i);
8005             Py_INCREF(r);
8006             return r;
8007         }
8008         else if ((-Py%(type)s_GET_SIZE(o) <= i) & (i < 0)) {
8009             PyObject *r = Py%(type)s_GET_ITEM(o, Py%(type)s_GET_SIZE(o) + i);
8010             Py_INCREF(r);
8011             return r;
8012         }
8013     }
8014     return __Pyx_GetItemInt_Generic(o, PyInt_FromSsize_t(i));
8015 }
8016 """ % {'type' : type_name} for type_name in ('List', 'Tuple')
8017 ]) + """
8018
8019 #define __Pyx_GetItemInt(o, i, size, to_py_func) (((size) <= sizeof(Py_ssize_t)) ? \\
8020                                                     __Pyx_GetItemInt_Fast(o, i) : \\
8021                                                     __Pyx_GetItemInt_Generic(o, to_py_func(i)))
8022
8023 static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Fast(PyObject *o, Py_ssize_t i) {
8024     PyObject *r;
8025     if (PyList_CheckExact(o) && ((0 <= i) & (i < PyList_GET_SIZE(o)))) {
8026         r = PyList_GET_ITEM(o, i);
8027         Py_INCREF(r);
8028     }
8029     else if (PyTuple_CheckExact(o) && ((0 <= i) & (i < PyTuple_GET_SIZE(o)))) {
8030         r = PyTuple_GET_ITEM(o, i);
8031         Py_INCREF(r);
8032     }
8033     else if (Py_TYPE(o)->tp_as_sequence && Py_TYPE(o)->tp_as_sequence->sq_item && (likely(i >= 0))) {
8034         r = PySequence_GetItem(o, i);
8035     }
8036     else {
8037         r = __Pyx_GetItemInt_Generic(o, PyInt_FromSsize_t(i));
8038     }
8039     return r;
8040 }
8041 """,
8042 impl = """
8043 """)
8044
8045
8046
8047 #------------------------------------------------------------------------------------
8048
8049 setitem_int_utility_code = UtilityCode(
8050 proto = """
8051 #define __Pyx_SetItemInt(o, i, v, size, to_py_func) (((size) <= sizeof(Py_ssize_t)) ? \\
8052                                                     __Pyx_SetItemInt_Fast(o, i, v) : \\
8053                                                     __Pyx_SetItemInt_Generic(o, to_py_func(i), v))
8054
8055 static CYTHON_INLINE int __Pyx_SetItemInt_Generic(PyObject *o, PyObject *j, PyObject *v) {
8056     int r;
8057     if (!j) return -1;
8058     r = PyObject_SetItem(o, j, v);
8059     Py_DECREF(j);
8060     return r;
8061 }
8062
8063 static CYTHON_INLINE int __Pyx_SetItemInt_Fast(PyObject *o, Py_ssize_t i, PyObject *v) {
8064     if (PyList_CheckExact(o) && ((0 <= i) & (i < PyList_GET_SIZE(o)))) {
8065         Py_INCREF(v);
8066         Py_DECREF(PyList_GET_ITEM(o, i));
8067         PyList_SET_ITEM(o, i, v);
8068         return 1;
8069     }
8070     else if (Py_TYPE(o)->tp_as_sequence && Py_TYPE(o)->tp_as_sequence->sq_ass_item && (likely(i >= 0)))
8071         return PySequence_SetItem(o, i, v);
8072     else {
8073         PyObject *j = PyInt_FromSsize_t(i);
8074         return __Pyx_SetItemInt_Generic(o, j, v);
8075     }
8076 }
8077 """,
8078 impl = """
8079 """)
8080
8081 #------------------------------------------------------------------------------------
8082
8083 delitem_int_utility_code = UtilityCode(
8084 proto = """
8085 #define __Pyx_DelItemInt(o, i, size, to_py_func) (((size) <= sizeof(Py_ssize_t)) ? \\
8086                                                     __Pyx_DelItemInt_Fast(o, i) : \\
8087                                                     __Pyx_DelItem_Generic(o, to_py_func(i)))
8088
8089 static CYTHON_INLINE int __Pyx_DelItem_Generic(PyObject *o, PyObject *j) {
8090     int r;
8091     if (!j) return -1;
8092     r = PyObject_DelItem(o, j);
8093     Py_DECREF(j);
8094     return r;
8095 }
8096
8097 static CYTHON_INLINE int __Pyx_DelItemInt_Fast(PyObject *o, Py_ssize_t i) {
8098     if (Py_TYPE(o)->tp_as_sequence && Py_TYPE(o)->tp_as_sequence->sq_ass_item && likely(i >= 0))
8099         return PySequence_DelItem(o, i);
8100     else {
8101         PyObject *j = PyInt_FromSsize_t(i);
8102         return __Pyx_DelItem_Generic(o, j);
8103     }
8104 }
8105 """,
8106 impl = """
8107 """)
8108
8109 #------------------------------------------------------------------------------------
8110
8111 raise_too_many_values_to_unpack = UtilityCode(
8112 proto = """
8113 static CYTHON_INLINE void __Pyx_RaiseTooManyValuesError(Py_ssize_t expected);
8114 """,
8115 impl = '''
8116 static CYTHON_INLINE void __Pyx_RaiseTooManyValuesError(Py_ssize_t expected) {
8117     PyErr_Format(PyExc_ValueError,
8118         #if PY_VERSION_HEX < 0x02050000
8119             "too many values to unpack (expected %d)", (int)expected);
8120         #else
8121             "too many values to unpack (expected %zd)", expected);
8122         #endif
8123 }
8124 ''')
8125
8126 raise_need_more_values_to_unpack = UtilityCode(
8127 proto = """
8128 static CYTHON_INLINE void __Pyx_RaiseNeedMoreValuesError(Py_ssize_t index);
8129 """,
8130 impl = '''
8131 static CYTHON_INLINE void __Pyx_RaiseNeedMoreValuesError(Py_ssize_t index) {
8132     PyErr_Format(PyExc_ValueError,
8133         #if PY_VERSION_HEX < 0x02050000
8134                  "need more than %d value%s to unpack", (int)index,
8135         #else
8136                  "need more than %zd value%s to unpack", index,
8137         #endif
8138                  (index == 1) ? "" : "s");
8139 }
8140 ''')
8141
8142 #------------------------------------------------------------------------------------
8143
8144 tuple_unpacking_error_code = UtilityCode(
8145 proto = """
8146 static void __Pyx_UnpackTupleError(PyObject *, Py_ssize_t index); /*proto*/
8147 """,
8148 impl = """
8149 static void __Pyx_UnpackTupleError(PyObject *t, Py_ssize_t index) {
8150     if (t == Py_None) {
8151       __Pyx_RaiseNoneNotIterableError();
8152     } else if (PyTuple_GET_SIZE(t) < index) {
8153       __Pyx_RaiseNeedMoreValuesError(PyTuple_GET_SIZE(t));
8154     } else {
8155       __Pyx_RaiseTooManyValuesError(index);
8156     }
8157 }
8158 """,
8159 requires = [raise_none_iter_error_utility_code,
8160             raise_need_more_values_to_unpack,
8161             raise_too_many_values_to_unpack]
8162 )
8163
8164 unpacking_utility_code = UtilityCode(
8165 proto = """
8166 static PyObject *__Pyx_UnpackItem(PyObject *, Py_ssize_t index); /*proto*/
8167 static int __Pyx_EndUnpack(PyObject *, Py_ssize_t expected); /*proto*/
8168 """,
8169 impl = """
8170 static PyObject *__Pyx_UnpackItem(PyObject *iter, Py_ssize_t index) {
8171     PyObject *item;
8172     if (!(item = PyIter_Next(iter))) {
8173         if (!PyErr_Occurred()) {
8174             __Pyx_RaiseNeedMoreValuesError(index);
8175         }
8176     }
8177     return item;
8178 }
8179
8180 static int __Pyx_EndUnpack(PyObject *iter, Py_ssize_t expected) {
8181     PyObject *item;
8182     if ((item = PyIter_Next(iter))) {
8183         Py_DECREF(item);
8184         __Pyx_RaiseTooManyValuesError(expected);
8185         return -1;
8186     }
8187     else if (!PyErr_Occurred())
8188         return 0;
8189     else
8190         return -1;
8191 }
8192 """,
8193 requires = [raise_need_more_values_to_unpack,
8194             raise_too_many_values_to_unpack]
8195 )
8196
8197 #------------------------------------------------------------------------------------
8198
8199 # CPython supports calling functions with non-dict kwargs by
8200 # converting them to a dict first
8201
8202 kwargs_call_utility_code = UtilityCode(
8203 proto = """
8204 static PyObject* __Pyx_PyEval_CallObjectWithKeywords(PyObject*, PyObject*, PyObject*); /*proto*/
8205 """,
8206 impl = """
8207 static PyObject* __Pyx_PyEval_CallObjectWithKeywords(PyObject *callable, PyObject *args, PyObject *kwargs) {
8208     PyObject* result;
8209     if (likely(PyDict_Check(kwargs))) {
8210         return PyEval_CallObjectWithKeywords(callable, args, kwargs);
8211     } else {
8212         PyObject* real_dict;
8213         real_dict = PyObject_CallFunctionObjArgs((PyObject*)&PyDict_Type, kwargs, NULL);
8214         if (unlikely(!real_dict))
8215             return NULL;
8216         result = PyEval_CallObjectWithKeywords(callable, args, real_dict);
8217         Py_DECREF(real_dict);
8218         return result; /* may be NULL */
8219     }
8220 }
8221 """,
8222 )
8223
8224
8225 #------------------------------------------------------------------------------------
8226
8227 int_pow_utility_code = UtilityCode(
8228 proto="""
8229 static CYTHON_INLINE %(type)s %(func_name)s(%(type)s, %(type)s); /* proto */
8230 """,
8231 impl="""
8232 static CYTHON_INLINE %(type)s %(func_name)s(%(type)s b, %(type)s e) {
8233     %(type)s t = b;
8234     switch (e) {
8235         case 3:
8236             t *= b;
8237         case 2:
8238             t *= b;
8239         case 1:
8240             return t;
8241         case 0:
8242             return 1;
8243     }
8244     if (unlikely(e<0)) return 0;
8245     t = 1;
8246     while (likely(e)) {
8247         t *= (b * (e&1)) | ((~e)&1);    /* 1 or b */
8248         b *= b;
8249         e >>= 1;
8250     }
8251     return t;
8252 }
8253 """)
8254
8255 # ------------------------------ Division ------------------------------------
8256
8257 div_int_utility_code = UtilityCode(
8258 proto="""
8259 static CYTHON_INLINE %(type)s __Pyx_div_%(type_name)s(%(type)s, %(type)s); /* proto */
8260 """,
8261 impl="""
8262 static CYTHON_INLINE %(type)s __Pyx_div_%(type_name)s(%(type)s a, %(type)s b) {
8263     %(type)s q = a / b;
8264     %(type)s r = a - q*b;
8265     q -= ((r != 0) & ((r ^ b) < 0));
8266     return q;
8267 }
8268 """)
8269
8270 mod_int_utility_code = UtilityCode(
8271 proto="""
8272 static CYTHON_INLINE %(type)s __Pyx_mod_%(type_name)s(%(type)s, %(type)s); /* proto */
8273 """,
8274 impl="""
8275 static CYTHON_INLINE %(type)s __Pyx_mod_%(type_name)s(%(type)s a, %(type)s b) {
8276     %(type)s r = a %% b;
8277     r += ((r != 0) & ((r ^ b) < 0)) * b;
8278     return r;
8279 }
8280 """)
8281
8282 mod_float_utility_code = UtilityCode(
8283 proto="""
8284 static CYTHON_INLINE %(type)s __Pyx_mod_%(type_name)s(%(type)s, %(type)s); /* proto */
8285 """,
8286 impl="""
8287 static CYTHON_INLINE %(type)s __Pyx_mod_%(type_name)s(%(type)s a, %(type)s b) {
8288     %(type)s r = fmod%(math_h_modifier)s(a, b);
8289     r += ((r != 0) & ((r < 0) ^ (b < 0))) * b;
8290     return r;
8291 }
8292 """)
8293
8294 cdivision_warning_utility_code = UtilityCode(
8295 proto="""
8296 static int __Pyx_cdivision_warning(void); /* proto */
8297 """,
8298 impl="""
8299 static int __Pyx_cdivision_warning(void) {
8300     return PyErr_WarnExplicit(PyExc_RuntimeWarning,
8301                               "division with oppositely signed operands, C and Python semantics differ",
8302                               %(FILENAME)s,
8303                               %(LINENO)s,
8304                               __Pyx_MODULE_NAME,
8305                               NULL);
8306 }
8307 """ % {
8308     'FILENAME': Naming.filename_cname,
8309     'LINENO':  Naming.lineno_cname,
8310 })
8311
8312 # from intobject.c
8313 division_overflow_test_code = UtilityCode(
8314 proto="""
8315 #define UNARY_NEG_WOULD_OVERFLOW(x)     \
8316         (((x) < 0) & ((unsigned long)(x) == 0-(unsigned long)(x)))
8317 """)
8318
8319
8320 binding_cfunc_utility_code = UtilityCode(
8321 proto="""
8322 #define %(binding_cfunc)s_USED 1
8323
8324 typedef struct {
8325     PyCFunctionObject func;
8326 } %(binding_cfunc)s_object;
8327
8328 static PyTypeObject %(binding_cfunc)s_type;
8329 static PyTypeObject *%(binding_cfunc)s = NULL;
8330
8331 static PyObject *%(binding_cfunc)s_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module); /* proto */
8332 #define %(binding_cfunc)s_New(ml, self) %(binding_cfunc)s_NewEx(ml, self, NULL)
8333
8334 static int %(binding_cfunc)s_init(void); /* proto */
8335 """ % Naming.__dict__,
8336 impl="""
8337
8338 static PyObject *%(binding_cfunc)s_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module) {
8339         %(binding_cfunc)s_object *op = PyObject_GC_New(%(binding_cfunc)s_object, %(binding_cfunc)s);
8340     if (op == NULL)
8341         return NULL;
8342         op->func.m_ml = ml;
8343         Py_XINCREF(self);
8344         op->func.m_self = self;
8345         Py_XINCREF(module);
8346         op->func.m_module = module;
8347         PyObject_GC_Track(op);
8348         return (PyObject *)op;
8349 }
8350
8351 static void %(binding_cfunc)s_dealloc(%(binding_cfunc)s_object *m) {
8352         PyObject_GC_UnTrack(m);
8353         Py_XDECREF(m->func.m_self);
8354         Py_XDECREF(m->func.m_module);
8355     PyObject_GC_Del(m);
8356 }
8357
8358 static PyObject *%(binding_cfunc)s_descr_get(PyObject *func, PyObject *obj, PyObject *type) {
8359         if (obj == Py_None)
8360                 obj = NULL;
8361         return PyMethod_New(func, obj, type);
8362 }
8363
8364 static int %(binding_cfunc)s_init(void) {
8365     %(binding_cfunc)s_type = PyCFunction_Type;
8366     %(binding_cfunc)s_type.tp_name = __Pyx_NAMESTR("cython_binding_builtin_function_or_method");
8367     %(binding_cfunc)s_type.tp_dealloc = (destructor)%(binding_cfunc)s_dealloc;
8368     %(binding_cfunc)s_type.tp_descr_get = %(binding_cfunc)s_descr_get;
8369     if (PyType_Ready(&%(binding_cfunc)s_type) < 0) {
8370         return -1;
8371     }
8372     %(binding_cfunc)s = &%(binding_cfunc)s_type;
8373     return 0;
8374
8375 }
8376 """ % Naming.__dict__)