[pypy-dev] Unbound method must be called with instance as first argument

Richard Emslie rxe at ukshells.co.uk
Sat Jul 3 18:54:23 CEST 2004


Hi Seo,

Seems the problem is that Method does not have a __get__ (since C.m 
returns a Method not a Function via __get__ while assigning to D).  In 
other words

mm = C.m
D.m = mm
assert D.m == mm     # True since no __get__()
assert C.m != mm     # True since __get__() creates a new method

Some simple fixes are

1. add a __get__() method to interpreter.function.Method which forwards 
request onto w_function attribute.

2. put a hack in objspace/descrooperation.DescrOperation in get() to 
handle special case for Method (yuk!)


The CPython implementation seems to be doing some hairy caching instead of 
creating new method each __get__(), so it hard to tell if this is correct 
behaviour.  And the above fixes are only based on my understanding of 
desciptors, which has a high probability to be wrong.  If someone backs me 
up I can commit fix 1. :-)

Cheers,
Richard


On Sat, 3 Jul 2004, Seo Sanghyeon wrote:

> Consider following testcase:
>
> class C: pass
> class D(C): pass
> def m(self): return self
> C.m = m
> D.m = C.m
> print C().m()
> print D().m()
>
> CPython runs it okay, but PyPy baffles and raises TypeError, with
> message ${SUBJ}.
>
> Changing 5th line to "D.m = m" and it runs fine. The point is,
> "D.m = C.m" should transform C.m from unbound method of class C to
> unbound method of class D...
>
> This breaks mailbox.py, among other things. Read how
> PortableUnixMailbox is implemented there.
>
> Regards,
> _______________________________________________
> pypy-dev at codespeak.net
> http://codespeak.net/mailman/listinfo/pypy-dev
>



More information about the Pypy-dev mailing list