Commit 95f34ab9 authored by Terry Jan Reedy's avatar Terry Jan Reedy

Issue #18151: Replace remaining Idle 'open...close' pairs with 'with open'.

parent c86d7e98
...@@ -882,12 +882,9 @@ class EditorWindow(object): ...@@ -882,12 +882,9 @@ class EditorWindow(object):
"Load and update the recent files list and menus" "Load and update the recent files list and menus"
rf_list = [] rf_list = []
if os.path.exists(self.recent_files_path): if os.path.exists(self.recent_files_path):
rf_list_file = open(self.recent_files_path,'r', with open(self.recent_files_path, 'r',
encoding='utf_8', errors='replace') encoding='utf_8', errors='replace') as rf_list_file:
try:
rf_list = rf_list_file.readlines() rf_list = rf_list_file.readlines()
finally:
rf_list_file.close()
if new_file: if new_file:
new_file = os.path.abspath(new_file) + '\n' new_file = os.path.abspath(new_file) + '\n'
if new_file in rf_list: if new_file in rf_list:
......
...@@ -208,11 +208,10 @@ class IOBinding: ...@@ -208,11 +208,10 @@ class IOBinding:
try: try:
# open the file in binary mode so that we can handle # open the file in binary mode so that we can handle
# end-of-line convention ourselves. # end-of-line convention ourselves.
f = open(filename,'rb') with open(filename, 'rb') as f:
two_lines = f.readline() + f.readline() two_lines = f.readline() + f.readline()
f.seek(0) f.seek(0)
bytes = f.read() bytes = f.read()
f.close()
except OSError as msg: except OSError as msg:
tkMessageBox.showerror("I/O Error", str(msg), master=self.text) tkMessageBox.showerror("I/O Error", str(msg), master=self.text)
return False return False
...@@ -373,10 +372,8 @@ class IOBinding: ...@@ -373,10 +372,8 @@ class IOBinding:
text = text.replace("\n", self.eol_convention) text = text.replace("\n", self.eol_convention)
chars = self.encode(text) chars = self.encode(text)
try: try:
f = open(filename, "wb") with open(filename, "wb") as f:
f.write(chars) f.write(chars)
f.flush()
f.close()
return True return True
except OSError as msg: except OSError as msg:
tkMessageBox.showerror("I/O Error", str(msg), tkMessageBox.showerror("I/O Error", str(msg),
......
...@@ -87,9 +87,8 @@ class ScriptBinding: ...@@ -87,9 +87,8 @@ class ScriptBinding:
self.shell = shell = self.flist.open_shell() self.shell = shell = self.flist.open_shell()
saved_stream = shell.get_warning_stream() saved_stream = shell.get_warning_stream()
shell.set_warning_stream(shell.stderr) shell.set_warning_stream(shell.stderr)
f = open(filename, 'rb') with open(filename, 'rb') as f:
source = f.read() source = f.read()
f.close()
if b'\r' in source: if b'\r' in source:
source = source.replace(b'\r\n', b'\n') source = source.replace(b'\r\n', b'\n')
source = source.replace(b'\r', b'\n') source = source.replace(b'\r', b'\n')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment