@@ -552,29 +552,31 @@ def _get_checksums(self, path):
552
552
553
553
class ReproducibleTGZFile (tarfile .TarFile ):
554
554
def __init__ (
555
- self , name = None , mode = None , compresslevel = - 1 , fileobj = None , mtime = None , ** kwargs
555
+ self , name = None , mode = None , compresslevel = 9 , fileobj = None , mtime = None , ** kwargs
556
556
):
557
- if fileobj is None :
558
- fileobj = open (name , mode + "b" )
557
+ self ._fileobj = fileobj or open (name , mode + "b" )
559
558
560
559
try :
561
- # output filename intentionally empty exclude it from gzip header
562
- gzipfileobj = gzip .GzipFile ("" , mode , compresslevel , fileobj , mtime )
563
- except Exception :
564
- fileobj .close ()
565
- raise
560
+ # filename intentionally empty to exclude it from gzip header
561
+ self ._gzipfileobj = gzip .GzipFile ("" , mode , compresslevel , self ._fileobj , mtime )
566
562
567
- # Allow GzipFile to close fileobj as needed
568
- gzipfileobj . myfileobj = fileobj
569
-
570
- try :
571
- super ( ReproducibleTGZFile , self ). __init__ ( mode = mode , fileobj = gzipfileobj , ** kwargs )
563
+ try :
564
+ super ( ReproducibleTGZFile , self ). __init__ ( mode = mode , fileobj = self . _gzipfileobj , ** kwargs )
565
+ except Exception :
566
+ self . _gzipfileobj . close ()
567
+ raise
572
568
except Exception :
573
- gzipfileobj .close ()
569
+ self . _fileobj .close ()
574
570
raise
575
571
576
- # Allow TarFile to close GzipFile as needed
577
- self ._extfileobj = False
572
+ def close (self ) -> None :
573
+ try :
574
+ super (ReproducibleTGZFile , self ).close ()
575
+ finally :
576
+ try :
577
+ self ._gzipfileobj .close ()
578
+ finally :
579
+ self ._fileobj .close ()
578
580
579
581
580
582
class TarArchive (Archive ):
0 commit comments