]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
GH-73991: Support preserving metadata in `pathlib.Path.copy()` (#120806)
authorBarney Gale <barney.gale@gmail.com>
Sat, 6 Jul 2024 16:18:39 +0000 (17:18 +0100)
committerGitHub <noreply@github.com>
Sat, 6 Jul 2024 16:18:39 +0000 (17:18 +0100)
commit88fc0655d4a487233efce293277690a799706bf9
treed39326c16c0d1881a0ba6663ed6c132b18de9d0b
parent6239d41527d5977aa5d44e4b894d719bc045860e
GH-73991: Support preserving metadata in `pathlib.Path.copy()` (#120806)

Add *preserve_metadata* keyword-only argument to `pathlib.Path.copy()`, defaulting to false. When set to true, we copy timestamps, permissions, extended attributes and flags where available, like `shutil.copystat()`. The argument has no effect on Windows, where metadata is always copied.

Internally (in the pathlib ABCs), path types gain `_readable_metadata` and `_writable_metadata` attributes. These sets of strings describe what kinds of metadata can be retrieved and stored. We take an intersection of `source._readable_metadata` and `target._writable_metadata` to minimise reads/writes. A new `_read_metadata()` method accepts a set of metadata keys and returns a dict with those keys, and a new `_write_metadata()` method accepts a dict of metadata. We *might* make these public in future, but it's hard to justify while the ABCs are still private.
Doc/library/pathlib.rst
Lib/pathlib/_abc.py
Lib/pathlib/_local.py
Lib/pathlib/_os.py
Lib/test/test_pathlib/test_pathlib.py