blob: 85550a34ca26865f5f99b458a76facd6f3e430c1 [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
141# ifndef Py_LIMITED_API
142# define _PyObject_CallMethodId _PyObject_CallMethodId_SizeT
143# endif /* !Py_LIMITED_API */
144#endif
145
Guido van Rossuma8275371995-07-18 14:07:00 +0000146
Victor Stinner2a358f82016-12-06 16:55:39 +0100147/* Call a callable Python object 'callable' with arguments given by the
148 tuple 'args' and keywords arguments given by the dictionary 'kwargs'.
Tim Peters6d6c1a32001-08-02 04:15:00 +0000149
Victor Stinner2a358f82016-12-06 16:55:39 +0100150 'args' must not be *NULL*, use an empty tuple if no arguments are
151 needed. If no named arguments are needed, 'kwargs' can be NULL.
Victor Stinner2d0eb652016-12-06 16:27:24 +0100152
Victor Stinner2a358f82016-12-06 16:55:39 +0100153 This is the equivalent of the Python expression:
154 callable(*args, **kwargs). */
155PyAPI_FUNC(PyObject *) PyObject_Call(PyObject *callable,
156 PyObject *args, PyObject *kwargs);
157
Victor Stinner4a7cc882015-03-06 23:35:27 +0100158#ifndef Py_LIMITED_API
Victor Stinner2a358f82016-12-06 16:55:39 +0100159PyAPI_FUNC(PyObject*) _PyStack_AsTuple(
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200160 PyObject *const *stack,
Victor Stinner2a358f82016-12-06 16:55:39 +0100161 Py_ssize_t nargs);
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200162
Victor Stinner69de71b2017-01-16 23:50:53 +0100163PyAPI_FUNC(PyObject*) _PyStack_AsTupleSlice(
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200164 PyObject *const *stack,
Victor Stinner69de71b2017-01-16 23:50:53 +0100165 Py_ssize_t nargs,
166 Py_ssize_t start,
167 Py_ssize_t end);
168
Victor Stinnerc3858bd2017-01-24 15:05:30 +0100169/* Convert keyword arguments from the FASTCALL (stack: C array, kwnames: tuple)
170 format to a Python dictionary ("kwargs" dict).
Victor Stinner57f91ac2016-09-12 13:37:07 +0200171
Victor Stinnerc3858bd2017-01-24 15:05:30 +0100172 The type of kwnames keys is not checked. The final function getting
Ville Skyttä49b27342017-08-03 09:00:59 +0300173 arguments is responsible to check if all keys are strings, for example using
Victor Stinnerc3858bd2017-01-24 15:05:30 +0100174 PyArg_ParseTupleAndKeywords() or PyArg_ValidateKeywordArguments().
175
176 Duplicate keys are merged using the last value. If duplicate keys must raise
177 an exception, the caller is responsible to implement an explicit keys on
178 kwnames. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100179PyAPI_FUNC(PyObject *) _PyStack_AsDict(
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200180 PyObject *const *values,
Victor Stinner2a358f82016-12-06 16:55:39 +0100181 PyObject *kwnames);
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700182
Victor Stinner998c2092017-01-17 01:57:29 +0100183/* Convert (args, nargs, kwargs: dict) into a (stack, nargs, kwnames: tuple).
Victor Stinnera9efb2f2016-09-09 17:40:22 -0700184
Victor Stinner998c2092017-01-17 01:57:29 +0100185 Return 0 on success, raise an exception and return -1 on error.
186
187 Write the new stack into *p_stack. If *p_stack is differen than args, it
188 must be released by PyMem_Free().
Victor Stinnera9efb2f2016-09-09 17:40:22 -0700189
Victor Stinner2a358f82016-12-06 16:55:39 +0100190 The stack uses borrowed references.
Victor Stinnera9efb2f2016-09-09 17:40:22 -0700191
Victor Stinner2a358f82016-12-06 16:55:39 +0100192 The type of keyword keys is not checked, these checks should be done
Victor Stinner3e1fad62017-01-17 01:29:01 +0100193 later (ex: _PyArg_ParseStackAndKeywords). */
Victor Stinner998c2092017-01-17 01:57:29 +0100194PyAPI_FUNC(int) _PyStack_UnpackDict(
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200195 PyObject *const *args,
Victor Stinner2a358f82016-12-06 16:55:39 +0100196 Py_ssize_t nargs,
197 PyObject *kwargs,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200198 PyObject *const **p_stack,
Victor Stinner35ecebe2017-01-18 10:31:46 +0100199 PyObject **p_kwnames);
Victor Stinnera9efb2f2016-09-09 17:40:22 -0700200
Victor Stinnerbc08ab42016-12-15 12:40:53 +0100201/* Suggested size (number of positional arguments) for arrays of PyObject*
202 allocated on a C stack to avoid allocating memory on the heap memory. Such
203 array is used to pass positional arguments to call functions of the
204 _PyObject_FastCall() family.
205
206 The size is chosen to not abuse the C stack and so limit the risk of stack
207 overflow. The size is also chosen to allow using the small stack for most
208 function calls of the Python standard library. On 64-bit CPU, it allocates
209 40 bytes on the stack. */
210#define _PY_FASTCALL_SMALL_STACK 5
211
Victor Stinner0f7b0b32017-03-14 21:37:20 +0100212/* Return 1 if callable supports FASTCALL calling convention for positional
213 arguments: see _PyObject_FastCallDict() and _PyObject_FastCallKeywords() */
214PyAPI_FUNC(int) _PyObject_HasFastCall(PyObject *callable);
215
Victor Stinner2a358f82016-12-06 16:55:39 +0100216/* Call the callable object 'callable' with the "fast call" calling convention:
217 args is a C array for positional arguments (nargs is the number of
218 positional arguments), kwargs is a dictionary for keyword arguments.
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200219
Victor Stinner2a358f82016-12-06 16:55:39 +0100220 If nargs is equal to zero, args can be NULL. kwargs can be NULL.
221 nargs must be greater or equal to zero.
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200222
Victor Stinner2a358f82016-12-06 16:55:39 +0100223 Return the result on success. Raise an exception on return NULL on
224 error. */
225PyAPI_FUNC(PyObject *) _PyObject_FastCallDict(
226 PyObject *callable,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200227 PyObject *const *args,
Victor Stinner2a358f82016-12-06 16:55:39 +0100228 Py_ssize_t nargs,
229 PyObject *kwargs);
Victor Stinner559bb6a2016-08-22 22:48:54 +0200230
Victor Stinner2a358f82016-12-06 16:55:39 +0100231/* Call the callable object 'callable' with the "fast call" calling convention:
232 args is a C array for positional arguments followed by values of
233 keyword arguments. Keys of keyword arguments are stored as a tuple
234 of strings in kwnames. nargs is the number of positional parameters at
235 the beginning of stack. The size of kwnames gives the number of keyword
236 values in the stack after positional arguments.
Victor Stinnerd8735722016-09-09 12:36:44 -0700237
Victor Stinner2a358f82016-12-06 16:55:39 +0100238 kwnames must only contains str strings, no subclass, and all keys must
239 be unique.
Victor Stinnerd8735722016-09-09 12:36:44 -0700240
Victor Stinner2a358f82016-12-06 16:55:39 +0100241 If nargs is equal to zero and there is no keyword argument (kwnames is
242 NULL or its size is zero), args can be NULL.
Victor Stinner57f91ac2016-09-12 13:37:07 +0200243
Victor Stinner2a358f82016-12-06 16:55:39 +0100244 Return the result on success. Raise an exception and return NULL on
245 error. */
246PyAPI_FUNC(PyObject *) _PyObject_FastCallKeywords(
247 PyObject *callable,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200248 PyObject *const *args,
Victor Stinner2a358f82016-12-06 16:55:39 +0100249 Py_ssize_t nargs,
250 PyObject *kwnames);
Victor Stinnerd8735722016-09-09 12:36:44 -0700251
Victor Stinner559bb6a2016-08-22 22:48:54 +0200252#define _PyObject_FastCall(func, args, nargs) \
253 _PyObject_FastCallDict((func), (args), (nargs), NULL)
254
255#define _PyObject_CallNoArg(func) \
Victor Stinner7bfb42d2016-12-05 17:04:32 +0100256 _PyObject_FastCallDict((func), NULL, 0, NULL)
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200257
Victor Stinner2a358f82016-12-06 16:55:39 +0100258PyAPI_FUNC(PyObject *) _PyObject_Call_Prepend(
259 PyObject *callable,
260 PyObject *obj,
261 PyObject *args,
262 PyObject *kwargs);
Victor Stinner3f1057a2016-08-25 01:04:14 +0200263
Victor Stinner516b9812017-02-09 22:53:47 +0100264PyAPI_FUNC(PyObject *) _PyObject_FastCall_Prepend(
265 PyObject *callable,
266 PyObject *obj,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200267 PyObject *const *args,
Victor Stinner516b9812017-02-09 22:53:47 +0100268 Py_ssize_t nargs);
269
Victor Stinner2a358f82016-12-06 16:55:39 +0100270PyAPI_FUNC(PyObject *) _Py_CheckFunctionResult(PyObject *callable,
271 PyObject *result,
272 const char *where);
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200273#endif /* Py_LIMITED_API */
Victor Stinner4a7cc882015-03-06 23:35:27 +0100274
Victor Stinner2d0eb652016-12-06 16:27:24 +0100275
Victor Stinner2a358f82016-12-06 16:55:39 +0100276/* Call a callable Python object 'callable', with arguments given by the
277 tuple 'args'. If no arguments are needed, then 'args' can be *NULL*.
Victor Stinner2d0eb652016-12-06 16:27:24 +0100278
Victor Stinner2a358f82016-12-06 16:55:39 +0100279 Returns the result of the call on success, or *NULL* on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000280
Victor Stinner2a358f82016-12-06 16:55:39 +0100281 This is the equivalent of the Python expression:
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100282 callable(*args). */
Victor Stinner2a358f82016-12-06 16:55:39 +0100283PyAPI_FUNC(PyObject *) PyObject_CallObject(PyObject *callable,
284 PyObject *args);
Guido van Rossuma8275371995-07-18 14:07:00 +0000285
Victor Stinner2a358f82016-12-06 16:55:39 +0100286/* Call a callable Python object, callable, with a variable number of C
287 arguments. The C arguments are described using a mkvalue-style format
288 string.
Victor Stinner2d0eb652016-12-06 16:27:24 +0100289
Victor Stinner2a358f82016-12-06 16:55:39 +0100290 The format may be NULL, indicating that no arguments are provided.
Victor Stinner2d0eb652016-12-06 16:27:24 +0100291
Victor Stinner2a358f82016-12-06 16:55:39 +0100292 Returns the result of the call on success, or NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000293
Victor Stinner2a358f82016-12-06 16:55:39 +0100294 This is the equivalent of the Python expression:
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100295 callable(arg1, arg2, ...). */
Victor Stinner2a358f82016-12-06 16:55:39 +0100296PyAPI_FUNC(PyObject *) PyObject_CallFunction(PyObject *callable,
297 const char *format, ...);
Guido van Rossuma8275371995-07-18 14:07:00 +0000298
Victor Stinner2a358f82016-12-06 16:55:39 +0100299/* Call the method named 'name' of object 'obj' with a variable number of
300 C arguments. The C arguments are described by a mkvalue format string.
Guido van Rossuma8275371995-07-18 14:07:00 +0000301
Victor Stinner2a358f82016-12-06 16:55:39 +0100302 The format can be NULL, indicating that no arguments are provided.
Victor Stinner2d0eb652016-12-06 16:27:24 +0100303
Victor Stinner2a358f82016-12-06 16:55:39 +0100304 Returns the result of the call on success, or NULL on failure.
305
306 This is the equivalent of the Python expression:
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100307 obj.name(arg1, arg2, ...). */
Victor Stinner2a358f82016-12-06 16:55:39 +0100308PyAPI_FUNC(PyObject *) PyObject_CallMethod(PyObject *obj,
309 const char *name,
310 const char *format, ...);
Guido van Rossuma8275371995-07-18 14:07:00 +0000311
Serhiy Storchaka9fab79b2016-09-11 11:03:14 +0300312#ifndef Py_LIMITED_API
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100313/* Like PyObject_CallMethod(), but expect a _Py_Identifier*
314 as the method name. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100315PyAPI_FUNC(PyObject *) _PyObject_CallMethodId(PyObject *obj,
316 _Py_Identifier *name,
317 const char *format, ...);
Serhiy Storchaka9fab79b2016-09-11 11:03:14 +0300318#endif /* !Py_LIMITED_API */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200319
Victor Stinner2a358f82016-12-06 16:55:39 +0100320PyAPI_FUNC(PyObject *) _PyObject_CallFunction_SizeT(PyObject *callable,
321 const char *format,
322 ...);
323
324PyAPI_FUNC(PyObject *) _PyObject_CallMethod_SizeT(PyObject *obj,
325 const char *name,
326 const char *format,
327 ...);
328
Serhiy Storchaka9fab79b2016-09-11 11:03:14 +0300329#ifndef Py_LIMITED_API
Victor Stinner2a358f82016-12-06 16:55:39 +0100330PyAPI_FUNC(PyObject *) _PyObject_CallMethodId_SizeT(PyObject *obj,
331 _Py_Identifier *name,
332 const char *format,
333 ...);
Serhiy Storchaka9fab79b2016-09-11 11:03:14 +0300334#endif /* !Py_LIMITED_API */
Guido van Rossuma8275371995-07-18 14:07:00 +0000335
Victor Stinner2a358f82016-12-06 16:55:39 +0100336/* Call a callable Python object 'callable' with a variable number of C
337 arguments. The C arguments are provided as PyObject* values, terminated
338 by a NULL.
Victor Stinner2d0eb652016-12-06 16:27:24 +0100339
Victor Stinner2a358f82016-12-06 16:55:39 +0100340 Returns the result of the call on success, or NULL on failure.
Victor Stinner2d0eb652016-12-06 16:27:24 +0100341
Victor Stinner2a358f82016-12-06 16:55:39 +0100342 This is the equivalent of the Python expression:
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100343 callable(arg1, arg2, ...). */
Victor Stinner2a358f82016-12-06 16:55:39 +0100344PyAPI_FUNC(PyObject *) PyObject_CallFunctionObjArgs(PyObject *callable,
345 ...);
Fred Drakeb421b8c2001-10-26 16:21:32 +0000346
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100347/* Call the method named 'name' of object 'obj' with a variable number of
348 C arguments. The C arguments are provided as PyObject* values, terminated
349 by NULL.
350
351 Returns the result of the call on success, or NULL on failure.
352
353 This is the equivalent of the Python expression: obj.name(*args). */
Guido van Rossuma8275371995-07-18 14:07:00 +0000354
Victor Stinner2a358f82016-12-06 16:55:39 +0100355PyAPI_FUNC(PyObject *) PyObject_CallMethodObjArgs(
356 PyObject *obj,
357 PyObject *name,
358 ...);
359
Victor Stinner2d0eb652016-12-06 16:27:24 +0100360#ifndef Py_LIMITED_API
Victor Stinner2a358f82016-12-06 16:55:39 +0100361PyAPI_FUNC(PyObject *) _PyObject_CallMethodIdObjArgs(
362 PyObject *obj,
363 struct _Py_Identifier *name,
364 ...);
Victor Stinner2d0eb652016-12-06 16:27:24 +0100365#endif /* !Py_LIMITED_API */
366
367
Victor Stinner2a358f82016-12-06 16:55:39 +0100368/* Implemented elsewhere:
Guido van Rossuma8275371995-07-18 14:07:00 +0000369
Andrew Svetlov95084022017-12-16 21:08:05 +0200370 Py_hash_t PyObject_Hash(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000371
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100372 Compute and return the hash, hash_value, of an object, o. On
373 failure, return -1.
374
375 This is the equivalent of the Python expression: hash(o). */
Guido van Rossuma8275371995-07-18 14:07:00 +0000376
377
Victor Stinner2a358f82016-12-06 16:55:39 +0100378/* Implemented elsewhere:
Guido van Rossuma8275371995-07-18 14:07:00 +0000379
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100380 int PyObject_IsTrue(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000381
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100382 Returns 1 if the object, o, is considered to be true, 0 if o is
383 considered to be false and -1 on failure.
384
385 This is equivalent to the Python expression: not not o. */
386
Guido van Rossuma8275371995-07-18 14:07:00 +0000387
Victor Stinner2a358f82016-12-06 16:55:39 +0100388/* Implemented elsewhere:
Guido van Rossumc3d3f961998-04-09 17:53:59 +0000389
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100390 int PyObject_Not(PyObject *o);
Guido van Rossumc3d3f961998-04-09 17:53:59 +0000391
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100392 Returns 0 if the object, o, is considered to be true, 1 if o is
393 considered to be false and -1 on failure.
Guido van Rossumc3d3f961998-04-09 17:53:59 +0000394
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100395 This is equivalent to the Python expression: not o. */
396
397
398/* Get the type of an object.
399
400 On success, returns a type object corresponding to the object type of object
401 'o'. On failure, returns NULL.
402
403 This is equivalent to the Python expression: type(o) */
Victor Stinner2a358f82016-12-06 16:55:39 +0100404PyAPI_FUNC(PyObject *) PyObject_Type(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000405
Guido van Rossuma8275371995-07-18 14:07:00 +0000406
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100407/* Return the size of object 'o'. If the object 'o' provides both sequence and
408 mapping protocols, the sequence size is returned.
409
410 On error, -1 is returned.
411
412 This is the equivalent to the Python expression: len(o) */
Victor Stinner2a358f82016-12-06 16:55:39 +0100413PyAPI_FUNC(Py_ssize_t) PyObject_Size(PyObject *o);
Jeremy Hylton6253f832000-07-12 12:56:19 +0000414
Guido van Rossuma8275371995-07-18 14:07:00 +0000415
Victor Stinner2a358f82016-12-06 16:55:39 +0100416/* For DLL compatibility */
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +0000417#undef PyObject_Length
Victor Stinner2a358f82016-12-06 16:55:39 +0100418PyAPI_FUNC(Py_ssize_t) PyObject_Length(PyObject *o);
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +0000419#define PyObject_Length PyObject_Size
420
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100421
Armin Ronacher74b38b12012-10-07 10:29:32 +0200422#ifndef Py_LIMITED_API
Victor Stinner2a358f82016-12-06 16:55:39 +0100423PyAPI_FUNC(int) _PyObject_HasLen(PyObject *o);
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100424
425/* Guess the size of object 'o' using len(o) or o.__length_hint__().
426 If neither of those return a non-negative value, then return the default
427 value. If one of the calls fails, this function returns -1. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100428PyAPI_FUNC(Py_ssize_t) PyObject_LengthHint(PyObject *o, Py_ssize_t);
Armin Ronacher74b38b12012-10-07 10:29:32 +0200429#endif
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000430
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100431/* Return element of 'o' corresponding to the object 'key'. Return NULL
432 on failure.
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +0000433
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100434 This is the equivalent of the Python expression: o[key] */
Victor Stinner2a358f82016-12-06 16:55:39 +0100435PyAPI_FUNC(PyObject *) PyObject_GetItem(PyObject *o, PyObject *key);
Guido van Rossuma8275371995-07-18 14:07:00 +0000436
Guido van Rossuma8275371995-07-18 14:07:00 +0000437
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100438/* Map the object 'key' to the value 'v' into 'o'.
439
440 Raise an exception and return -1 on failure; return 0 on success.
441
442 This is the equivalent of the Python statement: o[key]=v. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100443PyAPI_FUNC(int) PyObject_SetItem(PyObject *o, PyObject *key, PyObject *v);
Guido van Rossuma8275371995-07-18 14:07:00 +0000444
Serhiy Storchakaf5b11832018-05-22 11:02:44 +0300445/* Remove the mapping for the string 'key' from the object 'o'.
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100446 Returns -1 on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000447
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100448 This is equivalent to the Python statement: del o[key]. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100449PyAPI_FUNC(int) PyObject_DelItemString(PyObject *o, const char *key);
Martin v. Löwisb0d71d02002-01-05 10:50:30 +0000450
Serhiy Storchakaf5b11832018-05-22 11:02:44 +0300451/* Delete the mapping for the object 'key' from the object 'o'.
452 Returns -1 on failure.
Martin v. Löwisb0d71d02002-01-05 10:50:30 +0000453
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100454 This is the equivalent of the Python statement: del o[key]. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100455PyAPI_FUNC(int) PyObject_DelItem(PyObject *o, PyObject *key);
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000456
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000457
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100458/* === Old Buffer API ============================================ */
459
460/* FIXME: usage of these should all be replaced in Python itself
Victor Stinner2a358f82016-12-06 16:55:39 +0100461 but for backwards compatibility we will implement them.
462 Their usage without a corresponding "unlock" mechanism
463 may create issues (but they would already be there). */
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000464
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100465/* Takes an arbitrary object which must support the (character, single segment)
466 buffer interface and returns a pointer to a read-only memory location
467 useable as character based input for subsequent processing.
468
469 Return 0 on success. buffer and buffer_len are only set in case no error
470 occurs. Otherwise, -1 is returned and an exception set. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100471PyAPI_FUNC(int) PyObject_AsCharBuffer(PyObject *obj,
472 const char **buffer,
473 Py_ssize_t *buffer_len)
474 Py_DEPRECATED(3.0);
Guido van Rossumfd9eed32000-03-10 22:35:06 +0000475
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100476/* Checks whether an arbitrary object supports the (character, single segment)
477 buffer interface.
Guido van Rossumfd9eed32000-03-10 22:35:06 +0000478
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100479 Returns 1 on success, 0 on failure. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100480PyAPI_FUNC(int) PyObject_CheckReadBuffer(PyObject *obj)
481 Py_DEPRECATED(3.0);
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000482
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100483/* Same as PyObject_AsCharBuffer() except that this API expects (readable,
484 single segment) buffer interface and returns a pointer to a read-only memory
485 location which can contain arbitrary data.
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000486
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100487 0 is returned on success. buffer and buffer_len are only set in case no
488 error occurs. Otherwise, -1 is returned and an exception set. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100489PyAPI_FUNC(int) PyObject_AsReadBuffer(PyObject *obj,
490 const void **buffer,
491 Py_ssize_t *buffer_len)
492 Py_DEPRECATED(3.0);
Guido van Rossumfd9eed32000-03-10 22:35:06 +0000493
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100494/* Takes an arbitrary object which must support the (writable, single segment)
495 buffer interface and returns a pointer to a writable memory location in
496 buffer of size 'buffer_len'.
Guido van Rossumfd9eed32000-03-10 22:35:06 +0000497
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100498 Return 0 on success. buffer and buffer_len are only set in case no error
499 occurs. Otherwise, -1 is returned and an exception set. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100500PyAPI_FUNC(int) PyObject_AsWriteBuffer(PyObject *obj,
501 void **buffer,
502 Py_ssize_t *buffer_len)
503 Py_DEPRECATED(3.0);
Guido van Rossumfd9eed32000-03-10 22:35:06 +0000504
Guido van Rossumfd9eed32000-03-10 22:35:06 +0000505
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100506/* === New Buffer API ============================================ */
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000507
Martin v. Löwisc83bc3c2011-01-06 19:15:47 +0000508#ifndef Py_LIMITED_API
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100509
510/* Return 1 if the getbuffer function is available, otherwise return 0. */
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000511#define PyObject_CheckBuffer(obj) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 (((obj)->ob_type->tp_as_buffer != NULL) && \
513 ((obj)->ob_type->tp_as_buffer->bf_getbuffer != NULL))
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000514
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100515/* This is a C-API version of the getbuffer function call. It checks
516 to make sure object has the required function pointer and issues the
517 call.
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000518
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100519 Returns -1 and raises an error on failure and returns 0 on success. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100520PyAPI_FUNC(int) PyObject_GetBuffer(PyObject *obj, Py_buffer *view,
521 int flags);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000522
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100523/* Get the memory area pointed to by the indices for the buffer given.
524 Note that view->ndim is the assumed size of indices. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100525PyAPI_FUNC(void *) PyBuffer_GetPointer(Py_buffer *view, Py_ssize_t *indices);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000526
Victor Stinner2a358f82016-12-06 16:55:39 +0100527/* Return the implied itemsize of the data-format area from a
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100528 struct-style description. */
529PyAPI_FUNC(int) PyBuffer_SizeFromFormat(const char *);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530
Victor Stinner2a358f82016-12-06 16:55:39 +0100531/* Implementation in memoryobject.c */
532PyAPI_FUNC(int) PyBuffer_ToContiguous(void *buf, Py_buffer *view,
533 Py_ssize_t len, char order);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000534
Victor Stinner2a358f82016-12-06 16:55:39 +0100535PyAPI_FUNC(int) PyBuffer_FromContiguous(Py_buffer *view, void *buf,
536 Py_ssize_t len, char order);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000537
Victor Stinner2a358f82016-12-06 16:55:39 +0100538/* Copy len bytes of data from the contiguous chunk of memory
539 pointed to by buf into the buffer exported by obj. Return
540 0 on success and return -1 and raise a PyBuffer_Error on
541 error (i.e. the object does not have a buffer interface or
542 it is not working).
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000543
Victor Stinner2a358f82016-12-06 16:55:39 +0100544 If fort is 'F', then if the object is multi-dimensional,
545 then the data will be copied into the array in
546 Fortran-style (first dimension varies the fastest). If
547 fort is 'C', then the data will be copied into the array
548 in C-style (last dimension varies the fastest). If fort
549 is 'A', then it does not matter and the copy will be made
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100550 in whatever way is more efficient. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100551PyAPI_FUNC(int) PyObject_CopyData(PyObject *dest, PyObject *src);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100553/* Copy the data from the src buffer to the buffer of destination. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100554PyAPI_FUNC(int) PyBuffer_IsContiguous(const Py_buffer *view, char fort);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000555
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100556/*Fill the strides array with byte-strides of a contiguous
557 (Fortran-style if fort is 'F' or C-style otherwise)
558 array of the given shape with the given number of bytes
559 per element. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100560PyAPI_FUNC(void) PyBuffer_FillContiguousStrides(int ndims,
561 Py_ssize_t *shape,
562 Py_ssize_t *strides,
563 int itemsize,
564 char fort);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000565
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100566/* Fills in a buffer-info structure correctly for an exporter
567 that can only share a contiguous chunk of memory of
568 "unsigned bytes" of the given length.
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000569
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100570 Returns 0 on success and -1 (with raising an error) on error. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100571PyAPI_FUNC(int) PyBuffer_FillInfo(Py_buffer *view, PyObject *o, void *buf,
572 Py_ssize_t len, int readonly,
573 int flags);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000574
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100575/* Releases a Py_buffer obtained from getbuffer ParseTuple's "s*". */
Victor Stinner2a358f82016-12-06 16:55:39 +0100576PyAPI_FUNC(void) PyBuffer_Release(Py_buffer *view);
Martin v. Löwis423be952008-08-13 15:53:07 +0000577
Martin v. Löwisc83bc3c2011-01-06 19:15:47 +0000578#endif /* Py_LIMITED_API */
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000579
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100580/* Takes an arbitrary object and returns the result of calling
581 obj.__format__(format_spec). */
582PyAPI_FUNC(PyObject *) PyObject_Format(PyObject *obj,
Victor Stinner2a358f82016-12-06 16:55:39 +0100583 PyObject *format_spec);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000584
Guido van Rossum213c7a62001-04-23 14:08:49 +0000585
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100586/* ==== Iterators ================================================ */
587
588/* Takes an object and returns an iterator for it.
589 This is typically a new iterator but if the argument is an iterator, this
590 returns itself. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100591PyAPI_FUNC(PyObject *) PyObject_GetIter(PyObject *);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000592
Christian Tismerea62ce72018-06-09 20:32:25 +0200593/* Returns 1 if the object 'obj' provides iterator protocols, and 0 otherwise.
594
595 This function always succeeds. */
Serhiy Storchaka5cbefa92018-06-11 15:01:47 +0300596PyAPI_FUNC(int) PyIter_Check(PyObject *);
Christian Tismerea62ce72018-06-09 20:32:25 +0200597#ifndef Py_LIMITED_API
Guido van Rossum213c7a62001-04-23 14:08:49 +0000598#define PyIter_Check(obj) \
Amaury Forgeot d'Arcf343e012009-01-12 23:58:21 +0000599 ((obj)->ob_type->tp_iternext != NULL && \
600 (obj)->ob_type->tp_iternext != &_PyObject_NextNotImplemented)
Christian Tismerea62ce72018-06-09 20:32:25 +0200601#endif
Guido van Rossum213c7a62001-04-23 14:08:49 +0000602
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100603/* Takes an iterator object and calls its tp_iternext slot,
604 returning the next value.
605
606 If the iterator is exhausted, this returns NULL without setting an
607 exception.
608
609 NULL with an exception means an error occurred. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100610PyAPI_FUNC(PyObject *) PyIter_Next(PyObject *);
Guido van Rossum213c7a62001-04-23 14:08:49 +0000611
Guido van Rossuma8275371995-07-18 14:07:00 +0000612
Victor Stinner2a358f82016-12-06 16:55:39 +0100613/* === Number Protocol ================================================== */
Guido van Rossuma8275371995-07-18 14:07:00 +0000614
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100615/* Returns 1 if the object 'o' provides numeric protocols, and 0 otherwise.
616
617 This function always succeeds. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100618PyAPI_FUNC(int) PyNumber_Check(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000619
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100620/* Returns the result of adding o1 and o2, or NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000621
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100622 This is the equivalent of the Python expression: o1 + o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100623PyAPI_FUNC(PyObject *) PyNumber_Add(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000624
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100625/* Returns the result of subtracting o2 from o1, or NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000626
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100627 This is the equivalent of the Python expression: o1 - o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100628PyAPI_FUNC(PyObject *) PyNumber_Subtract(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000629
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100630/* Returns the result of multiplying o1 and o2, or NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000631
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100632 This is the equivalent of the Python expression: o1 * o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100633PyAPI_FUNC(PyObject *) PyNumber_Multiply(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000634
Serhiy Storchaka34d0ac82016-12-27 14:57:39 +0200635#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100636/* This is the equivalent of the Python expression: o1 @ o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100637PyAPI_FUNC(PyObject *) PyNumber_MatrixMultiply(PyObject *o1, PyObject *o2);
Serhiy Storchaka34d0ac82016-12-27 14:57:39 +0200638#endif
Benjamin Petersond51374e2014-04-09 23:55:56 -0400639
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100640/* Returns the result of dividing o1 by o2 giving an integral result,
641 or NULL on failure.
Guido van Rossum4668b002001-08-08 05:00:18 +0000642
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100643 This is the equivalent of the Python expression: o1 // o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100644PyAPI_FUNC(PyObject *) PyNumber_FloorDivide(PyObject *o1, PyObject *o2);
Guido van Rossum4668b002001-08-08 05:00:18 +0000645
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100646/* Returns the result of dividing o1 by o2 giving a float result, or NULL on
647 failure.
Guido van Rossum4668b002001-08-08 05:00:18 +0000648
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100649 This is the equivalent of the Python expression: o1 / o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100650PyAPI_FUNC(PyObject *) PyNumber_TrueDivide(PyObject *o1, PyObject *o2);
Guido van Rossum4668b002001-08-08 05:00:18 +0000651
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100652/* Returns the remainder of dividing o1 by o2, or NULL 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 expression: o1 % o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100655PyAPI_FUNC(PyObject *) PyNumber_Remainder(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000656
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100657/* See the built-in function divmod.
Guido van Rossuma8275371995-07-18 14:07:00 +0000658
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100659 Returns NULL on failure.
660
661 This is the equivalent of the Python expression: divmod(o1, o2). */
Victor Stinner2a358f82016-12-06 16:55:39 +0100662PyAPI_FUNC(PyObject *) PyNumber_Divmod(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000663
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100664/* See the built-in function pow. Returns NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000665
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100666 This is the equivalent of the Python expression: pow(o1, o2, o3),
667 where o3 is optional. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100668PyAPI_FUNC(PyObject *) PyNumber_Power(PyObject *o1, PyObject *o2,
669 PyObject *o3);
Guido van Rossuma8275371995-07-18 14:07:00 +0000670
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100671/* Returns the negation of o on success, or NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000672
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100673 This is the equivalent of the Python expression: -o. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100674PyAPI_FUNC(PyObject *) PyNumber_Negative(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000675
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100676/* Returns the positive of o on success, or NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000677
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100678 This is the equivalent of the Python expression: +o. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100679PyAPI_FUNC(PyObject *) PyNumber_Positive(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000680
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100681/* Returns the absolute value of 'o', or NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000682
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100683 This is the equivalent of the Python expression: abs(o). */
Victor Stinner2a358f82016-12-06 16:55:39 +0100684PyAPI_FUNC(PyObject *) PyNumber_Absolute(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000685
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100686/* Returns the bitwise negation of 'o' on success, or NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000687
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100688 This is the equivalent of the Python expression: ~o. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100689PyAPI_FUNC(PyObject *) PyNumber_Invert(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000690
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100691/* Returns the result of left shifting o1 by o2 on success, or NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000692
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100693 This is the equivalent of the Python expression: o1 << o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100694PyAPI_FUNC(PyObject *) PyNumber_Lshift(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000695
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100696/* Returns the result of right shifting o1 by o2 on success, or NULL on
697 failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000698
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100699 This is the equivalent of the Python expression: o1 >> o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100700PyAPI_FUNC(PyObject *) PyNumber_Rshift(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000701
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100702/* Returns the result of bitwise and of o1 and o2 on success, or NULL on
703 failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000704
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100705 This is the equivalent of the Python expression: o1 & o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100706PyAPI_FUNC(PyObject *) PyNumber_And(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000707
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100708/* Returns the bitwise exclusive or of o1 by o2 on success, or NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000709
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100710 This is the equivalent of the Python expression: o1 ^ o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100711PyAPI_FUNC(PyObject *) PyNumber_Xor(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000712
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100713/* Returns the result of bitwise or on o1 and o2 on success, or NULL on
714 failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000715
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100716 This is the equivalent of the Python expression: o1 | o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100717PyAPI_FUNC(PyObject *) PyNumber_Or(PyObject *o1, PyObject *o2);
718
Christian Tismerea62ce72018-06-09 20:32:25 +0200719/* Returns 1 if obj is an index integer (has the nb_index slot of the
720 tp_as_number structure filled in), and 0 otherwise. */
Serhiy Storchaka5cbefa92018-06-11 15:01:47 +0300721PyAPI_FUNC(int) PyIndex_Check(PyObject *);
Christian Tismerea62ce72018-06-09 20:32:25 +0200722#ifndef Py_LIMITED_API
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100723#define PyIndex_Check(obj) \
724 ((obj)->ob_type->tp_as_number != NULL && \
725 (obj)->ob_type->tp_as_number->nb_index != NULL)
Christian Tismerea62ce72018-06-09 20:32:25 +0200726#endif
Guido van Rossuma8275371995-07-18 14:07:00 +0000727
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100728/* Returns the object 'o' converted to a Python int, or NULL with an exception
729 raised on failure. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100730PyAPI_FUNC(PyObject *) PyNumber_Index(PyObject *o);
Guido van Rossum38fff8c2006-03-07 18:50:55 +0000731
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100732/* Returns the object 'o' converted to Py_ssize_t by going through
733 PyNumber_Index() first.
Guido van Rossum38fff8c2006-03-07 18:50:55 +0000734
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100735 If an overflow error occurs while converting the int to Py_ssize_t, then the
736 second argument 'exc' is the error-type to return. If it is NULL, then the
737 overflow error is cleared and the value is clipped. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100738PyAPI_FUNC(Py_ssize_t) PyNumber_AsSsize_t(PyObject *o, PyObject *exc);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000739
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100740/* Returns the object 'o' converted to an integer object on success, or NULL
741 on failure.
Guido van Rossum38fff8c2006-03-07 18:50:55 +0000742
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100743 This is the equivalent of the Python expression: int(o). */
Victor Stinner2a358f82016-12-06 16:55:39 +0100744PyAPI_FUNC(PyObject *) PyNumber_Long(PyObject *o);
Mark Dickinsond7467682009-01-10 22:14:33 +0000745
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100746/* Returns the object 'o' converted to a float object on success, or NULL
747 on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000748
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100749 This is the equivalent of the Python expression: float(o). */
Victor Stinner2a358f82016-12-06 16:55:39 +0100750PyAPI_FUNC(PyObject *) PyNumber_Float(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000751
Victor Stinner2a358f82016-12-06 16:55:39 +0100752
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100753/* --- In-place variants of (some of) the above number protocol functions -- */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100755/* Returns the result of adding o2 to o1, possibly in-place, or NULL
756 on failure.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000757
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100758 This is the equivalent of the Python expression: o1 += o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100759PyAPI_FUNC(PyObject *) PyNumber_InPlaceAdd(PyObject *o1, PyObject *o2);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000760
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100761/* Returns the result of subtracting o2 from o1, possibly in-place or
762 NULL on failure.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000763
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100764 This is the equivalent of the Python expression: o1 -= o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100765PyAPI_FUNC(PyObject *) PyNumber_InPlaceSubtract(PyObject *o1, PyObject *o2);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000766
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100767/* Returns the result of multiplying o1 by o2, possibly in-place, or NULL on
768 failure.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000769
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100770 This is the equivalent of the Python expression: o1 *= o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100771PyAPI_FUNC(PyObject *) PyNumber_InPlaceMultiply(PyObject *o1, PyObject *o2);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000772
Serhiy Storchaka34d0ac82016-12-27 14:57:39 +0200773#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100774/* This is the equivalent of the Python expression: o1 @= o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100775PyAPI_FUNC(PyObject *) PyNumber_InPlaceMatrixMultiply(PyObject *o1, PyObject *o2);
Serhiy Storchaka34d0ac82016-12-27 14:57:39 +0200776#endif
Benjamin Petersond51374e2014-04-09 23:55:56 -0400777
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100778/* Returns the result of dividing o1 by o2 giving an integral result, possibly
779 in-place, or NULL on failure.
Benjamin Petersond51374e2014-04-09 23:55:56 -0400780
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100781 This is the equivalent of the Python expression: o1 /= o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100782PyAPI_FUNC(PyObject *) PyNumber_InPlaceFloorDivide(PyObject *o1,
783 PyObject *o2);
Guido van Rossum4668b002001-08-08 05:00:18 +0000784
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100785/* Returns the result of dividing o1 by o2 giving a float result, possibly
786 in-place, or null on failure.
Guido van Rossum4668b002001-08-08 05:00:18 +0000787
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100788 This is the equivalent of the Python expression: o1 /= o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100789PyAPI_FUNC(PyObject *) PyNumber_InPlaceTrueDivide(PyObject *o1,
790 PyObject *o2);
Guido van Rossum4668b002001-08-08 05:00:18 +0000791
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100792/* Returns the remainder of dividing o1 by o2, possibly in-place, or NULL on
793 failure.
Guido van Rossum4668b002001-08-08 05:00:18 +0000794
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100795 This is the equivalent of the Python expression: o1 %= o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100796PyAPI_FUNC(PyObject *) PyNumber_InPlaceRemainder(PyObject *o1, PyObject *o2);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000797
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100798/* Returns the result of raising o1 to the power of o2, possibly in-place,
799 or NULL on failure.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000800
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100801 This is the equivalent of the Python expression: o1 **= o2,
802 or o1 = pow(o1, o2, o3) if o3 is present. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100803PyAPI_FUNC(PyObject *) PyNumber_InPlacePower(PyObject *o1, PyObject *o2,
804 PyObject *o3);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000805
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100806/* Returns the result of left shifting o1 by o2, possibly in-place, or NULL
807 on failure.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000808
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100809 This is the equivalent of the Python expression: o1 <<= o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100810PyAPI_FUNC(PyObject *) PyNumber_InPlaceLshift(PyObject *o1, PyObject *o2);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000811
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100812/* Returns the result of right shifting o1 by o2, possibly in-place or NULL
813 on failure.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000814
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100815 This is the equivalent of the Python expression: o1 >>= o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100816PyAPI_FUNC(PyObject *) PyNumber_InPlaceRshift(PyObject *o1, PyObject *o2);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000817
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100818/* Returns the result of bitwise and of o1 and o2, possibly in-place, or NULL
819 on failure.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000820
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100821 This is the equivalent of the Python expression: o1 &= o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100822PyAPI_FUNC(PyObject *) PyNumber_InPlaceAnd(PyObject *o1, PyObject *o2);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000823
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100824/* Returns the bitwise exclusive or of o1 by o2, possibly in-place, or NULL
825 on failure.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000826
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100827 This is the equivalent of the Python expression: o1 ^= o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100828PyAPI_FUNC(PyObject *) PyNumber_InPlaceXor(PyObject *o1, PyObject *o2);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000829
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100830/* Returns the result of bitwise or of o1 and o2, possibly in-place,
831 or NULL on failure.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000832
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100833 This is the equivalent of the Python expression: o1 |= o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100834PyAPI_FUNC(PyObject *) PyNumber_InPlaceOr(PyObject *o1, PyObject *o2);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000835
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100836/* Returns the integer n converted to a string with a base, with a base
837 marker of 0b, 0o or 0x prefixed if applicable.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000838
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100839 If n is not an int object, it is converted with PyNumber_Index first. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100840PyAPI_FUNC(PyObject *) PyNumber_ToBase(PyObject *n, int base);
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000841
Guido van Rossuma8275371995-07-18 14:07:00 +0000842
Victor Stinner2a358f82016-12-06 16:55:39 +0100843/* === Sequence protocol ================================================ */
Guido van Rossuma8275371995-07-18 14:07:00 +0000844
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100845/* Return 1 if the object provides sequence protocol, and zero
846 otherwise.
847
848 This function always succeeds. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100849PyAPI_FUNC(int) PySequence_Check(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000850
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100851/* Return the size of sequence object o, or -1 on failure. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100852PyAPI_FUNC(Py_ssize_t) PySequence_Size(PyObject *o);
Jeremy Hylton6253f832000-07-12 12:56:19 +0000853
Victor Stinner2a358f82016-12-06 16:55:39 +0100854/* For DLL compatibility */
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +0000855#undef PySequence_Length
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100856PyAPI_FUNC(Py_ssize_t) PySequence_Length(PyObject *o);
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +0000857#define PySequence_Length PySequence_Size
858
859
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100860/* Return the concatenation of o1 and o2 on success, and NULL on failure.
861
862 This is the equivalent of the Python expression: o1 + o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100863PyAPI_FUNC(PyObject *) PySequence_Concat(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000864
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100865/* Return the result of repeating sequence object 'o' 'count' times,
866 or NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000867
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100868 This is the equivalent of the Python expression: o * count. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100869PyAPI_FUNC(PyObject *) PySequence_Repeat(PyObject *o, Py_ssize_t count);
Guido van Rossuma8275371995-07-18 14:07:00 +0000870
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100871/* Return the ith element of o, or NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000872
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100873 This is the equivalent of the Python expression: o[i]. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100874PyAPI_FUNC(PyObject *) PySequence_GetItem(PyObject *o, Py_ssize_t i);
Guido van Rossuma8275371995-07-18 14:07:00 +0000875
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100876/* Return the slice of sequence object o between i1 and i2, or NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000877
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100878 This is the equivalent of the Python expression: o[i1:i2]. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100879PyAPI_FUNC(PyObject *) PySequence_GetSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000880
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100881/* Assign object 'v' to the ith element of the sequence 'o'. Raise an exception
882 and return -1 on failure; return 0 on success.
Guido van Rossuma8275371995-07-18 14:07:00 +0000883
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100884 This is the equivalent of the Python statement o[i] = v. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100885PyAPI_FUNC(int) PySequence_SetItem(PyObject *o, Py_ssize_t i, PyObject *v);
Guido van Rossuma8275371995-07-18 14:07:00 +0000886
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100887/* Delete the 'i'-th element of the sequence 'v'. Returns -1 on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000888
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100889 This is the equivalent of the Python statement: del o[i]. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100890PyAPI_FUNC(int) PySequence_DelItem(PyObject *o, Py_ssize_t i);
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000891
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100892/* Assign the sequence object 'v' to the slice in sequence object 'o',
893 from 'i1' to 'i2'. Returns -1 on failure.
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000894
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100895 This is the equivalent of the Python statement: o[i1:i2] = v. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100896PyAPI_FUNC(int) PySequence_SetSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2,
897 PyObject *v);
Guido van Rossuma8275371995-07-18 14:07:00 +0000898
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100899/* Delete the slice in sequence object 'o' from 'i1' to 'i2'.
900 Returns -1 on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000901
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100902 This is the equivalent of the Python statement: del o[i1:i2]. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100903PyAPI_FUNC(int) PySequence_DelSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2);
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000904
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100905/* Returns the sequence 'o' as a tuple on success, and NULL on failure.
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000906
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100907 This is equivalent to the Python expression: tuple(o). */
Victor Stinner2a358f82016-12-06 16:55:39 +0100908PyAPI_FUNC(PyObject *) PySequence_Tuple(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000909
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100910/* Returns the sequence 'o' as a list on success, and NULL on failure.
911 This is equivalent to the Python expression: list(o) */
Victor Stinner2a358f82016-12-06 16:55:39 +0100912PyAPI_FUNC(PyObject *) PySequence_List(PyObject *o);
Guido van Rossumf39fc431997-03-04 18:31:47 +0000913
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100914/* Return the sequence 'o' as a list, unless it's already a tuple or list.
915
916 Use PySequence_Fast_GET_ITEM to access the members of this list, and
917 PySequence_Fast_GET_SIZE to get its length.
918
919 Returns NULL on failure. If the object does not support iteration, raises a
920 TypeError exception with 'm' as the message text. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100921PyAPI_FUNC(PyObject *) PySequence_Fast(PyObject *o, const char* m);
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000922
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100923/* Return the size of the sequence 'o', assuming that 'o' was returned by
924 PySequence_Fast and is not NULL. */
Tim Peters1fc240e2001-10-26 05:06:50 +0000925#define PySequence_Fast_GET_SIZE(o) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 (PyList_Check(o) ? PyList_GET_SIZE(o) : PyTuple_GET_SIZE(o))
Tim Peters1fc240e2001-10-26 05:06:50 +0000927
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100928/* Return the 'i'-th element of the sequence 'o', assuming that o was returned
929 by PySequence_Fast, and that i is within bounds. */
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000930#define PySequence_Fast_GET_ITEM(o, i)\
931 (PyList_Check(o) ? PyList_GET_ITEM(o, i) : PyTuple_GET_ITEM(o, i))
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000932
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100933/* Assume tp_as_sequence and sq_item exist and that 'i' does not
934 need to be corrected for a negative index. */
Christian Tismerea62ce72018-06-09 20:32:25 +0200935#ifndef Py_LIMITED_API
Martin v. Löwis01f94bd2002-05-08 08:44:21 +0000936#define PySequence_ITEM(o, i)\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 ( Py_TYPE(o)->tp_as_sequence->sq_item(o, i) )
Christian Tismerea62ce72018-06-09 20:32:25 +0200938#endif
Martin v. Löwis01f94bd2002-05-08 08:44:21 +0000939
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100940/* Return a pointer to the underlying item array for
941 an object retured by PySequence_Fast */
Raymond Hettinger42bec932004-03-12 16:38:17 +0000942#define PySequence_Fast_ITEMS(sf) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 (PyList_Check(sf) ? ((PyListObject *)(sf))->ob_item \
944 : ((PyTupleObject *)(sf))->ob_item)
Raymond Hettingerc1e4f9d2004-03-12 08:04:00 +0000945
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100946/* Return the number of occurrences on value on 'o', that is, return
947 the number of keys for which o[key] == value.
948
949 On failure, return -1. This is equivalent to the Python expression:
950 o.count(value). */
Victor Stinner2a358f82016-12-06 16:55:39 +0100951PyAPI_FUNC(Py_ssize_t) PySequence_Count(PyObject *o, PyObject *value);
Guido van Rossuma8275371995-07-18 14:07:00 +0000952
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100953/* Return 1 if 'ob' is in the sequence 'seq'; 0 if 'ob' is not in the sequence
954 'seq'; -1 on error.
Guido van Rossuma8275371995-07-18 14:07:00 +0000955
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100956 Use __contains__ if possible, else _PySequence_IterSearch(). */
Victor Stinner2a358f82016-12-06 16:55:39 +0100957PyAPI_FUNC(int) PySequence_Contains(PyObject *seq, PyObject *ob);
Tim Peterscb8d3682001-05-05 21:05:01 +0000958
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000959#ifndef Py_LIMITED_API
Tim Peters16a77ad2001-09-08 04:00:12 +0000960#define PY_ITERSEARCH_COUNT 1
961#define PY_ITERSEARCH_INDEX 2
962#define PY_ITERSEARCH_CONTAINS 3
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100963
964/* Iterate over seq.
965
966 Result depends on the operation:
967
968 PY_ITERSEARCH_COUNT: return # of times obj appears in seq; -1 if
969 error.
970 PY_ITERSEARCH_INDEX: return 0-based index of first occurrence of
971 obj in seq; set ValueError and return -1 if none found;
972 also return -1 on error.
973 PY_ITERSEARCH_CONTAINS: return 1 if obj in seq, else 0; -1 on
974 error. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100975PyAPI_FUNC(Py_ssize_t) _PySequence_IterSearch(PyObject *seq,
976 PyObject *obj, int operation);
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000977#endif
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100978
Guido van Rossum83684531999-03-17 18:44:39 +0000979
980/* For DLL-level backwards compatibility */
981#undef PySequence_In
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100982/* Determine if the sequence 'o' contains 'value'. If an item in 'o' is equal
983 to 'value', return 1, otherwise return 0. On error, return -1.
984
985 This is equivalent to the Python expression: value in o. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100986PyAPI_FUNC(int) PySequence_In(PyObject *o, PyObject *value);
Guido van Rossum83684531999-03-17 18:44:39 +0000987
988/* For source-level backwards compatibility */
Guido van Rossumf1536db1998-08-23 22:06:59 +0000989#define PySequence_In PySequence_Contains
Guido van Rossuma8275371995-07-18 14:07:00 +0000990
Guido van Rossuma8275371995-07-18 14:07:00 +0000991
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100992/* Return the first index for which o[i] == value.
993 On error, return -1.
994
995 This is equivalent to the Python expression: o.index(value). */
Victor Stinner2a358f82016-12-06 16:55:39 +0100996PyAPI_FUNC(Py_ssize_t) PySequence_Index(PyObject *o, PyObject *value);
Guido van Rossuma8275371995-07-18 14:07:00 +0000997
Victor Stinner2a358f82016-12-06 16:55:39 +0100998
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100999/* --- In-place versions of some of the above Sequence functions --- */
Guido van Rossuma8275371995-07-18 14:07:00 +00001000
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001001/* Append sequence 'o2' to sequence 'o1', in-place when possible. Return the
1002 resulting object, which could be 'o1', or NULL on failure.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +00001003
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001004 This is the equivalent of the Python expression: o1 += o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +01001005PyAPI_FUNC(PyObject *) PySequence_InPlaceConcat(PyObject *o1, PyObject *o2);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +00001006
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001007/* Repeat sequence 'o' by 'count', in-place when possible. Return the resulting
1008 object, which could be 'o', or NULL on failure.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +00001009
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001010 This is the equivalent of the Python expression: o1 *= count. */
Victor Stinner2a358f82016-12-06 16:55:39 +01001011PyAPI_FUNC(PyObject *) PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +00001012
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +00001013
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001014/* === Mapping protocol ================================================= */
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +00001015
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001016/* Return 1 if the object provides mapping protocol, and 0 otherwise.
Guido van Rossuma8275371995-07-18 14:07:00 +00001017
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001018 This function always succeeds. */
Victor Stinner2a358f82016-12-06 16:55:39 +01001019PyAPI_FUNC(int) PyMapping_Check(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +00001020
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001021/* Returns the number of keys in mapping object 'o' on success, and -1 on
Serhiy Storchakaf5b11832018-05-22 11:02:44 +03001022 failure. This is equivalent to the Python expression: len(o). */
Victor Stinner2a358f82016-12-06 16:55:39 +01001023PyAPI_FUNC(Py_ssize_t) PyMapping_Size(PyObject *o);
Jeremy Hylton6253f832000-07-12 12:56:19 +00001024
Victor Stinner2a358f82016-12-06 16:55:39 +01001025/* For DLL compatibility */
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00001026#undef PyMapping_Length
Victor Stinner2a358f82016-12-06 16:55:39 +01001027PyAPI_FUNC(Py_ssize_t) PyMapping_Length(PyObject *o);
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00001028#define PyMapping_Length PyMapping_Size
1029
1030
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001031/* Implemented as a macro:
Guido van Rossuma25e5e91996-09-06 13:48:38 +00001032
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001033 int PyMapping_DelItemString(PyObject *o, const char *key);
Guido van Rossuma8275371995-07-18 14:07:00 +00001034
Serhiy Storchakaf5b11832018-05-22 11:02:44 +03001035 Remove the mapping for the string 'key' from the mapping 'o'. Returns -1 on
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001036 failure.
1037
1038 This is equivalent to the Python statement: del o[key]. */
Jeremy Hylton7c7ee5f2001-11-28 16:20:07 +00001039#define PyMapping_DelItemString(O,K) PyObject_DelItemString((O),(K))
Guido van Rossuma25e5e91996-09-06 13:48:38 +00001040
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001041/* Implemented as a macro:
Guido van Rossuma8275371995-07-18 14:07:00 +00001042
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001043 int PyMapping_DelItem(PyObject *o, PyObject *key);
Guido van Rossuma8275371995-07-18 14:07:00 +00001044
Serhiy Storchakaf5b11832018-05-22 11:02:44 +03001045 Remove the mapping for the object 'key' from the mapping object 'o'.
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001046 Returns -1 on failure.
1047
1048 This is equivalent to the Python statement: del o[key]. */
Jeremy Hylton7c7ee5f2001-11-28 16:20:07 +00001049#define PyMapping_DelItem(O,K) PyObject_DelItem((O),(K))
Guido van Rossuma8275371995-07-18 14:07:00 +00001050
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001051/* On success, return 1 if the mapping object 'o' has the key 'key',
1052 and 0 otherwise.
1053
1054 This is equivalent to the Python expression: key in o.
1055
1056 This function always succeeds. */
Victor Stinner2a358f82016-12-06 16:55:39 +01001057PyAPI_FUNC(int) PyMapping_HasKeyString(PyObject *o, const char *key);
Guido van Rossuma8275371995-07-18 14:07:00 +00001058
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001059/* Return 1 if the mapping object has the key 'key', and 0 otherwise.
Guido van Rossuma8275371995-07-18 14:07:00 +00001060
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001061 This is equivalent to the Python expression: key in o.
Guido van Rossuma8275371995-07-18 14:07:00 +00001062
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001063 This function always succeeds. */
Victor Stinner2a358f82016-12-06 16:55:39 +01001064PyAPI_FUNC(int) PyMapping_HasKey(PyObject *o, PyObject *key);
Guido van Rossuma8275371995-07-18 14:07:00 +00001065
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001066/* On success, return a list or tuple of the keys in mapping object 'o'.
1067 On failure, return NULL. */
Victor Stinner2a358f82016-12-06 16:55:39 +01001068PyAPI_FUNC(PyObject *) PyMapping_Keys(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +00001069
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001070/* On success, return a list or tuple of the values in mapping object 'o'.
1071 On failure, return NULL. */
Victor Stinner2a358f82016-12-06 16:55:39 +01001072PyAPI_FUNC(PyObject *) PyMapping_Values(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +00001073
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001074/* On success, return a list or tuple of the items in mapping object 'o',
1075 where each item is a tuple containing a key-value pair. On failure, return
1076 NULL. */
Victor Stinner2a358f82016-12-06 16:55:39 +01001077PyAPI_FUNC(PyObject *) PyMapping_Items(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +00001078
Serhiy Storchakaf5b11832018-05-22 11:02:44 +03001079/* Return element of 'o' corresponding to the string 'key' or NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +00001080
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001081 This is the equivalent of the Python expression: o[key]. */
Victor Stinner2a358f82016-12-06 16:55:39 +01001082PyAPI_FUNC(PyObject *) PyMapping_GetItemString(PyObject *o,
1083 const char *key);
Guido van Rossuma8275371995-07-18 14:07:00 +00001084
Serhiy Storchakaf5b11832018-05-22 11:02:44 +03001085/* Map the string 'key' to the value 'v' in the mapping 'o'.
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001086 Returns -1 on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +00001087
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001088 This is the equivalent of the Python statement: o[key]=v. */
Victor Stinner2a358f82016-12-06 16:55:39 +01001089PyAPI_FUNC(int) PyMapping_SetItemString(PyObject *o, const char *key,
1090 PyObject *value);
Guido van Rossuma8275371995-07-18 14:07:00 +00001091
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001092/* isinstance(object, typeorclass) */
Mark Hammond91a681d2002-08-12 07:21:58 +00001093PyAPI_FUNC(int) PyObject_IsInstance(PyObject *object, PyObject *typeorclass);
Guido van Rossum823649d2001-03-21 18:40:58 +00001094
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001095/* issubclass(object, typeorclass) */
Mark Hammond91a681d2002-08-12 07:21:58 +00001096PyAPI_FUNC(int) PyObject_IsSubclass(PyObject *object, PyObject *typeorclass);
Guido van Rossum823649d2001-03-21 18:40:58 +00001097
1098
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001099#ifndef Py_LIMITED_API
Antoine Pitrouec569b72008-08-26 22:40:48 +00001100PyAPI_FUNC(int) _PyObject_RealIsInstance(PyObject *inst, PyObject *cls);
1101
1102PyAPI_FUNC(int) _PyObject_RealIsSubclass(PyObject *derived, PyObject *cls);
1103
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001104PyAPI_FUNC(char *const *) _PySequence_BytesToCharpArray(PyObject* self);
1105
1106PyAPI_FUNC(void) _Py_FreeCharPArray(char *const array[]);
Antoine Pitrouec569b72008-08-26 22:40:48 +00001107
Antoine Pitrouf68c2a72010-09-01 12:58:21 +00001108/* For internal use by buffer API functions */
1109PyAPI_FUNC(void) _Py_add_one_to_index_F(int nd, Py_ssize_t *index,
1110 const Py_ssize_t *shape);
1111PyAPI_FUNC(void) _Py_add_one_to_index_C(int nd, Py_ssize_t *index,
1112 const Py_ssize_t *shape);
Serhiy Storchaka762bf402017-03-30 09:15:31 +03001113
1114/* Convert Python int to Py_ssize_t. Do nothing if the argument is None. */
1115PyAPI_FUNC(int) _Py_convert_optional_to_ssize_t(PyObject *, void *);
Serhiy Storchaka9fab79b2016-09-11 11:03:14 +03001116#endif /* !Py_LIMITED_API */
Antoine Pitrouf68c2a72010-09-01 12:58:21 +00001117
1118
Guido van Rossum8ca687a1995-09-18 21:20:02 +00001119#ifdef __cplusplus
1120}
1121#endif
Guido van Rossuma8275371995-07-18 14:07:00 +00001122#endif /* Py_ABSTRACTOBJECT_H */