This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Antony.Lee
Recipients Antony.Lee
Date 2016-05-31.00:22:12
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1464654135.6.0.522820749972.issue27161@psf.upfronthosting.co.za>
In-reply-to
Content
`Path().with_name` can fail with a lot of different exceptions:

    >>> Path("foo").with_name(0)
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/usr/lib/python3.5/pathlib.py", line 800, in with_name
        raise ValueError("Invalid name %r" % (name))
    ValueError: Invalid name 0

    >>> Path("foo").with_name(1)
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/usr/lib/python3.5/pathlib.py", line 797, in with_name
        drv, root, parts = self._flavour.parse_parts((name,))
    File "/usr/lib/python3.5/pathlib.py", line 62, in parse_parts
        drv, root, rel = self.splitroot(part)
    File "/usr/lib/python3.5/pathlib.py", line 268, in splitroot
        if part and part[0] == sep:
    TypeError: 'int' object is not subscriptable

    >>> Path("foo").with_name({})
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/usr/lib/python3.5/pathlib.py", line 800, in with_name
        raise ValueError("Invalid name %r" % (name))
    ValueError: Invalid name {}

    >>> Path("foo").with_name({0: 1})
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/usr/lib/python3.5/pathlib.py", line 797, in with_name
        drv, root, parts = self._flavour.parse_parts((name,))
    File "/usr/lib/python3.5/pathlib.py", line 69, in parse_parts
        parsed.append(sys.intern(rel))
    TypeError: must be str, not dict

    >>> Path("foo").with_name({"a": "b"})
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/usr/lib/python3.5/pathlib.py", line 797, in with_name
        drv, root, parts = self._flavour.parse_parts((name,))
    File "/usr/lib/python3.5/pathlib.py", line 62, in parse_parts
        drv, root, rel = self.splitroot(part)
    File "/usr/lib/python3.5/pathlib.py", line 268, in splitroot
        if part and part[0] == sep:
    KeyError: 0

While most are fairly clear, the last one is particularly confusing IMO; additionally, looking at the implementation of `_Flavour.parse_parts` there seems to be room for even more confusion with a properly crafted dict.

Of course, with Python's dynamicity a lot of exceptions can be triggered at weird places using carefully crafted objects, but I think pathlib should at least raise a clear exception when passed an incorrect builtin type.
History
Date User Action Args
2016-05-31 00:22:15Antony.Leesetrecipients: + Antony.Lee
2016-05-31 00:22:15Antony.Leesetmessageid: <1464654135.6.0.522820749972.issue27161@psf.upfronthosting.co.za>
2016-05-31 00:22:15Antony.Leelinkissue27161 messages
2016-05-31 00:22:12Antony.Leecreate