8000 BUG: Handle ``iso_c_type`` mappings more consistently by HaoZeke · Pull Request #25226 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: Handle iso_c_type mappings more consistently #25226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
MAINT: Correctly return mapped types [f2py]
  • Loading branch information
HaoZeke committed Nov 26, 2023
commit 13da8ad455b30b61aaee8eacce6c7159aab08422
35 changes: 21 additions & 14 deletions numpy/f2py/auxfuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,8 @@ def getuseblocks(pymod):

def process_f2cmap_dict(f2cmap_all, new_map, c2py_map):
"""
Update the Fortran-to-C type mapping dictionary with new mappings.
Update the Fortran-to-C type mapping dictionary with new mappings and
return a list of successfully mapped C types.

This function integrates a new mapping dictionary into an existing
Fortran-to-C type mapping dictionary. It ensures that all keys are in
Expand Down Expand Up @@ -948,29 +949,35 @@ def process_f2cmap_dict(f2cmap_all, new_map, c2py_map):

Returns
-------
dict
The updated Fortran-to-C type mapping dictionary.
tuple of (dict, list)
The updated Fortran-to-C type mapping dictionary and a list of
successfully mapped C types.
"""
f2cmap_mapped = []

new_map_lower = {}
for k, d1 in new_map.items():
for k1 in list(d1.keys()):
d1[k1.lower()] = d1.pop(k1)
k_lower = k.lower()
d1_lower = {k1.lower(): v1 for k1, v1 in d1.items()}
new_map_lower[k.lower()] = d1_lower

for k, d1 in new_map_lower.items():
if k not in f2cmap_all:
f2cmap_all[k] = {}

if k_lower not in f2cmap_all:
f2cmap_all[k_lower] = {}
for k1, v1 in d1.items():
if v1 in c2py_map:
if k1 in f2cmap_all[k_lower]:
if k1 in f2cmap_all[k]:
outmess(
"\tWarning: redefinition of {'%s':{'%s':'%s'->'%s'}}\n"
% (k_lower, k1, f2cmap_all[k_lower][k1], v1)
% (k, k1, f2cmap_all[k][k1], v1)
)
f2cmap_all[k_lower][k1] = v1
outmess('\tMapping "%s(kind=%s)" to "%s"\n' % (k_lower, k1, v1))
f2cmap_all[k][k1] = v1
outmess('\tMapping "%s(kind=%s)" to "%s"\n' % (k, k1, v1))
f2cmap_mapped.append(v1)
else:
errmess(
"\tIgnoring map {'%s':{'%s':'%s'}}: '%s' must be in %s\n"
% (k_lower, k1, v1, v1, list(c2py_map.keys()))
% (k, k1, v1, v1, list(c2py_map.keys()))
)

return f2cmap_all
return f2cmap_all, f2cmap_mapped
14 changes: 8 additions & 6 deletions numpy/f2py/tests/test_isoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ def test_bindc_kinds(self):

def test_process_f2cmap_dict():
from numpy.f2py.auxfuncs import process_f2cmap_dict
f2cmap_all = {'integer': {'8': 'long_long'}}
new_map = {'INTEGER': {'4': 'int'}}
c2py_map = {'int': 'int', 'long_long': 'long'}

expected_result = {'integer': {'8': 'long_long', '4': 'int'}}
f2cmap_all = {"integer": {"8": "rubbish_type"}}
new_map = {"INTEGER": {"4": "int"}}
c2py_map = {"int": "int", "rubbish_type": "long"}

exp_map, exp_maptyp = ({"integer": {"8": "rubbish_type", "4": "int"}}, ["int"])

# Call the function
result = process_f2cmap_dict(f2cmap_all, new_map, c2py_map)
res_map, res_maptyp = process_f2cmap_dict(f2cmap_all, new_map, c2py_map)

# Assert the result is as expected
assert result == expected_result
assert res_map == exp_map
assert res_maptyp == exp_maptyp
0