From 7f614b1239476ebc8f926bd8171b23d4640b23c3 Mon Sep 17 00:00:00 2001 From: Savannah Ostrowski Date: Tue, 4 Aug 2026 19:25:17 -0700 Subject: [PATCH 1/4] Preserve argparse prog for programmatic module-as-main execution --- Lib/argparse.py | 3 +++ Lib/test/test_argparse.py | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/Lib/argparse.py b/Lib/argparse.py index 0840c4c45cffaa..be5a594f0f360c 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -1964,6 +1964,9 @@ def _prog_name(prog=None): if modspec is None: # simple script return _os.path.basename(arg0) + if modspec.name != '__main__' and arg0 != modspec.origin: + # imported module executed as main without altering sys.argv[0] + return _os.path.basename(arg0) py = _os.path.basename(_sys.executable) if modspec.name != '__main__': # imported module or package diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index 287fcb7a084a49..ae02cd11804e50 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -7346,6 +7346,19 @@ def test_module(self, compiled=False): def test_module_compiled(self): self.test_module(compiled=True) + def test_module_as_main_without_altering_argv(self): + basename = 'module' + os_helper.FS_NONASCII + modulename = f'{self.dirname}.{basename}' + self.make_script(self.dirname, basename) + runner_source = textwrap.dedent(f'''\ + import runpy + runpy._run_module_as_main({modulename!r}, alter_argv=False) + ''') + runner = script_helper.make_script( + self.dirname, 'runner', runner_source) + self.check_usage(os.path.basename(runner), runner, + PYTHONPATH=os.curdir) + def test_package(self, compiled=False): basename = 'subpackage' + os_helper.FS_NONASCII packagename = f'{self.dirname}.{basename}' From 1e72ca776f60b393335767bf66a60bb6ee335483 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Wed, 5 Aug 2026 02:36:24 +0000 Subject: [PATCH 2/4] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Library/2026-08-05-02-36-18.gh-issue-155009.YiJAiP.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-08-05-02-36-18.gh-issue-155009.YiJAiP.rst diff --git a/Misc/NEWS.d/next/Library/2026-08-05-02-36-18.gh-issue-155009.YiJAiP.rst b/Misc/NEWS.d/next/Library/2026-08-05-02-36-18.gh-issue-155009.YiJAiP.rst new file mode 100644 index 00000000000000..dc67858e54a497 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-08-05-02-36-18.gh-issue-155009.YiJAiP.rst @@ -0,0 +1,3 @@ +Fix :class:`argparse.ArgumentParser` to preserve the program name from +``sys.argv[0]`` when a named module is executed as the main program without +replacing ``sys.argv[0]``. From 8d1379d2241e8951dcdab8929366485b92d9c565 Mon Sep 17 00:00:00 2001 From: Savannah Ostrowski Date: Tue, 4 Aug 2026 19:37:20 -0700 Subject: [PATCH 3/4] Improve comment --- Lib/argparse.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/argparse.py b/Lib/argparse.py index be5a594f0f360c..25994d7a389efa 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -1965,7 +1965,7 @@ def _prog_name(prog=None): # simple script return _os.path.basename(arg0) if modspec.name != '__main__' and arg0 != modspec.origin: - # imported module executed as main without altering sys.argv[0] + # named module executed as main without altering sys.argv[0] return _os.path.basename(arg0) py = _os.path.basename(_sys.executable) if modspec.name != '__main__': From b576d71cd383b87880d004f91328c575fa690f64 Mon Sep 17 00:00:00 2001 From: Savannah Ostrowski Date: Tue, 4 Aug 2026 21:10:30 -0700 Subject: [PATCH 4/4] Normalize NEWS entry line endings --- .../Library/2026-08-05-02-36-18.gh-issue-155009.YiJAiP.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2026-08-05-02-36-18.gh-issue-155009.YiJAiP.rst b/Misc/NEWS.d/next/Library/2026-08-05-02-36-18.gh-issue-155009.YiJAiP.rst index dc67858e54a497..219c6a94b91cb6 100644 --- a/Misc/NEWS.d/next/Library/2026-08-05-02-36-18.gh-issue-155009.YiJAiP.rst +++ b/Misc/NEWS.d/next/Library/2026-08-05-02-36-18.gh-issue-155009.YiJAiP.rst @@ -1,3 +1,3 @@ -Fix :class:`argparse.ArgumentParser` to preserve the program name from -``sys.argv[0]`` when a named module is executed as the main program without +Fix :class:`argparse.ArgumentParser` to preserve the program name from +``sys.argv[0]`` when a named module is executed as the main program without replacing ``sys.argv[0]``.