2 # Pyrex - Parse tree nodes for expressions
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)
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
23 from Nodes import Node
25 from PyrexTypes import py_object_type, c_long_type, typecast, error_type, \
27 from Builtin import list_type, tuple_type, set_type, dict_type, \
28 unicode_type, str_type, bytes_type, type_type
32 from Cython import Utils
33 from Annotate import AnnotationItem
35 from Cython.Debugging import print_call_chain
36 from DebugFlags import debug_disposal_code, debug_temp_alloc, \
40 from __builtin__ import basestring
42 basestring = str # Python 3
44 class NotConstant(object):
46 return "<NOT CONSTANT>"
48 not_a_constant = NotConstant()
49 constant_value_not_set = object()
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",
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')
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.
84 old_temp = None # error checker for multiple frees etc.
85 use_managed_ref = True # can be set by optimisation transforms
87 # The Analyse Expressions phase for expressions is split
88 # into two sub-phases:
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
98 # Allocates temporary variables where needed, and fills
99 # in the result_code field of each node.
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.
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.
114 # The framework makes use of a number of abstract methods.
115 # Their responsibilities are as follows.
117 # Declaration Analysis phase
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
125 # Expression Analysis phase
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.
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.
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.
146 # - Check that this node and its subnodes form a
147 # legal constant expression. If so, do nothing,
148 # otherwise call not_const.
150 # The default implementation of check_const
151 # assumes that the expression is not constant.
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.
158 # The default implementation of calc_const_addr
159 # assumes that the expression is not a constant
162 # Code Generation phase
164 # generate_evaluation_code
165 # - Call generate_evaluation_code for sub-expressions.
166 # - Perform the functions of generate_result_code
168 # - If result is temporary, call generate_disposal_code
169 # on all sub-expressions.
171 # A default implementation of generate_evaluation_code
172 # is provided which uses the following abstract methods:
174 # generate_result_code
175 # - Generate any C statements necessary to calculate
176 # the result of this node from the results of its
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
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.
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.
200 is_sequence_constructor = 0
203 saved_subexpr_nodes = None
208 constant_result = constant_value_not_set
211 _get_child_attrs = operator.attrgetter('subexprs')
212 except AttributeError:
214 def __get_child_attrs(self):
216 _get_child_attrs = __get_child_attrs
217 child_attrs = property(fget=_get_child_attrs)
219 def not_implemented(self, method_name):
220 print_call_chain(method_name, "not implemented") ###
222 "%s.%s not implemented" %
223 (self.__class__.__name__, method_name))
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
236 def subexpr_nodes(self):
237 # Extract a list of subexpression nodes based
238 # on the contents of the subexprs class attribute.
240 for name in self.subexprs:
241 item = getattr(self, name)
243 if type(item) is list:
251 return self.temp_code
253 return self.calculate_result_code()
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())
260 # Return the result code cast to PyObject *.
261 return self.result_as(py_object_type)
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
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.
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.
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
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.
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
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")
299 def compile_time_value_error(self, e):
300 error(self.pos, "Error in compile-time expression: %s: %s" % (
301 e.__class__.__name__, e))
303 # ------------- Declaration Analysis ----------------
305 def analyse_target_declaration(self, env):
306 error(self.pos, "Cannot assign to or delete this")
308 # ------------- Expression Analysis ----------------
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()
318 def analyse_expressions(self, env):
319 # Convenience routine performing both the Type
320 # Analysis and Temp Allocation phases for a whole
322 self.analyse_types(env)
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
328 self.analyse_target_types(env)
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)
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)
347 # --------------- Type Inference -----------------
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:
354 return sum([node.type_dependencies(env) for node in self.subexpr_nodes()], ())
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:
363 elif hasattr(self, 'entry') and self.entry is not None:
364 return self.entry.type
366 self.not_implemented("infer_type")
368 # --------------- Type Analysis ------------------
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.
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.
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.
385 def analyse_types(self, env):
386 self.not_implemented("analyse_types")
388 def analyse_target_types(self, env):
389 self.analyse_types(env)
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:
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")
402 def check_const(self):
407 error(self.pos, "Not allowed in a constant expression")
409 def check_const_addr(self):
410 self.addr_not_const()
413 def addr_not_const(self):
414 error(self.pos, "Address is not constant")
416 # ----------------- Result Allocation -----------------
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
425 def target_code(self):
426 # Return code fragment for use as LHS of a C assignment.
427 return self.calculate_result_code()
429 def calculate_result_code(self):
430 self.not_implemented("calculate_result_code")
432 # def release_target_temp(self, env):
433 # # Release temporaries used by LHS of an assignment.
434 # self.release_subexpr_temps(env)
436 def allocate_temp_result(self, code):
438 raise RuntimeError("Temp allocated multiple times in %r: %r" % (self.__class__.__name__, self.pos))
442 type = PyrexTypes.py_object_type
443 self.temp_code = code.funcstate.allocate_temp(
444 type, manage_ref=self.use_managed_ref)
446 self.temp_code = None
448 def release_temp_result(self, code):
449 if not self.temp_code:
451 raise RuntimeError("temp %s released multiple times in %s" % (
452 self.old_temp, self.__class__.__name__))
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
460 # ---------------- Code Generation -----------------
462 def make_owned_reference(self, code):
463 # If result is a pyobject, make sure we own
465 if self.type.is_pyobject and not self.result_in_temp():
466 code.put_incref(self.result(), self.ctype())
468 def generate_evaluation_code(self, code):
469 code.mark_pos(self.pos)
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)
477 self.allocate_temp_result(code)
479 self.generate_result_code(code)
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)
486 def generate_subexpr_evaluation_code(self, code):
487 for node in self.subexpr_nodes():
488 node.generate_evaluation_code(code)
490 def generate_result_code(self, code):
491 self.not_implemented("generate_result_code")
493 def generate_disposal_code(self, code):
495 if self.type.is_pyobject:
496 code.put_decref_clear(self.result(), self.ctype())
498 # Already done if self.is_temp
499 self.generate_subexpr_disposal_code(code)
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)
507 def generate_post_assignment_code(self, code):
509 if self.type.is_pyobject:
510 code.putln("%s = 0;" % self.result())
512 self.generate_subexpr_disposal_code(code)
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.
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.
526 def free_temps(self, code):
528 if not self.type.is_void:
529 self.release_temp_result(code)
531 self.free_subexpr_temps(code)
533 def free_subexpr_temps(self, code):
534 for sub in self.subexpr_nodes():
537 def generate_function_definitions(self, env, code):
540 # ---------------- Annotation ---------------------
542 def annotate(self, code):
543 for node in self.subexpr_nodes():
546 # ----------------- Coercion ----------------------
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.
554 # This method is called during the analyse_expressions
555 # phase of the src_node's processing.
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.
566 src_is_py_type = src_type.is_pyobject
567 dst_is_py_type = dst_type.is_pyobject
569 if self.check_for_coercion_error(dst_type):
572 if dst_type.is_reference:
573 dst_type = dst_type.ref_base_type
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)
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)
598 def fail_assignment(self, dst_type):
599 error(self.pos, "Cannot assign type '%s' to '%s'" % (self.type, dst_type))
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})
609 self.fail_assignment(dst_type)
613 def coerce_to_pyobject(self, env):
614 return self.coerce_to(PyrexTypes.py_object_type, env)
616 def coerce_to_boolean(self, env):
617 # Coerce result to something acceptable as
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)
627 if type.is_pyobject or type.is_ptr or type.is_float:
628 return CoerceToBooleanNode(self, env)
630 if not (type.is_int or type.is_enum or type.is_error):
632 "Type '%s' not acceptable as a boolean" % type)
635 def coerce_to_integer(self, env):
636 # If not already some C integer type, coerce to longint.
640 return self.coerce_to(PyrexTypes.c_long_type, env)
642 def coerce_to_temp(self, env):
643 # Ensure that the result is in a temporary.
644 if self.result_in_temp():
647 return CoerceToTempNode(self, env)
649 def coerce_to_simple(self, env):
650 # Ensure that the result is simple (see is_simple).
654 return self.coerce_to_temp(env)
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()
663 def may_be_none(self):
664 if not self.type.is_pyobject:
666 if self.constant_result not in (not_a_constant, constant_value_not_set):
667 return self.constant_result is not None
670 def as_cython_attribute(self):
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)
682 class AtomicExprNode(ExprNode):
683 # Abstract base class for expression nodes which have
684 # no sub-expressions.
688 # Override to optimize -- we know we have no children
689 def generate_subexpr_evaluation_code(self, code):
691 def generate_subexpr_disposal_code(self, code):
694 class PyConstNode(AtomicExprNode):
695 # Abstract base class for constant Python values.
698 type = py_object_type
703 def may_be_none(self):
706 def analyse_types(self, env):
709 def calculate_result_code(self):
712 def generate_result_code(self, code):
716 class NoneNode(PyConstNode):
717 # The constant value None
721 constant_result = None
725 def compile_time_value(self, denv):
728 def may_be_none(self):
732 class EllipsisNode(PyConstNode):
733 # '...' in a subscript list.
735 value = "Py_Ellipsis"
737 constant_result = Ellipsis
739 def compile_time_value(self, denv):
743 class ConstNode(AtomicExprNode):
744 # Abstract base type for literal constant nodes.
746 # value string C code fragment
754 def may_be_none(self):
757 def analyse_types(self, env):
758 pass # Types are held in class variables
760 def check_const(self):
763 def get_constant_c_result_code(self):
764 return self.calculate_result_code()
766 def calculate_result_code(self):
767 return str(self.value)
769 def generate_result_code(self, code):
773 class BoolNode(ConstNode):
774 type = PyrexTypes.c_bint_type
775 # The constant value True or False
777 def calculate_constant_result(self):
778 self.constant_result = self.value
780 def compile_time_value(self, denv):
783 def calculate_result_code(self):
784 return str(int(self.value))
787 class NullNode(ConstNode):
788 type = PyrexTypes.c_null_ptr_type
792 def get_constant_c_result_code(self):
796 class CharNode(ConstNode):
797 type = PyrexTypes.c_char_type
799 def calculate_constant_result(self):
800 self.constant_result = ord(self.value)
802 def compile_time_value(self, denv):
803 return ord(self.value)
805 def calculate_result_code(self):
806 return "'%s'" % StringEncoding.escape_char(self.value)
809 class IntNode(ConstNode):
812 # longness "" or "L" or "LL"
813 # is_c_literal True/False/None creator considers this a C integer literal
817 is_c_literal = None # unknown
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()
824 def find_suitable_type_for_value(self):
825 if self.constant_result is constant_value_not_set:
827 self.calculate_constant_result()
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"]
839 suitable_type = PyrexTypes.widest_numeric_type(suitable_type, self.type)
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
846 suitable_type = PyrexTypes.c_long_type
848 suitable_type = PyrexTypes.py_object_type
851 def coerce_to(self, dst_type, env):
852 if self.type is dst_type:
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)
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)
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)
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)
881 def coerce_to_boolean(self, env):
883 self.pos, value=self.value,
884 type = PyrexTypes.c_bint_type,
885 unsigned=self.unsigned, longness=self.longness)
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)
893 self.result_code = self.get_constant_c_result_code()
895 def get_constant_c_result_code(self):
896 return self.value_as_c_integer_string() + self.unsigned + self.longness
898 def value_as_c_integer_string(self, plain_digits=False):
900 if isinstance(value, basestring) and len(value) > 2:
901 # must convert C-incompatible Py3 oct/bin notations
904 value = int(value[2:], 8)
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)
913 def calculate_result_code(self):
914 return self.result_code
916 def calculate_constant_result(self):
917 self.constant_result = Utils.str_to_number(self.value)
919 def compile_time_value(self, denv):
920 return Utils.str_to_number(self.value)
923 class FloatNode(ConstNode):
924 type = PyrexTypes.c_double_type
926 def calculate_constant_result(self):
927 self.constant_result = float(self.value)
929 def compile_time_value(self, denv):
930 return float(self.value)
932 def calculate_result_code(self):
934 assert isinstance(strval, (str, unicode))
935 cmpval = repr(float(strval))
937 return "(Py_HUGE_VAL * 0)"
938 elif cmpval == 'inf':
940 elif cmpval == '-inf':
941 return "(-Py_HUGE_VAL)"
946 class BytesNode(ConstNode):
947 # A char* or bytes literal
951 # start off as Python 'bytes' to support len() in O(1)
954 def compile_time_value(self, denv):
957 def analyse_as_type(self, env):
958 type = PyrexTypes.parse_basic_type(self.value)
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
969 def can_coerce_to_char_literal(self):
970 return len(self.value) == 1
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))
977 def coerce_to(self, dst_type, env):
978 if self.type == dst_type:
981 if not self.can_coerce_to_char_literal():
982 error(self.pos, "Only single-character string literals can be coerced into ints.")
984 if dst_type is PyrexTypes.c_py_unicode_type:
985 error(self.pos, "Bytes literals cannot coerce to Py_UNICODE, use a unicode literal instead.")
987 return CharNode(self.pos, value=self.value)
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
994 self.check_for_coercion_error(dst_type, fail=True)
996 elif dst_type == PyrexTypes.c_char_ptr_type:
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
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)
1011 def generate_evaluation_code(self, code):
1012 if self.type.is_pyobject:
1013 self.result_code = code.get_py_string_const(self.value)
1015 self.result_code = code.get_string_const(self.value)
1017 def get_constant_c_result_code(self):
1020 def calculate_result_code(self):
1021 return self.result_code
1024 class UnicodeNode(PyConstNode):
1025 # A Python unicode object
1027 # value EncodedString
1028 # bytes_value BytesLiteral the literal parsed as bytes string ('-3' unicode literals only)
1033 def coerce_to(self, dst_type, env):
1034 if dst_type is self.type:
1036 elif dst_type is PyrexTypes.c_py_unicode_type:
1037 if not self.can_coerce_to_char_literal():
1038 error(self.pos, "Only single-character Unicode string literals can be coerced into Py_UNICODE.")
1040 int_value = ord(self.value)
1041 return IntNode(self.pos, value=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.")
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)
1052 def can_coerce_to_char_literal(self):
1053 return len(self.value) == 1
1055 def contains_surrogates(self):
1056 # Check if the unicode string contains surrogate code points
1057 # on a CPython platform with wide (UCS-4) or narrow (UTF-16)
1058 # Unicode, i.e. characters that would be spelled as two
1059 # separate code units on a narrow platform.
1060 for c in map(ord, self.value):
1061 if c > 65535: # can only happen on wide platforms
1063 # We only look for the first code unit (D800-DBFF) of a
1064 # surrogate pair - if we find one, the other one
1065 # (DC00-DFFF) is likely there, too. If we don't find it,
1066 # any second code unit cannot make for a surrogate pair by
1068 if c >= 0xD800 and c <= 0xDBFF:
1072 def generate_evaluation_code(self, code):
1073 self.result_code = code.get_py_string_const(self.value)
1075 def calculate_result_code(self):
1076 return self.result_code
1078 def compile_time_value(self, env):
1082 class StringNode(PyConstNode):
1083 # A Python str object, i.e. a byte string in Python 2.x and a
1084 # unicode string in Python 3.x
1086 # value BytesLiteral (or EncodedString with ASCII content)
1087 # unicode_value EncodedString or None
1088 # is_identifier boolean
1091 is_identifier = None
1092 unicode_value = None
1094 def coerce_to(self, dst_type, env):
1095 if dst_type is not py_object_type and not str_type.subtype_of(dst_type):
1096 # if dst_type is Builtin.bytes_type:
1097 # # special case: bytes = 'str literal'
1098 # return BytesNode(self.pos, value=self.value)
1099 if not dst_type.is_pyobject:
1100 return BytesNode(self.pos, value=self.value).coerce_to(dst_type, env)
1101 self.check_for_coercion_error(dst_type, fail=True)
1103 # this will be a unicode string in Py3, so make sure we can decode it
1104 if self.value.encoding and isinstance(self.value, StringEncoding.BytesLiteral):
1106 self.value.decode(self.value.encoding)
1107 except UnicodeDecodeError:
1108 error(self.pos, ("Decoding unprefixed string literal from '%s' failed. Consider using"
1109 "a byte string or unicode string explicitly, "
1110 "or adjust the source code encoding.") % self.value.encoding)
1114 def can_coerce_to_char_literal(self):
1115 return not self.is_identifier and len(self.value) == 1
1117 def generate_evaluation_code(self, code):
1118 self.result_code = code.get_py_string_const(
1119 self.value, identifier=self.is_identifier, is_str=True)
1121 def get_constant_c_result_code(self):
1124 def calculate_result_code(self):
1125 return self.result_code
1127 def compile_time_value(self, env):
1131 class IdentifierStringNode(StringNode):
1132 # A special str value that represents an identifier (bytes in Py2,
1134 is_identifier = True
1137 class LongNode(AtomicExprNode):
1138 # Python long integer literal
1142 type = py_object_type
1144 def calculate_constant_result(self):
1145 self.constant_result = Utils.str_to_number(self.value)
1147 def compile_time_value(self, denv):
1148 return Utils.str_to_number(self.value)
1150 def analyse_types(self, env):
1153 def may_be_none(self):
1156 gil_message = "Constructing Python long int"
1158 def generate_result_code(self, code):
1160 '%s = PyLong_FromString((char *)"%s", 0, 0); %s' % (
1163 code.error_goto_if_null(self.result(), self.pos)))
1164 code.put_gotref(self.py_result())
1167 class ImagNode(AtomicExprNode):
1168 # Imaginary number literal
1170 # value float imaginary part
1172 type = PyrexTypes.c_double_complex_type
1174 def calculate_constant_result(self):
1175 self.constant_result = complex(0.0, self.value)
1177 def compile_time_value(self, denv):
1178 return complex(0.0, self.value)
1180 def analyse_types(self, env):
1181 self.type.create_declaration_utility_code(env)
1183 def may_be_none(self):
1186 def coerce_to(self, dst_type, env):
1187 if self.type is dst_type:
1189 node = ImagNode(self.pos, value=self.value)
1190 if dst_type.is_pyobject:
1192 node.type = PyrexTypes.py_object_type
1193 # We still need to perform normal coerce_to processing on the
1194 # result, because we might be coercing to an extension type,
1195 # in which case a type test node will be needed.
1196 return AtomicExprNode.coerce_to(node, dst_type, env)
1198 gil_message = "Constructing complex number"
1200 def calculate_result_code(self):
1201 if self.type.is_pyobject:
1202 return self.result()
1204 return "%s(0, %r)" % (self.type.from_parts, float(self.value))
1206 def generate_result_code(self, code):
1207 if self.type.is_pyobject:
1209 "%s = PyComplex_FromDoubles(0.0, %r); %s" % (
1212 code.error_goto_if_null(self.result(), self.pos)))
1213 code.put_gotref(self.py_result())
1216 class NewExprNode(AtomicExprNode):
1220 # cppclass node c++ class to create
1224 def infer_type(self, env):
1225 type = self.cppclass.analyse_as_type(env)
1226 if type is None or not type.is_cpp_class:
1227 error(self.pos, "new operator can only be applied to a C++ class")
1228 self.type = error_type
1231 constructor = type.scope.lookup(u'<init>')
1232 if constructor is None:
1233 return_type = PyrexTypes.CFuncType(type, [])
1234 return_type = PyrexTypes.CPtrType(return_type)
1235 type.scope.declare_cfunction(u'<init>', return_type, self.pos)
1236 constructor = type.scope.lookup(u'<init>')
1237 self.class_type = type
1238 self.entry = constructor
1239 self.type = constructor.type
1242 def analyse_types(self, env):
1243 if self.type is None:
1244 self.infer_type(env)
1246 def may_be_none(self):
1249 def generate_result_code(self, code):
1252 def calculate_result_code(self):
1253 return "new " + self.class_type.declaration_code("")
1256 class NameNode(AtomicExprNode):
1257 # Reference to a local or global variable name.
1259 # name string Python name of the variable
1260 # entry Entry Symbol table entry
1261 # type_entry Entry For extension type names, the original type entry
1264 is_cython_module = False
1265 cython_attribute = None
1266 lhs_of_first_assignment = False
1267 is_used_as_rvalue = 0
1271 def create_analysed_rvalue(pos, env, entry):
1272 node = NameNode(pos)
1273 node.analyse_types(env, entry=entry)
1276 def as_cython_attribute(self):
1277 return self.cython_attribute
1279 create_analysed_rvalue = staticmethod(create_analysed_rvalue)
1281 def type_dependencies(self, env):
1282 if self.entry is None:
1283 self.entry = env.lookup(self.name)
1284 if self.entry is not None and self.entry.type.is_unspecified:
1285 return (self.entry,)
1289 def infer_type(self, env):
1290 if self.entry is None:
1291 self.entry = env.lookup(self.name)
1292 if self.entry is None:
1293 return py_object_type
1294 elif (self.entry.type.is_extension_type or self.entry.type.is_builtin_type) and \
1295 self.name == self.entry.type.name:
1296 # Unfortunately the type attribute of type objects
1297 # is used for the pointer to the type they represent.
1299 elif self.entry.type.is_cfunction:
1300 # special case: referring to a C function must return its pointer
1301 return PyrexTypes.CPtrType(self.entry.type)
1303 return self.entry.type
1305 def compile_time_value(self, denv):
1307 return denv.lookup(self.name)
1309 error(self.pos, "Compile-time name '%s' not defined" % self.name)
1311 def get_constant_c_result_code(self):
1312 if not self.entry or self.entry.type.is_pyobject:
1314 return self.entry.cname
1316 def coerce_to(self, dst_type, env):
1317 # If coercing to a generic pyobject and this is a builtin
1318 # C function with a Python equivalent, manufacture a NameNode
1319 # referring to the Python builtin.
1320 #print "NameNode.coerce_to:", self.name, dst_type ###
1321 if dst_type is py_object_type:
1323 if entry and entry.is_cfunction:
1324 var_entry = entry.as_variable
1326 if var_entry.is_builtin and Options.cache_builtins:
1327 var_entry = env.declare_builtin(var_entry.name, self.pos)
1328 node = NameNode(self.pos, name = self.name)
1329 node.entry = var_entry
1330 node.analyse_rvalue_entry(env)
1332 return super(NameNode, self).coerce_to(dst_type, env)
1334 def analyse_as_module(self, env):
1335 # Try to interpret this as a reference to a cimported module.
1336 # Returns the module scope, or None.
1339 entry = env.lookup(self.name)
1340 if entry and entry.as_module:
1341 return entry.as_module
1344 def analyse_as_type(self, env):
1345 if self.cython_attribute:
1346 type = PyrexTypes.parse_basic_type(self.cython_attribute)
1348 type = PyrexTypes.parse_basic_type(self.name)
1353 entry = env.lookup(self.name)
1354 if entry and entry.is_type:
1359 def analyse_as_extension_type(self, env):
1360 # Try to interpret this as a reference to an extension type.
1361 # Returns the extension type, or None.
1364 entry = env.lookup(self.name)
1365 if entry and entry.is_type and entry.type.is_extension_type:
1370 def analyse_target_declaration(self, env):
1372 self.entry = env.lookup_here(self.name)
1374 if env.directives['warn.undeclared']:
1375 warning(self.pos, "implicit declaration of '%s'" % self.name, 1)
1376 if env.directives['infer_types'] != False:
1377 type = unspecified_type
1379 type = py_object_type
1380 self.entry = env.declare_var(self.name, type, self.pos)
1381 env.control_flow.set_state(self.pos, (self.name, 'initialized'), True)
1382 env.control_flow.set_state(self.pos, (self.name, 'source'), 'assignment')
1383 if self.entry.is_declared_generic:
1384 self.result_ctype = py_object_type
1386 def analyse_types(self, env):
1387 if self.entry is None:
1388 self.entry = env.lookup(self.name)
1390 self.entry = env.declare_builtin(self.name, self.pos)
1392 self.type = PyrexTypes.error_type
1397 if entry.type.is_buffer:
1399 Buffer.used_buffer_aux_vars(entry)
1400 if entry.utility_code:
1401 env.use_utility_code(entry.utility_code)
1402 self.analyse_rvalue_entry(env)
1404 def analyse_target_types(self, env):
1405 self.analyse_entry(env)
1406 if not self.is_lvalue():
1407 error(self.pos, "Assignment to non-lvalue '%s'"
1409 self.type = PyrexTypes.error_type
1411 if self.entry.type.is_buffer:
1413 Buffer.used_buffer_aux_vars(self.entry)
1415 def analyse_rvalue_entry(self, env):
1416 #print "NameNode.analyse_rvalue_entry:", self.name ###
1417 #print "Entry:", self.entry.__dict__ ###
1418 self.analyse_entry(env)
1420 if entry.is_declared_generic:
1421 self.result_ctype = py_object_type
1422 if entry.is_pyglobal or entry.is_builtin:
1423 if Options.cache_builtins and entry.is_builtin:
1427 env.use_utility_code(get_name_interned_utility_code)
1428 self.is_used_as_rvalue = 1
1430 def nogil_check(self, env):
1431 if self.is_used_as_rvalue:
1433 if entry.is_builtin:
1434 if not Options.cache_builtins: # cached builtins are ok
1436 elif entry.is_pyglobal:
1439 gil_message = "Accessing Python global or builtin"
1441 def analyse_entry(self, env):
1442 #print "NameNode.analyse_entry:", self.name ###
1443 self.check_identifier_kind()
1448 def check_identifier_kind(self):
1449 # Check that this is an appropriate kind of name for use in an
1450 # expression. Also finds the variable entry associated with
1451 # an extension type.
1453 if entry.is_type and entry.type.is_extension_type:
1454 self.type_entry = entry
1455 if not (entry.is_const or entry.is_variable
1456 or entry.is_builtin or entry.is_cfunction
1457 or entry.is_cpp_class):
1458 if self.entry.as_variable:
1459 self.entry = self.entry.as_variable
1462 "'%s' is not a constant, variable or function identifier" % self.name)
1464 def is_simple(self):
1465 # If it's not a C variable, it'll be in a temp.
1468 def calculate_target_results(self, env):
1471 def check_const(self):
1473 if entry is not None and not (entry.is_const or entry.is_cfunction or entry.is_builtin):
1478 def check_const_addr(self):
1480 if not (entry.is_cglobal or entry.is_cfunction or entry.is_builtin):
1481 self.addr_not_const()
1485 def is_lvalue(self):
1486 return self.entry.is_variable and \
1487 not self.entry.type.is_array and \
1488 not self.entry.is_readonly
1490 def is_ephemeral(self):
1491 # Name nodes are never ephemeral, even if the
1492 # result is in a temporary.
1495 def calculate_result_code(self):
1498 return "<error>" # There was an error earlier
1501 def generate_result_code(self, code):
1502 assert hasattr(self, 'entry')
1505 return # There was an error earlier
1506 if entry.is_builtin and Options.cache_builtins:
1507 return # Lookup already cached
1508 elif entry.is_pyclass_attr:
1509 assert entry.type.is_pyobject, "Python global or builtin not a Python object"
1510 interned_cname = code.intern_identifier(self.entry.name)
1511 if entry.is_builtin:
1512 namespace = Naming.builtins_cname
1513 else: # entry.is_pyglobal
1514 namespace = entry.scope.namespace_cname
1516 '%s = PyObject_GetItem(%s, %s); %s' % (
1520 code.error_goto_if_null(self.result(), self.pos)))
1521 code.put_gotref(self.py_result())
1523 elif entry.is_pyglobal or entry.is_builtin:
1524 assert entry.type.is_pyobject, "Python global or builtin not a Python object"
1525 interned_cname = code.intern_identifier(self.entry.name)
1526 if entry.is_builtin:
1527 namespace = Naming.builtins_cname
1528 else: # entry.is_pyglobal
1529 namespace = entry.scope.namespace_cname
1530 code.globalstate.use_utility_code(get_name_interned_utility_code)
1532 '%s = __Pyx_GetName(%s, %s); %s' % (
1536 code.error_goto_if_null(self.result(), self.pos)))
1537 code.put_gotref(self.py_result())
1539 elif entry.is_local and False:
1540 # control flow not good enough yet
1541 assigned = entry.scope.control_flow.get_state((entry.name, 'initialized'), self.pos)
1542 if assigned is False:
1543 error(self.pos, "local variable '%s' referenced before assignment" % entry.name)
1544 elif not Options.init_local_none and assigned is None:
1545 code.putln('if (%s == 0) { PyErr_SetString(PyExc_UnboundLocalError, "%s"); %s }' %
1546 (entry.cname, entry.name, code.error_goto(self.pos)))
1547 entry.scope.control_flow.set_state(self.pos, (entry.name, 'initialized'), True)
1549 def generate_assignment_code(self, rhs, code):
1550 #print "NameNode.generate_assignment_code:", self.name ###
1553 return # There was an error earlier
1555 if (self.entry.type.is_ptr and isinstance(rhs, ListNode)
1556 and not self.lhs_of_first_assignment):
1557 error(self.pos, "Literal list must be assigned to pointer at time of declaration")
1559 # is_pyglobal seems to be True for module level-globals only.
1560 # We use this to access class->tp_dict if necessary.
1561 if entry.is_pyglobal:
1562 assert entry.type.is_pyobject, "Python global or builtin not a Python object"
1563 interned_cname = code.intern_identifier(self.entry.name)
1564 namespace = self.entry.scope.namespace_cname
1566 # if the entry is a member we have to cheat: SetAttr does not work
1567 # on types, so we create a descriptor which is then added to tp_dict
1568 code.put_error_if_neg(self.pos,
1569 'PyDict_SetItem(%s->tp_dict, %s, %s)' % (
1573 rhs.generate_disposal_code(code)
1574 rhs.free_temps(code)
1575 # in Py2.6+, we need to invalidate the method cache
1576 code.putln("PyType_Modified(%s);" %
1577 entry.scope.parent_type.typeptr_cname)
1578 elif entry.is_pyclass_attr:
1579 code.put_error_if_neg(self.pos,
1580 'PyObject_SetItem(%s, %s, %s)' % (
1584 rhs.generate_disposal_code(code)
1585 rhs.free_temps(code)
1587 code.put_error_if_neg(self.pos,
1588 'PyObject_SetAttr(%s, %s, %s)' % (
1592 if debug_disposal_code:
1593 print("NameNode.generate_assignment_code:")
1594 print("...generating disposal code for %s" % rhs)
1595 rhs.generate_disposal_code(code)
1596 rhs.free_temps(code)
1598 if self.type.is_buffer:
1599 # Generate code for doing the buffer release/acquisition.
1600 # This might raise an exception in which case the assignment (done
1601 # below) will not happen.
1603 # The reason this is not in a typetest-like node is because the
1604 # variables that the acquired buffer info is stored to is allocated
1605 # per entry and coupled with it.
1606 self.generate_acquire_buffer(rhs, code)
1608 if self.type.is_pyobject:
1609 #print "NameNode.generate_assignment_code: to", self.name ###
1610 #print "...from", rhs ###
1611 #print "...LHS type", self.type, "ctype", self.ctype() ###
1612 #print "...RHS type", rhs.type, "ctype", rhs.ctype() ###
1613 if self.use_managed_ref:
1614 rhs.make_owned_reference(code)
1615 if entry.is_cglobal:
1616 code.put_gotref(self.py_result())
1617 if not self.lhs_of_first_assignment:
1618 if entry.is_local and not Options.init_local_none:
1619 initialized = entry.scope.control_flow.get_state((entry.name, 'initialized'), self.pos)
1620 if initialized is True:
1621 code.put_decref(self.result(), self.ctype())
1622 elif initialized is None:
1623 code.put_xdecref(self.result(), self.ctype())
1625 code.put_decref(self.result(), self.ctype())
1626 if entry.is_cglobal:
1627 code.put_giveref(rhs.py_result())
1629 code.putln('%s = %s;' % (self.result(),
1630 rhs.result_as(self.ctype())))
1631 if debug_disposal_code:
1632 print("NameNode.generate_assignment_code:")
1633 print("...generating post-assignment code for %s" % rhs)
1634 rhs.generate_post_assignment_code(code)
1635 rhs.free_temps(code)
1637 def generate_acquire_buffer(self, rhs, code):
1638 # rhstmp is only used in case the rhs is a complicated expression leading to
1639 # the object, to avoid repeating the same C expression for every reference
1640 # to the rhs. It does NOT hold a reference.
1641 pretty_rhs = isinstance(rhs, NameNode) or rhs.is_temp
1643 rhstmp = rhs.result_as(self.ctype())
1645 rhstmp = code.funcstate.allocate_temp(self.entry.type, manage_ref=False)
1646 code.putln('%s = %s;' % (rhstmp, rhs.result_as(self.ctype())))
1648 buffer_aux = self.entry.buffer_aux
1649 bufstruct = buffer_aux.buffer_info_var.cname
1651 Buffer.put_assign_to_buffer(self.result(), rhstmp, buffer_aux, self.entry.type,
1652 is_initialized=not self.lhs_of_first_assignment,
1653 pos=self.pos, code=code)
1656 code.putln("%s = 0;" % rhstmp)
1657 code.funcstate.release_temp(rhstmp)
1659 def generate_deletion_code(self, code):
1660 if self.entry is None:
1661 return # There was an error earlier
1662 if not self.entry.is_pyglobal:
1663 error(self.pos, "Deletion of local or C global name not supported")
1665 if self.entry.is_pyclass_attr:
1666 namespace = self.entry.scope.namespace_cname
1667 code.put_error_if_neg(self.pos,
1668 'PyMapping_DelItemString(%s, "%s")' % (
1672 code.put_error_if_neg(self.pos,
1673 '__Pyx_DelAttrString(%s, "%s")' % (
1674 Naming.module_cname,
1677 def annotate(self, code):
1678 if hasattr(self, 'is_called') and self.is_called:
1679 pos = (self.pos[0], self.pos[1], self.pos[2] - len(self.name) - 1)
1680 if self.type.is_pyobject:
1681 code.annotate(pos, AnnotationItem('py_call', 'python function', size=len(self.name)))
1683 code.annotate(pos, AnnotationItem('c_call', 'c function', size=len(self.name)))
1685 class BackquoteNode(ExprNode):
1690 type = py_object_type
1694 def analyse_types(self, env):
1695 self.arg.analyse_types(env)
1696 self.arg = self.arg.coerce_to_pyobject(env)
1699 gil_message = "Backquote expression"
1701 def calculate_constant_result(self):
1702 self.constant_result = repr(self.arg.constant_result)
1704 def generate_result_code(self, code):
1706 "%s = PyObject_Repr(%s); %s" % (
1708 self.arg.py_result(),
1709 code.error_goto_if_null(self.result(), self.pos)))
1710 code.put_gotref(self.py_result())
1714 class ImportNode(ExprNode):
1715 # Used as part of import statement implementation.
1716 # Implements result =
1717 # __import__(module_name, globals(), None, name_list)
1719 # module_name StringNode dotted name of module
1720 # name_list ListNode or None list of names to be imported
1722 type = py_object_type
1724 subexprs = ['module_name', 'name_list']
1726 def analyse_types(self, env):
1727 self.module_name.analyse_types(env)
1728 self.module_name = self.module_name.coerce_to_pyobject(env)
1730 self.name_list.analyse_types(env)
1731 self.name_list.coerce_to_pyobject(env)
1733 env.use_utility_code(import_utility_code)
1735 gil_message = "Python import"
1737 def generate_result_code(self, code):
1739 name_list_code = self.name_list.py_result()
1741 name_list_code = "0"
1743 "%s = __Pyx_Import(%s, %s); %s" % (
1745 self.module_name.py_result(),
1747 code.error_goto_if_null(self.result(), self.pos)))
1748 code.put_gotref(self.py_result())
1751 class IteratorNode(ExprNode):
1752 # Used as part of for statement implementation.
1754 # allocate_counter_temp/release_counter_temp needs to be called
1755 # by parent (ForInStatNode)
1757 # Implements result = iter(sequence)
1761 type = py_object_type
1763 subexprs = ['sequence']
1765 def analyse_types(self, env):
1766 self.sequence.analyse_types(env)
1767 if (self.sequence.type.is_array or self.sequence.type.is_ptr) and \
1768 not self.sequence.type.is_string:
1769 # C array iteration will be transformed later on
1770 self.type = self.sequence.type
1772 self.sequence = self.sequence.coerce_to_pyobject(env)
1773 if self.sequence.type is list_type or \
1774 self.sequence.type is tuple_type:
1775 self.sequence = self.sequence.as_none_safe_node("'NoneType' object is not iterable")
1778 gil_message = "Iterating over Python object"
1780 def allocate_counter_temp(self, code):
1781 self.counter_cname = code.funcstate.allocate_temp(
1782 PyrexTypes.c_py_ssize_t_type, manage_ref=False)
1784 def release_counter_temp(self, code):
1785 code.funcstate.release_temp(self.counter_cname)
1787 def generate_result_code(self, code):
1788 if self.sequence.type.is_array or self.sequence.type.is_ptr:
1789 raise InternalError("for in carray slice not transformed")
1790 is_builtin_sequence = self.sequence.type is list_type or \
1791 self.sequence.type is tuple_type
1792 may_be_a_sequence = not self.sequence.type.is_builtin_type
1793 if may_be_a_sequence:
1795 "if (PyList_CheckExact(%s) || PyTuple_CheckExact(%s)) {" % (
1796 self.sequence.py_result(),
1797 self.sequence.py_result()))
1798 if is_builtin_sequence or may_be_a_sequence:
1800 "%s = 0; %s = %s; __Pyx_INCREF(%s);" % (
1803 self.sequence.py_result(),
1805 if not is_builtin_sequence:
1806 if may_be_a_sequence:
1807 code.putln("} else {")
1808 code.putln("%s = -1; %s = PyObject_GetIter(%s); %s" % (
1811 self.sequence.py_result(),
1812 code.error_goto_if_null(self.result(), self.pos)))
1813 code.put_gotref(self.py_result())
1814 if may_be_a_sequence:
1818 class NextNode(AtomicExprNode):
1819 # Used as part of for statement implementation.
1820 # Implements result = iterator.next()
1821 # Created during analyse_types phase.
1822 # The iterator is not owned by this node.
1826 type = py_object_type
1828 def __init__(self, iterator, env):
1829 self.pos = iterator.pos
1830 self.iterator = iterator
1831 if iterator.type.is_ptr or iterator.type.is_array:
1832 self.type = iterator.type.base_type
1835 def generate_result_code(self, code):
1836 sequence_type = self.iterator.sequence.type
1837 if sequence_type is list_type:
1838 type_checks = [(list_type, "List")]
1839 elif sequence_type is tuple_type:
1840 type_checks = [(tuple_type, "Tuple")]
1841 elif not sequence_type.is_builtin_type:
1842 type_checks = [(list_type, "List"), (tuple_type, "Tuple")]
1846 for py_type, prefix in type_checks:
1847 if len(type_checks) > 1:
1849 "if (likely(Py%s_CheckExact(%s))) {" % (
1850 prefix, self.iterator.py_result()))
1852 "if (%s >= Py%s_GET_SIZE(%s)) break;" % (
1853 self.iterator.counter_cname,
1855 self.iterator.py_result()))
1857 "%s = Py%s_GET_ITEM(%s, %s); __Pyx_INCREF(%s); %s++;" % (
1860 self.iterator.py_result(),
1861 self.iterator.counter_cname,
1863 self.iterator.counter_cname))
1864 if len(type_checks) > 1:
1866 if len(type_checks) == 1:
1870 "%s = PyIter_Next(%s);" % (
1872 self.iterator.py_result()))
1876 code.putln(code.error_goto_if_PyErr(self.pos))
1877 code.putln("break;")
1879 code.put_gotref(self.py_result())
1883 class ExcValueNode(AtomicExprNode):
1884 # Node created during analyse_types phase
1885 # of an ExceptClauseNode to fetch the current
1888 type = py_object_type
1890 def __init__(self, pos, env):
1891 ExprNode.__init__(self, pos)
1893 def set_var(self, var):
1896 def calculate_result_code(self):
1899 def generate_result_code(self, code):
1902 def analyse_types(self, env):
1906 class TempNode(ExprNode):
1907 # Node created during analyse_types phase
1908 # of some nodes to hold a temporary value.
1910 # Note: One must call "allocate" and "release" on
1911 # the node during code generation to get/release the temp.
1912 # This is because the temp result is often used outside of
1913 # the regular cycle.
1917 def __init__(self, pos, type, env):
1918 ExprNode.__init__(self, pos)
1920 if type.is_pyobject:
1921 self.result_ctype = py_object_type
1924 def analyse_types(self, env):
1927 def generate_result_code(self, code):
1930 def allocate(self, code):
1931 self.temp_cname = code.funcstate.allocate_temp(self.type, manage_ref=True)
1933 def release(self, code):
1934 code.funcstate.release_temp(self.temp_cname)
1935 self.temp_cname = None
1939 return self.temp_cname
1941 assert False, "Remember to call allocate/release on TempNode"
1944 # Do not participate in normal temp alloc/dealloc:
1945 def allocate_temp_result(self, code):
1948 def release_temp_result(self, code):
1951 class PyTempNode(TempNode):
1952 # TempNode holding a Python value.
1954 def __init__(self, pos, env):
1955 TempNode.__init__(self, pos, PyrexTypes.py_object_type, env)
1957 class RawCNameExprNode(ExprNode):
1960 def __init__(self, pos, type=None):
1964 def analyse_types(self, env):
1967 def set_cname(self, cname):
1973 def generate_result_code(self, code):
1977 #-------------------------------------------------------------------
1981 #-------------------------------------------------------------------
1983 class IndexNode(ExprNode):
1984 # Sequence indexing.
1988 # indices [ExprNode]
1989 # is_buffer_access boolean Whether this is a buffer access.
1991 # indices is used on buffer access, index on non-buffer access.
1992 # The former contains a clean list of index parameters, the
1993 # latter whatever Python object is needed for index access.
1995 subexprs = ['base', 'index', 'indices']
1998 def __init__(self, pos, index, *args, **kw):
1999 ExprNode.__init__(self, pos, index=index, *args, **kw)
2002 def calculate_constant_result(self):
2003 self.constant_result = \
2004 self.base.constant_result[self.index.constant_result]
2006 def compile_time_value(self, denv):
2007 base = self.base.compile_time_value(denv)
2008 index = self.index.compile_time_value(denv)
2011 except Exception, e:
2012 self.compile_time_value_error(e)
2014 def is_ephemeral(self):
2015 return self.base.is_ephemeral()
2017 def analyse_target_declaration(self, env):
2020 def analyse_as_type(self, env):
2021 base_type = self.base.analyse_as_type(env)
2022 if base_type and not base_type.is_pyobject:
2023 if base_type.is_cpp_class:
2024 if isinstance(self.index, TupleNode):
2025 template_values = self.index.args
2027 template_values = [self.index]
2029 type_node = Nodes.TemplatedTypeNode(
2031 positional_args = template_values,
2032 keyword_args = None)
2033 return type_node.analyse(env, base_type = base_type)
2035 return PyrexTypes.CArrayType(base_type, int(self.index.compile_time_value(env)))
2038 def type_dependencies(self, env):
2039 return self.base.type_dependencies(env) + self.index.type_dependencies(env)
2041 def infer_type(self, env):
2042 base_type = self.base.infer_type(env)
2043 if isinstance(self.index, SliceNode):
2045 if base_type.is_string:
2046 # sliced C strings must coerce to Python
2048 elif base_type in (unicode_type, bytes_type, str_type, list_type, tuple_type):
2049 # slicing these returns the same type
2052 # TODO: Handle buffers (hopefully without too much redundancy).
2053 return py_object_type
2055 index_type = self.index.infer_type(env)
2056 if index_type and index_type.is_int or isinstance(self.index, (IntNode, LongNode)):
2058 if base_type is unicode_type:
2059 # Py_UNICODE will automatically coerce to a unicode string
2060 # if required, so this is safe. We only infer Py_UNICODE
2061 # when the index is a C integer type. Otherwise, we may
2062 # need to use normal Python item access, in which case
2063 # it's faster to return the one-char unicode string than
2064 # to receive it, throw it away, and potentially rebuild it
2065 # on a subsequent PyObject coercion.
2066 return PyrexTypes.c_py_unicode_type
2067 elif isinstance(self.base, BytesNode):
2068 #if env.global_scope().context.language_level >= 3:
2069 # # infering 'char' can be made to work in Python 3 mode
2070 # return PyrexTypes.c_char_type
2071 # Py2/3 return different types on indexing bytes objects
2072 return py_object_type
2073 elif base_type.is_ptr or base_type.is_array:
2074 return base_type.base_type
2076 # may be slicing or indexing, we don't know
2077 if base_type is unicode_type:
2078 # this type always returns its own type on Python indexing/slicing
2081 # TODO: Handle buffers (hopefully without too much redundancy).
2082 return py_object_type
2084 def analyse_types(self, env):
2085 self.analyse_base_and_index_types(env, getting = 1)
2087 def analyse_target_types(self, env):
2088 self.analyse_base_and_index_types(env, setting = 1)
2090 def analyse_base_and_index_types(self, env, getting = 0, setting = 0):
2091 # Note: This might be cleaned up by having IndexNode
2092 # parsed in a saner way and only construct the tuple if
2095 # Note that this function must leave IndexNode in a cloneable state.
2096 # For buffers, self.index is packed out on the initial analysis, and
2097 # when cloning self.indices is copied.
2098 self.is_buffer_access = False
2100 self.base.analyse_types(env)
2101 if self.base.type.is_error:
2102 # Do not visit child tree if base is undeclared to avoid confusing
2104 self.type = PyrexTypes.error_type
2107 is_slice = isinstance(self.index, SliceNode)
2108 # Potentially overflowing index value.
2109 if not is_slice and isinstance(self.index, IntNode) and Utils.long_literal(self.index.value):
2110 self.index = self.index.coerce_to_pyobject(env)
2112 # Handle the case where base is a literal char* (and we expect a string, not an int)
2113 if isinstance(self.base, BytesNode) or is_slice:
2114 if self.base.type.is_string or not (self.base.type.is_ptr or self.base.type.is_array):
2115 self.base = self.base.coerce_to_pyobject(env)
2117 skip_child_analysis = False
2118 buffer_access = False
2119 if self.base.type.is_buffer:
2121 indices = self.indices
2123 if isinstance(self.index, TupleNode):
2124 indices = self.index.args
2126 indices = [self.index]
2127 if len(indices) == self.base.type.ndim:
2128 buffer_access = True
2129 skip_child_analysis = True
2131 x.analyse_types(env)
2132 if not x.type.is_int:
2133 buffer_access = False
2135 assert hasattr(self.base, "entry") # Must be a NameNode-like node
2137 # On cloning, indices is cloned. Otherwise, unpack index into indices
2138 assert not (buffer_access and isinstance(self.index, CloneNode))
2141 self.indices = indices
2143 self.type = self.base.type.dtype
2144 self.is_buffer_access = True
2145 self.buffer_type = self.base.entry.type
2147 if getting and self.type.is_pyobject:
2150 if not self.base.entry.type.writable:
2151 error(self.pos, "Writing to readonly buffer")
2153 self.base.entry.buffer_aux.writable_needed = True
2155 base_type = self.base.type
2156 if isinstance(self.index, TupleNode):
2157 self.index.analyse_types(env, skip_children=skip_child_analysis)
2158 elif not skip_child_analysis:
2159 self.index.analyse_types(env)
2160 self.original_index_type = self.index.type
2161 if base_type is PyrexTypes.c_py_unicode_type:
2162 # we infer Py_UNICODE for unicode strings in some
2163 # cases, but indexing must still work for them
2164 if self.index.constant_result in (0, -1):
2165 # FIXME: we know that this node is redundant -
2166 # currently, this needs to get handled in Optimize.py
2168 self.base = self.base.coerce_to_pyobject(env)
2169 base_type = self.base.type
2170 if base_type.is_pyobject:
2171 if self.index.type.is_int:
2173 and (base_type in (list_type, tuple_type, unicode_type))
2174 and (not self.index.type.signed or isinstance(self.index, IntNode) and int(self.index.value) >= 0)
2175 and not env.directives['boundscheck']):
2179 self.index = self.index.coerce_to(PyrexTypes.c_py_ssize_t_type, env).coerce_to_simple(env)
2181 self.index = self.index.coerce_to_pyobject(env)
2183 if self.index.type.is_int and base_type is unicode_type:
2184 # Py_UNICODE will automatically coerce to a unicode string
2185 # if required, so this is fast and safe
2186 self.type = PyrexTypes.c_py_unicode_type
2187 elif is_slice and base_type in (bytes_type, str_type, unicode_type, list_type, tuple_type):
2188 self.type = base_type
2190 self.type = py_object_type
2192 if base_type.is_ptr or base_type.is_array:
2193 self.type = base_type.base_type
2195 self.type = base_type
2196 elif self.index.type.is_pyobject:
2197 self.index = self.index.coerce_to(
2198 PyrexTypes.c_py_ssize_t_type, env)
2199 elif not self.index.type.is_int:
2201 "Invalid index type '%s'" %
2203 elif base_type.is_cpp_class:
2204 function = env.lookup_operator("[]", [self.base, self.index])
2205 if function is None:
2206 error(self.pos, "Indexing '%s' not supported for index type '%s'" % (base_type, self.index.type))
2207 self.type = PyrexTypes.error_type
2208 self.result_code = "<error>"
2210 func_type = function.type
2211 if func_type.is_ptr:
2212 func_type = func_type.base_type
2213 self.index = self.index.coerce_to(func_type.args[0].type, env)
2214 self.type = func_type.return_type
2215 if setting and not func_type.return_type.is_reference:
2216 error(self.pos, "Can't set non-reference result '%s'" % self.type)
2219 "Attempting to index non-array type '%s'" %
2221 self.type = PyrexTypes.error_type
2223 gil_message = "Indexing Python object"
2225 def nogil_check(self, env):
2226 if self.is_buffer_access:
2227 if env.directives['boundscheck']:
2228 error(self.pos, "Cannot check buffer index bounds without gil; use boundscheck(False) directive")
2230 elif self.type.is_pyobject:
2231 error(self.pos, "Cannot access buffer with object dtype without gil")
2233 super(IndexNode, self).nogil_check(env)
2236 def check_const_addr(self):
2237 return self.base.check_const_addr() and self.index.check_const()
2239 def is_lvalue(self):
2242 def calculate_result_code(self):
2243 if self.is_buffer_access:
2244 return "(*%s)" % self.buffer_ptr_code
2245 elif self.base.type is list_type:
2246 return "PyList_GET_ITEM(%s, %s)" % (self.base.result(), self.index.result())
2247 elif self.base.type is tuple_type:
2248 return "PyTuple_GET_ITEM(%s, %s)" % (self.base.result(), self.index.result())
2249 elif self.base.type is unicode_type and self.type is PyrexTypes.c_py_unicode_type:
2250 return "PyUnicode_AS_UNICODE(%s)[%s]" % (self.base.result(), self.index.result())
2251 elif (self.type.is_ptr or self.type.is_array) and self.type == self.base.type:
2252 error(self.pos, "Invalid use of pointer slice")
2254 return "(%s[%s])" % (
2255 self.base.result(), self.index.result())
2257 def extra_index_params(self):
2258 if self.index.type.is_int:
2259 if self.original_index_type.signed:
2260 size_adjustment = ""
2262 size_adjustment = "+1"
2263 return ", sizeof(%s)%s, %s" % (self.original_index_type.declaration_code(""), size_adjustment, self.original_index_type.to_py_function)
2267 def generate_subexpr_evaluation_code(self, code):
2268 self.base.generate_evaluation_code(code)
2269 if not self.indices:
2270 self.index.generate_evaluation_code(code)
2272 for i in self.indices:
2273 i.generate_evaluation_code(code)
2275 def generate_subexpr_disposal_code(self, code):
2276 self.base.generate_disposal_code(code)
2277 if not self.indices:
2278 self.index.generate_disposal_code(code)
2280 for i in self.indices:
2281 i.generate_disposal_code(code)
2283 def free_subexpr_temps(self, code):
2284 self.base.free_temps(code)
2285 if not self.indices:
2286 self.index.free_temps(code)
2288 for i in self.indices:
2291 def generate_result_code(self, code):
2292 if self.is_buffer_access:
2293 if code.globalstate.directives['nonecheck']:
2294 self.put_nonecheck(code)
2295 self.buffer_ptr_code = self.buffer_lookup_code(code)
2296 if self.type.is_pyobject:
2297 # is_temp is True, so must pull out value and incref it.
2298 code.putln("%s = *%s;" % (self.result(), self.buffer_ptr_code))
2299 code.putln("__Pyx_INCREF((PyObject*)%s);" % self.result())
2301 if self.type.is_pyobject:
2302 if self.index.type.is_int:
2303 index_code = self.index.result()
2304 if self.base.type is list_type:
2305 function = "__Pyx_GetItemInt_List"
2306 elif self.base.type is tuple_type:
2307 function = "__Pyx_GetItemInt_Tuple"
2309 function = "__Pyx_GetItemInt"
2310 code.globalstate.use_utility_code(getitem_int_utility_code)
2312 index_code = self.index.py_result()
2313 if self.base.type is dict_type:
2314 function = "__Pyx_PyDict_GetItem"
2315 code.globalstate.use_utility_code(getitem_dict_utility_code)
2317 function = "PyObject_GetItem"
2319 "%s = %s(%s, %s%s); if (!%s) %s" % (
2322 self.base.py_result(),
2324 self.extra_index_params(),
2326 code.error_goto(self.pos)))
2327 code.put_gotref(self.py_result())
2328 elif self.type is PyrexTypes.c_py_unicode_type and self.base.type is unicode_type:
2329 assert self.index.type.is_int
2330 index_code = self.index.result()
2331 function = "__Pyx_GetItemInt_Unicode"
2332 code.globalstate.use_utility_code(getitem_int_pyunicode_utility_code)
2334 "%s = %s(%s, %s%s); if (unlikely(%s == (Py_UNICODE)-1)) %s;" % (
2337 self.base.py_result(),
2339 self.extra_index_params(),
2341 code.error_goto(self.pos)))
2343 def generate_setitem_code(self, value_code, code):
2344 if self.index.type.is_int:
2345 function = "__Pyx_SetItemInt"
2346 index_code = self.index.result()
2347 code.globalstate.use_utility_code(setitem_int_utility_code)
2349 index_code = self.index.py_result()
2350 if self.base.type is dict_type:
2351 function = "PyDict_SetItem"
2352 # It would seem that we could specialized lists/tuples, but that
2353 # shouldn't happen here.
2354 # Both PyList_SetItem PyTuple_SetItem and a Py_ssize_t as input,
2355 # not a PyObject*, and bad conversion here would give the wrong
2356 # exception. Also, tuples are supposed to be immutable, and raise
2357 # TypeErrors when trying to set their entries (PyTuple_SetItem
2358 # is for creating new tuples from).
2360 function = "PyObject_SetItem"
2362 "if (%s(%s, %s, %s%s) < 0) %s" % (
2364 self.base.py_result(),
2367 self.extra_index_params(),
2368 code.error_goto(self.pos)))
2370 def generate_buffer_setitem_code(self, rhs, code, op=""):
2371 # Used from generate_assignment_code and InPlaceAssignmentNode
2372 if code.globalstate.directives['nonecheck']:
2373 self.put_nonecheck(code)
2374 ptrexpr = self.buffer_lookup_code(code)
2375 if self.buffer_type.dtype.is_pyobject:
2376 # Must manage refcounts. Decref what is already there
2377 # and incref what we put in.
2378 ptr = code.funcstate.allocate_temp(self.buffer_type.buffer_ptr_type, manage_ref=False)
2379 rhs_code = rhs.result()
2380 code.putln("%s = %s;" % (ptr, ptrexpr))
2381 code.put_gotref("*%s" % ptr)
2382 code.putln("__Pyx_DECREF(*%s); __Pyx_INCREF(%s);" % (
2385 code.putln("*%s %s= %s;" % (ptr, op, rhs_code))
2386 code.put_giveref("*%s" % ptr)
2387 code.funcstate.release_temp(ptr)
2390 code.putln("*%s %s= %s;" % (ptrexpr, op, rhs.result()))
2392 def generate_assignment_code(self, rhs, code):
2393 self.generate_subexpr_evaluation_code(code)
2394 if self.is_buffer_access:
2395 self.generate_buffer_setitem_code(rhs, code)
2396 elif self.type.is_pyobject:
2397 self.generate_setitem_code(rhs.py_result(), code)
2401 self.result(), rhs.result()))
2402 self.generate_subexpr_disposal_code(code)
2403 self.free_subexpr_temps(code)
2404 rhs.generate_disposal_code(code)
2405 rhs.free_temps(code)
2407 def generate_deletion_code(self, code):
2408 self.generate_subexpr_evaluation_code(code)
2409 #if self.type.is_pyobject:
2410 if self.index.type.is_int:
2411 function = "__Pyx_DelItemInt"
2412 index_code = self.index.result()
2413 code.globalstate.use_utility_code(delitem_int_utility_code)
2415 index_code = self.index.py_result()
2416 if self.base.type is dict_type:
2417 function = "PyDict_DelItem"
2419 function = "PyObject_DelItem"
2421 "if (%s(%s, %s%s) < 0) %s" % (
2423 self.base.py_result(),
2425 self.extra_index_params(),
2426 code.error_goto(self.pos)))
2427 self.generate_subexpr_disposal_code(code)
2428 self.free_subexpr_temps(code)
2430 def buffer_lookup_code(self, code):
2431 # Assign indices to temps
2432 index_temps = [code.funcstate.allocate_temp(i.type, manage_ref=False) for i in self.indices]
2433 for temp, index in zip(index_temps, self.indices):
2434 code.putln("%s = %s;" % (temp, index.result()))
2435 # Generate buffer access code using these temps
2437 # The above could happen because child_attrs is wrong somewhere so that
2438 # options are not propagated.
2439 return Buffer.put_buffer_lookup_code(entry=self.base.entry,
2440 index_signeds=[i.type.signed for i in self.indices],
2441 index_cnames=index_temps,
2442 directives=code.globalstate.directives,
2443 pos=self.pos, code=code)
2445 def put_nonecheck(self, code):
2446 code.globalstate.use_utility_code(raise_noneindex_error_utility_code)
2447 code.putln("if (%s) {" % code.unlikely("%s == Py_None") % self.base.result_as(PyrexTypes.py_object_type))
2448 code.putln("__Pyx_RaiseNoneIndexingError();")
2449 code.putln(code.error_goto(self.pos))
2452 class SliceIndexNode(ExprNode):
2453 # 2-element slice indexing
2456 # start ExprNode or None
2457 # stop ExprNode or None
2459 subexprs = ['base', 'start', 'stop']
2461 def infer_type(self, env):
2462 base_type = self.base.infer_type(env)
2463 if base_type.is_string:
2465 elif base_type in (bytes_type, str_type, unicode_type,
2466 list_type, tuple_type):
2468 return py_object_type
2470 def calculate_constant_result(self):
2471 self.constant_result = self.base.constant_result[
2472 self.start.constant_result : self.stop.constant_result]
2474 def compile_time_value(self, denv):
2475 base = self.base.compile_time_value(denv)
2476 if self.start is None:
2479 start = self.start.compile_time_value(denv)
2480 if self.stop is None:
2483 stop = self.stop.compile_time_value(denv)
2485 return base[start:stop]
2486 except Exception, e:
2487 self.compile_time_value_error(e)
2489 def analyse_target_declaration(self, env):
2492 def analyse_target_types(self, env):
2493 self.analyse_types(env)
2494 # when assigning, we must accept any Python type
2495 if self.type.is_pyobject:
2496 self.type = py_object_type
2498 def analyse_types(self, env):
2499 self.base.analyse_types(env)
2501 self.start.analyse_types(env)
2503 self.stop.analyse_types(env)
2504 base_type = self.base.type
2505 if base_type.is_string:
2506 self.type = bytes_type
2507 elif base_type.is_ptr:
2508 self.type = base_type
2509 elif base_type.is_array:
2510 # we need a ptr type here instead of an array type, as
2511 # array types can result in invalid type casts in the C
2513 self.type = PyrexTypes.CPtrType(base_type.base_type)
2515 self.base = self.base.coerce_to_pyobject(env)
2516 self.type = py_object_type
2517 if base_type.is_builtin_type:
2518 # slicing builtin types returns something of the same type
2519 self.type = base_type
2520 c_int = PyrexTypes.c_py_ssize_t_type
2522 self.start = self.start.coerce_to(c_int, env)
2524 self.stop = self.stop.coerce_to(c_int, env)
2527 nogil_check = Node.gil_error
2528 gil_message = "Slicing Python object"
2530 def generate_result_code(self, code):
2531 if not self.type.is_pyobject:
2533 "Slicing is not currently supported for '%s'." % self.type)
2535 if self.base.type.is_string:
2536 if self.stop is None:
2538 "%s = PyBytes_FromString(%s + %s); %s" % (
2542 code.error_goto_if_null(self.result(), self.pos)))
2545 "%s = PyBytes_FromStringAndSize(%s + %s, %s - %s); %s" % (
2551 code.error_goto_if_null(self.result(), self.pos)))
2554 "%s = __Pyx_PySequence_GetSlice(%s, %s, %s); %s" % (
2556 self.base.py_result(),
2559 code.error_goto_if_null(self.result(), self.pos)))
2560 code.put_gotref(self.py_result())
2562 def generate_assignment_code(self, rhs, code):
2563 self.generate_subexpr_evaluation_code(code)
2564 if self.type.is_pyobject:
2565 code.put_error_if_neg(self.pos,
2566 "__Pyx_PySequence_SetSlice(%s, %s, %s, %s)" % (
2567 self.base.py_result(),
2574 start_offset = self.start_code()
2575 if start_offset == '0':
2579 if rhs.type.is_array:
2580 array_length = rhs.type.size
2581 self.generate_slice_guard_code(code, array_length)
2584 "Slice assignments from pointers are not yet supported.")
2585 # FIXME: fix the array size according to start/stop
2586 array_length = self.base.type.size
2587 for i in range(array_length):
2588 code.putln("%s[%s%s] = %s[%d];" % (
2589 self.base.result(), start_offset, i,
2591 self.generate_subexpr_disposal_code(code)
2592 self.free_subexpr_temps(code)
2593 rhs.generate_disposal_code(code)
2594 rhs.free_temps(code)
2596 def generate_deletion_code(self, code):
2597 if not self.base.type.is_pyobject:
2599 "Deleting slices is only supported for Python types, not '%s'." % self.type)
2601 self.generate_subexpr_evaluation_code(code)
2602 code.put_error_if_neg(self.pos,
2603 "__Pyx_PySequence_DelSlice(%s, %s, %s)" % (
2604 self.base.py_result(),
2607 self.generate_subexpr_disposal_code(code)
2608 self.free_subexpr_temps(code)
2610 def generate_slice_guard_code(self, code, target_size):
2611 if not self.base.type.is_array:
2613 slice_size = self.base.type.size
2616 stop = self.stop.result()
2620 slice_size = self.base.type.size + stop
2627 start = self.start.result()
2631 start = self.base.type.size + start
2639 error(self.pos, "Assignment to empty slice.")
2640 elif start is None and stop is None:
2641 # we know the exact slice length
2642 if target_size != slice_size:
2643 error(self.pos, "Assignment to slice of wrong length, expected %d, got %d" % (
2644 slice_size, target_size))
2645 elif start is not None:
2648 check = "(%s)-(%s)" % (stop, start)
2649 else: # stop is not None:
2652 code.putln("if (unlikely((%s) != %d)) {" % (check, target_size))
2653 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));' % (
2654 target_size, check))
2655 code.putln(code.error_goto(self.pos))
2658 def start_code(self):
2660 return self.start.result()
2664 def stop_code(self):
2666 return self.stop.result()
2667 elif self.base.type.is_array:
2668 return self.base.type.size
2670 return "PY_SSIZE_T_MAX"
2672 def calculate_result_code(self):
2673 # self.result() is not used, but this method must exist
2677 class SliceNode(ExprNode):
2678 # start:stop:step in subscript list
2684 type = py_object_type
2687 def calculate_constant_result(self):
2688 self.constant_result = self.base.constant_result[
2689 self.start.constant_result : \
2690 self.stop.constant_result : \
2691 self.step.constant_result]
2693 def compile_time_value(self, denv):
2694 start = self.start.compile_time_value(denv)
2695 if self.stop is None:
2698 stop = self.stop.compile_time_value(denv)
2699 if self.step is None:
2702 step = self.step.compile_time_value(denv)
2704 return slice(start, stop, step)
2705 except Exception, e:
2706 self.compile_time_value_error(e)
2708 subexprs = ['start', 'stop', 'step']
2710 def analyse_types(self, env):
2711 self.start.analyse_types(env)
2712 self.stop.analyse_types(env)
2713 self.step.analyse_types(env)
2714 self.start = self.start.coerce_to_pyobject(env)
2715 self.stop = self.stop.coerce_to_pyobject(env)
2716 self.step = self.step.coerce_to_pyobject(env)
2718 gil_message = "Constructing Python slice object"
2720 def generate_result_code(self, code):
2722 "%s = PySlice_New(%s, %s, %s); %s" % (
2724 self.start.py_result(),
2725 self.stop.py_result(),
2726 self.step.py_result(),
2727 code.error_goto_if_null(self.result(), self.pos)))
2728 code.put_gotref(self.py_result())
2731 class CallNode(ExprNode):
2733 # allow overriding the default 'may_be_none' behaviour
2734 may_return_none = None
2736 def may_be_none(self):
2737 if self.may_return_none is not None:
2738 return self.may_return_none
2739 return ExprNode.may_be_none(self)
2741 def analyse_as_type_constructor(self, env):
2742 type = self.function.analyse_as_type(env)
2743 if type and type.is_struct_or_union:
2744 args, kwds = self.explicit_args_kwds()
2746 for arg, member in zip(args, type.scope.var_entries):
2747 items.append(DictItemNode(pos=arg.pos, key=StringNode(pos=arg.pos, value=member.name), value=arg))
2749 items += kwds.key_value_pairs
2750 self.key_value_pairs = items
2751 self.__class__ = DictNode
2752 self.analyse_types(env)
2753 self.coerce_to(type, env)
2755 elif type and type.is_cpp_class:
2756 for arg in self.args:
2757 arg.analyse_types(env)
2758 constructor = type.scope.lookup("<init>")
2759 self.function = RawCNameExprNode(self.function.pos, constructor.type)
2760 self.function.entry = constructor
2761 self.function.set_cname(type.declaration_code(""))
2762 self.analyse_c_function_call(env)
2765 def is_lvalue(self):
2766 return self.type.is_reference
2768 def nogil_check(self, env):
2769 func_type = self.function_type()
2770 if func_type.is_pyobject:
2772 elif not getattr(func_type, 'nogil', False):
2775 gil_message = "Calling gil-requiring function"
2778 class SimpleCallNode(CallNode):
2779 # Function call without keyword, * or ** args.
2783 # arg_tuple ExprNode or None used internally
2784 # self ExprNode or None used internally
2785 # coerced_self ExprNode or None used internally
2786 # wrapper_call bool used internally
2787 # has_optional_args bool used internally
2788 # nogil bool used internally
2790 subexprs = ['self', 'coerced_self', 'function', 'args', 'arg_tuple']
2795 wrapper_call = False
2796 has_optional_args = False
2800 def compile_time_value(self, denv):
2801 function = self.function.compile_time_value(denv)
2802 args = [arg.compile_time_value(denv) for arg in self.args]
2804 return function(*args)
2805 except Exception, e:
2806 self.compile_time_value_error(e)
2808 def type_dependencies(self, env):
2809 # TODO: Update when Danilo's C++ code merged in to handle the
2810 # the case of function overloading.
2811 return self.function.type_dependencies(env)
2813 def infer_type(self, env):
2814 function = self.function
2815 func_type = function.infer_type(env)
2816 if isinstance(self.function, NewExprNode):
2817 return PyrexTypes.CPtrType(self.function.class_type)
2818 if func_type.is_ptr:
2819 func_type = func_type.base_type
2820 if func_type.is_cfunction:
2821 return func_type.return_type
2822 elif func_type is type_type:
2823 if function.is_name and function.entry and function.entry.type:
2824 result_type = function.entry.type
2825 if result_type.is_extension_type:
2827 elif result_type.is_builtin_type:
2828 if function.entry.name == 'float':
2829 return PyrexTypes.c_double_type
2830 elif function.entry.name in Builtin.types_that_construct_their_instance:
2832 return py_object_type
2834 def analyse_as_type(self, env):
2835 attr = self.function.as_cython_attribute()
2836 if attr == 'pointer':
2837 if len(self.args) != 1:
2838 error(self.args.pos, "only one type allowed.")
2840 type = self.args[0].analyse_as_type(env)
2842 error(self.args[0].pos, "Unknown type")
2844 return PyrexTypes.CPtrType(type)
2846 def explicit_args_kwds(self):
2847 return self.args, None
2849 def analyse_types(self, env):
2850 if self.analyse_as_type_constructor(env):
2854 self.analysed = True
2855 function = self.function
2856 function.is_called = 1
2857 self.function.analyse_types(env)
2858 if function.is_attribute and function.entry and function.entry.is_cmethod:
2859 # Take ownership of the object from which the attribute
2860 # was obtained, because we need to pass it as 'self'.
2861 self.self = function.obj
2862 function.obj = CloneNode(self.self)
2863 func_type = self.function_type()
2864 if func_type.is_pyobject:
2865 self.arg_tuple = TupleNode(self.pos, args = self.args)
2866 self.arg_tuple.analyse_types(env)
2868 if func_type is Builtin.type_type and function.is_name and \
2869 function.entry and \
2870 function.entry.is_builtin and \
2871 function.entry.name in Builtin.types_that_construct_their_instance:
2872 # calling a builtin type that returns a specific object type
2873 if function.entry.name == 'float':
2874 # the following will come true later on in a transform
2875 self.type = PyrexTypes.c_double_type
2876 self.result_ctype = PyrexTypes.c_double_type
2878 self.type = Builtin.builtin_types[function.entry.name]
2879 self.result_ctype = py_object_type
2880 self.may_return_none = False
2881 elif function.is_name and function.type_entry:
2882 # We are calling an extension type constructor. As
2883 # long as we do not support __new__(), the result type
2885 self.type = function.type_entry.type
2886 self.result_ctype = py_object_type
2887 self.may_return_none = False
2889 self.type = py_object_type
2892 for arg in self.args:
2893 arg.analyse_types(env)
2894 if self.self and func_type.args:
2895 # Coerce 'self' to the type expected by the method.
2896 self_arg = func_type.args[0]
2897 if self_arg.not_none: # C methods must do the None test for self at *call* time
2898 self.self = self.self.as_none_safe_node(
2899 "'NoneType' object has no attribute '%s'" % self.function.entry.name,
2900 'PyExc_AttributeError')
2901 expected_type = self_arg.type
2902 self.coerced_self = CloneNode(self.self).coerce_to(
2904 # Insert coerced 'self' argument into argument list.
2905 self.args.insert(0, self.coerced_self)
2906 self.analyse_c_function_call(env)
2908 def function_type(self):
2909 # Return the type of the function being called, coercing a function
2910 # pointer to a function if necessary.
2911 func_type = self.function.type
2912 if func_type.is_ptr:
2913 func_type = func_type.base_type
2916 def analyse_c_function_call(self, env):
2917 if self.function.type is error_type:
2918 self.type = error_type
2920 if self.function.type.is_cpp_class:
2921 overloaded_entry = self.function.type.scope.lookup("operator()")
2922 if overloaded_entry is None:
2923 self.type = PyrexTypes.error_type
2924 self.result_code = "<error>"
2926 elif hasattr(self.function, 'entry'):
2927 overloaded_entry = self.function.entry
2929 overloaded_entry = None
2930 if overloaded_entry:
2931 entry = PyrexTypes.best_match(self.args, overloaded_entry.all_alternatives(), self.pos)
2933 self.type = PyrexTypes.error_type
2934 self.result_code = "<error>"
2936 self.function.entry = entry
2937 self.function.type = entry.type
2938 func_type = self.function_type()
2940 func_type = self.function_type()
2941 if not func_type.is_cfunction:
2942 error(self.pos, "Calling non-function type '%s'" % func_type)
2943 self.type = PyrexTypes.error_type
2944 self.result_code = "<error>"
2947 max_nargs = len(func_type.args)
2948 expected_nargs = max_nargs - func_type.optional_arg_count
2949 actual_nargs = len(self.args)
2950 if func_type.optional_arg_count and expected_nargs != actual_nargs:
2951 self.has_optional_args = 1
2954 for i in range(min(max_nargs, actual_nargs)):
2955 formal_type = func_type.args[i].type
2956 arg = self.args[i].coerce_to(formal_type, env)
2957 if arg.type.is_pyobject and not env.nogil and (arg.is_attribute or not arg.is_simple):
2958 # we do not own the argument's reference, but we must
2959 # make sure it cannot be collected before we return
2960 # from the function, so we create an owned temp
2962 arg = arg.coerce_to_temp(env)
2964 for i in range(max_nargs, actual_nargs):
2966 if arg.type.is_pyobject:
2967 arg_ctype = arg.type.default_coerced_ctype()
2968 if arg_ctype is None:
2969 error(self.args[i].pos,
2970 "Python object cannot be passed as a varargs parameter")
2972 self.args[i] = arg.coerce_to(arg_ctype, env)
2973 # Calc result type and code fragment
2974 if isinstance(self.function, NewExprNode):
2975 self.type = PyrexTypes.CPtrType(self.function.class_type)
2977 self.type = func_type.return_type
2978 if self.type.is_pyobject:
2979 self.result_ctype = py_object_type
2981 elif func_type.exception_value is not None \
2982 or func_type.exception_check:
2984 # Called in 'nogil' context?
2985 self.nogil = env.nogil
2987 func_type.exception_check and
2988 func_type.exception_check != '+'):
2989 env.use_utility_code(pyerr_occurred_withgil_utility_code)
2990 # C++ exception handler
2991 if func_type.exception_check == '+':
2992 if func_type.exception_value is None:
2993 env.use_utility_code(cpp_exception_utility_code)
2995 def calculate_result_code(self):
2996 return self.c_call_code()
2998 def c_call_code(self):
2999 func_type = self.function_type()
3000 if self.type is PyrexTypes.error_type or not func_type.is_cfunction:
3002 formal_args = func_type.args
3004 args = list(zip(formal_args, self.args))
3005 max_nargs = len(func_type.args)
3006 expected_nargs = max_nargs - func_type.optional_arg_count
3007 actual_nargs = len(self.args)
3008 for formal_arg, actual_arg in args[:expected_nargs]:
3009 arg_code = actual_arg.result_as(formal_arg.type)
3010 arg_list_code.append(arg_code)
3012 if func_type.is_overridable:
3013 arg_list_code.append(str(int(self.wrapper_call or self.function.entry.is_unbound_cmethod)))
3015 if func_type.optional_arg_count:
3016 if expected_nargs == actual_nargs:
3017 optional_args = 'NULL'
3019 optional_args = "&%s" % self.opt_arg_struct
3020 arg_list_code.append(optional_args)
3022 for actual_arg in self.args[len(formal_args):]:
3023 arg_list_code.append(actual_arg.result())
3024 result = "%s(%s)" % (self.function.result(),
3025 ', '.join(arg_list_code))
3028 def generate_result_code(self, code):
3029 func_type = self.function_type()
3030 if func_type.is_pyobject:
3031 arg_code = self.arg_tuple.py_result()
3033 "%s = PyObject_Call(%s, %s, NULL); %s" % (
3035 self.function.py_result(),
3037 code.error_goto_if_null(self.result(), self.pos)))
3038 code.put_gotref(self.py_result())
3039 elif func_type.is_cfunction:
3040 if self.has_optional_args:
3041 actual_nargs = len(self.args)
3042 expected_nargs = len(func_type.args) - func_type.optional_arg_count
3043 self.opt_arg_struct = code.funcstate.allocate_temp(
3044 func_type.op_arg_struct.base_type, manage_ref=True)
3045 code.putln("%s.%s = %s;" % (
3046 self.opt_arg_struct,
3047 Naming.pyrex_prefix + "n",
3048 len(self.args) - expected_nargs))
3049 args = list(zip(func_type.args, self.args))
3050 for formal_arg, actual_arg in args[expected_nargs:actual_nargs]:
3051 code.putln("%s.%s = %s;" % (
3052 self.opt_arg_struct,
3053 func_type.opt_arg_cname(formal_arg.name),
3054 actual_arg.result_as(formal_arg.type)))
3056 if self.type.is_pyobject and self.is_temp:
3057 exc_checks.append("!%s" % self.result())
3059 exc_val = func_type.exception_value
3060 exc_check = func_type.exception_check
3061 if exc_val is not None:
3062 exc_checks.append("%s == %s" % (self.result(), exc_val))
3065 exc_checks.append("__Pyx_ErrOccurredWithGIL()")
3067 exc_checks.append("PyErr_Occurred()")
3068 if self.is_temp or exc_checks:
3069 rhs = self.c_call_code()
3071 lhs = "%s = " % self.result()
3072 if self.is_temp and self.type.is_pyobject:
3073 #return_type = self.type # func_type.return_type
3074 #print "SimpleCallNode.generate_result_code: casting", rhs, \
3075 # "from", return_type, "to pyobject" ###
3076 rhs = typecast(py_object_type, self.type, rhs)
3079 if func_type.exception_check == '+':
3080 if func_type.exception_value is None:
3081 raise_py_exception = "__Pyx_CppExn2PyErr()"
3082 elif func_type.exception_value.type.is_pyobject:
3083 raise_py_exception = ' try { throw; } catch(const std::exception& exn) { PyErr_SetString(%s, exn.what()); } catch(...) { PyErr_SetNone(%s); }' % (
3084 func_type.exception_value.entry.cname,
3085 func_type.exception_value.entry.cname)
3087 raise_py_exception = '%s(); if (!PyErr_Occurred()) PyErr_SetString(PyExc_RuntimeError , "Error converting c++ exception.")' % func_type.exception_value.entry.cname
3089 raise_py_exception = 'Py_BLOCK_THREADS; %s; Py_UNBLOCK_THREADS' % raise_py_exception
3091 "try {%s%s;} catch(...) {%s; %s}" % (
3095 code.error_goto(self.pos)))
3098 goto_error = code.error_goto_if(" && ".join(exc_checks), self.pos)
3101 code.putln("%s%s; %s" % (lhs, rhs, goto_error))
3102 if self.type.is_pyobject and self.result():
3103 code.put_gotref(self.py_result())
3104 if self.has_optional_args:
3105 code.funcstate.release_temp(self.opt_arg_struct)
3108 class PythonCapiFunctionNode(ExprNode):
3110 def __init__(self, pos, py_name, cname, func_type, utility_code = None):
3114 self.type = func_type
3115 self.utility_code = utility_code
3117 def analyse_types(self, env):
3120 def generate_result_code(self, code):
3121 if self.utility_code:
3122 code.globalstate.use_utility_code(self.utility_code)
3124 def calculate_result_code(self):
3127 class PythonCapiCallNode(SimpleCallNode):
3128 # Python C-API Function call (only created in transforms)
3130 # By default, we assume that the call never returns None, as this
3131 # is true for most C-API functions in CPython. If this does not
3132 # apply to a call, set the following to True (or None to inherit
3133 # the default behaviour).
3134 may_return_none = False
3136 def __init__(self, pos, function_name, func_type,
3137 utility_code = None, py_name=None, **kwargs):
3138 self.type = func_type.return_type
3139 self.result_ctype = self.type
3140 self.function = PythonCapiFunctionNode(
3141 pos, py_name, function_name, func_type,
3142 utility_code = utility_code)
3143 # call this last so that we can override the constructed
3144 # attributes above with explicit keyword arguments if required
3145 SimpleCallNode.__init__(self, pos, **kwargs)
3148 class GeneralCallNode(CallNode):
3149 # General Python function call, including keyword,
3150 # * and ** arguments.
3153 # positional_args ExprNode Tuple of positional arguments
3154 # keyword_args ExprNode or None Dict of keyword arguments
3155 # starstar_arg ExprNode or None Dict of extra keyword args
3157 type = py_object_type
3159 subexprs = ['function', 'positional_args', 'keyword_args', 'starstar_arg']
3161 nogil_check = Node.gil_error
3163 def compile_time_value(self, denv):
3164 function = self.function.compile_time_value(denv)
3165 positional_args = self.positional_args.compile_time_value(denv)
3166 keyword_args = self.keyword_args.compile_time_value(denv)
3167 starstar_arg = self.starstar_arg.compile_time_value(denv)
3169 keyword_args.update(starstar_arg)
3170 return function(*positional_args, **keyword_args)
3171 except Exception, e:
3172 self.compile_time_value_error(e)
3174 def explicit_args_kwds(self):
3175 if self.starstar_arg or not isinstance(self.positional_args, TupleNode):
3176 raise CompileError(self.pos,
3177 'Compile-time keyword arguments must be explicit.')
3178 return self.positional_args.args, self.keyword_args
3180 def analyse_types(self, env):
3181 if self.analyse_as_type_constructor(env):
3183 self.function.analyse_types(env)
3184 self.positional_args.analyse_types(env)
3185 if self.keyword_args:
3186 self.keyword_args.analyse_types(env)
3187 if self.starstar_arg:
3188 self.starstar_arg.analyse_types(env)
3189 if not self.function.type.is_pyobject:
3190 if self.function.type.is_error:
3191 self.type = error_type
3193 if hasattr(self.function, 'entry') and not self.function.entry.as_variable:
3194 error(self.pos, "Keyword and starred arguments not allowed in cdef functions.")
3196 self.function = self.function.coerce_to_pyobject(env)
3197 self.positional_args = \
3198 self.positional_args.coerce_to_pyobject(env)
3199 if self.starstar_arg:
3200 self.starstar_arg = \
3201 self.starstar_arg.coerce_to_pyobject(env)
3202 function = self.function
3203 if function.is_name and function.type_entry:
3204 # We are calling an extension type constructor. As long
3205 # as we do not support __new__(), the result type is clear
3206 self.type = function.type_entry.type
3207 self.result_ctype = py_object_type
3208 self.may_return_none = False
3210 self.type = py_object_type
3213 def generate_result_code(self, code):
3214 if self.type.is_error: return
3215 kwargs_call_function = "PyEval_CallObjectWithKeywords"
3216 if self.keyword_args and self.starstar_arg:
3217 code.put_error_if_neg(self.pos,
3218 "PyDict_Update(%s, %s)" % (
3219 self.keyword_args.py_result(),
3220 self.starstar_arg.py_result()))
3221 keyword_code = self.keyword_args.py_result()
3222 elif self.keyword_args:
3223 keyword_code = self.keyword_args.py_result()
3224 elif self.starstar_arg:
3225 keyword_code = self.starstar_arg.py_result()
3226 if self.starstar_arg.type is not Builtin.dict_type:
3227 # CPython supports calling functions with non-dicts, so do we
3228 code.globalstate.use_utility_code(kwargs_call_utility_code)
3229 kwargs_call_function = "__Pyx_PyEval_CallObjectWithKeywords"
3232 if not keyword_code:
3233 call_code = "PyObject_Call(%s, %s, NULL)" % (
3234 self.function.py_result(),
3235 self.positional_args.py_result())
3237 call_code = "%s(%s, %s, %s)" % (
3238 kwargs_call_function,
3239 self.function.py_result(),
3240 self.positional_args.py_result(),
3246 code.error_goto_if_null(self.result(), self.pos)))
3247 code.put_gotref(self.py_result())
3250 class AsTupleNode(ExprNode):
3251 # Convert argument to tuple. Used for normalising
3252 # the * argument of a function call.
3258 def calculate_constant_result(self):
3259 self.constant_result = tuple(self.base.constant_result)
3261 def compile_time_value(self, denv):
3262 arg = self.arg.compile_time_value(denv)
3265 except Exception, e:
3266 self.compile_time_value_error(e)
3268 def analyse_types(self, env):
3269 self.arg.analyse_types(env)
3270 self.arg = self.arg.coerce_to_pyobject(env)
3271 self.type = tuple_type
3274 def may_be_none(self):
3277 nogil_check = Node.gil_error
3278 gil_message = "Constructing Python tuple"
3280 def generate_result_code(self, code):
3282 "%s = PySequence_Tuple(%s); %s" % (
3284 self.arg.py_result(),
3285 code.error_goto_if_null(self.result(), self.pos)))
3286 code.put_gotref(self.py_result())
3289 class AttributeNode(ExprNode):
3294 # needs_none_check boolean Used if obj is an extension type.
3295 # If set to True, it is known that the type is not None.
3299 # is_py_attr boolean Is a Python getattr operation
3300 # member string C name of struct member
3301 # is_called boolean Function call is being done on result
3302 # entry Entry Symbol table entry of attribute
3307 type = PyrexTypes.error_type
3310 needs_none_check = True
3312 def as_cython_attribute(self):
3313 if isinstance(self.obj, NameNode) and self.obj.is_cython_module:
3314 return self.attribute
3315 cy = self.obj.as_cython_attribute()
3317 return "%s.%s" % (cy, self.attribute)
3319 def coerce_to(self, dst_type, env):
3320 # If coercing to a generic pyobject and this is a cpdef function
3321 # we can create the corresponding attribute
3322 if dst_type is py_object_type:
3324 if entry and entry.is_cfunction and entry.as_variable:
3325 # must be a cpdef function
3327 self.entry = entry.as_variable
3328 self.analyse_as_python_attribute(env)
3330 return ExprNode.coerce_to(self, dst_type, env)
3332 def calculate_constant_result(self):
3333 attr = self.attribute
3334 if attr.startswith("__") and attr.endswith("__"):
3336 self.constant_result = getattr(self.obj.constant_result, attr)
3338 def compile_time_value(self, denv):
3339 attr = self.attribute
3340 if attr.startswith("__") and attr.endswith("__"):
3342 "Invalid attribute name '%s' in compile-time expression" % attr)
3344 obj = self.obj.compile_time_value(denv)
3346 return getattr(obj, attr)
3347 except Exception, e:
3348 self.compile_time_value_error(e)
3350 def type_dependencies(self, env):
3351 return self.obj.type_dependencies(env)
3353 def infer_type(self, env):
3354 if self.analyse_as_cimported_attribute(env, 0):
3355 return self.entry.type
3356 elif self.analyse_as_unbound_cmethod(env):
3357 return self.entry.type
3359 self.analyse_attribute(env, obj_type = self.obj.infer_type(env))
3362 def analyse_target_declaration(self, env):
3365 def analyse_target_types(self, env):
3366 self.analyse_types(env, target = 1)
3368 def analyse_types(self, env, target = 0):
3369 if self.analyse_as_cimported_attribute(env, target):
3371 if not target and self.analyse_as_unbound_cmethod(env):
3373 self.analyse_as_ordinary_attribute(env, target)
3375 def analyse_as_cimported_attribute(self, env, target):
3376 # Try to interpret this as a reference to an imported
3377 # C const, type, var or function. If successful, mutates
3378 # this node into a NameNode and returns 1, otherwise
3380 module_scope = self.obj.analyse_as_module(env)
3382 entry = module_scope.lookup_here(self.attribute)
3384 entry.is_cglobal or entry.is_cfunction
3385 or entry.is_type or entry.is_const):
3386 self.mutate_into_name_node(env, entry, target)
3390 def analyse_as_unbound_cmethod(self, env):
3391 # Try to interpret this as a reference to an unbound
3392 # C method of an extension type. If successful, mutates
3393 # this node into a NameNode and returns 1, otherwise
3395 type = self.obj.analyse_as_extension_type(env)
3397 entry = type.scope.lookup_here(self.attribute)
3398 if entry and entry.is_cmethod:
3399 # Create a temporary entry describing the C method
3400 # as an ordinary function.
3401 ubcm_entry = Symtab.Entry(entry.name,
3402 "%s->%s" % (type.vtabptr_cname, entry.cname),
3404 ubcm_entry.is_cfunction = 1
3405 ubcm_entry.func_cname = entry.func_cname
3406 ubcm_entry.is_unbound_cmethod = 1
3407 self.mutate_into_name_node(env, ubcm_entry, None)
3411 def analyse_as_type(self, env):
3412 module_scope = self.obj.analyse_as_module(env)
3414 return module_scope.lookup_type(self.attribute)
3415 if not isinstance(self.obj, (UnicodeNode, StringNode, BytesNode)):
3416 base_type = self.obj.analyse_as_type(env)
3417 if base_type and hasattr(base_type, 'scope') and base_type.scope is not None:
3418 return base_type.scope.lookup_type(self.attribute)
3421 def analyse_as_extension_type(self, env):
3422 # Try to interpret this as a reference to an extension type
3423 # in a cimported module. Returns the extension type, or None.
3424 module_scope = self.obj.analyse_as_module(env)
3426 entry = module_scope.lookup_here(self.attribute)
3427 if entry and entry.is_type and entry.type.is_extension_type:
3431 def analyse_as_module(self, env):
3432 # Try to interpret this as a reference to a cimported module
3433 # in another cimported module. Returns the module scope, or None.
3434 module_scope = self.obj.analyse_as_module(env)
3436 entry = module_scope.lookup_here(self.attribute)
3437 if entry and entry.as_module:
3438 return entry.as_module
3441 def mutate_into_name_node(self, env, entry, target):
3442 # Mutate this node into a NameNode and complete the
3443 # analyse_types phase.
3444 self.__class__ = NameNode
3445 self.name = self.attribute
3450 NameNode.analyse_target_types(self, env)
3452 NameNode.analyse_rvalue_entry(self, env)
3454 def analyse_as_ordinary_attribute(self, env, target):
3455 self.obj.analyse_types(env)
3456 self.analyse_attribute(env)
3457 if self.entry and self.entry.is_cmethod and not self.is_called:
3458 # error(self.pos, "C method can only be called")
3460 ## Reference to C array turns into pointer to first element.
3461 #while self.type.is_array:
3462 # self.type = self.type.element_ptr_type()
3466 self.result_ctype = py_object_type
3467 elif target and self.obj.type.is_builtin_type:
3468 error(self.pos, "Assignment to an immutable object field")
3470 def analyse_attribute(self, env, obj_type = None):
3471 # Look up attribute and set self.type and self.member.
3473 self.member = self.attribute
3474 if obj_type is None:
3475 if self.obj.type.is_string:
3476 self.obj = self.obj.coerce_to_pyobject(env)
3477 obj_type = self.obj.type
3479 if obj_type.is_string:
3480 obj_type = py_object_type
3481 if obj_type.is_ptr or obj_type.is_array:
3482 obj_type = obj_type.base_type
3484 elif obj_type.is_extension_type or obj_type.is_builtin_type:
3488 if obj_type.has_attributes:
3490 if obj_type.attributes_known():
3491 entry = obj_type.scope.lookup_here(self.attribute)
3492 if entry and entry.is_member:
3496 "Cannot select attribute of incomplete type '%s'"
3498 self.type = PyrexTypes.error_type
3502 if obj_type.is_extension_type and entry.name == "__weakref__":
3503 error(self.pos, "Illegal use of special attribute __weakref__")
3504 # methods need the normal attribute lookup
3505 # because they do not have struct entries
3506 if entry.is_variable or entry.is_cmethod:
3507 self.type = entry.type
3508 self.member = entry.cname
3511 # If it's not a variable or C method, it must be a Python
3512 # method of an extension type, so we treat it like a Python
3515 # If we get here, the base object is not a struct/union/extension
3516 # type, or it is an extension type and the attribute is either not
3517 # declared or is declared as a Python method. Treat it as a Python
3518 # attribute reference.
3519 self.analyse_as_python_attribute(env, obj_type)
3521 def analyse_as_python_attribute(self, env, obj_type = None):
3522 if obj_type is None:
3523 obj_type = self.obj.type
3524 self.member = self.attribute
3525 self.type = py_object_type
3527 if not obj_type.is_pyobject and not obj_type.is_error:
3528 if obj_type.can_coerce_to_pyobject(env):
3529 self.obj = self.obj.coerce_to_pyobject(env)
3532 "Object of type '%s' has no attribute '%s'" %
3533 (obj_type, self.attribute))
3535 def nogil_check(self, env):
3539 gil_message = "Accessing Python attribute"
3541 def is_simple(self):
3543 return self.result_in_temp() or self.obj.is_simple()
3545 return NameNode.is_simple(self)
3547 def is_lvalue(self):
3551 return NameNode.is_lvalue(self)
3553 def is_ephemeral(self):
3555 return self.obj.is_ephemeral()
3557 return NameNode.is_ephemeral(self)
3559 def calculate_result_code(self):
3560 #print "AttributeNode.calculate_result_code:", self.member ###
3561 #print "...obj node =", self.obj, "code", self.obj.result() ###
3562 #print "...obj type", self.obj.type, "ctype", self.obj.ctype() ###
3564 obj_code = obj.result_as(obj.type)
3565 #print "...obj_code =", obj_code ###
3566 if self.entry and self.entry.is_cmethod:
3567 if obj.type.is_extension_type:
3568 return "((struct %s *)%s%s%s)->%s" % (
3569 obj.type.vtabstruct_cname, obj_code, self.op,
3570 obj.type.vtabslot_cname, self.member)
3573 elif obj.type.is_complex:
3574 return "__Pyx_C%s(%s)" % (self.member.upper(), obj_code)
3576 if obj.type.is_builtin_type and self.entry and self.entry.is_variable:
3577 # accessing a field of a builtin type, need to cast better than result_as() does
3578 obj_code = obj.type.cast_code(obj.result(), to_object_struct = True)
3579 return "%s%s%s" % (obj_code, self.op, self.member)
3581 def generate_result_code(self, code):
3582 interned_attr_cname = code.intern_identifier(self.attribute)
3585 '%s = PyObject_GetAttr(%s, %s); %s' % (
3587 self.obj.py_result(),
3588 interned_attr_cname,
3589 code.error_goto_if_null(self.result(), self.pos)))
3590 code.put_gotref(self.py_result())
3592 # result_code contains what is needed, but we may need to insert
3593 # a check and raise an exception
3594 if (self.obj.type.is_extension_type
3595 and self.needs_none_check
3596 and code.globalstate.directives['nonecheck']):
3597 self.put_nonecheck(code)
3599 def generate_assignment_code(self, rhs, code):
3600 interned_attr_cname = code.intern_identifier(self.attribute)
3601 self.obj.generate_evaluation_code(code)
3603 code.put_error_if_neg(self.pos,
3604 'PyObject_SetAttr(%s, %s, %s)' % (
3605 self.obj.py_result(),
3606 interned_attr_cname,
3608 rhs.generate_disposal_code(code)
3609 rhs.free_temps(code)
3610 elif self.obj.type.is_complex:
3611 code.putln("__Pyx_SET_C%s(%s, %s);" % (
3612 self.member.upper(),
3613 self.obj.result_as(self.obj.type),
3614 rhs.result_as(self.ctype())))
3616 if (self.obj.type.is_extension_type
3617 and self.needs_none_check
3618 and code.globalstate.directives['nonecheck']):
3619 self.put_nonecheck(code)
3621 select_code = self.result()
3622 if self.type.is_pyobject and self.use_managed_ref:
3623 rhs.make_owned_reference(code)
3624 code.put_giveref(rhs.py_result())
3625 code.put_gotref(select_code)
3626 code.put_decref(select_code, self.ctype())
3630 rhs.result_as(self.ctype())))
3632 rhs.generate_post_assignment_code(code)
3633 rhs.free_temps(code)
3634 self.obj.generate_disposal_code(code)
3635 self.obj.free_temps(code)
3637 def generate_deletion_code(self, code):
3638 interned_attr_cname = code.intern_identifier(self.attribute)
3639 self.obj.generate_evaluation_code(code)
3640 if self.is_py_attr or (isinstance(self.entry.scope, Symtab.PropertyScope)
3641 and u'__del__' in self.entry.scope.entries):
3642 code.put_error_if_neg(self.pos,
3643 'PyObject_DelAttr(%s, %s)' % (
3644 self.obj.py_result(),
3645 interned_attr_cname))
3647 error(self.pos, "Cannot delete C attribute of extension type")
3648 self.obj.generate_disposal_code(code)
3649 self.obj.free_temps(code)
3651 def annotate(self, code):
3653 code.annotate(self.pos, AnnotationItem('py_attr', 'python attribute', size=len(self.attribute)))
3655 code.annotate(self.pos, AnnotationItem('c_attr', 'c attribute', size=len(self.attribute)))
3657 def put_nonecheck(self, code):
3658 code.globalstate.use_utility_code(raise_noneattr_error_utility_code)
3659 code.putln("if (%s) {" % code.unlikely("%s == Py_None") % self.obj.result_as(PyrexTypes.py_object_type))
3660 code.putln("__Pyx_RaiseNoneAttributeError(\"%s\");" % self.attribute)
3661 code.putln(code.error_goto(self.pos))
3665 #-------------------------------------------------------------------
3669 #-------------------------------------------------------------------
3671 class StarredTargetNode(ExprNode):
3672 # A starred expression like "*a"
3674 # This is only allowed in sequence assignment targets such as
3676 # a, *b = (1,2,3,4) => a = 1 ; b = [2,3,4]
3678 # and will be removed during type analysis (or generate an error
3679 # if it's found at unexpected places).
3683 subexprs = ['target']
3685 type = py_object_type
3688 def __init__(self, pos, target):
3690 self.target = target
3692 def analyse_declarations(self, env):
3693 error(self.pos, "can use starred expression only as assignment target")
3694 self.target.analyse_declarations(env)
3696 def analyse_types(self, env):
3697 error(self.pos, "can use starred expression only as assignment target")
3698 self.target.analyse_types(env)
3699 self.type = self.target.type
3701 def analyse_target_declaration(self, env):
3702 self.target.analyse_target_declaration(env)
3704 def analyse_target_types(self, env):
3705 self.target.analyse_target_types(env)
3706 self.type = self.target.type
3708 def calculate_result_code(self):
3711 def generate_result_code(self, code):
3715 class SequenceNode(ExprNode):
3716 # Base class for list and tuple constructor nodes.
3717 # Contains common code for performing sequence unpacking.
3721 # unpacked_items [ExprNode] or None
3722 # coerced_unpacked_items [ExprNode] or None
3726 is_sequence_constructor = 1
3727 unpacked_items = None
3729 def compile_time_value_list(self, denv):
3730 return [arg.compile_time_value(denv) for arg in self.args]
3732 def replace_starred_target_node(self):
3733 # replace a starred node in the targets by the contained expression
3734 self.starred_assignment = False
3736 for arg in self.args:
3738 if self.starred_assignment:
3739 error(arg.pos, "more than 1 starred expression in assignment")
3740 self.starred_assignment = True
3742 arg.is_starred = True
3746 def analyse_target_declaration(self, env):
3747 self.replace_starred_target_node()
3748 for arg in self.args:
3749 arg.analyse_target_declaration(env)
3751 def analyse_types(self, env, skip_children=False):
3752 for i in range(len(self.args)):
3754 if not skip_children: arg.analyse_types(env)
3755 self.args[i] = arg.coerce_to_pyobject(env)
3757 # not setting self.type here, subtypes do this
3759 def may_be_none(self):
3762 def analyse_target_types(self, env):
3763 self.iterator = PyTempNode(self.pos, env)
3764 self.unpacked_items = []
3765 self.coerced_unpacked_items = []
3766 for arg in self.args:
3767 arg.analyse_target_types(env)
3769 if not arg.type.assignable_from(Builtin.list_type):
3771 "starred target must have Python object (list) type")
3772 if arg.type is py_object_type:
3773 arg.type = Builtin.list_type
3774 unpacked_item = PyTempNode(self.pos, env)
3775 coerced_unpacked_item = unpacked_item.coerce_to(arg.type, env)
3776 self.unpacked_items.append(unpacked_item)
3777 self.coerced_unpacked_items.append(coerced_unpacked_item)
3778 self.type = py_object_type
3780 def generate_result_code(self, code):
3781 self.generate_operation_code(code)
3783 def generate_assignment_code(self, rhs, code):
3784 if self.starred_assignment:
3785 self.generate_starred_assignment_code(rhs, code)
3787 self.generate_parallel_assignment_code(rhs, code)
3789 for item in self.unpacked_items:
3791 rhs.free_temps(code)
3793 def generate_parallel_assignment_code(self, rhs, code):
3794 # Need to work around the fact that generate_evaluation_code
3795 # allocates the temps in a rather hacky way -- the assignment
3796 # is evaluated twice, within each if-block.
3798 if rhs.type is tuple_type:
3799 tuple_check = "likely(%s != Py_None)"
3801 tuple_check = "PyTuple_CheckExact(%s)"
3803 "if (%s && likely(PyTuple_GET_SIZE(%s) == %s)) {" % (
3804 tuple_check % rhs.py_result(),
3807 code.putln("PyObject* tuple = %s;" % rhs.py_result())
3808 for item in self.unpacked_items:
3810 for i in range(len(self.args)):
3811 item = self.unpacked_items[i]
3813 "%s = PyTuple_GET_ITEM(tuple, %s); " % (
3816 code.put_incref(item.result(), item.ctype())
3817 value_node = self.coerced_unpacked_items[i]
3818 value_node.generate_evaluation_code(code)
3819 rhs.generate_disposal_code(code)
3821 for i in range(len(self.args)):
3822 self.args[i].generate_assignment_code(
3823 self.coerced_unpacked_items[i], code)
3825 code.putln("} else {")
3827 if rhs.type is tuple_type:
3828 code.globalstate.use_utility_code(tuple_unpacking_error_code)
3829 code.putln("__Pyx_UnpackTupleError(%s, %s);" % (
3830 rhs.py_result(), len(self.args)))
3831 code.putln(code.error_goto(self.pos))
3833 code.globalstate.use_utility_code(unpacking_utility_code)
3835 self.iterator.allocate(code)
3837 "%s = PyObject_GetIter(%s); %s" % (
3838 self.iterator.result(),
3840 code.error_goto_if_null(self.iterator.result(), self.pos)))
3841 code.put_gotref(self.iterator.py_result())
3842 rhs.generate_disposal_code(code)
3843 for i in range(len(self.args)):
3844 item = self.unpacked_items[i]
3845 unpack_code = "__Pyx_UnpackItem(%s, %d)" % (
3846 self.iterator.py_result(), i)
3850 typecast(item.ctype(), py_object_type, unpack_code),
3851 code.error_goto_if_null(item.result(), self.pos)))
3852 code.put_gotref(item.py_result())
3853 value_node = self.coerced_unpacked_items[i]
3854 value_node.generate_evaluation_code(code)
3855 code.put_error_if_neg(self.pos, "__Pyx_EndUnpack(%s, %d)" % (
3856 self.iterator.py_result(),
3858 if debug_disposal_code:
3859 print("UnpackNode.generate_assignment_code:")
3860 print("...generating disposal code for %s" % self.iterator)
3861 self.iterator.generate_disposal_code(code)
3862 self.iterator.free_temps(code)
3863 self.iterator.release(code)
3865 for i in range(len(self.args)):
3866 self.args[i].generate_assignment_code(
3867 self.coerced_unpacked_items[i], code)
3871 def generate_starred_assignment_code(self, rhs, code):
3872 code.globalstate.use_utility_code(unpacking_utility_code)
3874 for i, arg in enumerate(self.args):
3876 starred_target = self.unpacked_items[i]
3877 fixed_args_left = self.args[:i]
3878 fixed_args_right = self.args[i+1:]
3881 self.iterator.allocate(code)
3883 "%s = PyObject_GetIter(%s); %s" % (
3884 self.iterator.result(),
3886 code.error_goto_if_null(self.iterator.result(), self.pos)))
3887 code.put_gotref(self.iterator.py_result())
3888 rhs.generate_disposal_code(code)
3890 for item in self.unpacked_items:
3892 for i in range(len(fixed_args_left)):
3893 item = self.unpacked_items[i]
3894 unpack_code = "__Pyx_UnpackItem(%s, %d)" % (
3895 self.iterator.py_result(), i)
3899 typecast(item.ctype(), py_object_type, unpack_code),
3900 code.error_goto_if_null(item.result(), self.pos)))
3901 code.put_gotref(item.py_result())
3902 value_node = self.coerced_unpacked_items[i]
3903 value_node.generate_evaluation_code(code)
3905 target_list = starred_target.result()
3906 code.putln("%s = PySequence_List(%s); %s" % (
3907 target_list, self.iterator.py_result(),
3908 code.error_goto_if_null(target_list, self.pos)))
3909 code.put_gotref(target_list)
3910 if fixed_args_right:
3911 code.globalstate.use_utility_code(raise_need_more_values_to_unpack)
3912 unpacked_right_args = self.unpacked_items[-len(fixed_args_right):]
3913 code.putln("if (unlikely(PyList_GET_SIZE(%s) < %d)) {" % (
3914 (target_list, len(unpacked_right_args))))
3915 code.put("__Pyx_RaiseNeedMoreValuesError(%d+PyList_GET_SIZE(%s)); %s" % (
3916 len(fixed_args_left), target_list,
3917 code.error_goto(self.pos)))
3919 for i, (arg, coerced_arg) in enumerate(zip(unpacked_right_args[::-1],
3920 self.coerced_unpacked_items[::-1])):
3922 "%s = PyList_GET_ITEM(%s, PyList_GET_SIZE(%s)-1); " % (
3924 target_list, target_list))
3925 # resize the list the hard way
3926 code.putln("((PyVarObject*)%s)->ob_size--;" % target_list)
3927 code.put_gotref(arg.py_result())
3928 coerced_arg.generate_evaluation_code(code)
3930 self.iterator.generate_disposal_code(code)
3931 self.iterator.free_temps(code)
3932 self.iterator.release(code)
3934 for i in range(len(self.args)):
3935 self.args[i].generate_assignment_code(
3936 self.coerced_unpacked_items[i], code)
3938 def annotate(self, code):
3939 for arg in self.args:
3941 if self.unpacked_items:
3942 for arg in self.unpacked_items:
3944 for arg in self.coerced_unpacked_items:
3948 class TupleNode(SequenceNode):
3949 # Tuple constructor.
3953 gil_message = "Constructing Python tuple"
3955 def analyse_types(self, env, skip_children=False):
3956 if len(self.args) == 0:
3960 SequenceNode.analyse_types(self, env, skip_children)
3961 for child in self.args:
3962 if not child.is_literal:
3968 def calculate_result_code(self):
3969 if len(self.args) > 0:
3970 return self.result_code
3972 return Naming.empty_tuple
3974 def calculate_constant_result(self):
3975 self.constant_result = tuple([
3976 arg.constant_result for arg in self.args])
3978 def compile_time_value(self, denv):
3979 values = self.compile_time_value_list(denv)
3981 return tuple(values)
3982 except Exception, e:
3983 self.compile_time_value_error(e)
3985 def generate_operation_code(self, code):
3986 if len(self.args) == 0:
3987 # result_code is Naming.empty_tuple
3990 # non-empty cached tuple => result is global constant,
3991 # creation code goes into separate code writer
3992 self.result_code = code.get_py_const(py_object_type, 'tuple_', cleanup_level=2)
3993 code = code.get_cached_constants_writer()
3994 code.mark_pos(self.pos)
3997 "%s = PyTuple_New(%s); %s" % (
4000 code.error_goto_if_null(self.result(), self.pos)))
4001 code.put_gotref(self.py_result())
4002 for i in range(len(self.args)):
4004 if not arg.result_in_temp():
4005 code.put_incref(arg.result(), arg.ctype())
4007 "PyTuple_SET_ITEM(%s, %s, %s);" % (
4011 code.put_giveref(arg.py_result())
4013 code.put_giveref(self.py_result())
4015 def generate_subexpr_disposal_code(self, code):
4016 # We call generate_post_assignment_code here instead
4017 # of generate_disposal_code, because values were stored
4018 # in the tuple using a reference-stealing operation.
4019 for arg in self.args:
4020 arg.generate_post_assignment_code(code)
4021 # Should NOT call free_temps -- this is invoked by the default
4022 # generate_evaluation_code which will do that.
4025 class ListNode(SequenceNode):
4028 # obj_conversion_errors [PyrexError] used internally
4029 # orignial_args [ExprNode] used internally
4031 obj_conversion_errors = []
4034 gil_message = "Constructing Python list"
4036 def type_dependencies(self, env):
4039 def infer_type(self, env):
4040 # TOOD: Infer non-object list arrays.
4043 def analyse_expressions(self, env):
4044 SequenceNode.analyse_expressions(self, env)
4045 self.coerce_to_pyobject(env)
4047 def analyse_types(self, env):
4049 self.original_args = list(self.args)
4050 SequenceNode.analyse_types(self, env)
4051 self.obj_conversion_errors = held_errors()
4052 release_errors(ignore=True)
4054 def coerce_to(self, dst_type, env):
4055 if dst_type.is_pyobject:
4056 for err in self.obj_conversion_errors:
4058 self.obj_conversion_errors = []
4059 if not self.type.subtype_of(dst_type):
4060 error(self.pos, "Cannot coerce list to type '%s'" % dst_type)
4061 elif dst_type.is_ptr and dst_type.base_type is not PyrexTypes.c_void_type:
4062 base_type = dst_type.base_type
4063 self.type = PyrexTypes.CArrayType(base_type, len(self.args))
4064 for i in range(len(self.original_args)):
4066 if isinstance(arg, CoerceToPyTypeNode):
4068 self.args[i] = arg.coerce_to(base_type, env)
4069 elif dst_type.is_struct:
4070 if len(self.args) > len(dst_type.scope.var_entries):
4071 error(self.pos, "Too may members for '%s'" % dst_type)
4073 if len(self.args) < len(dst_type.scope.var_entries):
4074 warning(self.pos, "Too few members for '%s'" % dst_type, 1)
4075 for i, (arg, member) in enumerate(zip(self.original_args, dst_type.scope.var_entries)):
4076 if isinstance(arg, CoerceToPyTypeNode):
4078 self.args[i] = arg.coerce_to(member.type, env)
4079 self.type = dst_type
4081 self.type = error_type
4082 error(self.pos, "Cannot coerce list to type '%s'" % dst_type)
4085 def release_temp(self, env):
4086 if self.type.is_array:
4087 # To be valid C++, we must allocate the memory on the stack
4088 # manually and be sure not to reuse it for something else.
4091 SequenceNode.release_temp(self, env)
4093 def calculate_constant_result(self):
4094 self.constant_result = [
4095 arg.constant_result for arg in self.args]
4097 def compile_time_value(self, denv):
4098 return self.compile_time_value_list(denv)
4100 def generate_operation_code(self, code):
4101 if self.type.is_pyobject:
4102 for err in self.obj_conversion_errors:
4104 code.putln("%s = PyList_New(%s); %s" %
4107 code.error_goto_if_null(self.result(), self.pos)))
4108 code.put_gotref(self.py_result())
4109 for i in range(len(self.args)):
4111 #if not arg.is_temp:
4112 if not arg.result_in_temp():
4113 code.put_incref(arg.result(), arg.ctype())
4114 code.putln("PyList_SET_ITEM(%s, %s, %s);" %
4118 code.put_giveref(arg.py_result())
4119 elif self.type.is_array:
4120 for i, arg in enumerate(self.args):
4121 code.putln("%s[%s] = %s;" % (
4125 elif self.type.is_struct:
4126 for arg, member in zip(self.args, self.type.scope.var_entries):
4127 code.putln("%s.%s = %s;" % (
4132 raise InternalError("List type never specified")
4134 def generate_subexpr_disposal_code(self, code):
4135 # We call generate_post_assignment_code here instead
4136 # of generate_disposal_code, because values were stored
4137 # in the list using a reference-stealing operation.
4138 for arg in self.args:
4139 arg.generate_post_assignment_code(code)
4140 # Should NOT call free_temps -- this is invoked by the default
4141 # generate_evaluation_code which will do that.
4144 class ScopedExprNode(ExprNode):
4145 # Abstract base class for ExprNodes that have their own local
4146 # scope, such as generator expressions.
4148 # expr_scope Scope the inner scope of the expression
4153 # does this node really have a local scope, e.g. does it leak loop
4154 # variables or not? non-leaking Py3 behaviour is default, except
4155 # for list comprehensions where the behaviour differs in Py2 and
4156 # Py3 (set in Parsing.py based on parser context)
4157 has_local_scope = True
4159 def init_scope(self, outer_scope, expr_scope=None):
4160 if expr_scope is not None:
4161 self.expr_scope = expr_scope
4162 elif self.has_local_scope:
4163 self.expr_scope = Symtab.GeneratorExpressionScope(outer_scope)
4165 self.expr_scope = None
4167 def analyse_declarations(self, env):
4168 self.init_scope(env)
4170 def analyse_scoped_declarations(self, env):
4171 # this is called with the expr_scope as env
4174 def analyse_types(self, env):
4175 # no recursion here, the children will be analysed separately below
4178 def analyse_scoped_expressions(self, env):
4179 # this is called with the expr_scope as env
4182 def generate_evaluation_code(self, code):
4183 # set up local variables and free their references on exit
4184 generate_inner_evaluation_code = super(ScopedExprNode, self).generate_evaluation_code
4185 if not self.has_local_scope or not self.expr_scope.var_entries:
4186 # no local variables => delegate, done
4187 generate_inner_evaluation_code(code)
4190 code.putln('{ /* enter inner scope */')
4192 for entry in self.expr_scope.var_entries:
4193 if not entry.in_closure:
4194 code.put_var_declaration(entry)
4195 if entry.type.is_pyobject and entry.used:
4196 py_entries.append(entry)
4198 # no local Python references => no cleanup required
4199 generate_inner_evaluation_code(code)
4200 code.putln('} /* exit inner scope */')
4202 for entry in py_entries:
4203 code.put_init_var_to_py_none(entry)
4205 # must free all local Python references at each exit point
4206 old_loop_labels = tuple(code.new_loop_labels())
4207 old_error_label = code.new_error_label()
4209 generate_inner_evaluation_code(code)
4211 # normal (non-error) exit
4212 for entry in py_entries:
4213 code.put_var_decref(entry)
4215 # error/loop body exit points
4216 exit_scope = code.new_label('exit_scope')
4217 code.put_goto(exit_scope)
4218 for label, old_label in ([(code.error_label, old_error_label)] +
4219 list(zip(code.get_loop_labels(), old_loop_labels))):
4220 if code.label_used(label):
4221 code.put_label(label)
4222 for entry in py_entries:
4223 code.put_var_decref(entry)
4224 code.put_goto(old_label)
4225 code.put_label(exit_scope)
4226 code.putln('} /* exit inner scope */')
4228 code.set_loop_labels(old_loop_labels)
4229 code.error_label = old_error_label
4232 class ComprehensionNode(ScopedExprNode):
4233 subexprs = ["target"]
4234 child_attrs = ["loop", "append"]
4236 def infer_type(self, env):
4237 return self.target.infer_type(env)
4239 def analyse_declarations(self, env):
4240 self.append.target = self # this is used in the PyList_Append of the inner loop
4241 self.init_scope(env)
4243 def analyse_scoped_declarations(self, env):
4244 self.loop.analyse_declarations(env)
4246 def analyse_types(self, env):
4247 self.target.analyse_expressions(env)
4248 self.type = self.target.type
4249 if not self.has_local_scope:
4250 self.loop.analyse_expressions(env)
4252 def analyse_scoped_expressions(self, env):
4253 if self.has_local_scope:
4254 self.loop.analyse_expressions(env)
4256 def may_be_none(self):
4259 def calculate_result_code(self):
4260 return self.target.result()
4262 def generate_result_code(self, code):
4263 self.generate_operation_code(code)
4265 def generate_operation_code(self, code):
4266 self.loop.generate_execution_code(code)
4268 def annotate(self, code):
4269 self.loop.annotate(code)
4272 class ComprehensionAppendNode(Node):
4273 # Need to be careful to avoid infinite recursion:
4274 # target must not be in child_attrs/subexprs
4276 child_attrs = ['expr']
4278 type = PyrexTypes.c_int_type
4280 def analyse_expressions(self, env):
4281 self.expr.analyse_expressions(env)
4282 if not self.expr.type.is_pyobject:
4283 self.expr = self.expr.coerce_to_pyobject(env)
4285 def generate_execution_code(self, code):
4286 if self.target.type is list_type:
4287 function = "PyList_Append"
4288 elif self.target.type is set_type:
4289 function = "PySet_Add"
4291 raise InternalError(
4292 "Invalid type for comprehension node: %s" % self.target.type)
4294 self.expr.generate_evaluation_code(code)
4295 code.putln(code.error_goto_if("%s(%s, (PyObject*)%s)" % (
4297 self.target.result(),
4300 self.expr.generate_disposal_code(code)
4301 self.expr.free_temps(code)
4303 def generate_function_definitions(self, env, code):
4304 self.expr.generate_function_definitions(env, code)
4306 def annotate(self, code):
4307 self.expr.annotate(code)
4309 class DictComprehensionAppendNode(ComprehensionAppendNode):
4310 child_attrs = ['key_expr', 'value_expr']
4312 def analyse_expressions(self, env):
4313 self.key_expr.analyse_expressions(env)
4314 if not self.key_expr.type.is_pyobject:
4315 self.key_expr = self.key_expr.coerce_to_pyobject(env)
4316 self.value_expr.analyse_expressions(env)
4317 if not self.value_expr.type.is_pyobject:
4318 self.value_expr = self.value_expr.coerce_to_pyobject(env)
4320 def generate_execution_code(self, code):
4321 self.key_expr.generate_evaluation_code(code)
4322 self.value_expr.generate_evaluation_code(code)
4323 code.putln(code.error_goto_if("PyDict_SetItem(%s, (PyObject*)%s, (PyObject*)%s)" % (
4324 self.target.result(),
4325 self.key_expr.result(),
4326 self.value_expr.result()
4328 self.key_expr.generate_disposal_code(code)
4329 self.key_expr.free_temps(code)
4330 self.value_expr.generate_disposal_code(code)
4331 self.value_expr.free_temps(code)
4333 def generate_function_definitions(self, env, code):
4334 self.key_expr.generate_function_definitions(env, code)
4335 self.value_expr.generate_function_definitions(env, code)
4337 def annotate(self, code):
4338 self.key_expr.annotate(code)
4339 self.value_expr.annotate(code)
4342 class GeneratorExpressionNode(ScopedExprNode):
4343 # A generator expression, e.g. (i for i in range(10))
4345 # Result is a generator.
4347 # loop ForStatNode the for-loop, containing a YieldExprNode
4349 child_attrs = ["loop"]
4351 type = py_object_type
4353 def analyse_scoped_declarations(self, env):
4354 self.loop.analyse_declarations(env)
4356 def analyse_types(self, env):
4357 if not self.has_local_scope:
4358 self.loop.analyse_expressions(env)
4361 def analyse_scoped_expressions(self, env):
4362 if self.has_local_scope:
4363 self.loop.analyse_expressions(env)
4365 def may_be_none(self):
4368 def annotate(self, code):
4369 self.loop.annotate(code)
4372 class InlinedGeneratorExpressionNode(GeneratorExpressionNode):
4373 # An inlined generator expression for which the result is
4374 # calculated inside of the loop. This will only be created by
4375 # transforms when replacing builtin calls on generator
4378 # loop ForStatNode the for-loop, not containing any YieldExprNodes
4379 # result_node ResultRefNode the reference to the result value temp
4380 # orig_func String the name of the builtin function this node replaces
4382 child_attrs = ["loop"]
4383 loop_analysed = False
4385 def infer_type(self, env):
4386 return self.result_node.infer_type(env)
4388 def analyse_types(self, env):
4389 if not self.has_local_scope:
4390 self.loop_analysed = True
4391 self.loop.analyse_expressions(env)
4392 self.type = self.result_node.type
4395 def analyse_scoped_expressions(self, env):
4396 self.loop_analysed = True
4397 GeneratorExpressionNode.analyse_scoped_expressions(self, env)
4399 def coerce_to(self, dst_type, env):
4400 if self.orig_func == 'sum' and dst_type.is_numeric and not self.loop_analysed:
4401 # We can optimise by dropping the aggregation variable and
4402 # the add operations into C. This can only be done safely
4403 # before analysing the loop body, after that, the result
4404 # reference type will have infected expressions and
4406 self.result_node.type = self.type = dst_type
4408 return GeneratorExpressionNode.coerce_to(self, dst_type, env)
4410 def generate_result_code(self, code):
4411 self.result_node.result_code = self.result()
4412 self.loop.generate_execution_code(code)
4415 class SetNode(ExprNode):
4422 gil_message = "Constructing Python set"
4424 def analyse_types(self, env):
4425 for i in range(len(self.args)):
4427 arg.analyse_types(env)
4428 self.args[i] = arg.coerce_to_pyobject(env)
4429 self.type = set_type
4432 def may_be_none(self):
4435 def calculate_constant_result(self):
4436 self.constant_result = set([
4437 arg.constant_result for arg in self.args])
4439 def compile_time_value(self, denv):
4440 values = [arg.compile_time_value(denv) for arg in self.args]
4443 except Exception, e:
4444 self.compile_time_value_error(e)
4446 def generate_evaluation_code(self, code):
4447 code.globalstate.use_utility_code(Builtin.py23_set_utility_code)
4448 self.allocate_temp_result(code)
4450 "%s = PySet_New(0); %s" % (
4452 code.error_goto_if_null(self.result(), self.pos)))
4453 code.put_gotref(self.py_result())
4454 for arg in self.args:
4455 arg.generate_evaluation_code(code)
4457 code.error_goto_if_neg(
4458 "PySet_Add(%s, %s)" % (self.result(), arg.py_result()),
4460 arg.generate_disposal_code(code)
4461 arg.free_temps(code)
4464 class DictNode(ExprNode):
4465 # Dictionary constructor.
4467 # key_value_pairs [DictItemNode]
4469 # obj_conversion_errors [PyrexError] used internally
4471 subexprs = ['key_value_pairs']
4475 obj_conversion_errors = []
4477 def calculate_constant_result(self):
4478 self.constant_result = dict([
4479 item.constant_result for item in self.key_value_pairs])
4481 def compile_time_value(self, denv):
4482 pairs = [(item.key.compile_time_value(denv), item.value.compile_time_value(denv))
4483 for item in self.key_value_pairs]
4486 except Exception, e:
4487 self.compile_time_value_error(e)
4489 def type_dependencies(self, env):
4492 def infer_type(self, env):
4493 # TOOD: Infer struct constructors.
4496 def analyse_types(self, env):
4498 for item in self.key_value_pairs:
4499 item.analyse_types(env)
4500 self.obj_conversion_errors = held_errors()
4501 release_errors(ignore=True)
4503 def may_be_none(self):
4506 def coerce_to(self, dst_type, env):
4507 if dst_type.is_pyobject:
4508 self.release_errors()
4509 if not self.type.subtype_of(dst_type):
4510 error(self.pos, "Cannot interpret dict as type '%s'" % dst_type)
4511 elif dst_type.is_struct_or_union:
4512 self.type = dst_type
4513 if not dst_type.is_struct and len(self.key_value_pairs) != 1:
4514 error(self.pos, "Exactly one field must be specified to convert to union '%s'" % dst_type)
4515 elif dst_type.is_struct and len(self.key_value_pairs) < len(dst_type.scope.var_entries):
4516 warning(self.pos, "Not all members given for struct '%s'" % dst_type, 1)
4517 for item in self.key_value_pairs:
4518 if isinstance(item.key, CoerceToPyTypeNode):
4519 item.key = item.key.arg
4520 if not isinstance(item.key, (UnicodeNode, StringNode, BytesNode)):
4521 error(item.key.pos, "Invalid struct field identifier")
4522 item.key = StringNode(item.key.pos, value="<error>")
4524 key = str(item.key.value) # converts string literals to unicode in Py3
4525 member = dst_type.scope.lookup_here(key)
4527 error(item.key.pos, "struct '%s' has no field '%s'" % (dst_type, key))
4530 if isinstance(value, CoerceToPyTypeNode):
4532 item.value = value.coerce_to(member.type, env)
4534 self.type = error_type
4535 error(self.pos, "Cannot interpret dict as type '%s'" % dst_type)
4538 def release_errors(self):
4539 for err in self.obj_conversion_errors:
4541 self.obj_conversion_errors = []
4543 gil_message = "Constructing Python dict"
4545 def generate_evaluation_code(self, code):
4546 # Custom method used here because key-value
4547 # pairs are evaluated and used one at a time.
4548 code.mark_pos(self.pos)
4549 self.allocate_temp_result(code)
4550 if self.type.is_pyobject:
4551 self.release_errors()
4553 "%s = PyDict_New(); %s" % (
4555 code.error_goto_if_null(self.result(), self.pos)))
4556 code.put_gotref(self.py_result())
4557 for item in self.key_value_pairs:
4558 item.generate_evaluation_code(code)
4559 if self.type.is_pyobject:
4560 code.put_error_if_neg(self.pos,
4561 "PyDict_SetItem(%s, %s, %s)" % (
4563 item.key.py_result(),
4564 item.value.py_result()))
4566 code.putln("%s.%s = %s;" % (
4569 item.value.result()))
4570 item.generate_disposal_code(code)
4571 item.free_temps(code)
4573 def annotate(self, code):
4574 for item in self.key_value_pairs:
4577 class DictItemNode(ExprNode):
4578 # Represents a single item in a DictNode
4582 subexprs = ['key', 'value']
4584 nogil_check = None # Parent DictNode takes care of it
4586 def calculate_constant_result(self):
4587 self.constant_result = (
4588 self.key.constant_result, self.value.constant_result)
4590 def analyse_types(self, env):
4591 self.key.analyse_types(env)
4592 self.value.analyse_types(env)
4593 self.key = self.key.coerce_to_pyobject(env)
4594 self.value = self.value.coerce_to_pyobject(env)
4596 def generate_evaluation_code(self, code):
4597 self.key.generate_evaluation_code(code)
4598 self.value.generate_evaluation_code(code)
4600 def generate_disposal_code(self, code):
4601 self.key.generate_disposal_code(code)
4602 self.value.generate_disposal_code(code)
4604 def free_temps(self, code):
4605 self.key.free_temps(code)
4606 self.value.free_temps(code)
4609 return iter([self.key, self.value])
4611 class ModuleNameMixin(object):
4612 def set_mod_name(self, env):
4613 self.module_name = env.global_scope().qualified_name
4615 def get_py_mod_name(self, code):
4616 return code.get_py_string_const(
4617 self.module_name, identifier=True)
4619 class ClassNode(ExprNode, ModuleNameMixin):
4620 # Helper class used in the implementation of Python
4621 # class definitions. Constructs a class object given
4622 # a name, tuple of bases and class dictionary.
4624 # name EncodedString Name of the class
4625 # bases ExprNode Base class tuple
4626 # dict ExprNode Class dict (not owned by this node)
4627 # doc ExprNode or None Doc string
4628 # module_name EncodedString Name of defining module
4630 subexprs = ['bases', 'doc']
4632 def analyse_types(self, env):
4633 self.bases.analyse_types(env)
4635 self.doc.analyse_types(env)
4636 self.doc = self.doc.coerce_to_pyobject(env)
4637 self.type = py_object_type
4639 env.use_utility_code(create_class_utility_code);
4640 #TODO(craig,haoyu) This should be moved to a better place
4641 self.set_mod_name(env)
4643 def may_be_none(self):
4646 gil_message = "Constructing Python class"
4648 def generate_result_code(self, code):
4649 cname = code.intern_identifier(self.name)
4652 code.put_error_if_neg(self.pos,
4653 'PyDict_SetItemString(%s, "__doc__", %s)' % (
4654 self.dict.py_result(),
4655 self.doc.py_result()))
4656 py_mod_name = self.get_py_mod_name(code)
4658 '%s = __Pyx_CreateClass(%s, %s, %s, %s); %s' % (
4660 self.bases.py_result(),
4661 self.dict.py_result(),
4664 code.error_goto_if_null(self.result(), self.pos)))
4665 code.put_gotref(self.py_result())
4668 class Py3ClassNode(ExprNode):
4669 # Helper class used in the implementation of Python3+
4670 # class definitions. Constructs a class object given
4671 # a name, tuple of bases and class dictionary.
4673 # name EncodedString Name of the class
4674 # dict ExprNode Class dict (not owned by this node)
4675 # module_name EncodedString Name of defining module
4679 def analyse_types(self, env):
4680 self.type = py_object_type
4683 def may_be_none(self):
4686 gil_message = "Constructing Python class"
4688 def generate_result_code(self, code):
4689 code.globalstate.use_utility_code(create_py3class_utility_code)
4690 cname = code.intern_identifier(self.name)
4692 '%s = __Pyx_Py3ClassCreate(%s, %s, %s, %s, %s); %s' % (
4694 self.metaclass.result(),
4696 self.bases.py_result(),
4697 self.dict.py_result(),
4698 self.mkw.py_result(),
4699 code.error_goto_if_null(self.result(), self.pos)))
4700 code.put_gotref(self.py_result())
4702 class KeywordArgsNode(ExprNode):
4703 # Helper class for keyword arguments
4705 # keyword_args ExprNode or None Keyword arguments
4706 # starstar_arg ExprNode or None Extra arguments
4708 subexprs = ['keyword_args', 'starstar_arg']
4710 def analyse_types(self, env):
4711 if self.keyword_args:
4712 self.keyword_args.analyse_types(env)
4713 if self.starstar_arg:
4714 self.starstar_arg.analyse_types(env)
4715 # make sure we have a Python object as **kwargs mapping
4716 self.starstar_arg = \
4717 self.starstar_arg.coerce_to_pyobject(env)
4718 self.type = py_object_type
4721 gil_message = "Constructing Keyword Args"
4723 def generate_result_code(self, code):
4724 if self.keyword_args and self.starstar_arg:
4725 code.put_error_if_neg(self.pos,
4726 "PyDict_Update(%s, %s)" % (
4727 self.keyword_args.py_result(),
4728 self.starstar_arg.py_result()))
4729 if self.keyword_args:
4730 code.putln("%s = %s;" % (self.result(), self.keyword_args.result()))
4731 code.put_incref(self.keyword_args.result(), self.keyword_args.ctype())
4732 elif self.starstar_arg:
4734 "%s = PyDict_Copy(%s); %s" % (
4736 self.starstar_arg.py_result(),
4737 code.error_goto_if_null(self.result(), self.pos)))
4738 code.put_gotref(self.py_result())
4741 "%s = PyDict_New(); %s" % (
4743 code.error_goto_if_null(self.result(), self.pos)))
4744 code.put_gotref(self.py_result())
4746 class PyClassMetaclassNode(ExprNode):
4747 # Helper class holds Python3 metaclass object
4749 # bases ExprNode Base class tuple (not owned by this node)
4750 # mkw ExprNode Class keyword arguments (not owned by this node)
4754 def analyse_types(self, env):
4755 self.type = py_object_type
4758 def may_be_none(self):
4761 def generate_result_code(self, code):
4763 "%s = __Pyx_Py3MetaclassGet(%s, %s); %s" % (
4765 self.bases.result(),
4767 code.error_goto_if_null(self.result(), self.pos)))
4768 code.put_gotref(self.py_result())
4770 class PyClassNamespaceNode(ExprNode, ModuleNameMixin):
4771 # Helper class holds Python3 namespace object
4773 # All this are not owned by this node
4774 # metaclass ExprNode Metaclass object
4775 # bases ExprNode Base class tuple
4776 # mkw ExprNode Class keyword arguments
4777 # doc ExprNode or None Doc string (owned)
4781 def analyse_types(self, env):
4782 self.bases.analyse_types(env)
4784 self.doc.analyse_types(env)
4785 self.doc = self.doc.coerce_to_pyobject(env)
4786 self.type = py_object_type
4788 #TODO(craig,haoyu) This should be moved to a better place
4789 self.set_mod_name(env)
4791 def may_be_none(self):
4794 def generate_result_code(self, code):
4795 cname = code.intern_identifier(self.name)
4796 py_mod_name = self.get_py_mod_name(code)
4798 doc_code = self.doc.result()
4800 doc_code = '(PyObject *) NULL'
4802 "%s = __Pyx_Py3MetaclassPrepare(%s, %s, %s, %s, %s, %s); %s" % (
4804 self.metaclass.result(),
4805 self.bases.result(),
4810 code.error_goto_if_null(self.result(), self.pos)))
4811 code.put_gotref(self.py_result())
4813 class BoundMethodNode(ExprNode):
4814 # Helper class used in the implementation of Python
4815 # class definitions. Constructs an bound method
4816 # object from a class and a function.
4818 # function ExprNode Function object
4819 # self_object ExprNode self object
4821 subexprs = ['function']
4823 def analyse_types(self, env):
4824 self.function.analyse_types(env)
4825 self.type = py_object_type
4828 gil_message = "Constructing an bound method"
4830 def generate_result_code(self, code):
4832 "%s = PyMethod_New(%s, %s, (PyObject*)%s->ob_type); %s" % (
4834 self.function.py_result(),
4835 self.self_object.py_result(),
4836 self.self_object.py_result(),
4837 code.error_goto_if_null(self.result(), self.pos)))
4838 code.put_gotref(self.py_result())
4840 class UnboundMethodNode(ExprNode):
4841 # Helper class used in the implementation of Python
4842 # class definitions. Constructs an unbound method
4843 # object from a class and a function.
4845 # function ExprNode Function object
4847 type = py_object_type
4850 subexprs = ['function']
4852 def analyse_types(self, env):
4853 self.function.analyse_types(env)
4855 def may_be_none(self):
4858 gil_message = "Constructing an unbound method"
4860 def generate_result_code(self, code):
4861 class_cname = code.pyclass_stack[-1].classobj.result()
4863 "%s = PyMethod_New(%s, 0, %s); %s" % (
4865 self.function.py_result(),
4867 code.error_goto_if_null(self.result(), self.pos)))
4868 code.put_gotref(self.py_result())
4871 class PyCFunctionNode(ExprNode, ModuleNameMixin):
4872 # Helper class used in the implementation of Python
4873 # class definitions. Constructs a PyCFunction object
4874 # from a PyMethodDef struct.
4876 # pymethdef_cname string PyMethodDef structure
4877 # self_object ExprNode or None
4879 # module_name EncodedString Name of defining module
4885 type = py_object_type
4888 def analyse_types(self, env):
4890 env.use_utility_code(binding_cfunc_utility_code)
4892 #TODO(craig,haoyu) This should be moved to a better place
4893 self.set_mod_name(env)
4895 def may_be_none(self):
4898 gil_message = "Constructing Python function"
4900 def self_result_code(self):
4901 if self.self_object is None:
4902 self_result = "NULL"
4904 self_result = self.self_object.py_result()
4907 def generate_result_code(self, code):
4909 constructor = "%s_NewEx" % Naming.binding_cfunc
4911 constructor = "PyCFunction_NewEx"
4912 py_mod_name = self.get_py_mod_name(code)
4914 '%s = %s(&%s, %s, %s); %s' % (
4917 self.pymethdef_cname,
4918 self.self_result_code(),
4920 code.error_goto_if_null(self.result(), self.pos)))
4921 code.put_gotref(self.py_result())
4923 class InnerFunctionNode(PyCFunctionNode):
4924 # Special PyCFunctionNode that depends on a closure class
4928 needs_self_code = True
4930 def self_result_code(self):
4931 if self.needs_self_code:
4932 return "((PyObject*)%s)" % (Naming.cur_scope_cname)
4935 class LambdaNode(InnerFunctionNode):
4936 # Lambda expression node (only used as a function reference)
4938 # args [CArgDeclNode] formal arguments
4939 # star_arg PyArgDeclNode or None * argument
4940 # starstar_arg PyArgDeclNode or None ** argument
4941 # lambda_name string a module-globally unique lambda name
4942 # result_expr ExprNode
4943 # def_node DefNode the underlying function 'def' node
4945 child_attrs = ['def_node']
4948 name = StringEncoding.EncodedString('<lambda>')
4950 def analyse_declarations(self, env):
4951 self.def_node.analyse_declarations(env)
4952 self.pymethdef_cname = self.def_node.entry.pymethdef_cname
4953 env.add_lambda_def(self.def_node)
4955 class YieldExprNode(ExprNode):
4956 # Yield expression node
4958 # arg ExprNode the value to return from the generator
4959 # label_name string name of the C label used for this yield
4962 type = py_object_type
4964 def analyse_types(self, env):
4966 if self.arg is not None:
4967 self.arg.analyse_types(env)
4968 if not self.arg.type.is_pyobject:
4969 self.arg = self.arg.coerce_to_pyobject(env)
4970 error(self.pos, "Generators are not supported")
4972 def generate_result_code(self, code):
4973 self.label_name = code.new_label('resume_from_yield')
4974 code.use_label(self.label_name)
4975 code.putln("/* FIXME: save temporary variables */")
4976 code.putln("/* FIXME: return from function, yielding value */")
4977 code.put_label(self.label_name)
4978 code.putln("/* FIXME: restore temporary variables and */")
4979 code.putln("/* FIXME: extract sent value from closure */")
4982 #-------------------------------------------------------------------
4984 # Unary operator nodes
4986 #-------------------------------------------------------------------
4988 compile_time_unary_operators = {
4989 'not': operator.not_,
4995 class UnopNode(ExprNode):
4999 # Processing during analyse_expressions phase:
5001 # analyse_c_operation
5002 # Called when the operand is not a pyobject.
5003 # - Check operand type and coerce if needed.
5004 # - Determine result type and result code fragment.
5005 # - Allocate temporary for result if needed.
5007 subexprs = ['operand']
5010 def calculate_constant_result(self):
5011 func = compile_time_unary_operators[self.operator]
5012 self.constant_result = func(self.operand.constant_result)
5014 def compile_time_value(self, denv):
5015 func = compile_time_unary_operators.get(self.operator)
5018 "Unary '%s' not supported in compile-time expression"
5020 operand = self.operand.compile_time_value(denv)
5022 return func(operand)
5023 except Exception, e:
5024 self.compile_time_value_error(e)
5026 def infer_type(self, env):
5027 operand_type = self.operand.infer_type(env)
5028 if operand_type.is_pyobject:
5029 return py_object_type
5033 def analyse_types(self, env):
5034 self.operand.analyse_types(env)
5035 if self.is_py_operation():
5036 self.coerce_operand_to_pyobject(env)
5037 self.type = py_object_type
5039 elif self.is_cpp_operation():
5040 self.analyse_cpp_operation(env)
5042 self.analyse_c_operation(env)
5044 def check_const(self):
5045 return self.operand.check_const()
5047 def is_py_operation(self):
5048 return self.operand.type.is_pyobject
5050 def nogil_check(self, env):
5051 if self.is_py_operation():
5054 def is_cpp_operation(self):
5055 type = self.operand.type
5056 return type.is_cpp_class
5058 def coerce_operand_to_pyobject(self, env):
5059 self.operand = self.operand.coerce_to_pyobject(env)
5061 def generate_result_code(self, code):
5062 if self.operand.type.is_pyobject:
5063 self.generate_py_operation_code(code)
5065 def generate_py_operation_code(self, code):
5066 function = self.py_operation_function()
5068 "%s = %s(%s); %s" % (
5071 self.operand.py_result(),
5072 code.error_goto_if_null(self.result(), self.pos)))
5073 code.put_gotref(self.py_result())
5075 def type_error(self):
5076 if not self.operand.type.is_error:
5077 error(self.pos, "Invalid operand type for '%s' (%s)" %
5078 (self.operator, self.operand.type))
5079 self.type = PyrexTypes.error_type
5081 def analyse_cpp_operation(self, env):
5082 type = self.operand.type
5084 type = type.base_type
5085 function = type.scope.lookup("operator%s" % self.operator)
5087 error(self.pos, "'%s' operator not defined for %s"
5088 % (self.operator, type))
5091 func_type = function.type
5092 if func_type.is_ptr:
5093 func_type = func_type.base_type
5094 self.type = func_type.return_type
5097 class NotNode(ExprNode):
5102 type = PyrexTypes.c_bint_type
5104 subexprs = ['operand']
5106 def calculate_constant_result(self):
5107 self.constant_result = not self.operand.constant_result
5109 def compile_time_value(self, denv):
5110 operand = self.operand.compile_time_value(denv)
5113 except Exception, e:
5114 self.compile_time_value_error(e)
5116 def infer_type(self, env):
5117 return PyrexTypes.c_bint_type
5119 def analyse_types(self, env):
5120 self.operand.analyse_types(env)
5121 self.operand = self.operand.coerce_to_boolean(env)
5123 def calculate_result_code(self):
5124 return "(!%s)" % self.operand.result()
5126 def generate_result_code(self, code):
5130 class UnaryPlusNode(UnopNode):
5131 # unary '+' operator
5135 def analyse_c_operation(self, env):
5136 self.type = self.operand.type
5138 def py_operation_function(self):
5139 return "PyNumber_Positive"
5141 def calculate_result_code(self):
5142 if self.is_cpp_operation():
5143 return "(+%s)" % self.operand.result()
5145 return self.operand.result()
5148 class UnaryMinusNode(UnopNode):
5149 # unary '-' operator
5153 def analyse_c_operation(self, env):
5154 if self.operand.type.is_numeric:
5155 self.type = self.operand.type
5158 if self.type.is_complex:
5161 def py_operation_function(self):
5162 return "PyNumber_Negative"
5164 def calculate_result_code(self):
5166 return "(-%s)" % self.operand.result()
5168 return "%s(%s)" % (self.operand.type.unary_op('-'), self.operand.result())
5170 def get_constant_c_result_code(self):
5171 value = self.operand.get_constant_c_result_code()
5173 return "(-%s)" % (value)
5175 class TildeNode(UnopNode):
5176 # unary '~' operator
5178 def analyse_c_operation(self, env):
5179 if self.operand.type.is_int:
5180 self.type = self.operand.type
5184 def py_operation_function(self):
5185 return "PyNumber_Invert"
5187 def calculate_result_code(self):
5188 return "(~%s)" % self.operand.result()
5191 class CUnopNode(UnopNode):
5193 def is_py_operation(self):
5196 class DereferenceNode(CUnopNode):
5201 def analyse_c_operation(self, env):
5202 if self.operand.type.is_ptr:
5203 self.type = self.operand.type.base_type
5207 def calculate_result_code(self):
5208 return "(*%s)" % self.operand.result()
5211 class DecrementIncrementNode(CUnopNode):
5212 # unary ++/-- operator
5214 def analyse_c_operation(self, env):
5215 if self.operand.type.is_ptr or self.operand.type.is_numeric:
5216 self.type = self.operand.type
5220 def calculate_result_code(self):
5222 return "(%s%s)" % (self.operator, self.operand.result())
5224 return "(%s%s)" % (self.operand.result(), self.operator)
5226 def inc_dec_constructor(is_prefix, operator):
5227 return lambda pos, **kwds: DecrementIncrementNode(pos, is_prefix=is_prefix, operator=operator, **kwds)
5230 class AmpersandNode(ExprNode):
5231 # The C address-of operator.
5235 subexprs = ['operand']
5237 def infer_type(self, env):
5238 return PyrexTypes.c_ptr_type(self.operand.infer_type(env))
5240 def analyse_types(self, env):
5241 self.operand.analyse_types(env)
5242 argtype = self.operand.type
5243 if not (argtype.is_cfunction or self.operand.is_lvalue()):
5244 self.error("Taking address of non-lvalue")
5246 if argtype.is_pyobject:
5247 self.error("Cannot take address of Python variable")
5249 self.type = PyrexTypes.c_ptr_type(argtype)
5251 def check_const(self):
5252 return self.operand.check_const_addr()
5254 def error(self, mess):
5255 error(self.pos, mess)
5256 self.type = PyrexTypes.error_type
5257 self.result_code = "<error>"
5259 def calculate_result_code(self):
5260 return "(&%s)" % self.operand.result()
5262 def generate_result_code(self, code):
5266 unop_node_classes = {
5268 "-": UnaryMinusNode,
5272 def unop_node(pos, operator, operand):
5273 # Construct unnop node of appropriate class for
5275 if isinstance(operand, IntNode) and operator == '-':
5276 return IntNode(pos = operand.pos, value = str(-Utils.str_to_number(operand.value)))
5277 elif isinstance(operand, UnopNode) and operand.operator == operator:
5278 warning(pos, "Python has no increment/decrement operator: %s%sx = %s(%sx) = x" % ((operator,)*4), 5)
5279 return unop_node_classes[operator](pos,
5280 operator = operator,
5284 class TypecastNode(ExprNode):
5288 # base_type CBaseTypeNode
5289 # declarator CDeclaratorNode
5291 # If used from a transform, one can if wanted specify the attribute
5292 # "type" directly and leave base_type and declarator to None
5294 subexprs = ['operand']
5295 base_type = declarator = type = None
5297 def type_dependencies(self, env):
5300 def infer_type(self, env):
5301 if self.type is None:
5302 base_type = self.base_type.analyse(env)
5303 _, self.type = self.declarator.analyse(base_type, env)
5306 def analyse_types(self, env):
5307 if self.type is None:
5308 base_type = self.base_type.analyse(env)
5309 _, self.type = self.declarator.analyse(base_type, env)
5310 if self.type.is_cfunction:
5312 "Cannot cast to a function type")
5313 self.type = PyrexTypes.error_type
5314 self.operand.analyse_types(env)
5315 to_py = self.type.is_pyobject
5316 from_py = self.operand.type.is_pyobject
5317 if from_py and not to_py and self.operand.is_ephemeral() and not self.type.is_numeric:
5318 error(self.pos, "Casting temporary Python object to non-numeric non-Python type")
5319 if to_py and not from_py:
5320 if self.type is bytes_type and self.operand.type.is_int:
5321 # FIXME: the type cast node isn't needed in this case
5322 # and can be dropped once analyse_types() can return a
5324 self.operand = CoerceIntToBytesNode(self.operand, env)
5325 elif self.operand.type.can_coerce_to_pyobject(env):
5326 self.result_ctype = py_object_type
5327 self.operand = self.operand.coerce_to_pyobject(env)
5329 if self.operand.type.is_ptr:
5330 if not (self.operand.type.base_type.is_void or self.operand.type.base_type.is_struct):
5331 error(self.pos, "Python objects cannot be cast from pointers of primitive types")
5333 # Should this be an error?
5334 warning(self.pos, "No conversion from %s to %s, python object pointer used." % (self.operand.type, self.type))
5335 self.operand = self.operand.coerce_to_simple(env)
5336 elif from_py and not to_py:
5337 if self.type.create_from_py_utility_code(env):
5338 self.operand = self.operand.coerce_to(self.type, env)
5339 elif self.type.is_ptr:
5340 if not (self.type.base_type.is_void or self.type.base_type.is_struct):
5341 error(self.pos, "Python objects cannot be cast to pointers of primitive types")
5343 warning(self.pos, "No conversion from %s to %s, python object pointer used." % (self.type, self.operand.type))
5344 elif from_py and to_py:
5345 if self.typecheck and self.type.is_extension_type:
5346 self.operand = PyTypeTestNode(self.operand, self.type, env, notnone=True)
5347 elif self.type.is_complex and self.operand.type.is_complex:
5348 self.operand = self.operand.coerce_to_simple(env)
5350 def nogil_check(self, env):
5351 if self.type and self.type.is_pyobject and self.is_temp:
5354 def check_const(self):
5355 return self.operand.check_const()
5357 def calculate_constant_result(self):
5358 # we usually do not know the result of a type cast at code
5362 def calculate_result_code(self):
5363 if self.type.is_complex:
5364 operand_result = self.operand.result()
5365 if self.operand.type.is_complex:
5366 real_part = self.type.real_type.cast_code("__Pyx_CREAL(%s)" % operand_result)
5367 imag_part = self.type.real_type.cast_code("__Pyx_CIMAG(%s)" % operand_result)
5369 real_part = self.type.real_type.cast_code(operand_result)
5371 return "%s(%s, %s)" % (
5372 self.type.from_parts,
5376 return self.type.cast_code(self.operand.result())
5378 def get_constant_c_result_code(self):
5379 operand_result = self.operand.get_constant_c_result_code()
5381 return self.type.cast_code(operand_result)
5383 def result_as(self, type):
5384 if self.type.is_pyobject and not self.is_temp:
5385 # Optimise away some unnecessary casting
5386 return self.operand.result_as(type)
5388 return ExprNode.result_as(self, type)
5390 def generate_result_code(self, code):
5393 "%s = (PyObject *)%s;" % (
5395 self.operand.result()))
5396 code.put_incref(self.result(), self.ctype())
5399 class SizeofNode(ExprNode):
5400 # Abstract base class for sizeof(x) expression nodes.
5402 type = PyrexTypes.c_size_t_type
5404 def check_const(self):
5407 def generate_result_code(self, code):
5411 class SizeofTypeNode(SizeofNode):
5412 # C sizeof function applied to a type
5414 # base_type CBaseTypeNode
5415 # declarator CDeclaratorNode
5420 def analyse_types(self, env):
5421 # we may have incorrectly interpreted a dotted name as a type rather than an attribute
5422 # this could be better handled by more uniformly treating types as runtime-available objects
5423 if 0 and self.base_type.module_path:
5424 path = self.base_type.module_path
5425 obj = env.lookup(path[0])
5426 if obj.as_module is None:
5427 operand = NameNode(pos=self.pos, name=path[0])
5428 for attr in path[1:]:
5429 operand = AttributeNode(pos=self.pos, obj=operand, attribute=attr)
5430 operand = AttributeNode(pos=self.pos, obj=operand, attribute=self.base_type.name)
5431 self.operand = operand
5432 self.__class__ = SizeofVarNode
5433 self.analyse_types(env)
5435 if self.arg_type is None:
5436 base_type = self.base_type.analyse(env)
5437 _, arg_type = self.declarator.analyse(base_type, env)
5438 self.arg_type = arg_type
5441 def check_type(self):
5442 arg_type = self.arg_type
5443 if arg_type.is_pyobject and not arg_type.is_extension_type:
5444 error(self.pos, "Cannot take sizeof Python object")
5445 elif arg_type.is_void:
5446 error(self.pos, "Cannot take sizeof void")
5447 elif not arg_type.is_complete():
5448 error(self.pos, "Cannot take sizeof incomplete type '%s'" % arg_type)
5450 def calculate_result_code(self):
5451 if self.arg_type.is_extension_type:
5452 # the size of the pointer is boring
5453 # we want the size of the actual struct
5454 arg_code = self.arg_type.declaration_code("", deref=1)
5456 arg_code = self.arg_type.declaration_code("")
5457 return "(sizeof(%s))" % arg_code
5460 class SizeofVarNode(SizeofNode):
5461 # C sizeof function applied to a variable
5465 subexprs = ['operand']
5467 def analyse_types(self, env):
5468 # We may actually be looking at a type rather than a variable...
5469 # If we are, traditional analysis would fail...
5470 operand_as_type = self.operand.analyse_as_type(env)
5472 self.arg_type = operand_as_type
5473 self.__class__ = SizeofTypeNode
5476 self.operand.analyse_types(env)
5478 def calculate_result_code(self):
5479 return "(sizeof(%s))" % self.operand.result()
5481 def generate_result_code(self, code):
5484 class TypeofNode(ExprNode):
5485 # Compile-time type of an expression, as a string.
5488 # literal StringNode # internal
5491 type = py_object_type
5493 subexprs = ['literal'] # 'operand' will be ignored after type analysis!
5495 def analyse_types(self, env):
5496 self.operand.analyse_types(env)
5497 self.literal = StringNode(
5498 self.pos, value=StringEncoding.EncodedString(str(self.operand.type)))
5499 self.literal.analyse_types(env)
5500 self.literal = self.literal.coerce_to_pyobject(env)
5502 def may_be_none(self):
5505 def generate_evaluation_code(self, code):
5506 self.literal.generate_evaluation_code(code)
5508 def calculate_result_code(self):
5509 return self.literal.calculate_result_code()
5511 #-------------------------------------------------------------------
5513 # Binary operator nodes
5515 #-------------------------------------------------------------------
5517 def _not_in(x, seq):
5520 compile_time_binary_operators = {
5528 'is_not': operator.is_not,
5531 '/': operator.truediv,
5532 '//': operator.floordiv,
5533 '<<': operator.lshift,
5538 '>>': operator.rshift,
5541 'in': operator.contains,
5545 def get_compile_time_binop(node):
5546 func = compile_time_binary_operators.get(node.operator)
5549 "Binary '%s' not supported in compile-time expression"
5553 class BinopNode(ExprNode):
5558 # Processing during analyse_expressions phase:
5560 # analyse_c_operation
5561 # Called when neither operand is a pyobject.
5562 # - Check operand types and coerce if needed.
5563 # - Determine result type and result code fragment.
5564 # - Allocate temporary for result if needed.
5566 subexprs = ['operand1', 'operand2']
5569 def calculate_constant_result(self):
5570 func = compile_time_binary_operators[self.operator]
5571 self.constant_result = func(
5572 self.operand1.constant_result,
5573 self.operand2.constant_result)
5575 def compile_time_value(self, denv):
5576 func = get_compile_time_binop(self)
5577 operand1 = self.operand1.compile_time_value(denv)
5578 operand2 = self.operand2.compile_time_value(denv)
5580 return func(operand1, operand2)
5581 except Exception, e:
5582 self.compile_time_value_error(e)
5584 def infer_type(self, env):
5585 return self.result_type(self.operand1.infer_type(env),
5586 self.operand2.infer_type(env))
5588 def analyse_types(self, env):
5589 self.operand1.analyse_types(env)
5590 self.operand2.analyse_types(env)
5591 self.analyse_operation(env)
5593 def analyse_operation(self, env):
5594 if self.is_py_operation():
5595 self.coerce_operands_to_pyobjects(env)
5596 self.type = self.result_type(self.operand1.type,
5598 assert self.type.is_pyobject
5600 elif self.is_cpp_operation():
5601 self.analyse_cpp_operation(env)
5603 self.analyse_c_operation(env)
5605 def is_py_operation(self):
5606 return self.is_py_operation_types(self.operand1.type, self.operand2.type)
5608 def is_py_operation_types(self, type1, type2):
5609 return type1.is_pyobject or type2.is_pyobject
5611 def is_cpp_operation(self):
5612 return (self.operand1.type.is_cpp_class
5613 or self.operand2.type.is_cpp_class)
5615 def analyse_cpp_operation(self, env):
5616 type1 = self.operand1.type
5617 type2 = self.operand2.type
5618 entry = env.lookup_operator(self.operator, [self.operand1, self.operand2])
5622 func_type = entry.type
5623 if func_type.is_ptr:
5624 func_type = func_type.base_type
5625 if len(func_type.args) == 1:
5626 self.operand2 = self.operand2.coerce_to(func_type.args[0].type, env)
5628 self.operand1 = self.operand1.coerce_to(func_type.args[0].type, env)
5629 self.operand2 = self.operand2.coerce_to(func_type.args[1].type, env)
5630 self.type = func_type.return_type
5632 def result_type(self, type1, type2):
5633 if self.is_py_operation_types(type1, type2):
5635 type2 = Builtin.bytes_type
5637 type1 = Builtin.bytes_type
5638 elif self.operator == '%' \
5639 and type1 in (Builtin.str_type, Builtin.unicode_type):
5640 # note that b'%s' % b'abc' doesn't work in Py3
5642 if type1.is_builtin_type:
5644 if self.operator in '**%+|&^':
5645 # FIXME: at least these operators should be safe - others?
5647 elif self.operator == '*':
5648 if type1 in (Builtin.bytes_type, Builtin.str_type, Builtin.unicode_type):
5650 # multiplication of containers/numbers with an
5651 # integer value always (?) returns the same type
5654 elif type2.is_builtin_type and type1.is_int and self.operator == '*':
5655 # multiplication of containers/numbers with an
5656 # integer value always (?) returns the same type
5658 return py_object_type
5660 return self.compute_c_result_type(type1, type2)
5662 def nogil_check(self, env):
5663 if self.is_py_operation():
5666 def coerce_operands_to_pyobjects(self, env):
5667 self.operand1 = self.operand1.coerce_to_pyobject(env)
5668 self.operand2 = self.operand2.coerce_to_pyobject(env)
5670 def check_const(self):
5671 return self.operand1.check_const() and self.operand2.check_const()
5673 def generate_result_code(self, code):
5674 #print "BinopNode.generate_result_code:", self.operand1, self.operand2 ###
5675 if self.operand1.type.is_pyobject:
5676 function = self.py_operation_function()
5677 if self.operator == '**':
5678 extra_args = ", Py_None"
5682 "%s = %s(%s, %s%s); %s" % (
5685 self.operand1.py_result(),
5686 self.operand2.py_result(),
5688 code.error_goto_if_null(self.result(), self.pos)))
5689 code.put_gotref(self.py_result())
5691 def type_error(self):
5692 if not (self.operand1.type.is_error
5693 or self.operand2.type.is_error):
5694 error(self.pos, "Invalid operand types for '%s' (%s; %s)" %
5695 (self.operator, self.operand1.type,
5696 self.operand2.type))
5697 self.type = PyrexTypes.error_type
5700 class CBinopNode(BinopNode):
5702 def analyse_types(self, env):
5703 BinopNode.analyse_types(self, env)
5704 if self.is_py_operation():
5705 self.type = PyrexTypes.error_type
5707 def py_operation_function():
5710 def calculate_result_code(self):
5711 return "(%s %s %s)" % (
5712 self.operand1.result(),
5714 self.operand2.result())
5717 def c_binop_constructor(operator):
5718 def make_binop_node(pos, **operands):
5719 return CBinopNode(pos, operator=operator, **operands)
5720 return make_binop_node
5722 class NumBinopNode(BinopNode):
5723 # Binary operation taking numeric arguments.
5727 def analyse_c_operation(self, env):
5728 type1 = self.operand1.type
5729 type2 = self.operand2.type
5730 self.type = self.compute_c_result_type(type1, type2)
5734 if self.type.is_complex:
5736 if not self.infix or (type1.is_numeric and type2.is_numeric):
5737 self.operand1 = self.operand1.coerce_to(self.type, env)
5738 self.operand2 = self.operand2.coerce_to(self.type, env)
5740 def compute_c_result_type(self, type1, type2):
5741 if self.c_types_okay(type1, type2):
5742 widest_type = PyrexTypes.widest_numeric_type(type1, type2)
5743 if widest_type is PyrexTypes.c_bint_type:
5744 if self.operator not in '|^&':
5745 # False + False == 0 # not False!
5746 widest_type = PyrexTypes.c_int_type
5751 def get_constant_c_result_code(self):
5752 value1 = self.operand1.get_constant_c_result_code()
5753 value2 = self.operand2.get_constant_c_result_code()
5754 if value1 and value2:
5755 return "(%s %s %s)" % (value1, self.operator, value2)
5759 def c_types_okay(self, type1, type2):
5760 #print "NumBinopNode.c_types_okay:", type1, type2 ###
5761 return (type1.is_numeric or type1.is_enum) \
5762 and (type2.is_numeric or type2.is_enum)
5764 def calculate_result_code(self):
5766 return "(%s %s %s)" % (
5767 self.operand1.result(),
5769 self.operand2.result())
5771 func = self.type.binary_op(self.operator)
5773 error(self.pos, "binary operator %s not supported for %s" % (self.operator, self.type))
5774 return "%s(%s, %s)" % (
5776 self.operand1.result(),
5777 self.operand2.result())
5779 def is_py_operation_types(self, type1, type2):
5780 return (type1 is PyrexTypes.c_py_unicode_type or
5781 type2 is PyrexTypes.c_py_unicode_type or
5782 BinopNode.is_py_operation_types(self, type1, type2))
5784 def py_operation_function(self):
5785 fuction = self.py_functions[self.operator]
5787 fuction = fuction.replace('PyNumber_', 'PyNumber_InPlace')
5792 "^": "PyNumber_Xor",
5793 "&": "PyNumber_And",
5794 "<<": "PyNumber_Lshift",
5795 ">>": "PyNumber_Rshift",
5796 "+": "PyNumber_Add",
5797 "-": "PyNumber_Subtract",
5798 "*": "PyNumber_Multiply",
5799 "/": "__Pyx_PyNumber_Divide",
5800 "//": "PyNumber_FloorDivide",
5801 "%": "PyNumber_Remainder",
5802 "**": "PyNumber_Power"
5805 class IntBinopNode(NumBinopNode):
5806 # Binary operation taking integer arguments.
5808 def c_types_okay(self, type1, type2):
5809 #print "IntBinopNode.c_types_okay:", type1, type2 ###
5810 return (type1.is_int or type1.is_enum) \
5811 and (type2.is_int or type2.is_enum)
5814 class AddNode(NumBinopNode):
5817 def is_py_operation_types(self, type1, type2):
5818 if type1.is_string and type2.is_string:
5821 return NumBinopNode.is_py_operation_types(self, type1, type2)
5823 def compute_c_result_type(self, type1, type2):
5824 #print "AddNode.compute_c_result_type:", type1, self.operator, type2 ###
5825 if (type1.is_ptr or type1.is_array) and (type2.is_int or type2.is_enum):
5827 elif (type2.is_ptr or type2.is_array) and (type1.is_int or type1.is_enum):
5830 return NumBinopNode.compute_c_result_type(
5834 class SubNode(NumBinopNode):
5837 def compute_c_result_type(self, type1, type2):
5838 if (type1.is_ptr or type1.is_array) and (type2.is_int or type2.is_enum):
5840 elif (type1.is_ptr or type1.is_array) and (type2.is_ptr or type2.is_array):
5841 return PyrexTypes.c_int_type
5843 return NumBinopNode.compute_c_result_type(
5847 class MulNode(NumBinopNode):
5850 def is_py_operation_types(self, type1, type2):
5851 if (type1.is_string and type2.is_int) \
5852 or (type2.is_string and type1.is_int):
5855 return NumBinopNode.is_py_operation_types(self, type1, type2)
5858 class DivNode(NumBinopNode):
5859 # '/' or '//' operator.
5862 truedivision = None # == "unknown" if operator == '/'
5863 ctruedivision = False
5864 cdivision_warnings = False
5865 zerodivision_check = None
5867 def find_compile_time_binary_operator(self, op1, op2):
5868 func = compile_time_binary_operators[self.operator]
5869 if self.operator == '/' and self.truedivision is None:
5870 # => true div for floats, floor div for integers
5871 if isinstance(op1, (int,long)) and isinstance(op2, (int,long)):
5872 func = compile_time_binary_operators['//']
5875 def calculate_constant_result(self):
5876 op1 = self.operand1.constant_result
5877 op2 = self.operand2.constant_result
5878 func = self.find_compile_time_binary_operator(op1, op2)
5879 self.constant_result = func(
5880 self.operand1.constant_result,
5881 self.operand2.constant_result)
5883 def compile_time_value(self, denv):
5884 operand1 = self.operand1.compile_time_value(denv)
5885 operand2 = self.operand2.compile_time_value(denv)
5887 func = self.find_compile_time_binary_operator(
5888 self, operand1, operand2)
5889 return func(operand1, operand2)
5890 except Exception, e:
5891 self.compile_time_value_error(e)
5893 def analyse_operation(self, env):
5894 if self.cdivision or env.directives['cdivision']:
5895 self.ctruedivision = False
5897 self.ctruedivision = self.truedivision
5898 NumBinopNode.analyse_operation(self, env)
5899 if self.is_cpp_operation():
5900 self.cdivision = True
5901 if not self.type.is_pyobject:
5902 self.zerodivision_check = (
5903 self.cdivision is None and not env.directives['cdivision']
5904 and (not self.operand2.has_constant_result() or
5905 self.operand2.constant_result == 0))
5906 if self.zerodivision_check or env.directives['cdivision_warnings']:
5907 # Need to check ahead of time to warn or raise zero division error
5908 self.operand1 = self.operand1.coerce_to_simple(env)
5909 self.operand2 = self.operand2.coerce_to_simple(env)
5911 error(self.pos, "Pythonic division not allowed without gil, consider using cython.cdivision(True)")
5913 def compute_c_result_type(self, type1, type2):
5914 if self.operator == '/' and self.ctruedivision:
5915 if not type1.is_float and not type2.is_float:
5916 widest_type = PyrexTypes.widest_numeric_type(type1, PyrexTypes.c_double_type)
5917 widest_type = PyrexTypes.widest_numeric_type(type2, widest_type)
5919 return NumBinopNode.compute_c_result_type(self, type1, type2)
5921 def zero_division_message(self):
5922 if self.type.is_int:
5923 return "integer division or modulo by zero"
5925 return "float division"
5927 def generate_evaluation_code(self, code):
5928 if not self.type.is_pyobject and not self.type.is_complex:
5929 if self.cdivision is None:
5930 self.cdivision = (code.globalstate.directives['cdivision']
5931 or not self.type.signed
5932 or self.type.is_float)
5933 if not self.cdivision:
5934 code.globalstate.use_utility_code(div_int_utility_code.specialize(self.type))
5935 NumBinopNode.generate_evaluation_code(self, code)
5936 self.generate_div_warning_code(code)
5938 def generate_div_warning_code(self, code):
5939 if not self.type.is_pyobject:
5940 if self.zerodivision_check:
5942 zero_test = "%s(%s)" % (self.type.unary_op('zero'), self.operand2.result())
5944 zero_test = "%s == 0" % self.operand2.result()
5945 code.putln("if (unlikely(%s)) {" % zero_test)
5946 code.putln('PyErr_Format(PyExc_ZeroDivisionError, "%s");' % self.zero_division_message())
5947 code.putln(code.error_goto(self.pos))
5949 if self.type.is_int and self.type.signed and self.operator != '%':
5950 code.globalstate.use_utility_code(division_overflow_test_code)
5951 code.putln("else if (sizeof(%s) == sizeof(long) && unlikely(%s == -1) && unlikely(UNARY_NEG_WOULD_OVERFLOW(%s))) {" % (
5952 self.type.declaration_code(''),
5953 self.operand2.result(),
5954 self.operand1.result()))
5955 code.putln('PyErr_Format(PyExc_OverflowError, "value too large to perform division");')
5956 code.putln(code.error_goto(self.pos))
5958 if code.globalstate.directives['cdivision_warnings'] and self.operator != '/':
5959 code.globalstate.use_utility_code(cdivision_warning_utility_code)
5960 code.putln("if ((%s < 0) ^ (%s < 0)) {" % (
5961 self.operand1.result(),
5962 self.operand2.result()))
5963 code.putln(code.set_error_info(self.pos));
5964 code.put("if (__Pyx_cdivision_warning()) ")
5965 code.put_goto(code.error_label)
5968 def calculate_result_code(self):
5969 if self.type.is_complex:
5970 return NumBinopNode.calculate_result_code(self)
5971 elif self.type.is_float and self.operator == '//':
5972 return "floor(%s / %s)" % (
5973 self.operand1.result(),
5974 self.operand2.result())
5975 elif self.truedivision or self.cdivision:
5976 op1 = self.operand1.result()
5977 op2 = self.operand2.result()
5978 if self.truedivision:
5979 if self.type != self.operand1.type:
5980 op1 = self.type.cast_code(op1)
5981 if self.type != self.operand2.type:
5982 op2 = self.type.cast_code(op2)
5983 return "(%s / %s)" % (op1, op2)
5985 return "__Pyx_div_%s(%s, %s)" % (
5986 self.type.specialization_name(),
5987 self.operand1.result(),
5988 self.operand2.result())
5991 class ModNode(DivNode):
5994 def is_py_operation_types(self, type1, type2):
5995 return (type1.is_string
5997 or NumBinopNode.is_py_operation_types(self, type1, type2))
5999 def zero_division_message(self):
6000 if self.type.is_int:
6001 return "integer division or modulo by zero"
6003 return "float divmod()"
6005 def generate_evaluation_code(self, code):
6006 if not self.type.is_pyobject:
6007 if self.cdivision is None:
6008 self.cdivision = code.globalstate.directives['cdivision'] or not self.type.signed
6009 if not self.cdivision:
6010 if self.type.is_int:
6011 code.globalstate.use_utility_code(mod_int_utility_code.specialize(self.type))
6013 code.globalstate.use_utility_code(
6014 mod_float_utility_code.specialize(self.type, math_h_modifier=self.type.math_h_modifier))
6015 NumBinopNode.generate_evaluation_code(self, code)
6016 self.generate_div_warning_code(code)
6018 def calculate_result_code(self):
6020 if self.type.is_float:
6021 return "fmod%s(%s, %s)" % (
6022 self.type.math_h_modifier,
6023 self.operand1.result(),
6024 self.operand2.result())
6026 return "(%s %% %s)" % (
6027 self.operand1.result(),
6028 self.operand2.result())
6030 return "__Pyx_mod_%s(%s, %s)" % (
6031 self.type.specialization_name(),
6032 self.operand1.result(),
6033 self.operand2.result())
6035 class PowNode(NumBinopNode):
6038 def analyse_c_operation(self, env):
6039 NumBinopNode.analyse_c_operation(self, env)
6040 if self.type.is_complex:
6041 if self.type.real_type.is_float:
6042 self.operand1 = self.operand1.coerce_to(self.type, env)
6043 self.operand2 = self.operand2.coerce_to(self.type, env)
6044 self.pow_func = "__Pyx_c_pow" + self.type.real_type.math_h_modifier
6046 error(self.pos, "complex int powers not supported")
6047 self.pow_func = "<error>"
6048 elif self.type.is_float:
6049 self.pow_func = "pow" + self.type.math_h_modifier
6051 self.pow_func = "__Pyx_pow_%s" % self.type.declaration_code('').replace(' ', '_')
6052 env.use_utility_code(
6053 int_pow_utility_code.specialize(func_name=self.pow_func,
6054 type=self.type.declaration_code('')))
6056 def calculate_result_code(self):
6057 # Work around MSVC overloading ambiguity.
6058 def typecast(operand):
6059 if self.type == operand.type:
6060 return operand.result()
6062 return self.type.cast_code(operand.result())
6063 return "%s(%s, %s)" % (
6065 typecast(self.operand1),
6066 typecast(self.operand2))
6069 # Note: This class is temporarily "shut down" into an ineffective temp
6072 # More sophisticated temp reuse was going on before, one could have a
6073 # look at adding this again after /all/ classes are converted to the
6074 # new temp scheme. (The temp juggling cannot work otherwise).
6075 class BoolBinopNode(ExprNode):
6076 # Short-circuiting boolean operation.
6082 subexprs = ['operand1', 'operand2']
6084 def infer_type(self, env):
6085 type1 = self.operand1.infer_type(env)
6086 type2 = self.operand2.infer_type(env)
6087 return PyrexTypes.independent_spanning_type(type1, type2)
6089 def may_be_none(self):
6090 if self.operator == 'or':
6091 return self.operand2.may_be_none()
6093 return self.operand1.may_be_none() or self.operand2.may_be_none()
6095 def calculate_constant_result(self):
6096 if self.operator == 'and':
6097 self.constant_result = \
6098 self.operand1.constant_result and \
6099 self.operand2.constant_result
6101 self.constant_result = \
6102 self.operand1.constant_result or \
6103 self.operand2.constant_result
6105 def compile_time_value(self, denv):
6106 if self.operator == 'and':
6107 return self.operand1.compile_time_value(denv) \
6108 and self.operand2.compile_time_value(denv)
6110 return self.operand1.compile_time_value(denv) \
6111 or self.operand2.compile_time_value(denv)
6113 def coerce_to_boolean(self, env):
6114 return BoolBinopNode(
6116 operator = self.operator,
6117 operand1 = self.operand1.coerce_to_boolean(env),
6118 operand2 = self.operand2.coerce_to_boolean(env),
6119 type = PyrexTypes.c_bint_type,
6120 is_temp = self.is_temp)
6122 def analyse_types(self, env):
6123 self.operand1.analyse_types(env)
6124 self.operand2.analyse_types(env)
6125 self.type = PyrexTypes.independent_spanning_type(self.operand1.type, self.operand2.type)
6126 self.operand1 = self.operand1.coerce_to(self.type, env)
6127 self.operand2 = self.operand2.coerce_to(self.type, env)
6129 # For what we're about to do, it's vital that
6130 # both operands be temp nodes.
6131 self.operand1 = self.operand1.coerce_to_simple(env)
6132 self.operand2 = self.operand2.coerce_to_simple(env)
6135 gil_message = "Truth-testing Python object"
6137 def check_const(self):
6138 return self.operand1.check_const() and self.operand2.check_const()
6140 def generate_evaluation_code(self, code):
6141 code.mark_pos(self.pos)
6142 self.operand1.generate_evaluation_code(code)
6143 test_result, uses_temp = self.generate_operand1_test(code)
6144 if self.operator == 'and':
6153 code.funcstate.release_temp(test_result)
6154 self.operand1.generate_disposal_code(code)
6155 self.operand2.generate_evaluation_code(code)
6156 self.allocate_temp_result(code)
6157 self.operand2.make_owned_reference(code)
6158 code.putln("%s = %s;" % (self.result(), self.operand2.result()))
6159 self.operand2.generate_post_assignment_code(code)
6160 self.operand2.free_temps(code)
6161 code.putln("} else {")
6162 self.operand1.make_owned_reference(code)
6163 code.putln("%s = %s;" % (self.result(), self.operand1.result()))
6164 self.operand1.generate_post_assignment_code(code)
6165 self.operand1.free_temps(code)
6168 def generate_operand1_test(self, code):
6169 # Generate code to test the truth of the first operand.
6170 if self.type.is_pyobject:
6171 test_result = code.funcstate.allocate_temp(PyrexTypes.c_bint_type,
6174 "%s = __Pyx_PyObject_IsTrue(%s); %s" % (
6176 self.operand1.py_result(),
6177 code.error_goto_if_neg(test_result, self.pos)))
6179 test_result = self.operand1.result()
6180 return (test_result, self.type.is_pyobject)
6183 class CondExprNode(ExprNode):
6184 # Short-circuiting conditional expression.
6188 # false_val ExprNode
6193 subexprs = ['test', 'true_val', 'false_val']
6195 def type_dependencies(self, env):
6196 return self.true_val.type_dependencies(env) + self.false_val.type_dependencies(env)
6198 def infer_type(self, env):
6199 return PyrexTypes.independent_spanning_type(self.true_val.infer_type(env),
6200 self.false_val.infer_type(env))
6202 def calculate_constant_result(self):
6203 if self.test.constant_result:
6204 self.constant_result = self.true_val.constant_result
6206 self.constant_result = self.false_val.constant_result
6208 def analyse_types(self, env):
6209 self.test.analyse_types(env)
6210 self.test = self.test.coerce_to_boolean(env)
6211 self.true_val.analyse_types(env)
6212 self.false_val.analyse_types(env)
6213 self.type = PyrexTypes.independent_spanning_type(self.true_val.type, self.false_val.type)
6214 if self.true_val.type.is_pyobject or self.false_val.type.is_pyobject:
6215 self.true_val = self.true_val.coerce_to(self.type, env)
6216 self.false_val = self.false_val.coerce_to(self.type, env)
6218 if self.type == PyrexTypes.error_type:
6221 def type_error(self):
6222 if not (self.true_val.type.is_error or self.false_val.type.is_error):
6223 error(self.pos, "Incompatable types in conditional expression (%s; %s)" %
6224 (self.true_val.type, self.false_val.type))
6225 self.type = PyrexTypes.error_type
6227 def check_const(self):
6228 return (self.test.check_const()
6229 and self.true_val.check_const()
6230 and self.false_val.check_const())
6232 def generate_evaluation_code(self, code):
6233 # Because subexprs may not be evaluated we can use a more optimal
6234 # subexpr allocation strategy than the default, so override evaluation_code.
6236 code.mark_pos(self.pos)
6237 self.allocate_temp_result(code)
6238 self.test.generate_evaluation_code(code)
6239 code.putln("if (%s) {" % self.test.result() )
6240 self.eval_and_get(code, self.true_val)
6241 code.putln("} else {")
6242 self.eval_and_get(code, self.false_val)
6244 self.test.generate_disposal_code(code)
6245 self.test.free_temps(code)
6247 def eval_and_get(self, code, expr):
6248 expr.generate_evaluation_code(code)
6249 expr.make_owned_reference(code)
6250 code.putln("%s = %s;" % (self.result(), expr.result()))
6251 expr.generate_post_assignment_code(code)
6252 expr.free_temps(code)
6254 richcmp_constants = {
6264 class CmpNode(object):
6265 # Mixin class containing code common to PrimaryCmpNodes
6266 # and CascadedCmpNodes.
6268 special_bool_cmp_function = None
6270 def infer_type(self, env):
6271 # TODO: Actually implement this (after merging with -unstable).
6272 return py_object_type
6274 def calculate_cascaded_constant_result(self, operand1_result):
6275 func = compile_time_binary_operators[self.operator]
6276 operand2_result = self.operand2.constant_result
6277 result = func(operand1_result, operand2_result)
6279 self.cascade.calculate_cascaded_constant_result(operand2_result)
6280 if self.cascade.constant_result:
6281 self.constant_result = result and self.cascade.constant_result
6283 self.constant_result = result
6285 def cascaded_compile_time_value(self, operand1, denv):
6286 func = get_compile_time_binop(self)
6287 operand2 = self.operand2.compile_time_value(denv)
6289 result = func(operand1, operand2)
6290 except Exception, e:
6291 self.compile_time_value_error(e)
6294 cascade = self.cascade
6296 # FIXME: I bet this must call cascaded_compile_time_value()
6297 result = result and cascade.cascaded_compile_time_value(operand2, denv)
6300 def is_cpp_comparison(self):
6301 return self.operand1.type.is_cpp_class or self.operand2.type.is_cpp_class
6303 def find_common_int_type(self, env, op, operand1, operand2):
6304 # type1 != type2 and at least one of the types is not a C int
6305 type1 = operand1.type
6306 type2 = operand2.type
6307 type1_can_be_int = False
6308 type2_can_be_int = False
6310 if isinstance(operand1, (StringNode, BytesNode, UnicodeNode)) \
6311 and operand1.can_coerce_to_char_literal():
6312 type1_can_be_int = True
6313 if isinstance(operand2, (StringNode, BytesNode, UnicodeNode)) \
6314 and operand2.can_coerce_to_char_literal():
6315 type2_can_be_int = True
6318 if type2_can_be_int:
6321 if type1_can_be_int:
6323 elif type1_can_be_int:
6324 if type2_can_be_int:
6325 return PyrexTypes.c_uchar_type
6329 def find_common_type(self, env, op, operand1, common_type=None):
6330 operand2 = self.operand2
6331 type1 = operand1.type
6332 type2 = operand2.type
6334 new_common_type = None
6336 # catch general errors
6337 if type1 == str_type and (type2.is_string or type2 in (bytes_type, unicode_type)) or \
6338 type2 == str_type and (type1.is_string or type1 in (bytes_type, unicode_type)):
6339 error(self.pos, "Comparisons between bytes/unicode and str are not portable to Python 3")
6340 new_common_type = error_type
6342 # try to use numeric comparisons where possible
6343 elif type1.is_complex or type2.is_complex:
6344 if op not in ('==', '!='):
6345 error(self.pos, "complex types are unordered")
6346 new_common_type = error_type
6347 if type1.is_pyobject:
6348 new_common_type = type1
6349 elif type2.is_pyobject:
6350 new_common_type = type2
6352 new_common_type = PyrexTypes.widest_numeric_type(type1, type2)
6353 elif type1.is_numeric and type2.is_numeric:
6354 new_common_type = PyrexTypes.widest_numeric_type(type1, type2)
6355 elif common_type is None or not common_type.is_pyobject:
6356 new_common_type = self.find_common_int_type(env, op, operand1, operand2)
6358 if new_common_type is None:
6359 # fall back to generic type compatibility tests
6361 new_common_type = type1
6362 elif type1.is_pyobject or type2.is_pyobject:
6363 if type2.is_numeric or type2.is_string:
6364 if operand2.check_for_coercion_error(type1):
6365 new_common_type = error_type
6367 new_common_type = py_object_type
6368 elif type1.is_numeric or type1.is_string:
6369 if operand1.check_for_coercion_error(type2):
6370 new_common_type = error_type
6372 new_common_type = py_object_type
6373 elif py_object_type.assignable_from(type1) and py_object_type.assignable_from(type2):
6374 new_common_type = py_object_type
6376 # one Python type and one non-Python type, not assignable
6377 self.invalid_types_error(operand1, op, operand2)
6378 new_common_type = error_type
6379 elif type1.assignable_from(type2):
6380 new_common_type = type1
6381 elif type2.assignable_from(type1):
6382 new_common_type = type2
6384 # C types that we couldn't handle up to here are an error
6385 self.invalid_types_error(operand1, op, operand2)
6386 new_common_type = error_type
6388 if new_common_type.is_string and (isinstance(operand1, BytesNode) or
6389 isinstance(operand2, BytesNode)):
6390 # special case when comparing char* to bytes literal: must
6391 # compare string values!
6392 new_common_type = bytes_type
6394 # recursively merge types
6395 if common_type is None or new_common_type.is_error:
6396 common_type = new_common_type
6398 # we could do a lot better by splitting the comparison
6399 # into a non-Python part and a Python part, but this is
6401 common_type = PyrexTypes.spanning_type(common_type, new_common_type)
6404 common_type = self.cascade.find_common_type(env, self.operator, operand2, common_type)
6408 def invalid_types_error(self, operand1, op, operand2):
6409 error(self.pos, "Invalid types for '%s' (%s, %s)" %
6410 (op, operand1.type, operand2.type))
6412 def is_python_comparison(self):
6413 return (not self.is_ptr_contains()
6414 and not self.is_c_string_contains()
6415 and (self.has_python_operands()
6416 or (self.cascade and self.cascade.is_python_comparison())
6417 or self.operator in ('in', 'not_in')))
6419 def coerce_operands_to(self, dst_type, env):
6420 operand2 = self.operand2
6421 if operand2.type != dst_type:
6422 self.operand2 = operand2.coerce_to(dst_type, env)
6424 self.cascade.coerce_operands_to(dst_type, env)
6426 def is_python_result(self):
6427 return ((self.has_python_operands() and
6428 self.special_bool_cmp_function is None and
6429 self.operator not in ('is', 'is_not', 'in', 'not_in') and
6430 not self.is_c_string_contains() and
6431 not self.is_ptr_contains())
6432 or (self.cascade and self.cascade.is_python_result()))
6434 def is_c_string_contains(self):
6435 return self.operator in ('in', 'not_in') and \
6436 ((self.operand1.type.is_int
6437 and (self.operand2.type.is_string or self.operand2.type is bytes_type)) or
6438 (self.operand1.type is PyrexTypes.c_py_unicode_type
6439 and self.operand2.type is unicode_type))
6441 def is_ptr_contains(self):
6442 if self.operator in ('in', 'not_in'):
6443 container_type = self.operand2.type
6444 return (container_type.is_ptr or container_type.is_array) \
6445 and not container_type.is_string
6447 def find_special_bool_compare_function(self, env):
6448 if self.operator in ('==', '!='):
6449 type1, type2 = self.operand1.type, self.operand2.type
6450 if type1.is_pyobject and type2.is_pyobject:
6451 if type1 is Builtin.unicode_type or type2 is Builtin.unicode_type:
6452 env.use_utility_code(pyunicode_equals_utility_code)
6453 self.special_bool_cmp_function = "__Pyx_PyUnicode_Equals"
6457 def generate_operation_code(self, code, result_code,
6458 operand1, op , operand2):
6459 if self.type.is_pyobject:
6460 coerce_result = "__Pyx_PyBool_FromLong"
6467 if self.special_bool_cmp_function:
6468 if operand1.type.is_pyobject:
6469 result1 = operand1.py_result()
6471 result1 = operand1.result()
6472 if operand2.type.is_pyobject:
6473 result2 = operand2.py_result()
6475 result2 = operand2.result()
6476 code.putln("%s = %s(%s, %s, %s); %s" % (
6478 self.special_bool_cmp_function,
6481 richcmp_constants[op],
6482 code.error_goto_if_neg(result_code, self.pos)))
6483 elif op == 'in' or op == 'not_in':
6484 code.globalstate.use_utility_code(contains_utility_code)
6485 if self.type.is_pyobject:
6486 coerce_result = "__Pyx_PyBoolOrNull_FromLong"
6488 negation = "__Pyx_NegateNonNeg"
6489 if operand2.type is dict_type:
6490 method = "PyDict_Contains"
6492 method = "PySequence_Contains"
6493 if self.type.is_pyobject:
6494 error_clause = code.error_goto_if_null
6495 got_ref = "__Pyx_XGOTREF(%s); " % result_code
6497 error_clause = code.error_goto_if_neg
6500 "%s = %s(%s(%s(%s, %s))); %s%s" % (
6505 operand2.py_result(),
6506 operand1.py_result(),
6508 error_clause(result_code, self.pos)))
6509 elif (operand1.type.is_pyobject
6510 and op not in ('is', 'is_not')):
6511 code.putln("%s = PyObject_RichCompare(%s, %s, %s); %s" % (
6513 operand1.py_result(),
6514 operand2.py_result(),
6515 richcmp_constants[op],
6516 code.error_goto_if_null(result_code, self.pos)))
6517 code.put_gotref(result_code)
6518 elif operand1.type.is_complex:
6523 code.putln("%s = %s(%s%s(%s, %s));" % (
6527 operand1.type.unary_op('eq'),
6531 type1 = operand1.type
6532 type2 = operand2.type
6533 if (type1.is_extension_type or type2.is_extension_type) \
6534 and not type1.same_as(type2):
6535 common_type = py_object_type
6536 elif type1.is_numeric:
6537 common_type = PyrexTypes.widest_numeric_type(type1, type2)
6540 code1 = operand1.result_as(common_type)
6541 code2 = operand2.result_as(common_type)
6542 code.putln("%s = %s(%s %s %s);" % (
6546 self.c_operator(op),
6549 def c_operator(self, op):
6552 elif op == 'is_not':
6557 contains_utility_code = UtilityCode(
6559 static CYTHON_INLINE long __Pyx_NegateNonNeg(long b) { return unlikely(b < 0) ? b : !b; }
6560 static CYTHON_INLINE PyObject* __Pyx_PyBoolOrNull_FromLong(long b) {
6561 return unlikely(b < 0) ? NULL : __Pyx_PyBool_FromLong(b);
6565 char_in_bytes_utility_code = UtilityCode(
6567 static CYTHON_INLINE int __Pyx_BytesContains(PyObject* bytes, char character); /*proto*/
6570 static CYTHON_INLINE int __Pyx_BytesContains(PyObject* bytes, char character) {
6571 const Py_ssize_t length = PyBytes_GET_SIZE(bytes);
6572 char* char_start = PyBytes_AS_STRING(bytes);
6574 for (pos=char_start; pos < char_start+length; pos++) {
6575 if (character == pos[0]) return 1;
6581 pyunicode_in_unicode_utility_code = UtilityCode(
6583 static CYTHON_INLINE int __Pyx_UnicodeContains(PyObject* unicode, Py_UNICODE character); /*proto*/
6586 static CYTHON_INLINE int __Pyx_UnicodeContains(PyObject* unicode, Py_UNICODE character) {
6587 const Py_ssize_t length = PyUnicode_GET_SIZE(unicode);
6588 Py_UNICODE* char_start = PyUnicode_AS_UNICODE(unicode);
6590 for (pos=char_start; pos < char_start+length; pos++) {
6591 if (character == pos[0]) return 1;
6597 pyunicode_equals_utility_code = UtilityCode(
6599 static CYTHON_INLINE int __Pyx_PyUnicode_Equals(PyObject* s1, PyObject* s2, int equals); /*proto*/
6602 static CYTHON_INLINE int __Pyx_PyUnicode_Equals(PyObject* s1, PyObject* s2, int equals) {
6603 if (s1 == s2) { /* as done by PyObject_RichCompareBool(); also catches the (interned) empty string */
6604 return (equals == Py_EQ);
6605 } else if (PyUnicode_CheckExact(s1) & PyUnicode_CheckExact(s2)) {
6606 if (PyUnicode_GET_SIZE(s1) != PyUnicode_GET_SIZE(s2)) {
6607 return (equals == Py_NE);
6608 } else if (PyUnicode_GET_SIZE(s1) == 1) {
6609 if (equals == Py_EQ)
6610 return (PyUnicode_AS_UNICODE(s1)[0] == PyUnicode_AS_UNICODE(s2)[0]);
6612 return (PyUnicode_AS_UNICODE(s1)[0] != PyUnicode_AS_UNICODE(s2)[0]);
6614 int result = PyUnicode_Compare(s1, s2);
6615 if ((result == -1) && unlikely(PyErr_Occurred()))
6617 return (equals == Py_EQ) ? (result == 0) : (result != 0);
6619 } else if ((s1 == Py_None) & (s2 == Py_None)) {
6620 return (equals == Py_EQ);
6621 } else if ((s1 == Py_None) & PyUnicode_CheckExact(s2)) {
6622 return (equals == Py_NE);
6623 } else if ((s2 == Py_None) & PyUnicode_CheckExact(s1)) {
6624 return (equals == Py_NE);
6627 PyObject* py_result = PyObject_RichCompare(s1, s2, equals);
6630 result = __Pyx_PyObject_IsTrue(py_result);
6631 Py_DECREF(py_result);
6638 class PrimaryCmpNode(ExprNode, CmpNode):
6639 # Non-cascaded comparison or first comparison of
6640 # a cascaded sequence.
6645 # cascade CascadedCmpNode
6647 # We don't use the subexprs mechanism, because
6648 # things here are too complicated for it to handle.
6649 # Instead, we override all the framework methods
6652 child_attrs = ['operand1', 'operand2', 'cascade']
6656 def infer_type(self, env):
6657 # TODO: Actually implement this (after merging with -unstable).
6658 return py_object_type
6660 def type_dependencies(self, env):
6663 def calculate_constant_result(self):
6664 self.calculate_cascaded_constant_result(self.operand1.constant_result)
6666 def compile_time_value(self, denv):
6667 operand1 = self.operand1.compile_time_value(denv)
6668 return self.cascaded_compile_time_value(operand1, denv)
6670 def analyse_types(self, env):
6671 self.operand1.analyse_types(env)
6672 self.operand2.analyse_types(env)
6673 if self.is_cpp_comparison():
6674 self.analyse_cpp_comparison(env)
6676 error(self.pos, "Cascading comparison not yet supported for cpp types.")
6679 self.cascade.analyse_types(env)
6681 if self.operator in ('in', 'not_in'):
6682 if self.is_c_string_contains():
6683 self.is_pycmp = False
6686 error(self.pos, "Cascading comparison not yet supported for 'int_val in string'.")
6688 if self.operand2.type is unicode_type:
6689 env.use_utility_code(pyunicode_in_unicode_utility_code)
6691 if self.operand1.type is PyrexTypes.c_uchar_type:
6692 self.operand1 = self.operand1.coerce_to(PyrexTypes.c_char_type, env)
6693 if self.operand2.type is not bytes_type:
6694 self.operand2 = self.operand2.coerce_to(bytes_type, env)
6695 env.use_utility_code(char_in_bytes_utility_code)
6696 self.operand2 = self.operand2.as_none_safe_node(
6697 "argument of type 'NoneType' is not iterable")
6698 elif self.is_ptr_contains():
6700 error(self.pos, "Cascading comparison not yet supported for 'val in sliced pointer'.")
6701 self.type = PyrexTypes.c_bint_type
6702 # Will be transformed by IterationTransform
6705 if self.operand2.type is dict_type:
6706 self.operand2 = self.operand2.as_none_safe_node("'NoneType' object is not iterable")
6707 common_type = py_object_type
6708 self.is_pycmp = True
6709 elif self.find_special_bool_compare_function(env):
6710 common_type = None # if coercion needed, the method call above has already done it
6711 self.is_pycmp = False # result is bint
6712 self.is_temp = True # must check for error return
6714 common_type = self.find_common_type(env, self.operator, self.operand1)
6715 self.is_pycmp = common_type.is_pyobject
6717 if common_type is not None and not common_type.is_error:
6718 if self.operand1.type != common_type:
6719 self.operand1 = self.operand1.coerce_to(common_type, env)
6720 self.coerce_operands_to(common_type, env)
6723 self.operand2 = self.operand2.coerce_to_simple(env)
6724 self.cascade.coerce_cascaded_operands_to_temp(env)
6725 if self.is_python_result():
6726 self.type = PyrexTypes.py_object_type
6728 self.type = PyrexTypes.c_bint_type
6731 cdr.type = self.type
6733 if self.is_pycmp or self.cascade:
6736 def analyse_cpp_comparison(self, env):
6737 type1 = self.operand1.type
6738 type2 = self.operand2.type
6739 entry = env.lookup_operator(self.operator, [self.operand1, self.operand2])
6741 error(self.pos, "Invalid types for '%s' (%s, %s)" %
6742 (self.operator, type1, type2))
6743 self.type = PyrexTypes.error_type
6744 self.result_code = "<error>"
6746 func_type = entry.type
6747 if func_type.is_ptr:
6748 func_type = func_type.base_type
6749 if len(func_type.args) == 1:
6750 self.operand2 = self.operand2.coerce_to(func_type.args[0].type, env)
6752 self.operand1 = self.operand1.coerce_to(func_type.args[0].type, env)
6753 self.operand2 = self.operand2.coerce_to(func_type.args[1].type, env)
6754 self.type = func_type.return_type
6756 def has_python_operands(self):
6757 return (self.operand1.type.is_pyobject
6758 or self.operand2.type.is_pyobject)
6760 def check_const(self):
6765 return self.operand1.check_const() and self.operand2.check_const()
6767 def calculate_result_code(self):
6768 if self.operand1.type.is_complex:
6769 if self.operator == "!=":
6773 return "(%s%s(%s, %s))" % (
6775 self.operand1.type.binary_op('=='),
6776 self.operand1.result(),
6777 self.operand2.result())
6778 elif self.is_c_string_contains():
6779 if self.operand2.type is bytes_type:
6780 method = "__Pyx_BytesContains"
6782 method = "__Pyx_UnicodeContains"
6783 if self.operator == "not_in":
6787 return "(%s%s(%s, %s))" % (
6790 self.operand2.result(),
6791 self.operand1.result())
6793 return "(%s %s %s)" % (
6794 self.operand1.result(),
6795 self.c_operator(self.operator),
6796 self.operand2.result())
6798 def generate_evaluation_code(self, code):
6799 self.operand1.generate_evaluation_code(code)
6800 self.operand2.generate_evaluation_code(code)
6802 self.allocate_temp_result(code)
6803 self.generate_operation_code(code, self.result(),
6804 self.operand1, self.operator, self.operand2)
6806 self.cascade.generate_evaluation_code(code,
6807 self.result(), self.operand2)
6808 self.operand1.generate_disposal_code(code)
6809 self.operand1.free_temps(code)
6810 self.operand2.generate_disposal_code(code)
6811 self.operand2.free_temps(code)
6813 def generate_subexpr_disposal_code(self, code):
6814 # If this is called, it is a non-cascaded cmp,
6815 # so only need to dispose of the two main operands.
6816 self.operand1.generate_disposal_code(code)
6817 self.operand2.generate_disposal_code(code)
6819 def free_subexpr_temps(self, code):
6820 # If this is called, it is a non-cascaded cmp,
6821 # so only need to dispose of the two main operands.
6822 self.operand1.free_temps(code)
6823 self.operand2.free_temps(code)
6825 def annotate(self, code):
6826 self.operand1.annotate(code)
6827 self.operand2.annotate(code)
6829 self.cascade.annotate(code)
6832 class CascadedCmpNode(Node, CmpNode):
6833 # A CascadedCmpNode is not a complete expression node. It
6834 # hangs off the side of another comparison node, shares
6835 # its left operand with that node, and shares its result
6836 # with the PrimaryCmpNode at the head of the chain.
6840 # cascade CascadedCmpNode
6842 child_attrs = ['operand2', 'cascade']
6845 constant_result = constant_value_not_set # FIXME: where to calculate this?
6847 def infer_type(self, env):
6848 # TODO: Actually implement this (after merging with -unstable).
6849 return py_object_type
6851 def type_dependencies(self, env):
6854 def has_constant_result(self):
6855 return self.constant_result is not constant_value_not_set and \
6856 self.constant_result is not not_a_constant
6858 def analyse_types(self, env):
6859 self.operand2.analyse_types(env)
6861 self.cascade.analyse_types(env)
6863 def has_python_operands(self):
6864 return self.operand2.type.is_pyobject
6866 def coerce_operands_to_pyobjects(self, env):
6867 self.operand2 = self.operand2.coerce_to_pyobject(env)
6868 if self.operand2.type is dict_type and self.operator in ('in', 'not_in'):
6869 self.operand2 = self.operand2.as_none_safe_node("'NoneType' object is not iterable")
6871 self.cascade.coerce_operands_to_pyobjects(env)
6873 def coerce_cascaded_operands_to_temp(self, env):
6875 #self.operand2 = self.operand2.coerce_to_temp(env) #CTT
6876 self.operand2 = self.operand2.coerce_to_simple(env)
6877 self.cascade.coerce_cascaded_operands_to_temp(env)
6879 def generate_evaluation_code(self, code, result, operand1):
6880 if self.type.is_pyobject:
6881 code.putln("if (__Pyx_PyObject_IsTrue(%s)) {" % result)
6882 code.put_decref(result, self.type)
6884 code.putln("if (%s) {" % result)
6885 self.operand2.generate_evaluation_code(code)
6886 self.generate_operation_code(code, result,
6887 operand1, self.operator, self.operand2)
6889 self.cascade.generate_evaluation_code(
6890 code, result, self.operand2)
6891 # Cascaded cmp result is always temp
6892 self.operand2.generate_disposal_code(code)
6893 self.operand2.free_temps(code)
6896 def annotate(self, code):
6897 self.operand2.annotate(code)
6899 self.cascade.annotate(code)
6902 binop_node_classes = {
6903 "or": BoolBinopNode,
6904 "and": BoolBinopNode,
6919 def binop_node(pos, operator, operand1, operand2, inplace=False):
6920 # Construct binop node of appropriate class for
6922 return binop_node_classes[operator](pos,
6923 operator = operator,
6924 operand1 = operand1,
6925 operand2 = operand2,
6928 #-------------------------------------------------------------------
6932 # Coercion nodes are special in that they are created during
6933 # the analyse_types phase of parse tree processing.
6934 # Their __init__ methods consequently incorporate some aspects
6937 #-------------------------------------------------------------------
6939 class CoercionNode(ExprNode):
6940 # Abstract base class for coercion nodes.
6942 # arg ExprNode node being coerced
6945 constant_result = not_a_constant
6947 def __init__(self, arg):
6951 print("%s Coercing %s" % (self, self.arg))
6953 def calculate_constant_result(self):
6954 # constant folding can break type coercion, so this is disabled
6957 def annotate(self, code):
6958 self.arg.annotate(code)
6959 if self.arg.type != self.type:
6960 file, line, col = self.pos
6961 code.annotate((file, line, col-1), AnnotationItem(style='coerce', tag='coerce', text='[%s] to [%s]' % (self.arg.type, self.type)))
6964 class CastNode(CoercionNode):
6965 # Wrap a node in a C type cast.
6967 def __init__(self, arg, new_type):
6968 CoercionNode.__init__(self, arg)
6969 self.type = new_type
6971 def may_be_none(self):
6972 return self.arg.may_be_none()
6974 def calculate_result_code(self):
6975 return self.arg.result_as(self.type)
6977 def generate_result_code(self, code):
6978 self.arg.generate_result_code(code)
6981 class PyTypeTestNode(CoercionNode):
6982 # This node is used to check that a generic Python
6983 # object is an instance of a particular extension type.
6984 # This node borrows the result of its argument node.
6986 def __init__(self, arg, dst_type, env, notnone=False):
6987 # The arg is know to be a Python object, and
6988 # the dst_type is known to be an extension type.
6989 assert dst_type.is_extension_type or dst_type.is_builtin_type, "PyTypeTest on non extension type"
6990 CoercionNode.__init__(self, arg)
6991 self.type = dst_type
6992 self.result_ctype = arg.ctype()
6993 self.notnone = notnone
6995 nogil_check = Node.gil_error
6996 gil_message = "Python type test"
6998 def analyse_types(self, env):
7001 def may_be_none(self):
7004 return self.arg.may_be_none()
7006 def result_in_temp(self):
7007 return self.arg.result_in_temp()
7009 def is_ephemeral(self):
7010 return self.arg.is_ephemeral()
7012 def calculate_constant_result(self):
7016 def calculate_result_code(self):
7017 return self.arg.result()
7019 def generate_result_code(self, code):
7020 if self.type.typeobj_is_available():
7021 if not self.type.is_builtin_type:
7022 code.globalstate.use_utility_code(type_test_utility_code)
7025 self.type.type_test_code(self.arg.py_result(), self.notnone),
7026 code.error_goto(self.pos)))
7028 error(self.pos, "Cannot test type of extern C class "
7029 "without type object name specification")
7031 def generate_post_assignment_code(self, code):
7032 self.arg.generate_post_assignment_code(code)
7034 def free_temps(self, code):
7035 self.arg.free_temps(code)
7038 class NoneCheckNode(CoercionNode):
7039 # This node is used to check that a Python object is not None and
7040 # raises an appropriate exception (as specified by the creating
7043 def __init__(self, arg, exception_type_cname, exception_message):
7044 CoercionNode.__init__(self, arg)
7045 self.type = arg.type
7046 self.result_ctype = arg.ctype()
7047 self.exception_type_cname = exception_type_cname
7048 self.exception_message = exception_message
7050 def analyse_types(self, env):
7053 def may_be_none(self):
7056 def result_in_temp(self):
7057 return self.arg.result_in_temp()
7059 def calculate_result_code(self):
7060 return self.arg.result()
7062 def generate_result_code(self, code):
7064 "if (unlikely(%s == Py_None)) {" % self.arg.result())
7065 code.putln('PyErr_SetString(%s, "%s"); %s ' % (
7066 self.exception_type_cname,
7067 StringEncoding.escape_byte_string(
7068 self.exception_message.encode('UTF-8')),
7069 code.error_goto(self.pos)))
7072 def generate_post_assignment_code(self, code):
7073 self.arg.generate_post_assignment_code(code)
7075 def free_temps(self, code):
7076 self.arg.free_temps(code)
7079 class CoerceToPyTypeNode(CoercionNode):
7080 # This node is used to convert a C data type
7081 # to a Python object.
7083 type = py_object_type
7086 def __init__(self, arg, env, type=py_object_type):
7087 CoercionNode.__init__(self, arg)
7088 if not arg.type.create_to_py_utility_code(env):
7090 "Cannot convert '%s' to Python object" % arg.type)
7091 if type is py_object_type:
7092 # be specific about some known types
7093 if arg.type.is_string:
7094 self.type = bytes_type
7095 elif arg.type is PyrexTypes.c_py_unicode_type:
7096 self.type = unicode_type
7097 elif arg.type.is_complex:
7098 self.type = Builtin.complex_type
7100 # FIXME: check that the target type and the resulting type are compatible
7103 gil_message = "Converting to Python object"
7105 def may_be_none(self):
7106 # FIXME: is this always safe?
7109 def coerce_to_boolean(self, env):
7110 arg_type = self.arg.type
7111 if (arg_type == PyrexTypes.c_bint_type or
7112 (arg_type.is_pyobject and arg_type.name == 'bool')):
7113 return self.arg.coerce_to_temp(env)
7115 return CoerceToBooleanNode(self, env)
7117 def coerce_to_integer(self, env):
7118 # If not already some C integer type, coerce to longint.
7119 if self.arg.type.is_int:
7122 return self.arg.coerce_to(PyrexTypes.c_long_type, env)
7124 def analyse_types(self, env):
7125 # The arg is always already analysed
7128 def generate_result_code(self, code):
7129 function = self.arg.type.to_py_function
7130 code.putln('%s = %s(%s); %s' % (
7134 code.error_goto_if_null(self.result(), self.pos)))
7135 code.put_gotref(self.py_result())
7138 class CoerceIntToBytesNode(CoerceToPyTypeNode):
7139 # This node is used to convert a C int type to a Python bytes
7144 def __init__(self, arg, env):
7145 arg = arg.coerce_to_simple(env)
7146 CoercionNode.__init__(self, arg)
7147 self.type = Builtin.bytes_type
7149 def generate_result_code(self, code):
7151 arg_result = arg.result()
7152 if arg.type not in (PyrexTypes.c_char_type,
7153 PyrexTypes.c_uchar_type,
7154 PyrexTypes.c_schar_type):
7156 code.putln("if ((%s < 0) || (%s > 255)) {" % (
7157 arg_result, arg_result))
7159 code.putln("if (%s > 255) {" % arg_result)
7160 code.putln('PyErr_Format(PyExc_OverflowError, '
7161 '"value too large to pack into a byte"); %s' % (
7162 code.error_goto(self.pos)))
7165 if arg.type is not PyrexTypes.c_char_type:
7166 temp = code.funcstate.allocate_temp(PyrexTypes.c_char_type, manage_ref=False)
7167 code.putln("%s = (char)%s;" % (temp, arg_result))
7169 code.putln('%s = PyBytes_FromStringAndSize(&%s, 1); %s' % (
7172 code.error_goto_if_null(self.result(), self.pos)))
7173 if temp is not None:
7174 code.funcstate.release_temp(temp)
7175 code.put_gotref(self.py_result())
7178 class CoerceFromPyTypeNode(CoercionNode):
7179 # This node is used to convert a Python object
7182 def __init__(self, result_type, arg, env):
7183 CoercionNode.__init__(self, arg)
7184 self.type = result_type
7186 if not result_type.create_from_py_utility_code(env):
7188 "Cannot convert Python object to '%s'" % result_type)
7189 if self.type.is_string and self.arg.is_ephemeral():
7191 "Obtaining char * from temporary Python value")
7193 def analyse_types(self, env):
7194 # The arg is always already analysed
7197 def generate_result_code(self, code):
7198 function = self.type.from_py_function
7199 operand = self.arg.py_result()
7200 rhs = "%s(%s)" % (function, operand)
7201 if self.type.is_enum:
7202 rhs = typecast(self.type, c_long_type, rhs)
7203 code.putln('%s = %s; %s' % (
7206 code.error_goto_if(self.type.error_condition(self.result()), self.pos)))
7207 if self.type.is_pyobject:
7208 code.put_gotref(self.py_result())
7211 class CoerceToBooleanNode(CoercionNode):
7212 # This node is used when a result needs to be used
7213 # in a boolean context.
7215 type = PyrexTypes.c_bint_type
7217 _special_builtins = {
7218 Builtin.list_type : 'PyList_GET_SIZE',
7219 Builtin.tuple_type : 'PyTuple_GET_SIZE',
7220 Builtin.bytes_type : 'PyBytes_GET_SIZE',
7221 Builtin.unicode_type : 'PyUnicode_GET_SIZE',
7224 def __init__(self, arg, env):
7225 CoercionNode.__init__(self, arg)
7226 if arg.type.is_pyobject:
7229 def nogil_check(self, env):
7230 if self.arg.type.is_pyobject and self._special_builtins.get(self.arg.type) is None:
7233 gil_message = "Truth-testing Python object"
7235 def check_const(self):
7239 return self.arg.check_const()
7241 def calculate_result_code(self):
7242 return "(%s != 0)" % self.arg.result()
7244 def generate_result_code(self, code):
7245 if not self.is_temp:
7247 test_func = self._special_builtins.get(self.arg.type)
7248 if test_func is not None:
7249 code.putln("%s = (%s != Py_None) && (%s(%s) != 0);" % (
7251 self.arg.py_result(),
7253 self.arg.py_result()))
7256 "%s = __Pyx_PyObject_IsTrue(%s); %s" % (
7258 self.arg.py_result(),
7259 code.error_goto_if_neg(self.result(), self.pos)))
7261 class CoerceToComplexNode(CoercionNode):
7263 def __init__(self, arg, dst_type, env):
7264 if arg.type.is_complex:
7265 arg = arg.coerce_to_simple(env)
7266 self.type = dst_type
7267 CoercionNode.__init__(self, arg)
7268 dst_type.create_declaration_utility_code(env)
7270 def calculate_result_code(self):
7271 if self.arg.type.is_complex:
7272 real_part = "__Pyx_CREAL(%s)" % self.arg.result()
7273 imag_part = "__Pyx_CIMAG(%s)" % self.arg.result()
7275 real_part = self.arg.result()
7277 return "%s(%s, %s)" % (
7278 self.type.from_parts,
7282 def generate_result_code(self, code):
7285 class CoerceToTempNode(CoercionNode):
7286 # This node is used to force the result of another node
7287 # to be stored in a temporary. It is only used if the
7288 # argument node's result is not already in a temporary.
7290 def __init__(self, arg, env):
7291 CoercionNode.__init__(self, arg)
7292 self.type = self.arg.type
7293 self.constant_result = self.arg.constant_result
7295 if self.type.is_pyobject:
7296 self.result_ctype = py_object_type
7298 gil_message = "Creating temporary Python reference"
7300 def analyse_types(self, env):
7301 # The arg is always already analysed
7304 def coerce_to_boolean(self, env):
7305 self.arg = self.arg.coerce_to_boolean(env)
7306 if self.arg.is_simple():
7308 self.type = self.arg.type
7309 self.result_ctype = self.type
7312 def generate_result_code(self, code):
7313 #self.arg.generate_evaluation_code(code) # Already done
7314 # by generic generate_subexpr_evaluation_code!
7315 code.putln("%s = %s;" % (
7316 self.result(), self.arg.result_as(self.ctype())))
7317 if self.type.is_pyobject and self.use_managed_ref:
7318 code.put_incref(self.result(), self.ctype())
7321 class CloneNode(CoercionNode):
7322 # This node is employed when the result of another node needs
7323 # to be used multiple times. The argument node's result must
7324 # be in a temporary. This node "borrows" the result from the
7325 # argument node, and does not generate any evaluation or
7326 # disposal code for it. The original owner of the argument
7327 # node is responsible for doing those things.
7329 subexprs = [] # Arg is not considered a subexpr
7332 def __init__(self, arg):
7333 CoercionNode.__init__(self, arg)
7334 if hasattr(arg, 'type'):
7335 self.type = arg.type
7336 self.result_ctype = arg.result_ctype
7337 if hasattr(arg, 'entry'):
7338 self.entry = arg.entry
7341 return self.arg.result()
7343 def type_dependencies(self, env):
7344 return self.arg.type_dependencies(env)
7346 def infer_type(self, env):
7347 return self.arg.infer_type(env)
7349 def analyse_types(self, env):
7350 self.type = self.arg.type
7351 self.result_ctype = self.arg.result_ctype
7353 if hasattr(self.arg, 'entry'):
7354 self.entry = self.arg.entry
7356 def generate_evaluation_code(self, code):
7359 def generate_result_code(self, code):
7362 def generate_disposal_code(self, code):
7365 def free_temps(self, code):
7369 class ModuleRefNode(ExprNode):
7370 # Simple returns the module object
7372 type = py_object_type
7376 def analyse_types(self, env):
7379 def may_be_none(self):
7382 def calculate_result_code(self):
7383 return Naming.module_cname
7385 def generate_result_code(self, code):
7388 class DocstringRefNode(ExprNode):
7389 # Extracts the docstring of the body element
7392 type = py_object_type
7395 def __init__(self, pos, body):
7396 ExprNode.__init__(self, pos)
7397 assert body.type.is_pyobject
7400 def analyse_types(self, env):
7403 def generate_result_code(self, code):
7404 code.putln('%s = __Pyx_GetAttrString(%s, "__doc__"); %s' % (
7405 self.result(), self.body.result(),
7406 code.error_goto_if_null(self.result(), self.pos)))
7407 code.put_gotref(self.result())
7411 #------------------------------------------------------------------------------------
7413 # Runtime support code
7415 #------------------------------------------------------------------------------------
7417 get_name_interned_utility_code = UtilityCode(
7419 static PyObject *__Pyx_GetName(PyObject *dict, PyObject *name); /*proto*/
7422 static PyObject *__Pyx_GetName(PyObject *dict, PyObject *name) {
7424 result = PyObject_GetAttr(dict, name);
7426 PyErr_SetObject(PyExc_NameError, name);
7431 #------------------------------------------------------------------------------------
7433 import_utility_code = UtilityCode(
7435 static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list); /*proto*/
7438 static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list) {
7439 PyObject *py_import = 0;
7440 PyObject *empty_list = 0;
7441 PyObject *module = 0;
7442 PyObject *global_dict = 0;
7443 PyObject *empty_dict = 0;
7445 py_import = __Pyx_GetAttrString(%(BUILTINS)s, "__import__");
7451 empty_list = PyList_New(0);
7456 global_dict = PyModule_GetDict(%(GLOBALS)s);
7459 empty_dict = PyDict_New();
7462 module = PyObject_CallFunctionObjArgs(py_import,
7463 name, global_dict, empty_dict, list, NULL);
7465 Py_XDECREF(empty_list);
7466 Py_XDECREF(py_import);
7467 Py_XDECREF(empty_dict);
7471 "BUILTINS": Naming.builtins_cname,
7472 "GLOBALS": Naming.module_cname,
7475 #------------------------------------------------------------------------------------
7477 get_exception_utility_code = UtilityCode(
7479 static PyObject *__Pyx_GetExcValue(void); /*proto*/
7482 static PyObject *__Pyx_GetExcValue(void) {
7483 PyObject *type = 0, *value = 0, *tb = 0;
7484 PyObject *tmp_type, *tmp_value, *tmp_tb;
7485 PyObject *result = 0;
7486 PyThreadState *tstate = PyThreadState_Get();
7487 PyErr_Fetch(&type, &value, &tb);
7488 PyErr_NormalizeException(&type, &value, &tb);
7489 if (PyErr_Occurred())
7495 tmp_type = tstate->exc_type;
7496 tmp_value = tstate->exc_value;
7497 tmp_tb = tstate->exc_traceback;
7498 tstate->exc_type = type;
7499 tstate->exc_value = value;
7500 tstate->exc_traceback = tb;
7501 /* Make sure tstate is in a consistent state when we XDECREF
7502 these objects (XDECREF may run arbitrary code). */
7503 Py_XDECREF(tmp_type);
7504 Py_XDECREF(tmp_value);
7519 #------------------------------------------------------------------------------------
7521 type_test_utility_code = UtilityCode(
7523 static CYTHON_INLINE int __Pyx_TypeTest(PyObject *obj, PyTypeObject *type); /*proto*/
7526 static CYTHON_INLINE int __Pyx_TypeTest(PyObject *obj, PyTypeObject *type) {
7527 if (unlikely(!type)) {
7528 PyErr_Format(PyExc_SystemError, "Missing type object");
7531 if (likely(PyObject_TypeCheck(obj, type)))
7533 PyErr_Format(PyExc_TypeError, "Cannot convert %.200s to %.200s",
7534 Py_TYPE(obj)->tp_name, type->tp_name);
7539 #------------------------------------------------------------------------------------
7541 find_py2_metaclass_utility_code = UtilityCode(
7543 static PyObject *__Pyx_FindPy2Metaclass(PyObject *bases); /*proto*/
7546 static PyObject *__Pyx_FindPy2Metaclass(PyObject *bases) {
7547 PyObject *metaclass;
7548 /* Default metaclass */
7549 #if PY_MAJOR_VERSION < 3
7550 if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
7551 PyObject *base = PyTuple_GET_ITEM(bases, 0);
7552 metaclass = PyObject_GetAttrString(base, "__class__");
7555 metaclass = (PyObject*) Py_TYPE(base);
7558 metaclass = (PyObject *) &PyClass_Type;
7561 if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
7562 PyObject *base = PyTuple_GET_ITEM(bases, 0);
7563 metaclass = (PyObject*) Py_TYPE(base);
7565 metaclass = (PyObject *) &PyType_Type;
7568 Py_INCREF(metaclass);
7573 create_class_utility_code = UtilityCode(
7575 static PyObject *__Pyx_CreateClass(PyObject *bases, PyObject *dict, PyObject *name,
7576 PyObject *modname); /*proto*/
7579 static PyObject *__Pyx_CreateClass(PyObject *bases, PyObject *dict, PyObject *name,
7580 PyObject *modname) {
7582 PyObject *metaclass;
7584 if (PyDict_SetItemString(dict, "__module__", modname) < 0)
7587 /* Python2 __metaclass__ */
7588 metaclass = PyDict_GetItemString(dict, "__metaclass__");
7590 Py_INCREF(metaclass);
7592 metaclass = __Pyx_FindPy2Metaclass(bases);
7594 result = PyObject_CallFunctionObjArgs(metaclass, name, bases, dict, NULL);
7595 Py_DECREF(metaclass);
7599 requires = [find_py2_metaclass_utility_code])
7601 #------------------------------------------------------------------------------------
7603 create_py3class_utility_code = UtilityCode(
7605 static PyObject *__Pyx_Py3MetaclassGet(PyObject *bases, PyObject *mkw); /*proto*/
7606 static PyObject *__Pyx_Py3MetaclassPrepare(PyObject *metaclass, PyObject *bases, PyObject *name, PyObject *mkw, PyObject *modname, PyObject *doc); /*proto*/
7607 static PyObject *__Pyx_Py3ClassCreate(PyObject *metaclass, PyObject *name, PyObject *bases, PyObject *dict, PyObject *mkw); /*proto*/
7610 PyObject *__Pyx_Py3MetaclassGet(PyObject *bases, PyObject *mkw) {
7611 PyObject *metaclass = PyDict_GetItemString(mkw, "metaclass");
7613 Py_INCREF(metaclass);
7614 if (PyDict_DelItemString(mkw, "metaclass") < 0) {
7615 Py_DECREF(metaclass);
7620 return __Pyx_FindPy2Metaclass(bases);
7623 PyObject *__Pyx_Py3MetaclassPrepare(PyObject *metaclass, PyObject *bases, PyObject *name, PyObject *mkw,
7624 PyObject *modname, PyObject *doc) {
7630 prep = PyObject_GetAttrString(metaclass, "__prepare__");
7632 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
7635 return PyDict_New();
7637 pargs = PyTuple_New(2);
7645 PyTuple_SET_ITEM(pargs, 0, name);
7646 PyTuple_SET_ITEM(pargs, 1, bases);
7648 ns = PyObject_Call(prep, pargs, mkw);
7656 /* Required here to emulate assignment order */
7657 /* XXX: use consts here */
7658 #if PY_MAJOR_VERSION >= 3
7659 str = PyUnicode_FromString("__module__");
7661 str = PyString_FromString("__module__");
7668 if (PyObject_SetItem(ns, str, modname) < 0) {
7675 #if PY_MAJOR_VERSION >= 3
7676 str = PyUnicode_FromString("__doc__");
7678 str = PyString_FromString("__doc__");
7684 if (PyObject_SetItem(ns, str, doc) < 0) {
7694 PyObject *__Pyx_Py3ClassCreate(PyObject *metaclass, PyObject *name, PyObject *bases, PyObject *dict, PyObject *mkw) {
7696 PyObject *margs = PyTuple_New(3);
7702 PyTuple_SET_ITEM(margs, 0, name);
7703 PyTuple_SET_ITEM(margs, 1, bases);
7704 PyTuple_SET_ITEM(margs, 2, dict);
7705 result = PyObject_Call(metaclass, margs, mkw);
7710 requires = [find_py2_metaclass_utility_code])
7712 #------------------------------------------------------------------------------------
7714 cpp_exception_utility_code = UtilityCode(
7716 #ifndef __Pyx_CppExn2PyErr
7717 static void __Pyx_CppExn2PyErr() {
7719 if (PyErr_Occurred())
7720 ; // let the latest Python exn pass through and ignore the current one
7723 } catch (const std::invalid_argument& exn) {
7724 // Catch a handful of different errors here and turn them into the
7725 // equivalent Python errors.
7726 // Change invalid_argument to ValueError
7727 PyErr_SetString(PyExc_ValueError, exn.what());
7728 } catch (const std::out_of_range& exn) {
7729 // Change out_of_range to IndexError
7730 PyErr_SetString(PyExc_IndexError, exn.what());
7731 } catch (const std::exception& exn) {
7732 PyErr_SetString(PyExc_RuntimeError, exn.what());
7736 PyErr_SetString(PyExc_RuntimeError, "Unknown exception");
7744 pyerr_occurred_withgil_utility_code= UtilityCode(
7746 static CYTHON_INLINE int __Pyx_ErrOccurredWithGIL(void); /* proto */
7749 static CYTHON_INLINE int __Pyx_ErrOccurredWithGIL(void) {
7752 PyGILState_STATE _save = PyGILState_Ensure();
7754 err = !!PyErr_Occurred();
7756 PyGILState_Release(_save);
7763 #------------------------------------------------------------------------------------
7765 raise_noneattr_error_utility_code = UtilityCode(
7767 static CYTHON_INLINE void __Pyx_RaiseNoneAttributeError(const char* attrname);
7770 static CYTHON_INLINE void __Pyx_RaiseNoneAttributeError(const char* attrname) {
7771 PyErr_Format(PyExc_AttributeError, "'NoneType' object has no attribute '%s'", attrname);
7775 raise_noneindex_error_utility_code = UtilityCode(
7777 static CYTHON_INLINE void __Pyx_RaiseNoneIndexingError(void);
7780 static CYTHON_INLINE void __Pyx_RaiseNoneIndexingError(void) {
7781 PyErr_SetString(PyExc_TypeError, "'NoneType' object is unsubscriptable");
7785 raise_none_iter_error_utility_code = UtilityCode(
7787 static CYTHON_INLINE void __Pyx_RaiseNoneNotIterableError(void);
7790 static CYTHON_INLINE void __Pyx_RaiseNoneNotIterableError(void) {
7791 PyErr_SetString(PyExc_TypeError, "'NoneType' object is not iterable");
7795 #------------------------------------------------------------------------------------
7797 getitem_dict_utility_code = UtilityCode(
7799 #if PY_MAJOR_VERSION >= 3
7800 static PyObject *__Pyx_PyDict_GetItem(PyObject *d, PyObject* key) {
7802 if (unlikely(d == Py_None)) {
7803 __Pyx_RaiseNoneIndexingError();
7806 value = PyDict_GetItemWithError(d, key);
7807 if (unlikely(!value)) {
7808 if (!PyErr_Occurred())
7809 PyErr_SetObject(PyExc_KeyError, key);
7816 #define __Pyx_PyDict_GetItem(d, key) PyObject_GetItem(d, key)
7819 requires = [raise_noneindex_error_utility_code])
7821 #------------------------------------------------------------------------------------
7823 getitem_int_pyunicode_utility_code = UtilityCode(
7825 #define __Pyx_GetItemInt_Unicode(o, i, size, to_py_func) (((size) <= sizeof(Py_ssize_t)) ? \\
7826 __Pyx_GetItemInt_Unicode_Fast(o, i) : \\
7827 __Pyx_GetItemInt_Unicode_Generic(o, to_py_func(i)))
7829 static CYTHON_INLINE Py_UNICODE __Pyx_GetItemInt_Unicode_Fast(PyObject* ustring, Py_ssize_t i) {
7830 if (likely((0 <= i) & (i < PyUnicode_GET_SIZE(ustring)))) {
7831 return PyUnicode_AS_UNICODE(ustring)[i];
7832 } else if ((-PyUnicode_GET_SIZE(ustring) <= i) & (i < 0)) {
7833 i += PyUnicode_GET_SIZE(ustring);
7834 return PyUnicode_AS_UNICODE(ustring)[i];
7836 PyErr_SetString(PyExc_IndexError, "string index out of range");
7837 return (Py_UNICODE)-1;
7841 static CYTHON_INLINE Py_UNICODE __Pyx_GetItemInt_Unicode_Generic(PyObject* ustring, PyObject* j) {
7843 PyObject *uchar_string;
7844 if (!j) return (Py_UNICODE)-1;
7845 uchar_string = PyObject_GetItem(ustring, j);
7847 if (!uchar_string) return (Py_UNICODE)-1;
7848 uchar = PyUnicode_AS_UNICODE(uchar_string)[0];
7849 Py_DECREF(uchar_string);
7854 getitem_int_utility_code = UtilityCode(
7857 static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Generic(PyObject *o, PyObject* j) {
7859 if (!j) return NULL;
7860 r = PyObject_GetItem(o, j);
7867 #define __Pyx_GetItemInt_%(type)s(o, i, size, to_py_func) (((size) <= sizeof(Py_ssize_t)) ? \\
7868 __Pyx_GetItemInt_%(type)s_Fast(o, i) : \\
7869 __Pyx_GetItemInt_Generic(o, to_py_func(i)))
7871 static CYTHON_INLINE PyObject *__Pyx_GetItemInt_%(type)s_Fast(PyObject *o, Py_ssize_t i) {
7872 if (likely(o != Py_None)) {
7873 if (likely((0 <= i) & (i < Py%(type)s_GET_SIZE(o)))) {
7874 PyObject *r = Py%(type)s_GET_ITEM(o, i);
7878 else if ((-Py%(type)s_GET_SIZE(o) <= i) & (i < 0)) {
7879 PyObject *r = Py%(type)s_GET_ITEM(o, Py%(type)s_GET_SIZE(o) + i);
7884 return __Pyx_GetItemInt_Generic(o, PyInt_FromSsize_t(i));
7886 """ % {'type' : type_name} for type_name in ('List', 'Tuple')
7889 #define __Pyx_GetItemInt(o, i, size, to_py_func) (((size) <= sizeof(Py_ssize_t)) ? \\
7890 __Pyx_GetItemInt_Fast(o, i) : \\
7891 __Pyx_GetItemInt_Generic(o, to_py_func(i)))
7893 static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Fast(PyObject *o, Py_ssize_t i) {
7895 if (PyList_CheckExact(o) && ((0 <= i) & (i < PyList_GET_SIZE(o)))) {
7896 r = PyList_GET_ITEM(o, i);
7899 else if (PyTuple_CheckExact(o) && ((0 <= i) & (i < PyTuple_GET_SIZE(o)))) {
7900 r = PyTuple_GET_ITEM(o, i);
7903 else if (Py_TYPE(o)->tp_as_sequence && Py_TYPE(o)->tp_as_sequence->sq_item && (likely(i >= 0))) {
7904 r = PySequence_GetItem(o, i);
7907 r = __Pyx_GetItemInt_Generic(o, PyInt_FromSsize_t(i));
7917 #------------------------------------------------------------------------------------
7919 setitem_int_utility_code = UtilityCode(
7921 #define __Pyx_SetItemInt(o, i, v, size, to_py_func) (((size) <= sizeof(Py_ssize_t)) ? \\
7922 __Pyx_SetItemInt_Fast(o, i, v) : \\
7923 __Pyx_SetItemInt_Generic(o, to_py_func(i), v))
7925 static CYTHON_INLINE int __Pyx_SetItemInt_Generic(PyObject *o, PyObject *j, PyObject *v) {
7928 r = PyObject_SetItem(o, j, v);
7933 static CYTHON_INLINE int __Pyx_SetItemInt_Fast(PyObject *o, Py_ssize_t i, PyObject *v) {
7934 if (PyList_CheckExact(o) && ((0 <= i) & (i < PyList_GET_SIZE(o)))) {
7936 Py_DECREF(PyList_GET_ITEM(o, i));
7937 PyList_SET_ITEM(o, i, v);
7940 else if (Py_TYPE(o)->tp_as_sequence && Py_TYPE(o)->tp_as_sequence->sq_ass_item && (likely(i >= 0)))
7941 return PySequence_SetItem(o, i, v);
7943 PyObject *j = PyInt_FromSsize_t(i);
7944 return __Pyx_SetItemInt_Generic(o, j, v);
7951 #------------------------------------------------------------------------------------
7953 delitem_int_utility_code = UtilityCode(
7955 #define __Pyx_DelItemInt(o, i, size, to_py_func) (((size) <= sizeof(Py_ssize_t)) ? \\
7956 __Pyx_DelItemInt_Fast(o, i) : \\
7957 __Pyx_DelItem_Generic(o, to_py_func(i)))
7959 static CYTHON_INLINE int __Pyx_DelItem_Generic(PyObject *o, PyObject *j) {
7962 r = PyObject_DelItem(o, j);
7967 static CYTHON_INLINE int __Pyx_DelItemInt_Fast(PyObject *o, Py_ssize_t i) {
7968 if (Py_TYPE(o)->tp_as_sequence && Py_TYPE(o)->tp_as_sequence->sq_ass_item && likely(i >= 0))
7969 return PySequence_DelItem(o, i);
7971 PyObject *j = PyInt_FromSsize_t(i);
7972 return __Pyx_DelItem_Generic(o, j);
7979 #------------------------------------------------------------------------------------
7981 raise_too_many_values_to_unpack = UtilityCode(
7983 static CYTHON_INLINE void __Pyx_RaiseTooManyValuesError(Py_ssize_t expected);
7986 static CYTHON_INLINE void __Pyx_RaiseTooManyValuesError(Py_ssize_t expected) {
7987 PyErr_Format(PyExc_ValueError,
7988 #if PY_VERSION_HEX < 0x02050000
7989 "too many values to unpack (expected %d)", (int)expected);
7991 "too many values to unpack (expected %zd)", expected);
7996 raise_need_more_values_to_unpack = UtilityCode(
7998 static CYTHON_INLINE void __Pyx_RaiseNeedMoreValuesError(Py_ssize_t index);
8001 static CYTHON_INLINE void __Pyx_RaiseNeedMoreValuesError(Py_ssize_t index) {
8002 PyErr_Format(PyExc_ValueError,
8003 #if PY_VERSION_HEX < 0x02050000
8004 "need more than %d value%s to unpack", (int)index,
8006 "need more than %zd value%s to unpack", index,
8008 (index == 1) ? "" : "s");
8012 #------------------------------------------------------------------------------------
8014 tuple_unpacking_error_code = UtilityCode(
8016 static void __Pyx_UnpackTupleError(PyObject *, Py_ssize_t index); /*proto*/
8019 static void __Pyx_UnpackTupleError(PyObject *t, Py_ssize_t index) {
8021 __Pyx_RaiseNoneNotIterableError();
8022 } else if (PyTuple_GET_SIZE(t) < index) {
8023 __Pyx_RaiseNeedMoreValuesError(PyTuple_GET_SIZE(t));
8025 __Pyx_RaiseTooManyValuesError(index);
8029 requires = [raise_none_iter_error_utility_code,
8030 raise_need_more_values_to_unpack,
8031 raise_too_many_values_to_unpack]
8034 unpacking_utility_code = UtilityCode(
8036 static PyObject *__Pyx_UnpackItem(PyObject *, Py_ssize_t index); /*proto*/
8037 static int __Pyx_EndUnpack(PyObject *, Py_ssize_t expected); /*proto*/
8040 static PyObject *__Pyx_UnpackItem(PyObject *iter, Py_ssize_t index) {
8042 if (!(item = PyIter_Next(iter))) {
8043 if (!PyErr_Occurred()) {
8044 __Pyx_RaiseNeedMoreValuesError(index);
8050 static int __Pyx_EndUnpack(PyObject *iter, Py_ssize_t expected) {
8052 if ((item = PyIter_Next(iter))) {
8054 __Pyx_RaiseTooManyValuesError(expected);
8057 else if (!PyErr_Occurred())
8063 requires = [raise_need_more_values_to_unpack,
8064 raise_too_many_values_to_unpack]
8067 #------------------------------------------------------------------------------------
8069 # CPython supports calling functions with non-dict kwargs by
8070 # converting them to a dict first
8072 kwargs_call_utility_code = UtilityCode(
8074 static PyObject* __Pyx_PyEval_CallObjectWithKeywords(PyObject*, PyObject*, PyObject*); /*proto*/
8077 static PyObject* __Pyx_PyEval_CallObjectWithKeywords(PyObject *callable, PyObject *args, PyObject *kwargs) {
8079 if (likely(PyDict_Check(kwargs))) {
8080 return PyEval_CallObjectWithKeywords(callable, args, kwargs);
8082 PyObject* real_dict;
8083 real_dict = PyObject_CallFunctionObjArgs((PyObject*)&PyDict_Type, kwargs, NULL);
8084 if (unlikely(!real_dict))
8086 result = PyEval_CallObjectWithKeywords(callable, args, real_dict);
8087 Py_DECREF(real_dict);
8088 return result; /* may be NULL */
8095 #------------------------------------------------------------------------------------
8097 int_pow_utility_code = UtilityCode(
8099 static CYTHON_INLINE %(type)s %(func_name)s(%(type)s, %(type)s); /* proto */
8102 static CYTHON_INLINE %(type)s %(func_name)s(%(type)s b, %(type)s e) {
8114 if (unlikely(e<0)) return 0;
8117 t *= (b * (e&1)) | ((~e)&1); /* 1 or b */
8125 # ------------------------------ Division ------------------------------------
8127 div_int_utility_code = UtilityCode(
8129 static CYTHON_INLINE %(type)s __Pyx_div_%(type_name)s(%(type)s, %(type)s); /* proto */
8132 static CYTHON_INLINE %(type)s __Pyx_div_%(type_name)s(%(type)s a, %(type)s b) {
8134 %(type)s r = a - q*b;
8135 q -= ((r != 0) & ((r ^ b) < 0));
8140 mod_int_utility_code = UtilityCode(
8142 static CYTHON_INLINE %(type)s __Pyx_mod_%(type_name)s(%(type)s, %(type)s); /* proto */
8145 static CYTHON_INLINE %(type)s __Pyx_mod_%(type_name)s(%(type)s a, %(type)s b) {
8146 %(type)s r = a %% b;
8147 r += ((r != 0) & ((r ^ b) < 0)) * b;
8152 mod_float_utility_code = UtilityCode(
8154 static CYTHON_INLINE %(type)s __Pyx_mod_%(type_name)s(%(type)s, %(type)s); /* proto */
8157 static CYTHON_INLINE %(type)s __Pyx_mod_%(type_name)s(%(type)s a, %(type)s b) {
8158 %(type)s r = fmod%(math_h_modifier)s(a, b);
8159 r += ((r != 0) & ((r < 0) ^ (b < 0))) * b;
8164 cdivision_warning_utility_code = UtilityCode(
8166 static int __Pyx_cdivision_warning(void); /* proto */
8169 static int __Pyx_cdivision_warning(void) {
8170 return PyErr_WarnExplicit(PyExc_RuntimeWarning,
8171 "division with oppositely signed operands, C and Python semantics differ",
8178 'FILENAME': Naming.filename_cname,
8179 'LINENO': Naming.lineno_cname,
8183 division_overflow_test_code = UtilityCode(
8185 #define UNARY_NEG_WOULD_OVERFLOW(x) \
8186 (((x) < 0) & ((unsigned long)(x) == 0-(unsigned long)(x)))
8190 binding_cfunc_utility_code = UtilityCode(
8192 #define %(binding_cfunc)s_USED 1
8195 PyCFunctionObject func;
8196 } %(binding_cfunc)s_object;
8198 static PyTypeObject %(binding_cfunc)s_type;
8199 static PyTypeObject *%(binding_cfunc)s = NULL;
8201 static PyObject *%(binding_cfunc)s_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module); /* proto */
8202 #define %(binding_cfunc)s_New(ml, self) %(binding_cfunc)s_NewEx(ml, self, NULL)
8204 static int %(binding_cfunc)s_init(void); /* proto */
8205 """ % Naming.__dict__,
8208 static PyObject *%(binding_cfunc)s_NewEx(PyMethodDef *ml, PyObject *self, PyObject *module) {
8209 %(binding_cfunc)s_object *op = PyObject_GC_New(%(binding_cfunc)s_object, %(binding_cfunc)s);
8214 op->func.m_self = self;
8216 op->func.m_module = module;
8217 PyObject_GC_Track(op);
8218 return (PyObject *)op;
8221 static void %(binding_cfunc)s_dealloc(%(binding_cfunc)s_object *m) {
8222 PyObject_GC_UnTrack(m);
8223 Py_XDECREF(m->func.m_self);
8224 Py_XDECREF(m->func.m_module);
8228 static PyObject *%(binding_cfunc)s_descr_get(PyObject *func, PyObject *obj, PyObject *type) {
8231 return PyMethod_New(func, obj, type);
8234 static int %(binding_cfunc)s_init(void) {
8235 %(binding_cfunc)s_type = PyCFunction_Type;
8236 %(binding_cfunc)s_type.tp_name = __Pyx_NAMESTR("cython_binding_builtin_function_or_method");
8237 %(binding_cfunc)s_type.tp_dealloc = (destructor)%(binding_cfunc)s_dealloc;
8238 %(binding_cfunc)s_type.tp_descr_get = %(binding_cfunc)s_descr_get;
8239 if (PyType_Ready(&%(binding_cfunc)s_type) < 0) {
8242 %(binding_cfunc)s = &%(binding_cfunc)s_type;
8246 """ % Naming.__dict__)