Updates of recent changes to logging.
[python.git] / Doc / api / utilities.tex
blob93e3796455612d23c60f6ae018ea60158208bf5d
1 \chapter{Utilities \label{utilities}}
3 The functions in this chapter perform various utility tasks, ranging
4 from helping C code be more portable across platforms, using Python
5 modules from C, and parsing function arguments and constructing Python
6 values from C values.
9 \section{Operating System Utilities \label{os}}
11 \begin{cfuncdesc}{int}{Py_FdIsInteractive}{FILE *fp, const 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}
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}
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}
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). \constant{USE_STACKCHECK} will be
38 defined automatically; you should never change the definition in
39 your own code.
40 \end{cfuncdesc}
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}
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}
59 \section{Process Control \label{processControl}}
61 \begin{cfuncdesc}{void}{Py_FatalError}{const char *message}
62 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}
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}
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 finalization will have completed before the cleanup
88 function, no Python APIs should be called by \var{func}.
89 \end{cfuncdesc}
92 \section{Importing Modules \label{importing}}
94 \begin{cfuncdesc}{PyObject*}{PyImport_ImportModule}{const 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
108 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}
113 \withsubitem{(in module sys)}{\ttindex{modules}}
114 \end{cfuncdesc}
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.
123 The return value is a new reference to the imported module or
124 top-level package, or \NULL{} with an exception set on failure (before
125 Python 2.4, the
126 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.
130 \versionchanged[failing imports remove incomplete module objects]{2.4}
131 \end{cfuncdesc}
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}
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}
151 \begin{cfuncdesc}{PyObject*}{PyImport_AddModule}{const char *name}
152 Return the module object corresponding to a module name. The
153 \var{name} argument may be of the form \code{package.module}.
154 First check the modules dictionary if there's one there, and if not,
155 create a new one and insert it in the modules dictionary.
156 Return \NULL{} with an exception set on failure.
157 \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
160 import a module. Package structures implied by a dotted name for
161 \var{name} are not created if not already present.}
162 \end{cfuncdesc}
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{}
169 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.
178 This function will reload the module if it was already imported. See
179 \cfunction{PyImport_ReloadModule()} for the intended way to reload a
180 module.
182 If \var{name} points to a dotted name of the
183 form \code{package.module}, any package structures not already
184 created will still not be created.
186 \versionchanged[\var{name} is removed from \code{sys.modules} in error cases]{2.4}
188 \end{cfuncdesc}
190 \begin{cfuncdesc}{long}{PyImport_GetMagicNumber}{}
191 Return the magic number for Python bytecode files
192 (a.k.a. \file{.pyc} and \file{.pyo} files). The magic number should
193 be present in the first four bytes of the bytecode file, in
194 little-endian byte order.
195 \end{cfuncdesc}
197 \begin{cfuncdesc}{PyObject*}{PyImport_GetModuleDict}{}
198 Return the dictionary used for the module administration
199 (a.k.a.\ \code{sys.modules}). Note that this is a per-interpreter
200 variable.
201 \end{cfuncdesc}
203 \begin{cfuncdesc}{void}{_PyImport_Init}{}
204 Initialize the import mechanism. For internal use only.
205 \end{cfuncdesc}
207 \begin{cfuncdesc}{void}{PyImport_Cleanup}{}
208 Empty the module table. For internal use only.
209 \end{cfuncdesc}
211 \begin{cfuncdesc}{void}{_PyImport_Fini}{}
212 Finalize the import mechanism. For internal use only.
213 \end{cfuncdesc}
215 \begin{cfuncdesc}{PyObject*}{_PyImport_FindExtension}{char *, char *}
216 For internal use only.
217 \end{cfuncdesc}
219 \begin{cfuncdesc}{PyObject*}{_PyImport_FixupExtension}{char *, char *}
220 For internal use only.
221 \end{cfuncdesc}
223 \begin{cfuncdesc}{int}{PyImport_ImportFrozenModule}{char *name}
224 Load a frozen module named \var{name}. Return \code{1} for success,
225 \code{0} if the module is not found, and \code{-1} with an exception
226 set if the initialization failed. To access the imported module on
227 a successful load, use \cfunction{PyImport_ImportModule()}. (Note
228 the misnomer --- this function would reload the module if it was
229 already imported.)
230 \end{cfuncdesc}
232 \begin{ctypedesc}[_frozen]{struct _frozen}
233 This is the structure type definition for frozen module descriptors,
234 as generated by the \program{freeze}\index{freeze utility} utility
235 (see \file{Tools/freeze/} in the Python source distribution). Its
236 definition, found in \file{Include/import.h}, is:
238 \begin{verbatim}
239 struct _frozen {
240 char *name;
241 unsigned char *code;
242 int size;
244 \end{verbatim}
245 \end{ctypedesc}
247 \begin{cvardesc}{struct _frozen*}{PyImport_FrozenModules}
248 This pointer is initialized to point to an array of \ctype{struct
249 _frozen} records, terminated by one whose members are all \NULL{} or
250 zero. When a frozen module is imported, it is searched in this
251 table. Third-party code could play tricks with this to provide a
252 dynamically created collection of frozen modules.
253 \end{cvardesc}
255 \begin{cfuncdesc}{int}{PyImport_AppendInittab}{char *name,
256 void (*initfunc)(void)}
257 Add a single module to the existing table of built-in modules. This
258 is a convenience wrapper around
259 \cfunction{PyImport_ExtendInittab()}, returning \code{-1} if the
260 table could not be extended. The new module can be imported by the
261 name \var{name}, and uses the function \var{initfunc} as the
262 initialization function called on the first attempted import. This
263 should be called before \cfunction{Py_Initialize()}.
264 \end{cfuncdesc}
266 \begin{ctypedesc}[_inittab]{struct _inittab}
267 Structure describing a single entry in the list of built-in
268 modules. Each of these structures gives the name and initialization
269 function for a module built into the interpreter. Programs which
270 embed Python may use an array of these structures in conjunction
271 with \cfunction{PyImport_ExtendInittab()} to provide additional
272 built-in modules. The structure is defined in
273 \file{Include/import.h} as:
275 \begin{verbatim}
276 struct _inittab {
277 char *name;
278 void (*initfunc)(void);
280 \end{verbatim}
281 \end{ctypedesc}
283 \begin{cfuncdesc}{int}{PyImport_ExtendInittab}{struct _inittab *newtab}
284 Add a collection of modules to the table of built-in modules. The
285 \var{newtab} array must end with a sentinel entry which contains
286 \NULL{} for the \member{name} field; failure to provide the sentinel
287 value can result in a memory fault. Returns \code{0} on success or
288 \code{-1} if insufficient memory could be allocated to extend the
289 internal table. In the event of failure, no modules are added to
290 the internal table. This should be called before
291 \cfunction{Py_Initialize()}.
292 \end{cfuncdesc}
295 \section{Data marshalling support \label{marshalling-utils}}
297 These routines allow C code to work with serialized objects using the
298 same data format as the \module{marshal} module. There are functions
299 to write data into the serialization format, and additional functions
300 that can be used to read the data back. Files used to store marshalled
301 data must be opened in binary mode.
303 Numeric values are stored with the least significant byte first.
305 The module supports two versions of the data format: version 0 is the
306 historical version, version 1 (new in Python 2.4) shares interned
307 strings in the file, and upon unmarshalling. \var{Py_MARSHAL_VERSION}
308 indicates the current file format (currently 1).
310 \begin{cfuncdesc}{void}{PyMarshal_WriteLongToFile}{long value, FILE *file, int version}
311 Marshal a \ctype{long} integer, \var{value}, to \var{file}. This
312 will only write the least-significant 32 bits of \var{value};
313 regardless of the size of the native \ctype{long} type.
315 \versionchanged[\var{version} indicates the file format]{2.4}
316 \end{cfuncdesc}
318 \begin{cfuncdesc}{void}{PyMarshal_WriteObjectToFile}{PyObject *value,
319 FILE *file, int version}
320 Marshal a Python object, \var{value}, to \var{file}.
322 \versionchanged[\var{version} indicates the file format]{2.4}
323 \end{cfuncdesc}
325 \begin{cfuncdesc}{PyObject*}{PyMarshal_WriteObjectToString}{PyObject *value, int version}
326 Return a string object containing the marshalled representation of
327 \var{value}.
329 \versionchanged[\var{version} indicates the file format]{2.4}
330 \end{cfuncdesc}
332 The following functions allow marshalled values to be read back in.
334 XXX What about error detection? It appears that reading past the end
335 of the file will always result in a negative numeric value (where
336 that's relevant), but it's not clear that negative values won't be
337 handled properly when there's no error. What's the right way to tell?
338 Should only non-negative values be written using these routines?
340 \begin{cfuncdesc}{long}{PyMarshal_ReadLongFromFile}{FILE *file}
341 Return a C \ctype{long} from the data stream in a \ctype{FILE*}
342 opened for reading. Only a 32-bit value can be read in using
343 this function, regardless of the native size of \ctype{long}.
344 \end{cfuncdesc}
346 \begin{cfuncdesc}{int}{PyMarshal_ReadShortFromFile}{FILE *file}
347 Return a C \ctype{short} from the data stream in a \ctype{FILE*}
348 opened for reading. Only a 16-bit value can be read in using
349 this function, regardless of the native size of \ctype{short}.
350 \end{cfuncdesc}
352 \begin{cfuncdesc}{PyObject*}{PyMarshal_ReadObjectFromFile}{FILE *file}
353 Return a Python object from the data stream in a \ctype{FILE*}
354 opened for reading. On error, sets the appropriate exception
355 (\exception{EOFError} or \exception{TypeError}) and returns \NULL.
356 \end{cfuncdesc}
358 \begin{cfuncdesc}{PyObject*}{PyMarshal_ReadLastObjectFromFile}{FILE *file}
359 Return a Python object from the data stream in a \ctype{FILE*}
360 opened for reading. Unlike
361 \cfunction{PyMarshal_ReadObjectFromFile()}, this function assumes
362 that no further objects will be read from the file, allowing it to
363 aggressively load file data into memory so that the de-serialization
364 can operate from data in memory rather than reading a byte at a time
365 from the file. Only use these variant if you are certain that you
366 won't be reading anything else from the file. On error, sets the
367 appropriate exception (\exception{EOFError} or
368 \exception{TypeError}) and returns \NULL.
369 \end{cfuncdesc}
371 \begin{cfuncdesc}{PyObject*}{PyMarshal_ReadObjectFromString}{char *string,
372 Py_ssize_t len}
373 Return a Python object from the data stream in a character buffer
374 containing \var{len} bytes pointed to by \var{string}. On error,
375 sets the appropriate exception (\exception{EOFError} or
376 \exception{TypeError}) and returns \NULL.
377 \end{cfuncdesc}
380 \section{Parsing arguments and building values
381 \label{arg-parsing}}
383 These functions are useful when creating your own extensions functions
384 and methods. Additional information and examples are available in
385 \citetitle[../ext/ext.html]{Extending and Embedding the Python
386 Interpreter}.
388 The first three of these functions described,
389 \cfunction{PyArg_ParseTuple()},
390 \cfunction{PyArg_ParseTupleAndKeywords()}, and
391 \cfunction{PyArg_Parse()}, all use \emph{format strings} which are
392 used to tell the function about the expected arguments. The format
393 strings use the same syntax for each of these functions.
395 A format string consists of zero or more ``format units.'' A format
396 unit describes one Python object; it is usually a single character or
397 a parenthesized sequence of format units. With a few exceptions, a
398 format unit that is not a parenthesized sequence normally corresponds
399 to a single address argument to these functions. In the following
400 description, the quoted form is the format unit; the entry in (round)
401 parentheses is the Python object type that matches the format unit;
402 and the entry in [square] brackets is the type of the C variable(s)
403 whose address should be passed.
405 \begin{description}
406 \item[\samp{s} (string or Unicode object) {[const char *]}]
407 Convert a Python string or Unicode object to a C pointer to a
408 character string. You must not provide storage for the string
409 itself; a pointer to an existing string is stored into the character
410 pointer variable whose address you pass. The C string is
411 NUL-terminated. The Python string must not contain embedded NUL
412 bytes; if it does, a \exception{TypeError} exception is raised.
413 Unicode objects are converted to C strings using the default
414 encoding. If this conversion fails, a \exception{UnicodeError} is
415 raised.
417 \item[\samp{s\#} (string, Unicode or any read buffer compatible object)
418 {[const char *, int]}]
419 This variant on \samp{s} stores into two C variables, the first one
420 a pointer to a character string, the second one its length. In this
421 case the Python string may contain embedded null bytes. Unicode
422 objects pass back a pointer to the default encoded string version of
423 the object if such a conversion is possible. All other read-buffer
424 compatible objects pass back a reference to the raw internal data
425 representation.
427 \item[\samp{z} (string or \code{None}) {[const char *]}]
428 Like \samp{s}, but the Python object may also be \code{None}, in
429 which case the C pointer is set to \NULL.
431 \item[\samp{z\#} (string or \code{None} or any read buffer
432 compatible object) {[const char *, int]}]
433 This is to \samp{s\#} as \samp{z} is to \samp{s}.
435 \item[\samp{u} (Unicode object) {[Py_UNICODE *]}]
436 Convert a Python Unicode object to a C pointer to a NUL-terminated
437 buffer of 16-bit Unicode (UTF-16) data. As with \samp{s}, there is
438 no need to provide storage for the Unicode data buffer; a pointer to
439 the existing Unicode data is stored into the \ctype{Py_UNICODE}
440 pointer variable whose address you pass.
442 \item[\samp{u\#} (Unicode object) {[Py_UNICODE *, int]}]
443 This variant on \samp{u} stores into two C variables, the first one
444 a pointer to a Unicode data buffer, the second one its length.
445 Non-Unicode objects are handled by interpreting their read-buffer
446 pointer as pointer to a \ctype{Py_UNICODE} array.
448 \item[\samp{es} (string, Unicode object or character buffer
449 compatible object) {[const char *encoding, char **buffer]}]
450 This variant on \samp{s} is used for encoding Unicode and objects
451 convertible to Unicode into a character buffer. It only works for
452 encoded data without embedded NUL bytes.
454 This format requires two arguments. The first is only used as
455 input, and must be a \ctype{const char*} which points to the name of an
456 encoding as a NUL-terminated string, or \NULL, in which case the
457 default encoding is used. An exception is raised if the named
458 encoding is not known to Python. The second argument must be a
459 \ctype{char**}; the value of the pointer it references will be set
460 to a buffer with the contents of the argument text. The text will
461 be encoded in the encoding specified by the first argument.
463 \cfunction{PyArg_ParseTuple()} will allocate a buffer of the needed
464 size, copy the encoded data into this buffer and adjust
465 \var{*buffer} to reference the newly allocated storage. The caller
466 is responsible for calling \cfunction{PyMem_Free()} to free the
467 allocated buffer after use.
469 \item[\samp{et} (string, Unicode object or character buffer
470 compatible object) {[const char *encoding, char **buffer]}]
471 Same as \samp{es} except that 8-bit string objects are passed
472 through without recoding them. Instead, the implementation assumes
473 that the string object uses the encoding passed in as parameter.
475 \item[\samp{es\#} (string, Unicode object or character buffer compatible
476 object) {[const char *encoding, char **buffer, int *buffer_length]}]
477 This variant on \samp{s\#} is used for encoding Unicode and objects
478 convertible to Unicode into a character buffer. Unlike the
479 \samp{es} format, this variant allows input data which contains NUL
480 characters.
482 It requires three arguments. The first is only used as input, and
483 must be a \ctype{const char*} which points to the name of an encoding as a
484 NUL-terminated string, or \NULL, in which case the default encoding
485 is used. An exception is raised if the named encoding is not known
486 to Python. The second argument must be a \ctype{char**}; the value
487 of the pointer it references will be set to a buffer with the
488 contents of the argument text. The text will be encoded in the
489 encoding specified by the first argument. The third argument must
490 be a pointer to an integer; the referenced integer will be set to
491 the number of bytes in the output buffer.
493 There are two modes of operation:
495 If \var{*buffer} points a \NULL{} pointer, the function will
496 allocate a buffer of the needed size, copy the encoded data into
497 this buffer and set \var{*buffer} to reference the newly allocated
498 storage. The caller is responsible for calling
499 \cfunction{PyMem_Free()} to free the allocated buffer after usage.
501 If \var{*buffer} points to a non-\NULL{} pointer (an already
502 allocated buffer), \cfunction{PyArg_ParseTuple()} will use this
503 location as the buffer and interpret the initial value of
504 \var{*buffer_length} as the buffer size. It will then copy the
505 encoded data into the buffer and NUL-terminate it. If the buffer
506 is not large enough, a \exception{ValueError} will be set.
508 In both cases, \var{*buffer_length} is set to the length of the
509 encoded data without the trailing NUL byte.
511 \item[\samp{et\#} (string, Unicode object or character buffer compatible
512 object) {[const char *encoding, char **buffer]}]
513 Same as \samp{es\#} except that string objects are passed through
514 without recoding them. Instead, the implementation assumes that the
515 string object uses the encoding passed in as parameter.
517 \item[\samp{b} (integer) {[char]}]
518 Convert a Python integer to a tiny int, stored in a C \ctype{char}.
520 \item[\samp{B} (integer) {[unsigned char]}]
521 Convert a Python integer to a tiny int without overflow checking,
522 stored in a C \ctype{unsigned char}. \versionadded{2.3}
524 \item[\samp{h} (integer) {[short int]}]
525 Convert a Python integer to a C \ctype{short int}.
527 \item[\samp{H} (integer) {[unsigned short int]}]
528 Convert a Python integer to a C \ctype{unsigned short int}, without
529 overflow checking. \versionadded{2.3}
531 \item[\samp{i} (integer) {[int]}]
532 Convert a Python integer to a plain C \ctype{int}.
534 \item[\samp{I} (integer) {[unsigned int]}]
535 Convert a Python integer to a C \ctype{unsigned int}, without
536 overflow checking. \versionadded{2.3}
538 \item[\samp{l} (integer) {[long int]}]
539 Convert a Python integer to a C \ctype{long int}.
541 \item[\samp{k} (integer) {[unsigned long]}]
542 Convert a Python integer or long integer to a C \ctype{unsigned long} without
543 overflow checking. \versionadded{2.3}
545 \item[\samp{L} (integer) {[PY_LONG_LONG]}]
546 Convert a Python integer to a C \ctype{long long}. This format is
547 only available on platforms that support \ctype{long long} (or
548 \ctype{_int64} on Windows).
550 \item[\samp{K} (integer) {[unsigned PY_LONG_LONG]}]
551 Convert a Python integer or long integer to a C \ctype{unsigned long long}
552 without overflow checking. This format is only available on
553 platforms that support \ctype{unsigned long long} (or
554 \ctype{unsigned _int64} on Windows). \versionadded{2.3}
556 \item[\samp{n} (integer) {[Py_ssize_t]}]
557 Convert a Python integer or long integer to a C \ctype{Py_ssize_t}.
558 \versionadded{2.5}
560 \item[\samp{c} (string of length 1) {[char]}]
561 Convert a Python character, represented as a string of length 1, to
562 a C \ctype{char}.
564 \item[\samp{f} (float) {[float]}]
565 Convert a Python floating point number to a C \ctype{float}.
567 \item[\samp{d} (float) {[double]}]
568 Convert a Python floating point number to a C \ctype{double}.
570 \item[\samp{D} (complex) {[Py_complex]}]
571 Convert a Python complex number to a C \ctype{Py_complex} structure.
573 \item[\samp{O} (object) {[PyObject *]}]
574 Store a Python object (without any conversion) in a C object
575 pointer. The C program thus receives the actual object that was
576 passed. The object's reference count is not increased. The pointer
577 stored is not \NULL.
579 \item[\samp{O!} (object) {[\var{typeobject}, PyObject *]}]
580 Store a Python object in a C object pointer. This is similar to
581 \samp{O}, but takes two C arguments: the first is the address of a
582 Python type object, the second is the address of the C variable (of
583 type \ctype{PyObject*}) into which the object pointer is stored. If
584 the Python object does not have the required type,
585 \exception{TypeError} is raised.
587 \item[\samp{O\&} (object) {[\var{converter}, \var{anything}]}]
588 Convert a Python object to a C variable through a \var{converter}
589 function. This takes two arguments: the first is a function, the
590 second is the address of a C variable (of arbitrary type), converted
591 to \ctype{void *}. The \var{converter} function in turn is called
592 as follows:
594 \var{status}\code{ = }\var{converter}\code{(}\var{object},
595 \var{address}\code{);}
597 where \var{object} is the Python object to be converted and
598 \var{address} is the \ctype{void*} argument that was passed to the
599 \cfunction{PyArg_Parse*()} function. The returned \var{status}
600 should be \code{1} for a successful conversion and \code{0} if the
601 conversion has failed. When the conversion fails, the
602 \var{converter} function should raise an exception.
604 \item[\samp{S} (string) {[PyStringObject *]}]
605 Like \samp{O} but requires that the Python object is a string
606 object. Raises \exception{TypeError} if the object is not a string
607 object. The C variable may also be declared as \ctype{PyObject*}.
609 \item[\samp{U} (Unicode string) {[PyUnicodeObject *]}]
610 Like \samp{O} but requires that the Python object is a Unicode
611 object. Raises \exception{TypeError} if the object is not a Unicode
612 object. The C variable may also be declared as \ctype{PyObject*}.
614 \item[\samp{t\#} (read-only character buffer) {[char *, int]}]
615 Like \samp{s\#}, but accepts any object which implements the
616 read-only buffer interface. The \ctype{char*} variable is set to
617 point to the first byte of the buffer, and the \ctype{int} is set to
618 the length of the buffer. Only single-segment buffer objects are
619 accepted; \exception{TypeError} is raised for all others.
621 \item[\samp{w} (read-write character buffer) {[char *]}]
622 Similar to \samp{s}, but accepts any object which implements the
623 read-write buffer interface. The caller must determine the length
624 of the buffer by other means, or use \samp{w\#} instead. Only
625 single-segment buffer objects are accepted; \exception{TypeError} is
626 raised for all others.
628 \item[\samp{w\#} (read-write character buffer) {[char *, int]}]
629 Like \samp{s\#}, but accepts any object which implements the
630 read-write buffer interface. The \ctype{char *} variable is set to
631 point to the first byte of the buffer, and the \ctype{int} is set to
632 the length of the buffer. Only single-segment buffer objects are
633 accepted; \exception{TypeError} is raised for all others.
635 \item[\samp{(\var{items})} (tuple) {[\var{matching-items}]}]
636 The object must be a Python sequence whose length is the number of
637 format units in \var{items}. The C arguments must correspond to the
638 individual format units in \var{items}. Format units for sequences
639 may be nested.
641 \note{Prior to Python version 1.5.2, this format specifier only
642 accepted a tuple containing the individual parameters, not an
643 arbitrary sequence. Code which previously caused
644 \exception{TypeError} to be raised here may now proceed without an
645 exception. This is not expected to be a problem for existing code.}
646 \end{description}
648 It is possible to pass Python long integers where integers are
649 requested; however no proper range checking is done --- the most
650 significant bits are silently truncated when the receiving field is
651 too small to receive the value (actually, the semantics are inherited
652 from downcasts in C --- your mileage may vary).
654 A few other characters have a meaning in a format string. These may
655 not occur inside nested parentheses. They are:
657 \begin{description}
658 \item[\samp{|}]
659 Indicates that the remaining arguments in the Python argument list
660 are optional. The C variables corresponding to optional arguments
661 should be initialized to their default value --- when an optional
662 argument is not specified, \cfunction{PyArg_ParseTuple()} does not
663 touch the contents of the corresponding C variable(s).
665 \item[\samp{:}]
666 The list of format units ends here; the string after the colon is
667 used as the function name in error messages (the ``associated
668 value'' of the exception that \cfunction{PyArg_ParseTuple()}
669 raises).
671 \item[\samp{;}]
672 The list of format units ends here; the string after the semicolon
673 is used as the error message \emph{instead} of the default error
674 message. Clearly, \samp{:} and \samp{;} mutually exclude each
675 other.
676 \end{description}
678 Note that any Python object references which are provided to the
679 caller are \emph{borrowed} references; do not decrement their
680 reference count!
682 Additional arguments passed to these functions must be addresses of
683 variables whose type is determined by the format string; these are
684 used to store values from the input tuple. There are a few cases, as
685 described in the list of format units above, where these parameters
686 are used as input values; they should match what is specified for the
687 corresponding format unit in that case.
689 For the conversion to succeed, the \var{arg} object must match the
690 format and the format must be exhausted. On success, the
691 \cfunction{PyArg_Parse*()} functions return true, otherwise they
692 return false and raise an appropriate exception.
694 \begin{cfuncdesc}{int}{PyArg_ParseTuple}{PyObject *args, const char *format,
695 \moreargs}
696 Parse the parameters of a function that takes only positional
697 parameters into local variables. Returns true on success; on
698 failure, it returns false and raises the appropriate exception.
699 \end{cfuncdesc}
701 \begin{cfuncdesc}{int}{PyArg_VaParse}{PyObject *args, const char *format,
702 va_list vargs}
703 Identical to \cfunction{PyArg_ParseTuple()}, except that it accepts a
704 va_list rather than a variable number of arguments.
705 \end{cfuncdesc}
707 \begin{cfuncdesc}{int}{PyArg_ParseTupleAndKeywords}{PyObject *args,
708 PyObject *kw, const char *format, char *keywords[],
709 \moreargs}
710 Parse the parameters of a function that takes both positional and
711 keyword parameters into local variables. Returns true on success;
712 on failure, it returns false and raises the appropriate exception.
713 \end{cfuncdesc}
715 \begin{cfuncdesc}{int}{PyArg_VaParseTupleAndKeywords}{PyObject *args,
716 PyObject *kw, const char *format, char *keywords[],
717 va_list vargs}
718 Identical to \cfunction{PyArg_ParseTupleAndKeywords()}, except that it
719 accepts a va_list rather than a variable number of arguments.
720 \end{cfuncdesc}
722 \begin{cfuncdesc}{int}{PyArg_Parse}{PyObject *args, const char *format,
723 \moreargs}
724 Function used to deconstruct the argument lists of ``old-style''
725 functions --- these are functions which use the
726 \constant{METH_OLDARGS} parameter parsing method. This is not
727 recommended for use in parameter parsing in new code, and most code
728 in the standard interpreter has been modified to no longer use this
729 for that purpose. It does remain a convenient way to decompose
730 other tuples, however, and may continue to be used for that
731 purpose.
732 \end{cfuncdesc}
734 \begin{cfuncdesc}{int}{PyArg_UnpackTuple}{PyObject *args, const char *name,
735 Py_ssize_t min, Py_ssize_t max, \moreargs}
736 A simpler form of parameter retrieval which does not use a format
737 string to specify the types of the arguments. Functions which use
738 this method to retrieve their parameters should be declared as
739 \constant{METH_VARARGS} in function or method tables. The tuple
740 containing the actual parameters should be passed as \var{args}; it
741 must actually be a tuple. The length of the tuple must be at least
742 \var{min} and no more than \var{max}; \var{min} and \var{max} may be
743 equal. Additional arguments must be passed to the function, each of
744 which should be a pointer to a \ctype{PyObject*} variable; these
745 will be filled in with the values from \var{args}; they will contain
746 borrowed references. The variables which correspond to optional
747 parameters not given by \var{args} will not be filled in; these
748 should be initialized by the caller.
749 This function returns true on success and false if \var{args} is not
750 a tuple or contains the wrong number of elements; an exception will
751 be set if there was a failure.
753 This is an example of the use of this function, taken from the
754 sources for the \module{_weakref} helper module for weak references:
756 \begin{verbatim}
757 static PyObject *
758 weakref_ref(PyObject *self, PyObject *args)
760 PyObject *object;
761 PyObject *callback = NULL;
762 PyObject *result = NULL;
764 if (PyArg_UnpackTuple(args, "ref", 1, 2, &object, &callback)) {
765 result = PyWeakref_NewRef(object, callback);
767 return result;
769 \end{verbatim}
771 The call to \cfunction{PyArg_UnpackTuple()} in this example is
772 entirely equivalent to this call to \cfunction{PyArg_ParseTuple()}:
774 \begin{verbatim}
775 PyArg_ParseTuple(args, "O|O:ref", &object, &callback)
776 \end{verbatim}
778 \versionadded{2.2}
779 \end{cfuncdesc}
781 \begin{cfuncdesc}{PyObject*}{Py_BuildValue}{const char *format,
782 \moreargs}
783 Create a new value based on a format string similar to those
784 accepted by the \cfunction{PyArg_Parse*()} family of functions and a
785 sequence of values. Returns the value or \NULL{} in the case of an
786 error; an exception will be raised if \NULL{} is returned.
788 \cfunction{Py_BuildValue()} does not always build a tuple. It
789 builds a tuple only if its format string contains two or more format
790 units. If the format string is empty, it returns \code{None}; if it
791 contains exactly one format unit, it returns whatever object is
792 described by that format unit. To force it to return a tuple of
793 size 0 or one, parenthesize the format string.
795 When memory buffers are passed as parameters to supply data to build
796 objects, as for the \samp{s} and \samp{s\#} formats, the required
797 data is copied. Buffers provided by the caller are never referenced
798 by the objects created by \cfunction{Py_BuildValue()}. In other
799 words, if your code invokes \cfunction{malloc()} and passes the
800 allocated memory to \cfunction{Py_BuildValue()}, your code is
801 responsible for calling \cfunction{free()} for that memory once
802 \cfunction{Py_BuildValue()} returns.
804 In the following description, the quoted form is the format unit;
805 the entry in (round) parentheses is the Python object type that the
806 format unit will return; and the entry in [square] brackets is the
807 type of the C value(s) to be passed.
809 The characters space, tab, colon and comma are ignored in format
810 strings (but not within format units such as \samp{s\#}). This can
811 be used to make long format strings a tad more readable.
813 \begin{description}
814 \item[\samp{s} (string) {[char *]}]
815 Convert a null-terminated C string to a Python object. If the C
816 string pointer is \NULL, \code{None} is used.
818 \item[\samp{s\#} (string) {[char *, int]}]
819 Convert a C string and its length to a Python object. If the C
820 string pointer is \NULL, the length is ignored and \code{None} is
821 returned.
823 \item[\samp{z} (string or \code{None}) {[char *]}]
824 Same as \samp{s}.
826 \item[\samp{z\#} (string or \code{None}) {[char *, int]}]
827 Same as \samp{s\#}.
829 \item[\samp{u} (Unicode string) {[Py_UNICODE *]}]
830 Convert a null-terminated buffer of Unicode (UCS-2 or UCS-4)
831 data to a Python Unicode object. If the Unicode buffer pointer
832 is \NULL, \code{None} is returned.
834 \item[\samp{u\#} (Unicode string) {[Py_UNICODE *, int]}]
835 Convert a Unicode (UCS-2 or UCS-4) data buffer and its length
836 to a Python Unicode object. If the Unicode buffer pointer
837 is \NULL, the length is ignored and \code{None} is returned.
839 \item[\samp{i} (integer) {[int]}]
840 Convert a plain C \ctype{int} to a Python integer object.
842 \item[\samp{b} (integer) {[char]}]
843 Convert a plain C \ctype{char} to a Python integer object.
845 \item[\samp{h} (integer) {[short int]}]
846 Convert a plain C \ctype{short int} to a Python integer object.
848 \item[\samp{l} (integer) {[long int]}]
849 Convert a C \ctype{long int} to a Python integer object.
851 \item[\samp{B} (integer) {[unsigned char]}]
852 Convert a C \ctype{unsigned char} to a Python integer object.
854 \item[\samp{H} (integer) {[unsigned short int]}]
855 Convert a C \ctype{unsigned short int} to a Python integer object.
857 \item[\samp{I} (integer/long) {[unsigned int]}]
858 Convert a C \ctype{unsigned int} to a Python integer object
859 or a Python long integer object, if it is larger than \code{sys.maxint}.
861 \item[\samp{k} (integer/long) {[unsigned long]}]
862 Convert a C \ctype{unsigned long} to a Python integer object
863 or a Python long integer object, if it is larger than \code{sys.maxint}.
865 \item[\samp{L} (long) {[PY_LONG_LONG]}]
866 Convert a C \ctype{long long} to a Python long integer object. Only
867 available on platforms that support \ctype{long long}.
869 \item[\samp{K} (long) {[unsigned PY_LONG_LONG]}]
870 Convert a C \ctype{unsigned long long} to a Python long integer object.
871 Only available on platforms that support \ctype{unsigned long long}.
873 \item[\samp{n} (int) {[Py_ssize_t]}]
874 Convert a C \ctype{Py_ssize_t} to a Python integer or long integer.
875 \versionadded{2.5}
877 \item[\samp{c} (string of length 1) {[char]}]
878 Convert a C \ctype{int} representing a character to a Python
879 string of length 1.
881 \item[\samp{d} (float) {[double]}]
882 Convert a C \ctype{double} to a Python floating point number.
884 \item[\samp{f} (float) {[float]}]
885 Same as \samp{d}.
887 \item[\samp{D} (complex) {[Py_complex *]}]
888 Convert a C \ctype{Py_complex} structure to a Python complex
889 number.
891 \item[\samp{O} (object) {[PyObject *]}]
892 Pass a Python object untouched (except for its reference count,
893 which is incremented by one). If the object passed in is a
894 \NULL{} pointer, it is assumed that this was caused because the
895 call producing the argument found an error and set an exception.
896 Therefore, \cfunction{Py_BuildValue()} will return \NULL{} but
897 won't raise an exception. If no exception has been raised yet,
898 \exception{SystemError} is set.
900 \item[\samp{S} (object) {[PyObject *]}]
901 Same as \samp{O}.
903 \item[\samp{N} (object) {[PyObject *]}]
904 Same as \samp{O}, except it doesn't increment the reference count
905 on the object. Useful when the object is created by a call to an
906 object constructor in the argument list.
908 \item[\samp{O\&} (object) {[\var{converter}, \var{anything}]}]
909 Convert \var{anything} to a Python object through a
910 \var{converter} function. The function is called with
911 \var{anything} (which should be compatible with \ctype{void *}) as
912 its argument and should return a ``new'' Python object, or \NULL{}
913 if an error occurred.
915 \item[\samp{(\var{items})} (tuple) {[\var{matching-items}]}]
916 Convert a sequence of C values to a Python tuple with the same
917 number of items.
919 \item[\samp{[\var{items}]} (list) {[\var{matching-items}]}]
920 Convert a sequence of C values to a Python list with the same
921 number of items.
923 \item[\samp{\{\var{items}\}} (dictionary) {[\var{matching-items}]}]
924 Convert a sequence of C values to a Python dictionary. Each pair
925 of consecutive C values adds one item to the dictionary, serving
926 as key and value, respectively.
928 \end{description}
930 If there is an error in the format string, the
931 \exception{SystemError} exception is set and \NULL{} returned.
932 \end{cfuncdesc}
934 \section{String conversion and formatting \label{string-formatting}}
936 Functions for number conversion and formatted string output.
938 \begin{cfuncdesc}{int}{PyOS_snprintf}{char *str, size_t size,
939 const char *format, \moreargs}
940 Output not more than \var{size} bytes to \var{str} according to the format
941 string \var{format} and the extra arguments. See the \UNIX{} man
942 page \manpage{snprintf}{2}.
943 \end{cfuncdesc}
945 \begin{cfuncdesc}{int}{PyOS_vsnprintf}{char *str, size_t size,
946 const char *format, va_list va}
947 Output not more than \var{size} bytes to \var{str} according to the format
948 string \var{format} and the variable argument list \var{va}. \UNIX{}
949 man page \manpage{vsnprintf}{2}.
950 \end{cfuncdesc}
952 \cfunction{PyOS_snprintf} and \cfunction{PyOS_vsnprintf} wrap the
953 Standard C library functions \cfunction{snprintf()} and
954 \cfunction{vsnprintf()}. Their purpose is to guarantee consistent
955 behavior in corner cases, which the Standard C functions do not.
957 The wrappers ensure that \var{str}[\var{size}-1] is always
958 \character{\textbackslash0} upon return. They never write more than
959 \var{size} bytes (including the trailing \character{\textbackslash0}
960 into str. Both functions require that \code{\var{str} != NULL},
961 \code{\var{size} > 0} and \code{\var{format} != NULL}.
963 If the platform doesn't have \cfunction{vsnprintf()} and the buffer
964 size needed to avoid truncation exceeds \var{size} by more than 512
965 bytes, Python aborts with a \var{Py_FatalError}.
967 The return value (\var{rv}) for these functions should be interpreted
968 as follows:
970 \begin{itemize}
972 \item When \code{0 <= \var{rv} < \var{size}}, the output conversion
973 was successful and \var{rv} characters were written to \var{str}
974 (excluding the trailing \character{\textbackslash0} byte at
975 \var{str}[\var{rv}]).
977 \item When \code{\var{rv} >= \var{size}}, the output conversion was
978 truncated and a buffer with \code{\var{rv} + 1} bytes would have
979 been needed to succeed. \var{str}[\var{size}-1] is
980 \character{\textbackslash0} in this case.
982 \item When \code{\var{rv} < 0}, ``something bad happened.''
983 \var{str}[\var{size}-1] is \character{\textbackslash0} in this case
984 too, but the rest of \var{str} is undefined. The exact cause of the
985 error depends on the underlying platform.
987 \end{itemize}
989 The following functions provide locale-independent string to number
990 conversions.
992 \begin{cfuncdesc}{double}{PyOS_ascii_strtod}{const char *nptr, char **endptr}
993 Convert a string to a \ctype{double}. This function behaves like the
994 Standard C function \cfunction{strtod()} does in the C locale. It does
995 this without changing the current locale, since that would not be
996 thread-safe.
998 \cfunction{PyOS_ascii_strtod} should typically be used for reading
999 configuration files or other non-user input that should be locale
1000 independent. \versionadded{2.4}
1002 See the \UNIX{} man page \manpage{strtod}{2} for details.
1004 \end{cfuncdesc}
1006 \begin{cfuncdesc}{char *}{PyOS_ascii_formatd}{char *buffer, size_t buf_len,
1007 const char *format, double d}
1008 Convert a \ctype{double} to a string using the \character{.} as the
1009 decimal separator. \var{format} is a \cfunction{printf()}-style format
1010 string specifying the number format. Allowed conversion characters are
1011 \character{e}, \character{E}, \character{f}, \character{F},
1012 \character{g} and \character{G}.
1014 The return value is a pointer to \var{buffer} with the converted
1015 string or NULL if the conversion failed. \versionadded{2.4}
1016 \end{cfuncdesc}
1018 \begin{cfuncdesc}{double}{PyOS_ascii_atof}{const char *nptr}
1019 Convert a string to a \ctype{double} in a locale-independent
1020 way. \versionadded{2.4}
1022 See the \UNIX{} man page \manpage{atof}{2} for details.
1023 \end{cfuncdesc}