1 |
commit: 5e5a5cddfdeb3aa05932114fc1dce65b5be11ae9 |
2 |
Author: Zac Medico <zmedico <AT> gentoo <DOT> org> |
3 |
AuthorDate: Sat Dec 29 06:30:09 2012 +0000 |
4 |
Commit: Zac Medico <zmedico <AT> gentoo <DOT> org> |
5 |
CommitDate: Sat Dec 29 06:30:09 2012 +0000 |
6 |
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/portage.git;a=commit;h=5e5a5cdd |
7 |
|
8 |
Use 'with file' more. |
9 |
|
10 |
This helps to minimize ResourceWarning triggered by ^C with python3. |
11 |
|
12 |
--- |
13 |
pym/portage/cache/flat_hash.py | 9 +++------ |
14 |
pym/portage/checksum.py | 17 ++++++++--------- |
15 |
pym/portage/manifest.py | 11 +++++------ |
16 |
3 files changed, 16 insertions(+), 21 deletions(-) |
17 |
|
18 |
diff --git a/pym/portage/cache/flat_hash.py b/pym/portage/cache/flat_hash.py |
19 |
index b71e118..b1c9431 100644 |
20 |
--- a/pym/portage/cache/flat_hash.py |
21 |
+++ b/pym/portage/cache/flat_hash.py |
22 |
@@ -1,4 +1,4 @@ |
23 |
-# Copyright: 2005-2011 Gentoo Foundation |
24 |
+# Copyright: 2005-2012 Gentoo Foundation |
25 |
# Distributed under the terms of the GNU General Public License v2 |
26 |
# Author(s): Brian Harring (ferringb@g.o) |
27 |
|
28 |
@@ -42,11 +42,10 @@ class database(fs_template.FsBased): |
29 |
# Don't use os.path.join, for better performance. |
30 |
fp = self.location + _os.sep + cpv |
31 |
try: |
32 |
- myf = io.open(_unicode_encode(fp, |
33 |
+ with io.open(_unicode_encode(fp, |
34 |
encoding=_encodings['fs'], errors='strict'), |
35 |
mode='r', encoding=_encodings['repo.content'], |
36 |
- errors='replace') |
37 |
- try: |
38 |
+ errors='replace') as myf: |
39 |
lines = myf.read().split("\n") |
40 |
if not lines[-1]: |
41 |
lines.pop() |
42 |
@@ -56,8 +55,6 @@ class database(fs_template.FsBased): |
43 |
# that uses mtime mangling. |
44 |
d['_mtime_'] = _os.fstat(myf.fileno())[stat.ST_MTIME] |
45 |
return d |
46 |
- finally: |
47 |
- myf.close() |
48 |
except (IOError, OSError) as e: |
49 |
if e.errno != errno.ENOENT: |
50 |
raise cache_errors.CacheCorruption(cpv, e) |
51 |
|
52 |
diff --git a/pym/portage/checksum.py b/pym/portage/checksum.py |
53 |
index 30a9234..cd663e7 100644 |
54 |
--- a/pym/portage/checksum.py |
55 |
+++ b/pym/portage/checksum.py |
56 |
@@ -50,16 +50,15 @@ class _generate_hash_function(object): |
57 |
@type filename: String |
58 |
@return: The hash and size of the data |
59 |
""" |
60 |
- f = _open_file(filename) |
61 |
- blocksize = HASHING_BLOCKSIZE |
62 |
- data = f.read(blocksize) |
63 |
- size = 0 |
64 |
- checksum = self._hashobject() |
65 |
- while data: |
66 |
- checksum.update(data) |
67 |
- size = size + len(data) |
68 |
+ with _open_file(filename) as f: |
69 |
+ blocksize = HASHING_BLOCKSIZE |
70 |
+ size = 0 |
71 |
+ checksum = self._hashobject() |
72 |
data = f.read(blocksize) |
73 |
- f.close() |
74 |
+ while data: |
75 |
+ checksum.update(data) |
76 |
+ size = size + len(data) |
77 |
+ data = f.read(blocksize) |
78 |
|
79 |
return (checksum.hexdigest(), size) |
80 |
|
81 |
|
82 |
diff --git a/pym/portage/manifest.py b/pym/portage/manifest.py |
83 |
index 9a85c8f..a75c63a 100644 |
84 |
--- a/pym/portage/manifest.py |
85 |
+++ b/pym/portage/manifest.py |
86 |
@@ -184,13 +184,12 @@ class Manifest(object): |
87 |
"""Parse a manifest. If myhashdict is given then data will be added too it. |
88 |
Otherwise, a new dict will be created and returned.""" |
89 |
try: |
90 |
- fd = io.open(_unicode_encode(file_path, |
91 |
+ with io.open(_unicode_encode(file_path, |
92 |
encoding=_encodings['fs'], errors='strict'), mode='r', |
93 |
- encoding=_encodings['repo.content'], errors='replace') |
94 |
- if myhashdict is None: |
95 |
- myhashdict = {} |
96 |
- self._parseDigests(fd, myhashdict=myhashdict, **kwargs) |
97 |
- fd.close() |
98 |
+ encoding=_encodings['repo.content'], errors='replace') as f: |
99 |
+ if myhashdict is None: |
100 |
+ myhashdict = {} |
101 |
+ self._parseDigests(f, myhashdict=myhashdict, **kwargs) |
102 |
return myhashdict |
103 |
except (OSError, IOError) as e: |
104 |
if e.errno == errno.ENOENT: |