diff options
author | Eric Smith <eric@trueblade.com> | 2009-10-23 12:56:11 (GMT) |
---|---|---|
committer | Eric Smith <eric@trueblade.com> | 2009-10-23 12:56:11 (GMT) |
commit | 5ccfa29f04c6e16d6a4fd990972fb96f77d0a969 (patch) | |
tree | 82063d0385d9c5532680ba51aa3bde6d3efb8292 /Lib | |
parent | 09d95625d9ef0abc629074a30ab4928a014f2cb9 (diff) | |
download | cpython-5ccfa29f04c6e16d6a4fd990972fb96f77d0a969.zip cpython-5ccfa29f04c6e16d6a4fd990972fb96f77d0a969.tar.gz cpython-5ccfa29f04c6e16d6a4fd990972fb96f77d0a969.tar.bz2 |
Changed try/finally to contextlib.closing, as discussed in issue 6882.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/ctypes/util.py | 31 |
1 files changed, 7 insertions, 24 deletions
diff --git a/Lib/ctypes/util.py b/Lib/ctypes/util.py index 1bd2d70..dca4ec3 100644 --- a/Lib/ctypes/util.py +++ b/Lib/ctypes/util.py @@ -1,4 +1,5 @@ import sys, os +import contextlib # find_library(name) returns the pathname of a library, or None. if os.name == "nt": @@ -117,11 +118,8 @@ elif os.name == "posix": if not f: return None cmd = "/usr/ccs/bin/dump -Lpv 2>/dev/null " + f - f = os.popen(cmd) - try: + with contextlib.closing(os.popen(cmd)) as f: data = f.read() - finally: - f.close() res = re.search(r'\[.*\]\sSONAME\s+([^\s]+)', data) if not res: return None @@ -138,11 +136,8 @@ elif os.name == "posix": rv = f.close() if rv == 10: raise OSError('objdump command not found') - f = os.popen(cmd) - try: + with contextlib.closing(os.popen(cmd)) as f: data = f.read() - finally: - f.close() res = re.search(r'\sSONAME\s+([^\s]+)', data) if not res: return None @@ -166,11 +161,8 @@ elif os.name == "posix": def find_library(name): ename = re.escape(name) expr = r':-l%s\.\S+ => \S*/(lib%s\.\S+)' % (ename, ename) - f = os.popen('/sbin/ldconfig -r 2>/dev/null') - try: + with contextlib.closing(os.popen('/sbin/ldconfig -r 2>/dev/null')) as f: data = f.read() - finally: - f.close() res = re.findall(expr, data) if not res: return _get_soname(_findLib_gcc(name)) @@ -182,20 +174,14 @@ elif os.name == "posix": def _findLib_ldconfig(name): # XXX assuming GLIBC's ldconfig (with option -p) expr = r'/[^\(\)\s]*lib%s\.[^\(\)\s]*' % re.escape(name) - f = os.popen('/sbin/ldconfig -p 2>/dev/null') - try: + with contextlib.closing(os.popen('/sbin/ldconfig -p 2>/dev/null')) as f: data = f.read() - finally: - f.close() res = re.search(expr, data) if not res: # Hm, this works only for libs needed by the python executable. cmd = 'ldd %s 2>/dev/null' % sys.executable - f = os.popen(cmd) - try: + with contextlib.closing(os.popen(cmd)) as f: data = f.read() - finally: - f.close() res = re.search(expr, data) if not res: return None @@ -219,11 +205,8 @@ elif os.name == "posix": # XXX assuming GLIBC's ldconfig (with option -p) expr = r'(\S+)\s+\((%s(?:, OS ABI:[^\)]*)?)\)[^/]*(/[^\(\)\s]*lib%s\.[^\(\)\s]*)' \ % (abi_type, re.escape(name)) - f = os.popen('/sbin/ldconfig -p 2>/dev/null') - try: + with contextlib.closing(os.popen('/sbin/ldconfig -p 2>/dev/null')) as f: data = f.read() - finally: - f.close() res = re.search(expr, data) if not res: return None |