<div dir="ltr">Hi all,<div><br></div><div>I have noticed there is a slight asymmetry in the way the interpreter (v3.3.5, reproduced also in v3.5.x) loads and stores globals. While loading globals from a custom mapping triggers __getitem__ just fine, writing seems to silently ignore __setitem__.</div>
<div><br></div><div><font face="courier new, monospace">class Namespace(dict):</font></div><div><font face="courier new, monospace"> def __getitem__(self, key):</font></div><div><font face="courier new, monospace"> print("getitem", key)</font></div>
<div><font face="courier new, monospace"> def __setitem__(self, key, value):</font></div><div><font face="courier new, monospace"> print("setitem", key, value)</font></div><div><font face="courier new, monospace"><br>
</font></div><div><font face="courier new, monospace">def fun():</font></div><div><font face="courier new, monospace"> global x, y</font></div><div><font face="courier new, monospace"> x # should call globals.__getitem__</font></div>
<div><font face="courier new, monospace"> y = 1 # should call globals.__setitem__</font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">exec(fun.__code__, Namespace())</font></div>
<div><font face="courier new, monospace"># => getitem x</font></div><div><br></div><div>I would have expected "setitem y 1" to show up as well, but to no avail. Am I doing something wrong? Is this on purpose?</div>
<div><br></div><div>Cheers,</div><div>Robert</div><div><br></div><div>PS. I found a 3.3.x commit (<a href="http://hg.python.org/cpython/rev/e3ab8aa0216c">e3ab8aa</a>) which fixed the LOAD_GLOBAL opcode to support other types than dict, but STORE_GLOBAL seems to use bare PyDict_SetItem instead of dispatching to PyObject_SetItem.</div>
<div><br></div></div>