From 8bc558db4d22a086182544b01b5a07c10560932d Mon Sep 17 00:00:00 2001 From: Joseph Lee Date: Tue, 4 Jun 2019 17:45:09 -0700 Subject: [PATCH] Log viewer/Python 3: save log file in UTF-8 format. Re #9038. Reviewed by Mick Curran (NV Access): open log file for saving with UTF-8 encoding from the start. Note that Python 2's open function does not include encoding parameter directly, but Python 3 does. --- source/gui/logViewer.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/source/gui/logViewer.py b/source/gui/logViewer.py index 44dbfef028f..2f334396b2c 100755 --- a/source/gui/logViewer.py +++ b/source/gui/logViewer.py @@ -80,8 +80,9 @@ def onSaveAsCommand(self, evt): try: # codecs.open() forces binary mode, which is bad under Windows because line endings won't be converted to crlf automatically. # Therefore, do the encoding manually. - # #9038 (Py3 review required): although eligible for "with open" function conversion, encoding issue must be settled first. - file(filename, "w").write(self.outputCtrl.GetValue().encode("UTF-8")) + # #9038: work with UTF-8 from the start. + with open(filename, "w", encoding="UTF-8") as f: + f.write(self.outputCtrl.GetValue()) except (IOError, OSError), e: # Translators: Dialog text presented when NVDA cannot save a log file. gui.messageBox(_("Error saving log: %s") % e.strerror, _("Error"), style=wx.OK | wx.ICON_ERROR, parent=self)