Integer Objects

All integers are implemented as “long” integer objects of arbitrary size.

PyLongObject

This subtype ofPyObject represents a Python integer object.

PyTypeObjectPyLong_Type

This instance ofPyTypeObject represents the Python integer type.This is the same object asint in the Python layer.

intPyLong_Check(PyObject *p)

Return true if its argument is aPyLongObject or a subtype ofPyLongObject.

intPyLong_CheckExact(PyObject *p)

Return true if its argument is aPyLongObject, but not a subtype ofPyLongObject.

PyObject*PyLong_FromLong(long v)
Return value: New reference.

Return a newPyLongObject object fromv, orNULL on failure.

The current implementation keeps an array of integer objects for all integersbetween-5 and256, when you create an int in that range you actuallyjust get back a reference to the existing object. So it should be possible tochange the value of1. I suspect the behaviour of Python in this case isundefined. :-)

PyObject*PyLong_FromUnsignedLong(unsigned long v)
Return value: New reference.

Return a newPyLongObject object from a Cunsignedlong, orNULL on failure.

PyObject*PyLong_FromSsize_t(Py_ssize_t v)

Return a newPyLongObject object from a CPy_ssize_t, orNULL on failure.

PyObject*PyLong_FromSize_t(size_t v)

Return a newPyLongObject object from a Csize_t, orNULL on failure.

PyObject*PyLong_FromLongLong(PY_LONG_LONG v)
Return value: New reference.

Return a newPyLongObject object from a Clonglong, orNULLon failure.

PyObject*PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG v)
Return value: New reference.

Return a newPyLongObject object from a Cunsignedlonglong,orNULL on failure.

PyObject*PyLong_FromDouble(double v)
Return value: New reference.

Return a newPyLongObject object from the integer part ofv, orNULL on failure.

PyObject*PyLong_FromString(const char *str, char **pend, int base)
Return value: New reference.

Return a newPyLongObject based 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 between2 and36, inclusive. Leading spaces and singleunderscores after a base specifier and between digits are ignored. If thereare no digits,ValueError will 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. The Unicodestring is first encoded to a byte string usingPyUnicode_EncodeDecimal()and then converted usingPyLong_FromString().

Deprecated since version 3.3, will be removed in version 4.0:Part of the old-stylePy_UNICODE API; please migrate to usingPyLong_FromUnicodeObject().

PyObject*PyLong_FromUnicodeObject(PyObject *u, int base)

Convert a sequence of Unicode digits in the stringu to a Python integervalue. The Unicode string is first encoded to a byte string usingPyUnicode_EncodeDecimal() and then converted usingPyLong_FromString().

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 usingPyLong_AsVoidPtr().

longPyLong_AsLong(PyObject *obj)

Return a Clong representation ofobj. Ifobj is not aninstance ofPyLongObject, first call its__int__() method(if present) to convert it to aPyLongObject.

RaiseOverflowError if the value ofobj is out of range for along.

longPyLong_AsLongAndOverflow(PyObject *obj, int *overflow)

Return a Clong representation ofobj. Ifobj is not aninstance ofPyLongObject, first call its__int__() method(if present) to convert it to aPyLongObject.

If the value ofobj is greater thanLONG_MAX or less thanLONG_MIN, set*overflow to1 or-1, respectively, andreturn-1; otherwise, set*overflow to0. If any other exceptionoccurs set*overflow to0 and return-1 as usual.

PY_LONG_LONGPyLong_AsLongLong(PyObject *obj)

Return a Clonglong representation ofobj. Ifobj is not aninstance ofPyLongObject, first call its__int__() method(if present) to convert it to aPyLongObject.

RaiseOverflowError if the value ofobj is out of range for along.

PY_LONG_LONGPyLong_AsLongLongAndOverflow(PyObject *obj, int *overflow)

Return a Clonglong representation ofobj. Ifobj is not aninstance ofPyLongObject, first call its__int__() method(if present) to convert it to aPyLongObject.

If the value ofobj is greater thanPY_LLONG_MAX or less thanPY_LLONG_MIN, set*overflow to1 or-1, respectively,and return-1; otherwise, set*overflow to0. If any otherexception occurs set*overflow to0 and return-1 as usual.

New in version 3.2.

Py_ssize_tPyLong_AsSsize_t(PyObject *pylong)

Return a CPy_ssize_t representation ofpylong.pylong mustbe an instance ofPyLongObject.

RaiseOverflowError if the value ofpylong is out of range for aPy_ssize_t.

unsigned longPyLong_AsUnsignedLong(PyObject *pylong)

Return a Cunsignedlong representation ofpylong.pylongmust be an instance ofPyLongObject.

RaiseOverflowError if the value ofpylong is out of range for aunsignedlong.

size_tPyLong_AsSize_t(PyObject *pylong)

Return a Csize_t representation ofpylong.pylong must bean instance ofPyLongObject.

RaiseOverflowError if the value ofpylong is out of range for asize_t.

unsigned PY_LONG_LONGPyLong_AsUnsignedLongLong(PyObject *pylong)

Return a CunsignedPY_LONG_LONG representation ofpylong.pylong must be an instance ofPyLongObject.

RaiseOverflowError if the value ofpylong is out of range for anunsignedPY_LONG_LONG.

Changed in version 3.1:A negativepylong now raisesOverflowError, notTypeError.

unsigned longPyLong_AsUnsignedLongMask(PyObject *obj)

Return a Cunsignedlong representation ofobj. Ifobjis not an instance ofPyLongObject, first call its__int__()method (if present) to convert it to aPyLongObject.

If the value ofobj is out of range for anunsignedlong,return the reduction of that value moduloULONG_MAX+1.

unsigned PY_LONG_LONGPyLong_AsUnsignedLongLongMask(PyObject *obj)

Return a Cunsignedlonglong representation ofobj. Ifobjis not an instance ofPyLongObject, first call its__int__()method (if present) to convert it to aPyLongObject.

If the value ofobj is out of range for anunsignedlonglong,return the reduction of that value moduloPY_ULLONG_MAX+1.

doublePyLong_AsDouble(PyObject *pylong)

Return a Cdouble representation ofpylong.pylong must bean instance ofPyLongObject.

RaiseOverflowError if the value ofpylong is out of range for adouble.

void*PyLong_AsVoidPtr(PyObject *pylong)

Convert a Python integerpylong to a Cvoid pointer.Ifpylong cannot be converted, anOverflowError will be raised. Thisis only assured to produce a usablevoid pointer for values createdwithPyLong_FromVoidPtr().