blob: 755539ac89e0a7f454acb283bb7194abfb0bf859 [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
205\subsubsection{Number Protocol}
206
207 \code{int PyNumber_Check(PyObject *o)}\\
208 Returns 1 if the object \code{o} provides numeric protocols, and
209 false otherwise.
210 This function always succeeds.
211
212
213 \code{PyObject *PyNumber_Add(PyObject *o1, PyObject *o2)}\\
214 Returns the result of adding \code{o1} and \code{o2}, or null on failure.
215 This is the equivalent of the Python expression: \code{o1+o2}.
216
217
218 \code{PyObject *PyNumber_Subtract(PyObject *o1, PyObject *o2)}\\
219 Returns the result of subtracting \code{o2} from \code{o1}, or null on
220 failure. This is the equivalent of the Python expression:
221 \code{o1-o2}.
222
223
224 \code{PyObject *PyNumber_Multiply(PyObject *o1, PyObject *o2)}\\
225 Returns the result of multiplying \code{o1} and \code{o2}, or null on
226 failure. This is the equivalent of the Python expression:
227 \code{o1*o2}.
228
229
230 \code{PyObject *PyNumber_Divide(PyObject *o1, PyObject *o2)}\\
231 Returns the result of dividing \code{o1} by \code{o2}, or null on failure.
232 This is the equivalent of the Python expression: \code{o1/o2}.
233
234
235 \code{PyObject *PyNumber_Remainder(PyObject *o1, PyObject *o2)}\\
236 Returns the remainder of dividing \code{o1} by \code{o2}, or null on
237 failure. This is the equivalent of the Python expression:
238 \code{o1\%o2}.
239
240
241 \code{PyObject *PyNumber_Divmod(PyObject *o1, PyObject *o2)}\\
242 See the built-in function divmod. Returns NULL on failure.
243 This is the equivalent of the Python expression:
244 \code{divmod(o1,o2)}.
245
246
247 \code{PyObject *PyNumber_Power(PyObject *o1, PyObject *o2, PyObject *o3)}\\
248 See the built-in function pow. Returns NULL on failure.
249 This is the equivalent of the Python expression:
250 \code{pow(o1,o2,o3)}, where \code{o3} is optional.
251
252
253 \code{PyObject *PyNumber_Negative(PyObject *o)}\\
254 Returns the negation of \code{o} on success, or null on failure.
255 This is the equivalent of the Python expression: \code{-o}.
256
257
258 \code{PyObject *PyNumber_Positive(PyObject *o)}\\
259 Returns \code{o} on success, or NULL on failure.
260 This is the equivalent of the Python expression: \code{+o}.
261
262
263 \code{PyObject *PyNumber_Absolute(PyObject *o)}\\
264 Returns the absolute value of \code{o}, or null on failure. This is
265 the equivalent of the Python expression: \code{abs(o)}.
266
267
268 \code{PyObject *PyNumber_Invert(PyObject *o)}\\
269 Returns the bitwise negation of \code{o} on success, or NULL on
270 failure. This is the equivalent of the Python expression:
271 \code{~o}.
272
273
274 \code{PyObject *PyNumber_Lshift(PyObject *o1, PyObject *o2)}\\
275 Returns the result of left shifting \code{o1} by \code{o2} on success, or
276 NULL on failure. This is the equivalent of the Python
277 expression: \code{o1 << o2}.
278
279
280 \code{PyObject *PyNumber_Rshift(PyObject *o1, PyObject *o2)}\\
281 Returns the result of right 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_And(PyObject *o1, PyObject *o2)}\\
287 Returns the result of "anding" \code{o2} and \code{o2} on success and NULL
288 on failure. This is the equivalent of the Python
289 expression: \code{o1 and o2}.
290
291
292 \code{PyObject *PyNumber_Xor(PyObject *o1, PyObject *o2)}\\
293 Returns the bitwise exclusive or of \code{o1} by \code{o2} on success, or
294 NULL on failure. This is the equivalent of the Python
295 expression: \code{o1\^{ }o2}.
296
297 \code{PyObject *PyNumber_Or(PyObject *o1, PyObject *o2)}\\
298 Returns the result or \code{o1} and \code{o2} on success, or NULL on
299 failure. This is the equivalent of the Python expression:
300 \code{o1 or o2}.
301
302
303 \code{PyObject *PyNumber_Coerce(PyObject *o1, PyObject *o2)}\\
304 On success, returns a tuple containing \code{o1} and \code{o2} converted to
305 a common numeric type, or None if no conversion is possible.
306 Returns -1 on failure. This is equivalent to the Python
307 expression: \code{coerce(o1,o2)}.
308
309
310 \code{PyObject *PyNumber_Int(PyObject *o)}\\
311 Returns the \code{o} converted to an integer object on success, or
312 NULL on failure. This is the equivalent of the Python
313 expression: \code{int(o)}.
314
315
316 \code{PyObject *PyNumber_Long(PyObject *o)}\\
317 Returns the \code{o} converted to a long integer object on success,
318 or NULL on failure. This is the equivalent of the Python
319 expression: \code{long(o)}.
320
321
322 \code{PyObject *PyNumber_Float(PyObject *o)}\\
323 Returns the \code{o} converted to a float object on success, or NULL
324 on failure. This is the equivalent of the Python expression:
325 \code{float(o)}.
326
327
328\subsubsection{Sequence protocol}
329
330 \code{int PySequence_Check(PyObject *o)}\\
331 Return 1 if the object provides sequence protocol, and 0
332 otherwise.
333 This function always succeeds.
334
335
336 \code{PyObject *PySequence_Concat(PyObject *o1, PyObject *o2)}\\
337 Return the concatination of \code{o1} and \code{o2} on success, and NULL on
338 failure. This is the equivalent of the Python
339 expression: \code{o1+o2}.
340
341
342 \code{PyObject *PySequence_Repeat(PyObject *o, int count)}\\
343 Return the result of repeating sequence object \code{o} count times,
344 or NULL on failure. This is the equivalent of the Python
345 expression: \code{o*count}.
346
347
348 \code{PyObject *PySequence_GetItem(PyObject *o, int i)}\\
349 Return the ith element of \code{o}, or NULL on failure. This is the
350 equivalent of the Python expression: \code{o[i]}.
351
352
353 \code{PyObject *PySequence_GetSlice(PyObject *o, int i1, int i2)}\\
354 Return the slice of sequence object \code{o} between \code{i1} and \code{i2}, or
355 NULL on failure. This is the equivalent of the Python
356 expression, \code{o[i1:i2]}.
357
358
359 \code{int PySequence_SetItem(PyObject *o, int i, PyObject *v)}\\
360 Assign object \code{v} to the \code{i}th element of \code{o}.
361Returns -1 on failure. This is the equivalent of the Python
362 statement, \code{o[i]=v}.
363
364 \code{int PySequence_SetSlice(PyObject *o, int i1, int i2, PyObject *v)}\\
365 Assign the sequence object \code{v} to the slice in sequence
366 object \code{o} from \code{i1} to \code{i2}. This is the equivalent of the Python
367 statement, \code{o[i1:i2]=v}.
368
369 \code{PyObject *PySequence_Tuple(PyObject *o)}\\
370 Returns the \code{o} as a tuple on success, and NULL on failure.
371 This is equivalent to the Python expression: \code{tuple(o)}.
372
373 \code{int PySequence_Count(PyObject *o, PyObject *value)}\\
374 Return the number of occurrences of \code{value} on \code{o}, that is,
375 return the number of keys for which \code{o[key]==value}. On
376 failure, return -1. This is equivalent to the Python
377 expression: \code{o.count(value)}.
378
379 \code{int PySequence_In(PyObject *o, PyObject *value)}\\
380 Determine if \code{o} contains \code{value}. If an item in \code{o} is equal to
381 \code{value}, return 1, otherwise return 0. On error, return -1. This
382 is equivalent to the Python expression: \code{value in o}.
383
384 \code{int PySequence_Index(PyObject *o, PyObject *value)}\\
385 Return the first index for which \code{o[i]=value}. On error,
386 return -1. This is equivalent to the Python
387 expression: \code{o.index(value)}.
388
389\subsubsection{Mapping protocol}
390
391 \code{int PyMapping_Check(PyObject *o)}\\
392 Return 1 if the object provides mapping protocol, and 0
393 otherwise.
394 This function always succeeds.
395
396
397 \code{int PyMapping_Length(PyObject *o)}\\
398 Returns the number of keys in object \code{o} on success, and -1 on
399 failure. For objects that do not provide sequence protocol,
400 this is equivalent to the Python expression: \code{len(o)}.
401
402
403 \code{int PyMapping_DelItemString(PyObject *o, char *key)}\\
404 Remove the mapping for object \code{key} from the object \code{o}.
405 Return -1 on failure. This is equivalent to
406 the Python statement: \code{del o[key]}.
407
408
409 \code{int PyMapping_DelItem(PyObject *o, PyObject *key)}\\
410 Remove the mapping for object \code{key} from the object \code{o}.
411 Return -1 on failure. This is equivalent to
412 the Python statement: \code{del o[key]}.
413
414
415 \code{int PyMapping_HasKeyString(PyObject *o, char *key)}\\
416 On success, return 1 if the mapping object has the key \code{key}
417 and 0 otherwise. This is equivalent to the Python expression:
418 \code{o.has_key(key)}.
419 This function always succeeds.
420
421
422 \code{int PyMapping_HasKey(PyObject *o, PyObject *key)}\\
423 Return 1 if the mapping object has the key \code{key}
424 and 0 otherwise. This is equivalent to the Python expression:
425 \code{o.has_key(key)}.
426 This function always succeeds.
427
428
429 \code{PyObject *PyMapping_Keys(PyObject *o)}\\
430 On success, return a list of the keys in object \code{o}. On
431 failure, return NULL. This is equivalent to the Python
432 expression: \code{o.keys()}.
433
434
435 \code{PyObject *PyMapping_Values(PyObject *o)}\\
436 On success, return a list of the values in object \code{o}. On
437 failure, return NULL. This is equivalent to the Python
438 expression: \code{o.values()}.
439
440
441 \code{PyObject *PyMapping_Items(PyObject *o)}\\
442 On success, return a list of the items in object \code{o}, where
443 each item is a tuple containing a key-value pair. On
444 failure, return NULL. This is equivalent to the Python
445 expression: \code{o.items()}.
446
447 \code{int PyMapping_Clear(PyObject *o)}\\
448 Make object \code{o} empty. Returns 1 on success and 0 on failure.
449 This is equivalent to the Python statement:
450 \code{for key in o.keys(): del o[key]}
451
452
453 \code{PyObject *PyMapping_GetItemString(PyObject *o, char *key)}\\
454 Return element of \code{o} corresponding to the object \code{key} or NULL
455 on failure. This is the equivalent of the Python expression:
456 \code{o[key]}.
457
458 \code{PyObject *PyMapping_SetItemString(PyObject *o, char *key, PyObject *v)}\\
459 Map the object \code{key} to the value \code{v} in object \code{o}. Returns
460 -1 on failure. This is the equivalent of the Python
461 statement: \code{o[key]=v}.
462
463
464\subsubsection{Constructors}
465
466 \code{PyObject *PyFile_FromString(char *file_name, char *mode)}\\
467 On success, returns a new file object that is opened on the
468 file given by \code{file_name}, with a file mode given by \code{mode},
469 where \code{mode} has the same semantics as the standard C routine,
470 fopen. On failure, return -1.
471
472 \code{PyObject *PyFile_FromFile(FILE *fp, char *file_name, char *mode, int close_on_del)}\\
473 Return a new file object for an already opened standard C
474 file pointer, \code{fp}. A file name, \code{file_name}, and open mode,
475 \code{mode}, must be provided as well as a flag, \code{close_on_del}, that
476 indicates whether the file is to be closed when the file
477 object is destroyed. On failure, return -1.
478
479 \code{PyObject *PyFloat_FromDouble(double v)}\\
480 Returns a new float object with the value \code{v} on success, and
481 NULL on failure.
482
483 \code{PyObject *PyInt_FromLong(long v)}\\
484 Returns a new int object with the value \code{v} on success, and
485 NULL on failure.
486
487 \code{PyObject *PyList_New(int l)}\\
488 Returns a new list of length \code{l} on success, and NULL on
489 failure.
490
491 \code{PyObject *PyLong_FromLong(long v)}\\
492 Returns a new long object with the value \code{v} on success, and
493 NULL on failure.
494
495 \code{PyObject *PyLong_FromDouble(double v)}\\
496 Returns a new long object with the value \code{v} on success, and
497 NULL on failure.
498
499 \code{PyObject *PyDict_New()}\\
500 Returns a new empty dictionary on success, and NULL on
501 failure.
502
503 \code{PyObject *PyString_FromString(char *v)}\\
504 Returns a new string object with the value \code{v} on success, and
505 NULL on failure.
506
507 \code{PyObject *PyString_FromStringAndSize(char *v, int l)}\\
508 Returns a new string object with the value \code{v} and length \code{l}
509 on success, and NULL on failure.
510
511 \code{PyObject *PyTuple_New(int l)}\\
512 Returns a new tuple of length \code{l} on success, and NULL on
513 failure.
514