blob: 7a6b7cff1dc7da08b87a0a03e320d5e4ad1eb901 [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
Guido van Rossuma8275371995-07-18 14:07:00 +0000386 /*
Jeremy Hylton6253f832000-07-12 12:56:19 +0000387 Return the size of object o. If the object, o, provides
388 both sequence and mapping protocols, the sequence size is
Guido van Rossuma8275371995-07-18 14:07:00 +0000389 returned. On error, -1 is returned. This is the equivalent
390 to the Python expression: len(o).
391
392 */
393
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +0000394 /* For DLL compatibility */
395#undef PyObject_Length
396 DL_IMPORT(int) PyObject_Length(PyObject *o);
397#define PyObject_Length PyObject_Size
398
399
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000400 DL_IMPORT(PyObject *) PyObject_GetItem(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
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000409 DL_IMPORT(int) PyObject_SetItem(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
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000417 DL_IMPORT(int) PyObject_DelItem(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
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000473 DL_IMPORT(int) PyNumber_Check(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
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000483 DL_IMPORT(PyObject *) PyNumber_Add(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
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000492 DL_IMPORT(PyObject *) PyNumber_Subtract(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
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000501 DL_IMPORT(PyObject *) PyNumber_Multiply(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
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000511 DL_IMPORT(PyObject *) PyNumber_Divide(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
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000520 DL_IMPORT(PyObject *) PyNumber_Remainder(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
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000530 DL_IMPORT(PyObject *) PyNumber_Divmod(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
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000540 DL_IMPORT(PyObject *) PyNumber_Power(PyObject *o1, PyObject *o2,
541 PyObject *o3);
Guido van Rossuma8275371995-07-18 14:07:00 +0000542
543 /*
544 See the built-in function pow. Returns NULL on failure.
545 This is the equivalent of the Python expression:
546 pow(o1,o2,o3), where o3 is optional.
547
548 */
549
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000550 DL_IMPORT(PyObject *) PyNumber_Negative(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000551
552 /*
553 Returns the negation of o on success, or null on failure.
554 This is the equivalent of the Python expression: -o.
555
556 */
557
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000558 DL_IMPORT(PyObject *) PyNumber_Positive(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000559
560 /*
561 Returns the (what?) of o on success, or NULL on failure.
562 This is the equivalent of the Python expression: +o.
563
564 */
565
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000566 DL_IMPORT(PyObject *) PyNumber_Absolute(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000567
568 /*
569 Returns the absolute value of o, or null on failure. This is
570 the equivalent of the Python expression: abs(o).
571
572 */
573
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000574 DL_IMPORT(PyObject *) PyNumber_Invert(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000575
576 /*
577 Returns the bitwise negation of o on success, or NULL on
578 failure. This is the equivalent of the Python expression:
579 ~o.
580
581
582 */
583
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000584 DL_IMPORT(PyObject *) PyNumber_Lshift(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000585
586 /*
587 Returns the result of left shifting o1 by o2 on success, or
588 NULL on failure. This is the equivalent of the Python
589 expression: o1 << o2.
590
591
592 */
593
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000594 DL_IMPORT(PyObject *) PyNumber_Rshift(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000595
596 /*
597 Returns the result of right shifting o1 by o2 on success, or
598 NULL on failure. This is the equivalent of the Python
599 expression: o1 >> o2.
600
601 */
602
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000603 DL_IMPORT(PyObject *) PyNumber_And(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000604
605 /*
Guido van Rossum1ca407f1997-02-14 22:51:40 +0000606 Returns the result of bitwise and of o1 and o2 on success, or
607 NULL on failure. This is the equivalent of the Python
608 expression: o1&o2.
Guido van Rossuma8275371995-07-18 14:07:00 +0000609
610
611 */
612
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000613 DL_IMPORT(PyObject *) PyNumber_Xor(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000614
615 /*
616 Returns the bitwise exclusive or of o1 by o2 on success, or
617 NULL on failure. This is the equivalent of the Python
618 expression: o1^o2.
619
620
621 */
622
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000623 DL_IMPORT(PyObject *) PyNumber_Or(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000624
625 /*
Guido van Rossum1ca407f1997-02-14 22:51:40 +0000626 Returns the result of bitwise or or o1 and o2 on success, or
627 NULL on failure. This is the equivalent of the Python
628 expression: o1|o2.
Guido van Rossuma8275371995-07-18 14:07:00 +0000629
630 */
631
632 /* Implemented elsewhere:
633
Guido van Rossumed227f01996-09-06 13:40:53 +0000634 int PyNumber_Coerce(PyObject **p1, PyObject **p2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000635
Guido van Rossumed227f01996-09-06 13:40:53 +0000636 This function takes the addresses of two variables of type
637 PyObject*.
638
639 If the objects pointed to by *p1 and *p2 have the same type,
640 increment their reference count and return 0 (success).
641 If the objects can be converted to a common numeric type,
642 replace *p1 and *p2 by their converted value (with 'new'
643 reference counts), and return 0.
644 If no conversion is possible, or if some other error occurs,
645 return -1 (failure) and don't increment the reference counts.
646 The call PyNumber_Coerce(&o1, &o2) is equivalent to the Python
647 statement o1, o2 = coerce(o1, o2).
Guido van Rossuma8275371995-07-18 14:07:00 +0000648
649 */
650
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000651 DL_IMPORT(PyObject *) PyNumber_Int(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000652
653 /*
654 Returns the o converted to an integer object on success, or
655 NULL on failure. This is the equivalent of the Python
656 expression: int(o).
657
658 */
659
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000660 DL_IMPORT(PyObject *) PyNumber_Long(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000661
662 /*
663 Returns the o converted to a long integer object on success,
664 or NULL on failure. This is the equivalent of the Python
665 expression: long(o).
666
667 */
668
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000669 DL_IMPORT(PyObject *) PyNumber_Float(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000670
671 /*
672 Returns the o converted to a float object on success, or NULL
673 on failure. This is the equivalent of the Python expression:
674 float(o).
675 */
676
677
678/* Sequence protocol:*/
679
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000680 DL_IMPORT(int) PySequence_Check(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000681
682 /*
683 Return 1 if the object provides sequence protocol, and zero
684 otherwise.
685
686 This function always succeeds.
687
688 */
689
Jeremy Hylton6253f832000-07-12 12:56:19 +0000690 DL_IMPORT(int) PySequence_Size(PyObject *o);
691
Guido van Rossum4f4ce681996-07-21 02:22:56 +0000692 /*
Jeremy Hylton6253f832000-07-12 12:56:19 +0000693 Return the size of sequence object o, or -1 on failure.
Guido van Rossum4f4ce681996-07-21 02:22:56 +0000694
695 */
696
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +0000697 /* For DLL compatibility */
698#undef PySequence_Length
699 DL_IMPORT(int) PySequence_Length(PyObject *o);
700#define PySequence_Length PySequence_Size
701
702
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000703 DL_IMPORT(PyObject *) PySequence_Concat(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000704
705 /*
Thomas Wouters7e474022000-07-16 12:04:32 +0000706 Return the concatenation of o1 and o2 on success, and NULL on
Guido van Rossuma8275371995-07-18 14:07:00 +0000707 failure. This is the equivalent of the Python
708 expression: o1+o2.
709
710 */
711
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000712 DL_IMPORT(PyObject *) PySequence_Repeat(PyObject *o, int count);
Guido van Rossuma8275371995-07-18 14:07:00 +0000713
714 /*
715 Return the result of repeating sequence object o count times,
716 or NULL on failure. This is the equivalent of the Python
717 expression: o1*count.
718
719 */
720
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000721 DL_IMPORT(PyObject *) PySequence_GetItem(PyObject *o, int i);
Guido van Rossuma8275371995-07-18 14:07:00 +0000722
723 /*
724 Return the ith element of o, or NULL on failure. This is the
725 equivalent of the Python expression: o[i].
Guido van Rossuma8275371995-07-18 14:07:00 +0000726 */
727
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000728 DL_IMPORT(PyObject *) PySequence_GetSlice(PyObject *o, int i1, int i2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000729
730 /*
731 Return the slice of sequence object o between i1 and i2, or
732 NULL on failure. This is the equivalent of the Python
733 expression: o[i1:i2].
734
735 */
736
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000737 DL_IMPORT(int) PySequence_SetItem(PyObject *o, int i, PyObject *v);
Guido van Rossuma8275371995-07-18 14:07:00 +0000738
739 /*
740 Assign object v to the ith element of o. Returns
741 -1 on failure. This is the equivalent of the Python
742 statement: o[i]=v.
743
744 */
745
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000746 DL_IMPORT(int) PySequence_DelItem(PyObject *o, int i);
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000747
748 /*
749 Delete the ith element of object v. Returns
750 -1 on failure. This is the equivalent of the Python
751 statement: del o[i].
752 */
753
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000754 DL_IMPORT(int) PySequence_SetSlice(PyObject *o, int i1, int i2,
755 PyObject *v);
Guido van Rossuma8275371995-07-18 14:07:00 +0000756
757 /*
758 Assign the sequence object, v, to the slice in sequence
759 object, o, from i1 to i2. Returns -1 on failure. This is the
760 equivalent of the Python statement: o[i1:i2]=v.
761 */
762
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000763 DL_IMPORT(int) PySequence_DelSlice(PyObject *o, int i1, int i2);
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000764
765 /*
766 Delete the slice in sequence object, o, from i1 to i2.
767 Returns -1 on failure. This is the equivalent of the Python
768 statement: del o[i1:i2].
769 */
770
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000771 DL_IMPORT(PyObject *) PySequence_Tuple(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000772
773 /*
Guido van Rossumf39fc431997-03-04 18:31:47 +0000774 Returns the sequence, o, as a tuple on success, and NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000775 This is equivalent to the Python expression: tuple(o)
776 */
777
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000778
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000779 DL_IMPORT(PyObject *) PySequence_List(PyObject *o);
Guido van Rossumf39fc431997-03-04 18:31:47 +0000780
Guido van Rossum2adf06b1996-12-05 21:48:50 +0000781 /*
Guido van Rossumf39fc431997-03-04 18:31:47 +0000782 Returns the sequence, o, as a list on success, and NULL on failure.
783 This is equivalent to the Python expression: list(o)
Guido van Rossum2adf06b1996-12-05 21:48:50 +0000784 */
Guido van Rossumf39fc431997-03-04 18:31:47 +0000785
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000786 DL_IMPORT(PyObject *) PySequence_Fast(PyObject *o, const char* m);
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000787
788 /*
789 Returns the sequence, o, as a tuple, unless it's already a
790 tuple or list. Use PySequence_Fast_GET_ITEM to access the
791 members of this list.
792
793 Returns NULL on failure. If the object is not a sequence,
794 raises a TypeError exception with m as the message text.
795 */
796
797#define PySequence_Fast_GET_ITEM(o, i)\
798 (PyList_Check(o) ? PyList_GET_ITEM(o, i) : PyTuple_GET_ITEM(o, i))
799
800 /*
801 Return the ith element of o, assuming that o was returned by
802 PySequence_Fast, and that i is within bounds.
803 */
804
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000805 DL_IMPORT(int) PySequence_Count(PyObject *o, PyObject *value);
Guido van Rossuma8275371995-07-18 14:07:00 +0000806
807 /*
808 Return the number of occurrences on value on o, that is,
809 return the number of keys for which o[key]==value. On
810 failure, return -1. This is equivalent to the Python
811 expression: o.count(value).
812 */
813
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000814 DL_IMPORT(int) PySequence_Contains(PyObject *o, PyObject *value);
Guido van Rossum83684531999-03-17 18:44:39 +0000815
816/* For DLL-level backwards compatibility */
817#undef PySequence_In
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000818 DL_IMPORT(int) PySequence_In(PyObject *o, PyObject *value);
Guido van Rossum83684531999-03-17 18:44:39 +0000819
820/* For source-level backwards compatibility */
Guido van Rossumf1536db1998-08-23 22:06:59 +0000821#define PySequence_In PySequence_Contains
Guido van Rossuma8275371995-07-18 14:07:00 +0000822
823 /*
824 Determine if o contains value. If an item in o is equal to
825 X, return 1, otherwise return 0. On error, return -1. This
826 is equivalent to the Python expression: value in o.
827 */
828
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000829 DL_IMPORT(int) PySequence_Index(PyObject *o, PyObject *value);
Guido van Rossuma8275371995-07-18 14:07:00 +0000830
831 /*
832 Return the first index for which o[i]=value. On error,
833 return -1. This is equivalent to the Python
834 expression: o.index(value).
835 */
836
837/* Mapping protocol:*/
838
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000839 DL_IMPORT(int) PyMapping_Check(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000840
841 /*
842 Return 1 if the object provides mapping protocol, and zero
843 otherwise.
844
845 This function always succeeds.
846 */
847
Jeremy Hylton6253f832000-07-12 12:56:19 +0000848 DL_IMPORT(int) PyMapping_Size(PyObject *o);
849
Guido van Rossuma8275371995-07-18 14:07:00 +0000850 /*
851 Returns the number of keys in object o on success, and -1 on
852 failure. For objects that do not provide sequence protocol,
853 this is equivalent to the Python expression: len(o).
854 */
855
Marc-André Lemburgcf5f3582000-07-17 09:22:55 +0000856 /* For DLL compatibility */
857#undef PyMapping_Length
858 DL_IMPORT(int) PyMapping_Length(PyObject *o);
859#define PyMapping_Length PyMapping_Size
860
861
Guido van Rossuma25e5e91996-09-06 13:48:38 +0000862 /* implemented as a macro:
863
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000864 int PyMapping_DelItemString(PyObject *o, char *key);
Guido van Rossuma8275371995-07-18 14:07:00 +0000865
Guido van Rossuma8275371995-07-18 14:07:00 +0000866 Remove the mapping for object, key, from the object *o.
867 Returns -1 on failure. This is equivalent to
868 the Python statement: del o[key].
869 */
Guido van Rossuma25e5e91996-09-06 13:48:38 +0000870#define PyMapping_DelItemString(O,K) PyDict_DelItemString((O),(K))
871
872 /* implemented as a macro:
Guido van Rossuma8275371995-07-18 14:07:00 +0000873
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000874 int PyMapping_DelItem(PyObject *o, PyObject *key);
Guido van Rossuma8275371995-07-18 14:07:00 +0000875
Guido van Rossuma8275371995-07-18 14:07:00 +0000876 Remove the mapping for object, key, from the object *o.
877 Returns -1 on failure. This is equivalent to
878 the Python statement: del o[key].
879 */
Guido van Rossuma25e5e91996-09-06 13:48:38 +0000880#define PyMapping_DelItem(O,K) PyDict_DelItem((O),(K))
Guido van Rossuma8275371995-07-18 14:07:00 +0000881
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000882 DL_IMPORT(int) PyMapping_HasKeyString(PyObject *o, char *key);
Guido van Rossuma8275371995-07-18 14:07:00 +0000883
884 /*
885 On success, return 1 if the mapping object has the key, key,
886 and 0 otherwise. This is equivalent to the Python expression:
887 o.has_key(key).
888
889 This function always succeeds.
890 */
891
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000892 DL_IMPORT(int) PyMapping_HasKey(PyObject *o, PyObject *key);
Guido van Rossuma8275371995-07-18 14:07:00 +0000893
894 /*
895 Return 1 if the mapping object has the key, key,
896 and 0 otherwise. This is equivalent to the Python expression:
897 o.has_key(key).
898
899 This function always succeeds.
900
901 */
902
903 /* Implemented as macro:
904
905 PyObject *PyMapping_Keys(PyObject *o);
906
907 On success, return a list of the keys in object o. On
908 failure, return NULL. This is equivalent to the Python
909 expression: o.keys().
910 */
911#define PyMapping_Keys(O) PyObject_CallMethod(O,"keys",NULL)
912
913 /* Implemented as macro:
914
915 PyObject *PyMapping_Values(PyObject *o);
916
917 On success, return a list of the values in object o. On
918 failure, return NULL. This is equivalent to the Python
919 expression: o.values().
920 */
921#define PyMapping_Values(O) PyObject_CallMethod(O,"values",NULL)
922
923 /* Implemented as macro:
924
925 PyObject *PyMapping_Items(PyObject *o);
926
927 On success, return a list of the items in object o, where
928 each item is a tuple containing a key-value pair. On
929 failure, return NULL. This is equivalent to the Python
930 expression: o.items().
931
932 */
933#define PyMapping_Items(O) PyObject_CallMethod(O,"items",NULL)
934
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000935 DL_IMPORT(PyObject *) PyMapping_GetItemString(PyObject *o, char *key);
Guido van Rossuma8275371995-07-18 14:07:00 +0000936
937 /*
938 Return element of o corresponding to the object, key, or NULL
939 on failure. This is the equivalent of the Python expression:
940 o[key].
941 */
942
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000943 DL_IMPORT(int) PyMapping_SetItemString(PyObject *o, char *key,
944 PyObject *value);
Guido van Rossuma8275371995-07-18 14:07:00 +0000945
946 /*
947 Map the object, key, to the value, v. Returns
948 -1 on failure. This is the equivalent of the Python
949 statement: o[key]=v.
950 */
951
952
Guido van Rossum8ca687a1995-09-18 21:20:02 +0000953#ifdef __cplusplus
954}
955#endif
Guido van Rossuma8275371995-07-18 14:07:00 +0000956#endif /* Py_ABSTRACTOBJECT_H */