Skip to content

Commit 7dde792

Browse files
committed
Use a context manager for some file objects.
1 parent 24e561a commit 7dde792

4 files changed

Lines changed: 20 additions & 32 deletions

File tree

Lib/keyword.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,19 @@ def main():
6161
else: optfile = "Lib/keyword.py"
6262

6363
# scan the source file for keywords
64-
fp = open(iptfile)
65-
strprog = re.compile('"([^"]+)"')
66-
lines = []
67-
for line in fp:
68-
if '{1, "' in line:
69-
match = strprog.search(line)
70-
if match:
71-
lines.append(" '" + match.group(1) + "',\n")
72-
fp.close()
64+
with open(iptfile) as fp:
65+
strprog = re.compile('"([^"]+)"')
66+
lines = []
67+
for line in fp:
68+
if '{1, "' in line:
69+
match = strprog.search(line)
70+
if match:
71+
lines.append(" '" + match.group(1) + "',\n")
7372
lines.sort()
7473

7574
# load the output skeleton from the target
76-
fp = open(optfile)
77-
format = fp.readlines()
78-
fp.close()
75+
with open(optfile) as fp:
76+
format = fp.readlines()
7977

8078
# insert the lines of keywords
8179
try:

Lib/pdb.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -155,21 +155,15 @@ def __init__(self, completekey='tab', stdin=None, stdout=None, skip=None):
155155
if 'HOME' in os.environ:
156156
envHome = os.environ['HOME']
157157
try:
158-
rcFile = open(os.path.join(envHome, ".pdbrc"))
158+
with open(os.path.join(envHome, ".pdbrc")) as rcFile:
159+
self.rcLines.extend(rcFile)
159160
except IOError:
160161
pass
161-
else:
162-
for line in rcFile.readlines():
163-
self.rcLines.append(line)
164-
rcFile.close()
165162
try:
166-
rcFile = open(".pdbrc")
163+
with open(".pdbrc") as rcFile:
164+
self.rcLines.extend(rcFile)
167165
except IOError:
168166
pass
169-
else:
170-
for line in rcFile.readlines():
171-
self.rcLines.append(line)
172-
rcFile.close()
173167

174168
self.commands = {} # associates a command list to breakpoint numbers
175169
self.commands_doprompt = {} # for each bp num, tells if the prompt

Lib/platform.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,8 @@ def _dist_try_harder(distname,version,id):
200200
"""
201201
if os.path.exists('/var/adm/inst-log/info'):
202202
# SuSE Linux stores distribution information in that file
203-
info = open('/var/adm/inst-log/info').readlines()
204203
distname = 'SuSE'
205-
for line in info:
204+
for line in open('/var/adm/inst-log/info'):
206205
tv = line.split()
207206
if len(tv) == 2:
208207
tag,value = tv
@@ -217,8 +216,7 @@ def _dist_try_harder(distname,version,id):
217216

218217
if os.path.exists('/etc/.installed'):
219218
# Caldera OpenLinux has some infos in that file (thanks to Colin Kong)
220-
info = open('/etc/.installed').readlines()
221-
for line in info:
219+
for line in open('/etc/.installed'):
222220
pkg = line.split('-')
223221
if len(pkg) >= 2 and pkg[0] == 'OpenLinux':
224222
# XXX does Caldera support non Intel platforms ? If yes,
@@ -327,9 +325,8 @@ def linux_distribution(distname='', version='', id='',
327325
return _dist_try_harder(distname,version,id)
328326

329327
# Read the first line
330-
f = open('/etc/'+file, 'r')
331-
firstline = f.readline()
332-
f.close()
328+
with open('/etc/'+file, 'r') as f:
329+
firstline = f.readline()
333330
_distname, _version, _id = _parse_release_file(firstline)
334331

335332
if _distname and full_distribution_name:

Lib/turtle.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,8 @@
169169

170170
def config_dict(filename):
171171
"""Convert content of config-file into dictionary."""
172-
f = open(filename, "r")
173-
cfglines = f.readlines()
174-
f.close()
172+
with open(filename, "r") as f:
173+
cfglines = f.readlines()
175174
cfgdict = {}
176175
for line in cfglines:
177176
line = line.strip()

0 commit comments

Comments
 (0)