Skip to content

Commit e917db4

Browse files
Merge master into deprecated_out_arg
2 parents 29494e1 + aec0164 commit e917db4

File tree

8 files changed

+57
-6
lines changed

8 files changed

+57
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ Also, that release drops support for Python 3.9, making Python 3.10 the minimum
2121
* Added implementation of `dpnp.ndarray.tolist` method [#2652](https://github.com/IntelPython/dpnp/pull/2652)
2222
* Added implementation of `dpnp.frexp` [#2635](https://github.com/IntelPython/dpnp/pull/2635)
2323
* Added implementation of `dpnp.ndarray.tofile` method [#2635](https://github.com/IntelPython/dpnp/pull/2635)
24-
* Extended `pre-commit` configuration with `pyupgrade`, `actionlint`, and `gersemi` hooks [#2658](https://github.com/IntelPython/dpnp/pull/2658)
24+
* Extended `pre-commit` configuration with `pyupgrade`, `actionlint`, and `gersemi` hooks [#2658](https://github.com/IntelPython/dpnp/pull/2658)
25+
* Added implementation of `dpnp.ndarray.tobytes` method [#2656](https://github.com/IntelPython/dpnp/pull/2656)
2526

2627
### Changed
2728

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ if(_use_onemath)
215215
onemath_library
216216
GIT_REPOSITORY https://github.com/uxlfoundation/oneMath.git
217217
GIT_TAG
218-
5c7e1e7a710556e51f70ecc8dd26dfd04e3abf41 # v0.8
218+
6ff3a43e555dbb20357017d48f0f6c6263259895 # v0.9
219219
)
220220
endif()
221221

dpnp/dpnp_array.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2011,7 +2011,42 @@ def to_device(self, device, /, *, stream=None):
20112011
usm_res = self._array_obj.to_device(device, stream=stream)
20122012
return dpnp_array._create_from_usm_ndarray(usm_res)
20132013

2014-
# 'tobytes',
2014+
def tobytes(self, order="C"):
2015+
r"""
2016+
Constructs Python bytes containing the raw data bytes in the array.
2017+
2018+
For full documentation refer to :obj:`numpy.ndarray.tobytes`.
2019+
2020+
Parameters
2021+
----------
2022+
order : {None, "C", "F", "A", "K"}, optional
2023+
Controls the memory layout of the bytes object.
2024+
2025+
Default: ``"C"``.
2026+
2027+
Returns
2028+
-------
2029+
out : bytes
2030+
Python bytes exhibiting a copy of array's raw data.
2031+
2032+
See Also
2033+
--------
2034+
:obj:`dpnp.frombuffer` : Construct a 1D array from Python bytes.
2035+
2036+
Examples
2037+
--------
2038+
>>> import dpnp as np
2039+
>>> x = np.array([[0, 1], [2, 3]], dtype='i2')
2040+
>>> x.tobytes()
2041+
b'\x00\x00\x01\x00\x02\x00\x03\x00'
2042+
>>> x.tobytes("C") == x.tobytes()
2043+
True
2044+
>>> x.tobytes("F")
2045+
b'\x00\x00\x02\x00\x01\x00\x03\x00'
2046+
2047+
"""
2048+
2049+
return self.asnumpy().tobytes(order=order)
20152050

20162051
def tofile(self, fid, sep="", format=""):
20172052
"""

dpnp/dpnp_iface_arraycreation.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,6 +1702,8 @@ def frombuffer(
17021702
:obj:`dpnp.fromfile` : Construct array from data in a text or binary file.
17031703
:obj:`dpnp.fromiter` : Construct array from an iterable object.
17041704
:obj:`dpnp.fromstring` : Construct array from the text data in a string.
1705+
:obj:`ndarray.tobytes` : Construct Python bytes from the raw data bytes
1706+
in the array.
17051707
17061708
Examples
17071709
--------

dpnp/dpnp_iface_bitwise.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def binary_repr(num, width=None):
110110
111111
Examples
112112
--------
113-
>>> import numpy as np
113+
>>> import dpnp as np
114114
>>> np.binary_repr(3)
115115
'11'
116116
>>> np.binary_repr(-3)

dpnp/dpnp_iface_indexing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ def compress(condition, a, axis=None, out=None):
395395
396396
Examples
397397
--------
398-
>>> import numpy as np
398+
>>> import dpnp as np
399399
>>> a = np.array([[1, 2], [3, 4], [5, 6]])
400400
>>> a
401401
array([[1, 2],

dpnp/tests/test_ndarray.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,18 @@ def test_strides(self):
108108
assert xp.full_like(a, fill_value=6) not in a
109109

110110

111+
class TestToBytes:
112+
@pytest.mark.parametrize("order", ["C", "F", "K", "A", None])
113+
def test_roundtrip_binary_str(self, order):
114+
x = generate_random_numpy_array((2, 4, 3), dtype=complex)
115+
x[0, :, 1] = [numpy.nan, numpy.inf, -numpy.inf, numpy.nan]
116+
a = dpnp.array(x)
117+
118+
s = a.tobytes(order=order)
119+
b = dpnp.frombuffer(s, dtype=a.dtype)
120+
assert_array_equal(b, a.asnumpy().flatten(order))
121+
122+
111123
class TestToFile:
112124
def _create_data(self):
113125
x = generate_random_numpy_array((2, 4, 3), dtype=complex)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import unittest
24

35
import numpy
@@ -42,7 +44,6 @@ def test_item(self):
4244
{"shape": (2, 3), "order": "C"},
4345
{"shape": (2, 3), "order": "F"},
4446
)
45-
@pytest.mark.skip("tobytes() method is not supported yet")
4647
class TestNdarrayToBytes(unittest.TestCase):
4748

4849
@testing.for_all_dtypes()

0 commit comments

Comments
 (0)