blob: bc067af61d5643dbae8b015df9c6afa79dcd373b [file] [log] [blame]
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001\documentstyle[twoside,11pt,myformat]{report}
2
Guido van Rossum9231c8f1997-05-15 21:43:21 +00003\title{Python-C API Reference}
4
5\input{boilerplate}
6
7\makeindex % tell \index to actually write the .idx file
8
9
10\begin{document}
11
12\pagenumbering{roman}
13
14\maketitle
15
16\input{copyright}
17
18\begin{abstract}
19
20\noindent
21This manual documents the API used by C (or C++) programmers who want
22to write extension modules or embed Python. It is a companion to
23``Extending and Embedding the Python Interpreter'', which describes
24the general principles of extension writing but does not document the
25API functions in detail.
26
27\end{abstract}
28
29\pagebreak
30
31{
32\parskip = 0mm
33\tableofcontents
34}
35
36\pagebreak
37
38\pagenumbering{arabic}
39
40
41\chapter{Introduction}
42
Guido van Rossumae110af1997-05-22 20:11:52 +000043(XXX This is the old introduction, mostly by Jim Fulton -- should be
44rewritten.)
45
Guido van Rossum9231c8f1997-05-15 21:43:21 +000046From the viewpoint of of C access to Python services, we have:
47
48\begin{enumerate}
49
50\item "Very high level layer": two or three functions that let you
51exec or eval arbitrary Python code given as a string in a module whose
52name is given, passing C values in and getting C values out using
53mkvalue/getargs style format strings. This does not require the user
54to declare any variables of type \code{PyObject *}. This should be
55enough to write a simple application that gets Python code from the
56user, execs it, and returns the output or errors.
57
58\item "Abstract objects layer": which is the subject of this chapter.
59It has many functions operating on objects, and lest you do many
60things from C that you can also write in Python, without going through
61the Python parser.
62
63\item "Concrete objects layer": This is the public type-dependent
64interface provided by the standard built-in types, such as floats,
65strings, and lists. This interface exists and is currently documented
66by the collection of include files provides with the Python
67distributions.
68
Guido van Rossumae110af1997-05-22 20:11:52 +000069\end{enumerate}
Guido van Rossum9231c8f1997-05-15 21:43:21 +000070
71From the point of view of Python accessing services provided by C
72modules:
73
Guido van Rossumae110af1997-05-22 20:11:52 +000074\begin{enumerate}
Guido van Rossum9231c8f1997-05-15 21:43:21 +000075
Guido van Rossumae110af1997-05-22 20:11:52 +000076\item[4.] "Python module interface": this interface consist of the basic
Guido van Rossum9231c8f1997-05-15 21:43:21 +000077routines used to define modules and their members. Most of the
78current extensions-writing guide deals with this interface.
79
Guido van Rossumae110af1997-05-22 20:11:52 +000080\item[5.] "Built-in object interface": this is the interface that a new
Guido van Rossum9231c8f1997-05-15 21:43:21 +000081built-in type must provide and the mechanisms and rules that a
82developer of a new built-in type must use and follow.
83
84\end{enumerate}
85
86The Python C API provides four groups of operations on objects,
87corresponding to the same operations in the Python language: object,
88numeric, sequence, and mapping. Each protocol consists of a
89collection of related operations. If an operation that is not
90provided by a particular type is invoked, then the standard exception
91\code{TypeError} is raised with a operation name as an argument.
92
93In addition, for convenience this interface defines a set of
94constructors for building objects of built-in types. This is needed
95so new objects can be returned from C functions that otherwise treat
96objects generically.
97
98\section{Reference Counting}
99
100For most of the functions in the Python-C API, if a function retains a
101reference to a Python object passed as an argument, then the function
102will increase the reference count of the object. It is unnecessary
103for the caller to increase the reference count of an argument in
104anticipation of the object's retention.
105
106Usually, Python objects returned from functions should be treated as
107new objects. Functions that return objects assume that the caller
108will retain a reference and the reference count of the object has
109already been incremented to account for this fact. A caller that does
110not retain a reference to an object that is returned from a function
111must decrement the reference count of the object (using
112\code{Py_DECREF()}) to prevent memory leaks.
113
114Exceptions to these rules will be noted with the individual functions.
115
116\section{Include Files}
117
118All function, type and macro definitions needed to use the Python-C
119API are included in your code by the following line:
120
121\code{\#include "Python.h"}
122
123This implies inclusion of the following standard header files:
124stdio.h, string.h, errno.h, and stdlib.h (if available).
125
126All user visible names defined by Python.h (except those defined by
127the included standard headers) have one of the prefixes \code{Py} or
128\code{_Py}. Names beginning with \code{_Py} are for internal use
129only.
130
131
132\chapter{Initialization and Shutdown of an Embedded Python Interpreter}
133
134When embedding the Python interpreter in a C or C++ program, the
135interpreter must be initialized.
136
137\begin{cfuncdesc}{void}{PyInitialize}{}
138This function initializes the interpreter. It must be called before
139any interaction with the interpreter takes place. If it is called
140more than once, the second and further calls have no effect.
141
142The function performs the following tasks: create an environment in
143which modules can be imported and Python code can be executed;
144initialize the \code{__builtin__} module; initialize the \code{sys}
145module; initialize \code{sys.path}; initialize signal handling; and
146create the empty \code{__main__} module.
147
148In the current system, there is no way to undo all these
149initializations or to create additional interpreter environments.
150\end{cfuncdesc}
151
152\begin{cfuncdesc}{int}{Py_AtExit}{void (*func) ()}
153Register a cleanup function to be called when Python exits. The
154cleanup function will be called with no arguments and should return no
155value. At most 32 cleanup functions can be registered. When the
156registration is successful, \code{Py_AtExit} returns 0; on failure, it
157returns -1. Each cleanup function will be called t most once. The
158cleanup function registered last is called first.
159\end{cfuncdesc}
160
161\begin{cfuncdesc}{void}{Py_Exit}{int status}
162Exit the current process. This calls \code{Py_Cleanup()} (see next
163item) and performs additional cleanup (under some circumstances it
164will attempt to delete all modules), and then calls the standard C
165library function \code{exit(status)}.
166\end{cfuncdesc}
167
168\begin{cfuncdesc}{void}{Py_Cleanup}{}
169Perform some of the cleanup that \code{Py_Exit} performs, but don't
170exit the process. In particular, this invokes the user's
171\code{sys.exitfunc} function (if defined at all), and it invokes the
172cleanup functions registered with \code{Py_AtExit()}, in reverse order
173of their registration.
174\end{cfuncdesc}
175
176\begin{cfuncdesc}{void}{Py_FatalError}{char *message}
177Print a fatal error message and die. No cleanup is performed. This
178function should only be invoked when a condition is detected that
179would make it dangerous to continue using the Python interpreter;
180e.g., when the object administration appears to be corrupted.
181\end{cfuncdesc}
182
183\begin{cfuncdesc}{void}{PyImport_Init}{}
184Initialize the module table. For internal use only.
185\end{cfuncdesc}
186
187\begin{cfuncdesc}{void}{PyImport_Cleanup}{}
188Empty the module table. For internal use only.
189\end{cfuncdesc}
190
191\begin{cfuncdesc}{void}{PyBuiltin_Init}{}
192Initialize the \code{__builtin__} module. For internal use only.
193\end{cfuncdesc}
194
Guido van Rossumae110af1997-05-22 20:11:52 +0000195XXX Other init functions: PyEval_InitThreads, PyOS_InitInterrupts,
196PyMarshal_Init, PySys_Init.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000197
198\chapter{Reference Counting}
199
200The functions in this chapter are used for managing reference counts
201of Python objects.
202
203\begin{cfuncdesc}{void}{Py_INCREF}{PyObject *o}
204Increment the reference count for object \code{o}. The object must
205not be \NULL{}; if you aren't sure that it isn't \NULL{}, use
206\code{Py_XINCREF()}.
207\end{cfuncdesc}
208
209\begin{cfuncdesc}{void}{Py_XINCREF}{PyObject *o}
210Increment the reference count for object \code{o}. The object may be
211\NULL{}, in which case the function has no effect.
212\end{cfuncdesc}
213
214\begin{cfuncdesc}{void}{Py_DECREF}{PyObject *o}
215Decrement the reference count for object \code{o}. The object must
216not be \NULL{}; if you aren't sure that it isn't \NULL{}, use
217\code{Py_XDECREF()}. If the reference count reaches zero, the object's
218type's deallocation function (which must not be \NULL{}) is invoked.
219
220\strong{Warning:} The deallocation function can cause arbitrary Python
221code to be invoked (e.g. when a class instance with a \code{__del__()}
222method is deallocated). While exceptions in such code are not
223propagated, the executed code has free access to all Python global
224variables. This means that any object that is reachable from a global
225variable should be in a consistent state before \code{Py_DECREF()} is
226invoked. For example, code to delete an object from a list should
227copy a reference to the deleted object in a temporary variable, update
228the list data structure, and then call \code{Py_DECREF()} for the
229temporary variable.
230\end{cfuncdesc}
231
232\begin{cfuncdesc}{void}{Py_XDECREF}{PyObject *o}
233Decrement the reference count for object \code{o}.The object may be
234\NULL{}, in which case the function has no effect; otherwise the
235effect is the same as for \code{Py_DECREF()}, and the same warning
236applies.
237\end{cfuncdesc}
238
Guido van Rossumae110af1997-05-22 20:11:52 +0000239The following functions are only for internal use:
240\code{_Py_Dealloc}, \code{_Py_ForgetReference}, \code{_Py_NewReference},
241as well as the global variable \code{_Py_RefTotal}.
242
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000243
244\chapter{Exception Handling}
245
246The functions in this chapter will let you handle and raise Python
Guido van Rossumae110af1997-05-22 20:11:52 +0000247exceptions. It is important to understand some of the basics of
248Python exception handling. It works somewhat like the Unix
249\code{errno} variable: there is a global indicator (per thread) of the
250last error that occurred. Most functions don't clear this on success,
251but will set it to indicate the cause of the error on failure. Most
252functions also return an error indicator, usually \NULL{} if they are
253supposed to return a pointer, or -1 if they return an integer
254(exception: the \code{PyArg_Parse*()} functions return 1 for success and
2550 for failure). When a function must fail because of some function it
256called failed, it generally doesn't set the error indicator; the
257function it called already set it.
258
259The error indicator consists of three Python objects corresponding to
260the Python variables \code{sys.exc_type}, \code{sys.exc_value} and
261\code{sys.exc_traceback}. API functions exist to interact with the
262error indicator in various ways. There is a separate error indicator
263for each thread.
264
265% XXX Order of these should be more thoughtful.
266% Either alphabetical or some kind of structure.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000267
268\begin{cfuncdesc}{void}{PyErr_Print}{}
Guido van Rossumae110af1997-05-22 20:11:52 +0000269Print a standard traceback to \code{sys.stderr} and clear the error
270indicator. Call this function only when the error indicator is set.
271(Otherwise it will cause a fatal error!)
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000272\end{cfuncdesc}
273
Guido van Rossumae110af1997-05-22 20:11:52 +0000274\begin{cfuncdesc}{PyObject *}{PyErr_Occurred}{}
275Test whether the error indicator is set. If set, return the exception
276\code{type} (the first argument to the last call to one of the
277\code{PyErr_Set*()} functions or to \code{PyErr_Restore()}). If not
278set, return \NULL{}. You do not own a reference to the return value,
279so you do not need to \code{Py_DECREF()} it.
280\end{cfuncdesc}
281
282\begin{cfuncdesc}{void}{PyErr_Clear}{}
283Clear the error indicator. If the error indicator is not set, there
284is no effect.
285\end{cfuncdesc}
286
287\begin{cfuncdesc}{void}{PyErr_Fetch}{PyObject **ptype, PyObject **pvalue, PyObject **ptraceback}
288Retrieve the error indicator into three variables whose addresses are
289passed. If the error indicator is not set, set all three variables to
290\NULL{}. If it is set, it will be cleared and you own a reference to
291each object retrieved. The value and traceback object may be \NULL{}
292even when the type object is not. \strong{Note:} this function is
293normally only used by code that needs to handle exceptions or by code
294that needs to save and restore the error indicator temporarily.
295\end{cfuncdesc}
296
297\begin{cfuncdesc}{void}{PyErr_Restore}{PyObject *type, PyObject *value, PyObject *traceback}
298Set the error indicator from the three objects. If the error
299indicator is already set, it is cleared first. If the objects are
300\NULL{}, the error indicator is cleared. Do not pass a \NULL{} type
301and non-\NULL{} value or traceback. The exception type should be a
302string or class; if it is a class, the value should be an instance of
303that class. Do not pass an invalid exception type or value.
304(Violating these rules will cause subtle problems later.) This call
305takes away a reference to each object, i.e. you must own a reference
306to each object before the call and after the call you no longer own
307these references. (If you don't understand this, don't use this
308function. I warned you.) \strong{Note:} this function is normally
309only used by code that needs to save and restore the error indicator
310temporarily.
311\end{cfuncdesc}
312
313\begin{cfuncdesc}{void}{PyErr_SetString}{PyObject *type, char *message}
314This is the most common way to set the error indicator. The first
315argument specifies the exception type; it is normally one of the
316standard exceptions, e.g. \code{PyExc_RuntimeError}. You need not
317increment its reference count. The second argument is an error
318message; it is converted to a string object.
319\end{cfuncdesc}
320
321\begin{cfuncdesc}{void}{PyErr_SetObject}{PyObject *type, PyObject *value}
322This function is similar to \code{PyErr_SetString()} but lets you
323specify an arbitrary Python object for the ``value'' of the exception.
324You need not increment its reference count.
325\end{cfuncdesc}
326
327\begin{cfuncdesc}{void}{PyErr_SetNone}{PyObject *type}
328This is a shorthand for \code{PyErr_SetString(\var{type}, Py_None}.
329\end{cfuncdesc}
330
331\begin{cfuncdesc}{int}{PyErr_BadArgument}{}
332This is a shorthand for \code{PyErr_SetString(PyExc_TypeError,
333\var{message})}, where \var{message} indicates that a built-in operation
334was invoked with an illegal argument. It is mostly for internal use.
335\end{cfuncdesc}
336
337\begin{cfuncdesc}{PyObject *}{PyErr_NoMemory}{}
338This is a shorthand for \code{PyErr_SetNone(PyExc_MemoryError)}; it
339returns \NULL{} so an object allocation function can write
340\code{return PyErr_NoMemory();} when it runs out of memory.
341\end{cfuncdesc}
342
343\begin{cfuncdesc}{PyObject *}{PyErr_SetFromErrno}{PyObject *type}
344This is a convenience function to raise an exception when a C library
345function has returned an error and set the C variable \code{errno}.
346It constructs a tuple object whose first item is the integer
347\code{errno} value and whose second item is the corresponding error
348message (gotten from \code{strerror()}), and then calls
349\code{PyErr_SetObject(\var{type}, \var{object})}. On \UNIX{}, when
350the \code{errno} value is \code{EINTR}, indicating an interrupted
351system call, this calls \code{PyErr_CheckSignals()}, and if that set
352the error indicator, leaves it set to that. The function always
353returns \NULL{}, so a wrapper function around a system call can write
354\code{return PyErr_NoMemory();} when the system call returns an error.
355\end{cfuncdesc}
356
357\begin{cfuncdesc}{void}{PyErr_BadInternalCall}{}
358This is a shorthand for \code{PyErr_SetString(PyExc_TypeError,
359\var{message})}, where \var{message} indicates that an internal
360operation (e.g. a Python-C API function) was invoked with an illegal
361argument. It is mostly for internal use.
362\end{cfuncdesc}
363
364\begin{cfuncdesc}{int}{PyErr_CheckSignals}{}
365This function interacts with Python's signal handling. It checks
366whether a signal has been sent to the processes and if so, invokes the
367corresponding signal handler. If the \code{signal} module is
368supported, this can invoke a signal handler written in Python. In all
369cases, the default effect for \code{SIGINT} is to raise the
370\code{KeyboadInterrupt} exception. If an exception is raised the
371error indicator is set and the function returns 1; otherwise the
372function returns 0. The error indicator may or may not be cleared if
373it was previously set.
374\end{cfuncdesc}
375
376\begin{cfuncdesc}{void}{PyErr_SetInterrupt}{}
377This function is obsolete (XXX or platform dependent?). It simulates
378the effect of a \code{SIGINT} signal arriving -- the next time
379\code{PyErr_CheckSignals()} is called, \code{KeyboadInterrupt} will be
380raised.
381\end{cfuncdesc}
382
383\section{Standard Exceptions}
384
385All standard Python exceptions are available as global variables whose
386names are \code{PyExc_} followed by the Python exception name.
387These have the type \code{PyObject *}; they are all string objects.
388For completion, here are all the variables:
389\code{PyExc_AccessError},
390\code{PyExc_AssertionError},
391\code{PyExc_AttributeError},
392\code{PyExc_EOFError},
393\code{PyExc_FloatingPointError},
394\code{PyExc_IOError},
395\code{PyExc_ImportError},
396\code{PyExc_IndexError},
397\code{PyExc_KeyError},
398\code{PyExc_KeyboardInterrupt},
399\code{PyExc_MemoryError},
400\code{PyExc_NameError},
401\code{PyExc_OverflowError},
402\code{PyExc_RuntimeError},
403\code{PyExc_SyntaxError},
404\code{PyExc_SystemError},
405\code{PyExc_SystemExit},
406\code{PyExc_TypeError},
407\code{PyExc_ValueError},
408\code{PyExc_ZeroDivisionError}.
409
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000410
411\chapter{Utilities}
412
413The functions in this chapter perform various utility tasks, such as
414parsing function arguments and constructing Python values from C
415values.
416
417\begin{cfuncdesc}{int}{Py_FdIsInteractive}{FILE *fp, char *filename}
418Return true (nonzero) if the standard I/O file \code{fp} with name
419\code{filename} is deemed interactive. This is the case for files for
420which \code{isatty(fileno(fp))} is true. If the global flag
421\code{Py_InteractiveFlag} is true, this function also returns true if
422the \code{name} pointer is \NULL{} or if the name is equal to one of
423the strings \code{"<stdin>"} or \code{"???"}.
424\end{cfuncdesc}
425
426\begin{cfuncdesc}{long}{PyOS_GetLastModificationTime}{char *filename}
427Return the time of last modification of the file \code{filename}.
428The result is encoded in the same way as the timestamp returned by
429the standard C library function \code{time()}.
430\end{cfuncdesc}
431
432
433\chapter{Debugging}
434
435XXX Explain Py_DEBUG, Py_TRACE_REFS, Py_REF_DEBUG.
436
437
438\chapter{The Very High Level Layer}
439
440The functions in this chapter will let you execute Python source code
441given in a file or a buffer, but they will not let you interact in a
442more detailed way with the interpreter.
443
Guido van Rossumae110af1997-05-22 20:11:52 +0000444\begin{cfuncdesc}{int}{PyRun_AnyFile}{FILE *, char *}
445\end{cfuncdesc}
446
447\begin{cfuncdesc}{int}{PyRun_SimpleString}{char *}
448\end{cfuncdesc}
449
450\begin{cfuncdesc}{int}{PyRun_SimpleFile}{FILE *, char *}
451\end{cfuncdesc}
452
453\begin{cfuncdesc}{int}{PyRun_InteractiveOne}{FILE *, char *}
454\end{cfuncdesc}
455
456\begin{cfuncdesc}{int}{PyRun_InteractiveLoop}{FILE *, char *}
457\end{cfuncdesc}
458
459\begin{cfuncdesc}{struct _node *}{PyParser_SimpleParseString}{char *, int}
460\end{cfuncdesc}
461
462\begin{cfuncdesc}{struct _node *}{PyParser_SimpleParseFile}{FILE *, char *, int}
463\end{cfuncdesc}
464
465\begin{cfuncdesc}{}{PyObject *PyRun}{ROTO((char *, int, PyObject *, PyObject *}
466\end{cfuncdesc}
467
468\begin{cfuncdesc}{}{PyObject *PyRun}{ROTO((FILE *, char *, int, PyObject *, PyObject *}
469\end{cfuncdesc}
470
471\begin{cfuncdesc}{}{PyObject *Py}{ROTO((char *, char *, int}
472\end{cfuncdesc}
473
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000474
475\chapter{Abstract Objects Layer}
476
477The functions in this chapter interact with Python objects regardless
478of their type, or with wide classes of object types (e.g. all
479numerical types, or all sequence types). When used on object types
480for which they do not apply, they will flag a Python exception.
481
482\section{Object Protocol}
483
484\begin{cfuncdesc}{int}{PyObject_Print}{PyObject *o, FILE *fp, int flags}
485Print an object \code{o}, on file \code{fp}. Returns -1 on error
486The flags argument is used to enable certain printing
487options. The only option currently supported is \code{Py_Print_RAW}.
488\end{cfuncdesc}
489
490\begin{cfuncdesc}{int}{PyObject_HasAttrString}{PyObject *o, char *attr_name}
491Returns 1 if o has the attribute attr_name, and 0 otherwise.
492This is equivalent to the Python expression:
493\code{hasattr(o,attr_name)}.
494This function always succeeds.
495\end{cfuncdesc}
496
497\begin{cfuncdesc}{PyObject*}{PyObject_GetAttrString}{PyObject *o, char *attr_name}
498Retrieve an attributed named attr_name form object o.
499Returns the attribute value on success, or \NULL{} on failure.
500This is the equivalent of the Python expression: \code{o.attr_name}.
501\end{cfuncdesc}
502
503
504\begin{cfuncdesc}{int}{PyObject_HasAttr}{PyObject *o, PyObject *attr_name}
505Returns 1 if o has the attribute attr_name, and 0 otherwise.
506This is equivalent to the Python expression:
507\code{hasattr(o,attr_name)}.
508This function always succeeds.
509\end{cfuncdesc}
510
511
512\begin{cfuncdesc}{PyObject*}{PyObject_GetAttr}{PyObject *o, PyObject *attr_name}
513Retrieve an attributed named attr_name form object o.
514Returns the attribute value on success, or \NULL{} on failure.
515This is the equivalent of the Python expression: o.attr_name.
516\end{cfuncdesc}
517
518
519\begin{cfuncdesc}{int}{PyObject_SetAttrString}{PyObject *o, char *attr_name, PyObject *v}
520Set the value of the attribute named \code{attr_name}, for object \code{o},
521to the value \code{v}. Returns -1 on failure. This is
522the equivalent of the Python statement: \code{o.attr_name=v}.
523\end{cfuncdesc}
524
525
526\begin{cfuncdesc}{int}{PyObject_SetAttr}{PyObject *o, PyObject *attr_name, PyObject *v}
527Set the value of the attribute named \code{attr_name}, for
528object \code{o},
529to the value \code{v}. Returns -1 on failure. This is
530the equivalent of the Python statement: \code{o.attr_name=v}.
531\end{cfuncdesc}
532
533
534\begin{cfuncdesc}{int}{PyObject_DelAttrString}{PyObject *o, char *attr_name}
535Delete attribute named \code{attr_name}, for object \code{o}. Returns -1 on
536failure. This is the equivalent of the Python
537statement: \code{del o.attr_name}.
538\end{cfuncdesc}
539
540
541\begin{cfuncdesc}{int}{PyObject_DelAttr}{PyObject *o, PyObject *attr_name}
542Delete attribute named \code{attr_name}, for object \code{o}. Returns -1 on
543failure. This is the equivalent of the Python
544statement: \code{del o.attr_name}.
545\end{cfuncdesc}
546
547
548\begin{cfuncdesc}{int}{PyObject_Cmp}{PyObject *o1, PyObject *o2, int *result}
549Compare the values of \code{o1} and \code{o2} using a routine provided by
550\code{o1}, if one exists, otherwise with a routine provided by \code{o2}.
551The result of the comparison is returned in \code{result}. Returns
552-1 on failure. This is the equivalent of the Python
553statement: \code{result=cmp(o1,o2)}.
554\end{cfuncdesc}
555
556
557\begin{cfuncdesc}{int}{PyObject_Compare}{PyObject *o1, PyObject *o2}
558Compare the values of \code{o1} and \code{o2} using a routine provided by
559\code{o1}, if one exists, otherwise with a routine provided by \code{o2}.
560Returns the result of the comparison on success. On error,
561the value returned is undefined. This is equivalent to the
562Python expression: \code{cmp(o1,o2)}.
563\end{cfuncdesc}
564
565
566\begin{cfuncdesc}{PyObject*}{PyObject_Repr}{PyObject *o}
567Compute the string representation of object, \code{o}. Returns the
568string representation on success, \NULL{} on failure. This is
569the equivalent of the Python expression: \code{repr(o)}.
570Called by the \code{repr()} built-in function and by reverse quotes.
571\end{cfuncdesc}
572
573
574\begin{cfuncdesc}{PyObject*}{PyObject_Str}{PyObject *o}
575Compute the string representation of object, \code{o}. Returns the
576string representation on success, \NULL{} on failure. This is
577the equivalent of the Python expression: \code{str(o)}.
578Called by the \code{str()} built-in function and by the \code{print}
579statement.
580\end{cfuncdesc}
581
582
583\begin{cfuncdesc}{int}{PyCallable_Check}{PyObject *o}
584Determine if the object \code{o}, is callable. Return 1 if the
585object is callable and 0 otherwise.
586This function always succeeds.
587\end{cfuncdesc}
588
589
590\begin{cfuncdesc}{PyObject*}{PyObject_CallObject}{PyObject *callable_object, PyObject *args}
591Call a callable Python object \code{callable_object}, with
592arguments given by the tuple \code{args}. If no arguments are
593needed, then args may be \NULL{}. Returns the result of the
594call on success, or \NULL{} on failure. This is the equivalent
595of the Python expression: \code{apply(o, args)}.
596\end{cfuncdesc}
597
598\begin{cfuncdesc}{PyObject*}{PyObject_CallFunction}{PyObject *callable_object, char *format, ...}
599Call a callable Python object \code{callable_object}, with a
600variable number of C arguments. The C arguments are described
601using a mkvalue-style format string. The format may be \NULL{},
602indicating that no arguments are provided. Returns the
603result of the call on success, or \NULL{} on failure. This is
604the equivalent of the Python expression: \code{apply(o,args)}.
605\end{cfuncdesc}
606
607
608\begin{cfuncdesc}{PyObject*}{PyObject_CallMethod}{PyObject *o, char *m, char *format, ...}
609Call the method named \code{m} of object \code{o} with a variable number of
610C arguments. The C arguments are described by a mkvalue
611format string. The format may be \NULL{}, indicating that no
612arguments are provided. Returns the result of the call on
613success, or \NULL{} on failure. This is the equivalent of the
614Python expression: \code{o.method(args)}.
615Note that Special method names, such as "\code{__add__}",
616"\code{__getitem__}", and so on are not supported. The specific
617abstract-object routines for these must be used.
618\end{cfuncdesc}
619
620
621\begin{cfuncdesc}{int}{PyObject_Hash}{PyObject *o}
622Compute and return the hash value of an object \code{o}. On
623failure, return -1. This is the equivalent of the Python
624expression: \code{hash(o)}.
625\end{cfuncdesc}
626
627
628\begin{cfuncdesc}{int}{PyObject_IsTrue}{PyObject *o}
629Returns 1 if the object \code{o} is considered to be true, and
6300 otherwise. This is equivalent to the Python expression:
631\code{not not o}.
632This function always succeeds.
633\end{cfuncdesc}
634
635
636\begin{cfuncdesc}{PyObject*}{PyObject_Type}{PyObject *o}
637On success, returns a type object corresponding to the object
638type of object \code{o}. On failure, returns \NULL{}. This is
639equivalent to the Python expression: \code{type(o)}.
640\end{cfuncdesc}
641
642\begin{cfuncdesc}{int}{PyObject_Length}{PyObject *o}
643Return the length of object \code{o}. If the object \code{o} provides
644both sequence and mapping protocols, the sequence length is
645returned. On error, -1 is returned. This is the equivalent
646to the Python expression: \code{len(o)}.
647\end{cfuncdesc}
648
649
650\begin{cfuncdesc}{PyObject*}{PyObject_GetItem}{PyObject *o, PyObject *key}
651Return element of \code{o} corresponding to the object \code{key} or \NULL{}
652on failure. This is the equivalent of the Python expression:
653\code{o[key]}.
654\end{cfuncdesc}
655
656
657\begin{cfuncdesc}{int}{PyObject_SetItem}{PyObject *o, PyObject *key, PyObject *v}
658Map the object \code{key} to the value \code{v}.
659Returns -1 on failure. This is the equivalent
660of the Python statement: \code{o[key]=v}.
661\end{cfuncdesc}
662
663
664\begin{cfuncdesc}{int}{PyObject_DelItem}{PyObject *o, PyObject *key, PyObject *v}
665Delete the mapping for \code{key} from \code{*o}. Returns -1
666on failure.
667This is the equivalent of the Python statement: del o[key].
668\end{cfuncdesc}
669
670
671\section{Number Protocol}
672
673\begin{cfuncdesc}{int}{PyNumber_Check}{PyObject *o}
674Returns 1 if the object \code{o} provides numeric protocols, and
675false otherwise.
676This function always succeeds.
677\end{cfuncdesc}
678
679
680\begin{cfuncdesc}{PyObject*}{PyNumber_Add}{PyObject *o1, PyObject *o2}
681Returns the result of adding \code{o1} and \code{o2}, or null on failure.
682This is the equivalent of the Python expression: \code{o1+o2}.
683\end{cfuncdesc}
684
685
686\begin{cfuncdesc}{PyObject*}{PyNumber_Subtract}{PyObject *o1, PyObject *o2}
687Returns the result of subtracting \code{o2} from \code{o1}, or null on
688failure. This is the equivalent of the Python expression:
689\code{o1-o2}.
690\end{cfuncdesc}
691
692
693\begin{cfuncdesc}{PyObject*}{PyNumber_Multiply}{PyObject *o1, PyObject *o2}
694Returns the result of multiplying \code{o1} and \code{o2}, or null on
695failure. This is the equivalent of the Python expression:
696\code{o1*o2}.
697\end{cfuncdesc}
698
699
700\begin{cfuncdesc}{PyObject*}{PyNumber_Divide}{PyObject *o1, PyObject *o2}
701Returns the result of dividing \code{o1} by \code{o2}, or null on failure.
702This is the equivalent of the Python expression: \code{o1/o2}.
703\end{cfuncdesc}
704
705
706\begin{cfuncdesc}{PyObject*}{PyNumber_Remainder}{PyObject *o1, PyObject *o2}
707Returns the remainder of dividing \code{o1} by \code{o2}, or null on
708failure. This is the equivalent of the Python expression:
709\code{o1\%o2}.
710\end{cfuncdesc}
711
712
713\begin{cfuncdesc}{PyObject*}{PyNumber_Divmod}{PyObject *o1, PyObject *o2}
714See the built-in function divmod. Returns \NULL{} on failure.
715This is the equivalent of the Python expression:
716\code{divmod(o1,o2)}.
717\end{cfuncdesc}
718
719
720\begin{cfuncdesc}{PyObject*}{PyNumber_Power}{PyObject *o1, PyObject *o2, PyObject *o3}
721See the built-in function pow. Returns \NULL{} on failure.
722This is the equivalent of the Python expression:
723\code{pow(o1,o2,o3)}, where \code{o3} is optional.
724\end{cfuncdesc}
725
726
727\begin{cfuncdesc}{PyObject*}{PyNumber_Negative}{PyObject *o}
728Returns the negation of \code{o} on success, or null on failure.
729This is the equivalent of the Python expression: \code{-o}.
730\end{cfuncdesc}
731
732
733\begin{cfuncdesc}{PyObject*}{PyNumber_Positive}{PyObject *o}
734Returns \code{o} on success, or \NULL{} on failure.
735This is the equivalent of the Python expression: \code{+o}.
736\end{cfuncdesc}
737
738
739\begin{cfuncdesc}{PyObject*}{PyNumber_Absolute}{PyObject *o}
740Returns the absolute value of \code{o}, or null on failure. This is
741the equivalent of the Python expression: \code{abs(o)}.
742\end{cfuncdesc}
743
744
745\begin{cfuncdesc}{PyObject*}{PyNumber_Invert}{PyObject *o}
746Returns the bitwise negation of \code{o} on success, or \NULL{} on
747failure. This is the equivalent of the Python expression:
748\code{~o}.
749\end{cfuncdesc}
750
751
752\begin{cfuncdesc}{PyObject*}{PyNumber_Lshift}{PyObject *o1, PyObject *o2}
753Returns the result of left shifting \code{o1} by \code{o2} on success, or
754\NULL{} on failure. This is the equivalent of the Python
755expression: \code{o1 << o2}.
756\end{cfuncdesc}
757
758
759\begin{cfuncdesc}{PyObject*}{PyNumber_Rshift}{PyObject *o1, PyObject *o2}
760Returns the result of right shifting \code{o1} by \code{o2} on success, or
761\NULL{} on failure. This is the equivalent of the Python
762expression: \code{o1 >> o2}.
763\end{cfuncdesc}
764
765
766\begin{cfuncdesc}{PyObject*}{PyNumber_And}{PyObject *o1, PyObject *o2}
767Returns the result of "anding" \code{o2} and \code{o2} on success and \NULL{}
768on failure. This is the equivalent of the Python
769expression: \code{o1 and o2}.
770\end{cfuncdesc}
771
772
773\begin{cfuncdesc}{PyObject*}{PyNumber_Xor}{PyObject *o1, PyObject *o2}
774Returns the bitwise exclusive or of \code{o1} by \code{o2} on success, or
775\NULL{} on failure. This is the equivalent of the Python
776expression: \code{o1\^{ }o2}.
777\end{cfuncdesc}
778
779\begin{cfuncdesc}{PyObject*}{PyNumber_Or}{PyObject *o1, PyObject *o2}
780Returns the result or \code{o1} and \code{o2} on success, or \NULL{} on
781failure. This is the equivalent of the Python expression:
782\code{o1 or o2}.
783\end{cfuncdesc}
784
785
786\begin{cfuncdesc}{PyObject*}{PyNumber_Coerce}{PyObject *o1, PyObject *o2}
787This function takes the addresses of two variables of type
788\code{PyObject*}.
789
790If the objects pointed to by \code{*p1} and \code{*p2} have the same type,
791increment their reference count and return 0 (success).
792If the objects can be converted to a common numeric type,
793replace \code{*p1} and \code{*p2} by their converted value (with 'new'
794reference counts), and return 0.
795If no conversion is possible, or if some other error occurs,
796return -1 (failure) and don't increment the reference counts.
797The call \code{PyNumber_Coerce(\&o1, \&o2)} is equivalent to the Python
798statement \code{o1, o2 = coerce(o1, o2)}.
799\end{cfuncdesc}
800
801
802\begin{cfuncdesc}{PyObject*}{PyNumber_Int}{PyObject *o}
803Returns the \code{o} converted to an integer object on success, or
804\NULL{} on failure. This is the equivalent of the Python
805expression: \code{int(o)}.
806\end{cfuncdesc}
807
808
809\begin{cfuncdesc}{PyObject*}{PyNumber_Long}{PyObject *o}
810Returns the \code{o} converted to a long integer object on success,
811or \NULL{} on failure. This is the equivalent of the Python
812expression: \code{long(o)}.
813\end{cfuncdesc}
814
815
816\begin{cfuncdesc}{PyObject*}{PyNumber_Float}{PyObject *o}
817Returns the \code{o} converted to a float object on success, or \NULL{}
818on failure. This is the equivalent of the Python expression:
819\code{float(o)}.
820\end{cfuncdesc}
821
822
823\section{Sequence protocol}
824
825\begin{cfuncdesc}{int}{PySequence_Check}{PyObject *o}
826Return 1 if the object provides sequence protocol, and 0
827otherwise.
828This function always succeeds.
829\end{cfuncdesc}
830
831
832\begin{cfuncdesc}{PyObject*}{PySequence_Concat}{PyObject *o1, PyObject *o2}
833Return the concatination of \code{o1} and \code{o2} on success, and \NULL{} on
834failure. This is the equivalent of the Python
835expression: \code{o1+o2}.
836\end{cfuncdesc}
837
838
839\begin{cfuncdesc}{PyObject*}{PySequence_Repeat}{PyObject *o, int count}
840Return the result of repeating sequence object \code{o} count times,
841or \NULL{} on failure. This is the equivalent of the Python
842expression: \code{o*count}.
843\end{cfuncdesc}
844
845
846\begin{cfuncdesc}{PyObject*}{PySequence_GetItem}{PyObject *o, int i}
847Return the ith element of \code{o}, or \NULL{} on failure. This is the
848equivalent of the Python expression: \code{o[i]}.
849\end{cfuncdesc}
850
851
852\begin{cfuncdesc}{PyObject*}{PySequence_GetSlice}{PyObject *o, int i1, int i2}
853Return the slice of sequence object \code{o} between \code{i1} and \code{i2}, or
854\NULL{} on failure. This is the equivalent of the Python
855expression, \code{o[i1:i2]}.
856\end{cfuncdesc}
857
858
859\begin{cfuncdesc}{int}{PySequence_SetItem}{PyObject *o, int i, PyObject *v}
860Assign object \code{v} to the \code{i}th element of \code{o}.
861Returns -1 on failure. This is the equivalent of the Python
862statement, \code{o[i]=v}.
863\end{cfuncdesc}
864
865\begin{cfuncdesc}{int}{PySequence_DelItem}{PyObject *o, int i}
866Delete the \code{i}th element of object \code{v}. Returns
867-1 on failure. This is the equivalent of the Python
868statement: \code{del o[i]}.
869\end{cfuncdesc}
870
871\begin{cfuncdesc}{int}{PySequence_SetSlice}{PyObject *o, int i1, int i2, PyObject *v}
872Assign the sequence object \code{v} to the slice in sequence
873object \code{o} from \code{i1} to \code{i2}. This is the equivalent of the Python
874statement, \code{o[i1:i2]=v}.
875\end{cfuncdesc}
876
877\begin{cfuncdesc}{int}{PySequence_DelSlice}{PyObject *o, int i1, int i2}
878Delete the slice in sequence object, \code{o}, from \code{i1} to \code{i2}.
879Returns -1 on failure. This is the equivalent of the Python
880statement: \code{del o[i1:i2]}.
881\end{cfuncdesc}
882
883\begin{cfuncdesc}{PyObject*}{PySequence_Tuple}{PyObject *o}
884Returns the \code{o} as a tuple on success, and \NULL{} on failure.
885This is equivalent to the Python expression: \code{tuple(o)}.
886\end{cfuncdesc}
887
888\begin{cfuncdesc}{int}{PySequence_Count}{PyObject *o, PyObject *value}
889Return the number of occurrences of \code{value} on \code{o}, that is,
890return the number of keys for which \code{o[key]==value}. On
891failure, return -1. This is equivalent to the Python
892expression: \code{o.count(value)}.
893\end{cfuncdesc}
894
895\begin{cfuncdesc}{int}{PySequence_In}{PyObject *o, PyObject *value}
896Determine if \code{o} contains \code{value}. If an item in \code{o} is equal to
897\code{value}, return 1, otherwise return 0. On error, return -1. This
898is equivalent to the Python expression: \code{value in o}.
899\end{cfuncdesc}
900
901\begin{cfuncdesc}{int}{PySequence_Index}{PyObject *o, PyObject *value}
902Return the first index for which \code{o[i]=value}. On error,
903return -1. This is equivalent to the Python
904expression: \code{o.index(value)}.
905\end{cfuncdesc}
906
907\section{Mapping protocol}
908
909\begin{cfuncdesc}{int}{PyMapping_Check}{PyObject *o}
910Return 1 if the object provides mapping protocol, and 0
911otherwise.
912This function always succeeds.
913\end{cfuncdesc}
914
915
916\begin{cfuncdesc}{int}{PyMapping_Length}{PyObject *o}
917Returns the number of keys in object \code{o} on success, and -1 on
918failure. For objects that do not provide sequence protocol,
919this is equivalent to the Python expression: \code{len(o)}.
920\end{cfuncdesc}
921
922
923\begin{cfuncdesc}{int}{PyMapping_DelItemString}{PyObject *o, char *key}
924Remove the mapping for object \code{key} from the object \code{o}.
925Return -1 on failure. This is equivalent to
926the Python statement: \code{del o[key]}.
927\end{cfuncdesc}
928
929
930\begin{cfuncdesc}{int}{PyMapping_DelItem}{PyObject *o, PyObject *key}
931Remove the mapping for object \code{key} from the object \code{o}.
932Return -1 on failure. This is equivalent to
933the Python statement: \code{del o[key]}.
934\end{cfuncdesc}
935
936
937\begin{cfuncdesc}{int}{PyMapping_HasKeyString}{PyObject *o, char *key}
938On success, return 1 if the mapping object has the key \code{key}
939and 0 otherwise. This is equivalent to the Python expression:
940\code{o.has_key(key)}.
941This function always succeeds.
942\end{cfuncdesc}
943
944
945\begin{cfuncdesc}{int}{PyMapping_HasKey}{PyObject *o, PyObject *key}
946Return 1 if the mapping object has the key \code{key}
947and 0 otherwise. This is equivalent to the Python expression:
948\code{o.has_key(key)}.
949This function always succeeds.
950\end{cfuncdesc}
951
952
953\begin{cfuncdesc}{PyObject*}{PyMapping_Keys}{PyObject *o}
954On success, return a list of the keys in object \code{o}. On
955failure, return \NULL{}. This is equivalent to the Python
956expression: \code{o.keys()}.
957\end{cfuncdesc}
958
959
960\begin{cfuncdesc}{PyObject*}{PyMapping_Values}{PyObject *o}
961On success, return a list of the values in object \code{o}. On
962failure, return \NULL{}. This is equivalent to the Python
963expression: \code{o.values()}.
964\end{cfuncdesc}
965
966
967\begin{cfuncdesc}{PyObject*}{PyMapping_Items}{PyObject *o}
968On success, return a list of the items in object \code{o}, where
969each item is a tuple containing a key-value pair. On
970failure, return \NULL{}. This is equivalent to the Python
971expression: \code{o.items()}.
972\end{cfuncdesc}
973
974\begin{cfuncdesc}{int}{PyMapping_Clear}{PyObject *o}
975Make object \code{o} empty. Returns 1 on success and 0 on failure.
976This is equivalent to the Python statement:
977\code{for key in o.keys(): del o[key]}
978\end{cfuncdesc}
979
980
981\begin{cfuncdesc}{PyObject*}{PyMapping_GetItemString}{PyObject *o, char *key}
982Return element of \code{o} corresponding to the object \code{key} or \NULL{}
983on failure. This is the equivalent of the Python expression:
984\code{o[key]}.
985\end{cfuncdesc}
986
987\begin{cfuncdesc}{PyObject*}{PyMapping_SetItemString}{PyObject *o, char *key, PyObject *v}
988Map the object \code{key} to the value \code{v} in object \code{o}. Returns
989-1 on failure. This is the equivalent of the Python
990statement: \code{o[key]=v}.
991\end{cfuncdesc}
992
993
994\section{Constructors}
995
996\begin{cfuncdesc}{PyObject*}{PyFile_FromString}{char *file_name, char *mode}
997On success, returns a new file object that is opened on the
998file given by \code{file_name}, with a file mode given by \code{mode},
999where \code{mode} has the same semantics as the standard C routine,
1000fopen. On failure, return -1.
1001\end{cfuncdesc}
1002
1003\begin{cfuncdesc}{PyObject*}{PyFile_FromFile}{FILE *fp, char *file_name, char *mode, int close_on_del}
1004Return a new file object for an already opened standard C
1005file pointer, \code{fp}. A file name, \code{file_name}, and open mode,
1006\code{mode}, must be provided as well as a flag, \code{close_on_del}, that
1007indicates whether the file is to be closed when the file
1008object is destroyed. On failure, return -1.
1009\end{cfuncdesc}
1010
1011\begin{cfuncdesc}{PyObject*}{PyFloat_FromDouble}{double v}
1012Returns a new float object with the value \code{v} on success, and
1013\NULL{} on failure.
1014\end{cfuncdesc}
1015
1016\begin{cfuncdesc}{PyObject*}{PyInt_FromLong}{long v}
1017Returns a new int object with the value \code{v} on success, and
1018\NULL{} on failure.
1019\end{cfuncdesc}
1020
1021\begin{cfuncdesc}{PyObject*}{PyList_New}{int l}
1022Returns a new list of length \code{l} on success, and \NULL{} on
1023failure.
1024\end{cfuncdesc}
1025
1026\begin{cfuncdesc}{PyObject*}{PyLong_FromLong}{long v}
1027Returns a new long object with the value \code{v} on success, and
1028\NULL{} on failure.
1029\end{cfuncdesc}
1030
1031\begin{cfuncdesc}{PyObject*}{PyLong_FromDouble}{double v}
1032Returns a new long object with the value \code{v} on success, and
1033\NULL{} on failure.
1034\end{cfuncdesc}
1035
1036\begin{cfuncdesc}{PyObject*}{PyDict_New}{}
1037Returns a new empty dictionary on success, and \NULL{} on
1038failure.
1039\end{cfuncdesc}
1040
1041\begin{cfuncdesc}{PyObject*}{PyString_FromString}{char *v}
1042Returns a new string object with the value \code{v} on success, and
1043\NULL{} on failure.
1044\end{cfuncdesc}
1045
1046\begin{cfuncdesc}{PyObject*}{PyString_FromStringAndSize}{char *v, int l}
1047Returns a new string object with the value \code{v} and length \code{l}
1048on success, and \NULL{} on failure.
1049\end{cfuncdesc}
1050
1051\begin{cfuncdesc}{PyObject*}{PyTuple_New}{int l}
1052Returns a new tuple of length \code{l} on success, and \NULL{} on
1053failure.
1054\end{cfuncdesc}
1055
1056
1057\chapter{Concrete Objects Layer}
1058
1059The functions in this chapter are specific to certain Python object
1060types. Passing them an object of the wrong type is not a good idea;
1061if you receive an object from a Python program and you are not sure
1062that it has the right type, you must perform a type check first;
1063e.g. to check that an object is a dictionary, use
1064\code{PyDict_Check()}.
1065
1066
1067\chapter{Defining New Object Types}
1068
1069\begin{cfuncdesc}{PyObject *}{_PyObject_New}{PyTypeObject *type}
1070\end{cfuncdesc}
1071
Guido van Rossumae110af1997-05-22 20:11:52 +00001072\begin{cfuncdesc}{PyObject *}{_PyObject_NewVar}{PyTypeObject *type, int size}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001073\end{cfuncdesc}
1074
Guido van Rossumae110af1997-05-22 20:11:52 +00001075\begin{cfuncdesc}{TYPE}{_PyObject_NEW}{TYPE, PyTypeObject *}
1076\end{cfuncdesc}
1077
1078\begin{cfuncdesc}{TYPE}{_PyObject_NEW_VAR}{TYPE, PyTypeObject *, int size}
1079\end{cfuncdesc}
1080
1081XXX To be done:
1082
1083PyObject, PyVarObject
1084
1085PyObject_HEAD, PyObject_HEAD_INIT, PyObject_VAR_HEAD
1086
1087Typedefs:
1088unaryfunc, binaryfunc, ternaryfunc, inquiry, coercion, intargfunc,
1089intintargfunc, intobjargproc, intintobjargproc, objobjargproc,
1090getreadbufferproc, getwritebufferproc, getsegcountproc,
1091destructor, printfunc, getattrfunc, getattrofunc, setattrfunc,
1092setattrofunc, cmpfunc, reprfunc, hashfunc
1093
1094PyNumberMethods
1095
1096PySequenceMethods
1097
1098PyMappingMethods
1099
1100PyBufferProcs
1101
1102PyTypeObject
1103
1104DL_IMPORT
1105
1106PyType_Type
1107
1108Py*_Check
1109
1110Py_None, _Py_NoneStruct
1111
1112_PyObject_New, _PyObject_NewVar
1113
1114PyObject_NEW, PyObject_NEW_VAR
1115
1116
1117\chapter{Specific Data Types}
1118
1119This chapter describes the functions that deal with specific types of
1120Python objects. It is structured like the ``family tree'' of Python
1121object types.
1122
1123
1124\section{Fundamental Objects}
1125
1126This section describes Python type objects and the singleton object
1127\code{None}.
1128
1129
1130\subsection{Type Objects}
1131
1132\begin{ctypedesc}{PyTypeObject}
1133
1134\end{ctypedesc}
1135
1136\begin{cvardesc}{PyObject *}{PyType_Type}
1137
1138\end{cvardesc}
1139
1140
1141\subsection{The None Object}
1142
1143\begin{cvardesc}{PyObject *}{Py_None}
1144macro
1145\end{cvardesc}
1146
1147
1148\section{Sequence Objects}
1149
1150Generic operations on sequence objects were discussed in the previous
1151chapter; this section deals with the specific kinds of sequence
1152objects that are intrinsuc to the Python language.
1153
1154
1155\subsection{String Objects}
1156
1157\begin{ctypedesc}{PyStringObject}
1158This subtype of \code{PyObject} represents a Python string object.
1159\end{ctypedesc}
1160
1161\begin{cvardesc}{PyTypeObject}{PyString_Type}
1162This instance of \code{PyTypeObject} represents the Python string type.
1163\end{cvardesc}
1164
1165\begin{cfuncdesc}{int}{PyString_Check}{PyObject *o}
1166
1167\end{cfuncdesc}
1168
1169\begin{cfuncdesc}{PyObject *}{PyString_FromStringAndSize}{const char *, int}
1170
1171\end{cfuncdesc}
1172
1173\begin{cfuncdesc}{PyObject *}{PyString_FromString}{const char *}
1174
1175\end{cfuncdesc}
1176
1177\begin{cfuncdesc}{int}{PyString_Size}{PyObject *}
1178
1179\end{cfuncdesc}
1180
1181\begin{cfuncdesc}{char *}{PyString_AsString}{PyObject *}
1182
1183\end{cfuncdesc}
1184
1185\begin{cfuncdesc}{void}{PyString_Concat}{PyObject **, PyObject *}
1186
1187\end{cfuncdesc}
1188
1189\begin{cfuncdesc}{void}{PyString_ConcatAndDel}{PyObject **, PyObject *}
1190
1191\end{cfuncdesc}
1192
1193\begin{cfuncdesc}{int}{_PyString_Resize}{PyObject **, int}
1194
1195\end{cfuncdesc}
1196
1197\begin{cfuncdesc}{PyObject *}{PyString_Format}{PyObject *, PyObject *}
1198
1199\end{cfuncdesc}
1200
1201\begin{cfuncdesc}{void}{PyString_InternInPlace}{PyObject **}
1202
1203\end{cfuncdesc}
1204
1205\begin{cfuncdesc}{PyObject *}{PyString_InternFromString}{const char *}
1206
1207\end{cfuncdesc}
1208
1209\begin{cfuncdesc}{char *}{PyString_AS_STRING}{PyStringObject *}
1210
1211\end{cfuncdesc}
1212
1213\begin{cfuncdesc}{int}{PyString_GET_SIZE}{PyStringObject *}
1214
1215\end{cfuncdesc}
1216
1217
1218\subsection{Tuple Objects}
1219
1220\begin{ctypedesc}{PyTupleObject}
1221This subtype of \code{PyObject} represents a Python tuple object.
1222\end{ctypedesc}
1223
1224\begin{cvardesc}{PyTypeObject}{PyTuple_Type}
1225This instance of \code{PyTypeObject} represents the Python tuple type.
1226\end{cvardesc}
1227
1228\begin{cfuncdesc}{int}{PyTuple_Check}{PyObject *p}
1229Return true if the argument is a tuple object.
1230\end{cfuncdesc}
1231
1232\begin{cfuncdesc}{PyTupleObject *}{PyTuple_New}{int s}
1233Return a new tuple object of size \code{s}
1234\end{cfuncdesc}
1235
1236\begin{cfuncdesc}{int}{PyTuple_Size}{PyTupleObject *p}
1237akes a pointer to a tuple object, and returns the size
1238of that tuple.
1239\end{cfuncdesc}
1240
1241\begin{cfuncdesc}{PyObject *}{PyTuple_GetItem}{PyTupleObject *p, int pos}
1242returns the object at position \code{pos} in the tuple pointed
1243to by \code{p}.
1244\end{cfuncdesc}
1245
1246\begin{cfuncdesc}{PyObject *}{PyTuple_GET_ITEM}{PyTupleObject *p, int pos}
1247does the same, but does no checking of it's
1248arguments.
1249\end{cfuncdesc}
1250
1251\begin{cfuncdesc}{PyTupleObject *}{PyTuple_GetSlice}{PyTupleObject *p,
1252 int low,
1253 int high}
1254takes a slice of the tuple pointed to by \code{p} from
1255\code{low} to \code{high} and returns it as a new tuple.
1256\end{cfuncdesc}
1257
1258\begin{cfuncdesc}{int}{PyTuple_SetItem}{PyTupleObject *p,
1259 int pos,
1260 PyObject *o}
1261inserts a reference to object \code{o} at position \code{pos} of
1262the tuple pointed to by \code{p}. It returns 0 on success.
1263\end{cfuncdesc}
1264
1265\begin{cfuncdesc}{void}{PyTuple_SET_ITEM}{PyTupleObject *p,
1266 int pos,
1267 PyObject *o}
1268
1269does the same, but does no error checking, and
1270should \emph{only} be used to fill in brand new tuples.
1271\end{cfuncdesc}
1272
1273\begin{cfuncdesc}{PyTupleObject *}{_PyTuple_Resize}{PyTupleObject *p,
1274 int new,
1275 int last_is_sticky}
1276can be used to resize a tuple. Because tuples are
1277\emph{supposed} to be immutable, this should only be used if there is only
1278one module referencing the object. Do \emph{not} use this if the tuple may
1279already be known to some other part of the code. \code{last_is_sticky} is
1280a flag - if set, the tuple will grow or shrink at the front, otherwise
1281it will grow or shrink at the end. Think of this as destroying the old
1282tuple and creating a new one, only more efficiently.
1283\end{cfuncdesc}
1284
1285
1286\subsection{List Objects}
1287
1288\begin{ctypedesc}{PyListObject}
1289This subtype of \code{PyObject} represents a Python list object.
1290\end{ctypedesc}
1291
1292\begin{cvardesc}{PyTypeObject}{PyList_Type}
1293This instance of \code{PyTypeObject} represents the Python list type.
1294\end{cvardesc}
1295
1296\begin{cfuncdesc}{int}{PyList_Check}{PyObject *p}
1297returns true if it's argument is a \code{PyListObject}
1298\end{cfuncdesc}
1299
1300\begin{cfuncdesc}{PyObject *}{PyList_New}{int size}
1301
1302\end{cfuncdesc}
1303
1304\begin{cfuncdesc}{int}{PyList_Size}{PyObject *}
1305
1306\end{cfuncdesc}
1307
1308\begin{cfuncdesc}{PyObject *}{PyList_GetItem}{PyObject *, int}
1309
1310\end{cfuncdesc}
1311
1312\begin{cfuncdesc}{int}{PyList_SetItem}{PyObject *, int, PyObject *}
1313
1314\end{cfuncdesc}
1315
1316\begin{cfuncdesc}{int}{PyList_Insert}{PyObject *, int, PyObject *}
1317
1318\end{cfuncdesc}
1319
1320\begin{cfuncdesc}{int}{PyList_Append}{PyObject *, PyObject *}
1321
1322\end{cfuncdesc}
1323
1324\begin{cfuncdesc}{PyObject *}{PyList_GetSlice}{PyObject *, int, int}
1325
1326\end{cfuncdesc}
1327
1328\begin{cfuncdesc}{int}{PyList_SetSlice}{PyObject *, int, int, PyObject *}
1329
1330\end{cfuncdesc}
1331
1332\begin{cfuncdesc}{int}{PyList_Sort}{PyObject *}
1333
1334\end{cfuncdesc}
1335
1336\begin{cfuncdesc}{int}{PyList_Reverse}{PyObject *}
1337
1338\end{cfuncdesc}
1339
1340\begin{cfuncdesc}{PyObject *}{PyList_AsTuple}{PyObject *}
1341
1342\end{cfuncdesc}
1343
1344\begin{cfuncdesc}{PyObject *}{PyList_GET_ITEM}{PyObject *list, int i}
1345
1346\end{cfuncdesc}
1347
1348\begin{cfuncdesc}{int}{PyList_GET_SIZE}{PyObject *list}
1349
1350\end{cfuncdesc}
1351
1352
1353\section{Mapping Objects}
1354
1355\subsection{Dictionary Objects}
1356
1357\begin{ctypedesc}{PyDictObject}
1358This subtype of \code{PyObject} represents a Python dictionary object.
1359\end{ctypedesc}
1360
1361\begin{cvardesc}{PyTypeObject}{PyDict_Type}
1362This instance of \code{PyTypeObject} represents the Python dictionary type.
1363\end{cvardesc}
1364
1365\begin{cfuncdesc}{int}{PyDict_Check}{PyObject *p}
1366returns true if it's argument is a PyDictObject
1367\end{cfuncdesc}
1368
1369\begin{cfuncdesc}{PyDictObject *}{PyDict_New}{}
1370returns a new empty dictionary.
1371\end{cfuncdesc}
1372
1373\begin{cfuncdesc}{void}{PyDict_Clear}{PyDictObject *p}
1374empties an existing dictionary and deletes it.
1375\end{cfuncdesc}
1376
1377\begin{cfuncdesc}{int}{PyDict_SetItem}{PyDictObject *p,
1378 PyObject *key,
1379 PyObject *val}
1380inserts \code{value} into the dictionary with a key of
1381\code{key}. Both \code{key} and \code{value} should be PyObjects, and \code{key} should
1382be hashable.
1383\end{cfuncdesc}
1384
1385\begin{cfuncdesc}{int}{PyDict_SetItemString}{PyDictObject *p,
1386 char *key,
1387 PyObject *val}
1388inserts \code{value} into the dictionary using \code{key}
1389as a key. \code{key} should be a char *
1390\end{cfuncdesc}
1391
1392\begin{cfuncdesc}{int}{PyDict_DelItem}{PyDictObject *p, PyObject *key}
1393removes the entry in dictionary \code{p} with key \code{key}.
1394\code{key} is a PyObject.
1395\end{cfuncdesc}
1396
1397\begin{cfuncdesc}{int}{PyDict_DelItemString}{PyDictObject *p, char *key}
1398removes the entry in dictionary \code{p} which has a key
1399specified by the \code{char *}\code{key}.
1400\end{cfuncdesc}
1401
1402\begin{cfuncdesc}{PyObject *}{PyDict_GetItem}{PyDictObject *p, PyObject *key}
1403returns the object from dictionary \code{p} which has a key
1404\code{key}.
1405\end{cfuncdesc}
1406
1407\begin{cfuncdesc}{PyObject *}{PyDict_GetItemString}{PyDictObject *p, char *key}
1408does the same, but \code{key} is specified as a
1409\code{char *}, rather than a \code{PyObject *}.
1410\end{cfuncdesc}
1411
1412\begin{cfuncdesc}{PyListObject *}{PyDict_Items}{PyDictObject *p}
1413returns a PyListObject containing all the items
1414from the dictionary, as in the mapping method \code{items()} (see the Reference
1415Guide)
1416\end{cfuncdesc}
1417
1418\begin{cfuncdesc}{PyListObject *}{PyDict_Keys}{PyDictObject *p}
1419returns a PyListObject containing all the keys
1420from the dictionary, as in the mapping method \code{keys()} (see the Reference Guide)
1421\end{cfuncdesc}
1422
1423\begin{cfuncdesc}{PyListObject *}{PyDict_Values}{PyDictObject *p}
1424returns a PyListObject containing all the values
1425from the dictionary, as in the mapping method \code{values()} (see the Reference Guide)
1426\end{cfuncdesc}
1427
1428\begin{cfuncdesc}{int}{PyDict_Size}{PyDictObject *p}
1429returns the number of items in the dictionary.
1430\end{cfuncdesc}
1431
1432\begin{cfuncdesc}{int}{PyDict_Next}{PyDictObject *p,
1433 int ppos,
1434 PyObject **pkey,
1435 PyObject **pvalue}
1436
1437\end{cfuncdesc}
1438
1439
1440\section{Numeric Objects}
1441
1442\subsection{Plain Integer Objects}
1443
1444\begin{ctypedesc}{PyIntObject}
1445This subtype of \code{PyObject} represents a Python integer object.
1446\end{ctypedesc}
1447
1448\begin{cvardesc}{PyTypeObject}{PyInt_Type}
1449This instance of \code{PyTypeObject} represents the Python plain
1450integer type.
1451\end{cvardesc}
1452
1453\begin{cfuncdesc}{int}{PyInt_Check}{PyObject *}
1454
1455\end{cfuncdesc}
1456
1457\begin{cfuncdesc}{PyIntObject *}{PyInt_FromLong}{long ival}
1458creates a new integer object with a value of \code{ival}.
1459
1460The current implementation keeps an array of integer objects for all
1461integers between -1 and 100, when you create an int in that range you
1462actually just get back a reference to the existing object. So it should
1463be possible to change the value of 1. I suspect the behaviour of python
1464in this case is undefined. :-)
1465\end{cfuncdesc}
1466
1467\begin{cfuncdesc}{long}{PyInt_AS_LONG}{PyIntObject *io}
1468returns the value of the object \code{io}.
1469\end{cfuncdesc}
1470
1471\begin{cfuncdesc}{long}{PyInt_AsLong}{PyObject *io}
1472will first attempt to cast the object to a PyIntObject, if
1473it is not already one, and the return it's value.
1474\end{cfuncdesc}
1475
1476\begin{cfuncdesc}{long}{PyInt_GetMax}{}
1477returns the systems idea of the largest int it can handle
1478(LONG_MAX, as defined in the system header files)
1479\end{cfuncdesc}
1480
1481
1482\subsection{Long Integer Objects}
1483
1484\begin{ctypedesc}{PyLongObject}
1485This subtype of \code{PyObject} represents a Python long integer object.
1486\end{ctypedesc}
1487
1488\begin{cvardesc}{PyTypeObject}{PyLong_Type}
1489This instance of \code{PyTypeObject} represents the Python long integer type.
1490\end{cvardesc}
1491
1492\begin{cfuncdesc}{int}{PyLong_Check}{PyObject *p}
1493returns true if it's argument is a \code{PyLongObject}
1494\end{cfuncdesc}
1495
1496\begin{cfuncdesc}{PyObject *}{PyLong_FromLong}{long}
1497
1498\end{cfuncdesc}
1499
1500\begin{cfuncdesc}{PyObject *}{PyLong_FromUnsignedLong}{unsigned long}
1501
1502\end{cfuncdesc}
1503
1504\begin{cfuncdesc}{PyObject *}{PyLong_FromDouble}{double}
1505
1506\end{cfuncdesc}
1507
1508\begin{cfuncdesc}{long}{PyLong_AsLong}{PyObject *}
1509
1510\end{cfuncdesc}
1511
1512\begin{cfuncdesc}{unsigned long}{PyLong_AsUnsignedLong}{PyObject }
1513
1514\end{cfuncdesc}
1515
1516\begin{cfuncdesc}{double}{PyLong_AsDouble}{PyObject *}
1517
1518\end{cfuncdesc}
1519
1520\begin{cfuncdesc}{PyObject *}{*PyLong_FromString}{char *, char **, int}
1521
1522\end{cfuncdesc}
1523
1524
1525\subsection{Floating Point Objects}
1526
1527\begin{ctypedesc}{PyFloatObject}
1528This subtype of \code{PyObject} represents a Python floating point object.
1529\end{ctypedesc}
1530
1531\begin{cvardesc}{PyTypeObject}{PyFloat_Type}
1532This instance of \code{PyTypeObject} represents the Python floating
1533point type.
1534\end{cvardesc}
1535
1536\begin{cfuncdesc}{int}{PyFloat_Check}{PyObject *p}
1537returns true if it's argument is a \code{PyFloatObject}
1538\end{cfuncdesc}
1539
1540\begin{cfuncdesc}{PyObject *}{PyFloat_FromDouble}{double}
1541
1542\end{cfuncdesc}
1543
1544\begin{cfuncdesc}{double}{PyFloat_AsDouble}{PyObject *}
1545
1546\end{cfuncdesc}
1547
1548\begin{cfuncdesc}{double}{PyFloat_AS_DOUBLE}{PyFloatObject *}
1549
1550\end{cfuncdesc}
1551
1552
1553\subsection{Complex Number Objects}
1554
1555\begin{ctypedesc}{Py_complex}
1556typedef struct {
1557 double real;
1558 double imag;
1559}
1560\end{ctypedesc}
1561
1562\begin{ctypedesc}{PyComplexObject}
1563This subtype of \code{PyObject} represents a Python complex number object.
1564\end{ctypedesc}
1565
1566\begin{cvardesc}{PyTypeObject}{PyComplex_Type}
1567This instance of \code{PyTypeObject} represents the Python complex
1568number type.
1569\end{cvardesc}
1570
1571\begin{cfuncdesc}{int}{PyComplex_Check}{PyObject *p}
1572returns true if it's argument is a \code{PyComplexObject}
1573\end{cfuncdesc}
1574
1575\begin{cfuncdesc}{Py_complex}{_Py_c_sum}{Py_complex, Py_complex}
1576
1577\end{cfuncdesc}
1578
1579\begin{cfuncdesc}{Py_complex}{_Py_c_diff}{Py_complex, Py_complex}
1580
1581\end{cfuncdesc}
1582
1583\begin{cfuncdesc}{Py_complex}{_Py_c_neg}{Py_complex}
1584
1585\end{cfuncdesc}
1586
1587\begin{cfuncdesc}{Py_complex}{_Py_c_prod}{Py_complex, Py_complex}
1588
1589\end{cfuncdesc}
1590
1591\begin{cfuncdesc}{Py_complex}{_Py_c_quot}{Py_complex, Py_complex}
1592
1593\end{cfuncdesc}
1594
1595\begin{cfuncdesc}{Py_complex}{_Py_c_pow}{Py_complex, Py_complex}
1596
1597\end{cfuncdesc}
1598
1599\begin{cfuncdesc}{PyObject *}{PyComplex_FromCComplex}{Py_complex}
1600
1601\end{cfuncdesc}
1602
1603\begin{cfuncdesc}{PyObject *}{PyComplex_FromDoubles}{double real, double imag}
1604
1605\end{cfuncdesc}
1606
1607\begin{cfuncdesc}{double}{PyComplex_RealAsDouble}{PyObject *op}
1608
1609\end{cfuncdesc}
1610
1611\begin{cfuncdesc}{double}{PyComplex_ImagAsDouble}{PyObject *op}
1612
1613\end{cfuncdesc}
1614
1615\begin{cfuncdesc}{Py_complex}{PyComplex_AsCComplex}{PyObject *op}
1616
1617\end{cfuncdesc}
1618
1619
1620
1621\section{Other Objects}
1622
1623\subsection{File Objects}
1624
1625\begin{ctypedesc}{PyFileObject}
1626This subtype of \code{PyObject} represents a Python file object.
1627\end{ctypedesc}
1628
1629\begin{cvardesc}{PyTypeObject}{PyFile_Type}
1630This instance of \code{PyTypeObject} represents the Python file type.
1631\end{cvardesc}
1632
1633\begin{cfuncdesc}{int}{PyFile_Check}{PyObject *p}
1634returns true if it's argument is a \code{PyFileObject}
1635\end{cfuncdesc}
1636
1637\begin{cfuncdesc}{PyObject *}{PyFile_FromString}{char *name, char *mode}
1638creates a new PyFileObject pointing to the file
1639specified in \code{name} with the mode specified in \code{mode}
1640\end{cfuncdesc}
1641
1642\begin{cfuncdesc}{PyObject *}{PyFile_FromFile}{FILE *fp,
1643 char *name, char *mode, int (*close})
1644creates a new PyFileObject from the already-open \code{fp}.
1645The function \code{close} will be called when the file should be closed.
1646\end{cfuncdesc}
1647
1648\begin{cfuncdesc}{FILE *}{PyFile_AsFile}{PyFileObject *p}
1649returns the file object associated with \code{p} as a \code{FILE *}
1650\end{cfuncdesc}
1651
1652\begin{cfuncdesc}{PyStringObject *}{PyFile_GetLine}{PyObject *p, int n}
1653undocumented as yet
1654\end{cfuncdesc}
1655
1656\begin{cfuncdesc}{PyStringObject *}{PyFile_Name}{PyObject *p}
1657returns the name of the file specified by \code{p} as a
1658PyStringObject
1659\end{cfuncdesc}
1660
1661\begin{cfuncdesc}{void}{PyFile_SetBufSize}{PyFileObject *p, int n}
1662on systems with \code{setvbuf} only
1663\end{cfuncdesc}
1664
1665\begin{cfuncdesc}{int}{PyFile_SoftSpace}{PyFileObject *p, int newflag}
1666same as the file object method \code{softspace}
1667\end{cfuncdesc}
1668
1669\begin{cfuncdesc}{int}{PyFile_WriteObject}{PyObject *obj, PyFileObject *p}
1670writes object \code{obj} to file object \code{p}
1671\end{cfuncdesc}
1672
1673\begin{cfuncdesc}{int}{PyFile_WriteString}{char *s, PyFileObject *p}
1674writes string \code{s} to file object \code{p}
1675\end{cfuncdesc}
1676
1677
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001678\input{api.ind} % Index -- must be last
1679
1680\end{document}