[Python-Dev] Using defaultdict as globals/locals for eval() (original) (raw)

Geert Jansen geertj@boskant.nl
Mon, 28 Oct 2002 15:44:43 +0100


On Mon, Oct 28, 2002 at 09:14:53AM -0500, Guido van Rossum wrote:

> > I created a derived class of the standard `dict' that fills in a > > default value when a key is not found. This is exactly the same as > > Guido describes in his "descintro" paper. I tried to use this > > dictionary as the "globals" parameter with eval(). As Guido > > already describes in his paper, this doesn't work. > > I've wanted this for years and think it would be an important > improvement. Unfortunately, it appears to be a problem without > a simple solution.

The solution appears simple (use PyObjectGetItem etc. instead of PyDictGetItem) but would cause serious performance hits: the dict API and its implementatio are highly optimized for this; PyObjectGetItem would add several levels of function calls, plus more reference count handling and exception handling.

Maybe eval() could check whether the "locals" and "globals" are plain dictionaries, or dictionary derived objects. This check would have to be done once, just after eval() is called. Depending on the result, PyDict_GetItem or PyObject_GetItem is used further on.

Hmm, this sounds too trivial. I bet there are complications...

Geert