Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 95f34ab

Browse files
committed
Issue #18151: Replace remaining Idle 'open...close' pairs with 'with open'.
1 parent c86d7e9 commit 95f34ab

3 files changed

Lines changed: 10 additions & 17 deletions

File tree

Lib/idlelib/EditorWindow.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -882,12 +882,9 @@ def update_recent_files_list(self, new_file=None):
882882
"Load and update the recent files list and menus"
883883
rf_list = []
884884
if os.path.exists(self.recent_files_path):
885-
rf_list_file = open(self.recent_files_path,'r',
886-
encoding='utf_8', errors='replace')
887-
try:
885+
with open(self.recent_files_path, 'r',
886+
encoding='utf_8', errors='replace') as rf_list_file:
888887
rf_list = rf_list_file.readlines()
889-
finally:
890-
rf_list_file.close()
891888
if new_file:
892889
new_file = os.path.abspath(new_file) + '\n'
893890
if new_file in rf_list:

Lib/idlelib/IOBinding.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,10 @@ def loadfile(self, filename):
208208
try:
209209
# open the file in binary mode so that we can handle
210210
# end-of-line convention ourselves.
211-
f = open(filename,'rb')
212-
two_lines = f.readline() + f.readline()
213-
f.seek(0)
214-
bytes = f.read()
215-
f.close()
211+
with open(filename, 'rb') as f:
212+
two_lines = f.readline() + f.readline()
213+
f.seek(0)
214+
bytes = f.read()
216215
except OSError as msg:
217216
tkMessageBox.showerror("I/O Error", str(msg), master=self.text)
218217
return False
@@ -373,10 +372,8 @@ def writefile(self, filename):
373372
text = text.replace("\n", self.eol_convention)
374373
chars = self.encode(text)
375374
try:
376-
f = open(filename, "wb")
377-
f.write(chars)
378-
f.flush()
379-
f.close()
375+
with open(filename, "wb") as f:
376+
f.write(chars)
380377
return True
381378
except OSError as msg:
382379
tkMessageBox.showerror("I/O Error", str(msg),

Lib/idlelib/ScriptBinding.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,8 @@ def checksyntax(self, filename):
8787
self.shell = shell = self.flist.open_shell()
8888
saved_stream = shell.get_warning_stream()
8989
shell.set_warning_stream(shell.stderr)
90-
f = open(filename, 'rb')
91-
source = f.read()
92-
f.close()
90+
with open(filename, 'rb') as f:
91+
source = f.read()
9392
if b'\r' in source:
9493
source = source.replace(b'\r\n', b'\n')
9594
source = source.replace(b'\r', b'\n')

0 commit comments

Comments
 (0)