Skip to content

Commit aae64d8

Browse files
authored
Merge pull request numpy#27182 from seberg/revert-I
REV: Revert undef I and document it
2 parents 9312d5e + 4964280 commit aae64d8

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

doc/source/numpy_2_0_migration_guide.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,19 @@ using the NumPy types. You can still write cython code using the ``c.real`` and
220220
``c.imag`` attributes (using the native typedefs), but you can no longer use
221221
in-place operators ``c.imag += 1`` in Cython's c++ mode.
222222

223+
Because NumPy 2 now includes ``complex.h`` code that uses a variable named
224+
``I`` may see an error such as
225+
226+
.. code-block::C
227+
error: expected ‘)’ before ‘__extension__’
228+
double I,
229+
230+
to use the name ``I`` requires an ``#undef I`` now.
231+
232+
.. note::
233+
NumPy 2.0.1 briefly included the ``#undef I`` to help users not already
234+
including ``complex.h``.
235+
223236

224237
Changes to namespaces
225238
=====================

doc/source/reference/c-api/types-and-structures.rst

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1611,3 +1611,29 @@ for completeness and assistance in understanding the code.
16111611
``arrayobject.h`` header. This type is not exposed to Python and
16121612
could be replaced with a C-structure. As a Python type it takes
16131613
advantage of reference- counted memory management.
1614+
1615+
1616+
NumPy C-API and C complex
1617+
=========================
1618+
When you use the NumPy C-API, you will have access to complex real declarations
1619+
``npy_cdouble`` and ``npy_cfloat``, which are declared in terms of the C
1620+
standard types from ``complex.h``. Unfortunately, ``complex.h`` contains
1621+
`#define I ...`` (where the actual definition depends on the compiler), which
1622+
means that any downstream user that does ``#include <numpy/arrayobject.h>``
1623+
could get ``I`` defined, and using something like declaring ``double I;`` in
1624+
their code will result in an obscure compiler error like
1625+
1626+
.. code-block::C
1627+
error: expected ‘)’ before ‘__extension__’
1628+
double I,
1629+
1630+
This error can be avoided by adding::
1631+
1632+
#undef I
1633+
1634+
to your code.
1635+
1636+
.. versionchanged:: 2.0
1637+
The inclusion of ``complex.h`` was new in NumPy 2, so that code defining
1638+
a different ``I`` may not have required the ``#undef I`` on older versions.
1639+
NumPy 2.0.1 briefly included the ``#under I``

numpy/_core/include/numpy/npy_common.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -379,11 +379,6 @@ typedef struct
379379

380380
#include <complex.h>
381381

382-
// Downstream libraries like sympy would like to use I
383-
// see https://github.com/numpy/numpy/issues/26787
384-
#ifdef I
385-
#undef I
386-
#endif
387382

388383
#if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
389384
typedef _Dcomplex npy_cdouble;

0 commit comments

Comments
 (0)