-
-
Notifications
You must be signed in to change notification settings - Fork 33.1k
gh-135443: Sometimes Fall Back to __main__.__dict__ For Globals #135491
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
Changes from 3 commits
54ea52b
39afdac
3cfddbc
1d5b7bd
2f8caf0
77fbd1f
2bf3a4b
69109c6
44a39a5
cbc4d3f
c93f890
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1414,6 +1414,41 @@ def test_call_invalid(self): | |
with self.assertRaises(interpreters.NotShareableError): | ||
interp.call(func, op, 'eggs!') | ||
|
||
def test_callable_requires_frame(self): | ||
# There are various functions tha require a current frame. | ||
interp = interpreters.create() | ||
for call, expected in [ | ||
((eval, '[1, 2, 3]'), | ||
[1, 2, 3]), | ||
((eval, 'sum([1, 2, 3])'), | ||
6), | ||
((exec, '...'), | ||
None), | ||
]: | ||
with self.subTest(str(call)): | ||
res = interp.call(*call) | ||
self.assertEqual(res, expected) | ||
|
||
notshareable = [ | ||
globals, | ||
locals, | ||
vars, | ||
] | ||
|
||
for func, expectedtype in { | ||
globals: dict, | ||
locals: dict, | ||
vars: dict, | ||
dir: list, | ||
}.items(): | ||
with self.subTest(str(func)): | ||
if func in notshareable: | ||
with self.assertRaises(interpreters.NotShareableError): | ||
interp.call(func) | ||
else: | ||
res = interp.call(func) | ||
self.assertIsInstance(res, expectedtype) | ||
self.assertIn('__builtins__', res) | ||
|
||
def test_call_in_thread(self): | ||
interp = interpreters.create() | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2083,9 +2083,23 @@ _dir_locals(void) | |
PyObject *names; | ||
PyObject *locals; | ||
|
||
locals = _PyEval_GetFrameLocals(); | ||
if (locals == NULL) | ||
if (_PyEval_GetFrame() != NULL) { | ||
locals = _PyEval_GetFrameLocals(); | ||
} | ||
PyThreadState *tstate = _PyThreadState_GET(); | ||
locals = _PyEval_GetGlobalsFromRunningMain(tstate); | ||
|
||
if (locals == NULL) { | ||
if (!_PyErr_Occurred(tstate)) { | ||
locals = _PyEval_GetFrameLocals(); | ||
assert(_PyErr_Occurred(tstate)); | ||
} | ||
} | ||
else { | ||
Py_INCREF(locals); | ||
} | ||
if (locals == NULL) { | ||
return NULL; | ||
} | ||
|
||
names = PyMapping_Keys(locals); | ||
Py_DECREF(locals); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a typo in the comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed