From c1609002021128e64e37da5001b76ca89b561f93 Mon Sep 17 00:00:00 2001 From: Robert Bradshaw Date: Wed, 5 Sep 2007 16:43:04 -0700 Subject: [PATCH] incref_local_binop option for SAGE This is so mutating inplace operations can be detected safely. --- Cython/Compiler/CmdLine.py | 4 ++++ Cython/Compiler/ExprNodes.py | 2 ++ Cython/Compiler/Nodes.py | 2 ++ Cython/Compiler/Options.py | 7 +++++++ Cython/Compiler/Symtab.py | 3 +++ 5 files changed, 18 insertions(+) diff --git a/Cython/Compiler/CmdLine.py b/Cython/Compiler/CmdLine.py index e0bd06bb..84f9e2be 100644 --- a/Cython/Compiler/CmdLine.py +++ b/Cython/Compiler/CmdLine.py @@ -22,6 +22,8 @@ Options: -z, --pre-import If specified, assume undeclared names in this module. Emulates the behavior of putting "from 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: diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py index 0dfa8633..dfdaf6f2 100644 --- a/Cython/Compiler/ExprNodes.py +++ b/Cython/Compiler/ExprNodes.py @@ -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) diff --git a/Cython/Compiler/Nodes.py b/Cython/Compiler/Nodes.py index e43f10cf..ddbfdda0 100644 --- a/Cython/Compiler/Nodes.py +++ b/Cython/Compiler/Nodes.py @@ -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 diff --git a/Cython/Compiler/Options.py b/Cython/Compiler/Options.py index d89d304e..3b426f24 100644 --- a/Cython/Compiler/Options.py +++ b/Cython/Compiler/Options.py @@ -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 + diff --git a/Cython/Compiler/Symtab.py b/Cython/Compiler/Symtab.py index b27e294a..7daeb4e4 100644 --- a/Cython/Compiler/Symtab.py +++ b/Cython/Compiler/Symtab.py @@ -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 -- 2.26.2