[Python-Dev] PyDict_SetItem hook

Amaury Forgeot d'Arc amauryfa at gmail.com
Fri Apr 3 00:26:23 CEST 2009

On Thu, Apr 2, 2009 at 03:23, Christian Heimes <lists at cheimes.de> wrote:
> John Ehresman wrote:
>> * To what extent should non-debugger code use the hook?  At one end of
>> the spectrum, the hook could be made readily available for non-debug use
>> and at the other end, it could be documented as being debug only,
>> disabled in python -O, & not exposed in the stdlib to python code.
> To explain Collin's mail:
> Python's dict implementation is crucial to the performance of any Python
> program. Modules, types, instances all rely on the speed of Python's
> dict type because most of them use a dict to store their name space.
> Even the smallest change to the C code may lead to a severe performance
> penalty. This is especially true for set and get operations.

A change that would have no performance impact could be to set mp->ma_lookup
to another function, that calls all the hooks it wants before calling
the "super()" method
This ma_lookup is already an attribute of every dict, so a debugger
could trace only
the namespaces it monitors.

The only problem here is that ma_lookup is called with the key and its hash,
but not with the value, and you cannot know whether you are reading or
setting the dict.
It is easy to add an argument and call ma_lookup with the value (or
NULL, or -1 depending
on the action: set, get or del), but this may have a slight impact
(benchmark needed!)
even if this argument is not used by the standard function.

Amaury Forgeot d'Arc

More information about the Python-Dev mailing list