Commits

Anonymous committed 814e913

fieldcache/multiproc: make sure some files get always closed (using "with" or try/finally)

Fixes this:
test_sorting.test_float_cache ... /home/tw/w/whoosh/src/whoosh/filedb/fieldcache.py:620:
ResourceWarning: unclosed file <_io.BufferedReader name='.../tmp/floatcache/floatcache_4maef540gqtr.num.fc'>
test_sorting.test_long_cache ... /home/tw/w/whoosh/src/whoosh/filedb/fieldcache.py:620:
ResourceWarning: unclosed file <_io.BufferedReader name='.../tmp/longcache/longcache_dxzwdp6c9v48.num.fc'>

  • Participants
  • Parent commits 3e4b8c3
  • Branches 2.4x

Comments (0)

Files changed (2)

File src/whoosh/filedb/fieldcache.py

             gzipped = True
 
         f = storage.open_file(filename, gzip=gzipped)
-        cache = self.fcclass.from_file(f)
-        f.close()
+        try:
+            cache = self.fcclass.from_file(f)
+        finally:
+            f.close()
         return cache
 
     def is_loaded(self, key):

File src/whoosh/filedb/multiproc.py

 
         writer = self.writer
         load = pickle.load
-        f = open(filename, "rb")
-        for _ in xrange(doc_count):
-            # Load the next pickled tuple from the file
-            code, args = load(f)
-            assert code == 0
-            writer.add_document(**args)
-        f.close()
+        with open(filename, "rb") as f:
+            for _ in xrange(doc_count):
+                # Load the next pickled tuple from the file
+                code, args = load(f)
+                assert code == 0
+                writer.add_document(**args)
         # Remove the job file
         os.remove(filename)