@@ -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.
0 commit comments