blob: 1982bae67fcecbc2e8308d5bb2d51a513a809266 [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}
Georg Brandl99363b62005-09-03 07:27:26 +000039 Return true if the object \var{o} is a type object, including
40 instances of types derived from the standard type object. Return
Fred Drakee3c764b2002-04-10 17:52:52 +000041 false in all other cases.
42\end{cfuncdesc}
43
44\begin{cfuncdesc}{int}{PyType_CheckExact}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +000045 Return true if the object \var{o} is a type object, but not a
46 subtype of the standard type object. Return false in all other
Fred Drakee3c764b2002-04-10 17:52:52 +000047 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}
Georg Brandl99363b62005-09-03 07:27:26 +000052 Return true if the type object \var{o} sets the feature
Fred Drake3adf79e2001-10-12 19:01:43 +000053 \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}
Georg Brandl99363b62005-09-03 07:27:26 +000063 Return true if \var{a} is a subtype of \var{b}.
Fred Drake3adf79e2001-10-12 19:01:43 +000064 \versionadded{2.2}
65\end{cfuncdesc}
66
67\begin{cfuncdesc}{PyObject*}{PyType_GenericAlloc}{PyTypeObject *type,
Martin v. Löwis29fafd82006-03-01 05:16:03 +000068 Py_ssize_t nitems}
Fred Drake3adf79e2001-10-12 19:01:43 +000069 \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
Georg Brandl99363b62005-09-03 07:27:26 +000080 adding inherited slots from a type's base class. Return \code{0}
81 on success, or return \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
Fred Drake7a700b82004-01-01 05:43:53 +000088\obindex{None}
Fred Drake3adf79e2001-10-12 19:01:43 +000089Note 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
Brett Cannon35d83602003-11-09 04:15:30 +0000100\begin{csimplemacrodesc}{Py_RETURN_NONE}
Georg Brandl99363b62005-09-03 07:27:26 +0000101 Properly handle returning \cdata{Py_None} from within a C function.
Brett Cannon35d83602003-11-09 04:15:30 +0000102\end{csimplemacrodesc}
103
Fred Drake3adf79e2001-10-12 19:01:43 +0000104
105\section{Numeric Objects \label{numericObjects}}
106
107\obindex{numeric}
108
109
110\subsection{Plain Integer Objects \label{intObjects}}
111
112\obindex{integer}
113\begin{ctypedesc}{PyIntObject}
114 This subtype of \ctype{PyObject} represents a Python integer
115 object.
116\end{ctypedesc}
117
118\begin{cvardesc}{PyTypeObject}{PyInt_Type}
Tim Petersf582b822001-12-11 18:51:08 +0000119 This instance of \ctype{PyTypeObject} represents the Python plain
Fred Drake3adf79e2001-10-12 19:01:43 +0000120 integer type. This is the same object as \code{types.IntType}.
121 \withsubitem{(in modules types)}{\ttindex{IntType}}
122\end{cvardesc}
123
Andrew M. Kuchling4eb1a002004-08-07 20:19:24 +0000124\begin{cfuncdesc}{int}{PyInt_Check}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000125 Return true if \var{o} is of type \cdata{PyInt_Type} or a subtype
Fred Drake3adf79e2001-10-12 19:01:43 +0000126 of \cdata{PyInt_Type}.
127 \versionchanged[Allowed subtypes to be accepted]{2.2}
128\end{cfuncdesc}
129
Andrew M. Kuchling4eb1a002004-08-07 20:19:24 +0000130\begin{cfuncdesc}{int}{PyInt_CheckExact}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000131 Return true if \var{o} is of type \cdata{PyInt_Type}, but not a
Fred Drake3adf79e2001-10-12 19:01:43 +0000132 subtype of \cdata{PyInt_Type}.
133 \versionadded{2.2}
134\end{cfuncdesc}
135
Skip Montanaro1ff49a72003-02-03 05:13:24 +0000136\begin{cfuncdesc}{PyObject*}{PyInt_FromString}{char *str, char **pend,
137 int base}
138 Return a new \ctype{PyIntObject} or \ctype{PyLongObject} based on the
139 string value in \var{str}, which is interpreted according to the radix in
Tim Peters9ddf40b2004-06-20 22:41:32 +0000140 \var{base}. If \var{pend} is non-\NULL{}, \code{*\var{pend}} will point to
Skip Montanaro1ff49a72003-02-03 05:13:24 +0000141 the first character in \var{str} which follows the representation of the
142 number. If \var{base} is \code{0}, the radix will be determined based on
143 the leading characters of \var{str}: if \var{str} starts with \code{'0x'}
144 or \code{'0X'}, radix 16 will be used; if \var{str} starts with
145 \code{'0'}, radix 8 will be used; otherwise radix 10 will be used. If
146 \var{base} is not \code{0}, it must be between \code{2} and \code{36},
147 inclusive. Leading spaces are ignored. If there are no digits,
148 \exception{ValueError} will be raised. If the string represents a number
149 too large to be contained within the machine's \ctype{long int} type and
150 overflow warnings are being suppressed, a \ctype{PyLongObject} will be
151 returned. If overflow warnings are not being suppressed, \NULL{} will be
152 returned in this case.
153\end{cfuncdesc}
154
Fred Drake3adf79e2001-10-12 19:01:43 +0000155\begin{cfuncdesc}{PyObject*}{PyInt_FromLong}{long ival}
Georg Brandl99363b62005-09-03 07:27:26 +0000156 Create a new integer object with a value of \var{ival}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000157
158 The current implementation keeps an array of integer objects for all
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000159 integers between \code{-5} and \code{256}, when you create an int in
Fred Drake3adf79e2001-10-12 19:01:43 +0000160 that range you actually just get back a reference to the existing
161 object. So it should be possible to change the value of \code{1}. I
162 suspect the behaviour of Python in this case is undefined. :-)
163\end{cfuncdesc}
164
Martin v. Löwis3b197542006-03-01 05:47:11 +0000165\begin{cfuncdesc}{PyObject*}{PyInt_FromSsize_t}{Py_ssize_t ival}
166 Create a new integer object with a value of \var{ival}.
167 If the value exceeds \code{LONG_MAX}, a long integer object is
168 returned.
169
170 \versionadded{2.5}
171\end{cfuncdesc}
172
Fred Drake3adf79e2001-10-12 19:01:43 +0000173\begin{cfuncdesc}{long}{PyInt_AsLong}{PyObject *io}
174 Will first attempt to cast the object to a \ctype{PyIntObject}, if
Martin v. Löwis3b197542006-03-01 05:47:11 +0000175 it is not already one, and then return its value. If there is an
176 error, \code{-1} is returned, and the caller should check
177 \code{PyErr_Occurred()} to find out whether there was an error, or
178 whether the value just happened to be -1.
Fred Drake3adf79e2001-10-12 19:01:43 +0000179\end{cfuncdesc}
180
181\begin{cfuncdesc}{long}{PyInt_AS_LONG}{PyObject *io}
Georg Brandl99363b62005-09-03 07:27:26 +0000182 Return the value of the object \var{io}. No error checking is
Fred Drake3adf79e2001-10-12 19:01:43 +0000183 performed.
184\end{cfuncdesc}
185
Thomas Heller34d7f092003-04-23 19:51:05 +0000186\begin{cfuncdesc}{unsigned long}{PyInt_AsUnsignedLongMask}{PyObject *io}
187 Will first attempt to cast the object to a \ctype{PyIntObject} or
Fred Drakec22b2992003-04-23 20:38:41 +0000188 \ctype{PyLongObject}, if it is not already one, and then return its
Thomas Heller34d7f092003-04-23 19:51:05 +0000189 value as unsigned long. This function does not check for overflow.
190 \versionadded{2.3}
191\end{cfuncdesc}
192
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000193\begin{cfuncdesc}{unsigned PY_LONG_LONG}{PyInt_AsUnsignedLongLongMask}{PyObject *io}
Thomas Heller34d7f092003-04-23 19:51:05 +0000194 Will first attempt to cast the object to a \ctype{PyIntObject} or
Fred Drakec22b2992003-04-23 20:38:41 +0000195 \ctype{PyLongObject}, if it is not already one, and then return its
Thomas Heller34d7f092003-04-23 19:51:05 +0000196 value as unsigned long long, without checking for overflow.
197 \versionadded{2.3}
198\end{cfuncdesc}
199
Martin v. Löwis3b197542006-03-01 05:47:11 +0000200\begin{cfuncdesc}{Py_ssize_t}{PyInt_AsSsize_t}{PyObject *io}
201 Will first attempt to cast the object to a \ctype{PyIntObject} or
202 \ctype{PyLongObject}, if it is not already one, and then return its
203 value as \ctype{Py_ssize_t}.
204 \versionadded{2.5}
205\end{cfuncdesc}
206
Fred Drake3adf79e2001-10-12 19:01:43 +0000207\begin{cfuncdesc}{long}{PyInt_GetMax}{}
Georg Brandl99363b62005-09-03 07:27:26 +0000208 Return the system's idea of the largest integer it can handle
Fred Drake3adf79e2001-10-12 19:01:43 +0000209 (\constant{LONG_MAX}\ttindex{LONG_MAX}, as defined in the system
210 header files).
211\end{cfuncdesc}
212
Fred Drake2be406b2004-08-03 16:02:35 +0000213\subsection{Boolean Objects \label{boolObjects}}
Skip Montanaro33ee76a2004-07-28 14:17:04 +0000214
215Booleans in Python are implemented as a subclass of integers. There
216are only two booleans, \constant{Py_False} and \constant{Py_True}. As
217such, the normal creation and deletion functions don't apply to
218booleans. The following macros are available, however.
219
Andrew M. Kuchling4eb1a002004-08-07 20:19:24 +0000220\begin{cfuncdesc}{int}{PyBool_Check}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000221 Return true if \var{o} is of type \cdata{PyBool_Type}.
Skip Montanaro33ee76a2004-07-28 14:17:04 +0000222 \versionadded{2.3}
223\end{cfuncdesc}
224
Skip Montanaro6d3db702004-07-29 02:16:04 +0000225\begin{cvardesc}{PyObject*}{Py_False}
226 The Python \code{False} object. This object has no methods. It needs to
227 be treated just like any other object with respect to reference counts.
228\end{cvardesc}
Skip Montanaro33ee76a2004-07-28 14:17:04 +0000229
Skip Montanaro6d3db702004-07-29 02:16:04 +0000230\begin{cvardesc}{PyObject*}{Py_True}
231 The Python \code{True} object. This object has no methods. It needs to
232 be treated just like any other object with respect to reference counts.
233\end{cvardesc}
234
235\begin{csimplemacrodesc}{Py_RETURN_FALSE}
236 Return \constant{Py_False} from a function, properly incrementing its
237 reference count.
238\versionadded{2.4}
239\end{csimplemacrodesc}
240
241\begin{csimplemacrodesc}{Py_RETURN_TRUE}
Andrew M. Kuchling4eb1a002004-08-07 20:19:24 +0000242 Return \constant{Py_True} from a function, properly incrementing its
243 reference count.
Skip Montanaro33ee76a2004-07-28 14:17:04 +0000244\versionadded{2.4}
Skip Montanaro6d3db702004-07-29 02:16:04 +0000245\end{csimplemacrodesc}
Skip Montanaro33ee76a2004-07-28 14:17:04 +0000246
Georg Brandl99363b62005-09-03 07:27:26 +0000247\begin{cfuncdesc}{PyObject*}{PyBool_FromLong}{long v}
248 Return a new reference to \constant{Py_True} or \constant{Py_False}
249 depending on the truth value of \var{v}.
Skip Montanaro33ee76a2004-07-28 14:17:04 +0000250\versionadded{2.3}
251\end{cfuncdesc}
Fred Drake3adf79e2001-10-12 19:01:43 +0000252
253\subsection{Long Integer Objects \label{longObjects}}
254
255\obindex{long integer}
256\begin{ctypedesc}{PyLongObject}
257 This subtype of \ctype{PyObject} represents a Python long integer
258 object.
259\end{ctypedesc}
260
261\begin{cvardesc}{PyTypeObject}{PyLong_Type}
262 This instance of \ctype{PyTypeObject} represents the Python long
263 integer type. This is the same object as \code{types.LongType}.
264 \withsubitem{(in modules types)}{\ttindex{LongType}}
265\end{cvardesc}
266
267\begin{cfuncdesc}{int}{PyLong_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +0000268 Return true if its argument is a \ctype{PyLongObject} or a subtype
Fred Drake3adf79e2001-10-12 19:01:43 +0000269 of \ctype{PyLongObject}.
270 \versionchanged[Allowed subtypes to be accepted]{2.2}
271\end{cfuncdesc}
272
273\begin{cfuncdesc}{int}{PyLong_CheckExact}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +0000274 Return true if its argument is a \ctype{PyLongObject}, but not a
Fred Drake3adf79e2001-10-12 19:01:43 +0000275 subtype of \ctype{PyLongObject}.
276 \versionadded{2.2}
277\end{cfuncdesc}
278
279\begin{cfuncdesc}{PyObject*}{PyLong_FromLong}{long v}
Georg Brandl99363b62005-09-03 07:27:26 +0000280 Return a new \ctype{PyLongObject} object from \var{v}, or \NULL{}
Fred Drake3adf79e2001-10-12 19:01:43 +0000281 on failure.
282\end{cfuncdesc}
283
284\begin{cfuncdesc}{PyObject*}{PyLong_FromUnsignedLong}{unsigned long v}
Georg Brandl99363b62005-09-03 07:27:26 +0000285 Return a new \ctype{PyLongObject} object from a C \ctype{unsigned
Fred Drake3adf79e2001-10-12 19:01:43 +0000286 long}, or \NULL{} on failure.
287\end{cfuncdesc}
288
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000289\begin{cfuncdesc}{PyObject*}{PyLong_FromLongLong}{PY_LONG_LONG v}
Georg Brandl99363b62005-09-03 07:27:26 +0000290 Return a new \ctype{PyLongObject} object from a C \ctype{long long},
Fred Drake3adf79e2001-10-12 19:01:43 +0000291 or \NULL{} on failure.
292\end{cfuncdesc}
293
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000294\begin{cfuncdesc}{PyObject*}{PyLong_FromUnsignedLongLong}{unsigned PY_LONG_LONG v}
Georg Brandl99363b62005-09-03 07:27:26 +0000295 Return a new \ctype{PyLongObject} object from a C \ctype{unsigned
Fred Drake3adf79e2001-10-12 19:01:43 +0000296 long long}, or \NULL{} on failure.
297\end{cfuncdesc}
298
299\begin{cfuncdesc}{PyObject*}{PyLong_FromDouble}{double v}
Georg Brandl99363b62005-09-03 07:27:26 +0000300 Return a new \ctype{PyLongObject} object from the integer part of
Fred Drake3adf79e2001-10-12 19:01:43 +0000301 \var{v}, or \NULL{} on failure.
302\end{cfuncdesc}
303
304\begin{cfuncdesc}{PyObject*}{PyLong_FromString}{char *str, char **pend,
305 int base}
306 Return a new \ctype{PyLongObject} based on the string value in
307 \var{str}, which is interpreted according to the radix in
Tim Peters9ddf40b2004-06-20 22:41:32 +0000308 \var{base}. If \var{pend} is non-\NULL{}, \code{*\var{pend}} will
Fred Drake3adf79e2001-10-12 19:01:43 +0000309 point to the first character in \var{str} which follows the
310 representation of the number. If \var{base} is \code{0}, the radix
Skip Montanaro1ff49a72003-02-03 05:13:24 +0000311 will be determined based on the leading characters of \var{str}: if
Fred Drake3adf79e2001-10-12 19:01:43 +0000312 \var{str} starts with \code{'0x'} or \code{'0X'}, radix 16 will be
313 used; if \var{str} starts with \code{'0'}, radix 8 will be used;
314 otherwise radix 10 will be used. If \var{base} is not \code{0}, it
315 must be between \code{2} and \code{36}, inclusive. Leading spaces
316 are ignored. If there are no digits, \exception{ValueError} will be
317 raised.
318\end{cfuncdesc}
319
320\begin{cfuncdesc}{PyObject*}{PyLong_FromUnicode}{Py_UNICODE *u,
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000321 Py_ssize_t length, int base}
Fred Drake3adf79e2001-10-12 19:01:43 +0000322 Convert a sequence of Unicode digits to a Python long integer
323 value. The first parameter, \var{u}, points to the first character
324 of the Unicode string, \var{length} gives the number of characters,
325 and \var{base} is the radix for the conversion. The radix must be
326 in the range [2, 36]; if it is out of range, \exception{ValueError}
327 will be raised.
328 \versionadded{1.6}
329\end{cfuncdesc}
330
331\begin{cfuncdesc}{PyObject*}{PyLong_FromVoidPtr}{void *p}
332 Create a Python integer or long integer from the pointer \var{p}.
333 The pointer value can be retrieved from the resulting value using
334 \cfunction{PyLong_AsVoidPtr()}.
335 \versionadded{1.5.2}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000336 \versionchanged[If the integer is larger than LONG_MAX,
337 a positive long integer is returned]{2.5}
338 \end{cfuncdesc}
Fred Drake3adf79e2001-10-12 19:01:43 +0000339
340\begin{cfuncdesc}{long}{PyLong_AsLong}{PyObject *pylong}
Georg Brandl99363b62005-09-03 07:27:26 +0000341 Return a C \ctype{long} representation of the contents of
Fred Drake3adf79e2001-10-12 19:01:43 +0000342 \var{pylong}. If \var{pylong} is greater than
343 \constant{LONG_MAX}\ttindex{LONG_MAX}, an \exception{OverflowError}
344 is raised.
345 \withsubitem{(built-in exception)}{\ttindex{OverflowError}}
346\end{cfuncdesc}
347
348\begin{cfuncdesc}{unsigned long}{PyLong_AsUnsignedLong}{PyObject *pylong}
Georg Brandl99363b62005-09-03 07:27:26 +0000349 Return a C \ctype{unsigned long} representation of the contents of
Fred Drake3adf79e2001-10-12 19:01:43 +0000350 \var{pylong}. If \var{pylong} is greater than
351 \constant{ULONG_MAX}\ttindex{ULONG_MAX}, an
352 \exception{OverflowError} is raised.
Tim Petersf582b822001-12-11 18:51:08 +0000353 \withsubitem{(built-in exception)}{\ttindex{OverflowError}}
Fred Drake3adf79e2001-10-12 19:01:43 +0000354\end{cfuncdesc}
355
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000356\begin{cfuncdesc}{PY_LONG_LONG}{PyLong_AsLongLong}{PyObject *pylong}
Fred Drake3adf79e2001-10-12 19:01:43 +0000357 Return a C \ctype{long long} from a Python long integer. If
358 \var{pylong} cannot be represented as a \ctype{long long}, an
359 \exception{OverflowError} will be raised.
360 \versionadded{2.2}
361\end{cfuncdesc}
362
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000363\begin{cfuncdesc}{unsigned PY_LONG_LONG}{PyLong_AsUnsignedLongLong}{PyObject
Fred Drake3adf79e2001-10-12 19:01:43 +0000364 *pylong}
365 Return a C \ctype{unsigned long long} from a Python long integer.
366 If \var{pylong} cannot be represented as an \ctype{unsigned long
367 long}, an \exception{OverflowError} will be raised if the value is
368 positive, or a \exception{TypeError} will be raised if the value is
369 negative.
370 \versionadded{2.2}
371\end{cfuncdesc}
372
Thomas Heller34d7f092003-04-23 19:51:05 +0000373\begin{cfuncdesc}{unsigned long}{PyLong_AsUnsignedLongMask}{PyObject *io}
374 Return a C \ctype{unsigned long} from a Python long integer, without
375 checking for overflow.
376 \versionadded{2.3}
377\end{cfuncdesc}
378
379\begin{cfuncdesc}{unsigned long}{PyLong_AsUnsignedLongLongMask}{PyObject *io}
380 Return a C \ctype{unsigned long long} from a Python long integer, without
381 checking for overflow.
382 \versionadded{2.3}
383\end{cfuncdesc}
384
Fred Drake3adf79e2001-10-12 19:01:43 +0000385\begin{cfuncdesc}{double}{PyLong_AsDouble}{PyObject *pylong}
Georg Brandl99363b62005-09-03 07:27:26 +0000386 Return a C \ctype{double} representation of the contents of
Fred Drake3adf79e2001-10-12 19:01:43 +0000387 \var{pylong}. If \var{pylong} cannot be approximately represented
388 as a \ctype{double}, an \exception{OverflowError} exception is
389 raised and \code{-1.0} will be returned.
390\end{cfuncdesc}
391
392\begin{cfuncdesc}{void*}{PyLong_AsVoidPtr}{PyObject *pylong}
393 Convert a Python integer or long integer \var{pylong} to a C
394 \ctype{void} pointer. If \var{pylong} cannot be converted, an
395 \exception{OverflowError} will be raised. This is only assured to
396 produce a usable \ctype{void} pointer for values created with
397 \cfunction{PyLong_FromVoidPtr()}.
398 \versionadded{1.5.2}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000399 \versionchanged[For values outside 0..LONG_MAX, both signed and
400 unsigned integers are acccepted]{2.5}
Fred Drake3adf79e2001-10-12 19:01:43 +0000401\end{cfuncdesc}
402
403
404\subsection{Floating Point Objects \label{floatObjects}}
405
406\obindex{floating point}
407\begin{ctypedesc}{PyFloatObject}
408 This subtype of \ctype{PyObject} represents a Python floating point
409 object.
410\end{ctypedesc}
411
412\begin{cvardesc}{PyTypeObject}{PyFloat_Type}
413 This instance of \ctype{PyTypeObject} represents the Python floating
414 point type. This is the same object as \code{types.FloatType}.
415 \withsubitem{(in modules types)}{\ttindex{FloatType}}
416\end{cvardesc}
417
418\begin{cfuncdesc}{int}{PyFloat_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +0000419 Return true if its argument is a \ctype{PyFloatObject} or a subtype
Fred Drake3adf79e2001-10-12 19:01:43 +0000420 of \ctype{PyFloatObject}.
421 \versionchanged[Allowed subtypes to be accepted]{2.2}
422\end{cfuncdesc}
423
424\begin{cfuncdesc}{int}{PyFloat_CheckExact}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +0000425 Return true if its argument is a \ctype{PyFloatObject}, but not a
Fred Drake3adf79e2001-10-12 19:01:43 +0000426 subtype of \ctype{PyFloatObject}.
427 \versionadded{2.2}
428\end{cfuncdesc}
429
Skip Montanaro1ff49a72003-02-03 05:13:24 +0000430\begin{cfuncdesc}{PyObject*}{PyFloat_FromString}{PyObject *str, char **pend}
Georg Brandl99363b62005-09-03 07:27:26 +0000431 Create a \ctype{PyFloatObject} object based on the string value in
Skip Montanaro1ff49a72003-02-03 05:13:24 +0000432 \var{str}, or \NULL{} on failure. The \var{pend} argument is ignored. It
433 remains only for backward compatibility.
Skip Montanaroae31e9b2003-02-03 03:56:36 +0000434\end{cfuncdesc}
435
Fred Drake3adf79e2001-10-12 19:01:43 +0000436\begin{cfuncdesc}{PyObject*}{PyFloat_FromDouble}{double v}
Georg Brandl99363b62005-09-03 07:27:26 +0000437 Create a \ctype{PyFloatObject} object from \var{v}, or \NULL{} on
Fred Drake3adf79e2001-10-12 19:01:43 +0000438 failure.
439\end{cfuncdesc}
440
441\begin{cfuncdesc}{double}{PyFloat_AsDouble}{PyObject *pyfloat}
Georg Brandl99363b62005-09-03 07:27:26 +0000442 Return a C \ctype{double} representation of the contents of
Fred Drake3adf79e2001-10-12 19:01:43 +0000443 \var{pyfloat}.
444\end{cfuncdesc}
445
446\begin{cfuncdesc}{double}{PyFloat_AS_DOUBLE}{PyObject *pyfloat}
Georg Brandl99363b62005-09-03 07:27:26 +0000447 Return a C \ctype{double} representation of the contents of
Fred Drake3adf79e2001-10-12 19:01:43 +0000448 \var{pyfloat}, but without error checking.
449\end{cfuncdesc}
450
451
452\subsection{Complex Number Objects \label{complexObjects}}
453
454\obindex{complex number}
455Python's complex number objects are implemented as two distinct types
456when viewed from the C API: one is the Python object exposed to
457Python programs, and the other is a C structure which represents the
458actual complex number value. The API provides functions for working
459with both.
460
461\subsubsection{Complex Numbers as C Structures}
462
463Note that the functions which accept these structures as parameters
464and return them as results do so \emph{by value} rather than
465dereferencing them through pointers. This is consistent throughout
466the API.
467
468\begin{ctypedesc}{Py_complex}
469 The C structure which corresponds to the value portion of a Python
470 complex number object. Most of the functions for dealing with
471 complex number objects use structures of this type as input or
472 output values, as appropriate. It is defined as:
473
474\begin{verbatim}
475typedef struct {
476 double real;
477 double imag;
478} Py_complex;
479\end{verbatim}
480\end{ctypedesc}
481
482\begin{cfuncdesc}{Py_complex}{_Py_c_sum}{Py_complex left, Py_complex right}
483 Return the sum of two complex numbers, using the C
484 \ctype{Py_complex} representation.
485\end{cfuncdesc}
486
487\begin{cfuncdesc}{Py_complex}{_Py_c_diff}{Py_complex left, Py_complex right}
488 Return the difference between two complex numbers, using the C
489 \ctype{Py_complex} representation.
490\end{cfuncdesc}
491
492\begin{cfuncdesc}{Py_complex}{_Py_c_neg}{Py_complex complex}
493 Return the negation of the complex number \var{complex}, using the C
494 \ctype{Py_complex} representation.
495\end{cfuncdesc}
496
497\begin{cfuncdesc}{Py_complex}{_Py_c_prod}{Py_complex left, Py_complex right}
498 Return the product of two complex numbers, using the C
499 \ctype{Py_complex} representation.
500\end{cfuncdesc}
501
502\begin{cfuncdesc}{Py_complex}{_Py_c_quot}{Py_complex dividend,
503 Py_complex divisor}
504 Return the quotient of two complex numbers, using the C
505 \ctype{Py_complex} representation.
506\end{cfuncdesc}
507
508\begin{cfuncdesc}{Py_complex}{_Py_c_pow}{Py_complex num, Py_complex exp}
509 Return the exponentiation of \var{num} by \var{exp}, using the C
510 \ctype{Py_complex} representation.
511\end{cfuncdesc}
512
513
514\subsubsection{Complex Numbers as Python Objects}
515
516\begin{ctypedesc}{PyComplexObject}
517 This subtype of \ctype{PyObject} represents a Python complex number
518 object.
519\end{ctypedesc}
520
521\begin{cvardesc}{PyTypeObject}{PyComplex_Type}
522 This instance of \ctype{PyTypeObject} represents the Python complex
523 number type.
524\end{cvardesc}
525
526\begin{cfuncdesc}{int}{PyComplex_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +0000527 Return true if its argument is a \ctype{PyComplexObject} or a
Fred Drake3adf79e2001-10-12 19:01:43 +0000528 subtype of \ctype{PyComplexObject}.
529 \versionchanged[Allowed subtypes to be accepted]{2.2}
530\end{cfuncdesc}
531
532\begin{cfuncdesc}{int}{PyComplex_CheckExact}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +0000533 Return true if its argument is a \ctype{PyComplexObject}, but not a
Fred Drake3adf79e2001-10-12 19:01:43 +0000534 subtype of \ctype{PyComplexObject}.
535 \versionadded{2.2}
536\end{cfuncdesc}
537
538\begin{cfuncdesc}{PyObject*}{PyComplex_FromCComplex}{Py_complex v}
539 Create a new Python complex number object from a C
540 \ctype{Py_complex} value.
541\end{cfuncdesc}
542
543\begin{cfuncdesc}{PyObject*}{PyComplex_FromDoubles}{double real, double imag}
Georg Brandl99363b62005-09-03 07:27:26 +0000544 Return a new \ctype{PyComplexObject} object from \var{real} and
Fred Drake3adf79e2001-10-12 19:01:43 +0000545 \var{imag}.
546\end{cfuncdesc}
547
548\begin{cfuncdesc}{double}{PyComplex_RealAsDouble}{PyObject *op}
Georg Brandl99363b62005-09-03 07:27:26 +0000549 Return the real part of \var{op} as a C \ctype{double}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000550\end{cfuncdesc}
551
552\begin{cfuncdesc}{double}{PyComplex_ImagAsDouble}{PyObject *op}
Georg Brandl99363b62005-09-03 07:27:26 +0000553 Return the imaginary part of \var{op} as a C \ctype{double}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000554\end{cfuncdesc}
555
556\begin{cfuncdesc}{Py_complex}{PyComplex_AsCComplex}{PyObject *op}
Georg Brandl99363b62005-09-03 07:27:26 +0000557 Return the \ctype{Py_complex} value of the complex number
Fred Drake3adf79e2001-10-12 19:01:43 +0000558 \var{op}.
559\end{cfuncdesc}
560
561
562
563\section{Sequence Objects \label{sequenceObjects}}
564
565\obindex{sequence}
Tim Petersf582b822001-12-11 18:51:08 +0000566Generic operations on sequence objects were discussed in the previous
567chapter; this section deals with the specific kinds of sequence
Fred Drake3adf79e2001-10-12 19:01:43 +0000568objects that are intrinsic to the Python language.
569
570
571\subsection{String Objects \label{stringObjects}}
572
573These functions raise \exception{TypeError} when expecting a string
574parameter and are called with a non-string parameter.
575
576\obindex{string}
577\begin{ctypedesc}{PyStringObject}
578 This subtype of \ctype{PyObject} represents a Python string object.
579\end{ctypedesc}
580
581\begin{cvardesc}{PyTypeObject}{PyString_Type}
582 This instance of \ctype{PyTypeObject} represents the Python string
583 type; it is the same object as \code{types.TypeType} in the Python
584 layer.
585 \withsubitem{(in module types)}{\ttindex{StringType}}.
586\end{cvardesc}
587
588\begin{cfuncdesc}{int}{PyString_Check}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000589 Return true if the object \var{o} is a string object or an instance
Fred Drake3adf79e2001-10-12 19:01:43 +0000590 of a subtype of the string type.
591 \versionchanged[Allowed subtypes to be accepted]{2.2}
592\end{cfuncdesc}
593
594\begin{cfuncdesc}{int}{PyString_CheckExact}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000595 Return true if the object \var{o} is a string object, but not an
Fred Drake3adf79e2001-10-12 19:01:43 +0000596 instance of a subtype of the string type.
597 \versionadded{2.2}
598\end{cfuncdesc}
599
600\begin{cfuncdesc}{PyObject*}{PyString_FromString}{const char *v}
Georg Brandl99363b62005-09-03 07:27:26 +0000601 Return a new string object with the value \var{v} on success, and
Tim Peters9ddf40b2004-06-20 22:41:32 +0000602 \NULL{} on failure. The parameter \var{v} must not be \NULL{}; it
Fred Drake32a35872001-12-06 20:38:15 +0000603 will not be checked.
Fred Drake3adf79e2001-10-12 19:01:43 +0000604\end{cfuncdesc}
605
606\begin{cfuncdesc}{PyObject*}{PyString_FromStringAndSize}{const char *v,
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000607 Py_ssize_t len}
Georg Brandl99363b62005-09-03 07:27:26 +0000608 Return a new string object with the value \var{v} and length
Fred Drake3adf79e2001-10-12 19:01:43 +0000609 \var{len} on success, and \NULL{} on failure. If \var{v} is
Tim Peters9ddf40b2004-06-20 22:41:32 +0000610 \NULL{}, the contents of the string are uninitialized.
Fred Drake3adf79e2001-10-12 19:01:43 +0000611\end{cfuncdesc}
612
613\begin{cfuncdesc}{PyObject*}{PyString_FromFormat}{const char *format, ...}
Georg Brandl99363b62005-09-03 07:27:26 +0000614 Take a C \cfunction{printf()}-style \var{format} string and a
615 variable number of arguments, calculate the size of the resulting
616 Python string and return a string with the values formatted into
Fred Drake3adf79e2001-10-12 19:01:43 +0000617 it. The variable arguments must be C types and must correspond
618 exactly to the format characters in the \var{format} string. The
619 following format characters are allowed:
620
621 \begin{tableiii}{l|l|l}{member}{Format Characters}{Type}{Comment}
622 \lineiii{\%\%}{\emph{n/a}}{The literal \% character.}
623 \lineiii{\%c}{int}{A single character, represented as an C int.}
624 \lineiii{\%d}{int}{Exactly equivalent to \code{printf("\%d")}.}
625 \lineiii{\%ld}{long}{Exactly equivalent to \code{printf("\%ld")}.}
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000626 \lineiii{\%zd}{long}{Exactly equivalent to \code{printf("\%zd")}.}
Fred Drake3adf79e2001-10-12 19:01:43 +0000627 \lineiii{\%i}{int}{Exactly equivalent to \code{printf("\%i")}.}
628 \lineiii{\%x}{int}{Exactly equivalent to \code{printf("\%x")}.}
629 \lineiii{\%s}{char*}{A null-terminated C character array.}
630 \lineiii{\%p}{void*}{The hex representation of a C pointer.
631 Mostly equivalent to \code{printf("\%p")} except that it is
632 guaranteed to start with the literal \code{0x} regardless of
633 what the platform's \code{printf} yields.}
634 \end{tableiii}
635\end{cfuncdesc}
636
637\begin{cfuncdesc}{PyObject*}{PyString_FromFormatV}{const char *format,
638 va_list vargs}
639 Identical to \function{PyString_FromFormat()} except that it takes
640 exactly two arguments.
641\end{cfuncdesc}
642
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000643\begin{cfuncdesc}{Py_ssize_t}{PyString_Size}{PyObject *string}
Georg Brandl99363b62005-09-03 07:27:26 +0000644 Return the length of the string in string object \var{string}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000645\end{cfuncdesc}
646
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000647\begin{cfuncdesc}{Py_ssize_t}{PyString_GET_SIZE}{PyObject *string}
Fred Drake3adf79e2001-10-12 19:01:43 +0000648 Macro form of \cfunction{PyString_Size()} but without error
649 checking.
650\end{cfuncdesc}
651
652\begin{cfuncdesc}{char*}{PyString_AsString}{PyObject *string}
Georg Brandl99363b62005-09-03 07:27:26 +0000653 Return a NUL-terminated representation of the contents of
Fred Drake3adf79e2001-10-12 19:01:43 +0000654 \var{string}. The pointer refers to the internal buffer of
655 \var{string}, not a copy. The data must not be modified in any way,
656 unless the string was just created using
657 \code{PyString_FromStringAndSize(NULL, \var{size})}.
Fred Drake4b247262002-10-22 20:20:20 +0000658 It must not be deallocated. If \var{string} is a Unicode object,
659 this function computes the default encoding of \var{string} and
660 operates on that. If \var{string} is not a string object at all,
661 \cfunction{PyString_AsString()} returns \NULL{} and raises
662 \exception{TypeError}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000663\end{cfuncdesc}
664
665\begin{cfuncdesc}{char*}{PyString_AS_STRING}{PyObject *string}
666 Macro form of \cfunction{PyString_AsString()} but without error
Fred Drake4b247262002-10-22 20:20:20 +0000667 checking. Only string objects are supported; no Unicode objects
668 should be passed.
Fred Drake3adf79e2001-10-12 19:01:43 +0000669\end{cfuncdesc}
670
671\begin{cfuncdesc}{int}{PyString_AsStringAndSize}{PyObject *obj,
672 char **buffer,
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000673 Py_ssize_t *length}
Georg Brandl99363b62005-09-03 07:27:26 +0000674 Return a NUL-terminated representation of the contents of the
Fred Drake3adf79e2001-10-12 19:01:43 +0000675 object \var{obj} through the output variables \var{buffer} and
676 \var{length}.
677
678 The function accepts both string and Unicode objects as input. For
679 Unicode objects it returns the default encoded version of the
Tim Peters9ddf40b2004-06-20 22:41:32 +0000680 object. If \var{length} is \NULL{}, the resulting buffer may not
Fred Drake4b247262002-10-22 20:20:20 +0000681 contain NUL characters; if it does, the function returns \code{-1}
682 and a \exception{TypeError} is raised.
Fred Drake3adf79e2001-10-12 19:01:43 +0000683
684 The buffer refers to an internal string buffer of \var{obj}, not a
685 copy. The data must not be modified in any way, unless the string
686 was just created using \code{PyString_FromStringAndSize(NULL,
Fred Drake4b247262002-10-22 20:20:20 +0000687 \var{size})}. It must not be deallocated. If \var{string} is a
688 Unicode object, this function computes the default encoding of
689 \var{string} and operates on that. If \var{string} is not a string
Georg Brandle53475d2005-09-28 12:53:12 +0000690 object at all, \cfunction{PyString_AsStringAndSize()} returns
691 \code{-1} and raises \exception{TypeError}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000692\end{cfuncdesc}
693
694\begin{cfuncdesc}{void}{PyString_Concat}{PyObject **string,
695 PyObject *newpart}
Georg Brandl99363b62005-09-03 07:27:26 +0000696 Create a new string object in \var{*string} containing the contents
Fred Drake3adf79e2001-10-12 19:01:43 +0000697 of \var{newpart} appended to \var{string}; the caller will own the
698 new reference. The reference to the old value of \var{string} will
699 be stolen. If the new string cannot be created, the old reference
700 to \var{string} will still be discarded and the value of
Tim Peters9ddf40b2004-06-20 22:41:32 +0000701 \var{*string} will be set to \NULL{}; the appropriate exception will
Fred Drake3adf79e2001-10-12 19:01:43 +0000702 be set.
703\end{cfuncdesc}
704
705\begin{cfuncdesc}{void}{PyString_ConcatAndDel}{PyObject **string,
706 PyObject *newpart}
Georg Brandl99363b62005-09-03 07:27:26 +0000707 Create a new string object in \var{*string} containing the contents
Fred Drake3adf79e2001-10-12 19:01:43 +0000708 of \var{newpart} appended to \var{string}. This version decrements
709 the reference count of \var{newpart}.
710\end{cfuncdesc}
711
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000712\begin{cfuncdesc}{int}{_PyString_Resize}{PyObject **string, Py_ssize_t newsize}
Fred Drake3adf79e2001-10-12 19:01:43 +0000713 A way to resize a string object even though it is ``immutable''.
714 Only use this to build up a brand new string object; don't use this
Tim Peters5de98422002-04-27 18:44:32 +0000715 if the string may already be known in other parts of the code. It
716 is an error to call this function if the refcount on the input string
717 object is not one.
718 Pass the address of an existing string object as an lvalue (it may
719 be written into), and the new size desired. On success, \var{*string}
Fred Drake432425e2002-04-29 15:17:16 +0000720 holds the resized string object and \code{0} is returned; the address in
Tim Peters5de98422002-04-27 18:44:32 +0000721 \var{*string} may differ from its input value. If the
722 reallocation fails, the original string object at \var{*string} is
723 deallocated, \var{*string} is set to \NULL{}, a memory exception is set,
Fred Drake432425e2002-04-29 15:17:16 +0000724 and \code{-1} is returned.
Fred Drake3adf79e2001-10-12 19:01:43 +0000725\end{cfuncdesc}
726
727\begin{cfuncdesc}{PyObject*}{PyString_Format}{PyObject *format,
728 PyObject *args}
Georg Brandl99363b62005-09-03 07:27:26 +0000729 Return a new string object from \var{format} and \var{args}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000730 Analogous to \code{\var{format} \%\ \var{args}}. The \var{args}
731 argument must be a tuple.
732\end{cfuncdesc}
733
734\begin{cfuncdesc}{void}{PyString_InternInPlace}{PyObject **string}
735 Intern the argument \var{*string} in place. The argument must be
736 the address of a pointer variable pointing to a Python string
737 object. If there is an existing interned string that is the same as
738 \var{*string}, it sets \var{*string} to it (decrementing the
739 reference count of the old string object and incrementing the
740 reference count of the interned string object), otherwise it leaves
741 \var{*string} alone and interns it (incrementing its reference
742 count). (Clarification: even though there is a lot of talk about
743 reference counts, think of this function as reference-count-neutral;
744 you own the object after the call if and only if you owned it before
745 the call.)
746\end{cfuncdesc}
747
748\begin{cfuncdesc}{PyObject*}{PyString_InternFromString}{const char *v}
749 A combination of \cfunction{PyString_FromString()} and
750 \cfunction{PyString_InternInPlace()}, returning either a new string
751 object that has been interned, or a new (``owned'') reference to an
752 earlier interned string object with the same value.
753\end{cfuncdesc}
754
755\begin{cfuncdesc}{PyObject*}{PyString_Decode}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000756 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +0000757 const char *encoding,
758 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +0000759 Create an object by decoding \var{size} bytes of the encoded
Fred Drake3adf79e2001-10-12 19:01:43 +0000760 buffer \var{s} using the codec registered for
761 \var{encoding}. \var{encoding} and \var{errors} have the same
762 meaning as the parameters of the same name in the
763 \function{unicode()} built-in function. The codec to be used is
Georg Brandl99363b62005-09-03 07:27:26 +0000764 looked up using the Python codec registry. Return \NULL{} if
Fred Drake3adf79e2001-10-12 19:01:43 +0000765 an exception was raised by the codec.
766\end{cfuncdesc}
767
768\begin{cfuncdesc}{PyObject*}{PyString_AsDecodedObject}{PyObject *str,
769 const char *encoding,
770 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +0000771 Decode a string object by passing it to the codec registered for
772 \var{encoding} and return the result as Python
Fred Drake3adf79e2001-10-12 19:01:43 +0000773 object. \var{encoding} and \var{errors} have the same meaning as the
774 parameters of the same name in the string \method{encode()} method.
775 The codec to be used is looked up using the Python codec registry.
Georg Brandl99363b62005-09-03 07:27:26 +0000776 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +0000777\end{cfuncdesc}
778
779\begin{cfuncdesc}{PyObject*}{PyString_Encode}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000780 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +0000781 const char *encoding,
782 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +0000783 Encode the \ctype{char} buffer of the given size by passing it to
784 the codec registered for \var{encoding} and return a Python object.
Fred Drake3adf79e2001-10-12 19:01:43 +0000785 \var{encoding} and \var{errors} have the same meaning as the
786 parameters of the same name in the string \method{encode()} method.
787 The codec to be used is looked up using the Python codec
Georg Brandl99363b62005-09-03 07:27:26 +0000788 registry. Return \NULL{} if an exception was raised by the
Fred Drake3adf79e2001-10-12 19:01:43 +0000789 codec.
790\end{cfuncdesc}
791
792\begin{cfuncdesc}{PyObject*}{PyString_AsEncodedObject}{PyObject *str,
793 const char *encoding,
794 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +0000795 Encode a string object using the codec registered for
796 \var{encoding} and return the result as Python object.
Fred Drake3adf79e2001-10-12 19:01:43 +0000797 \var{encoding} and \var{errors} have the same meaning as the
798 parameters of the same name in the string \method{encode()} method.
799 The codec to be used is looked up using the Python codec registry.
Georg Brandl99363b62005-09-03 07:27:26 +0000800 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +0000801\end{cfuncdesc}
802
803
804\subsection{Unicode Objects \label{unicodeObjects}}
805\sectionauthor{Marc-Andre Lemburg}{mal@lemburg.com}
806
807%--- Unicode Type -------------------------------------------------------
808
809These are the basic Unicode object types used for the Unicode
810implementation in Python:
811
812\begin{ctypedesc}{Py_UNICODE}
Marc-André Lemburgdf4f6e92005-10-10 19:08:41 +0000813 This type represents the storage type which is used by Python
814 internally as basis for holding Unicode ordinals. Python's default
815 builds use a 16-bit type for \ctype{Py_UNICODE} and store Unicode
816 values internally as UCS2. It is also possible to build a UCS4
817 version of Python (most recent Linux distributions come with UCS4
818 builds of Python). These builds then use a 32-bit type for
819 \ctype{Py_UNICODE} and store Unicode data internally as UCS4. On
820 platforms where \ctype{wchar_t} is available and compatible with the
821 chosen Python Unicode build variant, \ctype{Py_UNICODE} is a typedef
822 alias for \ctype{wchar_t} to enhance native platform compatibility.
823 On all other platforms, \ctype{Py_UNICODE} is a typedef alias for
824 either \ctype{unsigned short} (UCS2) or \ctype{unsigned long}
825 (UCS4).
Fred Drake3adf79e2001-10-12 19:01:43 +0000826\end{ctypedesc}
827
Marc-André Lemburgdf4f6e92005-10-10 19:08:41 +0000828Note that UCS2 and UCS4 Python builds are not binary compatible.
829Please keep this in mind when writing extensions or interfaces.
830
Fred Drake3adf79e2001-10-12 19:01:43 +0000831\begin{ctypedesc}{PyUnicodeObject}
832 This subtype of \ctype{PyObject} represents a Python Unicode object.
833\end{ctypedesc}
834
835\begin{cvardesc}{PyTypeObject}{PyUnicode_Type}
836 This instance of \ctype{PyTypeObject} represents the Python Unicode
837 type.
838\end{cvardesc}
839
840The following APIs are really C macros and can be used to do fast
841checks and to access internal read-only data of Unicode objects:
842
843\begin{cfuncdesc}{int}{PyUnicode_Check}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000844 Return true if the object \var{o} is a Unicode object or an
Fred Drake3adf79e2001-10-12 19:01:43 +0000845 instance of a Unicode subtype.
846 \versionchanged[Allowed subtypes to be accepted]{2.2}
847\end{cfuncdesc}
848
849\begin{cfuncdesc}{int}{PyUnicode_CheckExact}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000850 Return true if the object \var{o} is a Unicode object, but not an
Fred Drake3adf79e2001-10-12 19:01:43 +0000851 instance of a subtype.
852 \versionadded{2.2}
853\end{cfuncdesc}
854
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000855\begin{cfuncdesc}{Py_ssize_t}{PyUnicode_GET_SIZE}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000856 Return the size of the object. \var{o} has to be a
Fred Drake3adf79e2001-10-12 19:01:43 +0000857 \ctype{PyUnicodeObject} (not checked).
858\end{cfuncdesc}
859
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000860\begin{cfuncdesc}{Py_ssize_t}{PyUnicode_GET_DATA_SIZE}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000861 Return the size of the object's internal buffer in bytes. \var{o}
Fred Drake3adf79e2001-10-12 19:01:43 +0000862 has to be a \ctype{PyUnicodeObject} (not checked).
863\end{cfuncdesc}
864
865\begin{cfuncdesc}{Py_UNICODE*}{PyUnicode_AS_UNICODE}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000866 Return a pointer to the internal \ctype{Py_UNICODE} buffer of the
Fred Drake3adf79e2001-10-12 19:01:43 +0000867 object. \var{o} has to be a \ctype{PyUnicodeObject} (not checked).
868\end{cfuncdesc}
869
870\begin{cfuncdesc}{const char*}{PyUnicode_AS_DATA}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000871 Return a pointer to the internal buffer of the object.
Fred Drake3adf79e2001-10-12 19:01:43 +0000872 \var{o} has to be a \ctype{PyUnicodeObject} (not checked).
873\end{cfuncdesc}
874
875% --- Unicode character properties ---------------------------------------
876
877Unicode provides many different character properties. The most often
878needed ones are available through these macros which are mapped to C
879functions depending on the Python configuration.
880
881\begin{cfuncdesc}{int}{Py_UNICODE_ISSPACE}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000882 Return 1 or 0 depending on whether \var{ch} is a whitespace
Fred Drake3adf79e2001-10-12 19:01:43 +0000883 character.
884\end{cfuncdesc}
885
886\begin{cfuncdesc}{int}{Py_UNICODE_ISLOWER}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000887 Return 1 or 0 depending on whether \var{ch} is a lowercase character.
Fred Drake3adf79e2001-10-12 19:01:43 +0000888\end{cfuncdesc}
889
890\begin{cfuncdesc}{int}{Py_UNICODE_ISUPPER}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000891 Return 1 or 0 depending on whether \var{ch} is an uppercase
Fred Drake3adf79e2001-10-12 19:01:43 +0000892 character.
893\end{cfuncdesc}
894
895\begin{cfuncdesc}{int}{Py_UNICODE_ISTITLE}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000896 Return 1 or 0 depending on whether \var{ch} is a titlecase character.
Fred Drake3adf79e2001-10-12 19:01:43 +0000897\end{cfuncdesc}
898
899\begin{cfuncdesc}{int}{Py_UNICODE_ISLINEBREAK}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000900 Return 1 or 0 depending on whether \var{ch} is a linebreak character.
Fred Drake3adf79e2001-10-12 19:01:43 +0000901\end{cfuncdesc}
902
903\begin{cfuncdesc}{int}{Py_UNICODE_ISDECIMAL}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000904 Return 1 or 0 depending on whether \var{ch} is a decimal character.
Fred Drake3adf79e2001-10-12 19:01:43 +0000905\end{cfuncdesc}
906
907\begin{cfuncdesc}{int}{Py_UNICODE_ISDIGIT}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000908 Return 1 or 0 depending on whether \var{ch} is a digit character.
Fred Drake3adf79e2001-10-12 19:01:43 +0000909\end{cfuncdesc}
910
911\begin{cfuncdesc}{int}{Py_UNICODE_ISNUMERIC}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000912 Return 1 or 0 depending on whether \var{ch} is a numeric character.
Fred Drake3adf79e2001-10-12 19:01:43 +0000913\end{cfuncdesc}
914
915\begin{cfuncdesc}{int}{Py_UNICODE_ISALPHA}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000916 Return 1 or 0 depending on whether \var{ch} is an alphabetic
Fred Drake3adf79e2001-10-12 19:01:43 +0000917 character.
918\end{cfuncdesc}
919
920\begin{cfuncdesc}{int}{Py_UNICODE_ISALNUM}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000921 Return 1 or 0 depending on whether \var{ch} is an alphanumeric
Fred Drake3adf79e2001-10-12 19:01:43 +0000922 character.
923\end{cfuncdesc}
924
925These APIs can be used for fast direct character conversions:
926
927\begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOLOWER}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000928 Return the character \var{ch} converted to lower case.
Fred Drake3adf79e2001-10-12 19:01:43 +0000929\end{cfuncdesc}
930
931\begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOUPPER}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000932 Return the character \var{ch} converted to upper case.
Fred Drake3adf79e2001-10-12 19:01:43 +0000933\end{cfuncdesc}
934
935\begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOTITLE}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000936 Return the character \var{ch} converted to title case.
Fred Drake3adf79e2001-10-12 19:01:43 +0000937\end{cfuncdesc}
938
939\begin{cfuncdesc}{int}{Py_UNICODE_TODECIMAL}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000940 Return the character \var{ch} converted to a decimal positive
941 integer. Return \code{-1} if this is not possible. This macro
942 does not raise exceptions.
Fred Drake3adf79e2001-10-12 19:01:43 +0000943\end{cfuncdesc}
944
945\begin{cfuncdesc}{int}{Py_UNICODE_TODIGIT}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000946 Return the character \var{ch} converted to a single digit integer.
947 Return \code{-1} if this is not possible. This macro does not raise
Fred Drake3adf79e2001-10-12 19:01:43 +0000948 exceptions.
949\end{cfuncdesc}
950
951\begin{cfuncdesc}{double}{Py_UNICODE_TONUMERIC}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000952 Return the character \var{ch} converted to a (positive) double.
953 Return \code{-1.0} if this is not possible. This macro does not raise
Fred Drake3adf79e2001-10-12 19:01:43 +0000954 exceptions.
955\end{cfuncdesc}
956
957% --- Plain Py_UNICODE ---------------------------------------------------
958
959To create Unicode objects and access their basic sequence properties,
960use these APIs:
961
962\begin{cfuncdesc}{PyObject*}{PyUnicode_FromUnicode}{const Py_UNICODE *u,
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000963 Py_ssize_t size}
Fred Drake3adf79e2001-10-12 19:01:43 +0000964 Create a Unicode Object from the Py_UNICODE buffer \var{u} of the
965 given size. \var{u} may be \NULL{} which causes the contents to be
966 undefined. It is the user's responsibility to fill in the needed
967 data. The buffer is copied into the new object. If the buffer is
Tim Peters9ddf40b2004-06-20 22:41:32 +0000968 not \NULL{}, the return value might be a shared object. Therefore,
Fred Drake3adf79e2001-10-12 19:01:43 +0000969 modification of the resulting Unicode object is only allowed when
Tim Peters9ddf40b2004-06-20 22:41:32 +0000970 \var{u} is \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000971\end{cfuncdesc}
972
973\begin{cfuncdesc}{Py_UNICODE*}{PyUnicode_AsUnicode}{PyObject *unicode}
974 Return a read-only pointer to the Unicode object's internal
975 \ctype{Py_UNICODE} buffer, \NULL{} if \var{unicode} is not a Unicode
976 object.
977\end{cfuncdesc}
978
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000979\begin{cfuncdesc}{Py_ssize_t}{PyUnicode_GetSize}{PyObject *unicode}
Fred Drake3adf79e2001-10-12 19:01:43 +0000980 Return the length of the Unicode object.
981\end{cfuncdesc}
982
983\begin{cfuncdesc}{PyObject*}{PyUnicode_FromEncodedObject}{PyObject *obj,
984 const char *encoding,
985 const char *errors}
986 Coerce an encoded object \var{obj} to an Unicode object and return a
987 reference with incremented refcount.
988
989 Coercion is done in the following way:
990
991\begin{enumerate}
992\item Unicode objects are passed back as-is with incremented
993 refcount. \note{These cannot be decoded; passing a non-\NULL{}
994 value for encoding will result in a \exception{TypeError}.}
995
996\item String and other char buffer compatible objects are decoded
997 according to the given encoding and using the error handling
998 defined by errors. Both can be \NULL{} to have the interface
999 use the default values (see the next section for details).
1000
1001\item All other objects cause an exception.
1002\end{enumerate}
1003
1004 The API returns \NULL{} if there was an error. The caller is
1005 responsible for decref'ing the returned objects.
1006\end{cfuncdesc}
1007
1008\begin{cfuncdesc}{PyObject*}{PyUnicode_FromObject}{PyObject *obj}
1009 Shortcut for \code{PyUnicode_FromEncodedObject(obj, NULL, "strict")}
1010 which is used throughout the interpreter whenever coercion to
1011 Unicode is needed.
1012\end{cfuncdesc}
1013
1014% --- wchar_t support for platforms which support it ---------------------
1015
1016If the platform supports \ctype{wchar_t} and provides a header file
1017wchar.h, Python can interface directly to this type using the
1018following functions. Support is optimized if Python's own
1019\ctype{Py_UNICODE} type is identical to the system's \ctype{wchar_t}.
1020
1021\begin{cfuncdesc}{PyObject*}{PyUnicode_FromWideChar}{const wchar_t *w,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001022 Py_ssize_t size}
Thomas Heller541703b2002-04-29 17:28:43 +00001023 Create a Unicode object from the \ctype{wchar_t} buffer \var{w} of
Georg Brandl99363b62005-09-03 07:27:26 +00001024 the given size. Return \NULL{} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00001025\end{cfuncdesc}
1026
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001027\begin{cfuncdesc}{Py_ssize_t}{PyUnicode_AsWideChar}{PyUnicodeObject *unicode,
Fred Drake3adf79e2001-10-12 19:01:43 +00001028 wchar_t *w,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001029 Py_ssize_t size}
Georg Brandl99363b62005-09-03 07:27:26 +00001030 Copy the Unicode object contents into the \ctype{wchar_t} buffer
Marc-André Lemburga9cadcd2004-11-22 13:02:31 +00001031 \var{w}. At most \var{size} \ctype{wchar_t} characters are copied
Georg Brandl99363b62005-09-03 07:27:26 +00001032 (excluding a possibly trailing 0-termination character). Return
Marc-André Lemburga9cadcd2004-11-22 13:02:31 +00001033 the number of \ctype{wchar_t} characters copied or -1 in case of an
1034 error. Note that the resulting \ctype{wchar_t} string may or may
1035 not be 0-terminated. It is the responsibility of the caller to make
1036 sure that the \ctype{wchar_t} string is 0-terminated in case this is
1037 required by the application.
Fred Drake3adf79e2001-10-12 19:01:43 +00001038\end{cfuncdesc}
1039
1040
1041\subsubsection{Built-in Codecs \label{builtinCodecs}}
1042
1043Python provides a set of builtin codecs which are written in C
1044for speed. All of these codecs are directly usable via the
1045following functions.
1046
1047Many of the following APIs take two arguments encoding and
1048errors. These parameters encoding and errors have the same semantics
1049as the ones of the builtin unicode() Unicode object constructor.
1050
1051Setting encoding to \NULL{} causes the default encoding to be used
1052which is \ASCII. The file system calls should use
1053\cdata{Py_FileSystemDefaultEncoding} as the encoding for file
1054names. This variable should be treated as read-only: On some systems,
1055it will be a pointer to a static string, on others, it will change at
Raymond Hettingercb2da432003-10-12 18:24:34 +00001056run-time (such as when the application invokes setlocale).
Fred Drake3adf79e2001-10-12 19:01:43 +00001057
1058Error handling is set by errors which may also be set to \NULL{}
1059meaning to use the default handling defined for the codec. Default
1060error handling for all builtin codecs is ``strict''
1061(\exception{ValueError} is raised).
1062
1063The codecs all use a similar interface. Only deviation from the
1064following generic ones are documented for simplicity.
1065
1066% --- Generic Codecs -----------------------------------------------------
1067
1068These are the generic codec APIs:
1069
1070\begin{cfuncdesc}{PyObject*}{PyUnicode_Decode}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001071 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001072 const char *encoding,
1073 const char *errors}
1074 Create a Unicode object by decoding \var{size} bytes of the encoded
1075 string \var{s}. \var{encoding} and \var{errors} have the same
1076 meaning as the parameters of the same name in the
1077 \function{unicode()} builtin function. The codec to be used is
Georg Brandl99363b62005-09-03 07:27:26 +00001078 looked up using the Python codec registry. Return \NULL{} if an
Fred Drake3adf79e2001-10-12 19:01:43 +00001079 exception was raised by the codec.
1080\end{cfuncdesc}
1081
1082\begin{cfuncdesc}{PyObject*}{PyUnicode_Encode}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001083 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001084 const char *encoding,
1085 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001086 Encode the \ctype{Py_UNICODE} buffer of the given size and return
Fred Drake3adf79e2001-10-12 19:01:43 +00001087 a Python string object. \var{encoding} and \var{errors} have the
1088 same meaning as the parameters of the same name in the Unicode
1089 \method{encode()} method. The codec to be used is looked up using
Georg Brandl99363b62005-09-03 07:27:26 +00001090 the Python codec registry. Return \NULL{} if an exception was
Fred Drake3adf79e2001-10-12 19:01:43 +00001091 raised by the codec.
1092\end{cfuncdesc}
1093
1094\begin{cfuncdesc}{PyObject*}{PyUnicode_AsEncodedString}{PyObject *unicode,
1095 const char *encoding,
1096 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001097 Encode a Unicode object and return the result as Python string
Fred Drake3adf79e2001-10-12 19:01:43 +00001098 object. \var{encoding} and \var{errors} have the same meaning as the
1099 parameters of the same name in the Unicode \method{encode()} method.
1100 The codec to be used is looked up using the Python codec registry.
Georg Brandl99363b62005-09-03 07:27:26 +00001101 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +00001102\end{cfuncdesc}
1103
1104% --- UTF-8 Codecs -------------------------------------------------------
1105
1106These are the UTF-8 codec APIs:
1107
1108\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF8}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001109 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001110 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001111 Create a Unicode object by decoding \var{size} bytes of the UTF-8
1112 encoded string \var{s}. Return \NULL{} if an exception was raised
Fred Drake3adf79e2001-10-12 19:01:43 +00001113 by the codec.
1114\end{cfuncdesc}
1115
Walter Dörwald69652032004-09-07 20:24:22 +00001116\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF8Stateful}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001117 Py_ssize_t size,
Walter Dörwald69652032004-09-07 20:24:22 +00001118 const char *errors,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001119 Py_ssize_t *consumed}
Georg Brandl99363b62005-09-03 07:27:26 +00001120 If \var{consumed} is \NULL{}, behave like \cfunction{PyUnicode_DecodeUTF8()}.
Walter Dörwald69652032004-09-07 20:24:22 +00001121 If \var{consumed} is not \NULL{}, trailing incomplete UTF-8 byte sequences
1122 will not be treated as an error. Those bytes will not be decoded and the
1123 number of bytes that have been decoded will be stored in \var{consumed}.
1124 \versionadded{2.4}
1125\end{cfuncdesc}
1126
Fred Drake3adf79e2001-10-12 19:01:43 +00001127\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUTF8}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001128 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001129 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001130 Encode the \ctype{Py_UNICODE} buffer of the given size using UTF-8
1131 and return a Python string object. Return \NULL{} if an exception
Fred Drake3adf79e2001-10-12 19:01:43 +00001132 was raised by the codec.
1133\end{cfuncdesc}
1134
1135\begin{cfuncdesc}{PyObject*}{PyUnicode_AsUTF8String}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001136 Encode a Unicode objects using UTF-8 and return the result as
1137 Python string object. Error handling is ``strict''. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00001138 \NULL{} if an exception was raised by the codec.
1139\end{cfuncdesc}
1140
1141% --- UTF-16 Codecs ------------------------------------------------------ */
1142
1143These are the UTF-16 codec APIs:
1144
1145\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF16}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001146 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001147 const char *errors,
1148 int *byteorder}
Georg Brandl99363b62005-09-03 07:27:26 +00001149 Decode \var{length} bytes from a UTF-16 encoded buffer string and
1150 return the corresponding Unicode object. \var{errors} (if
Tim Peters9ddf40b2004-06-20 22:41:32 +00001151 non-\NULL{}) defines the error handling. It defaults to ``strict''.
Fred Drake3adf79e2001-10-12 19:01:43 +00001152
Tim Peters9ddf40b2004-06-20 22:41:32 +00001153 If \var{byteorder} is non-\NULL{}, the decoder starts decoding using
Fred Drake3adf79e2001-10-12 19:01:43 +00001154 the given byte order:
1155
1156\begin{verbatim}
1157 *byteorder == -1: little endian
1158 *byteorder == 0: native order
1159 *byteorder == 1: big endian
1160\end{verbatim}
1161
1162 and then switches according to all byte order marks (BOM) it finds
1163 in the input data. BOMs are not copied into the resulting Unicode
1164 string. After completion, \var{*byteorder} is set to the current
1165 byte order at the end of input data.
1166
Tim Peters9ddf40b2004-06-20 22:41:32 +00001167 If \var{byteorder} is \NULL{}, the codec starts in native order mode.
Fred Drake3adf79e2001-10-12 19:01:43 +00001168
Georg Brandl99363b62005-09-03 07:27:26 +00001169 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +00001170\end{cfuncdesc}
1171
Walter Dörwald69652032004-09-07 20:24:22 +00001172\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF16Stateful}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001173 Py_ssize_t size,
Walter Dörwald69652032004-09-07 20:24:22 +00001174 const char *errors,
1175 int *byteorder,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001176 Py_ssize_t *consumed}
Georg Brandl99363b62005-09-03 07:27:26 +00001177 If \var{consumed} is \NULL{}, behave like
Walter Dörwald69652032004-09-07 20:24:22 +00001178 \cfunction{PyUnicode_DecodeUTF16()}. If \var{consumed} is not \NULL{},
1179 \cfunction{PyUnicode_DecodeUTF16Stateful()} will not treat trailing incomplete
Raymond Hettinger0c230b92005-08-17 10:05:22 +00001180 UTF-16 byte sequences (such as an odd number of bytes or a split surrogate pair)
Walter Dörwald69652032004-09-07 20:24:22 +00001181 as an error. Those bytes will not be decoded and the number of bytes that
1182 have been decoded will be stored in \var{consumed}.
1183 \versionadded{2.4}
1184\end{cfuncdesc}
1185
Fred Drake3adf79e2001-10-12 19:01:43 +00001186\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUTF16}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001187 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001188 const char *errors,
1189 int byteorder}
Georg Brandl99363b62005-09-03 07:27:26 +00001190 Return a Python string object holding the UTF-16 encoded value of
Fred Drake3adf79e2001-10-12 19:01:43 +00001191 the Unicode data in \var{s}. If \var{byteorder} is not \code{0},
1192 output is written according to the following byte order:
1193
1194\begin{verbatim}
1195 byteorder == -1: little endian
1196 byteorder == 0: native byte order (writes a BOM mark)
1197 byteorder == 1: big endian
1198\end{verbatim}
1199
1200 If byteorder is \code{0}, the output string will always start with
1201 the Unicode BOM mark (U+FEFF). In the other two modes, no BOM mark
1202 is prepended.
1203
Martin v. Löwis9bc4f2d2004-06-03 09:55:28 +00001204 If \var{Py_UNICODE_WIDE} is defined, a single \ctype{Py_UNICODE}
1205 value may get represented as a surrogate pair. If it is not
1206 defined, each \ctype{Py_UNICODE} values is interpreted as an
1207 UCS-2 character.
Fred Drake3adf79e2001-10-12 19:01:43 +00001208
Georg Brandl99363b62005-09-03 07:27:26 +00001209 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +00001210\end{cfuncdesc}
1211
1212\begin{cfuncdesc}{PyObject*}{PyUnicode_AsUTF16String}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001213 Return a Python string using the UTF-16 encoding in native byte
Fred Drake3adf79e2001-10-12 19:01:43 +00001214 order. The string always starts with a BOM mark. Error handling is
Georg Brandl99363b62005-09-03 07:27:26 +00001215 ``strict''. Return \NULL{} if an exception was raised by the
Fred Drake3adf79e2001-10-12 19:01:43 +00001216 codec.
1217\end{cfuncdesc}
1218
1219% --- Unicode-Escape Codecs ----------------------------------------------
1220
Martin v. Löwis95cf84a2003-10-19 07:32:24 +00001221These are the ``Unicode Escape'' codec APIs:
Fred Drake3adf79e2001-10-12 19:01:43 +00001222
1223\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUnicodeEscape}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001224 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001225 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001226 Create a Unicode object by decoding \var{size} bytes of the
1227 Unicode-Escape encoded string \var{s}. Return \NULL{} if an
Fred Drake3adf79e2001-10-12 19:01:43 +00001228 exception was raised by the codec.
1229\end{cfuncdesc}
1230
1231\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUnicodeEscape}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001232 Py_ssize_t size}
Georg Brandl99363b62005-09-03 07:27:26 +00001233 Encode the \ctype{Py_UNICODE} buffer of the given size using
1234 Unicode-Escape and return a Python string object. Return \NULL{}
Fred Drake3adf79e2001-10-12 19:01:43 +00001235 if an exception was raised by the codec.
1236\end{cfuncdesc}
1237
1238\begin{cfuncdesc}{PyObject*}{PyUnicode_AsUnicodeEscapeString}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001239 Encode a Unicode objects using Unicode-Escape and return the
Fred Drake3adf79e2001-10-12 19:01:43 +00001240 result as Python string object. Error handling is ``strict''.
Georg Brandl99363b62005-09-03 07:27:26 +00001241 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +00001242\end{cfuncdesc}
1243
1244% --- Raw-Unicode-Escape Codecs ------------------------------------------
1245
Martin v. Löwis95cf84a2003-10-19 07:32:24 +00001246These are the ``Raw Unicode Escape'' codec APIs:
Fred Drake3adf79e2001-10-12 19:01:43 +00001247
1248\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeRawUnicodeEscape}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001249 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001250 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001251 Create a Unicode object by decoding \var{size} bytes of the
1252 Raw-Unicode-Escape encoded string \var{s}. Return \NULL{} if an
Fred Drake3adf79e2001-10-12 19:01:43 +00001253 exception was raised by the codec.
1254\end{cfuncdesc}
1255
1256\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeRawUnicodeEscape}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001257 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001258 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001259 Encode the \ctype{Py_UNICODE} buffer of the given size using
1260 Raw-Unicode-Escape and return a Python string object. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00001261 \NULL{} if an exception was raised by the codec.
1262\end{cfuncdesc}
1263
1264\begin{cfuncdesc}{PyObject*}{PyUnicode_AsRawUnicodeEscapeString}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001265 Encode a Unicode objects using Raw-Unicode-Escape and return the
Fred Drake3adf79e2001-10-12 19:01:43 +00001266 result as Python string object. Error handling is ``strict''.
Georg Brandl99363b62005-09-03 07:27:26 +00001267 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +00001268\end{cfuncdesc}
1269
Tim Petersf582b822001-12-11 18:51:08 +00001270% --- Latin-1 Codecs -----------------------------------------------------
Fred Drake3adf79e2001-10-12 19:01:43 +00001271
1272These are the Latin-1 codec APIs:
1273Latin-1 corresponds to the first 256 Unicode ordinals and only these
1274are accepted by the codecs during encoding.
1275
1276\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeLatin1}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001277 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001278 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001279 Create a Unicode object by decoding \var{size} bytes of the Latin-1
1280 encoded string \var{s}. Return \NULL{} if an exception was raised
Fred Drake3adf79e2001-10-12 19:01:43 +00001281 by the codec.
1282\end{cfuncdesc}
1283
1284\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeLatin1}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001285 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001286 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001287 Encode the \ctype{Py_UNICODE} buffer of the given size using
1288 Latin-1 and return a Python string object. Return \NULL{} if an
Fred Drake3adf79e2001-10-12 19:01:43 +00001289 exception was raised by the codec.
1290\end{cfuncdesc}
1291
1292\begin{cfuncdesc}{PyObject*}{PyUnicode_AsLatin1String}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001293 Encode a Unicode objects using Latin-1 and return the result as
1294 Python string object. Error handling is ``strict''. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00001295 \NULL{} if an exception was raised by the codec.
1296\end{cfuncdesc}
1297
Tim Petersf582b822001-12-11 18:51:08 +00001298% --- ASCII Codecs -------------------------------------------------------
Fred Drake3adf79e2001-10-12 19:01:43 +00001299
1300These are the \ASCII{} codec APIs. Only 7-bit \ASCII{} data is
1301accepted. All other codes generate errors.
1302
1303\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeASCII}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001304 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001305 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001306 Create a Unicode object by decoding \var{size} bytes of the
1307 \ASCII{} encoded string \var{s}. Return \NULL{} if an exception
Fred Drake3adf79e2001-10-12 19:01:43 +00001308 was raised by the codec.
1309\end{cfuncdesc}
1310
1311\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeASCII}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001312 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001313 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001314 Encode the \ctype{Py_UNICODE} buffer of the given size using
1315 \ASCII{} and return a Python string object. Return \NULL{} if an
Fred Drake3adf79e2001-10-12 19:01:43 +00001316 exception was raised by the codec.
1317\end{cfuncdesc}
1318
1319\begin{cfuncdesc}{PyObject*}{PyUnicode_AsASCIIString}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001320 Encode a Unicode objects using \ASCII{} and return the result as
1321 Python string object. Error handling is ``strict''. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00001322 \NULL{} if an exception was raised by the codec.
1323\end{cfuncdesc}
1324
Tim Petersf582b822001-12-11 18:51:08 +00001325% --- Character Map Codecs -----------------------------------------------
Fred Drake3adf79e2001-10-12 19:01:43 +00001326
1327These are the mapping codec APIs:
1328
1329This codec is special in that it can be used to implement many
1330different codecs (and this is in fact what was done to obtain most of
1331the standard codecs included in the \module{encodings} package). The
1332codec uses mapping to encode and decode characters.
1333
1334Decoding mappings must map single string characters to single Unicode
1335characters, integers (which are then interpreted as Unicode ordinals)
Tim Petersf582b822001-12-11 18:51:08 +00001336or None (meaning "undefined mapping" and causing an error).
Fred Drake3adf79e2001-10-12 19:01:43 +00001337
1338Encoding mappings must map single Unicode characters to single string
1339characters, integers (which are then interpreted as Latin-1 ordinals)
1340or None (meaning "undefined mapping" and causing an error).
1341
1342The mapping objects provided must only support the __getitem__ mapping
1343interface.
1344
1345If a character lookup fails with a LookupError, the character is
1346copied as-is meaning that its ordinal value will be interpreted as
1347Unicode or Latin-1 ordinal resp. Because of this, mappings only need
1348to contain those mappings which map characters to different code
1349points.
1350
1351\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeCharmap}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001352 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001353 PyObject *mapping,
1354 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001355 Create a Unicode object by decoding \var{size} bytes of the encoded
1356 string \var{s} using the given \var{mapping} object. Return
Walter Dörwaldd1c1e102005-10-06 20:29:57 +00001357 \NULL{} if an exception was raised by the codec. If \var{mapping} is \NULL{}
1358 latin-1 decoding will be done. Else it can be a dictionary mapping byte or a
1359 unicode string, which is treated as a lookup table. Byte values greater
1360 that the length of the string and U+FFFE "characters" are treated as
1361 "undefined mapping".
1362 \versionchanged[Allowed unicode string as mapping argument]{2.4}
Fred Drake3adf79e2001-10-12 19:01:43 +00001363\end{cfuncdesc}
1364
1365\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeCharmap}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001366 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001367 PyObject *mapping,
1368 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001369 Encode the \ctype{Py_UNICODE} buffer of the given size using the
1370 given \var{mapping} object and return a Python string object.
1371 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +00001372\end{cfuncdesc}
1373
1374\begin{cfuncdesc}{PyObject*}{PyUnicode_AsCharmapString}{PyObject *unicode,
1375 PyObject *mapping}
Georg Brandl99363b62005-09-03 07:27:26 +00001376 Encode a Unicode objects using the given \var{mapping} object and
1377 return the result as Python string object. Error handling is
1378 ``strict''. Return \NULL{} if an exception was raised by the
Fred Drake3adf79e2001-10-12 19:01:43 +00001379 codec.
1380\end{cfuncdesc}
1381
1382The following codec API is special in that maps Unicode to Unicode.
1383
1384\begin{cfuncdesc}{PyObject*}{PyUnicode_TranslateCharmap}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001385 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001386 PyObject *table,
1387 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001388 Translate a \ctype{Py_UNICODE} buffer of the given length by
1389 applying a character mapping \var{table} to it and return the
1390 resulting Unicode object. Return \NULL{} when an exception was
Fred Drake3adf79e2001-10-12 19:01:43 +00001391 raised by the codec.
1392
1393 The \var{mapping} table must map Unicode ordinal integers to Unicode
1394 ordinal integers or None (causing deletion of the character).
1395
1396 Mapping tables need only provide the method{__getitem__()}
1397 interface; dictionaries and sequences work well. Unmapped character
1398 ordinals (ones which cause a \exception{LookupError}) are left
1399 untouched and are copied as-is.
1400\end{cfuncdesc}
1401
1402% --- MBCS codecs for Windows --------------------------------------------
1403
1404These are the MBCS codec APIs. They are currently only available on
1405Windows and use the Win32 MBCS converters to implement the
1406conversions. Note that MBCS (or DBCS) is a class of encodings, not
1407just one. The target encoding is defined by the user settings on the
1408machine running the codec.
1409
1410\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeMBCS}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001411 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001412 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001413 Create a Unicode object by decoding \var{size} bytes of the MBCS
1414 encoded string \var{s}. Return \NULL{} if an exception was
Fred Drake3adf79e2001-10-12 19:01:43 +00001415 raised by the codec.
1416\end{cfuncdesc}
1417
1418\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeMBCS}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001419 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001420 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001421 Encode the \ctype{Py_UNICODE} buffer of the given size using MBCS
1422 and return a Python string object. Return \NULL{} if an exception
Fred Drake3adf79e2001-10-12 19:01:43 +00001423 was raised by the codec.
1424\end{cfuncdesc}
1425
1426\begin{cfuncdesc}{PyObject*}{PyUnicode_AsMBCSString}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001427 Encode a Unicode objects using MBCS and return the result as
1428 Python string object. Error handling is ``strict''. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00001429 \NULL{} if an exception was raised by the codec.
1430\end{cfuncdesc}
1431
1432% --- Methods & Slots ----------------------------------------------------
1433
1434\subsubsection{Methods and Slot Functions \label{unicodeMethodsAndSlots}}
1435
1436The following APIs are capable of handling Unicode objects and strings
1437on input (we refer to them as strings in the descriptions) and return
Martin v. Löwis95cf84a2003-10-19 07:32:24 +00001438Unicode objects or integers as appropriate.
Fred Drake3adf79e2001-10-12 19:01:43 +00001439
1440They all return \NULL{} or \code{-1} if an exception occurs.
1441
1442\begin{cfuncdesc}{PyObject*}{PyUnicode_Concat}{PyObject *left,
1443 PyObject *right}
1444 Concat two strings giving a new Unicode string.
1445\end{cfuncdesc}
1446
1447\begin{cfuncdesc}{PyObject*}{PyUnicode_Split}{PyObject *s,
1448 PyObject *sep,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001449 Py_ssize_t maxsplit}
Tim Peters9ddf40b2004-06-20 22:41:32 +00001450 Split a string giving a list of Unicode strings. If sep is \NULL{},
Fred Drake3adf79e2001-10-12 19:01:43 +00001451 splitting will be done at all whitespace substrings. Otherwise,
1452 splits occur at the given separator. At most \var{maxsplit} splits
1453 will be done. If negative, no limit is set. Separators are not
1454 included in the resulting list.
1455\end{cfuncdesc}
1456
1457\begin{cfuncdesc}{PyObject*}{PyUnicode_Splitlines}{PyObject *s,
Martin v. Löwis24b88812003-03-30 16:40:42 +00001458 int keepend}
Fred Drake3adf79e2001-10-12 19:01:43 +00001459 Split a Unicode string at line breaks, returning a list of Unicode
Martin v. Löwis24b88812003-03-30 16:40:42 +00001460 strings. CRLF is considered to be one line break. If \var{keepend}
1461 is 0, the Line break characters are not included in the resulting
1462 strings.
Fred Drake3adf79e2001-10-12 19:01:43 +00001463\end{cfuncdesc}
1464
1465\begin{cfuncdesc}{PyObject*}{PyUnicode_Translate}{PyObject *str,
1466 PyObject *table,
1467 const char *errors}
1468 Translate a string by applying a character mapping table to it and
1469 return the resulting Unicode object.
1470
1471 The mapping table must map Unicode ordinal integers to Unicode
1472 ordinal integers or None (causing deletion of the character).
1473
1474 Mapping tables need only provide the \method{__getitem__()}
1475 interface; dictionaries and sequences work well. Unmapped character
1476 ordinals (ones which cause a \exception{LookupError}) are left
1477 untouched and are copied as-is.
1478
1479 \var{errors} has the usual meaning for codecs. It may be \NULL{}
1480 which indicates to use the default error handling.
1481\end{cfuncdesc}
1482
1483\begin{cfuncdesc}{PyObject*}{PyUnicode_Join}{PyObject *separator,
1484 PyObject *seq}
1485 Join a sequence of strings using the given separator and return the
1486 resulting Unicode string.
1487\end{cfuncdesc}
1488
Raymond Hettinger8ef9b3e2004-12-10 17:12:32 +00001489\begin{cfuncdesc}{int}{PyUnicode_Tailmatch}{PyObject *str,
Fred Drake3adf79e2001-10-12 19:01:43 +00001490 PyObject *substr,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001491 Py_ssize_t start,
1492 Py_ssize_t end,
Fred Drake3adf79e2001-10-12 19:01:43 +00001493 int direction}
1494 Return 1 if \var{substr} matches \var{str}[\var{start}:\var{end}] at
1495 the given tail end (\var{direction} == -1 means to do a prefix
1496 match, \var{direction} == 1 a suffix match), 0 otherwise.
Georg Brandl99363b62005-09-03 07:27:26 +00001497 Return \code{-1} if an error occurred.
Fred Drake3adf79e2001-10-12 19:01:43 +00001498\end{cfuncdesc}
1499
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001500\begin{cfuncdesc}{Py_ssize_t}{PyUnicode_Find}{PyObject *str,
Fred Drake1d1e1db2002-06-20 22:07:04 +00001501 PyObject *substr,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001502 Py_ssize_t start,
1503 Py_ssize_t end,
Fred Drake1d1e1db2002-06-20 22:07:04 +00001504 int direction}
Fred Drake3adf79e2001-10-12 19:01:43 +00001505 Return the first position of \var{substr} in
1506 \var{str}[\var{start}:\var{end}] using the given \var{direction}
1507 (\var{direction} == 1 means to do a forward search,
Fred Drake1d1e1db2002-06-20 22:07:04 +00001508 \var{direction} == -1 a backward search). The return value is the
1509 index of the first match; a value of \code{-1} indicates that no
1510 match was found, and \code{-2} indicates that an error occurred and
1511 an exception has been set.
Fred Drake3adf79e2001-10-12 19:01:43 +00001512\end{cfuncdesc}
1513
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001514\begin{cfuncdesc}{Py_ssize_t}{PyUnicode_Count}{PyObject *str,
Fred Drake1d1e1db2002-06-20 22:07:04 +00001515 PyObject *substr,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001516 Py_ssize_t start,
1517 Py_ssize_t end}
Fred Drake1d1e1db2002-06-20 22:07:04 +00001518 Return the number of non-overlapping occurrences of \var{substr} in
Georg Brandl99363b62005-09-03 07:27:26 +00001519 \code{\var{str}[\var{start}:\var{end}]}. Return \code{-1} if an
Fred Drake1d1e1db2002-06-20 22:07:04 +00001520 error occurred.
Fred Drake3adf79e2001-10-12 19:01:43 +00001521\end{cfuncdesc}
1522
1523\begin{cfuncdesc}{PyObject*}{PyUnicode_Replace}{PyObject *str,
1524 PyObject *substr,
1525 PyObject *replstr,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001526 Py_ssize_t maxcount}
Fred Drake3adf79e2001-10-12 19:01:43 +00001527 Replace at most \var{maxcount} occurrences of \var{substr} in
1528 \var{str} with \var{replstr} and return the resulting Unicode object.
1529 \var{maxcount} == -1 means replace all occurrences.
1530\end{cfuncdesc}
1531
1532\begin{cfuncdesc}{int}{PyUnicode_Compare}{PyObject *left, PyObject *right}
1533 Compare two strings and return -1, 0, 1 for less than, equal, and
1534 greater than, respectively.
1535\end{cfuncdesc}
1536
1537\begin{cfuncdesc}{PyObject*}{PyUnicode_Format}{PyObject *format,
1538 PyObject *args}
Georg Brandl99363b62005-09-03 07:27:26 +00001539 Return a new string object from \var{format} and \var{args}; this
Fred Drake3adf79e2001-10-12 19:01:43 +00001540 is analogous to \code{\var{format} \%\ \var{args}}. The
1541 \var{args} argument must be a tuple.
1542\end{cfuncdesc}
1543
1544\begin{cfuncdesc}{int}{PyUnicode_Contains}{PyObject *container,
1545 PyObject *element}
Georg Brandl99363b62005-09-03 07:27:26 +00001546 Check whether \var{element} is contained in \var{container} and
1547 return true or false accordingly.
Fred Drake3adf79e2001-10-12 19:01:43 +00001548
1549 \var{element} has to coerce to a one element Unicode
1550 string. \code{-1} is returned if there was an error.
1551\end{cfuncdesc}
1552
1553
1554\subsection{Buffer Objects \label{bufferObjects}}
1555\sectionauthor{Greg Stein}{gstein@lyra.org}
1556
1557\obindex{buffer}
1558Python objects implemented in C can export a group of functions called
1559the ``buffer\index{buffer interface} interface.'' These functions can
1560be used by an object to expose its data in a raw, byte-oriented
1561format. Clients of the object can use the buffer interface to access
1562the object data directly, without needing to copy it first.
1563
Tim Petersf582b822001-12-11 18:51:08 +00001564Two examples of objects that support
1565the buffer interface are strings and arrays. The string object exposes
Fred Drake3adf79e2001-10-12 19:01:43 +00001566the character contents in the buffer interface's byte-oriented
1567form. An array can also expose its contents, but it should be noted
1568that array elements may be multi-byte values.
1569
1570An example user of the buffer interface is the file object's
1571\method{write()} method. Any object that can export a series of bytes
1572through the buffer interface can be written to a file. There are a
Tim Petersf582b822001-12-11 18:51:08 +00001573number of format codes to \cfunction{PyArg_ParseTuple()} that operate
Fred Drake3adf79e2001-10-12 19:01:43 +00001574against an object's buffer interface, returning data from the target
1575object.
1576
1577More information on the buffer interface is provided in the section
Fred Drake54e62942001-12-11 19:40:16 +00001578``Buffer Object Structures'' (section~\ref{buffer-structs}), under
Fred Drake3adf79e2001-10-12 19:01:43 +00001579the description for \ctype{PyBufferProcs}\ttindex{PyBufferProcs}.
1580
1581A ``buffer object'' is defined in the \file{bufferobject.h} header
1582(included by \file{Python.h}). These objects look very similar to
1583string objects at the Python programming level: they support slicing,
1584indexing, concatenation, and some other standard string
1585operations. However, their data can come from one of two sources: from
1586a block of memory, or from another object which exports the buffer
1587interface.
1588
1589Buffer objects are useful as a way to expose the data from another
1590object's buffer interface to the Python programmer. They can also be
1591used as a zero-copy slicing mechanism. Using their ability to
1592reference a block of memory, it is possible to expose any data to the
1593Python programmer quite easily. The memory could be a large, constant
1594array in a C extension, it could be a raw block of memory for
1595manipulation before passing to an operating system library, or it
1596could be used to pass around structured data in its native, in-memory
1597format.
1598
1599\begin{ctypedesc}{PyBufferObject}
1600 This subtype of \ctype{PyObject} represents a buffer object.
1601\end{ctypedesc}
1602
1603\begin{cvardesc}{PyTypeObject}{PyBuffer_Type}
1604 The instance of \ctype{PyTypeObject} which represents the Python
1605 buffer type; it is the same object as \code{types.BufferType} in the
1606 Python layer.\withsubitem{(in module types)}{\ttindex{BufferType}}.
1607\end{cvardesc}
1608
1609\begin{cvardesc}{int}{Py_END_OF_BUFFER}
1610 This constant may be passed as the \var{size} parameter to
1611 \cfunction{PyBuffer_FromObject()} or
1612 \cfunction{PyBuffer_FromReadWriteObject()}. It indicates that the
1613 new \ctype{PyBufferObject} should refer to \var{base} object from
1614 the specified \var{offset} to the end of its exported buffer. Using
1615 this enables the caller to avoid querying the \var{base} object for
1616 its length.
1617\end{cvardesc}
1618
1619\begin{cfuncdesc}{int}{PyBuffer_Check}{PyObject *p}
1620 Return true if the argument has type \cdata{PyBuffer_Type}.
1621\end{cfuncdesc}
1622
1623\begin{cfuncdesc}{PyObject*}{PyBuffer_FromObject}{PyObject *base,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001624 Py_ssize_t offset, Py_ssize_t size}
Fred Drake3adf79e2001-10-12 19:01:43 +00001625 Return a new read-only buffer object. This raises
1626 \exception{TypeError} if \var{base} doesn't support the read-only
1627 buffer protocol or doesn't provide exactly one buffer segment, or it
1628 raises \exception{ValueError} if \var{offset} is less than zero. The
1629 buffer will hold a reference to the \var{base} object, and the
1630 buffer's contents will refer to the \var{base} object's buffer
1631 interface, starting as position \var{offset} and extending for
1632 \var{size} bytes. If \var{size} is \constant{Py_END_OF_BUFFER}, then
1633 the new buffer's contents extend to the length of the \var{base}
1634 object's exported buffer data.
1635\end{cfuncdesc}
1636
1637\begin{cfuncdesc}{PyObject*}{PyBuffer_FromReadWriteObject}{PyObject *base,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001638 Py_ssize_t offset,
1639 Py_ssize_t size}
Fred Drake3adf79e2001-10-12 19:01:43 +00001640 Return a new writable buffer object. Parameters and exceptions are
1641 similar to those for \cfunction{PyBuffer_FromObject()}. If the
1642 \var{base} object does not export the writeable buffer protocol,
1643 then \exception{TypeError} is raised.
1644\end{cfuncdesc}
1645
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001646\begin{cfuncdesc}{PyObject*}{PyBuffer_FromMemory}{void *ptr, Py_ssize_t size}
Fred Drake3adf79e2001-10-12 19:01:43 +00001647 Return a new read-only buffer object that reads from a specified
1648 location in memory, with a specified size. The caller is
1649 responsible for ensuring that the memory buffer, passed in as
1650 \var{ptr}, is not deallocated while the returned buffer object
1651 exists. Raises \exception{ValueError} if \var{size} is less than
1652 zero. Note that \constant{Py_END_OF_BUFFER} may \emph{not} be
1653 passed for the \var{size} parameter; \exception{ValueError} will be
1654 raised in that case.
1655\end{cfuncdesc}
1656
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001657\begin{cfuncdesc}{PyObject*}{PyBuffer_FromReadWriteMemory}{void *ptr, Py_ssize_t size}
Fred Drake3adf79e2001-10-12 19:01:43 +00001658 Similar to \cfunction{PyBuffer_FromMemory()}, but the returned
1659 buffer is writable.
1660\end{cfuncdesc}
1661
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001662\begin{cfuncdesc}{PyObject*}{PyBuffer_New}{Py_ssize_t size}
Georg Brandl99363b62005-09-03 07:27:26 +00001663 Return a new writable buffer object that maintains its own memory
Fred Drake3adf79e2001-10-12 19:01:43 +00001664 buffer of \var{size} bytes. \exception{ValueError} is returned if
Neil Schemenauerd68d3ee2004-06-08 02:58:50 +00001665 \var{size} is not zero or positive. Note that the memory buffer (as
1666 returned by \cfunction{PyObject_AsWriteBuffer()}) is not specifically
1667 aligned.
Fred Drake3adf79e2001-10-12 19:01:43 +00001668\end{cfuncdesc}
1669
1670
1671\subsection{Tuple Objects \label{tupleObjects}}
1672
1673\obindex{tuple}
1674\begin{ctypedesc}{PyTupleObject}
1675 This subtype of \ctype{PyObject} represents a Python tuple object.
1676\end{ctypedesc}
1677
1678\begin{cvardesc}{PyTypeObject}{PyTuple_Type}
1679 This instance of \ctype{PyTypeObject} represents the Python tuple
1680 type; it is the same object as \code{types.TupleType} in the Python
1681 layer.\withsubitem{(in module types)}{\ttindex{TupleType}}.
1682\end{cvardesc}
1683
1684\begin{cfuncdesc}{int}{PyTuple_Check}{PyObject *p}
1685 Return true if \var{p} is a tuple object or an instance of a subtype
1686 of the tuple type.
1687 \versionchanged[Allowed subtypes to be accepted]{2.2}
1688\end{cfuncdesc}
1689
1690\begin{cfuncdesc}{int}{PyTuple_CheckExact}{PyObject *p}
1691 Return true if \var{p} is a tuple object, but not an instance of a
1692 subtype of the tuple type.
1693 \versionadded{2.2}
1694\end{cfuncdesc}
1695
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001696\begin{cfuncdesc}{PyObject*}{PyTuple_New}{Py_ssize_t len}
Fred Drake3adf79e2001-10-12 19:01:43 +00001697 Return a new tuple object of size \var{len}, or \NULL{} on failure.
1698\end{cfuncdesc}
1699
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001700\begin{cfuncdesc}{PyObject*}{PyTuple_Pack}{Py_ssize_t n, \moreargs}
Raymond Hettingercb2da432003-10-12 18:24:34 +00001701 Return a new tuple object of size \var{n}, or \NULL{} on failure.
1702 The tuple values are initialized to the subsequent \var{n} C arguments
1703 pointing to Python objects. \samp{PyTuple_Pack(2, \var{a}, \var{b})}
1704 is equivalent to \samp{Py_BuildValue("(OO)", \var{a}, \var{b})}.
Tim Peters9ddf40b2004-06-20 22:41:32 +00001705 \versionadded{2.4}
Raymond Hettingercb2da432003-10-12 18:24:34 +00001706\end{cfuncdesc}
1707
Fred Drake3adf79e2001-10-12 19:01:43 +00001708\begin{cfuncdesc}{int}{PyTuple_Size}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00001709 Take a pointer to a tuple object, and return the size of that
Fred Drake3adf79e2001-10-12 19:01:43 +00001710 tuple.
1711\end{cfuncdesc}
1712
1713\begin{cfuncdesc}{int}{PyTuple_GET_SIZE}{PyObject *p}
1714 Return the size of the tuple \var{p}, which must be non-\NULL{} and
1715 point to a tuple; no error checking is performed.
1716\end{cfuncdesc}
1717
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001718\begin{cfuncdesc}{PyObject*}{PyTuple_GetItem}{PyObject *p, Py_ssize_t pos}
Georg Brandl99363b62005-09-03 07:27:26 +00001719 Return the object at position \var{pos} in the tuple pointed to by
1720 \var{p}. If \var{pos} is out of bounds, return \NULL{} and sets an
Fred Drake3adf79e2001-10-12 19:01:43 +00001721 \exception{IndexError} exception.
1722\end{cfuncdesc}
1723
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001724\begin{cfuncdesc}{PyObject*}{PyTuple_GET_ITEM}{PyObject *p, Py_ssize_t pos}
Fred Drake3adf79e2001-10-12 19:01:43 +00001725 Like \cfunction{PyTuple_GetItem()}, but does no checking of its
1726 arguments.
1727\end{cfuncdesc}
1728
1729\begin{cfuncdesc}{PyObject*}{PyTuple_GetSlice}{PyObject *p,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001730 Py_ssize_t low, Py_ssize_t high}
Georg Brandl99363b62005-09-03 07:27:26 +00001731 Take a slice of the tuple pointed to by \var{p} from \var{low} to
1732 \var{high} and return it as a new tuple.
Fred Drake3adf79e2001-10-12 19:01:43 +00001733\end{cfuncdesc}
1734
1735\begin{cfuncdesc}{int}{PyTuple_SetItem}{PyObject *p,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001736 Py_ssize_t pos, PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +00001737 Insert a reference to object \var{o} at position \var{pos} of the
1738 tuple pointed to by \var{p}. Return \code{0} on success.
Fred Drake3adf79e2001-10-12 19:01:43 +00001739 \note{This function ``steals'' a reference to \var{o}.}
1740\end{cfuncdesc}
1741
1742\begin{cfuncdesc}{void}{PyTuple_SET_ITEM}{PyObject *p,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001743 Py_ssize_t pos, PyObject *o}
Fred Drake3adf79e2001-10-12 19:01:43 +00001744 Like \cfunction{PyTuple_SetItem()}, but does no error checking, and
1745 should \emph{only} be used to fill in brand new tuples. \note{This
1746 function ``steals'' a reference to \var{o}.}
1747\end{cfuncdesc}
1748
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001749\begin{cfuncdesc}{int}{_PyTuple_Resize}{PyObject **p, Py_ssize_t newsize}
Fred Drake3adf79e2001-10-12 19:01:43 +00001750 Can be used to resize a tuple. \var{newsize} will be the new length
1751 of the tuple. Because tuples are \emph{supposed} to be immutable,
1752 this should only be used if there is only one reference to the
1753 object. Do \emph{not} use this if the tuple may already be known to
1754 some other part of the code. The tuple will always grow or shrink
1755 at the end. Think of this as destroying the old tuple and creating
1756 a new one, only more efficiently. Returns \code{0} on success.
1757 Client code should never assume that the resulting value of
1758 \code{*\var{p}} will be the same as before calling this function.
1759 If the object referenced by \code{*\var{p}} is replaced, the
1760 original \code{*\var{p}} is destroyed. On failure, returns
Tim Peters9ddf40b2004-06-20 22:41:32 +00001761 \code{-1} and sets \code{*\var{p}} to \NULL{}, and raises
Fred Drake3adf79e2001-10-12 19:01:43 +00001762 \exception{MemoryError} or
1763 \exception{SystemError}.
Tim Petersf582b822001-12-11 18:51:08 +00001764 \versionchanged[Removed unused third parameter, \var{last_is_sticky}]{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +00001765\end{cfuncdesc}
1766
1767
1768\subsection{List Objects \label{listObjects}}
1769
1770\obindex{list}
1771\begin{ctypedesc}{PyListObject}
1772 This subtype of \ctype{PyObject} represents a Python list object.
1773\end{ctypedesc}
1774
1775\begin{cvardesc}{PyTypeObject}{PyList_Type}
1776 This instance of \ctype{PyTypeObject} represents the Python list
1777 type. This is the same object as \code{types.ListType}.
1778 \withsubitem{(in module types)}{\ttindex{ListType}}
1779\end{cvardesc}
1780
1781\begin{cfuncdesc}{int}{PyList_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00001782 Return true if \var{p} is a list object or an instance of a
Andrew MacIntyre13cd8892003-12-25 23:57:52 +00001783 subtype of the list type.
1784 \versionchanged[Allowed subtypes to be accepted]{2.2}
1785\end{cfuncdesc}
1786
1787\begin{cfuncdesc}{int}{PyList_CheckExact}{PyObject *p}
1788 Return true if \var{p} is a list object, but not an instance of a
1789 subtype of the list type.
1790 \versionadded{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +00001791\end{cfuncdesc}
1792
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001793\begin{cfuncdesc}{PyObject*}{PyList_New}{Py_ssize_t len}
Georg Brandl99363b62005-09-03 07:27:26 +00001794 Return a new list of length \var{len} on success, or \NULL{} on
Fred Drake3adf79e2001-10-12 19:01:43 +00001795 failure.
1796\end{cfuncdesc}
1797
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001798\begin{cfuncdesc}{Py_ssize_t}{PyList_Size}{PyObject *list}
Georg Brandl99363b62005-09-03 07:27:26 +00001799 Return the length of the list object in \var{list}; this is
Fred Drake3adf79e2001-10-12 19:01:43 +00001800 equivalent to \samp{len(\var{list})} on a list object.
1801 \bifuncindex{len}
1802\end{cfuncdesc}
1803
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001804\begin{cfuncdesc}{Py_ssize_t}{PyList_GET_SIZE}{PyObject *list}
Fred Drake3adf79e2001-10-12 19:01:43 +00001805 Macro form of \cfunction{PyList_Size()} without error checking.
1806\end{cfuncdesc}
1807
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001808\begin{cfuncdesc}{PyObject*}{PyList_GetItem}{PyObject *list, Py_ssize_t index}
Georg Brandl99363b62005-09-03 07:27:26 +00001809 Return the object at position \var{pos} in the list pointed to by
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001810 \var{p}. The position must be positive, indexing from the end of the
1811 list is not supported. If \var{pos} is out of bounds, return \NULL{}
1812 and set an \exception{IndexError} exception.
Fred Drake3adf79e2001-10-12 19:01:43 +00001813\end{cfuncdesc}
1814
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001815\begin{cfuncdesc}{PyObject*}{PyList_GET_ITEM}{PyObject *list, Py_ssize_t i}
Fred Drake3adf79e2001-10-12 19:01:43 +00001816 Macro form of \cfunction{PyList_GetItem()} without error checking.
1817\end{cfuncdesc}
1818
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001819\begin{cfuncdesc}{int}{PyList_SetItem}{PyObject *list, Py_ssize_t index,
Fred Drake3adf79e2001-10-12 19:01:43 +00001820 PyObject *item}
Georg Brandl99363b62005-09-03 07:27:26 +00001821 Set the item at index \var{index} in list to \var{item}. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00001822 \code{0} on success or \code{-1} on failure. \note{This function
1823 ``steals'' a reference to \var{item} and discards a reference to an
1824 item already in the list at the affected position.}
1825\end{cfuncdesc}
1826
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001827\begin{cfuncdesc}{void}{PyList_SET_ITEM}{PyObject *list, Py_ssize_t i,
Fred Drake3adf79e2001-10-12 19:01:43 +00001828 PyObject *o}
1829 Macro form of \cfunction{PyList_SetItem()} without error checking.
1830 This is normally only used to fill in new lists where there is no
1831 previous content.
1832 \note{This function ``steals'' a reference to \var{item}, and,
1833 unlike \cfunction{PyList_SetItem()}, does \emph{not} discard a
1834 reference to any item that it being replaced; any reference in
1835 \var{list} at position \var{i} will be leaked.}
1836\end{cfuncdesc}
1837
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001838\begin{cfuncdesc}{int}{PyList_Insert}{PyObject *list, Py_ssize_t index,
Fred Drake3adf79e2001-10-12 19:01:43 +00001839 PyObject *item}
Georg Brandl99363b62005-09-03 07:27:26 +00001840 Insert the item \var{item} into list \var{list} in front of index
1841 \var{index}. Return \code{0} if successful; return \code{-1} and
1842 set an exception if unsuccessful. Analogous to
Fred Drake3adf79e2001-10-12 19:01:43 +00001843 \code{\var{list}.insert(\var{index}, \var{item})}.
1844\end{cfuncdesc}
1845
1846\begin{cfuncdesc}{int}{PyList_Append}{PyObject *list, PyObject *item}
Georg Brandl99363b62005-09-03 07:27:26 +00001847 Append the object \var{item} at the end of list \var{list}.
1848 Return \code{0} if successful; return \code{-1} and set an
Fred Drake3adf79e2001-10-12 19:01:43 +00001849 exception if unsuccessful. Analogous to
1850 \code{\var{list}.append(\var{item})}.
1851\end{cfuncdesc}
1852
1853\begin{cfuncdesc}{PyObject*}{PyList_GetSlice}{PyObject *list,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001854 Py_ssize_t low, Py_ssize_t high}
Georg Brandl99363b62005-09-03 07:27:26 +00001855 Return a list of the objects in \var{list} containing the objects
1856 \emph{between} \var{low} and \var{high}. Return \NULL{} and set
Fred Drake3adf79e2001-10-12 19:01:43 +00001857 an exception if unsuccessful.
1858 Analogous to \code{\var{list}[\var{low}:\var{high}]}.
1859\end{cfuncdesc}
1860
1861\begin{cfuncdesc}{int}{PyList_SetSlice}{PyObject *list,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001862 Py_ssize_t low, Py_ssize_t high,
Fred Drake3adf79e2001-10-12 19:01:43 +00001863 PyObject *itemlist}
Georg Brandl99363b62005-09-03 07:27:26 +00001864 Set the slice of \var{list} between \var{low} and \var{high} to the
Fred Drake3adf79e2001-10-12 19:01:43 +00001865 contents of \var{itemlist}. Analogous to
Raymond Hettinger9c7ed4c2003-10-26 17:20:07 +00001866 \code{\var{list}[\var{low}:\var{high}] = \var{itemlist}}.
1867 The \var{itemlist} may be \NULL{}, indicating the assignment
1868 of an empty list (slice deletion).
Georg Brandl99363b62005-09-03 07:27:26 +00001869 Return \code{0} on success, \code{-1} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00001870\end{cfuncdesc}
1871
1872\begin{cfuncdesc}{int}{PyList_Sort}{PyObject *list}
Georg Brandl99363b62005-09-03 07:27:26 +00001873 Sort the items of \var{list} in place. Return \code{0} on
Fred Drake3adf79e2001-10-12 19:01:43 +00001874 success, \code{-1} on failure. This is equivalent to
1875 \samp{\var{list}.sort()}.
1876\end{cfuncdesc}
1877
1878\begin{cfuncdesc}{int}{PyList_Reverse}{PyObject *list}
Georg Brandl99363b62005-09-03 07:27:26 +00001879 Reverse the items of \var{list} in place. Return \code{0} on
Fred Drake3adf79e2001-10-12 19:01:43 +00001880 success, \code{-1} on failure. This is the equivalent of
1881 \samp{\var{list}.reverse()}.
1882\end{cfuncdesc}
1883
1884\begin{cfuncdesc}{PyObject*}{PyList_AsTuple}{PyObject *list}
Georg Brandl99363b62005-09-03 07:27:26 +00001885 Return a new tuple object containing the contents of \var{list};
Fred Drake3adf79e2001-10-12 19:01:43 +00001886 equivalent to \samp{tuple(\var{list})}.\bifuncindex{tuple}
1887\end{cfuncdesc}
1888
1889
1890\section{Mapping Objects \label{mapObjects}}
1891
1892\obindex{mapping}
1893
1894
1895\subsection{Dictionary Objects \label{dictObjects}}
1896
1897\obindex{dictionary}
1898\begin{ctypedesc}{PyDictObject}
1899 This subtype of \ctype{PyObject} represents a Python dictionary
1900 object.
1901\end{ctypedesc}
1902
1903\begin{cvardesc}{PyTypeObject}{PyDict_Type}
1904 This instance of \ctype{PyTypeObject} represents the Python
1905 dictionary type. This is exposed to Python programs as
1906 \code{types.DictType} and \code{types.DictionaryType}.
1907 \withsubitem{(in module types)}{\ttindex{DictType}\ttindex{DictionaryType}}
1908\end{cvardesc}
1909
1910\begin{cfuncdesc}{int}{PyDict_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00001911 Return true if \var{p} is a dict object or an instance of a
Andrew MacIntyre13cd8892003-12-25 23:57:52 +00001912 subtype of the dict type.
1913 \versionchanged[Allowed subtypes to be accepted]{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +00001914\end{cfuncdesc}
1915
Andrew MacIntyref72af652003-12-26 00:07:51 +00001916\begin{cfuncdesc}{int}{PyDict_CheckExact}{PyObject *p}
1917 Return true if \var{p} is a dict object, but not an instance of a
1918 subtype of the dict type.
1919 \versionadded{2.4}
1920\end{cfuncdesc}
1921
Fred Drake3adf79e2001-10-12 19:01:43 +00001922\begin{cfuncdesc}{PyObject*}{PyDict_New}{}
Georg Brandl99363b62005-09-03 07:27:26 +00001923 Return a new empty dictionary, or \NULL{} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00001924\end{cfuncdesc}
1925
1926\begin{cfuncdesc}{PyObject*}{PyDictProxy_New}{PyObject *dict}
1927 Return a proxy object for a mapping which enforces read-only
1928 behavior. This is normally used to create a proxy to prevent
1929 modification of the dictionary for non-dynamic class types.
1930 \versionadded{2.2}
1931\end{cfuncdesc}
1932
1933\begin{cfuncdesc}{void}{PyDict_Clear}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00001934 Empty an existing dictionary of all key-value pairs.
Fred Drake3adf79e2001-10-12 19:01:43 +00001935\end{cfuncdesc}
1936
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00001937\begin{cfuncdesc}{int}{PyDict_Contains}{PyObject *p, PyObject *key}
1938 Determine if dictionary \var{p} contains \var{key}. If an item
1939 in \var{p} is matches \var{key}, return \code{1}, otherwise return
1940 \code{0}. On error, return \code{-1}. This is equivalent to the
1941 Python expression \samp{\var{key} in \var{p}}.
Tim Peters9ddf40b2004-06-20 22:41:32 +00001942 \versionadded{2.4}
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00001943\end{cfuncdesc}
1944
Fred Drake3adf79e2001-10-12 19:01:43 +00001945\begin{cfuncdesc}{PyObject*}{PyDict_Copy}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00001946 Return a new dictionary that contains the same key-value pairs as
Fred Drake3adf79e2001-10-12 19:01:43 +00001947 \var{p}.
1948 \versionadded{1.6}
1949\end{cfuncdesc}
1950
1951\begin{cfuncdesc}{int}{PyDict_SetItem}{PyObject *p, PyObject *key,
1952 PyObject *val}
Georg Brandl99363b62005-09-03 07:27:26 +00001953 Insert \var{value} into the dictionary \var{p} with a key of
Fred Drake3adf79e2001-10-12 19:01:43 +00001954 \var{key}. \var{key} must be hashable; if it isn't,
1955 \exception{TypeError} will be raised.
Georg Brandl99363b62005-09-03 07:27:26 +00001956 Return \code{0} on success or \code{-1} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00001957\end{cfuncdesc}
1958
1959\begin{cfuncdesc}{int}{PyDict_SetItemString}{PyObject *p,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001960 const char *key,
Fred Drake3adf79e2001-10-12 19:01:43 +00001961 PyObject *val}
Georg Brandl99363b62005-09-03 07:27:26 +00001962 Insert \var{value} into the dictionary \var{p} using \var{key} as a
Fred Drake3adf79e2001-10-12 19:01:43 +00001963 key. \var{key} should be a \ctype{char*}. The key object is created
Georg Brandl99363b62005-09-03 07:27:26 +00001964 using \code{PyString_FromString(\var{key})}. Return \code{0} on
Fred Drake3adf79e2001-10-12 19:01:43 +00001965 success or \code{-1} on failure.
1966 \ttindex{PyString_FromString()}
1967\end{cfuncdesc}
1968
1969\begin{cfuncdesc}{int}{PyDict_DelItem}{PyObject *p, PyObject *key}
Georg Brandl99363b62005-09-03 07:27:26 +00001970 Remove the entry in dictionary \var{p} with key \var{key}.
Fred Drake3adf79e2001-10-12 19:01:43 +00001971 \var{key} must be hashable; if it isn't, \exception{TypeError} is
Georg Brandl99363b62005-09-03 07:27:26 +00001972 raised. Return \code{0} on success or \code{-1} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00001973\end{cfuncdesc}
1974
1975\begin{cfuncdesc}{int}{PyDict_DelItemString}{PyObject *p, char *key}
Georg Brandl99363b62005-09-03 07:27:26 +00001976 Remove the entry in dictionary \var{p} which has a key specified by
1977 the string \var{key}. Return \code{0} on success or \code{-1} on
Fred Drake3adf79e2001-10-12 19:01:43 +00001978 failure.
1979\end{cfuncdesc}
1980
1981\begin{cfuncdesc}{PyObject*}{PyDict_GetItem}{PyObject *p, PyObject *key}
Georg Brandl99363b62005-09-03 07:27:26 +00001982 Return the object from dictionary \var{p} which has a key
1983 \var{key}. Return \NULL{} if the key \var{key} is not present, but
Fred Drake3adf79e2001-10-12 19:01:43 +00001984 \emph{without} setting an exception.
1985\end{cfuncdesc}
1986
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001987\begin{cfuncdesc}{PyObject*}{PyDict_GetItemString}{PyObject *p, const char *key}
Fred Drake3adf79e2001-10-12 19:01:43 +00001988 This is the same as \cfunction{PyDict_GetItem()}, but \var{key} is
1989 specified as a \ctype{char*}, rather than a \ctype{PyObject*}.
1990\end{cfuncdesc}
1991
1992\begin{cfuncdesc}{PyObject*}{PyDict_Items}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00001993 Return a \ctype{PyListObject} containing all the items from the
Nicholas Bastin975e7252004-09-29 21:39:26 +00001994 dictionary, as in the dictionary method \method{items()} (see the
Fred Drake3adf79e2001-10-12 19:01:43 +00001995 \citetitle[../lib/lib.html]{Python Library Reference}).
1996\end{cfuncdesc}
1997
1998\begin{cfuncdesc}{PyObject*}{PyDict_Keys}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00001999 Return a \ctype{PyListObject} containing all the keys from the
Fred Drake3adf79e2001-10-12 19:01:43 +00002000 dictionary, as in the dictionary method \method{keys()} (see the
2001 \citetitle[../lib/lib.html]{Python Library Reference}).
2002\end{cfuncdesc}
2003
2004\begin{cfuncdesc}{PyObject*}{PyDict_Values}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002005 Return a \ctype{PyListObject} containing all the values from the
Fred Drake3adf79e2001-10-12 19:01:43 +00002006 dictionary \var{p}, as in the dictionary method \method{values()}
2007 (see the \citetitle[../lib/lib.html]{Python Library Reference}).
2008\end{cfuncdesc}
2009
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002010\begin{cfuncdesc}{Py_ssize_t}{PyDict_Size}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002011 Return the number of items in the dictionary. This is equivalent
Fred Drake3adf79e2001-10-12 19:01:43 +00002012 to \samp{len(\var{p})} on a dictionary.\bifuncindex{len}
2013\end{cfuncdesc}
2014
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002015\begin{cfuncdesc}{int}{PyDict_Next}{PyObject *p, Py_ssize_t *ppos,
Fred Drake3adf79e2001-10-12 19:01:43 +00002016 PyObject **pkey, PyObject **pvalue}
2017 Iterate over all key-value pairs in the dictionary \var{p}. The
2018 \ctype{int} referred to by \var{ppos} must be initialized to
2019 \code{0} prior to the first call to this function to start the
2020 iteration; the function returns true for each pair in the
2021 dictionary, and false once all pairs have been reported. The
2022 parameters \var{pkey} and \var{pvalue} should either point to
2023 \ctype{PyObject*} variables that will be filled in with each key and
Tim Peters9ddf40b2004-06-20 22:41:32 +00002024 value, respectively, or may be \NULL{}. Any references returned through
Raymond Hettinger54693242003-12-13 19:48:41 +00002025 them are borrowed. \var{ppos} should not be altered during iteration.
2026 Its value represents offsets within the internal dictionary structure,
2027 and since the structure is sparse, the offsets are not consecutive.
Fred Drake3adf79e2001-10-12 19:01:43 +00002028
2029 For example:
2030
2031\begin{verbatim}
2032PyObject *key, *value;
2033int pos = 0;
2034
2035while (PyDict_Next(self->dict, &pos, &key, &value)) {
2036 /* do something interesting with the values... */
2037 ...
2038}
2039\end{verbatim}
2040
2041 The dictionary \var{p} should not be mutated during iteration. It
2042 is safe (since Python 2.1) to modify the values of the keys as you
2043 iterate over the dictionary, but only so long as the set of keys
2044 does not change. For example:
2045
2046\begin{verbatim}
2047PyObject *key, *value;
2048int pos = 0;
2049
2050while (PyDict_Next(self->dict, &pos, &key, &value)) {
2051 int i = PyInt_AS_LONG(value) + 1;
2052 PyObject *o = PyInt_FromLong(i);
2053 if (o == NULL)
2054 return -1;
2055 if (PyDict_SetItem(self->dict, key, o) < 0) {
2056 Py_DECREF(o);
2057 return -1;
2058 }
2059 Py_DECREF(o);
2060}
2061\end{verbatim}
2062\end{cfuncdesc}
2063
2064\begin{cfuncdesc}{int}{PyDict_Merge}{PyObject *a, PyObject *b, int override}
Tim Petersf582b822001-12-11 18:51:08 +00002065 Iterate over mapping object \var{b} adding key-value pairs to dictionary
2066 \var{a}.
2067 \var{b} may be a dictionary, or any object supporting
2068 \function{PyMapping_Keys()} and \function{PyObject_GetItem()}.
2069 If \var{override} is true, existing pairs in \var{a} will
Fred Drake3adf79e2001-10-12 19:01:43 +00002070 be replaced if a matching key is found in \var{b}, otherwise pairs
2071 will only be added if there is not a matching key in \var{a}.
Tim Petersf582b822001-12-11 18:51:08 +00002072 Return \code{0} on success or \code{-1} if an exception was
Fred Drake3adf79e2001-10-12 19:01:43 +00002073 raised.
2074\versionadded{2.2}
2075\end{cfuncdesc}
2076
2077\begin{cfuncdesc}{int}{PyDict_Update}{PyObject *a, PyObject *b}
2078 This is the same as \code{PyDict_Merge(\var{a}, \var{b}, 1)} in C,
Tim Petersf582b822001-12-11 18:51:08 +00002079 or \code{\var{a}.update(\var{b})} in Python. Return \code{0} on
Fred Drake3adf79e2001-10-12 19:01:43 +00002080 success or \code{-1} if an exception was raised.
2081 \versionadded{2.2}
2082\end{cfuncdesc}
2083
Tim Petersf582b822001-12-11 18:51:08 +00002084\begin{cfuncdesc}{int}{PyDict_MergeFromSeq2}{PyObject *a, PyObject *seq2,
2085 int override}
2086 Update or merge into dictionary \var{a}, from the key-value pairs in
2087 \var{seq2}. \var{seq2} must be an iterable object producing
2088 iterable objects of length 2, viewed as key-value pairs. In case of
2089 duplicate keys, the last wins if \var{override} is true, else the
2090 first wins.
2091 Return \code{0} on success or \code{-1} if an exception
2092 was raised.
2093 Equivalent Python (except for the return value):
2094
2095\begin{verbatim}
2096def PyDict_MergeFromSeq2(a, seq2, override):
2097 for key, value in seq2:
2098 if override or key not in a:
2099 a[key] = value
2100\end{verbatim}
2101
2102 \versionadded{2.2}
2103\end{cfuncdesc}
Fred Drake3adf79e2001-10-12 19:01:43 +00002104
Fred Drake54e62942001-12-11 19:40:16 +00002105
Fred Drake3adf79e2001-10-12 19:01:43 +00002106\section{Other Objects \label{otherObjects}}
2107
2108\subsection{File Objects \label{fileObjects}}
2109
2110\obindex{file}
2111Python's built-in file objects are implemented entirely on the
2112\ctype{FILE*} support from the C standard library. This is an
2113implementation detail and may change in future releases of Python.
2114
2115\begin{ctypedesc}{PyFileObject}
2116 This subtype of \ctype{PyObject} represents a Python file object.
2117\end{ctypedesc}
2118
2119\begin{cvardesc}{PyTypeObject}{PyFile_Type}
2120 This instance of \ctype{PyTypeObject} represents the Python file
2121 type. This is exposed to Python programs as \code{types.FileType}.
2122 \withsubitem{(in module types)}{\ttindex{FileType}}
2123\end{cvardesc}
2124
2125\begin{cfuncdesc}{int}{PyFile_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002126 Return true if its argument is a \ctype{PyFileObject} or a subtype
Fred Drake3adf79e2001-10-12 19:01:43 +00002127 of \ctype{PyFileObject}.
2128 \versionchanged[Allowed subtypes to be accepted]{2.2}
2129\end{cfuncdesc}
2130
2131\begin{cfuncdesc}{int}{PyFile_CheckExact}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002132 Return true if its argument is a \ctype{PyFileObject}, but not a
Fred Drake3adf79e2001-10-12 19:01:43 +00002133 subtype of \ctype{PyFileObject}.
2134 \versionadded{2.2}
2135\end{cfuncdesc}
2136
2137\begin{cfuncdesc}{PyObject*}{PyFile_FromString}{char *filename, char *mode}
Georg Brandl99363b62005-09-03 07:27:26 +00002138 On success, return a new file object that is opened on the file
Fred Drake3adf79e2001-10-12 19:01:43 +00002139 given by \var{filename}, with a file mode given by \var{mode}, where
2140 \var{mode} has the same semantics as the standard C routine
Georg Brandl99363b62005-09-03 07:27:26 +00002141 \cfunction{fopen()}\ttindex{fopen()}. On failure, return \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002142\end{cfuncdesc}
2143
2144\begin{cfuncdesc}{PyObject*}{PyFile_FromFile}{FILE *fp,
2145 char *name, char *mode,
2146 int (*close)(FILE*)}
Georg Brandl99363b62005-09-03 07:27:26 +00002147 Create a new \ctype{PyFileObject} from the already-open standard C
Fred Drake3adf79e2001-10-12 19:01:43 +00002148 file pointer, \var{fp}. The function \var{close} will be called
Georg Brandl99363b62005-09-03 07:27:26 +00002149 when the file should be closed. Return \NULL{} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00002150\end{cfuncdesc}
2151
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002152\begin{cfuncdesc}{FILE*}{PyFile_AsFile}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002153 Return the file object associated with \var{p} as a \ctype{FILE*}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002154\end{cfuncdesc}
2155
2156\begin{cfuncdesc}{PyObject*}{PyFile_GetLine}{PyObject *p, int n}
2157 Equivalent to \code{\var{p}.readline(\optional{\var{n}})}, this
2158 function reads one line from the object \var{p}. \var{p} may be a
2159 file object or any object with a \method{readline()} method. If
2160 \var{n} is \code{0}, exactly one line is read, regardless of the
2161 length of the line. If \var{n} is greater than \code{0}, no more
2162 than \var{n} bytes will be read from the file; a partial line can be
2163 returned. In both cases, an empty string is returned if the end of
2164 the file is reached immediately. If \var{n} is less than \code{0},
2165 however, one line is read regardless of length, but
2166 \exception{EOFError} is raised if the end of the file is reached
2167 immediately.
2168 \withsubitem{(built-in exception)}{\ttindex{EOFError}}
2169\end{cfuncdesc}
2170
2171\begin{cfuncdesc}{PyObject*}{PyFile_Name}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002172 Return the name of the file specified by \var{p} as a string
Fred Drake3adf79e2001-10-12 19:01:43 +00002173 object.
2174\end{cfuncdesc}
2175
2176\begin{cfuncdesc}{void}{PyFile_SetBufSize}{PyFileObject *p, int n}
2177 Available on systems with \cfunction{setvbuf()}\ttindex{setvbuf()}
2178 only. This should only be called immediately after file object
2179 creation.
2180\end{cfuncdesc}
2181
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00002182\begin{cfuncdesc}{int}{PyFile_Encoding}{PyFileObject *p, char *enc}
2183 Set the file's encoding for Unicode output to \var{enc}. Return
2184 1 on success and 0 on failure.
2185 \versionadded{2.3}
2186\end{cfuncdesc}
2187
Fred Drake3adf79e2001-10-12 19:01:43 +00002188\begin{cfuncdesc}{int}{PyFile_SoftSpace}{PyObject *p, int newflag}
Georg Brandl99363b62005-09-03 07:27:26 +00002189 This function exists for internal use by the interpreter. Set the
Fred Drake3adf79e2001-10-12 19:01:43 +00002190 \member{softspace} attribute of \var{p} to \var{newflag} and
Georg Brandl99363b62005-09-03 07:27:26 +00002191 \withsubitem{(file attribute)}{\ttindex{softspace}}return the
Fred Drake3adf79e2001-10-12 19:01:43 +00002192 previous value. \var{p} does not have to be a file object for this
2193 function to work properly; any object is supported (thought its only
2194 interesting if the \member{softspace} attribute can be set). This
2195 function clears any errors, and will return \code{0} as the previous
2196 value if the attribute either does not exist or if there were errors
2197 in retrieving it. There is no way to detect errors from this
2198 function, but doing so should not be needed.
2199\end{cfuncdesc}
2200
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002201\begin{cfuncdesc}{int}{PyFile_WriteObject}{PyObject *obj, PyObject *p,
Fred Drake3adf79e2001-10-12 19:01:43 +00002202 int flags}
Georg Brandl99363b62005-09-03 07:27:26 +00002203 Write object \var{obj} to file object \var{p}. The only supported
Fred Drake3adf79e2001-10-12 19:01:43 +00002204 flag for \var{flags} is
2205 \constant{Py_PRINT_RAW}\ttindex{Py_PRINT_RAW}; if given, the
2206 \function{str()} of the object is written instead of the
Georg Brandl99363b62005-09-03 07:27:26 +00002207 \function{repr()}. Return \code{0} on success or \code{-1} on
Fred Drake3adf79e2001-10-12 19:01:43 +00002208 failure; the appropriate exception will be set.
2209\end{cfuncdesc}
2210
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002211\begin{cfuncdesc}{int}{PyFile_WriteString}{const char *s, PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002212 Write string \var{s} to file object \var{p}. Return \code{0} on
Fred Drake3adf79e2001-10-12 19:01:43 +00002213 success or \code{-1} on failure; the appropriate exception will be
2214 set.
2215\end{cfuncdesc}
2216
2217
2218\subsection{Instance Objects \label{instanceObjects}}
2219
2220\obindex{instance}
2221There are very few functions specific to instance objects.
2222
2223\begin{cvardesc}{PyTypeObject}{PyInstance_Type}
2224 Type object for class instances.
2225\end{cvardesc}
2226
2227\begin{cfuncdesc}{int}{PyInstance_Check}{PyObject *obj}
Georg Brandl99363b62005-09-03 07:27:26 +00002228 Return true if \var{obj} is an instance.
Fred Drake3adf79e2001-10-12 19:01:43 +00002229\end{cfuncdesc}
2230
2231\begin{cfuncdesc}{PyObject*}{PyInstance_New}{PyObject *class,
2232 PyObject *arg,
2233 PyObject *kw}
2234 Create a new instance of a specific class. The parameters \var{arg}
2235 and \var{kw} are used as the positional and keyword parameters to
2236 the object's constructor.
2237\end{cfuncdesc}
2238
2239\begin{cfuncdesc}{PyObject*}{PyInstance_NewRaw}{PyObject *class,
2240 PyObject *dict}
Neil Schemenauerc4932292005-06-18 17:54:13 +00002241 Create a new instance of a specific class without calling its
Fred Drake3adf79e2001-10-12 19:01:43 +00002242 constructor. \var{class} is the class of new object. The
2243 \var{dict} parameter will be used as the object's \member{__dict__};
Tim Peters9ddf40b2004-06-20 22:41:32 +00002244 if \NULL{}, a new dictionary will be created for the instance.
Fred Drake3adf79e2001-10-12 19:01:43 +00002245\end{cfuncdesc}
2246
2247
Georg Brandl9b743f52006-02-20 12:57:53 +00002248\subsection{Function Objects \label{function-objects}}
2249
2250\obindex{function}
2251There are a few functions specific to Python functions.
2252
2253\begin{ctypedesc}{PyFunctionObject}
2254 The C structure used for functions.
2255\end{ctypedesc}
2256
2257\begin{cvardesc}{PyTypeObject}{PyFunction_Type}
2258 This is an instance of \ctype{PyTypeObject} and represents the
2259 Python function type. It is exposed to Python programmers as
2260 \code{types.FunctionType}.
2261 \withsubitem{(in module types)}{\ttindex{MethodType}}
2262\end{cvardesc}
2263
2264\begin{cfuncdesc}{int}{PyFunction_Check}{PyObject *o}
2265 Return true if \var{o} is a function object (has type
2266 \cdata{PyFunction_Type}). The parameter must not be \NULL{}.
2267\end{cfuncdesc}
2268
2269\begin{cfuncdesc}{PyObject*}{PyFunction_New}{PyObject *code,
2270 PyObject *globals}
2271 Return a new function object associated with the code object
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002272 \var{code}. \var{globals} must be a dictionary with the global
2273 variables accessible to the function.
Georg Brandl9b743f52006-02-20 12:57:53 +00002274
2275 The function's docstring, name and \var{__module__} are retrieved
2276 from the code object, the argument defaults and closure are set to
2277 \NULL{}.
2278\end{cfuncdesc}
2279
2280\begin{cfuncdesc}{PyObject*}{PyFunction_GetCode}{PyObject *op}
2281 Return the code object associated with the function object \var{op}.
2282\end{cfuncdesc}
2283
2284\begin{cfuncdesc}{PyObject*}{PyFunction_GetGlobals}{PyObject *op}
2285 Return the globals dictionary associated with the function object
2286 \var{op}.
2287\end{cfuncdesc}
2288
2289\begin{cfuncdesc}{PyObject*}{PyFunction_GetModule}{PyObject *op}
2290 Return the \var{__module__} attribute of the function object \var{op}.
2291 This is normally a string containing the module name, but can be set
2292 to any other object by Python code.
2293\end{cfuncdesc}
2294
2295\begin{cfuncdesc}{PyObject*}{PyFunction_GetDefaults}{PyObject *op}
2296 Return the argument default values of the function object \var{op}.
2297 This can be a tuple of arguments or \NULL{}.
2298\end{cfuncdesc}
2299
2300\begin{cfuncdesc}{int}{PyFunction_SetDefaults}{PyObject *op,
2301 PyObject *defaults}
2302 Set the argument default values for the function object \var{op}.
2303 \var{defaults} must be \var{Py_None} or a tuple.
2304
2305 Raises \exception{SystemError} and returns \code{-1} on failure.
2306\end{cfuncdesc}
2307
2308\begin{cfuncdesc}{PyObject*}{PyFunction_GetClosure}{PyObject *op}
2309 Return the closure associated with the function object \var{op}.
2310 This can be \NULL{} or a tuple of cell objects.
2311\end{cfuncdesc}
2312
2313\begin{cfuncdesc}{int}{PyFunction_SetClosure}{PyObject *op,
2314 PyObject *closure}
2315 Set the closure associated with the function object \var{op}.
2316 \var{closure} must be \var{Py_None} or a tuple of cell objects.
2317
2318 Raises \exception{SystemError} and returns \code{-1} on failure.
2319\end{cfuncdesc}
2320
2321
Fred Drake3adf79e2001-10-12 19:01:43 +00002322\subsection{Method Objects \label{method-objects}}
2323
2324\obindex{method}
2325There are some useful functions that are useful for working with
2326method objects.
2327
2328\begin{cvardesc}{PyTypeObject}{PyMethod_Type}
2329 This instance of \ctype{PyTypeObject} represents the Python method
2330 type. This is exposed to Python programs as \code{types.MethodType}.
2331 \withsubitem{(in module types)}{\ttindex{MethodType}}
2332\end{cvardesc}
2333
2334\begin{cfuncdesc}{int}{PyMethod_Check}{PyObject *o}
2335 Return true if \var{o} is a method object (has type
Tim Peters9ddf40b2004-06-20 22:41:32 +00002336 \cdata{PyMethod_Type}). The parameter must not be \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002337\end{cfuncdesc}
2338
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002339\begin{cfuncdesc}{PyObject*}{PyMethod_New}{PyObject *func,
Fred Drake3adf79e2001-10-12 19:01:43 +00002340 PyObject *self, PyObject *class}
2341 Return a new method object, with \var{func} being any callable
2342 object; this is the function that will be called when the method is
2343 called. If this method should be bound to an instance, \var{self}
2344 should be the instance and \var{class} should be the class of
2345 \var{self}, otherwise \var{self} should be \NULL{} and \var{class}
2346 should be the class which provides the unbound method..
2347\end{cfuncdesc}
2348
2349\begin{cfuncdesc}{PyObject*}{PyMethod_Class}{PyObject *meth}
2350 Return the class object from which the method \var{meth} was
2351 created; if this was created from an instance, it will be the class
2352 of the instance.
2353\end{cfuncdesc}
2354
2355\begin{cfuncdesc}{PyObject*}{PyMethod_GET_CLASS}{PyObject *meth}
2356 Macro version of \cfunction{PyMethod_Class()} which avoids error
2357 checking.
2358\end{cfuncdesc}
2359
2360\begin{cfuncdesc}{PyObject*}{PyMethod_Function}{PyObject *meth}
2361 Return the function object associated with the method \var{meth}.
2362\end{cfuncdesc}
2363
2364\begin{cfuncdesc}{PyObject*}{PyMethod_GET_FUNCTION}{PyObject *meth}
2365 Macro version of \cfunction{PyMethod_Function()} which avoids error
2366 checking.
2367\end{cfuncdesc}
2368
2369\begin{cfuncdesc}{PyObject*}{PyMethod_Self}{PyObject *meth}
2370 Return the instance associated with the method \var{meth} if it is
Tim Peters9ddf40b2004-06-20 22:41:32 +00002371 bound, otherwise return \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002372\end{cfuncdesc}
2373
2374\begin{cfuncdesc}{PyObject*}{PyMethod_GET_SELF}{PyObject *meth}
2375 Macro version of \cfunction{PyMethod_Self()} which avoids error
2376 checking.
2377\end{cfuncdesc}
2378
2379
2380\subsection{Module Objects \label{moduleObjects}}
2381
2382\obindex{module}
2383There are only a few functions special to module objects.
2384
2385\begin{cvardesc}{PyTypeObject}{PyModule_Type}
2386 This instance of \ctype{PyTypeObject} represents the Python module
2387 type. This is exposed to Python programs as
2388 \code{types.ModuleType}.
2389 \withsubitem{(in module types)}{\ttindex{ModuleType}}
2390\end{cvardesc}
2391
2392\begin{cfuncdesc}{int}{PyModule_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002393 Return true if \var{p} is a module object, or a subtype of a module
Fred Drake3adf79e2001-10-12 19:01:43 +00002394 object.
2395 \versionchanged[Allowed subtypes to be accepted]{2.2}
2396\end{cfuncdesc}
2397
2398\begin{cfuncdesc}{int}{PyModule_CheckExact}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002399 Return true if \var{p} is a module object, but not a subtype of
Fred Drake3adf79e2001-10-12 19:01:43 +00002400 \cdata{PyModule_Type}.
2401 \versionadded{2.2}
2402\end{cfuncdesc}
2403
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002404\begin{cfuncdesc}{PyObject*}{PyModule_New}{const char *name}
Fred Drake3adf79e2001-10-12 19:01:43 +00002405 Return a new module object with the \member{__name__} attribute set
2406 to \var{name}. Only the module's \member{__doc__} and
2407 \member{__name__} attributes are filled in; the caller is
2408 responsible for providing a \member{__file__} attribute.
2409 \withsubitem{(module attribute)}{
2410 \ttindex{__name__}\ttindex{__doc__}\ttindex{__file__}}
2411\end{cfuncdesc}
2412
2413\begin{cfuncdesc}{PyObject*}{PyModule_GetDict}{PyObject *module}
2414 Return the dictionary object that implements \var{module}'s
2415 namespace; this object is the same as the \member{__dict__}
2416 attribute of the module object. This function never fails.
2417 \withsubitem{(module attribute)}{\ttindex{__dict__}}
Fred Drakef495ef72002-04-12 19:32:07 +00002418 It is recommended extensions use other \cfunction{PyModule_*()}
2419 and \cfunction{PyObject_*()} functions rather than directly
2420 manipulate a module's \member{__dict__}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002421\end{cfuncdesc}
2422
2423\begin{cfuncdesc}{char*}{PyModule_GetName}{PyObject *module}
2424 Return \var{module}'s \member{__name__} value. If the module does
2425 not provide one, or if it is not a string, \exception{SystemError}
2426 is raised and \NULL{} is returned.
2427 \withsubitem{(module attribute)}{\ttindex{__name__}}
2428 \withsubitem{(built-in exception)}{\ttindex{SystemError}}
2429\end{cfuncdesc}
2430
2431\begin{cfuncdesc}{char*}{PyModule_GetFilename}{PyObject *module}
2432 Return the name of the file from which \var{module} was loaded using
2433 \var{module}'s \member{__file__} attribute. If this is not defined,
2434 or if it is not a string, raise \exception{SystemError} and return
Tim Peters9ddf40b2004-06-20 22:41:32 +00002435 \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002436 \withsubitem{(module attribute)}{\ttindex{__file__}}
2437 \withsubitem{(built-in exception)}{\ttindex{SystemError}}
2438\end{cfuncdesc}
2439
2440\begin{cfuncdesc}{int}{PyModule_AddObject}{PyObject *module,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002441 const char *name, PyObject *value}
Fred Drake3adf79e2001-10-12 19:01:43 +00002442 Add an object to \var{module} as \var{name}. This is a convenience
2443 function which can be used from the module's initialization
Georg Brandl99363b62005-09-03 07:27:26 +00002444 function. This steals a reference to \var{value}. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00002445 \code{-1} on error, \code{0} on success.
2446 \versionadded{2.0}
2447\end{cfuncdesc}
2448
2449\begin{cfuncdesc}{int}{PyModule_AddIntConstant}{PyObject *module,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002450 const char *name, long value}
Fred Drake3adf79e2001-10-12 19:01:43 +00002451 Add an integer constant to \var{module} as \var{name}. This
2452 convenience function can be used from the module's initialization
Georg Brandl99363b62005-09-03 07:27:26 +00002453 function. Return \code{-1} on error, \code{0} on success.
Fred Drake3adf79e2001-10-12 19:01:43 +00002454 \versionadded{2.0}
2455\end{cfuncdesc}
2456
2457\begin{cfuncdesc}{int}{PyModule_AddStringConstant}{PyObject *module,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002458 const char *name, const char *value}
Fred Drake3adf79e2001-10-12 19:01:43 +00002459 Add a string constant to \var{module} as \var{name}. This
2460 convenience function can be used from the module's initialization
Georg Brandl99363b62005-09-03 07:27:26 +00002461 function. The string \var{value} must be null-terminated. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00002462 \code{-1} on error, \code{0} on success.
2463 \versionadded{2.0}
2464\end{cfuncdesc}
2465
2466
2467\subsection{Iterator Objects \label{iterator-objects}}
2468
2469Python provides two general-purpose iterator objects. The first, a
2470sequence iterator, works with an arbitrary sequence supporting the
2471\method{__getitem__()} method. The second works with a callable
2472object and a sentinel value, calling the callable for each item in the
2473sequence, and ending the iteration when the sentinel value is
2474returned.
2475
2476\begin{cvardesc}{PyTypeObject}{PySeqIter_Type}
2477 Type object for iterator objects returned by
2478 \cfunction{PySeqIter_New()} and the one-argument form of the
2479 \function{iter()} built-in function for built-in sequence types.
2480 \versionadded{2.2}
2481\end{cvardesc}
2482
2483\begin{cfuncdesc}{int}{PySeqIter_Check}{op}
2484 Return true if the type of \var{op} is \cdata{PySeqIter_Type}.
2485 \versionadded{2.2}
2486\end{cfuncdesc}
2487
2488\begin{cfuncdesc}{PyObject*}{PySeqIter_New}{PyObject *seq}
2489 Return an iterator that works with a general sequence object,
2490 \var{seq}. The iteration ends when the sequence raises
2491 \exception{IndexError} for the subscripting operation.
2492 \versionadded{2.2}
2493\end{cfuncdesc}
2494
2495\begin{cvardesc}{PyTypeObject}{PyCallIter_Type}
2496 Type object for iterator objects returned by
2497 \cfunction{PyCallIter_New()} and the two-argument form of the
2498 \function{iter()} built-in function.
2499 \versionadded{2.2}
2500\end{cvardesc}
2501
2502\begin{cfuncdesc}{int}{PyCallIter_Check}{op}
2503 Return true if the type of \var{op} is \cdata{PyCallIter_Type}.
2504 \versionadded{2.2}
2505\end{cfuncdesc}
2506
2507\begin{cfuncdesc}{PyObject*}{PyCallIter_New}{PyObject *callable,
2508 PyObject *sentinel}
2509 Return a new iterator. The first parameter, \var{callable}, can be
2510 any Python callable object that can be called with no parameters;
2511 each call to it should return the next item in the iteration. When
2512 \var{callable} returns a value equal to \var{sentinel}, the
2513 iteration will be terminated.
2514 \versionadded{2.2}
2515\end{cfuncdesc}
2516
2517
2518\subsection{Descriptor Objects \label{descriptor-objects}}
2519
Fred Drake54e62942001-12-11 19:40:16 +00002520``Descriptors'' are objects that describe some attribute of an object.
2521They are found in the dictionary of type objects.
2522
Fred Drake3adf79e2001-10-12 19:01:43 +00002523\begin{cvardesc}{PyTypeObject}{PyProperty_Type}
Fred Drake54e62942001-12-11 19:40:16 +00002524 The type object for the built-in descriptor types.
Fred Drake3adf79e2001-10-12 19:01:43 +00002525 \versionadded{2.2}
2526\end{cvardesc}
2527
2528\begin{cfuncdesc}{PyObject*}{PyDescr_NewGetSet}{PyTypeObject *type,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002529 struct PyGetSetDef *getset}
Fred Drake3adf79e2001-10-12 19:01:43 +00002530 \versionadded{2.2}
2531\end{cfuncdesc}
2532
2533\begin{cfuncdesc}{PyObject*}{PyDescr_NewMember}{PyTypeObject *type,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002534 struct PyMemberDef *meth}
Fred Drake3adf79e2001-10-12 19:01:43 +00002535 \versionadded{2.2}
2536\end{cfuncdesc}
2537
2538\begin{cfuncdesc}{PyObject*}{PyDescr_NewMethod}{PyTypeObject *type,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002539 struct PyMethodDef *meth}
Fred Drake3adf79e2001-10-12 19:01:43 +00002540 \versionadded{2.2}
2541\end{cfuncdesc}
2542
2543\begin{cfuncdesc}{PyObject*}{PyDescr_NewWrapper}{PyTypeObject *type,
2544 struct wrapperbase *wrapper,
2545 void *wrapped}
2546 \versionadded{2.2}
2547\end{cfuncdesc}
2548
Thomas Heller8178a222004-02-09 10:47:11 +00002549\begin{cfuncdesc}{PyObject*}{PyDescr_NewClassMethod}{PyTypeObject *type,
2550 PyMethodDef *method}
2551 \versionadded{2.3}
2552\end{cfuncdesc}
2553
Fred Drake3adf79e2001-10-12 19:01:43 +00002554\begin{cfuncdesc}{int}{PyDescr_IsData}{PyObject *descr}
Georg Brandl99363b62005-09-03 07:27:26 +00002555 Return true if the descriptor objects \var{descr} describes a data
Fred Drake3adf79e2001-10-12 19:01:43 +00002556 attribute, or false if it describes a method. \var{descr} must be a
2557 descriptor object; there is no error checking.
2558 \versionadded{2.2}
2559\end{cfuncdesc}
2560
2561\begin{cfuncdesc}{PyObject*}{PyWrapper_New}{PyObject *, PyObject *}
2562 \versionadded{2.2}
2563\end{cfuncdesc}
2564
2565
2566\subsection{Slice Objects \label{slice-objects}}
2567
2568\begin{cvardesc}{PyTypeObject}{PySlice_Type}
2569 The type object for slice objects. This is the same as
2570 \code{types.SliceType}.
2571 \withsubitem{(in module types)}{\ttindex{SliceType}}
2572\end{cvardesc}
2573
2574\begin{cfuncdesc}{int}{PySlice_Check}{PyObject *ob}
Georg Brandl99363b62005-09-03 07:27:26 +00002575 Return true if \var{ob} is a slice object; \var{ob} must not be
Tim Peters9ddf40b2004-06-20 22:41:32 +00002576 \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002577\end{cfuncdesc}
2578
2579\begin{cfuncdesc}{PyObject*}{PySlice_New}{PyObject *start, PyObject *stop,
2580 PyObject *step}
2581 Return a new slice object with the given values. The \var{start},
2582 \var{stop}, and \var{step} parameters are used as the values of the
2583 slice object attributes of the same names. Any of the values may be
Tim Peters9ddf40b2004-06-20 22:41:32 +00002584 \NULL{}, in which case the \code{None} will be used for the
Georg Brandl99363b62005-09-03 07:27:26 +00002585 corresponding attribute. Return \NULL{} if the new object could
Fred Drake3adf79e2001-10-12 19:01:43 +00002586 not be allocated.
2587\end{cfuncdesc}
2588
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002589\begin{cfuncdesc}{int}{PySlice_GetIndices}{PySliceObject *slice, Py_ssize_t length,
2590 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step}
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002591Retrieve the start, stop and step indices from the slice object
2592\var{slice}, assuming a sequence of length \var{length}. Treats
2593indices greater than \var{length} as errors.
2594
2595Returns 0 on success and -1 on error with no exception set (unless one
2596of the indices was not \constant{None} and failed to be converted to
2597an integer, in which case -1 is returned with an exception set).
2598
2599You probably do not want to use this function. If you want to use
2600slice objects in versions of Python prior to 2.3, you would probably
2601do well to incorporate the source of \cfunction{PySlice_GetIndicesEx},
2602suitably renamed, in the source of your extension.
2603\end{cfuncdesc}
2604
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002605\begin{cfuncdesc}{int}{PySlice_GetIndicesEx}{PySliceObject *slice, Py_ssize_t length,
2606 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step,
2607 Py_ssize_t *slicelength}
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002608Usable replacement for \cfunction{PySlice_GetIndices}. Retrieve the
2609start, stop, and step indices from the slice object \var{slice}
2610assuming a sequence of length \var{length}, and store the length of
2611the slice in \var{slicelength}. Out of bounds indices are clipped in
2612a manner consistent with the handling of normal slices.
2613
2614Returns 0 on success and -1 on error with exception set.
2615
2616\versionadded{2.3}
Fred Drake3adf79e2001-10-12 19:01:43 +00002617\end{cfuncdesc}
2618
2619
2620\subsection{Weak Reference Objects \label{weakref-objects}}
2621
2622Python supports \emph{weak references} as first-class objects. There
2623are two specific object types which directly implement weak
2624references. The first is a simple reference object, and the second
2625acts as a proxy for the original object as much as it can.
2626
2627\begin{cfuncdesc}{int}{PyWeakref_Check}{ob}
2628 Return true if \var{ob} is either a reference or proxy object.
2629 \versionadded{2.2}
2630\end{cfuncdesc}
2631
2632\begin{cfuncdesc}{int}{PyWeakref_CheckRef}{ob}
2633 Return true if \var{ob} is a reference object.
2634 \versionadded{2.2}
2635\end{cfuncdesc}
2636
2637\begin{cfuncdesc}{int}{PyWeakref_CheckProxy}{ob}
2638 Return true if \var{ob} is a proxy object.
2639 \versionadded{2.2}
2640\end{cfuncdesc}
2641
2642\begin{cfuncdesc}{PyObject*}{PyWeakref_NewRef}{PyObject *ob,
2643 PyObject *callback}
2644 Return a weak reference object for the object \var{ob}. This will
2645 always return a new reference, but is not guaranteed to create a new
2646 object; an existing reference object may be returned. The second
2647 parameter, \var{callback}, can be a callable object that receives
2648 notification when \var{ob} is garbage collected; it should accept a
Raymond Hettinger5232f502004-03-25 08:51:36 +00002649 single parameter, which will be the weak reference object itself.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002650 \var{callback} may also be \code{None} or \NULL{}. If \var{ob}
Fred Drake3adf79e2001-10-12 19:01:43 +00002651 is not a weakly-referencable object, or if \var{callback} is not
Tim Peters9ddf40b2004-06-20 22:41:32 +00002652 callable, \code{None}, or \NULL{}, this will return \NULL{} and
Fred Drake3adf79e2001-10-12 19:01:43 +00002653 raise \exception{TypeError}.
2654 \versionadded{2.2}
2655\end{cfuncdesc}
2656
2657\begin{cfuncdesc}{PyObject*}{PyWeakref_NewProxy}{PyObject *ob,
2658 PyObject *callback}
2659 Return a weak reference proxy object for the object \var{ob}. This
2660 will always return a new reference, but is not guaranteed to create
2661 a new object; an existing proxy object may be returned. The second
2662 parameter, \var{callback}, can be a callable object that receives
2663 notification when \var{ob} is garbage collected; it should accept a
Raymond Hettinger5232f502004-03-25 08:51:36 +00002664 single parameter, which will be the weak reference object itself.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002665 \var{callback} may also be \code{None} or \NULL{}. If \var{ob} is not
Fred Drake3adf79e2001-10-12 19:01:43 +00002666 a weakly-referencable object, or if \var{callback} is not callable,
Tim Peters9ddf40b2004-06-20 22:41:32 +00002667 \code{None}, or \NULL{}, this will return \NULL{} and raise
Fred Drake3adf79e2001-10-12 19:01:43 +00002668 \exception{TypeError}.
2669 \versionadded{2.2}
2670\end{cfuncdesc}
2671
2672\begin{cfuncdesc}{PyObject*}{PyWeakref_GetObject}{PyObject *ref}
Georg Brandl99363b62005-09-03 07:27:26 +00002673 Return the referenced object from a weak reference, \var{ref}. If
Ka-Ping Yeebd379e92003-03-28 18:07:16 +00002674 the referent is no longer live, returns \code{None}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002675 \versionadded{2.2}
2676\end{cfuncdesc}
2677
2678\begin{cfuncdesc}{PyObject*}{PyWeakref_GET_OBJECT}{PyObject *ref}
2679 Similar to \cfunction{PyWeakref_GetObject()}, but implemented as a
2680 macro that does no error checking.
2681 \versionadded{2.2}
2682\end{cfuncdesc}
2683
2684
2685\subsection{CObjects \label{cObjects}}
2686
2687\obindex{CObject}
2688Refer to \emph{Extending and Embedding the Python Interpreter},
Fred Drake54e62942001-12-11 19:40:16 +00002689section~1.12, ``Providing a C API for an Extension Module,'' for more
Fred Drake3adf79e2001-10-12 19:01:43 +00002690information on using these objects.
2691
2692
2693\begin{ctypedesc}{PyCObject}
2694 This subtype of \ctype{PyObject} represents an opaque value, useful
2695 for C extension modules who need to pass an opaque value (as a
2696 \ctype{void*} pointer) through Python code to other C code. It is
2697 often used to make a C function pointer defined in one module
2698 available to other modules, so the regular import mechanism can be
2699 used to access C APIs defined in dynamically loaded modules.
2700\end{ctypedesc}
2701
2702\begin{cfuncdesc}{int}{PyCObject_Check}{PyObject *p}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002703 Return true if its argument is a \ctype{PyCObject}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002704\end{cfuncdesc}
2705
Tim Petersf582b822001-12-11 18:51:08 +00002706\begin{cfuncdesc}{PyObject*}{PyCObject_FromVoidPtr}{void* cobj,
Fred Drake54e62942001-12-11 19:40:16 +00002707 void (*destr)(void *)}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002708 Create a \ctype{PyCObject} from the \code{void *}\var{cobj}. The
Fred Drake3adf79e2001-10-12 19:01:43 +00002709 \var{destr} function will be called when the object is reclaimed,
Tim Peters9ddf40b2004-06-20 22:41:32 +00002710 unless it is \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002711\end{cfuncdesc}
2712
2713\begin{cfuncdesc}{PyObject*}{PyCObject_FromVoidPtrAndDesc}{void* cobj,
2714 void* desc, void (*destr)(void *, void *)}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002715 Create a \ctype{PyCObject} from the \ctype{void *}\var{cobj}. The
Fred Drake3adf79e2001-10-12 19:01:43 +00002716 \var{destr} function will be called when the object is reclaimed.
2717 The \var{desc} argument can be used to pass extra callback data for
2718 the destructor function.
2719\end{cfuncdesc}
2720
2721\begin{cfuncdesc}{void*}{PyCObject_AsVoidPtr}{PyObject* self}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002722 Return the object \ctype{void *} that the \ctype{PyCObject}
Fred Drake3adf79e2001-10-12 19:01:43 +00002723 \var{self} was created with.
2724\end{cfuncdesc}
2725
2726\begin{cfuncdesc}{void*}{PyCObject_GetDesc}{PyObject* self}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002727 Return the description \ctype{void *} that the \ctype{PyCObject}
Fred Drake3adf79e2001-10-12 19:01:43 +00002728 \var{self} was created with.
2729\end{cfuncdesc}
Fred Drakecd8474e2001-11-26 21:29:17 +00002730
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002731\begin{cfuncdesc}{int}{PyCObject_SetVoidPtr}{PyObject* self, void* cobj}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002732 Set the void pointer inside \var{self} to \var{cobj}.
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002733 The \ctype{PyCObject} must not have an associated destructor.
2734 Return true on success, false on failure.
2735\end{cfuncdesc}
2736
Fred Drakecd8474e2001-11-26 21:29:17 +00002737
2738\subsection{Cell Objects \label{cell-objects}}
2739
2740``Cell'' objects are used to implement variables referenced by
2741multiple scopes. For each such variable, a cell object is created to
2742store the value; the local variables of each stack frame that
2743references the value contains a reference to the cells from outer
2744scopes which also use that variable. When the value is accessed, the
2745value contained in the cell is used instead of the cell object
2746itself. This de-referencing of the cell object requires support from
2747the generated byte-code; these are not automatically de-referenced
2748when accessed. Cell objects are not likely to be useful elsewhere.
2749
Fred Drake54e62942001-12-11 19:40:16 +00002750\begin{ctypedesc}{PyCellObject}
2751 The C structure used for cell objects.
2752\end{ctypedesc}
2753
Fred Drakecd8474e2001-11-26 21:29:17 +00002754\begin{cvardesc}{PyTypeObject}{PyCell_Type}
Georg Brandl9b743f52006-02-20 12:57:53 +00002755 The type object corresponding to cell objects.
Fred Drakecd8474e2001-11-26 21:29:17 +00002756\end{cvardesc}
2757
2758\begin{cfuncdesc}{int}{PyCell_Check}{ob}
2759 Return true if \var{ob} is a cell object; \var{ob} must not be
Tim Peters9ddf40b2004-06-20 22:41:32 +00002760 \NULL{}.
Fred Drakecd8474e2001-11-26 21:29:17 +00002761\end{cfuncdesc}
2762
2763\begin{cfuncdesc}{PyObject*}{PyCell_New}{PyObject *ob}
2764 Create and return a new cell object containing the value \var{ob}.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002765 The parameter may be \NULL{}.
Fred Drakecd8474e2001-11-26 21:29:17 +00002766\end{cfuncdesc}
2767
2768\begin{cfuncdesc}{PyObject*}{PyCell_Get}{PyObject *cell}
2769 Return the contents of the cell \var{cell}.
2770\end{cfuncdesc}
2771
2772\begin{cfuncdesc}{PyObject*}{PyCell_GET}{PyObject *cell}
2773 Return the contents of the cell \var{cell}, but without checking
Raymond Hettingerf4bb1f92003-08-23 03:38:11 +00002774 that \var{cell} is non-\NULL{} and a cell object.
Fred Drakecd8474e2001-11-26 21:29:17 +00002775\end{cfuncdesc}
2776
2777\begin{cfuncdesc}{int}{PyCell_Set}{PyObject *cell, PyObject *value}
2778 Set the contents of the cell object \var{cell} to \var{value}. This
2779 releases the reference to any current content of the cell.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002780 \var{value} may be \NULL{}. \var{cell} must be non-\NULL{}; if it is
Fred Drakecd8474e2001-11-26 21:29:17 +00002781 not a cell object, \code{-1} will be returned. On success, \code{0}
2782 will be returned.
2783\end{cfuncdesc}
2784
2785\begin{cfuncdesc}{void}{PyCell_SET}{PyObject *cell, PyObject *value}
2786 Sets the value of the cell object \var{cell} to \var{value}. No
2787 reference counts are adjusted, and no checks are made for safety;
2788 \var{cell} must be non-\NULL{} and must be a cell object.
2789\end{cfuncdesc}
Martin v. Löwise440e472004-06-01 15:22:42 +00002790
2791
2792\subsection{Generator Objects \label{gen-objects}}
2793
2794Generator objects are what Python uses to implement generator iterators.
2795They are normally created by iterating over a function that yields values,
2796rather than explicitly calling \cfunction{PyGen_New}.
2797
2798\begin{ctypedesc}{PyGenObject}
2799 The C structure used for generator objects.
2800\end{ctypedesc}
2801
2802\begin{cvardesc}{PyTypeObject}{PyGen_Type}
2803 The type object corresponding to generator objects
2804\end{cvardesc}
2805
2806\begin{cfuncdesc}{int}{PyGen_Check}{ob}
2807 Return true if \var{ob} is a generator object; \var{ob} must not be
Tim Peters9ddf40b2004-06-20 22:41:32 +00002808 \NULL{}.
Martin v. Löwise440e472004-06-01 15:22:42 +00002809\end{cfuncdesc}
2810
2811\begin{cfuncdesc}{int}{PyGen_CheckExact}{ob}
2812 Return true if \var{ob}'s type is \var{PyGen_Type}
2813 is a generator object; \var{ob} must not be
Tim Peters9ddf40b2004-06-20 22:41:32 +00002814 \NULL{}.
Martin v. Löwise440e472004-06-01 15:22:42 +00002815\end{cfuncdesc}
2816
2817\begin{cfuncdesc}{PyObject*}{PyGen_New}{PyFrameObject *frame}
2818 Create and return a new generator object based on the \var{frame} object.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002819 A reference to \var{frame} is stolen by this function.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002820 The parameter must not be \NULL{}.
2821\end{cfuncdesc}
2822
2823
2824\subsection{DateTime Objects \label{datetime-objects}}
2825
2826Various date and time objects are supplied by the \module{datetime}
2827module. Before using any of these functions, the header file
2828\file{datetime.h} must be included in your source (note that this is
2829not include by \file{Python.h}), and macro \cfunction{PyDateTime_IMPORT()}
2830must be invoked. The macro arranges to put a pointer to a C structure
2831in a static variable \code{PyDateTimeAPI}, which is used by the following
Tim Peters183dabc2004-07-11 19:26:19 +00002832macros.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002833
Tim Peters183dabc2004-07-11 19:26:19 +00002834Type-check macros:
2835
2836\begin{cfuncdesc}{int}{PyDate_Check}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002837 Return true if \var{ob} is of type \cdata{PyDateTime_DateType} or
2838 a subtype of \cdata{PyDateTime_DateType}. \var{ob} must not be
2839 \NULL{}.
2840 \versionadded{2.4}
2841\end{cfuncdesc}
2842
Tim Peters183dabc2004-07-11 19:26:19 +00002843\begin{cfuncdesc}{int}{PyDate_CheckExact}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002844 Return true if \var{ob} is of type \cdata{PyDateTime_DateType}.
2845 \var{ob} must not be \NULL{}.
2846 \versionadded{2.4}
2847\end{cfuncdesc}
2848
Tim Peters183dabc2004-07-11 19:26:19 +00002849\begin{cfuncdesc}{int}{PyDateTime_Check}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002850 Return true if \var{ob} is of type \cdata{PyDateTime_DateTimeType} or
2851 a subtype of \cdata{PyDateTime_DateTimeType}. \var{ob} must not be
2852 \NULL{}.
2853 \versionadded{2.4}
2854\end{cfuncdesc}
2855
Tim Peters183dabc2004-07-11 19:26:19 +00002856\begin{cfuncdesc}{int}{PyDateTime_CheckExact}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002857 Return true if \var{ob} is of type \cdata{PyDateTime_DateTimeType}.
2858 \var{ob} must not be \NULL{}.
2859 \versionadded{2.4}
2860\end{cfuncdesc}
2861
Tim Peters183dabc2004-07-11 19:26:19 +00002862\begin{cfuncdesc}{int}{PyTime_Check}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002863 Return true if \var{ob} is of type \cdata{PyDateTime_TimeType} or
2864 a subtype of \cdata{PyDateTime_TimeType}. \var{ob} must not be
2865 \NULL{}.
2866 \versionadded{2.4}
2867\end{cfuncdesc}
2868
Tim Peters183dabc2004-07-11 19:26:19 +00002869\begin{cfuncdesc}{int}{PyTime_CheckExact}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002870 Return true if \var{ob} is of type \cdata{PyDateTime_TimeType}.
2871 \var{ob} must not be \NULL{}.
2872 \versionadded{2.4}
2873\end{cfuncdesc}
2874
Tim Peters183dabc2004-07-11 19:26:19 +00002875\begin{cfuncdesc}{int}{PyDelta_Check}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002876 Return true if \var{ob} is of type \cdata{PyDateTime_DeltaType} or
2877 a subtype of \cdata{PyDateTime_DeltaType}. \var{ob} must not be
2878 \NULL{}.
2879 \versionadded{2.4}
2880\end{cfuncdesc}
2881
Tim Peters183dabc2004-07-11 19:26:19 +00002882\begin{cfuncdesc}{int}{PyDelta_CheckExact}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002883 Return true if \var{ob} is of type \cdata{PyDateTime_DeltaType}.
2884 \var{ob} must not be \NULL{}.
2885 \versionadded{2.4}
2886\end{cfuncdesc}
2887
Tim Peters183dabc2004-07-11 19:26:19 +00002888\begin{cfuncdesc}{int}{PyTZInfo_Check}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002889 Return true if \var{ob} is of type \cdata{PyDateTime_TZInfoType} or
2890 a subtype of \cdata{PyDateTime_TZInfoType}. \var{ob} must not be
2891 \NULL{}.
2892 \versionadded{2.4}
2893\end{cfuncdesc}
2894
Tim Peters183dabc2004-07-11 19:26:19 +00002895\begin{cfuncdesc}{int}{PyTZInfo_CheckExact}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002896 Return true if \var{ob} is of type \cdata{PyDateTime_TZInfoType}.
2897 \var{ob} must not be \NULL{}.
2898 \versionadded{2.4}
2899\end{cfuncdesc}
2900
Tim Peters183dabc2004-07-11 19:26:19 +00002901Macros to create objects:
2902
Tim Peters9ddf40b2004-06-20 22:41:32 +00002903\begin{cfuncdesc}{PyObject*}{PyDate_FromDate}{int year, int month, int day}
2904 Return a \code{datetime.date} object with the specified year, month
2905 and day.
2906 \versionadded{2.4}
2907\end{cfuncdesc}
2908
Brett Cannon5bbe6ad2005-02-17 05:17:17 +00002909\begin{cfuncdesc}{PyObject*}{PyDateTime_FromDateAndTime}{int year, int month,
Tim Peters9ddf40b2004-06-20 22:41:32 +00002910 int day, int hour, int minute, int second, int usecond}
2911 Return a \code{datetime.datetime} object with the specified year, month,
2912 day, hour, minute, second and microsecond.
2913 \versionadded{2.4}
2914\end{cfuncdesc}
2915
2916\begin{cfuncdesc}{PyObject*}{PyTime_FromTime}{int hour, int minute,
2917 int second, int usecond}
2918 Return a \code{datetime.time} object with the specified hour, minute,
2919 second and microsecond.
2920 \versionadded{2.4}
2921\end{cfuncdesc}
2922
2923\begin{cfuncdesc}{PyObject*}{PyDelta_FromDSU}{int days, int seconds,
2924 int useconds}
2925 Return a \code{datetime.timedelta} object representing the given number
2926 of days, seconds and microseconds. Normalization is performed so that
2927 the resulting number of microseconds and seconds lie in the ranges
2928 documented for \code{datetime.timedelta} objects.
2929 \versionadded{2.4}
2930\end{cfuncdesc}
2931
Tim Peters8ff9f9f2004-07-17 01:42:26 +00002932Macros to extract fields from date objects. The argument must be an
Tim Peters183dabc2004-07-11 19:26:19 +00002933instance of \cdata{PyDateTime_Date}, including subclasses (such as
2934\cdata{PyDateTime_DateTime}). The argument must not be \NULL{}, and
2935the type is not checked:
2936
2937\begin{cfuncdesc}{int}{PyDateTime_GET_YEAR}{PyDateTime_Date *o}
2938 Return the year, as a positive int.
2939 \versionadded{2.4}
2940\end{cfuncdesc}
2941
2942\begin{cfuncdesc}{int}{PyDateTime_GET_MONTH}{PyDateTime_Date *o}
2943 Return the month, as an int from 1 through 12.
2944 \versionadded{2.4}
2945\end{cfuncdesc}
2946
2947\begin{cfuncdesc}{int}{PyDateTime_GET_DAY}{PyDateTime_Date *o}
2948 Return the day, as an int from 1 through 31.
2949 \versionadded{2.4}
2950\end{cfuncdesc}
2951
Tim Peters8ff9f9f2004-07-17 01:42:26 +00002952Macros to extract fields from datetime objects. The argument must be an
Tim Peters183dabc2004-07-11 19:26:19 +00002953instance of \cdata{PyDateTime_DateTime}, including subclasses.
2954The argument must not be \NULL{}, and the type is not checked:
2955
2956\begin{cfuncdesc}{int}{PyDateTime_DATE_GET_HOUR}{PyDateTime_DateTime *o}
Neal Norwitz7fdd92f2004-08-02 21:56:33 +00002957 Return the hour, as an int from 0 through 23.
Tim Peters183dabc2004-07-11 19:26:19 +00002958 \versionadded{2.4}
2959\end{cfuncdesc}
2960
2961\begin{cfuncdesc}{int}{PyDateTime_DATE_GET_MINUTE}{PyDateTime_DateTime *o}
2962 Return the minute, as an int from 0 through 59.
2963 \versionadded{2.4}
2964\end{cfuncdesc}
2965
2966\begin{cfuncdesc}{int}{PyDateTime_DATE_GET_SECOND}{PyDateTime_DateTime *o}
2967 Return the second, as an int from 0 through 59.
2968 \versionadded{2.4}
2969\end{cfuncdesc}
2970
2971\begin{cfuncdesc}{int}{PyDateTime_DATE_GET_MICROSECOND}{PyDateTime_DateTime *o}
2972 Return the microsecond, as an int from 0 through 999999.
2973 \versionadded{2.4}
2974\end{cfuncdesc}
2975
Tim Peters8ff9f9f2004-07-17 01:42:26 +00002976Macros to extract fields from time objects. The argument must be an
Tim Peters183dabc2004-07-11 19:26:19 +00002977instance of \cdata{PyDateTime_Time}, including subclasses.
2978The argument must not be \NULL{}, and the type is not checked:
2979
2980\begin{cfuncdesc}{int}{PyDateTime_TIME_GET_HOUR}{PyDateTime_Time *o}
Neal Norwitz7fdd92f2004-08-02 21:56:33 +00002981 Return the hour, as an int from 0 through 23.
Tim Peters183dabc2004-07-11 19:26:19 +00002982 \versionadded{2.4}
2983\end{cfuncdesc}
2984
2985\begin{cfuncdesc}{int}{PyDateTime_TIME_GET_MINUTE}{PyDateTime_Time *o}
2986 Return the minute, as an int from 0 through 59.
2987 \versionadded{2.4}
2988\end{cfuncdesc}
2989
2990\begin{cfuncdesc}{int}{PyDateTime_TIME_GET_SECOND}{PyDateTime_Time *o}
2991 Return the second, as an int from 0 through 59.
2992 \versionadded{2.4}
2993\end{cfuncdesc}
2994
2995\begin{cfuncdesc}{int}{PyDateTime_TIME_GET_MICROSECOND}{PyDateTime_Time *o}
2996 Return the microsecond, as an int from 0 through 999999.
2997 \versionadded{2.4}
2998\end{cfuncdesc}
2999
3000Macros for the convenience of modules implementing the DB API:
3001
Tim Peters9ddf40b2004-06-20 22:41:32 +00003002\begin{cfuncdesc}{PyObject*}{PyDateTime_FromTimestamp}{PyObject *args}
3003 Create and return a new \code{datetime.datetime} object given an argument
3004 tuple suitable for passing to \code{datetime.datetime.fromtimestamp()}.
Tim Peters9ddf40b2004-06-20 22:41:32 +00003005 \versionadded{2.4}
3006\end{cfuncdesc}
3007
3008\begin{cfuncdesc}{PyObject*}{PyDate_FromTimestamp}{PyObject *args}
3009 Create and return a new \code{datetime.date} object given an argument
3010 tuple suitable for passing to \code{datetime.date.fromtimestamp()}.
Tim Peters9ddf40b2004-06-20 22:41:32 +00003011 \versionadded{2.4}
Martin v. Löwise440e472004-06-01 15:22:42 +00003012\end{cfuncdesc}
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003013
3014
3015\subsection{Set Objects \label{setObjects}}
3016\sectionauthor{Raymond D. Hettinger}{python@rcn.com}
3017
3018\obindex{set}
3019\obindex{frozenset}
3020\versionadded{2.5}
3021
3022This section details the public API for \class{set} and \class{frozenset}
3023objects. Any functionality not listed below is best accessed using the
Raymond Hettinger0c230b92005-08-17 10:05:22 +00003024either the abstract object protocol (including
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003025\cfunction{PyObject_CallMethod()}, \cfunction{PyObject_RichCompareBool()},
3026\cfunction{PyObject_Hash()}, \cfunction{PyObject_Repr()},
3027\cfunction{PyObject_IsTrue()}, \cfunction{PyObject_Print()}, and
Raymond Hettinger0c230b92005-08-17 10:05:22 +00003028\cfunction{PyObject_GetIter()})
3029or the abstract number protocol (including
3030\cfunction{PyNumber_Add()}, \cfunction{PyNumber_Subtract()},
3031\cfunction{PyNumber_Or()}, \cfunction{PyNumber_Xor()},
Georg Brandlb518d8c2006-02-22 11:46:55 +00003032\cfunction{PyNumber_InPlaceAdd()}, \cfunction{PyNumber_InPlaceSubtract()},
3033\cfunction{PyNumber_InPlaceOr()}, and \cfunction{PyNumber_InPlaceXor()}).
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003034
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003035\begin{ctypedesc}{PySetObject}
3036 This subtype of \ctype{PyObject} is used to hold the internal data for
3037 both \class{set} and \class{frozenset} objects. It is like a
3038 \ctype{PyDictObject} in that it is a fixed size for small sets
3039 (much like tuple storage) and will point to a separate, variable sized
3040 block of memory for medium and large sized sets (much like list storage).
3041 None of the fields of this structure should be considered public and
3042 are subject to change. All access should be done through the
Raymond Hettinger94fedf92005-08-17 12:23:45 +00003043 documented API rather than by manipulating the values in the structure.
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003044
3045\end{ctypedesc}
3046
3047\begin{cvardesc}{PyTypeObject}{PySet_Type}
3048 This is an instance of \ctype{PyTypeObject} representing the Python
3049 \class{set} type.
3050\end{cvardesc}
3051
3052\begin{cvardesc}{PyTypeObject}{PyFrozenSet_Type}
3053 This is an instance of \ctype{PyTypeObject} representing the Python
3054 \class{frozenset} type.
3055\end{cvardesc}
3056
3057
3058The following type check macros work on pointers to any Python object.
3059Likewise, the constructor functions work with any iterable Python object.
3060
3061\begin{cfuncdesc}{int}{PyAnySet_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00003062 Return true if \var{p} is a \class{set} object, a \class{frozenset}
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003063 object, or an instance of a subtype.
3064\end{cfuncdesc}
3065
3066\begin{cfuncdesc}{int}{PyAnySet_CheckExact}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00003067 Return true if \var{p} is a \class{set} object or a \class{frozenset}
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003068 object but not an instance of a subtype.
3069\end{cfuncdesc}
3070
3071\begin{cfuncdesc}{int}{PyFrozenSet_CheckExact}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00003072 Return true if \var{p} is a \class{frozenset} object
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003073 but not an instance of a subtype.
3074\end{cfuncdesc}
3075
3076\begin{cfuncdesc}{PyObject*}{PySet_New}{PyObject *iterable}
Georg Brandl99363b62005-09-03 07:27:26 +00003077 Return a new \class{set} containing objects returned by the
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003078 \var{iterable}. The \var{iterable} may be \NULL{} to create a
Georg Brandl99363b62005-09-03 07:27:26 +00003079 new empty set. Return the new set on success or \NULL{} on
3080 failure. Raise \exception{TypeError} if \var{iterable} is
Raymond Hettinger94fedf92005-08-17 12:23:45 +00003081 not actually iterable. The constructor is also useful for
3082 copying a set (\code{c=set(s)}).
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003083\end{cfuncdesc}
3084
3085\begin{cfuncdesc}{PyObject*}{PyFrozenSet_New}{PyObject *iterable}
Georg Brandl99363b62005-09-03 07:27:26 +00003086 Return a new \class{frozenset} containing objects returned by the
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003087 \var{iterable}. The \var{iterable} may be \NULL{} to create a
Georg Brandl99363b62005-09-03 07:27:26 +00003088 new empty frozenset. Return the new set on success or \NULL{} on
3089 failure. Raise \exception{TypeError} if \var{iterable} is
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00003090 not actually iterable.
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003091\end{cfuncdesc}
3092
3093
3094The following functions and macros are available for instances of
3095\class{set} or \class{frozenset} or instances of their subtypes.
3096
3097\begin{cfuncdesc}{int}{PySet_Size}{PyObject *anyset}
Georg Brandl99363b62005-09-03 07:27:26 +00003098 Return the length of a \class{set} or \class{frozenset} object.
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003099 Equivalent to \samp{len(\var{anyset})}. Raises a
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00003100 \exception{PyExc_SystemError} if \var{anyset} is not a \class{set},
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003101 \class{frozenset}, or an instance of a subtype.
3102 \bifuncindex{len}
3103\end{cfuncdesc}
3104
3105\begin{cfuncdesc}{int}{PySet_GET_SIZE}{PyObject *anyset}
3106 Macro form of \cfunction{PySet_Size()} without error checking.
3107\end{cfuncdesc}
3108
3109\begin{cfuncdesc}{int}{PySet_Contains}{PyObject *anyset, PyObject *key}
Georg Brandl99363b62005-09-03 07:27:26 +00003110 Return 1 if found, 0 if not found, and -1 if an error is
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003111 encountered. Unlike the Python \method{__contains__()} method, this
3112 function does not automatically convert unhashable sets into temporary
Georg Brandl99363b62005-09-03 07:27:26 +00003113 frozensets. Raise a \exception{TypeError} if the \var{key} is unhashable.
3114 Raise \exception{PyExc_SystemError} if \var{anyset} is not a \class{set},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00003115 \class{frozenset}, or an instance of a subtype.
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003116\end{cfuncdesc}
3117
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003118The following functions are available for instances of \class{set} or
3119its subtypes but not for instances of \class{frozenset} or its subtypes.
3120
3121\begin{cfuncdesc}{int}{PySet_Add}{PyObject *set, PyObject *key}
Georg Brandl99363b62005-09-03 07:27:26 +00003122 Add \var{key} to a \class{set} instance. Does not apply to
3123 \class{frozenset} instances. Return 0 on success or -1 on failure.
3124 Raise a \exception{TypeError} if the \var{key} is unhashable.
3125 Raise a \exception{MemoryError} if there is no room to grow.
3126 Raise a \exception{SystemError} if \var{set} is an not an instance
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003127 of \class{set} or its subtype.
3128\end{cfuncdesc}
3129
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00003130\begin{cfuncdesc}{int}{PySet_Discard}{PyObject *set, PyObject *key}
Georg Brandl99363b62005-09-03 07:27:26 +00003131 Return 1 if found and removed, 0 if not found (no action taken),
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00003132 and -1 if an error is encountered. Does not raise \exception{KeyError}
Georg Brandl99363b62005-09-03 07:27:26 +00003133 for missing keys. Raise a \exception{TypeError} if the \var{key} is
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00003134 unhashable. Unlike the Python \method{discard()} method, this function
3135 does not automatically convert unhashable sets into temporary frozensets.
Georg Brandl99363b62005-09-03 07:27:26 +00003136 Raise \exception{PyExc_SystemError} if \var{set} is an not an instance
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00003137 of \class{set} or its subtype.
3138\end{cfuncdesc}
3139
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003140\begin{cfuncdesc}{PyObject*}{PySet_Pop}{PyObject *set}
Georg Brandl99363b62005-09-03 07:27:26 +00003141 Return a new reference to an arbitrary object in the \var{set},
3142 and removes the object from the \var{set}. Return \NULL{} on
3143 failure. Raise \exception{KeyError} if the set is empty.
3144 Raise a \exception{SystemError} if \var{set} is an not an instance
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003145 of \class{set} or its subtype.
3146\end{cfuncdesc}
3147
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003148\begin{cfuncdesc}{int}{PySet_Clear}{PyObject *set}
3149 Empty an existing set of all elements.
3150\end{cfuncdesc}