This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: unittest -m claims executable is "python", not "python3"
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.3
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: michael.foord Nosy List: Arnav.Khare, chris.jerdonek, larry, michael.foord, python-dev
Priority: normal Keywords: patch

Created on 2012-09-27 18:41 by larry, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
larry.unittest.argv0.1.diff larry, 2012-09-28 09:20 Patch to fix argv[0] executable for unittest. review
arnav.unittest.argv0.1.diff Arnav.Khare, 2012-09-28 14:05 review
Messages (8)
msg171387 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2012-09-27 18:41
I wrote the following script called "bonkers.py":
--
    import sys
    print(sys.argv[0])
--

then ran
    % python3 -m unittest bonkers

It printed
--
    ['python -m unittest', 'bonkers']
--

Shouldn't it say "python3", not "python"?  Maybe it should use sys.executable?  (Though that gets tricky if there are spaces in the filename.)
msg171420 - (view) Author: Chris Jerdonek (chris.jerdonek) * (Python committer) Date: 2012-09-28 09:09
It looks like the offending line is here:

http://hg.python.org/cpython/file/6ccb04c4cbae/Lib/unittest/__main__.py#l5

if sys.argv[0].endswith("__main__.py"):
    sys.argv[0] = "python -m unittest"
msg171421 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2012-09-28 09:20
Certainly.  But what is the right thing to do?

I talked to Michael about it in person this morning, and our consensus was: use basename of sys.executable.

Patch attached.
msg171441 - (view) Author: Chris Jerdonek (chris.jerdonek) * (Python committer) Date: 2012-09-28 11:43
(I was just including the line for the convenience of anyone that might happen to come across the issue.  It was not to inform you of course! :) )

FWIW, if you already know from talking to Michael or looking at the code, I think a comment saying why sys.argv[0] is being rewritten in the first place would be useful.  For example, is it strictly to have a nicer-looking usage string for --help, or are there other reasons?

$ ./python.exe -m unittest --help
Usage: python -m unittest [options]
...
msg171442 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2012-09-28 11:45
Yes it is for nicer help output, and a comment in the code is certainly warranted.
msg171454 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2012-09-28 13:13
Michael, you're the one who came up with the feature.  If you write the comment I'll fold it into the patch.
msg171470 - (view) Author: Arnav Khare (Arnav.Khare) Date: 2012-09-28 14:05
Added comment explaining why we alter the executable string.
msg171471 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2012-09-28 14:14
New changeset c76f1d78ff78 by Michael Foord in branch 'default':
Closes issue 16064. No longer hard code executable name in unittest help output.
http://hg.python.org/cpython/rev/c76f1d78ff78
History
Date User Action Args
2022-04-11 14:57:36adminsetgithub: 60268
2012-09-28 14:14:51python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg171471

resolution: fixed
stage: patch review -> resolved
2012-09-28 14:05:26Arnav.Kharesetfiles: + arnav.unittest.argv0.1.diff
nosy: + Arnav.Khare
messages: + msg171470

2012-09-28 13:13:22larrysetmessages: + msg171454
2012-09-28 11:45:36michael.foordsetmessages: + msg171442
2012-09-28 11:43:16chris.jerdoneksetmessages: + msg171441
2012-09-28 09:20:07larrysetfiles: + larry.unittest.argv0.1.diff
keywords: + patch
messages: + msg171421

stage: needs patch -> patch review
2012-09-28 09:09:11chris.jerdoneksetnosy: + chris.jerdonek
messages: + msg171420
2012-09-27 18:41:19larrycreate