diff --git a/CHANGELOG.md b/CHANGELOG.md index b632cad907a..7a994baddb5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,7 +21,8 @@ Also, that release drops support for Python 3.9, making Python 3.10 the minimum * Added implementation of `dpnp.ndarray.tolist` method [#2652](https://github.com/IntelPython/dpnp/pull/2652) * Added implementation of `dpnp.frexp` [#2635](https://github.com/IntelPython/dpnp/pull/2635) * Added implementation of `dpnp.ndarray.tofile` method [#2635](https://github.com/IntelPython/dpnp/pull/2635) -* Extended `pre-commit` configuration with `pyupgrade`, `actionlint`, and `gersemi` hooks [#2658](https://github.com/IntelPython/dpnp/pull/2658) +* 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) ### Changed diff --git a/dpnp/dpnp_array.py b/dpnp/dpnp_array.py index 5963b0b8ebc..365d01a179a 100644 --- a/dpnp/dpnp_array.py +++ b/dpnp/dpnp_array.py @@ -2011,7 +2011,42 @@ def to_device(self, device, /, *, stream=None): usm_res = self._array_obj.to_device(device, stream=stream) return dpnp_array._create_from_usm_ndarray(usm_res) - # 'tobytes', + def tobytes(self, order="C"): + r""" + Constructs Python bytes containing the raw data bytes in the array. + + For full documentation refer to :obj:`numpy.ndarray.tobytes`. + + Parameters + ---------- + order : {None, "C", "F", "A", "K"}, optional + Controls the memory layout of the bytes object. + + Default: ``"C"``. + + Returns + ------- + out : bytes + Python bytes exhibiting a copy of array's raw data. + + See Also + -------- + :obj:`dpnp.frombuffer` : Construct a 1D array from Python bytes. + + Examples + -------- + >>> import dpnp as np + >>> x = np.array([[0, 1], [2, 3]], dtype='i2') + >>> x.tobytes() + b'\x00\x00\x01\x00\x02\x00\x03\x00' + >>> x.tobytes("C") == x.tobytes() + True + >>> x.tobytes("F") + b'\x00\x00\x02\x00\x01\x00\x03\x00' + + """ + + return self.asnumpy().tobytes(order=order) def tofile(self, fid, sep="", format=""): """ diff --git a/dpnp/dpnp_iface_arraycreation.py b/dpnp/dpnp_iface_arraycreation.py index 5e492ba92f5..4b5633864b1 100644 --- a/dpnp/dpnp_iface_arraycreation.py +++ b/dpnp/dpnp_iface_arraycreation.py @@ -1702,6 +1702,8 @@ def frombuffer( :obj:`dpnp.fromfile` : Construct array from data in a text or binary file. :obj:`dpnp.fromiter` : Construct array from an iterable object. :obj:`dpnp.fromstring` : Construct array from the text data in a string. + :obj:`ndarray.tobytes` : Construct Python bytes from the raw data bytes + in the array. Examples -------- diff --git a/dpnp/dpnp_iface_bitwise.py b/dpnp/dpnp_iface_bitwise.py index 8d8b7e3570d..f8ac12a3c48 100644 --- a/dpnp/dpnp_iface_bitwise.py +++ b/dpnp/dpnp_iface_bitwise.py @@ -110,7 +110,7 @@ def binary_repr(num, width=None): Examples -------- - >>> import numpy as np + >>> import dpnp as np >>> np.binary_repr(3) '11' >>> np.binary_repr(-3) diff --git a/dpnp/dpnp_iface_indexing.py b/dpnp/dpnp_iface_indexing.py index 13fc26ec30b..482cc862057 100644 --- a/dpnp/dpnp_iface_indexing.py +++ b/dpnp/dpnp_iface_indexing.py @@ -395,7 +395,7 @@ def compress(condition, a, axis=None, out=None): Examples -------- - >>> import numpy as np + >>> import dpnp as np >>> a = np.array([[1, 2], [3, 4], [5, 6]]) >>> a array([[1, 2], diff --git a/dpnp/tests/test_ndarray.py b/dpnp/tests/test_ndarray.py index 6fe5638d785..7b334ddbd97 100644 --- a/dpnp/tests/test_ndarray.py +++ b/dpnp/tests/test_ndarray.py @@ -108,6 +108,18 @@ def test_strides(self): assert xp.full_like(a, fill_value=6) not in a +class TestToBytes: + @pytest.mark.parametrize("order", ["C", "F", "K", "A", None]) + def test_roundtrip_binary_str(self, order): + x = generate_random_numpy_array((2, 4, 3), dtype=complex) + x[0, :, 1] = [numpy.nan, numpy.inf, -numpy.inf, numpy.nan] + a = dpnp.array(x) + + s = a.tobytes(order=order) + b = dpnp.frombuffer(s, dtype=a.dtype) + assert_array_equal(b, a.asnumpy().flatten(order)) + + class TestToFile: def _create_data(self): x = generate_random_numpy_array((2, 4, 3), dtype=complex) diff --git a/dpnp/tests/third_party/cupy/core_tests/test_ndarray_conversion.py b/dpnp/tests/third_party/cupy/core_tests/test_ndarray_conversion.py index 90961602eba..efd888094c9 100644 --- a/dpnp/tests/third_party/cupy/core_tests/test_ndarray_conversion.py +++ b/dpnp/tests/third_party/cupy/core_tests/test_ndarray_conversion.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import unittest import numpy @@ -42,7 +44,6 @@ def test_item(self): {"shape": (2, 3), "order": "C"}, {"shape": (2, 3), "order": "F"}, ) -@pytest.mark.skip("tobytes() method is not supported yet") class TestNdarrayToBytes(unittest.TestCase): @testing.for_all_dtypes()