Integer Objects¶
All integers are implemented as “long” integer objects of arbitrary size.
- 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.
- int
PyLong_CheckExact(PyObject *p)¶ Return true if its argument is a
PyLongObject, but not a subtype ofPyLongObject.
- PyObject*
PyLong_FromLong(long v)¶ - Return value: New reference.
Return a new
PyLongObjectobject fromv, orNULL on 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. 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 new
PyLongObjectobject from a Cunsignedlong, orNULL on failure.
- PyObject*
PyLong_FromSsize_t(Py_ssize_t v)¶ Return a new
PyLongObjectobject from a CPy_ssize_t, orNULL on failure.
- PyObject*
PyLong_FromSize_t(size_t v)¶ Return a new
PyLongObjectobject from a Csize_t, orNULL on failure.
- PyObject*
PyLong_FromLongLong(PY_LONG_LONG v)¶ - Return value: New reference.
Return a new
PyLongObjectobject from a Clonglong, orNULLon failure.
- PyObject*
PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG v)¶ - Return value: New reference.
Return a new
PyLongObjectobject from a Cunsignedlonglong,orNULL on failure.
- PyObject*
PyLong_FromDouble(double v)¶ - Return value: New reference.
Return a new
PyLongObjectobject from the integer part ofv, orNULL on 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. The Unicodestring is first encoded to a byte string using
PyUnicode_EncodeDecimal()and then converted usingPyLong_FromString().Deprecated since version 3.3, will be removed in version 4.0:Part of the old-style
Py_UNICODEAPI; 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 using
PyUnicode_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 using
PyLong_AsVoidPtr().
- long
PyLong_AsLong(PyObject *obj)¶ Return a C
longrepresentation ofobj. Ifobj is not aninstance ofPyLongObject, first call its__int__()method(if present) to convert it to aPyLongObject.Raise
OverflowErrorif the value ofobj is out of range for along.
- long
PyLong_AsLongAndOverflow(PyObject *obj, int *overflow)¶ Return a C
longrepresentation ofobj. Ifobj is not aninstance ofPyLongObject, first call its__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.
- PY_LONG_LONG
PyLong_AsLongLong(PyObject *obj)¶ Return a C
longlongrepresentation ofobj. Ifobj is not aninstance ofPyLongObject, first call its__int__()method(if present) to convert it to aPyLongObject.Raise
OverflowErrorif the value ofobj is out of range for along.
- PY_LONG_LONG
PyLong_AsLongLongAndOverflow(PyObject *obj, int *overflow)¶ Return a C
longlongrepresentation ofobj. Ifobj is not aninstance ofPyLongObject, first call its__int__()method(if present) to convert it to aPyLongObject.If the value ofobj is greater than
PY_LLONG_MAXor less thanPY_LLONG_MIN, set*overflow to1or-1, respectively,and return-1; otherwise, set*overflow to0. If any otherexception occurs set*overflow to0and return-1as usual.New in version 3.2.
- 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.
- 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.
- 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.
- unsigned PY_LONG_LONG
PyLong_AsUnsignedLongLong(PyObject *pylong)¶ Return a C
unsignedPY_LONG_LONGrepresentation ofpylong.pylong must be an instance ofPyLongObject.Raise
OverflowErrorif the value ofpylong is out of range for anunsignedPY_LONG_LONG.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__int__()method (if present) to convert it to aPyLongObject.If the value ofobj is out of range for an
unsignedlong,return the reduction of that value moduloULONG_MAX+1.
- unsigned PY_LONG_LONG
PyLong_AsUnsignedLongLongMask(PyObject *obj)¶ Return a C
unsignedlonglongrepresentation 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 an
unsignedlonglong,return the reduction of that value moduloPY_ULLONG_MAX+1.
- 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.
- 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().
