[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

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



commit fe398cdf47ab94d9199f286fc1b161b1ebee912c
Author: Darius Bacon <darius@xxxxxx>
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):



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits