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

list(pathlib.Path().glob("")) fails with IndexError #67265

Closed
anntzer mannequin opened this issue Dec 17, 2014 · 8 comments
Closed

list(pathlib.Path().glob("")) fails with IndexError #67265

anntzer mannequin opened this issue Dec 17, 2014 · 8 comments
Assignees
Labels
easy stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@anntzer
Copy link
Mannequin

anntzer mannequin commented Dec 17, 2014

BPO 23076
Nosy @gvanrossum, @pitrou, @berkerpeksag, @anntzer, @ApproximateIdentity
Files
  • pathlib_glob.patch
  • 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 = 'https://github.com/pitrou'
    closed_at = <Date 2016-01-30.16:03:15.622>
    created_at = <Date 2014-12-17.23:59:40.366>
    labels = ['easy', 'type-bug', 'library']
    title = 'list(pathlib.Path().glob("")) fails with IndexError'
    updated_at = <Date 2016-04-12.19:12:57.434>
    user = 'https://github.com/anntzer'

    bugs.python.org fields:

    activity = <Date 2016-04-12.19:12:57.434>
    actor = 'zach.ware'
    assignee = 'pitrou'
    closed = True
    closed_date = <Date 2016-01-30.16:03:15.622>
    closer = 'berker.peksag'
    components = ['Library (Lib)']
    creation = <Date 2014-12-17.23:59:40.366>
    creator = 'Antony.Lee'
    dependencies = []
    files = ['41703']
    hgrepos = []
    issue_num = 23076
    keywords = ['patch', 'easy']
    message_count = 8.0
    messages = ['232841', '232856', '232919', '233290', '257571', '258877', '259259', '259260']
    nosy_count = 8.0
    nosy_names = ['gvanrossum', 'pitrou', 'Arfrever', 'python-dev', 'berker.peksag', 'Antony.Lee', 'liu chang', 'thomas.nyberg']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue23076'
    versions = ['Python 3.5', 'Python 3.6']

    @anntzer
    Copy link
    Mannequin Author

    anntzer mannequin commented Dec 17, 2014

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

    @anntzer anntzer mannequin added the stdlib Python modules in the Lib dir label Dec 17, 2014
    @liuchang
    Copy link
    Mannequin

    liuchang mannequin commented Dec 18, 2014

    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。

    @Arfrever
    Copy link
    Mannequin

    Arfrever mannequin commented Dec 19, 2014

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

    @Arfrever Arfrever mannequin changed the title path.glob("") fails with IndexError list(pathlib.Path().glob("")) fails with IndexError Dec 19, 2014
    @Arfrever Arfrever mannequin assigned pitrou Dec 19, 2014
    @liuchang
    Copy link
    Mannequin

    liuchang mannequin commented Jan 1, 2015

    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)

    @gvanrossum
    Copy link
    Member

    This should be easy to fix. It should raise ValueError.

    @gvanrossum gvanrossum added the easy label Jan 6, 2016
    @ApproximateIdentity
    Copy link
    Mannequin

    ApproximateIdentity mannequin commented Jan 23, 2016

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

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Jan 30, 2016

    New changeset 35bf4f9e68f3 by Berker Peksag in branch '3.5':
    Issue bpo-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 bpo-23076: Path.glob() now raises a ValueError if it's called with an
    https://hg.python.org/cpython/rev/0a6290195f7c

    @berkerpeksag
    Copy link
    Member

    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.

    @berkerpeksag berkerpeksag added the type-bug An unexpected behavior, bug, or error label Jan 30, 2016
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    easy stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants