Commit 437c109a37e07d89e268013eed755a031664247e

Authored by Christian Herdtweck
1 parent 944f1f76

msodde: ensure zip file handle of docx file is closed

oletools/msodde.py
... ... @@ -533,17 +533,16 @@ def process_xls(filepath):
533 533 def process_docx(filepath, field_filter_mode=None):
534 534 log.debug('process_docx')
535 535 all_fields = []
536   - z = zipfile.ZipFile(filepath)
537   - for filepath in z.namelist():
538   - if filepath in LOCATIONS:
539   - data = z.read(filepath)
540   - fields = process_xml(data)
541   - if len(fields) > 0:
542   - #print ('DDE Links in %s:'%filepath)
543   - #for f in fields:
544   - # print(f)
545   - all_fields.extend(fields)
546   - z.close()
  536 + with zipfile.ZipFile(filepath) as z:
  537 + for filepath in z.namelist():
  538 + if filepath in LOCATIONS:
  539 + data = z.read(filepath)
  540 + fields = process_xml(data)
  541 + if len(fields) > 0:
  542 + #print ('DDE Links in %s:'%filepath)
  543 + #for f in fields:
  544 + # print(f)
  545 + all_fields.extend(fields)
547 546  
548 547 # apply field command filter
549 548 log.debug('filtering with mode "{0}"'.format(field_filter_mode))
... ...
oletools/ooxml.py
... ... @@ -337,7 +337,8 @@ class XmlParser(object):
337 337  
338 338 yields 3-tuples (filename, content_type, file_handle) where
339 339 content_type is based on filename or default for extension or is None,
340   - and file_handle is a ZipSubFile
  340 + and file_handle is a ZipSubFile. Caller does not have to care about
  341 + closing handle, will be closed even in error condition.
341 342  
342 343 To handle binary parts of an xlsb file, use xls_parser.parse_xlsb_part
343 344 """
... ...