[pypy-svn] r12339 - in pypy/dist/pypy/objspace: . std test
pedronis at codespeak.net
pedronis at codespeak.net
Mon May 16 02:01:25 CEST 2005
Author: pedronis
Date: Mon May 16 02:01:25 2005
New Revision: 12339
Modified:
pypy/dist/pypy/objspace/descroperation.py
pypy/dist/pypy/objspace/std/objspace.py
pypy/dist/pypy/objspace/test/test_descroperation.py
Log:
more subtle logic for dealing with r<op> operators and subclasses for the case that the subclass does not redefine them
(tests and we pass test_descr.subclass_right_op)
Modified: pypy/dist/pypy/objspace/descroperation.py
==============================================================================
--- pypy/dist/pypy/objspace/descroperation.py (original)
+++ pypy/dist/pypy/objspace/descroperation.py Mon May 16 02:01:25 2005
@@ -216,12 +216,12 @@
def pow(space, w_obj1, w_obj2, w_obj3):
w_typ1 = space.type(w_obj1)
w_typ2 = space.type(w_obj2)
- w_left_impl = space.lookup(w_obj1, '__pow__')
+ w_left_src, w_left_impl = space.lookup_where(w_obj1, '__pow__')
if space.is_true(space.is_(w_typ1, w_typ2)):
w_right_impl = None
else:
- w_right_impl = space.lookup(w_obj2, '__rpow__')
- if space.is_true(space.issubtype(w_typ2, w_typ1)):
+ w_right_src, w_right_impl = space.lookup_where(w_obj2, '__rpow__')
+ if space.is_true(space.issubtype(w_typ2, w_typ1)) and not space.is_w(w_left_src, w_right_src):
w_obj1, w_obj2 = w_obj2, w_obj1
w_left_impl, w_right_impl = w_right_impl, w_left_impl
if w_left_impl is not None:
@@ -351,12 +351,12 @@
def coerce(space, w_obj1, w_obj2):
w_typ1 = space.type(w_obj1)
w_typ2 = space.type(w_obj2)
- w_left_impl = space.lookup(w_obj1, '__coerce__')
+ w_left_src, w_left_impl = space.lookup_where(w_obj1, '__coerce__')
if space.is_true(space.is_(w_typ1, w_typ2)):
w_right_impl = None
else:
- w_right_impl = space.lookup(w_obj2, '__coerce__')
- if space.is_true(space.issubtype(w_typ2, w_typ1)):
+ w_right_src, w_right_impl = space.lookup_where(w_obj2, '__coerce__')
+ if space.is_true(space.issubtype(w_typ2, w_typ1)) and not space.is_w(w_left_src, w_right_src):
w_obj1, w_obj2 = w_obj2, w_obj1
w_left_impl, w_right_impl = w_right_impl, w_left_impl
@@ -406,14 +406,14 @@
def _cmp(space, w_obj1, w_obj2):
w_typ1 = space.type(w_obj1)
w_typ2 = space.type(w_obj2)
- w_left_impl = space.lookup(w_obj1, '__cmp__')
+ w_left_src, w_left_impl = space.lookup_where(w_obj1, '__cmp__')
do_neg1 = False
do_neg2 = True
if space.is_true(space.is_(w_typ1, w_typ2)):
w_right_impl = None
else:
- w_right_impl = space.lookup(w_obj2, '__cmp__')
- if space.is_true(space.issubtype(w_typ2, w_typ1)):
+ w_right_src, w_right_impl = space.lookup_where(w_obj2, '__cmp__')
+ if space.is_true(space.issubtype(w_typ2, w_typ1)) and not space.is_w(w_right_src, w_left_src):
w_obj1, w_obj2 = w_obj2, w_obj1
w_left_impl, w_right_impl = w_right_impl, w_left_impl
do_neg1, do_neg2 = do_neg2, do_neg1
@@ -451,12 +451,12 @@
def binop_impl(space, w_obj1, w_obj2):
w_typ1 = space.type(w_obj1)
w_typ2 = space.type(w_obj2)
- w_left_impl = space.lookup(w_obj1, left)
+ w_left_src, w_left_impl = space.lookup_where(w_obj1, left)
if space.is_true(space.is_(w_typ1, w_typ2)):
w_right_impl = None
else:
- w_right_impl = space.lookup(w_obj2, right)
- if space.is_true(space.issubtype(w_typ2, w_typ1)):
+ w_right_src, w_right_impl = space.lookup_where(w_obj2, right)
+ if space.is_true(space.issubtype(w_typ2, w_typ1)) and not space.is_w(w_right_src, w_left_src):
w_obj1, w_obj2 = w_obj2, w_obj1
w_left_impl, w_right_impl = w_right_impl, w_left_impl
@@ -476,7 +476,7 @@
def comparison_impl(space, w_obj1, w_obj2):
w_typ1 = space.type(w_obj1)
w_typ2 = space.type(w_obj2)
- w_left_impl = space.lookup(w_obj1, left)
+ w_left_src, w_left_impl = space.lookup_where(w_obj1, left)
w_first = w_obj1
w_second = w_obj2
@@ -516,8 +516,8 @@
if space.is_true(space.is_(w_typ1, w_typ2)):
w_right_impl = None
else:
- w_right_impl = space.lookup(w_obj2, right)
- if space.is_true(space.issubtype(w_typ2, w_typ1)):
+ w_right_src, w_right_impl = space.lookup_where(w_obj2, right)
+ if space.is_true(space.issubtype(w_typ2, w_typ1)) and not space.is_w(w_right_src, w_left_src):
w_obj1, w_obj2 = w_obj2, w_obj1
w_left_impl, w_right_impl = w_right_impl, w_left_impl
Modified: pypy/dist/pypy/objspace/std/objspace.py
==============================================================================
--- pypy/dist/pypy/objspace/std/objspace.py (original)
+++ pypy/dist/pypy/objspace/std/objspace.py Mon May 16 02:01:25 2005
@@ -279,6 +279,10 @@
w_type = w_obj.getclass(self)
return w_type.lookup(name)
+ def lookup_where(self, w_obj, name):
+ w_type = w_obj.getclass(self)
+ return w_type.lookup_where(name)
+
def allocate_instance(self, cls, w_subtype):
"""Allocate the memory needed for an instance of an internal or
user-defined type, without actually __init__ializing the instance."""
Modified: pypy/dist/pypy/objspace/test/test_descroperation.py
==============================================================================
--- pypy/dist/pypy/objspace/test/test_descroperation.py (original)
+++ pypy/dist/pypy/objspace/test/test_descroperation.py Mon May 16 02:01:25 2005
@@ -81,6 +81,19 @@
assert b ** b == "B's pow"
assert -b == "B's neg"
+ class C(B):
+ pass
+ c = C()
+ assert c - 1 == "B's sub"
+ assert 1 - c == "B's rsub"
+ assert c - b == "B's sub"
+ assert b - c == "B's sub"
+
+ assert c ** 1 == "B's pow"
+ assert 1 ** c == "B's rpow"
+ assert c ** b == "B's pow"
+ assert b ** c == "B's pow"
+
def test_getslice(self):
class Sq(object):
def __getslice__(self, start, stop):
More information about the Pypy-commit
mailing list