jenkins-bot has submitted this change and it was merged.

Change subject: throttle.py: Make sure opened files are closed properly
......................................................................


throttle.py: Make sure opened files are closed properly

Previously, if there was an IOError during file opening, the
file would not have been closed.

Bug: T95111
Change-Id: I6af4adae0e731cbcc8ff0c002b7d188f4e874520
---
M pywikibot/throttle.py
1 file changed, 6 insertions(+), 6 deletions(-)

Approvals:
  Xqt: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/pywikibot/throttle.py b/pywikibot/throttle.py
index d125df1..9a38e46 100644
--- a/pywikibot/throttle.py
+++ b/pywikibot/throttle.py
@@ -204,12 +204,13 @@
         self.checktime = 0
         processes = []
         try:
-            f = open(self.ctrlfilename, 'r')
+            with open(self.ctrlfilename, 'r') as f:
+                lines = f.readlines()
         except IOError:
             return
         else:
             now = time.time()
-            for line in f.readlines():
+            for line in lines:
                 try:
                     line = line.split(' ')
                     this_pid = int(line[0])
@@ -225,12 +226,11 @@
                                       'site': this_site})
         processes.sort(key=lambda p: p['pid'])
         try:
-            f = open(self.ctrlfilename, 'w')
-            for p in processes:
-                f.write("%(pid)s %(time)s %(site)s\n" % p)
+            with open(self.ctrlfilename, 'w') as f:
+                for p in processes:
+                    f.write("%(pid)s %(time)s %(site)s\n" % p)
         except IOError:
             return
-        f.close()
 
     def wait(self, seconds):
         """Wait for seconds seconds.

-- 
To view, visit https://gerrit.wikimedia.org/r/321727
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I6af4adae0e731cbcc8ff0c002b7d188f4e874520
Gerrit-PatchSet: 4
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Dalba <dalba.w...@gmail.com>
Gerrit-Reviewer: John Vandenberg <jay...@gmail.com>
Gerrit-Reviewer: Xqt <i...@gno.de>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to