mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +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
|
@ -905,11 +905,8 @@ def parse(file, namespaces=True):
|
|||
builder = ExpatBuilder()
|
||||
|
||||
if isinstance(file, str):
|
||||
fp = open(file, 'rb')
|
||||
try:
|
||||
with open(file, 'rb') as fp:
|
||||
result = builder.parseFile(fp)
|
||||
finally:
|
||||
fp.close()
|
||||
else:
|
||||
result = builder.parseFile(file)
|
||||
return result
|
||||
|
@ -939,11 +936,8 @@ def parseFragment(file, context, namespaces=True):
|
|||
builder = FragmentBuilder(context)
|
||||
|
||||
if isinstance(file, str):
|
||||
fp = open(file, 'rb')
|
||||
try:
|
||||
with open(file, 'rb') as fp:
|
||||
result = builder.parseFile(fp)
|
||||
finally:
|
||||
fp.close()
|
||||
else:
|
||||
result = builder.parseFile(file)
|
||||
return result
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue