Skip to content

Commit

Permalink
Thread PYBIND11_NUMPY2_SUPPORT through things and try to adept test…
Browse files Browse the repository at this point in the history
… matrix
  • Loading branch information
seberg committed Mar 11, 2024
1 parent 4555280 commit 63283c2
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 6 deletions.
11 changes: 9 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,14 @@ jobs:
run: python -m pip install pytest-github-actions-annotate-failures

# First build - C++11 mode and inplace
# More-or-less randomly adding -DPYBIND11_SIMPLE_GIL_MANAGEMENT=ON here.
# More-or-less randomly adding -DPYBIND11_SIMPLE_GIL_MANAGEMENT=ON here
# (same for PYBIND11_NUMPY2_SUPPORT).
- name: Configure C++11 ${{ matrix.args }}
run: >
cmake -S . -B .
-DPYBIND11_WERROR=ON
-DPYBIND11_SIMPLE_GIL_MANAGEMENT=ON
-DPYBIND11_NUMPY2_SUPPORT=ON
-DDOWNLOAD_CATCH=ON
-DDOWNLOAD_EIGEN=ON
-DCMAKE_CXX_STANDARD=11
Expand All @@ -138,11 +140,13 @@ jobs:

# Second build - C++17 mode and in a build directory
# More-or-less randomly adding -DPYBIND11_SIMPLE_GIL_MANAGEMENT=OFF here.
# (same for PYBIND11_NUMPY2_SUPPORT, but requires a NumPy 1.x at runtime).
- name: Configure C++17
run: >
cmake -S . -B build2
-DPYBIND11_WERROR=ON
-DPYBIND11_SIMPLE_GIL_MANAGEMENT=OFF
-DPYBIND11_NUMPY2_SUPPORT=OFF
-DDOWNLOAD_CATCH=ON
-DDOWNLOAD_EIGEN=ON
-DCMAKE_CXX_STANDARD=17
Expand Down Expand Up @@ -895,8 +899,10 @@ jobs:
python-version: ${{ matrix.python }}

- name: Prepare env
# Ensure use of NumPy 2 (via NumPy nightlies but can be changed soon)
run: |
python3 -m pip install -r tests/requirements.txt
python3 -m pip install -r tests/requirements.txt\
python3 -m pip install --upgrade --pre -i https://pypi.anaconda.org/scientific-python-nightly-wheels/simple numpy
- name: Update CMake
uses: jwlawson/actions-setup-cmake@v2.0
Expand Down Expand Up @@ -925,6 +931,7 @@ jobs:
run: >
cmake -S . -B build_partial
-DPYBIND11_WERROR=ON
-DPYBIND11_
-DDOWNLOAD_CATCH=ON
-DDOWNLOAD_EIGEN=ON
-DCMAKE_CXX_STANDARD=20
Expand Down
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,17 @@ option(PYBIND11_TEST "Build pybind11 test suite?" ${PYBIND11_MASTER_PROJECT})
option(PYBIND11_NOPYTHON "Disable search for Python" OFF)
option(PYBIND11_SIMPLE_GIL_MANAGEMENT
"Use simpler GIL management logic that does not support disassociation" OFF)
option(PYBIND11_NUMPY2_SUPPORT "Enable compile in a NumPy 2 and 1 compatible way" OFF)
set(PYBIND11_INTERNALS_VERSION
""
CACHE STRING "Override the ABI version, may be used to enable the unstable ABI.")

if(PYBIND11_SIMPLE_GIL_MANAGEMENT)
add_compile_definitions(PYBIND11_SIMPLE_GIL_MANAGEMENT)
endif()
if(PYBIND11_NUMPY2_SUPPORT)
add_compile_definitions(PYBIND11_NUMPY2_SUPPORT)
endif()

cmake_dependent_option(
USE_PYTHON_INCLUDE_DIR
Expand Down
6 changes: 3 additions & 3 deletions include/pybind11/numpy.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,9 @@ PYBIND11_NOINLINE module_ import_numpy_core_submodule(const char *submodule_name

#ifndef PYBIND11_NUMPY2_SUPPORT
if (major_version >= 2) {
throw std::runtime_error("module compiled without NumPy 2 support. Please "
"define PYBIND11_NUMPY2_SUPPORT before the `numpy2.h` "
"to enable it.");
throw std::runtime_error("module compiled without NumPy 2 support. Please define "
"PYBIND11_NUMPY2_SUPPORT before including `numpy2.h` "
"or define it during build to enable NumPy 2 support.");
}
#endif
/* `numpy.core` was renamed to `numpy._core` in NumPy 2.0 as it officially
Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,5 @@ def pytest_report_header(config):
f" {pybind11_tests.cpp_std}"
f" {pybind11_tests.PYBIND11_INTERNALS_ID}"
f" PYBIND11_SIMPLE_GIL_MANAGEMENT={pybind11_tests.PYBIND11_SIMPLE_GIL_MANAGEMENT}"
f" PYBIND11_NUMPY2_SUPPORT={pybind11_tests.PYBIND11_NUMPY2_SUPPORT}"
)
6 changes: 6 additions & 0 deletions tests/pybind11_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ PYBIND11_MODULE(pybind11_tests, m) {
#else
false;
#endif
m.attr("PYBIND11_NUMPY2_SUPPORT") =
#if defined(PYBIND11_NUMPY2_SUPPORT)
true;
#else
false;
#endif

bind_ConstructorStats(m);

Expand Down
8 changes: 7 additions & 1 deletion tests/test_numpy_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest

import env # noqa: F401
from pybind11_tests import PYBIND11_NUMPY2_SUPPORT
from pybind11_tests import numpy_dtypes as m

np = pytest.importorskip("numpy")
Expand Down Expand Up @@ -178,7 +179,12 @@ def test_dtype(simple_dtype):
assert m.test_dtype_num() == [np.dtype(ch).num for ch in expected_chars]
assert m.test_dtype_byteorder() == [np.dtype(ch).byteorder for ch in expected_chars]
assert m.test_dtype_alignment() == [np.dtype(ch).alignment for ch in expected_chars]
assert m.test_dtype_flags() == [np.dtype(ch).flags for ch in expected_chars]
if PYBIND11_NUMPY2_SUPPORT:
assert m.test_dtype_flags() == [np.dtype(ch).flags for ch in expected_chars]
else:
assert m.test_dtype_flags() == [
chr(np.dtype(ch).flags) for ch in expected_chars
]


def test_recarray(simple_dtype, packed_dtype):
Expand Down

0 comments on commit 63283c2

Please sign in to comment.