diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2019-03-30 06:33:02 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-30 06:33:02 (GMT) |
commit | 172bb39452ae8b3ccdf5d1f23ead46f44200cd49 (patch) | |
tree | 5e1effbca3664b839a81eb7a7d62fa4974cfbfb1 /Tools/unicode | |
parent | afbb7a371fb44edc731344eab5b474ad8f7b57d7 (diff) | |
download | cpython-172bb39452ae8b3ccdf5d1f23ead46f44200cd49.zip cpython-172bb39452ae8b3ccdf5d1f23ead46f44200cd49.tar.gz cpython-172bb39452ae8b3ccdf5d1f23ead46f44200cd49.tar.bz2 |
bpo-22831: Use "with" to avoid possible fd leaks in tools (part 2). (GH-10927)
Diffstat (limited to 'Tools/unicode')
-rw-r--r-- | Tools/unicode/gencjkcodecs.py | 3 | ||||
-rw-r--r-- | Tools/unicode/gencodec.py | 19 |
2 files changed, 10 insertions, 12 deletions
diff --git a/Tools/unicode/gencjkcodecs.py b/Tools/unicode/gencjkcodecs.py index ebccfc7..45866bf 100644 --- a/Tools/unicode/gencjkcodecs.py +++ b/Tools/unicode/gencjkcodecs.py @@ -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 --git a/Tools/unicode/gencodec.py b/Tools/unicode/gencodec.py index 31f0112..1e5aced 100644 --- a/Tools/unicode/gencodec.py +++ b/Tools/unicode/gencodec.py @@ -72,9 +72,8 @@ def parsecodes(codes, len=len, range=range): def readmap(filename): - f = open(filename,'r') - lines = f.readlines() - f.close() + with open(filename) as f: + lines = f.readlines() enc2uni = {} identity = [] unmapped = list(range(256)) @@ -359,18 +358,16 @@ encoding_table = codecs.charmap_build(decoding_table) 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='', comments=1): 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: |