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