From d3c79c9b1d3c89c9ec31c517e051264ad8fac287 Mon Sep 17 00:00:00 2001 From: Dag Sverre Seljebotn Date: Tue, 14 Oct 2008 09:08:43 +0200 Subject: [PATCH] Disable array literals outside of pointer declaration --- Cython/Compiler/ExprNodes.py | 10 +++++++--- Cython/Compiler/Optimize.py | 3 +-- tests/errors/literal_lists.pyx | 8 ++++++++ 3 files changed, 16 insertions(+), 5 deletions(-) create mode 100644 tests/errors/literal_lists.pyx diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py index 7f993eeb..ba049fff 100644 --- a/Cython/Compiler/ExprNodes.py +++ b/Cython/Compiler/ExprNodes.py @@ -935,7 +935,7 @@ class NameNode(AtomicExprNode): is_name = True is_cython_module = False cython_attribute = None - skip_assignment_decref = False + lhs_of_first_assignment = False entry = None def create_analysed_rvalue(pos, env, entry): @@ -1156,6 +1156,10 @@ class NameNode(AtomicExprNode): entry = self.entry if entry is None: return # There was an error earlier + + if (self.entry.type.is_ptr and isinstance(rhs, ListNode) + and not self.lhs_of_first_assignment): + error(self.pos, "Literal list must be assigned to pointer at time of declaration") # is_pyglobal seems to be True for module level-globals only. # We use this to access class->tp_dict if necessary. @@ -1200,7 +1204,7 @@ class NameNode(AtomicExprNode): #print "...from", rhs ### #print "...LHS type", self.type, "ctype", self.ctype() ### #print "...RHS type", rhs.type, "ctype", rhs.ctype() ### - if not self.skip_assignment_decref: + if not self.lhs_of_first_assignment: if entry.is_local and not Options.init_local_none: initalized = entry.scope.control_flow.get_state((entry.name, 'initalized'), self.pos) if initalized is True: @@ -1223,7 +1227,7 @@ class NameNode(AtomicExprNode): import Buffer Buffer.put_assign_to_buffer(self.result(), rhstmp, buffer_aux, self.entry.type, - is_initialized=not self.skip_assignment_decref, + is_initialized=not self.lhs_of_first_assignment, pos=self.pos, code=code) code.putln("%s = 0;" % rhstmp) code.funcstate.release_temp(rhstmp) diff --git a/Cython/Compiler/Optimize.py b/Cython/Compiler/Optimize.py index 7422b82e..2e464149 100644 --- a/Cython/Compiler/Optimize.py +++ b/Cython/Compiler/Optimize.py @@ -152,12 +152,11 @@ class FinalOptimizePhase(Visitor.CythonTransform): def visit_SingleAssignmentNode(self, node): if node.first: lhs = node.lhs + lhs.lhs_of_first_assignment = True if isinstance(lhs, ExprNodes.NameNode) and lhs.entry.type.is_pyobject: # Have variable initialized to 0 rather than None lhs.entry.init_to_none = False lhs.entry.init = 0 - # Set a flag in NameNode to skip the decref - lhs.skip_assignment_decref = True return node def visit_SimpleCallNode(self, node): diff --git a/tests/errors/literal_lists.pyx b/tests/errors/literal_lists.pyx new file mode 100644 index 00000000..adbff03c --- /dev/null +++ b/tests/errors/literal_lists.pyx @@ -0,0 +1,8 @@ +def f(): + cdef int* p + if False: + p = [1, 2, 3] + +_ERRORS = u""" +4:10: Literal list must be assigned to pointer at time of declaration +""" -- 2.26.2