Skip to content

Conversation

@rlaboiss
Copy link
Contributor

@rlaboiss rlaboiss commented Nov 2, 2025

The import_array macro, which is defined in the file numpy/core/code_generators/generate_numpy_api.py, is intended for use inside an internal SWIG function that is called in the generated C wrapper file. This macro contains a return statement whose argument must match the function definition.

Until version 4.3 of SWIG, the aforementioned function returned a void* value. However, in version 4.4, the return value was changed to int. This causes compilation of code using import_array() to fail with the following error message: returning 'void *' from a function with return type 'int' makes integer from pointer without a cast [-Wint-conversion].

This commit resolves the issue by returning either NULL or 0, depending on the SWIG version being used (< 4.4 or >= 4.4, respectively). This change has been successfully tested against SWIG versions 4.3 and 4.4.

Closes: #30122

The `import_array` macro, which is defined in the file
`numpy/core/code_generators/generate_numpy_api.py`, is intended for use
inside an internal SWIG function that is called in the generated C wrapper
file. This macro contains a return statement whose argument must match the
function definition.

Until version 4.3 of SWIG, the aforementioned function returned a `void*` value.
However, in version 4.4, the return value was changed to `int`. This causes compilation of code using import_array() to fail with the following error message: `returning 'void *' from a function with return type 'int' makes integer from pointer without a cast [-Wint-conversion].`

This commit resolves the issue by returning either `NULL` or `0`, depending
on the SWIG version being used (< 3.4 or >= 3.4, respectively). This change
has been successfully tested against SWIG versions 4.3 and 4.4.

Closes: numpy#30122
@mattip
Copy link
Member

mattip commented Nov 2, 2025

As mentioned in the issue, we don't really hit this code in our tests (which is why we missed it in the first place). Could you suggest a meson-based small code sample that uses SWIG? Then we can try to incorporate this into the Using Python as glue guide, and make it run in C to test it for bitrot.

@mattip
Copy link
Member

mattip commented Nov 2, 2025

The example idea can be in a separate PR if you wish, this one should be good to go.

@rlaboiss
Copy link
Contributor Author

rlaboiss commented Nov 2, 2025

As mentioned in the issue, we don't really hit this code in our tests (which is why we missed it in the first place). Could you suggest a meson-based small code sample that uses SWIG? Then we can try to incorporate this into the Using Python as glue guide, and make it run in C to test it for bitrot.

My knowledge of meson is virtually nonexistent. Could you direct me to a relevant example in the NumPy source code that could serve as a template for the proposed SWIG test?

@charris charris merged commit 1e424da into numpy:main Nov 2, 2025
77 of 78 checks passed
@charris
Copy link
Member

charris commented Nov 2, 2025

Thanks @rlaboiss . Let's get this in and leave an example test for another PR.

@rlaboiss
Copy link
Contributor Author

rlaboiss commented Nov 2, 2025

Thanks for merging the PR.

I just noticed that the bug can be triggered in NumPy's current source, with SWIG 4.4. installed in the system

$ swig --version | grep Version
SWIG Version 4.4.0
$ cd tools/swig/test/
$ make
swig -c++ -python Array.i
swig -c++ -python Farray.i
swig -c++ -python Vector.i
swig -c++ -python Matrix.i
swig -c++ -python Tensor.i
swig -c++ -python Fortran.i
swig -c++ -python Flat.i
./setup.py build_ext -i
Array_wrap.cxx: In function ‘int SWIG_mod_exec(PyObject*)’:
Array_wrap.cxx:8533:3: warning: converting to non-pointer type ‘int’ from NULL [-Wconversion-null]
 8533 |   import_array();
      |   ^~~~~~~~~~~~
Farray_wrap.cxx: In function ‘int SWIG_mod_exec(PyObject*)’:
Farray_wrap.cxx:6485:3: warning: converting to non-pointer type ‘int’ from NULL [-Wconversion-null]
 6485 |   import_array();
      |   ^~~~~~~~~~~~
Vector_wrap.cxx: In function ‘int SWIG_mod_exec(PyObject*)’:
Vector_wrap.cxx:9405:3: warning: converting to non-pointer type ‘int’ from NULL [-Wconversion-null]
 9405 |   import_array();
      |   ^~~~~~~~~~~~
Matrix_wrap.cxx: In function ‘int SWIG_mod_exec(PyObject*)’:
Matrix_wrap.cxx:9028:3: warning: converting to non-pointer type ‘int’ from NULL [-Wconversion-null]
 9028 |   import_array();
      |   ^~~~~~~~~~~~
Tensor_wrap.cxx: In function ‘int SWIG_mod_exec(PyObject*)’:
Tensor_wrap.cxx:9112:3: warning: converting to non-pointer type ‘int’ from NULL [-Wconversion-null]
 9112 |   import_array();
      |   ^~~~~~~~~~~~
Fortran_wrap.cxx: In function ‘int SWIG_mod_exec(PyObject*)’:
Fortran_wrap.cxx:5414:3: warning: converting to non-pointer type ‘int’ from NULL [-Wconversion-null]
 5414 |   import_array();
      |   ^~~~~~~~~~~~
Flat_wrap.cxx: In function ‘int SWIG_mod_exec(PyObject*)’:
Flat_wrap.cxx:5088:3: warning: converting to non-pointer type ‘int’ from NULL [-Wconversion-null]
 5088 |   import_array();
      |   ^~~~~~~~~~~~

@charris charris added the 09 - Backport-Candidate PRs tagged should be backported label Nov 2, 2025
charris pushed a commit to charris/numpy that referenced this pull request Nov 3, 2025
….4 (numpy#30128)

The `import_array` macro, which is defined in the file
`numpy/core/code_generators/generate_numpy_api.py`, is intended for use
inside an internal SWIG function that is called in the generated C wrapper
file. This macro contains a return statement whose argument must match the
function definition.

Until version 4.3 of SWIG, the aforementioned function returned a `void*` value.
However, in version 4.4, the return value was changed to `int`. This causes compilation of code using import_array() to fail with the following error message: `returning 'void *' from a function with return type 'int' makes integer from pointer without a cast [-Wint-conversion].`

This commit resolves the issue by returning either `NULL` or `0`, depending
on the SWIG version being used (< 3.4 or >= 3.4, respectively). This change
has been successfully tested against SWIG versions 4.3 and 4.4.

Closes: numpy#30122
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Nov 3, 2025
charris added a commit that referenced this pull request Nov 3, 2025
BUG: Avoid compilation error of wrapper file generated with SWIG >= 4.4 (#30128)
cakedev0 pushed a commit to cakedev0/numpy that referenced this pull request Dec 5, 2025
….4 (numpy#30128)

The `import_array` macro, which is defined in the file
`numpy/core/code_generators/generate_numpy_api.py`, is intended for use
inside an internal SWIG function that is called in the generated C wrapper
file. This macro contains a return statement whose argument must match the
function definition.

Until version 4.3 of SWIG, the aforementioned function returned a `void*` value.
However, in version 4.4, the return value was changed to `int`. This causes compilation of code using import_array() to fail with the following error message: `returning 'void *' from a function with return type 'int' makes integer from pointer without a cast [-Wint-conversion].`

This commit resolves the issue by returning either `NULL` or `0`, depending
on the SWIG version being used (< 3.4 or >= 3.4, respectively). This change
has been successfully tested against SWIG versions 4.3 and 4.4.

Closes: numpy#30122
IndifferentArea pushed a commit to IndifferentArea/numpy that referenced this pull request Dec 7, 2025
….4 (numpy#30128)

The `import_array` macro, which is defined in the file
`numpy/core/code_generators/generate_numpy_api.py`, is intended for use
inside an internal SWIG function that is called in the generated C wrapper
file. This macro contains a return statement whose argument must match the
function definition.

Until version 4.3 of SWIG, the aforementioned function returned a `void*` value.
However, in version 4.4, the return value was changed to `int`. This causes compilation of code using import_array() to fail with the following error message: `returning 'void *' from a function with return type 'int' makes integer from pointer without a cast [-Wint-conversion].`

This commit resolves the issue by returning either `NULL` or `0`, depending
on the SWIG version being used (< 3.4 or >= 3.4, respectively). This change
has been successfully tested against SWIG versions 4.3 and 4.4.

Closes: numpy#30122
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: Fail to compile wrap file generated with SWIG 4.4

3 participants