[tor-commits] [oonib/master] tighter try/except/with blocks

art at torproject.org art at torproject.org
Wed Apr 23 14:31:52 UTC 2014


commit 112a7e2655f2ec2ea94670361c094dcaa51b85f5
Author: Darius Bacon <darius at wry.me>
Date:   Wed Apr 16 12:06:12 2014 -0700

    tighter try/except/with blocks
---
 oonib/deck/handlers.py |   18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/oonib/deck/handlers.py b/oonib/deck/handlers.py
index 8f30bfd..b9589b0 100644
--- a/oonib/deck/handlers.py
+++ b/oonib/deck/handlers.py
@@ -16,18 +16,20 @@ class DeckDescHandler(OONIBHandler):
         # against matching a certain pattern in the handler.
         bn = os.path.basename(deckID + '.desc')
         try:
-            with open(os.path.join(config.main.deck_dir, bn)) as f:
-                response = {}
-                deckDesc = yaml.safe_load(f)
-                for k in ['name', 'description', 'version', 'author', 'date']:
-                    response[k] = deckDesc[k]
+            f = open(os.path.join(config.main.deck_dir, bn))
         except IOError:
             log.err("Deck %s missing" % deckID)
             raise e.MissingDeck
-        except KeyError:
-            log.err("Deck %s missing required keys!" % deckID)
-            raise e.MissingDeckKeys
+        with f:
+            deckDesc = yaml.safe_load(f)
 
+        response = {}
+        for k in ['name', 'description', 'version', 'author', 'date']:
+            try:
+                response[k] = deckDesc[k]
+            except KeyError:
+                log.err("Deck %s missing required keys!" % deckID)
+                raise e.MissingDeckKeys
         self.write(response)
 
 class DeckListHandler(OONIBHandler):





More information about the tor-commits mailing list