Search code examples
pythonnumpyipythoncython

Where to put #define in IPython Cython cell?


If I make a notebook with cells

%load_ext cython

and

%%cython -a

cimport numpy as cnp

then the second one outputs

In file included from /home/ignoring_gravity/.venv/lib/python3.9/site-packages/numpy/core/include/numpy/ndarraytypes.h:1960,
                 from /home/ignoring_gravity/.venv/lib/python3.9/site-packages/numpy/core/include/numpy/ndarrayobject.h:12,
                 from /home/ignoring_gravity/.venv/lib/python3.9/site-packages/numpy/core/include/numpy/arrayobject.h:5,
                 from /home/ignoring_gravity/.cache/ipython/cython/_cython_magic_7ec32efe76f8c3e83109f4fadb9a4e1f.c:765:
/home/ignoring_gravity/.venv/lib/python3.9/site-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:17:2: warning: #warning "Using deprecated NumPy API, disable it with " "#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp]
   17 | #warning "Using deprecated NumPy API, disable it with " \
      |  ^~~~~~~

My question is - where should I put#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION? If I change the second cell to

%%cython -a

#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
cimport numpy as cnp

then it still gives the same warning


Solution

  • Cython-magic doesn't have arguments to define or un-define a macro, but one could do by adding the following line:

    %%cython -a
    # distutils: define_macros=NPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION
    
    cimport numpy as cnp
    

    However, until Cython 3.0, the deprecated numpy-API was used and getting rid of this warning would/could lead to issues. Thus, for Cython's versions <= 3.0 one should follow @DavidW's advice and ignore the warning.

    See also a related note in the Cython's documentation.