Skip to content

Commit 679dcba

Browse files
authored
Add dpnp.ndarray.__bytes__ method (#2671)
The PR implements a special python method `__bytes__` for `dpnp.ndarray`.
1 parent a270259 commit 679dcba

File tree

4 files changed

+8
-6
lines changed

4 files changed

+8
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Also, that release drops support for Python 3.9, making Python 3.10 the minimum
2424
* Extended `pre-commit` configuration with `pyupgrade`, `actionlint`, and `gersemi` hooks [#2658](https://github.com/IntelPython/dpnp/pull/2658)
2525
* Added implementation of `dpnp.ndarray.tobytes` method [#2656](https://github.com/IntelPython/dpnp/pull/2656)
2626
* Added implementation of `dpnp.ndarray.__format__` method [#2662](https://github.com/IntelPython/dpnp/pull/2662)
27+
* Added implementation of `dpnp.ndarray.__bytes__` method [#2671](https://github.com/IntelPython/dpnp/pull/2671)
2728

2829
### Changed
2930

doc/reference/ndarray.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ and return the appropriate scalar.
436436
:toctree: generated/
437437
:nosignatures:
438438

439+
ndarray.__bytes__
439440
ndarray.__index__
440441
ndarray.__int__
441442
ndarray.__float__

dpnp/dpnp_array.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ def __bool__(self, /):
185185
"""``True`` if `self` else ``False``."""
186186
return self._array_obj.__bool__()
187187

188+
def __bytes__(self):
189+
r"""Return :math:`\text{bytes(self)}`."""
190+
return bytes(self.asnumpy())
191+
188192
# '__class__',
189193
# `__class_getitem__`,
190194

dpnp/tests/third_party/cupy/core_tests/test_ndarray.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -653,32 +653,28 @@ def test_size_zero_dim_array_with_axis(self):
653653

654654
class TestPythonInterface(unittest.TestCase):
655655

656-
@pytest.mark.skip("__bytes__ is not supported")
657656
@testing.for_all_dtypes()
658657
@testing.numpy_cupy_equal()
659658
def test_bytes_tobytes(self, xp, dtype):
660659
x = testing.shaped_arange((3, 4, 5), xp, dtype)
661660
return bytes(x)
662661

663-
@pytest.mark.skip("__bytes__ is not supported")
664662
@testing.for_all_dtypes()
665663
@testing.numpy_cupy_equal()
666664
def test_bytes_tobytes_empty(self, xp, dtype):
667-
x = xp.empty((0,), dtype)
665+
x = xp.empty((0,), dtype=dtype)
668666
return bytes(x)
669667

670-
@pytest.mark.skip("__bytes__ is not supported")
671668
@testing.for_all_dtypes()
672669
@testing.numpy_cupy_equal()
673670
def test_bytes_tobytes_empty2(self, xp, dtype):
674-
x = xp.empty((3, 0, 4), dtype)
671+
x = xp.empty((3, 0, 4), dtype=dtype)
675672
return bytes(x)
676673

677674
# The result of bytes(numpy.array(scalar)) is the same as bytes(scalar)
678675
# if scalar is of an integer dtype including bool_. It's spec is
679676
# bytes(int): bytes object of size given by the parameter initialized with
680677
# null bytes.
681-
@pytest.mark.skip("__bytes__ is not supported")
682678
@testing.for_float_dtypes()
683679
@testing.numpy_cupy_equal()
684680
def test_bytes_tobytes_scalar_array(self, xp, dtype):

0 commit comments

Comments
 (0)