summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Lib/binhex.py28
-rw-r--r--Lib/cgitb.py5
-rw-r--r--Lib/dbm/__init__.py6
-rw-r--r--Lib/http/cookiejar.py1
-rwxr-xr-xLib/platform.py65
-rwxr-xr-xLib/pydoc.py5
-rw-r--r--Lib/sre_constants.py37
-rw-r--r--Lib/token.py12
-rwxr-xr-xLib/trace.py40
-rw-r--r--Lib/xmlrpc/client.py28
10 files changed, 107 insertions, 120 deletions
diff --git a/Lib/binhex.py b/Lib/binhex.py
index 7bf9278..14badb7 100644
--- a/Lib/binhex.py
+++ b/Lib/binhex.py
@@ -229,14 +229,13 @@ def binhex(inp, out):
finfo = getfileinfo(inp)
ofp = BinHex(finfo, out)
- ifp = io.open(inp, 'rb')
- # XXXX Do textfile translation on non-mac systems
- while True:
- d = ifp.read(128000)
- if not d: break
- ofp.write(d)
- ofp.close_data()
- ifp.close()
+ with io.open(inp, 'rb') as ifp:
+ # XXXX Do textfile translation on non-mac systems
+ while True:
+ d = ifp.read(128000)
+ if not d: break
+ ofp.write(d)
+ ofp.close_data()
ifp = openrsrc(inp, 'rb')
while True:
@@ -449,13 +448,12 @@ def hexbin(inp, out):
if not out:
out = ifp.FName
- ofp = io.open(out, 'wb')
- # XXXX Do translation on non-mac systems
- while True:
- d = ifp.read(128000)
- if not d: break
- ofp.write(d)
- ofp.close()
+ with io.open(out, 'wb') as ofp:
+ # XXXX Do translation on non-mac systems
+ while True:
+ d = ifp.read(128000)
+ if not d: break
+ ofp.write(d)
ifp.close_data()
d = ifp.read_rsrc(128000)
diff --git a/Lib/cgitb.py b/Lib/cgitb.py
index 6eb52e7..b291100 100644
--- a/Lib/cgitb.py
+++ b/Lib/cgitb.py
@@ -294,9 +294,8 @@ class Hook:
(fd, path) = tempfile.mkstemp(suffix=suffix, dir=self.logdir)
try:
- file = os.fdopen(fd, 'w')
- file.write(doc)
- file.close()
+ with os.fdopen(fd, 'w') as file:
+ file.write(doc)
msg = '%s contains the description of this error.' % path
except:
msg = 'Tried to save traceback to %s, but failed.' % path
diff --git a/Lib/dbm/__init__.py b/Lib/dbm/__init__.py
index 5f4664a..6831a84 100644
--- a/Lib/dbm/__init__.py
+++ b/Lib/dbm/__init__.py
@@ -153,9 +153,9 @@ def whichdb(filename):
except OSError:
return None
- # Read the start of the file -- the magic number
- s16 = f.read(16)
- f.close()
+ with f:
+ # Read the start of the file -- the magic number
+ s16 = f.read(16)
s = s16[0:4]
# Return "" if not at least 4 bytes
diff --git a/Lib/http/cookiejar.py b/Lib/http/cookiejar.py
index cc9e0be..d54f58a 100644
--- a/Lib/http/cookiejar.py
+++ b/Lib/http/cookiejar.py
@@ -1999,7 +1999,6 @@ class MozillaCookieJar(FileCookieJar):
magic = f.readline()
if not self.magic_re.search(magic):
- f.close()
raise LoadError(
"%r does not look like a Netscape format cookies file" %
filename)
diff --git a/Lib/platform.py b/Lib/platform.py
index c4ffe95..b1c659e 100755
--- a/Lib/platform.py
+++ b/Lib/platform.py
@@ -163,40 +163,39 @@ def libc_ver(executable=sys.executable, lib='', version='',
# here to work around problems with Cygwin not being
# able to open symlinks for reading
executable = os.path.realpath(executable)
- f = open(executable, 'rb')
- binary = f.read(chunksize)
- pos = 0
- while 1:
- if b'libc' in binary or b'GLIBC' in binary:
- m = _libc_search.search(binary, pos)
- else:
- m = None
- if not m:
- binary = f.read(chunksize)
- if not binary:
- break
- pos = 0
- continue
- libcinit, glibc, glibcversion, so, threads, soversion = [
- s.decode('latin1') if s is not None else s
- for s in m.groups()]
- if libcinit and not lib:
- lib = 'libc'
- elif glibc:
- if lib != 'glibc':
- lib = 'glibc'
- version = glibcversion
- elif glibcversion > version:
- version = glibcversion
- elif so:
- if lib != 'glibc':
+ with open(executable, 'rb') as f:
+ binary = f.read(chunksize)
+ pos = 0
+ while 1:
+ if b'libc' in binary or b'GLIBC' in binary:
+ m = _libc_search.search(binary, pos)
+ else:
+ m = None
+ if not m:
+ binary = f.read(chunksize)
+ if not binary:
+ break
+ pos = 0
+ continue
+ libcinit, glibc, glibcversion, so, threads, soversion = [
+ s.decode('latin1') if s is not None else s
+ for s in m.groups()]
+ if libcinit and not lib:
lib = 'libc'
- if soversion and soversion > version:
- version = soversion
- if threads and version[-len(threads):] != threads:
- version = version + threads
- pos = m.end()
- f.close()
+ elif glibc:
+ if lib != 'glibc':
+ lib = 'glibc'
+ version = glibcversion
+ elif glibcversion > version:
+ version = glibcversion
+ elif so:
+ if lib != 'glibc':
+ lib = 'libc'
+ if soversion and soversion > version:
+ version = soversion
+ if threads and version[-len(threads):] != threads:
+ version = version + threads
+ pos = m.end()
return lib, version
def _dist_try_harder(distname, version, id):
diff --git a/Lib/pydoc.py b/Lib/pydoc.py
index 9f3401f..d77ed00 100755
--- a/Lib/pydoc.py
+++ b/Lib/pydoc.py
@@ -1639,9 +1639,8 @@ def writedoc(thing, forceload=0):
try:
object, name = resolve(thing, forceload)
page = html.page(describe(object), html.document(object, name))
- file = open(name + '.html', 'w', encoding='utf-8')
- file.write(page)
- file.close()
+ with open(name + '.html', 'w', encoding='utf-8') as file:
+ file.write(page)
print('wrote', name + '.html')
except (ImportError, ErrorDuringImport) as value:
print(value)
diff --git a/Lib/sre_constants.py b/Lib/sre_constants.py
index 8b6bbfa..fc684ae 100644
--- a/Lib/sre_constants.py
+++ b/Lib/sre_constants.py
@@ -182,8 +182,8 @@ if __name__ == "__main__":
items = sorted(d)
for item in items:
f.write("#define %s_%s %d\n" % (prefix, item, item))
- f = open("sre_constants.h", "w")
- f.write("""\
+ with open("sre_constants.h", "w") as f:
+ f.write("""\
/*
* Secret Labs' Regular Expression Engine
*
@@ -199,25 +199,24 @@ if __name__ == "__main__":
""")
- f.write("#define SRE_MAGIC %d\n" % MAGIC)
+ f.write("#define SRE_MAGIC %d\n" % MAGIC)
- dump(f, OPCODES, "SRE_OP")
- dump(f, ATCODES, "SRE")
- dump(f, CHCODES, "SRE")
+ dump(f, OPCODES, "SRE_OP")
+ dump(f, ATCODES, "SRE")
+ dump(f, CHCODES, "SRE")
- f.write("#define SRE_FLAG_TEMPLATE %d\n" % SRE_FLAG_TEMPLATE)
- f.write("#define SRE_FLAG_IGNORECASE %d\n" % SRE_FLAG_IGNORECASE)
- f.write("#define SRE_FLAG_LOCALE %d\n" % SRE_FLAG_LOCALE)
- f.write("#define SRE_FLAG_MULTILINE %d\n" % SRE_FLAG_MULTILINE)
- f.write("#define SRE_FLAG_DOTALL %d\n" % SRE_FLAG_DOTALL)
- f.write("#define SRE_FLAG_UNICODE %d\n" % SRE_FLAG_UNICODE)
- f.write("#define SRE_FLAG_VERBOSE %d\n" % SRE_FLAG_VERBOSE)
- f.write("#define SRE_FLAG_DEBUG %d\n" % SRE_FLAG_DEBUG)
- f.write("#define SRE_FLAG_ASCII %d\n" % SRE_FLAG_ASCII)
+ f.write("#define SRE_FLAG_TEMPLATE %d\n" % SRE_FLAG_TEMPLATE)
+ f.write("#define SRE_FLAG_IGNORECASE %d\n" % SRE_FLAG_IGNORECASE)
+ f.write("#define SRE_FLAG_LOCALE %d\n" % SRE_FLAG_LOCALE)
+ f.write("#define SRE_FLAG_MULTILINE %d\n" % SRE_FLAG_MULTILINE)
+ f.write("#define SRE_FLAG_DOTALL %d\n" % SRE_FLAG_DOTALL)
+ f.write("#define SRE_FLAG_UNICODE %d\n" % SRE_FLAG_UNICODE)
+ f.write("#define SRE_FLAG_VERBOSE %d\n" % SRE_FLAG_VERBOSE)
+ f.write("#define SRE_FLAG_DEBUG %d\n" % SRE_FLAG_DEBUG)
+ f.write("#define SRE_FLAG_ASCII %d\n" % SRE_FLAG_ASCII)
- f.write("#define SRE_INFO_PREFIX %d\n" % SRE_INFO_PREFIX)
- f.write("#define SRE_INFO_LITERAL %d\n" % SRE_INFO_LITERAL)
- f.write("#define SRE_INFO_CHARSET %d\n" % SRE_INFO_CHARSET)
+ f.write("#define SRE_INFO_PREFIX %d\n" % SRE_INFO_PREFIX)
+ f.write("#define SRE_INFO_LITERAL %d\n" % SRE_INFO_LITERAL)
+ f.write("#define SRE_INFO_CHARSET %d\n" % SRE_INFO_CHARSET)
- f.close()
print("done")
diff --git a/Lib/token.py b/Lib/token.py
index bdfcec8..a95d9b7 100644
--- a/Lib/token.py
+++ b/Lib/token.py
@@ -97,8 +97,8 @@ def _main():
except OSError as err:
sys.stdout.write("I/O error: %s\n" % str(err))
sys.exit(1)
- lines = fp.read().split("\n")
- fp.close()
+ with fp:
+ lines = fp.read().split("\n")
prog = re.compile(
"#define[ \t][ \t]*([A-Z0-9][A-Z0-9_]*)[ \t][ \t]*([0-9][0-9]*)",
re.IGNORECASE)
@@ -116,8 +116,8 @@ def _main():
except OSError as err:
sys.stderr.write("I/O error: %s\n" % str(err))
sys.exit(2)
- format = fp.read().split("\n")
- fp.close()
+ with fp:
+ format = fp.read().split("\n")
try:
start = format.index("#--start constants--") + 1
end = format.index("#--end constants--")
@@ -133,8 +133,8 @@ def _main():
except OSError as err:
sys.stderr.write("I/O error: %s\n" % str(err))
sys.exit(4)
- fp.write("\n".join(format))
- fp.close()
+ with fp:
+ fp.write("\n".join(format))
if __name__ == "__main__":
diff --git a/Lib/trace.py b/Lib/trace.py
index fe84973..41eff26 100755
--- a/Lib/trace.py
+++ b/Lib/trace.py
@@ -232,8 +232,8 @@ class CoverageResults:
if self.infile:
# Try to merge existing counts file.
try:
- counts, calledfuncs, callers = \
- pickle.load(open(self.infile, 'rb'))
+ with open(self.infile, 'rb') as f:
+ counts, calledfuncs, callers = pickle.load(f)
self.update(self.__class__(counts, calledfuncs, callers))
except (OSError, EOFError, ValueError) as err:
print(("Skipping counts file %r: %s"
@@ -361,26 +361,26 @@ class CoverageResults:
n_lines = 0
n_hits = 0
- for lineno, line in enumerate(lines, 1):
- # do the blank/comment match to try to mark more lines
- # (help the reader find stuff that hasn't been covered)
- if lineno in lines_hit:
- outfile.write("%5d: " % lines_hit[lineno])
- n_hits += 1
- n_lines += 1
- elif rx_blank.match(line):
- outfile.write(" ")
- else:
- # lines preceded by no marks weren't hit
- # Highlight them if so indicated, unless the line contains
- # #pragma: NO COVER
- if lineno in lnotab and not PRAGMA_NOCOVER in line:
- outfile.write(">>>>>> ")
+ with outfile:
+ for lineno, line in enumerate(lines, 1):
+ # do the blank/comment match to try to mark more lines
+ # (help the reader find stuff that hasn't been covered)
+ if lineno in lines_hit:
+ outfile.write("%5d: " % lines_hit[lineno])
+ n_hits += 1
n_lines += 1
- else:
+ elif rx_blank.match(line):
outfile.write(" ")
- outfile.write(line.expandtabs(8))
- outfile.close()
+ else:
+ # lines preceded by no marks weren't hit
+ # Highlight them if so indicated, unless the line contains
+ # #pragma: NO COVER
+ if lineno in lnotab and not PRAGMA_NOCOVER in line:
+ outfile.write(">>>>>> ")
+ n_lines += 1
+ else:
+ outfile.write(" ")
+ outfile.write(line.expandtabs(8))
return n_hits, n_lines
diff --git a/Lib/xmlrpc/client.py b/Lib/xmlrpc/client.py
index 047929a..34208d1 100644
--- a/Lib/xmlrpc/client.py
+++ b/Lib/xmlrpc/client.py
@@ -1010,12 +1010,9 @@ def gzip_encode(data):
if not gzip:
raise NotImplementedError
f = BytesIO()
- gzf = gzip.GzipFile(mode="wb", fileobj=f, compresslevel=1)
- gzf.write(data)
- gzf.close()
- encoded = f.getvalue()
- f.close()
- return encoded
+ with gzip.GzipFile(mode="wb", fileobj=f, compresslevel=1) as gzf:
+ gzf.write(data)
+ return f.getvalue()
##
# Decode a string using the gzip content encoding such as specified by the
@@ -1036,17 +1033,14 @@ def gzip_decode(data, max_decode=20971520):
"""
if not gzip:
raise NotImplementedError
- f = BytesIO(data)
- gzf = gzip.GzipFile(mode="rb", fileobj=f)
- try:
- if max_decode < 0: # no limit
- decoded = gzf.read()
- else:
- decoded = gzf.read(max_decode + 1)
- except OSError:
- raise ValueError("invalid data")
- f.close()
- gzf.close()
+ with gzip.GzipFile(mode="rb", fileobj=BytesIO(data)) as gzf:
+ try:
+ if max_decode < 0: # no limit
+ decoded = gzf.read()
+ else:
+ decoded = gzf.read(max_decode + 1)
+ except OSError:
+ raise ValueError("invalid data")
if max_decode >= 0 and len(decoded) > max_decode:
raise ValueError("max gzipped payload length exceeded")
return decoded