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

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


commit fe398cdf47ab94d9199f286fc1b161b1ebee912c
Author: Darius Bacon <darius at wry.me>
Date:   Wed Apr 16 12:13:34 2014 -0700

    tighter try/except/with blocks
---
 oonib/input/handlers.py |   28 ++++++++++++++++------------
 1 file changed, 16 insertions(+), 12 deletions(-)

diff --git a/oonib/input/handlers.py b/oonib/input/handlers.py
index fce7451..33bec55 100644
--- a/oonib/input/handlers.py
+++ b/oonib/input/handlers.py
@@ -11,23 +11,27 @@ class InputDescHandler(OONIBHandler):
     def get(self, inputID):
         bn = os.path.basename(inputID) + ".desc"
         try:
-            with open(os.path.join(config.main.input_dir, bn)) as f:
-                response = {}
-                inputDesc = yaml.safe_load(f)
-                for k in ['name', 'description', 'version', 'author', 'date']:
-                    response[k] = inputDesc[k]
-                response['id'] = inputID
-            self.write(response)
+            f = open(os.path.join(config.main.input_dir, bn))
         except IOError:
             log.err("No Input Descriptor found for id %s" % inputID) 
             self.set_status(404)
             self.write({'error': 'missing-input'})
+            return
+        with f:
+            inputDesc = yaml.safe_load(f)
  
-        except Exception, e:
-            log.exception(e)
-            log.err("Invalid Input Descriptor found for id %s" % inputID) 
-            self.set_status(500)
-            self.write({'error': 'invalid-input-descriptor'})
+        response = {'id': inputID}
+        for k in ['name', 'description', 'version', 'author', 'date']:
+            try:
+                response[k] = inputDesc[k]
+            except Exception, e: # XXX this should probably be KeyError
+                log.exception(e)
+                log.err("Invalid Input Descriptor found for id %s" % inputID) 
+                self.set_status(500)
+                self.write({'error': 'invalid-input-descriptor'})
+                return
+
+        self.write(response)
 
 class InputListHandler(OONIBHandler):
     def get(self):





More information about the tor-commits mailing list