diff options
author | Benjamin Peterson <benjamin@python.org> | 2010-03-15 03:02:37 (GMT) |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2010-03-15 03:02:37 (GMT) |
commit | f56813997fb546207ef1f9d15325dff2929dc455 (patch) | |
tree | c976fc78d537dccfa918f18b87b8dd685beb38e2 /Lib/py_compile.py | |
parent | 5a4c0f5cc2328d2f1dee504e1f71292b2290fd4a (diff) | |
download | cpython-f56813997fb546207ef1f9d15325dff2929dc455.zip cpython-f56813997fb546207ef1f9d15325dff2929dc455.tar.gz cpython-f56813997fb546207ef1f9d15325dff2929dc455.tar.bz2 |
clean up files correctly
Diffstat (limited to 'Lib/py_compile.py')
-rw-r--r-- | Lib/py_compile.py | 28 |
1 files changed, 13 insertions, 15 deletions
diff --git a/Lib/py_compile.py b/Lib/py_compile.py index 07c9c2a..43af3c0 100644 --- a/Lib/py_compile.py +++ b/Lib/py_compile.py @@ -103,13 +103,12 @@ def compile(file, cfile=None, dfile=None, doraise=False): directories). """ - f = open(file, 'U') - try: - timestamp = long(os.fstat(f.fileno()).st_mtime) - except AttributeError: - timestamp = long(os.stat(file).st_mtime) - codestring = f.read() - f.close() + with open(file, 'U') as f: + try: + timestamp = long(os.fstat(f.fileno()).st_mtime) + except AttributeError: + timestamp = long(os.stat(file).st_mtime) + codestring = f.read() if codestring and codestring[-1] != '\n': codestring = codestring + '\n' try: @@ -123,14 +122,13 @@ def compile(file, cfile=None, dfile=None, doraise=False): return if cfile is None: cfile = file + (__debug__ and 'c' or 'o') - fc = open(cfile, 'wb') - fc.write('\0\0\0\0') - wr_long(fc, timestamp) - marshal.dump(codeobject, fc) - fc.flush() - fc.seek(0, 0) - fc.write(MAGIC) - fc.close() + with open(cfile, 'wb') as fc: + fc.write('\0\0\0\0') + wr_long(fc, timestamp) + marshal.dump(codeobject, fc) + fc.flush() + fc.seek(0, 0) + fc.write(MAGIC) def main(args=None): """Compile several source files. |