mirror of
https://github.com/python/cpython.git
synced 2025-09-09 18:32:22 +00:00
modernize some modules' code by using with statement around open()
This commit is contained in:
parent
fd6e6cfa29
commit
2f50aaf2ff
14 changed files with 50 additions and 89 deletions
|
@ -349,9 +349,8 @@ class ImpLoader:
|
|||
self.file.close()
|
||||
elif mod_type==imp.PY_COMPILED:
|
||||
if os.path.exists(self.filename[:-1]):
|
||||
f = open(self.filename[:-1], 'r')
|
||||
self.source = f.read()
|
||||
f.close()
|
||||
with open(self.filename[:-1], 'r') as f:
|
||||
self.source = f.read()
|
||||
elif mod_type==imp.PKG_DIRECTORY:
|
||||
self.source = self._get_delegate().get_source()
|
||||
return self.source
|
||||
|
@ -591,12 +590,12 @@ def extend_path(path, name):
|
|||
sys.stderr.write("Can't open %s: %s\n" %
|
||||
(pkgfile, msg))
|
||||
else:
|
||||
for line in f:
|
||||
line = line.rstrip('\n')
|
||||
if not line or line.startswith('#'):
|
||||
continue
|
||||
path.append(line) # Don't check for existence!
|
||||
f.close()
|
||||
with f:
|
||||
for line in f:
|
||||
line = line.rstrip('\n')
|
||||
if not line or line.startswith('#'):
|
||||
continue
|
||||
path.append(line) # Don't check for existence!
|
||||
|
||||
return path
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue