blob: d60dcff2b52645c6cd79d464f7e375999bdc90bb [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
Thomas Wouters0e3f5912006-08-11 14:57:12 +000034 \code{type} and \code{types.TypeType} in the Python layer.
Fred Drake3adf79e2001-10-12 19:01:43 +000035 \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
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000120 integer type. This is the same object as \code{int} and
121 \code{types.IntType}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000122 \withsubitem{(in modules types)}{\ttindex{IntType}}
123\end{cvardesc}
124
Andrew M. Kuchling4eb1a002004-08-07 20:19:24 +0000125\begin{cfuncdesc}{int}{PyInt_Check}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000126 Return true if \var{o} is of type \cdata{PyInt_Type} or a subtype
Fred Drake3adf79e2001-10-12 19:01:43 +0000127 of \cdata{PyInt_Type}.
128 \versionchanged[Allowed subtypes to be accepted]{2.2}
129\end{cfuncdesc}
130
Andrew M. Kuchling4eb1a002004-08-07 20:19:24 +0000131\begin{cfuncdesc}{int}{PyInt_CheckExact}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000132 Return true if \var{o} is of type \cdata{PyInt_Type}, but not a
Fred Drake3adf79e2001-10-12 19:01:43 +0000133 subtype of \cdata{PyInt_Type}.
134 \versionadded{2.2}
135\end{cfuncdesc}
136
Skip Montanaro1ff49a72003-02-03 05:13:24 +0000137\begin{cfuncdesc}{PyObject*}{PyInt_FromString}{char *str, char **pend,
138 int base}
139 Return a new \ctype{PyIntObject} or \ctype{PyLongObject} based on the
140 string value in \var{str}, which is interpreted according to the radix in
Tim Peters9ddf40b2004-06-20 22:41:32 +0000141 \var{base}. If \var{pend} is non-\NULL{}, \code{*\var{pend}} will point to
Skip Montanaro1ff49a72003-02-03 05:13:24 +0000142 the first character in \var{str} which follows the representation of the
143 number. If \var{base} is \code{0}, the radix will be determined based on
144 the leading characters of \var{str}: if \var{str} starts with \code{'0x'}
145 or \code{'0X'}, radix 16 will be used; if \var{str} starts with
146 \code{'0'}, radix 8 will be used; otherwise radix 10 will be used. If
147 \var{base} is not \code{0}, it must be between \code{2} and \code{36},
148 inclusive. Leading spaces are ignored. If there are no digits,
149 \exception{ValueError} will be raised. If the string represents a number
150 too large to be contained within the machine's \ctype{long int} type and
151 overflow warnings are being suppressed, a \ctype{PyLongObject} will be
152 returned. If overflow warnings are not being suppressed, \NULL{} will be
153 returned in this case.
154\end{cfuncdesc}
155
Fred Drake3adf79e2001-10-12 19:01:43 +0000156\begin{cfuncdesc}{PyObject*}{PyInt_FromLong}{long ival}
Georg Brandl99363b62005-09-03 07:27:26 +0000157 Create a new integer object with a value of \var{ival}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000158
159 The current implementation keeps an array of integer objects for all
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000160 integers between \code{-5} and \code{256}, when you create an int in
Fred Drake3adf79e2001-10-12 19:01:43 +0000161 that range you actually just get back a reference to the existing
162 object. So it should be possible to change the value of \code{1}. I
163 suspect the behaviour of Python in this case is undefined. :-)
164\end{cfuncdesc}
165
Martin v. Löwis3b197542006-03-01 05:47:11 +0000166\begin{cfuncdesc}{PyObject*}{PyInt_FromSsize_t}{Py_ssize_t ival}
167 Create a new integer object with a value of \var{ival}.
168 If the value exceeds \code{LONG_MAX}, a long integer object is
169 returned.
170
171 \versionadded{2.5}
172\end{cfuncdesc}
173
Fred Drake3adf79e2001-10-12 19:01:43 +0000174\begin{cfuncdesc}{long}{PyInt_AsLong}{PyObject *io}
175 Will first attempt to cast the object to a \ctype{PyIntObject}, if
Martin v. Löwis3b197542006-03-01 05:47:11 +0000176 it is not already one, and then return its value. If there is an
177 error, \code{-1} is returned, and the caller should check
178 \code{PyErr_Occurred()} to find out whether there was an error, or
179 whether the value just happened to be -1.
Fred Drake3adf79e2001-10-12 19:01:43 +0000180\end{cfuncdesc}
181
182\begin{cfuncdesc}{long}{PyInt_AS_LONG}{PyObject *io}
Georg Brandl99363b62005-09-03 07:27:26 +0000183 Return the value of the object \var{io}. No error checking is
Fred Drake3adf79e2001-10-12 19:01:43 +0000184 performed.
185\end{cfuncdesc}
186
Thomas Heller34d7f092003-04-23 19:51:05 +0000187\begin{cfuncdesc}{unsigned long}{PyInt_AsUnsignedLongMask}{PyObject *io}
188 Will first attempt to cast the object to a \ctype{PyIntObject} or
Fred Drakec22b2992003-04-23 20:38:41 +0000189 \ctype{PyLongObject}, if it is not already one, and then return its
Thomas Heller34d7f092003-04-23 19:51:05 +0000190 value as unsigned long. This function does not check for overflow.
191 \versionadded{2.3}
192\end{cfuncdesc}
193
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000194\begin{cfuncdesc}{unsigned PY_LONG_LONG}{PyInt_AsUnsignedLongLongMask}{PyObject *io}
Thomas Heller34d7f092003-04-23 19:51:05 +0000195 Will first attempt to cast the object to a \ctype{PyIntObject} or
Fred Drakec22b2992003-04-23 20:38:41 +0000196 \ctype{PyLongObject}, if it is not already one, and then return its
Thomas Heller34d7f092003-04-23 19:51:05 +0000197 value as unsigned long long, without checking for overflow.
198 \versionadded{2.3}
199\end{cfuncdesc}
200
Martin v. Löwis3b197542006-03-01 05:47:11 +0000201\begin{cfuncdesc}{Py_ssize_t}{PyInt_AsSsize_t}{PyObject *io}
202 Will first attempt to cast the object to a \ctype{PyIntObject} or
203 \ctype{PyLongObject}, if it is not already one, and then return its
204 value as \ctype{Py_ssize_t}.
205 \versionadded{2.5}
206\end{cfuncdesc}
207
Fred Drake3adf79e2001-10-12 19:01:43 +0000208\begin{cfuncdesc}{long}{PyInt_GetMax}{}
Georg Brandl99363b62005-09-03 07:27:26 +0000209 Return the system's idea of the largest integer it can handle
Fred Drake3adf79e2001-10-12 19:01:43 +0000210 (\constant{LONG_MAX}\ttindex{LONG_MAX}, as defined in the system
211 header files).
212\end{cfuncdesc}
213
Fred Drake2be406b2004-08-03 16:02:35 +0000214\subsection{Boolean Objects \label{boolObjects}}
Skip Montanaro33ee76a2004-07-28 14:17:04 +0000215
216Booleans in Python are implemented as a subclass of integers. There
217are only two booleans, \constant{Py_False} and \constant{Py_True}. As
218such, the normal creation and deletion functions don't apply to
219booleans. The following macros are available, however.
220
Andrew M. Kuchling4eb1a002004-08-07 20:19:24 +0000221\begin{cfuncdesc}{int}{PyBool_Check}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000222 Return true if \var{o} is of type \cdata{PyBool_Type}.
Skip Montanaro33ee76a2004-07-28 14:17:04 +0000223 \versionadded{2.3}
224\end{cfuncdesc}
225
Skip Montanaro6d3db702004-07-29 02:16:04 +0000226\begin{cvardesc}{PyObject*}{Py_False}
227 The Python \code{False} object. This object has no methods. It needs to
228 be treated just like any other object with respect to reference counts.
229\end{cvardesc}
Skip Montanaro33ee76a2004-07-28 14:17:04 +0000230
Skip Montanaro6d3db702004-07-29 02:16:04 +0000231\begin{cvardesc}{PyObject*}{Py_True}
232 The Python \code{True} object. This object has no methods. It needs to
233 be treated just like any other object with respect to reference counts.
234\end{cvardesc}
235
236\begin{csimplemacrodesc}{Py_RETURN_FALSE}
237 Return \constant{Py_False} from a function, properly incrementing its
238 reference count.
239\versionadded{2.4}
240\end{csimplemacrodesc}
241
242\begin{csimplemacrodesc}{Py_RETURN_TRUE}
Andrew M. Kuchling4eb1a002004-08-07 20:19:24 +0000243 Return \constant{Py_True} from a function, properly incrementing its
244 reference count.
Skip Montanaro33ee76a2004-07-28 14:17:04 +0000245\versionadded{2.4}
Skip Montanaro6d3db702004-07-29 02:16:04 +0000246\end{csimplemacrodesc}
Skip Montanaro33ee76a2004-07-28 14:17:04 +0000247
Georg Brandl99363b62005-09-03 07:27:26 +0000248\begin{cfuncdesc}{PyObject*}{PyBool_FromLong}{long v}
Thomas Wouters477c8d52006-05-27 19:21:47 +0000249 Return a new reference to \constant{Py_True} or \constant{Py_False}
Georg Brandl99363b62005-09-03 07:27:26 +0000250 depending on the truth value of \var{v}.
Skip Montanaro33ee76a2004-07-28 14:17:04 +0000251\versionadded{2.3}
252\end{cfuncdesc}
Fred Drake3adf79e2001-10-12 19:01:43 +0000253
254\subsection{Long Integer Objects \label{longObjects}}
255
256\obindex{long integer}
257\begin{ctypedesc}{PyLongObject}
258 This subtype of \ctype{PyObject} represents a Python long integer
259 object.
260\end{ctypedesc}
261
262\begin{cvardesc}{PyTypeObject}{PyLong_Type}
263 This instance of \ctype{PyTypeObject} represents the Python long
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000264 integer type. This is the same object as \code{long} and
265 \code{types.LongType}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000266 \withsubitem{(in modules types)}{\ttindex{LongType}}
267\end{cvardesc}
268
269\begin{cfuncdesc}{int}{PyLong_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +0000270 Return true if its argument is a \ctype{PyLongObject} or a subtype
Fred Drake3adf79e2001-10-12 19:01:43 +0000271 of \ctype{PyLongObject}.
272 \versionchanged[Allowed subtypes to be accepted]{2.2}
273\end{cfuncdesc}
274
275\begin{cfuncdesc}{int}{PyLong_CheckExact}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +0000276 Return true if its argument is a \ctype{PyLongObject}, but not a
Fred Drake3adf79e2001-10-12 19:01:43 +0000277 subtype of \ctype{PyLongObject}.
278 \versionadded{2.2}
279\end{cfuncdesc}
280
281\begin{cfuncdesc}{PyObject*}{PyLong_FromLong}{long v}
Georg Brandl99363b62005-09-03 07:27:26 +0000282 Return a new \ctype{PyLongObject} object from \var{v}, or \NULL{}
Fred Drake3adf79e2001-10-12 19:01:43 +0000283 on failure.
284\end{cfuncdesc}
285
286\begin{cfuncdesc}{PyObject*}{PyLong_FromUnsignedLong}{unsigned long v}
Georg Brandl99363b62005-09-03 07:27:26 +0000287 Return a new \ctype{PyLongObject} object from a C \ctype{unsigned
Fred Drake3adf79e2001-10-12 19:01:43 +0000288 long}, or \NULL{} on failure.
289\end{cfuncdesc}
290
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000291\begin{cfuncdesc}{PyObject*}{PyLong_FromLongLong}{PY_LONG_LONG v}
Georg Brandl99363b62005-09-03 07:27:26 +0000292 Return a new \ctype{PyLongObject} object from a C \ctype{long long},
Fred Drake3adf79e2001-10-12 19:01:43 +0000293 or \NULL{} on failure.
294\end{cfuncdesc}
295
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000296\begin{cfuncdesc}{PyObject*}{PyLong_FromUnsignedLongLong}{unsigned PY_LONG_LONG v}
Georg Brandl99363b62005-09-03 07:27:26 +0000297 Return a new \ctype{PyLongObject} object from a C \ctype{unsigned
Fred Drake3adf79e2001-10-12 19:01:43 +0000298 long long}, or \NULL{} on failure.
299\end{cfuncdesc}
300
301\begin{cfuncdesc}{PyObject*}{PyLong_FromDouble}{double v}
Georg Brandl99363b62005-09-03 07:27:26 +0000302 Return a new \ctype{PyLongObject} object from the integer part of
Fred Drake3adf79e2001-10-12 19:01:43 +0000303 \var{v}, or \NULL{} on failure.
304\end{cfuncdesc}
305
306\begin{cfuncdesc}{PyObject*}{PyLong_FromString}{char *str, char **pend,
307 int base}
308 Return a new \ctype{PyLongObject} based on the string value in
309 \var{str}, which is interpreted according to the radix in
Tim Peters9ddf40b2004-06-20 22:41:32 +0000310 \var{base}. If \var{pend} is non-\NULL{}, \code{*\var{pend}} will
Fred Drake3adf79e2001-10-12 19:01:43 +0000311 point to the first character in \var{str} which follows the
312 representation of the number. If \var{base} is \code{0}, the radix
Skip Montanaro1ff49a72003-02-03 05:13:24 +0000313 will be determined based on the leading characters of \var{str}: if
Fred Drake3adf79e2001-10-12 19:01:43 +0000314 \var{str} starts with \code{'0x'} or \code{'0X'}, radix 16 will be
315 used; if \var{str} starts with \code{'0'}, radix 8 will be used;
316 otherwise radix 10 will be used. If \var{base} is not \code{0}, it
317 must be between \code{2} and \code{36}, inclusive. Leading spaces
318 are ignored. If there are no digits, \exception{ValueError} will be
319 raised.
320\end{cfuncdesc}
321
322\begin{cfuncdesc}{PyObject*}{PyLong_FromUnicode}{Py_UNICODE *u,
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000323 Py_ssize_t length, int base}
Fred Drake3adf79e2001-10-12 19:01:43 +0000324 Convert a sequence of Unicode digits to a Python long integer
325 value. The first parameter, \var{u}, points to the first character
326 of the Unicode string, \var{length} gives the number of characters,
327 and \var{base} is the radix for the conversion. The radix must be
328 in the range [2, 36]; if it is out of range, \exception{ValueError}
329 will be raised.
330 \versionadded{1.6}
331\end{cfuncdesc}
332
333\begin{cfuncdesc}{PyObject*}{PyLong_FromVoidPtr}{void *p}
334 Create a Python integer or long integer from the pointer \var{p}.
335 The pointer value can be retrieved from the resulting value using
336 \cfunction{PyLong_AsVoidPtr()}.
337 \versionadded{1.5.2}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000338 \versionchanged[If the integer is larger than LONG_MAX,
339 a positive long integer is returned]{2.5}
340 \end{cfuncdesc}
Fred Drake3adf79e2001-10-12 19:01:43 +0000341
342\begin{cfuncdesc}{long}{PyLong_AsLong}{PyObject *pylong}
Georg Brandl99363b62005-09-03 07:27:26 +0000343 Return a C \ctype{long} representation of the contents of
Fred Drake3adf79e2001-10-12 19:01:43 +0000344 \var{pylong}. If \var{pylong} is greater than
345 \constant{LONG_MAX}\ttindex{LONG_MAX}, an \exception{OverflowError}
346 is raised.
347 \withsubitem{(built-in exception)}{\ttindex{OverflowError}}
348\end{cfuncdesc}
349
350\begin{cfuncdesc}{unsigned long}{PyLong_AsUnsignedLong}{PyObject *pylong}
Georg Brandl99363b62005-09-03 07:27:26 +0000351 Return a C \ctype{unsigned long} representation of the contents of
Fred Drake3adf79e2001-10-12 19:01:43 +0000352 \var{pylong}. If \var{pylong} is greater than
353 \constant{ULONG_MAX}\ttindex{ULONG_MAX}, an
354 \exception{OverflowError} is raised.
Tim Petersf582b822001-12-11 18:51:08 +0000355 \withsubitem{(built-in exception)}{\ttindex{OverflowError}}
Fred Drake3adf79e2001-10-12 19:01:43 +0000356\end{cfuncdesc}
357
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000358\begin{cfuncdesc}{PY_LONG_LONG}{PyLong_AsLongLong}{PyObject *pylong}
Fred Drake3adf79e2001-10-12 19:01:43 +0000359 Return a C \ctype{long long} from a Python long integer. If
360 \var{pylong} cannot be represented as a \ctype{long long}, an
361 \exception{OverflowError} will be raised.
362 \versionadded{2.2}
363\end{cfuncdesc}
364
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000365\begin{cfuncdesc}{unsigned PY_LONG_LONG}{PyLong_AsUnsignedLongLong}{PyObject
Fred Drake3adf79e2001-10-12 19:01:43 +0000366 *pylong}
367 Return a C \ctype{unsigned long long} from a Python long integer.
368 If \var{pylong} cannot be represented as an \ctype{unsigned long
369 long}, an \exception{OverflowError} will be raised if the value is
370 positive, or a \exception{TypeError} will be raised if the value is
371 negative.
372 \versionadded{2.2}
373\end{cfuncdesc}
374
Thomas Heller34d7f092003-04-23 19:51:05 +0000375\begin{cfuncdesc}{unsigned long}{PyLong_AsUnsignedLongMask}{PyObject *io}
376 Return a C \ctype{unsigned long} from a Python long integer, without
377 checking for overflow.
378 \versionadded{2.3}
379\end{cfuncdesc}
380
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000381\begin{cfuncdesc}{unsigned PY_LONG_LONG}{PyLong_AsUnsignedLongLongMask}{PyObject *io}
Thomas Heller34d7f092003-04-23 19:51:05 +0000382 Return a C \ctype{unsigned long long} from a Python long integer, without
383 checking for overflow.
384 \versionadded{2.3}
385\end{cfuncdesc}
386
Fred Drake3adf79e2001-10-12 19:01:43 +0000387\begin{cfuncdesc}{double}{PyLong_AsDouble}{PyObject *pylong}
Georg Brandl99363b62005-09-03 07:27:26 +0000388 Return a C \ctype{double} representation of the contents of
Fred Drake3adf79e2001-10-12 19:01:43 +0000389 \var{pylong}. If \var{pylong} cannot be approximately represented
390 as a \ctype{double}, an \exception{OverflowError} exception is
391 raised and \code{-1.0} will be returned.
392\end{cfuncdesc}
393
394\begin{cfuncdesc}{void*}{PyLong_AsVoidPtr}{PyObject *pylong}
395 Convert a Python integer or long integer \var{pylong} to a C
396 \ctype{void} pointer. If \var{pylong} cannot be converted, an
397 \exception{OverflowError} will be raised. This is only assured to
398 produce a usable \ctype{void} pointer for values created with
399 \cfunction{PyLong_FromVoidPtr()}.
400 \versionadded{1.5.2}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000401 \versionchanged[For values outside 0..LONG_MAX, both signed and
402 unsigned integers are acccepted]{2.5}
Fred Drake3adf79e2001-10-12 19:01:43 +0000403\end{cfuncdesc}
404
405
406\subsection{Floating Point Objects \label{floatObjects}}
407
408\obindex{floating point}
409\begin{ctypedesc}{PyFloatObject}
410 This subtype of \ctype{PyObject} represents a Python floating point
411 object.
412\end{ctypedesc}
413
414\begin{cvardesc}{PyTypeObject}{PyFloat_Type}
415 This instance of \ctype{PyTypeObject} represents the Python floating
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000416 point type. This is the same object as \code{float} and
417 \code{types.FloatType}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000418 \withsubitem{(in modules types)}{\ttindex{FloatType}}
419\end{cvardesc}
420
421\begin{cfuncdesc}{int}{PyFloat_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +0000422 Return true if its argument is a \ctype{PyFloatObject} or a subtype
Fred Drake3adf79e2001-10-12 19:01:43 +0000423 of \ctype{PyFloatObject}.
424 \versionchanged[Allowed subtypes to be accepted]{2.2}
425\end{cfuncdesc}
426
427\begin{cfuncdesc}{int}{PyFloat_CheckExact}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +0000428 Return true if its argument is a \ctype{PyFloatObject}, but not a
Fred Drake3adf79e2001-10-12 19:01:43 +0000429 subtype of \ctype{PyFloatObject}.
430 \versionadded{2.2}
431\end{cfuncdesc}
432
Georg Brandl428f0642007-03-18 18:35:15 +0000433\begin{cfuncdesc}{PyObject*}{PyFloat_FromString}{PyObject *str}
Georg Brandl99363b62005-09-03 07:27:26 +0000434 Create a \ctype{PyFloatObject} object based on the string value in
Georg Brandl428f0642007-03-18 18:35:15 +0000435 \var{str}, or \NULL{} on failure.
Skip Montanaroae31e9b2003-02-03 03:56:36 +0000436\end{cfuncdesc}
437
Fred Drake3adf79e2001-10-12 19:01:43 +0000438\begin{cfuncdesc}{PyObject*}{PyFloat_FromDouble}{double v}
Georg Brandl99363b62005-09-03 07:27:26 +0000439 Create a \ctype{PyFloatObject} object from \var{v}, or \NULL{} on
Fred Drake3adf79e2001-10-12 19:01:43 +0000440 failure.
441\end{cfuncdesc}
442
443\begin{cfuncdesc}{double}{PyFloat_AsDouble}{PyObject *pyfloat}
Georg Brandl99363b62005-09-03 07:27:26 +0000444 Return a C \ctype{double} representation of the contents of
Guido van Rossumd8faa362007-04-27 19:54:29 +0000445 \var{pyfloat}. If \var{pyfloat} is not a Python floating point
446 object but has a \method{__float__} method, this method will first
447 be called to convert \var{pyfloat} into a float.
Fred Drake3adf79e2001-10-12 19:01:43 +0000448\end{cfuncdesc}
449
450\begin{cfuncdesc}{double}{PyFloat_AS_DOUBLE}{PyObject *pyfloat}
Georg Brandl99363b62005-09-03 07:27:26 +0000451 Return a C \ctype{double} representation of the contents of
Fred Drake3adf79e2001-10-12 19:01:43 +0000452 \var{pyfloat}, but without error checking.
453\end{cfuncdesc}
454
455
456\subsection{Complex Number Objects \label{complexObjects}}
457
458\obindex{complex number}
459Python's complex number objects are implemented as two distinct types
460when viewed from the C API: one is the Python object exposed to
461Python programs, and the other is a C structure which represents the
462actual complex number value. The API provides functions for working
463with both.
464
465\subsubsection{Complex Numbers as C Structures}
466
467Note that the functions which accept these structures as parameters
468and return them as results do so \emph{by value} rather than
469dereferencing them through pointers. This is consistent throughout
470the API.
471
472\begin{ctypedesc}{Py_complex}
473 The C structure which corresponds to the value portion of a Python
474 complex number object. Most of the functions for dealing with
475 complex number objects use structures of this type as input or
476 output values, as appropriate. It is defined as:
477
478\begin{verbatim}
479typedef struct {
480 double real;
481 double imag;
482} Py_complex;
483\end{verbatim}
484\end{ctypedesc}
485
486\begin{cfuncdesc}{Py_complex}{_Py_c_sum}{Py_complex left, Py_complex right}
487 Return the sum of two complex numbers, using the C
488 \ctype{Py_complex} representation.
489\end{cfuncdesc}
490
491\begin{cfuncdesc}{Py_complex}{_Py_c_diff}{Py_complex left, Py_complex right}
492 Return the difference between two complex numbers, using the C
493 \ctype{Py_complex} representation.
494\end{cfuncdesc}
495
496\begin{cfuncdesc}{Py_complex}{_Py_c_neg}{Py_complex complex}
497 Return the negation of the complex number \var{complex}, using the C
498 \ctype{Py_complex} representation.
499\end{cfuncdesc}
500
501\begin{cfuncdesc}{Py_complex}{_Py_c_prod}{Py_complex left, Py_complex right}
502 Return the product of two complex numbers, using the C
503 \ctype{Py_complex} representation.
504\end{cfuncdesc}
505
506\begin{cfuncdesc}{Py_complex}{_Py_c_quot}{Py_complex dividend,
507 Py_complex divisor}
508 Return the quotient of two complex numbers, using the C
509 \ctype{Py_complex} representation.
510\end{cfuncdesc}
511
512\begin{cfuncdesc}{Py_complex}{_Py_c_pow}{Py_complex num, Py_complex exp}
513 Return the exponentiation of \var{num} by \var{exp}, using the C
514 \ctype{Py_complex} representation.
515\end{cfuncdesc}
516
517
518\subsubsection{Complex Numbers as Python Objects}
519
520\begin{ctypedesc}{PyComplexObject}
521 This subtype of \ctype{PyObject} represents a Python complex number
522 object.
523\end{ctypedesc}
524
525\begin{cvardesc}{PyTypeObject}{PyComplex_Type}
526 This instance of \ctype{PyTypeObject} represents the Python complex
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000527 number type. It is the same object as \code{complex} and
528 \code{types.ComplexType}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000529\end{cvardesc}
530
531\begin{cfuncdesc}{int}{PyComplex_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +0000532 Return true if its argument is a \ctype{PyComplexObject} or a
Fred Drake3adf79e2001-10-12 19:01:43 +0000533 subtype of \ctype{PyComplexObject}.
534 \versionchanged[Allowed subtypes to be accepted]{2.2}
535\end{cfuncdesc}
536
537\begin{cfuncdesc}{int}{PyComplex_CheckExact}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +0000538 Return true if its argument is a \ctype{PyComplexObject}, but not a
Fred Drake3adf79e2001-10-12 19:01:43 +0000539 subtype of \ctype{PyComplexObject}.
540 \versionadded{2.2}
541\end{cfuncdesc}
542
543\begin{cfuncdesc}{PyObject*}{PyComplex_FromCComplex}{Py_complex v}
544 Create a new Python complex number object from a C
545 \ctype{Py_complex} value.
546\end{cfuncdesc}
547
548\begin{cfuncdesc}{PyObject*}{PyComplex_FromDoubles}{double real, double imag}
Georg Brandl99363b62005-09-03 07:27:26 +0000549 Return a new \ctype{PyComplexObject} object from \var{real} and
Fred Drake3adf79e2001-10-12 19:01:43 +0000550 \var{imag}.
551\end{cfuncdesc}
552
553\begin{cfuncdesc}{double}{PyComplex_RealAsDouble}{PyObject *op}
Georg Brandl99363b62005-09-03 07:27:26 +0000554 Return the real part of \var{op} as a C \ctype{double}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000555\end{cfuncdesc}
556
557\begin{cfuncdesc}{double}{PyComplex_ImagAsDouble}{PyObject *op}
Georg Brandl99363b62005-09-03 07:27:26 +0000558 Return the imaginary part of \var{op} as a C \ctype{double}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000559\end{cfuncdesc}
560
561\begin{cfuncdesc}{Py_complex}{PyComplex_AsCComplex}{PyObject *op}
Guido van Rossumd8faa362007-04-27 19:54:29 +0000562 Return the \ctype{Py_complex} value of the complex number \var{op}.
563 \versionchanged[If \var{op} is not a Python complex number object
564 but has a \method{__complex__} method, this method
565 will first be called to convert \var{op} to a Python
566 complex number object]{2.6}
Fred Drake3adf79e2001-10-12 19:01:43 +0000567\end{cfuncdesc}
568
569
570
571\section{Sequence Objects \label{sequenceObjects}}
572
573\obindex{sequence}
Tim Petersf582b822001-12-11 18:51:08 +0000574Generic operations on sequence objects were discussed in the previous
575chapter; this section deals with the specific kinds of sequence
Fred Drake3adf79e2001-10-12 19:01:43 +0000576objects that are intrinsic to the Python language.
577
578
579\subsection{String Objects \label{stringObjects}}
580
581These functions raise \exception{TypeError} when expecting a string
582parameter and are called with a non-string parameter.
583
584\obindex{string}
585\begin{ctypedesc}{PyStringObject}
586 This subtype of \ctype{PyObject} represents a Python string object.
587\end{ctypedesc}
588
589\begin{cvardesc}{PyTypeObject}{PyString_Type}
590 This instance of \ctype{PyTypeObject} represents the Python string
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000591 type; it is the same object as \code{str} and \code{types.StringType}
592 in the Python layer.
Fred Drake3adf79e2001-10-12 19:01:43 +0000593 \withsubitem{(in module types)}{\ttindex{StringType}}.
594\end{cvardesc}
595
596\begin{cfuncdesc}{int}{PyString_Check}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000597 Return true if the object \var{o} is a string object or an instance
Fred Drake3adf79e2001-10-12 19:01:43 +0000598 of a subtype of the string type.
599 \versionchanged[Allowed subtypes to be accepted]{2.2}
600\end{cfuncdesc}
601
602\begin{cfuncdesc}{int}{PyString_CheckExact}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000603 Return true if the object \var{o} is a string object, but not an
Fred Drake3adf79e2001-10-12 19:01:43 +0000604 instance of a subtype of the string type.
605 \versionadded{2.2}
606\end{cfuncdesc}
607
608\begin{cfuncdesc}{PyObject*}{PyString_FromString}{const char *v}
Thomas Wouters89f507f2006-12-13 04:49:30 +0000609 Return a new string object with a copy of the string \var{v} as value
610 on success, and \NULL{} on failure. The parameter \var{v} must not be
611 \NULL{}; it will not be checked.
Fred Drake3adf79e2001-10-12 19:01:43 +0000612\end{cfuncdesc}
613
614\begin{cfuncdesc}{PyObject*}{PyString_FromStringAndSize}{const char *v,
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000615 Py_ssize_t len}
Thomas Wouters89f507f2006-12-13 04:49:30 +0000616 Return a new string object with a copy of the string \var{v} as value
617 and length \var{len} on success, and \NULL{} on failure. If \var{v} is
Tim Peters9ddf40b2004-06-20 22:41:32 +0000618 \NULL{}, the contents of the string are uninitialized.
Fred Drake3adf79e2001-10-12 19:01:43 +0000619\end{cfuncdesc}
620
621\begin{cfuncdesc}{PyObject*}{PyString_FromFormat}{const char *format, ...}
Georg Brandl99363b62005-09-03 07:27:26 +0000622 Take a C \cfunction{printf()}-style \var{format} string and a
623 variable number of arguments, calculate the size of the resulting
624 Python string and return a string with the values formatted into
Fred Drake3adf79e2001-10-12 19:01:43 +0000625 it. The variable arguments must be C types and must correspond
626 exactly to the format characters in the \var{format} string. The
627 following format characters are allowed:
628
Thomas Wouters477c8d52006-05-27 19:21:47 +0000629 % This should be exactly the same as the table in PyErr_Format.
630 % One should just refer to the other.
631
632 % The descriptions for %zd and %zu are wrong, but the truth is complicated
633 % because not all compilers support the %z width modifier -- we fake it
634 % when necessary via interpolating PY_FORMAT_SIZE_T.
635
636 % %u, %lu, %zu should have "new in Python 2.5" blurbs.
637
Fred Drake3adf79e2001-10-12 19:01:43 +0000638 \begin{tableiii}{l|l|l}{member}{Format Characters}{Type}{Comment}
639 \lineiii{\%\%}{\emph{n/a}}{The literal \% character.}
640 \lineiii{\%c}{int}{A single character, represented as an C int.}
641 \lineiii{\%d}{int}{Exactly equivalent to \code{printf("\%d")}.}
Thomas Wouters477c8d52006-05-27 19:21:47 +0000642 \lineiii{\%u}{unsigned int}{Exactly equivalent to \code{printf("\%u")}.}
Fred Drake3adf79e2001-10-12 19:01:43 +0000643 \lineiii{\%ld}{long}{Exactly equivalent to \code{printf("\%ld")}.}
Thomas Wouters477c8d52006-05-27 19:21:47 +0000644 \lineiii{\%lu}{unsigned long}{Exactly equivalent to \code{printf("\%lu")}.}
645 \lineiii{\%zd}{Py_ssize_t}{Exactly equivalent to \code{printf("\%zd")}.}
646 \lineiii{\%zu}{size_t}{Exactly equivalent to \code{printf("\%zu")}.}
Fred Drake3adf79e2001-10-12 19:01:43 +0000647 \lineiii{\%i}{int}{Exactly equivalent to \code{printf("\%i")}.}
648 \lineiii{\%x}{int}{Exactly equivalent to \code{printf("\%x")}.}
649 \lineiii{\%s}{char*}{A null-terminated C character array.}
650 \lineiii{\%p}{void*}{The hex representation of a C pointer.
651 Mostly equivalent to \code{printf("\%p")} except that it is
652 guaranteed to start with the literal \code{0x} regardless of
653 what the platform's \code{printf} yields.}
654 \end{tableiii}
Thomas Wouters477c8d52006-05-27 19:21:47 +0000655
656 An unrecognized format character causes all the rest of the format
657 string to be copied as-is to the result string, and any extra
658 arguments discarded.
Fred Drake3adf79e2001-10-12 19:01:43 +0000659\end{cfuncdesc}
660
661\begin{cfuncdesc}{PyObject*}{PyString_FromFormatV}{const char *format,
662 va_list vargs}
663 Identical to \function{PyString_FromFormat()} except that it takes
664 exactly two arguments.
665\end{cfuncdesc}
666
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000667\begin{cfuncdesc}{Py_ssize_t}{PyString_Size}{PyObject *string}
Georg Brandl99363b62005-09-03 07:27:26 +0000668 Return the length of the string in string object \var{string}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000669\end{cfuncdesc}
670
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000671\begin{cfuncdesc}{Py_ssize_t}{PyString_GET_SIZE}{PyObject *string}
Fred Drake3adf79e2001-10-12 19:01:43 +0000672 Macro form of \cfunction{PyString_Size()} but without error
673 checking.
674\end{cfuncdesc}
675
676\begin{cfuncdesc}{char*}{PyString_AsString}{PyObject *string}
Georg Brandl99363b62005-09-03 07:27:26 +0000677 Return a NUL-terminated representation of the contents of
Fred Drake3adf79e2001-10-12 19:01:43 +0000678 \var{string}. The pointer refers to the internal buffer of
679 \var{string}, not a copy. The data must not be modified in any way,
680 unless the string was just created using
681 \code{PyString_FromStringAndSize(NULL, \var{size})}.
Fred Drake4b247262002-10-22 20:20:20 +0000682 It must not be deallocated. If \var{string} is a Unicode object,
683 this function computes the default encoding of \var{string} and
684 operates on that. If \var{string} is not a string object at all,
685 \cfunction{PyString_AsString()} returns \NULL{} and raises
686 \exception{TypeError}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000687\end{cfuncdesc}
688
689\begin{cfuncdesc}{char*}{PyString_AS_STRING}{PyObject *string}
690 Macro form of \cfunction{PyString_AsString()} but without error
Fred Drake4b247262002-10-22 20:20:20 +0000691 checking. Only string objects are supported; no Unicode objects
692 should be passed.
Fred Drake3adf79e2001-10-12 19:01:43 +0000693\end{cfuncdesc}
694
695\begin{cfuncdesc}{int}{PyString_AsStringAndSize}{PyObject *obj,
696 char **buffer,
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000697 Py_ssize_t *length}
Georg Brandl99363b62005-09-03 07:27:26 +0000698 Return a NUL-terminated representation of the contents of the
Fred Drake3adf79e2001-10-12 19:01:43 +0000699 object \var{obj} through the output variables \var{buffer} and
700 \var{length}.
701
702 The function accepts both string and Unicode objects as input. For
703 Unicode objects it returns the default encoded version of the
Tim Peters9ddf40b2004-06-20 22:41:32 +0000704 object. If \var{length} is \NULL{}, the resulting buffer may not
Fred Drake4b247262002-10-22 20:20:20 +0000705 contain NUL characters; if it does, the function returns \code{-1}
706 and a \exception{TypeError} is raised.
Fred Drake3adf79e2001-10-12 19:01:43 +0000707
708 The buffer refers to an internal string buffer of \var{obj}, not a
709 copy. The data must not be modified in any way, unless the string
710 was just created using \code{PyString_FromStringAndSize(NULL,
Fred Drake4b247262002-10-22 20:20:20 +0000711 \var{size})}. It must not be deallocated. If \var{string} is a
712 Unicode object, this function computes the default encoding of
713 \var{string} and operates on that. If \var{string} is not a string
Thomas Wouters477c8d52006-05-27 19:21:47 +0000714 object at all, \cfunction{PyString_AsStringAndSize()} returns
Georg Brandle53475d2005-09-28 12:53:12 +0000715 \code{-1} and raises \exception{TypeError}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000716\end{cfuncdesc}
717
718\begin{cfuncdesc}{void}{PyString_Concat}{PyObject **string,
719 PyObject *newpart}
Georg Brandl99363b62005-09-03 07:27:26 +0000720 Create a new string object in \var{*string} containing the contents
Fred Drake3adf79e2001-10-12 19:01:43 +0000721 of \var{newpart} appended to \var{string}; the caller will own the
722 new reference. The reference to the old value of \var{string} will
723 be stolen. If the new string cannot be created, the old reference
724 to \var{string} will still be discarded and the value of
Tim Peters9ddf40b2004-06-20 22:41:32 +0000725 \var{*string} will be set to \NULL{}; the appropriate exception will
Fred Drake3adf79e2001-10-12 19:01:43 +0000726 be set.
727\end{cfuncdesc}
728
729\begin{cfuncdesc}{void}{PyString_ConcatAndDel}{PyObject **string,
730 PyObject *newpart}
Georg Brandl99363b62005-09-03 07:27:26 +0000731 Create a new string object in \var{*string} containing the contents
Fred Drake3adf79e2001-10-12 19:01:43 +0000732 of \var{newpart} appended to \var{string}. This version decrements
733 the reference count of \var{newpart}.
734\end{cfuncdesc}
735
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000736\begin{cfuncdesc}{int}{_PyString_Resize}{PyObject **string, Py_ssize_t newsize}
Fred Drake3adf79e2001-10-12 19:01:43 +0000737 A way to resize a string object even though it is ``immutable''.
738 Only use this to build up a brand new string object; don't use this
Tim Peters5de98422002-04-27 18:44:32 +0000739 if the string may already be known in other parts of the code. It
740 is an error to call this function if the refcount on the input string
741 object is not one.
742 Pass the address of an existing string object as an lvalue (it may
743 be written into), and the new size desired. On success, \var{*string}
Fred Drake432425e2002-04-29 15:17:16 +0000744 holds the resized string object and \code{0} is returned; the address in
Tim Peters5de98422002-04-27 18:44:32 +0000745 \var{*string} may differ from its input value. If the
746 reallocation fails, the original string object at \var{*string} is
747 deallocated, \var{*string} is set to \NULL{}, a memory exception is set,
Fred Drake432425e2002-04-29 15:17:16 +0000748 and \code{-1} is returned.
Fred Drake3adf79e2001-10-12 19:01:43 +0000749\end{cfuncdesc}
750
751\begin{cfuncdesc}{PyObject*}{PyString_Format}{PyObject *format,
752 PyObject *args}
Georg Brandl99363b62005-09-03 07:27:26 +0000753 Return a new string object from \var{format} and \var{args}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000754 Analogous to \code{\var{format} \%\ \var{args}}. The \var{args}
755 argument must be a tuple.
756\end{cfuncdesc}
757
758\begin{cfuncdesc}{void}{PyString_InternInPlace}{PyObject **string}
759 Intern the argument \var{*string} in place. The argument must be
760 the address of a pointer variable pointing to a Python string
761 object. If there is an existing interned string that is the same as
762 \var{*string}, it sets \var{*string} to it (decrementing the
763 reference count of the old string object and incrementing the
764 reference count of the interned string object), otherwise it leaves
765 \var{*string} alone and interns it (incrementing its reference
766 count). (Clarification: even though there is a lot of talk about
767 reference counts, think of this function as reference-count-neutral;
768 you own the object after the call if and only if you owned it before
769 the call.)
770\end{cfuncdesc}
771
772\begin{cfuncdesc}{PyObject*}{PyString_InternFromString}{const char *v}
773 A combination of \cfunction{PyString_FromString()} and
774 \cfunction{PyString_InternInPlace()}, returning either a new string
775 object that has been interned, or a new (``owned'') reference to an
776 earlier interned string object with the same value.
777\end{cfuncdesc}
778
779\begin{cfuncdesc}{PyObject*}{PyString_Decode}{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 Create an object by decoding \var{size} bytes of the encoded
Fred Drake3adf79e2001-10-12 19:01:43 +0000784 buffer \var{s} using the codec registered for
785 \var{encoding}. \var{encoding} and \var{errors} have the same
786 meaning as the parameters of the same name in the
787 \function{unicode()} built-in function. The codec to be used is
Georg Brandl99363b62005-09-03 07:27:26 +0000788 looked up using the Python codec registry. Return \NULL{} if
Fred Drake3adf79e2001-10-12 19:01:43 +0000789 an exception was raised by the codec.
790\end{cfuncdesc}
791
792\begin{cfuncdesc}{PyObject*}{PyString_AsDecodedObject}{PyObject *str,
793 const char *encoding,
794 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +0000795 Decode a string object by passing it to the codec registered for
796 \var{encoding} and return the result as Python
Fred Drake3adf79e2001-10-12 19:01:43 +0000797 object. \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\begin{cfuncdesc}{PyObject*}{PyString_Encode}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000804 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +0000805 const char *encoding,
806 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +0000807 Encode the \ctype{char} buffer of the given size by passing it to
808 the codec registered for \var{encoding} and return a Python object.
Fred Drake3adf79e2001-10-12 19:01:43 +0000809 \var{encoding} and \var{errors} have the same meaning as the
810 parameters of the same name in the string \method{encode()} method.
811 The codec to be used is looked up using the Python codec
Georg Brandl99363b62005-09-03 07:27:26 +0000812 registry. Return \NULL{} if an exception was raised by the
Fred Drake3adf79e2001-10-12 19:01:43 +0000813 codec.
814\end{cfuncdesc}
815
816\begin{cfuncdesc}{PyObject*}{PyString_AsEncodedObject}{PyObject *str,
817 const char *encoding,
818 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +0000819 Encode a string object using the codec registered for
820 \var{encoding} and return the result as Python object.
Fred Drake3adf79e2001-10-12 19:01:43 +0000821 \var{encoding} and \var{errors} have the same meaning as the
822 parameters of the same name in the string \method{encode()} method.
823 The codec to be used is looked up using the Python codec registry.
Georg Brandl99363b62005-09-03 07:27:26 +0000824 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +0000825\end{cfuncdesc}
826
827
828\subsection{Unicode Objects \label{unicodeObjects}}
829\sectionauthor{Marc-Andre Lemburg}{mal@lemburg.com}
830
831%--- Unicode Type -------------------------------------------------------
832
833These are the basic Unicode object types used for the Unicode
834implementation in Python:
835
836\begin{ctypedesc}{Py_UNICODE}
Marc-André Lemburgdf4f6e92005-10-10 19:08:41 +0000837 This type represents the storage type which is used by Python
838 internally as basis for holding Unicode ordinals. Python's default
839 builds use a 16-bit type for \ctype{Py_UNICODE} and store Unicode
840 values internally as UCS2. It is also possible to build a UCS4
841 version of Python (most recent Linux distributions come with UCS4
842 builds of Python). These builds then use a 32-bit type for
843 \ctype{Py_UNICODE} and store Unicode data internally as UCS4. On
844 platforms where \ctype{wchar_t} is available and compatible with the
845 chosen Python Unicode build variant, \ctype{Py_UNICODE} is a typedef
846 alias for \ctype{wchar_t} to enhance native platform compatibility.
847 On all other platforms, \ctype{Py_UNICODE} is a typedef alias for
848 either \ctype{unsigned short} (UCS2) or \ctype{unsigned long}
849 (UCS4).
Fred Drake3adf79e2001-10-12 19:01:43 +0000850\end{ctypedesc}
851
Marc-André Lemburgdf4f6e92005-10-10 19:08:41 +0000852Note that UCS2 and UCS4 Python builds are not binary compatible.
853Please keep this in mind when writing extensions or interfaces.
854
Fred Drake3adf79e2001-10-12 19:01:43 +0000855\begin{ctypedesc}{PyUnicodeObject}
856 This subtype of \ctype{PyObject} represents a Python Unicode object.
857\end{ctypedesc}
858
859\begin{cvardesc}{PyTypeObject}{PyUnicode_Type}
860 This instance of \ctype{PyTypeObject} represents the Python Unicode
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000861 type. It is exposed to Python code as \code{unicode} and
862 \code{types.UnicodeType}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000863\end{cvardesc}
864
865The following APIs are really C macros and can be used to do fast
866checks and to access internal read-only data of Unicode objects:
867
868\begin{cfuncdesc}{int}{PyUnicode_Check}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000869 Return true if the object \var{o} is a Unicode object or an
Fred Drake3adf79e2001-10-12 19:01:43 +0000870 instance of a Unicode subtype.
871 \versionchanged[Allowed subtypes to be accepted]{2.2}
872\end{cfuncdesc}
873
874\begin{cfuncdesc}{int}{PyUnicode_CheckExact}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000875 Return true if the object \var{o} is a Unicode object, but not an
Fred Drake3adf79e2001-10-12 19:01:43 +0000876 instance of a subtype.
877 \versionadded{2.2}
878\end{cfuncdesc}
879
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000880\begin{cfuncdesc}{Py_ssize_t}{PyUnicode_GET_SIZE}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000881 Return the size of the object. \var{o} has to be a
Fred Drake3adf79e2001-10-12 19:01:43 +0000882 \ctype{PyUnicodeObject} (not checked).
883\end{cfuncdesc}
884
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000885\begin{cfuncdesc}{Py_ssize_t}{PyUnicode_GET_DATA_SIZE}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000886 Return the size of the object's internal buffer in bytes. \var{o}
Fred Drake3adf79e2001-10-12 19:01:43 +0000887 has to be a \ctype{PyUnicodeObject} (not checked).
888\end{cfuncdesc}
889
890\begin{cfuncdesc}{Py_UNICODE*}{PyUnicode_AS_UNICODE}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000891 Return a pointer to the internal \ctype{Py_UNICODE} buffer of the
Fred Drake3adf79e2001-10-12 19:01:43 +0000892 object. \var{o} has to be a \ctype{PyUnicodeObject} (not checked).
893\end{cfuncdesc}
894
895\begin{cfuncdesc}{const char*}{PyUnicode_AS_DATA}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000896 Return a pointer to the internal buffer of the object.
Fred Drake3adf79e2001-10-12 19:01:43 +0000897 \var{o} has to be a \ctype{PyUnicodeObject} (not checked).
898\end{cfuncdesc}
899
900% --- Unicode character properties ---------------------------------------
901
902Unicode provides many different character properties. The most often
903needed ones are available through these macros which are mapped to C
904functions depending on the Python configuration.
905
906\begin{cfuncdesc}{int}{Py_UNICODE_ISSPACE}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000907 Return 1 or 0 depending on whether \var{ch} is a whitespace
Fred Drake3adf79e2001-10-12 19:01:43 +0000908 character.
909\end{cfuncdesc}
910
911\begin{cfuncdesc}{int}{Py_UNICODE_ISLOWER}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000912 Return 1 or 0 depending on whether \var{ch} is a lowercase character.
Fred Drake3adf79e2001-10-12 19:01:43 +0000913\end{cfuncdesc}
914
915\begin{cfuncdesc}{int}{Py_UNICODE_ISUPPER}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000916 Return 1 or 0 depending on whether \var{ch} is an uppercase
Fred Drake3adf79e2001-10-12 19:01:43 +0000917 character.
918\end{cfuncdesc}
919
920\begin{cfuncdesc}{int}{Py_UNICODE_ISTITLE}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000921 Return 1 or 0 depending on whether \var{ch} is a titlecase character.
Fred Drake3adf79e2001-10-12 19:01:43 +0000922\end{cfuncdesc}
923
924\begin{cfuncdesc}{int}{Py_UNICODE_ISLINEBREAK}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000925 Return 1 or 0 depending on whether \var{ch} is a linebreak character.
Fred Drake3adf79e2001-10-12 19:01:43 +0000926\end{cfuncdesc}
927
928\begin{cfuncdesc}{int}{Py_UNICODE_ISDECIMAL}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000929 Return 1 or 0 depending on whether \var{ch} is a decimal character.
Fred Drake3adf79e2001-10-12 19:01:43 +0000930\end{cfuncdesc}
931
932\begin{cfuncdesc}{int}{Py_UNICODE_ISDIGIT}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000933 Return 1 or 0 depending on whether \var{ch} is a digit character.
Fred Drake3adf79e2001-10-12 19:01:43 +0000934\end{cfuncdesc}
935
936\begin{cfuncdesc}{int}{Py_UNICODE_ISNUMERIC}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000937 Return 1 or 0 depending on whether \var{ch} is a numeric character.
Fred Drake3adf79e2001-10-12 19:01:43 +0000938\end{cfuncdesc}
939
940\begin{cfuncdesc}{int}{Py_UNICODE_ISALPHA}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000941 Return 1 or 0 depending on whether \var{ch} is an alphabetic
Fred Drake3adf79e2001-10-12 19:01:43 +0000942 character.
943\end{cfuncdesc}
944
945\begin{cfuncdesc}{int}{Py_UNICODE_ISALNUM}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000946 Return 1 or 0 depending on whether \var{ch} is an alphanumeric
Fred Drake3adf79e2001-10-12 19:01:43 +0000947 character.
948\end{cfuncdesc}
949
950These APIs can be used for fast direct character conversions:
951
952\begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOLOWER}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000953 Return the character \var{ch} converted to lower case.
Fred Drake3adf79e2001-10-12 19:01:43 +0000954\end{cfuncdesc}
955
956\begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOUPPER}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000957 Return the character \var{ch} converted to upper case.
Fred Drake3adf79e2001-10-12 19:01:43 +0000958\end{cfuncdesc}
959
960\begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOTITLE}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000961 Return the character \var{ch} converted to title case.
Fred Drake3adf79e2001-10-12 19:01:43 +0000962\end{cfuncdesc}
963
964\begin{cfuncdesc}{int}{Py_UNICODE_TODECIMAL}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000965 Return the character \var{ch} converted to a decimal positive
966 integer. Return \code{-1} if this is not possible. This macro
967 does not raise exceptions.
Fred Drake3adf79e2001-10-12 19:01:43 +0000968\end{cfuncdesc}
969
970\begin{cfuncdesc}{int}{Py_UNICODE_TODIGIT}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000971 Return the character \var{ch} converted to a single digit integer.
972 Return \code{-1} if this is not possible. This macro does not raise
Fred Drake3adf79e2001-10-12 19:01:43 +0000973 exceptions.
974\end{cfuncdesc}
975
976\begin{cfuncdesc}{double}{Py_UNICODE_TONUMERIC}{Py_UNICODE ch}
Thomas Wouters477c8d52006-05-27 19:21:47 +0000977 Return the character \var{ch} converted to a double.
Georg Brandl99363b62005-09-03 07:27:26 +0000978 Return \code{-1.0} if this is not possible. This macro does not raise
Fred Drake3adf79e2001-10-12 19:01:43 +0000979 exceptions.
980\end{cfuncdesc}
981
982% --- Plain Py_UNICODE ---------------------------------------------------
983
984To create Unicode objects and access their basic sequence properties,
985use these APIs:
986
987\begin{cfuncdesc}{PyObject*}{PyUnicode_FromUnicode}{const Py_UNICODE *u,
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000988 Py_ssize_t size}
Fred Drake3adf79e2001-10-12 19:01:43 +0000989 Create a Unicode Object from the Py_UNICODE buffer \var{u} of the
990 given size. \var{u} may be \NULL{} which causes the contents to be
991 undefined. It is the user's responsibility to fill in the needed
992 data. The buffer is copied into the new object. If the buffer is
Tim Peters9ddf40b2004-06-20 22:41:32 +0000993 not \NULL{}, the return value might be a shared object. Therefore,
Fred Drake3adf79e2001-10-12 19:01:43 +0000994 modification of the resulting Unicode object is only allowed when
Tim Peters9ddf40b2004-06-20 22:41:32 +0000995 \var{u} is \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000996\end{cfuncdesc}
997
Walter Dörwaldacaa5a12007-05-05 12:00:46 +0000998\begin{cfuncdesc}{PyObject*}{PyUnicode_FromString}{const char *u}
Walter Dörwald1d0476b2007-05-24 19:10:53 +0000999 Create a Unicode Object from the char buffer \var{u}.
Walter Dörwaldacaa5a12007-05-05 12:00:46 +00001000 \var{u} must be 0-terminated, the bytes will be interpreted as
1001 being latin-1 encoded. \var{u} may also be \NULL{} which causes the
1002 contents to be undefined. It is the user's responsibility to fill
1003 in the needed data. The buffer is copied into the new object.
1004 If the buffer is not \NULL{}, the return value might be a shared object.
1005 Therefore, modification of the resulting Unicode object is only allowed
1006 when \var{u} is \NULL{}.
1007\end{cfuncdesc}
1008
Walter Dörwaldc0aa45f2007-06-11 16:43:18 +00001009\begin{cfuncdesc}{PyObject*}{PyUnicode_FromFormat}{const char *format, ...}
1010 Take a C \cfunction{printf()}-style \var{format} string and a
1011 variable number of arguments, calculate the size of the resulting
1012 Python unicode string and return a string with the values formatted into
1013 it. The variable arguments must be C types and must correspond
1014 exactly to the format characters in the \var{format} string. The
1015 following format characters are allowed:
1016
1017 % The descriptions for %zd and %zu are wrong, but the truth is complicated
1018 % because not all compilers support the %z width modifier -- we fake it
1019 % when necessary via interpolating PY_FORMAT_SIZE_T.
1020
1021 % %u, %lu, %zu should have "new in Python 2.5" blurbs.
1022
1023 \begin{tableiii}{l|l|l}{member}{Format Characters}{Type}{Comment}
1024 \lineiii{\%\%}{\emph{n/a}}{The literal \% character.}
1025 \lineiii{\%c}{int}{A single character, represented as an C int.}
1026 \lineiii{\%d}{int}{Exactly equivalent to \code{printf("\%d")}.}
1027 \lineiii{\%u}{unsigned int}{Exactly equivalent to \code{printf("\%u")}.}
1028 \lineiii{\%ld}{long}{Exactly equivalent to \code{printf("\%ld")}.}
1029 \lineiii{\%lu}{unsigned long}{Exactly equivalent to \code{printf("\%lu")}.}
1030 \lineiii{\%zd}{Py_ssize_t}{Exactly equivalent to \code{printf("\%zd")}.}
1031 \lineiii{\%zu}{size_t}{Exactly equivalent to \code{printf("\%zu")}.}
1032 \lineiii{\%i}{int}{Exactly equivalent to \code{printf("\%i")}.}
1033 \lineiii{\%x}{int}{Exactly equivalent to \code{printf("\%x")}.}
1034 \lineiii{\%s}{char*}{A null-terminated C character array.}
1035 \lineiii{\%p}{void*}{The hex representation of a C pointer.
1036 Mostly equivalent to \code{printf("\%p")} except that it is
1037 guaranteed to start with the literal \code{0x} regardless of
1038 what the platform's \code{printf} yields.}
1039 \lineiii{\%U}{PyObject*}{A unicode object.}
1040 \lineiii{\%V}{PyObject*, char *}{A unicode object (which may be \NULL{})
1041 and a null-terminated C character array as a second parameter (which
1042 will be used, if the first parameter is \NULL{}).}
1043 \lineiii{\%S}{PyObject*}{The result of calling \function{PyObject_Unicode()}.}
1044 \lineiii{\%R}{PyObject*}{The result of calling \function{PyObject_Repr()}.}
1045 \end{tableiii}
1046
1047 An unrecognized format character causes all the rest of the format
1048 string to be copied as-is to the result string, and any extra
1049 arguments discarded.
1050\end{cfuncdesc}
1051
1052\begin{cfuncdesc}{PyObject*}{PyUnicode_FromFormatV}{const char *format,
1053 va_list vargs}
1054 Identical to \function{PyUnicode_FromFormat()} except that it takes
1055 exactly two arguments.
1056\end{cfuncdesc}
1057
Fred Drake3adf79e2001-10-12 19:01:43 +00001058\begin{cfuncdesc}{Py_UNICODE*}{PyUnicode_AsUnicode}{PyObject *unicode}
1059 Return a read-only pointer to the Unicode object's internal
1060 \ctype{Py_UNICODE} buffer, \NULL{} if \var{unicode} is not a Unicode
1061 object.
1062\end{cfuncdesc}
1063
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001064\begin{cfuncdesc}{Py_ssize_t}{PyUnicode_GetSize}{PyObject *unicode}
Fred Drake3adf79e2001-10-12 19:01:43 +00001065 Return the length of the Unicode object.
1066\end{cfuncdesc}
1067
1068\begin{cfuncdesc}{PyObject*}{PyUnicode_FromEncodedObject}{PyObject *obj,
1069 const char *encoding,
1070 const char *errors}
1071 Coerce an encoded object \var{obj} to an Unicode object and return a
1072 reference with incremented refcount.
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001073
1074 String and other char buffer compatible objects are decoded
1075 according to the given encoding and using the error handling
1076 defined by errors. Both can be \NULL{} to have the interface
1077 use the default values (see the next section for details).
Fred Drake3adf79e2001-10-12 19:01:43 +00001078
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001079 All other objects, including Unicode objects, cause a
1080 \exception{TypeError} to be set.
Fred Drake3adf79e2001-10-12 19:01:43 +00001081
1082 The API returns \NULL{} if there was an error. The caller is
1083 responsible for decref'ing the returned objects.
1084\end{cfuncdesc}
1085
1086\begin{cfuncdesc}{PyObject*}{PyUnicode_FromObject}{PyObject *obj}
1087 Shortcut for \code{PyUnicode_FromEncodedObject(obj, NULL, "strict")}
1088 which is used throughout the interpreter whenever coercion to
1089 Unicode is needed.
1090\end{cfuncdesc}
1091
1092% --- wchar_t support for platforms which support it ---------------------
1093
1094If the platform supports \ctype{wchar_t} and provides a header file
1095wchar.h, Python can interface directly to this type using the
1096following functions. Support is optimized if Python's own
1097\ctype{Py_UNICODE} type is identical to the system's \ctype{wchar_t}.
1098
1099\begin{cfuncdesc}{PyObject*}{PyUnicode_FromWideChar}{const wchar_t *w,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001100 Py_ssize_t size}
Thomas Heller541703b2002-04-29 17:28:43 +00001101 Create a Unicode object from the \ctype{wchar_t} buffer \var{w} of
Georg Brandl99363b62005-09-03 07:27:26 +00001102 the given size. Return \NULL{} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00001103\end{cfuncdesc}
1104
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001105\begin{cfuncdesc}{Py_ssize_t}{PyUnicode_AsWideChar}{PyUnicodeObject *unicode,
Fred Drake3adf79e2001-10-12 19:01:43 +00001106 wchar_t *w,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001107 Py_ssize_t size}
Georg Brandl99363b62005-09-03 07:27:26 +00001108 Copy the Unicode object contents into the \ctype{wchar_t} buffer
Marc-André Lemburga9cadcd2004-11-22 13:02:31 +00001109 \var{w}. At most \var{size} \ctype{wchar_t} characters are copied
Georg Brandl99363b62005-09-03 07:27:26 +00001110 (excluding a possibly trailing 0-termination character). Return
Marc-André Lemburga9cadcd2004-11-22 13:02:31 +00001111 the number of \ctype{wchar_t} characters copied or -1 in case of an
1112 error. Note that the resulting \ctype{wchar_t} string may or may
1113 not be 0-terminated. It is the responsibility of the caller to make
1114 sure that the \ctype{wchar_t} string is 0-terminated in case this is
1115 required by the application.
Fred Drake3adf79e2001-10-12 19:01:43 +00001116\end{cfuncdesc}
1117
1118
1119\subsubsection{Built-in Codecs \label{builtinCodecs}}
1120
1121Python provides a set of builtin codecs which are written in C
1122for speed. All of these codecs are directly usable via the
1123following functions.
1124
1125Many of the following APIs take two arguments encoding and
1126errors. These parameters encoding and errors have the same semantics
1127as the ones of the builtin unicode() Unicode object constructor.
1128
1129Setting encoding to \NULL{} causes the default encoding to be used
1130which is \ASCII. The file system calls should use
1131\cdata{Py_FileSystemDefaultEncoding} as the encoding for file
1132names. This variable should be treated as read-only: On some systems,
1133it will be a pointer to a static string, on others, it will change at
Raymond Hettingercb2da432003-10-12 18:24:34 +00001134run-time (such as when the application invokes setlocale).
Fred Drake3adf79e2001-10-12 19:01:43 +00001135
1136Error handling is set by errors which may also be set to \NULL{}
1137meaning to use the default handling defined for the codec. Default
1138error handling for all builtin codecs is ``strict''
1139(\exception{ValueError} is raised).
1140
1141The codecs all use a similar interface. Only deviation from the
1142following generic ones are documented for simplicity.
1143
1144% --- Generic Codecs -----------------------------------------------------
1145
1146These are the generic codec APIs:
1147
1148\begin{cfuncdesc}{PyObject*}{PyUnicode_Decode}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001149 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001150 const char *encoding,
1151 const char *errors}
1152 Create a Unicode object by decoding \var{size} bytes of the encoded
1153 string \var{s}. \var{encoding} and \var{errors} have the same
1154 meaning as the parameters of the same name in the
1155 \function{unicode()} builtin function. The codec to be used is
Georg Brandl99363b62005-09-03 07:27:26 +00001156 looked up using the Python codec registry. Return \NULL{} if an
Fred Drake3adf79e2001-10-12 19:01:43 +00001157 exception was raised by the codec.
1158\end{cfuncdesc}
1159
1160\begin{cfuncdesc}{PyObject*}{PyUnicode_Encode}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001161 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001162 const char *encoding,
1163 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001164 Encode the \ctype{Py_UNICODE} buffer of the given size and return
Fred Drake3adf79e2001-10-12 19:01:43 +00001165 a Python string object. \var{encoding} and \var{errors} have the
1166 same meaning as the parameters of the same name in the Unicode
1167 \method{encode()} method. The codec to be used is looked up using
Georg Brandl99363b62005-09-03 07:27:26 +00001168 the Python codec registry. Return \NULL{} if an exception was
Fred Drake3adf79e2001-10-12 19:01:43 +00001169 raised by the codec.
1170\end{cfuncdesc}
1171
1172\begin{cfuncdesc}{PyObject*}{PyUnicode_AsEncodedString}{PyObject *unicode,
1173 const char *encoding,
1174 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001175 Encode a Unicode object and return the result as Python string
Fred Drake3adf79e2001-10-12 19:01:43 +00001176 object. \var{encoding} and \var{errors} have the same meaning as the
1177 parameters of the same name in the Unicode \method{encode()} method.
1178 The codec to be used is looked up using the Python codec registry.
Georg Brandl99363b62005-09-03 07:27:26 +00001179 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +00001180\end{cfuncdesc}
1181
1182% --- UTF-8 Codecs -------------------------------------------------------
1183
1184These are the UTF-8 codec APIs:
1185
1186\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF8}{const char *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}
Georg Brandl99363b62005-09-03 07:27:26 +00001189 Create a Unicode object by decoding \var{size} bytes of the UTF-8
1190 encoded string \var{s}. Return \NULL{} if an exception was raised
Fred Drake3adf79e2001-10-12 19:01:43 +00001191 by the codec.
1192\end{cfuncdesc}
1193
Walter Dörwald69652032004-09-07 20:24:22 +00001194\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF8Stateful}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001195 Py_ssize_t size,
Walter Dörwald69652032004-09-07 20:24:22 +00001196 const char *errors,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001197 Py_ssize_t *consumed}
Georg Brandl99363b62005-09-03 07:27:26 +00001198 If \var{consumed} is \NULL{}, behave like \cfunction{PyUnicode_DecodeUTF8()}.
Walter Dörwald69652032004-09-07 20:24:22 +00001199 If \var{consumed} is not \NULL{}, trailing incomplete UTF-8 byte sequences
1200 will not be treated as an error. Those bytes will not be decoded and the
1201 number of bytes that have been decoded will be stored in \var{consumed}.
1202 \versionadded{2.4}
1203\end{cfuncdesc}
1204
Fred Drake3adf79e2001-10-12 19:01:43 +00001205\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUTF8}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001206 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001207 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001208 Encode the \ctype{Py_UNICODE} buffer of the given size using UTF-8
1209 and return a Python string object. Return \NULL{} if an exception
Fred Drake3adf79e2001-10-12 19:01:43 +00001210 was raised by the codec.
1211\end{cfuncdesc}
1212
1213\begin{cfuncdesc}{PyObject*}{PyUnicode_AsUTF8String}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001214 Encode a Unicode objects using UTF-8 and return the result as
1215 Python string object. Error handling is ``strict''. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00001216 \NULL{} if an exception was raised by the codec.
1217\end{cfuncdesc}
1218
1219% --- UTF-16 Codecs ------------------------------------------------------ */
1220
1221These are the UTF-16 codec APIs:
1222
1223\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF16}{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,
1226 int *byteorder}
Georg Brandl99363b62005-09-03 07:27:26 +00001227 Decode \var{length} bytes from a UTF-16 encoded buffer string and
1228 return the corresponding Unicode object. \var{errors} (if
Tim Peters9ddf40b2004-06-20 22:41:32 +00001229 non-\NULL{}) defines the error handling. It defaults to ``strict''.
Fred Drake3adf79e2001-10-12 19:01:43 +00001230
Tim Peters9ddf40b2004-06-20 22:41:32 +00001231 If \var{byteorder} is non-\NULL{}, the decoder starts decoding using
Fred Drake3adf79e2001-10-12 19:01:43 +00001232 the given byte order:
1233
1234\begin{verbatim}
1235 *byteorder == -1: little endian
1236 *byteorder == 0: native order
1237 *byteorder == 1: big endian
1238\end{verbatim}
1239
Guido van Rossum360e4b82007-05-14 22:51:27 +00001240 and then switches if the first two bytes of the input data are a byte order
1241 mark (BOM) and the specified byte order is native order. This BOM is not
1242 copied into the resulting Unicode string. After completion, \var{*byteorder}
1243 is set to the current byte order at the.
Fred Drake3adf79e2001-10-12 19:01:43 +00001244
Tim Peters9ddf40b2004-06-20 22:41:32 +00001245 If \var{byteorder} is \NULL{}, the codec starts in native order mode.
Fred Drake3adf79e2001-10-12 19:01:43 +00001246
Georg Brandl99363b62005-09-03 07:27:26 +00001247 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +00001248\end{cfuncdesc}
1249
Walter Dörwald69652032004-09-07 20:24:22 +00001250\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF16Stateful}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001251 Py_ssize_t size,
Walter Dörwald69652032004-09-07 20:24:22 +00001252 const char *errors,
1253 int *byteorder,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001254 Py_ssize_t *consumed}
Georg Brandl99363b62005-09-03 07:27:26 +00001255 If \var{consumed} is \NULL{}, behave like
Walter Dörwald69652032004-09-07 20:24:22 +00001256 \cfunction{PyUnicode_DecodeUTF16()}. If \var{consumed} is not \NULL{},
1257 \cfunction{PyUnicode_DecodeUTF16Stateful()} will not treat trailing incomplete
Raymond Hettinger0c230b92005-08-17 10:05:22 +00001258 UTF-16 byte sequences (such as an odd number of bytes or a split surrogate pair)
Walter Dörwald69652032004-09-07 20:24:22 +00001259 as an error. Those bytes will not be decoded and the number of bytes that
1260 have been decoded will be stored in \var{consumed}.
1261 \versionadded{2.4}
1262\end{cfuncdesc}
1263
Fred Drake3adf79e2001-10-12 19:01:43 +00001264\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUTF16}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001265 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001266 const char *errors,
1267 int byteorder}
Georg Brandl99363b62005-09-03 07:27:26 +00001268 Return a Python string object holding the UTF-16 encoded value of
Fred Drake3adf79e2001-10-12 19:01:43 +00001269 the Unicode data in \var{s}. If \var{byteorder} is not \code{0},
1270 output is written according to the following byte order:
1271
1272\begin{verbatim}
1273 byteorder == -1: little endian
1274 byteorder == 0: native byte order (writes a BOM mark)
1275 byteorder == 1: big endian
1276\end{verbatim}
1277
1278 If byteorder is \code{0}, the output string will always start with
1279 the Unicode BOM mark (U+FEFF). In the other two modes, no BOM mark
1280 is prepended.
1281
Martin v. Löwis9bc4f2d2004-06-03 09:55:28 +00001282 If \var{Py_UNICODE_WIDE} is defined, a single \ctype{Py_UNICODE}
1283 value may get represented as a surrogate pair. If it is not
1284 defined, each \ctype{Py_UNICODE} values is interpreted as an
1285 UCS-2 character.
Fred Drake3adf79e2001-10-12 19:01:43 +00001286
Georg Brandl99363b62005-09-03 07:27:26 +00001287 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +00001288\end{cfuncdesc}
1289
1290\begin{cfuncdesc}{PyObject*}{PyUnicode_AsUTF16String}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001291 Return a Python string using the UTF-16 encoding in native byte
Fred Drake3adf79e2001-10-12 19:01:43 +00001292 order. The string always starts with a BOM mark. Error handling is
Georg Brandl99363b62005-09-03 07:27:26 +00001293 ``strict''. Return \NULL{} if an exception was raised by the
Fred Drake3adf79e2001-10-12 19:01:43 +00001294 codec.
1295\end{cfuncdesc}
1296
1297% --- Unicode-Escape Codecs ----------------------------------------------
1298
Martin v. Löwis95cf84a2003-10-19 07:32:24 +00001299These are the ``Unicode Escape'' codec APIs:
Fred Drake3adf79e2001-10-12 19:01:43 +00001300
1301\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUnicodeEscape}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001302 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001303 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001304 Create a Unicode object by decoding \var{size} bytes of the
1305 Unicode-Escape encoded string \var{s}. Return \NULL{} if an
Fred Drake3adf79e2001-10-12 19:01:43 +00001306 exception was raised by the codec.
1307\end{cfuncdesc}
1308
1309\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUnicodeEscape}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001310 Py_ssize_t size}
Georg Brandl99363b62005-09-03 07:27:26 +00001311 Encode the \ctype{Py_UNICODE} buffer of the given size using
1312 Unicode-Escape and return a Python string object. Return \NULL{}
Fred Drake3adf79e2001-10-12 19:01:43 +00001313 if an exception was raised by the codec.
1314\end{cfuncdesc}
1315
1316\begin{cfuncdesc}{PyObject*}{PyUnicode_AsUnicodeEscapeString}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001317 Encode a Unicode objects using Unicode-Escape and return the
Fred Drake3adf79e2001-10-12 19:01:43 +00001318 result as Python string object. Error handling is ``strict''.
Georg Brandl99363b62005-09-03 07:27:26 +00001319 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +00001320\end{cfuncdesc}
1321
1322% --- Raw-Unicode-Escape Codecs ------------------------------------------
1323
Martin v. Löwis95cf84a2003-10-19 07:32:24 +00001324These are the ``Raw Unicode Escape'' codec APIs:
Fred Drake3adf79e2001-10-12 19:01:43 +00001325
1326\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeRawUnicodeEscape}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001327 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001328 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001329 Create a Unicode object by decoding \var{size} bytes of the
1330 Raw-Unicode-Escape encoded string \var{s}. Return \NULL{} if an
Fred Drake3adf79e2001-10-12 19:01:43 +00001331 exception was raised by the codec.
1332\end{cfuncdesc}
1333
1334\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeRawUnicodeEscape}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001335 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001336 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001337 Encode the \ctype{Py_UNICODE} buffer of the given size using
1338 Raw-Unicode-Escape and return a Python string object. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00001339 \NULL{} if an exception was raised by the codec.
1340\end{cfuncdesc}
1341
1342\begin{cfuncdesc}{PyObject*}{PyUnicode_AsRawUnicodeEscapeString}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001343 Encode a Unicode objects using Raw-Unicode-Escape and return the
Fred Drake3adf79e2001-10-12 19:01:43 +00001344 result as Python string object. Error handling is ``strict''.
Georg Brandl99363b62005-09-03 07:27:26 +00001345 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +00001346\end{cfuncdesc}
1347
Tim Petersf582b822001-12-11 18:51:08 +00001348% --- Latin-1 Codecs -----------------------------------------------------
Fred Drake3adf79e2001-10-12 19:01:43 +00001349
1350These are the Latin-1 codec APIs:
1351Latin-1 corresponds to the first 256 Unicode ordinals and only these
1352are accepted by the codecs during encoding.
1353
1354\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeLatin1}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001355 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001356 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001357 Create a Unicode object by decoding \var{size} bytes of the Latin-1
1358 encoded string \var{s}. Return \NULL{} if an exception was raised
Fred Drake3adf79e2001-10-12 19:01:43 +00001359 by the codec.
1360\end{cfuncdesc}
1361
1362\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeLatin1}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001363 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001364 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001365 Encode the \ctype{Py_UNICODE} buffer of the given size using
1366 Latin-1 and return a Python string object. Return \NULL{} if an
Fred Drake3adf79e2001-10-12 19:01:43 +00001367 exception was raised by the codec.
1368\end{cfuncdesc}
1369
1370\begin{cfuncdesc}{PyObject*}{PyUnicode_AsLatin1String}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001371 Encode a Unicode objects using Latin-1 and return the result as
1372 Python string object. Error handling is ``strict''. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00001373 \NULL{} if an exception was raised by the codec.
1374\end{cfuncdesc}
1375
Tim Petersf582b822001-12-11 18:51:08 +00001376% --- ASCII Codecs -------------------------------------------------------
Fred Drake3adf79e2001-10-12 19:01:43 +00001377
1378These are the \ASCII{} codec APIs. Only 7-bit \ASCII{} data is
1379accepted. All other codes generate errors.
1380
1381\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeASCII}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001382 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001383 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001384 Create a Unicode object by decoding \var{size} bytes of the
1385 \ASCII{} encoded string \var{s}. Return \NULL{} if an exception
Fred Drake3adf79e2001-10-12 19:01:43 +00001386 was raised by the codec.
1387\end{cfuncdesc}
1388
1389\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeASCII}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001390 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001391 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001392 Encode the \ctype{Py_UNICODE} buffer of the given size using
1393 \ASCII{} and return a Python string object. Return \NULL{} if an
Fred Drake3adf79e2001-10-12 19:01:43 +00001394 exception was raised by the codec.
1395\end{cfuncdesc}
1396
1397\begin{cfuncdesc}{PyObject*}{PyUnicode_AsASCIIString}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001398 Encode a Unicode objects using \ASCII{} and return the result as
1399 Python string object. Error handling is ``strict''. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00001400 \NULL{} if an exception was raised by the codec.
1401\end{cfuncdesc}
1402
Tim Petersf582b822001-12-11 18:51:08 +00001403% --- Character Map Codecs -----------------------------------------------
Fred Drake3adf79e2001-10-12 19:01:43 +00001404
1405These are the mapping codec APIs:
1406
1407This codec is special in that it can be used to implement many
1408different codecs (and this is in fact what was done to obtain most of
1409the standard codecs included in the \module{encodings} package). The
1410codec uses mapping to encode and decode characters.
1411
1412Decoding mappings must map single string characters to single Unicode
1413characters, integers (which are then interpreted as Unicode ordinals)
Tim Petersf582b822001-12-11 18:51:08 +00001414or None (meaning "undefined mapping" and causing an error).
Fred Drake3adf79e2001-10-12 19:01:43 +00001415
1416Encoding mappings must map single Unicode characters to single string
1417characters, integers (which are then interpreted as Latin-1 ordinals)
1418or None (meaning "undefined mapping" and causing an error).
1419
1420The mapping objects provided must only support the __getitem__ mapping
1421interface.
1422
1423If a character lookup fails with a LookupError, the character is
1424copied as-is meaning that its ordinal value will be interpreted as
1425Unicode or Latin-1 ordinal resp. Because of this, mappings only need
1426to contain those mappings which map characters to different code
1427points.
1428
1429\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeCharmap}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001430 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001431 PyObject *mapping,
1432 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001433 Create a Unicode object by decoding \var{size} bytes of the encoded
1434 string \var{s} using the given \var{mapping} object. Return
Walter Dörwaldd1c1e102005-10-06 20:29:57 +00001435 \NULL{} if an exception was raised by the codec. If \var{mapping} is \NULL{}
1436 latin-1 decoding will be done. Else it can be a dictionary mapping byte or a
1437 unicode string, which is treated as a lookup table. Byte values greater
1438 that the length of the string and U+FFFE "characters" are treated as
1439 "undefined mapping".
1440 \versionchanged[Allowed unicode string as mapping argument]{2.4}
Fred Drake3adf79e2001-10-12 19:01:43 +00001441\end{cfuncdesc}
1442
1443\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeCharmap}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001444 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001445 PyObject *mapping,
1446 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001447 Encode the \ctype{Py_UNICODE} buffer of the given size using the
1448 given \var{mapping} object and return a Python string object.
1449 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +00001450\end{cfuncdesc}
1451
1452\begin{cfuncdesc}{PyObject*}{PyUnicode_AsCharmapString}{PyObject *unicode,
1453 PyObject *mapping}
Georg Brandl99363b62005-09-03 07:27:26 +00001454 Encode a Unicode objects using the given \var{mapping} object and
1455 return the result as Python string object. Error handling is
1456 ``strict''. Return \NULL{} if an exception was raised by the
Fred Drake3adf79e2001-10-12 19:01:43 +00001457 codec.
1458\end{cfuncdesc}
1459
1460The following codec API is special in that maps Unicode to Unicode.
1461
1462\begin{cfuncdesc}{PyObject*}{PyUnicode_TranslateCharmap}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001463 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001464 PyObject *table,
1465 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001466 Translate a \ctype{Py_UNICODE} buffer of the given length by
1467 applying a character mapping \var{table} to it and return the
1468 resulting Unicode object. Return \NULL{} when an exception was
Fred Drake3adf79e2001-10-12 19:01:43 +00001469 raised by the codec.
1470
1471 The \var{mapping} table must map Unicode ordinal integers to Unicode
1472 ordinal integers or None (causing deletion of the character).
1473
Thomas Wouters477c8d52006-05-27 19:21:47 +00001474 Mapping tables need only provide the \method{__getitem__()}
Fred Drake3adf79e2001-10-12 19:01:43 +00001475 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\end{cfuncdesc}
1479
1480% --- MBCS codecs for Windows --------------------------------------------
1481
1482These are the MBCS codec APIs. They are currently only available on
1483Windows and use the Win32 MBCS converters to implement the
1484conversions. Note that MBCS (or DBCS) is a class of encodings, not
1485just one. The target encoding is defined by the user settings on the
1486machine running the codec.
1487
1488\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeMBCS}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001489 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001490 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001491 Create a Unicode object by decoding \var{size} bytes of the MBCS
1492 encoded string \var{s}. Return \NULL{} if an exception was
Fred Drake3adf79e2001-10-12 19:01:43 +00001493 raised by the codec.
1494\end{cfuncdesc}
1495
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001496\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeMBCSStateful}{const char *s,
1497 int size,
1498 const char *errors,
1499 int *consumed}
1500 If \var{consumed} is \NULL{}, behave like
1501 \cfunction{PyUnicode_DecodeMBCS()}. If \var{consumed} is not \NULL{},
1502 \cfunction{PyUnicode_DecodeMBCSStateful()} will not decode trailing lead
1503 byte and the number of bytes that have been decoded will be stored in
1504 \var{consumed}.
1505 \versionadded{2.5}
1506\end{cfuncdesc}
1507
Fred Drake3adf79e2001-10-12 19:01:43 +00001508\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeMBCS}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001509 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001510 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001511 Encode the \ctype{Py_UNICODE} buffer of the given size using MBCS
1512 and return a Python string object. Return \NULL{} if an exception
Fred Drake3adf79e2001-10-12 19:01:43 +00001513 was raised by the codec.
1514\end{cfuncdesc}
1515
1516\begin{cfuncdesc}{PyObject*}{PyUnicode_AsMBCSString}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001517 Encode a Unicode objects using MBCS and return the result as
1518 Python string object. Error handling is ``strict''. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00001519 \NULL{} if an exception was raised by the codec.
1520\end{cfuncdesc}
1521
1522% --- Methods & Slots ----------------------------------------------------
1523
1524\subsubsection{Methods and Slot Functions \label{unicodeMethodsAndSlots}}
1525
1526The following APIs are capable of handling Unicode objects and strings
1527on input (we refer to them as strings in the descriptions) and return
Martin v. Löwis95cf84a2003-10-19 07:32:24 +00001528Unicode objects or integers as appropriate.
Fred Drake3adf79e2001-10-12 19:01:43 +00001529
1530They all return \NULL{} or \code{-1} if an exception occurs.
1531
1532\begin{cfuncdesc}{PyObject*}{PyUnicode_Concat}{PyObject *left,
1533 PyObject *right}
1534 Concat two strings giving a new Unicode string.
1535\end{cfuncdesc}
1536
1537\begin{cfuncdesc}{PyObject*}{PyUnicode_Split}{PyObject *s,
1538 PyObject *sep,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001539 Py_ssize_t maxsplit}
Tim Peters9ddf40b2004-06-20 22:41:32 +00001540 Split a string giving a list of Unicode strings. If sep is \NULL{},
Fred Drake3adf79e2001-10-12 19:01:43 +00001541 splitting will be done at all whitespace substrings. Otherwise,
1542 splits occur at the given separator. At most \var{maxsplit} splits
1543 will be done. If negative, no limit is set. Separators are not
1544 included in the resulting list.
1545\end{cfuncdesc}
1546
1547\begin{cfuncdesc}{PyObject*}{PyUnicode_Splitlines}{PyObject *s,
Martin v. Löwis24b88812003-03-30 16:40:42 +00001548 int keepend}
Fred Drake3adf79e2001-10-12 19:01:43 +00001549 Split a Unicode string at line breaks, returning a list of Unicode
Martin v. Löwis24b88812003-03-30 16:40:42 +00001550 strings. CRLF is considered to be one line break. If \var{keepend}
1551 is 0, the Line break characters are not included in the resulting
1552 strings.
Fred Drake3adf79e2001-10-12 19:01:43 +00001553\end{cfuncdesc}
1554
1555\begin{cfuncdesc}{PyObject*}{PyUnicode_Translate}{PyObject *str,
1556 PyObject *table,
1557 const char *errors}
1558 Translate a string by applying a character mapping table to it and
1559 return the resulting Unicode object.
1560
1561 The mapping table must map Unicode ordinal integers to Unicode
1562 ordinal integers or None (causing deletion of the character).
1563
1564 Mapping tables need only provide the \method{__getitem__()}
1565 interface; dictionaries and sequences work well. Unmapped character
1566 ordinals (ones which cause a \exception{LookupError}) are left
1567 untouched and are copied as-is.
1568
1569 \var{errors} has the usual meaning for codecs. It may be \NULL{}
1570 which indicates to use the default error handling.
1571\end{cfuncdesc}
1572
1573\begin{cfuncdesc}{PyObject*}{PyUnicode_Join}{PyObject *separator,
1574 PyObject *seq}
1575 Join a sequence of strings using the given separator and return the
1576 resulting Unicode string.
1577\end{cfuncdesc}
1578
Raymond Hettinger8ef9b3e2004-12-10 17:12:32 +00001579\begin{cfuncdesc}{int}{PyUnicode_Tailmatch}{PyObject *str,
Fred Drake3adf79e2001-10-12 19:01:43 +00001580 PyObject *substr,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001581 Py_ssize_t start,
1582 Py_ssize_t end,
Fred Drake3adf79e2001-10-12 19:01:43 +00001583 int direction}
1584 Return 1 if \var{substr} matches \var{str}[\var{start}:\var{end}] at
1585 the given tail end (\var{direction} == -1 means to do a prefix
1586 match, \var{direction} == 1 a suffix match), 0 otherwise.
Thomas Wouters477c8d52006-05-27 19:21:47 +00001587 Return \code{-1} if an error occurred.
Fred Drake3adf79e2001-10-12 19:01:43 +00001588\end{cfuncdesc}
1589
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001590\begin{cfuncdesc}{Py_ssize_t}{PyUnicode_Find}{PyObject *str,
Fred Drake1d1e1db2002-06-20 22:07:04 +00001591 PyObject *substr,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001592 Py_ssize_t start,
1593 Py_ssize_t end,
Fred Drake1d1e1db2002-06-20 22:07:04 +00001594 int direction}
Fred Drake3adf79e2001-10-12 19:01:43 +00001595 Return the first position of \var{substr} in
1596 \var{str}[\var{start}:\var{end}] using the given \var{direction}
1597 (\var{direction} == 1 means to do a forward search,
Fred Drake1d1e1db2002-06-20 22:07:04 +00001598 \var{direction} == -1 a backward search). The return value is the
1599 index of the first match; a value of \code{-1} indicates that no
1600 match was found, and \code{-2} indicates that an error occurred and
1601 an exception has been set.
Fred Drake3adf79e2001-10-12 19:01:43 +00001602\end{cfuncdesc}
1603
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001604\begin{cfuncdesc}{Py_ssize_t}{PyUnicode_Count}{PyObject *str,
Fred Drake1d1e1db2002-06-20 22:07:04 +00001605 PyObject *substr,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001606 Py_ssize_t start,
1607 Py_ssize_t end}
Fred Drake1d1e1db2002-06-20 22:07:04 +00001608 Return the number of non-overlapping occurrences of \var{substr} in
Georg Brandl99363b62005-09-03 07:27:26 +00001609 \code{\var{str}[\var{start}:\var{end}]}. Return \code{-1} if an
Fred Drake1d1e1db2002-06-20 22:07:04 +00001610 error occurred.
Fred Drake3adf79e2001-10-12 19:01:43 +00001611\end{cfuncdesc}
1612
1613\begin{cfuncdesc}{PyObject*}{PyUnicode_Replace}{PyObject *str,
1614 PyObject *substr,
1615 PyObject *replstr,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001616 Py_ssize_t maxcount}
Fred Drake3adf79e2001-10-12 19:01:43 +00001617 Replace at most \var{maxcount} occurrences of \var{substr} in
1618 \var{str} with \var{replstr} and return the resulting Unicode object.
1619 \var{maxcount} == -1 means replace all occurrences.
1620\end{cfuncdesc}
1621
1622\begin{cfuncdesc}{int}{PyUnicode_Compare}{PyObject *left, PyObject *right}
1623 Compare two strings and return -1, 0, 1 for less than, equal, and
1624 greater than, respectively.
1625\end{cfuncdesc}
1626
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001627\begin{cfuncdesc}{int}{PyUnicode_RichCompare}{PyObject *left,
1628 PyObject *right,
1629 int op}
1630
1631 Rich compare two unicode strings and return one of the following:
1632 \begin{itemize}
1633 \item \code{NULL} in case an exception was raised
1634 \item \constant{Py_True} or \constant{Py_False} for successful comparisons
1635 \item \constant{Py_NotImplemented} in case the type combination is unknown
1636 \end{itemize}
1637
1638 Note that \constant{Py_EQ} and \constant{Py_NE} comparisons can cause a
1639 \exception{UnicodeWarning} in case the conversion of the arguments to
1640 Unicode fails with a \exception{UnicodeDecodeError}.
1641
1642 Possible values for \var{op} are
1643 \constant{Py_GT}, \constant{Py_GE}, \constant{Py_EQ},
1644 \constant{Py_NE}, \constant{Py_LT}, and \constant{Py_LE}.
1645\end{cfuncdesc}
1646
Fred Drake3adf79e2001-10-12 19:01:43 +00001647\begin{cfuncdesc}{PyObject*}{PyUnicode_Format}{PyObject *format,
1648 PyObject *args}
Georg Brandl99363b62005-09-03 07:27:26 +00001649 Return a new string object from \var{format} and \var{args}; this
Fred Drake3adf79e2001-10-12 19:01:43 +00001650 is analogous to \code{\var{format} \%\ \var{args}}. The
1651 \var{args} argument must be a tuple.
1652\end{cfuncdesc}
1653
1654\begin{cfuncdesc}{int}{PyUnicode_Contains}{PyObject *container,
1655 PyObject *element}
Georg Brandl99363b62005-09-03 07:27:26 +00001656 Check whether \var{element} is contained in \var{container} and
1657 return true or false accordingly.
Fred Drake3adf79e2001-10-12 19:01:43 +00001658
1659 \var{element} has to coerce to a one element Unicode
1660 string. \code{-1} is returned if there was an error.
1661\end{cfuncdesc}
1662
Walter Dörwalde65c86c2007-05-25 14:14:31 +00001663\begin{cfuncdesc}{void}{PyUnicode_InternInPlace}{PyObject **string}
1664 Intern the argument \var{*string} in place. The argument must be
1665 the address of a pointer variable pointing to a Python unicode string
1666 object. If there is an existing interned string that is the same as
1667 \var{*string}, it sets \var{*string} to it (decrementing the
1668 reference count of the old string object and incrementing the
1669 reference count of the interned string object), otherwise it leaves
1670 \var{*string} alone and interns it (incrementing its reference
1671 count). (Clarification: even though there is a lot of talk about
1672 reference counts, think of this function as reference-count-neutral;
1673 you own the object after the call if and only if you owned it before
1674 the call.)
1675\end{cfuncdesc}
1676
1677\begin{cfuncdesc}{PyObject*}{PyUnicode_InternFromString}{const char *v}
1678 A combination of \cfunction{PyUnicode_FromString()} and
1679 \cfunction{PyUnicode_InternInPlace()}, returning either a new unicode
1680 string object that has been interned, or a new (``owned'') reference to
1681 an earlier interned string object with the same value.
1682\end{cfuncdesc}
1683
Fred Drake3adf79e2001-10-12 19:01:43 +00001684
1685\subsection{Buffer Objects \label{bufferObjects}}
1686\sectionauthor{Greg Stein}{gstein@lyra.org}
1687
1688\obindex{buffer}
1689Python objects implemented in C can export a group of functions called
1690the ``buffer\index{buffer interface} interface.'' These functions can
1691be used by an object to expose its data in a raw, byte-oriented
1692format. Clients of the object can use the buffer interface to access
1693the object data directly, without needing to copy it first.
1694
Tim Petersf582b822001-12-11 18:51:08 +00001695Two examples of objects that support
1696the buffer interface are strings and arrays. The string object exposes
Fred Drake3adf79e2001-10-12 19:01:43 +00001697the character contents in the buffer interface's byte-oriented
1698form. An array can also expose its contents, but it should be noted
1699that array elements may be multi-byte values.
1700
1701An example user of the buffer interface is the file object's
1702\method{write()} method. Any object that can export a series of bytes
1703through the buffer interface can be written to a file. There are a
Tim Petersf582b822001-12-11 18:51:08 +00001704number of format codes to \cfunction{PyArg_ParseTuple()} that operate
Fred Drake3adf79e2001-10-12 19:01:43 +00001705against an object's buffer interface, returning data from the target
1706object.
1707
1708More information on the buffer interface is provided in the section
Fred Drake54e62942001-12-11 19:40:16 +00001709``Buffer Object Structures'' (section~\ref{buffer-structs}), under
Fred Drake3adf79e2001-10-12 19:01:43 +00001710the description for \ctype{PyBufferProcs}\ttindex{PyBufferProcs}.
1711
1712A ``buffer object'' is defined in the \file{bufferobject.h} header
1713(included by \file{Python.h}). These objects look very similar to
1714string objects at the Python programming level: they support slicing,
1715indexing, concatenation, and some other standard string
1716operations. However, their data can come from one of two sources: from
1717a block of memory, or from another object which exports the buffer
1718interface.
1719
1720Buffer objects are useful as a way to expose the data from another
1721object's buffer interface to the Python programmer. They can also be
1722used as a zero-copy slicing mechanism. Using their ability to
1723reference a block of memory, it is possible to expose any data to the
1724Python programmer quite easily. The memory could be a large, constant
1725array in a C extension, it could be a raw block of memory for
1726manipulation before passing to an operating system library, or it
1727could be used to pass around structured data in its native, in-memory
1728format.
1729
1730\begin{ctypedesc}{PyBufferObject}
1731 This subtype of \ctype{PyObject} represents a buffer object.
1732\end{ctypedesc}
1733
1734\begin{cvardesc}{PyTypeObject}{PyBuffer_Type}
1735 The instance of \ctype{PyTypeObject} which represents the Python
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001736 buffer type; it is the same object as \code{buffer} and
1737 \code{types.BufferType} in the Python layer.
1738 \withsubitem{(in module types)}{\ttindex{BufferType}}.
Fred Drake3adf79e2001-10-12 19:01:43 +00001739\end{cvardesc}
1740
1741\begin{cvardesc}{int}{Py_END_OF_BUFFER}
1742 This constant may be passed as the \var{size} parameter to
1743 \cfunction{PyBuffer_FromObject()} or
1744 \cfunction{PyBuffer_FromReadWriteObject()}. It indicates that the
1745 new \ctype{PyBufferObject} should refer to \var{base} object from
1746 the specified \var{offset} to the end of its exported buffer. Using
1747 this enables the caller to avoid querying the \var{base} object for
1748 its length.
1749\end{cvardesc}
1750
1751\begin{cfuncdesc}{int}{PyBuffer_Check}{PyObject *p}
1752 Return true if the argument has type \cdata{PyBuffer_Type}.
1753\end{cfuncdesc}
1754
1755\begin{cfuncdesc}{PyObject*}{PyBuffer_FromObject}{PyObject *base,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001756 Py_ssize_t offset, Py_ssize_t size}
Fred Drake3adf79e2001-10-12 19:01:43 +00001757 Return a new read-only buffer object. This raises
1758 \exception{TypeError} if \var{base} doesn't support the read-only
1759 buffer protocol or doesn't provide exactly one buffer segment, or it
1760 raises \exception{ValueError} if \var{offset} is less than zero. The
1761 buffer will hold a reference to the \var{base} object, and the
1762 buffer's contents will refer to the \var{base} object's buffer
1763 interface, starting as position \var{offset} and extending for
1764 \var{size} bytes. If \var{size} is \constant{Py_END_OF_BUFFER}, then
1765 the new buffer's contents extend to the length of the \var{base}
1766 object's exported buffer data.
1767\end{cfuncdesc}
1768
1769\begin{cfuncdesc}{PyObject*}{PyBuffer_FromReadWriteObject}{PyObject *base,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001770 Py_ssize_t offset,
1771 Py_ssize_t size}
Fred Drake3adf79e2001-10-12 19:01:43 +00001772 Return a new writable buffer object. Parameters and exceptions are
1773 similar to those for \cfunction{PyBuffer_FromObject()}. If the
1774 \var{base} object does not export the writeable buffer protocol,
1775 then \exception{TypeError} is raised.
1776\end{cfuncdesc}
1777
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001778\begin{cfuncdesc}{PyObject*}{PyBuffer_FromMemory}{void *ptr, Py_ssize_t size}
Fred Drake3adf79e2001-10-12 19:01:43 +00001779 Return a new read-only buffer object that reads from a specified
1780 location in memory, with a specified size. The caller is
1781 responsible for ensuring that the memory buffer, passed in as
1782 \var{ptr}, is not deallocated while the returned buffer object
1783 exists. Raises \exception{ValueError} if \var{size} is less than
1784 zero. Note that \constant{Py_END_OF_BUFFER} may \emph{not} be
1785 passed for the \var{size} parameter; \exception{ValueError} will be
1786 raised in that case.
1787\end{cfuncdesc}
1788
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001789\begin{cfuncdesc}{PyObject*}{PyBuffer_FromReadWriteMemory}{void *ptr, Py_ssize_t size}
Fred Drake3adf79e2001-10-12 19:01:43 +00001790 Similar to \cfunction{PyBuffer_FromMemory()}, but the returned
1791 buffer is writable.
1792\end{cfuncdesc}
1793
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001794\begin{cfuncdesc}{PyObject*}{PyBuffer_New}{Py_ssize_t size}
Georg Brandl99363b62005-09-03 07:27:26 +00001795 Return a new writable buffer object that maintains its own memory
Fred Drake3adf79e2001-10-12 19:01:43 +00001796 buffer of \var{size} bytes. \exception{ValueError} is returned if
Neil Schemenauerd68d3ee2004-06-08 02:58:50 +00001797 \var{size} is not zero or positive. Note that the memory buffer (as
1798 returned by \cfunction{PyObject_AsWriteBuffer()}) is not specifically
1799 aligned.
Fred Drake3adf79e2001-10-12 19:01:43 +00001800\end{cfuncdesc}
1801
1802
1803\subsection{Tuple Objects \label{tupleObjects}}
1804
1805\obindex{tuple}
1806\begin{ctypedesc}{PyTupleObject}
1807 This subtype of \ctype{PyObject} represents a Python tuple object.
1808\end{ctypedesc}
1809
1810\begin{cvardesc}{PyTypeObject}{PyTuple_Type}
1811 This instance of \ctype{PyTypeObject} represents the Python tuple
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001812 type; it is the same object as \code{tuple} and \code{types.TupleType}
1813 in the Python layer.\withsubitem{(in module types)}{\ttindex{TupleType}}.
Fred Drake3adf79e2001-10-12 19:01:43 +00001814\end{cvardesc}
1815
1816\begin{cfuncdesc}{int}{PyTuple_Check}{PyObject *p}
1817 Return true if \var{p} is a tuple object or an instance of a subtype
1818 of the tuple type.
1819 \versionchanged[Allowed subtypes to be accepted]{2.2}
1820\end{cfuncdesc}
1821
1822\begin{cfuncdesc}{int}{PyTuple_CheckExact}{PyObject *p}
1823 Return true if \var{p} is a tuple object, but not an instance of a
1824 subtype of the tuple type.
1825 \versionadded{2.2}
1826\end{cfuncdesc}
1827
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001828\begin{cfuncdesc}{PyObject*}{PyTuple_New}{Py_ssize_t len}
Fred Drake3adf79e2001-10-12 19:01:43 +00001829 Return a new tuple object of size \var{len}, or \NULL{} on failure.
1830\end{cfuncdesc}
1831
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001832\begin{cfuncdesc}{PyObject*}{PyTuple_Pack}{Py_ssize_t n, \moreargs}
Raymond Hettingercb2da432003-10-12 18:24:34 +00001833 Return a new tuple object of size \var{n}, or \NULL{} on failure.
1834 The tuple values are initialized to the subsequent \var{n} C arguments
1835 pointing to Python objects. \samp{PyTuple_Pack(2, \var{a}, \var{b})}
1836 is equivalent to \samp{Py_BuildValue("(OO)", \var{a}, \var{b})}.
Tim Peters9ddf40b2004-06-20 22:41:32 +00001837 \versionadded{2.4}
Raymond Hettingercb2da432003-10-12 18:24:34 +00001838\end{cfuncdesc}
1839
Fred Drake3adf79e2001-10-12 19:01:43 +00001840\begin{cfuncdesc}{int}{PyTuple_Size}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00001841 Take a pointer to a tuple object, and return the size of that
Fred Drake3adf79e2001-10-12 19:01:43 +00001842 tuple.
1843\end{cfuncdesc}
1844
1845\begin{cfuncdesc}{int}{PyTuple_GET_SIZE}{PyObject *p}
1846 Return the size of the tuple \var{p}, which must be non-\NULL{} and
1847 point to a tuple; no error checking is performed.
1848\end{cfuncdesc}
1849
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001850\begin{cfuncdesc}{PyObject*}{PyTuple_GetItem}{PyObject *p, Py_ssize_t pos}
Georg Brandl99363b62005-09-03 07:27:26 +00001851 Return the object at position \var{pos} in the tuple pointed to by
1852 \var{p}. If \var{pos} is out of bounds, return \NULL{} and sets an
Fred Drake3adf79e2001-10-12 19:01:43 +00001853 \exception{IndexError} exception.
1854\end{cfuncdesc}
1855
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001856\begin{cfuncdesc}{PyObject*}{PyTuple_GET_ITEM}{PyObject *p, Py_ssize_t pos}
Fred Drake3adf79e2001-10-12 19:01:43 +00001857 Like \cfunction{PyTuple_GetItem()}, but does no checking of its
1858 arguments.
1859\end{cfuncdesc}
1860
1861\begin{cfuncdesc}{PyObject*}{PyTuple_GetSlice}{PyObject *p,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001862 Py_ssize_t low, Py_ssize_t high}
Georg Brandl99363b62005-09-03 07:27:26 +00001863 Take a slice of the tuple pointed to by \var{p} from \var{low} to
1864 \var{high} and return it as a new tuple.
Fred Drake3adf79e2001-10-12 19:01:43 +00001865\end{cfuncdesc}
1866
1867\begin{cfuncdesc}{int}{PyTuple_SetItem}{PyObject *p,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001868 Py_ssize_t pos, PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +00001869 Insert a reference to object \var{o} at position \var{pos} of the
1870 tuple pointed to by \var{p}. Return \code{0} on success.
Fred Drake3adf79e2001-10-12 19:01:43 +00001871 \note{This function ``steals'' a reference to \var{o}.}
1872\end{cfuncdesc}
1873
1874\begin{cfuncdesc}{void}{PyTuple_SET_ITEM}{PyObject *p,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001875 Py_ssize_t pos, PyObject *o}
Fred Drake3adf79e2001-10-12 19:01:43 +00001876 Like \cfunction{PyTuple_SetItem()}, but does no error checking, and
1877 should \emph{only} be used to fill in brand new tuples. \note{This
1878 function ``steals'' a reference to \var{o}.}
1879\end{cfuncdesc}
1880
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001881\begin{cfuncdesc}{int}{_PyTuple_Resize}{PyObject **p, Py_ssize_t newsize}
Fred Drake3adf79e2001-10-12 19:01:43 +00001882 Can be used to resize a tuple. \var{newsize} will be the new length
1883 of the tuple. Because tuples are \emph{supposed} to be immutable,
1884 this should only be used if there is only one reference to the
1885 object. Do \emph{not} use this if the tuple may already be known to
1886 some other part of the code. The tuple will always grow or shrink
1887 at the end. Think of this as destroying the old tuple and creating
1888 a new one, only more efficiently. Returns \code{0} on success.
1889 Client code should never assume that the resulting value of
1890 \code{*\var{p}} will be the same as before calling this function.
1891 If the object referenced by \code{*\var{p}} is replaced, the
1892 original \code{*\var{p}} is destroyed. On failure, returns
Tim Peters9ddf40b2004-06-20 22:41:32 +00001893 \code{-1} and sets \code{*\var{p}} to \NULL{}, and raises
Fred Drake3adf79e2001-10-12 19:01:43 +00001894 \exception{MemoryError} or
1895 \exception{SystemError}.
Tim Petersf582b822001-12-11 18:51:08 +00001896 \versionchanged[Removed unused third parameter, \var{last_is_sticky}]{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +00001897\end{cfuncdesc}
1898
1899
1900\subsection{List Objects \label{listObjects}}
1901
1902\obindex{list}
1903\begin{ctypedesc}{PyListObject}
1904 This subtype of \ctype{PyObject} represents a Python list object.
1905\end{ctypedesc}
1906
1907\begin{cvardesc}{PyTypeObject}{PyList_Type}
1908 This instance of \ctype{PyTypeObject} represents the Python list
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001909 type. This is the same object as \code{list} and \code{types.ListType}
1910 in the Python layer.\withsubitem{(in module types)}{\ttindex{ListType}}
Fred Drake3adf79e2001-10-12 19:01:43 +00001911\end{cvardesc}
1912
1913\begin{cfuncdesc}{int}{PyList_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00001914 Return true if \var{p} is a list object or an instance of a
Andrew MacIntyre13cd8892003-12-25 23:57:52 +00001915 subtype of the list type.
1916 \versionchanged[Allowed subtypes to be accepted]{2.2}
1917\end{cfuncdesc}
1918
1919\begin{cfuncdesc}{int}{PyList_CheckExact}{PyObject *p}
1920 Return true if \var{p} is a list object, but not an instance of a
1921 subtype of the list type.
1922 \versionadded{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +00001923\end{cfuncdesc}
1924
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001925\begin{cfuncdesc}{PyObject*}{PyList_New}{Py_ssize_t len}
Georg Brandl99363b62005-09-03 07:27:26 +00001926 Return a new list of length \var{len} on success, or \NULL{} on
Fred Drake3adf79e2001-10-12 19:01:43 +00001927 failure.
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001928 \note{If \var{length} is greater than zero, the returned list object's
1929 items are set to \code{NULL}. Thus you cannot use abstract
1930 API functions such as \cfunction{PySequence_SetItem()}
1931 or expose the object to Python code before setting all items to a
1932 real object with \cfunction{PyList_SetItem()}.}
Fred Drake3adf79e2001-10-12 19:01:43 +00001933\end{cfuncdesc}
1934
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001935\begin{cfuncdesc}{Py_ssize_t}{PyList_Size}{PyObject *list}
Georg Brandl99363b62005-09-03 07:27:26 +00001936 Return the length of the list object in \var{list}; this is
Fred Drake3adf79e2001-10-12 19:01:43 +00001937 equivalent to \samp{len(\var{list})} on a list object.
1938 \bifuncindex{len}
1939\end{cfuncdesc}
1940
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001941\begin{cfuncdesc}{Py_ssize_t}{PyList_GET_SIZE}{PyObject *list}
Fred Drake3adf79e2001-10-12 19:01:43 +00001942 Macro form of \cfunction{PyList_Size()} without error checking.
1943\end{cfuncdesc}
1944
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001945\begin{cfuncdesc}{PyObject*}{PyList_GetItem}{PyObject *list, Py_ssize_t index}
Georg Brandl99363b62005-09-03 07:27:26 +00001946 Return the object at position \var{pos} in the list pointed to by
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001947 \var{p}. The position must be positive, indexing from the end of the
1948 list is not supported. If \var{pos} is out of bounds, return \NULL{}
1949 and set an \exception{IndexError} exception.
Fred Drake3adf79e2001-10-12 19:01:43 +00001950\end{cfuncdesc}
1951
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001952\begin{cfuncdesc}{PyObject*}{PyList_GET_ITEM}{PyObject *list, Py_ssize_t i}
Fred Drake3adf79e2001-10-12 19:01:43 +00001953 Macro form of \cfunction{PyList_GetItem()} without error checking.
1954\end{cfuncdesc}
1955
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001956\begin{cfuncdesc}{int}{PyList_SetItem}{PyObject *list, Py_ssize_t index,
Fred Drake3adf79e2001-10-12 19:01:43 +00001957 PyObject *item}
Georg Brandl99363b62005-09-03 07:27:26 +00001958 Set the item at index \var{index} in list to \var{item}. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00001959 \code{0} on success or \code{-1} on failure. \note{This function
1960 ``steals'' a reference to \var{item} and discards a reference to an
1961 item already in the list at the affected position.}
1962\end{cfuncdesc}
1963
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001964\begin{cfuncdesc}{void}{PyList_SET_ITEM}{PyObject *list, Py_ssize_t i,
Fred Drake3adf79e2001-10-12 19:01:43 +00001965 PyObject *o}
1966 Macro form of \cfunction{PyList_SetItem()} without error checking.
1967 This is normally only used to fill in new lists where there is no
1968 previous content.
1969 \note{This function ``steals'' a reference to \var{item}, and,
1970 unlike \cfunction{PyList_SetItem()}, does \emph{not} discard a
1971 reference to any item that it being replaced; any reference in
1972 \var{list} at position \var{i} will be leaked.}
1973\end{cfuncdesc}
1974
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001975\begin{cfuncdesc}{int}{PyList_Insert}{PyObject *list, Py_ssize_t index,
Fred Drake3adf79e2001-10-12 19:01:43 +00001976 PyObject *item}
Georg Brandl99363b62005-09-03 07:27:26 +00001977 Insert the item \var{item} into list \var{list} in front of index
1978 \var{index}. Return \code{0} if successful; return \code{-1} and
1979 set an exception if unsuccessful. Analogous to
Fred Drake3adf79e2001-10-12 19:01:43 +00001980 \code{\var{list}.insert(\var{index}, \var{item})}.
1981\end{cfuncdesc}
1982
1983\begin{cfuncdesc}{int}{PyList_Append}{PyObject *list, PyObject *item}
Georg Brandl99363b62005-09-03 07:27:26 +00001984 Append the object \var{item} at the end of list \var{list}.
1985 Return \code{0} if successful; return \code{-1} and set an
Fred Drake3adf79e2001-10-12 19:01:43 +00001986 exception if unsuccessful. Analogous to
1987 \code{\var{list}.append(\var{item})}.
1988\end{cfuncdesc}
1989
1990\begin{cfuncdesc}{PyObject*}{PyList_GetSlice}{PyObject *list,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001991 Py_ssize_t low, Py_ssize_t high}
Georg Brandl99363b62005-09-03 07:27:26 +00001992 Return a list of the objects in \var{list} containing the objects
1993 \emph{between} \var{low} and \var{high}. Return \NULL{} and set
Fred Drake3adf79e2001-10-12 19:01:43 +00001994 an exception if unsuccessful.
1995 Analogous to \code{\var{list}[\var{low}:\var{high}]}.
1996\end{cfuncdesc}
1997
1998\begin{cfuncdesc}{int}{PyList_SetSlice}{PyObject *list,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001999 Py_ssize_t low, Py_ssize_t high,
Fred Drake3adf79e2001-10-12 19:01:43 +00002000 PyObject *itemlist}
Georg Brandl99363b62005-09-03 07:27:26 +00002001 Set the slice of \var{list} between \var{low} and \var{high} to the
Fred Drake3adf79e2001-10-12 19:01:43 +00002002 contents of \var{itemlist}. Analogous to
Raymond Hettinger9c7ed4c2003-10-26 17:20:07 +00002003 \code{\var{list}[\var{low}:\var{high}] = \var{itemlist}}.
2004 The \var{itemlist} may be \NULL{}, indicating the assignment
2005 of an empty list (slice deletion).
Georg Brandl99363b62005-09-03 07:27:26 +00002006 Return \code{0} on success, \code{-1} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00002007\end{cfuncdesc}
2008
2009\begin{cfuncdesc}{int}{PyList_Sort}{PyObject *list}
Georg Brandl99363b62005-09-03 07:27:26 +00002010 Sort the items of \var{list} in place. Return \code{0} on
Fred Drake3adf79e2001-10-12 19:01:43 +00002011 success, \code{-1} on failure. This is equivalent to
2012 \samp{\var{list}.sort()}.
2013\end{cfuncdesc}
2014
2015\begin{cfuncdesc}{int}{PyList_Reverse}{PyObject *list}
Georg Brandl99363b62005-09-03 07:27:26 +00002016 Reverse the items of \var{list} in place. Return \code{0} on
Fred Drake3adf79e2001-10-12 19:01:43 +00002017 success, \code{-1} on failure. This is the equivalent of
2018 \samp{\var{list}.reverse()}.
2019\end{cfuncdesc}
2020
2021\begin{cfuncdesc}{PyObject*}{PyList_AsTuple}{PyObject *list}
Georg Brandl99363b62005-09-03 07:27:26 +00002022 Return a new tuple object containing the contents of \var{list};
Fred Drake3adf79e2001-10-12 19:01:43 +00002023 equivalent to \samp{tuple(\var{list})}.\bifuncindex{tuple}
2024\end{cfuncdesc}
2025
2026
2027\section{Mapping Objects \label{mapObjects}}
2028
2029\obindex{mapping}
2030
2031
2032\subsection{Dictionary Objects \label{dictObjects}}
2033
2034\obindex{dictionary}
2035\begin{ctypedesc}{PyDictObject}
2036 This subtype of \ctype{PyObject} represents a Python dictionary
2037 object.
2038\end{ctypedesc}
2039
2040\begin{cvardesc}{PyTypeObject}{PyDict_Type}
2041 This instance of \ctype{PyTypeObject} represents the Python
2042 dictionary type. This is exposed to Python programs as
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002043 \code{dict} and \code{types.DictType}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002044 \withsubitem{(in module types)}{\ttindex{DictType}\ttindex{DictionaryType}}
2045\end{cvardesc}
2046
2047\begin{cfuncdesc}{int}{PyDict_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002048 Return true if \var{p} is a dict object or an instance of a
Andrew MacIntyre13cd8892003-12-25 23:57:52 +00002049 subtype of the dict type.
2050 \versionchanged[Allowed subtypes to be accepted]{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +00002051\end{cfuncdesc}
2052
Andrew MacIntyref72af652003-12-26 00:07:51 +00002053\begin{cfuncdesc}{int}{PyDict_CheckExact}{PyObject *p}
2054 Return true if \var{p} is a dict object, but not an instance of a
2055 subtype of the dict type.
2056 \versionadded{2.4}
2057\end{cfuncdesc}
2058
Fred Drake3adf79e2001-10-12 19:01:43 +00002059\begin{cfuncdesc}{PyObject*}{PyDict_New}{}
Georg Brandl99363b62005-09-03 07:27:26 +00002060 Return a new empty dictionary, or \NULL{} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00002061\end{cfuncdesc}
2062
2063\begin{cfuncdesc}{PyObject*}{PyDictProxy_New}{PyObject *dict}
2064 Return a proxy object for a mapping which enforces read-only
2065 behavior. This is normally used to create a proxy to prevent
2066 modification of the dictionary for non-dynamic class types.
2067 \versionadded{2.2}
2068\end{cfuncdesc}
2069
2070\begin{cfuncdesc}{void}{PyDict_Clear}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002071 Empty an existing dictionary of all key-value pairs.
Fred Drake3adf79e2001-10-12 19:01:43 +00002072\end{cfuncdesc}
2073
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00002074\begin{cfuncdesc}{int}{PyDict_Contains}{PyObject *p, PyObject *key}
2075 Determine if dictionary \var{p} contains \var{key}. If an item
2076 in \var{p} is matches \var{key}, return \code{1}, otherwise return
2077 \code{0}. On error, return \code{-1}. This is equivalent to the
2078 Python expression \samp{\var{key} in \var{p}}.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002079 \versionadded{2.4}
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00002080\end{cfuncdesc}
2081
Fred Drake3adf79e2001-10-12 19:01:43 +00002082\begin{cfuncdesc}{PyObject*}{PyDict_Copy}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002083 Return a new dictionary that contains the same key-value pairs as
Fred Drake3adf79e2001-10-12 19:01:43 +00002084 \var{p}.
2085 \versionadded{1.6}
2086\end{cfuncdesc}
2087
2088\begin{cfuncdesc}{int}{PyDict_SetItem}{PyObject *p, PyObject *key,
2089 PyObject *val}
Georg Brandl99363b62005-09-03 07:27:26 +00002090 Insert \var{value} into the dictionary \var{p} with a key of
Fred Drake3adf79e2001-10-12 19:01:43 +00002091 \var{key}. \var{key} must be hashable; if it isn't,
2092 \exception{TypeError} will be raised.
Georg Brandl99363b62005-09-03 07:27:26 +00002093 Return \code{0} on success or \code{-1} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00002094\end{cfuncdesc}
2095
2096\begin{cfuncdesc}{int}{PyDict_SetItemString}{PyObject *p,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002097 const char *key,
Fred Drake3adf79e2001-10-12 19:01:43 +00002098 PyObject *val}
Georg Brandl99363b62005-09-03 07:27:26 +00002099 Insert \var{value} into the dictionary \var{p} using \var{key} as a
Fred Drake3adf79e2001-10-12 19:01:43 +00002100 key. \var{key} should be a \ctype{char*}. The key object is created
Georg Brandl99363b62005-09-03 07:27:26 +00002101 using \code{PyString_FromString(\var{key})}. Return \code{0} on
Fred Drake3adf79e2001-10-12 19:01:43 +00002102 success or \code{-1} on failure.
2103 \ttindex{PyString_FromString()}
2104\end{cfuncdesc}
2105
2106\begin{cfuncdesc}{int}{PyDict_DelItem}{PyObject *p, PyObject *key}
Georg Brandl99363b62005-09-03 07:27:26 +00002107 Remove the entry in dictionary \var{p} with key \var{key}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002108 \var{key} must be hashable; if it isn't, \exception{TypeError} is
Georg Brandl99363b62005-09-03 07:27:26 +00002109 raised. Return \code{0} on success or \code{-1} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00002110\end{cfuncdesc}
2111
2112\begin{cfuncdesc}{int}{PyDict_DelItemString}{PyObject *p, char *key}
Georg Brandl99363b62005-09-03 07:27:26 +00002113 Remove the entry in dictionary \var{p} which has a key specified by
2114 the string \var{key}. Return \code{0} on success or \code{-1} on
Fred Drake3adf79e2001-10-12 19:01:43 +00002115 failure.
2116\end{cfuncdesc}
2117
2118\begin{cfuncdesc}{PyObject*}{PyDict_GetItem}{PyObject *p, PyObject *key}
Georg Brandl99363b62005-09-03 07:27:26 +00002119 Return the object from dictionary \var{p} which has a key
2120 \var{key}. Return \NULL{} if the key \var{key} is not present, but
Fred Drake3adf79e2001-10-12 19:01:43 +00002121 \emph{without} setting an exception.
2122\end{cfuncdesc}
2123
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002124\begin{cfuncdesc}{PyObject*}{PyDict_GetItemString}{PyObject *p, const char *key}
Fred Drake3adf79e2001-10-12 19:01:43 +00002125 This is the same as \cfunction{PyDict_GetItem()}, but \var{key} is
2126 specified as a \ctype{char*}, rather than a \ctype{PyObject*}.
2127\end{cfuncdesc}
2128
2129\begin{cfuncdesc}{PyObject*}{PyDict_Items}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002130 Return a \ctype{PyListObject} containing all the items from the
Nicholas Bastin975e7252004-09-29 21:39:26 +00002131 dictionary, as in the dictionary method \method{items()} (see the
Fred Drake3adf79e2001-10-12 19:01:43 +00002132 \citetitle[../lib/lib.html]{Python Library Reference}).
2133\end{cfuncdesc}
2134
2135\begin{cfuncdesc}{PyObject*}{PyDict_Keys}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002136 Return a \ctype{PyListObject} containing all the keys from the
Fred Drake3adf79e2001-10-12 19:01:43 +00002137 dictionary, as in the dictionary method \method{keys()} (see the
2138 \citetitle[../lib/lib.html]{Python Library Reference}).
2139\end{cfuncdesc}
2140
2141\begin{cfuncdesc}{PyObject*}{PyDict_Values}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002142 Return a \ctype{PyListObject} containing all the values from the
Fred Drake3adf79e2001-10-12 19:01:43 +00002143 dictionary \var{p}, as in the dictionary method \method{values()}
2144 (see the \citetitle[../lib/lib.html]{Python Library Reference}).
2145\end{cfuncdesc}
2146
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002147\begin{cfuncdesc}{Py_ssize_t}{PyDict_Size}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002148 Return the number of items in the dictionary. This is equivalent
Fred Drake3adf79e2001-10-12 19:01:43 +00002149 to \samp{len(\var{p})} on a dictionary.\bifuncindex{len}
2150\end{cfuncdesc}
2151
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002152\begin{cfuncdesc}{int}{PyDict_Next}{PyObject *p, Py_ssize_t *ppos,
Fred Drake3adf79e2001-10-12 19:01:43 +00002153 PyObject **pkey, PyObject **pvalue}
2154 Iterate over all key-value pairs in the dictionary \var{p}. The
2155 \ctype{int} referred to by \var{ppos} must be initialized to
2156 \code{0} prior to the first call to this function to start the
2157 iteration; the function returns true for each pair in the
2158 dictionary, and false once all pairs have been reported. The
2159 parameters \var{pkey} and \var{pvalue} should either point to
2160 \ctype{PyObject*} variables that will be filled in with each key and
Tim Peters9ddf40b2004-06-20 22:41:32 +00002161 value, respectively, or may be \NULL{}. Any references returned through
Raymond Hettinger54693242003-12-13 19:48:41 +00002162 them are borrowed. \var{ppos} should not be altered during iteration.
2163 Its value represents offsets within the internal dictionary structure,
2164 and since the structure is sparse, the offsets are not consecutive.
Fred Drake3adf79e2001-10-12 19:01:43 +00002165
2166 For example:
2167
2168\begin{verbatim}
2169PyObject *key, *value;
Thomas Woutersb2137042007-02-01 18:02:27 +00002170Py_ssize_t pos = 0;
Fred Drake3adf79e2001-10-12 19:01:43 +00002171
2172while (PyDict_Next(self->dict, &pos, &key, &value)) {
2173 /* do something interesting with the values... */
2174 ...
2175}
2176\end{verbatim}
2177
2178 The dictionary \var{p} should not be mutated during iteration. It
2179 is safe (since Python 2.1) to modify the values of the keys as you
2180 iterate over the dictionary, but only so long as the set of keys
2181 does not change. For example:
2182
2183\begin{verbatim}
2184PyObject *key, *value;
Thomas Woutersb2137042007-02-01 18:02:27 +00002185Py_ssize_t pos = 0;
Fred Drake3adf79e2001-10-12 19:01:43 +00002186
2187while (PyDict_Next(self->dict, &pos, &key, &value)) {
2188 int i = PyInt_AS_LONG(value) + 1;
2189 PyObject *o = PyInt_FromLong(i);
2190 if (o == NULL)
2191 return -1;
2192 if (PyDict_SetItem(self->dict, key, o) < 0) {
2193 Py_DECREF(o);
2194 return -1;
2195 }
2196 Py_DECREF(o);
2197}
2198\end{verbatim}
2199\end{cfuncdesc}
2200
2201\begin{cfuncdesc}{int}{PyDict_Merge}{PyObject *a, PyObject *b, int override}
Tim Petersf582b822001-12-11 18:51:08 +00002202 Iterate over mapping object \var{b} adding key-value pairs to dictionary
2203 \var{a}.
2204 \var{b} may be a dictionary, or any object supporting
2205 \function{PyMapping_Keys()} and \function{PyObject_GetItem()}.
2206 If \var{override} is true, existing pairs in \var{a} will
Fred Drake3adf79e2001-10-12 19:01:43 +00002207 be replaced if a matching key is found in \var{b}, otherwise pairs
2208 will only be added if there is not a matching key in \var{a}.
Tim Petersf582b822001-12-11 18:51:08 +00002209 Return \code{0} on success or \code{-1} if an exception was
Fred Drake3adf79e2001-10-12 19:01:43 +00002210 raised.
2211\versionadded{2.2}
2212\end{cfuncdesc}
2213
2214\begin{cfuncdesc}{int}{PyDict_Update}{PyObject *a, PyObject *b}
2215 This is the same as \code{PyDict_Merge(\var{a}, \var{b}, 1)} in C,
Tim Petersf582b822001-12-11 18:51:08 +00002216 or \code{\var{a}.update(\var{b})} in Python. Return \code{0} on
Fred Drake3adf79e2001-10-12 19:01:43 +00002217 success or \code{-1} if an exception was raised.
2218 \versionadded{2.2}
2219\end{cfuncdesc}
2220
Tim Petersf582b822001-12-11 18:51:08 +00002221\begin{cfuncdesc}{int}{PyDict_MergeFromSeq2}{PyObject *a, PyObject *seq2,
2222 int override}
2223 Update or merge into dictionary \var{a}, from the key-value pairs in
2224 \var{seq2}. \var{seq2} must be an iterable object producing
2225 iterable objects of length 2, viewed as key-value pairs. In case of
2226 duplicate keys, the last wins if \var{override} is true, else the
2227 first wins.
2228 Return \code{0} on success or \code{-1} if an exception
2229 was raised.
2230 Equivalent Python (except for the return value):
2231
2232\begin{verbatim}
2233def PyDict_MergeFromSeq2(a, seq2, override):
2234 for key, value in seq2:
2235 if override or key not in a:
2236 a[key] = value
2237\end{verbatim}
2238
2239 \versionadded{2.2}
2240\end{cfuncdesc}
Fred Drake3adf79e2001-10-12 19:01:43 +00002241
Fred Drake54e62942001-12-11 19:40:16 +00002242
Fred Drake3adf79e2001-10-12 19:01:43 +00002243\section{Other Objects \label{otherObjects}}
2244
Guido van Rossumd8faa362007-04-27 19:54:29 +00002245\subsection{Class Objects \label{classObjects}}
2246
2247\obindex{class}
2248Note that the class objects described here represent old-style classes,
2249which will go away in Python 3. When creating new types for extension
2250modules, you will want to work with type objects (section
2251\ref{typeObjects}).
2252
2253\begin{ctypedesc}{PyClassObject}
2254 The C structure of the objects used to describe built-in classes.
2255\end{ctypedesc}
2256
2257\begin{cvardesc}{PyObject*}{PyClass_Type}
2258 This is the type object for class objects; it is the same object as
2259 \code{types.ClassType} in the Python layer.
2260 \withsubitem{(in module types)}{\ttindex{ClassType}}
2261\end{cvardesc}
2262
2263\begin{cfuncdesc}{int}{PyClass_Check}{PyObject *o}
2264 Return true if the object \var{o} is a class object, including
2265 instances of types derived from the standard class object. Return
2266 false in all other cases.
2267\end{cfuncdesc}
2268
2269\begin{cfuncdesc}{int}{PyClass_IsSubclass}{PyObject *klass, PyObject *base}
2270 Return true if \var{klass} is a subclass of \var{base}. Return false in
2271 all other cases.
2272\end{cfuncdesc}
2273
Fred Drake3adf79e2001-10-12 19:01:43 +00002274\subsection{File Objects \label{fileObjects}}
2275
2276\obindex{file}
2277Python's built-in file objects are implemented entirely on the
2278\ctype{FILE*} support from the C standard library. This is an
2279implementation detail and may change in future releases of Python.
2280
2281\begin{ctypedesc}{PyFileObject}
2282 This subtype of \ctype{PyObject} represents a Python file object.
2283\end{ctypedesc}
2284
2285\begin{cvardesc}{PyTypeObject}{PyFile_Type}
2286 This instance of \ctype{PyTypeObject} represents the Python file
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002287 type. This is exposed to Python programs as \code{file} and
2288 \code{types.FileType}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002289 \withsubitem{(in module types)}{\ttindex{FileType}}
2290\end{cvardesc}
2291
2292\begin{cfuncdesc}{int}{PyFile_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002293 Return true if its argument is a \ctype{PyFileObject} or a subtype
Fred Drake3adf79e2001-10-12 19:01:43 +00002294 of \ctype{PyFileObject}.
2295 \versionchanged[Allowed subtypes to be accepted]{2.2}
2296\end{cfuncdesc}
2297
2298\begin{cfuncdesc}{int}{PyFile_CheckExact}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002299 Return true if its argument is a \ctype{PyFileObject}, but not a
Fred Drake3adf79e2001-10-12 19:01:43 +00002300 subtype of \ctype{PyFileObject}.
2301 \versionadded{2.2}
2302\end{cfuncdesc}
2303
2304\begin{cfuncdesc}{PyObject*}{PyFile_FromString}{char *filename, char *mode}
Georg Brandl99363b62005-09-03 07:27:26 +00002305 On success, return a new file object that is opened on the file
Fred Drake3adf79e2001-10-12 19:01:43 +00002306 given by \var{filename}, with a file mode given by \var{mode}, where
2307 \var{mode} has the same semantics as the standard C routine
Georg Brandl99363b62005-09-03 07:27:26 +00002308 \cfunction{fopen()}\ttindex{fopen()}. On failure, return \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002309\end{cfuncdesc}
2310
2311\begin{cfuncdesc}{PyObject*}{PyFile_FromFile}{FILE *fp,
2312 char *name, char *mode,
2313 int (*close)(FILE*)}
Georg Brandl99363b62005-09-03 07:27:26 +00002314 Create a new \ctype{PyFileObject} from the already-open standard C
Fred Drake3adf79e2001-10-12 19:01:43 +00002315 file pointer, \var{fp}. The function \var{close} will be called
Georg Brandl99363b62005-09-03 07:27:26 +00002316 when the file should be closed. Return \NULL{} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00002317\end{cfuncdesc}
2318
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002319\begin{cfuncdesc}{FILE*}{PyFile_AsFile}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002320 Return the file object associated with \var{p} as a \ctype{FILE*}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002321\end{cfuncdesc}
2322
2323\begin{cfuncdesc}{PyObject*}{PyFile_GetLine}{PyObject *p, int n}
2324 Equivalent to \code{\var{p}.readline(\optional{\var{n}})}, this
2325 function reads one line from the object \var{p}. \var{p} may be a
2326 file object or any object with a \method{readline()} method. If
2327 \var{n} is \code{0}, exactly one line is read, regardless of the
2328 length of the line. If \var{n} is greater than \code{0}, no more
2329 than \var{n} bytes will be read from the file; a partial line can be
2330 returned. In both cases, an empty string is returned if the end of
2331 the file is reached immediately. If \var{n} is less than \code{0},
2332 however, one line is read regardless of length, but
2333 \exception{EOFError} is raised if the end of the file is reached
2334 immediately.
2335 \withsubitem{(built-in exception)}{\ttindex{EOFError}}
2336\end{cfuncdesc}
2337
2338\begin{cfuncdesc}{PyObject*}{PyFile_Name}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002339 Return the name of the file specified by \var{p} as a string
Fred Drake3adf79e2001-10-12 19:01:43 +00002340 object.
2341\end{cfuncdesc}
2342
2343\begin{cfuncdesc}{void}{PyFile_SetBufSize}{PyFileObject *p, int n}
2344 Available on systems with \cfunction{setvbuf()}\ttindex{setvbuf()}
2345 only. This should only be called immediately after file object
2346 creation.
2347\end{cfuncdesc}
2348
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00002349\begin{cfuncdesc}{int}{PyFile_Encoding}{PyFileObject *p, char *enc}
2350 Set the file's encoding for Unicode output to \var{enc}. Return
2351 1 on success and 0 on failure.
2352 \versionadded{2.3}
2353\end{cfuncdesc}
2354
Fred Drake3adf79e2001-10-12 19:01:43 +00002355\begin{cfuncdesc}{int}{PyFile_SoftSpace}{PyObject *p, int newflag}
Georg Brandl99363b62005-09-03 07:27:26 +00002356 This function exists for internal use by the interpreter. Set the
Fred Drake3adf79e2001-10-12 19:01:43 +00002357 \member{softspace} attribute of \var{p} to \var{newflag} and
Georg Brandl99363b62005-09-03 07:27:26 +00002358 \withsubitem{(file attribute)}{\ttindex{softspace}}return the
Fred Drake3adf79e2001-10-12 19:01:43 +00002359 previous value. \var{p} does not have to be a file object for this
2360 function to work properly; any object is supported (thought its only
2361 interesting if the \member{softspace} attribute can be set). This
2362 function clears any errors, and will return \code{0} as the previous
2363 value if the attribute either does not exist or if there were errors
2364 in retrieving it. There is no way to detect errors from this
2365 function, but doing so should not be needed.
2366\end{cfuncdesc}
2367
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002368\begin{cfuncdesc}{int}{PyFile_WriteObject}{PyObject *obj, PyObject *p,
Fred Drake3adf79e2001-10-12 19:01:43 +00002369 int flags}
Georg Brandl99363b62005-09-03 07:27:26 +00002370 Write object \var{obj} to file object \var{p}. The only supported
Fred Drake3adf79e2001-10-12 19:01:43 +00002371 flag for \var{flags} is
2372 \constant{Py_PRINT_RAW}\ttindex{Py_PRINT_RAW}; if given, the
2373 \function{str()} of the object is written instead of the
Georg Brandl99363b62005-09-03 07:27:26 +00002374 \function{repr()}. Return \code{0} on success or \code{-1} on
Fred Drake3adf79e2001-10-12 19:01:43 +00002375 failure; the appropriate exception will be set.
2376\end{cfuncdesc}
2377
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002378\begin{cfuncdesc}{int}{PyFile_WriteString}{const char *s, PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002379 Write string \var{s} to file object \var{p}. Return \code{0} on
Fred Drake3adf79e2001-10-12 19:01:43 +00002380 success or \code{-1} on failure; the appropriate exception will be
2381 set.
2382\end{cfuncdesc}
2383
2384
2385\subsection{Instance Objects \label{instanceObjects}}
2386
2387\obindex{instance}
2388There are very few functions specific to instance objects.
2389
2390\begin{cvardesc}{PyTypeObject}{PyInstance_Type}
2391 Type object for class instances.
2392\end{cvardesc}
2393
2394\begin{cfuncdesc}{int}{PyInstance_Check}{PyObject *obj}
Georg Brandl99363b62005-09-03 07:27:26 +00002395 Return true if \var{obj} is an instance.
Fred Drake3adf79e2001-10-12 19:01:43 +00002396\end{cfuncdesc}
2397
2398\begin{cfuncdesc}{PyObject*}{PyInstance_New}{PyObject *class,
2399 PyObject *arg,
2400 PyObject *kw}
2401 Create a new instance of a specific class. The parameters \var{arg}
2402 and \var{kw} are used as the positional and keyword parameters to
2403 the object's constructor.
2404\end{cfuncdesc}
2405
2406\begin{cfuncdesc}{PyObject*}{PyInstance_NewRaw}{PyObject *class,
2407 PyObject *dict}
Neil Schemenauerc4932292005-06-18 17:54:13 +00002408 Create a new instance of a specific class without calling its
Fred Drake3adf79e2001-10-12 19:01:43 +00002409 constructor. \var{class} is the class of new object. The
2410 \var{dict} parameter will be used as the object's \member{__dict__};
Tim Peters9ddf40b2004-06-20 22:41:32 +00002411 if \NULL{}, a new dictionary will be created for the instance.
Fred Drake3adf79e2001-10-12 19:01:43 +00002412\end{cfuncdesc}
2413
2414
Georg Brandl9b743f52006-02-20 12:57:53 +00002415\subsection{Function Objects \label{function-objects}}
2416
2417\obindex{function}
2418There are a few functions specific to Python functions.
2419
2420\begin{ctypedesc}{PyFunctionObject}
2421 The C structure used for functions.
2422\end{ctypedesc}
2423
2424\begin{cvardesc}{PyTypeObject}{PyFunction_Type}
2425 This is an instance of \ctype{PyTypeObject} and represents the
2426 Python function type. It is exposed to Python programmers as
2427 \code{types.FunctionType}.
2428 \withsubitem{(in module types)}{\ttindex{MethodType}}
2429\end{cvardesc}
2430
2431\begin{cfuncdesc}{int}{PyFunction_Check}{PyObject *o}
2432 Return true if \var{o} is a function object (has type
2433 \cdata{PyFunction_Type}). The parameter must not be \NULL{}.
2434\end{cfuncdesc}
2435
2436\begin{cfuncdesc}{PyObject*}{PyFunction_New}{PyObject *code,
2437 PyObject *globals}
2438 Return a new function object associated with the code object
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002439 \var{code}. \var{globals} must be a dictionary with the global
2440 variables accessible to the function.
Georg Brandl9b743f52006-02-20 12:57:53 +00002441
2442 The function's docstring, name and \var{__module__} are retrieved
2443 from the code object, the argument defaults and closure are set to
2444 \NULL{}.
2445\end{cfuncdesc}
2446
2447\begin{cfuncdesc}{PyObject*}{PyFunction_GetCode}{PyObject *op}
2448 Return the code object associated with the function object \var{op}.
2449\end{cfuncdesc}
2450
2451\begin{cfuncdesc}{PyObject*}{PyFunction_GetGlobals}{PyObject *op}
2452 Return the globals dictionary associated with the function object
2453 \var{op}.
2454\end{cfuncdesc}
2455
2456\begin{cfuncdesc}{PyObject*}{PyFunction_GetModule}{PyObject *op}
2457 Return the \var{__module__} attribute of the function object \var{op}.
2458 This is normally a string containing the module name, but can be set
2459 to any other object by Python code.
2460\end{cfuncdesc}
2461
2462\begin{cfuncdesc}{PyObject*}{PyFunction_GetDefaults}{PyObject *op}
2463 Return the argument default values of the function object \var{op}.
2464 This can be a tuple of arguments or \NULL{}.
2465\end{cfuncdesc}
2466
2467\begin{cfuncdesc}{int}{PyFunction_SetDefaults}{PyObject *op,
2468 PyObject *defaults}
2469 Set the argument default values for the function object \var{op}.
2470 \var{defaults} must be \var{Py_None} or a tuple.
2471
2472 Raises \exception{SystemError} and returns \code{-1} on failure.
2473\end{cfuncdesc}
2474
2475\begin{cfuncdesc}{PyObject*}{PyFunction_GetClosure}{PyObject *op}
2476 Return the closure associated with the function object \var{op}.
2477 This can be \NULL{} or a tuple of cell objects.
2478\end{cfuncdesc}
2479
2480\begin{cfuncdesc}{int}{PyFunction_SetClosure}{PyObject *op,
2481 PyObject *closure}
2482 Set the closure associated with the function object \var{op}.
2483 \var{closure} must be \var{Py_None} or a tuple of cell objects.
2484
2485 Raises \exception{SystemError} and returns \code{-1} on failure.
2486\end{cfuncdesc}
2487
2488
Fred Drake3adf79e2001-10-12 19:01:43 +00002489\subsection{Method Objects \label{method-objects}}
2490
2491\obindex{method}
2492There are some useful functions that are useful for working with
2493method objects.
2494
2495\begin{cvardesc}{PyTypeObject}{PyMethod_Type}
2496 This instance of \ctype{PyTypeObject} represents the Python method
2497 type. This is exposed to Python programs as \code{types.MethodType}.
2498 \withsubitem{(in module types)}{\ttindex{MethodType}}
2499\end{cvardesc}
2500
2501\begin{cfuncdesc}{int}{PyMethod_Check}{PyObject *o}
2502 Return true if \var{o} is a method object (has type
Tim Peters9ddf40b2004-06-20 22:41:32 +00002503 \cdata{PyMethod_Type}). The parameter must not be \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002504\end{cfuncdesc}
2505
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002506\begin{cfuncdesc}{PyObject*}{PyMethod_New}{PyObject *func,
Fred Drake3adf79e2001-10-12 19:01:43 +00002507 PyObject *self, PyObject *class}
2508 Return a new method object, with \var{func} being any callable
2509 object; this is the function that will be called when the method is
2510 called. If this method should be bound to an instance, \var{self}
2511 should be the instance and \var{class} should be the class of
2512 \var{self}, otherwise \var{self} should be \NULL{} and \var{class}
2513 should be the class which provides the unbound method..
2514\end{cfuncdesc}
2515
2516\begin{cfuncdesc}{PyObject*}{PyMethod_Class}{PyObject *meth}
2517 Return the class object from which the method \var{meth} was
2518 created; if this was created from an instance, it will be the class
2519 of the instance.
2520\end{cfuncdesc}
2521
2522\begin{cfuncdesc}{PyObject*}{PyMethod_GET_CLASS}{PyObject *meth}
2523 Macro version of \cfunction{PyMethod_Class()} which avoids error
2524 checking.
2525\end{cfuncdesc}
2526
2527\begin{cfuncdesc}{PyObject*}{PyMethod_Function}{PyObject *meth}
2528 Return the function object associated with the method \var{meth}.
2529\end{cfuncdesc}
2530
2531\begin{cfuncdesc}{PyObject*}{PyMethod_GET_FUNCTION}{PyObject *meth}
2532 Macro version of \cfunction{PyMethod_Function()} which avoids error
2533 checking.
2534\end{cfuncdesc}
2535
2536\begin{cfuncdesc}{PyObject*}{PyMethod_Self}{PyObject *meth}
2537 Return the instance associated with the method \var{meth} if it is
Tim Peters9ddf40b2004-06-20 22:41:32 +00002538 bound, otherwise return \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002539\end{cfuncdesc}
2540
2541\begin{cfuncdesc}{PyObject*}{PyMethod_GET_SELF}{PyObject *meth}
2542 Macro version of \cfunction{PyMethod_Self()} which avoids error
2543 checking.
2544\end{cfuncdesc}
2545
2546
2547\subsection{Module Objects \label{moduleObjects}}
2548
2549\obindex{module}
2550There are only a few functions special to module objects.
2551
2552\begin{cvardesc}{PyTypeObject}{PyModule_Type}
2553 This instance of \ctype{PyTypeObject} represents the Python module
2554 type. This is exposed to Python programs as
2555 \code{types.ModuleType}.
2556 \withsubitem{(in module types)}{\ttindex{ModuleType}}
2557\end{cvardesc}
2558
2559\begin{cfuncdesc}{int}{PyModule_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002560 Return true if \var{p} is a module object, or a subtype of a module
Fred Drake3adf79e2001-10-12 19:01:43 +00002561 object.
2562 \versionchanged[Allowed subtypes to be accepted]{2.2}
2563\end{cfuncdesc}
2564
2565\begin{cfuncdesc}{int}{PyModule_CheckExact}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002566 Return true if \var{p} is a module object, but not a subtype of
Fred Drake3adf79e2001-10-12 19:01:43 +00002567 \cdata{PyModule_Type}.
2568 \versionadded{2.2}
2569\end{cfuncdesc}
2570
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002571\begin{cfuncdesc}{PyObject*}{PyModule_New}{const char *name}
Fred Drake3adf79e2001-10-12 19:01:43 +00002572 Return a new module object with the \member{__name__} attribute set
2573 to \var{name}. Only the module's \member{__doc__} and
2574 \member{__name__} attributes are filled in; the caller is
2575 responsible for providing a \member{__file__} attribute.
2576 \withsubitem{(module attribute)}{
2577 \ttindex{__name__}\ttindex{__doc__}\ttindex{__file__}}
2578\end{cfuncdesc}
2579
2580\begin{cfuncdesc}{PyObject*}{PyModule_GetDict}{PyObject *module}
2581 Return the dictionary object that implements \var{module}'s
2582 namespace; this object is the same as the \member{__dict__}
2583 attribute of the module object. This function never fails.
2584 \withsubitem{(module attribute)}{\ttindex{__dict__}}
Fred Drakef495ef72002-04-12 19:32:07 +00002585 It is recommended extensions use other \cfunction{PyModule_*()}
2586 and \cfunction{PyObject_*()} functions rather than directly
2587 manipulate a module's \member{__dict__}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002588\end{cfuncdesc}
2589
2590\begin{cfuncdesc}{char*}{PyModule_GetName}{PyObject *module}
2591 Return \var{module}'s \member{__name__} value. If the module does
2592 not provide one, or if it is not a string, \exception{SystemError}
2593 is raised and \NULL{} is returned.
2594 \withsubitem{(module attribute)}{\ttindex{__name__}}
2595 \withsubitem{(built-in exception)}{\ttindex{SystemError}}
2596\end{cfuncdesc}
2597
2598\begin{cfuncdesc}{char*}{PyModule_GetFilename}{PyObject *module}
2599 Return the name of the file from which \var{module} was loaded using
2600 \var{module}'s \member{__file__} attribute. If this is not defined,
2601 or if it is not a string, raise \exception{SystemError} and return
Tim Peters9ddf40b2004-06-20 22:41:32 +00002602 \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002603 \withsubitem{(module attribute)}{\ttindex{__file__}}
2604 \withsubitem{(built-in exception)}{\ttindex{SystemError}}
2605\end{cfuncdesc}
2606
2607\begin{cfuncdesc}{int}{PyModule_AddObject}{PyObject *module,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002608 const char *name, PyObject *value}
Fred Drake3adf79e2001-10-12 19:01:43 +00002609 Add an object to \var{module} as \var{name}. This is a convenience
2610 function which can be used from the module's initialization
Georg Brandl99363b62005-09-03 07:27:26 +00002611 function. This steals a reference to \var{value}. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00002612 \code{-1} on error, \code{0} on success.
2613 \versionadded{2.0}
2614\end{cfuncdesc}
2615
2616\begin{cfuncdesc}{int}{PyModule_AddIntConstant}{PyObject *module,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002617 const char *name, long value}
Fred Drake3adf79e2001-10-12 19:01:43 +00002618 Add an integer constant to \var{module} as \var{name}. This
2619 convenience function can be used from the module's initialization
Georg Brandl99363b62005-09-03 07:27:26 +00002620 function. Return \code{-1} on error, \code{0} on success.
Fred Drake3adf79e2001-10-12 19:01:43 +00002621 \versionadded{2.0}
2622\end{cfuncdesc}
2623
2624\begin{cfuncdesc}{int}{PyModule_AddStringConstant}{PyObject *module,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002625 const char *name, const char *value}
Fred Drake3adf79e2001-10-12 19:01:43 +00002626 Add a string constant to \var{module} as \var{name}. This
2627 convenience function can be used from the module's initialization
Georg Brandl99363b62005-09-03 07:27:26 +00002628 function. The string \var{value} must be null-terminated. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00002629 \code{-1} on error, \code{0} on success.
2630 \versionadded{2.0}
2631\end{cfuncdesc}
2632
2633
2634\subsection{Iterator Objects \label{iterator-objects}}
2635
2636Python provides two general-purpose iterator objects. The first, a
2637sequence iterator, works with an arbitrary sequence supporting the
2638\method{__getitem__()} method. The second works with a callable
2639object and a sentinel value, calling the callable for each item in the
2640sequence, and ending the iteration when the sentinel value is
2641returned.
2642
2643\begin{cvardesc}{PyTypeObject}{PySeqIter_Type}
2644 Type object for iterator objects returned by
2645 \cfunction{PySeqIter_New()} and the one-argument form of the
2646 \function{iter()} built-in function for built-in sequence types.
2647 \versionadded{2.2}
2648\end{cvardesc}
2649
2650\begin{cfuncdesc}{int}{PySeqIter_Check}{op}
2651 Return true if the type of \var{op} is \cdata{PySeqIter_Type}.
2652 \versionadded{2.2}
2653\end{cfuncdesc}
2654
2655\begin{cfuncdesc}{PyObject*}{PySeqIter_New}{PyObject *seq}
2656 Return an iterator that works with a general sequence object,
2657 \var{seq}. The iteration ends when the sequence raises
2658 \exception{IndexError} for the subscripting operation.
2659 \versionadded{2.2}
2660\end{cfuncdesc}
2661
2662\begin{cvardesc}{PyTypeObject}{PyCallIter_Type}
2663 Type object for iterator objects returned by
2664 \cfunction{PyCallIter_New()} and the two-argument form of the
2665 \function{iter()} built-in function.
2666 \versionadded{2.2}
2667\end{cvardesc}
2668
2669\begin{cfuncdesc}{int}{PyCallIter_Check}{op}
2670 Return true if the type of \var{op} is \cdata{PyCallIter_Type}.
2671 \versionadded{2.2}
2672\end{cfuncdesc}
2673
2674\begin{cfuncdesc}{PyObject*}{PyCallIter_New}{PyObject *callable,
2675 PyObject *sentinel}
2676 Return a new iterator. The first parameter, \var{callable}, can be
2677 any Python callable object that can be called with no parameters;
2678 each call to it should return the next item in the iteration. When
2679 \var{callable} returns a value equal to \var{sentinel}, the
2680 iteration will be terminated.
2681 \versionadded{2.2}
2682\end{cfuncdesc}
2683
2684
2685\subsection{Descriptor Objects \label{descriptor-objects}}
2686
Fred Drake54e62942001-12-11 19:40:16 +00002687``Descriptors'' are objects that describe some attribute of an object.
2688They are found in the dictionary of type objects.
2689
Fred Drake3adf79e2001-10-12 19:01:43 +00002690\begin{cvardesc}{PyTypeObject}{PyProperty_Type}
Fred Drake54e62942001-12-11 19:40:16 +00002691 The type object for the built-in descriptor types.
Fred Drake3adf79e2001-10-12 19:01:43 +00002692 \versionadded{2.2}
2693\end{cvardesc}
2694
2695\begin{cfuncdesc}{PyObject*}{PyDescr_NewGetSet}{PyTypeObject *type,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002696 struct PyGetSetDef *getset}
Fred Drake3adf79e2001-10-12 19:01:43 +00002697 \versionadded{2.2}
2698\end{cfuncdesc}
2699
2700\begin{cfuncdesc}{PyObject*}{PyDescr_NewMember}{PyTypeObject *type,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002701 struct PyMemberDef *meth}
Fred Drake3adf79e2001-10-12 19:01:43 +00002702 \versionadded{2.2}
2703\end{cfuncdesc}
2704
2705\begin{cfuncdesc}{PyObject*}{PyDescr_NewMethod}{PyTypeObject *type,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002706 struct PyMethodDef *meth}
Fred Drake3adf79e2001-10-12 19:01:43 +00002707 \versionadded{2.2}
2708\end{cfuncdesc}
2709
2710\begin{cfuncdesc}{PyObject*}{PyDescr_NewWrapper}{PyTypeObject *type,
2711 struct wrapperbase *wrapper,
2712 void *wrapped}
2713 \versionadded{2.2}
2714\end{cfuncdesc}
2715
Thomas Heller8178a222004-02-09 10:47:11 +00002716\begin{cfuncdesc}{PyObject*}{PyDescr_NewClassMethod}{PyTypeObject *type,
2717 PyMethodDef *method}
2718 \versionadded{2.3}
2719\end{cfuncdesc}
2720
Fred Drake3adf79e2001-10-12 19:01:43 +00002721\begin{cfuncdesc}{int}{PyDescr_IsData}{PyObject *descr}
Georg Brandl99363b62005-09-03 07:27:26 +00002722 Return true if the descriptor objects \var{descr} describes a data
Fred Drake3adf79e2001-10-12 19:01:43 +00002723 attribute, or false if it describes a method. \var{descr} must be a
2724 descriptor object; there is no error checking.
2725 \versionadded{2.2}
2726\end{cfuncdesc}
2727
2728\begin{cfuncdesc}{PyObject*}{PyWrapper_New}{PyObject *, PyObject *}
2729 \versionadded{2.2}
2730\end{cfuncdesc}
2731
2732
2733\subsection{Slice Objects \label{slice-objects}}
2734
2735\begin{cvardesc}{PyTypeObject}{PySlice_Type}
2736 The type object for slice objects. This is the same as
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002737 \code{slice} and \code{types.SliceType}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002738 \withsubitem{(in module types)}{\ttindex{SliceType}}
2739\end{cvardesc}
2740
2741\begin{cfuncdesc}{int}{PySlice_Check}{PyObject *ob}
Georg Brandl99363b62005-09-03 07:27:26 +00002742 Return true if \var{ob} is a slice object; \var{ob} must not be
Tim Peters9ddf40b2004-06-20 22:41:32 +00002743 \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002744\end{cfuncdesc}
2745
2746\begin{cfuncdesc}{PyObject*}{PySlice_New}{PyObject *start, PyObject *stop,
2747 PyObject *step}
2748 Return a new slice object with the given values. The \var{start},
2749 \var{stop}, and \var{step} parameters are used as the values of the
2750 slice object attributes of the same names. Any of the values may be
Tim Peters9ddf40b2004-06-20 22:41:32 +00002751 \NULL{}, in which case the \code{None} will be used for the
Georg Brandl99363b62005-09-03 07:27:26 +00002752 corresponding attribute. Return \NULL{} if the new object could
Fred Drake3adf79e2001-10-12 19:01:43 +00002753 not be allocated.
2754\end{cfuncdesc}
2755
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002756\begin{cfuncdesc}{int}{PySlice_GetIndices}{PySliceObject *slice, Py_ssize_t length,
2757 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step}
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002758Retrieve the start, stop and step indices from the slice object
2759\var{slice}, assuming a sequence of length \var{length}. Treats
2760indices greater than \var{length} as errors.
2761
2762Returns 0 on success and -1 on error with no exception set (unless one
2763of the indices was not \constant{None} and failed to be converted to
2764an integer, in which case -1 is returned with an exception set).
2765
2766You probably do not want to use this function. If you want to use
2767slice objects in versions of Python prior to 2.3, you would probably
2768do well to incorporate the source of \cfunction{PySlice_GetIndicesEx},
2769suitably renamed, in the source of your extension.
2770\end{cfuncdesc}
2771
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002772\begin{cfuncdesc}{int}{PySlice_GetIndicesEx}{PySliceObject *slice, Py_ssize_t length,
2773 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step,
2774 Py_ssize_t *slicelength}
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002775Usable replacement for \cfunction{PySlice_GetIndices}. Retrieve the
2776start, stop, and step indices from the slice object \var{slice}
2777assuming a sequence of length \var{length}, and store the length of
2778the slice in \var{slicelength}. Out of bounds indices are clipped in
2779a manner consistent with the handling of normal slices.
2780
2781Returns 0 on success and -1 on error with exception set.
2782
2783\versionadded{2.3}
Fred Drake3adf79e2001-10-12 19:01:43 +00002784\end{cfuncdesc}
2785
2786
2787\subsection{Weak Reference Objects \label{weakref-objects}}
2788
2789Python supports \emph{weak references} as first-class objects. There
2790are two specific object types which directly implement weak
2791references. The first is a simple reference object, and the second
2792acts as a proxy for the original object as much as it can.
2793
2794\begin{cfuncdesc}{int}{PyWeakref_Check}{ob}
2795 Return true if \var{ob} is either a reference or proxy object.
2796 \versionadded{2.2}
2797\end{cfuncdesc}
2798
2799\begin{cfuncdesc}{int}{PyWeakref_CheckRef}{ob}
2800 Return true if \var{ob} is a reference object.
2801 \versionadded{2.2}
2802\end{cfuncdesc}
2803
2804\begin{cfuncdesc}{int}{PyWeakref_CheckProxy}{ob}
2805 Return true if \var{ob} is a proxy object.
2806 \versionadded{2.2}
2807\end{cfuncdesc}
2808
2809\begin{cfuncdesc}{PyObject*}{PyWeakref_NewRef}{PyObject *ob,
2810 PyObject *callback}
2811 Return a weak reference object for the object \var{ob}. This will
2812 always return a new reference, but is not guaranteed to create a new
2813 object; an existing reference object may be returned. The second
2814 parameter, \var{callback}, can be a callable object that receives
2815 notification when \var{ob} is garbage collected; it should accept a
Raymond Hettinger5232f502004-03-25 08:51:36 +00002816 single parameter, which will be the weak reference object itself.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002817 \var{callback} may also be \code{None} or \NULL{}. If \var{ob}
Fred Drake3adf79e2001-10-12 19:01:43 +00002818 is not a weakly-referencable object, or if \var{callback} is not
Tim Peters9ddf40b2004-06-20 22:41:32 +00002819 callable, \code{None}, or \NULL{}, this will return \NULL{} and
Fred Drake3adf79e2001-10-12 19:01:43 +00002820 raise \exception{TypeError}.
2821 \versionadded{2.2}
2822\end{cfuncdesc}
2823
2824\begin{cfuncdesc}{PyObject*}{PyWeakref_NewProxy}{PyObject *ob,
2825 PyObject *callback}
2826 Return a weak reference proxy object for the object \var{ob}. This
2827 will always return a new reference, but is not guaranteed to create
2828 a new object; an existing proxy object may be returned. The second
2829 parameter, \var{callback}, can be a callable object that receives
2830 notification when \var{ob} is garbage collected; it should accept a
Raymond Hettinger5232f502004-03-25 08:51:36 +00002831 single parameter, which will be the weak reference object itself.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002832 \var{callback} may also be \code{None} or \NULL{}. If \var{ob} is not
Fred Drake3adf79e2001-10-12 19:01:43 +00002833 a weakly-referencable object, or if \var{callback} is not callable,
Tim Peters9ddf40b2004-06-20 22:41:32 +00002834 \code{None}, or \NULL{}, this will return \NULL{} and raise
Fred Drake3adf79e2001-10-12 19:01:43 +00002835 \exception{TypeError}.
2836 \versionadded{2.2}
2837\end{cfuncdesc}
2838
2839\begin{cfuncdesc}{PyObject*}{PyWeakref_GetObject}{PyObject *ref}
Georg Brandl99363b62005-09-03 07:27:26 +00002840 Return the referenced object from a weak reference, \var{ref}. If
Ka-Ping Yeebd379e92003-03-28 18:07:16 +00002841 the referent is no longer live, returns \code{None}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002842 \versionadded{2.2}
2843\end{cfuncdesc}
2844
2845\begin{cfuncdesc}{PyObject*}{PyWeakref_GET_OBJECT}{PyObject *ref}
2846 Similar to \cfunction{PyWeakref_GetObject()}, but implemented as a
2847 macro that does no error checking.
2848 \versionadded{2.2}
2849\end{cfuncdesc}
2850
2851
2852\subsection{CObjects \label{cObjects}}
2853
2854\obindex{CObject}
2855Refer to \emph{Extending and Embedding the Python Interpreter},
Fred Drake54e62942001-12-11 19:40:16 +00002856section~1.12, ``Providing a C API for an Extension Module,'' for more
Fred Drake3adf79e2001-10-12 19:01:43 +00002857information on using these objects.
2858
2859
2860\begin{ctypedesc}{PyCObject}
2861 This subtype of \ctype{PyObject} represents an opaque value, useful
2862 for C extension modules who need to pass an opaque value (as a
2863 \ctype{void*} pointer) through Python code to other C code. It is
2864 often used to make a C function pointer defined in one module
2865 available to other modules, so the regular import mechanism can be
2866 used to access C APIs defined in dynamically loaded modules.
2867\end{ctypedesc}
2868
2869\begin{cfuncdesc}{int}{PyCObject_Check}{PyObject *p}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002870 Return true if its argument is a \ctype{PyCObject}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002871\end{cfuncdesc}
2872
Tim Petersf582b822001-12-11 18:51:08 +00002873\begin{cfuncdesc}{PyObject*}{PyCObject_FromVoidPtr}{void* cobj,
Fred Drake54e62942001-12-11 19:40:16 +00002874 void (*destr)(void *)}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002875 Create a \ctype{PyCObject} from the \code{void *}\var{cobj}. The
Fred Drake3adf79e2001-10-12 19:01:43 +00002876 \var{destr} function will be called when the object is reclaimed,
Tim Peters9ddf40b2004-06-20 22:41:32 +00002877 unless it is \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002878\end{cfuncdesc}
2879
2880\begin{cfuncdesc}{PyObject*}{PyCObject_FromVoidPtrAndDesc}{void* cobj,
2881 void* desc, void (*destr)(void *, void *)}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002882 Create a \ctype{PyCObject} from the \ctype{void *}\var{cobj}. The
Fred Drake3adf79e2001-10-12 19:01:43 +00002883 \var{destr} function will be called when the object is reclaimed.
2884 The \var{desc} argument can be used to pass extra callback data for
2885 the destructor function.
2886\end{cfuncdesc}
2887
2888\begin{cfuncdesc}{void*}{PyCObject_AsVoidPtr}{PyObject* self}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002889 Return the object \ctype{void *} that the \ctype{PyCObject}
Fred Drake3adf79e2001-10-12 19:01:43 +00002890 \var{self} was created with.
2891\end{cfuncdesc}
2892
2893\begin{cfuncdesc}{void*}{PyCObject_GetDesc}{PyObject* self}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002894 Return the description \ctype{void *} that the \ctype{PyCObject}
Fred Drake3adf79e2001-10-12 19:01:43 +00002895 \var{self} was created with.
2896\end{cfuncdesc}
Fred Drakecd8474e2001-11-26 21:29:17 +00002897
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002898\begin{cfuncdesc}{int}{PyCObject_SetVoidPtr}{PyObject* self, void* cobj}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002899 Set the void pointer inside \var{self} to \var{cobj}.
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002900 The \ctype{PyCObject} must not have an associated destructor.
2901 Return true on success, false on failure.
2902\end{cfuncdesc}
2903
Fred Drakecd8474e2001-11-26 21:29:17 +00002904
2905\subsection{Cell Objects \label{cell-objects}}
2906
2907``Cell'' objects are used to implement variables referenced by
2908multiple scopes. For each such variable, a cell object is created to
2909store the value; the local variables of each stack frame that
2910references the value contains a reference to the cells from outer
2911scopes which also use that variable. When the value is accessed, the
2912value contained in the cell is used instead of the cell object
2913itself. This de-referencing of the cell object requires support from
2914the generated byte-code; these are not automatically de-referenced
2915when accessed. Cell objects are not likely to be useful elsewhere.
2916
Fred Drake54e62942001-12-11 19:40:16 +00002917\begin{ctypedesc}{PyCellObject}
2918 The C structure used for cell objects.
2919\end{ctypedesc}
2920
Fred Drakecd8474e2001-11-26 21:29:17 +00002921\begin{cvardesc}{PyTypeObject}{PyCell_Type}
Georg Brandl9b743f52006-02-20 12:57:53 +00002922 The type object corresponding to cell objects.
Fred Drakecd8474e2001-11-26 21:29:17 +00002923\end{cvardesc}
2924
2925\begin{cfuncdesc}{int}{PyCell_Check}{ob}
2926 Return true if \var{ob} is a cell object; \var{ob} must not be
Tim Peters9ddf40b2004-06-20 22:41:32 +00002927 \NULL{}.
Fred Drakecd8474e2001-11-26 21:29:17 +00002928\end{cfuncdesc}
2929
2930\begin{cfuncdesc}{PyObject*}{PyCell_New}{PyObject *ob}
2931 Create and return a new cell object containing the value \var{ob}.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002932 The parameter may be \NULL{}.
Fred Drakecd8474e2001-11-26 21:29:17 +00002933\end{cfuncdesc}
2934
2935\begin{cfuncdesc}{PyObject*}{PyCell_Get}{PyObject *cell}
2936 Return the contents of the cell \var{cell}.
2937\end{cfuncdesc}
2938
2939\begin{cfuncdesc}{PyObject*}{PyCell_GET}{PyObject *cell}
2940 Return the contents of the cell \var{cell}, but without checking
Raymond Hettingerf4bb1f92003-08-23 03:38:11 +00002941 that \var{cell} is non-\NULL{} and a cell object.
Fred Drakecd8474e2001-11-26 21:29:17 +00002942\end{cfuncdesc}
2943
2944\begin{cfuncdesc}{int}{PyCell_Set}{PyObject *cell, PyObject *value}
2945 Set the contents of the cell object \var{cell} to \var{value}. This
2946 releases the reference to any current content of the cell.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002947 \var{value} may be \NULL{}. \var{cell} must be non-\NULL{}; if it is
Fred Drakecd8474e2001-11-26 21:29:17 +00002948 not a cell object, \code{-1} will be returned. On success, \code{0}
2949 will be returned.
2950\end{cfuncdesc}
2951
2952\begin{cfuncdesc}{void}{PyCell_SET}{PyObject *cell, PyObject *value}
2953 Sets the value of the cell object \var{cell} to \var{value}. No
2954 reference counts are adjusted, and no checks are made for safety;
2955 \var{cell} must be non-\NULL{} and must be a cell object.
2956\end{cfuncdesc}
Martin v. Löwise440e472004-06-01 15:22:42 +00002957
2958
2959\subsection{Generator Objects \label{gen-objects}}
2960
2961Generator objects are what Python uses to implement generator iterators.
2962They are normally created by iterating over a function that yields values,
2963rather than explicitly calling \cfunction{PyGen_New}.
2964
2965\begin{ctypedesc}{PyGenObject}
2966 The C structure used for generator objects.
2967\end{ctypedesc}
2968
2969\begin{cvardesc}{PyTypeObject}{PyGen_Type}
2970 The type object corresponding to generator objects
2971\end{cvardesc}
2972
2973\begin{cfuncdesc}{int}{PyGen_Check}{ob}
2974 Return true if \var{ob} is a generator object; \var{ob} must not be
Tim Peters9ddf40b2004-06-20 22:41:32 +00002975 \NULL{}.
Martin v. Löwise440e472004-06-01 15:22:42 +00002976\end{cfuncdesc}
2977
2978\begin{cfuncdesc}{int}{PyGen_CheckExact}{ob}
2979 Return true if \var{ob}'s type is \var{PyGen_Type}
2980 is a generator object; \var{ob} must not be
Tim Peters9ddf40b2004-06-20 22:41:32 +00002981 \NULL{}.
Martin v. Löwise440e472004-06-01 15:22:42 +00002982\end{cfuncdesc}
2983
2984\begin{cfuncdesc}{PyObject*}{PyGen_New}{PyFrameObject *frame}
2985 Create and return a new generator object based on the \var{frame} object.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002986 A reference to \var{frame} is stolen by this function.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002987 The parameter must not be \NULL{}.
2988\end{cfuncdesc}
2989
2990
2991\subsection{DateTime Objects \label{datetime-objects}}
2992
2993Various date and time objects are supplied by the \module{datetime}
2994module. Before using any of these functions, the header file
2995\file{datetime.h} must be included in your source (note that this is
Thomas Wouters89f507f2006-12-13 04:49:30 +00002996not included by \file{Python.h}), and the macro
2997\cfunction{PyDateTime_IMPORT} must be invoked. The macro puts a
2998pointer to a C structure into a static variable,
2999\code{PyDateTimeAPI}, that is used by the following macros.
Tim Peters9ddf40b2004-06-20 22:41:32 +00003000
Tim Peters183dabc2004-07-11 19:26:19 +00003001Type-check macros:
3002
3003\begin{cfuncdesc}{int}{PyDate_Check}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00003004 Return true if \var{ob} is of type \cdata{PyDateTime_DateType} or
3005 a subtype of \cdata{PyDateTime_DateType}. \var{ob} must not be
3006 \NULL{}.
3007 \versionadded{2.4}
3008\end{cfuncdesc}
3009
Tim Peters183dabc2004-07-11 19:26:19 +00003010\begin{cfuncdesc}{int}{PyDate_CheckExact}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00003011 Return true if \var{ob} is of type \cdata{PyDateTime_DateType}.
3012 \var{ob} must not be \NULL{}.
3013 \versionadded{2.4}
3014\end{cfuncdesc}
3015
Tim Peters183dabc2004-07-11 19:26:19 +00003016\begin{cfuncdesc}{int}{PyDateTime_Check}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00003017 Return true if \var{ob} is of type \cdata{PyDateTime_DateTimeType} or
3018 a subtype of \cdata{PyDateTime_DateTimeType}. \var{ob} must not be
3019 \NULL{}.
3020 \versionadded{2.4}
3021\end{cfuncdesc}
3022
Tim Peters183dabc2004-07-11 19:26:19 +00003023\begin{cfuncdesc}{int}{PyDateTime_CheckExact}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00003024 Return true if \var{ob} is of type \cdata{PyDateTime_DateTimeType}.
3025 \var{ob} must not be \NULL{}.
3026 \versionadded{2.4}
3027\end{cfuncdesc}
3028
Tim Peters183dabc2004-07-11 19:26:19 +00003029\begin{cfuncdesc}{int}{PyTime_Check}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00003030 Return true if \var{ob} is of type \cdata{PyDateTime_TimeType} or
3031 a subtype of \cdata{PyDateTime_TimeType}. \var{ob} must not be
3032 \NULL{}.
3033 \versionadded{2.4}
3034\end{cfuncdesc}
3035
Tim Peters183dabc2004-07-11 19:26:19 +00003036\begin{cfuncdesc}{int}{PyTime_CheckExact}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00003037 Return true if \var{ob} is of type \cdata{PyDateTime_TimeType}.
3038 \var{ob} must not be \NULL{}.
3039 \versionadded{2.4}
3040\end{cfuncdesc}
3041
Tim Peters183dabc2004-07-11 19:26:19 +00003042\begin{cfuncdesc}{int}{PyDelta_Check}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00003043 Return true if \var{ob} is of type \cdata{PyDateTime_DeltaType} or
3044 a subtype of \cdata{PyDateTime_DeltaType}. \var{ob} must not be
3045 \NULL{}.
3046 \versionadded{2.4}
3047\end{cfuncdesc}
3048
Tim Peters183dabc2004-07-11 19:26:19 +00003049\begin{cfuncdesc}{int}{PyDelta_CheckExact}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00003050 Return true if \var{ob} is of type \cdata{PyDateTime_DeltaType}.
3051 \var{ob} must not be \NULL{}.
3052 \versionadded{2.4}
3053\end{cfuncdesc}
3054
Tim Peters183dabc2004-07-11 19:26:19 +00003055\begin{cfuncdesc}{int}{PyTZInfo_Check}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00003056 Return true if \var{ob} is of type \cdata{PyDateTime_TZInfoType} or
3057 a subtype of \cdata{PyDateTime_TZInfoType}. \var{ob} must not be
3058 \NULL{}.
3059 \versionadded{2.4}
3060\end{cfuncdesc}
3061
Tim Peters183dabc2004-07-11 19:26:19 +00003062\begin{cfuncdesc}{int}{PyTZInfo_CheckExact}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00003063 Return true if \var{ob} is of type \cdata{PyDateTime_TZInfoType}.
3064 \var{ob} must not be \NULL{}.
3065 \versionadded{2.4}
3066\end{cfuncdesc}
3067
Tim Peters183dabc2004-07-11 19:26:19 +00003068Macros to create objects:
3069
Tim Peters9ddf40b2004-06-20 22:41:32 +00003070\begin{cfuncdesc}{PyObject*}{PyDate_FromDate}{int year, int month, int day}
3071 Return a \code{datetime.date} object with the specified year, month
3072 and day.
3073 \versionadded{2.4}
3074\end{cfuncdesc}
3075
Brett Cannon5bbe6ad2005-02-17 05:17:17 +00003076\begin{cfuncdesc}{PyObject*}{PyDateTime_FromDateAndTime}{int year, int month,
Tim Peters9ddf40b2004-06-20 22:41:32 +00003077 int day, int hour, int minute, int second, int usecond}
3078 Return a \code{datetime.datetime} object with the specified year, month,
3079 day, hour, minute, second and microsecond.
3080 \versionadded{2.4}
3081\end{cfuncdesc}
3082
3083\begin{cfuncdesc}{PyObject*}{PyTime_FromTime}{int hour, int minute,
3084 int second, int usecond}
3085 Return a \code{datetime.time} object with the specified hour, minute,
3086 second and microsecond.
3087 \versionadded{2.4}
3088\end{cfuncdesc}
3089
3090\begin{cfuncdesc}{PyObject*}{PyDelta_FromDSU}{int days, int seconds,
3091 int useconds}
3092 Return a \code{datetime.timedelta} object representing the given number
3093 of days, seconds and microseconds. Normalization is performed so that
3094 the resulting number of microseconds and seconds lie in the ranges
3095 documented for \code{datetime.timedelta} objects.
3096 \versionadded{2.4}
3097\end{cfuncdesc}
3098
Tim Peters8ff9f9f2004-07-17 01:42:26 +00003099Macros to extract fields from date objects. The argument must be an
Tim Peters183dabc2004-07-11 19:26:19 +00003100instance of \cdata{PyDateTime_Date}, including subclasses (such as
3101\cdata{PyDateTime_DateTime}). The argument must not be \NULL{}, and
3102the type is not checked:
3103
3104\begin{cfuncdesc}{int}{PyDateTime_GET_YEAR}{PyDateTime_Date *o}
3105 Return the year, as a positive int.
3106 \versionadded{2.4}
3107\end{cfuncdesc}
3108
3109\begin{cfuncdesc}{int}{PyDateTime_GET_MONTH}{PyDateTime_Date *o}
3110 Return the month, as an int from 1 through 12.
3111 \versionadded{2.4}
3112\end{cfuncdesc}
3113
3114\begin{cfuncdesc}{int}{PyDateTime_GET_DAY}{PyDateTime_Date *o}
3115 Return the day, as an int from 1 through 31.
3116 \versionadded{2.4}
3117\end{cfuncdesc}
3118
Tim Peters8ff9f9f2004-07-17 01:42:26 +00003119Macros to extract fields from datetime objects. The argument must be an
Tim Peters183dabc2004-07-11 19:26:19 +00003120instance of \cdata{PyDateTime_DateTime}, including subclasses.
3121The argument must not be \NULL{}, and the type is not checked:
3122
3123\begin{cfuncdesc}{int}{PyDateTime_DATE_GET_HOUR}{PyDateTime_DateTime *o}
Neal Norwitz7fdd92f2004-08-02 21:56:33 +00003124 Return the hour, as an int from 0 through 23.
Tim Peters183dabc2004-07-11 19:26:19 +00003125 \versionadded{2.4}
3126\end{cfuncdesc}
3127
3128\begin{cfuncdesc}{int}{PyDateTime_DATE_GET_MINUTE}{PyDateTime_DateTime *o}
3129 Return the minute, as an int from 0 through 59.
3130 \versionadded{2.4}
3131\end{cfuncdesc}
3132
3133\begin{cfuncdesc}{int}{PyDateTime_DATE_GET_SECOND}{PyDateTime_DateTime *o}
3134 Return the second, as an int from 0 through 59.
3135 \versionadded{2.4}
3136\end{cfuncdesc}
3137
3138\begin{cfuncdesc}{int}{PyDateTime_DATE_GET_MICROSECOND}{PyDateTime_DateTime *o}
3139 Return the microsecond, as an int from 0 through 999999.
3140 \versionadded{2.4}
3141\end{cfuncdesc}
3142
Tim Peters8ff9f9f2004-07-17 01:42:26 +00003143Macros to extract fields from time objects. The argument must be an
Tim Peters183dabc2004-07-11 19:26:19 +00003144instance of \cdata{PyDateTime_Time}, including subclasses.
3145The argument must not be \NULL{}, and the type is not checked:
3146
3147\begin{cfuncdesc}{int}{PyDateTime_TIME_GET_HOUR}{PyDateTime_Time *o}
Neal Norwitz7fdd92f2004-08-02 21:56:33 +00003148 Return the hour, as an int from 0 through 23.
Tim Peters183dabc2004-07-11 19:26:19 +00003149 \versionadded{2.4}
3150\end{cfuncdesc}
3151
3152\begin{cfuncdesc}{int}{PyDateTime_TIME_GET_MINUTE}{PyDateTime_Time *o}
3153 Return the minute, as an int from 0 through 59.
3154 \versionadded{2.4}
3155\end{cfuncdesc}
3156
3157\begin{cfuncdesc}{int}{PyDateTime_TIME_GET_SECOND}{PyDateTime_Time *o}
3158 Return the second, as an int from 0 through 59.
3159 \versionadded{2.4}
3160\end{cfuncdesc}
3161
3162\begin{cfuncdesc}{int}{PyDateTime_TIME_GET_MICROSECOND}{PyDateTime_Time *o}
3163 Return the microsecond, as an int from 0 through 999999.
3164 \versionadded{2.4}
3165\end{cfuncdesc}
3166
3167Macros for the convenience of modules implementing the DB API:
3168
Tim Peters9ddf40b2004-06-20 22:41:32 +00003169\begin{cfuncdesc}{PyObject*}{PyDateTime_FromTimestamp}{PyObject *args}
3170 Create and return a new \code{datetime.datetime} object given an argument
3171 tuple suitable for passing to \code{datetime.datetime.fromtimestamp()}.
Tim Peters9ddf40b2004-06-20 22:41:32 +00003172 \versionadded{2.4}
3173\end{cfuncdesc}
3174
3175\begin{cfuncdesc}{PyObject*}{PyDate_FromTimestamp}{PyObject *args}
3176 Create and return a new \code{datetime.date} object given an argument
3177 tuple suitable for passing to \code{datetime.date.fromtimestamp()}.
Tim Peters9ddf40b2004-06-20 22:41:32 +00003178 \versionadded{2.4}
Martin v. Löwise440e472004-06-01 15:22:42 +00003179\end{cfuncdesc}
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003180
3181
3182\subsection{Set Objects \label{setObjects}}
Thomas Wouters477c8d52006-05-27 19:21:47 +00003183\sectionauthor{Raymond D. Hettinger}{python@rcn.com}
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003184
3185\obindex{set}
3186\obindex{frozenset}
3187\versionadded{2.5}
3188
3189This section details the public API for \class{set} and \class{frozenset}
3190objects. Any functionality not listed below is best accessed using the
Raymond Hettinger0c230b92005-08-17 10:05:22 +00003191either the abstract object protocol (including
Thomas Wouters477c8d52006-05-27 19:21:47 +00003192\cfunction{PyObject_CallMethod()}, \cfunction{PyObject_RichCompareBool()},
3193\cfunction{PyObject_Hash()}, \cfunction{PyObject_Repr()},
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003194\cfunction{PyObject_IsTrue()}, \cfunction{PyObject_Print()}, and
Raymond Hettinger0c230b92005-08-17 10:05:22 +00003195\cfunction{PyObject_GetIter()})
3196or the abstract number protocol (including
Thomas Wouters89f507f2006-12-13 04:49:30 +00003197\cfunction{PyNumber_And()}, \cfunction{PyNumber_Subtract()},
Raymond Hettinger0c230b92005-08-17 10:05:22 +00003198\cfunction{PyNumber_Or()}, \cfunction{PyNumber_Xor()},
Thomas Wouters89f507f2006-12-13 04:49:30 +00003199\cfunction{PyNumber_InPlaceAnd()}, \cfunction{PyNumber_InPlaceSubtract()},
Georg Brandlb518d8c2006-02-22 11:46:55 +00003200\cfunction{PyNumber_InPlaceOr()}, and \cfunction{PyNumber_InPlaceXor()}).
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003201
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003202\begin{ctypedesc}{PySetObject}
3203 This subtype of \ctype{PyObject} is used to hold the internal data for
3204 both \class{set} and \class{frozenset} objects. It is like a
3205 \ctype{PyDictObject} in that it is a fixed size for small sets
3206 (much like tuple storage) and will point to a separate, variable sized
3207 block of memory for medium and large sized sets (much like list storage).
3208 None of the fields of this structure should be considered public and
3209 are subject to change. All access should be done through the
Thomas Wouters477c8d52006-05-27 19:21:47 +00003210 documented API rather than by manipulating the values in the structure.
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003211
3212\end{ctypedesc}
3213
3214\begin{cvardesc}{PyTypeObject}{PySet_Type}
3215 This is an instance of \ctype{PyTypeObject} representing the Python
3216 \class{set} type.
3217\end{cvardesc}
3218
3219\begin{cvardesc}{PyTypeObject}{PyFrozenSet_Type}
3220 This is an instance of \ctype{PyTypeObject} representing the Python
3221 \class{frozenset} type.
3222\end{cvardesc}
3223
3224
3225The following type check macros work on pointers to any Python object.
3226Likewise, the constructor functions work with any iterable Python object.
3227
3228\begin{cfuncdesc}{int}{PyAnySet_Check}{PyObject *p}
Thomas Wouters477c8d52006-05-27 19:21:47 +00003229 Return true if \var{p} is a \class{set} object, a \class{frozenset}
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003230 object, or an instance of a subtype.
3231\end{cfuncdesc}
3232
3233\begin{cfuncdesc}{int}{PyAnySet_CheckExact}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00003234 Return true if \var{p} is a \class{set} object or a \class{frozenset}
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003235 object but not an instance of a subtype.
3236\end{cfuncdesc}
3237
3238\begin{cfuncdesc}{int}{PyFrozenSet_CheckExact}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00003239 Return true if \var{p} is a \class{frozenset} object
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003240 but not an instance of a subtype.
3241\end{cfuncdesc}
3242
3243\begin{cfuncdesc}{PyObject*}{PySet_New}{PyObject *iterable}
Georg Brandl99363b62005-09-03 07:27:26 +00003244 Return a new \class{set} containing objects returned by the
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003245 \var{iterable}. The \var{iterable} may be \NULL{} to create a
Georg Brandl99363b62005-09-03 07:27:26 +00003246 new empty set. Return the new set on success or \NULL{} on
3247 failure. Raise \exception{TypeError} if \var{iterable} is
Raymond Hettinger94fedf92005-08-17 12:23:45 +00003248 not actually iterable. The constructor is also useful for
3249 copying a set (\code{c=set(s)}).
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003250\end{cfuncdesc}
3251
3252\begin{cfuncdesc}{PyObject*}{PyFrozenSet_New}{PyObject *iterable}
Georg Brandl99363b62005-09-03 07:27:26 +00003253 Return a new \class{frozenset} containing objects returned by the
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003254 \var{iterable}. The \var{iterable} may be \NULL{} to create a
Georg Brandl99363b62005-09-03 07:27:26 +00003255 new empty frozenset. Return the new set on success or \NULL{} on
3256 failure. Raise \exception{TypeError} if \var{iterable} is
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00003257 not actually iterable.
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003258\end{cfuncdesc}
3259
3260
3261The following functions and macros are available for instances of
3262\class{set} or \class{frozenset} or instances of their subtypes.
3263
3264\begin{cfuncdesc}{int}{PySet_Size}{PyObject *anyset}
Georg Brandl99363b62005-09-03 07:27:26 +00003265 Return the length of a \class{set} or \class{frozenset} object.
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003266 Equivalent to \samp{len(\var{anyset})}. Raises a
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00003267 \exception{PyExc_SystemError} if \var{anyset} is not a \class{set},
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003268 \class{frozenset}, or an instance of a subtype.
3269 \bifuncindex{len}
3270\end{cfuncdesc}
3271
3272\begin{cfuncdesc}{int}{PySet_GET_SIZE}{PyObject *anyset}
3273 Macro form of \cfunction{PySet_Size()} without error checking.
3274\end{cfuncdesc}
3275
3276\begin{cfuncdesc}{int}{PySet_Contains}{PyObject *anyset, PyObject *key}
Georg Brandl99363b62005-09-03 07:27:26 +00003277 Return 1 if found, 0 if not found, and -1 if an error is
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003278 encountered. Unlike the Python \method{__contains__()} method, this
3279 function does not automatically convert unhashable sets into temporary
Georg Brandl99363b62005-09-03 07:27:26 +00003280 frozensets. Raise a \exception{TypeError} if the \var{key} is unhashable.
3281 Raise \exception{PyExc_SystemError} if \var{anyset} is not a \class{set},
Thomas Wouters477c8d52006-05-27 19:21:47 +00003282 \class{frozenset}, or an instance of a subtype.
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003283\end{cfuncdesc}
3284
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003285The following functions are available for instances of \class{set} or
3286its subtypes but not for instances of \class{frozenset} or its subtypes.
3287
3288\begin{cfuncdesc}{int}{PySet_Add}{PyObject *set, PyObject *key}
Georg Brandl99363b62005-09-03 07:27:26 +00003289 Add \var{key} to a \class{set} instance. Does not apply to
3290 \class{frozenset} instances. Return 0 on success or -1 on failure.
3291 Raise a \exception{TypeError} if the \var{key} is unhashable.
3292 Raise a \exception{MemoryError} if there is no room to grow.
3293 Raise a \exception{SystemError} if \var{set} is an not an instance
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003294 of \class{set} or its subtype.
3295\end{cfuncdesc}
3296
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00003297\begin{cfuncdesc}{int}{PySet_Discard}{PyObject *set, PyObject *key}
Georg Brandl99363b62005-09-03 07:27:26 +00003298 Return 1 if found and removed, 0 if not found (no action taken),
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00003299 and -1 if an error is encountered. Does not raise \exception{KeyError}
Georg Brandl99363b62005-09-03 07:27:26 +00003300 for missing keys. Raise a \exception{TypeError} if the \var{key} is
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00003301 unhashable. Unlike the Python \method{discard()} method, this function
3302 does not automatically convert unhashable sets into temporary frozensets.
Georg Brandl99363b62005-09-03 07:27:26 +00003303 Raise \exception{PyExc_SystemError} if \var{set} is an not an instance
Thomas Wouters477c8d52006-05-27 19:21:47 +00003304 of \class{set} or its subtype.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00003305\end{cfuncdesc}
3306
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003307\begin{cfuncdesc}{PyObject*}{PySet_Pop}{PyObject *set}
Georg Brandl99363b62005-09-03 07:27:26 +00003308 Return a new reference to an arbitrary object in the \var{set},
3309 and removes the object from the \var{set}. Return \NULL{} on
3310 failure. Raise \exception{KeyError} if the set is empty.
3311 Raise a \exception{SystemError} if \var{set} is an not an instance
Thomas Wouters477c8d52006-05-27 19:21:47 +00003312 of \class{set} or its subtype.
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003313\end{cfuncdesc}
3314
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003315\begin{cfuncdesc}{int}{PySet_Clear}{PyObject *set}
3316 Empty an existing set of all elements.
3317\end{cfuncdesc}