bpo-36220: STORE_GLOBAL: Support dict subclasses for globals. by pfalcon · Pull Request #18033 · python/cpython (original) (raw)
If globals are not concrete dict
type, use PyObject_SetItem() on it,
instead of PyDict_SetItem(). This is similar how STORE_NAME deals
with locals already. (And also similar to how some (but not all)
LOAD_* opcodes deal globals/locals),
Given that on the module level, locals and globals are the same,
this change fixed the situation when:
exec("foo = 1", my_globals)
and
exec("global foo; foo = 1", my_globals)
(where "my_globals" is a dict subclass, intended to trace namespace
operations)
lead to different behavior, where in first case, my_globals.setitem()
is called (because STORE_NAME opcode is generated), while in second case,
it's not called (because STORE_GLOBAL is generated).
Note that this change is not related in any way to security sandboxing
(in a sense that this change doesn't make any claims in regard to
improvements to such secuirty sandboxing matters). Instead, it's intended
to make the light-weight instrumentation of Python code for generic
purposes more consistent and predictable (e.g. to collect statistics,
profile code, etc).