2121from io import BytesIO , TextIOWrapper
2222import csv
2323from sys import version_info
24+ from contextlib import closing
2425
2526# Python 3 introduced .readable() to tarfile extracted files objects - this
2627# is required to wrap a TextIOWrapper around the object. However, wrapping
@@ -76,7 +77,7 @@ def _parse(tarOutput):
7677
7778 metadataMember = tarFile .getmember ("__METADATA__" )
7879 if not metadataMember .issym () and metadataMember .isfile ():
79- with _text_wrapper (tarFile .extractfile (metadataMember )) as metadataFile :
80+ with closing ( _text_wrapper (tarFile .extractfile (metadataMember ) )) as metadataFile :
8081 metadataReader = csv .reader (_truncate_nulls (metadataFile ))
8182 for metadataLine in metadataReader :
8283 # each metadata line comes as a key-value pair, with list values
@@ -97,18 +98,18 @@ def _parse(tarOutput):
9798 contentMember = tarFile .getmember ("__TEXT__" )
9899 if not contentMember .issym () and contentMember .isfile ():
99100 if version_info .major >= 3 :
100- with _text_wrapper (tarFile .extractfile (contentMember ), encoding = 'utf8' ) as content_file :
101+ with closing ( _text_wrapper (tarFile .extractfile (contentMember ), encoding = 'utf8' ) ) as content_file :
101102 content = content_file .read ()
102103 else :
103- with tarFile .extractfile (contentMember ) as content_file :
104+ with closing ( tarFile .extractfile (contentMember ) ) as content_file :
104105 content = content_file .read ().decode ('utf8' )
105106
106107 # get the remaining files as attachments
107108 attachments = {}
108109 for attachment in memberNames :
109110 attachmentMember = tarFile .getmember (attachment )
110111 if not attachmentMember .issym () and attachmentMember .isfile ():
111- with tarFile .extractfile (attachmentMember ) as attachment_file :
112+ with closing ( tarFile .extractfile (attachmentMember ) ) as attachment_file :
112113 attachments [attachment ] = attachment_file .read ()
113114
114115 parsed ["content" ] = content
0 commit comments