diff options
author | Eric Wieser <wieser.eric@gmail.com> | 2019-06-12 00:29:10 -0700 |
---|---|---|
committer | Eric Wieser <wieser.eric@gmail.com> | 2019-06-12 00:53:04 -0700 |
commit | 3df4b437723e05335e57a63fa2f5f7cce5047741 (patch) | |
tree | 7cb9fd458e529cbce38427437815b186dc968c22 /numpy/core/setup.py | |
parent | da1bc31213675425c43f025c5fa09869b904b667 (diff) | |
download | numpy-3df4b437723e05335e57a63fa2f5f7cce5047741.tar.gz |
MAINT: Use with statements and dedent in core/setup.py
Diffstat (limited to 'numpy/core/setup.py')
-rw-r--r-- | numpy/core/setup.py | 104 |
1 files changed, 50 insertions, 54 deletions
diff --git a/numpy/core/setup.py b/numpy/core/setup.py index 62147d22b..1a9c8410c 100644 --- a/numpy/core/setup.py +++ b/numpy/core/setup.py @@ -6,7 +6,9 @@ import pickle import copy import warnings import platform +import textwrap from os.path import join + from numpy.distutils import log from distutils.dep_util import newer from distutils.sysconfig import get_config_var @@ -466,45 +468,42 @@ def configuration(parent_package='',top_path=None): moredefs.append(('NPY_PY3K', 1)) # Generate the config.h file from moredefs - target_f = open(target, 'w') - for d in moredefs: - if isinstance(d, str): - target_f.write('#define %s\n' % (d)) + with open(target, 'w') as target_f: + for d in moredefs: + if isinstance(d, str): + target_f.write('#define %s\n' % (d)) + else: + target_f.write('#define %s %s\n' % (d[0], d[1])) + + # define inline to our keyword, or nothing + target_f.write('#ifndef __cplusplus\n') + if inline == 'inline': + target_f.write('/* #undef inline */\n') else: - target_f.write('#define %s %s\n' % (d[0], d[1])) + target_f.write('#define inline %s\n' % inline) + target_f.write('#endif\n') + + # add the guard to make sure config.h is never included directly, + # but always through npy_config.h + target_f.write(textwrap.dedent(""" + #ifndef _NPY_NPY_CONFIG_H_ + #error config.h should never be included directly, include npy_config.h instead + #endif + """)) - # define inline to our keyword, or nothing - target_f.write('#ifndef __cplusplus\n') - if inline == 'inline': - target_f.write('/* #undef inline */\n') - else: - target_f.write('#define inline %s\n' % inline) - target_f.write('#endif\n') - - # add the guard to make sure config.h is never included directly, - # but always through npy_config.h - target_f.write(""" -#ifndef _NPY_NPY_CONFIG_H_ -#error config.h should never be included directly, include npy_config.h instead -#endif -""") - - target_f.close() print('File:', target) - target_f = open(target) - print(target_f.read()) - target_f.close() + with open(target) as target_f: + print(target_f.read()) print('EOF') else: mathlibs = [] - target_f = open(target) - for line in target_f: - s = '#define MATHLIB' - if line.startswith(s): - value = line[len(s):].strip() - if value: - mathlibs.extend(value.split(',')) - target_f.close() + with open(target) as target_f: + for line in target_f: + s = '#define MATHLIB' + if line.startswith(s): + value = line[len(s):].strip() + if value: + mathlibs.extend(value.split(',')) # Ugly: this can be called within a library and not an extension, # in which case there is no libraries attributes (and none is @@ -567,26 +566,24 @@ def configuration(parent_package='',top_path=None): moredefs.append(('NPY_API_VERSION', '0x%.8X' % C_API_VERSION)) # Add moredefs to header - target_f = open(target, 'w') - for d in moredefs: - if isinstance(d, str): - target_f.write('#define %s\n' % (d)) - else: - target_f.write('#define %s %s\n' % (d[0], d[1])) - - # Define __STDC_FORMAT_MACROS - target_f.write(""" -#ifndef __STDC_FORMAT_MACROS -#define __STDC_FORMAT_MACROS 1 -#endif -""") - target_f.close() + with open(target, 'w') as target_f: + for d in moredefs: + if isinstance(d, str): + target_f.write('#define %s\n' % (d)) + else: + target_f.write('#define %s %s\n' % (d[0], d[1])) + + # Define __STDC_FORMAT_MACROS + target_f.write(textwrap.dedent(""" + #ifndef __STDC_FORMAT_MACROS + #define __STDC_FORMAT_MACROS 1 + #endif + """)) # Dump the numpyconfig.h header to stdout print('File: %s' % target) - target_f = open(target) - print(target_f.read()) - target_f.close() + with open(target) as target_f: + print(target_f.read()) print('EOF') config.add_data_files((header_dir, target)) return target @@ -891,10 +888,9 @@ def configuration(parent_package='',top_path=None): os.makedirs(dir) script = generate_umath_py if newer(script, target): - f = open(target, 'w') - f.write(generate_umath.make_code(generate_umath.defdict, - generate_umath.__file__)) - f.close() + with open(target, 'w') as f: + f.write(generate_umath.make_code(generate_umath.defdict, + generate_umath.__file__)) return [] umath_src = [ |