blob: 890ffcde7dfbdde0a846ef870db20fd535abe25b [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
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000236 DL_IMPORT(int) PyObject_Cmp(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
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000284 DL_IMPORT(int) PyCallable_Check(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
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000296 DL_IMPORT(PyObject *) PyObject_CallObject(PyObject *callable_object,
297 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
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000309 DL_IMPORT(PyObject *) PyObject_CallFunction(PyObject *callable_object,
310 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
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000323 DL_IMPORT(PyObject *) PyObject_CallMethod(PyObject *o, char *m,
324 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
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000376 DL_IMPORT(PyObject *) PyObject_Type(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
Jeremy Hylton6253f832000-07-12 12:56:19 +0000384 DL_IMPORT(int) PyObject_Size(PyObject *o);
385
Jeremy Hyltonfa521f12000-07-13 19:39:15 +0000386#define PyObject_Length PyObject_Size
Guido van Rossuma8275371995-07-18 14:07:00 +0000387
388 /*
Jeremy Hylton6253f832000-07-12 12:56:19 +0000389 Return the size of object o. If the object, o, provides
390 both sequence and mapping protocols, the sequence size is
Guido van Rossuma8275371995-07-18 14:07:00 +0000391 returned. On error, -1 is returned. This is the equivalent
392 to the Python expression: len(o).
393
394 */
395
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000396 DL_IMPORT(PyObject *) PyObject_GetItem(PyObject *o, PyObject *key);
Guido van Rossuma8275371995-07-18 14:07:00 +0000397
398 /*
399 Return element of o corresponding to the object, key, or NULL
400 on failure. This is the equivalent of the Python expression:
401 o[key].
402
403 */
404
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000405 DL_IMPORT(int) PyObject_SetItem(PyObject *o, PyObject *key, PyObject *v);
Guido van Rossuma8275371995-07-18 14:07:00 +0000406
407 /*
408 Map the object, key, to the value, v. Returns
409 -1 on failure. This is the equivalent of the Python
410 statement: o[key]=v.
411 */
412
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000413 DL_IMPORT(int) PyObject_DelItem(PyObject *o, PyObject *key);
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000414
415 /*
416 Delete the mapping for key from *o. Returns -1 on failure.
417 This is the equivalent of the Python statement: del o[key].
418 */
419
Guido van Rossumfd9eed32000-03-10 22:35:06 +0000420 DL_IMPORT(int) PyObject_AsCharBuffer(PyObject *obj,
421 const char **buffer,
422 int *buffer_len);
423
424 /*
425 Takes an arbitrary object which must support the (character,
426 single segment) buffer interface and returns a pointer to a
427 read-only memory location useable as character based input
428 for subsequent processing.
429
430 0 is returned on success. buffer and buffer_len are only
431 set in case no error occurrs. Otherwise, -1 is returned and
432 an exception set.
433
434 */
435
436 DL_IMPORT(int) PyObject_AsReadBuffer(PyObject *obj,
437 const void **buffer,
438 int *buffer_len);
439
440 /*
441 Same as PyObject_AsCharBuffer() except that this API expects
442 (readable, single segment) buffer interface and returns a
443 pointer to a read-only memory location which can contain
444 arbitrary data.
445
446 0 is returned on success. buffer and buffer_len are only
447 set in case no error occurrs. Otherwise, -1 is returned and
448 an exception set.
449
450 */
451
452 DL_IMPORT(int) PyObject_AsWriteBuffer(PyObject *obj,
453 void **buffer,
454 int *buffer_len);
455
456 /*
457 Takes an arbitrary object which must support the (writeable,
458 single segment) buffer interface and returns a pointer to a
459 writeable memory location in buffer of size buffer_len.
460
461 0 is returned on success. buffer and buffer_len are only
462 set in case no error occurrs. Otherwise, -1 is returned and
463 an exception set.
464
465 */
Guido van Rossuma8275371995-07-18 14:07:00 +0000466
467/* Number Protocol:*/
468
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000469 DL_IMPORT(int) PyNumber_Check(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000470
471 /*
472 Returns 1 if the object, o, provides numeric protocols, and
473 false otherwise.
474
475 This function always succeeds.
476
477 */
478
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000479 DL_IMPORT(PyObject *) PyNumber_Add(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000480
481 /*
482 Returns the result of adding o1 and o2, or null on failure.
483 This is the equivalent of the Python expression: o1+o2.
484
485
486 */
487
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000488 DL_IMPORT(PyObject *) PyNumber_Subtract(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000489
490 /*
491 Returns the result of subtracting o2 from o1, or null on
492 failure. This is the equivalent of the Python expression:
493 o1-o2.
494
495 */
496
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000497 DL_IMPORT(PyObject *) PyNumber_Multiply(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000498
499 /*
500 Returns the result of multiplying o1 and o2, or null on
501 failure. This is the equivalent of the Python expression:
502 o1*o2.
503
504
505 */
506
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000507 DL_IMPORT(PyObject *) PyNumber_Divide(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000508
509 /*
510 Returns the result of dividing o1 by o2, or null on failure.
511 This is the equivalent of the Python expression: o1/o2.
512
513
514 */
515
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000516 DL_IMPORT(PyObject *) PyNumber_Remainder(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000517
518 /*
519 Returns the remainder of dividing o1 by o2, or null on
520 failure. This is the equivalent of the Python expression:
521 o1%o2.
522
523
524 */
525
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000526 DL_IMPORT(PyObject *) PyNumber_Divmod(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000527
528 /*
529 See the built-in function divmod. Returns NULL on failure.
530 This is the equivalent of the Python expression:
531 divmod(o1,o2).
532
533
534 */
535
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000536 DL_IMPORT(PyObject *) PyNumber_Power(PyObject *o1, PyObject *o2,
537 PyObject *o3);
Guido van Rossuma8275371995-07-18 14:07:00 +0000538
539 /*
540 See the built-in function pow. Returns NULL on failure.
541 This is the equivalent of the Python expression:
542 pow(o1,o2,o3), where o3 is optional.
543
544 */
545
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000546 DL_IMPORT(PyObject *) PyNumber_Negative(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000547
548 /*
549 Returns the negation of o on success, or null on failure.
550 This is the equivalent of the Python expression: -o.
551
552 */
553
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000554 DL_IMPORT(PyObject *) PyNumber_Positive(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000555
556 /*
557 Returns the (what?) of o on success, or NULL on failure.
558 This is the equivalent of the Python expression: +o.
559
560 */
561
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000562 DL_IMPORT(PyObject *) PyNumber_Absolute(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000563
564 /*
565 Returns the absolute value of o, or null on failure. This is
566 the equivalent of the Python expression: abs(o).
567
568 */
569
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000570 DL_IMPORT(PyObject *) PyNumber_Invert(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000571
572 /*
573 Returns the bitwise negation of o on success, or NULL on
574 failure. This is the equivalent of the Python expression:
575 ~o.
576
577
578 */
579
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000580 DL_IMPORT(PyObject *) PyNumber_Lshift(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000581
582 /*
583 Returns the result of left shifting o1 by o2 on success, or
584 NULL on failure. This is the equivalent of the Python
585 expression: o1 << o2.
586
587
588 */
589
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000590 DL_IMPORT(PyObject *) PyNumber_Rshift(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000591
592 /*
593 Returns the result of right shifting o1 by o2 on success, or
594 NULL on failure. This is the equivalent of the Python
595 expression: o1 >> o2.
596
597 */
598
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000599 DL_IMPORT(PyObject *) PyNumber_And(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000600
601 /*
Guido van Rossum1ca407f1997-02-14 22:51:40 +0000602 Returns the result of bitwise and of o1 and o2 on success, or
603 NULL on failure. This is the equivalent of the Python
604 expression: o1&o2.
Guido van Rossuma8275371995-07-18 14:07:00 +0000605
606
607 */
608
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000609 DL_IMPORT(PyObject *) PyNumber_Xor(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000610
611 /*
612 Returns the bitwise exclusive or of o1 by o2 on success, or
613 NULL on failure. This is the equivalent of the Python
614 expression: o1^o2.
615
616
617 */
618
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000619 DL_IMPORT(PyObject *) PyNumber_Or(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000620
621 /*
Guido van Rossum1ca407f1997-02-14 22:51:40 +0000622 Returns the result of bitwise or or o1 and o2 on success, or
623 NULL on failure. This is the equivalent of the Python
624 expression: o1|o2.
Guido van Rossuma8275371995-07-18 14:07:00 +0000625
626 */
627
628 /* Implemented elsewhere:
629
Guido van Rossumed227f01996-09-06 13:40:53 +0000630 int PyNumber_Coerce(PyObject **p1, PyObject **p2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000631
Guido van Rossumed227f01996-09-06 13:40:53 +0000632 This function takes the addresses of two variables of type
633 PyObject*.
634
635 If the objects pointed to by *p1 and *p2 have the same type,
636 increment their reference count and return 0 (success).
637 If the objects can be converted to a common numeric type,
638 replace *p1 and *p2 by their converted value (with 'new'
639 reference counts), and return 0.
640 If no conversion is possible, or if some other error occurs,
641 return -1 (failure) and don't increment the reference counts.
642 The call PyNumber_Coerce(&o1, &o2) is equivalent to the Python
643 statement o1, o2 = coerce(o1, o2).
Guido van Rossuma8275371995-07-18 14:07:00 +0000644
645 */
646
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000647 DL_IMPORT(PyObject *) PyNumber_Int(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000648
649 /*
650 Returns the o converted to an integer object on success, or
651 NULL on failure. This is the equivalent of the Python
652 expression: int(o).
653
654 */
655
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000656 DL_IMPORT(PyObject *) PyNumber_Long(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000657
658 /*
659 Returns the o converted to a long integer object on success,
660 or NULL on failure. This is the equivalent of the Python
661 expression: long(o).
662
663 */
664
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000665 DL_IMPORT(PyObject *) PyNumber_Float(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000666
667 /*
668 Returns the o converted to a float object on success, or NULL
669 on failure. This is the equivalent of the Python expression:
670 float(o).
671 */
672
673
674/* Sequence protocol:*/
675
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000676 DL_IMPORT(int) PySequence_Check(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000677
678 /*
679 Return 1 if the object provides sequence protocol, and zero
680 otherwise.
681
682 This function always succeeds.
683
684 */
685
Jeremy Hylton6253f832000-07-12 12:56:19 +0000686 DL_IMPORT(int) PySequence_Size(PyObject *o);
687
Jeremy Hyltonfa521f12000-07-13 19:39:15 +0000688#define PySequence_Length PySequence_Size
Guido van Rossum4f4ce681996-07-21 02:22:56 +0000689
690 /*
Jeremy Hylton6253f832000-07-12 12:56:19 +0000691 Return the size of sequence object o, or -1 on failure.
Guido van Rossum4f4ce681996-07-21 02:22:56 +0000692
693 */
694
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000695 DL_IMPORT(PyObject *) PySequence_Concat(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000696
697 /*
698 Return the concatination of o1 and o2 on success, and NULL on
699 failure. This is the equivalent of the Python
700 expression: o1+o2.
701
702 */
703
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000704 DL_IMPORT(PyObject *) PySequence_Repeat(PyObject *o, int count);
Guido van Rossuma8275371995-07-18 14:07:00 +0000705
706 /*
707 Return the result of repeating sequence object o count times,
708 or NULL on failure. This is the equivalent of the Python
709 expression: o1*count.
710
711 */
712
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000713 DL_IMPORT(PyObject *) PySequence_GetItem(PyObject *o, int i);
Guido van Rossuma8275371995-07-18 14:07:00 +0000714
715 /*
716 Return the ith element of o, or NULL on failure. This is the
717 equivalent of the Python expression: o[i].
Guido van Rossuma8275371995-07-18 14:07:00 +0000718 */
719
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000720 DL_IMPORT(PyObject *) PySequence_GetSlice(PyObject *o, int i1, int i2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000721
722 /*
723 Return the slice of sequence object o between i1 and i2, or
724 NULL on failure. This is the equivalent of the Python
725 expression: o[i1:i2].
726
727 */
728
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000729 DL_IMPORT(int) PySequence_SetItem(PyObject *o, int i, PyObject *v);
Guido van Rossuma8275371995-07-18 14:07:00 +0000730
731 /*
732 Assign object v to the ith element of o. Returns
733 -1 on failure. This is the equivalent of the Python
734 statement: o[i]=v.
735
736 */
737
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000738 DL_IMPORT(int) PySequence_DelItem(PyObject *o, int i);
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000739
740 /*
741 Delete the ith element of object v. Returns
742 -1 on failure. This is the equivalent of the Python
743 statement: del o[i].
744 */
745
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000746 DL_IMPORT(int) PySequence_SetSlice(PyObject *o, int i1, int i2,
747 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
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000755 DL_IMPORT(int) PySequence_DelSlice(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
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000763 DL_IMPORT(PyObject *) PySequence_Tuple(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
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000771 DL_IMPORT(PyObject *) PySequence_List(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
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000778 DL_IMPORT(PyObject *) PySequence_Fast(PyObject *o, const char* m);
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000779
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
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000797 DL_IMPORT(int) PySequence_Count(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
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000806 DL_IMPORT(int) PySequence_Contains(PyObject *o, PyObject *value);
Guido van Rossum83684531999-03-17 18:44:39 +0000807
808/* For DLL-level backwards compatibility */
809#undef PySequence_In
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000810 DL_IMPORT(int) PySequence_In(PyObject *o, PyObject *value);
Guido van Rossum83684531999-03-17 18:44:39 +0000811
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
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000821 DL_IMPORT(int) PySequence_Index(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
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000831 DL_IMPORT(int) PyMapping_Check(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
Jeremy Hylton6253f832000-07-12 12:56:19 +0000840 DL_IMPORT(int) PyMapping_Size(PyObject *o);
841
Jeremy Hyltonfa521f12000-07-13 19:39:15 +0000842#define PyMapping_Length PyMapping_Size
Guido van Rossuma8275371995-07-18 14:07:00 +0000843
844 /*
845 Returns the number of keys in object o on success, and -1 on
846 failure. For objects that do not provide sequence protocol,
847 this is equivalent to the Python expression: len(o).
848 */
849
Guido van Rossuma25e5e91996-09-06 13:48:38 +0000850 /* implemented as a macro:
851
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000852 int PyMapping_DelItemString(PyObject *o, char *key);
Guido van Rossuma8275371995-07-18 14:07:00 +0000853
Guido van Rossuma8275371995-07-18 14:07:00 +0000854 Remove the mapping for object, key, from the object *o.
855 Returns -1 on failure. This is equivalent to
856 the Python statement: del o[key].
857 */
Guido van Rossuma25e5e91996-09-06 13:48:38 +0000858#define PyMapping_DelItemString(O,K) PyDict_DelItemString((O),(K))
859
860 /* implemented as a macro:
Guido van Rossuma8275371995-07-18 14:07:00 +0000861
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000862 int PyMapping_DelItem(PyObject *o, PyObject *key);
Guido van Rossuma8275371995-07-18 14:07:00 +0000863
Guido van Rossuma8275371995-07-18 14:07:00 +0000864 Remove the mapping for object, key, from the object *o.
865 Returns -1 on failure. This is equivalent to
866 the Python statement: del o[key].
867 */
Guido van Rossuma25e5e91996-09-06 13:48:38 +0000868#define PyMapping_DelItem(O,K) PyDict_DelItem((O),(K))
Guido van Rossuma8275371995-07-18 14:07:00 +0000869
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000870 DL_IMPORT(int) PyMapping_HasKeyString(PyObject *o, char *key);
Guido van Rossuma8275371995-07-18 14:07:00 +0000871
872 /*
873 On success, return 1 if the mapping object has the key, key,
874 and 0 otherwise. This is equivalent to the Python expression:
875 o.has_key(key).
876
877 This function always succeeds.
878 */
879
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000880 DL_IMPORT(int) PyMapping_HasKey(PyObject *o, PyObject *key);
Guido van Rossuma8275371995-07-18 14:07:00 +0000881
882 /*
883 Return 1 if the mapping object has the key, key,
884 and 0 otherwise. This is equivalent to the Python expression:
885 o.has_key(key).
886
887 This function always succeeds.
888
889 */
890
891 /* Implemented as macro:
892
893 PyObject *PyMapping_Keys(PyObject *o);
894
895 On success, return a list of the keys in object o. On
896 failure, return NULL. This is equivalent to the Python
897 expression: o.keys().
898 */
899#define PyMapping_Keys(O) PyObject_CallMethod(O,"keys",NULL)
900
901 /* Implemented as macro:
902
903 PyObject *PyMapping_Values(PyObject *o);
904
905 On success, return a list of the values in object o. On
906 failure, return NULL. This is equivalent to the Python
907 expression: o.values().
908 */
909#define PyMapping_Values(O) PyObject_CallMethod(O,"values",NULL)
910
911 /* Implemented as macro:
912
913 PyObject *PyMapping_Items(PyObject *o);
914
915 On success, return a list of the items in object o, where
916 each item is a tuple containing a key-value pair. On
917 failure, return NULL. This is equivalent to the Python
918 expression: o.items().
919
920 */
921#define PyMapping_Items(O) PyObject_CallMethod(O,"items",NULL)
922
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000923 DL_IMPORT(PyObject *) PyMapping_GetItemString(PyObject *o, char *key);
Guido van Rossuma8275371995-07-18 14:07:00 +0000924
925 /*
926 Return element of o corresponding to the object, key, or NULL
927 on failure. This is the equivalent of the Python expression:
928 o[key].
929 */
930
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000931 DL_IMPORT(int) PyMapping_SetItemString(PyObject *o, char *key,
932 PyObject *value);
Guido van Rossuma8275371995-07-18 14:07:00 +0000933
934 /*
935 Map the object, key, to the value, v. Returns
936 -1 on failure. This is the equivalent of the Python
937 statement: o[key]=v.
938 */
939
940
Guido van Rossum8ca687a1995-09-18 21:20:02 +0000941#ifdef __cplusplus
942}
943#endif
Guido van Rossuma8275371995-07-18 14:07:00 +0000944#endif /* Py_ABSTRACTOBJECT_H */