Re: ObjectWrapper subclasses always callable
Marcin Tustin <[email protected]> Wed, 23 May 2012 19:18:53 +0100
| Newsgroups | gmane.comp.python.peak |
|---|---|
| Message-ID | <CAJTzQMB62pa8vMWLw6HOK6L226ct06YRCvVWCSpShUUCswO1aw@mail.gmail.com> |
--===============1039602412==
Content-Type: multipart/alternative; boundary=485b390f79fef4cd2b04c0b82a31
--485b390f79fef4cd2b04c0b82a31
Content-Type: text/plain; charset=ISO-8859-1
I'm wrapping an object to pass to a django form, to alter how it is
displayed in the form. Django's form rendering code has the code:
if callable(data):
data = data()
This breaks when passed the proxy object, because the underlying object is
not callable.
On Wed, May 23, 2012 at 7:01 PM, PJ Eby <pje-Wh6+Hckhi6HFNGf7iClzIwC/[email protected]> wrote:
> On Wed, May 23, 2012 at 11:25 AM, Marcin Tustin <[email protected]>wrote:
>
>> Python 2.7.2
>>
>>
>> On Wed, May 23, 2012 at 4:23 PM, PJ Eby <pje-Wh6+Hckhi6HFNGf7iClzIwC/[email protected]> wrote:
>>
>>>
>>> On May 23, 2012 10:39 AM, "Marcin Tustin" <[email protected]>
>>> wrote:
>>> >
>>> > I've found that instances of ObjectWrapper (and any subclasses) are
>>> always callable (the callable function returns True), notwithstanding that
>>> the __subject__ is not callable. Is there a way to remedy this?
>>> >
>>> > I've tried deleting the __call__ method, creating empty slots,
>>> creating my own call method, then trying to delete, using del or delattr on
>>> the subclass object, and creating a custom __getattribute__, all to no
>>> avail.
>>>
>>> Which Python version are you using?
>>>
>>
>> Okay, I've verified that this happens all the way back to 2.3, so that's
> not it. (I think that maybe Python 3 uses hasattr(__call__) as its
> callable() implementation, but I'm not sure.)
>
> It looks like the only way for me to fix this is with a major revision:
> change the base classes to not have a __call__ slot, and dynamically change
> type at runtime to a subclass with a __call__ slot, if and only if their
> subject is callable... and to somehow change if the subject changes. (Not
> really sure how that's going to work.)
>
> What's the use case for this? What code is using callable(), and can you
> convince its author to use hasattr instead? ;-)
>
>
>
--
Marcin Tustin
Tel: 07773 787 105
--485b390f79fef4cd2b04c0b82a31
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
I'm wrapping an object to pass to a django form, to alter how it is dis=
played in the form. Django's form rendering code has the code:<div>if c=
allable(data):</div><div>=A0 =A0 data =3D data()<br><br>This breaks when pa=
ssed the proxy object, because the underlying object is not callable.<br>
<br><div class=3D"gmail_quote">On Wed, May 23, 2012 at 7:01 PM, PJ Eby <spa=
n dir=3D"ltr"><<a href=3D"mailto:pje-Wh6+Hckhi6HFNGf7iClzIwC/[email protected]" target=3D"_blank=
">pje-Wh6+Hckhi6HFNGf7iClzIwC/[email protected]</a>></span> wrote:<br><blockquote class=3D"gmail=
_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:=
1ex">
<div class=3D"HOEnZb"><div class=3D"h5">On Wed, May 23, 2012 at 11:25 AM, M=
arcin Tustin <span dir=3D"ltr"><<a href=3D"mailto:[email protected]=
m" target=3D"_blank">[email protected]</a>></span> wrote:<br><div =
class=3D"gmail_quote">
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex">
Python 2.7.2<div><div><br><br><div class=3D"gmail_quote">On Wed, May 23, 20=
12 at 4:23 PM, PJ Eby <span dir=3D"ltr"><<a href=3D"mailto:pje@telecommu=
nity.com" target=3D"_blank">pje-Wh6+Hckhi6HFNGf7iClzIwC/[email protected]</a>></span> wrote:<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex">
<div><p><br>
On May 23, 2012 10:39 AM, "Marcin Tustin" <<a href=3D"mailto:m=
[email protected]" target=3D"_blank">[email protected]</a>> w=
rote:<br>
><br>
> I've found that instances of ObjectWrapper (and any subclasses) ar=
e always callable (the callable function returns True), notwithstanding tha=
t the __subject__ is not callable. Is there a way to remedy this?<br>
><br>
> I've tried deleting the __call__ method, creating empty slots, cre=
ating my own call method, then trying to delete, using del or delattr on th=
e subclass object, and creating a custom __getattribute__, all to no avail.=
=A0</p>
</div><p>Which Python version are you using?</p>
</blockquote></div><br></div></div></blockquote></div></div></div>Okay, I&#=
39;ve verified that this happens all the way back to 2.3, so that's not=
it.=A0 (I think that maybe Python 3 uses hasattr(__call__) as its callable=
() implementation, but I'm not sure.)<br>
<br>It looks like the only way for me to fix this is with a major revision:=
change the base classes to not have a __call__ slot, and dynamically chang=
e type at runtime to a subclass with a __call__ slot, if and only if their =
subject is callable...=A0 and to somehow change if the subject changes.=A0 =
(Not really sure how that's going to work.)<br>
<br>What's the use case for this?=A0 What code is using callable(), and=
can you convince its author to use hasattr instead?=A0 ;-)<br><br><br>
</blockquote></div><br><br clear=3D"all"><div><br></div>-- <br>Marcin Tusti=
n<br>Tel: 07773 787 105<br><br>
</div>
--485b390f79fef4cd2b04c0b82a31--
--===============1039602412==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
PEAK mailing list
[email protected]
http://www.eby-sarna.com/mailman/listinfo/peak
--===============1039602412==--