blob: 254219cc4efd0008c3465169469f350ae88e3a11 [file] [log] [blame]
Georg Brandlf6842722008-01-19 22:08:21 +00001.. highlightlang:: c
2
3.. _intobjects:
4
5Plain Integer Objects
6---------------------
7
8.. index:: object: integer
9
10
Sandro Tosi98ed08f2012-01-14 16:42:02 +010011.. c:type:: PyIntObject
Georg Brandlf6842722008-01-19 22:08:21 +000012
Sandro Tosi98ed08f2012-01-14 16:42:02 +010013 This subtype of :c:type:`PyObject` represents a Python integer object.
Georg Brandlf6842722008-01-19 22:08:21 +000014
15
Sandro Tosi98ed08f2012-01-14 16:42:02 +010016.. c:var:: PyTypeObject PyInt_Type
Georg Brandlf6842722008-01-19 22:08:21 +000017
18 .. index:: single: IntType (in modules types)
19
Sandro Tosi98ed08f2012-01-14 16:42:02 +010020 This instance of :c:type:`PyTypeObject` represents the Python plain integer type.
Georg Brandlf6842722008-01-19 22:08:21 +000021 This is the same object as ``int`` and ``types.IntType``.
22
23
Sandro Tosi98ed08f2012-01-14 16:42:02 +010024.. c:function:: int PyInt_Check(PyObject *o)
Georg Brandlf6842722008-01-19 22:08:21 +000025
Sandro Tosi98ed08f2012-01-14 16:42:02 +010026 Return true if *o* is of type :c:data:`PyInt_Type` or a subtype of
27 :c:data:`PyInt_Type`.
Georg Brandlf6842722008-01-19 22:08:21 +000028
29 .. versionchanged:: 2.2
30 Allowed subtypes to be accepted.
31
32
Sandro Tosi98ed08f2012-01-14 16:42:02 +010033.. c:function:: int PyInt_CheckExact(PyObject *o)
Georg Brandlf6842722008-01-19 22:08:21 +000034
Sandro Tosi98ed08f2012-01-14 16:42:02 +010035 Return true if *o* is of type :c:data:`PyInt_Type`, but not a subtype of
36 :c:data:`PyInt_Type`.
Georg Brandlf6842722008-01-19 22:08:21 +000037
38 .. versionadded:: 2.2
39
40
Sandro Tosi98ed08f2012-01-14 16:42:02 +010041.. c:function:: PyObject* PyInt_FromString(char *str, char **pend, int base)
Georg Brandlf6842722008-01-19 22:08:21 +000042
Sandro Tosi98ed08f2012-01-14 16:42:02 +010043 Return a new :c:type:`PyIntObject` or :c:type:`PyLongObject` based on the string
Georg Brandlf6842722008-01-19 22:08:21 +000044 value in *str*, which is interpreted according to the radix in *base*. If
45 *pend* is non-*NULL*, ``*pend`` will point to the first character in *str* which
46 follows the representation of the number. If *base* is ``0``, the radix will be
47 determined based on the leading characters of *str*: if *str* starts with
48 ``'0x'`` or ``'0X'``, radix 16 will be used; if *str* starts with ``'0'``, radix
49 8 will be used; otherwise radix 10 will be used. If *base* is not ``0``, it
50 must be between ``2`` and ``36``, inclusive. Leading spaces are ignored. If
51 there are no digits, :exc:`ValueError` will be raised. If the string represents
Sandro Tosi98ed08f2012-01-14 16:42:02 +010052 a number too large to be contained within the machine's :c:type:`long int` type
53 and overflow warnings are being suppressed, a :c:type:`PyLongObject` will be
Georg Brandlf6842722008-01-19 22:08:21 +000054 returned. If overflow warnings are not being suppressed, *NULL* will be
55 returned in this case.
56
57
Sandro Tosi98ed08f2012-01-14 16:42:02 +010058.. c:function:: PyObject* PyInt_FromLong(long ival)
Georg Brandlf6842722008-01-19 22:08:21 +000059
60 Create a new integer object with a value of *ival*.
61
62 The current implementation keeps an array of integer objects for all integers
63 between ``-5`` and ``256``, when you create an int in that range you actually
64 just get back a reference to the existing object. So it should be possible to
65 change the value of ``1``. I suspect the behaviour of Python in this case is
66 undefined. :-)
67
68
Sandro Tosi98ed08f2012-01-14 16:42:02 +010069.. c:function:: PyObject* PyInt_FromSsize_t(Py_ssize_t ival)
Georg Brandlf6842722008-01-19 22:08:21 +000070
Georg Brandl476849c2009-06-17 09:43:31 +000071 Create a new integer object with a value of *ival*. If the value is larger
72 than ``LONG_MAX`` or smaller than ``LONG_MIN``, a long integer object is
73 returned.
74
75 .. versionadded:: 2.5
76
77
Sandro Tosi98ed08f2012-01-14 16:42:02 +010078.. c:function:: PyObject* PyInt_FromSize_t(size_t ival)
Georg Brandl476849c2009-06-17 09:43:31 +000079
Georg Brandlf6842722008-01-19 22:08:21 +000080 Create a new integer object with a value of *ival*. If the value exceeds
81 ``LONG_MAX``, a long integer object is returned.
82
83 .. versionadded:: 2.5
84
85
Sandro Tosi98ed08f2012-01-14 16:42:02 +010086.. c:function:: long PyInt_AsLong(PyObject *io)
Georg Brandlf6842722008-01-19 22:08:21 +000087
Sandro Tosi98ed08f2012-01-14 16:42:02 +010088 Will first attempt to cast the object to a :c:type:`PyIntObject`, if it is not
Georg Brandlf6842722008-01-19 22:08:21 +000089 already one, and then return its value. If there is an error, ``-1`` is
90 returned, and the caller should check ``PyErr_Occurred()`` to find out whether
91 there was an error, or whether the value just happened to be -1.
92
93
Sandro Tosi98ed08f2012-01-14 16:42:02 +010094.. c:function:: long PyInt_AS_LONG(PyObject *io)
Georg Brandlf6842722008-01-19 22:08:21 +000095
96 Return the value of the object *io*. No error checking is performed.
97
98
Sandro Tosi98ed08f2012-01-14 16:42:02 +010099.. c:function:: unsigned long PyInt_AsUnsignedLongMask(PyObject *io)
Georg Brandlf6842722008-01-19 22:08:21 +0000100
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100101 Will first attempt to cast the object to a :c:type:`PyIntObject` or
102 :c:type:`PyLongObject`, if it is not already one, and then return its value as
Georg Brandlf6842722008-01-19 22:08:21 +0000103 unsigned long. This function does not check for overflow.
104
105 .. versionadded:: 2.3
106
107
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100108.. c:function:: unsigned PY_LONG_LONG PyInt_AsUnsignedLongLongMask(PyObject *io)
Georg Brandlf6842722008-01-19 22:08:21 +0000109
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100110 Will first attempt to cast the object to a :c:type:`PyIntObject` or
111 :c:type:`PyLongObject`, if it is not already one, and then return its value as
Georg Brandlf6842722008-01-19 22:08:21 +0000112 unsigned long long, without checking for overflow.
113
114 .. versionadded:: 2.3
115
116
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100117.. c:function:: Py_ssize_t PyInt_AsSsize_t(PyObject *io)
Georg Brandlf6842722008-01-19 22:08:21 +0000118
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100119 Will first attempt to cast the object to a :c:type:`PyIntObject` or
120 :c:type:`PyLongObject`, if it is not already one, and then return its value as
121 :c:type:`Py_ssize_t`.
Georg Brandlf6842722008-01-19 22:08:21 +0000122
123 .. versionadded:: 2.5
124
125
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100126.. c:function:: long PyInt_GetMax()
Georg Brandlf6842722008-01-19 22:08:21 +0000127
128 .. index:: single: LONG_MAX
129
130 Return the system's idea of the largest integer it can handle
131 (:const:`LONG_MAX`, as defined in the system header files).
Christian Heimes422051a2008-02-04 18:00:12 +0000132
133
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100134.. c:function:: int PyInt_ClearFreeList()
Christian Heimes422051a2008-02-04 18:00:12 +0000135
Gregory P. Smith2fe77062008-07-06 03:35:58 +0000136 Clear the integer free list. Return the number of items that could not
137 be freed.
Christian Heimes422051a2008-02-04 18:00:12 +0000138
139 .. versionadded:: 2.6