Kaydet (Commit) a63a312a authored tarafından Raymond Hettinger's avatar Raymond Hettinger

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.)
üst e3b8f7c0
...@@ -370,19 +370,20 @@ be finalized; only the internally used file object will be closed. See the ...@@ -370,19 +370,20 @@ be finalized; only the internally used file object will be closed. See the
and :meth:`close`, and also supports iteration over its lines. and :meth:`close`, and also supports iteration over its lines.
.. method:: TarFile.add(name, arcname=None, recursive=True, exclude=None, filter=None) .. method:: TarFile.add(name, arcname=None, recursive=True, exclude=None, *, filter=None)
Add the file *name* to the archive. *name* may be any type of file (directory, Add the file *name* to the archive. *name* may be any type of file
fifo, symbolic link, etc.). If given, *arcname* specifies an alternative name (directory, fifo, symbolic link, etc.). If given, *arcname* specifies an
for the file in the archive. Directories are added recursively by default. This alternative name for the file in the archive. Directories are added
can be avoided by setting *recursive* to :const:`False`. If *exclude* is given, recursively by default. This can be avoided by setting *recursive* to
it must be a function that takes one filename argument and returns a boolean :const:`False`. If *exclude* is given, it must be a function that takes one
value. Depending on this value the respective file is either excluded filename argument and returns a boolean value. Depending on this value the
(:const:`True`) or added (:const:`False`). If *filter* is specified it must respective file is either excluded (:const:`True`) or added
be a function that takes a :class:`TarInfo` object argument and returns the (:const:`False`). If *filter* is specified it must be a keyword argument. It
changed :class:`TarInfo` object. If it instead returns :const:`None` the :class:`TarInfo` should be a function that takes a :class:`TarInfo` object argument and
object will be excluded from the archive. See :ref:`tar-examples` for an returns the changed :class:`TarInfo` object. If it instead returns
example. :const:`None` the :class:`TarInfo` object will be excluded from the
archive. See :ref:`tar-examples` for an example.
.. versionchanged:: 3.2 .. versionchanged:: 3.2
Added the *filter* parameter. Added the *filter* parameter.
......
...@@ -2025,7 +2025,7 @@ class TarFile(object): ...@@ -2025,7 +2025,7 @@ class TarFile(object):
print("link to", tarinfo.linkname, end=' ') print("link to", tarinfo.linkname, end=' ')
print() print()
def add(self, name, arcname=None, recursive=True, exclude=None, filter=None): def add(self, name, arcname=None, recursive=True, exclude=None, *, filter=None):
"""Add the file `name' to the archive. `name' may be any type of file """Add the file `name' to the archive. `name' may be any type of file
(directory, fifo, symbolic link, etc.). If given, `arcname' (directory, fifo, symbolic link, etc.). If given, `arcname'
specifies an alternative name for the file in the archive. specifies an alternative name for the file in the archive.
...@@ -2082,7 +2082,7 @@ class TarFile(object): ...@@ -2082,7 +2082,7 @@ class TarFile(object):
if recursive: if recursive:
for f in os.listdir(name): for f in os.listdir(name):
self.add(os.path.join(name, f), os.path.join(arcname, f), self.add(os.path.join(name, f), os.path.join(arcname, f),
recursive, exclude, filter) recursive, exclude, filter=filter)
else: else:
self.addfile(tarinfo) self.addfile(tarinfo)
......
...@@ -919,6 +919,10 @@ class WriteTest(WriteTestBase): ...@@ -919,6 +919,10 @@ class WriteTest(WriteTestBase):
finally: finally:
tar.close() tar.close()
# Verify that filter is a keyword-only argument
with self.assertRaises(TypeError):
tar.add(tempdir, "empty_dir", True, None, filter)
tar = tarfile.open(tmpname, "r") tar = tarfile.open(tmpname, "r")
try: try:
for tarinfo in tar: for tarinfo in tar:
......
...@@ -16,6 +16,10 @@ Core and Builtins ...@@ -16,6 +16,10 @@ Core and Builtins
Library Library
------- -------
- 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.
- Issue #11004: Repaired edge case in deque.count(). - Issue #11004: Repaired edge case in deque.count().
- Issue #10974: IDLE no longer crashes if its recent files list includes files - Issue #10974: IDLE no longer crashes if its recent files list includes files
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment