modernize some modules' code by using with statement around open()

This commit is contained in:
Giampaolo Rodola' 2013-02-12 02:04:27 +01:00
parent fd6e6cfa29
commit 2f50aaf2ff
14 changed files with 50 additions and 89 deletions

View File

@ -2010,16 +2010,13 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
# replace arguments referencing files with the file content
else:
try:
args_file = open(arg_string[1:])
try:
with open(arg_string[1:]) as args_file:
arg_strings = []
for arg_line in args_file.read().splitlines():
for arg in self.convert_arg_line_to_args(arg_line):
arg_strings.append(arg)
arg_strings = self._read_args_from_files(arg_strings)
new_arg_strings.extend(arg_strings)
finally:
args_file.close()
except OSError:
err = _sys.exc_info()[1]
self.error(str(err))

View File

@ -77,10 +77,9 @@ class Profile(_lsprof.Profiler):
def dump_stats(self, file):
import marshal
f = open(file, 'wb')
with open(file, 'wb') as f:
self.create_stats()
marshal.dump(self.stats, f)
f.close()
def create_stats(self):
self.disable()

View File

@ -1761,11 +1761,8 @@ class FileCookieJar(CookieJar):
if self.filename is not None: filename = self.filename
else: raise ValueError(MISSING_FILENAME_TEXT)
f = open(filename)
try:
with open(filename) as f:
self._really_load(f, filename, ignore_discard, ignore_expires)
finally:
f.close()
def revert(self, filename=None,
ignore_discard=False, ignore_expires=False):
@ -1856,15 +1853,12 @@ class LWPCookieJar(FileCookieJar):
if self.filename is not None: filename = self.filename
else: raise ValueError(MISSING_FILENAME_TEXT)
f = open(filename, "w")
try:
with open(filename, "w") as f:
# There really isn't an LWP Cookies 2.0 format, but this indicates
# that there is extra information in here (domain_dot and
# port_spec) while still being compatible with libwww-perl, I hope.
f.write("#LWP-Cookies-2.0\n")
f.write(self.as_lwp_str(ignore_discard, ignore_expires))
finally:
f.close()
def _really_load(self, f, filename, ignore_discard, ignore_expires):
magic = f.readline()
@ -2055,8 +2049,7 @@ class MozillaCookieJar(FileCookieJar):
if self.filename is not None: filename = self.filename
else: raise ValueError(MISSING_FILENAME_TEXT)
f = open(filename, "w")
try:
with open(filename, "w") as f:
f.write(self.header)
now = time.time()
for cookie in self:
@ -2085,5 +2078,3 @@ class MozillaCookieJar(FileCookieJar):
"\t".join([cookie.domain, initial_dot, cookie.path,
secure, expires, name, value])+
"\n")
finally:
f.close()

View File

@ -85,9 +85,8 @@ def main():
sys.exit(1)
# write the output file
fp = open(optfile, 'w')
with open(optfile, 'w') as fp:
fp.write(''.join(format))
fp.close()
if __name__ == "__main__":
main()

View File

@ -86,15 +86,13 @@ class Grammar(object):
def dump(self, filename):
"""Dump the grammar tables to a pickle file."""
f = open(filename, "wb")
with open(filename, "wb") as f:
pickle.dump(self.__dict__, f, 2)
f.close()
def load(self, filename):
"""Load the grammar tables from a pickle file."""
f = open(filename, "rb")
with open(filename, "rb") as f:
d = pickle.load(f)
f.close()
self.__dict__.update(d)
def copy(self):

View File

@ -366,14 +366,11 @@ class Maildir(Mailbox):
def get_message(self, key):
"""Return a Message representation or raise a KeyError."""
subpath = self._lookup(key)
f = open(os.path.join(self._path, subpath), 'rb')
try:
with open(os.path.join(self._path, subpath), 'rb') as f:
if self._factory:
msg = self._factory(f)
else:
msg = MaildirMessage(f)
finally:
f.close()
subdir, name = os.path.split(subpath)
msg.set_subdir(subdir)
if self.colon in name:
@ -383,11 +380,8 @@ class Maildir(Mailbox):
def get_bytes(self, key):
"""Return a bytes representation or raise a KeyError."""
f = open(os.path.join(self._path, self._lookup(key)), 'rb')
try:
with open(os.path.join(self._path, self._lookup(key)), 'rb') as f:
return f.read().replace(linesep, b'\n')
finally:
f.close()
def get_file(self, key):
"""Return a file-like representation or raise a KeyError."""
@ -1033,7 +1027,7 @@ class MH(Mailbox):
raise KeyError('No message with key: %s' % key)
else:
raise
try:
with f:
if self._locked:
_lock_file(f)
try:
@ -1041,8 +1035,6 @@ class MH(Mailbox):
finally:
if self._locked:
_unlock_file(f)
finally:
f.close()
for name, key_list in self.get_sequences().items():
if key in key_list:
msg.add_sequence(name)
@ -1060,7 +1052,7 @@ class MH(Mailbox):
raise KeyError('No message with key: %s' % key)
else:
raise
try:
with f:
if self._locked:
_lock_file(f)
try:
@ -1068,8 +1060,6 @@ class MH(Mailbox):
finally:
if self._locked:
_unlock_file(f)
finally:
f.close()
def get_file(self, key):
"""Return a file-like representation or raise a KeyError."""

View File

@ -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')
with open(self.filename[:-1], 'r') as f:
self.source = f.read()
f.close()
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:
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!
f.close()
return path

View File

@ -373,10 +373,9 @@ class Profile:
print_stats()
def dump_stats(self, file):
f = open(file, 'wb')
with open(file, 'wb') as f:
self.create_stats()
marshal.dump(self.stats, f)
f.close()
def create_stats(self):
self.simulate_cmd_complete()

View File

@ -93,9 +93,8 @@ class Stats:
self.stats = {}
return
elif isinstance(arg, str):
f = open(arg, 'rb')
with open(arg, 'rb') as f:
self.stats = marshal.load(f)
f.close()
try:
file_stats = os.stat(arg)
arg = time.ctime(file_stats.st_mtime) + " " + arg
@ -149,11 +148,8 @@ class Stats:
def dump_stats(self, filename):
"""Write the profile data to a file we know how to load back."""
f = open(filename, 'wb')
try:
with open(filename, 'wb') as f:
marshal.dump(self.stats, f)
finally:
f.close()
# list the tuple indices and directions for sorting,
# along with some printable description

View File

@ -1426,9 +1426,8 @@ def tempfilepager(text, cmd):
"""Page through text by invoking a program on a temporary file."""
import tempfile
filename = tempfile.mktemp()
file = open(filename, 'w')
with open(filename, 'w') as file:
file.write(text)
file.close()
try:
os.system(cmd + ' "' + filename + '"')
finally:

View File

@ -384,9 +384,8 @@ class _Printer(object):
for filename in self.__files:
filename = os.path.join(dir, filename)
try:
fp = open(filename, "r")
with open(filename, "r") as fp:
data = fp.read()
fp.close()
break
except OSError:
pass

View File

@ -235,7 +235,8 @@ class Symbol(object):
if __name__ == "__main__":
import os, sys
src = open(sys.argv[0]).read()
with open(sys.argv[0]) as f:
src = f.read()
mod = symtable(src, os.path.split(sys.argv[0])[1], "exec")
for ident in mod.get_identifiers():
info = mod.lookup(ident)

View File

@ -3843,7 +3843,7 @@ def write_docstringdict(filename="turtle_docstringdict"):
key = "Turtle."+methodname
docsdict[key] = eval(key).__doc__
f = open("%s.py" % filename,"w")
with open("%s.py" % filename,"w") as f:
keys = sorted([x for x in docsdict.keys()
if x.split('.')[1] not in _alias_list])
f.write('docsdict = {\n\n')

View File

@ -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