[Python-Dev] cpython: locals dictionary in PyRun_String

2015-07-14 Thread Matthew Keeter
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.)

Thanks,
Matt

[1] https://docs.python.org/3.4/c-api/veryhigh.html#c.PyRun_StringFlags
[2] 
https://github.com/python/cpython/blob/master/Objects/frameobject.c#L628-L629___
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] cpython: locals dictionary in PyRun_String

2015-07-14 Thread Matthew Keeter
One more data point:
On the Python side, exec has documentation 
(https://docs.python.org/3/library/functions.html#exec)
that nicely reflects what’s going on in the frame code (where globals must be a 
dict but locals can be
any mapping object).

I’ll file a bug to see what people think about loosening the restrictions in 
the C API (which will make it
match exec’s documented restrictions).

Thanks,
Matt

On Jul 14, 2015, at 2:47 PM, Brett Cannon  wrote:

> 
> 
> On Tue, Jul 14, 2015 at 11:39 AM Matthew Keeter  
> 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. 

___
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com