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

Skip to content

Commit 761db5c

Browse files
committed
Docs
1 parent 043947f commit 761db5c

4 files changed

Lines changed: 70 additions & 2 deletions

File tree

Doc/c-api/long.rst

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,28 @@ distinguished from a number. Use :c:func:`PyErr_Occurred` to disambiguate.
113113
retrieved from the resulting value using :c:func:`PyLong_AsVoidPtr`.
114114
115115
116+
.. c:function:: PyObject* PyLong_FromBits(const void* buffer, size_t n_bytes, int endianness)
117+
118+
Create a Python integer from the value contained in the first *n_bytes* of
119+
*buffer*, interpreted as twos-complement.
120+
121+
*endianness* may be passed ``-1`` for the native endian that CPython was
122+
compiled with, or ``0`` for big endian and ``1`` for little.
123+
124+
.. versionadded:: 3.13
125+
126+
127+
.. c:function:: PyObject* PyLong_FromUnsignedBits(const void* buffer, size_t n_bytes, int endianness)
128+
129+
Create a Python integer from the value contained in the first *n_bytes* of
130+
*buffer*, interpreted as an unsigned number.
131+
132+
*endianness* may be passed ``-1`` for the native endian that CPython was
133+
compiled with, or ``0`` for big endian and ``1`` for little.
134+
135+
.. versionadded:: 3.13
136+
137+
116138
.. XXX alias PyLong_AS_LONG (for now)
117139
.. c:function:: long PyLong_AsLong(PyObject *obj)
118140
@@ -332,6 +354,46 @@ distinguished from a number. Use :c:func:`PyErr_Occurred` to disambiguate.
332354
Returns ``NULL`` on error. Use :c:func:`PyErr_Occurred` to disambiguate.
333355
334356
357+
.. c:function:: int PyLong_CopyBits(PyObject *pylong, void* buffer, size_t n_bytes, int endianness)
358+
359+
Copy the Python integer value to a native *buffer* of size *n_bytes*::
360+
361+
int value;
362+
int bytes = PyLong_CopyBits(v, &value, sizeof(value), -1);
363+
if (bytes < 0) {
364+
// Error occurred
365+
return NULL;
366+
}
367+
else if (bytes > sizeof(value)) {
368+
// Overflow occurred, but 'value' contains as much as could fit
369+
}
370+
371+
*endianness* may be passed ``-1`` for the native endian that CPython was
372+
compiled with, or ``0`` for big endian and ``1`` for little.
373+
374+
Returns ``-1`` with an exception raised if *pylong* cannot be interpreted as
375+
an integer. Otherwise, returns the size of the buffer required to store the
376+
value. If this is equal to or less than *n_bytes*, the entire value was
377+
copied.
378+
379+
Unless an exception is raised, all *n_bytes* of the buffer will be written
380+
with as much of the value as can fit. This allows the caller to ignore all
381+
non-negative results, if the intent is to match the typical behavior of a
382+
C-style downcast.
383+
384+
Values are always copied as twos-complement, and sufficient size will be
385+
requested for a sign bit. For example, this may cause an value that fits into
386+
8-bytes when treated as unsigned to request 9 bytes, even though all eight
387+
bytes were copied into the buffer.
388+
389+
Passing *n_bytes* of zero will always return the requested buffer size.
390+
The requested buffer size may be larger than necessary, but only when the
391+
larger size is required to contain the full value. This function is not an
392+
accurate way to determine the bit length of a value.
393+
394+
.. versionadded:: 3.13
395+
396+
335397
.. c:function:: int PyUnstable_Long_IsCompact(const PyLongObject* op)
336398
337399
Return 1 if *op* is compact, 0 otherwise.

Doc/whatsnew/3.13.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1466,6 +1466,11 @@ New Features
14661466
* Add :c:func:`Py_HashPointer` function to hash a pointer.
14671467
(Contributed by Victor Stinner in :gh:`111545`.)
14681468

1469+
* Add :c:func:`PyLong_CopyBits`, :c:func:`PyLong_FromBits` and
1470+
:c:func:`PyLong_FromUnsignedBits` functions to simplify converting between
1471+
native integer types and Python ``int`` objects.
1472+
(Contributed by Steve Dower in :gh:`111140`.)
1473+
14691474

14701475
Porting to Python 3.13
14711476
----------------------
@@ -1525,7 +1530,6 @@ Porting to Python 3.13
15251530
platforms, the ``HAVE_STDDEF_H`` macro is only defined on Windows.
15261531
(Contributed by Victor Stinner in :gh:`108765`.)
15271532

1528-
15291533
Deprecated
15301534
----------
15311535

Include/cpython/longobject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ PyAPI_FUNC(int) PyLong_CopyBits(PyObject* v, void* buffer, size_t n_bytes,
2525
/* PyLong_FromBits: Create an int value from a native integer
2626
n_bytes is the number of bytes to read from the buffer. Passing 0 will
2727
always produce the zero int.
28-
PyLong_FromUnsignedBits always produces a positive int.
28+
PyLong_FromUnsignedBits always produces a non-negative int.
2929
endianness is -1 for native endian, 0 for big endian or 1 for little.
3030
3131
Returns the int object, or NULL with an exception set. */
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Adds :c:func:`PyLong_CopyBits`, :c:func:`PyLong_FromBits` and
2+
:c:func:`PyLong_FromUnsignedBits` functions.

0 commit comments

Comments
 (0)