blob: b7249a6368fefd4b83825bf6f8c6db4045aee4f6 [file] [log] [blame]
Guido van Rossum267e80d1996-08-09 21:01:07 +00001
2\section{Extension Reference}
3
4From the viewpoint of of C access to Python services, we have:
5
6\begin{enumerate}
7 \item "Very high level layer": two or three functions that let you exec or
8 eval arbitrary Python code given as a string in a module whose name is
9 given, passing C values in and getting C values out using
10 mkvalue/getargs style format strings. This does not require the user
11 to declare any variables of type "PyObject *". This should be enough
12 to write a simple application that gets Python code from the user,
13 execs it, and returns the output or errors.
14
15 \item "Abstract objects layer": which is the subject of this proposal.
16 It has many functions operating on objects, and lest you do many
17 things from C that you can also write in Python, without going
18 through the Python parser.
19
20 \item "Concrete objects layer": This is the public type-dependent
21 interface provided by the standard built-in types, such as floats,
22 strings, and lists. This interface exists and is currently
23 documented by the collection of include files provides with the
24 Python distributions.
25
26 From the point of view of Python accessing services provided by C
27 modules:
28
29 \item "Python module interface": this interface consist of the basic
30 routines used to define modules and their members. Most of the
31 current extensions-writing guide deals with this interface.
32
33 \item "Built-in object interface": this is the interface that a new
34 built-in type must provide and the mechanisms and rules that a
35 developer of a new built-in type must use and follow.
36\end{enumerate}
37
38 The Python C object interface provides four protocols: object,
39 numeric, sequence, and mapping. Each protocol consists of a
40 collection of related operations. If an operation that is not
41 provided by a particular type is invoked, then a standard exception,
42 NotImplementedError is raised with a operation name as an argument.
43 In addition, for convenience this interface defines a set of
44 constructors for building objects of built-in types. This is needed
45 so new objects can be returned from C functions that otherwise treat
46 objects generically.
47
48\subsubsection{Object Protocol}
49 \code{int *PyObject_Print(PyObject *o, FILE *fp, int flags)}\\
50 Print an object \code{o}, on file \code{fp}. Returns -1 on error
51 The flags argument is used to enable certain printing
52 options. The only option currently supported is \code{Py_Print_RAW}.
53
54 \code{int PyObject_HasAttrString(PyObject *o, char *attr_name)}\\
55 Returns 1 if o has the attribute attr_name, and 0 otherwise.
56 This is equivalent to the Python expression:
57 \code{hasattr(o,attr_name)}.
58 This function always succeeds.
59
60 \code{PyObject* PyObject_AttrString(PyObject *o, char *attr_name)}\\
61 Retrieve an attributed named attr_name form object o.
62 Returns the attribute value on success, or NULL on failure.
63 This is the equivalent of the Python expression: \code{o.attr_name}.
64
65
66 \code{int PyObject_HasAttr(PyObject *o, PyObject *attr_name)}\\
67 Returns 1 if o has the attribute attr_name, and 0 otherwise.
68 This is equivalent to the Python expression:
69 \code{hasattr(o,attr_name)}.
70 This function always succeeds.
71
72
73 \code{PyObject* PyObject_GetAttr(PyObject *o, PyObject *attr_name)}\\
74 Retrieve an attributed named attr_name form object o.
75 Returns the attribute value on success, or NULL on failure.
76 This is the equivalent of the Python expression: o.attr_name.
77
78
79 \code{int PyObject_SetAttrString(PyObject *o, char *attr_name, PyObject *v)}\\
80 Set the value of the attribute named \code{attr_name}, for object \code{o},
81 to the value \code{v}. Returns -1 on failure. This is
82 the equivalent of the Python statement: \code{o.attr_name=v}.
83
84
85 \code{int PyObject_SetAttr(PyObject *o, PyObject *attr_name, PyObject *v)}\\
86 Set the value of the attribute named \code{attr_name}, for object \code{o},
87 to the value \code{v}. Returns -1 on failure. This is
88 the equivalent of the Python statement: \code{o.attr_name=v}.
89
90
91 \code{int PyObject_DelAttrString(PyObject *o, char *attr_name)}\\
92 Delete attribute named \code{attr_name}, for object \code{o}. Returns -1 on
93 failure. This is the equivalent of the Python
94 statement: \code{del o.attr_name}.
95
96
97 \code{int PyObject_DelAttr(PyObject *o, PyObject *attr_name)}\\
98 Delete attribute named \code{attr_name}, for object \code{o}. Returns -1 on
99 failure. This is the equivalent of the Python
100 statement: \code{del o.attr_name}.
101
102
103 \code{int PyObject_Cmp(PyObject *o1, PyObject *o2, int *result)}\\
104 Compare the values of \code{o1} and \code{o2} using a routine provided by
105 \code{o1}, if one exists, otherwise with a routine provided by \code{o2}.
106 The result of the comparison is returned in \code{result}. Returns
107 -1 on failure. This is the equivalent of the Python
108 statement: \code{result=cmp(o1,o2)}.
109
110
111 \code{int PyObject_Compare(PyObject *o1, PyObject *o2)}\\
112 Compare the values of \code{o1} and \code{o2} using a routine provided by
113 \code{o1}, if one exists, otherwise with a routine provided by \code{o2}.
114 Returns the result of the comparison on success. On error,
115 the value returned is undefined. This is equivalent to the
116 Python expression: \code{cmp(o1,o2)}.
117
118
119 \code{PyObject *PyObject_Repr(PyObject *o)}\\
120 Compute the string representation of object, \code{o}. Returns the
121 string representation on success, NULL on failure. This is
122 the equivalent of the Python expression: \code{repr(o)}.
123 Called by the \code{repr()} built-in function and by reverse quotes.
124
125
126 \code{PyObject *PyObject_Str(PyObject *o)}\\
127 Compute the string representation of object, \code{o}. Returns the
128 string representation on success, NULL on failure. This is
129 the equivalent of the Python expression: \code{str(o)}.
130 Called by the \code{str()} built-in function and by the \code{print}
131 statement.
132
133
134 \code{int *PyCallable_Check(PyObject *o))}\\
135 Determine if the object \code{o}, is callable. Return 1 if the
136 object is callable and 0 otherwise.
137 This function always succeeds.
138
139
140 \code{PyObject *PyObject_CallObject(PyObject *callable_object, PyObject *args)}\\
141 Call a callable Python object \code{callable_object}, with
142 arguments given by the tuple \code{args}. If no arguments are
143 needed, then args may be NULL. Returns the result of the
144 call on success, or NULL on failure. This is the equivalent
145 of the Python expression: \code{apply(o,args)}.
146
147 \code{PyObject *PyObject_CallFunction(PyObject *callable_object, char *format, ...)}\\
148 Call a callable Python object \code{callable_object}, with a
149 variable number of C arguments. The C arguments are described
150 using a mkvalue-style format string. The format may be NULL,
151 indicating that no arguments are provided. Returns the
152 result of the call on success, or NULL on failure. This is
153 the equivalent of the Python expression: \code{apply(o,args)}.
154
155
156 \code{PyObject *PyObject_CallMethod(PyObject *o, char *m, char *format, ...)}\\
157 Call the method named \code{m} of object \code{o} with a variable number of
158 C arguments. The C arguments are described by a mkvalue
159 format string. The format may be NULL, indicating that no
160 arguments are provided. Returns the result of the call on
161 success, or NULL on failure. This is the equivalent of the
162 Python expression: \code{o.method(args)}.
163 Note that Special method names, such as "\code{__add__}",
164 "\code{__getitem__}", and so on are not supported. The specific
165 abstract-object routines for these must be used.
166
167
168 \code{int PyObject_Hash(PyObject *o)}\\
169 Compute and return the hash value of an object \code{o}. On
170 failure, return -1. This is the equivalent of the Python
171 expression: \code{hash(o)}.
172
173
174 \code{int *PyObject_IsTrue(PyObject *o)}\\
175 Returns 1 if the object \code{o} is considered to be true, and
176 0 otherwise. This is equivalent to the Python expression:
177 \code{not not o}.
178 This function always succeeds.
179
180
181 \code{PyObject *PyObject_Type(PyObject *o)}\\
182 On success, returns a type object corresponding to the object
183 type of object \code{o}. On failure, returns NULL. This is
184 equivalent to the Python expression: \code{type(o)}.
185
186 \code{int PyObject_Length(PyObject *o)}\\
187 Return the length of object \code{o}. If the object \code{o} provides
188 both sequence and mapping protocols, the sequence length is
189 returned. On error, -1 is returned. This is the equivalent
190 to the Python expression: \code{len(o)}.
191
192
193 \code{PyObject *PyObject_GetItem(PyObject *o, PyObject *key)}\\
194 Return element of \code{o} corresponding to the object \code{key} or NULL
195 on failure. This is the equivalent of the Python expression:
196 \code{o[key]}.
197
198
199 \code{int PyObject_SetItem(PyObject *o, PyObject *key, PyObject *v)}\\
200 Map the object \code{key} to the value \code{v}.
201 Returns -1 on failure. This is the equivalent
202 of the Python statement: \code{o[key]=v}.
203
204
Guido van Rossumd0f11de1996-08-21 19:08:12 +0000205 \code{int PyObject_DelItem(PyObject *o, PyObject *key, PyObject *v)}\\
206 Delete the mapping for \code{key} from \code{*o}. Returns -1
207 on failure.
208 This is the equivalent of the Python statement: del o[key].
209
210
Guido van Rossum267e80d1996-08-09 21:01:07 +0000211\subsubsection{Number Protocol}
212
213 \code{int PyNumber_Check(PyObject *o)}\\
214 Returns 1 if the object \code{o} provides numeric protocols, and
215 false otherwise.
216 This function always succeeds.
217
218
219 \code{PyObject *PyNumber_Add(PyObject *o1, PyObject *o2)}\\
220 Returns the result of adding \code{o1} and \code{o2}, or null on failure.
221 This is the equivalent of the Python expression: \code{o1+o2}.
222
223
224 \code{PyObject *PyNumber_Subtract(PyObject *o1, PyObject *o2)}\\
225 Returns the result of subtracting \code{o2} from \code{o1}, or null on
226 failure. This is the equivalent of the Python expression:
227 \code{o1-o2}.
228
229
230 \code{PyObject *PyNumber_Multiply(PyObject *o1, PyObject *o2)}\\
231 Returns the result of multiplying \code{o1} and \code{o2}, or null on
232 failure. This is the equivalent of the Python expression:
233 \code{o1*o2}.
234
235
236 \code{PyObject *PyNumber_Divide(PyObject *o1, PyObject *o2)}\\
237 Returns the result of dividing \code{o1} by \code{o2}, or null on failure.
238 This is the equivalent of the Python expression: \code{o1/o2}.
239
240
241 \code{PyObject *PyNumber_Remainder(PyObject *o1, PyObject *o2)}\\
242 Returns the remainder of dividing \code{o1} by \code{o2}, or null on
243 failure. This is the equivalent of the Python expression:
244 \code{o1\%o2}.
245
246
247 \code{PyObject *PyNumber_Divmod(PyObject *o1, PyObject *o2)}\\
248 See the built-in function divmod. Returns NULL on failure.
249 This is the equivalent of the Python expression:
250 \code{divmod(o1,o2)}.
251
252
253 \code{PyObject *PyNumber_Power(PyObject *o1, PyObject *o2, PyObject *o3)}\\
254 See the built-in function pow. Returns NULL on failure.
255 This is the equivalent of the Python expression:
256 \code{pow(o1,o2,o3)}, where \code{o3} is optional.
257
258
259 \code{PyObject *PyNumber_Negative(PyObject *o)}\\
260 Returns the negation of \code{o} on success, or null on failure.
261 This is the equivalent of the Python expression: \code{-o}.
262
263
264 \code{PyObject *PyNumber_Positive(PyObject *o)}\\
265 Returns \code{o} on success, or NULL on failure.
266 This is the equivalent of the Python expression: \code{+o}.
267
268
269 \code{PyObject *PyNumber_Absolute(PyObject *o)}\\
270 Returns the absolute value of \code{o}, or null on failure. This is
271 the equivalent of the Python expression: \code{abs(o)}.
272
273
274 \code{PyObject *PyNumber_Invert(PyObject *o)}\\
275 Returns the bitwise negation of \code{o} on success, or NULL on
276 failure. This is the equivalent of the Python expression:
277 \code{~o}.
278
279
280 \code{PyObject *PyNumber_Lshift(PyObject *o1, PyObject *o2)}\\
281 Returns the result of left shifting \code{o1} by \code{o2} on success, or
282 NULL on failure. This is the equivalent of the Python
283 expression: \code{o1 << o2}.
284
285
286 \code{PyObject *PyNumber_Rshift(PyObject *o1, PyObject *o2)}\\
287 Returns the result of right shifting \code{o1} by \code{o2} on success, or
288 NULL on failure. This is the equivalent of the Python
289 expression: \code{o1 >> o2}.
290
291
292 \code{PyObject *PyNumber_And(PyObject *o1, PyObject *o2)}\\
293 Returns the result of "anding" \code{o2} and \code{o2} on success and NULL
294 on failure. This is the equivalent of the Python
295 expression: \code{o1 and o2}.
296
297
298 \code{PyObject *PyNumber_Xor(PyObject *o1, PyObject *o2)}\\
299 Returns the bitwise exclusive or of \code{o1} by \code{o2} on success, or
300 NULL on failure. This is the equivalent of the Python
301 expression: \code{o1\^{ }o2}.
302
303 \code{PyObject *PyNumber_Or(PyObject *o1, PyObject *o2)}\\
304 Returns the result or \code{o1} and \code{o2} on success, or NULL on
305 failure. This is the equivalent of the Python expression:
306 \code{o1 or o2}.
307
308
309 \code{PyObject *PyNumber_Coerce(PyObject *o1, PyObject *o2)}\\
310 On success, returns a tuple containing \code{o1} and \code{o2} converted to
311 a common numeric type, or None if no conversion is possible.
312 Returns -1 on failure. This is equivalent to the Python
313 expression: \code{coerce(o1,o2)}.
314
315
316 \code{PyObject *PyNumber_Int(PyObject *o)}\\
317 Returns the \code{o} converted to an integer object on success, or
318 NULL on failure. This is the equivalent of the Python
319 expression: \code{int(o)}.
320
321
322 \code{PyObject *PyNumber_Long(PyObject *o)}\\
323 Returns the \code{o} converted to a long integer object on success,
324 or NULL on failure. This is the equivalent of the Python
325 expression: \code{long(o)}.
326
327
328 \code{PyObject *PyNumber_Float(PyObject *o)}\\
329 Returns the \code{o} converted to a float object on success, or NULL
330 on failure. This is the equivalent of the Python expression:
331 \code{float(o)}.
332
333
334\subsubsection{Sequence protocol}
335
336 \code{int PySequence_Check(PyObject *o)}\\
337 Return 1 if the object provides sequence protocol, and 0
338 otherwise.
339 This function always succeeds.
340
341
342 \code{PyObject *PySequence_Concat(PyObject *o1, PyObject *o2)}\\
343 Return the concatination of \code{o1} and \code{o2} on success, and NULL on
344 failure. This is the equivalent of the Python
345 expression: \code{o1+o2}.
346
347
348 \code{PyObject *PySequence_Repeat(PyObject *o, int count)}\\
349 Return the result of repeating sequence object \code{o} count times,
350 or NULL on failure. This is the equivalent of the Python
351 expression: \code{o*count}.
352
353
354 \code{PyObject *PySequence_GetItem(PyObject *o, int i)}\\
355 Return the ith element of \code{o}, or NULL on failure. This is the
356 equivalent of the Python expression: \code{o[i]}.
357
358
359 \code{PyObject *PySequence_GetSlice(PyObject *o, int i1, int i2)}\\
360 Return the slice of sequence object \code{o} between \code{i1} and \code{i2}, or
361 NULL on failure. This is the equivalent of the Python
362 expression, \code{o[i1:i2]}.
363
364
365 \code{int PySequence_SetItem(PyObject *o, int i, PyObject *v)}\\
366 Assign object \code{v} to the \code{i}th element of \code{o}.
367Returns -1 on failure. This is the equivalent of the Python
368 statement, \code{o[i]=v}.
369
Guido van Rossumd0f11de1996-08-21 19:08:12 +0000370 \code{int PySequence_DelItem(PyObject *o, int i)}\\
371 Delete the \code{i}th element of object \code{v}. Returns
372 -1 on failure. This is the equivalent of the Python
373 statement: \code{del o[i]}.
374
Guido van Rossum267e80d1996-08-09 21:01:07 +0000375 \code{int PySequence_SetSlice(PyObject *o, int i1, int i2, PyObject *v)}\\
376 Assign the sequence object \code{v} to the slice in sequence
377 object \code{o} from \code{i1} to \code{i2}. This is the equivalent of the Python
378 statement, \code{o[i1:i2]=v}.
379
Guido van Rossumd0f11de1996-08-21 19:08:12 +0000380 \code{int PySequence_DelSlice(PyObject *o, int i1, int i2)}\\
381 Delete the slice in sequence object, \code{o}, from \code{i1} to \code{i2}.
382 Returns -1 on failure. This is the equivalent of the Python
383 statement: \code{del o[i1:i2]}.
384
Guido van Rossum267e80d1996-08-09 21:01:07 +0000385 \code{PyObject *PySequence_Tuple(PyObject *o)}\\
386 Returns the \code{o} as a tuple on success, and NULL on failure.
387 This is equivalent to the Python expression: \code{tuple(o)}.
388
389 \code{int PySequence_Count(PyObject *o, PyObject *value)}\\
390 Return the number of occurrences of \code{value} on \code{o}, that is,
391 return the number of keys for which \code{o[key]==value}. On
392 failure, return -1. This is equivalent to the Python
393 expression: \code{o.count(value)}.
394
395 \code{int PySequence_In(PyObject *o, PyObject *value)}\\
396 Determine if \code{o} contains \code{value}. If an item in \code{o} is equal to
397 \code{value}, return 1, otherwise return 0. On error, return -1. This
398 is equivalent to the Python expression: \code{value in o}.
399
400 \code{int PySequence_Index(PyObject *o, PyObject *value)}\\
401 Return the first index for which \code{o[i]=value}. On error,
402 return -1. This is equivalent to the Python
403 expression: \code{o.index(value)}.
404
405\subsubsection{Mapping protocol}
406
407 \code{int PyMapping_Check(PyObject *o)}\\
408 Return 1 if the object provides mapping protocol, and 0
409 otherwise.
410 This function always succeeds.
411
412
413 \code{int PyMapping_Length(PyObject *o)}\\
414 Returns the number of keys in object \code{o} on success, and -1 on
415 failure. For objects that do not provide sequence protocol,
416 this is equivalent to the Python expression: \code{len(o)}.
417
418
419 \code{int PyMapping_DelItemString(PyObject *o, char *key)}\\
420 Remove the mapping for object \code{key} from the object \code{o}.
421 Return -1 on failure. This is equivalent to
422 the Python statement: \code{del o[key]}.
423
424
425 \code{int PyMapping_DelItem(PyObject *o, PyObject *key)}\\
426 Remove the mapping for object \code{key} from the object \code{o}.
427 Return -1 on failure. This is equivalent to
428 the Python statement: \code{del o[key]}.
429
430
431 \code{int PyMapping_HasKeyString(PyObject *o, char *key)}\\
432 On success, return 1 if the mapping object has the key \code{key}
433 and 0 otherwise. This is equivalent to the Python expression:
434 \code{o.has_key(key)}.
435 This function always succeeds.
436
437
438 \code{int PyMapping_HasKey(PyObject *o, PyObject *key)}\\
439 Return 1 if the mapping object has the key \code{key}
440 and 0 otherwise. This is equivalent to the Python expression:
441 \code{o.has_key(key)}.
442 This function always succeeds.
443
444
445 \code{PyObject *PyMapping_Keys(PyObject *o)}\\
446 On success, return a list of the keys in object \code{o}. On
447 failure, return NULL. This is equivalent to the Python
448 expression: \code{o.keys()}.
449
450
451 \code{PyObject *PyMapping_Values(PyObject *o)}\\
452 On success, return a list of the values in object \code{o}. On
453 failure, return NULL. This is equivalent to the Python
454 expression: \code{o.values()}.
455
456
457 \code{PyObject *PyMapping_Items(PyObject *o)}\\
458 On success, return a list of the items in object \code{o}, where
459 each item is a tuple containing a key-value pair. On
460 failure, return NULL. This is equivalent to the Python
461 expression: \code{o.items()}.
462
463 \code{int PyMapping_Clear(PyObject *o)}\\
464 Make object \code{o} empty. Returns 1 on success and 0 on failure.
465 This is equivalent to the Python statement:
466 \code{for key in o.keys(): del o[key]}
467
468
469 \code{PyObject *PyMapping_GetItemString(PyObject *o, char *key)}\\
470 Return element of \code{o} corresponding to the object \code{key} or NULL
471 on failure. This is the equivalent of the Python expression:
472 \code{o[key]}.
473
474 \code{PyObject *PyMapping_SetItemString(PyObject *o, char *key, PyObject *v)}\\
475 Map the object \code{key} to the value \code{v} in object \code{o}. Returns
476 -1 on failure. This is the equivalent of the Python
477 statement: \code{o[key]=v}.
478
479
480\subsubsection{Constructors}
481
482 \code{PyObject *PyFile_FromString(char *file_name, char *mode)}\\
483 On success, returns a new file object that is opened on the
484 file given by \code{file_name}, with a file mode given by \code{mode},
485 where \code{mode} has the same semantics as the standard C routine,
486 fopen. On failure, return -1.
487
488 \code{PyObject *PyFile_FromFile(FILE *fp, char *file_name, char *mode, int close_on_del)}\\
489 Return a new file object for an already opened standard C
490 file pointer, \code{fp}. A file name, \code{file_name}, and open mode,
491 \code{mode}, must be provided as well as a flag, \code{close_on_del}, that
492 indicates whether the file is to be closed when the file
493 object is destroyed. On failure, return -1.
494
495 \code{PyObject *PyFloat_FromDouble(double v)}\\
496 Returns a new float object with the value \code{v} on success, and
497 NULL on failure.
498
499 \code{PyObject *PyInt_FromLong(long v)}\\
500 Returns a new int object with the value \code{v} on success, and
501 NULL on failure.
502
503 \code{PyObject *PyList_New(int l)}\\
504 Returns a new list of length \code{l} on success, and NULL on
505 failure.
506
507 \code{PyObject *PyLong_FromLong(long v)}\\
508 Returns a new long object with the value \code{v} on success, and
509 NULL on failure.
510
511 \code{PyObject *PyLong_FromDouble(double v)}\\
512 Returns a new long object with the value \code{v} on success, and
513 NULL on failure.
514
515 \code{PyObject *PyDict_New()}\\
516 Returns a new empty dictionary on success, and NULL on
517 failure.
518
519 \code{PyObject *PyString_FromString(char *v)}\\
520 Returns a new string object with the value \code{v} on success, and
521 NULL on failure.
522
523 \code{PyObject *PyString_FromStringAndSize(char *v, int l)}\\
524 Returns a new string object with the value \code{v} and length \code{l}
525 on success, and NULL on failure.
526
527 \code{PyObject *PyTuple_New(int l)}\\
528 Returns a new tuple of length \code{l} on success, and NULL on
529 failure.
530