-
-
Notifications
You must be signed in to change notification settings - Fork 11.2k
ENH: add a casting option 'same_value' and use it in np.astype #29129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
cb8ea97
3859a73
9dc63a5
7cad6af
03e9ceb
93b8bce
87e2487
9aa1e5a
953714e
cd3e144
6d6b045
1293657
d151c91
6254ae5
6922b35
a323a4b
aec8ea1
26c0fa1
0846081
76e01c1
963ea05
4a9a498
10c4493
58a0a09
64b8747
9d55847
a800154
70d92bc
3549d66
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,12 @@ | ||
- Check where PyArray_CopyObject, PyArray_NewCopy, PyArray_CopyInto, array_datetime_as_string, PyArray_Concatenate, PyArray_where are used, do we need a 'same_value' equivalents? | ||
- Is the comment in multiarray/common.c about NPY_DEFAULT_ASSIGN_CASTING warning still correct? | ||
- In PyArray_FromArray(arr, newtype, flags) shoule there be a SAME_VALUE flag? | ||
- In PyArray_FromArray(arr, newtype, flags) should there be a SAME_VALUE flag? | ||
- Examine places where PyArray_CastingConverter is used and add SAME_VALUE handling | ||
- array_astype: now errors, need to fix | ||
- array_datetime_as_string: | ||
- array_copyto: | ||
- PyArray_AssignArray (called with a cast arg) | ||
- PyArray_AssignArray with wheremask (called with a cast arg) | ||
- PyArray_AssignRawScalar with/without wheremask | ||
- PyArray_ConcatenateInto (called with a cast arg) | ||
- PyArray_EinsteinSum (called with a cast arg) | ||
- NpyIter_AdvancedNew (called with a cast arg) | ||
|
||
In CanCast, make sure user defined and datetime dtypes will fail with SAME_VALUE | ||
---- | ||
latest commit: `git grep UNSAFE_CASTING` up to `numpy/_core/src/multiarray/multiarraymodule.c` |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -107,6 +107,12 @@ typedef struct PyArrayMethod_Context_tag { | |
|
||
/* Operand descriptors, filled in by resolve_descriptors */ | ||
PyArray_Descr *const *descriptors; | ||
void * padding; | ||
/* | ||
* Optional flag to pass information into the inner loop | ||
* If set, it will be NPY_CASTING | ||
*/ | ||
uint64_t flags; | ||
/* Structure may grow (this is harmless for DType authors) */ | ||
} PyArrayMethod_Context; | ||
|
||
|
@@ -144,6 +150,11 @@ typedef struct { | |
#define NPY_METH_contiguous_indexed_loop 9 | ||
#define _NPY_METH_static_data 10 | ||
|
||
/* | ||
* Constants for same_value casting | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would be constant for special array method return values, I think (i.e. not just applicable to casts). Is it really worth to just keep this inside the inner-loop? Yes, you need to grab the GIL there, but on the plus side, you could actually consider reporting the error. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought the point of having a return value from the inner loop function is so we can use it to report errors. In general I prefer a code style that tries as much as possible to separate programming metaphors: keep the python c-api calls separate from the "pure" C functions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can be convinced to add this. But it is public API and we call these loops from a lot of places so we need machinery to make sure that all of these places give the same errors. That may be a great refactor! E.g. we could have a single function that does EDIT: I.e. basically, wherever we have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will try to do this as a separate PR. |
||
*/ | ||
#define NPY_SAME_VALUE_OVERFLOW -31 | ||
|
||
|
||
/* | ||
* The resolve descriptors function, must be able to handle NULL values for | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -131,6 +131,10 @@ raw_array_assign_array(int ndim, npy_intp const *shape, | |
npy_clear_floatstatus_barrier((char*)&src_data); | ||
} | ||
|
||
if (same_value_cast) { | ||
cast_info.context.flags |= NPY_SAME_VALUE_CASTING; | ||
seberg marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this wasn't effectively private API right now, I would be opposed to re-using the (I would like if we could note that somewhere. Probably here, since this is where the error for unsupported casts happens.) |
||
} | ||
|
||
/* Ensure number of elements exceeds threshold for threading */ | ||
if (!(method_flags & NPY_METH_REQUIRES_PYAPI)) { | ||
npy_intp nitems = 1, i; | ||
|
@@ -149,6 +153,9 @@ raw_array_assign_array(int ndim, npy_intp const *shape, | |
args, &shape_it[0], strides, | ||
cast_info.auxdata); | ||
if (result < 0) { | ||
if (result == NPY_SAME_VALUE_OVERFLOW) { | ||
goto same_value_overflow; | ||
} | ||
goto fail; | ||
} | ||
} NPY_RAW_ITER_TWO_NEXT(idim, ndim, coord, shape_it, | ||
|
@@ -166,6 +173,8 @@ raw_array_assign_array(int ndim, npy_intp const *shape, | |
} | ||
|
||
return 0; | ||
same_value_overflow: | ||
PyErr_SetString(PyExc_ValueError, "overflow in 'same_value' cast"); | ||
fail: | ||
NPY_END_THREADS; | ||
NPY_cast_info_xfree(&cast_info); | ||
|
Uh oh!
There was an error while loading. Please reload this page.