10000 Merge pull request #472 from certik/1.7.x-backport · numpy/numpy@86fbf95 · GitHub
[go: up one dir, main page]

Skip to content

Commit 86fbf95

Browse files
committed
Merge pull request #472 from certik/1.7.x-backport
1.7.x backport
2 parents 7ae2fb0 + 9ea0f0f commit 86fbf95

22 files changed

+252
-13
lines changed

doc/release/1.7.0-notes.rst

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,14 @@ np.diagonal, numpy 1.7 produces a FutureWarning if it detects
3333
that you may be attemping to write to such an array. See the documentation
3434
for array indexing for details.
3535

36-
The default casting rule for UFunc out= parameters has been changed from
37-
'unsafe' to 'same_kind'. Most usages which violate the 'same_kind'
38-
rule are likely bugs, so this change may expose previously undetected
39-
errors in projects that depend on NumPy.
36+
In a future version of numpy, the default casting rule for UFunc out=
37+
parameters will be changed from 'unsafe' to 'same_kind'. (This also
38+
applies to in-place operations like a += b, which is equivalent to
39+
np.add(a, b, out=a).) Most usages which violate the 'same_kind' rule
40+
are likely bugs, so this change may expose previously undetected
41+
errors in projects that depend on NumPy. In this version of numpy,
42+
such usages will continue to succeed, but will raise a
43+
DeprecationWarning.
4044

4145
Full-array boolean indexing has been optimized to use a different,
4246
optimized code path. This code path should produce the same results,

doc/source/reference/ufuncs.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,12 @@ advanced usage and will not typically be used.
309309
'equiv', 'safe', 'same_kind', or 'unsafe'. See :func:`can_cast` for
310310
explanations of the parameter values.
311311

312+
In a future version of numpy, this argument will default to
313+
'same_kind'. As part of this transition, starting in version 1.7,
314+
ufuncs will produce a DeprecationWarning for calls which are
315+
allowed under the 'unsafe' rules, but not under the 'same_kind'
316+
rules.
317+
312318
*order*
313319

314320
.. versionadded:: 1.6

numpy/core/code_generators/numpy_api.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414

1515
multiarray_global_vars = {
1616
'NPY_NUMUSERTYPES': 7,
17+
'NPY_DEFAULT_ASSIGN_CASTING': 292,
1718
}
1819

1920
multiarray_global_vars_types = {
2021
'NPY_NUMUSERTYPES': 'int',
22+
'NPY_DEFAULT_ASSIGN_CASTING': 'NPY_CASTING',
2123
}
2224

2325
multiarray_scalar_bool_values = {

numpy/core/include/numpy/ndarraytypes.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,14 @@ typedef enum {
199199
/* Allow safe casts or casts within the same kind */
200200
NPY_SAME_KIND_CASTING=3,
201201
/* Allow any casts */
202-
NPY_UNSAFE_CASTING=4
203-
} NPY_CASTING;
202+
NPY_UNSAFE_CASTING=4,
204203

205-
/* The default casting to use for typical assignment operations */
206-
#define NPY_DEFAULT_ASSIGN_CASTING NPY_SAME_KIND_CASTING
204+
/*
205+
* Temporary internal definition only, will be removed in upcoming
206+
* release, see below
207+
* */
208+
NPY_INTERNAL_UNSAFE_CASTING_BUT_WARN_UNLESS_SAME_KIND = 100,
209+
} NPY_CASTING;
207210

208211
typedef enum {
209212
NPY_CLIP=0,

numpy/core/src/multiarray/array_assign.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ broadcast_error: {
7373
PyUString_ConcatAndDel(&errmsg,
7474
build_shape_string(ndim, shape));
7575
PyErr_SetObject(PyExc_ValueError, errmsg);
76+
Py_DECREF(errmsg);
7677

7778
return -1;
7879
}

numpy/core/src/multiarray/array_assign_array.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ PyArray_AssignArray(PyArrayObject *dst, PyArrayObject *src,
284284
PyUString_FromFormat(" according to the rule %s",
285285
npy_casting_to_string(casting)));
286286
PyErr_SetObject(PyExc_TypeError, errmsg);
287+
Py_DECREF(errmsg);
287288
goto fail;
288289
}
289290

numpy/core/src/multiarray/array_assign_scalar.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ PyArray_AssignRawScalar(PyArrayObject *dst,
209209
PyUString_FromFormat(" according to the rule %s",
210210
npy_casting_to_string(casting)));
211211
PyErr_SetObject(PyExc_TypeError, errmsg);
212+
Py_DECREF(errmsg);
212213
return -1;
213214
}
214215

numpy/core/src/multiarray/common.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,22 @@
1313
#include "common.h"
1414
#include "buffer.h"
1515

16+
/*
17+
* The casting to use for implicit assignment operations resulting from
18+
* in-place operations (like +=) and out= arguments. (Notice that this
19+
* variable is misnamed, but it's part of the public API so I'm not sure we
20+
* can just change it. Maybe someone should try and see if anyone notices.
21+
*/
22+
/*
23+
* In numpy 1.6 and earlier, this was NPY_UNSAFE_CASTING. In a future
24+
* release, it will become NPY_SAME_KIND_CASTING. Right now, during the
25+
* transitional period, we continue to follow the NPY_UNSAFE_CASTING rules (to
26+
* avoid breaking people's code), but we also check for whether the cast would
27+
* be allowed under the NPY_SAME_KIND_CASTING rules, and if not we issue a
28+
* warning (that people's code will be broken in a future release.)
29+
*/
30+
NPY_NO_EXPORT NPY_CASTING NPY_DEFAULT_ASSIGN_CASTING = NPY_INTERNAL_UNSAFE_CASTING_BUT_WARN_UNLESS_SAME_KIND;
31+
1632

1733
NPY_NO_EXPORT PyArray_Descr *
1834
_array_find_python_scalar_type(PyObject *op)

numpy/core/src/multiarray/convert_datatype.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,12 +503,43 @@ type_num_unsigned_to_signed(int type_num)
503503
}
504504
}
505505

506+
/*
507+
* NOTE: once the UNSAFE_CASTING -> SAME_KIND_CASTING transition is over,
508+
* we should remove NPY_INTERNAL_UNSAFE_CASTING_BUT_WARN_UNLESS_SAME_KIND
509+
* and PyArray_CanCastTypeTo_impl should be renamed back to
510+
* PyArray_CanCastTypeTo.
511+
*/
512+
static npy_bool
513+
PyArray_CanCastTypeTo_impl(PyArray_Descr *from, PyArray_Descr *to,
514+
NPY_CASTING casting);
515+
506516
/*NUMPY_API
507517
* Returns true if data of type 'from' may be cast to data of type
508518
* 'to' according to the rule 'casting'.
509519
*/
510520
NPY_NO_EXPORT npy_bool
511521
PyArray_CanCastTypeTo(PyArray_Descr *from, PyArray_Descr *to,
522+
NPY_CASTING casting)
523+
{
524+
if (casting == NPY_INTERNAL_UNSAFE_CASTING_BUT_WARN_UNLESS_SAME_KIND) {
525+
npy_bool unsafe_ok, same_kind_ok;
526+
unsafe_ok = PyArray_CanCastTypeTo_impl(from, to, NPY_UNSAFE_CASTING);
527+
same_kind_ok = PyArray_CanCastTypeTo_impl(from, to,
528+
NPY_SAME_KIND_CASTING);
529+
if (unsafe_ok && !same_kind_ok) {
530+
DEPRECATE("Implicitly casting between incompatible kinds. In "
531+
"a future numpy release, this will raise an error. "
532+
"Use casting=\"unsafe\" if this is intentional.");
533+
}
534+
return unsafe_ok;
535+
}
536+
else {
537+
return PyArray_CanCastTypeTo_impl(from, to, casting);
538+
}
539+
}
540+
541+
static npy_bool
542+
PyArray_CanCastTypeTo_impl(PyArray_Descr *from, PyArray_Descr *to,
512543
NPY_CASTING casting)
513544
{
514545
/* If unsafe casts are allowed */

numpy/core/src/multiarray/ctors.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1818,6 +1818,7 @@ PyArray_FromArray(PyArrayObject *arr, PyArray_Descr *newtype, int flags)
18181818
PyUString_FromFormat(" according to the rule %s",
18191819
npy_casting_to_string(casting)));
18201820
PyErr_SetObject(PyExc_TypeError, errmsg);
1821+
Py_DECREF(errmsg);
18211822

18221823
Py_DECREF(newtype);
18231824
return NULL;

0 commit comments

Comments
 (0)
0