]> granicus.if.org Git - python/commitdiff
Issue #19734: ctypes resource management fixes
authorNick Coghlan <ncoghlan@gmail.com>
Sun, 24 Nov 2013 02:53:50 +0000 (12:53 +1000)
committerNick Coghlan <ncoghlan@gmail.com>
Sun, 24 Nov 2013 02:53:50 +0000 (12:53 +1000)
Lib/ctypes/util.py

index d2c04d21424e0b067074ffa564fd54c0131ce477..0cf20766ccace357d8edd469e48c5025d34b52ec 100644 (file)
@@ -132,8 +132,10 @@ elif os.name == "posix":
             cmd = 'if ! type objdump >/dev/null 2>&1; then exit 10; fi;' \
                   "objdump -p -j .dynamic 2>/dev/null " + f
             f = os.popen(cmd)
-            dump = f.read()
-            rv = f.close()
+            try:
+                dump = f.read()
+            finally:
+                rv = f.close()
             if rv == 10:
                 raise OSError('objdump command not found')
             res = re.search(r'\sSONAME\s+([^\s]+)', dump)
@@ -176,10 +178,11 @@ elif os.name == "posix":
             else:
                 cmd = 'env LC_ALL=C /usr/bin/crle 2>/dev/null'
 
-            for line in os.popen(cmd).readlines():
-                line = line.strip()
-                if line.startswith('Default Library Path (ELF):'):
-                    paths = line.split()[4]
+            with contextlib.closing(os.popen(cmd)) as f:
+                for line in f.readlines():
+                    line = line.strip()
+                    if line.startswith('Default Library Path (ELF):'):
+                        paths = line.split()[4]
 
             if not paths:
                 return None