diff options
author | Terry Jan Reedy <tjreedy@udel.edu> | 2013-08-04 19:39:03 (GMT) |
---|---|---|
committer | Terry Jan Reedy <tjreedy@udel.edu> | 2013-08-04 19:39:03 (GMT) |
commit | 95f34ab95959fa67d258043622744dae8519c5b2 (patch) | |
tree | 3fbbe3a8bf381438abfc71fd5613e883b55dc4cf /Lib | |
parent | c86d7e989c98d57449263201e826a52c15102a64 (diff) | |
download | cpython-95f34ab95959fa67d258043622744dae8519c5b2.zip cpython-95f34ab95959fa67d258043622744dae8519c5b2.tar.gz cpython-95f34ab95959fa67d258043622744dae8519c5b2.tar.bz2 |
Issue #18151: Replace remaining Idle 'open...close' pairs with 'with open'.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/idlelib/EditorWindow.py | 7 | ||||
-rw-r--r-- | Lib/idlelib/IOBinding.py | 15 | ||||
-rw-r--r-- | Lib/idlelib/ScriptBinding.py | 5 |
3 files changed, 10 insertions, 17 deletions
diff --git a/Lib/idlelib/EditorWindow.py b/Lib/idlelib/EditorWindow.py index 24f133f..c9ad364 100644 --- a/Lib/idlelib/EditorWindow.py +++ b/Lib/idlelib/EditorWindow.py @@ -882,12 +882,9 @@ class EditorWindow(object): "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', - encoding='utf_8', errors='replace') - try: + with open(self.recent_files_path, 'r', + encoding='utf_8', errors='replace') 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 --git a/Lib/idlelib/IOBinding.py b/Lib/idlelib/IOBinding.py index 37a11ad..4558ae6 100644 --- a/Lib/idlelib/IOBinding.py +++ b/Lib/idlelib/IOBinding.py @@ -208,11 +208,10 @@ class IOBinding: try: # open the file in binary mode so that we can handle # end-of-line convention ourselves. - f = open(filename,'rb') - two_lines = f.readline() + f.readline() - f.seek(0) - bytes = f.read() - f.close() + with open(filename, 'rb') as f: + two_lines = f.readline() + f.readline() + f.seek(0) + bytes = f.read() except OSError as msg: tkMessageBox.showerror("I/O Error", str(msg), master=self.text) return False @@ -373,10 +372,8 @@ class IOBinding: text = text.replace("\n", self.eol_convention) chars = self.encode(text) try: - f = open(filename, "wb") - f.write(chars) - f.flush() - f.close() + with open(filename, "wb") as f: + f.write(chars) return True except OSError as msg: tkMessageBox.showerror("I/O Error", str(msg), diff --git a/Lib/idlelib/ScriptBinding.py b/Lib/idlelib/ScriptBinding.py index 528adf6..6bfe128 100644 --- a/Lib/idlelib/ScriptBinding.py +++ b/Lib/idlelib/ScriptBinding.py @@ -87,9 +87,8 @@ class ScriptBinding: self.shell = shell = self.flist.open_shell() saved_stream = shell.get_warning_stream() shell.set_warning_stream(shell.stderr) - f = open(filename, 'rb') - source = f.read() - f.close() + with open(filename, 'rb') as f: + source = f.read() if b'\r' in source: source = source.replace(b'\r\n', b'\n') source = source.replace(b'\r', b'\n') |