]> granicus.if.org Git - python/commitdiff
Fix ResourceWarning for unclosed files (from os.popen)
authorBrian Curtin <brian.curtin@gmail.com>
Sun, 31 Oct 2010 01:10:58 +0000 (01:10 +0000)
committerBrian Curtin <brian.curtin@gmail.com>
Sun, 31 Oct 2010 01:10:58 +0000 (01:10 +0000)
Lib/uuid.py

index 6e687943d66f8624a8ab67c29f709ee01280a812..df5146428f092d352c466bb32bf492292e19d40b 100644 (file)
@@ -322,15 +322,15 @@ def _find_mac(command, args, hw_identifiers, get_index):
             # LC_ALL to get English output, 2>/dev/null to
             # prevent output on stderr
             cmd = 'LC_ALL=C %s %s 2>/dev/null' % (executable, args)
-            pipe = os.popen(cmd)
+            with os.popen(cmd) as pipe:
+                for line in pipe:
+                    words = line.lower().split()
+                    for i in range(len(words)):
+                        if words[i] in hw_identifiers:
+                            return int(
+                                words[get_index(i)].replace(':', ''), 16)
         except IOError:
             continue
-
-        for line in pipe:
-            words = line.lower().split()
-            for i in range(len(words)):
-                if words[i] in hw_identifiers:
-                    return int(words[get_index(i)].replace(':', ''), 16)
     return None
 
 def _ifconfig_getnode():