[issue36220] LOAD_NAME and LOAD_GLOBAL, STORE_GLOBAL handle dict subclasses for globals() differently

Paul Sokolovsky report at bugs.python.org
Mon Dec 30 11:18:36 EST 2019


Paul Sokolovsky <pfalcon at users.sourceforge.net> added the comment:

> I wanted to write a sandbox for Python.

Sandbox indeed, it is.

class NS(dict):

    def __setitem__(self, k, v):
        if not isinstance(v, type(lambda: 0)):
            raise RuntimeError("Global variables considered harmful")

globals = NS()

exec("foo = 1", globals)

But then:

exec("""
global foo
foo = "watch me escaping your sandboxes"
""", globals)

This is due to STORE_GLOBAL not handling dict subclasses, unlike STORE_NAME.


While @Kevin Shweh's issue with LOAD_NAME, and @vstinner's concerns of adding yet another branch to LOAD_NAME implementation may be one matter, issue with STORE_GLOBAL is both related and somewhat different. STORE_GLOBAL's should be relatively infrequent, so adding another branch to it unlikely will be quantifiable in performance. But lack of its support disallows to write to tools which police/constrain Python's overdynamicity, which is useful in the age.

Anyway, I decided to not open a separate ticket for this, but add here. Fairly speaking, as long as work to support dict subclasses as globals/locals started, the only reasonable way forward seems to implement it completely.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue36220>
_______________________________________


More information about the Python-bugs-list mailing list