[pypy-commit] pypy annotator: kill some FlowObjSpace methods, use operations directly instead
rlamy
noreply at buildbot.pypy.org
Fri Jan 17 18:36:40 CET 2014
Author: Ronan Lamy <ronan.lamy at gmail.com>
Branch: annotator
Changeset: r68734:8c85266bb4a5
Date: 2014-01-03 03:18 +0100
http://bitbucket.org/pypy/pypy/changeset/8c85266bb4a5/
Log: kill some FlowObjSpace methods, use operations directly instead
diff --git a/rpython/flowspace/flowcontext.py b/rpython/flowspace/flowcontext.py
--- a/rpython/flowspace/flowcontext.py
+++ b/rpython/flowspace/flowcontext.py
@@ -14,6 +14,7 @@
recursively_flatten)
from rpython.flowspace.specialcase import (rpython_print_item,
rpython_print_newline)
+from rpython.flowspace.operation import op
class FlowingError(Exception):
@@ -213,60 +214,55 @@
# ____________________________________________________________
_unary_ops = [
- ('UNARY_POSITIVE', "pos"),
- ('UNARY_NEGATIVE', "neg"),
- ('UNARY_NOT', "not_"),
- ('UNARY_CONVERT', "repr"),
- ('UNARY_INVERT', "invert"),
+ ('UNARY_POSITIVE', op.pos),
+ ('UNARY_NEGATIVE', op.neg),
+ ('UNARY_CONVERT', op.repr),
+ ('UNARY_INVERT', op.invert),
]
-def unaryoperation(OPCODE, op):
+def unaryoperation(OPCODE, operation):
def UNARY_OP(self, *ignored):
- operation = getattr(self.space, op)
w_1 = self.popvalue()
- w_result = operation(w_1)
+ w_result = operation(w_1).eval(self)
self.pushvalue(w_result)
- UNARY_OP.unaryop = op
UNARY_OP.func_name = OPCODE
return UNARY_OP
_binary_ops = [
- ('BINARY_MULTIPLY', "mul"),
- ('BINARY_TRUE_DIVIDE', "truediv"),
- ('BINARY_FLOOR_DIVIDE', "floordiv"),
- ('BINARY_DIVIDE', "div"),
- ('BINARY_MODULO', "mod"),
- ('BINARY_ADD', "add"),
- ('BINARY_SUBTRACT', "sub"),
- ('BINARY_SUBSCR', "getitem"),
- ('BINARY_LSHIFT', "lshift"),
- ('BINARY_RSHIFT', "rshift"),
- ('BINARY_AND', "and_"),
- ('BINARY_XOR', "xor"),
- ('BINARY_OR', "or_"),
- ('INPLACE_MULTIPLY', "inplace_mul"),
- ('INPLACE_TRUE_DIVIDE', "inplace_truediv"),
- ('INPLACE_FLOOR_DIVIDE', "inplace_floordiv"),
- ('INPLACE_DIVIDE', "inplace_div"),
- ('INPLACE_MODULO', "inplace_mod"),
- ('INPLACE_ADD', "inplace_add"),
- ('INPLACE_SUBTRACT', "inplace_sub"),
- ('INPLACE_LSHIFT', "inplace_lshift"),
- ('INPLACE_RSHIFT', "inplace_rshift"),
- ('INPLACE_AND', "inplace_and"),
- ('INPLACE_XOR', "inplace_xor"),
- ('INPLACE_OR', "inplace_or"),
+ ('BINARY_MULTIPLY', op.mul),
+ ('BINARY_TRUE_DIVIDE', op.truediv),
+ ('BINARY_FLOOR_DIVIDE', op.floordiv),
+ ('BINARY_DIVIDE', op.div),
+ ('BINARY_MODULO', op.mod),
+ ('BINARY_ADD', op.add),
+ ('BINARY_SUBTRACT', op.sub),
+ ('BINARY_SUBSCR', op.getitem),
+ ('BINARY_LSHIFT', op.lshift),
+ ('BINARY_RSHIFT', op.rshift),
+ ('BINARY_AND', op.and_),
+ ('BINARY_XOR', op.xor),
+ ('BINARY_OR', op.or_),
+ ('INPLACE_MULTIPLY', op.inplace_mul),
+ ('INPLACE_TRUE_DIVIDE', op.inplace_truediv),
+ ('INPLACE_FLOOR_DIVIDE', op.inplace_floordiv),
+ ('INPLACE_DIVIDE', op.inplace_div),
+ ('INPLACE_MODULO', op.inplace_mod),
+ ('INPLACE_ADD', op.inplace_add),
+ ('INPLACE_SUBTRACT', op.inplace_sub),
+ ('INPLACE_LSHIFT', op.inplace_lshift),
+ ('INPLACE_RSHIFT', op.inplace_rshift),
+ ('INPLACE_AND', op.inplace_and),
+ ('INPLACE_XOR', op.inplace_xor),
+ ('INPLACE_OR', op.inplace_or),
]
-def binaryoperation(OPCODE, op):
+def binaryoperation(OPCODE, operation):
"""NOT_RPYTHON"""
- def BINARY_OP(self, *ignored):
- operation = getattr(self.space, op)
+ def BINARY_OP(self, _):
w_2 = self.popvalue()
w_1 = self.popvalue()
- w_result = operation(w_1, w_2)
+ w_result = operation(w_1, w_2).eval(self)
self.pushvalue(w_result)
- BINARY_OP.binop = op
BINARY_OP.func_name = OPCODE
return BINARY_OP
@@ -585,6 +581,14 @@
def CONTINUE_LOOP(self, startofloop):
raise Continue(startofloop)
+ def not_(self, w_obj):
+ w_bool = op.bool(w_obj).eval(self)
+ return const(not self.guessbool(w_bool))
+
+ def UNARY_NOT(self, _):
+ w_obj = self.popvalue()
+ self.pushvalue(self.not_(w_obj))
+
def cmp_lt(self, w_1, w_2):
return self.space.lt(w_1, w_2)
@@ -607,13 +611,13 @@
return self.space.contains(w_2, w_1)
def cmp_not_in(self, w_1, w_2):
- return self.space.not_(self.space.contains(w_2, w_1))
+ return self.not_(self.space.contains(w_2, w_1))
def cmp_is(self, w_1, w_2):
- return self.space.is_(w_1, w_2)
+ return op.is_(w_1, w_2).eval(self)
def cmp_is_not(self, w_1, w_2):
- return self.space.not_(self.space.is_(w_1, w_2))
+ return self.not_(op.is_(w_1, w_2).eval(self))
def cmp_exc_match(self, w_1, w_2):
return self.space.newbool(self.space.exception_match(w_1, w_2))
@@ -722,34 +726,35 @@
def JUMP_IF_FALSE(self, target):
# Python <= 2.6 only
w_cond = self.peekvalue()
- if not self.guessbool(self.space.bool(w_cond)):
+ if not self.guessbool(op.bool(w_cond).eval(self)):
return target
def JUMP_IF_TRUE(self, target):
# Python <= 2.6 only
w_cond = self.peekvalue()
- if self.guessbool(self.space.bool(w_cond)):
+ if self.guessbool(op.bool(w_cond).eval(self)):
return target
def POP_JUMP_IF_FALSE(self, target):
w_value = self.popvalue()
- if not self.guessbool(self.space.bool(w_value)):
+ if not self.guessbool(op.bool(w_value).eval(self)):
return target
def POP_JUMP_IF_TRUE(self, target):
w_value = self.popvalue()
- if self.guessbool(self.space.bool(w_value)):
+ if self.guessbool(op.bool(w_value).eval(self)):
return target
def JUMP_IF_FALSE_OR_POP(self, target):
w_value = self.peekvalue()
- if not self.guessbool(self.space.bool(w_value)):
+ if not self.guessbool(op.bool(w_value).eval(self)):
return target
self.popvalue()
def JUMP_IF_TRUE_OR_POP(self, target):
w_value = self.peekvalue()
- if self.guessbool(self.space.bool(w_value)):
+ if self.guessbool(op.bool(w_value).eval(self)):
+ return target
return target
self.popvalue()
@@ -841,7 +846,7 @@
"obj.attributename"
w_obj = self.popvalue()
w_attributename = self.getname_w(nameindex)
- w_value = self.space.getattr(w_obj, w_attributename)
+ w_value = op.getattr(w_obj, w_attributename).eval(self)
self.pushvalue(w_value)
LOOKUP_METHOD = LOAD_ATTR
@@ -965,7 +970,7 @@
w_attributename = self.getname_w(nameindex)
w_obj = self.popvalue()
w_newvalue = self.popvalue()
- self.space.setattr(w_obj, w_attributename, w_newvalue)
+ op.setattr(w_obj, w_attributename, w_newvalue).eval(self)
def UNPACK_SEQUENCE(self, itemcount):
w_iterable = self.popvalue()
diff --git a/rpython/flowspace/objspace.py b/rpython/flowspace/objspace.py
--- a/rpython/flowspace/objspace.py
+++ b/rpython/flowspace/objspace.py
@@ -11,7 +11,7 @@
from rpython.flowspace.model import (Constant, Variable, checkgraph, const,
FSException)
from rpython.flowspace.bytecode import HostCode
-from rpython.flowspace.operation import op, NOT_REALLY_CONST
+from rpython.flowspace.operation import op
from rpython.flowspace.flowcontext import (FlowSpaceFrame, fixeggblocks,
FlowingError, Raise)
from rpython.flowspace.generator import (tweak_generator_graph,
@@ -87,11 +87,11 @@
"Catching %s is not valid in RPython" % check_class.__name__)
if not isinstance(check_class, tuple):
# the simple case
- return frame.guessbool(self.issubtype(w_exc_type, w_check_class))
+ return frame.guessbool(op.issubtype(w_exc_type, w_check_class).eval(frame))
# special case for StackOverflow (see rlib/rstackovf.py)
if check_class == rstackovf.StackOverflow:
w_real_class = const(rstackovf._StackOverflow)
- return frame.guessbool(self.issubtype(w_exc_type, w_real_class))
+ return frame.guessbool(op.issubtype(w_exc_type, w_real_class).eval(frame))
# checking a tuple of classes
for klass in w_check_class.value:
if self.exception_match(w_exc_type, const(klass)):
@@ -108,12 +108,12 @@
if frame.guessbool(self.call_function(const(isinstance), w_arg1,
self.w_type)):
# this is for all cases of the form (Class, something)
- if frame.guessbool(self.is_(w_arg2, self.w_None)):
+ if frame.guessbool(op.is_(w_arg2, self.w_None).eval(frame)):
# raise Type: we assume we have to instantiate Type
w_value = self.call_function(w_arg1)
else:
- w_valuetype = self.type(w_arg2)
- if frame.guessbool(self.issubtype(w_valuetype, w_arg1)):
+ w_valuetype = op.type(w_arg2).eval(frame)
+ if frame.guessbool(op.issubtype(w_valuetype, w_arg1).eval(frame)):
# raise Type, Instance: let etype be the exact type of value
w_value = w_arg2
else:
@@ -121,7 +121,7 @@
w_value = self.call_function(w_arg1, w_arg2)
else:
# the only case left here is (inst, None), from a 'raise inst'.
- if not frame.guessbool(self.is_(w_arg2, self.w_None)):
+ if not frame.guessbool(op.is_(w_arg2, self.w_None).eval(frame)):
exc = TypeError("instance exception may not have a "
"separate value")
raise Raise(const(exc))
@@ -138,16 +138,13 @@
else:
w_len = self.len(w_iterable)
w_correct = self.eq(w_len, const(expected_length))
- if not self.frame.guessbool(self.bool(w_correct)):
+ if not self.frame.guessbool(op.bool(w_correct).eval(self.frame)):
w_exc = self.exc_from_raise(self.w_ValueError, self.w_None)
raise Raise(w_exc)
return [self.getitem(w_iterable, const(i))
for i in range(expected_length)]
# ____________________________________________________________
- def not_(self, w_obj):
- return const(not self.frame.guessbool(self.bool(w_obj)))
-
def import_name(self, name, glob=None, loc=None, frm=None, level=-1):
try:
mod = __import__(name, glob, loc, frm, level)
@@ -211,7 +208,11 @@
raise FlowingError("global name '%s' is not defined" % varname)
return const(value)
-for cls in op.__dict__.values():
+
+for cls in [op.len, op.type, op.eq, op.ne, op.contains, op.getitem, op.getattr,
+ op.getslice, op.setslice, op.delslice, op.yield_, op.iter, op.next,
+ op.lt, op.gt, op.le, op.ge, op.str,
+ op.newlist, op.newtuple, op.newdict, op.setitem, op.delitem]:
if getattr(FlowObjSpace, cls.opname, None) is None:
setattr(FlowObjSpace, cls.opname, cls.make_sc())
diff --git a/rpython/flowspace/test/test_objspace.py b/rpython/flowspace/test/test_objspace.py
--- a/rpython/flowspace/test/test_objspace.py
+++ b/rpython/flowspace/test/test_objspace.py
@@ -914,6 +914,13 @@
simplify_graph(graph)
assert self.all_operations(graph) == {'getitem': 1}
+ def test_delitem(self):
+ def f(c, x):
+ del c[x]
+ graph = self.codetest(f)
+ simplify_graph(graph)
+ assert self.all_operations(graph) == {'delitem': 1}
+
def test_context_manager(self):
def f(c, x):
with x:
More information about the pypy-commit
mailing list