blob: afba52fbec8219367b5a83956817c93831bb614f [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.
Guido van Rossum7eaf8222007-06-18 17:58:50 +0000102 \versionadded{2.4}
Brett Cannon35d83602003-11-09 04:15:30 +0000103\end{csimplemacrodesc}
104
Fred Drake3adf79e2001-10-12 19:01:43 +0000105
106\section{Numeric Objects \label{numericObjects}}
107
108\obindex{numeric}
109
110
111\subsection{Plain Integer Objects \label{intObjects}}
112
113\obindex{integer}
114\begin{ctypedesc}{PyIntObject}
115 This subtype of \ctype{PyObject} represents a Python integer
116 object.
117\end{ctypedesc}
118
119\begin{cvardesc}{PyTypeObject}{PyInt_Type}
Tim Petersf582b822001-12-11 18:51:08 +0000120 This instance of \ctype{PyTypeObject} represents the Python plain
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000121 integer type. This is the same object as \code{int} and
122 \code{types.IntType}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000123 \withsubitem{(in modules types)}{\ttindex{IntType}}
124\end{cvardesc}
125
Andrew M. Kuchling4eb1a002004-08-07 20:19:24 +0000126\begin{cfuncdesc}{int}{PyInt_Check}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000127 Return true if \var{o} is of type \cdata{PyInt_Type} or a subtype
Fred Drake3adf79e2001-10-12 19:01:43 +0000128 of \cdata{PyInt_Type}.
129 \versionchanged[Allowed subtypes to be accepted]{2.2}
130\end{cfuncdesc}
131
Andrew M. Kuchling4eb1a002004-08-07 20:19:24 +0000132\begin{cfuncdesc}{int}{PyInt_CheckExact}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000133 Return true if \var{o} is of type \cdata{PyInt_Type}, but not a
Fred Drake3adf79e2001-10-12 19:01:43 +0000134 subtype of \cdata{PyInt_Type}.
135 \versionadded{2.2}
136\end{cfuncdesc}
137
Skip Montanaro1ff49a72003-02-03 05:13:24 +0000138\begin{cfuncdesc}{PyObject*}{PyInt_FromString}{char *str, char **pend,
139 int base}
140 Return a new \ctype{PyIntObject} or \ctype{PyLongObject} based on the
141 string value in \var{str}, which is interpreted according to the radix in
Tim Peters9ddf40b2004-06-20 22:41:32 +0000142 \var{base}. If \var{pend} is non-\NULL{}, \code{*\var{pend}} will point to
Skip Montanaro1ff49a72003-02-03 05:13:24 +0000143 the first character in \var{str} which follows the representation of the
144 number. If \var{base} is \code{0}, the radix will be determined based on
145 the leading characters of \var{str}: if \var{str} starts with \code{'0x'}
146 or \code{'0X'}, radix 16 will be used; if \var{str} starts with
147 \code{'0'}, radix 8 will be used; otherwise radix 10 will be used. If
148 \var{base} is not \code{0}, it must be between \code{2} and \code{36},
149 inclusive. Leading spaces are ignored. If there are no digits,
150 \exception{ValueError} will be raised. If the string represents a number
151 too large to be contained within the machine's \ctype{long int} type and
152 overflow warnings are being suppressed, a \ctype{PyLongObject} will be
153 returned. If overflow warnings are not being suppressed, \NULL{} will be
154 returned in this case.
155\end{cfuncdesc}
156
Fred Drake3adf79e2001-10-12 19:01:43 +0000157\begin{cfuncdesc}{PyObject*}{PyInt_FromLong}{long ival}
Georg Brandl99363b62005-09-03 07:27:26 +0000158 Create a new integer object with a value of \var{ival}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000159
160 The current implementation keeps an array of integer objects for all
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000161 integers between \code{-5} and \code{256}, when you create an int in
Fred Drake3adf79e2001-10-12 19:01:43 +0000162 that range you actually just get back a reference to the existing
163 object. So it should be possible to change the value of \code{1}. I
164 suspect the behaviour of Python in this case is undefined. :-)
165\end{cfuncdesc}
166
Martin v. Löwis3b197542006-03-01 05:47:11 +0000167\begin{cfuncdesc}{PyObject*}{PyInt_FromSsize_t}{Py_ssize_t ival}
168 Create a new integer object with a value of \var{ival}.
169 If the value exceeds \code{LONG_MAX}, a long integer object is
170 returned.
171
172 \versionadded{2.5}
173\end{cfuncdesc}
174
Fred Drake3adf79e2001-10-12 19:01:43 +0000175\begin{cfuncdesc}{long}{PyInt_AsLong}{PyObject *io}
176 Will first attempt to cast the object to a \ctype{PyIntObject}, if
Martin v. Löwis3b197542006-03-01 05:47:11 +0000177 it is not already one, and then return its value. If there is an
178 error, \code{-1} is returned, and the caller should check
179 \code{PyErr_Occurred()} to find out whether there was an error, or
180 whether the value just happened to be -1.
Fred Drake3adf79e2001-10-12 19:01:43 +0000181\end{cfuncdesc}
182
183\begin{cfuncdesc}{long}{PyInt_AS_LONG}{PyObject *io}
Georg Brandl99363b62005-09-03 07:27:26 +0000184 Return the value of the object \var{io}. No error checking is
Fred Drake3adf79e2001-10-12 19:01:43 +0000185 performed.
186\end{cfuncdesc}
187
Thomas Heller34d7f092003-04-23 19:51:05 +0000188\begin{cfuncdesc}{unsigned long}{PyInt_AsUnsignedLongMask}{PyObject *io}
189 Will first attempt to cast the object to a \ctype{PyIntObject} or
Fred Drakec22b2992003-04-23 20:38:41 +0000190 \ctype{PyLongObject}, if it is not already one, and then return its
Thomas Heller34d7f092003-04-23 19:51:05 +0000191 value as unsigned long. This function does not check for overflow.
192 \versionadded{2.3}
193\end{cfuncdesc}
194
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000195\begin{cfuncdesc}{unsigned PY_LONG_LONG}{PyInt_AsUnsignedLongLongMask}{PyObject *io}
Thomas Heller34d7f092003-04-23 19:51:05 +0000196 Will first attempt to cast the object to a \ctype{PyIntObject} or
Fred Drakec22b2992003-04-23 20:38:41 +0000197 \ctype{PyLongObject}, if it is not already one, and then return its
Thomas Heller34d7f092003-04-23 19:51:05 +0000198 value as unsigned long long, without checking for overflow.
199 \versionadded{2.3}
200\end{cfuncdesc}
201
Martin v. Löwis3b197542006-03-01 05:47:11 +0000202\begin{cfuncdesc}{Py_ssize_t}{PyInt_AsSsize_t}{PyObject *io}
203 Will first attempt to cast the object to a \ctype{PyIntObject} or
204 \ctype{PyLongObject}, if it is not already one, and then return its
205 value as \ctype{Py_ssize_t}.
206 \versionadded{2.5}
207\end{cfuncdesc}
208
Fred Drake3adf79e2001-10-12 19:01:43 +0000209\begin{cfuncdesc}{long}{PyInt_GetMax}{}
Georg Brandl99363b62005-09-03 07:27:26 +0000210 Return the system's idea of the largest integer it can handle
Fred Drake3adf79e2001-10-12 19:01:43 +0000211 (\constant{LONG_MAX}\ttindex{LONG_MAX}, as defined in the system
212 header files).
213\end{cfuncdesc}
214
Fred Drake2be406b2004-08-03 16:02:35 +0000215\subsection{Boolean Objects \label{boolObjects}}
Skip Montanaro33ee76a2004-07-28 14:17:04 +0000216
217Booleans in Python are implemented as a subclass of integers. There
218are only two booleans, \constant{Py_False} and \constant{Py_True}. As
219such, the normal creation and deletion functions don't apply to
220booleans. The following macros are available, however.
221
Andrew M. Kuchling4eb1a002004-08-07 20:19:24 +0000222\begin{cfuncdesc}{int}{PyBool_Check}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000223 Return true if \var{o} is of type \cdata{PyBool_Type}.
Skip Montanaro33ee76a2004-07-28 14:17:04 +0000224 \versionadded{2.3}
225\end{cfuncdesc}
226
Skip Montanaro6d3db702004-07-29 02:16:04 +0000227\begin{cvardesc}{PyObject*}{Py_False}
228 The Python \code{False} object. This object has no methods. It needs to
229 be treated just like any other object with respect to reference counts.
230\end{cvardesc}
Skip Montanaro33ee76a2004-07-28 14:17:04 +0000231
Skip Montanaro6d3db702004-07-29 02:16:04 +0000232\begin{cvardesc}{PyObject*}{Py_True}
233 The Python \code{True} object. This object has no methods. It needs to
234 be treated just like any other object with respect to reference counts.
235\end{cvardesc}
236
237\begin{csimplemacrodesc}{Py_RETURN_FALSE}
238 Return \constant{Py_False} from a function, properly incrementing its
239 reference count.
240\versionadded{2.4}
241\end{csimplemacrodesc}
242
243\begin{csimplemacrodesc}{Py_RETURN_TRUE}
Andrew M. Kuchling4eb1a002004-08-07 20:19:24 +0000244 Return \constant{Py_True} from a function, properly incrementing its
245 reference count.
Skip Montanaro33ee76a2004-07-28 14:17:04 +0000246\versionadded{2.4}
Skip Montanaro6d3db702004-07-29 02:16:04 +0000247\end{csimplemacrodesc}
Skip Montanaro33ee76a2004-07-28 14:17:04 +0000248
Georg Brandl99363b62005-09-03 07:27:26 +0000249\begin{cfuncdesc}{PyObject*}{PyBool_FromLong}{long v}
Thomas Wouters477c8d52006-05-27 19:21:47 +0000250 Return a new reference to \constant{Py_True} or \constant{Py_False}
Georg Brandl99363b62005-09-03 07:27:26 +0000251 depending on the truth value of \var{v}.
Skip Montanaro33ee76a2004-07-28 14:17:04 +0000252\versionadded{2.3}
253\end{cfuncdesc}
Fred Drake3adf79e2001-10-12 19:01:43 +0000254
255\subsection{Long Integer Objects \label{longObjects}}
256
257\obindex{long integer}
258\begin{ctypedesc}{PyLongObject}
259 This subtype of \ctype{PyObject} represents a Python long integer
260 object.
261\end{ctypedesc}
262
263\begin{cvardesc}{PyTypeObject}{PyLong_Type}
264 This instance of \ctype{PyTypeObject} represents the Python long
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000265 integer type. This is the same object as \code{long} and
266 \code{types.LongType}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000267 \withsubitem{(in modules types)}{\ttindex{LongType}}
268\end{cvardesc}
269
270\begin{cfuncdesc}{int}{PyLong_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +0000271 Return true if its argument is a \ctype{PyLongObject} or a subtype
Fred Drake3adf79e2001-10-12 19:01:43 +0000272 of \ctype{PyLongObject}.
273 \versionchanged[Allowed subtypes to be accepted]{2.2}
274\end{cfuncdesc}
275
276\begin{cfuncdesc}{int}{PyLong_CheckExact}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +0000277 Return true if its argument is a \ctype{PyLongObject}, but not a
Fred Drake3adf79e2001-10-12 19:01:43 +0000278 subtype of \ctype{PyLongObject}.
279 \versionadded{2.2}
280\end{cfuncdesc}
281
282\begin{cfuncdesc}{PyObject*}{PyLong_FromLong}{long v}
Georg Brandl99363b62005-09-03 07:27:26 +0000283 Return a new \ctype{PyLongObject} object from \var{v}, or \NULL{}
Fred Drake3adf79e2001-10-12 19:01:43 +0000284 on failure.
285\end{cfuncdesc}
286
287\begin{cfuncdesc}{PyObject*}{PyLong_FromUnsignedLong}{unsigned long v}
Georg Brandl99363b62005-09-03 07:27:26 +0000288 Return a new \ctype{PyLongObject} object from a C \ctype{unsigned
Fred Drake3adf79e2001-10-12 19:01:43 +0000289 long}, or \NULL{} on failure.
290\end{cfuncdesc}
291
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000292\begin{cfuncdesc}{PyObject*}{PyLong_FromLongLong}{PY_LONG_LONG v}
Georg Brandl99363b62005-09-03 07:27:26 +0000293 Return a new \ctype{PyLongObject} object from a C \ctype{long long},
Fred Drake3adf79e2001-10-12 19:01:43 +0000294 or \NULL{} on failure.
295\end{cfuncdesc}
296
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000297\begin{cfuncdesc}{PyObject*}{PyLong_FromUnsignedLongLong}{unsigned PY_LONG_LONG v}
Georg Brandl99363b62005-09-03 07:27:26 +0000298 Return a new \ctype{PyLongObject} object from a C \ctype{unsigned
Fred Drake3adf79e2001-10-12 19:01:43 +0000299 long long}, or \NULL{} on failure.
300\end{cfuncdesc}
301
302\begin{cfuncdesc}{PyObject*}{PyLong_FromDouble}{double v}
Georg Brandl99363b62005-09-03 07:27:26 +0000303 Return a new \ctype{PyLongObject} object from the integer part of
Fred Drake3adf79e2001-10-12 19:01:43 +0000304 \var{v}, or \NULL{} on failure.
305\end{cfuncdesc}
306
307\begin{cfuncdesc}{PyObject*}{PyLong_FromString}{char *str, char **pend,
308 int base}
309 Return a new \ctype{PyLongObject} based on the string value in
310 \var{str}, which is interpreted according to the radix in
Tim Peters9ddf40b2004-06-20 22:41:32 +0000311 \var{base}. If \var{pend} is non-\NULL{}, \code{*\var{pend}} will
Fred Drake3adf79e2001-10-12 19:01:43 +0000312 point to the first character in \var{str} which follows the
313 representation of the number. If \var{base} is \code{0}, the radix
Skip Montanaro1ff49a72003-02-03 05:13:24 +0000314 will be determined based on the leading characters of \var{str}: if
Fred Drake3adf79e2001-10-12 19:01:43 +0000315 \var{str} starts with \code{'0x'} or \code{'0X'}, radix 16 will be
316 used; if \var{str} starts with \code{'0'}, radix 8 will be used;
317 otherwise radix 10 will be used. If \var{base} is not \code{0}, it
318 must be between \code{2} and \code{36}, inclusive. Leading spaces
319 are ignored. If there are no digits, \exception{ValueError} will be
320 raised.
321\end{cfuncdesc}
322
323\begin{cfuncdesc}{PyObject*}{PyLong_FromUnicode}{Py_UNICODE *u,
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000324 Py_ssize_t length, int base}
Fred Drake3adf79e2001-10-12 19:01:43 +0000325 Convert a sequence of Unicode digits to a Python long integer
326 value. The first parameter, \var{u}, points to the first character
327 of the Unicode string, \var{length} gives the number of characters,
328 and \var{base} is the radix for the conversion. The radix must be
329 in the range [2, 36]; if it is out of range, \exception{ValueError}
330 will be raised.
331 \versionadded{1.6}
332\end{cfuncdesc}
333
334\begin{cfuncdesc}{PyObject*}{PyLong_FromVoidPtr}{void *p}
335 Create a Python integer or long integer from the pointer \var{p}.
336 The pointer value can be retrieved from the resulting value using
337 \cfunction{PyLong_AsVoidPtr()}.
338 \versionadded{1.5.2}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000339 \versionchanged[If the integer is larger than LONG_MAX,
340 a positive long integer is returned]{2.5}
341 \end{cfuncdesc}
Fred Drake3adf79e2001-10-12 19:01:43 +0000342
343\begin{cfuncdesc}{long}{PyLong_AsLong}{PyObject *pylong}
Georg Brandl99363b62005-09-03 07:27:26 +0000344 Return a C \ctype{long} representation of the contents of
Fred Drake3adf79e2001-10-12 19:01:43 +0000345 \var{pylong}. If \var{pylong} is greater than
346 \constant{LONG_MAX}\ttindex{LONG_MAX}, an \exception{OverflowError}
347 is raised.
348 \withsubitem{(built-in exception)}{\ttindex{OverflowError}}
349\end{cfuncdesc}
350
351\begin{cfuncdesc}{unsigned long}{PyLong_AsUnsignedLong}{PyObject *pylong}
Georg Brandl99363b62005-09-03 07:27:26 +0000352 Return a C \ctype{unsigned long} representation of the contents of
Fred Drake3adf79e2001-10-12 19:01:43 +0000353 \var{pylong}. If \var{pylong} is greater than
354 \constant{ULONG_MAX}\ttindex{ULONG_MAX}, an
355 \exception{OverflowError} is raised.
Tim Petersf582b822001-12-11 18:51:08 +0000356 \withsubitem{(built-in exception)}{\ttindex{OverflowError}}
Fred Drake3adf79e2001-10-12 19:01:43 +0000357\end{cfuncdesc}
358
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000359\begin{cfuncdesc}{PY_LONG_LONG}{PyLong_AsLongLong}{PyObject *pylong}
Fred Drake3adf79e2001-10-12 19:01:43 +0000360 Return a C \ctype{long long} from a Python long integer. If
361 \var{pylong} cannot be represented as a \ctype{long long}, an
362 \exception{OverflowError} will be raised.
363 \versionadded{2.2}
364\end{cfuncdesc}
365
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000366\begin{cfuncdesc}{unsigned PY_LONG_LONG}{PyLong_AsUnsignedLongLong}{PyObject
Fred Drake3adf79e2001-10-12 19:01:43 +0000367 *pylong}
368 Return a C \ctype{unsigned long long} from a Python long integer.
369 If \var{pylong} cannot be represented as an \ctype{unsigned long
370 long}, an \exception{OverflowError} will be raised if the value is
371 positive, or a \exception{TypeError} will be raised if the value is
372 negative.
373 \versionadded{2.2}
374\end{cfuncdesc}
375
Thomas Heller34d7f092003-04-23 19:51:05 +0000376\begin{cfuncdesc}{unsigned long}{PyLong_AsUnsignedLongMask}{PyObject *io}
377 Return a C \ctype{unsigned long} from a Python long integer, without
378 checking for overflow.
379 \versionadded{2.3}
380\end{cfuncdesc}
381
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000382\begin{cfuncdesc}{unsigned PY_LONG_LONG}{PyLong_AsUnsignedLongLongMask}{PyObject *io}
Thomas Heller34d7f092003-04-23 19:51:05 +0000383 Return a C \ctype{unsigned long long} from a Python long integer, without
384 checking for overflow.
385 \versionadded{2.3}
386\end{cfuncdesc}
387
Fred Drake3adf79e2001-10-12 19:01:43 +0000388\begin{cfuncdesc}{double}{PyLong_AsDouble}{PyObject *pylong}
Georg Brandl99363b62005-09-03 07:27:26 +0000389 Return a C \ctype{double} representation of the contents of
Fred Drake3adf79e2001-10-12 19:01:43 +0000390 \var{pylong}. If \var{pylong} cannot be approximately represented
391 as a \ctype{double}, an \exception{OverflowError} exception is
392 raised and \code{-1.0} will be returned.
393\end{cfuncdesc}
394
395\begin{cfuncdesc}{void*}{PyLong_AsVoidPtr}{PyObject *pylong}
396 Convert a Python integer or long integer \var{pylong} to a C
397 \ctype{void} pointer. If \var{pylong} cannot be converted, an
398 \exception{OverflowError} will be raised. This is only assured to
399 produce a usable \ctype{void} pointer for values created with
400 \cfunction{PyLong_FromVoidPtr()}.
401 \versionadded{1.5.2}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000402 \versionchanged[For values outside 0..LONG_MAX, both signed and
403 unsigned integers are acccepted]{2.5}
Fred Drake3adf79e2001-10-12 19:01:43 +0000404\end{cfuncdesc}
405
406
407\subsection{Floating Point Objects \label{floatObjects}}
408
409\obindex{floating point}
410\begin{ctypedesc}{PyFloatObject}
411 This subtype of \ctype{PyObject} represents a Python floating point
412 object.
413\end{ctypedesc}
414
415\begin{cvardesc}{PyTypeObject}{PyFloat_Type}
416 This instance of \ctype{PyTypeObject} represents the Python floating
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000417 point type. This is the same object as \code{float} and
418 \code{types.FloatType}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000419 \withsubitem{(in modules types)}{\ttindex{FloatType}}
420\end{cvardesc}
421
422\begin{cfuncdesc}{int}{PyFloat_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +0000423 Return true if its argument is a \ctype{PyFloatObject} or a subtype
Fred Drake3adf79e2001-10-12 19:01:43 +0000424 of \ctype{PyFloatObject}.
425 \versionchanged[Allowed subtypes to be accepted]{2.2}
426\end{cfuncdesc}
427
428\begin{cfuncdesc}{int}{PyFloat_CheckExact}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +0000429 Return true if its argument is a \ctype{PyFloatObject}, but not a
Fred Drake3adf79e2001-10-12 19:01:43 +0000430 subtype of \ctype{PyFloatObject}.
431 \versionadded{2.2}
432\end{cfuncdesc}
433
Georg Brandl428f0642007-03-18 18:35:15 +0000434\begin{cfuncdesc}{PyObject*}{PyFloat_FromString}{PyObject *str}
Georg Brandl99363b62005-09-03 07:27:26 +0000435 Create a \ctype{PyFloatObject} object based on the string value in
Georg Brandl428f0642007-03-18 18:35:15 +0000436 \var{str}, or \NULL{} on failure.
Skip Montanaroae31e9b2003-02-03 03:56:36 +0000437\end{cfuncdesc}
438
Fred Drake3adf79e2001-10-12 19:01:43 +0000439\begin{cfuncdesc}{PyObject*}{PyFloat_FromDouble}{double v}
Georg Brandl99363b62005-09-03 07:27:26 +0000440 Create a \ctype{PyFloatObject} object from \var{v}, or \NULL{} on
Fred Drake3adf79e2001-10-12 19:01:43 +0000441 failure.
442\end{cfuncdesc}
443
444\begin{cfuncdesc}{double}{PyFloat_AsDouble}{PyObject *pyfloat}
Georg Brandl99363b62005-09-03 07:27:26 +0000445 Return a C \ctype{double} representation of the contents of
Guido van Rossumd8faa362007-04-27 19:54:29 +0000446 \var{pyfloat}. If \var{pyfloat} is not a Python floating point
447 object but has a \method{__float__} method, this method will first
448 be called to convert \var{pyfloat} into a float.
Fred Drake3adf79e2001-10-12 19:01:43 +0000449\end{cfuncdesc}
450
451\begin{cfuncdesc}{double}{PyFloat_AS_DOUBLE}{PyObject *pyfloat}
Georg Brandl99363b62005-09-03 07:27:26 +0000452 Return a C \ctype{double} representation of the contents of
Fred Drake3adf79e2001-10-12 19:01:43 +0000453 \var{pyfloat}, but without error checking.
454\end{cfuncdesc}
455
456
457\subsection{Complex Number Objects \label{complexObjects}}
458
459\obindex{complex number}
460Python's complex number objects are implemented as two distinct types
461when viewed from the C API: one is the Python object exposed to
462Python programs, and the other is a C structure which represents the
463actual complex number value. The API provides functions for working
464with both.
465
466\subsubsection{Complex Numbers as C Structures}
467
468Note that the functions which accept these structures as parameters
469and return them as results do so \emph{by value} rather than
470dereferencing them through pointers. This is consistent throughout
471the API.
472
473\begin{ctypedesc}{Py_complex}
474 The C structure which corresponds to the value portion of a Python
475 complex number object. Most of the functions for dealing with
476 complex number objects use structures of this type as input or
477 output values, as appropriate. It is defined as:
478
479\begin{verbatim}
480typedef struct {
481 double real;
482 double imag;
483} Py_complex;
484\end{verbatim}
485\end{ctypedesc}
486
487\begin{cfuncdesc}{Py_complex}{_Py_c_sum}{Py_complex left, Py_complex right}
488 Return the sum of two complex numbers, using the C
489 \ctype{Py_complex} representation.
490\end{cfuncdesc}
491
492\begin{cfuncdesc}{Py_complex}{_Py_c_diff}{Py_complex left, Py_complex right}
493 Return the difference between two complex numbers, using the C
494 \ctype{Py_complex} representation.
495\end{cfuncdesc}
496
497\begin{cfuncdesc}{Py_complex}{_Py_c_neg}{Py_complex complex}
498 Return the negation of the complex number \var{complex}, using the C
499 \ctype{Py_complex} representation.
500\end{cfuncdesc}
501
502\begin{cfuncdesc}{Py_complex}{_Py_c_prod}{Py_complex left, Py_complex right}
503 Return the product of two complex numbers, using the C
504 \ctype{Py_complex} representation.
505\end{cfuncdesc}
506
507\begin{cfuncdesc}{Py_complex}{_Py_c_quot}{Py_complex dividend,
508 Py_complex divisor}
509 Return the quotient of two complex numbers, using the C
510 \ctype{Py_complex} representation.
511\end{cfuncdesc}
512
513\begin{cfuncdesc}{Py_complex}{_Py_c_pow}{Py_complex num, Py_complex exp}
514 Return the exponentiation of \var{num} by \var{exp}, using the C
515 \ctype{Py_complex} representation.
516\end{cfuncdesc}
517
518
519\subsubsection{Complex Numbers as Python Objects}
520
521\begin{ctypedesc}{PyComplexObject}
522 This subtype of \ctype{PyObject} represents a Python complex number
523 object.
524\end{ctypedesc}
525
526\begin{cvardesc}{PyTypeObject}{PyComplex_Type}
527 This instance of \ctype{PyTypeObject} represents the Python complex
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000528 number type. It is the same object as \code{complex} and
529 \code{types.ComplexType}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000530\end{cvardesc}
531
532\begin{cfuncdesc}{int}{PyComplex_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +0000533 Return true if its argument is a \ctype{PyComplexObject} or a
Fred Drake3adf79e2001-10-12 19:01:43 +0000534 subtype of \ctype{PyComplexObject}.
535 \versionchanged[Allowed subtypes to be accepted]{2.2}
536\end{cfuncdesc}
537
538\begin{cfuncdesc}{int}{PyComplex_CheckExact}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +0000539 Return true if its argument is a \ctype{PyComplexObject}, but not a
Fred Drake3adf79e2001-10-12 19:01:43 +0000540 subtype of \ctype{PyComplexObject}.
541 \versionadded{2.2}
542\end{cfuncdesc}
543
544\begin{cfuncdesc}{PyObject*}{PyComplex_FromCComplex}{Py_complex v}
545 Create a new Python complex number object from a C
546 \ctype{Py_complex} value.
547\end{cfuncdesc}
548
549\begin{cfuncdesc}{PyObject*}{PyComplex_FromDoubles}{double real, double imag}
Georg Brandl99363b62005-09-03 07:27:26 +0000550 Return a new \ctype{PyComplexObject} object from \var{real} and
Fred Drake3adf79e2001-10-12 19:01:43 +0000551 \var{imag}.
552\end{cfuncdesc}
553
554\begin{cfuncdesc}{double}{PyComplex_RealAsDouble}{PyObject *op}
Georg Brandl99363b62005-09-03 07:27:26 +0000555 Return the real part of \var{op} as a C \ctype{double}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000556\end{cfuncdesc}
557
558\begin{cfuncdesc}{double}{PyComplex_ImagAsDouble}{PyObject *op}
Georg Brandl99363b62005-09-03 07:27:26 +0000559 Return the imaginary part of \var{op} as a C \ctype{double}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000560\end{cfuncdesc}
561
562\begin{cfuncdesc}{Py_complex}{PyComplex_AsCComplex}{PyObject *op}
Guido van Rossumd8faa362007-04-27 19:54:29 +0000563 Return the \ctype{Py_complex} value of the complex number \var{op}.
564 \versionchanged[If \var{op} is not a Python complex number object
565 but has a \method{__complex__} method, this method
566 will first be called to convert \var{op} to a Python
567 complex number object]{2.6}
Fred Drake3adf79e2001-10-12 19:01:43 +0000568\end{cfuncdesc}
569
570
571
572\section{Sequence Objects \label{sequenceObjects}}
573
574\obindex{sequence}
Tim Petersf582b822001-12-11 18:51:08 +0000575Generic operations on sequence objects were discussed in the previous
576chapter; this section deals with the specific kinds of sequence
Fred Drake3adf79e2001-10-12 19:01:43 +0000577objects that are intrinsic to the Python language.
578
579
580\subsection{String Objects \label{stringObjects}}
581
582These functions raise \exception{TypeError} when expecting a string
583parameter and are called with a non-string parameter.
584
585\obindex{string}
586\begin{ctypedesc}{PyStringObject}
587 This subtype of \ctype{PyObject} represents a Python string object.
588\end{ctypedesc}
589
590\begin{cvardesc}{PyTypeObject}{PyString_Type}
591 This instance of \ctype{PyTypeObject} represents the Python string
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000592 type; it is the same object as \code{str} and \code{types.StringType}
593 in the Python layer.
Fred Drake3adf79e2001-10-12 19:01:43 +0000594 \withsubitem{(in module types)}{\ttindex{StringType}}.
595\end{cvardesc}
596
597\begin{cfuncdesc}{int}{PyString_Check}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000598 Return true if the object \var{o} is a string object or an instance
Fred Drake3adf79e2001-10-12 19:01:43 +0000599 of a subtype of the string type.
600 \versionchanged[Allowed subtypes to be accepted]{2.2}
601\end{cfuncdesc}
602
603\begin{cfuncdesc}{int}{PyString_CheckExact}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000604 Return true if the object \var{o} is a string object, but not an
Fred Drake3adf79e2001-10-12 19:01:43 +0000605 instance of a subtype of the string type.
606 \versionadded{2.2}
607\end{cfuncdesc}
608
609\begin{cfuncdesc}{PyObject*}{PyString_FromString}{const char *v}
Thomas Wouters89f507f2006-12-13 04:49:30 +0000610 Return a new string object with a copy of the string \var{v} as value
611 on success, and \NULL{} on failure. The parameter \var{v} must not be
612 \NULL{}; it will not be checked.
Fred Drake3adf79e2001-10-12 19:01:43 +0000613\end{cfuncdesc}
614
615\begin{cfuncdesc}{PyObject*}{PyString_FromStringAndSize}{const char *v,
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000616 Py_ssize_t len}
Thomas Wouters89f507f2006-12-13 04:49:30 +0000617 Return a new string object with a copy of the string \var{v} as value
618 and length \var{len} on success, and \NULL{} on failure. If \var{v} is
Tim Peters9ddf40b2004-06-20 22:41:32 +0000619 \NULL{}, the contents of the string are uninitialized.
Fred Drake3adf79e2001-10-12 19:01:43 +0000620\end{cfuncdesc}
621
622\begin{cfuncdesc}{PyObject*}{PyString_FromFormat}{const char *format, ...}
Georg Brandl99363b62005-09-03 07:27:26 +0000623 Take a C \cfunction{printf()}-style \var{format} string and a
624 variable number of arguments, calculate the size of the resulting
625 Python string and return a string with the values formatted into
Fred Drake3adf79e2001-10-12 19:01:43 +0000626 it. The variable arguments must be C types and must correspond
627 exactly to the format characters in the \var{format} string. The
628 following format characters are allowed:
629
Thomas Wouters477c8d52006-05-27 19:21:47 +0000630 % This should be exactly the same as the table in PyErr_Format.
631 % One should just refer to the other.
632
633 % The descriptions for %zd and %zu are wrong, but the truth is complicated
634 % because not all compilers support the %z width modifier -- we fake it
635 % when necessary via interpolating PY_FORMAT_SIZE_T.
636
637 % %u, %lu, %zu should have "new in Python 2.5" blurbs.
638
Fred Drake3adf79e2001-10-12 19:01:43 +0000639 \begin{tableiii}{l|l|l}{member}{Format Characters}{Type}{Comment}
640 \lineiii{\%\%}{\emph{n/a}}{The literal \% character.}
641 \lineiii{\%c}{int}{A single character, represented as an C int.}
642 \lineiii{\%d}{int}{Exactly equivalent to \code{printf("\%d")}.}
Thomas Wouters477c8d52006-05-27 19:21:47 +0000643 \lineiii{\%u}{unsigned int}{Exactly equivalent to \code{printf("\%u")}.}
Fred Drake3adf79e2001-10-12 19:01:43 +0000644 \lineiii{\%ld}{long}{Exactly equivalent to \code{printf("\%ld")}.}
Thomas Wouters477c8d52006-05-27 19:21:47 +0000645 \lineiii{\%lu}{unsigned long}{Exactly equivalent to \code{printf("\%lu")}.}
646 \lineiii{\%zd}{Py_ssize_t}{Exactly equivalent to \code{printf("\%zd")}.}
647 \lineiii{\%zu}{size_t}{Exactly equivalent to \code{printf("\%zu")}.}
Fred Drake3adf79e2001-10-12 19:01:43 +0000648 \lineiii{\%i}{int}{Exactly equivalent to \code{printf("\%i")}.}
649 \lineiii{\%x}{int}{Exactly equivalent to \code{printf("\%x")}.}
650 \lineiii{\%s}{char*}{A null-terminated C character array.}
651 \lineiii{\%p}{void*}{The hex representation of a C pointer.
652 Mostly equivalent to \code{printf("\%p")} except that it is
653 guaranteed to start with the literal \code{0x} regardless of
654 what the platform's \code{printf} yields.}
655 \end{tableiii}
Thomas Wouters477c8d52006-05-27 19:21:47 +0000656
657 An unrecognized format character causes all the rest of the format
658 string to be copied as-is to the result string, and any extra
659 arguments discarded.
Fred Drake3adf79e2001-10-12 19:01:43 +0000660\end{cfuncdesc}
661
662\begin{cfuncdesc}{PyObject*}{PyString_FromFormatV}{const char *format,
663 va_list vargs}
664 Identical to \function{PyString_FromFormat()} except that it takes
665 exactly two arguments.
666\end{cfuncdesc}
667
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000668\begin{cfuncdesc}{Py_ssize_t}{PyString_Size}{PyObject *string}
Georg Brandl99363b62005-09-03 07:27:26 +0000669 Return the length of the string in string object \var{string}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000670\end{cfuncdesc}
671
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000672\begin{cfuncdesc}{Py_ssize_t}{PyString_GET_SIZE}{PyObject *string}
Fred Drake3adf79e2001-10-12 19:01:43 +0000673 Macro form of \cfunction{PyString_Size()} but without error
674 checking.
675\end{cfuncdesc}
676
677\begin{cfuncdesc}{char*}{PyString_AsString}{PyObject *string}
Georg Brandl99363b62005-09-03 07:27:26 +0000678 Return a NUL-terminated representation of the contents of
Fred Drake3adf79e2001-10-12 19:01:43 +0000679 \var{string}. The pointer refers to the internal buffer of
680 \var{string}, not a copy. The data must not be modified in any way,
681 unless the string was just created using
682 \code{PyString_FromStringAndSize(NULL, \var{size})}.
Fred Drake4b247262002-10-22 20:20:20 +0000683 It must not be deallocated. If \var{string} is a Unicode object,
684 this function computes the default encoding of \var{string} and
685 operates on that. If \var{string} is not a string object at all,
686 \cfunction{PyString_AsString()} returns \NULL{} and raises
687 \exception{TypeError}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000688\end{cfuncdesc}
689
690\begin{cfuncdesc}{char*}{PyString_AS_STRING}{PyObject *string}
691 Macro form of \cfunction{PyString_AsString()} but without error
Fred Drake4b247262002-10-22 20:20:20 +0000692 checking. Only string objects are supported; no Unicode objects
693 should be passed.
Fred Drake3adf79e2001-10-12 19:01:43 +0000694\end{cfuncdesc}
695
696\begin{cfuncdesc}{int}{PyString_AsStringAndSize}{PyObject *obj,
697 char **buffer,
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000698 Py_ssize_t *length}
Georg Brandl99363b62005-09-03 07:27:26 +0000699 Return a NUL-terminated representation of the contents of the
Fred Drake3adf79e2001-10-12 19:01:43 +0000700 object \var{obj} through the output variables \var{buffer} and
701 \var{length}.
702
703 The function accepts both string and Unicode objects as input. For
704 Unicode objects it returns the default encoded version of the
Tim Peters9ddf40b2004-06-20 22:41:32 +0000705 object. If \var{length} is \NULL{}, the resulting buffer may not
Fred Drake4b247262002-10-22 20:20:20 +0000706 contain NUL characters; if it does, the function returns \code{-1}
707 and a \exception{TypeError} is raised.
Fred Drake3adf79e2001-10-12 19:01:43 +0000708
709 The buffer refers to an internal string buffer of \var{obj}, not a
710 copy. The data must not be modified in any way, unless the string
711 was just created using \code{PyString_FromStringAndSize(NULL,
Fred Drake4b247262002-10-22 20:20:20 +0000712 \var{size})}. It must not be deallocated. If \var{string} is a
713 Unicode object, this function computes the default encoding of
714 \var{string} and operates on that. If \var{string} is not a string
Thomas Wouters477c8d52006-05-27 19:21:47 +0000715 object at all, \cfunction{PyString_AsStringAndSize()} returns
Georg Brandle53475d2005-09-28 12:53:12 +0000716 \code{-1} and raises \exception{TypeError}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000717\end{cfuncdesc}
718
719\begin{cfuncdesc}{void}{PyString_Concat}{PyObject **string,
720 PyObject *newpart}
Georg Brandl99363b62005-09-03 07:27:26 +0000721 Create a new string object in \var{*string} containing the contents
Fred Drake3adf79e2001-10-12 19:01:43 +0000722 of \var{newpart} appended to \var{string}; the caller will own the
723 new reference. The reference to the old value of \var{string} will
724 be stolen. If the new string cannot be created, the old reference
725 to \var{string} will still be discarded and the value of
Tim Peters9ddf40b2004-06-20 22:41:32 +0000726 \var{*string} will be set to \NULL{}; the appropriate exception will
Fred Drake3adf79e2001-10-12 19:01:43 +0000727 be set.
728\end{cfuncdesc}
729
730\begin{cfuncdesc}{void}{PyString_ConcatAndDel}{PyObject **string,
731 PyObject *newpart}
Georg Brandl99363b62005-09-03 07:27:26 +0000732 Create a new string object in \var{*string} containing the contents
Fred Drake3adf79e2001-10-12 19:01:43 +0000733 of \var{newpart} appended to \var{string}. This version decrements
734 the reference count of \var{newpart}.
735\end{cfuncdesc}
736
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000737\begin{cfuncdesc}{int}{_PyString_Resize}{PyObject **string, Py_ssize_t newsize}
Fred Drake3adf79e2001-10-12 19:01:43 +0000738 A way to resize a string object even though it is ``immutable''.
739 Only use this to build up a brand new string object; don't use this
Tim Peters5de98422002-04-27 18:44:32 +0000740 if the string may already be known in other parts of the code. It
741 is an error to call this function if the refcount on the input string
742 object is not one.
743 Pass the address of an existing string object as an lvalue (it may
744 be written into), and the new size desired. On success, \var{*string}
Fred Drake432425e2002-04-29 15:17:16 +0000745 holds the resized string object and \code{0} is returned; the address in
Tim Peters5de98422002-04-27 18:44:32 +0000746 \var{*string} may differ from its input value. If the
747 reallocation fails, the original string object at \var{*string} is
748 deallocated, \var{*string} is set to \NULL{}, a memory exception is set,
Fred Drake432425e2002-04-29 15:17:16 +0000749 and \code{-1} is returned.
Fred Drake3adf79e2001-10-12 19:01:43 +0000750\end{cfuncdesc}
751
752\begin{cfuncdesc}{PyObject*}{PyString_Format}{PyObject *format,
753 PyObject *args}
Georg Brandl99363b62005-09-03 07:27:26 +0000754 Return a new string object from \var{format} and \var{args}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000755 Analogous to \code{\var{format} \%\ \var{args}}. The \var{args}
756 argument must be a tuple.
757\end{cfuncdesc}
758
759\begin{cfuncdesc}{void}{PyString_InternInPlace}{PyObject **string}
760 Intern the argument \var{*string} in place. The argument must be
761 the address of a pointer variable pointing to a Python string
762 object. If there is an existing interned string that is the same as
763 \var{*string}, it sets \var{*string} to it (decrementing the
764 reference count of the old string object and incrementing the
765 reference count of the interned string object), otherwise it leaves
766 \var{*string} alone and interns it (incrementing its reference
767 count). (Clarification: even though there is a lot of talk about
768 reference counts, think of this function as reference-count-neutral;
769 you own the object after the call if and only if you owned it before
770 the call.)
771\end{cfuncdesc}
772
773\begin{cfuncdesc}{PyObject*}{PyString_InternFromString}{const char *v}
774 A combination of \cfunction{PyString_FromString()} and
775 \cfunction{PyString_InternInPlace()}, returning either a new string
776 object that has been interned, or a new (``owned'') reference to an
777 earlier interned string object with the same value.
778\end{cfuncdesc}
779
780\begin{cfuncdesc}{PyObject*}{PyString_Decode}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000781 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +0000782 const char *encoding,
783 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +0000784 Create an object by decoding \var{size} bytes of the encoded
Fred Drake3adf79e2001-10-12 19:01:43 +0000785 buffer \var{s} using the codec registered for
786 \var{encoding}. \var{encoding} and \var{errors} have the same
787 meaning as the parameters of the same name in the
788 \function{unicode()} built-in function. The codec to be used is
Georg Brandl99363b62005-09-03 07:27:26 +0000789 looked up using the Python codec registry. Return \NULL{} if
Fred Drake3adf79e2001-10-12 19:01:43 +0000790 an exception was raised by the codec.
791\end{cfuncdesc}
792
793\begin{cfuncdesc}{PyObject*}{PyString_AsDecodedObject}{PyObject *str,
794 const char *encoding,
795 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +0000796 Decode a string object by passing it to the codec registered for
797 \var{encoding} and return the result as Python
Fred Drake3adf79e2001-10-12 19:01:43 +0000798 object. \var{encoding} and \var{errors} have the same meaning as the
799 parameters of the same name in the string \method{encode()} method.
800 The codec to be used is looked up using the Python codec registry.
Georg Brandl99363b62005-09-03 07:27:26 +0000801 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +0000802\end{cfuncdesc}
803
804\begin{cfuncdesc}{PyObject*}{PyString_Encode}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000805 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +0000806 const char *encoding,
807 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +0000808 Encode the \ctype{char} buffer of the given size by passing it to
809 the codec registered for \var{encoding} and return a Python object.
Fred Drake3adf79e2001-10-12 19:01:43 +0000810 \var{encoding} and \var{errors} have the same meaning as the
811 parameters of the same name in the string \method{encode()} method.
812 The codec to be used is looked up using the Python codec
Georg Brandl99363b62005-09-03 07:27:26 +0000813 registry. Return \NULL{} if an exception was raised by the
Fred Drake3adf79e2001-10-12 19:01:43 +0000814 codec.
815\end{cfuncdesc}
816
817\begin{cfuncdesc}{PyObject*}{PyString_AsEncodedObject}{PyObject *str,
818 const char *encoding,
819 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +0000820 Encode a string object using the codec registered for
821 \var{encoding} and return the result as Python object.
Fred Drake3adf79e2001-10-12 19:01:43 +0000822 \var{encoding} and \var{errors} have the same meaning as the
823 parameters of the same name in the string \method{encode()} method.
824 The codec to be used is looked up using the Python codec registry.
Georg Brandl99363b62005-09-03 07:27:26 +0000825 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +0000826\end{cfuncdesc}
827
828
829\subsection{Unicode Objects \label{unicodeObjects}}
830\sectionauthor{Marc-Andre Lemburg}{mal@lemburg.com}
831
832%--- Unicode Type -------------------------------------------------------
833
834These are the basic Unicode object types used for the Unicode
835implementation in Python:
836
837\begin{ctypedesc}{Py_UNICODE}
Marc-André Lemburgdf4f6e92005-10-10 19:08:41 +0000838 This type represents the storage type which is used by Python
839 internally as basis for holding Unicode ordinals. Python's default
840 builds use a 16-bit type for \ctype{Py_UNICODE} and store Unicode
841 values internally as UCS2. It is also possible to build a UCS4
842 version of Python (most recent Linux distributions come with UCS4
843 builds of Python). These builds then use a 32-bit type for
844 \ctype{Py_UNICODE} and store Unicode data internally as UCS4. On
845 platforms where \ctype{wchar_t} is available and compatible with the
846 chosen Python Unicode build variant, \ctype{Py_UNICODE} is a typedef
847 alias for \ctype{wchar_t} to enhance native platform compatibility.
848 On all other platforms, \ctype{Py_UNICODE} is a typedef alias for
849 either \ctype{unsigned short} (UCS2) or \ctype{unsigned long}
850 (UCS4).
Fred Drake3adf79e2001-10-12 19:01:43 +0000851\end{ctypedesc}
852
Marc-André Lemburgdf4f6e92005-10-10 19:08:41 +0000853Note that UCS2 and UCS4 Python builds are not binary compatible.
854Please keep this in mind when writing extensions or interfaces.
855
Fred Drake3adf79e2001-10-12 19:01:43 +0000856\begin{ctypedesc}{PyUnicodeObject}
857 This subtype of \ctype{PyObject} represents a Python Unicode object.
858\end{ctypedesc}
859
860\begin{cvardesc}{PyTypeObject}{PyUnicode_Type}
861 This instance of \ctype{PyTypeObject} represents the Python Unicode
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000862 type. It is exposed to Python code as \code{unicode} and
863 \code{types.UnicodeType}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000864\end{cvardesc}
865
866The following APIs are really C macros and can be used to do fast
867checks and to access internal read-only data of Unicode objects:
868
869\begin{cfuncdesc}{int}{PyUnicode_Check}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000870 Return true if the object \var{o} is a Unicode object or an
Fred Drake3adf79e2001-10-12 19:01:43 +0000871 instance of a Unicode subtype.
872 \versionchanged[Allowed subtypes to be accepted]{2.2}
873\end{cfuncdesc}
874
875\begin{cfuncdesc}{int}{PyUnicode_CheckExact}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000876 Return true if the object \var{o} is a Unicode object, but not an
Fred Drake3adf79e2001-10-12 19:01:43 +0000877 instance of a subtype.
878 \versionadded{2.2}
879\end{cfuncdesc}
880
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000881\begin{cfuncdesc}{Py_ssize_t}{PyUnicode_GET_SIZE}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000882 Return the size of the object. \var{o} has to be a
Fred Drake3adf79e2001-10-12 19:01:43 +0000883 \ctype{PyUnicodeObject} (not checked).
884\end{cfuncdesc}
885
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000886\begin{cfuncdesc}{Py_ssize_t}{PyUnicode_GET_DATA_SIZE}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000887 Return the size of the object's internal buffer in bytes. \var{o}
Fred Drake3adf79e2001-10-12 19:01:43 +0000888 has to be a \ctype{PyUnicodeObject} (not checked).
889\end{cfuncdesc}
890
891\begin{cfuncdesc}{Py_UNICODE*}{PyUnicode_AS_UNICODE}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000892 Return a pointer to the internal \ctype{Py_UNICODE} buffer of the
Fred Drake3adf79e2001-10-12 19:01:43 +0000893 object. \var{o} has to be a \ctype{PyUnicodeObject} (not checked).
894\end{cfuncdesc}
895
896\begin{cfuncdesc}{const char*}{PyUnicode_AS_DATA}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000897 Return a pointer to the internal buffer of the object.
Fred Drake3adf79e2001-10-12 19:01:43 +0000898 \var{o} has to be a \ctype{PyUnicodeObject} (not checked).
899\end{cfuncdesc}
900
901% --- Unicode character properties ---------------------------------------
902
903Unicode provides many different character properties. The most often
904needed ones are available through these macros which are mapped to C
905functions depending on the Python configuration.
906
907\begin{cfuncdesc}{int}{Py_UNICODE_ISSPACE}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000908 Return 1 or 0 depending on whether \var{ch} is a whitespace
Fred Drake3adf79e2001-10-12 19:01:43 +0000909 character.
910\end{cfuncdesc}
911
912\begin{cfuncdesc}{int}{Py_UNICODE_ISLOWER}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000913 Return 1 or 0 depending on whether \var{ch} is a lowercase character.
Fred Drake3adf79e2001-10-12 19:01:43 +0000914\end{cfuncdesc}
915
916\begin{cfuncdesc}{int}{Py_UNICODE_ISUPPER}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000917 Return 1 or 0 depending on whether \var{ch} is an uppercase
Fred Drake3adf79e2001-10-12 19:01:43 +0000918 character.
919\end{cfuncdesc}
920
921\begin{cfuncdesc}{int}{Py_UNICODE_ISTITLE}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000922 Return 1 or 0 depending on whether \var{ch} is a titlecase character.
Fred Drake3adf79e2001-10-12 19:01:43 +0000923\end{cfuncdesc}
924
925\begin{cfuncdesc}{int}{Py_UNICODE_ISLINEBREAK}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000926 Return 1 or 0 depending on whether \var{ch} is a linebreak character.
Fred Drake3adf79e2001-10-12 19:01:43 +0000927\end{cfuncdesc}
928
929\begin{cfuncdesc}{int}{Py_UNICODE_ISDECIMAL}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000930 Return 1 or 0 depending on whether \var{ch} is a decimal character.
Fred Drake3adf79e2001-10-12 19:01:43 +0000931\end{cfuncdesc}
932
933\begin{cfuncdesc}{int}{Py_UNICODE_ISDIGIT}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000934 Return 1 or 0 depending on whether \var{ch} is a digit character.
Fred Drake3adf79e2001-10-12 19:01:43 +0000935\end{cfuncdesc}
936
937\begin{cfuncdesc}{int}{Py_UNICODE_ISNUMERIC}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000938 Return 1 or 0 depending on whether \var{ch} is a numeric character.
Fred Drake3adf79e2001-10-12 19:01:43 +0000939\end{cfuncdesc}
940
941\begin{cfuncdesc}{int}{Py_UNICODE_ISALPHA}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000942 Return 1 or 0 depending on whether \var{ch} is an alphabetic
Fred Drake3adf79e2001-10-12 19:01:43 +0000943 character.
944\end{cfuncdesc}
945
946\begin{cfuncdesc}{int}{Py_UNICODE_ISALNUM}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000947 Return 1 or 0 depending on whether \var{ch} is an alphanumeric
Fred Drake3adf79e2001-10-12 19:01:43 +0000948 character.
949\end{cfuncdesc}
950
951These APIs can be used for fast direct character conversions:
952
953\begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOLOWER}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000954 Return the character \var{ch} converted to lower case.
Fred Drake3adf79e2001-10-12 19:01:43 +0000955\end{cfuncdesc}
956
957\begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOUPPER}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000958 Return the character \var{ch} converted to upper case.
Fred Drake3adf79e2001-10-12 19:01:43 +0000959\end{cfuncdesc}
960
961\begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOTITLE}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000962 Return the character \var{ch} converted to title case.
Fred Drake3adf79e2001-10-12 19:01:43 +0000963\end{cfuncdesc}
964
965\begin{cfuncdesc}{int}{Py_UNICODE_TODECIMAL}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000966 Return the character \var{ch} converted to a decimal positive
967 integer. Return \code{-1} if this is not possible. This macro
968 does not raise exceptions.
Fred Drake3adf79e2001-10-12 19:01:43 +0000969\end{cfuncdesc}
970
971\begin{cfuncdesc}{int}{Py_UNICODE_TODIGIT}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000972 Return the character \var{ch} converted to a single digit integer.
973 Return \code{-1} if this is not possible. This macro does not raise
Fred Drake3adf79e2001-10-12 19:01:43 +0000974 exceptions.
975\end{cfuncdesc}
976
977\begin{cfuncdesc}{double}{Py_UNICODE_TONUMERIC}{Py_UNICODE ch}
Thomas Wouters477c8d52006-05-27 19:21:47 +0000978 Return the character \var{ch} converted to a double.
Georg Brandl99363b62005-09-03 07:27:26 +0000979 Return \code{-1.0} if this is not possible. This macro does not raise
Fred Drake3adf79e2001-10-12 19:01:43 +0000980 exceptions.
981\end{cfuncdesc}
982
983% --- Plain Py_UNICODE ---------------------------------------------------
984
985To create Unicode objects and access their basic sequence properties,
986use these APIs:
987
988\begin{cfuncdesc}{PyObject*}{PyUnicode_FromUnicode}{const Py_UNICODE *u,
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000989 Py_ssize_t size}
Fred Drake3adf79e2001-10-12 19:01:43 +0000990 Create a Unicode Object from the Py_UNICODE buffer \var{u} of the
991 given size. \var{u} may be \NULL{} which causes the contents to be
992 undefined. It is the user's responsibility to fill in the needed
993 data. The buffer is copied into the new object. If the buffer is
Tim Peters9ddf40b2004-06-20 22:41:32 +0000994 not \NULL{}, the return value might be a shared object. Therefore,
Fred Drake3adf79e2001-10-12 19:01:43 +0000995 modification of the resulting Unicode object is only allowed when
Tim Peters9ddf40b2004-06-20 22:41:32 +0000996 \var{u} is \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000997\end{cfuncdesc}
998
Walter Dörwaldacaa5a12007-05-05 12:00:46 +0000999\begin{cfuncdesc}{PyObject*}{PyUnicode_FromString}{const char *u}
Walter Dörwald1d0476b2007-05-24 19:10:53 +00001000 Create a Unicode Object from the char buffer \var{u}.
Walter Dörwaldacaa5a12007-05-05 12:00:46 +00001001 \var{u} must be 0-terminated, the bytes will be interpreted as
1002 being latin-1 encoded. \var{u} may also be \NULL{} which causes the
1003 contents to be undefined. It is the user's responsibility to fill
1004 in the needed data. The buffer is copied into the new object.
1005 If the buffer is not \NULL{}, the return value might be a shared object.
1006 Therefore, modification of the resulting Unicode object is only allowed
1007 when \var{u} is \NULL{}.
Walter Dörwald80bfb722007-06-11 16:44:48 +00001008 \versionadded{3.0}
Walter Dörwaldacaa5a12007-05-05 12:00:46 +00001009\end{cfuncdesc}
1010
Walter Dörwaldc0aa45f2007-06-11 16:43:18 +00001011\begin{cfuncdesc}{PyObject*}{PyUnicode_FromFormat}{const char *format, ...}
1012 Take a C \cfunction{printf()}-style \var{format} string and a
1013 variable number of arguments, calculate the size of the resulting
1014 Python unicode string and return a string with the values formatted into
1015 it. The variable arguments must be C types and must correspond
1016 exactly to the format characters in the \var{format} string. The
1017 following format characters are allowed:
1018
1019 % The descriptions for %zd and %zu are wrong, but the truth is complicated
1020 % because not all compilers support the %z width modifier -- we fake it
1021 % when necessary via interpolating PY_FORMAT_SIZE_T.
1022
Walter Dörwaldc0aa45f2007-06-11 16:43:18 +00001023 \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.
Walter Dörwald80bfb722007-06-11 16:44:48 +00001050 \versionadded{3.0}
Walter Dörwaldc0aa45f2007-06-11 16:43:18 +00001051\end{cfuncdesc}
1052
1053\begin{cfuncdesc}{PyObject*}{PyUnicode_FromFormatV}{const char *format,
1054 va_list vargs}
1055 Identical to \function{PyUnicode_FromFormat()} except that it takes
1056 exactly two arguments.
Walter Dörwald80bfb722007-06-11 16:44:48 +00001057 \versionadded{3.0}
Walter Dörwaldc0aa45f2007-06-11 16:43:18 +00001058\end{cfuncdesc}
1059
Fred Drake3adf79e2001-10-12 19:01:43 +00001060\begin{cfuncdesc}{Py_UNICODE*}{PyUnicode_AsUnicode}{PyObject *unicode}
1061 Return a read-only pointer to the Unicode object's internal
1062 \ctype{Py_UNICODE} buffer, \NULL{} if \var{unicode} is not a Unicode
1063 object.
1064\end{cfuncdesc}
1065
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001066\begin{cfuncdesc}{Py_ssize_t}{PyUnicode_GetSize}{PyObject *unicode}
Fred Drake3adf79e2001-10-12 19:01:43 +00001067 Return the length of the Unicode object.
1068\end{cfuncdesc}
1069
1070\begin{cfuncdesc}{PyObject*}{PyUnicode_FromEncodedObject}{PyObject *obj,
1071 const char *encoding,
1072 const char *errors}
1073 Coerce an encoded object \var{obj} to an Unicode object and return a
1074 reference with incremented refcount.
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001075
1076 String and other char buffer compatible objects are decoded
1077 according to the given encoding and using the error handling
1078 defined by errors. Both can be \NULL{} to have the interface
1079 use the default values (see the next section for details).
Fred Drake3adf79e2001-10-12 19:01:43 +00001080
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001081 All other objects, including Unicode objects, cause a
1082 \exception{TypeError} to be set.
Fred Drake3adf79e2001-10-12 19:01:43 +00001083
1084 The API returns \NULL{} if there was an error. The caller is
1085 responsible for decref'ing the returned objects.
1086\end{cfuncdesc}
1087
1088\begin{cfuncdesc}{PyObject*}{PyUnicode_FromObject}{PyObject *obj}
1089 Shortcut for \code{PyUnicode_FromEncodedObject(obj, NULL, "strict")}
1090 which is used throughout the interpreter whenever coercion to
1091 Unicode is needed.
1092\end{cfuncdesc}
1093
1094% --- wchar_t support for platforms which support it ---------------------
1095
1096If the platform supports \ctype{wchar_t} and provides a header file
1097wchar.h, Python can interface directly to this type using the
1098following functions. Support is optimized if Python's own
1099\ctype{Py_UNICODE} type is identical to the system's \ctype{wchar_t}.
1100
1101\begin{cfuncdesc}{PyObject*}{PyUnicode_FromWideChar}{const wchar_t *w,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001102 Py_ssize_t size}
Thomas Heller541703b2002-04-29 17:28:43 +00001103 Create a Unicode object from the \ctype{wchar_t} buffer \var{w} of
Georg Brandl99363b62005-09-03 07:27:26 +00001104 the given size. Return \NULL{} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00001105\end{cfuncdesc}
1106
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001107\begin{cfuncdesc}{Py_ssize_t}{PyUnicode_AsWideChar}{PyUnicodeObject *unicode,
Fred Drake3adf79e2001-10-12 19:01:43 +00001108 wchar_t *w,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001109 Py_ssize_t size}
Georg Brandl99363b62005-09-03 07:27:26 +00001110 Copy the Unicode object contents into the \ctype{wchar_t} buffer
Marc-André Lemburga9cadcd2004-11-22 13:02:31 +00001111 \var{w}. At most \var{size} \ctype{wchar_t} characters are copied
Georg Brandl99363b62005-09-03 07:27:26 +00001112 (excluding a possibly trailing 0-termination character). Return
Marc-André Lemburga9cadcd2004-11-22 13:02:31 +00001113 the number of \ctype{wchar_t} characters copied or -1 in case of an
1114 error. Note that the resulting \ctype{wchar_t} string may or may
1115 not be 0-terminated. It is the responsibility of the caller to make
1116 sure that the \ctype{wchar_t} string is 0-terminated in case this is
1117 required by the application.
Fred Drake3adf79e2001-10-12 19:01:43 +00001118\end{cfuncdesc}
1119
1120
1121\subsubsection{Built-in Codecs \label{builtinCodecs}}
1122
1123Python provides a set of builtin codecs which are written in C
1124for speed. All of these codecs are directly usable via the
1125following functions.
1126
1127Many of the following APIs take two arguments encoding and
1128errors. These parameters encoding and errors have the same semantics
1129as the ones of the builtin unicode() Unicode object constructor.
1130
1131Setting encoding to \NULL{} causes the default encoding to be used
1132which is \ASCII. The file system calls should use
1133\cdata{Py_FileSystemDefaultEncoding} as the encoding for file
1134names. This variable should be treated as read-only: On some systems,
1135it will be a pointer to a static string, on others, it will change at
Raymond Hettingercb2da432003-10-12 18:24:34 +00001136run-time (such as when the application invokes setlocale).
Fred Drake3adf79e2001-10-12 19:01:43 +00001137
1138Error handling is set by errors which may also be set to \NULL{}
1139meaning to use the default handling defined for the codec. Default
1140error handling for all builtin codecs is ``strict''
1141(\exception{ValueError} is raised).
1142
1143The codecs all use a similar interface. Only deviation from the
1144following generic ones are documented for simplicity.
1145
1146% --- Generic Codecs -----------------------------------------------------
1147
1148These are the generic codec APIs:
1149
1150\begin{cfuncdesc}{PyObject*}{PyUnicode_Decode}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001151 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001152 const char *encoding,
1153 const char *errors}
1154 Create a Unicode object by decoding \var{size} bytes of the encoded
1155 string \var{s}. \var{encoding} and \var{errors} have the same
1156 meaning as the parameters of the same name in the
1157 \function{unicode()} builtin function. The codec to be used is
Georg Brandl99363b62005-09-03 07:27:26 +00001158 looked up using the Python codec registry. Return \NULL{} if an
Fred Drake3adf79e2001-10-12 19:01:43 +00001159 exception was raised by the codec.
1160\end{cfuncdesc}
1161
1162\begin{cfuncdesc}{PyObject*}{PyUnicode_Encode}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001163 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001164 const char *encoding,
1165 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001166 Encode the \ctype{Py_UNICODE} buffer of the given size and return
Fred Drake3adf79e2001-10-12 19:01:43 +00001167 a Python string object. \var{encoding} and \var{errors} have the
1168 same meaning as the parameters of the same name in the Unicode
1169 \method{encode()} method. The codec to be used is looked up using
Georg Brandl99363b62005-09-03 07:27:26 +00001170 the Python codec registry. Return \NULL{} if an exception was
Fred Drake3adf79e2001-10-12 19:01:43 +00001171 raised by the codec.
1172\end{cfuncdesc}
1173
1174\begin{cfuncdesc}{PyObject*}{PyUnicode_AsEncodedString}{PyObject *unicode,
1175 const char *encoding,
1176 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001177 Encode a Unicode object and return the result as Python string
Fred Drake3adf79e2001-10-12 19:01:43 +00001178 object. \var{encoding} and \var{errors} have the same meaning as the
1179 parameters of the same name in the Unicode \method{encode()} method.
1180 The codec to be used is looked up using the Python codec registry.
Georg Brandl99363b62005-09-03 07:27:26 +00001181 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +00001182\end{cfuncdesc}
1183
1184% --- UTF-8 Codecs -------------------------------------------------------
1185
1186These are the UTF-8 codec APIs:
1187
1188\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF8}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001189 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001190 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001191 Create a Unicode object by decoding \var{size} bytes of the UTF-8
1192 encoded string \var{s}. Return \NULL{} if an exception was raised
Fred Drake3adf79e2001-10-12 19:01:43 +00001193 by the codec.
1194\end{cfuncdesc}
1195
Walter Dörwald69652032004-09-07 20:24:22 +00001196\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF8Stateful}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001197 Py_ssize_t size,
Walter Dörwald69652032004-09-07 20:24:22 +00001198 const char *errors,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001199 Py_ssize_t *consumed}
Georg Brandl99363b62005-09-03 07:27:26 +00001200 If \var{consumed} is \NULL{}, behave like \cfunction{PyUnicode_DecodeUTF8()}.
Walter Dörwald69652032004-09-07 20:24:22 +00001201 If \var{consumed} is not \NULL{}, trailing incomplete UTF-8 byte sequences
1202 will not be treated as an error. Those bytes will not be decoded and the
1203 number of bytes that have been decoded will be stored in \var{consumed}.
1204 \versionadded{2.4}
1205\end{cfuncdesc}
1206
Fred Drake3adf79e2001-10-12 19:01:43 +00001207\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUTF8}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001208 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001209 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001210 Encode the \ctype{Py_UNICODE} buffer of the given size using UTF-8
1211 and return a Python string object. Return \NULL{} if an exception
Fred Drake3adf79e2001-10-12 19:01:43 +00001212 was raised by the codec.
1213\end{cfuncdesc}
1214
1215\begin{cfuncdesc}{PyObject*}{PyUnicode_AsUTF8String}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001216 Encode a Unicode objects using UTF-8 and return the result as
1217 Python string object. Error handling is ``strict''. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00001218 \NULL{} if an exception was raised by the codec.
1219\end{cfuncdesc}
1220
1221% --- UTF-16 Codecs ------------------------------------------------------ */
1222
1223These are the UTF-16 codec APIs:
1224
1225\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF16}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001226 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001227 const char *errors,
1228 int *byteorder}
Georg Brandl99363b62005-09-03 07:27:26 +00001229 Decode \var{length} bytes from a UTF-16 encoded buffer string and
1230 return the corresponding Unicode object. \var{errors} (if
Tim Peters9ddf40b2004-06-20 22:41:32 +00001231 non-\NULL{}) defines the error handling. It defaults to ``strict''.
Fred Drake3adf79e2001-10-12 19:01:43 +00001232
Tim Peters9ddf40b2004-06-20 22:41:32 +00001233 If \var{byteorder} is non-\NULL{}, the decoder starts decoding using
Fred Drake3adf79e2001-10-12 19:01:43 +00001234 the given byte order:
1235
1236\begin{verbatim}
1237 *byteorder == -1: little endian
1238 *byteorder == 0: native order
1239 *byteorder == 1: big endian
1240\end{verbatim}
1241
Guido van Rossum360e4b82007-05-14 22:51:27 +00001242 and then switches if the first two bytes of the input data are a byte order
1243 mark (BOM) and the specified byte order is native order. This BOM is not
1244 copied into the resulting Unicode string. After completion, \var{*byteorder}
1245 is set to the current byte order at the.
Fred Drake3adf79e2001-10-12 19:01:43 +00001246
Tim Peters9ddf40b2004-06-20 22:41:32 +00001247 If \var{byteorder} is \NULL{}, the codec starts in native order mode.
Fred Drake3adf79e2001-10-12 19:01:43 +00001248
Georg Brandl99363b62005-09-03 07:27:26 +00001249 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +00001250\end{cfuncdesc}
1251
Walter Dörwald69652032004-09-07 20:24:22 +00001252\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF16Stateful}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001253 Py_ssize_t size,
Walter Dörwald69652032004-09-07 20:24:22 +00001254 const char *errors,
1255 int *byteorder,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001256 Py_ssize_t *consumed}
Georg Brandl99363b62005-09-03 07:27:26 +00001257 If \var{consumed} is \NULL{}, behave like
Walter Dörwald69652032004-09-07 20:24:22 +00001258 \cfunction{PyUnicode_DecodeUTF16()}. If \var{consumed} is not \NULL{},
1259 \cfunction{PyUnicode_DecodeUTF16Stateful()} will not treat trailing incomplete
Raymond Hettinger0c230b92005-08-17 10:05:22 +00001260 UTF-16 byte sequences (such as an odd number of bytes or a split surrogate pair)
Walter Dörwald69652032004-09-07 20:24:22 +00001261 as an error. Those bytes will not be decoded and the number of bytes that
1262 have been decoded will be stored in \var{consumed}.
1263 \versionadded{2.4}
1264\end{cfuncdesc}
1265
Fred Drake3adf79e2001-10-12 19:01:43 +00001266\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUTF16}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001267 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001268 const char *errors,
1269 int byteorder}
Georg Brandl99363b62005-09-03 07:27:26 +00001270 Return a Python string object holding the UTF-16 encoded value of
Fred Drake3adf79e2001-10-12 19:01:43 +00001271 the Unicode data in \var{s}. If \var{byteorder} is not \code{0},
1272 output is written according to the following byte order:
1273
1274\begin{verbatim}
1275 byteorder == -1: little endian
1276 byteorder == 0: native byte order (writes a BOM mark)
1277 byteorder == 1: big endian
1278\end{verbatim}
1279
1280 If byteorder is \code{0}, the output string will always start with
1281 the Unicode BOM mark (U+FEFF). In the other two modes, no BOM mark
1282 is prepended.
1283
Martin v. Löwis9bc4f2d2004-06-03 09:55:28 +00001284 If \var{Py_UNICODE_WIDE} is defined, a single \ctype{Py_UNICODE}
1285 value may get represented as a surrogate pair. If it is not
1286 defined, each \ctype{Py_UNICODE} values is interpreted as an
1287 UCS-2 character.
Fred Drake3adf79e2001-10-12 19:01:43 +00001288
Georg Brandl99363b62005-09-03 07:27:26 +00001289 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +00001290\end{cfuncdesc}
1291
1292\begin{cfuncdesc}{PyObject*}{PyUnicode_AsUTF16String}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001293 Return a Python string using the UTF-16 encoding in native byte
Fred Drake3adf79e2001-10-12 19:01:43 +00001294 order. The string always starts with a BOM mark. Error handling is
Georg Brandl99363b62005-09-03 07:27:26 +00001295 ``strict''. Return \NULL{} if an exception was raised by the
Fred Drake3adf79e2001-10-12 19:01:43 +00001296 codec.
1297\end{cfuncdesc}
1298
1299% --- Unicode-Escape Codecs ----------------------------------------------
1300
Martin v. Löwis95cf84a2003-10-19 07:32:24 +00001301These are the ``Unicode Escape'' codec APIs:
Fred Drake3adf79e2001-10-12 19:01:43 +00001302
1303\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUnicodeEscape}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001304 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001305 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001306 Create a Unicode object by decoding \var{size} bytes of the
1307 Unicode-Escape encoded string \var{s}. Return \NULL{} if an
Fred Drake3adf79e2001-10-12 19:01:43 +00001308 exception was raised by the codec.
1309\end{cfuncdesc}
1310
1311\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUnicodeEscape}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001312 Py_ssize_t size}
Georg Brandl99363b62005-09-03 07:27:26 +00001313 Encode the \ctype{Py_UNICODE} buffer of the given size using
1314 Unicode-Escape and return a Python string object. Return \NULL{}
Fred Drake3adf79e2001-10-12 19:01:43 +00001315 if an exception was raised by the codec.
1316\end{cfuncdesc}
1317
1318\begin{cfuncdesc}{PyObject*}{PyUnicode_AsUnicodeEscapeString}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001319 Encode a Unicode objects using Unicode-Escape and return the
Fred Drake3adf79e2001-10-12 19:01:43 +00001320 result as Python string object. Error handling is ``strict''.
Georg Brandl99363b62005-09-03 07:27:26 +00001321 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +00001322\end{cfuncdesc}
1323
1324% --- Raw-Unicode-Escape Codecs ------------------------------------------
1325
Martin v. Löwis95cf84a2003-10-19 07:32:24 +00001326These are the ``Raw Unicode Escape'' codec APIs:
Fred Drake3adf79e2001-10-12 19:01:43 +00001327
1328\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeRawUnicodeEscape}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001329 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001330 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001331 Create a Unicode object by decoding \var{size} bytes of the
1332 Raw-Unicode-Escape encoded string \var{s}. Return \NULL{} if an
Fred Drake3adf79e2001-10-12 19:01:43 +00001333 exception was raised by the codec.
1334\end{cfuncdesc}
1335
1336\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeRawUnicodeEscape}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001337 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001338 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001339 Encode the \ctype{Py_UNICODE} buffer of the given size using
1340 Raw-Unicode-Escape and return a Python string object. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00001341 \NULL{} if an exception was raised by the codec.
1342\end{cfuncdesc}
1343
1344\begin{cfuncdesc}{PyObject*}{PyUnicode_AsRawUnicodeEscapeString}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001345 Encode a Unicode objects using Raw-Unicode-Escape and return the
Fred Drake3adf79e2001-10-12 19:01:43 +00001346 result as Python string object. Error handling is ``strict''.
Georg Brandl99363b62005-09-03 07:27:26 +00001347 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +00001348\end{cfuncdesc}
1349
Tim Petersf582b822001-12-11 18:51:08 +00001350% --- Latin-1 Codecs -----------------------------------------------------
Fred Drake3adf79e2001-10-12 19:01:43 +00001351
1352These are the Latin-1 codec APIs:
1353Latin-1 corresponds to the first 256 Unicode ordinals and only these
1354are accepted by the codecs during encoding.
1355
1356\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeLatin1}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001357 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001358 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001359 Create a Unicode object by decoding \var{size} bytes of the Latin-1
1360 encoded string \var{s}. Return \NULL{} if an exception was raised
Fred Drake3adf79e2001-10-12 19:01:43 +00001361 by the codec.
1362\end{cfuncdesc}
1363
1364\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeLatin1}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001365 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001366 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001367 Encode the \ctype{Py_UNICODE} buffer of the given size using
1368 Latin-1 and return a Python string object. Return \NULL{} if an
Fred Drake3adf79e2001-10-12 19:01:43 +00001369 exception was raised by the codec.
1370\end{cfuncdesc}
1371
1372\begin{cfuncdesc}{PyObject*}{PyUnicode_AsLatin1String}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001373 Encode a Unicode objects using Latin-1 and return the result as
1374 Python string object. Error handling is ``strict''. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00001375 \NULL{} if an exception was raised by the codec.
1376\end{cfuncdesc}
1377
Tim Petersf582b822001-12-11 18:51:08 +00001378% --- ASCII Codecs -------------------------------------------------------
Fred Drake3adf79e2001-10-12 19:01:43 +00001379
1380These are the \ASCII{} codec APIs. Only 7-bit \ASCII{} data is
1381accepted. All other codes generate errors.
1382
1383\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeASCII}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001384 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001385 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001386 Create a Unicode object by decoding \var{size} bytes of the
1387 \ASCII{} encoded string \var{s}. Return \NULL{} if an exception
Fred Drake3adf79e2001-10-12 19:01:43 +00001388 was raised by the codec.
1389\end{cfuncdesc}
1390
1391\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeASCII}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001392 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001393 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001394 Encode the \ctype{Py_UNICODE} buffer of the given size using
1395 \ASCII{} and return a Python string object. Return \NULL{} if an
Fred Drake3adf79e2001-10-12 19:01:43 +00001396 exception was raised by the codec.
1397\end{cfuncdesc}
1398
1399\begin{cfuncdesc}{PyObject*}{PyUnicode_AsASCIIString}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001400 Encode a Unicode objects using \ASCII{} and return the result as
1401 Python string object. Error handling is ``strict''. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00001402 \NULL{} if an exception was raised by the codec.
1403\end{cfuncdesc}
1404
Tim Petersf582b822001-12-11 18:51:08 +00001405% --- Character Map Codecs -----------------------------------------------
Fred Drake3adf79e2001-10-12 19:01:43 +00001406
1407These are the mapping codec APIs:
1408
1409This codec is special in that it can be used to implement many
1410different codecs (and this is in fact what was done to obtain most of
1411the standard codecs included in the \module{encodings} package). The
1412codec uses mapping to encode and decode characters.
1413
1414Decoding mappings must map single string characters to single Unicode
1415characters, integers (which are then interpreted as Unicode ordinals)
Tim Petersf582b822001-12-11 18:51:08 +00001416or None (meaning "undefined mapping" and causing an error).
Fred Drake3adf79e2001-10-12 19:01:43 +00001417
1418Encoding mappings must map single Unicode characters to single string
1419characters, integers (which are then interpreted as Latin-1 ordinals)
1420or None (meaning "undefined mapping" and causing an error).
1421
1422The mapping objects provided must only support the __getitem__ mapping
1423interface.
1424
1425If a character lookup fails with a LookupError, the character is
1426copied as-is meaning that its ordinal value will be interpreted as
1427Unicode or Latin-1 ordinal resp. Because of this, mappings only need
1428to contain those mappings which map characters to different code
1429points.
1430
1431\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeCharmap}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001432 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001433 PyObject *mapping,
1434 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001435 Create a Unicode object by decoding \var{size} bytes of the encoded
1436 string \var{s} using the given \var{mapping} object. Return
Walter Dörwaldd1c1e102005-10-06 20:29:57 +00001437 \NULL{} if an exception was raised by the codec. If \var{mapping} is \NULL{}
1438 latin-1 decoding will be done. Else it can be a dictionary mapping byte or a
1439 unicode string, which is treated as a lookup table. Byte values greater
1440 that the length of the string and U+FFFE "characters" are treated as
1441 "undefined mapping".
1442 \versionchanged[Allowed unicode string as mapping argument]{2.4}
Fred Drake3adf79e2001-10-12 19:01:43 +00001443\end{cfuncdesc}
1444
1445\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeCharmap}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001446 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001447 PyObject *mapping,
1448 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001449 Encode the \ctype{Py_UNICODE} buffer of the given size using the
1450 given \var{mapping} object and return a Python string object.
1451 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +00001452\end{cfuncdesc}
1453
1454\begin{cfuncdesc}{PyObject*}{PyUnicode_AsCharmapString}{PyObject *unicode,
1455 PyObject *mapping}
Georg Brandl99363b62005-09-03 07:27:26 +00001456 Encode a Unicode objects using the given \var{mapping} object and
1457 return the result as Python string object. Error handling is
1458 ``strict''. Return \NULL{} if an exception was raised by the
Fred Drake3adf79e2001-10-12 19:01:43 +00001459 codec.
1460\end{cfuncdesc}
1461
1462The following codec API is special in that maps Unicode to Unicode.
1463
1464\begin{cfuncdesc}{PyObject*}{PyUnicode_TranslateCharmap}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001465 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001466 PyObject *table,
1467 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001468 Translate a \ctype{Py_UNICODE} buffer of the given length by
1469 applying a character mapping \var{table} to it and return the
1470 resulting Unicode object. Return \NULL{} when an exception was
Fred Drake3adf79e2001-10-12 19:01:43 +00001471 raised by the codec.
1472
1473 The \var{mapping} table must map Unicode ordinal integers to Unicode
1474 ordinal integers or None (causing deletion of the character).
1475
Thomas Wouters477c8d52006-05-27 19:21:47 +00001476 Mapping tables need only provide the \method{__getitem__()}
Fred Drake3adf79e2001-10-12 19:01:43 +00001477 interface; dictionaries and sequences work well. Unmapped character
1478 ordinals (ones which cause a \exception{LookupError}) are left
1479 untouched and are copied as-is.
1480\end{cfuncdesc}
1481
1482% --- MBCS codecs for Windows --------------------------------------------
1483
1484These are the MBCS codec APIs. They are currently only available on
1485Windows and use the Win32 MBCS converters to implement the
1486conversions. Note that MBCS (or DBCS) is a class of encodings, not
1487just one. The target encoding is defined by the user settings on the
1488machine running the codec.
1489
1490\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeMBCS}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001491 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001492 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001493 Create a Unicode object by decoding \var{size} bytes of the MBCS
1494 encoded string \var{s}. Return \NULL{} if an exception was
Fred Drake3adf79e2001-10-12 19:01:43 +00001495 raised by the codec.
1496\end{cfuncdesc}
1497
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001498\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeMBCSStateful}{const char *s,
1499 int size,
1500 const char *errors,
1501 int *consumed}
1502 If \var{consumed} is \NULL{}, behave like
1503 \cfunction{PyUnicode_DecodeMBCS()}. If \var{consumed} is not \NULL{},
1504 \cfunction{PyUnicode_DecodeMBCSStateful()} will not decode trailing lead
1505 byte and the number of bytes that have been decoded will be stored in
1506 \var{consumed}.
1507 \versionadded{2.5}
1508\end{cfuncdesc}
1509
Fred Drake3adf79e2001-10-12 19:01:43 +00001510\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeMBCS}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001511 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001512 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001513 Encode the \ctype{Py_UNICODE} buffer of the given size using MBCS
1514 and return a Python string object. Return \NULL{} if an exception
Fred Drake3adf79e2001-10-12 19:01:43 +00001515 was raised by the codec.
1516\end{cfuncdesc}
1517
1518\begin{cfuncdesc}{PyObject*}{PyUnicode_AsMBCSString}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001519 Encode a Unicode objects using MBCS and return the result as
1520 Python string object. Error handling is ``strict''. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00001521 \NULL{} if an exception was raised by the codec.
1522\end{cfuncdesc}
1523
1524% --- Methods & Slots ----------------------------------------------------
1525
1526\subsubsection{Methods and Slot Functions \label{unicodeMethodsAndSlots}}
1527
1528The following APIs are capable of handling Unicode objects and strings
1529on input (we refer to them as strings in the descriptions) and return
Martin v. Löwis95cf84a2003-10-19 07:32:24 +00001530Unicode objects or integers as appropriate.
Fred Drake3adf79e2001-10-12 19:01:43 +00001531
1532They all return \NULL{} or \code{-1} if an exception occurs.
1533
1534\begin{cfuncdesc}{PyObject*}{PyUnicode_Concat}{PyObject *left,
1535 PyObject *right}
1536 Concat two strings giving a new Unicode string.
1537\end{cfuncdesc}
1538
1539\begin{cfuncdesc}{PyObject*}{PyUnicode_Split}{PyObject *s,
1540 PyObject *sep,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001541 Py_ssize_t maxsplit}
Tim Peters9ddf40b2004-06-20 22:41:32 +00001542 Split a string giving a list of Unicode strings. If sep is \NULL{},
Fred Drake3adf79e2001-10-12 19:01:43 +00001543 splitting will be done at all whitespace substrings. Otherwise,
1544 splits occur at the given separator. At most \var{maxsplit} splits
1545 will be done. If negative, no limit is set. Separators are not
1546 included in the resulting list.
1547\end{cfuncdesc}
1548
1549\begin{cfuncdesc}{PyObject*}{PyUnicode_Splitlines}{PyObject *s,
Martin v. Löwis24b88812003-03-30 16:40:42 +00001550 int keepend}
Fred Drake3adf79e2001-10-12 19:01:43 +00001551 Split a Unicode string at line breaks, returning a list of Unicode
Martin v. Löwis24b88812003-03-30 16:40:42 +00001552 strings. CRLF is considered to be one line break. If \var{keepend}
1553 is 0, the Line break characters are not included in the resulting
1554 strings.
Fred Drake3adf79e2001-10-12 19:01:43 +00001555\end{cfuncdesc}
1556
1557\begin{cfuncdesc}{PyObject*}{PyUnicode_Translate}{PyObject *str,
1558 PyObject *table,
1559 const char *errors}
1560 Translate a string by applying a character mapping table to it and
1561 return the resulting Unicode object.
1562
1563 The mapping table must map Unicode ordinal integers to Unicode
1564 ordinal integers or None (causing deletion of the character).
1565
1566 Mapping tables need only provide the \method{__getitem__()}
1567 interface; dictionaries and sequences work well. Unmapped character
1568 ordinals (ones which cause a \exception{LookupError}) are left
1569 untouched and are copied as-is.
1570
1571 \var{errors} has the usual meaning for codecs. It may be \NULL{}
1572 which indicates to use the default error handling.
1573\end{cfuncdesc}
1574
1575\begin{cfuncdesc}{PyObject*}{PyUnicode_Join}{PyObject *separator,
1576 PyObject *seq}
1577 Join a sequence of strings using the given separator and return the
1578 resulting Unicode string.
1579\end{cfuncdesc}
1580
Raymond Hettinger8ef9b3e2004-12-10 17:12:32 +00001581\begin{cfuncdesc}{int}{PyUnicode_Tailmatch}{PyObject *str,
Fred Drake3adf79e2001-10-12 19:01:43 +00001582 PyObject *substr,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001583 Py_ssize_t start,
1584 Py_ssize_t end,
Fred Drake3adf79e2001-10-12 19:01:43 +00001585 int direction}
1586 Return 1 if \var{substr} matches \var{str}[\var{start}:\var{end}] at
1587 the given tail end (\var{direction} == -1 means to do a prefix
1588 match, \var{direction} == 1 a suffix match), 0 otherwise.
Thomas Wouters477c8d52006-05-27 19:21:47 +00001589 Return \code{-1} if an error occurred.
Fred Drake3adf79e2001-10-12 19:01:43 +00001590\end{cfuncdesc}
1591
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001592\begin{cfuncdesc}{Py_ssize_t}{PyUnicode_Find}{PyObject *str,
Fred Drake1d1e1db2002-06-20 22:07:04 +00001593 PyObject *substr,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001594 Py_ssize_t start,
1595 Py_ssize_t end,
Fred Drake1d1e1db2002-06-20 22:07:04 +00001596 int direction}
Fred Drake3adf79e2001-10-12 19:01:43 +00001597 Return the first position of \var{substr} in
1598 \var{str}[\var{start}:\var{end}] using the given \var{direction}
1599 (\var{direction} == 1 means to do a forward search,
Fred Drake1d1e1db2002-06-20 22:07:04 +00001600 \var{direction} == -1 a backward search). The return value is the
1601 index of the first match; a value of \code{-1} indicates that no
1602 match was found, and \code{-2} indicates that an error occurred and
1603 an exception has been set.
Fred Drake3adf79e2001-10-12 19:01:43 +00001604\end{cfuncdesc}
1605
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001606\begin{cfuncdesc}{Py_ssize_t}{PyUnicode_Count}{PyObject *str,
Fred Drake1d1e1db2002-06-20 22:07:04 +00001607 PyObject *substr,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001608 Py_ssize_t start,
1609 Py_ssize_t end}
Fred Drake1d1e1db2002-06-20 22:07:04 +00001610 Return the number of non-overlapping occurrences of \var{substr} in
Georg Brandl99363b62005-09-03 07:27:26 +00001611 \code{\var{str}[\var{start}:\var{end}]}. Return \code{-1} if an
Fred Drake1d1e1db2002-06-20 22:07:04 +00001612 error occurred.
Fred Drake3adf79e2001-10-12 19:01:43 +00001613\end{cfuncdesc}
1614
1615\begin{cfuncdesc}{PyObject*}{PyUnicode_Replace}{PyObject *str,
1616 PyObject *substr,
1617 PyObject *replstr,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001618 Py_ssize_t maxcount}
Fred Drake3adf79e2001-10-12 19:01:43 +00001619 Replace at most \var{maxcount} occurrences of \var{substr} in
1620 \var{str} with \var{replstr} and return the resulting Unicode object.
1621 \var{maxcount} == -1 means replace all occurrences.
1622\end{cfuncdesc}
1623
1624\begin{cfuncdesc}{int}{PyUnicode_Compare}{PyObject *left, PyObject *right}
1625 Compare two strings and return -1, 0, 1 for less than, equal, and
1626 greater than, respectively.
1627\end{cfuncdesc}
1628
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001629\begin{cfuncdesc}{int}{PyUnicode_RichCompare}{PyObject *left,
1630 PyObject *right,
1631 int op}
1632
1633 Rich compare two unicode strings and return one of the following:
1634 \begin{itemize}
1635 \item \code{NULL} in case an exception was raised
1636 \item \constant{Py_True} or \constant{Py_False} for successful comparisons
1637 \item \constant{Py_NotImplemented} in case the type combination is unknown
1638 \end{itemize}
1639
1640 Note that \constant{Py_EQ} and \constant{Py_NE} comparisons can cause a
1641 \exception{UnicodeWarning} in case the conversion of the arguments to
1642 Unicode fails with a \exception{UnicodeDecodeError}.
1643
1644 Possible values for \var{op} are
1645 \constant{Py_GT}, \constant{Py_GE}, \constant{Py_EQ},
1646 \constant{Py_NE}, \constant{Py_LT}, and \constant{Py_LE}.
1647\end{cfuncdesc}
1648
Fred Drake3adf79e2001-10-12 19:01:43 +00001649\begin{cfuncdesc}{PyObject*}{PyUnicode_Format}{PyObject *format,
1650 PyObject *args}
Georg Brandl99363b62005-09-03 07:27:26 +00001651 Return a new string object from \var{format} and \var{args}; this
Fred Drake3adf79e2001-10-12 19:01:43 +00001652 is analogous to \code{\var{format} \%\ \var{args}}. The
1653 \var{args} argument must be a tuple.
1654\end{cfuncdesc}
1655
1656\begin{cfuncdesc}{int}{PyUnicode_Contains}{PyObject *container,
1657 PyObject *element}
Georg Brandl99363b62005-09-03 07:27:26 +00001658 Check whether \var{element} is contained in \var{container} and
1659 return true or false accordingly.
Fred Drake3adf79e2001-10-12 19:01:43 +00001660
1661 \var{element} has to coerce to a one element Unicode
1662 string. \code{-1} is returned if there was an error.
1663\end{cfuncdesc}
1664
Walter Dörwalde65c86c2007-05-25 14:14:31 +00001665\begin{cfuncdesc}{void}{PyUnicode_InternInPlace}{PyObject **string}
1666 Intern the argument \var{*string} in place. The argument must be
1667 the address of a pointer variable pointing to a Python unicode string
1668 object. If there is an existing interned string that is the same as
1669 \var{*string}, it sets \var{*string} to it (decrementing the
1670 reference count of the old string object and incrementing the
1671 reference count of the interned string object), otherwise it leaves
1672 \var{*string} alone and interns it (incrementing its reference
1673 count). (Clarification: even though there is a lot of talk about
1674 reference counts, think of this function as reference-count-neutral;
1675 you own the object after the call if and only if you owned it before
1676 the call.)
1677\end{cfuncdesc}
1678
1679\begin{cfuncdesc}{PyObject*}{PyUnicode_InternFromString}{const char *v}
1680 A combination of \cfunction{PyUnicode_FromString()} and
1681 \cfunction{PyUnicode_InternInPlace()}, returning either a new unicode
1682 string object that has been interned, or a new (``owned'') reference to
1683 an earlier interned string object with the same value.
1684\end{cfuncdesc}
1685
Fred Drake3adf79e2001-10-12 19:01:43 +00001686
1687\subsection{Buffer Objects \label{bufferObjects}}
1688\sectionauthor{Greg Stein}{gstein@lyra.org}
1689
1690\obindex{buffer}
1691Python objects implemented in C can export a group of functions called
1692the ``buffer\index{buffer interface} interface.'' These functions can
1693be used by an object to expose its data in a raw, byte-oriented
1694format. Clients of the object can use the buffer interface to access
1695the object data directly, without needing to copy it first.
1696
Tim Petersf582b822001-12-11 18:51:08 +00001697Two examples of objects that support
1698the buffer interface are strings and arrays. The string object exposes
Fred Drake3adf79e2001-10-12 19:01:43 +00001699the character contents in the buffer interface's byte-oriented
1700form. An array can also expose its contents, but it should be noted
1701that array elements may be multi-byte values.
1702
1703An example user of the buffer interface is the file object's
1704\method{write()} method. Any object that can export a series of bytes
1705through the buffer interface can be written to a file. There are a
Tim Petersf582b822001-12-11 18:51:08 +00001706number of format codes to \cfunction{PyArg_ParseTuple()} that operate
Fred Drake3adf79e2001-10-12 19:01:43 +00001707against an object's buffer interface, returning data from the target
1708object.
1709
1710More information on the buffer interface is provided in the section
Fred Drake54e62942001-12-11 19:40:16 +00001711``Buffer Object Structures'' (section~\ref{buffer-structs}), under
Fred Drake3adf79e2001-10-12 19:01:43 +00001712the description for \ctype{PyBufferProcs}\ttindex{PyBufferProcs}.
1713
1714A ``buffer object'' is defined in the \file{bufferobject.h} header
1715(included by \file{Python.h}). These objects look very similar to
1716string objects at the Python programming level: they support slicing,
1717indexing, concatenation, and some other standard string
1718operations. However, their data can come from one of two sources: from
1719a block of memory, or from another object which exports the buffer
1720interface.
1721
1722Buffer objects are useful as a way to expose the data from another
1723object's buffer interface to the Python programmer. They can also be
1724used as a zero-copy slicing mechanism. Using their ability to
1725reference a block of memory, it is possible to expose any data to the
1726Python programmer quite easily. The memory could be a large, constant
1727array in a C extension, it could be a raw block of memory for
1728manipulation before passing to an operating system library, or it
1729could be used to pass around structured data in its native, in-memory
1730format.
1731
1732\begin{ctypedesc}{PyBufferObject}
1733 This subtype of \ctype{PyObject} represents a buffer object.
1734\end{ctypedesc}
1735
1736\begin{cvardesc}{PyTypeObject}{PyBuffer_Type}
1737 The instance of \ctype{PyTypeObject} which represents the Python
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001738 buffer type; it is the same object as \code{buffer} and
1739 \code{types.BufferType} in the Python layer.
1740 \withsubitem{(in module types)}{\ttindex{BufferType}}.
Fred Drake3adf79e2001-10-12 19:01:43 +00001741\end{cvardesc}
1742
1743\begin{cvardesc}{int}{Py_END_OF_BUFFER}
1744 This constant may be passed as the \var{size} parameter to
1745 \cfunction{PyBuffer_FromObject()} or
1746 \cfunction{PyBuffer_FromReadWriteObject()}. It indicates that the
1747 new \ctype{PyBufferObject} should refer to \var{base} object from
1748 the specified \var{offset} to the end of its exported buffer. Using
1749 this enables the caller to avoid querying the \var{base} object for
1750 its length.
1751\end{cvardesc}
1752
1753\begin{cfuncdesc}{int}{PyBuffer_Check}{PyObject *p}
1754 Return true if the argument has type \cdata{PyBuffer_Type}.
1755\end{cfuncdesc}
1756
1757\begin{cfuncdesc}{PyObject*}{PyBuffer_FromObject}{PyObject *base,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001758 Py_ssize_t offset, Py_ssize_t size}
Fred Drake3adf79e2001-10-12 19:01:43 +00001759 Return a new read-only buffer object. This raises
1760 \exception{TypeError} if \var{base} doesn't support the read-only
1761 buffer protocol or doesn't provide exactly one buffer segment, or it
1762 raises \exception{ValueError} if \var{offset} is less than zero. The
1763 buffer will hold a reference to the \var{base} object, and the
1764 buffer's contents will refer to the \var{base} object's buffer
1765 interface, starting as position \var{offset} and extending for
1766 \var{size} bytes. If \var{size} is \constant{Py_END_OF_BUFFER}, then
1767 the new buffer's contents extend to the length of the \var{base}
1768 object's exported buffer data.
1769\end{cfuncdesc}
1770
1771\begin{cfuncdesc}{PyObject*}{PyBuffer_FromReadWriteObject}{PyObject *base,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001772 Py_ssize_t offset,
1773 Py_ssize_t size}
Fred Drake3adf79e2001-10-12 19:01:43 +00001774 Return a new writable buffer object. Parameters and exceptions are
1775 similar to those for \cfunction{PyBuffer_FromObject()}. If the
1776 \var{base} object does not export the writeable buffer protocol,
1777 then \exception{TypeError} is raised.
1778\end{cfuncdesc}
1779
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001780\begin{cfuncdesc}{PyObject*}{PyBuffer_FromMemory}{void *ptr, Py_ssize_t size}
Fred Drake3adf79e2001-10-12 19:01:43 +00001781 Return a new read-only buffer object that reads from a specified
1782 location in memory, with a specified size. The caller is
1783 responsible for ensuring that the memory buffer, passed in as
1784 \var{ptr}, is not deallocated while the returned buffer object
1785 exists. Raises \exception{ValueError} if \var{size} is less than
1786 zero. Note that \constant{Py_END_OF_BUFFER} may \emph{not} be
1787 passed for the \var{size} parameter; \exception{ValueError} will be
1788 raised in that case.
1789\end{cfuncdesc}
1790
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001791\begin{cfuncdesc}{PyObject*}{PyBuffer_FromReadWriteMemory}{void *ptr, Py_ssize_t size}
Fred Drake3adf79e2001-10-12 19:01:43 +00001792 Similar to \cfunction{PyBuffer_FromMemory()}, but the returned
1793 buffer is writable.
1794\end{cfuncdesc}
1795
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001796\begin{cfuncdesc}{PyObject*}{PyBuffer_New}{Py_ssize_t size}
Georg Brandl99363b62005-09-03 07:27:26 +00001797 Return a new writable buffer object that maintains its own memory
Fred Drake3adf79e2001-10-12 19:01:43 +00001798 buffer of \var{size} bytes. \exception{ValueError} is returned if
Neil Schemenauerd68d3ee2004-06-08 02:58:50 +00001799 \var{size} is not zero or positive. Note that the memory buffer (as
1800 returned by \cfunction{PyObject_AsWriteBuffer()}) is not specifically
1801 aligned.
Fred Drake3adf79e2001-10-12 19:01:43 +00001802\end{cfuncdesc}
1803
1804
1805\subsection{Tuple Objects \label{tupleObjects}}
1806
1807\obindex{tuple}
1808\begin{ctypedesc}{PyTupleObject}
1809 This subtype of \ctype{PyObject} represents a Python tuple object.
1810\end{ctypedesc}
1811
1812\begin{cvardesc}{PyTypeObject}{PyTuple_Type}
1813 This instance of \ctype{PyTypeObject} represents the Python tuple
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001814 type; it is the same object as \code{tuple} and \code{types.TupleType}
1815 in the Python layer.\withsubitem{(in module types)}{\ttindex{TupleType}}.
Fred Drake3adf79e2001-10-12 19:01:43 +00001816\end{cvardesc}
1817
1818\begin{cfuncdesc}{int}{PyTuple_Check}{PyObject *p}
1819 Return true if \var{p} is a tuple object or an instance of a subtype
1820 of the tuple type.
1821 \versionchanged[Allowed subtypes to be accepted]{2.2}
1822\end{cfuncdesc}
1823
1824\begin{cfuncdesc}{int}{PyTuple_CheckExact}{PyObject *p}
1825 Return true if \var{p} is a tuple object, but not an instance of a
1826 subtype of the tuple type.
1827 \versionadded{2.2}
1828\end{cfuncdesc}
1829
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001830\begin{cfuncdesc}{PyObject*}{PyTuple_New}{Py_ssize_t len}
Fred Drake3adf79e2001-10-12 19:01:43 +00001831 Return a new tuple object of size \var{len}, or \NULL{} on failure.
1832\end{cfuncdesc}
1833
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001834\begin{cfuncdesc}{PyObject*}{PyTuple_Pack}{Py_ssize_t n, \moreargs}
Raymond Hettingercb2da432003-10-12 18:24:34 +00001835 Return a new tuple object of size \var{n}, or \NULL{} on failure.
1836 The tuple values are initialized to the subsequent \var{n} C arguments
1837 pointing to Python objects. \samp{PyTuple_Pack(2, \var{a}, \var{b})}
1838 is equivalent to \samp{Py_BuildValue("(OO)", \var{a}, \var{b})}.
Tim Peters9ddf40b2004-06-20 22:41:32 +00001839 \versionadded{2.4}
Raymond Hettingercb2da432003-10-12 18:24:34 +00001840\end{cfuncdesc}
1841
Fred Drake3adf79e2001-10-12 19:01:43 +00001842\begin{cfuncdesc}{int}{PyTuple_Size}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00001843 Take a pointer to a tuple object, and return the size of that
Fred Drake3adf79e2001-10-12 19:01:43 +00001844 tuple.
1845\end{cfuncdesc}
1846
1847\begin{cfuncdesc}{int}{PyTuple_GET_SIZE}{PyObject *p}
1848 Return the size of the tuple \var{p}, which must be non-\NULL{} and
1849 point to a tuple; no error checking is performed.
1850\end{cfuncdesc}
1851
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001852\begin{cfuncdesc}{PyObject*}{PyTuple_GetItem}{PyObject *p, Py_ssize_t pos}
Georg Brandl99363b62005-09-03 07:27:26 +00001853 Return the object at position \var{pos} in the tuple pointed to by
1854 \var{p}. If \var{pos} is out of bounds, return \NULL{} and sets an
Fred Drake3adf79e2001-10-12 19:01:43 +00001855 \exception{IndexError} exception.
1856\end{cfuncdesc}
1857
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001858\begin{cfuncdesc}{PyObject*}{PyTuple_GET_ITEM}{PyObject *p, Py_ssize_t pos}
Fred Drake3adf79e2001-10-12 19:01:43 +00001859 Like \cfunction{PyTuple_GetItem()}, but does no checking of its
1860 arguments.
1861\end{cfuncdesc}
1862
1863\begin{cfuncdesc}{PyObject*}{PyTuple_GetSlice}{PyObject *p,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001864 Py_ssize_t low, Py_ssize_t high}
Georg Brandl99363b62005-09-03 07:27:26 +00001865 Take a slice of the tuple pointed to by \var{p} from \var{low} to
1866 \var{high} and return it as a new tuple.
Fred Drake3adf79e2001-10-12 19:01:43 +00001867\end{cfuncdesc}
1868
1869\begin{cfuncdesc}{int}{PyTuple_SetItem}{PyObject *p,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001870 Py_ssize_t pos, PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +00001871 Insert a reference to object \var{o} at position \var{pos} of the
1872 tuple pointed to by \var{p}. Return \code{0} on success.
Fred Drake3adf79e2001-10-12 19:01:43 +00001873 \note{This function ``steals'' a reference to \var{o}.}
1874\end{cfuncdesc}
1875
1876\begin{cfuncdesc}{void}{PyTuple_SET_ITEM}{PyObject *p,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001877 Py_ssize_t pos, PyObject *o}
Fred Drake3adf79e2001-10-12 19:01:43 +00001878 Like \cfunction{PyTuple_SetItem()}, but does no error checking, and
1879 should \emph{only} be used to fill in brand new tuples. \note{This
1880 function ``steals'' a reference to \var{o}.}
1881\end{cfuncdesc}
1882
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001883\begin{cfuncdesc}{int}{_PyTuple_Resize}{PyObject **p, Py_ssize_t newsize}
Fred Drake3adf79e2001-10-12 19:01:43 +00001884 Can be used to resize a tuple. \var{newsize} will be the new length
1885 of the tuple. Because tuples are \emph{supposed} to be immutable,
1886 this should only be used if there is only one reference to the
1887 object. Do \emph{not} use this if the tuple may already be known to
1888 some other part of the code. The tuple will always grow or shrink
1889 at the end. Think of this as destroying the old tuple and creating
1890 a new one, only more efficiently. Returns \code{0} on success.
1891 Client code should never assume that the resulting value of
1892 \code{*\var{p}} will be the same as before calling this function.
1893 If the object referenced by \code{*\var{p}} is replaced, the
1894 original \code{*\var{p}} is destroyed. On failure, returns
Tim Peters9ddf40b2004-06-20 22:41:32 +00001895 \code{-1} and sets \code{*\var{p}} to \NULL{}, and raises
Fred Drake3adf79e2001-10-12 19:01:43 +00001896 \exception{MemoryError} or
1897 \exception{SystemError}.
Tim Petersf582b822001-12-11 18:51:08 +00001898 \versionchanged[Removed unused third parameter, \var{last_is_sticky}]{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +00001899\end{cfuncdesc}
1900
1901
1902\subsection{List Objects \label{listObjects}}
1903
1904\obindex{list}
1905\begin{ctypedesc}{PyListObject}
1906 This subtype of \ctype{PyObject} represents a Python list object.
1907\end{ctypedesc}
1908
1909\begin{cvardesc}{PyTypeObject}{PyList_Type}
1910 This instance of \ctype{PyTypeObject} represents the Python list
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001911 type. This is the same object as \code{list} and \code{types.ListType}
1912 in the Python layer.\withsubitem{(in module types)}{\ttindex{ListType}}
Fred Drake3adf79e2001-10-12 19:01:43 +00001913\end{cvardesc}
1914
1915\begin{cfuncdesc}{int}{PyList_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00001916 Return true if \var{p} is a list object or an instance of a
Andrew MacIntyre13cd8892003-12-25 23:57:52 +00001917 subtype of the list type.
1918 \versionchanged[Allowed subtypes to be accepted]{2.2}
1919\end{cfuncdesc}
1920
1921\begin{cfuncdesc}{int}{PyList_CheckExact}{PyObject *p}
1922 Return true if \var{p} is a list object, but not an instance of a
1923 subtype of the list type.
1924 \versionadded{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +00001925\end{cfuncdesc}
1926
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001927\begin{cfuncdesc}{PyObject*}{PyList_New}{Py_ssize_t len}
Georg Brandl99363b62005-09-03 07:27:26 +00001928 Return a new list of length \var{len} on success, or \NULL{} on
Fred Drake3adf79e2001-10-12 19:01:43 +00001929 failure.
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001930 \note{If \var{length} is greater than zero, the returned list object's
1931 items are set to \code{NULL}. Thus you cannot use abstract
1932 API functions such as \cfunction{PySequence_SetItem()}
1933 or expose the object to Python code before setting all items to a
1934 real object with \cfunction{PyList_SetItem()}.}
Fred Drake3adf79e2001-10-12 19:01:43 +00001935\end{cfuncdesc}
1936
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001937\begin{cfuncdesc}{Py_ssize_t}{PyList_Size}{PyObject *list}
Georg Brandl99363b62005-09-03 07:27:26 +00001938 Return the length of the list object in \var{list}; this is
Fred Drake3adf79e2001-10-12 19:01:43 +00001939 equivalent to \samp{len(\var{list})} on a list object.
1940 \bifuncindex{len}
1941\end{cfuncdesc}
1942
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001943\begin{cfuncdesc}{Py_ssize_t}{PyList_GET_SIZE}{PyObject *list}
Fred Drake3adf79e2001-10-12 19:01:43 +00001944 Macro form of \cfunction{PyList_Size()} without error checking.
1945\end{cfuncdesc}
1946
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001947\begin{cfuncdesc}{PyObject*}{PyList_GetItem}{PyObject *list, Py_ssize_t index}
Georg Brandl99363b62005-09-03 07:27:26 +00001948 Return the object at position \var{pos} in the list pointed to by
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001949 \var{p}. The position must be positive, indexing from the end of the
1950 list is not supported. If \var{pos} is out of bounds, return \NULL{}
1951 and set an \exception{IndexError} exception.
Fred Drake3adf79e2001-10-12 19:01:43 +00001952\end{cfuncdesc}
1953
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001954\begin{cfuncdesc}{PyObject*}{PyList_GET_ITEM}{PyObject *list, Py_ssize_t i}
Fred Drake3adf79e2001-10-12 19:01:43 +00001955 Macro form of \cfunction{PyList_GetItem()} without error checking.
1956\end{cfuncdesc}
1957
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001958\begin{cfuncdesc}{int}{PyList_SetItem}{PyObject *list, Py_ssize_t index,
Fred Drake3adf79e2001-10-12 19:01:43 +00001959 PyObject *item}
Georg Brandl99363b62005-09-03 07:27:26 +00001960 Set the item at index \var{index} in list to \var{item}. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00001961 \code{0} on success or \code{-1} on failure. \note{This function
1962 ``steals'' a reference to \var{item} and discards a reference to an
1963 item already in the list at the affected position.}
1964\end{cfuncdesc}
1965
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001966\begin{cfuncdesc}{void}{PyList_SET_ITEM}{PyObject *list, Py_ssize_t i,
Fred Drake3adf79e2001-10-12 19:01:43 +00001967 PyObject *o}
1968 Macro form of \cfunction{PyList_SetItem()} without error checking.
1969 This is normally only used to fill in new lists where there is no
1970 previous content.
1971 \note{This function ``steals'' a reference to \var{item}, and,
1972 unlike \cfunction{PyList_SetItem()}, does \emph{not} discard a
1973 reference to any item that it being replaced; any reference in
1974 \var{list} at position \var{i} will be leaked.}
1975\end{cfuncdesc}
1976
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001977\begin{cfuncdesc}{int}{PyList_Insert}{PyObject *list, Py_ssize_t index,
Fred Drake3adf79e2001-10-12 19:01:43 +00001978 PyObject *item}
Georg Brandl99363b62005-09-03 07:27:26 +00001979 Insert the item \var{item} into list \var{list} in front of index
1980 \var{index}. Return \code{0} if successful; return \code{-1} and
1981 set an exception if unsuccessful. Analogous to
Fred Drake3adf79e2001-10-12 19:01:43 +00001982 \code{\var{list}.insert(\var{index}, \var{item})}.
1983\end{cfuncdesc}
1984
1985\begin{cfuncdesc}{int}{PyList_Append}{PyObject *list, PyObject *item}
Georg Brandl99363b62005-09-03 07:27:26 +00001986 Append the object \var{item} at the end of list \var{list}.
1987 Return \code{0} if successful; return \code{-1} and set an
Fred Drake3adf79e2001-10-12 19:01:43 +00001988 exception if unsuccessful. Analogous to
1989 \code{\var{list}.append(\var{item})}.
1990\end{cfuncdesc}
1991
1992\begin{cfuncdesc}{PyObject*}{PyList_GetSlice}{PyObject *list,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001993 Py_ssize_t low, Py_ssize_t high}
Georg Brandl99363b62005-09-03 07:27:26 +00001994 Return a list of the objects in \var{list} containing the objects
1995 \emph{between} \var{low} and \var{high}. Return \NULL{} and set
Fred Drake3adf79e2001-10-12 19:01:43 +00001996 an exception if unsuccessful.
1997 Analogous to \code{\var{list}[\var{low}:\var{high}]}.
1998\end{cfuncdesc}
1999
2000\begin{cfuncdesc}{int}{PyList_SetSlice}{PyObject *list,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002001 Py_ssize_t low, Py_ssize_t high,
Fred Drake3adf79e2001-10-12 19:01:43 +00002002 PyObject *itemlist}
Georg Brandl99363b62005-09-03 07:27:26 +00002003 Set the slice of \var{list} between \var{low} and \var{high} to the
Fred Drake3adf79e2001-10-12 19:01:43 +00002004 contents of \var{itemlist}. Analogous to
Raymond Hettinger9c7ed4c2003-10-26 17:20:07 +00002005 \code{\var{list}[\var{low}:\var{high}] = \var{itemlist}}.
2006 The \var{itemlist} may be \NULL{}, indicating the assignment
2007 of an empty list (slice deletion).
Georg Brandl99363b62005-09-03 07:27:26 +00002008 Return \code{0} on success, \code{-1} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00002009\end{cfuncdesc}
2010
2011\begin{cfuncdesc}{int}{PyList_Sort}{PyObject *list}
Georg Brandl99363b62005-09-03 07:27:26 +00002012 Sort the items of \var{list} in place. Return \code{0} on
Fred Drake3adf79e2001-10-12 19:01:43 +00002013 success, \code{-1} on failure. This is equivalent to
2014 \samp{\var{list}.sort()}.
2015\end{cfuncdesc}
2016
2017\begin{cfuncdesc}{int}{PyList_Reverse}{PyObject *list}
Georg Brandl99363b62005-09-03 07:27:26 +00002018 Reverse the items of \var{list} in place. Return \code{0} on
Fred Drake3adf79e2001-10-12 19:01:43 +00002019 success, \code{-1} on failure. This is the equivalent of
2020 \samp{\var{list}.reverse()}.
2021\end{cfuncdesc}
2022
2023\begin{cfuncdesc}{PyObject*}{PyList_AsTuple}{PyObject *list}
Georg Brandl99363b62005-09-03 07:27:26 +00002024 Return a new tuple object containing the contents of \var{list};
Fred Drake3adf79e2001-10-12 19:01:43 +00002025 equivalent to \samp{tuple(\var{list})}.\bifuncindex{tuple}
2026\end{cfuncdesc}
2027
2028
2029\section{Mapping Objects \label{mapObjects}}
2030
2031\obindex{mapping}
2032
2033
2034\subsection{Dictionary Objects \label{dictObjects}}
2035
2036\obindex{dictionary}
2037\begin{ctypedesc}{PyDictObject}
2038 This subtype of \ctype{PyObject} represents a Python dictionary
2039 object.
2040\end{ctypedesc}
2041
2042\begin{cvardesc}{PyTypeObject}{PyDict_Type}
2043 This instance of \ctype{PyTypeObject} represents the Python
2044 dictionary type. This is exposed to Python programs as
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002045 \code{dict} and \code{types.DictType}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002046 \withsubitem{(in module types)}{\ttindex{DictType}\ttindex{DictionaryType}}
2047\end{cvardesc}
2048
2049\begin{cfuncdesc}{int}{PyDict_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002050 Return true if \var{p} is a dict object or an instance of a
Andrew MacIntyre13cd8892003-12-25 23:57:52 +00002051 subtype of the dict type.
2052 \versionchanged[Allowed subtypes to be accepted]{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +00002053\end{cfuncdesc}
2054
Andrew MacIntyref72af652003-12-26 00:07:51 +00002055\begin{cfuncdesc}{int}{PyDict_CheckExact}{PyObject *p}
2056 Return true if \var{p} is a dict object, but not an instance of a
2057 subtype of the dict type.
2058 \versionadded{2.4}
2059\end{cfuncdesc}
2060
Fred Drake3adf79e2001-10-12 19:01:43 +00002061\begin{cfuncdesc}{PyObject*}{PyDict_New}{}
Georg Brandl99363b62005-09-03 07:27:26 +00002062 Return a new empty dictionary, or \NULL{} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00002063\end{cfuncdesc}
2064
2065\begin{cfuncdesc}{PyObject*}{PyDictProxy_New}{PyObject *dict}
2066 Return a proxy object for a mapping which enforces read-only
2067 behavior. This is normally used to create a proxy to prevent
2068 modification of the dictionary for non-dynamic class types.
2069 \versionadded{2.2}
2070\end{cfuncdesc}
2071
2072\begin{cfuncdesc}{void}{PyDict_Clear}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002073 Empty an existing dictionary of all key-value pairs.
Fred Drake3adf79e2001-10-12 19:01:43 +00002074\end{cfuncdesc}
2075
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00002076\begin{cfuncdesc}{int}{PyDict_Contains}{PyObject *p, PyObject *key}
2077 Determine if dictionary \var{p} contains \var{key}. If an item
2078 in \var{p} is matches \var{key}, return \code{1}, otherwise return
2079 \code{0}. On error, return \code{-1}. This is equivalent to the
2080 Python expression \samp{\var{key} in \var{p}}.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002081 \versionadded{2.4}
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00002082\end{cfuncdesc}
2083
Fred Drake3adf79e2001-10-12 19:01:43 +00002084\begin{cfuncdesc}{PyObject*}{PyDict_Copy}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002085 Return a new dictionary that contains the same key-value pairs as
Fred Drake3adf79e2001-10-12 19:01:43 +00002086 \var{p}.
2087 \versionadded{1.6}
2088\end{cfuncdesc}
2089
2090\begin{cfuncdesc}{int}{PyDict_SetItem}{PyObject *p, PyObject *key,
2091 PyObject *val}
Georg Brandl99363b62005-09-03 07:27:26 +00002092 Insert \var{value} into the dictionary \var{p} with a key of
Fred Drake3adf79e2001-10-12 19:01:43 +00002093 \var{key}. \var{key} must be hashable; if it isn't,
2094 \exception{TypeError} will be raised.
Georg Brandl99363b62005-09-03 07:27:26 +00002095 Return \code{0} on success or \code{-1} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00002096\end{cfuncdesc}
2097
2098\begin{cfuncdesc}{int}{PyDict_SetItemString}{PyObject *p,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002099 const char *key,
Fred Drake3adf79e2001-10-12 19:01:43 +00002100 PyObject *val}
Georg Brandl99363b62005-09-03 07:27:26 +00002101 Insert \var{value} into the dictionary \var{p} using \var{key} as a
Fred Drake3adf79e2001-10-12 19:01:43 +00002102 key. \var{key} should be a \ctype{char*}. The key object is created
Georg Brandl99363b62005-09-03 07:27:26 +00002103 using \code{PyString_FromString(\var{key})}. Return \code{0} on
Fred Drake3adf79e2001-10-12 19:01:43 +00002104 success or \code{-1} on failure.
2105 \ttindex{PyString_FromString()}
2106\end{cfuncdesc}
2107
2108\begin{cfuncdesc}{int}{PyDict_DelItem}{PyObject *p, PyObject *key}
Georg Brandl99363b62005-09-03 07:27:26 +00002109 Remove the entry in dictionary \var{p} with key \var{key}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002110 \var{key} must be hashable; if it isn't, \exception{TypeError} is
Georg Brandl99363b62005-09-03 07:27:26 +00002111 raised. Return \code{0} on success or \code{-1} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00002112\end{cfuncdesc}
2113
2114\begin{cfuncdesc}{int}{PyDict_DelItemString}{PyObject *p, char *key}
Georg Brandl99363b62005-09-03 07:27:26 +00002115 Remove the entry in dictionary \var{p} which has a key specified by
2116 the string \var{key}. Return \code{0} on success or \code{-1} on
Fred Drake3adf79e2001-10-12 19:01:43 +00002117 failure.
2118\end{cfuncdesc}
2119
2120\begin{cfuncdesc}{PyObject*}{PyDict_GetItem}{PyObject *p, PyObject *key}
Georg Brandl99363b62005-09-03 07:27:26 +00002121 Return the object from dictionary \var{p} which has a key
2122 \var{key}. Return \NULL{} if the key \var{key} is not present, but
Fred Drake3adf79e2001-10-12 19:01:43 +00002123 \emph{without} setting an exception.
2124\end{cfuncdesc}
2125
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002126\begin{cfuncdesc}{PyObject*}{PyDict_GetItemString}{PyObject *p, const char *key}
Fred Drake3adf79e2001-10-12 19:01:43 +00002127 This is the same as \cfunction{PyDict_GetItem()}, but \var{key} is
2128 specified as a \ctype{char*}, rather than a \ctype{PyObject*}.
2129\end{cfuncdesc}
2130
2131\begin{cfuncdesc}{PyObject*}{PyDict_Items}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002132 Return a \ctype{PyListObject} containing all the items from the
Nicholas Bastin975e7252004-09-29 21:39:26 +00002133 dictionary, as in the dictionary method \method{items()} (see the
Fred Drake3adf79e2001-10-12 19:01:43 +00002134 \citetitle[../lib/lib.html]{Python Library Reference}).
2135\end{cfuncdesc}
2136
2137\begin{cfuncdesc}{PyObject*}{PyDict_Keys}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002138 Return a \ctype{PyListObject} containing all the keys from the
Fred Drake3adf79e2001-10-12 19:01:43 +00002139 dictionary, as in the dictionary method \method{keys()} (see the
2140 \citetitle[../lib/lib.html]{Python Library Reference}).
2141\end{cfuncdesc}
2142
2143\begin{cfuncdesc}{PyObject*}{PyDict_Values}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002144 Return a \ctype{PyListObject} containing all the values from the
Fred Drake3adf79e2001-10-12 19:01:43 +00002145 dictionary \var{p}, as in the dictionary method \method{values()}
2146 (see the \citetitle[../lib/lib.html]{Python Library Reference}).
2147\end{cfuncdesc}
2148
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002149\begin{cfuncdesc}{Py_ssize_t}{PyDict_Size}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002150 Return the number of items in the dictionary. This is equivalent
Fred Drake3adf79e2001-10-12 19:01:43 +00002151 to \samp{len(\var{p})} on a dictionary.\bifuncindex{len}
2152\end{cfuncdesc}
2153
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002154\begin{cfuncdesc}{int}{PyDict_Next}{PyObject *p, Py_ssize_t *ppos,
Fred Drake3adf79e2001-10-12 19:01:43 +00002155 PyObject **pkey, PyObject **pvalue}
2156 Iterate over all key-value pairs in the dictionary \var{p}. The
2157 \ctype{int} referred to by \var{ppos} must be initialized to
2158 \code{0} prior to the first call to this function to start the
2159 iteration; the function returns true for each pair in the
2160 dictionary, and false once all pairs have been reported. The
2161 parameters \var{pkey} and \var{pvalue} should either point to
2162 \ctype{PyObject*} variables that will be filled in with each key and
Tim Peters9ddf40b2004-06-20 22:41:32 +00002163 value, respectively, or may be \NULL{}. Any references returned through
Raymond Hettinger54693242003-12-13 19:48:41 +00002164 them are borrowed. \var{ppos} should not be altered during iteration.
2165 Its value represents offsets within the internal dictionary structure,
2166 and since the structure is sparse, the offsets are not consecutive.
Fred Drake3adf79e2001-10-12 19:01:43 +00002167
2168 For example:
2169
2170\begin{verbatim}
2171PyObject *key, *value;
Thomas Woutersb2137042007-02-01 18:02:27 +00002172Py_ssize_t pos = 0;
Fred Drake3adf79e2001-10-12 19:01:43 +00002173
2174while (PyDict_Next(self->dict, &pos, &key, &value)) {
2175 /* do something interesting with the values... */
2176 ...
2177}
2178\end{verbatim}
2179
2180 The dictionary \var{p} should not be mutated during iteration. It
2181 is safe (since Python 2.1) to modify the values of the keys as you
2182 iterate over the dictionary, but only so long as the set of keys
2183 does not change. For example:
2184
2185\begin{verbatim}
2186PyObject *key, *value;
Thomas Woutersb2137042007-02-01 18:02:27 +00002187Py_ssize_t pos = 0;
Fred Drake3adf79e2001-10-12 19:01:43 +00002188
2189while (PyDict_Next(self->dict, &pos, &key, &value)) {
2190 int i = PyInt_AS_LONG(value) + 1;
2191 PyObject *o = PyInt_FromLong(i);
2192 if (o == NULL)
2193 return -1;
2194 if (PyDict_SetItem(self->dict, key, o) < 0) {
2195 Py_DECREF(o);
2196 return -1;
2197 }
2198 Py_DECREF(o);
2199}
2200\end{verbatim}
2201\end{cfuncdesc}
2202
2203\begin{cfuncdesc}{int}{PyDict_Merge}{PyObject *a, PyObject *b, int override}
Tim Petersf582b822001-12-11 18:51:08 +00002204 Iterate over mapping object \var{b} adding key-value pairs to dictionary
2205 \var{a}.
2206 \var{b} may be a dictionary, or any object supporting
2207 \function{PyMapping_Keys()} and \function{PyObject_GetItem()}.
2208 If \var{override} is true, existing pairs in \var{a} will
Fred Drake3adf79e2001-10-12 19:01:43 +00002209 be replaced if a matching key is found in \var{b}, otherwise pairs
2210 will only be added if there is not a matching key in \var{a}.
Tim Petersf582b822001-12-11 18:51:08 +00002211 Return \code{0} on success or \code{-1} if an exception was
Fred Drake3adf79e2001-10-12 19:01:43 +00002212 raised.
2213\versionadded{2.2}
2214\end{cfuncdesc}
2215
2216\begin{cfuncdesc}{int}{PyDict_Update}{PyObject *a, PyObject *b}
2217 This is the same as \code{PyDict_Merge(\var{a}, \var{b}, 1)} in C,
Tim Petersf582b822001-12-11 18:51:08 +00002218 or \code{\var{a}.update(\var{b})} in Python. Return \code{0} on
Fred Drake3adf79e2001-10-12 19:01:43 +00002219 success or \code{-1} if an exception was raised.
2220 \versionadded{2.2}
2221\end{cfuncdesc}
2222
Tim Petersf582b822001-12-11 18:51:08 +00002223\begin{cfuncdesc}{int}{PyDict_MergeFromSeq2}{PyObject *a, PyObject *seq2,
2224 int override}
2225 Update or merge into dictionary \var{a}, from the key-value pairs in
2226 \var{seq2}. \var{seq2} must be an iterable object producing
2227 iterable objects of length 2, viewed as key-value pairs. In case of
2228 duplicate keys, the last wins if \var{override} is true, else the
2229 first wins.
2230 Return \code{0} on success or \code{-1} if an exception
2231 was raised.
2232 Equivalent Python (except for the return value):
2233
2234\begin{verbatim}
2235def PyDict_MergeFromSeq2(a, seq2, override):
2236 for key, value in seq2:
2237 if override or key not in a:
2238 a[key] = value
2239\end{verbatim}
2240
2241 \versionadded{2.2}
2242\end{cfuncdesc}
Fred Drake3adf79e2001-10-12 19:01:43 +00002243
Fred Drake54e62942001-12-11 19:40:16 +00002244
Fred Drake3adf79e2001-10-12 19:01:43 +00002245\section{Other Objects \label{otherObjects}}
2246
Guido van Rossumd8faa362007-04-27 19:54:29 +00002247\subsection{Class Objects \label{classObjects}}
2248
2249\obindex{class}
2250Note that the class objects described here represent old-style classes,
2251which will go away in Python 3. When creating new types for extension
2252modules, you will want to work with type objects (section
2253\ref{typeObjects}).
2254
2255\begin{ctypedesc}{PyClassObject}
2256 The C structure of the objects used to describe built-in classes.
2257\end{ctypedesc}
2258
2259\begin{cvardesc}{PyObject*}{PyClass_Type}
2260 This is the type object for class objects; it is the same object as
2261 \code{types.ClassType} in the Python layer.
2262 \withsubitem{(in module types)}{\ttindex{ClassType}}
2263\end{cvardesc}
2264
2265\begin{cfuncdesc}{int}{PyClass_Check}{PyObject *o}
2266 Return true if the object \var{o} is a class object, including
2267 instances of types derived from the standard class object. Return
2268 false in all other cases.
2269\end{cfuncdesc}
2270
2271\begin{cfuncdesc}{int}{PyClass_IsSubclass}{PyObject *klass, PyObject *base}
2272 Return true if \var{klass} is a subclass of \var{base}. Return false in
2273 all other cases.
2274\end{cfuncdesc}
2275
Fred Drake3adf79e2001-10-12 19:01:43 +00002276\subsection{File Objects \label{fileObjects}}
2277
2278\obindex{file}
2279Python's built-in file objects are implemented entirely on the
2280\ctype{FILE*} support from the C standard library. This is an
2281implementation detail and may change in future releases of Python.
2282
2283\begin{ctypedesc}{PyFileObject}
2284 This subtype of \ctype{PyObject} represents a Python file object.
2285\end{ctypedesc}
2286
2287\begin{cvardesc}{PyTypeObject}{PyFile_Type}
2288 This instance of \ctype{PyTypeObject} represents the Python file
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002289 type. This is exposed to Python programs as \code{file} and
2290 \code{types.FileType}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002291 \withsubitem{(in module types)}{\ttindex{FileType}}
2292\end{cvardesc}
2293
2294\begin{cfuncdesc}{int}{PyFile_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002295 Return true if its argument is a \ctype{PyFileObject} or a subtype
Fred Drake3adf79e2001-10-12 19:01:43 +00002296 of \ctype{PyFileObject}.
2297 \versionchanged[Allowed subtypes to be accepted]{2.2}
2298\end{cfuncdesc}
2299
2300\begin{cfuncdesc}{int}{PyFile_CheckExact}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002301 Return true if its argument is a \ctype{PyFileObject}, but not a
Fred Drake3adf79e2001-10-12 19:01:43 +00002302 subtype of \ctype{PyFileObject}.
2303 \versionadded{2.2}
2304\end{cfuncdesc}
2305
2306\begin{cfuncdesc}{PyObject*}{PyFile_FromString}{char *filename, char *mode}
Georg Brandl99363b62005-09-03 07:27:26 +00002307 On success, return a new file object that is opened on the file
Fred Drake3adf79e2001-10-12 19:01:43 +00002308 given by \var{filename}, with a file mode given by \var{mode}, where
2309 \var{mode} has the same semantics as the standard C routine
Georg Brandl99363b62005-09-03 07:27:26 +00002310 \cfunction{fopen()}\ttindex{fopen()}. On failure, return \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002311\end{cfuncdesc}
2312
2313\begin{cfuncdesc}{PyObject*}{PyFile_FromFile}{FILE *fp,
2314 char *name, char *mode,
2315 int (*close)(FILE*)}
Georg Brandl99363b62005-09-03 07:27:26 +00002316 Create a new \ctype{PyFileObject} from the already-open standard C
Fred Drake3adf79e2001-10-12 19:01:43 +00002317 file pointer, \var{fp}. The function \var{close} will be called
Georg Brandl99363b62005-09-03 07:27:26 +00002318 when the file should be closed. Return \NULL{} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00002319\end{cfuncdesc}
2320
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002321\begin{cfuncdesc}{FILE*}{PyFile_AsFile}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002322 Return the file object associated with \var{p} as a \ctype{FILE*}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002323\end{cfuncdesc}
2324
2325\begin{cfuncdesc}{PyObject*}{PyFile_GetLine}{PyObject *p, int n}
2326 Equivalent to \code{\var{p}.readline(\optional{\var{n}})}, this
2327 function reads one line from the object \var{p}. \var{p} may be a
2328 file object or any object with a \method{readline()} method. If
2329 \var{n} is \code{0}, exactly one line is read, regardless of the
2330 length of the line. If \var{n} is greater than \code{0}, no more
2331 than \var{n} bytes will be read from the file; a partial line can be
2332 returned. In both cases, an empty string is returned if the end of
2333 the file is reached immediately. If \var{n} is less than \code{0},
2334 however, one line is read regardless of length, but
2335 \exception{EOFError} is raised if the end of the file is reached
2336 immediately.
2337 \withsubitem{(built-in exception)}{\ttindex{EOFError}}
2338\end{cfuncdesc}
2339
2340\begin{cfuncdesc}{PyObject*}{PyFile_Name}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002341 Return the name of the file specified by \var{p} as a string
Fred Drake3adf79e2001-10-12 19:01:43 +00002342 object.
2343\end{cfuncdesc}
2344
2345\begin{cfuncdesc}{void}{PyFile_SetBufSize}{PyFileObject *p, int n}
2346 Available on systems with \cfunction{setvbuf()}\ttindex{setvbuf()}
2347 only. This should only be called immediately after file object
2348 creation.
2349\end{cfuncdesc}
2350
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00002351\begin{cfuncdesc}{int}{PyFile_Encoding}{PyFileObject *p, char *enc}
2352 Set the file's encoding for Unicode output to \var{enc}. Return
2353 1 on success and 0 on failure.
2354 \versionadded{2.3}
2355\end{cfuncdesc}
2356
Fred Drake3adf79e2001-10-12 19:01:43 +00002357\begin{cfuncdesc}{int}{PyFile_SoftSpace}{PyObject *p, int newflag}
Georg Brandl99363b62005-09-03 07:27:26 +00002358 This function exists for internal use by the interpreter. Set the
Fred Drake3adf79e2001-10-12 19:01:43 +00002359 \member{softspace} attribute of \var{p} to \var{newflag} and
Georg Brandl99363b62005-09-03 07:27:26 +00002360 \withsubitem{(file attribute)}{\ttindex{softspace}}return the
Fred Drake3adf79e2001-10-12 19:01:43 +00002361 previous value. \var{p} does not have to be a file object for this
2362 function to work properly; any object is supported (thought its only
2363 interesting if the \member{softspace} attribute can be set). This
2364 function clears any errors, and will return \code{0} as the previous
2365 value if the attribute either does not exist or if there were errors
2366 in retrieving it. There is no way to detect errors from this
2367 function, but doing so should not be needed.
2368\end{cfuncdesc}
2369
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002370\begin{cfuncdesc}{int}{PyFile_WriteObject}{PyObject *obj, PyObject *p,
Fred Drake3adf79e2001-10-12 19:01:43 +00002371 int flags}
Georg Brandl99363b62005-09-03 07:27:26 +00002372 Write object \var{obj} to file object \var{p}. The only supported
Fred Drake3adf79e2001-10-12 19:01:43 +00002373 flag for \var{flags} is
2374 \constant{Py_PRINT_RAW}\ttindex{Py_PRINT_RAW}; if given, the
2375 \function{str()} of the object is written instead of the
Georg Brandl99363b62005-09-03 07:27:26 +00002376 \function{repr()}. Return \code{0} on success or \code{-1} on
Fred Drake3adf79e2001-10-12 19:01:43 +00002377 failure; the appropriate exception will be set.
2378\end{cfuncdesc}
2379
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002380\begin{cfuncdesc}{int}{PyFile_WriteString}{const char *s, PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002381 Write string \var{s} to file object \var{p}. Return \code{0} on
Fred Drake3adf79e2001-10-12 19:01:43 +00002382 success or \code{-1} on failure; the appropriate exception will be
2383 set.
2384\end{cfuncdesc}
2385
2386
2387\subsection{Instance Objects \label{instanceObjects}}
2388
2389\obindex{instance}
2390There are very few functions specific to instance objects.
2391
2392\begin{cvardesc}{PyTypeObject}{PyInstance_Type}
2393 Type object for class instances.
2394\end{cvardesc}
2395
2396\begin{cfuncdesc}{int}{PyInstance_Check}{PyObject *obj}
Georg Brandl99363b62005-09-03 07:27:26 +00002397 Return true if \var{obj} is an instance.
Fred Drake3adf79e2001-10-12 19:01:43 +00002398\end{cfuncdesc}
2399
2400\begin{cfuncdesc}{PyObject*}{PyInstance_New}{PyObject *class,
2401 PyObject *arg,
2402 PyObject *kw}
2403 Create a new instance of a specific class. The parameters \var{arg}
2404 and \var{kw} are used as the positional and keyword parameters to
2405 the object's constructor.
2406\end{cfuncdesc}
2407
2408\begin{cfuncdesc}{PyObject*}{PyInstance_NewRaw}{PyObject *class,
2409 PyObject *dict}
Neil Schemenauerc4932292005-06-18 17:54:13 +00002410 Create a new instance of a specific class without calling its
Fred Drake3adf79e2001-10-12 19:01:43 +00002411 constructor. \var{class} is the class of new object. The
2412 \var{dict} parameter will be used as the object's \member{__dict__};
Tim Peters9ddf40b2004-06-20 22:41:32 +00002413 if \NULL{}, a new dictionary will be created for the instance.
Fred Drake3adf79e2001-10-12 19:01:43 +00002414\end{cfuncdesc}
2415
2416
Georg Brandl9b743f52006-02-20 12:57:53 +00002417\subsection{Function Objects \label{function-objects}}
2418
2419\obindex{function}
2420There are a few functions specific to Python functions.
2421
2422\begin{ctypedesc}{PyFunctionObject}
2423 The C structure used for functions.
2424\end{ctypedesc}
2425
2426\begin{cvardesc}{PyTypeObject}{PyFunction_Type}
2427 This is an instance of \ctype{PyTypeObject} and represents the
2428 Python function type. It is exposed to Python programmers as
2429 \code{types.FunctionType}.
2430 \withsubitem{(in module types)}{\ttindex{MethodType}}
2431\end{cvardesc}
2432
2433\begin{cfuncdesc}{int}{PyFunction_Check}{PyObject *o}
2434 Return true if \var{o} is a function object (has type
2435 \cdata{PyFunction_Type}). The parameter must not be \NULL{}.
2436\end{cfuncdesc}
2437
2438\begin{cfuncdesc}{PyObject*}{PyFunction_New}{PyObject *code,
2439 PyObject *globals}
2440 Return a new function object associated with the code object
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002441 \var{code}. \var{globals} must be a dictionary with the global
2442 variables accessible to the function.
Georg Brandl9b743f52006-02-20 12:57:53 +00002443
2444 The function's docstring, name and \var{__module__} are retrieved
2445 from the code object, the argument defaults and closure are set to
2446 \NULL{}.
2447\end{cfuncdesc}
2448
2449\begin{cfuncdesc}{PyObject*}{PyFunction_GetCode}{PyObject *op}
2450 Return the code object associated with the function object \var{op}.
2451\end{cfuncdesc}
2452
2453\begin{cfuncdesc}{PyObject*}{PyFunction_GetGlobals}{PyObject *op}
2454 Return the globals dictionary associated with the function object
2455 \var{op}.
2456\end{cfuncdesc}
2457
2458\begin{cfuncdesc}{PyObject*}{PyFunction_GetModule}{PyObject *op}
2459 Return the \var{__module__} attribute of the function object \var{op}.
2460 This is normally a string containing the module name, but can be set
2461 to any other object by Python code.
2462\end{cfuncdesc}
2463
2464\begin{cfuncdesc}{PyObject*}{PyFunction_GetDefaults}{PyObject *op}
2465 Return the argument default values of the function object \var{op}.
2466 This can be a tuple of arguments or \NULL{}.
2467\end{cfuncdesc}
2468
2469\begin{cfuncdesc}{int}{PyFunction_SetDefaults}{PyObject *op,
2470 PyObject *defaults}
2471 Set the argument default values for the function object \var{op}.
2472 \var{defaults} must be \var{Py_None} or a tuple.
2473
2474 Raises \exception{SystemError} and returns \code{-1} on failure.
2475\end{cfuncdesc}
2476
2477\begin{cfuncdesc}{PyObject*}{PyFunction_GetClosure}{PyObject *op}
2478 Return the closure associated with the function object \var{op}.
2479 This can be \NULL{} or a tuple of cell objects.
2480\end{cfuncdesc}
2481
2482\begin{cfuncdesc}{int}{PyFunction_SetClosure}{PyObject *op,
2483 PyObject *closure}
2484 Set the closure associated with the function object \var{op}.
2485 \var{closure} must be \var{Py_None} or a tuple of cell objects.
2486
2487 Raises \exception{SystemError} and returns \code{-1} on failure.
2488\end{cfuncdesc}
2489
2490
Fred Drake3adf79e2001-10-12 19:01:43 +00002491\subsection{Method Objects \label{method-objects}}
2492
2493\obindex{method}
2494There are some useful functions that are useful for working with
2495method objects.
2496
2497\begin{cvardesc}{PyTypeObject}{PyMethod_Type}
2498 This instance of \ctype{PyTypeObject} represents the Python method
2499 type. This is exposed to Python programs as \code{types.MethodType}.
2500 \withsubitem{(in module types)}{\ttindex{MethodType}}
2501\end{cvardesc}
2502
2503\begin{cfuncdesc}{int}{PyMethod_Check}{PyObject *o}
2504 Return true if \var{o} is a method object (has type
Tim Peters9ddf40b2004-06-20 22:41:32 +00002505 \cdata{PyMethod_Type}). The parameter must not be \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002506\end{cfuncdesc}
2507
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002508\begin{cfuncdesc}{PyObject*}{PyMethod_New}{PyObject *func,
Fred Drake3adf79e2001-10-12 19:01:43 +00002509 PyObject *self, PyObject *class}
2510 Return a new method object, with \var{func} being any callable
2511 object; this is the function that will be called when the method is
2512 called. If this method should be bound to an instance, \var{self}
2513 should be the instance and \var{class} should be the class of
2514 \var{self}, otherwise \var{self} should be \NULL{} and \var{class}
2515 should be the class which provides the unbound method..
2516\end{cfuncdesc}
2517
2518\begin{cfuncdesc}{PyObject*}{PyMethod_Class}{PyObject *meth}
2519 Return the class object from which the method \var{meth} was
2520 created; if this was created from an instance, it will be the class
2521 of the instance.
2522\end{cfuncdesc}
2523
2524\begin{cfuncdesc}{PyObject*}{PyMethod_GET_CLASS}{PyObject *meth}
2525 Macro version of \cfunction{PyMethod_Class()} which avoids error
2526 checking.
2527\end{cfuncdesc}
2528
2529\begin{cfuncdesc}{PyObject*}{PyMethod_Function}{PyObject *meth}
2530 Return the function object associated with the method \var{meth}.
2531\end{cfuncdesc}
2532
2533\begin{cfuncdesc}{PyObject*}{PyMethod_GET_FUNCTION}{PyObject *meth}
2534 Macro version of \cfunction{PyMethod_Function()} which avoids error
2535 checking.
2536\end{cfuncdesc}
2537
2538\begin{cfuncdesc}{PyObject*}{PyMethod_Self}{PyObject *meth}
2539 Return the instance associated with the method \var{meth} if it is
Tim Peters9ddf40b2004-06-20 22:41:32 +00002540 bound, otherwise return \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002541\end{cfuncdesc}
2542
2543\begin{cfuncdesc}{PyObject*}{PyMethod_GET_SELF}{PyObject *meth}
2544 Macro version of \cfunction{PyMethod_Self()} which avoids error
2545 checking.
2546\end{cfuncdesc}
2547
2548
2549\subsection{Module Objects \label{moduleObjects}}
2550
2551\obindex{module}
2552There are only a few functions special to module objects.
2553
2554\begin{cvardesc}{PyTypeObject}{PyModule_Type}
2555 This instance of \ctype{PyTypeObject} represents the Python module
2556 type. This is exposed to Python programs as
2557 \code{types.ModuleType}.
2558 \withsubitem{(in module types)}{\ttindex{ModuleType}}
2559\end{cvardesc}
2560
2561\begin{cfuncdesc}{int}{PyModule_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002562 Return true if \var{p} is a module object, or a subtype of a module
Fred Drake3adf79e2001-10-12 19:01:43 +00002563 object.
2564 \versionchanged[Allowed subtypes to be accepted]{2.2}
2565\end{cfuncdesc}
2566
2567\begin{cfuncdesc}{int}{PyModule_CheckExact}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002568 Return true if \var{p} is a module object, but not a subtype of
Fred Drake3adf79e2001-10-12 19:01:43 +00002569 \cdata{PyModule_Type}.
2570 \versionadded{2.2}
2571\end{cfuncdesc}
2572
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002573\begin{cfuncdesc}{PyObject*}{PyModule_New}{const char *name}
Fred Drake3adf79e2001-10-12 19:01:43 +00002574 Return a new module object with the \member{__name__} attribute set
2575 to \var{name}. Only the module's \member{__doc__} and
2576 \member{__name__} attributes are filled in; the caller is
2577 responsible for providing a \member{__file__} attribute.
2578 \withsubitem{(module attribute)}{
2579 \ttindex{__name__}\ttindex{__doc__}\ttindex{__file__}}
2580\end{cfuncdesc}
2581
2582\begin{cfuncdesc}{PyObject*}{PyModule_GetDict}{PyObject *module}
2583 Return the dictionary object that implements \var{module}'s
2584 namespace; this object is the same as the \member{__dict__}
2585 attribute of the module object. This function never fails.
2586 \withsubitem{(module attribute)}{\ttindex{__dict__}}
Fred Drakef495ef72002-04-12 19:32:07 +00002587 It is recommended extensions use other \cfunction{PyModule_*()}
2588 and \cfunction{PyObject_*()} functions rather than directly
2589 manipulate a module's \member{__dict__}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002590\end{cfuncdesc}
2591
2592\begin{cfuncdesc}{char*}{PyModule_GetName}{PyObject *module}
2593 Return \var{module}'s \member{__name__} value. If the module does
2594 not provide one, or if it is not a string, \exception{SystemError}
2595 is raised and \NULL{} is returned.
2596 \withsubitem{(module attribute)}{\ttindex{__name__}}
2597 \withsubitem{(built-in exception)}{\ttindex{SystemError}}
2598\end{cfuncdesc}
2599
2600\begin{cfuncdesc}{char*}{PyModule_GetFilename}{PyObject *module}
2601 Return the name of the file from which \var{module} was loaded using
2602 \var{module}'s \member{__file__} attribute. If this is not defined,
2603 or if it is not a string, raise \exception{SystemError} and return
Tim Peters9ddf40b2004-06-20 22:41:32 +00002604 \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002605 \withsubitem{(module attribute)}{\ttindex{__file__}}
2606 \withsubitem{(built-in exception)}{\ttindex{SystemError}}
2607\end{cfuncdesc}
2608
2609\begin{cfuncdesc}{int}{PyModule_AddObject}{PyObject *module,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002610 const char *name, PyObject *value}
Fred Drake3adf79e2001-10-12 19:01:43 +00002611 Add an object to \var{module} as \var{name}. This is a convenience
2612 function which can be used from the module's initialization
Georg Brandl99363b62005-09-03 07:27:26 +00002613 function. This steals a reference to \var{value}. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00002614 \code{-1} on error, \code{0} on success.
2615 \versionadded{2.0}
2616\end{cfuncdesc}
2617
2618\begin{cfuncdesc}{int}{PyModule_AddIntConstant}{PyObject *module,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002619 const char *name, long value}
Fred Drake3adf79e2001-10-12 19:01:43 +00002620 Add an integer constant to \var{module} as \var{name}. This
2621 convenience function can be used from the module's initialization
Georg Brandl99363b62005-09-03 07:27:26 +00002622 function. Return \code{-1} on error, \code{0} on success.
Fred Drake3adf79e2001-10-12 19:01:43 +00002623 \versionadded{2.0}
2624\end{cfuncdesc}
2625
2626\begin{cfuncdesc}{int}{PyModule_AddStringConstant}{PyObject *module,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002627 const char *name, const char *value}
Fred Drake3adf79e2001-10-12 19:01:43 +00002628 Add a string constant to \var{module} as \var{name}. This
2629 convenience function can be used from the module's initialization
Georg Brandl99363b62005-09-03 07:27:26 +00002630 function. The string \var{value} must be null-terminated. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00002631 \code{-1} on error, \code{0} on success.
2632 \versionadded{2.0}
2633\end{cfuncdesc}
2634
2635
2636\subsection{Iterator Objects \label{iterator-objects}}
2637
2638Python provides two general-purpose iterator objects. The first, a
2639sequence iterator, works with an arbitrary sequence supporting the
2640\method{__getitem__()} method. The second works with a callable
2641object and a sentinel value, calling the callable for each item in the
2642sequence, and ending the iteration when the sentinel value is
2643returned.
2644
2645\begin{cvardesc}{PyTypeObject}{PySeqIter_Type}
2646 Type object for iterator objects returned by
2647 \cfunction{PySeqIter_New()} and the one-argument form of the
2648 \function{iter()} built-in function for built-in sequence types.
2649 \versionadded{2.2}
2650\end{cvardesc}
2651
2652\begin{cfuncdesc}{int}{PySeqIter_Check}{op}
2653 Return true if the type of \var{op} is \cdata{PySeqIter_Type}.
2654 \versionadded{2.2}
2655\end{cfuncdesc}
2656
2657\begin{cfuncdesc}{PyObject*}{PySeqIter_New}{PyObject *seq}
2658 Return an iterator that works with a general sequence object,
2659 \var{seq}. The iteration ends when the sequence raises
2660 \exception{IndexError} for the subscripting operation.
2661 \versionadded{2.2}
2662\end{cfuncdesc}
2663
2664\begin{cvardesc}{PyTypeObject}{PyCallIter_Type}
2665 Type object for iterator objects returned by
2666 \cfunction{PyCallIter_New()} and the two-argument form of the
2667 \function{iter()} built-in function.
2668 \versionadded{2.2}
2669\end{cvardesc}
2670
2671\begin{cfuncdesc}{int}{PyCallIter_Check}{op}
2672 Return true if the type of \var{op} is \cdata{PyCallIter_Type}.
2673 \versionadded{2.2}
2674\end{cfuncdesc}
2675
2676\begin{cfuncdesc}{PyObject*}{PyCallIter_New}{PyObject *callable,
2677 PyObject *sentinel}
2678 Return a new iterator. The first parameter, \var{callable}, can be
2679 any Python callable object that can be called with no parameters;
2680 each call to it should return the next item in the iteration. When
2681 \var{callable} returns a value equal to \var{sentinel}, the
2682 iteration will be terminated.
2683 \versionadded{2.2}
2684\end{cfuncdesc}
2685
2686
2687\subsection{Descriptor Objects \label{descriptor-objects}}
2688
Fred Drake54e62942001-12-11 19:40:16 +00002689``Descriptors'' are objects that describe some attribute of an object.
2690They are found in the dictionary of type objects.
2691
Fred Drake3adf79e2001-10-12 19:01:43 +00002692\begin{cvardesc}{PyTypeObject}{PyProperty_Type}
Fred Drake54e62942001-12-11 19:40:16 +00002693 The type object for the built-in descriptor types.
Fred Drake3adf79e2001-10-12 19:01:43 +00002694 \versionadded{2.2}
2695\end{cvardesc}
2696
2697\begin{cfuncdesc}{PyObject*}{PyDescr_NewGetSet}{PyTypeObject *type,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002698 struct PyGetSetDef *getset}
Fred Drake3adf79e2001-10-12 19:01:43 +00002699 \versionadded{2.2}
2700\end{cfuncdesc}
2701
2702\begin{cfuncdesc}{PyObject*}{PyDescr_NewMember}{PyTypeObject *type,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002703 struct PyMemberDef *meth}
Fred Drake3adf79e2001-10-12 19:01:43 +00002704 \versionadded{2.2}
2705\end{cfuncdesc}
2706
2707\begin{cfuncdesc}{PyObject*}{PyDescr_NewMethod}{PyTypeObject *type,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002708 struct PyMethodDef *meth}
Fred Drake3adf79e2001-10-12 19:01:43 +00002709 \versionadded{2.2}
2710\end{cfuncdesc}
2711
2712\begin{cfuncdesc}{PyObject*}{PyDescr_NewWrapper}{PyTypeObject *type,
2713 struct wrapperbase *wrapper,
2714 void *wrapped}
2715 \versionadded{2.2}
2716\end{cfuncdesc}
2717
Thomas Heller8178a222004-02-09 10:47:11 +00002718\begin{cfuncdesc}{PyObject*}{PyDescr_NewClassMethod}{PyTypeObject *type,
2719 PyMethodDef *method}
2720 \versionadded{2.3}
2721\end{cfuncdesc}
2722
Fred Drake3adf79e2001-10-12 19:01:43 +00002723\begin{cfuncdesc}{int}{PyDescr_IsData}{PyObject *descr}
Georg Brandl99363b62005-09-03 07:27:26 +00002724 Return true if the descriptor objects \var{descr} describes a data
Fred Drake3adf79e2001-10-12 19:01:43 +00002725 attribute, or false if it describes a method. \var{descr} must be a
2726 descriptor object; there is no error checking.
2727 \versionadded{2.2}
2728\end{cfuncdesc}
2729
2730\begin{cfuncdesc}{PyObject*}{PyWrapper_New}{PyObject *, PyObject *}
2731 \versionadded{2.2}
2732\end{cfuncdesc}
2733
2734
2735\subsection{Slice Objects \label{slice-objects}}
2736
2737\begin{cvardesc}{PyTypeObject}{PySlice_Type}
2738 The type object for slice objects. This is the same as
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002739 \code{slice} and \code{types.SliceType}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002740 \withsubitem{(in module types)}{\ttindex{SliceType}}
2741\end{cvardesc}
2742
2743\begin{cfuncdesc}{int}{PySlice_Check}{PyObject *ob}
Georg Brandl99363b62005-09-03 07:27:26 +00002744 Return true if \var{ob} is a slice object; \var{ob} must not be
Tim Peters9ddf40b2004-06-20 22:41:32 +00002745 \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002746\end{cfuncdesc}
2747
2748\begin{cfuncdesc}{PyObject*}{PySlice_New}{PyObject *start, PyObject *stop,
2749 PyObject *step}
2750 Return a new slice object with the given values. The \var{start},
2751 \var{stop}, and \var{step} parameters are used as the values of the
2752 slice object attributes of the same names. Any of the values may be
Tim Peters9ddf40b2004-06-20 22:41:32 +00002753 \NULL{}, in which case the \code{None} will be used for the
Georg Brandl99363b62005-09-03 07:27:26 +00002754 corresponding attribute. Return \NULL{} if the new object could
Fred Drake3adf79e2001-10-12 19:01:43 +00002755 not be allocated.
2756\end{cfuncdesc}
2757
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002758\begin{cfuncdesc}{int}{PySlice_GetIndices}{PySliceObject *slice, Py_ssize_t length,
2759 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step}
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002760Retrieve the start, stop and step indices from the slice object
2761\var{slice}, assuming a sequence of length \var{length}. Treats
2762indices greater than \var{length} as errors.
2763
2764Returns 0 on success and -1 on error with no exception set (unless one
2765of the indices was not \constant{None} and failed to be converted to
2766an integer, in which case -1 is returned with an exception set).
2767
2768You probably do not want to use this function. If you want to use
2769slice objects in versions of Python prior to 2.3, you would probably
2770do well to incorporate the source of \cfunction{PySlice_GetIndicesEx},
2771suitably renamed, in the source of your extension.
2772\end{cfuncdesc}
2773
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002774\begin{cfuncdesc}{int}{PySlice_GetIndicesEx}{PySliceObject *slice, Py_ssize_t length,
2775 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step,
2776 Py_ssize_t *slicelength}
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002777Usable replacement for \cfunction{PySlice_GetIndices}. Retrieve the
2778start, stop, and step indices from the slice object \var{slice}
2779assuming a sequence of length \var{length}, and store the length of
2780the slice in \var{slicelength}. Out of bounds indices are clipped in
2781a manner consistent with the handling of normal slices.
2782
2783Returns 0 on success and -1 on error with exception set.
2784
2785\versionadded{2.3}
Fred Drake3adf79e2001-10-12 19:01:43 +00002786\end{cfuncdesc}
2787
2788
2789\subsection{Weak Reference Objects \label{weakref-objects}}
2790
2791Python supports \emph{weak references} as first-class objects. There
2792are two specific object types which directly implement weak
2793references. The first is a simple reference object, and the second
2794acts as a proxy for the original object as much as it can.
2795
2796\begin{cfuncdesc}{int}{PyWeakref_Check}{ob}
2797 Return true if \var{ob} is either a reference or proxy object.
2798 \versionadded{2.2}
2799\end{cfuncdesc}
2800
2801\begin{cfuncdesc}{int}{PyWeakref_CheckRef}{ob}
2802 Return true if \var{ob} is a reference object.
2803 \versionadded{2.2}
2804\end{cfuncdesc}
2805
2806\begin{cfuncdesc}{int}{PyWeakref_CheckProxy}{ob}
2807 Return true if \var{ob} is a proxy object.
2808 \versionadded{2.2}
2809\end{cfuncdesc}
2810
2811\begin{cfuncdesc}{PyObject*}{PyWeakref_NewRef}{PyObject *ob,
2812 PyObject *callback}
2813 Return a weak reference object for the object \var{ob}. This will
2814 always return a new reference, but is not guaranteed to create a new
2815 object; an existing reference object may be returned. The second
2816 parameter, \var{callback}, can be a callable object that receives
2817 notification when \var{ob} is garbage collected; it should accept a
Raymond Hettinger5232f502004-03-25 08:51:36 +00002818 single parameter, which will be the weak reference object itself.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002819 \var{callback} may also be \code{None} or \NULL{}. If \var{ob}
Fred Drake3adf79e2001-10-12 19:01:43 +00002820 is not a weakly-referencable object, or if \var{callback} is not
Tim Peters9ddf40b2004-06-20 22:41:32 +00002821 callable, \code{None}, or \NULL{}, this will return \NULL{} and
Fred Drake3adf79e2001-10-12 19:01:43 +00002822 raise \exception{TypeError}.
2823 \versionadded{2.2}
2824\end{cfuncdesc}
2825
2826\begin{cfuncdesc}{PyObject*}{PyWeakref_NewProxy}{PyObject *ob,
2827 PyObject *callback}
2828 Return a weak reference proxy object for the object \var{ob}. This
2829 will always return a new reference, but is not guaranteed to create
2830 a new object; an existing proxy object may be returned. The second
2831 parameter, \var{callback}, can be a callable object that receives
2832 notification when \var{ob} is garbage collected; it should accept a
Raymond Hettinger5232f502004-03-25 08:51:36 +00002833 single parameter, which will be the weak reference object itself.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002834 \var{callback} may also be \code{None} or \NULL{}. If \var{ob} is not
Fred Drake3adf79e2001-10-12 19:01:43 +00002835 a weakly-referencable object, or if \var{callback} is not callable,
Tim Peters9ddf40b2004-06-20 22:41:32 +00002836 \code{None}, or \NULL{}, this will return \NULL{} and raise
Fred Drake3adf79e2001-10-12 19:01:43 +00002837 \exception{TypeError}.
2838 \versionadded{2.2}
2839\end{cfuncdesc}
2840
2841\begin{cfuncdesc}{PyObject*}{PyWeakref_GetObject}{PyObject *ref}
Georg Brandl99363b62005-09-03 07:27:26 +00002842 Return the referenced object from a weak reference, \var{ref}. If
Ka-Ping Yeebd379e92003-03-28 18:07:16 +00002843 the referent is no longer live, returns \code{None}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002844 \versionadded{2.2}
2845\end{cfuncdesc}
2846
2847\begin{cfuncdesc}{PyObject*}{PyWeakref_GET_OBJECT}{PyObject *ref}
2848 Similar to \cfunction{PyWeakref_GetObject()}, but implemented as a
2849 macro that does no error checking.
2850 \versionadded{2.2}
2851\end{cfuncdesc}
2852
2853
2854\subsection{CObjects \label{cObjects}}
2855
2856\obindex{CObject}
2857Refer to \emph{Extending and Embedding the Python Interpreter},
Fred Drake54e62942001-12-11 19:40:16 +00002858section~1.12, ``Providing a C API for an Extension Module,'' for more
Fred Drake3adf79e2001-10-12 19:01:43 +00002859information on using these objects.
2860
2861
2862\begin{ctypedesc}{PyCObject}
2863 This subtype of \ctype{PyObject} represents an opaque value, useful
2864 for C extension modules who need to pass an opaque value (as a
2865 \ctype{void*} pointer) through Python code to other C code. It is
2866 often used to make a C function pointer defined in one module
2867 available to other modules, so the regular import mechanism can be
2868 used to access C APIs defined in dynamically loaded modules.
2869\end{ctypedesc}
2870
2871\begin{cfuncdesc}{int}{PyCObject_Check}{PyObject *p}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002872 Return true if its argument is a \ctype{PyCObject}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002873\end{cfuncdesc}
2874
Tim Petersf582b822001-12-11 18:51:08 +00002875\begin{cfuncdesc}{PyObject*}{PyCObject_FromVoidPtr}{void* cobj,
Fred Drake54e62942001-12-11 19:40:16 +00002876 void (*destr)(void *)}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002877 Create a \ctype{PyCObject} from the \code{void *}\var{cobj}. The
Fred Drake3adf79e2001-10-12 19:01:43 +00002878 \var{destr} function will be called when the object is reclaimed,
Tim Peters9ddf40b2004-06-20 22:41:32 +00002879 unless it is \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002880\end{cfuncdesc}
2881
2882\begin{cfuncdesc}{PyObject*}{PyCObject_FromVoidPtrAndDesc}{void* cobj,
2883 void* desc, void (*destr)(void *, void *)}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002884 Create a \ctype{PyCObject} from the \ctype{void *}\var{cobj}. The
Fred Drake3adf79e2001-10-12 19:01:43 +00002885 \var{destr} function will be called when the object is reclaimed.
2886 The \var{desc} argument can be used to pass extra callback data for
2887 the destructor function.
2888\end{cfuncdesc}
2889
2890\begin{cfuncdesc}{void*}{PyCObject_AsVoidPtr}{PyObject* self}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002891 Return the object \ctype{void *} that the \ctype{PyCObject}
Fred Drake3adf79e2001-10-12 19:01:43 +00002892 \var{self} was created with.
2893\end{cfuncdesc}
2894
2895\begin{cfuncdesc}{void*}{PyCObject_GetDesc}{PyObject* self}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002896 Return the description \ctype{void *} that the \ctype{PyCObject}
Fred Drake3adf79e2001-10-12 19:01:43 +00002897 \var{self} was created with.
2898\end{cfuncdesc}
Fred Drakecd8474e2001-11-26 21:29:17 +00002899
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002900\begin{cfuncdesc}{int}{PyCObject_SetVoidPtr}{PyObject* self, void* cobj}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002901 Set the void pointer inside \var{self} to \var{cobj}.
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002902 The \ctype{PyCObject} must not have an associated destructor.
2903 Return true on success, false on failure.
2904\end{cfuncdesc}
2905
Fred Drakecd8474e2001-11-26 21:29:17 +00002906
2907\subsection{Cell Objects \label{cell-objects}}
2908
2909``Cell'' objects are used to implement variables referenced by
2910multiple scopes. For each such variable, a cell object is created to
2911store the value; the local variables of each stack frame that
2912references the value contains a reference to the cells from outer
2913scopes which also use that variable. When the value is accessed, the
2914value contained in the cell is used instead of the cell object
2915itself. This de-referencing of the cell object requires support from
2916the generated byte-code; these are not automatically de-referenced
2917when accessed. Cell objects are not likely to be useful elsewhere.
2918
Fred Drake54e62942001-12-11 19:40:16 +00002919\begin{ctypedesc}{PyCellObject}
2920 The C structure used for cell objects.
2921\end{ctypedesc}
2922
Fred Drakecd8474e2001-11-26 21:29:17 +00002923\begin{cvardesc}{PyTypeObject}{PyCell_Type}
Georg Brandl9b743f52006-02-20 12:57:53 +00002924 The type object corresponding to cell objects.
Fred Drakecd8474e2001-11-26 21:29:17 +00002925\end{cvardesc}
2926
2927\begin{cfuncdesc}{int}{PyCell_Check}{ob}
2928 Return true if \var{ob} is a cell object; \var{ob} must not be
Tim Peters9ddf40b2004-06-20 22:41:32 +00002929 \NULL{}.
Fred Drakecd8474e2001-11-26 21:29:17 +00002930\end{cfuncdesc}
2931
2932\begin{cfuncdesc}{PyObject*}{PyCell_New}{PyObject *ob}
2933 Create and return a new cell object containing the value \var{ob}.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002934 The parameter may be \NULL{}.
Fred Drakecd8474e2001-11-26 21:29:17 +00002935\end{cfuncdesc}
2936
2937\begin{cfuncdesc}{PyObject*}{PyCell_Get}{PyObject *cell}
2938 Return the contents of the cell \var{cell}.
2939\end{cfuncdesc}
2940
2941\begin{cfuncdesc}{PyObject*}{PyCell_GET}{PyObject *cell}
2942 Return the contents of the cell \var{cell}, but without checking
Raymond Hettingerf4bb1f92003-08-23 03:38:11 +00002943 that \var{cell} is non-\NULL{} and a cell object.
Fred Drakecd8474e2001-11-26 21:29:17 +00002944\end{cfuncdesc}
2945
2946\begin{cfuncdesc}{int}{PyCell_Set}{PyObject *cell, PyObject *value}
2947 Set the contents of the cell object \var{cell} to \var{value}. This
2948 releases the reference to any current content of the cell.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002949 \var{value} may be \NULL{}. \var{cell} must be non-\NULL{}; if it is
Fred Drakecd8474e2001-11-26 21:29:17 +00002950 not a cell object, \code{-1} will be returned. On success, \code{0}
2951 will be returned.
2952\end{cfuncdesc}
2953
2954\begin{cfuncdesc}{void}{PyCell_SET}{PyObject *cell, PyObject *value}
2955 Sets the value of the cell object \var{cell} to \var{value}. No
2956 reference counts are adjusted, and no checks are made for safety;
2957 \var{cell} must be non-\NULL{} and must be a cell object.
2958\end{cfuncdesc}
Martin v. Löwise440e472004-06-01 15:22:42 +00002959
2960
2961\subsection{Generator Objects \label{gen-objects}}
2962
2963Generator objects are what Python uses to implement generator iterators.
2964They are normally created by iterating over a function that yields values,
2965rather than explicitly calling \cfunction{PyGen_New}.
2966
2967\begin{ctypedesc}{PyGenObject}
2968 The C structure used for generator objects.
2969\end{ctypedesc}
2970
2971\begin{cvardesc}{PyTypeObject}{PyGen_Type}
2972 The type object corresponding to generator objects
2973\end{cvardesc}
2974
2975\begin{cfuncdesc}{int}{PyGen_Check}{ob}
2976 Return true if \var{ob} is a generator object; \var{ob} must not be
Tim Peters9ddf40b2004-06-20 22:41:32 +00002977 \NULL{}.
Martin v. Löwise440e472004-06-01 15:22:42 +00002978\end{cfuncdesc}
2979
2980\begin{cfuncdesc}{int}{PyGen_CheckExact}{ob}
2981 Return true if \var{ob}'s type is \var{PyGen_Type}
2982 is a generator object; \var{ob} must not be
Tim Peters9ddf40b2004-06-20 22:41:32 +00002983 \NULL{}.
Martin v. Löwise440e472004-06-01 15:22:42 +00002984\end{cfuncdesc}
2985
2986\begin{cfuncdesc}{PyObject*}{PyGen_New}{PyFrameObject *frame}
2987 Create and return a new generator object based on the \var{frame} object.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002988 A reference to \var{frame} is stolen by this function.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002989 The parameter must not be \NULL{}.
2990\end{cfuncdesc}
2991
2992
2993\subsection{DateTime Objects \label{datetime-objects}}
2994
2995Various date and time objects are supplied by the \module{datetime}
2996module. Before using any of these functions, the header file
2997\file{datetime.h} must be included in your source (note that this is
Thomas Wouters89f507f2006-12-13 04:49:30 +00002998not included by \file{Python.h}), and the macro
2999\cfunction{PyDateTime_IMPORT} must be invoked. The macro puts a
3000pointer to a C structure into a static variable,
3001\code{PyDateTimeAPI}, that is used by the following macros.
Tim Peters9ddf40b2004-06-20 22:41:32 +00003002
Tim Peters183dabc2004-07-11 19:26:19 +00003003Type-check macros:
3004
3005\begin{cfuncdesc}{int}{PyDate_Check}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00003006 Return true if \var{ob} is of type \cdata{PyDateTime_DateType} or
3007 a subtype of \cdata{PyDateTime_DateType}. \var{ob} must not be
3008 \NULL{}.
3009 \versionadded{2.4}
3010\end{cfuncdesc}
3011
Tim Peters183dabc2004-07-11 19:26:19 +00003012\begin{cfuncdesc}{int}{PyDate_CheckExact}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00003013 Return true if \var{ob} is of type \cdata{PyDateTime_DateType}.
3014 \var{ob} must not be \NULL{}.
3015 \versionadded{2.4}
3016\end{cfuncdesc}
3017
Tim Peters183dabc2004-07-11 19:26:19 +00003018\begin{cfuncdesc}{int}{PyDateTime_Check}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00003019 Return true if \var{ob} is of type \cdata{PyDateTime_DateTimeType} or
3020 a subtype of \cdata{PyDateTime_DateTimeType}. \var{ob} must not be
3021 \NULL{}.
3022 \versionadded{2.4}
3023\end{cfuncdesc}
3024
Tim Peters183dabc2004-07-11 19:26:19 +00003025\begin{cfuncdesc}{int}{PyDateTime_CheckExact}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00003026 Return true if \var{ob} is of type \cdata{PyDateTime_DateTimeType}.
3027 \var{ob} must not be \NULL{}.
3028 \versionadded{2.4}
3029\end{cfuncdesc}
3030
Tim Peters183dabc2004-07-11 19:26:19 +00003031\begin{cfuncdesc}{int}{PyTime_Check}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00003032 Return true if \var{ob} is of type \cdata{PyDateTime_TimeType} or
3033 a subtype of \cdata{PyDateTime_TimeType}. \var{ob} must not be
3034 \NULL{}.
3035 \versionadded{2.4}
3036\end{cfuncdesc}
3037
Tim Peters183dabc2004-07-11 19:26:19 +00003038\begin{cfuncdesc}{int}{PyTime_CheckExact}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00003039 Return true if \var{ob} is of type \cdata{PyDateTime_TimeType}.
3040 \var{ob} must not be \NULL{}.
3041 \versionadded{2.4}
3042\end{cfuncdesc}
3043
Tim Peters183dabc2004-07-11 19:26:19 +00003044\begin{cfuncdesc}{int}{PyDelta_Check}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00003045 Return true if \var{ob} is of type \cdata{PyDateTime_DeltaType} or
3046 a subtype of \cdata{PyDateTime_DeltaType}. \var{ob} must not be
3047 \NULL{}.
3048 \versionadded{2.4}
3049\end{cfuncdesc}
3050
Tim Peters183dabc2004-07-11 19:26:19 +00003051\begin{cfuncdesc}{int}{PyDelta_CheckExact}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00003052 Return true if \var{ob} is of type \cdata{PyDateTime_DeltaType}.
3053 \var{ob} must not be \NULL{}.
3054 \versionadded{2.4}
3055\end{cfuncdesc}
3056
Tim Peters183dabc2004-07-11 19:26:19 +00003057\begin{cfuncdesc}{int}{PyTZInfo_Check}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00003058 Return true if \var{ob} is of type \cdata{PyDateTime_TZInfoType} or
3059 a subtype of \cdata{PyDateTime_TZInfoType}. \var{ob} must not be
3060 \NULL{}.
3061 \versionadded{2.4}
3062\end{cfuncdesc}
3063
Tim Peters183dabc2004-07-11 19:26:19 +00003064\begin{cfuncdesc}{int}{PyTZInfo_CheckExact}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00003065 Return true if \var{ob} is of type \cdata{PyDateTime_TZInfoType}.
3066 \var{ob} must not be \NULL{}.
3067 \versionadded{2.4}
3068\end{cfuncdesc}
3069
Tim Peters183dabc2004-07-11 19:26:19 +00003070Macros to create objects:
3071
Tim Peters9ddf40b2004-06-20 22:41:32 +00003072\begin{cfuncdesc}{PyObject*}{PyDate_FromDate}{int year, int month, int day}
3073 Return a \code{datetime.date} object with the specified year, month
3074 and day.
3075 \versionadded{2.4}
3076\end{cfuncdesc}
3077
Brett Cannon5bbe6ad2005-02-17 05:17:17 +00003078\begin{cfuncdesc}{PyObject*}{PyDateTime_FromDateAndTime}{int year, int month,
Tim Peters9ddf40b2004-06-20 22:41:32 +00003079 int day, int hour, int minute, int second, int usecond}
3080 Return a \code{datetime.datetime} object with the specified year, month,
3081 day, hour, minute, second and microsecond.
3082 \versionadded{2.4}
3083\end{cfuncdesc}
3084
3085\begin{cfuncdesc}{PyObject*}{PyTime_FromTime}{int hour, int minute,
3086 int second, int usecond}
3087 Return a \code{datetime.time} object with the specified hour, minute,
3088 second and microsecond.
3089 \versionadded{2.4}
3090\end{cfuncdesc}
3091
3092\begin{cfuncdesc}{PyObject*}{PyDelta_FromDSU}{int days, int seconds,
3093 int useconds}
3094 Return a \code{datetime.timedelta} object representing the given number
3095 of days, seconds and microseconds. Normalization is performed so that
3096 the resulting number of microseconds and seconds lie in the ranges
3097 documented for \code{datetime.timedelta} objects.
3098 \versionadded{2.4}
3099\end{cfuncdesc}
3100
Tim Peters8ff9f9f2004-07-17 01:42:26 +00003101Macros to extract fields from date objects. The argument must be an
Tim Peters183dabc2004-07-11 19:26:19 +00003102instance of \cdata{PyDateTime_Date}, including subclasses (such as
3103\cdata{PyDateTime_DateTime}). The argument must not be \NULL{}, and
3104the type is not checked:
3105
3106\begin{cfuncdesc}{int}{PyDateTime_GET_YEAR}{PyDateTime_Date *o}
3107 Return the year, as a positive int.
3108 \versionadded{2.4}
3109\end{cfuncdesc}
3110
3111\begin{cfuncdesc}{int}{PyDateTime_GET_MONTH}{PyDateTime_Date *o}
3112 Return the month, as an int from 1 through 12.
3113 \versionadded{2.4}
3114\end{cfuncdesc}
3115
3116\begin{cfuncdesc}{int}{PyDateTime_GET_DAY}{PyDateTime_Date *o}
3117 Return the day, as an int from 1 through 31.
3118 \versionadded{2.4}
3119\end{cfuncdesc}
3120
Tim Peters8ff9f9f2004-07-17 01:42:26 +00003121Macros to extract fields from datetime objects. The argument must be an
Tim Peters183dabc2004-07-11 19:26:19 +00003122instance of \cdata{PyDateTime_DateTime}, including subclasses.
3123The argument must not be \NULL{}, and the type is not checked:
3124
3125\begin{cfuncdesc}{int}{PyDateTime_DATE_GET_HOUR}{PyDateTime_DateTime *o}
Neal Norwitz7fdd92f2004-08-02 21:56:33 +00003126 Return the hour, as an int from 0 through 23.
Tim Peters183dabc2004-07-11 19:26:19 +00003127 \versionadded{2.4}
3128\end{cfuncdesc}
3129
3130\begin{cfuncdesc}{int}{PyDateTime_DATE_GET_MINUTE}{PyDateTime_DateTime *o}
3131 Return the minute, as an int from 0 through 59.
3132 \versionadded{2.4}
3133\end{cfuncdesc}
3134
3135\begin{cfuncdesc}{int}{PyDateTime_DATE_GET_SECOND}{PyDateTime_DateTime *o}
3136 Return the second, as an int from 0 through 59.
3137 \versionadded{2.4}
3138\end{cfuncdesc}
3139
3140\begin{cfuncdesc}{int}{PyDateTime_DATE_GET_MICROSECOND}{PyDateTime_DateTime *o}
3141 Return the microsecond, as an int from 0 through 999999.
3142 \versionadded{2.4}
3143\end{cfuncdesc}
3144
Tim Peters8ff9f9f2004-07-17 01:42:26 +00003145Macros to extract fields from time objects. The argument must be an
Tim Peters183dabc2004-07-11 19:26:19 +00003146instance of \cdata{PyDateTime_Time}, including subclasses.
3147The argument must not be \NULL{}, and the type is not checked:
3148
3149\begin{cfuncdesc}{int}{PyDateTime_TIME_GET_HOUR}{PyDateTime_Time *o}
Neal Norwitz7fdd92f2004-08-02 21:56:33 +00003150 Return the hour, as an int from 0 through 23.
Tim Peters183dabc2004-07-11 19:26:19 +00003151 \versionadded{2.4}
3152\end{cfuncdesc}
3153
3154\begin{cfuncdesc}{int}{PyDateTime_TIME_GET_MINUTE}{PyDateTime_Time *o}
3155 Return the minute, as an int from 0 through 59.
3156 \versionadded{2.4}
3157\end{cfuncdesc}
3158
3159\begin{cfuncdesc}{int}{PyDateTime_TIME_GET_SECOND}{PyDateTime_Time *o}
3160 Return the second, as an int from 0 through 59.
3161 \versionadded{2.4}
3162\end{cfuncdesc}
3163
3164\begin{cfuncdesc}{int}{PyDateTime_TIME_GET_MICROSECOND}{PyDateTime_Time *o}
3165 Return the microsecond, as an int from 0 through 999999.
3166 \versionadded{2.4}
3167\end{cfuncdesc}
3168
3169Macros for the convenience of modules implementing the DB API:
3170
Tim Peters9ddf40b2004-06-20 22:41:32 +00003171\begin{cfuncdesc}{PyObject*}{PyDateTime_FromTimestamp}{PyObject *args}
3172 Create and return a new \code{datetime.datetime} object given an argument
3173 tuple suitable for passing to \code{datetime.datetime.fromtimestamp()}.
Tim Peters9ddf40b2004-06-20 22:41:32 +00003174 \versionadded{2.4}
3175\end{cfuncdesc}
3176
3177\begin{cfuncdesc}{PyObject*}{PyDate_FromTimestamp}{PyObject *args}
3178 Create and return a new \code{datetime.date} object given an argument
3179 tuple suitable for passing to \code{datetime.date.fromtimestamp()}.
Tim Peters9ddf40b2004-06-20 22:41:32 +00003180 \versionadded{2.4}
Martin v. Löwise440e472004-06-01 15:22:42 +00003181\end{cfuncdesc}
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003182
3183
3184\subsection{Set Objects \label{setObjects}}
Thomas Wouters477c8d52006-05-27 19:21:47 +00003185\sectionauthor{Raymond D. Hettinger}{python@rcn.com}
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003186
3187\obindex{set}
3188\obindex{frozenset}
3189\versionadded{2.5}
3190
3191This section details the public API for \class{set} and \class{frozenset}
3192objects. Any functionality not listed below is best accessed using the
Raymond Hettinger0c230b92005-08-17 10:05:22 +00003193either the abstract object protocol (including
Thomas Wouters477c8d52006-05-27 19:21:47 +00003194\cfunction{PyObject_CallMethod()}, \cfunction{PyObject_RichCompareBool()},
3195\cfunction{PyObject_Hash()}, \cfunction{PyObject_Repr()},
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003196\cfunction{PyObject_IsTrue()}, \cfunction{PyObject_Print()}, and
Raymond Hettinger0c230b92005-08-17 10:05:22 +00003197\cfunction{PyObject_GetIter()})
3198or the abstract number protocol (including
Thomas Wouters89f507f2006-12-13 04:49:30 +00003199\cfunction{PyNumber_And()}, \cfunction{PyNumber_Subtract()},
Raymond Hettinger0c230b92005-08-17 10:05:22 +00003200\cfunction{PyNumber_Or()}, \cfunction{PyNumber_Xor()},
Thomas Wouters89f507f2006-12-13 04:49:30 +00003201\cfunction{PyNumber_InPlaceAnd()}, \cfunction{PyNumber_InPlaceSubtract()},
Georg Brandlb518d8c2006-02-22 11:46:55 +00003202\cfunction{PyNumber_InPlaceOr()}, and \cfunction{PyNumber_InPlaceXor()}).
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003203
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003204\begin{ctypedesc}{PySetObject}
3205 This subtype of \ctype{PyObject} is used to hold the internal data for
3206 both \class{set} and \class{frozenset} objects. It is like a
3207 \ctype{PyDictObject} in that it is a fixed size for small sets
3208 (much like tuple storage) and will point to a separate, variable sized
3209 block of memory for medium and large sized sets (much like list storage).
3210 None of the fields of this structure should be considered public and
3211 are subject to change. All access should be done through the
Thomas Wouters477c8d52006-05-27 19:21:47 +00003212 documented API rather than by manipulating the values in the structure.
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003213
3214\end{ctypedesc}
3215
3216\begin{cvardesc}{PyTypeObject}{PySet_Type}
3217 This is an instance of \ctype{PyTypeObject} representing the Python
3218 \class{set} type.
3219\end{cvardesc}
3220
3221\begin{cvardesc}{PyTypeObject}{PyFrozenSet_Type}
3222 This is an instance of \ctype{PyTypeObject} representing the Python
3223 \class{frozenset} type.
3224\end{cvardesc}
3225
3226
3227The following type check macros work on pointers to any Python object.
3228Likewise, the constructor functions work with any iterable Python object.
3229
3230\begin{cfuncdesc}{int}{PyAnySet_Check}{PyObject *p}
Thomas Wouters477c8d52006-05-27 19:21:47 +00003231 Return true if \var{p} is a \class{set} object, a \class{frozenset}
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003232 object, or an instance of a subtype.
3233\end{cfuncdesc}
3234
3235\begin{cfuncdesc}{int}{PyAnySet_CheckExact}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00003236 Return true if \var{p} is a \class{set} object or a \class{frozenset}
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003237 object but not an instance of a subtype.
3238\end{cfuncdesc}
3239
3240\begin{cfuncdesc}{int}{PyFrozenSet_CheckExact}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00003241 Return true if \var{p} is a \class{frozenset} object
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003242 but not an instance of a subtype.
3243\end{cfuncdesc}
3244
3245\begin{cfuncdesc}{PyObject*}{PySet_New}{PyObject *iterable}
Georg Brandl99363b62005-09-03 07:27:26 +00003246 Return a new \class{set} containing objects returned by the
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003247 \var{iterable}. The \var{iterable} may be \NULL{} to create a
Georg Brandl99363b62005-09-03 07:27:26 +00003248 new empty set. Return the new set on success or \NULL{} on
3249 failure. Raise \exception{TypeError} if \var{iterable} is
Raymond Hettinger94fedf92005-08-17 12:23:45 +00003250 not actually iterable. The constructor is also useful for
3251 copying a set (\code{c=set(s)}).
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003252\end{cfuncdesc}
3253
3254\begin{cfuncdesc}{PyObject*}{PyFrozenSet_New}{PyObject *iterable}
Georg Brandl99363b62005-09-03 07:27:26 +00003255 Return a new \class{frozenset} containing objects returned by the
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003256 \var{iterable}. The \var{iterable} may be \NULL{} to create a
Georg Brandl99363b62005-09-03 07:27:26 +00003257 new empty frozenset. Return the new set on success or \NULL{} on
3258 failure. Raise \exception{TypeError} if \var{iterable} is
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00003259 not actually iterable.
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003260\end{cfuncdesc}
3261
3262
3263The following functions and macros are available for instances of
3264\class{set} or \class{frozenset} or instances of their subtypes.
3265
3266\begin{cfuncdesc}{int}{PySet_Size}{PyObject *anyset}
Georg Brandl99363b62005-09-03 07:27:26 +00003267 Return the length of a \class{set} or \class{frozenset} object.
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003268 Equivalent to \samp{len(\var{anyset})}. Raises a
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00003269 \exception{PyExc_SystemError} if \var{anyset} is not a \class{set},
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003270 \class{frozenset}, or an instance of a subtype.
3271 \bifuncindex{len}
3272\end{cfuncdesc}
3273
3274\begin{cfuncdesc}{int}{PySet_GET_SIZE}{PyObject *anyset}
3275 Macro form of \cfunction{PySet_Size()} without error checking.
3276\end{cfuncdesc}
3277
3278\begin{cfuncdesc}{int}{PySet_Contains}{PyObject *anyset, PyObject *key}
Georg Brandl99363b62005-09-03 07:27:26 +00003279 Return 1 if found, 0 if not found, and -1 if an error is
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003280 encountered. Unlike the Python \method{__contains__()} method, this
3281 function does not automatically convert unhashable sets into temporary
Georg Brandl99363b62005-09-03 07:27:26 +00003282 frozensets. Raise a \exception{TypeError} if the \var{key} is unhashable.
3283 Raise \exception{PyExc_SystemError} if \var{anyset} is not a \class{set},
Thomas Wouters477c8d52006-05-27 19:21:47 +00003284 \class{frozenset}, or an instance of a subtype.
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003285\end{cfuncdesc}
3286
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003287The following functions are available for instances of \class{set} or
3288its subtypes but not for instances of \class{frozenset} or its subtypes.
3289
3290\begin{cfuncdesc}{int}{PySet_Add}{PyObject *set, PyObject *key}
Georg Brandl99363b62005-09-03 07:27:26 +00003291 Add \var{key} to a \class{set} instance. Does not apply to
3292 \class{frozenset} instances. Return 0 on success or -1 on failure.
3293 Raise a \exception{TypeError} if the \var{key} is unhashable.
3294 Raise a \exception{MemoryError} if there is no room to grow.
3295 Raise a \exception{SystemError} if \var{set} is an not an instance
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003296 of \class{set} or its subtype.
3297\end{cfuncdesc}
3298
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00003299\begin{cfuncdesc}{int}{PySet_Discard}{PyObject *set, PyObject *key}
Georg Brandl99363b62005-09-03 07:27:26 +00003300 Return 1 if found and removed, 0 if not found (no action taken),
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00003301 and -1 if an error is encountered. Does not raise \exception{KeyError}
Georg Brandl99363b62005-09-03 07:27:26 +00003302 for missing keys. Raise a \exception{TypeError} if the \var{key} is
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00003303 unhashable. Unlike the Python \method{discard()} method, this function
3304 does not automatically convert unhashable sets into temporary frozensets.
Georg Brandl99363b62005-09-03 07:27:26 +00003305 Raise \exception{PyExc_SystemError} if \var{set} is an not an instance
Thomas Wouters477c8d52006-05-27 19:21:47 +00003306 of \class{set} or its subtype.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00003307\end{cfuncdesc}
3308
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003309\begin{cfuncdesc}{PyObject*}{PySet_Pop}{PyObject *set}
Georg Brandl99363b62005-09-03 07:27:26 +00003310 Return a new reference to an arbitrary object in the \var{set},
3311 and removes the object from the \var{set}. Return \NULL{} on
3312 failure. Raise \exception{KeyError} if the set is empty.
3313 Raise a \exception{SystemError} if \var{set} is an not an instance
Thomas Wouters477c8d52006-05-27 19:21:47 +00003314 of \class{set} or its subtype.
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003315\end{cfuncdesc}
3316
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003317\begin{cfuncdesc}{int}{PySet_Clear}{PyObject *set}
3318 Empty an existing set of all elements.
3319\end{cfuncdesc}