Merge pull request #1200 from pkienzle/master

python 2/3 compatible file handling in wx.py
This commit is contained in:
Robin Dunn
2019-05-13 21:28:34 -07:00
committed by GitHub
3 changed files with 17 additions and 8 deletions

View File

@@ -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:

View File

@@ -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.",

View File

@@ -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!!