整數物件

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

On error, most PyLong_As* APIs return (return type)-1 which cannot be distinguished from a number. Use PyErr_Occurred() to disambiguate.

type PyLongObject
受限 API 的一部分 (做為一個不透明結構 (opaque struct)).

This subtype of PyObject represents a Python integer object.

PyTypeObject PyLong_Type
穩定 ABI 的一部分.

This instance of PyTypeObject represents the Python integer type. This is the same object as int in the Python layer.

int PyLong_Check(PyObject *p)

Return true if its argument is a PyLongObject or a subtype of PyLongObject. This function always succeeds.

int PyLong_CheckExact(PyObject *p)

Return true if its argument is a PyLongObject, but not a subtype of PyLongObject. This function always succeeds.

PyObject *PyLong_FromLong(long v)
回傳值:新的參照。穩定 ABI 的一部分.

Return a new PyLongObject object from v, or NULL on failure.

CPython 實作細節: CPython 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.

PyObject *PyLong_FromUnsignedLong(unsigned long v)
回傳值:新的參照。穩定 ABI 的一部分.

Return a new PyLongObject object from a C unsigned long, or NULL on failure.

PyObject *PyLong_FromSsize_t(Py_ssize_t v)
回傳值:新的參照。穩定 ABI 的一部分.

Return a new PyLongObject object from a C Py_ssize_t, or NULL on failure.

PyObject *PyLong_FromSize_t(size_t v)
回傳值:新的參照。穩定 ABI 的一部分.

Return a new PyLongObject object from a C size_t, or NULL on failure.

PyObject *PyLong_FromLongLong(long long v)
回傳值:新的參照。穩定 ABI 的一部分.

Return a new PyLongObject object from a C long long, or NULL on failure.

PyObject *PyLong_FromInt32(int32_t value)
PyObject *PyLong_FromInt64(int64_t value)
穩定 ABI 的一部分 自 3.14 版本開始.

Return a new PyLongObject object from a signed C int32_t or int64_t, or NULL with an exception set on failure.

在 3.14 版被加入.

PyObject *PyLong_FromUnsignedLongLong(unsigned long long v)
回傳值:新的參照。穩定 ABI 的一部分.

Return a new PyLongObject object from a C unsigned long long, or NULL on failure.

PyObject *PyLong_FromUInt32(uint32_t value)
PyObject *PyLong_FromUInt64(uint64_t value)
穩定 ABI 的一部分 自 3.14 版本開始.

Return a new PyLongObject object from an unsigned C uint32_t or uint64_t, or NULL with an exception set on failure.

在 3.14 版被加入.

PyObject *PyLong_FromDouble(double v)
回傳值:新的參照。穩定 ABI 的一部分.

Return a new PyLongObject object from the integer part of v, or NULL on failure.

PyObject *PyLong_FromString(const char *str, char **pend, int base)
回傳值:新的參照。穩定 ABI 的一部分.

Return a new PyLongObject based on the string value in str, which is interpreted according to the radix in base, or NULL on failure. If pend is non-NULL, *pend will point to the end of str on success or to the first character that could not be processed on error. If base is 0, str is interpreted using the Integer literals definition; in this case, leading zeros in a non-zero decimal number raises a ValueError. If base is not 0, it must be between 2 and 36, inclusive. Leading and trailing whitespace and single underscores after a base specifier and between digits are ignored. If there are no digits or str is not NULL-terminated following the digits and trailing whitespace, ValueError will be raised.

也參考

PyLong_AsNativeBytes() and PyLong_FromNativeBytes() functions can be used to convert a PyLongObject to/from an array of bytes in base 256.

PyObject *PyLong_FromUnicodeObject(PyObject *u, int base)
回傳值:新的參照。

Convert a sequence of Unicode digits in the string u to a Python integer value.

在 3.3 版被加入.

PyObject *PyLong_FromVoidPtr(void *p)
回傳值:新的參照。穩定 ABI 的一部分.

Create a Python integer from the pointer p. The pointer value can be retrieved from the resulting value using PyLong_AsVoidPtr().

PyObject *PyLong_FromNativeBytes(const void *buffer, size_t n_bytes, int flags)
穩定 ABI 的一部分 自 3.14 版本開始.

Create a Python integer from the value contained in the first n_bytes of buffer, interpreted as a two's-complement signed number.

flags are as for PyLong_AsNativeBytes(). Passing -1 will select the native endian that CPython was compiled with and assume that the most-significant bit is a sign bit. Passing Py_ASNATIVEBYTES_UNSIGNED_BUFFER will produce the same result as calling PyLong_FromUnsignedNativeBytes(). Other flags are ignored.

在 3.13 版被加入.

PyObject *PyLong_FromUnsignedNativeBytes(const void *buffer, size_t n_bytes, int