This issue tracker will soon become read-only and move to GitHub.
For a smoother transition, remember to log in and link your GitHub username to your profile.
For more information, see this post about the migration.

classification
Title: pathlib.Path.glob fails on empty string
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Jeffrey.Kintscher, alex.heger, iritkatriel, pablogsal, pitrou
Priority: normal Keywords:

Created on 2020-08-16 02:02 by alex.heger, last changed 2021-10-18 21:39 by iritkatriel.

Messages (4)
msg375499 - (view) Author: Alexander Heger (alex.heger) Date: 2020-08-16 02:02
Passing an empty string to pathlib.Path.glob fails.

Example
```
from pathlib import Path
path = Path('./myfile.txt')
path.glob('')
```

The result is:
```
~/Python/lib/python3.8/pathlib.py in glob(self, pattern)
   1129         """
   1130         if not pattern:
-> 1131             raise ValueError("Unacceptable pattern: {!r}".format(pattern))
   1132         drv, root, pattern_parts = self._flavour.parse_parts((pattern,))
   1133         if drv or root:

ValueError: Unacceptable pattern: ''
```

This is not the desired or expected behaviour, which would be to just return `path` if it exists.  This behaviour is also inconsistent with the documentation which states (Python 3.8.5):
"""
Glob the given relative pattern in the directory represented by this path, yielding all matching files (of any kind):
"""

And it is in contrast to the behaviour of glob.glob, which is just fine with the empty string, returning an empty list.
msg375578 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2020-08-17 23:39
> This is not the desired or expected behaviour, which would be to just return `path` if it exists. 

That's the expected behaviour if the call is successful (it does not violate any pre-condition.

> This behaviour is also inconsistent with the documentation which states

Not IMHO, what the documentation is described is the happy path/successful call. If the pattern is invalid, it can perfectly raise and error and the documentation will still be true.

> And it is in contrast to the behaviour of glob.glob, which is just fine with the empty string, returning an empty list.

This is the point that is interesting to discuss. Why is this behavior desirable? In my view, it can be the source of bugs if the user is not aware that what is placing there is an empty string. When is useful to pass something that can be an empty string?+
msg375590 - (view) Author: Alexander Heger (alex.heger) Date: 2020-08-18 04:34
In my code, having been translated form use of `os.path` to `pathlib.Path` the change in behaviour caused errors and required significant refactoring.  

Why not just return the empty list if there is no match, as is done in other cases when there is no match, except when passing the empty string.  For example, in my case, the base path already refers to a potentially existing file[name] and I then "glob" for appendices to the filename or suffices (or neither or both).  So in this case the glob for empty strong would just return the file if it exists.
msg404225 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-10-18 21:39
path.glob() returns a generator now, but the contents are still this exception:

>>> from pathlib import Path
>>> path = Path('./myfile.txt')
>>> 
>>> path.glob('')
<generator object Path.glob at 0x1059abc50>
>>> list(path.glob(''))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/iritkatriel/src/cpython-654/Lib/pathlib.py", line 1027, in glob
    raise ValueError("Unacceptable pattern: {!r}".format(pattern))
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: Unacceptable pattern: ''
>>> 


I'm changing the type as crash typically refers to segfaults or hangs and not exceptions reporting errors in input.
History
Date User Action Args
2021-10-18 21:39:41iritkatrielsetversions: + Python 3.9, Python 3.10, Python 3.11, - Python 3.8
nosy: + iritkatriel

messages: + msg404225

type: crash -> behavior
2020-08-20 04:38:08Jeffrey.Kintschersetnosy: + Jeffrey.Kintscher
2020-08-18 16:59:41pablogsalsetnosy: + pitrou
2020-08-18 04:34:00alex.hegersetmessages: + msg375590
2020-08-17 23:39:18pablogsalsetnosy: + pablogsal
messages: + msg375578
2020-08-16 02:02:44alex.hegercreate