classification
Title: list(pathlib.Path().glob("")) fails with IndexError
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.6, Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: pitrou Nosy List: Antony.Lee, Arfrever, berker.peksag, gvanrossum, liu chang, pitrou, python-dev, thomas.nyberg
Priority: normal Keywords: easy, patch

Created on 2014-12-17 23:59 by Antony.Lee, last changed 2016-04-12 19:12 by zach.ware. This issue is now closed.

Files
File name Uploaded Description Edit
pathlib_glob.patch thomas.nyberg, 2016-01-23 22:18 review
Messages (8)
msg232841 - (view) Author: Antony Lee (Antony.Lee) * Date: 2014-12-17 23:59
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).
msg232856 - (view) Author: liu chang (liu chang) * Date: 2014-12-18 04:30
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。
I got a TypeError, No IndexError。
msg232919 - (view) Author: Arfrever Frehtes Taifersar Arahesis (Arfrever) * (Python triager) Date: 2014-12-19 00:30
>>> 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
>>>
msg233290 - (view) Author: liu chang (liu chang) * Date: 2015-01-01 18:42
hi pitrou, should we fix it in _make_selector(pattern_parts) function? 
origin code as:

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)
msg257571 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2016-01-06 01:05
This should be easy to fix. It should raise ValueError.
msg258877 - (view) Author: Thomas Nyberg (thomas.nyberg) * Date: 2016-01-23 22:18
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).
msg259259 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-01-30 15:51
New changeset 35bf4f9e68f3 by Berker Peksag in branch '3.5':
Issue #23076: Path.glob() now raises a ValueError if it's called with an
https://hg.python.org/cpython/rev/35bf4f9e68f3

New changeset 0a6290195f7c by Berker Peksag in branch 'default':
Issue #23076: Path.glob() now raises a ValueError if it's called with an
https://hg.python.org/cpython/rev/0a6290195f7c
msg259260 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2016-01-30 16:03
Thanks for the patch, Thomas!

> 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.

You did everything right :) I just tweaked the test a bit.

> 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).

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.
History
Date User Action Args
2016-04-12 19:12:57zach.waresethgrepos: - hgrepo331
2016-01-30 16:03:15berker.peksagsetstatus: open -> closed

type: behavior
versions: - Python 3.4
nosy: + berker.peksag

messages: + msg259260
resolution: fixed
stage: resolved
2016-01-30 15:51:24python-devsetnosy: + python-dev
messages: + msg259259
2016-01-23 22:18:31thomas.nybergsetfiles: + pathlib_glob.patch

nosy: + thomas.nyberg
messages: + msg258877

hgrepos: + hgrepo331
keywords: + patch
2016-01-06 01:05:02gvanrossumsetversions: + Python 3.6
nosy: + gvanrossum

messages: + msg257571

keywords: + easy
2015-01-01 18:42:51liu changsetmessages: + msg233290
2014-12-19 00:30:29Arfreversettitle: path.glob("") fails with IndexError -> list(pathlib.Path().glob("")) fails with IndexError
nosy: + pitrou, Arfrever

messages: + msg232919

assignee: pitrou
versions: + Python 3.5
2014-12-18 04:30:48liu changsetnosy: + liu chang
messages: + msg232856
2014-12-17 23:59:40Antony.Leecreate