Integer Objects¶
All integers are implemented as “long” integer objects of arbitrary size.
On error, mostPyLong_As* APIs return(returntype)-1 which cannot bedistinguished from a number. UsePyErr_Occurred() to disambiguate.
- PyTypeObject
PyLong_Type¶ This instance of
PyTypeObjectrepresents the Python integer type.This is the same object asintin the Python layer.
- int
PyLong_Check(PyObject *p)¶ Return true if its argument is a
PyLongObjector a subtype ofPyLongObject. This function always succeeds.
- int
PyLong_CheckExact(PyObject *p)¶ Return true if its argument is a
PyLongObject, but not a subtype ofPyLongObject. This function always succeeds.
- PyObject*
PyLong_FromLong(long v)¶ - Return value: New reference.
Return a new
PyLongObjectobject fromv, orNULLon failure.The current implementation keeps an array of integer objects for all integersbetween
-5and256. When you create an int in that range you actuallyjust get back a reference to the existing object.
- PyObject*
PyLong_FromUnsignedLong(unsigned long v)¶ - Return value: New reference.
Return a new
PyLongObjectobject from a Cunsignedlong, orNULLon failure.
- PyObject*
PyLong_FromSsize_t(Py_ssize_t v)¶ - Return value: New reference.
Return a new
PyLongObjectobject from a CPy_ssize_t, orNULLon failure.
- PyObject*
PyLong_FromSize_t(size_t v)¶ - Return value: New reference.
Return a new
PyLongObjectobject from a Csize_t, orNULLon failure.
- PyObject*
PyLong_FromLongLong(long long v)¶ - Return value: New reference.
Return a new
PyLongObjectobject from a Clonglong, orNULLon failure.
- PyObject*
PyLong_FromUnsignedLongLong(unsigned long long v)¶ - Return value: New reference.
Return a new
PyLongObjectobject from a Cunsignedlonglong,orNULLon failure.
- PyObject*
PyLong_FromDouble(double v)¶ - Return value: New reference.
Return a new
PyLongObjectobject from the integer part ofv, orNULLon failure.
- PyObject*
PyLong_FromString(const char *str, char **pend, int base)¶ - Return value: New reference.
Return a new
PyLongObjectbased on the string value instr, whichis interpreted according to the radix inbase. Ifpend is non-NULL,*pend will point to the first character instr which follows therepresentation of the number. Ifbase is0,str is interpreted usingtheInteger literals definition; in this case, leading zeros in anon-zero decimal number raises aValueError. Ifbase is not0,it must be between2and36, inclusive. Leading spaces and singleunderscores after a base specifier and between digits are ignored. If thereare no digits,ValueErrorwill be raised.
- PyObject*
PyLong_FromUnicode(Py_UNICODE *u,Py_ssize_t 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
Py_UNICODEAPI; please migrate to usingPyLong_FromUnicodeObject().
- PyObject*
PyLong_FromUnicodeObject(PyObject *u, int base)¶ - Return value: New reference.
Convert a sequence of Unicode digits in the stringu to a Python integervalue.
New in version 3.3.
- PyObject*
PyLong_FromVoidPtr(void *p)¶ - Return value: New reference.
Create a Python integer from the pointerp. The pointer value can beretrieved from the resulting value using
PyLong_AsVoidPtr().
- long
PyLong_AsLong(PyObject *obj)¶ Return a C
longrepresentation ofobj. Ifobj is not aninstance ofPyLongObject, first call its__index__()or__int__()method (if present) to convert it to aPyLongObject.Raise
OverflowErrorif the value ofobj is out of range for along.Returns
-1on error. UsePyErr_Occurred()to disambiguate.Changed in version 3.8:Use
__index__()if available.Deprecated since version 3.8:Using
__int__()is deprecated.
- long
PyLong_AsLongAndOverflow(PyObject *obj, int *overflow)¶ Return a C
longrepresentation ofobj. Ifobj is not aninstance ofPyLongObject, first call its__index__()or__int__()method (if present) to convert it to aPyLongObject.If the value ofobj is greater than
LONG_MAXor less thanLONG_MIN, set*overflow to1or-1, respectively, andreturn-1; otherwise, set*overflow to0. If any other exceptionoccurs set*overflow to0and return-1as usual.Returns
-1on error. UsePyErr_Occurred()to disambiguate.Changed in version 3.8:Use
__index__()if available.Deprecated since version 3.8:Using
__int__()is deprecated.
- long long
PyLong_AsLongLong(PyObject *obj)¶ Return a C
longlongrepresentation ofobj. Ifobj is not aninstance ofPyLongObject, first call its__index__()or__int__()method (if present) to convert it to aPyLongObject.Raise
OverflowErrorif the value ofobj is out of range for alonglong.Returns
-1on error. UsePyErr_Occurred()to disambiguate.Changed in version 3.8:Use
__index__()if available.Deprecated since version 3.8:Using
__int__()is deprecated.
- long long
PyLong_AsLongLongAndOverflow(PyObject *obj, int *overflow)¶ Return a C
longlongrepresentation ofobj. Ifobj is not aninstance ofPyLongObject, first call its__index__()or__int__()method (if present) to convert it to aPyLongObject.If the value ofobj is greater than
LLONG_MAXor less thanLLONG_MIN, set*overflow to1or-1, respectively,and return-1; otherwise, set*overflow to0. If any otherexception occurs set*overflow to0and return-1as usual.Returns
-1on error. UsePyErr_Occurred()to disambiguate.New in version 3.2.
Changed in version 3.8:Use
__index__()if available.Deprecated since version 3.8:Using
__int__()is deprecated.
- Py_ssize_t
PyLong_AsSsize_t(PyObject *pylong)¶ Return a C
Py_ssize_trepresentation ofpylong.pylong mustbe an instance ofPyLongObject.Raise
OverflowErrorif the value ofpylong is out of range for aPy_ssize_t.Returns
-1on error. UsePyErr_Occurred()to disambiguate.
- unsigned long
PyLong_AsUnsignedLong(PyObject *pylong)¶ Return a C
unsignedlongrepresentation ofpylong.pylongmust be an instance ofPyLongObject.Raise
OverflowErrorif the value ofpylong is out of range for aunsignedlong.Returns
(unsignedlong)-1on error.UsePyErr_Occurred()to disambiguate.
- size_t
PyLong_AsSize_t(PyObject *pylong)¶ Return a C
size_trepresentation ofpylong.pylong must bean instance ofPyLongObject.Raise
OverflowErrorif the value ofpylong is out of range for asize_t.Returns
(size_t)-1on error.UsePyErr_Occurred()to disambiguate.
- unsigned long long
PyLong_AsUnsignedLongLong(PyObject *pylong)¶ Return a C
unsignedlonglongrepresentation ofpylong.pylongmust be an instance ofPyLongObject.Raise
OverflowErrorif the value ofpylong is out of range for anunsignedlonglong.Returns
(unsignedlonglong)-1on error.UsePyErr_Occurred()to disambiguate.Changed in version 3.1:A negativepylong now raises
OverflowError, notTypeError.
- unsigned long
PyLong_AsUnsignedLongMask(PyObject *obj)¶ Return a C
unsignedlongrepresentation ofobj. Ifobjis not an instance ofPyLongObject, first call its__index__()or__int__()method (if present) to convertit to aPyLongObject.If the value ofobj is out of range for an
unsignedlong,return the reduction of that value moduloULONG_MAX+1.Returns
(unsignedlong)-1on error. UsePyErr_Occurred()todisambiguate.Changed in version 3.8:Use
__index__()if available.Deprecated since version 3.8:Using
__int__()is deprecated.
- unsigned long long
PyLong_AsUnsignedLongLongMask(PyObject *obj)¶ Return a C
unsignedlonglongrepresentation ofobj. Ifobjis not an instance ofPyLongObject, first call its__index__()or__int__()method (if present) to convertit to aPyLongObject.If the value ofobj is out of range for an
unsignedlonglong,return the reduction of that value moduloULLONG_MAX+1.Returns
(unsignedlonglong)-1on error. UsePyErr_Occurred()to disambiguate.Changed in version 3.8:Use
__index__()if available.Deprecated since version 3.8:Using
__int__()is deprecated.
- double
PyLong_AsDouble(PyObject *pylong)¶ Return a C
doublerepresentation ofpylong.pylong must bean instance ofPyLongObject.Raise
OverflowErrorif the value ofpylong is out of range for adouble.Returns
-1.0on error. UsePyErr_Occurred()to disambiguate.
- void*
PyLong_AsVoidPtr(PyObject *pylong)¶ Convert a Python integerpylong to a C
voidpointer.Ifpylong cannot be converted, anOverflowErrorwill be raised. Thisis only assured to produce a usablevoidpointer for values createdwithPyLong_FromVoidPtr().Returns
NULLon error. UsePyErr_Occurred()to disambiguate.