All integers are implemented as “long” integer objects of arbitrary size.
On error, most PyLong_As* APIs return (returntype)-1 which cannot be distinguished from a number. Use
to disambiguate.
PyLongObject
This subtype of
represents a Python integer object.
PyLong_Type
This instance of
represents the Python integer type. This is the same object as
in the Python layer.
int PyLong_Check(
*p)
Return true if its argument is a
or a subtype of
. This function always succeeds.
int PyLong_CheckExact(
*p)
Return true if its argument is a
, but not a subtype of
. This function always succeeds.
* PyLong_FromLong(long v)
Return value: New reference.Return a new
object from v, or NULL on failure.
The current implementation keeps an array of integer objects for all integers between -5 and 256. When you create an int in that range you actually just get back a reference to the existing object.
* PyLong_FromUnsignedLong(unsigned long v)
Return value: New reference.Return a new
object from a C unsignedlong, or NULL on failure.
* PyLong_FromSsize_t(
v)
Return value: New reference.Return a new
object from a C
, or NULL on failure.
* PyLong_FromSize_t(size_t v)
Return value: New reference.Return a new
object from a C size_t, or NULL on failure.
* PyLong_FromLongLong(long long v)
Return value: New reference.Return a new
object from a C longlong, or NULL on failure.
* PyLong_FromUnsignedLongLong(unsigned long long v)
Return value: New reference.Return a new
object from a C unsignedlonglong, or NULL on failure.
* PyLong_FromDouble(double v)
Return value: New reference.Return a new
object from the integer part of v, or NULL on failure.
* PyLong_FromString(const char *str, char **pend, int base)
Return value: New reference.Return a new
based on the string value in str, which is interpreted according to the radix in base. If pend is non-NULL, *pend will point to the first character in str which follows the representation of the number. If base is 0, str is interpreted using the
definition; in this case, leading zeros in a non-zero decimal number raises a
. If base is not 0, it must be between 2 and 36, inclusive. Leading spaces and single underscores after a base specifier and between digits are ignored. If there are no digits,
will be raised.
* PyLong_FromUnicode(
*u,
length, int base)
Return value: New reference.Convert a sequence of Unicode digits to a Python integer value.
Deprecated since version 3.3, will be removed in version 3.10: Part of the old-style
API; please migrate to using
.
* PyLong_FromUnicodeObject(
*u, int base)
Return value: New reference.Convert a sequence of Unicode digits in the string u to a Python integer value.
New in version 3.3.
* PyLong_FromVoidPtr(void *p)
Return value: New reference.Create a Python integer from the pointer p. The pointer value can be retrieved from the resulting value using
.
long PyLong_AsLong(
*obj)
Return a C long representation of obj. If obj is not an instance of
, first call its
or
method (if present) to convert it to a
.
Raise
if the value of obj is out of range for a long.
Returns -1 on error. Use
to disambiguate.
Changed in version 3.8: Use
if available.
Deprecated since version 3.8: Using
is deprecated.
long PyLong_AsLongAndOverflow(
*obj, int *overflow)
Return a C long representation of obj. If obj is not an instance of
, first call its
or
method (if present) to convert it to a
.
If the value of obj is greater than LONG_MAX or less than LONG_MIN, set *overflow to 1 or -1, respectively, and return -1; otherwise, set *overflow to 0. If any other exception occurs set *overflow to 0 and return -1 as usual.
Returns -1 on error. Use
to disambiguate.
Changed in version 3.8: Use
if available.
Deprecated since version 3.8: Using
is deprecated.
long long PyLong_AsLongLong(
*obj)
Return a C longlong representation of obj. If obj is not an instance of
, first call its
or
method (if present) to convert it to a
.
Raise
if the value of obj is out of range for a longlong.
Returns -1 on error. Use
to disambiguate.
Changed in version 3.8: Use
if available.
Deprecated since version 3.8: Using
is deprecated.
long long PyLong_AsLongLongAndOverflow(
*obj, int *overflow)
Return a C longlong representation of obj. If obj is not an instance of
, first call its
or
method (if present) to convert it to a
.
If the value of obj is greater than LLONG_MAX or less than LLONG_MIN, set *overflow to 1 or -1, respectively, and return -1; otherwise, set *overflow to 0. If any other exception occurs set *overflow to 0 and return -1 as usual.
Returns -1 on error. Use
to disambiguate.
New in version 3.2.
Changed in version 3.8: Use
if available.
Deprecated since version 3.8: Using
is deprecated.
PyLong_AsSsize_t(
*pylong)
Return a C
representation of pylong. pylong must be an instance of
.
Raise
if the value of pylong is out of range for a
.
Returns -1 on error. Use
to disambiguate.
unsigned long PyLong_AsUnsignedLong(
*pylong)
Return a C unsignedlong representation of pylong. pylong must be an instance of
.
Raise
if the value of pylong is out of range for a unsignedlong.
Returns (unsignedlong)-1 on error. Use
to disambiguate.
size_t PyLong_AsSize_t(
*pylong)
Return a C size_t representation of pylong. pylong must be an instance of
.
Raise
if the value of pylong is out of range for a size_t.
Returns (size_t)-1 on error. Use
to disambiguate.
unsigned long long PyLong_AsUnsignedLongLong(
*pylong)
Return a C unsignedlonglong representation of pylong. pylong must be an instance of
.
Raise
if the value of pylong is out of range for an unsignedlonglong.
Returns (unsignedlonglong)-1 on error. Use
to disambiguate.
unsigned long PyLong_AsUnsignedLongMask(
*obj)
Return a C unsignedlong representation of obj. If obj is not an instance of
, first call its
or
method (if present) to convert it to a
.
If the value of obj is out of range for an unsignedlong, return the reduction of that value modulo ULONG_MAX+1.
Returns (unsignedlong)-1 on error. Use
to disambiguate.
Changed in version 3.8: Use
if available.
Deprecated since version 3.8: Using
is deprecated.
unsigned long long PyLong_AsUnsignedLongLongMask(
*obj)
Return a C unsignedlonglong representation of obj. If obj is not an instance of
, first call its
or
method (if present) to convert it to a
.
If the value of obj is out of range for an unsignedlonglong, return the reduction of that value modulo ULLONG_MAX+1.
Returns (unsignedlonglong)-1 on error. Use
to disambiguate.
Changed in version 3.8: Use
if available.
Deprecated since version 3.8: Using
is deprecated.
double PyLong_AsDouble(
*pylong)
Return a C double representation of pylong. pylong must be an instance of
.
Raise
if the value of pylong is out of range for a double.
Returns -1.0 on error. Use
to disambiguate.
void* PyLong_AsVoidPtr(
*pylong)
Convert a Python integer pylong to a C void pointer. If pylong cannot be converted, an
will be raised. This is only assured to produce a usable void pointer for values created with
.
Returns NULL on error. Use
to disambiguate.