]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #18151: Replace remaining Idle 'open...close' pairs with 'with open'.
authorTerry Jan Reedy <tjreedy@udel.edu>
Sun, 4 Aug 2013 19:39:56 +0000 (15:39 -0400)
committerTerry Jan Reedy <tjreedy@udel.edu>
Sun, 4 Aug 2013 19:39:56 +0000 (15:39 -0400)
Lib/idlelib/EditorWindow.py
Lib/idlelib/IOBinding.py
Lib/idlelib/ScriptBinding.py

index 5de53a99d9454b9bcd5390ffe547373277b0de90..07ca5562330e7868a4689726ebd779ca9f6ca618 100644 (file)
@@ -894,11 +894,8 @@ 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')
-            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:
index f0d4d227c3c5a4e0e6adc06386386d67899404ca..ba45ee833feb843d87f40c89872a6733a8228871 100644 (file)
@@ -248,9 +248,8 @@ class IOBinding:
         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 @@ class IOBinding:
         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),
index 665b3b241ccd8f6d2b12b7d7686af62b9ca634f3..a024ebfb58e07ddc4e17c9d99dc4d67b210a0c08 100644 (file)
@@ -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, '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)