[pypy-svn] r23462 - in pypy/dist/pypy/interpreter: . test
nik at codespeak.net
nik at codespeak.net
Fri Feb 17 20:37:30 CET 2006
Author: nik
Date: Fri Feb 17 20:37:29 2006
New Revision: 23462
Modified:
pypy/dist/pypy/interpreter/baseobjspace.py
pypy/dist/pypy/interpreter/test/test_objspace.py
Log:
nicer space.callable implemention through a generic lookup helper (thanks pedronis)
Modified: pypy/dist/pypy/interpreter/baseobjspace.py
==============================================================================
--- pypy/dist/pypy/interpreter/baseobjspace.py (original)
+++ pypy/dist/pypy/interpreter/baseobjspace.py Fri Feb 17 20:37:29 2006
@@ -520,23 +520,28 @@
w_meth = self.getattr(w_obj, self.wrap(methname))
return self.call_function(w_meth, *arg_w)
- def callable(self, w_obj):
+ def lookup(self, w_obj, name):
w_type = self.type(w_obj)
w_mro = self.getattr(w_type, self.wrap("__mro__"))
for w_supertype in self.unpackiterable(w_mro):
- w_dict = self.getattr(w_supertype, self.wrap("__dict__"))
- if self.is_true(
- self.call_method(w_dict, "has_key", self.wrap("__call__"))):
- w_is_oldstyle = self.isinstance(w_obj, self.w_instance)
- if self.is_true(w_is_oldstyle):
- # ugly old style class special treatment, but well ...
- try:
- self.getattr(w_obj, self.wrap("__call__"))
- return self.w_True
- except OperationError, e:
- if not e.match(self, self.w_AttributeError):
- raise
- return self.w_False
+ w_value = w_supertype.getdictvalue(self, name)
+ if w_value is not None:
+ return w_value
+ return None
+
+ def callable(self, w_obj):
+ if self.lookup(w_obj, "__call__") is not None:
+ w_is_oldstyle = self.isinstance(w_obj, self.w_instance)
+ if self.is_true(w_is_oldstyle):
+ # ugly old style class special treatment, but well ...
+ try:
+ self.getattr(w_obj, self.wrap("__call__"))
+ return self.w_True
+ except OperationError, e:
+ if not e.match(self, self.w_AttributeError):
+ raise
+ return self.w_False
+ else:
return self.w_True
return self.w_False
Modified: pypy/dist/pypy/interpreter/test/test_objspace.py
==============================================================================
--- pypy/dist/pypy/interpreter/test/test_objspace.py (original)
+++ pypy/dist/pypy/interpreter/test/test_objspace.py Fri Feb 17 20:37:29 2006
@@ -89,6 +89,18 @@
assert not self.space.exception_match(self.space.w_ValueError,
self.space.w_LookupError)
+ def test_lookup(self):
+ w = self.space.wrap
+ w_object_doc = self.space.getattr(self.space.w_object, w("__doc__"))
+ w_instance = self.space.appexec([], "(): return object()")
+ assert self.space.lookup(w_instance, "__doc__") == w_object_doc
+ assert self.space.lookup(w_instance, "gobbledygook") is None
+ w_instance = self.space.appexec([], """():
+ class Lookup(object):
+ "bla"
+ return Lookup()""")
+ assert self.space.str_w(self.space.lookup(w_instance, "__doc__")) == "bla"
+
def test_callable(self):
def is_callable(w_obj):
return self.space.is_true(self.space.callable(w_obj))
More information about the Pypy-commit
mailing list