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
list(pathlib.Path().glob("")) fails with IndexError #67265
Comments
glob.glob returns an empty list when passed an empty pattern as argument, but pathlib's Path.glob fails with IndexError. The first option seems more reasonable (or at least it should be a ValueError). |
In[6]: pathlib.Path.glob("")
Traceback (most recent call last):
File "/home/liuchang/ENV3/lib/python3.4/site-packages/IPython/core/interactiveshell.py", line 2883, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-6-a6dcf250fe73>", line 1, in <module>
pathlib.Path.glob("")
TypeError: glob() missing 1 required positional argument: 'pattern' the version of my python is 3.4。 |
>>> list(pathlib.Path().glob(""))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python3.5/pathlib.py", line 999, in glob
selector = _make_selector(tuple(pattern_parts))
File "/usr/lib64/python3.5/functools.py", line 458, in wrapper
result = user_function(*args, **kwds)
File "/usr/lib64/python3.5/pathlib.py", line 403, in _make_selector
pat = pattern_parts[0]
IndexError: tuple index out of range
>>> |
hi pitrou, should we fix it in _make_selector(pattern_parts) function? def _make_selector(pattern_parts):
pat = pattern_parts[0]
child_parts = pattern_parts[1:]
if pat == '**':
cls = _RecursiveWildcardSelector
elif '**' in pat:
raise ValueError("Invalid pattern: '**' can only be an entire path component")
elif _is_wildcard_pattern(pat):
cls = _WildcardSelector
else:
cls = _PreciseSelector
return cls(pat, child_parts) Is it a good fix that: check the length of pattern_parts, if its length < 2, we set pat to empty str, set child_parts to a empty list。 A simple code like: def _make_selector(pattern_parts):
try:
pat = pattern_parts[0]
child_parts = pattern_parts[1:]
except IndexError:
pat = ""
child_parts = []
if pat == '**':
cls = _RecursiveWildcardSelector
elif '**' in pat:
raise ValueError("Invalid pattern: '**' can only be an entire path component")
elif _is_wildcard_pattern(pat):
cls = _WildcardSelector
else:
cls = _PreciseSelector
return cls(pat, child_parts) |
This should be easy to fix. It should raise ValueError. |
I added a patch which causes glob to raise a ValueError exception if it is called with an empty string. I also added a test verifying the change and have run all the tests and they pass. Though I've been reading the developer guide, I'm a bit unfamiliar with the process here so I'm not totally sure I'm doing this right. I created the patch relative to the current default branch (even though the discussion here seems to indicate it should maybe be applied going back a few versions). |
New changeset 35bf4f9e68f3 by Berker Peksag in branch '3.5': New changeset 0a6290195f7c by Berker Peksag in branch 'default': |
Thanks for the patch, Thomas!
You did everything right :) I just tweaked the test a bit.
Backporting a patch is (most of the time) the core developer's responsibility. However, if the patch is going to be very different in maintenance branches (let's say 2.7 for example), it would be really helpful to attach a separate patch for 2.7. |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: