blob: 10f898ea9478041d0d6969c05989d924eacfbb7a [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
Georg Brandl7572f032006-08-08 20:48:10 +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
Georg Brandl7572f032006-08-08 20:48:10 +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
Georg Brandlb6e92c42006-03-27 22:09:16 +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}
Tim Peters8931ff12006-05-13 23:28:20 +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
Georg Brandl7572f032006-08-08 20:48:10 +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}
Martin v. Löwis0bc2ab92006-04-10 20:28:17 +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 Heller653f23c2006-07-06 15:06:05 +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}
Martin v. Löwis0bc2ab92006-04-10 20:28:17 +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
Georg Brandl7572f032006-08-08 20:48:10 +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
Skip Montanaro1ff49a72003-02-03 05:13:24 +0000433\begin{cfuncdesc}{PyObject*}{PyFloat_FromString}{PyObject *str, char **pend}
Georg Brandl99363b62005-09-03 07:27:26 +0000434 Create a \ctype{PyFloatObject} object based on the string value in
Skip Montanaro1ff49a72003-02-03 05:13:24 +0000435 \var{str}, or \NULL{} on failure. The \var{pend} argument is ignored. It
436 remains only for backward compatibility.
Skip Montanaroae31e9b2003-02-03 03:56:36 +0000437\end{cfuncdesc}
438
Fred Drake3adf79e2001-10-12 19:01:43 +0000439\begin{cfuncdesc}{PyObject*}{PyFloat_FromDouble}{double v}
Georg Brandl99363b62005-09-03 07:27:26 +0000440 Create a \ctype{PyFloatObject} object from \var{v}, or \NULL{} on
Fred Drake3adf79e2001-10-12 19:01:43 +0000441 failure.
442\end{cfuncdesc}
443
444\begin{cfuncdesc}{double}{PyFloat_AsDouble}{PyObject *pyfloat}
Georg Brandl99363b62005-09-03 07:27:26 +0000445 Return a C \ctype{double} representation of the contents of
Georg Brandl2b869942007-03-17 16:08:45 +0000446 \var{pyfloat}. If \var{pyfloat} is not a Python floating point
447 object but has a \method{__float__} method, this method will first
448 be called to convert \var{pyfloat} into a float.
Fred Drake3adf79e2001-10-12 19:01:43 +0000449\end{cfuncdesc}
450
451\begin{cfuncdesc}{double}{PyFloat_AS_DOUBLE}{PyObject *pyfloat}
Georg Brandl99363b62005-09-03 07:27:26 +0000452 Return a C \ctype{double} representation of the contents of
Fred Drake3adf79e2001-10-12 19:01:43 +0000453 \var{pyfloat}, but without error checking.
454\end{cfuncdesc}
455
456
457\subsection{Complex Number Objects \label{complexObjects}}
458
459\obindex{complex number}
460Python's complex number objects are implemented as two distinct types
461when viewed from the C API: one is the Python object exposed to
462Python programs, and the other is a C structure which represents the
463actual complex number value. The API provides functions for working
464with both.
465
466\subsubsection{Complex Numbers as C Structures}
467
468Note that the functions which accept these structures as parameters
469and return them as results do so \emph{by value} rather than
470dereferencing them through pointers. This is consistent throughout
471the API.
472
473\begin{ctypedesc}{Py_complex}
474 The C structure which corresponds to the value portion of a Python
475 complex number object. Most of the functions for dealing with
476 complex number objects use structures of this type as input or
477 output values, as appropriate. It is defined as:
478
479\begin{verbatim}
480typedef struct {
481 double real;
482 double imag;
483} Py_complex;
484\end{verbatim}
485\end{ctypedesc}
486
487\begin{cfuncdesc}{Py_complex}{_Py_c_sum}{Py_complex left, Py_complex right}
488 Return the sum of two complex numbers, using the C
489 \ctype{Py_complex} representation.
490\end{cfuncdesc}
491
492\begin{cfuncdesc}{Py_complex}{_Py_c_diff}{Py_complex left, Py_complex right}
493 Return the difference between two complex numbers, using the C
494 \ctype{Py_complex} representation.
495\end{cfuncdesc}
496
497\begin{cfuncdesc}{Py_complex}{_Py_c_neg}{Py_complex complex}
498 Return the negation of the complex number \var{complex}, using the C
499 \ctype{Py_complex} representation.
500\end{cfuncdesc}
501
502\begin{cfuncdesc}{Py_complex}{_Py_c_prod}{Py_complex left, Py_complex right}
503 Return the product of two complex numbers, using the C
504 \ctype{Py_complex} representation.
505\end{cfuncdesc}
506
507\begin{cfuncdesc}{Py_complex}{_Py_c_quot}{Py_complex dividend,
508 Py_complex divisor}
509 Return the quotient of two complex numbers, using the C
510 \ctype{Py_complex} representation.
511\end{cfuncdesc}
512
513\begin{cfuncdesc}{Py_complex}{_Py_c_pow}{Py_complex num, Py_complex exp}
514 Return the exponentiation of \var{num} by \var{exp}, using the C
515 \ctype{Py_complex} representation.
516\end{cfuncdesc}
517
518
519\subsubsection{Complex Numbers as Python Objects}
520
521\begin{ctypedesc}{PyComplexObject}
522 This subtype of \ctype{PyObject} represents a Python complex number
523 object.
524\end{ctypedesc}
525
526\begin{cvardesc}{PyTypeObject}{PyComplex_Type}
527 This instance of \ctype{PyTypeObject} represents the Python complex
Georg Brandl7572f032006-08-08 20:48:10 +0000528 number type. It is the same object as \code{complex} and
529 \code{types.ComplexType}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000530\end{cvardesc}
531
532\begin{cfuncdesc}{int}{PyComplex_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +0000533 Return true if its argument is a \ctype{PyComplexObject} or a
Fred Drake3adf79e2001-10-12 19:01:43 +0000534 subtype of \ctype{PyComplexObject}.
535 \versionchanged[Allowed subtypes to be accepted]{2.2}
536\end{cfuncdesc}
537
538\begin{cfuncdesc}{int}{PyComplex_CheckExact}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +0000539 Return true if its argument is a \ctype{PyComplexObject}, but not a
Fred Drake3adf79e2001-10-12 19:01:43 +0000540 subtype of \ctype{PyComplexObject}.
541 \versionadded{2.2}
542\end{cfuncdesc}
543
544\begin{cfuncdesc}{PyObject*}{PyComplex_FromCComplex}{Py_complex v}
545 Create a new Python complex number object from a C
546 \ctype{Py_complex} value.
547\end{cfuncdesc}
548
549\begin{cfuncdesc}{PyObject*}{PyComplex_FromDoubles}{double real, double imag}
Georg Brandl99363b62005-09-03 07:27:26 +0000550 Return a new \ctype{PyComplexObject} object from \var{real} and
Fred Drake3adf79e2001-10-12 19:01:43 +0000551 \var{imag}.
552\end{cfuncdesc}
553
554\begin{cfuncdesc}{double}{PyComplex_RealAsDouble}{PyObject *op}
Georg Brandl99363b62005-09-03 07:27:26 +0000555 Return the real part of \var{op} as a C \ctype{double}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000556\end{cfuncdesc}
557
558\begin{cfuncdesc}{double}{PyComplex_ImagAsDouble}{PyObject *op}
Georg Brandl99363b62005-09-03 07:27:26 +0000559 Return the imaginary part of \var{op} as a C \ctype{double}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000560\end{cfuncdesc}
561
562\begin{cfuncdesc}{Py_complex}{PyComplex_AsCComplex}{PyObject *op}
Georg Brandl2b869942007-03-17 16:08:45 +0000563 Return the \ctype{Py_complex} value of the complex number \var{op}.
564 \versionchanged[If \var{op} is not a Python complex number object
565 but has a \method{__complex__} method, this method
566 will first be called to convert \var{op} to a Python
567 complex number object]{2.6}
Fred Drake3adf79e2001-10-12 19:01:43 +0000568\end{cfuncdesc}
569
570
571
572\section{Sequence Objects \label{sequenceObjects}}
573
574\obindex{sequence}
Tim Petersf582b822001-12-11 18:51:08 +0000575Generic operations on sequence objects were discussed in the previous
576chapter; this section deals with the specific kinds of sequence
Fred Drake3adf79e2001-10-12 19:01:43 +0000577objects that are intrinsic to the Python language.
578
579
580\subsection{String Objects \label{stringObjects}}
581
582These functions raise \exception{TypeError} when expecting a string
583parameter and are called with a non-string parameter.
584
585\obindex{string}
586\begin{ctypedesc}{PyStringObject}
587 This subtype of \ctype{PyObject} represents a Python string object.
588\end{ctypedesc}
589
590\begin{cvardesc}{PyTypeObject}{PyString_Type}
591 This instance of \ctype{PyTypeObject} represents the Python string
Georg Brandl7572f032006-08-08 20:48:10 +0000592 type; it is the same object as \code{str} and \code{types.StringType}
593 in the Python layer.
Fred Drake3adf79e2001-10-12 19:01:43 +0000594 \withsubitem{(in module types)}{\ttindex{StringType}}.
595\end{cvardesc}
596
597\begin{cfuncdesc}{int}{PyString_Check}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000598 Return true if the object \var{o} is a string object or an instance
Fred Drake3adf79e2001-10-12 19:01:43 +0000599 of a subtype of the string type.
600 \versionchanged[Allowed subtypes to be accepted]{2.2}
601\end{cfuncdesc}
602
603\begin{cfuncdesc}{int}{PyString_CheckExact}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000604 Return true if the object \var{o} is a string object, but not an
Fred Drake3adf79e2001-10-12 19:01:43 +0000605 instance of a subtype of the string type.
606 \versionadded{2.2}
607\end{cfuncdesc}
608
609\begin{cfuncdesc}{PyObject*}{PyString_FromString}{const char *v}
Georg Brandl44a7b3a2006-09-30 12:02:57 +0000610 Return a new string object with a copy of the string \var{v} as value
611 on success, and \NULL{} on failure. The parameter \var{v} must not be
612 \NULL{}; it will not be checked.
Fred Drake3adf79e2001-10-12 19:01:43 +0000613\end{cfuncdesc}
614
615\begin{cfuncdesc}{PyObject*}{PyString_FromStringAndSize}{const char *v,
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000616 Py_ssize_t len}
Georg Brandl44a7b3a2006-09-30 12:02:57 +0000617 Return a new string object with a copy of the string \var{v} as value
618 and length \var{len} on success, and \NULL{} on failure. If \var{v} is
Tim Peters9ddf40b2004-06-20 22:41:32 +0000619 \NULL{}, the contents of the string are uninitialized.
Fred Drake3adf79e2001-10-12 19:01:43 +0000620\end{cfuncdesc}
621
622\begin{cfuncdesc}{PyObject*}{PyString_FromFormat}{const char *format, ...}
Georg Brandl99363b62005-09-03 07:27:26 +0000623 Take a C \cfunction{printf()}-style \var{format} string and a
624 variable number of arguments, calculate the size of the resulting
625 Python string and return a string with the values formatted into
Fred Drake3adf79e2001-10-12 19:01:43 +0000626 it. The variable arguments must be C types and must correspond
627 exactly to the format characters in the \var{format} string. The
628 following format characters are allowed:
629
Tim Peters8931ff12006-05-13 23:28:20 +0000630 % This should be exactly the same as the table in PyErr_Format.
631 % One should just refer to the other.
632
633 % The descriptions for %zd and %zu are wrong, but the truth is complicated
634 % because not all compilers support the %z width modifier -- we fake it
635 % when necessary via interpolating PY_FORMAT_SIZE_T.
636
637 % %u, %lu, %zu should have "new in Python 2.5" blurbs.
638
Fred Drake3adf79e2001-10-12 19:01:43 +0000639 \begin{tableiii}{l|l|l}{member}{Format Characters}{Type}{Comment}
640 \lineiii{\%\%}{\emph{n/a}}{The literal \% character.}
641 \lineiii{\%c}{int}{A single character, represented as an C int.}
642 \lineiii{\%d}{int}{Exactly equivalent to \code{printf("\%d")}.}
Tim Peters8931ff12006-05-13 23:28:20 +0000643 \lineiii{\%u}{unsigned int}{Exactly equivalent to \code{printf("\%u")}.}
Fred Drake3adf79e2001-10-12 19:01:43 +0000644 \lineiii{\%ld}{long}{Exactly equivalent to \code{printf("\%ld")}.}
Tim Peters8931ff12006-05-13 23:28:20 +0000645 \lineiii{\%lu}{unsigned long}{Exactly equivalent to \code{printf("\%lu")}.}
646 \lineiii{\%zd}{Py_ssize_t}{Exactly equivalent to \code{printf("\%zd")}.}
Tim Peterse6d95062006-05-13 23:31:05 +0000647 \lineiii{\%zu}{size_t}{Exactly equivalent to \code{printf("\%zu")}.}
Fred Drake3adf79e2001-10-12 19:01:43 +0000648 \lineiii{\%i}{int}{Exactly equivalent to \code{printf("\%i")}.}
649 \lineiii{\%x}{int}{Exactly equivalent to \code{printf("\%x")}.}
650 \lineiii{\%s}{char*}{A null-terminated C character array.}
651 \lineiii{\%p}{void*}{The hex representation of a C pointer.
652 Mostly equivalent to \code{printf("\%p")} except that it is
653 guaranteed to start with the literal \code{0x} regardless of
654 what the platform's \code{printf} yields.}
655 \end{tableiii}
Tim Peters8931ff12006-05-13 23:28:20 +0000656
657 An unrecognized format character causes all the rest of the format
658 string to be copied as-is to the result string, and any extra
659 arguments discarded.
Fred Drake3adf79e2001-10-12 19:01:43 +0000660\end{cfuncdesc}
661
662\begin{cfuncdesc}{PyObject*}{PyString_FromFormatV}{const char *format,
663 va_list vargs}
664 Identical to \function{PyString_FromFormat()} except that it takes
665 exactly two arguments.
666\end{cfuncdesc}
667
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000668\begin{cfuncdesc}{Py_ssize_t}{PyString_Size}{PyObject *string}
Georg Brandl99363b62005-09-03 07:27:26 +0000669 Return the length of the string in string object \var{string}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000670\end{cfuncdesc}
671
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000672\begin{cfuncdesc}{Py_ssize_t}{PyString_GET_SIZE}{PyObject *string}
Fred Drake3adf79e2001-10-12 19:01:43 +0000673 Macro form of \cfunction{PyString_Size()} but without error
674 checking.
675\end{cfuncdesc}
676
677\begin{cfuncdesc}{char*}{PyString_AsString}{PyObject *string}
Georg Brandl99363b62005-09-03 07:27:26 +0000678 Return a NUL-terminated representation of the contents of
Fred Drake3adf79e2001-10-12 19:01:43 +0000679 \var{string}. The pointer refers to the internal buffer of
680 \var{string}, not a copy. The data must not be modified in any way,
681 unless the string was just created using
682 \code{PyString_FromStringAndSize(NULL, \var{size})}.
Fred Drake4b247262002-10-22 20:20:20 +0000683 It must not be deallocated. If \var{string} is a Unicode object,
684 this function computes the default encoding of \var{string} and
685 operates on that. If \var{string} is not a string object at all,
686 \cfunction{PyString_AsString()} returns \NULL{} and raises
687 \exception{TypeError}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000688\end{cfuncdesc}
689
690\begin{cfuncdesc}{char*}{PyString_AS_STRING}{PyObject *string}
691 Macro form of \cfunction{PyString_AsString()} but without error
Fred Drake4b247262002-10-22 20:20:20 +0000692 checking. Only string objects are supported; no Unicode objects
693 should be passed.
Fred Drake3adf79e2001-10-12 19:01:43 +0000694\end{cfuncdesc}
695
696\begin{cfuncdesc}{int}{PyString_AsStringAndSize}{PyObject *obj,
697 char **buffer,
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000698 Py_ssize_t *length}
Georg Brandl99363b62005-09-03 07:27:26 +0000699 Return a NUL-terminated representation of the contents of the
Fred Drake3adf79e2001-10-12 19:01:43 +0000700 object \var{obj} through the output variables \var{buffer} and
701 \var{length}.
702
703 The function accepts both string and Unicode objects as input. For
704 Unicode objects it returns the default encoded version of the
Tim Peters9ddf40b2004-06-20 22:41:32 +0000705 object. If \var{length} is \NULL{}, the resulting buffer may not
Fred Drake4b247262002-10-22 20:20:20 +0000706 contain NUL characters; if it does, the function returns \code{-1}
707 and a \exception{TypeError} is raised.
Fred Drake3adf79e2001-10-12 19:01:43 +0000708
709 The buffer refers to an internal string buffer of \var{obj}, not a
710 copy. The data must not be modified in any way, unless the string
711 was just created using \code{PyString_FromStringAndSize(NULL,
Fred Drake4b247262002-10-22 20:20:20 +0000712 \var{size})}. It must not be deallocated. If \var{string} is a
713 Unicode object, this function computes the default encoding of
714 \var{string} and operates on that. If \var{string} is not a string
Tim Peters8931ff12006-05-13 23:28:20 +0000715 object at all, \cfunction{PyString_AsStringAndSize()} returns
Georg Brandle53475d2005-09-28 12:53:12 +0000716 \code{-1} and raises \exception{TypeError}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000717\end{cfuncdesc}
718
719\begin{cfuncdesc}{void}{PyString_Concat}{PyObject **string,
720 PyObject *newpart}
Georg Brandl99363b62005-09-03 07:27:26 +0000721 Create a new string object in \var{*string} containing the contents
Fred Drake3adf79e2001-10-12 19:01:43 +0000722 of \var{newpart} appended to \var{string}; the caller will own the
723 new reference. The reference to the old value of \var{string} will
724 be stolen. If the new string cannot be created, the old reference
725 to \var{string} will still be discarded and the value of
Tim Peters9ddf40b2004-06-20 22:41:32 +0000726 \var{*string} will be set to \NULL{}; the appropriate exception will
Fred Drake3adf79e2001-10-12 19:01:43 +0000727 be set.
728\end{cfuncdesc}
729
730\begin{cfuncdesc}{void}{PyString_ConcatAndDel}{PyObject **string,
731 PyObject *newpart}
Georg Brandl99363b62005-09-03 07:27:26 +0000732 Create a new string object in \var{*string} containing the contents
Fred Drake3adf79e2001-10-12 19:01:43 +0000733 of \var{newpart} appended to \var{string}. This version decrements
734 the reference count of \var{newpart}.
735\end{cfuncdesc}
736
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000737\begin{cfuncdesc}{int}{_PyString_Resize}{PyObject **string, Py_ssize_t newsize}
Fred Drake3adf79e2001-10-12 19:01:43 +0000738 A way to resize a string object even though it is ``immutable''.
739 Only use this to build up a brand new string object; don't use this
Tim Peters5de98422002-04-27 18:44:32 +0000740 if the string may already be known in other parts of the code. It
741 is an error to call this function if the refcount on the input string
742 object is not one.
743 Pass the address of an existing string object as an lvalue (it may
744 be written into), and the new size desired. On success, \var{*string}
Fred Drake432425e2002-04-29 15:17:16 +0000745 holds the resized string object and \code{0} is returned; the address in
Tim Peters5de98422002-04-27 18:44:32 +0000746 \var{*string} may differ from its input value. If the
747 reallocation fails, the original string object at \var{*string} is
748 deallocated, \var{*string} is set to \NULL{}, a memory exception is set,
Fred Drake432425e2002-04-29 15:17:16 +0000749 and \code{-1} is returned.
Fred Drake3adf79e2001-10-12 19:01:43 +0000750\end{cfuncdesc}
751
752\begin{cfuncdesc}{PyObject*}{PyString_Format}{PyObject *format,
753 PyObject *args}
Georg Brandl99363b62005-09-03 07:27:26 +0000754 Return a new string object from \var{format} and \var{args}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000755 Analogous to \code{\var{format} \%\ \var{args}}. The \var{args}
756 argument must be a tuple.
757\end{cfuncdesc}
758
759\begin{cfuncdesc}{void}{PyString_InternInPlace}{PyObject **string}
760 Intern the argument \var{*string} in place. The argument must be
761 the address of a pointer variable pointing to a Python string
762 object. If there is an existing interned string that is the same as
763 \var{*string}, it sets \var{*string} to it (decrementing the
764 reference count of the old string object and incrementing the
765 reference count of the interned string object), otherwise it leaves
766 \var{*string} alone and interns it (incrementing its reference
767 count). (Clarification: even though there is a lot of talk about
768 reference counts, think of this function as reference-count-neutral;
769 you own the object after the call if and only if you owned it before
770 the call.)
771\end{cfuncdesc}
772
773\begin{cfuncdesc}{PyObject*}{PyString_InternFromString}{const char *v}
774 A combination of \cfunction{PyString_FromString()} and
775 \cfunction{PyString_InternInPlace()}, returning either a new string
776 object that has been interned, or a new (``owned'') reference to an
777 earlier interned string object with the same value.
778\end{cfuncdesc}
779
780\begin{cfuncdesc}{PyObject*}{PyString_Decode}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000781 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +0000782 const char *encoding,
783 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +0000784 Create an object by decoding \var{size} bytes of the encoded
Fred Drake3adf79e2001-10-12 19:01:43 +0000785 buffer \var{s} using the codec registered for
786 \var{encoding}. \var{encoding} and \var{errors} have the same
787 meaning as the parameters of the same name in the
788 \function{unicode()} built-in function. The codec to be used is
Georg Brandl99363b62005-09-03 07:27:26 +0000789 looked up using the Python codec registry. Return \NULL{} if
Fred Drake3adf79e2001-10-12 19:01:43 +0000790 an exception was raised by the codec.
791\end{cfuncdesc}
792
793\begin{cfuncdesc}{PyObject*}{PyString_AsDecodedObject}{PyObject *str,
794 const char *encoding,
795 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +0000796 Decode a string object by passing it to the codec registered for
797 \var{encoding} and return the result as Python
Fred Drake3adf79e2001-10-12 19:01:43 +0000798 object. \var{encoding} and \var{errors} have the same meaning as the
799 parameters of the same name in the string \method{encode()} method.
800 The codec to be used is looked up using the Python codec registry.
Georg Brandl99363b62005-09-03 07:27:26 +0000801 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +0000802\end{cfuncdesc}
803
804\begin{cfuncdesc}{PyObject*}{PyString_Encode}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000805 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +0000806 const char *encoding,
807 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +0000808 Encode the \ctype{char} buffer of the given size by passing it to
809 the codec registered for \var{encoding} and return a Python object.
Fred Drake3adf79e2001-10-12 19:01:43 +0000810 \var{encoding} and \var{errors} have the same meaning as the
811 parameters of the same name in the string \method{encode()} method.
812 The codec to be used is looked up using the Python codec
Georg Brandl99363b62005-09-03 07:27:26 +0000813 registry. Return \NULL{} if an exception was raised by the
Fred Drake3adf79e2001-10-12 19:01:43 +0000814 codec.
815\end{cfuncdesc}
816
817\begin{cfuncdesc}{PyObject*}{PyString_AsEncodedObject}{PyObject *str,
818 const char *encoding,
819 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +0000820 Encode a string object using the codec registered for
821 \var{encoding} and return the result as Python object.
Fred Drake3adf79e2001-10-12 19:01:43 +0000822 \var{encoding} and \var{errors} have the same meaning as the
823 parameters of the same name in the string \method{encode()} method.
824 The codec to be used is looked up using the Python codec registry.
Georg Brandl99363b62005-09-03 07:27:26 +0000825 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +0000826\end{cfuncdesc}
827
828
829\subsection{Unicode Objects \label{unicodeObjects}}
830\sectionauthor{Marc-Andre Lemburg}{mal@lemburg.com}
831
832%--- Unicode Type -------------------------------------------------------
833
834These are the basic Unicode object types used for the Unicode
835implementation in Python:
836
837\begin{ctypedesc}{Py_UNICODE}
Marc-André Lemburgdf4f6e92005-10-10 19:08:41 +0000838 This type represents the storage type which is used by Python
839 internally as basis for holding Unicode ordinals. Python's default
840 builds use a 16-bit type for \ctype{Py_UNICODE} and store Unicode
841 values internally as UCS2. It is also possible to build a UCS4
842 version of Python (most recent Linux distributions come with UCS4
843 builds of Python). These builds then use a 32-bit type for
844 \ctype{Py_UNICODE} and store Unicode data internally as UCS4. On
845 platforms where \ctype{wchar_t} is available and compatible with the
846 chosen Python Unicode build variant, \ctype{Py_UNICODE} is a typedef
847 alias for \ctype{wchar_t} to enhance native platform compatibility.
848 On all other platforms, \ctype{Py_UNICODE} is a typedef alias for
849 either \ctype{unsigned short} (UCS2) or \ctype{unsigned long}
850 (UCS4).
Fred Drake3adf79e2001-10-12 19:01:43 +0000851\end{ctypedesc}
852
Marc-André Lemburgdf4f6e92005-10-10 19:08:41 +0000853Note that UCS2 and UCS4 Python builds are not binary compatible.
854Please keep this in mind when writing extensions or interfaces.
855
Fred Drake3adf79e2001-10-12 19:01:43 +0000856\begin{ctypedesc}{PyUnicodeObject}
857 This subtype of \ctype{PyObject} represents a Python Unicode object.
858\end{ctypedesc}
859
860\begin{cvardesc}{PyTypeObject}{PyUnicode_Type}
861 This instance of \ctype{PyTypeObject} represents the Python Unicode
Georg Brandl7572f032006-08-08 20:48:10 +0000862 type. It is exposed to Python code as \code{unicode} and
863 \code{types.UnicodeType}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000864\end{cvardesc}
865
866The following APIs are really C macros and can be used to do fast
867checks and to access internal read-only data of Unicode objects:
868
869\begin{cfuncdesc}{int}{PyUnicode_Check}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000870 Return true if the object \var{o} is a Unicode object or an
Fred Drake3adf79e2001-10-12 19:01:43 +0000871 instance of a Unicode subtype.
872 \versionchanged[Allowed subtypes to be accepted]{2.2}
873\end{cfuncdesc}
874
875\begin{cfuncdesc}{int}{PyUnicode_CheckExact}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000876 Return true if the object \var{o} is a Unicode object, but not an
Fred Drake3adf79e2001-10-12 19:01:43 +0000877 instance of a subtype.
878 \versionadded{2.2}
879\end{cfuncdesc}
880
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000881\begin{cfuncdesc}{Py_ssize_t}{PyUnicode_GET_SIZE}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000882 Return the size of the object. \var{o} has to be a
Fred Drake3adf79e2001-10-12 19:01:43 +0000883 \ctype{PyUnicodeObject} (not checked).
884\end{cfuncdesc}
885
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000886\begin{cfuncdesc}{Py_ssize_t}{PyUnicode_GET_DATA_SIZE}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000887 Return the size of the object's internal buffer in bytes. \var{o}
Fred Drake3adf79e2001-10-12 19:01:43 +0000888 has to be a \ctype{PyUnicodeObject} (not checked).
889\end{cfuncdesc}
890
891\begin{cfuncdesc}{Py_UNICODE*}{PyUnicode_AS_UNICODE}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000892 Return a pointer to the internal \ctype{Py_UNICODE} buffer of the
Fred Drake3adf79e2001-10-12 19:01:43 +0000893 object. \var{o} has to be a \ctype{PyUnicodeObject} (not checked).
894\end{cfuncdesc}
895
896\begin{cfuncdesc}{const char*}{PyUnicode_AS_DATA}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000897 Return a pointer to the internal buffer of the object.
Fred Drake3adf79e2001-10-12 19:01:43 +0000898 \var{o} has to be a \ctype{PyUnicodeObject} (not checked).
899\end{cfuncdesc}
900
901% --- Unicode character properties ---------------------------------------
902
903Unicode provides many different character properties. The most often
904needed ones are available through these macros which are mapped to C
905functions depending on the Python configuration.
906
907\begin{cfuncdesc}{int}{Py_UNICODE_ISSPACE}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000908 Return 1 or 0 depending on whether \var{ch} is a whitespace
Fred Drake3adf79e2001-10-12 19:01:43 +0000909 character.
910\end{cfuncdesc}
911
912\begin{cfuncdesc}{int}{Py_UNICODE_ISLOWER}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000913 Return 1 or 0 depending on whether \var{ch} is a lowercase character.
Fred Drake3adf79e2001-10-12 19:01:43 +0000914\end{cfuncdesc}
915
916\begin{cfuncdesc}{int}{Py_UNICODE_ISUPPER}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000917 Return 1 or 0 depending on whether \var{ch} is an uppercase
Fred Drake3adf79e2001-10-12 19:01:43 +0000918 character.
919\end{cfuncdesc}
920
921\begin{cfuncdesc}{int}{Py_UNICODE_ISTITLE}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000922 Return 1 or 0 depending on whether \var{ch} is a titlecase character.
Fred Drake3adf79e2001-10-12 19:01:43 +0000923\end{cfuncdesc}
924
925\begin{cfuncdesc}{int}{Py_UNICODE_ISLINEBREAK}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000926 Return 1 or 0 depending on whether \var{ch} is a linebreak character.
Fred Drake3adf79e2001-10-12 19:01:43 +0000927\end{cfuncdesc}
928
929\begin{cfuncdesc}{int}{Py_UNICODE_ISDECIMAL}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000930 Return 1 or 0 depending on whether \var{ch} is a decimal character.
Fred Drake3adf79e2001-10-12 19:01:43 +0000931\end{cfuncdesc}
932
933\begin{cfuncdesc}{int}{Py_UNICODE_ISDIGIT}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000934 Return 1 or 0 depending on whether \var{ch} is a digit character.
Fred Drake3adf79e2001-10-12 19:01:43 +0000935\end{cfuncdesc}
936
937\begin{cfuncdesc}{int}{Py_UNICODE_ISNUMERIC}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000938 Return 1 or 0 depending on whether \var{ch} is a numeric character.
Fred Drake3adf79e2001-10-12 19:01:43 +0000939\end{cfuncdesc}
940
941\begin{cfuncdesc}{int}{Py_UNICODE_ISALPHA}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000942 Return 1 or 0 depending on whether \var{ch} is an alphabetic
Fred Drake3adf79e2001-10-12 19:01:43 +0000943 character.
944\end{cfuncdesc}
945
946\begin{cfuncdesc}{int}{Py_UNICODE_ISALNUM}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000947 Return 1 or 0 depending on whether \var{ch} is an alphanumeric
Fred Drake3adf79e2001-10-12 19:01:43 +0000948 character.
949\end{cfuncdesc}
950
951These APIs can be used for fast direct character conversions:
952
953\begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOLOWER}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000954 Return the character \var{ch} converted to lower case.
Fred Drake3adf79e2001-10-12 19:01:43 +0000955\end{cfuncdesc}
956
957\begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOUPPER}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000958 Return the character \var{ch} converted to upper case.
Fred Drake3adf79e2001-10-12 19:01:43 +0000959\end{cfuncdesc}
960
961\begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOTITLE}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000962 Return the character \var{ch} converted to title case.
Fred Drake3adf79e2001-10-12 19:01:43 +0000963\end{cfuncdesc}
964
965\begin{cfuncdesc}{int}{Py_UNICODE_TODECIMAL}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000966 Return the character \var{ch} converted to a decimal positive
967 integer. Return \code{-1} if this is not possible. This macro
968 does not raise exceptions.
Fred Drake3adf79e2001-10-12 19:01:43 +0000969\end{cfuncdesc}
970
971\begin{cfuncdesc}{int}{Py_UNICODE_TODIGIT}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000972 Return the character \var{ch} converted to a single digit integer.
973 Return \code{-1} if this is not possible. This macro does not raise
Fred Drake3adf79e2001-10-12 19:01:43 +0000974 exceptions.
975\end{cfuncdesc}
976
977\begin{cfuncdesc}{double}{Py_UNICODE_TONUMERIC}{Py_UNICODE ch}
Martin v. Löwisd004fc82006-05-27 08:36:52 +0000978 Return the character \var{ch} converted to a double.
Georg Brandl99363b62005-09-03 07:27:26 +0000979 Return \code{-1.0} if this is not possible. This macro does not raise
Fred Drake3adf79e2001-10-12 19:01:43 +0000980 exceptions.
981\end{cfuncdesc}
982
983% --- Plain Py_UNICODE ---------------------------------------------------
984
985To create Unicode objects and access their basic sequence properties,
986use these APIs:
987
988\begin{cfuncdesc}{PyObject*}{PyUnicode_FromUnicode}{const Py_UNICODE *u,
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000989 Py_ssize_t size}
Fred Drake3adf79e2001-10-12 19:01:43 +0000990 Create a Unicode Object from the Py_UNICODE buffer \var{u} of the
991 given size. \var{u} may be \NULL{} which causes the contents to be
992 undefined. It is the user's responsibility to fill in the needed
993 data. The buffer is copied into the new object. If the buffer is
Tim Peters9ddf40b2004-06-20 22:41:32 +0000994 not \NULL{}, the return value might be a shared object. Therefore,
Fred Drake3adf79e2001-10-12 19:01:43 +0000995 modification of the resulting Unicode object is only allowed when
Tim Peters9ddf40b2004-06-20 22:41:32 +0000996 \var{u} is \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000997\end{cfuncdesc}
998
999\begin{cfuncdesc}{Py_UNICODE*}{PyUnicode_AsUnicode}{PyObject *unicode}
1000 Return a read-only pointer to the Unicode object's internal
1001 \ctype{Py_UNICODE} buffer, \NULL{} if \var{unicode} is not a Unicode
1002 object.
1003\end{cfuncdesc}
1004
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001005\begin{cfuncdesc}{Py_ssize_t}{PyUnicode_GetSize}{PyObject *unicode}
Fred Drake3adf79e2001-10-12 19:01:43 +00001006 Return the length of the Unicode object.
1007\end{cfuncdesc}
1008
1009\begin{cfuncdesc}{PyObject*}{PyUnicode_FromEncodedObject}{PyObject *obj,
1010 const char *encoding,
1011 const char *errors}
1012 Coerce an encoded object \var{obj} to an Unicode object and return a
1013 reference with incremented refcount.
Georg Brandl69f61682006-06-14 16:46:43 +00001014
1015 String and other char buffer compatible objects are decoded
1016 according to the given encoding and using the error handling
1017 defined by errors. Both can be \NULL{} to have the interface
1018 use the default values (see the next section for details).
Fred Drake3adf79e2001-10-12 19:01:43 +00001019
Georg Brandl69f61682006-06-14 16:46:43 +00001020 All other objects, including Unicode objects, cause a
1021 \exception{TypeError} to be set.
Fred Drake3adf79e2001-10-12 19:01:43 +00001022
1023 The API returns \NULL{} if there was an error. The caller is
1024 responsible for decref'ing the returned objects.
1025\end{cfuncdesc}
1026
1027\begin{cfuncdesc}{PyObject*}{PyUnicode_FromObject}{PyObject *obj}
1028 Shortcut for \code{PyUnicode_FromEncodedObject(obj, NULL, "strict")}
1029 which is used throughout the interpreter whenever coercion to
1030 Unicode is needed.
1031\end{cfuncdesc}
1032
1033% --- wchar_t support for platforms which support it ---------------------
1034
1035If the platform supports \ctype{wchar_t} and provides a header file
1036wchar.h, Python can interface directly to this type using the
1037following functions. Support is optimized if Python's own
1038\ctype{Py_UNICODE} type is identical to the system's \ctype{wchar_t}.
1039
1040\begin{cfuncdesc}{PyObject*}{PyUnicode_FromWideChar}{const wchar_t *w,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001041 Py_ssize_t size}
Thomas Heller541703b2002-04-29 17:28:43 +00001042 Create a Unicode object from the \ctype{wchar_t} buffer \var{w} of
Georg Brandl99363b62005-09-03 07:27:26 +00001043 the given size. Return \NULL{} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00001044\end{cfuncdesc}
1045
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001046\begin{cfuncdesc}{Py_ssize_t}{PyUnicode_AsWideChar}{PyUnicodeObject *unicode,
Fred Drake3adf79e2001-10-12 19:01:43 +00001047 wchar_t *w,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001048 Py_ssize_t size}
Georg Brandl99363b62005-09-03 07:27:26 +00001049 Copy the Unicode object contents into the \ctype{wchar_t} buffer
Marc-André Lemburga9cadcd2004-11-22 13:02:31 +00001050 \var{w}. At most \var{size} \ctype{wchar_t} characters are copied
Georg Brandl99363b62005-09-03 07:27:26 +00001051 (excluding a possibly trailing 0-termination character). Return
Marc-André Lemburga9cadcd2004-11-22 13:02:31 +00001052 the number of \ctype{wchar_t} characters copied or -1 in case of an
1053 error. Note that the resulting \ctype{wchar_t} string may or may
1054 not be 0-terminated. It is the responsibility of the caller to make
1055 sure that the \ctype{wchar_t} string is 0-terminated in case this is
1056 required by the application.
Fred Drake3adf79e2001-10-12 19:01:43 +00001057\end{cfuncdesc}
1058
1059
1060\subsubsection{Built-in Codecs \label{builtinCodecs}}
1061
1062Python provides a set of builtin codecs which are written in C
1063for speed. All of these codecs are directly usable via the
1064following functions.
1065
1066Many of the following APIs take two arguments encoding and
1067errors. These parameters encoding and errors have the same semantics
1068as the ones of the builtin unicode() Unicode object constructor.
1069
1070Setting encoding to \NULL{} causes the default encoding to be used
1071which is \ASCII. The file system calls should use
1072\cdata{Py_FileSystemDefaultEncoding} as the encoding for file
1073names. This variable should be treated as read-only: On some systems,
1074it will be a pointer to a static string, on others, it will change at
Raymond Hettingercb2da432003-10-12 18:24:34 +00001075run-time (such as when the application invokes setlocale).
Fred Drake3adf79e2001-10-12 19:01:43 +00001076
1077Error handling is set by errors which may also be set to \NULL{}
1078meaning to use the default handling defined for the codec. Default
1079error handling for all builtin codecs is ``strict''
1080(\exception{ValueError} is raised).
1081
1082The codecs all use a similar interface. Only deviation from the
1083following generic ones are documented for simplicity.
1084
1085% --- Generic Codecs -----------------------------------------------------
1086
1087These are the generic codec APIs:
1088
1089\begin{cfuncdesc}{PyObject*}{PyUnicode_Decode}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001090 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001091 const char *encoding,
1092 const char *errors}
1093 Create a Unicode object by decoding \var{size} bytes of the encoded
1094 string \var{s}. \var{encoding} and \var{errors} have the same
1095 meaning as the parameters of the same name in the
1096 \function{unicode()} builtin function. The codec to be used is
Georg Brandl99363b62005-09-03 07:27:26 +00001097 looked up using the Python codec registry. Return \NULL{} if an
Fred Drake3adf79e2001-10-12 19:01:43 +00001098 exception was raised by the codec.
1099\end{cfuncdesc}
1100
1101\begin{cfuncdesc}{PyObject*}{PyUnicode_Encode}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001102 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001103 const char *encoding,
1104 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001105 Encode the \ctype{Py_UNICODE} buffer of the given size and return
Fred Drake3adf79e2001-10-12 19:01:43 +00001106 a Python string object. \var{encoding} and \var{errors} have the
1107 same meaning as the parameters of the same name in the Unicode
1108 \method{encode()} method. The codec to be used is looked up using
Georg Brandl99363b62005-09-03 07:27:26 +00001109 the Python codec registry. Return \NULL{} if an exception was
Fred Drake3adf79e2001-10-12 19:01:43 +00001110 raised by the codec.
1111\end{cfuncdesc}
1112
1113\begin{cfuncdesc}{PyObject*}{PyUnicode_AsEncodedString}{PyObject *unicode,
1114 const char *encoding,
1115 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001116 Encode a Unicode object and return the result as Python string
Fred Drake3adf79e2001-10-12 19:01:43 +00001117 object. \var{encoding} and \var{errors} have the same meaning as the
1118 parameters of the same name in the Unicode \method{encode()} method.
1119 The codec to be used is looked up using the Python codec registry.
Georg Brandl99363b62005-09-03 07:27:26 +00001120 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +00001121\end{cfuncdesc}
1122
1123% --- UTF-8 Codecs -------------------------------------------------------
1124
1125These are the UTF-8 codec APIs:
1126
1127\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF8}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001128 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001129 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001130 Create a Unicode object by decoding \var{size} bytes of the UTF-8
1131 encoded string \var{s}. Return \NULL{} if an exception was raised
Fred Drake3adf79e2001-10-12 19:01:43 +00001132 by the codec.
1133\end{cfuncdesc}
1134
Walter Dörwald69652032004-09-07 20:24:22 +00001135\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF8Stateful}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001136 Py_ssize_t size,
Walter Dörwald69652032004-09-07 20:24:22 +00001137 const char *errors,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001138 Py_ssize_t *consumed}
Georg Brandl99363b62005-09-03 07:27:26 +00001139 If \var{consumed} is \NULL{}, behave like \cfunction{PyUnicode_DecodeUTF8()}.
Walter Dörwald69652032004-09-07 20:24:22 +00001140 If \var{consumed} is not \NULL{}, trailing incomplete UTF-8 byte sequences
1141 will not be treated as an error. Those bytes will not be decoded and the
1142 number of bytes that have been decoded will be stored in \var{consumed}.
1143 \versionadded{2.4}
1144\end{cfuncdesc}
1145
Fred Drake3adf79e2001-10-12 19:01:43 +00001146\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUTF8}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001147 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001148 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001149 Encode the \ctype{Py_UNICODE} buffer of the given size using UTF-8
1150 and return a Python string object. Return \NULL{} if an exception
Fred Drake3adf79e2001-10-12 19:01:43 +00001151 was raised by the codec.
1152\end{cfuncdesc}
1153
1154\begin{cfuncdesc}{PyObject*}{PyUnicode_AsUTF8String}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001155 Encode a Unicode objects using UTF-8 and return the result as
1156 Python string object. Error handling is ``strict''. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00001157 \NULL{} if an exception was raised by the codec.
1158\end{cfuncdesc}
1159
1160% --- UTF-16 Codecs ------------------------------------------------------ */
1161
1162These are the UTF-16 codec APIs:
1163
1164\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF16}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001165 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001166 const char *errors,
1167 int *byteorder}
Georg Brandl99363b62005-09-03 07:27:26 +00001168 Decode \var{length} bytes from a UTF-16 encoded buffer string and
1169 return the corresponding Unicode object. \var{errors} (if
Tim Peters9ddf40b2004-06-20 22:41:32 +00001170 non-\NULL{}) defines the error handling. It defaults to ``strict''.
Fred Drake3adf79e2001-10-12 19:01:43 +00001171
Tim Peters9ddf40b2004-06-20 22:41:32 +00001172 If \var{byteorder} is non-\NULL{}, the decoder starts decoding using
Fred Drake3adf79e2001-10-12 19:01:43 +00001173 the given byte order:
1174
1175\begin{verbatim}
1176 *byteorder == -1: little endian
1177 *byteorder == 0: native order
1178 *byteorder == 1: big endian
1179\end{verbatim}
1180
1181 and then switches according to all byte order marks (BOM) it finds
1182 in the input data. BOMs are not copied into the resulting Unicode
1183 string. After completion, \var{*byteorder} is set to the current
1184 byte order at the end of input data.
1185
Tim Peters9ddf40b2004-06-20 22:41:32 +00001186 If \var{byteorder} is \NULL{}, the codec starts in native order mode.
Fred Drake3adf79e2001-10-12 19:01:43 +00001187
Georg Brandl99363b62005-09-03 07:27:26 +00001188 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +00001189\end{cfuncdesc}
1190
Walter Dörwald69652032004-09-07 20:24:22 +00001191\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF16Stateful}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001192 Py_ssize_t size,
Walter Dörwald69652032004-09-07 20:24:22 +00001193 const char *errors,
1194 int *byteorder,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001195 Py_ssize_t *consumed}
Georg Brandl99363b62005-09-03 07:27:26 +00001196 If \var{consumed} is \NULL{}, behave like
Walter Dörwald69652032004-09-07 20:24:22 +00001197 \cfunction{PyUnicode_DecodeUTF16()}. If \var{consumed} is not \NULL{},
1198 \cfunction{PyUnicode_DecodeUTF16Stateful()} will not treat trailing incomplete
Raymond Hettinger0c230b92005-08-17 10:05:22 +00001199 UTF-16 byte sequences (such as an odd number of bytes or a split surrogate pair)
Walter Dörwald69652032004-09-07 20:24:22 +00001200 as an error. Those bytes will not be decoded and the number of bytes that
1201 have been decoded will be stored in \var{consumed}.
1202 \versionadded{2.4}
1203\end{cfuncdesc}
1204
Fred Drake3adf79e2001-10-12 19:01:43 +00001205\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUTF16}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001206 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001207 const char *errors,
1208 int byteorder}
Georg Brandl99363b62005-09-03 07:27:26 +00001209 Return a Python string object holding the UTF-16 encoded value of
Fred Drake3adf79e2001-10-12 19:01:43 +00001210 the Unicode data in \var{s}. If \var{byteorder} is not \code{0},
1211 output is written according to the following byte order:
1212
1213\begin{verbatim}
1214 byteorder == -1: little endian
1215 byteorder == 0: native byte order (writes a BOM mark)
1216 byteorder == 1: big endian
1217\end{verbatim}
1218
1219 If byteorder is \code{0}, the output string will always start with
1220 the Unicode BOM mark (U+FEFF). In the other two modes, no BOM mark
1221 is prepended.
1222
Martin v. Löwis9bc4f2d2004-06-03 09:55:28 +00001223 If \var{Py_UNICODE_WIDE} is defined, a single \ctype{Py_UNICODE}
1224 value may get represented as a surrogate pair. If it is not
1225 defined, each \ctype{Py_UNICODE} values is interpreted as an
1226 UCS-2 character.
Fred Drake3adf79e2001-10-12 19:01:43 +00001227
Georg Brandl99363b62005-09-03 07:27:26 +00001228 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +00001229\end{cfuncdesc}
1230
1231\begin{cfuncdesc}{PyObject*}{PyUnicode_AsUTF16String}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001232 Return a Python string using the UTF-16 encoding in native byte
Fred Drake3adf79e2001-10-12 19:01:43 +00001233 order. The string always starts with a BOM mark. Error handling is
Georg Brandl99363b62005-09-03 07:27:26 +00001234 ``strict''. Return \NULL{} if an exception was raised by the
Fred Drake3adf79e2001-10-12 19:01:43 +00001235 codec.
1236\end{cfuncdesc}
1237
1238% --- Unicode-Escape Codecs ----------------------------------------------
1239
Martin v. Löwis95cf84a2003-10-19 07:32:24 +00001240These are the ``Unicode Escape'' codec APIs:
Fred Drake3adf79e2001-10-12 19:01:43 +00001241
1242\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUnicodeEscape}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001243 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001244 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001245 Create a Unicode object by decoding \var{size} bytes of the
1246 Unicode-Escape encoded string \var{s}. Return \NULL{} if an
Fred Drake3adf79e2001-10-12 19:01:43 +00001247 exception was raised by the codec.
1248\end{cfuncdesc}
1249
1250\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUnicodeEscape}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001251 Py_ssize_t size}
Georg Brandl99363b62005-09-03 07:27:26 +00001252 Encode the \ctype{Py_UNICODE} buffer of the given size using
1253 Unicode-Escape and return a Python string object. Return \NULL{}
Fred Drake3adf79e2001-10-12 19:01:43 +00001254 if an exception was raised by the codec.
1255\end{cfuncdesc}
1256
1257\begin{cfuncdesc}{PyObject*}{PyUnicode_AsUnicodeEscapeString}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001258 Encode a Unicode objects using Unicode-Escape and return the
Fred Drake3adf79e2001-10-12 19:01:43 +00001259 result as Python string object. Error handling is ``strict''.
Georg Brandl99363b62005-09-03 07:27:26 +00001260 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +00001261\end{cfuncdesc}
1262
1263% --- Raw-Unicode-Escape Codecs ------------------------------------------
1264
Martin v. Löwis95cf84a2003-10-19 07:32:24 +00001265These are the ``Raw Unicode Escape'' codec APIs:
Fred Drake3adf79e2001-10-12 19:01:43 +00001266
1267\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeRawUnicodeEscape}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001268 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001269 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001270 Create a Unicode object by decoding \var{size} bytes of the
1271 Raw-Unicode-Escape encoded string \var{s}. Return \NULL{} if an
Fred Drake3adf79e2001-10-12 19:01:43 +00001272 exception was raised by the codec.
1273\end{cfuncdesc}
1274
1275\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeRawUnicodeEscape}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001276 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001277 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001278 Encode the \ctype{Py_UNICODE} buffer of the given size using
1279 Raw-Unicode-Escape and return a Python string object. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00001280 \NULL{} if an exception was raised by the codec.
1281\end{cfuncdesc}
1282
1283\begin{cfuncdesc}{PyObject*}{PyUnicode_AsRawUnicodeEscapeString}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001284 Encode a Unicode objects using Raw-Unicode-Escape and return the
Fred Drake3adf79e2001-10-12 19:01:43 +00001285 result as Python string object. Error handling is ``strict''.
Georg Brandl99363b62005-09-03 07:27:26 +00001286 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +00001287\end{cfuncdesc}
1288
Tim Petersf582b822001-12-11 18:51:08 +00001289% --- Latin-1 Codecs -----------------------------------------------------
Fred Drake3adf79e2001-10-12 19:01:43 +00001290
1291These are the Latin-1 codec APIs:
1292Latin-1 corresponds to the first 256 Unicode ordinals and only these
1293are accepted by the codecs during encoding.
1294
1295\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeLatin1}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001296 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001297 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001298 Create a Unicode object by decoding \var{size} bytes of the Latin-1
1299 encoded string \var{s}. Return \NULL{} if an exception was raised
Fred Drake3adf79e2001-10-12 19:01:43 +00001300 by the codec.
1301\end{cfuncdesc}
1302
1303\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeLatin1}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001304 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001305 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001306 Encode the \ctype{Py_UNICODE} buffer of the given size using
1307 Latin-1 and return a Python string object. Return \NULL{} if an
Fred Drake3adf79e2001-10-12 19:01:43 +00001308 exception was raised by the codec.
1309\end{cfuncdesc}
1310
1311\begin{cfuncdesc}{PyObject*}{PyUnicode_AsLatin1String}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001312 Encode a Unicode objects using Latin-1 and return the result as
1313 Python string object. Error handling is ``strict''. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00001314 \NULL{} if an exception was raised by the codec.
1315\end{cfuncdesc}
1316
Tim Petersf582b822001-12-11 18:51:08 +00001317% --- ASCII Codecs -------------------------------------------------------
Fred Drake3adf79e2001-10-12 19:01:43 +00001318
1319These are the \ASCII{} codec APIs. Only 7-bit \ASCII{} data is
1320accepted. All other codes generate errors.
1321
1322\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeASCII}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001323 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001324 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001325 Create a Unicode object by decoding \var{size} bytes of the
1326 \ASCII{} encoded string \var{s}. Return \NULL{} if an exception
Fred Drake3adf79e2001-10-12 19:01:43 +00001327 was raised by the codec.
1328\end{cfuncdesc}
1329
1330\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeASCII}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001331 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001332 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001333 Encode the \ctype{Py_UNICODE} buffer of the given size using
1334 \ASCII{} and return a Python string object. Return \NULL{} if an
Fred Drake3adf79e2001-10-12 19:01:43 +00001335 exception was raised by the codec.
1336\end{cfuncdesc}
1337
1338\begin{cfuncdesc}{PyObject*}{PyUnicode_AsASCIIString}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001339 Encode a Unicode objects using \ASCII{} and return the result as
1340 Python string object. Error handling is ``strict''. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00001341 \NULL{} if an exception was raised by the codec.
1342\end{cfuncdesc}
1343
Tim Petersf582b822001-12-11 18:51:08 +00001344% --- Character Map Codecs -----------------------------------------------
Fred Drake3adf79e2001-10-12 19:01:43 +00001345
1346These are the mapping codec APIs:
1347
1348This codec is special in that it can be used to implement many
1349different codecs (and this is in fact what was done to obtain most of
1350the standard codecs included in the \module{encodings} package). The
1351codec uses mapping to encode and decode characters.
1352
1353Decoding mappings must map single string characters to single Unicode
1354characters, integers (which are then interpreted as Unicode ordinals)
Tim Petersf582b822001-12-11 18:51:08 +00001355or None (meaning "undefined mapping" and causing an error).
Fred Drake3adf79e2001-10-12 19:01:43 +00001356
1357Encoding mappings must map single Unicode characters to single string
1358characters, integers (which are then interpreted as Latin-1 ordinals)
1359or None (meaning "undefined mapping" and causing an error).
1360
1361The mapping objects provided must only support the __getitem__ mapping
1362interface.
1363
1364If a character lookup fails with a LookupError, the character is
1365copied as-is meaning that its ordinal value will be interpreted as
1366Unicode or Latin-1 ordinal resp. Because of this, mappings only need
1367to contain those mappings which map characters to different code
1368points.
1369
1370\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeCharmap}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001371 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001372 PyObject *mapping,
1373 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001374 Create a Unicode object by decoding \var{size} bytes of the encoded
1375 string \var{s} using the given \var{mapping} object. Return
Walter Dörwaldd1c1e102005-10-06 20:29:57 +00001376 \NULL{} if an exception was raised by the codec. If \var{mapping} is \NULL{}
1377 latin-1 decoding will be done. Else it can be a dictionary mapping byte or a
1378 unicode string, which is treated as a lookup table. Byte values greater
1379 that the length of the string and U+FFFE "characters" are treated as
1380 "undefined mapping".
1381 \versionchanged[Allowed unicode string as mapping argument]{2.4}
Fred Drake3adf79e2001-10-12 19:01:43 +00001382\end{cfuncdesc}
1383
1384\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeCharmap}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001385 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001386 PyObject *mapping,
1387 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001388 Encode the \ctype{Py_UNICODE} buffer of the given size using the
1389 given \var{mapping} object and return a Python string object.
1390 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +00001391\end{cfuncdesc}
1392
1393\begin{cfuncdesc}{PyObject*}{PyUnicode_AsCharmapString}{PyObject *unicode,
1394 PyObject *mapping}
Georg Brandl99363b62005-09-03 07:27:26 +00001395 Encode a Unicode objects using the given \var{mapping} object and
1396 return the result as Python string object. Error handling is
1397 ``strict''. Return \NULL{} if an exception was raised by the
Fred Drake3adf79e2001-10-12 19:01:43 +00001398 codec.
1399\end{cfuncdesc}
1400
1401The following codec API is special in that maps Unicode to Unicode.
1402
1403\begin{cfuncdesc}{PyObject*}{PyUnicode_TranslateCharmap}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001404 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001405 PyObject *table,
1406 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001407 Translate a \ctype{Py_UNICODE} buffer of the given length by
1408 applying a character mapping \var{table} to it and return the
1409 resulting Unicode object. Return \NULL{} when an exception was
Fred Drake3adf79e2001-10-12 19:01:43 +00001410 raised by the codec.
1411
1412 The \var{mapping} table must map Unicode ordinal integers to Unicode
1413 ordinal integers or None (causing deletion of the character).
1414
George Yoshida9dea97a2006-04-28 16:09:45 +00001415 Mapping tables need only provide the \method{__getitem__()}
Fred Drake3adf79e2001-10-12 19:01:43 +00001416 interface; dictionaries and sequences work well. Unmapped character
1417 ordinals (ones which cause a \exception{LookupError}) are left
1418 untouched and are copied as-is.
1419\end{cfuncdesc}
1420
1421% --- MBCS codecs for Windows --------------------------------------------
1422
1423These are the MBCS codec APIs. They are currently only available on
1424Windows and use the Win32 MBCS converters to implement the
1425conversions. Note that MBCS (or DBCS) is a class of encodings, not
1426just one. The target encoding is defined by the user settings on the
1427machine running the codec.
1428
1429\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeMBCS}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001430 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001431 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001432 Create a Unicode object by decoding \var{size} bytes of the MBCS
1433 encoded string \var{s}. Return \NULL{} if an exception was
Fred Drake3adf79e2001-10-12 19:01:43 +00001434 raised by the codec.
1435\end{cfuncdesc}
1436
Martin v. Löwisd8251432006-06-14 05:21:04 +00001437\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeMBCSStateful}{const char *s,
1438 int size,
1439 const char *errors,
1440 int *consumed}
1441 If \var{consumed} is \NULL{}, behave like
1442 \cfunction{PyUnicode_DecodeMBCS()}. If \var{consumed} is not \NULL{},
1443 \cfunction{PyUnicode_DecodeMBCSStateful()} will not decode trailing lead
1444 byte and the number of bytes that have been decoded will be stored in
1445 \var{consumed}.
1446 \versionadded{2.5}
1447\end{cfuncdesc}
1448
Fred Drake3adf79e2001-10-12 19:01:43 +00001449\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeMBCS}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001450 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001451 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001452 Encode the \ctype{Py_UNICODE} buffer of the given size using MBCS
1453 and return a Python string object. Return \NULL{} if an exception
Fred Drake3adf79e2001-10-12 19:01:43 +00001454 was raised by the codec.
1455\end{cfuncdesc}
1456
1457\begin{cfuncdesc}{PyObject*}{PyUnicode_AsMBCSString}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001458 Encode a Unicode objects using MBCS and return the result as
1459 Python string object. Error handling is ``strict''. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00001460 \NULL{} if an exception was raised by the codec.
1461\end{cfuncdesc}
1462
1463% --- Methods & Slots ----------------------------------------------------
1464
1465\subsubsection{Methods and Slot Functions \label{unicodeMethodsAndSlots}}
1466
1467The following APIs are capable of handling Unicode objects and strings
1468on input (we refer to them as strings in the descriptions) and return
Martin v. Löwis95cf84a2003-10-19 07:32:24 +00001469Unicode objects or integers as appropriate.
Fred Drake3adf79e2001-10-12 19:01:43 +00001470
1471They all return \NULL{} or \code{-1} if an exception occurs.
1472
1473\begin{cfuncdesc}{PyObject*}{PyUnicode_Concat}{PyObject *left,
1474 PyObject *right}
1475 Concat two strings giving a new Unicode string.
1476\end{cfuncdesc}
1477
1478\begin{cfuncdesc}{PyObject*}{PyUnicode_Split}{PyObject *s,
1479 PyObject *sep,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001480 Py_ssize_t maxsplit}
Tim Peters9ddf40b2004-06-20 22:41:32 +00001481 Split a string giving a list of Unicode strings. If sep is \NULL{},
Fred Drake3adf79e2001-10-12 19:01:43 +00001482 splitting will be done at all whitespace substrings. Otherwise,
1483 splits occur at the given separator. At most \var{maxsplit} splits
1484 will be done. If negative, no limit is set. Separators are not
1485 included in the resulting list.
1486\end{cfuncdesc}
1487
1488\begin{cfuncdesc}{PyObject*}{PyUnicode_Splitlines}{PyObject *s,
Martin v. Löwis24b88812003-03-30 16:40:42 +00001489 int keepend}
Fred Drake3adf79e2001-10-12 19:01:43 +00001490 Split a Unicode string at line breaks, returning a list of Unicode
Martin v. Löwis24b88812003-03-30 16:40:42 +00001491 strings. CRLF is considered to be one line break. If \var{keepend}
1492 is 0, the Line break characters are not included in the resulting
1493 strings.
Fred Drake3adf79e2001-10-12 19:01:43 +00001494\end{cfuncdesc}
1495
1496\begin{cfuncdesc}{PyObject*}{PyUnicode_Translate}{PyObject *str,
1497 PyObject *table,
1498 const char *errors}
1499 Translate a string by applying a character mapping table to it and
1500 return the resulting Unicode object.
1501
1502 The mapping table must map Unicode ordinal integers to Unicode
1503 ordinal integers or None (causing deletion of the character).
1504
1505 Mapping tables need only provide the \method{__getitem__()}
1506 interface; dictionaries and sequences work well. Unmapped character
1507 ordinals (ones which cause a \exception{LookupError}) are left
1508 untouched and are copied as-is.
1509
1510 \var{errors} has the usual meaning for codecs. It may be \NULL{}
1511 which indicates to use the default error handling.
1512\end{cfuncdesc}
1513
1514\begin{cfuncdesc}{PyObject*}{PyUnicode_Join}{PyObject *separator,
1515 PyObject *seq}
1516 Join a sequence of strings using the given separator and return the
1517 resulting Unicode string.
1518\end{cfuncdesc}
1519
Raymond Hettinger8ef9b3e2004-12-10 17:12:32 +00001520\begin{cfuncdesc}{int}{PyUnicode_Tailmatch}{PyObject *str,
Fred Drake3adf79e2001-10-12 19:01:43 +00001521 PyObject *substr,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001522 Py_ssize_t start,
1523 Py_ssize_t end,
Fred Drake3adf79e2001-10-12 19:01:43 +00001524 int direction}
1525 Return 1 if \var{substr} matches \var{str}[\var{start}:\var{end}] at
1526 the given tail end (\var{direction} == -1 means to do a prefix
1527 match, \var{direction} == 1 a suffix match), 0 otherwise.
Tim Peters8931ff12006-05-13 23:28:20 +00001528 Return \code{-1} if an error occurred.
Fred Drake3adf79e2001-10-12 19:01:43 +00001529\end{cfuncdesc}
1530
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001531\begin{cfuncdesc}{Py_ssize_t}{PyUnicode_Find}{PyObject *str,
Fred Drake1d1e1db2002-06-20 22:07:04 +00001532 PyObject *substr,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001533 Py_ssize_t start,
1534 Py_ssize_t end,
Fred Drake1d1e1db2002-06-20 22:07:04 +00001535 int direction}
Fred Drake3adf79e2001-10-12 19:01:43 +00001536 Return the first position of \var{substr} in
1537 \var{str}[\var{start}:\var{end}] using the given \var{direction}
1538 (\var{direction} == 1 means to do a forward search,
Fred Drake1d1e1db2002-06-20 22:07:04 +00001539 \var{direction} == -1 a backward search). The return value is the
1540 index of the first match; a value of \code{-1} indicates that no
1541 match was found, and \code{-2} indicates that an error occurred and
1542 an exception has been set.
Fred Drake3adf79e2001-10-12 19:01:43 +00001543\end{cfuncdesc}
1544
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001545\begin{cfuncdesc}{Py_ssize_t}{PyUnicode_Count}{PyObject *str,
Fred Drake1d1e1db2002-06-20 22:07:04 +00001546 PyObject *substr,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001547 Py_ssize_t start,
1548 Py_ssize_t end}
Fred Drake1d1e1db2002-06-20 22:07:04 +00001549 Return the number of non-overlapping occurrences of \var{substr} in
Georg Brandl99363b62005-09-03 07:27:26 +00001550 \code{\var{str}[\var{start}:\var{end}]}. Return \code{-1} if an
Fred Drake1d1e1db2002-06-20 22:07:04 +00001551 error occurred.
Fred Drake3adf79e2001-10-12 19:01:43 +00001552\end{cfuncdesc}
1553
1554\begin{cfuncdesc}{PyObject*}{PyUnicode_Replace}{PyObject *str,
1555 PyObject *substr,
1556 PyObject *replstr,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001557 Py_ssize_t maxcount}
Fred Drake3adf79e2001-10-12 19:01:43 +00001558 Replace at most \var{maxcount} occurrences of \var{substr} in
1559 \var{str} with \var{replstr} and return the resulting Unicode object.
1560 \var{maxcount} == -1 means replace all occurrences.
1561\end{cfuncdesc}
1562
1563\begin{cfuncdesc}{int}{PyUnicode_Compare}{PyObject *left, PyObject *right}
1564 Compare two strings and return -1, 0, 1 for less than, equal, and
1565 greater than, respectively.
1566\end{cfuncdesc}
1567
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00001568\begin{cfuncdesc}{int}{PyUnicode_RichCompare}{PyObject *left,
1569 PyObject *right,
1570 int op}
1571
Georg Brandl4873fb22006-08-14 12:36:06 +00001572 Rich compare two unicode strings and return one of the following:
1573 \begin{itemize}
1574 \item \code{NULL} in case an exception was raised
1575 \item \constant{Py_True} or \constant{Py_False} for successful comparisons
1576 \item \constant{Py_NotImplemented} in case the type combination is unknown
1577 \end{itemize}
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00001578
Georg Brandl4873fb22006-08-14 12:36:06 +00001579 Note that \constant{Py_EQ} and \constant{Py_NE} comparisons can cause a
1580 \exception{UnicodeWarning} in case the conversion of the arguments to
1581 Unicode fails with a \exception{UnicodeDecodeError}.
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00001582
Georg Brandl4873fb22006-08-14 12:36:06 +00001583 Possible values for \var{op} are
1584 \constant{Py_GT}, \constant{Py_GE}, \constant{Py_EQ},
1585 \constant{Py_NE}, \constant{Py_LT}, and \constant{Py_LE}.
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00001586\end{cfuncdesc}
1587
Fred Drake3adf79e2001-10-12 19:01:43 +00001588\begin{cfuncdesc}{PyObject*}{PyUnicode_Format}{PyObject *format,
1589 PyObject *args}
Georg Brandl99363b62005-09-03 07:27:26 +00001590 Return a new string object from \var{format} and \var{args}; this
Fred Drake3adf79e2001-10-12 19:01:43 +00001591 is analogous to \code{\var{format} \%\ \var{args}}. The
1592 \var{args} argument must be a tuple.
1593\end{cfuncdesc}
1594
1595\begin{cfuncdesc}{int}{PyUnicode_Contains}{PyObject *container,
1596 PyObject *element}
Georg Brandl99363b62005-09-03 07:27:26 +00001597 Check whether \var{element} is contained in \var{container} and
1598 return true or false accordingly.
Fred Drake3adf79e2001-10-12 19:01:43 +00001599
1600 \var{element} has to coerce to a one element Unicode
1601 string. \code{-1} is returned if there was an error.
1602\end{cfuncdesc}
1603
1604
1605\subsection{Buffer Objects \label{bufferObjects}}
1606\sectionauthor{Greg Stein}{gstein@lyra.org}
1607
1608\obindex{buffer}
1609Python objects implemented in C can export a group of functions called
1610the ``buffer\index{buffer interface} interface.'' These functions can
1611be used by an object to expose its data in a raw, byte-oriented
1612format. Clients of the object can use the buffer interface to access
1613the object data directly, without needing to copy it first.
1614
Tim Petersf582b822001-12-11 18:51:08 +00001615Two examples of objects that support
1616the buffer interface are strings and arrays. The string object exposes
Fred Drake3adf79e2001-10-12 19:01:43 +00001617the character contents in the buffer interface's byte-oriented
1618form. An array can also expose its contents, but it should be noted
1619that array elements may be multi-byte values.
1620
1621An example user of the buffer interface is the file object's
1622\method{write()} method. Any object that can export a series of bytes
1623through the buffer interface can be written to a file. There are a
Tim Petersf582b822001-12-11 18:51:08 +00001624number of format codes to \cfunction{PyArg_ParseTuple()} that operate
Fred Drake3adf79e2001-10-12 19:01:43 +00001625against an object's buffer interface, returning data from the target
1626object.
1627
1628More information on the buffer interface is provided in the section
Fred Drake54e62942001-12-11 19:40:16 +00001629``Buffer Object Structures'' (section~\ref{buffer-structs}), under
Fred Drake3adf79e2001-10-12 19:01:43 +00001630the description for \ctype{PyBufferProcs}\ttindex{PyBufferProcs}.
1631
1632A ``buffer object'' is defined in the \file{bufferobject.h} header
1633(included by \file{Python.h}). These objects look very similar to
1634string objects at the Python programming level: they support slicing,
1635indexing, concatenation, and some other standard string
1636operations. However, their data can come from one of two sources: from
1637a block of memory, or from another object which exports the buffer
1638interface.
1639
1640Buffer objects are useful as a way to expose the data from another
1641object's buffer interface to the Python programmer. They can also be
1642used as a zero-copy slicing mechanism. Using their ability to
1643reference a block of memory, it is possible to expose any data to the
1644Python programmer quite easily. The memory could be a large, constant
1645array in a C extension, it could be a raw block of memory for
1646manipulation before passing to an operating system library, or it
1647could be used to pass around structured data in its native, in-memory
1648format.
1649
1650\begin{ctypedesc}{PyBufferObject}
1651 This subtype of \ctype{PyObject} represents a buffer object.
1652\end{ctypedesc}
1653
1654\begin{cvardesc}{PyTypeObject}{PyBuffer_Type}
1655 The instance of \ctype{PyTypeObject} which represents the Python
Georg Brandl7572f032006-08-08 20:48:10 +00001656 buffer type; it is the same object as \code{buffer} and
1657 \code{types.BufferType} in the Python layer.
1658 \withsubitem{(in module types)}{\ttindex{BufferType}}.
Fred Drake3adf79e2001-10-12 19:01:43 +00001659\end{cvardesc}
1660
1661\begin{cvardesc}{int}{Py_END_OF_BUFFER}
1662 This constant may be passed as the \var{size} parameter to
1663 \cfunction{PyBuffer_FromObject()} or
1664 \cfunction{PyBuffer_FromReadWriteObject()}. It indicates that the
1665 new \ctype{PyBufferObject} should refer to \var{base} object from
1666 the specified \var{offset} to the end of its exported buffer. Using
1667 this enables the caller to avoid querying the \var{base} object for
1668 its length.
1669\end{cvardesc}
1670
1671\begin{cfuncdesc}{int}{PyBuffer_Check}{PyObject *p}
1672 Return true if the argument has type \cdata{PyBuffer_Type}.
1673\end{cfuncdesc}
1674
1675\begin{cfuncdesc}{PyObject*}{PyBuffer_FromObject}{PyObject *base,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001676 Py_ssize_t offset, Py_ssize_t size}
Fred Drake3adf79e2001-10-12 19:01:43 +00001677 Return a new read-only buffer object. This raises
1678 \exception{TypeError} if \var{base} doesn't support the read-only
1679 buffer protocol or doesn't provide exactly one buffer segment, or it
1680 raises \exception{ValueError} if \var{offset} is less than zero. The
1681 buffer will hold a reference to the \var{base} object, and the
1682 buffer's contents will refer to the \var{base} object's buffer
1683 interface, starting as position \var{offset} and extending for
1684 \var{size} bytes. If \var{size} is \constant{Py_END_OF_BUFFER}, then
1685 the new buffer's contents extend to the length of the \var{base}
1686 object's exported buffer data.
1687\end{cfuncdesc}
1688
1689\begin{cfuncdesc}{PyObject*}{PyBuffer_FromReadWriteObject}{PyObject *base,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001690 Py_ssize_t offset,
1691 Py_ssize_t size}
Fred Drake3adf79e2001-10-12 19:01:43 +00001692 Return a new writable buffer object. Parameters and exceptions are
1693 similar to those for \cfunction{PyBuffer_FromObject()}. If the
1694 \var{base} object does not export the writeable buffer protocol,
1695 then \exception{TypeError} is raised.
1696\end{cfuncdesc}
1697
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001698\begin{cfuncdesc}{PyObject*}{PyBuffer_FromMemory}{void *ptr, Py_ssize_t size}
Fred Drake3adf79e2001-10-12 19:01:43 +00001699 Return a new read-only buffer object that reads from a specified
1700 location in memory, with a specified size. The caller is
1701 responsible for ensuring that the memory buffer, passed in as
1702 \var{ptr}, is not deallocated while the returned buffer object
1703 exists. Raises \exception{ValueError} if \var{size} is less than
1704 zero. Note that \constant{Py_END_OF_BUFFER} may \emph{not} be
1705 passed for the \var{size} parameter; \exception{ValueError} will be
1706 raised in that case.
1707\end{cfuncdesc}
1708
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001709\begin{cfuncdesc}{PyObject*}{PyBuffer_FromReadWriteMemory}{void *ptr, Py_ssize_t size}
Fred Drake3adf79e2001-10-12 19:01:43 +00001710 Similar to \cfunction{PyBuffer_FromMemory()}, but the returned
1711 buffer is writable.
1712\end{cfuncdesc}
1713
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001714\begin{cfuncdesc}{PyObject*}{PyBuffer_New}{Py_ssize_t size}
Georg Brandl99363b62005-09-03 07:27:26 +00001715 Return a new writable buffer object that maintains its own memory
Fred Drake3adf79e2001-10-12 19:01:43 +00001716 buffer of \var{size} bytes. \exception{ValueError} is returned if
Neil Schemenauerd68d3ee2004-06-08 02:58:50 +00001717 \var{size} is not zero or positive. Note that the memory buffer (as
1718 returned by \cfunction{PyObject_AsWriteBuffer()}) is not specifically
1719 aligned.
Fred Drake3adf79e2001-10-12 19:01:43 +00001720\end{cfuncdesc}
1721
1722
1723\subsection{Tuple Objects \label{tupleObjects}}
1724
1725\obindex{tuple}
1726\begin{ctypedesc}{PyTupleObject}
1727 This subtype of \ctype{PyObject} represents a Python tuple object.
1728\end{ctypedesc}
1729
1730\begin{cvardesc}{PyTypeObject}{PyTuple_Type}
1731 This instance of \ctype{PyTypeObject} represents the Python tuple
Georg Brandl7572f032006-08-08 20:48:10 +00001732 type; it is the same object as \code{tuple} and \code{types.TupleType}
1733 in the Python layer.\withsubitem{(in module types)}{\ttindex{TupleType}}.
Fred Drake3adf79e2001-10-12 19:01:43 +00001734\end{cvardesc}
1735
1736\begin{cfuncdesc}{int}{PyTuple_Check}{PyObject *p}
1737 Return true if \var{p} is a tuple object or an instance of a subtype
1738 of the tuple type.
1739 \versionchanged[Allowed subtypes to be accepted]{2.2}
1740\end{cfuncdesc}
1741
1742\begin{cfuncdesc}{int}{PyTuple_CheckExact}{PyObject *p}
1743 Return true if \var{p} is a tuple object, but not an instance of a
1744 subtype of the tuple type.
1745 \versionadded{2.2}
1746\end{cfuncdesc}
1747
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001748\begin{cfuncdesc}{PyObject*}{PyTuple_New}{Py_ssize_t len}
Fred Drake3adf79e2001-10-12 19:01:43 +00001749 Return a new tuple object of size \var{len}, or \NULL{} on failure.
1750\end{cfuncdesc}
1751
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001752\begin{cfuncdesc}{PyObject*}{PyTuple_Pack}{Py_ssize_t n, \moreargs}
Raymond Hettingercb2da432003-10-12 18:24:34 +00001753 Return a new tuple object of size \var{n}, or \NULL{} on failure.
1754 The tuple values are initialized to the subsequent \var{n} C arguments
1755 pointing to Python objects. \samp{PyTuple_Pack(2, \var{a}, \var{b})}
1756 is equivalent to \samp{Py_BuildValue("(OO)", \var{a}, \var{b})}.
Tim Peters9ddf40b2004-06-20 22:41:32 +00001757 \versionadded{2.4}
Raymond Hettingercb2da432003-10-12 18:24:34 +00001758\end{cfuncdesc}
1759
Fred Drake3adf79e2001-10-12 19:01:43 +00001760\begin{cfuncdesc}{int}{PyTuple_Size}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00001761 Take a pointer to a tuple object, and return the size of that
Fred Drake3adf79e2001-10-12 19:01:43 +00001762 tuple.
1763\end{cfuncdesc}
1764
1765\begin{cfuncdesc}{int}{PyTuple_GET_SIZE}{PyObject *p}
1766 Return the size of the tuple \var{p}, which must be non-\NULL{} and
1767 point to a tuple; no error checking is performed.
1768\end{cfuncdesc}
1769
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001770\begin{cfuncdesc}{PyObject*}{PyTuple_GetItem}{PyObject *p, Py_ssize_t pos}
Georg Brandl99363b62005-09-03 07:27:26 +00001771 Return the object at position \var{pos} in the tuple pointed to by
1772 \var{p}. If \var{pos} is out of bounds, return \NULL{} and sets an
Fred Drake3adf79e2001-10-12 19:01:43 +00001773 \exception{IndexError} exception.
1774\end{cfuncdesc}
1775
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001776\begin{cfuncdesc}{PyObject*}{PyTuple_GET_ITEM}{PyObject *p, Py_ssize_t pos}
Fred Drake3adf79e2001-10-12 19:01:43 +00001777 Like \cfunction{PyTuple_GetItem()}, but does no checking of its
1778 arguments.
1779\end{cfuncdesc}
1780
1781\begin{cfuncdesc}{PyObject*}{PyTuple_GetSlice}{PyObject *p,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001782 Py_ssize_t low, Py_ssize_t high}
Georg Brandl99363b62005-09-03 07:27:26 +00001783 Take a slice of the tuple pointed to by \var{p} from \var{low} to
1784 \var{high} and return it as a new tuple.
Fred Drake3adf79e2001-10-12 19:01:43 +00001785\end{cfuncdesc}
1786
1787\begin{cfuncdesc}{int}{PyTuple_SetItem}{PyObject *p,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001788 Py_ssize_t pos, PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +00001789 Insert a reference to object \var{o} at position \var{pos} of the
1790 tuple pointed to by \var{p}. Return \code{0} on success.
Fred Drake3adf79e2001-10-12 19:01:43 +00001791 \note{This function ``steals'' a reference to \var{o}.}
1792\end{cfuncdesc}
1793
1794\begin{cfuncdesc}{void}{PyTuple_SET_ITEM}{PyObject *p,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001795 Py_ssize_t pos, PyObject *o}
Fred Drake3adf79e2001-10-12 19:01:43 +00001796 Like \cfunction{PyTuple_SetItem()}, but does no error checking, and
1797 should \emph{only} be used to fill in brand new tuples. \note{This
1798 function ``steals'' a reference to \var{o}.}
1799\end{cfuncdesc}
1800
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001801\begin{cfuncdesc}{int}{_PyTuple_Resize}{PyObject **p, Py_ssize_t newsize}
Fred Drake3adf79e2001-10-12 19:01:43 +00001802 Can be used to resize a tuple. \var{newsize} will be the new length
1803 of the tuple. Because tuples are \emph{supposed} to be immutable,
1804 this should only be used if there is only one reference to the
1805 object. Do \emph{not} use this if the tuple may already be known to
1806 some other part of the code. The tuple will always grow or shrink
1807 at the end. Think of this as destroying the old tuple and creating
1808 a new one, only more efficiently. Returns \code{0} on success.
1809 Client code should never assume that the resulting value of
1810 \code{*\var{p}} will be the same as before calling this function.
1811 If the object referenced by \code{*\var{p}} is replaced, the
1812 original \code{*\var{p}} is destroyed. On failure, returns
Tim Peters9ddf40b2004-06-20 22:41:32 +00001813 \code{-1} and sets \code{*\var{p}} to \NULL{}, and raises
Fred Drake3adf79e2001-10-12 19:01:43 +00001814 \exception{MemoryError} or
1815 \exception{SystemError}.
Tim Petersf582b822001-12-11 18:51:08 +00001816 \versionchanged[Removed unused third parameter, \var{last_is_sticky}]{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +00001817\end{cfuncdesc}
1818
1819
1820\subsection{List Objects \label{listObjects}}
1821
1822\obindex{list}
1823\begin{ctypedesc}{PyListObject}
1824 This subtype of \ctype{PyObject} represents a Python list object.
1825\end{ctypedesc}
1826
1827\begin{cvardesc}{PyTypeObject}{PyList_Type}
1828 This instance of \ctype{PyTypeObject} represents the Python list
Georg Brandl7572f032006-08-08 20:48:10 +00001829 type. This is the same object as \code{list} and \code{types.ListType}
1830 in the Python layer.\withsubitem{(in module types)}{\ttindex{ListType}}
Fred Drake3adf79e2001-10-12 19:01:43 +00001831\end{cvardesc}
1832
1833\begin{cfuncdesc}{int}{PyList_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00001834 Return true if \var{p} is a list object or an instance of a
Andrew MacIntyre13cd8892003-12-25 23:57:52 +00001835 subtype of the list type.
1836 \versionchanged[Allowed subtypes to be accepted]{2.2}
1837\end{cfuncdesc}
1838
1839\begin{cfuncdesc}{int}{PyList_CheckExact}{PyObject *p}
1840 Return true if \var{p} is a list object, but not an instance of a
1841 subtype of the list type.
1842 \versionadded{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +00001843\end{cfuncdesc}
1844
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001845\begin{cfuncdesc}{PyObject*}{PyList_New}{Py_ssize_t len}
Georg Brandl99363b62005-09-03 07:27:26 +00001846 Return a new list of length \var{len} on success, or \NULL{} on
Fred Drake3adf79e2001-10-12 19:01:43 +00001847 failure.
Georg Brandl648c1102006-08-18 07:27:59 +00001848 \note{If \var{length} is greater than zero, the returned list object's
1849 items are set to \code{NULL}. Thus you cannot use abstract
Andrew M. Kuchlingc4584332006-08-18 13:57:13 +00001850 API functions such as \cfunction{PySequence_SetItem()}
1851 or expose the object to Python code before setting all items to a
Georg Brandl648c1102006-08-18 07:27:59 +00001852 real object with \cfunction{PyList_SetItem()}.}
Fred Drake3adf79e2001-10-12 19:01:43 +00001853\end{cfuncdesc}
1854
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001855\begin{cfuncdesc}{Py_ssize_t}{PyList_Size}{PyObject *list}
Georg Brandl99363b62005-09-03 07:27:26 +00001856 Return the length of the list object in \var{list}; this is
Fred Drake3adf79e2001-10-12 19:01:43 +00001857 equivalent to \samp{len(\var{list})} on a list object.
1858 \bifuncindex{len}
1859\end{cfuncdesc}
1860
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001861\begin{cfuncdesc}{Py_ssize_t}{PyList_GET_SIZE}{PyObject *list}
Fred Drake3adf79e2001-10-12 19:01:43 +00001862 Macro form of \cfunction{PyList_Size()} without error checking.
1863\end{cfuncdesc}
1864
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001865\begin{cfuncdesc}{PyObject*}{PyList_GetItem}{PyObject *list, Py_ssize_t index}
Georg Brandl99363b62005-09-03 07:27:26 +00001866 Return the object at position \var{pos} in the list pointed to by
Georg Brandl4dce8e42006-04-06 12:45:51 +00001867 \var{p}. The position must be positive, indexing from the end of the
1868 list is not supported. If \var{pos} is out of bounds, return \NULL{}
1869 and set an \exception{IndexError} exception.
Fred Drake3adf79e2001-10-12 19:01:43 +00001870\end{cfuncdesc}
1871
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001872\begin{cfuncdesc}{PyObject*}{PyList_GET_ITEM}{PyObject *list, Py_ssize_t i}
Fred Drake3adf79e2001-10-12 19:01:43 +00001873 Macro form of \cfunction{PyList_GetItem()} without error checking.
1874\end{cfuncdesc}
1875
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001876\begin{cfuncdesc}{int}{PyList_SetItem}{PyObject *list, Py_ssize_t index,
Fred Drake3adf79e2001-10-12 19:01:43 +00001877 PyObject *item}
Georg Brandl99363b62005-09-03 07:27:26 +00001878 Set the item at index \var{index} in list to \var{item}. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00001879 \code{0} on success or \code{-1} on failure. \note{This function
1880 ``steals'' a reference to \var{item} and discards a reference to an
1881 item already in the list at the affected position.}
1882\end{cfuncdesc}
1883
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001884\begin{cfuncdesc}{void}{PyList_SET_ITEM}{PyObject *list, Py_ssize_t i,
Fred Drake3adf79e2001-10-12 19:01:43 +00001885 PyObject *o}
1886 Macro form of \cfunction{PyList_SetItem()} without error checking.
1887 This is normally only used to fill in new lists where there is no
1888 previous content.
1889 \note{This function ``steals'' a reference to \var{item}, and,
1890 unlike \cfunction{PyList_SetItem()}, does \emph{not} discard a
1891 reference to any item that it being replaced; any reference in
1892 \var{list} at position \var{i} will be leaked.}
1893\end{cfuncdesc}
1894
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001895\begin{cfuncdesc}{int}{PyList_Insert}{PyObject *list, Py_ssize_t index,
Fred Drake3adf79e2001-10-12 19:01:43 +00001896 PyObject *item}
Georg Brandl99363b62005-09-03 07:27:26 +00001897 Insert the item \var{item} into list \var{list} in front of index
1898 \var{index}. Return \code{0} if successful; return \code{-1} and
1899 set an exception if unsuccessful. Analogous to
Fred Drake3adf79e2001-10-12 19:01:43 +00001900 \code{\var{list}.insert(\var{index}, \var{item})}.
1901\end{cfuncdesc}
1902
1903\begin{cfuncdesc}{int}{PyList_Append}{PyObject *list, PyObject *item}
Georg Brandl99363b62005-09-03 07:27:26 +00001904 Append the object \var{item} at the end of list \var{list}.
1905 Return \code{0} if successful; return \code{-1} and set an
Fred Drake3adf79e2001-10-12 19:01:43 +00001906 exception if unsuccessful. Analogous to
1907 \code{\var{list}.append(\var{item})}.
1908\end{cfuncdesc}
1909
1910\begin{cfuncdesc}{PyObject*}{PyList_GetSlice}{PyObject *list,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001911 Py_ssize_t low, Py_ssize_t high}
Georg Brandl99363b62005-09-03 07:27:26 +00001912 Return a list of the objects in \var{list} containing the objects
1913 \emph{between} \var{low} and \var{high}. Return \NULL{} and set
Fred Drake3adf79e2001-10-12 19:01:43 +00001914 an exception if unsuccessful.
1915 Analogous to \code{\var{list}[\var{low}:\var{high}]}.
1916\end{cfuncdesc}
1917
1918\begin{cfuncdesc}{int}{PyList_SetSlice}{PyObject *list,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001919 Py_ssize_t low, Py_ssize_t high,
Fred Drake3adf79e2001-10-12 19:01:43 +00001920 PyObject *itemlist}
Georg Brandl99363b62005-09-03 07:27:26 +00001921 Set the slice of \var{list} between \var{low} and \var{high} to the
Fred Drake3adf79e2001-10-12 19:01:43 +00001922 contents of \var{itemlist}. Analogous to
Raymond Hettinger9c7ed4c2003-10-26 17:20:07 +00001923 \code{\var{list}[\var{low}:\var{high}] = \var{itemlist}}.
1924 The \var{itemlist} may be \NULL{}, indicating the assignment
1925 of an empty list (slice deletion).
Georg Brandl99363b62005-09-03 07:27:26 +00001926 Return \code{0} on success, \code{-1} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00001927\end{cfuncdesc}
1928
1929\begin{cfuncdesc}{int}{PyList_Sort}{PyObject *list}
Georg Brandl99363b62005-09-03 07:27:26 +00001930 Sort 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 equivalent to
1932 \samp{\var{list}.sort()}.
1933\end{cfuncdesc}
1934
1935\begin{cfuncdesc}{int}{PyList_Reverse}{PyObject *list}
Georg Brandl99363b62005-09-03 07:27:26 +00001936 Reverse the items of \var{list} in place. Return \code{0} on
Fred Drake3adf79e2001-10-12 19:01:43 +00001937 success, \code{-1} on failure. This is the equivalent of
1938 \samp{\var{list}.reverse()}.
1939\end{cfuncdesc}
1940
1941\begin{cfuncdesc}{PyObject*}{PyList_AsTuple}{PyObject *list}
Georg Brandl99363b62005-09-03 07:27:26 +00001942 Return a new tuple object containing the contents of \var{list};
Fred Drake3adf79e2001-10-12 19:01:43 +00001943 equivalent to \samp{tuple(\var{list})}.\bifuncindex{tuple}
1944\end{cfuncdesc}
1945
1946
1947\section{Mapping Objects \label{mapObjects}}
1948
1949\obindex{mapping}
1950
1951
1952\subsection{Dictionary Objects \label{dictObjects}}
1953
1954\obindex{dictionary}
1955\begin{ctypedesc}{PyDictObject}
1956 This subtype of \ctype{PyObject} represents a Python dictionary
1957 object.
1958\end{ctypedesc}
1959
1960\begin{cvardesc}{PyTypeObject}{PyDict_Type}
1961 This instance of \ctype{PyTypeObject} represents the Python
1962 dictionary type. This is exposed to Python programs as
Georg Brandl7572f032006-08-08 20:48:10 +00001963 \code{dict} and \code{types.DictType}.
Fred Drake3adf79e2001-10-12 19:01:43 +00001964 \withsubitem{(in module types)}{\ttindex{DictType}\ttindex{DictionaryType}}
1965\end{cvardesc}
1966
1967\begin{cfuncdesc}{int}{PyDict_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00001968 Return true if \var{p} is a dict object or an instance of a
Andrew MacIntyre13cd8892003-12-25 23:57:52 +00001969 subtype of the dict type.
1970 \versionchanged[Allowed subtypes to be accepted]{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +00001971\end{cfuncdesc}
1972
Andrew MacIntyref72af652003-12-26 00:07:51 +00001973\begin{cfuncdesc}{int}{PyDict_CheckExact}{PyObject *p}
1974 Return true if \var{p} is a dict object, but not an instance of a
1975 subtype of the dict type.
1976 \versionadded{2.4}
1977\end{cfuncdesc}
1978
Fred Drake3adf79e2001-10-12 19:01:43 +00001979\begin{cfuncdesc}{PyObject*}{PyDict_New}{}
Georg Brandl99363b62005-09-03 07:27:26 +00001980 Return a new empty dictionary, or \NULL{} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00001981\end{cfuncdesc}
1982
1983\begin{cfuncdesc}{PyObject*}{PyDictProxy_New}{PyObject *dict}
1984 Return a proxy object for a mapping which enforces read-only
1985 behavior. This is normally used to create a proxy to prevent
1986 modification of the dictionary for non-dynamic class types.
1987 \versionadded{2.2}
1988\end{cfuncdesc}
1989
1990\begin{cfuncdesc}{void}{PyDict_Clear}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00001991 Empty an existing dictionary of all key-value pairs.
Fred Drake3adf79e2001-10-12 19:01:43 +00001992\end{cfuncdesc}
1993
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00001994\begin{cfuncdesc}{int}{PyDict_Contains}{PyObject *p, PyObject *key}
1995 Determine if dictionary \var{p} contains \var{key}. If an item
1996 in \var{p} is matches \var{key}, return \code{1}, otherwise return
1997 \code{0}. On error, return \code{-1}. This is equivalent to the
1998 Python expression \samp{\var{key} in \var{p}}.
Tim Peters9ddf40b2004-06-20 22:41:32 +00001999 \versionadded{2.4}
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00002000\end{cfuncdesc}
2001
Fred Drake3adf79e2001-10-12 19:01:43 +00002002\begin{cfuncdesc}{PyObject*}{PyDict_Copy}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002003 Return a new dictionary that contains the same key-value pairs as
Fred Drake3adf79e2001-10-12 19:01:43 +00002004 \var{p}.
2005 \versionadded{1.6}
2006\end{cfuncdesc}
2007
2008\begin{cfuncdesc}{int}{PyDict_SetItem}{PyObject *p, PyObject *key,
2009 PyObject *val}
Georg Brandl99363b62005-09-03 07:27:26 +00002010 Insert \var{value} into the dictionary \var{p} with a key of
Fred Drake3adf79e2001-10-12 19:01:43 +00002011 \var{key}. \var{key} must be hashable; if it isn't,
2012 \exception{TypeError} will be raised.
Georg Brandl99363b62005-09-03 07:27:26 +00002013 Return \code{0} on success or \code{-1} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00002014\end{cfuncdesc}
2015
2016\begin{cfuncdesc}{int}{PyDict_SetItemString}{PyObject *p,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002017 const char *key,
Fred Drake3adf79e2001-10-12 19:01:43 +00002018 PyObject *val}
Georg Brandl99363b62005-09-03 07:27:26 +00002019 Insert \var{value} into the dictionary \var{p} using \var{key} as a
Fred Drake3adf79e2001-10-12 19:01:43 +00002020 key. \var{key} should be a \ctype{char*}. The key object is created
Georg Brandl99363b62005-09-03 07:27:26 +00002021 using \code{PyString_FromString(\var{key})}. Return \code{0} on
Fred Drake3adf79e2001-10-12 19:01:43 +00002022 success or \code{-1} on failure.
2023 \ttindex{PyString_FromString()}
2024\end{cfuncdesc}
2025
2026\begin{cfuncdesc}{int}{PyDict_DelItem}{PyObject *p, PyObject *key}
Georg Brandl99363b62005-09-03 07:27:26 +00002027 Remove the entry in dictionary \var{p} with key \var{key}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002028 \var{key} must be hashable; if it isn't, \exception{TypeError} is
Georg Brandl99363b62005-09-03 07:27:26 +00002029 raised. Return \code{0} on success or \code{-1} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00002030\end{cfuncdesc}
2031
2032\begin{cfuncdesc}{int}{PyDict_DelItemString}{PyObject *p, char *key}
Georg Brandl99363b62005-09-03 07:27:26 +00002033 Remove the entry in dictionary \var{p} which has a key specified by
2034 the string \var{key}. Return \code{0} on success or \code{-1} on
Fred Drake3adf79e2001-10-12 19:01:43 +00002035 failure.
2036\end{cfuncdesc}
2037
2038\begin{cfuncdesc}{PyObject*}{PyDict_GetItem}{PyObject *p, PyObject *key}
Georg Brandl99363b62005-09-03 07:27:26 +00002039 Return the object from dictionary \var{p} which has a key
2040 \var{key}. Return \NULL{} if the key \var{key} is not present, but
Fred Drake3adf79e2001-10-12 19:01:43 +00002041 \emph{without} setting an exception.
2042\end{cfuncdesc}
2043
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002044\begin{cfuncdesc}{PyObject*}{PyDict_GetItemString}{PyObject *p, const char *key}
Fred Drake3adf79e2001-10-12 19:01:43 +00002045 This is the same as \cfunction{PyDict_GetItem()}, but \var{key} is
2046 specified as a \ctype{char*}, rather than a \ctype{PyObject*}.
2047\end{cfuncdesc}
2048
2049\begin{cfuncdesc}{PyObject*}{PyDict_Items}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002050 Return a \ctype{PyListObject} containing all the items from the
Nicholas Bastin975e7252004-09-29 21:39:26 +00002051 dictionary, as in the dictionary method \method{items()} (see the
Fred Drake3adf79e2001-10-12 19:01:43 +00002052 \citetitle[../lib/lib.html]{Python Library Reference}).
2053\end{cfuncdesc}
2054
2055\begin{cfuncdesc}{PyObject*}{PyDict_Keys}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002056 Return a \ctype{PyListObject} containing all the keys from the
Fred Drake3adf79e2001-10-12 19:01:43 +00002057 dictionary, as in the dictionary method \method{keys()} (see the
2058 \citetitle[../lib/lib.html]{Python Library Reference}).
2059\end{cfuncdesc}
2060
2061\begin{cfuncdesc}{PyObject*}{PyDict_Values}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002062 Return a \ctype{PyListObject} containing all the values from the
Fred Drake3adf79e2001-10-12 19:01:43 +00002063 dictionary \var{p}, as in the dictionary method \method{values()}
2064 (see the \citetitle[../lib/lib.html]{Python Library Reference}).
2065\end{cfuncdesc}
2066
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002067\begin{cfuncdesc}{Py_ssize_t}{PyDict_Size}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002068 Return the number of items in the dictionary. This is equivalent
Fred Drake3adf79e2001-10-12 19:01:43 +00002069 to \samp{len(\var{p})} on a dictionary.\bifuncindex{len}
2070\end{cfuncdesc}
2071
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002072\begin{cfuncdesc}{int}{PyDict_Next}{PyObject *p, Py_ssize_t *ppos,
Fred Drake3adf79e2001-10-12 19:01:43 +00002073 PyObject **pkey, PyObject **pvalue}
2074 Iterate over all key-value pairs in the dictionary \var{p}. The
2075 \ctype{int} referred to by \var{ppos} must be initialized to
2076 \code{0} prior to the first call to this function to start the
2077 iteration; the function returns true for each pair in the
2078 dictionary, and false once all pairs have been reported. The
2079 parameters \var{pkey} and \var{pvalue} should either point to
2080 \ctype{PyObject*} variables that will be filled in with each key and
Tim Peters9ddf40b2004-06-20 22:41:32 +00002081 value, respectively, or may be \NULL{}. Any references returned through
Raymond Hettinger54693242003-12-13 19:48:41 +00002082 them are borrowed. \var{ppos} should not be altered during iteration.
2083 Its value represents offsets within the internal dictionary structure,
2084 and since the structure is sparse, the offsets are not consecutive.
Fred Drake3adf79e2001-10-12 19:01:43 +00002085
2086 For example:
2087
2088\begin{verbatim}
2089PyObject *key, *value;
Georg Brandlb26b1c62007-01-17 21:19:58 +00002090Py_ssize_t pos = 0;
Fred Drake3adf79e2001-10-12 19:01:43 +00002091
2092while (PyDict_Next(self->dict, &pos, &key, &value)) {
2093 /* do something interesting with the values... */
2094 ...
2095}
2096\end{verbatim}
2097
2098 The dictionary \var{p} should not be mutated during iteration. It
2099 is safe (since Python 2.1) to modify the values of the keys as you
2100 iterate over the dictionary, but only so long as the set of keys
2101 does not change. For example:
2102
2103\begin{verbatim}
2104PyObject *key, *value;
Georg Brandlb26b1c62007-01-17 21:19:58 +00002105Py_ssize_t pos = 0;
Fred Drake3adf79e2001-10-12 19:01:43 +00002106
2107while (PyDict_Next(self->dict, &pos, &key, &value)) {
2108 int i = PyInt_AS_LONG(value) + 1;
2109 PyObject *o = PyInt_FromLong(i);
2110 if (o == NULL)
2111 return -1;
2112 if (PyDict_SetItem(self->dict, key, o) < 0) {
2113 Py_DECREF(o);
2114 return -1;
2115 }
2116 Py_DECREF(o);
2117}
2118\end{verbatim}
2119\end{cfuncdesc}
2120
2121\begin{cfuncdesc}{int}{PyDict_Merge}{PyObject *a, PyObject *b, int override}
Tim Petersf582b822001-12-11 18:51:08 +00002122 Iterate over mapping object \var{b} adding key-value pairs to dictionary
2123 \var{a}.
2124 \var{b} may be a dictionary, or any object supporting
2125 \function{PyMapping_Keys()} and \function{PyObject_GetItem()}.
2126 If \var{override} is true, existing pairs in \var{a} will
Fred Drake3adf79e2001-10-12 19:01:43 +00002127 be replaced if a matching key is found in \var{b}, otherwise pairs
2128 will only be added if there is not a matching key in \var{a}.
Tim Petersf582b822001-12-11 18:51:08 +00002129 Return \code{0} on success or \code{-1} if an exception was
Fred Drake3adf79e2001-10-12 19:01:43 +00002130 raised.
2131\versionadded{2.2}
2132\end{cfuncdesc}
2133
2134\begin{cfuncdesc}{int}{PyDict_Update}{PyObject *a, PyObject *b}
2135 This is the same as \code{PyDict_Merge(\var{a}, \var{b}, 1)} in C,
Tim Petersf582b822001-12-11 18:51:08 +00002136 or \code{\var{a}.update(\var{b})} in Python. Return \code{0} on
Fred Drake3adf79e2001-10-12 19:01:43 +00002137 success or \code{-1} if an exception was raised.
2138 \versionadded{2.2}
2139\end{cfuncdesc}
2140
Tim Petersf582b822001-12-11 18:51:08 +00002141\begin{cfuncdesc}{int}{PyDict_MergeFromSeq2}{PyObject *a, PyObject *seq2,
2142 int override}
2143 Update or merge into dictionary \var{a}, from the key-value pairs in
2144 \var{seq2}. \var{seq2} must be an iterable object producing
2145 iterable objects of length 2, viewed as key-value pairs. In case of
2146 duplicate keys, the last wins if \var{override} is true, else the
2147 first wins.
2148 Return \code{0} on success or \code{-1} if an exception
2149 was raised.
2150 Equivalent Python (except for the return value):
2151
2152\begin{verbatim}
2153def PyDict_MergeFromSeq2(a, seq2, override):
2154 for key, value in seq2:
2155 if override or key not in a:
2156 a[key] = value
2157\end{verbatim}
2158
2159 \versionadded{2.2}
2160\end{cfuncdesc}
Fred Drake3adf79e2001-10-12 19:01:43 +00002161
Fred Drake54e62942001-12-11 19:40:16 +00002162
Fred Drake3adf79e2001-10-12 19:01:43 +00002163\section{Other Objects \label{otherObjects}}
2164
Collin Winterd4a01182007-03-30 14:01:25 +00002165\subsection{Class Objects \label{classObjects}}
2166
2167\obindex{class}
2168Note that the class objects described here represent old-style classes,
2169which will go away in Python 3. When creating new types for extension
2170modules, you will want to work with type objects (section
2171\ref{typeObjects}).
2172
2173\begin{ctypedesc}{PyClassObject}
2174 The C structure of the objects used to describe built-in classes.
2175\end{ctypedesc}
2176
2177\begin{cvardesc}{PyObject*}{PyClass_Type}
2178 This is the type object for class objects; it is the same object as
2179 \code{types.ClassType} in the Python layer.
2180 \withsubitem{(in module types)}{\ttindex{ClassType}}
2181\end{cvardesc}
2182
2183\begin{cfuncdesc}{int}{PyClass_Check}{PyObject *o}
2184 Return true if the object \var{o} is a class object, including
2185 instances of types derived from the standard class object. Return
2186 false in all other cases.
2187\end{cfuncdesc}
2188
2189\begin{cfuncdesc}{int}{PyClass_IsSubclass}{PyObject *klass, PyObject *base}
2190 Return true if \var{klass} is a subclass of \var{base}. Return false in
2191 all other cases.
2192\end{cfuncdesc}
2193
Fred Drake3adf79e2001-10-12 19:01:43 +00002194\subsection{File Objects \label{fileObjects}}
2195
2196\obindex{file}
2197Python's built-in file objects are implemented entirely on the
2198\ctype{FILE*} support from the C standard library. This is an
2199implementation detail and may change in future releases of Python.
2200
2201\begin{ctypedesc}{PyFileObject}
2202 This subtype of \ctype{PyObject} represents a Python file object.
2203\end{ctypedesc}
2204
2205\begin{cvardesc}{PyTypeObject}{PyFile_Type}
2206 This instance of \ctype{PyTypeObject} represents the Python file
Georg Brandl7572f032006-08-08 20:48:10 +00002207 type. This is exposed to Python programs as \code{file} and
2208 \code{types.FileType}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002209 \withsubitem{(in module types)}{\ttindex{FileType}}
2210\end{cvardesc}
2211
2212\begin{cfuncdesc}{int}{PyFile_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002213 Return true if its argument is a \ctype{PyFileObject} or a subtype
Fred Drake3adf79e2001-10-12 19:01:43 +00002214 of \ctype{PyFileObject}.
2215 \versionchanged[Allowed subtypes to be accepted]{2.2}
2216\end{cfuncdesc}
2217
2218\begin{cfuncdesc}{int}{PyFile_CheckExact}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002219 Return true if its argument is a \ctype{PyFileObject}, but not a
Fred Drake3adf79e2001-10-12 19:01:43 +00002220 subtype of \ctype{PyFileObject}.
2221 \versionadded{2.2}
2222\end{cfuncdesc}
2223
2224\begin{cfuncdesc}{PyObject*}{PyFile_FromString}{char *filename, char *mode}
Georg Brandl99363b62005-09-03 07:27:26 +00002225 On success, return a new file object that is opened on the file
Fred Drake3adf79e2001-10-12 19:01:43 +00002226 given by \var{filename}, with a file mode given by \var{mode}, where
2227 \var{mode} has the same semantics as the standard C routine
Georg Brandl99363b62005-09-03 07:27:26 +00002228 \cfunction{fopen()}\ttindex{fopen()}. On failure, return \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002229\end{cfuncdesc}
2230
2231\begin{cfuncdesc}{PyObject*}{PyFile_FromFile}{FILE *fp,
2232 char *name, char *mode,
2233 int (*close)(FILE*)}
Georg Brandl99363b62005-09-03 07:27:26 +00002234 Create a new \ctype{PyFileObject} from the already-open standard C
Fred Drake3adf79e2001-10-12 19:01:43 +00002235 file pointer, \var{fp}. The function \var{close} will be called
Georg Brandl99363b62005-09-03 07:27:26 +00002236 when the file should be closed. Return \NULL{} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00002237\end{cfuncdesc}
2238
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002239\begin{cfuncdesc}{FILE*}{PyFile_AsFile}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002240 Return the file object associated with \var{p} as a \ctype{FILE*}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002241\end{cfuncdesc}
2242
2243\begin{cfuncdesc}{PyObject*}{PyFile_GetLine}{PyObject *p, int n}
2244 Equivalent to \code{\var{p}.readline(\optional{\var{n}})}, this
2245 function reads one line from the object \var{p}. \var{p} may be a
2246 file object or any object with a \method{readline()} method. If
2247 \var{n} is \code{0}, exactly one line is read, regardless of the
2248 length of the line. If \var{n} is greater than \code{0}, no more
2249 than \var{n} bytes will be read from the file; a partial line can be
2250 returned. In both cases, an empty string is returned if the end of
2251 the file is reached immediately. If \var{n} is less than \code{0},
2252 however, one line is read regardless of length, but
2253 \exception{EOFError} is raised if the end of the file is reached
2254 immediately.
2255 \withsubitem{(built-in exception)}{\ttindex{EOFError}}
2256\end{cfuncdesc}
2257
2258\begin{cfuncdesc}{PyObject*}{PyFile_Name}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002259 Return the name of the file specified by \var{p} as a string
Fred Drake3adf79e2001-10-12 19:01:43 +00002260 object.
2261\end{cfuncdesc}
2262
2263\begin{cfuncdesc}{void}{PyFile_SetBufSize}{PyFileObject *p, int n}
2264 Available on systems with \cfunction{setvbuf()}\ttindex{setvbuf()}
2265 only. This should only be called immediately after file object
2266 creation.
2267\end{cfuncdesc}
2268
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00002269\begin{cfuncdesc}{int}{PyFile_Encoding}{PyFileObject *p, char *enc}
2270 Set the file's encoding for Unicode output to \var{enc}. Return
2271 1 on success and 0 on failure.
2272 \versionadded{2.3}
2273\end{cfuncdesc}
2274
Fred Drake3adf79e2001-10-12 19:01:43 +00002275\begin{cfuncdesc}{int}{PyFile_SoftSpace}{PyObject *p, int newflag}
Georg Brandl99363b62005-09-03 07:27:26 +00002276 This function exists for internal use by the interpreter. Set the
Fred Drake3adf79e2001-10-12 19:01:43 +00002277 \member{softspace} attribute of \var{p} to \var{newflag} and
Georg Brandl99363b62005-09-03 07:27:26 +00002278 \withsubitem{(file attribute)}{\ttindex{softspace}}return the
Fred Drake3adf79e2001-10-12 19:01:43 +00002279 previous value. \var{p} does not have to be a file object for this
2280 function to work properly; any object is supported (thought its only
2281 interesting if the \member{softspace} attribute can be set). This
2282 function clears any errors, and will return \code{0} as the previous
2283 value if the attribute either does not exist or if there were errors
2284 in retrieving it. There is no way to detect errors from this
2285 function, but doing so should not be needed.
2286\end{cfuncdesc}
2287
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002288\begin{cfuncdesc}{int}{PyFile_WriteObject}{PyObject *obj, PyObject *p,
Fred Drake3adf79e2001-10-12 19:01:43 +00002289 int flags}
Georg Brandl99363b62005-09-03 07:27:26 +00002290 Write object \var{obj} to file object \var{p}. The only supported
Fred Drake3adf79e2001-10-12 19:01:43 +00002291 flag for \var{flags} is
2292 \constant{Py_PRINT_RAW}\ttindex{Py_PRINT_RAW}; if given, the
2293 \function{str()} of the object is written instead of the
Georg Brandl99363b62005-09-03 07:27:26 +00002294 \function{repr()}. Return \code{0} on success or \code{-1} on
Fred Drake3adf79e2001-10-12 19:01:43 +00002295 failure; the appropriate exception will be set.
2296\end{cfuncdesc}
2297
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002298\begin{cfuncdesc}{int}{PyFile_WriteString}{const char *s, PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002299 Write string \var{s} to file object \var{p}. Return \code{0} on
Fred Drake3adf79e2001-10-12 19:01:43 +00002300 success or \code{-1} on failure; the appropriate exception will be
2301 set.
2302\end{cfuncdesc}
2303
2304
2305\subsection{Instance Objects \label{instanceObjects}}
2306
2307\obindex{instance}
2308There are very few functions specific to instance objects.
2309
2310\begin{cvardesc}{PyTypeObject}{PyInstance_Type}
2311 Type object for class instances.
2312\end{cvardesc}
2313
2314\begin{cfuncdesc}{int}{PyInstance_Check}{PyObject *obj}
Georg Brandl99363b62005-09-03 07:27:26 +00002315 Return true if \var{obj} is an instance.
Fred Drake3adf79e2001-10-12 19:01:43 +00002316\end{cfuncdesc}
2317
2318\begin{cfuncdesc}{PyObject*}{PyInstance_New}{PyObject *class,
2319 PyObject *arg,
2320 PyObject *kw}
2321 Create a new instance of a specific class. The parameters \var{arg}
2322 and \var{kw} are used as the positional and keyword parameters to
2323 the object's constructor.
2324\end{cfuncdesc}
2325
2326\begin{cfuncdesc}{PyObject*}{PyInstance_NewRaw}{PyObject *class,
2327 PyObject *dict}
Neil Schemenauerc4932292005-06-18 17:54:13 +00002328 Create a new instance of a specific class without calling its
Fred Drake3adf79e2001-10-12 19:01:43 +00002329 constructor. \var{class} is the class of new object. The
2330 \var{dict} parameter will be used as the object's \member{__dict__};
Tim Peters9ddf40b2004-06-20 22:41:32 +00002331 if \NULL{}, a new dictionary will be created for the instance.
Fred Drake3adf79e2001-10-12 19:01:43 +00002332\end{cfuncdesc}
2333
2334
Georg Brandl9b743f52006-02-20 12:57:53 +00002335\subsection{Function Objects \label{function-objects}}
2336
2337\obindex{function}
2338There are a few functions specific to Python functions.
2339
2340\begin{ctypedesc}{PyFunctionObject}
2341 The C structure used for functions.
2342\end{ctypedesc}
2343
2344\begin{cvardesc}{PyTypeObject}{PyFunction_Type}
2345 This is an instance of \ctype{PyTypeObject} and represents the
2346 Python function type. It is exposed to Python programmers as
2347 \code{types.FunctionType}.
2348 \withsubitem{(in module types)}{\ttindex{MethodType}}
2349\end{cvardesc}
2350
2351\begin{cfuncdesc}{int}{PyFunction_Check}{PyObject *o}
2352 Return true if \var{o} is a function object (has type
2353 \cdata{PyFunction_Type}). The parameter must not be \NULL{}.
2354\end{cfuncdesc}
2355
2356\begin{cfuncdesc}{PyObject*}{PyFunction_New}{PyObject *code,
2357 PyObject *globals}
2358 Return a new function object associated with the code object
Walter Dörwaldc44e14e2006-03-31 11:03:57 +00002359 \var{code}. \var{globals} must be a dictionary with the global
2360 variables accessible to the function.
Georg Brandl9b743f52006-02-20 12:57:53 +00002361
2362 The function's docstring, name and \var{__module__} are retrieved
2363 from the code object, the argument defaults and closure are set to
2364 \NULL{}.
2365\end{cfuncdesc}
2366
2367\begin{cfuncdesc}{PyObject*}{PyFunction_GetCode}{PyObject *op}
2368 Return the code object associated with the function object \var{op}.
2369\end{cfuncdesc}
2370
2371\begin{cfuncdesc}{PyObject*}{PyFunction_GetGlobals}{PyObject *op}
2372 Return the globals dictionary associated with the function object
2373 \var{op}.
2374\end{cfuncdesc}
2375
2376\begin{cfuncdesc}{PyObject*}{PyFunction_GetModule}{PyObject *op}
2377 Return the \var{__module__} attribute of the function object \var{op}.
2378 This is normally a string containing the module name, but can be set
2379 to any other object by Python code.
2380\end{cfuncdesc}
2381
2382\begin{cfuncdesc}{PyObject*}{PyFunction_GetDefaults}{PyObject *op}
2383 Return the argument default values of the function object \var{op}.
2384 This can be a tuple of arguments or \NULL{}.
2385\end{cfuncdesc}
2386
2387\begin{cfuncdesc}{int}{PyFunction_SetDefaults}{PyObject *op,
2388 PyObject *defaults}
2389 Set the argument default values for the function object \var{op}.
2390 \var{defaults} must be \var{Py_None} or a tuple.
2391
2392 Raises \exception{SystemError} and returns \code{-1} on failure.
2393\end{cfuncdesc}
2394
2395\begin{cfuncdesc}{PyObject*}{PyFunction_GetClosure}{PyObject *op}
2396 Return the closure associated with the function object \var{op}.
2397 This can be \NULL{} or a tuple of cell objects.
2398\end{cfuncdesc}
2399
2400\begin{cfuncdesc}{int}{PyFunction_SetClosure}{PyObject *op,
2401 PyObject *closure}
2402 Set the closure associated with the function object \var{op}.
2403 \var{closure} must be \var{Py_None} or a tuple of cell objects.
2404
2405 Raises \exception{SystemError} and returns \code{-1} on failure.
2406\end{cfuncdesc}
2407
2408
Fred Drake3adf79e2001-10-12 19:01:43 +00002409\subsection{Method Objects \label{method-objects}}
2410
2411\obindex{method}
2412There are some useful functions that are useful for working with
2413method objects.
2414
2415\begin{cvardesc}{PyTypeObject}{PyMethod_Type}
2416 This instance of \ctype{PyTypeObject} represents the Python method
2417 type. This is exposed to Python programs as \code{types.MethodType}.
2418 \withsubitem{(in module types)}{\ttindex{MethodType}}
2419\end{cvardesc}
2420
2421\begin{cfuncdesc}{int}{PyMethod_Check}{PyObject *o}
2422 Return true if \var{o} is a method object (has type
Tim Peters9ddf40b2004-06-20 22:41:32 +00002423 \cdata{PyMethod_Type}). The parameter must not be \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002424\end{cfuncdesc}
2425
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002426\begin{cfuncdesc}{PyObject*}{PyMethod_New}{PyObject *func,
Fred Drake3adf79e2001-10-12 19:01:43 +00002427 PyObject *self, PyObject *class}
2428 Return a new method object, with \var{func} being any callable
2429 object; this is the function that will be called when the method is
2430 called. If this method should be bound to an instance, \var{self}
2431 should be the instance and \var{class} should be the class of
2432 \var{self}, otherwise \var{self} should be \NULL{} and \var{class}
2433 should be the class which provides the unbound method..
2434\end{cfuncdesc}
2435
2436\begin{cfuncdesc}{PyObject*}{PyMethod_Class}{PyObject *meth}
2437 Return the class object from which the method \var{meth} was
2438 created; if this was created from an instance, it will be the class
2439 of the instance.
2440\end{cfuncdesc}
2441
2442\begin{cfuncdesc}{PyObject*}{PyMethod_GET_CLASS}{PyObject *meth}
2443 Macro version of \cfunction{PyMethod_Class()} which avoids error
2444 checking.
2445\end{cfuncdesc}
2446
2447\begin{cfuncdesc}{PyObject*}{PyMethod_Function}{PyObject *meth}
2448 Return the function object associated with the method \var{meth}.
2449\end{cfuncdesc}
2450
2451\begin{cfuncdesc}{PyObject*}{PyMethod_GET_FUNCTION}{PyObject *meth}
2452 Macro version of \cfunction{PyMethod_Function()} which avoids error
2453 checking.
2454\end{cfuncdesc}
2455
2456\begin{cfuncdesc}{PyObject*}{PyMethod_Self}{PyObject *meth}
2457 Return the instance associated with the method \var{meth} if it is
Tim Peters9ddf40b2004-06-20 22:41:32 +00002458 bound, otherwise return \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002459\end{cfuncdesc}
2460
2461\begin{cfuncdesc}{PyObject*}{PyMethod_GET_SELF}{PyObject *meth}
2462 Macro version of \cfunction{PyMethod_Self()} which avoids error
2463 checking.
2464\end{cfuncdesc}
2465
2466
2467\subsection{Module Objects \label{moduleObjects}}
2468
2469\obindex{module}
2470There are only a few functions special to module objects.
2471
2472\begin{cvardesc}{PyTypeObject}{PyModule_Type}
2473 This instance of \ctype{PyTypeObject} represents the Python module
2474 type. This is exposed to Python programs as
2475 \code{types.ModuleType}.
2476 \withsubitem{(in module types)}{\ttindex{ModuleType}}
2477\end{cvardesc}
2478
2479\begin{cfuncdesc}{int}{PyModule_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002480 Return true if \var{p} is a module object, or a subtype of a module
Fred Drake3adf79e2001-10-12 19:01:43 +00002481 object.
2482 \versionchanged[Allowed subtypes to be accepted]{2.2}
2483\end{cfuncdesc}
2484
2485\begin{cfuncdesc}{int}{PyModule_CheckExact}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002486 Return true if \var{p} is a module object, but not a subtype of
Fred Drake3adf79e2001-10-12 19:01:43 +00002487 \cdata{PyModule_Type}.
2488 \versionadded{2.2}
2489\end{cfuncdesc}
2490
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002491\begin{cfuncdesc}{PyObject*}{PyModule_New}{const char *name}
Fred Drake3adf79e2001-10-12 19:01:43 +00002492 Return a new module object with the \member{__name__} attribute set
2493 to \var{name}. Only the module's \member{__doc__} and
2494 \member{__name__} attributes are filled in; the caller is
2495 responsible for providing a \member{__file__} attribute.
2496 \withsubitem{(module attribute)}{
2497 \ttindex{__name__}\ttindex{__doc__}\ttindex{__file__}}
2498\end{cfuncdesc}
2499
2500\begin{cfuncdesc}{PyObject*}{PyModule_GetDict}{PyObject *module}
2501 Return the dictionary object that implements \var{module}'s
2502 namespace; this object is the same as the \member{__dict__}
2503 attribute of the module object. This function never fails.
2504 \withsubitem{(module attribute)}{\ttindex{__dict__}}
Fred Drakef495ef72002-04-12 19:32:07 +00002505 It is recommended extensions use other \cfunction{PyModule_*()}
2506 and \cfunction{PyObject_*()} functions rather than directly
2507 manipulate a module's \member{__dict__}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002508\end{cfuncdesc}
2509
2510\begin{cfuncdesc}{char*}{PyModule_GetName}{PyObject *module}
2511 Return \var{module}'s \member{__name__} value. If the module does
2512 not provide one, or if it is not a string, \exception{SystemError}
2513 is raised and \NULL{} is returned.
2514 \withsubitem{(module attribute)}{\ttindex{__name__}}
2515 \withsubitem{(built-in exception)}{\ttindex{SystemError}}
2516\end{cfuncdesc}
2517
2518\begin{cfuncdesc}{char*}{PyModule_GetFilename}{PyObject *module}
2519 Return the name of the file from which \var{module} was loaded using
2520 \var{module}'s \member{__file__} attribute. If this is not defined,
2521 or if it is not a string, raise \exception{SystemError} and return
Tim Peters9ddf40b2004-06-20 22:41:32 +00002522 \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002523 \withsubitem{(module attribute)}{\ttindex{__file__}}
2524 \withsubitem{(built-in exception)}{\ttindex{SystemError}}
2525\end{cfuncdesc}
2526
2527\begin{cfuncdesc}{int}{PyModule_AddObject}{PyObject *module,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002528 const char *name, PyObject *value}
Fred Drake3adf79e2001-10-12 19:01:43 +00002529 Add an object to \var{module} as \var{name}. This is a convenience
2530 function which can be used from the module's initialization
Georg Brandl99363b62005-09-03 07:27:26 +00002531 function. This steals a reference to \var{value}. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00002532 \code{-1} on error, \code{0} on success.
2533 \versionadded{2.0}
2534\end{cfuncdesc}
2535
2536\begin{cfuncdesc}{int}{PyModule_AddIntConstant}{PyObject *module,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002537 const char *name, long value}
Fred Drake3adf79e2001-10-12 19:01:43 +00002538 Add an integer constant to \var{module} as \var{name}. This
2539 convenience function can be used from the module's initialization
Georg Brandl99363b62005-09-03 07:27:26 +00002540 function. Return \code{-1} on error, \code{0} on success.
Fred Drake3adf79e2001-10-12 19:01:43 +00002541 \versionadded{2.0}
2542\end{cfuncdesc}
2543
2544\begin{cfuncdesc}{int}{PyModule_AddStringConstant}{PyObject *module,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002545 const char *name, const char *value}
Fred Drake3adf79e2001-10-12 19:01:43 +00002546 Add a string constant to \var{module} as \var{name}. This
2547 convenience function can be used from the module's initialization
Georg Brandl99363b62005-09-03 07:27:26 +00002548 function. The string \var{value} must be null-terminated. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00002549 \code{-1} on error, \code{0} on success.
2550 \versionadded{2.0}
2551\end{cfuncdesc}
2552
2553
2554\subsection{Iterator Objects \label{iterator-objects}}
2555
2556Python provides two general-purpose iterator objects. The first, a
2557sequence iterator, works with an arbitrary sequence supporting the
2558\method{__getitem__()} method. The second works with a callable
2559object and a sentinel value, calling the callable for each item in the
2560sequence, and ending the iteration when the sentinel value is
2561returned.
2562
2563\begin{cvardesc}{PyTypeObject}{PySeqIter_Type}
2564 Type object for iterator objects returned by
2565 \cfunction{PySeqIter_New()} and the one-argument form of the
2566 \function{iter()} built-in function for built-in sequence types.
2567 \versionadded{2.2}
2568\end{cvardesc}
2569
2570\begin{cfuncdesc}{int}{PySeqIter_Check}{op}
2571 Return true if the type of \var{op} is \cdata{PySeqIter_Type}.
2572 \versionadded{2.2}
2573\end{cfuncdesc}
2574
2575\begin{cfuncdesc}{PyObject*}{PySeqIter_New}{PyObject *seq}
2576 Return an iterator that works with a general sequence object,
2577 \var{seq}. The iteration ends when the sequence raises
2578 \exception{IndexError} for the subscripting operation.
2579 \versionadded{2.2}
2580\end{cfuncdesc}
2581
2582\begin{cvardesc}{PyTypeObject}{PyCallIter_Type}
2583 Type object for iterator objects returned by
2584 \cfunction{PyCallIter_New()} and the two-argument form of the
2585 \function{iter()} built-in function.
2586 \versionadded{2.2}
2587\end{cvardesc}
2588
2589\begin{cfuncdesc}{int}{PyCallIter_Check}{op}
2590 Return true if the type of \var{op} is \cdata{PyCallIter_Type}.
2591 \versionadded{2.2}
2592\end{cfuncdesc}
2593
2594\begin{cfuncdesc}{PyObject*}{PyCallIter_New}{PyObject *callable,
2595 PyObject *sentinel}
2596 Return a new iterator. The first parameter, \var{callable}, can be
2597 any Python callable object that can be called with no parameters;
2598 each call to it should return the next item in the iteration. When
2599 \var{callable} returns a value equal to \var{sentinel}, the
2600 iteration will be terminated.
2601 \versionadded{2.2}
2602\end{cfuncdesc}
2603
2604
2605\subsection{Descriptor Objects \label{descriptor-objects}}
2606
Fred Drake54e62942001-12-11 19:40:16 +00002607``Descriptors'' are objects that describe some attribute of an object.
2608They are found in the dictionary of type objects.
2609
Fred Drake3adf79e2001-10-12 19:01:43 +00002610\begin{cvardesc}{PyTypeObject}{PyProperty_Type}
Fred Drake54e62942001-12-11 19:40:16 +00002611 The type object for the built-in descriptor types.
Fred Drake3adf79e2001-10-12 19:01:43 +00002612 \versionadded{2.2}
2613\end{cvardesc}
2614
2615\begin{cfuncdesc}{PyObject*}{PyDescr_NewGetSet}{PyTypeObject *type,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002616 struct PyGetSetDef *getset}
Fred Drake3adf79e2001-10-12 19:01:43 +00002617 \versionadded{2.2}
2618\end{cfuncdesc}
2619
2620\begin{cfuncdesc}{PyObject*}{PyDescr_NewMember}{PyTypeObject *type,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002621 struct PyMemberDef *meth}
Fred Drake3adf79e2001-10-12 19:01:43 +00002622 \versionadded{2.2}
2623\end{cfuncdesc}
2624
2625\begin{cfuncdesc}{PyObject*}{PyDescr_NewMethod}{PyTypeObject *type,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002626 struct PyMethodDef *meth}
Fred Drake3adf79e2001-10-12 19:01:43 +00002627 \versionadded{2.2}
2628\end{cfuncdesc}
2629
2630\begin{cfuncdesc}{PyObject*}{PyDescr_NewWrapper}{PyTypeObject *type,
2631 struct wrapperbase *wrapper,
2632 void *wrapped}
2633 \versionadded{2.2}
2634\end{cfuncdesc}
2635
Thomas Heller8178a222004-02-09 10:47:11 +00002636\begin{cfuncdesc}{PyObject*}{PyDescr_NewClassMethod}{PyTypeObject *type,
2637 PyMethodDef *method}
2638 \versionadded{2.3}
2639\end{cfuncdesc}
2640
Fred Drake3adf79e2001-10-12 19:01:43 +00002641\begin{cfuncdesc}{int}{PyDescr_IsData}{PyObject *descr}
Georg Brandl99363b62005-09-03 07:27:26 +00002642 Return true if the descriptor objects \var{descr} describes a data
Fred Drake3adf79e2001-10-12 19:01:43 +00002643 attribute, or false if it describes a method. \var{descr} must be a
2644 descriptor object; there is no error checking.
2645 \versionadded{2.2}
2646\end{cfuncdesc}
2647
2648\begin{cfuncdesc}{PyObject*}{PyWrapper_New}{PyObject *, PyObject *}
2649 \versionadded{2.2}
2650\end{cfuncdesc}
2651
2652
2653\subsection{Slice Objects \label{slice-objects}}
2654
2655\begin{cvardesc}{PyTypeObject}{PySlice_Type}
2656 The type object for slice objects. This is the same as
Georg Brandl7572f032006-08-08 20:48:10 +00002657 \code{slice} and \code{types.SliceType}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002658 \withsubitem{(in module types)}{\ttindex{SliceType}}
2659\end{cvardesc}
2660
2661\begin{cfuncdesc}{int}{PySlice_Check}{PyObject *ob}
Georg Brandl99363b62005-09-03 07:27:26 +00002662 Return true if \var{ob} is a slice object; \var{ob} must not be
Tim Peters9ddf40b2004-06-20 22:41:32 +00002663 \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002664\end{cfuncdesc}
2665
2666\begin{cfuncdesc}{PyObject*}{PySlice_New}{PyObject *start, PyObject *stop,
2667 PyObject *step}
2668 Return a new slice object with the given values. The \var{start},
2669 \var{stop}, and \var{step} parameters are used as the values of the
2670 slice object attributes of the same names. Any of the values may be
Tim Peters9ddf40b2004-06-20 22:41:32 +00002671 \NULL{}, in which case the \code{None} will be used for the
Georg Brandl99363b62005-09-03 07:27:26 +00002672 corresponding attribute. Return \NULL{} if the new object could
Fred Drake3adf79e2001-10-12 19:01:43 +00002673 not be allocated.
2674\end{cfuncdesc}
2675
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002676\begin{cfuncdesc}{int}{PySlice_GetIndices}{PySliceObject *slice, Py_ssize_t length,
2677 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step}
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002678Retrieve the start, stop and step indices from the slice object
2679\var{slice}, assuming a sequence of length \var{length}. Treats
2680indices greater than \var{length} as errors.
2681
2682Returns 0 on success and -1 on error with no exception set (unless one
2683of the indices was not \constant{None} and failed to be converted to
2684an integer, in which case -1 is returned with an exception set).
2685
2686You probably do not want to use this function. If you want to use
2687slice objects in versions of Python prior to 2.3, you would probably
2688do well to incorporate the source of \cfunction{PySlice_GetIndicesEx},
2689suitably renamed, in the source of your extension.
2690\end{cfuncdesc}
2691
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002692\begin{cfuncdesc}{int}{PySlice_GetIndicesEx}{PySliceObject *slice, Py_ssize_t length,
2693 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step,
2694 Py_ssize_t *slicelength}
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002695Usable replacement for \cfunction{PySlice_GetIndices}. Retrieve the
2696start, stop, and step indices from the slice object \var{slice}
2697assuming a sequence of length \var{length}, and store the length of
2698the slice in \var{slicelength}. Out of bounds indices are clipped in
2699a manner consistent with the handling of normal slices.
2700
2701Returns 0 on success and -1 on error with exception set.
2702
2703\versionadded{2.3}
Fred Drake3adf79e2001-10-12 19:01:43 +00002704\end{cfuncdesc}
2705
2706
2707\subsection{Weak Reference Objects \label{weakref-objects}}
2708
2709Python supports \emph{weak references} as first-class objects. There
2710are two specific object types which directly implement weak
2711references. The first is a simple reference object, and the second
2712acts as a proxy for the original object as much as it can.
2713
2714\begin{cfuncdesc}{int}{PyWeakref_Check}{ob}
2715 Return true if \var{ob} is either a reference or proxy object.
2716 \versionadded{2.2}
2717\end{cfuncdesc}
2718
2719\begin{cfuncdesc}{int}{PyWeakref_CheckRef}{ob}
2720 Return true if \var{ob} is a reference object.
2721 \versionadded{2.2}
2722\end{cfuncdesc}
2723
2724\begin{cfuncdesc}{int}{PyWeakref_CheckProxy}{ob}
2725 Return true if \var{ob} is a proxy object.
2726 \versionadded{2.2}
2727\end{cfuncdesc}
2728
2729\begin{cfuncdesc}{PyObject*}{PyWeakref_NewRef}{PyObject *ob,
2730 PyObject *callback}
2731 Return a weak reference object for the object \var{ob}. This will
2732 always return a new reference, but is not guaranteed to create a new
2733 object; an existing reference object may be returned. The second
2734 parameter, \var{callback}, can be a callable object that receives
2735 notification when \var{ob} is garbage collected; it should accept a
Raymond Hettinger5232f502004-03-25 08:51:36 +00002736 single parameter, which will be the weak reference object itself.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002737 \var{callback} may also be \code{None} or \NULL{}. If \var{ob}
Fred Drake3adf79e2001-10-12 19:01:43 +00002738 is not a weakly-referencable object, or if \var{callback} is not
Tim Peters9ddf40b2004-06-20 22:41:32 +00002739 callable, \code{None}, or \NULL{}, this will return \NULL{} and
Fred Drake3adf79e2001-10-12 19:01:43 +00002740 raise \exception{TypeError}.
2741 \versionadded{2.2}
2742\end{cfuncdesc}
2743
2744\begin{cfuncdesc}{PyObject*}{PyWeakref_NewProxy}{PyObject *ob,
2745 PyObject *callback}
2746 Return a weak reference proxy object for the object \var{ob}. This
2747 will always return a new reference, but is not guaranteed to create
2748 a new object; an existing proxy object may be returned. The second
2749 parameter, \var{callback}, can be a callable object that receives
2750 notification when \var{ob} is garbage collected; it should accept a
Raymond Hettinger5232f502004-03-25 08:51:36 +00002751 single parameter, which will be the weak reference object itself.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002752 \var{callback} may also be \code{None} or \NULL{}. If \var{ob} is not
Fred Drake3adf79e2001-10-12 19:01:43 +00002753 a weakly-referencable object, or if \var{callback} is not callable,
Tim Peters9ddf40b2004-06-20 22:41:32 +00002754 \code{None}, or \NULL{}, this will return \NULL{} and raise
Fred Drake3adf79e2001-10-12 19:01:43 +00002755 \exception{TypeError}.
2756 \versionadded{2.2}
2757\end{cfuncdesc}
2758
2759\begin{cfuncdesc}{PyObject*}{PyWeakref_GetObject}{PyObject *ref}
Georg Brandl99363b62005-09-03 07:27:26 +00002760 Return the referenced object from a weak reference, \var{ref}. If
Ka-Ping Yeebd379e92003-03-28 18:07:16 +00002761 the referent is no longer live, returns \code{None}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002762 \versionadded{2.2}
2763\end{cfuncdesc}
2764
2765\begin{cfuncdesc}{PyObject*}{PyWeakref_GET_OBJECT}{PyObject *ref}
2766 Similar to \cfunction{PyWeakref_GetObject()}, but implemented as a
2767 macro that does no error checking.
2768 \versionadded{2.2}
2769\end{cfuncdesc}
2770
2771
2772\subsection{CObjects \label{cObjects}}
2773
2774\obindex{CObject}
2775Refer to \emph{Extending and Embedding the Python Interpreter},
Fred Drake54e62942001-12-11 19:40:16 +00002776section~1.12, ``Providing a C API for an Extension Module,'' for more
Fred Drake3adf79e2001-10-12 19:01:43 +00002777information on using these objects.
2778
2779
2780\begin{ctypedesc}{PyCObject}
2781 This subtype of \ctype{PyObject} represents an opaque value, useful
2782 for C extension modules who need to pass an opaque value (as a
2783 \ctype{void*} pointer) through Python code to other C code. It is
2784 often used to make a C function pointer defined in one module
2785 available to other modules, so the regular import mechanism can be
2786 used to access C APIs defined in dynamically loaded modules.
2787\end{ctypedesc}
2788
2789\begin{cfuncdesc}{int}{PyCObject_Check}{PyObject *p}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002790 Return true if its argument is a \ctype{PyCObject}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002791\end{cfuncdesc}
2792
Tim Petersf582b822001-12-11 18:51:08 +00002793\begin{cfuncdesc}{PyObject*}{PyCObject_FromVoidPtr}{void* cobj,
Fred Drake54e62942001-12-11 19:40:16 +00002794 void (*destr)(void *)}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002795 Create a \ctype{PyCObject} from the \code{void *}\var{cobj}. The
Fred Drake3adf79e2001-10-12 19:01:43 +00002796 \var{destr} function will be called when the object is reclaimed,
Tim Peters9ddf40b2004-06-20 22:41:32 +00002797 unless it is \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002798\end{cfuncdesc}
2799
2800\begin{cfuncdesc}{PyObject*}{PyCObject_FromVoidPtrAndDesc}{void* cobj,
2801 void* desc, void (*destr)(void *, void *)}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002802 Create a \ctype{PyCObject} from the \ctype{void *}\var{cobj}. The
Fred Drake3adf79e2001-10-12 19:01:43 +00002803 \var{destr} function will be called when the object is reclaimed.
2804 The \var{desc} argument can be used to pass extra callback data for
2805 the destructor function.
2806\end{cfuncdesc}
2807
2808\begin{cfuncdesc}{void*}{PyCObject_AsVoidPtr}{PyObject* self}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002809 Return the object \ctype{void *} that the \ctype{PyCObject}
Fred Drake3adf79e2001-10-12 19:01:43 +00002810 \var{self} was created with.
2811\end{cfuncdesc}
2812
2813\begin{cfuncdesc}{void*}{PyCObject_GetDesc}{PyObject* self}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002814 Return the description \ctype{void *} that the \ctype{PyCObject}
Fred Drake3adf79e2001-10-12 19:01:43 +00002815 \var{self} was created with.
2816\end{cfuncdesc}
Fred Drakecd8474e2001-11-26 21:29:17 +00002817
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002818\begin{cfuncdesc}{int}{PyCObject_SetVoidPtr}{PyObject* self, void* cobj}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002819 Set the void pointer inside \var{self} to \var{cobj}.
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002820 The \ctype{PyCObject} must not have an associated destructor.
2821 Return true on success, false on failure.
2822\end{cfuncdesc}
2823
Fred Drakecd8474e2001-11-26 21:29:17 +00002824
2825\subsection{Cell Objects \label{cell-objects}}
2826
2827``Cell'' objects are used to implement variables referenced by
2828multiple scopes. For each such variable, a cell object is created to
2829store the value; the local variables of each stack frame that
2830references the value contains a reference to the cells from outer
2831scopes which also use that variable. When the value is accessed, the
2832value contained in the cell is used instead of the cell object
2833itself. This de-referencing of the cell object requires support from
2834the generated byte-code; these are not automatically de-referenced
2835when accessed. Cell objects are not likely to be useful elsewhere.
2836
Fred Drake54e62942001-12-11 19:40:16 +00002837\begin{ctypedesc}{PyCellObject}
2838 The C structure used for cell objects.
2839\end{ctypedesc}
2840
Fred Drakecd8474e2001-11-26 21:29:17 +00002841\begin{cvardesc}{PyTypeObject}{PyCell_Type}
Georg Brandl9b743f52006-02-20 12:57:53 +00002842 The type object corresponding to cell objects.
Fred Drakecd8474e2001-11-26 21:29:17 +00002843\end{cvardesc}
2844
2845\begin{cfuncdesc}{int}{PyCell_Check}{ob}
2846 Return true if \var{ob} is a cell object; \var{ob} must not be
Tim Peters9ddf40b2004-06-20 22:41:32 +00002847 \NULL{}.
Fred Drakecd8474e2001-11-26 21:29:17 +00002848\end{cfuncdesc}
2849
2850\begin{cfuncdesc}{PyObject*}{PyCell_New}{PyObject *ob}
2851 Create and return a new cell object containing the value \var{ob}.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002852 The parameter may be \NULL{}.
Fred Drakecd8474e2001-11-26 21:29:17 +00002853\end{cfuncdesc}
2854
2855\begin{cfuncdesc}{PyObject*}{PyCell_Get}{PyObject *cell}
2856 Return the contents of the cell \var{cell}.
2857\end{cfuncdesc}
2858
2859\begin{cfuncdesc}{PyObject*}{PyCell_GET}{PyObject *cell}
2860 Return the contents of the cell \var{cell}, but without checking
Raymond Hettingerf4bb1f92003-08-23 03:38:11 +00002861 that \var{cell} is non-\NULL{} and a cell object.
Fred Drakecd8474e2001-11-26 21:29:17 +00002862\end{cfuncdesc}
2863
2864\begin{cfuncdesc}{int}{PyCell_Set}{PyObject *cell, PyObject *value}
2865 Set the contents of the cell object \var{cell} to \var{value}. This
2866 releases the reference to any current content of the cell.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002867 \var{value} may be \NULL{}. \var{cell} must be non-\NULL{}; if it is
Fred Drakecd8474e2001-11-26 21:29:17 +00002868 not a cell object, \code{-1} will be returned. On success, \code{0}
2869 will be returned.
2870\end{cfuncdesc}
2871
2872\begin{cfuncdesc}{void}{PyCell_SET}{PyObject *cell, PyObject *value}
2873 Sets the value of the cell object \var{cell} to \var{value}. No
2874 reference counts are adjusted, and no checks are made for safety;
2875 \var{cell} must be non-\NULL{} and must be a cell object.
2876\end{cfuncdesc}
Martin v. Löwise440e472004-06-01 15:22:42 +00002877
2878
2879\subsection{Generator Objects \label{gen-objects}}
2880
2881Generator objects are what Python uses to implement generator iterators.
2882They are normally created by iterating over a function that yields values,
2883rather than explicitly calling \cfunction{PyGen_New}.
2884
2885\begin{ctypedesc}{PyGenObject}
2886 The C structure used for generator objects.
2887\end{ctypedesc}
2888
2889\begin{cvardesc}{PyTypeObject}{PyGen_Type}
2890 The type object corresponding to generator objects
2891\end{cvardesc}
2892
2893\begin{cfuncdesc}{int}{PyGen_Check}{ob}
2894 Return true if \var{ob} is a generator object; \var{ob} must not be
Tim Peters9ddf40b2004-06-20 22:41:32 +00002895 \NULL{}.
Martin v. Löwise440e472004-06-01 15:22:42 +00002896\end{cfuncdesc}
2897
2898\begin{cfuncdesc}{int}{PyGen_CheckExact}{ob}
2899 Return true if \var{ob}'s type is \var{PyGen_Type}
2900 is a generator object; \var{ob} must not be
Tim Peters9ddf40b2004-06-20 22:41:32 +00002901 \NULL{}.
Martin v. Löwise440e472004-06-01 15:22:42 +00002902\end{cfuncdesc}
2903
2904\begin{cfuncdesc}{PyObject*}{PyGen_New}{PyFrameObject *frame}
2905 Create and return a new generator object based on the \var{frame} object.
Fred Drake3e482d92006-03-30 02:58:38 +00002906 A reference to \var{frame} is stolen by this function.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002907 The parameter must not be \NULL{}.
2908\end{cfuncdesc}
2909
2910
2911\subsection{DateTime Objects \label{datetime-objects}}
2912
2913Various date and time objects are supplied by the \module{datetime}
2914module. Before using any of these functions, the header file
2915\file{datetime.h} must be included in your source (note that this is
Andrew M. Kuchlingacde6102006-10-26 19:10:46 +00002916not included by \file{Python.h}), and the macro
2917\cfunction{PyDateTime_IMPORT} must be invoked. The macro puts a
2918pointer to a C structure into a static variable,
2919\code{PyDateTimeAPI}, that is used by the following macros.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002920
Tim Peters183dabc2004-07-11 19:26:19 +00002921Type-check macros:
2922
2923\begin{cfuncdesc}{int}{PyDate_Check}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002924 Return true if \var{ob} is of type \cdata{PyDateTime_DateType} or
2925 a subtype of \cdata{PyDateTime_DateType}. \var{ob} must not be
2926 \NULL{}.
2927 \versionadded{2.4}
2928\end{cfuncdesc}
2929
Tim Peters183dabc2004-07-11 19:26:19 +00002930\begin{cfuncdesc}{int}{PyDate_CheckExact}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002931 Return true if \var{ob} is of type \cdata{PyDateTime_DateType}.
2932 \var{ob} must not be \NULL{}.
2933 \versionadded{2.4}
2934\end{cfuncdesc}
2935
Tim Peters183dabc2004-07-11 19:26:19 +00002936\begin{cfuncdesc}{int}{PyDateTime_Check}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002937 Return true if \var{ob} is of type \cdata{PyDateTime_DateTimeType} or
2938 a subtype of \cdata{PyDateTime_DateTimeType}. \var{ob} must not be
2939 \NULL{}.
2940 \versionadded{2.4}
2941\end{cfuncdesc}
2942
Tim Peters183dabc2004-07-11 19:26:19 +00002943\begin{cfuncdesc}{int}{PyDateTime_CheckExact}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002944 Return true if \var{ob} is of type \cdata{PyDateTime_DateTimeType}.
2945 \var{ob} must not be \NULL{}.
2946 \versionadded{2.4}
2947\end{cfuncdesc}
2948
Tim Peters183dabc2004-07-11 19:26:19 +00002949\begin{cfuncdesc}{int}{PyTime_Check}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002950 Return true if \var{ob} is of type \cdata{PyDateTime_TimeType} or
2951 a subtype of \cdata{PyDateTime_TimeType}. \var{ob} must not be
2952 \NULL{}.
2953 \versionadded{2.4}
2954\end{cfuncdesc}
2955
Tim Peters183dabc2004-07-11 19:26:19 +00002956\begin{cfuncdesc}{int}{PyTime_CheckExact}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002957 Return true if \var{ob} is of type \cdata{PyDateTime_TimeType}.
2958 \var{ob} must not be \NULL{}.
2959 \versionadded{2.4}
2960\end{cfuncdesc}
2961
Tim Peters183dabc2004-07-11 19:26:19 +00002962\begin{cfuncdesc}{int}{PyDelta_Check}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002963 Return true if \var{ob} is of type \cdata{PyDateTime_DeltaType} or
2964 a subtype of \cdata{PyDateTime_DeltaType}. \var{ob} must not be
2965 \NULL{}.
2966 \versionadded{2.4}
2967\end{cfuncdesc}
2968
Tim Peters183dabc2004-07-11 19:26:19 +00002969\begin{cfuncdesc}{int}{PyDelta_CheckExact}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002970 Return true if \var{ob} is of type \cdata{PyDateTime_DeltaType}.
2971 \var{ob} must not be \NULL{}.
2972 \versionadded{2.4}
2973\end{cfuncdesc}
2974
Tim Peters183dabc2004-07-11 19:26:19 +00002975\begin{cfuncdesc}{int}{PyTZInfo_Check}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002976 Return true if \var{ob} is of type \cdata{PyDateTime_TZInfoType} or
2977 a subtype of \cdata{PyDateTime_TZInfoType}. \var{ob} must not be
2978 \NULL{}.
2979 \versionadded{2.4}
2980\end{cfuncdesc}
2981
Tim Peters183dabc2004-07-11 19:26:19 +00002982\begin{cfuncdesc}{int}{PyTZInfo_CheckExact}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002983 Return true if \var{ob} is of type \cdata{PyDateTime_TZInfoType}.
2984 \var{ob} must not be \NULL{}.
2985 \versionadded{2.4}
2986\end{cfuncdesc}
2987
Tim Peters183dabc2004-07-11 19:26:19 +00002988Macros to create objects:
2989
Tim Peters9ddf40b2004-06-20 22:41:32 +00002990\begin{cfuncdesc}{PyObject*}{PyDate_FromDate}{int year, int month, int day}
2991 Return a \code{datetime.date} object with the specified year, month
2992 and day.
2993 \versionadded{2.4}
2994\end{cfuncdesc}
2995
Brett Cannon5bbe6ad2005-02-17 05:17:17 +00002996\begin{cfuncdesc}{PyObject*}{PyDateTime_FromDateAndTime}{int year, int month,
Tim Peters9ddf40b2004-06-20 22:41:32 +00002997 int day, int hour, int minute, int second, int usecond}
2998 Return a \code{datetime.datetime} object with the specified year, month,
2999 day, hour, minute, second and microsecond.
3000 \versionadded{2.4}
3001\end{cfuncdesc}
3002
3003\begin{cfuncdesc}{PyObject*}{PyTime_FromTime}{int hour, int minute,
3004 int second, int usecond}
3005 Return a \code{datetime.time} object with the specified hour, minute,
3006 second and microsecond.
3007 \versionadded{2.4}
3008\end{cfuncdesc}
3009
3010\begin{cfuncdesc}{PyObject*}{PyDelta_FromDSU}{int days, int seconds,
3011 int useconds}
3012 Return a \code{datetime.timedelta} object representing the given number
3013 of days, seconds and microseconds. Normalization is performed so that
3014 the resulting number of microseconds and seconds lie in the ranges
3015 documented for \code{datetime.timedelta} objects.
3016 \versionadded{2.4}
3017\end{cfuncdesc}
3018
Tim Peters8ff9f9f2004-07-17 01:42:26 +00003019Macros to extract fields from date objects. The argument must be an
Tim Peters183dabc2004-07-11 19:26:19 +00003020instance of \cdata{PyDateTime_Date}, including subclasses (such as
3021\cdata{PyDateTime_DateTime}). The argument must not be \NULL{}, and
3022the type is not checked:
3023
3024\begin{cfuncdesc}{int}{PyDateTime_GET_YEAR}{PyDateTime_Date *o}
3025 Return the year, as a positive int.
3026 \versionadded{2.4}
3027\end{cfuncdesc}
3028
3029\begin{cfuncdesc}{int}{PyDateTime_GET_MONTH}{PyDateTime_Date *o}
3030 Return the month, as an int from 1 through 12.
3031 \versionadded{2.4}
3032\end{cfuncdesc}
3033
3034\begin{cfuncdesc}{int}{PyDateTime_GET_DAY}{PyDateTime_Date *o}
3035 Return the day, as an int from 1 through 31.
3036 \versionadded{2.4}
3037\end{cfuncdesc}
3038
Tim Peters8ff9f9f2004-07-17 01:42:26 +00003039Macros to extract fields from datetime objects. The argument must be an
Tim Peters183dabc2004-07-11 19:26:19 +00003040instance of \cdata{PyDateTime_DateTime}, including subclasses.
3041The argument must not be \NULL{}, and the type is not checked:
3042
3043\begin{cfuncdesc}{int}{PyDateTime_DATE_GET_HOUR}{PyDateTime_DateTime *o}
Neal Norwitz7fdd92f2004-08-02 21:56:33 +00003044 Return the hour, as an int from 0 through 23.
Tim Peters183dabc2004-07-11 19:26:19 +00003045 \versionadded{2.4}
3046\end{cfuncdesc}
3047
3048\begin{cfuncdesc}{int}{PyDateTime_DATE_GET_MINUTE}{PyDateTime_DateTime *o}
3049 Return the minute, as an int from 0 through 59.
3050 \versionadded{2.4}
3051\end{cfuncdesc}
3052
3053\begin{cfuncdesc}{int}{PyDateTime_DATE_GET_SECOND}{PyDateTime_DateTime *o}
3054 Return the second, as an int from 0 through 59.
3055 \versionadded{2.4}
3056\end{cfuncdesc}
3057
3058\begin{cfuncdesc}{int}{PyDateTime_DATE_GET_MICROSECOND}{PyDateTime_DateTime *o}
3059 Return the microsecond, as an int from 0 through 999999.
3060 \versionadded{2.4}
3061\end{cfuncdesc}
3062
Tim Peters8ff9f9f2004-07-17 01:42:26 +00003063Macros to extract fields from time objects. The argument must be an
Tim Peters183dabc2004-07-11 19:26:19 +00003064instance of \cdata{PyDateTime_Time}, including subclasses.
3065The argument must not be \NULL{}, and the type is not checked:
3066
3067\begin{cfuncdesc}{int}{PyDateTime_TIME_GET_HOUR}{PyDateTime_Time *o}
Neal Norwitz7fdd92f2004-08-02 21:56:33 +00003068 Return the hour, as an int from 0 through 23.
Tim Peters183dabc2004-07-11 19:26:19 +00003069 \versionadded{2.4}
3070\end{cfuncdesc}
3071
3072\begin{cfuncdesc}{int}{PyDateTime_TIME_GET_MINUTE}{PyDateTime_Time *o}
3073 Return the minute, as an int from 0 through 59.
3074 \versionadded{2.4}
3075\end{cfuncdesc}
3076
3077\begin{cfuncdesc}{int}{PyDateTime_TIME_GET_SECOND}{PyDateTime_Time *o}
3078 Return the second, as an int from 0 through 59.
3079 \versionadded{2.4}
3080\end{cfuncdesc}
3081
3082\begin{cfuncdesc}{int}{PyDateTime_TIME_GET_MICROSECOND}{PyDateTime_Time *o}
3083 Return the microsecond, as an int from 0 through 999999.
3084 \versionadded{2.4}
3085\end{cfuncdesc}
3086
3087Macros for the convenience of modules implementing the DB API:
3088
Tim Peters9ddf40b2004-06-20 22:41:32 +00003089\begin{cfuncdesc}{PyObject*}{PyDateTime_FromTimestamp}{PyObject *args}
3090 Create and return a new \code{datetime.datetime} object given an argument
3091 tuple suitable for passing to \code{datetime.datetime.fromtimestamp()}.
Tim Peters9ddf40b2004-06-20 22:41:32 +00003092 \versionadded{2.4}
3093\end{cfuncdesc}
3094
3095\begin{cfuncdesc}{PyObject*}{PyDate_FromTimestamp}{PyObject *args}
3096 Create and return a new \code{datetime.date} object given an argument
3097 tuple suitable for passing to \code{datetime.date.fromtimestamp()}.
Tim Peters9ddf40b2004-06-20 22:41:32 +00003098 \versionadded{2.4}
Martin v. Löwise440e472004-06-01 15:22:42 +00003099\end{cfuncdesc}
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003100
3101
3102\subsection{Set Objects \label{setObjects}}
Tim Peters8931ff12006-05-13 23:28:20 +00003103\sectionauthor{Raymond D. Hettinger}{python@rcn.com}
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003104
3105\obindex{set}
3106\obindex{frozenset}
3107\versionadded{2.5}
3108
3109This section details the public API for \class{set} and \class{frozenset}
3110objects. Any functionality not listed below is best accessed using the
Raymond Hettinger0c230b92005-08-17 10:05:22 +00003111either the abstract object protocol (including
Tim Peters8931ff12006-05-13 23:28:20 +00003112\cfunction{PyObject_CallMethod()}, \cfunction{PyObject_RichCompareBool()},
3113\cfunction{PyObject_Hash()}, \cfunction{PyObject_Repr()},
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003114\cfunction{PyObject_IsTrue()}, \cfunction{PyObject_Print()}, and
Raymond Hettinger0c230b92005-08-17 10:05:22 +00003115\cfunction{PyObject_GetIter()})
3116or the abstract number protocol (including
Raymond Hettingera912c6c2006-11-23 21:06:03 +00003117\cfunction{PyNumber_And()}, \cfunction{PyNumber_Subtract()},
Raymond Hettinger0c230b92005-08-17 10:05:22 +00003118\cfunction{PyNumber_Or()}, \cfunction{PyNumber_Xor()},
Raymond Hettingera912c6c2006-11-23 21:06:03 +00003119\cfunction{PyNumber_InPlaceAnd()}, \cfunction{PyNumber_InPlaceSubtract()},
Georg Brandlb518d8c2006-02-22 11:46:55 +00003120\cfunction{PyNumber_InPlaceOr()}, and \cfunction{PyNumber_InPlaceXor()}).
Raymond Hettinger66760f82006-03-20 18:35:55 +00003121
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003122\begin{ctypedesc}{PySetObject}
3123 This subtype of \ctype{PyObject} is used to hold the internal data for
3124 both \class{set} and \class{frozenset} objects. It is like a
3125 \ctype{PyDictObject} in that it is a fixed size for small sets
3126 (much like tuple storage) and will point to a separate, variable sized
3127 block of memory for medium and large sized sets (much like list storage).
3128 None of the fields of this structure should be considered public and
3129 are subject to change. All access should be done through the
Tim Peters8931ff12006-05-13 23:28:20 +00003130 documented API rather than by manipulating the values in the structure.
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003131
3132\end{ctypedesc}
3133
3134\begin{cvardesc}{PyTypeObject}{PySet_Type}
3135 This is an instance of \ctype{PyTypeObject} representing the Python
3136 \class{set} type.
3137\end{cvardesc}
3138
3139\begin{cvardesc}{PyTypeObject}{PyFrozenSet_Type}
3140 This is an instance of \ctype{PyTypeObject} representing the Python
3141 \class{frozenset} type.
3142\end{cvardesc}
3143
3144
3145The following type check macros work on pointers to any Python object.
3146Likewise, the constructor functions work with any iterable Python object.
3147
3148\begin{cfuncdesc}{int}{PyAnySet_Check}{PyObject *p}
Tim Peters8931ff12006-05-13 23:28:20 +00003149 Return true if \var{p} is a \class{set} object, a \class{frozenset}
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003150 object, or an instance of a subtype.
3151\end{cfuncdesc}
3152
3153\begin{cfuncdesc}{int}{PyAnySet_CheckExact}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00003154 Return true if \var{p} is a \class{set} object or a \class{frozenset}
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003155 object but not an instance of a subtype.
3156\end{cfuncdesc}
3157
3158\begin{cfuncdesc}{int}{PyFrozenSet_CheckExact}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00003159 Return true if \var{p} is a \class{frozenset} object
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003160 but not an instance of a subtype.
3161\end{cfuncdesc}
3162
3163\begin{cfuncdesc}{PyObject*}{PySet_New}{PyObject *iterable}
Georg Brandl99363b62005-09-03 07:27:26 +00003164 Return a new \class{set} containing objects returned by the
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003165 \var{iterable}. The \var{iterable} may be \NULL{} to create a
Georg Brandl99363b62005-09-03 07:27:26 +00003166 new empty set. Return the new set on success or \NULL{} on
3167 failure. Raise \exception{TypeError} if \var{iterable} is
Raymond Hettinger94fedf92005-08-17 12:23:45 +00003168 not actually iterable. The constructor is also useful for
3169 copying a set (\code{c=set(s)}).
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003170\end{cfuncdesc}
3171
3172\begin{cfuncdesc}{PyObject*}{PyFrozenSet_New}{PyObject *iterable}
Georg Brandl99363b62005-09-03 07:27:26 +00003173 Return a new \class{frozenset} containing objects returned by the
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003174 \var{iterable}. The \var{iterable} may be \NULL{} to create a
Georg Brandl99363b62005-09-03 07:27:26 +00003175 new empty frozenset. Return the new set on success or \NULL{} on
3176 failure. Raise \exception{TypeError} if \var{iterable} is
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00003177 not actually iterable.
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003178\end{cfuncdesc}
3179
3180
3181The following functions and macros are available for instances of
3182\class{set} or \class{frozenset} or instances of their subtypes.
3183
3184\begin{cfuncdesc}{int}{PySet_Size}{PyObject *anyset}
Georg Brandl99363b62005-09-03 07:27:26 +00003185 Return the length of a \class{set} or \class{frozenset} object.
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003186 Equivalent to \samp{len(\var{anyset})}. Raises a
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00003187 \exception{PyExc_SystemError} if \var{anyset} is not a \class{set},
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003188 \class{frozenset}, or an instance of a subtype.
3189 \bifuncindex{len}
3190\end{cfuncdesc}
3191
3192\begin{cfuncdesc}{int}{PySet_GET_SIZE}{PyObject *anyset}
3193 Macro form of \cfunction{PySet_Size()} without error checking.
3194\end{cfuncdesc}
3195
3196\begin{cfuncdesc}{int}{PySet_Contains}{PyObject *anyset, PyObject *key}
Georg Brandl99363b62005-09-03 07:27:26 +00003197 Return 1 if found, 0 if not found, and -1 if an error is
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003198 encountered. Unlike the Python \method{__contains__()} method, this
3199 function does not automatically convert unhashable sets into temporary
Georg Brandl99363b62005-09-03 07:27:26 +00003200 frozensets. Raise a \exception{TypeError} if the \var{key} is unhashable.
3201 Raise \exception{PyExc_SystemError} if \var{anyset} is not a \class{set},
Tim Peters8931ff12006-05-13 23:28:20 +00003202 \class{frozenset}, or an instance of a subtype.
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003203\end{cfuncdesc}
3204
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003205The following functions are available for instances of \class{set} or
3206its subtypes but not for instances of \class{frozenset} or its subtypes.
3207
3208\begin{cfuncdesc}{int}{PySet_Add}{PyObject *set, PyObject *key}
Georg Brandl99363b62005-09-03 07:27:26 +00003209 Add \var{key} to a \class{set} instance. Does not apply to
3210 \class{frozenset} instances. Return 0 on success or -1 on failure.
3211 Raise a \exception{TypeError} if the \var{key} is unhashable.
3212 Raise a \exception{MemoryError} if there is no room to grow.
3213 Raise a \exception{SystemError} if \var{set} is an not an instance
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003214 of \class{set} or its subtype.
3215\end{cfuncdesc}
3216
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00003217\begin{cfuncdesc}{int}{PySet_Discard}{PyObject *set, PyObject *key}
Georg Brandl99363b62005-09-03 07:27:26 +00003218 Return 1 if found and removed, 0 if not found (no action taken),
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00003219 and -1 if an error is encountered. Does not raise \exception{KeyError}
Georg Brandl99363b62005-09-03 07:27:26 +00003220 for missing keys. Raise a \exception{TypeError} if the \var{key} is
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00003221 unhashable. Unlike the Python \method{discard()} method, this function
3222 does not automatically convert unhashable sets into temporary frozensets.
Georg Brandl99363b62005-09-03 07:27:26 +00003223 Raise \exception{PyExc_SystemError} if \var{set} is an not an instance
Tim Peters8931ff12006-05-13 23:28:20 +00003224 of \class{set} or its subtype.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00003225\end{cfuncdesc}
3226
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003227\begin{cfuncdesc}{PyObject*}{PySet_Pop}{PyObject *set}
Georg Brandl99363b62005-09-03 07:27:26 +00003228 Return a new reference to an arbitrary object in the \var{set},
3229 and removes the object from the \var{set}. Return \NULL{} on
3230 failure. Raise \exception{KeyError} if the set is empty.
3231 Raise a \exception{SystemError} if \var{set} is an not an instance
Tim Peters8931ff12006-05-13 23:28:20 +00003232 of \class{set} or its subtype.
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003233\end{cfuncdesc}
3234
Barry Warsaw176014f2006-03-30 22:45:35 +00003235\begin{cfuncdesc}{int}{PySet_Clear}{PyObject *set}
3236 Empty an existing set of all elements.
3237\end{cfuncdesc}