From 54b53c067219cf0e8e580149e395ec1c402b4e6d Mon Sep 17 00:00:00 2001 From: Arfrever Frehtes Taifersar Arahesis Date: Wed, 2 Oct 2013 11:49:46 +0200 Subject: [PATCH] Use 'with' statements. --- pym/portage/output.py | 14 ++++---------- pym/portage/util/__init__.py | 20 +++++++++----------- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/pym/portage/output.py b/pym/portage/output.py index 20471bccb..fc1b04233 100644 --- a/pym/portage/output.py +++ b/pym/portage/output.py @@ -164,15 +164,12 @@ def _parse_color_map(config_root='/', onerror=None): token = token[1:-1] return token - f = None try: - f = io.open(_unicode_encode(myfile, + with io.open(_unicode_encode(myfile, encoding=_encodings['fs'], errors='strict'), - mode='r', encoding=_encodings['content'], errors='replace') - lineno = 0 - for line in f: - lineno += 1 - + mode='r', encoding=_encodings['content'], errors='replace') as f: + lines = f.readlines() + for lineno, line in enumerate(lines): commenter_pos = line.find("#") line = line[:commenter_pos].strip() @@ -230,9 +227,6 @@ def _parse_color_map(config_root='/', onerror=None): elif e.errno == errno.EACCES: raise PermissionDenied(myfile) raise - finally: - if f is not None: - f.close() def nc_len(mystr): tmp = re.sub(esc_seq + "^m]+m", "", mystr); diff --git a/pym/portage/util/__init__.py b/pym/portage/util/__init__.py index 8f311bf3b..e94849f8a 100644 --- a/pym/portage/util/__init__.py +++ b/pym/portage/util/__init__.py @@ -406,16 +406,15 @@ def read_corresponding_eapi_file(filename, default="0"): eapi = None try: - f = io.open(_unicode_encode(eapi_file, + with io.open(_unicode_encode(eapi_file, encoding=_encodings['fs'], errors='strict'), - mode='r', encoding=_encodings['repo.content'], errors='replace') - lines = f.readlines() + mode='r', encoding=_encodings['repo.content'], errors='replace') as f: + lines = f.readlines() if len(lines) == 1: eapi = lines[0].rstrip("\n") else: writemsg(_("--- Invalid 'eapi' file (doesn't contain exactly one line): %s\n") % (eapi_file), noiselevel=-1) - f.close() except IOError: pass @@ -546,14 +545,13 @@ def grablines(myfilename, recursive=0, remember_source_file=False): else: try: - myfile = io.open(_unicode_encode(myfilename, + with io.open(_unicode_encode(myfilename, encoding=_encodings['fs'], errors='strict'), - mode='r', encoding=_encodings['content'], errors='replace') - if remember_source_file: - mylines = [(line, myfilename) for line in myfile.readlines()] - else: - mylines = myfile.readlines() - myfile.close() + mode='r', encoding=_encodings['content'], errors='replace') as myfile: + if remember_source_file: + mylines = [(line, myfilename) for line in myfile.readlines()] + else: + mylines = myfile.readlines() except IOError as e: if e.errno == PermissionDenied.errno: raise PermissionDenied(myfilename) -- 2.26.2