Skip to content

Commit

Permalink
BUG: ensure _UFuncNoLoopError can be pickled (#17377)
Browse files Browse the repository at this point in the history
* BUG: ensure _UFuncNoLoopError can be pickled; closes #16490

* update quickstart.rst

* add round trip pickle test

* move _ArrayMemoryError picking test
  • Loading branch information
bfgray3 authored and charris committed Dec 4, 2020
1 parent 672a6c8 commit 7cf3ec9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion doc/source/user/quickstart.rst
Expand Up @@ -361,7 +361,7 @@ existing array rather than create a new one.
>>> a += b # b is not automatically converted to integer type
Traceback (most recent call last):
...
numpy.core._exceptions.UFuncTypeError: Cannot cast ufunc 'add' output from dtype('float64') to dtype('int64') with casting rule 'same_kind'
numpy.core._exceptions._UFuncOutputCastingError: Cannot cast ufunc 'add' output from dtype('float64') to dtype('int64') with casting rule 'same_kind'

When operating with arrays of different types, the type of the resulting
array corresponds to the more general or precise one (a behavior known
Expand Down
2 changes: 0 additions & 2 deletions numpy/core/_exceptions.py
Expand Up @@ -26,8 +26,6 @@ def _display_as_base(cls):
"""
assert issubclass(cls, Exception)
cls.__name__ = cls.__base__.__name__
cls.__qualname__ = cls.__base__.__qualname__
set_module(cls.__base__.__module__)(cls)
return cls


Expand Down
16 changes: 16 additions & 0 deletions numpy/core/tests/test__exceptions.py
@@ -1,11 +1,21 @@
"""
Tests of the ._exceptions module. Primarily for exercising the __str__ methods.
"""

import pickle

import numpy as np

_ArrayMemoryError = np.core._exceptions._ArrayMemoryError
_UFuncNoLoopError = np.core._exceptions._UFuncNoLoopError

class TestArrayMemoryError:
def test_pickling(self):
""" Test that _ArrayMemoryError can be pickled """
error = _ArrayMemoryError((1023,), np.dtype(np.uint8))
res = pickle.loads(pickle.dumps(error))
assert res._total_size == error._total_size

def test_str(self):
e = _ArrayMemoryError((1023,), np.dtype(np.uint8))
str(e) # not crashing is enough
Expand Down Expand Up @@ -40,3 +50,9 @@ def test__total_size(self):

e = _ArrayMemoryError((2, 4), np.dtype((np.uint64, 16)))
assert e._total_size == 1024


class TestUFuncNoLoopError:
def test_pickling(self):
""" Test that _UFuncNoLoopError can be pickled """
assert isinstance(pickle.dumps(_UFuncNoLoopError), bytes)

0 comments on commit 7cf3ec9

Please sign in to comment.