Skip to content
Permalink
Browse files

Merge pull request #1200 from pkienzle/master

 python 2/3 compatible file handling in wx.py

(cherry picked from commit 1c51bc0)
  • Loading branch information...
RobinD42 committed May 14, 2019
1 parent b2d5aeb commit 0ef7667e18f1b6eeb00c52759a55be13d5b7ed12
Showing with 17 additions and 8 deletions.
  1. +6 −1 wx/py/document.py
  2. +7 −3 wx/py/frame.py
  3. +4 −4 wx/py/sliceshell.py
@@ -25,7 +25,7 @@ def read(self):
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 @@ def write(self, text):
"""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,16 +920,20 @@ def bufferSave(self):

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 = ''

dlg = EditStartupScriptDialog(self, self.startupScript, text)
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 @@ def LoadPySlicesFile(self,fid):

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 @@ def fid_write(s):
fid_write(outputStartText)
addComment=True
if addComment:
fid.write('#')
fid_write(u'#')

fid_write(self.GetLine(i))

# FIX ME!!

0 comments on commit 0ef7667

Please sign in to comment.
You can’t perform that action at this time.