blob: bf4456a69b9dccc1a884b80bf4a0ae3cf094cc67 [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}
536 Returns a null-terminated representation of the contents of
537 \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})}.
541 It must not be deallocated.
542\end{cfuncdesc}
543
544\begin{cfuncdesc}{char*}{PyString_AS_STRING}{PyObject *string}
545 Macro form of \cfunction{PyString_AsString()} but without error
546 checking.
547\end{cfuncdesc}
548
549\begin{cfuncdesc}{int}{PyString_AsStringAndSize}{PyObject *obj,
550 char **buffer,
551 int *length}
552 Returns a null-terminated representation of the contents of the
553 object \var{obj} through the output variables \var{buffer} and
554 \var{length}.
555
556 The function accepts both string and Unicode objects as input. For
557 Unicode objects it returns the default encoded version of the
558 object. If \var{length} is set to \NULL, the resulting buffer may
559 not contain null characters; if it does, the function returns -1 and
560 a \exception{TypeError} is raised.
561
562 The buffer refers to an internal string buffer of \var{obj}, not a
563 copy. The data must not be modified in any way, unless the string
564 was just created using \code{PyString_FromStringAndSize(NULL,
565 \var{size})}. It must not be deallocated.
566\end{cfuncdesc}
567
568\begin{cfuncdesc}{void}{PyString_Concat}{PyObject **string,
569 PyObject *newpart}
570 Creates a new string object in \var{*string} containing the contents
571 of \var{newpart} appended to \var{string}; the caller will own the
572 new reference. The reference to the old value of \var{string} will
573 be stolen. If the new string cannot be created, the old reference
574 to \var{string} will still be discarded and the value of
575 \var{*string} will be set to \NULL; the appropriate exception will
576 be set.
577\end{cfuncdesc}
578
579\begin{cfuncdesc}{void}{PyString_ConcatAndDel}{PyObject **string,
580 PyObject *newpart}
581 Creates a new string object in \var{*string} containing the contents
582 of \var{newpart} appended to \var{string}. This version decrements
583 the reference count of \var{newpart}.
584\end{cfuncdesc}
585
586\begin{cfuncdesc}{int}{_PyString_Resize}{PyObject **string, int newsize}
587 A way to resize a string object even though it is ``immutable''.
588 Only use this to build up a brand new string object; don't use this
Tim Peters5de98422002-04-27 18:44:32 +0000589 if the string may already be known in other parts of the code. It
590 is an error to call this function if the refcount on the input string
591 object is not one.
592 Pass the address of an existing string object as an lvalue (it may
593 be written into), and the new size desired. On success, \var{*string}
Fred Drake432425e2002-04-29 15:17:16 +0000594 holds the resized string object and \code{0} is returned; the address in
Tim Peters5de98422002-04-27 18:44:32 +0000595 \var{*string} may differ from its input value. If the
596 reallocation fails, the original string object at \var{*string} is
597 deallocated, \var{*string} is set to \NULL{}, a memory exception is set,
Fred Drake432425e2002-04-29 15:17:16 +0000598 and \code{-1} is returned.
Fred Drake3adf79e2001-10-12 19:01:43 +0000599\end{cfuncdesc}
600
601\begin{cfuncdesc}{PyObject*}{PyString_Format}{PyObject *format,
602 PyObject *args}
603 Returns a new string object from \var{format} and \var{args}.
604 Analogous to \code{\var{format} \%\ \var{args}}. The \var{args}
605 argument must be a tuple.
606\end{cfuncdesc}
607
608\begin{cfuncdesc}{void}{PyString_InternInPlace}{PyObject **string}
609 Intern the argument \var{*string} in place. The argument must be
610 the address of a pointer variable pointing to a Python string
611 object. If there is an existing interned string that is the same as
612 \var{*string}, it sets \var{*string} to it (decrementing the
613 reference count of the old string object and incrementing the
614 reference count of the interned string object), otherwise it leaves
615 \var{*string} alone and interns it (incrementing its reference
616 count). (Clarification: even though there is a lot of talk about
617 reference counts, think of this function as reference-count-neutral;
618 you own the object after the call if and only if you owned it before
619 the call.)
620\end{cfuncdesc}
621
622\begin{cfuncdesc}{PyObject*}{PyString_InternFromString}{const char *v}
623 A combination of \cfunction{PyString_FromString()} and
624 \cfunction{PyString_InternInPlace()}, returning either a new string
625 object that has been interned, or a new (``owned'') reference to an
626 earlier interned string object with the same value.
627\end{cfuncdesc}
628
629\begin{cfuncdesc}{PyObject*}{PyString_Decode}{const char *s,
630 int size,
631 const char *encoding,
632 const char *errors}
633 Creates an object by decoding \var{size} bytes of the encoded
634 buffer \var{s} using the codec registered for
635 \var{encoding}. \var{encoding} and \var{errors} have the same
636 meaning as the parameters of the same name in the
637 \function{unicode()} built-in function. The codec to be used is
638 looked up using the Python codec registry. Returns \NULL{} if
639 an exception was raised by the codec.
640\end{cfuncdesc}
641
642\begin{cfuncdesc}{PyObject*}{PyString_AsDecodedObject}{PyObject *str,
643 const char *encoding,
644 const char *errors}
645 Decodes a string object by passing it to the codec registered for
646 \var{encoding} and returns the result as Python
647 object. \var{encoding} and \var{errors} have the same meaning as the
648 parameters of the same name in the string \method{encode()} method.
649 The codec to be used is looked up using the Python codec registry.
650 Returns \NULL{} if an exception was raised by the codec.
651\end{cfuncdesc}
652
653\begin{cfuncdesc}{PyObject*}{PyString_Encode}{const char *s,
654 int size,
655 const char *encoding,
656 const char *errors}
657 Encodes the \ctype{char} buffer of the given size by passing it to
658 the codec registered for \var{encoding} and returns a Python object.
659 \var{encoding} and \var{errors} have the same meaning as the
660 parameters of the same name in the string \method{encode()} method.
661 The codec to be used is looked up using the Python codec
662 registry. Returns \NULL{} if an exception was raised by the
663 codec.
664\end{cfuncdesc}
665
666\begin{cfuncdesc}{PyObject*}{PyString_AsEncodedObject}{PyObject *str,
667 const char *encoding,
668 const char *errors}
669 Encodes a string object using the codec registered for
670 \var{encoding} and returns the result as Python object.
671 \var{encoding} and \var{errors} have the same meaning as the
672 parameters of the same name in the string \method{encode()} method.
673 The codec to be used is looked up using the Python codec registry.
674 Returns \NULL{} if an exception was raised by the codec.
675\end{cfuncdesc}
676
677
678\subsection{Unicode Objects \label{unicodeObjects}}
679\sectionauthor{Marc-Andre Lemburg}{mal@lemburg.com}
680
681%--- Unicode Type -------------------------------------------------------
682
683These are the basic Unicode object types used for the Unicode
684implementation in Python:
685
686\begin{ctypedesc}{Py_UNICODE}
687 This type represents a 16-bit unsigned storage type which is used by
688 Python internally as basis for holding Unicode ordinals. On
689 platforms where \ctype{wchar_t} is available and also has 16-bits,
690 \ctype{Py_UNICODE} is a typedef alias for \ctype{wchar_t} to enhance
691 native platform compatibility. On all other platforms,
692 \ctype{Py_UNICODE} is a typedef alias for \ctype{unsigned short}.
693\end{ctypedesc}
694
695\begin{ctypedesc}{PyUnicodeObject}
696 This subtype of \ctype{PyObject} represents a Python Unicode object.
697\end{ctypedesc}
698
699\begin{cvardesc}{PyTypeObject}{PyUnicode_Type}
700 This instance of \ctype{PyTypeObject} represents the Python Unicode
701 type.
702\end{cvardesc}
703
704The following APIs are really C macros and can be used to do fast
705checks and to access internal read-only data of Unicode objects:
706
707\begin{cfuncdesc}{int}{PyUnicode_Check}{PyObject *o}
708 Returns true if the object \var{o} is a Unicode object or an
709 instance of a Unicode subtype.
710 \versionchanged[Allowed subtypes to be accepted]{2.2}
711\end{cfuncdesc}
712
713\begin{cfuncdesc}{int}{PyUnicode_CheckExact}{PyObject *o}
714 Returns true if the object \var{o} is a Unicode object, but not an
715 instance of a subtype.
716 \versionadded{2.2}
717\end{cfuncdesc}
718
719\begin{cfuncdesc}{int}{PyUnicode_GET_SIZE}{PyObject *o}
720 Returns the size of the object. \var{o} has to be a
721 \ctype{PyUnicodeObject} (not checked).
722\end{cfuncdesc}
723
724\begin{cfuncdesc}{int}{PyUnicode_GET_DATA_SIZE}{PyObject *o}
725 Returns the size of the object's internal buffer in bytes. \var{o}
726 has to be a \ctype{PyUnicodeObject} (not checked).
727\end{cfuncdesc}
728
729\begin{cfuncdesc}{Py_UNICODE*}{PyUnicode_AS_UNICODE}{PyObject *o}
730 Returns a pointer to the internal \ctype{Py_UNICODE} buffer of the
731 object. \var{o} has to be a \ctype{PyUnicodeObject} (not checked).
732\end{cfuncdesc}
733
734\begin{cfuncdesc}{const char*}{PyUnicode_AS_DATA}{PyObject *o}
735 Returns a pointer to the internal buffer of the object.
736 \var{o} has to be a \ctype{PyUnicodeObject} (not checked).
737\end{cfuncdesc}
738
739% --- Unicode character properties ---------------------------------------
740
741Unicode provides many different character properties. The most often
742needed ones are available through these macros which are mapped to C
743functions depending on the Python configuration.
744
745\begin{cfuncdesc}{int}{Py_UNICODE_ISSPACE}{Py_UNICODE ch}
746 Returns 1/0 depending on whether \var{ch} is a whitespace
747 character.
748\end{cfuncdesc}
749
750\begin{cfuncdesc}{int}{Py_UNICODE_ISLOWER}{Py_UNICODE ch}
751 Returns 1/0 depending on whether \var{ch} is a lowercase character.
752\end{cfuncdesc}
753
754\begin{cfuncdesc}{int}{Py_UNICODE_ISUPPER}{Py_UNICODE ch}
755 Returns 1/0 depending on whether \var{ch} is an uppercase
756 character.
757\end{cfuncdesc}
758
759\begin{cfuncdesc}{int}{Py_UNICODE_ISTITLE}{Py_UNICODE ch}
760 Returns 1/0 depending on whether \var{ch} is a titlecase character.
761\end{cfuncdesc}
762
763\begin{cfuncdesc}{int}{Py_UNICODE_ISLINEBREAK}{Py_UNICODE ch}
764 Returns 1/0 depending on whether \var{ch} is a linebreak character.
765\end{cfuncdesc}
766
767\begin{cfuncdesc}{int}{Py_UNICODE_ISDECIMAL}{Py_UNICODE ch}
768 Returns 1/0 depending on whether \var{ch} is a decimal character.
769\end{cfuncdesc}
770
771\begin{cfuncdesc}{int}{Py_UNICODE_ISDIGIT}{Py_UNICODE ch}
772 Returns 1/0 depending on whether \var{ch} is a digit character.
773\end{cfuncdesc}
774
775\begin{cfuncdesc}{int}{Py_UNICODE_ISNUMERIC}{Py_UNICODE ch}
776 Returns 1/0 depending on whether \var{ch} is a numeric character.
777\end{cfuncdesc}
778
779\begin{cfuncdesc}{int}{Py_UNICODE_ISALPHA}{Py_UNICODE ch}
780 Returns 1/0 depending on whether \var{ch} is an alphabetic
781 character.
782\end{cfuncdesc}
783
784\begin{cfuncdesc}{int}{Py_UNICODE_ISALNUM}{Py_UNICODE ch}
785 Returns 1/0 depending on whether \var{ch} is an alphanumeric
786 character.
787\end{cfuncdesc}
788
789These APIs can be used for fast direct character conversions:
790
791\begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOLOWER}{Py_UNICODE ch}
792 Returns the character \var{ch} converted to lower case.
793\end{cfuncdesc}
794
795\begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOUPPER}{Py_UNICODE ch}
796 Returns the character \var{ch} converted to upper case.
797\end{cfuncdesc}
798
799\begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOTITLE}{Py_UNICODE ch}
800 Returns the character \var{ch} converted to title case.
801\end{cfuncdesc}
802
803\begin{cfuncdesc}{int}{Py_UNICODE_TODECIMAL}{Py_UNICODE ch}
804 Returns the character \var{ch} converted to a decimal positive
805 integer. Returns \code{-1} if this is not possible. Does not raise
806 exceptions.
807\end{cfuncdesc}
808
809\begin{cfuncdesc}{int}{Py_UNICODE_TODIGIT}{Py_UNICODE ch}
810 Returns the character \var{ch} converted to a single digit integer.
811 Returns \code{-1} if this is not possible. Does not raise
812 exceptions.
813\end{cfuncdesc}
814
815\begin{cfuncdesc}{double}{Py_UNICODE_TONUMERIC}{Py_UNICODE ch}
816 Returns the character \var{ch} converted to a (positive) double.
817 Returns \code{-1.0} if this is not possible. Does not raise
818 exceptions.
819\end{cfuncdesc}
820
821% --- Plain Py_UNICODE ---------------------------------------------------
822
823To create Unicode objects and access their basic sequence properties,
824use these APIs:
825
826\begin{cfuncdesc}{PyObject*}{PyUnicode_FromUnicode}{const Py_UNICODE *u,
Tim Petersf582b822001-12-11 18:51:08 +0000827 int size}
Fred Drake3adf79e2001-10-12 19:01:43 +0000828 Create a Unicode Object from the Py_UNICODE buffer \var{u} of the
829 given size. \var{u} may be \NULL{} which causes the contents to be
830 undefined. It is the user's responsibility to fill in the needed
831 data. The buffer is copied into the new object. If the buffer is
832 not \NULL, the return value might be a shared object. Therefore,
833 modification of the resulting Unicode object is only allowed when
834 \var{u} is \NULL.
835\end{cfuncdesc}
836
837\begin{cfuncdesc}{Py_UNICODE*}{PyUnicode_AsUnicode}{PyObject *unicode}
838 Return a read-only pointer to the Unicode object's internal
839 \ctype{Py_UNICODE} buffer, \NULL{} if \var{unicode} is not a Unicode
840 object.
841\end{cfuncdesc}
842
843\begin{cfuncdesc}{int}{PyUnicode_GetSize}{PyObject *unicode}
844 Return the length of the Unicode object.
845\end{cfuncdesc}
846
847\begin{cfuncdesc}{PyObject*}{PyUnicode_FromEncodedObject}{PyObject *obj,
848 const char *encoding,
849 const char *errors}
850 Coerce an encoded object \var{obj} to an Unicode object and return a
851 reference with incremented refcount.
852
853 Coercion is done in the following way:
854
855\begin{enumerate}
856\item Unicode objects are passed back as-is with incremented
857 refcount. \note{These cannot be decoded; passing a non-\NULL{}
858 value for encoding will result in a \exception{TypeError}.}
859
860\item String and other char buffer compatible objects are decoded
861 according to the given encoding and using the error handling
862 defined by errors. Both can be \NULL{} to have the interface
863 use the default values (see the next section for details).
864
865\item All other objects cause an exception.
866\end{enumerate}
867
868 The API returns \NULL{} if there was an error. The caller is
869 responsible for decref'ing the returned objects.
870\end{cfuncdesc}
871
872\begin{cfuncdesc}{PyObject*}{PyUnicode_FromObject}{PyObject *obj}
873 Shortcut for \code{PyUnicode_FromEncodedObject(obj, NULL, "strict")}
874 which is used throughout the interpreter whenever coercion to
875 Unicode is needed.
876\end{cfuncdesc}
877
878% --- wchar_t support for platforms which support it ---------------------
879
880If the platform supports \ctype{wchar_t} and provides a header file
881wchar.h, Python can interface directly to this type using the
882following functions. Support is optimized if Python's own
883\ctype{Py_UNICODE} type is identical to the system's \ctype{wchar_t}.
884
885\begin{cfuncdesc}{PyObject*}{PyUnicode_FromWideChar}{const wchar_t *w,
886 int size}
Thomas Heller541703b2002-04-29 17:28:43 +0000887 Create a Unicode object from the \ctype{wchar_t} buffer \var{w} of
Fred Drake3adf79e2001-10-12 19:01:43 +0000888 the given size. Returns \NULL{} on failure.
889\end{cfuncdesc}
890
891\begin{cfuncdesc}{int}{PyUnicode_AsWideChar}{PyUnicodeObject *unicode,
892 wchar_t *w,
893 int size}
Thomas Heller541703b2002-04-29 17:28:43 +0000894 Copies the Unicode object contents into the \ctype{wchar_t} buffer
895 \var{w}. At most \var{size} \ctype{wchar_t} characters are copied.
896 Returns the number of \ctype{wchar_t} characters copied or -1 in
Fred Drake3adf79e2001-10-12 19:01:43 +0000897 case of an error.
898\end{cfuncdesc}
899
900
901\subsubsection{Built-in Codecs \label{builtinCodecs}}
902
903Python provides a set of builtin codecs which are written in C
904for speed. All of these codecs are directly usable via the
905following functions.
906
907Many of the following APIs take two arguments encoding and
908errors. These parameters encoding and errors have the same semantics
909as the ones of the builtin unicode() Unicode object constructor.
910
911Setting encoding to \NULL{} causes the default encoding to be used
912which is \ASCII. The file system calls should use
913\cdata{Py_FileSystemDefaultEncoding} as the encoding for file
914names. This variable should be treated as read-only: On some systems,
915it will be a pointer to a static string, on others, it will change at
916run-time, e.g. when the application invokes setlocale.
917
918Error handling is set by errors which may also be set to \NULL{}
919meaning to use the default handling defined for the codec. Default
920error handling for all builtin codecs is ``strict''
921(\exception{ValueError} is raised).
922
923The codecs all use a similar interface. Only deviation from the
924following generic ones are documented for simplicity.
925
926% --- Generic Codecs -----------------------------------------------------
927
928These are the generic codec APIs:
929
930\begin{cfuncdesc}{PyObject*}{PyUnicode_Decode}{const char *s,
931 int size,
932 const char *encoding,
933 const char *errors}
934 Create a Unicode object by decoding \var{size} bytes of the encoded
935 string \var{s}. \var{encoding} and \var{errors} have the same
936 meaning as the parameters of the same name in the
937 \function{unicode()} builtin function. The codec to be used is
938 looked up using the Python codec registry. Returns \NULL{} if an
939 exception was raised by the codec.
940\end{cfuncdesc}
941
942\begin{cfuncdesc}{PyObject*}{PyUnicode_Encode}{const Py_UNICODE *s,
943 int size,
944 const char *encoding,
945 const char *errors}
946 Encodes the \ctype{Py_UNICODE} buffer of the given size and returns
947 a Python string object. \var{encoding} and \var{errors} have the
948 same meaning as the parameters of the same name in the Unicode
949 \method{encode()} method. The codec to be used is looked up using
950 the Python codec registry. Returns \NULL{} if an exception was
951 raised by the codec.
952\end{cfuncdesc}
953
954\begin{cfuncdesc}{PyObject*}{PyUnicode_AsEncodedString}{PyObject *unicode,
955 const char *encoding,
956 const char *errors}
957 Encodes a Unicode object and returns the result as Python string
958 object. \var{encoding} and \var{errors} have the same meaning as the
959 parameters of the same name in the Unicode \method{encode()} method.
960 The codec to be used is looked up using the Python codec registry.
961 Returns \NULL{} if an exception was raised by the codec.
962\end{cfuncdesc}
963
964% --- UTF-8 Codecs -------------------------------------------------------
965
966These are the UTF-8 codec APIs:
967
968\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF8}{const char *s,
969 int size,
970 const char *errors}
971 Creates a Unicode object by decoding \var{size} bytes of the UTF-8
972 encoded string \var{s}. Returns \NULL{} if an exception was raised
973 by the codec.
974\end{cfuncdesc}
975
976\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUTF8}{const Py_UNICODE *s,
977 int size,
978 const char *errors}
979 Encodes the \ctype{Py_UNICODE} buffer of the given size using UTF-8
980 and returns a Python string object. Returns \NULL{} if an exception
981 was raised by the codec.
982\end{cfuncdesc}
983
984\begin{cfuncdesc}{PyObject*}{PyUnicode_AsUTF8String}{PyObject *unicode}
985 Encodes a Unicode objects using UTF-8 and returns the result as
986 Python string object. Error handling is ``strict''. Returns
987 \NULL{} if an exception was raised by the codec.
988\end{cfuncdesc}
989
990% --- UTF-16 Codecs ------------------------------------------------------ */
991
992These are the UTF-16 codec APIs:
993
994\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF16}{const char *s,
995 int size,
996 const char *errors,
997 int *byteorder}
998 Decodes \var{length} bytes from a UTF-16 encoded buffer string and
999 returns the corresponding Unicode object. \var{errors} (if
1000 non-\NULL) defines the error handling. It defaults to ``strict''.
1001
1002 If \var{byteorder} is non-\NULL, the decoder starts decoding using
1003 the given byte order:
1004
1005\begin{verbatim}
1006 *byteorder == -1: little endian
1007 *byteorder == 0: native order
1008 *byteorder == 1: big endian
1009\end{verbatim}
1010
1011 and then switches according to all byte order marks (BOM) it finds
1012 in the input data. BOMs are not copied into the resulting Unicode
1013 string. After completion, \var{*byteorder} is set to the current
1014 byte order at the end of input data.
1015
1016 If \var{byteorder} is \NULL, the codec starts in native order mode.
1017
1018 Returns \NULL{} if an exception was raised by the codec.
1019\end{cfuncdesc}
1020
1021\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUTF16}{const Py_UNICODE *s,
1022 int size,
1023 const char *errors,
1024 int byteorder}
1025 Returns a Python string object holding the UTF-16 encoded value of
1026 the Unicode data in \var{s}. If \var{byteorder} is not \code{0},
1027 output is written according to the following byte order:
1028
1029\begin{verbatim}
1030 byteorder == -1: little endian
1031 byteorder == 0: native byte order (writes a BOM mark)
1032 byteorder == 1: big endian
1033\end{verbatim}
1034
1035 If byteorder is \code{0}, the output string will always start with
1036 the Unicode BOM mark (U+FEFF). In the other two modes, no BOM mark
1037 is prepended.
1038
1039 Note that \ctype{Py_UNICODE} data is being interpreted as UTF-16
1040 reduced to UCS-2. This trick makes it possible to add full UTF-16
1041 capabilities at a later point without comprimising the APIs.
1042
1043 Returns \NULL{} if an exception was raised by the codec.
1044\end{cfuncdesc}
1045
1046\begin{cfuncdesc}{PyObject*}{PyUnicode_AsUTF16String}{PyObject *unicode}
1047 Returns a Python string using the UTF-16 encoding in native byte
1048 order. The string always starts with a BOM mark. Error handling is
1049 ``strict''. Returns \NULL{} if an exception was raised by the
1050 codec.
1051\end{cfuncdesc}
1052
1053% --- Unicode-Escape Codecs ----------------------------------------------
1054
1055These are the ``Unicode Esacpe'' codec APIs:
1056
1057\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUnicodeEscape}{const char *s,
1058 int size,
1059 const char *errors}
1060 Creates a Unicode object by decoding \var{size} bytes of the
1061 Unicode-Escape encoded string \var{s}. Returns \NULL{} if an
1062 exception was raised by the codec.
1063\end{cfuncdesc}
1064
1065\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUnicodeEscape}{const Py_UNICODE *s,
1066 int size,
1067 const char *errors}
1068 Encodes the \ctype{Py_UNICODE} buffer of the given size using
1069 Unicode-Escape and returns a Python string object. Returns \NULL{}
1070 if an exception was raised by the codec.
1071\end{cfuncdesc}
1072
1073\begin{cfuncdesc}{PyObject*}{PyUnicode_AsUnicodeEscapeString}{PyObject *unicode}
1074 Encodes a Unicode objects using Unicode-Escape and returns the
1075 result as Python string object. Error handling is ``strict''.
1076 Returns \NULL{} if an exception was raised by the codec.
1077\end{cfuncdesc}
1078
1079% --- Raw-Unicode-Escape Codecs ------------------------------------------
1080
1081These are the ``Raw Unicode Esacpe'' codec APIs:
1082
1083\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeRawUnicodeEscape}{const char *s,
1084 int size,
1085 const char *errors}
1086 Creates a Unicode object by decoding \var{size} bytes of the
1087 Raw-Unicode-Esacpe encoded string \var{s}. Returns \NULL{} if an
1088 exception was raised by the codec.
1089\end{cfuncdesc}
1090
1091\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeRawUnicodeEscape}{const Py_UNICODE *s,
1092 int size,
1093 const char *errors}
1094 Encodes the \ctype{Py_UNICODE} buffer of the given size using
1095 Raw-Unicode-Escape and returns a Python string object. Returns
1096 \NULL{} if an exception was raised by the codec.
1097\end{cfuncdesc}
1098
1099\begin{cfuncdesc}{PyObject*}{PyUnicode_AsRawUnicodeEscapeString}{PyObject *unicode}
1100 Encodes a Unicode objects using Raw-Unicode-Escape and returns the
1101 result as Python string object. Error handling is ``strict''.
1102 Returns \NULL{} if an exception was raised by the codec.
1103\end{cfuncdesc}
1104
Tim Petersf582b822001-12-11 18:51:08 +00001105% --- Latin-1 Codecs -----------------------------------------------------
Fred Drake3adf79e2001-10-12 19:01:43 +00001106
1107These are the Latin-1 codec APIs:
1108Latin-1 corresponds to the first 256 Unicode ordinals and only these
1109are accepted by the codecs during encoding.
1110
1111\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeLatin1}{const char *s,
1112 int size,
1113 const char *errors}
1114 Creates a Unicode object by decoding \var{size} bytes of the Latin-1
1115 encoded string \var{s}. Returns \NULL{} if an exception was raised
1116 by the codec.
1117\end{cfuncdesc}
1118
1119\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeLatin1}{const Py_UNICODE *s,
1120 int size,
1121 const char *errors}
1122 Encodes the \ctype{Py_UNICODE} buffer of the given size using
1123 Latin-1 and returns a Python string object. Returns \NULL{} if an
1124 exception was raised by the codec.
1125\end{cfuncdesc}
1126
1127\begin{cfuncdesc}{PyObject*}{PyUnicode_AsLatin1String}{PyObject *unicode}
1128 Encodes a Unicode objects using Latin-1 and returns the result as
1129 Python string object. Error handling is ``strict''. Returns
1130 \NULL{} if an exception was raised by the codec.
1131\end{cfuncdesc}
1132
Tim Petersf582b822001-12-11 18:51:08 +00001133% --- ASCII Codecs -------------------------------------------------------
Fred Drake3adf79e2001-10-12 19:01:43 +00001134
1135These are the \ASCII{} codec APIs. Only 7-bit \ASCII{} data is
1136accepted. All other codes generate errors.
1137
1138\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeASCII}{const char *s,
1139 int size,
1140 const char *errors}
1141 Creates a Unicode object by decoding \var{size} bytes of the
1142 \ASCII{} encoded string \var{s}. Returns \NULL{} if an exception
1143 was raised by the codec.
1144\end{cfuncdesc}
1145
1146\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeASCII}{const Py_UNICODE *s,
1147 int size,
1148 const char *errors}
1149 Encodes the \ctype{Py_UNICODE} buffer of the given size using
1150 \ASCII{} and returns a Python string object. Returns \NULL{} if an
1151 exception was raised by the codec.
1152\end{cfuncdesc}
1153
1154\begin{cfuncdesc}{PyObject*}{PyUnicode_AsASCIIString}{PyObject *unicode}
1155 Encodes a Unicode objects using \ASCII{} and returns the result as
1156 Python string object. Error handling is ``strict''. Returns
1157 \NULL{} if an exception was raised by the codec.
1158\end{cfuncdesc}
1159
Tim Petersf582b822001-12-11 18:51:08 +00001160% --- Character Map Codecs -----------------------------------------------
Fred Drake3adf79e2001-10-12 19:01:43 +00001161
1162These are the mapping codec APIs:
1163
1164This codec is special in that it can be used to implement many
1165different codecs (and this is in fact what was done to obtain most of
1166the standard codecs included in the \module{encodings} package). The
1167codec uses mapping to encode and decode characters.
1168
1169Decoding mappings must map single string characters to single Unicode
1170characters, integers (which are then interpreted as Unicode ordinals)
Tim Petersf582b822001-12-11 18:51:08 +00001171or None (meaning "undefined mapping" and causing an error).
Fred Drake3adf79e2001-10-12 19:01:43 +00001172
1173Encoding mappings must map single Unicode characters to single string
1174characters, integers (which are then interpreted as Latin-1 ordinals)
1175or None (meaning "undefined mapping" and causing an error).
1176
1177The mapping objects provided must only support the __getitem__ mapping
1178interface.
1179
1180If a character lookup fails with a LookupError, the character is
1181copied as-is meaning that its ordinal value will be interpreted as
1182Unicode or Latin-1 ordinal resp. Because of this, mappings only need
1183to contain those mappings which map characters to different code
1184points.
1185
1186\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeCharmap}{const char *s,
1187 int size,
1188 PyObject *mapping,
1189 const char *errors}
1190 Creates a Unicode object by decoding \var{size} bytes of the encoded
1191 string \var{s} using the given \var{mapping} object. Returns
1192 \NULL{} if an exception was raised by the codec.
1193\end{cfuncdesc}
1194
1195\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeCharmap}{const Py_UNICODE *s,
1196 int size,
1197 PyObject *mapping,
1198 const char *errors}
1199 Encodes the \ctype{Py_UNICODE} buffer of the given size using the
1200 given \var{mapping} object and returns a Python string object.
1201 Returns \NULL{} if an exception was raised by the codec.
1202\end{cfuncdesc}
1203
1204\begin{cfuncdesc}{PyObject*}{PyUnicode_AsCharmapString}{PyObject *unicode,
1205 PyObject *mapping}
1206 Encodes a Unicode objects using the given \var{mapping} object and
1207 returns the result as Python string object. Error handling is
1208 ``strict''. Returns \NULL{} if an exception was raised by the
1209 codec.
1210\end{cfuncdesc}
1211
1212The following codec API is special in that maps Unicode to Unicode.
1213
1214\begin{cfuncdesc}{PyObject*}{PyUnicode_TranslateCharmap}{const Py_UNICODE *s,
1215 int size,
1216 PyObject *table,
1217 const char *errors}
1218 Translates a \ctype{Py_UNICODE} buffer of the given length by
1219 applying a character mapping \var{table} to it and returns the
1220 resulting Unicode object. Returns \NULL{} when an exception was
1221 raised by the codec.
1222
1223 The \var{mapping} table must map Unicode ordinal integers to Unicode
1224 ordinal integers or None (causing deletion of the character).
1225
1226 Mapping tables need only provide the method{__getitem__()}
1227 interface; dictionaries and sequences work well. Unmapped character
1228 ordinals (ones which cause a \exception{LookupError}) are left
1229 untouched and are copied as-is.
1230\end{cfuncdesc}
1231
1232% --- MBCS codecs for Windows --------------------------------------------
1233
1234These are the MBCS codec APIs. They are currently only available on
1235Windows and use the Win32 MBCS converters to implement the
1236conversions. Note that MBCS (or DBCS) is a class of encodings, not
1237just one. The target encoding is defined by the user settings on the
1238machine running the codec.
1239
1240\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeMBCS}{const char *s,
1241 int size,
1242 const char *errors}
1243 Creates a Unicode object by decoding \var{size} bytes of the MBCS
1244 encoded string \var{s}. Returns \NULL{} if an exception was
1245 raised by the codec.
1246\end{cfuncdesc}
1247
1248\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeMBCS}{const Py_UNICODE *s,
1249 int size,
1250 const char *errors}
1251 Encodes the \ctype{Py_UNICODE} buffer of the given size using MBCS
1252 and returns a Python string object. Returns \NULL{} if an exception
1253 was raised by the codec.
1254\end{cfuncdesc}
1255
1256\begin{cfuncdesc}{PyObject*}{PyUnicode_AsMBCSString}{PyObject *unicode}
1257 Encodes a Unicode objects using MBCS and returns the result as
1258 Python string object. Error handling is ``strict''. Returns
1259 \NULL{} if an exception was raised by the codec.
1260\end{cfuncdesc}
1261
1262% --- Methods & Slots ----------------------------------------------------
1263
1264\subsubsection{Methods and Slot Functions \label{unicodeMethodsAndSlots}}
1265
1266The following APIs are capable of handling Unicode objects and strings
1267on input (we refer to them as strings in the descriptions) and return
1268Unicode objects or integers as apporpriate.
1269
1270They all return \NULL{} or \code{-1} if an exception occurs.
1271
1272\begin{cfuncdesc}{PyObject*}{PyUnicode_Concat}{PyObject *left,
1273 PyObject *right}
1274 Concat two strings giving a new Unicode string.
1275\end{cfuncdesc}
1276
1277\begin{cfuncdesc}{PyObject*}{PyUnicode_Split}{PyObject *s,
1278 PyObject *sep,
1279 int maxsplit}
1280 Split a string giving a list of Unicode strings. If sep is \NULL,
1281 splitting will be done at all whitespace substrings. Otherwise,
1282 splits occur at the given separator. At most \var{maxsplit} splits
1283 will be done. If negative, no limit is set. Separators are not
1284 included in the resulting list.
1285\end{cfuncdesc}
1286
1287\begin{cfuncdesc}{PyObject*}{PyUnicode_Splitlines}{PyObject *s,
1288 int maxsplit}
1289 Split a Unicode string at line breaks, returning a list of Unicode
1290 strings. CRLF is considered to be one line break. The Line break
1291 characters are not included in the resulting strings.
1292\end{cfuncdesc}
1293
1294\begin{cfuncdesc}{PyObject*}{PyUnicode_Translate}{PyObject *str,
1295 PyObject *table,
1296 const char *errors}
1297 Translate a string by applying a character mapping table to it and
1298 return the resulting Unicode object.
1299
1300 The mapping table must map Unicode ordinal integers to Unicode
1301 ordinal integers or None (causing deletion of the character).
1302
1303 Mapping tables need only provide the \method{__getitem__()}
1304 interface; dictionaries and sequences work well. Unmapped character
1305 ordinals (ones which cause a \exception{LookupError}) are left
1306 untouched and are copied as-is.
1307
1308 \var{errors} has the usual meaning for codecs. It may be \NULL{}
1309 which indicates to use the default error handling.
1310\end{cfuncdesc}
1311
1312\begin{cfuncdesc}{PyObject*}{PyUnicode_Join}{PyObject *separator,
1313 PyObject *seq}
1314 Join a sequence of strings using the given separator and return the
1315 resulting Unicode string.
1316\end{cfuncdesc}
1317
1318\begin{cfuncdesc}{PyObject*}{PyUnicode_Tailmatch}{PyObject *str,
1319 PyObject *substr,
1320 int start,
1321 int end,
1322 int direction}
1323 Return 1 if \var{substr} matches \var{str}[\var{start}:\var{end}] at
1324 the given tail end (\var{direction} == -1 means to do a prefix
1325 match, \var{direction} == 1 a suffix match), 0 otherwise.
1326\end{cfuncdesc}
1327
Fred Drake1d1e1db2002-06-20 22:07:04 +00001328\begin{cfuncdesc}{int}{PyUnicode_Find}{PyObject *str,
1329 PyObject *substr,
1330 int start,
1331 int end,
1332 int direction}
Fred Drake3adf79e2001-10-12 19:01:43 +00001333 Return the first position of \var{substr} in
1334 \var{str}[\var{start}:\var{end}] using the given \var{direction}
1335 (\var{direction} == 1 means to do a forward search,
Fred Drake1d1e1db2002-06-20 22:07:04 +00001336 \var{direction} == -1 a backward search). The return value is the
1337 index of the first match; a value of \code{-1} indicates that no
1338 match was found, and \code{-2} indicates that an error occurred and
1339 an exception has been set.
Fred Drake3adf79e2001-10-12 19:01:43 +00001340\end{cfuncdesc}
1341
Fred Drake1d1e1db2002-06-20 22:07:04 +00001342\begin{cfuncdesc}{int}{PyUnicode_Count}{PyObject *str,
1343 PyObject *substr,
1344 int start,
1345 int end}
1346 Return the number of non-overlapping occurrences of \var{substr} in
1347 \code{\var{str}[\var{start}:\var{end}]}. Returns \code{-1} if an
1348 error occurred.
Fred Drake3adf79e2001-10-12 19:01:43 +00001349\end{cfuncdesc}
1350
1351\begin{cfuncdesc}{PyObject*}{PyUnicode_Replace}{PyObject *str,
1352 PyObject *substr,
1353 PyObject *replstr,
1354 int maxcount}
1355 Replace at most \var{maxcount} occurrences of \var{substr} in
1356 \var{str} with \var{replstr} and return the resulting Unicode object.
1357 \var{maxcount} == -1 means replace all occurrences.
1358\end{cfuncdesc}
1359
1360\begin{cfuncdesc}{int}{PyUnicode_Compare}{PyObject *left, PyObject *right}
1361 Compare two strings and return -1, 0, 1 for less than, equal, and
1362 greater than, respectively.
1363\end{cfuncdesc}
1364
1365\begin{cfuncdesc}{PyObject*}{PyUnicode_Format}{PyObject *format,
1366 PyObject *args}
1367 Returns a new string object from \var{format} and \var{args}; this
1368 is analogous to \code{\var{format} \%\ \var{args}}. The
1369 \var{args} argument must be a tuple.
1370\end{cfuncdesc}
1371
1372\begin{cfuncdesc}{int}{PyUnicode_Contains}{PyObject *container,
1373 PyObject *element}
1374 Checks whether \var{element} is contained in \var{container} and
1375 returns true or false accordingly.
1376
1377 \var{element} has to coerce to a one element Unicode
1378 string. \code{-1} is returned if there was an error.
1379\end{cfuncdesc}
1380
1381
1382\subsection{Buffer Objects \label{bufferObjects}}
1383\sectionauthor{Greg Stein}{gstein@lyra.org}
1384
1385\obindex{buffer}
1386Python objects implemented in C can export a group of functions called
1387the ``buffer\index{buffer interface} interface.'' These functions can
1388be used by an object to expose its data in a raw, byte-oriented
1389format. Clients of the object can use the buffer interface to access
1390the object data directly, without needing to copy it first.
1391
Tim Petersf582b822001-12-11 18:51:08 +00001392Two examples of objects that support
1393the buffer interface are strings and arrays. The string object exposes
Fred Drake3adf79e2001-10-12 19:01:43 +00001394the character contents in the buffer interface's byte-oriented
1395form. An array can also expose its contents, but it should be noted
1396that array elements may be multi-byte values.
1397
1398An example user of the buffer interface is the file object's
1399\method{write()} method. Any object that can export a series of bytes
1400through the buffer interface can be written to a file. There are a
Tim Petersf582b822001-12-11 18:51:08 +00001401number of format codes to \cfunction{PyArg_ParseTuple()} that operate
Fred Drake3adf79e2001-10-12 19:01:43 +00001402against an object's buffer interface, returning data from the target
1403object.
1404
1405More information on the buffer interface is provided in the section
Fred Drake54e62942001-12-11 19:40:16 +00001406``Buffer Object Structures'' (section~\ref{buffer-structs}), under
Fred Drake3adf79e2001-10-12 19:01:43 +00001407the description for \ctype{PyBufferProcs}\ttindex{PyBufferProcs}.
1408
1409A ``buffer object'' is defined in the \file{bufferobject.h} header
1410(included by \file{Python.h}). These objects look very similar to
1411string objects at the Python programming level: they support slicing,
1412indexing, concatenation, and some other standard string
1413operations. However, their data can come from one of two sources: from
1414a block of memory, or from another object which exports the buffer
1415interface.
1416
1417Buffer objects are useful as a way to expose the data from another
1418object's buffer interface to the Python programmer. They can also be
1419used as a zero-copy slicing mechanism. Using their ability to
1420reference a block of memory, it is possible to expose any data to the
1421Python programmer quite easily. The memory could be a large, constant
1422array in a C extension, it could be a raw block of memory for
1423manipulation before passing to an operating system library, or it
1424could be used to pass around structured data in its native, in-memory
1425format.
1426
1427\begin{ctypedesc}{PyBufferObject}
1428 This subtype of \ctype{PyObject} represents a buffer object.
1429\end{ctypedesc}
1430
1431\begin{cvardesc}{PyTypeObject}{PyBuffer_Type}
1432 The instance of \ctype{PyTypeObject} which represents the Python
1433 buffer type; it is the same object as \code{types.BufferType} in the
1434 Python layer.\withsubitem{(in module types)}{\ttindex{BufferType}}.
1435\end{cvardesc}
1436
1437\begin{cvardesc}{int}{Py_END_OF_BUFFER}
1438 This constant may be passed as the \var{size} parameter to
1439 \cfunction{PyBuffer_FromObject()} or
1440 \cfunction{PyBuffer_FromReadWriteObject()}. It indicates that the
1441 new \ctype{PyBufferObject} should refer to \var{base} object from
1442 the specified \var{offset} to the end of its exported buffer. Using
1443 this enables the caller to avoid querying the \var{base} object for
1444 its length.
1445\end{cvardesc}
1446
1447\begin{cfuncdesc}{int}{PyBuffer_Check}{PyObject *p}
1448 Return true if the argument has type \cdata{PyBuffer_Type}.
1449\end{cfuncdesc}
1450
1451\begin{cfuncdesc}{PyObject*}{PyBuffer_FromObject}{PyObject *base,
1452 int offset, int size}
1453 Return a new read-only buffer object. This raises
1454 \exception{TypeError} if \var{base} doesn't support the read-only
1455 buffer protocol or doesn't provide exactly one buffer segment, or it
1456 raises \exception{ValueError} if \var{offset} is less than zero. The
1457 buffer will hold a reference to the \var{base} object, and the
1458 buffer's contents will refer to the \var{base} object's buffer
1459 interface, starting as position \var{offset} and extending for
1460 \var{size} bytes. If \var{size} is \constant{Py_END_OF_BUFFER}, then
1461 the new buffer's contents extend to the length of the \var{base}
1462 object's exported buffer data.
1463\end{cfuncdesc}
1464
1465\begin{cfuncdesc}{PyObject*}{PyBuffer_FromReadWriteObject}{PyObject *base,
1466 int offset,
1467 int size}
1468 Return a new writable buffer object. Parameters and exceptions are
1469 similar to those for \cfunction{PyBuffer_FromObject()}. If the
1470 \var{base} object does not export the writeable buffer protocol,
1471 then \exception{TypeError} is raised.
1472\end{cfuncdesc}
1473
1474\begin{cfuncdesc}{PyObject*}{PyBuffer_FromMemory}{void *ptr, int size}
1475 Return a new read-only buffer object that reads from a specified
1476 location in memory, with a specified size. The caller is
1477 responsible for ensuring that the memory buffer, passed in as
1478 \var{ptr}, is not deallocated while the returned buffer object
1479 exists. Raises \exception{ValueError} if \var{size} is less than
1480 zero. Note that \constant{Py_END_OF_BUFFER} may \emph{not} be
1481 passed for the \var{size} parameter; \exception{ValueError} will be
1482 raised in that case.
1483\end{cfuncdesc}
1484
1485\begin{cfuncdesc}{PyObject*}{PyBuffer_FromReadWriteMemory}{void *ptr, int size}
1486 Similar to \cfunction{PyBuffer_FromMemory()}, but the returned
1487 buffer is writable.
1488\end{cfuncdesc}
1489
1490\begin{cfuncdesc}{PyObject*}{PyBuffer_New}{int size}
1491 Returns a new writable buffer object that maintains its own memory
1492 buffer of \var{size} bytes. \exception{ValueError} is returned if
1493 \var{size} is not zero or positive.
1494\end{cfuncdesc}
1495
1496
1497\subsection{Tuple Objects \label{tupleObjects}}
1498
1499\obindex{tuple}
1500\begin{ctypedesc}{PyTupleObject}
1501 This subtype of \ctype{PyObject} represents a Python tuple object.
1502\end{ctypedesc}
1503
1504\begin{cvardesc}{PyTypeObject}{PyTuple_Type}
1505 This instance of \ctype{PyTypeObject} represents the Python tuple
1506 type; it is the same object as \code{types.TupleType} in the Python
1507 layer.\withsubitem{(in module types)}{\ttindex{TupleType}}.
1508\end{cvardesc}
1509
1510\begin{cfuncdesc}{int}{PyTuple_Check}{PyObject *p}
1511 Return true if \var{p} is a tuple object or an instance of a subtype
1512 of the tuple type.
1513 \versionchanged[Allowed subtypes to be accepted]{2.2}
1514\end{cfuncdesc}
1515
1516\begin{cfuncdesc}{int}{PyTuple_CheckExact}{PyObject *p}
1517 Return true if \var{p} is a tuple object, but not an instance of a
1518 subtype of the tuple type.
1519 \versionadded{2.2}
1520\end{cfuncdesc}
1521
1522\begin{cfuncdesc}{PyObject*}{PyTuple_New}{int len}
1523 Return a new tuple object of size \var{len}, or \NULL{} on failure.
1524\end{cfuncdesc}
1525
1526\begin{cfuncdesc}{int}{PyTuple_Size}{PyObject *p}
1527 Takes a pointer to a tuple object, and returns the size of that
1528 tuple.
1529\end{cfuncdesc}
1530
1531\begin{cfuncdesc}{int}{PyTuple_GET_SIZE}{PyObject *p}
1532 Return the size of the tuple \var{p}, which must be non-\NULL{} and
1533 point to a tuple; no error checking is performed.
1534\end{cfuncdesc}
1535
1536\begin{cfuncdesc}{PyObject*}{PyTuple_GetItem}{PyObject *p, int pos}
1537 Returns the object at position \var{pos} in the tuple pointed to by
1538 \var{p}. If \var{pos} is out of bounds, returns \NULL{} and sets an
1539 \exception{IndexError} exception.
1540\end{cfuncdesc}
1541
1542\begin{cfuncdesc}{PyObject*}{PyTuple_GET_ITEM}{PyObject *p, int pos}
1543 Like \cfunction{PyTuple_GetItem()}, but does no checking of its
1544 arguments.
1545\end{cfuncdesc}
1546
1547\begin{cfuncdesc}{PyObject*}{PyTuple_GetSlice}{PyObject *p,
1548 int low, int high}
1549 Takes a slice of the tuple pointed to by \var{p} from \var{low} to
1550 \var{high} and returns it as a new tuple.
1551\end{cfuncdesc}
1552
1553\begin{cfuncdesc}{int}{PyTuple_SetItem}{PyObject *p,
1554 int pos, PyObject *o}
1555 Inserts a reference to object \var{o} at position \var{pos} of the
1556 tuple pointed to by \var{p}. It returns \code{0} on success.
1557 \note{This function ``steals'' a reference to \var{o}.}
1558\end{cfuncdesc}
1559
1560\begin{cfuncdesc}{void}{PyTuple_SET_ITEM}{PyObject *p,
1561 int pos, PyObject *o}
1562 Like \cfunction{PyTuple_SetItem()}, but does no error checking, and
1563 should \emph{only} be used to fill in brand new tuples. \note{This
1564 function ``steals'' a reference to \var{o}.}
1565\end{cfuncdesc}
1566
1567\begin{cfuncdesc}{int}{_PyTuple_Resize}{PyObject **p, int newsize}
1568 Can be used to resize a tuple. \var{newsize} will be the new length
1569 of the tuple. Because tuples are \emph{supposed} to be immutable,
1570 this should only be used if there is only one reference to the
1571 object. Do \emph{not} use this if the tuple may already be known to
1572 some other part of the code. The tuple will always grow or shrink
1573 at the end. Think of this as destroying the old tuple and creating
1574 a new one, only more efficiently. Returns \code{0} on success.
1575 Client code should never assume that the resulting value of
1576 \code{*\var{p}} will be the same as before calling this function.
1577 If the object referenced by \code{*\var{p}} is replaced, the
1578 original \code{*\var{p}} is destroyed. On failure, returns
1579 \code{-1} and sets \code{*\var{p}} to \NULL, and raises
1580 \exception{MemoryError} or
1581 \exception{SystemError}.
Tim Petersf582b822001-12-11 18:51:08 +00001582 \versionchanged[Removed unused third parameter, \var{last_is_sticky}]{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +00001583\end{cfuncdesc}
1584
1585
1586\subsection{List Objects \label{listObjects}}
1587
1588\obindex{list}
1589\begin{ctypedesc}{PyListObject}
1590 This subtype of \ctype{PyObject} represents a Python list object.
1591\end{ctypedesc}
1592
1593\begin{cvardesc}{PyTypeObject}{PyList_Type}
1594 This instance of \ctype{PyTypeObject} represents the Python list
1595 type. This is the same object as \code{types.ListType}.
1596 \withsubitem{(in module types)}{\ttindex{ListType}}
1597\end{cvardesc}
1598
1599\begin{cfuncdesc}{int}{PyList_Check}{PyObject *p}
1600 Returns true if its argument is a \ctype{PyListObject}.
1601\end{cfuncdesc}
1602
1603\begin{cfuncdesc}{PyObject*}{PyList_New}{int len}
1604 Returns a new list of length \var{len} on success, or \NULL{} on
1605 failure.
1606\end{cfuncdesc}
1607
1608\begin{cfuncdesc}{int}{PyList_Size}{PyObject *list}
1609 Returns the length of the list object in \var{list}; this is
1610 equivalent to \samp{len(\var{list})} on a list object.
1611 \bifuncindex{len}
1612\end{cfuncdesc}
1613
1614\begin{cfuncdesc}{int}{PyList_GET_SIZE}{PyObject *list}
1615 Macro form of \cfunction{PyList_Size()} without error checking.
1616\end{cfuncdesc}
1617
1618\begin{cfuncdesc}{PyObject*}{PyList_GetItem}{PyObject *list, int index}
1619 Returns the object at position \var{pos} in the list pointed to by
1620 \var{p}. If \var{pos} is out of bounds, returns \NULL{} and sets an
1621 \exception{IndexError} exception.
1622\end{cfuncdesc}
1623
1624\begin{cfuncdesc}{PyObject*}{PyList_GET_ITEM}{PyObject *list, int i}
1625 Macro form of \cfunction{PyList_GetItem()} without error checking.
1626\end{cfuncdesc}
1627
1628\begin{cfuncdesc}{int}{PyList_SetItem}{PyObject *list, int index,
1629 PyObject *item}
1630 Sets the item at index \var{index} in list to \var{item}. Returns
1631 \code{0} on success or \code{-1} on failure. \note{This function
1632 ``steals'' a reference to \var{item} and discards a reference to an
1633 item already in the list at the affected position.}
1634\end{cfuncdesc}
1635
1636\begin{cfuncdesc}{void}{PyList_SET_ITEM}{PyObject *list, int i,
1637 PyObject *o}
1638 Macro form of \cfunction{PyList_SetItem()} without error checking.
1639 This is normally only used to fill in new lists where there is no
1640 previous content.
1641 \note{This function ``steals'' a reference to \var{item}, and,
1642 unlike \cfunction{PyList_SetItem()}, does \emph{not} discard a
1643 reference to any item that it being replaced; any reference in
1644 \var{list} at position \var{i} will be leaked.}
1645\end{cfuncdesc}
1646
1647\begin{cfuncdesc}{int}{PyList_Insert}{PyObject *list, int index,
1648 PyObject *item}
1649 Inserts the item \var{item} into list \var{list} in front of index
1650 \var{index}. Returns \code{0} if successful; returns \code{-1} and
1651 raises an exception if unsuccessful. Analogous to
1652 \code{\var{list}.insert(\var{index}, \var{item})}.
1653\end{cfuncdesc}
1654
1655\begin{cfuncdesc}{int}{PyList_Append}{PyObject *list, PyObject *item}
1656 Appends the object \var{item} at the end of list \var{list}.
1657 Returns \code{0} if successful; returns \code{-1} and sets an
1658 exception if unsuccessful. Analogous to
1659 \code{\var{list}.append(\var{item})}.
1660\end{cfuncdesc}
1661
1662\begin{cfuncdesc}{PyObject*}{PyList_GetSlice}{PyObject *list,
1663 int low, int high}
1664 Returns a list of the objects in \var{list} containing the objects
1665 \emph{between} \var{low} and \var{high}. Returns \NULL{} and sets
1666 an exception if unsuccessful.
1667 Analogous to \code{\var{list}[\var{low}:\var{high}]}.
1668\end{cfuncdesc}
1669
1670\begin{cfuncdesc}{int}{PyList_SetSlice}{PyObject *list,
1671 int low, int high,
1672 PyObject *itemlist}
1673 Sets the slice of \var{list} between \var{low} and \var{high} to the
1674 contents of \var{itemlist}. Analogous to
1675 \code{\var{list}[\var{low}:\var{high}] = \var{itemlist}}. Returns
1676 \code{0} on success, \code{-1} on failure.
1677\end{cfuncdesc}
1678
1679\begin{cfuncdesc}{int}{PyList_Sort}{PyObject *list}
1680 Sorts the items of \var{list} in place. Returns \code{0} on
1681 success, \code{-1} on failure. This is equivalent to
1682 \samp{\var{list}.sort()}.
1683\end{cfuncdesc}
1684
1685\begin{cfuncdesc}{int}{PyList_Reverse}{PyObject *list}
1686 Reverses the items of \var{list} in place. Returns \code{0} on
1687 success, \code{-1} on failure. This is the equivalent of
1688 \samp{\var{list}.reverse()}.
1689\end{cfuncdesc}
1690
1691\begin{cfuncdesc}{PyObject*}{PyList_AsTuple}{PyObject *list}
1692 Returns a new tuple object containing the contents of \var{list};
1693 equivalent to \samp{tuple(\var{list})}.\bifuncindex{tuple}
1694\end{cfuncdesc}
1695
1696
1697\section{Mapping Objects \label{mapObjects}}
1698
1699\obindex{mapping}
1700
1701
1702\subsection{Dictionary Objects \label{dictObjects}}
1703
1704\obindex{dictionary}
1705\begin{ctypedesc}{PyDictObject}
1706 This subtype of \ctype{PyObject} represents a Python dictionary
1707 object.
1708\end{ctypedesc}
1709
1710\begin{cvardesc}{PyTypeObject}{PyDict_Type}
1711 This instance of \ctype{PyTypeObject} represents the Python
1712 dictionary type. This is exposed to Python programs as
1713 \code{types.DictType} and \code{types.DictionaryType}.
1714 \withsubitem{(in module types)}{\ttindex{DictType}\ttindex{DictionaryType}}
1715\end{cvardesc}
1716
1717\begin{cfuncdesc}{int}{PyDict_Check}{PyObject *p}
1718 Returns true if its argument is a \ctype{PyDictObject}.
1719\end{cfuncdesc}
1720
1721\begin{cfuncdesc}{PyObject*}{PyDict_New}{}
1722 Returns a new empty dictionary, or \NULL{} on failure.
1723\end{cfuncdesc}
1724
1725\begin{cfuncdesc}{PyObject*}{PyDictProxy_New}{PyObject *dict}
1726 Return a proxy object for a mapping which enforces read-only
1727 behavior. This is normally used to create a proxy to prevent
1728 modification of the dictionary for non-dynamic class types.
1729 \versionadded{2.2}
1730\end{cfuncdesc}
1731
1732\begin{cfuncdesc}{void}{PyDict_Clear}{PyObject *p}
1733 Empties an existing dictionary of all key-value pairs.
1734\end{cfuncdesc}
1735
1736\begin{cfuncdesc}{PyObject*}{PyDict_Copy}{PyObject *p}
1737 Returns a new dictionary that contains the same key-value pairs as
1738 \var{p}.
1739 \versionadded{1.6}
1740\end{cfuncdesc}
1741
1742\begin{cfuncdesc}{int}{PyDict_SetItem}{PyObject *p, PyObject *key,
1743 PyObject *val}
1744 Inserts \var{value} into the dictionary \var{p} with a key of
1745 \var{key}. \var{key} must be hashable; if it isn't,
1746 \exception{TypeError} will be raised.
1747 Returns \code{0} on success or \code{-1} on failure.
1748\end{cfuncdesc}
1749
1750\begin{cfuncdesc}{int}{PyDict_SetItemString}{PyObject *p,
1751 char *key,
1752 PyObject *val}
1753 Inserts \var{value} into the dictionary \var{p} using \var{key} as a
1754 key. \var{key} should be a \ctype{char*}. The key object is created
1755 using \code{PyString_FromString(\var{key})}. Returns \code{0} on
1756 success or \code{-1} on failure.
1757 \ttindex{PyString_FromString()}
1758\end{cfuncdesc}
1759
1760\begin{cfuncdesc}{int}{PyDict_DelItem}{PyObject *p, PyObject *key}
1761 Removes the entry in dictionary \var{p} with key \var{key}.
1762 \var{key} must be hashable; if it isn't, \exception{TypeError} is
Skip Montanaroa23bc422002-01-23 08:18:30 +00001763 raised. Returns \code{0} on success or \code{-1} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00001764\end{cfuncdesc}
1765
1766\begin{cfuncdesc}{int}{PyDict_DelItemString}{PyObject *p, char *key}
1767 Removes the entry in dictionary \var{p} which has a key specified by
1768 the string \var{key}. Returns \code{0} on success or \code{-1} on
1769 failure.
1770\end{cfuncdesc}
1771
1772\begin{cfuncdesc}{PyObject*}{PyDict_GetItem}{PyObject *p, PyObject *key}
1773 Returns the object from dictionary \var{p} which has a key
1774 \var{key}. Returns \NULL{} if the key \var{key} is not present, but
1775 \emph{without} setting an exception.
1776\end{cfuncdesc}
1777
1778\begin{cfuncdesc}{PyObject*}{PyDict_GetItemString}{PyObject *p, char *key}
1779 This is the same as \cfunction{PyDict_GetItem()}, but \var{key} is
1780 specified as a \ctype{char*}, rather than a \ctype{PyObject*}.
1781\end{cfuncdesc}
1782
1783\begin{cfuncdesc}{PyObject*}{PyDict_Items}{PyObject *p}
1784 Returns a \ctype{PyListObject} containing all the items from the
1785 dictionary, as in the dictinoary method \method{items()} (see the
1786 \citetitle[../lib/lib.html]{Python Library Reference}).
1787\end{cfuncdesc}
1788
1789\begin{cfuncdesc}{PyObject*}{PyDict_Keys}{PyObject *p}
1790 Returns a \ctype{PyListObject} containing all the keys from the
1791 dictionary, as in the dictionary method \method{keys()} (see the
1792 \citetitle[../lib/lib.html]{Python Library Reference}).
1793\end{cfuncdesc}
1794
1795\begin{cfuncdesc}{PyObject*}{PyDict_Values}{PyObject *p}
1796 Returns a \ctype{PyListObject} containing all the values from the
1797 dictionary \var{p}, as in the dictionary method \method{values()}
1798 (see the \citetitle[../lib/lib.html]{Python Library Reference}).
1799\end{cfuncdesc}
1800
1801\begin{cfuncdesc}{int}{PyDict_Size}{PyObject *p}
1802 Returns the number of items in the dictionary. This is equivalent
1803 to \samp{len(\var{p})} on a dictionary.\bifuncindex{len}
1804\end{cfuncdesc}
1805
1806\begin{cfuncdesc}{int}{PyDict_Next}{PyObject *p, int *ppos,
1807 PyObject **pkey, PyObject **pvalue}
1808 Iterate over all key-value pairs in the dictionary \var{p}. The
1809 \ctype{int} referred to by \var{ppos} must be initialized to
1810 \code{0} prior to the first call to this function to start the
1811 iteration; the function returns true for each pair in the
1812 dictionary, and false once all pairs have been reported. The
1813 parameters \var{pkey} and \var{pvalue} should either point to
1814 \ctype{PyObject*} variables that will be filled in with each key and
Skip Montanaroea3ceaa2002-01-23 10:54:41 +00001815 value, respectively, or may be \NULL. Any references returned through
1816 them are borrowed.
Fred Drake3adf79e2001-10-12 19:01:43 +00001817
1818 For example:
1819
1820\begin{verbatim}
1821PyObject *key, *value;
1822int pos = 0;
1823
1824while (PyDict_Next(self->dict, &pos, &key, &value)) {
1825 /* do something interesting with the values... */
1826 ...
1827}
1828\end{verbatim}
1829
1830 The dictionary \var{p} should not be mutated during iteration. It
1831 is safe (since Python 2.1) to modify the values of the keys as you
1832 iterate over the dictionary, but only so long as the set of keys
1833 does not change. For example:
1834
1835\begin{verbatim}
1836PyObject *key, *value;
1837int pos = 0;
1838
1839while (PyDict_Next(self->dict, &pos, &key, &value)) {
1840 int i = PyInt_AS_LONG(value) + 1;
1841 PyObject *o = PyInt_FromLong(i);
1842 if (o == NULL)
1843 return -1;
1844 if (PyDict_SetItem(self->dict, key, o) < 0) {
1845 Py_DECREF(o);
1846 return -1;
1847 }
1848 Py_DECREF(o);
1849}
1850\end{verbatim}
1851\end{cfuncdesc}
1852
1853\begin{cfuncdesc}{int}{PyDict_Merge}{PyObject *a, PyObject *b, int override}
Tim Petersf582b822001-12-11 18:51:08 +00001854 Iterate over mapping object \var{b} adding key-value pairs to dictionary
1855 \var{a}.
1856 \var{b} may be a dictionary, or any object supporting
1857 \function{PyMapping_Keys()} and \function{PyObject_GetItem()}.
1858 If \var{override} is true, existing pairs in \var{a} will
Fred Drake3adf79e2001-10-12 19:01:43 +00001859 be replaced if a matching key is found in \var{b}, otherwise pairs
1860 will only be added if there is not a matching key in \var{a}.
Tim Petersf582b822001-12-11 18:51:08 +00001861 Return \code{0} on success or \code{-1} if an exception was
Fred Drake3adf79e2001-10-12 19:01:43 +00001862 raised.
1863\versionadded{2.2}
1864\end{cfuncdesc}
1865
1866\begin{cfuncdesc}{int}{PyDict_Update}{PyObject *a, PyObject *b}
1867 This is the same as \code{PyDict_Merge(\var{a}, \var{b}, 1)} in C,
Tim Petersf582b822001-12-11 18:51:08 +00001868 or \code{\var{a}.update(\var{b})} in Python. Return \code{0} on
Fred Drake3adf79e2001-10-12 19:01:43 +00001869 success or \code{-1} if an exception was raised.
1870 \versionadded{2.2}
1871\end{cfuncdesc}
1872
Tim Petersf582b822001-12-11 18:51:08 +00001873\begin{cfuncdesc}{int}{PyDict_MergeFromSeq2}{PyObject *a, PyObject *seq2,
1874 int override}
1875 Update or merge into dictionary \var{a}, from the key-value pairs in
1876 \var{seq2}. \var{seq2} must be an iterable object producing
1877 iterable objects of length 2, viewed as key-value pairs. In case of
1878 duplicate keys, the last wins if \var{override} is true, else the
1879 first wins.
1880 Return \code{0} on success or \code{-1} if an exception
1881 was raised.
1882 Equivalent Python (except for the return value):
1883
1884\begin{verbatim}
1885def PyDict_MergeFromSeq2(a, seq2, override):
1886 for key, value in seq2:
1887 if override or key not in a:
1888 a[key] = value
1889\end{verbatim}
1890
1891 \versionadded{2.2}
1892\end{cfuncdesc}
Fred Drake3adf79e2001-10-12 19:01:43 +00001893
Fred Drake54e62942001-12-11 19:40:16 +00001894
Fred Drake3adf79e2001-10-12 19:01:43 +00001895\section{Other Objects \label{otherObjects}}
1896
1897\subsection{File Objects \label{fileObjects}}
1898
1899\obindex{file}
1900Python's built-in file objects are implemented entirely on the
1901\ctype{FILE*} support from the C standard library. This is an
1902implementation detail and may change in future releases of Python.
1903
1904\begin{ctypedesc}{PyFileObject}
1905 This subtype of \ctype{PyObject} represents a Python file object.
1906\end{ctypedesc}
1907
1908\begin{cvardesc}{PyTypeObject}{PyFile_Type}
1909 This instance of \ctype{PyTypeObject} represents the Python file
1910 type. This is exposed to Python programs as \code{types.FileType}.
1911 \withsubitem{(in module types)}{\ttindex{FileType}}
1912\end{cvardesc}
1913
1914\begin{cfuncdesc}{int}{PyFile_Check}{PyObject *p}
1915 Returns true if its argument is a \ctype{PyFileObject} or a subtype
1916 of \ctype{PyFileObject}.
1917 \versionchanged[Allowed subtypes to be accepted]{2.2}
1918\end{cfuncdesc}
1919
1920\begin{cfuncdesc}{int}{PyFile_CheckExact}{PyObject *p}
1921 Returns true if its argument is a \ctype{PyFileObject}, but not a
1922 subtype of \ctype{PyFileObject}.
1923 \versionadded{2.2}
1924\end{cfuncdesc}
1925
1926\begin{cfuncdesc}{PyObject*}{PyFile_FromString}{char *filename, char *mode}
1927 On success, returns a new file object that is opened on the file
1928 given by \var{filename}, with a file mode given by \var{mode}, where
1929 \var{mode} has the same semantics as the standard C routine
1930 \cfunction{fopen()}\ttindex{fopen()}. On failure, returns \NULL.
1931\end{cfuncdesc}
1932
1933\begin{cfuncdesc}{PyObject*}{PyFile_FromFile}{FILE *fp,
1934 char *name, char *mode,
1935 int (*close)(FILE*)}
1936 Creates a new \ctype{PyFileObject} from the already-open standard C
1937 file pointer, \var{fp}. The function \var{close} will be called
1938 when the file should be closed. Returns \NULL{} on failure.
1939\end{cfuncdesc}
1940
1941\begin{cfuncdesc}{FILE*}{PyFile_AsFile}{PyFileObject *p}
1942 Returns the file object associated with \var{p} as a \ctype{FILE*}.
1943\end{cfuncdesc}
1944
1945\begin{cfuncdesc}{PyObject*}{PyFile_GetLine}{PyObject *p, int n}
1946 Equivalent to \code{\var{p}.readline(\optional{\var{n}})}, this
1947 function reads one line from the object \var{p}. \var{p} may be a
1948 file object or any object with a \method{readline()} method. If
1949 \var{n} is \code{0}, exactly one line is read, regardless of the
1950 length of the line. If \var{n} is greater than \code{0}, no more
1951 than \var{n} bytes will be read from the file; a partial line can be
1952 returned. In both cases, an empty string is returned if the end of
1953 the file is reached immediately. If \var{n} is less than \code{0},
1954 however, one line is read regardless of length, but
1955 \exception{EOFError} is raised if the end of the file is reached
1956 immediately.
1957 \withsubitem{(built-in exception)}{\ttindex{EOFError}}
1958\end{cfuncdesc}
1959
1960\begin{cfuncdesc}{PyObject*}{PyFile_Name}{PyObject *p}
1961 Returns the name of the file specified by \var{p} as a string
1962 object.
1963\end{cfuncdesc}
1964
1965\begin{cfuncdesc}{void}{PyFile_SetBufSize}{PyFileObject *p, int n}
1966 Available on systems with \cfunction{setvbuf()}\ttindex{setvbuf()}
1967 only. This should only be called immediately after file object
1968 creation.
1969\end{cfuncdesc}
1970
1971\begin{cfuncdesc}{int}{PyFile_SoftSpace}{PyObject *p, int newflag}
1972 This function exists for internal use by the interpreter. Sets the
1973 \member{softspace} attribute of \var{p} to \var{newflag} and
1974 \withsubitem{(file attribute)}{\ttindex{softspace}}returns the
1975 previous value. \var{p} does not have to be a file object for this
1976 function to work properly; any object is supported (thought its only
1977 interesting if the \member{softspace} attribute can be set). This
1978 function clears any errors, and will return \code{0} as the previous
1979 value if the attribute either does not exist or if there were errors
1980 in retrieving it. There is no way to detect errors from this
1981 function, but doing so should not be needed.
1982\end{cfuncdesc}
1983
1984\begin{cfuncdesc}{int}{PyFile_WriteObject}{PyObject *obj, PyFileObject *p,
1985 int flags}
1986 Writes object \var{obj} to file object \var{p}. The only supported
1987 flag for \var{flags} is
1988 \constant{Py_PRINT_RAW}\ttindex{Py_PRINT_RAW}; if given, the
1989 \function{str()} of the object is written instead of the
1990 \function{repr()}. Returns \code{0} on success or \code{-1} on
1991 failure; the appropriate exception will be set.
1992\end{cfuncdesc}
1993
Fred Drake454af892001-11-29 22:42:59 +00001994\begin{cfuncdesc}{int}{PyFile_WriteString}{const char *s, PyFileObject *p}
Fred Drake3adf79e2001-10-12 19:01:43 +00001995 Writes string \var{s} to file object \var{p}. Returns \code{0} on
1996 success or \code{-1} on failure; the appropriate exception will be
1997 set.
1998\end{cfuncdesc}
1999
2000
2001\subsection{Instance Objects \label{instanceObjects}}
2002
2003\obindex{instance}
2004There are very few functions specific to instance objects.
2005
2006\begin{cvardesc}{PyTypeObject}{PyInstance_Type}
2007 Type object for class instances.
2008\end{cvardesc}
2009
2010\begin{cfuncdesc}{int}{PyInstance_Check}{PyObject *obj}
2011 Returns true if \var{obj} is an instance.
2012\end{cfuncdesc}
2013
2014\begin{cfuncdesc}{PyObject*}{PyInstance_New}{PyObject *class,
2015 PyObject *arg,
2016 PyObject *kw}
2017 Create a new instance of a specific class. The parameters \var{arg}
2018 and \var{kw} are used as the positional and keyword parameters to
2019 the object's constructor.
2020\end{cfuncdesc}
2021
2022\begin{cfuncdesc}{PyObject*}{PyInstance_NewRaw}{PyObject *class,
2023 PyObject *dict}
2024 Create a new instance of a specific class without calling it's
2025 constructor. \var{class} is the class of new object. The
2026 \var{dict} parameter will be used as the object's \member{__dict__};
2027 if \NULL, a new dictionary will be created for the instance.
2028\end{cfuncdesc}
2029
2030
2031\subsection{Method Objects \label{method-objects}}
2032
2033\obindex{method}
2034There are some useful functions that are useful for working with
2035method objects.
2036
2037\begin{cvardesc}{PyTypeObject}{PyMethod_Type}
2038 This instance of \ctype{PyTypeObject} represents the Python method
2039 type. This is exposed to Python programs as \code{types.MethodType}.
2040 \withsubitem{(in module types)}{\ttindex{MethodType}}
2041\end{cvardesc}
2042
2043\begin{cfuncdesc}{int}{PyMethod_Check}{PyObject *o}
2044 Return true if \var{o} is a method object (has type
2045 \cdata{PyMethod_Type}). The parameter must not be \NULL.
2046\end{cfuncdesc}
2047
2048\begin{cfuncdesc}{PyObject*}{PyMethod_New}{PyObject *func.
2049 PyObject *self, PyObject *class}
2050 Return a new method object, with \var{func} being any callable
2051 object; this is the function that will be called when the method is
2052 called. If this method should be bound to an instance, \var{self}
2053 should be the instance and \var{class} should be the class of
2054 \var{self}, otherwise \var{self} should be \NULL{} and \var{class}
2055 should be the class which provides the unbound method..
2056\end{cfuncdesc}
2057
2058\begin{cfuncdesc}{PyObject*}{PyMethod_Class}{PyObject *meth}
2059 Return the class object from which the method \var{meth} was
2060 created; if this was created from an instance, it will be the class
2061 of the instance.
2062\end{cfuncdesc}
2063
2064\begin{cfuncdesc}{PyObject*}{PyMethod_GET_CLASS}{PyObject *meth}
2065 Macro version of \cfunction{PyMethod_Class()} which avoids error
2066 checking.
2067\end{cfuncdesc}
2068
2069\begin{cfuncdesc}{PyObject*}{PyMethod_Function}{PyObject *meth}
2070 Return the function object associated with the method \var{meth}.
2071\end{cfuncdesc}
2072
2073\begin{cfuncdesc}{PyObject*}{PyMethod_GET_FUNCTION}{PyObject *meth}
2074 Macro version of \cfunction{PyMethod_Function()} which avoids error
2075 checking.
2076\end{cfuncdesc}
2077
2078\begin{cfuncdesc}{PyObject*}{PyMethod_Self}{PyObject *meth}
2079 Return the instance associated with the method \var{meth} if it is
2080 bound, otherwise return \NULL.
2081\end{cfuncdesc}
2082
2083\begin{cfuncdesc}{PyObject*}{PyMethod_GET_SELF}{PyObject *meth}
2084 Macro version of \cfunction{PyMethod_Self()} which avoids error
2085 checking.
2086\end{cfuncdesc}
2087
2088
2089\subsection{Module Objects \label{moduleObjects}}
2090
2091\obindex{module}
2092There are only a few functions special to module objects.
2093
2094\begin{cvardesc}{PyTypeObject}{PyModule_Type}
2095 This instance of \ctype{PyTypeObject} represents the Python module
2096 type. This is exposed to Python programs as
2097 \code{types.ModuleType}.
2098 \withsubitem{(in module types)}{\ttindex{ModuleType}}
2099\end{cvardesc}
2100
2101\begin{cfuncdesc}{int}{PyModule_Check}{PyObject *p}
2102 Returns true if \var{p} is a module object, or a subtype of a module
2103 object.
2104 \versionchanged[Allowed subtypes to be accepted]{2.2}
2105\end{cfuncdesc}
2106
2107\begin{cfuncdesc}{int}{PyModule_CheckExact}{PyObject *p}
2108 Returns true if \var{p} is a module object, but not a subtype of
2109 \cdata{PyModule_Type}.
2110 \versionadded{2.2}
2111\end{cfuncdesc}
2112
2113\begin{cfuncdesc}{PyObject*}{PyModule_New}{char *name}
2114 Return a new module object with the \member{__name__} attribute set
2115 to \var{name}. Only the module's \member{__doc__} and
2116 \member{__name__} attributes are filled in; the caller is
2117 responsible for providing a \member{__file__} attribute.
2118 \withsubitem{(module attribute)}{
2119 \ttindex{__name__}\ttindex{__doc__}\ttindex{__file__}}
2120\end{cfuncdesc}
2121
2122\begin{cfuncdesc}{PyObject*}{PyModule_GetDict}{PyObject *module}
2123 Return the dictionary object that implements \var{module}'s
2124 namespace; this object is the same as the \member{__dict__}
2125 attribute of the module object. This function never fails.
2126 \withsubitem{(module attribute)}{\ttindex{__dict__}}
Fred Drakef495ef72002-04-12 19:32:07 +00002127 It is recommended extensions use other \cfunction{PyModule_*()}
2128 and \cfunction{PyObject_*()} functions rather than directly
2129 manipulate a module's \member{__dict__}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002130\end{cfuncdesc}
2131
2132\begin{cfuncdesc}{char*}{PyModule_GetName}{PyObject *module}
2133 Return \var{module}'s \member{__name__} value. If the module does
2134 not provide one, or if it is not a string, \exception{SystemError}
2135 is raised and \NULL{} is returned.
2136 \withsubitem{(module attribute)}{\ttindex{__name__}}
2137 \withsubitem{(built-in exception)}{\ttindex{SystemError}}
2138\end{cfuncdesc}
2139
2140\begin{cfuncdesc}{char*}{PyModule_GetFilename}{PyObject *module}
2141 Return the name of the file from which \var{module} was loaded using
2142 \var{module}'s \member{__file__} attribute. If this is not defined,
2143 or if it is not a string, raise \exception{SystemError} and return
2144 \NULL.
2145 \withsubitem{(module attribute)}{\ttindex{__file__}}
2146 \withsubitem{(built-in exception)}{\ttindex{SystemError}}
2147\end{cfuncdesc}
2148
2149\begin{cfuncdesc}{int}{PyModule_AddObject}{PyObject *module,
2150 char *name, PyObject *value}
2151 Add an object to \var{module} as \var{name}. This is a convenience
2152 function which can be used from the module's initialization
2153 function. This steals a reference to \var{value}. Returns
2154 \code{-1} on error, \code{0} on success.
2155 \versionadded{2.0}
2156\end{cfuncdesc}
2157
2158\begin{cfuncdesc}{int}{PyModule_AddIntConstant}{PyObject *module,
2159 char *name, int value}
2160 Add an integer constant to \var{module} as \var{name}. This
2161 convenience function can be used from the module's initialization
2162 function. Returns \code{-1} on error, \code{0} on success.
2163 \versionadded{2.0}
2164\end{cfuncdesc}
2165
2166\begin{cfuncdesc}{int}{PyModule_AddStringConstant}{PyObject *module,
2167 char *name, char *value}
2168 Add a string constant to \var{module} as \var{name}. This
2169 convenience function can be used from the module's initialization
2170 function. The string \var{value} must be null-terminated. Returns
2171 \code{-1} on error, \code{0} on success.
2172 \versionadded{2.0}
2173\end{cfuncdesc}
2174
2175
2176\subsection{Iterator Objects \label{iterator-objects}}
2177
2178Python provides two general-purpose iterator objects. The first, a
2179sequence iterator, works with an arbitrary sequence supporting the
2180\method{__getitem__()} method. The second works with a callable
2181object and a sentinel value, calling the callable for each item in the
2182sequence, and ending the iteration when the sentinel value is
2183returned.
2184
2185\begin{cvardesc}{PyTypeObject}{PySeqIter_Type}
2186 Type object for iterator objects returned by
2187 \cfunction{PySeqIter_New()} and the one-argument form of the
2188 \function{iter()} built-in function for built-in sequence types.
2189 \versionadded{2.2}
2190\end{cvardesc}
2191
2192\begin{cfuncdesc}{int}{PySeqIter_Check}{op}
2193 Return true if the type of \var{op} is \cdata{PySeqIter_Type}.
2194 \versionadded{2.2}
2195\end{cfuncdesc}
2196
2197\begin{cfuncdesc}{PyObject*}{PySeqIter_New}{PyObject *seq}
2198 Return an iterator that works with a general sequence object,
2199 \var{seq}. The iteration ends when the sequence raises
2200 \exception{IndexError} for the subscripting operation.
2201 \versionadded{2.2}
2202\end{cfuncdesc}
2203
2204\begin{cvardesc}{PyTypeObject}{PyCallIter_Type}
2205 Type object for iterator objects returned by
2206 \cfunction{PyCallIter_New()} and the two-argument form of the
2207 \function{iter()} built-in function.
2208 \versionadded{2.2}
2209\end{cvardesc}
2210
2211\begin{cfuncdesc}{int}{PyCallIter_Check}{op}
2212 Return true if the type of \var{op} is \cdata{PyCallIter_Type}.
2213 \versionadded{2.2}
2214\end{cfuncdesc}
2215
2216\begin{cfuncdesc}{PyObject*}{PyCallIter_New}{PyObject *callable,
2217 PyObject *sentinel}
2218 Return a new iterator. The first parameter, \var{callable}, can be
2219 any Python callable object that can be called with no parameters;
2220 each call to it should return the next item in the iteration. When
2221 \var{callable} returns a value equal to \var{sentinel}, the
2222 iteration will be terminated.
2223 \versionadded{2.2}
2224\end{cfuncdesc}
2225
2226
2227\subsection{Descriptor Objects \label{descriptor-objects}}
2228
Fred Drake54e62942001-12-11 19:40:16 +00002229``Descriptors'' are objects that describe some attribute of an object.
2230They are found in the dictionary of type objects.
2231
Fred Drake3adf79e2001-10-12 19:01:43 +00002232\begin{cvardesc}{PyTypeObject}{PyProperty_Type}
Fred Drake54e62942001-12-11 19:40:16 +00002233 The type object for the built-in descriptor types.
Fred Drake3adf79e2001-10-12 19:01:43 +00002234 \versionadded{2.2}
2235\end{cvardesc}
2236
2237\begin{cfuncdesc}{PyObject*}{PyDescr_NewGetSet}{PyTypeObject *type,
2238 PyGetSetDef *getset}
2239 \versionadded{2.2}
2240\end{cfuncdesc}
2241
2242\begin{cfuncdesc}{PyObject*}{PyDescr_NewMember}{PyTypeObject *type,
2243 PyMemberDef *meth}
2244 \versionadded{2.2}
2245\end{cfuncdesc}
2246
2247\begin{cfuncdesc}{PyObject*}{PyDescr_NewMethod}{PyTypeObject *type,
2248 PyMethodDef *meth}
2249 \versionadded{2.2}
2250\end{cfuncdesc}
2251
2252\begin{cfuncdesc}{PyObject*}{PyDescr_NewWrapper}{PyTypeObject *type,
2253 struct wrapperbase *wrapper,
2254 void *wrapped}
2255 \versionadded{2.2}
2256\end{cfuncdesc}
2257
2258\begin{cfuncdesc}{int}{PyDescr_IsData}{PyObject *descr}
2259 Returns true if the descriptor objects \var{descr} describes a data
2260 attribute, or false if it describes a method. \var{descr} must be a
2261 descriptor object; there is no error checking.
2262 \versionadded{2.2}
2263\end{cfuncdesc}
2264
2265\begin{cfuncdesc}{PyObject*}{PyWrapper_New}{PyObject *, PyObject *}
2266 \versionadded{2.2}
2267\end{cfuncdesc}
2268
2269
2270\subsection{Slice Objects \label{slice-objects}}
2271
2272\begin{cvardesc}{PyTypeObject}{PySlice_Type}
2273 The type object for slice objects. This is the same as
2274 \code{types.SliceType}.
2275 \withsubitem{(in module types)}{\ttindex{SliceType}}
2276\end{cvardesc}
2277
2278\begin{cfuncdesc}{int}{PySlice_Check}{PyObject *ob}
2279 Returns true if \var{ob} is a slice object; \var{ob} must not be
2280 \NULL.
2281\end{cfuncdesc}
2282
2283\begin{cfuncdesc}{PyObject*}{PySlice_New}{PyObject *start, PyObject *stop,
2284 PyObject *step}
2285 Return a new slice object with the given values. The \var{start},
2286 \var{stop}, and \var{step} parameters are used as the values of the
2287 slice object attributes of the same names. Any of the values may be
2288 \NULL, in which case the \code{None} will be used for the
2289 corresponding attribute. Returns \NULL{} if the new object could
2290 not be allocated.
2291\end{cfuncdesc}
2292
2293\begin{cfuncdesc}{int}{PySlice_GetIndices}{PySliceObject *slice, int length,
2294 int *start, int *stop, int *step}
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002295Retrieve the start, stop and step indices from the slice object
2296\var{slice}, assuming a sequence of length \var{length}. Treats
2297indices greater than \var{length} as errors.
2298
2299Returns 0 on success and -1 on error with no exception set (unless one
2300of the indices was not \constant{None} and failed to be converted to
2301an integer, in which case -1 is returned with an exception set).
2302
2303You probably do not want to use this function. If you want to use
2304slice objects in versions of Python prior to 2.3, you would probably
2305do well to incorporate the source of \cfunction{PySlice_GetIndicesEx},
2306suitably renamed, in the source of your extension.
2307\end{cfuncdesc}
2308
2309\begin{cfuncdesc}{int}{PySlice_GetIndicesEx}{PySliceObject *slice, int length,
2310 int *start, int *stop, int *step,
2311 int *slicelength}
2312Usable replacement for \cfunction{PySlice_GetIndices}. Retrieve the
2313start, stop, and step indices from the slice object \var{slice}
2314assuming a sequence of length \var{length}, and store the length of
2315the slice in \var{slicelength}. Out of bounds indices are clipped in
2316a manner consistent with the handling of normal slices.
2317
2318Returns 0 on success and -1 on error with exception set.
2319
2320\versionadded{2.3}
Fred Drake3adf79e2001-10-12 19:01:43 +00002321\end{cfuncdesc}
2322
2323
2324\subsection{Weak Reference Objects \label{weakref-objects}}
2325
2326Python supports \emph{weak references} as first-class objects. There
2327are two specific object types which directly implement weak
2328references. The first is a simple reference object, and the second
2329acts as a proxy for the original object as much as it can.
2330
2331\begin{cfuncdesc}{int}{PyWeakref_Check}{ob}
2332 Return true if \var{ob} is either a reference or proxy object.
2333 \versionadded{2.2}
2334\end{cfuncdesc}
2335
2336\begin{cfuncdesc}{int}{PyWeakref_CheckRef}{ob}
2337 Return true if \var{ob} is a reference object.
2338 \versionadded{2.2}
2339\end{cfuncdesc}
2340
2341\begin{cfuncdesc}{int}{PyWeakref_CheckProxy}{ob}
2342 Return true if \var{ob} is a proxy object.
2343 \versionadded{2.2}
2344\end{cfuncdesc}
2345
2346\begin{cfuncdesc}{PyObject*}{PyWeakref_NewRef}{PyObject *ob,
2347 PyObject *callback}
2348 Return a weak reference object for the object \var{ob}. This will
2349 always return a new reference, but is not guaranteed to create a new
2350 object; an existing reference object may be returned. The second
2351 parameter, \var{callback}, can be a callable object that receives
2352 notification when \var{ob} is garbage collected; it should accept a
2353 single paramter, which will be the weak reference object itself.
2354 \var{callback} may also be \code{None} or \NULL. If \var{ob}
2355 is not a weakly-referencable object, or if \var{callback} is not
2356 callable, \code{None}, or \NULL, this will return \NULL{} and
2357 raise \exception{TypeError}.
2358 \versionadded{2.2}
2359\end{cfuncdesc}
2360
2361\begin{cfuncdesc}{PyObject*}{PyWeakref_NewProxy}{PyObject *ob,
2362 PyObject *callback}
2363 Return a weak reference proxy object for the object \var{ob}. This
2364 will always return a new reference, but is not guaranteed to create
2365 a new object; an existing proxy object may be returned. The second
2366 parameter, \var{callback}, can be a callable object that receives
2367 notification when \var{ob} is garbage collected; it should accept a
2368 single paramter, which will be the weak reference object itself.
2369 \var{callback} may also be \code{None} or \NULL. If \var{ob} is not
2370 a weakly-referencable object, or if \var{callback} is not callable,
2371 \code{None}, or \NULL, this will return \NULL{} and raise
2372 \exception{TypeError}.
2373 \versionadded{2.2}
2374\end{cfuncdesc}
2375
2376\begin{cfuncdesc}{PyObject*}{PyWeakref_GetObject}{PyObject *ref}
2377 Returns the referenced object from a weak reference, \var{ref}. If
2378 the referent is no longer live, returns \NULL.
2379 \versionadded{2.2}
2380\end{cfuncdesc}
2381
2382\begin{cfuncdesc}{PyObject*}{PyWeakref_GET_OBJECT}{PyObject *ref}
2383 Similar to \cfunction{PyWeakref_GetObject()}, but implemented as a
2384 macro that does no error checking.
2385 \versionadded{2.2}
2386\end{cfuncdesc}
2387
2388
2389\subsection{CObjects \label{cObjects}}
2390
2391\obindex{CObject}
2392Refer to \emph{Extending and Embedding the Python Interpreter},
Fred Drake54e62942001-12-11 19:40:16 +00002393section~1.12, ``Providing a C API for an Extension Module,'' for more
Fred Drake3adf79e2001-10-12 19:01:43 +00002394information on using these objects.
2395
2396
2397\begin{ctypedesc}{PyCObject}
2398 This subtype of \ctype{PyObject} represents an opaque value, useful
2399 for C extension modules who need to pass an opaque value (as a
2400 \ctype{void*} pointer) through Python code to other C code. It is
2401 often used to make a C function pointer defined in one module
2402 available to other modules, so the regular import mechanism can be
2403 used to access C APIs defined in dynamically loaded modules.
2404\end{ctypedesc}
2405
2406\begin{cfuncdesc}{int}{PyCObject_Check}{PyObject *p}
2407 Returns true if its argument is a \ctype{PyCObject}.
2408\end{cfuncdesc}
2409
Tim Petersf582b822001-12-11 18:51:08 +00002410\begin{cfuncdesc}{PyObject*}{PyCObject_FromVoidPtr}{void* cobj,
Fred Drake54e62942001-12-11 19:40:16 +00002411 void (*destr)(void *)}
Fred Drake3adf79e2001-10-12 19:01:43 +00002412 Creates a \ctype{PyCObject} from the \code{void *}\var{cobj}. The
2413 \var{destr} function will be called when the object is reclaimed,
2414 unless it is \NULL.
2415\end{cfuncdesc}
2416
2417\begin{cfuncdesc}{PyObject*}{PyCObject_FromVoidPtrAndDesc}{void* cobj,
2418 void* desc, void (*destr)(void *, void *)}
2419 Creates a \ctype{PyCObject} from the \ctype{void *}\var{cobj}. The
2420 \var{destr} function will be called when the object is reclaimed.
2421 The \var{desc} argument can be used to pass extra callback data for
2422 the destructor function.
2423\end{cfuncdesc}
2424
2425\begin{cfuncdesc}{void*}{PyCObject_AsVoidPtr}{PyObject* self}
2426 Returns the object \ctype{void *} that the \ctype{PyCObject}
2427 \var{self} was created with.
2428\end{cfuncdesc}
2429
2430\begin{cfuncdesc}{void*}{PyCObject_GetDesc}{PyObject* self}
2431 Returns the description \ctype{void *} that the \ctype{PyCObject}
2432 \var{self} was created with.
2433\end{cfuncdesc}
Fred Drakecd8474e2001-11-26 21:29:17 +00002434
2435
2436\subsection{Cell Objects \label{cell-objects}}
2437
2438``Cell'' objects are used to implement variables referenced by
2439multiple scopes. For each such variable, a cell object is created to
2440store the value; the local variables of each stack frame that
2441references the value contains a reference to the cells from outer
2442scopes which also use that variable. When the value is accessed, the
2443value contained in the cell is used instead of the cell object
2444itself. This de-referencing of the cell object requires support from
2445the generated byte-code; these are not automatically de-referenced
2446when accessed. Cell objects are not likely to be useful elsewhere.
2447
Fred Drake54e62942001-12-11 19:40:16 +00002448\begin{ctypedesc}{PyCellObject}
2449 The C structure used for cell objects.
2450\end{ctypedesc}
2451
Fred Drakecd8474e2001-11-26 21:29:17 +00002452\begin{cvardesc}{PyTypeObject}{PyCell_Type}
2453 The type object corresponding to cell objects
2454\end{cvardesc}
2455
2456\begin{cfuncdesc}{int}{PyCell_Check}{ob}
2457 Return true if \var{ob} is a cell object; \var{ob} must not be
2458 \NULL.
2459\end{cfuncdesc}
2460
2461\begin{cfuncdesc}{PyObject*}{PyCell_New}{PyObject *ob}
2462 Create and return a new cell object containing the value \var{ob}.
2463 The parameter may be \NULL.
2464\end{cfuncdesc}
2465
2466\begin{cfuncdesc}{PyObject*}{PyCell_Get}{PyObject *cell}
2467 Return the contents of the cell \var{cell}.
2468\end{cfuncdesc}
2469
2470\begin{cfuncdesc}{PyObject*}{PyCell_GET}{PyObject *cell}
2471 Return the contents of the cell \var{cell}, but without checking
2472 that \var{cell} is non-\NULL{} and a call object.
2473\end{cfuncdesc}
2474
2475\begin{cfuncdesc}{int}{PyCell_Set}{PyObject *cell, PyObject *value}
2476 Set the contents of the cell object \var{cell} to \var{value}. This
2477 releases the reference to any current content of the cell.
2478 \var{value} may be \NULL. \var{cell} must be non-\NULL; if it is
2479 not a cell object, \code{-1} will be returned. On success, \code{0}
2480 will be returned.
2481\end{cfuncdesc}
2482
2483\begin{cfuncdesc}{void}{PyCell_SET}{PyObject *cell, PyObject *value}
2484 Sets the value of the cell object \var{cell} to \var{value}. No
2485 reference counts are adjusted, and no checks are made for safety;
2486 \var{cell} must be non-\NULL{} and must be a cell object.
2487\end{cfuncdesc}