diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2019-03-30 08:32:18 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-30 08:32:18 +0200 |
commit | afbb7a371fb44edc731344eab5b474ad8f7b57d7 (patch) | |
tree | bf32e3e6a14a508ee67f0f08e072ffff6ba895df /Tools/scripts/fixheader.py | |
parent | 2524fdefc9bb2a97b99319190aeb23703079ad4c (diff) | |
download | cpython-afbb7a371fb44edc731344eab5b474ad8f7b57d7.tar.gz cpython-afbb7a371fb44edc731344eab5b474ad8f7b57d7.zip |
bpo-22831: Use "with" to avoid possible fd leaks in tools (part 1). (GH-10926)
Diffstat (limited to 'Tools/scripts/fixheader.py')
-rwxr-xr-x | Tools/scripts/fixheader.py | 42 |
1 files changed, 21 insertions, 21 deletions
diff --git a/Tools/scripts/fixheader.py b/Tools/scripts/fixheader.py index ec840575b20..c834eec1e20 100755 --- a/Tools/scripts/fixheader.py +++ b/Tools/scripts/fixheader.py @@ -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 @@ -25,25 +25,25 @@ def process(filename): except IOError as msg: sys.stderr.write('%s: can\'t write: %s\n' % (filename, str(msg))) return - sys.stderr.write('Processing %s ...\n' % filename) - magic = 'Py_' - for c in 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: + sys.stderr.write('Processing %s ...\n' % filename) + magic = 'Py_' + for c in filename: + if ord(c)<=0x80 and c.isalnum(): + magic = magic + c.upper() + else: magic = magic + '_' + 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() |