Cython has moved to github.

cython-devel

view Cython/Compiler/ExprNodes.py @ 1270:f24f2963a581

Re: [Cython] PATCH: fix delitem for index nodes
author "Lisandro Dalcin" <dalcinl@gmail.com>
date Mon Oct 27 09:22:00 2008 -0700 (3 years ago)
parents d53e468baad4
children cedb01d56181
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 from Errors import hold_errors, release_errors, held_errors, report_error
10 from Cython.Utils import UtilityCode
11 import StringEncoding
12 import Naming
13 from Nodes import Node
14 import PyrexTypes
15 from PyrexTypes import py_object_type, c_long_type, typecast, error_type
16 from Builtin import list_type, tuple_type, dict_type, unicode_type
17 import Symtab
18 import Options
19 from Annotate import AnnotationItem
21 from Cython.Debugging import print_call_chain
22 from DebugFlags import debug_disposal_code, debug_temp_alloc, \
23 debug_coercion
26 class ExprNode(Node):
27 # subexprs [string] Class var holding names of subexpr node attrs
28 # type PyrexType Type of the result
29 # result_code string Code fragment
30 # result_ctype string C type of result_code if different from type
31 # is_temp boolean Result is in a temporary variable
32 # is_sequence_constructor
33 # boolean Is a list or tuple constructor expression
34 # saved_subexpr_nodes
35 # [ExprNode or [ExprNode or None] or None]
36 # Cached result of subexpr_nodes()
38 result_ctype = None
39 type = None
41 # The Analyse Expressions phase for expressions is split
42 # into two sub-phases:
43 #
44 # Analyse Types
45 # Determines the result type of the expression based
46 # on the types of its sub-expressions, and inserts
47 # coercion nodes into the expression tree where needed.
48 # Marks nodes which will need to have temporary variables
49 # allocated.
50 #
51 # Allocate Temps
52 # Allocates temporary variables where needed, and fills
53 # in the result_code field of each node.
54 #
55 # ExprNode provides some convenience routines which
56 # perform both of the above phases. These should only
57 # be called from statement nodes, and only when no
58 # coercion nodes need to be added around the expression
59 # being analysed. In that case, the above two phases
60 # should be invoked separately.
61 #
62 # Framework code in ExprNode provides much of the common
63 # processing for the various phases. It makes use of the
64 # 'subexprs' class attribute of ExprNodes, which should
65 # contain a list of the names of attributes which can
66 # hold sub-nodes or sequences of sub-nodes.
67 #
68 # The framework makes use of a number of abstract methods.
69 # Their responsibilities are as follows.
70 #
71 # Declaration Analysis phase
72 #
73 # analyse_target_declaration
74 # Called during the Analyse Declarations phase to analyse
75 # the LHS of an assignment or argument of a del statement.
76 # Nodes which cannot be the LHS of an assignment need not
77 # implement it.
78 #
79 # Expression Analysis phase
80 #
81 # analyse_types
82 # - Call analyse_types on all sub-expressions.
83 # - Check operand types, and wrap coercion nodes around
84 # sub-expressions where needed.
85 # - Set the type of this node.
86 # - If a temporary variable will be required for the
87 # result, set the is_temp flag of this node.
88 #
89 # analyse_target_types
90 # Called during the Analyse Types phase to analyse
91 # the LHS of an assignment or argument of a del
92 # statement. Similar responsibilities to analyse_types.
93 #
94 # allocate_temps
95 # - Call allocate_temps for all sub-nodes.
96 # - Call allocate_temp for this node.
97 # - If a temporary was allocated, call release_temp on
98 # all sub-expressions.
99 #
100 # allocate_target_temps
101 # - Call allocate_temps on sub-nodes and allocate any other
102 # temps used during assignment.
103 # - Fill in result_code with a C lvalue if needed.
104 # - If a rhs node is supplied, call release_temp on it.
105 # - Call release_temp on sub-nodes and release any other
106 # temps used during assignment.
107 #
108 # target_code
109 # Called by the default implementation of allocate_target_temps.
110 # Should return a C lvalue for assigning to the node. The default
111 # implementation calls calculate_result_code.
112 #
113 # check_const
114 # - Check that this node and its subnodes form a
115 # legal constant expression. If so, do nothing,
116 # otherwise call not_const.
117 #
118 # The default implementation of check_const
119 # assumes that the expression is not constant.
120 #
121 # check_const_addr
122 # - Same as check_const, except check that the
123 # expression is a C lvalue whose address is
124 # constant. Otherwise, call addr_not_const.
125 #
126 # The default implementation of calc_const_addr
127 # assumes that the expression is not a constant
128 # lvalue.
129 #
130 # Code Generation phase
131 #
132 # generate_evaluation_code
133 # - Call generate_evaluation_code for sub-expressions.
134 # - Perform the functions of generate_result_code
135 # (see below).
136 # - If result is temporary, call generate_disposal_code
137 # on all sub-expressions.
138 #
139 # A default implementation of generate_evaluation_code
140 # is provided which uses the following abstract methods:
141 #
142 # generate_result_code
143 # - Generate any C statements necessary to calculate
144 # the result of this node from the results of its
145 # sub-expressions.
146 #
147 # calculate_result_code
148 # - Should return a C code fragment evaluating to the
149 # result. This is only called when the result is not
150 # a temporary.
151 #
152 # generate_assignment_code
153 # Called on the LHS of an assignment.
154 # - Call generate_evaluation_code for sub-expressions.
155 # - Generate code to perform the assignment.
156 # - If the assignment absorbed a reference, call
157 # generate_post_assignment_code on the RHS,
158 # otherwise call generate_disposal_code on it.
159 #
160 # generate_deletion_code
161 # Called on an argument of a del statement.
162 # - Call generate_evaluation_code for sub-expressions.
163 # - Generate code to perform the deletion.
164 # - Call generate_disposal_code on all sub-expressions.
165 #
166 #
168 is_sequence_constructor = 0
169 is_attribute = 0
171 saved_subexpr_nodes = None
172 is_temp = 0
173 is_target = 0
175 def get_child_attrs(self):
176 return self.subexprs
177 child_attrs = property(fget=get_child_attrs)
179 def not_implemented(self, method_name):
180 print_call_chain(method_name, "not implemented") ###
181 raise InternalError(
182 "%s.%s not implemented" %
183 (self.__class__.__name__, method_name))
185 def is_lvalue(self):
186 return 0
188 def is_ephemeral(self):
189 # An ephemeral node is one whose result is in
190 # a Python temporary and we suspect there are no
191 # other references to it. Certain operations are
192 # disallowed on such values, since they are
193 # likely to result in a dangling pointer.
194 return self.type.is_pyobject and self.is_temp
196 def subexpr_nodes(self):
197 # Extract a list of subexpression nodes based
198 # on the contents of the subexprs class attribute.
199 if self.saved_subexpr_nodes is None:
200 nodes = []
201 for name in self.subexprs:
202 item = getattr(self, name)
203 if item:
204 if isinstance(item, ExprNode):
205 nodes.append(item)
206 else:
207 nodes.extend(item)
208 self.saved_subexpr_nodes = nodes
209 return self.saved_subexpr_nodes
211 def result(self):
212 if not self.is_temp or self.is_target:
213 return self.calculate_result_code()
214 else: # i.e. self.is_temp:
215 return self.result_code
217 def result_as(self, type = None):
218 # Return the result code cast to the specified C type.
219 return typecast(type, self.ctype(), self.result())
221 def py_result(self):
222 # Return the result code cast to PyObject *.
223 return self.result_as(py_object_type)
225 def ctype(self):
226 # Return the native C type of the result (i.e. the
227 # C type of the result_code expression).
228 return self.result_ctype or self.type
230 def compile_time_value(self, denv):
231 # Return value of compile-time expression, or report error.
232 error(self.pos, "Invalid compile-time expression")
234 def compile_time_value_error(self, e):
235 error(self.pos, "Error in compile-time expression: %s: %s" % (
236 e.__class__.__name__, e))
238 # ------------- Declaration Analysis ----------------
240 def analyse_target_declaration(self, env):
241 error(self.pos, "Cannot assign to or delete this")
243 # ------------- Expression Analysis ----------------
245 def analyse_const_expression(self, env):
246 # Called during the analyse_declarations phase of a
247 # constant expression. Analyses the expression's type,
248 # checks whether it is a legal const expression,
249 # and determines its value.
250 self.analyse_types(env)
251 self.allocate_temps(env)
252 self.check_const()
254 def analyse_expressions(self, env):
255 # Convenience routine performing both the Type
256 # Analysis and Temp Allocation phases for a whole
257 # expression.
258 self.analyse_types(env)
259 self.allocate_temps(env)
261 def analyse_target_expression(self, env, rhs):
262 # Convenience routine performing both the Type
263 # Analysis and Temp Allocation phases for the LHS of
264 # an assignment.
265 self.analyse_target_types(env)
266 self.allocate_target_temps(env, rhs)
268 def analyse_boolean_expression(self, env):
269 # Analyse expression and coerce to a boolean.
270 self.analyse_types(env)
271 bool = self.coerce_to_boolean(env)
272 bool.allocate_temps(env)
273 return bool
275 def analyse_temp_boolean_expression(self, env):
276 # Analyse boolean expression and coerce result into
277 # a temporary. This is used when a branch is to be
278 # performed on the result and we won't have an
279 # opportunity to ensure disposal code is executed
280 # afterwards. By forcing the result into a temporary,
281 # we ensure that all disposal has been done by the
282 # time we get the result.
283 self.analyse_types(env)
284 bool = self.coerce_to_boolean(env)
285 temp_bool = bool.coerce_to_temp(env)
286 temp_bool.allocate_temps(env)
287 return temp_bool
289 # --------------- Type Analysis ------------------
291 def analyse_as_module(self, env):
292 # If this node can be interpreted as a reference to a
293 # cimported module, return its scope, else None.
294 return None
296 def analyse_as_type(self, env):
297 # If this node can be interpreted as a reference to a
298 # type, return that type, else None.
299 return None
301 def analyse_as_extension_type(self, env):
302 # If this node can be interpreted as a reference to an
303 # extension type, return its type, else None.
304 return None
306 def analyse_types(self, env):
307 self.not_implemented("analyse_types")
309 def analyse_target_types(self, env):
310 self.analyse_types(env)
312 def gil_assignment_check(self, env):
313 if env.nogil and self.type.is_pyobject:
314 error(self.pos, "Assignment of Python object not allowed without gil")
316 def check_const(self):
317 self.not_const()
319 def not_const(self):
320 error(self.pos, "Not allowed in a constant expression")
322 def check_const_addr(self):
323 self.addr_not_const()
325 def addr_not_const(self):
326 error(self.pos, "Address is not constant")
328 def gil_check(self, env):
329 if env.nogil and self.type.is_pyobject:
330 self.gil_error()
332 # ----------------- Result Allocation -----------------
334 def result_in_temp(self):
335 # Return true if result is in a temporary owned by
336 # this node or one of its subexpressions. Overridden
337 # by certain nodes which can share the result of
338 # a subnode.
339 return self.is_temp
341 def allocate_target_temps(self, env, rhs):
342 # Perform temp allocation for the LHS of an assignment.
343 if debug_temp_alloc:
344 print("%s Allocating target temps" % self)
345 self.allocate_subexpr_temps(env)
346 self.is_target = True
347 if rhs:
348 rhs.release_temp(env)
349 self.release_subexpr_temps(env)
351 def allocate_temps(self, env, result = None):
352 # Allocate temporary variables for this node and
353 # all its sub-expressions. If a result is specified,
354 # this must be a temp node and the specified variable
355 # is used as the result instead of allocating a new
356 # one.
357 if debug_temp_alloc:
358 print("%s Allocating temps" % self)
359 self.allocate_subexpr_temps(env)
360 self.allocate_temp(env, result)
361 if self.is_temp:
362 self.release_subexpr_temps(env)
364 def allocate_subexpr_temps(self, env):
365 # Allocate temporary variables for all sub-expressions
366 # of this node.
367 if debug_temp_alloc:
368 print("%s Allocating temps for: %s" % (self, self.subexprs))
369 for node in self.subexpr_nodes():
370 if node:
371 if debug_temp_alloc:
372 print("%s Allocating temps for %s" % (self, node))
373 node.allocate_temps(env)
375 def allocate_temp(self, env, result = None):
376 # If this node requires a temporary variable for its
377 # result, allocate one, otherwise set the result to
378 # a C code fragment. If a result is specified,
379 # this must be a temp node and the specified variable
380 # is used as the result instead of allocating a new
381 # one.
382 if debug_temp_alloc:
383 print("%s Allocating temp" % self)
384 if result:
385 if not self.is_temp:
386 raise InternalError("Result forced on non-temp node")
387 self.result_code = result
388 elif self.is_temp:
389 type = self.type
390 if not type.is_void:
391 if type.is_pyobject:
392 type = PyrexTypes.py_object_type
393 self.result_code = env.allocate_temp(type)
394 else:
395 self.result_code = None
396 if debug_temp_alloc:
397 print("%s Allocated result %s" % (self, self.result_code))
399 def target_code(self):
400 # Return code fragment for use as LHS of a C assignment.
401 return self.calculate_result_code()
403 def calculate_result_code(self):
404 self.not_implemented("calculate_result_code")
406 # def release_target_temp(self, env):
407 # # Release temporaries used by LHS of an assignment.
408 # self.release_subexpr_temps(env)
410 def release_temp(self, env):
411 # If this node owns a temporary result, release it,
412 # otherwise release results of its sub-expressions.
413 if self.is_temp:
414 if debug_temp_alloc:
415 print("%s Releasing result %s" % (self, self.result_code))
416 env.release_temp(self.result_code)
417 else:
418 self.release_subexpr_temps(env)
420 def release_subexpr_temps(self, env):
421 # Release the results of all sub-expressions of
422 # this node.
423 for node in self.subexpr_nodes():
424 if node:
425 node.release_temp(env)
427 # ---------------- Code Generation -----------------
429 def make_owned_reference(self, code):
430 # If result is a pyobject, make sure we own
431 # a reference to it.
432 if self.type.is_pyobject and not self.result_in_temp():
433 code.put_incref(self.result(), self.ctype())
435 def generate_evaluation_code(self, code):
436 code.mark_pos(self.pos)
437 # Generate code to evaluate this node and
438 # its sub-expressions, and dispose of any
439 # temporary results of its sub-expressions.
440 self.generate_subexpr_evaluation_code(code)
441 self.pre_generate_result_code(code)
442 self.generate_result_code(code)
443 if self.is_temp:
444 self.generate_subexpr_disposal_code(code)
446 def pre_generate_result_code(self, code):
447 pass
449 def generate_subexpr_evaluation_code(self, code):
450 for node in self.subexpr_nodes():
451 node.generate_evaluation_code(code)
453 def generate_result_code(self, code):
454 self.not_implemented("generate_result_code")
456 def generate_disposal_code(self, code):
457 # If necessary, generate code to dispose of
458 # temporary Python reference.
459 if self.is_temp:
460 if self.type.is_pyobject:
461 code.put_decref_clear(self.result(), self.ctype())
462 else:
463 self.generate_subexpr_disposal_code(code)
465 def generate_subexpr_disposal_code(self, code):
466 # Generate code to dispose of temporary results
467 # of all sub-expressions.
468 for node in self.subexpr_nodes():
469 node.generate_disposal_code(code)
471 def generate_post_assignment_code(self, code):
472 # Same as generate_disposal_code except that
473 # assignment will have absorbed a reference to
474 # the result if it is a Python object.
475 if self.is_temp:
476 if self.type.is_pyobject:
477 code.putln("%s = 0;" % self.result())
478 else:
479 self.generate_subexpr_disposal_code(code)
481 def generate_assignment_code(self, rhs, code):
482 # Stub method for nodes which are not legal as
483 # the LHS of an assignment. An error will have
484 # been reported earlier.
485 pass
487 def generate_deletion_code(self, code):
488 # Stub method for nodes that are not legal as
489 # the argument of a del statement. An error
490 # will have been reported earlier.
491 pass
493 # ---------------- Annotation ---------------------
495 def annotate(self, code):
496 for node in self.subexpr_nodes():
497 node.annotate(code)
499 # ----------------- Coercion ----------------------
501 def coerce_to(self, dst_type, env):
502 # Coerce the result so that it can be assigned to
503 # something of type dst_type. If processing is necessary,
504 # wraps this node in a coercion node and returns that.
505 # Otherwise, returns this node unchanged.
506 #
507 # This method is called during the analyse_expressions
508 # phase of the src_node's processing.
509 src = self
510 src_type = self.type
511 src_is_py_type = src_type.is_pyobject
512 dst_is_py_type = dst_type.is_pyobject
514 if dst_type.is_pyobject:
515 if not src.type.is_pyobject:
516 src = CoerceToPyTypeNode(src, env)
517 if not src.type.subtype_of(dst_type):
518 if not isinstance(src, NoneNode):
519 src = PyTypeTestNode(src, dst_type, env)
520 elif src.type.is_pyobject:
521 src = CoerceFromPyTypeNode(dst_type, src, env)
522 else: # neither src nor dst are py types
523 # Added the string comparison, since for c types that
524 # is enough, but Cython gets confused when the types are
525 # in different files.
526 if not (str(src.type) == str(dst_type) or dst_type.assignable_from(src_type)):
527 error(self.pos, "Cannot assign type '%s' to '%s'" %
528 (src.type, dst_type))
529 return src
531 def coerce_to_pyobject(self, env):
532 return self.coerce_to(PyrexTypes.py_object_type, env)
534 def coerce_to_boolean(self, env):
535 # Coerce result to something acceptable as
536 # a boolean value.
537 type = self.type
538 if type.is_pyobject or type.is_ptr or type.is_float:
539 return CoerceToBooleanNode(self, env)
540 else:
541 if not type.is_int and not type.is_error:
542 error(self.pos,
543 "Type '%s' not acceptable as a boolean" % type)
544 return self
546 def coerce_to_integer(self, env):
547 # If not already some C integer type, coerce to longint.
548 if self.type.is_int:
549 return self
550 else:
551 return self.coerce_to(PyrexTypes.c_long_type, env)
553 def coerce_to_temp(self, env):
554 # Ensure that the result is in a temporary.
555 if self.result_in_temp():
556 return self
557 else:
558 return CoerceToTempNode(self, env)
560 def coerce_to_simple(self, env):
561 # Ensure that the result is simple (see is_simple).
562 if self.is_simple():
563 return self
564 else:
565 return self.coerce_to_temp(env)
567 def is_simple(self):
568 # A node is simple if its result is something that can
569 # be referred to without performing any operations, e.g.
570 # a constant, local var, C global var, struct member
571 # reference, or temporary.
572 return self.result_in_temp()
574 def as_cython_attribute(self):
575 return None
578 class NewTempExprNode(ExprNode):
579 backwards_compatible_result = None
581 def result(self):
582 if self.is_temp:
583 return self.temp_code
584 else:
585 return self.calculate_result_code()
587 def allocate_target_temps(self, env, rhs):
588 self.allocate_subexpr_temps(env)
589 rhs.release_temp(rhs)
590 self.release_subexpr_temps(env)
592 def allocate_temps(self, env, result = None):
593 self.allocate_subexpr_temps(env)
594 self.backwards_compatible_result = result
595 if self.is_temp:
596 self.release_subexpr_temps(env)
598 def allocate_temp(self, env, result = None):
599 assert result is None
601 def release_temp(self, env):
602 pass
604 def pre_generate_result_code(self, code):
605 if self.is_temp:
606 type = self.type
607 if not type.is_void:
608 if type.is_pyobject:
609 type = PyrexTypes.py_object_type
610 if self.backwards_compatible_result:
611 self.temp_code = self.backwards_compatible_result
612 else:
613 self.temp_code = code.funcstate.allocate_temp(type)
614 else:
615 self.temp_code = None
617 def generate_disposal_code(self, code):
618 if self.is_temp:
619 if self.type.is_pyobject:
620 code.put_decref_clear(self.result(), self.ctype())
621 if not self.backwards_compatible_result:
622 code.funcstate.release_temp(self.temp_code)
623 else:
624 self.generate_subexpr_disposal_code(code)
626 def generate_post_assignment_code(self, code):
627 if self.is_temp:
628 if self.type.is_pyobject:
629 code.putln("%s = 0;" % self.temp_code)
630 if not self.backwards_compatible_result:
631 code.funcstate.release_temp(self.temp_code)
632 else:
633 self.generate_subexpr_disposal_code(code)
638 class AtomicExprNode(ExprNode):
639 # Abstract base class for expression nodes which have
640 # no sub-expressions.
642 subexprs = []
645 class PyConstNode(AtomicExprNode):
646 # Abstract base class for constant Python values.
648 is_literal = 1
650 def is_simple(self):
651 return 1
653 def analyse_types(self, env):
654 self.type = py_object_type
656 def calculate_result_code(self):
657 return self.value
659 def generate_result_code(self, code):
660 pass
663 class NoneNode(PyConstNode):
664 # The constant value None
666 value = "Py_None"
668 def compile_time_value(self, denv):
669 return None
671 class EllipsisNode(PyConstNode):
672 # '...' in a subscript list.
674 value = "Py_Ellipsis"
676 def compile_time_value(self, denv):
677 return Ellipsis
680 class ConstNode(AtomicExprNode):
681 # Abstract base type for literal constant nodes.
682 #
683 # value string C code fragment
685 is_literal = 1
687 def is_simple(self):
688 return 1
690 def analyse_types(self, env):
691 pass # Types are held in class variables
693 def check_const(self):
694 pass
696 def calculate_result_code(self):
697 return str(self.value)
699 def generate_result_code(self, code):
700 pass
703 class BoolNode(ConstNode):
704 type = PyrexTypes.c_bint_type
705 # The constant value True or False
707 def compile_time_value(self, denv):
708 return self.value
710 def calculate_result_code(self):
711 return str(int(self.value))
713 class NullNode(ConstNode):
714 type = PyrexTypes.c_null_ptr_type
715 value = "NULL"
718 class CharNode(ConstNode):
719 type = PyrexTypes.c_char_type
721 def compile_time_value(self, denv):
722 return ord(self.value)
724 def calculate_result_code(self):
725 return "'%s'" % StringEncoding.escape_character(self.value)
728 class IntNode(ConstNode):
730 # unsigned "" or "U"
731 # longness "" or "L" or "LL"
733 unsigned = ""
734 longness = ""
735 type = PyrexTypes.c_long_type
737 def coerce_to(self, dst_type, env):
738 if dst_type.is_numeric:
739 self.type = PyrexTypes.c_long_type
740 return self
741 # Arrange for a Python version of the number to be pre-allocated
742 # when coercing to a Python type.
743 if dst_type.is_pyobject:
744 self.entry = env.get_py_num(self.value, self.longness)
745 self.type = PyrexTypes.py_object_type
746 # We still need to perform normal coerce_to processing on the
747 # result, because we might be coercing to an extension type,
748 # in which case a type test node will be needed.
749 return ConstNode.coerce_to(self, dst_type, env)
751 def coerce_to_boolean(self, env):
752 self.type = PyrexTypes.c_bint_type
753 return self
755 def calculate_result_code(self):
756 if self.type.is_pyobject:
757 return self.entry.cname
758 else:
759 return str(self.value) + self.unsigned + self.longness
761 def compile_time_value(self, denv):
762 return int(self.value, 0)
765 class FloatNode(ConstNode):
766 type = PyrexTypes.c_double_type
768 def compile_time_value(self, denv):
769 return float(self.value)
771 def calculate_result_code(self):
772 strval = str(self.value)
773 if strval == 'nan':
774 return "(Py_HUGE_VAL * 0)"
775 elif strval == 'inf':
776 return "Py_HUGE_VAL"
777 elif strval == '-inf':
778 return "(-Py_HUGE_VAL)"
779 else:
780 return strval
783 class StringNode(ConstNode):
784 # entry Symtab.Entry
786 type = PyrexTypes.c_char_ptr_type
788 def compile_time_value(self, denv):
789 return self.value
791 def analyse_types(self, env):
792 self.entry = env.add_string_const(self.value)
794 def analyse_as_type(self, env):
795 type = PyrexTypes.parse_basic_type(self.value)
796 if type is not None:
797 return type
798 from TreeFragment import TreeFragment
799 pos = (self.pos[0], self.pos[1], self.pos[2]-7)
800 declaration = TreeFragment(u"sizeof(%s)" % self.value, name=pos[0].filename, initial_pos=pos)
801 sizeof_node = declaration.root.stats[0].expr
802 sizeof_node.analyse_types(env)
803 if isinstance(sizeof_node, SizeofTypeNode):
804 return sizeof_node.arg_type
806 def coerce_to(self, dst_type, env):
807 if dst_type == PyrexTypes.c_char_ptr_type:
808 self.type = PyrexTypes.c_char_ptr_type
809 return self
811 if dst_type.is_int:
812 if not self.type.is_pyobject and len(self.entry.init) == 1:
813 return CharNode(self.pos, value=self.value)
814 else:
815 error(self.pos, "Only coerce single-character ascii strings can be used as ints.")
816 return self
817 # Arrange for a Python version of the string to be pre-allocated
818 # when coercing to a Python type.
819 if dst_type.is_pyobject and not self.type.is_pyobject:
820 node = self.as_py_string_node(env)
821 else:
822 node = self
823 # We still need to perform normal coerce_to processing on the
824 # result, because we might be coercing to an extension type,
825 # in which case a type test node will be needed.
826 return ConstNode.coerce_to(node, dst_type, env)
828 def as_py_string_node(self, env):
829 # Return a new StringNode with the same entry as this node
830 # but whose type is a Python type instead of a C type.
831 entry = self.entry
832 env.add_py_string(entry)
833 return StringNode(self.pos, value = self.value, entry = entry, type = py_object_type)
835 def calculate_result_code(self):
836 if self.type.is_pyobject:
837 return self.entry.pystring_cname
838 else:
839 return self.entry.cname
842 class UnicodeNode(PyConstNode):
843 # entry Symtab.Entry
845 type = unicode_type
847 def analyse_types(self, env):
848 self.entry = env.add_string_const(self.value)
849 env.add_py_string(self.entry)
851 def calculate_result_code(self):
852 return self.entry.pystring_cname
854 def _coerce_to(self, dst_type, env):
855 if not dst_type.is_pyobject:
856 node = StringNode(self.pos, entry = entry, type = py_object_type)
857 return ConstNode.coerce_to(node, dst_type, env)
858 else:
859 return self
860 # We still need to perform normal coerce_to processing on the
861 # result, because we might be coercing to an extension type,
862 # in which case a type test node will be needed.
864 def compile_time_value(self, env):
865 return self.value
868 class IdentifierStringNode(ConstNode):
869 # A Python string that behaves like an identifier, e.g. for
870 # keyword arguments in a call, or for imported names
871 type = PyrexTypes.py_object_type
873 def analyse_types(self, env):
874 self.cname = env.intern_identifier(self.value)
876 def calculate_result_code(self):
877 return self.cname
880 class LongNode(AtomicExprNode):
881 # Python long integer literal
882 #
883 # value string
885 def compile_time_value(self, denv):
886 return long(self.value)
888 gil_message = "Constructing Python long int"
890 def analyse_types(self, env):
891 self.type = py_object_type
892 self.gil_check(env)
893 self.is_temp = 1
895 gil_message = "Constructing Python long int"
897 def generate_evaluation_code(self, code):
898 code.putln(
899 '%s = PyLong_FromString("%s", 0, 0); %s' % (
900 self.result(),
901 self.value,
902 code.error_goto_if_null(self.result(), self.pos)))
905 class ImagNode(AtomicExprNode):
906 # Imaginary number literal
907 #
908 # value float imaginary part
910 def compile_time_value(self, denv):
911 return complex(0.0, self.value)
913 def analyse_types(self, env):
914 self.type = py_object_type
915 self.gil_check(env)
916 self.is_temp = 1
918 gil_message = "Constructing complex number"
920 def generate_evaluation_code(self, code):
921 code.putln(
922 "%s = PyComplex_FromDoubles(0.0, %s); %s" % (
923 self.result(),
924 self.value,
925 code.error_goto_if_null(self.result(), self.pos)))
928 class NameNode(AtomicExprNode):
929 # Reference to a local or global variable name.
930 #
931 # name string Python name of the variable
932 #
933 # entry Entry Symbol table entry
934 # interned_cname string
936 is_name = True
937 is_cython_module = False
938 cython_attribute = None
939 lhs_of_first_assignment = False
940 entry = None
942 def create_analysed_rvalue(pos, env, entry):
943 node = NameNode(pos)
944 node.analyse_types(env, entry=entry)
945 return node
947 def as_cython_attribute(self):
948 return self.cython_attribute
950 create_analysed_rvalue = staticmethod(create_analysed_rvalue)
952 def compile_time_value(self, denv):
953 try:
954 return denv.lookup(self.name)
955 except KeyError:
956 error(self.pos, "Compile-time name '%s' not defined" % self.name)
958 def coerce_to(self, dst_type, env):
959 # If coercing to a generic pyobject and this is a builtin
960 # C function with a Python equivalent, manufacture a NameNode
961 # referring to the Python builtin.
962 #print "NameNode.coerce_to:", self.name, dst_type ###
963 if dst_type is py_object_type:
964 entry = self.entry
965 if entry and entry.is_cfunction:
966 var_entry = entry.as_variable
967 if var_entry:
968 if var_entry.is_builtin and Options.cache_builtins:
969 var_entry = env.declare_builtin(var_entry.name, self.pos)
970 node = NameNode(self.pos, name = self.name)
971 node.entry = var_entry
972 node.analyse_rvalue_entry(env)
973 return node
974 return AtomicExprNode.coerce_to(self, dst_type, env)
976 def analyse_as_module(self, env):
977 # Try to interpret this as a reference to a cimported module.
978 # Returns the module scope, or None.
979 entry = self.entry
980 if not entry:
981 entry = env.lookup(self.name)
982 if entry and entry.as_module:
983 return entry.as_module
984 return None
986 def analyse_as_type(self, env):
987 if self.cython_attribute:
988 type = PyrexTypes.parse_basic_type(self.cython_attribute)
989 else:
990 type = PyrexTypes.parse_basic_type(self.name)
991 if type:
992 return type
993 entry = self.entry
994 if not entry:
995 entry = env.lookup(self.name)
996 if entry and entry.is_type:
997 return entry.type
998 else:
999 return None
1001 def analyse_as_extension_type(self, env):
1002 # Try to interpret this as a reference to an extension type.
1003 # Returns the extension type, or None.
1004 entry = self.entry
1005 if not entry:
1006 entry = env.lookup(self.name)
1007 if entry and entry.is_type and entry.type.is_extension_type:
1008 return entry.type
1009 else:
1010 return None
1012 def analyse_target_declaration(self, env):
1013 if not self.entry:
1014 self.entry = env.lookup_here(self.name)
1015 if not self.entry:
1016 self.entry = env.declare_var(self.name, py_object_type, self.pos)
1017 env.control_flow.set_state(self.pos, (self.name, 'initalized'), True)
1018 env.control_flow.set_state(self.pos, (self.name, 'source'), 'assignment')
1019 if self.entry.is_declared_generic:
1020 self.result_ctype = py_object_type
1022 def analyse_types(self, env):
1023 if self.entry is None:
1024 self.entry = env.lookup(self.name)
1025 if not self.entry:
1026 self.entry = env.declare_builtin(self.name, self.pos)
1027 if not self.entry:
1028 self.type = PyrexTypes.error_type
1029 return
1030 self.analyse_rvalue_entry(env)
1032 def analyse_target_types(self, env):
1033 self.analyse_entry(env)
1034 if not self.is_lvalue():
1035 error(self.pos, "Assignment to non-lvalue '%s'"
1036 % self.name)
1037 self.type = PyrexTypes.error_type
1038 self.entry.used = 1
1039 if self.entry.type.is_buffer:
1040 import Buffer
1041 Buffer.used_buffer_aux_vars(self.entry)
1043 def analyse_rvalue_entry(self, env):
1044 #print "NameNode.analyse_rvalue_entry:", self.name ###
1045 #print "Entry:", self.entry.__dict__ ###
1046 self.analyse_entry(env)
1047 entry = self.entry
1048 if entry.is_declared_generic:
1049 self.result_ctype = py_object_type
1050 if entry.is_pyglobal or entry.is_builtin:
1051 if Options.cache_builtins and entry.is_builtin:
1052 self.is_temp = 0
1053 else:
1054 self.is_temp = 1
1055 env.use_utility_code(get_name_interned_utility_code)
1056 self.gil_check(env)
1058 gil_message = "Accessing Python global or builtin"
1060 def analyse_entry(self, env):
1061 #print "NameNode.analyse_entry:", self.name ###
1062 self.check_identifier_kind()
1063 entry = self.entry
1064 type = entry.type
1065 self.type = type
1066 if entry.is_pyglobal or entry.is_builtin:
1067 assert type.is_pyobject, "Python global or builtin not a Python object"
1068 self.interned_cname = self.entry.interned_cname = \
1069 env.intern_identifier(self.entry.name)
1071 def check_identifier_kind(self):
1072 #print "NameNode.check_identifier_kind:", self.entry.name ###
1073 #print self.entry.__dict__ ###
1074 entry = self.entry
1075 #entry.used = 1
1076 if not (entry.is_const or entry.is_variable
1077 or entry.is_builtin or entry.is_cfunction):
1078 if self.entry.as_variable:
1079 self.entry = self.entry.as_variable
1080 else:
1081 error(self.pos,
1082 "'%s' is not a constant, variable or function identifier" % self.name)
1084 def is_simple(self):
1085 # If it's not a C variable, it'll be in a temp.
1086 return 1
1088 def calculate_target_results(self, env):
1089 pass
1091 def check_const(self):
1092 entry = self.entry
1093 if entry is not None and not (entry.is_const or entry.is_cfunction or entry.is_builtin):
1094 self.not_const()
1096 def check_const_addr(self):
1097 entry = self.entry
1098 if not (entry.is_cglobal or entry.is_cfunction or entry.is_builtin):
1099 self.addr_not_const()
1101 def is_lvalue(self):
1102 return self.entry.is_variable and \
1103 not self.entry.type.is_array and \
1104 not self.entry.is_readonly
1106 def is_ephemeral(self):
1107 # Name nodes are never ephemeral, even if the
1108 # result is in a temporary.
1109 return 0
1111 def allocate_temp(self, env, result = None):
1112 AtomicExprNode.allocate_temp(self, env, result)
1113 entry = self.entry
1114 if entry:
1115 entry.used = 1
1116 if entry.type.is_buffer:
1117 import Buffer
1118 Buffer.used_buffer_aux_vars(entry)
1119 if entry.utility_code:
1120 env.use_utility_code(entry.utility_code)
1122 def calculate_result_code(self):
1123 entry = self.entry
1124 if not entry:
1125 return "<error>" # There was an error earlier
1126 return entry.cname
1128 def generate_result_code(self, code):
1129 assert hasattr(self, 'entry')
1130 entry = self.entry
1131 if entry is None:
1132 return # There was an error earlier
1133 if entry.is_builtin and Options.cache_builtins:
1134 return # Lookup already cached
1135 elif entry.is_pyglobal or entry.is_builtin:
1136 if entry.is_builtin:
1137 namespace = Naming.builtins_cname
1138 else: # entry.is_pyglobal
1139 namespace = entry.scope.namespace_cname
1140 code.putln(
1141 '%s = __Pyx_GetName(%s, %s); %s' % (
1142 self.result(),
1143 namespace,
1144 self.interned_cname,
1145 code.error_goto_if_null(self.result(), self.pos)))
1146 elif entry.is_local and False:
1147 # control flow not good enough yet
1148 assigned = entry.scope.control_flow.get_state((entry.name, 'initalized'), self.pos)
1149 if assigned is False:
1150 error(self.pos, "local variable '%s' referenced before assignment" % entry.name)
1151 elif not Options.init_local_none and assigned is None:
1152 code.putln('if (%s == 0) { PyErr_SetString(PyExc_UnboundLocalError, "%s"); %s }' % (entry.cname, entry.name, code.error_goto(self.pos)))
1153 entry.scope.control_flow.set_state(self.pos, (entry.name, 'initalized'), True)
1155 def generate_assignment_code(self, rhs, code):
1156 #print "NameNode.generate_assignment_code:", self.name ###
1157 entry = self.entry
1158 if entry is None:
1159 return # There was an error earlier
1161 if (self.entry.type.is_ptr and isinstance(rhs, ListNode)
1162 and not self.lhs_of_first_assignment):
1163 error(self.pos, "Literal list must be assigned to pointer at time of declaration")
1165 # is_pyglobal seems to be True for module level-globals only.
1166 # We use this to access class->tp_dict if necessary.
1167 if entry.is_pyglobal:
1168 namespace = self.entry.scope.namespace_cname
1169 if entry.is_member:
1170 # if the entry is a member we have to cheat: SetAttr does not work
1171 # on types, so we create a descriptor which is then added to tp_dict
1172 code.put_error_if_neg(self.pos,
1173 'PyDict_SetItem(%s->tp_dict, %s, %s)' % (
1174 namespace,
1175 self.interned_cname,
1176 rhs.py_result()))
1177 # in Py2.6+, we need to invalidate the method cache
1178 code.putln("PyType_Modified(%s);" %
1179 entry.scope.parent_type.typeptr_cname)
1180 else:
1181 code.put_error_if_neg(self.pos,
1182 'PyObject_SetAttr(%s, %s, %s)' % (
1183 namespace,
1184 self.interned_cname,
1185 rhs.py_result()))
1186 if debug_disposal_code:
1187 print("NameNode.generate_assignment_code:")
1188 print("...generating disposal code for %s" % rhs)
1189 rhs.generate_disposal_code(code)
1191 else:
1192 if self.type.is_buffer:
1193 # Generate code for doing the buffer release/acquisition.
1194 # This might raise an exception in which case the assignment (done
1195 # below) will not happen.
1197 # The reason this is not in a typetest-like node is because the
1198 # variables that the acquired buffer info is stored to is allocated
1199 # per entry and coupled with it.
1200 self.generate_acquire_buffer(rhs, code)
1202 if self.type.is_pyobject:
1203 rhs.make_owned_reference(code)
1204 #print "NameNode.generate_assignment_code: to", self.name ###
1205 #print "...from", rhs ###
1206 #print "...LHS type", self.type, "ctype", self.ctype() ###
1207 #print "...RHS type", rhs.type, "ctype", rhs.ctype() ###
1208 if not self.lhs_of_first_assignment:
1209 if entry.is_local and not Options.init_local_none:
1210 initalized = entry.scope.control_flow.get_state((entry.name, 'initalized'), self.pos)
1211 if initalized is True:
1212 code.put_decref(self.result(), self.ctype())
1213 elif initalized is None:
1214 code.put_xdecref(self.result(), self.ctype())
1215 else:
1216 code.put_decref(self.result(), self.ctype())
1217 code.putln('%s = %s;' % (self.result(), rhs.result_as(self.ctype())))
1218 if debug_disposal_code:
1219 print("NameNode.generate_assignment_code:")
1220 print("...generating post-assignment code for %s" % rhs)
1221 rhs.generate_post_assignment_code(code)
1223 def generate_acquire_buffer(self, rhs, code):
1224 rhstmp = code.funcstate.allocate_temp(self.entry.type)
1225 buffer_aux = self.entry.buffer_aux
1226 bufstruct = buffer_aux.buffer_info_var.cname
1227 code.putln('%s = %s;' % (rhstmp, rhs.result_as(self.ctype())))
1229 import Buffer
1230 Buffer.put_assign_to_buffer(self.result(), rhstmp, buffer_aux, self.entry.type,
1231 is_initialized=not self.lhs_of_first_assignment,
1232 pos=self.pos, code=code)
1233 code.putln("%s = 0;" % rhstmp)
1234 code.funcstate.release_temp(rhstmp)
1236 def generate_deletion_code(self, code):
1237 if self.entry is None:
1238 return # There was an error earlier
1239 if not self.entry.is_pyglobal:
1240 error(self.pos, "Deletion of local or C global name not supported")
1241 return
1242 code.put_error_if_neg(self.pos,
1243 'PyObject_DelAttrString(%s, "%s")' % (
1244 Naming.module_cname,
1245 self.entry.name))
1247 def annotate(self, code):
1248 if hasattr(self, 'is_called') and self.is_called:
1249 pos = (self.pos[0], self.pos[1], self.pos[2] - len(self.name) - 1)
1250 if self.type.is_pyobject:
1251 code.annotate(pos, AnnotationItem('py_call', 'python function', size=len(self.name)))
1252 else:
1253 code.annotate(pos, AnnotationItem('c_call', 'c function', size=len(self.name)))
1255 class BackquoteNode(ExprNode):
1256 # `expr`
1258 # arg ExprNode
1260 subexprs = ['arg']
1262 def analyse_types(self, env):
1263 self.arg.analyse_types(env)
1264 self.arg = self.arg.coerce_to_pyobject(env)
1265 self.type = py_object_type
1266 self.gil_check(env)
1267 self.is_temp = 1
1269 gil_message = "Backquote expression"
1271 def generate_result_code(self, code):
1272 code.putln(
1273 "%s = PyObject_Repr(%s); %s" % (
1274 self.result(),
1275 self.arg.py_result(),
1276 code.error_goto_if_null(self.result(), self.pos)))
1279 class ImportNode(ExprNode):
1280 # Used as part of import statement implementation.
1281 # Implements result =
1282 # __import__(module_name, globals(), None, name_list)
1284 # module_name IdentifierStringNode dotted name of module
1285 # name_list ListNode or None list of names to be imported
1287 subexprs = ['module_name', 'name_list']
1289 def analyse_types(self, env):
1290 self.module_name.analyse_types(env)
1291 self.module_name = self.module_name.coerce_to_pyobject(env)
1292 if self.name_list:
1293 self.name_list.analyse_types(env)
1294 self.name_list.coerce_to_pyobject(env)
1295 self.type = py_object_type
1296 self.gil_check(env)
1297 self.is_temp = 1
1298 env.use_utility_code(import_utility_code)
1300 gil_message = "Python import"
1302 def generate_result_code(self, code):
1303 if self.name_list:
1304 name_list_code = self.name_list.py_result()
1305 else:
1306 name_list_code = "0"
1307 code.putln(
1308 "%s = __Pyx_Import(%s, %s); %s" % (
1309 self.result(),
1310 self.module_name.py_result(),
1311 name_list_code,
1312 code.error_goto_if_null(self.result(), self.pos)))
1315 class IteratorNode(ExprNode):
1316 # Used as part of for statement implementation.
1317 # Implements result = iter(sequence)
1319 # sequence ExprNode
1321 subexprs = ['sequence']
1323 def analyse_types(self, env):
1324 self.sequence.analyse_types(env)
1325 self.sequence = self.sequence.coerce_to_pyobject(env)
1326 self.type = py_object_type
1327 self.gil_check(env)
1328 self.is_temp = 1
1330 self.counter = TempNode(self.pos, PyrexTypes.c_py_ssize_t_type, env)
1331 self.counter.allocate_temp(env)
1333 gil_message = "Iterating over Python object"
1335 def release_temp(self, env):
1336 env.release_temp(self.result())
1337 self.counter.release_temp(env)
1339 def generate_result_code(self, code):
1340 code.putln(
1341 "if (PyList_CheckExact(%s) || PyTuple_CheckExact(%s)) {" % (
1342 self.sequence.py_result(),
1343 self.sequence.py_result()))
1344 code.putln(
1345 "%s = 0; %s = %s; Py_INCREF(%s);" % (
1346 self.counter.result(),
1347 self.result(),
1348 self.sequence.py_result(),
1349 self.result()))
1350 code.putln("} else {")
1351 code.putln("%s = -1; %s = PyObject_GetIter(%s); %s" % (
1352 self.counter.result(),
1353 self.result(),
1354 self.sequence.py_result(),
1355 code.error_goto_if_null(self.result(), self.pos)))
1356 code.putln("}")
1359 class NextNode(AtomicExprNode):
1360 # Used as part of for statement implementation.
1361 # Implements result = iterator.next()
1362 # Created during analyse_types phase.
1363 # The iterator is not owned by this node.
1365 # iterator ExprNode
1367 def __init__(self, iterator, env):
1368 self.pos = iterator.pos
1369 self.iterator = iterator
1370 self.type = py_object_type
1371 self.is_temp = 1
1373 def generate_result_code(self, code):
1374 for py_type in ["List", "Tuple"]:
1375 code.putln(
1376 "if (likely(Py%s_CheckExact(%s))) {" % (py_type, self.iterator.py_result()))
1377 code.putln(
1378 "if (%s >= Py%s_GET_SIZE(%s)) break;" % (
1379 self.iterator.counter.result(),
1380 py_type,
1381 self.iterator.py_result()))
1382 code.putln(
1383 "%s = Py%s_GET_ITEM(%s, %s); Py_INCREF(%s); %s++;" % (
1384 self.result(),
1385 py_type,
1386 self.iterator.py_result(),
1387 self.iterator.counter.result(),
1388 self.result(),
1389 self.iterator.counter.result()))
1390 code.put("} else ")
1391 code.putln("{")
1392 code.putln(
1393 "%s = PyIter_Next(%s);" % (
1394 self.result(),
1395 self.iterator.py_result()))
1396 code.putln(
1397 "if (!%s) {" %
1398 self.result())
1399 code.putln(code.error_goto_if_PyErr(self.pos))
1400 code.putln("break;")
1401 code.putln("}")
1402 code.putln("}")
1405 class ExcValueNode(AtomicExprNode):
1406 # Node created during analyse_types phase
1407 # of an ExceptClauseNode to fetch the current
1408 # exception value.
1410 def __init__(self, pos, env, var):
1411 ExprNode.__init__(self, pos)
1412 self.type = py_object_type
1413 self.var = var
1415 def calculate_result_code(self):
1416 return self.var
1418 def generate_result_code(self, code):
1419 pass
1421 def analyse_types(self, env):
1422 pass
1425 class TempNode(AtomicExprNode):
1426 # Node created during analyse_types phase
1427 # of some nodes to hold a temporary value.
1429 def __init__(self, pos, type, env):
1430 ExprNode.__init__(self, pos)
1431 self.type = type
1432 if type.is_pyobject:
1433 self.result_ctype = py_object_type
1434 self.is_temp = 1
1436 def analyse_types(self, env):
1437 return self.type
1439 def generate_result_code(self, code):
1440 pass
1443 class PyTempNode(TempNode):
1444 # TempNode holding a Python value.
1446 def __init__(self, pos, env):
1447 TempNode.__init__(self, pos, PyrexTypes.py_object_type, env)
1450 #-------------------------------------------------------------------
1452 # Trailer nodes
1454 #-------------------------------------------------------------------
1456 class IndexNode(ExprNode):
1457 # Sequence indexing.
1459 # base ExprNode
1460 # index ExprNode
1461 # indices [ExprNode]
1462 # is_buffer_access boolean Whether this is a buffer access.
1464 # indices is used on buffer access, index on non-buffer access.
1465 # The former contains a clean list of index parameters, the
1466 # latter whatever Python object is needed for index access.
1468 subexprs = ['base', 'index', 'indices']
1469 indices = None
1471 def __init__(self, pos, index, *args, **kw):
1472 ExprNode.__init__(self, pos, index=index, *args, **kw)
1473 self._index = index
1475 def compile_time_value(self, denv):
1476 base = self.base.compile_time_value(denv)
1477 index = self.index.compile_time_value(denv)
1478 try:
1479 return base[index]
1480 except Exception, e:
1481 self.compile_time_value_error(e)
1483 def is_ephemeral(self):
1484 return self.base.is_ephemeral()
1486 def analyse_target_declaration(self, env):
1487 pass
1489 def analyse_as_type(self, env):
1490 base_type = self.base.analyse_as_type(env)
1491 if base_type and not base_type.is_pyobject:
1492 return PyrexTypes.CArrayType(base_type, int(self.index.compile_time_value(env)))
1493 return None
1495 def analyse_types(self, env):
1496 self.analyse_base_and_index_types(env, getting = 1)
1498 def analyse_target_types(self, env):
1499 self.analyse_base_and_index_types(env, setting = 1)
1501 def analyse_base_and_index_types(self, env, getting = 0, setting = 0):
1502 # Note: This might be cleaned up by having IndexNode
1503 # parsed in a saner way and only construct the tuple if
1504 # needed.
1506 # Note that this function must leave IndexNode in a cloneable state.
1507 # For buffers, self.index is packed out on the initial analysis, and
1508 # when cloning self.indices is copied.
1509 self.is_buffer_access = False
1511 self.base.analyse_types(env)
1512 # Handle the case where base is a literal char* (and we expect a string, not an int)
1513 if isinstance(self.base, StringNode):
1514 self.base = self.base.coerce_to_pyobject(env)
1516 skip_child_analysis = False
1517 buffer_access = False
1518 if self.base.type.is_buffer:
1519 assert hasattr(self.base, "entry") # Must be a NameNode-like node
1520 if self.indices:
1521 indices = self.indices
1522 else:
1523 # On cloning, indices is cloned. Otherwise, unpack index into indices
1524 assert not isinstance(self.index, CloneNode)
1525 if isinstance(self.index, TupleNode):
1526 indices = self.index.args
1527 else:
1528 indices = [self.index]
1529 if len(indices) == self.base.type.ndim:
1530 buffer_access = True
1531 skip_child_analysis = True
1532 for x in indices:
1533 x.analyse_types(env)
1534 if not x.type.is_int:
1535 buffer_access = False
1537 if buffer_access:
1538 self.indices = indices
1539 self.index = None
1540 self.type = self.base.type.dtype
1541 self.is_buffer_access = True
1542 self.buffer_type = self.base.entry.type
1544 if getting and self.type.is_pyobject:
1545 self.is_temp = True
1546 if setting:
1547 if not self.base.entry.type.writable:
1548 error(self.pos, "Writing to readonly buffer")
1549 else:
1550 self.base.entry.buffer_aux.writable_needed = True
1551 else:
1552 if isinstance(self.index, TupleNode):
1553 self.index.analyse_types(env, skip_children=skip_child_analysis)
1554 elif not skip_child_analysis:
1555 self.index.analyse_types(env)
1556 if self.base.type.is_pyobject:
1557 if self.index.type.is_int and not self.index.type.is_longlong:
1558 self.original_index_type = self.index.type
1559 self.index = self.index.coerce_to(PyrexTypes.c_py_ssize_t_type, env).coerce_to_simple(env)
1560 else:
1561 self.index = self.index.coerce_to_pyobject(env)
1562 self.type = py_object_type
1563 self.gil_check(env)
1564 self.is_temp = 1
1565 else:
1566 if self.base.type.is_ptr or self.base.type.is_array:
1567 self.type = self.base.type.base_type
1568 else:
1569 error(self.pos,
1570 "Attempting to index non-array type '%s'" %
1571 self.base.type)
1572 self.type = PyrexTypes.error_type
1573 if self.index.type.is_pyobject:
1574 self.index = self.index.coerce_to(
1575 PyrexTypes.c_py_ssize_t_type, env)
1576 if not self.index.type.is_int:
1577 error(self.pos,
1578 "Invalid index type '%s'" %
1579 self.index.type)
1581 gil_message = "Indexing Python object"
1583 def check_const_addr(self):
1584 self.base.check_const_addr()
1585 self.index.check_const()
1587 def is_lvalue(self):
1588 return 1
1590 def calculate_result_code(self):
1591 if self.is_buffer_access:
1592 return "(*%s)" % self.buffer_ptr_code
1593 else:
1594 return "(%s[%s])" % (
1595 self.base.result(), self.index.result())
1597 def index_unsigned_parameter(self):
1598 if self.index.type.is_int:
1599 if self.original_index_type.signed:
1600 return ", 0"
1601 else:
1602 return ", sizeof(Py_ssize_t) <= sizeof(%s)" % self.original_index_type.declaration_code("")
1603 else:
1604 return ""
1606 def generate_subexpr_evaluation_code(self, code):
1607 self.base.generate_evaluation_code(code)
1608 if not self.indices:
1609 self.index.generate_evaluation_code(code)
1610 else:
1611 for i in self.indices:
1612 i.generate_evaluation_code(code)
1614 def generate_subexpr_disposal_code(self, code):
1615 self.base.generate_disposal_code(code)
1616 if not self.indices:
1617 self.index.generate_disposal_code(code)
1618 else:
1619 for i in self.indices:
1620 i.generate_disposal_code(code)
1622 def generate_result_code(self, code):
1623 if self.is_buffer_access:
1624 if code.globalstate.directives['nonecheck']:
1625 self.put_nonecheck(code)
1626 self.buffer_ptr_code = self.buffer_lookup_code(code)
1627 if self.type.is_pyobject:
1628 # is_temp is True, so must pull out value and incref it.
1629 code.putln("%s = *%s;" % (self.result(), self.buffer_ptr_code))
1630 code.putln("Py_INCREF((PyObject*)%s);" % self.result())
1631 elif self.type.is_pyobject:
1632 if self.index.type.is_int:
1633 function = "__Pyx_GetItemInt"
1634 index_code = self.index.result()
1635 code.globalstate.use_utility_code(getitem_int_utility_code)
1636 else:
1637 function = "PyObject_GetItem"
1638 index_code = self.index.py_result()
1639 sign_code = ""
1640 code.putln(
1641 "%s = %s(%s, %s%s); if (!%s) %s" % (
1642 self.result(),
1643 function,
1644 self.base.py_result(),
1645 index_code,
1646 self.index_unsigned_parameter(),
1647 self.result(),
1648 code.error_goto(self.pos)))
1650 def generate_setitem_code(self, value_code, code):
1651 if self.index.type.is_int:
1652 function = "__Pyx_SetItemInt"
1653 index_code = self.index.result()
1654 code.globalstate.use_utility_code(setitem_int_utility_code)
1655 else:
1656 function = "PyObject_SetItem"
1657 index_code = self.index.py_result()
1658 code.putln(
1659 "if (%s(%s, %s, %s%s) < 0) %s" % (
1660 function,
1661 self.base.py_result(),
1662 index_code,
1663 value_code,
1664 self.index_unsigned_parameter(),
1665 code.error_goto(self.pos)))
1667 def generate_buffer_setitem_code(self, rhs, code, op=""):
1668 # Used from generate_assignment_code and InPlaceAssignmentNode
1669 if code.globalstate.directives['nonecheck']:
1670 self.put_nonecheck(code)
1671 ptrexpr = self.buffer_lookup_code(code)
1672 if self.buffer_type.dtype.is_pyobject:
1673 # Must manage refcounts. Decref what is already there
1674 # and incref what we put in.
1675 ptr = code.funcstate.allocate_temp(self.buffer_type.buffer_ptr_type)
1676 if rhs.is_temp:
1677 rhs_code = code.funcstate.allocate_temp(rhs.type)
1678 else:
1679 rhs_code = rhs.result()
1680 code.putln("%s = %s;" % (ptr, ptrexpr))
1681 code.putln("Py_DECREF(*%s); Py_INCREF(%s);" % (
1682 ptr, rhs_code
1683 ))
1684 code.putln("*%s %s= %s;" % (ptr, op, rhs_code))
1685 if rhs.is_temp:
1686 code.funcstate.release_temp(rhs_code)
1687 code.funcstate.release_temp(ptr)
1688 else:
1689 # Simple case
1690 code.putln("*%s %s= %s;" % (ptrexpr, op, rhs.result()))
1692 def generate_assignment_code(self, rhs, code):
1693 self.generate_subexpr_evaluation_code(code)
1694 if self.is_buffer_access:
1695 self.generate_buffer_setitem_code(rhs, code)
1696 elif self.type.is_pyobject:
1697 self.generate_setitem_code(rhs.py_result(), code)
1698 else:
1699 code.putln(
1700 "%s = %s;" % (
1701 self.result(), rhs.result()))
1702 self.generate_subexpr_disposal_code(code)
1703 rhs.generate_disposal_code(code)
1705 def generate_deletion_code(self, code):
1706 self.generate_subexpr_evaluation_code(code)
1707 #if self.type.is_pyobject:
1708 if self.index.type.is_int:
1709 function = "__Pyx_DelItemInt"
1710 index_code = self.index.result()
1711 code.globalstate.use_utility_code(delitem_int_utility_code)
1712 else:
1713 function = "PyObject_DelItem"
1714 index_code = self.index.py_result()
1715 code.putln(
1716 "if (%s(%s, %s%s) < 0) %s" % (
1717 function,
1718 self.base.py_result(),
1719 index_code,
1720 self.index_unsigned_parameter(),
1721 code.error_goto(self.pos)))
1722 self.generate_subexpr_disposal_code(code)
1724 def buffer_lookup_code(self, code):
1725 # Assign indices to temps
1726 index_temps = [code.funcstate.allocate_temp(i.type) for i in self.indices]
1727 for temp, index in zip(index_temps, self.indices):
1728 code.putln("%s = %s;" % (temp, index.result()))
1729 # Generate buffer access code using these temps
1730 import Buffer
1731 # The above could happen because child_attrs is wrong somewhere so that
1732 # options are not propagated.
1733 return Buffer.put_buffer_lookup_code(entry=self.base.entry,
1734 index_signeds=[i.type.signed for i in self.indices],
1735 index_cnames=index_temps,
1736 options=code.globalstate.directives,
1737 pos=self.pos, code=code)
1739 def put_nonecheck(self, code):
1740 code.globalstate.use_utility_code(raise_noneindex_error_utility_code)
1741 code.putln("if (%s) {" % code.unlikely("%s == Py_None") % self.base.result_as(PyrexTypes.py_object_type))
1742 code.putln("__Pyx_RaiseNoneIndexingError();")
1743 code.putln(code.error_goto(self.pos))
1744 code.putln("}")
1746 class SliceIndexNode(ExprNode):
1747 # 2-element slice indexing
1749 # base ExprNode
1750 # start ExprNode or None
1751 # stop ExprNode or None
1753 subexprs = ['base', 'start', 'stop']
1755 def compile_time_value(self, denv):
1756 base = self.base.compile_time_value(denv)
1757 start = self.start.compile_time_value(denv)
1758 stop = self.stop.compile_time_value(denv)
1759 try:
1760 return base[start:stop]
1761 except Exception, e:
1762 self.compile_time_value_error(e)
1764 def analyse_target_declaration(self, env):
1765 pass
1767 def analyse_types(self, env):
1768 self.base.analyse_types(env)
1769 if self.start:
1770 self.start.analyse_types(env)
1771 if self.stop:
1772 self.stop.analyse_types(env)
1773 self.base = self.base.coerce_to_pyobject(env)
1774 c_int = PyrexTypes.c_py_ssize_t_type
1775 if self.start:
1776 self.start = self.start.coerce_to(c_int, env)
1777 if self.stop:
1778 self.stop = self.stop.coerce_to(c_int, env)
1779 self.type = py_object_type
1780 self.gil_check(env)
1781 self.is_temp = 1
1783 gil_message = "Slicing Python object"
1785 def generate_result_code(self, code):
1786 code.putln(
1787 "%s = PySequence_GetSlice(%s, %s, %s); %s" % (
1788 self.result(),
1789 self.base.py_result(),
1790 self.start_code(),
1791 self.stop_code(),
1792 code.error_goto_if_null(self.result(), self.pos)))
1794 def generate_assignment_code(self, rhs, code):
1795 self.generate_subexpr_evaluation_code(code)
1796 code.put_error_if_neg(self.pos,
1797 "PySequence_SetSlice(%s, %s, %s, %s)" % (
1798 self.base.py_result(),
1799 self.start_code(),
1800 self.stop_code(),
1801 rhs.result()))
1802 self.generate_subexpr_disposal_code(code)
1803 rhs.generate_disposal_code(code)
1805 def generate_deletion_code(self, code):
1806 self.generate_subexpr_evaluation_code(code)
1807 code.put_error_if_neg(self.pos,
1808 "PySequence_DelSlice(%s, %s, %s)" % (
1809 self.base.py_result(),
1810 self.start_code(),
1811 self.stop_code()))
1812 self.generate_subexpr_disposal_code(code)
1814 def start_code(self):
1815 if self.start:
1816 return self.start.result()
1817 else:
1818 return "0"
1820 def stop_code(self):
1821 if self.stop:
1822 return self.stop.result()
1823 else:
1824 return "PY_SSIZE_T_MAX"
1826 def calculate_result_code(self):
1827 # self.result() is not used, but this method must exist
1828 return "<unused>"
1831 class SliceNode(ExprNode):
1832 # start:stop:step in subscript list
1834 # start ExprNode
1835 # stop ExprNode
1836 # step ExprNode
1838 def compile_time_value(self, denv):
1839 start = self.start.compile_time_value(denv)
1840 stop = self.stop.compile_time_value(denv)
1841 step = step.step.compile_time_value(denv)
1842 try:
1843 return slice(start, stop, step)
1844 except Exception, e:
1845 self.compile_time_value_error(e)
1847 subexprs = ['start', 'stop', 'step']
1849 def analyse_types(self, env):
1850 self.start.analyse_types(env)
1851 self.stop.analyse_types(env)
1852 self.step.analyse_types(env)
1853 self.start = self.start.coerce_to_pyobject(env)
1854 self.stop = self.stop.coerce_to_pyobject(env)
1855 self.step = self.step.coerce_to_pyobject(env)
1856 self.type = py_object_type
1857 self.gil_check(env)
1858 self.is_temp = 1
1860 gil_message = "Constructing Python slice object"
1862 def generate_result_code(self, code):
1863 code.putln(
1864 "%s = PySlice_New(%s, %s, %s); %s" % (
1865 self.result(),
1866 self.start.py_result(),
1867 self.stop.py_result(),
1868 self.step.py_result(),
1869 code.error_goto_if_null(self.result(), self.pos)))
1872 class CallNode(ExprNode):
1873 def gil_check(self, env):
1874 # Make sure we're not in a nogil environment
1875 if env.nogil:
1876 error(self.pos, "Calling gil-requiring function without gil")
1878 def analyse_as_type_constructor(self, env):
1879 type = self.function.analyse_as_type(env)
1880 if type and type.is_struct_or_union:
1881 args, kwds = self.explicit_args_kwds()
1882 items = []
1883 for arg, member in zip(args, type.scope.var_entries):
1884 items.append(DictItemNode(pos=arg.pos, key=IdentifierStringNode(pos=arg.pos, value=member.name), value=arg))
1885 if kwds:
1886 items += kwds.key_value_pairs
1887 self.key_value_pairs = items
1888 self.__class__ = DictNode
1889 self.analyse_types(env)
1890 self.coerce_to(type, env)
1891 return True
1894 class SimpleCallNode(CallNode):
1895 # Function call without keyword, * or ** args.
1897 # function ExprNode
1898 # args [ExprNode]
1899 # arg_tuple ExprNode or None used internally
1900 # self ExprNode or None used internally
1901 # coerced_self ExprNode or None used internally
1902 # wrapper_call bool used internally
1903 # has_optional_args bool used internally
1905 subexprs = ['self', 'coerced_self', 'function', 'args', 'arg_tuple']
1907 self = None
1908 coerced_self = None
1909 arg_tuple = None
1910 wrapper_call = False
1911 has_optional_args = False
1913 def compile_time_value(self, denv):
1914 function = self.function.compile_time_value(denv)
1915 args = [arg.compile_time_value(denv) for arg in self.args]
1916 try:
1917 return function(*args)
1918 except Exception, e:
1919 self.compile_time_value_error(e)
1921 def analyse_as_type(self, env):
1922 attr = self.function.as_cython_attribute()
1923 if attr == 'pointer':
1924 if len(self.args) != 1:
1925 error(self.args.pos, "only one type allowed.")
1926 else:
1927 type = self.args[0].analyse_as_type(env)
1928 if not type:
1929 error(self.args[0].pos, "Unknown type")
1930 else:
1931 return PyrexTypes.CPtrType(type)
1933 def explicit_args_kwds(self):
1934 return self.args, None
1936 def analyse_types(self, env):
1937 if self.analyse_as_type_constructor(env):
1938 return
1939 function = self.function
1940 function.is_called = 1
1941 self.function.analyse_types(env)
1942 if function.is_attribute and function.is_py_attr and \
1943 function.attribute == "append" and len(self.args) == 1:
1944 # L.append(x) is almost always applied to a list
1945 self.py_func = self.function
1946 self.function = NameNode(pos=self.function.pos, name="__Pyx_PyObject_Append")
1947 self.function.analyse_types(env)
1948 self.self = self.py_func.obj
1949 function.obj = CloneNode(self.self)
1950 env.use_utility_code(append_utility_code)
1951 if function.is_attribute and function.entry and function.entry.is_cmethod:
1952 # Take ownership of the object from which the attribute
1953 # was obtained, because we need to pass it as 'self'.
1954 self.self = function.obj
1955 function.obj = CloneNode(self.self)
1956 func_type = self.function_type()
1957 if func_type.is_pyobject:
1958 self.arg_tuple = TupleNode(self.pos, args = self.args)
1959 self.arg_tuple.analyse_types(env)
1960 self.args = None
1961 self.type = py_object_type
1962 self.gil_check(env)
1963 self.is_temp = 1
1964 else:
1965 for arg in self.args:
1966 arg.analyse_types(env)
1967 if self.self and func_type.args:
1968 # Coerce 'self' to the type expected by the method.
1969 expected_type = func_type.args[0].type
1970 self.coerced_self = CloneNode(self.self).coerce_to(
1971 expected_type, env)
1972 # Insert coerced 'self' argument into argument list.
1973 self.args.insert(0, self.coerced_self)
1974 self.analyse_c_function_call(env)
1976 def function_type(self):
1977 # Return the type of the function being called, coercing a function
1978 # pointer to a function if necessary.
1979 func_type = self.function.type
1980 if func_type.is_ptr:
1981 func_type = func_type.base_type
1982 return func_type
1984 def analyse_c_function_call(self, env):
1985 func_type = self.function_type()
1986 # Check function type
1987 if not func_type.is_cfunction:
1988 if not func_type.is_error:
1989 error(self.pos, "Calling non-function type '%s'" %
1990 func_type)
1991 self.type = PyrexTypes.error_type
1992 self.result_code = "<error>"
1993 return
1994 # Check no. of args
1995 max_nargs = len(func_type.args)
1996 expected_nargs = max_nargs - func_type.optional_arg_count
1997 actual_nargs = len(self.args)
1998 if actual_nargs < expected_nargs \
1999 or (not func_type.has_varargs and actual_nargs > max_nargs):
2000 expected_str = str(expected_nargs)
2001 if func_type.has_varargs:
2002 expected_str = "at least " + expected_str
2003 elif func_type.optional_arg_count:
2004 if actual_nargs < max_nargs:
2005 expected_str = "at least " + expected_str
2006 else:
2007 expected_str = "at most " + str(max_nargs)
2008 error(self.pos,
2009 "Call with wrong number of arguments (expected %s, got %s)"
2010 % (expected_str, actual_nargs))
2011 self.args = None
2012 self.type = PyrexTypes.error_type
2013 self.result_code = "<error>"
2014 return
2015 if func_type.optional_arg_count and expected_nargs != actual_nargs:
2016 self.has_optional_args = 1
2017 self.is_temp = 1
2018 self.opt_arg_struct = env.allocate_temp(func_type.op_arg_struct.base_type)
2019 env.release_temp(self.opt_arg_struct)
2020 # Coerce arguments
2021 for i in range(min(max_nargs, actual_nargs)):
2022 formal_type = func_type.args[i].type
2023 self.args[i] = self.args[i].coerce_to(formal_type, env)
2024 for i in range(max_nargs, actual_nargs):
2025 if self.args[i].type.is_pyobject:
2026 error(self.args[i].pos,
2027 "Python object cannot be passed as a varargs parameter")
2028 # Calc result type and code fragment
2029 self.type = func_type.return_type
2030 if self.type.is_pyobject \
2031 or func_type.exception_value is not None \
2032 or func_type.exception_check:
2033 self.is_temp = 1
2034 if self.type.is_pyobject:
2035 self.result_ctype = py_object_type
2036 # C++ exception handler
2037 if func_type.exception_check == '+':
2038 if func_type.exception_value is None:
2039 env.use_utility_code(cpp_exception_utility_code)
2040 # Check gil
2041 if not func_type.nogil:
2042 self.gil_check(env)
2044 def calculate_result_code(self):
2045 return self.c_call_code()
2047 def c_call_code(self):
2048 func_type = self.function_type()
2049 if self.args is None or not func_type.is_cfunction:
2050 return "<error>"
2051 formal_args = func_type.args
2052 arg_list_code = []
2053 args = zip(formal_args, self.args)
2054 max_nargs = len(func_type.args)
2055 expected_nargs = max_nargs - func_type.optional_arg_count
2056 actual_nargs = len(self.args)
2057 for formal_arg, actual_arg in args[:expected_nargs]:
2058 arg_code = actual_arg.result_as(formal_arg.type)
2059 arg_list_code.append(arg_code)
2061 if func_type.is_overridable:
2062 arg_list_code.append(str(int(self.wrapper_call or self.function.entry.is_unbound_cmethod)))
2064 if func_type.optional_arg_count:
2065 if expected_nargs == actual_nargs:
2066 optional_args = 'NULL'
2067 else:
2068 optional_args = "&%s" % self.opt_arg_struct
2069 arg_list_code.append(optional_args)
2071 for actual_arg in self.args[len(formal_args):]:
2072 arg_list_code.append(actual_arg.result())
2073 result = "%s(%s)" % (self.function.result(),
2074 join(arg_list_code, ", "))
2075 # if self.wrapper_call or \
2076 # self.function.entry.is_unbound_cmethod and self.function.entry.type.is_overridable:
2077 # result = "(%s = 1, %s)" % (Naming.skip_dispatch_cname, result)
2078 return result
2080 def generate_result_code(self, code):
2081 func_type = self.function_type()
2082 if func_type.is_pyobject:
2083 arg_code = self.arg_tuple.py_result()
2084 code.putln(
2085 "%s = PyObject_Call(%s, %s, NULL); %s" % (
2086 self.result(),
2087 self.function.py_result(),
2088 arg_code,
2089 code.error_goto_if_null(self.result(), self.pos)))
2090 elif func_type.is_cfunction:
2091 if self.has_optional_args:
2092 actual_nargs = len(self.args)
2093 expected_nargs = len(func_type.args) - func_type.optional_arg_count
2094 code.putln("%s.%s = %s;" % (
2095 self.opt_arg_struct,
2096 Naming.pyrex_prefix + "n",
2097 len(self.args) - expected_nargs))
2098 args = zip(func_type.args, self.args)
2099 for formal_arg, actual_arg in args[expected_nargs:actual_nargs]:
2100 code.putln("%s.%s = %s;" % (
2101 self.opt_arg_struct,
2102 formal_arg.name,
2103 actual_arg.result_as(formal_arg.type)))
2104 exc_checks = []
2105 if self.type.is_pyobject:
2106 exc_checks.append("!%s" % self.result())
2107 else:
2108 exc_val = func_type.exception_value
2109 exc_check = func_type.exception_check
2110 if exc_val is not None:
2111 exc_checks.append("%s == %s" % (self.result(), exc_val))
2112 if exc_check:
2113 exc_checks.append("PyErr_Occurred()")
2114 if self.is_temp or exc_checks:
2115 rhs = self.c_call_code()
2116 if self.result():
2117 lhs = "%s = " % self.result()
2118 if self.is_temp and self.type.is_pyobject:
2119 #return_type = self.type # func_type.return_type
2120 #print "SimpleCallNode.generate_result_code: casting", rhs, \
2121 # "from", return_type, "to pyobject" ###
2122 rhs = typecast(py_object_type, self.type, rhs)
2123 else:
2124 lhs = ""
2125 if func_type.exception_check == '+':
2126 if func_type.exception_value is None:
2127 raise_py_exception = "__Pyx_CppExn2PyErr()"
2128 elif func_type.exception_value.type.is_pyobject:
2129 raise_py_exception = 'PyErr_SetString(%s, "")' % func_type.exception_value.entry.cname
2130 else:
2131 raise_py_exception = '%s(); if (!PyErr_Occurred()) PyErr_SetString(PyExc_RuntimeError , "Error converting c++ exception.")' % func_type.exception_value.entry.cname
2132 code.putln(
2133 "try {%s%s;} catch(...) {%s; %s}" % (
2134 lhs,
2135 rhs,
2136 raise_py_exception,
2137 code.error_goto(self.pos)))
2138 else:
2139 if exc_checks:
2140 goto_error = code.error_goto_if(" && ".join(exc_checks), self.pos)
2141 else:
2142 goto_error = ""
2143 code.putln("%s%s; %s" % (lhs, rhs, goto_error))
2145 class GeneralCallNode(CallNode):
2146 # General Python function call, including keyword,
2147 # * and ** arguments.
2149 # function ExprNode
2150 # positional_args ExprNode Tuple of positional arguments
2151 # keyword_args ExprNode or None Dict of keyword arguments
2152 # starstar_arg ExprNode or None Dict of extra keyword args
2154 subexprs = ['function', 'positional_args', 'keyword_args', 'starstar_arg']
2156 def compile_time_value(self, denv):
2157 function = self.function.compile_time_value(denv)
2158 positional_args = self.positional_args.compile_time_value(denv)
2159 keyword_args = self.keyword_args.compile_time_value(denv)
2160 starstar_arg = self.starstar_arg.compile_time_value(denv)
2161 try:
2162 keyword_args.update(starstar_arg)
2163 return function(*positional_args, **keyword_args)
2164 except Exception, e:
2165 self.compile_time_value_error(e)
2167 def explicit_args_kwds(self):
2168 if self.starstar_arg or not isinstance(self.positional_args, TupleNode):
2169 raise PostParseError(self.pos,
2170 'Compile-time keyword arguments must be explicit.')
2171 return self.positional_args.args, self.keyword_args
2173 def analyse_types(self, env):
2174 if self.analyse_as_type_constructor(env):
2175 return
2176 self.function.analyse_types(env)
2177 self.positional_args.analyse_types(env)
2178 if self.keyword_args:
2179 self.keyword_args.analyse_types(env)
2180 if self.starstar_arg:
2181 self.starstar_arg.analyse_types(env)
2182 self.function = self.function.coerce_to_pyobject(env)
2183 self.positional_args = \
2184 self.positional_args.coerce_to_pyobject(env)
2185 if self.starstar_arg:
2186 self.starstar_arg = \
2187 self.starstar_arg.coerce_to_pyobject(env)
2188 self.type = py_object_type
2189 self.gil_check(env)
2190 self.is_temp = 1
2192 def generate_result_code(self, code):
2193 if self.keyword_args and self.starstar_arg:
2194 code.put_error_if_neg(self.pos,
2195 "PyDict_Update(%s, %s)" % (
2196 self.keyword_args.py_result(),
2197 self.starstar_arg.py_result()))
2198 keyword_code = self.keyword_args.py_result()
2199 elif self.keyword_args:
2200 keyword_code = self.keyword_args.py_result()
2201 elif self.starstar_arg:
2202 keyword_code = self.starstar_arg.py_result()
2203 else:
2204 keyword_code = None
2205 if not keyword_code:
2206 call_code = "PyObject_Call(%s, %s, NULL)" % (
2207 self.function.py_result(),
2208 self.positional_args.py_result())
2209 else:
2210 call_code = "PyEval_CallObjectWithKeywords(%s, %s, %s)" % (
2211 self.function.py_result(),
2212 self.positional_args.py_result(),
2213 keyword_code)
2214 code.putln(
2215 "%s = %s; %s" % (
2216 self.result(),
2217 call_code,
2218 code.error_goto_if_null(self.result(), self.pos)))
2221 class AsTupleNode(ExprNode):
2222 # Convert argument to tuple. Used for normalising
2223 # the * argument of a function call.
2225 # arg ExprNode
2227 subexprs = ['arg']
2229 def compile_time_value(self, denv):
2230 arg = self.arg.compile_time_value(denv)
2231 try:
2232 return tuple(arg)
2233 except Exception, e:
2234 self.compile_time_value_error(e)
2236 def analyse_types(self, env):
2237 self.arg.analyse_types(env)
2238 self.arg = self.arg.coerce_to_pyobject(env)
2239 self.type = py_object_type
2240 self.gil_check(env)
2241 self.is_temp = 1
2243 gil_message = "Constructing Python tuple"
2245 def generate_result_code(self, code):
2246 code.putln(
2247 "%s = PySequence_Tuple(%s); %s" % (
2248 self.result(),
2249 self.arg.py_result(),
2250 code.error_goto_if_null(self.result(), self.pos)))
2253 class AttributeNode(ExprNode):
2254 # obj.attribute
2256 # obj ExprNode
2257 # attribute string
2258 # needs_none_check boolean Used if obj is an extension type.
2259 # If set to True, it is known that the type is not None.
2261 # Used internally:
2263 # is_py_attr boolean Is a Python getattr operation
2264 # member string C name of struct member
2265 # is_called boolean Function call is being done on result
2266 # entry Entry Symbol table entry of attribute
2267 # interned_attr_cname string C name of interned attribute name
2269 is_attribute = 1
2270 subexprs = ['obj']
2272 type = PyrexTypes.error_type
2273 entry = None
2274 is_called = 0
2275 needs_none_check = True
2277 def as_cython_attribute(self):
2278 if isinstance(self.obj, NameNode) and self.obj.is_cython_module:
2279 return self.attribute
2281 def coerce_to(self, dst_type, env):
2282 # If coercing to a generic pyobject and this is a cpdef function
2283 # we can create the corresponding attribute
2284 if dst_type is py_object_type:
2285 entry = self.entry
2286 if entry and entry.is_cfunction and entry.as_variable:
2287 # must be a cpdef function
2288 self.is_temp = 1
2289 self.entry = entry.as_variable
2290 self.analyse_as_python_attribute(env)
2291 return self
2292 return ExprNode.coerce_to(self, dst_type, env)
2294 def compile_time_value(self, denv):
2295 attr = self.attribute
2296 if attr.beginswith("__") and attr.endswith("__"):
2297 self.error("Invalid attribute name '%s' in compile-time expression"
2298 % attr)
2299 return None
2300 obj = self.arg.compile_time_value(denv)
2301 try:
2302 return getattr(obj, attr)
2303 except Exception, e:
2304 self.compile_time_value_error(e)
2306 def analyse_target_declaration(self, env):
2307 pass
2309 def analyse_target_types(self, env):
2310 self.analyse_types(env, target = 1)
2312 def analyse_types(self, env, target = 0):
2313 if self.analyse_as_cimported_attribute(env, target):
2314 return
2315 if not target and self.analyse_as_unbound_cmethod(env):
2316 return
2317 self.analyse_as_ordinary_attribute(env, target)
2319 def analyse_as_cimported_attribute(self, env, target):
2320 # Try to interpret this as a reference to an imported
2321 # C const, type, var or function. If successful, mutates
2322 # this node into a NameNode and returns 1, otherwise
2323 # returns 0.
2324 module_scope = self.obj.analyse_as_module(env)
2325 if module_scope:
2326 entry = module_scope.lookup_here(self.attribute)
2327 if entry and (
2328 entry.is_cglobal or entry.is_cfunction
2329 or entry.is_type or entry.is_const):
2330 self.mutate_into_name_node(env, entry, target)
2331 return 1
2332 return 0
2334 def analyse_as_unbound_cmethod(self, env):
2335 # Try to interpret this as a reference to an unbound
2336 # C method of an extension type. If successful, mutates
2337 # this node into a NameNode and returns 1, otherwise
2338 # returns 0.
2339 type = self.obj.analyse_as_extension_type(env)
2340 if type:
2341 entry = type.scope.lookup_here(self.attribute)
2342 if entry and entry.is_cmethod:
2343 # Create a temporary entry describing the C method
2344 # as an ordinary function.
2345 ubcm_entry = Symtab.Entry(entry.name,
2346 "%s->%s" % (type.vtabptr_cname, entry.cname),
2347 entry.type)
2348 ubcm_entry.is_cfunction = 1
2349 ubcm_entry.func_cname = entry.func_cname
2350 ubcm_entry.is_unbound_cmethod = 1
2351 self.mutate_into_name_node(env, ubcm_entry, None)
2352 return 1
2353 return 0
2355 def analyse_as_type(self, env):
2356 module_scope = self.obj.analyse_as_module(env)
2357 if module_scope:
2358 return module_scope.lookup_type(self.attribute)
2359 return None
2361 def analyse_as_extension_type(self, env):
2362 # Try to interpret this as a reference to an extension type
2363 # in a cimported module. Returns the extension type, or None.
2364 module_scope = self.obj.analyse_as_module(env)
2365 if module_scope:
2366 entry = module_scope.lookup_here(self.attribute)
2367 if entry and entry.is_type and entry.type.is_extension_type:
2368 return entry.type
2369 return None
2371 def analyse_as_module(self, env):
2372 # Try to interpret this as a reference to a cimported module
2373 # in another cimported module. Returns the module scope, or None.
2374 module_scope = self.obj.analyse_as_module(env)
2375 if module_scope:
2376 entry = module_scope.lookup_here(self.attribute)
2377 if entry and entry.as_module:
2378 return entry.as_module
2379 return None
2381 def mutate_into_name_node(self, env, entry, target):
2382 # Mutate this node into a NameNode and complete the
2383 # analyse_types phase.
2384 self.__class__ = NameNode
2385 self.name = self.attribute
2386 self.entry = entry
2387 del self.obj
2388 del self.attribute
2389 if target:
2390 NameNode.analyse_target_types(self, env)
2391 else:
2392 NameNode.analyse_rvalue_entry(self, env)
2394 def analyse_as_ordinary_attribute(self, env, target):
2395 self.obj.analyse_types(env)
2396 self.analyse_attribute(env)
2397 if self.entry and self.entry.is_cmethod and not self.is_called:
2398 # error(self.pos, "C method can only be called")
2399 pass
2400 ## Reference to C array turns into pointer to first element.
2401 #while self.type.is_array:
2402 # self.type = self.type.element_ptr_type()
2403 if self.is_py_attr:
2404 if not target:
2405 self.is_temp = 1
2406 self.result_ctype = py_object_type
2408 def analyse_attribute(self, env):
2409 # Look up attribute and set self.type and self.member.
2410 self.is_py_attr = 0
2411 self.member = self.attribute
2412 if self.obj.type.is_string:
2413 self.obj = self.obj.coerce_to_pyobject(env)
2414 obj_type = self.obj.type
2415 if obj_type.is_ptr or obj_type.is_array:
2416 obj_type = obj_type.base_type
2417 self.op = "->"
2418 elif obj_type.is_extension_type:
2419 self.op = "->"
2420 else:
2421 self.op = "."
2422 if obj_type.has_attributes:
2423 entry = None
2424 if obj_type.attributes_known():
2425 entry = obj_type.scope.lookup_here(self.attribute)
2426 if entry and entry.is_member:
2427 entry = None
2428 else:
2429 error(self.pos,
2430 "Cannot select attribute of incomplete type '%s'"
2431 % obj_type)
2432 self.type = PyrexTypes.error_type
2433 return
2434 self.entry = entry
2435 if entry:
2436 if obj_type.is_extension_type and entry.name == "__weakref__":
2437 error(self.pos, "Illegal use of special attribute __weakref__")
2438 # methods need the normal attribute lookup
2439 # because they do not have struct entries
2440 if entry.is_variable or entry.is_cmethod:
2441 self.type = entry.type
2442 self.member = entry.cname
2443 return
2444 else:
2445 # If it's not a variable or C method, it must be a Python
2446 # method of an extension type, so we treat it like a Python
2447 # attribute.
2448 pass
2449 # If we get here, the base object is not a struct/union/extension
2450 # type, or it is an extension type and the attribute is either not
2451 # declared or is declared as a Python method. Treat it as a Python
2452 # attribute reference.
2453 self.analyse_as_python_attribute(env)
2455 def analyse_as_python_attribute(self, env):
2456 obj_type = self.obj.type
2457 self.member = self.attribute
2458 if obj_type.is_pyobject:
2459 self.type = py_object_type
2460 self.is_py_attr = 1
2461 self.interned_attr_cname = env.intern_identifier(self.attribute)
2462 self.gil_check(env)
2463 else:
2464 if not obj_type.is_error:
2465 error(self.pos,
2466 "Object of type '%s' has no attribute '%s'" %
2467 (obj_type, self.attribute))
2469 gil_message = "Accessing Python attribute"
2471 def is_simple(self):
2472 if self.obj:
2473 return self.result_in_temp() or self.obj.is_simple()
2474 else:
2475 return NameNode.is_simple(self)
2477 def is_lvalue(self):
2478 if self.obj:
2479 return 1
2480 else:
2481 return NameNode.is_lvalue(self)
2483 def is_ephemeral(self):
2484 if self.obj:
2485 return self.obj.is_ephemeral()
2486 else:
2487 return NameNode.is_ephemeral(self)
2489 def calculate_result_code(self):
2490 #print "AttributeNode.calculate_result_code:", self.member ###
2491 #print "...obj node =", self.obj, "code", self.obj.result() ###
2492 #print "...obj type", self.obj.type, "ctype", self.obj.ctype() ###
2493 obj = self.obj
2494 obj_code = obj.result_as(obj.type)
2495 #print "...obj_code =", obj_code ###
2496 if self.entry and self.entry.is_cmethod:
2497 if obj.type.is_extension_type:
2498 return "((struct %s *)%s%s%s)->%s" % (
2499 obj.type.vtabstruct_cname, obj_code, self.op,
2500 obj.type.vtabslot_cname, self.member)
2501 else:
2502 return self.member
2503 else:
2504 return "%s%s%s" % (obj_code, self.op, self.member)
2506 def generate_result_code(self, code):
2507 if self.is_py_attr:
2508 code.putln(
2509 '%s = PyObject_GetAttr(%s, %s); %s' % (
2510 self.result(),
2511 self.obj.py_result(),
2512 self.interned_attr_cname,
2513 code.error_goto_if_null(self.result(), self.pos)))
2514 else:
2515 # result_code contains what is needed, but we may need to insert
2516 # a check and raise an exception
2517 if (self.obj.type.is_extension_type
2518 and self.needs_none_check
2519 and code.globalstate.directives['nonecheck']):
2520 self.put_nonecheck(code)
2522 def generate_assignment_code(self, rhs, code):
2523 self.obj.generate_evaluation_code(code)
2524 if self.is_py_attr:
2525 code.put_error_if_neg(self.pos,
2526 'PyObject_SetAttr(%s, %s, %s)' % (
2527 self.obj.py_result(),
2528 self.interned_attr_cname,
2529 rhs.py_result()))
2530 rhs.generate_disposal_code(code)
2531 else:
2532 if (self.obj.type.is_extension_type
2533 and self.needs_none_check
2534 and code.globalstate.directives['nonecheck']):
2535 self.put_nonecheck(code)
2537 select_code = self.result()
2538 if self.type.is_pyobject:
2539 rhs.make_owned_reference(code)
2540 code.put_decref(select_code, self.ctype())
2541 code.putln(
2542 "%s = %s;" % (
2543 select_code,
2544 rhs.result_as(self.ctype())))
2545 #rhs.result()))
2546 rhs.generate_post_assignment_code(code)
2547 self.obj.generate_disposal_code(code)
2549 def generate_deletion_code(self, code):
2550 self.obj.generate_evaluation_code(code)
2551 if self.is_py_attr:
2552 code.put_error_if_neg(self.pos,
2553 'PyObject_DelAttr(%s, %s)' % (
2554 self.obj.py_result(),
2555 self.interned_attr_cname))
2556 else:
2557 error(self.pos, "Cannot delete C attribute of extension type")
2558 self.obj.generate_disposal_code(code)
2560 def annotate(self, code):
2561 if self.is_py_attr:
2562 code.annotate(self.pos, AnnotationItem('py_attr', 'python attribute', size=len(self.attribute)))
2563 else:
2564 code.annotate(self.pos, AnnotationItem('c_attr', 'c attribute', size=len(self.attribute)))
2566 def put_nonecheck(self, code):
2567 code.globalstate.use_utility_code(raise_noneattr_error_utility_code)
2568 code.putln("if (%s) {" % code.unlikely("%s == Py_None") % self.obj.result_as(PyrexTypes.py_object_type))
2569 code.putln("__Pyx_RaiseNoneAttributeError(\"%s\");" % self.attribute.encode("UTF-8")) # todo: fix encoding
2570 code.putln(code.error_goto(self.pos))
2571 code.putln("}")
2574 #-------------------------------------------------------------------
2576 # Constructor nodes
2578 #-------------------------------------------------------------------
2580 class SequenceNode(ExprNode):
2581 # Base class for list and tuple constructor nodes.
2582 # Contains common code for performing sequence unpacking.
2584 # args [ExprNode]
2585 # iterator ExprNode
2586 # unpacked_items [ExprNode] or None
2587 # coerced_unpacked_items [ExprNode] or None
2589 subexprs = ['args']
2591 is_sequence_constructor = 1
2592 unpacked_items = None
2594 def compile_time_value_list(self, denv):
2595 return [arg.compile_time_value(denv) for arg in self.args]
2597 def analyse_target_declaration(self, env):
2598 for arg in self.args:
2599 arg.analyse_target_declaration(env)
2601 def analyse_types(self, env, skip_children=False):
2602 for i in range(len(self.args)):
2603 arg = self.args[i]
2604 if not skip_children: arg.analyse_types(env)
2605 self.args[i] = arg.coerce_to_pyobject(env)
2606 self.type = py_object_type
2607 self.gil_check(env)
2608 self.is_temp = 1
2610 def analyse_target_types(self, env):
2611 self.iterator = PyTempNode(self.pos, env)
2612 self.unpacked_items = []
2613 self.coerced_unpacked_items = []
2614 for arg in self.args:
2615 arg.analyse_target_types(env)
2616 unpacked_item = PyTempNode(self.pos, env)
2617 coerced_unpacked_item = unpacked_item.coerce_to(arg.type, env)
2618 self.unpacked_items.append(unpacked_item)
2619 self.coerced_unpacked_items.append(coerced_unpacked_item)
2620 self.type = py_object_type
2621 env.use_utility_code(unpacking_utility_code)
2623 def allocate_target_temps(self, env, rhs):
2624 self.iterator.allocate_temps(env)
2625 for arg, node in zip(self.args, self.coerced_unpacked_items):
2626 node.allocate_temps(env)
2627 arg.allocate_target_temps(env, node)
2628 #arg.release_target_temp(env)
2629 #node.release_temp(env)
2630 if rhs:
2631 rhs.release_temp(env)
2632 self.iterator.release_temp(env)
2634 # def release_target_temp(self, env):
2635 # #for arg in self.args:
2636 # # arg.release_target_temp(env)
2637 # #for node in self.coerced_unpacked_items:
2638 # # node.release_temp(env)
2639 # self.iterator.release_temp(env)
2641 def generate_result_code(self, code):
2642 self.generate_operation_code(code)
2644 def generate_assignment_code(self, rhs, code):
2645 code.putln(
2646 "if (PyTuple_CheckExact(%s) && PyTuple_GET_SIZE(%s) == %s) {" % (
2647 rhs.py_result(),
2648 rhs.py_result(),
2649 len(self.args)))
2650 code.putln("PyObject* tuple = %s;" % rhs.py_result())
2651 for i in range(len(self.args)):
2652 item = self.unpacked_items[i]
2653 code.putln(
2654 "%s = PyTuple_GET_ITEM(tuple, %s);" % (
2655 item.result(),
2656 i))
2657 code.put_incref(item.result(), item.ctype())
2658 value_node = self.coerced_unpacked_items[i]
2659 value_node.generate_evaluation_code(code)
2660 self.args[i].generate_assignment_code(value_node, code)
2662 rhs.generate_disposal_code(code)
2663 code.putln("}")
2664 code.putln("else {")
2666 code.putln(
2667 "%s = PyObject_GetIter(%s); %s" % (
2668 self.iterator.result(),
2669 rhs.py_result(),
2670 code.error_goto_if_null(self.iterator.result(), self.pos)))
2671 rhs.generate_disposal_code(code)
2672 for i in range(len(self.args)):
2673 item = self.unpacked_items[i]
2674 unpack_code = "__Pyx_UnpackItem(%s, %d)" % (
2675 self.iterator.py_result(), i)
2676 code.putln(
2677 "%s = %s; %s" % (
2678 item.result(),
2679 typecast(item.ctype(), py_object_type, unpack_code),
2680 code.error_goto_if_null(item.result(), self.pos)))
2681 value_node = self.coerced_unpacked_items[i]
2682 value_node.generate_evaluation_code(code)
2683 self.args[i].generate_assignment_code(value_node, code)
2684 code.put_error_if_neg(self.pos,
2685 "__Pyx_EndUnpack(%s)" % (
2686 self.iterator.py_result()))
2687 if debug_disposal_code:
2688 print("UnpackNode.generate_assignment_code:")
2689 print("...generating disposal code for %s" % self.iterator)
2690 self.iterator.generate_disposal_code(code)
2692 code.putln("}")
2694 def annotate(self, code):
2695 for arg in self.args:
2696 arg.annotate(code)
2697 if self.unpacked_items:
2698 for arg in self.unpacked_items:
2699 arg.annotate(code)
2700 for arg in self.coerced_unpacked_items:
2701 arg.annotate(code)
2704 class TupleNode(SequenceNode):
2705 # Tuple constructor.
2707 gil_message = "Constructing Python tuple"
2709 def analyse_types(self, env, skip_children=False):
2710 if len(self.args) == 0:
2711 self.is_temp = 0
2712 self.is_literal = 1
2713 else:
2714 SequenceNode.analyse_types(self, env, skip_children)
2715 self.type = tuple_type
2717 def calculate_result_code(self):
2718 if len(self.args) > 0:
2719 error(self.pos, "Positive length tuples must be constructed.")
2720 else:
2721 return Naming.empty_tuple
2723 def compile_time_value(self, denv):
2724 values = self.compile_time_value_list(denv)
2725 try:
2726 return tuple(values)
2727 except Exception, e:
2728 self.compile_time_value_error(e)
2730 def generate_operation_code(self, code):
2731 if len(self.args) == 0:
2732 # result_code is Naming.empty_tuple
2733 return
2734 code.putln(
2735 "%s = PyTuple_New(%s); %s" % (
2736 self.result(),
2737 len(self.args),
2738 code.error_goto_if_null(self.result(), self.pos)))
2739 for i in range(len(self.args)):
2740 arg = self.args[i]
2741 if not arg.result_in_temp():
2742 code.put_incref(arg.result(), arg.ctype())
2743 code.putln(
2744 "PyTuple_SET_ITEM(%s, %s, %s);" % (
2745 self.result(),
2746 i,
2747 arg.py_result()))
2749 def generate_subexpr_disposal_code(self, code):
2750 # We call generate_post_assignment_code here instead
2751 # of generate_disposal_code, because values were stored
2752 # in the tuple using a reference-stealing operation.
2753 for arg in self.args:
2754 arg.generate_post_assignment_code(code)
2757 class ListNode(SequenceNode):
2758 # List constructor.
2760 # obj_conversion_errors [PyrexError] used internally
2761 # orignial_args [ExprNode] used internally
2763 gil_message = "Constructing Python list"
2765 def analyse_expressions(self, env):
2766 ExprNode.analyse_expressions(self, env)
2767 self.coerce_to_pyobject(env)
2769 def analyse_types(self, env):
2770 hold_errors()
2771 self.original_args = list(self.args)
2772 SequenceNode.analyse_types(self, env)
2773 self.type = list_type
2774 self.obj_conversion_errors = held_errors()
2775 release_errors(ignore=True)
2777 def coerce_to(self, dst_type, env):
2778 if dst_type.is_pyobject:
2779 for err in self.obj_conversion_errors:
2780 report_error(err)
2781 self.obj_conversion_errors = []
2782 if not self.type.subtype_of(dst_type):
2783 error(self.pos, "Cannot coerce list to type '%s'" % dst_type)
2784 elif dst_type.is_ptr:
2785 base_type = dst_type.base_type
2786 self.type = PyrexTypes.CArrayType(base_type, len(self.args))
2787 for i in range(len(self.original_args)):
2788 arg = self.args[i]
2789 if isinstance(arg, CoerceToPyTypeNode):
2790 arg = arg.arg
2791 self.args[i] = arg.coerce_to(base_type, env)
2792 elif dst_type.is_struct:
2793 if len(self.args) > len(dst_type.scope.var_entries):
2794 error(self.pos, "Too may members for '%s'" % dst_type)
2795 else:
2796 if len(self.args) < len(dst_type.scope.var_entries):
2797 warning(self.pos, "Too few members for '%s'" % dst_type, 1)
2798 for i, (arg, member) in enumerate(zip(self.original_args, dst_type.scope.var_entries)):
2799 if isinstance(arg, CoerceToPyTypeNode):
2800 arg = arg.arg
2801 self.args[i] = arg.coerce_to(member.type, env)
2802 self.type = dst_type
2803 else:
2804 self.type = error_type
2805 error(self.pos, "Cannot coerce list to type '%s'" % dst_type)
2806 return self
2808 def release_temp(self, env):
2809 if self.type.is_array:
2810 # To be valid C++, we must allocate the memory on the stack
2811 # manually and be sure not to reuse it for something else.
2812 pass
2813 else:
2814 SequenceNode.release_temp(self, env)
2816 def compile_time_value(self, denv):
2817 return self.compile_time_value_list(denv)
2819 def generate_operation_code(self, code):
2820 if self.type.is_pyobject:
2821 for err in self.obj_conversion_errors:
2822 report_error(err)
2823 code.putln("%s = PyList_New(%s); %s" %
2824 (self.result(),
2825 len(self.args),
2826 code.error_goto_if_null(self.result(), self.pos)))
2827 for i in range(len(self.args)):
2828 arg = self.args[i]
2829 #if not arg.is_temp:
2830 if not arg.result_in_temp():
2831 code.put_incref(arg.result(), arg.ctype())
2832 code.putln("PyList_SET_ITEM(%s, %s, %s);" %
2833 (self.result(),
2834 i,
2835 arg.py_result()))
2836 elif self.type.is_array:
2837 for i, arg in enumerate(self.args):
2838 code.putln("%s[%s] = %s;" % (
2839 self.result(),
2840 i,
2841 arg.result()))
2842 elif self.type.is_struct:
2843 for arg, member in zip(self.args, self.type.scope.var_entries):
2844 code.putln("%s.%s = %s;" % (
2845 self.result(),
2846 member.cname,
2847 arg.result()))
2848 else:
2849 raise InternalError("List type never specified")
2851 def generate_subexpr_disposal_code(self, code):
2852 # We call generate_post_assignment_code here instead
2853 # of generate_disposal_code, because values were stored
2854 # in the list using a reference-stealing operation.
2855 for arg in self.args:
2856 arg.generate_post_assignment_code(code)
2859 class ListComprehensionNode(SequenceNode):
2861 subexprs = []
2862 is_sequence_constructor = 0 # not unpackable
2864 child_attrs = ["loop", "append"]
2866 def analyse_types(self, env):
2867 self.type = list_type
2868 self.is_temp = 1
2869 self.append.target = self # this is a CloneNode used in the PyList_Append in the inner loop
2871 def allocate_temps(self, env, result = None):
2872 if debug_temp_alloc:
2873 print("%s Allocating temps" % self)
2874 self.allocate_temp(env, result)
2875 self.loop.analyse_declarations(env)
2876 self.loop.analyse_expressions(env)
2878 def generate_operation_code(self, code):
2879 code.putln("%s = PyList_New(%s); %s" %
2880 (self.result(),
2881 0,
2882 code.error_goto_if_null(self.result(), self.pos)))
2883 self.loop.generate_execution_code(code)
2885 def annotate(self, code):
2886 self.loop.annotate(code)
2889 class ListComprehensionAppendNode(ExprNode):
2891 # Need to be careful to avoid infinite recursion:
2892 # target must not be in child_attrs/subexprs
2893 subexprs = ['expr']
2895 def analyse_types(self, env):
2896 self.expr.analyse_types(env)
2897 if self.expr.type != py_object_type:
2898 self.expr = self.expr.coerce_to_pyobject(env)
2899 self.type = PyrexTypes.c_int_type
2900 self.is_temp = 1
2902 def generate_result_code(self, code):
2903 code.putln("%s = PyList_Append(%s, (PyObject*)%s); %s" %
2904 (self.result(),
2905 self.target.result(),
2906 self.expr.result(),
2907 code.error_goto_if(self.result(), self.pos)))
2910 class DictNode(ExprNode):
2911 # Dictionary constructor.
2913 # key_value_pairs [DictItemNode]
2915 # obj_conversion_errors [PyrexError] used internally
2917 subexprs = ['key_value_pairs']
2919 def compile_time_value(self, denv):
2920 pairs = [(item.key.compile_time_value(denv), item.value.compile_time_value(denv))
2921 for item in self.key_value_pairs]
2922 try:
2923 return dict(pairs)
2924 except Exception, e:
2925 self.compile_time_value_error(e)
2927 def analyse_types(self, env):
2928 hold_errors()
2929 self.type = dict_type
2930 for item in self.key_value_pairs:
2931 item.analyse_types(env)
2932 self.gil_check(env)
2933 self.obj_conversion_errors = held_errors()
2934 release_errors(ignore=True)
2935 self.is_temp = 1
2937 def coerce_to(self, dst_type, env):
2938 if dst_type.is_pyobject:
2939 self.release_errors()
2940 if not self.type.subtype_of(dst_type):
2941 error(self.pos, "Cannot interpret dict as type '%s'" % dst_type)
2942 elif dst_type.is_struct_or_union:
2943 self.type = dst_type
2944 if not dst_type.is_struct and len(self.key_value_pairs) != 1:
2945 error(self.pos, "Exactly one field must be specified to convert to union '%s'" % dst_type)
2946 elif dst_type.is_struct and len(self.key_value_pairs) < len(dst_type.scope.var_entries):
2947 warning(self.pos, "Not all members given for struct '%s'" % dst_type, 1)
2948 for item in self.key_value_pairs:
2949 if isinstance(item.key, CoerceToPyTypeNode):
2950 item.key = item.key.arg
2951 if not isinstance(item.key, (StringNode, IdentifierStringNode)):
2952 error(item.key.pos, "Invalid struct field identifier")
2953 item.key = IdentifierStringNode(item.key.pos, value="<error>")
2954 else:
2955 member = dst_type.scope.lookup_here(item.key.value)
2956 if not member:
2957 error(item.key.pos, "struct '%s' has no field '%s'" % (dst_type, item.key.value))
2958 else:
2959 value = item.value
2960 if isinstance(value, CoerceToPyTypeNode):
2961 value = value.arg
2962 item.value = value.coerce_to(member.type, env)
2963 else:
2964 self.type = error_type
2965 error(self.pos, "Cannot interpret dict as type '%s'" % dst_type)
2966 return self
2968 def release_errors(self):
2969 for err in self.obj_conversion_errors:
2970 report_error(err)
2971 self.obj_conversion_errors = []
2973 gil_message = "Constructing Python dict"
2975 def allocate_temps(self, env, result = None):
2976 # Custom method used here because key-value
2977 # pairs are evaluated and used one at a time.
2978 self.allocate_temp(env, result)
2979 for item in self.key_value_pairs:
2980 item.key.allocate_temps(env)
2981 item.value.allocate_temps(env)
2982 item.key.release_temp(env)
2983 item.value.release_temp(env)
2985 def generate_evaluation_code(self, code):
2986 # Custom method used here because key-value
2987 # pairs are evaluated and used one at a time.
2988 if self.type.is_pyobject:
2989 self.release_errors()
2990 code.putln(
2991 "%s = PyDict_New(); %s" % (
2992 self.result(),
2993 code.error_goto_if_null(self.result(), self.pos)))
2994 for item in self.key_value_pairs:
2995 item.generate_evaluation_code(code)
2996 if self.type.is_pyobject:
2997 code.put_error_if_neg(self.pos,
2998 "PyDict_SetItem(%s, %s, %s)" % (
2999 self.result(),
3000 item.key.py_result(),
3001 item.value.py_result()))
3002 else:
3003 code.putln("%s.%s = %s;" % (
3004 self.result(),
3005 item.key.value,
3006 item.value.result()))
3007 item.generate_disposal_code(code)
3009 def annotate(self, code):
3010 for item in self.key_value_pairs:
3011 item.annotate(code)
3013 class DictItemNode(ExprNode):
3014 # Represents a single item in a DictNode
3016 # key ExprNode
3017 # value ExprNode
3018 subexprs = ['key', 'value']
3020 def analyse_types(self, env):
3021 self.key.analyse_types(env)
3022 self.value.analyse_types(env)
3023 self.key = self.key.coerce_to_pyobject(env)
3024 self.value = self.value.coerce_to_pyobject(env)
3026 def generate_evaluation_code(self, code):
3027 self.key.generate_evaluation_code(code)
3028 self.value.generate_evaluation_code(code)
3030 def generate_disposal_code(self, code):
3031 self.key.generate_disposal_code(code)
3032 self.value.generate_disposal_code(code)
3034 def __iter__(self):
3035 return iter([self.key, self.value])
3038 class ClassNode(ExprNode):
3039 # Helper class used in the implementation of Python
3040 # class definitions. Constructs a class object given
3041 # a name, tuple of bases and class dictionary.
3043 # name EncodedString Name of the class
3044 # cname string Class name as a Python string
3045 # bases ExprNode Base class tuple
3046 # dict ExprNode Class dict (not owned by this node)
3047 # doc ExprNode or None Doc string
3048 # module_name string Name of defining module
3050 subexprs = ['bases', 'doc']
3052 def analyse_types(self, env):
3053 self.cname = env.intern_identifier(self.name)
3054 self.bases.analyse_types(env)
3055 if self.doc:
3056 self.doc.analyse_types(env)
3057 self.doc = self.doc.coerce_to_pyobject(env)
3058 self.module_name = env.global_scope().qualified_name
3059 self.type = py_object_type
3060 self.gil_check(env)
3061 self.is_temp = 1
3062 env.use_utility_code(create_class_utility_code);
3064 gil_message = "Constructing Python class"
3066 def generate_result_code(self, code):
3067 if self.doc:
3068 code.put_error_if_neg(self.pos,
3069 'PyDict_SetItemString(%s, "__doc__", %s)' % (
3070 self.dict.py_result(),
3071 self.doc.py_result()))
3072 code.putln(
3073 '%s = __Pyx_CreateClass(%s, %s, %s, "%s"); %s' % (
3074 self.result(),
3075 self.bases.py_result(),
3076 self.dict.py_result(),
3077 self.cname,
3078 self.module_name,
3079 code.error_goto_if_null(self.result(), self.pos)))
3082 class UnboundMethodNode(ExprNode):
3083 # Helper class used in the implementation of Python
3084 # class definitions. Constructs an unbound method
3085 # object from a class and a function.
3087 # class_cname string C var holding the class object
3088 # function ExprNode Function object
3090 subexprs = ['function']
3092 def analyse_types(self, env):
3093 self.function.analyse_types(env)
3094 self.type = py_object_type
3095 self.gil_check(env)
3096 self.is_temp = 1
3098 gil_message = "Constructing an unbound method"
3100 def generate_result_code(self, code):
3101 code.putln(
3102 "%s = PyMethod_New(%s, 0, %s); %s" % (
3103 self.result(),
3104 self.function.py_result(),
3105 self.class_cname,
3106 code.error_goto_if_null(self.result(), self.pos)))
3109 class PyCFunctionNode(AtomicExprNode):
3110 # Helper class used in the implementation of Python
3111 # class definitions. Constructs a PyCFunction object
3112 # from a PyMethodDef struct.
3114 # pymethdef_cname string PyMethodDef structure
3116 def analyse_types(self, env):
3117 self.type = py_object_type
3118 self.gil_check(env)
3119 self.is_temp = 1
3121 gil_message = "Constructing Python function"
3123 def generate_result_code(self, code):
3124 code.putln(
3125 "%s = PyCFunction_New(&%s, 0); %s" % (
3126 self.result(),
3127 self.pymethdef_cname,
3128 code.error_goto_if_null(self.result(), self.pos)))
3130 #-------------------------------------------------------------------
3132 # Unary operator nodes
3134 #-------------------------------------------------------------------
3136 compile_time_unary_operators = {
3137 'not': operator.not_,
3138 '~': operator.inv,
3139 '-': operator.neg,
3140 '+': operator.pos,
3143 class UnopNode(ExprNode):
3144 # operator string
3145 # operand ExprNode
3147 # Processing during analyse_expressions phase:
3149 # analyse_c_operation
3150 # Called when the operand is not a pyobject.
3151 # - Check operand type and coerce if needed.
3152 # - Determine result type and result code fragment.
3153 # - Allocate temporary for result if needed.
3155 subexprs = ['operand']
3157 def compile_time_value(self, denv):
3158 func = compile_time_unary_operators.get(self.operator)
3159 if not func:
3160 error(self.pos,
3161 "Unary '%s' not supported in compile-time expression"
3162 % self.operator)
3163 operand = self.operand.compile_time_value(denv)
3164 try:
3165 return func(operand)
3166 except Exception, e:
3167 self.compile_time_value_error(e)
3169 def analyse_types(self, env):
3170 self.operand.analyse_types(env)
3171 if self.is_py_operation():
3172 self.coerce_operand_to_pyobject(env)
3173 self.type = py_object_type
3174 self.gil_check(env)
3175 self.is_temp = 1
3176 else:
3177 self.analyse_c_operation(env)
3179 def check_const(self):
3180 self.operand.check_const()
3182 def is_py_operation(self):
3183 return self.operand.type.is_pyobject
3185 def coerce_operand_to_pyobject(self, env):
3186 self.operand = self.operand.coerce_to_pyobject(env)
3188 def generate_result_code(self, code):
3189 if self.operand.type.is_pyobject:
3190 self.generate_py_operation_code(code)
3191 else:
3192 if self.is_temp:
3193 self.generate_c_operation_code(code)
3195 def generate_py_operation_code(self, code):
3196 function = self.py_operation_function()
3197 code.putln(
3198 "%s = %s(%s); %s" % (
3199 self.result(),
3200 function,
3201 self.operand.py_result(),
3202 code.error_goto_if_null(self.result(), self.pos)))
3204 def type_error(self):
3205 if not self.operand.type.is_error:
3206 error(self.pos, "Invalid operand type for '%s' (%s)" %
3207 (self.operator, self.operand.type))
3208 self.type = PyrexTypes.error_type
3211 class NotNode(ExprNode):
3212 # 'not' operator
3214 # operand ExprNode
3216 def compile_time_value(self, denv):
3217 operand = self.operand.compile_time_value(denv)
3218 try:
3219 return not operand
3220 except Exception, e:
3221 self.compile_time_value_error(e)
3223 subexprs = ['operand']
3225 def analyse_types(self, env):
3226 self.operand.analyse_types(env)
3227 self.operand = self.operand.coerce_to_boolean(env)
3228 self.type = PyrexTypes.c_bint_type
3230 def calculate_result_code(self):
3231 return "(!%s)" % self.operand.result()
3233 def generate_result_code(self, code):
3234 pass
3237 class UnaryPlusNode(UnopNode):
3238 # unary '+' operator
3240 operator = '+'
3242 def analyse_c_operation(self, env):
3243 self.type = self.operand.type
3245 def py_operation_function(self):
3246 return "PyNumber_Positive"
3248 def calculate_result_code(self):
3249 return self.operand.result()
3252 class UnaryMinusNode(UnopNode):
3253 # unary '-' operator
3255 operator = '-'
3257 def analyse_c_operation(self, env):
3258 if self.operand.type.is_numeric:
3259 self.type = self.operand.type
3260 else:
3261 self.type_error()
3263 def py_operation_function(self):
3264 return "PyNumber_Negative"
3266 def calculate_result_code(self):
3267 return "(-%s)" % self.operand.result()
3270 class TildeNode(UnopNode):
3271 # unary '~' operator
3273 def analyse_c_operation(self, env):
3274 if self.operand.type.is_int:
3275 self.type = self.operand.type
3276 else:
3277 self.type_error()
3279 def py_operation_function(self):
3280 return "PyNumber_Invert"
3282 def calculate_result_code(self):
3283 return "(~%s)" % self.operand.result()
3286 class AmpersandNode(ExprNode):
3287 # The C address-of operator.
3289 # operand ExprNode
3291 subexprs = ['operand']
3293 def analyse_types(self, env):
3294 self.operand.analyse_types(env)
3295 argtype = self.operand.type
3296 if not (argtype.is_cfunction or self.operand.is_lvalue()):
3297 self.error("Taking address of non-lvalue")
3298 return
3299 if argtype.is_pyobject:
3300 self.error("Cannot take address of Python variable")
3301 return
3302 self.type = PyrexTypes.c_ptr_type(argtype)
3304 def check_const(self):
3305 self.operand.check_const_addr()
3307 def error(self, mess):
3308 error(self.pos, mess)
3309 self.type = PyrexTypes.error_type
3310 self.result_code = "<error>"
3312 def calculate_result_code(self):
3313 return "(&%s)" % self.operand.result()
3315 def generate_result_code(self, code):
3316 pass
3319 unop_node_classes = {
3320 "+": UnaryPlusNode,
3321 "-": UnaryMinusNode,
3322 "~": TildeNode,
3325 def unop_node(pos, operator, operand):
3326 # Construct unnop node of appropriate class for
3327 # given operator.
3328 if isinstance(operand, IntNode) and operator == '-':
3329 return IntNode(pos = operand.pos, value = str(-int(operand.value, 0)))
3330 elif isinstance(operand, UnopNode) and operand.operator == operator:
3331 warning(pos, "Python has no increment/decrement operator: %s%sx = %s(%sx) = x" % ((operator,)*4), 5)
3332 return unop_node_classes[operator](pos,
3333 operator = operator,
3334 operand = operand)
3337 class TypecastNode(ExprNode):
3338 # C type cast
3340 # operand ExprNode
3341 # base_type CBaseTypeNode
3342 # declarator CDeclaratorNode
3344 # If used from a transform, one can if wanted specify the attribute
3345 # "type" directly and leave base_type and declarator to None
3347 subexprs = ['operand']
3348 base_type = declarator = type = None
3350 def analyse_types(self, env):
3351 if self.type is None:
3352 base_type = self.base_type.analyse(env)
3353 _, self.type = self.declarator.analyse(base_type, env)
3354 if self.type.is_cfunction:
3355 error(self.pos,
3356 "Cannot cast to a function type")
3357 self.type = PyrexTypes.error_type
3358 self.operand.analyse_types(env)
3359 to_py = self.type.is_pyobject
3360 from_py = self.operand.type.is_pyobject
3361 if from_py and not to_py and self.operand.is_ephemeral() and not self.type.is_numeric:
3362 error(self.pos, "Casting temporary Python object to non-numeric non-Python type")
3363 if to_py and not from_py:
3364 if (self.operand.type.to_py_function and
3365 self.operand.type.create_convert_utility_code(env)):
3366 self.result_ctype = py_object_type
3367 self.operand = self.operand.coerce_to_pyobject(env)
3368 else:
3369 warning(self.pos, "No conversion from %s to %s, python object pointer used." % (self.operand.type, self.type))
3370 self.operand = self.operand.coerce_to_simple(env)
3371 elif from_py and not to_py:
3372 if self.type.from_py_function:
3373 self.operand = self.operand.coerce_to(self.type, env)
3374 else:
3375 warning(self.pos, "No conversion from %s to %s, python object pointer used." % (self.type, self.operand.type))
3376 elif from_py and to_py:
3377 if self.typecheck and self.type.is_extension_type:
3378 self.operand = PyTypeTestNode(self.operand, self.type, env)
3380 def check_const(self):
3381 self.operand.check_const()
3383 def calculate_result_code(self):
3384 opnd = self.operand
3385 return self.type.cast_code(opnd.result())
3387 def result_as(self, type):
3388 if self.type.is_pyobject and not self.is_temp:
3389 # Optimise away some unnecessary casting
3390 return self.operand.result_as(type)
3391 else:
3392 return ExprNode.result_as(self, type)
3394 def generate_result_code(self, code):
3395 if self.is_temp:
3396 code.putln(
3397 "%s = (PyObject *)%s;" % (
3398 self.result(),
3399 self.operand.result()))
3400 code.put_incref(self.result(), self.ctype())
3403 class SizeofNode(ExprNode):
3404 # Abstract base class for sizeof(x) expression nodes.
3406 type = PyrexTypes.c_int_type
3408 def check_const(self):
3409 pass
3411 def generate_result_code(self, code):
3412 pass
3415 class SizeofTypeNode(SizeofNode):
3416 # C sizeof function applied to a type
3418 # base_type CBaseTypeNode
3419 # declarator CDeclaratorNode
3421 subexprs = []
3422 arg_type = None
3424 def analyse_types(self, env):
3425 # we may have incorrectly interpreted a dotted name as a type rather than an attribute
3426 # this could be better handled by more uniformly treating types as runtime-available objects
3427 if 0 and self.base_type.module_path:
3428 path = self.base_type.module_path
3429 obj = env.lookup(path[0])
3430 if obj.as_module is None:
3431 operand = NameNode(pos=self.pos, name=path[0])
3432 for attr in path[1:]:
3433 operand = AttributeNode(pos=self.pos, obj=operand, attribute=attr)
3434 operand = AttributeNode(pos=self.pos, obj=operand, attribute=self.base_type.name)
3435 self.operand = operand
3436 self.__class__ = SizeofVarNode
3437 self.analyse_types(env)
3438 return
3439 if self.arg_type is None:
3440 base_type = self.base_type.analyse(env)
3441 _, arg_type = self.declarator.analyse(base_type, env)
3442 self.arg_type = arg_type
3443 self.check_type()
3445 def check_type(self):
3446 arg_type = self.arg_type
3447 if arg_type.is_pyobject and not arg_type.is_extension_type:
3448 error(self.pos, "Cannot take sizeof Python object")
3449 elif arg_type.is_void:
3450 error(self.pos, "Cannot take sizeof void")
3451 elif not arg_type.is_complete():
3452 error(self.pos, "Cannot take sizeof incomplete type '%s'" % arg_type)
3454 def calculate_result_code(self):
3455 if self.arg_type.is_extension_type:
3456 # the size of the pointer is boring
3457 # we want the size of the actual struct
3458 arg_code = self.arg_type.declaration_code("", deref=1)
3459 else:
3460 arg_code = self.arg_type.declaration_code("")
3461 return "(sizeof(%s))" % arg_code
3464 class SizeofVarNode(SizeofNode):
3465 # C sizeof function applied to a variable
3467 # operand ExprNode
3469 subexprs = ['operand']
3471 def analyse_types(self, env):
3472 # We may actually be looking at a type rather than a variable...
3473 # If we are, traditional analysis would fail...
3474 operand_as_type = self.operand.analyse_as_type(env)
3475 if operand_as_type:
3476 self.arg_type = operand_as_type
3477 self.__class__ = SizeofTypeNode
3478 self.check_type()
3479 else:
3480 self.operand.analyse_types(env)
3482 def calculate_result_code(self):
3483 return "(sizeof(%s))" % self.operand.result()
3485 def generate_result_code(self, code):
3486 pass
3489 #-------------------------------------------------------------------
3491 # Binary operator nodes
3493 #-------------------------------------------------------------------
3495 def _not_in(x, seq):
3496 return x not in seq
3498 compile_time_binary_operators = {
3499 '<': operator.lt,
3500 '<=': operator.le,
3501 '==': operator.eq,
3502 '!=': operator.ne,
3503 '>=': operator.ge,
3504 '>': operator.gt,
3505 'is': operator.is_,
3506 'is_not': operator.is_not,
3507 '+': operator.add,
3508 '&': operator.and_,
3509 '/': operator.div,
3510 '//': operator.floordiv,
3511 '<<': operator.lshift,
3512 '%': operator.mod,
3513 '*': operator.mul,
3514 '|': operator.or_,
3515 '**': operator.pow,
3516 '>>': operator.rshift,
3517 '-': operator.sub,
3518 #'/': operator.truediv,
3519 '^': operator.xor,
3520 'in': operator.contains,
3521 'not_in': _not_in,
3524 def get_compile_time_binop(node):
3525 func = compile_time_binary_operators.get(node.operator)
3526 if not func:
3527 error(node.pos,
3528 "Binary '%s' not supported in compile-time expression"
3529 % node.operator)
3530 return func
3532 class BinopNode(NewTempExprNode):
3533 # operator string
3534 # operand1 ExprNode
3535 # operand2 ExprNode
3537 # Processing during analyse_expressions phase:
3539 # analyse_c_operation
3540 # Called when neither operand is a pyobject.
3541 # - Check operand types and coerce if needed.
3542 # - Determine result type and result code fragment.
3543 # - Allocate temporary for result if needed.
3545 subexprs = ['operand1', 'operand2']
3547 def compile_time_value(self, denv):
3548 func = get_compile_time_binop(self)
3549 operand1 = self.operand1.compile_time_value(denv)
3550 operand2 = self.operand2.compile_time_value(denv)
3551 try:
3552 return func(operand1, operand2)
3553 except Exception, e:
3554 self.compile_time_value_error(e)
3556 def analyse_types(self, env):
3557 self.operand1.analyse_types(env)
3558 self.operand2.analyse_types(env)
3559 if self.is_py_operation():
3560 self.coerce_operands_to_pyobjects(env)
3561 self.type = py_object_type
3562 self.gil_check(env)
3563 self.is_temp = 1
3564 if Options.incref_local_binop and self.operand1.type.is_pyobject:
3565 self.operand1 = self.operand1.coerce_to_temp(env)
3566 else:
3567 self.analyse_c_operation(env)
3569 def is_py_operation(self):
3570 return (self.operand1.type.is_pyobject
3571 or self.operand2.type.is_pyobject)
3573 def coerce_operands_to_pyobjects(self, env):
3574 self.operand1 = self.operand1.coerce_to_pyobject(env)
3575 self.operand2 = self.operand2.coerce_to_pyobject(env)
3577 def check_const(self):
3578 self.operand1.check_const()
3579 self.operand2.check_const()
3581 def generate_result_code(self, code):
3582 #print "BinopNode.generate_result_code:", self.operand1, self.operand2 ###
3583 if self.operand1.type.is_pyobject:
3584 function = self.py_operation_function()
3585 if function == "PyNumber_Power":
3586 extra_args = ", Py_None"
3587 else:
3588 extra_args = ""
3589 code.putln(
3590 "%s = %s(%s, %s%s); %s" % (
3591 self.result(),
3592 function,
3593 self.operand1.py_result(),
3594 self.operand2.py_result(),
3595 extra_args,
3596 code.error_goto_if_null(self.result(), self.pos)))
3597 else:
3598 if self.is_temp:
3599 self.generate_c_operation_code(code)
3601 def type_error(self):
3602 if not (self.operand1.type.is_error
3603 or self.operand2.type.is_error):
3604 error(self.pos, "Invalid operand types for '%s' (%s; %s)" %
3605 (self.operator, self.operand1.type,
3606 self.operand2.type))
3607 self.type = PyrexTypes.error_type
3610 class NumBinopNode(BinopNode):
3611 # Binary operation taking numeric arguments.
3613 def analyse_c_operation(self, env):
3614 type1 = self.operand1.type
3615 type2 = self.operand2.type
3616 if self.operator == "**" and type1.is_int and type2.is_int:
3617 error(self.pos, "** with two C int types is ambiguous")
3618 self.type = error_type
3619 return
3620 self.type = self.compute_c_result_type(type1, type2)
3621 if not self.type:
3622 self.type_error()
3624 def compute_c_result_type(self, type1, type2):
3625 if self.c_types_okay(type1, type2):
3626 return PyrexTypes.widest_numeric_type(type1, type2)
3627 else:
3628 return None
3630 def c_types_okay(self, type1, type2):
3631 #print "NumBinopNode.c_types_okay:", type1, type2 ###
3632 return (type1.is_numeric or type1.is_enum) \
3633 and (type2.is_numeric or type2.is_enum)
3635 def calculate_result_code(self):
3636 return "(%s %s %s)" % (
3637 self.operand1.result(),
3638 self.operator,
3639 self.operand2.result())
3641 def py_operation_function(self):
3642 return self.py_functions[self.operator]
3644 py_functions = {
3645 "|": "PyNumber_Or",
3646 "^": "PyNumber_Xor",
3647 "&": "PyNumber_And",
3648 "<<": "PyNumber_Lshift",
3649 ">>": "PyNumber_Rshift",
3650 "+": "PyNumber_Add",
3651 "-": "PyNumber_Subtract",
3652 "*": "PyNumber_Multiply",
3653 "/": "__Pyx_PyNumber_Divide",
3654 "//": "PyNumber_FloorDivide",
3655 "%": "PyNumber_Remainder",
3656 "**": "PyNumber_Power"
3660 class IntBinopNode(NumBinopNode):
3661 # Binary operation taking integer arguments.
3663 def c_types_okay(self, type1, type2):
3664 #print "IntBinopNode.c_types_okay:", type1, type2 ###
3665 return (type1.is_int or type1.is_enum) \
3666 and (type2.is_int or type2.is_enum)
3669 class AddNode(NumBinopNode):
3670 # '+' operator.
3672 def is_py_operation(self):
3673 if self.operand1.type.is_string \
3674 and self.operand2.type.is_string:
3675 return 1
3676 else:
3677 return NumBinopNode.is_py_operation(self)
3679 def compute_c_result_type(self, type1, type2):
3680 #print "AddNode.compute_c_result_type:", type1, self.operator, type2 ###
3681 if (type1.is_ptr or type1.is_array) and (type2.is_int or type2.is_enum):
3682 return type1
3683 elif (type2.is_ptr or type2.is_array) and (type1.is_int or type1.is_enum):
3684 return type2
3685 else:
3686 return NumBinopNode.compute_c_result_type(
3687 self, type1, type2)
3690 class SubNode(NumBinopNode):
3691 # '-' operator.
3693 def compute_c_result_type(self, type1, type2):
3694 if (type1.is_ptr or type1.is_array) and (type2.is_int or type2.is_enum):
3695 return type1
3696 elif (type1.is_ptr or type1.is_array) and (type2.is_ptr or type2.is_array):
3697 return PyrexTypes.c_int_type
3698 else:
3699 return NumBinopNode.compute_c_result_type(
3700 self, type1, type2)
3703 class MulNode(NumBinopNode):
3704 # '*' operator.
3706 def is_py_operation(self):
3707 type1 = self.operand1.type
3708 type2 = self.operand2.type
3709 if (type1.is_string and type2.is_int) \
3710 or (type2.is_string and type1.is_int):
3711 return 1
3712 else:
3713 return NumBinopNode.is_py_operation(self)
3716 class FloorDivNode(NumBinopNode):
3717 # '//' operator.
3719 def calculate_result_code(self):
3720 return "(%s %s %s)" % (
3721 self.operand1.result(),
3722 "/", # c division is by default floor-div
3723 self.operand2.result())
3726 class ModNode(NumBinopNode):
3727 # '%' operator.
3729 def is_py_operation(self):
3730 return (self.operand1.type.is_string
3731 or self.operand2.type.is_string
3732 or NumBinopNode.is_py_operation(self))
3734 def calculate_result_code(self):
3735 if self.operand1.type.is_float or self.operand2.type.is_float:
3736 return "fmod(%s, %s)" % (
3737 self.operand1.result(),
3738 self.operand2.result())
3739 else:
3740 return "(%s %% %s)" % (
3741 self.operand1.result(),
3742 self.operand2.result())
3744 class PowNode(NumBinopNode):
3745 # '**' operator.
3747 def compute_c_result_type(self, type1, type2):
3748 if self.c_types_okay(type1, type2):
3749 return PyrexTypes.c_double_type
3750 else:
3751 return None
3753 def c_types_okay(self, type1, type2):
3754 return (type1.is_float or type2.is_float) and \
3755 NumBinopNode.c_types_okay(self, type1, type2)
3757 def type_error(self):
3758 if not (self.operand1.type.is_error or self.operand2.type.is_error):
3759 if self.operand1.type.is_int and self.operand2.type.is_int:
3760 error(self.pos, "C has no integer powering, use python ints or floats instead '%s' (%s; %s)" %
3761 (self.operator, self.operand1.type, self.operand2.type))
3762 else:
3763 NumBinopNode.type_error(self)
3764 self.type = PyrexTypes.error_type
3766 def calculate_result_code(self):
3767 return "pow(%s, %s)" % (
3768 self.operand1.result(), self.operand2.result())
3771 class BoolBinopNode(ExprNode):
3772 # Short-circuiting boolean operation.
3774 # operator string
3775 # operand1 ExprNode
3776 # operand2 ExprNode
3777 # temp_bool ExprNode used internally
3779 temp_bool = None
3781 subexprs = ['operand1', 'operand2', 'temp_bool']
3783 def compile_time_value(self, denv):
3784 if self.operator == 'and':
3785 return self.operand1.compile_time_value(denv) \
3786 and self.operand2.compile_time_value(denv)
3787 else:
3788 return self.operand1.compile_time_value(denv) \
3789 or self.operand2.compile_time_value(denv)
3791 def coerce_to_boolean(self, env):
3792 self.operand1 = self.operand1.coerce_to_boolean(env)
3793 self.operand2 = self.operand2.coerce_to_boolean(env)
3794 self.type = PyrexTypes.c_bint_type
3795 return self
3797 def analyse_types(self, env):
3798 self.operand1.analyse_types(env)
3799 self.operand2.analyse_types(env)
3800 if self.operand1.type.is_pyobject or \
3801 self.operand2.type.is_pyobject:
3802 self.operand1 = self.operand1.coerce_to_pyobject(env)
3803 self.operand2 = self.operand2.coerce_to_pyobject(env)
3804 self.temp_bool = TempNode(self.pos, PyrexTypes.c_bint_type, env)
3805 self.type = py_object_type
3806 self.gil_check(env)
3807 else:
3808 self.operand1 = self.operand1.coerce_to_boolean(env)
3809 self.operand2 = self.operand2.coerce_to_boolean(env)
3810 self.type = PyrexTypes.c_bint_type
3811 # For what we're about to do, it's vital that
3812 # both operands be temp nodes.
3813 self.operand1 = self.operand1.coerce_to_temp(env) #CTT
3814 self.operand2 = self.operand2.coerce_to_temp(env)
3815 self.is_temp = 1
3817 gil_message = "Truth-testing Python object"
3819 def allocate_temps(self, env, result_code = None):
3820 # We don't need both operands at the same time, and
3821 # one of the operands will also be our result. So we
3822 # use an allocation strategy here which results in
3823 # this node and both its operands sharing the same
3824 # result variable. This allows us to avoid some
3825 # assignments and increfs/decrefs that would otherwise
3826 # be necessary.
3827 self.allocate_temp(env, result_code)
3828 self.operand1.allocate_temps(env, self.result())
3829 if self.temp_bool:
3830 self.temp_bool.allocate_temp(env)
3831 self.temp_bool.release_temp(env)
3832 self.operand2.allocate_temps(env, self.result())
3833 # We haven't called release_temp on either operand,
3834 # because although they are temp nodes, they don't own
3835 # their result variable. And because they are temp
3836 # nodes, any temps in their subnodes will have been
3837 # released before their allocate_temps returned.
3838 # Therefore, they contain no temp vars that need to
3839 # be released.
3841 def check_const(self):
3842 self.operand1.check_const()
3843 self.operand2.check_const()
3845 def calculate_result_code(self):
3846 return "(%s %s %s)" % (
3847 self.operand1.result(),
3848 self.py_to_c_op[self.operator],
3849 self.operand2.result())
3851 py_to_c_op = {'and': "&&", 'or': "||"}
3853 def generate_evaluation_code(self, code):
3854 self.operand1.generate_evaluation_code(code)
3855 test_result = self.generate_operand1_test(code)
3856 if self.operator == 'and':
3857 sense = ""
3858 else:
3859 sense = "!"
3860 code.putln(
3861 "if (%s%s) {" % (
3862 sense,
3863 test_result))
3864 self.operand1.generate_disposal_code(code)
3865 self.operand2.generate_evaluation_code(code)
3866 code.putln(
3867 "}")
3869 def generate_operand1_test(self, code):
3870 # Generate code to test the truth of the first operand.
3871 if self.type.is_pyobject:
3872 test_result = self.temp_bool.result()
3873 code.putln(
3874 "%s = __Pyx_PyObject_IsTrue(%s); %s" % (
3875 test_result,
3876 self.operand1.py_result(),
3877 code.error_goto_if_neg(test_result, self.pos)))
3878 else:
3879 test_result = self.operand1.result()
3880 return test_result
3883 class CondExprNode(ExprNode):
3884 # Short-circuiting conditional expression.
3886 # test ExprNode
3887 # true_val ExprNode
3888 # false_val ExprNode
3890 temp_bool = None
3891 true_val = None
3892 false_val = None
3894 subexprs = ['test', 'true_val', 'false_val']
3896 def analyse_types(self, env):
3897 self.test.analyse_types(env)
3898 self.test = self.test.coerce_to_boolean(env)
3899 self.true_val.analyse_types(env)
3900 self.false_val.analyse_types(env)
3901 self.type = self.compute_result_type(self.true_val.type, self.false_val.type)
3902 if self.true_val.type.is_pyobject or self.false_val.type.is_pyobject:
3903 self.true_val = self.true_val.coerce_to(self.type, env)
3904 self.false_val = self.false_val.coerce_to(self.type, env)
3905 # must be tmp variables so they can share a result
3906 self.true_val = self.true_val.coerce_to_temp(env)
3907 self.false_val = self.false_val.coerce_to_temp(env)
3908 self.is_temp = 1
3909 if self.type == PyrexTypes.error_type:
3910 self.type_error()
3912 def allocate_temps(self, env, result_code = None):
3913 # We only ever evaluate one side, and this is
3914 # after evaluating the truth value, so we may
3915 # use an allocation strategy here which results in
3916 # this node and both its operands sharing the same
3917 # result variable. This allows us to avoid some
3918 # assignments and increfs/decrefs that would otherwise
3919 # be necessary.
3920 self.allocate_temp(env, result_code)
3921 self.test.allocate_temps(env, result_code)
3922 self.true_val.allocate_temps(env, self.result())
3923 self.false_val.allocate_temps(env, self.result())
3924 # We haven't called release_temp on either value,
3925 # because although they are temp nodes, they don't own
3926 # their result variable. And because they are temp
3927 # nodes, any temps in their subnodes will have been
3928 # released before their allocate_temps returned.
3929 # Therefore, they contain no temp vars that need to
3930 # be released.
3932 def compute_result_type(self, type1, type2):
3933 if type1 == type2:
3934 return type1
3935 elif type1.is_numeric and type2.is_numeric:
3936 return PyrexTypes.widest_numeric_type(type1, type2)
3937 elif type1.is_extension_type and type1.subtype_of_resolved_type(type2):
3938 return type2
3939 elif type2.is_extension_type and type2.subtype_of_resolved_type(type1):
3940 return type1
3941 elif type1.is_pyobject or type2.is_pyobject:
3942 return py_object_type
3943 elif type1.assignable_from(type2):
3944 return type1
3945 elif type2.assignable_from(type1):
3946 return type2
3947 else:
3948 return PyrexTypes.error_type
3950 def type_error(self):
3951 if not (self.true_val.type.is_error or self.false_val.type.is_error):
3952 error(self.pos, "Incompatable types in conditional expression (%s; %s)" %
3953 (self.true_val.type, self.false_val.type))
3954 self.type = PyrexTypes.error_type
3956 def check_const(self):
3957 self.test.check_const()
3958 self.true_val.check_const()
3959 self.false_val.check_const()
3961 def generate_evaluation_code(self, code):
3962 self.test.generate_evaluation_code(code)
3963 code.putln("if (%s) {" % self.test.result() )
3964 self.true_val.generate_evaluation_code(code)
3965 code.putln("} else {")
3966 self.false_val.generate_evaluation_code(code)
3967 code.putln("}")
3968 self.test.generate_disposal_code(code)
3970 richcmp_constants = {
3971 "<" : "Py_LT",
3972 "<=": "Py_LE",
3973 "==": "Py_EQ",
3974 "!=": "Py_NE",
3975 "<>": "Py_NE",
3976 ">" : "Py_GT",
3977 ">=": "Py_GE",
3980 class CmpNode:
3981 # Mixin class containing code common to PrimaryCmpNodes
3982 # and CascadedCmpNodes.
3984 def cascaded_compile_time_value(self, operand1, denv):
3985 func = get_compile_time_binop(self)
3986 operand2 = self.operand2.compile_time_value(denv)
3987 try:
3988 result = func(operand1, operand2)
3989 except Exception, e:
3990 self.compile_time_value_error(e)
3991 result = None
3992 if result:
3993 cascade = self.cascade
3994 if cascade:
3995 result = result and cascade.compile_time_value(operand2, denv)
3996 return result
3998 def is_python_comparison(self):
3999 return (self.has_python_operands()
4000 or (self.cascade and self.cascade.is_python_comparison())
4001 or self.operator in ('in', 'not_in'))
4003 def is_python_result(self):
4004 return ((self.has_python_operands() and self.operator not in ('is', 'is_not', 'in', 'not_in'))
4005 or (self.cascade and self.cascade.is_python_result()))
4007 def check_types(self, env, operand1, op, operand2):
4008 if not self.types_okay(operand1, op, operand2):
4009 error(self.pos, "Invalid types for '%s' (%s, %s)" %
4010 (self.operator, operand1.type, operand2.type))
4012 def types_okay(self, operand1, op, operand2):
4013 type1 = operand1.type
4014 type2 = operand2.type
4015 if type1.is_error or type2.is_error:
4016 return 1
4017 if type1.is_pyobject: # type2 will be, too
4018 return 1
4019 elif type1.is_ptr or type1.is_array:
4020 return type1.is_null_ptr or type2.is_null_ptr \
4021 or ((type2.is_ptr or type2.is_array)
4022 and type1.base_type.same_as(type2.base_type))
4023 elif ((type1.is_numeric and type2.is_numeric
4024 or type1.is_enum and (type1 is type2 or type2.is_int)
4025 or type1.is_int and type2.is_enum)
4026 and op not in ('is', 'is_not')):
4027 return 1
4028 else:
4029 return type1.is_cfunction and type1.is_cfunction and type1 == type2
4031 def generate_operation_code(self, code, result_code,
4032 operand1, op , operand2):
4033 if self.type is PyrexTypes.py_object_type:
4034 coerce_result = "__Pyx_PyBool_FromLong"
4035 else:
4036 coerce_result = ""
4037 if 'not' in op: negation = "!"
4038 else: negation = ""
4039 if op == 'in' or op == 'not_in':
4040 code.putln(
4041 "%s = %s(%sPySequence_Contains(%s, %s)); %s" % (
4042 result_code,
4043 coerce_result,
4044 negation,
4045 operand2.py_result(),
4046 operand1.py_result(),
4047 code.error_goto_if_neg(result_code, self.pos)))
4048 elif (operand1.type.is_pyobject
4049 and op not in ('is', 'is_not')):
4050 code.putln("%s = PyObject_RichCompare(%s, %s, %s); %s" % (
4051 result_code,
4052 operand1.py_result(),
4053 operand2.py_result(),
4054 richcmp_constants[op],
4055 code.error_goto_if_null(result_code, self.pos)))
4056 else:
4057 type1 = operand1.type
4058 type2 = operand2.type
4059 if (type1.is_extension_type or type2.is_extension_type) \
4060 and not type1.same_as(type2):
4061 common_type = py_object_type
4062 elif type1.is_numeric:
4063 common_type = PyrexTypes.widest_numeric_type(type1, type2)
4064 else:
4065 common_type = type1
4066 code1 = operand1.result_as(common_type)
4067 code2 = operand2.result_as(common_type)
4068 code.putln("%s = %s(%s %s %s);" % (
4069 result_code,
4070 coerce_result,
4071 code1,
4072 self.c_operator(op),
4073 code2))
4075 def c_operator(self, op):
4076 if op == 'is':
4077 return "=="
4078 elif op == 'is_not':
4079 return "!="
4080 else:
4081 return op
4084 class PrimaryCmpNode(ExprNode, CmpNode):
4085 # Non-cascaded comparison or first comparison of
4086 # a cascaded sequence.
4088 # operator string
4089 # operand1 ExprNode
4090 # operand2 ExprNode
4091 # cascade CascadedCmpNode
4093 # We don't use the subexprs mechanism, because
4094 # things here are too complicated for it to handle.
4095 # Instead, we override all the framework methods
4096 # which use it.
4098 child_attrs = ['operand1', 'operand2', 'cascade']
4100 cascade = None
4102 def compile_time_value(self, denv):
4103 operand1 = self.operand1.compile_time_value(denv)
4104 return self.cascaded_compile_time_value(operand1, denv)
4106 def analyse_types(self, env):
4107 self.operand1.analyse_types(env)
4108 self.operand2.analyse_types(env)
4109 if self.cascade:
4110 self.cascade.analyse_types(env, self.operand2)
4111 self.is_pycmp = self.is_python_comparison()
4112 if self.is_pycmp:
4113 self.coerce_operands_to_pyobjects(env)
4114 if self.has_int_operands():
4115 self.coerce_chars_to_ints(env)
4116 if self.cascade:
4117 self.operand2 = self.operand2.coerce_to_simple(env)
4118 self.cascade.coerce_cascaded_operands_to_temp(env)
4119 self.check_operand_types(env)
4120 if self.is_python_result():
4121 self.type = PyrexTypes.py_object_type
4122 else:
4123 self.type = PyrexTypes.c_bint_type
4124 cdr = self.cascade
4125 while cdr:
4126 cdr.type = self.type
4127 cdr = cdr.cascade
4128 if self.is_pycmp or self.cascade:
4129 self.is_temp = 1
4131 def check_operand_types(self, env):
4132 self.check_types(env,
4133 self.operand1, self.operator, self.operand2)
4134 if self.cascade:
4135 self.cascade.check_operand_types(env, self.operand2)
4137 def has_python_operands(self):
4138 return (self.operand1.type.is_pyobject
4139 or self.operand2.type.is_pyobject)
4141 def coerce_operands_to_pyobjects(self, env):
4142 self.operand1 = self.operand1.coerce_to_pyobject(env)
4143 self.operand2 = self.operand2.coerce_to_pyobject(env)
4144 if self.cascade:
4145 self.cascade.coerce_operands_to_pyobjects(env)
4147 def has_int_operands(self):
4148 return (self.operand1.type.is_int or self.operand2.type.is_int) \
4149 or (self.cascade and self.cascade.has_int_operands())
4151 def coerce_chars_to_ints(self, env):
4152 # coerce literal single-char strings to c chars
4153 if self.operand1.type.is_string and isinstance(self.operand1, StringNode):
4154 self.operand1 = self.operand1.coerce_to(PyrexTypes.c_uchar_type, env)
4155 if self.operand2.type.is_string and isinstance(self.operand2, StringNode):
4156 self.operand2 = self.operand2.coerce_to(PyrexTypes.c_uchar_type, env)
4157 if self.cascade:
4158 self.cascade.coerce_chars_to_ints(env)
4160 def allocate_subexpr_temps(self, env):
4161 self.operand1.allocate_temps(env)
4162 self.operand2.allocate_temps(env)
4163 if self.cascade:
4164 self.cascade.allocate_subexpr_temps(env)
4166 def release_subexpr_temps(self, env):
4167 self.operand1.release_temp(env)
4168 self.operand2.release_temp(env)
4169 if self.cascade:
4170 self.cascade.release_subexpr_temps(env)
4172 def check_const(self):
4173 self.operand1.check_const()
4174 self.operand2.check_const()
4175 if self.cascade:
4176 self.not_const()
4178 def calculate_result_code(self):
4179 return "(%s %s %s)" % (
4180 self.operand1.result(),
4181 self.c_operator(self.operator),
4182 self.operand2.result())
4184 def generate_evaluation_code(self, code):
4185 self.operand1.generate_evaluation_code(code)
4186 self.operand2.generate_evaluation_code(code)
4187 if self.is_temp:
4188 self.generate_operation_code(code, self.result(),
4189 self.operand1, self.operator, self.operand2)
4190 if self.cascade:
4191 self.cascade.generate_evaluation_code(code,
4192 self.result(), self.operand2)
4193 self.operand1.generate_disposal_code(code)
4194 self.operand2.generate_disposal_code(code)
4196 def generate_subexpr_disposal_code(self, code):
4197 # If this is called, it is a non-cascaded cmp,
4198 # so only need to dispose of the two main operands.
4199 self.operand1.generate_disposal_code(code)
4200 self.operand2.generate_disposal_code(code)
4202 def annotate(self, code):
4203 self.operand1.annotate(code)
4204 self.operand2.annotate(code)
4205 if self.cascade:
4206 self.cascade.annotate(code)
4209 class CascadedCmpNode(Node, CmpNode):
4210 # A CascadedCmpNode is not a complete expression node. It
4211 # hangs off the side of another comparison node, shares
4212 # its left operand with that node, and shares its result
4213 # with the PrimaryCmpNode at the head of the chain.
4215 # operator string
4216 # operand2 ExprNode
4217 # cascade CascadedCmpNode
4219 child_attrs = ['operand2', 'cascade']
4221 cascade = None
4223 def analyse_types(self, env, operand1):
4224 self.operand2.analyse_types(env)
4225 if self.cascade:
4226 self.cascade.analyse_types(env, self.operand2)
4228 def check_operand_types(self, env, operand1):
4229 self.check_types(env,
4230 operand1, self.operator, self.operand2)
4231 if self.cascade:
4232 self.cascade.check_operand_types(env, self.operand2)
4234 def has_python_operands(self):
4235 return self.operand2.type.is_pyobject
4237 def coerce_operands_to_pyobjects(self, env):
4238 self.operand2 = self.operand2.coerce_to_pyobject(env)
4239 if self.cascade:
4240 self.cascade.coerce_operands_to_pyobjects(env)
4242 def has_int_operands(self):
4243 return self.operand2.type.is_int
4245 def coerce_chars_to_ints(self, env):
4246 if self.operand2.type.is_string and isinstance(self.operand2, StringNode):
4247 self.operand2 = self.operand2.coerce_to(PyrexTypes.c_uchar_type, env)
4249 def coerce_cascaded_operands_to_temp(self, env):
4250 if self.cascade:
4251 #self.operand2 = self.operand2.coerce_to_temp(env) #CTT
4252 self.operand2 = self.operand2.coerce_to_simple(env)
4253 self.cascade.coerce_cascaded_operands_to_temp(env)
4255 def allocate_subexpr_temps(self, env):
4256 self.operand2.allocate_temps(env)
4257 if self.cascade:
4258 self.cascade.allocate_subexpr_temps(env)
4260 def release_subexpr_temps(self, env):
4261 self.operand2.release_temp(env)
4262 if self.cascade:
4263 self.cascade.release_subexpr_temps(env)
4265 def generate_evaluation_code(self, code, result, operand1):
4266 if self.type.is_pyobject:
4267 code.putln("if (__Pyx_PyObject_IsTrue(%s)) {" % result)
4268 else:
4269 code.putln("if (%s) {" % result)
4270 self.operand2.generate_evaluation_code(code)
4271 self.generate_operation_code(code, result,
4272 operand1, self.operator, self.operand2)
4273 if self.cascade:
4274 self.cascade.generate_evaluation_code(
4275 code, result, self.operand2)
4276 # Cascaded cmp result is always temp
4277 self.operand2.generate_disposal_code(code)
4278 code.putln("}")
4280 def annotate(self, code):
4281 self.operand2.annotate(code)
4282 if self.cascade:
4283 self.cascade.annotate(code)
4286 binop_node_classes = {
4287 "or": BoolBinopNode,
4288 "and": BoolBinopNode,
4289 "|": IntBinopNode,
4290 "^": IntBinopNode,
4291 "&": IntBinopNode,
4292 "<<": IntBinopNode,
4293 ">>": IntBinopNode,
4294 "+": AddNode,
4295 "-": SubNode,
4296 "*": MulNode,
4297 "/": NumBinopNode,
4298 "//": FloorDivNode,
4299 "%": ModNode,
4300 "**": PowNode
4303 def binop_node(pos, operator, operand1, operand2):
4304 # Construct binop node of appropriate class for
4305 # given operator.
4306 return binop_node_classes[operator](pos,
4307 operator = operator,
4308 operand1 = operand1,
4309 operand2 = operand2)
4311 #-------------------------------------------------------------------
4313 # Coercion nodes
4315 # Coercion nodes are special in that they are created during
4316 # the analyse_types phase of parse tree processing.
4317 # Their __init__ methods consequently incorporate some aspects
4318 # of that phase.
4320 #-------------------------------------------------------------------
4322 class CoercionNode(ExprNode):
4323 # Abstract base class for coercion nodes.
4325 # arg ExprNode node being coerced
4327 subexprs = ['arg']
4329 def __init__(self, arg):
4330 self.pos = arg.pos
4331 self.arg = arg
4332 if debug_coercion:
4333 print("%s Coercing %s" % (self, self.arg))
4335 def annotate(self, code):
4336 self.arg.annotate(code)
4337 if self.arg.type != self.type:
4338 file, line, col = self.pos
4339 code.annotate((file, line, col-1), AnnotationItem(style='coerce', tag='coerce', text='[%s] to [%s]' % (self.arg.type, self.type)))
4342 class CastNode(CoercionNode):
4343 # Wrap a node in a C type cast.
4345 def __init__(self, arg, new_type):
4346 CoercionNode.__init__(self, arg)
4347 self.type = new_type
4349 def calculate_result_code(self):
4350 return self.arg.result_as(self.type)
4352 def generate_result_code(self, code):
4353 self.arg.generate_result_code(code)
4356 class PyTypeTestNode(CoercionNode):
4357 # This node is used to check that a generic Python
4358 # object is an instance of a particular extension type.
4359 # This node borrows the result of its argument node.
4361 def __init__(self, arg, dst_type, env):
4362 # The arg is know to be a Python object, and
4363 # the dst_type is known to be an extension type.
4364 assert dst_type.is_extension_type or dst_type.is_builtin_type, "PyTypeTest on non extension type"
4365 CoercionNode.__init__(self, arg)
4366 self.type = dst_type
4367 self.gil_check(env)
4368 self.result_ctype = arg.ctype()
4369 if not dst_type.is_builtin_type:
4370 env.use_utility_code(type_test_utility_code)
4372 gil_message = "Python type test"
4374 def analyse_types(self, env):
4375 pass
4377 def result_in_temp(self):
4378 return self.arg.result_in_temp()
4380 def is_ephemeral(self):
4381 return self.arg.is_ephemeral()
4383 def calculate_result_code(self):
4384 return self.arg.result()
4386 def generate_result_code(self, code):
4387 if self.type.typeobj_is_available():
4388 code.putln(
4389 "if (!(%s)) %s" % (
4390 self.type.type_test_code(self.arg.py_result()),
4391 code.error_goto(self.pos)))
4392 else:
4393 error(self.pos, "Cannot test type of extern C class "
4394 "without type object name specification")
4396 def generate_post_assignment_code(self, code):
4397 self.arg.generate_post_assignment_code(code)
4400 class CoerceToPyTypeNode(CoercionNode):
4401 # This node is used to convert a C data type
4402 # to a Python object.
4404 def __init__(self, arg, env):
4405 CoercionNode.__init__(self, arg)
4406 self.type = py_object_type
4407 self.gil_check(env)
4408 self.is_temp = 1
4409 if not arg.type.to_py_function or not arg.type.create_convert_utility_code(env):
4410 error(arg.pos,
4411 "Cannot convert '%s' to Python object" % arg.type)
4413 gil_message = "Converting to Python object"
4415 def coerce_to_boolean(self, env):
4416 return self.arg.coerce_to_boolean(env).coerce_to_temp(env)
4418 def analyse_types(self, env):
4419 # The arg is always already analysed
4420 pass
4422 def generate_result_code(self, code):
4423 function = self.arg.type.to_py_function
4424 code.putln('%s = %s(%s); %s' % (
4425 self.result(),
4426 function,
4427 self.arg.result(),
4428 code.error_goto_if_null(self.result(), self.pos)))
4431 class CoerceFromPyTypeNode(CoercionNode):
4432 # This node is used to convert a Python object
4433 # to a C data type.
4435 def __init__(self, result_type, arg, env):
4436 CoercionNode.__init__(self, arg)
4437 self.type = result_type
4438 self.is_temp = 1
4439 if not result_type.from_py_function:
4440 error(arg.pos,
4441 "Cannot convert Python object to '%s'" % result_type)
4442 if self.type.is_string and self.arg.is_ephemeral():
4443 error(arg.pos,
4444 "Obtaining char * from temporary Python value")
4446 def analyse_types(self, env):
4447 # The arg is always already analysed
4448 pass
4450 def generate_result_code(self, code):
4451 function = self.type.from_py_function
4452 operand = self.arg.py_result()
4453 rhs = "%s(%s)" % (function, operand)
4454 if self.type.is_enum:
4455 rhs = typecast(self.type, c_long_type, rhs)
4456 code.putln('%s = %s; %s' % (
4457 self.result(),
4458 rhs,
4459 code.error_goto_if(self.type.error_condition(self.result()), self.pos)))
4462 class CoerceToBooleanNode(CoercionNode):
4463 # This node is used when a result needs to be used
4464 # in a boolean context.
4466 def __init__(self, arg, env):
4467 CoercionNode.__init__(self, arg)
4468 self.type = PyrexTypes.c_bint_type
4469 if arg.type.is_pyobject:
4470 if env.nogil:
4471 self.gil_error()
4472 self.is_temp = 1
4474 gil_message = "Truth-testing Python object"
4476 def check_const(self):
4477 if self.is_temp:
4478 self.not_const()
4479 self.arg.check_const()
4481 def calculate_result_code(self):
4482 return "(%s != 0)" % self.arg.result()
4484 def generate_result_code(self, code):
4485 if self.arg.type.is_pyobject:
4486 code.putln(
4487 "%s = __Pyx_PyObject_IsTrue(%s); %s" % (
4488 self.result(),
4489 self.arg.py_result(),
4490 code.error_goto_if_neg(self.result(), self.pos)))
4493 class CoerceToTempNode(CoercionNode):
4494 # This node is used to force the result of another node
4495 # to be stored in a temporary. It is only used if the
4496 # argument node's result is not already in a temporary.
4498 def __init__(self, arg, env):
4499 CoercionNode.__init__(self, arg)
4500 self.type = self.arg.type
4501 self.is_temp = 1
4502 if self.type.is_pyobject:
4503 self.gil_check(env)
4504 self.result_ctype = py_object_type
4506 gil_message = "Creating temporary Python reference"
4508 def analyse_types(self, env):
4509 # The arg is always already analysed
4510 pass
4512 def coerce_to_boolean(self, env):
4513 self.arg = self.arg.coerce_to_boolean(env)
4514 self.type = self.arg.type
4515 self.result_ctype = self.type
4516 return self
4518 def generate_result_code(self, code):
4519 #self.arg.generate_evaluation_code(code) # Already done
4520 # by generic generate_subexpr_evaluation_code!
4521 code.putln("%s = %s;" % (
4522 self.result(), self.arg.result_as(self.ctype())))
4523 if self.type.is_pyobject:
4524 code.put_incref(self.result(), self.ctype())
4527 class CloneNode(CoercionNode):
4528 # This node is employed when the result of another node needs
4529 # to be used multiple times. The argument node's result must
4530 # be in a temporary. This node "borrows" the result from the
4531 # argument node, and does not generate any evaluation or
4532 # disposal code for it. The original owner of the argument
4533 # node is responsible for doing those things.
4535 subexprs = [] # Arg is not considered a subexpr
4537 def __init__(self, arg):
4538 CoercionNode.__init__(self, arg)
4539 if hasattr(arg, 'type'):
4540 self.type = arg.type
4541 self.result_ctype = arg.result_ctype
4542 if hasattr(arg, 'entry'):
4543 self.entry = arg.entry
4545 def result(self):
4546 return self.arg.result()
4548 def analyse_types(self, env):
4549 self.type = self.arg.type
4550 self.result_ctype = self.arg.result_ctype
4551 self.is_temp = 1
4552 if hasattr(self.arg, 'entry'):
4553 self.entry = self.arg.entry
4555 def generate_evaluation_code(self, code):
4556 pass
4558 def generate_result_code(self, code):
4559 pass
4561 def generate_disposal_code(self, code):
4562 pass
4564 def allocate_temps(self, env):
4565 pass
4567 def release_temp(self, env):
4568 pass
4570 class PersistentNode(ExprNode):
4571 # A PersistentNode is like a CloneNode except it handles the temporary
4572 # allocation itself by keeping track of the number of times it has been
4573 # used.
4575 subexprs = ["arg"]
4576 temp_counter = 0
4577 generate_counter = 0
4578 analyse_counter = 0
4579 result_code = None
4581 def __init__(self, arg, uses):
4582 self.pos = arg.pos
4583 self.arg = arg
4584 self.uses = uses
4586 def analyse_types(self, env):
4587 if self.analyse_counter == 0:
4588 self.arg.analyse_types(env)
4589 self.type = self.arg.type
4590 self.result_ctype = self.arg.result_ctype
4591 self.is_temp = 1
4592 self.analyse_counter += 1
4594 def calculate_result_code(self):
4595 return self.result()
4597 def generate_evaluation_code(self, code):
4598 if self.generate_counter == 0:
4599 self.arg.generate_evaluation_code(code)
4600 code.putln("%s = %s;" % (
4601 self.result(), self.arg.result_as(self.ctype())))
4602 if self.type.is_pyobject:
4603 code.put_incref(self.result(), self.ctype())
4604 self.arg.generate_disposal_code(code)
4605 self.generate_counter += 1
4607 def generate_disposal_code(self, code):
4608 if self.generate_counter == self.uses:
4609 if self.type.is_pyobject:
4610 code.put_decref_clear(self.result(), self.ctype())
4612 def allocate_temps(self, env, result=None):
4613 if self.temp_counter == 0:
4614 self.arg.allocate_temps(env)
4615 self.allocate_temp(env, result)
4616 self.arg.release_temp(env)
4617 self.temp_counter += 1
4619 def allocate_temp(self, env, result=None):
4620 if result is None:
4621 self.result_code = env.allocate_temp(self.type)
4622 else:
4623 self.result_code = result
4625 def release_temp(self, env):
4626 if self.temp_counter == self.uses:
4627 env.release_temp(self.result())
4629 #------------------------------------------------------------------------------------
4631 # Runtime support code
4633 #------------------------------------------------------------------------------------
4635 get_name_interned_utility_code = UtilityCode(
4636 proto = """
4637 static PyObject *__Pyx_GetName(PyObject *dict, PyObject *name); /*proto*/
4638 """,
4639 impl = """
4640 static PyObject *__Pyx_GetName(PyObject *dict, PyObject *name) {
4641 PyObject *result;
4642 result = PyObject_GetAttr(dict, name);
4643 if (!result)
4644 PyErr_SetObject(PyExc_NameError, name);
4645 return result;
4647 """)
4649 #------------------------------------------------------------------------------------
4651 import_utility_code = UtilityCode(
4652 proto = """
4653 static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list); /*proto*/
4654 """,
4655 impl = """
4656 static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list) {
4657 PyObject *__import__ = 0;
4658 PyObject *empty_list = 0;
4659 PyObject *module = 0;
4660 PyObject *global_dict = 0;
4661 PyObject *empty_dict = 0;
4662 PyObject *list;
4663 __import__ = PyObject_GetAttrString(%(BUILTINS)s, "__import__");
4664 if (!__import__)
4665 goto bad;
4666 if (from_list)
4667 list = from_list;
4668 else {
4669 empty_list = PyList_New(0);
4670 if (!empty_list)
4671 goto bad;
4672 list = empty_list;
4674 global_dict = PyModule_GetDict(%(GLOBALS)s);
4675 if (!global_dict)
4676 goto bad;
4677 empty_dict = PyDict_New();
4678 if (!empty_dict)
4679 goto bad;
4680 module = PyObject_CallFunction(__import__, "OOOO",
4681 name, global_dict, empty_dict, list);
4682 bad:
4683 Py_XDECREF(empty_list);
4684 Py_XDECREF(__import__);
4685 Py_XDECREF(empty_dict);
4686 return module;
4688 """ % {
4689 "BUILTINS": Naming.builtins_cname,
4690 "GLOBALS": Naming.module_cname,
4691 })
4693 #------------------------------------------------------------------------------------
4695 get_exception_utility_code = UtilityCode(
4696 proto = """
4697 static PyObject *__Pyx_GetExcValue(void); /*proto*/
4698 """,
4699 impl = """
4700 static PyObject *__Pyx_GetExcValue(void) {
4701 PyObject *type = 0, *value = 0, *tb = 0;
4702 PyObject *tmp_type, *tmp_value, *tmp_tb;
4703 PyObject *result = 0;
4704 PyThreadState *tstate = PyThreadState_Get();
4705 PyErr_Fetch(&type, &value, &tb);
4706 PyErr_NormalizeException(&type, &value, &tb);
4707 if (PyErr_Occurred())
4708 goto bad;
4709 if (!value) {
4710 value = Py_None;
4711 Py_INCREF(value);
4713 tmp_type = tstate->exc_type;
4714 tmp_value = tstate->exc_value;
4715 tmp_tb = tstate->exc_traceback;
4716 tstate->exc_type = type;
4717 tstate->exc_value = value;
4718 tstate->exc_traceback = tb;
4719 /* Make sure tstate is in a consistent state when we XDECREF
4720 these objects (XDECREF may run arbitrary code). */
4721 Py_XDECREF(tmp_type);
4722 Py_XDECREF(tmp_value);
4723 Py_XDECREF(tmp_tb);
4724 result = value;
4725 Py_XINCREF(result);
4726 type = 0;
4727 value = 0;
4728 tb = 0;
4729 bad:
4730 Py_XDECREF(type);
4731 Py_XDECREF(value);
4732 Py_XDECREF(tb);
4733 return result;
4735 """)
4737 #------------------------------------------------------------------------------------
4739 unpacking_utility_code = UtilityCode(
4740 proto = """
4741 static PyObject *__Pyx_UnpackItem(PyObject *, Py_ssize_t index); /*proto*/
4742 static int __Pyx_EndUnpack(PyObject *); /*proto*/
4743 """,
4744 impl = """
4745 static PyObject *__Pyx_UnpackItem(PyObject *iter, Py_ssize_t index) {
4746 PyObject *item;
4747 if (!(item = PyIter_Next(iter))) {
4748 if (!PyErr_Occurred()) {
4749 PyErr_Format(PyExc_ValueError,
4750 #if PY_VERSION_HEX < 0x02050000
4751 "need more than %d values to unpack", (int)index);
4752 #else
4753 "need more than %zd values to unpack", index);
4754 #endif
4757 return item;
4760 static int __Pyx_EndUnpack(PyObject *iter) {
4761 PyObject *item;
4762 if ((item = PyIter_Next(iter))) {
4763 Py_DECREF(item);
4764 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
4765 return -1;
4767 else if (!PyErr_Occurred())
4768 return 0;
4769 else
4770 return -1;
4772 """)
4774 #------------------------------------------------------------------------------------
4776 type_test_utility_code = UtilityCode(
4777 proto = """
4778 static int __Pyx_TypeTest(PyObject *obj, PyTypeObject *type); /*proto*/
4779 """,
4780 impl = """
4781 static int __Pyx_TypeTest(PyObject *obj, PyTypeObject *type) {
4782 if (!type) {
4783 PyErr_Format(PyExc_SystemError, "Missing type object");
4784 return 0;
4786 if (obj == Py_None || PyObject_TypeCheck(obj, type))
4787 return 1;
4788 PyErr_Format(PyExc_TypeError, "Cannot convert %s to %s",
4789 Py_TYPE(obj)->tp_name, type->tp_name);
4790 return 0;
4792 """)
4794 #------------------------------------------------------------------------------------
4796 create_class_utility_code = UtilityCode(
4797 proto = """
4798 static PyObject *__Pyx_CreateClass(PyObject *bases, PyObject *dict, PyObject *name, char *modname); /*proto*/
4799 """,
4800 impl = """
4801 static PyObject *__Pyx_CreateClass(
4802 PyObject *bases, PyObject *dict, PyObject *name, char *modname)
4804 PyObject *py_modname;
4805 PyObject *result = 0;
4807 #if PY_MAJOR_VERSION < 3
4808 py_modname = PyString_FromString(modname);
4809 #else
4810 py_modname = PyUnicode_FromString(modname);
4811 #endif
4812 if (!py_modname)
4813 goto bad;
4814 if (PyDict_SetItemString(dict, "__module__", py_modname) < 0)
4815 goto bad;
4816 #if PY_MAJOR_VERSION < 3
4817 result = PyClass_New(bases, dict, name);
4818 #else
4819 result = PyObject_CallFunctionObjArgs((PyObject *)&PyType_Type, name, bases, dict, NULL);
4820 #endif
4821 bad:
4822 Py_XDECREF(py_modname);
4823 return result;
4825 """)
4827 #------------------------------------------------------------------------------------
4829 cpp_exception_utility_code = UtilityCode(
4830 proto = """
4831 #ifndef __Pyx_CppExn2PyErr
4832 static void __Pyx_CppExn2PyErr() {
4833 try {
4834 if (PyErr_Occurred())
4835 ; // let the latest Python exn pass through and ignore the current one
4836 else
4837 throw;
4838 } catch (const std::out_of_range& exn) {
4839 // catch out_of_range explicitly so the proper Python exn may be raised
4840 PyErr_SetString(PyExc_IndexError, exn.what());
4841 } catch (const std::exception& exn) {
4842 PyErr_SetString(PyExc_RuntimeError, exn.what());
4844 catch (...)
4846 PyErr_SetString(PyExc_RuntimeError, "Unknown exception");
4849 #endif
4850 """,
4851 impl = ""
4854 #------------------------------------------------------------------------------------
4856 append_utility_code = UtilityCode(
4857 proto = """
4858 static INLINE PyObject* __Pyx_PyObject_Append(PyObject* L, PyObject* x) {
4859 if (likely(PyList_CheckExact(L))) {
4860 if (PyList_Append(L, x) < 0) return NULL;
4861 Py_INCREF(Py_None);
4862 return Py_None; // this is just to have an accurate signature
4864 else {
4865 return PyObject_CallMethod(L, "append", "(O)", x);
4868 """,
4869 impl = ""
4872 #------------------------------------------------------------------------------------
4874 # If the is_unsigned flag is set, we need to do some extra work to make
4875 # sure the index doesn't become negative.
4877 getitem_int_utility_code = UtilityCode(
4878 proto = """
4879 static INLINE PyObject *__Pyx_GetItemInt(PyObject *o, Py_ssize_t i, int is_unsigned) {
4880 PyObject *r;
4881 if (PyList_CheckExact(o) && 0 <= i && i < PyList_GET_SIZE(o)) {
4882 r = PyList_GET_ITEM(o, i);
4883 Py_INCREF(r);
4885 else if (PyTuple_CheckExact(o) && 0 <= i && i < PyTuple_GET_SIZE(o)) {
4886 r = PyTuple_GET_ITEM(o, i);
4887 Py_INCREF(r);
4889 else if (Py_TYPE(o)->tp_as_sequence && Py_TYPE(o)->tp_as_sequence->sq_item && (likely(i >= 0) || !is_unsigned))
4890 r = PySequence_GetItem(o, i);
4891 else {
4892 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);
4893 if (!j)
4894 return 0;
4895 r = PyObject_GetItem(o, j);
4896 Py_DECREF(j);
4898 return r;
4900 """,
4901 impl = """
4902 """)
4904 #------------------------------------------------------------------------------------
4906 setitem_int_utility_code = UtilityCode(
4907 proto = """
4908 static INLINE int __Pyx_SetItemInt(PyObject *o, Py_ssize_t i, PyObject *v, int is_unsigned) {
4909 int r;
4910 if (PyList_CheckExact(o) && 0 <= i && i < PyList_GET_SIZE(o)) {
4911 Py_DECREF(PyList_GET_ITEM(o, i));
4912 Py_INCREF(v);
4913 PyList_SET_ITEM(o, i, v);
4914 return 1;
4916 else if (Py_TYPE(o)->tp_as_sequence && Py_TYPE(o)->tp_as_sequence->sq_ass_item && (likely(i >= 0) || !is_unsigned))
4917 r = PySequence_SetItem(o, i, v);
4918 else {
4919 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);
4920 if (!j)
4921 return -1;
4922 r = PyObject_SetItem(o, j, v);
4923 Py_DECREF(j);
4925 return r;
4927 """,
4928 impl = """
4929 """)
4931 #------------------------------------------------------------------------------------
4933 delitem_int_utility_code = UtilityCode(
4934 proto = """
4935 static INLINE int __Pyx_DelItemInt(PyObject *o, Py_ssize_t i, int is_unsigned) {
4936 int r;
4937 if (Py_TYPE(o)->tp_as_sequence && Py_TYPE(o)->tp_as_sequence->sq_ass_item && (likely(i >= 0) || !is_unsigned))
4938 r = PySequence_DelItem(o, i);
4939 else {
4940 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);
4941 if (!j)
4942 return -1;
4943 r = PyObject_DelItem(o, j);
4944 Py_DECREF(j);
4946 return r;
4948 """,
4949 impl = """
4950 """)
4952 #------------------------------------------------------------------------------------
4954 raise_noneattr_error_utility_code = UtilityCode(
4955 proto = """
4956 static INLINE void __Pyx_RaiseNoneAttributeError(char* attrname);
4957 """,
4958 impl = """
4959 static INLINE void __Pyx_RaiseNoneAttributeError(char* attrname) {
4960 PyErr_Format(PyExc_AttributeError, "'NoneType' object has no attribute '%s'", attrname);
4962 """)
4964 raise_noneindex_error_utility_code = UtilityCode(
4965 proto = """
4966 static INLINE void __Pyx_RaiseNoneIndexingError();
4967 """,
4968 impl = """
4969 static INLINE void __Pyx_RaiseNoneIndexingError() {
4970 PyErr_SetString(PyExc_TypeError, "'NoneType' object is unsubscriptable");
4972 """)