[Python-Dev] cpython: locals dictionary in PyRun_String

Brett Cannon brett at python.org
Tue Jul 14 20:47:45 CEST 2015


On Tue, Jul 14, 2015 at 11:39 AM Matthew Keeter <matt.j.keeter at gmail.com>
wrote:

> The docs for PyRun_String say that both globals and locals should be
> dictionaries [1].
>
> However, digging into the source [2] shows me that locals doesn’t need to
> be a dictionary;
> it just needs to implement the mapping protocol.  Is it a bad idea to rely
> on this fact?
>
> (Context: I’m plugging a custom object into locals that uses __getitem__
> to track lookups.)
>

As you pointed out in the code, that's in the frame creation code and not
directly the PyRun_StringFlags code, so technically there is a chance for
us to insert a PyDict_CheckExact() call before hitting the code you linked
to.

As to whether we could loosen the documented restrictions so they are
guaranteed, it would be best to file an issue at bugs.python.org requesting
the restriction be officially loosened and if people are amenable then a
test to make sure no one accidentally breaks the API promise.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20150714/2bc35a6e/attachment.html>


More information about the Python-Dev mailing list