GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Pathlib.rename isn't robust · Issue #90475 · python/cpython

Pathlib.rename isn't robust #90475

New issue
Copy link
New issue
Copy link
Closed as not planned
Closed as not planned
Pathlib.rename isn't robust#90475
Copy link
Labels
3.10 (EOL)end of lifeend of life3.11only security fixesonly security fixes3.9 (EOL)end of lifeend of lifestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-pathlib

Description

oz123
mannequin
opened on Jan 9, 2022
Issue body actions
BPO 46317
Nosy @oz123, @barneygale
PRs
  • GH-73991: Add pathlib.Path.move that can handle rename across FS #30650
  • 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 = None
    created_at = <Date 2022-01-09.14:52:56.361>
    labels = ['library', '3.9', '3.10', '3.11']
    title = "Pathlib.rename isn't robust"
    updated_at = <Date 2022-01-17.19:38:48.397>
    user = 'https://github.com/oz123'

    bugs.python.org fields:

    activity = <Date 2022-01-17.19:38:48.397>
    actor = 'Oz.Tiram'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2022-01-09.14:52:56.361>
    creator = 'Oz.Tiram'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 46317
    keywords = ['patch']
    message_count = 6.0
    messages = ['410155', '410590', '410593', '410595', '410648', '410649']
    nosy_count = 2.0
    nosy_names = ['Oz.Tiram', 'barneygale']
    pr_nums = ['30650']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue46317'
    versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

    Activity

    1. oz123 commented on Jan 9, 2022

      @oz123
      oz123mannequin
      on Jan 9, 2022
      MannequinAuthor
      More actions

      Pathlib.rename will fail across file system with:

      OSError: [Errno 18] Invalid cross-device link

      e.g:
      -> path_dict["current_path"].rename(path_dict["destination"])
      (Pdb) n
      OSError: [Errno 18] Invalid cross-device link: '/tmp/pipenv-k1m0oynt-yaml/PyYAML-6.0/lib/yaml' -> '/home/oz123/Software/pipenv/pipenv/patched/yaml3'

      This is because it uses os.rename under the hood:
      https://github.com/python/cpython/blob/3.10/Lib/pathlib.py#L306

      One can either replace it with shutil.move which works, or one could
      add another method to Pathlib.move(...) with similar signature and return value, which calls shutil.move under the hood.

      Before submitting a patch for that, I would like to get feedback for that.

    2. oz123
      added
      3.9 (EOL)end of lifeend of life
      3.10 (EOL)end of lifeend of life
      3.11only security fixesonly security fixes
      stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
      on Jan 9, 2022
    3. barneygale commented on Jan 14, 2022

      @barneygale
      barneygalemannequin
      on Jan 14, 2022
      Mannequin
      More actions

      Sounds good. Would you expose the copy_function argument in pathlib, or do something else (like metadata=True)?

    4. oz123 commented on Jan 14, 2022

      @oz123
      oz123mannequin
      on Jan 14, 2022
      MannequinAuthor
      More actions

      @Barney, I am not sure that I understand your question.

      I think adding another method Pathlib.Path and Pathlib._Accessor is my preferred way. The would be something like:

          class _NormalAccessor(_Accessor):
             ...
             self.move = shutil.move
          
      
          class Path:
             ....
      
             def move(self, src, dest):
                self._accessor.move(self, target)
                return self.__class__(target)

      Now, this is hardly a patch. I need to submit a PR with proper docs, tests and NEWS entry... I will be glad to work on it. However, I guess I need someone to "sponsor" it and merge it.

    5. barneygale commented on Jan 14, 2022

      @barneygale
      barneygalemannequin
      on Jan 14, 2022
      Mannequin
      More actions

      shutil.move() accepts a copy_function argument:

          shutil.move(src, dst, copy_function=copy2)

      It's possible to set copy_function=copy to skip copying file metadata.

    6. oz123 commented on Jan 15, 2022

      @oz123
      oz123mannequin
      on Jan 15, 2022
      MannequinAuthor
      More actions

      Thanks for the answer, it makes sense now. Yes, I would adopt this.
      Allowing users to use copy2 (or any other functio ...) using a keyword.

    7. barneygale commented on Jan 15, 2022

      @barneygale
      barneygalemannequin
      on Jan 15, 2022
      Mannequin
      More actions

      Fair enough. Users who wanted to avoid copying file metadata would then do something like this, I suppose?

          import pathlib
          import shutil
      
          path = pathlib.Path('foo')
          path.move('bar', copy_function=shutil.copy)

      I guess the downside here is that users would still need to import shutil to do this. But I see the utility of allowing any copy_function to be supplied!

    8. ezio-melotti
      transferred this issue fromon Apr 10, 2022
    9. brettcannon
      added
      topic-pathlib
      on Jun 21, 2022
    10. barneygale commented on Apr 14, 2023

      @barneygale
      barneygale
      on Apr 14, 2023
      Contributor
      More actions

      Hey @oz123, I've just spotted that this issue is a duplicate of #73991. There's some more discussion there. I'll resolve this issue as a duplicate, and re-title your PR. Hope that's alright, thanks!

      Reacted by Oz Tiram
    11. barneygale
      closed this as not plannedon Apr 14, 2023
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.10 (EOL)end of lifeend of life3.11only security fixesonly security fixes3.9 (EOL)end of lifeend of lifestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-pathlib

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions

        You are visitor 01402520 — GeezerVison does not store the pages you look up.
        Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.