summaryrefslogtreecommitdiffstats
path: root/Tools/scripts/fixcid.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2019-03-30 06:32:18 (GMT)
committerGitHub <noreply@github.com>2019-03-30 06:32:18 (GMT)
commitafbb7a371fb44edc731344eab5b474ad8f7b57d7 (patch)
treebf32e3e6a14a508ee67f0f08e072ffff6ba895df /Tools/scripts/fixcid.py
parent2524fdefc9bb2a97b99319190aeb23703079ad4c (diff)
downloadcpython-afbb7a371fb44edc731344eab5b474ad8f7b57d7.zip
cpython-afbb7a371fb44edc731344eab5b474ad8f7b57d7.tar.gz
cpython-afbb7a371fb44edc731344eab5b474ad8f7b57d7.tar.bz2
bpo-22831: Use "with" to avoid possible fd leaks in tools (part 1). (GH-10926)
Diffstat (limited to 'Tools/scripts/fixcid.py')
-rwxr-xr-xTools/scripts/fixcid.py60
1 files changed, 30 insertions, 30 deletions
diff --git a/Tools/scripts/fixcid.py b/Tools/scripts/fixcid.py
index c66a0ac..8f35eae 100755
--- a/Tools/scripts/fixcid.py
+++ b/Tools/scripts/fixcid.py
@@ -281,36 +281,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()