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: argparse fails to detect program name when there is a slash at the end of the program's path
Type: behavior Stage: test needed
Components: Library (Lib) Versions: Python 3.4, Python 3.5, Python 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: berker.peksag, bethard, boramalper, gregory.p.smith, iritkatriel, paul.j3, r.david.murray
Priority: normal Keywords: patch

Created on 2015-04-18 17:48 by boramalper, last changed 2022-04-11 14:58 by admin.

Files
File name Uploaded Description Edit
__main__.py boramalper, 2015-04-18 17:48 An example __main__.py file that used in my example
argparse.patch boramalper, 2015-04-19 14:20 review
Messages (7)
msg241434 - (view) Author: Mert Bora Alper (boramalper) * Date: 2015-04-18 17:48
Sorry if the title is not descriptive enough.

When I try to execute a program from a directory which contains an `__main__.py` file, argparse fails to detect programs name. For example:

    $ ls foo
    __main__.py
    $ python3 foo
    usage: foo [-h] [-c COUNT] length
    foo: error: the following arguments are required: length
    $ python3 foo/
    usage: [-h] [-c COUNT] length
    : error: the following arguments are required: length

----

    >>> argparse.__version__
    '1.1'

I followed same steps using Python 2.7.6 and got same result. It will probably be same on other versions too. Same result can also be obtained when using zip files instead of directories.

I don't know if this is a bug since I don't know if I do something wrong or is this a design decision or not.

Thank you,
Bora
msg241441 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2015-04-18 18:52
I think this was just overlooked when implementing argparse.  Most code out there is likely to get the executable name using:

os.path.basename(sys.argv[0])

Which is going to do exactly what you are seeing here when sys.argv[0] ends with a /.

feel free to submit a patch.  perhaps as simple as this?

 os.path.basename(sys.argv[0].rstrip(os.path.sep))

But I'd expect a bunch of other code out there to have the same problem.
msg241518 - (view) Author: Mert Bora Alper (boramalper) * Date: 2015-04-19 14:20
> I think this was just overlooked when implementing argparse.  Most code out there is likely to get the executable name using:
>
> os.path.basename(sys.argv[0])
>
> Which is going to do exactly what you are seeing here when sys.argv[0] ends with a /.
> 
> feel free to submit a patch.  perhaps as simple as this?
>
>  os.path.basename(sys.argv[0].rstrip(os.path.sep))
>
> But I'd expect a bunch of other code out there to have the same problem.

It is exactly as you said: https://hg.python.org/cpython/file/3.4/Lib/argparse.py#l1619

Patch included. Created and tested using latest source at https://hg.python.org/cpython/.
msg241541 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-04-19 19:03
Thanks for the patch.  We'll want a unit test for the behavior before committing this.
msg241668 - (view) Author: Mert Bora Alper (boramalper) * Date: 2015-04-20 16:29
> Thanks for the patch.  We'll want a unit test for the behavior before committing this.

You're welcome. Since I have no experience in writing unit tests, I don't really know where to start but I will try to do my best.

I added bethard to the Nosy List, as he is the author of argparse's test.

(In addition, you may want to change cpython/Lib/test/test_argparse.py:2163 )
msg241741 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2015-04-21 20:11
This overlaps with http://bugs.python.org/issue22240
argparse support for "python -m module" in help

This issue also tries to handle zip files as well.

Deducing the `prog` was moved to a separate function.

One challenge with that issue was constructing a test framework.  Here the testing might simpler since the `foo/` directory doesn't have to exist (or at least we can fake it with a custom `sys.argv`).
msg408215 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-12-10 15:55
I am unable to reproduce this problem on 3.11 on a Mac.
History
Date User Action Args
2022-04-11 14:58:15adminsetstatus: pending -> open
github: 68182
2021-12-10 15:55:25iritkatrielsetstatus: open -> pending
nosy: + iritkatriel
messages: + msg408215

2015-04-21 20:11:07paul.j3setnosy: + paul.j3
messages: + msg241741
2015-04-20 16:29:43boramalpersetnosy: + bethard
messages: + msg241668
2015-04-20 03:45:53berker.peksagsetnosy: + berker.peksag
resolution: fixed ->
2015-04-19 19:03:31r.david.murraysetversions: + Python 3.5
nosy: + r.david.murray

messages: + msg241541

stage: test needed
2015-04-19 14:20:43boramalpersetfiles: + argparse.patch
resolution: fixed
messages: + msg241518

keywords: + patch
2015-04-18 18:52:39gregory.p.smithsetnosy: + gregory.p.smith
messages: + msg241441
2015-04-18 17:48:03boramalpercreate