public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
From: "Arfrever Frehtes Taifersar Arahesis" <Arfrever@Apache.Org>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/portage:master commit in: pym/portage/, pym/portage/util/
Date: Wed,  2 Oct 2013 09:50:59 +0000 (UTC)	[thread overview]
Message-ID: <1380707386.54b53c067219cf0e8e580149e395ec1c402b4e6d.arfrever@gentoo> (raw)

commit:     54b53c067219cf0e8e580149e395ec1c402b4e6d
Author:     Arfrever Frehtes Taifersar Arahesis <Arfrever <AT> Apache <DOT> Org>
AuthorDate: Wed Oct  2 09:49:46 2013 +0000
Commit:     Arfrever Frehtes Taifersar Arahesis <Arfrever <AT> Apache <DOT> Org>
CommitDate: Wed Oct  2 09:49:46 2013 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/portage.git;a=commit;h=54b53c06

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 20471bc..fc1b042 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 8f311bf..e94849f 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)


             reply	other threads:[~2013-10-02  9:51 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-10-02  9:50 Arfrever Frehtes Taifersar Arahesis [this message]
  -- strict thread matches above, loose matches on Subject: below --
2012-02-04 14:47 [gentoo-commits] proj/portage:master commit in: pym/portage/, pym/portage/util/ Zac Medico
2011-12-11  6:41 Zac Medico
2011-10-02  5:36 Zac Medico
2011-06-04  0:01 Zac Medico

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1380707386.54b53c067219cf0e8e580149e395ec1c402b4e6d.arfrever@gentoo \
    --to=arfrever@apache.org \
    --cc=gentoo-commits@lists.gentoo.org \
    --cc=gentoo-dev@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox