-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
Unsafe cast deprecation #451
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
Changes from all commits
4708615
cea0a20
c038fe5
f18987a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,22 @@ | |
#include "common.h" | ||
#include "buffer.h" | ||
|
||
/* | ||
* The casting to use for implicit assignment operations resulting from | ||
* in-place operations (like +=) and out= arguments. (Notice that this | ||
* variable is misnamed, but it's part of the public API so I'm not sure we | ||
* can just change it. Maybe someone should try and see if anyone notices. | ||
*/ | ||
/* | ||
* In numpy 1.6 and earlier, this was NPY_UNSAFE_CASTING. In a future | ||
* release, it will become NPY_SAME_KIND_CASTING. Right now, during the | ||
* transitional period, we continue to follow the NPY_UNSAFE_CASTING rules (to | ||
* avoid breaking people's code), but we also check for whether the cast would | ||
* be allowed under the NPY_SAME_KIND_CASTING rules, and if not we issue a | ||
* warning (that people's code will be broken in a future release.) | ||
*/ | ||
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. First line of multiline comments should be blank |
||
NPY_NO_EXPORT NPY_CASTING NPY_DEFAULT_ASSIGN_CASTING = NPY_INTERNAL_UNSAFE_CASTING_BUT_WARN_UNLESS_SAME_KIND; | ||
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. Will static do instead of NPY_NO_EXPORT ? It doesn't look like this is in a header. 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. No, I'm pretty sure that would break separate compilation -- it's declared extern in __multiarray_api.h, and references in other .c files need to be resolved to this definition by the linker. 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. Maybe it should be declared extern in a header? I don't know if the is compatible with NPY_NO_EXORT though, I think that becomes static in one file builds. 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. Well, the trick is that for one-file builds it has to be declared static, in multi-file builds it has to be declared extern in a header, and in builds of third-party packages it needs to be looked up via the famous array-of-pointers API structure. I'm pretty sure declaring it NPY_NO_EXPORT in a .c file plus putting it in numpy_api.py is the right way to automatically achieve all of those things. I found it useful to look at the generated __multiarray_api.h to see exactly what was going on. |
||
|
||
|
||
NPY_NO_EXPORT PyArray_Descr * | ||
_array_find_python_scalar_type(PyObject *op) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -503,12 +503,43 @@ type_num_unsigned_to_signed(int type_num) | |
} | ||
} | ||
|
||
/* | ||
* NOTE: once the UNSAFE_CASTING -> SAME_KIND_CASTING transition is over, | ||
* we should remove NPY_INTERNAL_UNSAFE_CASTING_BUT_WARN_UNLESS_SAME_KIND | ||
* and PyArray_CanCastTypeTo_impl should be renamed back to | ||
* PyArray_CanCastTypeTo. | ||
*/ | ||
static npy_bool | ||
PyArray_CanCastTypeTo_impl(PyArray_Descr *from, PyArray_Descr *to, | ||
NPY_CASTING casting); | ||
|
||
/*NUMPY_API | ||
* Returns true if data of type 'from' may be cast to data of type | ||
* 'to' according to the rule 'casting'. | ||
*/ | ||
NPY_NO_EXPORT npy_bool | ||
PyArray_CanCastTypeTo(PyArray_Descr *from, PyArray_Descr *to, | ||
NPY_CASTING 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. I'm a bit concerned because the GIL now needs to be held in case of the deprecation message. We should really be returning an int instead of npy_bool, but it is probably too late to change that. 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. PyArray_CanCastTypeTo already requires the GIL -- it allocates and DECREFs dtype objects. |
||
{ | ||
if (casting == NPY_INTERNAL_UNSAFE_CASTING_BUT_WARN_UNLESS_SAME_KIND) { | ||
npy_bool unsafe_ok, same_kind_ok; | ||
unsafe_ok = PyArray_CanCastTypeTo_impl(from, to, NPY_UNSAFE_CASTING); | ||
same_kind_ok = PyArray_CanCastTypeTo_impl(from, to, | ||
NPY_SAME_KIND_CASTING); | ||
if (unsafe_ok && !same_kind_ok) { | ||
DEPRECATE("Implicitly casting between incompatible kinds. In " | ||
"a future numpy release, this will raise an error. " | ||
"Use casting=\"unsafe\" if this is intentional."); | ||
} | ||
return unsafe_ok; | ||
} | ||
else { | ||
return PyArray_CanCastTypeTo_impl(from, to, casting); | ||
} | ||
} | ||
|
||
static npy_bool | ||
PyArray_CanCastTypeTo_impl(PyArray_Descr *from, PyArray_Descr *to, | ||
NPY_CASTING casting) | ||
{ | ||
/* If unsafe casts are allowed */ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe NPY_UNSAFE_CASTING_WARN ?
Also the comment formatting should be