incref_local_binop option for SAGE
authorRobert Bradshaw <robertwb@math.washington.edu>
Wed, 5 Sep 2007 23:43:04 +0000 (16:43 -0700)
committerRobert Bradshaw <robertwb@math.washington.edu>
Wed, 5 Sep 2007 23:43:04 +0000 (16:43 -0700)
This is so mutating inplace operations can be detected safely.

Cython/Compiler/CmdLine.py
Cython/Compiler/ExprNodes.py
Cython/Compiler/Nodes.py
Cython/Compiler/Options.py
Cython/Compiler/Symtab.py

index e0bd06bb73259f77f64ae6915891d5211eda4091..84f9e2beab2aaab701deb5156d4f919069e6cd1c 100644 (file)
@@ -22,6 +22,8 @@ Options:
   -z, --pre-import <module>      If specified, assume undeclared names in this 
                                  module. Emulates the behavior of putting 
                                  "from <module> import *" at the top of the file. 
+  --incref_local_binop           Force local an extra incref on local variables before
+                                 performing any binary operations. 
 """  
 #The following experimental options are supported only on MacOSX:
 #  -C, --compile    Compile generated .c file to .o file
@@ -76,6 +78,8 @@ def parse_command_line(args):
                 Options.embed_pos_in_docstring = 1
             elif option in ("-z", "--pre-import"):
                 Options.pre_import = pop_arg()
+            elif option == "--incref_local_binop":
+                Options.incref_local_binop = 1
             else:
                 bad_usage()
         else:
index 0dfa86338d83c2577b265618a4afe41ccf803a07..dfdaf6f2398bbbe763ed0892a29ccd77367983b4 100644 (file)
@@ -2430,6 +2430,8 @@ class BinopNode(ExprNode):
             self.coerce_operands_to_pyobjects(env)
             self.type = py_object_type
             self.is_temp = 1
+            if Options.incref_local_binop and self.operand1.type.is_pyobject:
+                self.operand1 = self.operand1.coerce_to_temp(env)
         else:
             self.analyse_c_operation(env)
     
index e43f10cf079297b96dc0f2a86c7ef9d94229990a..ddbfdda0a7b1f4cbedd81f601e614a6189936705 100644 (file)
@@ -1624,6 +1624,8 @@ class InPlaceAssignmentNode(AssignmentNode):
         self.dup = self.create_dup_node(env) # re-assigns lhs to a shallow copy
         self.rhs.analyse_types(env)
         self.lhs.analyse_target_types(env)
+        if Options.incref_local_binop and self.dup.type.is_pyobject:
+            self.dup = self.dup.coerce_to_temp(env)
         
     def allocate_rhs_temps(self, env):
         import ExprNodes
index d89d304eb2cb328a350bfc56472227ffa96c7b49..3b426f24ee1fb2755899cc30f593b7f8f4e4f06d 100644 (file)
@@ -9,3 +9,10 @@ embed_pos_in_docstring = 0
 gcc_branch_hints = 1
 
 pre_import = None
+
+# This is a SAGE-specific option that will 
+# cause Cython to incref local variables before
+# performing a binary operation on them, for 
+# safe detection of inplace operators. 
+incref_local_binop = 0
+
index b27e294af5fe5cd94edcfa6fc076c3f9eea52fd1..7daeb4e406398c1c410d16ed5195ba771c0473ea 100644 (file)
@@ -40,6 +40,7 @@ class Entry:
     # getter_cname     string          C func for getting property
     # setter_cname     string          C func for setting or deleting property
     # is_self_arg      boolean    Is the "self" arg of an exttype method
+    # is_arg           boolean    Is the arg of a method
     # is_readonly      boolean    Can't be assigned to
     # func_cname       string     C func implementing Python func
     # pos              position   Source position where declared
@@ -81,6 +82,7 @@ class Entry:
     getter_cname = None
     setter_cname = None
     is_self_arg = 0
+    is_arg = 0
     is_declared_generic = 0
     is_readonly = 0
     func_cname = None
@@ -910,6 +912,7 @@ class LocalScope(Scope):
         entry.is_variable = 1
         if type.is_pyobject:
             entry.init = "0"
+        entry.is_arg = 1
         #entry.borrowed = 1 # Not using borrowed arg refs for now
         self.arg_entries.append(entry)
         return entry