1 |
commit: 54b53c067219cf0e8e580149e395ec1c402b4e6d |
2 |
Author: Arfrever Frehtes Taifersar Arahesis <Arfrever <AT> Apache <DOT> Org> |
3 |
AuthorDate: Wed Oct 2 09:49:46 2013 +0000 |
4 |
Commit: Arfrever Frehtes Taifersar Arahesis <Arfrever <AT> Apache <DOT> Org> |
5 |
CommitDate: Wed Oct 2 09:49:46 2013 +0000 |
6 |
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/portage.git;a=commit;h=54b53c06 |
7 |
|
8 |
Use 'with' statements. |
9 |
|
10 |
--- |
11 |
pym/portage/output.py | 14 ++++---------- |
12 |
pym/portage/util/__init__.py | 20 +++++++++----------- |
13 |
2 files changed, 13 insertions(+), 21 deletions(-) |
14 |
|
15 |
diff --git a/pym/portage/output.py b/pym/portage/output.py |
16 |
index 20471bc..fc1b042 100644 |
17 |
--- a/pym/portage/output.py |
18 |
+++ b/pym/portage/output.py |
19 |
@@ -164,15 +164,12 @@ def _parse_color_map(config_root='/', onerror=None): |
20 |
token = token[1:-1] |
21 |
return token |
22 |
|
23 |
- f = None |
24 |
try: |
25 |
- f = io.open(_unicode_encode(myfile, |
26 |
+ with io.open(_unicode_encode(myfile, |
27 |
encoding=_encodings['fs'], errors='strict'), |
28 |
- mode='r', encoding=_encodings['content'], errors='replace') |
29 |
- lineno = 0 |
30 |
- for line in f: |
31 |
- lineno += 1 |
32 |
- |
33 |
+ mode='r', encoding=_encodings['content'], errors='replace') as f: |
34 |
+ lines = f.readlines() |
35 |
+ for lineno, line in enumerate(lines): |
36 |
commenter_pos = line.find("#") |
37 |
line = line[:commenter_pos].strip() |
38 |
|
39 |
@@ -230,9 +227,6 @@ def _parse_color_map(config_root='/', onerror=None): |
40 |
elif e.errno == errno.EACCES: |
41 |
raise PermissionDenied(myfile) |
42 |
raise |
43 |
- finally: |
44 |
- if f is not None: |
45 |
- f.close() |
46 |
|
47 |
def nc_len(mystr): |
48 |
tmp = re.sub(esc_seq + "^m]+m", "", mystr); |
49 |
|
50 |
diff --git a/pym/portage/util/__init__.py b/pym/portage/util/__init__.py |
51 |
index 8f311bf..e94849f 100644 |
52 |
--- a/pym/portage/util/__init__.py |
53 |
+++ b/pym/portage/util/__init__.py |
54 |
@@ -406,16 +406,15 @@ def read_corresponding_eapi_file(filename, default="0"): |
55 |
|
56 |
eapi = None |
57 |
try: |
58 |
- f = io.open(_unicode_encode(eapi_file, |
59 |
+ with io.open(_unicode_encode(eapi_file, |
60 |
encoding=_encodings['fs'], errors='strict'), |
61 |
- mode='r', encoding=_encodings['repo.content'], errors='replace') |
62 |
- lines = f.readlines() |
63 |
+ mode='r', encoding=_encodings['repo.content'], errors='replace') as f: |
64 |
+ lines = f.readlines() |
65 |
if len(lines) == 1: |
66 |
eapi = lines[0].rstrip("\n") |
67 |
else: |
68 |
writemsg(_("--- Invalid 'eapi' file (doesn't contain exactly one line): %s\n") % (eapi_file), |
69 |
noiselevel=-1) |
70 |
- f.close() |
71 |
except IOError: |
72 |
pass |
73 |
|
74 |
@@ -546,14 +545,13 @@ def grablines(myfilename, recursive=0, remember_source_file=False): |
75 |
|
76 |
else: |
77 |
try: |
78 |
- myfile = io.open(_unicode_encode(myfilename, |
79 |
+ with io.open(_unicode_encode(myfilename, |
80 |
encoding=_encodings['fs'], errors='strict'), |
81 |
- mode='r', encoding=_encodings['content'], errors='replace') |
82 |
- if remember_source_file: |
83 |
- mylines = [(line, myfilename) for line in myfile.readlines()] |
84 |
- else: |
85 |
- mylines = myfile.readlines() |
86 |
- myfile.close() |
87 |
+ mode='r', encoding=_encodings['content'], errors='replace') as myfile: |
88 |
+ if remember_source_file: |
89 |
+ mylines = [(line, myfilename) for line in myfile.readlines()] |
90 |
+ else: |
91 |
+ mylines = myfile.readlines() |
92 |
except IOError as e: |
93 |
if e.errno == PermissionDenied.errno: |
94 |
raise PermissionDenied(myfilename) |