From a12c63842b28e29f3bc6718e6d940d5b697f010f Mon Sep 17 00:00:00 2001 From: Zac Medico Date: Wed, 24 Aug 2011 19:25:59 -0700 Subject: python3.2 fixes: ResourceWarning: unclosed file --- pym/portage/util/__init__.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'pym/portage/util/__init__.py') diff --git a/pym/portage/util/__init__.py b/pym/portage/util/__init__.py index 4aa63d5e3..de3abdaa2 100644 --- a/pym/portage/util/__init__.py +++ b/pym/portage/util/__init__.py @@ -534,16 +534,18 @@ def getconfig(mycfg, tolerant=0, allow_sourcing=False, expand=True): else: expand_map = {} mykeys = {} + f = None try: # NOTE: shlex doesn't support unicode objects with Python 2 # (produces spurious \0 characters). if sys.hexversion < 0x3000000: - content = open(_unicode_encode(mycfg, - encoding=_encodings['fs'], errors='strict'), 'rb').read() + f = open(_unicode_encode(mycfg, + encoding=_encodings['fs'], errors='strict'), 'rb') else: - content = open(_unicode_encode(mycfg, + f = open(_unicode_encode(mycfg, encoding=_encodings['fs'], errors='strict'), mode='r', - encoding=_encodings['content'], errors='replace').read() + encoding=_encodings['content'], errors='replace') + content = f.read() except IOError as e: if e.errno == PermissionDenied.errno: raise PermissionDenied(mycfg) @@ -552,6 +554,9 @@ def getconfig(mycfg, tolerant=0, allow_sourcing=False, expand=True): if e.errno not in (errno.EISDIR,): raise return None + finally: + if f is not None: + f.close() # Workaround for avoiding a silent error in shlex that is # triggered by a source statement at the end of the file -- cgit v1.2.3-1-g7c22