[pypy-commit] pypy cells-local-stack: fix asserts about the stack depth

cfbolz noreply at buildbot.pypy.org
Fri May 8 18:46:07 CEST 2015


Author: Carl Friedrich Bolz <cfbolz at gmx.de>
Branch: cells-local-stack
Changeset: r77229:e95331850baf
Date: 2015-05-08 18:42 +0200
http://bitbucket.org/pypy/pypy/changeset/e95331850baf/

Log:	fix asserts about the stack depth

diff --git a/pypy/interpreter/pyframe.py b/pypy/interpreter/pyframe.py
--- a/pypy/interpreter/pyframe.py
+++ b/pypy/interpreter/pyframe.py
@@ -294,10 +294,18 @@
         self.locals_cells_stack_w[depth] = w_object
         self.valuestackdepth = depth + 1
 
+    def _check_stack_index(self, index):
+        # will be completely removed by the optimizer if only used in an assert
+        # and if asserts are disabled
+        code = self.pycode
+        ncellvars = len(code.co_cellvars)
+        nfreevars = len(code.co_freevars)
+        stackstart = code.co_nlocals + ncellvars + nfreevars
+        return index >= stackstart
+
     def popvalue(self):
         depth = self.valuestackdepth - 1
-        # YYY
-        assert depth >= self.pycode.co_nlocals, "pop from empty value stack"
+        assert self._check_stack_index(depth)
         w_object = self.locals_cells_stack_w[depth]
         self.locals_cells_stack_w[depth] = None
         self.valuestackdepth = depth
@@ -325,8 +333,7 @@
     def peekvalues(self, n):
         values_w = [None] * n
         base = self.valuestackdepth - n
-        # YYY
-        assert base >= self.pycode.co_nlocals
+        assert self._check_stack_index(base)
         while True:
             n -= 1
             if n < 0:
@@ -338,9 +345,7 @@
     def dropvalues(self, n):
         n = hint(n, promote=True)
         finaldepth = self.valuestackdepth - n
-        # YYY
-        assert finaldepth >= self.pycode.co_nlocals, (
-            "stack underflow in dropvalues()")
+        assert self._check_stack_index(finaldepth)
         while True:
             n -= 1
             if n < 0:
@@ -372,17 +377,13 @@
         # Contrast this with CPython where it's PEEK(-1).
         index_from_top = hint(index_from_top, promote=True)
         index = self.valuestackdepth + ~index_from_top
-        # YYY
-        assert index >= self.pycode.co_nlocals, (
-            "peek past the bottom of the stack")
+        assert self._check_stack_index(index)
         return self.locals_cells_stack_w[index]
 
     def settopvalue(self, w_object, index_from_top=0):
         index_from_top = hint(index_from_top, promote=True)
         index = self.valuestackdepth + ~index_from_top
-        # YYY
-        assert index >= self.pycode.co_nlocals, (
-            "settop past the bottom of the stack")
+        assert self._check_stack_index(index)
         self.locals_cells_stack_w[index] = w_object
 
     @jit.unroll_safe
@@ -504,8 +505,8 @@
                                  for w_blk in space.unpackiterable(w_blockstack)])
         self.locals_cells_stack_w = values_w[:]
         valuestackdepth = space.int_w(w_stackdepth)
-        if valuestackdepth < 0:
-            raise OperationError(space.w_ValueError, space.wrap("stackdepth must be non-negative"))
+        if not self._check_stack_index(valuestackdepth):
+            raise OperationError(space.w_ValueError, space.wrap("invalid stackdepth"))
         self.valuestackdepth = valuestackdepth
         if space.is_w(w_exc_value, space.w_None):
             new_frame.last_exception = None


More information about the pypy-commit mailing list