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

Path.with_name can construct invalid paths #65913

Closed
anntzer mannequin opened this issue Jun 11, 2014 · 7 comments
Closed

Path.with_name can construct invalid paths #65913

anntzer mannequin opened this issue Jun 11, 2014 · 7 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@anntzer
Copy link
Mannequin

anntzer mannequin commented Jun 11, 2014

BPO 21714
Nosy @pitrou, @anntzer
Files
  • pathlib-with_name-with_suffix.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 = None
    closed_at = <Date 2014-07-07.01:40:30.122>
    created_at = <Date 2014-06-11.05:39:26.783>
    labels = ['type-bug', 'library']
    title = 'Path.with_name can construct invalid paths'
    updated_at = <Date 2014-07-07.01:40:30.120>
    user = 'https://github.com/anntzer'

    bugs.python.org fields:

    activity = <Date 2014-07-07.01:40:30.120>
    actor = 'pitrou'
    assignee = 'none'
    closed = True
    closed_date = <Date 2014-07-07.01:40:30.122>
    closer = 'pitrou'
    components = ['Library (Lib)']
    creation = <Date 2014-06-11.05:39:26.783>
    creator = 'Antony.Lee'
    dependencies = []
    files = ['35735']
    hgrepos = []
    issue_num = 21714
    keywords = ['patch']
    message_count = 7.0
    messages = ['220235', '220269', '220327', '221263', '221329', '222429', '222431']
    nosy_count = 3.0
    nosy_names = ['pitrou', 'python-dev', 'Antony.Lee']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue21714'
    versions = ['Python 3.4', 'Python 3.5']

    @anntzer
    Copy link
    Mannequin Author

    anntzer mannequin commented Jun 11, 2014

    Path.with_name can be used to construct paths containing slashes as name contents (rather than as separators), as demonstrated below.

    $ python -c 'from pathlib import Path; print(Path("foo").with_name("bar/baz").name)'
    bar/baz

    This should be changed to either raise a ValueError, or behave like path.parent / new_name. Given the amount of checking in the related with_suffix method (and the fact that the second option can be readily implemented by hand), the first option seems preferable.

    @anntzer anntzer mannequin added the stdlib Python modules in the Lib dir label Jun 11, 2014
    @pitrou
    Copy link
    Member

    pitrou commented Jun 11, 2014

    I think raising ValueError is the way to go. Would you like to try writing a patch for it?

    @pitrou pitrou added the type-bug An unexpected behavior, bug, or error label Jun 11, 2014
    @anntzer
    Copy link
    Mannequin Author

    anntzer mannequin commented Jun 12, 2014

    I have the patch almost ready, but ran into another issue: should "path.with_name('foo/')" be allowed? It may make sense to treat it like "path.with_name('foo')", just like 'Path("foo/") == Path("foo")'.

    The implementation is also simpler with it, as it can reuse the "parse_parts" approach used by "with_suffix". But this also raises a separate issue, which is that with the current implementation, we have "Path('foo').with_suffix('.bar') == Path('foo').with_suffix('.bar/')", and that is less reasonable. Should I open a separate issue for that?

    @pitrou
    Copy link
    Member

    pitrou commented Jun 22, 2014

    should "path.with_name('foo/')" be allowed?

    For sanity, I think path separators should be disallowed.

    @anntzer
    Copy link
    Mannequin Author

    anntzer mannequin commented Jun 23, 2014

    The attached patch fixes all the issues mentioned, and also integrates the fixes of bpo-20639 (issues with with_suffix) as they are quite similar.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Jul 7, 2014

    New changeset c2636b5816a3 by Antoine Pitrou in branch '3.4':
    Issue bpo-21714: Disallow the construction of invalid paths using Path.with_name(). Original patch by Antony Lee.
    http://hg.python.org/cpython/rev/c2636b5816a3

    @pitrou
    Copy link
    Member

    pitrou commented Jul 7, 2014

    Ok, patch now applied (as well as the original patch for with_suffix()). Thank you very much!

    @pitrou pitrou closed this as completed Jul 7, 2014
    @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
    stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant