blob: adace8badd51b2904e2ca8d42279a7efcb47781c [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/***********************************************************
8Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
9The Netherlands.
10
11 All Rights Reserved
12
Guido van Rossumfd71b9e2000-06-30 23:50:40 +000013Copyright (c) 2000, BeOpen.com.
14Copyright (c) 1995-2000, Corporation for National Research Initiatives.
15Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
16All rights reserved.
Guido van Rossuma8275371995-07-18 14:07:00 +000017
Guido van Rossumfd71b9e2000-06-30 23:50:40 +000018See the file "Misc/COPYRIGHT" for information on usage and
19redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Guido van Rossuma8275371995-07-18 14:07:00 +000020
21******************************************************************/
22
23/* Abstract Object Interface (many thanks to Jim Fulton) */
24
25/*
26 PROPOSAL: A Generic Python Object Interface for Python C Modules
27
28Problem
29
30 Python modules written in C that must access Python objects must do
31 so through routines whose interfaces are described by a set of
32 include files. Unfortunately, these routines vary according to the
33 object accessed. To use these routines, the C programmer must check
34 the type of the object being used and must call a routine based on
35 the object type. For example, to access an element of a sequence,
36 the programmer must determine whether the sequence is a list or a
37 tuple:
38
39 if(is_tupleobject(o))
40 e=gettupleitem(o,i)
41 else if(is_listitem(o))
42 e=getlistitem(o,i)
43
44 If the programmer wants to get an item from another type of object
45 that provides sequence behavior, there is no clear way to do it
46 correctly.
47
48 The persistent programmer may peruse object.h and find that the
49 _typeobject structure provides a means of invoking up to (currently
50 about) 41 special operators. So, for example, a routine can get an
51 item from any object that provides sequence behavior. However, to
52 use this mechanism, the programmer must make their code dependent on
53 the current Python implementation.
54
55 Also, certain semantics, especially memory management semantics, may
56 differ by the type of object being used. Unfortunately, these
57 semantics are not clearly described in the current include files.
58 An abstract interface providing more consistent semantics is needed.
59
60Proposal
61
62 I propose the creation of a standard interface (with an associated
63 library of routines and/or macros) for generically obtaining the
64 services of Python objects. This proposal can be viewed as one
65 components of a Python C interface consisting of several components.
66
67 From the viewpoint of of C access to Python services, we have (as
68 suggested by Guido in off-line discussions):
69
70 - "Very high level layer": two or three functions that let you exec or
71 eval arbitrary Python code given as a string in a module whose name is
72 given, passing C values in and getting C values out using
73 mkvalue/getargs style format strings. This does not require the user
74 to declare any variables of type "PyObject *". This should be enough
75 to write a simple application that gets Python code from the user,
76 execs it, and returns the output or errors. (Error handling must also
77 be part of this API.)
78
79 - "Abstract objects layer": which is the subject of this proposal.
80 It has many functions operating on objects, and lest you do many
81 things from C that you can also write in Python, without going
82 through the Python parser.
83
84 - "Concrete objects layer": This is the public type-dependent
85 interface provided by the standard built-in types, such as floats,
86 strings, and lists. This interface exists and is currently
87 documented by the collection of include files provides with the
88 Python distributions.
89
90 From the point of view of Python accessing services provided by C
91 modules:
92
93 - "Python module interface": this interface consist of the basic
94 routines used to define modules and their members. Most of the
95 current extensions-writing guide deals with this interface.
96
97 - "Built-in object interface": this is the interface that a new
98 built-in type must provide and the mechanisms and rules that a
99 developer of a new built-in type must use and follow.
100
101 This proposal is a "first-cut" that is intended to spur
102 discussion. See especially the lists of notes.
103
104 The Python C object interface will provide four protocols: object,
105 numeric, sequence, and mapping. Each protocol consists of a
106 collection of related operations. If an operation that is not
107 provided by a particular type is invoked, then a standard exception,
108 NotImplementedError is raised with a operation name as an argument.
109 In addition, for convenience this interface defines a set of
110 constructors for building objects of built-in types. This is needed
111 so new objects can be returned from C functions that otherwise treat
112 objects generically.
113
114Memory Management
115
116 For all of the functions described in this proposal, if a function
117 retains a reference to a Python object passed as an argument, then the
118 function will increase the reference count of the object. It is
119 unnecessary for the caller to increase the reference count of an
120 argument in anticipation of the object's retention.
121
122 All Python objects returned from functions should be treated as new
123 objects. Functions that return objects assume that the caller will
124 retain a reference and the reference count of the object has already
125 been incremented to account for this fact. A caller that does not
126 retain a reference to an object that is returned from a function
127 must decrement the reference count of the object (using
128 DECREF(object)) to prevent memory leaks.
129
130 Note that the behavior mentioned here is different from the current
131 behavior for some objects (e.g. lists and tuples) when certain
132 type-specific routines are called directly (e.g. setlistitem). The
133 proposed abstraction layer will provide a consistent memory
134 management interface, correcting for inconsistent behavior for some
135 built-in types.
136
137Protocols
138
139xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
140
141/* Object Protocol: */
142
143 /* Implemented elsewhere:
144
145 int PyObject_Print(PyObject *o, FILE *fp, int flags);
146
147 Print an object, o, on file, fp. Returns -1 on
148 error. The flags argument is used to enable certain printing
149 options. The only option currently supported is Py_Print_RAW.
150
151 (What should be said about Py_Print_RAW?)
152
153 */
154
155 /* Implemented elsewhere:
156
157 int PyObject_HasAttrString(PyObject *o, char *attr_name);
158
159 Returns 1 if o has the attribute attr_name, and 0 otherwise.
160 This is equivalent to the Python expression:
161 hasattr(o,attr_name).
162
163 This function always succeeds.
164
165 */
166
167 /* Implemented elsewhere:
168
169 PyObject* PyObject_GetAttrString(PyObject *o, char *attr_name);
170
171 Retrieve an attributed named attr_name form object o.
172 Returns the attribute value on success, or NULL on failure.
173 This is the equivalent of the Python expression: o.attr_name.
174
175 */
176
177 /* Implemented elsewhere:
178
179 int PyObject_HasAttr(PyObject *o, PyObject *attr_name);
180
181 Returns 1 if o has the attribute attr_name, and 0 otherwise.
182 This is equivalent to the Python expression:
183 hasattr(o,attr_name).
184
185 This function always succeeds.
186
187 */
188
189 /* Implemented elsewhere:
190
191 PyObject* PyObject_GetAttr(PyObject *o, PyObject *attr_name);
192
193 Retrieve an attributed named attr_name form object o.
194 Returns the attribute value on success, or NULL on failure.
195 This is the equivalent of the Python expression: o.attr_name.
196
197 */
198
199
200 /* Implemented elsewhere:
201
202 int PyObject_SetAttrString(PyObject *o, char *attr_name, PyObject *v);
203
204 Set the value of the attribute named attr_name, for object o,
205 to the value, v. Returns -1 on failure. This is
206 the equivalent of the Python statement: o.attr_name=v.
207
208 */
209
210 /* Implemented elsewhere:
211
212 int PyObject_SetAttr(PyObject *o, PyObject *attr_name, PyObject *v);
213
214 Set the value of the attribute named attr_name, for object o,
215 to the value, v. Returns -1 on failure. This is
216 the equivalent of the Python statement: o.attr_name=v.
217
218 */
219
220 /* implemented as a macro:
221
222 int PyObject_DelAttrString(PyObject *o, char *attr_name);
223
224 Delete attribute named attr_name, for object o. Returns
225 -1 on failure. This is the equivalent of the Python
226 statement: del o.attr_name.
227
228 */
229#define PyObject_DelAttrString(O,A) PyObject_SetAttrString((O),(A),NULL)
230
231 /* implemented as a macro:
232
233 int PyObject_DelAttr(PyObject *o, PyObject *attr_name);
234
235 Delete attribute named attr_name, for object o. Returns -1
236 on failure. This is the equivalent of the Python
237 statement: del o.attr_name.
238
239 */
240#define PyObject_DelAttr(O,A) PyObject_SetAttr((O),(A),NULL)
241
Guido van Rossum43466ec1998-12-04 18:48:25 +0000242 DL_IMPORT(int) PyObject_Cmp Py_PROTO((PyObject *o1, PyObject *o2, int *result));
Guido van Rossuma8275371995-07-18 14:07:00 +0000243
244 /*
245 Compare the values of o1 and o2 using a routine provided by
246 o1, if one exists, otherwise with a routine provided by o2.
247 The result of the comparison is returned in result. Returns
248 -1 on failure. This is the equivalent of the Python
249 statement: result=cmp(o1,o2).
250
251 */
252
253 /* Implemented elsewhere:
254
255 int PyObject_Compare(PyObject *o1, PyObject *o2);
256
257 Compare the values of o1 and o2 using a routine provided by
258 o1, if one exists, otherwise with a routine provided by o2.
259 Returns the result of the comparison on success. On error,
260 the value returned is undefined. This is equivalent to the
261 Python expression: cmp(o1,o2).
262
263 */
264
265 /* Implemented elsewhere:
266
267 PyObject *PyObject_Repr(PyObject *o);
268
269 Compute the string representation of object, o. Returns the
270 string representation on success, NULL on failure. This is
271 the equivalent of the Python expression: repr(o).
272
273 Called by the repr() built-in function and by reverse quotes.
274
275 */
276
277 /* Implemented elsewhere:
278
279 PyObject *PyObject_Str(PyObject *o);
280
281 Compute the string representation of object, o. Returns the
282 string representation on success, NULL on failure. This is
283 the equivalent of the Python expression: str(o).)
284
285 Called by the str() built-in function and by the print
286 statement.
287
288 */
289
Guido van Rossum43466ec1998-12-04 18:48:25 +0000290 DL_IMPORT(int) PyCallable_Check Py_PROTO((PyObject *o));
Guido van Rossuma8275371995-07-18 14:07:00 +0000291
292 /*
293 Determine if the object, o, is callable. Return 1 if the
294 object is callable and 0 otherwise.
295
296 This function always succeeds.
297
298 */
299
300
301
Guido van Rossum43466ec1998-12-04 18:48:25 +0000302 DL_IMPORT(PyObject *) PyObject_CallObject Py_PROTO((PyObject *callable_object,
Guido van Rossum8ca687a1995-09-18 21:20:02 +0000303 PyObject *args));
Guido van Rossuma8275371995-07-18 14:07:00 +0000304
305 /*
306
307 Call a callable Python object, callable_object, with
308 arguments given by the tuple, args. If no arguments are
309 needed, then args may be NULL. Returns the result of the
310 call on success, or NULL on failure. This is the equivalent
311 of the Python expression: apply(o,args).
312
313 */
314
Guido van Rossum43466ec1998-12-04 18:48:25 +0000315 DL_IMPORT(PyObject *) PyObject_CallFunction Py_PROTO((PyObject *callable_object,
Guido van Rossum8ca687a1995-09-18 21:20:02 +0000316 char *format, ...));
Guido van Rossuma8275371995-07-18 14:07:00 +0000317
318 /*
319 Call a callable Python object, callable_object, with a
320 variable number of C arguments. The C arguments are described
321 using a mkvalue-style format string. The format may be NULL,
322 indicating that no arguments are provided. Returns the
323 result of the call on success, or NULL on failure. This is
324 the equivalent of the Python expression: apply(o,args).
325
326 */
327
328
Guido van Rossum43466ec1998-12-04 18:48:25 +0000329 DL_IMPORT(PyObject *) PyObject_CallMethod Py_PROTO((PyObject *o, char *m,
Guido van Rossum8ca687a1995-09-18 21:20:02 +0000330 char *format, ...));
Guido van Rossuma8275371995-07-18 14:07:00 +0000331
332 /*
333 Call the method named m of object o with a variable number of
334 C arguments. The C arguments are described by a mkvalue
335 format string. The format may be NULL, indicating that no
336 arguments are provided. Returns the result of the call on
337 success, or NULL on failure. This is the equivalent of the
338 Python expression: o.method(args).
339
340 Note that Special method names, such as "__add__",
341 "__getitem__", and so on are not supported. The specific
342 abstract-object routines for these must be used.
343
344 */
345
346
347 /* Implemented elsewhere:
348
349 long PyObject_Hash(PyObject *o);
350
351 Compute and return the hash, hash_value, of an object, o. On
352 failure, return -1. This is the equivalent of the Python
353 expression: hash(o).
354
355 */
356
357
358 /* Implemented elsewhere:
359
360 int PyObject_IsTrue(PyObject *o);
361
362 Returns 1 if the object, o, is considered to be true, and
363 0 otherwise. This is equivalent to the Python expression:
364 not not o
365
366 This function always succeeds.
367
368 */
369
Guido van Rossumc3d3f961998-04-09 17:53:59 +0000370 /* Implemented elsewhere:
371
372 int PyObject_Not(PyObject *o);
373
374 Returns 0 if the object, o, is considered to be true, and
375 1 otherwise. This is equivalent to the Python expression:
376 not o
377
378 This function always succeeds.
379
380 */
381
Guido van Rossum43466ec1998-12-04 18:48:25 +0000382 DL_IMPORT(PyObject *) PyObject_Type Py_PROTO((PyObject *o));
Guido van Rossuma8275371995-07-18 14:07:00 +0000383
384 /*
385 On success, returns a type object corresponding to the object
386 type of object o. On failure, returns NULL. This is
387 equivalent to the Python expression: type(o).
388 */
389
Guido van Rossum43466ec1998-12-04 18:48:25 +0000390 DL_IMPORT(int) PyObject_Length Py_PROTO((PyObject *o));
Guido van Rossuma8275371995-07-18 14:07:00 +0000391
392 /*
393 Return the length of object o. If the object, o, provides
394 both sequence and mapping protocols, the sequence length is
395 returned. On error, -1 is returned. This is the equivalent
396 to the Python expression: len(o).
397
398 */
399
Guido van Rossum43466ec1998-12-04 18:48:25 +0000400 DL_IMPORT(PyObject *) PyObject_GetItem Py_PROTO((PyObject *o, PyObject *key));
Guido van Rossuma8275371995-07-18 14:07:00 +0000401
402 /*
403 Return element of o corresponding to the object, key, or NULL
404 on failure. This is the equivalent of the Python expression:
405 o[key].
406
407 */
408
Guido van Rossum43466ec1998-12-04 18:48:25 +0000409 DL_IMPORT(int) PyObject_SetItem Py_PROTO((PyObject *o, PyObject *key, PyObject *v));
Guido van Rossuma8275371995-07-18 14:07:00 +0000410
411 /*
412 Map the object, key, to the value, v. Returns
413 -1 on failure. This is the equivalent of the Python
414 statement: o[key]=v.
415 */
416
Guido van Rossum43466ec1998-12-04 18:48:25 +0000417 DL_IMPORT(int) PyObject_DelItem Py_PROTO((PyObject *o, PyObject *key));
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000418
419 /*
420 Delete the mapping for key from *o. Returns -1 on failure.
421 This is the equivalent of the Python statement: del o[key].
422 */
423
Guido van Rossumfd9eed32000-03-10 22:35:06 +0000424 DL_IMPORT(int) PyObject_AsCharBuffer(PyObject *obj,
425 const char **buffer,
426 int *buffer_len);
427
428 /*
429 Takes an arbitrary object which must support the (character,
430 single segment) buffer interface and returns a pointer to a
431 read-only memory location useable as character based input
432 for subsequent processing.
433
434 0 is returned on success. buffer and buffer_len are only
435 set in case no error occurrs. Otherwise, -1 is returned and
436 an exception set.
437
438 */
439
440 DL_IMPORT(int) PyObject_AsReadBuffer(PyObject *obj,
441 const void **buffer,
442 int *buffer_len);
443
444 /*
445 Same as PyObject_AsCharBuffer() except that this API expects
446 (readable, single segment) buffer interface and returns a
447 pointer to a read-only memory location which can contain
448 arbitrary data.
449
450 0 is returned on success. buffer and buffer_len are only
451 set in case no error occurrs. Otherwise, -1 is returned and
452 an exception set.
453
454 */
455
456 DL_IMPORT(int) PyObject_AsWriteBuffer(PyObject *obj,
457 void **buffer,
458 int *buffer_len);
459
460 /*
461 Takes an arbitrary object which must support the (writeable,
462 single segment) buffer interface and returns a pointer to a
463 writeable memory location in buffer of size buffer_len.
464
465 0 is returned on success. buffer and buffer_len are only
466 set in case no error occurrs. Otherwise, -1 is returned and
467 an exception set.
468
469 */
Guido van Rossuma8275371995-07-18 14:07:00 +0000470
471/* Number Protocol:*/
472
Guido van Rossum43466ec1998-12-04 18:48:25 +0000473 DL_IMPORT(int) PyNumber_Check Py_PROTO((PyObject *o));
Guido van Rossuma8275371995-07-18 14:07:00 +0000474
475 /*
476 Returns 1 if the object, o, provides numeric protocols, and
477 false otherwise.
478
479 This function always succeeds.
480
481 */
482
Guido van Rossum43466ec1998-12-04 18:48:25 +0000483 DL_IMPORT(PyObject *) PyNumber_Add Py_PROTO((PyObject *o1, PyObject *o2));
Guido van Rossuma8275371995-07-18 14:07:00 +0000484
485 /*
486 Returns the result of adding o1 and o2, or null on failure.
487 This is the equivalent of the Python expression: o1+o2.
488
489
490 */
491
Guido van Rossum43466ec1998-12-04 18:48:25 +0000492 DL_IMPORT(PyObject *) PyNumber_Subtract Py_PROTO((PyObject *o1, PyObject *o2));
Guido van Rossuma8275371995-07-18 14:07:00 +0000493
494 /*
495 Returns the result of subtracting o2 from o1, or null on
496 failure. This is the equivalent of the Python expression:
497 o1-o2.
498
499 */
500
Guido van Rossum43466ec1998-12-04 18:48:25 +0000501 DL_IMPORT(PyObject *) PyNumber_Multiply Py_PROTO((PyObject *o1, PyObject *o2));
Guido van Rossuma8275371995-07-18 14:07:00 +0000502
503 /*
504 Returns the result of multiplying o1 and o2, or null on
505 failure. This is the equivalent of the Python expression:
506 o1*o2.
507
508
509 */
510
Guido van Rossum43466ec1998-12-04 18:48:25 +0000511 DL_IMPORT(PyObject *) PyNumber_Divide Py_PROTO((PyObject *o1, PyObject *o2));
Guido van Rossuma8275371995-07-18 14:07:00 +0000512
513 /*
514 Returns the result of dividing o1 by o2, or null on failure.
515 This is the equivalent of the Python expression: o1/o2.
516
517
518 */
519
Guido van Rossum43466ec1998-12-04 18:48:25 +0000520 DL_IMPORT(PyObject *) PyNumber_Remainder Py_PROTO((PyObject *o1, PyObject *o2));
Guido van Rossuma8275371995-07-18 14:07:00 +0000521
522 /*
523 Returns the remainder of dividing o1 by o2, or null on
524 failure. This is the equivalent of the Python expression:
525 o1%o2.
526
527
528 */
529
Guido van Rossum43466ec1998-12-04 18:48:25 +0000530 DL_IMPORT(PyObject *) PyNumber_Divmod Py_PROTO((PyObject *o1, PyObject *o2));
Guido van Rossuma8275371995-07-18 14:07:00 +0000531
532 /*
533 See the built-in function divmod. Returns NULL on failure.
534 This is the equivalent of the Python expression:
535 divmod(o1,o2).
536
537
538 */
539
Guido van Rossum43466ec1998-12-04 18:48:25 +0000540 DL_IMPORT(PyObject *) PyNumber_Power Py_PROTO((PyObject *o1, PyObject *o2, PyObject *o3));
Guido van Rossuma8275371995-07-18 14:07:00 +0000541
542 /*
543 See the built-in function pow. Returns NULL on failure.
544 This is the equivalent of the Python expression:
545 pow(o1,o2,o3), where o3 is optional.
546
547 */
548
Guido van Rossum43466ec1998-12-04 18:48:25 +0000549 DL_IMPORT(PyObject *) PyNumber_Negative Py_PROTO((PyObject *o));
Guido van Rossuma8275371995-07-18 14:07:00 +0000550
551 /*
552 Returns the negation of o on success, or null on failure.
553 This is the equivalent of the Python expression: -o.
554
555 */
556
Guido van Rossum43466ec1998-12-04 18:48:25 +0000557 DL_IMPORT(PyObject *) PyNumber_Positive Py_PROTO((PyObject *o));
Guido van Rossuma8275371995-07-18 14:07:00 +0000558
559 /*
560 Returns the (what?) of o on success, or NULL on failure.
561 This is the equivalent of the Python expression: +o.
562
563 */
564
Guido van Rossum43466ec1998-12-04 18:48:25 +0000565 DL_IMPORT(PyObject *) PyNumber_Absolute Py_PROTO((PyObject *o));
Guido van Rossuma8275371995-07-18 14:07:00 +0000566
567 /*
568 Returns the absolute value of o, or null on failure. This is
569 the equivalent of the Python expression: abs(o).
570
571 */
572
Guido van Rossum43466ec1998-12-04 18:48:25 +0000573 DL_IMPORT(PyObject *) PyNumber_Invert Py_PROTO((PyObject *o));
Guido van Rossuma8275371995-07-18 14:07:00 +0000574
575 /*
576 Returns the bitwise negation of o on success, or NULL on
577 failure. This is the equivalent of the Python expression:
578 ~o.
579
580
581 */
582
Guido van Rossum43466ec1998-12-04 18:48:25 +0000583 DL_IMPORT(PyObject *) PyNumber_Lshift Py_PROTO((PyObject *o1, PyObject *o2));
Guido van Rossuma8275371995-07-18 14:07:00 +0000584
585 /*
586 Returns the result of left shifting o1 by o2 on success, or
587 NULL on failure. This is the equivalent of the Python
588 expression: o1 << o2.
589
590
591 */
592
Guido van Rossum43466ec1998-12-04 18:48:25 +0000593 DL_IMPORT(PyObject *) PyNumber_Rshift Py_PROTO((PyObject *o1, PyObject *o2));
Guido van Rossuma8275371995-07-18 14:07:00 +0000594
595 /*
596 Returns the result of right shifting o1 by o2 on success, or
597 NULL on failure. This is the equivalent of the Python
598 expression: o1 >> o2.
599
600 */
601
Guido van Rossum43466ec1998-12-04 18:48:25 +0000602 DL_IMPORT(PyObject *) PyNumber_And Py_PROTO((PyObject *o1, PyObject *o2));
Guido van Rossuma8275371995-07-18 14:07:00 +0000603
604 /*
Guido van Rossum1ca407f1997-02-14 22:51:40 +0000605 Returns the result of bitwise and of o1 and o2 on success, or
606 NULL on failure. This is the equivalent of the Python
607 expression: o1&o2.
Guido van Rossuma8275371995-07-18 14:07:00 +0000608
609
610 */
611
Guido van Rossum43466ec1998-12-04 18:48:25 +0000612 DL_IMPORT(PyObject *) PyNumber_Xor Py_PROTO((PyObject *o1, PyObject *o2));
Guido van Rossuma8275371995-07-18 14:07:00 +0000613
614 /*
615 Returns the bitwise exclusive or of o1 by o2 on success, or
616 NULL on failure. This is the equivalent of the Python
617 expression: o1^o2.
618
619
620 */
621
Guido van Rossum43466ec1998-12-04 18:48:25 +0000622 DL_IMPORT(PyObject *) PyNumber_Or Py_PROTO((PyObject *o1, PyObject *o2));
Guido van Rossuma8275371995-07-18 14:07:00 +0000623
624 /*
Guido van Rossum1ca407f1997-02-14 22:51:40 +0000625 Returns the result of bitwise or or o1 and o2 on success, or
626 NULL on failure. This is the equivalent of the Python
627 expression: o1|o2.
Guido van Rossuma8275371995-07-18 14:07:00 +0000628
629 */
630
631 /* Implemented elsewhere:
632
Guido van Rossumed227f01996-09-06 13:40:53 +0000633 int PyNumber_Coerce(PyObject **p1, PyObject **p2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000634
Guido van Rossumed227f01996-09-06 13:40:53 +0000635 This function takes the addresses of two variables of type
636 PyObject*.
637
638 If the objects pointed to by *p1 and *p2 have the same type,
639 increment their reference count and return 0 (success).
640 If the objects can be converted to a common numeric type,
641 replace *p1 and *p2 by their converted value (with 'new'
642 reference counts), and return 0.
643 If no conversion is possible, or if some other error occurs,
644 return -1 (failure) and don't increment the reference counts.
645 The call PyNumber_Coerce(&o1, &o2) is equivalent to the Python
646 statement o1, o2 = coerce(o1, o2).
Guido van Rossuma8275371995-07-18 14:07:00 +0000647
648 */
649
Guido van Rossum43466ec1998-12-04 18:48:25 +0000650 DL_IMPORT(PyObject *) PyNumber_Int Py_PROTO((PyObject *o));
Guido van Rossuma8275371995-07-18 14:07:00 +0000651
652 /*
653 Returns the o converted to an integer object on success, or
654 NULL on failure. This is the equivalent of the Python
655 expression: int(o).
656
657 */
658
Guido van Rossum43466ec1998-12-04 18:48:25 +0000659 DL_IMPORT(PyObject *) PyNumber_Long Py_PROTO((PyObject *o));
Guido van Rossuma8275371995-07-18 14:07:00 +0000660
661 /*
662 Returns the o converted to a long integer object on success,
663 or NULL on failure. This is the equivalent of the Python
664 expression: long(o).
665
666 */
667
Guido van Rossum43466ec1998-12-04 18:48:25 +0000668 DL_IMPORT(PyObject *) PyNumber_Float Py_PROTO((PyObject *o));
Guido van Rossuma8275371995-07-18 14:07:00 +0000669
670 /*
671 Returns the o converted to a float object on success, or NULL
672 on failure. This is the equivalent of the Python expression:
673 float(o).
674 */
675
676
677/* Sequence protocol:*/
678
Guido van Rossum43466ec1998-12-04 18:48:25 +0000679 DL_IMPORT(int) PySequence_Check Py_PROTO((PyObject *o));
Guido van Rossuma8275371995-07-18 14:07:00 +0000680
681 /*
682 Return 1 if the object provides sequence protocol, and zero
683 otherwise.
684
685 This function always succeeds.
686
687 */
688
Guido van Rossum43466ec1998-12-04 18:48:25 +0000689 DL_IMPORT(int) PySequence_Length Py_PROTO((PyObject *o));
Guido van Rossum4f4ce681996-07-21 02:22:56 +0000690
691 /*
692 Return the length of sequence object o, or -1 on failure.
693
694 */
695
Guido van Rossum43466ec1998-12-04 18:48:25 +0000696 DL_IMPORT(PyObject *) PySequence_Concat Py_PROTO((PyObject *o1, PyObject *o2));
Guido van Rossuma8275371995-07-18 14:07:00 +0000697
698 /*
699 Return the concatination of o1 and o2 on success, and NULL on
700 failure. This is the equivalent of the Python
701 expression: o1+o2.
702
703 */
704
Guido van Rossum43466ec1998-12-04 18:48:25 +0000705 DL_IMPORT(PyObject *) PySequence_Repeat Py_PROTO((PyObject *o, int count));
Guido van Rossuma8275371995-07-18 14:07:00 +0000706
707 /*
708 Return the result of repeating sequence object o count times,
709 or NULL on failure. This is the equivalent of the Python
710 expression: o1*count.
711
712 */
713
Guido van Rossum43466ec1998-12-04 18:48:25 +0000714 DL_IMPORT(PyObject *) PySequence_GetItem Py_PROTO((PyObject *o, int i));
Guido van Rossuma8275371995-07-18 14:07:00 +0000715
716 /*
717 Return the ith element of o, or NULL on failure. This is the
718 equivalent of the Python expression: o[i].
Guido van Rossuma8275371995-07-18 14:07:00 +0000719 */
720
Guido van Rossum43466ec1998-12-04 18:48:25 +0000721 DL_IMPORT(PyObject *) PySequence_GetSlice Py_PROTO((PyObject *o, int i1, int i2));
Guido van Rossuma8275371995-07-18 14:07:00 +0000722
723 /*
724 Return the slice of sequence object o between i1 and i2, or
725 NULL on failure. This is the equivalent of the Python
726 expression: o[i1:i2].
727
728 */
729
Guido van Rossum43466ec1998-12-04 18:48:25 +0000730 DL_IMPORT(int) PySequence_SetItem Py_PROTO((PyObject *o, int i, PyObject *v));
Guido van Rossuma8275371995-07-18 14:07:00 +0000731
732 /*
733 Assign object v to the ith element of o. Returns
734 -1 on failure. This is the equivalent of the Python
735 statement: o[i]=v.
736
737 */
738
Guido van Rossum43466ec1998-12-04 18:48:25 +0000739 DL_IMPORT(int) PySequence_DelItem Py_PROTO((PyObject *o, int i));
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000740
741 /*
742 Delete the ith element of object v. Returns
743 -1 on failure. This is the equivalent of the Python
744 statement: del o[i].
745 */
746
Guido van Rossum43466ec1998-12-04 18:48:25 +0000747 DL_IMPORT(int) PySequence_SetSlice Py_PROTO((PyObject *o, int i1, int i2, PyObject *v));
Guido van Rossuma8275371995-07-18 14:07:00 +0000748
749 /*
750 Assign the sequence object, v, to the slice in sequence
751 object, o, from i1 to i2. Returns -1 on failure. This is the
752 equivalent of the Python statement: o[i1:i2]=v.
753 */
754
Guido van Rossum43466ec1998-12-04 18:48:25 +0000755 DL_IMPORT(int) PySequence_DelSlice Py_PROTO((PyObject *o, int i1, int i2));
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000756
757 /*
758 Delete the slice in sequence object, o, from i1 to i2.
759 Returns -1 on failure. This is the equivalent of the Python
760 statement: del o[i1:i2].
761 */
762
Guido van Rossum43466ec1998-12-04 18:48:25 +0000763 DL_IMPORT(PyObject *) PySequence_Tuple Py_PROTO((PyObject *o));
Guido van Rossuma8275371995-07-18 14:07:00 +0000764
765 /*
Guido van Rossumf39fc431997-03-04 18:31:47 +0000766 Returns the sequence, o, as a tuple on success, and NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000767 This is equivalent to the Python expression: tuple(o)
768 */
769
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000770
Guido van Rossum43466ec1998-12-04 18:48:25 +0000771 DL_IMPORT(PyObject *) PySequence_List Py_PROTO((PyObject *o));
Guido van Rossumf39fc431997-03-04 18:31:47 +0000772
Guido van Rossum2adf06b1996-12-05 21:48:50 +0000773 /*
Guido van Rossumf39fc431997-03-04 18:31:47 +0000774 Returns the sequence, o, as a list on success, and NULL on failure.
775 This is equivalent to the Python expression: list(o)
Guido van Rossum2adf06b1996-12-05 21:48:50 +0000776 */
Guido van Rossumf39fc431997-03-04 18:31:47 +0000777
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000778 DL_IMPORT(PyObject *) PySequence_Fast Py_PROTO((PyObject *o, const char* m));
779
780 /*
781 Returns the sequence, o, as a tuple, unless it's already a
782 tuple or list. Use PySequence_Fast_GET_ITEM to access the
783 members of this list.
784
785 Returns NULL on failure. If the object is not a sequence,
786 raises a TypeError exception with m as the message text.
787 */
788
789#define PySequence_Fast_GET_ITEM(o, i)\
790 (PyList_Check(o) ? PyList_GET_ITEM(o, i) : PyTuple_GET_ITEM(o, i))
791
792 /*
793 Return the ith element of o, assuming that o was returned by
794 PySequence_Fast, and that i is within bounds.
795 */
796
Guido van Rossum43466ec1998-12-04 18:48:25 +0000797 DL_IMPORT(int) PySequence_Count Py_PROTO((PyObject *o, PyObject *value));
Guido van Rossuma8275371995-07-18 14:07:00 +0000798
799 /*
800 Return the number of occurrences on value on o, that is,
801 return the number of keys for which o[key]==value. On
802 failure, return -1. This is equivalent to the Python
803 expression: o.count(value).
804 */
805
Guido van Rossum43466ec1998-12-04 18:48:25 +0000806 DL_IMPORT(int) PySequence_Contains Py_PROTO((PyObject *o, PyObject *value));
Guido van Rossum83684531999-03-17 18:44:39 +0000807
808/* For DLL-level backwards compatibility */
809#undef PySequence_In
810 DL_IMPORT(int) PySequence_In Py_PROTO((PyObject *o, PyObject *value));
811
812/* For source-level backwards compatibility */
Guido van Rossumf1536db1998-08-23 22:06:59 +0000813#define PySequence_In PySequence_Contains
Guido van Rossuma8275371995-07-18 14:07:00 +0000814
815 /*
816 Determine if o contains value. If an item in o is equal to
817 X, return 1, otherwise return 0. On error, return -1. This
818 is equivalent to the Python expression: value in o.
819 */
820
Guido van Rossum43466ec1998-12-04 18:48:25 +0000821 DL_IMPORT(int) PySequence_Index Py_PROTO((PyObject *o, PyObject *value));
Guido van Rossuma8275371995-07-18 14:07:00 +0000822
823 /*
824 Return the first index for which o[i]=value. On error,
825 return -1. This is equivalent to the Python
826 expression: o.index(value).
827 */
828
829/* Mapping protocol:*/
830
Guido van Rossum43466ec1998-12-04 18:48:25 +0000831 DL_IMPORT(int) PyMapping_Check Py_PROTO((PyObject *o));
Guido van Rossuma8275371995-07-18 14:07:00 +0000832
833 /*
834 Return 1 if the object provides mapping protocol, and zero
835 otherwise.
836
837 This function always succeeds.
838 */
839
Guido van Rossum43466ec1998-12-04 18:48:25 +0000840 DL_IMPORT(int) PyMapping_Length Py_PROTO((PyObject *o));
Guido van Rossuma8275371995-07-18 14:07:00 +0000841
842 /*
843 Returns the number of keys in object o on success, and -1 on
844 failure. For objects that do not provide sequence protocol,
845 this is equivalent to the Python expression: len(o).
846 */
847
Guido van Rossuma25e5e91996-09-06 13:48:38 +0000848 /* implemented as a macro:
849
Guido van Rossum8ca687a1995-09-18 21:20:02 +0000850 int PyMapping_DelItemString Py_PROTO((PyObject *o, char *key));
Guido van Rossuma8275371995-07-18 14:07:00 +0000851
Guido van Rossuma8275371995-07-18 14:07:00 +0000852 Remove the mapping for object, key, from the object *o.
853 Returns -1 on failure. This is equivalent to
854 the Python statement: del o[key].
855 */
Guido van Rossuma25e5e91996-09-06 13:48:38 +0000856#define PyMapping_DelItemString(O,K) PyDict_DelItemString((O),(K))
857
858 /* implemented as a macro:
Guido van Rossuma8275371995-07-18 14:07:00 +0000859
Guido van Rossum8ca687a1995-09-18 21:20:02 +0000860 int PyMapping_DelItem Py_PROTO((PyObject *o, PyObject *key));
Guido van Rossuma8275371995-07-18 14:07:00 +0000861
Guido van Rossuma8275371995-07-18 14:07:00 +0000862 Remove the mapping for object, key, from the object *o.
863 Returns -1 on failure. This is equivalent to
864 the Python statement: del o[key].
865 */
Guido van Rossuma25e5e91996-09-06 13:48:38 +0000866#define PyMapping_DelItem(O,K) PyDict_DelItem((O),(K))
Guido van Rossuma8275371995-07-18 14:07:00 +0000867
Guido van Rossum43466ec1998-12-04 18:48:25 +0000868 DL_IMPORT(int) PyMapping_HasKeyString Py_PROTO((PyObject *o, char *key));
Guido van Rossuma8275371995-07-18 14:07:00 +0000869
870 /*
871 On success, return 1 if the mapping object has the key, key,
872 and 0 otherwise. This is equivalent to the Python expression:
873 o.has_key(key).
874
875 This function always succeeds.
876 */
877
Guido van Rossum43466ec1998-12-04 18:48:25 +0000878 DL_IMPORT(int) PyMapping_HasKey Py_PROTO((PyObject *o, PyObject *key));
Guido van Rossuma8275371995-07-18 14:07:00 +0000879
880 /*
881 Return 1 if the mapping object has the key, key,
882 and 0 otherwise. This is equivalent to the Python expression:
883 o.has_key(key).
884
885 This function always succeeds.
886
887 */
888
889 /* Implemented as macro:
890
891 PyObject *PyMapping_Keys(PyObject *o);
892
893 On success, return a list of the keys in object o. On
894 failure, return NULL. This is equivalent to the Python
895 expression: o.keys().
896 */
897#define PyMapping_Keys(O) PyObject_CallMethod(O,"keys",NULL)
898
899 /* Implemented as macro:
900
901 PyObject *PyMapping_Values(PyObject *o);
902
903 On success, return a list of the values in object o. On
904 failure, return NULL. This is equivalent to the Python
905 expression: o.values().
906 */
907#define PyMapping_Values(O) PyObject_CallMethod(O,"values",NULL)
908
909 /* Implemented as macro:
910
911 PyObject *PyMapping_Items(PyObject *o);
912
913 On success, return a list of the items in object o, where
914 each item is a tuple containing a key-value pair. On
915 failure, return NULL. This is equivalent to the Python
916 expression: o.items().
917
918 */
919#define PyMapping_Items(O) PyObject_CallMethod(O,"items",NULL)
920
Guido van Rossum43466ec1998-12-04 18:48:25 +0000921 DL_IMPORT(PyObject *) PyMapping_GetItemString Py_PROTO((PyObject *o, char *key));
Guido van Rossuma8275371995-07-18 14:07:00 +0000922
923 /*
924 Return element of o corresponding to the object, key, or NULL
925 on failure. This is the equivalent of the Python expression:
926 o[key].
927 */
928
Guido van Rossum43466ec1998-12-04 18:48:25 +0000929 DL_IMPORT(int) PyMapping_SetItemString Py_PROTO((PyObject *o, char *key,
Guido van Rossum8ca687a1995-09-18 21:20:02 +0000930 PyObject *value));
Guido van Rossuma8275371995-07-18 14:07:00 +0000931
932 /*
933 Map the object, key, to the value, v. Returns
934 -1 on failure. This is the equivalent of the Python
935 statement: o[key]=v.
936 */
937
938
Guido van Rossum8ca687a1995-09-18 21:20:02 +0000939#ifdef __cplusplus
940}
941#endif
Guido van Rossuma8275371995-07-18 14:07:00 +0000942#endif /* Py_ABSTRACTOBJECT_H */