blob: c226aab9b730407b87edff73b5511a6be433dd8f [file] [log] [blame]
Victor Stinnerb6522d02016-12-19 13:12:08 +01001/* Abstract Object Interface (many thanks to Jim Fulton) */
2
Guido van Rossuma8275371995-07-18 14:07:00 +00003#ifndef Py_ABSTRACTOBJECT_H
4#define Py_ABSTRACTOBJECT_H
5#ifdef __cplusplus
6extern "C" {
7#endif
8
Victor Stinner2a358f82016-12-06 16:55:39 +01009/* === Object Protocol ================================================== */
Guido van Rossuma8275371995-07-18 14:07:00 +000010
Victor Stinner2a358f82016-12-06 16:55:39 +010011/* Implemented elsewhere:
Guido van Rossuma8275371995-07-18 14:07:00 +000012
Victor Stinner5bef7cd2016-12-15 09:14:25 +010013 int PyObject_Print(PyObject *o, FILE *fp, int flags);
Guido van Rossuma8275371995-07-18 14:07:00 +000014
Victor Stinner5bef7cd2016-12-15 09:14:25 +010015 Print an object 'o' on file 'fp'. Returns -1 on error. The flags argument
16 is used to enable certain printing options. The only option currently
17 supported is Py_Print_RAW.
Guido van Rossuma8275371995-07-18 14:07:00 +000018
Victor Stinner5bef7cd2016-12-15 09:14:25 +010019 (What should be said about Py_Print_RAW?). */
Victor Stinner2a358f82016-12-06 16:55:39 +010020
21
22/* Implemented elsewhere:
23
Victor Stinner5bef7cd2016-12-15 09:14:25 +010024 int PyObject_HasAttrString(PyObject *o, const char *attr_name);
Victor Stinner2a358f82016-12-06 16:55:39 +010025
Victor Stinner5bef7cd2016-12-15 09:14:25 +010026 Returns 1 if object 'o' has the attribute attr_name, and 0 otherwise.
Victor Stinner2a358f82016-12-06 16:55:39 +010027
Victor Stinner5bef7cd2016-12-15 09:14:25 +010028 This is equivalent to the Python expression: hasattr(o,attr_name).
29
30 This function always succeeds. */
31
Victor Stinner2a358f82016-12-06 16:55:39 +010032
33/* Implemented elsewhere:
34
Victor Stinner5bef7cd2016-12-15 09:14:25 +010035 PyObject* PyObject_GetAttrString(PyObject *o, const char *attr_name);
Victor Stinner2a358f82016-12-06 16:55:39 +010036
Victor Stinner5bef7cd2016-12-15 09:14:25 +010037 Retrieve an attributed named attr_name form object o.
38 Returns the attribute value on success, or NULL on failure.
Victor Stinner2a358f82016-12-06 16:55:39 +010039
Victor Stinner5bef7cd2016-12-15 09:14:25 +010040 This is the equivalent of the Python expression: o.attr_name. */
Victor Stinner2a358f82016-12-06 16:55:39 +010041
Guido van Rossuma8275371995-07-18 14:07:00 +000042
Victor Stinner2a358f82016-12-06 16:55:39 +010043/* Implemented elsewhere:
Guido van Rossuma8275371995-07-18 14:07:00 +000044
Victor Stinner5bef7cd2016-12-15 09:14:25 +010045 int PyObject_HasAttr(PyObject *o, PyObject *attr_name);
Guido van Rossuma8275371995-07-18 14:07:00 +000046
Victor Stinner5bef7cd2016-12-15 09:14:25 +010047 Returns 1 if o has the attribute attr_name, and 0 otherwise.
Guido van Rossuma8275371995-07-18 14:07:00 +000048
Victor Stinner5bef7cd2016-12-15 09:14:25 +010049 This is equivalent to the Python expression: hasattr(o,attr_name).
Guido van Rossuma8275371995-07-18 14:07:00 +000050
Victor Stinner5bef7cd2016-12-15 09:14:25 +010051 This function always succeeds. */
Guido van Rossuma8275371995-07-18 14:07:00 +000052
Victor Stinner2a358f82016-12-06 16:55:39 +010053/* Implemented elsewhere:
Guido van Rossuma8275371995-07-18 14:07:00 +000054
Victor Stinner5bef7cd2016-12-15 09:14:25 +010055 PyObject* PyObject_GetAttr(PyObject *o, PyObject *attr_name);
Guido van Rossuma8275371995-07-18 14:07:00 +000056
Victor Stinner5bef7cd2016-12-15 09:14:25 +010057 Retrieve an attributed named 'attr_name' form object 'o'.
58 Returns the attribute value on success, or NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +000059
Victor Stinner5bef7cd2016-12-15 09:14:25 +010060 This is the equivalent of the Python expression: o.attr_name. */
Marc-André Lemburgad7c98e2001-01-17 17:09:53 +000061
Marc-André Lemburgad7c98e2001-01-17 17:09:53 +000062
Victor Stinner5bef7cd2016-12-15 09:14:25 +010063/* Implemented elsewhere:
Thomas Wouters89f507f2006-12-13 04:49:30 +000064
Victor Stinner5bef7cd2016-12-15 09:14:25 +010065 int PyObject_SetAttrString(PyObject *o, const char *attr_name, PyObject *v);
Guido van Rossuma8275371995-07-18 14:07:00 +000066
Victor Stinner5bef7cd2016-12-15 09:14:25 +010067 Set the value of the attribute named attr_name, for object 'o',
68 to the value 'v'. Raise an exception and return -1 on failure; return 0 on
69 success.
Guido van Rossuma8275371995-07-18 14:07:00 +000070
Victor Stinner5bef7cd2016-12-15 09:14:25 +010071 This is the equivalent of the Python statement o.attr_name=v. */
72
73
74/* Implemented elsewhere:
75
76 int PyObject_SetAttr(PyObject *o, PyObject *attr_name, PyObject *v);
77
78 Set the value of the attribute named attr_name, for object 'o', to the value
79 'v'. an exception and return -1 on failure; return 0 on success.
80
81 This is the equivalent of the Python statement o.attr_name=v. */
82
83/* Implemented as a macro:
84
85 int PyObject_DelAttrString(PyObject *o, const char *attr_name);
86
87 Delete attribute named attr_name, for object o. Returns
88 -1 on failure.
89
90 This is the equivalent of the Python statement: del o.attr_name. */
91#define PyObject_DelAttrString(O,A) PyObject_SetAttrString((O),(A), NULL)
92
93
94/* Implemented as a macro:
95
96 int PyObject_DelAttr(PyObject *o, PyObject *attr_name);
97
98 Delete attribute named attr_name, for object o. Returns -1
99 on failure. This is the equivalent of the Python
100 statement: del o.attr_name. */
101#define PyObject_DelAttr(O,A) PyObject_SetAttr((O),(A), NULL)
102
103
104/* Implemented elsewhere:
105
106 PyObject *PyObject_Repr(PyObject *o);
107
108 Compute the string representation of object 'o'. Returns the
109 string representation on success, NULL on failure.
110
111 This is the equivalent of the Python expression: repr(o).
112
113 Called by the repr() built-in function. */
114
115
116/* Implemented elsewhere:
117
118 PyObject *PyObject_Str(PyObject *o);
119
120 Compute the string representation of object, o. Returns the
121 string representation on success, NULL on failure.
122
123 This is the equivalent of the Python expression: str(o).
124
125 Called by the str() and print() built-in functions. */
126
127
128/* Declared elsewhere
129
130 PyAPI_FUNC(int) PyCallable_Check(PyObject *o);
131
132 Determine if the object, o, is callable. Return 1 if the object is callable
133 and 0 otherwise.
134
135 This function always succeeds. */
136
137
138#ifdef PY_SSIZE_T_CLEAN
139# define PyObject_CallFunction _PyObject_CallFunction_SizeT
140# define PyObject_CallMethod _PyObject_CallMethod_SizeT
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100141#endif
142
Guido van Rossuma8275371995-07-18 14:07:00 +0000143
Victor Stinner2a358f82016-12-06 16:55:39 +0100144/* Call a callable Python object 'callable' with arguments given by the
145 tuple 'args' and keywords arguments given by the dictionary 'kwargs'.
Tim Peters6d6c1a32001-08-02 04:15:00 +0000146
Victor Stinner2a358f82016-12-06 16:55:39 +0100147 'args' must not be *NULL*, use an empty tuple if no arguments are
148 needed. If no named arguments are needed, 'kwargs' can be NULL.
Victor Stinner2d0eb652016-12-06 16:27:24 +0100149
Victor Stinner2a358f82016-12-06 16:55:39 +0100150 This is the equivalent of the Python expression:
151 callable(*args, **kwargs). */
152PyAPI_FUNC(PyObject *) PyObject_Call(PyObject *callable,
153 PyObject *args, PyObject *kwargs);
154
Victor Stinner2d0eb652016-12-06 16:27:24 +0100155
Victor Stinner2a358f82016-12-06 16:55:39 +0100156/* Call a callable Python object 'callable', with arguments given by the
157 tuple 'args'. If no arguments are needed, then 'args' can be *NULL*.
Victor Stinner2d0eb652016-12-06 16:27:24 +0100158
Victor Stinner2a358f82016-12-06 16:55:39 +0100159 Returns the result of the call on success, or *NULL* on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000160
Victor Stinner2a358f82016-12-06 16:55:39 +0100161 This is the equivalent of the Python expression:
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100162 callable(*args). */
Victor Stinner2a358f82016-12-06 16:55:39 +0100163PyAPI_FUNC(PyObject *) PyObject_CallObject(PyObject *callable,
164 PyObject *args);
Guido van Rossuma8275371995-07-18 14:07:00 +0000165
Victor Stinner2a358f82016-12-06 16:55:39 +0100166/* Call a callable Python object, callable, with a variable number of C
167 arguments. The C arguments are described using a mkvalue-style format
168 string.
Victor Stinner2d0eb652016-12-06 16:27:24 +0100169
Victor Stinner2a358f82016-12-06 16:55:39 +0100170 The format may be NULL, indicating that no arguments are provided.
Victor Stinner2d0eb652016-12-06 16:27:24 +0100171
Victor Stinner2a358f82016-12-06 16:55:39 +0100172 Returns the result of the call on success, or NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000173
Victor Stinner2a358f82016-12-06 16:55:39 +0100174 This is the equivalent of the Python expression:
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100175 callable(arg1, arg2, ...). */
Victor Stinner2a358f82016-12-06 16:55:39 +0100176PyAPI_FUNC(PyObject *) PyObject_CallFunction(PyObject *callable,
177 const char *format, ...);
Guido van Rossuma8275371995-07-18 14:07:00 +0000178
Victor Stinner2a358f82016-12-06 16:55:39 +0100179/* Call the method named 'name' of object 'obj' with a variable number of
180 C arguments. The C arguments are described by a mkvalue format string.
Guido van Rossuma8275371995-07-18 14:07:00 +0000181
Victor Stinner2a358f82016-12-06 16:55:39 +0100182 The format can be NULL, indicating that no arguments are provided.
Victor Stinner2d0eb652016-12-06 16:27:24 +0100183
Victor Stinner2a358f82016-12-06 16:55:39 +0100184 Returns the result of the call on success, or NULL on failure.
185
186 This is the equivalent of the Python expression:
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100187 obj.name(arg1, arg2, ...). */
Victor Stinner2a358f82016-12-06 16:55:39 +0100188PyAPI_FUNC(PyObject *) PyObject_CallMethod(PyObject *obj,
189 const char *name,
190 const char *format, ...);
Guido van Rossuma8275371995-07-18 14:07:00 +0000191
Victor Stinner2a358f82016-12-06 16:55:39 +0100192PyAPI_FUNC(PyObject *) _PyObject_CallFunction_SizeT(PyObject *callable,
193 const char *format,
194 ...);
195
196PyAPI_FUNC(PyObject *) _PyObject_CallMethod_SizeT(PyObject *obj,
197 const char *name,
198 const char *format,
199 ...);
200
Victor Stinner2a358f82016-12-06 16:55:39 +0100201/* Call a callable Python object 'callable' with a variable number of C
202 arguments. The C arguments are provided as PyObject* values, terminated
203 by a NULL.
Victor Stinner2d0eb652016-12-06 16:27:24 +0100204
Victor Stinner2a358f82016-12-06 16:55:39 +0100205 Returns the result of the call on success, or NULL on failure.
Victor Stinner2d0eb652016-12-06 16:27:24 +0100206
Victor Stinner2a358f82016-12-06 16:55:39 +0100207 This is the equivalent of the Python expression:
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100208 callable(arg1, arg2, ...). */
Victor Stinner2a358f82016-12-06 16:55:39 +0100209PyAPI_FUNC(PyObject *) PyObject_CallFunctionObjArgs(PyObject *callable,
210 ...);
Fred Drakeb421b8c2001-10-26 16:21:32 +0000211
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100212/* Call the method named 'name' of object 'obj' with a variable number of
213 C arguments. The C arguments are provided as PyObject* values, terminated
214 by NULL.
215
216 Returns the result of the call on success, or NULL on failure.
217
218 This is the equivalent of the Python expression: obj.name(*args). */
Guido van Rossuma8275371995-07-18 14:07:00 +0000219
Victor Stinner2a358f82016-12-06 16:55:39 +0100220PyAPI_FUNC(PyObject *) PyObject_CallMethodObjArgs(
221 PyObject *obj,
222 PyObject *name,
223 ...);
224
Victor Stinner2d0eb652016-12-06 16:27:24 +0100225
Victor Stinner2a358f82016-12-06 16:55:39 +0100226/* Implemented elsewhere:
Guido van Rossuma8275371995-07-18 14:07:00 +0000227
Andrew Svetlov95084022017-12-16 21:08:05 +0200228 Py_hash_t PyObject_Hash(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000229
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100230 Compute and return the hash, hash_value, of an object, o. On
231 failure, return -1.
232
233 This is the equivalent of the Python expression: hash(o). */
Guido van Rossuma8275371995-07-18 14:07:00 +0000234
235
Victor Stinner2a358f82016-12-06 16:55:39 +0100236/* Implemented elsewhere:
Guido van Rossuma8275371995-07-18 14:07:00 +0000237
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100238 int PyObject_IsTrue(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000239
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100240 Returns 1 if the object, o, is considered to be true, 0 if o is
241 considered to be false and -1 on failure.
242
243 This is equivalent to the Python expression: not not o. */
244
Guido van Rossuma8275371995-07-18 14:07:00 +0000245
Victor Stinner2a358f82016-12-06 16:55:39 +0100246/* Implemented elsewhere:
Guido van Rossumc3d3f961998-04-09 17:53:59 +0000247
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100248 int PyObject_Not(PyObject *o);
Guido van Rossumc3d3f961998-04-09 17:53:59 +0000249
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100250 Returns 0 if the object, o, is considered to be true, 1 if o is
251 considered to be false and -1 on failure.
Guido van Rossumc3d3f961998-04-09 17:53:59 +0000252
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100253 This is equivalent to the Python expression: not o. */
254
255
256/* Get the type of an object.
257
258 On success, returns a type object corresponding to the object type of object
259 'o'. On failure, returns NULL.
260
261 This is equivalent to the Python expression: type(o) */
Victor Stinner2a358f82016-12-06 16:55:39 +0100262PyAPI_FUNC(PyObject *) PyObject_Type(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000263
Guido van Rossuma8275371995-07-18 14:07:00 +0000264
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100265/* Return the size of object 'o'. If the object 'o' provides both sequence and
266 mapping protocols, the sequence size is returned.
267
268 On error, -1 is returned.
269
270 This is the equivalent to the Python expression: len(o) */
Victor Stinner2a358f82016-12-06 16:55:39 +0100271PyAPI_FUNC(Py_ssize_t) PyObject_Size(PyObject *o);
Jeremy Hylton6253f832000-07-12 12:56:19 +0000272
Guido van Rossuma8275371995-07-18 14:07:00 +0000273
Victor Stinner2a358f82016-12-06 16:55:39 +0100274/* For DLL compatibility */
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +0000275#undef PyObject_Length
Victor Stinner2a358f82016-12-06 16:55:39 +0100276PyAPI_FUNC(Py_ssize_t) PyObject_Length(PyObject *o);
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +0000277#define PyObject_Length PyObject_Size
278
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100279/* Return element of 'o' corresponding to the object 'key'. Return NULL
280 on failure.
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +0000281
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100282 This is the equivalent of the Python expression: o[key] */
Victor Stinner2a358f82016-12-06 16:55:39 +0100283PyAPI_FUNC(PyObject *) PyObject_GetItem(PyObject *o, PyObject *key);
Guido van Rossuma8275371995-07-18 14:07:00 +0000284
Guido van Rossuma8275371995-07-18 14:07:00 +0000285
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100286/* Map the object 'key' to the value 'v' into 'o'.
287
288 Raise an exception and return -1 on failure; return 0 on success.
289
290 This is the equivalent of the Python statement: o[key]=v. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100291PyAPI_FUNC(int) PyObject_SetItem(PyObject *o, PyObject *key, PyObject *v);
Guido van Rossuma8275371995-07-18 14:07:00 +0000292
Serhiy Storchakaf5b11832018-05-22 11:02:44 +0300293/* Remove the mapping for the string 'key' from the object 'o'.
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100294 Returns -1 on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000295
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100296 This is equivalent to the Python statement: del o[key]. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100297PyAPI_FUNC(int) PyObject_DelItemString(PyObject *o, const char *key);
Martin v. Löwisb0d71d02002-01-05 10:50:30 +0000298
Serhiy Storchakaf5b11832018-05-22 11:02:44 +0300299/* Delete the mapping for the object 'key' from the object 'o'.
300 Returns -1 on failure.
Martin v. Löwisb0d71d02002-01-05 10:50:30 +0000301
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100302 This is the equivalent of the Python statement: del o[key]. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100303PyAPI_FUNC(int) PyObject_DelItem(PyObject *o, PyObject *key);
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000304
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000305
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100306/* === Old Buffer API ============================================ */
307
308/* FIXME: usage of these should all be replaced in Python itself
Victor Stinner2a358f82016-12-06 16:55:39 +0100309 but for backwards compatibility we will implement them.
310 Their usage without a corresponding "unlock" mechanism
311 may create issues (but they would already be there). */
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000312
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100313/* Takes an arbitrary object which must support the (character, single segment)
314 buffer interface and returns a pointer to a read-only memory location
315 useable as character based input for subsequent processing.
316
317 Return 0 on success. buffer and buffer_len are only set in case no error
318 occurs. Otherwise, -1 is returned and an exception set. */
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600319Py_DEPRECATED(3.0)
Victor Stinner2a358f82016-12-06 16:55:39 +0100320PyAPI_FUNC(int) PyObject_AsCharBuffer(PyObject *obj,
321 const char **buffer,
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600322 Py_ssize_t *buffer_len);
Guido van Rossumfd9eed32000-03-10 22:35:06 +0000323
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100324/* Checks whether an arbitrary object supports the (character, single segment)
325 buffer interface.
Guido van Rossumfd9eed32000-03-10 22:35:06 +0000326
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100327 Returns 1 on success, 0 on failure. */
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600328Py_DEPRECATED(3.0) PyAPI_FUNC(int) PyObject_CheckReadBuffer(PyObject *obj);
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000329
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100330/* Same as PyObject_AsCharBuffer() except that this API expects (readable,
331 single segment) buffer interface and returns a pointer to a read-only memory
332 location which can contain arbitrary data.
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000333
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100334 0 is returned on success. buffer and buffer_len are only set in case no
335 error occurs. Otherwise, -1 is returned and an exception set. */
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600336Py_DEPRECATED(3.0)
Victor Stinner2a358f82016-12-06 16:55:39 +0100337PyAPI_FUNC(int) PyObject_AsReadBuffer(PyObject *obj,
338 const void **buffer,
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600339 Py_ssize_t *buffer_len);
Guido van Rossumfd9eed32000-03-10 22:35:06 +0000340
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100341/* Takes an arbitrary object which must support the (writable, single segment)
342 buffer interface and returns a pointer to a writable memory location in
343 buffer of size 'buffer_len'.
Guido van Rossumfd9eed32000-03-10 22:35:06 +0000344
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100345 Return 0 on success. buffer and buffer_len are only set in case no error
346 occurs. Otherwise, -1 is returned and an exception set. */
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600347Py_DEPRECATED(3.0)
Victor Stinner2a358f82016-12-06 16:55:39 +0100348PyAPI_FUNC(int) PyObject_AsWriteBuffer(PyObject *obj,
349 void **buffer,
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600350 Py_ssize_t *buffer_len);
Guido van Rossumfd9eed32000-03-10 22:35:06 +0000351
Guido van Rossumfd9eed32000-03-10 22:35:06 +0000352
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100353/* === New Buffer API ============================================ */
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000354
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100355/* Takes an arbitrary object and returns the result of calling
356 obj.__format__(format_spec). */
357PyAPI_FUNC(PyObject *) PyObject_Format(PyObject *obj,
Victor Stinner2a358f82016-12-06 16:55:39 +0100358 PyObject *format_spec);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000359
Guido van Rossum213c7a62001-04-23 14:08:49 +0000360
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100361/* ==== Iterators ================================================ */
362
363/* Takes an object and returns an iterator for it.
364 This is typically a new iterator but if the argument is an iterator, this
365 returns itself. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100366PyAPI_FUNC(PyObject *) PyObject_GetIter(PyObject *);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000367
Christian Tismerea62ce72018-06-09 20:32:25 +0200368/* Returns 1 if the object 'obj' provides iterator protocols, and 0 otherwise.
369
370 This function always succeeds. */
Serhiy Storchaka5cbefa92018-06-11 15:01:47 +0300371PyAPI_FUNC(int) PyIter_Check(PyObject *);
Guido van Rossum213c7a62001-04-23 14:08:49 +0000372
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100373/* Takes an iterator object and calls its tp_iternext slot,
374 returning the next value.
375
376 If the iterator is exhausted, this returns NULL without setting an
377 exception.
378
379 NULL with an exception means an error occurred. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100380PyAPI_FUNC(PyObject *) PyIter_Next(PyObject *);
Guido van Rossum213c7a62001-04-23 14:08:49 +0000381
Guido van Rossuma8275371995-07-18 14:07:00 +0000382
Victor Stinner2a358f82016-12-06 16:55:39 +0100383/* === Number Protocol ================================================== */
Guido van Rossuma8275371995-07-18 14:07:00 +0000384
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100385/* Returns 1 if the object 'o' provides numeric protocols, and 0 otherwise.
386
387 This function always succeeds. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100388PyAPI_FUNC(int) PyNumber_Check(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000389
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100390/* Returns the result of adding o1 and o2, or NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000391
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100392 This is the equivalent of the Python expression: o1 + o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100393PyAPI_FUNC(PyObject *) PyNumber_Add(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000394
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100395/* Returns the result of subtracting o2 from o1, or NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000396
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100397 This is the equivalent of the Python expression: o1 - o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100398PyAPI_FUNC(PyObject *) PyNumber_Subtract(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000399
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100400/* Returns the result of multiplying o1 and o2, or NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000401
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100402 This is the equivalent of the Python expression: o1 * o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100403PyAPI_FUNC(PyObject *) PyNumber_Multiply(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000404
Serhiy Storchaka34d0ac82016-12-27 14:57:39 +0200405#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100406/* This is the equivalent of the Python expression: o1 @ o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100407PyAPI_FUNC(PyObject *) PyNumber_MatrixMultiply(PyObject *o1, PyObject *o2);
Serhiy Storchaka34d0ac82016-12-27 14:57:39 +0200408#endif
Benjamin Petersond51374e2014-04-09 23:55:56 -0400409
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100410/* Returns the result of dividing o1 by o2 giving an integral result,
411 or NULL on failure.
Guido van Rossum4668b002001-08-08 05:00:18 +0000412
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100413 This is the equivalent of the Python expression: o1 // o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100414PyAPI_FUNC(PyObject *) PyNumber_FloorDivide(PyObject *o1, PyObject *o2);
Guido van Rossum4668b002001-08-08 05:00:18 +0000415
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100416/* Returns the result of dividing o1 by o2 giving a float result, or NULL on
417 failure.
Guido van Rossum4668b002001-08-08 05:00:18 +0000418
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100419 This is the equivalent of the Python expression: o1 / o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100420PyAPI_FUNC(PyObject *) PyNumber_TrueDivide(PyObject *o1, PyObject *o2);
Guido van Rossum4668b002001-08-08 05:00:18 +0000421
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100422/* Returns the remainder of dividing o1 by o2, or NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000423
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100424 This is the equivalent of the Python expression: o1 % o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100425PyAPI_FUNC(PyObject *) PyNumber_Remainder(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000426
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100427/* See the built-in function divmod.
Guido van Rossuma8275371995-07-18 14:07:00 +0000428
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100429 Returns NULL on failure.
430
431 This is the equivalent of the Python expression: divmod(o1, o2). */
Victor Stinner2a358f82016-12-06 16:55:39 +0100432PyAPI_FUNC(PyObject *) PyNumber_Divmod(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000433
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100434/* See the built-in function pow. Returns NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000435
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100436 This is the equivalent of the Python expression: pow(o1, o2, o3),
437 where o3 is optional. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100438PyAPI_FUNC(PyObject *) PyNumber_Power(PyObject *o1, PyObject *o2,
439 PyObject *o3);
Guido van Rossuma8275371995-07-18 14:07:00 +0000440
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100441/* Returns the negation of o on success, or NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000442
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100443 This is the equivalent of the Python expression: -o. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100444PyAPI_FUNC(PyObject *) PyNumber_Negative(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000445
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100446/* Returns the positive of o on success, or NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000447
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100448 This is the equivalent of the Python expression: +o. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100449PyAPI_FUNC(PyObject *) PyNumber_Positive(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000450
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100451/* Returns the absolute value of 'o', or NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000452
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100453 This is the equivalent of the Python expression: abs(o). */
Victor Stinner2a358f82016-12-06 16:55:39 +0100454PyAPI_FUNC(PyObject *) PyNumber_Absolute(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000455
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100456/* Returns the bitwise negation of 'o' on success, or NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000457
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100458 This is the equivalent of the Python expression: ~o. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100459PyAPI_FUNC(PyObject *) PyNumber_Invert(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000460
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100461/* Returns the result of left shifting o1 by o2 on success, or NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000462
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100463 This is the equivalent of the Python expression: o1 << o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100464PyAPI_FUNC(PyObject *) PyNumber_Lshift(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000465
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100466/* Returns the result of right shifting o1 by o2 on success, or NULL on
467 failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000468
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100469 This is the equivalent of the Python expression: o1 >> o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100470PyAPI_FUNC(PyObject *) PyNumber_Rshift(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000471
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100472/* Returns the result of bitwise and of o1 and o2 on success, or NULL on
473 failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000474
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100475 This is the equivalent of the Python expression: o1 & o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100476PyAPI_FUNC(PyObject *) PyNumber_And(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000477
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100478/* Returns the bitwise exclusive or of o1 by o2 on success, or NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000479
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100480 This is the equivalent of the Python expression: o1 ^ o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100481PyAPI_FUNC(PyObject *) PyNumber_Xor(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000482
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100483/* Returns the result of bitwise or on o1 and o2 on success, or NULL on
484 failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000485
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100486 This is the equivalent of the Python expression: o1 | o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100487PyAPI_FUNC(PyObject *) PyNumber_Or(PyObject *o1, PyObject *o2);
488
Christian Tismerea62ce72018-06-09 20:32:25 +0200489/* Returns 1 if obj is an index integer (has the nb_index slot of the
490 tp_as_number structure filled in), and 0 otherwise. */
Serhiy Storchaka5cbefa92018-06-11 15:01:47 +0300491PyAPI_FUNC(int) PyIndex_Check(PyObject *);
Guido van Rossuma8275371995-07-18 14:07:00 +0000492
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100493/* Returns the object 'o' converted to a Python int, or NULL with an exception
494 raised on failure. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100495PyAPI_FUNC(PyObject *) PyNumber_Index(PyObject *o);
Guido van Rossum38fff8c2006-03-07 18:50:55 +0000496
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100497/* Returns the object 'o' converted to Py_ssize_t by going through
498 PyNumber_Index() first.
Guido van Rossum38fff8c2006-03-07 18:50:55 +0000499
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100500 If an overflow error occurs while converting the int to Py_ssize_t, then the
501 second argument 'exc' is the error-type to return. If it is NULL, then the
502 overflow error is cleared and the value is clipped. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100503PyAPI_FUNC(Py_ssize_t) PyNumber_AsSsize_t(PyObject *o, PyObject *exc);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000504
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100505/* Returns the object 'o' converted to an integer object on success, or NULL
506 on failure.
Guido van Rossum38fff8c2006-03-07 18:50:55 +0000507
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100508 This is the equivalent of the Python expression: int(o). */
Victor Stinner2a358f82016-12-06 16:55:39 +0100509PyAPI_FUNC(PyObject *) PyNumber_Long(PyObject *o);
Mark Dickinsond7467682009-01-10 22:14:33 +0000510
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100511/* Returns the object 'o' converted to a float object on success, or NULL
512 on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000513
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100514 This is the equivalent of the Python expression: float(o). */
Victor Stinner2a358f82016-12-06 16:55:39 +0100515PyAPI_FUNC(PyObject *) PyNumber_Float(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000516
Victor Stinner2a358f82016-12-06 16:55:39 +0100517
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100518/* --- In-place variants of (some of) the above number protocol functions -- */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100520/* Returns the result of adding o2 to o1, possibly in-place, or NULL
521 on failure.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000522
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100523 This is the equivalent of the Python expression: o1 += o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100524PyAPI_FUNC(PyObject *) PyNumber_InPlaceAdd(PyObject *o1, PyObject *o2);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000525
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100526/* Returns the result of subtracting o2 from o1, possibly in-place or
527 NULL on failure.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000528
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100529 This is the equivalent of the Python expression: o1 -= o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100530PyAPI_FUNC(PyObject *) PyNumber_InPlaceSubtract(PyObject *o1, PyObject *o2);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000531
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100532/* Returns the result of multiplying o1 by o2, possibly in-place, or NULL on
533 failure.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000534
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100535 This is the equivalent of the Python expression: o1 *= o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100536PyAPI_FUNC(PyObject *) PyNumber_InPlaceMultiply(PyObject *o1, PyObject *o2);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000537
Serhiy Storchaka34d0ac82016-12-27 14:57:39 +0200538#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100539/* This is the equivalent of the Python expression: o1 @= o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100540PyAPI_FUNC(PyObject *) PyNumber_InPlaceMatrixMultiply(PyObject *o1, PyObject *o2);
Serhiy Storchaka34d0ac82016-12-27 14:57:39 +0200541#endif
Benjamin Petersond51374e2014-04-09 23:55:56 -0400542
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100543/* Returns the result of dividing o1 by o2 giving an integral result, possibly
544 in-place, or NULL on failure.
Benjamin Petersond51374e2014-04-09 23:55:56 -0400545
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100546 This is the equivalent of the Python expression: o1 /= o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100547PyAPI_FUNC(PyObject *) PyNumber_InPlaceFloorDivide(PyObject *o1,
548 PyObject *o2);
Guido van Rossum4668b002001-08-08 05:00:18 +0000549
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100550/* Returns the result of dividing o1 by o2 giving a float result, possibly
551 in-place, or null on failure.
Guido van Rossum4668b002001-08-08 05:00:18 +0000552
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100553 This is the equivalent of the Python expression: o1 /= o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100554PyAPI_FUNC(PyObject *) PyNumber_InPlaceTrueDivide(PyObject *o1,
555 PyObject *o2);
Guido van Rossum4668b002001-08-08 05:00:18 +0000556
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100557/* Returns the remainder of dividing o1 by o2, possibly in-place, or NULL on
558 failure.
Guido van Rossum4668b002001-08-08 05:00:18 +0000559
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100560 This is the equivalent of the Python expression: o1 %= o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100561PyAPI_FUNC(PyObject *) PyNumber_InPlaceRemainder(PyObject *o1, PyObject *o2);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000562
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100563/* Returns the result of raising o1 to the power of o2, possibly in-place,
564 or NULL on failure.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000565
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100566 This is the equivalent of the Python expression: o1 **= o2,
567 or o1 = pow(o1, o2, o3) if o3 is present. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100568PyAPI_FUNC(PyObject *) PyNumber_InPlacePower(PyObject *o1, PyObject *o2,
569 PyObject *o3);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000570
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100571/* Returns the result of left shifting o1 by o2, possibly in-place, or NULL
572 on failure.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000573
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100574 This is the equivalent of the Python expression: o1 <<= o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100575PyAPI_FUNC(PyObject *) PyNumber_InPlaceLshift(PyObject *o1, PyObject *o2);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000576
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100577/* Returns the result of right shifting o1 by o2, possibly in-place or NULL
578 on failure.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000579
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100580 This is the equivalent of the Python expression: o1 >>= o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100581PyAPI_FUNC(PyObject *) PyNumber_InPlaceRshift(PyObject *o1, PyObject *o2);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000582
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100583/* Returns the result of bitwise and of o1 and o2, possibly in-place, or NULL
584 on failure.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000585
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100586 This is the equivalent of the Python expression: o1 &= o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100587PyAPI_FUNC(PyObject *) PyNumber_InPlaceAnd(PyObject *o1, PyObject *o2);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000588
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100589/* Returns the bitwise exclusive or of o1 by o2, possibly in-place, or NULL
590 on failure.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000591
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100592 This is the equivalent of the Python expression: o1 ^= o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100593PyAPI_FUNC(PyObject *) PyNumber_InPlaceXor(PyObject *o1, PyObject *o2);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000594
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100595/* Returns the result of bitwise or of o1 and o2, possibly in-place,
596 or NULL on failure.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000597
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100598 This is the equivalent of the Python expression: o1 |= o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100599PyAPI_FUNC(PyObject *) PyNumber_InPlaceOr(PyObject *o1, PyObject *o2);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000600
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100601/* Returns the integer n converted to a string with a base, with a base
602 marker of 0b, 0o or 0x prefixed if applicable.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000603
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100604 If n is not an int object, it is converted with PyNumber_Index first. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100605PyAPI_FUNC(PyObject *) PyNumber_ToBase(PyObject *n, int base);
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000606
Guido van Rossuma8275371995-07-18 14:07:00 +0000607
Victor Stinner2a358f82016-12-06 16:55:39 +0100608/* === Sequence protocol ================================================ */
Guido van Rossuma8275371995-07-18 14:07:00 +0000609
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100610/* Return 1 if the object provides sequence protocol, and zero
611 otherwise.
612
613 This function always succeeds. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100614PyAPI_FUNC(int) PySequence_Check(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000615
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100616/* Return the size of sequence object o, or -1 on failure. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100617PyAPI_FUNC(Py_ssize_t) PySequence_Size(PyObject *o);
Jeremy Hylton6253f832000-07-12 12:56:19 +0000618
Victor Stinner2a358f82016-12-06 16:55:39 +0100619/* For DLL compatibility */
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +0000620#undef PySequence_Length
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100621PyAPI_FUNC(Py_ssize_t) PySequence_Length(PyObject *o);
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +0000622#define PySequence_Length PySequence_Size
623
624
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100625/* Return the concatenation of o1 and o2 on success, and NULL on failure.
626
627 This is the equivalent of the Python expression: o1 + o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100628PyAPI_FUNC(PyObject *) PySequence_Concat(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000629
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100630/* Return the result of repeating sequence object 'o' 'count' times,
631 or NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000632
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100633 This is the equivalent of the Python expression: o * count. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100634PyAPI_FUNC(PyObject *) PySequence_Repeat(PyObject *o, Py_ssize_t count);
Guido van Rossuma8275371995-07-18 14:07:00 +0000635
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100636/* Return the ith element of o, or NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000637
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100638 This is the equivalent of the Python expression: o[i]. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100639PyAPI_FUNC(PyObject *) PySequence_GetItem(PyObject *o, Py_ssize_t i);
Guido van Rossuma8275371995-07-18 14:07:00 +0000640
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100641/* Return the slice of sequence object o between i1 and i2, or NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000642
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100643 This is the equivalent of the Python expression: o[i1:i2]. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100644PyAPI_FUNC(PyObject *) PySequence_GetSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000645
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100646/* Assign object 'v' to the ith element of the sequence 'o'. Raise an exception
647 and return -1 on failure; return 0 on success.
Guido van Rossuma8275371995-07-18 14:07:00 +0000648
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100649 This is the equivalent of the Python statement o[i] = v. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100650PyAPI_FUNC(int) PySequence_SetItem(PyObject *o, Py_ssize_t i, PyObject *v);
Guido van Rossuma8275371995-07-18 14:07:00 +0000651
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100652/* Delete the 'i'-th element of the sequence 'v'. Returns -1 on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000653
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100654 This is the equivalent of the Python statement: del o[i]. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100655PyAPI_FUNC(int) PySequence_DelItem(PyObject *o, Py_ssize_t i);
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000656
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100657/* Assign the sequence object 'v' to the slice in sequence object 'o',
658 from 'i1' to 'i2'. Returns -1 on failure.
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000659
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100660 This is the equivalent of the Python statement: o[i1:i2] = v. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100661PyAPI_FUNC(int) PySequence_SetSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2,
662 PyObject *v);
Guido van Rossuma8275371995-07-18 14:07:00 +0000663
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100664/* Delete the slice in sequence object 'o' from 'i1' to 'i2'.
665 Returns -1 on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000666
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100667 This is the equivalent of the Python statement: del o[i1:i2]. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100668PyAPI_FUNC(int) PySequence_DelSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2);
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000669
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100670/* Returns the sequence 'o' as a tuple on success, and NULL on failure.
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000671
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100672 This is equivalent to the Python expression: tuple(o). */
Victor Stinner2a358f82016-12-06 16:55:39 +0100673PyAPI_FUNC(PyObject *) PySequence_Tuple(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000674
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100675/* Returns the sequence 'o' as a list on success, and NULL on failure.
676 This is equivalent to the Python expression: list(o) */
Victor Stinner2a358f82016-12-06 16:55:39 +0100677PyAPI_FUNC(PyObject *) PySequence_List(PyObject *o);
Guido van Rossumf39fc431997-03-04 18:31:47 +0000678
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100679/* Return the sequence 'o' as a list, unless it's already a tuple or list.
680
681 Use PySequence_Fast_GET_ITEM to access the members of this list, and
682 PySequence_Fast_GET_SIZE to get its length.
683
684 Returns NULL on failure. If the object does not support iteration, raises a
685 TypeError exception with 'm' as the message text. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100686PyAPI_FUNC(PyObject *) PySequence_Fast(PyObject *o, const char* m);
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000687
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100688/* Return the size of the sequence 'o', assuming that 'o' was returned by
689 PySequence_Fast and is not NULL. */
Tim Peters1fc240e2001-10-26 05:06:50 +0000690#define PySequence_Fast_GET_SIZE(o) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 (PyList_Check(o) ? PyList_GET_SIZE(o) : PyTuple_GET_SIZE(o))
Tim Peters1fc240e2001-10-26 05:06:50 +0000692
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100693/* Return the 'i'-th element of the sequence 'o', assuming that o was returned
694 by PySequence_Fast, and that i is within bounds. */
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000695#define PySequence_Fast_GET_ITEM(o, i)\
696 (PyList_Check(o) ? PyList_GET_ITEM(o, i) : PyTuple_GET_ITEM(o, i))
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000697
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100698/* Return a pointer to the underlying item array for
699 an object retured by PySequence_Fast */
Raymond Hettinger42bec932004-03-12 16:38:17 +0000700#define PySequence_Fast_ITEMS(sf) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 (PyList_Check(sf) ? ((PyListObject *)(sf))->ob_item \
702 : ((PyTupleObject *)(sf))->ob_item)
Raymond Hettingerc1e4f9d2004-03-12 08:04:00 +0000703
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100704/* Return the number of occurrences on value on 'o', that is, return
705 the number of keys for which o[key] == value.
706
707 On failure, return -1. This is equivalent to the Python expression:
708 o.count(value). */
Victor Stinner2a358f82016-12-06 16:55:39 +0100709PyAPI_FUNC(Py_ssize_t) PySequence_Count(PyObject *o, PyObject *value);
Guido van Rossuma8275371995-07-18 14:07:00 +0000710
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100711/* Return 1 if 'ob' is in the sequence 'seq'; 0 if 'ob' is not in the sequence
712 'seq'; -1 on error.
Guido van Rossuma8275371995-07-18 14:07:00 +0000713
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100714 Use __contains__ if possible, else _PySequence_IterSearch(). */
Victor Stinner2a358f82016-12-06 16:55:39 +0100715PyAPI_FUNC(int) PySequence_Contains(PyObject *seq, PyObject *ob);
Tim Peterscb8d3682001-05-05 21:05:01 +0000716
Guido van Rossum83684531999-03-17 18:44:39 +0000717/* For DLL-level backwards compatibility */
718#undef PySequence_In
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100719/* Determine if the sequence 'o' contains 'value'. If an item in 'o' is equal
720 to 'value', return 1, otherwise return 0. On error, return -1.
721
722 This is equivalent to the Python expression: value in o. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100723PyAPI_FUNC(int) PySequence_In(PyObject *o, PyObject *value);
Guido van Rossum83684531999-03-17 18:44:39 +0000724
725/* For source-level backwards compatibility */
Guido van Rossumf1536db1998-08-23 22:06:59 +0000726#define PySequence_In PySequence_Contains
Guido van Rossuma8275371995-07-18 14:07:00 +0000727
Guido van Rossuma8275371995-07-18 14:07:00 +0000728
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100729/* Return the first index for which o[i] == value.
730 On error, return -1.
731
732 This is equivalent to the Python expression: o.index(value). */
Victor Stinner2a358f82016-12-06 16:55:39 +0100733PyAPI_FUNC(Py_ssize_t) PySequence_Index(PyObject *o, PyObject *value);
Guido van Rossuma8275371995-07-18 14:07:00 +0000734
Victor Stinner2a358f82016-12-06 16:55:39 +0100735
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100736/* --- In-place versions of some of the above Sequence functions --- */
Guido van Rossuma8275371995-07-18 14:07:00 +0000737
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100738/* Append sequence 'o2' to sequence 'o1', in-place when possible. Return the
739 resulting object, which could be 'o1', or NULL on failure.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000740
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100741 This is the equivalent of the Python expression: o1 += o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100742PyAPI_FUNC(PyObject *) PySequence_InPlaceConcat(PyObject *o1, PyObject *o2);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000743
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100744/* Repeat sequence 'o' by 'count', in-place when possible. Return the resulting
745 object, which could be 'o', or NULL on failure.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000746
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100747 This is the equivalent of the Python expression: o1 *= count. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100748PyAPI_FUNC(PyObject *) PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000749
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000750
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100751/* === Mapping protocol ================================================= */
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000752
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100753/* Return 1 if the object provides mapping protocol, and 0 otherwise.
Guido van Rossuma8275371995-07-18 14:07:00 +0000754
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100755 This function always succeeds. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100756PyAPI_FUNC(int) PyMapping_Check(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000757
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100758/* Returns the number of keys in mapping object 'o' on success, and -1 on
Serhiy Storchakaf5b11832018-05-22 11:02:44 +0300759 failure. This is equivalent to the Python expression: len(o). */
Victor Stinner2a358f82016-12-06 16:55:39 +0100760PyAPI_FUNC(Py_ssize_t) PyMapping_Size(PyObject *o);
Jeremy Hylton6253f832000-07-12 12:56:19 +0000761
Victor Stinner2a358f82016-12-06 16:55:39 +0100762/* For DLL compatibility */
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +0000763#undef PyMapping_Length
Victor Stinner2a358f82016-12-06 16:55:39 +0100764PyAPI_FUNC(Py_ssize_t) PyMapping_Length(PyObject *o);
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +0000765#define PyMapping_Length PyMapping_Size
766
767
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100768/* Implemented as a macro:
Guido van Rossuma25e5e91996-09-06 13:48:38 +0000769
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100770 int PyMapping_DelItemString(PyObject *o, const char *key);
Guido van Rossuma8275371995-07-18 14:07:00 +0000771
Serhiy Storchakaf5b11832018-05-22 11:02:44 +0300772 Remove the mapping for the string 'key' from the mapping 'o'. Returns -1 on
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100773 failure.
774
775 This is equivalent to the Python statement: del o[key]. */
Jeremy Hylton7c7ee5f2001-11-28 16:20:07 +0000776#define PyMapping_DelItemString(O,K) PyObject_DelItemString((O),(K))
Guido van Rossuma25e5e91996-09-06 13:48:38 +0000777
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100778/* Implemented as a macro:
Guido van Rossuma8275371995-07-18 14:07:00 +0000779
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100780 int PyMapping_DelItem(PyObject *o, PyObject *key);
Guido van Rossuma8275371995-07-18 14:07:00 +0000781
Serhiy Storchakaf5b11832018-05-22 11:02:44 +0300782 Remove the mapping for the object 'key' from the mapping object 'o'.
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100783 Returns -1 on failure.
784
785 This is equivalent to the Python statement: del o[key]. */
Jeremy Hylton7c7ee5f2001-11-28 16:20:07 +0000786#define PyMapping_DelItem(O,K) PyObject_DelItem((O),(K))
Guido van Rossuma8275371995-07-18 14:07:00 +0000787
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100788/* On success, return 1 if the mapping object 'o' has the key 'key',
789 and 0 otherwise.
790
791 This is equivalent to the Python expression: key in o.
792
793 This function always succeeds. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100794PyAPI_FUNC(int) PyMapping_HasKeyString(PyObject *o, const char *key);
Guido van Rossuma8275371995-07-18 14:07:00 +0000795
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100796/* Return 1 if the mapping object has the key 'key', and 0 otherwise.
Guido van Rossuma8275371995-07-18 14:07:00 +0000797
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100798 This is equivalent to the Python expression: key in o.
Guido van Rossuma8275371995-07-18 14:07:00 +0000799
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100800 This function always succeeds. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100801PyAPI_FUNC(int) PyMapping_HasKey(PyObject *o, PyObject *key);
Guido van Rossuma8275371995-07-18 14:07:00 +0000802
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100803/* On success, return a list or tuple of the keys in mapping object 'o'.
804 On failure, return NULL. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100805PyAPI_FUNC(PyObject *) PyMapping_Keys(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000806
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100807/* On success, return a list or tuple of the values in mapping object 'o'.
808 On failure, return NULL. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100809PyAPI_FUNC(PyObject *) PyMapping_Values(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000810
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100811/* On success, return a list or tuple of the items in mapping object 'o',
812 where each item is a tuple containing a key-value pair. On failure, return
813 NULL. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100814PyAPI_FUNC(PyObject *) PyMapping_Items(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000815
Serhiy Storchakaf5b11832018-05-22 11:02:44 +0300816/* Return element of 'o' corresponding to the string 'key' or NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000817
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100818 This is the equivalent of the Python expression: o[key]. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100819PyAPI_FUNC(PyObject *) PyMapping_GetItemString(PyObject *o,
820 const char *key);
Guido van Rossuma8275371995-07-18 14:07:00 +0000821
Serhiy Storchakaf5b11832018-05-22 11:02:44 +0300822/* Map the string 'key' to the value 'v' in the mapping 'o'.
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100823 Returns -1 on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000824
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100825 This is the equivalent of the Python statement: o[key]=v. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100826PyAPI_FUNC(int) PyMapping_SetItemString(PyObject *o, const char *key,
827 PyObject *value);
Guido van Rossuma8275371995-07-18 14:07:00 +0000828
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100829/* isinstance(object, typeorclass) */
Mark Hammond91a681d2002-08-12 07:21:58 +0000830PyAPI_FUNC(int) PyObject_IsInstance(PyObject *object, PyObject *typeorclass);
Guido van Rossum823649d2001-03-21 18:40:58 +0000831
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100832/* issubclass(object, typeorclass) */
Mark Hammond91a681d2002-08-12 07:21:58 +0000833PyAPI_FUNC(int) PyObject_IsSubclass(PyObject *object, PyObject *typeorclass);
Guido van Rossum823649d2001-03-21 18:40:58 +0000834
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000835#ifndef Py_LIMITED_API
Victor Stinner40602832018-11-26 22:42:04 +0100836# define Py_CPYTHON_ABSTRACTOBJECT_H
837# include "cpython/abstract.h"
838# undef Py_CPYTHON_ABSTRACTOBJECT_H
839#endif
Antoine Pitrouf68c2a72010-09-01 12:58:21 +0000840
Guido van Rossum8ca687a1995-09-18 21:20:02 +0000841#ifdef __cplusplus
842}
843#endif
Guido van Rossuma8275371995-07-18 14:07:00 +0000844#endif /* Py_ABSTRACTOBJECT_H */