Skip to content

Commit e75ef6a

Browse files
gh-155040: Support copy.replace() for tarfile.TarInfo (GH-155046)
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent b4af851 commit e75ef6a

4 files changed

Lines changed: 19 additions & 0 deletions

File tree

Doc/library/tarfile.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,11 @@ A ``TarInfo`` object has the following public data attributes:
963963
If *deep* is false, the copy is shallow, i.e. ``pax_headers``
964964
and any custom attributes are shared with the original ``TarInfo`` object.
965965

966+
This method is also used by :func:`copy.replace`.
967+
968+
.. versionchanged:: next
969+
Added support for :func:`copy.replace`.
970+
966971
A :class:`TarInfo` object also provides some convenient query methods:
967972

968973

Lib/tarfile.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,8 @@ def replace(self, *,
10361036
result.gname = gname
10371037
return result
10381038

1039+
__replace__ = replace
1040+
10391041
def get_info(self):
10401042
"""Return the TarInfo's attributes as a dictionary.
10411043
"""

Lib/test/test_tarfile.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import copy
12
import errno
23
import sys
34
import os
@@ -3524,6 +3525,16 @@ def test_replace_internal(self):
35243525
with self.assertRaises(TypeError):
35253526
member.replace(offset=123456789)
35263527

3528+
def test_copy_replace(self):
3529+
member = self.tar.getmember('ustar/regtype')
3530+
replaced = copy.replace(member, name='misc/other', mode=0o644)
3531+
self.assertEqual(replaced.name, 'misc/other')
3532+
self.assertEqual(replaced.mode, 0o644)
3533+
self.assertEqual(replaced.size, member.size)
3534+
self.assertEqual(member.name, 'ustar/regtype')
3535+
with self.assertRaises(TypeError):
3536+
copy.replace(member, offset=123456789)
3537+
35273538

35283539
class NoneInfoExtractTests(ReadTest):
35293540
# These mainly check that all kinds of members are extracted successfully
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:class:`tarfile.TarInfo` objects now support :func:`copy.replace`.

0 commit comments

Comments
 (0)