blob: 3a0ddb0130a43a83ba668251135499f1137413db [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 Rossum59a61351997-08-14 20:34:33 +000043The Application Programmer's Interface to Python gives C and C++
44programmers access to the Python interpreter at a variety of levels.
Guido van Rossum4a944d71997-08-14 20:35:38 +000045There are two fundamentally different reasons for using the Python/C
46API. (The API is equally usable from C++, but for brevity it is
47generally referred to as the Python/C API.) The first reason is to
48write ``extension modules'' for specific purposes; these are C modules
49that extend the Python interpreter. This is probably the most common
50use. The second reason is to use Python as a component in a larger
51application; this technique is generally referred to as ``embedding''
Guido van Rossum59a61351997-08-14 20:34:33 +000052Python in an application.
53
Guido van Rossum4a944d71997-08-14 20:35:38 +000054Writing an extension module is a relatively well-understood process,
55where a ``cookbook'' approach works well. There are several tools
56that automate the process to some extent. While people have embedded
57Python in other applications since its early existence, the process of
58embedding Python is less straightforward that writing an extension.
59Python 1.5 introduces a number of new API functions as well as some
60changes to the build process that make embedding much simpler.
Guido van Rossum59a61351997-08-14 20:34:33 +000061This manual describes the 1.5 state of affair (as of Python 1.5a3).
62% XXX Eventually, take the historical notes out
63
Guido van Rossum4a944d71997-08-14 20:35:38 +000064Many API functions are useful independent of whether you're embedding
65or extending Python; moreover, most applications that embed Python
66will need to provide a custom extension as well, so it's probably a
67good idea to become familiar with writing an extension before
Guido van Rossum59a61351997-08-14 20:34:33 +000068attempting to embed Python in a real application.
69
70\section{Objects, Types and Reference Counts}
71
Guido van Rossum4a944d71997-08-14 20:35:38 +000072Most Python/C API functions have one or more arguments as well as a
73return value of type \code{PyObject *}. This type is a pointer
74(obviously!) to an opaque data type representing an arbitrary Python
75object. Since all Python object types are treated the same way by the
76Python language in most situations (e.g., assignments, scope rules,
77and argument passing), it is only fitting that they should be
Guido van Rossum59a61351997-08-14 20:34:33 +000078represented by a single C type. All Python objects live on the heap:
Guido van Rossum4a944d71997-08-14 20:35:38 +000079you never declare an automatic or static variable of type
80\code{PyObject}, only pointer variables of type \code{PyObject *} can
Guido van Rossum59a61351997-08-14 20:34:33 +000081be declared.
82
Guido van Rossum4a944d71997-08-14 20:35:38 +000083All Python objects (even Python integers) have a ``type'' and a
84``reference count''. An object's type determines what kind of object
85it is (e.g., an integer, a list, or a user-defined function; there are
86many more as explained in the Python Language Reference Manual). For
87each of the well-known types there is a macro to check whether an
88object is of that type; for instance, \code{PyList_Check(a)} is true
Guido van Rossum59a61351997-08-14 20:34:33 +000089iff the object pointed to by \code{a} is a Python list.
90
Guido van Rossum4a944d71997-08-14 20:35:38 +000091The reference count is important only because today's computers have a
92finite (and often severly limited) memory size; it counts how many
93different places there are that have a reference to an object. Such a
94place could be another object, or a global (or static) C variable, or
95a local variable in some C function. When an object's reference count
96becomes zero, the object is deallocated. If it contains references to
97other objects, their reference count is decremented. Those other
98objects may be deallocated in turn, if this decrement makes their
99reference count become zero, and so on. (There's an obvious problem
100with objects that reference each other here; for now, the solution is
Guido van Rossum59a61351997-08-14 20:34:33 +0000101``don't do that''.)
102
Guido van Rossum4a944d71997-08-14 20:35:38 +0000103Reference counts are always manipulated explicitly. The normal way is
104to use the macro \code{Py_INCREF(a)} to increment an object's
105reference count by one, and \code{Py_DECREF(a)} to decrement it by
106one. The latter macro is considerably more complex than the former,
107since it must check whether the reference count becomes zero and then
108cause the object's deallocator, which is a function pointer contained
109in the object's type structure. The type-specific deallocator takes
110care of decrementing the reference counts for other objects contained
111in the object, and so on, if this is a compound object type such as a
112list. There's no chance that the reference count can overflow; at
113least as many bits are used to hold the reference count as there are
114distinct memory locations in virtual memory (assuming
115\code{sizeof(long) >= sizeof(char *)}). Thus, the reference count
Guido van Rossum59a61351997-08-14 20:34:33 +0000116increment is a simple operation.
117
Guido van Rossum4a944d71997-08-14 20:35:38 +0000118It is not necessary to increment an object's reference count for every
119local variable that contains a pointer to an object. In theory, the
120oject's reference count goes up by one when the variable is made to
121point to it and it goes down by one when the variable goes out of
122scope. However, these two cancel each other out, so at the end the
123reference count hasn't changed. The only real reason to use the
124reference count is to prevent the object from being deallocated as
125long as our variable is pointing to it. If we know that there is at
126least one other reference to the object that lives at least as long as
127our variable, there is no need to increment the reference count
128temporarily. An important situation where this arises is in objects
129that are passed as arguments to C functions in an extension module
130that are called from Python; the call mechanism guarantees to hold a
Guido van Rossum59a61351997-08-14 20:34:33 +0000131reference to every argument for the duration of the call.
132
Guido van Rossum4a944d71997-08-14 20:35:38 +0000133However, a common pitfall is to extract an object from a list and
134holding on to it for a while without incrementing its reference count.
135Some other operation might conceivably remove the object from the
136list, decrementing its reference count and possible deallocating it.
137The real danger is that innocent-looking operations may invoke
138arbitrary Python code which could do this; there is a code path which
139allows control to flow back to the user from a \code{Py_DECREF()}, so
Guido van Rossum59a61351997-08-14 20:34:33 +0000140almost any operation is potentially dangerous.
141
Guido van Rossum4a944d71997-08-14 20:35:38 +0000142A safe approach is to always use the generic operations (functions
143whose name begins with \code{PyObject_}, \code{PyNumber_},
144\code{PySequence_} or \code{PyMapping_}). These operations always
145increment the reference count of the object they return. This leaves
146the caller with the responsibility to call \code{Py_DECREF()} when
Guido van Rossum59a61351997-08-14 20:34:33 +0000147they are done with the result; this soon becomes second nature.
148
Guido van Rossum4a944d71997-08-14 20:35:38 +0000149There are very few other data types that play a significant role in
150the Python/C API; most are all simple C types such as \code{int},
151\code{long}, \code{double} and \code{char *}. A few structure types
152are used to describe static tables used to list the functions exported
153by a module or the data attributes of a new object type. These will
Guido van Rossum59a61351997-08-14 20:34:33 +0000154be discussed together with the functions that use them.
155
156\section{Exceptions}
157
Guido van Rossum4a944d71997-08-14 20:35:38 +0000158The Python programmer only needs to deal with exceptions if specific
159error handling is required; unhandled exceptions are automatically
160propagated to the caller, then to the caller's caller, and so on, till
161they reach the top-level interpreter, where they are reported to the
Guido van Rossum59a61351997-08-14 20:34:33 +0000162user accompanied by a stack trace.
163
164For C programmers, however, error checking always has to be explicit.
165% XXX add more stuff here
166
167\section{Embedding Python}
168
Guido van Rossum4a944d71997-08-14 20:35:38 +0000169The one important task that only embedders of the Python interpreter
170have to worry about is the initialization (and possibly the
171finalization) of the Python interpreter. Most functionality of the
172interpreter can only be used after the interpreter has been
Guido van Rossum59a61351997-08-14 20:34:33 +0000173initialized.
174
Guido van Rossum4a944d71997-08-14 20:35:38 +0000175The basic initialization function is \code{Py_Initialize()}. This
176initializes the table of loaded modules, and creates the fundamental
177modules \code{__builtin__}, \code{__main__} and \code{sys}. It also
Guido van Rossum59a61351997-08-14 20:34:33 +0000178initializes the module search path (\code{sys.path}).
179
Guido van Rossum4a944d71997-08-14 20:35:38 +0000180\code{Py_Initialize()} does not set the ``script argument list''
181(\code{sys.argv}). If this variable is needed by Python code that
182will be executed later, it must be set explicitly with a call to
183\code{PySys_SetArgv(\var{argc}, \var{argv})} subsequent to the call
Guido van Rossum59a61351997-08-14 20:34:33 +0000184to \code{Py_Initialize()}.
185
Guido van Rossum4a944d71997-08-14 20:35:38 +0000186On Unix, \code{Py_Initialize()} calculates the module search path
187based upon its best guess for the location of the standard Python
188interpreter executable, assuming that the Python library is found in a
189fixed location relative to the Python interpreter executable. In
190particular, it looks for a directory named \code{lib/python1.5}
191(replacing \code{1.5} with the current interpreter version) relative
192to the parent directory where the executable named \code{python} is
193found on the shell command search path (the environment variable
194\code{$PATH}). For instance, if the Python executable is found in
195\code{/usr/local/bin/python}, it will assume that the libraries are in
196\code{/usr/local/lib/python1.5}. In fact, this also the ``fallback''
197location, used when no executable file named \code{python} is found
198along \code{\$PATH}. The user can change this behavior by setting the
199environment variable \code{\$PYTHONHOME}, and can insert additional
200directories in front of the standard path by setting
Guido van Rossum59a61351997-08-14 20:34:33 +0000201\code{\$PYTHONPATH}.
202
Guido van Rossum4a944d71997-08-14 20:35:38 +0000203The embedding application can steer the search by calling
204\code{Py_SetProgramName(\var{file})} \emph{before} calling
205\code{Py_Initialize()}. Note that \code[$PYTHONHOME} still overrides
206this and \code{\$PYTHONPATH} is still inserted in front of the
Guido van Rossum59a61351997-08-14 20:34:33 +0000207standard path.
208
Guido van Rossum4a944d71997-08-14 20:35:38 +0000209Sometimes, it is desirable to ``uninitialize'' Python. For instance,
210the application may want to start over (make another call to
211\code{Py_Initialize()}) or the application is simply done with its
212use of Python and wants to free all memory allocated by Python. This
Guido van Rossum59a61351997-08-14 20:34:33 +0000213can be accomplished by calling \code{Py_Finalize()}.
214% XXX More...
215
216\section{Embedding Python in Threaded Applications}
217
Guido van Rossum4a944d71997-08-14 20:35:38 +0000218
219
220
221
222
223
224
225
Guido van Rossum59a61351997-08-14 20:34:33 +0000226
227\chapter{Old Introduction}
228
Guido van Rossumae110af1997-05-22 20:11:52 +0000229(XXX This is the old introduction, mostly by Jim Fulton -- should be
230rewritten.)
231
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000232From the viewpoint of of C access to Python services, we have:
233
234\begin{enumerate}
235
236\item "Very high level layer": two or three functions that let you
237exec or eval arbitrary Python code given as a string in a module whose
238name is given, passing C values in and getting C values out using
239mkvalue/getargs style format strings. This does not require the user
240to declare any variables of type \code{PyObject *}. This should be
241enough to write a simple application that gets Python code from the
242user, execs it, and returns the output or errors.
243
244\item "Abstract objects layer": which is the subject of this chapter.
Guido van Rossum59a61351997-08-14 20:34:33 +0000245It has many functions operating on objects, and lets you do many
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000246things from C that you can also write in Python, without going through
247the Python parser.
248
249\item "Concrete objects layer": This is the public type-dependent
250interface provided by the standard built-in types, such as floats,
251strings, and lists. This interface exists and is currently documented
252by the collection of include files provides with the Python
253distributions.
254
Guido van Rossumae110af1997-05-22 20:11:52 +0000255\end{enumerate}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000256
257From the point of view of Python accessing services provided by C
258modules:
259
Guido van Rossumae110af1997-05-22 20:11:52 +0000260\begin{enumerate}
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000261
Guido van Rossumae110af1997-05-22 20:11:52 +0000262\item[4.] "Python module interface": this interface consist of the basic
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000263routines used to define modules and their members. Most of the
264current extensions-writing guide deals with this interface.
265
Guido van Rossumae110af1997-05-22 20:11:52 +0000266\item[5.] "Built-in object interface": this is the interface that a new
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000267built-in type must provide and the mechanisms and rules that a
268developer of a new built-in type must use and follow.
269
270\end{enumerate}
271
272The Python C API provides four groups of operations on objects,
273corresponding to the same operations in the Python language: object,
274numeric, sequence, and mapping. Each protocol consists of a
275collection of related operations. If an operation that is not
276provided by a particular type is invoked, then the standard exception
277\code{TypeError} is raised with a operation name as an argument.
278
279In addition, for convenience this interface defines a set of
280constructors for building objects of built-in types. This is needed
281so new objects can be returned from C functions that otherwise treat
282objects generically.
283
284\section{Reference Counting}
285
286For most of the functions in the Python-C API, if a function retains a
287reference to a Python object passed as an argument, then the function
288will increase the reference count of the object. It is unnecessary
289for the caller to increase the reference count of an argument in
290anticipation of the object's retention.
291
292Usually, Python objects returned from functions should be treated as
293new objects. Functions that return objects assume that the caller
294will retain a reference and the reference count of the object has
295already been incremented to account for this fact. A caller that does
296not retain a reference to an object that is returned from a function
297must decrement the reference count of the object (using
298\code{Py_DECREF()}) to prevent memory leaks.
299
300Exceptions to these rules will be noted with the individual functions.
301
302\section{Include Files}
303
304All function, type and macro definitions needed to use the Python-C
305API are included in your code by the following line:
306
307\code{\#include "Python.h"}
308
309This implies inclusion of the following standard header files:
310stdio.h, string.h, errno.h, and stdlib.h (if available).
311
312All user visible names defined by Python.h (except those defined by
313the included standard headers) have one of the prefixes \code{Py} or
314\code{_Py}. Names beginning with \code{_Py} are for internal use
315only.
316
317
318\chapter{Initialization and Shutdown of an Embedded Python Interpreter}
319
320When embedding the Python interpreter in a C or C++ program, the
321interpreter must be initialized.
322
323\begin{cfuncdesc}{void}{PyInitialize}{}
324This function initializes the interpreter. It must be called before
325any interaction with the interpreter takes place. If it is called
326more than once, the second and further calls have no effect.
327
328The function performs the following tasks: create an environment in
329which modules can be imported and Python code can be executed;
330initialize the \code{__builtin__} module; initialize the \code{sys}
331module; initialize \code{sys.path}; initialize signal handling; and
332create the empty \code{__main__} module.
333
334In the current system, there is no way to undo all these
335initializations or to create additional interpreter environments.
336\end{cfuncdesc}
337
338\begin{cfuncdesc}{int}{Py_AtExit}{void (*func) ()}
339Register a cleanup function to be called when Python exits. The
340cleanup function will be called with no arguments and should return no
341value. At most 32 cleanup functions can be registered. When the
342registration is successful, \code{Py_AtExit} returns 0; on failure, it
343returns -1. Each cleanup function will be called t most once. The
344cleanup function registered last is called first.
345\end{cfuncdesc}
346
347\begin{cfuncdesc}{void}{Py_Exit}{int status}
348Exit the current process. This calls \code{Py_Cleanup()} (see next
349item) and performs additional cleanup (under some circumstances it
350will attempt to delete all modules), and then calls the standard C
351library function \code{exit(status)}.
352\end{cfuncdesc}
353
354\begin{cfuncdesc}{void}{Py_Cleanup}{}
355Perform some of the cleanup that \code{Py_Exit} performs, but don't
356exit the process. In particular, this invokes the user's
357\code{sys.exitfunc} function (if defined at all), and it invokes the
358cleanup functions registered with \code{Py_AtExit()}, in reverse order
359of their registration.
360\end{cfuncdesc}
361
362\begin{cfuncdesc}{void}{Py_FatalError}{char *message}
363Print a fatal error message and die. No cleanup is performed. This
364function should only be invoked when a condition is detected that
365would make it dangerous to continue using the Python interpreter;
366e.g., when the object administration appears to be corrupted.
367\end{cfuncdesc}
368
369\begin{cfuncdesc}{void}{PyImport_Init}{}
370Initialize the module table. For internal use only.
371\end{cfuncdesc}
372
373\begin{cfuncdesc}{void}{PyImport_Cleanup}{}
374Empty the module table. For internal use only.
375\end{cfuncdesc}
376
377\begin{cfuncdesc}{void}{PyBuiltin_Init}{}
378Initialize the \code{__builtin__} module. For internal use only.
379\end{cfuncdesc}
380
Guido van Rossumae110af1997-05-22 20:11:52 +0000381XXX Other init functions: PyEval_InitThreads, PyOS_InitInterrupts,
382PyMarshal_Init, PySys_Init.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000383
384\chapter{Reference Counting}
385
386The functions in this chapter are used for managing reference counts
387of Python objects.
388
389\begin{cfuncdesc}{void}{Py_INCREF}{PyObject *o}
390Increment the reference count for object \code{o}. The object must
391not be \NULL{}; if you aren't sure that it isn't \NULL{}, use
392\code{Py_XINCREF()}.
393\end{cfuncdesc}
394
395\begin{cfuncdesc}{void}{Py_XINCREF}{PyObject *o}
396Increment the reference count for object \code{o}. The object may be
397\NULL{}, in which case the function has no effect.
398\end{cfuncdesc}
399
400\begin{cfuncdesc}{void}{Py_DECREF}{PyObject *o}
401Decrement the reference count for object \code{o}. The object must
402not be \NULL{}; if you aren't sure that it isn't \NULL{}, use
403\code{Py_XDECREF()}. If the reference count reaches zero, the object's
404type's deallocation function (which must not be \NULL{}) is invoked.
405
406\strong{Warning:} The deallocation function can cause arbitrary Python
407code to be invoked (e.g. when a class instance with a \code{__del__()}
408method is deallocated). While exceptions in such code are not
409propagated, the executed code has free access to all Python global
410variables. This means that any object that is reachable from a global
411variable should be in a consistent state before \code{Py_DECREF()} is
412invoked. For example, code to delete an object from a list should
413copy a reference to the deleted object in a temporary variable, update
414the list data structure, and then call \code{Py_DECREF()} for the
415temporary variable.
416\end{cfuncdesc}
417
418\begin{cfuncdesc}{void}{Py_XDECREF}{PyObject *o}
419Decrement the reference count for object \code{o}.The object may be
420\NULL{}, in which case the function has no effect; otherwise the
421effect is the same as for \code{Py_DECREF()}, and the same warning
422applies.
423\end{cfuncdesc}
424
Guido van Rossumae110af1997-05-22 20:11:52 +0000425The following functions are only for internal use:
426\code{_Py_Dealloc}, \code{_Py_ForgetReference}, \code{_Py_NewReference},
427as well as the global variable \code{_Py_RefTotal}.
428
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000429
430\chapter{Exception Handling}
431
432The functions in this chapter will let you handle and raise Python
Guido van Rossumae110af1997-05-22 20:11:52 +0000433exceptions. It is important to understand some of the basics of
434Python exception handling. It works somewhat like the Unix
435\code{errno} variable: there is a global indicator (per thread) of the
436last error that occurred. Most functions don't clear this on success,
437but will set it to indicate the cause of the error on failure. Most
438functions also return an error indicator, usually \NULL{} if they are
439supposed to return a pointer, or -1 if they return an integer
440(exception: the \code{PyArg_Parse*()} functions return 1 for success and
4410 for failure). When a function must fail because of some function it
442called failed, it generally doesn't set the error indicator; the
443function it called already set it.
444
445The error indicator consists of three Python objects corresponding to
446the Python variables \code{sys.exc_type}, \code{sys.exc_value} and
447\code{sys.exc_traceback}. API functions exist to interact with the
448error indicator in various ways. There is a separate error indicator
449for each thread.
450
451% XXX Order of these should be more thoughtful.
452% Either alphabetical or some kind of structure.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000453
454\begin{cfuncdesc}{void}{PyErr_Print}{}
Guido van Rossumae110af1997-05-22 20:11:52 +0000455Print a standard traceback to \code{sys.stderr} and clear the error
456indicator. Call this function only when the error indicator is set.
457(Otherwise it will cause a fatal error!)
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000458\end{cfuncdesc}
459
Guido van Rossumae110af1997-05-22 20:11:52 +0000460\begin{cfuncdesc}{PyObject *}{PyErr_Occurred}{}
461Test whether the error indicator is set. If set, return the exception
462\code{type} (the first argument to the last call to one of the
463\code{PyErr_Set*()} functions or to \code{PyErr_Restore()}). If not
464set, return \NULL{}. You do not own a reference to the return value,
465so you do not need to \code{Py_DECREF()} it.
466\end{cfuncdesc}
467
468\begin{cfuncdesc}{void}{PyErr_Clear}{}
469Clear the error indicator. If the error indicator is not set, there
470is no effect.
471\end{cfuncdesc}
472
473\begin{cfuncdesc}{void}{PyErr_Fetch}{PyObject **ptype, PyObject **pvalue, PyObject **ptraceback}
474Retrieve the error indicator into three variables whose addresses are
475passed. If the error indicator is not set, set all three variables to
476\NULL{}. If it is set, it will be cleared and you own a reference to
477each object retrieved. The value and traceback object may be \NULL{}
478even when the type object is not. \strong{Note:} this function is
479normally only used by code that needs to handle exceptions or by code
480that needs to save and restore the error indicator temporarily.
481\end{cfuncdesc}
482
483\begin{cfuncdesc}{void}{PyErr_Restore}{PyObject *type, PyObject *value, PyObject *traceback}
484Set the error indicator from the three objects. If the error
485indicator is already set, it is cleared first. If the objects are
486\NULL{}, the error indicator is cleared. Do not pass a \NULL{} type
487and non-\NULL{} value or traceback. The exception type should be a
488string or class; if it is a class, the value should be an instance of
489that class. Do not pass an invalid exception type or value.
490(Violating these rules will cause subtle problems later.) This call
491takes away a reference to each object, i.e. you must own a reference
492to each object before the call and after the call you no longer own
493these references. (If you don't understand this, don't use this
494function. I warned you.) \strong{Note:} this function is normally
495only used by code that needs to save and restore the error indicator
496temporarily.
497\end{cfuncdesc}
498
499\begin{cfuncdesc}{void}{PyErr_SetString}{PyObject *type, char *message}
500This is the most common way to set the error indicator. The first
501argument specifies the exception type; it is normally one of the
502standard exceptions, e.g. \code{PyExc_RuntimeError}. You need not
503increment its reference count. The second argument is an error
504message; it is converted to a string object.
505\end{cfuncdesc}
506
507\begin{cfuncdesc}{void}{PyErr_SetObject}{PyObject *type, PyObject *value}
508This function is similar to \code{PyErr_SetString()} but lets you
509specify an arbitrary Python object for the ``value'' of the exception.
510You need not increment its reference count.
511\end{cfuncdesc}
512
513\begin{cfuncdesc}{void}{PyErr_SetNone}{PyObject *type}
514This is a shorthand for \code{PyErr_SetString(\var{type}, Py_None}.
515\end{cfuncdesc}
516
517\begin{cfuncdesc}{int}{PyErr_BadArgument}{}
518This is a shorthand for \code{PyErr_SetString(PyExc_TypeError,
519\var{message})}, where \var{message} indicates that a built-in operation
520was invoked with an illegal argument. It is mostly for internal use.
521\end{cfuncdesc}
522
523\begin{cfuncdesc}{PyObject *}{PyErr_NoMemory}{}
524This is a shorthand for \code{PyErr_SetNone(PyExc_MemoryError)}; it
525returns \NULL{} so an object allocation function can write
526\code{return PyErr_NoMemory();} when it runs out of memory.
527\end{cfuncdesc}
528
529\begin{cfuncdesc}{PyObject *}{PyErr_SetFromErrno}{PyObject *type}
530This is a convenience function to raise an exception when a C library
531function has returned an error and set the C variable \code{errno}.
532It constructs a tuple object whose first item is the integer
533\code{errno} value and whose second item is the corresponding error
534message (gotten from \code{strerror()}), and then calls
535\code{PyErr_SetObject(\var{type}, \var{object})}. On \UNIX{}, when
536the \code{errno} value is \code{EINTR}, indicating an interrupted
537system call, this calls \code{PyErr_CheckSignals()}, and if that set
538the error indicator, leaves it set to that. The function always
539returns \NULL{}, so a wrapper function around a system call can write
540\code{return PyErr_NoMemory();} when the system call returns an error.
541\end{cfuncdesc}
542
543\begin{cfuncdesc}{void}{PyErr_BadInternalCall}{}
544This is a shorthand for \code{PyErr_SetString(PyExc_TypeError,
545\var{message})}, where \var{message} indicates that an internal
546operation (e.g. a Python-C API function) was invoked with an illegal
547argument. It is mostly for internal use.
548\end{cfuncdesc}
549
550\begin{cfuncdesc}{int}{PyErr_CheckSignals}{}
551This function interacts with Python's signal handling. It checks
552whether a signal has been sent to the processes and if so, invokes the
553corresponding signal handler. If the \code{signal} module is
554supported, this can invoke a signal handler written in Python. In all
555cases, the default effect for \code{SIGINT} is to raise the
556\code{KeyboadInterrupt} exception. If an exception is raised the
557error indicator is set and the function returns 1; otherwise the
558function returns 0. The error indicator may or may not be cleared if
559it was previously set.
560\end{cfuncdesc}
561
562\begin{cfuncdesc}{void}{PyErr_SetInterrupt}{}
563This function is obsolete (XXX or platform dependent?). It simulates
564the effect of a \code{SIGINT} signal arriving -- the next time
565\code{PyErr_CheckSignals()} is called, \code{KeyboadInterrupt} will be
566raised.
567\end{cfuncdesc}
568
569\section{Standard Exceptions}
570
571All standard Python exceptions are available as global variables whose
572names are \code{PyExc_} followed by the Python exception name.
573These have the type \code{PyObject *}; they are all string objects.
574For completion, here are all the variables:
575\code{PyExc_AccessError},
576\code{PyExc_AssertionError},
577\code{PyExc_AttributeError},
578\code{PyExc_EOFError},
579\code{PyExc_FloatingPointError},
580\code{PyExc_IOError},
581\code{PyExc_ImportError},
582\code{PyExc_IndexError},
583\code{PyExc_KeyError},
584\code{PyExc_KeyboardInterrupt},
585\code{PyExc_MemoryError},
586\code{PyExc_NameError},
587\code{PyExc_OverflowError},
588\code{PyExc_RuntimeError},
589\code{PyExc_SyntaxError},
590\code{PyExc_SystemError},
591\code{PyExc_SystemExit},
592\code{PyExc_TypeError},
593\code{PyExc_ValueError},
594\code{PyExc_ZeroDivisionError}.
595
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000596
597\chapter{Utilities}
598
599The functions in this chapter perform various utility tasks, such as
600parsing function arguments and constructing Python values from C
601values.
602
603\begin{cfuncdesc}{int}{Py_FdIsInteractive}{FILE *fp, char *filename}
604Return true (nonzero) if the standard I/O file \code{fp} with name
605\code{filename} is deemed interactive. This is the case for files for
606which \code{isatty(fileno(fp))} is true. If the global flag
607\code{Py_InteractiveFlag} is true, this function also returns true if
608the \code{name} pointer is \NULL{} or if the name is equal to one of
609the strings \code{"<stdin>"} or \code{"???"}.
610\end{cfuncdesc}
611
612\begin{cfuncdesc}{long}{PyOS_GetLastModificationTime}{char *filename}
613Return the time of last modification of the file \code{filename}.
614The result is encoded in the same way as the timestamp returned by
615the standard C library function \code{time()}.
616\end{cfuncdesc}
617
618
619\chapter{Debugging}
620
621XXX Explain Py_DEBUG, Py_TRACE_REFS, Py_REF_DEBUG.
622
623
624\chapter{The Very High Level Layer}
625
626The functions in this chapter will let you execute Python source code
627given in a file or a buffer, but they will not let you interact in a
628more detailed way with the interpreter.
629
Guido van Rossumae110af1997-05-22 20:11:52 +0000630\begin{cfuncdesc}{int}{PyRun_AnyFile}{FILE *, char *}
631\end{cfuncdesc}
632
633\begin{cfuncdesc}{int}{PyRun_SimpleString}{char *}
634\end{cfuncdesc}
635
636\begin{cfuncdesc}{int}{PyRun_SimpleFile}{FILE *, char *}
637\end{cfuncdesc}
638
639\begin{cfuncdesc}{int}{PyRun_InteractiveOne}{FILE *, char *}
640\end{cfuncdesc}
641
642\begin{cfuncdesc}{int}{PyRun_InteractiveLoop}{FILE *, char *}
643\end{cfuncdesc}
644
645\begin{cfuncdesc}{struct _node *}{PyParser_SimpleParseString}{char *, int}
646\end{cfuncdesc}
647
648\begin{cfuncdesc}{struct _node *}{PyParser_SimpleParseFile}{FILE *, char *, int}
649\end{cfuncdesc}
650
651\begin{cfuncdesc}{}{PyObject *PyRun}{ROTO((char *, int, PyObject *, PyObject *}
652\end{cfuncdesc}
653
654\begin{cfuncdesc}{}{PyObject *PyRun}{ROTO((FILE *, char *, int, PyObject *, PyObject *}
655\end{cfuncdesc}
656
657\begin{cfuncdesc}{}{PyObject *Py}{ROTO((char *, char *, int}
658\end{cfuncdesc}
659
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000660
661\chapter{Abstract Objects Layer}
662
663The functions in this chapter interact with Python objects regardless
664of their type, or with wide classes of object types (e.g. all
665numerical types, or all sequence types). When used on object types
666for which they do not apply, they will flag a Python exception.
667
668\section{Object Protocol}
669
670\begin{cfuncdesc}{int}{PyObject_Print}{PyObject *o, FILE *fp, int flags}
671Print an object \code{o}, on file \code{fp}. Returns -1 on error
672The flags argument is used to enable certain printing
673options. The only option currently supported is \code{Py_Print_RAW}.
674\end{cfuncdesc}
675
676\begin{cfuncdesc}{int}{PyObject_HasAttrString}{PyObject *o, char *attr_name}
677Returns 1 if o has the attribute attr_name, and 0 otherwise.
678This is equivalent to the Python expression:
679\code{hasattr(o,attr_name)}.
680This function always succeeds.
681\end{cfuncdesc}
682
683\begin{cfuncdesc}{PyObject*}{PyObject_GetAttrString}{PyObject *o, char *attr_name}
Guido van Rossum59a61351997-08-14 20:34:33 +0000684Retrieve an attributed named attr_name from object o.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000685Returns the attribute value on success, or \NULL{} on failure.
686This is the equivalent of the Python expression: \code{o.attr_name}.
687\end{cfuncdesc}
688
689
690\begin{cfuncdesc}{int}{PyObject_HasAttr}{PyObject *o, PyObject *attr_name}
691Returns 1 if o has the attribute attr_name, and 0 otherwise.
692This is equivalent to the Python expression:
693\code{hasattr(o,attr_name)}.
694This function always succeeds.
695\end{cfuncdesc}
696
697
698\begin{cfuncdesc}{PyObject*}{PyObject_GetAttr}{PyObject *o, PyObject *attr_name}
699Retrieve an attributed named attr_name form object o.
700Returns the attribute value on success, or \NULL{} on failure.
701This is the equivalent of the Python expression: o.attr_name.
702\end{cfuncdesc}
703
704
705\begin{cfuncdesc}{int}{PyObject_SetAttrString}{PyObject *o, char *attr_name, PyObject *v}
706Set the value of the attribute named \code{attr_name}, for object \code{o},
707to the value \code{v}. Returns -1 on failure. This is
708the equivalent of the Python statement: \code{o.attr_name=v}.
709\end{cfuncdesc}
710
711
712\begin{cfuncdesc}{int}{PyObject_SetAttr}{PyObject *o, PyObject *attr_name, PyObject *v}
713Set the value of the attribute named \code{attr_name}, for
714object \code{o},
715to the value \code{v}. Returns -1 on failure. This is
716the equivalent of the Python statement: \code{o.attr_name=v}.
717\end{cfuncdesc}
718
719
720\begin{cfuncdesc}{int}{PyObject_DelAttrString}{PyObject *o, char *attr_name}
721Delete attribute named \code{attr_name}, for object \code{o}. Returns -1 on
722failure. This is the equivalent of the Python
723statement: \code{del o.attr_name}.
724\end{cfuncdesc}
725
726
727\begin{cfuncdesc}{int}{PyObject_DelAttr}{PyObject *o, PyObject *attr_name}
728Delete attribute named \code{attr_name}, for object \code{o}. Returns -1 on
729failure. This is the equivalent of the Python
730statement: \code{del o.attr_name}.
731\end{cfuncdesc}
732
733
734\begin{cfuncdesc}{int}{PyObject_Cmp}{PyObject *o1, PyObject *o2, int *result}
735Compare the values of \code{o1} and \code{o2} using a routine provided by
736\code{o1}, if one exists, otherwise with a routine provided by \code{o2}.
737The result of the comparison is returned in \code{result}. Returns
738-1 on failure. This is the equivalent of the Python
739statement: \code{result=cmp(o1,o2)}.
740\end{cfuncdesc}
741
742
743\begin{cfuncdesc}{int}{PyObject_Compare}{PyObject *o1, PyObject *o2}
744Compare the values of \code{o1} and \code{o2} using a routine provided by
745\code{o1}, if one exists, otherwise with a routine provided by \code{o2}.
746Returns the result of the comparison on success. On error,
747the value returned is undefined. This is equivalent to the
748Python expression: \code{cmp(o1,o2)}.
749\end{cfuncdesc}
750
751
752\begin{cfuncdesc}{PyObject*}{PyObject_Repr}{PyObject *o}
753Compute the string representation of object, \code{o}. Returns the
754string representation on success, \NULL{} on failure. This is
755the equivalent of the Python expression: \code{repr(o)}.
756Called by the \code{repr()} built-in function and by reverse quotes.
757\end{cfuncdesc}
758
759
760\begin{cfuncdesc}{PyObject*}{PyObject_Str}{PyObject *o}
761Compute the string representation of object, \code{o}. Returns the
762string representation on success, \NULL{} on failure. This is
763the equivalent of the Python expression: \code{str(o)}.
764Called by the \code{str()} built-in function and by the \code{print}
765statement.
766\end{cfuncdesc}
767
768
769\begin{cfuncdesc}{int}{PyCallable_Check}{PyObject *o}
770Determine if the object \code{o}, is callable. Return 1 if the
771object is callable and 0 otherwise.
772This function always succeeds.
773\end{cfuncdesc}
774
775
776\begin{cfuncdesc}{PyObject*}{PyObject_CallObject}{PyObject *callable_object, PyObject *args}
777Call a callable Python object \code{callable_object}, with
778arguments given by the tuple \code{args}. If no arguments are
779needed, then args may be \NULL{}. Returns the result of the
780call on success, or \NULL{} on failure. This is the equivalent
781of the Python expression: \code{apply(o, args)}.
782\end{cfuncdesc}
783
784\begin{cfuncdesc}{PyObject*}{PyObject_CallFunction}{PyObject *callable_object, char *format, ...}
785Call a callable Python object \code{callable_object}, with a
786variable number of C arguments. The C arguments are described
787using a mkvalue-style format string. The format may be \NULL{},
788indicating that no arguments are provided. Returns the
789result of the call on success, or \NULL{} on failure. This is
790the equivalent of the Python expression: \code{apply(o,args)}.
791\end{cfuncdesc}
792
793
794\begin{cfuncdesc}{PyObject*}{PyObject_CallMethod}{PyObject *o, char *m, char *format, ...}
795Call the method named \code{m} of object \code{o} with a variable number of
796C arguments. The C arguments are described by a mkvalue
797format string. The format may be \NULL{}, indicating that no
798arguments are provided. Returns the result of the call on
799success, or \NULL{} on failure. This is the equivalent of the
800Python expression: \code{o.method(args)}.
801Note that Special method names, such as "\code{__add__}",
802"\code{__getitem__}", and so on are not supported. The specific
803abstract-object routines for these must be used.
804\end{cfuncdesc}
805
806
807\begin{cfuncdesc}{int}{PyObject_Hash}{PyObject *o}
808Compute and return the hash value of an object \code{o}. On
809failure, return -1. This is the equivalent of the Python
810expression: \code{hash(o)}.
811\end{cfuncdesc}
812
813
814\begin{cfuncdesc}{int}{PyObject_IsTrue}{PyObject *o}
815Returns 1 if the object \code{o} is considered to be true, and
8160 otherwise. This is equivalent to the Python expression:
817\code{not not o}.
818This function always succeeds.
819\end{cfuncdesc}
820
821
822\begin{cfuncdesc}{PyObject*}{PyObject_Type}{PyObject *o}
823On success, returns a type object corresponding to the object
824type of object \code{o}. On failure, returns \NULL{}. This is
825equivalent to the Python expression: \code{type(o)}.
826\end{cfuncdesc}
827
828\begin{cfuncdesc}{int}{PyObject_Length}{PyObject *o}
829Return the length of object \code{o}. If the object \code{o} provides
830both sequence and mapping protocols, the sequence length is
831returned. On error, -1 is returned. This is the equivalent
832to the Python expression: \code{len(o)}.
833\end{cfuncdesc}
834
835
836\begin{cfuncdesc}{PyObject*}{PyObject_GetItem}{PyObject *o, PyObject *key}
837Return element of \code{o} corresponding to the object \code{key} or \NULL{}
838on failure. This is the equivalent of the Python expression:
839\code{o[key]}.
840\end{cfuncdesc}
841
842
843\begin{cfuncdesc}{int}{PyObject_SetItem}{PyObject *o, PyObject *key, PyObject *v}
844Map the object \code{key} to the value \code{v}.
845Returns -1 on failure. This is the equivalent
846of the Python statement: \code{o[key]=v}.
847\end{cfuncdesc}
848
849
850\begin{cfuncdesc}{int}{PyObject_DelItem}{PyObject *o, PyObject *key, PyObject *v}
851Delete the mapping for \code{key} from \code{*o}. Returns -1
852on failure.
Guido van Rossum59a61351997-08-14 20:34:33 +0000853This is the equivalent of the Python statement: \code{del o[key]}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000854\end{cfuncdesc}
855
856
857\section{Number Protocol}
858
859\begin{cfuncdesc}{int}{PyNumber_Check}{PyObject *o}
860Returns 1 if the object \code{o} provides numeric protocols, and
861false otherwise.
862This function always succeeds.
863\end{cfuncdesc}
864
865
866\begin{cfuncdesc}{PyObject*}{PyNumber_Add}{PyObject *o1, PyObject *o2}
867Returns the result of adding \code{o1} and \code{o2}, or null on failure.
868This is the equivalent of the Python expression: \code{o1+o2}.
869\end{cfuncdesc}
870
871
872\begin{cfuncdesc}{PyObject*}{PyNumber_Subtract}{PyObject *o1, PyObject *o2}
873Returns the result of subtracting \code{o2} from \code{o1}, or null on
874failure. This is the equivalent of the Python expression:
875\code{o1-o2}.
876\end{cfuncdesc}
877
878
879\begin{cfuncdesc}{PyObject*}{PyNumber_Multiply}{PyObject *o1, PyObject *o2}
880Returns the result of multiplying \code{o1} and \code{o2}, or null on
881failure. This is the equivalent of the Python expression:
882\code{o1*o2}.
883\end{cfuncdesc}
884
885
886\begin{cfuncdesc}{PyObject*}{PyNumber_Divide}{PyObject *o1, PyObject *o2}
887Returns the result of dividing \code{o1} by \code{o2}, or null on failure.
888This is the equivalent of the Python expression: \code{o1/o2}.
889\end{cfuncdesc}
890
891
892\begin{cfuncdesc}{PyObject*}{PyNumber_Remainder}{PyObject *o1, PyObject *o2}
893Returns the remainder of dividing \code{o1} by \code{o2}, or null on
894failure. This is the equivalent of the Python expression:
895\code{o1\%o2}.
896\end{cfuncdesc}
897
898
899\begin{cfuncdesc}{PyObject*}{PyNumber_Divmod}{PyObject *o1, PyObject *o2}
900See the built-in function divmod. Returns \NULL{} on failure.
901This is the equivalent of the Python expression:
902\code{divmod(o1,o2)}.
903\end{cfuncdesc}
904
905
906\begin{cfuncdesc}{PyObject*}{PyNumber_Power}{PyObject *o1, PyObject *o2, PyObject *o3}
907See the built-in function pow. Returns \NULL{} on failure.
908This is the equivalent of the Python expression:
909\code{pow(o1,o2,o3)}, where \code{o3} is optional.
910\end{cfuncdesc}
911
912
913\begin{cfuncdesc}{PyObject*}{PyNumber_Negative}{PyObject *o}
914Returns the negation of \code{o} on success, or null on failure.
915This is the equivalent of the Python expression: \code{-o}.
916\end{cfuncdesc}
917
918
919\begin{cfuncdesc}{PyObject*}{PyNumber_Positive}{PyObject *o}
920Returns \code{o} on success, or \NULL{} on failure.
921This is the equivalent of the Python expression: \code{+o}.
922\end{cfuncdesc}
923
924
925\begin{cfuncdesc}{PyObject*}{PyNumber_Absolute}{PyObject *o}
926Returns the absolute value of \code{o}, or null on failure. This is
927the equivalent of the Python expression: \code{abs(o)}.
928\end{cfuncdesc}
929
930
931\begin{cfuncdesc}{PyObject*}{PyNumber_Invert}{PyObject *o}
932Returns the bitwise negation of \code{o} on success, or \NULL{} on
933failure. This is the equivalent of the Python expression:
Guido van Rossum59a61351997-08-14 20:34:33 +0000934\code{\~o}.
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000935\end{cfuncdesc}
936
937
938\begin{cfuncdesc}{PyObject*}{PyNumber_Lshift}{PyObject *o1, PyObject *o2}
939Returns the result of left shifting \code{o1} by \code{o2} on success, or
940\NULL{} on failure. This is the equivalent of the Python
941expression: \code{o1 << o2}.
942\end{cfuncdesc}
943
944
945\begin{cfuncdesc}{PyObject*}{PyNumber_Rshift}{PyObject *o1, PyObject *o2}
946Returns the result of right shifting \code{o1} by \code{o2} on success, or
947\NULL{} on failure. This is the equivalent of the Python
948expression: \code{o1 >> o2}.
949\end{cfuncdesc}
950
951
952\begin{cfuncdesc}{PyObject*}{PyNumber_And}{PyObject *o1, PyObject *o2}
953Returns the result of "anding" \code{o2} and \code{o2} on success and \NULL{}
954on failure. This is the equivalent of the Python
955expression: \code{o1 and o2}.
956\end{cfuncdesc}
957
958
959\begin{cfuncdesc}{PyObject*}{PyNumber_Xor}{PyObject *o1, PyObject *o2}
960Returns the bitwise exclusive or of \code{o1} by \code{o2} on success, or
961\NULL{} on failure. This is the equivalent of the Python
962expression: \code{o1\^{ }o2}.
963\end{cfuncdesc}
964
965\begin{cfuncdesc}{PyObject*}{PyNumber_Or}{PyObject *o1, PyObject *o2}
Guido van Rossum59a61351997-08-14 20:34:33 +0000966Returns the result of \code{o1} and \code{o2} on success, or \NULL{} on
Guido van Rossum9231c8f1997-05-15 21:43:21 +0000967failure. This is the equivalent of the Python expression:
968\code{o1 or o2}.
969\end{cfuncdesc}
970
971
972\begin{cfuncdesc}{PyObject*}{PyNumber_Coerce}{PyObject *o1, PyObject *o2}
973This function takes the addresses of two variables of type
974\code{PyObject*}.
975
976If the objects pointed to by \code{*p1} and \code{*p2} have the same type,
977increment their reference count and return 0 (success).
978If the objects can be converted to a common numeric type,
979replace \code{*p1} and \code{*p2} by their converted value (with 'new'
980reference counts), and return 0.
981If no conversion is possible, or if some other error occurs,
982return -1 (failure) and don't increment the reference counts.
983The call \code{PyNumber_Coerce(\&o1, \&o2)} is equivalent to the Python
984statement \code{o1, o2 = coerce(o1, o2)}.
985\end{cfuncdesc}
986
987
988\begin{cfuncdesc}{PyObject*}{PyNumber_Int}{PyObject *o}
989Returns the \code{o} converted to an integer object on success, or
990\NULL{} on failure. This is the equivalent of the Python
991expression: \code{int(o)}.
992\end{cfuncdesc}
993
994
995\begin{cfuncdesc}{PyObject*}{PyNumber_Long}{PyObject *o}
996Returns the \code{o} converted to a long integer object on success,
997or \NULL{} on failure. This is the equivalent of the Python
998expression: \code{long(o)}.
999\end{cfuncdesc}
1000
1001
1002\begin{cfuncdesc}{PyObject*}{PyNumber_Float}{PyObject *o}
1003Returns the \code{o} converted to a float object on success, or \NULL{}
1004on failure. This is the equivalent of the Python expression:
1005\code{float(o)}.
1006\end{cfuncdesc}
1007
1008
1009\section{Sequence protocol}
1010
1011\begin{cfuncdesc}{int}{PySequence_Check}{PyObject *o}
1012Return 1 if the object provides sequence protocol, and 0
1013otherwise.
1014This function always succeeds.
1015\end{cfuncdesc}
1016
1017
1018\begin{cfuncdesc}{PyObject*}{PySequence_Concat}{PyObject *o1, PyObject *o2}
1019Return the concatination of \code{o1} and \code{o2} on success, and \NULL{} on
1020failure. This is the equivalent of the Python
1021expression: \code{o1+o2}.
1022\end{cfuncdesc}
1023
1024
1025\begin{cfuncdesc}{PyObject*}{PySequence_Repeat}{PyObject *o, int count}
Guido van Rossum59a61351997-08-14 20:34:33 +00001026Return the result of repeating sequence object \code{o} \code{count} times,
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001027or \NULL{} on failure. This is the equivalent of the Python
1028expression: \code{o*count}.
1029\end{cfuncdesc}
1030
1031
1032\begin{cfuncdesc}{PyObject*}{PySequence_GetItem}{PyObject *o, int i}
1033Return the ith element of \code{o}, or \NULL{} on failure. This is the
1034equivalent of the Python expression: \code{o[i]}.
1035\end{cfuncdesc}
1036
1037
1038\begin{cfuncdesc}{PyObject*}{PySequence_GetSlice}{PyObject *o, int i1, int i2}
1039Return the slice of sequence object \code{o} between \code{i1} and \code{i2}, or
1040\NULL{} on failure. This is the equivalent of the Python
1041expression, \code{o[i1:i2]}.
1042\end{cfuncdesc}
1043
1044
1045\begin{cfuncdesc}{int}{PySequence_SetItem}{PyObject *o, int i, PyObject *v}
1046Assign object \code{v} to the \code{i}th element of \code{o}.
1047Returns -1 on failure. This is the equivalent of the Python
1048statement, \code{o[i]=v}.
1049\end{cfuncdesc}
1050
1051\begin{cfuncdesc}{int}{PySequence_DelItem}{PyObject *o, int i}
1052Delete the \code{i}th element of object \code{v}. Returns
1053-1 on failure. This is the equivalent of the Python
1054statement: \code{del o[i]}.
1055\end{cfuncdesc}
1056
1057\begin{cfuncdesc}{int}{PySequence_SetSlice}{PyObject *o, int i1, int i2, PyObject *v}
1058Assign the sequence object \code{v} to the slice in sequence
1059object \code{o} from \code{i1} to \code{i2}. This is the equivalent of the Python
1060statement, \code{o[i1:i2]=v}.
1061\end{cfuncdesc}
1062
1063\begin{cfuncdesc}{int}{PySequence_DelSlice}{PyObject *o, int i1, int i2}
1064Delete the slice in sequence object, \code{o}, from \code{i1} to \code{i2}.
1065Returns -1 on failure. This is the equivalent of the Python
1066statement: \code{del o[i1:i2]}.
1067\end{cfuncdesc}
1068
1069\begin{cfuncdesc}{PyObject*}{PySequence_Tuple}{PyObject *o}
1070Returns the \code{o} as a tuple on success, and \NULL{} on failure.
1071This is equivalent to the Python expression: \code{tuple(o)}.
1072\end{cfuncdesc}
1073
1074\begin{cfuncdesc}{int}{PySequence_Count}{PyObject *o, PyObject *value}
1075Return the number of occurrences of \code{value} on \code{o}, that is,
1076return the number of keys for which \code{o[key]==value}. On
1077failure, return -1. This is equivalent to the Python
1078expression: \code{o.count(value)}.
1079\end{cfuncdesc}
1080
1081\begin{cfuncdesc}{int}{PySequence_In}{PyObject *o, PyObject *value}
1082Determine if \code{o} contains \code{value}. If an item in \code{o} is equal to
1083\code{value}, return 1, otherwise return 0. On error, return -1. This
1084is equivalent to the Python expression: \code{value in o}.
1085\end{cfuncdesc}
1086
1087\begin{cfuncdesc}{int}{PySequence_Index}{PyObject *o, PyObject *value}
Guido van Rossum59a61351997-08-14 20:34:33 +00001088Return the first index for which \code{o[i]==value}. On error,
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001089return -1. This is equivalent to the Python
1090expression: \code{o.index(value)}.
1091\end{cfuncdesc}
1092
1093\section{Mapping protocol}
1094
1095\begin{cfuncdesc}{int}{PyMapping_Check}{PyObject *o}
1096Return 1 if the object provides mapping protocol, and 0
1097otherwise.
1098This function always succeeds.
1099\end{cfuncdesc}
1100
1101
1102\begin{cfuncdesc}{int}{PyMapping_Length}{PyObject *o}
1103Returns the number of keys in object \code{o} on success, and -1 on
1104failure. For objects that do not provide sequence protocol,
1105this is equivalent to the Python expression: \code{len(o)}.
1106\end{cfuncdesc}
1107
1108
1109\begin{cfuncdesc}{int}{PyMapping_DelItemString}{PyObject *o, char *key}
1110Remove the mapping for object \code{key} from the object \code{o}.
1111Return -1 on failure. This is equivalent to
1112the Python statement: \code{del o[key]}.
1113\end{cfuncdesc}
1114
1115
1116\begin{cfuncdesc}{int}{PyMapping_DelItem}{PyObject *o, PyObject *key}
1117Remove the mapping for object \code{key} from the object \code{o}.
1118Return -1 on failure. This is equivalent to
1119the Python statement: \code{del o[key]}.
1120\end{cfuncdesc}
1121
1122
1123\begin{cfuncdesc}{int}{PyMapping_HasKeyString}{PyObject *o, char *key}
1124On success, return 1 if the mapping object has the key \code{key}
1125and 0 otherwise. This is equivalent to the Python expression:
1126\code{o.has_key(key)}.
1127This function always succeeds.
1128\end{cfuncdesc}
1129
1130
1131\begin{cfuncdesc}{int}{PyMapping_HasKey}{PyObject *o, PyObject *key}
1132Return 1 if the mapping object has the key \code{key}
1133and 0 otherwise. This is equivalent to the Python expression:
1134\code{o.has_key(key)}.
1135This function always succeeds.
1136\end{cfuncdesc}
1137
1138
1139\begin{cfuncdesc}{PyObject*}{PyMapping_Keys}{PyObject *o}
1140On success, return a list of the keys in object \code{o}. On
1141failure, return \NULL{}. This is equivalent to the Python
1142expression: \code{o.keys()}.
1143\end{cfuncdesc}
1144
1145
1146\begin{cfuncdesc}{PyObject*}{PyMapping_Values}{PyObject *o}
1147On success, return a list of the values in object \code{o}. On
1148failure, return \NULL{}. This is equivalent to the Python
1149expression: \code{o.values()}.
1150\end{cfuncdesc}
1151
1152
1153\begin{cfuncdesc}{PyObject*}{PyMapping_Items}{PyObject *o}
1154On success, return a list of the items in object \code{o}, where
1155each item is a tuple containing a key-value pair. On
1156failure, return \NULL{}. This is equivalent to the Python
1157expression: \code{o.items()}.
1158\end{cfuncdesc}
1159
1160\begin{cfuncdesc}{int}{PyMapping_Clear}{PyObject *o}
1161Make object \code{o} empty. Returns 1 on success and 0 on failure.
1162This is equivalent to the Python statement:
1163\code{for key in o.keys(): del o[key]}
1164\end{cfuncdesc}
1165
1166
1167\begin{cfuncdesc}{PyObject*}{PyMapping_GetItemString}{PyObject *o, char *key}
1168Return element of \code{o} corresponding to the object \code{key} or \NULL{}
1169on failure. This is the equivalent of the Python expression:
1170\code{o[key]}.
1171\end{cfuncdesc}
1172
1173\begin{cfuncdesc}{PyObject*}{PyMapping_SetItemString}{PyObject *o, char *key, PyObject *v}
1174Map the object \code{key} to the value \code{v} in object \code{o}. Returns
1175-1 on failure. This is the equivalent of the Python
1176statement: \code{o[key]=v}.
1177\end{cfuncdesc}
1178
1179
1180\section{Constructors}
1181
1182\begin{cfuncdesc}{PyObject*}{PyFile_FromString}{char *file_name, char *mode}
1183On success, returns a new file object that is opened on the
1184file given by \code{file_name}, with a file mode given by \code{mode},
1185where \code{mode} has the same semantics as the standard C routine,
1186fopen. On failure, return -1.
1187\end{cfuncdesc}
1188
1189\begin{cfuncdesc}{PyObject*}{PyFile_FromFile}{FILE *fp, char *file_name, char *mode, int close_on_del}
1190Return a new file object for an already opened standard C
1191file pointer, \code{fp}. A file name, \code{file_name}, and open mode,
1192\code{mode}, must be provided as well as a flag, \code{close_on_del}, that
1193indicates whether the file is to be closed when the file
1194object is destroyed. On failure, return -1.
1195\end{cfuncdesc}
1196
1197\begin{cfuncdesc}{PyObject*}{PyFloat_FromDouble}{double v}
1198Returns a new float object with the value \code{v} on success, and
1199\NULL{} on failure.
1200\end{cfuncdesc}
1201
1202\begin{cfuncdesc}{PyObject*}{PyInt_FromLong}{long v}
1203Returns a new int object with the value \code{v} on success, and
1204\NULL{} on failure.
1205\end{cfuncdesc}
1206
1207\begin{cfuncdesc}{PyObject*}{PyList_New}{int l}
1208Returns a new list of length \code{l} on success, and \NULL{} on
1209failure.
1210\end{cfuncdesc}
1211
1212\begin{cfuncdesc}{PyObject*}{PyLong_FromLong}{long v}
1213Returns a new long object with the value \code{v} on success, and
1214\NULL{} on failure.
1215\end{cfuncdesc}
1216
1217\begin{cfuncdesc}{PyObject*}{PyLong_FromDouble}{double v}
1218Returns a new long object with the value \code{v} on success, and
1219\NULL{} on failure.
1220\end{cfuncdesc}
1221
1222\begin{cfuncdesc}{PyObject*}{PyDict_New}{}
1223Returns a new empty dictionary on success, and \NULL{} on
1224failure.
1225\end{cfuncdesc}
1226
1227\begin{cfuncdesc}{PyObject*}{PyString_FromString}{char *v}
1228Returns a new string object with the value \code{v} on success, and
1229\NULL{} on failure.
1230\end{cfuncdesc}
1231
1232\begin{cfuncdesc}{PyObject*}{PyString_FromStringAndSize}{char *v, int l}
1233Returns a new string object with the value \code{v} and length \code{l}
1234on success, and \NULL{} on failure.
1235\end{cfuncdesc}
1236
1237\begin{cfuncdesc}{PyObject*}{PyTuple_New}{int l}
1238Returns a new tuple of length \code{l} on success, and \NULL{} on
1239failure.
1240\end{cfuncdesc}
1241
1242
1243\chapter{Concrete Objects Layer}
1244
1245The functions in this chapter are specific to certain Python object
1246types. Passing them an object of the wrong type is not a good idea;
1247if you receive an object from a Python program and you are not sure
1248that it has the right type, you must perform a type check first;
1249e.g. to check that an object is a dictionary, use
1250\code{PyDict_Check()}.
1251
1252
1253\chapter{Defining New Object Types}
1254
1255\begin{cfuncdesc}{PyObject *}{_PyObject_New}{PyTypeObject *type}
1256\end{cfuncdesc}
1257
Guido van Rossumae110af1997-05-22 20:11:52 +00001258\begin{cfuncdesc}{PyObject *}{_PyObject_NewVar}{PyTypeObject *type, int size}
Guido van Rossum9231c8f1997-05-15 21:43:21 +00001259\end{cfuncdesc}
1260
Guido van Rossumae110af1997-05-22 20:11:52 +00001261\begin{cfuncdesc}{TYPE}{_PyObject_NEW}{TYPE, PyTypeObject *}
1262\end{cfuncdesc}
1263
1264\begin{cfuncdesc}{TYPE}{_PyObject_NEW_VAR}{TYPE, PyTypeObject *, int size}
1265\end{cfuncdesc}
1266
Guido van Rossum4a944d71997-08-14 20:35:38 +00001267\chapter{Initialization, Finalization, and Threads}
1268
1269% XXX Check argument/return type of all these
1270
1271\begin{cfuncdesc}{void}{Py_Initialize}{}
1272Initialize the Python interpreter. In an application embedding
1273Python, this should be called before using any other Python/C API
1274functions; with the exception of \code{Py_SetProgramName()},
1275\code{PyEval_InitThreads()}, \code{PyEval_ReleaseLock()}, and
1276\code{PyEval_AcquireLock()}. This initializes the table of loaded
1277modules (\code{sys.modules}), and creates the fundamental modules
1278\code{__builtin__}, \code{__main__} and \code{sys}. It also
1279initializes the module search path (\code{sys.path}). It does not set
1280\code{sys.argv}; use \code{PySys_SetArgv()} for that. It is a fatal
1281error to call it for a second time without calling
1282\code{Py_Finalize()} first. There is no return value; it is a fatal
1283error if the initialization fails.
1284\end{cfuncdesc}
1285
1286\begin{cfuncdesc}{void}{Py_Finalize}{}
1287Undo all initializations made by \code{Py_Initialize()} and subsequent
1288use of Python/C API functions, and destroy all sub-interpreters (see
1289\code{Py_NewInterpreter()} below) that were created and not yet
1290destroyed since the last call to \code{Py_Initialize()}. Ideally,
1291this frees all memory allocated by the Python interpreter. It is a
1292fatal error to call it for a second time without calling
1293\code{Py_Initialize()} again first. There is no return value; errors
1294during finalization are ignored.
1295
1296This function is provided for a number of reasons. An embedding
1297application might want to restart Python without having to restart the
1298application itself. An application that has loaded the Python
1299interpreter from a dynamically loadable library (or DLL) might want to
1300free all memory allocated by Python before unloading the DLL. During a
1301hunt for memory leaks in an application a developer might want to free
1302all memory allocated by Python before exiting from the application.
1303
1304\emph{Bugs and caveats:} The destruction of modules and objects in
1305modules is done in random order; this may cause destructors
1306(\code{__del__} methods) to fail when they depend on other objects
1307(even functions) or modules. Dynamically loaded extension modules
1308loaded by Python are not unloaded. Small amounts of memory allocated
1309by the Python interpreter may not be freed (if you find a leak, please
1310report it). Memory tied up in circular references between objects is
1311not freed. Some memory allocated by extension modules may not be
1312freed. Some extension may not work properly if their initialization
1313routine is called more than once; this can happen if an applcation
1314calls \code{Py_Initialize()} and \code{Py_Finalize()} more than once.
1315\end{cfuncdesc}
1316
1317\begin{cfuncdesc}{PyThreadState *}{Py_NewInterpreter}{}
1318Create a new sub-interpreter. This is an (almost) totally separate
1319environment for the execution of Python code. In particular, the new
1320interpreter has separate, independent versions of all imported
1321modules, including the fundamental modules \code{__builtin__},
1322\code{__main__} and \code{sys}. The table of loaded modules
1323(\code{sys.modules}) and the module search path (\code{sys.path}) are
1324also separate. The new environment has no \code{sys.argv} variable.
1325It has new standard I/O stream file objects \code{sys.stdin},
1326\code{sys.stdout} and \code{sys.stderr} (however these refer to the
1327same underlying \code{FILE} structures in the C library).
1328
1329The return value points to the first thread state created in the new
1330sub-interpreter. This thread state is made the current thread state.
1331Note that no actual thread is created; see the discussion of thread
1332states below. If creation of the new interpreter is unsuccessful,
1333\code{NULL} is returned; no exception is set since the exception state
1334is stored in the current thread state and there may not be a current
1335thread state. (Like all other Python/C API functions, the global
1336interpreter lock must be held before calling this function and is
1337still held when it returns; however, unlike most other Python/C API
1338functions, there needn't be a current thread state on entry.)
1339
1340Extension modules are shared between (sub-)interpreters as follows:
1341the first time a particular extension is imported, it is initialized
1342normally, and a (shallow) copy of its module's dictionary is
1343squirreled away. When the same extension is imported by another
1344(sub-)interpreter, a new module is initialized and filled with the
1345contents of this copy; the extension's \code{init} function is not
1346called. Note that this is different from what happens when as
1347extension is imported after the interpreter has been completely
1348re-initialized by calling \code{Py_Finalize()} and
1349\code{Py_Initialize()}; in that case, the extension's \code{init}
1350function \emph{is} called again.
1351
1352\emph{Bugs and caveats:} Because sub-interpreters (and the main
1353interpreter) are part of the same process, the insulation between them
1354isn't perfect -- for example, using low-level file operations like
1355\code{os.close()} they can (accidentally or maliciously) affect each
1356other's open files. Because of the way extensions are shared between
1357(sub-)interpreters, some extensions may not work properly; this is
1358especially likely when the extension makes use of (static) global
1359variables, or when the extension manipulates its module's dictionary
1360after its initialization. It is possible to insert objects created in
1361one sub-interpreter into a namespace of another sub-interpreter; this
1362should be done with great care to avoid sharing user-defined
1363functions, methods, instances or classes between sub-interpreters,
1364since import operations executed by such objects may affect the
1365wrong (sub-)interpreter's dictionary of loaded modules. (XXX This is
1366a hard-to-fix bug that will be addressed in a future release.)
1367\end{cfuncdesc}
1368
1369\begin{cfuncdesc}{void}{Py_EndInterpreter}{PyThreadState *tstate}
1370Destroy the (sub-)interpreter represented by the given thread state.
1371The given thread state must be the current thread state. See the
1372discussion of thread states below. When the call returns, the current
1373thread state is \code{NULL}. All thread states associated with this
1374interpreted are destroyed. (The global interpreter lock must be held
1375before calling this function and is still held when it returns.)
1376\code{Py_Finalize()} will destroy all sub-interpreters that haven't
1377been explicitly destroyed at that point.
1378\end{cfuncdesc}
1379
1380\begin{cfuncdesc}{void}{Py_SetProgramName}{char *name}
1381This function should be called before \code{Py_Initialize()} is called
1382for the first time, if it is called at all. It tells the interpreter
1383the value of the \code{argv[0]} argument to the \code{main()} function
1384of the program. This is used by \code{Py_GetPath()} and some other
1385functions below to find the Python run-time libraries relative to the
1386interpreter executable. The default value is \code{"python"}. The
1387argument should point to a zero-terminated character string in static
1388storage whose contents will not change for the duration of the
1389program's execution. No code in the Python interpreter will change
1390the contents of this storage.
1391\end{cfuncdesc}
1392
1393\begin{cfuncdesc}{char *}{Py_GetProgramName}{}
1394Return the program name set with \code{Py_SetProgramName()}, or the
1395default. The returned string points into static storage; the caller
1396should not modify its value.
1397\end{cfuncdesc}
1398
1399\begin{cfuncdesc}{char *}{Py_GetPrefix}{}
1400Return the ``prefix'' for installed platform-independent files. This
1401is derived through a number of complicated rules from the program name
1402set with \code{Py_SetProgramName()} and some environment variables;
1403for example, if the program name is \code{"/usr/local/bin/python"},
1404the prefix is \code{"/usr/local"}. The returned string points into
1405static storage; the caller should not modify its value. This
1406corresponds to the \code{prefix} variable in the top-level
1407\code{Makefile} and the \code{--prefix} argument to the
1408\code{configure} script at build time. The value is available to
1409Python code as \code{sys.prefix}. It is only useful on Unix. See
1410also the next function.
1411\end{cfuncdesc}
1412
1413\begin{cfuncdesc}{char *}{Py_GetExecPrefix}{}
1414Return the ``exec-prefix'' for installed platform-\emph{de}pendent
1415files. This is derived through a number of complicated rules from the
1416program name set with \code{Py_SetProgramName()} and some environment
1417variables; for example, if the program name is
1418\code{"/usr/local/bin/python"}, the exec-prefix is
1419\code{"/usr/local"}. The returned string points into static storage;
1420the caller should not modify its value. This corresponds to the
1421\code{exec_prefix} variable in the top-level \code{Makefile} and the
1422\code{--exec_prefix} argument to the \code{configure} script at build
1423time. The value is available to Python code as
1424\code{sys.exec_prefix}. It is only useful on Unix.
1425
1426Background: The exec-prefix differs from the prefix when platform
1427dependent files (such as executables and shared libraries) are
1428installed in a different directory tree. In a typical installation,
1429platform dependent files may be installed in the
1430\code{"/usr/local/plat"} subtree while platform independent may be
1431installed in \code{"/usr/local"}.
1432
1433Generally speaking, a platform is a combination of hardware and
1434software families, e.g. Sparc machines running the Solaris 2.x
1435operating system are considered the same platform, but Intel machines
1436running Solaris 2.x are another platform, and Intel machines running
1437Linux are yet another platform. Different major revisions of the same
1438operating system generally also form different platforms. Non-Unix
1439operating systems are a different story; the installation strategies
1440on those systems are so different that the prefix and exec-prefix are
1441meaningless, and set to the empty string. Note that compiled Python
1442bytecode files are platform independent (but not independent from the
1443Python version by which they were compiled!).
1444
1445System administrators will know how to configure the \code{mount} or
1446\code{automount} programs to share \code{"/usr/local"} between platforms
1447while having \code{"/usr/local/plat"} be a different filesystem for each
1448platform.
1449\end{cfuncdesc}
1450
1451\begin{cfuncdesc}{char *}{Py_GetProgramFullPath}{}
1452Return the full program name of the Python executable; this is
1453computed as a side-effect of deriving the default module search path
1454from the program name (set by \code{Py_SetProgramName() above). The
1455returned string points into static storage; the caller should not
1456modify its value. The value is available to Python code as
1457\code{sys.executable}. % XXX is that the right sys.name?
1458\end{cfuncdesc}
1459
1460\begin{cfuncdesc}{char *}{Py_GetPath}{}
1461Return the default module search path; this is computed from the
1462program name (set by \code{Py_SetProgramName() above) and some
1463environment variables. The returned string consists of a series of
1464directory names separated by a platform dependent delimiter character.
1465The delimiter character is \code{':'} on Unix, \code{';'} on
1466DOS/Windows, and \code{'\n'} (the ASCII newline character) on
1467Macintosh. The returned string points into static storage; the caller
1468should not modify its value. The value is available to Python code
1469as the list \code{sys.path}, which may be modified to change the
1470future search path for loaded modules.
1471
1472% XXX should give the exact rules
1473\end{cfuncdesc}
1474
1475\begin{cfuncdesc}{const char *}{Py_GetVersion}{}
1476Return the version of this Python interpreter. This is a string that
1477looks something like
1478
1479\code{"1.5a3 (#67, Aug 1 1997, 22:34:28) [GCC 2.7.2.2]"}.
1480
1481The first word (up to the first space character) is the current Python
1482version; the first three characters are the major and minor version
1483separated by a period. The returned string points into static storage;
1484the caller should not modify its value. The value is available to
1485Python code as the list \code{sys.version}.
1486\end{cfuncdesc}
1487
1488\begin{cfuncdesc}{const char *}{Py_GetPlatform}{}
1489Return the platform identifier for the current platform. On Unix,
1490this is formed from the ``official'' name of the operating system,
1491converted to lower case, followed by the major revision number; e.g.,
1492for Solaris 2.x, which is also known as SunOS 5.x, the value is
1493\code{"sunos5"}. On Macintosh, it is \code{"mac"}. On Windows, it
1494is \code{"win"}. The returned string points into static storage;
1495the caller should not modify its value. The value is available to
1496Python code as \code{sys.platform}.
1497\end{cfuncdesc}
1498
1499\begin{cfuncdesc}{const char *}{Py_GetCopyright}{}
1500Return the official copyright string for the current Python version,
1501for example
1502
1503\code{"Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam"}
1504
1505The returned string points into static storage; the caller should not
1506modify its value. The value is available to Python code as the list
1507\code{sys.copyright}.
1508\end{cfuncdesc}
1509
1510\begin{cfuncdesc}{const char *}{Py_GetCompiler}{}
1511Return an indication of the compiler used to build the current Python
1512version, in square brackets, for example
1513
1514\code{"[GCC 2.7.2.2]"}
1515
1516The returned string points into static storage; the caller should not
1517modify its value. The value is available to Python code as part of
1518the variable \code{sys.version}.
1519\end{cfuncdesc}
1520
1521\begin{cfuncdesc}{const char *}{Py_GetBuildInfo}{}
1522Return information about the sequence number and build date and time
1523of the current Python interpreter instance, for example
1524
1525\code{"#67, Aug 1 1997, 22:34:28"}
1526
1527The returned string points into static storage; the caller should not
1528modify its value. The value is available to Python code as part of
1529the variable \code{sys.version}.
1530\end{cfuncdesc}
1531
1532\begin{cfuncdesc}{int}{PySys_SetArgv}{int argc, char **argv}
1533% XXX
1534\end{cfuncdesc}
1535
1536% XXX Other PySys thingies (doesn't really belong in this chapter)
1537
1538\section{Thread State and the Global Interpreter Lock}
1539
1540\begin{cfuncdesc}{void}{PyEval_AcquireLock}{}
1541\end{cfuncdesc}
1542
1543\begin{cfuncdesc}{void}{PyEval_ReleaseLock}{}
1544\end{cfuncdesc}
1545
1546\begin{cfuncdesc}{void}{PyEval_AcquireThread}{PyThreadState *tstate}
1547\end{cfuncdesc}
1548
1549\begin{cfuncdesc}{void}{PyEval_ReleaseThread}{PyThreadState *tstate}
1550\end{cfuncdesc}
1551
1552\begin{cfuncdesc}{void}{PyEval_RestoreThread}{PyThreadState *tstate}
1553\end{cfuncdesc}
1554
1555\begin{cfuncdesc}{PyThreadState *}{PyEval_SaveThread}{}
1556\end{cfuncdesc}
1557
1558% XXX These aren't really C functions!
1559\begin{cfuncdesc}{Py_BEGIN_ALLOW_THREADS}{}
1560\end{cfuncdesc}
1561
1562\begin{cfuncdesc}{Py_BEGIN_END_THREADS}{}
1563\end{cfuncdesc}
1564
1565\begin{cfuncdesc}{Py_BEGIN_XXX_THREADS}{}
1566\end{cfuncdesc}
1567
1568
Guido van Rossumae110af1997-05-22 20:11:52 +00001569XXX To be done:
1570
1571PyObject, PyVarObject
1572
1573PyObject_HEAD, PyObject_HEAD_INIT, PyObject_VAR_HEAD
1574
1575Typedefs:
1576unaryfunc, binaryfunc, ternaryfunc, inquiry, coercion, intargfunc,
1577intintargfunc, intobjargproc, intintobjargproc, objobjargproc,
1578getreadbufferproc, getwritebufferproc, getsegcountproc,
1579destructor, printfunc, getattrfunc, getattrofunc, setattrfunc,
1580setattrofunc, cmpfunc, reprfunc, hashfunc
1581
1582PyNumberMethods
1583
1584PySequenceMethods
1585
1586PyMappingMethods
1587
1588PyBufferProcs
1589
1590PyTypeObject
1591
1592DL_IMPORT
1593
1594PyType_Type
1595
1596Py*_Check
1597
1598Py_None, _Py_NoneStruct
1599
1600_PyObject_New, _PyObject_NewVar
1601
1602PyObject_NEW, PyObject_NEW_VAR
1603
1604
1605\chapter{Specific Data Types}
1606
1607This chapter describes the functions that deal with specific types of
1608Python objects. It is structured like the ``family tree'' of Python
1609object types.
1610
1611
1612\section{Fundamental Objects}
1613
1614This section describes Python type objects and the singleton object
1615\code{None}.
1616
1617
1618\subsection{Type Objects}
1619
1620\begin{ctypedesc}{PyTypeObject}
1621
1622\end{ctypedesc}
1623
1624\begin{cvardesc}{PyObject *}{PyType_Type}
1625
1626\end{cvardesc}
1627
1628
1629\subsection{The None Object}
1630
1631\begin{cvardesc}{PyObject *}{Py_None}
1632macro
1633\end{cvardesc}
1634
1635
1636\section{Sequence Objects}
1637
1638Generic operations on sequence objects were discussed in the previous
1639chapter; this section deals with the specific kinds of sequence
1640objects that are intrinsuc to the Python language.
1641
1642
1643\subsection{String Objects}
1644
1645\begin{ctypedesc}{PyStringObject}
1646This subtype of \code{PyObject} represents a Python string object.
1647\end{ctypedesc}
1648
1649\begin{cvardesc}{PyTypeObject}{PyString_Type}
1650This instance of \code{PyTypeObject} represents the Python string type.
1651\end{cvardesc}
1652
1653\begin{cfuncdesc}{int}{PyString_Check}{PyObject *o}
1654
1655\end{cfuncdesc}
1656
1657\begin{cfuncdesc}{PyObject *}{PyString_FromStringAndSize}{const char *, int}
1658
1659\end{cfuncdesc}
1660
1661\begin{cfuncdesc}{PyObject *}{PyString_FromString}{const char *}
1662
1663\end{cfuncdesc}
1664
1665\begin{cfuncdesc}{int}{PyString_Size}{PyObject *}
1666
1667\end{cfuncdesc}
1668
1669\begin{cfuncdesc}{char *}{PyString_AsString}{PyObject *}
1670
1671\end{cfuncdesc}
1672
1673\begin{cfuncdesc}{void}{PyString_Concat}{PyObject **, PyObject *}
1674
1675\end{cfuncdesc}
1676
1677\begin{cfuncdesc}{void}{PyString_ConcatAndDel}{PyObject **, PyObject *}
1678
1679\end{cfuncdesc}
1680
1681\begin{cfuncdesc}{int}{_PyString_Resize}{PyObject **, int}
1682
1683\end{cfuncdesc}
1684
1685\begin{cfuncdesc}{PyObject *}{PyString_Format}{PyObject *, PyObject *}
1686
1687\end{cfuncdesc}
1688
1689\begin{cfuncdesc}{void}{PyString_InternInPlace}{PyObject **}
1690
1691\end{cfuncdesc}
1692
1693\begin{cfuncdesc}{PyObject *}{PyString_InternFromString}{const char *}
1694
1695\end{cfuncdesc}
1696
1697\begin{cfuncdesc}{char *}{PyString_AS_STRING}{PyStringObject *}
1698
1699\end{cfuncdesc}
1700
1701\begin{cfuncdesc}{int}{PyString_GET_SIZE}{PyStringObject *}
1702
1703\end{cfuncdesc}
1704
1705
1706\subsection{Tuple Objects}
1707
1708\begin{ctypedesc}{PyTupleObject}
1709This subtype of \code{PyObject} represents a Python tuple object.
1710\end{ctypedesc}
1711
1712\begin{cvardesc}{PyTypeObject}{PyTuple_Type}
1713This instance of \code{PyTypeObject} represents the Python tuple type.
1714\end{cvardesc}
1715
1716\begin{cfuncdesc}{int}{PyTuple_Check}{PyObject *p}
1717Return true if the argument is a tuple object.
1718\end{cfuncdesc}
1719
1720\begin{cfuncdesc}{PyTupleObject *}{PyTuple_New}{int s}
1721Return a new tuple object of size \code{s}
1722\end{cfuncdesc}
1723
1724\begin{cfuncdesc}{int}{PyTuple_Size}{PyTupleObject *p}
1725akes a pointer to a tuple object, and returns the size
1726of that tuple.
1727\end{cfuncdesc}
1728
1729\begin{cfuncdesc}{PyObject *}{PyTuple_GetItem}{PyTupleObject *p, int pos}
1730returns the object at position \code{pos} in the tuple pointed
1731to by \code{p}.
1732\end{cfuncdesc}
1733
1734\begin{cfuncdesc}{PyObject *}{PyTuple_GET_ITEM}{PyTupleObject *p, int pos}
1735does the same, but does no checking of it's
1736arguments.
1737\end{cfuncdesc}
1738
1739\begin{cfuncdesc}{PyTupleObject *}{PyTuple_GetSlice}{PyTupleObject *p,
1740 int low,
1741 int high}
1742takes a slice of the tuple pointed to by \code{p} from
1743\code{low} to \code{high} and returns it as a new tuple.
1744\end{cfuncdesc}
1745
1746\begin{cfuncdesc}{int}{PyTuple_SetItem}{PyTupleObject *p,
1747 int pos,
1748 PyObject *o}
1749inserts a reference to object \code{o} at position \code{pos} of
1750the tuple pointed to by \code{p}. It returns 0 on success.
1751\end{cfuncdesc}
1752
1753\begin{cfuncdesc}{void}{PyTuple_SET_ITEM}{PyTupleObject *p,
1754 int pos,
1755 PyObject *o}
1756
1757does the same, but does no error checking, and
1758should \emph{only} be used to fill in brand new tuples.
1759\end{cfuncdesc}
1760
1761\begin{cfuncdesc}{PyTupleObject *}{_PyTuple_Resize}{PyTupleObject *p,
1762 int new,
1763 int last_is_sticky}
1764can be used to resize a tuple. Because tuples are
1765\emph{supposed} to be immutable, this should only be used if there is only
1766one module referencing the object. Do \emph{not} use this if the tuple may
1767already be known to some other part of the code. \code{last_is_sticky} is
1768a flag - if set, the tuple will grow or shrink at the front, otherwise
1769it will grow or shrink at the end. Think of this as destroying the old
1770tuple and creating a new one, only more efficiently.
1771\end{cfuncdesc}
1772
1773
1774\subsection{List Objects}
1775
1776\begin{ctypedesc}{PyListObject}
1777This subtype of \code{PyObject} represents a Python list object.
1778\end{ctypedesc}
1779
1780\begin{cvardesc}{PyTypeObject}{PyList_Type}
1781This instance of \code{PyTypeObject} represents the Python list type.
1782\end{cvardesc}
1783
1784\begin{cfuncdesc}{int}{PyList_Check}{PyObject *p}
1785returns true if it's argument is a \code{PyListObject}
1786\end{cfuncdesc}
1787
1788\begin{cfuncdesc}{PyObject *}{PyList_New}{int size}
1789
1790\end{cfuncdesc}
1791
1792\begin{cfuncdesc}{int}{PyList_Size}{PyObject *}
1793
1794\end{cfuncdesc}
1795
1796\begin{cfuncdesc}{PyObject *}{PyList_GetItem}{PyObject *, int}
1797
1798\end{cfuncdesc}
1799
1800\begin{cfuncdesc}{int}{PyList_SetItem}{PyObject *, int, PyObject *}
1801
1802\end{cfuncdesc}
1803
1804\begin{cfuncdesc}{int}{PyList_Insert}{PyObject *, int, PyObject *}
1805
1806\end{cfuncdesc}
1807
1808\begin{cfuncdesc}{int}{PyList_Append}{PyObject *, PyObject *}
1809
1810\end{cfuncdesc}
1811
1812\begin{cfuncdesc}{PyObject *}{PyList_GetSlice}{PyObject *, int, int}
1813
1814\end{cfuncdesc}
1815
1816\begin{cfuncdesc}{int}{PyList_SetSlice}{PyObject *, int, int, PyObject *}
1817
1818\end{cfuncdesc}
1819
1820\begin{cfuncdesc}{int}{PyList_Sort}{PyObject *}
1821
1822\end{cfuncdesc}
1823
1824\begin{cfuncdesc}{int}{PyList_Reverse}{PyObject *}
1825
1826\end{cfuncdesc}
1827
1828\begin{cfuncdesc}{PyObject *}{PyList_AsTuple}{PyObject *}
1829
1830\end{cfuncdesc}
1831
1832\begin{cfuncdesc}{PyObject *}{PyList_GET_ITEM}{PyObject *list, int i}
1833
1834\end{cfuncdesc}
1835
1836\begin{cfuncdesc}{int}{PyList_GET_SIZE}{PyObject *list}
1837
1838\end{cfuncdesc}
1839
1840
1841\section{Mapping Objects}
1842
1843\subsection{Dictionary Objects}
1844
1845\begin{ctypedesc}{PyDictObject}
1846This subtype of \code{PyObject} represents a Python dictionary object.
1847\end{ctypedesc}
1848
1849\begin{cvardesc}{PyTypeObject}{PyDict_Type}
1850This instance of \code{PyTypeObject} represents the Python dictionary type.
1851\end{cvardesc}
1852
1853\begin{cfuncdesc}{int}{PyDict_Check}{PyObject *p}
1854returns true if it's argument is a PyDictObject
1855\end{cfuncdesc}
1856
1857\begin{cfuncdesc}{PyDictObject *}{PyDict_New}{}
1858returns a new empty dictionary.
1859\end{cfuncdesc}
1860
1861\begin{cfuncdesc}{void}{PyDict_Clear}{PyDictObject *p}
1862empties an existing dictionary and deletes it.
1863\end{cfuncdesc}
1864
1865\begin{cfuncdesc}{int}{PyDict_SetItem}{PyDictObject *p,
1866 PyObject *key,
1867 PyObject *val}
1868inserts \code{value} into the dictionary with a key of
1869\code{key}. Both \code{key} and \code{value} should be PyObjects, and \code{key} should
1870be hashable.
1871\end{cfuncdesc}
1872
1873\begin{cfuncdesc}{int}{PyDict_SetItemString}{PyDictObject *p,
1874 char *key,
1875 PyObject *val}
1876inserts \code{value} into the dictionary using \code{key}
1877as a key. \code{key} should be a char *
1878\end{cfuncdesc}
1879
1880\begin{cfuncdesc}{int}{PyDict_DelItem}{PyDictObject *p, PyObject *key}
1881removes the entry in dictionary \code{p} with key \code{key}.
1882\code{key} is a PyObject.
1883\end{cfuncdesc}
1884
1885\begin{cfuncdesc}{int}{PyDict_DelItemString}{PyDictObject *p, char *key}
1886removes the entry in dictionary \code{p} which has a key
1887specified by the \code{char *}\code{key}.
1888\end{cfuncdesc}
1889
1890\begin{cfuncdesc}{PyObject *}{PyDict_GetItem}{PyDictObject *p, PyObject *key}
1891returns the object from dictionary \code{p} which has a key
1892\code{key}.
1893\end{cfuncdesc}
1894
1895\begin{cfuncdesc}{PyObject *}{PyDict_GetItemString}{PyDictObject *p, char *key}
1896does the same, but \code{key} is specified as a
1897\code{char *}, rather than a \code{PyObject *}.
1898\end{cfuncdesc}
1899
1900\begin{cfuncdesc}{PyListObject *}{PyDict_Items}{PyDictObject *p}
1901returns a PyListObject containing all the items
1902from the dictionary, as in the mapping method \code{items()} (see the Reference
1903Guide)
1904\end{cfuncdesc}
1905
1906\begin{cfuncdesc}{PyListObject *}{PyDict_Keys}{PyDictObject *p}
1907returns a PyListObject containing all the keys
1908from the dictionary, as in the mapping method \code{keys()} (see the Reference Guide)
1909\end{cfuncdesc}
1910
1911\begin{cfuncdesc}{PyListObject *}{PyDict_Values}{PyDictObject *p}
1912returns a PyListObject containing all the values
1913from the dictionary, as in the mapping method \code{values()} (see the Reference Guide)
1914\end{cfuncdesc}
1915
1916\begin{cfuncdesc}{int}{PyDict_Size}{PyDictObject *p}
1917returns the number of items in the dictionary.
1918\end{cfuncdesc}
1919
1920\begin{cfuncdesc}{int}{PyDict_Next}{PyDictObject *p,
1921 int ppos,
1922 PyObject **pkey,
1923 PyObject **pvalue}
1924
1925\end{cfuncdesc}
1926
1927
1928\section{Numeric Objects}
1929
1930\subsection{Plain Integer Objects}
1931
1932\begin{ctypedesc}{PyIntObject}
1933This subtype of \code{PyObject} represents a Python integer object.
1934\end{ctypedesc}
1935
1936\begin{cvardesc}{PyTypeObject}{PyInt_Type}
1937This instance of \code{PyTypeObject} represents the Python plain
1938integer type.
1939\end{cvardesc}
1940
1941\begin{cfuncdesc}{int}{PyInt_Check}{PyObject *}
1942
1943\end{cfuncdesc}
1944
1945\begin{cfuncdesc}{PyIntObject *}{PyInt_FromLong}{long ival}
1946creates a new integer object with a value of \code{ival}.
1947
1948The current implementation keeps an array of integer objects for all
1949integers between -1 and 100, when you create an int in that range you
1950actually just get back a reference to the existing object. So it should
1951be possible to change the value of 1. I suspect the behaviour of python
1952in this case is undefined. :-)
1953\end{cfuncdesc}
1954
1955\begin{cfuncdesc}{long}{PyInt_AS_LONG}{PyIntObject *io}
1956returns the value of the object \code{io}.
1957\end{cfuncdesc}
1958
1959\begin{cfuncdesc}{long}{PyInt_AsLong}{PyObject *io}
1960will first attempt to cast the object to a PyIntObject, if
1961it is not already one, and the return it's value.
1962\end{cfuncdesc}
1963
1964\begin{cfuncdesc}{long}{PyInt_GetMax}{}
1965returns the systems idea of the largest int it can handle
1966(LONG_MAX, as defined in the system header files)
1967\end{cfuncdesc}
1968
1969
1970\subsection{Long Integer Objects}
1971
1972\begin{ctypedesc}{PyLongObject}
1973This subtype of \code{PyObject} represents a Python long integer object.
1974\end{ctypedesc}
1975
1976\begin{cvardesc}{PyTypeObject}{PyLong_Type}
1977This instance of \code{PyTypeObject} represents the Python long integer type.
1978\end{cvardesc}
1979
1980\begin{cfuncdesc}{int}{PyLong_Check}{PyObject *p}
1981returns true if it's argument is a \code{PyLongObject}
1982\end{cfuncdesc}
1983
1984\begin{cfuncdesc}{PyObject *}{PyLong_FromLong}{long}
1985
1986\end{cfuncdesc}
1987
1988\begin{cfuncdesc}{PyObject *}{PyLong_FromUnsignedLong}{unsigned long}
1989
1990\end{cfuncdesc}
1991
1992\begin{cfuncdesc}{PyObject *}{PyLong_FromDouble}{double}
1993
1994\end{cfuncdesc}
1995
1996\begin{cfuncdesc}{long}{PyLong_AsLong}{PyObject *}
1997
1998\end{cfuncdesc}
1999
2000\begin{cfuncdesc}{unsigned long}{PyLong_AsUnsignedLong}{PyObject }
2001
2002\end{cfuncdesc}
2003
2004\begin{cfuncdesc}{double}{PyLong_AsDouble}{PyObject *}
2005
2006\end{cfuncdesc}
2007
2008\begin{cfuncdesc}{PyObject *}{*PyLong_FromString}{char *, char **, int}
2009
2010\end{cfuncdesc}
2011
2012
2013\subsection{Floating Point Objects}
2014
2015\begin{ctypedesc}{PyFloatObject}
2016This subtype of \code{PyObject} represents a Python floating point object.
2017\end{ctypedesc}
2018
2019\begin{cvardesc}{PyTypeObject}{PyFloat_Type}
2020This instance of \code{PyTypeObject} represents the Python floating
2021point type.
2022\end{cvardesc}
2023
2024\begin{cfuncdesc}{int}{PyFloat_Check}{PyObject *p}
2025returns true if it's argument is a \code{PyFloatObject}
2026\end{cfuncdesc}
2027
2028\begin{cfuncdesc}{PyObject *}{PyFloat_FromDouble}{double}
2029
2030\end{cfuncdesc}
2031
2032\begin{cfuncdesc}{double}{PyFloat_AsDouble}{PyObject *}
2033
2034\end{cfuncdesc}
2035
2036\begin{cfuncdesc}{double}{PyFloat_AS_DOUBLE}{PyFloatObject *}
2037
2038\end{cfuncdesc}
2039
2040
2041\subsection{Complex Number Objects}
2042
2043\begin{ctypedesc}{Py_complex}
2044typedef struct {
2045 double real;
2046 double imag;
2047}
2048\end{ctypedesc}
2049
2050\begin{ctypedesc}{PyComplexObject}
2051This subtype of \code{PyObject} represents a Python complex number object.
2052\end{ctypedesc}
2053
2054\begin{cvardesc}{PyTypeObject}{PyComplex_Type}
2055This instance of \code{PyTypeObject} represents the Python complex
2056number type.
2057\end{cvardesc}
2058
2059\begin{cfuncdesc}{int}{PyComplex_Check}{PyObject *p}
2060returns true if it's argument is a \code{PyComplexObject}
2061\end{cfuncdesc}
2062
2063\begin{cfuncdesc}{Py_complex}{_Py_c_sum}{Py_complex, Py_complex}
2064
2065\end{cfuncdesc}
2066
2067\begin{cfuncdesc}{Py_complex}{_Py_c_diff}{Py_complex, Py_complex}
2068
2069\end{cfuncdesc}
2070
2071\begin{cfuncdesc}{Py_complex}{_Py_c_neg}{Py_complex}
2072
2073\end{cfuncdesc}
2074
2075\begin{cfuncdesc}{Py_complex}{_Py_c_prod}{Py_complex, Py_complex}
2076
2077\end{cfuncdesc}
2078
2079\begin{cfuncdesc}{Py_complex}{_Py_c_quot}{Py_complex, Py_complex}
2080
2081\end{cfuncdesc}
2082
2083\begin{cfuncdesc}{Py_complex}{_Py_c_pow}{Py_complex, Py_complex}
2084
2085\end{cfuncdesc}
2086
2087\begin{cfuncdesc}{PyObject *}{PyComplex_FromCComplex}{Py_complex}
2088
2089\end{cfuncdesc}
2090
2091\begin{cfuncdesc}{PyObject *}{PyComplex_FromDoubles}{double real, double imag}
2092
2093\end{cfuncdesc}
2094
2095\begin{cfuncdesc}{double}{PyComplex_RealAsDouble}{PyObject *op}
2096
2097\end{cfuncdesc}
2098
2099\begin{cfuncdesc}{double}{PyComplex_ImagAsDouble}{PyObject *op}
2100
2101\end{cfuncdesc}
2102
2103\begin{cfuncdesc}{Py_complex}{PyComplex_AsCComplex}{PyObject *op}
2104
2105\end{cfuncdesc}
2106
2107
2108
2109\section{Other Objects}
2110
2111\subsection{File Objects}
2112
2113\begin{ctypedesc}{PyFileObject}
2114This subtype of \code{PyObject} represents a Python file object.
2115\end{ctypedesc}
2116
2117\begin{cvardesc}{PyTypeObject}{PyFile_Type}
2118This instance of \code{PyTypeObject} represents the Python file type.
2119\end{cvardesc}
2120
2121\begin{cfuncdesc}{int}{PyFile_Check}{PyObject *p}
2122returns true if it's argument is a \code{PyFileObject}
2123\end{cfuncdesc}
2124
2125\begin{cfuncdesc}{PyObject *}{PyFile_FromString}{char *name, char *mode}
2126creates a new PyFileObject pointing to the file
2127specified in \code{name} with the mode specified in \code{mode}
2128\end{cfuncdesc}
2129
2130\begin{cfuncdesc}{PyObject *}{PyFile_FromFile}{FILE *fp,
2131 char *name, char *mode, int (*close})
2132creates a new PyFileObject from the already-open \code{fp}.
2133The function \code{close} will be called when the file should be closed.
2134\end{cfuncdesc}
2135
2136\begin{cfuncdesc}{FILE *}{PyFile_AsFile}{PyFileObject *p}
2137returns the file object associated with \code{p} as a \code{FILE *}
2138\end{cfuncdesc}
2139
2140\begin{cfuncdesc}{PyStringObject *}{PyFile_GetLine}{PyObject *p, int n}
2141undocumented as yet
2142\end{cfuncdesc}
2143
2144\begin{cfuncdesc}{PyStringObject *}{PyFile_Name}{PyObject *p}
2145returns the name of the file specified by \code{p} as a
2146PyStringObject
2147\end{cfuncdesc}
2148
2149\begin{cfuncdesc}{void}{PyFile_SetBufSize}{PyFileObject *p, int n}
2150on systems with \code{setvbuf} only
2151\end{cfuncdesc}
2152
2153\begin{cfuncdesc}{int}{PyFile_SoftSpace}{PyFileObject *p, int newflag}
2154same as the file object method \code{softspace}
2155\end{cfuncdesc}
2156
2157\begin{cfuncdesc}{int}{PyFile_WriteObject}{PyObject *obj, PyFileObject *p}
2158writes object \code{obj} to file object \code{p}
2159\end{cfuncdesc}
2160
2161\begin{cfuncdesc}{int}{PyFile_WriteString}{char *s, PyFileObject *p}
2162writes string \code{s} to file object \code{p}
2163\end{cfuncdesc}
2164
2165
Guido van Rossum9231c8f1997-05-15 21:43:21 +00002166\input{api.ind} % Index -- must be last
2167
2168\end{document}