changeset 209:76a6dd2fae57

fs2: use file context manager
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 01 May 2011 02:18:11 +0200
parents 321898d8273f
children 316c839a6f62
files MoinMoin/storage/backends/fs2.py
diffstat 1 files changed, 11 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/storage/backends/fs2.py	Sat Apr 30 23:18:40 2011 +0200
+++ b/MoinMoin/storage/backends/fs2.py	Sun May 01 02:18:11 2011 +0200
@@ -73,12 +73,11 @@
 
     @cached_property
     def _fs_metadata(self):
-        f = open(self._fs_path_meta, 'rb')
-        try:
-            metadata = pickle.load(f)
-        except EOFError:
-            metadata = {}
-        f.close()
+        with open(self._fs_path_meta, 'rb') as f:
+            try:
+                metadata = pickle.load(f)
+            except EOFError:
+                metadata = {}
         return metadata
 
     @cached_property
@@ -294,9 +293,8 @@
         if itemmeta:
             # only write item level metadata file if we have any
             mp = self._make_path('meta', item_id, 'item')
-            f = open(mp, 'wb')
-            pickle.dump(itemmeta, f, protocol=PICKLEPROTOCOL)
-            f.close()
+            with open(mp, 'wb') as f:
+                pickle.dump(itemmeta, f, protocol=PICKLEPROTOCOL)
 
         item._fs_item_id = item_id
 
@@ -399,9 +397,8 @@
                     # ignore, there might not have been metadata
             else:
                 tmp = self._make_path('meta', item._fs_item_id, 'item.tmp')
-                f = open(tmp, 'wb')
-                pickle.dump(md, f, protocol=PICKLEPROTOCOL)
-                f.close()
+                with open(tmp, 'wb') as f:
+                    pickle.dump(md, f, protocol=PICKLEPROTOCOL)
 
                 filesys.rename(tmp, self._make_path('meta', item._fs_item_id, 'item'))
             item._fs_metadata_lock.release()
@@ -411,9 +408,8 @@
         if item._fs_item_id is not None:
             p = self._make_path('meta', item._fs_item_id, 'item')
             try:
-                f = open(p, 'rb')
-                metadata = pickle.load(f)
-                f.close()
+                with open(p, 'rb') as f:
+                    metadata = pickle.load(f)
             except IOError as err:
                 if err.errno != errno.ENOENT:
                     raise