@@ -36,13 +36,6 @@ Some facts and figures:
36
36
.. versionchanged :: 3.3
37
37
Added support for :mod: `lzma ` compression.
38
38
39
- .. versionchanged :: 3.12
40
- Archives are extracted using a :ref: `filter <tarfile-extraction-filter >`,
41
- which makes it possible to either limit surprising/dangerous features,
42
- or to acknowledge that they are expected and the archive is fully trusted.
43
- By default, archives are fully trusted, but this default is deprecated
44
- and slated to change in Python 3.14.
45
-
46
39
47
40
.. function :: open(name=None, mode='r', fileobj=None, bufsize=10240, \*\*kwargs)
48
41
@@ -425,8 +418,8 @@ be finalized; only the internally used file object will be closed. See the
425
418
are used to set the owner/group for the extracted files. Otherwise, the named
426
419
values from the tarfile are used.
427
420
428
- The *filter * argument specifies how `` members `` are modified or rejected
429
- before extraction.
421
+ The *filter * argument, which was added in Python 3.7.17, specifies how
422
+ `` members `` are modified or rejected before extraction.
430
423
See :ref: `tarfile-extraction-filter ` for details.
431
424
It is recommended to set this explicitly depending on which *tar * features
432
425
you need to support.
@@ -447,7 +440,7 @@ be finalized; only the internally used file object will be closed. See the
447
440
.. versionchanged :: 3.6
448
441
The *path * parameter accepts a :term: `path-like object `.
449
442
450
- .. versionchanged :: 3.12
443
+ .. versionchanged :: 3.7.17
451
444
Added the *filter * parameter.
452
445
453
446
@@ -483,7 +476,7 @@ be finalized; only the internally used file object will be closed. See the
483
476
.. versionchanged :: 3.6
484
477
The *path * parameter accepts a :term: `path-like object `.
485
478
486
- .. versionchanged :: 3.12
479
+ .. versionchanged :: 3.7.17
487
480
Added the *filter * parameter.
488
481
489
482
@@ -498,7 +491,6 @@ be finalized; only the internally used file object will be closed. See the
498
491
Return an :class: `io.BufferedReader ` object.
499
492
500
493
.. attribute :: TarFile.errorlevel
501
- :type: int
502
494
503
495
If *errorlevel * is ``0 ``, errors are ignored when using :meth: `TarFile.extract `
504
496
and :meth: `TarFile.extractall `.
@@ -520,7 +512,7 @@ be finalized; only the internally used file object will be closed. See the
520
512
521
513
.. attribute :: TarFile.extraction_filter
522
514
523
- .. versionadded :: 3.12
515
+ .. versionadded :: 3.7.17
524
516
525
517
The :ref: `extraction filter <tarfile-extraction-filter >` used
526
518
as a default for the *filter * argument of :meth: `~TarFile.extract `
@@ -531,10 +523,12 @@ be finalized; only the internally used file object will be closed. See the
531
523
argument to :meth: `~TarFile.extract `.
532
524
533
525
If ``extraction_filter `` is ``None `` (the default),
534
- calling an extraction method without a *filter * argument will raise a
535
- ``DeprecationWarning ``,
536
- and fall back to the :func: `fully_trusted <fully_trusted_filter> ` filter,
537
- whose dangerous behavior matches previous versions of Python.
526
+ calling an extraction method without a *filter * argument will
527
+ use the :func: `fully_trusted <fully_trusted_filter> ` filter for
528
+ compatibility with previous Python versions.
529
+
530
+ In Python 3.12+, leaving ``extraction_filter=None `` will emit a
531
+ ``DeprecationWarning ``.
538
532
539
533
In Python 3.14+, leaving ``extraction_filter=None `` will cause
540
534
extraction methods to use the :func: `data <data_filter> ` filter by default.
@@ -639,6 +633,11 @@ Different :class:`TarInfo` methods handle ``None`` differently:
639
633
- :meth: `~TarFile.addfile ` will fail.
640
634
- :meth: `~TarFile.list ` will print a placeholder string.
641
635
636
+
637
+ .. versionchanged :: 3.7.17
638
+ Added :meth: `~TarInfo.replace ` and handling of ``None ``.
639
+
640
+
642
641
.. class :: TarInfo(name="")
643
642
644
643
Create a :class: `TarInfo ` object.
@@ -670,35 +669,31 @@ A ``TarInfo`` object has the following public data attributes:
670
669
671
670
672
671
.. attribute :: TarInfo.name
673
- :type: str
674
672
675
673
Name of the archive member.
676
674
677
675
678
676
.. attribute :: TarInfo.size
679
- :type: int
680
677
681
678
Size in bytes.
682
679
683
680
684
681
.. attribute :: TarInfo.mtime
685
- :type: int | float
686
682
687
683
Time of last modification in seconds since the :ref: `epoch <epoch >`,
688
684
as in :attr: `os.stat_result.st_mtime `.
689
685
690
- .. versionchanged :: 3.12
686
+ .. versionchanged :: 3.7.17
691
687
692
688
Can be set to ``None `` for :meth: `~TarFile.extract ` and
693
689
:meth: `~TarFile.extractall `, causing extraction to skip applying this
694
690
attribute.
695
691
696
692
.. attribute :: TarInfo.mode
697
- :type: int
698
693
699
694
Permission bits, as for :func: `os.chmod `.
700
695
701
- .. versionchanged :: 3.12
696
+ .. versionchanged :: 3.7.17
702
697
703
698
Can be set to ``None `` for :meth: `~TarFile.extract ` and
704
699
:meth: `~TarFile.extractall `, causing extraction to skip applying this
@@ -714,66 +709,60 @@ A ``TarInfo`` object has the following public data attributes:
714
709
715
710
716
711
.. attribute :: TarInfo.linkname
717
- :type: str
718
712
719
713
Name of the target file name, which is only present in :class: `TarInfo ` objects
720
714
of type :const: `LNKTYPE ` and :const: `SYMTYPE `.
721
715
722
716
723
717
.. attribute :: TarInfo.uid
724
- :type: int
725
718
726
719
User ID of the user who originally stored this member.
727
720
728
- .. versionchanged :: 3.12
721
+ .. versionchanged :: 3.7.17
729
722
730
723
Can be set to ``None `` for :meth: `~TarFile.extract ` and
731
724
:meth: `~TarFile.extractall `, causing extraction to skip applying this
732
725
attribute.
733
726
734
727
.. attribute :: TarInfo.gid
735
- :type: int
736
728
737
729
Group ID of the user who originally stored this member.
738
730
739
- .. versionchanged :: 3.12
731
+ .. versionchanged :: 3.7.17
740
732
741
733
Can be set to ``None `` for :meth: `~TarFile.extract ` and
742
734
:meth: `~TarFile.extractall `, causing extraction to skip applying this
743
735
attribute.
744
736
745
737
.. attribute :: TarInfo.uname
746
- :type: str
747
738
748
739
User name.
749
740
750
- .. versionchanged :: 3.12
741
+ .. versionchanged :: 3.7.17
751
742
752
743
Can be set to ``None `` for :meth: `~TarFile.extract ` and
753
744
:meth: `~TarFile.extractall `, causing extraction to skip applying this
754
745
attribute.
755
746
756
747
.. attribute :: TarInfo.gname
757
- :type: str
758
748
759
749
Group name.
760
750
761
- .. versionchanged :: 3.12
751
+ .. versionchanged :: 3.7.17
762
752
763
753
Can be set to ``None `` for :meth: `~TarFile.extract ` and
764
754
:meth: `~TarFile.extractall `, causing extraction to skip applying this
765
755
attribute.
766
756
767
757
.. attribute :: TarInfo.pax_headers
768
- :type: dict
769
758
770
759
A dictionary containing key-value pairs of an associated pax extended header.
771
760
772
761
.. method :: TarInfo.replace(name=..., mtime=..., mode=..., linkname=...,
773
762
uid=..., gid=..., uname=..., gname=...,
774
763
deep=True)
775
764
776
- .. versionadded :: 3.12
765
+ .. versionadded :: 3.7.17
777
766
778
767
Return a *new * copy of the :class: `!TarInfo ` object with the given attributes
779
768
changed. For example, to return a ``TarInfo `` with the group name set to
@@ -838,7 +827,7 @@ A :class:`TarInfo` object also provides some convenient query methods:
838
827
Extraction filters
839
828
------------------
840
829
841
- .. versionadded :: 3.12
830
+ .. versionadded :: 3.7.17
842
831
843
832
The *tar * format is designed to capture all details of a UNIX-like filesystem,
844
833
which makes it very powerful.
@@ -875,9 +864,10 @@ can be:
875
864
876
865
* ``None `` (default): Use :attr: `TarFile.extraction_filter `.
877
866
878
- If that is also ``None `` (the default), raise a ``DeprecationWarning ``,
879
- and fall back to the ``'fully_trusted' `` filter, whose dangerous behavior
880
- matches previous versions of Python.
867
+ If that is also ``None `` (the default), the ``'fully_trusted' ``
868
+ filter will be used (for compatibility with earlier versions of Python).
869
+
870
+ In Python 3.12, the default will emit a ``DeprecationWarning ``.
881
871
882
872
In Python 3.14, the ``'data' `` filter will become the default instead.
883
873
It's possible to switch earlier; see :attr: `TarFile.extraction_filter `.
@@ -1014,7 +1004,7 @@ Also note that:
1014
1004
Supporting older Python versions
1015
1005
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1016
1006
1017
- Extraction filters were added to Python 3.12, but may be backported to older
1007
+ Extraction filters were added to Python 3.12, and are backported to older
1018
1008
versions as security updates.
1019
1009
To check whether the feature is available, use e.g.
1020
1010
``hasattr(tarfile, 'data_filter') `` rather than checking the Python version.
@@ -1161,6 +1151,8 @@ Command-line options
1161
1151
Only string names are accepted (that is, ``fully_trusted ``, ``tar ``,
1162
1152
and ``data ``).
1163
1153
1154
+ .. versionadded :: 3.7.17
1155
+
1164
1156
.. _tar-examples :
1165
1157
1166
1158
Examples
@@ -1170,7 +1162,7 @@ How to extract an entire tar archive to the current working directory::
1170
1162
1171
1163
import tarfile
1172
1164
tar = tarfile.open("sample.tar.gz")
1173
- tar.extractall(filter='data' )
1165
+ tar.extractall()
1174
1166
tar.close()
1175
1167
1176
1168
How to extract a subset of a tar archive with :meth: `TarFile.extractall ` using
0 commit comments