Cython has moved to github.

cython-devel

view Cython/Compiler/ExprNodes.py @ 906:def99eb4f83c

Fix ticket #36, casting non-simple expression.
author Robert Bradshaw <robertwb@math.washington.edu>
date Sat Aug 02 22:37:19 2008 -0700 (3 years ago)
parents 7d24ad4f8a9d
children fe3d5f426ff2
line source
1 #
2 # Pyrex - Parse tree nodes for expressions
3 #
5 import operator
6 from string import join
8 from Errors import error, warning, InternalError
9 import Naming
10 from Nodes import Node
11 import PyrexTypes
12 from PyrexTypes import py_object_type, c_long_type, typecast, error_type
13 from Builtin import list_type, tuple_type, dict_type
14 import Symtab
15 import Options
16 from Annotate import AnnotationItem
18 from Cython.Debugging import print_call_chain
19 from DebugFlags import debug_disposal_code, debug_temp_alloc, \
20 debug_coercion
23 class ExprNode(Node):
24 # subexprs [string] Class var holding names of subexpr node attrs
25 # type PyrexType Type of the result
26 # result_code string Code fragment
27 # result_ctype string C type of result_code if different from type
28 # is_temp boolean Result is in a temporary variable
29 # is_sequence_constructor
30 # boolean Is a list or tuple constructor expression
31 # saved_subexpr_nodes
32 # [ExprNode or [ExprNode or None] or None]
33 # Cached result of subexpr_nodes()
35 result_ctype = None
36 type = None
38 # The Analyse Expressions phase for expressions is split
39 # into two sub-phases:
40 #
41 # Analyse Types
42 # Determines the result type of the expression based
43 # on the types of its sub-expressions, and inserts
44 # coercion nodes into the expression tree where needed.
45 # Marks nodes which will need to have temporary variables
46 # allocated.
47 #
48 # Allocate Temps
49 # Allocates temporary variables where needed, and fills
50 # in the result_code field of each node.
51 #
52 # ExprNode provides some convenience routines which
53 # perform both of the above phases. These should only
54 # be called from statement nodes, and only when no
55 # coercion nodes need to be added around the expression
56 # being analysed. In that case, the above two phases
57 # should be invoked separately.
58 #
59 # Framework code in ExprNode provides much of the common
60 # processing for the various phases. It makes use of the
61 # 'subexprs' class attribute of ExprNodes, which should
62 # contain a list of the names of attributes which can
63 # hold sub-nodes or sequences of sub-nodes.
64 #
65 # The framework makes use of a number of abstract methods.
66 # Their responsibilities are as follows.
67 #
68 # Declaration Analysis phase
69 #
70 # analyse_target_declaration
71 # Called during the Analyse Declarations phase to analyse
72 # the LHS of an assignment or argument of a del statement.
73 # Nodes which cannot be the LHS of an assignment need not
74 # implement it.
75 #
76 # Expression Analysis phase
77 #
78 # analyse_types
79 # - Call analyse_types on all sub-expressions.
80 # - Check operand types, and wrap coercion nodes around
81 # sub-expressions where needed.
82 # - Set the type of this node.
83 # - If a temporary variable will be required for the
84 # result, set the is_temp flag of this node.
85 #
86 # analyse_target_types
87 # Called during the Analyse Types phase to analyse
88 # the LHS of an assignment or argument of a del
89 # statement. Similar responsibilities to analyse_types.
90 #
91 # allocate_temps
92 # - Call allocate_temps for all sub-nodes.
93 # - Call allocate_temp for this node.
94 # - If a temporary was allocated, call release_temp on
95 # all sub-expressions.
96 #
97 # allocate_target_temps
98 # - Call allocate_temps on sub-nodes and allocate any other
99 # temps used during assignment.
100 # - Fill in result_code with a C lvalue if needed.
101 # - If a rhs node is supplied, call release_temp on it.
102 # - Call release_temp on sub-nodes and release any other
103 # temps used during assignment.
104 #
105 # calculate_result_code
106 # - Called during the Allocate Temps phase. Should return a
107 # C code fragment evaluating to the result. This is only
108 # called when the result is not a temporary.
109 #
110 # target_code
111 # Called by the default implementation of allocate_target_temps.
112 # Should return a C lvalue for assigning to the node. The default
113 # implementation calls calculate_result_code.
114 #
115 # check_const
116 # - Check that this node and its subnodes form a
117 # legal constant expression. If so, do nothing,
118 # otherwise call not_const.
119 #
120 # The default implementation of check_const
121 # assumes that the expression is not constant.
122 #
123 # check_const_addr
124 # - Same as check_const, except check that the
125 # expression is a C lvalue whose address is
126 # constant. Otherwise, call addr_not_const.
127 #
128 # The default implementation of calc_const_addr
129 # assumes that the expression is not a constant
130 # lvalue.
131 #
132 # Code Generation phase
133 #
134 # generate_evaluation_code
135 # - Call generate_evaluation_code for sub-expressions.
136 # - Perform the functions of generate_result_code
137 # (see below).
138 # - If result is temporary, call generate_disposal_code
139 # on all sub-expressions.
140 #
141 # A default implementation of generate_evaluation_code
142 # is provided which uses the following abstract method:
143 #
144 # generate_result_code
145 # - Generate any C statements necessary to calculate
146 # the result of this node from the results of its
147 # sub-expressions.
148 #
149 # generate_assignment_code
150 # Called on the LHS of an assignment.
151 # - Call generate_evaluation_code for sub-expressions.
152 # - Generate code to perform the assignment.
153 # - If the assignment absorbed a reference, call
154 # generate_post_assignment_code on the RHS,
155 # otherwise call generate_disposal_code on it.
156 #
157 # generate_deletion_code
158 # Called on an argument of a del statement.
159 # - Call generate_evaluation_code for sub-expressions.
160 # - Generate code to perform the deletion.
161 # - Call generate_disposal_code on all sub-expressions.
162 #
163 #
165 is_sequence_constructor = 0
166 is_attribute = 0
168 saved_subexpr_nodes = None
169 is_temp = 0
171 def get_child_attrs(self):
172 return self.subexprs
173 child_attrs = property(fget=get_child_attrs)
175 def not_implemented(self, method_name):
176 print_call_chain(method_name, "not implemented") ###
177 raise InternalError(
178 "%s.%s not implemented" %
179 (self.__class__.__name__, method_name))
181 def is_lvalue(self):
182 return 0
184 def is_ephemeral(self):
185 # An ephemeral node is one whose result is in
186 # a Python temporary and we suspect there are no
187 # other references to it. Certain operations are
188 # disallowed on such values, since they are
189 # likely to result in a dangling pointer.
190 return self.type.is_pyobject and self.is_temp
192 def subexpr_nodes(self):
193 # Extract a list of subexpression nodes based
194 # on the contents of the subexprs class attribute.
195 if self.saved_subexpr_nodes is None:
196 nodes = []
197 for name in self.subexprs:
198 item = getattr(self, name)
199 if item:
200 if isinstance(item, ExprNode):
201 nodes.append(item)
202 else:
203 nodes.extend(item)
204 self.saved_subexpr_nodes = nodes
205 return self.saved_subexpr_nodes
207 def result_as(self, type = None):
208 # Return the result code cast to the specified C type.
209 return typecast(type, self.ctype(), self.result_code)
211 def py_result(self):
212 # Return the result code cast to PyObject *.
213 return self.result_as(py_object_type)
215 def ctype(self):
216 # Return the native C type of the result (i.e. the
217 # C type of the result_code expression).
218 return self.result_ctype or self.type
220 def compile_time_value(self, denv):
221 # Return value of compile-time expression, or report error.
222 error(self.pos, "Invalid compile-time expression")
224 def compile_time_value_error(self, e):
225 error(self.pos, "Error in compile-time expression: %s: %s" % (
226 e.__class__.__name__, e))
228 # ------------- Declaration Analysis ----------------
230 def analyse_target_declaration(self, env):
231 error(self.pos, "Cannot assign to or delete this")
233 # ------------- Expression Analysis ----------------
235 def analyse_const_expression(self, env):
236 # Called during the analyse_declarations phase of a
237 # constant expression. Analyses the expression's type,
238 # checks whether it is a legal const expression,
239 # and determines its value.
240 self.analyse_types(env)
241 self.allocate_temps(env)
242 self.check_const()
244 def analyse_expressions(self, env):
245 # Convenience routine performing both the Type
246 # Analysis and Temp Allocation phases for a whole
247 # expression.
248 self.analyse_types(env)
249 self.allocate_temps(env)
251 def analyse_target_expression(self, env, rhs):
252 # Convenience routine performing both the Type
253 # Analysis and Temp Allocation phases for the LHS of
254 # an assignment.
255 self.analyse_target_types(env)
256 self.allocate_target_temps(env, rhs)
258 def analyse_boolean_expression(self, env):
259 # Analyse expression and coerce to a boolean.
260 self.analyse_types(env)
261 bool = self.coerce_to_boolean(env)
262 bool.allocate_temps(env)
263 return bool
265 def analyse_temp_boolean_expression(self, env):
266 # Analyse boolean expression and coerce result into
267 # a temporary. This is used when a branch is to be
268 # performed on the result and we won't have an
269 # opportunity to ensure disposal code is executed
270 # afterwards. By forcing the result into a temporary,
271 # we ensure that all disposal has been done by the
272 # time we get the result.
273 self.analyse_types(env)
274 bool = self.coerce_to_boolean(env)
275 temp_bool = bool.coerce_to_temp(env)
276 temp_bool.allocate_temps(env)
277 return temp_bool
279 # --------------- Type Analysis ------------------
281 def analyse_as_module(self, env):
282 # If this node can be interpreted as a reference to a
283 # cimported module, return its scope, else None.
284 return None
286 def analyse_as_extension_type(self, env):
287 # If this node can be interpreted as a reference to an
288 # extension type, return its type, else None.
289 return None
291 def analyse_types(self, env):
292 self.not_implemented("analyse_types")
294 def analyse_target_types(self, env):
295 self.analyse_types(env)
297 def gil_assignment_check(self, env):
298 if env.nogil and self.type.is_pyobject:
299 error(self.pos, "Assignment of Python object not allowed without gil")
301 def check_const(self):
302 self.not_const()
304 def not_const(self):
305 error(self.pos, "Not allowed in a constant expression")
307 def check_const_addr(self):
308 self.addr_not_const()
310 def addr_not_const(self):
311 error(self.pos, "Address is not constant")
313 def gil_check(self, env):
314 if env.nogil and self.type.is_pyobject:
315 self.gil_error()
317 # ----------------- Result Allocation -----------------
319 def result_in_temp(self):
320 # Return true if result is in a temporary owned by
321 # this node or one of its subexpressions. Overridden
322 # by certain nodes which can share the result of
323 # a subnode.
324 return self.is_temp
326 def allocate_target_temps(self, env, rhs):
327 # Perform temp allocation for the LHS of an assignment.
328 if debug_temp_alloc:
329 print("%s Allocating target temps" % self)
330 self.allocate_subexpr_temps(env)
331 self.result_code = self.target_code()
332 if rhs:
333 rhs.release_temp(env)
334 self.release_subexpr_temps(env)
336 def allocate_temps(self, env, result = None):
337 # Allocate temporary variables for this node and
338 # all its sub-expressions. If a result is specified,
339 # this must be a temp node and the specified variable
340 # is used as the result instead of allocating a new
341 # one.
342 if debug_temp_alloc:
343 print("%s Allocating temps" % self)
344 self.allocate_subexpr_temps(env)
345 self.allocate_temp(env, result)
346 if self.is_temp:
347 self.release_subexpr_temps(env)
349 def allocate_subexpr_temps(self, env):
350 # Allocate temporary variables for all sub-expressions
351 # of this node.
352 if debug_temp_alloc:
353 print("%s Allocating temps for: %s" % (self, self.subexprs))
354 for node in self.subexpr_nodes():
355 if node:
356 if debug_temp_alloc:
357 print("%s Allocating temps for %s" % (self, node))
358 node.allocate_temps(env)
360 def allocate_temp(self, env, result = None):
361 # If this node requires a temporary variable for its
362 # result, allocate one, otherwise set the result to
363 # a C code fragment. If a result is specified,
364 # this must be a temp node and the specified variable
365 # is used as the result instead of allocating a new
366 # one.
367 if debug_temp_alloc:
368 print("%s Allocating temp" % self)
369 if result:
370 if not self.is_temp:
371 raise InternalError("Result forced on non-temp node")
372 self.result_code = result
373 elif self.is_temp:
374 type = self.type
375 if not type.is_void:
376 if type.is_pyobject:
377 type = PyrexTypes.py_object_type
378 self.result_code = env.allocate_temp(type)
379 else:
380 self.result_code = None
381 if debug_temp_alloc:
382 print("%s Allocated result %s" % (self, self.result_code))
383 else:
384 self.result_code = self.calculate_result_code()
386 def target_code(self):
387 # Return code fragment for use as LHS of a C assignment.
388 return self.calculate_result_code()
390 def calculate_result_code(self):
391 self.not_implemented("calculate_result_code")
393 # def release_target_temp(self, env):
394 # # Release temporaries used by LHS of an assignment.
395 # self.release_subexpr_temps(env)
397 def release_temp(self, env):
398 # If this node owns a temporary result, release it,
399 # otherwise release results of its sub-expressions.
400 if self.is_temp:
401 if debug_temp_alloc:
402 print("%s Releasing result %s" % (self, self.result_code))
403 env.release_temp(self.result_code)
404 else:
405 self.release_subexpr_temps(env)
407 def release_subexpr_temps(self, env):
408 # Release the results of all sub-expressions of
409 # this node.
410 for node in self.subexpr_nodes():
411 if node:
412 node.release_temp(env)
414 # ---------------- Code Generation -----------------
416 def make_owned_reference(self, code):
417 # If result is a pyobject, make sure we own
418 # a reference to it.
419 if self.type.is_pyobject and not self.result_in_temp():
420 code.put_incref(self.result_code, self.ctype())
422 def generate_evaluation_code(self, code):
423 code.mark_pos(self.pos)
424 # Generate code to evaluate this node and
425 # its sub-expressions, and dispose of any
426 # temporary results of its sub-expressions.
427 self.generate_subexpr_evaluation_code(code)
428 self.generate_result_code(code)
429 if self.is_temp:
430 self.generate_subexpr_disposal_code(code)
432 def generate_subexpr_evaluation_code(self, code):
433 for node in self.subexpr_nodes():
434 node.generate_evaluation_code(code)
436 def generate_result_code(self, code):
437 self.not_implemented("generate_result_code")
439 def generate_disposal_code(self, code):
440 # If necessary, generate code to dispose of
441 # temporary Python reference.
442 if self.is_temp:
443 if self.type.is_pyobject:
444 code.put_decref_clear(self.result_code, self.ctype())
445 else:
446 self.generate_subexpr_disposal_code(code)
448 def generate_subexpr_disposal_code(self, code):
449 # Generate code to dispose of temporary results
450 # of all sub-expressions.
451 for node in self.subexpr_nodes():
452 node.generate_disposal_code(code)
454 def generate_post_assignment_code(self, code):
455 # Same as generate_disposal_code except that
456 # assignment will have absorbed a reference to
457 # the result if it is a Python object.
458 if self.is_temp:
459 if self.type.is_pyobject:
460 code.putln("%s = 0;" % self.result_code)
461 else:
462 self.generate_subexpr_disposal_code(code)
464 def generate_assignment_code(self, rhs, code):
465 # Stub method for nodes which are not legal as
466 # the LHS of an assignment. An error will have
467 # been reported earlier.
468 pass
470 def generate_deletion_code(self, code):
471 # Stub method for nodes that are not legal as
472 # the argument of a del statement. An error
473 # will have been reported earlier.
474 pass
476 # ---------------- Annotation ---------------------
478 def annotate(self, code):
479 for node in self.subexpr_nodes():
480 node.annotate(code)
482 # ----------------- Coercion ----------------------
484 def coerce_to(self, dst_type, env):
485 # Coerce the result so that it can be assigned to
486 # something of type dst_type. If processing is necessary,
487 # wraps this node in a coercion node and returns that.
488 # Otherwise, returns this node unchanged.
489 #
490 # This method is called during the analyse_expressions
491 # phase of the src_node's processing.
492 src = self
493 src_type = self.type
494 src_is_py_type = src_type.is_pyobject
495 dst_is_py_type = dst_type.is_pyobject
497 if dst_type.is_pyobject:
498 if not src.type.is_pyobject:
499 src = CoerceToPyTypeNode(src, env)
500 if not src.type.subtype_of(dst_type):
501 if not isinstance(src, NoneNode):
502 src = PyTypeTestNode(src, dst_type, env)
503 elif src.type.is_pyobject:
504 src = CoerceFromPyTypeNode(dst_type, src, env)
505 else: # neither src nor dst are py types
506 # Added the string comparison, since for c types that
507 # is enough, but Cython gets confused when the types are
508 # in different files.
509 if not (str(src.type) == str(dst_type) or dst_type.assignable_from(src_type)):
510 error(self.pos, "Cannot assign type '%s' to '%s'" %
511 (src.type, dst_type))
512 return src
514 def coerce_to_pyobject(self, env):
515 return self.coerce_to(PyrexTypes.py_object_type, env)
517 def coerce_to_boolean(self, env):
518 # Coerce result to something acceptable as
519 # a boolean value.
520 type = self.type
521 if type.is_pyobject or type.is_ptr or type.is_float:
522 return CoerceToBooleanNode(self, env)
523 else:
524 if not type.is_int and not type.is_error:
525 error(self.pos,
526 "Type '%s' not acceptable as a boolean" % type)
527 return self
529 def coerce_to_integer(self, env):
530 # If not already some C integer type, coerce to longint.
531 if self.type.is_int:
532 return self
533 else:
534 return self.coerce_to(PyrexTypes.c_long_type, env)
536 def coerce_to_temp(self, env):
537 # Ensure that the result is in a temporary.
538 if self.result_in_temp():
539 return self
540 else:
541 return CoerceToTempNode(self, env)
543 def coerce_to_simple(self, env):
544 # Ensure that the result is simple (see is_simple).
545 if self.is_simple():
546 return self
547 else:
548 return self.coerce_to_temp(env)
550 def is_simple(self):
551 # A node is simple if its result is something that can
552 # be referred to without performing any operations, e.g.
553 # a constant, local var, C global var, struct member
554 # reference, or temporary.
555 return self.result_in_temp()
558 class AtomicExprNode(ExprNode):
559 # Abstract base class for expression nodes which have
560 # no sub-expressions.
562 subexprs = []
565 class PyConstNode(AtomicExprNode):
566 # Abstract base class for constant Python values.
568 is_literal = 1
570 def is_simple(self):
571 return 1
573 def analyse_types(self, env):
574 self.type = py_object_type
576 def calculate_result_code(self):
577 return self.value
579 def generate_result_code(self, code):
580 pass
583 class NoneNode(PyConstNode):
584 # The constant value None
586 value = "Py_None"
588 def compile_time_value(self, denv):
589 return None
591 class EllipsisNode(PyConstNode):
592 # '...' in a subscript list.
594 value = "Py_Ellipsis"
596 def compile_time_value(self, denv):
597 return Ellipsis
600 class ConstNode(AtomicExprNode):
601 # Abstract base type for literal constant nodes.
602 #
603 # value string C code fragment
605 is_literal = 1
607 def is_simple(self):
608 return 1
610 def analyse_types(self, env):
611 pass # Types are held in class variables
613 def check_const(self):
614 pass
616 def calculate_result_code(self):
617 return str(self.value)
619 def generate_result_code(self, code):
620 pass
623 class BoolNode(ConstNode):
624 type = PyrexTypes.c_bint_type
625 # The constant value True or False
627 def compile_time_value(self, denv):
628 return self.value
630 def calculate_result_code(self):
631 return int(self.value)
633 class NullNode(ConstNode):
634 type = PyrexTypes.c_null_ptr_type
635 value = "NULL"
638 class CharNode(ConstNode):
639 type = PyrexTypes.c_char_type
641 def compile_time_value(self, denv):
642 return ord(self.value)
644 def calculate_result_code(self):
645 return "'%s'" % self.value
648 class IntNode(ConstNode):
650 # unsigned "" or "U"
651 # longness "" or "L" or "LL"
653 unsigned = ""
654 longness = ""
655 type = PyrexTypes.c_long_type
657 def coerce_to(self, dst_type, env):
658 # Arrange for a Python version of the string to be pre-allocated
659 # when coercing to a Python type.
660 if dst_type.is_pyobject:
661 self.entry = env.get_py_num(self.value, self.longness)
662 self.type = PyrexTypes.py_object_type
663 # We still need to perform normal coerce_to processing on the
664 # result, because we might be coercing to an extension type,
665 # in which case a type test node will be needed.
666 return ConstNode.coerce_to(self, dst_type, env)
668 def calculate_result_code(self):
669 if self.type.is_pyobject:
670 return self.entry.cname
671 else:
672 return str(self.value) + self.unsigned + self.longness
674 def compile_time_value(self, denv):
675 return int(self.value, 0)
678 class FloatNode(ConstNode):
679 type = PyrexTypes.c_double_type
681 def compile_time_value(self, denv):
682 return float(self.value)
684 def calculate_result_code(self):
685 strval = str(self.value)
686 if strval == 'nan':
687 return "(Py_HUGE_VAL * 0)"
688 elif strval == 'inf':
689 return "Py_HUGE_VAL"
690 elif strval == '-inf':
691 return "(-Py_HUGE_VAL)"
692 else:
693 return strval
696 class StringNode(ConstNode):
697 # entry Symtab.Entry
699 type = PyrexTypes.c_char_ptr_type
701 def compile_time_value(self, denv):
702 return self.value
704 def analyse_types(self, env):
705 self.entry = env.add_string_const(self.value)
707 def coerce_to(self, dst_type, env):
708 if dst_type.is_int:
709 if not self.type.is_pyobject and len(self.entry.init) == 1:
710 # we use the *encoded* value here
711 return CharNode(self.pos, value=self.entry.init)
712 else:
713 error(self.pos, "Only coerce single-character ascii strings can be used as ints.")
714 return self
715 # Arrange for a Python version of the string to be pre-allocated
716 # when coercing to a Python type.
717 if dst_type.is_pyobject and not self.type.is_pyobject:
718 node = self.as_py_string_node(env)
719 else:
720 node = self
721 # We still need to perform normal coerce_to processing on the
722 # result, because we might be coercing to an extension type,
723 # in which case a type test node will be needed.
724 return ConstNode.coerce_to(node, dst_type, env)
726 def as_py_string_node(self, env):
727 # Return a new StringNode with the same entry as this node
728 # but whose type is a Python type instead of a C type.
729 entry = self.entry
730 env.add_py_string(entry)
731 return StringNode(self.pos, entry = entry, type = py_object_type)
733 def calculate_result_code(self):
734 if self.type.is_pyobject:
735 return self.entry.pystring_cname
736 else:
737 return self.entry.cname
740 class IdentifierStringNode(ConstNode):
741 # A Python string that behaves like an identifier, e.g. for
742 # keyword arguments in a call, or for imported names
743 type = PyrexTypes.py_object_type
745 def analyse_types(self, env):
746 self.cname = env.intern_identifier(self.value)
748 def calculate_result_code(self):
749 return self.cname
752 class LongNode(AtomicExprNode):
753 # Python long integer literal
754 #
755 # value string
757 def compile_time_value(self, denv):
758 return long(self.value)
760 gil_message = "Constructing Python long int"
762 def analyse_types(self, env):
763 self.type = py_object_type
764 self.gil_check(env)
765 self.is_temp = 1
767 gil_message = "Constructing Python long int"
769 def generate_evaluation_code(self, code):
770 code.putln(
771 '%s = PyLong_FromString("%s", 0, 0); %s' % (
772 self.result_code,
773 self.value,
774 code.error_goto_if_null(self.result_code, self.pos)))
777 class ImagNode(AtomicExprNode):
778 # Imaginary number literal
779 #
780 # value float imaginary part
782 def compile_time_value(self, denv):
783 return complex(0.0, self.value)
785 def analyse_types(self, env):
786 self.type = py_object_type
787 self.gil_check(env)
788 self.is_temp = 1
790 gil_message = "Constructing complex number"
792 def generate_evaluation_code(self, code):
793 code.putln(
794 "%s = PyComplex_FromDoubles(0.0, %s); %s" % (
795 self.result_code,
796 self.value,
797 code.error_goto_if_null(self.result_code, self.pos)))
800 class NameNode(AtomicExprNode):
801 # Reference to a local or global variable name.
802 #
803 # name string Python name of the variable
804 #
805 # entry Entry Symbol table entry
806 # interned_cname string
808 is_name = 1
809 skip_assignment_decref = False
810 entry = None
812 def create_analysed_rvalue(pos, env, entry):
813 node = NameNode(pos)
814 node.analyse_types(env, entry=entry)
815 return node
817 create_analysed_rvalue = staticmethod(create_analysed_rvalue)
819 def compile_time_value(self, denv):
820 try:
821 return denv.lookup(self.name)
822 except KeyError:
823 error(self.pos, "Compile-time name '%s' not defined" % self.name)
825 def coerce_to(self, dst_type, env):
826 # If coercing to a generic pyobject and this is a builtin
827 # C function with a Python equivalent, manufacture a NameNode
828 # referring to the Python builtin.
829 #print "NameNode.coerce_to:", self.name, dst_type ###
830 if dst_type is py_object_type:
831 entry = self.entry
832 if entry and entry.is_cfunction:
833 var_entry = entry.as_variable
834 if var_entry:
835 if var_entry.is_builtin and Options.cache_builtins:
836 var_entry = env.declare_builtin(var_entry.name, self.pos)
837 node = NameNode(self.pos, name = self.name)
838 node.entry = var_entry
839 node.analyse_rvalue_entry(env)
840 return node
841 return AtomicExprNode.coerce_to(self, dst_type, env)
843 def analyse_as_module(self, env):
844 # Try to interpret this as a reference to a cimported module.
845 # Returns the module scope, or None.
846 entry = self.entry
847 if not entry:
848 entry = env.lookup(self.name)
849 if entry and entry.as_module:
850 return entry.as_module
851 return None
853 def analyse_as_extension_type(self, env):
854 # Try to interpret this as a reference to an extension type.
855 # Returns the extension type, or None.
856 entry = self.entry
857 if not entry:
858 entry = env.lookup(self.name)
859 if entry and entry.is_type and entry.type.is_extension_type:
860 return entry.type
861 else:
862 return None
864 def analyse_target_declaration(self, env):
865 if not self.entry:
866 self.entry = env.lookup_here(self.name)
867 if not self.entry:
868 self.entry = env.declare_var(self.name, py_object_type, self.pos)
869 env.control_flow.set_state(self.pos, (self.name, 'initalized'), True)
870 env.control_flow.set_state(self.pos, (self.name, 'source'), 'assignment')
871 if self.entry.is_declared_generic:
872 self.result_ctype = py_object_type
873 if self.entry.is_pyglobal and self.entry.is_member:
874 env.use_utility_code(type_cache_invalidation_code)
876 def analyse_types(self, env):
877 if self.entry is None:
878 self.entry = env.lookup(self.name)
879 if not self.entry:
880 self.entry = env.declare_builtin(self.name, self.pos)
881 if not self.entry:
882 self.type = PyrexTypes.error_type
883 return
884 self.analyse_rvalue_entry(env)
886 def analyse_target_types(self, env):
887 self.analyse_entry(env)
888 if not self.is_lvalue():
889 error(self.pos, "Assignment to non-lvalue '%s'"
890 % self.name)
891 self.type = PyrexTypes.error_type
892 self.entry.used = 1
893 if self.entry.type.is_buffer:
894 # Have an rhs temp just in case. All rhs I could
895 # think of had a single symbol result_code but better
896 # safe than sorry. Feel free to change this.
897 import Buffer
898 Buffer.used_buffer_aux_vars(self.entry)
900 def analyse_rvalue_entry(self, env):
901 #print "NameNode.analyse_rvalue_entry:", self.name ###
902 #print "Entry:", self.entry.__dict__ ###
903 self.analyse_entry(env)
904 entry = self.entry
905 if entry.is_declared_generic:
906 self.result_ctype = py_object_type
907 if entry.is_pyglobal or entry.is_builtin:
908 if Options.cache_builtins and entry.is_builtin:
909 self.is_temp = 0
910 else:
911 self.is_temp = 1
912 env.use_utility_code(get_name_interned_utility_code)
913 self.gil_check(env)
915 gil_message = "Accessing Python global or builtin"
917 def analyse_entry(self, env):
918 #print "NameNode.analyse_entry:", self.name ###
919 self.check_identifier_kind()
920 entry = self.entry
921 type = entry.type
922 self.type = type
923 if entry.is_pyglobal or entry.is_builtin:
924 assert type.is_pyobject, "Python global or builtin not a Python object"
925 self.interned_cname = self.entry.interned_cname = \
926 env.intern_identifier(self.entry.name)
928 def check_identifier_kind(self):
929 #print "NameNode.check_identifier_kind:", self.entry.name ###
930 #print self.entry.__dict__ ###
931 entry = self.entry
932 #entry.used = 1
933 if not (entry.is_const or entry.is_variable
934 or entry.is_builtin or entry.is_cfunction):
935 if self.entry.as_variable:
936 self.entry = self.entry.as_variable
937 else:
938 error(self.pos,
939 "'%s' is not a constant, variable or function identifier" % self.name)
941 def is_simple(self):
942 # If it's not a C variable, it'll be in a temp.
943 return 1
945 def calculate_target_results(self, env):
946 pass
948 def check_const(self):
949 entry = self.entry
950 if entry is not None and not (entry.is_const or entry.is_cfunction or entry.is_builtin):
951 self.not_const()
953 def check_const_addr(self):
954 entry = self.entry
955 if not (entry.is_cglobal or entry.is_cfunction or entry.is_builtin):
956 self.addr_not_const()
958 def is_lvalue(self):
959 return self.entry.is_variable and \
960 not self.entry.type.is_array and \
961 not self.entry.is_readonly
963 def is_ephemeral(self):
964 # Name nodes are never ephemeral, even if the
965 # result is in a temporary.
966 return 0
968 def allocate_temp(self, env, result = None):
969 AtomicExprNode.allocate_temp(self, env, result)
970 entry = self.entry
971 if entry:
972 entry.used = 1
973 if entry.utility_code:
974 env.use_utility_code(entry.utility_code)
976 def calculate_result_code(self):
977 entry = self.entry
978 if not entry:
979 return "<error>" # There was an error earlier
980 return entry.cname
982 def generate_result_code(self, code):
983 assert hasattr(self, 'entry')
984 entry = self.entry
985 if entry is None:
986 return # There was an error earlier
987 if entry.is_builtin and Options.cache_builtins:
988 return # Lookup already cached
989 elif entry.is_pyglobal or entry.is_builtin:
990 if entry.is_builtin:
991 namespace = Naming.builtins_cname
992 else: # entry.is_pyglobal
993 namespace = entry.scope.namespace_cname
994 code.putln(
995 '%s = __Pyx_GetName(%s, %s); %s' % (
996 self.result_code,
997 namespace,
998 self.interned_cname,
999 code.error_goto_if_null(self.result_code, self.pos)))
1000 elif entry.is_local and False:
1001 # control flow not good enough yet
1002 assigned = entry.scope.control_flow.get_state((entry.name, 'initalized'), self.pos)
1003 if assigned is False:
1004 error(self.pos, "local variable '%s' referenced before assignment" % entry.name)
1005 elif not Options.init_local_none and assigned is None:
1006 code.putln('if (%s == 0) { PyErr_SetString(PyExc_UnboundLocalError, "%s"); %s }' % (entry.cname, entry.name, code.error_goto(self.pos)))
1007 entry.scope.control_flow.set_state(self.pos, (entry.name, 'initalized'), True)
1009 def generate_assignment_code(self, rhs, code):
1010 #print "NameNode.generate_assignment_code:", self.name ###
1011 entry = self.entry
1012 if entry is None:
1013 return # There was an error earlier
1015 # is_pyglobal seems to be True for module level-globals only.
1016 # We use this to access class->tp_dict if necessary.
1017 if entry.is_pyglobal:
1018 namespace = self.entry.scope.namespace_cname
1019 if entry.is_member:
1020 # if the entry is a member we have to cheat: SetAttr does not work
1021 # on types, so we create a descriptor which is then added to tp_dict
1022 code.put_error_if_neg(self.pos,
1023 'PyDict_SetItem(%s->tp_dict, %s, %s)' % (
1024 namespace,
1025 self.interned_cname,
1026 rhs.py_result()))
1027 # in Py2.6+, we need to invalidate the method cache
1028 code.putln("__Pyx_TypeModified(%s);" %
1029 entry.scope.parent_type.typeptr_cname)
1030 else:
1031 code.put_error_if_neg(self.pos,
1032 'PyObject_SetAttr(%s, %s, %s)' % (
1033 namespace,
1034 self.interned_cname,
1035 rhs.py_result()))
1036 if debug_disposal_code:
1037 print("NameNode.generate_assignment_code:")
1038 print("...generating disposal code for %s" % rhs)
1039 rhs.generate_disposal_code(code)
1041 else:
1042 if self.type.is_buffer:
1043 # Generate code for doing the buffer release/acquisition.
1044 # This might raise an exception in which case the assignment (done
1045 # below) will not happen.
1047 # The reason this is not in a typetest-like node is because the
1048 # variables that the acquired buffer info is stored to is allocated
1049 # per entry and coupled with it.
1050 self.generate_acquire_buffer(rhs, code)
1052 if self.type.is_pyobject:
1053 rhs.make_owned_reference(code)
1054 #print "NameNode.generate_assignment_code: to", self.name ###
1055 #print "...from", rhs ###
1056 #print "...LHS type", self.type, "ctype", self.ctype() ###
1057 #print "...RHS type", rhs.type, "ctype", rhs.ctype() ###
1058 if not self.skip_assignment_decref:
1059 if entry.is_local and not Options.init_local_none:
1060 initalized = entry.scope.control_flow.get_state((entry.name, 'initalized'), self.pos)
1061 if initalized is True:
1062 code.put_decref(self.result_code, self.ctype())
1063 elif initalized is None:
1064 code.put_xdecref(self.result_code, self.ctype())
1065 else:
1066 code.put_decref(self.result_code, self.ctype())
1067 code.putln('%s = %s;' % (self.result_code, rhs.result_as(self.ctype())))
1068 if debug_disposal_code:
1069 print("NameNode.generate_assignment_code:")
1070 print("...generating post-assignment code for %s" % rhs)
1071 rhs.generate_post_assignment_code(code)
1073 def generate_acquire_buffer(self, rhs, code):
1074 rhstmp = code.func.allocate_temp(self.entry.type)
1075 buffer_aux = self.entry.buffer_aux
1076 bufstruct = buffer_aux.buffer_info_var.cname
1077 code.putln('%s = %s;' % (rhstmp, rhs.result_as(self.ctype())))
1079 import Buffer
1080 Buffer.put_assign_to_buffer(self.result_code, rhstmp, buffer_aux, self.entry.type,
1081 is_initialized=not self.skip_assignment_decref,
1082 pos=self.pos, code=code)
1083 code.putln("%s = 0;" % rhstmp)
1084 code.func.release_temp(rhstmp)
1086 def generate_deletion_code(self, code):
1087 if self.entry is None:
1088 return # There was an error earlier
1089 if not self.entry.is_pyglobal:
1090 error(self.pos, "Deletion of local or C global name not supported")
1091 return
1092 code.put_error_if_neg(self.pos,
1093 'PyObject_DelAttrString(%s, "%s")' % (
1094 Naming.module_cname,
1095 self.entry.name))
1097 def annotate(self, code):
1098 if hasattr(self, 'is_called') and self.is_called:
1099 pos = (self.pos[0], self.pos[1], self.pos[2] - len(self.name) - 1)
1100 if self.type.is_pyobject:
1101 code.annotate(pos, AnnotationItem('py_call', 'python function', size=len(self.name)))
1102 else:
1103 code.annotate(pos, AnnotationItem('c_call', 'c function', size=len(self.name)))
1105 class BackquoteNode(ExprNode):
1106 # `expr`
1108 # arg ExprNode
1110 subexprs = ['arg']
1112 def analyse_types(self, env):
1113 self.arg.analyse_types(env)
1114 self.arg = self.arg.coerce_to_pyobject(env)
1115 self.type = py_object_type
1116 self.gil_check(env)
1117 self.is_temp = 1
1119 gil_message = "Backquote expression"
1121 def generate_result_code(self, code):
1122 code.putln(
1123 "%s = PyObject_Repr(%s); %s" % (
1124 self.result_code,
1125 self.arg.py_result(),
1126 code.error_goto_if_null(self.result_code, self.pos)))
1129 class ImportNode(ExprNode):
1130 # Used as part of import statement implementation.
1131 # Implements result =
1132 # __import__(module_name, globals(), None, name_list)
1134 # module_name IdentifierStringNode dotted name of module
1135 # name_list ListNode or None list of names to be imported
1137 subexprs = ['module_name', 'name_list']
1139 def analyse_types(self, env):
1140 self.module_name.analyse_types(env)
1141 self.module_name = self.module_name.coerce_to_pyobject(env)
1142 if self.name_list:
1143 self.name_list.analyse_types(env)
1144 self.type = py_object_type
1145 self.gil_check(env)
1146 self.is_temp = 1
1147 env.use_utility_code(import_utility_code)
1149 gil_message = "Python import"
1151 def generate_result_code(self, code):
1152 if self.name_list:
1153 name_list_code = self.name_list.py_result()
1154 else:
1155 name_list_code = "0"
1156 code.putln(
1157 "%s = __Pyx_Import(%s, %s); %s" % (
1158 self.result_code,
1159 self.module_name.py_result(),
1160 name_list_code,
1161 code.error_goto_if_null(self.result_code, self.pos)))
1164 class IteratorNode(ExprNode):
1165 # Used as part of for statement implementation.
1166 # Implements result = iter(sequence)
1168 # sequence ExprNode
1170 subexprs = ['sequence']
1172 def analyse_types(self, env):
1173 self.sequence.analyse_types(env)
1174 self.sequence = self.sequence.coerce_to_pyobject(env)
1175 self.type = py_object_type
1176 self.gil_check(env)
1177 self.is_temp = 1
1179 self.counter = TempNode(self.pos, PyrexTypes.c_py_ssize_t_type, env)
1180 self.counter.allocate_temp(env)
1182 gil_message = "Iterating over Python object"
1184 def release_temp(self, env):
1185 env.release_temp(self.result_code)
1186 self.counter.release_temp(env)
1188 def generate_result_code(self, code):
1189 code.putln(
1190 "if (PyList_CheckExact(%s) || PyTuple_CheckExact(%s)) {" % (
1191 self.sequence.py_result(),
1192 self.sequence.py_result()))
1193 code.putln(
1194 "%s = 0; %s = %s; Py_INCREF(%s);" % (
1195 self.counter.result_code,
1196 self.result_code,
1197 self.sequence.py_result(),
1198 self.result_code))
1199 code.putln("} else {")
1200 code.putln("%s = -1; %s = PyObject_GetIter(%s); %s" % (
1201 self.counter.result_code,
1202 self.result_code,
1203 self.sequence.py_result(),
1204 code.error_goto_if_null(self.result_code, self.pos)))
1205 code.putln("}")
1208 class NextNode(AtomicExprNode):
1209 # Used as part of for statement implementation.
1210 # Implements result = iterator.next()
1211 # Created during analyse_types phase.
1212 # The iterator is not owned by this node.
1214 # iterator ExprNode
1216 def __init__(self, iterator, env):
1217 self.pos = iterator.pos
1218 self.iterator = iterator
1219 self.type = py_object_type
1220 self.is_temp = 1
1222 def generate_result_code(self, code):
1223 for py_type in ["List", "Tuple"]:
1224 code.putln(
1225 "if (likely(Py%s_CheckExact(%s))) {" % (py_type, self.iterator.py_result()))
1226 code.putln(
1227 "if (%s >= Py%s_GET_SIZE(%s)) break;" % (
1228 self.iterator.counter.result_code,
1229 py_type,
1230 self.iterator.py_result()))
1231 code.putln(
1232 "%s = Py%s_GET_ITEM(%s, %s); Py_INCREF(%s); %s++;" % (
1233 self.result_code,
1234 py_type,
1235 self.iterator.py_result(),
1236 self.iterator.counter.result_code,
1237 self.result_code,
1238 self.iterator.counter.result_code))
1239 code.put("} else ")
1240 code.putln("{")
1241 code.putln(
1242 "%s = PyIter_Next(%s);" % (
1243 self.result_code,
1244 self.iterator.py_result()))
1245 code.putln(
1246 "if (!%s) {" %
1247 self.result_code)
1248 code.putln(code.error_goto_if_PyErr(self.pos))
1249 code.putln("break;")
1250 code.putln("}")
1251 code.putln("}")
1254 class ExcValueNode(AtomicExprNode):
1255 # Node created during analyse_types phase
1256 # of an ExceptClauseNode to fetch the current
1257 # exception value.
1259 def __init__(self, pos, env, var):
1260 ExprNode.__init__(self, pos)
1261 self.type = py_object_type
1262 self.var = var
1264 def calculate_result_code(self):
1265 return self.var
1267 def generate_result_code(self, code):
1268 pass
1270 def analyse_types(self, env):
1271 pass
1274 class TempNode(AtomicExprNode):
1275 # Node created during analyse_types phase
1276 # of some nodes to hold a temporary value.
1278 def __init__(self, pos, type, env):
1279 ExprNode.__init__(self, pos)
1280 self.type = type
1281 if type.is_pyobject:
1282 self.result_ctype = py_object_type
1283 self.is_temp = 1
1285 def analyse_types(self, env):
1286 return self.type
1288 def generate_result_code(self, code):
1289 pass
1292 class PyTempNode(TempNode):
1293 # TempNode holding a Python value.
1295 def __init__(self, pos, env):
1296 TempNode.__init__(self, pos, PyrexTypes.py_object_type, env)
1299 #-------------------------------------------------------------------
1301 # Trailer nodes
1303 #-------------------------------------------------------------------
1305 class IndexNode(ExprNode):
1306 # Sequence indexing.
1308 # base ExprNode
1309 # index ExprNode
1310 # indices [ExprNode]
1311 # is_buffer_access boolean Whether this is a buffer access.
1313 # indices is used on buffer access, index on non-buffer access.
1314 # The former contains a clean list of index parameters, the
1315 # latter whatever Python object is needed for index access.
1317 subexprs = ['base', 'index', 'indices']
1318 indices = None
1320 def __init__(self, pos, index, *args, **kw):
1321 ExprNode.__init__(self, pos, index=index, *args, **kw)
1322 self._index = index
1324 def compile_time_value(self, denv):
1325 base = self.base.compile_time_value(denv)
1326 index = self.index.compile_time_value(denv)
1327 try:
1328 return base[index]
1329 except Exception, e:
1330 self.compile_time_value_error(e)
1332 def is_ephemeral(self):
1333 return self.base.is_ephemeral()
1335 def analyse_target_declaration(self, env):
1336 pass
1338 def analyse_types(self, env):
1339 self.analyse_base_and_index_types(env, getting = 1)
1341 def analyse_target_types(self, env):
1342 self.analyse_base_and_index_types(env, setting = 1)
1344 def analyse_base_and_index_types(self, env, getting = 0, setting = 0):
1345 # Note: This might be cleaned up by having IndexNode
1346 # parsed in a saner way and only construct the tuple if
1347 # needed.
1348 self.is_buffer_access = False
1350 self.base.analyse_types(env)
1352 skip_child_analysis = False
1353 buffer_access = False
1354 if self.base.type.is_buffer:
1355 assert isinstance(self.base, NameNode)
1356 if isinstance(self.index, TupleNode):
1357 indices = self.index.args
1358 else:
1359 indices = [self.index]
1360 if len(indices) == self.base.type.ndim:
1361 buffer_access = True
1362 skip_child_analysis = True
1363 for x in indices:
1364 x.analyse_types(env)
1365 if not x.type.is_int:
1366 buffer_access = False
1368 if buffer_access:
1369 self.indices = indices
1370 self.index = None
1371 self.type = self.base.type.dtype
1372 self.is_buffer_access = True
1374 if getting:
1375 # we only need a temp because result_code isn't refactored to
1376 # generation time, but this seems an ok shortcut to take
1377 self.is_temp = True
1378 if setting:
1379 if not self.base.entry.type.writable:
1380 error(self.pos, "Writing to readonly buffer")
1381 else:
1382 self.base.entry.buffer_aux.writable_needed = True
1383 else:
1384 if isinstance(self.index, TupleNode):
1385 self.index.analyse_types(env, skip_children=skip_child_analysis)
1386 elif not skip_child_analysis:
1387 self.index.analyse_types(env)
1388 if self.base.type.is_pyobject:
1389 if self.index.type.is_int:
1390 self.original_index_type = self.index.type
1391 self.index = self.index.coerce_to(PyrexTypes.c_py_ssize_t_type, env).coerce_to_simple(env)
1392 if getting:
1393 env.use_utility_code(getitem_int_utility_code)
1394 if setting:
1395 env.use_utility_code(setitem_int_utility_code)
1396 else:
1397 self.index = self.index.coerce_to_pyobject(env)
1398 self.type = py_object_type
1399 self.gil_check(env)
1400 self.is_temp = 1
1401 else:
1402 if self.base.type.is_ptr or self.base.type.is_array:
1403 self.type = self.base.type.base_type
1404 else:
1405 error(self.pos,
1406 "Attempting to index non-array type '%s'" %
1407 self.base.type)
1408 self.type = PyrexTypes.error_type
1409 if self.index.type.is_pyobject:
1410 self.index = self.index.coerce_to(
1411 PyrexTypes.c_py_ssize_t_type, env)
1412 if not self.index.type.is_int:
1413 error(self.pos,
1414 "Invalid index type '%s'" %
1415 self.index.type)
1417 gil_message = "Indexing Python object"
1419 def check_const_addr(self):
1420 self.base.check_const_addr()
1421 self.index.check_const()
1423 def is_lvalue(self):
1424 return 1
1426 def calculate_result_code(self):
1427 if self.is_buffer_access:
1428 return "<not used>"
1429 else:
1430 return "(%s[%s])" % (
1431 self.base.result_code, self.index.result_code)
1433 def index_unsigned_parameter(self):
1434 if self.index.type.is_int:
1435 if self.original_index_type.signed:
1436 return ", 0"
1437 else:
1438 return ", sizeof(Py_ssize_t) <= sizeof(%s)" % self.original_index_type.declaration_code("")
1439 else:
1440 return ""
1442 def generate_subexpr_evaluation_code(self, code):
1443 self.base.generate_evaluation_code(code)
1444 if self.index is not None:
1445 self.index.generate_evaluation_code(code)
1446 else:
1447 for i in self.indices:
1448 i.generate_evaluation_code(code)
1450 def generate_subexpr_disposal_code(self, code):
1451 self.base.generate_disposal_code(code)
1452 if self.index is not None:
1453 self.index.generate_disposal_code(code)
1454 else:
1455 for i in self.indices:
1456 i.generate_disposal_code(code)
1458 def generate_result_code(self, code):
1459 if self.is_buffer_access:
1460 valuecode = self.buffer_access_code(code)
1461 code.putln("%s = %s;" % (self.result_code, valuecode))
1462 elif self.type.is_pyobject:
1463 if self.index.type.is_int:
1464 function = "__Pyx_GetItemInt"
1465 index_code = self.index.result_code
1466 else:
1467 function = "PyObject_GetItem"
1468 index_code = self.index.py_result()
1469 sign_code = ""
1470 code.putln(
1471 "%s = %s(%s, %s%s); if (!%s) %s" % (
1472 self.result_code,
1473 function,
1474 self.base.py_result(),
1475 index_code,
1476 self.index_unsigned_parameter(),
1477 self.result_code,
1478 code.error_goto(self.pos)))
1480 def generate_setitem_code(self, value_code, code):
1481 if self.index.type.is_int:
1482 function = "__Pyx_SetItemInt"
1483 index_code = self.index.result_code
1484 else:
1485 function = "PyObject_SetItem"
1486 index_code = self.index.py_result()
1487 code.putln(
1488 "if (%s(%s, %s, %s%s) < 0) %s" % (
1489 function,
1490 self.base.py_result(),
1491 index_code,
1492 value_code,
1493 self.index_unsigned_parameter(),
1494 code.error_goto(self.pos)))
1496 def generate_assignment_code(self, rhs, code):
1497 self.generate_subexpr_evaluation_code(code)
1498 if self.is_buffer_access:
1499 valuecode = self.buffer_access_code(code)
1500 code.putln("%s = %s;" % (valuecode, rhs.result_code))
1501 elif self.type.is_pyobject:
1502 self.generate_setitem_code(rhs.py_result(), code)
1503 else:
1504 code.putln(
1505 "%s = %s;" % (
1506 self.result_code, rhs.result_code))
1507 self.generate_subexpr_disposal_code(code)
1508 rhs.generate_disposal_code(code)
1510 def generate_deletion_code(self, code):
1511 self.generate_subexpr_evaluation_code(code)
1512 #if self.type.is_pyobject:
1513 if self.index.type.is_int:
1514 function = "PySequence_DelItem"
1515 index_code = self.index.result_code
1516 else:
1517 function = "PyObject_DelItem"
1518 index_code = self.index.py_result()
1519 code.putln(
1520 "if (%s(%s, %s) < 0) %s" % (
1521 function,
1522 self.base.py_result(),
1523 index_code,
1524 code.error_goto(self.pos)))
1525 self.generate_subexpr_disposal_code(code)
1527 def buffer_access_code(self, code):
1528 # Assign indices to temps
1529 index_temps = [code.func.allocate_temp(i.type) for i in self.indices]
1530 for temp, index in zip(index_temps, self.indices):
1531 code.putln("%s = %s;" % (temp, index.result_code))
1532 # Generate buffer access code using these temps
1533 import Buffer
1534 valuecode = Buffer.put_access(entry=self.base.entry,
1535 index_signeds=[i.type.signed for i in self.indices],
1536 index_cnames=index_temps,
1537 pos=self.pos, code=code)
1538 return valuecode
1541 class SliceIndexNode(ExprNode):
1542 # 2-element slice indexing
1544 # base ExprNode
1545 # start ExprNode or None
1546 # stop ExprNode or None
1548 subexprs = ['base', 'start', 'stop']
1550 def compile_time_value(self, denv):
1551 base = self.base.compile_time_value(denv)
1552 start = self.start.compile_time_value(denv)
1553 stop = self.stop.compile_time_value(denv)
1554 try:
1555 return base[start:stop]
1556 except Exception, e:
1557 self.compile_time_value_error(e)
1559 def analyse_target_declaration(self, env):
1560 pass
1562 def analyse_types(self, env):
1563 self.base.analyse_types(env)
1564 if self.start:
1565 self.start.analyse_types(env)
1566 if self.stop:
1567 self.stop.analyse_types(env)
1568 self.base = self.base.coerce_to_pyobject(env)
1569 c_int = PyrexTypes.c_py_ssize_t_type
1570 if self.start:
1571 self.start = self.start.coerce_to(c_int, env)
1572 if self.stop:
1573 self.stop = self.stop.coerce_to(c_int, env)
1574 self.type = py_object_type
1575 self.gil_check(env)
1576 self.is_temp = 1
1578 gil_message = "Slicing Python object"
1580 def generate_result_code(self, code):
1581 code.putln(
1582 "%s = PySequence_GetSlice(%s, %s, %s); %s" % (
1583 self.result_code,
1584 self.base.py_result(),
1585 self.start_code(),
1586 self.stop_code(),
1587 code.error_goto_if_null(self.result_code, self.pos)))
1589 def generate_assignment_code(self, rhs, code):
1590 self.generate_subexpr_evaluation_code(code)
1591 code.put_error_if_neg(self.pos,
1592 "PySequence_SetSlice(%s, %s, %s, %s)" % (
1593 self.base.py_result(),
1594 self.start_code(),
1595 self.stop_code(),
1596 rhs.result_code))
1597 self.generate_subexpr_disposal_code(code)
1598 rhs.generate_disposal_code(code)
1600 def generate_deletion_code(self, code):
1601 self.generate_subexpr_evaluation_code(code)
1602 code.put_error_if_neg(self.pos,
1603 "PySequence_DelSlice(%s, %s, %s)" % (
1604 self.base.py_result(),
1605 self.start_code(),
1606 self.stop_code()))
1607 self.generate_subexpr_disposal_code(code)
1609 def start_code(self):
1610 if self.start:
1611 return self.start.result_code
1612 else:
1613 return "0"
1615 def stop_code(self):
1616 if self.stop:
1617 return self.stop.result_code
1618 else:
1619 return "PY_SSIZE_T_MAX"
1621 def calculate_result_code(self):
1622 # self.result_code is not used, but this method must exist
1623 return "<unused>"
1626 class SliceNode(ExprNode):
1627 # start:stop:step in subscript list
1629 # start ExprNode
1630 # stop ExprNode
1631 # step ExprNode
1633 def compile_time_value(self, denv):
1634 start = self.start.compile_time_value(denv)
1635 stop = self.stop.compile_time_value(denv)
1636 step = step.step.compile_time_value(denv)
1637 try:
1638 return slice(start, stop, step)
1639 except Exception, e:
1640 self.compile_time_value_error(e)
1642 subexprs = ['start', 'stop', 'step']
1644 def analyse_types(self, env):
1645 self.start.analyse_types(env)
1646 self.stop.analyse_types(env)
1647 self.step.analyse_types(env)
1648 self.start = self.start.coerce_to_pyobject(env)
1649 self.stop = self.stop.coerce_to_pyobject(env)
1650 self.step = self.step.coerce_to_pyobject(env)
1651 self.type = py_object_type
1652 self.gil_check(env)
1653 self.is_temp = 1
1655 gil_message = "Constructing Python slice object"
1657 def generate_result_code(self, code):
1658 code.putln(
1659 "%s = PySlice_New(%s, %s, %s); %s" % (
1660 self.result_code,
1661 self.start.py_result(),
1662 self.stop.py_result(),
1663 self.step.py_result(),
1664 code.error_goto_if_null(self.result_code, self.pos)))
1667 class CallNode(ExprNode):
1668 def gil_check(self, env):
1669 # Make sure we're not in a nogil environment
1670 if env.nogil:
1671 error(self.pos, "Calling gil-requiring function without gil")
1674 class SimpleCallNode(CallNode):
1675 # Function call without keyword, * or ** args.
1677 # function ExprNode
1678 # args [ExprNode]
1679 # arg_tuple ExprNode or None used internally
1680 # self ExprNode or None used internally
1681 # coerced_self ExprNode or None used internally
1682 # wrapper_call bool used internally
1684 subexprs = ['self', 'coerced_self', 'function', 'args', 'arg_tuple']
1686 self = None
1687 coerced_self = None
1688 arg_tuple = None
1689 wrapper_call = False
1691 def compile_time_value(self, denv):
1692 function = self.function.compile_time_value(denv)
1693 args = [arg.compile_time_value(denv) for arg in self.args]
1694 try:
1695 return function(*args)
1696 except Exception, e:
1697 self.compile_time_value_error(e)
1699 def analyse_types(self, env):
1700 function = self.function
1701 function.is_called = 1
1702 self.function.analyse_types(env)
1703 if function.is_attribute and function.is_py_attr and \
1704 function.attribute == "append" and len(self.args) == 1:
1705 # L.append(x) is almost always applied to a list
1706 self.py_func = self.function
1707 self.function = NameNode(pos=self.function.pos, name="__Pyx_PyObject_Append")
1708 self.function.analyse_types(env)
1709 self.self = self.py_func.obj
1710 function.obj = CloneNode(self.self)
1711 env.use_utility_code(append_utility_code)
1712 if function.is_attribute and function.entry and function.entry.is_cmethod:
1713 # Take ownership of the object from which the attribute
1714 # was obtained, because we need to pass it as 'self'.
1715 self.self = function.obj
1716 function.obj = CloneNode(self.self)
1717 func_type = self.function_type()
1718 if func_type.is_pyobject:
1719 self.arg_tuple = TupleNode(self.pos, args = self.args)
1720 self.arg_tuple.analyse_types(env)
1721 self.args = None
1722 self.type = py_object_type
1723 self.gil_check(env)
1724 self.is_temp = 1
1725 else:
1726 for arg in self.args:
1727 arg.analyse_types(env)
1728 if self.self and func_type.args:
1729 # Coerce 'self' to the type expected by the method.
1730 expected_type = func_type.args[0].type
1731 self.coerced_self = CloneNode(self.self).coerce_to(
1732 expected_type, env)
1733 # Insert coerced 'self' argument into argument list.
1734 self.args.insert(0, self.coerced_self)
1735 self.analyse_c_function_call(env)
1737 def function_type(self):
1738 # Return the type of the function being called, coercing a function
1739 # pointer to a function if necessary.
1740 func_type = self.function.type
1741 if func_type.is_ptr:
1742 func_type = func_type.base_type
1743 return func_type
1745 def analyse_c_function_call(self, env):
1746 func_type = self.function_type()
1747 # Check function type
1748 if not func_type.is_cfunction:
1749 if not func_type.is_error:
1750 error(self.pos, "Calling non-function type '%s'" %
1751 func_type)
1752 self.type = PyrexTypes.error_type
1753 self.result_code = "<error>"
1754 return
1755 # Check no. of args
1756 max_nargs = len(func_type.args)
1757 expected_nargs = max_nargs - func_type.optional_arg_count
1758 actual_nargs = len(self.args)
1759 if actual_nargs < expected_nargs \
1760 or (not func_type.has_varargs and actual_nargs > max_nargs):
1761 expected_str = str(expected_nargs)
1762 if func_type.has_varargs:
1763 expected_str = "at least " + expected_str
1764 elif func_type.optional_arg_count:
1765 if actual_nargs < max_nargs:
1766 expected_str = "at least " + expected_str
1767 else:
1768 expected_str = "at most " + str(max_nargs)
1769 error(self.pos,
1770 "Call with wrong number of arguments (expected %s, got %s)"
1771 % (expected_str, actual_nargs))
1772 self.args = None
1773 self.type = PyrexTypes.error_type
1774 self.result_code = "<error>"
1775 return
1776 # Coerce arguments
1777 for i in range(min(max_nargs, actual_nargs)):
1778 formal_type = func_type.args[i].type
1779 self.args[i] = self.args[i].coerce_to(formal_type, env)
1780 for i in range(max_nargs, actual_nargs):
1781 if self.args[i].type.is_pyobject:
1782 error(self.args[i].pos,
1783 "Python object cannot be passed as a varargs parameter")
1784 # Calc result type and code fragment
1785 self.type = func_type.return_type
1786 if self.type.is_pyobject \
1787 or func_type.exception_value is not None \
1788 or func_type.exception_check:
1789 self.is_temp = 1
1790 if self.type.is_pyobject:
1791 self.result_ctype = py_object_type
1792 # C++ exception handler
1793 if func_type.exception_check == '+':
1794 if func_type.exception_value is None:
1795 env.use_utility_code(cpp_exception_utility_code)
1796 # Check gil
1797 if not func_type.nogil:
1798 self.gil_check(env)
1800 def calculate_result_code(self):
1801 return self.c_call_code()
1803 def c_call_code(self):
1804 func_type = self.function_type()
1805 if self.args is None or not func_type.is_cfunction:
1806 return "<error>"
1807 formal_args = func_type.args
1808 arg_list_code = []
1809 args = zip(formal_args, self.args)
1810 max_nargs = len(func_type.args)
1811 expected_nargs = max_nargs - func_type.optional_arg_count
1812 actual_nargs = len(self.args)
1813 for formal_arg, actual_arg in args[:expected_nargs]:
1814 arg_code = actual_arg.result_as(formal_arg.type)
1815 arg_list_code.append(arg_code)
1817 if func_type.optional_arg_count:
1818 if expected_nargs == actual_nargs:
1819 optional_args = 'NULL'
1820 else:
1821 optional_arg_code = [str(actual_nargs - expected_nargs)]
1822 for formal_arg, actual_arg in args[expected_nargs:actual_nargs]:
1823 arg_code = actual_arg.result_as(formal_arg.type)
1824 optional_arg_code.append(arg_code)
1825 # for formal_arg in formal_args[actual_nargs:max_nargs]:
1826 # optional_arg_code.append(formal_arg.type.cast_code('0'))
1827 optional_arg_struct = '{%s}' % ','.join(optional_arg_code)
1828 optional_args = PyrexTypes.c_void_ptr_type.cast_code(
1829 '&' + func_type.op_arg_struct.base_type.cast_code(optional_arg_struct))
1830 arg_list_code.append(optional_args)
1832 for actual_arg in self.args[len(formal_args):]:
1833 arg_list_code.append(actual_arg.result_code)
1834 result = "%s(%s)" % (self.function.result_code,
1835 join(arg_list_code, ", "))
1836 if self.wrapper_call or \
1837 self.function.entry.is_unbound_cmethod and self.function.entry.type.is_overridable:
1838 result = "(%s = 1, %s)" % (Naming.skip_dispatch_cname, result)
1839 return result
1841 def generate_result_code(self, code):
1842 func_type = self.function_type()
1843 if func_type.is_pyobject:
1844 arg_code = self.arg_tuple.py_result()
1845 code.putln(
1846 "%s = PyObject_Call(%s, %s, NULL); %s" % (
1847 self.result_code,
1848 self.function.py_result(),
1849 arg_code,
1850 code.error_goto_if_null(self.result_code, self.pos)))
1851 elif func_type.is_cfunction:
1852 exc_checks = []
1853 if self.type.is_pyobject:
1854 exc_checks.append("!%s" % self.result_code)
1855 else:
1856 exc_val = func_type.exception_value
1857 exc_check = func_type.exception_check
1858 if exc_val is not None:
1859 exc_checks.append("%s == %s" % (self.result_code, exc_val))
1860 if exc_check:
1861 exc_checks.append("PyErr_Occurred()")
1862 if self.is_temp or exc_checks:
1863 rhs = self.c_call_code()
1864 if self.result_code:
1865 lhs = "%s = " % self.result_code
1866 if self.is_temp and self.type.is_pyobject:
1867 #return_type = self.type # func_type.return_type
1868 #print "SimpleCallNode.generate_result_code: casting", rhs, \
1869 # "from", return_type, "to pyobject" ###
1870 rhs = typecast(py_object_type, self.type, rhs)
1871 else:
1872 lhs = ""
1873 if func_type.exception_check == '+':
1874 if func_type.exception_value is None:
1875 raise_py_exception = "__Pyx_CppExn2PyErr()"
1876 elif func_type.exception_value.type.is_pyobject:
1877 raise_py_exception = 'PyErr_SetString(%s, "")' % func_type.exception_value.entry.cname
1878 else:
1879 raise_py_exception = '%s(); if (!PyErr_Occurred()) PyErr_SetString(PyExc_RuntimeError , "Error converting c++ exception.")' % func_type.exception_value.entry.cname
1880 code.putln(
1881 "try {%s%s;} catch(...) {%s; %s}" % (
1882 lhs,
1883 rhs,
1884 raise_py_exception,
1885 code.error_goto(self.pos)))
1886 return
1887 code.putln(
1888 "%s%s; %s" % (
1889 lhs,
1890 rhs,
1891 code.error_goto_if(" && ".join(exc_checks), self.pos)))
1893 class GeneralCallNode(CallNode):
1894 # General Python function call, including keyword,
1895 # * and ** arguments.
1897 # function ExprNode
1898 # positional_args ExprNode Tuple of positional arguments
1899 # keyword_args ExprNode or None Dict of keyword arguments
1900 # starstar_arg ExprNode or None Dict of extra keyword args
1902 subexprs = ['function', 'positional_args', 'keyword_args', 'starstar_arg']
1904 def compile_time_value(self, denv):
1905 function = self.function.compile_time_value(denv)
1906 positional_args = self.positional_args.compile_time_value(denv)
1907 keyword_args = self.keyword_args.compile_time_value(denv)
1908 starstar_arg = self.starstar_arg.compile_time_value(denv)
1909 try:
1910 keyword_args.update(starstar_arg)
1911 return function(*positional_args, **keyword_args)
1912 except Exception, e:
1913 self.compile_time_value_error(e)
1915 def analyse_types(self, env):
1916 self.function.analyse_types(env)
1917 self.positional_args.analyse_types(env)
1918 if self.keyword_args:
1919 self.keyword_args.analyse_types(env)
1920 if self.starstar_arg:
1921 self.starstar_arg.analyse_types(env)
1922 self.function = self.function.coerce_to_pyobject(env)
1923 self.positional_args = \
1924 self.positional_args.coerce_to_pyobject(env)
1925 if self.starstar_arg:
1926 self.starstar_arg = \
1927 self.starstar_arg.coerce_to_pyobject(env)
1928 self.type = py_object_type
1929 self.gil_check(env)
1930 self.is_temp = 1
1932 def generate_result_code(self, code):
1933 if self.keyword_args and self.starstar_arg:
1934 code.put_error_if_neg(self.pos,
1935 "PyDict_Update(%s, %s)" % (
1936 self.keyword_args.py_result(),
1937 self.starstar_arg.py_result()))
1938 keyword_code = self.keyword_args.py_result()
1939 elif self.keyword_args:
1940 keyword_code = self.keyword_args.py_result()
1941 elif self.starstar_arg:
1942 keyword_code = self.starstar_arg.py_result()
1943 else:
1944 keyword_code = None
1945 if not keyword_code:
1946 call_code = "PyObject_Call(%s, %s, NULL)" % (
1947 self.function.py_result(),
1948 self.positional_args.py_result())
1949 else:
1950 call_code = "PyEval_CallObjectWithKeywords(%s, %s, %s)" % (
1951 self.function.py_result(),
1952 self.positional_args.py_result(),
1953 keyword_code)
1954 code.putln(
1955 "%s = %s; %s" % (
1956 self.result_code,
1957 call_code,
1958 code.error_goto_if_null(self.result_code, self.pos)))
1961 class AsTupleNode(ExprNode):
1962 # Convert argument to tuple. Used for normalising
1963 # the * argument of a function call.
1965 # arg ExprNode
1967 subexprs = ['arg']
1969 def compile_time_value(self, denv):
1970 arg = self.arg.compile_time_value(denv)
1971 try:
1972 return tuple(arg)
1973 except Exception, e:
1974 self.compile_time_value_error(e)
1976 def analyse_types(self, env):
1977 self.arg.analyse_types(env)
1978 self.arg = self.arg.coerce_to_pyobject(env)
1979 self.type = py_object_type
1980 self.gil_check(env)
1981 self.is_temp = 1
1983 gil_message = "Constructing Python tuple"
1985 def generate_result_code(self, code):
1986 code.putln(
1987 "%s = PySequence_Tuple(%s); %s" % (
1988 self.result_code,
1989 self.arg.py_result(),
1990 code.error_goto_if_null(self.result_code, self.pos)))
1993 class AttributeNode(ExprNode):
1994 # obj.attribute
1996 # obj ExprNode
1997 # attribute string
1999 # Used internally:
2001 # is_py_attr boolean Is a Python getattr operation
2002 # member string C name of struct member
2003 # is_called boolean Function call is being done on result
2004 # entry Entry Symbol table entry of attribute
2005 # interned_attr_cname string C name of interned attribute name
2007 is_attribute = 1
2008 subexprs = ['obj']
2010 type = PyrexTypes.error_type
2011 result = "<error>"
2012 entry = None
2013 is_called = 0
2015 def coerce_to(self, dst_type, env):
2016 # If coercing to a generic pyobject and this is a cpdef function
2017 # we can create the corresponding attribute
2018 if dst_type is py_object_type:
2019 entry = self.entry
2020 if entry and entry.is_cfunction and entry.as_variable:
2021 # must be a cpdef function
2022 self.is_temp = 1
2023 self.entry = entry.as_variable
2024 self.analyse_as_python_attribute(env)
2025 return self
2026 return ExprNode.coerce_to(self, dst_type, env)
2028 def compile_time_value(self, denv):
2029 attr = self.attribute
2030 if attr.beginswith("__") and attr.endswith("__"):
2031 self.error("Invalid attribute name '%s' in compile-time expression"
2032 % attr)
2033 return None
2034 obj = self.arg.compile_time_value(denv)
2035 try:
2036 return getattr(obj, attr)
2037 except Exception, e:
2038 self.compile_time_value_error(e)
2040 def analyse_target_declaration(self, env):
2041 pass
2043 def analyse_target_types(self, env):
2044 self.analyse_types(env, target = 1)
2046 def analyse_types(self, env, target = 0):
2047 if self.analyse_as_cimported_attribute(env, target):
2048 return
2049 if not target and self.analyse_as_unbound_cmethod(env):
2050 return
2051 self.analyse_as_ordinary_attribute(env, target)
2053 def analyse_as_cimported_attribute(self, env, target):
2054 # Try to interpret this as a reference to an imported
2055 # C const, type, var or function. If successful, mutates
2056 # this node into a NameNode and returns 1, otherwise
2057 # returns 0.
2058 module_scope = self.obj.analyse_as_module(env)
2059 if module_scope:
2060 entry = module_scope.lookup_here(self.attribute)
2061 if entry and (
2062 entry.is_cglobal or entry.is_cfunction
2063 or entry.is_type or entry.is_const):
2064 self.mutate_into_name_node(env, entry, target)
2065 return 1
2066 return 0
2068 def analyse_as_unbound_cmethod(self, env):
2069 # Try to interpret this as a reference to an unbound
2070 # C method of an extension type. If successful, mutates
2071 # this node into a NameNode and returns 1, otherwise
2072 # returns 0.
2073 type = self.obj.analyse_as_extension_type(env)
2074 if type:
2075 entry = type.scope.lookup_here(self.attribute)
2076 if entry and entry.is_cmethod:
2077 # Create a temporary entry describing the C method
2078 # as an ordinary function.
2079 ubcm_entry = Symtab.Entry(entry.name,
2080 "%s->%s" % (type.vtabptr_cname, entry.cname),
2081 entry.type)
2082 ubcm_entry.is_cfunction = 1
2083 ubcm_entry.func_cname = entry.func_cname
2084 ubcm_entry.is_unbound_cmethod = 1
2085 self.mutate_into_name_node(env, ubcm_entry, None)
2086 return 1
2087 return 0
2089 def analyse_as_extension_type(self, env):
2090 # Try to interpret this as a reference to an extension type
2091 # in a cimported module. Returns the extension type, or None.
2092 module_scope = self.obj.analyse_as_module(env)
2093 if module_scope:
2094 entry = module_scope.lookup_here(self.attribute)
2095 if entry and entry.is_type and entry.type.is_extension_type:
2096 return entry.type
2097 return None
2099 def analyse_as_module(self, env):
2100 # Try to interpret this as a reference to a cimported module
2101 # in another cimported module. Returns the module scope, or None.
2102 module_scope = self.obj.analyse_as_module(env)
2103 if module_scope:
2104 entry = module_scope.lookup_here(self.attribute)
2105 if entry and entry.as_module:
2106 return entry.as_module
2107 return None
2109 def mutate_into_name_node(self, env, entry, target):
2110 # Mutate this node into a NameNode and complete the
2111 # analyse_types phase.
2112 self.__class__ = NameNode
2113 self.name = self.attribute
2114 self.entry = entry
2115 del self.obj
2116 del self.attribute
2117 if target:
2118 NameNode.analyse_target_types(self, env)
2119 else:
2120 NameNode.analyse_rvalue_entry(self, env)
2122 def analyse_as_ordinary_attribute(self, env, target):
2123 self.obj.analyse_types(env)
2124 self.analyse_attribute(env)
2125 if self.entry and self.entry.is_cmethod and not self.is_called:
2126 # error(self.pos, "C method can only be called")
2127 pass
2128 ## Reference to C array turns into pointer to first element.
2129 #while self.type.is_array:
2130 # self.type = self.type.element_ptr_type()
2131 if self.is_py_attr:
2132 if not target:
2133 self.is_temp = 1
2134 self.result_ctype = py_object_type
2136 def analyse_attribute(self, env):
2137 # Look up attribute and set self.type and self.member.
2138 self.is_py_attr = 0
2139 self.member = self.attribute
2140 if self.obj.type.is_string:
2141 self.obj = self.obj.coerce_to_pyobject(env)
2142 obj_type = self.obj.type
2143 if obj_type.is_ptr or obj_type.is_array:
2144 obj_type = obj_type.base_type
2145 self.op = "->"
2146 elif obj_type.is_extension_type:
2147 self.op = "->"
2148 else:
2149 self.op = "."
2150 if obj_type.has_attributes:
2151 entry = None
2152 if obj_type.attributes_known():
2153 entry = obj_type.scope.lookup_here(self.attribute)
2154 if entry and entry.is_member:
2155 entry = None
2156 else:
2157 error(self.pos,
2158 "Cannot select attribute of incomplete type '%s'"
2159 % obj_type)
2160 self.type = PyrexTypes.error_type
2161 return
2162 self.entry = entry
2163 if entry:
2164 if obj_type.is_extension_type and entry.name == "__weakref__":
2165 error(self.pos, "Illegal use of special attribute __weakref__")
2166 # methods need the normal attribute lookup
2167 # because they do not have struct entries
2168 if entry.is_variable or entry.is_cmethod:
2169 self.type = entry.type
2170 self.member = entry.cname
2171 return
2172 else:
2173 # If it's not a variable or C method, it must be a Python
2174 # method of an extension type, so we treat it like a Python
2175 # attribute.
2176 pass
2177 # If we get here, the base object is not a struct/union/extension
2178 # type, or it is an extension type and the attribute is either not
2179 # declared or is declared as a Python method. Treat it as a Python
2180 # attribute reference.
2181 self.analyse_as_python_attribute(env)
2183 def analyse_as_python_attribute(self, env):
2184 obj_type = self.obj.type
2185 self.member = self.attribute
2186 if obj_type.is_pyobject:
2187 self.type = py_object_type
2188 self.is_py_attr = 1
2189 self.interned_attr_cname = env.intern_identifier(self.attribute)
2190 self.gil_check(env)
2191 else:
2192 if not obj_type.is_error:
2193 error(self.pos,
2194 "Object of type '%s' has no attribute '%s'" %
2195 (obj_type, self.attribute))
2197 gil_message = "Accessing Python attribute"
2199 def is_simple(self):
2200 if self.obj:
2201 return self.result_in_temp() or self.obj.is_simple()
2202 else:
2203 return NameNode.is_simple(self)
2205 def is_lvalue(self):
2206 if self.obj:
2207 return 1
2208 else:
2209 return NameNode.is_lvalue(self)
2211 def is_ephemeral(self):
2212 if self.obj:
2213 return self.obj.is_ephemeral()
2214 else:
2215 return NameNode.is_ephemeral(self)
2217 def calculate_result_code(self):
2218 #print "AttributeNode.calculate_result_code:", self.member ###
2219 #print "...obj node =", self.obj, "code", self.obj.result_code ###
2220 #print "...obj type", self.obj.type, "ctype", self.obj.ctype() ###
2221 obj = self.obj
2222 obj_code = obj.result_as(obj.type)
2223 #print "...obj_code =", obj_code ###
2224 if self.entry and self.entry.is_cmethod:
2225 if obj.type.is_extension_type:
2226 return "((struct %s *)%s%s%s)->%s" % (
2227 obj.type.vtabstruct_cname, obj_code, self.op,
2228 obj.type.vtabslot_cname, self.member)
2229 else:
2230 return self.member
2231 else:
2232 return "%s%s%s" % (obj_code, self.op, self.member)
2234 def generate_result_code(self, code):
2235 if self.is_py_attr:
2236 code.putln(
2237 '%s = PyObject_GetAttr(%s, %s); %s' % (
2238 self.result_code,
2239 self.obj.py_result(),
2240 self.interned_attr_cname,
2241 code.error_goto_if_null(self.result_code, self.pos)))
2243 def generate_assignment_code(self, rhs, code):
2244 self.obj.generate_evaluation_code(code)
2245 if self.is_py_attr:
2246 code.put_error_if_neg(self.pos,
2247 'PyObject_SetAttr(%s, %s, %s)' % (
2248 self.obj.py_result(),
2249 self.interned_attr_cname,
2250 rhs.py_result()))
2251 rhs.generate_disposal_code(code)
2252 else:
2253 select_code = self.result_code
2254 if self.type.is_pyobject:
2255 rhs.make_owned_reference(code)
2256 code.put_decref(select_code, self.ctype())
2257 code.putln(
2258 "%s = %s;" % (
2259 select_code,
2260 rhs.result_as(self.ctype())))
2261 #rhs.result_code))
2262 rhs.generate_post_assignment_code(code)
2263 self.obj.generate_disposal_code(code)
2265 def generate_deletion_code(self, code):
2266 self.obj.generate_evaluation_code(code)
2267 if self.is_py_attr:
2268 code.put_error_if_neg(self.pos,
2269 'PyObject_DelAttr(%s, %s)' % (
2270 self.obj.py_result(),
2271 self.interned_attr_cname))
2272 else:
2273 error(self.pos, "Cannot delete C attribute of extension type")
2274 self.obj.generate_disposal_code(code)
2276 def annotate(self, code):
2277 if self.is_py_attr:
2278 code.annotate(self.pos, AnnotationItem('py_attr', 'python attribute', size=len(self.attribute)))
2279 else:
2280 code.annotate(self.pos, AnnotationItem('c_attr', 'c attribute', size=len(self.attribute)))
2282 #-------------------------------------------------------------------
2284 # Constructor nodes
2286 #-------------------------------------------------------------------
2288 class SequenceNode(ExprNode):
2289 # Base class for list and tuple constructor nodes.
2290 # Contains common code for performing sequence unpacking.
2292 # args [ExprNode]
2293 # iterator ExprNode
2294 # unpacked_items [ExprNode] or None
2295 # coerced_unpacked_items [ExprNode] or None
2297 subexprs = ['args']
2299 is_sequence_constructor = 1
2300 unpacked_items = None
2302 def compile_time_value_list(self, denv):
2303 return [arg.compile_time_value(denv) for arg in self.args]
2305 def analyse_target_declaration(self, env):
2306 for arg in self.args:
2307 arg.analyse_target_declaration(env)
2309 def analyse_types(self, env, skip_children=False):
2310 for i in range(len(self.args)):
2311 arg = self.args[i]
2312 if not skip_children: arg.analyse_types(env)
2313 self.args[i] = arg.coerce_to_pyobject(env)
2314 self.type = py_object_type
2315 self.gil_check(env)
2316 self.is_temp = 1
2318 def analyse_target_types(self, env):
2319 self.iterator = PyTempNode(self.pos, env)
2320 self.unpacked_items = []
2321 self.coerced_unpacked_items = []
2322 for arg in self.args:
2323 arg.analyse_target_types(env)
2324 unpacked_item = PyTempNode(self.pos, env)
2325 coerced_unpacked_item = unpacked_item.coerce_to(arg.type, env)
2326 self.unpacked_items.append(unpacked_item)
2327 self.coerced_unpacked_items.append(coerced_unpacked_item)
2328 self.type = py_object_type
2329 env.use_utility_code(unpacking_utility_code)
2331 def allocate_target_temps(self, env, rhs):
2332 self.iterator.allocate_temps(env)
2333 for arg, node in zip(self.args, self.coerced_unpacked_items):
2334 node.allocate_temps(env)
2335 arg.allocate_target_temps(env, node)
2336 #arg.release_target_temp(env)
2337 #node.release_temp(env)
2338 if rhs:
2339 rhs.release_temp(env)
2340 self.iterator.release_temp(env)
2342 # def release_target_temp(self, env):
2343 # #for arg in self.args:
2344 # # arg.release_target_temp(env)
2345 # #for node in self.coerced_unpacked_items:
2346 # # node.release_temp(env)
2347 # self.iterator.release_temp(env)
2349 def generate_result_code(self, code):
2350 self.generate_operation_code(code)
2352 def generate_assignment_code(self, rhs, code):
2353 code.putln(
2354 "if (PyTuple_CheckExact(%s) && PyTuple_GET_SIZE(%s) == %s) {" % (
2355 rhs.py_result(),
2356 rhs.py_result(),
2357 len(self.args)))
2358 code.putln("PyObject* tuple = %s;" % rhs.py_result())
2359 for i in range(len(self.args)):
2360 item = self.unpacked_items[i]
2361 code.putln(
2362 "%s = PyTuple_GET_ITEM(tuple, %s);" % (
2363 item.result_code,
2364 i))
2365 code.put_incref(item.result_code, item.ctype())
2366 value_node = self.coerced_unpacked_items[i]
2367 value_node.generate_evaluation_code(code)
2368 self.args[i].generate_assignment_code(value_node, code)
2370 rhs.generate_disposal_code(code)
2371 code.putln("}")
2372 code.putln("else {")
2374 code.putln(
2375 "%s = PyObject_GetIter(%s); %s" % (
2376 self.iterator.result_code,
2377 rhs.py_result(),
2378 code.error_goto_if_null(self.iterator.result_code, self.pos)))
2379 rhs.generate_disposal_code(code)
2380 for i in range(len(self.args)):
2381 item = self.unpacked_items[i]
2382 unpack_code = "__Pyx_UnpackItem(%s, %d)" % (
2383 self.iterator.py_result(), i)
2384 code.putln(
2385 "%s = %s; %s" % (
2386 item.result_code,
2387 typecast(item.ctype(), py_object_type, unpack_code),
2388 code.error_goto_if_null(item.result_code, self.pos)))
2389 value_node = self.coerced_unpacked_items[i]
2390 value_node.generate_evaluation_code(code)
2391 self.args[i].generate_assignment_code(value_node, code)
2392 code.put_error_if_neg(self.pos,
2393 "__Pyx_EndUnpack(%s)" % (
2394 self.iterator.py_result()))
2395 if debug_disposal_code:
2396 print("UnpackNode.generate_assignment_code:")
2397 print("...generating disposal code for %s" % self.iterator)
2398 self.iterator.generate_disposal_code(code)
2400 code.putln("}")
2402 def annotate(self, code):
2403 for arg in self.args:
2404 arg.annotate(code)
2405 if self.unpacked_items:
2406 for arg in self.unpacked_items:
2407 arg.annotate(code)
2408 for arg in self.coerced_unpacked_items:
2409 arg.annotate(code)
2412 class TupleNode(SequenceNode):
2413 # Tuple constructor.
2415 gil_message = "Constructing Python tuple"
2417 def analyse_types(self, env, skip_children=False):
2418 if len(self.args) == 0:
2419 self.is_temp = 0
2420 self.is_literal = 1
2421 else:
2422 SequenceNode.analyse_types(self, env, skip_children)
2423 self.type = tuple_type
2425 def calculate_result_code(self):
2426 if len(self.args) > 0:
2427 error(self.pos, "Positive length tuples must be constructed.")
2428 else:
2429 return Naming.empty_tuple
2431 def compile_time_value(self, denv):
2432 values = self.compile_time_value_list(denv)
2433 try:
2434 return tuple(values)
2435 except Exception, e:
2436 self.compile_time_value_error(e)
2438 def generate_operation_code(self, code):
2439 if len(self.args) == 0:
2440 # result_code is Naming.empty_tuple
2441 return
2442 code.putln(
2443 "%s = PyTuple_New(%s); %s" % (
2444 self.result_code,
2445 len(self.args),
2446 code.error_goto_if_null(self.result_code, self.pos)))
2447 for i in range(len(self.args)):
2448 arg = self.args[i]
2449 if not arg.result_in_temp():
2450 code.put_incref(arg.result_code, arg.ctype())
2451 code.putln(
2452 "PyTuple_SET_ITEM(%s, %s, %s);" % (
2453 self.result_code,
2454 i,
2455 arg.py_result()))
2457 def generate_subexpr_disposal_code(self, code):
2458 # We call generate_post_assignment_code here instead
2459 # of generate_disposal_code, because values were stored
2460 # in the tuple using a reference-stealing operation.
2461 for arg in self.args:
2462 arg.generate_post_assignment_code(code)
2465 class ListNode(SequenceNode):
2466 # List constructor.
2468 gil_message = "Constructing Python list"
2470 def analyse_types(self, env):
2471 SequenceNode.analyse_types(self, env)
2472 self.type = list_type
2474 def compile_time_value(self, denv):
2475 return self.compile_time_value_list(denv)
2477 def generate_operation_code(self, code):
2478 code.putln("%s = PyList_New(%s); %s" %
2479 (self.result_code,
2480 len(self.args),
2481 code.error_goto_if_null(self.result_code, self.pos)))
2482 for i in range(len(self.args)):
2483 arg = self.args[i]
2484 #if not arg.is_temp:
2485 if not arg.result_in_temp():
2486 code.put_incref(arg.result_code, arg.ctype())
2487 code.putln("PyList_SET_ITEM(%s, %s, %s);" %
2488 (self.result_code,
2489 i,
2490 arg.py_result()))
2492 def generate_subexpr_disposal_code(self, code):
2493 # We call generate_post_assignment_code here instead
2494 # of generate_disposal_code, because values were stored
2495 # in the list using a reference-stealing operation.
2496 for arg in self.args:
2497 arg.generate_post_assignment_code(code)
2500 class ListComprehensionNode(SequenceNode):
2502 subexprs = []
2503 is_sequence_constructor = 0 # not unpackable
2505 def analyse_types(self, env):
2506 self.type = list_type
2507 self.is_temp = 1
2508 self.append.target = self # this is a CloneNode used in the PyList_Append in the inner loop
2510 def allocate_temps(self, env, result = None):
2511 if debug_temp_alloc:
2512 print("%s Allocating temps" % self)
2513 self.allocate_temp(env, result)
2514 self.loop.analyse_declarations(env)
2515 self.loop.analyse_expressions(env)
2517 def generate_operation_code(self, code):
2518 code.putln("%s = PyList_New(%s); %s" %
2519 (self.result_code,
2520 0,
2521 code.error_goto_if_null(self.result_code, self.pos)))
2522 self.loop.generate_execution_code(code)
2524 def annotate(self, code):
2525 self.loop.annotate(code)
2528 class ListComprehensionAppendNode(ExprNode):
2530 subexprs = ['expr']
2532 def analyse_types(self, env):
2533 self.expr.analyse_types(env)
2534 if self.expr.type != py_object_type:
2535 self.expr = self.expr.coerce_to_pyobject(env)
2536 self.type = PyrexTypes.c_int_type
2537 self.is_temp = 1
2539 def generate_result_code(self, code):
2540 code.putln("%s = PyList_Append(%s, %s); %s" %
2541 (self.result_code,
2542 self.target.result_code,
2543 self.expr.result_code,
2544 code.error_goto_if(self.result_code, self.pos)))
2547 class DictNode(ExprNode):
2548 # Dictionary constructor.
2550 # key_value_pairs [DictItemNode]
2552 subexprs = ['key_value_pairs']
2554 def compile_time_value(self, denv):
2555 pairs = [(item.key.compile_time_value(denv), item.value.compile_time_value(denv))
2556 for item in self.key_value_pairs]
2557 try:
2558 return dict(pairs)
2559 except Exception, e:
2560 self.compile_time_value_error(e)
2562 def analyse_types(self, env):
2563 for item in self.key_value_pairs:
2564 item.analyse_types(env)
2565 self.type = dict_type
2566 self.gil_check(env)
2567 self.is_temp = 1
2569 gil_message = "Constructing Python dict"
2571 def allocate_temps(self, env, result = None):
2572 # Custom method used here because key-value
2573 # pairs are evaluated and used one at a time.
2574 self.allocate_temp(env, result)
2575 for item in self.key_value_pairs:
2576 item.key.allocate_temps(env)
2577 item.value.allocate_temps(env)
2578 item.key.release_temp(env)
2579 item.value.release_temp(env)
2581 def generate_evaluation_code(self, code):
2582 # Custom method used here because key-value
2583 # pairs are evaluated and used one at a time.
2584 code.putln(
2585 "%s = PyDict_New(); %s" % (
2586 self.result_code,
2587 code.error_goto_if_null(self.result_code, self.pos)))
2588 for item in self.key_value_pairs:
2589 item.generate_evaluation_code(code)
2590 code.put_error_if_neg(self.pos,
2591 "PyDict_SetItem(%s, %s, %s)" % (
2592 self.result_code,
2593 item.key.py_result(),
2594 item.value.py_result()))
2595 item.generate_disposal_code(code)
2597 def annotate(self, code):
2598 for item in self.key_value_pairs:
2599 item.annotate(code)
2601 class DictItemNode(ExprNode):
2602 # Represents a single item in a DictNode
2604 # key ExprNode
2605 # value ExprNode
2606 subexprs = ['key', 'value']
2608 def analyse_types(self, env):
2609 self.key.analyse_types(env)
2610 self.value.analyse_types(env)
2611 self.key = self.key.coerce_to_pyobject(env)
2612 self.value = self.value.coerce_to_pyobject(env)
2614 def generate_evaluation_code(self, code):
2615 self.key.generate_evaluation_code(code)
2616 self.value.generate_evaluation_code(code)
2618 def generate_disposal_code(self, code):
2619 self.key.generate_disposal_code(code)
2620 self.value.generate_disposal_code(code)
2623 class ClassNode(ExprNode):
2624 # Helper class used in the implementation of Python
2625 # class definitions. Constructs a class object given
2626 # a name, tuple of bases and class dictionary.
2628 # name EncodedString Name of the class
2629 # cname string Class name as a Python string
2630 # bases ExprNode Base class tuple
2631 # dict ExprNode Class dict (not owned by this node)
2632 # doc ExprNode or None Doc string
2633 # module_name string Name of defining module
2635 subexprs = ['bases', 'doc']
2637 def analyse_types(self, env):
2638 self.cname = env.intern_identifier(self.name)
2639 self.bases.analyse_types(env)
2640 if self.doc:
2641 self.doc.analyse_types(env)
2642 self.doc = self.doc.coerce_to_pyobject(env)
2643 self.module_name = env.global_scope().qualified_name
2644 self.type = py_object_type
2645 self.gil_check(env)
2646 self.is_temp = 1
2647 env.use_utility_code(create_class_utility_code);
2649 gil_message = "Constructing Python class"
2651 def generate_result_code(self, code):
2652 if self.doc:
2653 code.put_error_if_neg(self.pos,
2654 'PyDict_SetItemString(%s, "__doc__", %s)' % (
2655 self.dict.py_result(),
2656 self.doc.py_result()))
2657 code.putln(
2658 '%s = __Pyx_CreateClass(%s, %s, %s, "%s"); %s' % (
2659 self.result_code,
2660 self.bases.py_result(),
2661 self.dict.py_result(),
2662 self.cname,
2663 self.module_name,
2664 code.error_goto_if_null(self.result_code, self.pos)))
2667 class UnboundMethodNode(ExprNode):
2668 # Helper class used in the implementation of Python
2669 # class definitions. Constructs an unbound method
2670 # object from a class and a function.
2672 # class_cname string C var holding the class object
2673 # function ExprNode Function object
2675 subexprs = ['function']
2677 def analyse_types(self, env):
2678 self.function.analyse_types(env)
2679 self.type = py_object_type
2680 self.gil_check(env)
2681 self.is_temp = 1
2683 gil_message = "Constructing an unbound method"
2685 def generate_result_code(self, code):
2686 code.putln(
2687 "%s = PyMethod_New(%s, 0, %s); %s" % (
2688 self.result_code,
2689 self.function.py_result(),
2690 self.class_cname,
2691 code.error_goto_if_null(self.result_code, self.pos)))
2694 class PyCFunctionNode(AtomicExprNode):
2695 # Helper class used in the implementation of Python
2696 # class definitions. Constructs a PyCFunction object
2697 # from a PyMethodDef struct.
2699 # pymethdef_cname string PyMethodDef structure
2701 def analyse_types(self, env):
2702 self.type = py_object_type
2703 self.gil_check(env)
2704 self.is_temp = 1
2706 gil_message = "Constructing Python function"
2708 def generate_result_code(self, code):
2709 code.putln(
2710 "%s = PyCFunction_New(&%s, 0); %s" % (
2711 self.result_code,
2712 self.pymethdef_cname,
2713 code.error_goto_if_null(self.result_code, self.pos)))
2715 #-------------------------------------------------------------------
2717 # Unary operator nodes
2719 #-------------------------------------------------------------------
2721 compile_time_unary_operators = {
2722 'not': operator.not_,
2723 '~': operator.inv,
2724 '-': operator.neg,
2725 '+': operator.pos,
2728 class UnopNode(ExprNode):
2729 # operator string
2730 # operand ExprNode
2732 # Processing during analyse_expressions phase:
2734 # analyse_c_operation
2735 # Called when the operand is not a pyobject.
2736 # - Check operand type and coerce if needed.
2737 # - Determine result type and result code fragment.
2738 # - Allocate temporary for result if needed.
2740 subexprs = ['operand']
2742 def compile_time_value(self, denv):
2743 func = compile_time_unary_operators.get(self.operator)
2744 if not func:
2745 error(self.pos,
2746 "Unary '%s' not supported in compile-time expression"
2747 % self.operator)
2748 operand = self.operand.compile_time_value(denv)
2749 try:
2750 return func(operand)
2751 except Exception, e:
2752 self.compile_time_value_error(e)
2754 def analyse_types(self, env):
2755 self.operand.analyse_types(env)
2756 if self.is_py_operation():
2757 self.coerce_operand_to_pyobject(env)
2758 self.type = py_object_type
2759 self.gil_check(env)
2760 self.is_temp = 1
2761 else:
2762 self.analyse_c_operation(env)
2764 def check_const(self):
2765 self.operand.check_const()
2767 def is_py_operation(self):
2768 return self.operand.type.is_pyobject
2770 def coerce_operand_to_pyobject(self, env):
2771 self.operand = self.operand.coerce_to_pyobject(env)
2773 def generate_result_code(self, code):
2774 if self.operand.type.is_pyobject:
2775 self.generate_py_operation_code(code)
2776 else:
2777 if self.is_temp:
2778 self.generate_c_operation_code(code)
2780 def generate_py_operation_code(self, code):
2781 function = self.py_operation_function()
2782 code.putln(
2783 "%s = %s(%s); %s" % (
2784 self.result_code,
2785 function,
2786 self.operand.py_result(),
2787 code.error_goto_if_null(self.result_code, self.pos)))
2789 def type_error(self):
2790 if not self.operand.type.is_error:
2791 error(self.pos, "Invalid operand type for '%s' (%s)" %
2792 (self.operator, self.operand.type))
2793 self.type = PyrexTypes.error_type
2796 class NotNode(ExprNode):
2797 # 'not' operator
2799 # operand ExprNode
2801 def compile_time_value(self, denv):
2802 operand = self.operand.compile_time_value(denv)
2803 try:
2804 return not operand
2805 except Exception, e:
2806 self.compile_time_value_error(e)
2808 subexprs = ['operand']
2810 def analyse_types(self, env):
2811 self.operand.analyse_types(env)
2812 self.operand = self.operand.coerce_to_boolean(env)
2813 self.type = PyrexTypes.c_bint_type
2815 def calculate_result_code(self):
2816 return "(!%s)" % self.operand.result_code
2818 def generate_result_code(self, code):
2819 pass
2822 class UnaryPlusNode(UnopNode):
2823 # unary '+' operator
2825 operator = '+'
2827 def analyse_c_operation(self, env):
2828 self.type = self.operand.type
2830 def py_operation_function(self):
2831 return "PyNumber_Positive"
2833 def calculate_result_code(self):
2834 return self.operand.result_code
2837 class UnaryMinusNode(UnopNode):
2838 # unary '-' operator
2840 operator = '-'
2842 def analyse_c_operation(self, env):
2843 if self.operand.type.is_numeric:
2844 self.type = self.operand.type
2845 else:
2846 self.type_error()
2848 def py_operation_function(self):
2849 return "PyNumber_Negative"
2851 def calculate_result_code(self):
2852 return "(-%s)" % self.operand.result_code
2855 class TildeNode(UnopNode):
2856 # unary '~' operator
2858 def analyse_c_operation(self, env):
2859 if self.operand.type.is_int:
2860 self.type = self.operand.type
2861 else:
2862 self.type_error()
2864 def py_operation_function(self):
2865 return "PyNumber_Invert"
2867 def calculate_result_code(self):
2868 return "(~%s)" % self.operand.result_code
2871 class AmpersandNode(ExprNode):
2872 # The C address-of operator.
2874 # operand ExprNode
2876 subexprs = ['operand']
2878 def analyse_types(self, env):
2879 self.operand.analyse_types(env)
2880 argtype = self.operand.type
2881 if not (argtype.is_cfunction or self.operand.is_lvalue()):
2882 self.error("Taking address of non-lvalue")
2883 return
2884 if argtype.is_pyobject:
2885 self.error("Cannot take address of Python variable")
2886 return
2887 self.type = PyrexTypes.c_ptr_type(argtype)
2889 def check_const(self):
2890 self.operand.check_const_addr()
2892 def error(self, mess):
2893 error(self.pos, mess)
2894 self.type = PyrexTypes.error_type
2895 self.result_code = "<error>"
2897 def calculate_result_code(self):
2898 return "(&%s)" % self.operand.result_code
2900 def generate_result_code(self, code):
2901 pass
2904 unop_node_classes = {
2905 "+": UnaryPlusNode,
2906 "-": UnaryMinusNode,
2907 "~": TildeNode,
2910 def unop_node(pos, operator, operand):
2911 # Construct unnop node of appropriate class for
2912 # given operator.
2913 if isinstance(operand, IntNode) and operator == '-':
2914 return IntNode(pos = operand.pos, value = str(-int(operand.value, 0)))
2915 elif isinstance(operand, UnopNode) and operand.operator == operator:
2916 warning(pos, "Python has no increment/decrement operator: %s%sx = %s(%sx) = x" % ((operator,)*4), 5)
2917 return unop_node_classes[operator](pos,
2918 operator = operator,
2919 operand = operand)
2922 class TypecastNode(ExprNode):
2923 # C type cast
2925 # operand ExprNode
2926 # base_type CBaseTypeNode
2927 # declarator CDeclaratorNode
2929 # If used from a transform, one can if wanted specify the attribute
2930 # "type" directly and leave base_type and declarator to None
2932 subexprs = ['operand']
2933 base_type = declarator = type = None
2935 def analyse_types(self, env):
2936 if self.type is None:
2937 base_type = self.base_type.analyse(env)
2938 _, self.type = self.declarator.analyse(base_type, env)
2939 if self.type.is_cfunction:
2940 error(self.pos,
2941 "Cannot cast to a function type")
2942 self.type = PyrexTypes.error_type
2943 self.operand.analyse_types(env)
2944 to_py = self.type.is_pyobject
2945 from_py = self.operand.type.is_pyobject
2946 if from_py and not to_py and self.operand.is_ephemeral() and not self.type.is_numeric:
2947 error(self.pos, "Casting temporary Python object to non-numeric non-Python type")
2948 if to_py and not from_py:
2949 if self.operand.type.to_py_function:
2950 self.result_ctype = py_object_type
2951 self.operand = self.operand.coerce_to_pyobject(env)
2952 else:
2953 warning(self.pos, "No conversion from %s to %s, python object pointer used." % (self.operand.type, self.type))
2954 self.operand = self.operand.coerce_to_simple(env)
2955 elif from_py and not to_py:
2956 if self.type.from_py_function:
2957 self.operand = self.operand.coerce_to(self.type, env)
2958 else:
2959 warning(self.pos, "No conversion from %s to %s, python object pointer used." % (self.type, self.operand.type))
2960 elif from_py and to_py:
2961 if self.typecheck and self.type.is_extension_type:
2962 self.operand = PyTypeTestNode(self.operand, self.type, env)
2964 def check_const(self):
2965 self.operand.check_const()
2967 def calculate_result_code(self):
2968 opnd = self.operand
2969 result_code = self.type.cast_code(opnd.result_code)
2970 return result_code
2972 def result_as(self, type):
2973 if self.type.is_pyobject and not self.is_temp:
2974 # Optimise away some unnecessary casting
2975 return self.operand.result_as(type)
2976 else:
2977 return ExprNode.result_as(self, type)
2979 def generate_result_code(self, code):
2980 if self.is_temp:
2981 code.putln(
2982 "%s = (PyObject *)%s;" % (
2983 self.result_code,
2984 self.operand.result_code))
2985 code.put_incref(self.result_code, self.ctype())
2988 class SizeofNode(ExprNode):
2989 # Abstract base class for sizeof(x) expression nodes.
2991 def check_const(self):
2992 pass
2994 def generate_result_code(self, code):
2995 pass
2998 class SizeofTypeNode(SizeofNode):
2999 # C sizeof function applied to a type
3001 # base_type CBaseTypeNode
3002 # declarator CDeclaratorNode
3004 subexprs = []
3006 def analyse_types(self, env):
3007 base_type = self.base_type.analyse(env)
3008 _, arg_type = self.declarator.analyse(base_type, env)
3009 self.arg_type = arg_type
3010 if arg_type.is_pyobject and not arg_type.is_extension_type:
3011 error(self.pos, "Cannot take sizeof Python object")
3012 elif arg_type.is_void:
3013 error(self.pos, "Cannot take sizeof void")
3014 elif not arg_type.is_complete():
3015 error(self.pos, "Cannot take sizeof incomplete type '%s'" % arg_type)
3016 self.type = PyrexTypes.c_int_type
3018 def calculate_result_code(self):
3019 if self.arg_type.is_extension_type:
3020 # the size of the pointer is boring
3021 # we want the size of the actual struct
3022 arg_code = self.arg_type.declaration_code("", deref=1)
3023 else:
3024 arg_code = self.arg_type.declaration_code("")
3025 return "(sizeof(%s))" % arg_code
3028 class SizeofVarNode(SizeofNode):
3029 # C sizeof function applied to a variable
3031 # operand ExprNode
3033 subexprs = ['operand']
3035 def analyse_types(self, env):
3036 self.operand.analyse_types(env)
3037 self.type = PyrexTypes.c_int_type
3039 def calculate_result_code(self):
3040 return "(sizeof(%s))" % self.operand.result_code
3042 def generate_result_code(self, code):
3043 pass
3046 #-------------------------------------------------------------------
3048 # Binary operator nodes
3050 #-------------------------------------------------------------------
3052 def _not_in(x, seq):
3053 return x not in seq
3055 compile_time_binary_operators = {
3056 '<': operator.lt,
3057 '<=': operator.le,
3058 '==': operator.eq,
3059 '!=': operator.ne,
3060 '>=': operator.ge,
3061 '>': operator.gt,
3062 'is': operator.is_,
3063 'is_not': operator.is_not,
3064 '+': operator.add,
3065 '&': operator.and_,
3066 '/': operator.div,
3067 '//': operator.floordiv,
3068 '<<': operator.lshift,
3069 '%': operator.mod,
3070 '*': operator.mul,
3071 '|': operator.or_,
3072 '**': operator.pow,
3073 '>>': operator.rshift,
3074 '-': operator.sub,
3075 #'/': operator.truediv,
3076 '^': operator.xor,
3077 'in': operator.contains,
3078 'not_in': _not_in,
3081 def get_compile_time_binop(node):
3082 func = compile_time_binary_operators.get(node.operator)
3083 if not func:
3084 error(node.pos,
3085 "Binary '%s' not supported in compile-time expression"
3086 % node.operator)
3087 return func
3089 class BinopNode(ExprNode):
3090 # operator string
3091 # operand1 ExprNode
3092 # operand2 ExprNode
3094 # Processing during analyse_expressions phase:
3096 # analyse_c_operation
3097 # Called when neither operand is a pyobject.
3098 # - Check operand types and coerce if needed.
3099 # - Determine result type and result code fragment.
3100 # - Allocate temporary for result if needed.
3102 subexprs = ['operand1', 'operand2']
3104 def compile_time_value(self, denv):
3105 func = get_compile_time_binop(self)
3106 operand1 = self.operand1.compile_time_value(denv)
3107 operand2 = self.operand2.compile_time_value(denv)
3108 try:
3109 return func(operand1, operand2)
3110 except Exception, e:
3111 self.compile_time_value_error(e)
3113 def analyse_types(self, env):
3114 self.operand1.analyse_types(env)
3115 self.operand2.analyse_types(env)
3116 if self.is_py_operation():
3117 self.coerce_operands_to_pyobjects(env)
3118 self.type = py_object_type
3119 self.gil_check(env)
3120 self.is_temp = 1
3121 if Options.incref_local_binop and self.operand1.type.is_pyobject:
3122 self.operand1 = self.operand1.coerce_to_temp(env)
3123 else:
3124 self.analyse_c_operation(env)
3126 def is_py_operation(self):
3127 return (self.operand1.type.is_pyobject
3128 or self.operand2.type.is_pyobject)
3130 def coerce_operands_to_pyobjects(self, env):
3131 self.operand1 = self.operand1.coerce_to_pyobject(env)
3132 self.operand2 = self.operand2.coerce_to_pyobject(env)
3134 def check_const(self):
3135 self.operand1.check_const()
3136 self.operand2.check_const()
3138 def generate_result_code(self, code):
3139 #print "BinopNode.generate_result_code:", self.operand1, self.operand2 ###
3140 if self.operand1.type.is_pyobject:
3141 function = self.py_operation_function()
3142 if function == "PyNumber_Power":
3143 extra_args = ", Py_None"
3144 else:
3145 extra_args = ""
3146 code.putln(
3147 "%s = %s(%s, %s%s); %s" % (
3148 self.result_code,
3149 function,
3150 self.operand1.py_result(),
3151 self.operand2.py_result(),
3152 extra_args,
3153 code.error_goto_if_null(self.result_code, self.pos)))
3154 else:
3155 if self.is_temp:
3156 self.generate_c_operation_code(code)
3158 def type_error(self):
3159 if not (self.operand1.type.is_error
3160 or self.operand2.type.is_error):
3161 error(self.pos, "Invalid operand types for '%s' (%s; %s)" %
3162 (self.operator, self.operand1.type,
3163 self.operand2.type))
3164 self.type = PyrexTypes.error_type
3167 class NumBinopNode(BinopNode):
3168 # Binary operation taking numeric arguments.
3170 def analyse_c_operation(self, env):
3171 type1 = self.operand1.type
3172 type2 = self.operand2.type
3173 if self.operator == "**" and type1.is_int and type2.is_int:
3174 error(self.pos, "** with two C int types is ambiguous")
3175 self.type = error_type
3176 return
3177 self.type = self.compute_c_result_type(type1, type2)
3178 if not self.type:
3179 self.type_error()
3181 def compute_c_result_type(self, type1, type2):
3182 if self.c_types_okay(type1, type2):
3183 return PyrexTypes.widest_numeric_type(type1, type2)
3184 else:
3185 return None
3187 def c_types_okay(self, type1, type2):
3188 #print "NumBinopNode.c_types_okay:", type1, type2 ###
3189 return (type1.is_numeric or type1.is_enum) \
3190 and (type2.is_numeric or type2.is_enum)
3192 def calculate_result_code(self):
3193 return "(%s %s %s)" % (
3194 self.operand1.result_code,
3195 self.operator,
3196 self.operand2.result_code)
3198 def py_operation_function(self):
3199 return self.py_functions[self.operator]
3201 py_functions = {
3202 "|": "PyNumber_Or",
3203 "^": "PyNumber_Xor",
3204 "&": "PyNumber_And",
3205 "<<": "PyNumber_Lshift",
3206 ">>": "PyNumber_Rshift",
3207 "+": "PyNumber_Add",
3208 "-": "PyNumber_Subtract",
3209 "*": "PyNumber_Multiply",
3210 "/": "__Pyx_PyNumber_Divide",
3211 "//": "PyNumber_FloorDivide",
3212 "%": "PyNumber_Remainder",
3213 "**": "PyNumber_Power"
3217 class IntBinopNode(NumBinopNode):
3218 # Binary operation taking integer arguments.
3220 def c_types_okay(self, type1, type2):
3221 #print "IntBinopNode.c_types_okay:", type1, type2 ###
3222 return (type1.is_int or type1.is_enum) \
3223 and (type2.is_int or type2.is_enum)
3226 class AddNode(NumBinopNode):
3227 # '+' operator.
3229 def is_py_operation(self):
3230 if self.operand1.type.is_string \
3231 and self.operand2.type.is_string:
3232 return 1
3233 else:
3234 return NumBinopNode.is_py_operation(self)
3236 def compute_c_result_type(self, type1, type2):
3237 #print "AddNode.compute_c_result_type:", type1, self.operator, type2 ###
3238 if (type1.is_ptr or type1.is_array) and (type2.is_int or type2.is_enum):
3239 return type1
3240 elif (type2.is_ptr or type2.is_array) and (type1.is_int or type1.is_enum):
3241 return type2
3242 else:
3243 return NumBinopNode.compute_c_result_type(
3244 self, type1, type2)
3247 class SubNode(NumBinopNode):
3248 # '-' operator.
3250 def compute_c_result_type(self, type1, type2):
3251 if (type1.is_ptr or type1.is_array) and (type2.is_int or type2.is_enum):
3252 return type1
3253 elif (type1.is_ptr or type1.is_array) and (type2.is_ptr or type2.is_array):
3254 return PyrexTypes.c_int_type
3255 else:
3256 return NumBinopNode.compute_c_result_type(
3257 self, type1, type2)
3260 class MulNode(NumBinopNode):
3261 # '*' operator.
3263 def is_py_operation(self):
3264 type1 = self.operand1.type
3265 type2 = self.operand2.type
3266 if (type1.is_string and type2.is_int) \
3267 or (type2.is_string and type1.is_int):
3268 return 1
3269 else:
3270 return NumBinopNode.is_py_operation(self)
3273 class FloorDivNode(NumBinopNode):
3274 # '//' operator.
3276 def calculate_result_code(self):
3277 return "(%s %s %s)" % (
3278 self.operand1.result_code,
3279 "/", # c division is by default floor-div
3280 self.operand2.result_code)
3283 class ModNode(IntBinopNode):
3284 # '%' operator.
3286 def is_py_operation(self):
3287 return (self.operand1.type.is_string
3288 or self.operand2.type.is_string
3289 or IntBinopNode.is_py_operation(self))
3292 class PowNode(NumBinopNode):
3293 # '**' operator.
3295 def analyse_types(self, env):
3296 env.pow_function_used = 1
3297 NumBinopNode.analyse_types(self, env)
3299 def compute_c_result_type(self, type1, type2):
3300 if self.c_types_okay(type1, type2):
3301 return PyrexTypes.c_double_type
3302 else:
3303 return None
3305 def c_types_okay(self, type1, type2):
3306 return (type1.is_float or type2.is_float) and \
3307 NumBinopNode.c_types_okay(self, type1, type2)
3309 def type_error(self):
3310 if not (self.operand1.type.is_error or self.operand2.type.is_error):
3311 if self.operand1.type.is_int and self.operand2.type.is_int:
3312 error(self.pos, "C has no integer powering, use python ints or floats instead '%s' (%s; %s)" %
3313 (self.operator, self.operand1.type, self.operand2.type))
3314 else:
3315 NumBinopNode.type_error(self)
3316 self.type = PyrexTypes.error_type
3318 def calculate_result_code(self):
3319 return "pow(%s, %s)" % (
3320 self.operand1.result_code, self.operand2.result_code)
3323 class BoolBinopNode(ExprNode):
3324 # Short-circuiting boolean operation.
3326 # operator string
3327 # operand1 ExprNode
3328 # operand2 ExprNode
3329 # temp_bool ExprNode used internally
3331 temp_bool = None
3333 subexprs = ['operand1', 'operand2', 'temp_bool']
3335 def compile_time_value(self, denv):
3336 if self.operator == 'and':
3337 return self.operand1.compile_time_value(denv) \
3338 and self.operand2.compile_time_value(denv)
3339 else:
3340 return self.operand1.compile_time_value(denv) \
3341 or self.operand2.compile_time_value(denv)
3343 def analyse_types(self, env):
3344 self.operand1.analyse_types(env)
3345 self.operand2.analyse_types(env)
3346 if self.operand1.type.is_pyobject or \
3347 self.operand2.type.is_pyobject:
3348 self.operand1 = self.operand1.coerce_to_pyobject(env)
3349 self.operand2 = self.operand2.coerce_to_pyobject(env)
3350 self.temp_bool = TempNode(self.pos, PyrexTypes.c_bint_type, env)
3351 self.type = py_object_type
3352 self.gil_check(env)
3353 else:
3354 self.operand1 = self.operand1.coerce_to_boolean(env)
3355 self.operand2 = self.operand2.coerce_to_boolean(env)
3356 self.type = PyrexTypes.c_bint_type
3357 # For what we're about to do, it's vital that
3358 # both operands be temp nodes.
3359 self.operand1 = self.operand1.coerce_to_temp(env) #CTT
3360 self.operand2 = self.operand2.coerce_to_temp(env)
3361 self.is_temp = 1
3363 gil_message = "Truth-testing Python object"
3365 def allocate_temps(self, env, result_code = None):
3366 # We don't need both operands at the same time, and
3367 # one of the operands will also be our result. So we
3368 # use an allocation strategy here which results in
3369 # this node and both its operands sharing the same
3370 # result variable. This allows us to avoid some
3371 # assignments and increfs/decrefs that would otherwise
3372 # be necessary.
3373 self.allocate_temp(env, result_code)
3374 self.operand1.allocate_temps(env, self.result_code)
3375 if self.temp_bool:
3376 self.temp_bool.allocate_temp(env)
3377 self.temp_bool.release_temp(env)
3378 self.operand2.allocate_temps(env, self.result_code)
3379 # We haven't called release_temp on either operand,
3380 # because although they are temp nodes, they don't own
3381 # their result variable. And because they are temp
3382 # nodes, any temps in their subnodes will have been
3383 # released before their allocate_temps returned.
3384 # Therefore, they contain no temp vars that need to
3385 # be released.
3387 def check_const(self):
3388 self.operand1.check_const()
3389 self.operand2.check_const()
3391 def calculate_result_code(self):
3392 return "(%s %s %s)" % (
3393 self.operand1.result_code,
3394 self.py_to_c_op[self.operator],
3395 self.operand2.result_code)
3397 py_to_c_op = {'and': "&&", 'or': "||"}
3399 def generate_evaluation_code(self, code):
3400 self.operand1.generate_evaluation_code(code)
3401 test_result = self.generate_operand1_test(code)
3402 if self.operator == 'and':
3403 sense = ""
3404 else:
3405 sense = "!"
3406 code.putln(
3407 "if (%s%s) {" % (
3408 sense,
3409 test_result))
3410 self.operand1.generate_disposal_code(code)
3411 self.operand2.generate_evaluation_code(code)
3412 code.putln(
3413 "}")
3415 def generate_operand1_test(self, code):
3416 # Generate code to test the truth of the first operand.
3417 if self.type.is_pyobject:
3418 test_result = self.temp_bool.result_code
3419 code.putln(
3420 "%s = __Pyx_PyObject_IsTrue(%s); %s" % (
3421 test_result,
3422 self.operand1.py_result(),
3423 code.error_goto_if_neg(test_result, self.pos)))
3424 else:
3425 test_result = self.operand1.result_code
3426 return test_result
3429 class CondExprNode(ExprNode):
3430 # Short-circuiting conditional expression.
3432 # test ExprNode
3433 # true_val ExprNode
3434 # false_val ExprNode
3436 temp_bool = None
3437 true_val = None
3438 false_val = None
3440 subexprs = ['test', 'true_val', 'false_val']
3442 def analyse_types(self, env):
3443 self.test.analyse_types(env)
3444 self.test = self.test.coerce_to_boolean(env)
3445 self.true_val.analyse_types(env)
3446 self.false_val.analyse_types(env)
3447 self.type = self.compute_result_type(self.true_val.type, self.false_val.type)
3448 if self.true_val.type.is_pyobject or self.false_val.type.is_pyobject:
3449 self.true_val = self.true_val.coerce_to(self.type, env)
3450 self.false_val = self.false_val.coerce_to(self.type, env)
3451 # must be tmp variables so they can share a result
3452 self.true_val = self.true_val.coerce_to_temp(env)
3453 self.false_val = self.false_val.coerce_to_temp(env)
3454 self.is_temp = 1
3455 if self.type == PyrexTypes.error_type:
3456 self.type_error()
3458 def allocate_temps(self, env, result_code = None):
3459 # We only ever evaluate one side, and this is
3460 # after evaluating the truth value, so we may
3461 # use an allocation strategy here which results in
3462 # this node and both its operands sharing the same
3463 # result variable. This allows us to avoid some
3464 # assignments and increfs/decrefs that would otherwise
3465 # be necessary.
3466 self.allocate_temp(env, result_code)
3467 self.test.allocate_temps(env, result_code)
3468 self.true_val.allocate_temps(env, self.result_code)
3469 self.false_val.allocate_temps(env, self.result_code)
3470 # We haven't called release_temp on either value,
3471 # because although they are temp nodes, they don't own
3472 # their result variable. And because they are temp
3473 # nodes, any temps in their subnodes will have been
3474 # released before their allocate_temps returned.
3475 # Therefore, they contain no temp vars that need to
3476 # be released.
3478 def compute_result_type(self, type1, type2):
3479 if type1 == type2:
3480 return type1
3481 elif type1.is_numeric and type2.is_numeric:
3482 return PyrexTypes.widest_numeric_type(type1, type2)
3483 elif type1.is_extension_type and type1.subtype_of_resolved_type(type2):
3484 return type2
3485 elif type2.is_extension_type and type2.subtype_of_resolved_type(type1):
3486 return type1
3487 elif type1.is_pyobject or type2.is_pyobject:
3488 return py_object_type
3489 elif type1.assignable_from(type2):
3490 return type1
3491 elif type2.assignable_from(type1):
3492 return type2
3493 else:
3494 return PyrexTypes.error_type
3496 def type_error(self):
3497 if not (self.true_val.type.is_error or self.false_val.type.is_error):
3498 error(self.pos, "Incompatable types in conditional expression (%s; %s)" %
3499 (self.true_val.type, self.false_val.type))
3500 self.type = PyrexTypes.error_type
3502 def check_const(self):
3503 self.test.check_const()
3504 self.true_val.check_const()
3505 self.false_val.check_const()
3507 def generate_evaluation_code(self, code):
3508 self.test.generate_evaluation_code(code)
3509 code.putln("if (%s) {" % self.test.result_code )
3510 self.true_val.generate_evaluation_code(code)
3511 code.putln("} else {")
3512 self.false_val.generate_evaluation_code(code)
3513 code.putln("}")
3514 self.test.generate_disposal_code(code)
3516 richcmp_constants = {
3517 "<" : "Py_LT",
3518 "<=": "Py_LE",
3519 "==": "Py_EQ",
3520 "!=": "Py_NE",
3521 "<>": "Py_NE",
3522 ">" : "Py_GT",
3523 ">=": "Py_GE",
3526 class CmpNode:
3527 # Mixin class containing code common to PrimaryCmpNodes
3528 # and CascadedCmpNodes.
3530 def cascaded_compile_time_value(self, operand1, denv):
3531 func = get_compile_time_binop(self)
3532 operand2 = self.operand2.compile_time_value(denv)
3533 try:
3534 result = func(operand1, operand2)
3535 except Exception, e:
3536 self.compile_time_value_error(e)
3537 result = None
3538 if result:
3539 cascade = self.cascade
3540 if cascade:
3541 result = result and cascade.compile_time_value(operand2, denv)
3542 return result
3544 def is_python_comparison(self):
3545 return (self.has_python_operands()
3546 or (self.cascade and self.cascade.is_python_comparison())
3547 or self.operator in ('in', 'not_in'))
3549 def is_python_result(self):
3550 return ((self.has_python_operands() and self.operator not in ('is', 'is_not', 'in', 'not_in'))
3551 or (self.cascade and self.cascade.is_python_result()))
3553 def check_types(self, env, operand1, op, operand2):
3554 if not self.types_okay(operand1, op, operand2):
3555 error(self.pos, "Invalid types for '%s' (%s, %s)" %
3556 (self.operator, operand1.type, operand2.type))
3558 def types_okay(self, operand1, op, operand2):
3559 type1 = operand1.type
3560 type2 = operand2.type
3561 if type1.is_error or type2.is_error:
3562 return 1
3563 if type1.is_pyobject: # type2 will be, too
3564 return 1
3565 elif type1.is_ptr or type1.is_array:
3566 return type1.is_null_ptr or type2.is_null_ptr \
3567 or ((type2.is_ptr or type2.is_array)
3568 and type1.base_type.same_as(type2.base_type))
3569 elif ((type1.is_numeric and type2.is_numeric
3570 or type1.is_enum and (type1 is type2 or type2.is_int)
3571 or type1.is_int and type2.is_enum)
3572 and op not in ('is', 'is_not')):
3573 return 1
3574 else:
3575 return type1.is_cfunction and type1.is_cfunction and type1 == type2
3577 def generate_operation_code(self, code, result_code,
3578 operand1, op , operand2):
3579 if self.type is PyrexTypes.py_object_type:
3580 coerce_result = "__Pyx_PyBool_FromLong"
3581 else:
3582 coerce_result = ""
3583 if 'not' in op: negation = "!"
3584 else: negation = ""
3585 if op == 'in' or op == 'not_in':
3586 code.putln(
3587 "%s = %s(%sPySequence_Contains(%s, %s)); %s" % (
3588 result_code,
3589 coerce_result,
3590 negation,
3591 operand2.py_result(),
3592 operand1.py_result(),
3593 code.error_goto_if_neg(result_code, self.pos)))
3594 elif (operand1.type.is_pyobject
3595 and op not in ('is', 'is_not')):
3596 code.putln("%s = PyObject_RichCompare(%s, %s, %s); %s" % (
3597 result_code,
3598 operand1.py_result(),
3599 operand2.py_result(),
3600 richcmp_constants[op],
3601 code.error_goto_if_null(result_code, self.pos)))
3602 else:
3603 type1 = operand1.type
3604 type2 = operand2.type
3605 if (type1.is_extension_type or type2.is_extension_type) \
3606 and not type1.same_as(type2):
3607 common_type = py_object_type
3608 elif type1.is_numeric:
3609 common_type = PyrexTypes.widest_numeric_type(type1, type2)
3610 else:
3611 common_type = type1
3612 code1 = operand1.result_as(common_type)
3613 code2 = operand2.result_as(common_type)
3614 code.putln("%s = %s(%s %s %s);" % (
3615 result_code,
3616 coerce_result,
3617 code1,
3618 self.c_operator(op),
3619 code2))
3621 def c_operator(self, op):
3622 if op == 'is':
3623 return "=="
3624 elif op == 'is_not':
3625 return "!="
3626 else:
3627 return op
3630 class PrimaryCmpNode(ExprNode, CmpNode):
3631 # Non-cascaded comparison or first comparison of
3632 # a cascaded sequence.
3634 # operator string
3635 # operand1 ExprNode
3636 # operand2 ExprNode
3637 # cascade CascadedCmpNode
3639 # We don't use the subexprs mechanism, because
3640 # things here are too complicated for it to handle.
3641 # Instead, we override all the framework methods
3642 # which use it.
3644 child_attrs = ['operand1', 'operand2', 'cascade']
3646 cascade = None
3648 def compile_time_value(self, denv):
3649 operand1 = self.operand1.compile_time_value(denv)
3650 return self.cascaded_compile_time_value(operand1, denv)
3652 def analyse_types(self, env):
3653 self.operand1.analyse_types(env)
3654 self.operand2.analyse_types(env)
3655 if self.cascade:
3656 self.cascade.analyse_types(env, self.operand2)
3657 self.is_pycmp = self.is_python_comparison()
3658 if self.is_pycmp:
3659 self.coerce_operands_to_pyobjects(env)
3660 if self.has_int_operands():
3661 self.coerce_chars_to_ints(env)
3662 if self.cascade:
3663 self.operand2 = self.operand2.coerce_to_simple(env)
3664 self.cascade.coerce_cascaded_operands_to_temp(env)
3665 self.check_operand_types(env)
3666 if self.is_python_result():
3667 self.type = PyrexTypes.py_object_type
3668 else:
3669 self.type = PyrexTypes.c_bint_type
3670 cdr = self.cascade
3671 while cdr:
3672 cdr.type = self.type
3673 cdr = cdr.cascade
3674 if self.is_pycmp or self.cascade:
3675 self.is_temp = 1
3677 def check_operand_types(self, env):
3678 self.check_types(env,
3679 self.operand1, self.operator, self.operand2)
3680 if self.cascade:
3681 self.cascade.check_operand_types(env, self.operand2)
3683 def has_python_operands(self):
3684 return (self.operand1.type.is_pyobject
3685 or self.operand2.type.is_pyobject)
3687 def coerce_operands_to_pyobjects(self, env):
3688 self.operand1 = self.operand1.coerce_to_pyobject(env)
3689 self.operand2 = self.operand2.coerce_to_pyobject(env)
3690 if self.cascade:
3691 self.cascade.coerce_operands_to_pyobjects(env)
3693 def has_int_operands(self):
3694 return (self.operand1.type.is_int or self.operand2.type.is_int) \
3695 or (self.cascade and self.cascade.has_int_operands())
3697 def coerce_chars_to_ints(self, env):
3698 # coerce literal single-char strings to c chars
3699 if self.operand1.type.is_string and isinstance(self.operand1, StringNode):
3700 self.operand1 = self.operand1.coerce_to(PyrexTypes.c_uchar_type, env)
3701 if self.operand2.type.is_string and isinstance(self.operand2, StringNode):
3702 self.operand2 = self.operand2.coerce_to(PyrexTypes.c_uchar_type, env)
3703 if self.cascade:
3704 self.cascade.coerce_chars_to_ints(env)
3706 def allocate_subexpr_temps(self, env):
3707 self.operand1.allocate_temps(env)
3708 self.operand2.allocate_temps(env)
3709 if self.cascade:
3710 self.cascade.allocate_subexpr_temps(env)
3712 def release_subexpr_temps(self, env):
3713 self.operand1.release_temp(env)
3714 self.operand2.release_temp(env)
3715 if self.cascade:
3716 self.cascade.release_subexpr_temps(env)
3718 def check_const(self):
3719 self.operand1.check_const()
3720 self.operand2.check_const()
3721 if self.cascade:
3722 self.not_const()
3724 def calculate_result_code(self):
3725 return "(%s %s %s)" % (
3726 self.operand1.result_code,
3727 self.c_operator(self.operator),
3728 self.operand2.result_code)
3730 def generate_evaluation_code(self, code):
3731 self.operand1.generate_evaluation_code(code)
3732 self.operand2.generate_evaluation_code(code)
3733 if self.is_temp:
3734 self.generate_operation_code(code, self.result_code,
3735 self.operand1, self.operator, self.operand2)
3736 if self.cascade:
3737 self.cascade.generate_evaluation_code(code,
3738 self.result_code, self.operand2)
3739 self.operand1.generate_disposal_code(code)
3740 self.operand2.generate_disposal_code(code)
3742 def generate_subexpr_disposal_code(self, code):
3743 # If this is called, it is a non-cascaded cmp,
3744 # so only need to dispose of the two main operands.
3745 self.operand1.generate_disposal_code(code)
3746 self.operand2.generate_disposal_code(code)
3748 def annotate(self, code):
3749 self.operand1.annotate(code)
3750 self.operand2.annotate(code)
3751 if self.cascade:
3752 self.cascade.annotate(code)
3755 class CascadedCmpNode(Node, CmpNode):
3756 # A CascadedCmpNode is not a complete expression node. It
3757 # hangs off the side of another comparison node, shares
3758 # its left operand with that node, and shares its result
3759 # with the PrimaryCmpNode at the head of the chain.
3761 # operator string
3762 # operand2 ExprNode
3763 # cascade CascadedCmpNode
3765 child_attrs = ['operand2', 'cascade']
3767 cascade = None
3769 def analyse_types(self, env, operand1):
3770 self.operand2.analyse_types(env)
3771 if self.cascade:
3772 self.cascade.analyse_types(env, self.operand2)
3774 def check_operand_types(self, env, operand1):
3775 self.check_types(env,
3776 operand1, self.operator, self.operand2)
3777 if self.cascade:
3778 self.cascade.check_operand_types(env, self.operand2)
3780 def has_python_operands(self):
3781 return self.operand2.type.is_pyobject
3783 def coerce_operands_to_pyobjects(self, env):
3784 self.operand2 = self.operand2.coerce_to_pyobject(env)
3785 if self.cascade:
3786 self.cascade.coerce_operands_to_pyobjects(env)
3788 def has_int_operands(self):
3789 return self.operand2.type.is_int
3791 def coerce_chars_to_ints(self, env):
3792 if self.operand2.type.is_string and isinstance(self.operand2, StringNode):
3793 self.operand2 = self.operand2.coerce_to(PyrexTypes.c_uchar_type, env)
3795 def coerce_cascaded_operands_to_temp(self, env):
3796 if self.cascade:
3797 #self.operand2 = self.operand2.coerce_to_temp(env) #CTT
3798 self.operand2 = self.operand2.coerce_to_simple(env)
3799 self.cascade.coerce_cascaded_operands_to_temp(env)
3801 def allocate_subexpr_temps(self, env):
3802 self.operand2.allocate_temps(env)
3803 if self.cascade:
3804 self.cascade.allocate_subexpr_temps(env)
3806 def release_subexpr_temps(self, env):
3807 self.operand2.release_temp(env)
3808 if self.cascade:
3809 self.cascade.release_subexpr_temps(env)
3811 def generate_evaluation_code(self, code, result, operand1):
3812 if self.type.is_pyobject:
3813 code.putln("if (__Pyx_PyObject_IsTrue(%s)) {" % result)
3814 else:
3815 code.putln("if (%s) {" % result)
3816 self.operand2.generate_evaluation_code(code)
3817 self.generate_operation_code(code, result,
3818 operand1, self.operator, self.operand2)
3819 if self.cascade:
3820 self.cascade.generate_evaluation_code(
3821 code, result, self.operand2)
3822 # Cascaded cmp result is always temp
3823 self.operand2.generate_disposal_code(code)
3824 code.putln("}")
3826 def annotate(self, code):
3827 self.operand2.annotate(code)
3828 if self.cascade:
3829 self.cascade.annotate(code)
3832 binop_node_classes = {
3833 "or": BoolBinopNode,
3834 "and": BoolBinopNode,
3835 "|": IntBinopNode,
3836 "^": IntBinopNode,
3837 "&": IntBinopNode,
3838 "<<": IntBinopNode,
3839 ">>": IntBinopNode,
3840 "+": AddNode,
3841 "-": SubNode,
3842 "*": MulNode,
3843 "/": NumBinopNode,
3844 "//": FloorDivNode,
3845 "%": ModNode,
3846 "**": PowNode
3849 def binop_node(pos, operator, operand1, operand2):
3850 # Construct binop node of appropriate class for
3851 # given operator.
3852 return binop_node_classes[operator](pos,
3853 operator = operator,
3854 operand1 = operand1,
3855 operand2 = operand2)
3857 #-------------------------------------------------------------------
3859 # Coercion nodes
3861 # Coercion nodes are special in that they are created during
3862 # the analyse_types phase of parse tree processing.
3863 # Their __init__ methods consequently incorporate some aspects
3864 # of that phase.
3866 #-------------------------------------------------------------------
3868 class CoercionNode(ExprNode):
3869 # Abstract base class for coercion nodes.
3871 # arg ExprNode node being coerced
3873 subexprs = ['arg']
3875 def __init__(self, arg):
3876 self.pos = arg.pos
3877 self.arg = arg
3878 if debug_coercion:
3879 print("%s Coercing %s" % (self, self.arg))
3881 def annotate(self, code):
3882 self.arg.annotate(code)
3883 if self.arg.type != self.type:
3884 file, line, col = self.pos
3885 code.annotate((file, line, col-1), AnnotationItem(style='coerce', tag='coerce', text='[%s] to [%s]' % (self.arg.type, self.type)))
3888 class CastNode(CoercionNode):
3889 # Wrap a node in a C type cast.
3891 def __init__(self, arg, new_type):
3892 CoercionNode.__init__(self, arg)
3893 self.type = new_type
3895 def calculate_result_code(self):
3896 return self.arg.result_as(self.type)
3898 def generate_result_code(self, code):
3899 self.arg.generate_result_code(code)
3902 class PyTypeTestNode(CoercionNode):
3903 # This node is used to check that a generic Python
3904 # object is an instance of a particular extension type.
3905 # This node borrows the result of its argument node.
3907 def __init__(self, arg, dst_type, env):
3908 # The arg is know to be a Python object, and
3909 # the dst_type is known to be an extension type.
3910 assert dst_type.is_extension_type or dst_type.is_builtin_type, "PyTypeTest on non extension type"
3911 CoercionNode.__init__(self, arg)
3912 self.type = dst_type
3913 self.gil_check(env)
3914 self.result_ctype = arg.ctype()
3915 env.use_utility_code(type_test_utility_code)
3917 gil_message = "Python type test"
3919 def analyse_types(self, env):
3920 pass
3922 def result_in_temp(self):
3923 return self.arg.result_in_temp()
3925 def is_ephemeral(self):
3926 return self.arg.is_ephemeral()
3928 def calculate_result_code(self):
3929 return self.arg.result_code
3931 def generate_result_code(self, code):
3932 if self.type.typeobj_is_available():
3933 code.putln(
3934 "if (!(%s)) %s" % (
3935 self.type.type_test_code(self.arg.py_result()),
3936 code.error_goto(self.pos)))
3937 else:
3938 error(self.pos, "Cannot test type of extern C class "
3939 "without type object name specification")
3941 def generate_post_assignment_code(self, code):
3942 self.arg.generate_post_assignment_code(code)
3945 class CoerceToPyTypeNode(CoercionNode):
3946 # This node is used to convert a C data type
3947 # to a Python object.
3949 def __init__(self, arg, env):
3950 CoercionNode.__init__(self, arg)
3951 self.type = py_object_type
3952 self.gil_check(env)
3953 self.is_temp = 1
3954 if not arg.type.to_py_function:
3955 error(arg.pos,
3956 "Cannot convert '%s' to Python object" % arg.type)
3958 gil_message = "Converting to Python object"
3960 def analyse_types(self, env):
3961 # The arg is always already analysed
3962 pass
3964 def generate_result_code(self, code):
3965 function = self.arg.type.to_py_function
3966 code.putln('%s = %s(%s); %s' % (
3967 self.result_code,
3968 function,
3969 self.arg.result_code,
3970 code.error_goto_if_null(self.result_code, self.pos)))
3973 class CoerceFromPyTypeNode(CoercionNode):
3974 # This node is used to convert a Python object
3975 # to a C data type.
3977 def __init__(self, result_type, arg, env):
3978 CoercionNode.__init__(self, arg)
3979 self.type = result_type
3980 self.is_temp = 1
3981 if not result_type.from_py_function:
3982 error(arg.pos,
3983 "Cannot convert Python object to '%s'" % result_type)
3984 if self.type.is_string and self.arg.is_ephemeral():
3985 error(arg.pos,
3986 "Obtaining char * from temporary Python value")
3988 def analyse_types(self, env):
3989 # The arg is always already analysed
3990 pass
3992 def generate_result_code(self, code):
3993 function = self.type.from_py_function
3994 operand = self.arg.py_result()
3995 rhs = "%s(%s)" % (function, operand)
3996 if self.type.is_enum:
3997 rhs = typecast(self.type, c_long_type, rhs)
3998 code.putln('%s = %s; %s' % (
3999 self.result_code,
4000 rhs,
4001 code.error_goto_if(self.type.error_condition(self.result_code), self.pos)))
4004 class CoerceToBooleanNode(CoercionNode):
4005 # This node is used when a result needs to be used
4006 # in a boolean context.
4008 def __init__(self, arg, env):
4009 CoercionNode.__init__(self, arg)
4010 self.type = PyrexTypes.c_bint_type
4011 if arg.type.is_pyobject:
4012 if env.nogil:
4013 self.gil_error()
4014 self.is_temp = 1
4016 gil_message = "Truth-testing Python object"
4018 def check_const(self):
4019 if self.is_temp:
4020 self.not_const()
4021 self.arg.check_const()
4023 def calculate_result_code(self):
4024 return "(%s != 0)" % self.arg.result_code
4026 def generate_result_code(self, code):
4027 if self.arg.type.is_pyobject:
4028 code.putln(
4029 "%s = __Pyx_PyObject_IsTrue(%s); %s" % (
4030 self.result_code,
4031 self.arg.py_result(),
4032 code.error_goto_if_neg(self.result_code, self.pos)))
4035 class CoerceToTempNode(CoercionNode):
4036 # This node is used to force the result of another node
4037 # to be stored in a temporary. It is only used if the
4038 # argument node's result is not already in a temporary.
4040 def __init__(self, arg, env):
4041 CoercionNode.__init__(self, arg)
4042 self.type = self.arg.type
4043 self.is_temp = 1
4044 if self.type.is_pyobject:
4045 self.gil_check(env)
4046 self.result_ctype = py_object_type
4048 gil_message = "Creating temporary Python reference"
4050 def analyse_types(self, env):
4051 # The arg is always already analysed
4052 pass
4054 def generate_result_code(self, code):
4055 #self.arg.generate_evaluation_code(code) # Already done
4056 # by generic generate_subexpr_evaluation_code!
4057 code.putln("%s = %s;" % (
4058 self.result_code, self.arg.result_as(self.ctype())))
4059 if self.type.is_pyobject:
4060 code.put_incref(self.result_code, self.ctype())
4063 class CloneNode(CoercionNode):
4064 # This node is employed when the result of another node needs
4065 # to be used multiple times. The argument node's result must
4066 # be in a temporary. This node "borrows" the result from the
4067 # argument node, and does not generate any evaluation or
4068 # disposal code for it. The original owner of the argument
4069 # node is responsible for doing those things.
4071 subexprs = [] # Arg is not considered a subexpr
4073 def __init__(self, arg):
4074 CoercionNode.__init__(self, arg)
4075 if hasattr(arg, 'type'):
4076 self.type = arg.type
4077 self.result_ctype = arg.result_ctype
4078 if hasattr(arg, 'entry'):
4079 self.entry = arg.entry
4081 def calculate_result_code(self):
4082 return self.arg.result_code
4084 def analyse_types(self, env):
4085 self.type = self.arg.type
4086 self.result_ctype = self.arg.result_ctype
4087 self.is_temp = 1
4088 if hasattr(self.arg, 'entry'):
4089 self.entry = self.arg.entry
4091 def generate_evaluation_code(self, code):
4092 pass
4094 def generate_result_code(self, code):
4095 pass
4097 def generate_disposal_code(self, code):
4098 pass
4100 def allocate_temps(self, env):
4101 self.result_code = self.calculate_result_code()
4103 def release_temp(self, env):
4104 pass
4106 class PersistentNode(ExprNode):
4107 # A PersistentNode is like a CloneNode except it handles the temporary
4108 # allocation itself by keeping track of the number of times it has been
4109 # used.
4111 subexprs = ["arg"]
4112 temp_counter = 0
4113 generate_counter = 0
4114 analyse_counter = 0
4115 result_code = None
4117 def __init__(self, arg, uses):
4118 self.pos = arg.pos
4119 self.arg = arg
4120 self.uses = uses
4122 def analyse_types(self, env):
4123 if self.analyse_counter == 0:
4124 self.arg.analyse_types(env)
4125 self.type = self.arg.type
4126 self.result_ctype = self.arg.result_ctype
4127 self.is_temp = 1
4128 self.analyse_counter += 1
4130 def calculate_result_code(self):
4131 return self.result_code
4133 def generate_evaluation_code(self, code):
4134 if self.generate_counter == 0:
4135 self.arg.generate_evaluation_code(code)
4136 code.putln("%s = %s;" % (
4137 self.result_code, self.arg.result_as(self.ctype())))
4138 if self.type.is_pyobject:
4139 code.put_incref(self.result_code, self.ctype())
4140 self.arg.generate_disposal_code(code)
4141 self.generate_counter += 1
4143 def generate_disposal_code(self, code):
4144 if self.generate_counter == self.uses:
4145 if self.type.is_pyobject:
4146 code.put_decref_clear(self.result_code, self.ctype())
4148 def allocate_temps(self, env, result=None):
4149 if self.temp_counter == 0:
4150 self.arg.allocate_temps(env)
4151 self.allocate_temp(env, result)
4152 self.arg.release_temp(env)
4153 self.temp_counter += 1
4155 def allocate_temp(self, env, result=None):
4156 if result is None:
4157 self.result_code = env.allocate_temp(self.type)
4158 else:
4159 self.result_code = result
4161 def release_temp(self, env):
4162 if self.temp_counter == self.uses:
4163 env.release_temp(self.result_code)
4165 #------------------------------------------------------------------------------------
4167 # Runtime support code
4169 #------------------------------------------------------------------------------------
4171 get_name_interned_utility_code = [
4172 """
4173 static PyObject *__Pyx_GetName(PyObject *dict, PyObject *name); /*proto*/
4174 ""","""
4175 static PyObject *__Pyx_GetName(PyObject *dict, PyObject *name) {
4176 PyObject *result;
4177 result = PyObject_GetAttr(dict, name);
4178 if (!result)
4179 PyErr_SetObject(PyExc_NameError, name);
4180 return result;
4182 """]
4184 #------------------------------------------------------------------------------------
4186 import_utility_code = [
4187 """
4188 static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list); /*proto*/
4189 ""","""
4190 static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list) {
4191 PyObject *__import__ = 0;
4192 PyObject *empty_list = 0;
4193 PyObject *module = 0;
4194 PyObject *global_dict = 0;
4195 PyObject *empty_dict = 0;
4196 PyObject *list;
4197 __import__ = PyObject_GetAttrString(%(BUILTINS)s, "__import__");
4198 if (!__import__)
4199 goto bad;
4200 if (from_list)
4201 list = from_list;
4202 else {
4203 empty_list = PyList_New(0);
4204 if (!empty_list)
4205 goto bad;
4206 list = empty_list;
4208 global_dict = PyModule_GetDict(%(GLOBALS)s);
4209 if (!global_dict)
4210 goto bad;
4211 empty_dict = PyDict_New();
4212 if (!empty_dict)
4213 goto bad;
4214 module = PyObject_CallFunction(__import__, "OOOO",
4215 name, global_dict, empty_dict, list);
4216 bad:
4217 Py_XDECREF(empty_list);
4218 Py_XDECREF(__import__);
4219 Py_XDECREF(empty_dict);
4220 return module;
4222 """ % {
4223 "BUILTINS": Naming.builtins_cname,
4224 "GLOBALS": Naming.module_cname,
4225 }]
4227 #------------------------------------------------------------------------------------
4229 get_exception_utility_code = [
4230 """
4231 static PyObject *__Pyx_GetExcValue(void); /*proto*/
4232 ""","""
4233 static PyObject *__Pyx_GetExcValue(void) {
4234 PyObject *type = 0, *value = 0, *tb = 0;
4235 PyObject *tmp_type, *tmp_value, *tmp_tb;
4236 PyObject *result = 0;
4237 PyThreadState *tstate = PyThreadState_Get();
4238 PyErr_Fetch(&type, &value, &tb);
4239 PyErr_NormalizeException(&type, &value, &tb);
4240 if (PyErr_Occurred())
4241 goto bad;
4242 if (!value) {
4243 value = Py_None;
4244 Py_INCREF(value);
4246 tmp_type = tstate->exc_type;
4247 tmp_value = tstate->exc_value;
4248 tmp_tb = tstate->exc_traceback;
4249 tstate->exc_type = type;
4250 tstate->exc_value = value;
4251 tstate->exc_traceback = tb;
4252 /* Make sure tstate is in a consistent state when we XDECREF
4253 these objects (XDECREF may run arbitrary code). */
4254 Py_XDECREF(tmp_type);
4255 Py_XDECREF(tmp_value);
4256 Py_XDECREF(tmp_tb);
4257 result = value;
4258 Py_XINCREF(result);
4259 type = 0;
4260 value = 0;
4261 tb = 0;
4262 bad:
4263 Py_XDECREF(type);
4264 Py_XDECREF(value);
4265 Py_XDECREF(tb);
4266 return result;
4268 """]
4270 #------------------------------------------------------------------------------------
4272 unpacking_utility_code = [
4273 """
4274 static PyObject *__Pyx_UnpackItem(PyObject *, Py_ssize_t index); /*proto*/
4275 static int __Pyx_EndUnpack(PyObject *); /*proto*/
4276 ""","""
4277 static PyObject *__Pyx_UnpackItem(PyObject *iter, Py_ssize_t index) {
4278 PyObject *item;
4279 if (!(item = PyIter_Next(iter))) {
4280 if (!PyErr_Occurred()) {
4281 PyErr_Format(PyExc_ValueError,
4282 #if PY_VERSION_HEX < 0x02050000
4283 "need more than %d values to unpack", (int)index);
4284 #else
4285 "need more than %zd values to unpack", index);
4286 #endif
4289 return item;
4292 static int __Pyx_EndUnpack(PyObject *iter) {
4293 PyObject *item;
4294 if ((item = PyIter_Next(iter))) {
4295 Py_DECREF(item);
4296 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
4297 return -1;
4299 else if (!PyErr_Occurred())
4300 return 0;
4301 else
4302 return -1;
4304 """]
4306 #------------------------------------------------------------------------------------
4308 type_test_utility_code = [
4309 """
4310 static int __Pyx_TypeTest(PyObject *obj, PyTypeObject *type); /*proto*/
4311 ""","""
4312 static int __Pyx_TypeTest(PyObject *obj, PyTypeObject *type) {
4313 if (!type) {
4314 PyErr_Format(PyExc_SystemError, "Missing type object");
4315 return 0;
4317 if (obj == Py_None || PyObject_TypeCheck(obj, type))
4318 return 1;
4319 PyErr_Format(PyExc_TypeError, "Cannot convert %s to %s",
4320 Py_TYPE(obj)->tp_name, type->tp_name);
4321 return 0;
4323 """]
4325 #------------------------------------------------------------------------------------
4327 create_class_utility_code = [
4328 """
4329 static PyObject *__Pyx_CreateClass(PyObject *bases, PyObject *dict, PyObject *name, char *modname); /*proto*/
4330 ""","""
4331 static PyObject *__Pyx_CreateClass(
4332 PyObject *bases, PyObject *dict, PyObject *name, char *modname)
4334 PyObject *py_modname;
4335 PyObject *result = 0;
4337 #if PY_MAJOR_VERSION < 3
4338 py_modname = PyString_FromString(modname);
4339 #else
4340 py_modname = PyUnicode_FromString(modname);
4341 #endif
4342 if (!py_modname)
4343 goto bad;
4344 if (PyDict_SetItemString(dict, "__module__", py_modname) < 0)
4345 goto bad;
4346 #if PY_MAJOR_VERSION < 3
4347 result = PyClass_New(bases, dict, name);
4348 #else
4349 result = PyObject_CallFunctionObjArgs((PyObject *)&PyType_Type, name, bases, dict, NULL);
4350 #endif
4351 bad:
4352 Py_XDECREF(py_modname);
4353 return result;
4355 """]
4357 #------------------------------------------------------------------------------------
4359 cpp_exception_utility_code = [
4360 """
4361 #ifndef __Pyx_CppExn2PyErr
4362 static void __Pyx_CppExn2PyErr() {
4363 try {
4364 if (PyErr_Occurred())
4365 ; // let the latest Python exn pass through and ignore the current one
4366 else
4367 throw;
4368 } catch (const std::out_of_range& exn) {
4369 // catch out_of_range explicitly so the proper Python exn may be raised
4370 PyErr_SetString(PyExc_IndexError, exn.what());
4371 } catch (const std::exception& exn) {
4372 PyErr_SetString(PyExc_RuntimeError, exn.what());
4374 catch (...)
4376 PyErr_SetString(PyExc_RuntimeError, "Unknown exception");
4379 #endif
4380 """,""]
4382 #------------------------------------------------------------------------------------
4384 append_utility_code = [
4385 """
4386 static INLINE PyObject* __Pyx_PyObject_Append(PyObject* L, PyObject* x) {
4387 if (likely(PyList_CheckExact(L))) {
4388 if (PyList_Append(L, x) < 0) return NULL;
4389 Py_INCREF(Py_None);
4390 return Py_None; // this is just to have an accurate signature
4392 else {
4393 return PyObject_CallMethod(L, "append", "(O)", x);
4396 """,""
4399 #------------------------------------------------------------------------------------
4401 type_cache_invalidation_code = [
4402 """
4403 #if PY_VERSION_HEX >= 0x02060000
4404 /* #define __Pyx_TypeModified(t) PyType_Modified(t) */ /* Py3.0beta1 */
4405 static void __Pyx_TypeModified(PyTypeObject* type); /*proto*/
4406 #else
4407 #define __Pyx_TypeModified(t)
4408 #endif
4409 ""","""
4410 #if PY_VERSION_HEX >= 0x02060000
4411 /* copied from typeobject.c in Python 3.0a5 */
4412 static void __Pyx_TypeModified(PyTypeObject* type) {
4413 PyObject *raw, *ref;
4414 Py_ssize_t i, n;
4416 if (!PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG))
4417 return;
4419 raw = type->tp_subclasses;
4420 if (raw != NULL) {
4421 n = PyList_GET_SIZE(raw);
4422 for (i = 0; i < n; i++) {
4423 ref = PyList_GET_ITEM(raw, i);
4424 ref = PyWeakref_GET_OBJECT(ref);
4425 if (ref != Py_None) {
4426 __Pyx_TypeModified((PyTypeObject *)ref);
4430 type->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG;
4432 #endif
4433 """
4436 #------------------------------------------------------------------------------------
4438 # If the is_unsigned flag is set, we need to do some extra work to make
4439 # sure the index doesn't become negative.
4441 getitem_int_utility_code = [
4442 """
4443 static INLINE PyObject *__Pyx_GetItemInt(PyObject *o, Py_ssize_t i, int is_unsigned) {
4444 PyObject *r;
4445 if (PyList_CheckExact(o) && 0 <= i && i < PyList_GET_SIZE(o)) {
4446 r = PyList_GET_ITEM(o, i);
4447 Py_INCREF(r);
4449 else if (PyTuple_CheckExact(o) && 0 <= i && i < PyTuple_GET_SIZE(o)) {
4450 r = PyTuple_GET_ITEM(o, i);
4451 Py_INCREF(r);
4453 else if (Py_TYPE(o)->tp_as_sequence && Py_TYPE(o)->tp_as_sequence->sq_item && (likely(i >= 0) || !is_unsigned))
4454 r = PySequence_GetItem(o, i);
4455 else {
4456 PyObject *j = (likely(i >= 0) || !is_unsigned) ? PyInt_FromLong(i) : PyLong_FromUnsignedLongLong((sizeof(unsigned long long) > sizeof(Py_ssize_t) ? (1ULL << (sizeof(Py_ssize_t)*8)) : 0) + i);
4457 if (!j)
4458 return 0;
4459 r = PyObject_GetItem(o, j);
4460 Py_DECREF(j);
4462 return r;
4464 """,
4465 """
4466 """]
4468 #------------------------------------------------------------------------------------
4470 setitem_int_utility_code = [
4471 """
4472 static INLINE int __Pyx_SetItemInt(PyObject *o, Py_ssize_t i, PyObject *v, int is_unsigned) {
4473 int r;
4474 if (PyList_CheckExact(o) && 0 <= i && i < PyList_GET_SIZE(o)) {
4475 Py_DECREF(PyList_GET_ITEM(o, i));
4476 Py_INCREF(v);
4477 PyList_SET_ITEM(o, i, v);
4478 return 1;
4480 else if (Py_TYPE(o)->tp_as_sequence && Py_TYPE(o)->tp_as_sequence->sq_ass_item && (likely(i >= 0) || !is_unsigned))
4481 r = PySequence_SetItem(o, i, v);
4482 else {
4483 PyObject *j = (likely(i >= 0) || !is_unsigned) ? PyInt_FromLong(i) : PyLong_FromUnsignedLongLong((sizeof(unsigned long long) > sizeof(Py_ssize_t) ? (1ULL << (sizeof(Py_ssize_t)*8)) : 0) + i);
4484 if (!j)
4485 return -1;
4486 r = PyObject_SetItem(o, j, v);
4487 Py_DECREF(j);
4489 return r;
4491 """,
4492 """
4493 """]