[Cython] Speedup module-level lookup

Chris Colbert sccolbert at gmail.com
Sat Jan 21 07:09:49 CET 2012


On Fri, Jan 20, 2012 at 11:58 PM, Stefan Behnel <stefan_ml at behnel.de> wrote:

> Chris Colbert, 19.01.2012 09:18:
> > If it doesn't pass PyDict_CheckExact you won't be able to use it as the
> > globals to eval or exec.
>
> What makes you say that? I tried and it worked for me, all the way back to
> Python 2.4:
>
>
Ah, you're right. I was mixing up issues I'd dealt with recently. The issue
with the eval/exec is that the globals must be a dict (or dict subclass)
whereas the locals can be a mapping. However, if you subclass dict for your
globals, any overridden __getitem__ or __setitem__ will not be called.
There is this line for eval/exec in ceval.c

if (!PyDict_Check(globals)) {
        PyErr_SetString(PyExc_TypeError,
            "exec: arg 2 must be a dictionary or None");
        return -1;

}

This allows the eval loop to use PyDict_GetItem on the globals instead of
PyObject_GetItem, so no subclass method overrides will be called.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cython-devel/attachments/20120121/2822a9c8/attachment.html>


More information about the cython-devel mailing list