mirror of
https://github.com/wxWidgets/Phoenix.git
synced 2025-12-16 09:40:07 +01:00
Merge pull request #1200 from pkienzle/master
python 2/3 compatible file handling in wx.py
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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.",
|
||||
|
||||
@@ -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!!
|
||||
|
||||
Reference in New Issue
Block a user