Skip to content

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

ericsnowcurrently
Copy link
Member

@ericsnowcurrently ericsnowcurrently commented Jun 13, 2025

For several builtin functions, we now fall back to __main__.__dict__ for the globals
when there is no current frame and _PyInterpreterState_IsRunningMain() returns
true. This allows those functions to be run with Interpreter.call().

The affected builtins:

  • exec()
  • eval()
  • globals()
  • locals()
  • vars()
  • dir()

Objects/object.c Outdated
Comment on lines 2086 to 2090
if (_PyEval_GetFrame() != NULL) {
locals = _PyEval_GetFrameLocals();
}
PyThreadState *tstate = _PyThreadState_GET();
locals = _PyEval_GetGlobalsFromRunningMain(tstate);
Copy link
Contributor

@neonene neonene Jun 15, 2025

Choose a reason for hiding this comment

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

_PyEval_GetFrameLocals() returns a new reference (leak), which is overwritten by _PyEval_GetGlobalsFromRunningMain(). Thus the fallback mechanism looks to be missing.

The same goes for builtin_vars().

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

@@ -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.
Copy link
Contributor

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

Comment on lines 1432 to 1436
notshareable = [
globals,
locals,
vars,
]
Copy link
Contributor

@neonene neonene Jun 15, 2025

Choose a reason for hiding this comment

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

If "notshareable" here means that the result of the functions cannot be pickled, could NotShareableError differentiate the round-trip in the future? Also, interpreters.is_shareable() returns False for both globals and dir, which might be confusing at first.

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

@ericsnowcurrently ericsnowcurrently marked this pull request as ready for review June 16, 2025 20:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants