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