diff -r fc7dbba57869 Doc/includes/email-mime.py --- a/Doc/includes/email-mime.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Doc/includes/email-mime.py Sun Nov 09 21:03:10 2014 +0200 @@ -20,9 +20,8 @@ msg.preamble = 'Our family reunion' for file in pngfiles: # Open the files in binary mode. Let the MIMEImage class automatically # guess the specific image type. - fp = open(file, 'rb') - img = MIMEImage(fp.read()) - fp.close() + with open(file, 'rb') as fp: + img = MIMEImage(fp.read()) msg.attach(img) # Send the email via our own SMTP server. diff -r fc7dbba57869 Doc/includes/email-simple.py --- a/Doc/includes/email-simple.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Doc/includes/email-simple.py Sun Nov 09 21:03:10 2014 +0200 @@ -6,10 +6,9 @@ from email.mime.text import MIMEText # Open a plain text file for reading. For this example, assume that # the text file contains only ASCII characters. -fp = open(textfile, 'rb') -# Create a text/plain message -msg = MIMEText(fp.read()) -fp.close() +with open(textfile, 'rb') as fp: + # Create a text/plain message + msg = MIMEText(fp.read()) # me == the sender's email address # you == the recipient's email address diff -r fc7dbba57869 Lib/binhex.py --- a/Lib/binhex.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/binhex.py Sun Nov 09 21:03:10 2014 +0200 @@ -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 -r fc7dbba57869 Lib/cgitb.py --- a/Lib/cgitb.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/cgitb.py Sun Nov 09 21:03:10 2014 +0200 @@ -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 -r fc7dbba57869 Lib/dbm/__init__.py --- a/Lib/dbm/__init__.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/dbm/__init__.py Sun Nov 09 21:03:10 2014 +0200 @@ -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 -r fc7dbba57869 Lib/distutils/archive_util.py --- a/Lib/distutils/archive_util.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/distutils/archive_util.py Sun Nov 09 21:03:10 2014 +0200 @@ -164,13 +164,13 @@ def make_zipfile(base_name, base_dir, ve zip = zipfile.ZipFile(zip_filename, "w", compression=zipfile.ZIP_STORED) - for dirpath, dirnames, filenames in os.walk(base_dir): - for name in filenames: - path = os.path.normpath(os.path.join(dirpath, name)) - if os.path.isfile(path): - zip.write(path, path) - log.info("adding '%s'" % path) - zip.close() + with zip: + for dirpath, dirnames, filenames in os.walk(base_dir): + for name in filenames: + path = os.path.normpath(os.path.join(dirpath, name)) + if os.path.isfile(path): + zip.write(path, path) + log.info("adding '%s'" % path) return zip_filename diff -r fc7dbba57869 Lib/distutils/command/bdist_msi.py --- a/Lib/distutils/command/bdist_msi.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/distutils/command/bdist_msi.py Sun Nov 09 21:03:10 2014 +0200 @@ -390,18 +390,18 @@ class bdist_msi(Command): # entries for each version as the above code does if self.pre_install_script: scriptfn = os.path.join(self.bdist_dir, "preinstall.bat") - f = open(scriptfn, "w") - # The batch file will be executed with [PYTHON], so that %1 - # is the path to the Python interpreter; %0 will be the path - # of the batch file. - # rem =""" - # %1 %0 - # exit - # """ - # - f.write('rem ="""\n%1 %0\nexit\n"""\n') - f.write(open(self.pre_install_script).read()) - f.close() + with open(scriptfn, "w") as f: + # The batch file will be executed with [PYTHON], so that %1 + # is the path to the Python interpreter; %0 will be the path + # of the batch file. + # rem =""" + # %1 %0 + # exit + # """ + # + f.write('rem ="""\n%1 %0\nexit\n"""\n') + with open(self.pre_install_script) as fin: + f.write(fin.read()) add_data(self.db, "Binary", [("PreInstall", msilib.Binary(scriptfn)) ]) diff -r fc7dbba57869 Lib/distutils/command/config.py --- a/Lib/distutils/command/config.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/distutils/command/config.py Sun Nov 09 21:03:10 2014 +0200 @@ -106,15 +106,14 @@ class config(Command): def _gen_temp_sourcefile(self, body, headers, lang): filename = "_configtest" + LANG_EXT[lang] - file = open(filename, "w") - if headers: - for header in headers: - file.write("#include <%s>\n" % header) - file.write("\n") - file.write(body) - if body[-1] != "\n": - file.write("\n") - file.close() + with open(filename, "w") as file: + if headers: + for header in headers: + file.write("#include <%s>\n" % header) + file.write("\n") + file.write(body) + if body[-1] != "\n": + file.write("\n") return filename def _preprocess(self, body, headers, include_dirs, lang): @@ -203,17 +202,16 @@ class config(Command): if isinstance(pattern, str): pattern = re.compile(pattern) - file = open(out) - match = False - while True: - line = file.readline() - if line == '': - break - if pattern.search(line): - match = True - break + with open(out) as file: + match = False + while True: + line = file.readline() + if line == '': + break + if pattern.search(line): + match = True + break - file.close() self._clean() return match diff -r fc7dbba57869 Lib/distutils/command/sdist.py --- a/Lib/distutils/command/sdist.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/distutils/command/sdist.py Sun Nov 09 21:03:10 2014 +0200 @@ -379,14 +379,13 @@ class sdist(Command): distribution. """ log.info("reading manifest file '%s'", self.manifest) - manifest = open(self.manifest) - for line in manifest: - # ignore comments and blank lines - line = line.strip() - if line.startswith('#') or not line: - continue - self.filelist.append(line) - manifest.close() + with open(self.manifest) as manifest: + for line in manifest: + # ignore comments and blank lines + line = line.strip() + if line.startswith('#') or not line: + continue + self.filelist.append(line) def make_release_tree(self, base_dir, files): """Create the directory tree that will become the source diff -r fc7dbba57869 Lib/distutils/util.py --- a/Lib/distutils/util.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/distutils/util.py Sun Nov 09 21:03:10 2014 +0200 @@ -383,35 +383,34 @@ def byte_compile (py_files, else: script = open(script_name, "w") - script.write("""\ + with script: + script.write("""\ from distutils.util import byte_compile files = [ """) - # XXX would be nice to write absolute filenames, just for - # safety's sake (script should be more robust in the face of - # chdir'ing before running it). But this requires abspath'ing - # 'prefix' as well, and that breaks the hack in build_lib's - # 'byte_compile()' method that carefully tacks on a trailing - # slash (os.sep really) to make sure the prefix here is "just - # right". This whole prefix business is rather delicate -- the - # problem is that it's really a directory, but I'm treating it - # as a dumb string, so trailing slashes and so forth matter. + # XXX would be nice to write absolute filenames, just for + # safety's sake (script should be more robust in the face of + # chdir'ing before running it). But this requires abspath'ing + # 'prefix' as well, and that breaks the hack in build_lib's + # 'byte_compile()' method that carefully tacks on a trailing + # slash (os.sep really) to make sure the prefix here is "just + # right". This whole prefix business is rather delicate -- the + # problem is that it's really a directory, but I'm treating it + # as a dumb string, so trailing slashes and so forth matter. - #py_files = map(os.path.abspath, py_files) - #if prefix: - # prefix = os.path.abspath(prefix) + #py_files = map(os.path.abspath, py_files) + #if prefix: + # prefix = os.path.abspath(prefix) - script.write(",\n".join(map(repr, py_files)) + "]\n") - script.write(""" + script.write(",\n".join(map(repr, py_files)) + "]\n") + script.write(""" byte_compile(files, optimize=%r, force=%r, prefix=%r, base_dir=%r, verbose=%r, dry_run=0, direct=1) """ % (optimize, force, prefix, base_dir, verbose)) - script.close() - cmd = [sys.executable, script_name] if optimize == 1: cmd.insert(1, "-O") diff -r fc7dbba57869 Lib/http/cookiejar.py --- a/Lib/http/cookiejar.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/http/cookiejar.py Sun Nov 09 21:03:10 2014 +0200 @@ -1983,7 +1983,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 -r fc7dbba57869 Lib/platform.py --- a/Lib/platform.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/platform.py Sun Nov 09 21:03:10 2014 +0200 @@ -163,40 +163,39 @@ def libc_ver(executable=sys.executable, # 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 -r fc7dbba57869 Lib/pydoc.py --- a/Lib/pydoc.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/pydoc.py Sun Nov 09 21:03:10 2014 +0200 @@ -1614,9 +1614,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 -r fc7dbba57869 Lib/sre_constants.py --- a/Lib/sre_constants.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/sre_constants.py Sun Nov 09 21:03:10 2014 +0200 @@ -166,8 +166,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 * @@ -183,25 +183,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 -r fc7dbba57869 Lib/test/script_helper.py --- a/Lib/test/script_helper.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/test/script_helper.py Sun Nov 09 21:03:10 2014 +0200 @@ -115,31 +115,28 @@ def make_script(script_dir, script_basen script_filename += os.extsep + 'py' script_name = os.path.join(script_dir, script_filename) # The script should be encoded to UTF-8, the default string encoding - script_file = open(script_name, 'w', encoding='utf-8') - script_file.write(source) - script_file.close() + with open(script_name, 'w', encoding='utf-8') as script_file: + script_file.write(source) importlib.invalidate_caches() return script_name def make_zip_script(zip_dir, zip_basename, script_name, name_in_zip=None): zip_filename = zip_basename+os.extsep+'zip' zip_name = os.path.join(zip_dir, zip_filename) - zip_file = zipfile.ZipFile(zip_name, 'w') - if name_in_zip is None: - parts = script_name.split(os.sep) - if len(parts) >= 2 and parts[-2] == '__pycache__': - legacy_pyc = make_legacy_pyc(source_from_cache(script_name)) - name_in_zip = os.path.basename(legacy_pyc) - script_name = legacy_pyc - else: - name_in_zip = os.path.basename(script_name) - zip_file.write(script_name, name_in_zip) - zip_file.close() + with zipfile.ZipFile(zip_name, 'w') as zip_file: + if name_in_zip is None: + parts = script_name.split(os.sep) + if len(parts) >= 2 and parts[-2] == '__pycache__': + legacy_pyc = make_legacy_pyc(source_from_cache(script_name)) + name_in_zip = os.path.basename(legacy_pyc) + script_name = legacy_pyc + else: + name_in_zip = os.path.basename(script_name) + zip_file.write(script_name, name_in_zip) #if test.support.verbose: - # zip_file = zipfile.ZipFile(zip_name, 'r') - # print 'Contents of %r:' % zip_name - # zip_file.printdir() - # zip_file.close() + # with zipfile.ZipFile(zip_name, 'r') as zip_file: + # print 'Contents of %r:' % zip_name + # zip_file.printdir() return zip_name, os.path.join(zip_name, name_in_zip) def make_pkg(pkg_dir, init_source=''): @@ -162,17 +159,15 @@ def make_zip_pkg(zip_dir, zip_basename, script_name_in_zip = os.path.join(pkg_names[-1], os.path.basename(script_name)) zip_filename = zip_basename+os.extsep+'zip' zip_name = os.path.join(zip_dir, zip_filename) - zip_file = zipfile.ZipFile(zip_name, 'w') - for name in pkg_names: - init_name_in_zip = os.path.join(name, init_basename) - zip_file.write(init_name, init_name_in_zip) - zip_file.write(script_name, script_name_in_zip) - zip_file.close() + with zipfile.ZipFile(zip_name, 'w') as zip_file: + for name in pkg_names: + init_name_in_zip = os.path.join(name, init_basename) + zip_file.write(init_name, init_name_in_zip) + zip_file.write(script_name, script_name_in_zip) for name in unlink: os.unlink(name) #if test.support.verbose: - # zip_file = zipfile.ZipFile(zip_name, 'r') - # print 'Contents of %r:' % zip_name - # zip_file.printdir() - # zip_file.close() + # with zipfile.ZipFile(zip_name, 'r') as zip_file: + # print 'Contents of %r:' % zip_name + # zip_file.printdir() return zip_name, os.path.join(zip_name, script_name_in_zip) diff -r fc7dbba57869 Lib/test/support/__init__.py --- a/Lib/test/support/__init__.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/test/support/__init__.py Sun Nov 09 21:03:10 2014 +0200 @@ -631,9 +631,8 @@ def find_unused_port(family=socket.AF_IN issue if/when we come across it. """ - tempsock = socket.socket(family, socktype) - port = bind_port(tempsock) - tempsock.close() + with socket.socket(family, socktype) as tempsock: + port = bind_port(tempsock) del tempsock return port @@ -1575,10 +1574,11 @@ class _MemoryWatchdog: sys.stderr.flush() return - watchdog_script = findfile("memory_watchdog.py") - self.mem_watchdog = subprocess.Popen([sys.executable, watchdog_script], - stdin=f, stderr=subprocess.DEVNULL) - f.close() + with f: + watchdog_script = findfile("memory_watchdog.py") + self.mem_watchdog = subprocess.Popen([sys.executable, watchdog_script], + stdin=f, + stderr=subprocess.DEVNULL) self.started = True def stop(self): diff -r fc7dbba57869 Lib/test/test_argparse.py --- a/Lib/test/test_argparse.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/test/test_argparse.py Sun Nov 09 21:03:10 2014 +0200 @@ -1349,9 +1349,8 @@ class TestArgumentsFromFile(TempDirMixin ('invalid', '@no-such-path\n'), ] for path, text in file_texts: - file = open(path, 'w') - file.write(text) - file.close() + with open(path, 'w') as file: + file.write(text) parser_signature = Sig(fromfile_prefix_chars='@') argument_signatures = [ @@ -1380,9 +1379,8 @@ class TestArgumentsFromFileConverter(Tem ('hello', 'hello world!\n'), ] for path, text in file_texts: - file = open(path, 'w') - file.write(text) - file.close() + with open(path, 'w') as file: + file.write(text) class FromFileConverterArgumentParser(ErrorRaisingArgumentParser): @@ -1453,9 +1451,8 @@ class TestFileTypeR(TempDirMixin, Parser def setUp(self): super(TestFileTypeR, self).setUp() for file_name in ['foo', 'bar']: - file = open(os.path.join(self.temp_dir, file_name), 'w') - file.write(file_name) - file.close() + with open(os.path.join(self.temp_dir, file_name), 'w') as file: + file.write(file_name) self.create_readonly_file('readonly') argument_signatures = [ @@ -1494,9 +1491,8 @@ class TestFileTypeRB(TempDirMixin, Parse def setUp(self): super(TestFileTypeRB, self).setUp() for file_name in ['foo', 'bar']: - file = open(os.path.join(self.temp_dir, file_name), 'w') - file.write(file_name) - file.close() + with open(os.path.join(self.temp_dir, file_name), 'w') as file: + file.write(file_name) argument_signatures = [ Sig('-x', type=argparse.FileType('rb')), diff -r fc7dbba57869 Lib/test/test_binhex.py --- a/Lib/test/test_binhex.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/test/test_binhex.py Sun Nov 09 21:03:10 2014 +0200 @@ -24,17 +24,15 @@ class BinHexTestCase(unittest.TestCase): DATA = b'Jack is my hero' def test_binhex(self): - f = open(self.fname1, 'wb') - f.write(self.DATA) - f.close() + with open(self.fname1, 'wb') as f: + f.write(self.DATA) binhex.binhex(self.fname1, self.fname2) binhex.hexbin(self.fname2, self.fname1) - f = open(self.fname1, 'rb') - finish = f.readline() - f.close() + with open(self.fname1, 'rb') as f: + finish = f.readline() self.assertEqual(self.DATA, finish) diff -r fc7dbba57869 Lib/test/test_bool.py --- a/Lib/test/test_bool.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/test/test_bool.py Sun Nov 09 21:03:10 2014 +0200 @@ -20,13 +20,11 @@ class BoolTest(unittest.TestCase): def test_print(self): try: - fo = open(support.TESTFN, "w") - print(False, True, file=fo) - fo.close() - fo = open(support.TESTFN, "r") - self.assertEqual(fo.read(), 'False True\n') + with open(support.TESTFN, "w") as fo: + print(False, True, file=fo) + with open(support.TESTFN, "r") as fi: + self.assertEqual(fi.read(), 'False True\n') finally: - fo.close() os.remove(support.TESTFN) def test_repr(self): @@ -234,9 +232,8 @@ class BoolTest(unittest.TestCase): def test_fileclosed(self): try: - f = open(support.TESTFN, "w") - self.assertIs(f.closed, False) - f.close() + with open(support.TESTFN, "w") as f: + self.assertIs(f.closed, False) self.assertIs(f.closed, True) finally: os.remove(support.TESTFN) diff -r fc7dbba57869 Lib/test/test_codecs.py --- a/Lib/test/test_codecs.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/test/test_codecs.py Sun Nov 09 21:03:10 2014 +0200 @@ -1129,9 +1129,8 @@ class EscapeDecodeTest(unittest.TestCase class RecodingTest(unittest.TestCase): def test_recoding(self): f = io.BytesIO() - f2 = codecs.EncodedFile(f, "unicode_internal", "utf-8") - f2.write("a") - f2.close() + with codecs.EncodedFile(f, "unicode_internal", "utf-8") as f2: + f2.write("a") # Python used to crash on this at exit because of a refcount # bug in _codecsmodule.c diff -r fc7dbba57869 Lib/test/test_curses.py --- a/Lib/test/test_curses.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/test/test_curses.py Sun Nov 09 21:03:10 2014 +0200 @@ -184,11 +184,10 @@ class TestCurses(unittest.TestCase): curses.delay_output(1) curses.echo() ; curses.echo(1) - f = tempfile.TemporaryFile() - stdscr.putwin(f) - f.seek(0) - curses.getwin(f) - f.close() + with tempfile.TemporaryFile() as f: + stdscr.putwin(f) + f.seek(0) + curses.getwin(f) curses.halfdelay(1) curses.intrflush(1) diff -r fc7dbba57869 Lib/test/test_dbm_dumb.py --- a/Lib/test/test_dbm_dumb.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/test/test_dbm_dumb.py Sun Nov 09 21:03:10 2014 +0200 @@ -2,6 +2,7 @@ Original by Roger E. Masse """ +import contextlib import io import operator import os @@ -31,12 +32,11 @@ class DumbDBMTestCase(unittest.TestCase) } def test_dumbdbm_creation(self): - f = dumbdbm.open(_fname, 'c') - self.assertEqual(list(f.keys()), []) - for key in self._dict: - f[key] = self._dict[key] - self.read_helper(f) - f.close() + with contextlib.closing(dumbdbm.open(_fname, 'c')) as f: + self.assertEqual(list(f.keys()), []) + for key in self._dict: + f[key] = self._dict[key] + self.read_helper(f) @unittest.skipUnless(hasattr(os, 'umask'), 'test needs os.umask()') @unittest.skipUnless(hasattr(os, 'chmod'), 'test needs os.chmod()') @@ -69,63 +69,55 @@ class DumbDBMTestCase(unittest.TestCase) def test_dumbdbm_modification(self): self.init_db() - f = dumbdbm.open(_fname, 'w') - self._dict[b'g'] = f[b'g'] = b"indented" - self.read_helper(f) - f.close() + with contextlib.closing(dumbdbm.open(_fname, 'w')) as f: + self._dict[b'g'] = f[b'g'] = b"indented" + self.read_helper(f) def test_dumbdbm_read(self): self.init_db() - f = dumbdbm.open(_fname, 'r') - self.read_helper(f) - f.close() + with contextlib.closing(dumbdbm.open(_fname, 'r')) as f: + self.read_helper(f) def test_dumbdbm_keys(self): self.init_db() - f = dumbdbm.open(_fname) - keys = self.keys_helper(f) - f.close() + with contextlib.closing(dumbdbm.open(_fname)) as f: + keys = self.keys_helper(f) def test_write_contains(self): - f = dumbdbm.open(_fname) - f[b'1'] = b'hello' - self.assertIn(b'1', f) - f.close() + with contextlib.closing(dumbdbm.open(_fname)) as f: + f[b'1'] = b'hello' + self.assertIn(b'1', f) def test_write_write_read(self): # test for bug #482460 - f = dumbdbm.open(_fname) - f[b'1'] = b'hello' - f[b'1'] = b'hello2' - f.close() - f = dumbdbm.open(_fname) - self.assertEqual(f[b'1'], b'hello2') - f.close() + with contextlib.closing(dumbdbm.open(_fname)) as f: + f[b'1'] = b'hello' + f[b'1'] = b'hello2' + with contextlib.closing(dumbdbm.open(_fname)) as f: + self.assertEqual(f[b'1'], b'hello2') def test_str_read(self): self.init_db() - f = dumbdbm.open(_fname, 'r') - self.assertEqual(f['\u00fc'], self._dict['\u00fc'.encode('utf-8')]) + with contextlib.closing(dumbdbm.open(_fname, 'r')) as f: + self.assertEqual(f['\u00fc'], self._dict['\u00fc'.encode('utf-8')]) def test_str_write_contains(self): self.init_db() - f = dumbdbm.open(_fname) - f['\u00fc'] = b'!' - f['1'] = 'a' - f.close() - f = dumbdbm.open(_fname, 'r') - self.assertIn('\u00fc', f) - self.assertEqual(f['\u00fc'.encode('utf-8')], - self._dict['\u00fc'.encode('utf-8')]) - self.assertEqual(f[b'1'], b'a') + with contextlib.closing(dumbdbm.open(_fname)) as f: + f['\u00fc'] = b'!' + f['1'] = 'a' + with contextlib.closing(dumbdbm.open(_fname, 'r')) as f: + self.assertIn('\u00fc', f) + self.assertEqual(f['\u00fc'.encode('utf-8')], + self._dict['\u00fc'.encode('utf-8')]) + self.assertEqual(f[b'1'], b'a') def test_line_endings(self): # test for bug #1172763: dumbdbm would die if the line endings # weren't what was expected. - f = dumbdbm.open(_fname) - f[b'1'] = b'hello' - f[b'2'] = b'hello2' - f.close() + with contextlib.closing(dumbdbm.open(_fname)) as f: + f[b'1'] = b'hello' + f[b'2'] = b'hello2' # Mangle the file by changing the line separator to Windows or Unix with io.open(_fname + '.dir', 'rb') as file: @@ -148,10 +140,9 @@ class DumbDBMTestCase(unittest.TestCase) self.assertEqual(self._dict[key], f[key]) def init_db(self): - f = dumbdbm.open(_fname, 'w') - for k in self._dict: - f[k] = self._dict[k] - f.close() + with contextlib.closing(dumbdbm.open(_fname, 'w')) as f: + for k in self._dict: + f[k] = self._dict[k] def keys_helper(self, f): keys = sorted(f.keys()) @@ -165,25 +156,23 @@ class DumbDBMTestCase(unittest.TestCase) import random d = {} # mirror the database for dummy in range(5): - f = dumbdbm.open(_fname) - for dummy in range(100): - k = random.choice('abcdefghijklm') - if random.random() < 0.2: - if k in d: - del d[k] - del f[k] - else: - v = random.choice((b'a', b'b', b'c')) * random.randrange(10000) - d[k] = v - f[k] = v - self.assertEqual(f[k], v) - f.close() + with contextlib.closing(dumbdbm.open(_fname)) as f: + for dummy in range(100): + k = random.choice('abcdefghijklm') + if random.random() < 0.2: + if k in d: + del d[k] + del f[k] + else: + v = random.choice((b'a', b'b', b'c')) * random.randrange(10000) + d[k] = v + f[k] = v + self.assertEqual(f[k], v) - f = dumbdbm.open(_fname) - expected = sorted((k.encode("latin-1"), v) for k, v in d.items()) - got = sorted(f.items()) - self.assertEqual(expected, got) - f.close() + with contextlib.closing(dumbdbm.open(_fname)) as f: + expected = sorted((k.encode("latin-1"), v) for k, v in d.items()) + got = sorted(f.items()) + self.assertEqual(expected, got) def test_context_manager(self): with dumbdbm.open(_fname, 'c') as db: diff -r fc7dbba57869 Lib/test/test_epoll.py --- a/Lib/test/test_epoll.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/test/test_epoll.py Sun Nov 09 21:03:10 2014 +0200 @@ -135,18 +135,17 @@ class TestEPoll(unittest.TestCase): def test_fromfd(self): server, client = self._connected_pair() - ep = select.epoll(2) - ep2 = select.epoll.fromfd(ep.fileno()) + with select.epoll(2) as ep: + ep2 = select.epoll.fromfd(ep.fileno()) - ep2.register(server.fileno(), select.EPOLLIN | select.EPOLLOUT) - ep2.register(client.fileno(), select.EPOLLIN | select.EPOLLOUT) + ep2.register(server.fileno(), select.EPOLLIN | select.EPOLLOUT) + ep2.register(client.fileno(), select.EPOLLIN | select.EPOLLOUT) - events = ep.poll(1, 4) - events2 = ep2.poll(0.9, 4) - self.assertEqual(len(events), 2) - self.assertEqual(len(events2), 2) + events = ep.poll(1, 4) + events2 = ep2.poll(0.9, 4) + self.assertEqual(len(events), 2) + self.assertEqual(len(events2), 2) - ep.close() try: ep2.poll(1, 4) except OSError as e: diff -r fc7dbba57869 Lib/test/test_float.py --- a/Lib/test/test_float.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/test/test_float.py Sun Nov 09 21:03:10 2014 +0200 @@ -599,15 +599,14 @@ class FormatTestCase(unittest.TestCase): class ReprTestCase(unittest.TestCase): def test_repr(self): - floats_file = open(os.path.join(os.path.split(__file__)[0], - 'floating_points.txt')) - for line in floats_file: - line = line.strip() - if not line or line.startswith('#'): - continue - v = eval(line) - self.assertEqual(v, eval(repr(v))) - floats_file.close() + with open(os.path.join(os.path.split(__file__)[0], + 'floating_points.txt')) as floats_file: + for line in floats_file: + line = line.strip() + if not line or line.startswith('#'): + continue + v = eval(line) + self.assertEqual(v, eval(repr(v))) @unittest.skipUnless(getattr(sys, 'float_repr_style', '') == 'short', "applies only when using short float repr style") diff -r fc7dbba57869 Lib/test/test_ioctl.py --- a/Lib/test/test_ioctl.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/test/test_ioctl.py Sun Nov 09 21:03:10 2014 +0200 @@ -11,9 +11,9 @@ try: except OSError: raise unittest.SkipTest("Unable to open /dev/tty") else: - # Skip if another process is in foreground - r = fcntl.ioctl(tty, termios.TIOCGPGRP, " ") - tty.close() + with tty: + # Skip if another process is in foreground + r = fcntl.ioctl(tty, termios.TIOCGPGRP, " ") rpgrp = struct.unpack("i", r)[0] if rpgrp not in (os.getpgrp(), os.getsid(0)): raise unittest.SkipTest("Neither the process group nor the session " diff -r fc7dbba57869 Lib/test/test_mmap.py --- a/Lib/test/test_mmap.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/test/test_mmap.py Sun Nov 09 21:03:10 2014 +0200 @@ -268,13 +268,12 @@ class MmapTests(unittest.TestCase): def test_find_end(self): # test the new 'end' parameter works as expected - f = open(TESTFN, 'wb+') - data = b'one two ones' - n = len(data) - f.write(data) - f.flush() - m = mmap.mmap(f.fileno(), n) - f.close() + with open(TESTFN, 'wb+') as f: + data = b'one two ones' + n = len(data) + f.write(data) + f.flush() + m = mmap.mmap(f.fileno(), n) self.assertEqual(m.find(b'one'), 0) self.assertEqual(m.find(b'ones'), 8) @@ -286,13 +285,12 @@ class MmapTests(unittest.TestCase): def test_rfind(self): # test the new 'end' parameter works as expected - f = open(TESTFN, 'wb+') - data = b'one two ones' - n = len(data) - f.write(data) - f.flush() - m = mmap.mmap(f.fileno(), n) - f.close() + with open(TESTFN, 'wb+') as f: + data = b'one two ones' + n = len(data) + f.write(data) + f.flush() + m = mmap.mmap(f.fileno(), n) self.assertEqual(m.rfind(b'one'), 8) self.assertEqual(m.rfind(b'one '), 0) @@ -304,31 +302,24 @@ class MmapTests(unittest.TestCase): def test_double_close(self): # make sure a double close doesn't crash on Solaris (Bug# 665913) - f = open(TESTFN, 'wb+') + with open(TESTFN, 'wb+') as f: + f.write(2**16 * b'a') # Arbitrary character - f.write(2**16 * b'a') # Arbitrary character - f.close() - - f = open(TESTFN, 'rb') - mf = mmap.mmap(f.fileno(), 2**16, access=mmap.ACCESS_READ) - mf.close() - mf.close() - f.close() + with open(TESTFN, 'rb') as f: + mf = mmap.mmap(f.fileno(), 2**16, access=mmap.ACCESS_READ) + mf.close() + mf.close() @unittest.skipUnless(hasattr(os, "stat"), "needs os.stat()") def test_entire_file(self): # test mapping of entire file by passing 0 for map length - f = open(TESTFN, "wb+") + with open(TESTFN, "wb+") as f: + f.write(2**16 * b'm') # Arbitrary character - f.write(2**16 * b'm') # Arbitrary character - f.close() - - f = open(TESTFN, "rb+") - mf = mmap.mmap(f.fileno(), 0) - self.assertEqual(len(mf), 2**16, "Map size should equal file size.") - self.assertEqual(mf.read(2**16), 2**16 * b"m") - mf.close() - f.close() + with open(TESTFN, "rb+") as f, \ + mmap.mmap(f.fileno(), 0) as mf: + self.assertEqual(len(mf), 2**16, "Map size should equal file size.") + self.assertEqual(mf.read(2**16), 2**16 * b"m") @unittest.skipUnless(hasattr(os, "stat"), "needs os.stat()") def test_length_0_offset(self): @@ -356,16 +347,15 @@ class MmapTests(unittest.TestCase): def test_move(self): # make move works everywhere (64-bit format problem earlier) - f = open(TESTFN, 'wb+') + with open(TESTFN, 'wb+') as f: - f.write(b"ABCDEabcde") # Arbitrary character - f.flush() + f.write(b"ABCDEabcde") # Arbitrary character + f.flush() - mf = mmap.mmap(f.fileno(), 10) - mf.move(5, 0, 5) - self.assertEqual(mf[:], b"ABCDEABCDE", "Map move should have duplicated front 5") - mf.close() - f.close() + mf = mmap.mmap(f.fileno(), 10) + mf.move(5, 0, 5) + self.assertEqual(mf[:], b"ABCDEABCDE", "Map move should have duplicated front 5") + mf.close() # more excessive test data = b"0123456789" @@ -563,10 +553,9 @@ class MmapTests(unittest.TestCase): mapsize = 10 with open(TESTFN, "wb") as fp: fp.write(b"a"*mapsize) - f = open(TESTFN, "rb") - m = mmap.mmap(f.fileno(), mapsize, prot=mmap.PROT_READ) - self.assertRaises(TypeError, m.write, "foo") - f.close() + with open(TESTFN, "rb") as f: + m = mmap.mmap(f.fileno(), mapsize, prot=mmap.PROT_READ) + self.assertRaises(TypeError, m.write, "foo") def test_error(self): self.assertIs(mmap.error, OSError) @@ -575,9 +564,8 @@ class MmapTests(unittest.TestCase): data = b"0123456789" with open(TESTFN, "wb") as fp: fp.write(b"x"*len(data)) - f = open(TESTFN, "r+b") - m = mmap.mmap(f.fileno(), len(data)) - f.close() + with open(TESTFN, "r+b") as f: + m = mmap.mmap(f.fileno(), len(data)) # Test write_byte() for i in range(len(data)): self.assertEqual(m.tell(), i) diff -r fc7dbba57869 Lib/test/test_os.py --- a/Lib/test/test_os.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/test/test_os.py Sun Nov 09 21:03:10 2014 +0200 @@ -205,9 +205,8 @@ class StatAttributeTests(unittest.TestCa def setUp(self): os.mkdir(support.TESTFN) self.fname = os.path.join(support.TESTFN, "f1") - f = open(self.fname, 'wb') - f.write(b"ABC") - f.close() + with open(self.fname, 'wb') as f: + f.write(b"ABC") def tearDown(self): os.unlink(self.fname) @@ -772,9 +771,8 @@ class WalkTests(unittest.TestCase): os.makedirs(sub2_path) os.makedirs(t2_path) for path in tmp1_path, tmp2_path, tmp3_path, tmp4_path: - f = open(path, "w") - f.write("I'm " + path + " and proud of it. Blame test_os.\n") - f.close() + with open(path, "w") as f: + f.write("I'm " + path + " and proud of it. Blame test_os.\n") if support.can_symlink(): os.symlink(os.path.abspath(t2_path), link_path) os.symlink('broken', broken_link_path, True) @@ -996,9 +994,8 @@ class MakedirTests(unittest.TestCase): def test_exist_ok_existing_regular_file(self): base = support.TESTFN path = os.path.join(support.TESTFN, 'dir1') - f = open(path, 'w') - f.write('abc') - f.close() + with open(path, 'w') as f: + f.write('abc') self.assertRaises(OSError, os.makedirs, path) self.assertRaises(OSError, os.makedirs, path, exist_ok=False) self.assertRaises(OSError, os.makedirs, path, exist_ok=True) diff -r fc7dbba57869 Lib/test/test_pipes.py --- a/Lib/test/test_pipes.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/test/test_pipes.py Sun Nov 09 21:03:10 2014 +0200 @@ -20,9 +20,8 @@ class SimplePipeTests(unittest.TestCase) def testSimplePipe1(self): t = pipes.Template() t.append(s_command, pipes.STDIN_STDOUT) - f = t.open(TESTFN, 'w') - f.write('hello world #1') - f.close() + with t.open(TESTFN, 'w') as f: + f.write('hello world #1') with open(TESTFN) as f: self.assertEqual(f.read(), 'HELLO WORLD #1') diff -r fc7dbba57869 Lib/test/test_pkg.py --- a/Lib/test/test_pkg.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/test/test_pkg.py Sun Nov 09 21:03:10 2014 +0200 @@ -81,11 +81,10 @@ class TestPkg(unittest.TestCase): if contents is None: os.mkdir(fullname) else: - f = open(fullname, "w") - f.write(contents) - if contents and contents[-1] != '\n': - f.write('\n') - f.close() + with open(fullname, "w") as f: + f.write(contents) + if contents and contents[-1] != '\n': + f.write('\n') self.root = root # package name is the name of the first item self.pkgname = descr[0][0] diff -r fc7dbba57869 Lib/test/test_platform.py --- a/Lib/test/test_platform.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/test/test_platform.py Sun Nov 09 21:03:10 2014 +0200 @@ -190,13 +190,12 @@ class PlatformTest(unittest.TestCase): if platform.uname().system == 'Darwin': # We're on a MacOSX system, check that # the right version information is returned - fd = os.popen('sw_vers', 'r') - real_ver = None - for ln in fd: - if ln.startswith('ProductVersion:'): - real_ver = ln.strip().split()[-1] - break - fd.close() + with os.popen('sw_vers', 'r') as fd: + real_ver = None + for ln in fd: + if ln.startswith('ProductVersion:'): + real_ver = ln.strip().split()[-1] + break self.assertFalse(real_ver is None) result_list = res[0].split('.') expect_list = real_ver.split('.') diff -r fc7dbba57869 Lib/test/test_poll.py --- a/Lib/test/test_poll.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/test/test_poll.py Sun Nov 09 21:03:10 2014 +0200 @@ -86,13 +86,12 @@ class PollTests(unittest.TestCase): r = p.poll() self.assertEqual(r[0], (FD, select.POLLNVAL)) - f = open(TESTFN, 'w') - fd = f.fileno() - p = select.poll() - p.register(f) - r = p.poll() - self.assertEqual(r[0][0], fd) - f.close() + with open(TESTFN, 'w') as f: + fd = f.fileno() + p = select.poll() + p.register(f) + r = p.poll() + self.assertEqual(r[0][0], fd) r = p.poll() self.assertEqual(r[0], (fd, select.POLLNVAL)) os.unlink(TESTFN) diff -r fc7dbba57869 Lib/test/test_random.py --- a/Lib/test/test_random.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/test/test_random.py Sun Nov 09 21:03:10 2014 +0200 @@ -173,9 +173,8 @@ class TestBasicOps: ("randv2_64.pck", 866), ("randv3.pck", 343)] for file, value in files: - f = open(support.findfile(file),"rb") - r = pickle.load(f) - f.close() + with open(support.findfile(file),"rb") as f: + r = pickle.load(f) self.assertEqual(int(r.random()*1000), value) def test_bug_9025(self): diff -r fc7dbba57869 Lib/test/test_runpy.py --- a/Lib/test/test_runpy.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/test/test_runpy.py Sun Nov 09 21:03:10 2014 +0200 @@ -234,9 +234,8 @@ class RunModuleTestCase(unittest.TestCas if verbose > 1: print(" Next level in:", sub_dir) if verbose > 1: print(" Created:", pkg_fname) mod_fname = os.path.join(sub_dir, test_fname) - mod_file = open(mod_fname, "w") - mod_file.write(source) - mod_file.close() + with open(mod_fname, "w") as mod_file: + mod_file.write(source) if verbose > 1: print(" Created:", mod_fname) mod_name = (pkg_name+".")*depth + mod_base mod_spec = importlib.util.spec_from_file_location(mod_name, diff -r fc7dbba57869 Lib/test/test_select.py --- a/Lib/test/test_select.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/test/test_select.py Sun Nov 09 21:03:10 2014 +0200 @@ -46,24 +46,23 @@ class SelectTestCase(unittest.TestCase): def test_select(self): cmd = 'for i in 0 1 2 3 4 5 6 7 8 9; do echo testing...; sleep 1; done' - p = os.popen(cmd, 'r') - for tout in (0, 1, 2, 4, 8, 16) + (None,)*10: - if support.verbose: - print('timeout =', tout) - rfd, wfd, xfd = select.select([p], [], [], tout) - if (rfd, wfd, xfd) == ([], [], []): - continue - if (rfd, wfd, xfd) == ([p], [], []): - line = p.readline() + with os.popen(cmd, 'r') as p: + for tout in (0, 1, 2, 4, 8, 16) + (None,)*10: if support.verbose: - print(repr(line)) - if not line: + print('timeout =', tout) + rfd, wfd, xfd = select.select([p], [], [], tout) + if (rfd, wfd, xfd) == ([], [], []): + continue + if (rfd, wfd, xfd) == ([p], [], []): + line = p.readline() if support.verbose: - print('EOF') - break - continue - self.fail('Unexpected return values from select():', rfd, wfd, xfd) - p.close() + print(repr(line)) + if not line: + if support.verbose: + print('EOF') + break + continue + self.fail('Unexpected return values from select():', rfd, wfd, xfd) # Issue 16230: Crash on select resized list def test_select_mutated(self): diff -r fc7dbba57869 Lib/test/test_shelve.py --- a/Lib/test/test_shelve.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/test/test_shelve.py Sun Nov 09 21:03:10 2014 +0200 @@ -88,15 +88,13 @@ class TestCase(unittest.TestCase): def test_in_memory_shelf(self): d1 = byteskeydict() - s = shelve.Shelf(d1, protocol=0) - s['key1'] = (1,2,3,4) - self.assertEqual(s['key1'], (1,2,3,4)) - s.close() + with shelve.Shelf(d1, protocol=0) as s: + s['key1'] = (1,2,3,4) + self.assertEqual(s['key1'], (1,2,3,4)) d2 = byteskeydict() - s = shelve.Shelf(d2, protocol=1) - s['key1'] = (1,2,3,4) - self.assertEqual(s['key1'], (1,2,3,4)) - s.close() + with shelve.Shelf(d2, protocol=1) as s: + s['key1'] = (1,2,3,4) + self.assertEqual(s['key1'], (1,2,3,4)) self.assertEqual(len(d1), 1) self.assertEqual(len(d2), 1) @@ -104,20 +102,18 @@ class TestCase(unittest.TestCase): def test_mutable_entry(self): d1 = byteskeydict() - s = shelve.Shelf(d1, protocol=2, writeback=False) - s['key1'] = [1,2,3,4] - self.assertEqual(s['key1'], [1,2,3,4]) - s['key1'].append(5) - self.assertEqual(s['key1'], [1,2,3,4]) - s.close() + with shelve.Shelf(d1, protocol=2, writeback=False) as s: + s['key1'] = [1,2,3,4] + self.assertEqual(s['key1'], [1,2,3,4]) + s['key1'].append(5) + self.assertEqual(s['key1'], [1,2,3,4]) d2 = byteskeydict() - s = shelve.Shelf(d2, protocol=2, writeback=True) - s['key1'] = [1,2,3,4] - self.assertEqual(s['key1'], [1,2,3,4]) - s['key1'].append(5) - self.assertEqual(s['key1'], [1,2,3,4,5]) - s.close() + with shelve.Shelf(d2, protocol=2, writeback=True) as s: + s['key1'] = [1,2,3,4] + self.assertEqual(s['key1'], [1,2,3,4]) + s['key1'].append(5) + self.assertEqual(s['key1'], [1,2,3,4,5]) self.assertEqual(len(d1), 1) self.assertEqual(len(d2), 1) @@ -140,11 +136,10 @@ class TestCase(unittest.TestCase): d = {} key = 'key' encodedkey = key.encode('utf-8') - s = shelve.Shelf(d, writeback=True) - s[key] = [1] - p1 = d[encodedkey] # Will give a KeyError if backing store not updated - s['key'].append(2) - s.close() + with shelve.Shelf(d, writeback=True) as s: + s[key] = [1] + p1 = d[encodedkey] # Will give a KeyError if backing store not updated + s['key'].append(2) p2 = d[encodedkey] self.assertNotEqual(p1, p2) # Write creates new object in store diff -r fc7dbba57869 Lib/test/test_site.py --- a/Lib/test/test_site.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/test/test_site.py Sun Nov 09 21:03:10 2014 +0200 @@ -102,10 +102,9 @@ class HelperFunctionsTests(unittest.Test pth_dir = os.path.abspath(pth_dir) pth_basename = pth_name + '.pth' pth_fn = os.path.join(pth_dir, pth_basename) - pth_file = open(pth_fn, 'w', encoding='utf-8') - self.addCleanup(lambda: os.remove(pth_fn)) - pth_file.write(contents) - pth_file.close() + with open(pth_fn, 'w', encoding='utf-8') as pth_file: + self.addCleanup(lambda: os.remove(pth_fn)) + pth_file.write(contents) return pth_dir, pth_basename def test_addpackage_import_bad_syntax(self): diff -r fc7dbba57869 Lib/test/test_socket.py --- a/Lib/test/test_socket.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/test/test_socket.py Sun Nov 09 21:03:10 2014 +0200 @@ -686,10 +686,9 @@ class GeneralModuleTests(unittest.TestCa self.assertEqual(repr(s), expected) def test_weakref(self): - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - p = proxy(s) - self.assertEqual(p.fileno(), s.fileno()) - s.close() + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + p = proxy(s) + self.assertEqual(p.fileno(), s.fileno()) s = None try: p.fileno() @@ -933,23 +932,20 @@ class GeneralModuleTests(unittest.TestCa # Testing default timeout # The default timeout should initially be None self.assertEqual(socket.getdefaulttimeout(), None) - s = socket.socket() - self.assertEqual(s.gettimeout(), None) - s.close() + with socket.socket() as s: + self.assertEqual(s.gettimeout(), None) # Set the default timeout to 10, and see if it propagates socket.setdefaulttimeout(10) self.assertEqual(socket.getdefaulttimeout(), 10) - s = socket.socket() - self.assertEqual(s.gettimeout(), 10) - s.close() + with socket.socket() as s: + self.assertEqual(s.gettimeout(), 10) # Reset the default timeout to None, and see if it propagates socket.setdefaulttimeout(None) self.assertEqual(socket.getdefaulttimeout(), None) - s = socket.socket() - self.assertEqual(s.gettimeout(), None) - s.close() + with socket.socket() as s: + self.assertEqual(s.gettimeout(), None) # Check that setting it to an invalid value raises ValueError self.assertRaises(ValueError, socket.setdefaulttimeout, -1) @@ -1153,24 +1149,22 @@ class GeneralModuleTests(unittest.TestCa def testSendAfterClose(self): # testing send() after close() with timeout - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - sock.settimeout(1) - sock.close() + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: + sock.settimeout(1) self.assertRaises(OSError, sock.send, b"spam") def testNewAttributes(self): # testing .family, .type and .protocol - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - self.assertEqual(sock.family, socket.AF_INET) - if hasattr(socket, 'SOCK_CLOEXEC'): - self.assertIn(sock.type, - (socket.SOCK_STREAM | socket.SOCK_CLOEXEC, - socket.SOCK_STREAM)) - else: - self.assertEqual(sock.type, socket.SOCK_STREAM) - self.assertEqual(sock.proto, 0) - sock.close() + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: + self.assertEqual(sock.family, socket.AF_INET) + if hasattr(socket, 'SOCK_CLOEXEC'): + self.assertIn(sock.type, + (socket.SOCK_STREAM | socket.SOCK_CLOEXEC, + socket.SOCK_STREAM)) + else: + self.assertEqual(sock.type, socket.SOCK_STREAM) + self.assertEqual(sock.proto, 0) def test_getsockaddrarg(self): sock = socket.socket() @@ -1392,10 +1386,9 @@ class GeneralModuleTests(unittest.TestCa def test_listen_backlog_overflow(self): # Issue 15989 import _testcapi - srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - srv.bind((HOST, 0)) - self.assertRaises(OverflowError, srv.listen, _testcapi.INT_MAX + 1) - srv.close() + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as srv: + srv.bind((HOST, 0)) + self.assertRaises(OverflowError, srv.listen, _testcapi.INT_MAX + 1) @unittest.skipUnless(support.IPV6_ENABLED, 'IPv6 required for this test.') def test_flowinfo(self): diff -r fc7dbba57869 Lib/test/test_socketserver.py --- a/Lib/test/test_socketserver.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/test/test_socketserver.py Sun Nov 09 21:03:10 2014 +0200 @@ -148,25 +148,23 @@ class SocketServerTest(unittest.TestCase if verbose: print("done") def stream_examine(self, proto, addr): - s = socket.socket(proto, socket.SOCK_STREAM) - s.connect(addr) - s.sendall(TEST_STR) - buf = data = receive(s, 100) - while data and b'\n' not in buf: - data = receive(s, 100) - buf += data - self.assertEqual(buf, TEST_STR) - s.close() + with socket.socket(proto, socket.SOCK_STREAM) as s: + s.connect(addr) + s.sendall(TEST_STR) + buf = data = receive(s, 100) + while data and b'\n' not in buf: + data = receive(s, 100) + buf += data + self.assertEqual(buf, TEST_STR) def dgram_examine(self, proto, addr): - s = socket.socket(proto, socket.SOCK_DGRAM) - s.sendto(TEST_STR, addr) - buf = data = receive(s, 100) - while data and b'\n' not in buf: - data = receive(s, 100) - buf += data - self.assertEqual(buf, TEST_STR) - s.close() + with socket.socket(proto, socket.SOCK_DGRAM) as s: + s.sendto(TEST_STR, addr) + buf = data = receive(s, 100) + while data and b'\n' not in buf: + data = receive(s, 100) + buf += data + self.assertEqual(buf, TEST_STR) def test_TCPServer(self): self.run_server(socketserver.TCPServer, diff -r fc7dbba57869 Lib/test/test_ssl.py --- a/Lib/test/test_ssl.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/test/test_ssl.py Sun Nov 09 21:03:10 2014 +0200 @@ -1328,11 +1328,10 @@ class NetworkedTests(unittest.TestCase): s.close() # this should fail because we have no verification certs - s = ssl.wrap_socket(socket.socket(socket.AF_INET), - cert_reqs=ssl.CERT_REQUIRED) - self.assertRaisesRegex(ssl.SSLError, "certificate verify failed", - s.connect, ("svn.python.org", 443)) - s.close() + with ssl.wrap_socket(socket.socket(socket.AF_INET), + cert_reqs=ssl.CERT_REQUIRED) as s: + self.assertRaisesRegex(ssl.SSLError, "certificate verify failed", + s.connect, ("svn.python.org", 443)) # this should succeed because we specify the root cert s = ssl.wrap_socket(socket.socket(socket.AF_INET), diff -r fc7dbba57869 Lib/test/test_tarfile.py --- a/Lib/test/test_tarfile.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/test/test_tarfile.py Sun Nov 09 21:03:10 2014 +0200 @@ -127,49 +127,47 @@ class UstarReadTest(ReadTest, unittest.T data = fobj.read() tarinfo = self.tar.getmember("ustar/regtype") - fobj = self.tar.extractfile(tarinfo) - - text = fobj.read() - fobj.seek(0) - self.assertEqual(0, fobj.tell(), - "seek() to file's start failed") - fobj.seek(2048, 0) - self.assertEqual(2048, fobj.tell(), - "seek() to absolute position failed") - fobj.seek(-1024, 1) - self.assertEqual(1024, fobj.tell(), - "seek() to negative relative position failed") - fobj.seek(1024, 1) - self.assertEqual(2048, fobj.tell(), - "seek() to positive relative position failed") - s = fobj.read(10) - self.assertEqual(s, data[2048:2058], - "read() after seek failed") - fobj.seek(0, 2) - self.assertEqual(tarinfo.size, fobj.tell(), - "seek() to file's end failed") - self.assertEqual(fobj.read(), b"", - "read() at file's end did not return empty string") - fobj.seek(-tarinfo.size, 2) - self.assertEqual(0, fobj.tell(), - "relative seek() to file's end failed") - fobj.seek(512) - s1 = fobj.readlines() - fobj.seek(512) - s2 = fobj.readlines() - self.assertEqual(s1, s2, - "readlines() after seek failed") - fobj.seek(0) - self.assertEqual(len(fobj.readline()), fobj.tell(), - "tell() after readline() failed") - fobj.seek(512) - self.assertEqual(len(fobj.readline()) + 512, fobj.tell(), - "tell() after seek() and readline() failed") - fobj.seek(0) - line = fobj.readline() - self.assertEqual(fobj.read(), data[len(line):], - "read() after readline() failed") - fobj.close() + with self.tar.extractfile(tarinfo) as fobj: + text = fobj.read() + fobj.seek(0) + self.assertEqual(0, fobj.tell(), + "seek() to file's start failed") + fobj.seek(2048, 0) + self.assertEqual(2048, fobj.tell(), + "seek() to absolute position failed") + fobj.seek(-1024, 1) + self.assertEqual(1024, fobj.tell(), + "seek() to negative relative position failed") + fobj.seek(1024, 1) + self.assertEqual(2048, fobj.tell(), + "seek() to positive relative position failed") + s = fobj.read(10) + self.assertEqual(s, data[2048:2058], + "read() after seek failed") + fobj.seek(0, 2) + self.assertEqual(tarinfo.size, fobj.tell(), + "seek() to file's end failed") + self.assertEqual(fobj.read(), b"", + "read() at file's end did not return empty string") + fobj.seek(-tarinfo.size, 2) + self.assertEqual(0, fobj.tell(), + "relative seek() to file's end failed") + fobj.seek(512) + s1 = fobj.readlines() + fobj.seek(512) + s2 = fobj.readlines() + self.assertEqual(s1, s2, + "readlines() after seek failed") + fobj.seek(0) + self.assertEqual(len(fobj.readline()), fobj.tell(), + "tell() after readline() failed") + fobj.seek(512) + self.assertEqual(len(fobj.readline()) + 512, fobj.tell(), + "tell() after seek() and readline() failed") + fobj.seek(0) + line = fobj.readline() + self.assertEqual(fobj.read(), data[len(line):], + "read() after readline() failed") def test_fileobj_text(self): with self.tar.extractfile("ustar/regtype") as fobj: @@ -437,15 +435,14 @@ class MiscReadTestBase(CommonReadTest): fobj.seek(offset) # Test if the tarfile starts with the second member. - tar = tar.open(self.tarname, mode="r:", fileobj=fobj) - t = tar.next() - self.assertEqual(t.name, name) - # Read to the end of fileobj and test if seeking back to the - # beginning works. - tar.getmembers() - self.assertEqual(tar.extractfile(t).read(), data, - "seek back did not work") - tar.close() + with tar.open(self.tarname, mode="r:", fileobj=fobj) as tar: + t = tar.next() + self.assertEqual(t.name, name) + # Read to the end of fileobj and test if seeking back to the + # beginning works. + tar.getmembers() + self.assertEqual(tar.extractfile(t).read(), data, + "seek back did not work") def test_fail_comp(self): # For Gzip and Bz2 Tests: fail with a ReadError on an uncompressed file. @@ -968,9 +965,8 @@ class WriteTestBase(TarTest): def test_fileobj_no_close(self): fobj = io.BytesIO() - tar = tarfile.open(fileobj=fobj, mode=self.mode) - tar.addfile(tarfile.TarInfo("foo")) - tar.close() + with tarfile.open(fileobj=fobj, mode=self.mode) as tar: + tar.addfile(tarfile.TarInfo("foo")) self.assertFalse(fobj.closed, "external fileobjs must never closed") # Issue #20238: Incomplete gzip output with mode="w:gz" data = fobj.getvalue() @@ -1210,10 +1206,9 @@ class WriteTest(WriteTestBase, unittest. with open(source_file,'w') as f: f.write('something\n') os.symlink(source_file, target_file) - tar = tarfile.open(temparchive,'w') - tar.add(source_file) - tar.add(target_file) - tar.close() + with tarfile.open(temparchive, 'w') as tar: + tar.add(source_file) + tar.add(target_file) # Let's extract it to the location which contains the symlink tar = tarfile.open(temparchive,'r') # this should not raise OSError: [Errno 17] File exists @@ -1223,6 +1218,12 @@ class WriteTest(WriteTestBase, unittest. self.fail("extractall failed with symlinked files") finally: tar.close() + with tarfile.open(temparchive,'r') as tar: + # this should not raise OSError: [Errno 17] File exists + try: + tar.extractall(path=tempdir) + except OSError: + self.fail("extractall failed with symlinked files") finally: support.unlink(temparchive) support.rmtree(tempdir) diff -r fc7dbba57869 Lib/test/test_tempfile.py --- a/Lib/test/test_tempfile.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/test/test_tempfile.py Sun Nov 09 21:03:10 2014 +0200 @@ -466,9 +466,8 @@ class TestGetTempDir(BaseTestCase): # sneaky: just instantiate a NamedTemporaryFile, which # defaults to writing into the directory returned by # gettempdir. - file = tempfile.NamedTemporaryFile() - file.write(b"blat") - file.close() + with tempfile.NamedTemporaryFile() as file: + file.write(b"blat") def test_same_thing(self): # gettempdir always returns the same object @@ -717,9 +716,8 @@ class TestNamedTemporaryFile(BaseTestCas # A NamedTemporaryFile is deleted when closed dir = tempfile.mkdtemp() try: - f = tempfile.NamedTemporaryFile(dir=dir) - f.write(b'blat') - f.close() + with tempfile.NamedTemporaryFile(dir=dir) as f: + f.write(b'blat') self.assertFalse(os.path.exists(f.name), "NamedTemporaryFile %s exists after close" % f.name) finally: diff -r fc7dbba57869 Lib/test/test_threading.py --- a/Lib/test/test_threading.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/test/test_threading.py Sun Nov 09 21:03:10 2014 +0200 @@ -746,13 +746,11 @@ class ThreadJoinOnShutdown(BaseTestCase) def random_io(): '''Loop for a while sleeping random tiny amounts and doing some I/O.''' while True: - in_f = open(os.__file__, 'rb') - stuff = in_f.read(200) - null_f = open(os.devnull, 'wb') - null_f.write(stuff) - time.sleep(random.random() / 1995) - null_f.close() - in_f.close() + with open(os.__file__, 'rb') as in_f: + stuff = in_f.read(200) + with open(os.devnull, 'wb') as null_f: + null_f.write(stuff) + time.sleep(random.random() / 1995) thread_has_run.add(threading.current_thread()) def main(): diff -r fc7dbba57869 Lib/test/test_urllib2.py --- a/Lib/test/test_urllib2.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/test/test_urllib2.py Sun Nov 09 21:03:10 2014 +0200 @@ -51,10 +51,8 @@ class TrivialTests(unittest.TestCase): else: file_url = "file://%s" % fname - f = urllib.request.urlopen(file_url) - - f.read() - f.close() + with urllib.request.urlopen(file_url) as f: + f.read() def test_parse_http_list(self): tests = [ diff -r fc7dbba57869 Lib/test/test_urllib2_localnet.py --- a/Lib/test/test_urllib2_localnet.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/test/test_urllib2_localnet.py Sun Nov 09 21:03:10 2014 +0200 @@ -361,10 +361,9 @@ class ProxyAuthTests(unittest.TestCase): self.proxy_digest_handler.add_password(self.REALM, self.URL, self.USER, self.PASSWD) self.digest_auth_handler.set_qop("auth") - result = self.opener.open(self.URL) - while result.read(): - pass - result.close() + with self.opener.open(self.URL) as result: + while result.read(): + pass def test_proxy_qop_auth_int_works_or_throws_urlerror(self): self.proxy_digest_handler.add_password(self.REALM, self.URL, @@ -376,11 +375,11 @@ class ProxyAuthTests(unittest.TestCase): # It's okay if we don't support auth-int, but we certainly # shouldn't receive any kind of exception here other than # a URLError. - result = None - if result: - while result.read(): - pass - result.close() + pass + else: + with result: + while result.read(): + pass def GetRequestHandler(responses): @@ -598,14 +597,11 @@ class TestUrlopen(unittest.TestCase): def test_basic(self): handler = self.start_server() - open_url = urllib.request.urlopen("http://localhost:%s" % handler.port) - for attr in ("read", "close", "info", "geturl"): - self.assertTrue(hasattr(open_url, attr), "object returned from " - "urlopen lacks the %s attribute" % attr) - try: + with urllib.request.urlopen("http://localhost:%s" % handler.port) as open_url: + for attr in ("read", "close", "info", "geturl"): + self.assertTrue(hasattr(open_url, attr), "object returned from " + "urlopen lacks the %s attribute" % attr) self.assertTrue(open_url.read(), "calling 'read' failed") - finally: - open_url.close() def test_info(self): handler = self.start_server() diff -r fc7dbba57869 Lib/test/test_uu.py --- a/Lib/test/test_uu.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/test/test_uu.py Sun Nov 09 21:03:10 2014 +0200 @@ -172,26 +172,21 @@ class UUFileTest(unittest.TestCase): fin = fout = None try: support.unlink(self.tmpin) - fin = open(self.tmpin, 'wb') - fin.write(plaintext) - fin.close() + with open(self.tmpin, 'wb') as fin: + fin.write(plaintext) - fin = open(self.tmpin, 'rb') - fout = open(self.tmpout, 'wb') - uu.encode(fin, fout, self.tmpin, mode=0o644) - fin.close() - fout.close() + with open(self.tmpin, 'rb') as fin, \ + open(self.tmpout, 'wb') as fout: + uu.encode(fin, fout, self.tmpin, mode=0o644) - fout = open(self.tmpout, 'rb') - s = fout.read() - fout.close() + with open(self.tmpout, 'rb') as fout: + s = fout.read() self.assertEqual(s, encodedtextwrapped(0o644, self.tmpin)) # in_file and out_file as filenames uu.encode(self.tmpin, self.tmpout, self.tmpin, mode=0o644) - fout = open(self.tmpout, 'rb') - s = fout.read() - fout.close() + with open(self.tmpout, 'rb') as fout: + s = fout.read() self.assertEqual(s, encodedtextwrapped(0o644, self.tmpin)) finally: @@ -202,17 +197,14 @@ class UUFileTest(unittest.TestCase): f = None try: support.unlink(self.tmpin) - f = open(self.tmpin, 'wb') - f.write(encodedtextwrapped(0o644, self.tmpout)) - f.close() + with open(self.tmpin, 'wb') as f: + f.write(encodedtextwrapped(0o644, self.tmpout)) - f = open(self.tmpin, 'rb') - uu.decode(f) - f.close() + with open(self.tmpin, 'rb') as f: + uu.decode(f) - f = open(self.tmpout, 'rb') - s = f.read() - f.close() + with open(self.tmpout, 'rb') as f: + s = f.read() self.assertEqual(s, plaintext) # XXX is there an xp way to verify the mode? finally: @@ -222,15 +214,13 @@ class UUFileTest(unittest.TestCase): f = None try: support.unlink(self.tmpin) - f = open(self.tmpin, 'wb') - f.write(encodedtextwrapped(0o644, self.tmpout)) - f.close() + with open(self.tmpin, 'wb') as f: + f.write(encodedtextwrapped(0o644, self.tmpout)) uu.decode(self.tmpin) - f = open(self.tmpout, 'rb') - s = f.read() - f.close() + with open(self.tmpout, 'rb') as f: + s = f.read() self.assertEqual(s, plaintext) finally: self._kill(f) @@ -241,13 +231,11 @@ class UUFileTest(unittest.TestCase): try: f = io.BytesIO(encodedtextwrapped(0o644, self.tmpout)) - f = open(self.tmpin, 'rb') - uu.decode(f) - f.close() + with open(self.tmpin, 'rb') as f: + uu.decode(f) - f = open(self.tmpin, 'rb') - self.assertRaises(uu.Error, uu.decode, f) - f.close() + with open(self.tmpin, 'rb') as f: + self.assertRaises(uu.Error, uu.decode, f) finally: self._kill(f) diff -r fc7dbba57869 Lib/test/test_xmlrpc.py --- a/Lib/test/test_xmlrpc.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/test/test_xmlrpc.py Sun Nov 09 21:03:10 2014 +0200 @@ -586,10 +586,9 @@ class SimpleServerTestCase(BaseServerTes def XXXtest_404(self): # send POST with http.client, it should return 404 header and # 'Not Found' message. - conn = httplib.client.HTTPConnection(ADDR, PORT) - conn.request('POST', '/this-is-not-valid') - response = conn.getresponse() - conn.close() + with contextlib.closing(httplib.client.HTTPConnection(ADDR, PORT)) as conn: + conn.request('POST', '/this-is-not-valid') + response = conn.getresponse() self.assertEqual(response.status, 404) self.assertEqual(response.reason, 'Not Found') @@ -709,9 +708,8 @@ class SimpleServerTestCase(BaseServerTes def test_partial_post(self): # Check that a partial POST doesn't make the server loop: issue #14001. - conn = http.client.HTTPConnection(ADDR, PORT) - conn.request('POST', '/RPC2 HTTP/1.0\r\nContent-Length: 100\r\n\r\nbye') - conn.close() + with contextlib.closing(http.client.HTTPConnection(ADDR, PORT)) as conn: + conn.request('POST', '/RPC2 HTTP/1.0\r\nContent-Length: 100\r\n\r\nbye') def test_context_manager(self): with xmlrpclib.ServerProxy(URL) as server: diff -r fc7dbba57869 Lib/test/test_zipfile64.py --- a/Lib/test/test_zipfile64.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/test/test_zipfile64.py Sun Nov 09 21:03:10 2014 +0200 @@ -32,42 +32,34 @@ class TestsWithSourceFile(unittest.TestC self.data = '\n'.join(line_gen).encode('ascii') # And write it to a file. - fp = open(TESTFN, "wb") - fp.write(self.data) - fp.close() + with open(TESTFN, "wb") as fp: + fp.write(self.data) def zipTest(self, f, compression): # Create the ZIP archive. - zipfp = zipfile.ZipFile(f, "w", compression) - - # It will contain enough copies of self.data to reach about 6GB of - # raw data to store. - filecount = 6*1024**3 // len(self.data) - - next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL - for num in range(filecount): - zipfp.writestr("testfn%d" % num, self.data) - # Print still working message since this test can be really slow - if next_time <= time.time(): - next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL - print(( - ' zipTest still writing %d of %d, be patient...' % - (num, filecount)), file=sys.__stdout__) - sys.__stdout__.flush() - zipfp.close() + with zipfile.ZipFile(f, "w", compression) as zipfp: + next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL + for num in range(filecount): + zipfp.writestr("testfn%d" % num, self.data) + # Print still working message since this test can be really slow + if next_time <= time.time(): + next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL + print(( + ' zipTest still writing %d of %d, be patient...' % + (num, filecount)), file=sys.__stdout__) + sys.__stdout__.flush() # Read the ZIP archive - zipfp = zipfile.ZipFile(f, "r", compression) - for num in range(filecount): - self.assertEqual(zipfp.read("testfn%d" % num), self.data) - # Print still working message since this test can be really slow - if next_time <= time.time(): - next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL - print(( - ' zipTest still reading %d of %d, be patient...' % - (num, filecount)), file=sys.__stdout__) - sys.__stdout__.flush() - zipfp.close() + with zipfile.ZipFile(f, "r", compression) as zipfp: + for num in range(filecount): + self.assertEqual(zipfp.read("testfn%d" % num), self.data) + # Print still working message since this test can be really slow + if next_time <= time.time(): + next_time = time.time() + _PRINT_WORKING_MSG_INTERVAL + print(( + ' zipTest still reading %d of %d, be patient...' % + (num, filecount)), file=sys.__stdout__) + sys.__stdout__.flush() def testStored(self): # Try the temp file first. If we do TESTFN2 first, then it hogs @@ -92,20 +84,19 @@ class OtherTests(unittest.TestCase): def testMoreThan64kFiles(self): # This test checks that more than 64k files can be added to an archive, # and that the resulting archive can be read properly by ZipFile - zipf = zipfile.ZipFile(TESTFN, mode="w", allowZip64=True) - zipf.debug = 100 - numfiles = (1 << 16) * 3//2 - for i in range(numfiles): - zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57)) - self.assertEqual(len(zipf.namelist()), numfiles) + with zipfile.ZipFile(TESTFN, mode="w", allowZip64=True) as zipf: + zipf.debug = 100 + numfiles = (1 << 16) * 3//2 + for i in range(numfiles): + zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57)) + self.assertEqual(len(zipf.namelist()), numfiles) zipf.close() - zipf2 = zipfile.ZipFile(TESTFN, mode="r") - self.assertEqual(len(zipf2.namelist()), numfiles) - for i in range(numfiles): - content = zipf2.read("foo%08d" % i).decode('ascii') - self.assertEqual(content, "%d" % (i**3 % 57)) - zipf2.close() + with zipfile.ZipFile(TESTFN, mode="r") as zipf2: + self.assertEqual(len(zipf2.namelist()), numfiles) + for i in range(numfiles): + content = zipf2.read("foo%08d" % i).decode('ascii') + self.assertEqual(content, "%d" % (i**3 % 57)) def testMoreThan64kFilesAppend(self): zipf = zipfile.ZipFile(TESTFN, mode="w", allowZip64=False) diff -r fc7dbba57869 Lib/test/test_zipimport.py --- a/Lib/test/test_zipimport.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/test/test_zipimport.py Sun Nov 09 21:03:10 2014 +0200 @@ -406,15 +406,12 @@ class UncompressedZipImportTestCase(Impo "need an unencodable filename") def testUnencodable(self): filename = support.TESTFN_UNENCODABLE + ".zip" - z = ZipFile(filename, "w") - zinfo = ZipInfo(TESTMOD + ".py", time.localtime(NOW)) - zinfo.compress_type = self.compression - z.writestr(zinfo, test_src) - z.close() - try: - zipimport.zipimporter(filename) - finally: - os.remove(filename) + with ZipFile(filename, "w") as z: + self.addCleanup(os.remove, filename) + zinfo = ZipInfo(TESTMOD + ".py", time.localtime(NOW)) + zinfo.compress_type = self.compression + z.writestr(zinfo, test_src) + zipimport.zipimporter(filename) @support.requires_zlib diff -r fc7dbba57869 Lib/test/test_zipimport_support.py --- a/Lib/test/test_zipimport_support.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/test/test_zipimport_support.py Sun Nov 09 21:03:10 2014 +0200 @@ -123,15 +123,13 @@ class ZipSupportTests(unittest.TestCase) test_src) zip_name, run_name = make_zip_script(d, 'test_zip', script_name) - z = zipfile.ZipFile(zip_name, 'a') - for mod_name, src in sample_sources.items(): - z.writestr(mod_name + ".py", src) - z.close() + with zipfile.ZipFile(zip_name, 'a') as z: + for mod_name, src in sample_sources.items(): + z.writestr(mod_name + ".py", src) if verbose: - zip_file = zipfile.ZipFile(zip_name, 'r') - print ('Contents of %r:' % zip_name) - zip_file.printdir() - zip_file.close() + with zipfile.ZipFile(zip_name, 'r') as zip_file: + print ('Contents of %r:' % zip_name) + zip_file.printdir() os.remove(script_name) sys.path.insert(0, zip_name) import test_zipped_doctest diff -r fc7dbba57869 Lib/token.py --- a/Lib/token.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/token.py Sun Nov 09 21:03:10 2014 +0200 @@ -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 -r fc7dbba57869 Lib/trace.py --- a/Lib/trace.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/trace.py Sun Nov 09 21:03:10 2014 +0200 @@ -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 + elif rx_blank.match(line): + outfile.write(" ") else: - outfile.write(" ") - outfile.write(line.expandtabs(8)) - outfile.close() + # 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 -r fc7dbba57869 Lib/xmlrpc/client.py --- a/Lib/xmlrpc/client.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Lib/xmlrpc/client.py Sun Nov 09 21:03:10 2014 +0200 @@ -1017,12 +1017,10 @@ 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() + with BytesIO() as f: + with gzip.GzipFile(mode="wb", fileobj=f, compresslevel=1) as gzf: + gzf.write(data) + encoded = f.getvalue() return encoded ## @@ -1041,14 +1039,12 @@ def gzip_decode(data): """ if not gzip: raise NotImplementedError - f = BytesIO(data) - gzf = gzip.GzipFile(mode="rb", fileobj=f) - try: - decoded = gzf.read() - except OSError: - raise ValueError("invalid data") - f.close() - gzf.close() + with BytesIO(data) as f, \ + gzip.GzipFile(mode="rb", fileobj=f) as gzf: + try: + decoded = gzf.read() + except OSError: + raise ValueError("invalid data") return decoded ## diff -r fc7dbba57869 Parser/asdl_c.py --- a/Parser/asdl_c.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Parser/asdl_c.py Sun Nov 09 21:03:10 2014 +0200 @@ -1252,41 +1252,39 @@ def main(srcfile, dump_module=False): sys.exit(1) if INC_DIR: p = "%s/%s-ast.h" % (INC_DIR, mod.name) - f = open(p, "w") - f.write(auto_gen_msg) - f.write('#include "asdl.h"\n\n') - c = ChainOfVisitors(TypeDefVisitor(f), - StructVisitor(f), - PrototypeVisitor(f), - ) - c.visit(mod) - f.write("PyObject* PyAST_mod2obj(mod_ty t);\n") - f.write("mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode);\n") - f.write("int PyAST_Check(PyObject* obj);\n") - f.close() + with open(p, "w") as f: + f.write(auto_gen_msg) + f.write('#include "asdl.h"\n\n') + c = ChainOfVisitors(TypeDefVisitor(f), + StructVisitor(f), + PrototypeVisitor(f), + ) + c.visit(mod) + f.write("PyObject* PyAST_mod2obj(mod_ty t);\n") + f.write("mod_ty PyAST_obj2mod(PyObject* ast, PyArena* arena, int mode);\n") + f.write("int PyAST_Check(PyObject* obj);\n") if SRC_DIR: p = os.path.join(SRC_DIR, str(mod.name) + "-ast.c") - f = open(p, "w") - f.write(auto_gen_msg) - f.write('#include \n') - f.write('\n') - f.write('#include "Python.h"\n') - f.write('#include "%s-ast.h"\n' % mod.name) - f.write('\n') - f.write("static PyTypeObject AST_type;\n") - v = ChainOfVisitors( - PyTypesDeclareVisitor(f), - PyTypesVisitor(f), - Obj2ModPrototypeVisitor(f), - FunctionVisitor(f), - ObjVisitor(f), - Obj2ModVisitor(f), - ASTModuleVisitor(f), - PartingShots(f), - ) - v.visit(mod) - f.close() + with open(p, "w") as f: + f.write(auto_gen_msg) + f.write('#include \n') + f.write('\n') + f.write('#include "Python.h"\n') + f.write('#include "%s-ast.h"\n' % mod.name) + f.write('\n') + f.write("static PyTypeObject AST_type;\n") + v = ChainOfVisitors( + PyTypesDeclareVisitor(f), + PyTypesVisitor(f), + Obj2ModPrototypeVisitor(f), + FunctionVisitor(f), + ObjVisitor(f), + Obj2ModVisitor(f), + ASTModuleVisitor(f), + PartingShots(f), + ) + v.visit(mod) if __name__ == "__main__": import sys diff -r fc7dbba57869 Tools/demo/markov.py --- a/Tools/demo/markov.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Tools/demo/markov.py Sun Nov 09 21:03:10 2014 +0200 @@ -79,8 +79,8 @@ def test(): else: f = open(filename, 'r') if debug: print('processing', filename, '...') - text = f.read() - f.close() + with f: + text = f.read() paralist = text.split('\n\n') for para in paralist: if debug > 1: print('feeding ...') diff -r fc7dbba57869 Tools/demo/rpython.py --- a/Tools/demo/rpython.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Tools/demo/rpython.py Sun Nov 09 21:03:10 2014 +0200 @@ -22,17 +22,16 @@ def main(): port = int(port[i+1:]) host = host[:i] command = ' '.join(sys.argv[2:]) - s = socket(AF_INET, SOCK_STREAM) - s.connect((host, port)) - s.send(command.encode()) - s.shutdown(SHUT_WR) - reply = b'' - while True: - data = s.recv(BUFSIZE) - if not data: - break - reply += data - print(reply.decode(), end=' ') - s.close() + with socket(AF_INET, SOCK_STREAM) as s: + s.connect((host, port)) + s.send(command.encode()) + s.shutdown(SHUT_WR) + reply = b'' + while True: + data = s.recv(BUFSIZE) + if not data: + break + reply += data + print(reply.decode(), end=' ') main() diff -r fc7dbba57869 Tools/demo/rpythond.py --- a/Tools/demo/rpythond.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Tools/demo/rpythond.py Sun Nov 09 21:03:10 2014 +0200 @@ -26,16 +26,16 @@ def main(): s.listen(1) while True: conn, (remotehost, remoteport) = s.accept() - print('connection from', remotehost, remoteport) - request = b'' - while 1: - data = conn.recv(BUFSIZE) - if not data: - break - request += data - reply = execute(request.decode()) - conn.send(reply.encode()) - conn.close() + with conn: + print('connection from', remotehost, remoteport) + request = b'' + while 1: + data = conn.recv(BUFSIZE) + if not data: + break + request += data + reply = execute(request.decode()) + conn.send(reply.encode()) def execute(request): stdout = sys.stdout diff -r fc7dbba57869 Tools/freeze/checkextensions_win32.py --- a/Tools/freeze/checkextensions_win32.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Tools/freeze/checkextensions_win32.py Sun Nov 09 21:03:10 2014 +0200 @@ -130,7 +130,8 @@ def parse_dsp(dsp): ret = [] dsp_path, dsp_name = os.path.split(dsp) try: - lines = open(dsp, "r").readlines() + with open(dsp, "r") as fp: + lines = fp.readlines() except IOError as msg: sys.stderr.write("%s: %s\n" % (dsp, msg)) return None diff -r fc7dbba57869 Tools/freeze/freeze.py --- a/Tools/freeze/freeze.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Tools/freeze/freeze.py Sun Nov 09 21:03:10 2014 +0200 @@ -144,7 +144,8 @@ def main(): # last option can not be "-i", so this ensures "pos+1" is in range! if sys.argv[pos] == '-i': try: - options = open(sys.argv[pos+1]).read().split() + with open(sys.argv[pos+1]) as infp: + options = infp.read().split() except IOError as why: usage("File name '%s' specified with the -i option " "can not be read - %s" % (sys.argv[pos+1], why) ) @@ -451,13 +452,12 @@ def main(): # generate config.c and Makefile builtins.sort() - infp = open(config_c_in) - outfp = bkfile.open(config_c, 'w') - try: - makeconfig.makeconfig(infp, outfp, builtins) - finally: - outfp.close() - infp.close() + with open(config_c_in) as infp: + outfp = bkfile.open(config_c, 'w') + try: + makeconfig.makeconfig(infp, outfp, builtins) + finally: + outfp.close() cflags = ['$(OPT)'] cppflags = defines + includes diff -r fc7dbba57869 Tools/i18n/msgfmt.py --- a/Tools/i18n/msgfmt.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Tools/i18n/msgfmt.py Sun Nov 09 21:03:10 2014 +0200 @@ -109,7 +109,8 @@ def make(filename, outfile): outfile = os.path.splitext(infile)[0] + '.mo' try: - lines = open(infile, 'rb').readlines() + with open(infile, 'rb') as f: + lines = f.readlines() except IOError as msg: print(msg, file=sys.stderr) sys.exit(1) @@ -199,7 +200,8 @@ def make(filename, outfile): output = generate() try: - open(outfile,"wb").write(output) + with open(outfile,"wb") as f: + f.write(output) except IOError as msg: print(msg, file=sys.stderr) diff -r fc7dbba57869 Tools/i18n/pygettext.py --- a/Tools/i18n/pygettext.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Tools/i18n/pygettext.py Sun Nov 09 21:03:10 2014 +0200 @@ -595,9 +595,8 @@ def main(): # initialize list of strings to exclude if options.excludefilename: try: - fp = open(options.excludefilename) - options.toexclude = fp.readlines() - fp.close() + with open(options.excludefilename) as fp: + options.toexclude = fp.readlines() except IOError: print(_( "Can't read --exclude-file: %s") % options.excludefilename, file=sys.stderr) diff -r fc7dbba57869 Tools/msi/msi.py --- a/Tools/msi/msi.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Tools/msi/msi.py Sun Nov 09 21:03:10 2014 +0200 @@ -39,7 +39,8 @@ except ImportError: pass # Extract current version from Include/patchlevel.h -lines = open(srcdir + "/Include/patchlevel.h").readlines() +with open(srcdir + "/Include/patchlevel.h") as f: + lines = f.readlines() major = minor = micro = level = serial = None levels = { 'PY_RELEASE_LEVEL_ALPHA':0xA, @@ -149,17 +150,16 @@ def build_mingw_lib(lib_file, def_file, (dlltool, dll_file, def_file, mingw_lib) export_match = re.compile(r"^_imp__(.*) in python\d+\.dll").match - f = open(def_file,'w') - f.write("LIBRARY %s\n" % dll_file) - f.write("EXPORTS\n") + with open(def_file,'w') as f: + f.write("LIBRARY %s\n" % dll_file) + f.write("EXPORTS\n") - nm_pipe = os.popen(nm_command) - for line in nm_pipe.readlines(): - m = export_match(line) - if m: - f.write(m.group(1)+"\n") - f.close() - exit = nm_pipe.close() + nm_pipe = os.popen(nm_command) + for line in nm_pipe.readlines(): + m = export_match(line) + if m: + f.write(m.group(1)+"\n") + exit = nm_pipe.close() if exit: print(warning % "nm did not run successfully") @@ -920,23 +920,25 @@ def extract_msvcr100(): def generate_license(): import shutil, glob - out = open("LICENSE.txt", "w") - shutil.copyfileobj(open(os.path.join(srcdir, "LICENSE")), out) - shutil.copyfileobj(open("crtlicense.txt"), out) - for name, pat, file in (("bzip2","bzip2-*", "LICENSE"), - ("openssl", "openssl-*", "LICENSE"), - ("Tcl", "tcl-8*", "license.terms"), - ("Tk", "tk-8*", "license.terms"), - ("Tix", "tix-*", "license.terms")): - out.write("\nThis copy of Python includes a copy of %s, which is licensed under the following terms:\n\n" % name) - dirs = glob.glob(srcdir+"/../"+pat) - if not dirs: - raise ValueError, "Could not find "+srcdir+"/../"+pat - if len(dirs) > 2 and not snapshot: - raise ValueError, "Multiple copies of "+pat - dir = dirs[0] - shutil.copyfileobj(open(os.path.join(dir, file)), out) - out.close() + with open("LICENSE.txt", "w") as out: + with open(os.path.join(srcdir, "LICENSE") as f: + shutil.copyfileobj(f, out) + with open("crtlicense.txt") as f: + shutil.copyfileobj(f, out) + for name, pat, file in (("bzip2","bzip2-*", "LICENSE"), + ("openssl", "openssl-*", "LICENSE"), + ("Tcl", "tcl-8*", "license.terms"), + ("Tk", "tk-8*", "license.terms"), + ("Tix", "tix-*", "license.terms")): + out.write("\nThis copy of Python includes a copy of %s, which is licensed under the following terms:\n\n" % name) + dirs = glob.glob(srcdir+"/../"+pat) + if not dirs: + raise ValueError, "Could not find "+srcdir+"/../"+pat + if len(dirs) > 2 and not snapshot: + raise ValueError, "Multiple copies of "+pat + dir = dirs[0] + with open(os.path.join(dir, file)) as f: + shutil.copyfileobj(f, out) class PyDirectory(Directory): @@ -1358,11 +1360,10 @@ def build_pdbzip(): pdbexclude = ['kill_python.pdb', 'make_buildinfo.pdb', 'make_versioninfo.pdb'] path = "python-%s%s-pdb.zip" % (full_current_version, msilib.arch_ext) - pdbzip = zipfile.ZipFile(path, 'w') - for f in glob.glob1(os.path.join(srcdir, PCBUILD), "*.pdb"): - if f not in pdbexclude and not f.endswith('_d.pdb'): - pdbzip.write(os.path.join(srcdir, PCBUILD, f), f) - pdbzip.close() + with zipfile.ZipFile(path, 'w') as pdbzip: + for f in glob.glob1(os.path.join(srcdir, PCBUILD), "*.pdb"): + if f not in pdbexclude and not f.endswith('_d.pdb'): + pdbzip.write(os.path.join(srcdir, PCBUILD, f), f) db,msiname = build_database() try: diff -r fc7dbba57869 Tools/msi/msilib.py --- a/Tools/msi/msilib.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Tools/msi/msilib.py Sun Nov 09 21:03:10 2014 +0200 @@ -649,7 +649,8 @@ class Dialog: return self.control(name, "CheckBox", x, y, w, h, attr, prop, text, next, None) def pe_type(path): - header = open(path, "rb").read(1000) + with open(path, "rb") as f: + header = f.read(1000) # offset of PE header is at offset 0x3c pe_offset = struct.unpack("%s]' % (addr2rc[addr], rc), end=' ') print(guts, addr2guts[addr]) - f.close() print("%d objects before, %d after" % (before, after)) +def combine(fname): + with open(fname) as f: + combinefile(f) + if __name__ == '__main__': combine(sys.argv[1]) diff -r fc7dbba57869 Tools/scripts/dutree.py --- a/Tools/scripts/dutree.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Tools/scripts/dutree.py Sun Nov 09 21:03:10 2014 +0200 @@ -4,18 +4,18 @@ import os, sys, errno def main(): - p = os.popen('du ' + ' '.join(sys.argv[1:]), 'r') total, d = None, {} - for line in p.readlines(): - i = 0 - while line[i] in '0123456789': i = i+1 - size = eval(line[:i]) - while line[i] in ' \t': i = i+1 - filename = line[i:-1] - comps = filename.split('/') - if comps[0] == '': comps[0] = '/' - if comps[len(comps)-1] == '': del comps[len(comps)-1] - total, d = store(size, comps, total, d) + with os.popen('du ' + ' '.join(sys.argv[1:]), 'r') as p: + for line in p: + i = 0 + while line[i] in '0123456789': i = i+1 + size = eval(line[:i]) + while line[i] in ' \t': i = i+1 + filename = line[i:-1] + comps = filename.split('/') + if comps[0] == '': comps[0] = '/' + if comps[len(comps)-1] == '': del comps[len(comps)-1] + total, d = store(size, comps, total, d) try: display(total, d) except IOError as e: diff -r fc7dbba57869 Tools/scripts/eptags.py --- a/Tools/scripts/eptags.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Tools/scripts/eptags.py Sun Nov 09 21:03:10 2014 +0200 @@ -28,29 +28,30 @@ def treat_file(filename, outfp): except: sys.stderr.write('Cannot open %s\n'%filename) return - charno = 0 - lineno = 0 - tags = [] - size = 0 - while 1: - line = fp.readline() - if not line: - break - lineno = lineno + 1 - m = matcher.search(line) - if m: - tag = m.group(0) + '\177%d,%d\n' % (lineno, charno) - tags.append(tag) - size = size + len(tag) - charno = charno + len(line) + with fp: + charno = 0 + lineno = 0 + tags = [] + size = 0 + while 1: + line = fp.readline() + if not line: + break + lineno = lineno + 1 + m = matcher.search(line) + if m: + tag = m.group(0) + '\177%d,%d\n' % (lineno, charno) + tags.append(tag) + size = size + len(tag) + charno = charno + len(line) outfp.write('\f\n%s,%d\n' % (filename,size)) for tag in tags: outfp.write(tag) def main(): - outfp = open('TAGS', 'w') - for filename in sys.argv[1:]: - treat_file(filename, outfp) + with open('TAGS', 'w') as outfp: + for filename in sys.argv[1:]: + treat_file(filename, outfp) if __name__=="__main__": main() diff -r fc7dbba57869 Tools/scripts/finddiv.py --- a/Tools/scripts/finddiv.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Tools/scripts/finddiv.py Sun Nov 09 21:03:10 2014 +0200 @@ -55,17 +55,17 @@ def process(filename, listnames): except IOError as msg: sys.stderr.write("Can't open: %s\n" % msg) return 1 - g = tokenize.generate_tokens(fp.readline) - lastrow = None - for type, token, (row, col), end, line in g: - if token in ("/", "/="): - if listnames: - print(filename) - break - if row != lastrow: - lastrow = row - print("%s:%d:%s" % (filename, row, line), end=' ') - fp.close() + with fp: + g = tokenize.generate_tokens(fp.readline) + lastrow = None + for type, token, (row, col), end, line in g: + if token in ("/", "/="): + if listnames: + print(filename) + break + if row != lastrow: + lastrow = row + print("%s:%d:%s" % (filename, row, line), end=' ') def processdir(dir, listnames): try: diff -r fc7dbba57869 Tools/scripts/fixcid.py --- a/Tools/scripts/fixcid.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Tools/scripts/fixcid.py Sun Nov 09 21:03:10 2014 +0200 @@ -279,36 +279,36 @@ def addsubst(substfile): except IOError as msg: err(substfile + ': cannot read substfile: ' + str(msg) + '\n') sys.exit(1) - lineno = 0 - while 1: - line = fp.readline() - if not line: break - lineno = lineno + 1 - try: - i = line.index('#') - except ValueError: - i = -1 # Happens to delete trailing \n - words = line[:i].split() - if not words: continue - if len(words) == 3 and words[0] == 'struct': - words[:2] = [words[0] + ' ' + words[1]] - elif len(words) != 2: - err(substfile + '%s:%r: warning: bad line: %r' % (substfile, lineno, line)) - continue - if Reverse: - [value, key] = words - else: - [key, value] = words - if value[0] == '*': - value = value[1:] - if key[0] == '*': - key = key[1:] - NotInComment[key] = value - if key in Dict: - err('%s:%r: warning: overriding: %r %r\n' % (substfile, lineno, key, value)) - err('%s:%r: warning: previous: %r\n' % (substfile, lineno, Dict[key])) - Dict[key] = value - fp.close() + with fp: + lineno = 0 + while 1: + line = fp.readline() + if not line: break + lineno = lineno + 1 + try: + i = line.index('#') + except ValueError: + i = -1 # Happens to delete trailing \n + words = line[:i].split() + if not words: continue + if len(words) == 3 and words[0] == 'struct': + words[:2] = [words[0] + ' ' + words[1]] + elif len(words) != 2: + err(substfile + '%s:%r: warning: bad line: %r' % (substfile, lineno, line)) + continue + if Reverse: + [value, key] = words + else: + [key, value] = words + if value[0] == '*': + value = value[1:] + if key[0] == '*': + key = key[1:] + NotInComment[key] = value + if key in Dict: + err('%s:%r: warning: overriding: %r %r\n' % (substfile, lineno, key, value)) + err('%s:%r: warning: previous: %r\n' % (substfile, lineno, Dict[key])) + Dict[key] = value if __name__ == '__main__': main() diff -r fc7dbba57869 Tools/scripts/fixdiv.py --- a/Tools/scripts/fixdiv.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Tools/scripts/fixdiv.py Sun Nov 09 21:03:10 2014 +0200 @@ -185,21 +185,21 @@ def readwarnings(warningsfile): sys.stderr.write("can't open: %s\n" % msg) return warnings = {} - while 1: - line = f.readline() - if not line: - break - m = prog.match(line) - if not m: - if line.find("division") >= 0: - sys.stderr.write("Warning: ignored input " + line) - continue - filename, lineno, what = m.groups() - list = warnings.get(filename) - if list is None: - warnings[filename] = list = [] - list.append((int(lineno), sys.intern(what))) - f.close() + with f: + while 1: + line = f.readline() + if not line: + break + m = prog.match(line) + if not m: + if line.find("division") >= 0: + sys.stderr.write("Warning: ignored input " + line) + continue + filename, lineno, what = m.groups() + list = warnings.get(filename) + if list is None: + warnings[filename] = list = [] + list.append((int(lineno), sys.intern(what))) return warnings def process(filename, list): @@ -210,84 +210,84 @@ def process(filename, list): except IOError as msg: sys.stderr.write("can't open: %s\n" % msg) return 1 - print("Index:", filename) - f = FileContext(fp) - list.sort() - index = 0 # list[:index] has been processed, list[index:] is still to do - g = tokenize.generate_tokens(f.readline) - while 1: - startlineno, endlineno, slashes = lineinfo = scanline(g) - if startlineno is None: - break - assert startlineno <= endlineno is not None - orphans = [] - while index < len(list) and list[index][0] < startlineno: - orphans.append(list[index]) - index += 1 - if orphans: - reportphantomwarnings(orphans, f) - warnings = [] - while index < len(list) and list[index][0] <= endlineno: - warnings.append(list[index]) - index += 1 - if not slashes and not warnings: - pass - elif slashes and not warnings: - report(slashes, "No conclusive evidence") - elif warnings and not slashes: - reportphantomwarnings(warnings, f) - else: - if len(slashes) > 1: - if not multi_ok: - rows = [] - lastrow = None - for (row, col), line in slashes: - if row == lastrow: - continue - rows.append(row) - lastrow = row - assert rows - if len(rows) == 1: - print("*** More than one / operator in line", rows[0]) + with fp: + print("Index:", filename) + f = FileContext(fp) + list.sort() + index = 0 # list[:index] has been processed, list[index:] is still to do + g = tokenize.generate_tokens(f.readline) + while 1: + startlineno, endlineno, slashes = lineinfo = scanline(g) + if startlineno is None: + break + assert startlineno <= endlineno is not None + orphans = [] + while index < len(list) and list[index][0] < startlineno: + orphans.append(list[index]) + index += 1 + if orphans: + reportphantomwarnings(orphans, f) + warnings = [] + while index < len(list) and list[index][0] <= endlineno: + warnings.append(list[index]) + index += 1 + if not slashes and not warnings: + pass + elif slashes and not warnings: + report(slashes, "No conclusive evidence") + elif warnings and not slashes: + reportphantomwarnings(warnings, f) + else: + if len(slashes) > 1: + if not multi_ok: + rows = [] + lastrow = None + for (row, col), line in slashes: + if row == lastrow: + continue + rows.append(row) + lastrow = row + assert rows + if len(rows) == 1: + print("*** More than one / operator in line", rows[0]) + else: + print("*** More than one / operator per statement", end=' ') + print("in lines %d-%d" % (rows[0], rows[-1])) + intlong = [] + floatcomplex = [] + bad = [] + for lineno, what in warnings: + if what in ("int", "long"): + intlong.append(what) + elif what in ("float", "complex"): + floatcomplex.append(what) else: - print("*** More than one / operator per statement", end=' ') - print("in lines %d-%d" % (rows[0], rows[-1])) - intlong = [] - floatcomplex = [] - bad = [] - for lineno, what in warnings: - if what in ("int", "long"): - intlong.append(what) - elif what in ("float", "complex"): - floatcomplex.append(what) - else: - bad.append(what) - lastrow = None - for (row, col), line in slashes: - if row == lastrow: - continue - lastrow = row - line = chop(line) - if line[col:col+1] != "/": - print("*** Can't find the / operator in line %d:" % row) - print("*", line) - continue - if bad: - print("*** Bad warning for line %d:" % row, bad) - print("*", line) - elif intlong and not floatcomplex: - print("%dc%d" % (row, row)) - print("<", line) - print("---") - print(">", line[:col] + "/" + line[col:]) - elif floatcomplex and not intlong: - print("True division / operator at line %d:" % row) - print("=", line) - elif intlong and floatcomplex: - print("*** Ambiguous / operator (%s, %s) at line %d:" % ( - "|".join(intlong), "|".join(floatcomplex), row)) - print("?", line) - fp.close() + bad.append(what) + lastrow = None + for (row, col), line in slashes: + if row == lastrow: + continue + lastrow = row + line = chop(line) + if line[col:col+1] != "/": + print("*** Can't find the / operator in line %d:" % row) + print("*", line) + continue + if bad: + print("*** Bad warning for line %d:" % row, bad) + print("*", line) + elif intlong and not floatcomplex: + print("%dc%d" % (row, row)) + print("<", line) + print("---") + print(">", line[:col] + "/" + line[col:]) + elif floatcomplex and not intlong: + print("True division / operator at line %d:" % row) + print("=", line) + elif intlong and floatcomplex: + print("*** Ambiguous / operator (%s, %s) at line %d:" % + ("|".join(intlong), "|".join(floatcomplex), row)) + print("?", line) def reportphantomwarnings(warnings, f): blocks = [] diff -r fc7dbba57869 Tools/scripts/fixheader.py --- a/Tools/scripts/fixheader.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Tools/scripts/fixheader.py Sun Nov 09 21:03:10 2014 +0200 @@ -15,8 +15,8 @@ def process(filename): except IOError as msg: sys.stderr.write('%s: can\'t open: %s\n' % (filename, str(msg))) return - data = f.read() - f.close() + with f: + data = f.read() if data[:2] != '/*': sys.stderr.write('%s does not begin with C comment\n' % filename) return @@ -31,19 +31,19 @@ def process(filename): if ord(c)<=0x80 and c.isalnum(): magic = magic + c.upper() else: magic = magic + '_' - sys.stdout = f - print('#ifndef', magic) - print('#define', magic) - print('#ifdef __cplusplus') - print('extern "C" {') - print('#endif') - print() - f.write(data) - print() - print('#ifdef __cplusplus') - print('}') - print('#endif') - print('#endif /*', '!'+magic, '*/') + with f: + print('#ifndef', magic, file=f) + print('#define', magic, file=f) + print('#ifdef __cplusplus', file=f) + print('extern "C" {', file=f) + print('#endif', file=f) + print(file=f) + f.write(data) + print(file=f) + print('#ifdef __cplusplus', file=f) + print('}', file=f) + print('#endif', file=f) + print('#endif /*', '!'+magic, '*/', file=f) if __name__ == '__main__': main() diff -r fc7dbba57869 Tools/scripts/fixnotice.py --- a/Tools/scripts/fixnotice.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Tools/scripts/fixnotice.py Sun Nov 09 21:03:10 2014 +0200 @@ -73,22 +73,19 @@ def main(): elif opt == '--dry-run': DRYRUN = 1 elif opt == '--oldnotice': - fp = open(arg) - OLD_NOTICE = fp.read() - fp.close() + with open(arg) as fp: + OLD_NOTICE = fp.read() elif opt == '--newnotice': - fp = open(arg) - NEW_NOTICE = fp.read() - fp.close() + with open(arg) as fp: + NEW_NOTICE = fp.read() for arg in args: process(arg) def process(file): - f = open(file) - data = f.read() - f.close() + with open(file) as f: + data = f.read() i = data.find(OLD_NOTICE) if i < 0: if VERBOSE: @@ -102,9 +99,8 @@ def process(file): data = data[:i] + NEW_NOTICE + data[i+len(OLD_NOTICE):] new = file + ".new" backup = file + ".bak" - f = open(new, "w") - f.write(data) - f.close() + with open(new, "w") as f: + f.write(data) os.rename(file, backup) os.rename(new, file) diff -r fc7dbba57869 Tools/scripts/fixps.py --- a/Tools/scripts/fixps.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Tools/scripts/fixps.py Sun Nov 09 21:03:10 2014 +0200 @@ -14,20 +14,18 @@ def main(): except IOError as msg: print(filename, ': can\'t open :', msg) continue - line = f.readline() - if not re.match('^#! */usr/local/bin/python', line): - print(filename, ': not a /usr/local/bin/python script') - f.close() - continue - rest = f.read() - f.close() + with f: + line = f.readline() + if not re.match('^#! */usr/local/bin/python', line): + print(filename, ': not a /usr/local/bin/python script') + continue + rest = f.read() line = re.sub('/usr/local/bin/python', '/usr/bin/env python', line) print(filename, ':', repr(line)) - f = open(filename, "w") - f.write(line) - f.write(rest) - f.close() + with open(filename, "w") as f: + f.write(line) + f.write(rest) if __name__ == '__main__': main() diff -r fc7dbba57869 Tools/scripts/ftpmirror.py --- a/Tools/scripts/ftpmirror.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Tools/scripts/ftpmirror.py Sun Nov 09 21:03:10 2014 +0200 @@ -113,7 +113,8 @@ def mirrorsubdir(f, localdir): return infofilename = os.path.join(localdir, '.mirrorinfo') try: - text = open(infofilename, 'r').read() + with open(infofilename, 'r') as fd: + text = fd.read() except IOError as msg: text = '{}' try: @@ -388,12 +389,11 @@ def writedict(dict, filename): os.unlink(backup) except OSError: pass - fp = open(tempname, 'w') - fp.write('{\n') - for key, value in dict.items(): - fp.write('%r: %r,\n' % (key, value)) - fp.write('}\n') - fp.close() + with open(tempname, 'w') as fp: + fp.write('{\n') + for key, value in dict.items(): + fp.write('%r: %r,\n' % (key, value)) + fp.write('}\n') try: os.rename(filename, backup) except OSError: diff -r fc7dbba57869 Tools/scripts/get-remote-certificate.py --- a/Tools/scripts/get-remote-certificate.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Tools/scripts/get-remote-certificate.py Sun Nov 09 21:03:10 2014 +0200 @@ -29,9 +29,8 @@ def fetch_server_certificate (host, port return None else: tn = tempfile.mktemp() - fp = open(tn, "wb") - fp.write(m.group(1) + b"\n") - fp.close() + with open(tn, "wb") as fp: + fp.write(m.group(1) + b"\n") try: tn2 = (outfile or tempfile.mktemp()) status, output = subproc(r'openssl x509 -in "%s" -out "%s"' % @@ -39,9 +38,8 @@ def fetch_server_certificate (host, port if status != 0: raise RuntimeError('OpenSSL x509 failed with status %s and ' 'output: %r' % (status, output)) - fp = open(tn2, 'rb') - data = fp.read() - fp.close() + with open(tn2, 'rb') as fp: + data = fp.read() os.unlink(tn2) return data finally: @@ -49,9 +47,8 @@ def fetch_server_certificate (host, port if sys.platform.startswith("win"): tfile = tempfile.mktemp() - fp = open(tfile, "w") - fp.write("quit\n") - fp.close() + with open(tfile, "w") as fp: + fp.write("quit\n") try: status, output = subproc( 'openssl s_client -connect "%s:%s" -showcerts < "%s"' % diff -r fc7dbba57869 Tools/scripts/gprof2html.py --- a/Tools/scripts/gprof2html.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Tools/scripts/gprof2html.py Sun Nov 09 21:03:10 2014 +0200 @@ -24,14 +24,7 @@ def add_escapes(filename): for line in fp: yield cgi.escape(line) - -def main(): - filename = "gprof.out" - if sys.argv[1:]: - filename = sys.argv[1] - outputfilename = filename + ".html" - input = add_escapes(filename) - output = open(outputfilename, "w") +def gprof2html(input, output, filename): output.write(header % filename) for line in input: output.write(line) @@ -74,7 +67,16 @@ def main(): part = '%s' % (part, part) output.write(part) output.write(trailer) - output.close() + + +def main(): + filename = "gprof.out" + if sys.argv[1:]: + filename = sys.argv[1] + outputfilename = filename + ".html" + input = add_escapes(filename) + with open(outputfilename, "w") as output: + gprof2html(input, output, filename) webbrowser.open("file:" + os.path.abspath(outputfilename)) if __name__ == '__main__': diff -r fc7dbba57869 Tools/scripts/h2py.py --- a/Tools/scripts/h2py.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Tools/scripts/h2py.py Sun Nov 09 21:03:10 2014 +0200 @@ -69,23 +69,21 @@ def main(): sys.stdout.write('# Generated by h2py from stdin\n') process(sys.stdin, sys.stdout) else: - fp = open(filename, 'r') - outfile = os.path.basename(filename) - i = outfile.rfind('.') - if i > 0: outfile = outfile[:i] - modname = outfile.upper() - outfile = modname + '.py' - outfp = open(outfile, 'w') - outfp.write('# Generated by h2py from %s\n' % filename) - filedict = {} - for dir in searchdirs: - if filename[:len(dir)] == dir: - filedict[filename[len(dir)+1:]] = None # no '/' trailing - importable[filename[len(dir)+1:]] = modname - break - process(fp, outfp) - outfp.close() - fp.close() + with open(filename, 'r') as fp: + outfile = os.path.basename(filename) + i = outfile.rfind('.') + if i > 0: outfile = outfile[:i] + modname = outfile.upper() + outfile = modname + '.py' + with open(outfile, 'w') as outfp: + outfp.write('# Generated by h2py from %s\n' % filename) + filedict = {} + for dir in searchdirs: + if filename[:len(dir)] == dir: + filedict[filename[len(dir)+1:]] = None # no '/' trailing + importable[filename[len(dir)+1:]] = modname + break + process(fp, outfp) def pytify(body): # replace ignored patterns by spaces @@ -161,9 +159,10 @@ def process(fp, outfp, env = {}): except IOError: pass if inclfp: - outfp.write( - '\n# Included from %s\n' % filename) - process(inclfp, outfp, env) + with inclfp: + outfp.write( + '\n# Included from %s\n' % filename) + process(inclfp, outfp, env) else: sys.stderr.write('Warning - could not find file %s\n' % filename) diff -r fc7dbba57869 Tools/scripts/ifdef.py --- a/Tools/scripts/ifdef.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Tools/scripts/ifdef.py Sun Nov 09 21:03:10 2014 +0200 @@ -45,9 +45,8 @@ def main(): if filename == '-': process(sys.stdin, sys.stdout) else: - f = open(filename, 'r') - process(f, sys.stdout) - f.close() + with open(filename, 'r') as f: + process(f, sys.stdout) def process(fpi, fpo): keywords = ('if', 'ifdef', 'ifndef', 'else', 'endif') diff -r fc7dbba57869 Tools/scripts/md5sum.py --- a/Tools/scripts/md5sum.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Tools/scripts/md5sum.py Sun Nov 09 21:03:10 2014 +0200 @@ -47,10 +47,10 @@ def printsum(filename, out=sys.stdout): except IOError as msg: sys.stderr.write('%s: Can\'t open: %s\n' % (filename, msg)) return 1 - if fnfilter: - filename = fnfilter(filename) - sts = printsumfp(fp, filename, out) - fp.close() + with fp: + if fnfilter: + filename = fnfilter(filename) + sts = printsumfp(fp, filename, out) return sts def printsumfp(fp, filename, out=sys.stdout): diff -r fc7dbba57869 Tools/scripts/mkreal.py --- a/Tools/scripts/mkreal.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Tools/scripts/mkreal.py Sun Nov 09 21:03:10 2014 +0200 @@ -18,14 +18,13 @@ def mkrealfile(name): st = os.stat(name) # Get the mode mode = S_IMODE(st[ST_MODE]) linkto = os.readlink(name) # Make sure again it's a symlink - f_in = open(name, 'r') # This ensures it's a file - os.unlink(name) - f_out = open(name, 'w') - while 1: - buf = f_in.read(BUFSIZE) - if not buf: break - f_out.write(buf) - del f_out # Flush data to disk before changing mode + with open(name, 'rb') as f_in: # This ensures it's a file + os.unlink(name) + with open(name, 'wb') as f_out: + while 1: + buf = f_in.read(BUFSIZE) + if not buf: break + f_out.write(buf) os.chmod(name, mode) def mkrealdir(name): diff -r fc7dbba57869 Tools/scripts/nm2def.py --- a/Tools/scripts/nm2def.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Tools/scripts/nm2def.py Sun Nov 09 21:03:10 2014 +0200 @@ -42,7 +42,8 @@ NM = 'nm -p -g %s' def symbols(lib=PYTHONLIB,types=('T','C','D')): - lines = os.popen(NM % lib).readlines() + with os.popen(NM % lib) as pipe: + lines = pipe.readlines() lines = [s.strip() for s in lines] symbols = {} for line in lines: @@ -97,7 +98,7 @@ def main(): exports = export_list(s) f = sys.stdout # open('PC/python_nt.def','w') f.write(DEF_TEMPLATE % (exports)) - f.close() + # f.close() if __name__ == '__main__': main() diff -r fc7dbba57869 Tools/scripts/objgraph.py --- a/Tools/scripts/objgraph.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Tools/scripts/objgraph.py Sun Nov 09 21:03:10 2014 +0200 @@ -180,7 +180,8 @@ def main(): if filename == '-': readinput(sys.stdin) else: - readinput(open(filename, 'r')) + with open(filename, 'r') as f: + readinput(f) # warndups() # diff -r fc7dbba57869 Tools/scripts/parseentities.py --- a/Tools/scripts/parseentities.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Tools/scripts/parseentities.py Sun Nov 09 21:03:10 2014 +0200 @@ -50,13 +50,15 @@ def writefile(f,defs): if __name__ == '__main__': if len(sys.argv) > 1: - infile = open(sys.argv[1]) + with open(sys.argv[1]) as infile: + text = infile.read() else: - infile = sys.stdin + text = sys.stdin.read() + + defs = parse(text) + if len(sys.argv) > 2: - outfile = open(sys.argv[2],'w') + with open(sys.argv[2],'w') as outfile: + writefile(outfile, defs) else: - outfile = sys.stdout - text = infile.read() - defs = parse(text) - writefile(outfile,defs) + writefile(sys.stdout, defs) diff -r fc7dbba57869 Tools/scripts/pathfix.py --- a/Tools/scripts/pathfix.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Tools/scripts/pathfix.py Sun Nov 09 21:03:10 2014 +0200 @@ -97,29 +97,27 @@ def fix(filename): except IOError as msg: err('%s: cannot open: %r\n' % (filename, msg)) return 1 - line = f.readline() - fixed = fixline(line) - if line == fixed: - rep(filename+': no change\n') - f.close() - return - head, tail = os.path.split(filename) - tempname = os.path.join(head, '@' + tail) - try: - g = open(tempname, 'wb') - except IOError as msg: - f.close() - err('%s: cannot create: %r\n' % (tempname, msg)) - return 1 - rep(filename + ': updating\n') - g.write(fixed) - BUFSIZE = 8*1024 - while 1: - buf = f.read(BUFSIZE) - if not buf: break - g.write(buf) - g.close() - f.close() + with f: + line = f.readline() + fixed = fixline(line) + if line == fixed: + rep(filename+': no change\n') + return + head, tail = os.path.split(filename) + tempname = os.path.join(head, '@' + tail) + try: + g = open(tempname, 'wb') + except IOError as msg: + err('%s: cannot create: %r\n' % (tempname, msg)) + return 1 + with g: + rep(filename + ': updating\n') + g.write(fixed) + BUFSIZE = 8*1024 + while 1: + buf = f.read(BUFSIZE) + if not buf: break + g.write(buf) # Finishing touch -- move files diff -r fc7dbba57869 Tools/scripts/pdeps.py --- a/Tools/scripts/pdeps.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Tools/scripts/pdeps.py Sun Nov 09 21:03:10 2014 +0200 @@ -64,29 +64,28 @@ m_from = re.compile('^[ \t]*import[ \t]+ # Collect data from one file # def process(filename, table): - fp = open(filename, 'r') - mod = os.path.basename(filename) - if mod[-3:] == '.py': - mod = mod[:-3] - table[mod] = list = [] - while 1: - line = fp.readline() - if not line: break - while line[-1:] == '\\': - nextline = fp.readline() - if not nextline: break - line = line[:-1] + nextline - m_found = m_import.match(line) or m_from.match(line) - if m_found: - (a, b), (a1, b1) = m_found.regs[:2] - else: continue - words = line[a1:b1].split(',') - # print '#', line, words - for word in words: - word = word.strip() - if word not in list: - list.append(word) - fp.close() + with open(filename, 'r') as fp: + mod = os.path.basename(filename) + if mod[-3:] == '.py': + mod = mod[:-3] + table[mod] = list = [] + while 1: + line = fp.readline() + if not line: break + while line[-1:] == '\\': + nextline = fp.readline() + if not nextline: break + line = line[:-1] + nextline + m_found = m_import.match(line) or m_from.match(line) + if m_found: + (a, b), (a1, b1) = m_found.regs[:2] + else: continue + words = line[a1:b1].split(',') + # print '#', line, words + for word in words: + word = word.strip() + if word not in list: + list.append(word) # Compute closure (this is in fact totally general) diff -r fc7dbba57869 Tools/scripts/ptags.py --- a/Tools/scripts/ptags.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Tools/scripts/ptags.py Sun Nov 09 21:03:10 2014 +0200 @@ -19,9 +19,9 @@ def main(): for filename in args: treat_file(filename) if tags: - fp = open('tags', 'w') - tags.sort() - for s in tags: fp.write(s) + with open('tags', 'w') as fp: + tags.sort() + for s in tags: fp.write(s) expr = '^[ \t]*(def|class)[ \t]+([a-zA-Z0-9_]+)[ \t]*[:\(]' @@ -33,21 +33,22 @@ def treat_file(filename): except: sys.stderr.write('Cannot open %s\n' % filename) return - base = os.path.basename(filename) - if base[-3:] == '.py': - base = base[:-3] - s = base + '\t' + filename + '\t' + '1\n' - tags.append(s) - while 1: - line = fp.readline() - if not line: - break - m = matcher.match(line) - if m: - content = m.group(0) - name = m.group(2) - s = name + '\t' + filename + '\t/^' + content + '/\n' - tags.append(s) + with fp: + base = os.path.basename(filename) + if base[-3:] == '.py': + base = base[:-3] + s = base + '\t' + filename + '\t' + '1\n' + tags.append(s) + while 1: + line = fp.readline() + if not line: + break + m = matcher.match(line) + if m: + content = m.group(0) + name = m.group(2) + s = name + '\t' + filename + '\t/^' + content + '/\n' + tags.append(s) if __name__ == '__main__': main() diff -r fc7dbba57869 Tools/scripts/rgrep.py --- a/Tools/scripts/rgrep.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Tools/scripts/rgrep.py Sun Nov 09 21:03:10 2014 +0200 @@ -30,29 +30,30 @@ def main(): f = open(filename) except IOError as msg: usage("can't open %r: %s" % (filename, msg), 1) - f.seek(0, 2) - pos = f.tell() - leftover = None - while pos > 0: - size = min(pos, bufsize) - pos = pos - size - f.seek(pos) - buffer = f.read(size) - lines = buffer.split("\n") - del buffer - if leftover is None: - if not lines[-1]: - del lines[-1] - else: - lines[-1] = lines[-1] + leftover - if pos > 0: - leftover = lines[0] - del lines[0] - else: - leftover = None - for line in reversed(lines): - if prog.search(line): - print(line) + with f: + f.seek(0, 2) + pos = f.tell() + leftover = None + while pos > 0: + size = min(pos, bufsize) + pos = pos - size + f.seek(pos) + buffer = f.read(size) + lines = buffer.split("\n") + del buffer + if leftover is None: + if not lines[-1]: + del lines[-1] + else: + lines[-1] = lines[-1] + leftover + if pos > 0: + leftover = lines[0] + del lines[0] + else: + leftover = None + for line in reversed(lines): + if prog.search(line): + print(line) def usage(msg, code=2): diff -r fc7dbba57869 Tools/scripts/svneol.py --- a/Tools/scripts/svneol.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Tools/scripts/svneol.py Sun Nov 09 21:03:10 2014 +0200 @@ -39,7 +39,8 @@ import subprocess def propfiles(root, fn): default = os.path.join(root, ".svn", "props", fn + ".svn-work") try: - format = int(open(os.path.join(root, ".svn", "format")).read().strip()) + with open(os.path.join(root, ".svn", "format")) as f: + format = int(f.read().strip()) except IOError: return [] if format in (8, 9): @@ -60,27 +61,27 @@ def proplist(root, fn): # no properties file: not under version control, # or no properties set continue - while True: - # key-value pairs, of the form - # K - # NL - # V length - # NL - # END - line = f.readline() - if line.startswith("END"): - break - assert line.startswith("K ") - L = int(line.split()[1]) - key = f.read(L) - result.append(key) - f.readline() - line = f.readline() - assert line.startswith("V ") - L = int(line.split()[1]) - value = f.read(L) - f.readline() - f.close() + with f: + while True: + # key-value pairs, of the form + # K + # NL + # V length + # NL + # END + line = f.readline() + if line.startswith("END"): + break + assert line.startswith("K ") + L = int(line.split()[1]) + key = f.read(L) + result.append(key) + f.readline() + line = f.readline() + assert line.startswith("V ") + L = int(line.split()[1]) + value = f.read(L) + f.readline() return result diff -r fc7dbba57869 Tools/scripts/texi2html.py --- a/Tools/scripts/texi2html.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Tools/scripts/texi2html.py Sun Nov 09 21:03:10 2014 +0200 @@ -118,11 +118,10 @@ class HTMLNode: self.lines.append(line) def flush(self): - fp = open(self.dirname + '/' + makefile(self.name), 'w') - fp.write(self.prologue) - fp.write(self.text) - fp.write(self.epilogue) - fp.close() + with open(self.dirname + '/' + makefile(self.name), 'w') as fp: + fp.write(self.prologue) + fp.write(self.text) + fp.write(self.epilogue) def link(self, label, nodename, rel=None, rev=None): if nodename: @@ -558,14 +557,14 @@ class TexinfoParser: except IOError as msg: print('*** Can\'t open include file', repr(file)) return - print('!'*self.debugging, '--> file', repr(file)) - save_done = self.done - save_skip = self.skip - save_stack = self.stack - self.includedepth = self.includedepth + 1 - self.parserest(fp, 0) - self.includedepth = self.includedepth - 1 - fp.close() + with fp: + print('!'*self.debugging, '--> file', repr(file)) + save_done = self.done + save_skip = self.skip + save_stack = self.stack + self.includedepth = self.includedepth + 1 + self.parserest(fp, 0) + self.includedepth = self.includedepth - 1 self.done = save_done self.skip = save_skip self.stack = save_stack @@ -1770,78 +1769,75 @@ class HTMLHelp: # PROJECT FILE try: - fp = open(projectfile,'w') - print('[OPTIONS]', file=fp) - print('Auto Index=Yes', file=fp) - print('Binary TOC=No', file=fp) - print('Binary Index=Yes', file=fp) - print('Compatibility=1.1', file=fp) - print('Compiled file=' + resultfile + '', file=fp) - print('Contents file=' + contentfile + '', file=fp) - print('Default topic=' + defaulttopic + '', file=fp) - print('Error log file=ErrorLog.log', file=fp) - print('Index file=' + indexfile + '', file=fp) - print('Title=' + title + '', file=fp) - print('Display compile progress=Yes', file=fp) - print('Full-text search=Yes', file=fp) - print('Default window=main', file=fp) - print('', file=fp) - print('[WINDOWS]', file=fp) - print('main=,"' + contentfile + '","' + indexfile - + '","","",,,,,0x23520,222,0x1046,[10,10,780,560],' - '0xB0000,,,,,,0', file=fp) - print('', file=fp) - print('[FILES]', file=fp) - print('', file=fp) - self.dumpfiles(fp) - fp.close() + with open(projectfile, 'w') as fp: + print('[OPTIONS]', file=fp) + print('Auto Index=Yes', file=fp) + print('Binary TOC=No', file=fp) + print('Binary Index=Yes', file=fp) + print('Compatibility=1.1', file=fp) + print('Compiled file=' + resultfile + '', file=fp) + print('Contents file=' + contentfile + '', file=fp) + print('Default topic=' + defaulttopic + '', file=fp) + print('Error log file=ErrorLog.log', file=fp) + print('Index file=' + indexfile + '', file=fp) + print('Title=' + title + '', file=fp) + print('Display compile progress=Yes', file=fp) + print('Full-text search=Yes', file=fp) + print('Default window=main', file=fp) + print('', file=fp) + print('[WINDOWS]', file=fp) + print('main=,"' + contentfile + '","' + indexfile + + '","","",,,,,0x23520,222,0x1046,[10,10,780,560],' + '0xB0000,,,,,,0', file=fp) + print('', file=fp) + print('[FILES]', file=fp) + print('', file=fp) + self.dumpfiles(fp) except IOError as msg: print(projectfile, ':', msg) sys.exit(1) # CONTENT FILE try: - fp = open(contentfile,'w') - print('', file=fp) - print('', file=fp) - print('', file=fp) - print('', file=fp) - print('', file=fp) - print('', file=fp) - print('', file=fp) - print('', file=fp) - print(' ', file=fp) - print(' ', file=fp) - print(' ', file=fp) - print(' ', file=fp) - print(' ', file=fp) - self.dumpnodes(fp) - print('', file=fp) - print('', file=fp) - fp.close() + with open(contentfile, 'w') as fp: + print('', file=fp) + print('', file=fp) + print('', file=fp) + print('', file=fp) + print('', file=fp) + print('', file=fp) + print('', file=fp) + print('', file=fp) + print(' ', file=fp) + print(' ', file=fp) + print(' ', file=fp) + print(' ', file=fp) + print(' ', file=fp) + self.dumpnodes(fp) + print('', file=fp) + print('', file=fp) except IOError as msg: print(contentfile, ':', msg) sys.exit(1) # INDEX FILE try: - fp = open(indexfile ,'w') - print('', file=fp) - print('', file=fp) - print('', file=fp) - print('', file=fp) - print('', file=fp) - print('', file=fp) - print('', file=fp) - print('', file=fp) - print('', file=fp) - print('', file=fp) - self.dumpindex(fp) - print('', file=fp) - print('', file=fp) - fp.close() + with open(indexfile, 'w') as fp: + print('', file=fp) + print('', file=fp) + print('', file=fp) + print('', file=fp) + print('', file=fp) + print('', file=fp) + print('', file=fp) + print('', file=fp) + print('', file=fp) + print('', file=fp) + self.dumpindex(fp) + print('', file=fp) + print('', file=fp) except IOError as msg: print(indexfile , ':', msg) sys.exit(1) @@ -2064,8 +2060,8 @@ def test(): print(file, ':', msg) sys.exit(1) - parser.parse(fp) - fp.close() + with fp: + parser.parse(fp) parser.report() htmlhelp.finalize() diff -r fc7dbba57869 Tools/scripts/treesync.py --- a/Tools/scripts/treesync.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Tools/scripts/treesync.py Sun Nov 09 21:03:10 2014 +0200 @@ -23,7 +23,7 @@ entry in the master tree are synchronize """ -import os, sys, stat, getopt +import os, sys, stat, getopt, shutil # Interactivity options default_answer = "ask" @@ -96,7 +96,9 @@ def process(slave, master): subdirs.append((slavename, mastername)) if cvsdir: entries = os.path.join(cvsdir, "Entries") - for e in open(entries).readlines(): + with open(entries) as f: + lines = f.readlines() + for e in lines: words = e.split('/') if words[0] == '' and words[1:]: name = words[1] @@ -110,47 +112,45 @@ def compare(slave, master): try: sf = open(slave, 'r') except IOError: - sf = None - try: - mf = open(master, 'rb') - except IOError: - mf = None - if not sf: - if not mf: + try: + with open(master, 'rb'): pass + except IOError: print("Neither master nor slave exists", master) return print("Creating missing slave", slave) copy(master, slave, answer=create_files) return - if not mf: - print("Not updating missing master", master) - return - if sf and mf: - if identical(sf, mf): + with sf: + try: + mf = open(master, 'rb') + except IOError: + print("Not updating missing master", master) return - sft = mtime(sf) - mft = mtime(mf) - if mft > sft: - # Master is newer -- copy master to slave - sf.close() - mf.close() - print("Master ", master) - print("is newer than slave", slave) - copy(master, slave, answer=write_slave) - return - # Slave is newer -- copy slave to master - print("Slave is", sft-mft, "seconds newer than master") - # But first check what to do about CRLF - mf.seek(0) - fun = funnychars(mf) - mf.close() - sf.close() - if fun: - print("***UPDATING MASTER (BINARY COPY)***") - copy(slave, master, "rb", answer=write_master) - else: - print("***UPDATING MASTER***") - copy(slave, master, "r", answer=write_master) + with mf: + if identical(sf, mf): + return + sft = mtime(sf) + mft = mtime(mf) + if mft > sft: + # Master is newer -- copy master to slave + print("Master ", master) + print("is newer than slave", slave) + args = (master, slave) + answer = write_slave + else: + # Slave is newer -- copy slave to master + print("Slave is", sft-mft, "seconds newer than master") + # But first check what to do about CRLF + mf.seek(0) + fun = funnychars(mf) + if fun: + print("***UPDATING MASTER (BINARY COPY)***") + args = (slave, master, "rb") + else: + print("***UPDATING MASTER***") + args = (slave, master, "r") + answer=write_master + copy(*args, answer=answer) BUFSIZE = 16*1024 @@ -178,14 +178,12 @@ def copy(src, dst, rmode="rb", wmode="wb print(" to", dst) if not okay("okay to copy? ", answer): return - f = open(src, rmode) - g = open(dst, wmode) - while 1: - buf = f.read(BUFSIZE) - if not buf: break - g.write(buf) - f.close() - g.close() + with open(src, rmode) as f, \ + open(dst, wmode) as g: + while 1: + buf = f.read(BUFSIZE) + if not buf: break + g.write(buf) def raw_input(prompt): sys.stdout.write(prompt) diff -r fc7dbba57869 Tools/unicode/gencjkcodecs.py --- a/Tools/unicode/gencjkcodecs.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Tools/unicode/gencjkcodecs.py Sun Nov 09 21:03:10 2014 +0200 @@ -61,7 +61,8 @@ def gencodecs(prefix): encoding=enc.lower(), owner=loc) codecpath = os.path.join(prefix, enc + '.py') - open(codecpath, 'w').write(code) + with open(codecpath, 'w') as f: + f.write(code) if __name__ == '__main__': import sys diff -r fc7dbba57869 Tools/unicode/gencodec.py --- a/Tools/unicode/gencodec.py Sun Nov 09 20:48:36 2014 +0200 +++ b/Tools/unicode/gencodec.py Sun Nov 09 21:03:10 2014 +0200 @@ -72,9 +72,8 @@ def parsecodes(codes, len=len, range=ran def readmap(filename): - f = open(filename,'r') - lines = f.readlines() - f.close() + with open(filename,'r') as f: + lines = f.readlines() enc2uni = {} identity = [] unmapped = list(range(256)) @@ -359,18 +358,16 @@ encoding_table = codecs.charmap_build(de def pymap(name,map,pyfile,encodingname,comments=1): code = codegen(name,map,encodingname,comments) - f = open(pyfile,'w') - f.write(code) - f.close() + with open(pyfile,'w') as f: + f.write(code) def marshalmap(name,map,marshalfile): d = {} for e,(u,c) in map.items(): d[e] = (u,c) - f = open(marshalfile,'wb') - marshal.dump(d,f) - f.close() + with open(marshalfile,'wb') as f: + marshal.dump(d,f) def convertdir(dir, dirprefix='', nameprefix='', comments=1): @@ -411,8 +408,8 @@ def rewritepythondir(dir, dirprefix='', print('converting %s to %s' % (mapname, dirprefix + codefile)) try: - map = marshal.load(open(os.path.join(dir,mapname), - 'rb')) + with open(os.path.join(dir, mapname), 'rb') as f: + map = marshal.load(f) if not map: print('* map is empty; skipping') else: