From f162ae0de0f71391c56957389cc3c8babc8022e1 Mon Sep 17 00:00:00 2001
From: Walter Purcaro <vuolter@users.noreply.github.com>
Date: Sun, 19 Apr 2015 16:37:00 +0200
Subject: Use with statement

---
 tests/APIExerciser.py | 54 ++++++++++++++++++++++++++-------------------------
 1 file changed, 28 insertions(+), 26 deletions(-)

(limited to 'tests')

diff --git a/tests/APIExerciser.py b/tests/APIExerciser.py
index d17f81ae2..d7300b1a5 100644
--- a/tests/APIExerciser.py
+++ b/tests/APIExerciser.py
@@ -1,6 +1,8 @@
 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
 
+from __future__ import with_statement
+
 import string
 from threading import Thread
 from random import choice, sample, randint
@@ -60,32 +62,32 @@ class APIExerciser(Thread):
 
         self.core.log.info("API Excerciser started %d" % self.id)
 
-        out = open("error.log", "ab")
-        # core errors are not logged of course
-        out.write("\n" + "Starting\n")
-        out.flush()
-
-        while True:
-            try:
-                self.testAPI()
-            except Exception:
-                self.core.log.error("Excerciser %d throw an execption" % self.id)
-                print_exc()
-                out.write(format_exc() + 2 * "\n")
-                out.flush()
-
-            if not self.count % 100:
-                self.core.log.info("Exerciser %d tested %d api calls" % (self.id, self.count))
-            if not self.count % 1000:
-                out.flush()
-
-            if not sumCalled % 1000:  #: not thread safe
-                self.core.log.info("Exercisers tested %d api calls" % sumCalled)
-                persec = sumCalled / (time() - self.time)
-                self.core.log.info("Approx. %.2f calls per second." % persec)
-                self.core.log.info("Approx. %.2f ms per call." % (1000 / persec))
-                self.core.log.info("Collected garbage: %d" % gc.collect())
-                # sleep(random() / 500)
+        with open("error.log", "ab") as out:
+            # core errors are not logged of course
+            out.write("\n" + "Starting\n")
+            out.flush()
+
+            while True:
+                try:
+                    self.testAPI()
+                except Exception:
+                    self.core.log.error("Excerciser %d throw an execption" % self.id)
+                    print_exc()
+                    out.write(format_exc() + 2 * "\n")
+                    out.flush()
+
+                if not self.count % 100:
+                    self.core.log.info("Exerciser %d tested %d api calls" % (self.id, self.count))
+                if not self.count % 1000:
+                    out.flush()
+
+                if not sumCalled % 1000:  #: not thread safe
+                    self.core.log.info("Exercisers tested %d api calls" % sumCalled)
+                    persec = sumCalled / (time() - self.time)
+                    self.core.log.info("Approx. %.2f calls per second." % persec)
+                    self.core.log.info("Approx. %.2f ms per call." % (1000 / persec))
+                    self.core.log.info("Collected garbage: %d" % gc.collect())
+                    # sleep(random() / 500)
 
 
     def testAPI(self):
-- 
cgit v1.2.3