Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
allendowney
GitHub Repository: allendowney/cpython
Path: blob/main/Include/abstract.h
12 views
1
/* Abstract Object Interface (many thanks to Jim Fulton) */
2
3
#ifndef Py_ABSTRACTOBJECT_H
4
#define Py_ABSTRACTOBJECT_H
5
#ifdef __cplusplus
6
extern "C" {
7
#endif
8
9
/* === Object Protocol ================================================== */
10
11
/* Implemented elsewhere:
12
13
int PyObject_Print(PyObject *o, FILE *fp, int flags);
14
15
Print an object 'o' on file 'fp'. Returns -1 on error. The flags argument
16
is used to enable certain printing options. The only option currently
17
supported is Py_PRINT_RAW. By default (flags=0), PyObject_Print() formats
18
the object by calling PyObject_Repr(). If flags equals to Py_PRINT_RAW, it
19
formats the object by calling PyObject_Str(). */
20
21
22
/* Implemented elsewhere:
23
24
int PyObject_HasAttrString(PyObject *o, const char *attr_name);
25
26
Returns 1 if object 'o' has the attribute attr_name, and 0 otherwise.
27
28
This is equivalent to the Python expression: hasattr(o,attr_name).
29
30
This function always succeeds. */
31
32
33
/* Implemented elsewhere:
34
35
PyObject* PyObject_GetAttrString(PyObject *o, const char *attr_name);
36
37
Retrieve an attributed named attr_name form object o.
38
Returns the attribute value on success, or NULL on failure.
39
40
This is the equivalent of the Python expression: o.attr_name. */
41
42
43
/* Implemented elsewhere:
44
45
int PyObject_HasAttr(PyObject *o, PyObject *attr_name);
46
47
Returns 1 if o has the attribute attr_name, and 0 otherwise.
48
49
This is equivalent to the Python expression: hasattr(o,attr_name).
50
51
This function always succeeds. */
52
53
/* Implemented elsewhere:
54
55
PyObject* PyObject_GetAttr(PyObject *o, PyObject *attr_name);
56
57
Retrieve an attributed named 'attr_name' form object 'o'.
58
Returns the attribute value on success, or NULL on failure.
59
60
This is the equivalent of the Python expression: o.attr_name. */
61
62
63
/* Implemented elsewhere:
64
65
int PyObject_SetAttrString(PyObject *o, const char *attr_name, PyObject *v);
66
67
Set the value of the attribute named attr_name, for object 'o',
68
to the value 'v'. Raise an exception and return -1 on failure; return 0 on
69
success.
70
71
This is the equivalent of the Python statement o.attr_name=v. */
72
73
74
/* Implemented elsewhere:
75
76
int PyObject_SetAttr(PyObject *o, PyObject *attr_name, PyObject *v);
77
78
Set the value of the attribute named attr_name, for object 'o', to the value
79
'v'. an exception and return -1 on failure; return 0 on success.
80
81
This is the equivalent of the Python statement o.attr_name=v. */
82
83
/* Implemented as a macro:
84
85
int PyObject_DelAttrString(PyObject *o, const char *attr_name);
86
87
Delete attribute named attr_name, for object o. Returns
88
-1 on failure.
89
90
This is the equivalent of the Python statement: del o.attr_name. */
91
#define PyObject_DelAttrString(O, A) PyObject_SetAttrString((O), (A), NULL)
92
93
94
/* Implemented as a macro:
95
96
int PyObject_DelAttr(PyObject *o, PyObject *attr_name);
97
98
Delete attribute named attr_name, for object o. Returns -1
99
on failure. This is the equivalent of the Python
100
statement: del o.attr_name. */
101
#define PyObject_DelAttr(O, A) PyObject_SetAttr((O), (A), NULL)
102
103
104
/* Implemented elsewhere:
105
106
PyObject *PyObject_Repr(PyObject *o);
107
108
Compute the string representation of object 'o'. Returns the
109
string representation on success, NULL on failure.
110
111
This is the equivalent of the Python expression: repr(o).
112
113
Called by the repr() built-in function. */
114
115
116
/* Implemented elsewhere:
117
118
PyObject *PyObject_Str(PyObject *o);
119
120
Compute the string representation of object, o. Returns the
121
string representation on success, NULL on failure.
122
123
This is the equivalent of the Python expression: str(o).
124
125
Called by the str() and print() built-in functions. */
126
127
128
/* Declared elsewhere
129
130
PyAPI_FUNC(int) PyCallable_Check(PyObject *o);
131
132
Determine if the object, o, is callable. Return 1 if the object is callable
133
and 0 otherwise.
134
135
This function always succeeds. */
136
137
138
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03090000
139
/* Call a callable Python object without any arguments */
140
PyAPI_FUNC(PyObject *) PyObject_CallNoArgs(PyObject *func);
141
#endif
142
143
144
/* Call a callable Python object 'callable' with arguments given by the
145
tuple 'args' and keywords arguments given by the dictionary 'kwargs'.
146
147
'args' must not be NULL, use an empty tuple if no arguments are
148
needed. If no named arguments are needed, 'kwargs' can be NULL.
149
150
This is the equivalent of the Python expression:
151
callable(*args, **kwargs). */
152
PyAPI_FUNC(PyObject *) PyObject_Call(PyObject *callable,
153
PyObject *args, PyObject *kwargs);
154
155
156
/* Call a callable Python object 'callable', with arguments given by the
157
tuple 'args'. If no arguments are needed, then 'args' can be NULL.
158
159
Returns the result of the call on success, or NULL on failure.
160
161
This is the equivalent of the Python expression:
162
callable(*args). */
163
PyAPI_FUNC(PyObject *) PyObject_CallObject(PyObject *callable,
164
PyObject *args);
165
166
/* Call a callable Python object, callable, with a variable number of C
167
arguments. The C arguments are described using a mkvalue-style format
168
string.
169
170
The format may be NULL, indicating that no arguments are provided.
171
172
Returns the result of the call on success, or NULL on failure.
173
174
This is the equivalent of the Python expression:
175
callable(arg1, arg2, ...). */
176
PyAPI_FUNC(PyObject *) PyObject_CallFunction(PyObject *callable,
177
const char *format, ...);
178
179
/* Call the method named 'name' of object 'obj' with a variable number of
180
C arguments. The C arguments are described by a mkvalue format string.
181
182
The format can be NULL, indicating that no arguments are provided.
183
184
Returns the result of the call on success, or NULL on failure.
185
186
This is the equivalent of the Python expression:
187
obj.name(arg1, arg2, ...). */
188
PyAPI_FUNC(PyObject *) PyObject_CallMethod(PyObject *obj,
189
const char *name,
190
const char *format, ...);
191
192
/* Call a callable Python object 'callable' with a variable number of C
193
arguments. The C arguments are provided as PyObject* values, terminated
194
by a NULL.
195
196
Returns the result of the call on success, or NULL on failure.
197
198
This is the equivalent of the Python expression:
199
callable(arg1, arg2, ...). */
200
PyAPI_FUNC(PyObject *) PyObject_CallFunctionObjArgs(PyObject *callable,
201
...);
202
203
/* Call the method named 'name' of object 'obj' with a variable number of
204
C arguments. The C arguments are provided as PyObject* values, terminated
205
by NULL.
206
207
Returns the result of the call on success, or NULL on failure.
208
209
This is the equivalent of the Python expression: obj.name(*args). */
210
211
PyAPI_FUNC(PyObject *) PyObject_CallMethodObjArgs(
212
PyObject *obj,
213
PyObject *name,
214
...);
215
216
/* Given a vectorcall nargsf argument, return the actual number of arguments.
217
* (For use outside the limited API, this is re-defined as a static inline
218
* function in cpython/abstract.h)
219
*/
220
PyAPI_FUNC(Py_ssize_t) PyVectorcall_NARGS(size_t nargsf);
221
222
/* Call "callable" (which must support vectorcall) with positional arguments
223
"tuple" and keyword arguments "dict". "dict" may also be NULL */
224
PyAPI_FUNC(PyObject *) PyVectorcall_Call(PyObject *callable, PyObject *tuple, PyObject *dict);
225
226
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030C0000
227
#define PY_VECTORCALL_ARGUMENTS_OFFSET \
228
(_Py_STATIC_CAST(size_t, 1) << (8 * sizeof(size_t) - 1))
229
230
/* Perform a PEP 590-style vector call on 'callable' */
231
PyAPI_FUNC(PyObject *) PyObject_Vectorcall(
232
PyObject *callable,
233
PyObject *const *args,
234
size_t nargsf,
235
PyObject *kwnames);
236
237
/* Call the method 'name' on args[0] with arguments in args[1..nargsf-1]. */
238
PyAPI_FUNC(PyObject *) PyObject_VectorcallMethod(
239
PyObject *name, PyObject *const *args,
240
size_t nargsf, PyObject *kwnames);
241
#endif
242
243
/* Implemented elsewhere:
244
245
Py_hash_t PyObject_Hash(PyObject *o);
246
247
Compute and return the hash, hash_value, of an object, o. On
248
failure, return -1.
249
250
This is the equivalent of the Python expression: hash(o). */
251
252
253
/* Implemented elsewhere:
254
255
int PyObject_IsTrue(PyObject *o);
256
257
Returns 1 if the object, o, is considered to be true, 0 if o is
258
considered to be false and -1 on failure.
259
260
This is equivalent to the Python expression: not not o. */
261
262
263
/* Implemented elsewhere:
264
265
int PyObject_Not(PyObject *o);
266
267
Returns 0 if the object, o, is considered to be true, 1 if o is
268
considered to be false and -1 on failure.
269
270
This is equivalent to the Python expression: not o. */
271
272
273
/* Get the type of an object.
274
275
On success, returns a type object corresponding to the object type of object
276
'o'. On failure, returns NULL.
277
278
This is equivalent to the Python expression: type(o) */
279
PyAPI_FUNC(PyObject *) PyObject_Type(PyObject *o);
280
281
282
/* Return the size of object 'o'. If the object 'o' provides both sequence and
283
mapping protocols, the sequence size is returned.
284
285
On error, -1 is returned.
286
287
This is the equivalent to the Python expression: len(o) */
288
PyAPI_FUNC(Py_ssize_t) PyObject_Size(PyObject *o);
289
290
291
/* For DLL compatibility */
292
#undef PyObject_Length
293
PyAPI_FUNC(Py_ssize_t) PyObject_Length(PyObject *o);
294
#define PyObject_Length PyObject_Size
295
296
/* Return element of 'o' corresponding to the object 'key'. Return NULL
297
on failure.
298
299
This is the equivalent of the Python expression: o[key] */
300
PyAPI_FUNC(PyObject *) PyObject_GetItem(PyObject *o, PyObject *key);
301
302
303
/* Map the object 'key' to the value 'v' into 'o'.
304
305
Raise an exception and return -1 on failure; return 0 on success.
306
307
This is the equivalent of the Python statement: o[key]=v. */
308
PyAPI_FUNC(int) PyObject_SetItem(PyObject *o, PyObject *key, PyObject *v);
309
310
/* Remove the mapping for the string 'key' from the object 'o'.
311
Returns -1 on failure.
312
313
This is equivalent to the Python statement: del o[key]. */
314
PyAPI_FUNC(int) PyObject_DelItemString(PyObject *o, const char *key);
315
316
/* Delete the mapping for the object 'key' from the object 'o'.
317
Returns -1 on failure.
318
319
This is the equivalent of the Python statement: del o[key]. */
320
PyAPI_FUNC(int) PyObject_DelItem(PyObject *o, PyObject *key);
321
322
323
/* Takes an arbitrary object and returns the result of calling
324
obj.__format__(format_spec). */
325
PyAPI_FUNC(PyObject *) PyObject_Format(PyObject *obj,
326
PyObject *format_spec);
327
328
329
/* ==== Iterators ================================================ */
330
331
/* Takes an object and returns an iterator for it.
332
This is typically a new iterator but if the argument is an iterator, this
333
returns itself. */
334
PyAPI_FUNC(PyObject *) PyObject_GetIter(PyObject *);
335
336
/* Takes an AsyncIterable object and returns an AsyncIterator for it.
337
This is typically a new iterator but if the argument is an AsyncIterator,
338
this returns itself. */
339
PyAPI_FUNC(PyObject *) PyObject_GetAIter(PyObject *);
340
341
/* Returns non-zero if the object 'obj' provides iterator protocols, and 0 otherwise.
342
343
This function always succeeds. */
344
PyAPI_FUNC(int) PyIter_Check(PyObject *);
345
346
/* Returns non-zero if the object 'obj' provides AsyncIterator protocols, and 0 otherwise.
347
348
This function always succeeds. */
349
PyAPI_FUNC(int) PyAIter_Check(PyObject *);
350
351
/* Takes an iterator object and calls its tp_iternext slot,
352
returning the next value.
353
354
If the iterator is exhausted, this returns NULL without setting an
355
exception.
356
357
NULL with an exception means an error occurred. */
358
PyAPI_FUNC(PyObject *) PyIter_Next(PyObject *);
359
360
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030A0000
361
362
/* Takes generator, coroutine or iterator object and sends the value into it.
363
Returns:
364
- PYGEN_RETURN (0) if generator has returned.
365
'result' parameter is filled with return value
366
- PYGEN_ERROR (-1) if exception was raised.
367
'result' parameter is NULL
368
- PYGEN_NEXT (1) if generator has yielded.
369
'result' parameter is filled with yielded value. */
370
PyAPI_FUNC(PySendResult) PyIter_Send(PyObject *, PyObject *, PyObject **);
371
#endif
372
373
374
/* === Number Protocol ================================================== */
375
376
/* Returns 1 if the object 'o' provides numeric protocols, and 0 otherwise.
377
378
This function always succeeds. */
379
PyAPI_FUNC(int) PyNumber_Check(PyObject *o);
380
381
/* Returns the result of adding o1 and o2, or NULL on failure.
382
383
This is the equivalent of the Python expression: o1 + o2. */
384
PyAPI_FUNC(PyObject *) PyNumber_Add(PyObject *o1, PyObject *o2);
385
386
/* Returns the result of subtracting o2 from o1, or NULL on failure.
387
388
This is the equivalent of the Python expression: o1 - o2. */
389
PyAPI_FUNC(PyObject *) PyNumber_Subtract(PyObject *o1, PyObject *o2);
390
391
/* Returns the result of multiplying o1 and o2, or NULL on failure.
392
393
This is the equivalent of the Python expression: o1 * o2. */
394
PyAPI_FUNC(PyObject *) PyNumber_Multiply(PyObject *o1, PyObject *o2);
395
396
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
397
/* This is the equivalent of the Python expression: o1 @ o2. */
398
PyAPI_FUNC(PyObject *) PyNumber_MatrixMultiply(PyObject *o1, PyObject *o2);
399
#endif
400
401
/* Returns the result of dividing o1 by o2 giving an integral result,
402
or NULL on failure.
403
404
This is the equivalent of the Python expression: o1 // o2. */
405
PyAPI_FUNC(PyObject *) PyNumber_FloorDivide(PyObject *o1, PyObject *o2);
406
407
/* Returns the result of dividing o1 by o2 giving a float result, or NULL on
408
failure.
409
410
This is the equivalent of the Python expression: o1 / o2. */
411
PyAPI_FUNC(PyObject *) PyNumber_TrueDivide(PyObject *o1, PyObject *o2);
412
413
/* Returns the remainder of dividing o1 by o2, or NULL on failure.
414
415
This is the equivalent of the Python expression: o1 % o2. */
416
PyAPI_FUNC(PyObject *) PyNumber_Remainder(PyObject *o1, PyObject *o2);
417
418
/* See the built-in function divmod.
419
420
Returns NULL on failure.
421
422
This is the equivalent of the Python expression: divmod(o1, o2). */
423
PyAPI_FUNC(PyObject *) PyNumber_Divmod(PyObject *o1, PyObject *o2);
424
425
/* See the built-in function pow. Returns NULL on failure.
426
427
This is the equivalent of the Python expression: pow(o1, o2, o3),
428
where o3 is optional. */
429
PyAPI_FUNC(PyObject *) PyNumber_Power(PyObject *o1, PyObject *o2,
430
PyObject *o3);
431
432
/* Returns the negation of o on success, or NULL on failure.
433
434
This is the equivalent of the Python expression: -o. */
435
PyAPI_FUNC(PyObject *) PyNumber_Negative(PyObject *o);
436
437
/* Returns the positive of o on success, or NULL on failure.
438
439
This is the equivalent of the Python expression: +o. */
440
PyAPI_FUNC(PyObject *) PyNumber_Positive(PyObject *o);
441
442
/* Returns the absolute value of 'o', or NULL on failure.
443
444
This is the equivalent of the Python expression: abs(o). */
445
PyAPI_FUNC(PyObject *) PyNumber_Absolute(PyObject *o);
446
447
/* Returns the bitwise negation of 'o' on success, or NULL on failure.
448
449
This is the equivalent of the Python expression: ~o. */
450
PyAPI_FUNC(PyObject *) PyNumber_Invert(PyObject *o);
451
452
/* Returns the result of left shifting o1 by o2 on success, or NULL on failure.
453
454
This is the equivalent of the Python expression: o1 << o2. */
455
PyAPI_FUNC(PyObject *) PyNumber_Lshift(PyObject *o1, PyObject *o2);
456
457
/* Returns the result of right shifting o1 by o2 on success, or NULL on
458
failure.
459
460
This is the equivalent of the Python expression: o1 >> o2. */
461
PyAPI_FUNC(PyObject *) PyNumber_Rshift(PyObject *o1, PyObject *o2);
462
463
/* Returns the result of bitwise and of o1 and o2 on success, or NULL on
464
failure.
465
466
This is the equivalent of the Python expression: o1 & o2. */
467
PyAPI_FUNC(PyObject *) PyNumber_And(PyObject *o1, PyObject *o2);
468
469
/* Returns the bitwise exclusive or of o1 by o2 on success, or NULL on failure.
470
471
This is the equivalent of the Python expression: o1 ^ o2. */
472
PyAPI_FUNC(PyObject *) PyNumber_Xor(PyObject *o1, PyObject *o2);
473
474
/* Returns the result of bitwise or on o1 and o2 on success, or NULL on
475
failure.
476
477
This is the equivalent of the Python expression: o1 | o2. */
478
PyAPI_FUNC(PyObject *) PyNumber_Or(PyObject *o1, PyObject *o2);
479
480
/* Returns 1 if obj is an index integer (has the nb_index slot of the
481
tp_as_number structure filled in), and 0 otherwise. */
482
PyAPI_FUNC(int) PyIndex_Check(PyObject *);
483
484
/* Returns the object 'o' converted to a Python int, or NULL with an exception
485
raised on failure. */
486
PyAPI_FUNC(PyObject *) PyNumber_Index(PyObject *o);
487
488
/* Returns the object 'o' converted to Py_ssize_t by going through
489
PyNumber_Index() first.
490
491
If an overflow error occurs while converting the int to Py_ssize_t, then the
492
second argument 'exc' is the error-type to return. If it is NULL, then the
493
overflow error is cleared and the value is clipped. */
494
PyAPI_FUNC(Py_ssize_t) PyNumber_AsSsize_t(PyObject *o, PyObject *exc);
495
496
/* Returns the object 'o' converted to an integer object on success, or NULL
497
on failure.
498
499
This is the equivalent of the Python expression: int(o). */
500
PyAPI_FUNC(PyObject *) PyNumber_Long(PyObject *o);
501
502
/* Returns the object 'o' converted to a float object on success, or NULL
503
on failure.
504
505
This is the equivalent of the Python expression: float(o). */
506
PyAPI_FUNC(PyObject *) PyNumber_Float(PyObject *o);
507
508
509
/* --- In-place variants of (some of) the above number protocol functions -- */
510
511
/* Returns the result of adding o2 to o1, possibly in-place, or NULL
512
on failure.
513
514
This is the equivalent of the Python expression: o1 += o2. */
515
PyAPI_FUNC(PyObject *) PyNumber_InPlaceAdd(PyObject *o1, PyObject *o2);
516
517
/* Returns the result of subtracting o2 from o1, possibly in-place or
518
NULL on failure.
519
520
This is the equivalent of the Python expression: o1 -= o2. */
521
PyAPI_FUNC(PyObject *) PyNumber_InPlaceSubtract(PyObject *o1, PyObject *o2);
522
523
/* Returns the result of multiplying o1 by o2, possibly in-place, or NULL on
524
failure.
525
526
This is the equivalent of the Python expression: o1 *= o2. */
527
PyAPI_FUNC(PyObject *) PyNumber_InPlaceMultiply(PyObject *o1, PyObject *o2);
528
529
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000
530
/* This is the equivalent of the Python expression: o1 @= o2. */
531
PyAPI_FUNC(PyObject *) PyNumber_InPlaceMatrixMultiply(PyObject *o1, PyObject *o2);
532
#endif
533
534
/* Returns the result of dividing o1 by o2 giving an integral result, possibly
535
in-place, or NULL on failure.
536
537
This is the equivalent of the Python expression: o1 /= o2. */
538
PyAPI_FUNC(PyObject *) PyNumber_InPlaceFloorDivide(PyObject *o1,
539
PyObject *o2);
540
541
/* Returns the result of dividing o1 by o2 giving a float result, possibly
542
in-place, or null on failure.
543
544
This is the equivalent of the Python expression: o1 /= o2. */
545
PyAPI_FUNC(PyObject *) PyNumber_InPlaceTrueDivide(PyObject *o1,
546
PyObject *o2);
547
548
/* Returns the remainder of dividing o1 by o2, possibly in-place, or NULL on
549
failure.
550
551
This is the equivalent of the Python expression: o1 %= o2. */
552
PyAPI_FUNC(PyObject *) PyNumber_InPlaceRemainder(PyObject *o1, PyObject *o2);
553
554
/* Returns the result of raising o1 to the power of o2, possibly in-place,
555
or NULL on failure.
556
557
This is the equivalent of the Python expression: o1 **= o2,
558
or o1 = pow(o1, o2, o3) if o3 is present. */
559
PyAPI_FUNC(PyObject *) PyNumber_InPlacePower(PyObject *o1, PyObject *o2,
560
PyObject *o3);
561
562
/* Returns the result of left shifting o1 by o2, possibly in-place, or NULL
563
on failure.
564
565
This is the equivalent of the Python expression: o1 <<= o2. */
566
PyAPI_FUNC(PyObject *) PyNumber_InPlaceLshift(PyObject *o1, PyObject *o2);
567
568
/* Returns the result of right shifting o1 by o2, possibly in-place or NULL
569
on failure.
570
571
This is the equivalent of the Python expression: o1 >>= o2. */
572
PyAPI_FUNC(PyObject *) PyNumber_InPlaceRshift(PyObject *o1, PyObject *o2);
573
574
/* Returns the result of bitwise and of o1 and o2, possibly in-place, or NULL
575
on failure.
576
577
This is the equivalent of the Python expression: o1 &= o2. */
578
PyAPI_FUNC(PyObject *) PyNumber_InPlaceAnd(PyObject *o1, PyObject *o2);
579
580
/* Returns the bitwise exclusive or of o1 by o2, possibly in-place, or NULL
581
on failure.
582
583
This is the equivalent of the Python expression: o1 ^= o2. */
584
PyAPI_FUNC(PyObject *) PyNumber_InPlaceXor(PyObject *o1, PyObject *o2);
585
586
/* Returns the result of bitwise or of o1 and o2, possibly in-place,
587
or NULL on failure.
588
589
This is the equivalent of the Python expression: o1 |= o2. */
590
PyAPI_FUNC(PyObject *) PyNumber_InPlaceOr(PyObject *o1, PyObject *o2);
591
592
/* Returns the integer n converted to a string with a base, with a base
593
marker of 0b, 0o or 0x prefixed if applicable.
594
595
If n is not an int object, it is converted with PyNumber_Index first. */
596
PyAPI_FUNC(PyObject *) PyNumber_ToBase(PyObject *n, int base);
597
598
599
/* === Sequence protocol ================================================ */
600
601
/* Return 1 if the object provides sequence protocol, and zero
602
otherwise.
603
604
This function always succeeds. */
605
PyAPI_FUNC(int) PySequence_Check(PyObject *o);
606
607
/* Return the size of sequence object o, or -1 on failure. */
608
PyAPI_FUNC(Py_ssize_t) PySequence_Size(PyObject *o);
609
610
/* For DLL compatibility */
611
#undef PySequence_Length
612
PyAPI_FUNC(Py_ssize_t) PySequence_Length(PyObject *o);
613
#define PySequence_Length PySequence_Size
614
615
616
/* Return the concatenation of o1 and o2 on success, and NULL on failure.
617
618
This is the equivalent of the Python expression: o1 + o2. */
619
PyAPI_FUNC(PyObject *) PySequence_Concat(PyObject *o1, PyObject *o2);
620
621
/* Return the result of repeating sequence object 'o' 'count' times,
622
or NULL on failure.
623
624
This is the equivalent of the Python expression: o * count. */
625
PyAPI_FUNC(PyObject *) PySequence_Repeat(PyObject *o, Py_ssize_t count);
626
627
/* Return the ith element of o, or NULL on failure.
628
629
This is the equivalent of the Python expression: o[i]. */
630
PyAPI_FUNC(PyObject *) PySequence_GetItem(PyObject *o, Py_ssize_t i);
631
632
/* Return the slice of sequence object o between i1 and i2, or NULL on failure.
633
634
This is the equivalent of the Python expression: o[i1:i2]. */
635
PyAPI_FUNC(PyObject *) PySequence_GetSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2);
636
637
/* Assign object 'v' to the ith element of the sequence 'o'. Raise an exception
638
and return -1 on failure; return 0 on success.
639
640
This is the equivalent of the Python statement o[i] = v. */
641
PyAPI_FUNC(int) PySequence_SetItem(PyObject *o, Py_ssize_t i, PyObject *v);
642
643
/* Delete the 'i'-th element of the sequence 'v'. Returns -1 on failure.
644
645
This is the equivalent of the Python statement: del o[i]. */
646
PyAPI_FUNC(int) PySequence_DelItem(PyObject *o, Py_ssize_t i);
647
648
/* Assign the sequence object 'v' to the slice in sequence object 'o',
649
from 'i1' to 'i2'. Returns -1 on failure.
650
651
This is the equivalent of the Python statement: o[i1:i2] = v. */
652
PyAPI_FUNC(int) PySequence_SetSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2,
653
PyObject *v);
654
655
/* Delete the slice in sequence object 'o' from 'i1' to 'i2'.
656
Returns -1 on failure.
657
658
This is the equivalent of the Python statement: del o[i1:i2]. */
659
PyAPI_FUNC(int) PySequence_DelSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2);
660
661
/* Returns the sequence 'o' as a tuple on success, and NULL on failure.
662
663
This is equivalent to the Python expression: tuple(o). */
664
PyAPI_FUNC(PyObject *) PySequence_Tuple(PyObject *o);
665
666
/* Returns the sequence 'o' as a list on success, and NULL on failure.
667
This is equivalent to the Python expression: list(o) */
668
PyAPI_FUNC(PyObject *) PySequence_List(PyObject *o);
669
670
/* Return the sequence 'o' as a list, unless it's already a tuple or list.
671
672
Use PySequence_Fast_GET_ITEM to access the members of this list, and
673
PySequence_Fast_GET_SIZE to get its length.
674
675
Returns NULL on failure. If the object does not support iteration, raises a
676
TypeError exception with 'm' as the message text. */
677
PyAPI_FUNC(PyObject *) PySequence_Fast(PyObject *o, const char* m);
678
679
/* Return the size of the sequence 'o', assuming that 'o' was returned by
680
PySequence_Fast and is not NULL. */
681
#define PySequence_Fast_GET_SIZE(o) \
682
(PyList_Check(o) ? PyList_GET_SIZE(o) : PyTuple_GET_SIZE(o))
683
684
/* Return the 'i'-th element of the sequence 'o', assuming that o was returned
685
by PySequence_Fast, and that i is within bounds. */
686
#define PySequence_Fast_GET_ITEM(o, i)\
687
(PyList_Check(o) ? PyList_GET_ITEM((o), (i)) : PyTuple_GET_ITEM((o), (i)))
688
689
/* Return a pointer to the underlying item array for
690
an object returned by PySequence_Fast */
691
#define PySequence_Fast_ITEMS(sf) \
692
(PyList_Check(sf) ? ((PyListObject *)(sf))->ob_item \
693
: ((PyTupleObject *)(sf))->ob_item)
694
695
/* Return the number of occurrences on value on 'o', that is, return
696
the number of keys for which o[key] == value.
697
698
On failure, return -1. This is equivalent to the Python expression:
699
o.count(value). */
700
PyAPI_FUNC(Py_ssize_t) PySequence_Count(PyObject *o, PyObject *value);
701
702
/* Return 1 if 'ob' is in the sequence 'seq'; 0 if 'ob' is not in the sequence
703
'seq'; -1 on error.
704
705
Use __contains__ if possible, else _PySequence_IterSearch(). */
706
PyAPI_FUNC(int) PySequence_Contains(PyObject *seq, PyObject *ob);
707
708
/* For DLL-level backwards compatibility */
709
#undef PySequence_In
710
/* Determine if the sequence 'o' contains 'value'. If an item in 'o' is equal
711
to 'value', return 1, otherwise return 0. On error, return -1.
712
713
This is equivalent to the Python expression: value in o. */
714
PyAPI_FUNC(int) PySequence_In(PyObject *o, PyObject *value);
715
716
/* For source-level backwards compatibility */
717
#define PySequence_In PySequence_Contains
718
719
720
/* Return the first index for which o[i] == value.
721
On error, return -1.
722
723
This is equivalent to the Python expression: o.index(value). */
724
PyAPI_FUNC(Py_ssize_t) PySequence_Index(PyObject *o, PyObject *value);
725
726
727
/* --- In-place versions of some of the above Sequence functions --- */
728
729
/* Append sequence 'o2' to sequence 'o1', in-place when possible. Return the
730
resulting object, which could be 'o1', or NULL on failure.
731
732
This is the equivalent of the Python expression: o1 += o2. */
733
PyAPI_FUNC(PyObject *) PySequence_InPlaceConcat(PyObject *o1, PyObject *o2);
734
735
/* Repeat sequence 'o' by 'count', in-place when possible. Return the resulting
736
object, which could be 'o', or NULL on failure.
737
738
This is the equivalent of the Python expression: o1 *= count. */
739
PyAPI_FUNC(PyObject *) PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count);
740
741
742
/* === Mapping protocol ================================================= */
743
744
/* Return 1 if the object provides mapping protocol, and 0 otherwise.
745
746
This function always succeeds. */
747
PyAPI_FUNC(int) PyMapping_Check(PyObject *o);
748
749
/* Returns the number of keys in mapping object 'o' on success, and -1 on
750
failure. This is equivalent to the Python expression: len(o). */
751
PyAPI_FUNC(Py_ssize_t) PyMapping_Size(PyObject *o);
752
753
/* For DLL compatibility */
754
#undef PyMapping_Length
755
PyAPI_FUNC(Py_ssize_t) PyMapping_Length(PyObject *o);
756
#define PyMapping_Length PyMapping_Size
757
758
759
/* Implemented as a macro:
760
761
int PyMapping_DelItemString(PyObject *o, const char *key);
762
763
Remove the mapping for the string 'key' from the mapping 'o'. Returns -1 on
764
failure.
765
766
This is equivalent to the Python statement: del o[key]. */
767
#define PyMapping_DelItemString(O, K) PyObject_DelItemString((O), (K))
768
769
/* Implemented as a macro:
770
771
int PyMapping_DelItem(PyObject *o, PyObject *key);
772
773
Remove the mapping for the object 'key' from the mapping object 'o'.
774
Returns -1 on failure.
775
776
This is equivalent to the Python statement: del o[key]. */
777
#define PyMapping_DelItem(O, K) PyObject_DelItem((O), (K))
778
779
/* On success, return 1 if the mapping object 'o' has the key 'key',
780
and 0 otherwise.
781
782
This is equivalent to the Python expression: key in o.
783
784
This function always succeeds. */
785
PyAPI_FUNC(int) PyMapping_HasKeyString(PyObject *o, const char *key);
786
787
/* Return 1 if the mapping object has the key 'key', and 0 otherwise.
788
789
This is equivalent to the Python expression: key in o.
790
791
This function always succeeds. */
792
PyAPI_FUNC(int) PyMapping_HasKey(PyObject *o, PyObject *key);
793
794
/* On success, return a list or tuple of the keys in mapping object 'o'.
795
On failure, return NULL. */
796
PyAPI_FUNC(PyObject *) PyMapping_Keys(PyObject *o);
797
798
/* On success, return a list or tuple of the values in mapping object 'o'.
799
On failure, return NULL. */
800
PyAPI_FUNC(PyObject *) PyMapping_Values(PyObject *o);
801
802
/* On success, return a list or tuple of the items in mapping object 'o',
803
where each item is a tuple containing a key-value pair. On failure, return
804
NULL. */
805
PyAPI_FUNC(PyObject *) PyMapping_Items(PyObject *o);
806
807
/* Return element of 'o' corresponding to the string 'key' or NULL on failure.
808
809
This is the equivalent of the Python expression: o[key]. */
810
PyAPI_FUNC(PyObject *) PyMapping_GetItemString(PyObject *o,
811
const char *key);
812
813
/* Map the string 'key' to the value 'v' in the mapping 'o'.
814
Returns -1 on failure.
815
816
This is the equivalent of the Python statement: o[key]=v. */
817
PyAPI_FUNC(int) PyMapping_SetItemString(PyObject *o, const char *key,
818
PyObject *value);
819
820
/* isinstance(object, typeorclass) */
821
PyAPI_FUNC(int) PyObject_IsInstance(PyObject *object, PyObject *typeorclass);
822
823
/* issubclass(object, typeorclass) */
824
PyAPI_FUNC(int) PyObject_IsSubclass(PyObject *object, PyObject *typeorclass);
825
826
#ifndef Py_LIMITED_API
827
# define Py_CPYTHON_ABSTRACTOBJECT_H
828
# include "cpython/abstract.h"
829
# undef Py_CPYTHON_ABSTRACTOBJECT_H
830
#endif
831
832
#ifdef __cplusplus
833
}
834
#endif
835
#endif /* Py_ABSTRACTOBJECT_H */
836
837