blob: 8d8900b77b674b0e1c9bff4fd14ee86d4c3af740 [file] [log] [blame]
Guido van Rossuma8275371995-07-18 14:07:00 +00001#ifndef Py_ABSTRACTOBJECT_H
2#define Py_ABSTRACTOBJECT_H
3#ifdef __cplusplus
4extern "C" {
5#endif
6
7/***********************************************************
8Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
9The Netherlands.
10
11 All Rights Reserved
12
13Permission to use, copy, modify, and distribute this software and its
14documentation for any purpose and without fee is hereby granted,
15provided that the above copyright notice appear in all copies and that
16both that copyright notice and this permission notice appear in
17supporting documentation, and that the names of Stichting Mathematisch
18Centrum or CWI not be used in advertising or publicity pertaining to
19distribution of the software without specific, written prior permission.
20
21STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
22THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
23FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
24FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
25WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
26ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
27OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
28
29******************************************************************/
30
31/* Abstract Object Interface (many thanks to Jim Fulton) */
32
33/*
34 PROPOSAL: A Generic Python Object Interface for Python C Modules
35
36Problem
37
38 Python modules written in C that must access Python objects must do
39 so through routines whose interfaces are described by a set of
40 include files. Unfortunately, these routines vary according to the
41 object accessed. To use these routines, the C programmer must check
42 the type of the object being used and must call a routine based on
43 the object type. For example, to access an element of a sequence,
44 the programmer must determine whether the sequence is a list or a
45 tuple:
46
47 if(is_tupleobject(o))
48 e=gettupleitem(o,i)
49 else if(is_listitem(o))
50 e=getlistitem(o,i)
51
52 If the programmer wants to get an item from another type of object
53 that provides sequence behavior, there is no clear way to do it
54 correctly.
55
56 The persistent programmer may peruse object.h and find that the
57 _typeobject structure provides a means of invoking up to (currently
58 about) 41 special operators. So, for example, a routine can get an
59 item from any object that provides sequence behavior. However, to
60 use this mechanism, the programmer must make their code dependent on
61 the current Python implementation.
62
63 Also, certain semantics, especially memory management semantics, may
64 differ by the type of object being used. Unfortunately, these
65 semantics are not clearly described in the current include files.
66 An abstract interface providing more consistent semantics is needed.
67
68Proposal
69
70 I propose the creation of a standard interface (with an associated
71 library of routines and/or macros) for generically obtaining the
72 services of Python objects. This proposal can be viewed as one
73 components of a Python C interface consisting of several components.
74
75 From the viewpoint of of C access to Python services, we have (as
76 suggested by Guido in off-line discussions):
77
78 - "Very high level layer": two or three functions that let you exec or
79 eval arbitrary Python code given as a string in a module whose name is
80 given, passing C values in and getting C values out using
81 mkvalue/getargs style format strings. This does not require the user
82 to declare any variables of type "PyObject *". This should be enough
83 to write a simple application that gets Python code from the user,
84 execs it, and returns the output or errors. (Error handling must also
85 be part of this API.)
86
87 - "Abstract objects layer": which is the subject of this proposal.
88 It has many functions operating on objects, and lest you do many
89 things from C that you can also write in Python, without going
90 through the Python parser.
91
92 - "Concrete objects layer": This is the public type-dependent
93 interface provided by the standard built-in types, such as floats,
94 strings, and lists. This interface exists and is currently
95 documented by the collection of include files provides with the
96 Python distributions.
97
98 From the point of view of Python accessing services provided by C
99 modules:
100
101 - "Python module interface": this interface consist of the basic
102 routines used to define modules and their members. Most of the
103 current extensions-writing guide deals with this interface.
104
105 - "Built-in object interface": this is the interface that a new
106 built-in type must provide and the mechanisms and rules that a
107 developer of a new built-in type must use and follow.
108
109 This proposal is a "first-cut" that is intended to spur
110 discussion. See especially the lists of notes.
111
112 The Python C object interface will provide four protocols: object,
113 numeric, sequence, and mapping. Each protocol consists of a
114 collection of related operations. If an operation that is not
115 provided by a particular type is invoked, then a standard exception,
116 NotImplementedError is raised with a operation name as an argument.
117 In addition, for convenience this interface defines a set of
118 constructors for building objects of built-in types. This is needed
119 so new objects can be returned from C functions that otherwise treat
120 objects generically.
121
122Memory Management
123
124 For all of the functions described in this proposal, if a function
125 retains a reference to a Python object passed as an argument, then the
126 function will increase the reference count of the object. It is
127 unnecessary for the caller to increase the reference count of an
128 argument in anticipation of the object's retention.
129
130 All Python objects returned from functions should be treated as new
131 objects. Functions that return objects assume that the caller will
132 retain a reference and the reference count of the object has already
133 been incremented to account for this fact. A caller that does not
134 retain a reference to an object that is returned from a function
135 must decrement the reference count of the object (using
136 DECREF(object)) to prevent memory leaks.
137
138 Note that the behavior mentioned here is different from the current
139 behavior for some objects (e.g. lists and tuples) when certain
140 type-specific routines are called directly (e.g. setlistitem). The
141 proposed abstraction layer will provide a consistent memory
142 management interface, correcting for inconsistent behavior for some
143 built-in types.
144
145Protocols
146
147xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
148
149/* Object Protocol: */
150
151 /* Implemented elsewhere:
152
153 int PyObject_Print(PyObject *o, FILE *fp, int flags);
154
155 Print an object, o, on file, fp. Returns -1 on
156 error. The flags argument is used to enable certain printing
157 options. The only option currently supported is Py_Print_RAW.
158
159 (What should be said about Py_Print_RAW?)
160
161 */
162
163 /* Implemented elsewhere:
164
165 int PyObject_HasAttrString(PyObject *o, char *attr_name);
166
167 Returns 1 if o has the attribute attr_name, and 0 otherwise.
168 This is equivalent to the Python expression:
169 hasattr(o,attr_name).
170
171 This function always succeeds.
172
173 */
174
175 /* Implemented elsewhere:
176
177 PyObject* PyObject_GetAttrString(PyObject *o, char *attr_name);
178
179 Retrieve an attributed named attr_name form object o.
180 Returns the attribute value on success, or NULL on failure.
181 This is the equivalent of the Python expression: o.attr_name.
182
183 */
184
185 /* Implemented elsewhere:
186
187 int PyObject_HasAttr(PyObject *o, PyObject *attr_name);
188
189 Returns 1 if o has the attribute attr_name, and 0 otherwise.
190 This is equivalent to the Python expression:
191 hasattr(o,attr_name).
192
193 This function always succeeds.
194
195 */
196
197 /* Implemented elsewhere:
198
199 PyObject* PyObject_GetAttr(PyObject *o, PyObject *attr_name);
200
201 Retrieve an attributed named attr_name form object o.
202 Returns the attribute value on success, or NULL on failure.
203 This is the equivalent of the Python expression: o.attr_name.
204
205 */
206
207
208 /* Implemented elsewhere:
209
210 int PyObject_SetAttrString(PyObject *o, char *attr_name, PyObject *v);
211
212 Set the value of the attribute named attr_name, for object o,
213 to the value, v. Returns -1 on failure. This is
214 the equivalent of the Python statement: o.attr_name=v.
215
216 */
217
218 /* Implemented elsewhere:
219
220 int PyObject_SetAttr(PyObject *o, PyObject *attr_name, PyObject *v);
221
222 Set the value of the attribute named attr_name, for object o,
223 to the value, v. Returns -1 on failure. This is
224 the equivalent of the Python statement: o.attr_name=v.
225
226 */
227
228 /* implemented as a macro:
229
230 int PyObject_DelAttrString(PyObject *o, char *attr_name);
231
232 Delete attribute named attr_name, for object o. Returns
233 -1 on failure. This is the equivalent of the Python
234 statement: del o.attr_name.
235
236 */
237#define PyObject_DelAttrString(O,A) PyObject_SetAttrString((O),(A),NULL)
238
239 /* implemented as a macro:
240
241 int PyObject_DelAttr(PyObject *o, PyObject *attr_name);
242
243 Delete attribute named attr_name, for object o. Returns -1
244 on failure. This is the equivalent of the Python
245 statement: del o.attr_name.
246
247 */
248#define PyObject_DelAttr(O,A) PyObject_SetAttr((O),(A),NULL)
249
250 int PyObject_Cmp(PyObject *o1, PyObject *o2, int *result);
251
252 /*
253 Compare the values of o1 and o2 using a routine provided by
254 o1, if one exists, otherwise with a routine provided by o2.
255 The result of the comparison is returned in result. Returns
256 -1 on failure. This is the equivalent of the Python
257 statement: result=cmp(o1,o2).
258
259 */
260
261 /* Implemented elsewhere:
262
263 int PyObject_Compare(PyObject *o1, PyObject *o2);
264
265 Compare the values of o1 and o2 using a routine provided by
266 o1, if one exists, otherwise with a routine provided by o2.
267 Returns the result of the comparison on success. On error,
268 the value returned is undefined. This is equivalent to the
269 Python expression: cmp(o1,o2).
270
271 */
272
273 /* Implemented elsewhere:
274
275 PyObject *PyObject_Repr(PyObject *o);
276
277 Compute the string representation of object, o. Returns the
278 string representation on success, NULL on failure. This is
279 the equivalent of the Python expression: repr(o).
280
281 Called by the repr() built-in function and by reverse quotes.
282
283 */
284
285 /* Implemented elsewhere:
286
287 PyObject *PyObject_Str(PyObject *o);
288
289 Compute the string representation of object, o. Returns the
290 string representation on success, NULL on failure. This is
291 the equivalent of the Python expression: str(o).)
292
293 Called by the str() built-in function and by the print
294 statement.
295
296 */
297
298 int PyCallable_Check(PyObject *o);
299
300 /*
301 Determine if the object, o, is callable. Return 1 if the
302 object is callable and 0 otherwise.
303
304 This function always succeeds.
305
306 */
307
308
309
310 PyObject *PyObject_CallObject(PyObject *callable_object, PyObject *args);
311
312 /*
313
314 Call a callable Python object, callable_object, with
315 arguments given by the tuple, args. If no arguments are
316 needed, then args may be NULL. Returns the result of the
317 call on success, or NULL on failure. This is the equivalent
318 of the Python expression: apply(o,args).
319
320 */
321
322 PyObject *PyObject_CallFunction(PyObject *callable_object, char *format, ...);
323
324 /*
325 Call a callable Python object, callable_object, with a
326 variable number of C arguments. The C arguments are described
327 using a mkvalue-style format string. The format may be NULL,
328 indicating that no arguments are provided. Returns the
329 result of the call on success, or NULL on failure. This is
330 the equivalent of the Python expression: apply(o,args).
331
332 */
333
334
335 PyObject *PyObject_CallMethod(PyObject *o, char *m, char *format, ...);
336
337 /*
338 Call the method named m of object o with a variable number of
339 C arguments. The C arguments are described by a mkvalue
340 format string. The format may be NULL, indicating that no
341 arguments are provided. Returns the result of the call on
342 success, or NULL on failure. This is the equivalent of the
343 Python expression: o.method(args).
344
345 Note that Special method names, such as "__add__",
346 "__getitem__", and so on are not supported. The specific
347 abstract-object routines for these must be used.
348
349 */
350
351
352 /* Implemented elsewhere:
353
354 long PyObject_Hash(PyObject *o);
355
356 Compute and return the hash, hash_value, of an object, o. On
357 failure, return -1. This is the equivalent of the Python
358 expression: hash(o).
359
360 */
361
362
363 /* Implemented elsewhere:
364
365 int PyObject_IsTrue(PyObject *o);
366
367 Returns 1 if the object, o, is considered to be true, and
368 0 otherwise. This is equivalent to the Python expression:
369 not not o
370
371 This function always succeeds.
372
373 */
374
375 PyObject *PyObject_Type(PyObject *o);
376
377 /*
378 On success, returns a type object corresponding to the object
379 type of object o. On failure, returns NULL. This is
380 equivalent to the Python expression: type(o).
381 */
382
383 int PyObject_Length(PyObject *o);
384
385 /*
386 Return the length of object o. If the object, o, provides
387 both sequence and mapping protocols, the sequence length is
388 returned. On error, -1 is returned. This is the equivalent
389 to the Python expression: len(o).
390
391 */
392
393 PyObject *PyObject_GetItem(PyObject *o, PyObject *key);
394
395 /*
396 Return element of o corresponding to the object, key, or NULL
397 on failure. This is the equivalent of the Python expression:
398 o[key].
399
400 */
401
402 int PyObject_SetItem(PyObject *o, PyObject *key, PyObject *v);
403
404 /*
405 Map the object, key, to the value, v. Returns
406 -1 on failure. This is the equivalent of the Python
407 statement: o[key]=v.
408 */
409
410
411/* Number Protocol:*/
412
413 int PyNumber_Check(PyObject *o);
414
415 /*
416 Returns 1 if the object, o, provides numeric protocols, and
417 false otherwise.
418
419 This function always succeeds.
420
421 */
422
423 PyObject *PyNumber_Add(PyObject *o1, PyObject *o2);
424
425 /*
426 Returns the result of adding o1 and o2, or null on failure.
427 This is the equivalent of the Python expression: o1+o2.
428
429
430 */
431
432 PyObject *PyNumber_Subtract(PyObject *o1, PyObject *o2);
433
434 /*
435 Returns the result of subtracting o2 from o1, or null on
436 failure. This is the equivalent of the Python expression:
437 o1-o2.
438
439 */
440
441 PyObject *PyNumber_Multiply(PyObject *o1, PyObject *o2);
442
443 /*
444 Returns the result of multiplying o1 and o2, or null on
445 failure. This is the equivalent of the Python expression:
446 o1*o2.
447
448
449 */
450
451 PyObject *PyNumber_Divide(PyObject *o1, PyObject *o2);
452
453 /*
454 Returns the result of dividing o1 by o2, or null on failure.
455 This is the equivalent of the Python expression: o1/o2.
456
457
458 */
459
460 PyObject *PyNumber_Remainder(PyObject *o1, PyObject *o2);
461
462 /*
463 Returns the remainder of dividing o1 by o2, or null on
464 failure. This is the equivalent of the Python expression:
465 o1%o2.
466
467
468 */
469
470 PyObject *PyNumber_Divmod(PyObject *o1, PyObject *o2);
471
472 /*
473 See the built-in function divmod. Returns NULL on failure.
474 This is the equivalent of the Python expression:
475 divmod(o1,o2).
476
477
478 */
479
480 PyObject *PyNumber_Power(PyObject *o1, PyObject *o2, PyObject *o3);
481
482 /*
483 See the built-in function pow. Returns NULL on failure.
484 This is the equivalent of the Python expression:
485 pow(o1,o2,o3), where o3 is optional.
486
487 */
488
489 PyObject *PyNumber_Negative(PyObject *o);
490
491 /*
492 Returns the negation of o on success, or null on failure.
493 This is the equivalent of the Python expression: -o.
494
495 */
496
497 PyObject *PyNumber_Positive(PyObject *o);
498
499 /*
500 Returns the (what?) of o on success, or NULL on failure.
501 This is the equivalent of the Python expression: +o.
502
503 */
504
505 PyObject *PyNumber_Absolute(PyObject *o);
506
507 /*
508 Returns the absolute value of o, or null on failure. This is
509 the equivalent of the Python expression: abs(o).
510
511 */
512
513 PyObject *PyNumber_Invert(PyObject *o);
514
515 /*
516 Returns the bitwise negation of o on success, or NULL on
517 failure. This is the equivalent of the Python expression:
518 ~o.
519
520
521 */
522
523 PyObject *PyNumber_Lshift(PyObject *o1, PyObject *o2);
524
525 /*
526 Returns the result of left shifting o1 by o2 on success, or
527 NULL on failure. This is the equivalent of the Python
528 expression: o1 << o2.
529
530
531 */
532
533 PyObject *PyNumber_Rshift(PyObject *o1, PyObject *o2);
534
535 /*
536 Returns the result of right shifting o1 by o2 on success, or
537 NULL on failure. This is the equivalent of the Python
538 expression: o1 >> o2.
539
540 */
541
542 PyObject *PyNumber_And(PyObject *o1, PyObject *o2);
543
544 /*
545 Returns the result of "anding" o2 and o2 on success and NULL
546 on failure. This is the equivalent of the Python
547 expression: o1 and o2.
548
549
550 */
551
552 PyObject *PyNumber_Xor(PyObject *o1, PyObject *o2);
553
554 /*
555 Returns the bitwise exclusive or of o1 by o2 on success, or
556 NULL on failure. This is the equivalent of the Python
557 expression: o1^o2.
558
559
560 */
561
562 PyObject *PyNumber_Or(PyObject *o1, PyObject *o2);
563
564 /*
565 Returns the result or o1 and o2 on success, or NULL on
566 failure. This is the equivalent of the Python expression:
567 o1 or o2.
568
569 */
570
571 /* Implemented elsewhere:
572
573 int PyNumber_Coerce(PyObject *o1, PyObject *o2);
574
575 On success, returns a tuple containing o1 and o2 converted to
576 a common numeric type, or None if no conversion is possible.
577 Returns -1 on failure. This is equivalent to the Python
578 expression: coerce(o1,o2).
579
580 */
581
582 PyObject *PyNumber_Int(PyObject *o);
583
584 /*
585 Returns the o converted to an integer object on success, or
586 NULL on failure. This is the equivalent of the Python
587 expression: int(o).
588
589 */
590
591 PyObject *PyNumber_Long(PyObject *o);
592
593 /*
594 Returns the o converted to a long integer object on success,
595 or NULL on failure. This is the equivalent of the Python
596 expression: long(o).
597
598 */
599
600 PyObject *PyNumber_Float(PyObject *o);
601
602 /*
603 Returns the o converted to a float object on success, or NULL
604 on failure. This is the equivalent of the Python expression:
605 float(o).
606 */
607
608
609/* Sequence protocol:*/
610
611 int PySequence_Check(PyObject *o);
612
613 /*
614 Return 1 if the object provides sequence protocol, and zero
615 otherwise.
616
617 This function always succeeds.
618
619 */
620
621 PyObject *PySequence_Concat(PyObject *o1, PyObject *o2);
622
623 /*
624 Return the concatination of o1 and o2 on success, and NULL on
625 failure. This is the equivalent of the Python
626 expression: o1+o2.
627
628 */
629
630 PyObject *PySequence_Repeat(PyObject *o, int count);
631
632 /*
633 Return the result of repeating sequence object o count times,
634 or NULL on failure. This is the equivalent of the Python
635 expression: o1*count.
636
637 */
638
639 PyObject *PySequence_GetItem(PyObject *o, int i);
640
641 /*
642 Return the ith element of o, or NULL on failure. This is the
643 equivalent of the Python expression: o[i].
644
645 */
646
647 PyObject *PySequence_GetSlice(PyObject *o, int i1, int i2);
648
649 /*
650 Return the slice of sequence object o between i1 and i2, or
651 NULL on failure. This is the equivalent of the Python
652 expression: o[i1:i2].
653
654 */
655
656 int PySequence_SetItem(PyObject *o, int i, PyObject *v);
657
658 /*
659 Assign object v to the ith element of o. Returns
660 -1 on failure. This is the equivalent of the Python
661 statement: o[i]=v.
662
663 */
664
665 int PySequence_SetSlice(PyObject *o, int i1, int i2, PyObject *v);
666
667 /*
668 Assign the sequence object, v, to the slice in sequence
669 object, o, from i1 to i2. Returns -1 on failure. This is the
670 equivalent of the Python statement: o[i1:i2]=v.
671 */
672
673 PyObject *PySequence_Tuple(PyObject *o);
674
675 /*
676 Returns the o as a tuple on success, and NULL on failure.
677 This is equivalent to the Python expression: tuple(o)
678 */
679
680 int PySequence_Count(PyObject *o, PyObject *value);
681
682 /*
683 Return the number of occurrences on value on o, that is,
684 return the number of keys for which o[key]==value. On
685 failure, return -1. This is equivalent to the Python
686 expression: o.count(value).
687 */
688
689 int PySequence_In(PyObject *o, PyObject *value);
690
691 /*
692 Determine if o contains value. If an item in o is equal to
693 X, return 1, otherwise return 0. On error, return -1. This
694 is equivalent to the Python expression: value in o.
695 */
696
697 int PySequence_Index(PyObject *o, PyObject *value);
698
699 /*
700 Return the first index for which o[i]=value. On error,
701 return -1. This is equivalent to the Python
702 expression: o.index(value).
703 */
704
705/* Mapping protocol:*/
706
707 int PyMapping_Check(PyObject *o);
708
709 /*
710 Return 1 if the object provides mapping protocol, and zero
711 otherwise.
712
713 This function always succeeds.
714 */
715
716 int PyMapping_Length(PyObject *o);
717
718 /*
719 Returns the number of keys in object o on success, and -1 on
720 failure. For objects that do not provide sequence protocol,
721 this is equivalent to the Python expression: len(o).
722 */
723
724 int PyMapping_DelItemString(PyObject *o, char *key);
725
726 /*
727 Remove the mapping for object, key, from the object *o.
728 Returns -1 on failure. This is equivalent to
729 the Python statement: del o[key].
730 */
731
732 int PyMapping_DelItem(PyObject *o, PyObject *key);
733
734 /*
735 Remove the mapping for object, key, from the object *o.
736 Returns -1 on failure. This is equivalent to
737 the Python statement: del o[key].
738 */
739
740 int PyMapping_HasKeyString(PyObject *o, char *key);
741
742 /*
743 On success, return 1 if the mapping object has the key, key,
744 and 0 otherwise. This is equivalent to the Python expression:
745 o.has_key(key).
746
747 This function always succeeds.
748 */
749
750 int PyMapping_HasKey(PyObject *o, PyObject *key);
751
752 /*
753 Return 1 if the mapping object has the key, key,
754 and 0 otherwise. This is equivalent to the Python expression:
755 o.has_key(key).
756
757 This function always succeeds.
758
759 */
760
761 /* Implemented as macro:
762
763 PyObject *PyMapping_Keys(PyObject *o);
764
765 On success, return a list of the keys in object o. On
766 failure, return NULL. This is equivalent to the Python
767 expression: o.keys().
768 */
769#define PyMapping_Keys(O) PyObject_CallMethod(O,"keys",NULL)
770
771 /* Implemented as macro:
772
773 PyObject *PyMapping_Values(PyObject *o);
774
775 On success, return a list of the values in object o. On
776 failure, return NULL. This is equivalent to the Python
777 expression: o.values().
778 */
779#define PyMapping_Values(O) PyObject_CallMethod(O,"values",NULL)
780
781 /* Implemented as macro:
782
783 PyObject *PyMapping_Items(PyObject *o);
784
785 On success, return a list of the items in object o, where
786 each item is a tuple containing a key-value pair. On
787 failure, return NULL. This is equivalent to the Python
788 expression: o.items().
789
790 */
791#define PyMapping_Items(O) PyObject_CallMethod(O,"items",NULL)
792
793 PyObject *PyMapping_GetItemString(PyObject *o, char *key);
794
795 /*
796 Return element of o corresponding to the object, key, or NULL
797 on failure. This is the equivalent of the Python expression:
798 o[key].
799 */
800
801 int PyMapping_SetItemString(PyObject *o, char *key, PyObject *value);
802
803 /*
804 Map the object, key, to the value, v. Returns
805 -1 on failure. This is the equivalent of the Python
806 statement: o[key]=v.
807 */
808
809
810#endif /* Py_ABSTRACTOBJECT_H */