Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions doc/reference/ndarray.rst
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ and return the appropriate scalar.
:toctree: generated/
:nosignatures:

ndarray.__bytes__
ndarray.__index__
ndarray.__int__
ndarray.__float__
Expand Down
4 changes: 4 additions & 0 deletions dpnp/dpnp_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__`,

Expand Down
8 changes: 2 additions & 6 deletions dpnp/tests/third_party/cupy/core_tests/test_ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Loading