blob: 95f6569f9becac14b6f8fe9a34185ceda5e83344 [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
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007#ifdef PY_SSIZE_T_CLEAN
8#define PyObject_CallFunction _PyObject_CallFunction_SizeT
9#define PyObject_CallMethod _PyObject_CallMethod_SizeT
Serhiy Storchaka9fab79b2016-09-11 11:03:14 +030010#ifndef Py_LIMITED_API
Martin v. Löwisafe55bb2011-10-09 10:38:36 +020011#define _PyObject_CallMethodId _PyObject_CallMethodId_SizeT
Serhiy Storchaka9fab79b2016-09-11 11:03:14 +030012#endif /* !Py_LIMITED_API */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000013#endif
14
Guido van Rossuma8275371995-07-18 14:07:00 +000015/* Abstract Object Interface (many thanks to Jim Fulton) */
16
17/*
18 PROPOSAL: A Generic Python Object Interface for Python C Modules
19
20Problem
21
22 Python modules written in C that must access Python objects must do
23 so through routines whose interfaces are described by a set of
24 include files. Unfortunately, these routines vary according to the
25 object accessed. To use these routines, the C programmer must check
26 the type of the object being used and must call a routine based on
27 the object type. For example, to access an element of a sequence,
28 the programmer must determine whether the sequence is a list or a
29 tuple:
30
31 if(is_tupleobject(o))
32 e=gettupleitem(o,i)
33 else if(is_listitem(o))
34 e=getlistitem(o,i)
35
36 If the programmer wants to get an item from another type of object
37 that provides sequence behavior, there is no clear way to do it
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000038 correctly.
Guido van Rossuma8275371995-07-18 14:07:00 +000039
40 The persistent programmer may peruse object.h and find that the
41 _typeobject structure provides a means of invoking up to (currently
42 about) 41 special operators. So, for example, a routine can get an
43 item from any object that provides sequence behavior. However, to
44 use this mechanism, the programmer must make their code dependent on
45 the current Python implementation.
46
47 Also, certain semantics, especially memory management semantics, may
48 differ by the type of object being used. Unfortunately, these
49 semantics are not clearly described in the current include files.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000050 An abstract interface providing more consistent semantics is needed.
Guido van Rossuma8275371995-07-18 14:07:00 +000051
52Proposal
53
54 I propose the creation of a standard interface (with an associated
55 library of routines and/or macros) for generically obtaining the
56 services of Python objects. This proposal can be viewed as one
57 components of a Python C interface consisting of several components.
58
Raymond Hettingera72e2f92003-02-28 05:11:03 +000059 From the viewpoint of C access to Python services, we have (as
Guido van Rossuma8275371995-07-18 14:07:00 +000060 suggested by Guido in off-line discussions):
61
62 - "Very high level layer": two or three functions that let you exec or
63 eval arbitrary Python code given as a string in a module whose name is
64 given, passing C values in and getting C values out using
65 mkvalue/getargs style format strings. This does not require the user
66 to declare any variables of type "PyObject *". This should be enough
67 to write a simple application that gets Python code from the user,
68 execs it, and returns the output or errors. (Error handling must also
69 be part of this API.)
70
71 - "Abstract objects layer": which is the subject of this proposal.
72 It has many functions operating on objects, and lest you do many
73 things from C that you can also write in Python, without going
74 through the Python parser.
75
76 - "Concrete objects layer": This is the public type-dependent
77 interface provided by the standard built-in types, such as floats,
78 strings, and lists. This interface exists and is currently
Raymond Hettingera72e2f92003-02-28 05:11:03 +000079 documented by the collection of include files provided with the
Guido van Rossuma8275371995-07-18 14:07:00 +000080 Python distributions.
81
82 From the point of view of Python accessing services provided by C
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000083 modules:
Guido van Rossuma8275371995-07-18 14:07:00 +000084
85 - "Python module interface": this interface consist of the basic
86 routines used to define modules and their members. Most of the
87 current extensions-writing guide deals with this interface.
88
89 - "Built-in object interface": this is the interface that a new
90 built-in type must provide and the mechanisms and rules that a
91 developer of a new built-in type must use and follow.
92
93 This proposal is a "first-cut" that is intended to spur
94 discussion. See especially the lists of notes.
95
96 The Python C object interface will provide four protocols: object,
97 numeric, sequence, and mapping. Each protocol consists of a
98 collection of related operations. If an operation that is not
99 provided by a particular type is invoked, then a standard exception,
Martin Panter7462b6492015-11-02 03:37:02 +0000100 NotImplementedError is raised with an operation name as an argument.
Guido van Rossuma8275371995-07-18 14:07:00 +0000101 In addition, for convenience this interface defines a set of
102 constructors for building objects of built-in types. This is needed
103 so new objects can be returned from C functions that otherwise treat
104 objects generically.
105
106Memory Management
107
108 For all of the functions described in this proposal, if a function
109 retains a reference to a Python object passed as an argument, then the
110 function will increase the reference count of the object. It is
111 unnecessary for the caller to increase the reference count of an
112 argument in anticipation of the object's retention.
113
114 All Python objects returned from functions should be treated as new
115 objects. Functions that return objects assume that the caller will
116 retain a reference and the reference count of the object has already
117 been incremented to account for this fact. A caller that does not
118 retain a reference to an object that is returned from a function
119 must decrement the reference count of the object (using
120 DECREF(object)) to prevent memory leaks.
121
122 Note that the behavior mentioned here is different from the current
123 behavior for some objects (e.g. lists and tuples) when certain
124 type-specific routines are called directly (e.g. setlistitem). The
125 proposed abstraction layer will provide a consistent memory
126 management interface, correcting for inconsistent behavior for some
127 built-in types.
128
129Protocols
130
131xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
132
133/* Object Protocol: */
134
135 /* Implemented elsewhere:
136
137 int PyObject_Print(PyObject *o, FILE *fp, int flags);
138
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 Print an object, o, on file, fp. Returns -1 on
140 error. The flags argument is used to enable certain printing
141 options. The only option currently supported is Py_Print_RAW.
Guido van Rossuma8275371995-07-18 14:07:00 +0000142
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000143 (What should be said about Py_Print_RAW?)
Guido van Rossuma8275371995-07-18 14:07:00 +0000144
145 */
146
147 /* Implemented elsewhere:
148
Serhiy Storchakac6792272013-10-19 21:03:34 +0300149 int PyObject_HasAttrString(PyObject *o, const char *attr_name);
Guido van Rossuma8275371995-07-18 14:07:00 +0000150
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 Returns 1 if o has the attribute attr_name, and 0 otherwise.
152 This is equivalent to the Python expression:
153 hasattr(o,attr_name).
Guido van Rossuma8275371995-07-18 14:07:00 +0000154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155 This function always succeeds.
Guido van Rossuma8275371995-07-18 14:07:00 +0000156
157 */
158
159 /* Implemented elsewhere:
160
Serhiy Storchakac6792272013-10-19 21:03:34 +0300161 PyObject* PyObject_GetAttrString(PyObject *o, const char *attr_name);
Guido van Rossuma8275371995-07-18 14:07:00 +0000162
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163 Retrieve an attributed named attr_name form object o.
164 Returns the attribute value on success, or NULL on failure.
165 This is the equivalent of the Python expression: o.attr_name.
Guido van Rossuma8275371995-07-18 14:07:00 +0000166
167 */
168
169 /* Implemented elsewhere:
170
171 int PyObject_HasAttr(PyObject *o, PyObject *attr_name);
172
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173 Returns 1 if o has the attribute attr_name, and 0 otherwise.
174 This is equivalent to the Python expression:
175 hasattr(o,attr_name).
Guido van Rossuma8275371995-07-18 14:07:00 +0000176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177 This function always succeeds.
Guido van Rossuma8275371995-07-18 14:07:00 +0000178
179 */
180
181 /* Implemented elsewhere:
182
183 PyObject* PyObject_GetAttr(PyObject *o, PyObject *attr_name);
184
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185 Retrieve an attributed named attr_name form object o.
186 Returns the attribute value on success, or NULL on failure.
187 This is the equivalent of the Python expression: o.attr_name.
Guido van Rossuma8275371995-07-18 14:07:00 +0000188
189 */
190
191
192 /* Implemented elsewhere:
193
Serhiy Storchakac6792272013-10-19 21:03:34 +0300194 int PyObject_SetAttrString(PyObject *o, const char *attr_name, PyObject *v);
Guido van Rossuma8275371995-07-18 14:07:00 +0000195
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000196 Set the value of the attribute named attr_name, for object o,
Martin Panter45be8d62015-12-08 00:03:20 +0000197 to the value v. Raise an exception and return -1 on failure; return 0 on
198 success. This is the equivalent of the Python statement o.attr_name=v.
Guido van Rossuma8275371995-07-18 14:07:00 +0000199
200 */
201
202 /* Implemented elsewhere:
203
204 int PyObject_SetAttr(PyObject *o, PyObject *attr_name, PyObject *v);
205
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 Set the value of the attribute named attr_name, for object o,
Martin Panter45be8d62015-12-08 00:03:20 +0000207 to the value v. Raise an exception and return -1 on failure; return 0 on
208 success. This is the equivalent of the Python statement o.attr_name=v.
Guido van Rossuma8275371995-07-18 14:07:00 +0000209
210 */
211
212 /* implemented as a macro:
213
Serhiy Storchakac6792272013-10-19 21:03:34 +0300214 int PyObject_DelAttrString(PyObject *o, const char *attr_name);
Guido van Rossuma8275371995-07-18 14:07:00 +0000215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 Delete attribute named attr_name, for object o. Returns
217 -1 on failure. This is the equivalent of the Python
218 statement: del o.attr_name.
Guido van Rossuma8275371995-07-18 14:07:00 +0000219
220 */
221#define PyObject_DelAttrString(O,A) PyObject_SetAttrString((O),(A),NULL)
222
223 /* implemented as a macro:
224
225 int PyObject_DelAttr(PyObject *o, PyObject *attr_name);
226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227 Delete attribute named attr_name, for object o. Returns -1
228 on failure. This is the equivalent of the Python
229 statement: del o.attr_name.
Guido van Rossuma8275371995-07-18 14:07:00 +0000230
231 */
232#define PyObject_DelAttr(O,A) PyObject_SetAttr((O),(A),NULL)
233
Guido van Rossuma8275371995-07-18 14:07:00 +0000234 /* Implemented elsewhere:
235
236 PyObject *PyObject_Repr(PyObject *o);
237
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 Compute the string representation of object, o. Returns the
239 string representation on success, NULL on failure. This is
240 the equivalent of the Python expression: repr(o).
Guido van Rossuma8275371995-07-18 14:07:00 +0000241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 Called by the repr() built-in function.
Guido van Rossuma8275371995-07-18 14:07:00 +0000243
244 */
245
246 /* Implemented elsewhere:
247
248 PyObject *PyObject_Str(PyObject *o);
249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 Compute the string representation of object, o. Returns the
251 string representation on success, NULL on failure. This is
252 the equivalent of the Python expression: str(o).)
Guido van Rossuma8275371995-07-18 14:07:00 +0000253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 Called by the str() and print() built-in functions.
Marc-André Lemburgad7c98e2001-01-17 17:09:53 +0000255
256 */
257
Thomas Wouters89f507f2006-12-13 04:49:30 +0000258 /* Declared elsewhere
259
Victor Stinner8be1c392016-11-30 12:10:54 +0100260 PyAPI_FUNC(int) PyCallable_Check(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000261
Victor Stinner8be1c392016-11-30 12:10:54 +0100262 Determine if the object, o, is callable. Return 1 if the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 object is callable and 0 otherwise.
Guido van Rossuma8275371995-07-18 14:07:00 +0000264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 This function always succeeds.
Guido van Rossuma8275371995-07-18 14:07:00 +0000266 */
267
Victor Stinner2d0eb652016-12-06 16:27:24 +0100268 /* Call a callable Python object 'callable' with arguments given by the
269 tuple 'args' and keywords arguments given by the dictionary 'kwargs'.
Tim Peters6d6c1a32001-08-02 04:15:00 +0000270
Victor Stinner2d0eb652016-12-06 16:27:24 +0100271 'args' must not be *NULL*, use an empty tuple if no arguments are
272 needed. If no named arguments are needed, 'kwargs' can be NULL.
273
274 This is the equivalent of the Python expression:
275 callable(*args, **kwargs). */
276 PyAPI_FUNC(PyObject *) PyObject_Call(PyObject *callable,
277 PyObject *args, PyObject *kwargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278
Victor Stinner4a7cc882015-03-06 23:35:27 +0100279#ifndef Py_LIMITED_API
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700280 PyAPI_FUNC(PyObject*) _PyStack_AsTuple(
281 PyObject **stack,
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200282 Py_ssize_t nargs);
283
Victor Stinner57f91ac2016-09-12 13:37:07 +0200284 /* Convert keyword arguments from the (stack, kwnames) format to a Python
285 dictionary.
286
287 kwnames must only contains str strings, no subclass, and all keys must
288 be unique. kwnames is not checked, usually these checks are done before or later
289 calling _PyStack_AsDict(). For example, _PyArg_ParseStack() raises an
290 error if a key is not a string. */
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700291 PyAPI_FUNC(PyObject *) _PyStack_AsDict(
292 PyObject **values,
Victor Stinnerb8d768b2016-09-12 13:30:02 +0200293 PyObject *kwnames);
Victor Stinnerae8b69c2016-09-09 14:07:44 -0700294
Victor Stinnera9efb2f2016-09-09 17:40:22 -0700295 /* Convert (args, nargs, kwargs) into a (stack, nargs, kwnames).
296
297 Return a new stack which should be released by PyMem_Free(), or return
298 args unchanged if kwargs is NULL or an empty dictionary.
299
300 The stack uses borrowed references.
301
302 The type of keyword keys is not checked, these checks should be done
303 later (ex: _PyArg_ParseStack). */
304 PyAPI_FUNC(PyObject **) _PyStack_UnpackDict(
305 PyObject **args,
306 Py_ssize_t nargs,
307 PyObject *kwargs,
308 PyObject **kwnames,
309 PyObject *func);
310
Victor Stinner2d0eb652016-12-06 16:27:24 +0100311 /* Call the callable object 'callable' with the "fast call" calling convention:
Victor Stinner57f91ac2016-09-12 13:37:07 +0200312 args is a C array for positional arguments (nargs is the number of
313 positional arguments), kwargs is a dictionary for keyword arguments.
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200314
Victor Stinner57f91ac2016-09-12 13:37:07 +0200315 If nargs is equal to zero, args can be NULL. kwargs can be NULL.
316 nargs must be greater or equal to zero.
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200317
Victor Stinner57f91ac2016-09-12 13:37:07 +0200318 Return the result on success. Raise an exception on return NULL on
319 error. */
Victor Stinner2d0eb652016-12-06 16:27:24 +0100320 PyAPI_FUNC(PyObject *) _PyObject_FastCallDict(PyObject *callable,
Victor Stinner57f91ac2016-09-12 13:37:07 +0200321 PyObject **args, Py_ssize_t nargs,
322 PyObject *kwargs);
Victor Stinner559bb6a2016-08-22 22:48:54 +0200323
Victor Stinner2d0eb652016-12-06 16:27:24 +0100324 /* Call the callable object 'callable' with the "fast call" calling convention:
Victor Stinner57f91ac2016-09-12 13:37:07 +0200325 args is a C array for positional arguments followed by values of
326 keyword arguments. Keys of keyword arguments are stored as a tuple
327 of strings in kwnames. nargs is the number of positional parameters at
328 the beginning of stack. The size of kwnames gives the number of keyword
329 values in the stack after positional arguments.
Victor Stinnerd8735722016-09-09 12:36:44 -0700330
Victor Stinner57f91ac2016-09-12 13:37:07 +0200331 kwnames must only contains str strings, no subclass, and all keys must
332 be unique.
Victor Stinnerd8735722016-09-09 12:36:44 -0700333
Victor Stinner57f91ac2016-09-12 13:37:07 +0200334 If nargs is equal to zero and there is no keyword argument (kwnames is
335 NULL or its size is zero), args can be NULL.
336
337 Return the result on success. Raise an exception and return NULL on
338 error. */
339 PyAPI_FUNC(PyObject *) _PyObject_FastCallKeywords
Victor Stinner2d0eb652016-12-06 16:27:24 +0100340 (PyObject *callable,
Victor Stinner57f91ac2016-09-12 13:37:07 +0200341 PyObject **args,
342 Py_ssize_t nargs,
343 PyObject *kwnames);
Victor Stinnerd8735722016-09-09 12:36:44 -0700344
Victor Stinner559bb6a2016-08-22 22:48:54 +0200345#define _PyObject_FastCall(func, args, nargs) \
346 _PyObject_FastCallDict((func), (args), (nargs), NULL)
347
348#define _PyObject_CallNoArg(func) \
Victor Stinner7bfb42d2016-12-05 17:04:32 +0100349 _PyObject_FastCallDict((func), NULL, 0, NULL)
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200350
Victor Stinner2d0eb652016-12-06 16:27:24 +0100351 PyAPI_FUNC(PyObject *) _PyObject_Call_Prepend(PyObject *callable,
Victor Stinner8be1c392016-11-30 12:10:54 +0100352 PyObject *obj, PyObject *args,
Victor Stinner3f1057a2016-08-25 01:04:14 +0200353 PyObject *kwargs);
354
Victor Stinner2d0eb652016-12-06 16:27:24 +0100355 PyAPI_FUNC(PyObject *) _Py_CheckFunctionResult(PyObject *callable,
Victor Stinnerefde1462015-03-21 15:04:43 +0100356 PyObject *result,
357 const char *where);
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200358#endif /* Py_LIMITED_API */
Victor Stinner4a7cc882015-03-06 23:35:27 +0100359
Victor Stinner2d0eb652016-12-06 16:27:24 +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*.
362
363 Returns the result of the call on success, or *NULL* on failure.
364
365 This is the equivalent of the Python expression:
366 callable(*args) */
367 PyAPI_FUNC(PyObject *) PyObject_CallObject(PyObject *callable,
Neal Norwitzfe554642006-03-17 06:58:45 +0000368 PyObject *args);
Guido van Rossuma8275371995-07-18 14:07:00 +0000369
Victor Stinner2d0eb652016-12-06 16:27:24 +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.
Guido van Rossuma8275371995-07-18 14:07:00 +0000373
Victor Stinner2d0eb652016-12-06 16:27:24 +0100374 The format may be NULL, indicating that no arguments are provided.
375
376 Returns the result of the call on success, or NULL on failure.
377
378 This is the equivalent of the Python expression:
379 callable(arg1, arg2, ...) */
380 PyAPI_FUNC(PyObject *) PyObject_CallFunction(PyObject *callable,
Serhiy Storchaka1cfebc72013-05-29 18:50:54 +0300381 const char *format, ...);
Guido van Rossuma8275371995-07-18 14:07:00 +0000382
Victor Stinner2d0eb652016-12-06 16:27:24 +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 Stinner2d0eb652016-12-06 16:27:24 +0100386 The format can be NULL, indicating that no arguments are provided.
Guido van Rossuma8275371995-07-18 14:07:00 +0000387
Victor Stinner2d0eb652016-12-06 16:27:24 +0100388 Returns the result of the call on success, or NULL on failure.
389
390 This is the equivalent of the Python expression:
391 obj.name(arg1, arg2, ...) */
392 PyAPI_FUNC(PyObject *) PyObject_CallMethod(PyObject *obj,
393 const char *name,
Serhiy Storchaka1cfebc72013-05-29 18:50:54 +0300394 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 Stinner2d0eb652016-12-06 16:27:24 +0100397 PyAPI_FUNC(PyObject *) _PyObject_CallMethodId(PyObject *obj,
398 _Py_Identifier *name,
Serhiy Storchaka1cfebc72013-05-29 18:50:54 +0300399 const char *format, ...);
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200400
401 /*
402 Like PyObject_CallMethod, but expect a _Py_Identifier* as the
403 method name.
404 */
Serhiy Storchaka9fab79b2016-09-11 11:03:14 +0300405#endif /* !Py_LIMITED_API */
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200406
Victor Stinner8be1c392016-11-30 12:10:54 +0100407 PyAPI_FUNC(PyObject *) _PyObject_CallFunction_SizeT(PyObject *callable,
Serhiy Storchaka1cfebc72013-05-29 18:50:54 +0300408 const char *format,
409 ...);
Victor Stinner2d0eb652016-12-06 16:27:24 +0100410 PyAPI_FUNC(PyObject *) _PyObject_CallMethod_SizeT(PyObject *obj,
Victor Stinner8be1c392016-11-30 12:10:54 +0100411 const char *name,
Serhiy Storchaka1cfebc72013-05-29 18:50:54 +0300412 const char *format,
413 ...);
Serhiy Storchaka9fab79b2016-09-11 11:03:14 +0300414#ifndef Py_LIMITED_API
Victor Stinner2d0eb652016-12-06 16:27:24 +0100415 PyAPI_FUNC(PyObject *) _PyObject_CallMethodId_SizeT(PyObject *obj,
416 _Py_Identifier *name,
417 const char *format,
418 ...);
Serhiy Storchaka9fab79b2016-09-11 11:03:14 +0300419#endif /* !Py_LIMITED_API */
Guido van Rossuma8275371995-07-18 14:07:00 +0000420
Victor Stinner2d0eb652016-12-06 16:27:24 +0100421 /* Call a callable Python object 'callable' with a variable number of C
422 arguments. The C arguments are provided as PyObject* values, terminated
423 by a NULL.
424
425 Returns the result of the call on success, or NULL on failure.
426
427 This is the equivalent of the Python expression:
428 callable(arg1, arg2, ...) */
Victor Stinner8be1c392016-11-30 12:10:54 +0100429 PyAPI_FUNC(PyObject *) PyObject_CallFunctionObjArgs(PyObject *callable,
Neal Norwitzfe554642006-03-17 06:58:45 +0000430 ...);
Fred Drakeb421b8c2001-10-26 16:21:32 +0000431
432 /*
Victor Stinner2d0eb652016-12-06 16:27:24 +0100433 Call the method named 'name' of object 'obj' with a variable number of
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 C arguments. The C arguments are provided as PyObject *
435 values, terminated by NULL. Returns the result of the call
436 on success, or NULL on failure. This is the equivalent of
Victor Stinner2d0eb652016-12-06 16:27:24 +0100437 the Python expression: obj.name(args).
Guido van Rossuma8275371995-07-18 14:07:00 +0000438 */
439
Victor Stinner2d0eb652016-12-06 16:27:24 +0100440 PyAPI_FUNC(PyObject *) PyObject_CallMethodObjArgs(PyObject *obj,
441 PyObject *name,
442 ...);
443#ifndef Py_LIMITED_API
444 PyAPI_FUNC(PyObject *) _PyObject_CallMethodIdObjArgs(PyObject *obj,
445 struct _Py_Identifier *name,
446 ...);
447#endif /* !Py_LIMITED_API */
448
449
Guido van Rossuma8275371995-07-18 14:07:00 +0000450
451 /* Implemented elsewhere:
452
453 long PyObject_Hash(PyObject *o);
454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 Compute and return the hash, hash_value, of an object, o. On
456 failure, return -1. This is the equivalent of the Python
457 expression: hash(o).
Guido van Rossuma8275371995-07-18 14:07:00 +0000458 */
459
460
461 /* Implemented elsewhere:
462
463 int PyObject_IsTrue(PyObject *o);
464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 Returns 1 if the object, o, is considered to be true, 0 if o is
466 considered to be false and -1 on failure. This is equivalent to the
467 Python expression: not not o
Guido van Rossuma8275371995-07-18 14:07:00 +0000468 */
469
Guido van Rossumc3d3f961998-04-09 17:53:59 +0000470 /* Implemented elsewhere:
471
472 int PyObject_Not(PyObject *o);
473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 Returns 0 if the object, o, is considered to be true, 1 if o is
475 considered to be false and -1 on failure. This is equivalent to the
476 Python expression: not o
Guido van Rossumc3d3f961998-04-09 17:53:59 +0000477 */
478
Mark Hammond91a681d2002-08-12 07:21:58 +0000479 PyAPI_FUNC(PyObject *) PyObject_Type(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000480
481 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 On success, returns a type object corresponding to the object
483 type of object o. On failure, returns NULL. This is
484 equivalent to the Python expression: type(o).
Guido van Rossuma8275371995-07-18 14:07:00 +0000485 */
486
Martin v. Löwis18e16552006-02-15 17:27:45 +0000487 PyAPI_FUNC(Py_ssize_t) PyObject_Size(PyObject *o);
Jeremy Hylton6253f832000-07-12 12:56:19 +0000488
Guido van Rossuma8275371995-07-18 14:07:00 +0000489 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 Return the size of object o. If the object, o, provides
491 both sequence and mapping protocols, the sequence size is
492 returned. On error, -1 is returned. This is the equivalent
493 to the Python expression: len(o).
Guido van Rossuma8275371995-07-18 14:07:00 +0000494 */
495
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +0000496 /* For DLL compatibility */
497#undef PyObject_Length
Martin v. Löwis18e16552006-02-15 17:27:45 +0000498 PyAPI_FUNC(Py_ssize_t) PyObject_Length(PyObject *o);
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +0000499#define PyObject_Length PyObject_Size
500
Armin Ronacher74b38b12012-10-07 10:29:32 +0200501#ifndef Py_LIMITED_API
502 PyAPI_FUNC(int) _PyObject_HasLen(PyObject *o);
Martin v. Löwis1c0689c2014-01-03 21:36:49 +0100503 PyAPI_FUNC(Py_ssize_t) PyObject_LengthHint(PyObject *o, Py_ssize_t);
Armin Ronacher74b38b12012-10-07 10:29:32 +0200504#endif
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000505
506 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 Guess the size of object o using len(o) or o.__length_hint__().
508 If neither of those return a non-negative value, then return the
509 default value. If one of the calls fails, this function returns -1.
Raymond Hettinger6b27cda2005-09-24 21:23:05 +0000510 */
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +0000511
Mark Hammond91a681d2002-08-12 07:21:58 +0000512 PyAPI_FUNC(PyObject *) PyObject_GetItem(PyObject *o, PyObject *key);
Guido van Rossuma8275371995-07-18 14:07:00 +0000513
514 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 Return element of o corresponding to the object, key, or NULL
516 on failure. This is the equivalent of the Python expression:
517 o[key].
Guido van Rossuma8275371995-07-18 14:07:00 +0000518 */
519
Mark Hammond91a681d2002-08-12 07:21:58 +0000520 PyAPI_FUNC(int) PyObject_SetItem(PyObject *o, PyObject *key, PyObject *v);
Guido van Rossuma8275371995-07-18 14:07:00 +0000521
522 /*
Martin Panter45be8d62015-12-08 00:03:20 +0000523 Map the object key to the value v. Raise an exception and return -1
524 on failure; return 0 on success. This is the equivalent of the Python
525 statement o[key]=v.
Guido van Rossuma8275371995-07-18 14:07:00 +0000526 */
527
Serhiy Storchakac6792272013-10-19 21:03:34 +0300528 PyAPI_FUNC(int) PyObject_DelItemString(PyObject *o, const char *key);
Martin v. Löwisb0d71d02002-01-05 10:50:30 +0000529
530 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 Remove the mapping for object, key, from the object *o.
532 Returns -1 on failure. This is equivalent to
533 the Python statement: del o[key].
Martin v. Löwisb0d71d02002-01-05 10:50:30 +0000534 */
535
Mark Hammond91a681d2002-08-12 07:21:58 +0000536 PyAPI_FUNC(int) PyObject_DelItem(PyObject *o, PyObject *key);
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000537
538 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 Delete the mapping for key from *o. Returns -1 on failure.
540 This is the equivalent of the Python statement: del o[key].
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000541 */
542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 /* old buffer API
544 FIXME: usage of these should all be replaced in Python itself
545 but for backwards compatibility we will implement them.
Raymond Hettinger15f44ab2016-08-30 10:47:49 -0700546 Their usage without a corresponding "unlock" mechanism
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547 may create issues (but they would already be there). */
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000548
Mark Hammond91a681d2002-08-12 07:21:58 +0000549 PyAPI_FUNC(int) PyObject_AsCharBuffer(PyObject *obj,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 const char **buffer,
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +0200551 Py_ssize_t *buffer_len)
552 Py_DEPRECATED(3.0);
Guido van Rossumfd9eed32000-03-10 22:35:06 +0000553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 /*
555 Takes an arbitrary object which must support the (character,
556 single segment) buffer interface and returns a pointer to a
557 read-only memory location useable as character based input
558 for subsequent processing.
Guido van Rossumfd9eed32000-03-10 22:35:06 +0000559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 0 is returned on success. buffer and buffer_len are only
561 set in case no error occurs. Otherwise, -1 is returned and
562 an exception set.
Guido van Rossumfd9eed32000-03-10 22:35:06 +0000563 */
564
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +0200565 PyAPI_FUNC(int) PyObject_CheckReadBuffer(PyObject *obj)
566 Py_DEPRECATED(3.0);
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 /*
569 Checks whether an arbitrary object supports the (character,
570 single segment) buffer interface. Returns 1 on success, 0
571 on failure.
Jeremy Hylton89c3a222001-11-09 21:59:42 +0000572 */
573
Mark Hammond91a681d2002-08-12 07:21:58 +0000574 PyAPI_FUNC(int) PyObject_AsReadBuffer(PyObject *obj,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 const void **buffer,
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +0200576 Py_ssize_t *buffer_len)
577 Py_DEPRECATED(3.0);
Guido van Rossumfd9eed32000-03-10 22:35:06 +0000578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 /*
580 Same as PyObject_AsCharBuffer() except that this API expects
581 (readable, single segment) buffer interface and returns a
582 pointer to a read-only memory location which can contain
583 arbitrary data.
Guido van Rossumfd9eed32000-03-10 22:35:06 +0000584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 0 is returned on success. buffer and buffer_len are only
Ezio Melotti13925002011-03-16 11:05:33 +0200586 set in case no error occurs. Otherwise, -1 is returned and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 an exception set.
Guido van Rossumfd9eed32000-03-10 22:35:06 +0000588 */
589
Mark Hammond91a681d2002-08-12 07:21:58 +0000590 PyAPI_FUNC(int) PyObject_AsWriteBuffer(PyObject *obj,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 void **buffer,
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +0200592 Py_ssize_t *buffer_len)
593 Py_DEPRECATED(3.0);
Guido van Rossumfd9eed32000-03-10 22:35:06 +0000594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 /*
596 Takes an arbitrary object which must support the (writable,
597 single segment) buffer interface and returns a pointer to a
598 writable memory location in buffer of size buffer_len.
Guido van Rossumfd9eed32000-03-10 22:35:06 +0000599
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 0 is returned on success. buffer and buffer_len are only
Ezio Melotti13925002011-03-16 11:05:33 +0200601 set in case no error occurs. Otherwise, -1 is returned and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602 an exception set.
Guido van Rossumfd9eed32000-03-10 22:35:06 +0000603 */
Guido van Rossuma8275371995-07-18 14:07:00 +0000604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 /* new buffer API */
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000606
Martin v. Löwisc83bc3c2011-01-06 19:15:47 +0000607#ifndef Py_LIMITED_API
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000608#define PyObject_CheckBuffer(obj) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000609 (((obj)->ob_type->tp_as_buffer != NULL) && \
610 ((obj)->ob_type->tp_as_buffer->bf_getbuffer != NULL))
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000611
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000612 /* Return 1 if the getbuffer function is available, otherwise
613 return 0 */
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000614
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 PyAPI_FUNC(int) PyObject_GetBuffer(PyObject *obj, Py_buffer *view,
616 int flags);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000617
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 /* This is a C-API version of the getbuffer function call. It checks
619 to make sure object has the required function pointer and issues the
620 call. Returns -1 and raises an error on failure and returns 0 on
621 success
622 */
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000623
624
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000625 PyAPI_FUNC(void *) PyBuffer_GetPointer(Py_buffer *view, Py_ssize_t *indices);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626
627 /* Get the memory area pointed to by the indices for the buffer given.
628 Note that view->ndim is the assumed size of indices
629 */
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000630
631 PyAPI_FUNC(int) PyBuffer_SizeFromFormat(const char *);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000632
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 /* Return the implied itemsize of the data-format area from a
634 struct-style description */
635
636
637
Stefan Krah7d12d9d2012-07-28 12:25:55 +0200638 /* Implementation in memoryobject.c */
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000639 PyAPI_FUNC(int) PyBuffer_ToContiguous(void *buf, Py_buffer *view,
Stefan Krah7d12d9d2012-07-28 12:25:55 +0200640 Py_ssize_t len, char order);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 PyAPI_FUNC(int) PyBuffer_FromContiguous(Py_buffer *view, void *buf,
Stefan Krah7d12d9d2012-07-28 12:25:55 +0200643 Py_ssize_t len, char order);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000644
645
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 /* Copy len bytes of data from the contiguous chunk of memory
647 pointed to by buf into the buffer exported by obj. Return
648 0 on success and return -1 and raise a PyBuffer_Error on
649 error (i.e. the object does not have a buffer interface or
650 it is not working).
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 If fort is 'F', then if the object is multi-dimensional,
653 then the data will be copied into the array in
654 Fortran-style (first dimension varies the fastest). If
655 fort is 'C', then the data will be copied into the array
656 in C-style (last dimension varies the fastest). If fort
657 is 'A', then it does not matter and the copy will be made
658 in whatever way is more efficient.
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 */
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000661
662 PyAPI_FUNC(int) PyObject_CopyData(PyObject *dest, PyObject *src);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663
664 /* Copy the data from the src buffer to the buffer of destination
665 */
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000666
Stefan Krah9a2d99e2012-02-25 12:24:21 +0100667 PyAPI_FUNC(int) PyBuffer_IsContiguous(const Py_buffer *view, char fort);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000668
669
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000670 PyAPI_FUNC(void) PyBuffer_FillContiguousStrides(int ndims,
671 Py_ssize_t *shape,
672 Py_ssize_t *strides,
673 int itemsize,
674 char fort);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000675
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000676 /* Fill the strides array with byte-strides of a contiguous
677 (Fortran-style if fort is 'F' or C-style otherwise)
678 array of the given shape with the given number of bytes
679 per element.
680 */
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000681
Martin v. Löwis423be952008-08-13 15:53:07 +0000682 PyAPI_FUNC(int) PyBuffer_FillInfo(Py_buffer *view, PyObject *o, void *buf,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 Py_ssize_t len, int readonly,
684 int flags);
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000685
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 /* Fills in a buffer-info structure correctly for an exporter
687 that can only share a contiguous chunk of memory of
688 "unsigned bytes" of the given length. Returns 0 on success
689 and -1 (with raising an error) on error.
690 */
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000691
Martin v. Löwis423be952008-08-13 15:53:07 +0000692 PyAPI_FUNC(void) PyBuffer_Release(Py_buffer *view);
693
694 /* Releases a Py_buffer obtained from getbuffer ParseTuple's s*.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 */
Martin v. Löwisc83bc3c2011-01-06 19:15:47 +0000696#endif /* Py_LIMITED_API */
Travis E. Oliphantb99f7622007-08-18 11:21:56 +0000697
Eric Smith8fd3eba2008-02-17 19:48:00 +0000698 PyAPI_FUNC(PyObject *) PyObject_Format(PyObject* obj,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 PyObject *format_spec);
Eric Smith8fd3eba2008-02-17 19:48:00 +0000700 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 Takes an arbitrary object and returns the result of
702 calling obj.__format__(format_spec).
Eric Smith8fd3eba2008-02-17 19:48:00 +0000703 */
704
Guido van Rossum213c7a62001-04-23 14:08:49 +0000705/* Iterators */
706
Mark Hammond91a681d2002-08-12 07:21:58 +0000707 PyAPI_FUNC(PyObject *) PyObject_GetIter(PyObject *);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000708 /* Takes an object and returns an iterator for it.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 This is typically a new iterator but if the argument
710 is an iterator, this returns itself. */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000711
Guido van Rossum213c7a62001-04-23 14:08:49 +0000712#define PyIter_Check(obj) \
Amaury Forgeot d'Arcf343e012009-01-12 23:58:21 +0000713 ((obj)->ob_type->tp_iternext != NULL && \
714 (obj)->ob_type->tp_iternext != &_PyObject_NextNotImplemented)
Guido van Rossum213c7a62001-04-23 14:08:49 +0000715
Mark Hammond91a681d2002-08-12 07:21:58 +0000716 PyAPI_FUNC(PyObject *) PyIter_Next(PyObject *);
Guido van Rossum213c7a62001-04-23 14:08:49 +0000717 /* Takes an iterator object and calls its tp_iternext slot,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 returning the next value. If the iterator is exhausted,
719 this returns NULL without setting an exception.
720 NULL with an exception means an error occurred. */
Guido van Rossum213c7a62001-04-23 14:08:49 +0000721
Guido van Rossuma8275371995-07-18 14:07:00 +0000722/* Number Protocol:*/
723
Mark Hammond91a681d2002-08-12 07:21:58 +0000724 PyAPI_FUNC(int) PyNumber_Check(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000725
726 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 Returns 1 if the object, o, provides numeric protocols, and
728 false otherwise.
Guido van Rossuma8275371995-07-18 14:07:00 +0000729
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 This function always succeeds.
Guido van Rossuma8275371995-07-18 14:07:00 +0000731 */
732
Mark Hammond91a681d2002-08-12 07:21:58 +0000733 PyAPI_FUNC(PyObject *) PyNumber_Add(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000734
735 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 Returns the result of adding o1 and o2, or null on failure.
737 This is the equivalent of the Python expression: o1+o2.
Guido van Rossuma8275371995-07-18 14:07:00 +0000738 */
739
Mark Hammond91a681d2002-08-12 07:21:58 +0000740 PyAPI_FUNC(PyObject *) PyNumber_Subtract(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000741
742 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 Returns the result of subtracting o2 from o1, or null on
744 failure. This is the equivalent of the Python expression:
745 o1-o2.
Guido van Rossuma8275371995-07-18 14:07:00 +0000746 */
747
Mark Hammond91a681d2002-08-12 07:21:58 +0000748 PyAPI_FUNC(PyObject *) PyNumber_Multiply(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000749
750 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000751 Returns the result of multiplying o1 and o2, or null on
752 failure. This is the equivalent of the Python expression:
753 o1*o2.
Guido van Rossuma8275371995-07-18 14:07:00 +0000754 */
755
Benjamin Petersond51374e2014-04-09 23:55:56 -0400756 PyAPI_FUNC(PyObject *) PyNumber_MatrixMultiply(PyObject *o1, PyObject *o2);
757
758 /*
759 This is the equivalent of the Python expression: o1 @ o2.
760 */
761
Mark Hammond91a681d2002-08-12 07:21:58 +0000762 PyAPI_FUNC(PyObject *) PyNumber_FloorDivide(PyObject *o1, PyObject *o2);
Guido van Rossum4668b002001-08-08 05:00:18 +0000763
764 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 Returns the result of dividing o1 by o2 giving an integral result,
766 or null on failure.
767 This is the equivalent of the Python expression: o1//o2.
Guido van Rossum4668b002001-08-08 05:00:18 +0000768 */
769
Mark Hammond91a681d2002-08-12 07:21:58 +0000770 PyAPI_FUNC(PyObject *) PyNumber_TrueDivide(PyObject *o1, PyObject *o2);
Guido van Rossum4668b002001-08-08 05:00:18 +0000771
772 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 Returns the result of dividing o1 by o2 giving a float result,
774 or null on failure.
775 This is the equivalent of the Python expression: o1/o2.
Guido van Rossum4668b002001-08-08 05:00:18 +0000776 */
777
Mark Hammond91a681d2002-08-12 07:21:58 +0000778 PyAPI_FUNC(PyObject *) PyNumber_Remainder(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000779
780 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 Returns the remainder of dividing o1 by o2, or null on
782 failure. This is the equivalent of the Python expression:
783 o1%o2.
Guido van Rossuma8275371995-07-18 14:07:00 +0000784 */
785
Mark Hammond91a681d2002-08-12 07:21:58 +0000786 PyAPI_FUNC(PyObject *) PyNumber_Divmod(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000787
788 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 See the built-in function divmod. Returns NULL on failure.
790 This is the equivalent of the Python expression:
791 divmod(o1,o2).
Guido van Rossuma8275371995-07-18 14:07:00 +0000792 */
793
Mark Hammond91a681d2002-08-12 07:21:58 +0000794 PyAPI_FUNC(PyObject *) PyNumber_Power(PyObject *o1, PyObject *o2,
Neal Norwitzfe554642006-03-17 06:58:45 +0000795 PyObject *o3);
Guido van Rossuma8275371995-07-18 14:07:00 +0000796
797 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 See the built-in function pow. Returns NULL on failure.
799 This is the equivalent of the Python expression:
800 pow(o1,o2,o3), where o3 is optional.
Guido van Rossuma8275371995-07-18 14:07:00 +0000801 */
802
Mark Hammond91a681d2002-08-12 07:21:58 +0000803 PyAPI_FUNC(PyObject *) PyNumber_Negative(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000804
805 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 Returns the negation of o on success, or null on failure.
807 This is the equivalent of the Python expression: -o.
Guido van Rossuma8275371995-07-18 14:07:00 +0000808 */
809
Mark Hammond91a681d2002-08-12 07:21:58 +0000810 PyAPI_FUNC(PyObject *) PyNumber_Positive(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000811
812 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 Returns the (what?) of o on success, or NULL on failure.
814 This is the equivalent of the Python expression: +o.
Guido van Rossuma8275371995-07-18 14:07:00 +0000815 */
816
Mark Hammond91a681d2002-08-12 07:21:58 +0000817 PyAPI_FUNC(PyObject *) PyNumber_Absolute(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000818
819 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 Returns the absolute value of o, or null on failure. This is
821 the equivalent of the Python expression: abs(o).
Guido van Rossuma8275371995-07-18 14:07:00 +0000822 */
823
Mark Hammond91a681d2002-08-12 07:21:58 +0000824 PyAPI_FUNC(PyObject *) PyNumber_Invert(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000825
826 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 Returns the bitwise negation of o on success, or NULL on
828 failure. This is the equivalent of the Python expression:
829 ~o.
Guido van Rossuma8275371995-07-18 14:07:00 +0000830 */
831
Mark Hammond91a681d2002-08-12 07:21:58 +0000832 PyAPI_FUNC(PyObject *) PyNumber_Lshift(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000833
834 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000835 Returns the result of left shifting o1 by o2 on success, or
836 NULL on failure. This is the equivalent of the Python
837 expression: o1 << o2.
Guido van Rossuma8275371995-07-18 14:07:00 +0000838 */
839
Mark Hammond91a681d2002-08-12 07:21:58 +0000840 PyAPI_FUNC(PyObject *) PyNumber_Rshift(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000841
842 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000843 Returns the result of right shifting o1 by o2 on success, or
844 NULL on failure. This is the equivalent of the Python
845 expression: o1 >> o2.
Guido van Rossuma8275371995-07-18 14:07:00 +0000846 */
847
Mark Hammond91a681d2002-08-12 07:21:58 +0000848 PyAPI_FUNC(PyObject *) PyNumber_And(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000849
850 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 Returns the result of bitwise and of o1 and o2 on success, or
852 NULL on failure. This is the equivalent of the Python
853 expression: o1&o2.
Guido van Rossuma8275371995-07-18 14:07:00 +0000854
Guido van Rossuma8275371995-07-18 14:07:00 +0000855 */
856
Mark Hammond91a681d2002-08-12 07:21:58 +0000857 PyAPI_FUNC(PyObject *) PyNumber_Xor(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000858
859 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 Returns the bitwise exclusive or of o1 by o2 on success, or
861 NULL on failure. This is the equivalent of the Python
862 expression: o1^o2.
Guido van Rossuma8275371995-07-18 14:07:00 +0000863 */
864
Mark Hammond91a681d2002-08-12 07:21:58 +0000865 PyAPI_FUNC(PyObject *) PyNumber_Or(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000866
867 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 Returns the result of bitwise or on o1 and o2 on success, or
869 NULL on failure. This is the equivalent of the Python
870 expression: o1|o2.
Guido van Rossuma8275371995-07-18 14:07:00 +0000871 */
872
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000873#define PyIndex_Check(obj) \
874 ((obj)->ob_type->tp_as_number != NULL && \
875 (obj)->ob_type->tp_as_number->nb_index != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000877 PyAPI_FUNC(PyObject *) PyNumber_Index(PyObject *o);
Guido van Rossum38fff8c2006-03-07 18:50:55 +0000878
879 /*
Serhiy Storchaka95949422013-08-27 19:40:23 +0300880 Returns the object converted to a Python int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 or NULL with an error raised on failure.
Guido van Rossum38fff8c2006-03-07 18:50:55 +0000882 */
883
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000884 PyAPI_FUNC(Py_ssize_t) PyNumber_AsSsize_t(PyObject *o, PyObject *exc);
885
886 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 Returns the object converted to Py_ssize_t by going through
888 PyNumber_Index first. If an overflow error occurs while
Serhiy Storchaka95949422013-08-27 19:40:23 +0300889 converting the int to Py_ssize_t, then the second argument
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 is the error-type to return. If it is NULL, then the overflow error
891 is cleared and the value is clipped.
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000892 */
Guido van Rossum38fff8c2006-03-07 18:50:55 +0000893
Mark Dickinsond7467682009-01-10 22:14:33 +0000894 PyAPI_FUNC(PyObject *) PyNumber_Long(PyObject *o);
895
Guido van Rossuma8275371995-07-18 14:07:00 +0000896 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 Returns the o converted to an integer object on success, or
898 NULL on failure. This is the equivalent of the Python
899 expression: int(o).
Guido van Rossuma8275371995-07-18 14:07:00 +0000900 */
901
Mark Hammond91a681d2002-08-12 07:21:58 +0000902 PyAPI_FUNC(PyObject *) PyNumber_Float(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000903
904 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 Returns the o converted to a float object on success, or NULL
906 on failure. This is the equivalent of the Python expression:
907 float(o).
Guido van Rossuma8275371995-07-18 14:07:00 +0000908 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000910/* In-place variants of (some of) the above number protocol functions */
911
Mark Hammond91a681d2002-08-12 07:21:58 +0000912 PyAPI_FUNC(PyObject *) PyNumber_InPlaceAdd(PyObject *o1, PyObject *o2);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000913
914 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 Returns the result of adding o2 to o1, possibly in-place, or null
916 on failure. This is the equivalent of the Python expression:
917 o1 += o2.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000918 */
919
Mark Hammond91a681d2002-08-12 07:21:58 +0000920 PyAPI_FUNC(PyObject *) PyNumber_InPlaceSubtract(PyObject *o1, PyObject *o2);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000921
922 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 Returns the result of subtracting o2 from o1, possibly in-place or
924 null on failure. This is the equivalent of the Python expression:
925 o1 -= o2.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000926 */
927
Mark Hammond91a681d2002-08-12 07:21:58 +0000928 PyAPI_FUNC(PyObject *) PyNumber_InPlaceMultiply(PyObject *o1, PyObject *o2);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000929
930 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 Returns the result of multiplying o1 by o2, possibly in-place, or
932 null on failure. This is the equivalent of the Python expression:
933 o1 *= o2.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000934 */
935
Benjamin Petersond51374e2014-04-09 23:55:56 -0400936 PyAPI_FUNC(PyObject *) PyNumber_InPlaceMatrixMultiply(PyObject *o1, PyObject *o2);
937
938 /*
939 This is the equivalent of the Python expression: o1 @= o2.
940 */
941
Mark Hammond91a681d2002-08-12 07:21:58 +0000942 PyAPI_FUNC(PyObject *) PyNumber_InPlaceFloorDivide(PyObject *o1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 PyObject *o2);
Guido van Rossum4668b002001-08-08 05:00:18 +0000944
945 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 Returns the result of dividing o1 by o2 giving an integral result,
947 possibly in-place, or null on failure.
948 This is the equivalent of the Python expression:
949 o1 /= o2.
Guido van Rossum4668b002001-08-08 05:00:18 +0000950 */
951
Mark Hammond91a681d2002-08-12 07:21:58 +0000952 PyAPI_FUNC(PyObject *) PyNumber_InPlaceTrueDivide(PyObject *o1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 PyObject *o2);
Guido van Rossum4668b002001-08-08 05:00:18 +0000954
955 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 Returns the result of dividing o1 by o2 giving a float result,
957 possibly in-place, or null on failure.
958 This is the equivalent of the Python expression:
959 o1 /= o2.
Guido van Rossum4668b002001-08-08 05:00:18 +0000960 */
961
Mark Hammond91a681d2002-08-12 07:21:58 +0000962 PyAPI_FUNC(PyObject *) PyNumber_InPlaceRemainder(PyObject *o1, PyObject *o2);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000963
964 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 Returns the remainder of dividing o1 by o2, possibly in-place, or
966 null on failure. This is the equivalent of the Python expression:
967 o1 %= o2.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000968 */
969
Mark Hammond91a681d2002-08-12 07:21:58 +0000970 PyAPI_FUNC(PyObject *) PyNumber_InPlacePower(PyObject *o1, PyObject *o2,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 PyObject *o3);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000972
973 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 Returns the result of raising o1 to the power of o2, possibly
975 in-place, or null on failure. This is the equivalent of the Python
976 expression: o1 **= o2, or pow(o1, o2, o3) if o3 is present.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000977 */
978
Mark Hammond91a681d2002-08-12 07:21:58 +0000979 PyAPI_FUNC(PyObject *) PyNumber_InPlaceLshift(PyObject *o1, PyObject *o2);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000980
981 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 Returns the result of left shifting o1 by o2, possibly in-place, or
983 null on failure. This is the equivalent of the Python expression:
984 o1 <<= o2.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000985 */
986
Mark Hammond91a681d2002-08-12 07:21:58 +0000987 PyAPI_FUNC(PyObject *) PyNumber_InPlaceRshift(PyObject *o1, PyObject *o2);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000988
989 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 Returns the result of right shifting o1 by o2, possibly in-place or
991 null on failure. This is the equivalent of the Python expression:
992 o1 >>= o2.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000993 */
994
Mark Hammond91a681d2002-08-12 07:21:58 +0000995 PyAPI_FUNC(PyObject *) PyNumber_InPlaceAnd(PyObject *o1, PyObject *o2);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +0000996
997 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 Returns the result of bitwise and of o1 and o2, possibly in-place,
999 or null on failure. This is the equivalent of the Python
1000 expression: o1 &= o2.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +00001001 */
1002
Mark Hammond91a681d2002-08-12 07:21:58 +00001003 PyAPI_FUNC(PyObject *) PyNumber_InPlaceXor(PyObject *o1, PyObject *o2);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +00001004
1005 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 Returns the bitwise exclusive or of o1 by o2, possibly in-place, or
1007 null on failure. This is the equivalent of the Python expression:
1008 o1 ^= o2.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +00001009 */
1010
Mark Hammond91a681d2002-08-12 07:21:58 +00001011 PyAPI_FUNC(PyObject *) PyNumber_InPlaceOr(PyObject *o1, PyObject *o2);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +00001012
1013 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 Returns the result of bitwise or of o1 and o2, possibly in-place,
1015 or null on failure. This is the equivalent of the Python
1016 expression: o1 |= o2.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +00001017 */
1018
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001019 PyAPI_FUNC(PyObject *) PyNumber_ToBase(PyObject *n, int base);
1020
1021 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 Returns the integer n converted to a string with a base, with a base
1023 marker of 0b, 0o or 0x prefixed if applicable.
1024 If n is not an int object, it is converted with PyNumber_Index first.
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001025 */
1026
Guido van Rossuma8275371995-07-18 14:07:00 +00001027
1028/* Sequence protocol:*/
1029
Mark Hammond91a681d2002-08-12 07:21:58 +00001030 PyAPI_FUNC(int) PySequence_Check(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +00001031
1032 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 Return 1 if the object provides sequence protocol, and zero
1034 otherwise.
Guido van Rossuma8275371995-07-18 14:07:00 +00001035
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 This function always succeeds.
Guido van Rossuma8275371995-07-18 14:07:00 +00001037 */
1038
Martin v. Löwis18e16552006-02-15 17:27:45 +00001039 PyAPI_FUNC(Py_ssize_t) PySequence_Size(PyObject *o);
Jeremy Hylton6253f832000-07-12 12:56:19 +00001040
Guido van Rossum4f4ce681996-07-21 02:22:56 +00001041 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 Return the size of sequence object o, or -1 on failure.
Guido van Rossum4f4ce681996-07-21 02:22:56 +00001043 */
1044
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00001045 /* For DLL compatibility */
1046#undef PySequence_Length
Martin v. Löwis18e16552006-02-15 17:27:45 +00001047 PyAPI_FUNC(Py_ssize_t) PySequence_Length(PyObject *o);
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00001048#define PySequence_Length PySequence_Size
1049
1050
Mark Hammond91a681d2002-08-12 07:21:58 +00001051 PyAPI_FUNC(PyObject *) PySequence_Concat(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +00001052
1053 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 Return the concatenation of o1 and o2 on success, and NULL on
1055 failure. This is the equivalent of the Python
1056 expression: o1+o2.
Guido van Rossuma8275371995-07-18 14:07:00 +00001057 */
1058
Martin v. Löwis18e16552006-02-15 17:27:45 +00001059 PyAPI_FUNC(PyObject *) PySequence_Repeat(PyObject *o, Py_ssize_t count);
Guido van Rossuma8275371995-07-18 14:07:00 +00001060
1061 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 Return the result of repeating sequence object o count times,
1063 or NULL on failure. This is the equivalent of the Python
1064 expression: o1*count.
Guido van Rossuma8275371995-07-18 14:07:00 +00001065 */
1066
Martin v. Löwis18e16552006-02-15 17:27:45 +00001067 PyAPI_FUNC(PyObject *) PySequence_GetItem(PyObject *o, Py_ssize_t i);
Guido van Rossuma8275371995-07-18 14:07:00 +00001068
1069 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 Return the ith element of o, or NULL on failure. This is the
1071 equivalent of the Python expression: o[i].
Guido van Rossuma8275371995-07-18 14:07:00 +00001072 */
1073
Martin v. Löwis18e16552006-02-15 17:27:45 +00001074 PyAPI_FUNC(PyObject *) PySequence_GetSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2);
Guido van Rossuma8275371995-07-18 14:07:00 +00001075
1076 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 Return the slice of sequence object o between i1 and i2, or
1078 NULL on failure. This is the equivalent of the Python
1079 expression: o[i1:i2].
Guido van Rossuma8275371995-07-18 14:07:00 +00001080 */
1081
Martin v. Löwis18e16552006-02-15 17:27:45 +00001082 PyAPI_FUNC(int) PySequence_SetItem(PyObject *o, Py_ssize_t i, PyObject *v);
Guido van Rossuma8275371995-07-18 14:07:00 +00001083
1084 /*
Martin Panter45be8d62015-12-08 00:03:20 +00001085 Assign object v to the ith element of o. Raise an exception and return
1086 -1 on failure; return 0 on success. This is the equivalent of the
1087 Python statement o[i]=v.
Guido van Rossuma8275371995-07-18 14:07:00 +00001088 */
1089
Martin v. Löwis18e16552006-02-15 17:27:45 +00001090 PyAPI_FUNC(int) PySequence_DelItem(PyObject *o, Py_ssize_t i);
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001091
1092 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 Delete the ith element of object v. Returns
1094 -1 on failure. This is the equivalent of the Python
1095 statement: del o[i].
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001096 */
1097
Martin v. Löwis18e16552006-02-15 17:27:45 +00001098 PyAPI_FUNC(int) PySequence_SetSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2,
Neal Norwitzfe554642006-03-17 06:58:45 +00001099 PyObject *v);
Guido van Rossuma8275371995-07-18 14:07:00 +00001100
1101 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001102 Assign the sequence object, v, to the slice in sequence
1103 object, o, from i1 to i2. Returns -1 on failure. This is the
1104 equivalent of the Python statement: o[i1:i2]=v.
Guido van Rossuma8275371995-07-18 14:07:00 +00001105 */
1106
Martin v. Löwis18e16552006-02-15 17:27:45 +00001107 PyAPI_FUNC(int) PySequence_DelSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2);
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001108
1109 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001110 Delete the slice in sequence object, o, from i1 to i2.
1111 Returns -1 on failure. This is the equivalent of the Python
1112 statement: del o[i1:i2].
Guido van Rossum6cdc6f41996-08-21 17:41:54 +00001113 */
1114
Mark Hammond91a681d2002-08-12 07:21:58 +00001115 PyAPI_FUNC(PyObject *) PySequence_Tuple(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +00001116
1117 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 Returns the sequence, o, as a tuple on success, and NULL on failure.
1119 This is equivalent to the Python expression: tuple(o)
Guido van Rossuma8275371995-07-18 14:07:00 +00001120 */
1121
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001122
Mark Hammond91a681d2002-08-12 07:21:58 +00001123 PyAPI_FUNC(PyObject *) PySequence_List(PyObject *o);
Guido van Rossum2adf06b1996-12-05 21:48:50 +00001124 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 Returns the sequence, o, as a list on success, and NULL on failure.
1126 This is equivalent to the Python expression: list(o)
Guido van Rossum2adf06b1996-12-05 21:48:50 +00001127 */
Guido van Rossumf39fc431997-03-04 18:31:47 +00001128
Mark Hammond91a681d2002-08-12 07:21:58 +00001129 PyAPI_FUNC(PyObject *) PySequence_Fast(PyObject *o, const char* m);
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001130 /*
Benjamin Peterson7ddf3eb2014-04-08 10:51:20 -04001131 Return the sequence, o, as a list, unless it's already a
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001132 tuple or list. Use PySequence_Fast_GET_ITEM to access the
1133 members of this list, and PySequence_Fast_GET_SIZE to get its length.
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001134
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135 Returns NULL on failure. If the object does not support iteration,
1136 raises a TypeError exception with m as the message text.
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001137 */
1138
Tim Peters1fc240e2001-10-26 05:06:50 +00001139#define PySequence_Fast_GET_SIZE(o) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 (PyList_Check(o) ? PyList_GET_SIZE(o) : PyTuple_GET_SIZE(o))
Tim Peters1fc240e2001-10-26 05:06:50 +00001141 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 Return the size of o, assuming that o was returned by
1143 PySequence_Fast and is not NULL.
Tim Peters1fc240e2001-10-26 05:06:50 +00001144 */
1145
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001146#define PySequence_Fast_GET_ITEM(o, i)\
1147 (PyList_Check(o) ? PyList_GET_ITEM(o, i) : PyTuple_GET_ITEM(o, i))
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001148 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 Return the ith element of o, assuming that o was returned by
1150 PySequence_Fast, and that i is within bounds.
Andrew M. Kuchling74042d62000-06-18 18:43:14 +00001151 */
1152
Martin v. Löwis01f94bd2002-05-08 08:44:21 +00001153#define PySequence_ITEM(o, i)\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001154 ( Py_TYPE(o)->tp_as_sequence->sq_item(o, i) )
Martin v. Löwis01f94bd2002-05-08 08:44:21 +00001155 /* Assume tp_as_sequence and sq_item exist and that i does not
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001156 need to be corrected for a negative index
1157 */
Martin v. Löwis01f94bd2002-05-08 08:44:21 +00001158
Raymond Hettinger42bec932004-03-12 16:38:17 +00001159#define PySequence_Fast_ITEMS(sf) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001160 (PyList_Check(sf) ? ((PyListObject *)(sf))->ob_item \
1161 : ((PyTupleObject *)(sf))->ob_item)
1162 /* Return a pointer to the underlying item array for
1163 an object retured by PySequence_Fast */
Raymond Hettingerc1e4f9d2004-03-12 08:04:00 +00001164
Neal Norwitz1fc4b772006-03-04 18:49:58 +00001165 PyAPI_FUNC(Py_ssize_t) PySequence_Count(PyObject *o, PyObject *value);
Guido van Rossuma8275371995-07-18 14:07:00 +00001166
1167 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001168 Return the number of occurrences on value on o, that is,
1169 return the number of keys for which o[key]==value. On
1170 failure, return -1. This is equivalent to the Python
1171 expression: o.count(value).
Guido van Rossuma8275371995-07-18 14:07:00 +00001172 */
1173
Mark Hammond91a681d2002-08-12 07:21:58 +00001174 PyAPI_FUNC(int) PySequence_Contains(PyObject *seq, PyObject *ob);
Tim Peterscb8d3682001-05-05 21:05:01 +00001175 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001176 Return -1 if error; 1 if ob in seq; 0 if ob not in seq.
1177 Use __contains__ if possible, else _PySequence_IterSearch().
Tim Peterscb8d3682001-05-05 21:05:01 +00001178 */
1179
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001180#ifndef Py_LIMITED_API
Tim Peters16a77ad2001-09-08 04:00:12 +00001181#define PY_ITERSEARCH_COUNT 1
1182#define PY_ITERSEARCH_INDEX 2
1183#define PY_ITERSEARCH_CONTAINS 3
Neal Norwitz1fc4b772006-03-04 18:49:58 +00001184 PyAPI_FUNC(Py_ssize_t) _PySequence_IterSearch(PyObject *seq,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 PyObject *obj, int operation);
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001186#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 /*
1188 Iterate over seq. Result depends on the operation:
1189 PY_ITERSEARCH_COUNT: return # of times obj appears in seq; -1 if
1190 error.
1191 PY_ITERSEARCH_INDEX: return 0-based index of first occurrence of
1192 obj in seq; set ValueError and return -1 if none found;
1193 also return -1 on error.
1194 PY_ITERSEARCH_CONTAINS: return 1 if obj in seq, else 0; -1 on
1195 error.
1196 */
Guido van Rossum83684531999-03-17 18:44:39 +00001197
1198/* For DLL-level backwards compatibility */
1199#undef PySequence_In
Mark Hammond91a681d2002-08-12 07:21:58 +00001200 PyAPI_FUNC(int) PySequence_In(PyObject *o, PyObject *value);
Guido van Rossum83684531999-03-17 18:44:39 +00001201
1202/* For source-level backwards compatibility */
Guido van Rossumf1536db1998-08-23 22:06:59 +00001203#define PySequence_In PySequence_Contains
Guido van Rossuma8275371995-07-18 14:07:00 +00001204
1205 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 Determine if o contains value. If an item in o is equal to
1207 X, return 1, otherwise return 0. On error, return -1. This
1208 is equivalent to the Python expression: value in o.
Guido van Rossuma8275371995-07-18 14:07:00 +00001209 */
1210
Neal Norwitz1fc4b772006-03-04 18:49:58 +00001211 PyAPI_FUNC(Py_ssize_t) PySequence_Index(PyObject *o, PyObject *value);
Guido van Rossuma8275371995-07-18 14:07:00 +00001212
1213 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214 Return the first index for which o[i]=value. On error,
1215 return -1. This is equivalent to the Python
1216 expression: o.index(value).
Guido van Rossuma8275371995-07-18 14:07:00 +00001217 */
1218
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +00001219/* In-place versions of some of the above Sequence functions. */
1220
Mark Hammond91a681d2002-08-12 07:21:58 +00001221 PyAPI_FUNC(PyObject *) PySequence_InPlaceConcat(PyObject *o1, PyObject *o2);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +00001222
1223 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 Append o2 to o1, in-place when possible. Return the resulting
1225 object, which could be o1, or NULL on failure. This is the
1226 equivalent of the Python expression: o1 += o2.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +00001227
1228 */
1229
Martin v. Löwis18e16552006-02-15 17:27:45 +00001230 PyAPI_FUNC(PyObject *) PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count);
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +00001231
1232 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 Repeat o1 by count, in-place when possible. Return the resulting
1234 object, which could be o1, or NULL on failure. This is the
1235 equivalent of the Python expression: o1 *= count.
Thomas Woutersdd8dbdb2000-08-24 20:09:45 +00001236
1237 */
1238
Guido van Rossuma8275371995-07-18 14:07:00 +00001239/* Mapping protocol:*/
1240
Mark Hammond91a681d2002-08-12 07:21:58 +00001241 PyAPI_FUNC(int) PyMapping_Check(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +00001242
1243 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 Return 1 if the object provides mapping protocol, and zero
1245 otherwise.
Guido van Rossuma8275371995-07-18 14:07:00 +00001246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 This function always succeeds.
Guido van Rossuma8275371995-07-18 14:07:00 +00001248 */
1249
Martin v. Löwis18e16552006-02-15 17:27:45 +00001250 PyAPI_FUNC(Py_ssize_t) PyMapping_Size(PyObject *o);
Jeremy Hylton6253f832000-07-12 12:56:19 +00001251
Guido van Rossuma8275371995-07-18 14:07:00 +00001252 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 Returns the number of keys in object o on success, and -1 on
1254 failure. For objects that do not provide sequence protocol,
1255 this is equivalent to the Python expression: len(o).
Guido van Rossuma8275371995-07-18 14:07:00 +00001256 */
1257
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00001258 /* For DLL compatibility */
1259#undef PyMapping_Length
Martin v. Löwis18e16552006-02-15 17:27:45 +00001260 PyAPI_FUNC(Py_ssize_t) PyMapping_Length(PyObject *o);
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +00001261#define PyMapping_Length PyMapping_Size
1262
1263
Guido van Rossuma25e5e91996-09-06 13:48:38 +00001264 /* implemented as a macro:
1265
Serhiy Storchakac6792272013-10-19 21:03:34 +03001266 int PyMapping_DelItemString(PyObject *o, const char *key);
Guido van Rossuma8275371995-07-18 14:07:00 +00001267
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 Remove the mapping for object, key, from the object *o.
1269 Returns -1 on failure. This is equivalent to
1270 the Python statement: del o[key].
Guido van Rossuma8275371995-07-18 14:07:00 +00001271 */
Jeremy Hylton7c7ee5f2001-11-28 16:20:07 +00001272#define PyMapping_DelItemString(O,K) PyObject_DelItemString((O),(K))
Guido van Rossuma25e5e91996-09-06 13:48:38 +00001273
1274 /* implemented as a macro:
Guido van Rossuma8275371995-07-18 14:07:00 +00001275
Fred Drakeea9cb5a2000-07-09 00:20:36 +00001276 int PyMapping_DelItem(PyObject *o, PyObject *key);
Guido van Rossuma8275371995-07-18 14:07:00 +00001277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001278 Remove the mapping for object, key, from the object *o.
1279 Returns -1 on failure. This is equivalent to
1280 the Python statement: del o[key].
Guido van Rossuma8275371995-07-18 14:07:00 +00001281 */
Jeremy Hylton7c7ee5f2001-11-28 16:20:07 +00001282#define PyMapping_DelItem(O,K) PyObject_DelItem((O),(K))
Guido van Rossuma8275371995-07-18 14:07:00 +00001283
Serhiy Storchakac6792272013-10-19 21:03:34 +03001284 PyAPI_FUNC(int) PyMapping_HasKeyString(PyObject *o, const char *key);
Guido van Rossuma8275371995-07-18 14:07:00 +00001285
1286 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 On success, return 1 if the mapping object has the key, key,
1288 and 0 otherwise. This is equivalent to the Python expression:
1289 key in o.
Guido van Rossuma8275371995-07-18 14:07:00 +00001290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 This function always succeeds.
Guido van Rossuma8275371995-07-18 14:07:00 +00001292 */
1293
Mark Hammond91a681d2002-08-12 07:21:58 +00001294 PyAPI_FUNC(int) PyMapping_HasKey(PyObject *o, PyObject *key);
Guido van Rossuma8275371995-07-18 14:07:00 +00001295
1296 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 Return 1 if the mapping object has the key, key,
1298 and 0 otherwise. This is equivalent to the Python expression:
1299 key in o.
Guido van Rossuma8275371995-07-18 14:07:00 +00001300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001301 This function always succeeds.
Guido van Rossuma8275371995-07-18 14:07:00 +00001302
1303 */
1304
Guido van Rossumcc2b0162007-02-11 06:12:03 +00001305 PyAPI_FUNC(PyObject *) PyMapping_Keys(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +00001306
Guido van Rossumcc2b0162007-02-11 06:12:03 +00001307 /*
Serhiy Storchaka1d480be2016-09-26 20:52:41 +03001308 On success, return a list or tuple of the keys in object o.
1309 On failure, return NULL.
Guido van Rossuma8275371995-07-18 14:07:00 +00001310 */
Guido van Rossuma8275371995-07-18 14:07:00 +00001311
Guido van Rossumcc2b0162007-02-11 06:12:03 +00001312 PyAPI_FUNC(PyObject *) PyMapping_Values(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +00001313
Guido van Rossumcc2b0162007-02-11 06:12:03 +00001314 /*
Serhiy Storchaka1d480be2016-09-26 20:52:41 +03001315 On success, return a list or tuple of the values in object o.
1316 On failure, return NULL.
Guido van Rossuma8275371995-07-18 14:07:00 +00001317 */
Guido van Rossuma8275371995-07-18 14:07:00 +00001318
Guido van Rossumcc2b0162007-02-11 06:12:03 +00001319 PyAPI_FUNC(PyObject *) PyMapping_Items(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +00001320
Guido van Rossumcc2b0162007-02-11 06:12:03 +00001321 /*
Serhiy Storchaka1d480be2016-09-26 20:52:41 +03001322 On success, return a list or tuple of the items in object o,
1323 where each item is a tuple containing a key-value pair.
1324 On failure, return NULL.
Guido van Rossuma8275371995-07-18 14:07:00 +00001325
1326 */
Guido van Rossuma8275371995-07-18 14:07:00 +00001327
Serhiy Storchakac6792272013-10-19 21:03:34 +03001328 PyAPI_FUNC(PyObject *) PyMapping_GetItemString(PyObject *o,
1329 const char *key);
Guido van Rossuma8275371995-07-18 14:07:00 +00001330
1331 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 Return element of o corresponding to the object, key, or NULL
1333 on failure. This is the equivalent of the Python expression:
1334 o[key].
Guido van Rossuma8275371995-07-18 14:07:00 +00001335 */
1336
Serhiy Storchakac6792272013-10-19 21:03:34 +03001337 PyAPI_FUNC(int) PyMapping_SetItemString(PyObject *o, const char *key,
Fred Drakeea9cb5a2000-07-09 00:20:36 +00001338 PyObject *value);
Guido van Rossuma8275371995-07-18 14:07:00 +00001339
1340 /*
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 Map the object, key, to the value, v. Returns
1342 -1 on failure. This is the equivalent of the Python
1343 statement: o[key]=v.
Guido van Rossuma8275371995-07-18 14:07:00 +00001344 */
1345
1346
Mark Hammond91a681d2002-08-12 07:21:58 +00001347PyAPI_FUNC(int) PyObject_IsInstance(PyObject *object, PyObject *typeorclass);
Guido van Rossum823649d2001-03-21 18:40:58 +00001348 /* isinstance(object, typeorclass) */
1349
Mark Hammond91a681d2002-08-12 07:21:58 +00001350PyAPI_FUNC(int) PyObject_IsSubclass(PyObject *object, PyObject *typeorclass);
Guido van Rossum823649d2001-03-21 18:40:58 +00001351 /* issubclass(object, typeorclass) */
1352
1353
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001354#ifndef Py_LIMITED_API
Antoine Pitrouec569b72008-08-26 22:40:48 +00001355PyAPI_FUNC(int) _PyObject_RealIsInstance(PyObject *inst, PyObject *cls);
1356
1357PyAPI_FUNC(int) _PyObject_RealIsSubclass(PyObject *derived, PyObject *cls);
1358
Gregory P. Smithfb94c5f2010-03-14 06:49:55 +00001359PyAPI_FUNC(char *const *) _PySequence_BytesToCharpArray(PyObject* self);
1360
1361PyAPI_FUNC(void) _Py_FreeCharPArray(char *const array[]);
Antoine Pitrouec569b72008-08-26 22:40:48 +00001362
Antoine Pitrouf68c2a72010-09-01 12:58:21 +00001363/* For internal use by buffer API functions */
1364PyAPI_FUNC(void) _Py_add_one_to_index_F(int nd, Py_ssize_t *index,
1365 const Py_ssize_t *shape);
1366PyAPI_FUNC(void) _Py_add_one_to_index_C(int nd, Py_ssize_t *index,
1367 const Py_ssize_t *shape);
Serhiy Storchaka9fab79b2016-09-11 11:03:14 +03001368#endif /* !Py_LIMITED_API */
Antoine Pitrouf68c2a72010-09-01 12:58:21 +00001369
1370
Guido van Rossum8ca687a1995-09-18 21:20:02 +00001371#ifdef __cplusplus
1372}
1373#endif
Guido van Rossuma8275371995-07-18 14:07:00 +00001374#endif /* Py_ABSTRACTOBJECT_H */