[pypy-commit] pypy remove-intlong-smm: fix the inplace ops

pjenvey noreply at buildbot.pypy.org
Fri Feb 21 19:13:23 CET 2014


Author: Philip Jenvey <pjenvey at underboss.org>
Branch: remove-intlong-smm
Changeset: r69240:3aff7d060076
Date: 2014-02-21 10:04 -0800
http://bitbucket.org/pypy/pypy/changeset/3aff7d060076/

Log:	fix the inplace ops

diff --git a/pypy/objspace/std/frame.py b/pypy/objspace/std/frame.py
--- a/pypy/objspace/std/frame.py
+++ b/pypy/objspace/std/frame.py
@@ -1,6 +1,9 @@
 """StdObjSpace custom opcode implementations"""
 
+import operator
+
 from rpython.rlib.rarithmetic import ovfcheck
+from rpython.tool.sourcetools import func_renamer
 
 from pypy.interpreter.pyframe import PyFrame
 from pypy.interpreter.error import oefmt
@@ -20,36 +23,41 @@
             raise AssertionError
 
 
-def int_BINARY_ADD(self, oparg, next_instr):
-    space = self.space
-    w_2 = self.popvalue()
-    w_1 = self.popvalue()
-    if type(w_1) is W_IntObject and type(w_2) is W_IntObject:
-        try:
-            z = ovfcheck(w_1.intval + w_2.intval)
-        except OverflowError:
-            w_result = w_1.descr_add(space, w_2)
+def _intshortcut(spaceopname):
+    if spaceopname.startswith('inplace_'):
+        opname = spaceopname[len('inplace_'):]
+        funcprefix = 'int_'
+    else:
+        opname = spaceopname
+        funcprefix = 'int_BINARY_'
+    op = getattr(operator, opname)
+    int_op = getattr(W_IntObject, 'descr_' + opname)
+
+    @func_renamer(funcprefix + spaceopname.upper())
+    def opimpl(self, oparg, next_instr):
+        space = self.space
+        space_op = getattr(space, spaceopname)
+
+        w_2 = self.popvalue()
+        w_1 = self.popvalue()
+        if type(w_1) is W_IntObject and type(w_2) is W_IntObject:
+            try:
+                z = ovfcheck(op(w_1.intval, w_2.intval))
+            except OverflowError:
+                w_result = int_op(w_1, space, w_2)
+            else:
+                w_result = space.newint(z)
         else:
-            w_result = space.newint(z)
-    else:
-        w_result = space.add(w_1, w_2)
-    self.pushvalue(w_result)
+            w_result = space_op(w_1, w_2)
+        self.pushvalue(w_result)
 
+    return opimpl
 
-def int_BINARY_SUBTRACT(self, oparg, next_instr):
-    space = self.space
-    w_2 = self.popvalue()
-    w_1 = self.popvalue()
-    if type(w_1) is W_IntObject and type(w_2) is W_IntObject:
-        try:
-            z = ovfcheck(w_1.intval - w_2.intval)
-        except OverflowError:
-            w_result = w_1.descr_sub(space, w_2)
-        else:
-            w_result = space.newint(z)
-    else:
-        w_result = space.sub(w_1, w_2)
-    self.pushvalue(w_result)
+
+int_BINARY_ADD = _intshortcut('add')
+int_INPLACE_ADD = _intshortcut('inplace_add')
+int_BINARY_SUBTRACT = _intshortcut('sub')
+int_INPLACE_SUBTRACT = _intshortcut('inplace_sub')
 
 
 def list_BINARY_SUBSCR(self, oparg, next_instr):
@@ -72,9 +80,9 @@
         pass
     if space.config.objspace.std.optimized_int_add:
         StdObjSpaceFrame.BINARY_ADD = int_BINARY_ADD
-        StdObjSpaceFrame.INPLACE_ADD = int_BINARY_ADD
-        StdObjSpaceFrame.BINARY_SUB = int_BINARY_SUBTRACT
-        StdObjSpaceFrame.INPLACE_SUBTRACT = int_BINARY_SUBTRACT
+        StdObjSpaceFrame.INPLACE_ADD = int_INPLACE_ADD
+        StdObjSpaceFrame.BINARY_SUBTRACT = int_BINARY_SUBTRACT
+        StdObjSpaceFrame.INPLACE_SUBTRACT = int_INPLACE_SUBTRACT
     if space.config.objspace.std.optimized_list_getitem:
         StdObjSpaceFrame.BINARY_SUBSCR = list_BINARY_SUBSCR
     from pypy.objspace.std.callmethod import LOOKUP_METHOD, CALL_METHOD
diff --git a/pypy/objspace/std/test/test_intobject.py b/pypy/objspace/std/test/test_intobject.py
--- a/pypy/objspace/std/test/test_intobject.py
+++ b/pypy/objspace/std/test/test_intobject.py
@@ -556,3 +556,12 @@
 
 class AppTestIntOptimizedAdd(AppTestInt):
     spaceconfig = {"objspace.std.optimized_int_add": True}
+
+    def test_inplace(self):
+        # ensure other inplace ops still work
+        l = []
+        l += xrange(5)
+        assert l == list(range(5))
+        a = 8.5
+        a -= .5
+        assert a == 8


More information about the pypy-commit mailing list