blob: 630a7261160d9022ee3c36b3ea4c328b6d4430f5 [file] [log] [blame]
Fred Drake3adf79e2001-10-12 19:01:43 +00001\chapter{Concrete Objects Layer \label{concrete}}
2
3
4The functions in this chapter are specific to certain Python object
5types. Passing them an object of the wrong type is not a good idea;
6if you receive an object from a Python program and you are not sure
7that it has the right type, you must perform a type check first;
8for example, to check that an object is a dictionary, use
9\cfunction{PyDict_Check()}. The chapter is structured like the
10``family tree'' of Python object types.
11
12\warning{While the functions described in this chapter carefully check
13the type of the objects which are passed in, many of them do not check
14for \NULL{} being passed instead of a valid object. Allowing \NULL{}
15to be passed in can cause memory access violations and immediate
16termination of the interpreter.}
17
18
19\section{Fundamental Objects \label{fundamental}}
20
Tim Petersf582b822001-12-11 18:51:08 +000021This section describes Python type objects and the singleton object
Fred Drake3adf79e2001-10-12 19:01:43 +000022\code{None}.
23
24
25\subsection{Type Objects \label{typeObjects}}
26
27\obindex{type}
28\begin{ctypedesc}{PyTypeObject}
29 The C structure of the objects used to describe built-in types.
30\end{ctypedesc}
31
32\begin{cvardesc}{PyObject*}{PyType_Type}
33 This is the type object for type objects; it is the same object as
Thomas Wouters0e3f5912006-08-11 14:57:12 +000034 \code{type} and \code{types.TypeType} in the Python layer.
Fred Drake3adf79e2001-10-12 19:01:43 +000035 \withsubitem{(in module types)}{\ttindex{TypeType}}
36\end{cvardesc}
37
38\begin{cfuncdesc}{int}{PyType_Check}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +000039 Return true if the object \var{o} is a type object, including
40 instances of types derived from the standard type object. Return
Fred Drakee3c764b2002-04-10 17:52:52 +000041 false in all other cases.
42\end{cfuncdesc}
43
44\begin{cfuncdesc}{int}{PyType_CheckExact}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +000045 Return true if the object \var{o} is a type object, but not a
46 subtype of the standard type object. Return false in all other
Fred Drakee3c764b2002-04-10 17:52:52 +000047 cases.
48 \versionadded{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +000049\end{cfuncdesc}
50
51\begin{cfuncdesc}{int}{PyType_HasFeature}{PyObject *o, int feature}
Georg Brandl99363b62005-09-03 07:27:26 +000052 Return true if the type object \var{o} sets the feature
Fred Drake3adf79e2001-10-12 19:01:43 +000053 \var{feature}. Type features are denoted by single bit flags.
54\end{cfuncdesc}
55
Fred Drakee3c764b2002-04-10 17:52:52 +000056\begin{cfuncdesc}{int}{PyType_IS_GC}{PyObject *o}
57 Return true if the type object includes support for the cycle
58 detector; this tests the type flag \constant{Py_TPFLAGS_HAVE_GC}.
59 \versionadded{2.0}
60\end{cfuncdesc}
61
Fred Drake3adf79e2001-10-12 19:01:43 +000062\begin{cfuncdesc}{int}{PyType_IsSubtype}{PyTypeObject *a, PyTypeObject *b}
Georg Brandl99363b62005-09-03 07:27:26 +000063 Return true if \var{a} is a subtype of \var{b}.
Fred Drake3adf79e2001-10-12 19:01:43 +000064 \versionadded{2.2}
65\end{cfuncdesc}
66
67\begin{cfuncdesc}{PyObject*}{PyType_GenericAlloc}{PyTypeObject *type,
Martin v. Löwis29fafd82006-03-01 05:16:03 +000068 Py_ssize_t nitems}
Fred Drake3adf79e2001-10-12 19:01:43 +000069 \versionadded{2.2}
70\end{cfuncdesc}
71
72\begin{cfuncdesc}{PyObject*}{PyType_GenericNew}{PyTypeObject *type,
73 PyObject *args, PyObject *kwds}
74 \versionadded{2.2}
75\end{cfuncdesc}
76
77\begin{cfuncdesc}{int}{PyType_Ready}{PyTypeObject *type}
Fred Drake28de8d42002-04-12 16:15:10 +000078 Finalize a type object. This should be called on all type objects
79 to finish their initialization. This function is responsible for
Georg Brandl99363b62005-09-03 07:27:26 +000080 adding inherited slots from a type's base class. Return \code{0}
81 on success, or return \code{-1} and sets an exception on error.
Fred Drake3adf79e2001-10-12 19:01:43 +000082 \versionadded{2.2}
83\end{cfuncdesc}
84
85
86\subsection{The None Object \label{noneObject}}
87
Fred Drake7a700b82004-01-01 05:43:53 +000088\obindex{None}
Fred Drake3adf79e2001-10-12 19:01:43 +000089Note that the \ctype{PyTypeObject} for \code{None} is not directly
90exposed in the Python/C API. Since \code{None} is a singleton,
91testing for object identity (using \samp{==} in C) is sufficient.
92There is no \cfunction{PyNone_Check()} function for the same reason.
93
94\begin{cvardesc}{PyObject*}{Py_None}
95 The Python \code{None} object, denoting lack of value. This object
Fred Drake6ccdccd2002-03-12 20:12:54 +000096 has no methods. It needs to be treated just like any other object
97 with respect to reference counts.
Fred Drake3adf79e2001-10-12 19:01:43 +000098\end{cvardesc}
99
Brett Cannon35d83602003-11-09 04:15:30 +0000100\begin{csimplemacrodesc}{Py_RETURN_NONE}
Georg Brandl99363b62005-09-03 07:27:26 +0000101 Properly handle returning \cdata{Py_None} from within a C function.
Brett Cannon35d83602003-11-09 04:15:30 +0000102\end{csimplemacrodesc}
103
Fred Drake3adf79e2001-10-12 19:01:43 +0000104
105\section{Numeric Objects \label{numericObjects}}
106
107\obindex{numeric}
108
109
110\subsection{Plain Integer Objects \label{intObjects}}
111
112\obindex{integer}
113\begin{ctypedesc}{PyIntObject}
114 This subtype of \ctype{PyObject} represents a Python integer
115 object.
116\end{ctypedesc}
117
118\begin{cvardesc}{PyTypeObject}{PyInt_Type}
Tim Petersf582b822001-12-11 18:51:08 +0000119 This instance of \ctype{PyTypeObject} represents the Python plain
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000120 integer type. This is the same object as \code{int} and
121 \code{types.IntType}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000122 \withsubitem{(in modules types)}{\ttindex{IntType}}
123\end{cvardesc}
124
Andrew M. Kuchling4eb1a002004-08-07 20:19:24 +0000125\begin{cfuncdesc}{int}{PyInt_Check}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000126 Return true if \var{o} is of type \cdata{PyInt_Type} or a subtype
Fred Drake3adf79e2001-10-12 19:01:43 +0000127 of \cdata{PyInt_Type}.
128 \versionchanged[Allowed subtypes to be accepted]{2.2}
129\end{cfuncdesc}
130
Andrew M. Kuchling4eb1a002004-08-07 20:19:24 +0000131\begin{cfuncdesc}{int}{PyInt_CheckExact}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000132 Return true if \var{o} is of type \cdata{PyInt_Type}, but not a
Fred Drake3adf79e2001-10-12 19:01:43 +0000133 subtype of \cdata{PyInt_Type}.
134 \versionadded{2.2}
135\end{cfuncdesc}
136
Skip Montanaro1ff49a72003-02-03 05:13:24 +0000137\begin{cfuncdesc}{PyObject*}{PyInt_FromString}{char *str, char **pend,
138 int base}
139 Return a new \ctype{PyIntObject} or \ctype{PyLongObject} based on the
140 string value in \var{str}, which is interpreted according to the radix in
Tim Peters9ddf40b2004-06-20 22:41:32 +0000141 \var{base}. If \var{pend} is non-\NULL{}, \code{*\var{pend}} will point to
Skip Montanaro1ff49a72003-02-03 05:13:24 +0000142 the first character in \var{str} which follows the representation of the
143 number. If \var{base} is \code{0}, the radix will be determined based on
144 the leading characters of \var{str}: if \var{str} starts with \code{'0x'}
145 or \code{'0X'}, radix 16 will be used; if \var{str} starts with
146 \code{'0'}, radix 8 will be used; otherwise radix 10 will be used. If
147 \var{base} is not \code{0}, it must be between \code{2} and \code{36},
148 inclusive. Leading spaces are ignored. If there are no digits,
149 \exception{ValueError} will be raised. If the string represents a number
150 too large to be contained within the machine's \ctype{long int} type and
151 overflow warnings are being suppressed, a \ctype{PyLongObject} will be
152 returned. If overflow warnings are not being suppressed, \NULL{} will be
153 returned in this case.
154\end{cfuncdesc}
155
Fred Drake3adf79e2001-10-12 19:01:43 +0000156\begin{cfuncdesc}{PyObject*}{PyInt_FromLong}{long ival}
Georg Brandl99363b62005-09-03 07:27:26 +0000157 Create a new integer object with a value of \var{ival}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000158
159 The current implementation keeps an array of integer objects for all
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000160 integers between \code{-5} and \code{256}, when you create an int in
Fred Drake3adf79e2001-10-12 19:01:43 +0000161 that range you actually just get back a reference to the existing
162 object. So it should be possible to change the value of \code{1}. I
163 suspect the behaviour of Python in this case is undefined. :-)
164\end{cfuncdesc}
165
Martin v. Löwis3b197542006-03-01 05:47:11 +0000166\begin{cfuncdesc}{PyObject*}{PyInt_FromSsize_t}{Py_ssize_t ival}
167 Create a new integer object with a value of \var{ival}.
168 If the value exceeds \code{LONG_MAX}, a long integer object is
169 returned.
170
171 \versionadded{2.5}
172\end{cfuncdesc}
173
Fred Drake3adf79e2001-10-12 19:01:43 +0000174\begin{cfuncdesc}{long}{PyInt_AsLong}{PyObject *io}
175 Will first attempt to cast the object to a \ctype{PyIntObject}, if
Martin v. Löwis3b197542006-03-01 05:47:11 +0000176 it is not already one, and then return its value. If there is an
177 error, \code{-1} is returned, and the caller should check
178 \code{PyErr_Occurred()} to find out whether there was an error, or
179 whether the value just happened to be -1.
Fred Drake3adf79e2001-10-12 19:01:43 +0000180\end{cfuncdesc}
181
182\begin{cfuncdesc}{long}{PyInt_AS_LONG}{PyObject *io}
Georg Brandl99363b62005-09-03 07:27:26 +0000183 Return the value of the object \var{io}. No error checking is
Fred Drake3adf79e2001-10-12 19:01:43 +0000184 performed.
185\end{cfuncdesc}
186
Thomas Heller34d7f092003-04-23 19:51:05 +0000187\begin{cfuncdesc}{unsigned long}{PyInt_AsUnsignedLongMask}{PyObject *io}
188 Will first attempt to cast the object to a \ctype{PyIntObject} or
Fred Drakec22b2992003-04-23 20:38:41 +0000189 \ctype{PyLongObject}, if it is not already one, and then return its
Thomas Heller34d7f092003-04-23 19:51:05 +0000190 value as unsigned long. This function does not check for overflow.
191 \versionadded{2.3}
192\end{cfuncdesc}
193
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000194\begin{cfuncdesc}{unsigned PY_LONG_LONG}{PyInt_AsUnsignedLongLongMask}{PyObject *io}
Thomas Heller34d7f092003-04-23 19:51:05 +0000195 Will first attempt to cast the object to a \ctype{PyIntObject} or
Fred Drakec22b2992003-04-23 20:38:41 +0000196 \ctype{PyLongObject}, if it is not already one, and then return its
Thomas Heller34d7f092003-04-23 19:51:05 +0000197 value as unsigned long long, without checking for overflow.
198 \versionadded{2.3}
199\end{cfuncdesc}
200
Martin v. Löwis3b197542006-03-01 05:47:11 +0000201\begin{cfuncdesc}{Py_ssize_t}{PyInt_AsSsize_t}{PyObject *io}
202 Will first attempt to cast the object to a \ctype{PyIntObject} or
203 \ctype{PyLongObject}, if it is not already one, and then return its
204 value as \ctype{Py_ssize_t}.
205 \versionadded{2.5}
206\end{cfuncdesc}
207
Fred Drake3adf79e2001-10-12 19:01:43 +0000208\begin{cfuncdesc}{long}{PyInt_GetMax}{}
Georg Brandl99363b62005-09-03 07:27:26 +0000209 Return the system's idea of the largest integer it can handle
Fred Drake3adf79e2001-10-12 19:01:43 +0000210 (\constant{LONG_MAX}\ttindex{LONG_MAX}, as defined in the system
211 header files).
212\end{cfuncdesc}
213
Fred Drake2be406b2004-08-03 16:02:35 +0000214\subsection{Boolean Objects \label{boolObjects}}
Skip Montanaro33ee76a2004-07-28 14:17:04 +0000215
216Booleans in Python are implemented as a subclass of integers. There
217are only two booleans, \constant{Py_False} and \constant{Py_True}. As
218such, the normal creation and deletion functions don't apply to
219booleans. The following macros are available, however.
220
Andrew M. Kuchling4eb1a002004-08-07 20:19:24 +0000221\begin{cfuncdesc}{int}{PyBool_Check}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000222 Return true if \var{o} is of type \cdata{PyBool_Type}.
Skip Montanaro33ee76a2004-07-28 14:17:04 +0000223 \versionadded{2.3}
224\end{cfuncdesc}
225
Skip Montanaro6d3db702004-07-29 02:16:04 +0000226\begin{cvardesc}{PyObject*}{Py_False}
227 The Python \code{False} object. This object has no methods. It needs to
228 be treated just like any other object with respect to reference counts.
229\end{cvardesc}
Skip Montanaro33ee76a2004-07-28 14:17:04 +0000230
Skip Montanaro6d3db702004-07-29 02:16:04 +0000231\begin{cvardesc}{PyObject*}{Py_True}
232 The Python \code{True} object. This object has no methods. It needs to
233 be treated just like any other object with respect to reference counts.
234\end{cvardesc}
235
236\begin{csimplemacrodesc}{Py_RETURN_FALSE}
237 Return \constant{Py_False} from a function, properly incrementing its
238 reference count.
239\versionadded{2.4}
240\end{csimplemacrodesc}
241
242\begin{csimplemacrodesc}{Py_RETURN_TRUE}
Andrew M. Kuchling4eb1a002004-08-07 20:19:24 +0000243 Return \constant{Py_True} from a function, properly incrementing its
244 reference count.
Skip Montanaro33ee76a2004-07-28 14:17:04 +0000245\versionadded{2.4}
Skip Montanaro6d3db702004-07-29 02:16:04 +0000246\end{csimplemacrodesc}
Skip Montanaro33ee76a2004-07-28 14:17:04 +0000247
Georg Brandl99363b62005-09-03 07:27:26 +0000248\begin{cfuncdesc}{PyObject*}{PyBool_FromLong}{long v}
Thomas Wouters477c8d52006-05-27 19:21:47 +0000249 Return a new reference to \constant{Py_True} or \constant{Py_False}
Georg Brandl99363b62005-09-03 07:27:26 +0000250 depending on the truth value of \var{v}.
Skip Montanaro33ee76a2004-07-28 14:17:04 +0000251\versionadded{2.3}
252\end{cfuncdesc}
Fred Drake3adf79e2001-10-12 19:01:43 +0000253
254\subsection{Long Integer Objects \label{longObjects}}
255
256\obindex{long integer}
257\begin{ctypedesc}{PyLongObject}
258 This subtype of \ctype{PyObject} represents a Python long integer
259 object.
260\end{ctypedesc}
261
262\begin{cvardesc}{PyTypeObject}{PyLong_Type}
263 This instance of \ctype{PyTypeObject} represents the Python long
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000264 integer type. This is the same object as \code{long} and
265 \code{types.LongType}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000266 \withsubitem{(in modules types)}{\ttindex{LongType}}
267\end{cvardesc}
268
269\begin{cfuncdesc}{int}{PyLong_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +0000270 Return true if its argument is a \ctype{PyLongObject} or a subtype
Fred Drake3adf79e2001-10-12 19:01:43 +0000271 of \ctype{PyLongObject}.
272 \versionchanged[Allowed subtypes to be accepted]{2.2}
273\end{cfuncdesc}
274
275\begin{cfuncdesc}{int}{PyLong_CheckExact}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +0000276 Return true if its argument is a \ctype{PyLongObject}, but not a
Fred Drake3adf79e2001-10-12 19:01:43 +0000277 subtype of \ctype{PyLongObject}.
278 \versionadded{2.2}
279\end{cfuncdesc}
280
281\begin{cfuncdesc}{PyObject*}{PyLong_FromLong}{long v}
Georg Brandl99363b62005-09-03 07:27:26 +0000282 Return a new \ctype{PyLongObject} object from \var{v}, or \NULL{}
Fred Drake3adf79e2001-10-12 19:01:43 +0000283 on failure.
284\end{cfuncdesc}
285
286\begin{cfuncdesc}{PyObject*}{PyLong_FromUnsignedLong}{unsigned long v}
Georg Brandl99363b62005-09-03 07:27:26 +0000287 Return a new \ctype{PyLongObject} object from a C \ctype{unsigned
Fred Drake3adf79e2001-10-12 19:01:43 +0000288 long}, or \NULL{} on failure.
289\end{cfuncdesc}
290
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000291\begin{cfuncdesc}{PyObject*}{PyLong_FromLongLong}{PY_LONG_LONG v}
Georg Brandl99363b62005-09-03 07:27:26 +0000292 Return a new \ctype{PyLongObject} object from a C \ctype{long long},
Fred Drake3adf79e2001-10-12 19:01:43 +0000293 or \NULL{} on failure.
294\end{cfuncdesc}
295
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000296\begin{cfuncdesc}{PyObject*}{PyLong_FromUnsignedLongLong}{unsigned PY_LONG_LONG v}
Georg Brandl99363b62005-09-03 07:27:26 +0000297 Return a new \ctype{PyLongObject} object from a C \ctype{unsigned
Fred Drake3adf79e2001-10-12 19:01:43 +0000298 long long}, or \NULL{} on failure.
299\end{cfuncdesc}
300
301\begin{cfuncdesc}{PyObject*}{PyLong_FromDouble}{double v}
Georg Brandl99363b62005-09-03 07:27:26 +0000302 Return a new \ctype{PyLongObject} object from the integer part of
Fred Drake3adf79e2001-10-12 19:01:43 +0000303 \var{v}, or \NULL{} on failure.
304\end{cfuncdesc}
305
306\begin{cfuncdesc}{PyObject*}{PyLong_FromString}{char *str, char **pend,
307 int base}
308 Return a new \ctype{PyLongObject} based on the string value in
309 \var{str}, which is interpreted according to the radix in
Tim Peters9ddf40b2004-06-20 22:41:32 +0000310 \var{base}. If \var{pend} is non-\NULL{}, \code{*\var{pend}} will
Fred Drake3adf79e2001-10-12 19:01:43 +0000311 point to the first character in \var{str} which follows the
312 representation of the number. If \var{base} is \code{0}, the radix
Skip Montanaro1ff49a72003-02-03 05:13:24 +0000313 will be determined based on the leading characters of \var{str}: if
Fred Drake3adf79e2001-10-12 19:01:43 +0000314 \var{str} starts with \code{'0x'} or \code{'0X'}, radix 16 will be
315 used; if \var{str} starts with \code{'0'}, radix 8 will be used;
316 otherwise radix 10 will be used. If \var{base} is not \code{0}, it
317 must be between \code{2} and \code{36}, inclusive. Leading spaces
318 are ignored. If there are no digits, \exception{ValueError} will be
319 raised.
320\end{cfuncdesc}
321
322\begin{cfuncdesc}{PyObject*}{PyLong_FromUnicode}{Py_UNICODE *u,
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000323 Py_ssize_t length, int base}
Fred Drake3adf79e2001-10-12 19:01:43 +0000324 Convert a sequence of Unicode digits to a Python long integer
325 value. The first parameter, \var{u}, points to the first character
326 of the Unicode string, \var{length} gives the number of characters,
327 and \var{base} is the radix for the conversion. The radix must be
328 in the range [2, 36]; if it is out of range, \exception{ValueError}
329 will be raised.
330 \versionadded{1.6}
331\end{cfuncdesc}
332
333\begin{cfuncdesc}{PyObject*}{PyLong_FromVoidPtr}{void *p}
334 Create a Python integer or long integer from the pointer \var{p}.
335 The pointer value can be retrieved from the resulting value using
336 \cfunction{PyLong_AsVoidPtr()}.
337 \versionadded{1.5.2}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000338 \versionchanged[If the integer is larger than LONG_MAX,
339 a positive long integer is returned]{2.5}
340 \end{cfuncdesc}
Fred Drake3adf79e2001-10-12 19:01:43 +0000341
342\begin{cfuncdesc}{long}{PyLong_AsLong}{PyObject *pylong}
Georg Brandl99363b62005-09-03 07:27:26 +0000343 Return a C \ctype{long} representation of the contents of
Fred Drake3adf79e2001-10-12 19:01:43 +0000344 \var{pylong}. If \var{pylong} is greater than
345 \constant{LONG_MAX}\ttindex{LONG_MAX}, an \exception{OverflowError}
346 is raised.
347 \withsubitem{(built-in exception)}{\ttindex{OverflowError}}
348\end{cfuncdesc}
349
350\begin{cfuncdesc}{unsigned long}{PyLong_AsUnsignedLong}{PyObject *pylong}
Georg Brandl99363b62005-09-03 07:27:26 +0000351 Return a C \ctype{unsigned long} representation of the contents of
Fred Drake3adf79e2001-10-12 19:01:43 +0000352 \var{pylong}. If \var{pylong} is greater than
353 \constant{ULONG_MAX}\ttindex{ULONG_MAX}, an
354 \exception{OverflowError} is raised.
Tim Petersf582b822001-12-11 18:51:08 +0000355 \withsubitem{(built-in exception)}{\ttindex{OverflowError}}
Fred Drake3adf79e2001-10-12 19:01:43 +0000356\end{cfuncdesc}
357
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000358\begin{cfuncdesc}{PY_LONG_LONG}{PyLong_AsLongLong}{PyObject *pylong}
Fred Drake3adf79e2001-10-12 19:01:43 +0000359 Return a C \ctype{long long} from a Python long integer. If
360 \var{pylong} cannot be represented as a \ctype{long long}, an
361 \exception{OverflowError} will be raised.
362 \versionadded{2.2}
363\end{cfuncdesc}
364
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000365\begin{cfuncdesc}{unsigned PY_LONG_LONG}{PyLong_AsUnsignedLongLong}{PyObject
Fred Drake3adf79e2001-10-12 19:01:43 +0000366 *pylong}
367 Return a C \ctype{unsigned long long} from a Python long integer.
368 If \var{pylong} cannot be represented as an \ctype{unsigned long
369 long}, an \exception{OverflowError} will be raised if the value is
370 positive, or a \exception{TypeError} will be raised if the value is
371 negative.
372 \versionadded{2.2}
373\end{cfuncdesc}
374
Thomas Heller34d7f092003-04-23 19:51:05 +0000375\begin{cfuncdesc}{unsigned long}{PyLong_AsUnsignedLongMask}{PyObject *io}
376 Return a C \ctype{unsigned long} from a Python long integer, without
377 checking for overflow.
378 \versionadded{2.3}
379\end{cfuncdesc}
380
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000381\begin{cfuncdesc}{unsigned PY_LONG_LONG}{PyLong_AsUnsignedLongLongMask}{PyObject *io}
Thomas Heller34d7f092003-04-23 19:51:05 +0000382 Return a C \ctype{unsigned long long} from a Python long integer, without
383 checking for overflow.
384 \versionadded{2.3}
385\end{cfuncdesc}
386
Fred Drake3adf79e2001-10-12 19:01:43 +0000387\begin{cfuncdesc}{double}{PyLong_AsDouble}{PyObject *pylong}
Georg Brandl99363b62005-09-03 07:27:26 +0000388 Return a C \ctype{double} representation of the contents of
Fred Drake3adf79e2001-10-12 19:01:43 +0000389 \var{pylong}. If \var{pylong} cannot be approximately represented
390 as a \ctype{double}, an \exception{OverflowError} exception is
391 raised and \code{-1.0} will be returned.
392\end{cfuncdesc}
393
394\begin{cfuncdesc}{void*}{PyLong_AsVoidPtr}{PyObject *pylong}
395 Convert a Python integer or long integer \var{pylong} to a C
396 \ctype{void} pointer. If \var{pylong} cannot be converted, an
397 \exception{OverflowError} will be raised. This is only assured to
398 produce a usable \ctype{void} pointer for values created with
399 \cfunction{PyLong_FromVoidPtr()}.
400 \versionadded{1.5.2}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000401 \versionchanged[For values outside 0..LONG_MAX, both signed and
402 unsigned integers are acccepted]{2.5}
Fred Drake3adf79e2001-10-12 19:01:43 +0000403\end{cfuncdesc}
404
405
406\subsection{Floating Point Objects \label{floatObjects}}
407
408\obindex{floating point}
409\begin{ctypedesc}{PyFloatObject}
410 This subtype of \ctype{PyObject} represents a Python floating point
411 object.
412\end{ctypedesc}
413
414\begin{cvardesc}{PyTypeObject}{PyFloat_Type}
415 This instance of \ctype{PyTypeObject} represents the Python floating
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000416 point type. This is the same object as \code{float} and
417 \code{types.FloatType}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000418 \withsubitem{(in modules types)}{\ttindex{FloatType}}
419\end{cvardesc}
420
421\begin{cfuncdesc}{int}{PyFloat_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +0000422 Return true if its argument is a \ctype{PyFloatObject} or a subtype
Fred Drake3adf79e2001-10-12 19:01:43 +0000423 of \ctype{PyFloatObject}.
424 \versionchanged[Allowed subtypes to be accepted]{2.2}
425\end{cfuncdesc}
426
427\begin{cfuncdesc}{int}{PyFloat_CheckExact}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +0000428 Return true if its argument is a \ctype{PyFloatObject}, but not a
Fred Drake3adf79e2001-10-12 19:01:43 +0000429 subtype of \ctype{PyFloatObject}.
430 \versionadded{2.2}
431\end{cfuncdesc}
432
Georg Brandl428f0642007-03-18 18:35:15 +0000433\begin{cfuncdesc}{PyObject*}{PyFloat_FromString}{PyObject *str}
Georg Brandl99363b62005-09-03 07:27:26 +0000434 Create a \ctype{PyFloatObject} object based on the string value in
Georg Brandl428f0642007-03-18 18:35:15 +0000435 \var{str}, or \NULL{} on failure.
Skip Montanaroae31e9b2003-02-03 03:56:36 +0000436\end{cfuncdesc}
437
Fred Drake3adf79e2001-10-12 19:01:43 +0000438\begin{cfuncdesc}{PyObject*}{PyFloat_FromDouble}{double v}
Georg Brandl99363b62005-09-03 07:27:26 +0000439 Create a \ctype{PyFloatObject} object from \var{v}, or \NULL{} on
Fred Drake3adf79e2001-10-12 19:01:43 +0000440 failure.
441\end{cfuncdesc}
442
443\begin{cfuncdesc}{double}{PyFloat_AsDouble}{PyObject *pyfloat}
Georg Brandl99363b62005-09-03 07:27:26 +0000444 Return a C \ctype{double} representation of the contents of
Fred Drake3adf79e2001-10-12 19:01:43 +0000445 \var{pyfloat}.
446\end{cfuncdesc}
447
448\begin{cfuncdesc}{double}{PyFloat_AS_DOUBLE}{PyObject *pyfloat}
Georg Brandl99363b62005-09-03 07:27:26 +0000449 Return a C \ctype{double} representation of the contents of
Fred Drake3adf79e2001-10-12 19:01:43 +0000450 \var{pyfloat}, but without error checking.
451\end{cfuncdesc}
452
453
454\subsection{Complex Number Objects \label{complexObjects}}
455
456\obindex{complex number}
457Python's complex number objects are implemented as two distinct types
458when viewed from the C API: one is the Python object exposed to
459Python programs, and the other is a C structure which represents the
460actual complex number value. The API provides functions for working
461with both.
462
463\subsubsection{Complex Numbers as C Structures}
464
465Note that the functions which accept these structures as parameters
466and return them as results do so \emph{by value} rather than
467dereferencing them through pointers. This is consistent throughout
468the API.
469
470\begin{ctypedesc}{Py_complex}
471 The C structure which corresponds to the value portion of a Python
472 complex number object. Most of the functions for dealing with
473 complex number objects use structures of this type as input or
474 output values, as appropriate. It is defined as:
475
476\begin{verbatim}
477typedef struct {
478 double real;
479 double imag;
480} Py_complex;
481\end{verbatim}
482\end{ctypedesc}
483
484\begin{cfuncdesc}{Py_complex}{_Py_c_sum}{Py_complex left, Py_complex right}
485 Return the sum of two complex numbers, using the C
486 \ctype{Py_complex} representation.
487\end{cfuncdesc}
488
489\begin{cfuncdesc}{Py_complex}{_Py_c_diff}{Py_complex left, Py_complex right}
490 Return the difference between two complex numbers, using the C
491 \ctype{Py_complex} representation.
492\end{cfuncdesc}
493
494\begin{cfuncdesc}{Py_complex}{_Py_c_neg}{Py_complex complex}
495 Return the negation of the complex number \var{complex}, using the C
496 \ctype{Py_complex} representation.
497\end{cfuncdesc}
498
499\begin{cfuncdesc}{Py_complex}{_Py_c_prod}{Py_complex left, Py_complex right}
500 Return the product of two complex numbers, using the C
501 \ctype{Py_complex} representation.
502\end{cfuncdesc}
503
504\begin{cfuncdesc}{Py_complex}{_Py_c_quot}{Py_complex dividend,
505 Py_complex divisor}
506 Return the quotient of two complex numbers, using the C
507 \ctype{Py_complex} representation.
508\end{cfuncdesc}
509
510\begin{cfuncdesc}{Py_complex}{_Py_c_pow}{Py_complex num, Py_complex exp}
511 Return the exponentiation of \var{num} by \var{exp}, using the C
512 \ctype{Py_complex} representation.
513\end{cfuncdesc}
514
515
516\subsubsection{Complex Numbers as Python Objects}
517
518\begin{ctypedesc}{PyComplexObject}
519 This subtype of \ctype{PyObject} represents a Python complex number
520 object.
521\end{ctypedesc}
522
523\begin{cvardesc}{PyTypeObject}{PyComplex_Type}
524 This instance of \ctype{PyTypeObject} represents the Python complex
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000525 number type. It is the same object as \code{complex} and
526 \code{types.ComplexType}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000527\end{cvardesc}
528
529\begin{cfuncdesc}{int}{PyComplex_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +0000530 Return true if its argument is a \ctype{PyComplexObject} or a
Fred Drake3adf79e2001-10-12 19:01:43 +0000531 subtype of \ctype{PyComplexObject}.
532 \versionchanged[Allowed subtypes to be accepted]{2.2}
533\end{cfuncdesc}
534
535\begin{cfuncdesc}{int}{PyComplex_CheckExact}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +0000536 Return true if its argument is a \ctype{PyComplexObject}, but not a
Fred Drake3adf79e2001-10-12 19:01:43 +0000537 subtype of \ctype{PyComplexObject}.
538 \versionadded{2.2}
539\end{cfuncdesc}
540
541\begin{cfuncdesc}{PyObject*}{PyComplex_FromCComplex}{Py_complex v}
542 Create a new Python complex number object from a C
543 \ctype{Py_complex} value.
544\end{cfuncdesc}
545
546\begin{cfuncdesc}{PyObject*}{PyComplex_FromDoubles}{double real, double imag}
Georg Brandl99363b62005-09-03 07:27:26 +0000547 Return a new \ctype{PyComplexObject} object from \var{real} and
Fred Drake3adf79e2001-10-12 19:01:43 +0000548 \var{imag}.
549\end{cfuncdesc}
550
551\begin{cfuncdesc}{double}{PyComplex_RealAsDouble}{PyObject *op}
Georg Brandl99363b62005-09-03 07:27:26 +0000552 Return the real part of \var{op} as a C \ctype{double}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000553\end{cfuncdesc}
554
555\begin{cfuncdesc}{double}{PyComplex_ImagAsDouble}{PyObject *op}
Georg Brandl99363b62005-09-03 07:27:26 +0000556 Return the imaginary part of \var{op} as a C \ctype{double}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000557\end{cfuncdesc}
558
559\begin{cfuncdesc}{Py_complex}{PyComplex_AsCComplex}{PyObject *op}
Georg Brandl99363b62005-09-03 07:27:26 +0000560 Return the \ctype{Py_complex} value of the complex number
Fred Drake3adf79e2001-10-12 19:01:43 +0000561 \var{op}.
562\end{cfuncdesc}
563
564
565
566\section{Sequence Objects \label{sequenceObjects}}
567
568\obindex{sequence}
Tim Petersf582b822001-12-11 18:51:08 +0000569Generic operations on sequence objects were discussed in the previous
570chapter; this section deals with the specific kinds of sequence
Fred Drake3adf79e2001-10-12 19:01:43 +0000571objects that are intrinsic to the Python language.
572
573
574\subsection{String Objects \label{stringObjects}}
575
576These functions raise \exception{TypeError} when expecting a string
577parameter and are called with a non-string parameter.
578
579\obindex{string}
580\begin{ctypedesc}{PyStringObject}
581 This subtype of \ctype{PyObject} represents a Python string object.
582\end{ctypedesc}
583
584\begin{cvardesc}{PyTypeObject}{PyString_Type}
585 This instance of \ctype{PyTypeObject} represents the Python string
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000586 type; it is the same object as \code{str} and \code{types.StringType}
587 in the Python layer.
Fred Drake3adf79e2001-10-12 19:01:43 +0000588 \withsubitem{(in module types)}{\ttindex{StringType}}.
589\end{cvardesc}
590
591\begin{cfuncdesc}{int}{PyString_Check}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000592 Return true if the object \var{o} is a string object or an instance
Fred Drake3adf79e2001-10-12 19:01:43 +0000593 of a subtype of the string type.
594 \versionchanged[Allowed subtypes to be accepted]{2.2}
595\end{cfuncdesc}
596
597\begin{cfuncdesc}{int}{PyString_CheckExact}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000598 Return true if the object \var{o} is a string object, but not an
Fred Drake3adf79e2001-10-12 19:01:43 +0000599 instance of a subtype of the string type.
600 \versionadded{2.2}
601\end{cfuncdesc}
602
603\begin{cfuncdesc}{PyObject*}{PyString_FromString}{const char *v}
Thomas Wouters89f507f2006-12-13 04:49:30 +0000604 Return a new string object with a copy of the string \var{v} as value
605 on success, and \NULL{} on failure. The parameter \var{v} must not be
606 \NULL{}; it will not be checked.
Fred Drake3adf79e2001-10-12 19:01:43 +0000607\end{cfuncdesc}
608
609\begin{cfuncdesc}{PyObject*}{PyString_FromStringAndSize}{const char *v,
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000610 Py_ssize_t len}
Thomas Wouters89f507f2006-12-13 04:49:30 +0000611 Return a new string object with a copy of the string \var{v} as value
612 and length \var{len} on success, and \NULL{} on failure. If \var{v} is
Tim Peters9ddf40b2004-06-20 22:41:32 +0000613 \NULL{}, the contents of the string are uninitialized.
Fred Drake3adf79e2001-10-12 19:01:43 +0000614\end{cfuncdesc}
615
616\begin{cfuncdesc}{PyObject*}{PyString_FromFormat}{const char *format, ...}
Georg Brandl99363b62005-09-03 07:27:26 +0000617 Take a C \cfunction{printf()}-style \var{format} string and a
618 variable number of arguments, calculate the size of the resulting
619 Python string and return a string with the values formatted into
Fred Drake3adf79e2001-10-12 19:01:43 +0000620 it. The variable arguments must be C types and must correspond
621 exactly to the format characters in the \var{format} string. The
622 following format characters are allowed:
623
Thomas Wouters477c8d52006-05-27 19:21:47 +0000624 % This should be exactly the same as the table in PyErr_Format.
625 % One should just refer to the other.
626
627 % The descriptions for %zd and %zu are wrong, but the truth is complicated
628 % because not all compilers support the %z width modifier -- we fake it
629 % when necessary via interpolating PY_FORMAT_SIZE_T.
630
631 % %u, %lu, %zu should have "new in Python 2.5" blurbs.
632
Fred Drake3adf79e2001-10-12 19:01:43 +0000633 \begin{tableiii}{l|l|l}{member}{Format Characters}{Type}{Comment}
634 \lineiii{\%\%}{\emph{n/a}}{The literal \% character.}
635 \lineiii{\%c}{int}{A single character, represented as an C int.}
636 \lineiii{\%d}{int}{Exactly equivalent to \code{printf("\%d")}.}
Thomas Wouters477c8d52006-05-27 19:21:47 +0000637 \lineiii{\%u}{unsigned int}{Exactly equivalent to \code{printf("\%u")}.}
Fred Drake3adf79e2001-10-12 19:01:43 +0000638 \lineiii{\%ld}{long}{Exactly equivalent to \code{printf("\%ld")}.}
Thomas Wouters477c8d52006-05-27 19:21:47 +0000639 \lineiii{\%lu}{unsigned long}{Exactly equivalent to \code{printf("\%lu")}.}
640 \lineiii{\%zd}{Py_ssize_t}{Exactly equivalent to \code{printf("\%zd")}.}
641 \lineiii{\%zu}{size_t}{Exactly equivalent to \code{printf("\%zu")}.}
Fred Drake3adf79e2001-10-12 19:01:43 +0000642 \lineiii{\%i}{int}{Exactly equivalent to \code{printf("\%i")}.}
643 \lineiii{\%x}{int}{Exactly equivalent to \code{printf("\%x")}.}
644 \lineiii{\%s}{char*}{A null-terminated C character array.}
645 \lineiii{\%p}{void*}{The hex representation of a C pointer.
646 Mostly equivalent to \code{printf("\%p")} except that it is
647 guaranteed to start with the literal \code{0x} regardless of
648 what the platform's \code{printf} yields.}
649 \end{tableiii}
Thomas Wouters477c8d52006-05-27 19:21:47 +0000650
651 An unrecognized format character causes all the rest of the format
652 string to be copied as-is to the result string, and any extra
653 arguments discarded.
Fred Drake3adf79e2001-10-12 19:01:43 +0000654\end{cfuncdesc}
655
656\begin{cfuncdesc}{PyObject*}{PyString_FromFormatV}{const char *format,
657 va_list vargs}
658 Identical to \function{PyString_FromFormat()} except that it takes
659 exactly two arguments.
660\end{cfuncdesc}
661
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000662\begin{cfuncdesc}{Py_ssize_t}{PyString_Size}{PyObject *string}
Georg Brandl99363b62005-09-03 07:27:26 +0000663 Return the length of the string in string object \var{string}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000664\end{cfuncdesc}
665
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000666\begin{cfuncdesc}{Py_ssize_t}{PyString_GET_SIZE}{PyObject *string}
Fred Drake3adf79e2001-10-12 19:01:43 +0000667 Macro form of \cfunction{PyString_Size()} but without error
668 checking.
669\end{cfuncdesc}
670
671\begin{cfuncdesc}{char*}{PyString_AsString}{PyObject *string}
Georg Brandl99363b62005-09-03 07:27:26 +0000672 Return a NUL-terminated representation of the contents of
Fred Drake3adf79e2001-10-12 19:01:43 +0000673 \var{string}. The pointer refers to the internal buffer of
674 \var{string}, not a copy. The data must not be modified in any way,
675 unless the string was just created using
676 \code{PyString_FromStringAndSize(NULL, \var{size})}.
Fred Drake4b247262002-10-22 20:20:20 +0000677 It must not be deallocated. If \var{string} is a Unicode object,
678 this function computes the default encoding of \var{string} and
679 operates on that. If \var{string} is not a string object at all,
680 \cfunction{PyString_AsString()} returns \NULL{} and raises
681 \exception{TypeError}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000682\end{cfuncdesc}
683
684\begin{cfuncdesc}{char*}{PyString_AS_STRING}{PyObject *string}
685 Macro form of \cfunction{PyString_AsString()} but without error
Fred Drake4b247262002-10-22 20:20:20 +0000686 checking. Only string objects are supported; no Unicode objects
687 should be passed.
Fred Drake3adf79e2001-10-12 19:01:43 +0000688\end{cfuncdesc}
689
690\begin{cfuncdesc}{int}{PyString_AsStringAndSize}{PyObject *obj,
691 char **buffer,
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000692 Py_ssize_t *length}
Georg Brandl99363b62005-09-03 07:27:26 +0000693 Return a NUL-terminated representation of the contents of the
Fred Drake3adf79e2001-10-12 19:01:43 +0000694 object \var{obj} through the output variables \var{buffer} and
695 \var{length}.
696
697 The function accepts both string and Unicode objects as input. For
698 Unicode objects it returns the default encoded version of the
Tim Peters9ddf40b2004-06-20 22:41:32 +0000699 object. If \var{length} is \NULL{}, the resulting buffer may not
Fred Drake4b247262002-10-22 20:20:20 +0000700 contain NUL characters; if it does, the function returns \code{-1}
701 and a \exception{TypeError} is raised.
Fred Drake3adf79e2001-10-12 19:01:43 +0000702
703 The buffer refers to an internal string buffer of \var{obj}, not a
704 copy. The data must not be modified in any way, unless the string
705 was just created using \code{PyString_FromStringAndSize(NULL,
Fred Drake4b247262002-10-22 20:20:20 +0000706 \var{size})}. It must not be deallocated. If \var{string} is a
707 Unicode object, this function computes the default encoding of
708 \var{string} and operates on that. If \var{string} is not a string
Thomas Wouters477c8d52006-05-27 19:21:47 +0000709 object at all, \cfunction{PyString_AsStringAndSize()} returns
Georg Brandle53475d2005-09-28 12:53:12 +0000710 \code{-1} and raises \exception{TypeError}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000711\end{cfuncdesc}
712
713\begin{cfuncdesc}{void}{PyString_Concat}{PyObject **string,
714 PyObject *newpart}
Georg Brandl99363b62005-09-03 07:27:26 +0000715 Create a new string object in \var{*string} containing the contents
Fred Drake3adf79e2001-10-12 19:01:43 +0000716 of \var{newpart} appended to \var{string}; the caller will own the
717 new reference. The reference to the old value of \var{string} will
718 be stolen. If the new string cannot be created, the old reference
719 to \var{string} will still be discarded and the value of
Tim Peters9ddf40b2004-06-20 22:41:32 +0000720 \var{*string} will be set to \NULL{}; the appropriate exception will
Fred Drake3adf79e2001-10-12 19:01:43 +0000721 be set.
722\end{cfuncdesc}
723
724\begin{cfuncdesc}{void}{PyString_ConcatAndDel}{PyObject **string,
725 PyObject *newpart}
Georg Brandl99363b62005-09-03 07:27:26 +0000726 Create a new string object in \var{*string} containing the contents
Fred Drake3adf79e2001-10-12 19:01:43 +0000727 of \var{newpart} appended to \var{string}. This version decrements
728 the reference count of \var{newpart}.
729\end{cfuncdesc}
730
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000731\begin{cfuncdesc}{int}{_PyString_Resize}{PyObject **string, Py_ssize_t newsize}
Fred Drake3adf79e2001-10-12 19:01:43 +0000732 A way to resize a string object even though it is ``immutable''.
733 Only use this to build up a brand new string object; don't use this
Tim Peters5de98422002-04-27 18:44:32 +0000734 if the string may already be known in other parts of the code. It
735 is an error to call this function if the refcount on the input string
736 object is not one.
737 Pass the address of an existing string object as an lvalue (it may
738 be written into), and the new size desired. On success, \var{*string}
Fred Drake432425e2002-04-29 15:17:16 +0000739 holds the resized string object and \code{0} is returned; the address in
Tim Peters5de98422002-04-27 18:44:32 +0000740 \var{*string} may differ from its input value. If the
741 reallocation fails, the original string object at \var{*string} is
742 deallocated, \var{*string} is set to \NULL{}, a memory exception is set,
Fred Drake432425e2002-04-29 15:17:16 +0000743 and \code{-1} is returned.
Fred Drake3adf79e2001-10-12 19:01:43 +0000744\end{cfuncdesc}
745
746\begin{cfuncdesc}{PyObject*}{PyString_Format}{PyObject *format,
747 PyObject *args}
Georg Brandl99363b62005-09-03 07:27:26 +0000748 Return a new string object from \var{format} and \var{args}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000749 Analogous to \code{\var{format} \%\ \var{args}}. The \var{args}
750 argument must be a tuple.
751\end{cfuncdesc}
752
753\begin{cfuncdesc}{void}{PyString_InternInPlace}{PyObject **string}
754 Intern the argument \var{*string} in place. The argument must be
755 the address of a pointer variable pointing to a Python string
756 object. If there is an existing interned string that is the same as
757 \var{*string}, it sets \var{*string} to it (decrementing the
758 reference count of the old string object and incrementing the
759 reference count of the interned string object), otherwise it leaves
760 \var{*string} alone and interns it (incrementing its reference
761 count). (Clarification: even though there is a lot of talk about
762 reference counts, think of this function as reference-count-neutral;
763 you own the object after the call if and only if you owned it before
764 the call.)
765\end{cfuncdesc}
766
767\begin{cfuncdesc}{PyObject*}{PyString_InternFromString}{const char *v}
768 A combination of \cfunction{PyString_FromString()} and
769 \cfunction{PyString_InternInPlace()}, returning either a new string
770 object that has been interned, or a new (``owned'') reference to an
771 earlier interned string object with the same value.
772\end{cfuncdesc}
773
774\begin{cfuncdesc}{PyObject*}{PyString_Decode}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000775 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +0000776 const char *encoding,
777 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +0000778 Create an object by decoding \var{size} bytes of the encoded
Fred Drake3adf79e2001-10-12 19:01:43 +0000779 buffer \var{s} using the codec registered for
780 \var{encoding}. \var{encoding} and \var{errors} have the same
781 meaning as the parameters of the same name in the
782 \function{unicode()} built-in function. The codec to be used is
Georg Brandl99363b62005-09-03 07:27:26 +0000783 looked up using the Python codec registry. Return \NULL{} if
Fred Drake3adf79e2001-10-12 19:01:43 +0000784 an exception was raised by the codec.
785\end{cfuncdesc}
786
787\begin{cfuncdesc}{PyObject*}{PyString_AsDecodedObject}{PyObject *str,
788 const char *encoding,
789 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +0000790 Decode a string object by passing it to the codec registered for
791 \var{encoding} and return the result as Python
Fred Drake3adf79e2001-10-12 19:01:43 +0000792 object. \var{encoding} and \var{errors} have the same meaning as the
793 parameters of the same name in the string \method{encode()} method.
794 The codec to be used is looked up using the Python codec registry.
Georg Brandl99363b62005-09-03 07:27:26 +0000795 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +0000796\end{cfuncdesc}
797
798\begin{cfuncdesc}{PyObject*}{PyString_Encode}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000799 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +0000800 const char *encoding,
801 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +0000802 Encode the \ctype{char} buffer of the given size by passing it to
803 the codec registered for \var{encoding} and return a Python object.
Fred Drake3adf79e2001-10-12 19:01:43 +0000804 \var{encoding} and \var{errors} have the same meaning as the
805 parameters of the same name in the string \method{encode()} method.
806 The codec to be used is looked up using the Python codec
Georg Brandl99363b62005-09-03 07:27:26 +0000807 registry. Return \NULL{} if an exception was raised by the
Fred Drake3adf79e2001-10-12 19:01:43 +0000808 codec.
809\end{cfuncdesc}
810
811\begin{cfuncdesc}{PyObject*}{PyString_AsEncodedObject}{PyObject *str,
812 const char *encoding,
813 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +0000814 Encode a string object using the codec registered for
815 \var{encoding} and return the result as Python object.
Fred Drake3adf79e2001-10-12 19:01:43 +0000816 \var{encoding} and \var{errors} have the same meaning as the
817 parameters of the same name in the string \method{encode()} method.
818 The codec to be used is looked up using the Python codec registry.
Georg Brandl99363b62005-09-03 07:27:26 +0000819 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +0000820\end{cfuncdesc}
821
822
823\subsection{Unicode Objects \label{unicodeObjects}}
824\sectionauthor{Marc-Andre Lemburg}{mal@lemburg.com}
825
826%--- Unicode Type -------------------------------------------------------
827
828These are the basic Unicode object types used for the Unicode
829implementation in Python:
830
831\begin{ctypedesc}{Py_UNICODE}
Marc-André Lemburgdf4f6e92005-10-10 19:08:41 +0000832 This type represents the storage type which is used by Python
833 internally as basis for holding Unicode ordinals. Python's default
834 builds use a 16-bit type for \ctype{Py_UNICODE} and store Unicode
835 values internally as UCS2. It is also possible to build a UCS4
836 version of Python (most recent Linux distributions come with UCS4
837 builds of Python). These builds then use a 32-bit type for
838 \ctype{Py_UNICODE} and store Unicode data internally as UCS4. On
839 platforms where \ctype{wchar_t} is available and compatible with the
840 chosen Python Unicode build variant, \ctype{Py_UNICODE} is a typedef
841 alias for \ctype{wchar_t} to enhance native platform compatibility.
842 On all other platforms, \ctype{Py_UNICODE} is a typedef alias for
843 either \ctype{unsigned short} (UCS2) or \ctype{unsigned long}
844 (UCS4).
Fred Drake3adf79e2001-10-12 19:01:43 +0000845\end{ctypedesc}
846
Marc-André Lemburgdf4f6e92005-10-10 19:08:41 +0000847Note that UCS2 and UCS4 Python builds are not binary compatible.
848Please keep this in mind when writing extensions or interfaces.
849
Fred Drake3adf79e2001-10-12 19:01:43 +0000850\begin{ctypedesc}{PyUnicodeObject}
851 This subtype of \ctype{PyObject} represents a Python Unicode object.
852\end{ctypedesc}
853
854\begin{cvardesc}{PyTypeObject}{PyUnicode_Type}
855 This instance of \ctype{PyTypeObject} represents the Python Unicode
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000856 type. It is exposed to Python code as \code{unicode} and
857 \code{types.UnicodeType}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000858\end{cvardesc}
859
860The following APIs are really C macros and can be used to do fast
861checks and to access internal read-only data of Unicode objects:
862
863\begin{cfuncdesc}{int}{PyUnicode_Check}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000864 Return true if the object \var{o} is a Unicode object or an
Fred Drake3adf79e2001-10-12 19:01:43 +0000865 instance of a Unicode subtype.
866 \versionchanged[Allowed subtypes to be accepted]{2.2}
867\end{cfuncdesc}
868
869\begin{cfuncdesc}{int}{PyUnicode_CheckExact}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000870 Return true if the object \var{o} is a Unicode object, but not an
Fred Drake3adf79e2001-10-12 19:01:43 +0000871 instance of a subtype.
872 \versionadded{2.2}
873\end{cfuncdesc}
874
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000875\begin{cfuncdesc}{Py_ssize_t}{PyUnicode_GET_SIZE}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000876 Return the size of the object. \var{o} has to be a
Fred Drake3adf79e2001-10-12 19:01:43 +0000877 \ctype{PyUnicodeObject} (not checked).
878\end{cfuncdesc}
879
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000880\begin{cfuncdesc}{Py_ssize_t}{PyUnicode_GET_DATA_SIZE}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000881 Return the size of the object's internal buffer in bytes. \var{o}
Fred Drake3adf79e2001-10-12 19:01:43 +0000882 has to be a \ctype{PyUnicodeObject} (not checked).
883\end{cfuncdesc}
884
885\begin{cfuncdesc}{Py_UNICODE*}{PyUnicode_AS_UNICODE}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000886 Return a pointer to the internal \ctype{Py_UNICODE} buffer of the
Fred Drake3adf79e2001-10-12 19:01:43 +0000887 object. \var{o} has to be a \ctype{PyUnicodeObject} (not checked).
888\end{cfuncdesc}
889
890\begin{cfuncdesc}{const char*}{PyUnicode_AS_DATA}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000891 Return a pointer to the internal buffer of the object.
Fred Drake3adf79e2001-10-12 19:01:43 +0000892 \var{o} has to be a \ctype{PyUnicodeObject} (not checked).
893\end{cfuncdesc}
894
895% --- Unicode character properties ---------------------------------------
896
897Unicode provides many different character properties. The most often
898needed ones are available through these macros which are mapped to C
899functions depending on the Python configuration.
900
901\begin{cfuncdesc}{int}{Py_UNICODE_ISSPACE}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000902 Return 1 or 0 depending on whether \var{ch} is a whitespace
Fred Drake3adf79e2001-10-12 19:01:43 +0000903 character.
904\end{cfuncdesc}
905
906\begin{cfuncdesc}{int}{Py_UNICODE_ISLOWER}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000907 Return 1 or 0 depending on whether \var{ch} is a lowercase character.
Fred Drake3adf79e2001-10-12 19:01:43 +0000908\end{cfuncdesc}
909
910\begin{cfuncdesc}{int}{Py_UNICODE_ISUPPER}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000911 Return 1 or 0 depending on whether \var{ch} is an uppercase
Fred Drake3adf79e2001-10-12 19:01:43 +0000912 character.
913\end{cfuncdesc}
914
915\begin{cfuncdesc}{int}{Py_UNICODE_ISTITLE}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000916 Return 1 or 0 depending on whether \var{ch} is a titlecase character.
Fred Drake3adf79e2001-10-12 19:01:43 +0000917\end{cfuncdesc}
918
919\begin{cfuncdesc}{int}{Py_UNICODE_ISLINEBREAK}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000920 Return 1 or 0 depending on whether \var{ch} is a linebreak character.
Fred Drake3adf79e2001-10-12 19:01:43 +0000921\end{cfuncdesc}
922
923\begin{cfuncdesc}{int}{Py_UNICODE_ISDECIMAL}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000924 Return 1 or 0 depending on whether \var{ch} is a decimal character.
Fred Drake3adf79e2001-10-12 19:01:43 +0000925\end{cfuncdesc}
926
927\begin{cfuncdesc}{int}{Py_UNICODE_ISDIGIT}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000928 Return 1 or 0 depending on whether \var{ch} is a digit character.
Fred Drake3adf79e2001-10-12 19:01:43 +0000929\end{cfuncdesc}
930
931\begin{cfuncdesc}{int}{Py_UNICODE_ISNUMERIC}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000932 Return 1 or 0 depending on whether \var{ch} is a numeric character.
Fred Drake3adf79e2001-10-12 19:01:43 +0000933\end{cfuncdesc}
934
935\begin{cfuncdesc}{int}{Py_UNICODE_ISALPHA}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000936 Return 1 or 0 depending on whether \var{ch} is an alphabetic
Fred Drake3adf79e2001-10-12 19:01:43 +0000937 character.
938\end{cfuncdesc}
939
940\begin{cfuncdesc}{int}{Py_UNICODE_ISALNUM}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000941 Return 1 or 0 depending on whether \var{ch} is an alphanumeric
Fred Drake3adf79e2001-10-12 19:01:43 +0000942 character.
943\end{cfuncdesc}
944
945These APIs can be used for fast direct character conversions:
946
947\begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOLOWER}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000948 Return the character \var{ch} converted to lower case.
Fred Drake3adf79e2001-10-12 19:01:43 +0000949\end{cfuncdesc}
950
951\begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOUPPER}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000952 Return the character \var{ch} converted to upper case.
Fred Drake3adf79e2001-10-12 19:01:43 +0000953\end{cfuncdesc}
954
955\begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOTITLE}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000956 Return the character \var{ch} converted to title case.
Fred Drake3adf79e2001-10-12 19:01:43 +0000957\end{cfuncdesc}
958
959\begin{cfuncdesc}{int}{Py_UNICODE_TODECIMAL}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000960 Return the character \var{ch} converted to a decimal positive
961 integer. Return \code{-1} if this is not possible. This macro
962 does not raise exceptions.
Fred Drake3adf79e2001-10-12 19:01:43 +0000963\end{cfuncdesc}
964
965\begin{cfuncdesc}{int}{Py_UNICODE_TODIGIT}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000966 Return the character \var{ch} converted to a single digit integer.
967 Return \code{-1} if this is not possible. This macro does not raise
Fred Drake3adf79e2001-10-12 19:01:43 +0000968 exceptions.
969\end{cfuncdesc}
970
971\begin{cfuncdesc}{double}{Py_UNICODE_TONUMERIC}{Py_UNICODE ch}
Thomas Wouters477c8d52006-05-27 19:21:47 +0000972 Return the character \var{ch} converted to a double.
Georg Brandl99363b62005-09-03 07:27:26 +0000973 Return \code{-1.0} if this is not possible. This macro does not raise
Fred Drake3adf79e2001-10-12 19:01:43 +0000974 exceptions.
975\end{cfuncdesc}
976
977% --- Plain Py_UNICODE ---------------------------------------------------
978
979To create Unicode objects and access their basic sequence properties,
980use these APIs:
981
982\begin{cfuncdesc}{PyObject*}{PyUnicode_FromUnicode}{const Py_UNICODE *u,
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000983 Py_ssize_t size}
Fred Drake3adf79e2001-10-12 19:01:43 +0000984 Create a Unicode Object from the Py_UNICODE buffer \var{u} of the
985 given size. \var{u} may be \NULL{} which causes the contents to be
986 undefined. It is the user's responsibility to fill in the needed
987 data. The buffer is copied into the new object. If the buffer is
Tim Peters9ddf40b2004-06-20 22:41:32 +0000988 not \NULL{}, the return value might be a shared object. Therefore,
Fred Drake3adf79e2001-10-12 19:01:43 +0000989 modification of the resulting Unicode object is only allowed when
Tim Peters9ddf40b2004-06-20 22:41:32 +0000990 \var{u} is \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000991\end{cfuncdesc}
992
993\begin{cfuncdesc}{Py_UNICODE*}{PyUnicode_AsUnicode}{PyObject *unicode}
994 Return a read-only pointer to the Unicode object's internal
995 \ctype{Py_UNICODE} buffer, \NULL{} if \var{unicode} is not a Unicode
996 object.
997\end{cfuncdesc}
998
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000999\begin{cfuncdesc}{Py_ssize_t}{PyUnicode_GetSize}{PyObject *unicode}
Fred Drake3adf79e2001-10-12 19:01:43 +00001000 Return the length of the Unicode object.
1001\end{cfuncdesc}
1002
1003\begin{cfuncdesc}{PyObject*}{PyUnicode_FromEncodedObject}{PyObject *obj,
1004 const char *encoding,
1005 const char *errors}
1006 Coerce an encoded object \var{obj} to an Unicode object and return a
1007 reference with incremented refcount.
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001008
1009 String and other char buffer compatible objects are decoded
1010 according to the given encoding and using the error handling
1011 defined by errors. Both can be \NULL{} to have the interface
1012 use the default values (see the next section for details).
Fred Drake3adf79e2001-10-12 19:01:43 +00001013
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001014 All other objects, including Unicode objects, cause a
1015 \exception{TypeError} to be set.
Fred Drake3adf79e2001-10-12 19:01:43 +00001016
1017 The API returns \NULL{} if there was an error. The caller is
1018 responsible for decref'ing the returned objects.
1019\end{cfuncdesc}
1020
1021\begin{cfuncdesc}{PyObject*}{PyUnicode_FromObject}{PyObject *obj}
1022 Shortcut for \code{PyUnicode_FromEncodedObject(obj, NULL, "strict")}
1023 which is used throughout the interpreter whenever coercion to
1024 Unicode is needed.
1025\end{cfuncdesc}
1026
1027% --- wchar_t support for platforms which support it ---------------------
1028
1029If the platform supports \ctype{wchar_t} and provides a header file
1030wchar.h, Python can interface directly to this type using the
1031following functions. Support is optimized if Python's own
1032\ctype{Py_UNICODE} type is identical to the system's \ctype{wchar_t}.
1033
1034\begin{cfuncdesc}{PyObject*}{PyUnicode_FromWideChar}{const wchar_t *w,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001035 Py_ssize_t size}
Thomas Heller541703b2002-04-29 17:28:43 +00001036 Create a Unicode object from the \ctype{wchar_t} buffer \var{w} of
Georg Brandl99363b62005-09-03 07:27:26 +00001037 the given size. Return \NULL{} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00001038\end{cfuncdesc}
1039
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001040\begin{cfuncdesc}{Py_ssize_t}{PyUnicode_AsWideChar}{PyUnicodeObject *unicode,
Fred Drake3adf79e2001-10-12 19:01:43 +00001041 wchar_t *w,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001042 Py_ssize_t size}
Georg Brandl99363b62005-09-03 07:27:26 +00001043 Copy the Unicode object contents into the \ctype{wchar_t} buffer
Marc-André Lemburga9cadcd2004-11-22 13:02:31 +00001044 \var{w}. At most \var{size} \ctype{wchar_t} characters are copied
Georg Brandl99363b62005-09-03 07:27:26 +00001045 (excluding a possibly trailing 0-termination character). Return
Marc-André Lemburga9cadcd2004-11-22 13:02:31 +00001046 the number of \ctype{wchar_t} characters copied or -1 in case of an
1047 error. Note that the resulting \ctype{wchar_t} string may or may
1048 not be 0-terminated. It is the responsibility of the caller to make
1049 sure that the \ctype{wchar_t} string is 0-terminated in case this is
1050 required by the application.
Fred Drake3adf79e2001-10-12 19:01:43 +00001051\end{cfuncdesc}
1052
1053
1054\subsubsection{Built-in Codecs \label{builtinCodecs}}
1055
1056Python provides a set of builtin codecs which are written in C
1057for speed. All of these codecs are directly usable via the
1058following functions.
1059
1060Many of the following APIs take two arguments encoding and
1061errors. These parameters encoding and errors have the same semantics
1062as the ones of the builtin unicode() Unicode object constructor.
1063
1064Setting encoding to \NULL{} causes the default encoding to be used
1065which is \ASCII. The file system calls should use
1066\cdata{Py_FileSystemDefaultEncoding} as the encoding for file
1067names. This variable should be treated as read-only: On some systems,
1068it will be a pointer to a static string, on others, it will change at
Raymond Hettingercb2da432003-10-12 18:24:34 +00001069run-time (such as when the application invokes setlocale).
Fred Drake3adf79e2001-10-12 19:01:43 +00001070
1071Error handling is set by errors which may also be set to \NULL{}
1072meaning to use the default handling defined for the codec. Default
1073error handling for all builtin codecs is ``strict''
1074(\exception{ValueError} is raised).
1075
1076The codecs all use a similar interface. Only deviation from the
1077following generic ones are documented for simplicity.
1078
1079% --- Generic Codecs -----------------------------------------------------
1080
1081These are the generic codec APIs:
1082
1083\begin{cfuncdesc}{PyObject*}{PyUnicode_Decode}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001084 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001085 const char *encoding,
1086 const char *errors}
1087 Create a Unicode object by decoding \var{size} bytes of the encoded
1088 string \var{s}. \var{encoding} and \var{errors} have the same
1089 meaning as the parameters of the same name in the
1090 \function{unicode()} builtin function. The codec to be used is
Georg Brandl99363b62005-09-03 07:27:26 +00001091 looked up using the Python codec registry. Return \NULL{} if an
Fred Drake3adf79e2001-10-12 19:01:43 +00001092 exception was raised by the codec.
1093\end{cfuncdesc}
1094
1095\begin{cfuncdesc}{PyObject*}{PyUnicode_Encode}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001096 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001097 const char *encoding,
1098 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001099 Encode the \ctype{Py_UNICODE} buffer of the given size and return
Fred Drake3adf79e2001-10-12 19:01:43 +00001100 a Python string object. \var{encoding} and \var{errors} have the
1101 same meaning as the parameters of the same name in the Unicode
1102 \method{encode()} method. The codec to be used is looked up using
Georg Brandl99363b62005-09-03 07:27:26 +00001103 the Python codec registry. Return \NULL{} if an exception was
Fred Drake3adf79e2001-10-12 19:01:43 +00001104 raised by the codec.
1105\end{cfuncdesc}
1106
1107\begin{cfuncdesc}{PyObject*}{PyUnicode_AsEncodedString}{PyObject *unicode,
1108 const char *encoding,
1109 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001110 Encode a Unicode object and return the result as Python string
Fred Drake3adf79e2001-10-12 19:01:43 +00001111 object. \var{encoding} and \var{errors} have the same meaning as the
1112 parameters of the same name in the Unicode \method{encode()} method.
1113 The codec to be used is looked up using the Python codec registry.
Georg Brandl99363b62005-09-03 07:27:26 +00001114 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +00001115\end{cfuncdesc}
1116
1117% --- UTF-8 Codecs -------------------------------------------------------
1118
1119These are the UTF-8 codec APIs:
1120
1121\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF8}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001122 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001123 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001124 Create a Unicode object by decoding \var{size} bytes of the UTF-8
1125 encoded string \var{s}. Return \NULL{} if an exception was raised
Fred Drake3adf79e2001-10-12 19:01:43 +00001126 by the codec.
1127\end{cfuncdesc}
1128
Walter Dörwald69652032004-09-07 20:24:22 +00001129\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF8Stateful}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001130 Py_ssize_t size,
Walter Dörwald69652032004-09-07 20:24:22 +00001131 const char *errors,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001132 Py_ssize_t *consumed}
Georg Brandl99363b62005-09-03 07:27:26 +00001133 If \var{consumed} is \NULL{}, behave like \cfunction{PyUnicode_DecodeUTF8()}.
Walter Dörwald69652032004-09-07 20:24:22 +00001134 If \var{consumed} is not \NULL{}, trailing incomplete UTF-8 byte sequences
1135 will not be treated as an error. Those bytes will not be decoded and the
1136 number of bytes that have been decoded will be stored in \var{consumed}.
1137 \versionadded{2.4}
1138\end{cfuncdesc}
1139
Fred Drake3adf79e2001-10-12 19:01:43 +00001140\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUTF8}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001141 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001142 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001143 Encode the \ctype{Py_UNICODE} buffer of the given size using UTF-8
1144 and return a Python string object. Return \NULL{} if an exception
Fred Drake3adf79e2001-10-12 19:01:43 +00001145 was raised by the codec.
1146\end{cfuncdesc}
1147
1148\begin{cfuncdesc}{PyObject*}{PyUnicode_AsUTF8String}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001149 Encode a Unicode objects using UTF-8 and return the result as
1150 Python string object. Error handling is ``strict''. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00001151 \NULL{} if an exception was raised by the codec.
1152\end{cfuncdesc}
1153
1154% --- UTF-16 Codecs ------------------------------------------------------ */
1155
1156These are the UTF-16 codec APIs:
1157
1158\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF16}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001159 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001160 const char *errors,
1161 int *byteorder}
Georg Brandl99363b62005-09-03 07:27:26 +00001162 Decode \var{length} bytes from a UTF-16 encoded buffer string and
1163 return the corresponding Unicode object. \var{errors} (if
Tim Peters9ddf40b2004-06-20 22:41:32 +00001164 non-\NULL{}) defines the error handling. It defaults to ``strict''.
Fred Drake3adf79e2001-10-12 19:01:43 +00001165
Tim Peters9ddf40b2004-06-20 22:41:32 +00001166 If \var{byteorder} is non-\NULL{}, the decoder starts decoding using
Fred Drake3adf79e2001-10-12 19:01:43 +00001167 the given byte order:
1168
1169\begin{verbatim}
1170 *byteorder == -1: little endian
1171 *byteorder == 0: native order
1172 *byteorder == 1: big endian
1173\end{verbatim}
1174
1175 and then switches according to all byte order marks (BOM) it finds
1176 in the input data. BOMs are not copied into the resulting Unicode
1177 string. After completion, \var{*byteorder} is set to the current
1178 byte order at the end of input data.
1179
Tim Peters9ddf40b2004-06-20 22:41:32 +00001180 If \var{byteorder} is \NULL{}, the codec starts in native order mode.
Fred Drake3adf79e2001-10-12 19:01:43 +00001181
Georg Brandl99363b62005-09-03 07:27:26 +00001182 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +00001183\end{cfuncdesc}
1184
Walter Dörwald69652032004-09-07 20:24:22 +00001185\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF16Stateful}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001186 Py_ssize_t size,
Walter Dörwald69652032004-09-07 20:24:22 +00001187 const char *errors,
1188 int *byteorder,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001189 Py_ssize_t *consumed}
Georg Brandl99363b62005-09-03 07:27:26 +00001190 If \var{consumed} is \NULL{}, behave like
Walter Dörwald69652032004-09-07 20:24:22 +00001191 \cfunction{PyUnicode_DecodeUTF16()}. If \var{consumed} is not \NULL{},
1192 \cfunction{PyUnicode_DecodeUTF16Stateful()} will not treat trailing incomplete
Raymond Hettinger0c230b92005-08-17 10:05:22 +00001193 UTF-16 byte sequences (such as an odd number of bytes or a split surrogate pair)
Walter Dörwald69652032004-09-07 20:24:22 +00001194 as an error. Those bytes will not be decoded and the number of bytes that
1195 have been decoded will be stored in \var{consumed}.
1196 \versionadded{2.4}
1197\end{cfuncdesc}
1198
Fred Drake3adf79e2001-10-12 19:01:43 +00001199\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUTF16}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001200 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001201 const char *errors,
1202 int byteorder}
Georg Brandl99363b62005-09-03 07:27:26 +00001203 Return a Python string object holding the UTF-16 encoded value of
Fred Drake3adf79e2001-10-12 19:01:43 +00001204 the Unicode data in \var{s}. If \var{byteorder} is not \code{0},
1205 output is written according to the following byte order:
1206
1207\begin{verbatim}
1208 byteorder == -1: little endian
1209 byteorder == 0: native byte order (writes a BOM mark)
1210 byteorder == 1: big endian
1211\end{verbatim}
1212
1213 If byteorder is \code{0}, the output string will always start with
1214 the Unicode BOM mark (U+FEFF). In the other two modes, no BOM mark
1215 is prepended.
1216
Martin v. Löwis9bc4f2d2004-06-03 09:55:28 +00001217 If \var{Py_UNICODE_WIDE} is defined, a single \ctype{Py_UNICODE}
1218 value may get represented as a surrogate pair. If it is not
1219 defined, each \ctype{Py_UNICODE} values is interpreted as an
1220 UCS-2 character.
Fred Drake3adf79e2001-10-12 19:01:43 +00001221
Georg Brandl99363b62005-09-03 07:27:26 +00001222 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +00001223\end{cfuncdesc}
1224
1225\begin{cfuncdesc}{PyObject*}{PyUnicode_AsUTF16String}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001226 Return a Python string using the UTF-16 encoding in native byte
Fred Drake3adf79e2001-10-12 19:01:43 +00001227 order. The string always starts with a BOM mark. Error handling is
Georg Brandl99363b62005-09-03 07:27:26 +00001228 ``strict''. Return \NULL{} if an exception was raised by the
Fred Drake3adf79e2001-10-12 19:01:43 +00001229 codec.
1230\end{cfuncdesc}
1231
1232% --- Unicode-Escape Codecs ----------------------------------------------
1233
Martin v. Löwis95cf84a2003-10-19 07:32:24 +00001234These are the ``Unicode Escape'' codec APIs:
Fred Drake3adf79e2001-10-12 19:01:43 +00001235
1236\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUnicodeEscape}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001237 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001238 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001239 Create a Unicode object by decoding \var{size} bytes of the
1240 Unicode-Escape encoded string \var{s}. Return \NULL{} if an
Fred Drake3adf79e2001-10-12 19:01:43 +00001241 exception was raised by the codec.
1242\end{cfuncdesc}
1243
1244\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUnicodeEscape}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001245 Py_ssize_t size}
Georg Brandl99363b62005-09-03 07:27:26 +00001246 Encode the \ctype{Py_UNICODE} buffer of the given size using
1247 Unicode-Escape and return a Python string object. Return \NULL{}
Fred Drake3adf79e2001-10-12 19:01:43 +00001248 if an exception was raised by the codec.
1249\end{cfuncdesc}
1250
1251\begin{cfuncdesc}{PyObject*}{PyUnicode_AsUnicodeEscapeString}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001252 Encode a Unicode objects using Unicode-Escape and return the
Fred Drake3adf79e2001-10-12 19:01:43 +00001253 result as Python string object. Error handling is ``strict''.
Georg Brandl99363b62005-09-03 07:27:26 +00001254 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +00001255\end{cfuncdesc}
1256
1257% --- Raw-Unicode-Escape Codecs ------------------------------------------
1258
Martin v. Löwis95cf84a2003-10-19 07:32:24 +00001259These are the ``Raw Unicode Escape'' codec APIs:
Fred Drake3adf79e2001-10-12 19:01:43 +00001260
1261\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeRawUnicodeEscape}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001262 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001263 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001264 Create a Unicode object by decoding \var{size} bytes of the
1265 Raw-Unicode-Escape encoded string \var{s}. Return \NULL{} if an
Fred Drake3adf79e2001-10-12 19:01:43 +00001266 exception was raised by the codec.
1267\end{cfuncdesc}
1268
1269\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeRawUnicodeEscape}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001270 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001271 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001272 Encode the \ctype{Py_UNICODE} buffer of the given size using
1273 Raw-Unicode-Escape and return a Python string object. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00001274 \NULL{} if an exception was raised by the codec.
1275\end{cfuncdesc}
1276
1277\begin{cfuncdesc}{PyObject*}{PyUnicode_AsRawUnicodeEscapeString}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001278 Encode a Unicode objects using Raw-Unicode-Escape and return the
Fred Drake3adf79e2001-10-12 19:01:43 +00001279 result as Python string object. Error handling is ``strict''.
Georg Brandl99363b62005-09-03 07:27:26 +00001280 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +00001281\end{cfuncdesc}
1282
Tim Petersf582b822001-12-11 18:51:08 +00001283% --- Latin-1 Codecs -----------------------------------------------------
Fred Drake3adf79e2001-10-12 19:01:43 +00001284
1285These are the Latin-1 codec APIs:
1286Latin-1 corresponds to the first 256 Unicode ordinals and only these
1287are accepted by the codecs during encoding.
1288
1289\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeLatin1}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001290 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001291 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001292 Create a Unicode object by decoding \var{size} bytes of the Latin-1
1293 encoded string \var{s}. Return \NULL{} if an exception was raised
Fred Drake3adf79e2001-10-12 19:01:43 +00001294 by the codec.
1295\end{cfuncdesc}
1296
1297\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeLatin1}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001298 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001299 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001300 Encode the \ctype{Py_UNICODE} buffer of the given size using
1301 Latin-1 and return a Python string object. Return \NULL{} if an
Fred Drake3adf79e2001-10-12 19:01:43 +00001302 exception was raised by the codec.
1303\end{cfuncdesc}
1304
1305\begin{cfuncdesc}{PyObject*}{PyUnicode_AsLatin1String}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001306 Encode a Unicode objects using Latin-1 and return the result as
1307 Python string object. Error handling is ``strict''. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00001308 \NULL{} if an exception was raised by the codec.
1309\end{cfuncdesc}
1310
Tim Petersf582b822001-12-11 18:51:08 +00001311% --- ASCII Codecs -------------------------------------------------------
Fred Drake3adf79e2001-10-12 19:01:43 +00001312
1313These are the \ASCII{} codec APIs. Only 7-bit \ASCII{} data is
1314accepted. All other codes generate errors.
1315
1316\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeASCII}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001317 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001318 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001319 Create a Unicode object by decoding \var{size} bytes of the
1320 \ASCII{} encoded string \var{s}. Return \NULL{} if an exception
Fred Drake3adf79e2001-10-12 19:01:43 +00001321 was raised by the codec.
1322\end{cfuncdesc}
1323
1324\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeASCII}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001325 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001326 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001327 Encode the \ctype{Py_UNICODE} buffer of the given size using
1328 \ASCII{} and return a Python string object. Return \NULL{} if an
Fred Drake3adf79e2001-10-12 19:01:43 +00001329 exception was raised by the codec.
1330\end{cfuncdesc}
1331
1332\begin{cfuncdesc}{PyObject*}{PyUnicode_AsASCIIString}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001333 Encode a Unicode objects using \ASCII{} and return the result as
1334 Python string object. Error handling is ``strict''. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00001335 \NULL{} if an exception was raised by the codec.
1336\end{cfuncdesc}
1337
Tim Petersf582b822001-12-11 18:51:08 +00001338% --- Character Map Codecs -----------------------------------------------
Fred Drake3adf79e2001-10-12 19:01:43 +00001339
1340These are the mapping codec APIs:
1341
1342This codec is special in that it can be used to implement many
1343different codecs (and this is in fact what was done to obtain most of
1344the standard codecs included in the \module{encodings} package). The
1345codec uses mapping to encode and decode characters.
1346
1347Decoding mappings must map single string characters to single Unicode
1348characters, integers (which are then interpreted as Unicode ordinals)
Tim Petersf582b822001-12-11 18:51:08 +00001349or None (meaning "undefined mapping" and causing an error).
Fred Drake3adf79e2001-10-12 19:01:43 +00001350
1351Encoding mappings must map single Unicode characters to single string
1352characters, integers (which are then interpreted as Latin-1 ordinals)
1353or None (meaning "undefined mapping" and causing an error).
1354
1355The mapping objects provided must only support the __getitem__ mapping
1356interface.
1357
1358If a character lookup fails with a LookupError, the character is
1359copied as-is meaning that its ordinal value will be interpreted as
1360Unicode or Latin-1 ordinal resp. Because of this, mappings only need
1361to contain those mappings which map characters to different code
1362points.
1363
1364\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeCharmap}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001365 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001366 PyObject *mapping,
1367 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001368 Create a Unicode object by decoding \var{size} bytes of the encoded
1369 string \var{s} using the given \var{mapping} object. Return
Walter Dörwaldd1c1e102005-10-06 20:29:57 +00001370 \NULL{} if an exception was raised by the codec. If \var{mapping} is \NULL{}
1371 latin-1 decoding will be done. Else it can be a dictionary mapping byte or a
1372 unicode string, which is treated as a lookup table. Byte values greater
1373 that the length of the string and U+FFFE "characters" are treated as
1374 "undefined mapping".
1375 \versionchanged[Allowed unicode string as mapping argument]{2.4}
Fred Drake3adf79e2001-10-12 19:01:43 +00001376\end{cfuncdesc}
1377
1378\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeCharmap}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001379 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001380 PyObject *mapping,
1381 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001382 Encode the \ctype{Py_UNICODE} buffer of the given size using the
1383 given \var{mapping} object and return a Python string object.
1384 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +00001385\end{cfuncdesc}
1386
1387\begin{cfuncdesc}{PyObject*}{PyUnicode_AsCharmapString}{PyObject *unicode,
1388 PyObject *mapping}
Georg Brandl99363b62005-09-03 07:27:26 +00001389 Encode a Unicode objects using the given \var{mapping} object and
1390 return the result as Python string object. Error handling is
1391 ``strict''. Return \NULL{} if an exception was raised by the
Fred Drake3adf79e2001-10-12 19:01:43 +00001392 codec.
1393\end{cfuncdesc}
1394
1395The following codec API is special in that maps Unicode to Unicode.
1396
1397\begin{cfuncdesc}{PyObject*}{PyUnicode_TranslateCharmap}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001398 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001399 PyObject *table,
1400 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001401 Translate a \ctype{Py_UNICODE} buffer of the given length by
1402 applying a character mapping \var{table} to it and return the
1403 resulting Unicode object. Return \NULL{} when an exception was
Fred Drake3adf79e2001-10-12 19:01:43 +00001404 raised by the codec.
1405
1406 The \var{mapping} table must map Unicode ordinal integers to Unicode
1407 ordinal integers or None (causing deletion of the character).
1408
Thomas Wouters477c8d52006-05-27 19:21:47 +00001409 Mapping tables need only provide the \method{__getitem__()}
Fred Drake3adf79e2001-10-12 19:01:43 +00001410 interface; dictionaries and sequences work well. Unmapped character
1411 ordinals (ones which cause a \exception{LookupError}) are left
1412 untouched and are copied as-is.
1413\end{cfuncdesc}
1414
1415% --- MBCS codecs for Windows --------------------------------------------
1416
1417These are the MBCS codec APIs. They are currently only available on
1418Windows and use the Win32 MBCS converters to implement the
1419conversions. Note that MBCS (or DBCS) is a class of encodings, not
1420just one. The target encoding is defined by the user settings on the
1421machine running the codec.
1422
1423\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeMBCS}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001424 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001425 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001426 Create a Unicode object by decoding \var{size} bytes of the MBCS
1427 encoded string \var{s}. Return \NULL{} if an exception was
Fred Drake3adf79e2001-10-12 19:01:43 +00001428 raised by the codec.
1429\end{cfuncdesc}
1430
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001431\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeMBCSStateful}{const char *s,
1432 int size,
1433 const char *errors,
1434 int *consumed}
1435 If \var{consumed} is \NULL{}, behave like
1436 \cfunction{PyUnicode_DecodeMBCS()}. If \var{consumed} is not \NULL{},
1437 \cfunction{PyUnicode_DecodeMBCSStateful()} will not decode trailing lead
1438 byte and the number of bytes that have been decoded will be stored in
1439 \var{consumed}.
1440 \versionadded{2.5}
1441\end{cfuncdesc}
1442
Fred Drake3adf79e2001-10-12 19:01:43 +00001443\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeMBCS}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001444 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001445 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001446 Encode the \ctype{Py_UNICODE} buffer of the given size using MBCS
1447 and return a Python string object. Return \NULL{} if an exception
Fred Drake3adf79e2001-10-12 19:01:43 +00001448 was raised by the codec.
1449\end{cfuncdesc}
1450
1451\begin{cfuncdesc}{PyObject*}{PyUnicode_AsMBCSString}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001452 Encode a Unicode objects using MBCS and return the result as
1453 Python string object. Error handling is ``strict''. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00001454 \NULL{} if an exception was raised by the codec.
1455\end{cfuncdesc}
1456
1457% --- Methods & Slots ----------------------------------------------------
1458
1459\subsubsection{Methods and Slot Functions \label{unicodeMethodsAndSlots}}
1460
1461The following APIs are capable of handling Unicode objects and strings
1462on input (we refer to them as strings in the descriptions) and return
Martin v. Löwis95cf84a2003-10-19 07:32:24 +00001463Unicode objects or integers as appropriate.
Fred Drake3adf79e2001-10-12 19:01:43 +00001464
1465They all return \NULL{} or \code{-1} if an exception occurs.
1466
1467\begin{cfuncdesc}{PyObject*}{PyUnicode_Concat}{PyObject *left,
1468 PyObject *right}
1469 Concat two strings giving a new Unicode string.
1470\end{cfuncdesc}
1471
1472\begin{cfuncdesc}{PyObject*}{PyUnicode_Split}{PyObject *s,
1473 PyObject *sep,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001474 Py_ssize_t maxsplit}
Tim Peters9ddf40b2004-06-20 22:41:32 +00001475 Split a string giving a list of Unicode strings. If sep is \NULL{},
Fred Drake3adf79e2001-10-12 19:01:43 +00001476 splitting will be done at all whitespace substrings. Otherwise,
1477 splits occur at the given separator. At most \var{maxsplit} splits
1478 will be done. If negative, no limit is set. Separators are not
1479 included in the resulting list.
1480\end{cfuncdesc}
1481
1482\begin{cfuncdesc}{PyObject*}{PyUnicode_Splitlines}{PyObject *s,
Martin v. Löwis24b88812003-03-30 16:40:42 +00001483 int keepend}
Fred Drake3adf79e2001-10-12 19:01:43 +00001484 Split a Unicode string at line breaks, returning a list of Unicode
Martin v. Löwis24b88812003-03-30 16:40:42 +00001485 strings. CRLF is considered to be one line break. If \var{keepend}
1486 is 0, the Line break characters are not included in the resulting
1487 strings.
Fred Drake3adf79e2001-10-12 19:01:43 +00001488\end{cfuncdesc}
1489
1490\begin{cfuncdesc}{PyObject*}{PyUnicode_Translate}{PyObject *str,
1491 PyObject *table,
1492 const char *errors}
1493 Translate a string by applying a character mapping table to it and
1494 return the resulting Unicode object.
1495
1496 The mapping table must map Unicode ordinal integers to Unicode
1497 ordinal integers or None (causing deletion of the character).
1498
1499 Mapping tables need only provide the \method{__getitem__()}
1500 interface; dictionaries and sequences work well. Unmapped character
1501 ordinals (ones which cause a \exception{LookupError}) are left
1502 untouched and are copied as-is.
1503
1504 \var{errors} has the usual meaning for codecs. It may be \NULL{}
1505 which indicates to use the default error handling.
1506\end{cfuncdesc}
1507
1508\begin{cfuncdesc}{PyObject*}{PyUnicode_Join}{PyObject *separator,
1509 PyObject *seq}
1510 Join a sequence of strings using the given separator and return the
1511 resulting Unicode string.
1512\end{cfuncdesc}
1513
Raymond Hettinger8ef9b3e2004-12-10 17:12:32 +00001514\begin{cfuncdesc}{int}{PyUnicode_Tailmatch}{PyObject *str,
Fred Drake3adf79e2001-10-12 19:01:43 +00001515 PyObject *substr,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001516 Py_ssize_t start,
1517 Py_ssize_t end,
Fred Drake3adf79e2001-10-12 19:01:43 +00001518 int direction}
1519 Return 1 if \var{substr} matches \var{str}[\var{start}:\var{end}] at
1520 the given tail end (\var{direction} == -1 means to do a prefix
1521 match, \var{direction} == 1 a suffix match), 0 otherwise.
Thomas Wouters477c8d52006-05-27 19:21:47 +00001522 Return \code{-1} if an error occurred.
Fred Drake3adf79e2001-10-12 19:01:43 +00001523\end{cfuncdesc}
1524
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001525\begin{cfuncdesc}{Py_ssize_t}{PyUnicode_Find}{PyObject *str,
Fred Drake1d1e1db2002-06-20 22:07:04 +00001526 PyObject *substr,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001527 Py_ssize_t start,
1528 Py_ssize_t end,
Fred Drake1d1e1db2002-06-20 22:07:04 +00001529 int direction}
Fred Drake3adf79e2001-10-12 19:01:43 +00001530 Return the first position of \var{substr} in
1531 \var{str}[\var{start}:\var{end}] using the given \var{direction}
1532 (\var{direction} == 1 means to do a forward search,
Fred Drake1d1e1db2002-06-20 22:07:04 +00001533 \var{direction} == -1 a backward search). The return value is the
1534 index of the first match; a value of \code{-1} indicates that no
1535 match was found, and \code{-2} indicates that an error occurred and
1536 an exception has been set.
Fred Drake3adf79e2001-10-12 19:01:43 +00001537\end{cfuncdesc}
1538
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001539\begin{cfuncdesc}{Py_ssize_t}{PyUnicode_Count}{PyObject *str,
Fred Drake1d1e1db2002-06-20 22:07:04 +00001540 PyObject *substr,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001541 Py_ssize_t start,
1542 Py_ssize_t end}
Fred Drake1d1e1db2002-06-20 22:07:04 +00001543 Return the number of non-overlapping occurrences of \var{substr} in
Georg Brandl99363b62005-09-03 07:27:26 +00001544 \code{\var{str}[\var{start}:\var{end}]}. Return \code{-1} if an
Fred Drake1d1e1db2002-06-20 22:07:04 +00001545 error occurred.
Fred Drake3adf79e2001-10-12 19:01:43 +00001546\end{cfuncdesc}
1547
1548\begin{cfuncdesc}{PyObject*}{PyUnicode_Replace}{PyObject *str,
1549 PyObject *substr,
1550 PyObject *replstr,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001551 Py_ssize_t maxcount}
Fred Drake3adf79e2001-10-12 19:01:43 +00001552 Replace at most \var{maxcount} occurrences of \var{substr} in
1553 \var{str} with \var{replstr} and return the resulting Unicode object.
1554 \var{maxcount} == -1 means replace all occurrences.
1555\end{cfuncdesc}
1556
1557\begin{cfuncdesc}{int}{PyUnicode_Compare}{PyObject *left, PyObject *right}
1558 Compare two strings and return -1, 0, 1 for less than, equal, and
1559 greater than, respectively.
1560\end{cfuncdesc}
1561
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001562\begin{cfuncdesc}{int}{PyUnicode_RichCompare}{PyObject *left,
1563 PyObject *right,
1564 int op}
1565
1566 Rich compare two unicode strings and return one of the following:
1567 \begin{itemize}
1568 \item \code{NULL} in case an exception was raised
1569 \item \constant{Py_True} or \constant{Py_False} for successful comparisons
1570 \item \constant{Py_NotImplemented} in case the type combination is unknown
1571 \end{itemize}
1572
1573 Note that \constant{Py_EQ} and \constant{Py_NE} comparisons can cause a
1574 \exception{UnicodeWarning} in case the conversion of the arguments to
1575 Unicode fails with a \exception{UnicodeDecodeError}.
1576
1577 Possible values for \var{op} are
1578 \constant{Py_GT}, \constant{Py_GE}, \constant{Py_EQ},
1579 \constant{Py_NE}, \constant{Py_LT}, and \constant{Py_LE}.
1580\end{cfuncdesc}
1581
Fred Drake3adf79e2001-10-12 19:01:43 +00001582\begin{cfuncdesc}{PyObject*}{PyUnicode_Format}{PyObject *format,
1583 PyObject *args}
Georg Brandl99363b62005-09-03 07:27:26 +00001584 Return a new string object from \var{format} and \var{args}; this
Fred Drake3adf79e2001-10-12 19:01:43 +00001585 is analogous to \code{\var{format} \%\ \var{args}}. The
1586 \var{args} argument must be a tuple.
1587\end{cfuncdesc}
1588
1589\begin{cfuncdesc}{int}{PyUnicode_Contains}{PyObject *container,
1590 PyObject *element}
Georg Brandl99363b62005-09-03 07:27:26 +00001591 Check whether \var{element} is contained in \var{container} and
1592 return true or false accordingly.
Fred Drake3adf79e2001-10-12 19:01:43 +00001593
1594 \var{element} has to coerce to a one element Unicode
1595 string. \code{-1} is returned if there was an error.
1596\end{cfuncdesc}
1597
1598
1599\subsection{Buffer Objects \label{bufferObjects}}
1600\sectionauthor{Greg Stein}{gstein@lyra.org}
1601
1602\obindex{buffer}
1603Python objects implemented in C can export a group of functions called
1604the ``buffer\index{buffer interface} interface.'' These functions can
1605be used by an object to expose its data in a raw, byte-oriented
1606format. Clients of the object can use the buffer interface to access
1607the object data directly, without needing to copy it first.
1608
Tim Petersf582b822001-12-11 18:51:08 +00001609Two examples of objects that support
1610the buffer interface are strings and arrays. The string object exposes
Fred Drake3adf79e2001-10-12 19:01:43 +00001611the character contents in the buffer interface's byte-oriented
1612form. An array can also expose its contents, but it should be noted
1613that array elements may be multi-byte values.
1614
1615An example user of the buffer interface is the file object's
1616\method{write()} method. Any object that can export a series of bytes
1617through the buffer interface can be written to a file. There are a
Tim Petersf582b822001-12-11 18:51:08 +00001618number of format codes to \cfunction{PyArg_ParseTuple()} that operate
Fred Drake3adf79e2001-10-12 19:01:43 +00001619against an object's buffer interface, returning data from the target
1620object.
1621
1622More information on the buffer interface is provided in the section
Fred Drake54e62942001-12-11 19:40:16 +00001623``Buffer Object Structures'' (section~\ref{buffer-structs}), under
Fred Drake3adf79e2001-10-12 19:01:43 +00001624the description for \ctype{PyBufferProcs}\ttindex{PyBufferProcs}.
1625
1626A ``buffer object'' is defined in the \file{bufferobject.h} header
1627(included by \file{Python.h}). These objects look very similar to
1628string objects at the Python programming level: they support slicing,
1629indexing, concatenation, and some other standard string
1630operations. However, their data can come from one of two sources: from
1631a block of memory, or from another object which exports the buffer
1632interface.
1633
1634Buffer objects are useful as a way to expose the data from another
1635object's buffer interface to the Python programmer. They can also be
1636used as a zero-copy slicing mechanism. Using their ability to
1637reference a block of memory, it is possible to expose any data to the
1638Python programmer quite easily. The memory could be a large, constant
1639array in a C extension, it could be a raw block of memory for
1640manipulation before passing to an operating system library, or it
1641could be used to pass around structured data in its native, in-memory
1642format.
1643
1644\begin{ctypedesc}{PyBufferObject}
1645 This subtype of \ctype{PyObject} represents a buffer object.
1646\end{ctypedesc}
1647
1648\begin{cvardesc}{PyTypeObject}{PyBuffer_Type}
1649 The instance of \ctype{PyTypeObject} which represents the Python
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001650 buffer type; it is the same object as \code{buffer} and
1651 \code{types.BufferType} in the Python layer.
1652 \withsubitem{(in module types)}{\ttindex{BufferType}}.
Fred Drake3adf79e2001-10-12 19:01:43 +00001653\end{cvardesc}
1654
1655\begin{cvardesc}{int}{Py_END_OF_BUFFER}
1656 This constant may be passed as the \var{size} parameter to
1657 \cfunction{PyBuffer_FromObject()} or
1658 \cfunction{PyBuffer_FromReadWriteObject()}. It indicates that the
1659 new \ctype{PyBufferObject} should refer to \var{base} object from
1660 the specified \var{offset} to the end of its exported buffer. Using
1661 this enables the caller to avoid querying the \var{base} object for
1662 its length.
1663\end{cvardesc}
1664
1665\begin{cfuncdesc}{int}{PyBuffer_Check}{PyObject *p}
1666 Return true if the argument has type \cdata{PyBuffer_Type}.
1667\end{cfuncdesc}
1668
1669\begin{cfuncdesc}{PyObject*}{PyBuffer_FromObject}{PyObject *base,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001670 Py_ssize_t offset, Py_ssize_t size}
Fred Drake3adf79e2001-10-12 19:01:43 +00001671 Return a new read-only buffer object. This raises
1672 \exception{TypeError} if \var{base} doesn't support the read-only
1673 buffer protocol or doesn't provide exactly one buffer segment, or it
1674 raises \exception{ValueError} if \var{offset} is less than zero. The
1675 buffer will hold a reference to the \var{base} object, and the
1676 buffer's contents will refer to the \var{base} object's buffer
1677 interface, starting as position \var{offset} and extending for
1678 \var{size} bytes. If \var{size} is \constant{Py_END_OF_BUFFER}, then
1679 the new buffer's contents extend to the length of the \var{base}
1680 object's exported buffer data.
1681\end{cfuncdesc}
1682
1683\begin{cfuncdesc}{PyObject*}{PyBuffer_FromReadWriteObject}{PyObject *base,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001684 Py_ssize_t offset,
1685 Py_ssize_t size}
Fred Drake3adf79e2001-10-12 19:01:43 +00001686 Return a new writable buffer object. Parameters and exceptions are
1687 similar to those for \cfunction{PyBuffer_FromObject()}. If the
1688 \var{base} object does not export the writeable buffer protocol,
1689 then \exception{TypeError} is raised.
1690\end{cfuncdesc}
1691
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001692\begin{cfuncdesc}{PyObject*}{PyBuffer_FromMemory}{void *ptr, Py_ssize_t size}
Fred Drake3adf79e2001-10-12 19:01:43 +00001693 Return a new read-only buffer object that reads from a specified
1694 location in memory, with a specified size. The caller is
1695 responsible for ensuring that the memory buffer, passed in as
1696 \var{ptr}, is not deallocated while the returned buffer object
1697 exists. Raises \exception{ValueError} if \var{size} is less than
1698 zero. Note that \constant{Py_END_OF_BUFFER} may \emph{not} be
1699 passed for the \var{size} parameter; \exception{ValueError} will be
1700 raised in that case.
1701\end{cfuncdesc}
1702
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001703\begin{cfuncdesc}{PyObject*}{PyBuffer_FromReadWriteMemory}{void *ptr, Py_ssize_t size}
Fred Drake3adf79e2001-10-12 19:01:43 +00001704 Similar to \cfunction{PyBuffer_FromMemory()}, but the returned
1705 buffer is writable.
1706\end{cfuncdesc}
1707
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001708\begin{cfuncdesc}{PyObject*}{PyBuffer_New}{Py_ssize_t size}
Georg Brandl99363b62005-09-03 07:27:26 +00001709 Return a new writable buffer object that maintains its own memory
Fred Drake3adf79e2001-10-12 19:01:43 +00001710 buffer of \var{size} bytes. \exception{ValueError} is returned if
Neil Schemenauerd68d3ee2004-06-08 02:58:50 +00001711 \var{size} is not zero or positive. Note that the memory buffer (as
1712 returned by \cfunction{PyObject_AsWriteBuffer()}) is not specifically
1713 aligned.
Fred Drake3adf79e2001-10-12 19:01:43 +00001714\end{cfuncdesc}
1715
1716
1717\subsection{Tuple Objects \label{tupleObjects}}
1718
1719\obindex{tuple}
1720\begin{ctypedesc}{PyTupleObject}
1721 This subtype of \ctype{PyObject} represents a Python tuple object.
1722\end{ctypedesc}
1723
1724\begin{cvardesc}{PyTypeObject}{PyTuple_Type}
1725 This instance of \ctype{PyTypeObject} represents the Python tuple
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001726 type; it is the same object as \code{tuple} and \code{types.TupleType}
1727 in the Python layer.\withsubitem{(in module types)}{\ttindex{TupleType}}.
Fred Drake3adf79e2001-10-12 19:01:43 +00001728\end{cvardesc}
1729
1730\begin{cfuncdesc}{int}{PyTuple_Check}{PyObject *p}
1731 Return true if \var{p} is a tuple object or an instance of a subtype
1732 of the tuple type.
1733 \versionchanged[Allowed subtypes to be accepted]{2.2}
1734\end{cfuncdesc}
1735
1736\begin{cfuncdesc}{int}{PyTuple_CheckExact}{PyObject *p}
1737 Return true if \var{p} is a tuple object, but not an instance of a
1738 subtype of the tuple type.
1739 \versionadded{2.2}
1740\end{cfuncdesc}
1741
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001742\begin{cfuncdesc}{PyObject*}{PyTuple_New}{Py_ssize_t len}
Fred Drake3adf79e2001-10-12 19:01:43 +00001743 Return a new tuple object of size \var{len}, or \NULL{} on failure.
1744\end{cfuncdesc}
1745
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001746\begin{cfuncdesc}{PyObject*}{PyTuple_Pack}{Py_ssize_t n, \moreargs}
Raymond Hettingercb2da432003-10-12 18:24:34 +00001747 Return a new tuple object of size \var{n}, or \NULL{} on failure.
1748 The tuple values are initialized to the subsequent \var{n} C arguments
1749 pointing to Python objects. \samp{PyTuple_Pack(2, \var{a}, \var{b})}
1750 is equivalent to \samp{Py_BuildValue("(OO)", \var{a}, \var{b})}.
Tim Peters9ddf40b2004-06-20 22:41:32 +00001751 \versionadded{2.4}
Raymond Hettingercb2da432003-10-12 18:24:34 +00001752\end{cfuncdesc}
1753
Fred Drake3adf79e2001-10-12 19:01:43 +00001754\begin{cfuncdesc}{int}{PyTuple_Size}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00001755 Take a pointer to a tuple object, and return the size of that
Fred Drake3adf79e2001-10-12 19:01:43 +00001756 tuple.
1757\end{cfuncdesc}
1758
1759\begin{cfuncdesc}{int}{PyTuple_GET_SIZE}{PyObject *p}
1760 Return the size of the tuple \var{p}, which must be non-\NULL{} and
1761 point to a tuple; no error checking is performed.
1762\end{cfuncdesc}
1763
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001764\begin{cfuncdesc}{PyObject*}{PyTuple_GetItem}{PyObject *p, Py_ssize_t pos}
Georg Brandl99363b62005-09-03 07:27:26 +00001765 Return the object at position \var{pos} in the tuple pointed to by
1766 \var{p}. If \var{pos} is out of bounds, return \NULL{} and sets an
Fred Drake3adf79e2001-10-12 19:01:43 +00001767 \exception{IndexError} exception.
1768\end{cfuncdesc}
1769
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001770\begin{cfuncdesc}{PyObject*}{PyTuple_GET_ITEM}{PyObject *p, Py_ssize_t pos}
Fred Drake3adf79e2001-10-12 19:01:43 +00001771 Like \cfunction{PyTuple_GetItem()}, but does no checking of its
1772 arguments.
1773\end{cfuncdesc}
1774
1775\begin{cfuncdesc}{PyObject*}{PyTuple_GetSlice}{PyObject *p,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001776 Py_ssize_t low, Py_ssize_t high}
Georg Brandl99363b62005-09-03 07:27:26 +00001777 Take a slice of the tuple pointed to by \var{p} from \var{low} to
1778 \var{high} and return it as a new tuple.
Fred Drake3adf79e2001-10-12 19:01:43 +00001779\end{cfuncdesc}
1780
1781\begin{cfuncdesc}{int}{PyTuple_SetItem}{PyObject *p,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001782 Py_ssize_t pos, PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +00001783 Insert a reference to object \var{o} at position \var{pos} of the
1784 tuple pointed to by \var{p}. Return \code{0} on success.
Fred Drake3adf79e2001-10-12 19:01:43 +00001785 \note{This function ``steals'' a reference to \var{o}.}
1786\end{cfuncdesc}
1787
1788\begin{cfuncdesc}{void}{PyTuple_SET_ITEM}{PyObject *p,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001789 Py_ssize_t pos, PyObject *o}
Fred Drake3adf79e2001-10-12 19:01:43 +00001790 Like \cfunction{PyTuple_SetItem()}, but does no error checking, and
1791 should \emph{only} be used to fill in brand new tuples. \note{This
1792 function ``steals'' a reference to \var{o}.}
1793\end{cfuncdesc}
1794
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001795\begin{cfuncdesc}{int}{_PyTuple_Resize}{PyObject **p, Py_ssize_t newsize}
Fred Drake3adf79e2001-10-12 19:01:43 +00001796 Can be used to resize a tuple. \var{newsize} will be the new length
1797 of the tuple. Because tuples are \emph{supposed} to be immutable,
1798 this should only be used if there is only one reference to the
1799 object. Do \emph{not} use this if the tuple may already be known to
1800 some other part of the code. The tuple will always grow or shrink
1801 at the end. Think of this as destroying the old tuple and creating
1802 a new one, only more efficiently. Returns \code{0} on success.
1803 Client code should never assume that the resulting value of
1804 \code{*\var{p}} will be the same as before calling this function.
1805 If the object referenced by \code{*\var{p}} is replaced, the
1806 original \code{*\var{p}} is destroyed. On failure, returns
Tim Peters9ddf40b2004-06-20 22:41:32 +00001807 \code{-1} and sets \code{*\var{p}} to \NULL{}, and raises
Fred Drake3adf79e2001-10-12 19:01:43 +00001808 \exception{MemoryError} or
1809 \exception{SystemError}.
Tim Petersf582b822001-12-11 18:51:08 +00001810 \versionchanged[Removed unused third parameter, \var{last_is_sticky}]{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +00001811\end{cfuncdesc}
1812
1813
1814\subsection{List Objects \label{listObjects}}
1815
1816\obindex{list}
1817\begin{ctypedesc}{PyListObject}
1818 This subtype of \ctype{PyObject} represents a Python list object.
1819\end{ctypedesc}
1820
1821\begin{cvardesc}{PyTypeObject}{PyList_Type}
1822 This instance of \ctype{PyTypeObject} represents the Python list
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001823 type. This is the same object as \code{list} and \code{types.ListType}
1824 in the Python layer.\withsubitem{(in module types)}{\ttindex{ListType}}
Fred Drake3adf79e2001-10-12 19:01:43 +00001825\end{cvardesc}
1826
1827\begin{cfuncdesc}{int}{PyList_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00001828 Return true if \var{p} is a list object or an instance of a
Andrew MacIntyre13cd8892003-12-25 23:57:52 +00001829 subtype of the list type.
1830 \versionchanged[Allowed subtypes to be accepted]{2.2}
1831\end{cfuncdesc}
1832
1833\begin{cfuncdesc}{int}{PyList_CheckExact}{PyObject *p}
1834 Return true if \var{p} is a list object, but not an instance of a
1835 subtype of the list type.
1836 \versionadded{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +00001837\end{cfuncdesc}
1838
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001839\begin{cfuncdesc}{PyObject*}{PyList_New}{Py_ssize_t len}
Georg Brandl99363b62005-09-03 07:27:26 +00001840 Return a new list of length \var{len} on success, or \NULL{} on
Fred Drake3adf79e2001-10-12 19:01:43 +00001841 failure.
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001842 \note{If \var{length} is greater than zero, the returned list object's
1843 items are set to \code{NULL}. Thus you cannot use abstract
1844 API functions such as \cfunction{PySequence_SetItem()}
1845 or expose the object to Python code before setting all items to a
1846 real object with \cfunction{PyList_SetItem()}.}
Fred Drake3adf79e2001-10-12 19:01:43 +00001847\end{cfuncdesc}
1848
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001849\begin{cfuncdesc}{Py_ssize_t}{PyList_Size}{PyObject *list}
Georg Brandl99363b62005-09-03 07:27:26 +00001850 Return the length of the list object in \var{list}; this is
Fred Drake3adf79e2001-10-12 19:01:43 +00001851 equivalent to \samp{len(\var{list})} on a list object.
1852 \bifuncindex{len}
1853\end{cfuncdesc}
1854
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001855\begin{cfuncdesc}{Py_ssize_t}{PyList_GET_SIZE}{PyObject *list}
Fred Drake3adf79e2001-10-12 19:01:43 +00001856 Macro form of \cfunction{PyList_Size()} without error checking.
1857\end{cfuncdesc}
1858
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001859\begin{cfuncdesc}{PyObject*}{PyList_GetItem}{PyObject *list, Py_ssize_t index}
Georg Brandl99363b62005-09-03 07:27:26 +00001860 Return the object at position \var{pos} in the list pointed to by
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001861 \var{p}. The position must be positive, indexing from the end of the
1862 list is not supported. If \var{pos} is out of bounds, return \NULL{}
1863 and set an \exception{IndexError} exception.
Fred Drake3adf79e2001-10-12 19:01:43 +00001864\end{cfuncdesc}
1865
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001866\begin{cfuncdesc}{PyObject*}{PyList_GET_ITEM}{PyObject *list, Py_ssize_t i}
Fred Drake3adf79e2001-10-12 19:01:43 +00001867 Macro form of \cfunction{PyList_GetItem()} without error checking.
1868\end{cfuncdesc}
1869
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001870\begin{cfuncdesc}{int}{PyList_SetItem}{PyObject *list, Py_ssize_t index,
Fred Drake3adf79e2001-10-12 19:01:43 +00001871 PyObject *item}
Georg Brandl99363b62005-09-03 07:27:26 +00001872 Set the item at index \var{index} in list to \var{item}. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00001873 \code{0} on success or \code{-1} on failure. \note{This function
1874 ``steals'' a reference to \var{item} and discards a reference to an
1875 item already in the list at the affected position.}
1876\end{cfuncdesc}
1877
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001878\begin{cfuncdesc}{void}{PyList_SET_ITEM}{PyObject *list, Py_ssize_t i,
Fred Drake3adf79e2001-10-12 19:01:43 +00001879 PyObject *o}
1880 Macro form of \cfunction{PyList_SetItem()} without error checking.
1881 This is normally only used to fill in new lists where there is no
1882 previous content.
1883 \note{This function ``steals'' a reference to \var{item}, and,
1884 unlike \cfunction{PyList_SetItem()}, does \emph{not} discard a
1885 reference to any item that it being replaced; any reference in
1886 \var{list} at position \var{i} will be leaked.}
1887\end{cfuncdesc}
1888
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001889\begin{cfuncdesc}{int}{PyList_Insert}{PyObject *list, Py_ssize_t index,
Fred Drake3adf79e2001-10-12 19:01:43 +00001890 PyObject *item}
Georg Brandl99363b62005-09-03 07:27:26 +00001891 Insert the item \var{item} into list \var{list} in front of index
1892 \var{index}. Return \code{0} if successful; return \code{-1} and
1893 set an exception if unsuccessful. Analogous to
Fred Drake3adf79e2001-10-12 19:01:43 +00001894 \code{\var{list}.insert(\var{index}, \var{item})}.
1895\end{cfuncdesc}
1896
1897\begin{cfuncdesc}{int}{PyList_Append}{PyObject *list, PyObject *item}
Georg Brandl99363b62005-09-03 07:27:26 +00001898 Append the object \var{item} at the end of list \var{list}.
1899 Return \code{0} if successful; return \code{-1} and set an
Fred Drake3adf79e2001-10-12 19:01:43 +00001900 exception if unsuccessful. Analogous to
1901 \code{\var{list}.append(\var{item})}.
1902\end{cfuncdesc}
1903
1904\begin{cfuncdesc}{PyObject*}{PyList_GetSlice}{PyObject *list,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001905 Py_ssize_t low, Py_ssize_t high}
Georg Brandl99363b62005-09-03 07:27:26 +00001906 Return a list of the objects in \var{list} containing the objects
1907 \emph{between} \var{low} and \var{high}. Return \NULL{} and set
Fred Drake3adf79e2001-10-12 19:01:43 +00001908 an exception if unsuccessful.
1909 Analogous to \code{\var{list}[\var{low}:\var{high}]}.
1910\end{cfuncdesc}
1911
1912\begin{cfuncdesc}{int}{PyList_SetSlice}{PyObject *list,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001913 Py_ssize_t low, Py_ssize_t high,
Fred Drake3adf79e2001-10-12 19:01:43 +00001914 PyObject *itemlist}
Georg Brandl99363b62005-09-03 07:27:26 +00001915 Set the slice of \var{list} between \var{low} and \var{high} to the
Fred Drake3adf79e2001-10-12 19:01:43 +00001916 contents of \var{itemlist}. Analogous to
Raymond Hettinger9c7ed4c2003-10-26 17:20:07 +00001917 \code{\var{list}[\var{low}:\var{high}] = \var{itemlist}}.
1918 The \var{itemlist} may be \NULL{}, indicating the assignment
1919 of an empty list (slice deletion).
Georg Brandl99363b62005-09-03 07:27:26 +00001920 Return \code{0} on success, \code{-1} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00001921\end{cfuncdesc}
1922
1923\begin{cfuncdesc}{int}{PyList_Sort}{PyObject *list}
Georg Brandl99363b62005-09-03 07:27:26 +00001924 Sort the items of \var{list} in place. Return \code{0} on
Fred Drake3adf79e2001-10-12 19:01:43 +00001925 success, \code{-1} on failure. This is equivalent to
1926 \samp{\var{list}.sort()}.
1927\end{cfuncdesc}
1928
1929\begin{cfuncdesc}{int}{PyList_Reverse}{PyObject *list}
Georg Brandl99363b62005-09-03 07:27:26 +00001930 Reverse the items of \var{list} in place. Return \code{0} on
Fred Drake3adf79e2001-10-12 19:01:43 +00001931 success, \code{-1} on failure. This is the equivalent of
1932 \samp{\var{list}.reverse()}.
1933\end{cfuncdesc}
1934
1935\begin{cfuncdesc}{PyObject*}{PyList_AsTuple}{PyObject *list}
Georg Brandl99363b62005-09-03 07:27:26 +00001936 Return a new tuple object containing the contents of \var{list};
Fred Drake3adf79e2001-10-12 19:01:43 +00001937 equivalent to \samp{tuple(\var{list})}.\bifuncindex{tuple}
1938\end{cfuncdesc}
1939
1940
1941\section{Mapping Objects \label{mapObjects}}
1942
1943\obindex{mapping}
1944
1945
1946\subsection{Dictionary Objects \label{dictObjects}}
1947
1948\obindex{dictionary}
1949\begin{ctypedesc}{PyDictObject}
1950 This subtype of \ctype{PyObject} represents a Python dictionary
1951 object.
1952\end{ctypedesc}
1953
1954\begin{cvardesc}{PyTypeObject}{PyDict_Type}
1955 This instance of \ctype{PyTypeObject} represents the Python
1956 dictionary type. This is exposed to Python programs as
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001957 \code{dict} and \code{types.DictType}.
Fred Drake3adf79e2001-10-12 19:01:43 +00001958 \withsubitem{(in module types)}{\ttindex{DictType}\ttindex{DictionaryType}}
1959\end{cvardesc}
1960
1961\begin{cfuncdesc}{int}{PyDict_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00001962 Return true if \var{p} is a dict object or an instance of a
Andrew MacIntyre13cd8892003-12-25 23:57:52 +00001963 subtype of the dict type.
1964 \versionchanged[Allowed subtypes to be accepted]{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +00001965\end{cfuncdesc}
1966
Andrew MacIntyref72af652003-12-26 00:07:51 +00001967\begin{cfuncdesc}{int}{PyDict_CheckExact}{PyObject *p}
1968 Return true if \var{p} is a dict object, but not an instance of a
1969 subtype of the dict type.
1970 \versionadded{2.4}
1971\end{cfuncdesc}
1972
Fred Drake3adf79e2001-10-12 19:01:43 +00001973\begin{cfuncdesc}{PyObject*}{PyDict_New}{}
Georg Brandl99363b62005-09-03 07:27:26 +00001974 Return a new empty dictionary, or \NULL{} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00001975\end{cfuncdesc}
1976
1977\begin{cfuncdesc}{PyObject*}{PyDictProxy_New}{PyObject *dict}
1978 Return a proxy object for a mapping which enforces read-only
1979 behavior. This is normally used to create a proxy to prevent
1980 modification of the dictionary for non-dynamic class types.
1981 \versionadded{2.2}
1982\end{cfuncdesc}
1983
1984\begin{cfuncdesc}{void}{PyDict_Clear}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00001985 Empty an existing dictionary of all key-value pairs.
Fred Drake3adf79e2001-10-12 19:01:43 +00001986\end{cfuncdesc}
1987
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00001988\begin{cfuncdesc}{int}{PyDict_Contains}{PyObject *p, PyObject *key}
1989 Determine if dictionary \var{p} contains \var{key}. If an item
1990 in \var{p} is matches \var{key}, return \code{1}, otherwise return
1991 \code{0}. On error, return \code{-1}. This is equivalent to the
1992 Python expression \samp{\var{key} in \var{p}}.
Tim Peters9ddf40b2004-06-20 22:41:32 +00001993 \versionadded{2.4}
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00001994\end{cfuncdesc}
1995
Fred Drake3adf79e2001-10-12 19:01:43 +00001996\begin{cfuncdesc}{PyObject*}{PyDict_Copy}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00001997 Return a new dictionary that contains the same key-value pairs as
Fred Drake3adf79e2001-10-12 19:01:43 +00001998 \var{p}.
1999 \versionadded{1.6}
2000\end{cfuncdesc}
2001
2002\begin{cfuncdesc}{int}{PyDict_SetItem}{PyObject *p, PyObject *key,
2003 PyObject *val}
Georg Brandl99363b62005-09-03 07:27:26 +00002004 Insert \var{value} into the dictionary \var{p} with a key of
Fred Drake3adf79e2001-10-12 19:01:43 +00002005 \var{key}. \var{key} must be hashable; if it isn't,
2006 \exception{TypeError} will be raised.
Georg Brandl99363b62005-09-03 07:27:26 +00002007 Return \code{0} on success or \code{-1} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00002008\end{cfuncdesc}
2009
2010\begin{cfuncdesc}{int}{PyDict_SetItemString}{PyObject *p,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002011 const char *key,
Fred Drake3adf79e2001-10-12 19:01:43 +00002012 PyObject *val}
Georg Brandl99363b62005-09-03 07:27:26 +00002013 Insert \var{value} into the dictionary \var{p} using \var{key} as a
Fred Drake3adf79e2001-10-12 19:01:43 +00002014 key. \var{key} should be a \ctype{char*}. The key object is created
Georg Brandl99363b62005-09-03 07:27:26 +00002015 using \code{PyString_FromString(\var{key})}. Return \code{0} on
Fred Drake3adf79e2001-10-12 19:01:43 +00002016 success or \code{-1} on failure.
2017 \ttindex{PyString_FromString()}
2018\end{cfuncdesc}
2019
2020\begin{cfuncdesc}{int}{PyDict_DelItem}{PyObject *p, PyObject *key}
Georg Brandl99363b62005-09-03 07:27:26 +00002021 Remove the entry in dictionary \var{p} with key \var{key}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002022 \var{key} must be hashable; if it isn't, \exception{TypeError} is
Georg Brandl99363b62005-09-03 07:27:26 +00002023 raised. Return \code{0} on success or \code{-1} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00002024\end{cfuncdesc}
2025
2026\begin{cfuncdesc}{int}{PyDict_DelItemString}{PyObject *p, char *key}
Georg Brandl99363b62005-09-03 07:27:26 +00002027 Remove the entry in dictionary \var{p} which has a key specified by
2028 the string \var{key}. Return \code{0} on success or \code{-1} on
Fred Drake3adf79e2001-10-12 19:01:43 +00002029 failure.
2030\end{cfuncdesc}
2031
2032\begin{cfuncdesc}{PyObject*}{PyDict_GetItem}{PyObject *p, PyObject *key}
Georg Brandl99363b62005-09-03 07:27:26 +00002033 Return the object from dictionary \var{p} which has a key
2034 \var{key}. Return \NULL{} if the key \var{key} is not present, but
Fred Drake3adf79e2001-10-12 19:01:43 +00002035 \emph{without} setting an exception.
2036\end{cfuncdesc}
2037
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002038\begin{cfuncdesc}{PyObject*}{PyDict_GetItemString}{PyObject *p, const char *key}
Fred Drake3adf79e2001-10-12 19:01:43 +00002039 This is the same as \cfunction{PyDict_GetItem()}, but \var{key} is
2040 specified as a \ctype{char*}, rather than a \ctype{PyObject*}.
2041\end{cfuncdesc}
2042
2043\begin{cfuncdesc}{PyObject*}{PyDict_Items}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002044 Return a \ctype{PyListObject} containing all the items from the
Nicholas Bastin975e7252004-09-29 21:39:26 +00002045 dictionary, as in the dictionary method \method{items()} (see the
Fred Drake3adf79e2001-10-12 19:01:43 +00002046 \citetitle[../lib/lib.html]{Python Library Reference}).
2047\end{cfuncdesc}
2048
2049\begin{cfuncdesc}{PyObject*}{PyDict_Keys}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002050 Return a \ctype{PyListObject} containing all the keys from the
Fred Drake3adf79e2001-10-12 19:01:43 +00002051 dictionary, as in the dictionary method \method{keys()} (see the
2052 \citetitle[../lib/lib.html]{Python Library Reference}).
2053\end{cfuncdesc}
2054
2055\begin{cfuncdesc}{PyObject*}{PyDict_Values}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002056 Return a \ctype{PyListObject} containing all the values from the
Fred Drake3adf79e2001-10-12 19:01:43 +00002057 dictionary \var{p}, as in the dictionary method \method{values()}
2058 (see the \citetitle[../lib/lib.html]{Python Library Reference}).
2059\end{cfuncdesc}
2060
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002061\begin{cfuncdesc}{Py_ssize_t}{PyDict_Size}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002062 Return the number of items in the dictionary. This is equivalent
Fred Drake3adf79e2001-10-12 19:01:43 +00002063 to \samp{len(\var{p})} on a dictionary.\bifuncindex{len}
2064\end{cfuncdesc}
2065
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002066\begin{cfuncdesc}{int}{PyDict_Next}{PyObject *p, Py_ssize_t *ppos,
Fred Drake3adf79e2001-10-12 19:01:43 +00002067 PyObject **pkey, PyObject **pvalue}
2068 Iterate over all key-value pairs in the dictionary \var{p}. The
2069 \ctype{int} referred to by \var{ppos} must be initialized to
2070 \code{0} prior to the first call to this function to start the
2071 iteration; the function returns true for each pair in the
2072 dictionary, and false once all pairs have been reported. The
2073 parameters \var{pkey} and \var{pvalue} should either point to
2074 \ctype{PyObject*} variables that will be filled in with each key and
Tim Peters9ddf40b2004-06-20 22:41:32 +00002075 value, respectively, or may be \NULL{}. Any references returned through
Raymond Hettinger54693242003-12-13 19:48:41 +00002076 them are borrowed. \var{ppos} should not be altered during iteration.
2077 Its value represents offsets within the internal dictionary structure,
2078 and since the structure is sparse, the offsets are not consecutive.
Fred Drake3adf79e2001-10-12 19:01:43 +00002079
2080 For example:
2081
2082\begin{verbatim}
2083PyObject *key, *value;
Thomas Woutersb2137042007-02-01 18:02:27 +00002084Py_ssize_t pos = 0;
Fred Drake3adf79e2001-10-12 19:01:43 +00002085
2086while (PyDict_Next(self->dict, &pos, &key, &value)) {
2087 /* do something interesting with the values... */
2088 ...
2089}
2090\end{verbatim}
2091
2092 The dictionary \var{p} should not be mutated during iteration. It
2093 is safe (since Python 2.1) to modify the values of the keys as you
2094 iterate over the dictionary, but only so long as the set of keys
2095 does not change. For example:
2096
2097\begin{verbatim}
2098PyObject *key, *value;
Thomas Woutersb2137042007-02-01 18:02:27 +00002099Py_ssize_t pos = 0;
Fred Drake3adf79e2001-10-12 19:01:43 +00002100
2101while (PyDict_Next(self->dict, &pos, &key, &value)) {
2102 int i = PyInt_AS_LONG(value) + 1;
2103 PyObject *o = PyInt_FromLong(i);
2104 if (o == NULL)
2105 return -1;
2106 if (PyDict_SetItem(self->dict, key, o) < 0) {
2107 Py_DECREF(o);
2108 return -1;
2109 }
2110 Py_DECREF(o);
2111}
2112\end{verbatim}
2113\end{cfuncdesc}
2114
2115\begin{cfuncdesc}{int}{PyDict_Merge}{PyObject *a, PyObject *b, int override}
Tim Petersf582b822001-12-11 18:51:08 +00002116 Iterate over mapping object \var{b} adding key-value pairs to dictionary
2117 \var{a}.
2118 \var{b} may be a dictionary, or any object supporting
2119 \function{PyMapping_Keys()} and \function{PyObject_GetItem()}.
2120 If \var{override} is true, existing pairs in \var{a} will
Fred Drake3adf79e2001-10-12 19:01:43 +00002121 be replaced if a matching key is found in \var{b}, otherwise pairs
2122 will only be added if there is not a matching key in \var{a}.
Tim Petersf582b822001-12-11 18:51:08 +00002123 Return \code{0} on success or \code{-1} if an exception was
Fred Drake3adf79e2001-10-12 19:01:43 +00002124 raised.
2125\versionadded{2.2}
2126\end{cfuncdesc}
2127
2128\begin{cfuncdesc}{int}{PyDict_Update}{PyObject *a, PyObject *b}
2129 This is the same as \code{PyDict_Merge(\var{a}, \var{b}, 1)} in C,
Tim Petersf582b822001-12-11 18:51:08 +00002130 or \code{\var{a}.update(\var{b})} in Python. Return \code{0} on
Fred Drake3adf79e2001-10-12 19:01:43 +00002131 success or \code{-1} if an exception was raised.
2132 \versionadded{2.2}
2133\end{cfuncdesc}
2134
Tim Petersf582b822001-12-11 18:51:08 +00002135\begin{cfuncdesc}{int}{PyDict_MergeFromSeq2}{PyObject *a, PyObject *seq2,
2136 int override}
2137 Update or merge into dictionary \var{a}, from the key-value pairs in
2138 \var{seq2}. \var{seq2} must be an iterable object producing
2139 iterable objects of length 2, viewed as key-value pairs. In case of
2140 duplicate keys, the last wins if \var{override} is true, else the
2141 first wins.
2142 Return \code{0} on success or \code{-1} if an exception
2143 was raised.
2144 Equivalent Python (except for the return value):
2145
2146\begin{verbatim}
2147def PyDict_MergeFromSeq2(a, seq2, override):
2148 for key, value in seq2:
2149 if override or key not in a:
2150 a[key] = value
2151\end{verbatim}
2152
2153 \versionadded{2.2}
2154\end{cfuncdesc}
Fred Drake3adf79e2001-10-12 19:01:43 +00002155
Fred Drake54e62942001-12-11 19:40:16 +00002156
Fred Drake3adf79e2001-10-12 19:01:43 +00002157\section{Other Objects \label{otherObjects}}
2158
2159\subsection{File Objects \label{fileObjects}}
2160
2161\obindex{file}
2162Python's built-in file objects are implemented entirely on the
2163\ctype{FILE*} support from the C standard library. This is an
2164implementation detail and may change in future releases of Python.
2165
2166\begin{ctypedesc}{PyFileObject}
2167 This subtype of \ctype{PyObject} represents a Python file object.
2168\end{ctypedesc}
2169
2170\begin{cvardesc}{PyTypeObject}{PyFile_Type}
2171 This instance of \ctype{PyTypeObject} represents the Python file
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002172 type. This is exposed to Python programs as \code{file} and
2173 \code{types.FileType}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002174 \withsubitem{(in module types)}{\ttindex{FileType}}
2175\end{cvardesc}
2176
2177\begin{cfuncdesc}{int}{PyFile_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002178 Return true if its argument is a \ctype{PyFileObject} or a subtype
Fred Drake3adf79e2001-10-12 19:01:43 +00002179 of \ctype{PyFileObject}.
2180 \versionchanged[Allowed subtypes to be accepted]{2.2}
2181\end{cfuncdesc}
2182
2183\begin{cfuncdesc}{int}{PyFile_CheckExact}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002184 Return true if its argument is a \ctype{PyFileObject}, but not a
Fred Drake3adf79e2001-10-12 19:01:43 +00002185 subtype of \ctype{PyFileObject}.
2186 \versionadded{2.2}
2187\end{cfuncdesc}
2188
2189\begin{cfuncdesc}{PyObject*}{PyFile_FromString}{char *filename, char *mode}
Georg Brandl99363b62005-09-03 07:27:26 +00002190 On success, return a new file object that is opened on the file
Fred Drake3adf79e2001-10-12 19:01:43 +00002191 given by \var{filename}, with a file mode given by \var{mode}, where
2192 \var{mode} has the same semantics as the standard C routine
Georg Brandl99363b62005-09-03 07:27:26 +00002193 \cfunction{fopen()}\ttindex{fopen()}. On failure, return \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002194\end{cfuncdesc}
2195
2196\begin{cfuncdesc}{PyObject*}{PyFile_FromFile}{FILE *fp,
2197 char *name, char *mode,
2198 int (*close)(FILE*)}
Georg Brandl99363b62005-09-03 07:27:26 +00002199 Create a new \ctype{PyFileObject} from the already-open standard C
Fred Drake3adf79e2001-10-12 19:01:43 +00002200 file pointer, \var{fp}. The function \var{close} will be called
Georg Brandl99363b62005-09-03 07:27:26 +00002201 when the file should be closed. Return \NULL{} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00002202\end{cfuncdesc}
2203
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002204\begin{cfuncdesc}{FILE*}{PyFile_AsFile}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002205 Return the file object associated with \var{p} as a \ctype{FILE*}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002206\end{cfuncdesc}
2207
2208\begin{cfuncdesc}{PyObject*}{PyFile_GetLine}{PyObject *p, int n}
2209 Equivalent to \code{\var{p}.readline(\optional{\var{n}})}, this
2210 function reads one line from the object \var{p}. \var{p} may be a
2211 file object or any object with a \method{readline()} method. If
2212 \var{n} is \code{0}, exactly one line is read, regardless of the
2213 length of the line. If \var{n} is greater than \code{0}, no more
2214 than \var{n} bytes will be read from the file; a partial line can be
2215 returned. In both cases, an empty string is returned if the end of
2216 the file is reached immediately. If \var{n} is less than \code{0},
2217 however, one line is read regardless of length, but
2218 \exception{EOFError} is raised if the end of the file is reached
2219 immediately.
2220 \withsubitem{(built-in exception)}{\ttindex{EOFError}}
2221\end{cfuncdesc}
2222
2223\begin{cfuncdesc}{PyObject*}{PyFile_Name}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002224 Return the name of the file specified by \var{p} as a string
Fred Drake3adf79e2001-10-12 19:01:43 +00002225 object.
2226\end{cfuncdesc}
2227
2228\begin{cfuncdesc}{void}{PyFile_SetBufSize}{PyFileObject *p, int n}
2229 Available on systems with \cfunction{setvbuf()}\ttindex{setvbuf()}
2230 only. This should only be called immediately after file object
2231 creation.
2232\end{cfuncdesc}
2233
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00002234\begin{cfuncdesc}{int}{PyFile_Encoding}{PyFileObject *p, char *enc}
2235 Set the file's encoding for Unicode output to \var{enc}. Return
2236 1 on success and 0 on failure.
2237 \versionadded{2.3}
2238\end{cfuncdesc}
2239
Fred Drake3adf79e2001-10-12 19:01:43 +00002240\begin{cfuncdesc}{int}{PyFile_SoftSpace}{PyObject *p, int newflag}
Georg Brandl99363b62005-09-03 07:27:26 +00002241 This function exists for internal use by the interpreter. Set the
Fred Drake3adf79e2001-10-12 19:01:43 +00002242 \member{softspace} attribute of \var{p} to \var{newflag} and
Georg Brandl99363b62005-09-03 07:27:26 +00002243 \withsubitem{(file attribute)}{\ttindex{softspace}}return the
Fred Drake3adf79e2001-10-12 19:01:43 +00002244 previous value. \var{p} does not have to be a file object for this
2245 function to work properly; any object is supported (thought its only
2246 interesting if the \member{softspace} attribute can be set). This
2247 function clears any errors, and will return \code{0} as the previous
2248 value if the attribute either does not exist or if there were errors
2249 in retrieving it. There is no way to detect errors from this
2250 function, but doing so should not be needed.
2251\end{cfuncdesc}
2252
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002253\begin{cfuncdesc}{int}{PyFile_WriteObject}{PyObject *obj, PyObject *p,
Fred Drake3adf79e2001-10-12 19:01:43 +00002254 int flags}
Georg Brandl99363b62005-09-03 07:27:26 +00002255 Write object \var{obj} to file object \var{p}. The only supported
Fred Drake3adf79e2001-10-12 19:01:43 +00002256 flag for \var{flags} is
2257 \constant{Py_PRINT_RAW}\ttindex{Py_PRINT_RAW}; if given, the
2258 \function{str()} of the object is written instead of the
Georg Brandl99363b62005-09-03 07:27:26 +00002259 \function{repr()}. Return \code{0} on success or \code{-1} on
Fred Drake3adf79e2001-10-12 19:01:43 +00002260 failure; the appropriate exception will be set.
2261\end{cfuncdesc}
2262
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002263\begin{cfuncdesc}{int}{PyFile_WriteString}{const char *s, PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002264 Write string \var{s} to file object \var{p}. Return \code{0} on
Fred Drake3adf79e2001-10-12 19:01:43 +00002265 success or \code{-1} on failure; the appropriate exception will be
2266 set.
2267\end{cfuncdesc}
2268
2269
2270\subsection{Instance Objects \label{instanceObjects}}
2271
2272\obindex{instance}
2273There are very few functions specific to instance objects.
2274
2275\begin{cvardesc}{PyTypeObject}{PyInstance_Type}
2276 Type object for class instances.
2277\end{cvardesc}
2278
2279\begin{cfuncdesc}{int}{PyInstance_Check}{PyObject *obj}
Georg Brandl99363b62005-09-03 07:27:26 +00002280 Return true if \var{obj} is an instance.
Fred Drake3adf79e2001-10-12 19:01:43 +00002281\end{cfuncdesc}
2282
2283\begin{cfuncdesc}{PyObject*}{PyInstance_New}{PyObject *class,
2284 PyObject *arg,
2285 PyObject *kw}
2286 Create a new instance of a specific class. The parameters \var{arg}
2287 and \var{kw} are used as the positional and keyword parameters to
2288 the object's constructor.
2289\end{cfuncdesc}
2290
2291\begin{cfuncdesc}{PyObject*}{PyInstance_NewRaw}{PyObject *class,
2292 PyObject *dict}
Neil Schemenauerc4932292005-06-18 17:54:13 +00002293 Create a new instance of a specific class without calling its
Fred Drake3adf79e2001-10-12 19:01:43 +00002294 constructor. \var{class} is the class of new object. The
2295 \var{dict} parameter will be used as the object's \member{__dict__};
Tim Peters9ddf40b2004-06-20 22:41:32 +00002296 if \NULL{}, a new dictionary will be created for the instance.
Fred Drake3adf79e2001-10-12 19:01:43 +00002297\end{cfuncdesc}
2298
2299
Georg Brandl9b743f52006-02-20 12:57:53 +00002300\subsection{Function Objects \label{function-objects}}
2301
2302\obindex{function}
2303There are a few functions specific to Python functions.
2304
2305\begin{ctypedesc}{PyFunctionObject}
2306 The C structure used for functions.
2307\end{ctypedesc}
2308
2309\begin{cvardesc}{PyTypeObject}{PyFunction_Type}
2310 This is an instance of \ctype{PyTypeObject} and represents the
2311 Python function type. It is exposed to Python programmers as
2312 \code{types.FunctionType}.
2313 \withsubitem{(in module types)}{\ttindex{MethodType}}
2314\end{cvardesc}
2315
2316\begin{cfuncdesc}{int}{PyFunction_Check}{PyObject *o}
2317 Return true if \var{o} is a function object (has type
2318 \cdata{PyFunction_Type}). The parameter must not be \NULL{}.
2319\end{cfuncdesc}
2320
2321\begin{cfuncdesc}{PyObject*}{PyFunction_New}{PyObject *code,
2322 PyObject *globals}
2323 Return a new function object associated with the code object
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002324 \var{code}. \var{globals} must be a dictionary with the global
2325 variables accessible to the function.
Georg Brandl9b743f52006-02-20 12:57:53 +00002326
2327 The function's docstring, name and \var{__module__} are retrieved
2328 from the code object, the argument defaults and closure are set to
2329 \NULL{}.
2330\end{cfuncdesc}
2331
2332\begin{cfuncdesc}{PyObject*}{PyFunction_GetCode}{PyObject *op}
2333 Return the code object associated with the function object \var{op}.
2334\end{cfuncdesc}
2335
2336\begin{cfuncdesc}{PyObject*}{PyFunction_GetGlobals}{PyObject *op}
2337 Return the globals dictionary associated with the function object
2338 \var{op}.
2339\end{cfuncdesc}
2340
2341\begin{cfuncdesc}{PyObject*}{PyFunction_GetModule}{PyObject *op}
2342 Return the \var{__module__} attribute of the function object \var{op}.
2343 This is normally a string containing the module name, but can be set
2344 to any other object by Python code.
2345\end{cfuncdesc}
2346
2347\begin{cfuncdesc}{PyObject*}{PyFunction_GetDefaults}{PyObject *op}
2348 Return the argument default values of the function object \var{op}.
2349 This can be a tuple of arguments or \NULL{}.
2350\end{cfuncdesc}
2351
2352\begin{cfuncdesc}{int}{PyFunction_SetDefaults}{PyObject *op,
2353 PyObject *defaults}
2354 Set the argument default values for the function object \var{op}.
2355 \var{defaults} must be \var{Py_None} or a tuple.
2356
2357 Raises \exception{SystemError} and returns \code{-1} on failure.
2358\end{cfuncdesc}
2359
2360\begin{cfuncdesc}{PyObject*}{PyFunction_GetClosure}{PyObject *op}
2361 Return the closure associated with the function object \var{op}.
2362 This can be \NULL{} or a tuple of cell objects.
2363\end{cfuncdesc}
2364
2365\begin{cfuncdesc}{int}{PyFunction_SetClosure}{PyObject *op,
2366 PyObject *closure}
2367 Set the closure associated with the function object \var{op}.
2368 \var{closure} must be \var{Py_None} or a tuple of cell objects.
2369
2370 Raises \exception{SystemError} and returns \code{-1} on failure.
2371\end{cfuncdesc}
2372
2373
Fred Drake3adf79e2001-10-12 19:01:43 +00002374\subsection{Method Objects \label{method-objects}}
2375
2376\obindex{method}
2377There are some useful functions that are useful for working with
2378method objects.
2379
2380\begin{cvardesc}{PyTypeObject}{PyMethod_Type}
2381 This instance of \ctype{PyTypeObject} represents the Python method
2382 type. This is exposed to Python programs as \code{types.MethodType}.
2383 \withsubitem{(in module types)}{\ttindex{MethodType}}
2384\end{cvardesc}
2385
2386\begin{cfuncdesc}{int}{PyMethod_Check}{PyObject *o}
2387 Return true if \var{o} is a method object (has type
Tim Peters9ddf40b2004-06-20 22:41:32 +00002388 \cdata{PyMethod_Type}). The parameter must not be \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002389\end{cfuncdesc}
2390
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002391\begin{cfuncdesc}{PyObject*}{PyMethod_New}{PyObject *func,
Fred Drake3adf79e2001-10-12 19:01:43 +00002392 PyObject *self, PyObject *class}
2393 Return a new method object, with \var{func} being any callable
2394 object; this is the function that will be called when the method is
2395 called. If this method should be bound to an instance, \var{self}
2396 should be the instance and \var{class} should be the class of
2397 \var{self}, otherwise \var{self} should be \NULL{} and \var{class}
2398 should be the class which provides the unbound method..
2399\end{cfuncdesc}
2400
2401\begin{cfuncdesc}{PyObject*}{PyMethod_Class}{PyObject *meth}
2402 Return the class object from which the method \var{meth} was
2403 created; if this was created from an instance, it will be the class
2404 of the instance.
2405\end{cfuncdesc}
2406
2407\begin{cfuncdesc}{PyObject*}{PyMethod_GET_CLASS}{PyObject *meth}
2408 Macro version of \cfunction{PyMethod_Class()} which avoids error
2409 checking.
2410\end{cfuncdesc}
2411
2412\begin{cfuncdesc}{PyObject*}{PyMethod_Function}{PyObject *meth}
2413 Return the function object associated with the method \var{meth}.
2414\end{cfuncdesc}
2415
2416\begin{cfuncdesc}{PyObject*}{PyMethod_GET_FUNCTION}{PyObject *meth}
2417 Macro version of \cfunction{PyMethod_Function()} which avoids error
2418 checking.
2419\end{cfuncdesc}
2420
2421\begin{cfuncdesc}{PyObject*}{PyMethod_Self}{PyObject *meth}
2422 Return the instance associated with the method \var{meth} if it is
Tim Peters9ddf40b2004-06-20 22:41:32 +00002423 bound, otherwise return \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002424\end{cfuncdesc}
2425
2426\begin{cfuncdesc}{PyObject*}{PyMethod_GET_SELF}{PyObject *meth}
2427 Macro version of \cfunction{PyMethod_Self()} which avoids error
2428 checking.
2429\end{cfuncdesc}
2430
2431
2432\subsection{Module Objects \label{moduleObjects}}
2433
2434\obindex{module}
2435There are only a few functions special to module objects.
2436
2437\begin{cvardesc}{PyTypeObject}{PyModule_Type}
2438 This instance of \ctype{PyTypeObject} represents the Python module
2439 type. This is exposed to Python programs as
2440 \code{types.ModuleType}.
2441 \withsubitem{(in module types)}{\ttindex{ModuleType}}
2442\end{cvardesc}
2443
2444\begin{cfuncdesc}{int}{PyModule_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002445 Return true if \var{p} is a module object, or a subtype of a module
Fred Drake3adf79e2001-10-12 19:01:43 +00002446 object.
2447 \versionchanged[Allowed subtypes to be accepted]{2.2}
2448\end{cfuncdesc}
2449
2450\begin{cfuncdesc}{int}{PyModule_CheckExact}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002451 Return true if \var{p} is a module object, but not a subtype of
Fred Drake3adf79e2001-10-12 19:01:43 +00002452 \cdata{PyModule_Type}.
2453 \versionadded{2.2}
2454\end{cfuncdesc}
2455
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002456\begin{cfuncdesc}{PyObject*}{PyModule_New}{const char *name}
Fred Drake3adf79e2001-10-12 19:01:43 +00002457 Return a new module object with the \member{__name__} attribute set
2458 to \var{name}. Only the module's \member{__doc__} and
2459 \member{__name__} attributes are filled in; the caller is
2460 responsible for providing a \member{__file__} attribute.
2461 \withsubitem{(module attribute)}{
2462 \ttindex{__name__}\ttindex{__doc__}\ttindex{__file__}}
2463\end{cfuncdesc}
2464
2465\begin{cfuncdesc}{PyObject*}{PyModule_GetDict}{PyObject *module}
2466 Return the dictionary object that implements \var{module}'s
2467 namespace; this object is the same as the \member{__dict__}
2468 attribute of the module object. This function never fails.
2469 \withsubitem{(module attribute)}{\ttindex{__dict__}}
Fred Drakef495ef72002-04-12 19:32:07 +00002470 It is recommended extensions use other \cfunction{PyModule_*()}
2471 and \cfunction{PyObject_*()} functions rather than directly
2472 manipulate a module's \member{__dict__}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002473\end{cfuncdesc}
2474
2475\begin{cfuncdesc}{char*}{PyModule_GetName}{PyObject *module}
2476 Return \var{module}'s \member{__name__} value. If the module does
2477 not provide one, or if it is not a string, \exception{SystemError}
2478 is raised and \NULL{} is returned.
2479 \withsubitem{(module attribute)}{\ttindex{__name__}}
2480 \withsubitem{(built-in exception)}{\ttindex{SystemError}}
2481\end{cfuncdesc}
2482
2483\begin{cfuncdesc}{char*}{PyModule_GetFilename}{PyObject *module}
2484 Return the name of the file from which \var{module} was loaded using
2485 \var{module}'s \member{__file__} attribute. If this is not defined,
2486 or if it is not a string, raise \exception{SystemError} and return
Tim Peters9ddf40b2004-06-20 22:41:32 +00002487 \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002488 \withsubitem{(module attribute)}{\ttindex{__file__}}
2489 \withsubitem{(built-in exception)}{\ttindex{SystemError}}
2490\end{cfuncdesc}
2491
2492\begin{cfuncdesc}{int}{PyModule_AddObject}{PyObject *module,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002493 const char *name, PyObject *value}
Fred Drake3adf79e2001-10-12 19:01:43 +00002494 Add an object to \var{module} as \var{name}. This is a convenience
2495 function which can be used from the module's initialization
Georg Brandl99363b62005-09-03 07:27:26 +00002496 function. This steals a reference to \var{value}. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00002497 \code{-1} on error, \code{0} on success.
2498 \versionadded{2.0}
2499\end{cfuncdesc}
2500
2501\begin{cfuncdesc}{int}{PyModule_AddIntConstant}{PyObject *module,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002502 const char *name, long value}
Fred Drake3adf79e2001-10-12 19:01:43 +00002503 Add an integer constant to \var{module} as \var{name}. This
2504 convenience function can be used from the module's initialization
Georg Brandl99363b62005-09-03 07:27:26 +00002505 function. Return \code{-1} on error, \code{0} on success.
Fred Drake3adf79e2001-10-12 19:01:43 +00002506 \versionadded{2.0}
2507\end{cfuncdesc}
2508
2509\begin{cfuncdesc}{int}{PyModule_AddStringConstant}{PyObject *module,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002510 const char *name, const char *value}
Fred Drake3adf79e2001-10-12 19:01:43 +00002511 Add a string constant to \var{module} as \var{name}. This
2512 convenience function can be used from the module's initialization
Georg Brandl99363b62005-09-03 07:27:26 +00002513 function. The string \var{value} must be null-terminated. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00002514 \code{-1} on error, \code{0} on success.
2515 \versionadded{2.0}
2516\end{cfuncdesc}
2517
2518
2519\subsection{Iterator Objects \label{iterator-objects}}
2520
2521Python provides two general-purpose iterator objects. The first, a
2522sequence iterator, works with an arbitrary sequence supporting the
2523\method{__getitem__()} method. The second works with a callable
2524object and a sentinel value, calling the callable for each item in the
2525sequence, and ending the iteration when the sentinel value is
2526returned.
2527
2528\begin{cvardesc}{PyTypeObject}{PySeqIter_Type}
2529 Type object for iterator objects returned by
2530 \cfunction{PySeqIter_New()} and the one-argument form of the
2531 \function{iter()} built-in function for built-in sequence types.
2532 \versionadded{2.2}
2533\end{cvardesc}
2534
2535\begin{cfuncdesc}{int}{PySeqIter_Check}{op}
2536 Return true if the type of \var{op} is \cdata{PySeqIter_Type}.
2537 \versionadded{2.2}
2538\end{cfuncdesc}
2539
2540\begin{cfuncdesc}{PyObject*}{PySeqIter_New}{PyObject *seq}
2541 Return an iterator that works with a general sequence object,
2542 \var{seq}. The iteration ends when the sequence raises
2543 \exception{IndexError} for the subscripting operation.
2544 \versionadded{2.2}
2545\end{cfuncdesc}
2546
2547\begin{cvardesc}{PyTypeObject}{PyCallIter_Type}
2548 Type object for iterator objects returned by
2549 \cfunction{PyCallIter_New()} and the two-argument form of the
2550 \function{iter()} built-in function.
2551 \versionadded{2.2}
2552\end{cvardesc}
2553
2554\begin{cfuncdesc}{int}{PyCallIter_Check}{op}
2555 Return true if the type of \var{op} is \cdata{PyCallIter_Type}.
2556 \versionadded{2.2}
2557\end{cfuncdesc}
2558
2559\begin{cfuncdesc}{PyObject*}{PyCallIter_New}{PyObject *callable,
2560 PyObject *sentinel}
2561 Return a new iterator. The first parameter, \var{callable}, can be
2562 any Python callable object that can be called with no parameters;
2563 each call to it should return the next item in the iteration. When
2564 \var{callable} returns a value equal to \var{sentinel}, the
2565 iteration will be terminated.
2566 \versionadded{2.2}
2567\end{cfuncdesc}
2568
2569
2570\subsection{Descriptor Objects \label{descriptor-objects}}
2571
Fred Drake54e62942001-12-11 19:40:16 +00002572``Descriptors'' are objects that describe some attribute of an object.
2573They are found in the dictionary of type objects.
2574
Fred Drake3adf79e2001-10-12 19:01:43 +00002575\begin{cvardesc}{PyTypeObject}{PyProperty_Type}
Fred Drake54e62942001-12-11 19:40:16 +00002576 The type object for the built-in descriptor types.
Fred Drake3adf79e2001-10-12 19:01:43 +00002577 \versionadded{2.2}
2578\end{cvardesc}
2579
2580\begin{cfuncdesc}{PyObject*}{PyDescr_NewGetSet}{PyTypeObject *type,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002581 struct PyGetSetDef *getset}
Fred Drake3adf79e2001-10-12 19:01:43 +00002582 \versionadded{2.2}
2583\end{cfuncdesc}
2584
2585\begin{cfuncdesc}{PyObject*}{PyDescr_NewMember}{PyTypeObject *type,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002586 struct PyMemberDef *meth}
Fred Drake3adf79e2001-10-12 19:01:43 +00002587 \versionadded{2.2}
2588\end{cfuncdesc}
2589
2590\begin{cfuncdesc}{PyObject*}{PyDescr_NewMethod}{PyTypeObject *type,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002591 struct PyMethodDef *meth}
Fred Drake3adf79e2001-10-12 19:01:43 +00002592 \versionadded{2.2}
2593\end{cfuncdesc}
2594
2595\begin{cfuncdesc}{PyObject*}{PyDescr_NewWrapper}{PyTypeObject *type,
2596 struct wrapperbase *wrapper,
2597 void *wrapped}
2598 \versionadded{2.2}
2599\end{cfuncdesc}
2600
Thomas Heller8178a222004-02-09 10:47:11 +00002601\begin{cfuncdesc}{PyObject*}{PyDescr_NewClassMethod}{PyTypeObject *type,
2602 PyMethodDef *method}
2603 \versionadded{2.3}
2604\end{cfuncdesc}
2605
Fred Drake3adf79e2001-10-12 19:01:43 +00002606\begin{cfuncdesc}{int}{PyDescr_IsData}{PyObject *descr}
Georg Brandl99363b62005-09-03 07:27:26 +00002607 Return true if the descriptor objects \var{descr} describes a data
Fred Drake3adf79e2001-10-12 19:01:43 +00002608 attribute, or false if it describes a method. \var{descr} must be a
2609 descriptor object; there is no error checking.
2610 \versionadded{2.2}
2611\end{cfuncdesc}
2612
2613\begin{cfuncdesc}{PyObject*}{PyWrapper_New}{PyObject *, PyObject *}
2614 \versionadded{2.2}
2615\end{cfuncdesc}
2616
2617
2618\subsection{Slice Objects \label{slice-objects}}
2619
2620\begin{cvardesc}{PyTypeObject}{PySlice_Type}
2621 The type object for slice objects. This is the same as
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002622 \code{slice} and \code{types.SliceType}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002623 \withsubitem{(in module types)}{\ttindex{SliceType}}
2624\end{cvardesc}
2625
2626\begin{cfuncdesc}{int}{PySlice_Check}{PyObject *ob}
Georg Brandl99363b62005-09-03 07:27:26 +00002627 Return true if \var{ob} is a slice object; \var{ob} must not be
Tim Peters9ddf40b2004-06-20 22:41:32 +00002628 \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002629\end{cfuncdesc}
2630
2631\begin{cfuncdesc}{PyObject*}{PySlice_New}{PyObject *start, PyObject *stop,
2632 PyObject *step}
2633 Return a new slice object with the given values. The \var{start},
2634 \var{stop}, and \var{step} parameters are used as the values of the
2635 slice object attributes of the same names. Any of the values may be
Tim Peters9ddf40b2004-06-20 22:41:32 +00002636 \NULL{}, in which case the \code{None} will be used for the
Georg Brandl99363b62005-09-03 07:27:26 +00002637 corresponding attribute. Return \NULL{} if the new object could
Fred Drake3adf79e2001-10-12 19:01:43 +00002638 not be allocated.
2639\end{cfuncdesc}
2640
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002641\begin{cfuncdesc}{int}{PySlice_GetIndices}{PySliceObject *slice, Py_ssize_t length,
2642 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step}
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002643Retrieve the start, stop and step indices from the slice object
2644\var{slice}, assuming a sequence of length \var{length}. Treats
2645indices greater than \var{length} as errors.
2646
2647Returns 0 on success and -1 on error with no exception set (unless one
2648of the indices was not \constant{None} and failed to be converted to
2649an integer, in which case -1 is returned with an exception set).
2650
2651You probably do not want to use this function. If you want to use
2652slice objects in versions of Python prior to 2.3, you would probably
2653do well to incorporate the source of \cfunction{PySlice_GetIndicesEx},
2654suitably renamed, in the source of your extension.
2655\end{cfuncdesc}
2656
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002657\begin{cfuncdesc}{int}{PySlice_GetIndicesEx}{PySliceObject *slice, Py_ssize_t length,
2658 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step,
2659 Py_ssize_t *slicelength}
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002660Usable replacement for \cfunction{PySlice_GetIndices}. Retrieve the
2661start, stop, and step indices from the slice object \var{slice}
2662assuming a sequence of length \var{length}, and store the length of
2663the slice in \var{slicelength}. Out of bounds indices are clipped in
2664a manner consistent with the handling of normal slices.
2665
2666Returns 0 on success and -1 on error with exception set.
2667
2668\versionadded{2.3}
Fred Drake3adf79e2001-10-12 19:01:43 +00002669\end{cfuncdesc}
2670
2671
2672\subsection{Weak Reference Objects \label{weakref-objects}}
2673
2674Python supports \emph{weak references} as first-class objects. There
2675are two specific object types which directly implement weak
2676references. The first is a simple reference object, and the second
2677acts as a proxy for the original object as much as it can.
2678
2679\begin{cfuncdesc}{int}{PyWeakref_Check}{ob}
2680 Return true if \var{ob} is either a reference or proxy object.
2681 \versionadded{2.2}
2682\end{cfuncdesc}
2683
2684\begin{cfuncdesc}{int}{PyWeakref_CheckRef}{ob}
2685 Return true if \var{ob} is a reference object.
2686 \versionadded{2.2}
2687\end{cfuncdesc}
2688
2689\begin{cfuncdesc}{int}{PyWeakref_CheckProxy}{ob}
2690 Return true if \var{ob} is a proxy object.
2691 \versionadded{2.2}
2692\end{cfuncdesc}
2693
2694\begin{cfuncdesc}{PyObject*}{PyWeakref_NewRef}{PyObject *ob,
2695 PyObject *callback}
2696 Return a weak reference object for the object \var{ob}. This will
2697 always return a new reference, but is not guaranteed to create a new
2698 object; an existing reference object may be returned. The second
2699 parameter, \var{callback}, can be a callable object that receives
2700 notification when \var{ob} is garbage collected; it should accept a
Raymond Hettinger5232f502004-03-25 08:51:36 +00002701 single parameter, which will be the weak reference object itself.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002702 \var{callback} may also be \code{None} or \NULL{}. If \var{ob}
Fred Drake3adf79e2001-10-12 19:01:43 +00002703 is not a weakly-referencable object, or if \var{callback} is not
Tim Peters9ddf40b2004-06-20 22:41:32 +00002704 callable, \code{None}, or \NULL{}, this will return \NULL{} and
Fred Drake3adf79e2001-10-12 19:01:43 +00002705 raise \exception{TypeError}.
2706 \versionadded{2.2}
2707\end{cfuncdesc}
2708
2709\begin{cfuncdesc}{PyObject*}{PyWeakref_NewProxy}{PyObject *ob,
2710 PyObject *callback}
2711 Return a weak reference proxy object for the object \var{ob}. This
2712 will always return a new reference, but is not guaranteed to create
2713 a new object; an existing proxy object may be returned. The second
2714 parameter, \var{callback}, can be a callable object that receives
2715 notification when \var{ob} is garbage collected; it should accept a
Raymond Hettinger5232f502004-03-25 08:51:36 +00002716 single parameter, which will be the weak reference object itself.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002717 \var{callback} may also be \code{None} or \NULL{}. If \var{ob} is not
Fred Drake3adf79e2001-10-12 19:01:43 +00002718 a weakly-referencable object, or if \var{callback} is not callable,
Tim Peters9ddf40b2004-06-20 22:41:32 +00002719 \code{None}, or \NULL{}, this will return \NULL{} and raise
Fred Drake3adf79e2001-10-12 19:01:43 +00002720 \exception{TypeError}.
2721 \versionadded{2.2}
2722\end{cfuncdesc}
2723
2724\begin{cfuncdesc}{PyObject*}{PyWeakref_GetObject}{PyObject *ref}
Georg Brandl99363b62005-09-03 07:27:26 +00002725 Return the referenced object from a weak reference, \var{ref}. If
Ka-Ping Yeebd379e92003-03-28 18:07:16 +00002726 the referent is no longer live, returns \code{None}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002727 \versionadded{2.2}
2728\end{cfuncdesc}
2729
2730\begin{cfuncdesc}{PyObject*}{PyWeakref_GET_OBJECT}{PyObject *ref}
2731 Similar to \cfunction{PyWeakref_GetObject()}, but implemented as a
2732 macro that does no error checking.
2733 \versionadded{2.2}
2734\end{cfuncdesc}
2735
2736
2737\subsection{CObjects \label{cObjects}}
2738
2739\obindex{CObject}
2740Refer to \emph{Extending and Embedding the Python Interpreter},
Fred Drake54e62942001-12-11 19:40:16 +00002741section~1.12, ``Providing a C API for an Extension Module,'' for more
Fred Drake3adf79e2001-10-12 19:01:43 +00002742information on using these objects.
2743
2744
2745\begin{ctypedesc}{PyCObject}
2746 This subtype of \ctype{PyObject} represents an opaque value, useful
2747 for C extension modules who need to pass an opaque value (as a
2748 \ctype{void*} pointer) through Python code to other C code. It is
2749 often used to make a C function pointer defined in one module
2750 available to other modules, so the regular import mechanism can be
2751 used to access C APIs defined in dynamically loaded modules.
2752\end{ctypedesc}
2753
2754\begin{cfuncdesc}{int}{PyCObject_Check}{PyObject *p}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002755 Return true if its argument is a \ctype{PyCObject}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002756\end{cfuncdesc}
2757
Tim Petersf582b822001-12-11 18:51:08 +00002758\begin{cfuncdesc}{PyObject*}{PyCObject_FromVoidPtr}{void* cobj,
Fred Drake54e62942001-12-11 19:40:16 +00002759 void (*destr)(void *)}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002760 Create a \ctype{PyCObject} from the \code{void *}\var{cobj}. The
Fred Drake3adf79e2001-10-12 19:01:43 +00002761 \var{destr} function will be called when the object is reclaimed,
Tim Peters9ddf40b2004-06-20 22:41:32 +00002762 unless it is \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002763\end{cfuncdesc}
2764
2765\begin{cfuncdesc}{PyObject*}{PyCObject_FromVoidPtrAndDesc}{void* cobj,
2766 void* desc, void (*destr)(void *, void *)}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002767 Create a \ctype{PyCObject} from the \ctype{void *}\var{cobj}. The
Fred Drake3adf79e2001-10-12 19:01:43 +00002768 \var{destr} function will be called when the object is reclaimed.
2769 The \var{desc} argument can be used to pass extra callback data for
2770 the destructor function.
2771\end{cfuncdesc}
2772
2773\begin{cfuncdesc}{void*}{PyCObject_AsVoidPtr}{PyObject* self}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002774 Return the object \ctype{void *} that the \ctype{PyCObject}
Fred Drake3adf79e2001-10-12 19:01:43 +00002775 \var{self} was created with.
2776\end{cfuncdesc}
2777
2778\begin{cfuncdesc}{void*}{PyCObject_GetDesc}{PyObject* self}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002779 Return the description \ctype{void *} that the \ctype{PyCObject}
Fred Drake3adf79e2001-10-12 19:01:43 +00002780 \var{self} was created with.
2781\end{cfuncdesc}
Fred Drakecd8474e2001-11-26 21:29:17 +00002782
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002783\begin{cfuncdesc}{int}{PyCObject_SetVoidPtr}{PyObject* self, void* cobj}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002784 Set the void pointer inside \var{self} to \var{cobj}.
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002785 The \ctype{PyCObject} must not have an associated destructor.
2786 Return true on success, false on failure.
2787\end{cfuncdesc}
2788
Fred Drakecd8474e2001-11-26 21:29:17 +00002789
2790\subsection{Cell Objects \label{cell-objects}}
2791
2792``Cell'' objects are used to implement variables referenced by
2793multiple scopes. For each such variable, a cell object is created to
2794store the value; the local variables of each stack frame that
2795references the value contains a reference to the cells from outer
2796scopes which also use that variable. When the value is accessed, the
2797value contained in the cell is used instead of the cell object
2798itself. This de-referencing of the cell object requires support from
2799the generated byte-code; these are not automatically de-referenced
2800when accessed. Cell objects are not likely to be useful elsewhere.
2801
Fred Drake54e62942001-12-11 19:40:16 +00002802\begin{ctypedesc}{PyCellObject}
2803 The C structure used for cell objects.
2804\end{ctypedesc}
2805
Fred Drakecd8474e2001-11-26 21:29:17 +00002806\begin{cvardesc}{PyTypeObject}{PyCell_Type}
Georg Brandl9b743f52006-02-20 12:57:53 +00002807 The type object corresponding to cell objects.
Fred Drakecd8474e2001-11-26 21:29:17 +00002808\end{cvardesc}
2809
2810\begin{cfuncdesc}{int}{PyCell_Check}{ob}
2811 Return true if \var{ob} is a cell object; \var{ob} must not be
Tim Peters9ddf40b2004-06-20 22:41:32 +00002812 \NULL{}.
Fred Drakecd8474e2001-11-26 21:29:17 +00002813\end{cfuncdesc}
2814
2815\begin{cfuncdesc}{PyObject*}{PyCell_New}{PyObject *ob}
2816 Create and return a new cell object containing the value \var{ob}.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002817 The parameter may be \NULL{}.
Fred Drakecd8474e2001-11-26 21:29:17 +00002818\end{cfuncdesc}
2819
2820\begin{cfuncdesc}{PyObject*}{PyCell_Get}{PyObject *cell}
2821 Return the contents of the cell \var{cell}.
2822\end{cfuncdesc}
2823
2824\begin{cfuncdesc}{PyObject*}{PyCell_GET}{PyObject *cell}
2825 Return the contents of the cell \var{cell}, but without checking
Raymond Hettingerf4bb1f92003-08-23 03:38:11 +00002826 that \var{cell} is non-\NULL{} and a cell object.
Fred Drakecd8474e2001-11-26 21:29:17 +00002827\end{cfuncdesc}
2828
2829\begin{cfuncdesc}{int}{PyCell_Set}{PyObject *cell, PyObject *value}
2830 Set the contents of the cell object \var{cell} to \var{value}. This
2831 releases the reference to any current content of the cell.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002832 \var{value} may be \NULL{}. \var{cell} must be non-\NULL{}; if it is
Fred Drakecd8474e2001-11-26 21:29:17 +00002833 not a cell object, \code{-1} will be returned. On success, \code{0}
2834 will be returned.
2835\end{cfuncdesc}
2836
2837\begin{cfuncdesc}{void}{PyCell_SET}{PyObject *cell, PyObject *value}
2838 Sets the value of the cell object \var{cell} to \var{value}. No
2839 reference counts are adjusted, and no checks are made for safety;
2840 \var{cell} must be non-\NULL{} and must be a cell object.
2841\end{cfuncdesc}
Martin v. Löwise440e472004-06-01 15:22:42 +00002842
2843
2844\subsection{Generator Objects \label{gen-objects}}
2845
2846Generator objects are what Python uses to implement generator iterators.
2847They are normally created by iterating over a function that yields values,
2848rather than explicitly calling \cfunction{PyGen_New}.
2849
2850\begin{ctypedesc}{PyGenObject}
2851 The C structure used for generator objects.
2852\end{ctypedesc}
2853
2854\begin{cvardesc}{PyTypeObject}{PyGen_Type}
2855 The type object corresponding to generator objects
2856\end{cvardesc}
2857
2858\begin{cfuncdesc}{int}{PyGen_Check}{ob}
2859 Return true if \var{ob} is a generator object; \var{ob} must not be
Tim Peters9ddf40b2004-06-20 22:41:32 +00002860 \NULL{}.
Martin v. Löwise440e472004-06-01 15:22:42 +00002861\end{cfuncdesc}
2862
2863\begin{cfuncdesc}{int}{PyGen_CheckExact}{ob}
2864 Return true if \var{ob}'s type is \var{PyGen_Type}
2865 is a generator object; \var{ob} must not be
Tim Peters9ddf40b2004-06-20 22:41:32 +00002866 \NULL{}.
Martin v. Löwise440e472004-06-01 15:22:42 +00002867\end{cfuncdesc}
2868
2869\begin{cfuncdesc}{PyObject*}{PyGen_New}{PyFrameObject *frame}
2870 Create and return a new generator object based on the \var{frame} object.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002871 A reference to \var{frame} is stolen by this function.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002872 The parameter must not be \NULL{}.
2873\end{cfuncdesc}
2874
2875
2876\subsection{DateTime Objects \label{datetime-objects}}
2877
2878Various date and time objects are supplied by the \module{datetime}
2879module. Before using any of these functions, the header file
2880\file{datetime.h} must be included in your source (note that this is
Thomas Wouters89f507f2006-12-13 04:49:30 +00002881not included by \file{Python.h}), and the macro
2882\cfunction{PyDateTime_IMPORT} must be invoked. The macro puts a
2883pointer to a C structure into a static variable,
2884\code{PyDateTimeAPI}, that is used by the following macros.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002885
Tim Peters183dabc2004-07-11 19:26:19 +00002886Type-check macros:
2887
2888\begin{cfuncdesc}{int}{PyDate_Check}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002889 Return true if \var{ob} is of type \cdata{PyDateTime_DateType} or
2890 a subtype of \cdata{PyDateTime_DateType}. \var{ob} must not be
2891 \NULL{}.
2892 \versionadded{2.4}
2893\end{cfuncdesc}
2894
Tim Peters183dabc2004-07-11 19:26:19 +00002895\begin{cfuncdesc}{int}{PyDate_CheckExact}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002896 Return true if \var{ob} is of type \cdata{PyDateTime_DateType}.
2897 \var{ob} must not be \NULL{}.
2898 \versionadded{2.4}
2899\end{cfuncdesc}
2900
Tim Peters183dabc2004-07-11 19:26:19 +00002901\begin{cfuncdesc}{int}{PyDateTime_Check}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002902 Return true if \var{ob} is of type \cdata{PyDateTime_DateTimeType} or
2903 a subtype of \cdata{PyDateTime_DateTimeType}. \var{ob} must not be
2904 \NULL{}.
2905 \versionadded{2.4}
2906\end{cfuncdesc}
2907
Tim Peters183dabc2004-07-11 19:26:19 +00002908\begin{cfuncdesc}{int}{PyDateTime_CheckExact}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002909 Return true if \var{ob} is of type \cdata{PyDateTime_DateTimeType}.
2910 \var{ob} must not be \NULL{}.
2911 \versionadded{2.4}
2912\end{cfuncdesc}
2913
Tim Peters183dabc2004-07-11 19:26:19 +00002914\begin{cfuncdesc}{int}{PyTime_Check}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002915 Return true if \var{ob} is of type \cdata{PyDateTime_TimeType} or
2916 a subtype of \cdata{PyDateTime_TimeType}. \var{ob} must not be
2917 \NULL{}.
2918 \versionadded{2.4}
2919\end{cfuncdesc}
2920
Tim Peters183dabc2004-07-11 19:26:19 +00002921\begin{cfuncdesc}{int}{PyTime_CheckExact}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002922 Return true if \var{ob} is of type \cdata{PyDateTime_TimeType}.
2923 \var{ob} must not be \NULL{}.
2924 \versionadded{2.4}
2925\end{cfuncdesc}
2926
Tim Peters183dabc2004-07-11 19:26:19 +00002927\begin{cfuncdesc}{int}{PyDelta_Check}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002928 Return true if \var{ob} is of type \cdata{PyDateTime_DeltaType} or
2929 a subtype of \cdata{PyDateTime_DeltaType}. \var{ob} must not be
2930 \NULL{}.
2931 \versionadded{2.4}
2932\end{cfuncdesc}
2933
Tim Peters183dabc2004-07-11 19:26:19 +00002934\begin{cfuncdesc}{int}{PyDelta_CheckExact}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002935 Return true if \var{ob} is of type \cdata{PyDateTime_DeltaType}.
2936 \var{ob} must not be \NULL{}.
2937 \versionadded{2.4}
2938\end{cfuncdesc}
2939
Tim Peters183dabc2004-07-11 19:26:19 +00002940\begin{cfuncdesc}{int}{PyTZInfo_Check}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002941 Return true if \var{ob} is of type \cdata{PyDateTime_TZInfoType} or
2942 a subtype of \cdata{PyDateTime_TZInfoType}. \var{ob} must not be
2943 \NULL{}.
2944 \versionadded{2.4}
2945\end{cfuncdesc}
2946
Tim Peters183dabc2004-07-11 19:26:19 +00002947\begin{cfuncdesc}{int}{PyTZInfo_CheckExact}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002948 Return true if \var{ob} is of type \cdata{PyDateTime_TZInfoType}.
2949 \var{ob} must not be \NULL{}.
2950 \versionadded{2.4}
2951\end{cfuncdesc}
2952
Tim Peters183dabc2004-07-11 19:26:19 +00002953Macros to create objects:
2954
Tim Peters9ddf40b2004-06-20 22:41:32 +00002955\begin{cfuncdesc}{PyObject*}{PyDate_FromDate}{int year, int month, int day}
2956 Return a \code{datetime.date} object with the specified year, month
2957 and day.
2958 \versionadded{2.4}
2959\end{cfuncdesc}
2960
Brett Cannon5bbe6ad2005-02-17 05:17:17 +00002961\begin{cfuncdesc}{PyObject*}{PyDateTime_FromDateAndTime}{int year, int month,
Tim Peters9ddf40b2004-06-20 22:41:32 +00002962 int day, int hour, int minute, int second, int usecond}
2963 Return a \code{datetime.datetime} object with the specified year, month,
2964 day, hour, minute, second and microsecond.
2965 \versionadded{2.4}
2966\end{cfuncdesc}
2967
2968\begin{cfuncdesc}{PyObject*}{PyTime_FromTime}{int hour, int minute,
2969 int second, int usecond}
2970 Return a \code{datetime.time} object with the specified hour, minute,
2971 second and microsecond.
2972 \versionadded{2.4}
2973\end{cfuncdesc}
2974
2975\begin{cfuncdesc}{PyObject*}{PyDelta_FromDSU}{int days, int seconds,
2976 int useconds}
2977 Return a \code{datetime.timedelta} object representing the given number
2978 of days, seconds and microseconds. Normalization is performed so that
2979 the resulting number of microseconds and seconds lie in the ranges
2980 documented for \code{datetime.timedelta} objects.
2981 \versionadded{2.4}
2982\end{cfuncdesc}
2983
Tim Peters8ff9f9f2004-07-17 01:42:26 +00002984Macros to extract fields from date objects. The argument must be an
Tim Peters183dabc2004-07-11 19:26:19 +00002985instance of \cdata{PyDateTime_Date}, including subclasses (such as
2986\cdata{PyDateTime_DateTime}). The argument must not be \NULL{}, and
2987the type is not checked:
2988
2989\begin{cfuncdesc}{int}{PyDateTime_GET_YEAR}{PyDateTime_Date *o}
2990 Return the year, as a positive int.
2991 \versionadded{2.4}
2992\end{cfuncdesc}
2993
2994\begin{cfuncdesc}{int}{PyDateTime_GET_MONTH}{PyDateTime_Date *o}
2995 Return the month, as an int from 1 through 12.
2996 \versionadded{2.4}
2997\end{cfuncdesc}
2998
2999\begin{cfuncdesc}{int}{PyDateTime_GET_DAY}{PyDateTime_Date *o}
3000 Return the day, as an int from 1 through 31.
3001 \versionadded{2.4}
3002\end{cfuncdesc}
3003
Tim Peters8ff9f9f2004-07-17 01:42:26 +00003004Macros to extract fields from datetime objects. The argument must be an
Tim Peters183dabc2004-07-11 19:26:19 +00003005instance of \cdata{PyDateTime_DateTime}, including subclasses.
3006The argument must not be \NULL{}, and the type is not checked:
3007
3008\begin{cfuncdesc}{int}{PyDateTime_DATE_GET_HOUR}{PyDateTime_DateTime *o}
Neal Norwitz7fdd92f2004-08-02 21:56:33 +00003009 Return the hour, as an int from 0 through 23.
Tim Peters183dabc2004-07-11 19:26:19 +00003010 \versionadded{2.4}
3011\end{cfuncdesc}
3012
3013\begin{cfuncdesc}{int}{PyDateTime_DATE_GET_MINUTE}{PyDateTime_DateTime *o}
3014 Return the minute, as an int from 0 through 59.
3015 \versionadded{2.4}
3016\end{cfuncdesc}
3017
3018\begin{cfuncdesc}{int}{PyDateTime_DATE_GET_SECOND}{PyDateTime_DateTime *o}
3019 Return the second, as an int from 0 through 59.
3020 \versionadded{2.4}
3021\end{cfuncdesc}
3022
3023\begin{cfuncdesc}{int}{PyDateTime_DATE_GET_MICROSECOND}{PyDateTime_DateTime *o}
3024 Return the microsecond, as an int from 0 through 999999.
3025 \versionadded{2.4}
3026\end{cfuncdesc}
3027
Tim Peters8ff9f9f2004-07-17 01:42:26 +00003028Macros to extract fields from time objects. The argument must be an
Tim Peters183dabc2004-07-11 19:26:19 +00003029instance of \cdata{PyDateTime_Time}, including subclasses.
3030The argument must not be \NULL{}, and the type is not checked:
3031
3032\begin{cfuncdesc}{int}{PyDateTime_TIME_GET_HOUR}{PyDateTime_Time *o}
Neal Norwitz7fdd92f2004-08-02 21:56:33 +00003033 Return the hour, as an int from 0 through 23.
Tim Peters183dabc2004-07-11 19:26:19 +00003034 \versionadded{2.4}
3035\end{cfuncdesc}
3036
3037\begin{cfuncdesc}{int}{PyDateTime_TIME_GET_MINUTE}{PyDateTime_Time *o}
3038 Return the minute, as an int from 0 through 59.
3039 \versionadded{2.4}
3040\end{cfuncdesc}
3041
3042\begin{cfuncdesc}{int}{PyDateTime_TIME_GET_SECOND}{PyDateTime_Time *o}
3043 Return the second, as an int from 0 through 59.
3044 \versionadded{2.4}
3045\end{cfuncdesc}
3046
3047\begin{cfuncdesc}{int}{PyDateTime_TIME_GET_MICROSECOND}{PyDateTime_Time *o}
3048 Return the microsecond, as an int from 0 through 999999.
3049 \versionadded{2.4}
3050\end{cfuncdesc}
3051
3052Macros for the convenience of modules implementing the DB API:
3053
Tim Peters9ddf40b2004-06-20 22:41:32 +00003054\begin{cfuncdesc}{PyObject*}{PyDateTime_FromTimestamp}{PyObject *args}
3055 Create and return a new \code{datetime.datetime} object given an argument
3056 tuple suitable for passing to \code{datetime.datetime.fromtimestamp()}.
Tim Peters9ddf40b2004-06-20 22:41:32 +00003057 \versionadded{2.4}
3058\end{cfuncdesc}
3059
3060\begin{cfuncdesc}{PyObject*}{PyDate_FromTimestamp}{PyObject *args}
3061 Create and return a new \code{datetime.date} object given an argument
3062 tuple suitable for passing to \code{datetime.date.fromtimestamp()}.
Tim Peters9ddf40b2004-06-20 22:41:32 +00003063 \versionadded{2.4}
Martin v. Löwise440e472004-06-01 15:22:42 +00003064\end{cfuncdesc}
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003065
3066
3067\subsection{Set Objects \label{setObjects}}
Thomas Wouters477c8d52006-05-27 19:21:47 +00003068\sectionauthor{Raymond D. Hettinger}{python@rcn.com}
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003069
3070\obindex{set}
3071\obindex{frozenset}
3072\versionadded{2.5}
3073
3074This section details the public API for \class{set} and \class{frozenset}
3075objects. Any functionality not listed below is best accessed using the
Raymond Hettinger0c230b92005-08-17 10:05:22 +00003076either the abstract object protocol (including
Thomas Wouters477c8d52006-05-27 19:21:47 +00003077\cfunction{PyObject_CallMethod()}, \cfunction{PyObject_RichCompareBool()},
3078\cfunction{PyObject_Hash()}, \cfunction{PyObject_Repr()},
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003079\cfunction{PyObject_IsTrue()}, \cfunction{PyObject_Print()}, and
Raymond Hettinger0c230b92005-08-17 10:05:22 +00003080\cfunction{PyObject_GetIter()})
3081or the abstract number protocol (including
Thomas Wouters89f507f2006-12-13 04:49:30 +00003082\cfunction{PyNumber_And()}, \cfunction{PyNumber_Subtract()},
Raymond Hettinger0c230b92005-08-17 10:05:22 +00003083\cfunction{PyNumber_Or()}, \cfunction{PyNumber_Xor()},
Thomas Wouters89f507f2006-12-13 04:49:30 +00003084\cfunction{PyNumber_InPlaceAnd()}, \cfunction{PyNumber_InPlaceSubtract()},
Georg Brandlb518d8c2006-02-22 11:46:55 +00003085\cfunction{PyNumber_InPlaceOr()}, and \cfunction{PyNumber_InPlaceXor()}).
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003086
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003087\begin{ctypedesc}{PySetObject}
3088 This subtype of \ctype{PyObject} is used to hold the internal data for
3089 both \class{set} and \class{frozenset} objects. It is like a
3090 \ctype{PyDictObject} in that it is a fixed size for small sets
3091 (much like tuple storage) and will point to a separate, variable sized
3092 block of memory for medium and large sized sets (much like list storage).
3093 None of the fields of this structure should be considered public and
3094 are subject to change. All access should be done through the
Thomas Wouters477c8d52006-05-27 19:21:47 +00003095 documented API rather than by manipulating the values in the structure.
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003096
3097\end{ctypedesc}
3098
3099\begin{cvardesc}{PyTypeObject}{PySet_Type}
3100 This is an instance of \ctype{PyTypeObject} representing the Python
3101 \class{set} type.
3102\end{cvardesc}
3103
3104\begin{cvardesc}{PyTypeObject}{PyFrozenSet_Type}
3105 This is an instance of \ctype{PyTypeObject} representing the Python
3106 \class{frozenset} type.
3107\end{cvardesc}
3108
3109
3110The following type check macros work on pointers to any Python object.
3111Likewise, the constructor functions work with any iterable Python object.
3112
3113\begin{cfuncdesc}{int}{PyAnySet_Check}{PyObject *p}
Thomas Wouters477c8d52006-05-27 19:21:47 +00003114 Return true if \var{p} is a \class{set} object, a \class{frozenset}
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003115 object, or an instance of a subtype.
3116\end{cfuncdesc}
3117
3118\begin{cfuncdesc}{int}{PyAnySet_CheckExact}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00003119 Return true if \var{p} is a \class{set} object or a \class{frozenset}
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003120 object but not an instance of a subtype.
3121\end{cfuncdesc}
3122
3123\begin{cfuncdesc}{int}{PyFrozenSet_CheckExact}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00003124 Return true if \var{p} is a \class{frozenset} object
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003125 but not an instance of a subtype.
3126\end{cfuncdesc}
3127
3128\begin{cfuncdesc}{PyObject*}{PySet_New}{PyObject *iterable}
Georg Brandl99363b62005-09-03 07:27:26 +00003129 Return a new \class{set} containing objects returned by the
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003130 \var{iterable}. The \var{iterable} may be \NULL{} to create a
Georg Brandl99363b62005-09-03 07:27:26 +00003131 new empty set. Return the new set on success or \NULL{} on
3132 failure. Raise \exception{TypeError} if \var{iterable} is
Raymond Hettinger94fedf92005-08-17 12:23:45 +00003133 not actually iterable. The constructor is also useful for
3134 copying a set (\code{c=set(s)}).
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003135\end{cfuncdesc}
3136
3137\begin{cfuncdesc}{PyObject*}{PyFrozenSet_New}{PyObject *iterable}
Georg Brandl99363b62005-09-03 07:27:26 +00003138 Return a new \class{frozenset} containing objects returned by the
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003139 \var{iterable}. The \var{iterable} may be \NULL{} to create a
Georg Brandl99363b62005-09-03 07:27:26 +00003140 new empty frozenset. Return the new set on success or \NULL{} on
3141 failure. Raise \exception{TypeError} if \var{iterable} is
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00003142 not actually iterable.
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003143\end{cfuncdesc}
3144
3145
3146The following functions and macros are available for instances of
3147\class{set} or \class{frozenset} or instances of their subtypes.
3148
3149\begin{cfuncdesc}{int}{PySet_Size}{PyObject *anyset}
Georg Brandl99363b62005-09-03 07:27:26 +00003150 Return the length of a \class{set} or \class{frozenset} object.
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003151 Equivalent to \samp{len(\var{anyset})}. Raises a
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00003152 \exception{PyExc_SystemError} if \var{anyset} is not a \class{set},
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003153 \class{frozenset}, or an instance of a subtype.
3154 \bifuncindex{len}
3155\end{cfuncdesc}
3156
3157\begin{cfuncdesc}{int}{PySet_GET_SIZE}{PyObject *anyset}
3158 Macro form of \cfunction{PySet_Size()} without error checking.
3159\end{cfuncdesc}
3160
3161\begin{cfuncdesc}{int}{PySet_Contains}{PyObject *anyset, PyObject *key}
Georg Brandl99363b62005-09-03 07:27:26 +00003162 Return 1 if found, 0 if not found, and -1 if an error is
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003163 encountered. Unlike the Python \method{__contains__()} method, this
3164 function does not automatically convert unhashable sets into temporary
Georg Brandl99363b62005-09-03 07:27:26 +00003165 frozensets. Raise a \exception{TypeError} if the \var{key} is unhashable.
3166 Raise \exception{PyExc_SystemError} if \var{anyset} is not a \class{set},
Thomas Wouters477c8d52006-05-27 19:21:47 +00003167 \class{frozenset}, or an instance of a subtype.
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003168\end{cfuncdesc}
3169
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003170The following functions are available for instances of \class{set} or
3171its subtypes but not for instances of \class{frozenset} or its subtypes.
3172
3173\begin{cfuncdesc}{int}{PySet_Add}{PyObject *set, PyObject *key}
Georg Brandl99363b62005-09-03 07:27:26 +00003174 Add \var{key} to a \class{set} instance. Does not apply to
3175 \class{frozenset} instances. Return 0 on success or -1 on failure.
3176 Raise a \exception{TypeError} if the \var{key} is unhashable.
3177 Raise a \exception{MemoryError} if there is no room to grow.
3178 Raise a \exception{SystemError} if \var{set} is an not an instance
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003179 of \class{set} or its subtype.
3180\end{cfuncdesc}
3181
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00003182\begin{cfuncdesc}{int}{PySet_Discard}{PyObject *set, PyObject *key}
Georg Brandl99363b62005-09-03 07:27:26 +00003183 Return 1 if found and removed, 0 if not found (no action taken),
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00003184 and -1 if an error is encountered. Does not raise \exception{KeyError}
Georg Brandl99363b62005-09-03 07:27:26 +00003185 for missing keys. Raise a \exception{TypeError} if the \var{key} is
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00003186 unhashable. Unlike the Python \method{discard()} method, this function
3187 does not automatically convert unhashable sets into temporary frozensets.
Georg Brandl99363b62005-09-03 07:27:26 +00003188 Raise \exception{PyExc_SystemError} if \var{set} is an not an instance
Thomas Wouters477c8d52006-05-27 19:21:47 +00003189 of \class{set} or its subtype.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00003190\end{cfuncdesc}
3191
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003192\begin{cfuncdesc}{PyObject*}{PySet_Pop}{PyObject *set}
Georg Brandl99363b62005-09-03 07:27:26 +00003193 Return a new reference to an arbitrary object in the \var{set},
3194 and removes the object from the \var{set}. Return \NULL{} on
3195 failure. Raise \exception{KeyError} if the set is empty.
3196 Raise a \exception{SystemError} if \var{set} is an not an instance
Thomas Wouters477c8d52006-05-27 19:21:47 +00003197 of \class{set} or its subtype.
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003198\end{cfuncdesc}
3199
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003200\begin{cfuncdesc}{int}{PySet_Clear}{PyObject *set}
3201 Empty an existing set of all elements.
3202\end{cfuncdesc}