summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZac Medico <zmedico@gentoo.org>2012-12-28 22:30:09 -0800
committerZac Medico <zmedico@gentoo.org>2012-12-28 22:30:09 -0800
commit5e5a5cddfdeb3aa05932114fc1dce65b5be11ae9 (patch)
tree3000e19e6917b7d1642d7c878972d5ec165e8878
parente94233871e20fd3852e511ed1bd91c2826870f50 (diff)
downloadportage-5e5a5cddfdeb3aa05932114fc1dce65b5be11ae9.tar.gz
portage-5e5a5cddfdeb3aa05932114fc1dce65b5be11ae9.tar.bz2
portage-5e5a5cddfdeb3aa05932114fc1dce65b5be11ae9.zip
Use 'with file' more.
This helps to minimize ResourceWarning triggered by ^C with python3.
-rw-r--r--pym/portage/cache/flat_hash.py9
-rw-r--r--pym/portage/checksum.py17
-rw-r--r--pym/portage/manifest.py11
3 files changed, 16 insertions, 21 deletions
diff --git a/pym/portage/cache/flat_hash.py b/pym/portage/cache/flat_hash.py
index b71e118d5..b1c94313b 100644
--- a/pym/portage/cache/flat_hash.py
+++ b/pym/portage/cache/flat_hash.py
@@ -1,4 +1,4 @@
-# Copyright: 2005-2011 Gentoo Foundation
+# Copyright: 2005-2012 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# Author(s): Brian Harring (ferringb@gentoo.org)
@@ -42,11 +42,10 @@ class database(fs_template.FsBased):
# Don't use os.path.join, for better performance.
fp = self.location + _os.sep + cpv
try:
- myf = io.open(_unicode_encode(fp,
+ with io.open(_unicode_encode(fp,
encoding=_encodings['fs'], errors='strict'),
mode='r', encoding=_encodings['repo.content'],
- errors='replace')
- try:
+ errors='replace') as myf:
lines = myf.read().split("\n")
if not lines[-1]:
lines.pop()
@@ -56,8 +55,6 @@ class database(fs_template.FsBased):
# that uses mtime mangling.
d['_mtime_'] = _os.fstat(myf.fileno())[stat.ST_MTIME]
return d
- finally:
- myf.close()
except (IOError, OSError) as e:
if e.errno != errno.ENOENT:
raise cache_errors.CacheCorruption(cpv, e)
diff --git a/pym/portage/checksum.py b/pym/portage/checksum.py
index 30a9234e1..cd663e767 100644
--- a/pym/portage/checksum.py
+++ b/pym/portage/checksum.py
@@ -50,16 +50,15 @@ class _generate_hash_function(object):
@type filename: String
@return: The hash and size of the data
"""
- f = _open_file(filename)
- blocksize = HASHING_BLOCKSIZE
- data = f.read(blocksize)
- size = 0
- checksum = self._hashobject()
- while data:
- checksum.update(data)
- size = size + len(data)
+ with _open_file(filename) as f:
+ blocksize = HASHING_BLOCKSIZE
+ size = 0
+ checksum = self._hashobject()
data = f.read(blocksize)
- f.close()
+ while data:
+ checksum.update(data)
+ size = size + len(data)
+ data = f.read(blocksize)
return (checksum.hexdigest(), size)
diff --git a/pym/portage/manifest.py b/pym/portage/manifest.py
index 9a85c8f6d..a75c63a69 100644
--- a/pym/portage/manifest.py
+++ b/pym/portage/manifest.py
@@ -184,13 +184,12 @@ class Manifest(object):
"""Parse a manifest. If myhashdict is given then data will be added too it.
Otherwise, a new dict will be created and returned."""
try:
- fd = io.open(_unicode_encode(file_path,
+ with io.open(_unicode_encode(file_path,
encoding=_encodings['fs'], errors='strict'), mode='r',
- encoding=_encodings['repo.content'], errors='replace')
- if myhashdict is None:
- myhashdict = {}
- self._parseDigests(fd, myhashdict=myhashdict, **kwargs)
- fd.close()
+ encoding=_encodings['repo.content'], errors='replace') as f:
+ if myhashdict is None:
+ myhashdict = {}
+ self._parseDigests(f, myhashdict=myhashdict, **kwargs)
return myhashdict
except (OSError, IOError) as e:
if e.errno == errno.ENOENT: