Repository navigation
Closed
Labels
docsDocumentation in the Doc dir
Description
| BPO | 643841 |
|---|---|
| Nosy | @gvanrossum, @warsaw, @birkenfeld, @rhettinger, @terryjreedy, @jcea, @ncoghlan, @PiDelport |
| Files |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee = 'https://github.com/ncoghlan'
closed_at = <Date 2008-08-31.12:46:05.409>
created_at = <Date 2002-11-25.23:37:25.000>
labels = ['docs']
title = 'New class special method lookup change'
updated_at = <Date 2008-08-31.12:46:05.386>
user = 'https://github.com/terryjreedy'bugs.python.org fields:
activity = <Date 2008-08-31.12:46:05.386>
actor = 'ncoghlan'
assignee = 'ncoghlan'
closed = True
closed_date = <Date 2008-08-31.12:46:05.409>
closer = 'ncoghlan'
components = ['Documentation']
creation = <Date 2002-11-25.23:37:25.000>
creator = 'terry.reedy'
dependencies = []
files = ['10437', '10438', '10439', '10521', '10586', '11056']
hgrepos = []
issue_num = 643841
keywords = ['patch']
message_count = 47.0
messages = ['60293', '60294', '60295', '64641', '64678', '64813', '64853', '64854', '65179', '65575', '65677', '67127', '67136', '67155', '67190', '67191', '67201', '67207', '67209', '67223', '67224', '67257', '67276', '67346', '67712', '67713', '67719', '67744', '67948', '67954', '67961', '67962', '67963', '67976', '67977', '67987', '67996', '68001', '69386', '69675', '69676', '70694', '70696', '70699', '70700', '70701', '72206']
nosy_count = 11.0
nosy_names = ['gvanrossum', 'barry', 'georg.brandl', 'rhettinger', 'terry.reedy', 'jcea', 'ncoghlan', 'Rhamphoryncus', 'jkrukoff', 'PiDelport', 'kundeng06']
pr_nums = []
priority = 'critical'
resolution = 'fixed'
stage = None
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue643841'
versions = ['Python 2.6', 'Python 3.0']
Activity
terryjreedy commentedon Nov 25, 2002
The lookup of special methods invoked implicitly by
syntax other than explicit instance.attribute changed
to *not* use __gettattr__ when normal lookup failed.
This is contrary to docs, which consistently say
__getattr__ is unchanged. New special method
__getattribute__ is also bypassed, contrary to
implication of doc-essay.
This was reported on c.l.p by Jan Decaluwe using
2.2.2 on Red Hat Linux. On Win98, 2.2.1, I get same
behavior (I added test of __getattribute__):
http://python.org/2.2/descrintro.html says:
"Note that while in general operator overloading
works just as for classic classes, there are some
differences. (The biggest one is the lack of support for
__coerce__; new-style classes should always use the
new-style numeric API, which passes the other
operand uncoerced to the __add__ and __radd__
methods, etc.) "
Was lookup change meant to be one of differences?
"There's a new way of overriding attribute access. The
__getattr__ hook, if defined, works the same way as it
does for classic classes: it is only called if the regular
way of searching for the attribute doesn't find it."
But it is different.
"But you can now also override __getattribute__, a
new operation that is called for all attribute
references."
Except for implicit special methods.
I did not classify discrepancy because I don't know
whether interpreter or docs are off.
terryjreedy commentedon Nov 26, 2002
Logged In: YES
user_id=593130
Print-style 'debugging' output provided by Bengt Richter in
a follow-up in the c.l.p. thread "Issue with new-style
classes and operators" showed that 'a+1' worked because
of a __getattr__ call getting '__coerce__' (rather
than '__add__') and that 'b+1' did not trigger such a call.
So I presume the quoted parenthesized statement about
__coerce__ and 'new-style numeric API' was meant to
explain as least this part of the change in behavior.
However, it is still not clear to me, even after reading the
development (2.3a) version of the ref manual, why failure
to find '__add__' in 'the usual places' (to quote RefMan
3.3.2 __getattr__ entry) does not trigger a call to
__getattr__, nor why the initial attempt to find it did not
trigger __getattribute__. The sentence 'For objects x and y,
first x.__op__(y) is tried' (3.3.7) implies to me that there is
an attempt to find __op__ as an attribute of x. Unless I
missed something I should have found, a bit more
clarification might be usefully added.
ncoghlan commentedon Sep 27, 2004
Logged In: YES
user_id=1038590
FWIW, this is still true for Python 2.4 (i.e. the search for
'__add__' by '+' invokes neither __getattr__ nor
__getattribute__).
The 'infinite loop' is courtesy of the call to 'self.ob'
inside __getattr__.
Also of interest - int(b) fails with a TypeError, and str(b)
returns the generic object representation of b, rather than
delegating to the contained object.
So it looks like these methods are not invoked when looking
up any 'magic methods' recognised by the interpreter.
Inspection of PyNumber_Add shows that this is the case - the
objects' method pointers are inspected directly at the C level.
To me, this looks like a documentation bug. Magic methods
are found by checking if the associated slot in the method
structure is not NULL, rather than by actually looking for
the relevant magic 'attribute'.
In order to change the 'implicit' behaviour of the object,
it is necessary to change the contents of the underlying
method slot - a task which is carried out by type whenever
an attribute is set, or when a new instance of type is created.
jkrukoff commentedon Mar 28, 2008
I was just bit by this today in converting a proxy class from old style
to new style. The official documentation was of no help in discoverting
that neither __getattr__ or __getattribute__ are used to look up magic
attribute names. Even the link to "New-style Classes" off the
development documentation page is useless, as none of the resources
there (http://www.python.org/doc/newstyle/) mention the incompatible change.
This seems like an issue that is going to come up more frequently as
python 3000 pushes everyone to using only new style classes. It'd be
very useful if whatever conversion tool we get, or the python 3000
standard library includes a proxy class or metaclass that is able to
help with this conversion, such as this one:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/252151
Though preferably with some knowledge of all exising magic names.
ncoghlan commentedon Mar 29, 2008
The 2.6 and 3.0 documentation has already been updated appropriately.
The forward compatible way to handle this is to inherit from object and
override the special methods explicitly in the 2.x version (Yes this can
make writing proxy objects more tedious, but from our experience with
the tempfile module, I would say that the bulk of proxy objects that
aren't overriding special methods on a case-by-case basis are probably
broken anyway).
It may be appropriate to add a -3 warning that is triggered whenever a
special method is retrieved via __getattr__ on a classic class.
jkrukoff commentedon Apr 1, 2008
I assume when you say that the documentation has already been updated,
you mean something other than what's shown at:
http://docs.python.org/dev/reference/datamodel.html#new-style-and-
classic-classes
or
http://docs.python.org/dev/3.0/reference/datamodel.html#new-style-and-
classic-classes ?
As both of those claim to still not be up to date in relation to new
style classes, and the __getattr__ & __getattribute__ sections under
special names make no reference to how magic methods are handled.
Additionally, the "Class Instances" section under the type heirachy
makes mention of how attributes are looked up, but doesn't mention the
new style differences even in the 3.0 documentation.
Sure, there's this note under "Special Method Names":
For new-style classes, special methods are only guaranteed to work if
defined in an object’s class, not in the object’s instance dictionary.
But that only helps you figure it out if you already know what the
problem is, and it's hardly comprehensive.
I'm not arguing that this is something that's going to change, as we're
way past the point of discussion on the implementation, but this looks
far more annoying if you're looking at it from the perspective of
proxying to container classes or numeric types in a generic fashion. My
two use cases I've had to convert are for lazy initialization of an
object and for building an object that uses RPC to proxy access to an
object to a remote server.
In both cases, since they are generic proxies that once initialized are
supposed to behave exactly like the proxied instance, the list of magic
methods to pass along is ridiculously long. Sure, I have to handle
__copy__ & __deepcopy__, and __getstate__ & __setstate__ to make sure
that they return instances of the proxy rather than the proxied object,
but other than that there's over 50 attributes to override for new
style classes just to handle proxying to numeric and container types.
It's hard to get fancy about it too, as I can't just dynamically add
the needed attributes to my instances by looking at the object to be
proxied, it really has to be a static list of everything that python
supports on the class. Additionally, while metaclasses might help here,
there's still the problem that while my old style proxy class has
continued to work fine as magic attributes have been added over python
revisions, my new style equivalent will have to be updated work
currectly as magic methods are added. Which, given the 2.x track seems
to happen pretty frequently. Some of the bugs from that would have been
quite tricky to track down too, such as the __cmp__ augmentation with
the rich comparison methods.
None of the solutions really seem ideal, or at least as good as what
old style classes provided, which is why I was hoping for some support
in the 3.0 standard library or the conversion tool.
ncoghlan commentedon Apr 2, 2008
Agreed that section of the docs should be more explicit in pointing out
that __getattr__ and __getattribute__ won't work for proxying special
methods for new-style classes.
It's also true that there are quite a few special methods where nothing
special is needed to correctly proxy the methods. That said, defining
all of the methods blindly is also bad (as a lot of informal type
testing is done by checking for certain special methods as attributes).
Perhaps it would be useful to have a method on type instances that could
be explicitly invoked to tell the type to run through all the tp_* slots
and populate them based on doing getattr(self, "__slotname__").
(Note that it's only those methods with dedicated slots in the C type
structure that are a problem here - those which are implemented solely
as normal Python methods like __enter__, __exit__ or the pickle module's
special methods can typically be overridden as expected through the use
of __getattr__ and __getattribute__)
ncoghlan commentedon Apr 2, 2008
I've started a discussion on the Py3k development list regarding the
possibility of adding a new method to type to aid in converting proxy
classes from classic- to new-style.
ncoghlan commentedon Apr 8, 2008
I spent an enlightening evening browsing through the source code for
weakref.proxy. The way that code works is to define every slot,
delegating to the proxied object to handle each call (wrapping and
unwrapping the proxied object as needed).
This is normally transparent to the user due to the fact that
__getattribute__ is one of the proxied methods (and at the C level, the
delegated slot invocations return NotImplemented or set the appropriate
exceptions). The only way it shows through is the fact that
operator.isNumber and operator.isMapping will always return True for the
proxy instance, and operator.isSequence will always return False - this
is due to the proxy type filling in the number and mapping slots, but
not the sequence slots.
However, this prompted me to try an experiment (Python 2.5.1), and the
results didn't fill me with confidence regarding the approach of
expecting 3rd party developers to explicitly delegate all of the special
methods:
Oops.
PiDelport commentedon Apr 17, 2008
Somewhat related: bpo-2605 (descriptor __get__/set/delete)
jkrukoff commentedon Apr 22, 2008
I've been following the py3k maliing list disscussion for this issue,
and wanted to add a note about the proposed solution described here:
http://mail.python.org/pipermail/python-3000/2008-April/013004.html
The reason I think this approach is valuable is that in all of the
proxy classes I've written, I'm concerned about which behaviour of the
proxied class I want to override, not which behaviour I want to keep.
In other words, when I proxy something, my mental model has always
been, okay, I want something that behaves just like X, except it does
this (usually small bit) differently.
This is also why I expect my proxies to keep working the same when I
change the proxied class, without having to go and update the proxy to
also use the new behaviour.
So, yeah, very much in favor of a base proxy class in the standard
library.
44 remaining items
ncoghlan commentedon Aug 4, 2008
Attaching a documentation patch for the moment until I get some info
back from Georg as to why I can't build the docs locally.
Once I get my local doc build working again, I'll check the formatting
and check it in.
ncoghlan commentedon Aug 4, 2008
Committed for 2.6 as r65487.
I also blocked the automatic merge to 3.0 since the references to
old-style classes don't make sense there.
birkenfeld commentedon Aug 4, 2008
But don't the docs with patch describe the behavior of new-style classes
better?
ncoghlan commentedon Aug 4, 2008
I meant to say that I will be merging it manually to avoid bringing the
old-style class specific parts over (that's why I left the issue open
and assigned to me).
birkenfeld commentedon Aug 4, 2008
Ah, I'm sorry for the noise then.
ncoghlan commentedon Aug 31, 2008
Docs updated for 3.0 in r66084 (and I was right in thinking the
automatic merge didn't have a hope of getting this right - there were
conflicts all the way through the file).
Closing this issue after a mere 5 years and 9 months - any requests for
better proxying/special method delegation support in the standard
library should be raised in a new tracker issue as a feature request for
2.7/3.1.