[tor-commits] [ooni-probe/master] Fix bug in utils.py

art at torproject.org art at torproject.org
Sun Feb 12 16:18:24 UTC 2012


commit e765c73bf1c96258a18f3609b5ee3a7ed369c58d
Author: Sathyanarayanan Gunasekaran <gsathya.ceg at gmail.com>
Date:   Sun Feb 12 14:10:25 2012 +0530

    Fix bug in utils.py
    
    UnboundLocalError: local variable 'f' referenced before assignment.
    Fixed by using 'with' rather than manually opening and closing file
    descriptors.
---
 refactor/utils.py |   27 +++++++++++++--------------
 1 files changed, 13 insertions(+), 14 deletions(-)

diff --git a/refactor/utils.py b/refactor/utils.py
index cc5ae48..5c743ba 100644
--- a/refactor/utils.py
+++ b/refactor/utils.py
@@ -68,29 +68,28 @@ def get_logger(config):
 def parse_asset(asset):
     parsed = Storage()
     try:
-        f = open(asset, 'r')
-        for line in f.readlines():
+        with open(asset, 'r') as f:
+            for line in f.readlines():
             # XXX This should be rewritten, if the values contain
             #     #: they will be rewritten with blank.
             # should not be an issue but this is not a very good parser
-            if line.startswith("#:"):
-                n = line.split(' ')[0].replace('#:','')
-                v = line.replace('#:'+n+' ', '').strip()
-                if n in ('tests', 'files'):
-                    parsed[n] = v.split(",")
+                if line.startswith("#:"):
+                    n = line.split(' ')[0].replace('#:','')
+                    v = line.replace('#:'+n+' ', '').strip()
+                    if n in ('tests', 'files'):
+                        parsed[n] = v.split(",")
+                    else:
+                        parsed[n] = v
+                        
+                elif line.startswith("#"):
+                    continue
                 else:
-                    parsed[n] = v
-
-            elif line.startswith("#"):
-                continue
-            else:
-                break
+                        break
     finally:
         if not parsed.name:
             parsed.name = asset
         if not parsed.files:
             parsed.files = asset
-        f.close()
         return parsed
 
 def import_test(name, config):





More information about the tor-commits mailing list