14

I am writing a small C function that is supposed to accelerate some compute intensive portions of a larger application that I have in Python. Naturally I have written a wrapper that ensures that my C code can talk seamlessly with my Python numpy arrays. All is well and I am using the following setup.py

from distutils.core import setup, Extension
import numpy

module1 = Extension('my_wrapper', 
    sources = ['my_c_file.c'],  
    include_dirs=[numpy.get_include()],
    extra_compile_args = ['-fopenmp'],
    extra_link_args = ['-lgomp'])

setup(name = 'my_wrapper',
    version = '1.0',
    description = 'Some description here',
    ext_modules = [module1])

Everything works when I compile this with the command python3 setup.py install and the code behaviour is as expected but I am getting the following warning,

warning: #warning "Using deprecated NumPy API, disable it by " "#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp]
#warning "Using deprecated NumPy API, disable it by " \
^

Although this is just a warning, I would still like to avoid this if I can. Any ideas on how to do that?

2
  • 1
    Did you try the thing the warning says? Commented Oct 10, 2018 at 22:38
  • @user2357112 I tried a #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION in my_c_file.c but I don't think that helped Commented Oct 10, 2018 at 22:47

2 Answers 2

13

This is a known issue, and it stems from the fact that Cython historically has supported very old numpy versions. As cython's doc mentions:

Despite this, you will still get warnings like the following from the compiler, because Cython is using a deprecated Numpy API:

.../include/numpy/npy_1_7_deprecated_api.h:15:2: warning: #warning
"Using deprecated NumPy API, disable it by " "#defining
NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp]

For the time being, it is just a warning that you can ignore.

However, there's work in progress in several pull requests and the parent issue tracking the progress can be found here.

Soon these warnings will be gone.

Sign up to request clarification or add additional context in comments.

2 Comments

I don't see the question mentioning Cython anywhere. I'm facing the same issue - and I'm not using Cython. Just plain extension modules for CPython, written in C, compiled with gcc. Not even using setuptools/distutils. #include <numpy/arrayobject.h> (Numpy: 1.20.2, Python: 3.9.4)
@TobiasBergkvist it's not really about Cython, it's about using deprecated numpy C-API, which is used by the generated Cython code. If your own C code uses the deprecated API, then you can use the new API instead, and you could probably figure out how to do that by looking at the work done on the Cython side :)
7

Until this is resolved upstream as mentioned by adrin, I found a way to insert the preprocessor symbol NPY_NO_DEPRECATED_API into the setup.py code so that the warning will be suppressed. Add the keyword define_macros=[args] to your Extension. Just putting it in your .h, .cpp, or .pyx files will not help because the auto-generated project .cpp file will not see the preprocessor defines. https://docs.python.org/2.0/dist/node13.html

Specifically for OP's case:

module1 = Extension('my_wrapper', sources = ['my_c_file.c'],  
    include_dirs=[numpy.get_include()],
    extra_compile_args = ['-fopenmp'],
    extra_link_args = ['-lgomp']),
    define_macros=[('NPY_NO_DEPRECATED_API', 'NPY_1_7_API_VERSION')])

This will add -DNPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION to the compile line.

Fair warning: doing this may enable several new warnings that were suppressed because it was maintaining compatibility with old versions.

1 Comment

It seems that the canonical way to avoid the warning entails adding a compliation flag similar to the above mentioned, as mentioned on the related cython issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.