Re: Two curious errors when function globals are manipulated

2016-07-05 Thread Steven D'Aprano
On Wed, 6 Jul 2016 02:13 am, eryk sun wrote: > On Tue, Jul 5, 2016 at 3:50 PM, Steven D'Aprano > wrote: >> It works with exec: [...] > No, actually it doesn't work. Remember that when you store to a > variable, it's implicitly a local variable, for which CPython uses > STORE_NAME in unoptimized c

Re: Two curious errors when function globals are manipulated

2016-07-05 Thread eryk sun
On Tue, Jul 5, 2016 at 4:12 PM, Random832 wrote: > So any special logic in your own __setitem__, which may have > included e.g. copying it to an alternate place, changing the key or > value, or simply refusing to add the item to the dictionary at all, will > be ignored, and your object may end up

Re: Two curious errors when function globals are manipulated

2016-07-05 Thread eryk sun
On Tue, Jul 5, 2016 at 3:50 PM, Steven D'Aprano wrote: > It works with exec: > > py> from collections import ChainMap > py> class ChainDict(ChainMap, dict): > ... pass > ... > py> m = ChainDict() > py> exec("x = 1", m, m) > py> m['x'] > 1 > > (Tested in both 3.3 and 3.6.) No, actually it does

Re: Two curious errors when function globals are manipulated

2016-07-05 Thread Random832
On Tue, Jul 5, 2016, at 11:50, Steven D'Aprano wrote: > If PyDict_SetItem expects an actual dict (accept no substitutes or > subclasses), why is there no error? I would have expected a segfault at > worst or at least an exception. Subclasses are fine. Sort of. In general if you pass a *subclass* o

Re: Two curious errors when function globals are manipulated

2016-07-05 Thread eryk sun
On Tue, Jul 5, 2016 at 3:37 PM, eryk sun wrote: > In CPython 3.6, LOAD_GLOBAL does fall back on PyObject_GetItem I did some digging to find when this changed in 3.3: https://hg.python.org/cpython/diff/e3ab8aa0216c/Python/ceval.c Notice in the last comment on issue 14385 that Martijn wanted to u

Re: Two curious errors when function globals are manipulated

2016-07-05 Thread Steven D'Aprano
On Wed, 6 Jul 2016 01:27 am, eryk sun wrote: > On Tue, Jul 5, 2016 at 2:46 PM, Steven D'Aprano > wrote: >> I've come across two curious behaviours of a function using custom >> globals. In both cases, I have a function where __globals__ is set to a >> ChainMap. > > ChainMap implements the Mutabl

Re: Two curious errors when function globals are manipulated

2016-07-05 Thread eryk sun
On Tue, Jul 5, 2016 at 3:27 PM, eryk sun wrote: > ChainMap implements the MutableMapping abstract base class. But > CPython needs globals to be a dict. In the current implementation, > LOAD_GLOBAL calls _PyDict_LoadGlobal, and STORE_GLOBAL calls > PyDict_SetItem. They don't fall back on the abstra

Re: Two curious errors when function globals are manipulated

2016-07-05 Thread eryk sun
On Tue, Jul 5, 2016 at 2:46 PM, Steven D'Aprano wrote: > I've come across two curious behaviours of a function using custom globals. > In both cases, I have a function where __globals__ is set to a ChainMap. ChainMap implements the MutableMapping abstract base class. But CPython needs globals to