[Python-checkins] cpython: Issue #19734: ctypes resource management fixes

nick.coghlan python-checkins at python.org
Sun Nov 24 03:54:05 CET 2013


http://hg.python.org/cpython/rev/7c080ee796a6
changeset:   87484:7c080ee796a6
user:        Nick Coghlan <ncoghlan at gmail.com>
date:        Sun Nov 24 12:53:50 2013 +1000
summary:
  Issue #19734: ctypes resource management fixes

files:
  Lib/ctypes/util.py |  15 +++++++++------
  1 files changed, 9 insertions(+), 6 deletions(-)


diff --git a/Lib/ctypes/util.py b/Lib/ctypes/util.py
--- a/Lib/ctypes/util.py
+++ b/Lib/ctypes/util.py
@@ -132,8 +132,10 @@
             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 @@
             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

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list