diff options
| author | Sebastian Berg <sebastian@sipsolutions.net> | 2020-11-19 17:48:30 -0600 |
|---|---|---|
| committer | Sebastian Berg <sebastian@sipsolutions.net> | 2020-11-24 13:07:26 -0600 |
| commit | 706796733f6c643046009677c46a5fbc3ff2029d (patch) | |
| tree | 89dd428b91603b69429816b472c97a48c476fe99 /numpy | |
| parent | 5f316a85dd205cb0a6134a5a1e6b5bc76943f17c (diff) | |
| download | numpy-706796733f6c643046009677c46a5fbc3ff2029d.tar.gz | |
DEP: Give a deprecation warning when unpickling an object scalar
This should not really be possible, but just in case it happens
warn about it so that we can raise an error in the future.
Update numpy/core/src/multiarray/multiarraymodule.c
Co-authored-by: Matti Picus <matti.picus@gmail.com>
Diffstat (limited to 'numpy')
| -rw-r--r-- | numpy/core/src/multiarray/multiarraymodule.c | 8 | ||||
| -rw-r--r-- | numpy/core/tests/test_deprecations.py | 14 | ||||
| -rw-r--r-- | numpy/core/tests/test_records.py | 3 |
3 files changed, 24 insertions, 1 deletions
diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c index 8dcd67c20..a4efdfac3 100644 --- a/numpy/core/src/multiarray/multiarraymodule.c +++ b/numpy/core/src/multiarray/multiarraymodule.c @@ -2011,6 +2011,14 @@ array_scalar(PyObject *NPY_UNUSED(ignored), PyObject *args, PyObject *kwds) } if (PyDataType_FLAGCHK(typecode, NPY_LIST_PICKLE)) { if (typecode->type_num == NPY_OBJECT) { + /* Deprecated 2020-11-24, NumPy 1.20 */ + if (DEPRECATE( + "Unpickling a scalar with object dtype is deprecated. " + "Object scalars should never be created. If this was a " + "properly created pickle, please open a NumPy issue. In " + "a best effort this returns the original object.") < 0) { + return NULL; + } Py_INCREF(obj); return obj; } diff --git a/numpy/core/tests/test_deprecations.py b/numpy/core/tests/test_deprecations.py index 380b78f67..a67fe62c3 100644 --- a/numpy/core/tests/test_deprecations.py +++ b/numpy/core/tests/test_deprecations.py @@ -771,3 +771,17 @@ class TestDeprecateSubarrayDTypeDuringArrayCoercion(_DeprecationTestCase): np.array(arr, dtype="(2,2)f") self.assert_deprecated(check) + + +class TestDeprecatedUnpickleObjectScalar(_DeprecationTestCase): + # Deprecated 2020-11-24, NumPy 1.20 + """ + Technically, it should be impossible to create numpy object scalars, + but there was an unpickle path that would in theory allow it. That + path is invalid and must lead to the warning. + """ + message = "Unpickling a scalar with object dtype is deprecated." + + def test_deprecated(self): + ctor = np.core.multiarray.scalar + self.assert_deprecated(lambda: ctor(np.dtype("O"), 1)) diff --git a/numpy/core/tests/test_records.py b/numpy/core/tests/test_records.py index e30d8e65c..4d4b4b515 100644 --- a/numpy/core/tests/test_records.py +++ b/numpy/core/tests/test_records.py @@ -432,7 +432,8 @@ class TestRecord: assert a[0] == unpickled # Also check the similar (impossible) "object scalar" path: - assert ctor(np.dtype("O"), data) is data + with pytest.warns(DeprecationWarning): + assert ctor(np.dtype("O"), data) is data def test_objview_record(self): # https://github.com/numpy/numpy/issues/2599 |
