diff options
| author | Pauli Virtanen <pav@iki.fi> | 2019-12-14 15:31:50 +0200 |
|---|---|---|
| committer | Pauli Virtanen <pav@iki.fi> | 2019-12-14 17:09:06 +0200 |
| commit | 5a4f62bbf7622530e2a43d1859ae496537e9f0f0 (patch) | |
| tree | 12bab1afefe906e715991df5a94786e478d27954 /numpy/f2py/capi_maps.py | |
| parent | b6f91207549fc97765c114c0c01625eaa5e58956 (diff) | |
| download | numpy-5a4f62bbf7622530e2a43d1859ae496537e9f0f0.tar.gz | |
ENH: f2py: add --f2cmap option for specifying the name of .f2py_f2cmap
Previously, f2py loaded the type mappings from a file ``.f2py_f2cmap``
in current directory, at import time.
Make the file name customizable by adding a ``--f2cmap`` command line
option, and postpone loading the file to f2py.run_main().
Moreover, restore the default type mapping in f2py.run_main() before
loading the customizations, so that multiple calls to f2py.run_main() do
not interfere with each other. (For example, numpy.distutils calls f2py
multiple times in the same process.)
Diffstat (limited to 'numpy/f2py/capi_maps.py')
| -rw-r--r-- | numpy/f2py/capi_maps.py | 28 |
1 files changed, 20 insertions, 8 deletions
diff --git a/numpy/f2py/capi_maps.py b/numpy/f2py/capi_maps.py index c41dd77c6..ce79f680f 100644 --- a/numpy/f2py/capi_maps.py +++ b/numpy/f2py/capi_maps.py @@ -179,17 +179,29 @@ f2cmap_all = {'real': {'': 'float', '4': 'float', '8': 'double', 'character': {'': 'string'} } -if os.path.isfile('.f2py_f2cmap'): +f2cmap_default = copy.deepcopy(f2cmap_all) + + +def load_f2cmap_file(f2cmap_file): + global f2cmap_all + + f2cmap_all = copy.deepcopy(f2cmap_default) + + if f2cmap_file is None: + # Default value + f2cmap_file = '.f2py_f2cmap' + if not os.path.isfile(f2cmap_file): + return + # User defined additions to f2cmap_all. - # .f2py_f2cmap must contain a dictionary of dictionaries, only. For + # f2cmap_file must contain a dictionary of dictionaries, only. For # example, {'real':{'low':'float'}} means that Fortran 'real(low)' is # interpreted as C 'float'. This feature is useful for F90/95 users if # they use PARAMETERSs in type specifications. try: - outmess('Reading .f2py_f2cmap ...\n') - f = open('.f2py_f2cmap', 'r') - d = eval(f.read(), {}, {}) - f.close() + outmess('Reading f2cmap from {!r} ...\n'.format(f2cmap_file)) + with open(f2cmap_file, 'r') as f: + d = eval(f.read(), {}, {}) for k, d1 in list(d.items()): for k1 in list(d1.keys()): d1[k1.lower()] = d1[k1] @@ -208,10 +220,10 @@ if os.path.isfile('.f2py_f2cmap'): else: errmess("\tIgnoring map {'%s':{'%s':'%s'}}: '%s' must be in %s\n" % ( k, k1, d[k][k1], d[k][k1], list(c2py_map.keys()))) - outmess('Successfully applied user defined changes from .f2py_f2cmap\n') + outmess('Successfully applied user defined f2cmap changes\n') except Exception as msg: errmess( - 'Failed to apply user defined changes from .f2py_f2cmap: %s. Skipping.\n' % (msg)) + 'Failed to apply user defined f2cmap changes: %s. Skipping.\n' % (msg)) cformat_map = {'double': '%g', 'float': '%g', |
