blob: 6b96adf6c1d3198acc17e015909ce705c5d1d059 [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
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000384 DL_IMPORT(int) PyObject_Length(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000385
386 /*
387 Return the length of object o. If the object, o, provides
388 both sequence and mapping protocols, the sequence length is
389 returned. On error, -1 is returned. This is the equivalent
390 to the Python expression: len(o).
391
392 */
393
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000394 DL_IMPORT(PyObject *) PyObject_GetItem(PyObject *o, PyObject *key);
Guido van Rossuma8275371995-07-18 14:07:00 +0000395
396 /*
397 Return element of o corresponding to the object, key, or NULL
398 on failure. This is the equivalent of the Python expression:
399 o[key].
400
401 */
402
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000403 DL_IMPORT(int) PyObject_SetItem(PyObject *o, PyObject *key, PyObject *v);
Guido van Rossuma8275371995-07-18 14:07:00 +0000404
405 /*
406 Map the object, key, to the value, v. Returns
407 -1 on failure. This is the equivalent of the Python
408 statement: o[key]=v.
409 */
410
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000411 DL_IMPORT(int) PyObject_DelItem(PyObject *o, PyObject *key);
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000412
413 /*
414 Delete the mapping for key from *o. Returns -1 on failure.
415 This is the equivalent of the Python statement: del o[key].
416 */
417
Guido van Rossumfd9eed32000-03-10 22:35:06 +0000418 DL_IMPORT(int) PyObject_AsCharBuffer(PyObject *obj,
419 const char **buffer,
420 int *buffer_len);
421
422 /*
423 Takes an arbitrary object which must support the (character,
424 single segment) buffer interface and returns a pointer to a
425 read-only memory location useable as character based input
426 for subsequent processing.
427
428 0 is returned on success. buffer and buffer_len are only
429 set in case no error occurrs. Otherwise, -1 is returned and
430 an exception set.
431
432 */
433
434 DL_IMPORT(int) PyObject_AsReadBuffer(PyObject *obj,
435 const void **buffer,
436 int *buffer_len);
437
438 /*
439 Same as PyObject_AsCharBuffer() except that this API expects
440 (readable, single segment) buffer interface and returns a
441 pointer to a read-only memory location which can contain
442 arbitrary data.
443
444 0 is returned on success. buffer and buffer_len are only
445 set in case no error occurrs. Otherwise, -1 is returned and
446 an exception set.
447
448 */
449
450 DL_IMPORT(int) PyObject_AsWriteBuffer(PyObject *obj,
451 void **buffer,
452 int *buffer_len);
453
454 /*
455 Takes an arbitrary object which must support the (writeable,
456 single segment) buffer interface and returns a pointer to a
457 writeable memory location in buffer of size buffer_len.
458
459 0 is returned on success. buffer and buffer_len are only
460 set in case no error occurrs. Otherwise, -1 is returned and
461 an exception set.
462
463 */
Guido van Rossuma8275371995-07-18 14:07:00 +0000464
465/* Number Protocol:*/
466
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000467 DL_IMPORT(int) PyNumber_Check(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000468
469 /*
470 Returns 1 if the object, o, provides numeric protocols, and
471 false otherwise.
472
473 This function always succeeds.
474
475 */
476
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000477 DL_IMPORT(PyObject *) PyNumber_Add(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000478
479 /*
480 Returns the result of adding o1 and o2, or null on failure.
481 This is the equivalent of the Python expression: o1+o2.
482
483
484 */
485
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000486 DL_IMPORT(PyObject *) PyNumber_Subtract(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000487
488 /*
489 Returns the result of subtracting o2 from o1, or null on
490 failure. This is the equivalent of the Python expression:
491 o1-o2.
492
493 */
494
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000495 DL_IMPORT(PyObject *) PyNumber_Multiply(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000496
497 /*
498 Returns the result of multiplying o1 and o2, or null on
499 failure. This is the equivalent of the Python expression:
500 o1*o2.
501
502
503 */
504
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000505 DL_IMPORT(PyObject *) PyNumber_Divide(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000506
507 /*
508 Returns the result of dividing o1 by o2, or null on failure.
509 This is the equivalent of the Python expression: o1/o2.
510
511
512 */
513
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000514 DL_IMPORT(PyObject *) PyNumber_Remainder(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000515
516 /*
517 Returns the remainder of dividing o1 by o2, or null on
518 failure. This is the equivalent of the Python expression:
519 o1%o2.
520
521
522 */
523
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000524 DL_IMPORT(PyObject *) PyNumber_Divmod(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000525
526 /*
527 See the built-in function divmod. Returns NULL on failure.
528 This is the equivalent of the Python expression:
529 divmod(o1,o2).
530
531
532 */
533
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000534 DL_IMPORT(PyObject *) PyNumber_Power(PyObject *o1, PyObject *o2,
535 PyObject *o3);
Guido van Rossuma8275371995-07-18 14:07:00 +0000536
537 /*
538 See the built-in function pow. Returns NULL on failure.
539 This is the equivalent of the Python expression:
540 pow(o1,o2,o3), where o3 is optional.
541
542 */
543
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000544 DL_IMPORT(PyObject *) PyNumber_Negative(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000545
546 /*
547 Returns the negation of o on success, or null on failure.
548 This is the equivalent of the Python expression: -o.
549
550 */
551
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000552 DL_IMPORT(PyObject *) PyNumber_Positive(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000553
554 /*
555 Returns the (what?) of o on success, or NULL on failure.
556 This is the equivalent of the Python expression: +o.
557
558 */
559
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000560 DL_IMPORT(PyObject *) PyNumber_Absolute(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000561
562 /*
563 Returns the absolute value of o, or null on failure. This is
564 the equivalent of the Python expression: abs(o).
565
566 */
567
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000568 DL_IMPORT(PyObject *) PyNumber_Invert(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000569
570 /*
571 Returns the bitwise negation of o on success, or NULL on
572 failure. This is the equivalent of the Python expression:
573 ~o.
574
575
576 */
577
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000578 DL_IMPORT(PyObject *) PyNumber_Lshift(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000579
580 /*
581 Returns the result of left shifting o1 by o2 on success, or
582 NULL on failure. This is the equivalent of the Python
583 expression: o1 << o2.
584
585
586 */
587
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000588 DL_IMPORT(PyObject *) PyNumber_Rshift(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000589
590 /*
591 Returns the result of right shifting o1 by o2 on success, or
592 NULL on failure. This is the equivalent of the Python
593 expression: o1 >> o2.
594
595 */
596
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000597 DL_IMPORT(PyObject *) PyNumber_And(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000598
599 /*
Guido van Rossum1ca407f1997-02-14 22:51:40 +0000600 Returns the result of bitwise and of o1 and o2 on success, or
601 NULL on failure. This is the equivalent of the Python
602 expression: o1&o2.
Guido van Rossuma8275371995-07-18 14:07:00 +0000603
604
605 */
606
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000607 DL_IMPORT(PyObject *) PyNumber_Xor(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000608
609 /*
610 Returns the bitwise exclusive or of o1 by o2 on success, or
611 NULL on failure. This is the equivalent of the Python
612 expression: o1^o2.
613
614
615 */
616
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000617 DL_IMPORT(PyObject *) PyNumber_Or(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000618
619 /*
Guido van Rossum1ca407f1997-02-14 22:51:40 +0000620 Returns the result of bitwise or or o1 and o2 on success, or
621 NULL on failure. This is the equivalent of the Python
622 expression: o1|o2.
Guido van Rossuma8275371995-07-18 14:07:00 +0000623
624 */
625
626 /* Implemented elsewhere:
627
Guido van Rossumed227f01996-09-06 13:40:53 +0000628 int PyNumber_Coerce(PyObject **p1, PyObject **p2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000629
Guido van Rossumed227f01996-09-06 13:40:53 +0000630 This function takes the addresses of two variables of type
631 PyObject*.
632
633 If the objects pointed to by *p1 and *p2 have the same type,
634 increment their reference count and return 0 (success).
635 If the objects can be converted to a common numeric type,
636 replace *p1 and *p2 by their converted value (with 'new'
637 reference counts), and return 0.
638 If no conversion is possible, or if some other error occurs,
639 return -1 (failure) and don't increment the reference counts.
640 The call PyNumber_Coerce(&o1, &o2) is equivalent to the Python
641 statement o1, o2 = coerce(o1, o2).
Guido van Rossuma8275371995-07-18 14:07:00 +0000642
643 */
644
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000645 DL_IMPORT(PyObject *) PyNumber_Int(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000646
647 /*
648 Returns the o converted to an integer object on success, or
649 NULL on failure. This is the equivalent of the Python
650 expression: int(o).
651
652 */
653
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000654 DL_IMPORT(PyObject *) PyNumber_Long(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000655
656 /*
657 Returns the o converted to a long integer object on success,
658 or NULL on failure. This is the equivalent of the Python
659 expression: long(o).
660
661 */
662
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000663 DL_IMPORT(PyObject *) PyNumber_Float(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000664
665 /*
666 Returns the o converted to a float object on success, or NULL
667 on failure. This is the equivalent of the Python expression:
668 float(o).
669 */
670
671
672/* Sequence protocol:*/
673
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000674 DL_IMPORT(int) PySequence_Check(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000675
676 /*
677 Return 1 if the object provides sequence protocol, and zero
678 otherwise.
679
680 This function always succeeds.
681
682 */
683
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000684 DL_IMPORT(int) PySequence_Length(PyObject *o);
Guido van Rossum4f4ce681996-07-21 02:22:56 +0000685
686 /*
687 Return the length of sequence object o, or -1 on failure.
688
689 */
690
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000691 DL_IMPORT(PyObject *) PySequence_Concat(PyObject *o1, PyObject *o2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000692
693 /*
694 Return the concatination of o1 and o2 on success, and NULL on
695 failure. This is the equivalent of the Python
696 expression: o1+o2.
697
698 */
699
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000700 DL_IMPORT(PyObject *) PySequence_Repeat(PyObject *o, int count);
Guido van Rossuma8275371995-07-18 14:07:00 +0000701
702 /*
703 Return the result of repeating sequence object o count times,
704 or NULL on failure. This is the equivalent of the Python
705 expression: o1*count.
706
707 */
708
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000709 DL_IMPORT(PyObject *) PySequence_GetItem(PyObject *o, int i);
Guido van Rossuma8275371995-07-18 14:07:00 +0000710
711 /*
712 Return the ith element of o, or NULL on failure. This is the
713 equivalent of the Python expression: o[i].
Guido van Rossuma8275371995-07-18 14:07:00 +0000714 */
715
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000716 DL_IMPORT(PyObject *) PySequence_GetSlice(PyObject *o, int i1, int i2);
Guido van Rossuma8275371995-07-18 14:07:00 +0000717
718 /*
719 Return the slice of sequence object o between i1 and i2, or
720 NULL on failure. This is the equivalent of the Python
721 expression: o[i1:i2].
722
723 */
724
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000725 DL_IMPORT(int) PySequence_SetItem(PyObject *o, int i, PyObject *v);
Guido van Rossuma8275371995-07-18 14:07:00 +0000726
727 /*
728 Assign object v to the ith element of o. Returns
729 -1 on failure. This is the equivalent of the Python
730 statement: o[i]=v.
731
732 */
733
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000734 DL_IMPORT(int) PySequence_DelItem(PyObject *o, int i);
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000735
736 /*
737 Delete the ith element of object v. Returns
738 -1 on failure. This is the equivalent of the Python
739 statement: del o[i].
740 */
741
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000742 DL_IMPORT(int) PySequence_SetSlice(PyObject *o, int i1, int i2,
743 PyObject *v);
Guido van Rossuma8275371995-07-18 14:07:00 +0000744
745 /*
746 Assign the sequence object, v, to the slice in sequence
747 object, o, from i1 to i2. Returns -1 on failure. This is the
748 equivalent of the Python statement: o[i1:i2]=v.
749 */
750
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000751 DL_IMPORT(int) PySequence_DelSlice(PyObject *o, int i1, int i2);
Guido van Rossum6cdc6f41996-08-21 17:41:54 +0000752
753 /*
754 Delete the slice in sequence object, o, from i1 to i2.
755 Returns -1 on failure. This is the equivalent of the Python
756 statement: del o[i1:i2].
757 */
758
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000759 DL_IMPORT(PyObject *) PySequence_Tuple(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000760
761 /*
Guido van Rossumf39fc431997-03-04 18:31:47 +0000762 Returns the sequence, o, as a tuple on success, and NULL on failure.
Guido van Rossuma8275371995-07-18 14:07:00 +0000763 This is equivalent to the Python expression: tuple(o)
764 */
765
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000766
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000767 DL_IMPORT(PyObject *) PySequence_List(PyObject *o);
Guido van Rossumf39fc431997-03-04 18:31:47 +0000768
Guido van Rossum2adf06b1996-12-05 21:48:50 +0000769 /*
Guido van Rossumf39fc431997-03-04 18:31:47 +0000770 Returns the sequence, o, as a list on success, and NULL on failure.
771 This is equivalent to the Python expression: list(o)
Guido van Rossum2adf06b1996-12-05 21:48:50 +0000772 */
Guido van Rossumf39fc431997-03-04 18:31:47 +0000773
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000774 DL_IMPORT(PyObject *) PySequence_Fast(PyObject *o, const char* m);
Andrew M. Kuchling74042d62000-06-18 18:43:14 +0000775
776 /*
777 Returns the sequence, o, as a tuple, unless it's already a
778 tuple or list. Use PySequence_Fast_GET_ITEM to access the
779 members of this list.
780
781 Returns NULL on failure. If the object is not a sequence,
782 raises a TypeError exception with m as the message text.
783 */
784
785#define PySequence_Fast_GET_ITEM(o, i)\
786 (PyList_Check(o) ? PyList_GET_ITEM(o, i) : PyTuple_GET_ITEM(o, i))
787
788 /*
789 Return the ith element of o, assuming that o was returned by
790 PySequence_Fast, and that i is within bounds.
791 */
792
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000793 DL_IMPORT(int) PySequence_Count(PyObject *o, PyObject *value);
Guido van Rossuma8275371995-07-18 14:07:00 +0000794
795 /*
796 Return the number of occurrences on value on o, that is,
797 return the number of keys for which o[key]==value. On
798 failure, return -1. This is equivalent to the Python
799 expression: o.count(value).
800 */
801
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000802 DL_IMPORT(int) PySequence_Contains(PyObject *o, PyObject *value);
Guido van Rossum83684531999-03-17 18:44:39 +0000803
804/* For DLL-level backwards compatibility */
805#undef PySequence_In
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000806 DL_IMPORT(int) PySequence_In(PyObject *o, PyObject *value);
Guido van Rossum83684531999-03-17 18:44:39 +0000807
808/* For source-level backwards compatibility */
Guido van Rossumf1536db1998-08-23 22:06:59 +0000809#define PySequence_In PySequence_Contains
Guido van Rossuma8275371995-07-18 14:07:00 +0000810
811 /*
812 Determine if o contains value. If an item in o is equal to
813 X, return 1, otherwise return 0. On error, return -1. This
814 is equivalent to the Python expression: value in o.
815 */
816
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000817 DL_IMPORT(int) PySequence_Index(PyObject *o, PyObject *value);
Guido van Rossuma8275371995-07-18 14:07:00 +0000818
819 /*
820 Return the first index for which o[i]=value. On error,
821 return -1. This is equivalent to the Python
822 expression: o.index(value).
823 */
824
825/* Mapping protocol:*/
826
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000827 DL_IMPORT(int) PyMapping_Check(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000828
829 /*
830 Return 1 if the object provides mapping protocol, and zero
831 otherwise.
832
833 This function always succeeds.
834 */
835
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000836 DL_IMPORT(int) PyMapping_Length(PyObject *o);
Guido van Rossuma8275371995-07-18 14:07:00 +0000837
838 /*
839 Returns the number of keys in object o on success, and -1 on
840 failure. For objects that do not provide sequence protocol,
841 this is equivalent to the Python expression: len(o).
842 */
843
Guido van Rossuma25e5e91996-09-06 13:48:38 +0000844 /* implemented as a macro:
845
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000846 int PyMapping_DelItemString(PyObject *o, char *key);
Guido van Rossuma8275371995-07-18 14:07:00 +0000847
Guido van Rossuma8275371995-07-18 14:07:00 +0000848 Remove the mapping for object, key, from the object *o.
849 Returns -1 on failure. This is equivalent to
850 the Python statement: del o[key].
851 */
Guido van Rossuma25e5e91996-09-06 13:48:38 +0000852#define PyMapping_DelItemString(O,K) PyDict_DelItemString((O),(K))
853
854 /* implemented as a macro:
Guido van Rossuma8275371995-07-18 14:07:00 +0000855
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000856 int PyMapping_DelItem(PyObject *o, PyObject *key);
Guido van Rossuma8275371995-07-18 14:07:00 +0000857
Guido van Rossuma8275371995-07-18 14:07:00 +0000858 Remove the mapping for object, key, from the object *o.
859 Returns -1 on failure. This is equivalent to
860 the Python statement: del o[key].
861 */
Guido van Rossuma25e5e91996-09-06 13:48:38 +0000862#define PyMapping_DelItem(O,K) PyDict_DelItem((O),(K))
Guido van Rossuma8275371995-07-18 14:07:00 +0000863
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000864 DL_IMPORT(int) PyMapping_HasKeyString(PyObject *o, char *key);
Guido van Rossuma8275371995-07-18 14:07:00 +0000865
866 /*
867 On success, return 1 if the mapping object has the key, key,
868 and 0 otherwise. This is equivalent to the Python expression:
869 o.has_key(key).
870
871 This function always succeeds.
872 */
873
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000874 DL_IMPORT(int) PyMapping_HasKey(PyObject *o, PyObject *key);
Guido van Rossuma8275371995-07-18 14:07:00 +0000875
876 /*
877 Return 1 if the mapping object has the key, key,
878 and 0 otherwise. This is equivalent to the Python expression:
879 o.has_key(key).
880
881 This function always succeeds.
882
883 */
884
885 /* Implemented as macro:
886
887 PyObject *PyMapping_Keys(PyObject *o);
888
889 On success, return a list of the keys in object o. On
890 failure, return NULL. This is equivalent to the Python
891 expression: o.keys().
892 */
893#define PyMapping_Keys(O) PyObject_CallMethod(O,"keys",NULL)
894
895 /* Implemented as macro:
896
897 PyObject *PyMapping_Values(PyObject *o);
898
899 On success, return a list of the values in object o. On
900 failure, return NULL. This is equivalent to the Python
901 expression: o.values().
902 */
903#define PyMapping_Values(O) PyObject_CallMethod(O,"values",NULL)
904
905 /* Implemented as macro:
906
907 PyObject *PyMapping_Items(PyObject *o);
908
909 On success, return a list of the items in object o, where
910 each item is a tuple containing a key-value pair. On
911 failure, return NULL. This is equivalent to the Python
912 expression: o.items().
913
914 */
915#define PyMapping_Items(O) PyObject_CallMethod(O,"items",NULL)
916
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000917 DL_IMPORT(PyObject *) PyMapping_GetItemString(PyObject *o, char *key);
Guido van Rossuma8275371995-07-18 14:07:00 +0000918
919 /*
920 Return element of o corresponding to the object, key, or NULL
921 on failure. This is the equivalent of the Python expression:
922 o[key].
923 */
924
Fred Drakeea9cb5a2000-07-09 00:20:36 +0000925 DL_IMPORT(int) PyMapping_SetItemString(PyObject *o, char *key,
926 PyObject *value);
Guido van Rossuma8275371995-07-18 14:07:00 +0000927
928 /*
929 Map the object, key, to the value, v. Returns
930 -1 on failure. This is the equivalent of the Python
931 statement: o[key]=v.
932 */
933
934
Guido van Rossum8ca687a1995-09-18 21:20:02 +0000935#ifdef __cplusplus
936}
937#endif
Guido van Rossuma8275371995-07-18 14:07:00 +0000938#endif /* Py_ABSTRACTOBJECT_H */