Skip to content

BUG: f2py map complex_long_double to NPY_CLONGDOUBLE#31047

Merged
HaoZeke merged 2 commits into
numpy:mainfrom
Anarion-zuo:aaronzuo/f2py-fix-complex-long-double-dtype
Apr 1, 2026
Merged

BUG: f2py map complex_long_double to NPY_CLONGDOUBLE#31047
HaoZeke merged 2 commits into
numpy:mainfrom
Anarion-zuo:aaronzuo/f2py-fix-complex-long-double-dtype

Conversation

@Anarion-zuo

Copy link
Copy Markdown
Contributor

PR summary

Resolved issue #31012.

This PR fixes an inconsistency in f2py’s C-type → NumPy C-API dtype mapping for extended-precision complex values.

  • Problem: In capi_maps.py, c2capi_map["complex_long_double"] was mapped to NPY_CDOUBLE, which can silently downcast complex_long_double to double precision when selecting the NumPy dtype typenum. At the same time, c2pycode_map already treated complex_long_double as distinct ('G' vs 'D'), implying support for a separate type.
  • Fix: Map complex_long_double to NPY_CLONGDOUBLE in c2capi_map, aligning dtype selection with NumPy’s clongdouble dtype and f2py’s existing internal support for npy_clongdouble.
  • Tests: Add a pure-Python regression test (in numpy/f2py/tests_pure/) that asserts:
    • complex_long_double maps to NPY_CLONGDOUBLE
    • complex_long_double is distinct from complex_double in both c2capi_map and c2pycode_map

Run locally:

python -m pytest numpy/f2py/tests_pure/test_capi_maps.py -q

First time committer introduction

I’m a first-time NumPy contributor. I use NumPy for scientific computing and data analysis workflows, and I rely on f2py to integrate legacy Fortran code into Python. I noticed the complex_long_double mapping inconsistency when working with extended-precision complex data, and wanted to fix it to avoid silent precision loss for users.

AI Disclosure

No AI tools are used.

@charris charris changed the title fix: f2py map complex_long_double to NPY_CLONGDOUBLE BUG: f2py map complex_long_double to NPY_CLONGDOUBLE Mar 22, 2026

@themavik themavik left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

block test

@ngoldbaum

Copy link
Copy Markdown
Member

@jorenham do you understand why the type coverage tests failed on this PR?

Comment thread numpy/f2py/tests/test_capi_maps.py
@jorenham

jorenham commented Mar 23, 2026

Copy link
Copy Markdown
Member

@jorenham do you understand why the type coverage tests failed on this PR?

Because the numpy/f2py/tests_pure/test_capi_maps.py (or anything in numpy/f2py/tests_pure for that matter) isn't excluded.

Which begs the question: why didn't you just add the tests to numpy/f2py/tests and instead created a new numpy/f2py/tests_pure directory, @Anarion-zuo ?

@Anarion-zuo

Copy link
Copy Markdown
Contributor Author

Sorry. It would seem I accidentally used some of our internal directory structure. Will fix this ASAP.

Signed-off-by: aaronzuo <anarionzuo@outlook.com>
@Anarion-zuo

Copy link
Copy Markdown
Contributor Author

@jorenham Just removed the unwanted folder.

@ngoldbaum ngoldbaum left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a straightforward fix to me.

@ngoldbaum ngoldbaum added this to the 2.4.5 release milestone Mar 31, 2026
@ngoldbaum

Copy link
Copy Markdown
Member

@charris @HaoZeke would one of you like to hit the merge button?

@HaoZeke HaoZeke merged commit 0d92b75 into numpy:main Apr 1, 2026
84 checks passed
@HaoZeke

HaoZeke commented Apr 1, 2026

Copy link
Copy Markdown
Member

Thanks @ngoldbaum @Anarion-zuo

@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Apr 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants