blob: 09d2cb33d66c8b2dd26d9dfb37e379388e69bc2e [file] [log] [blame]
Fred Drake3adf79e2001-10-12 19:01:43 +00001\chapter{Utilities \label{utilities}}
2
3The functions in this chapter perform various utility tasks, ranging
4from helping C code be more portable across platforms, using Python
5modules from C, and parsing function arguments and constructing Python
6values from C values.
7
8
9\section{Operating System Utilities \label{os}}
10
11\begin{cfuncdesc}{int}{Py_FdIsInteractive}{FILE *fp, char *filename}
12 Return true (nonzero) if the standard I/O file \var{fp} with name
13 \var{filename} is deemed interactive. This is the case for files
14 for which \samp{isatty(fileno(\var{fp}))} is true. If the global
15 flag \cdata{Py_InteractiveFlag} is true, this function also returns
16 true if the \var{filename} pointer is \NULL{} or if the name is
17 equal to one of the strings \code{'<stdin>'} or \code{'???'}.
18\end{cfuncdesc}
19
20\begin{cfuncdesc}{long}{PyOS_GetLastModificationTime}{char *filename}
21 Return the time of last modification of the file \var{filename}.
22 The result is encoded in the same way as the timestamp returned by
23 the standard C library function \cfunction{time()}.
24\end{cfuncdesc}
25
26\begin{cfuncdesc}{void}{PyOS_AfterFork}{}
27 Function to update some internal state after a process fork; this
28 should be called in the new process if the Python interpreter will
29 continue to be used. If a new executable is loaded into the new
30 process, this function does not need to be called.
31\end{cfuncdesc}
32
33\begin{cfuncdesc}{int}{PyOS_CheckStack}{}
34 Return true when the interpreter runs out of stack space. This is a
35 reliable check, but is only available when \constant{USE_STACKCHECK}
36 is defined (currently on Windows using the Microsoft Visual \Cpp{}
37 compiler and on the Macintosh). \constant{USE_CHECKSTACK} will be
38 defined automatically; you should never change the definition in
39 your own code.
40\end{cfuncdesc}
41
42\begin{cfuncdesc}{PyOS_sighandler_t}{PyOS_getsig}{int i}
43 Return the current signal handler for signal \var{i}. This is a
44 thin wrapper around either \cfunction{sigaction()} or
45 \cfunction{signal()}. Do not call those functions directly!
46 \ctype{PyOS_sighandler_t} is a typedef alias for \ctype{void
47 (*)(int)}.
48\end{cfuncdesc}
49
50\begin{cfuncdesc}{PyOS_sighandler_t}{PyOS_setsig}{int i, PyOS_sighandler_t h}
51 Set the signal handler for signal \var{i} to be \var{h}; return the
52 old signal handler. This is a thin wrapper around either
53 \cfunction{sigaction()} or \cfunction{signal()}. Do not call those
54 functions directly! \ctype{PyOS_sighandler_t} is a typedef alias
55 for \ctype{void (*)(int)}.
56\end{cfuncdesc}
57
58
59\section{Process Control \label{processControl}}
60
Tim Peters7c321a82002-07-09 02:57:01 +000061\begin{cfuncdesc}{void}{Py_FatalError}{const char *message}
Fred Drake3adf79e2001-10-12 19:01:43 +000062 Print a fatal error message and kill the process. No cleanup is
63 performed. This function should only be invoked when a condition is
64 detected that would make it dangerous to continue using the Python
65 interpreter; e.g., when the object administration appears to be
66 corrupted. On \UNIX, the standard C library function
67 \cfunction{abort()}\ttindex{abort()} is called which will attempt to
68 produce a \file{core} file.
69\end{cfuncdesc}
70
71\begin{cfuncdesc}{void}{Py_Exit}{int status}
72 Exit the current process. This calls
73 \cfunction{Py_Finalize()}\ttindex{Py_Finalize()} and then calls the
74 standard C library function
75 \code{exit(\var{status})}\ttindex{exit()}.
76\end{cfuncdesc}
77
78\begin{cfuncdesc}{int}{Py_AtExit}{void (*func) ()}
79 Register a cleanup function to be called by
80 \cfunction{Py_Finalize()}\ttindex{Py_Finalize()}. The cleanup
81 function will be called with no arguments and should return no
82 value. At most 32 \index{cleanup functions}cleanup functions can be
83 registered. When the registration is successful,
84 \cfunction{Py_AtExit()} returns \code{0}; on failure, it returns
85 \code{-1}. The cleanup function registered last is called first.
86 Each cleanup function will be called at most once. Since Python's
87 internal finallization will have completed before the cleanup
88 function, no Python APIs should be called by \var{func}.
89\end{cfuncdesc}
90
91
92\section{Importing Modules \label{importing}}
93
94\begin{cfuncdesc}{PyObject*}{PyImport_ImportModule}{char *name}
95 This is a simplified interface to
96 \cfunction{PyImport_ImportModuleEx()} below, leaving the
97 \var{globals} and \var{locals} arguments set to \NULL. When the
98 \var{name} argument contains a dot (when it specifies a submodule of
99 a package), the \var{fromlist} argument is set to the list
100 \code{['*']} so that the return value is the named module rather
101 than the top-level package containing it as would otherwise be the
102 case. (Unfortunately, this has an additional side effect when
103 \var{name} in fact specifies a subpackage instead of a submodule:
104 the submodules specified in the package's \code{__all__} variable
105 are \index{package variable!\code{__all__}}
106 \withsubitem{(package variable)}{\ttindex{__all__}}loaded.) Return
107 a new reference to the imported module, or \NULL{} with an exception
Tim Peterscfd575d2004-08-02 03:46:45 +0000108 set on failure. Before Python 2.4, the module may still be created in
109 the failure case --- examine \code{sys.modules} to find out. Starting
110 with Python 2.4, a failing import of a module no longer leaves the
111 module in \code{sys.modules}.
112 \versionchanged[failing imports remove incomplete module objects]{2.4}
Fred Drake3adf79e2001-10-12 19:01:43 +0000113 \withsubitem{(in module sys)}{\ttindex{modules}}
114\end{cfuncdesc}
115
116\begin{cfuncdesc}{PyObject*}{PyImport_ImportModuleEx}{char *name,
117 PyObject *globals, PyObject *locals, PyObject *fromlist}
118 Import a module. This is best described by referring to the
119 built-in Python function
120 \function{__import__()}\bifuncindex{__import__}, as the standard
121 \function{__import__()} function calls this function directly.
122
123 The return value is a new reference to the imported module or
Tim Peterscfd575d2004-08-02 03:46:45 +0000124 top-level package, or \NULL{} with an exception set on failure (before
125 Python 2.4, the
Fred Drake3adf79e2001-10-12 19:01:43 +0000126 module may still be created in this case). Like for
127 \function{__import__()}, the return value when a submodule of a
128 package was requested is normally the top-level package, unless a
129 non-empty \var{fromlist} was given.
Tim Peterscfd575d2004-08-02 03:46:45 +0000130 \versionchanged[failing imports remove incomplete module objects]{2.4}
Fred Drake3adf79e2001-10-12 19:01:43 +0000131\end{cfuncdesc}
132
133\begin{cfuncdesc}{PyObject*}{PyImport_Import}{PyObject *name}
134 This is a higher-level interface that calls the current ``import
135 hook function''. It invokes the \function{__import__()} function
136 from the \code{__builtins__} of the current globals. This means
137 that the import is done using whatever import hooks are installed in
138 the current environment, e.g. by \module{rexec}\refstmodindex{rexec}
139 or \module{ihooks}\refstmodindex{ihooks}.
140\end{cfuncdesc}
141
142\begin{cfuncdesc}{PyObject*}{PyImport_ReloadModule}{PyObject *m}
143 Reload a module. This is best described by referring to the
144 built-in Python function \function{reload()}\bifuncindex{reload}, as
145 the standard \function{reload()} function calls this function
146 directly. Return a new reference to the reloaded module, or \NULL{}
147 with an exception set on failure (the module still exists in this
148 case).
149\end{cfuncdesc}
150
151\begin{cfuncdesc}{PyObject*}{PyImport_AddModule}{char *name}
152 Return the module object corresponding to a module name. The
Brett Cannon711e7d92004-07-10 22:20:32 +0000153 \var{name} argument may be of the form \code{package.module}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000154 First check the modules dictionary if there's one there, and if not,
Raymond Hettinger92016dc2003-09-22 15:27:11 +0000155 create a new one and insert it in the modules dictionary.
Fred Drake674dae22002-11-13 15:13:38 +0000156 Return \NULL{} with an exception set on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +0000157 \note{This function does not load or import the module; if the
158 module wasn't already loaded, you will get an empty module object.
159 Use \cfunction{PyImport_ImportModule()} or one of its variants to
Fred Drake674dae22002-11-13 15:13:38 +0000160 import a module. Package structures implied by a dotted name for
161 \var{name} are not created if not already present.}
Fred Drake3adf79e2001-10-12 19:01:43 +0000162\end{cfuncdesc}
163
164\begin{cfuncdesc}{PyObject*}{PyImport_ExecCodeModule}{char *name, PyObject *co}
165 Given a module name (possibly of the form \code{package.module}) and
166 a code object read from a Python bytecode file or obtained from the
167 built-in function \function{compile()}\bifuncindex{compile}, load
168 the module. Return a new reference to the module object, or \NULL{}
Tim Peterscfd575d2004-08-02 03:46:45 +0000169 with an exception set if an error occurred. Before Python 2.4, the module
170 could still be created in error cases. Starting with Python 2.4,
171 \var{name} is removed from \code{sys.modules} in error cases, and even
172 if \var{name} was already in \code{sys.modules} on entry to
173 \cfunction{PyImport_ExecCodeModule()}. Leaving incompletely initialized
174 modules in \code{sys.modules} is dangerous, as imports of such modules
175 have no way to know that the module object is an unknown (and probably
176 damaged with respect to the module author's intents) state.
177
178 This function will reload the module if it was already imported. See
179 \cfunction{PyImport_ReloadModule()}
180
181 If \var{name} points to a dotted name of the
Fred Drake674dae22002-11-13 15:13:38 +0000182 form \code{package.module}, any package structures not already
183 created will still not be created.
Tim Peterscfd575d2004-08-02 03:46:45 +0000184
185 \versionchanged[\var{name} is removed from \code{sys.modules} in error cases]{2.4}
186
Fred Drake3adf79e2001-10-12 19:01:43 +0000187\end{cfuncdesc}
188
189\begin{cfuncdesc}{long}{PyImport_GetMagicNumber}{}
190 Return the magic number for Python bytecode files
191 (a.k.a. \file{.pyc} and \file{.pyo} files). The magic number should
192 be present in the first four bytes of the bytecode file, in
193 little-endian byte order.
194\end{cfuncdesc}
195
196\begin{cfuncdesc}{PyObject*}{PyImport_GetModuleDict}{}
197 Return the dictionary used for the module administration
198 (a.k.a.\ \code{sys.modules}). Note that this is a per-interpreter
199 variable.
200\end{cfuncdesc}
201
202\begin{cfuncdesc}{void}{_PyImport_Init}{}
203 Initialize the import mechanism. For internal use only.
204\end{cfuncdesc}
205
206\begin{cfuncdesc}{void}{PyImport_Cleanup}{}
207 Empty the module table. For internal use only.
208\end{cfuncdesc}
209
210\begin{cfuncdesc}{void}{_PyImport_Fini}{}
211 Finalize the import mechanism. For internal use only.
212\end{cfuncdesc}
213
214\begin{cfuncdesc}{PyObject*}{_PyImport_FindExtension}{char *, char *}
215 For internal use only.
216\end{cfuncdesc}
217
218\begin{cfuncdesc}{PyObject*}{_PyImport_FixupExtension}{char *, char *}
219 For internal use only.
220\end{cfuncdesc}
221
222\begin{cfuncdesc}{int}{PyImport_ImportFrozenModule}{char *name}
223 Load a frozen module named \var{name}. Return \code{1} for success,
224 \code{0} if the module is not found, and \code{-1} with an exception
225 set if the initialization failed. To access the imported module on
226 a successful load, use \cfunction{PyImport_ImportModule()}. (Note
227 the misnomer --- this function would reload the module if it was
228 already imported.)
229\end{cfuncdesc}
230
231\begin{ctypedesc}[_frozen]{struct _frozen}
232 This is the structure type definition for frozen module descriptors,
233 as generated by the \program{freeze}\index{freeze utility} utility
234 (see \file{Tools/freeze/} in the Python source distribution). Its
235 definition, found in \file{Include/import.h}, is:
236
237\begin{verbatim}
238struct _frozen {
239 char *name;
240 unsigned char *code;
241 int size;
242};
243\end{verbatim}
244\end{ctypedesc}
245
246\begin{cvardesc}{struct _frozen*}{PyImport_FrozenModules}
247 This pointer is initialized to point to an array of \ctype{struct
248 _frozen} records, terminated by one whose members are all \NULL{} or
249 zero. When a frozen module is imported, it is searched in this
250 table. Third-party code could play tricks with this to provide a
251 dynamically created collection of frozen modules.
252\end{cvardesc}
253
254\begin{cfuncdesc}{int}{PyImport_AppendInittab}{char *name,
255 void (*initfunc)(void)}
256 Add a single module to the existing table of built-in modules. This
257 is a convenience wrapper around
258 \cfunction{PyImport_ExtendInittab()}, returning \code{-1} if the
259 table could not be extended. The new module can be imported by the
260 name \var{name}, and uses the function \var{initfunc} as the
261 initialization function called on the first attempted import. This
262 should be called before \cfunction{Py_Initialize()}.
263\end{cfuncdesc}
264
265\begin{ctypedesc}[_inittab]{struct _inittab}
266 Structure describing a single entry in the list of built-in
267 modules. Each of these structures gives the name and initialization
268 function for a module built into the interpreter. Programs which
269 embed Python may use an array of these structures in conjunction
270 with \cfunction{PyImport_ExtendInittab()} to provide additional
271 built-in modules. The structure is defined in
272 \file{Include/import.h} as:
273
274\begin{verbatim}
275struct _inittab {
276 char *name;
277 void (*initfunc)(void);
278};
279\end{verbatim}
280\end{ctypedesc}
281
282\begin{cfuncdesc}{int}{PyImport_ExtendInittab}{struct _inittab *newtab}
283 Add a collection of modules to the table of built-in modules. The
284 \var{newtab} array must end with a sentinel entry which contains
285 \NULL{} for the \member{name} field; failure to provide the sentinel
286 value can result in a memory fault. Returns \code{0} on success or
287 \code{-1} if insufficient memory could be allocated to extend the
288 internal table. In the event of failure, no modules are added to
289 the internal table. This should be called before
290 \cfunction{Py_Initialize()}.
291\end{cfuncdesc}
292
293
Fred Drake0fae49f2001-10-14 04:45:51 +0000294\section{Data marshalling support \label{marshalling-utils}}
295
296These routines allow C code to work with serialized objects using the
297same data format as the \module{marshal} module. There are functions
298to write data into the serialization format, and additional functions
299that can be used to read the data back. Files used to store marshalled
300data must be opened in binary mode.
301
302Numeric values are stored with the least significant byte first.
303
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000304The module supports two versions of the data format: version 0 is the
305historical version, version 1 (new in Python 2.4) shares interned
306strings in the file, and upon unmarshalling. \var{Py_MARSHAL_VERSION}
307indicates the current file format (currently 1).
308
309\begin{cfuncdesc}{void}{PyMarshal_WriteLongToFile}{long value, FILE *file, int version}
Fred Drake0fae49f2001-10-14 04:45:51 +0000310 Marshal a \ctype{long} integer, \var{value}, to \var{file}. This
311 will only write the least-significant 32 bits of \var{value};
312 regardless of the size of the native \ctype{long} type.
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000313
314 \versionchanged[\var{version} indicates the file format]{2.4}
Fred Drake0fae49f2001-10-14 04:45:51 +0000315\end{cfuncdesc}
316
Fred Drake0fae49f2001-10-14 04:45:51 +0000317\begin{cfuncdesc}{void}{PyMarshal_WriteObjectToFile}{PyObject *value,
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000318 FILE *file, int version}
Fred Drakeb0840172002-06-17 15:44:18 +0000319 Marshal a Python object, \var{value}, to \var{file}.
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000320
321 \versionchanged[\var{version} indicates the file format]{2.4}
Fred Drake0fae49f2001-10-14 04:45:51 +0000322\end{cfuncdesc}
323
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000324\begin{cfuncdesc}{PyObject*}{PyMarshal_WriteObjectToString}{PyObject *value, int version}
Fred Drake0fae49f2001-10-14 04:45:51 +0000325 Return a string object containing the marshalled representation of
326 \var{value}.
Martin v. Löwisef82d2f2004-06-27 16:51:46 +0000327
328 \versionchanged[\var{version} indicates the file format]{2.4}
Fred Drake0fae49f2001-10-14 04:45:51 +0000329\end{cfuncdesc}
330
331The following functions allow marshalled values to be read back in.
332
333XXX What about error detection? It appears that reading past the end
334of the file will always result in a negative numeric value (where
335that's relevant), but it's not clear that negative values won't be
336handled properly when there's no error. What's the right way to tell?
337Should only non-negative values be written using these routines?
338
339\begin{cfuncdesc}{long}{PyMarshal_ReadLongFromFile}{FILE *file}
340 Return a C \ctype{long} from the data stream in a \ctype{FILE*}
341 opened for reading. Only a 32-bit value can be read in using
342 this function, regardless of the native size of \ctype{long}.
343\end{cfuncdesc}
344
345\begin{cfuncdesc}{int}{PyMarshal_ReadShortFromFile}{FILE *file}
346 Return a C \ctype{short} from the data stream in a \ctype{FILE*}
347 opened for reading. Only a 16-bit value can be read in using
Fred Drakeb0840172002-06-17 15:44:18 +0000348 this function, regardless of the native size of \ctype{short}.
Fred Drake0fae49f2001-10-14 04:45:51 +0000349\end{cfuncdesc}
350
351\begin{cfuncdesc}{PyObject*}{PyMarshal_ReadObjectFromFile}{FILE *file}
352 Return a Python object from the data stream in a \ctype{FILE*}
353 opened for reading. On error, sets the appropriate exception
354 (\exception{EOFError} or \exception{TypeError}) and returns \NULL.
355\end{cfuncdesc}
356
357\begin{cfuncdesc}{PyObject*}{PyMarshal_ReadLastObjectFromFile}{FILE *file}
358 Return a Python object from the data stream in a \ctype{FILE*}
359 opened for reading. Unlike
360 \cfunction{PyMarshal_ReadObjectFromFile()}, this function assumes
361 that no further objects will be read from the file, allowing it to
362 aggressively load file data into memory so that the de-serialization
363 can operate from data in memory rather than reading a byte at a time
364 from the file. Only use these variant if you are certain that you
365 won't be reading anything else from the file. On error, sets the
366 appropriate exception (\exception{EOFError} or
367 \exception{TypeError}) and returns \NULL.
368\end{cfuncdesc}
369
370\begin{cfuncdesc}{PyObject*}{PyMarshal_ReadObjectFromString}{char *string,
371 int len}
372 Return a Python object from the data stream in a character buffer
373 containing \var{len} bytes pointed to by \var{string}. On error,
374 sets the appropriate exception (\exception{EOFError} or
375 \exception{TypeError}) and returns \NULL.
376\end{cfuncdesc}
377
378
Fred Drake3adf79e2001-10-12 19:01:43 +0000379\section{Parsing arguments and building values
380 \label{arg-parsing}}
381
382These functions are useful when creating your own extensions functions
383and methods. Additional information and examples are available in
384\citetitle[../ext/ext.html]{Extending and Embedding the Python
385Interpreter}.
386
Fred Drake68304cc2002-04-05 23:01:14 +0000387The first three of these functions described,
388\cfunction{PyArg_ParseTuple()},
389\cfunction{PyArg_ParseTupleAndKeywords()}, and
390\cfunction{PyArg_Parse()}, all use \emph{format strings} which are
391used to tell the function about the expected arguments. The format
392strings use the same syntax for each of these functions.
393
394A format string consists of zero or more ``format units.'' A format
395unit describes one Python object; it is usually a single character or
396a parenthesized sequence of format units. With a few exceptions, a
397format unit that is not a parenthesized sequence normally corresponds
398to a single address argument to these functions. In the following
399description, the quoted form is the format unit; the entry in (round)
400parentheses is the Python object type that matches the format unit;
401and the entry in [square] brackets is the type of the C variable(s)
402whose address should be passed.
403
404\begin{description}
Brett Cannond88471f2004-07-01 20:55:42 +0000405 \item[\samp{s} (string or Unicode object) {[const char *]}]
Fred Drake68304cc2002-04-05 23:01:14 +0000406 Convert a Python string or Unicode object to a C pointer to a
407 character string. You must not provide storage for the string
408 itself; a pointer to an existing string is stored into the character
409 pointer variable whose address you pass. The C string is
410 NUL-terminated. The Python string must not contain embedded NUL
411 bytes; if it does, a \exception{TypeError} exception is raised.
412 Unicode objects are converted to C strings using the default
413 encoding. If this conversion fails, a \exception{UnicodeError} is
414 raised.
415
416 \item[\samp{s\#} (string, Unicode or any read buffer compatible object)
Brett Cannond88471f2004-07-01 20:55:42 +0000417 {[const char *, int]}]
Fred Drake68304cc2002-04-05 23:01:14 +0000418 This variant on \samp{s} stores into two C variables, the first one
419 a pointer to a character string, the second one its length. In this
420 case the Python string may contain embedded null bytes. Unicode
421 objects pass back a pointer to the default encoded string version of
422 the object if such a conversion is possible. All other read-buffer
423 compatible objects pass back a reference to the raw internal data
424 representation.
425
Brett Cannond88471f2004-07-01 20:55:42 +0000426 \item[\samp{z} (string or \code{None}) {[const char *]}]
Fred Drake68304cc2002-04-05 23:01:14 +0000427 Like \samp{s}, but the Python object may also be \code{None}, in
428 which case the C pointer is set to \NULL.
429
430 \item[\samp{z\#} (string or \code{None} or any read buffer
Brett Cannond88471f2004-07-01 20:55:42 +0000431 compatible object) {[const char *, int]}]
Fred Drake68304cc2002-04-05 23:01:14 +0000432 This is to \samp{s\#} as \samp{z} is to \samp{s}.
433
434 \item[\samp{u} (Unicode object) {[Py_UNICODE *]}]
435 Convert a Python Unicode object to a C pointer to a NUL-terminated
436 buffer of 16-bit Unicode (UTF-16) data. As with \samp{s}, there is
437 no need to provide storage for the Unicode data buffer; a pointer to
438 the existing Unicode data is stored into the \ctype{Py_UNICODE}
439 pointer variable whose address you pass.
440
441 \item[\samp{u\#} (Unicode object) {[Py_UNICODE *, int]}]
442 This variant on \samp{u} stores into two C variables, the first one
443 a pointer to a Unicode data buffer, the second one its length.
444 Non-Unicode objects are handled by interpreting their read-buffer
445 pointer as pointer to a \ctype{Py_UNICODE} array.
446
447 \item[\samp{es} (string, Unicode object or character buffer
448 compatible object) {[const char *encoding, char **buffer]}]
449 This variant on \samp{s} is used for encoding Unicode and objects
450 convertible to Unicode into a character buffer. It only works for
451 encoded data without embedded NUL bytes.
452
453 This format requires two arguments. The first is only used as
Brett Cannond88471f2004-07-01 20:55:42 +0000454 input, and must be a \ctype{const char*} which points to the name of an
Fred Drake68304cc2002-04-05 23:01:14 +0000455 encoding as a NUL-terminated string, or \NULL, in which case the
456 default encoding is used. An exception is raised if the named
457 encoding is not known to Python. The second argument must be a
458 \ctype{char**}; the value of the pointer it references will be set
459 to a buffer with the contents of the argument text. The text will
460 be encoded in the encoding specified by the first argument.
461
462 \cfunction{PyArg_ParseTuple()} will allocate a buffer of the needed
463 size, copy the encoded data into this buffer and adjust
464 \var{*buffer} to reference the newly allocated storage. The caller
465 is responsible for calling \cfunction{PyMem_Free()} to free the
466 allocated buffer after use.
467
468 \item[\samp{et} (string, Unicode object or character buffer
469 compatible object) {[const char *encoding, char **buffer]}]
470 Same as \samp{es} except that 8-bit string objects are passed
471 through without recoding them. Instead, the implementation assumes
472 that the string object uses the encoding passed in as parameter.
473
474 \item[\samp{es\#} (string, Unicode object or character buffer compatible
475 object) {[const char *encoding, char **buffer, int *buffer_length]}]
476 This variant on \samp{s\#} is used for encoding Unicode and objects
477 convertible to Unicode into a character buffer. Unlike the
478 \samp{es} format, this variant allows input data which contains NUL
479 characters.
480
481 It requires three arguments. The first is only used as input, and
Brett Cannond88471f2004-07-01 20:55:42 +0000482 must be a \ctype{const char*} which points to the name of an encoding as a
Fred Drake68304cc2002-04-05 23:01:14 +0000483 NUL-terminated string, or \NULL, in which case the default encoding
484 is used. An exception is raised if the named encoding is not known
485 to Python. The second argument must be a \ctype{char**}; the value
486 of the pointer it references will be set to a buffer with the
487 contents of the argument text. The text will be encoded in the
488 encoding specified by the first argument. The third argument must
489 be a pointer to an integer; the referenced integer will be set to
490 the number of bytes in the output buffer.
491
492 There are two modes of operation:
493
494 If \var{*buffer} points a \NULL{} pointer, the function will
495 allocate a buffer of the needed size, copy the encoded data into
496 this buffer and set \var{*buffer} to reference the newly allocated
497 storage. The caller is responsible for calling
498 \cfunction{PyMem_Free()} to free the allocated buffer after usage.
499
500 If \var{*buffer} points to a non-\NULL{} pointer (an already
501 allocated buffer), \cfunction{PyArg_ParseTuple()} will use this
502 location as the buffer and interpret the initial value of
503 \var{*buffer_length} as the buffer size. It will then copy the
504 encoded data into the buffer and NUL-terminate it. If the buffer
505 is not large enough, a \exception{ValueError} will be set.
506
507 In both cases, \var{*buffer_length} is set to the length of the
508 encoded data without the trailing NUL byte.
509
510 \item[\samp{et\#} (string, Unicode object or character buffer compatible
511 object) {[const char *encoding, char **buffer]}]
512 Same as \samp{es\#} except that string objects are passed through
513 without recoding them. Instead, the implementation assumes that the
514 string object uses the encoding passed in as parameter.
515
516 \item[\samp{b} (integer) {[char]}]
517 Convert a Python integer to a tiny int, stored in a C \ctype{char}.
518
Thomas Heller42a11722003-04-23 19:27:35 +0000519 \item[\samp{B} (integer) {[unsigned char]}]
520 Convert a Python integer to a tiny int without overflow checking,
521 stored in a C \ctype{unsigned char}. \versionadded{2.3}
522
Fred Drake68304cc2002-04-05 23:01:14 +0000523 \item[\samp{h} (integer) {[short int]}]
524 Convert a Python integer to a C \ctype{short int}.
525
Thomas Heller42a11722003-04-23 19:27:35 +0000526 \item[\samp{H} (integer) {[unsigned short int]}]
527 Convert a Python integer to a C \ctype{unsigned short int}, without
528 overflow checking. \versionadded{2.3}
529
Fred Drake68304cc2002-04-05 23:01:14 +0000530 \item[\samp{i} (integer) {[int]}]
531 Convert a Python integer to a plain C \ctype{int}.
532
Thomas Heller42a11722003-04-23 19:27:35 +0000533 \item[\samp{I} (integer) {[unsigned int]}]
534 Convert a Python integer to a C \ctype{unsigned int}, without
535 overflow checking. \versionadded{2.3}
536
Fred Drake68304cc2002-04-05 23:01:14 +0000537 \item[\samp{l} (integer) {[long int]}]
538 Convert a Python integer to a C \ctype{long int}.
539
Thomas Heller42a11722003-04-23 19:27:35 +0000540 \item[\samp{k} (integer) {[unsigned long]}]
541 Convert a Python integer to a C \ctype{unsigned long} without
542 overflow checking. \versionadded{2.3}
543
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000544 \item[\samp{L} (integer) {[PY_LONG_LONG]}]
Fred Drake68304cc2002-04-05 23:01:14 +0000545 Convert a Python integer to a C \ctype{long long}. This format is
546 only available on platforms that support \ctype{long long} (or
547 \ctype{_int64} on Windows).
548
Thomas Heller42a11722003-04-23 19:27:35 +0000549 \item[\samp{K} (integer) {[unsigned PY_LONG_LONG]}]
550 Convert a Python integer to a C \ctype{unsigned long long}
551 without overflow checking. This format is only available on
552 platforms that support \ctype{unsigned long long} (or
553 \ctype{unsigned _int64} on Windows). \versionadded{2.3}
554
Fred Drake68304cc2002-04-05 23:01:14 +0000555 \item[\samp{c} (string of length 1) {[char]}]
556 Convert a Python character, represented as a string of length 1, to
557 a C \ctype{char}.
558
559 \item[\samp{f} (float) {[float]}]
560 Convert a Python floating point number to a C \ctype{float}.
561
562 \item[\samp{d} (float) {[double]}]
563 Convert a Python floating point number to a C \ctype{double}.
564
565 \item[\samp{D} (complex) {[Py_complex]}]
566 Convert a Python complex number to a C \ctype{Py_complex} structure.
567
568 \item[\samp{O} (object) {[PyObject *]}]
569 Store a Python object (without any conversion) in a C object
570 pointer. The C program thus receives the actual object that was
571 passed. The object's reference count is not increased. The pointer
572 stored is not \NULL.
573
574 \item[\samp{O!} (object) {[\var{typeobject}, PyObject *]}]
575 Store a Python object in a C object pointer. This is similar to
576 \samp{O}, but takes two C arguments: the first is the address of a
577 Python type object, the second is the address of the C variable (of
578 type \ctype{PyObject*}) into which the object pointer is stored. If
579 the Python object does not have the required type,
580 \exception{TypeError} is raised.
581
582 \item[\samp{O\&} (object) {[\var{converter}, \var{anything}]}]
583 Convert a Python object to a C variable through a \var{converter}
584 function. This takes two arguments: the first is a function, the
585 second is the address of a C variable (of arbitrary type), converted
586 to \ctype{void *}. The \var{converter} function in turn is called
587 as follows:
588
589 \var{status}\code{ = }\var{converter}\code{(}\var{object},
590 \var{address}\code{);}
591
592 where \var{object} is the Python object to be converted and
593 \var{address} is the \ctype{void*} argument that was passed to the
594 \cfunction{PyArg_Parse*()} function. The returned \var{status}
595 should be \code{1} for a successful conversion and \code{0} if the
596 conversion has failed. When the conversion fails, the
597 \var{converter} function should raise an exception.
598
599 \item[\samp{S} (string) {[PyStringObject *]}]
600 Like \samp{O} but requires that the Python object is a string
601 object. Raises \exception{TypeError} if the object is not a string
602 object. The C variable may also be declared as \ctype{PyObject*}.
603
604 \item[\samp{U} (Unicode string) {[PyUnicodeObject *]}]
605 Like \samp{O} but requires that the Python object is a Unicode
606 object. Raises \exception{TypeError} if the object is not a Unicode
607 object. The C variable may also be declared as \ctype{PyObject*}.
608
609 \item[\samp{t\#} (read-only character buffer) {[char *, int]}]
610 Like \samp{s\#}, but accepts any object which implements the
611 read-only buffer interface. The \ctype{char*} variable is set to
612 point to the first byte of the buffer, and the \ctype{int} is set to
613 the length of the buffer. Only single-segment buffer objects are
614 accepted; \exception{TypeError} is raised for all others.
615
616 \item[\samp{w} (read-write character buffer) {[char *]}]
617 Similar to \samp{s}, but accepts any object which implements the
618 read-write buffer interface. The caller must determine the length
619 of the buffer by other means, or use \samp{w\#} instead. Only
620 single-segment buffer objects are accepted; \exception{TypeError} is
621 raised for all others.
622
623 \item[\samp{w\#} (read-write character buffer) {[char *, int]}]
624 Like \samp{s\#}, but accepts any object which implements the
625 read-write buffer interface. The \ctype{char *} variable is set to
626 point to the first byte of the buffer, and the \ctype{int} is set to
627 the length of the buffer. Only single-segment buffer objects are
628 accepted; \exception{TypeError} is raised for all others.
629
630 \item[\samp{(\var{items})} (tuple) {[\var{matching-items}]}]
631 The object must be a Python sequence whose length is the number of
632 format units in \var{items}. The C arguments must correspond to the
633 individual format units in \var{items}. Format units for sequences
634 may be nested.
635
636 \note{Prior to Python version 1.5.2, this format specifier only
637 accepted a tuple containing the individual parameters, not an
638 arbitrary sequence. Code which previously caused
639 \exception{TypeError} to be raised here may now proceed without an
640 exception. This is not expected to be a problem for existing code.}
641\end{description}
642
643It is possible to pass Python long integers where integers are
644requested; however no proper range checking is done --- the most
645significant bits are silently truncated when the receiving field is
646too small to receive the value (actually, the semantics are inherited
647from downcasts in C --- your mileage may vary).
648
649A few other characters have a meaning in a format string. These may
650not occur inside nested parentheses. They are:
651
652\begin{description}
653 \item[\samp{|}]
654 Indicates that the remaining arguments in the Python argument list
655 are optional. The C variables corresponding to optional arguments
656 should be initialized to their default value --- when an optional
657 argument is not specified, \cfunction{PyArg_ParseTuple()} does not
658 touch the contents of the corresponding C variable(s).
659
660 \item[\samp{:}]
661 The list of format units ends here; the string after the colon is
662 used as the function name in error messages (the ``associated
663 value'' of the exception that \cfunction{PyArg_ParseTuple()}
664 raises).
665
666 \item[\samp{;}]
667 The list of format units ends here; the string after the semicolon
668 is used as the error message \emph{instead} of the default error
669 message. Clearly, \samp{:} and \samp{;} mutually exclude each
670 other.
671\end{description}
672
673Note that any Python object references which are provided to the
674caller are \emph{borrowed} references; do not decrement their
675reference count!
676
677Additional arguments passed to these functions must be addresses of
678variables whose type is determined by the format string; these are
679used to store values from the input tuple. There are a few cases, as
680described in the list of format units above, where these parameters
681are used as input values; they should match what is specified for the
682corresponding format unit in that case.
683
684For the conversion to succeed, the \var{arg} object must match the
685format and the format must be exhausted. On success, the
686\cfunction{PyArg_Parse*()} functions return true, otherwise they
687return false and raise an appropriate exception.
688
Fred Drake3adf79e2001-10-12 19:01:43 +0000689\begin{cfuncdesc}{int}{PyArg_ParseTuple}{PyObject *args, char *format,
690 \moreargs}
691 Parse the parameters of a function that takes only positional
692 parameters into local variables. Returns true on success; on
Fred Drake68304cc2002-04-05 23:01:14 +0000693 failure, it returns false and raises the appropriate exception.
Fred Drake3adf79e2001-10-12 19:01:43 +0000694\end{cfuncdesc}
695
Brett Cannon711e7d92004-07-10 22:20:32 +0000696\begin{cfuncdesc}{int}{PyArg_VaParse}{PyObject *args, char *format,
697 va_list vargs}
698 Identical to \cfunction{PyArg_ParseTuple()}, except that it accepts a
699 va_list rather than a variable number of arguments.
700\end{cfuncdesc}
701
Fred Drake3adf79e2001-10-12 19:01:43 +0000702\begin{cfuncdesc}{int}{PyArg_ParseTupleAndKeywords}{PyObject *args,
703 PyObject *kw, char *format, char *keywords[],
704 \moreargs}
705 Parse the parameters of a function that takes both positional and
706 keyword parameters into local variables. Returns true on success;
707 on failure, it returns false and raises the appropriate exception.
Fred Drake3adf79e2001-10-12 19:01:43 +0000708\end{cfuncdesc}
709
Brett Cannon711e7d92004-07-10 22:20:32 +0000710\begin{cfuncdesc}{int}{PyArg_VaParseTupleAndKeywords}{PyObject *args,
711 PyObject *kw, char *format, char *keywords[],
712 va_list vargs}
713 Identical to \cfunction{PyArg_ParseTupleAndKeywords()}, except that it
714 accepts a va_list rather than a variable number of arguments.
715\end{cfuncdesc}
716
Fred Drake3adf79e2001-10-12 19:01:43 +0000717\begin{cfuncdesc}{int}{PyArg_Parse}{PyObject *args, char *format,
718 \moreargs}
719 Function used to deconstruct the argument lists of ``old-style''
720 functions --- these are functions which use the
721 \constant{METH_OLDARGS} parameter parsing method. This is not
722 recommended for use in parameter parsing in new code, and most code
723 in the standard interpreter has been modified to no longer use this
724 for that purpose. It does remain a convenient way to decompose
725 other tuples, however, and may continue to be used for that
726 purpose.
727\end{cfuncdesc}
728
Fred Drakec84f2c52001-10-23 21:10:18 +0000729\begin{cfuncdesc}{int}{PyArg_UnpackTuple}{PyObject *args, char *name,
730 int min, int max, \moreargs}
731 A simpler form of parameter retrieval which does not use a format
732 string to specify the types of the arguments. Functions which use
733 this method to retrieve their parameters should be declared as
734 \constant{METH_VARARGS} in function or method tables. The tuple
735 containing the actual parameters should be passed as \var{args}; it
736 must actually be a tuple. The length of the tuple must be at least
737 \var{min} and no more than \var{max}; \var{min} and \var{max} may be
738 equal. Additional arguments must be passed to the function, each of
739 which should be a pointer to a \ctype{PyObject*} variable; these
740 will be filled in with the values from \var{args}; they will contain
741 borrowed references. The variables which correspond to optional
742 parameters not given by \var{args} will not be filled in; these
743 should be initialized by the caller.
744 This function returns true on success and false if \var{args} is not
745 a tuple or contains the wrong number of elements; an exception will
746 be set if there was a failure.
747
748 This is an example of the use of this function, taken from the
749 sources for the \module{_weakref} helper module for weak references:
750
751\begin{verbatim}
752static PyObject *
753weakref_ref(PyObject *self, PyObject *args)
754{
755 PyObject *object;
756 PyObject *callback = NULL;
757 PyObject *result = NULL;
758
759 if (PyArg_UnpackTuple(args, "ref", 1, 2, &object, &callback)) {
760 result = PyWeakref_NewRef(object, callback);
761 }
762 return result;
763}
764\end{verbatim}
765
766 The call to \cfunction{PyArg_UnpackTuple()} in this example is
767 entirely equivalent to this call to \cfunction{PyArg_ParseTuple()}:
768
769\begin{verbatim}
770PyArg_ParseTuple(args, "O|O:ref", &object, &callback)
771\end{verbatim}
772
773 \versionadded{2.2}
774\end{cfuncdesc}
775
Fred Drake3adf79e2001-10-12 19:01:43 +0000776\begin{cfuncdesc}{PyObject*}{Py_BuildValue}{char *format,
777 \moreargs}
778 Create a new value based on a format string similar to those
779 accepted by the \cfunction{PyArg_Parse*()} family of functions and a
780 sequence of values. Returns the value or \NULL{} in the case of an
Fred Drake68304cc2002-04-05 23:01:14 +0000781 error; an exception will be raised if \NULL{} is returned.
782
783 \cfunction{Py_BuildValue()} does not always build a tuple. It
784 builds a tuple only if its format string contains two or more format
785 units. If the format string is empty, it returns \code{None}; if it
786 contains exactly one format unit, it returns whatever object is
787 described by that format unit. To force it to return a tuple of
788 size 0 or one, parenthesize the format string.
789
790 When memory buffers are passed as parameters to supply data to build
791 objects, as for the \samp{s} and \samp{s\#} formats, the required
792 data is copied. Buffers provided by the caller are never referenced
793 by the objects created by \cfunction{Py_BuildValue()}. In other
794 words, if your code invokes \cfunction{malloc()} and passes the
795 allocated memory to \cfunction{Py_BuildValue()}, your code is
796 responsible for calling \cfunction{free()} for that memory once
797 \cfunction{Py_BuildValue()} returns.
798
799 In the following description, the quoted form is the format unit;
800 the entry in (round) parentheses is the Python object type that the
801 format unit will return; and the entry in [square] brackets is the
802 type of the C value(s) to be passed.
803
804 The characters space, tab, colon and comma are ignored in format
805 strings (but not within format units such as \samp{s\#}). This can
806 be used to make long format strings a tad more readable.
807
808 \begin{description}
809 \item[\samp{s} (string) {[char *]}]
810 Convert a null-terminated C string to a Python object. If the C
811 string pointer is \NULL, \code{None} is used.
812
813 \item[\samp{s\#} (string) {[char *, int]}]
814 Convert a C string and its length to a Python object. If the C
815 string pointer is \NULL, the length is ignored and \code{None} is
816 returned.
817
818 \item[\samp{z} (string or \code{None}) {[char *]}]
819 Same as \samp{s}.
820
821 \item[\samp{z\#} (string or \code{None}) {[char *, int]}]
822 Same as \samp{s\#}.
823
824 \item[\samp{u} (Unicode string) {[Py_UNICODE *]}]
Tim Peterscfd575d2004-08-02 03:46:45 +0000825 Convert a null-terminated buffer of Unicode (UCS-2 or UCS-4)
826 data to a Python Unicode object. If the Unicode buffer pointer
Martin v. Löwis9bc4f2d2004-06-03 09:55:28 +0000827 is \NULL, \code{None} is returned.
Fred Drake68304cc2002-04-05 23:01:14 +0000828
829 \item[\samp{u\#} (Unicode string) {[Py_UNICODE *, int]}]
Tim Peterscfd575d2004-08-02 03:46:45 +0000830 Convert a Unicode (UCS-2 or UCS-4) data buffer and its length
831 to a Python Unicode object. If the Unicode buffer pointer
Martin v. Löwis9bc4f2d2004-06-03 09:55:28 +0000832 is \NULL, the length is ignored and \code{None} is returned.
Fred Drake68304cc2002-04-05 23:01:14 +0000833
834 \item[\samp{i} (integer) {[int]}]
835 Convert a plain C \ctype{int} to a Python integer object.
836
837 \item[\samp{b} (integer) {[char]}]
838 Same as \samp{i}.
839
840 \item[\samp{h} (integer) {[short int]}]
841 Same as \samp{i}.
842
843 \item[\samp{l} (integer) {[long int]}]
844 Convert a C \ctype{long int} to a Python integer object.
845
846 \item[\samp{c} (string of length 1) {[char]}]
847 Convert a C \ctype{int} representing a character to a Python
848 string of length 1.
849
850 \item[\samp{d} (float) {[double]}]
851 Convert a C \ctype{double} to a Python floating point number.
852
853 \item[\samp{f} (float) {[float]}]
854 Same as \samp{d}.
855
856 \item[\samp{D} (complex) {[Py_complex *]}]
857 Convert a C \ctype{Py_complex} structure to a Python complex
858 number.
859
860 \item[\samp{O} (object) {[PyObject *]}]
861 Pass a Python object untouched (except for its reference count,
862 which is incremented by one). If the object passed in is a
863 \NULL{} pointer, it is assumed that this was caused because the
864 call producing the argument found an error and set an exception.
865 Therefore, \cfunction{Py_BuildValue()} will return \NULL{} but
866 won't raise an exception. If no exception has been raised yet,
867 \exception{SystemError} is set.
868
869 \item[\samp{S} (object) {[PyObject *]}]
870 Same as \samp{O}.
871
Fred Drake68304cc2002-04-05 23:01:14 +0000872 \item[\samp{N} (object) {[PyObject *]}]
873 Same as \samp{O}, except it doesn't increment the reference count
874 on the object. Useful when the object is created by a call to an
875 object constructor in the argument list.
876
877 \item[\samp{O\&} (object) {[\var{converter}, \var{anything}]}]
878 Convert \var{anything} to a Python object through a
879 \var{converter} function. The function is called with
880 \var{anything} (which should be compatible with \ctype{void *}) as
881 its argument and should return a ``new'' Python object, or \NULL{}
882 if an error occurred.
883
884 \item[\samp{(\var{items})} (tuple) {[\var{matching-items}]}]
885 Convert a sequence of C values to a Python tuple with the same
886 number of items.
887
888 \item[\samp{[\var{items}]} (list) {[\var{matching-items}]}]
889 Convert a sequence of C values to a Python list with the same
890 number of items.
891
892 \item[\samp{\{\var{items}\}} (dictionary) {[\var{matching-items}]}]
893 Convert a sequence of C values to a Python dictionary. Each pair
894 of consecutive C values adds one item to the dictionary, serving
895 as key and value, respectively.
896
897 \end{description}
898
899 If there is an error in the format string, the
900 \exception{SystemError} exception is set and \NULL{} returned.
Fred Drake3adf79e2001-10-12 19:01:43 +0000901\end{cfuncdesc}