--- a/eric6/DebugClients/Python/PyProfile.py Tue Oct 13 19:02:26 2020 +0200 +++ b/eric6/DebugClients/Python/PyProfile.py Wed Oct 14 17:50:39 2020 +0200 @@ -52,14 +52,12 @@ return try: - cache = open(self.timingCache, 'rb') - timings = marshal.load(cache) # secok - if isinstance(timings, dict): - self.timings = timings + with open(self.timingCache, 'rb') as cache: + timings = marshal.load(cache) # secok + if isinstance(timings, dict): + self.timings = timings except (EnvironmentError, EOFError, ValueError, TypeError): pass - finally: - cache.close() def save(self): """ @@ -67,12 +65,10 @@ """ # dump the raw timing data try: - cache = open(self.timingCache, 'wb') - marshal.dump(self.timings, cache) + with open(self.timingCache, 'wb') as cache: + marshal.dump(self.timings, cache) except EnvironmentError: pass - finally: - cache.close() # dump the profile data self.dump_stats(self.profileCache) @@ -83,14 +79,12 @@ @param file name of the file to write to (string) """ + self.create_stats() try: - f = open(file, 'wb') - self.create_stats() - pickle.dump(self.stats, f, 4) + with open(file, 'wb') as f: + pickle.dump(self.stats, f, 4) except (EnvironmentError, pickle.PickleError): pass - finally: - f.close() def erase(self): """