diff options
| author | Serhiy Storchaka <storchaka@gmail.com> | 2019-03-30 08:33:02 +0200 | 
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-03-30 08:33:02 +0200 | 
| commit | 172bb39452ae8b3ccdf5d1f23ead46f44200cd49 (patch) | |
| tree | 5e1effbca3664b839a81eb7a7d62fa4974cfbfb1 /Tools/scripts/pathfix.py | |
| parent | afbb7a371fb44edc731344eab5b474ad8f7b57d7 (diff) | |
| download | cpython-git-172bb39452ae8b3ccdf5d1f23ead46f44200cd49.tar.gz | |
bpo-22831: Use "with" to avoid possible fd leaks in tools (part 2). (GH-10927)
Diffstat (limited to 'Tools/scripts/pathfix.py')
| -rwxr-xr-x | Tools/scripts/pathfix.py | 44 | 
1 files changed, 21 insertions, 23 deletions
| diff --git a/Tools/scripts/pathfix.py b/Tools/scripts/pathfix.py index c5bf984306..1a0cf1c9e6 100755 --- a/Tools/scripts/pathfix.py +++ b/Tools/scripts/pathfix.py @@ -103,29 +103,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 | 
