Title: Function attribute access doesn't invoke methods in dict subclasses
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.3
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: alex, dabeaz, daniel.urban, eric.snow, meador.inge
Priority: normal Keywords:

Created on 2013-01-08 18:43 by dabeaz, last changed 2021-01-02 02:42 by dabeaz. This issue is now closed.

Messages (2)
msg179364 - (view) Author: David Beazley (dabeaz) Date: 2013-01-08 18:43
Suppose you subclass a dictionary:

class mdict(dict):
    def __getitem__(self, index):
        print('Getting:', index)
        return super().__getitem__(index)

Now, suppose you define a function and perform these steps that reassign the function's attribute dictionary:

>>> def foo():
...     pass
>>> foo.__dict__ = mdict()
>>> foo.x = 23
>>> foo.x          # Observe: No output from overridden __getitem__
>>> type(foo.__dict__)
<class '__main__.mdict'>
>>> foo.__dict__
{'x': 23}

Carefully observe that access to foo.x does not invoke the overridden __getitem__() method in mdict.  Instead, it just directly accesses the default __getitem__() on dict. 

Admittedly, this is a really obscure corner case.  However, if the __dict__ attribute of a function can be legally reassigned, it might be nice for inheritance to work ;-).
msg179530 - (view) Author: Eric Snow (eric.snow) * (Python committer) Date: 2013-01-10 07:42
Looks like a case where the concrete dict API is ignoring subtype implementations.

In your example the attribute access will be handled by a LOAD_ATTR which calls PyObject_GetAttr() (Python/ceval.c:2369).  That ends up calling PyFunction_Type.tp_getattro (inherited from PyBaseObject_Type).

That ends up just being PyObject_GenericGetAttr() (Objects/object.c:1061).  The dict gets pulled from foo using PyFunction_Type.->tp_dictoffset and then PyDict_GetItem() gets called on it.

Unfortunately, PyDict_GetItem() is hard-coded to the dict implementation.  At this point your __getitem__() has been entirely circumvented!

FYI, this is a pain point for myself right now so it's been on my mind (COrderedDict and kwargs).  This came up in 2011.  See issue 10977.
Date User Action Args
2021-01-02 02:42:47dabeazsetstatus: open -> closed
stage: resolved
2020-03-06 19:50:01brett.cannonsetnosy: - brett.cannon
2013-01-15 03:37:16meador.ingesetnosy: + meador.inge
2013-01-10 09:56:02daniel.urbansetnosy: + daniel.urban
2013-01-10 07:42:18eric.snowsetnosy: + eric.snow
messages: + msg179530
2013-01-08 19:06:06brett.cannonsetnosy: + brett.cannon
2013-01-08 18:52:55alexsetnosy: + alex
2013-01-08 18:43:12dabeazcreate