blob: f4fa899ec074866971aebc916665bdfc7508a2b0 [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
108 set on failure (the module may still be created in this case ---
109 examine \code{sys.modules} to find out).
110 \withsubitem{(in module sys)}{\ttindex{modules}}
111\end{cfuncdesc}
112
113\begin{cfuncdesc}{PyObject*}{PyImport_ImportModuleEx}{char *name,
114 PyObject *globals, PyObject *locals, PyObject *fromlist}
115 Import a module. This is best described by referring to the
116 built-in Python function
117 \function{__import__()}\bifuncindex{__import__}, as the standard
118 \function{__import__()} function calls this function directly.
119
120 The return value is a new reference to the imported module or
121 top-level package, or \NULL{} with an exception set on failure (the
122 module may still be created in this case). Like for
123 \function{__import__()}, the return value when a submodule of a
124 package was requested is normally the top-level package, unless a
125 non-empty \var{fromlist} was given.
126\end{cfuncdesc}
127
128\begin{cfuncdesc}{PyObject*}{PyImport_Import}{PyObject *name}
129 This is a higher-level interface that calls the current ``import
130 hook function''. It invokes the \function{__import__()} function
131 from the \code{__builtins__} of the current globals. This means
132 that the import is done using whatever import hooks are installed in
133 the current environment, e.g. by \module{rexec}\refstmodindex{rexec}
134 or \module{ihooks}\refstmodindex{ihooks}.
135\end{cfuncdesc}
136
137\begin{cfuncdesc}{PyObject*}{PyImport_ReloadModule}{PyObject *m}
138 Reload a module. This is best described by referring to the
139 built-in Python function \function{reload()}\bifuncindex{reload}, as
140 the standard \function{reload()} function calls this function
141 directly. Return a new reference to the reloaded module, or \NULL{}
142 with an exception set on failure (the module still exists in this
143 case).
144\end{cfuncdesc}
145
146\begin{cfuncdesc}{PyObject*}{PyImport_AddModule}{char *name}
147 Return the module object corresponding to a module name. The
148 \var{name} argument may be of the form \code{package.module}).
149 First check the modules dictionary if there's one there, and if not,
Raymond Hettinger92016dc2003-09-22 15:27:11 +0000150 create a new one and insert it in the modules dictionary.
Fred Drake674dae22002-11-13 15:13:38 +0000151 Return \NULL{} with an exception set on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +0000152 \note{This function does not load or import the module; if the
153 module wasn't already loaded, you will get an empty module object.
154 Use \cfunction{PyImport_ImportModule()} or one of its variants to
Fred Drake674dae22002-11-13 15:13:38 +0000155 import a module. Package structures implied by a dotted name for
156 \var{name} are not created if not already present.}
Fred Drake3adf79e2001-10-12 19:01:43 +0000157\end{cfuncdesc}
158
159\begin{cfuncdesc}{PyObject*}{PyImport_ExecCodeModule}{char *name, PyObject *co}
160 Given a module name (possibly of the form \code{package.module}) and
161 a code object read from a Python bytecode file or obtained from the
162 built-in function \function{compile()}\bifuncindex{compile}, load
163 the module. Return a new reference to the module object, or \NULL{}
164 with an exception set if an error occurred (the module may still be
Fred Drake674dae22002-11-13 15:13:38 +0000165 created in this case). This function would reload the module if it
166 was already imported. If \var{name} points to a dotted name of the
167 form \code{package.module}, any package structures not already
168 created will still not be created.
Fred Drake3adf79e2001-10-12 19:01:43 +0000169\end{cfuncdesc}
170
171\begin{cfuncdesc}{long}{PyImport_GetMagicNumber}{}
172 Return the magic number for Python bytecode files
173 (a.k.a. \file{.pyc} and \file{.pyo} files). The magic number should
174 be present in the first four bytes of the bytecode file, in
175 little-endian byte order.
176\end{cfuncdesc}
177
178\begin{cfuncdesc}{PyObject*}{PyImport_GetModuleDict}{}
179 Return the dictionary used for the module administration
180 (a.k.a.\ \code{sys.modules}). Note that this is a per-interpreter
181 variable.
182\end{cfuncdesc}
183
184\begin{cfuncdesc}{void}{_PyImport_Init}{}
185 Initialize the import mechanism. For internal use only.
186\end{cfuncdesc}
187
188\begin{cfuncdesc}{void}{PyImport_Cleanup}{}
189 Empty the module table. For internal use only.
190\end{cfuncdesc}
191
192\begin{cfuncdesc}{void}{_PyImport_Fini}{}
193 Finalize the import mechanism. For internal use only.
194\end{cfuncdesc}
195
196\begin{cfuncdesc}{PyObject*}{_PyImport_FindExtension}{char *, char *}
197 For internal use only.
198\end{cfuncdesc}
199
200\begin{cfuncdesc}{PyObject*}{_PyImport_FixupExtension}{char *, char *}
201 For internal use only.
202\end{cfuncdesc}
203
204\begin{cfuncdesc}{int}{PyImport_ImportFrozenModule}{char *name}
205 Load a frozen module named \var{name}. Return \code{1} for success,
206 \code{0} if the module is not found, and \code{-1} with an exception
207 set if the initialization failed. To access the imported module on
208 a successful load, use \cfunction{PyImport_ImportModule()}. (Note
209 the misnomer --- this function would reload the module if it was
210 already imported.)
211\end{cfuncdesc}
212
213\begin{ctypedesc}[_frozen]{struct _frozen}
214 This is the structure type definition for frozen module descriptors,
215 as generated by the \program{freeze}\index{freeze utility} utility
216 (see \file{Tools/freeze/} in the Python source distribution). Its
217 definition, found in \file{Include/import.h}, is:
218
219\begin{verbatim}
220struct _frozen {
221 char *name;
222 unsigned char *code;
223 int size;
224};
225\end{verbatim}
226\end{ctypedesc}
227
228\begin{cvardesc}{struct _frozen*}{PyImport_FrozenModules}
229 This pointer is initialized to point to an array of \ctype{struct
230 _frozen} records, terminated by one whose members are all \NULL{} or
231 zero. When a frozen module is imported, it is searched in this
232 table. Third-party code could play tricks with this to provide a
233 dynamically created collection of frozen modules.
234\end{cvardesc}
235
236\begin{cfuncdesc}{int}{PyImport_AppendInittab}{char *name,
237 void (*initfunc)(void)}
238 Add a single module to the existing table of built-in modules. This
239 is a convenience wrapper around
240 \cfunction{PyImport_ExtendInittab()}, returning \code{-1} if the
241 table could not be extended. The new module can be imported by the
242 name \var{name}, and uses the function \var{initfunc} as the
243 initialization function called on the first attempted import. This
244 should be called before \cfunction{Py_Initialize()}.
245\end{cfuncdesc}
246
247\begin{ctypedesc}[_inittab]{struct _inittab}
248 Structure describing a single entry in the list of built-in
249 modules. Each of these structures gives the name and initialization
250 function for a module built into the interpreter. Programs which
251 embed Python may use an array of these structures in conjunction
252 with \cfunction{PyImport_ExtendInittab()} to provide additional
253 built-in modules. The structure is defined in
254 \file{Include/import.h} as:
255
256\begin{verbatim}
257struct _inittab {
258 char *name;
259 void (*initfunc)(void);
260};
261\end{verbatim}
262\end{ctypedesc}
263
264\begin{cfuncdesc}{int}{PyImport_ExtendInittab}{struct _inittab *newtab}
265 Add a collection of modules to the table of built-in modules. The
266 \var{newtab} array must end with a sentinel entry which contains
267 \NULL{} for the \member{name} field; failure to provide the sentinel
268 value can result in a memory fault. Returns \code{0} on success or
269 \code{-1} if insufficient memory could be allocated to extend the
270 internal table. In the event of failure, no modules are added to
271 the internal table. This should be called before
272 \cfunction{Py_Initialize()}.
273\end{cfuncdesc}
274
275
Fred Drake0fae49f2001-10-14 04:45:51 +0000276\section{Data marshalling support \label{marshalling-utils}}
277
278These routines allow C code to work with serialized objects using the
279same data format as the \module{marshal} module. There are functions
280to write data into the serialization format, and additional functions
281that can be used to read the data back. Files used to store marshalled
282data must be opened in binary mode.
283
284Numeric values are stored with the least significant byte first.
285
286\begin{cfuncdesc}{void}{PyMarshal_WriteLongToFile}{long value, FILE *file}
287 Marshal a \ctype{long} integer, \var{value}, to \var{file}. This
288 will only write the least-significant 32 bits of \var{value};
289 regardless of the size of the native \ctype{long} type.
290\end{cfuncdesc}
291
Fred Drake0fae49f2001-10-14 04:45:51 +0000292\begin{cfuncdesc}{void}{PyMarshal_WriteObjectToFile}{PyObject *value,
293 FILE *file}
Fred Drakeb0840172002-06-17 15:44:18 +0000294 Marshal a Python object, \var{value}, to \var{file}.
Fred Drake0fae49f2001-10-14 04:45:51 +0000295\end{cfuncdesc}
296
297\begin{cfuncdesc}{PyObject*}{PyMarshal_WriteObjectToString}{PyObject *value}
298 Return a string object containing the marshalled representation of
299 \var{value}.
300\end{cfuncdesc}
301
302The following functions allow marshalled values to be read back in.
303
304XXX What about error detection? It appears that reading past the end
305of the file will always result in a negative numeric value (where
306that's relevant), but it's not clear that negative values won't be
307handled properly when there's no error. What's the right way to tell?
308Should only non-negative values be written using these routines?
309
310\begin{cfuncdesc}{long}{PyMarshal_ReadLongFromFile}{FILE *file}
311 Return a C \ctype{long} from the data stream in a \ctype{FILE*}
312 opened for reading. Only a 32-bit value can be read in using
313 this function, regardless of the native size of \ctype{long}.
314\end{cfuncdesc}
315
316\begin{cfuncdesc}{int}{PyMarshal_ReadShortFromFile}{FILE *file}
317 Return a C \ctype{short} from the data stream in a \ctype{FILE*}
318 opened for reading. Only a 16-bit value can be read in using
Fred Drakeb0840172002-06-17 15:44:18 +0000319 this function, regardless of the native size of \ctype{short}.
Fred Drake0fae49f2001-10-14 04:45:51 +0000320\end{cfuncdesc}
321
322\begin{cfuncdesc}{PyObject*}{PyMarshal_ReadObjectFromFile}{FILE *file}
323 Return a Python object from the data stream in a \ctype{FILE*}
324 opened for reading. On error, sets the appropriate exception
325 (\exception{EOFError} or \exception{TypeError}) and returns \NULL.
326\end{cfuncdesc}
327
328\begin{cfuncdesc}{PyObject*}{PyMarshal_ReadLastObjectFromFile}{FILE *file}
329 Return a Python object from the data stream in a \ctype{FILE*}
330 opened for reading. Unlike
331 \cfunction{PyMarshal_ReadObjectFromFile()}, this function assumes
332 that no further objects will be read from the file, allowing it to
333 aggressively load file data into memory so that the de-serialization
334 can operate from data in memory rather than reading a byte at a time
335 from the file. Only use these variant if you are certain that you
336 won't be reading anything else from the file. On error, sets the
337 appropriate exception (\exception{EOFError} or
338 \exception{TypeError}) and returns \NULL.
339\end{cfuncdesc}
340
341\begin{cfuncdesc}{PyObject*}{PyMarshal_ReadObjectFromString}{char *string,
342 int len}
343 Return a Python object from the data stream in a character buffer
344 containing \var{len} bytes pointed to by \var{string}. On error,
345 sets the appropriate exception (\exception{EOFError} or
346 \exception{TypeError}) and returns \NULL.
347\end{cfuncdesc}
348
349
Fred Drake3adf79e2001-10-12 19:01:43 +0000350\section{Parsing arguments and building values
351 \label{arg-parsing}}
352
353These functions are useful when creating your own extensions functions
354and methods. Additional information and examples are available in
355\citetitle[../ext/ext.html]{Extending and Embedding the Python
356Interpreter}.
357
Fred Drake68304cc2002-04-05 23:01:14 +0000358The first three of these functions described,
359\cfunction{PyArg_ParseTuple()},
360\cfunction{PyArg_ParseTupleAndKeywords()}, and
361\cfunction{PyArg_Parse()}, all use \emph{format strings} which are
362used to tell the function about the expected arguments. The format
363strings use the same syntax for each of these functions.
364
365A format string consists of zero or more ``format units.'' A format
366unit describes one Python object; it is usually a single character or
367a parenthesized sequence of format units. With a few exceptions, a
368format unit that is not a parenthesized sequence normally corresponds
369to a single address argument to these functions. In the following
370description, the quoted form is the format unit; the entry in (round)
371parentheses is the Python object type that matches the format unit;
372and the entry in [square] brackets is the type of the C variable(s)
373whose address should be passed.
374
375\begin{description}
376 \item[\samp{s} (string or Unicode object) {[char *]}]
377 Convert a Python string or Unicode object to a C pointer to a
378 character string. You must not provide storage for the string
379 itself; a pointer to an existing string is stored into the character
380 pointer variable whose address you pass. The C string is
381 NUL-terminated. The Python string must not contain embedded NUL
382 bytes; if it does, a \exception{TypeError} exception is raised.
383 Unicode objects are converted to C strings using the default
384 encoding. If this conversion fails, a \exception{UnicodeError} is
385 raised.
386
387 \item[\samp{s\#} (string, Unicode or any read buffer compatible object)
388 {[char *, int]}]
389 This variant on \samp{s} stores into two C variables, the first one
390 a pointer to a character string, the second one its length. In this
391 case the Python string may contain embedded null bytes. Unicode
392 objects pass back a pointer to the default encoded string version of
393 the object if such a conversion is possible. All other read-buffer
394 compatible objects pass back a reference to the raw internal data
395 representation.
396
397 \item[\samp{z} (string or \code{None}) {[char *]}]
398 Like \samp{s}, but the Python object may also be \code{None}, in
399 which case the C pointer is set to \NULL.
400
401 \item[\samp{z\#} (string or \code{None} or any read buffer
402 compatible object) {[char *, int]}]
403 This is to \samp{s\#} as \samp{z} is to \samp{s}.
404
405 \item[\samp{u} (Unicode object) {[Py_UNICODE *]}]
406 Convert a Python Unicode object to a C pointer to a NUL-terminated
407 buffer of 16-bit Unicode (UTF-16) data. As with \samp{s}, there is
408 no need to provide storage for the Unicode data buffer; a pointer to
409 the existing Unicode data is stored into the \ctype{Py_UNICODE}
410 pointer variable whose address you pass.
411
412 \item[\samp{u\#} (Unicode object) {[Py_UNICODE *, int]}]
413 This variant on \samp{u} stores into two C variables, the first one
414 a pointer to a Unicode data buffer, the second one its length.
415 Non-Unicode objects are handled by interpreting their read-buffer
416 pointer as pointer to a \ctype{Py_UNICODE} array.
417
418 \item[\samp{es} (string, Unicode object or character buffer
419 compatible object) {[const char *encoding, char **buffer]}]
420 This variant on \samp{s} is used for encoding Unicode and objects
421 convertible to Unicode into a character buffer. It only works for
422 encoded data without embedded NUL bytes.
423
424 This format requires two arguments. The first is only used as
425 input, and must be a \ctype{char*} which points to the name of an
426 encoding as a NUL-terminated string, or \NULL, in which case the
427 default encoding is used. An exception is raised if the named
428 encoding is not known to Python. The second argument must be a
429 \ctype{char**}; the value of the pointer it references will be set
430 to a buffer with the contents of the argument text. The text will
431 be encoded in the encoding specified by the first argument.
432
433 \cfunction{PyArg_ParseTuple()} will allocate a buffer of the needed
434 size, copy the encoded data into this buffer and adjust
435 \var{*buffer} to reference the newly allocated storage. The caller
436 is responsible for calling \cfunction{PyMem_Free()} to free the
437 allocated buffer after use.
438
439 \item[\samp{et} (string, Unicode object or character buffer
440 compatible object) {[const char *encoding, char **buffer]}]
441 Same as \samp{es} except that 8-bit string objects are passed
442 through without recoding them. Instead, the implementation assumes
443 that the string object uses the encoding passed in as parameter.
444
445 \item[\samp{es\#} (string, Unicode object or character buffer compatible
446 object) {[const char *encoding, char **buffer, int *buffer_length]}]
447 This variant on \samp{s\#} is used for encoding Unicode and objects
448 convertible to Unicode into a character buffer. Unlike the
449 \samp{es} format, this variant allows input data which contains NUL
450 characters.
451
452 It requires three arguments. The first is only used as input, and
453 must be a \ctype{char*} which points to the name of an encoding as a
454 NUL-terminated string, or \NULL, in which case the default encoding
455 is used. An exception is raised if the named encoding is not known
456 to Python. The second argument must be a \ctype{char**}; the value
457 of the pointer it references will be set to a buffer with the
458 contents of the argument text. The text will be encoded in the
459 encoding specified by the first argument. The third argument must
460 be a pointer to an integer; the referenced integer will be set to
461 the number of bytes in the output buffer.
462
463 There are two modes of operation:
464
465 If \var{*buffer} points a \NULL{} pointer, the function will
466 allocate a buffer of the needed size, copy the encoded data into
467 this buffer and set \var{*buffer} to reference the newly allocated
468 storage. The caller is responsible for calling
469 \cfunction{PyMem_Free()} to free the allocated buffer after usage.
470
471 If \var{*buffer} points to a non-\NULL{} pointer (an already
472 allocated buffer), \cfunction{PyArg_ParseTuple()} will use this
473 location as the buffer and interpret the initial value of
474 \var{*buffer_length} as the buffer size. It will then copy the
475 encoded data into the buffer and NUL-terminate it. If the buffer
476 is not large enough, a \exception{ValueError} will be set.
477
478 In both cases, \var{*buffer_length} is set to the length of the
479 encoded data without the trailing NUL byte.
480
481 \item[\samp{et\#} (string, Unicode object or character buffer compatible
482 object) {[const char *encoding, char **buffer]}]
483 Same as \samp{es\#} except that string objects are passed through
484 without recoding them. Instead, the implementation assumes that the
485 string object uses the encoding passed in as parameter.
486
487 \item[\samp{b} (integer) {[char]}]
488 Convert a Python integer to a tiny int, stored in a C \ctype{char}.
489
Thomas Heller42a11722003-04-23 19:27:35 +0000490 \item[\samp{B} (integer) {[unsigned char]}]
491 Convert a Python integer to a tiny int without overflow checking,
492 stored in a C \ctype{unsigned char}. \versionadded{2.3}
493
Fred Drake68304cc2002-04-05 23:01:14 +0000494 \item[\samp{h} (integer) {[short int]}]
495 Convert a Python integer to a C \ctype{short int}.
496
Thomas Heller42a11722003-04-23 19:27:35 +0000497 \item[\samp{H} (integer) {[unsigned short int]}]
498 Convert a Python integer to a C \ctype{unsigned short int}, without
499 overflow checking. \versionadded{2.3}
500
Fred Drake68304cc2002-04-05 23:01:14 +0000501 \item[\samp{i} (integer) {[int]}]
502 Convert a Python integer to a plain C \ctype{int}.
503
Thomas Heller42a11722003-04-23 19:27:35 +0000504 \item[\samp{I} (integer) {[unsigned int]}]
505 Convert a Python integer to a C \ctype{unsigned int}, without
506 overflow checking. \versionadded{2.3}
507
Fred Drake68304cc2002-04-05 23:01:14 +0000508 \item[\samp{l} (integer) {[long int]}]
509 Convert a Python integer to a C \ctype{long int}.
510
Thomas Heller42a11722003-04-23 19:27:35 +0000511 \item[\samp{k} (integer) {[unsigned long]}]
512 Convert a Python integer to a C \ctype{unsigned long} without
513 overflow checking. \versionadded{2.3}
514
Martin v. Löwisb9a0f912003-03-29 10:06:18 +0000515 \item[\samp{L} (integer) {[PY_LONG_LONG]}]
Fred Drake68304cc2002-04-05 23:01:14 +0000516 Convert a Python integer to a C \ctype{long long}. This format is
517 only available on platforms that support \ctype{long long} (or
518 \ctype{_int64} on Windows).
519
Thomas Heller42a11722003-04-23 19:27:35 +0000520 \item[\samp{K} (integer) {[unsigned PY_LONG_LONG]}]
521 Convert a Python integer to a C \ctype{unsigned long long}
522 without overflow checking. This format is only available on
523 platforms that support \ctype{unsigned long long} (or
524 \ctype{unsigned _int64} on Windows). \versionadded{2.3}
525
Fred Drake68304cc2002-04-05 23:01:14 +0000526 \item[\samp{c} (string of length 1) {[char]}]
527 Convert a Python character, represented as a string of length 1, to
528 a C \ctype{char}.
529
530 \item[\samp{f} (float) {[float]}]
531 Convert a Python floating point number to a C \ctype{float}.
532
533 \item[\samp{d} (float) {[double]}]
534 Convert a Python floating point number to a C \ctype{double}.
535
536 \item[\samp{D} (complex) {[Py_complex]}]
537 Convert a Python complex number to a C \ctype{Py_complex} structure.
538
539 \item[\samp{O} (object) {[PyObject *]}]
540 Store a Python object (without any conversion) in a C object
541 pointer. The C program thus receives the actual object that was
542 passed. The object's reference count is not increased. The pointer
543 stored is not \NULL.
544
545 \item[\samp{O!} (object) {[\var{typeobject}, PyObject *]}]
546 Store a Python object in a C object pointer. This is similar to
547 \samp{O}, but takes two C arguments: the first is the address of a
548 Python type object, the second is the address of the C variable (of
549 type \ctype{PyObject*}) into which the object pointer is stored. If
550 the Python object does not have the required type,
551 \exception{TypeError} is raised.
552
553 \item[\samp{O\&} (object) {[\var{converter}, \var{anything}]}]
554 Convert a Python object to a C variable through a \var{converter}
555 function. This takes two arguments: the first is a function, the
556 second is the address of a C variable (of arbitrary type), converted
557 to \ctype{void *}. The \var{converter} function in turn is called
558 as follows:
559
560 \var{status}\code{ = }\var{converter}\code{(}\var{object},
561 \var{address}\code{);}
562
563 where \var{object} is the Python object to be converted and
564 \var{address} is the \ctype{void*} argument that was passed to the
565 \cfunction{PyArg_Parse*()} function. The returned \var{status}
566 should be \code{1} for a successful conversion and \code{0} if the
567 conversion has failed. When the conversion fails, the
568 \var{converter} function should raise an exception.
569
570 \item[\samp{S} (string) {[PyStringObject *]}]
571 Like \samp{O} but requires that the Python object is a string
572 object. Raises \exception{TypeError} if the object is not a string
573 object. The C variable may also be declared as \ctype{PyObject*}.
574
575 \item[\samp{U} (Unicode string) {[PyUnicodeObject *]}]
576 Like \samp{O} but requires that the Python object is a Unicode
577 object. Raises \exception{TypeError} if the object is not a Unicode
578 object. The C variable may also be declared as \ctype{PyObject*}.
579
580 \item[\samp{t\#} (read-only character buffer) {[char *, int]}]
581 Like \samp{s\#}, but accepts any object which implements the
582 read-only buffer interface. The \ctype{char*} variable is set to
583 point to the first byte of the buffer, and the \ctype{int} is set to
584 the length of the buffer. Only single-segment buffer objects are
585 accepted; \exception{TypeError} is raised for all others.
586
587 \item[\samp{w} (read-write character buffer) {[char *]}]
588 Similar to \samp{s}, but accepts any object which implements the
589 read-write buffer interface. The caller must determine the length
590 of the buffer by other means, or use \samp{w\#} instead. Only
591 single-segment buffer objects are accepted; \exception{TypeError} is
592 raised for all others.
593
594 \item[\samp{w\#} (read-write character buffer) {[char *, int]}]
595 Like \samp{s\#}, but accepts any object which implements the
596 read-write buffer interface. The \ctype{char *} variable is set to
597 point to the first byte of the buffer, and the \ctype{int} is set to
598 the length of the buffer. Only single-segment buffer objects are
599 accepted; \exception{TypeError} is raised for all others.
600
601 \item[\samp{(\var{items})} (tuple) {[\var{matching-items}]}]
602 The object must be a Python sequence whose length is the number of
603 format units in \var{items}. The C arguments must correspond to the
604 individual format units in \var{items}. Format units for sequences
605 may be nested.
606
607 \note{Prior to Python version 1.5.2, this format specifier only
608 accepted a tuple containing the individual parameters, not an
609 arbitrary sequence. Code which previously caused
610 \exception{TypeError} to be raised here may now proceed without an
611 exception. This is not expected to be a problem for existing code.}
612\end{description}
613
614It is possible to pass Python long integers where integers are
615requested; however no proper range checking is done --- the most
616significant bits are silently truncated when the receiving field is
617too small to receive the value (actually, the semantics are inherited
618from downcasts in C --- your mileage may vary).
619
620A few other characters have a meaning in a format string. These may
621not occur inside nested parentheses. They are:
622
623\begin{description}
624 \item[\samp{|}]
625 Indicates that the remaining arguments in the Python argument list
626 are optional. The C variables corresponding to optional arguments
627 should be initialized to their default value --- when an optional
628 argument is not specified, \cfunction{PyArg_ParseTuple()} does not
629 touch the contents of the corresponding C variable(s).
630
631 \item[\samp{:}]
632 The list of format units ends here; the string after the colon is
633 used as the function name in error messages (the ``associated
634 value'' of the exception that \cfunction{PyArg_ParseTuple()}
635 raises).
636
637 \item[\samp{;}]
638 The list of format units ends here; the string after the semicolon
639 is used as the error message \emph{instead} of the default error
640 message. Clearly, \samp{:} and \samp{;} mutually exclude each
641 other.
642\end{description}
643
644Note that any Python object references which are provided to the
645caller are \emph{borrowed} references; do not decrement their
646reference count!
647
648Additional arguments passed to these functions must be addresses of
649variables whose type is determined by the format string; these are
650used to store values from the input tuple. There are a few cases, as
651described in the list of format units above, where these parameters
652are used as input values; they should match what is specified for the
653corresponding format unit in that case.
654
655For the conversion to succeed, the \var{arg} object must match the
656format and the format must be exhausted. On success, the
657\cfunction{PyArg_Parse*()} functions return true, otherwise they
658return false and raise an appropriate exception.
659
Fred Drake3adf79e2001-10-12 19:01:43 +0000660\begin{cfuncdesc}{int}{PyArg_ParseTuple}{PyObject *args, char *format,
661 \moreargs}
662 Parse the parameters of a function that takes only positional
663 parameters into local variables. Returns true on success; on
Fred Drake68304cc2002-04-05 23:01:14 +0000664 failure, it returns false and raises the appropriate exception.
Fred Drake3adf79e2001-10-12 19:01:43 +0000665\end{cfuncdesc}
666
667\begin{cfuncdesc}{int}{PyArg_ParseTupleAndKeywords}{PyObject *args,
668 PyObject *kw, char *format, char *keywords[],
669 \moreargs}
670 Parse the parameters of a function that takes both positional and
671 keyword parameters into local variables. Returns true on success;
672 on failure, it returns false and raises the appropriate exception.
Fred Drake3adf79e2001-10-12 19:01:43 +0000673\end{cfuncdesc}
674
675\begin{cfuncdesc}{int}{PyArg_Parse}{PyObject *args, char *format,
676 \moreargs}
677 Function used to deconstruct the argument lists of ``old-style''
678 functions --- these are functions which use the
679 \constant{METH_OLDARGS} parameter parsing method. This is not
680 recommended for use in parameter parsing in new code, and most code
681 in the standard interpreter has been modified to no longer use this
682 for that purpose. It does remain a convenient way to decompose
683 other tuples, however, and may continue to be used for that
684 purpose.
685\end{cfuncdesc}
686
Fred Drakec84f2c52001-10-23 21:10:18 +0000687\begin{cfuncdesc}{int}{PyArg_UnpackTuple}{PyObject *args, char *name,
688 int min, int max, \moreargs}
689 A simpler form of parameter retrieval which does not use a format
690 string to specify the types of the arguments. Functions which use
691 this method to retrieve their parameters should be declared as
692 \constant{METH_VARARGS} in function or method tables. The tuple
693 containing the actual parameters should be passed as \var{args}; it
694 must actually be a tuple. The length of the tuple must be at least
695 \var{min} and no more than \var{max}; \var{min} and \var{max} may be
696 equal. Additional arguments must be passed to the function, each of
697 which should be a pointer to a \ctype{PyObject*} variable; these
698 will be filled in with the values from \var{args}; they will contain
699 borrowed references. The variables which correspond to optional
700 parameters not given by \var{args} will not be filled in; these
701 should be initialized by the caller.
702 This function returns true on success and false if \var{args} is not
703 a tuple or contains the wrong number of elements; an exception will
704 be set if there was a failure.
705
706 This is an example of the use of this function, taken from the
707 sources for the \module{_weakref} helper module for weak references:
708
709\begin{verbatim}
710static PyObject *
711weakref_ref(PyObject *self, PyObject *args)
712{
713 PyObject *object;
714 PyObject *callback = NULL;
715 PyObject *result = NULL;
716
717 if (PyArg_UnpackTuple(args, "ref", 1, 2, &object, &callback)) {
718 result = PyWeakref_NewRef(object, callback);
719 }
720 return result;
721}
722\end{verbatim}
723
724 The call to \cfunction{PyArg_UnpackTuple()} in this example is
725 entirely equivalent to this call to \cfunction{PyArg_ParseTuple()}:
726
727\begin{verbatim}
728PyArg_ParseTuple(args, "O|O:ref", &object, &callback)
729\end{verbatim}
730
731 \versionadded{2.2}
732\end{cfuncdesc}
733
Fred Drake3adf79e2001-10-12 19:01:43 +0000734\begin{cfuncdesc}{PyObject*}{Py_BuildValue}{char *format,
735 \moreargs}
736 Create a new value based on a format string similar to those
737 accepted by the \cfunction{PyArg_Parse*()} family of functions and a
738 sequence of values. Returns the value or \NULL{} in the case of an
Fred Drake68304cc2002-04-05 23:01:14 +0000739 error; an exception will be raised if \NULL{} is returned.
740
741 \cfunction{Py_BuildValue()} does not always build a tuple. It
742 builds a tuple only if its format string contains two or more format
743 units. If the format string is empty, it returns \code{None}; if it
744 contains exactly one format unit, it returns whatever object is
745 described by that format unit. To force it to return a tuple of
746 size 0 or one, parenthesize the format string.
747
748 When memory buffers are passed as parameters to supply data to build
749 objects, as for the \samp{s} and \samp{s\#} formats, the required
750 data is copied. Buffers provided by the caller are never referenced
751 by the objects created by \cfunction{Py_BuildValue()}. In other
752 words, if your code invokes \cfunction{malloc()} and passes the
753 allocated memory to \cfunction{Py_BuildValue()}, your code is
754 responsible for calling \cfunction{free()} for that memory once
755 \cfunction{Py_BuildValue()} returns.
756
757 In the following description, the quoted form is the format unit;
758 the entry in (round) parentheses is the Python object type that the
759 format unit will return; and the entry in [square] brackets is the
760 type of the C value(s) to be passed.
761
762 The characters space, tab, colon and comma are ignored in format
763 strings (but not within format units such as \samp{s\#}). This can
764 be used to make long format strings a tad more readable.
765
766 \begin{description}
767 \item[\samp{s} (string) {[char *]}]
768 Convert a null-terminated C string to a Python object. If the C
769 string pointer is \NULL, \code{None} is used.
770
771 \item[\samp{s\#} (string) {[char *, int]}]
772 Convert a C string and its length to a Python object. If the C
773 string pointer is \NULL, the length is ignored and \code{None} is
774 returned.
775
776 \item[\samp{z} (string or \code{None}) {[char *]}]
777 Same as \samp{s}.
778
779 \item[\samp{z\#} (string or \code{None}) {[char *, int]}]
780 Same as \samp{s\#}.
781
782 \item[\samp{u} (Unicode string) {[Py_UNICODE *]}]
Martin v. Löwis9bc4f2d2004-06-03 09:55:28 +0000783 Convert a null-terminated buffer of Unicode (UCS-2 or UCS-4)
784 data to a Python Unicode object. If the Unicode buffer pointer
785 is \NULL, \code{None} is returned.
Fred Drake68304cc2002-04-05 23:01:14 +0000786
787 \item[\samp{u\#} (Unicode string) {[Py_UNICODE *, int]}]
Martin v. Löwis9bc4f2d2004-06-03 09:55:28 +0000788 Convert a Unicode (UCS-2 or UCS-4) data buffer and its length
789 to a Python Unicode object. If the Unicode buffer pointer
790 is \NULL, the length is ignored and \code{None} is returned.
Fred Drake68304cc2002-04-05 23:01:14 +0000791
792 \item[\samp{i} (integer) {[int]}]
793 Convert a plain C \ctype{int} to a Python integer object.
794
795 \item[\samp{b} (integer) {[char]}]
796 Same as \samp{i}.
797
798 \item[\samp{h} (integer) {[short int]}]
799 Same as \samp{i}.
800
801 \item[\samp{l} (integer) {[long int]}]
802 Convert a C \ctype{long int} to a Python integer object.
803
804 \item[\samp{c} (string of length 1) {[char]}]
805 Convert a C \ctype{int} representing a character to a Python
806 string of length 1.
807
808 \item[\samp{d} (float) {[double]}]
809 Convert a C \ctype{double} to a Python floating point number.
810
811 \item[\samp{f} (float) {[float]}]
812 Same as \samp{d}.
813
814 \item[\samp{D} (complex) {[Py_complex *]}]
815 Convert a C \ctype{Py_complex} structure to a Python complex
816 number.
817
818 \item[\samp{O} (object) {[PyObject *]}]
819 Pass a Python object untouched (except for its reference count,
820 which is incremented by one). If the object passed in is a
821 \NULL{} pointer, it is assumed that this was caused because the
822 call producing the argument found an error and set an exception.
823 Therefore, \cfunction{Py_BuildValue()} will return \NULL{} but
824 won't raise an exception. If no exception has been raised yet,
825 \exception{SystemError} is set.
826
827 \item[\samp{S} (object) {[PyObject *]}]
828 Same as \samp{O}.
829
Fred Drake68304cc2002-04-05 23:01:14 +0000830 \item[\samp{N} (object) {[PyObject *]}]
831 Same as \samp{O}, except it doesn't increment the reference count
832 on the object. Useful when the object is created by a call to an
833 object constructor in the argument list.
834
835 \item[\samp{O\&} (object) {[\var{converter}, \var{anything}]}]
836 Convert \var{anything} to a Python object through a
837 \var{converter} function. The function is called with
838 \var{anything} (which should be compatible with \ctype{void *}) as
839 its argument and should return a ``new'' Python object, or \NULL{}
840 if an error occurred.
841
842 \item[\samp{(\var{items})} (tuple) {[\var{matching-items}]}]
843 Convert a sequence of C values to a Python tuple with the same
844 number of items.
845
846 \item[\samp{[\var{items}]} (list) {[\var{matching-items}]}]
847 Convert a sequence of C values to a Python list with the same
848 number of items.
849
850 \item[\samp{\{\var{items}\}} (dictionary) {[\var{matching-items}]}]
851 Convert a sequence of C values to a Python dictionary. Each pair
852 of consecutive C values adds one item to the dictionary, serving
853 as key and value, respectively.
854
855 \end{description}
856
857 If there is an error in the format string, the
858 \exception{SystemError} exception is set and \NULL{} returned.
Fred Drake3adf79e2001-10-12 19:01:43 +0000859\end{cfuncdesc}