Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a63a312

Browse files
committed
Issue #11014: Make 'filter' argument in tarfile.Tarfile.add() into a
keyword-only argument. The preceding positional argument was deprecated, so it made no sense to add filter as a positional argument. (Patch reviewed by Brian Curtin and Anthony Long.)
1 parent e3b8f7c commit a63a312

4 files changed

Lines changed: 24 additions & 15 deletions

File tree

Doc/library/tarfile.rst

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -370,19 +370,20 @@ be finalized; only the internally used file object will be closed. See the
370370
and :meth:`close`, and also supports iteration over its lines.
371371

372372

373-
.. method:: TarFile.add(name, arcname=None, recursive=True, exclude=None, filter=None)
374-
375-
Add the file *name* to the archive. *name* may be any type of file (directory,
376-
fifo, symbolic link, etc.). If given, *arcname* specifies an alternative name
377-
for the file in the archive. Directories are added recursively by default. This
378-
can be avoided by setting *recursive* to :const:`False`. If *exclude* is given,
379-
it must be a function that takes one filename argument and returns a boolean
380-
value. Depending on this value the respective file is either excluded
381-
(:const:`True`) or added (:const:`False`). If *filter* is specified it must
382-
be a function that takes a :class:`TarInfo` object argument and returns the
383-
changed :class:`TarInfo` object. If it instead returns :const:`None` the :class:`TarInfo`
384-
object will be excluded from the archive. See :ref:`tar-examples` for an
385-
example.
373+
.. method:: TarFile.add(name, arcname=None, recursive=True, exclude=None, *, filter=None)
374+
375+
Add the file *name* to the archive. *name* may be any type of file
376+
(directory, fifo, symbolic link, etc.). If given, *arcname* specifies an
377+
alternative name for the file in the archive. Directories are added
378+
recursively by default. This can be avoided by setting *recursive* to
379+
:const:`False`. If *exclude* is given, it must be a function that takes one
380+
filename argument and returns a boolean value. Depending on this value the
381+
respective file is either excluded (:const:`True`) or added
382+
(:const:`False`). If *filter* is specified it must be a keyword argument. It
383+
should be a function that takes a :class:`TarInfo` object argument and
384+
returns the changed :class:`TarInfo` object. If it instead returns
385+
:const:`None` the :class:`TarInfo` object will be excluded from the
386+
archive. See :ref:`tar-examples` for an example.
386387

387388
.. versionchanged:: 3.2
388389
Added the *filter* parameter.

Lib/tarfile.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2025,7 +2025,7 @@ def list(self, verbose=True):
20252025
print("link to", tarinfo.linkname, end=' ')
20262026
print()
20272027

2028-
def add(self, name, arcname=None, recursive=True, exclude=None, filter=None):
2028+
def add(self, name, arcname=None, recursive=True, exclude=None, *, filter=None):
20292029
"""Add the file `name' to the archive. `name' may be any type of file
20302030
(directory, fifo, symbolic link, etc.). If given, `arcname'
20312031
specifies an alternative name for the file in the archive.
@@ -2082,7 +2082,7 @@ def add(self, name, arcname=None, recursive=True, exclude=None, filter=None):
20822082
if recursive:
20832083
for f in os.listdir(name):
20842084
self.add(os.path.join(name, f), os.path.join(arcname, f),
2085-
recursive, exclude, filter)
2085+
recursive, exclude, filter=filter)
20862086

20872087
else:
20882088
self.addfile(tarinfo)

Lib/test/test_tarfile.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,10 @@ def filter(tarinfo):
919919
finally:
920920
tar.close()
921921

922+
# Verify that filter is a keyword-only argument
923+
with self.assertRaises(TypeError):
924+
tar.add(tempdir, "empty_dir", True, None, filter)
925+
922926
tar = tarfile.open(tmpname, "r")
923927
try:
924928
for tarinfo in tar:

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ Core and Builtins
1616
Library
1717
-------
1818

19+
- Issue #11014: Make 'filter' argument in tarfile.Tarfile.add() into a
20+
keyword-only argument. The preceding positional argument was deprecated,
21+
so it made no sense to add filter as a positional argument.
22+
1923
- Issue #11004: Repaired edge case in deque.count().
2024

2125
- Issue #10974: IDLE no longer crashes if its recent files list includes files

0 commit comments

Comments
 (0)