Skip to content

Commit

Permalink
Clarify, document both use cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
gpshead committed Feb 17, 2024
1 parent 97292ba commit 71d527f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 16 deletions.
71 changes: 56 additions & 15 deletions Doc/c-api/long.rst
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,8 @@ distinguished from a number. Use :c:func:`PyErr_Occurred` to disambiguate.
Copy the Python integer value to a native *buffer* of size *n_bytes*::
unsigned bignum[4]; // Example size chosen by random die roll.
Py_ssize_t bytes = PyLong_AsNativeBits(v, &bignum, sizeof(bignum), -1);
int32_t value;
Py_ssize_t bytes = PyLong_AsNativeBits(pylong, &value, sizeof(value), -1);
if (bytes < 0) {
// A Python exception was set with the reason.
return NULL;
Expand All @@ -368,36 +368,77 @@ distinguished from a number. Use :c:func:`PyErr_Occurred` to disambiguate.
// Success!
}
else {
// Overflow occurred, but 'bignum' contains a truncated value.
// Overflow occurred, but 'value' contains the truncated
// lowest bits of pylong.
}
The above example may look *similar* to
:c:func:`PyLong_As* <PyLong_AsSize_t>`
but instead fills in a specific caller defined type and never raises an
error about of the :class:`int` *pylong*'s value regardless of *n_bytes*
or the returned byte count.
To get at the entire potentially big Python value, this can be used to
reserve enough space and copy it::
// Ask how much space we need.
Py_ssize_t expected = PyLong_AsNativeBits(pylong, NULL, 0, -1);
if (expected < 0) {
// A Python exception was set with the reason.
return NULL;
}
assert(expected != 0); // Impossible per the API definition.
uint8_t *bignum = malloc(expected);
if (!bignum) {
PyErr_SetString(PyExc_MemoryError, "bignum malloc failed.");
return NULL;
}
// Safely get the entire value.
Py_ssize_t bytes = PyLong_AsNativeBits(pylong, bignum, expected, -1);
if (bytes < 0 || bytes > expected) { // Be safe, should not be possible.
if (!PyErr_Occurred()) {
PyErr_SetString(PyExc_RuntimeError,
"Unexpected bignum truncation after a size check.");
}
free(bignum);
return NULL;
}
// The expected success given the above pre-check.
// ... use bignum ...
free(bignum);
*endianness* may be passed ``-1`` for the native endian that CPython was
compiled with, or ``0`` for big endian and ``1`` for little.
Return ``-1`` with an exception raised if *pylong* cannot be interpreted as
Returns ``-1`` with an exception raised if *pylong* cannot be interpreted as
an integer. Otherwise, return the size of the buffer required to store the
value. If this is equal to or less than *n_bytes*, the entire value was
copied.
copied. ``0`` will never be returned.
Unless an exception is raised, all *n_bytes* of the buffer will be written
with as much of the value as can fit. This allows the caller to ignore all
non-negative results if the intent is to match the typical behavior of a
C-style downcast. No exception is set for this case.
Unless an exception is raised, all *n_bytes* of the buffer will always be
written. In the case of truncation, as many of the lowest bits of the value
as could fit are written. This allows the caller to ignore all non-negative
results if the intent is to match the typical behavior of a C-style
downcast. No exception is set on truncation.
Values are always copied as two's-complement, and sufficient buffer will be
Values are always copied as two's-complement and sufficient buffer will be
requested to include a sign bit. For example, this may cause an value that
fits into 8 bytes when treated as unsigned to request 9 bytes, even though
all eight bytes were copied into the buffer. What has been omitted is the
zero sign bit, which is redundant when the intention is to treat the value as
unsigned.
zero sign bit -- redundant if the caller's intention is to treat the value
as unsigned.
Passing zero to *n_bytes* will return the requested buffer size.
Passing zero to *n_bytes* will return the size of a buffer that'd at least
be large enough to hold the value.
.. note::
When the value does not fit in the provided buffer, the requested size
returned from the function may be larger than necessary. Passing 0 to this
function is not an accurate way to determine the bit length of a value.
returned from the function may be larger than necessary for unsigned use
cases. Passing ``n_bytes=0`` to this function is not a way to determine
the bit length of a value, the result will merely indicate a buffer size
sufficient to hold it. When passed ``n_bytes=0`` the return value may be
larger than technically necessary.
.. versionadded:: 3.13
Expand Down
7 changes: 6 additions & 1 deletion Lib/test/test_capi/test_long.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,12 @@ def test_long_asnativebytes(self):
if support.verbose:
print(f"SIZEOF_SIZE={SZ}\n{MAX_SSIZE=:016X}\n{MAX_USIZE=:016X}")

# These tests check that the requested buffer size is correct
# These tests check that the requested buffer size is correct.
# This matches our current implementation: We only specify that the
# return value is a size *sufficient* to hold the result when queried
# using n_bytes=0. If our implementation changes, feel free to update
# the expectations here -- or loosen them to be range checks.
# (i.e. 0 *could* be stored in 1 byte and 512 in 2)
for v, expect in [
(0, SZ),
(512, SZ),
Expand Down

0 comments on commit 71d527f

Please sign in to comment.