blob: 48c44b17775818b761e25a9e586b59f347b2a5c6 [file] [log] [blame]
Fred Drake3adf79e2001-10-12 19:01:43 +00001\chapter{Concrete Objects Layer \label{concrete}}
2
3
4The functions in this chapter are specific to certain Python object
5types. Passing them an object of the wrong type is not a good idea;
6if you receive an object from a Python program and you are not sure
7that it has the right type, you must perform a type check first;
8for example, to check that an object is a dictionary, use
9\cfunction{PyDict_Check()}. The chapter is structured like the
10``family tree'' of Python object types.
11
12\warning{While the functions described in this chapter carefully check
13the type of the objects which are passed in, many of them do not check
14for \NULL{} being passed instead of a valid object. Allowing \NULL{}
15to be passed in can cause memory access violations and immediate
16termination of the interpreter.}
17
18
19\section{Fundamental Objects \label{fundamental}}
20
Tim Petersf582b822001-12-11 18:51:08 +000021This section describes Python type objects and the singleton object
Fred Drake3adf79e2001-10-12 19:01:43 +000022\code{None}.
23
24
25\subsection{Type Objects \label{typeObjects}}
26
27\obindex{type}
28\begin{ctypedesc}{PyTypeObject}
29 The C structure of the objects used to describe built-in types.
30\end{ctypedesc}
31
32\begin{cvardesc}{PyObject*}{PyType_Type}
33 This is the type object for type objects; it is the same object as
34 \code{types.TypeType} in the Python layer.
35 \withsubitem{(in module types)}{\ttindex{TypeType}}
36\end{cvardesc}
37
38\begin{cfuncdesc}{int}{PyType_Check}{PyObject *o}
Fred Drakee3c764b2002-04-10 17:52:52 +000039 Returns true if the object \var{o} is a type object, including
40 instances of types derived from the standard type object. Returns
41 false in all other cases.
42\end{cfuncdesc}
43
44\begin{cfuncdesc}{int}{PyType_CheckExact}{PyObject *o}
45 Returns true if the object \var{o} is a type object, but not a
46 subtype of the standard type object. Returns false in all other
47 cases.
48 \versionadded{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +000049\end{cfuncdesc}
50
51\begin{cfuncdesc}{int}{PyType_HasFeature}{PyObject *o, int feature}
52 Returns true if the type object \var{o} sets the feature
53 \var{feature}. Type features are denoted by single bit flags.
54\end{cfuncdesc}
55
Fred Drakee3c764b2002-04-10 17:52:52 +000056\begin{cfuncdesc}{int}{PyType_IS_GC}{PyObject *o}
57 Return true if the type object includes support for the cycle
58 detector; this tests the type flag \constant{Py_TPFLAGS_HAVE_GC}.
59 \versionadded{2.0}
60\end{cfuncdesc}
61
Fred Drake3adf79e2001-10-12 19:01:43 +000062\begin{cfuncdesc}{int}{PyType_IsSubtype}{PyTypeObject *a, PyTypeObject *b}
63 Returns true if \var{a} is a subtype of \var{b}.
64 \versionadded{2.2}
65\end{cfuncdesc}
66
67\begin{cfuncdesc}{PyObject*}{PyType_GenericAlloc}{PyTypeObject *type,
68 int nitems}
69 \versionadded{2.2}
70\end{cfuncdesc}
71
72\begin{cfuncdesc}{PyObject*}{PyType_GenericNew}{PyTypeObject *type,
73 PyObject *args, PyObject *kwds}
74 \versionadded{2.2}
75\end{cfuncdesc}
76
77\begin{cfuncdesc}{int}{PyType_Ready}{PyTypeObject *type}
Fred Drake28de8d42002-04-12 16:15:10 +000078 Finalize a type object. This should be called on all type objects
79 to finish their initialization. This function is responsible for
80 adding inherited slots from a type's base class. Returns \code{0}
81 on success, or returns \code{-1} and sets an exception on error.
Fred Drake3adf79e2001-10-12 19:01:43 +000082 \versionadded{2.2}
83\end{cfuncdesc}
84
85
86\subsection{The None Object \label{noneObject}}
87
88\obindex{None@\texttt{None}}
89Note that the \ctype{PyTypeObject} for \code{None} is not directly
90exposed in the Python/C API. Since \code{None} is a singleton,
91testing for object identity (using \samp{==} in C) is sufficient.
92There is no \cfunction{PyNone_Check()} function for the same reason.
93
94\begin{cvardesc}{PyObject*}{Py_None}
95 The Python \code{None} object, denoting lack of value. This object
Fred Drake6ccdccd2002-03-12 20:12:54 +000096 has no methods. It needs to be treated just like any other object
97 with respect to reference counts.
Fred Drake3adf79e2001-10-12 19:01:43 +000098\end{cvardesc}
99
100
101\section{Numeric Objects \label{numericObjects}}
102
103\obindex{numeric}
104
105
106\subsection{Plain Integer Objects \label{intObjects}}
107
108\obindex{integer}
109\begin{ctypedesc}{PyIntObject}
110 This subtype of \ctype{PyObject} represents a Python integer
111 object.
112\end{ctypedesc}
113
114\begin{cvardesc}{PyTypeObject}{PyInt_Type}
Tim Petersf582b822001-12-11 18:51:08 +0000115 This instance of \ctype{PyTypeObject} represents the Python plain
Fred Drake3adf79e2001-10-12 19:01:43 +0000116 integer type. This is the same object as \code{types.IntType}.
117 \withsubitem{(in modules types)}{\ttindex{IntType}}
118\end{cvardesc}
119
120\begin{cfuncdesc}{int}{PyInt_Check}{PyObject* o}
121 Returns true if \var{o} is of type \cdata{PyInt_Type} or a subtype
122 of \cdata{PyInt_Type}.
123 \versionchanged[Allowed subtypes to be accepted]{2.2}
124\end{cfuncdesc}
125
126\begin{cfuncdesc}{int}{PyInt_CheckExact}{PyObject* o}
127 Returns true if \var{o} is of type \cdata{PyInt_Type}, but not a
128 subtype of \cdata{PyInt_Type}.
129 \versionadded{2.2}
130\end{cfuncdesc}
131
Skip Montanaro1ff49a72003-02-03 05:13:24 +0000132\begin{cfuncdesc}{PyObject*}{PyInt_FromString}{char *str, char **pend,
133 int base}
134 Return a new \ctype{PyIntObject} or \ctype{PyLongObject} based on the
135 string value in \var{str}, which is interpreted according to the radix in
136 \var{base}. If \var{pend} is non-\NULL, \code{*\var{pend}} will point to
137 the first character in \var{str} which follows the representation of the
138 number. If \var{base} is \code{0}, the radix will be determined based on
139 the leading characters of \var{str}: if \var{str} starts with \code{'0x'}
140 or \code{'0X'}, radix 16 will be used; if \var{str} starts with
141 \code{'0'}, radix 8 will be used; otherwise radix 10 will be used. If
142 \var{base} is not \code{0}, it must be between \code{2} and \code{36},
143 inclusive. Leading spaces are ignored. If there are no digits,
144 \exception{ValueError} will be raised. If the string represents a number
145 too large to be contained within the machine's \ctype{long int} type and
146 overflow warnings are being suppressed, a \ctype{PyLongObject} will be
147 returned. If overflow warnings are not being suppressed, \NULL{} will be
148 returned in this case.
149\end{cfuncdesc}
150
Fred Drake3adf79e2001-10-12 19:01:43 +0000151\begin{cfuncdesc}{PyObject*}{PyInt_FromLong}{long ival}
152 Creates a new integer object with a value of \var{ival}.
153
154 The current implementation keeps an array of integer objects for all
155 integers between \code{-1} and \code{100}, when you create an int in
156 that range you actually just get back a reference to the existing
157 object. So it should be possible to change the value of \code{1}. I
158 suspect the behaviour of Python in this case is undefined. :-)
159\end{cfuncdesc}
160
161\begin{cfuncdesc}{long}{PyInt_AsLong}{PyObject *io}
162 Will first attempt to cast the object to a \ctype{PyIntObject}, if
163 it is not already one, and then return its value.
164\end{cfuncdesc}
165
166\begin{cfuncdesc}{long}{PyInt_AS_LONG}{PyObject *io}
167 Returns the value of the object \var{io}. No error checking is
168 performed.
169\end{cfuncdesc}
170
171\begin{cfuncdesc}{long}{PyInt_GetMax}{}
172 Returns the system's idea of the largest integer it can handle
173 (\constant{LONG_MAX}\ttindex{LONG_MAX}, as defined in the system
174 header files).
175\end{cfuncdesc}
176
177
178\subsection{Long Integer Objects \label{longObjects}}
179
180\obindex{long integer}
181\begin{ctypedesc}{PyLongObject}
182 This subtype of \ctype{PyObject} represents a Python long integer
183 object.
184\end{ctypedesc}
185
186\begin{cvardesc}{PyTypeObject}{PyLong_Type}
187 This instance of \ctype{PyTypeObject} represents the Python long
188 integer type. This is the same object as \code{types.LongType}.
189 \withsubitem{(in modules types)}{\ttindex{LongType}}
190\end{cvardesc}
191
192\begin{cfuncdesc}{int}{PyLong_Check}{PyObject *p}
193 Returns true if its argument is a \ctype{PyLongObject} or a subtype
194 of \ctype{PyLongObject}.
195 \versionchanged[Allowed subtypes to be accepted]{2.2}
196\end{cfuncdesc}
197
198\begin{cfuncdesc}{int}{PyLong_CheckExact}{PyObject *p}
199 Returns true if its argument is a \ctype{PyLongObject}, but not a
200 subtype of \ctype{PyLongObject}.
201 \versionadded{2.2}
202\end{cfuncdesc}
203
204\begin{cfuncdesc}{PyObject*}{PyLong_FromLong}{long v}
205 Returns a new \ctype{PyLongObject} object from \var{v}, or \NULL{}
206 on failure.
207\end{cfuncdesc}
208
209\begin{cfuncdesc}{PyObject*}{PyLong_FromUnsignedLong}{unsigned long v}
210 Returns a new \ctype{PyLongObject} object from a C \ctype{unsigned
211 long}, or \NULL{} on failure.
212\end{cfuncdesc}
213
214\begin{cfuncdesc}{PyObject*}{PyLong_FromLongLong}{long long v}
215 Returns a new \ctype{PyLongObject} object from a C \ctype{long long},
216 or \NULL{} on failure.
217\end{cfuncdesc}
218
219\begin{cfuncdesc}{PyObject*}{PyLong_FromUnsignedLongLong}{unsigned long long v}
220 Returns a new \ctype{PyLongObject} object from a C \ctype{unsigned
221 long long}, or \NULL{} on failure.
222\end{cfuncdesc}
223
224\begin{cfuncdesc}{PyObject*}{PyLong_FromDouble}{double v}
225 Returns a new \ctype{PyLongObject} object from the integer part of
226 \var{v}, or \NULL{} on failure.
227\end{cfuncdesc}
228
229\begin{cfuncdesc}{PyObject*}{PyLong_FromString}{char *str, char **pend,
230 int base}
231 Return a new \ctype{PyLongObject} based on the string value in
232 \var{str}, which is interpreted according to the radix in
233 \var{base}. If \var{pend} is non-\NULL, \code{*\var{pend}} will
234 point to the first character in \var{str} which follows the
235 representation of the number. If \var{base} is \code{0}, the radix
Skip Montanaro1ff49a72003-02-03 05:13:24 +0000236 will be determined based on the leading characters of \var{str}: if
Fred Drake3adf79e2001-10-12 19:01:43 +0000237 \var{str} starts with \code{'0x'} or \code{'0X'}, radix 16 will be
238 used; if \var{str} starts with \code{'0'}, radix 8 will be used;
239 otherwise radix 10 will be used. If \var{base} is not \code{0}, it
240 must be between \code{2} and \code{36}, inclusive. Leading spaces
241 are ignored. If there are no digits, \exception{ValueError} will be
242 raised.
243\end{cfuncdesc}
244
245\begin{cfuncdesc}{PyObject*}{PyLong_FromUnicode}{Py_UNICODE *u,
246 int length, int base}
247 Convert a sequence of Unicode digits to a Python long integer
248 value. The first parameter, \var{u}, points to the first character
249 of the Unicode string, \var{length} gives the number of characters,
250 and \var{base} is the radix for the conversion. The radix must be
251 in the range [2, 36]; if it is out of range, \exception{ValueError}
252 will be raised.
253 \versionadded{1.6}
254\end{cfuncdesc}
255
256\begin{cfuncdesc}{PyObject*}{PyLong_FromVoidPtr}{void *p}
257 Create a Python integer or long integer from the pointer \var{p}.
258 The pointer value can be retrieved from the resulting value using
259 \cfunction{PyLong_AsVoidPtr()}.
260 \versionadded{1.5.2}
261\end{cfuncdesc}
262
263\begin{cfuncdesc}{long}{PyLong_AsLong}{PyObject *pylong}
264 Returns a C \ctype{long} representation of the contents of
265 \var{pylong}. If \var{pylong} is greater than
266 \constant{LONG_MAX}\ttindex{LONG_MAX}, an \exception{OverflowError}
267 is raised.
268 \withsubitem{(built-in exception)}{\ttindex{OverflowError}}
269\end{cfuncdesc}
270
271\begin{cfuncdesc}{unsigned long}{PyLong_AsUnsignedLong}{PyObject *pylong}
272 Returns a C \ctype{unsigned long} representation of the contents of
273 \var{pylong}. If \var{pylong} is greater than
274 \constant{ULONG_MAX}\ttindex{ULONG_MAX}, an
275 \exception{OverflowError} is raised.
Tim Petersf582b822001-12-11 18:51:08 +0000276 \withsubitem{(built-in exception)}{\ttindex{OverflowError}}
Fred Drake3adf79e2001-10-12 19:01:43 +0000277\end{cfuncdesc}
278
279\begin{cfuncdesc}{long long}{PyLong_AsLongLong}{PyObject *pylong}
280 Return a C \ctype{long long} from a Python long integer. If
281 \var{pylong} cannot be represented as a \ctype{long long}, an
282 \exception{OverflowError} will be raised.
283 \versionadded{2.2}
284\end{cfuncdesc}
285
286\begin{cfuncdesc}{unsigned long long}{PyLong_AsUnsignedLongLong}{PyObject
287 *pylong}
288 Return a C \ctype{unsigned long long} from a Python long integer.
289 If \var{pylong} cannot be represented as an \ctype{unsigned long
290 long}, an \exception{OverflowError} will be raised if the value is
291 positive, or a \exception{TypeError} will be raised if the value is
292 negative.
293 \versionadded{2.2}
294\end{cfuncdesc}
295
296\begin{cfuncdesc}{double}{PyLong_AsDouble}{PyObject *pylong}
297 Returns a C \ctype{double} representation of the contents of
298 \var{pylong}. If \var{pylong} cannot be approximately represented
299 as a \ctype{double}, an \exception{OverflowError} exception is
300 raised and \code{-1.0} will be returned.
301\end{cfuncdesc}
302
303\begin{cfuncdesc}{void*}{PyLong_AsVoidPtr}{PyObject *pylong}
304 Convert a Python integer or long integer \var{pylong} to a C
305 \ctype{void} pointer. If \var{pylong} cannot be converted, an
306 \exception{OverflowError} will be raised. This is only assured to
307 produce a usable \ctype{void} pointer for values created with
308 \cfunction{PyLong_FromVoidPtr()}.
309 \versionadded{1.5.2}
310\end{cfuncdesc}
311
312
313\subsection{Floating Point Objects \label{floatObjects}}
314
315\obindex{floating point}
316\begin{ctypedesc}{PyFloatObject}
317 This subtype of \ctype{PyObject} represents a Python floating point
318 object.
319\end{ctypedesc}
320
321\begin{cvardesc}{PyTypeObject}{PyFloat_Type}
322 This instance of \ctype{PyTypeObject} represents the Python floating
323 point type. This is the same object as \code{types.FloatType}.
324 \withsubitem{(in modules types)}{\ttindex{FloatType}}
325\end{cvardesc}
326
327\begin{cfuncdesc}{int}{PyFloat_Check}{PyObject *p}
328 Returns true if its argument is a \ctype{PyFloatObject} or a subtype
329 of \ctype{PyFloatObject}.
330 \versionchanged[Allowed subtypes to be accepted]{2.2}
331\end{cfuncdesc}
332
333\begin{cfuncdesc}{int}{PyFloat_CheckExact}{PyObject *p}
334 Returns true if its argument is a \ctype{PyFloatObject}, but not a
335 subtype of \ctype{PyFloatObject}.
336 \versionadded{2.2}
337\end{cfuncdesc}
338
Skip Montanaro1ff49a72003-02-03 05:13:24 +0000339\begin{cfuncdesc}{PyObject*}{PyFloat_FromString}{PyObject *str, char **pend}
Skip Montanaroae31e9b2003-02-03 03:56:36 +0000340 Creates a \ctype{PyFloatObject} object based on the string value in
Skip Montanaro1ff49a72003-02-03 05:13:24 +0000341 \var{str}, or \NULL{} on failure. The \var{pend} argument is ignored. It
342 remains only for backward compatibility.
Skip Montanaroae31e9b2003-02-03 03:56:36 +0000343\end{cfuncdesc}
344
Fred Drake3adf79e2001-10-12 19:01:43 +0000345\begin{cfuncdesc}{PyObject*}{PyFloat_FromDouble}{double v}
346 Creates a \ctype{PyFloatObject} object from \var{v}, or \NULL{} on
347 failure.
348\end{cfuncdesc}
349
350\begin{cfuncdesc}{double}{PyFloat_AsDouble}{PyObject *pyfloat}
351 Returns a C \ctype{double} representation of the contents of
352 \var{pyfloat}.
353\end{cfuncdesc}
354
355\begin{cfuncdesc}{double}{PyFloat_AS_DOUBLE}{PyObject *pyfloat}
356 Returns a C \ctype{double} representation of the contents of
357 \var{pyfloat}, but without error checking.
358\end{cfuncdesc}
359
360
361\subsection{Complex Number Objects \label{complexObjects}}
362
363\obindex{complex number}
364Python's complex number objects are implemented as two distinct types
365when viewed from the C API: one is the Python object exposed to
366Python programs, and the other is a C structure which represents the
367actual complex number value. The API provides functions for working
368with both.
369
370\subsubsection{Complex Numbers as C Structures}
371
372Note that the functions which accept these structures as parameters
373and return them as results do so \emph{by value} rather than
374dereferencing them through pointers. This is consistent throughout
375the API.
376
377\begin{ctypedesc}{Py_complex}
378 The C structure which corresponds to the value portion of a Python
379 complex number object. Most of the functions for dealing with
380 complex number objects use structures of this type as input or
381 output values, as appropriate. It is defined as:
382
383\begin{verbatim}
384typedef struct {
385 double real;
386 double imag;
387} Py_complex;
388\end{verbatim}
389\end{ctypedesc}
390
391\begin{cfuncdesc}{Py_complex}{_Py_c_sum}{Py_complex left, Py_complex right}
392 Return the sum of two complex numbers, using the C
393 \ctype{Py_complex} representation.
394\end{cfuncdesc}
395
396\begin{cfuncdesc}{Py_complex}{_Py_c_diff}{Py_complex left, Py_complex right}
397 Return the difference between two complex numbers, using the C
398 \ctype{Py_complex} representation.
399\end{cfuncdesc}
400
401\begin{cfuncdesc}{Py_complex}{_Py_c_neg}{Py_complex complex}
402 Return the negation of the complex number \var{complex}, using the C
403 \ctype{Py_complex} representation.
404\end{cfuncdesc}
405
406\begin{cfuncdesc}{Py_complex}{_Py_c_prod}{Py_complex left, Py_complex right}
407 Return the product of two complex numbers, using the C
408 \ctype{Py_complex} representation.
409\end{cfuncdesc}
410
411\begin{cfuncdesc}{Py_complex}{_Py_c_quot}{Py_complex dividend,
412 Py_complex divisor}
413 Return the quotient of two complex numbers, using the C
414 \ctype{Py_complex} representation.
415\end{cfuncdesc}
416
417\begin{cfuncdesc}{Py_complex}{_Py_c_pow}{Py_complex num, Py_complex exp}
418 Return the exponentiation of \var{num} by \var{exp}, using the C
419 \ctype{Py_complex} representation.
420\end{cfuncdesc}
421
422
423\subsubsection{Complex Numbers as Python Objects}
424
425\begin{ctypedesc}{PyComplexObject}
426 This subtype of \ctype{PyObject} represents a Python complex number
427 object.
428\end{ctypedesc}
429
430\begin{cvardesc}{PyTypeObject}{PyComplex_Type}
431 This instance of \ctype{PyTypeObject} represents the Python complex
432 number type.
433\end{cvardesc}
434
435\begin{cfuncdesc}{int}{PyComplex_Check}{PyObject *p}
436 Returns true if its argument is a \ctype{PyComplexObject} or a
437 subtype of \ctype{PyComplexObject}.
438 \versionchanged[Allowed subtypes to be accepted]{2.2}
439\end{cfuncdesc}
440
441\begin{cfuncdesc}{int}{PyComplex_CheckExact}{PyObject *p}
442 Returns true if its argument is a \ctype{PyComplexObject}, but not a
443 subtype of \ctype{PyComplexObject}.
444 \versionadded{2.2}
445\end{cfuncdesc}
446
447\begin{cfuncdesc}{PyObject*}{PyComplex_FromCComplex}{Py_complex v}
448 Create a new Python complex number object from a C
449 \ctype{Py_complex} value.
450\end{cfuncdesc}
451
452\begin{cfuncdesc}{PyObject*}{PyComplex_FromDoubles}{double real, double imag}
453 Returns a new \ctype{PyComplexObject} object from \var{real} and
454 \var{imag}.
455\end{cfuncdesc}
456
457\begin{cfuncdesc}{double}{PyComplex_RealAsDouble}{PyObject *op}
458 Returns the real part of \var{op} as a C \ctype{double}.
459\end{cfuncdesc}
460
461\begin{cfuncdesc}{double}{PyComplex_ImagAsDouble}{PyObject *op}
462 Returns the imaginary part of \var{op} as a C \ctype{double}.
463\end{cfuncdesc}
464
465\begin{cfuncdesc}{Py_complex}{PyComplex_AsCComplex}{PyObject *op}
466 Returns the \ctype{Py_complex} value of the complex number
467 \var{op}.
468\end{cfuncdesc}
469
470
471
472\section{Sequence Objects \label{sequenceObjects}}
473
474\obindex{sequence}
Tim Petersf582b822001-12-11 18:51:08 +0000475Generic operations on sequence objects were discussed in the previous
476chapter; this section deals with the specific kinds of sequence
Fred Drake3adf79e2001-10-12 19:01:43 +0000477objects that are intrinsic to the Python language.
478
479
480\subsection{String Objects \label{stringObjects}}
481
482These functions raise \exception{TypeError} when expecting a string
483parameter and are called with a non-string parameter.
484
485\obindex{string}
486\begin{ctypedesc}{PyStringObject}
487 This subtype of \ctype{PyObject} represents a Python string object.
488\end{ctypedesc}
489
490\begin{cvardesc}{PyTypeObject}{PyString_Type}
491 This instance of \ctype{PyTypeObject} represents the Python string
492 type; it is the same object as \code{types.TypeType} in the Python
493 layer.
494 \withsubitem{(in module types)}{\ttindex{StringType}}.
495\end{cvardesc}
496
497\begin{cfuncdesc}{int}{PyString_Check}{PyObject *o}
498 Returns true if the object \var{o} is a string object or an instance
499 of a subtype of the string type.
500 \versionchanged[Allowed subtypes to be accepted]{2.2}
501\end{cfuncdesc}
502
503\begin{cfuncdesc}{int}{PyString_CheckExact}{PyObject *o}
504 Returns true if the object \var{o} is a string object, but not an
505 instance of a subtype of the string type.
506 \versionadded{2.2}
507\end{cfuncdesc}
508
509\begin{cfuncdesc}{PyObject*}{PyString_FromString}{const char *v}
510 Returns a new string object with the value \var{v} on success, and
Fred Drake32a35872001-12-06 20:38:15 +0000511 \NULL{} on failure. The parameter \var{v} must not be \NULL; it
512 will not be checked.
Fred Drake3adf79e2001-10-12 19:01:43 +0000513\end{cfuncdesc}
514
515\begin{cfuncdesc}{PyObject*}{PyString_FromStringAndSize}{const char *v,
516 int len}
517 Returns a new string object with the value \var{v} and length
518 \var{len} on success, and \NULL{} on failure. If \var{v} is
519 \NULL, the contents of the string are uninitialized.
520\end{cfuncdesc}
521
522\begin{cfuncdesc}{PyObject*}{PyString_FromFormat}{const char *format, ...}
523 Takes a C \cfunction{printf()}-style \var{format} string and a
524 variable number of arguments, calculates the size of the resulting
525 Python string and returns a string with the values formatted into
526 it. The variable arguments must be C types and must correspond
527 exactly to the format characters in the \var{format} string. The
528 following format characters are allowed:
529
530 \begin{tableiii}{l|l|l}{member}{Format Characters}{Type}{Comment}
531 \lineiii{\%\%}{\emph{n/a}}{The literal \% character.}
532 \lineiii{\%c}{int}{A single character, represented as an C int.}
533 \lineiii{\%d}{int}{Exactly equivalent to \code{printf("\%d")}.}
534 \lineiii{\%ld}{long}{Exactly equivalent to \code{printf("\%ld")}.}
535 \lineiii{\%i}{int}{Exactly equivalent to \code{printf("\%i")}.}
536 \lineiii{\%x}{int}{Exactly equivalent to \code{printf("\%x")}.}
537 \lineiii{\%s}{char*}{A null-terminated C character array.}
538 \lineiii{\%p}{void*}{The hex representation of a C pointer.
539 Mostly equivalent to \code{printf("\%p")} except that it is
540 guaranteed to start with the literal \code{0x} regardless of
541 what the platform's \code{printf} yields.}
542 \end{tableiii}
543\end{cfuncdesc}
544
545\begin{cfuncdesc}{PyObject*}{PyString_FromFormatV}{const char *format,
546 va_list vargs}
547 Identical to \function{PyString_FromFormat()} except that it takes
548 exactly two arguments.
549\end{cfuncdesc}
550
551\begin{cfuncdesc}{int}{PyString_Size}{PyObject *string}
552 Returns the length of the string in string object \var{string}.
553\end{cfuncdesc}
554
555\begin{cfuncdesc}{int}{PyString_GET_SIZE}{PyObject *string}
556 Macro form of \cfunction{PyString_Size()} but without error
557 checking.
558\end{cfuncdesc}
559
560\begin{cfuncdesc}{char*}{PyString_AsString}{PyObject *string}
Fred Drake4b247262002-10-22 20:20:20 +0000561 Returns a NUL-terminated representation of the contents of
Fred Drake3adf79e2001-10-12 19:01:43 +0000562 \var{string}. The pointer refers to the internal buffer of
563 \var{string}, not a copy. The data must not be modified in any way,
564 unless the string was just created using
565 \code{PyString_FromStringAndSize(NULL, \var{size})}.
Fred Drake4b247262002-10-22 20:20:20 +0000566 It must not be deallocated. If \var{string} is a Unicode object,
567 this function computes the default encoding of \var{string} and
568 operates on that. If \var{string} is not a string object at all,
569 \cfunction{PyString_AsString()} returns \NULL{} and raises
570 \exception{TypeError}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000571\end{cfuncdesc}
572
573\begin{cfuncdesc}{char*}{PyString_AS_STRING}{PyObject *string}
574 Macro form of \cfunction{PyString_AsString()} but without error
Fred Drake4b247262002-10-22 20:20:20 +0000575 checking. Only string objects are supported; no Unicode objects
576 should be passed.
Fred Drake3adf79e2001-10-12 19:01:43 +0000577\end{cfuncdesc}
578
579\begin{cfuncdesc}{int}{PyString_AsStringAndSize}{PyObject *obj,
580 char **buffer,
581 int *length}
Fred Drake4b247262002-10-22 20:20:20 +0000582 Returns a NUL-terminated representation of the contents of the
Fred Drake3adf79e2001-10-12 19:01:43 +0000583 object \var{obj} through the output variables \var{buffer} and
584 \var{length}.
585
586 The function accepts both string and Unicode objects as input. For
587 Unicode objects it returns the default encoded version of the
Fred Drake4b247262002-10-22 20:20:20 +0000588 object. If \var{length} is \NULL, the resulting buffer may not
589 contain NUL characters; if it does, the function returns \code{-1}
590 and a \exception{TypeError} is raised.
Fred Drake3adf79e2001-10-12 19:01:43 +0000591
592 The buffer refers to an internal string buffer of \var{obj}, not a
593 copy. The data must not be modified in any way, unless the string
594 was just created using \code{PyString_FromStringAndSize(NULL,
Fred Drake4b247262002-10-22 20:20:20 +0000595 \var{size})}. It must not be deallocated. If \var{string} is a
596 Unicode object, this function computes the default encoding of
597 \var{string} and operates on that. If \var{string} is not a string
598 object at all, \cfunction{PyString_AsString()} returns \NULL{} and
599 raises \exception{TypeError}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000600\end{cfuncdesc}
601
602\begin{cfuncdesc}{void}{PyString_Concat}{PyObject **string,
603 PyObject *newpart}
604 Creates a new string object in \var{*string} containing the contents
605 of \var{newpart} appended to \var{string}; the caller will own the
606 new reference. The reference to the old value of \var{string} will
607 be stolen. If the new string cannot be created, the old reference
608 to \var{string} will still be discarded and the value of
609 \var{*string} will be set to \NULL; the appropriate exception will
610 be set.
611\end{cfuncdesc}
612
613\begin{cfuncdesc}{void}{PyString_ConcatAndDel}{PyObject **string,
614 PyObject *newpart}
615 Creates a new string object in \var{*string} containing the contents
616 of \var{newpart} appended to \var{string}. This version decrements
617 the reference count of \var{newpart}.
618\end{cfuncdesc}
619
620\begin{cfuncdesc}{int}{_PyString_Resize}{PyObject **string, int newsize}
621 A way to resize a string object even though it is ``immutable''.
622 Only use this to build up a brand new string object; don't use this
Tim Peters5de98422002-04-27 18:44:32 +0000623 if the string may already be known in other parts of the code. It
624 is an error to call this function if the refcount on the input string
625 object is not one.
626 Pass the address of an existing string object as an lvalue (it may
627 be written into), and the new size desired. On success, \var{*string}
Fred Drake432425e2002-04-29 15:17:16 +0000628 holds the resized string object and \code{0} is returned; the address in
Tim Peters5de98422002-04-27 18:44:32 +0000629 \var{*string} may differ from its input value. If the
630 reallocation fails, the original string object at \var{*string} is
631 deallocated, \var{*string} is set to \NULL{}, a memory exception is set,
Fred Drake432425e2002-04-29 15:17:16 +0000632 and \code{-1} is returned.
Fred Drake3adf79e2001-10-12 19:01:43 +0000633\end{cfuncdesc}
634
635\begin{cfuncdesc}{PyObject*}{PyString_Format}{PyObject *format,
636 PyObject *args}
637 Returns a new string object from \var{format} and \var{args}.
638 Analogous to \code{\var{format} \%\ \var{args}}. The \var{args}
639 argument must be a tuple.
640\end{cfuncdesc}
641
642\begin{cfuncdesc}{void}{PyString_InternInPlace}{PyObject **string}
643 Intern the argument \var{*string} in place. The argument must be
644 the address of a pointer variable pointing to a Python string
645 object. If there is an existing interned string that is the same as
646 \var{*string}, it sets \var{*string} to it (decrementing the
647 reference count of the old string object and incrementing the
648 reference count of the interned string object), otherwise it leaves
649 \var{*string} alone and interns it (incrementing its reference
650 count). (Clarification: even though there is a lot of talk about
651 reference counts, think of this function as reference-count-neutral;
652 you own the object after the call if and only if you owned it before
653 the call.)
654\end{cfuncdesc}
655
656\begin{cfuncdesc}{PyObject*}{PyString_InternFromString}{const char *v}
657 A combination of \cfunction{PyString_FromString()} and
658 \cfunction{PyString_InternInPlace()}, returning either a new string
659 object that has been interned, or a new (``owned'') reference to an
660 earlier interned string object with the same value.
661\end{cfuncdesc}
662
663\begin{cfuncdesc}{PyObject*}{PyString_Decode}{const char *s,
664 int size,
665 const char *encoding,
666 const char *errors}
667 Creates an object by decoding \var{size} bytes of the encoded
668 buffer \var{s} using the codec registered for
669 \var{encoding}. \var{encoding} and \var{errors} have the same
670 meaning as the parameters of the same name in the
671 \function{unicode()} built-in function. The codec to be used is
672 looked up using the Python codec registry. Returns \NULL{} if
673 an exception was raised by the codec.
674\end{cfuncdesc}
675
676\begin{cfuncdesc}{PyObject*}{PyString_AsDecodedObject}{PyObject *str,
677 const char *encoding,
678 const char *errors}
679 Decodes a string object by passing it to the codec registered for
680 \var{encoding} and returns the result as Python
681 object. \var{encoding} and \var{errors} have the same meaning as the
682 parameters of the same name in the string \method{encode()} method.
683 The codec to be used is looked up using the Python codec registry.
684 Returns \NULL{} if an exception was raised by the codec.
685\end{cfuncdesc}
686
687\begin{cfuncdesc}{PyObject*}{PyString_Encode}{const char *s,
688 int size,
689 const char *encoding,
690 const char *errors}
691 Encodes the \ctype{char} buffer of the given size by passing it to
692 the codec registered for \var{encoding} and returns a Python object.
693 \var{encoding} and \var{errors} have the same meaning as the
694 parameters of the same name in the string \method{encode()} method.
695 The codec to be used is looked up using the Python codec
696 registry. Returns \NULL{} if an exception was raised by the
697 codec.
698\end{cfuncdesc}
699
700\begin{cfuncdesc}{PyObject*}{PyString_AsEncodedObject}{PyObject *str,
701 const char *encoding,
702 const char *errors}
703 Encodes a string object using the codec registered for
704 \var{encoding} and returns the result as Python object.
705 \var{encoding} and \var{errors} have the same meaning as the
706 parameters of the same name in the string \method{encode()} method.
707 The codec to be used is looked up using the Python codec registry.
708 Returns \NULL{} if an exception was raised by the codec.
709\end{cfuncdesc}
710
711
712\subsection{Unicode Objects \label{unicodeObjects}}
713\sectionauthor{Marc-Andre Lemburg}{mal@lemburg.com}
714
715%--- Unicode Type -------------------------------------------------------
716
717These are the basic Unicode object types used for the Unicode
718implementation in Python:
719
720\begin{ctypedesc}{Py_UNICODE}
721 This type represents a 16-bit unsigned storage type which is used by
722 Python internally as basis for holding Unicode ordinals. On
723 platforms where \ctype{wchar_t} is available and also has 16-bits,
724 \ctype{Py_UNICODE} is a typedef alias for \ctype{wchar_t} to enhance
725 native platform compatibility. On all other platforms,
726 \ctype{Py_UNICODE} is a typedef alias for \ctype{unsigned short}.
727\end{ctypedesc}
728
729\begin{ctypedesc}{PyUnicodeObject}
730 This subtype of \ctype{PyObject} represents a Python Unicode object.
731\end{ctypedesc}
732
733\begin{cvardesc}{PyTypeObject}{PyUnicode_Type}
734 This instance of \ctype{PyTypeObject} represents the Python Unicode
735 type.
736\end{cvardesc}
737
738The following APIs are really C macros and can be used to do fast
739checks and to access internal read-only data of Unicode objects:
740
741\begin{cfuncdesc}{int}{PyUnicode_Check}{PyObject *o}
742 Returns true if the object \var{o} is a Unicode object or an
743 instance of a Unicode subtype.
744 \versionchanged[Allowed subtypes to be accepted]{2.2}
745\end{cfuncdesc}
746
747\begin{cfuncdesc}{int}{PyUnicode_CheckExact}{PyObject *o}
748 Returns true if the object \var{o} is a Unicode object, but not an
749 instance of a subtype.
750 \versionadded{2.2}
751\end{cfuncdesc}
752
753\begin{cfuncdesc}{int}{PyUnicode_GET_SIZE}{PyObject *o}
754 Returns the size of the object. \var{o} has to be a
755 \ctype{PyUnicodeObject} (not checked).
756\end{cfuncdesc}
757
758\begin{cfuncdesc}{int}{PyUnicode_GET_DATA_SIZE}{PyObject *o}
759 Returns the size of the object's internal buffer in bytes. \var{o}
760 has to be a \ctype{PyUnicodeObject} (not checked).
761\end{cfuncdesc}
762
763\begin{cfuncdesc}{Py_UNICODE*}{PyUnicode_AS_UNICODE}{PyObject *o}
764 Returns a pointer to the internal \ctype{Py_UNICODE} buffer of the
765 object. \var{o} has to be a \ctype{PyUnicodeObject} (not checked).
766\end{cfuncdesc}
767
768\begin{cfuncdesc}{const char*}{PyUnicode_AS_DATA}{PyObject *o}
769 Returns a pointer to the internal buffer of the object.
770 \var{o} has to be a \ctype{PyUnicodeObject} (not checked).
771\end{cfuncdesc}
772
773% --- Unicode character properties ---------------------------------------
774
775Unicode provides many different character properties. The most often
776needed ones are available through these macros which are mapped to C
777functions depending on the Python configuration.
778
779\begin{cfuncdesc}{int}{Py_UNICODE_ISSPACE}{Py_UNICODE ch}
780 Returns 1/0 depending on whether \var{ch} is a whitespace
781 character.
782\end{cfuncdesc}
783
784\begin{cfuncdesc}{int}{Py_UNICODE_ISLOWER}{Py_UNICODE ch}
785 Returns 1/0 depending on whether \var{ch} is a lowercase character.
786\end{cfuncdesc}
787
788\begin{cfuncdesc}{int}{Py_UNICODE_ISUPPER}{Py_UNICODE ch}
789 Returns 1/0 depending on whether \var{ch} is an uppercase
790 character.
791\end{cfuncdesc}
792
793\begin{cfuncdesc}{int}{Py_UNICODE_ISTITLE}{Py_UNICODE ch}
794 Returns 1/0 depending on whether \var{ch} is a titlecase character.
795\end{cfuncdesc}
796
797\begin{cfuncdesc}{int}{Py_UNICODE_ISLINEBREAK}{Py_UNICODE ch}
798 Returns 1/0 depending on whether \var{ch} is a linebreak character.
799\end{cfuncdesc}
800
801\begin{cfuncdesc}{int}{Py_UNICODE_ISDECIMAL}{Py_UNICODE ch}
802 Returns 1/0 depending on whether \var{ch} is a decimal character.
803\end{cfuncdesc}
804
805\begin{cfuncdesc}{int}{Py_UNICODE_ISDIGIT}{Py_UNICODE ch}
806 Returns 1/0 depending on whether \var{ch} is a digit character.
807\end{cfuncdesc}
808
809\begin{cfuncdesc}{int}{Py_UNICODE_ISNUMERIC}{Py_UNICODE ch}
810 Returns 1/0 depending on whether \var{ch} is a numeric character.
811\end{cfuncdesc}
812
813\begin{cfuncdesc}{int}{Py_UNICODE_ISALPHA}{Py_UNICODE ch}
814 Returns 1/0 depending on whether \var{ch} is an alphabetic
815 character.
816\end{cfuncdesc}
817
818\begin{cfuncdesc}{int}{Py_UNICODE_ISALNUM}{Py_UNICODE ch}
819 Returns 1/0 depending on whether \var{ch} is an alphanumeric
820 character.
821\end{cfuncdesc}
822
823These APIs can be used for fast direct character conversions:
824
825\begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOLOWER}{Py_UNICODE ch}
826 Returns the character \var{ch} converted to lower case.
827\end{cfuncdesc}
828
829\begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOUPPER}{Py_UNICODE ch}
830 Returns the character \var{ch} converted to upper case.
831\end{cfuncdesc}
832
833\begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOTITLE}{Py_UNICODE ch}
834 Returns the character \var{ch} converted to title case.
835\end{cfuncdesc}
836
837\begin{cfuncdesc}{int}{Py_UNICODE_TODECIMAL}{Py_UNICODE ch}
838 Returns the character \var{ch} converted to a decimal positive
839 integer. Returns \code{-1} if this is not possible. Does not raise
840 exceptions.
841\end{cfuncdesc}
842
843\begin{cfuncdesc}{int}{Py_UNICODE_TODIGIT}{Py_UNICODE ch}
844 Returns the character \var{ch} converted to a single digit integer.
845 Returns \code{-1} if this is not possible. Does not raise
846 exceptions.
847\end{cfuncdesc}
848
849\begin{cfuncdesc}{double}{Py_UNICODE_TONUMERIC}{Py_UNICODE ch}
850 Returns the character \var{ch} converted to a (positive) double.
851 Returns \code{-1.0} if this is not possible. Does not raise
852 exceptions.
853\end{cfuncdesc}
854
855% --- Plain Py_UNICODE ---------------------------------------------------
856
857To create Unicode objects and access their basic sequence properties,
858use these APIs:
859
860\begin{cfuncdesc}{PyObject*}{PyUnicode_FromUnicode}{const Py_UNICODE *u,
Tim Petersf582b822001-12-11 18:51:08 +0000861 int size}
Fred Drake3adf79e2001-10-12 19:01:43 +0000862 Create a Unicode Object from the Py_UNICODE buffer \var{u} of the
863 given size. \var{u} may be \NULL{} which causes the contents to be
864 undefined. It is the user's responsibility to fill in the needed
865 data. The buffer is copied into the new object. If the buffer is
866 not \NULL, the return value might be a shared object. Therefore,
867 modification of the resulting Unicode object is only allowed when
868 \var{u} is \NULL.
869\end{cfuncdesc}
870
871\begin{cfuncdesc}{Py_UNICODE*}{PyUnicode_AsUnicode}{PyObject *unicode}
872 Return a read-only pointer to the Unicode object's internal
873 \ctype{Py_UNICODE} buffer, \NULL{} if \var{unicode} is not a Unicode
874 object.
875\end{cfuncdesc}
876
877\begin{cfuncdesc}{int}{PyUnicode_GetSize}{PyObject *unicode}
878 Return the length of the Unicode object.
879\end{cfuncdesc}
880
881\begin{cfuncdesc}{PyObject*}{PyUnicode_FromEncodedObject}{PyObject *obj,
882 const char *encoding,
883 const char *errors}
884 Coerce an encoded object \var{obj} to an Unicode object and return a
885 reference with incremented refcount.
886
887 Coercion is done in the following way:
888
889\begin{enumerate}
890\item Unicode objects are passed back as-is with incremented
891 refcount. \note{These cannot be decoded; passing a non-\NULL{}
892 value for encoding will result in a \exception{TypeError}.}
893
894\item String and other char buffer compatible objects are decoded
895 according to the given encoding and using the error handling
896 defined by errors. Both can be \NULL{} to have the interface
897 use the default values (see the next section for details).
898
899\item All other objects cause an exception.
900\end{enumerate}
901
902 The API returns \NULL{} if there was an error. The caller is
903 responsible for decref'ing the returned objects.
904\end{cfuncdesc}
905
906\begin{cfuncdesc}{PyObject*}{PyUnicode_FromObject}{PyObject *obj}
907 Shortcut for \code{PyUnicode_FromEncodedObject(obj, NULL, "strict")}
908 which is used throughout the interpreter whenever coercion to
909 Unicode is needed.
910\end{cfuncdesc}
911
912% --- wchar_t support for platforms which support it ---------------------
913
914If the platform supports \ctype{wchar_t} and provides a header file
915wchar.h, Python can interface directly to this type using the
916following functions. Support is optimized if Python's own
917\ctype{Py_UNICODE} type is identical to the system's \ctype{wchar_t}.
918
919\begin{cfuncdesc}{PyObject*}{PyUnicode_FromWideChar}{const wchar_t *w,
920 int size}
Thomas Heller541703b2002-04-29 17:28:43 +0000921 Create a Unicode object from the \ctype{wchar_t} buffer \var{w} of
Fred Drake3adf79e2001-10-12 19:01:43 +0000922 the given size. Returns \NULL{} on failure.
923\end{cfuncdesc}
924
925\begin{cfuncdesc}{int}{PyUnicode_AsWideChar}{PyUnicodeObject *unicode,
926 wchar_t *w,
927 int size}
Thomas Heller541703b2002-04-29 17:28:43 +0000928 Copies the Unicode object contents into the \ctype{wchar_t} buffer
929 \var{w}. At most \var{size} \ctype{wchar_t} characters are copied.
930 Returns the number of \ctype{wchar_t} characters copied or -1 in
Fred Drake3adf79e2001-10-12 19:01:43 +0000931 case of an error.
932\end{cfuncdesc}
933
934
935\subsubsection{Built-in Codecs \label{builtinCodecs}}
936
937Python provides a set of builtin codecs which are written in C
938for speed. All of these codecs are directly usable via the
939following functions.
940
941Many of the following APIs take two arguments encoding and
942errors. These parameters encoding and errors have the same semantics
943as the ones of the builtin unicode() Unicode object constructor.
944
945Setting encoding to \NULL{} causes the default encoding to be used
946which is \ASCII. The file system calls should use
947\cdata{Py_FileSystemDefaultEncoding} as the encoding for file
948names. This variable should be treated as read-only: On some systems,
949it will be a pointer to a static string, on others, it will change at
950run-time, e.g. when the application invokes setlocale.
951
952Error handling is set by errors which may also be set to \NULL{}
953meaning to use the default handling defined for the codec. Default
954error handling for all builtin codecs is ``strict''
955(\exception{ValueError} is raised).
956
957The codecs all use a similar interface. Only deviation from the
958following generic ones are documented for simplicity.
959
960% --- Generic Codecs -----------------------------------------------------
961
962These are the generic codec APIs:
963
964\begin{cfuncdesc}{PyObject*}{PyUnicode_Decode}{const char *s,
965 int size,
966 const char *encoding,
967 const char *errors}
968 Create a Unicode object by decoding \var{size} bytes of the encoded
969 string \var{s}. \var{encoding} and \var{errors} have the same
970 meaning as the parameters of the same name in the
971 \function{unicode()} builtin function. The codec to be used is
972 looked up using the Python codec registry. Returns \NULL{} if an
973 exception was raised by the codec.
974\end{cfuncdesc}
975
976\begin{cfuncdesc}{PyObject*}{PyUnicode_Encode}{const Py_UNICODE *s,
977 int size,
978 const char *encoding,
979 const char *errors}
980 Encodes the \ctype{Py_UNICODE} buffer of the given size and returns
981 a Python string object. \var{encoding} and \var{errors} have the
982 same meaning as the parameters of the same name in the Unicode
983 \method{encode()} method. The codec to be used is looked up using
984 the Python codec registry. Returns \NULL{} if an exception was
985 raised by the codec.
986\end{cfuncdesc}
987
988\begin{cfuncdesc}{PyObject*}{PyUnicode_AsEncodedString}{PyObject *unicode,
989 const char *encoding,
990 const char *errors}
991 Encodes a Unicode object and returns the result as Python string
992 object. \var{encoding} and \var{errors} have the same meaning as the
993 parameters of the same name in the Unicode \method{encode()} method.
994 The codec to be used is looked up using the Python codec registry.
995 Returns \NULL{} if an exception was raised by the codec.
996\end{cfuncdesc}
997
998% --- UTF-8 Codecs -------------------------------------------------------
999
1000These are the UTF-8 codec APIs:
1001
1002\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF8}{const char *s,
1003 int size,
1004 const char *errors}
1005 Creates a Unicode object by decoding \var{size} bytes of the UTF-8
1006 encoded string \var{s}. Returns \NULL{} if an exception was raised
1007 by the codec.
1008\end{cfuncdesc}
1009
1010\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUTF8}{const Py_UNICODE *s,
1011 int size,
1012 const char *errors}
1013 Encodes the \ctype{Py_UNICODE} buffer of the given size using UTF-8
1014 and returns a Python string object. Returns \NULL{} if an exception
1015 was raised by the codec.
1016\end{cfuncdesc}
1017
1018\begin{cfuncdesc}{PyObject*}{PyUnicode_AsUTF8String}{PyObject *unicode}
1019 Encodes a Unicode objects using UTF-8 and returns the result as
1020 Python string object. Error handling is ``strict''. Returns
1021 \NULL{} if an exception was raised by the codec.
1022\end{cfuncdesc}
1023
1024% --- UTF-16 Codecs ------------------------------------------------------ */
1025
1026These are the UTF-16 codec APIs:
1027
1028\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF16}{const char *s,
1029 int size,
1030 const char *errors,
1031 int *byteorder}
1032 Decodes \var{length} bytes from a UTF-16 encoded buffer string and
1033 returns the corresponding Unicode object. \var{errors} (if
1034 non-\NULL) defines the error handling. It defaults to ``strict''.
1035
1036 If \var{byteorder} is non-\NULL, the decoder starts decoding using
1037 the given byte order:
1038
1039\begin{verbatim}
1040 *byteorder == -1: little endian
1041 *byteorder == 0: native order
1042 *byteorder == 1: big endian
1043\end{verbatim}
1044
1045 and then switches according to all byte order marks (BOM) it finds
1046 in the input data. BOMs are not copied into the resulting Unicode
1047 string. After completion, \var{*byteorder} is set to the current
1048 byte order at the end of input data.
1049
1050 If \var{byteorder} is \NULL, the codec starts in native order mode.
1051
1052 Returns \NULL{} if an exception was raised by the codec.
1053\end{cfuncdesc}
1054
1055\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUTF16}{const Py_UNICODE *s,
1056 int size,
1057 const char *errors,
1058 int byteorder}
1059 Returns a Python string object holding the UTF-16 encoded value of
1060 the Unicode data in \var{s}. If \var{byteorder} is not \code{0},
1061 output is written according to the following byte order:
1062
1063\begin{verbatim}
1064 byteorder == -1: little endian
1065 byteorder == 0: native byte order (writes a BOM mark)
1066 byteorder == 1: big endian
1067\end{verbatim}
1068
1069 If byteorder is \code{0}, the output string will always start with
1070 the Unicode BOM mark (U+FEFF). In the other two modes, no BOM mark
1071 is prepended.
1072
1073 Note that \ctype{Py_UNICODE} data is being interpreted as UTF-16
1074 reduced to UCS-2. This trick makes it possible to add full UTF-16
1075 capabilities at a later point without comprimising the APIs.
1076
1077 Returns \NULL{} if an exception was raised by the codec.
1078\end{cfuncdesc}
1079
1080\begin{cfuncdesc}{PyObject*}{PyUnicode_AsUTF16String}{PyObject *unicode}
1081 Returns a Python string using the UTF-16 encoding in native byte
1082 order. The string always starts with a BOM mark. Error handling is
1083 ``strict''. Returns \NULL{} if an exception was raised by the
1084 codec.
1085\end{cfuncdesc}
1086
1087% --- Unicode-Escape Codecs ----------------------------------------------
1088
1089These are the ``Unicode Esacpe'' codec APIs:
1090
1091\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUnicodeEscape}{const char *s,
1092 int size,
1093 const char *errors}
1094 Creates a Unicode object by decoding \var{size} bytes of the
1095 Unicode-Escape encoded string \var{s}. Returns \NULL{} if an
1096 exception was raised by the codec.
1097\end{cfuncdesc}
1098
1099\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUnicodeEscape}{const Py_UNICODE *s,
1100 int size,
1101 const char *errors}
1102 Encodes the \ctype{Py_UNICODE} buffer of the given size using
1103 Unicode-Escape and returns a Python string object. Returns \NULL{}
1104 if an exception was raised by the codec.
1105\end{cfuncdesc}
1106
1107\begin{cfuncdesc}{PyObject*}{PyUnicode_AsUnicodeEscapeString}{PyObject *unicode}
1108 Encodes a Unicode objects using Unicode-Escape and returns the
1109 result as Python string object. Error handling is ``strict''.
1110 Returns \NULL{} if an exception was raised by the codec.
1111\end{cfuncdesc}
1112
1113% --- Raw-Unicode-Escape Codecs ------------------------------------------
1114
1115These are the ``Raw Unicode Esacpe'' codec APIs:
1116
1117\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeRawUnicodeEscape}{const char *s,
1118 int size,
1119 const char *errors}
1120 Creates a Unicode object by decoding \var{size} bytes of the
1121 Raw-Unicode-Esacpe encoded string \var{s}. Returns \NULL{} if an
1122 exception was raised by the codec.
1123\end{cfuncdesc}
1124
1125\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeRawUnicodeEscape}{const Py_UNICODE *s,
1126 int size,
1127 const char *errors}
1128 Encodes the \ctype{Py_UNICODE} buffer of the given size using
1129 Raw-Unicode-Escape and returns a Python string object. Returns
1130 \NULL{} if an exception was raised by the codec.
1131\end{cfuncdesc}
1132
1133\begin{cfuncdesc}{PyObject*}{PyUnicode_AsRawUnicodeEscapeString}{PyObject *unicode}
1134 Encodes a Unicode objects using Raw-Unicode-Escape and returns the
1135 result as Python string object. Error handling is ``strict''.
1136 Returns \NULL{} if an exception was raised by the codec.
1137\end{cfuncdesc}
1138
Tim Petersf582b822001-12-11 18:51:08 +00001139% --- Latin-1 Codecs -----------------------------------------------------
Fred Drake3adf79e2001-10-12 19:01:43 +00001140
1141These are the Latin-1 codec APIs:
1142Latin-1 corresponds to the first 256 Unicode ordinals and only these
1143are accepted by the codecs during encoding.
1144
1145\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeLatin1}{const char *s,
1146 int size,
1147 const char *errors}
1148 Creates a Unicode object by decoding \var{size} bytes of the Latin-1
1149 encoded string \var{s}. Returns \NULL{} if an exception was raised
1150 by the codec.
1151\end{cfuncdesc}
1152
1153\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeLatin1}{const Py_UNICODE *s,
1154 int size,
1155 const char *errors}
1156 Encodes the \ctype{Py_UNICODE} buffer of the given size using
1157 Latin-1 and returns a Python string object. Returns \NULL{} if an
1158 exception was raised by the codec.
1159\end{cfuncdesc}
1160
1161\begin{cfuncdesc}{PyObject*}{PyUnicode_AsLatin1String}{PyObject *unicode}
1162 Encodes a Unicode objects using Latin-1 and returns the result as
1163 Python string object. Error handling is ``strict''. Returns
1164 \NULL{} if an exception was raised by the codec.
1165\end{cfuncdesc}
1166
Tim Petersf582b822001-12-11 18:51:08 +00001167% --- ASCII Codecs -------------------------------------------------------
Fred Drake3adf79e2001-10-12 19:01:43 +00001168
1169These are the \ASCII{} codec APIs. Only 7-bit \ASCII{} data is
1170accepted. All other codes generate errors.
1171
1172\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeASCII}{const char *s,
1173 int size,
1174 const char *errors}
1175 Creates a Unicode object by decoding \var{size} bytes of the
1176 \ASCII{} encoded string \var{s}. Returns \NULL{} if an exception
1177 was raised by the codec.
1178\end{cfuncdesc}
1179
1180\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeASCII}{const Py_UNICODE *s,
1181 int size,
1182 const char *errors}
1183 Encodes the \ctype{Py_UNICODE} buffer of the given size using
1184 \ASCII{} and returns a Python string object. Returns \NULL{} if an
1185 exception was raised by the codec.
1186\end{cfuncdesc}
1187
1188\begin{cfuncdesc}{PyObject*}{PyUnicode_AsASCIIString}{PyObject *unicode}
1189 Encodes a Unicode objects using \ASCII{} and returns the result as
1190 Python string object. Error handling is ``strict''. Returns
1191 \NULL{} if an exception was raised by the codec.
1192\end{cfuncdesc}
1193
Tim Petersf582b822001-12-11 18:51:08 +00001194% --- Character Map Codecs -----------------------------------------------
Fred Drake3adf79e2001-10-12 19:01:43 +00001195
1196These are the mapping codec APIs:
1197
1198This codec is special in that it can be used to implement many
1199different codecs (and this is in fact what was done to obtain most of
1200the standard codecs included in the \module{encodings} package). The
1201codec uses mapping to encode and decode characters.
1202
1203Decoding mappings must map single string characters to single Unicode
1204characters, integers (which are then interpreted as Unicode ordinals)
Tim Petersf582b822001-12-11 18:51:08 +00001205or None (meaning "undefined mapping" and causing an error).
Fred Drake3adf79e2001-10-12 19:01:43 +00001206
1207Encoding mappings must map single Unicode characters to single string
1208characters, integers (which are then interpreted as Latin-1 ordinals)
1209or None (meaning "undefined mapping" and causing an error).
1210
1211The mapping objects provided must only support the __getitem__ mapping
1212interface.
1213
1214If a character lookup fails with a LookupError, the character is
1215copied as-is meaning that its ordinal value will be interpreted as
1216Unicode or Latin-1 ordinal resp. Because of this, mappings only need
1217to contain those mappings which map characters to different code
1218points.
1219
1220\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeCharmap}{const char *s,
1221 int size,
1222 PyObject *mapping,
1223 const char *errors}
1224 Creates a Unicode object by decoding \var{size} bytes of the encoded
1225 string \var{s} using the given \var{mapping} object. Returns
1226 \NULL{} if an exception was raised by the codec.
1227\end{cfuncdesc}
1228
1229\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeCharmap}{const Py_UNICODE *s,
1230 int size,
1231 PyObject *mapping,
1232 const char *errors}
1233 Encodes the \ctype{Py_UNICODE} buffer of the given size using the
1234 given \var{mapping} object and returns a Python string object.
1235 Returns \NULL{} if an exception was raised by the codec.
1236\end{cfuncdesc}
1237
1238\begin{cfuncdesc}{PyObject*}{PyUnicode_AsCharmapString}{PyObject *unicode,
1239 PyObject *mapping}
1240 Encodes a Unicode objects using the given \var{mapping} object and
1241 returns the result as Python string object. Error handling is
1242 ``strict''. Returns \NULL{} if an exception was raised by the
1243 codec.
1244\end{cfuncdesc}
1245
1246The following codec API is special in that maps Unicode to Unicode.
1247
1248\begin{cfuncdesc}{PyObject*}{PyUnicode_TranslateCharmap}{const Py_UNICODE *s,
1249 int size,
1250 PyObject *table,
1251 const char *errors}
1252 Translates a \ctype{Py_UNICODE} buffer of the given length by
1253 applying a character mapping \var{table} to it and returns the
1254 resulting Unicode object. Returns \NULL{} when an exception was
1255 raised by the codec.
1256
1257 The \var{mapping} table must map Unicode ordinal integers to Unicode
1258 ordinal integers or None (causing deletion of the character).
1259
1260 Mapping tables need only provide the method{__getitem__()}
1261 interface; dictionaries and sequences work well. Unmapped character
1262 ordinals (ones which cause a \exception{LookupError}) are left
1263 untouched and are copied as-is.
1264\end{cfuncdesc}
1265
1266% --- MBCS codecs for Windows --------------------------------------------
1267
1268These are the MBCS codec APIs. They are currently only available on
1269Windows and use the Win32 MBCS converters to implement the
1270conversions. Note that MBCS (or DBCS) is a class of encodings, not
1271just one. The target encoding is defined by the user settings on the
1272machine running the codec.
1273
1274\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeMBCS}{const char *s,
1275 int size,
1276 const char *errors}
1277 Creates a Unicode object by decoding \var{size} bytes of the MBCS
1278 encoded string \var{s}. Returns \NULL{} if an exception was
1279 raised by the codec.
1280\end{cfuncdesc}
1281
1282\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeMBCS}{const Py_UNICODE *s,
1283 int size,
1284 const char *errors}
1285 Encodes the \ctype{Py_UNICODE} buffer of the given size using MBCS
1286 and returns a Python string object. Returns \NULL{} if an exception
1287 was raised by the codec.
1288\end{cfuncdesc}
1289
1290\begin{cfuncdesc}{PyObject*}{PyUnicode_AsMBCSString}{PyObject *unicode}
1291 Encodes a Unicode objects using MBCS and returns the result as
1292 Python string object. Error handling is ``strict''. Returns
1293 \NULL{} if an exception was raised by the codec.
1294\end{cfuncdesc}
1295
1296% --- Methods & Slots ----------------------------------------------------
1297
1298\subsubsection{Methods and Slot Functions \label{unicodeMethodsAndSlots}}
1299
1300The following APIs are capable of handling Unicode objects and strings
1301on input (we refer to them as strings in the descriptions) and return
1302Unicode objects or integers as apporpriate.
1303
1304They all return \NULL{} or \code{-1} if an exception occurs.
1305
1306\begin{cfuncdesc}{PyObject*}{PyUnicode_Concat}{PyObject *left,
1307 PyObject *right}
1308 Concat two strings giving a new Unicode string.
1309\end{cfuncdesc}
1310
1311\begin{cfuncdesc}{PyObject*}{PyUnicode_Split}{PyObject *s,
1312 PyObject *sep,
1313 int maxsplit}
1314 Split a string giving a list of Unicode strings. If sep is \NULL,
1315 splitting will be done at all whitespace substrings. Otherwise,
1316 splits occur at the given separator. At most \var{maxsplit} splits
1317 will be done. If negative, no limit is set. Separators are not
1318 included in the resulting list.
1319\end{cfuncdesc}
1320
1321\begin{cfuncdesc}{PyObject*}{PyUnicode_Splitlines}{PyObject *s,
Martin v. Löwis24b88812003-03-30 16:40:42 +00001322 int keepend}
Fred Drake3adf79e2001-10-12 19:01:43 +00001323 Split a Unicode string at line breaks, returning a list of Unicode
Martin v. Löwis24b88812003-03-30 16:40:42 +00001324 strings. CRLF is considered to be one line break. If \var{keepend}
1325 is 0, the Line break characters are not included in the resulting
1326 strings.
Fred Drake3adf79e2001-10-12 19:01:43 +00001327\end{cfuncdesc}
1328
1329\begin{cfuncdesc}{PyObject*}{PyUnicode_Translate}{PyObject *str,
1330 PyObject *table,
1331 const char *errors}
1332 Translate a string by applying a character mapping table to it and
1333 return the resulting Unicode object.
1334
1335 The mapping table must map Unicode ordinal integers to Unicode
1336 ordinal integers or None (causing deletion of the character).
1337
1338 Mapping tables need only provide the \method{__getitem__()}
1339 interface; dictionaries and sequences work well. Unmapped character
1340 ordinals (ones which cause a \exception{LookupError}) are left
1341 untouched and are copied as-is.
1342
1343 \var{errors} has the usual meaning for codecs. It may be \NULL{}
1344 which indicates to use the default error handling.
1345\end{cfuncdesc}
1346
1347\begin{cfuncdesc}{PyObject*}{PyUnicode_Join}{PyObject *separator,
1348 PyObject *seq}
1349 Join a sequence of strings using the given separator and return the
1350 resulting Unicode string.
1351\end{cfuncdesc}
1352
1353\begin{cfuncdesc}{PyObject*}{PyUnicode_Tailmatch}{PyObject *str,
1354 PyObject *substr,
1355 int start,
1356 int end,
1357 int direction}
1358 Return 1 if \var{substr} matches \var{str}[\var{start}:\var{end}] at
1359 the given tail end (\var{direction} == -1 means to do a prefix
1360 match, \var{direction} == 1 a suffix match), 0 otherwise.
1361\end{cfuncdesc}
1362
Fred Drake1d1e1db2002-06-20 22:07:04 +00001363\begin{cfuncdesc}{int}{PyUnicode_Find}{PyObject *str,
1364 PyObject *substr,
1365 int start,
1366 int end,
1367 int direction}
Fred Drake3adf79e2001-10-12 19:01:43 +00001368 Return the first position of \var{substr} in
1369 \var{str}[\var{start}:\var{end}] using the given \var{direction}
1370 (\var{direction} == 1 means to do a forward search,
Fred Drake1d1e1db2002-06-20 22:07:04 +00001371 \var{direction} == -1 a backward search). The return value is the
1372 index of the first match; a value of \code{-1} indicates that no
1373 match was found, and \code{-2} indicates that an error occurred and
1374 an exception has been set.
Fred Drake3adf79e2001-10-12 19:01:43 +00001375\end{cfuncdesc}
1376
Fred Drake1d1e1db2002-06-20 22:07:04 +00001377\begin{cfuncdesc}{int}{PyUnicode_Count}{PyObject *str,
1378 PyObject *substr,
1379 int start,
1380 int end}
1381 Return the number of non-overlapping occurrences of \var{substr} in
1382 \code{\var{str}[\var{start}:\var{end}]}. Returns \code{-1} if an
1383 error occurred.
Fred Drake3adf79e2001-10-12 19:01:43 +00001384\end{cfuncdesc}
1385
1386\begin{cfuncdesc}{PyObject*}{PyUnicode_Replace}{PyObject *str,
1387 PyObject *substr,
1388 PyObject *replstr,
1389 int maxcount}
1390 Replace at most \var{maxcount} occurrences of \var{substr} in
1391 \var{str} with \var{replstr} and return the resulting Unicode object.
1392 \var{maxcount} == -1 means replace all occurrences.
1393\end{cfuncdesc}
1394
1395\begin{cfuncdesc}{int}{PyUnicode_Compare}{PyObject *left, PyObject *right}
1396 Compare two strings and return -1, 0, 1 for less than, equal, and
1397 greater than, respectively.
1398\end{cfuncdesc}
1399
1400\begin{cfuncdesc}{PyObject*}{PyUnicode_Format}{PyObject *format,
1401 PyObject *args}
1402 Returns a new string object from \var{format} and \var{args}; this
1403 is analogous to \code{\var{format} \%\ \var{args}}. The
1404 \var{args} argument must be a tuple.
1405\end{cfuncdesc}
1406
1407\begin{cfuncdesc}{int}{PyUnicode_Contains}{PyObject *container,
1408 PyObject *element}
1409 Checks whether \var{element} is contained in \var{container} and
1410 returns true or false accordingly.
1411
1412 \var{element} has to coerce to a one element Unicode
1413 string. \code{-1} is returned if there was an error.
1414\end{cfuncdesc}
1415
1416
1417\subsection{Buffer Objects \label{bufferObjects}}
1418\sectionauthor{Greg Stein}{gstein@lyra.org}
1419
1420\obindex{buffer}
1421Python objects implemented in C can export a group of functions called
1422the ``buffer\index{buffer interface} interface.'' These functions can
1423be used by an object to expose its data in a raw, byte-oriented
1424format. Clients of the object can use the buffer interface to access
1425the object data directly, without needing to copy it first.
1426
Tim Petersf582b822001-12-11 18:51:08 +00001427Two examples of objects that support
1428the buffer interface are strings and arrays. The string object exposes
Fred Drake3adf79e2001-10-12 19:01:43 +00001429the character contents in the buffer interface's byte-oriented
1430form. An array can also expose its contents, but it should be noted
1431that array elements may be multi-byte values.
1432
1433An example user of the buffer interface is the file object's
1434\method{write()} method. Any object that can export a series of bytes
1435through the buffer interface can be written to a file. There are a
Tim Petersf582b822001-12-11 18:51:08 +00001436number of format codes to \cfunction{PyArg_ParseTuple()} that operate
Fred Drake3adf79e2001-10-12 19:01:43 +00001437against an object's buffer interface, returning data from the target
1438object.
1439
1440More information on the buffer interface is provided in the section
Fred Drake54e62942001-12-11 19:40:16 +00001441``Buffer Object Structures'' (section~\ref{buffer-structs}), under
Fred Drake3adf79e2001-10-12 19:01:43 +00001442the description for \ctype{PyBufferProcs}\ttindex{PyBufferProcs}.
1443
1444A ``buffer object'' is defined in the \file{bufferobject.h} header
1445(included by \file{Python.h}). These objects look very similar to
1446string objects at the Python programming level: they support slicing,
1447indexing, concatenation, and some other standard string
1448operations. However, their data can come from one of two sources: from
1449a block of memory, or from another object which exports the buffer
1450interface.
1451
1452Buffer objects are useful as a way to expose the data from another
1453object's buffer interface to the Python programmer. They can also be
1454used as a zero-copy slicing mechanism. Using their ability to
1455reference a block of memory, it is possible to expose any data to the
1456Python programmer quite easily. The memory could be a large, constant
1457array in a C extension, it could be a raw block of memory for
1458manipulation before passing to an operating system library, or it
1459could be used to pass around structured data in its native, in-memory
1460format.
1461
1462\begin{ctypedesc}{PyBufferObject}
1463 This subtype of \ctype{PyObject} represents a buffer object.
1464\end{ctypedesc}
1465
1466\begin{cvardesc}{PyTypeObject}{PyBuffer_Type}
1467 The instance of \ctype{PyTypeObject} which represents the Python
1468 buffer type; it is the same object as \code{types.BufferType} in the
1469 Python layer.\withsubitem{(in module types)}{\ttindex{BufferType}}.
1470\end{cvardesc}
1471
1472\begin{cvardesc}{int}{Py_END_OF_BUFFER}
1473 This constant may be passed as the \var{size} parameter to
1474 \cfunction{PyBuffer_FromObject()} or
1475 \cfunction{PyBuffer_FromReadWriteObject()}. It indicates that the
1476 new \ctype{PyBufferObject} should refer to \var{base} object from
1477 the specified \var{offset} to the end of its exported buffer. Using
1478 this enables the caller to avoid querying the \var{base} object for
1479 its length.
1480\end{cvardesc}
1481
1482\begin{cfuncdesc}{int}{PyBuffer_Check}{PyObject *p}
1483 Return true if the argument has type \cdata{PyBuffer_Type}.
1484\end{cfuncdesc}
1485
1486\begin{cfuncdesc}{PyObject*}{PyBuffer_FromObject}{PyObject *base,
1487 int offset, int size}
1488 Return a new read-only buffer object. This raises
1489 \exception{TypeError} if \var{base} doesn't support the read-only
1490 buffer protocol or doesn't provide exactly one buffer segment, or it
1491 raises \exception{ValueError} if \var{offset} is less than zero. The
1492 buffer will hold a reference to the \var{base} object, and the
1493 buffer's contents will refer to the \var{base} object's buffer
1494 interface, starting as position \var{offset} and extending for
1495 \var{size} bytes. If \var{size} is \constant{Py_END_OF_BUFFER}, then
1496 the new buffer's contents extend to the length of the \var{base}
1497 object's exported buffer data.
1498\end{cfuncdesc}
1499
1500\begin{cfuncdesc}{PyObject*}{PyBuffer_FromReadWriteObject}{PyObject *base,
1501 int offset,
1502 int size}
1503 Return a new writable buffer object. Parameters and exceptions are
1504 similar to those for \cfunction{PyBuffer_FromObject()}. If the
1505 \var{base} object does not export the writeable buffer protocol,
1506 then \exception{TypeError} is raised.
1507\end{cfuncdesc}
1508
1509\begin{cfuncdesc}{PyObject*}{PyBuffer_FromMemory}{void *ptr, int size}
1510 Return a new read-only buffer object that reads from a specified
1511 location in memory, with a specified size. The caller is
1512 responsible for ensuring that the memory buffer, passed in as
1513 \var{ptr}, is not deallocated while the returned buffer object
1514 exists. Raises \exception{ValueError} if \var{size} is less than
1515 zero. Note that \constant{Py_END_OF_BUFFER} may \emph{not} be
1516 passed for the \var{size} parameter; \exception{ValueError} will be
1517 raised in that case.
1518\end{cfuncdesc}
1519
1520\begin{cfuncdesc}{PyObject*}{PyBuffer_FromReadWriteMemory}{void *ptr, int size}
1521 Similar to \cfunction{PyBuffer_FromMemory()}, but the returned
1522 buffer is writable.
1523\end{cfuncdesc}
1524
1525\begin{cfuncdesc}{PyObject*}{PyBuffer_New}{int size}
1526 Returns a new writable buffer object that maintains its own memory
1527 buffer of \var{size} bytes. \exception{ValueError} is returned if
1528 \var{size} is not zero or positive.
1529\end{cfuncdesc}
1530
1531
1532\subsection{Tuple Objects \label{tupleObjects}}
1533
1534\obindex{tuple}
1535\begin{ctypedesc}{PyTupleObject}
1536 This subtype of \ctype{PyObject} represents a Python tuple object.
1537\end{ctypedesc}
1538
1539\begin{cvardesc}{PyTypeObject}{PyTuple_Type}
1540 This instance of \ctype{PyTypeObject} represents the Python tuple
1541 type; it is the same object as \code{types.TupleType} in the Python
1542 layer.\withsubitem{(in module types)}{\ttindex{TupleType}}.
1543\end{cvardesc}
1544
1545\begin{cfuncdesc}{int}{PyTuple_Check}{PyObject *p}
1546 Return true if \var{p} is a tuple object or an instance of a subtype
1547 of the tuple type.
1548 \versionchanged[Allowed subtypes to be accepted]{2.2}
1549\end{cfuncdesc}
1550
1551\begin{cfuncdesc}{int}{PyTuple_CheckExact}{PyObject *p}
1552 Return true if \var{p} is a tuple object, but not an instance of a
1553 subtype of the tuple type.
1554 \versionadded{2.2}
1555\end{cfuncdesc}
1556
1557\begin{cfuncdesc}{PyObject*}{PyTuple_New}{int len}
1558 Return a new tuple object of size \var{len}, or \NULL{} on failure.
1559\end{cfuncdesc}
1560
1561\begin{cfuncdesc}{int}{PyTuple_Size}{PyObject *p}
1562 Takes a pointer to a tuple object, and returns the size of that
1563 tuple.
1564\end{cfuncdesc}
1565
1566\begin{cfuncdesc}{int}{PyTuple_GET_SIZE}{PyObject *p}
1567 Return the size of the tuple \var{p}, which must be non-\NULL{} and
1568 point to a tuple; no error checking is performed.
1569\end{cfuncdesc}
1570
1571\begin{cfuncdesc}{PyObject*}{PyTuple_GetItem}{PyObject *p, int pos}
1572 Returns the object at position \var{pos} in the tuple pointed to by
1573 \var{p}. If \var{pos} is out of bounds, returns \NULL{} and sets an
1574 \exception{IndexError} exception.
1575\end{cfuncdesc}
1576
1577\begin{cfuncdesc}{PyObject*}{PyTuple_GET_ITEM}{PyObject *p, int pos}
1578 Like \cfunction{PyTuple_GetItem()}, but does no checking of its
1579 arguments.
1580\end{cfuncdesc}
1581
1582\begin{cfuncdesc}{PyObject*}{PyTuple_GetSlice}{PyObject *p,
1583 int low, int high}
1584 Takes a slice of the tuple pointed to by \var{p} from \var{low} to
1585 \var{high} and returns it as a new tuple.
1586\end{cfuncdesc}
1587
1588\begin{cfuncdesc}{int}{PyTuple_SetItem}{PyObject *p,
1589 int pos, PyObject *o}
1590 Inserts a reference to object \var{o} at position \var{pos} of the
1591 tuple pointed to by \var{p}. It returns \code{0} on success.
1592 \note{This function ``steals'' a reference to \var{o}.}
1593\end{cfuncdesc}
1594
1595\begin{cfuncdesc}{void}{PyTuple_SET_ITEM}{PyObject *p,
1596 int pos, PyObject *o}
1597 Like \cfunction{PyTuple_SetItem()}, but does no error checking, and
1598 should \emph{only} be used to fill in brand new tuples. \note{This
1599 function ``steals'' a reference to \var{o}.}
1600\end{cfuncdesc}
1601
1602\begin{cfuncdesc}{int}{_PyTuple_Resize}{PyObject **p, int newsize}
1603 Can be used to resize a tuple. \var{newsize} will be the new length
1604 of the tuple. Because tuples are \emph{supposed} to be immutable,
1605 this should only be used if there is only one reference to the
1606 object. Do \emph{not} use this if the tuple may already be known to
1607 some other part of the code. The tuple will always grow or shrink
1608 at the end. Think of this as destroying the old tuple and creating
1609 a new one, only more efficiently. Returns \code{0} on success.
1610 Client code should never assume that the resulting value of
1611 \code{*\var{p}} will be the same as before calling this function.
1612 If the object referenced by \code{*\var{p}} is replaced, the
1613 original \code{*\var{p}} is destroyed. On failure, returns
1614 \code{-1} and sets \code{*\var{p}} to \NULL, and raises
1615 \exception{MemoryError} or
1616 \exception{SystemError}.
Tim Petersf582b822001-12-11 18:51:08 +00001617 \versionchanged[Removed unused third parameter, \var{last_is_sticky}]{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +00001618\end{cfuncdesc}
1619
1620
1621\subsection{List Objects \label{listObjects}}
1622
1623\obindex{list}
1624\begin{ctypedesc}{PyListObject}
1625 This subtype of \ctype{PyObject} represents a Python list object.
1626\end{ctypedesc}
1627
1628\begin{cvardesc}{PyTypeObject}{PyList_Type}
1629 This instance of \ctype{PyTypeObject} represents the Python list
1630 type. This is the same object as \code{types.ListType}.
1631 \withsubitem{(in module types)}{\ttindex{ListType}}
1632\end{cvardesc}
1633
1634\begin{cfuncdesc}{int}{PyList_Check}{PyObject *p}
1635 Returns true if its argument is a \ctype{PyListObject}.
1636\end{cfuncdesc}
1637
1638\begin{cfuncdesc}{PyObject*}{PyList_New}{int len}
1639 Returns a new list of length \var{len} on success, or \NULL{} on
1640 failure.
1641\end{cfuncdesc}
1642
1643\begin{cfuncdesc}{int}{PyList_Size}{PyObject *list}
1644 Returns the length of the list object in \var{list}; this is
1645 equivalent to \samp{len(\var{list})} on a list object.
1646 \bifuncindex{len}
1647\end{cfuncdesc}
1648
1649\begin{cfuncdesc}{int}{PyList_GET_SIZE}{PyObject *list}
1650 Macro form of \cfunction{PyList_Size()} without error checking.
1651\end{cfuncdesc}
1652
1653\begin{cfuncdesc}{PyObject*}{PyList_GetItem}{PyObject *list, int index}
1654 Returns the object at position \var{pos} in the list pointed to by
1655 \var{p}. If \var{pos} is out of bounds, returns \NULL{} and sets an
1656 \exception{IndexError} exception.
1657\end{cfuncdesc}
1658
1659\begin{cfuncdesc}{PyObject*}{PyList_GET_ITEM}{PyObject *list, int i}
1660 Macro form of \cfunction{PyList_GetItem()} without error checking.
1661\end{cfuncdesc}
1662
1663\begin{cfuncdesc}{int}{PyList_SetItem}{PyObject *list, int index,
1664 PyObject *item}
1665 Sets the item at index \var{index} in list to \var{item}. Returns
1666 \code{0} on success or \code{-1} on failure. \note{This function
1667 ``steals'' a reference to \var{item} and discards a reference to an
1668 item already in the list at the affected position.}
1669\end{cfuncdesc}
1670
1671\begin{cfuncdesc}{void}{PyList_SET_ITEM}{PyObject *list, int i,
1672 PyObject *o}
1673 Macro form of \cfunction{PyList_SetItem()} without error checking.
1674 This is normally only used to fill in new lists where there is no
1675 previous content.
1676 \note{This function ``steals'' a reference to \var{item}, and,
1677 unlike \cfunction{PyList_SetItem()}, does \emph{not} discard a
1678 reference to any item that it being replaced; any reference in
1679 \var{list} at position \var{i} will be leaked.}
1680\end{cfuncdesc}
1681
1682\begin{cfuncdesc}{int}{PyList_Insert}{PyObject *list, int index,
1683 PyObject *item}
1684 Inserts the item \var{item} into list \var{list} in front of index
1685 \var{index}. Returns \code{0} if successful; returns \code{-1} and
1686 raises an exception if unsuccessful. Analogous to
1687 \code{\var{list}.insert(\var{index}, \var{item})}.
1688\end{cfuncdesc}
1689
1690\begin{cfuncdesc}{int}{PyList_Append}{PyObject *list, PyObject *item}
1691 Appends the object \var{item} at the end of list \var{list}.
1692 Returns \code{0} if successful; returns \code{-1} and sets an
1693 exception if unsuccessful. Analogous to
1694 \code{\var{list}.append(\var{item})}.
1695\end{cfuncdesc}
1696
1697\begin{cfuncdesc}{PyObject*}{PyList_GetSlice}{PyObject *list,
1698 int low, int high}
1699 Returns a list of the objects in \var{list} containing the objects
1700 \emph{between} \var{low} and \var{high}. Returns \NULL{} and sets
1701 an exception if unsuccessful.
1702 Analogous to \code{\var{list}[\var{low}:\var{high}]}.
1703\end{cfuncdesc}
1704
1705\begin{cfuncdesc}{int}{PyList_SetSlice}{PyObject *list,
1706 int low, int high,
1707 PyObject *itemlist}
1708 Sets the slice of \var{list} between \var{low} and \var{high} to the
1709 contents of \var{itemlist}. Analogous to
1710 \code{\var{list}[\var{low}:\var{high}] = \var{itemlist}}. Returns
1711 \code{0} on success, \code{-1} on failure.
1712\end{cfuncdesc}
1713
1714\begin{cfuncdesc}{int}{PyList_Sort}{PyObject *list}
1715 Sorts the items of \var{list} in place. Returns \code{0} on
1716 success, \code{-1} on failure. This is equivalent to
1717 \samp{\var{list}.sort()}.
1718\end{cfuncdesc}
1719
1720\begin{cfuncdesc}{int}{PyList_Reverse}{PyObject *list}
1721 Reverses the items of \var{list} in place. Returns \code{0} on
1722 success, \code{-1} on failure. This is the equivalent of
1723 \samp{\var{list}.reverse()}.
1724\end{cfuncdesc}
1725
1726\begin{cfuncdesc}{PyObject*}{PyList_AsTuple}{PyObject *list}
1727 Returns a new tuple object containing the contents of \var{list};
1728 equivalent to \samp{tuple(\var{list})}.\bifuncindex{tuple}
1729\end{cfuncdesc}
1730
1731
1732\section{Mapping Objects \label{mapObjects}}
1733
1734\obindex{mapping}
1735
1736
1737\subsection{Dictionary Objects \label{dictObjects}}
1738
1739\obindex{dictionary}
1740\begin{ctypedesc}{PyDictObject}
1741 This subtype of \ctype{PyObject} represents a Python dictionary
1742 object.
1743\end{ctypedesc}
1744
1745\begin{cvardesc}{PyTypeObject}{PyDict_Type}
1746 This instance of \ctype{PyTypeObject} represents the Python
1747 dictionary type. This is exposed to Python programs as
1748 \code{types.DictType} and \code{types.DictionaryType}.
1749 \withsubitem{(in module types)}{\ttindex{DictType}\ttindex{DictionaryType}}
1750\end{cvardesc}
1751
1752\begin{cfuncdesc}{int}{PyDict_Check}{PyObject *p}
1753 Returns true if its argument is a \ctype{PyDictObject}.
1754\end{cfuncdesc}
1755
1756\begin{cfuncdesc}{PyObject*}{PyDict_New}{}
1757 Returns a new empty dictionary, or \NULL{} on failure.
1758\end{cfuncdesc}
1759
1760\begin{cfuncdesc}{PyObject*}{PyDictProxy_New}{PyObject *dict}
1761 Return a proxy object for a mapping which enforces read-only
1762 behavior. This is normally used to create a proxy to prevent
1763 modification of the dictionary for non-dynamic class types.
1764 \versionadded{2.2}
1765\end{cfuncdesc}
1766
1767\begin{cfuncdesc}{void}{PyDict_Clear}{PyObject *p}
1768 Empties an existing dictionary of all key-value pairs.
1769\end{cfuncdesc}
1770
1771\begin{cfuncdesc}{PyObject*}{PyDict_Copy}{PyObject *p}
1772 Returns a new dictionary that contains the same key-value pairs as
1773 \var{p}.
1774 \versionadded{1.6}
1775\end{cfuncdesc}
1776
1777\begin{cfuncdesc}{int}{PyDict_SetItem}{PyObject *p, PyObject *key,
1778 PyObject *val}
1779 Inserts \var{value} into the dictionary \var{p} with a key of
1780 \var{key}. \var{key} must be hashable; if it isn't,
1781 \exception{TypeError} will be raised.
1782 Returns \code{0} on success or \code{-1} on failure.
1783\end{cfuncdesc}
1784
1785\begin{cfuncdesc}{int}{PyDict_SetItemString}{PyObject *p,
1786 char *key,
1787 PyObject *val}
1788 Inserts \var{value} into the dictionary \var{p} using \var{key} as a
1789 key. \var{key} should be a \ctype{char*}. The key object is created
1790 using \code{PyString_FromString(\var{key})}. Returns \code{0} on
1791 success or \code{-1} on failure.
1792 \ttindex{PyString_FromString()}
1793\end{cfuncdesc}
1794
1795\begin{cfuncdesc}{int}{PyDict_DelItem}{PyObject *p, PyObject *key}
1796 Removes the entry in dictionary \var{p} with key \var{key}.
1797 \var{key} must be hashable; if it isn't, \exception{TypeError} is
Skip Montanaroa23bc422002-01-23 08:18:30 +00001798 raised. Returns \code{0} on success or \code{-1} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00001799\end{cfuncdesc}
1800
1801\begin{cfuncdesc}{int}{PyDict_DelItemString}{PyObject *p, char *key}
1802 Removes the entry in dictionary \var{p} which has a key specified by
1803 the string \var{key}. Returns \code{0} on success or \code{-1} on
1804 failure.
1805\end{cfuncdesc}
1806
1807\begin{cfuncdesc}{PyObject*}{PyDict_GetItem}{PyObject *p, PyObject *key}
1808 Returns the object from dictionary \var{p} which has a key
1809 \var{key}. Returns \NULL{} if the key \var{key} is not present, but
1810 \emph{without} setting an exception.
1811\end{cfuncdesc}
1812
1813\begin{cfuncdesc}{PyObject*}{PyDict_GetItemString}{PyObject *p, char *key}
1814 This is the same as \cfunction{PyDict_GetItem()}, but \var{key} is
1815 specified as a \ctype{char*}, rather than a \ctype{PyObject*}.
1816\end{cfuncdesc}
1817
1818\begin{cfuncdesc}{PyObject*}{PyDict_Items}{PyObject *p}
1819 Returns a \ctype{PyListObject} containing all the items from the
1820 dictionary, as in the dictinoary method \method{items()} (see the
1821 \citetitle[../lib/lib.html]{Python Library Reference}).
1822\end{cfuncdesc}
1823
1824\begin{cfuncdesc}{PyObject*}{PyDict_Keys}{PyObject *p}
1825 Returns a \ctype{PyListObject} containing all the keys from the
1826 dictionary, as in the dictionary method \method{keys()} (see the
1827 \citetitle[../lib/lib.html]{Python Library Reference}).
1828\end{cfuncdesc}
1829
1830\begin{cfuncdesc}{PyObject*}{PyDict_Values}{PyObject *p}
1831 Returns a \ctype{PyListObject} containing all the values from the
1832 dictionary \var{p}, as in the dictionary method \method{values()}
1833 (see the \citetitle[../lib/lib.html]{Python Library Reference}).
1834\end{cfuncdesc}
1835
1836\begin{cfuncdesc}{int}{PyDict_Size}{PyObject *p}
1837 Returns the number of items in the dictionary. This is equivalent
1838 to \samp{len(\var{p})} on a dictionary.\bifuncindex{len}
1839\end{cfuncdesc}
1840
1841\begin{cfuncdesc}{int}{PyDict_Next}{PyObject *p, int *ppos,
1842 PyObject **pkey, PyObject **pvalue}
1843 Iterate over all key-value pairs in the dictionary \var{p}. The
1844 \ctype{int} referred to by \var{ppos} must be initialized to
1845 \code{0} prior to the first call to this function to start the
1846 iteration; the function returns true for each pair in the
1847 dictionary, and false once all pairs have been reported. The
1848 parameters \var{pkey} and \var{pvalue} should either point to
1849 \ctype{PyObject*} variables that will be filled in with each key and
Skip Montanaroea3ceaa2002-01-23 10:54:41 +00001850 value, respectively, or may be \NULL. Any references returned through
1851 them are borrowed.
Fred Drake3adf79e2001-10-12 19:01:43 +00001852
1853 For example:
1854
1855\begin{verbatim}
1856PyObject *key, *value;
1857int pos = 0;
1858
1859while (PyDict_Next(self->dict, &pos, &key, &value)) {
1860 /* do something interesting with the values... */
1861 ...
1862}
1863\end{verbatim}
1864
1865 The dictionary \var{p} should not be mutated during iteration. It
1866 is safe (since Python 2.1) to modify the values of the keys as you
1867 iterate over the dictionary, but only so long as the set of keys
1868 does not change. For example:
1869
1870\begin{verbatim}
1871PyObject *key, *value;
1872int pos = 0;
1873
1874while (PyDict_Next(self->dict, &pos, &key, &value)) {
1875 int i = PyInt_AS_LONG(value) + 1;
1876 PyObject *o = PyInt_FromLong(i);
1877 if (o == NULL)
1878 return -1;
1879 if (PyDict_SetItem(self->dict, key, o) < 0) {
1880 Py_DECREF(o);
1881 return -1;
1882 }
1883 Py_DECREF(o);
1884}
1885\end{verbatim}
1886\end{cfuncdesc}
1887
1888\begin{cfuncdesc}{int}{PyDict_Merge}{PyObject *a, PyObject *b, int override}
Tim Petersf582b822001-12-11 18:51:08 +00001889 Iterate over mapping object \var{b} adding key-value pairs to dictionary
1890 \var{a}.
1891 \var{b} may be a dictionary, or any object supporting
1892 \function{PyMapping_Keys()} and \function{PyObject_GetItem()}.
1893 If \var{override} is true, existing pairs in \var{a} will
Fred Drake3adf79e2001-10-12 19:01:43 +00001894 be replaced if a matching key is found in \var{b}, otherwise pairs
1895 will only be added if there is not a matching key in \var{a}.
Tim Petersf582b822001-12-11 18:51:08 +00001896 Return \code{0} on success or \code{-1} if an exception was
Fred Drake3adf79e2001-10-12 19:01:43 +00001897 raised.
1898\versionadded{2.2}
1899\end{cfuncdesc}
1900
1901\begin{cfuncdesc}{int}{PyDict_Update}{PyObject *a, PyObject *b}
1902 This is the same as \code{PyDict_Merge(\var{a}, \var{b}, 1)} in C,
Tim Petersf582b822001-12-11 18:51:08 +00001903 or \code{\var{a}.update(\var{b})} in Python. Return \code{0} on
Fred Drake3adf79e2001-10-12 19:01:43 +00001904 success or \code{-1} if an exception was raised.
1905 \versionadded{2.2}
1906\end{cfuncdesc}
1907
Tim Petersf582b822001-12-11 18:51:08 +00001908\begin{cfuncdesc}{int}{PyDict_MergeFromSeq2}{PyObject *a, PyObject *seq2,
1909 int override}
1910 Update or merge into dictionary \var{a}, from the key-value pairs in
1911 \var{seq2}. \var{seq2} must be an iterable object producing
1912 iterable objects of length 2, viewed as key-value pairs. In case of
1913 duplicate keys, the last wins if \var{override} is true, else the
1914 first wins.
1915 Return \code{0} on success or \code{-1} if an exception
1916 was raised.
1917 Equivalent Python (except for the return value):
1918
1919\begin{verbatim}
1920def PyDict_MergeFromSeq2(a, seq2, override):
1921 for key, value in seq2:
1922 if override or key not in a:
1923 a[key] = value
1924\end{verbatim}
1925
1926 \versionadded{2.2}
1927\end{cfuncdesc}
Fred Drake3adf79e2001-10-12 19:01:43 +00001928
Fred Drake54e62942001-12-11 19:40:16 +00001929
Fred Drake3adf79e2001-10-12 19:01:43 +00001930\section{Other Objects \label{otherObjects}}
1931
1932\subsection{File Objects \label{fileObjects}}
1933
1934\obindex{file}
1935Python's built-in file objects are implemented entirely on the
1936\ctype{FILE*} support from the C standard library. This is an
1937implementation detail and may change in future releases of Python.
1938
1939\begin{ctypedesc}{PyFileObject}
1940 This subtype of \ctype{PyObject} represents a Python file object.
1941\end{ctypedesc}
1942
1943\begin{cvardesc}{PyTypeObject}{PyFile_Type}
1944 This instance of \ctype{PyTypeObject} represents the Python file
1945 type. This is exposed to Python programs as \code{types.FileType}.
1946 \withsubitem{(in module types)}{\ttindex{FileType}}
1947\end{cvardesc}
1948
1949\begin{cfuncdesc}{int}{PyFile_Check}{PyObject *p}
1950 Returns true if its argument is a \ctype{PyFileObject} or a subtype
1951 of \ctype{PyFileObject}.
1952 \versionchanged[Allowed subtypes to be accepted]{2.2}
1953\end{cfuncdesc}
1954
1955\begin{cfuncdesc}{int}{PyFile_CheckExact}{PyObject *p}
1956 Returns true if its argument is a \ctype{PyFileObject}, but not a
1957 subtype of \ctype{PyFileObject}.
1958 \versionadded{2.2}
1959\end{cfuncdesc}
1960
1961\begin{cfuncdesc}{PyObject*}{PyFile_FromString}{char *filename, char *mode}
1962 On success, returns a new file object that is opened on the file
1963 given by \var{filename}, with a file mode given by \var{mode}, where
1964 \var{mode} has the same semantics as the standard C routine
1965 \cfunction{fopen()}\ttindex{fopen()}. On failure, returns \NULL.
1966\end{cfuncdesc}
1967
1968\begin{cfuncdesc}{PyObject*}{PyFile_FromFile}{FILE *fp,
1969 char *name, char *mode,
1970 int (*close)(FILE*)}
1971 Creates a new \ctype{PyFileObject} from the already-open standard C
1972 file pointer, \var{fp}. The function \var{close} will be called
1973 when the file should be closed. Returns \NULL{} on failure.
1974\end{cfuncdesc}
1975
1976\begin{cfuncdesc}{FILE*}{PyFile_AsFile}{PyFileObject *p}
1977 Returns the file object associated with \var{p} as a \ctype{FILE*}.
1978\end{cfuncdesc}
1979
1980\begin{cfuncdesc}{PyObject*}{PyFile_GetLine}{PyObject *p, int n}
1981 Equivalent to \code{\var{p}.readline(\optional{\var{n}})}, this
1982 function reads one line from the object \var{p}. \var{p} may be a
1983 file object or any object with a \method{readline()} method. If
1984 \var{n} is \code{0}, exactly one line is read, regardless of the
1985 length of the line. If \var{n} is greater than \code{0}, no more
1986 than \var{n} bytes will be read from the file; a partial line can be
1987 returned. In both cases, an empty string is returned if the end of
1988 the file is reached immediately. If \var{n} is less than \code{0},
1989 however, one line is read regardless of length, but
1990 \exception{EOFError} is raised if the end of the file is reached
1991 immediately.
1992 \withsubitem{(built-in exception)}{\ttindex{EOFError}}
1993\end{cfuncdesc}
1994
1995\begin{cfuncdesc}{PyObject*}{PyFile_Name}{PyObject *p}
1996 Returns the name of the file specified by \var{p} as a string
1997 object.
1998\end{cfuncdesc}
1999
2000\begin{cfuncdesc}{void}{PyFile_SetBufSize}{PyFileObject *p, int n}
2001 Available on systems with \cfunction{setvbuf()}\ttindex{setvbuf()}
2002 only. This should only be called immediately after file object
2003 creation.
2004\end{cfuncdesc}
2005
2006\begin{cfuncdesc}{int}{PyFile_SoftSpace}{PyObject *p, int newflag}
2007 This function exists for internal use by the interpreter. Sets the
2008 \member{softspace} attribute of \var{p} to \var{newflag} and
2009 \withsubitem{(file attribute)}{\ttindex{softspace}}returns the
2010 previous value. \var{p} does not have to be a file object for this
2011 function to work properly; any object is supported (thought its only
2012 interesting if the \member{softspace} attribute can be set). This
2013 function clears any errors, and will return \code{0} as the previous
2014 value if the attribute either does not exist or if there were errors
2015 in retrieving it. There is no way to detect errors from this
2016 function, but doing so should not be needed.
2017\end{cfuncdesc}
2018
2019\begin{cfuncdesc}{int}{PyFile_WriteObject}{PyObject *obj, PyFileObject *p,
2020 int flags}
2021 Writes object \var{obj} to file object \var{p}. The only supported
2022 flag for \var{flags} is
2023 \constant{Py_PRINT_RAW}\ttindex{Py_PRINT_RAW}; if given, the
2024 \function{str()} of the object is written instead of the
2025 \function{repr()}. Returns \code{0} on success or \code{-1} on
2026 failure; the appropriate exception will be set.
2027\end{cfuncdesc}
2028
Fred Drake454af892001-11-29 22:42:59 +00002029\begin{cfuncdesc}{int}{PyFile_WriteString}{const char *s, PyFileObject *p}
Fred Drake3adf79e2001-10-12 19:01:43 +00002030 Writes string \var{s} to file object \var{p}. Returns \code{0} on
2031 success or \code{-1} on failure; the appropriate exception will be
2032 set.
2033\end{cfuncdesc}
2034
2035
2036\subsection{Instance Objects \label{instanceObjects}}
2037
2038\obindex{instance}
2039There are very few functions specific to instance objects.
2040
2041\begin{cvardesc}{PyTypeObject}{PyInstance_Type}
2042 Type object for class instances.
2043\end{cvardesc}
2044
2045\begin{cfuncdesc}{int}{PyInstance_Check}{PyObject *obj}
2046 Returns true if \var{obj} is an instance.
2047\end{cfuncdesc}
2048
2049\begin{cfuncdesc}{PyObject*}{PyInstance_New}{PyObject *class,
2050 PyObject *arg,
2051 PyObject *kw}
2052 Create a new instance of a specific class. The parameters \var{arg}
2053 and \var{kw} are used as the positional and keyword parameters to
2054 the object's constructor.
2055\end{cfuncdesc}
2056
2057\begin{cfuncdesc}{PyObject*}{PyInstance_NewRaw}{PyObject *class,
2058 PyObject *dict}
2059 Create a new instance of a specific class without calling it's
2060 constructor. \var{class} is the class of new object. The
2061 \var{dict} parameter will be used as the object's \member{__dict__};
2062 if \NULL, a new dictionary will be created for the instance.
2063\end{cfuncdesc}
2064
2065
2066\subsection{Method Objects \label{method-objects}}
2067
2068\obindex{method}
2069There are some useful functions that are useful for working with
2070method objects.
2071
2072\begin{cvardesc}{PyTypeObject}{PyMethod_Type}
2073 This instance of \ctype{PyTypeObject} represents the Python method
2074 type. This is exposed to Python programs as \code{types.MethodType}.
2075 \withsubitem{(in module types)}{\ttindex{MethodType}}
2076\end{cvardesc}
2077
2078\begin{cfuncdesc}{int}{PyMethod_Check}{PyObject *o}
2079 Return true if \var{o} is a method object (has type
2080 \cdata{PyMethod_Type}). The parameter must not be \NULL.
2081\end{cfuncdesc}
2082
2083\begin{cfuncdesc}{PyObject*}{PyMethod_New}{PyObject *func.
2084 PyObject *self, PyObject *class}
2085 Return a new method object, with \var{func} being any callable
2086 object; this is the function that will be called when the method is
2087 called. If this method should be bound to an instance, \var{self}
2088 should be the instance and \var{class} should be the class of
2089 \var{self}, otherwise \var{self} should be \NULL{} and \var{class}
2090 should be the class which provides the unbound method..
2091\end{cfuncdesc}
2092
2093\begin{cfuncdesc}{PyObject*}{PyMethod_Class}{PyObject *meth}
2094 Return the class object from which the method \var{meth} was
2095 created; if this was created from an instance, it will be the class
2096 of the instance.
2097\end{cfuncdesc}
2098
2099\begin{cfuncdesc}{PyObject*}{PyMethod_GET_CLASS}{PyObject *meth}
2100 Macro version of \cfunction{PyMethod_Class()} which avoids error
2101 checking.
2102\end{cfuncdesc}
2103
2104\begin{cfuncdesc}{PyObject*}{PyMethod_Function}{PyObject *meth}
2105 Return the function object associated with the method \var{meth}.
2106\end{cfuncdesc}
2107
2108\begin{cfuncdesc}{PyObject*}{PyMethod_GET_FUNCTION}{PyObject *meth}
2109 Macro version of \cfunction{PyMethod_Function()} which avoids error
2110 checking.
2111\end{cfuncdesc}
2112
2113\begin{cfuncdesc}{PyObject*}{PyMethod_Self}{PyObject *meth}
2114 Return the instance associated with the method \var{meth} if it is
2115 bound, otherwise return \NULL.
2116\end{cfuncdesc}
2117
2118\begin{cfuncdesc}{PyObject*}{PyMethod_GET_SELF}{PyObject *meth}
2119 Macro version of \cfunction{PyMethod_Self()} which avoids error
2120 checking.
2121\end{cfuncdesc}
2122
2123
2124\subsection{Module Objects \label{moduleObjects}}
2125
2126\obindex{module}
2127There are only a few functions special to module objects.
2128
2129\begin{cvardesc}{PyTypeObject}{PyModule_Type}
2130 This instance of \ctype{PyTypeObject} represents the Python module
2131 type. This is exposed to Python programs as
2132 \code{types.ModuleType}.
2133 \withsubitem{(in module types)}{\ttindex{ModuleType}}
2134\end{cvardesc}
2135
2136\begin{cfuncdesc}{int}{PyModule_Check}{PyObject *p}
2137 Returns true if \var{p} is a module object, or a subtype of a module
2138 object.
2139 \versionchanged[Allowed subtypes to be accepted]{2.2}
2140\end{cfuncdesc}
2141
2142\begin{cfuncdesc}{int}{PyModule_CheckExact}{PyObject *p}
2143 Returns true if \var{p} is a module object, but not a subtype of
2144 \cdata{PyModule_Type}.
2145 \versionadded{2.2}
2146\end{cfuncdesc}
2147
2148\begin{cfuncdesc}{PyObject*}{PyModule_New}{char *name}
2149 Return a new module object with the \member{__name__} attribute set
2150 to \var{name}. Only the module's \member{__doc__} and
2151 \member{__name__} attributes are filled in; the caller is
2152 responsible for providing a \member{__file__} attribute.
2153 \withsubitem{(module attribute)}{
2154 \ttindex{__name__}\ttindex{__doc__}\ttindex{__file__}}
2155\end{cfuncdesc}
2156
2157\begin{cfuncdesc}{PyObject*}{PyModule_GetDict}{PyObject *module}
2158 Return the dictionary object that implements \var{module}'s
2159 namespace; this object is the same as the \member{__dict__}
2160 attribute of the module object. This function never fails.
2161 \withsubitem{(module attribute)}{\ttindex{__dict__}}
Fred Drakef495ef72002-04-12 19:32:07 +00002162 It is recommended extensions use other \cfunction{PyModule_*()}
2163 and \cfunction{PyObject_*()} functions rather than directly
2164 manipulate a module's \member{__dict__}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002165\end{cfuncdesc}
2166
2167\begin{cfuncdesc}{char*}{PyModule_GetName}{PyObject *module}
2168 Return \var{module}'s \member{__name__} value. If the module does
2169 not provide one, or if it is not a string, \exception{SystemError}
2170 is raised and \NULL{} is returned.
2171 \withsubitem{(module attribute)}{\ttindex{__name__}}
2172 \withsubitem{(built-in exception)}{\ttindex{SystemError}}
2173\end{cfuncdesc}
2174
2175\begin{cfuncdesc}{char*}{PyModule_GetFilename}{PyObject *module}
2176 Return the name of the file from which \var{module} was loaded using
2177 \var{module}'s \member{__file__} attribute. If this is not defined,
2178 or if it is not a string, raise \exception{SystemError} and return
2179 \NULL.
2180 \withsubitem{(module attribute)}{\ttindex{__file__}}
2181 \withsubitem{(built-in exception)}{\ttindex{SystemError}}
2182\end{cfuncdesc}
2183
2184\begin{cfuncdesc}{int}{PyModule_AddObject}{PyObject *module,
2185 char *name, PyObject *value}
2186 Add an object to \var{module} as \var{name}. This is a convenience
2187 function which can be used from the module's initialization
2188 function. This steals a reference to \var{value}. Returns
2189 \code{-1} on error, \code{0} on success.
2190 \versionadded{2.0}
2191\end{cfuncdesc}
2192
2193\begin{cfuncdesc}{int}{PyModule_AddIntConstant}{PyObject *module,
2194 char *name, int value}
2195 Add an integer constant to \var{module} as \var{name}. This
2196 convenience function can be used from the module's initialization
2197 function. Returns \code{-1} on error, \code{0} on success.
2198 \versionadded{2.0}
2199\end{cfuncdesc}
2200
2201\begin{cfuncdesc}{int}{PyModule_AddStringConstant}{PyObject *module,
2202 char *name, char *value}
2203 Add a string constant to \var{module} as \var{name}. This
2204 convenience function can be used from the module's initialization
2205 function. The string \var{value} must be null-terminated. Returns
2206 \code{-1} on error, \code{0} on success.
2207 \versionadded{2.0}
2208\end{cfuncdesc}
2209
2210
2211\subsection{Iterator Objects \label{iterator-objects}}
2212
2213Python provides two general-purpose iterator objects. The first, a
2214sequence iterator, works with an arbitrary sequence supporting the
2215\method{__getitem__()} method. The second works with a callable
2216object and a sentinel value, calling the callable for each item in the
2217sequence, and ending the iteration when the sentinel value is
2218returned.
2219
2220\begin{cvardesc}{PyTypeObject}{PySeqIter_Type}
2221 Type object for iterator objects returned by
2222 \cfunction{PySeqIter_New()} and the one-argument form of the
2223 \function{iter()} built-in function for built-in sequence types.
2224 \versionadded{2.2}
2225\end{cvardesc}
2226
2227\begin{cfuncdesc}{int}{PySeqIter_Check}{op}
2228 Return true if the type of \var{op} is \cdata{PySeqIter_Type}.
2229 \versionadded{2.2}
2230\end{cfuncdesc}
2231
2232\begin{cfuncdesc}{PyObject*}{PySeqIter_New}{PyObject *seq}
2233 Return an iterator that works with a general sequence object,
2234 \var{seq}. The iteration ends when the sequence raises
2235 \exception{IndexError} for the subscripting operation.
2236 \versionadded{2.2}
2237\end{cfuncdesc}
2238
2239\begin{cvardesc}{PyTypeObject}{PyCallIter_Type}
2240 Type object for iterator objects returned by
2241 \cfunction{PyCallIter_New()} and the two-argument form of the
2242 \function{iter()} built-in function.
2243 \versionadded{2.2}
2244\end{cvardesc}
2245
2246\begin{cfuncdesc}{int}{PyCallIter_Check}{op}
2247 Return true if the type of \var{op} is \cdata{PyCallIter_Type}.
2248 \versionadded{2.2}
2249\end{cfuncdesc}
2250
2251\begin{cfuncdesc}{PyObject*}{PyCallIter_New}{PyObject *callable,
2252 PyObject *sentinel}
2253 Return a new iterator. The first parameter, \var{callable}, can be
2254 any Python callable object that can be called with no parameters;
2255 each call to it should return the next item in the iteration. When
2256 \var{callable} returns a value equal to \var{sentinel}, the
2257 iteration will be terminated.
2258 \versionadded{2.2}
2259\end{cfuncdesc}
2260
2261
2262\subsection{Descriptor Objects \label{descriptor-objects}}
2263
Fred Drake54e62942001-12-11 19:40:16 +00002264``Descriptors'' are objects that describe some attribute of an object.
2265They are found in the dictionary of type objects.
2266
Fred Drake3adf79e2001-10-12 19:01:43 +00002267\begin{cvardesc}{PyTypeObject}{PyProperty_Type}
Fred Drake54e62942001-12-11 19:40:16 +00002268 The type object for the built-in descriptor types.
Fred Drake3adf79e2001-10-12 19:01:43 +00002269 \versionadded{2.2}
2270\end{cvardesc}
2271
2272\begin{cfuncdesc}{PyObject*}{PyDescr_NewGetSet}{PyTypeObject *type,
2273 PyGetSetDef *getset}
2274 \versionadded{2.2}
2275\end{cfuncdesc}
2276
2277\begin{cfuncdesc}{PyObject*}{PyDescr_NewMember}{PyTypeObject *type,
2278 PyMemberDef *meth}
2279 \versionadded{2.2}
2280\end{cfuncdesc}
2281
2282\begin{cfuncdesc}{PyObject*}{PyDescr_NewMethod}{PyTypeObject *type,
2283 PyMethodDef *meth}
2284 \versionadded{2.2}
2285\end{cfuncdesc}
2286
2287\begin{cfuncdesc}{PyObject*}{PyDescr_NewWrapper}{PyTypeObject *type,
2288 struct wrapperbase *wrapper,
2289 void *wrapped}
2290 \versionadded{2.2}
2291\end{cfuncdesc}
2292
2293\begin{cfuncdesc}{int}{PyDescr_IsData}{PyObject *descr}
2294 Returns true if the descriptor objects \var{descr} describes a data
2295 attribute, or false if it describes a method. \var{descr} must be a
2296 descriptor object; there is no error checking.
2297 \versionadded{2.2}
2298\end{cfuncdesc}
2299
2300\begin{cfuncdesc}{PyObject*}{PyWrapper_New}{PyObject *, PyObject *}
2301 \versionadded{2.2}
2302\end{cfuncdesc}
2303
2304
2305\subsection{Slice Objects \label{slice-objects}}
2306
2307\begin{cvardesc}{PyTypeObject}{PySlice_Type}
2308 The type object for slice objects. This is the same as
2309 \code{types.SliceType}.
2310 \withsubitem{(in module types)}{\ttindex{SliceType}}
2311\end{cvardesc}
2312
2313\begin{cfuncdesc}{int}{PySlice_Check}{PyObject *ob}
2314 Returns true if \var{ob} is a slice object; \var{ob} must not be
2315 \NULL.
2316\end{cfuncdesc}
2317
2318\begin{cfuncdesc}{PyObject*}{PySlice_New}{PyObject *start, PyObject *stop,
2319 PyObject *step}
2320 Return a new slice object with the given values. The \var{start},
2321 \var{stop}, and \var{step} parameters are used as the values of the
2322 slice object attributes of the same names. Any of the values may be
2323 \NULL, in which case the \code{None} will be used for the
2324 corresponding attribute. Returns \NULL{} if the new object could
2325 not be allocated.
2326\end{cfuncdesc}
2327
2328\begin{cfuncdesc}{int}{PySlice_GetIndices}{PySliceObject *slice, int length,
2329 int *start, int *stop, int *step}
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002330Retrieve the start, stop and step indices from the slice object
2331\var{slice}, assuming a sequence of length \var{length}. Treats
2332indices greater than \var{length} as errors.
2333
2334Returns 0 on success and -1 on error with no exception set (unless one
2335of the indices was not \constant{None} and failed to be converted to
2336an integer, in which case -1 is returned with an exception set).
2337
2338You probably do not want to use this function. If you want to use
2339slice objects in versions of Python prior to 2.3, you would probably
2340do well to incorporate the source of \cfunction{PySlice_GetIndicesEx},
2341suitably renamed, in the source of your extension.
2342\end{cfuncdesc}
2343
2344\begin{cfuncdesc}{int}{PySlice_GetIndicesEx}{PySliceObject *slice, int length,
2345 int *start, int *stop, int *step,
2346 int *slicelength}
2347Usable replacement for \cfunction{PySlice_GetIndices}. Retrieve the
2348start, stop, and step indices from the slice object \var{slice}
2349assuming a sequence of length \var{length}, and store the length of
2350the slice in \var{slicelength}. Out of bounds indices are clipped in
2351a manner consistent with the handling of normal slices.
2352
2353Returns 0 on success and -1 on error with exception set.
2354
2355\versionadded{2.3}
Fred Drake3adf79e2001-10-12 19:01:43 +00002356\end{cfuncdesc}
2357
2358
2359\subsection{Weak Reference Objects \label{weakref-objects}}
2360
2361Python supports \emph{weak references} as first-class objects. There
2362are two specific object types which directly implement weak
2363references. The first is a simple reference object, and the second
2364acts as a proxy for the original object as much as it can.
2365
2366\begin{cfuncdesc}{int}{PyWeakref_Check}{ob}
2367 Return true if \var{ob} is either a reference or proxy object.
2368 \versionadded{2.2}
2369\end{cfuncdesc}
2370
2371\begin{cfuncdesc}{int}{PyWeakref_CheckRef}{ob}
2372 Return true if \var{ob} is a reference object.
2373 \versionadded{2.2}
2374\end{cfuncdesc}
2375
2376\begin{cfuncdesc}{int}{PyWeakref_CheckProxy}{ob}
2377 Return true if \var{ob} is a proxy object.
2378 \versionadded{2.2}
2379\end{cfuncdesc}
2380
2381\begin{cfuncdesc}{PyObject*}{PyWeakref_NewRef}{PyObject *ob,
2382 PyObject *callback}
2383 Return a weak reference object for the object \var{ob}. This will
2384 always return a new reference, but is not guaranteed to create a new
2385 object; an existing reference object may be returned. The second
2386 parameter, \var{callback}, can be a callable object that receives
2387 notification when \var{ob} is garbage collected; it should accept a
2388 single paramter, which will be the weak reference object itself.
2389 \var{callback} may also be \code{None} or \NULL. If \var{ob}
2390 is not a weakly-referencable object, or if \var{callback} is not
2391 callable, \code{None}, or \NULL, this will return \NULL{} and
2392 raise \exception{TypeError}.
2393 \versionadded{2.2}
2394\end{cfuncdesc}
2395
2396\begin{cfuncdesc}{PyObject*}{PyWeakref_NewProxy}{PyObject *ob,
2397 PyObject *callback}
2398 Return a weak reference proxy object for the object \var{ob}. This
2399 will always return a new reference, but is not guaranteed to create
2400 a new object; an existing proxy object may be returned. The second
2401 parameter, \var{callback}, can be a callable object that receives
2402 notification when \var{ob} is garbage collected; it should accept a
2403 single paramter, which will be the weak reference object itself.
2404 \var{callback} may also be \code{None} or \NULL. If \var{ob} is not
2405 a weakly-referencable object, or if \var{callback} is not callable,
2406 \code{None}, or \NULL, this will return \NULL{} and raise
2407 \exception{TypeError}.
2408 \versionadded{2.2}
2409\end{cfuncdesc}
2410
2411\begin{cfuncdesc}{PyObject*}{PyWeakref_GetObject}{PyObject *ref}
2412 Returns the referenced object from a weak reference, \var{ref}. If
Ka-Ping Yeebd379e92003-03-28 18:07:16 +00002413 the referent is no longer live, returns \code{None}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002414 \versionadded{2.2}
2415\end{cfuncdesc}
2416
2417\begin{cfuncdesc}{PyObject*}{PyWeakref_GET_OBJECT}{PyObject *ref}
2418 Similar to \cfunction{PyWeakref_GetObject()}, but implemented as a
2419 macro that does no error checking.
2420 \versionadded{2.2}
2421\end{cfuncdesc}
2422
2423
2424\subsection{CObjects \label{cObjects}}
2425
2426\obindex{CObject}
2427Refer to \emph{Extending and Embedding the Python Interpreter},
Fred Drake54e62942001-12-11 19:40:16 +00002428section~1.12, ``Providing a C API for an Extension Module,'' for more
Fred Drake3adf79e2001-10-12 19:01:43 +00002429information on using these objects.
2430
2431
2432\begin{ctypedesc}{PyCObject}
2433 This subtype of \ctype{PyObject} represents an opaque value, useful
2434 for C extension modules who need to pass an opaque value (as a
2435 \ctype{void*} pointer) through Python code to other C code. It is
2436 often used to make a C function pointer defined in one module
2437 available to other modules, so the regular import mechanism can be
2438 used to access C APIs defined in dynamically loaded modules.
2439\end{ctypedesc}
2440
2441\begin{cfuncdesc}{int}{PyCObject_Check}{PyObject *p}
2442 Returns true if its argument is a \ctype{PyCObject}.
2443\end{cfuncdesc}
2444
Tim Petersf582b822001-12-11 18:51:08 +00002445\begin{cfuncdesc}{PyObject*}{PyCObject_FromVoidPtr}{void* cobj,
Fred Drake54e62942001-12-11 19:40:16 +00002446 void (*destr)(void *)}
Fred Drake3adf79e2001-10-12 19:01:43 +00002447 Creates a \ctype{PyCObject} from the \code{void *}\var{cobj}. The
2448 \var{destr} function will be called when the object is reclaimed,
2449 unless it is \NULL.
2450\end{cfuncdesc}
2451
2452\begin{cfuncdesc}{PyObject*}{PyCObject_FromVoidPtrAndDesc}{void* cobj,
2453 void* desc, void (*destr)(void *, void *)}
2454 Creates a \ctype{PyCObject} from the \ctype{void *}\var{cobj}. The
2455 \var{destr} function will be called when the object is reclaimed.
2456 The \var{desc} argument can be used to pass extra callback data for
2457 the destructor function.
2458\end{cfuncdesc}
2459
2460\begin{cfuncdesc}{void*}{PyCObject_AsVoidPtr}{PyObject* self}
2461 Returns the object \ctype{void *} that the \ctype{PyCObject}
2462 \var{self} was created with.
2463\end{cfuncdesc}
2464
2465\begin{cfuncdesc}{void*}{PyCObject_GetDesc}{PyObject* self}
2466 Returns the description \ctype{void *} that the \ctype{PyCObject}
2467 \var{self} was created with.
2468\end{cfuncdesc}
Fred Drakecd8474e2001-11-26 21:29:17 +00002469
2470
2471\subsection{Cell Objects \label{cell-objects}}
2472
2473``Cell'' objects are used to implement variables referenced by
2474multiple scopes. For each such variable, a cell object is created to
2475store the value; the local variables of each stack frame that
2476references the value contains a reference to the cells from outer
2477scopes which also use that variable. When the value is accessed, the
2478value contained in the cell is used instead of the cell object
2479itself. This de-referencing of the cell object requires support from
2480the generated byte-code; these are not automatically de-referenced
2481when accessed. Cell objects are not likely to be useful elsewhere.
2482
Fred Drake54e62942001-12-11 19:40:16 +00002483\begin{ctypedesc}{PyCellObject}
2484 The C structure used for cell objects.
2485\end{ctypedesc}
2486
Fred Drakecd8474e2001-11-26 21:29:17 +00002487\begin{cvardesc}{PyTypeObject}{PyCell_Type}
2488 The type object corresponding to cell objects
2489\end{cvardesc}
2490
2491\begin{cfuncdesc}{int}{PyCell_Check}{ob}
2492 Return true if \var{ob} is a cell object; \var{ob} must not be
2493 \NULL.
2494\end{cfuncdesc}
2495
2496\begin{cfuncdesc}{PyObject*}{PyCell_New}{PyObject *ob}
2497 Create and return a new cell object containing the value \var{ob}.
2498 The parameter may be \NULL.
2499\end{cfuncdesc}
2500
2501\begin{cfuncdesc}{PyObject*}{PyCell_Get}{PyObject *cell}
2502 Return the contents of the cell \var{cell}.
2503\end{cfuncdesc}
2504
2505\begin{cfuncdesc}{PyObject*}{PyCell_GET}{PyObject *cell}
2506 Return the contents of the cell \var{cell}, but without checking
2507 that \var{cell} is non-\NULL{} and a call object.
2508\end{cfuncdesc}
2509
2510\begin{cfuncdesc}{int}{PyCell_Set}{PyObject *cell, PyObject *value}
2511 Set the contents of the cell object \var{cell} to \var{value}. This
2512 releases the reference to any current content of the cell.
2513 \var{value} may be \NULL. \var{cell} must be non-\NULL; if it is
2514 not a cell object, \code{-1} will be returned. On success, \code{0}
2515 will be returned.
2516\end{cfuncdesc}
2517
2518\begin{cfuncdesc}{void}{PyCell_SET}{PyObject *cell, PyObject *value}
2519 Sets the value of the cell object \var{cell} to \var{value}. No
2520 reference counts are adjusted, and no checks are made for safety;
2521 \var{cell} must be non-\NULL{} and must be a cell object.
2522\end{cfuncdesc}