Fix call_parse to reliably detect direct script execution#835
Open
ncoop57 wants to merge 2 commits into
Open
Conversation
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Replaces the fragile
inspect.getmodule()-based__main__check incall_parsewith a dedicated_is_script_run()helper that correctly identifies direct script execution across all three invocation styles:python foo.py,python -m foo, and%run foo.pyin notebooks. Also fixes in-process nested runners (nested=True), broken since the introduction of_in_call_parse.Changes
_is_script_run(frame): resolves and compares__file__from globals againstframe.f_code.co_filenameto confirm the frame is the top-level body of a directly-run script — not an import or nested call._finto_run_cli: auto-execution now calls it directly, since at that point we already know the file is a script entry point. Previously auto-exec re-entered_f, which infers what to do from its caller's frame — and on the auto-exec path that frame iscall_parseitself, giving the wrong answer. This restores in-process nesting (anested=Truerunner launching another script viarunpy): the inner script parses the leftoversys.argveven while the outer runner holds_in_call_parse, which previously suppressed it._fitself is unchanged: decorated functions calling each other still get plain calls (Handle nestedcall_parse#759), and interactive zero-arg calls (notebook cells, the REPL) remain plain calls rather than argv parses.setattr(mod, ...)withframe.f_globals[func.__name__] = _fto avoid theinspect.getmodule()call that could misidentify the calling context.Motivation
The previous check could incorrectly fire (or fail to fire) when
call_parse-decorated functions were imported into a__main__module, or called programmatically from a script. Additionally, nested runners silently passed defaults instead of parsing leftoversys.argv, since the inner script's entry point was treated as a plain function call.