Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf60da51

Browse files
committed
API: Shrink MultiIterObject and makeNPY_MAXARGS a runtime macro
Cython is OK with increased sizes, but doesn't like shrinking anobject size. We should fix this (providing a size to compare within the `pyd` maybe). But just not including the end of the objecthere publically is totally fine...This makes `NPY_MAXARGS` also a *runtime* constant, since thepresumably only user (`numexpr`) should be better off with thatanyway. Yes, they need to change the code to hard-code the maximum,but they also need the NumPy cap from us anyway.This is needed, because Cython breaks for sklearn which uses`numpy.broadcast` AKA `PyArrayMultiIterObject`.
1 parent06d7bdf commitf60da51

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
``NPY_MAXARGS`` not constant and ``PyArrayMultiIterObject`` size change
2+
-----------------------------------------------------------------------
3+
Since ``NPY_MAXARGS`` was increased, it is now a runtime constant and not
4+
compiletime constant anymore.
5+
We expect almost no users to notice this. But if used for stack allocations
6+
it now must be replaced with a custom constant using ``NPY_MAXARGS`` as an
7+
additional runtime check.
8+
9+
The ``sizeof(PyArrayMultiIterObject)`` does now not include the full size
10+
of the object. We expect nobody to notice this change. It was necessary
11+
to avoid issues with Cython.

‎doc/source/reference/c-api/array.rst‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3429,10 +3429,12 @@ Other constants
34293429
34303430
The maximum number of array arguments that can be used in some
34313431
functions. This used to be 32 before NumPy 2 and is now 64.
3432+
To continue to allow using it as a check whether a number of arguments
3433+
is compatible ufuncs, this macro is now runtime dependent.
34323434
34333435
.. note::
3434-
You should never usethis. We may remove it in future versions of
3435-
NumPy.
3436+
We discourage any useof ``NPY_MAXARGS`` that isn't explicitly tied
3437+
to checking for knownNumPy limitations.
34363438
34373439
.. c:macro:: NPY_FALSE
34383440

‎numpy/_core/include/numpy/ndarraytypes.h‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
#defineNPY_MAXDIMS 64
4646
/* We cannot change this as it would break ABI: */
4747
#defineNPY_MAXDIMS_LEGACY_ITERS 32
48-
#defineNPY_MAXARGS64
48+
/* NPY_MAXARGSis version dependent and defined in npy_2_compat.h */
4949

5050
/* Used for Converter Functions "O&" code in ParseTuple */
5151
#defineNPY_FAIL 0
@@ -1239,7 +1239,18 @@ typedef struct {
12391239
npy_intpindex;/* current index */
12401240
intnd;/* number of dims */
12411241
npy_intpdimensions[NPY_MAXDIMS_LEGACY_ITERS];/* dimensions */
1242-
PyArrayIterObject*iters[NPY_MAXARGS];/* iterators */
1242+
/*
1243+
* Space for the indivdual iterators, do not specify size publically
1244+
* to allow changing it more easily.
1245+
* One reason is that Cython uses this for checks and only allows
1246+
* growing structs (as of Cython 3.0.6). It also allows NPY_MAXARGS
1247+
* to be runtime dependent.
1248+
*/
1249+
#if defined(NPY_INTERNAL_BUILD)&&NPY_INTERNAL_BUILD
1250+
PyArrayIterObject*iters[64];/* 64 is NPY_MAXARGS */
1251+
#else/* not internal build */
1252+
PyArrayIterObject*iters[];
1253+
#endif
12431254
}PyArrayMultiIterObject;
12441255

12451256
#define_PyMIT(m) ((PyArrayMultiIterObject *)(m))

‎numpy/_core/include/numpy/npy_2_compat.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,14 @@
7676
#endif
7777

7878

79+
#ifNPY_FEATURE_VERSION >=NPY_2_0_API_VERSION
80+
#defineNPY_MAXARGS 64
81+
#elifNPY_ABI_VERSION<0x02000000
82+
#defineNPY_MAXARGS 32
83+
#else
84+
#defineNPY_MAXARGS \
85+
(PyArray_RUNTIME_VERSION >= NPY_2_0_API_VERSION ? 64 : 32)
86+
#endif
87+
88+
7989
#endif/* NUMPY_CORE_INCLUDE_NUMPY_NPY_2_COMPAT_H_ */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp