Skip to content
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

glob.glob handling of * (asterisk) wildcard is broken #88546

Closed
max0x7ba mannequin opened this issue Jun 10, 2021 · 6 comments
Closed

glob.glob handling of * (asterisk) wildcard is broken #88546

max0x7ba mannequin opened this issue Jun 10, 2021 · 6 comments
Labels
3.9 only security fixes 3.10 only security fixes 3.11 only security fixes docs Documentation in the Doc dir type-feature A feature request or enhancement

Comments

@max0x7ba
Copy link
Mannequin

max0x7ba mannequin commented Jun 10, 2021

BPO 44380
Nosy @akheron, @serhiy-storchaka

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2021-06-10.15:53:43.363>
labels = ['3.11', 'type-feature', '3.9', '3.10', 'docs']
title = 'glob.glob handling of * (asterisk) wildcard is broken'
updated_at = <Date 2021-06-10.16:33:19.945>
user = 'https://bugs.python.org/max0x7ba'

bugs.python.org fields:

activity = <Date 2021-06-10.16:33:19.945>
actor = 'max0x7ba'
assignee = 'docs@python'
closed = False
closed_date = None
closer = None
components = ['Documentation']
creation = <Date 2021-06-10.15:53:43.363>
creator = 'max0x7ba'
dependencies = []
files = []
hgrepos = []
issue_num = 44380
keywords = []
message_count = 5.0
messages = ['395545', '395547', '395548', '395550', '395551']
nosy_count = 6.0
nosy_names = ['docs@python', 'nailor', 'petri.lehtinen', 'max0x7ba', 'serhiy.storchaka', 'Isaac Muse']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue44380'
versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

@max0x7ba
Copy link
Mannequin Author

max0x7ba mannequin commented Jun 10, 2021

Problem:

glob.glob documentation states that "pathname ... can contain shell-style wildcards."

However, it stops short of saying that shell-style wildcards are handled the same way as in a POSIX-compliant/friendly shell.

https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_13_02 POSIX requires that "* (asterisk) is a pattern that shall match any string, including the null string."

However, glob.glob pattern * (asterisk) doesn't match an empty/null string.

Reproduction:

$ ls *.bash_profile
.bash_profile
$ python3 -c 'import glob; print(glob.glob("*.bash_profile"))'
[]
$ python3 -c 'import glob; print(glob.glob(".bash_profile"))'
['.bash_profile']

@max0x7ba max0x7ba mannequin added stdlib Python modules in the Lib dir 3.8 only security fixes type-bug An unexpected behavior, bug, or error labels Jun 10, 2021
@serhiy-storchaka
Copy link
Member

Only pattern beginning with a dot can match filename beginning with a dot.

From https://docs.python.org/3/library/glob.html

Note that unlike fnmatch.fnmatch(), glob treats filenames beginning with a dot (.) as special cases.

This phrase was added in bpo-16695 in attempt to improve documentation, but it is still not clear.

@serhiy-storchaka serhiy-storchaka added docs Documentation in the Doc dir 3.9 only security fixes 3.10 only security fixes 3.11 only security fixes and removed stdlib Python modules in the Lib dir 3.8 only security fixes labels Jun 10, 2021
@serhiy-storchaka serhiy-storchaka added docs Documentation in the Doc dir type-feature A feature request or enhancement 3.9 only security fixes 3.10 only security fixes 3.11 only security fixes and removed type-bug An unexpected behavior, bug, or error stdlib Python modules in the Lib dir 3.8 only security fixes labels Jun 10, 2021
@serhiy-storchaka serhiy-storchaka added type-feature A feature request or enhancement and removed type-bug An unexpected behavior, bug, or error labels Jun 10, 2021
@max0x7ba
Copy link
Mannequin Author

max0x7ba mannequin commented Jun 10, 2021

I may be naive, but why then:

$ python3 -c 'from pathlib import Path; print(list(Path(".").glob("*.bash_profile")))'

Outputs:

[PosixPath('.bash_profile')]

?

@IsaacMuse
Copy link
Mannequin

IsaacMuse mannequin commented Jun 10, 2021

Sadly, this because pathlib glob and glob.glob use different implementations. And glob.glob does not provide something equivalent to a DOTALL flag allowing a user to glob hidden files without explicitly defining the leading dot in the pattern.

@max0x7ba
Copy link
Mannequin Author

max0x7ba mannequin commented Jun 10, 2021

glob.glob does not provide something equivalent to a DOTALL flag

I see now, said a blind man.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
miss-islington pushed a commit to miss-islington/cpython that referenced this issue May 2, 2022
…eordered sentence (pythonGH-91614)

(cherry picked from commit b9ab6ce)

Co-authored-by: slateny <46876382+slateny@users.noreply.github.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue May 2, 2022
…eordered sentence (pythonGH-91614)

(cherry picked from commit b9ab6ce)

Co-authored-by: slateny <46876382+slateny@users.noreply.github.com>
miss-islington added a commit that referenced this issue May 2, 2022
…ed sentence (GH-91614)

(cherry picked from commit b9ab6ce)

Co-authored-by: slateny <46876382+slateny@users.noreply.github.com>
miss-islington added a commit that referenced this issue May 2, 2022
…ed sentence (GH-91614)

(cherry picked from commit b9ab6ce)

Co-authored-by: slateny <46876382+slateny@users.noreply.github.com>
@hauntsaninja
Copy link
Contributor

hauntsaninja commented May 15, 2022

While a little surprising, this has been the behaviour of glob since 1991 (the relevant check looked like if name[0] <> '.' or pattern[0] = '.': at the time). It's been explicitly documented for a decade, and now thanks to this issue, more clearly documented.

hello-adam pushed a commit to hello-adam/cpython that referenced this issue Jun 2, 2022
…eordered sentence (pythonGH-91614)

(cherry picked from commit b9ab6ce)

Co-authored-by: slateny <46876382+slateny@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.9 only security fixes 3.10 only security fixes 3.11 only security fixes docs Documentation in the Doc dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants