blob: 6c43933d54f3558e2a7eab513fa3b728a9b110d4 [file] [log] [blame]
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001\documentstyle[twoside,11pt,myformat]{report}
2
3% NOTE: this file controls which chapters/sections of the library
4% manual are actually printed. It is easy to customize your manual
5% by commenting out sections that you're not interested in.
6
7\title{Python-C API Reference}
8
9\input{boilerplate}
10
11\makeindex % tell \index to actually write the .idx file
12
13
14\begin{document}
15
16\pagenumbering{roman}
17
18\maketitle
19
20\input{copyright}
21
22\begin{abstract}
23
24\noindent
25This manual documents the API used by C (or C++) programmers who want
26to write extension modules or embed Python. It is a companion to
27``Extending and Embedding the Python Interpreter'', which describes
28the general principles of extension writing but does not document the
29API functions in detail.
30
31\end{abstract}
32
33\pagebreak
34
35{
36\parskip = 0mm
37\tableofcontents
38}
39
40\pagebreak
41
42\pagenumbering{arabic}
43
44
45\chapter{Introduction}
46
47From the viewpoint of of C access to Python services, we have:
48
49\begin{enumerate}
50
51\item "Very high level layer": two or three functions that let you
52exec or eval arbitrary Python code given as a string in a module whose
53name is given, passing C values in and getting C values out using
54mkvalue/getargs style format strings. This does not require the user
55to declare any variables of type \code{PyObject *}. This should be
56enough to write a simple application that gets Python code from the
57user, execs it, and returns the output or errors.
58
59\item "Abstract objects layer": which is the subject of this chapter.
60It has many functions operating on objects, and lest you do many
61things from C that you can also write in Python, without going through
62the Python parser.
63
64\item "Concrete objects layer": This is the public type-dependent
65interface provided by the standard built-in types, such as floats,
66strings, and lists. This interface exists and is currently documented
67by the collection of include files provides with the Python
68distributions.
69
70\begin{enumerate}
71
72From the point of view of Python accessing services provided by C
73modules:
74
75\end{enumerate}
76
77\item[4] "Python module interface": this interface consist of the basic
78routines used to define modules and their members. Most of the
79current extensions-writing guide deals with this interface.
80
81\item[5] "Built-in object interface": this is the interface that a new
82built-in type must provide and the mechanisms and rules that a
83developer of a new built-in type must use and follow.
84
85\end{enumerate}
86
87The Python C API provides four groups of operations on objects,
88corresponding to the same operations in the Python language: object,
89numeric, sequence, and mapping. Each protocol consists of a
90collection of related operations. If an operation that is not
91provided by a particular type is invoked, then the standard exception
92\code{TypeError} is raised with a operation name as an argument.
93
94In addition, for convenience this interface defines a set of
95constructors for building objects of built-in types. This is needed
96so new objects can be returned from C functions that otherwise treat
97objects generically.
98
99\section{Reference Counting}
100
101For most of the functions in the Python-C API, if a function retains a
102reference to a Python object passed as an argument, then the function
103will increase the reference count of the object. It is unnecessary
104for the caller to increase the reference count of an argument in
105anticipation of the object's retention.
106
107Usually, Python objects returned from functions should be treated as
108new objects. Functions that return objects assume that the caller
109will retain a reference and the reference count of the object has
110already been incremented to account for this fact. A caller that does
111not retain a reference to an object that is returned from a function
112must decrement the reference count of the object (using
113\code{Py_DECREF()}) to prevent memory leaks.
114
115Exceptions to these rules will be noted with the individual functions.
116
117\section{Include Files}
118
119All function, type and macro definitions needed to use the Python-C
120API are included in your code by the following line:
121
122\code{\#include "Python.h"}
123
124This implies inclusion of the following standard header files:
125stdio.h, string.h, errno.h, and stdlib.h (if available).
126
127All user visible names defined by Python.h (except those defined by
128the included standard headers) have one of the prefixes \code{Py} or
129\code{_Py}. Names beginning with \code{_Py} are for internal use
130only.
131
132
133\chapter{Initialization and Shutdown of an Embedded Python Interpreter}
134
135When embedding the Python interpreter in a C or C++ program, the
136interpreter must be initialized.
137
138\begin{cfuncdesc}{void}{PyInitialize}{}
139This function initializes the interpreter. It must be called before
140any interaction with the interpreter takes place. If it is called
141more than once, the second and further calls have no effect.
142
143The function performs the following tasks: create an environment in
144which modules can be imported and Python code can be executed;
145initialize the \code{__builtin__} module; initialize the \code{sys}
146module; initialize \code{sys.path}; initialize signal handling; and
147create the empty \code{__main__} module.
148
149In the current system, there is no way to undo all these
150initializations or to create additional interpreter environments.
151\end{cfuncdesc}
152
153\begin{cfuncdesc}{int}{Py_AtExit}{void (*func) ()}
154Register a cleanup function to be called when Python exits. The
155cleanup function will be called with no arguments and should return no
156value. At most 32 cleanup functions can be registered. When the
157registration is successful, \code{Py_AtExit} returns 0; on failure, it
158returns -1. Each cleanup function will be called t most once. The
159cleanup function registered last is called first.
160\end{cfuncdesc}
161
162\begin{cfuncdesc}{void}{Py_Exit}{int status}
163Exit the current process. This calls \code{Py_Cleanup()} (see next
164item) and performs additional cleanup (under some circumstances it
165will attempt to delete all modules), and then calls the standard C
166library function \code{exit(status)}.
167\end{cfuncdesc}
168
169\begin{cfuncdesc}{void}{Py_Cleanup}{}
170Perform some of the cleanup that \code{Py_Exit} performs, but don't
171exit the process. In particular, this invokes the user's
172\code{sys.exitfunc} function (if defined at all), and it invokes the
173cleanup functions registered with \code{Py_AtExit()}, in reverse order
174of their registration.
175\end{cfuncdesc}
176
177\begin{cfuncdesc}{void}{Py_FatalError}{char *message}
178Print a fatal error message and die. No cleanup is performed. This
179function should only be invoked when a condition is detected that
180would make it dangerous to continue using the Python interpreter;
181e.g., when the object administration appears to be corrupted.
182\end{cfuncdesc}
183
184\begin{cfuncdesc}{void}{PyImport_Init}{}
185Initialize the module table. For internal use only.
186\end{cfuncdesc}
187
188\begin{cfuncdesc}{void}{PyImport_Cleanup}{}
189Empty the module table. For internal use only.
190\end{cfuncdesc}
191
192\begin{cfuncdesc}{void}{PyBuiltin_Init}{}
193Initialize the \code{__builtin__} module. For internal use only.
194\end{cfuncdesc}
195
196
197\chapter{Reference Counting}
198
199The functions in this chapter are used for managing reference counts
200of Python objects.
201
202\begin{cfuncdesc}{void}{Py_INCREF}{PyObject *o}
203Increment the reference count for object \code{o}. The object must
204not be \NULL{}; if you aren't sure that it isn't \NULL{}, use
205\code{Py_XINCREF()}.
206\end{cfuncdesc}
207
208\begin{cfuncdesc}{void}{Py_XINCREF}{PyObject *o}
209Increment the reference count for object \code{o}. The object may be
210\NULL{}, in which case the function has no effect.
211\end{cfuncdesc}
212
213\begin{cfuncdesc}{void}{Py_DECREF}{PyObject *o}
214Decrement the reference count for object \code{o}. The object must
215not be \NULL{}; if you aren't sure that it isn't \NULL{}, use
216\code{Py_XDECREF()}. If the reference count reaches zero, the object's
217type's deallocation function (which must not be \NULL{}) is invoked.
218
219\strong{Warning:} The deallocation function can cause arbitrary Python
220code to be invoked (e.g. when a class instance with a \code{__del__()}
221method is deallocated). While exceptions in such code are not
222propagated, the executed code has free access to all Python global
223variables. This means that any object that is reachable from a global
224variable should be in a consistent state before \code{Py_DECREF()} is
225invoked. For example, code to delete an object from a list should
226copy a reference to the deleted object in a temporary variable, update
227the list data structure, and then call \code{Py_DECREF()} for the
228temporary variable.
229\end{cfuncdesc}
230
231\begin{cfuncdesc}{void}{Py_XDECREF}{PyObject *o}
232Decrement the reference count for object \code{o}.The object may be
233\NULL{}, in which case the function has no effect; otherwise the
234effect is the same as for \code{Py_DECREF()}, and the same warning
235applies.
236\end{cfuncdesc}
237
238
239\chapter{Exception Handling}
240
241The functions in this chapter will let you handle and raise Python
242exceptions.
243
244\begin{cfuncdesc}{void}{PyErr_Print}{}
245\end{cfuncdesc}
246
247
248\chapter{Utilities}
249
250The functions in this chapter perform various utility tasks, such as
251parsing function arguments and constructing Python values from C
252values.
253
254\begin{cfuncdesc}{int}{Py_FdIsInteractive}{FILE *fp, char *filename}
255Return true (nonzero) if the standard I/O file \code{fp} with name
256\code{filename} is deemed interactive. This is the case for files for
257which \code{isatty(fileno(fp))} is true. If the global flag
258\code{Py_InteractiveFlag} is true, this function also returns true if
259the \code{name} pointer is \NULL{} or if the name is equal to one of
260the strings \code{"<stdin>"} or \code{"???"}.
261\end{cfuncdesc}
262
263\begin{cfuncdesc}{long}{PyOS_GetLastModificationTime}{char *filename}
264Return the time of last modification of the file \code{filename}.
265The result is encoded in the same way as the timestamp returned by
266the standard C library function \code{time()}.
267\end{cfuncdesc}
268
269
270\chapter{Debugging}
271
272XXX Explain Py_DEBUG, Py_TRACE_REFS, Py_REF_DEBUG.
273
274
275\chapter{The Very High Level Layer}
276
277The functions in this chapter will let you execute Python source code
278given in a file or a buffer, but they will not let you interact in a
279more detailed way with the interpreter.
280
281
282\chapter{Abstract Objects Layer}
283
284The functions in this chapter interact with Python objects regardless
285of their type, or with wide classes of object types (e.g. all
286numerical types, or all sequence types). When used on object types
287for which they do not apply, they will flag a Python exception.
288
289\section{Object Protocol}
290
291\begin{cfuncdesc}{int}{PyObject_Print}{PyObject *o, FILE *fp, int flags}
292Print an object \code{o}, on file \code{fp}. Returns -1 on error
293The flags argument is used to enable certain printing
294options. The only option currently supported is \code{Py_Print_RAW}.
295\end{cfuncdesc}
296
297\begin{cfuncdesc}{int}{PyObject_HasAttrString}{PyObject *o, char *attr_name}
298Returns 1 if o has the attribute attr_name, and 0 otherwise.
299This is equivalent to the Python expression:
300\code{hasattr(o,attr_name)}.
301This function always succeeds.
302\end{cfuncdesc}
303
304\begin{cfuncdesc}{PyObject*}{PyObject_GetAttrString}{PyObject *o, char *attr_name}
305Retrieve an attributed named attr_name form object o.
306Returns the attribute value on success, or \NULL{} on failure.
307This is the equivalent of the Python expression: \code{o.attr_name}.
308\end{cfuncdesc}
309
310
311\begin{cfuncdesc}{int}{PyObject_HasAttr}{PyObject *o, PyObject *attr_name}
312Returns 1 if o has the attribute attr_name, and 0 otherwise.
313This is equivalent to the Python expression:
314\code{hasattr(o,attr_name)}.
315This function always succeeds.
316\end{cfuncdesc}
317
318
319\begin{cfuncdesc}{PyObject*}{PyObject_GetAttr}{PyObject *o, PyObject *attr_name}
320Retrieve an attributed named attr_name form object o.
321Returns the attribute value on success, or \NULL{} on failure.
322This is the equivalent of the Python expression: o.attr_name.
323\end{cfuncdesc}
324
325
326\begin{cfuncdesc}{int}{PyObject_SetAttrString}{PyObject *o, char *attr_name, PyObject *v}
327Set the value of the attribute named \code{attr_name}, for object \code{o},
328to the value \code{v}. Returns -1 on failure. This is
329the equivalent of the Python statement: \code{o.attr_name=v}.
330\end{cfuncdesc}
331
332
333\begin{cfuncdesc}{int}{PyObject_SetAttr}{PyObject *o, PyObject *attr_name, PyObject *v}
334Set the value of the attribute named \code{attr_name}, for
335object \code{o},
336to the value \code{v}. Returns -1 on failure. This is
337the equivalent of the Python statement: \code{o.attr_name=v}.
338\end{cfuncdesc}
339
340
341\begin{cfuncdesc}{int}{PyObject_DelAttrString}{PyObject *o, char *attr_name}
342Delete attribute named \code{attr_name}, for object \code{o}. Returns -1 on
343failure. This is the equivalent of the Python
344statement: \code{del o.attr_name}.
345\end{cfuncdesc}
346
347
348\begin{cfuncdesc}{int}{PyObject_DelAttr}{PyObject *o, PyObject *attr_name}
349Delete attribute named \code{attr_name}, for object \code{o}. Returns -1 on
350failure. This is the equivalent of the Python
351statement: \code{del o.attr_name}.
352\end{cfuncdesc}
353
354
355\begin{cfuncdesc}{int}{PyObject_Cmp}{PyObject *o1, PyObject *o2, int *result}
356Compare the values of \code{o1} and \code{o2} using a routine provided by
357\code{o1}, if one exists, otherwise with a routine provided by \code{o2}.
358The result of the comparison is returned in \code{result}. Returns
359-1 on failure. This is the equivalent of the Python
360statement: \code{result=cmp(o1,o2)}.
361\end{cfuncdesc}
362
363
364\begin{cfuncdesc}{int}{PyObject_Compare}{PyObject *o1, PyObject *o2}
365Compare the values of \code{o1} and \code{o2} using a routine provided by
366\code{o1}, if one exists, otherwise with a routine provided by \code{o2}.
367Returns the result of the comparison on success. On error,
368the value returned is undefined. This is equivalent to the
369Python expression: \code{cmp(o1,o2)}.
370\end{cfuncdesc}
371
372
373\begin{cfuncdesc}{PyObject*}{PyObject_Repr}{PyObject *o}
374Compute the string representation of object, \code{o}. Returns the
375string representation on success, \NULL{} on failure. This is
376the equivalent of the Python expression: \code{repr(o)}.
377Called by the \code{repr()} built-in function and by reverse quotes.
378\end{cfuncdesc}
379
380
381\begin{cfuncdesc}{PyObject*}{PyObject_Str}{PyObject *o}
382Compute the string representation of object, \code{o}. Returns the
383string representation on success, \NULL{} on failure. This is
384the equivalent of the Python expression: \code{str(o)}.
385Called by the \code{str()} built-in function and by the \code{print}
386statement.
387\end{cfuncdesc}
388
389
390\begin{cfuncdesc}{int}{PyCallable_Check}{PyObject *o}
391Determine if the object \code{o}, is callable. Return 1 if the
392object is callable and 0 otherwise.
393This function always succeeds.
394\end{cfuncdesc}
395
396
397\begin{cfuncdesc}{PyObject*}{PyObject_CallObject}{PyObject *callable_object, PyObject *args}
398Call a callable Python object \code{callable_object}, with
399arguments given by the tuple \code{args}. If no arguments are
400needed, then args may be \NULL{}. Returns the result of the
401call on success, or \NULL{} on failure. This is the equivalent
402of the Python expression: \code{apply(o, args)}.
403\end{cfuncdesc}
404
405\begin{cfuncdesc}{PyObject*}{PyObject_CallFunction}{PyObject *callable_object, char *format, ...}
406Call a callable Python object \code{callable_object}, with a
407variable number of C arguments. The C arguments are described
408using a mkvalue-style format string. The format may be \NULL{},
409indicating that no arguments are provided. Returns the
410result of the call on success, or \NULL{} on failure. This is
411the equivalent of the Python expression: \code{apply(o,args)}.
412\end{cfuncdesc}
413
414
415\begin{cfuncdesc}{PyObject*}{PyObject_CallMethod}{PyObject *o, char *m, char *format, ...}
416Call the method named \code{m} of object \code{o} with a variable number of
417C arguments. The C arguments are described by a mkvalue
418format string. The format may be \NULL{}, indicating that no
419arguments are provided. Returns the result of the call on
420success, or \NULL{} on failure. This is the equivalent of the
421Python expression: \code{o.method(args)}.
422Note that Special method names, such as "\code{__add__}",
423"\code{__getitem__}", and so on are not supported. The specific
424abstract-object routines for these must be used.
425\end{cfuncdesc}
426
427
428\begin{cfuncdesc}{int}{PyObject_Hash}{PyObject *o}
429Compute and return the hash value of an object \code{o}. On
430failure, return -1. This is the equivalent of the Python
431expression: \code{hash(o)}.
432\end{cfuncdesc}
433
434
435\begin{cfuncdesc}{int}{PyObject_IsTrue}{PyObject *o}
436Returns 1 if the object \code{o} is considered to be true, and
4370 otherwise. This is equivalent to the Python expression:
438\code{not not o}.
439This function always succeeds.
440\end{cfuncdesc}
441
442
443\begin{cfuncdesc}{PyObject*}{PyObject_Type}{PyObject *o}
444On success, returns a type object corresponding to the object
445type of object \code{o}. On failure, returns \NULL{}. This is
446equivalent to the Python expression: \code{type(o)}.
447\end{cfuncdesc}
448
449\begin{cfuncdesc}{int}{PyObject_Length}{PyObject *o}
450Return the length of object \code{o}. If the object \code{o} provides
451both sequence and mapping protocols, the sequence length is
452returned. On error, -1 is returned. This is the equivalent
453to the Python expression: \code{len(o)}.
454\end{cfuncdesc}
455
456
457\begin{cfuncdesc}{PyObject*}{PyObject_GetItem}{PyObject *o, PyObject *key}
458Return element of \code{o} corresponding to the object \code{key} or \NULL{}
459on failure. This is the equivalent of the Python expression:
460\code{o[key]}.
461\end{cfuncdesc}
462
463
464\begin{cfuncdesc}{int}{PyObject_SetItem}{PyObject *o, PyObject *key, PyObject *v}
465Map the object \code{key} to the value \code{v}.
466Returns -1 on failure. This is the equivalent
467of the Python statement: \code{o[key]=v}.
468\end{cfuncdesc}
469
470
471\begin{cfuncdesc}{int}{PyObject_DelItem}{PyObject *o, PyObject *key, PyObject *v}
472Delete the mapping for \code{key} from \code{*o}. Returns -1
473on failure.
474This is the equivalent of the Python statement: del o[key].
475\end{cfuncdesc}
476
477
478\section{Number Protocol}
479
480\begin{cfuncdesc}{int}{PyNumber_Check}{PyObject *o}
481Returns 1 if the object \code{o} provides numeric protocols, and
482false otherwise.
483This function always succeeds.
484\end{cfuncdesc}
485
486
487\begin{cfuncdesc}{PyObject*}{PyNumber_Add}{PyObject *o1, PyObject *o2}
488Returns the result of adding \code{o1} and \code{o2}, or null on failure.
489This is the equivalent of the Python expression: \code{o1+o2}.
490\end{cfuncdesc}
491
492
493\begin{cfuncdesc}{PyObject*}{PyNumber_Subtract}{PyObject *o1, PyObject *o2}
494Returns the result of subtracting \code{o2} from \code{o1}, or null on
495failure. This is the equivalent of the Python expression:
496\code{o1-o2}.
497\end{cfuncdesc}
498
499
500\begin{cfuncdesc}{PyObject*}{PyNumber_Multiply}{PyObject *o1, PyObject *o2}
501Returns the result of multiplying \code{o1} and \code{o2}, or null on
502failure. This is the equivalent of the Python expression:
503\code{o1*o2}.
504\end{cfuncdesc}
505
506
507\begin{cfuncdesc}{PyObject*}{PyNumber_Divide}{PyObject *o1, PyObject *o2}
508Returns the result of dividing \code{o1} by \code{o2}, or null on failure.
509This is the equivalent of the Python expression: \code{o1/o2}.
510\end{cfuncdesc}
511
512
513\begin{cfuncdesc}{PyObject*}{PyNumber_Remainder}{PyObject *o1, PyObject *o2}
514Returns the remainder of dividing \code{o1} by \code{o2}, or null on
515failure. This is the equivalent of the Python expression:
516\code{o1\%o2}.
517\end{cfuncdesc}
518
519
520\begin{cfuncdesc}{PyObject*}{PyNumber_Divmod}{PyObject *o1, PyObject *o2}
521See the built-in function divmod. Returns \NULL{} on failure.
522This is the equivalent of the Python expression:
523\code{divmod(o1,o2)}.
524\end{cfuncdesc}
525
526
527\begin{cfuncdesc}{PyObject*}{PyNumber_Power}{PyObject *o1, PyObject *o2, PyObject *o3}
528See the built-in function pow. Returns \NULL{} on failure.
529This is the equivalent of the Python expression:
530\code{pow(o1,o2,o3)}, where \code{o3} is optional.
531\end{cfuncdesc}
532
533
534\begin{cfuncdesc}{PyObject*}{PyNumber_Negative}{PyObject *o}
535Returns the negation of \code{o} on success, or null on failure.
536This is the equivalent of the Python expression: \code{-o}.
537\end{cfuncdesc}
538
539
540\begin{cfuncdesc}{PyObject*}{PyNumber_Positive}{PyObject *o}
541Returns \code{o} on success, or \NULL{} on failure.
542This is the equivalent of the Python expression: \code{+o}.
543\end{cfuncdesc}
544
545
546\begin{cfuncdesc}{PyObject*}{PyNumber_Absolute}{PyObject *o}
547Returns the absolute value of \code{o}, or null on failure. This is
548the equivalent of the Python expression: \code{abs(o)}.
549\end{cfuncdesc}
550
551
552\begin{cfuncdesc}{PyObject*}{PyNumber_Invert}{PyObject *o}
553Returns the bitwise negation of \code{o} on success, or \NULL{} on
554failure. This is the equivalent of the Python expression:
555\code{~o}.
556\end{cfuncdesc}
557
558
559\begin{cfuncdesc}{PyObject*}{PyNumber_Lshift}{PyObject *o1, PyObject *o2}
560Returns the result of left shifting \code{o1} by \code{o2} on success, or
561\NULL{} on failure. This is the equivalent of the Python
562expression: \code{o1 << o2}.
563\end{cfuncdesc}
564
565
566\begin{cfuncdesc}{PyObject*}{PyNumber_Rshift}{PyObject *o1, PyObject *o2}
567Returns the result of right shifting \code{o1} by \code{o2} on success, or
568\NULL{} on failure. This is the equivalent of the Python
569expression: \code{o1 >> o2}.
570\end{cfuncdesc}
571
572
573\begin{cfuncdesc}{PyObject*}{PyNumber_And}{PyObject *o1, PyObject *o2}
574Returns the result of "anding" \code{o2} and \code{o2} on success and \NULL{}
575on failure. This is the equivalent of the Python
576expression: \code{o1 and o2}.
577\end{cfuncdesc}
578
579
580\begin{cfuncdesc}{PyObject*}{PyNumber_Xor}{PyObject *o1, PyObject *o2}
581Returns the bitwise exclusive or of \code{o1} by \code{o2} on success, or
582\NULL{} on failure. This is the equivalent of the Python
583expression: \code{o1\^{ }o2}.
584\end{cfuncdesc}
585
586\begin{cfuncdesc}{PyObject*}{PyNumber_Or}{PyObject *o1, PyObject *o2}
587Returns the result or \code{o1} and \code{o2} on success, or \NULL{} on
588failure. This is the equivalent of the Python expression:
589\code{o1 or o2}.
590\end{cfuncdesc}
591
592
593\begin{cfuncdesc}{PyObject*}{PyNumber_Coerce}{PyObject *o1, PyObject *o2}
594This function takes the addresses of two variables of type
595\code{PyObject*}.
596
597If the objects pointed to by \code{*p1} and \code{*p2} have the same type,
598increment their reference count and return 0 (success).
599If the objects can be converted to a common numeric type,
600replace \code{*p1} and \code{*p2} by their converted value (with 'new'
601reference counts), and return 0.
602If no conversion is possible, or if some other error occurs,
603return -1 (failure) and don't increment the reference counts.
604The call \code{PyNumber_Coerce(\&o1, \&o2)} is equivalent to the Python
605statement \code{o1, o2 = coerce(o1, o2)}.
606\end{cfuncdesc}
607
608
609\begin{cfuncdesc}{PyObject*}{PyNumber_Int}{PyObject *o}
610Returns the \code{o} converted to an integer object on success, or
611\NULL{} on failure. This is the equivalent of the Python
612expression: \code{int(o)}.
613\end{cfuncdesc}
614
615
616\begin{cfuncdesc}{PyObject*}{PyNumber_Long}{PyObject *o}
617Returns the \code{o} converted to a long integer object on success,
618or \NULL{} on failure. This is the equivalent of the Python
619expression: \code{long(o)}.
620\end{cfuncdesc}
621
622
623\begin{cfuncdesc}{PyObject*}{PyNumber_Float}{PyObject *o}
624Returns the \code{o} converted to a float object on success, or \NULL{}
625on failure. This is the equivalent of the Python expression:
626\code{float(o)}.
627\end{cfuncdesc}
628
629
630\section{Sequence protocol}
631
632\begin{cfuncdesc}{int}{PySequence_Check}{PyObject *o}
633Return 1 if the object provides sequence protocol, and 0
634otherwise.
635This function always succeeds.
636\end{cfuncdesc}
637
638
639\begin{cfuncdesc}{PyObject*}{PySequence_Concat}{PyObject *o1, PyObject *o2}
640Return the concatination of \code{o1} and \code{o2} on success, and \NULL{} on
641failure. This is the equivalent of the Python
642expression: \code{o1+o2}.
643\end{cfuncdesc}
644
645
646\begin{cfuncdesc}{PyObject*}{PySequence_Repeat}{PyObject *o, int count}
647Return the result of repeating sequence object \code{o} count times,
648or \NULL{} on failure. This is the equivalent of the Python
649expression: \code{o*count}.
650\end{cfuncdesc}
651
652
653\begin{cfuncdesc}{PyObject*}{PySequence_GetItem}{PyObject *o, int i}
654Return the ith element of \code{o}, or \NULL{} on failure. This is the
655equivalent of the Python expression: \code{o[i]}.
656\end{cfuncdesc}
657
658
659\begin{cfuncdesc}{PyObject*}{PySequence_GetSlice}{PyObject *o, int i1, int i2}
660Return the slice of sequence object \code{o} between \code{i1} and \code{i2}, or
661\NULL{} on failure. This is the equivalent of the Python
662expression, \code{o[i1:i2]}.
663\end{cfuncdesc}
664
665
666\begin{cfuncdesc}{int}{PySequence_SetItem}{PyObject *o, int i, PyObject *v}
667Assign object \code{v} to the \code{i}th element of \code{o}.
668Returns -1 on failure. This is the equivalent of the Python
669statement, \code{o[i]=v}.
670\end{cfuncdesc}
671
672\begin{cfuncdesc}{int}{PySequence_DelItem}{PyObject *o, int i}
673Delete the \code{i}th element of object \code{v}. Returns
674-1 on failure. This is the equivalent of the Python
675statement: \code{del o[i]}.
676\end{cfuncdesc}
677
678\begin{cfuncdesc}{int}{PySequence_SetSlice}{PyObject *o, int i1, int i2, PyObject *v}
679Assign the sequence object \code{v} to the slice in sequence
680object \code{o} from \code{i1} to \code{i2}. This is the equivalent of the Python
681statement, \code{o[i1:i2]=v}.
682\end{cfuncdesc}
683
684\begin{cfuncdesc}{int}{PySequence_DelSlice}{PyObject *o, int i1, int i2}
685Delete the slice in sequence object, \code{o}, from \code{i1} to \code{i2}.
686Returns -1 on failure. This is the equivalent of the Python
687statement: \code{del o[i1:i2]}.
688\end{cfuncdesc}
689
690\begin{cfuncdesc}{PyObject*}{PySequence_Tuple}{PyObject *o}
691Returns the \code{o} as a tuple on success, and \NULL{} on failure.
692This is equivalent to the Python expression: \code{tuple(o)}.
693\end{cfuncdesc}
694
695\begin{cfuncdesc}{int}{PySequence_Count}{PyObject *o, PyObject *value}
696Return the number of occurrences of \code{value} on \code{o}, that is,
697return the number of keys for which \code{o[key]==value}. On
698failure, return -1. This is equivalent to the Python
699expression: \code{o.count(value)}.
700\end{cfuncdesc}
701
702\begin{cfuncdesc}{int}{PySequence_In}{PyObject *o, PyObject *value}
703Determine if \code{o} contains \code{value}. If an item in \code{o} is equal to
704\code{value}, return 1, otherwise return 0. On error, return -1. This
705is equivalent to the Python expression: \code{value in o}.
706\end{cfuncdesc}
707
708\begin{cfuncdesc}{int}{PySequence_Index}{PyObject *o, PyObject *value}
709Return the first index for which \code{o[i]=value}. On error,
710return -1. This is equivalent to the Python
711expression: \code{o.index(value)}.
712\end{cfuncdesc}
713
714\section{Mapping protocol}
715
716\begin{cfuncdesc}{int}{PyMapping_Check}{PyObject *o}
717Return 1 if the object provides mapping protocol, and 0
718otherwise.
719This function always succeeds.
720\end{cfuncdesc}
721
722
723\begin{cfuncdesc}{int}{PyMapping_Length}{PyObject *o}
724Returns the number of keys in object \code{o} on success, and -1 on
725failure. For objects that do not provide sequence protocol,
726this is equivalent to the Python expression: \code{len(o)}.
727\end{cfuncdesc}
728
729
730\begin{cfuncdesc}{int}{PyMapping_DelItemString}{PyObject *o, char *key}
731Remove the mapping for object \code{key} from the object \code{o}.
732Return -1 on failure. This is equivalent to
733the Python statement: \code{del o[key]}.
734\end{cfuncdesc}
735
736
737\begin{cfuncdesc}{int}{PyMapping_DelItem}{PyObject *o, PyObject *key}
738Remove the mapping for object \code{key} from the object \code{o}.
739Return -1 on failure. This is equivalent to
740the Python statement: \code{del o[key]}.
741\end{cfuncdesc}
742
743
744\begin{cfuncdesc}{int}{PyMapping_HasKeyString}{PyObject *o, char *key}
745On success, return 1 if the mapping object has the key \code{key}
746and 0 otherwise. This is equivalent to the Python expression:
747\code{o.has_key(key)}.
748This function always succeeds.
749\end{cfuncdesc}
750
751
752\begin{cfuncdesc}{int}{PyMapping_HasKey}{PyObject *o, PyObject *key}
753Return 1 if the mapping object has the key \code{key}
754and 0 otherwise. This is equivalent to the Python expression:
755\code{o.has_key(key)}.
756This function always succeeds.
757\end{cfuncdesc}
758
759
760\begin{cfuncdesc}{PyObject*}{PyMapping_Keys}{PyObject *o}
761On success, return a list of the keys in object \code{o}. On
762failure, return \NULL{}. This is equivalent to the Python
763expression: \code{o.keys()}.
764\end{cfuncdesc}
765
766
767\begin{cfuncdesc}{PyObject*}{PyMapping_Values}{PyObject *o}
768On success, return a list of the values in object \code{o}. On
769failure, return \NULL{}. This is equivalent to the Python
770expression: \code{o.values()}.
771\end{cfuncdesc}
772
773
774\begin{cfuncdesc}{PyObject*}{PyMapping_Items}{PyObject *o}
775On success, return a list of the items in object \code{o}, where
776each item is a tuple containing a key-value pair. On
777failure, return \NULL{}. This is equivalent to the Python
778expression: \code{o.items()}.
779\end{cfuncdesc}
780
781\begin{cfuncdesc}{int}{PyMapping_Clear}{PyObject *o}
782Make object \code{o} empty. Returns 1 on success and 0 on failure.
783This is equivalent to the Python statement:
784\code{for key in o.keys(): del o[key]}
785\end{cfuncdesc}
786
787
788\begin{cfuncdesc}{PyObject*}{PyMapping_GetItemString}{PyObject *o, char *key}
789Return element of \code{o} corresponding to the object \code{key} or \NULL{}
790on failure. This is the equivalent of the Python expression:
791\code{o[key]}.
792\end{cfuncdesc}
793
794\begin{cfuncdesc}{PyObject*}{PyMapping_SetItemString}{PyObject *o, char *key, PyObject *v}
795Map the object \code{key} to the value \code{v} in object \code{o}. Returns
796-1 on failure. This is the equivalent of the Python
797statement: \code{o[key]=v}.
798\end{cfuncdesc}
799
800
801\section{Constructors}
802
803\begin{cfuncdesc}{PyObject*}{PyFile_FromString}{char *file_name, char *mode}
804On success, returns a new file object that is opened on the
805file given by \code{file_name}, with a file mode given by \code{mode},
806where \code{mode} has the same semantics as the standard C routine,
807fopen. On failure, return -1.
808\end{cfuncdesc}
809
810\begin{cfuncdesc}{PyObject*}{PyFile_FromFile}{FILE *fp, char *file_name, char *mode, int close_on_del}
811Return a new file object for an already opened standard C
812file pointer, \code{fp}. A file name, \code{file_name}, and open mode,
813\code{mode}, must be provided as well as a flag, \code{close_on_del}, that
814indicates whether the file is to be closed when the file
815object is destroyed. On failure, return -1.
816\end{cfuncdesc}
817
818\begin{cfuncdesc}{PyObject*}{PyFloat_FromDouble}{double v}
819Returns a new float object with the value \code{v} on success, and
820\NULL{} on failure.
821\end{cfuncdesc}
822
823\begin{cfuncdesc}{PyObject*}{PyInt_FromLong}{long v}
824Returns a new int object with the value \code{v} on success, and
825\NULL{} on failure.
826\end{cfuncdesc}
827
828\begin{cfuncdesc}{PyObject*}{PyList_New}{int l}
829Returns a new list of length \code{l} on success, and \NULL{} on
830failure.
831\end{cfuncdesc}
832
833\begin{cfuncdesc}{PyObject*}{PyLong_FromLong}{long v}
834Returns a new long object with the value \code{v} on success, and
835\NULL{} on failure.
836\end{cfuncdesc}
837
838\begin{cfuncdesc}{PyObject*}{PyLong_FromDouble}{double v}
839Returns a new long object with the value \code{v} on success, and
840\NULL{} on failure.
841\end{cfuncdesc}
842
843\begin{cfuncdesc}{PyObject*}{PyDict_New}{}
844Returns a new empty dictionary on success, and \NULL{} on
845failure.
846\end{cfuncdesc}
847
848\begin{cfuncdesc}{PyObject*}{PyString_FromString}{char *v}
849Returns a new string object with the value \code{v} on success, and
850\NULL{} on failure.
851\end{cfuncdesc}
852
853\begin{cfuncdesc}{PyObject*}{PyString_FromStringAndSize}{char *v, int l}
854Returns a new string object with the value \code{v} and length \code{l}
855on success, and \NULL{} on failure.
856\end{cfuncdesc}
857
858\begin{cfuncdesc}{PyObject*}{PyTuple_New}{int l}
859Returns a new tuple of length \code{l} on success, and \NULL{} on
860failure.
861\end{cfuncdesc}
862
863
864\chapter{Concrete Objects Layer}
865
866The functions in this chapter are specific to certain Python object
867types. Passing them an object of the wrong type is not a good idea;
868if you receive an object from a Python program and you are not sure
869that it has the right type, you must perform a type check first;
870e.g. to check that an object is a dictionary, use
871\code{PyDict_Check()}.
872
873
874\chapter{Defining New Object Types}
875
876\begin{cfuncdesc}{PyObject *}{_PyObject_New}{PyTypeObject *type}
877\end{cfuncdesc}
878
879\begin{cfuncdesc}{PyObject *}{_PyObject_New}{PyTypeObject *type}
880\end{cfuncdesc}
881
882\input{api.ind} % Index -- must be last
883
884\end{document}