[Python-Dev] in a pickle: method lookup irregularities

Nick Coghlan ncoghlan at gmail.com
Sun Feb 17 05:42:18 CET 2013


On Sun, Feb 17, 2013 at 1:36 PM, Eric Snow <ericsnowcurrently at gmail.com> wrote:
> (See http://bugs.python.org/issue16251)
>
> The pickle module looks up special methods on instances rather than
> using the behavior described in the language reference (or even just
> looking them up on the class).  This means that custom
> __getattribute__() and __getattr__() implementations may get triggered
> (e.g. if there is no __reduce__() implemented).
>
> Bringing pickle in sync with the language reference would not involve
> a big change, but it would impact anyone that relies on the current
> behavior.  Most notably, proxy types could exist which leverage the
> current semantics.
>
> I look at this as trading one "bug" for another in a .  So I want to
> get some general feedback on the situation and see what others
> consider to be the trade-offs of fixing or not fixing this bug.  I'm
> particularly interested in existing cases where code uses the current
> behavior.  With that in hand we could assess what alternatives exist
> in those cases or how we could better document workarounds for the
> current behavior.

I vaguely recall some discussions about this and other similar cases
of metaclass confusion (perhaps related to the copy module).

One possible approach is to look specifically for cases where
type(obj) != obj.__class__ and handle them differently.

The general trend has been towards more consistently ignoring instance
attributes when looking up special methods, but you're right that this
can be a backwards compatibility minefield.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-Dev mailing list