mirror of
https://github.com/python/cpython.git
synced 2025-08-30 13:38:43 +00:00
Issue #18151: Replace remaining Idle 'open...close' pairs with 'with open'.
This commit is contained in:
parent
c86d7e989c
commit
95f34ab959
3 changed files with 10 additions and 17 deletions
|
@ -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:
|
||||||
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:
|
||||||
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:
|
||||||
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')
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue