Skip to content

Commit 0dfa7ce

Browse files
authored
gh-115256: Remove refcycles from tarfile writing (GH-115257)
1 parent cfbdce7 commit 0dfa7ce

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

Doc/whatsnew/3.13.rst

+3
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,9 @@ Deprecated
791791
coroutine.
792792
(Contributed by Irit Katriel in :gh:`81137`.)
793793

794+
* The undocumented and unused ``tarfile`` attribute of :class:`tarfile.TarFile`
795+
is deprecated and scheduled for removal in Python 3.16.
796+
794797

795798
Pending Removal in Python 3.14
796799
------------------------------

Lib/tarfile.py

+20-2
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,7 @@ class TarInfo(object):
872872
pax_headers = ('A dictionary containing key-value pairs of an '
873873
'associated pax extended header.'),
874874
sparse = 'Sparse member information.',
875-
tarfile = None,
875+
_tarfile = None,
876876
_sparse_structs = None,
877877
_link_target = None,
878878
)
@@ -901,6 +901,24 @@ def __init__(self, name=""):
901901
self.sparse = None # sparse member information
902902
self.pax_headers = {} # pax header information
903903

904+
@property
905+
def tarfile(self):
906+
import warnings
907+
warnings.warn(
908+
'The undocumented "tarfile" attribute of TarInfo objects '
909+
+ 'is deprecated and will be removed in Python 3.16',
910+
DeprecationWarning, stacklevel=2)
911+
return self._tarfile
912+
913+
@tarfile.setter
914+
def tarfile(self, tarfile):
915+
import warnings
916+
warnings.warn(
917+
'The undocumented "tarfile" attribute of TarInfo objects '
918+
+ 'is deprecated and will be removed in Python 3.16',
919+
DeprecationWarning, stacklevel=2)
920+
self._tarfile = tarfile
921+
904922
@property
905923
def path(self):
906924
'In pax headers, "name" is called "path".'
@@ -2030,7 +2048,7 @@ def gettarinfo(self, name=None, arcname=None, fileobj=None):
20302048
# Now, fill the TarInfo object with
20312049
# information specific for the file.
20322050
tarinfo = self.tarinfo()
2033-
tarinfo.tarfile = self # Not needed
2051+
tarinfo._tarfile = self # To be removed in 3.16.
20342052

20352053
# Use os.stat or os.lstat, depending on if symlinks shall be resolved.
20362054
if fileobj is None:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Added DeprecationWarning when accessing the tarfile attribute of TarInfo
2+
objects. The attribute is never used internally and is only attached to
3+
TarInfos when the tarfile is opened in write-mode, not read-mode. The
4+
attribute creates an unnecessary reference cycle which may cause
5+
corruption when not closing the handle after writing a tarfile.

0 commit comments

Comments
 (0)