Commits

Jason R. Coombs  committed 3b5eb3a

Added context managers around calls for robustness

  • Participants
  • Parent commits 4005e3a

Comments (0)

Files changed (1)

File jaraco/office/convert.py

 @contextmanager
 def word_context(word, filename, close_flags):
 	doc = word.Documents.Open(filename)
-	yield doc
-	doc.Close(close_flags)
+	try:
+		yield doc
+	finally:
+		doc.Close(close_flags)
 
 class Converter(object):
 	"""
 			pdffile = replace_extension('.pdf', docfile)
 			dont_save = getattr(constants, 'wdDoNotSaveChanges', 0)
 			with word_context(self.word, docfile, dont_save) as doc:
+				raise Exception
 				res = doc.SaveAs(pdffile, target_format)
-			content = open(pdffile, 'rb').read()
+			with open(pdffile, 'rb') as pdf:
+				content = pdf.read()
 			os.remove(pdffile)
 		return content