Skip to content

Commit 5cd17c8

Browse files
committed
add version handling both at compile time and at runtime, add ci run with nightly numpy
1 parent 0d234bd commit 5cd17c8

File tree

3 files changed

+36
-18
lines changed

3 files changed

+36
-18
lines changed

.github/workflows/ci.yml

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,27 @@ on:
1010
jobs:
1111
test:
1212
runs-on: ubuntu-latest
13+
strategy:
14+
matrix:
15+
numpy-version: ['2.3.2', 'nightly']
1316

1417
steps:
1518
- uses: actions/checkout@v3
1619
- name: Setup Python
1720
uses: actions/setup-python@v3
1821
with:
19-
python-version: "3.10"
22+
python-version: "3.11"
2023
- name: Install build and test dependencies
2124
run: |
22-
python -m pip install -U pip build pytest unyt wheel meson ninja meson-python patchelf pandas numpy
25+
python -m pip install -U pip build pytest unyt wheel meson ninja meson-python patchelf pandas
26+
- name: Install numpy nightly
27+
if: matrix.numpy-version == 'nightly'
28+
run: |
29+
pip install -i https://pypi.anaconda.org/scientific-python-nightly-wheels/simple numpy
30+
- name: Install numpy from PyPI
31+
if: matrix.numpy-version != 'nightly'
32+
run: |
33+
pip install "numpy==${{ matrix.numpy-verson }}"
2334
- name: Install asciidtype
2435
working-directory: asciidtype
2536
run: |
@@ -63,8 +74,11 @@ jobs:
6374
- name: Install quaddtype
6475
working-directory: quaddtype
6576
run: |
77+
if [ "${{ matrix.numpy-version }}" == "nightly" ]; then
78+
extra_args=-Csetup-args="-Dcpp_args=-DNPY_TARGET_VERSION=NPY_2_4_API_VERSION"
79+
fi
6680
export LDFLAGS="-fopenmp"
67-
python -m pip install . -v
81+
python -m pip install $extra_args . -v
6882
6983
- name: Run quaddtype tests
7084
working-directory: quaddtype

quaddtype/numpy_quaddtype/src/casts.cpp

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#define PY_ARRAY_UNIQUE_SYMBOL QuadPrecType_ARRAY_API
22
#define PY_UFUNC_UNIQUE_SYMBOL QuadPrecType_UFUNC_API
3-
#define NPY_NO_DEPRECATED_API NPY_2_4_API_VERSION
4-
#define NPY_TARGET_VERSION NPY_2_4_API_VERSION
3+
#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
4+
#ifndef NPY_TARGET_VERSION
5+
#define NPY_TARGET_VERSION NPY_2_0_API_VERSION
6+
#endif
57
#define NO_IMPORT_ARRAY
68
#define NO_IMPORT_UFUNC
79

@@ -419,7 +421,7 @@ numpy_to_quad_resolve_descriptors(PyObject *NPY_UNUSED(self), PyArray_DTypeMeta
419421
}
420422

421423
loop_descrs[0] = PyArray_GetDefaultDescr(dtypes[0]);
422-
#if NPY_FEATURE_VERSION > NPY_2_3_API_VERSION
424+
#if NPY_TARGET_VERSION > NPY_2_3_API_VERSION
423425
return NPY_SAFE_CASTING | NPY_SAME_VALUE_CASTING_FLAG;
424426
#else
425427
return NPY_SAFE_CASTING;
@@ -692,7 +694,6 @@ from_quad_checked(quad_value x, QuadBackendType backend, typename NpyType<T>::TY
692694
return -1;
693695
}
694696

695-
template <typename T>
696697
static NPY_CASTING
697698
quad_to_numpy_resolve_descriptors(PyObject *NPY_UNUSED(self), PyArray_DTypeMeta *dtypes[2],
698699
PyArray_Descr *given_descrs[2], PyArray_Descr *loop_descrs[2],
@@ -711,9 +712,6 @@ quad_to_numpy_strided_loop_unaligned(PyArrayMethod_Context *context, char *const
711712
npy_intp const dimensions[], npy_intp const strides[],
712713
void *NPY_UNUSED(auxdata))
713714
{
714-
#if NPY_FEATURE_VERSION > NPY_2_3_API_VERSION
715-
int same_value_casting = ((context->flags & NPY_SAME_VALUE_CONTEXT_FLAG) == NPY_SAME_VALUE_CONTEXT_FLAG);
716-
#endif
717715
npy_intp N = dimensions[0];
718716
char *in_ptr = data[0];
719717
char *out_ptr = data[1];
@@ -723,7 +721,11 @@ quad_to_numpy_strided_loop_unaligned(PyArrayMethod_Context *context, char *const
723721

724722
size_t elem_size = (backend == BACKEND_SLEEF) ? sizeof(Sleef_quad) : sizeof(long double);
725723

726-
#if NPY_FEATURE_VERSION > NPY_2_3_API_VERSION
724+
#if NPY_TARGET_VERSION > NPY_2_3_API_VERSION
725+
int same_value_casting = 0;
726+
if (PyArray_GetNDArrayCFeatureVersion() > NPY_2_3_API_VERSION) {
727+
same_value_casting = ((context->flags & NPY_SAME_VALUE_CONTEXT_FLAG) == NPY_SAME_VALUE_CONTEXT_FLAG);
728+
}
727729
if (same_value_casting) {
728730
while (N--) {
729731
quad_value in_val;
@@ -763,14 +765,15 @@ quad_to_numpy_strided_loop_aligned(PyArrayMethod_Context *context, char *const d
763765
npy_intp N = dimensions[0];
764766
char *in_ptr = data[0];
765767
char *out_ptr = data[1];
766-
#if NPY_FEATURE_VERSION > NPY_2_3_API_VERSION
767-
int same_value_casting = ((context->flags & NPY_SAME_VALUE_CONTEXT_FLAG) == NPY_SAME_VALUE_CONTEXT_FLAG);
768-
#endif
769768

770769
QuadPrecDTypeObject *quad_descr = (QuadPrecDTypeObject *)context->descriptors[0];
771770
QuadBackendType backend = quad_descr->backend;
772771

773-
#if NPY_FEATURE_VERSION > NPY_2_3_API_VERSION
772+
#if NPY_TARGET_VERSION > NPY_2_3_API_VERSION
773+
int same_value_casting = 0;
774+
if (PyArray_GetNDArrayCFeatureVersion() > NPY_2_3_API_VERSION) {
775+
same_value_casting = ((context->flags & NPY_SAME_VALUE_CONTEXT_FLAG) == NPY_SAME_VALUE_CONTEXT_FLAG);
776+
}
774777
if (same_value_casting) {
775778
while (N--) {
776779
quad_value in_val;
@@ -835,7 +838,7 @@ add_cast_from(PyArray_DTypeMeta *to)
835838
PyArray_DTypeMeta **dtypes = new PyArray_DTypeMeta *[2]{&QuadPrecDType, to};
836839

837840
PyType_Slot *slots = new PyType_Slot[]{
838-
{NPY_METH_resolve_descriptors, (void *)&quad_to_numpy_resolve_descriptors<T>},
841+
{NPY_METH_resolve_descriptors, (void *)&quad_to_numpy_resolve_descriptors},
839842
{NPY_METH_strided_loop, (void *)&quad_to_numpy_strided_loop_aligned<T>},
840843
{NPY_METH_unaligned_strided_loop, (void *)&quad_to_numpy_strided_loop_unaligned<T>},
841844
{0, nullptr}};
@@ -844,7 +847,7 @@ add_cast_from(PyArray_DTypeMeta *to)
844847
.name = "cast_QuadPrec_to_NumPy",
845848
.nin = 1,
846849
.nout = 1,
847-
#if NPY_FEATURE_VERSION > NPY_2_3_API_VERSION
850+
#if PyArray_RUNTIME_VERSION > NPY_2_3_API_VERSION
848851
.casting = NPY_SAME_VALUE_CASTING,
849852
#else
850853
.casting = NPY_UNSAFE_CASTING,

quaddtype/tests/test_quaddtype.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import numpy_quaddtype
77
from numpy_quaddtype import QuadPrecDType, QuadPrecision
88

9+
np_major, np_minor = np.__version__.split('.')[:2]
910

1011
def test_create_scalar_simple():
1112
assert isinstance(QuadPrecision("12.0"), QuadPrecision)
@@ -76,8 +77,8 @@ def test_unsupported_astype(dtype):
7677
np.array(QuadPrecision(1)).astype(dtype, casting="unsafe")
7778

7879

80+
@pytest.mark.skip(np_major <2 or (np_major == 2 and np_minor < 4))
7981
def test_same_value_cast():
80-
# This will fail if compiled with NPY_TARGET_VERSION NPY<2_4_API_VERSION
8182
a = np.arange(30, dtype=np.float32)
8283
# upcasting can never fail
8384
b = a.astype(QuadPrecision, casting='same_value')

0 commit comments

Comments
 (0)