GeezerVison is showing github.com — BUG: Work with the ISOC_BINDINGS more consisently · charris/numpy@d7e78b6
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: numpy/f2py/auxfuncs.py +58-1Lines changed: 58 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 50 | 50 | 'isunsigned_long_longarray', 'isunsigned_short', | 51 | 51 | 'isunsigned_shortarray', 'l_and', 'l_not', 'l_or', 'outmess', | 52 | 52 | 'replace', 'show', 'stripcomma', 'throw_error', 'isattr_value', | 53 | | - 'deep_merge', 'getuseblocks' | | 53 | + 'deep_merge', 'getuseblocks', 'process_f2cmap_dict' | 54 | 54 | ] | 55 | 55 |
| 56 | 56 |
| @@ -945,3 +945,60 @@ def getuseblocks(pymod): | 945 | 945 | if modblock.get('use'): | 946 | 946 | all_uses.extend([x for x in modblock.get("use").keys() if "__" not in x]) | 947 | 947 | return all_uses | | 948 | + | | 949 | +def process_f2cmap_dict(f2cmap_all, new_map, c2py_map): | | 950 | + """ | | 951 | + Update the Fortran-to-C type mapping dictionary with new mappings. | | 952 | +
| | 953 | + This function integrates a new mapping dictionary into an existing | | 954 | + Fortran-to-C type mapping dictionary. It ensures that all keys are in | | 955 | + lowercase and validates new entries against a given C-to-Python mapping | | 956 | + dictionary. Redefinitions and invalid entries are reported with a warning. | | 957 | +
| | 958 | + Parameters | | 959 | + ---------- | | 960 | + f2cmap_all : dict | | 961 | + The existing Fortran-to-C type mapping dictionary that will be updated. | | 962 | + It should be a dictionary of dictionaries where the main keys represent | | 963 | + Fortran types and the nested dictionaries map Fortran type specifiers | | 964 | + to corresponding C types. | | 965 | +
| | 966 | + new_map : dict | | 967 | + A dictionary containing new type mappings to be added to `f2cmap_all`. | | 968 | + The structure should be similar to `f2cmap_all`, with keys representing | | 969 | + Fortran types and values being dictionaries of type specifiers and their | | 970 | + C type equivalents. | | 971 | +
| | 972 | + c2py_map : dict | | 973 | + A dictionary used for validating the C types in `new_map`. It maps C | | 974 | + types to corresponding Python types and is used to ensure that the C | | 975 | + types specified in `new_map` are valid. | | 976 | +
| | 977 | + Returns | | 978 | + ------- | | 979 | + dict | | 980 | + The updated Fortran-to-C type mapping dictionary. | | 981 | + """ | | 982 | + for k, d1 in new_map.items(): | | 983 | + for k1 in list(d1.keys()): | | 984 | + d1[k1.lower()] = d1.pop(k1) | | 985 | + k_lower = k.lower() | | 986 | + | | 987 | + if k_lower not in f2cmap_all: | | 988 | + f2cmap_all[k_lower] = {} | | 989 | + for k1, v1 in d1.items(): | | 990 | + if v1 in c2py_map: | | 991 | + if k1 in f2cmap_all[k_lower]: | | 992 | + outmess( | | 993 | + "\tWarning: redefinition of {'%s':{'%s':'%s'->'%s'}}\n" | | 994 | + % (k_lower, k1, f2cmap_all[k_lower][k1], v1) | | 995 | + ) | | 996 | + f2cmap_all[k_lower][k1] = v1 | | 997 | + outmess('\tMapping "%s(kind=%s)" to "%s"\n' % (k_lower, k1, v1)) | | 998 | + else: | | 999 | + errmess( | | 1000 | + "\tIgnoring map {'%s':{'%s':'%s'}}: '%s' must be in %s\n" | | 1001 | + % (k_lower, k1, v1, v1, list(c2py_map.keys())) | | 1002 | + ) | | 1003 | + | | 1004 | + return f2cmap_all |
Collapse file Copy file name to clipboardExpand all lines: numpy/f2py/capi_maps.py +5-23Lines changed: 5 additions & 23 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 29 | 29 | __all__ = [ | 30 | 30 | 'getctype', 'getstrlength', 'getarrdims', 'getpydocsign', | 31 | 31 | 'getarrdocsign', 'getinit', 'sign2map', 'routsign2map', 'modsign2map', | 32 | | - 'cb_sign2map', 'cb_routsign2map', 'common_sign2map' | | 32 | + 'cb_sign2map', 'cb_routsign2map', 'common_sign2map', 'process_f2cmap_dict' | 33 | 33 | ] | 34 | 34 |
| 35 | 35 |
| | 131 | 131 | 'byte': {'': 'char'}, | 132 | 132 | } | 133 | 133 |
| 134 | | -f2cmap_all = deep_merge(f2cmap_all, iso_c_binding_map) | | 134 | +f2cmap_all = process_f2cmap_dict(f2cmap_all, iso_c_binding_map, c2py_map) | 135 | 135 | f2cmap_default = copy.deepcopy(f2cmap_all) | 136 | 136 |
| 137 | 137 | f2cmap_mapped = [] | @@ -156,29 +156,11 @@ def load_f2cmap_file(f2cmap_file): | 156 | 156 | outmess('Reading f2cmap from {!r} ...\n'.format(f2cmap_file)) | 157 | 157 | with open(f2cmap_file) as f: | 158 | 158 | d = eval(f.read().lower(), {}, {}) | 159 | | - for k, d1 in d.items(): | 160 | | - for k1 in d1.keys(): | 161 | | - d1[k1.lower()] = d1[k1] | 162 | | - d[k.lower()] = d[k] | 163 | | - for k in d.keys(): | 164 | | - if k not in f2cmap_all: | 165 | | - f2cmap_all[k] = {} | 166 | | - for k1 in d[k].keys(): | 167 | | - if d[k][k1] in c2py_map: | 168 | | - if k1 in f2cmap_all[k]: | 169 | | - outmess( | 170 | | - "\tWarning: redefinition of {'%s':{'%s':'%s'->'%s'}}\n" % (k, k1, f2cmap_all[k][k1], d[k][k1])) | 171 | | - f2cmap_all[k][k1] = d[k][k1] | 172 | | - outmess('\tMapping "%s(kind=%s)" to "%s"\n' % | 173 | | - (k, k1, d[k][k1])) | 174 | | - f2cmap_mapped.append(d[k][k1]) | 175 | | - else: | 176 | | - errmess("\tIgnoring map {'%s':{'%s':'%s'}}: '%s' must be in %s\n" % ( | 177 | | - k, k1, d[k][k1], d[k][k1], list(c2py_map.keys()))) | | 159 | + f2cmap_all = process_f2cmap_dict(f2cmap_all, d, c2py_map) | 178 | 160 | outmess('Successfully applied user defined f2cmap changes\n') | 179 | 161 | except Exception as msg: | 180 | | - errmess( | 181 | | - 'Failed to apply user defined f2cmap changes: %s. Skipping.\n' % (msg)) | | 162 | + errmess('Failed to apply user defined f2cmap changes: %s. Skipping.\n' % (msg)) | | 163 | + | 182 | 164 |
| 183 | 165 | cformat_map = {'double': '%g', | 184 | 166 | 'float': '%g', | |
Collapse file Copy file name to clipboardExpand all lines: numpy/f2py/tests/test_isoc.py +15Lines changed: 15 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -23,3 +23,18 @@ def test_bindc_kinds(self): | 23 | 23 | out = self.module.coddity.c_add_int64(1, 20) | 24 | 24 | exp_out = 21 | 25 | 25 | assert out == exp_out | | 26 | + | | 27 | + | | 28 | +def test_process_f2cmap_dict(): | | 29 | + from numpy.f2py.auxfuncs import process_f2cmap_dict | | 30 | + f2cmap_all = {'integer': {'8': 'long_long'}} | | 31 | + new_map = {'INTEGER': {'4': 'int'}} | | 32 | + c2py_map = {'int': 'int', 'long_long': 'long'} | | 33 | + | | 34 | + expected_result = {'integer': {'8': 'long_long', '4': 'int'}} | | 35 | + | | 36 | + # Call the function | | 37 | + result = process_f2cmap_dict(f2cmap_all, new_map, c2py_map) | | 38 | + | | 39 | + # Assert the result is as expected | | 40 | + assert result == expected_result |
|