blob: f1808061f12879c9bc78c37af6cf62197391f98f [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
7/***********************************************************
Guido van Rossumfd71b9e2000-06-30 23:50:40 +00008Copyright (c) 2000, BeOpen.com.
9Copyright (c) 1995-2000, Corporation for National Research Initiatives.
10Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
11All rights reserved.
Guido van Rossuma8275371995-07-18 14:07:00 +000012
Guido van Rossumfd71b9e2000-06-30 23:50:40 +000013See the file "Misc/COPYRIGHT" for information on usage and
14redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossuma8275371995-07-18 14:07:00 +000015******************************************************************/
16
17/* Abstract Object Interface (many thanks to Jim Fulton) */
18
19/*
20 PROPOSAL: A Generic Python Object Interface for Python C Modules
21
22Problem
23
24 Python modules written in C that must access Python objects must do
25 so through routines whose interfaces are described by a set of
26 include files. Unfortunately, these routines vary according to the
27 object accessed. To use these routines, the C programmer must check
28 the type of the object being used and must call a routine based on
29 the object type. For example, to access an element of a sequence,
30 the programmer must determine whether the sequence is a list or a
31 tuple:
32
33 if(is_tupleobject(o))
34 e=gettupleitem(o,i)
35 else if(is_listitem(o))
36 e=getlistitem(o,i)
37
38 If the programmer wants to get an item from another type of object
39 that provides sequence behavior, there is no clear way to do it
40 correctly.
41
42 The persistent programmer may peruse object.h and find that the
43 _typeobject structure provides a means of invoking up to (currently
44 about) 41 special operators. So, for example, a routine can get an
45 item from any object that provides sequence behavior. However, to
46 use this mechanism, the programmer must make their code dependent on
47 the current Python implementation.
48
49 Also, certain semantics, especially memory management semantics, may
50 differ by the type of object being used. Unfortunately, these
51 semantics are not clearly described in the current include files.
52 An abstract interface providing more consistent semantics is needed.
53
54Proposal
55
56 I propose the creation of a standard interface (with an associated
57 library of routines and/or macros) for generically obtaining the
58 services of Python objects. This proposal can be viewed as one
59 components of a Python C interface consisting of several components.
60
61 From the viewpoint of of C access to Python services, we have (as
62 suggested by Guido in off-line discussions):
63
64 - "Very high level layer": two or three functions that let you exec or
65 eval arbitrary Python code given as a string in a module whose name is
66 given, passing C values in and getting C values out using
67 mkvalue/getargs style format strings. This does not require the user
68 to declare any variables of type "PyObject *". This should be enough
69 to write a simple application that gets Python code from the user,
70 execs it, and returns the output or errors. (Error handling must also
71 be part of this API.)
72
73 - "Abstract objects layer": which is the subject of this proposal.
74 It has many functions operating on objects, and lest you do many
75 things from C that you can also write in Python, without going
76 through the Python parser.
77
78 - "Concrete objects layer": This is the public type-dependent
79 interface provided by the standard built-in types, such as floats,
80 strings, and lists. This interface exists and is currently
81 documented by the collection of include files provides with the
82 Python distributions.
83
84 From the point of view of Python accessing services provided by C
85 modules:
86
87 - "Python module interface": this interface consist of the basic
88 routines used to define modules and their members. Most of the
89 current extensions-writing guide deals with this interface.
90
91 - "Built-in object interface": this is the interface that a new
92 built-in type must provide and the mechanisms and rules that a
93 developer of a new built-in type must use and follow.
94
95 This proposal is a "first-cut" that is intended to spur
96 discussion. See especially the lists of notes.
97
98 The Python C object interface will provide four protocols: object,
99 numeric, sequence, and mapping. Each protocol consists of a
100 collection of related operations. If an operation that is not
101 provided by a particular type is invoked, then a standard exception,
102 NotImplementedError is raised with a operation name as an argument.
103 In addition, for convenience this interface defines a set of
104 constructors for building objects of built-in types. This is needed
105 so new objects can be returned from C functions that otherwise treat
106 objects generically.
107
108Memory Management
109
110 For all of the functions described in this proposal, if a function
111 retains a reference to a Python object passed as an argument, then the
112 function will increase the reference count of the object. It is
113 unnecessary for the caller to increase the reference count of an
114 argument in anticipation of the object's retention.
115
116 All Python objects returned from functions should be treated as new
117 objects. Functions that return objects assume that the caller will
118 retain a reference and the reference count of the object has already
119 been incremented to account for this fact. A caller that does not
120 retain a reference to an object that is returned from a function
121 must decrement the reference count of the object (using
122 DECREF(object)) to prevent memory leaks.
123
124 Note that the behavior mentioned here is different from the current
125 behavior for some objects (e.g. lists and tuples) when certain
126 type-specific routines are called directly (e.g. setlistitem). The
127 proposed abstraction layer will provide a consistent memory
128 management interface, correcting for inconsistent behavior for some
129 built-in types.
130
131Protocols
132
133xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
134
135/* Object Protocol: */
136
137 /* Implemented elsewhere:
138
139 int PyObject_Print(PyObject *o, FILE *fp, int flags);
140
141 Print an object, o, on file, fp. Returns -1 on
142 error. The flags argument is used to enable certain printing
143 options. The only option currently supported is Py_Print_RAW.
144
145 (What should be said about Py_Print_RAW?)
146
147 */
148
149 /* Implemented elsewhere:
150
151 int PyObject_HasAttrString(PyObject *o, char *attr_name);
152
153 Returns 1 if o has the attribute attr_name, and 0 otherwise.
154 This is equivalent to the Python expression:
155 hasattr(o,attr_name).
156
157 This function always succeeds.
158
159 */
160
161 /* Implemented elsewhere:
162
163 PyObject* PyObject_GetAttrString(PyObject *o, char *attr_name);
164
165 Retrieve an attributed named attr_name form object o.
166 Returns the attribute value on success, or NULL on failure.
167 This is the equivalent of the Python expression: o.attr_name.
168
169 */
170
171 /* Implemented elsewhere:
172
173 int PyObject_HasAttr(PyObject *o, PyObject *attr_name);
174
175 Returns 1 if o has the attribute attr_name, and 0 otherwise.
176 This is equivalent to the Python expression:
177 hasattr(o,attr_name).
178
179 This function always succeeds.
180
181 */
182
183 /* Implemented elsewhere:
184
185 PyObject* PyObject_GetAttr(PyObject *o, PyObject *attr_name);
186
187 Retrieve an attributed named attr_name form object o.
188 Returns the attribute value on success, or NULL on failure.
189 This is the equivalent of the Python expression: o.attr_name.
190
191 */
192
193
194 /* Implemented elsewhere:
195
196 int PyObject_SetAttrString(PyObject *o, char *attr_name, PyObject *v);
197
198 Set the value of the attribute named attr_name, for object o,
199 to the value, v. Returns -1 on failure. This is
200 the equivalent of the Python statement: o.attr_name=v.
201
202 */
203
204 /* Implemented elsewhere:
205
206 int PyObject_SetAttr(PyObject *o, PyObject *attr_name, PyObject *v);
207
208 Set the value of the attribute named attr_name, for object o,
209 to the value, v. Returns -1 on failure. This is
210 the equivalent of the Python statement: o.attr_name=v.
211
212 */
213
214 /* implemented as a macro:
215
216 int PyObject_DelAttrString(PyObject *o, char *attr_name);
217
218 Delete attribute named attr_name, for object o. Returns
219 -1 on failure. This is the equivalent of the Python
220 statement: del o.attr_name.
221
222 */
223#define PyObject_DelAttrString(O,A) PyObject_SetAttrString((O),(A),NULL)
224
225 /* implemented as a macro:
226
227 int PyObject_DelAttr(PyObject *o, PyObject *attr_name);
228
229 Delete attribute named attr_name, for object o. Returns -1
230 on failure. This is the equivalent of the Python
231 statement: del o.attr_name.
232
233 */
234#define PyObject_DelAttr(O,A) PyObject_SetAttr((O),(A),NULL)
235
Guido van Rossum43466ec1998-12-04 18:48:25 +0000236 DL_IMPORT(int) PyObject_Cmp Py_PROTO((PyObject *o1, PyObject *o2, int *result));
Guido van Rossuma8275371995-07-18 14:07:00 +0000237
238 /*
239 Compare the values of o1 and o2 using a routine provided by
240 o1, if one exists, otherwise with a routine provided by o2.
241 The result of the comparison is returned in result. Returns
242 -1 on failure. This is the equivalent of the Python
243 statement: result=cmp(o1,o2).
244
245 */
246
247 /* Implemented elsewhere:
248
249 int PyObject_Compare(PyObject *o1, PyObject *o2);
250
251 Compare the values of o1 and o2 using a routine provided by
252 o1, if one exists, otherwise with a routine provided by o2.
253 Returns the result of the comparison on success. On error,
254 the value returned is undefined. This is equivalent to the
255 Python expression: cmp(o1,o2).
256
257 */
258
259 /* Implemented elsewhere:
260
261 PyObject *PyObject_Repr(PyObject *o);
262
263 Compute the string representation of object, o. Returns the
264 string representation on success, NULL on failure. This is
265 the equivalent of the Python expression: repr(o).
266
267 Called by the repr() built-in function and by reverse quotes.
268
269 */
270
271 /* Implemented elsewhere:
272
273 PyObject *PyObject_Str(PyObject *o);
274
275 Compute the string representation of object, o. Returns the
276 string representation on success, NULL on failure. This is
277 the equivalent of the Python expression: str(o).)
278
279 Called by the str() built-in function and by the print
280 statement.
281
282 */
283
Guido van Rossum43466ec1998-12-04 18:48:25 +0000284 DL_IMPORT(int) PyCallable_Check Py_PROTO((PyObject *o));
Guido van Rossuma8275371995-07-18 14:07:00 +0000285
286 /*
287 Determine if the object, o, is callable. Return 1 if the
288 object is callable and 0 otherwise.
289
290 This function always succeeds.
291
292 */
293
294
295
Guido van Rossum43466ec1998-12-04 18:48:25 +0000296 DL_IMPORT(PyObject *) PyObject_CallObject Py_PROTO((PyObject *callable_object,
Guido van Rossum8ca687a1995-09-18 21:20:02 +0000297 PyObject *args));
Guido van Rossuma8275371995-07-18 14:07:00 +0000298
299 /*
300
301 Call a callable Python object, callable_object, with
302 arguments given by the tuple, args. If no arguments are
303 needed, then args may be NULL. Returns the result of the
304 call on success, or NULL on failure. This is the equivalent
305 of the Python expression: apply(o,args).
306
307 */
308
Guido van Rossum43466ec1998-12-04 18:48:25 +0000309 DL_IMPORT(PyObject *) PyObject_CallFunction Py_PROTO((PyObject *callable_object,
Guido van Rossum8ca687a1995-09-18 21:20:02 +0000310 char *format, ...));
Guido van Rossuma8275371995-07-18 14:07:00 +0000311
312 /*
313 Call a callable Python object, callable_object, with a
314 variable number of C arguments. The C arguments are described
315 using a mkvalue-style format string. The format may be NULL,
316 indicating that no arguments are provided. Returns the
317 result of the call on success, or NULL on failure. This is
318 the equivalent of the Python expression: apply(o,args).
319
320 */
321
322
Guido van Rossum43466ec1998-12-04 18:48:25 +0000323 DL_IMPORT(PyObject *) PyObject_CallMethod Py_PROTO((PyObject *o, char *m,
Guido van Rossum8ca687a1995-09-18 21:20:02 +0000324 char *format, ...));
Guido van Rossuma8275371995-07-18 14:07:00 +0000325
326 /*
327 Call the method named m of object o with a variable number of
328 C arguments. The C arguments are described by a mkvalue
329 format string. The format may be NULL, indicating that no
330 arguments are provided. Returns the result of the call on
331 success, or NULL on failure. This is the equivalent of the
332 Python expression: o.method(args).
333
334 Note that Special method names, such as "__add__",
335 "__getitem__", and so on are not supported. The specific
336 abstract-object routines for these must be used.
337
338 */
339
340
341 /* Implemented elsewhere:
342
343 long PyObject_Hash(PyObject *o);
344
345 Compute and return the hash, hash_value, of an object, o. On
346 failure, return -1. This is the equivalent of the Python
347 expression: hash(o).
348
349 */
350
351
352 /* Implemented elsewhere:
353
354 int PyObject_IsTrue(PyObject *o);
355
356 Returns 1 if the object, o, is considered to be true, and
357 0 otherwise. This is equivalent to the Python expression:
358 not not o
359
360 This function always succeeds.
361
362 */
363
Guido van Rossumc3d3f961998-04-09 17:53:59 +0000364 /* Implemented elsewhere:
365
366 int PyObject_Not(PyObject *o);
367
368 Returns 0 if the object, o, is considered to be true, and
369 1 otherwise. This is equivalent to the Python expression:
370 not o
371
372 This function always succeeds.
373
374 */
375
Guido van Rossum43466ec1998-12-04 18:48:25 +0000376 DL_IMPORT(PyObject *) PyObject_Type Py_PROTO((PyObject *o));
Guido van Rossuma8275371995-07-18 14:07:00 +0000377
378 /*
379 On success, returns a type object corresponding to the object
380 type of object o. On failure, returns NULL. This is
381 equivalent to the Python expression: type(o).
382 */
383
Guido van Rossum43466ec1998-12-04 18:48:25 +0000384 DL_IMPORT(int) PyObject_Length Py_PROTO((PyObject *o));
Guido van Rossuma8275371995-07-18 14:07:00 +0000385
386 /*
387 Return the length of object o. If the object, o, provides
388 both sequence and mapping protocols, the sequence length is
389 returned. On error, -1 is returned. This is the equivalent
390 to the Python expression: len(o).
391
392 */
393
Guido van Rossum43466ec1998-12-04 18:48:25 +0000394 DL_IMPORT(PyObject *) PyObject_GetItem Py_PROTO((PyObject *o, PyObject *key));
Guido van Rossuma8275371995-07-18 14:07:00 +0000395
396 /*
397 Return element of o corresponding to the object, key, or NULL
398 on failure. This is the equivalent of the Python expression:
399 o[key].
400
401 */
402
Guido van Rossum43466ec1998-12-04 18:48:25 +0000403 DL_IMPORT(int) PyObject_SetItem Py_PROTO((PyObject *o, PyObject *key, PyObject *v));
Guido van Rossuma8275371995-07-18 14:07:00 +0000404
405 /*
406 Map the object, key, to the value, v. Returns
407 -1 on failure. This is the equivalent of the Python
408 statement: o[key]=v.
409 */
410
Guido van Rossum43466ec1998-12-04 18:48:25 +0000411 DL_IMPORT(int) PyObject_DelItem Py_PROTO((PyObject *o, PyObject *key));
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000412
413 /*
414 Delete the mapping for key from *o. Returns -1 on failure.
415 This is the equivalent of the Python statement: del o[key].
416 */
417
Guido van Rossumfd9eed32000-03-10 22:35:06 +0000418 DL_IMPORT(int) PyObject_AsCharBuffer(PyObject *obj,
419 const char **buffer,
420 int *buffer_len);
421
422 /*
423 Takes an arbitrary object which must support the (character,
424 single segment) buffer interface and returns a pointer to a
425 read-only memory location useable as character based input
426 for subsequent processing.
427
428 0 is returned on success. buffer and buffer_len are only
429 set in case no error occurrs. Otherwise, -1 is returned and
430 an exception set.
431
432 */
433
434 DL_IMPORT(int) PyObject_AsReadBuffer(PyObject *obj,
435 const void **buffer,
436 int *buffer_len);
437
438 /*
439 Same as PyObject_AsCharBuffer() except that this API expects
440 (readable, single segment) buffer interface and returns a
441 pointer to a read-only memory location which can contain
442 arbitrary data.
443
444 0 is returned on success. buffer and buffer_len are only
445 set in case no error occurrs. Otherwise, -1 is returned and
446 an exception set.
447
448 */
449
450 DL_IMPORT(int) PyObject_AsWriteBuffer(PyObject *obj,
451 void **buffer,
452 int *buffer_len);
453
454 /*
455 Takes an arbitrary object which must support the (writeable,
456 single segment) buffer interface and returns a pointer to a
457 writeable memory location in buffer of size buffer_len.
458
459 0 is returned on success. buffer and buffer_len are only
460 set in case no error occurrs. Otherwise, -1 is returned and
461 an exception set.
462
463 */
Guido van Rossuma8275371995-07-18 14:07:00 +0000464
465/* Number Protocol:*/
466
Guido van Rossum43466ec1998-12-04 18:48:25 +0000467 DL_IMPORT(int) PyNumber_Check Py_PROTO((PyObject *o));
Guido van Rossuma8275371995-07-18 14:07:00 +0000468
469 /*
470 Returns 1 if the object, o, provides numeric protocols, and
471 false otherwise.
472
473 This function always succeeds.
474
475 */
476
Guido van Rossum43466ec1998-12-04 18:48:25 +0000477 DL_IMPORT(PyObject *) PyNumber_Add Py_PROTO((PyObject *o1, PyObject *o2));
Guido van Rossuma8275371995-07-18 14:07:00 +0000478
479 /*
480 Returns the result of adding o1 and o2, or null on failure.
481 This is the equivalent of the Python expression: o1+o2.
482
483
484 */
485
Guido van Rossum43466ec1998-12-04 18:48:25 +0000486 DL_IMPORT(PyObject *) PyNumber_Subtract Py_PROTO((PyObject *o1, PyObject *o2));
Guido van Rossuma8275371995-07-18 14:07:00 +0000487
488 /*
489 Returns the result of subtracting o2 from o1, or null on
490 failure. This is the equivalent of the Python expression:
491 o1-o2.
492
493 */
494
Guido van Rossum43466ec1998-12-04 18:48:25 +0000495 DL_IMPORT(PyObject *) PyNumber_Multiply Py_PROTO((PyObject *o1, PyObject *o2));
Guido van Rossuma8275371995-07-18 14:07:00 +0000496
497 /*
498 Returns the result of multiplying o1 and o2, or null on
499 failure. This is the equivalent of the Python expression:
500 o1*o2.
501
502
503 */
504
Guido van Rossum43466ec1998-12-04 18:48:25 +0000505 DL_IMPORT(PyObject *) PyNumber_Divide Py_PROTO((PyObject *o1, PyObject *o2));
Guido van Rossuma8275371995-07-18 14:07:00 +0000506
507 /*
508 Returns the result of dividing o1 by o2, or null on failure.
509 This is the equivalent of the Python expression: o1/o2.
510
511
512 */
513
Guido van Rossum43466ec1998-12-04 18:48:25 +0000514 DL_IMPORT(PyObject *) PyNumber_Remainder Py_PROTO((PyObject *o1, PyObject *o2));
Guido van Rossuma8275371995-07-18 14:07:00 +0000515
516 /*
517 Returns the remainder of dividing o1 by o2, or null on
518 failure. This is the equivalent of the Python expression:
519 o1%o2.
520
521
522 */
523
Guido van Rossum43466ec1998-12-04 18:48:25 +0000524 DL_IMPORT(PyObject *) PyNumber_Divmod Py_PROTO((PyObject *o1, PyObject *o2));
Guido van Rossuma8275371995-07-18 14:07:00 +0000525
526 /*
527 See the built-in function divmod. Returns NULL on failure.
528 This is the equivalent of the Python expression:
529 divmod(o1,o2).
530
531
532 */
533
Guido van Rossum43466ec1998-12-04 18:48:25 +0000534 DL_IMPORT(PyObject *) PyNumber_Power Py_PROTO((PyObject *o1, PyObject *o2, PyObject *o3));
Guido van Rossuma8275371995-07-18 14:07:00 +0000535
536 /*
537 See the built-in function pow. Returns NULL on failure.
538 This is the equivalent of the Python expression:
539 pow(o1,o2,o3), where o3 is optional.
540
541 */
542
Guido van Rossum43466ec1998-12-04 18:48:25 +0000543 DL_IMPORT(PyObject *) PyNumber_Negative Py_PROTO((PyObject *o));
Guido van Rossuma8275371995-07-18 14:07:00 +0000544
545 /*
546 Returns the negation of o on success, or null on failure.
547 This is the equivalent of the Python expression: -o.
548
549 */
550
Guido van Rossum43466ec1998-12-04 18:48:25 +0000551 DL_IMPORT(PyObject *) PyNumber_Positive Py_PROTO((PyObject *o));
Guido van Rossuma8275371995-07-18 14:07:00 +0000552
553 /*
554 Returns the (what?) of o on success, or NULL on failure.
555 This is the equivalent of the Python expression: +o.
556
557 */
558
Guido van Rossum43466ec1998-12-04 18:48:25 +0000559 DL_IMPORT(PyObject *) PyNumber_Absolute Py_PROTO((PyObject *o));
Guido van Rossuma8275371995-07-18 14:07:00 +0000560
561 /*
562 Returns the absolute value of o, or null on failure. This is
563 the equivalent of the Python expression: abs(o).
564
565 */
566
Guido van Rossum43466ec1998-12-04 18:48:25 +0000567 DL_IMPORT(PyObject *) PyNumber_Invert Py_PROTO((PyObject *o));
Guido van Rossuma8275371995-07-18 14:07:00 +0000568
569 /*
570 Returns the bitwise negation of o on success, or NULL on
571 failure. This is the equivalent of the Python expression:
572 ~o.
573
574
575 */
576
Guido van Rossum43466ec1998-12-04 18:48:25 +0000577 DL_IMPORT(PyObject *) PyNumber_Lshift Py_PROTO((PyObject *o1, PyObject *o2));
Guido van Rossuma8275371995-07-18 14:07:00 +0000578
579 /*
580 Returns the result of left shifting o1 by o2 on success, or
581 NULL on failure. This is the equivalent of the Python
582 expression: o1 << o2.
583
584
585 */
586
Guido van Rossum43466ec1998-12-04 18:48:25 +0000587 DL_IMPORT(PyObject *) PyNumber_Rshift Py_PROTO((PyObject *o1, PyObject *o2));
Guido van Rossuma8275371995-07-18 14:07:00 +0000588
589 /*
590 Returns the result of right shifting o1 by o2 on success, or
591 NULL on failure. This is the equivalent of the Python
592 expression: o1 >> o2.
593
594 */
595
Guido van Rossum43466ec1998-12-04 18:48:25 +0000596 DL_IMPORT(PyObject *) PyNumber_And Py_PROTO((PyObject *o1, PyObject *o2));
Guido van Rossuma8275371995-07-18 14:07:00 +0000597
598 /*
Guido van Rossum1ca407f1997-02-14 22:51:40 +0000599 Returns the result of bitwise and of o1 and o2 on success, or
600 NULL on failure. This is the equivalent of the Python
601 expression: o1&o2.
Guido van Rossuma8275371995-07-18 14:07:00 +0000602
603
604 */
605
Guido van Rossum43466ec1998-12-04 18:48:25 +0000606 DL_IMPORT(PyObject *) PyNumber_Xor Py_PROTO((PyObject *o1, PyObject *o2));
Guido van Rossuma8275371995-07-18 14:07:00 +0000607
608 /*
609 Returns the bitwise exclusive or of o1 by o2 on success, or
610 NULL on failure. This is the equivalent of the Python
611 expression: o1^o2.
612
613
614 */
615
Guido van Rossum43466ec1998-12-04 18:48:25 +0000616 DL_IMPORT(PyObject *) PyNumber_Or Py_PROTO((PyObject *o1, PyObject *o2));
Guido van Rossuma8275371995-07-18 14:07:00 +0000617
618 /*
Guido van Rossum1ca407f1997-02-14 22:51:40 +0000619 Returns the result of bitwise or or o1 and o2 on success, or
620 NULL on failure. This is the equivalent of the Python
621 expression: o1|o2.
Guido van Rossuma8275371995-07-18 14:07:00 +0000622
623 */
624
625 /* Implemented elsewhere:
626
Guido van Rossumed227f01996-09-06 13:40:53 +0000627 int PyNumber_Coerce(PyObject **p1, PyObject **p2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000628
Guido van Rossumed227f01996-09-06 13:40:53 +0000629 This function takes the addresses of two variables of type
630 PyObject*.
631
632 If the objects pointed to by *p1 and *p2 have the same type,
633 increment their reference count and return 0 (success).
634 If the objects can be converted to a common numeric type,
635 replace *p1 and *p2 by their converted value (with 'new'
636 reference counts), and return 0.
637 If no conversion is possible, or if some other error occurs,
638 return -1 (failure) and don't increment the reference counts.
639 The call PyNumber_Coerce(&o1, &o2) is equivalent to the Python
640 statement o1, o2 = coerce(o1, o2).
Guido van Rossuma8275371995-07-18 14:07:00 +0000641
642 */
643
Guido van Rossum43466ec1998-12-04 18:48:25 +0000644 DL_IMPORT(PyObject *) PyNumber_Int Py_PROTO((PyObject *o));
Guido van Rossuma8275371995-07-18 14:07:00 +0000645
646 /*
647 Returns the o converted to an integer object on success, or
648 NULL on failure. This is the equivalent of the Python
649 expression: int(o).
650
651 */
652
Guido van Rossum43466ec1998-12-04 18:48:25 +0000653 DL_IMPORT(PyObject *) PyNumber_Long Py_PROTO((PyObject *o));
Guido van Rossuma8275371995-07-18 14:07:00 +0000654
655 /*
656 Returns the o converted to a long integer object on success,
657 or NULL on failure. This is the equivalent of the Python
658 expression: long(o).
659
660 */
661
Guido van Rossum43466ec1998-12-04 18:48:25 +0000662 DL_IMPORT(PyObject *) PyNumber_Float Py_PROTO((PyObject *o));
Guido van Rossuma8275371995-07-18 14:07:00 +0000663
664 /*
665 Returns the o converted to a float object on success, or NULL
666 on failure. This is the equivalent of the Python expression:
667 float(o).
668 */
669
670
671/* Sequence protocol:*/
672
Guido van Rossum43466ec1998-12-04 18:48:25 +0000673 DL_IMPORT(int) PySequence_Check Py_PROTO((PyObject *o));
Guido van Rossuma8275371995-07-18 14:07:00 +0000674
675 /*
676 Return 1 if the object provides sequence protocol, and zero
677 otherwise.
678
679 This function always succeeds.
680
681 */
682
Guido van Rossum43466ec1998-12-04 18:48:25 +0000683 DL_IMPORT(int) PySequence_Length Py_PROTO((PyObject *o));
Guido van Rossum4f4ce681996-07-21 02:22:56 +0000684
685 /*
686 Return the length of sequence object o, or -1 on failure.
687
688 */
689
Guido van Rossum43466ec1998-12-04 18:48:25 +0000690 DL_IMPORT(PyObject *) PySequence_Concat Py_PROTO((PyObject *o1, PyObject *o2));
Guido van Rossuma8275371995-07-18 14:07:00 +0000691
692 /*
693 Return the concatination of o1 and o2 on success, and NULL on
694 failure. This is the equivalent of the Python
695 expression: o1+o2.
696
697 */
698
Guido van Rossum43466ec1998-12-04 18:48:25 +0000699 DL_IMPORT(PyObject *) PySequence_Repeat Py_PROTO((PyObject *o, int count));
Guido van Rossuma8275371995-07-18 14:07:00 +0000700
701 /*
702 Return the result of repeating sequence object o count times,
703 or NULL on failure. This is the equivalent of the Python
704 expression: o1*count.
705
706 */
707
Guido van Rossum43466ec1998-12-04 18:48:25 +0000708 DL_IMPORT(PyObject *) PySequence_GetItem Py_PROTO((PyObject *o, int i));
Guido van Rossuma8275371995-07-18 14:07:00 +0000709
710 /*
711 Return the ith element of o, or NULL on failure. This is the
712 equivalent of the Python expression: o[i].
Guido van Rossuma8275371995-07-18 14:07:00 +0000713 */
714
Guido van Rossum43466ec1998-12-04 18:48:25 +0000715 DL_IMPORT(PyObject *) PySequence_GetSlice Py_PROTO((PyObject *o, int i1, int i2));
Guido van Rossuma8275371995-07-18 14:07:00 +0000716
717 /*
718 Return the slice of sequence object o between i1 and i2, or
719 NULL on failure. This is the equivalent of the Python
720 expression: o[i1:i2].
721
722 */
723
Guido van Rossum43466ec1998-12-04 18:48:25 +0000724 DL_IMPORT(int) PySequence_SetItem Py_PROTO((PyObject *o, int i, PyObject *v));
Guido van Rossuma8275371995-07-18 14:07:00 +0000725
726 /*
727 Assign object v to the ith element of o. Returns
728 -1 on failure. This is the equivalent of the Python
729 statement: o[i]=v.
730
731 */
732
Guido van Rossum43466ec1998-12-04 18:48:25 +0000733 DL_IMPORT(int) PySequence_DelItem Py_PROTO((PyObject *o, int i));
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000734
735 /*
736 Delete the ith element of object v. Returns
737 -1 on failure. This is the equivalent of the Python
738 statement: del o[i].
739 */
740
Guido van Rossum43466ec1998-12-04 18:48:25 +0000741 DL_IMPORT(int) PySequence_SetSlice Py_PROTO((PyObject *o, int i1, int i2, PyObject *v));
Guido van Rossuma8275371995-07-18 14:07:00 +0000742
743 /*
744 Assign the sequence object, v, to the slice in sequence
745 object, o, from i1 to i2. Returns -1 on failure. This is the
746 equivalent of the Python statement: o[i1:i2]=v.
747 */
748
Guido van Rossum43466ec1998-12-04 18:48:25 +0000749 DL_IMPORT(int) PySequence_DelSlice Py_PROTO((PyObject *o, int i1, int i2));
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000750
751 /*
752 Delete the slice in sequence object, o, from i1 to i2.
753 Returns -1 on failure. This is the equivalent of the Python
754 statement: del o[i1:i2].
755 */
756
Guido van Rossum43466ec1998-12-04 18:48:25 +0000757 DL_IMPORT(PyObject *) PySequence_Tuple Py_PROTO((PyObject *o));
Guido van Rossuma8275371995-07-18 14:07:00 +0000758
759 /*
Guido van Rossumf39fc431997-03-04 18:31:47 +0000760 Returns the sequence, o, as a tuple on success, and NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000761 This is equivalent to the Python expression: tuple(o)
762 */
763
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000764
Guido van Rossum43466ec1998-12-04 18:48:25 +0000765 DL_IMPORT(PyObject *) PySequence_List Py_PROTO((PyObject *o));
Guido van Rossumf39fc431997-03-04 18:31:47 +0000766
Guido van Rossum2adf06b1996-12-05 21:48:50 +0000767 /*
Guido van Rossumf39fc431997-03-04 18:31:47 +0000768 Returns the sequence, o, as a list on success, and NULL on failure.
769 This is equivalent to the Python expression: list(o)
Guido van Rossum2adf06b1996-12-05 21:48:50 +0000770 */
Guido van Rossumf39fc431997-03-04 18:31:47 +0000771
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000772 DL_IMPORT(PyObject *) PySequence_Fast Py_PROTO((PyObject *o, const char* m));
773
774 /*
775 Returns the sequence, o, as a tuple, unless it's already a
776 tuple or list. Use PySequence_Fast_GET_ITEM to access the
777 members of this list.
778
779 Returns NULL on failure. If the object is not a sequence,
780 raises a TypeError exception with m as the message text.
781 */
782
783#define PySequence_Fast_GET_ITEM(o, i)\
784 (PyList_Check(o) ? PyList_GET_ITEM(o, i) : PyTuple_GET_ITEM(o, i))
785
786 /*
787 Return the ith element of o, assuming that o was returned by
788 PySequence_Fast, and that i is within bounds.
789 */
790
Guido van Rossum43466ec1998-12-04 18:48:25 +0000791 DL_IMPORT(int) PySequence_Count Py_PROTO((PyObject *o, PyObject *value));
Guido van Rossuma8275371995-07-18 14:07:00 +0000792
793 /*
794 Return the number of occurrences on value on o, that is,
795 return the number of keys for which o[key]==value. On
796 failure, return -1. This is equivalent to the Python
797 expression: o.count(value).
798 */
799
Guido van Rossum43466ec1998-12-04 18:48:25 +0000800 DL_IMPORT(int) PySequence_Contains Py_PROTO((PyObject *o, PyObject *value));
Guido van Rossum83684531999-03-17 18:44:39 +0000801
802/* For DLL-level backwards compatibility */
803#undef PySequence_In
804 DL_IMPORT(int) PySequence_In Py_PROTO((PyObject *o, PyObject *value));
805
806/* For source-level backwards compatibility */
Guido van Rossumf1536db1998-08-23 22:06:59 +0000807#define PySequence_In PySequence_Contains
Guido van Rossuma8275371995-07-18 14:07:00 +0000808
809 /*
810 Determine if o contains value. If an item in o is equal to
811 X, return 1, otherwise return 0. On error, return -1. This
812 is equivalent to the Python expression: value in o.
813 */
814
Guido van Rossum43466ec1998-12-04 18:48:25 +0000815 DL_IMPORT(int) PySequence_Index Py_PROTO((PyObject *o, PyObject *value));
Guido van Rossuma8275371995-07-18 14:07:00 +0000816
817 /*
818 Return the first index for which o[i]=value. On error,
819 return -1. This is equivalent to the Python
820 expression: o.index(value).
821 */
822
823/* Mapping protocol:*/
824
Guido van Rossum43466ec1998-12-04 18:48:25 +0000825 DL_IMPORT(int) PyMapping_Check Py_PROTO((PyObject *o));
Guido van Rossuma8275371995-07-18 14:07:00 +0000826
827 /*
828 Return 1 if the object provides mapping protocol, and zero
829 otherwise.
830
831 This function always succeeds.
832 */
833
Guido van Rossum43466ec1998-12-04 18:48:25 +0000834 DL_IMPORT(int) PyMapping_Length Py_PROTO((PyObject *o));
Guido van Rossuma8275371995-07-18 14:07:00 +0000835
836 /*
837 Returns the number of keys in object o on success, and -1 on
838 failure. For objects that do not provide sequence protocol,
839 this is equivalent to the Python expression: len(o).
840 */
841
Guido van Rossuma25e5e91996-09-06 13:48:38 +0000842 /* implemented as a macro:
843
Guido van Rossum8ca687a1995-09-18 21:20:02 +0000844 int PyMapping_DelItemString Py_PROTO((PyObject *o, char *key));
Guido van Rossuma8275371995-07-18 14:07:00 +0000845
Guido van Rossuma8275371995-07-18 14:07:00 +0000846 Remove the mapping for object, key, from the object *o.
847 Returns -1 on failure. This is equivalent to
848 the Python statement: del o[key].
849 */
Guido van Rossuma25e5e91996-09-06 13:48:38 +0000850#define PyMapping_DelItemString(O,K) PyDict_DelItemString((O),(K))
851
852 /* implemented as a macro:
Guido van Rossuma8275371995-07-18 14:07:00 +0000853
Guido van Rossum8ca687a1995-09-18 21:20:02 +0000854 int PyMapping_DelItem Py_PROTO((PyObject *o, PyObject *key));
Guido van Rossuma8275371995-07-18 14:07:00 +0000855
Guido van Rossuma8275371995-07-18 14:07:00 +0000856 Remove the mapping for object, key, from the object *o.
857 Returns -1 on failure. This is equivalent to
858 the Python statement: del o[key].
859 */
Guido van Rossuma25e5e91996-09-06 13:48:38 +0000860#define PyMapping_DelItem(O,K) PyDict_DelItem((O),(K))
Guido van Rossuma8275371995-07-18 14:07:00 +0000861
Guido van Rossum43466ec1998-12-04 18:48:25 +0000862 DL_IMPORT(int) PyMapping_HasKeyString Py_PROTO((PyObject *o, char *key));
Guido van Rossuma8275371995-07-18 14:07:00 +0000863
864 /*
865 On success, return 1 if the mapping object has the key, key,
866 and 0 otherwise. This is equivalent to the Python expression:
867 o.has_key(key).
868
869 This function always succeeds.
870 */
871
Guido van Rossum43466ec1998-12-04 18:48:25 +0000872 DL_IMPORT(int) PyMapping_HasKey Py_PROTO((PyObject *o, PyObject *key));
Guido van Rossuma8275371995-07-18 14:07:00 +0000873
874 /*
875 Return 1 if the mapping object has the key, key,
876 and 0 otherwise. This is equivalent to the Python expression:
877 o.has_key(key).
878
879 This function always succeeds.
880
881 */
882
883 /* Implemented as macro:
884
885 PyObject *PyMapping_Keys(PyObject *o);
886
887 On success, return a list of the keys in object o. On
888 failure, return NULL. This is equivalent to the Python
889 expression: o.keys().
890 */
891#define PyMapping_Keys(O) PyObject_CallMethod(O,"keys",NULL)
892
893 /* Implemented as macro:
894
895 PyObject *PyMapping_Values(PyObject *o);
896
897 On success, return a list of the values in object o. On
898 failure, return NULL. This is equivalent to the Python
899 expression: o.values().
900 */
901#define PyMapping_Values(O) PyObject_CallMethod(O,"values",NULL)
902
903 /* Implemented as macro:
904
905 PyObject *PyMapping_Items(PyObject *o);
906
907 On success, return a list of the items in object o, where
908 each item is a tuple containing a key-value pair. On
909 failure, return NULL. This is equivalent to the Python
910 expression: o.items().
911
912 */
913#define PyMapping_Items(O) PyObject_CallMethod(O,"items",NULL)
914
Guido van Rossum43466ec1998-12-04 18:48:25 +0000915 DL_IMPORT(PyObject *) PyMapping_GetItemString Py_PROTO((PyObject *o, char *key));
Guido van Rossuma8275371995-07-18 14:07:00 +0000916
917 /*
918 Return element of o corresponding to the object, key, or NULL
919 on failure. This is the equivalent of the Python expression:
920 o[key].
921 */
922
Guido van Rossum43466ec1998-12-04 18:48:25 +0000923 DL_IMPORT(int) PyMapping_SetItemString Py_PROTO((PyObject *o, char *key,
Guido van Rossum8ca687a1995-09-18 21:20:02 +0000924 PyObject *value));
Guido van Rossuma8275371995-07-18 14:07:00 +0000925
926 /*
927 Map the object, key, to the value, v. Returns
928 -1 on failure. This is the equivalent of the Python
929 statement: o[key]=v.
930 */
931
932
Guido van Rossum8ca687a1995-09-18 21:20:02 +0000933#ifdef __cplusplus
934}
935#endif
Guido van Rossuma8275371995-07-18 14:07:00 +0000936#endif /* Py_ABSTRACTOBJECT_H */