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