# HG changeset patch # User Terry Jan Reedy # Date 1375645196 14400 # Branch 2.7 # Node ID 7f6661a90d02120e4207f0e789e049a5c1ed61aa # Parent b3efc140d8a6a5bc10e39de20fa772e3b82c134f Issue #18151: Replace remaining Idle 'open...close' pairs with 'with open'. diff -r b3efc140d8a6 -r 7f6661a90d02 Lib/idlelib/EditorWindow.py --- a/Lib/idlelib/EditorWindow.py Sun Aug 04 06:09:49 2013 -0700 +++ b/Lib/idlelib/EditorWindow.py Sun Aug 04 15:39:56 2013 -0400 @@ -894,11 +894,8 @@ "Load and update the recent files list and menus" rf_list = [] if os.path.exists(self.recent_files_path): - rf_list_file = open(self.recent_files_path,'r') - try: + with open(self.recent_files_path, 'r') as rf_list_file: rf_list = rf_list_file.readlines() - finally: - rf_list_file.close() if new_file: new_file = os.path.abspath(new_file) + '\n' if new_file in rf_list: diff -r b3efc140d8a6 -r 7f6661a90d02 Lib/idlelib/IOBinding.py --- a/Lib/idlelib/IOBinding.py Sun Aug 04 06:09:49 2013 -0700 +++ b/Lib/idlelib/IOBinding.py Sun Aug 04 15:39:56 2013 -0400 @@ -248,9 +248,8 @@ try: # open the file in binary mode so that we can handle # end-of-line convention ourselves. - f = open(filename,'rb') - chars = f.read() - f.close() + with open(filename, 'rb') as f: + chars = f.read() except IOError as msg: tkMessageBox.showerror("I/O Error", str(msg), master=self.text) return False @@ -383,10 +382,8 @@ if self.eol_convention != "\n": chars = chars.replace("\n", self.eol_convention) try: - f = open(filename, "wb") - f.write(chars) - f.flush() - f.close() + with open(filename, "wb") as f: + f.write(chars) return True except IOError as msg: tkMessageBox.showerror("I/O Error", str(msg), diff -r b3efc140d8a6 -r 7f6661a90d02 Lib/idlelib/ScriptBinding.py --- a/Lib/idlelib/ScriptBinding.py Sun Aug 04 06:09:49 2013 -0700 +++ b/Lib/idlelib/ScriptBinding.py Sun Aug 04 15:39:56 2013 -0400 @@ -87,9 +87,8 @@ self.shell = shell = self.flist.open_shell() saved_stream = shell.get_warning_stream() shell.set_warning_stream(shell.stderr) - f = open(filename, 'r') - source = f.read() - f.close() + with open(filename, 'r') as f: + source = f.read() if '\r' in source: source = re.sub(r"\r\n", "\n", source) source = re.sub(r"\r", "\n", source)