From: Colin Watson Date: Thu, 19 Jan 2012 16:53:06 +0000 (+0000) Subject: Ensure that files are closed promptly in lsb_release.py X-Git-Tag: archive/raspbian/10.2018112800+rpi1^2~182 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=6d330a825c24be97ab0df9c0826fe4681d1c58d1;p=lsb.git Ensure that files are closed promptly in lsb_release.py Python 3.2 prints ResourceWarning noise otherwise. Signed-off-by: Didier Raboud --- diff --git a/lsb_release.py b/lsb_release.py index adbc24d..5c6cb9a 100644 --- a/lsb_release.py +++ b/lsb_release.py @@ -211,7 +211,8 @@ def guess_debian_release(): if os.path.exists('/etc/debian_version'): try: - release = open('/etc/debian_version').read().strip() + with open('/etc/debian_version') as debian_version: + release = debian_version.read().strip() except IOError, msg: print >> sys.stderr, 'Unable to open /etc/debian_version:', str(msg) release = 'unknown' @@ -262,20 +263,21 @@ def get_lsb_information(): distinfo = {} if os.path.exists('/etc/lsb-release'): try: - for line in open('/etc/lsb-release'): - line = line.strip() - if not line: - continue - # Skip invalid lines - if not '=' in line: - continue - var, arg = line.split('=', 1) - if var.startswith('DISTRIB_'): - var = var[8:] - if arg.startswith('"') and arg.endswith('"'): - arg = arg[1:-1] - if arg: # Ignore empty arguments - distinfo[var] = arg.strip() + with open('/etc/lsb-release') as lsb_release_file: + for line in lsb_release_file: + line = line.strip() + if not line: + continue + # Skip invalid lines + if not '=' in line: + continue + var, arg = line.split('=', 1) + if var.startswith('DISTRIB_'): + var = var[8:] + if arg.startswith('"') and arg.endswith('"'): + arg = arg[1:-1] + if arg: # Ignore empty arguments + distinfo[var] = arg.strip() except IOError, msg: print >> sys.stderr, 'Unable to open /etc/lsb-release:', str(msg)