Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-94808: Cover LOAD_GLOBAL for custom dict subtypes #96767

Merged
merged 3 commits into from
Nov 4, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Lib/test/test_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,32 @@ def __setitem__(self, key, value):
self.assertRaises(frozendict_error,
exec, code, namespace)

# custom `globals` or `builtins` can raise errors on item access
class setonlyerror(Exception):
pass

class setonlydict(dict):
def __getitem__(self, key):
raise setonlyerror

class customdict(dict): # this one should not do anything fancy
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be defined just before the test that uses it (below). Ideally this function would be broken up - it's testing a lot of things.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! Thanks!

pass

# globals' `__getitem__` raises
code = compile("print(globalname)", "test", "exec")
self.assertRaises(setonlyerror,
exec, code, setonlydict({'globalname': 1}))

# builtins' `__getitem__` raises
code = compile("print(superglobal)", "test", "exec")
self.assertRaises(setonlyerror, exec, code,
{'__builtins__': setonlydict({'superglobal': 1})})

# custom builtins dict subclass is missing key
code = compile("print(superglobal)", "test", "exec")
self.assertRaises(NameError, exec, code,
{'__builtins__': customdict()})

def test_exec_redirected(self):
savestdout = sys.stdout
sys.stdout = None # Whatever that cannot flush()
Expand Down