blob: 8c1e45bb267f722d4ed04225ecfc14de43c1a3fa [file] [log] [blame]
Guido van Rossuma8275371995-07-18 14:07:00 +00001#ifndef Py_ABSTRACTOBJECT_H
2#define Py_ABSTRACTOBJECT_H
3#ifdef __cplusplus
4extern "C" {
5#endif
6
Guido van Rossuma8275371995-07-18 14:07:00 +00007/* Abstract Object Interface (many thanks to Jim Fulton) */
8
9/*
10 PROPOSAL: A Generic Python Object Interface for Python C Modules
11
12Problem
13
14 Python modules written in C that must access Python objects must do
15 so through routines whose interfaces are described by a set of
16 include files. Unfortunately, these routines vary according to the
17 object accessed. To use these routines, the C programmer must check
18 the type of the object being used and must call a routine based on
19 the object type. For example, to access an element of a sequence,
20 the programmer must determine whether the sequence is a list or a
21 tuple:
22
Victor Stinner2a358f82016-12-06 16:55:39 +010023 if (is_tupleobject(o))
24 e = gettupleitem(o, i)
25 else if (is_listitem(o))
26 e = getlistitem(o, i)
Guido van Rossuma8275371995-07-18 14:07:00 +000027
28 If the programmer wants to get an item from another type of object
29 that provides sequence behavior, there is no clear way to do it
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000030 correctly.
Guido van Rossuma8275371995-07-18 14:07:00 +000031
32 The persistent programmer may peruse object.h and find that the
33 _typeobject structure provides a means of invoking up to (currently
34 about) 41 special operators. So, for example, a routine can get an
35 item from any object that provides sequence behavior. However, to
36 use this mechanism, the programmer must make their code dependent on
37 the current Python implementation.
38
39 Also, certain semantics, especially memory management semantics, may
40 differ by the type of object being used. Unfortunately, these
41 semantics are not clearly described in the current include files.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000042 An abstract interface providing more consistent semantics is needed.
Guido van Rossuma8275371995-07-18 14:07:00 +000043
44Proposal
45
46 I propose the creation of a standard interface (with an associated
47 library of routines and/or macros) for generically obtaining the
48 services of Python objects. This proposal can be viewed as one
49 components of a Python C interface consisting of several components.
50
Raymond Hettingera72e2f92003-02-28 05:11:03 +000051 From the viewpoint of C access to Python services, we have (as
Guido van Rossuma8275371995-07-18 14:07:00 +000052 suggested by Guido in off-line discussions):
53
54 - "Very high level layer": two or three functions that let you exec or
55 eval arbitrary Python code given as a string in a module whose name is
56 given, passing C values in and getting C values out using
57 mkvalue/getargs style format strings. This does not require the user
58 to declare any variables of type "PyObject *". This should be enough
59 to write a simple application that gets Python code from the user,
60 execs it, and returns the output or errors. (Error handling must also
61 be part of this API.)
62
63 - "Abstract objects layer": which is the subject of this proposal.
64 It has many functions operating on objects, and lest you do many
65 things from C that you can also write in Python, without going
66 through the Python parser.
67
68 - "Concrete objects layer": This is the public type-dependent
69 interface provided by the standard built-in types, such as floats,
70 strings, and lists. This interface exists and is currently
Raymond Hettingera72e2f92003-02-28 05:11:03 +000071 documented by the collection of include files provided with the
Guido van Rossuma8275371995-07-18 14:07:00 +000072 Python distributions.
73
74 From the point of view of Python accessing services provided by C
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 modules:
Guido van Rossuma8275371995-07-18 14:07:00 +000076
77 - "Python module interface": this interface consist of the basic
78 routines used to define modules and their members. Most of the
79 current extensions-writing guide deals with this interface.
80
81 - "Built-in object interface": this is the interface that a new
82 built-in type must provide and the mechanisms and rules that a
83 developer of a new built-in type must use and follow.
84
85 This proposal is a "first-cut" that is intended to spur
86 discussion. See especially the lists of notes.
87
88 The Python C object interface will provide four protocols: object,
89 numeric, sequence, and mapping. Each protocol consists of a
90 collection of related operations. If an operation that is not
91 provided by a particular type is invoked, then a standard exception,
Martin Panter7462b6492015-11-02 03:37:02 +000092 NotImplementedError is raised with an operation name as an argument.
Guido van Rossuma8275371995-07-18 14:07:00 +000093 In addition, for convenience this interface defines a set of
94 constructors for building objects of built-in types. This is needed
95 so new objects can be returned from C functions that otherwise treat
96 objects generically.
97
98Memory Management
99
100 For all of the functions described in this proposal, if a function
101 retains a reference to a Python object passed as an argument, then the
102 function will increase the reference count of the object. It is
103 unnecessary for the caller to increase the reference count of an
104 argument in anticipation of the object's retention.
105
106 All Python objects returned from functions should be treated as new
107 objects. Functions that return objects assume that the caller will
108 retain a reference and the reference count of the object has already
109 been incremented to account for this fact. A caller that does not
110 retain a reference to an object that is returned from a function
111 must decrement the reference count of the object (using
112 DECREF(object)) to prevent memory leaks.
113
114 Note that the behavior mentioned here is different from the current
115 behavior for some objects (e.g. lists and tuples) when certain
116 type-specific routines are called directly (e.g. setlistitem). The
117 proposed abstraction layer will provide a consistent memory
118 management interface, correcting for inconsistent behavior for some
119 built-in types.
120
121Protocols
Victor Stinner2a358f82016-12-06 16:55:39 +0100122*/
Guido van Rossuma8275371995-07-18 14:07:00 +0000123
124
Victor Stinner2a358f82016-12-06 16:55:39 +0100125/* === Object Protocol ================================================== */
Guido van Rossuma8275371995-07-18 14:07:00 +0000126
Victor Stinner2a358f82016-12-06 16:55:39 +0100127/* Implemented elsewhere:
Guido van Rossuma8275371995-07-18 14:07:00 +0000128
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100129 int PyObject_Print(PyObject *o, FILE *fp, int flags);
Guido van Rossuma8275371995-07-18 14:07:00 +0000130
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100131 Print an object 'o' on file 'fp'. Returns -1 on error. The flags argument
132 is used to enable certain printing options. The only option currently
133 supported is Py_Print_RAW.
Guido van Rossuma8275371995-07-18 14:07:00 +0000134
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100135 (What should be said about Py_Print_RAW?). */
Victor Stinner2a358f82016-12-06 16:55:39 +0100136
137
138/* Implemented elsewhere:
139
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100140 int PyObject_HasAttrString(PyObject *o, const char *attr_name);
Victor Stinner2a358f82016-12-06 16:55:39 +0100141
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100142 Returns 1 if object 'o' has the attribute attr_name, and 0 otherwise.
Victor Stinner2a358f82016-12-06 16:55:39 +0100143
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100144 This is equivalent to the Python expression: hasattr(o,attr_name).
145
146 This function always succeeds. */
147
Victor Stinner2a358f82016-12-06 16:55:39 +0100148
149/* Implemented elsewhere:
150
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100151 PyObject* PyObject_GetAttrString(PyObject *o, const char *attr_name);
Victor Stinner2a358f82016-12-06 16:55:39 +0100152
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100153 Retrieve an attributed named attr_name form object o.
154 Returns the attribute value on success, or NULL on failure.
Victor Stinner2a358f82016-12-06 16:55:39 +0100155
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100156 This is the equivalent of the Python expression: o.attr_name. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100157
Guido van Rossuma8275371995-07-18 14:07:00 +0000158
Victor Stinner2a358f82016-12-06 16:55:39 +0100159/* Implemented elsewhere:
Guido van Rossuma8275371995-07-18 14:07:00 +0000160
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100161 int PyObject_HasAttr(PyObject *o, PyObject *attr_name);
Guido van Rossuma8275371995-07-18 14:07:00 +0000162
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100163 Returns 1 if o has the attribute attr_name, and 0 otherwise.
Guido van Rossuma8275371995-07-18 14:07:00 +0000164
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100165 This is equivalent to the Python expression: hasattr(o,attr_name).
Guido van Rossuma8275371995-07-18 14:07:00 +0000166
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100167 This function always succeeds. */
Guido van Rossuma8275371995-07-18 14:07:00 +0000168
Victor Stinner2a358f82016-12-06 16:55:39 +0100169/* Implemented elsewhere:
Guido van Rossuma8275371995-07-18 14:07:00 +0000170
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100171 PyObject* PyObject_GetAttr(PyObject *o, PyObject *attr_name);
Guido van Rossuma8275371995-07-18 14:07:00 +0000172
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100173 Retrieve an attributed named 'attr_name' form object 'o'.
174 Returns the attribute value on success, or NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000175
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100176 This is the equivalent of the Python expression: o.attr_name. */
Marc-André Lemburgad7c98e2001-01-17 17:09:53 +0000177
Marc-André Lemburgad7c98e2001-01-17 17:09:53 +0000178
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100179/* Implemented elsewhere:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000180
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100181 int PyObject_SetAttrString(PyObject *o, const char *attr_name, PyObject *v);
Guido van Rossuma8275371995-07-18 14:07:00 +0000182
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100183 Set the value of the attribute named attr_name, for object 'o',
184 to the value 'v'. Raise an exception and return -1 on failure; return 0 on
185 success.
Guido van Rossuma8275371995-07-18 14:07:00 +0000186
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100187 This is the equivalent of the Python statement o.attr_name=v. */
188
189
190/* Implemented elsewhere:
191
192 int PyObject_SetAttr(PyObject *o, PyObject *attr_name, PyObject *v);
193
194 Set the value of the attribute named attr_name, for object 'o', to the value
195 'v'. an exception and return -1 on failure; return 0 on success.
196
197 This is the equivalent of the Python statement o.attr_name=v. */
198
199/* Implemented as a macro:
200
201 int PyObject_DelAttrString(PyObject *o, const char *attr_name);
202
203 Delete attribute named attr_name, for object o. Returns
204 -1 on failure.
205
206 This is the equivalent of the Python statement: del o.attr_name. */
207#define PyObject_DelAttrString(O,A) PyObject_SetAttrString((O),(A), NULL)
208
209
210/* Implemented as a macro:
211
212 int PyObject_DelAttr(PyObject *o, PyObject *attr_name);
213
214 Delete attribute named attr_name, for object o. Returns -1
215 on failure. This is the equivalent of the Python
216 statement: del o.attr_name. */
217#define PyObject_DelAttr(O,A) PyObject_SetAttr((O),(A), NULL)
218
219
220/* Implemented elsewhere:
221
222 PyObject *PyObject_Repr(PyObject *o);
223
224 Compute the string representation of object 'o'. Returns the
225 string representation on success, NULL on failure.
226
227 This is the equivalent of the Python expression: repr(o).
228
229 Called by the repr() built-in function. */
230
231
232/* Implemented elsewhere:
233
234 PyObject *PyObject_Str(PyObject *o);
235
236 Compute the string representation of object, o. Returns the
237 string representation on success, NULL on failure.
238
239 This is the equivalent of the Python expression: str(o).
240
241 Called by the str() and print() built-in functions. */
242
243
244/* Declared elsewhere
245
246 PyAPI_FUNC(int) PyCallable_Check(PyObject *o);
247
248 Determine if the object, o, is callable. Return 1 if the object is callable
249 and 0 otherwise.
250
251 This function always succeeds. */
252
253
254#ifdef PY_SSIZE_T_CLEAN
255# define PyObject_CallFunction _PyObject_CallFunction_SizeT
256# define PyObject_CallMethod _PyObject_CallMethod_SizeT
257# ifndef Py_LIMITED_API
258# define _PyObject_CallMethodId _PyObject_CallMethodId_SizeT
259# endif /* !Py_LIMITED_API */
260#endif
261
Guido van Rossuma8275371995-07-18 14:07:00 +0000262
Victor Stinner2a358f82016-12-06 16:55:39 +0100263/* Call a callable Python object 'callable' with arguments given by the
264 tuple 'args' and keywords arguments given by the dictionary 'kwargs'.
Tim Peters6d6c1a32001-08-02 04:15:00 +0000265
Victor Stinner2a358f82016-12-06 16:55:39 +0100266 'args' must not be *NULL*, use an empty tuple if no arguments are
267 needed. If no named arguments are needed, 'kwargs' can be NULL.
Victor Stinner2d0eb652016-12-06 16:27:24 +0100268
Victor Stinner2a358f82016-12-06 16:55:39 +0100269 This is the equivalent of the Python expression:
270 callable(*args, **kwargs). */
271PyAPI_FUNC(PyObject *) PyObject_Call(PyObject *callable,
272 PyObject *args, PyObject *kwargs);
273
Victor Stinner4a7cc882015-03-06 23:35:27 +0100274#ifndef Py_LIMITED_API
Victor Stinner2a358f82016-12-06 16:55:39 +0100275PyAPI_FUNC(PyObject*) _PyStack_AsTuple(
276 PyObject **stack,
277 Py_ssize_t nargs);
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200278
Victor Stinner2a358f82016-12-06 16:55:39 +0100279/* Convert keyword arguments from the (stack, kwnames) format to a Python
280 dictionary.
Victor Stinner57f91ac2016-09-12 13:37:07 +0200281
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100282 kwnames must only contains str strings, no subclass, and all keys must be
283 unique. kwnames is not checked, usually these checks are done before or
284 later calling _PyStack_AsDict(). For example, _PyArg_ParseStack() raises an
Victor Stinner2a358f82016-12-06 16:55:39 +0100285 error if a key is not a string. */
286PyAPI_FUNC(PyObject *) _PyStack_AsDict(
287 PyObject **values,
288 PyObject *kwnames);
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700289
Victor Stinner2a358f82016-12-06 16:55:39 +0100290/* Convert (args, nargs, kwargs) into a (stack, nargs, kwnames).
Victor Stinnera9efb2f2016-09-09 17:40:22 -0700291
Victor Stinner2a358f82016-12-06 16:55:39 +0100292 Return a new stack which should be released by PyMem_Free(), or return
293 args unchanged if kwargs is NULL or an empty dictionary.
Victor Stinnera9efb2f2016-09-09 17:40:22 -0700294
Victor Stinner2a358f82016-12-06 16:55:39 +0100295 The stack uses borrowed references.
Victor Stinnera9efb2f2016-09-09 17:40:22 -0700296
Victor Stinner2a358f82016-12-06 16:55:39 +0100297 The type of keyword keys is not checked, these checks should be done
298 later (ex: _PyArg_ParseStack). */
299PyAPI_FUNC(PyObject **) _PyStack_UnpackDict(
300 PyObject **args,
301 Py_ssize_t nargs,
302 PyObject *kwargs,
303 PyObject **kwnames,
304 PyObject *func);
Victor Stinnera9efb2f2016-09-09 17:40:22 -0700305
Victor Stinner2a358f82016-12-06 16:55:39 +0100306/* Call the callable object 'callable' with the "fast call" calling convention:
307 args is a C array for positional arguments (nargs is the number of
308 positional arguments), kwargs is a dictionary for keyword arguments.
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200309
Victor Stinner2a358f82016-12-06 16:55:39 +0100310 If nargs is equal to zero, args can be NULL. kwargs can be NULL.
311 nargs must be greater or equal to zero.
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200312
Victor Stinner2a358f82016-12-06 16:55:39 +0100313 Return the result on success. Raise an exception on return NULL on
314 error. */
315PyAPI_FUNC(PyObject *) _PyObject_FastCallDict(
316 PyObject *callable,
317 PyObject **args,
318 Py_ssize_t nargs,
319 PyObject *kwargs);
Victor Stinner559bb6a2016-08-22 22:48:54 +0200320
Victor Stinner2a358f82016-12-06 16:55:39 +0100321/* Call the callable object 'callable' with the "fast call" calling convention:
322 args is a C array for positional arguments followed by values of
323 keyword arguments. Keys of keyword arguments are stored as a tuple
324 of strings in kwnames. nargs is the number of positional parameters at
325 the beginning of stack. The size of kwnames gives the number of keyword
326 values in the stack after positional arguments.
Victor Stinnerd8735722016-09-09 12:36:44 -0700327
Victor Stinner2a358f82016-12-06 16:55:39 +0100328 kwnames must only contains str strings, no subclass, and all keys must
329 be unique.
Victor Stinnerd8735722016-09-09 12:36:44 -0700330
Victor Stinner2a358f82016-12-06 16:55:39 +0100331 If nargs is equal to zero and there is no keyword argument (kwnames is
332 NULL or its size is zero), args can be NULL.
Victor Stinner57f91ac2016-09-12 13:37:07 +0200333
Victor Stinner2a358f82016-12-06 16:55:39 +0100334 Return the result on success. Raise an exception and return NULL on
335 error. */
336PyAPI_FUNC(PyObject *) _PyObject_FastCallKeywords(
337 PyObject *callable,
338 PyObject **args,
339 Py_ssize_t nargs,
340 PyObject *kwnames);
Victor Stinnerd8735722016-09-09 12:36:44 -0700341
Victor Stinner559bb6a2016-08-22 22:48:54 +0200342#define _PyObject_FastCall(func, args, nargs) \
343 _PyObject_FastCallDict((func), (args), (nargs), NULL)
344
345#define _PyObject_CallNoArg(func) \
Victor Stinner7bfb42d2016-12-05 17:04:32 +0100346 _PyObject_FastCallDict((func), NULL, 0, NULL)
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200347
Victor Stinner2a358f82016-12-06 16:55:39 +0100348PyAPI_FUNC(PyObject *) _PyObject_Call_Prepend(
349 PyObject *callable,
350 PyObject *obj,
351 PyObject *args,
352 PyObject *kwargs);
Victor Stinner3f1057a2016-08-25 01:04:14 +0200353
Victor Stinner2a358f82016-12-06 16:55:39 +0100354PyAPI_FUNC(PyObject *) _Py_CheckFunctionResult(PyObject *callable,
355 PyObject *result,
356 const char *where);
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200357#endif /* Py_LIMITED_API */
Victor Stinner4a7cc882015-03-06 23:35:27 +0100358
Victor Stinner2d0eb652016-12-06 16:27:24 +0100359
Victor Stinner2a358f82016-12-06 16:55:39 +0100360/* Call a callable Python object 'callable', with arguments given by the
361 tuple 'args'. If no arguments are needed, then 'args' can be *NULL*.
Victor Stinner2d0eb652016-12-06 16:27:24 +0100362
Victor Stinner2a358f82016-12-06 16:55:39 +0100363 Returns the result of the call on success, or *NULL* on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000364
Victor Stinner2a358f82016-12-06 16:55:39 +0100365 This is the equivalent of the Python expression:
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100366 callable(*args). */
Victor Stinner2a358f82016-12-06 16:55:39 +0100367PyAPI_FUNC(PyObject *) PyObject_CallObject(PyObject *callable,
368 PyObject *args);
Guido van Rossuma8275371995-07-18 14:07:00 +0000369
Victor Stinner2a358f82016-12-06 16:55:39 +0100370/* Call a callable Python object, callable, with a variable number of C
371 arguments. The C arguments are described using a mkvalue-style format
372 string.
Victor Stinner2d0eb652016-12-06 16:27:24 +0100373
Victor Stinner2a358f82016-12-06 16:55:39 +0100374 The format may be NULL, indicating that no arguments are provided.
Victor Stinner2d0eb652016-12-06 16:27:24 +0100375
Victor Stinner2a358f82016-12-06 16:55:39 +0100376 Returns the result of the call on success, or NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000377
Victor Stinner2a358f82016-12-06 16:55:39 +0100378 This is the equivalent of the Python expression:
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100379 callable(arg1, arg2, ...). */
Victor Stinner2a358f82016-12-06 16:55:39 +0100380PyAPI_FUNC(PyObject *) PyObject_CallFunction(PyObject *callable,
381 const char *format, ...);
Guido van Rossuma8275371995-07-18 14:07:00 +0000382
Victor Stinner2a358f82016-12-06 16:55:39 +0100383/* Call the method named 'name' of object 'obj' with a variable number of
384 C arguments. The C arguments are described by a mkvalue format string.
Guido van Rossuma8275371995-07-18 14:07:00 +0000385
Victor Stinner2a358f82016-12-06 16:55:39 +0100386 The format can be NULL, indicating that no arguments are provided.
Victor Stinner2d0eb652016-12-06 16:27:24 +0100387
Victor Stinner2a358f82016-12-06 16:55:39 +0100388 Returns the result of the call on success, or NULL on failure.
389
390 This is the equivalent of the Python expression:
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100391 obj.name(arg1, arg2, ...). */
Victor Stinner2a358f82016-12-06 16:55:39 +0100392PyAPI_FUNC(PyObject *) PyObject_CallMethod(PyObject *obj,
393 const char *name,
394 const char *format, ...);
Guido van Rossuma8275371995-07-18 14:07:00 +0000395
Serhiy Storchaka9fab79b2016-09-11 11:03:14 +0300396#ifndef Py_LIMITED_API
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100397/* Like PyObject_CallMethod(), but expect a _Py_Identifier*
398 as the method name. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100399PyAPI_FUNC(PyObject *) _PyObject_CallMethodId(PyObject *obj,
400 _Py_Identifier *name,
401 const char *format, ...);
Serhiy Storchaka9fab79b2016-09-11 11:03:14 +0300402#endif /* !Py_LIMITED_API */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200403
Victor Stinner2a358f82016-12-06 16:55:39 +0100404PyAPI_FUNC(PyObject *) _PyObject_CallFunction_SizeT(PyObject *callable,
405 const char *format,
406 ...);
407
408PyAPI_FUNC(PyObject *) _PyObject_CallMethod_SizeT(PyObject *obj,
409 const char *name,
410 const char *format,
411 ...);
412
Serhiy Storchaka9fab79b2016-09-11 11:03:14 +0300413#ifndef Py_LIMITED_API
Victor Stinner2a358f82016-12-06 16:55:39 +0100414PyAPI_FUNC(PyObject *) _PyObject_CallMethodId_SizeT(PyObject *obj,
415 _Py_Identifier *name,
416 const char *format,
417 ...);
Serhiy Storchaka9fab79b2016-09-11 11:03:14 +0300418#endif /* !Py_LIMITED_API */
Guido van Rossuma8275371995-07-18 14:07:00 +0000419
Victor Stinner2a358f82016-12-06 16:55:39 +0100420/* Call a callable Python object 'callable' with a variable number of C
421 arguments. The C arguments are provided as PyObject* values, terminated
422 by a NULL.
Victor Stinner2d0eb652016-12-06 16:27:24 +0100423
Victor Stinner2a358f82016-12-06 16:55:39 +0100424 Returns the result of the call on success, or NULL on failure.
Victor Stinner2d0eb652016-12-06 16:27:24 +0100425
Victor Stinner2a358f82016-12-06 16:55:39 +0100426 This is the equivalent of the Python expression:
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100427 callable(arg1, arg2, ...). */
Victor Stinner2a358f82016-12-06 16:55:39 +0100428PyAPI_FUNC(PyObject *) PyObject_CallFunctionObjArgs(PyObject *callable,
429 ...);
Fred Drakeb421b8c2001-10-26 16:21:32 +0000430
Victor Stinner7f39c0c2016-12-09 00:40:33 +0100431#ifndef Py_LIMITED_API
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100432/* Similar PyObject_CallFunctionObjArgs(), but pass positional arguments
433 as a va_list: list of PyObject* object. */
Victor Stinner7f39c0c2016-12-09 00:40:33 +0100434PyAPI_FUNC(PyObject *) _PyObject_VaCallFunctionObjArgs(
435 PyObject *callable,
436 va_list vargs);
437#endif
438
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100439/* Call the method named 'name' of object 'obj' with a variable number of
440 C arguments. The C arguments are provided as PyObject* values, terminated
441 by NULL.
442
443 Returns the result of the call on success, or NULL on failure.
444
445 This is the equivalent of the Python expression: obj.name(*args). */
Guido van Rossuma8275371995-07-18 14:07:00 +0000446
Victor Stinner2a358f82016-12-06 16:55:39 +0100447PyAPI_FUNC(PyObject *) PyObject_CallMethodObjArgs(
448 PyObject *obj,
449 PyObject *name,
450 ...);
451
Victor Stinner2d0eb652016-12-06 16:27:24 +0100452#ifndef Py_LIMITED_API
Victor Stinner2a358f82016-12-06 16:55:39 +0100453PyAPI_FUNC(PyObject *) _PyObject_CallMethodIdObjArgs(
454 PyObject *obj,
455 struct _Py_Identifier *name,
456 ...);
Victor Stinner2d0eb652016-12-06 16:27:24 +0100457#endif /* !Py_LIMITED_API */
458
459
Victor Stinner2a358f82016-12-06 16:55:39 +0100460/* Implemented elsewhere:
Guido van Rossuma8275371995-07-18 14:07:00 +0000461
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100462 long PyObject_Hash(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000463
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100464 Compute and return the hash, hash_value, of an object, o. On
465 failure, return -1.
466
467 This is the equivalent of the Python expression: hash(o). */
Guido van Rossuma8275371995-07-18 14:07:00 +0000468
469
Victor Stinner2a358f82016-12-06 16:55:39 +0100470/* Implemented elsewhere:
Guido van Rossuma8275371995-07-18 14:07:00 +0000471
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100472 int PyObject_IsTrue(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000473
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100474 Returns 1 if the object, o, is considered to be true, 0 if o is
475 considered to be false and -1 on failure.
476
477 This is equivalent to the Python expression: not not o. */
478
Guido van Rossuma8275371995-07-18 14:07:00 +0000479
Victor Stinner2a358f82016-12-06 16:55:39 +0100480/* Implemented elsewhere:
Guido van Rossumc3d3f961998-04-09 17:53:59 +0000481
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100482 int PyObject_Not(PyObject *o);
Guido van Rossumc3d3f961998-04-09 17:53:59 +0000483
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100484 Returns 0 if the object, o, is considered to be true, 1 if o is
485 considered to be false and -1 on failure.
Guido van Rossumc3d3f961998-04-09 17:53:59 +0000486
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100487 This is equivalent to the Python expression: not o. */
488
489
490/* Get the type of an object.
491
492 On success, returns a type object corresponding to the object type of object
493 'o'. On failure, returns NULL.
494
495 This is equivalent to the Python expression: type(o) */
Victor Stinner2a358f82016-12-06 16:55:39 +0100496PyAPI_FUNC(PyObject *) PyObject_Type(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000497
Guido van Rossuma8275371995-07-18 14:07:00 +0000498
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100499/* Return the size of object 'o'. If the object 'o' provides both sequence and
500 mapping protocols, the sequence size is returned.
501
502 On error, -1 is returned.
503
504 This is the equivalent to the Python expression: len(o) */
Victor Stinner2a358f82016-12-06 16:55:39 +0100505PyAPI_FUNC(Py_ssize_t) PyObject_Size(PyObject *o);
Jeremy Hylton6253f832000-07-12 12:56:19 +0000506
Guido van Rossuma8275371995-07-18 14:07:00 +0000507
Victor Stinner2a358f82016-12-06 16:55:39 +0100508/* For DLL compatibility */
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +0000509#undef PyObject_Length
Victor Stinner2a358f82016-12-06 16:55:39 +0100510PyAPI_FUNC(Py_ssize_t) PyObject_Length(PyObject *o);
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +0000511#define PyObject_Length PyObject_Size
512
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100513
Armin Ronacher74b38b12012-10-07 10:29:32 +0200514#ifndef Py_LIMITED_API
Victor Stinner2a358f82016-12-06 16:55:39 +0100515PyAPI_FUNC(int) _PyObject_HasLen(PyObject *o);
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100516
517/* Guess the size of object 'o' using len(o) or o.__length_hint__().
518 If neither of those return a non-negative value, then return the default
519 value. If one of the calls fails, this function returns -1. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100520PyAPI_FUNC(Py_ssize_t) PyObject_LengthHint(PyObject *o, Py_ssize_t);
Armin Ronacher74b38b12012-10-07 10:29:32 +0200521#endif
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000522
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100523/* Return element of 'o' corresponding to the object 'key'. Return NULL
524 on failure.
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +0000525
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100526 This is the equivalent of the Python expression: o[key] */
Victor Stinner2a358f82016-12-06 16:55:39 +0100527PyAPI_FUNC(PyObject *) PyObject_GetItem(PyObject *o, PyObject *key);
Guido van Rossuma8275371995-07-18 14:07:00 +0000528
Guido van Rossuma8275371995-07-18 14:07:00 +0000529
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100530/* Map the object 'key' to the value 'v' into 'o'.
531
532 Raise an exception and return -1 on failure; return 0 on success.
533
534 This is the equivalent of the Python statement: o[key]=v. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100535PyAPI_FUNC(int) PyObject_SetItem(PyObject *o, PyObject *key, PyObject *v);
Guido van Rossuma8275371995-07-18 14:07:00 +0000536
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100537/* Remove the mapping for object, key, from the object 'o'.
538 Returns -1 on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000539
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100540 This is equivalent to the Python statement: del o[key]. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100541PyAPI_FUNC(int) PyObject_DelItemString(PyObject *o, const char *key);
Martin v. Löwisb0d71d02002-01-05 10:50:30 +0000542
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100543/* Delete the mapping for key from object 'o'. Returns -1 on failure.
Martin v. Löwisb0d71d02002-01-05 10:50:30 +0000544
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100545 This is the equivalent of the Python statement: del o[key]. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100546PyAPI_FUNC(int) PyObject_DelItem(PyObject *o, PyObject *key);
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000547
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000548
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100549/* === Old Buffer API ============================================ */
550
551/* FIXME: usage of these should all be replaced in Python itself
Victor Stinner2a358f82016-12-06 16:55:39 +0100552 but for backwards compatibility we will implement them.
553 Their usage without a corresponding "unlock" mechanism
554 may create issues (but they would already be there). */
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000555
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100556/* Takes an arbitrary object which must support the (character, single segment)
557 buffer interface and returns a pointer to a read-only memory location
558 useable as character based input for subsequent processing.
559
560 Return 0 on success. buffer and buffer_len are only set in case no error
561 occurs. Otherwise, -1 is returned and an exception set. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100562PyAPI_FUNC(int) PyObject_AsCharBuffer(PyObject *obj,
563 const char **buffer,
564 Py_ssize_t *buffer_len)
565 Py_DEPRECATED(3.0);
Guido van Rossumfd9eed32000-03-10 22:35:06 +0000566
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100567/* Checks whether an arbitrary object supports the (character, single segment)
568 buffer interface.
Guido van Rossumfd9eed32000-03-10 22:35:06 +0000569
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100570 Returns 1 on success, 0 on failure. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100571PyAPI_FUNC(int) PyObject_CheckReadBuffer(PyObject *obj)
572 Py_DEPRECATED(3.0);
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000573
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100574/* Same as PyObject_AsCharBuffer() except that this API expects (readable,
575 single segment) buffer interface and returns a pointer to a read-only memory
576 location which can contain arbitrary data.
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000577
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100578 0 is returned on success. buffer and buffer_len are only set in case no
579 error occurs. Otherwise, -1 is returned and an exception set. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100580PyAPI_FUNC(int) PyObject_AsReadBuffer(PyObject *obj,
581 const void **buffer,
582 Py_ssize_t *buffer_len)
583 Py_DEPRECATED(3.0);
Guido van Rossumfd9eed32000-03-10 22:35:06 +0000584
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100585/* Takes an arbitrary object which must support the (writable, single segment)
586 buffer interface and returns a pointer to a writable memory location in
587 buffer of size 'buffer_len'.
Guido van Rossumfd9eed32000-03-10 22:35:06 +0000588
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100589 Return 0 on success. buffer and buffer_len are only set in case no error
590 occurs. Otherwise, -1 is returned and an exception set. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100591PyAPI_FUNC(int) PyObject_AsWriteBuffer(PyObject *obj,
592 void **buffer,
593 Py_ssize_t *buffer_len)
594 Py_DEPRECATED(3.0);
Guido van Rossumfd9eed32000-03-10 22:35:06 +0000595
Guido van Rossumfd9eed32000-03-10 22:35:06 +0000596
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100597/* === New Buffer API ============================================ */
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000598
Martin v. Löwisc83bc3c2011-01-06 19:15:47 +0000599#ifndef Py_LIMITED_API
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100600
601/* Return 1 if the getbuffer function is available, otherwise return 0. */
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000602#define PyObject_CheckBuffer(obj) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 (((obj)->ob_type->tp_as_buffer != NULL) && \
604 ((obj)->ob_type->tp_as_buffer->bf_getbuffer != NULL))
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000605
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100606/* This is a C-API version of the getbuffer function call. It checks
607 to make sure object has the required function pointer and issues the
608 call.
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000609
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100610 Returns -1 and raises an error on failure and returns 0 on success. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100611PyAPI_FUNC(int) PyObject_GetBuffer(PyObject *obj, Py_buffer *view,
612 int flags);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000613
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100614/* Get the memory area pointed to by the indices for the buffer given.
615 Note that view->ndim is the assumed size of indices. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100616PyAPI_FUNC(void *) PyBuffer_GetPointer(Py_buffer *view, Py_ssize_t *indices);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000617
Victor Stinner2a358f82016-12-06 16:55:39 +0100618/* Return the implied itemsize of the data-format area from a
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100619 struct-style description. */
620PyAPI_FUNC(int) PyBuffer_SizeFromFormat(const char *);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621
Victor Stinner2a358f82016-12-06 16:55:39 +0100622/* Implementation in memoryobject.c */
623PyAPI_FUNC(int) PyBuffer_ToContiguous(void *buf, Py_buffer *view,
624 Py_ssize_t len, char order);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000625
Victor Stinner2a358f82016-12-06 16:55:39 +0100626PyAPI_FUNC(int) PyBuffer_FromContiguous(Py_buffer *view, void *buf,
627 Py_ssize_t len, char order);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000628
Victor Stinner2a358f82016-12-06 16:55:39 +0100629/* Copy len bytes of data from the contiguous chunk of memory
630 pointed to by buf into the buffer exported by obj. Return
631 0 on success and return -1 and raise a PyBuffer_Error on
632 error (i.e. the object does not have a buffer interface or
633 it is not working).
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000634
Victor Stinner2a358f82016-12-06 16:55:39 +0100635 If fort is 'F', then if the object is multi-dimensional,
636 then the data will be copied into the array in
637 Fortran-style (first dimension varies the fastest). If
638 fort is 'C', then the data will be copied into the array
639 in C-style (last dimension varies the fastest). If fort
640 is 'A', then it does not matter and the copy will be made
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100641 in whatever way is more efficient. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100642PyAPI_FUNC(int) PyObject_CopyData(PyObject *dest, PyObject *src);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100644/* Copy the data from the src buffer to the buffer of destination. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100645PyAPI_FUNC(int) PyBuffer_IsContiguous(const Py_buffer *view, char fort);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000646
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100647/*Fill the strides array with byte-strides of a contiguous
648 (Fortran-style if fort is 'F' or C-style otherwise)
649 array of the given shape with the given number of bytes
650 per element. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100651PyAPI_FUNC(void) PyBuffer_FillContiguousStrides(int ndims,
652 Py_ssize_t *shape,
653 Py_ssize_t *strides,
654 int itemsize,
655 char fort);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000656
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100657/* Fills in a buffer-info structure correctly for an exporter
658 that can only share a contiguous chunk of memory of
659 "unsigned bytes" of the given length.
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000660
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100661 Returns 0 on success and -1 (with raising an error) on error. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100662PyAPI_FUNC(int) PyBuffer_FillInfo(Py_buffer *view, PyObject *o, void *buf,
663 Py_ssize_t len, int readonly,
664 int flags);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000665
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100666/* Releases a Py_buffer obtained from getbuffer ParseTuple's "s*". */
Victor Stinner2a358f82016-12-06 16:55:39 +0100667PyAPI_FUNC(void) PyBuffer_Release(Py_buffer *view);
Martin v. Löwis423be952008-08-13 15:53:07 +0000668
Martin v. Löwisc83bc3c2011-01-06 19:15:47 +0000669#endif /* Py_LIMITED_API */
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000670
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100671/* Takes an arbitrary object and returns the result of calling
672 obj.__format__(format_spec). */
673PyAPI_FUNC(PyObject *) PyObject_Format(PyObject *obj,
Victor Stinner2a358f82016-12-06 16:55:39 +0100674 PyObject *format_spec);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000675
Guido van Rossum213c7a62001-04-23 14:08:49 +0000676
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100677/* ==== Iterators ================================================ */
678
679/* Takes an object and returns an iterator for it.
680 This is typically a new iterator but if the argument is an iterator, this
681 returns itself. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100682PyAPI_FUNC(PyObject *) PyObject_GetIter(PyObject *);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000683
Guido van Rossum213c7a62001-04-23 14:08:49 +0000684#define PyIter_Check(obj) \
Amaury Forgeot d'Arcf343e012009-01-12 23:58:21 +0000685 ((obj)->ob_type->tp_iternext != NULL && \
686 (obj)->ob_type->tp_iternext != &_PyObject_NextNotImplemented)
Guido van Rossum213c7a62001-04-23 14:08:49 +0000687
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100688/* Takes an iterator object and calls its tp_iternext slot,
689 returning the next value.
690
691 If the iterator is exhausted, this returns NULL without setting an
692 exception.
693
694 NULL with an exception means an error occurred. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100695PyAPI_FUNC(PyObject *) PyIter_Next(PyObject *);
Guido van Rossum213c7a62001-04-23 14:08:49 +0000696
Guido van Rossuma8275371995-07-18 14:07:00 +0000697
Victor Stinner2a358f82016-12-06 16:55:39 +0100698/* === Number Protocol ================================================== */
Guido van Rossuma8275371995-07-18 14:07:00 +0000699
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100700/* Returns 1 if the object 'o' provides numeric protocols, and 0 otherwise.
701
702 This function always succeeds. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100703PyAPI_FUNC(int) PyNumber_Check(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000704
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100705/* Returns the result of adding o1 and o2, or NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000706
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100707 This is the equivalent of the Python expression: o1 + o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100708PyAPI_FUNC(PyObject *) PyNumber_Add(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000709
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100710/* Returns the result of subtracting o2 from o1, or NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000711
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100712 This is the equivalent of the Python expression: o1 - o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100713PyAPI_FUNC(PyObject *) PyNumber_Subtract(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000714
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100715/* Returns the result of multiplying o1 and o2, or NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000716
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100717 This is the equivalent of the Python expression: o1 * o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100718PyAPI_FUNC(PyObject *) PyNumber_Multiply(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000719
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100720/* This is the equivalent of the Python expression: o1 @ o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100721PyAPI_FUNC(PyObject *) PyNumber_MatrixMultiply(PyObject *o1, PyObject *o2);
Benjamin Petersond51374e2014-04-09 23:55:56 -0400722
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100723/* Returns the result of dividing o1 by o2 giving an integral result,
724 or NULL on failure.
Guido van Rossum4668b002001-08-08 05:00:18 +0000725
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100726 This is the equivalent of the Python expression: o1 // o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100727PyAPI_FUNC(PyObject *) PyNumber_FloorDivide(PyObject *o1, PyObject *o2);
Guido van Rossum4668b002001-08-08 05:00:18 +0000728
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100729/* Returns the result of dividing o1 by o2 giving a float result, or NULL on
730 failure.
Guido van Rossum4668b002001-08-08 05:00:18 +0000731
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100732 This is the equivalent of the Python expression: o1 / o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100733PyAPI_FUNC(PyObject *) PyNumber_TrueDivide(PyObject *o1, PyObject *o2);
Guido van Rossum4668b002001-08-08 05:00:18 +0000734
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100735/* Returns the remainder of dividing o1 by o2, or NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000736
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100737 This is the equivalent of the Python expression: o1 % o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100738PyAPI_FUNC(PyObject *) PyNumber_Remainder(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000739
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100740/* See the built-in function divmod.
Guido van Rossuma8275371995-07-18 14:07:00 +0000741
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100742 Returns NULL on failure.
743
744 This is the equivalent of the Python expression: divmod(o1, o2). */
Victor Stinner2a358f82016-12-06 16:55:39 +0100745PyAPI_FUNC(PyObject *) PyNumber_Divmod(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000746
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100747/* See the built-in function pow. Returns NULL 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: pow(o1, o2, o3),
750 where o3 is optional. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100751PyAPI_FUNC(PyObject *) PyNumber_Power(PyObject *o1, PyObject *o2,
752 PyObject *o3);
Guido van Rossuma8275371995-07-18 14:07:00 +0000753
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100754/* Returns the negation of o on success, or NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000755
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100756 This is the equivalent of the Python expression: -o. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100757PyAPI_FUNC(PyObject *) PyNumber_Negative(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000758
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100759/* Returns the positive of o on success, or NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000760
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100761 This is the equivalent of the Python expression: +o. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100762PyAPI_FUNC(PyObject *) PyNumber_Positive(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000763
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100764/* Returns the absolute value of 'o', or NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000765
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100766 This is the equivalent of the Python expression: abs(o). */
Victor Stinner2a358f82016-12-06 16:55:39 +0100767PyAPI_FUNC(PyObject *) PyNumber_Absolute(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000768
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100769/* Returns the bitwise negation of 'o' on success, or NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000770
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100771 This is the equivalent of the Python expression: ~o. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100772PyAPI_FUNC(PyObject *) PyNumber_Invert(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000773
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100774/* Returns the result of left shifting o1 by o2 on success, or NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000775
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100776 This is the equivalent of the Python expression: o1 << o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100777PyAPI_FUNC(PyObject *) PyNumber_Lshift(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000778
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100779/* Returns the result of right shifting o1 by o2 on success, or NULL on
780 failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000781
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100782 This is the equivalent of the Python expression: o1 >> o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100783PyAPI_FUNC(PyObject *) PyNumber_Rshift(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000784
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100785/* Returns the result of bitwise and of o1 and o2 on success, or NULL on
786 failure.
Guido van Rossuma8275371995-07-18 14:07:00 +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_And(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000790
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100791/* Returns the bitwise exclusive or of o1 by o2 on success, or NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000792
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100793 This is the equivalent of the Python expression: o1 ^ o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100794PyAPI_FUNC(PyObject *) PyNumber_Xor(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000795
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100796/* Returns the result of bitwise or on o1 and o2 on success, or NULL on
797 failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000798
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100799 This is the equivalent of the Python expression: o1 | o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100800PyAPI_FUNC(PyObject *) PyNumber_Or(PyObject *o1, PyObject *o2);
801
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100802#define PyIndex_Check(obj) \
803 ((obj)->ob_type->tp_as_number != NULL && \
804 (obj)->ob_type->tp_as_number->nb_index != NULL)
Guido van Rossuma8275371995-07-18 14:07:00 +0000805
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100806/* Returns the object 'o' converted to a Python int, or NULL with an exception
807 raised on failure. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100808PyAPI_FUNC(PyObject *) PyNumber_Index(PyObject *o);
Guido van Rossum38fff8c2006-03-07 18:50:55 +0000809
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100810/* Returns the object 'o' converted to Py_ssize_t by going through
811 PyNumber_Index() first.
Guido van Rossum38fff8c2006-03-07 18:50:55 +0000812
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100813 If an overflow error occurs while converting the int to Py_ssize_t, then the
814 second argument 'exc' is the error-type to return. If it is NULL, then the
815 overflow error is cleared and the value is clipped. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100816PyAPI_FUNC(Py_ssize_t) PyNumber_AsSsize_t(PyObject *o, PyObject *exc);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000817
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100818/* Returns the object 'o' converted to an integer object on success, or NULL
819 on failure.
Guido van Rossum38fff8c2006-03-07 18:50:55 +0000820
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100821 This is the equivalent of the Python expression: int(o). */
Victor Stinner2a358f82016-12-06 16:55:39 +0100822PyAPI_FUNC(PyObject *) PyNumber_Long(PyObject *o);
Mark Dickinsond7467682009-01-10 22:14:33 +0000823
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100824/* Returns the object 'o' converted to a float object on success, or NULL
825 on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000826
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100827 This is the equivalent of the Python expression: float(o). */
Victor Stinner2a358f82016-12-06 16:55:39 +0100828PyAPI_FUNC(PyObject *) PyNumber_Float(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000829
Victor Stinner2a358f82016-12-06 16:55:39 +0100830
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100831/* --- In-place variants of (some of) the above number protocol functions -- */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100833/* Returns the result of adding o2 to o1, possibly in-place, or NULL
834 on failure.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000835
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100836 This is the equivalent of the Python expression: o1 += o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100837PyAPI_FUNC(PyObject *) PyNumber_InPlaceAdd(PyObject *o1, PyObject *o2);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000838
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100839/* Returns the result of subtracting o2 from o1, possibly in-place or
840 NULL on failure.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000841
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100842 This is the equivalent of the Python expression: o1 -= o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100843PyAPI_FUNC(PyObject *) PyNumber_InPlaceSubtract(PyObject *o1, PyObject *o2);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000844
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100845/* Returns the result of multiplying o1 by o2, possibly in-place, or NULL on
846 failure.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000847
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100848 This is the equivalent of the Python expression: o1 *= o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100849PyAPI_FUNC(PyObject *) PyNumber_InPlaceMultiply(PyObject *o1, PyObject *o2);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000850
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100851/* This is the equivalent of the Python expression: o1 @= o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100852PyAPI_FUNC(PyObject *) PyNumber_InPlaceMatrixMultiply(PyObject *o1, PyObject *o2);
Benjamin Petersond51374e2014-04-09 23:55:56 -0400853
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100854/* Returns the result of dividing o1 by o2 giving an integral result, possibly
855 in-place, or NULL on failure.
Benjamin Petersond51374e2014-04-09 23:55:56 -0400856
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100857 This is the equivalent of the Python expression: o1 /= o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100858PyAPI_FUNC(PyObject *) PyNumber_InPlaceFloorDivide(PyObject *o1,
859 PyObject *o2);
Guido van Rossum4668b002001-08-08 05:00:18 +0000860
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100861/* Returns the result of dividing o1 by o2 giving a float result, possibly
862 in-place, or null on failure.
Guido van Rossum4668b002001-08-08 05:00:18 +0000863
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100864 This is the equivalent of the Python expression: o1 /= o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100865PyAPI_FUNC(PyObject *) PyNumber_InPlaceTrueDivide(PyObject *o1,
866 PyObject *o2);
Guido van Rossum4668b002001-08-08 05:00:18 +0000867
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100868/* Returns the remainder of dividing o1 by o2, possibly in-place, or NULL on
869 failure.
Guido van Rossum4668b002001-08-08 05:00:18 +0000870
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100871 This is the equivalent of the Python expression: o1 %= o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100872PyAPI_FUNC(PyObject *) PyNumber_InPlaceRemainder(PyObject *o1, PyObject *o2);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000873
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100874/* Returns the result of raising o1 to the power of o2, possibly in-place,
875 or NULL on failure.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000876
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100877 This is the equivalent of the Python expression: o1 **= o2,
878 or o1 = pow(o1, o2, o3) if o3 is present. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100879PyAPI_FUNC(PyObject *) PyNumber_InPlacePower(PyObject *o1, PyObject *o2,
880 PyObject *o3);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000881
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100882/* Returns the result of left shifting o1 by o2, possibly in-place, or NULL
883 on failure.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000884
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100885 This is the equivalent of the Python expression: o1 <<= o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100886PyAPI_FUNC(PyObject *) PyNumber_InPlaceLshift(PyObject *o1, PyObject *o2);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000887
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100888/* Returns the result of right shifting o1 by o2, possibly in-place or NULL
889 on failure.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000890
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100891 This is the equivalent of the Python expression: o1 >>= o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100892PyAPI_FUNC(PyObject *) PyNumber_InPlaceRshift(PyObject *o1, PyObject *o2);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000893
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100894/* Returns the result of bitwise and of o1 and o2, possibly in-place, or NULL
895 on failure.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000896
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100897 This is the equivalent of the Python expression: o1 &= o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100898PyAPI_FUNC(PyObject *) PyNumber_InPlaceAnd(PyObject *o1, PyObject *o2);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000899
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100900/* Returns the bitwise exclusive or of o1 by o2, possibly in-place, or NULL
901 on failure.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000902
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100903 This is the equivalent of the Python expression: o1 ^= o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100904PyAPI_FUNC(PyObject *) PyNumber_InPlaceXor(PyObject *o1, PyObject *o2);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000905
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100906/* Returns the result of bitwise or of o1 and o2, possibly in-place,
907 or NULL on failure.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000908
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100909 This is the equivalent of the Python expression: o1 |= o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100910PyAPI_FUNC(PyObject *) PyNumber_InPlaceOr(PyObject *o1, PyObject *o2);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000911
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100912/* Returns the integer n converted to a string with a base, with a base
913 marker of 0b, 0o or 0x prefixed if applicable.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000914
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100915 If n is not an int object, it is converted with PyNumber_Index first. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100916PyAPI_FUNC(PyObject *) PyNumber_ToBase(PyObject *n, int base);
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000917
Guido van Rossuma8275371995-07-18 14:07:00 +0000918
Victor Stinner2a358f82016-12-06 16:55:39 +0100919/* === Sequence protocol ================================================ */
Guido van Rossuma8275371995-07-18 14:07:00 +0000920
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100921/* Return 1 if the object provides sequence protocol, and zero
922 otherwise.
923
924 This function always succeeds. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100925PyAPI_FUNC(int) PySequence_Check(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000926
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100927/* Return the size of sequence object o, or -1 on failure. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100928PyAPI_FUNC(Py_ssize_t) PySequence_Size(PyObject *o);
Jeremy Hylton6253f832000-07-12 12:56:19 +0000929
Victor Stinner2a358f82016-12-06 16:55:39 +0100930/* For DLL compatibility */
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +0000931#undef PySequence_Length
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100932PyAPI_FUNC(Py_ssize_t) PySequence_Length(PyObject *o);
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +0000933#define PySequence_Length PySequence_Size
934
935
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100936/* Return the concatenation of o1 and o2 on success, and NULL on failure.
937
938 This is the equivalent of the Python expression: o1 + o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100939PyAPI_FUNC(PyObject *) PySequence_Concat(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000940
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100941/* Return the result of repeating sequence object 'o' 'count' times,
942 or NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000943
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100944 This is the equivalent of the Python expression: o * count. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100945PyAPI_FUNC(PyObject *) PySequence_Repeat(PyObject *o, Py_ssize_t count);
Guido van Rossuma8275371995-07-18 14:07:00 +0000946
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100947/* Return the ith element of o, or NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000948
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100949 This is the equivalent of the Python expression: o[i]. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100950PyAPI_FUNC(PyObject *) PySequence_GetItem(PyObject *o, Py_ssize_t i);
Guido van Rossuma8275371995-07-18 14:07:00 +0000951
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100952/* Return the slice of sequence object o between i1 and i2, or NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000953
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100954 This is the equivalent of the Python expression: o[i1:i2]. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100955PyAPI_FUNC(PyObject *) PySequence_GetSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000956
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100957/* Assign object 'v' to the ith element of the sequence 'o'. Raise an exception
958 and return -1 on failure; return 0 on success.
Guido van Rossuma8275371995-07-18 14:07:00 +0000959
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100960 This is the equivalent of the Python statement o[i] = v. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100961PyAPI_FUNC(int) PySequence_SetItem(PyObject *o, Py_ssize_t i, PyObject *v);
Guido van Rossuma8275371995-07-18 14:07:00 +0000962
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100963/* Delete the 'i'-th element of the sequence 'v'. Returns -1 on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000964
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100965 This is the equivalent of the Python statement: del o[i]. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100966PyAPI_FUNC(int) PySequence_DelItem(PyObject *o, Py_ssize_t i);
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000967
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100968/* Assign the sequence object 'v' to the slice in sequence object 'o',
969 from 'i1' to 'i2'. Returns -1 on failure.
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000970
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100971 This is the equivalent of the Python statement: o[i1:i2] = v. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100972PyAPI_FUNC(int) PySequence_SetSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2,
973 PyObject *v);
Guido van Rossuma8275371995-07-18 14:07:00 +0000974
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100975/* Delete the slice in sequence object 'o' from 'i1' to 'i2'.
976 Returns -1 on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000977
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100978 This is the equivalent of the Python statement: del o[i1:i2]. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100979PyAPI_FUNC(int) PySequence_DelSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2);
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000980
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100981/* Returns the sequence 'o' as a tuple on success, and NULL on failure.
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000982
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100983 This is equivalent to the Python expression: tuple(o). */
Victor Stinner2a358f82016-12-06 16:55:39 +0100984PyAPI_FUNC(PyObject *) PySequence_Tuple(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000985
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100986/* Returns the sequence 'o' as a list on success, and NULL on failure.
987 This is equivalent to the Python expression: list(o) */
Victor Stinner2a358f82016-12-06 16:55:39 +0100988PyAPI_FUNC(PyObject *) PySequence_List(PyObject *o);
Guido van Rossumf39fc431997-03-04 18:31:47 +0000989
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100990/* Return the sequence 'o' as a list, unless it's already a tuple or list.
991
992 Use PySequence_Fast_GET_ITEM to access the members of this list, and
993 PySequence_Fast_GET_SIZE to get its length.
994
995 Returns NULL on failure. If the object does not support iteration, raises a
996 TypeError exception with 'm' as the message text. */
Victor Stinner2a358f82016-12-06 16:55:39 +0100997PyAPI_FUNC(PyObject *) PySequence_Fast(PyObject *o, const char* m);
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000998
Victor Stinner5bef7cd2016-12-15 09:14:25 +0100999/* Return the size of the sequence 'o', assuming that 'o' was returned by
1000 PySequence_Fast and is not NULL. */
Tim Peters1fc240e2001-10-26 05:06:50 +00001001#define PySequence_Fast_GET_SIZE(o) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 (PyList_Check(o) ? PyList_GET_SIZE(o) : PyTuple_GET_SIZE(o))
Tim Peters1fc240e2001-10-26 05:06:50 +00001003
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001004/* Return the 'i'-th element of the sequence 'o', assuming that o was returned
1005 by PySequence_Fast, and that i is within bounds. */
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001006#define PySequence_Fast_GET_ITEM(o, i)\
1007 (PyList_Check(o) ? PyList_GET_ITEM(o, i) : PyTuple_GET_ITEM(o, i))
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001008
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001009/* Assume tp_as_sequence and sq_item exist and that 'i' does not
1010 need to be corrected for a negative index. */
Martin v. Löwis01f94bd2002-05-08 08:44:21 +00001011#define PySequence_ITEM(o, i)\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 ( Py_TYPE(o)->tp_as_sequence->sq_item(o, i) )
Martin v. Löwis01f94bd2002-05-08 08:44:21 +00001013
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001014/* Return a pointer to the underlying item array for
1015 an object retured by PySequence_Fast */
Raymond Hettinger42bec932004-03-12 16:38:17 +00001016#define PySequence_Fast_ITEMS(sf) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 (PyList_Check(sf) ? ((PyListObject *)(sf))->ob_item \
1018 : ((PyTupleObject *)(sf))->ob_item)
Raymond Hettingerc1e4f9d2004-03-12 08:04:00 +00001019
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001020/* Return the number of occurrences on value on 'o', that is, return
1021 the number of keys for which o[key] == value.
1022
1023 On failure, return -1. This is equivalent to the Python expression:
1024 o.count(value). */
Victor Stinner2a358f82016-12-06 16:55:39 +01001025PyAPI_FUNC(Py_ssize_t) PySequence_Count(PyObject *o, PyObject *value);
Guido van Rossuma8275371995-07-18 14:07:00 +00001026
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001027/* Return 1 if 'ob' is in the sequence 'seq'; 0 if 'ob' is not in the sequence
1028 'seq'; -1 on error.
Guido van Rossuma8275371995-07-18 14:07:00 +00001029
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001030 Use __contains__ if possible, else _PySequence_IterSearch(). */
Victor Stinner2a358f82016-12-06 16:55:39 +01001031PyAPI_FUNC(int) PySequence_Contains(PyObject *seq, PyObject *ob);
Tim Peterscb8d3682001-05-05 21:05:01 +00001032
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001033#ifndef Py_LIMITED_API
Tim Peters16a77ad2001-09-08 04:00:12 +00001034#define PY_ITERSEARCH_COUNT 1
1035#define PY_ITERSEARCH_INDEX 2
1036#define PY_ITERSEARCH_CONTAINS 3
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001037
1038/* Iterate over seq.
1039
1040 Result depends on the operation:
1041
1042 PY_ITERSEARCH_COUNT: return # of times obj appears in seq; -1 if
1043 error.
1044 PY_ITERSEARCH_INDEX: return 0-based index of first occurrence of
1045 obj in seq; set ValueError and return -1 if none found;
1046 also return -1 on error.
1047 PY_ITERSEARCH_CONTAINS: return 1 if obj in seq, else 0; -1 on
1048 error. */
Victor Stinner2a358f82016-12-06 16:55:39 +01001049PyAPI_FUNC(Py_ssize_t) _PySequence_IterSearch(PyObject *seq,
1050 PyObject *obj, int operation);
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001051#endif
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001052
Guido van Rossum83684531999-03-17 18:44:39 +00001053
1054/* For DLL-level backwards compatibility */
1055#undef PySequence_In
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001056/* Determine if the sequence 'o' contains 'value'. If an item in 'o' is equal
1057 to 'value', return 1, otherwise return 0. On error, return -1.
1058
1059 This is equivalent to the Python expression: value in o. */
Victor Stinner2a358f82016-12-06 16:55:39 +01001060PyAPI_FUNC(int) PySequence_In(PyObject *o, PyObject *value);
Guido van Rossum83684531999-03-17 18:44:39 +00001061
1062/* For source-level backwards compatibility */
Guido van Rossumf1536db1998-08-23 22:06:59 +00001063#define PySequence_In PySequence_Contains
Guido van Rossuma8275371995-07-18 14:07:00 +00001064
Guido van Rossuma8275371995-07-18 14:07:00 +00001065
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001066/* Return the first index for which o[i] == value.
1067 On error, return -1.
1068
1069 This is equivalent to the Python expression: o.index(value). */
Victor Stinner2a358f82016-12-06 16:55:39 +01001070PyAPI_FUNC(Py_ssize_t) PySequence_Index(PyObject *o, PyObject *value);
Guido van Rossuma8275371995-07-18 14:07:00 +00001071
Victor Stinner2a358f82016-12-06 16:55:39 +01001072
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001073/* --- In-place versions of some of the above Sequence functions --- */
Guido van Rossuma8275371995-07-18 14:07:00 +00001074
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001075/* Append sequence 'o2' to sequence 'o1', in-place when possible. Return the
1076 resulting object, which could be 'o1', or NULL on failure.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +00001077
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001078 This is the equivalent of the Python expression: o1 += o2. */
Victor Stinner2a358f82016-12-06 16:55:39 +01001079PyAPI_FUNC(PyObject *) PySequence_InPlaceConcat(PyObject *o1, PyObject *o2);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +00001080
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001081/* Repeat sequence 'o' by 'count', in-place when possible. Return the resulting
1082 object, which could be 'o', or NULL on failure.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +00001083
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001084 This is the equivalent of the Python expression: o1 *= count. */
Victor Stinner2a358f82016-12-06 16:55:39 +01001085PyAPI_FUNC(PyObject *) PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +00001086
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +00001087
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001088/* === Mapping protocol ================================================= */
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +00001089
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001090/* Return 1 if the object provides mapping protocol, and 0 otherwise.
Guido van Rossuma8275371995-07-18 14:07:00 +00001091
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001092 This function always succeeds. */
Victor Stinner2a358f82016-12-06 16:55:39 +01001093PyAPI_FUNC(int) PyMapping_Check(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +00001094
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001095/* Returns the number of keys in mapping object 'o' on success, and -1 on
1096 failure. For objects that do not provide sequence protocol, this is
1097 equivalent to the Python expression: len(o). */
Victor Stinner2a358f82016-12-06 16:55:39 +01001098PyAPI_FUNC(Py_ssize_t) PyMapping_Size(PyObject *o);
Jeremy Hylton6253f832000-07-12 12:56:19 +00001099
Victor Stinner2a358f82016-12-06 16:55:39 +01001100/* For DLL compatibility */
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00001101#undef PyMapping_Length
Victor Stinner2a358f82016-12-06 16:55:39 +01001102PyAPI_FUNC(Py_ssize_t) PyMapping_Length(PyObject *o);
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00001103#define PyMapping_Length PyMapping_Size
1104
1105
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001106/* Implemented as a macro:
Guido van Rossuma25e5e91996-09-06 13:48:38 +00001107
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001108 int PyMapping_DelItemString(PyObject *o, const char *key);
Guido van Rossuma8275371995-07-18 14:07:00 +00001109
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001110 Remove the mapping for object 'key' from the mapping 'o'. Returns -1 on
1111 failure.
1112
1113 This is equivalent to the Python statement: del o[key]. */
Jeremy Hylton7c7ee5f2001-11-28 16:20:07 +00001114#define PyMapping_DelItemString(O,K) PyObject_DelItemString((O),(K))
Guido van Rossuma25e5e91996-09-06 13:48:38 +00001115
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001116/* Implemented as a macro:
Guido van Rossuma8275371995-07-18 14:07:00 +00001117
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001118 int PyMapping_DelItem(PyObject *o, PyObject *key);
Guido van Rossuma8275371995-07-18 14:07:00 +00001119
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001120 Remove the mapping for object 'key' from the mapping object 'o'.
1121 Returns -1 on failure.
1122
1123 This is equivalent to the Python statement: del o[key]. */
Jeremy Hylton7c7ee5f2001-11-28 16:20:07 +00001124#define PyMapping_DelItem(O,K) PyObject_DelItem((O),(K))
Guido van Rossuma8275371995-07-18 14:07:00 +00001125
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001126/* On success, return 1 if the mapping object 'o' has the key 'key',
1127 and 0 otherwise.
1128
1129 This is equivalent to the Python expression: key in o.
1130
1131 This function always succeeds. */
Victor Stinner2a358f82016-12-06 16:55:39 +01001132PyAPI_FUNC(int) PyMapping_HasKeyString(PyObject *o, const char *key);
Guido van Rossuma8275371995-07-18 14:07:00 +00001133
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001134/* Return 1 if the mapping object has the key 'key', and 0 otherwise.
Guido van Rossuma8275371995-07-18 14:07:00 +00001135
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001136 This is equivalent to the Python expression: key in o.
Guido van Rossuma8275371995-07-18 14:07:00 +00001137
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001138 This function always succeeds. */
Victor Stinner2a358f82016-12-06 16:55:39 +01001139PyAPI_FUNC(int) PyMapping_HasKey(PyObject *o, PyObject *key);
Guido van Rossuma8275371995-07-18 14:07:00 +00001140
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001141/* On success, return a list or tuple of the keys in mapping object 'o'.
1142 On failure, return NULL. */
Victor Stinner2a358f82016-12-06 16:55:39 +01001143PyAPI_FUNC(PyObject *) PyMapping_Keys(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +00001144
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001145/* On success, return a list or tuple of the values in mapping object 'o'.
1146 On failure, return NULL. */
Victor Stinner2a358f82016-12-06 16:55:39 +01001147PyAPI_FUNC(PyObject *) PyMapping_Values(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +00001148
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001149/* On success, return a list or tuple of the items in mapping object 'o',
1150 where each item is a tuple containing a key-value pair. On failure, return
1151 NULL. */
Victor Stinner2a358f82016-12-06 16:55:39 +01001152PyAPI_FUNC(PyObject *) PyMapping_Items(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +00001153
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001154/* Return element of o corresponding to the object, key, or NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +00001155
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001156 This is the equivalent of the Python expression: o[key]. */
Victor Stinner2a358f82016-12-06 16:55:39 +01001157PyAPI_FUNC(PyObject *) PyMapping_GetItemString(PyObject *o,
1158 const char *key);
Guido van Rossuma8275371995-07-18 14:07:00 +00001159
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001160/* Map the object 'key' to the value 'v' in the mapping 'o'.
1161 Returns -1 on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +00001162
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001163 This is the equivalent of the Python statement: o[key]=v. */
Victor Stinner2a358f82016-12-06 16:55:39 +01001164PyAPI_FUNC(int) PyMapping_SetItemString(PyObject *o, const char *key,
1165 PyObject *value);
Guido van Rossuma8275371995-07-18 14:07:00 +00001166
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001167/* isinstance(object, typeorclass) */
Mark Hammond91a681d2002-08-12 07:21:58 +00001168PyAPI_FUNC(int) PyObject_IsInstance(PyObject *object, PyObject *typeorclass);
Guido van Rossum823649d2001-03-21 18:40:58 +00001169
Victor Stinner5bef7cd2016-12-15 09:14:25 +01001170/* issubclass(object, typeorclass) */
Mark Hammond91a681d2002-08-12 07:21:58 +00001171PyAPI_FUNC(int) PyObject_IsSubclass(PyObject *object, PyObject *typeorclass);
Guido van Rossum823649d2001-03-21 18:40:58 +00001172
1173
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001174#ifndef Py_LIMITED_API
Antoine Pitrouec569b72008-08-26 22:40:48 +00001175PyAPI_FUNC(int) _PyObject_RealIsInstance(PyObject *inst, PyObject *cls);
1176
1177PyAPI_FUNC(int) _PyObject_RealIsSubclass(PyObject *derived, PyObject *cls);
1178
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001179PyAPI_FUNC(char *const *) _PySequence_BytesToCharpArray(PyObject* self);
1180
1181PyAPI_FUNC(void) _Py_FreeCharPArray(char *const array[]);
Antoine Pitrouec569b72008-08-26 22:40:48 +00001182
Antoine Pitrouf68c2a72010-09-01 12:58:21 +00001183/* For internal use by buffer API functions */
1184PyAPI_FUNC(void) _Py_add_one_to_index_F(int nd, Py_ssize_t *index,
1185 const Py_ssize_t *shape);
1186PyAPI_FUNC(void) _Py_add_one_to_index_C(int nd, Py_ssize_t *index,
1187 const Py_ssize_t *shape);
Serhiy Storchaka9fab79b2016-09-11 11:03:14 +03001188#endif /* !Py_LIMITED_API */
Antoine Pitrouf68c2a72010-09-01 12:58:21 +00001189
1190
Guido van Rossum8ca687a1995-09-18 21:20:02 +00001191#ifdef __cplusplus
1192}
1193#endif
Guido van Rossuma8275371995-07-18 14:07:00 +00001194#endif /* Py_ABSTRACTOBJECT_H */