Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Doc/library/tarfile.rst
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,11 @@ A ``TarInfo`` object has the following public data attributes:
If *deep* is false, the copy is shallow, i.e. ``pax_headers``
and any custom attributes are shared with the original ``TarInfo`` object.

This method is also used by :func:`copy.replace`.

.. versionchanged:: next
Added support for :func:`copy.replace`.

A :class:`TarInfo` object also provides some convenient query methods:


Expand Down
2 changes: 2 additions & 0 deletions Lib/tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,8 @@ def replace(self, *,
result.gname = gname
return result

__replace__ = replace

def get_info(self):
"""Return the TarInfo's attributes as a dictionary.
"""
Expand Down
11 changes: 11 additions & 0 deletions Lib/test/test_tarfile.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import copy
import errno
import sys
import os
Expand Down Expand Up @@ -3524,6 +3525,16 @@ def test_replace_internal(self):
with self.assertRaises(TypeError):
member.replace(offset=123456789)

def test_copy_replace(self):
member = self.tar.getmember('ustar/regtype')
replaced = copy.replace(member, name='misc/other', mode=0o644)
self.assertEqual(replaced.name, 'misc/other')
self.assertEqual(replaced.mode, 0o644)
self.assertEqual(replaced.size, member.size)
self.assertEqual(member.name, 'ustar/regtype')
with self.assertRaises(TypeError):
copy.replace(member, offset=123456789)


class NoneInfoExtractTests(ReadTest):
# These mainly check that all kinds of members are extracted successfully
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:class:`tarfile.TarInfo` objects now support :func:`copy.replace`.
Loading