diff options
author | Benjamin Peterson <benjamin@python.org> | 2009-05-08 03:06:00 (GMT) |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2009-05-08 03:06:00 (GMT) |
commit | 399e4c4f8f2570a342ac50e9500b12e86eb330a9 (patch) | |
tree | 46472e94ea46a4fbe8c3eebe3a30ef9fc529f6f8 /Lib | |
parent | d846f1d4c21f4589966512f78a4a4d74f8399d6d (diff) | |
download | cpython-399e4c4f8f2570a342ac50e9500b12e86eb330a9.zip cpython-399e4c4f8f2570a342ac50e9500b12e86eb330a9.tar.gz cpython-399e4c4f8f2570a342ac50e9500b12e86eb330a9.tar.bz2 |
add _PyObject_LookupSpecial to handle fetching special method lookup
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_descr.py | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index ae22af7..46fb581 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -1665,6 +1665,58 @@ order (MRO) for bases """ self.assertEqual(E().foo, C.foo) # i.e., unbound self.assert_(repr(C.foo.__get__(C(1))).startswith("<bound method ")) + def test_special_method_lookup(self): + # The lookup of special methods bypasses __getattr__ and + # __getattribute__, but they still can be descriptors. + + def run_context(manager): + with manager: + pass + def iden(self): + return self + def hello(self): + return "hello" + + # It would be nice to have every special method tested here, but I'm + # only listing the ones I can remember outside of typeobject.c, since it + # does it right. + specials = [ + ("__unicode__", unicode, hello), + # These two fail because the compiler generates LOAD_ATTR to look + # them up. We'd have to add a new opcode to fix this, and it's + # probably not worth it. + # ("__enter__", run_context, iden), + # ("__exit__", run_context, iden), + ] + + class Checker(object): + def __getattr__(self, attr, test=self): + test.fail("__getattr__ called with {0}".format(attr)) + def __getattribute__(self, attr, test=self): + test.fail("__getattribute__ called with {0}".format(attr)) + class SpecialDescr(object): + def __init__(self, impl): + self.impl = impl + def __get__(self, obj, owner): + record.append(1) + return self + def __call__(self, *args): + return self.impl(*args) + + + for name, runner, meth_impl in specials: + class X(Checker): + pass + setattr(X, name, staticmethod(meth_impl)) + runner(X()) + + record = [] + class X(Checker): + pass + setattr(X, name, SpecialDescr(meth_impl)) + runner(X()) + self.assertEqual(record, [1], name) + def test_specials(self): # Testing special operators... # Test operators like __hash__ for which a built-in default exists |