@@ -1924,9 +1924,8 @@ def add(self, name, arcname=None, recursive=True, exclude=None, *, filter=None):
19241924
19251925 # Append the tar header and data to the archive.
19261926 if tarinfo .isreg ():
1927- f = bltn_open (name , "rb" )
1928- self .addfile (tarinfo , f )
1929- f .close ()
1927+ with bltn_open (name , "rb" ) as f :
1928+ self .addfile (tarinfo , f )
19301929
19311930 elif tarinfo .isdir ():
19321931 self .addfile (tarinfo )
@@ -2131,16 +2130,15 @@ def makefile(self, tarinfo, targetpath):
21312130 """
21322131 source = self .fileobj
21332132 source .seek (tarinfo .offset_data )
2134- target = bltn_open (targetpath , "wb" )
2135- if tarinfo .sparse is not None :
2136- for offset , size in tarinfo .sparse :
2137- target .seek (offset )
2138- copyfileobj (source , target , size )
2139- else :
2140- copyfileobj (source , target , tarinfo .size )
2141- target .seek (tarinfo .size )
2142- target .truncate ()
2143- target .close ()
2133+ with bltn_open (targetpath , "wb" ) as target :
2134+ if tarinfo .sparse is not None :
2135+ for offset , size in tarinfo .sparse :
2136+ target .seek (offset )
2137+ copyfileobj (source , target , size )
2138+ else :
2139+ copyfileobj (source , target , tarinfo .size )
2140+ target .seek (tarinfo .size )
2141+ target .truncate ()
21442142
21452143 def makeunknown (self , tarinfo , targetpath ):
21462144 """Make a file from a TarInfo object with an unknown type
0 commit comments