diff --git a/CHANGELOG.md b/CHANGELOG.md index 2bb33ea4805..d2b283086e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ Also, that release drops support for Python 3.9, making Python 3.10 the minimum * Extended `pre-commit` configuration with `pyupgrade`, `actionlint`, and `gersemi` hooks [#2658](https://github.com/IntelPython/dpnp/pull/2658) * Added implementation of `dpnp.ndarray.tobytes` method [#2656](https://github.com/IntelPython/dpnp/pull/2656) * Added implementation of `dpnp.ndarray.__format__` method [#2662](https://github.com/IntelPython/dpnp/pull/2662) +* Added implementation of `dpnp.ndarray.__bytes__` method [#2671](https://github.com/IntelPython/dpnp/pull/2671) ### Changed diff --git a/doc/reference/ndarray.rst b/doc/reference/ndarray.rst index 09d60a307b0..6fc1fb07675 100644 --- a/doc/reference/ndarray.rst +++ b/doc/reference/ndarray.rst @@ -436,6 +436,7 @@ and return the appropriate scalar. :toctree: generated/ :nosignatures: + ndarray.__bytes__ ndarray.__index__ ndarray.__int__ ndarray.__float__ diff --git a/dpnp/dpnp_array.py b/dpnp/dpnp_array.py index 3e747f00ee4..656f099a0c4 100644 --- a/dpnp/dpnp_array.py +++ b/dpnp/dpnp_array.py @@ -185,6 +185,10 @@ def __bool__(self, /): """``True`` if `self` else ``False``.""" return self._array_obj.__bool__() + def __bytes__(self): + r"""Return :math:`\text{bytes(self)}`.""" + return bytes(self.asnumpy()) + # '__class__', # `__class_getitem__`, diff --git a/dpnp/tests/third_party/cupy/core_tests/test_ndarray.py b/dpnp/tests/third_party/cupy/core_tests/test_ndarray.py index 6f79a6a231f..200a29d2926 100644 --- a/dpnp/tests/third_party/cupy/core_tests/test_ndarray.py +++ b/dpnp/tests/third_party/cupy/core_tests/test_ndarray.py @@ -653,32 +653,28 @@ def test_size_zero_dim_array_with_axis(self): class TestPythonInterface(unittest.TestCase): - @pytest.mark.skip("__bytes__ is not supported") @testing.for_all_dtypes() @testing.numpy_cupy_equal() def test_bytes_tobytes(self, xp, dtype): x = testing.shaped_arange((3, 4, 5), xp, dtype) return bytes(x) - @pytest.mark.skip("__bytes__ is not supported") @testing.for_all_dtypes() @testing.numpy_cupy_equal() def test_bytes_tobytes_empty(self, xp, dtype): - x = xp.empty((0,), dtype) + x = xp.empty((0,), dtype=dtype) return bytes(x) - @pytest.mark.skip("__bytes__ is not supported") @testing.for_all_dtypes() @testing.numpy_cupy_equal() def test_bytes_tobytes_empty2(self, xp, dtype): - x = xp.empty((3, 0, 4), dtype) + x = xp.empty((3, 0, 4), dtype=dtype) return bytes(x) # The result of bytes(numpy.array(scalar)) is the same as bytes(scalar) # if scalar is of an integer dtype including bool_. It's spec is # bytes(int): bytes object of size given by the parameter initialized with # null bytes. - @pytest.mark.skip("__bytes__ is not supported") @testing.for_float_dtypes() @testing.numpy_cupy_equal() def test_bytes_tobytes_scalar_array(self, xp, dtype):