Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Doc fixes and improved test failure output
  • Loading branch information
zooba committed Apr 2, 2024
commit 68b9d8ed9ae41bef09c2b11694b65a088c150d65
13 changes: 7 additions & 6 deletions Doc/c-api/long.rst
Original file line number Diff line number Diff line change
Expand Up @@ -113,21 +113,21 @@ distinguished from a number. Use :c:func:`PyErr_Occurred` to disambiguate.
retrieved from the resulting value using :c:func:`PyLong_AsVoidPtr`.


.. c:function:: PyObject* PyLong_FromNativeBytes(const void* buffer, size_t n_bytes, int endianness)
.. c:function:: PyObject* PyLong_FromNativeBytes(const void* buffer, size_t n_bytes, int flags)

Create a Python integer from the value contained in the first *n_bytes* of
*buffer*, interpreted as a two's-complement signed number.

*flags* are as for :c:func:`PyLong_AsNativeBytes`. Passing ``-1`` will select
the native endian that CPython was compiled with and assume that the
most-significant bit is a sign bit. ``Py_ASNATIVEBYTES_REJECT_NEGATIVE`` has
no effect. Passing ``Py_ASNATIVEBYTES_UNSIGNED_BUFFER`` will produce the same
result as calling :c:func:`PyLong_FromUnsignedNativeBytes`.
most-significant bit is a sign bit. Passing
``Py_ASNATIVEBYTES_UNSIGNED_BUFFER`` will produce the same result as calling
:c:func:`PyLong_FromUnsignedNativeBytes`. Other flags are ignored.

.. versionadded:: 3.13


.. c:function:: PyObject* PyLong_FromUnsignedNativeBytes(const void* buffer, size_t n_bytes, int endianness)
.. c:function:: PyObject* PyLong_FromUnsignedNativeBytes(const void* buffer, size_t n_bytes, int flags)

Create a Python integer from the value contained in the first *n_bytes* of
*buffer*, interpreted as an unsigned number.
Expand Down Expand Up @@ -416,6 +416,7 @@ distinguished from a number. Use :c:func:`PyErr_Occurred` to disambiguate.
*flags* is a combination of the flags shown in the table below, or ``-1`` to
select defaults that behave most like a C cast (currently,
``Py_ASNATIVEBYTES_NATIVE_ENDIAN | Py_ASNATIVEBYTES_UNSIGNED_BUFFER``).
Note that ``Py_ASNATIVEBYTES_DEFAULTS`` cannot be combined with other flags.

============================================= ======
Flag Value
Expand All @@ -429,7 +430,7 @@ distinguished from a number. Use :c:func:`PyErr_Occurred` to disambiguate.
============================================= ======

Specifying ``Py_ASNATIVEBYTES_NATIVE_ENDIAN`` will override any other endian
flags.
flags. Passing ``2`` is reserved.

Specifying ``Py_ASNATIVEBYTES_UNSIGNED_BUFFER`` allows positive input values
that would set the most-significant bit to be converted. For example,
Expand Down
12 changes: 9 additions & 3 deletions Lib/test/test_capi/test_long.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,16 +660,22 @@ def test_long_asnativebytes_fuzz(self):
self.assertEqual(bytes_le, buffer[:n])

actual = asnativebytes(v, buffer, n, 4)
self.assertEqual(1, v)
self.assertIn(actual, expect_2, bytes_be.hex())
actual = asnativebytes(v, buffer, n, 5)
self.assertIn(actual, expect_2, bytes_be.hex())
except AssertionError:
except AssertionError as ex:
value_hex = ''.join(reversed([
f'{b:02X}{"" if i % 8 else "_"}'
for i, b in enumerate(bytes_le, start=1)
])).strip('_')
if support.verbose:
print()
print(''.join(f'{b:02X}' for b in bytes_be))
print(n, 'bytes')
print('hex =', value_hex)
print('int =', v)
raise
raise
raise AssertionError(f"Value: 0x{value_hex}") from ex

def test_long_fromnativebytes(self):
import math
Expand Down