diff --git a/wx/py/document.py b/wx/py/document.py index a25a1143..c706e08d 100644 --- a/wx/py/document.py +++ b/wx/py/document.py @@ -25,7 +25,7 @@ class Document: if self.filepath and os.path.exists(self.filepath): f = open(self.filepath, 'rb') try: - return f.read() + return f.read().decode('utf-8') finally: f.close() else: @@ -35,6 +35,11 @@ class Document: """Write text to file.""" try: f = open(self.filepath, 'wb') + try: + # Convert from unicode to bytes + text = text.encode('utf-8') + except AttributeError: + pass f.write(text) finally: if f: diff --git a/wx/py/frame.py b/wx/py/frame.py index 539110e4..6acbd770 100644 --- a/wx/py/frame.py +++ b/wx/py/frame.py @@ -920,7 +920,11 @@ class ShellFrameMixin: def EditStartupScript(self): if os.path.exists(self.startupScript): - text = open(self.startupScript, 'U').read() + import io + # Use newline=None to translate \n \r \r\n to \n on read. The + # old-style mode='U' is deprecated. + text = io.open(self.startupScript, 'r', + newline=None, encoding='utf-8').read() else: text = '' @@ -928,8 +932,8 @@ class ShellFrameMixin: if dlg.ShowModal() == wx.ID_OK: text = dlg.GetText() try: - f = open(self.startupScript, 'w') - f.write(text) + f = open(self.startupScript, 'wb') + f.write(text.encode('utf-8')) f.close() except: d = wx.MessageDialog(self, "Error saving startup file.", diff --git a/wx/py/sliceshell.py b/wx/py/sliceshell.py index 4aa9b7cf..28f4c61c 100755 --- a/wx/py/sliceshell.py +++ b/wx/py/sliceshell.py @@ -3715,12 +3715,12 @@ class SlicesShell(editwindow.EditWindow): def SavePySlicesFile(self,fid): addComment=False - + def fid_write(s): fid.write(s.replace('\r\n', '\n') .replace('\n', os.linesep) .encode('utf-8')) - + fid_write(usrBinEnvPythonText) fid_write(pyslicesFormatHeaderText[-1]) for i in range(self.GetLineCount()): @@ -3734,8 +3734,8 @@ class SlicesShell(editwindow.EditWindow): fid_write(outputStartText) addComment=True if addComment: - fid.write('#') - + fid_write(u'#') + fid_write(self.GetLine(i)) # FIX ME!!