long.rst 7.66 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
.. highlightlang:: c

.. _longobjects:

Integer Objects
---------------

.. index:: object: long integer
           object: integer

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

13
.. c:type:: PyLongObject
14

15
   This subtype of :c:type:`PyObject` represents a Python integer object.
16 17


18
.. c:var:: PyTypeObject PyLong_Type
19

20
   This instance of :c:type:`PyTypeObject` represents the Python integer type.
21
   This is the same object as :class:`int` in the Python layer.
22 23


24
.. c:function:: int PyLong_Check(PyObject *p)
25

26 27
   Return true if its argument is a :c:type:`PyLongObject` or a subtype of
   :c:type:`PyLongObject`.
28 29


30
.. c:function:: int PyLong_CheckExact(PyObject *p)
31

32 33
   Return true if its argument is a :c:type:`PyLongObject`, but not a subtype of
   :c:type:`PyLongObject`.
34 35


36
.. c:function:: PyObject* PyLong_FromLong(long v)
37

38
   Return a new :c:type:`PyLongObject` object from *v*, or *NULL* on failure.
39 40 41 42 43 44 45 46

   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. So it should be possible to
   change the value of ``1``.  I suspect the behaviour of Python in this case is
   undefined. :-)


47
.. c:function:: PyObject* PyLong_FromUnsignedLong(unsigned long v)
48

49
   Return a new :c:type:`PyLongObject` object from a C :c:type:`unsigned long`, or
50 51 52
   *NULL* on failure.


53
.. c:function:: PyObject* PyLong_FromSsize_t(Py_ssize_t v)
54

55
   Return a new :c:type:`PyLongObject` object from a C :c:type:`Py_ssize_t`, or
Christian Heimes's avatar
Christian Heimes committed
56
   *NULL* on failure.
57 58


59
.. c:function:: PyObject* PyLong_FromSize_t(size_t v)
60

61
   Return a new :c:type:`PyLongObject` object from a C :c:type:`size_t`, or
Christian Heimes's avatar
Christian Heimes committed
62
   *NULL* on failure.
63 64


65
.. c:function:: PyObject* PyLong_FromLongLong(PY_LONG_LONG v)
66

67
   Return a new :c:type:`PyLongObject` object from a C :c:type:`long long`, or *NULL*
68 69 70
   on failure.


71
.. c:function:: PyObject* PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG v)
72

73
   Return a new :c:type:`PyLongObject` object from a C :c:type:`unsigned long long`,
74 75 76
   or *NULL* on failure.


77
.. c:function:: PyObject* PyLong_FromDouble(double v)
78

79
   Return a new :c:type:`PyLongObject` object from the integer part of *v*, or
80 81 82
   *NULL* on failure.


83
.. c:function:: PyObject* PyLong_FromString(char *str, char **pend, int base)
84

85
   Return a new :c:type:`PyLongObject` based on the string value in *str*, which
86
   is interpreted according to the radix in *base*.  If *pend* is non-*NULL*,
87
   *\*pend* will point to the first character in *str* which follows the
88 89 90 91 92 93 94 95 96
   representation of the number.  If *base* is ``0``, the radix will be
   determined based on the leading characters of *str*: if *str* starts with
   ``'0x'`` or ``'0X'``, radix 16 will be used; if *str* starts with ``'0o'`` or
   ``'0O'``, radix 8 will be used; if *str* starts with ``'0b'`` or ``'0B'``,
   radix 2 will be used; otherwise radix 10 will be used.  If *base* is not
   ``0``, it must be between ``2`` and ``36``, inclusive.  Leading spaces are
   ignored.  If there are no digits, :exc:`ValueError` will be raised.


97
.. c:function:: PyObject* PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base)
98 99

   Convert a sequence of Unicode digits to a Python integer value.  The Unicode
100 101
   string is first encoded to a byte string using :c:func:`PyUnicode_EncodeDecimal`
   and then converted using :c:func:`PyLong_FromString`.
102 103


104
.. c:function:: PyObject* PyLong_FromVoidPtr(void *p)
105 106

   Create a Python integer from the pointer *p*. The pointer value can be
107
   retrieved from the resulting value using :c:func:`PyLong_AsVoidPtr`.
108 109


110
.. XXX alias PyLong_AS_LONG (for now)
111
.. c:function:: long PyLong_AsLong(PyObject *pylong)
112 113 114 115 116

   .. index::
      single: LONG_MAX
      single: OverflowError (built-in exception)

117
   Return a C :c:type:`long` representation of the contents of *pylong*.  If
118 119 120 121
   *pylong* is greater than :const:`LONG_MAX`, raise an :exc:`OverflowError`,
   and return -1. Convert non-long objects automatically to long first,
   and return -1 if that raises exceptions.

122
.. c:function:: long PyLong_AsLongAndOverflow(PyObject *pylong, int *overflow)
123

124
   Return a C :c:type:`long` representation of the contents of
125
   *pylong*.  If *pylong* is greater than :const:`LONG_MAX` or less
126 127
   than :const:`LONG_MIN`, set *\*overflow* to ``1`` or ``-1``,
   respectively, and return ``-1``; otherwise, set *\*overflow* to
128
   ``0``.  If any other exception occurs (for example a TypeError or
129
   MemoryError), then ``-1`` will be returned and *\*overflow* will
130
   be ``0``.
131 132


133
.. c:function:: PY_LONG_LONG PyLong_AsLongLongAndOverflow(PyObject *pylong, int *overflow)
134

135
   Return a C :c:type:`long long` representation of the contents of
136
   *pylong*.  If *pylong* is greater than :const:`PY_LLONG_MAX` or less
137 138
   than :const:`PY_LLONG_MIN`, set *\*overflow* to ``1`` or ``-1``,
   respectively, and return ``-1``; otherwise, set *\*overflow* to
139
   ``0``.  If any other exception occurs (for example a TypeError or
140
   MemoryError), then ``-1`` will be returned and *\*overflow* will
141 142 143 144 145
   be ``0``.

   .. versionadded:: 3.2


146
.. c:function:: Py_ssize_t PyLong_AsSsize_t(PyObject *pylong)
Christian Heimes's avatar
Christian Heimes committed
147 148 149 150 151

   .. index::
      single: PY_SSIZE_T_MAX
      single: OverflowError (built-in exception)

152
   Return a C :c:type:`Py_ssize_t` representation of the contents of *pylong*.
153 154
   If *pylong* is greater than :const:`PY_SSIZE_T_MAX`, an :exc:`OverflowError`
   is raised and ``-1`` will be returned.
Christian Heimes's avatar
Christian Heimes committed
155 156


157
.. c:function:: unsigned long PyLong_AsUnsignedLong(PyObject *pylong)
158 159 160 161 162

   .. index::
      single: ULONG_MAX
      single: OverflowError (built-in exception)

163
   Return a C :c:type:`unsigned long` representation of the contents of *pylong*.
164 165 166 167
   If *pylong* is greater than :const:`ULONG_MAX`, an :exc:`OverflowError` is
   raised.


168
.. c:function:: size_t PyLong_AsSize_t(PyObject *pylong)
169

170 171
   Return a :c:type:`size_t` representation of the contents of *pylong*.  If
   *pylong* is greater than the maximum value for a :c:type:`size_t`, an
172 173 174
   :exc:`OverflowError` is raised.


175
.. c:function:: PY_LONG_LONG PyLong_AsLongLong(PyObject *pylong)
176

177 178
   .. index::
      single: OverflowError (built-in exception)
179

180 181
   Return a C :c:type:`long long` from a Python integer.  If *pylong*
   cannot be represented as a :c:type:`long long`, an
182
   :exc:`OverflowError` is raised and ``-1`` is returned.
183

184

185
.. c:function:: unsigned PY_LONG_LONG PyLong_AsUnsignedLongLong(PyObject *pylong)
186

187 188 189
   .. index::
      single: OverflowError (built-in exception)

190 191
   Return a C :c:type:`unsigned long long` from a Python integer. If
   *pylong* cannot be represented as an :c:type:`unsigned long long`,
192 193
   an :exc:`OverflowError` is raised and ``(unsigned long long)-1`` is
   returned.
194

195
   .. versionchanged:: 3.1
196 197
      A negative *pylong* now raises :exc:`OverflowError`, not :exc:`TypeError`.

198

199
.. c:function:: unsigned long PyLong_AsUnsignedLongMask(PyObject *io)
200

201
   Return a C :c:type:`unsigned long` from a Python integer, without checking for
202 203 204
   overflow.


205
.. c:function:: unsigned PY_LONG_LONG PyLong_AsUnsignedLongLongMask(PyObject *io)
206

207
   Return a C :c:type:`unsigned long long` from a Python integer, without
208 209 210
   checking for overflow.


211
.. c:function:: double PyLong_AsDouble(PyObject *pylong)
212

213 214
   Return a C :c:type:`double` representation of the contents of *pylong*.  If
   *pylong* cannot be approximately represented as a :c:type:`double`, an
215 216 217
   :exc:`OverflowError` exception is raised and ``-1.0`` will be returned.


218
.. c:function:: void* PyLong_AsVoidPtr(PyObject *pylong)
219

220
   Convert a Python integer *pylong* to a C :c:type:`void` pointer.
Christian Heimes's avatar
Christian Heimes committed
221
   If *pylong* cannot be converted, an :exc:`OverflowError` will be raised.  This
222 223
   is only assured to produce a usable :c:type:`void` pointer for values created
   with :c:func:`PyLong_FromVoidPtr`.