Commit 8f68844bc2634b0adcbd169a9406c55dc048f669
1 parent
a58b7439
olevba[3]: ensure file handles are closed
Trying to get rid of all those ResourceWarnings that python3 shows during unittests
Showing
2 changed files
with
12 additions
and
6 deletions
oletools/olevba.py
| ... | ... | @@ -2336,7 +2336,8 @@ class VBA_Parser(object): |
| 2336 | 2336 | # read file from disk, check if it is a Word 2003 XML file (WordProcessingML), Excel 2003 XML, |
| 2337 | 2337 | # or a plain text file containing VBA code |
| 2338 | 2338 | if data is None: |
| 2339 | - data = open(filename, 'rb').read() | |
| 2339 | + with open(filename, 'rb') as file_handle: | |
| 2340 | + data = file_handle.read() | |
| 2340 | 2341 | # check if it is a Word 2003 XML file (WordProcessingML): must contain the namespace |
| 2341 | 2342 | if b'http://schemas.microsoft.com/office/word/2003/wordml' in data: |
| 2342 | 2343 | self.open_word2003xml(data) |
| ... | ... | @@ -2398,10 +2399,12 @@ class VBA_Parser(object): |
| 2398 | 2399 | #TODO: if the zip file is encrypted, suggest to use the -z option, or try '-z infected' automatically |
| 2399 | 2400 | # check each file within the zip if it is an OLE file, by reading its magic: |
| 2400 | 2401 | for subfile in z.namelist(): |
| 2401 | - magic = z.open(subfile).read(len(olefile.MAGIC)) | |
| 2402 | + with z.open(subfile) as file_handle: | |
| 2403 | + magic = file_handle.read(len(olefile.MAGIC)) | |
| 2402 | 2404 | if magic == olefile.MAGIC: |
| 2403 | 2405 | log.debug('Opening OLE file %s within zip' % subfile) |
| 2404 | - ole_data = z.open(subfile).read() | |
| 2406 | + with z.open(subfile) as file_handle: | |
| 2407 | + ole_data = file_handle.read() | |
| 2405 | 2408 | try: |
| 2406 | 2409 | self.ole_subfiles.append( |
| 2407 | 2410 | VBA_Parser(filename=subfile, data=ole_data, | ... | ... |
oletools/olevba3.py
| ... | ... | @@ -2289,7 +2289,8 @@ class VBA_Parser(object): |
| 2289 | 2289 | # read file from disk, check if it is a Word 2003 XML file (WordProcessingML), Excel 2003 XML, |
| 2290 | 2290 | # or a plain text file containing VBA code |
| 2291 | 2291 | if data is None: |
| 2292 | - data = open(filename, 'rb').read() | |
| 2292 | + with open(filename, 'rb') as file_handle: | |
| 2293 | + data = file_handle.read() | |
| 2293 | 2294 | # check if it is a Word 2003 XML file (WordProcessingML): must contain the namespace |
| 2294 | 2295 | if b'http://schemas.microsoft.com/office/word/2003/wordml' in data: |
| 2295 | 2296 | self.open_word2003xml(data) |
| ... | ... | @@ -2351,10 +2352,12 @@ class VBA_Parser(object): |
| 2351 | 2352 | #TODO: if the zip file is encrypted, suggest to use the -z option, or try '-z infected' automatically |
| 2352 | 2353 | # check each file within the zip if it is an OLE file, by reading its magic: |
| 2353 | 2354 | for subfile in z.namelist(): |
| 2354 | - magic = z.open(subfile).read(len(olefile.MAGIC)) | |
| 2355 | + with z.open(subfile) as file_handle: | |
| 2356 | + magic = file_handle.read(len(olefile.MAGIC)) | |
| 2355 | 2357 | if magic == olefile.MAGIC: |
| 2356 | 2358 | log.debug('Opening OLE file %s within zip' % subfile) |
| 2357 | - ole_data = z.open(subfile).read() | |
| 2359 | + with z.open(subfile) as file_handle: | |
| 2360 | + ole_data = file_handle.read() | |
| 2358 | 2361 | try: |
| 2359 | 2362 | self.ole_subfiles.append( |
| 2360 | 2363 | VBA_Parser(filename=subfile, data=ole_data, | ... | ... |