blob: 53c3b67760c560dfa04cf276a5f8c98b3dc7ff99 [file] [log] [blame]
Fred Drake3adf79e2001-10-12 19:01:43 +00001\chapter{Concrete Objects Layer \label{concrete}}
2
3
4The functions in this chapter are specific to certain Python object
5types. Passing them an object of the wrong type is not a good idea;
6if you receive an object from a Python program and you are not sure
7that it has the right type, you must perform a type check first;
8for example, to check that an object is a dictionary, use
9\cfunction{PyDict_Check()}. The chapter is structured like the
10``family tree'' of Python object types.
11
12\warning{While the functions described in this chapter carefully check
13the type of the objects which are passed in, many of them do not check
14for \NULL{} being passed instead of a valid object. Allowing \NULL{}
15to be passed in can cause memory access violations and immediate
16termination of the interpreter.}
17
18
19\section{Fundamental Objects \label{fundamental}}
20
Tim Petersf582b822001-12-11 18:51:08 +000021This section describes Python type objects and the singleton object
Fred Drake3adf79e2001-10-12 19:01:43 +000022\code{None}.
23
24
25\subsection{Type Objects \label{typeObjects}}
26
27\obindex{type}
28\begin{ctypedesc}{PyTypeObject}
29 The C structure of the objects used to describe built-in types.
30\end{ctypedesc}
31
32\begin{cvardesc}{PyObject*}{PyType_Type}
33 This is the type object for type objects; it is the same object as
34 \code{types.TypeType} in the Python layer.
35 \withsubitem{(in module types)}{\ttindex{TypeType}}
36\end{cvardesc}
37
38\begin{cfuncdesc}{int}{PyType_Check}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +000039 Return true if the object \var{o} is a type object, including
40 instances of types derived from the standard type object. Return
Fred Drakee3c764b2002-04-10 17:52:52 +000041 false in all other cases.
42\end{cfuncdesc}
43
44\begin{cfuncdesc}{int}{PyType_CheckExact}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +000045 Return true if the object \var{o} is a type object, but not a
46 subtype of the standard type object. Return false in all other
Fred Drakee3c764b2002-04-10 17:52:52 +000047 cases.
48 \versionadded{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +000049\end{cfuncdesc}
50
51\begin{cfuncdesc}{int}{PyType_HasFeature}{PyObject *o, int feature}
Georg Brandl99363b62005-09-03 07:27:26 +000052 Return true if the type object \var{o} sets the feature
Fred Drake3adf79e2001-10-12 19:01:43 +000053 \var{feature}. Type features are denoted by single bit flags.
54\end{cfuncdesc}
55
Fred Drakee3c764b2002-04-10 17:52:52 +000056\begin{cfuncdesc}{int}{PyType_IS_GC}{PyObject *o}
57 Return true if the type object includes support for the cycle
58 detector; this tests the type flag \constant{Py_TPFLAGS_HAVE_GC}.
59 \versionadded{2.0}
60\end{cfuncdesc}
61
Fred Drake3adf79e2001-10-12 19:01:43 +000062\begin{cfuncdesc}{int}{PyType_IsSubtype}{PyTypeObject *a, PyTypeObject *b}
Georg Brandl99363b62005-09-03 07:27:26 +000063 Return true if \var{a} is a subtype of \var{b}.
Fred Drake3adf79e2001-10-12 19:01:43 +000064 \versionadded{2.2}
65\end{cfuncdesc}
66
67\begin{cfuncdesc}{PyObject*}{PyType_GenericAlloc}{PyTypeObject *type,
68 int nitems}
69 \versionadded{2.2}
70\end{cfuncdesc}
71
72\begin{cfuncdesc}{PyObject*}{PyType_GenericNew}{PyTypeObject *type,
73 PyObject *args, PyObject *kwds}
74 \versionadded{2.2}
75\end{cfuncdesc}
76
77\begin{cfuncdesc}{int}{PyType_Ready}{PyTypeObject *type}
Fred Drake28de8d42002-04-12 16:15:10 +000078 Finalize a type object. This should be called on all type objects
79 to finish their initialization. This function is responsible for
Georg Brandl99363b62005-09-03 07:27:26 +000080 adding inherited slots from a type's base class. Return \code{0}
81 on success, or return \code{-1} and sets an exception on error.
Fred Drake3adf79e2001-10-12 19:01:43 +000082 \versionadded{2.2}
83\end{cfuncdesc}
84
85
86\subsection{The None Object \label{noneObject}}
87
Fred Drake7a700b82004-01-01 05:43:53 +000088\obindex{None}
Fred Drake3adf79e2001-10-12 19:01:43 +000089Note that the \ctype{PyTypeObject} for \code{None} is not directly
90exposed in the Python/C API. Since \code{None} is a singleton,
91testing for object identity (using \samp{==} in C) is sufficient.
92There is no \cfunction{PyNone_Check()} function for the same reason.
93
94\begin{cvardesc}{PyObject*}{Py_None}
95 The Python \code{None} object, denoting lack of value. This object
Fred Drake6ccdccd2002-03-12 20:12:54 +000096 has no methods. It needs to be treated just like any other object
97 with respect to reference counts.
Fred Drake3adf79e2001-10-12 19:01:43 +000098\end{cvardesc}
99
Brett Cannon35d83602003-11-09 04:15:30 +0000100\begin{csimplemacrodesc}{Py_RETURN_NONE}
Georg Brandl99363b62005-09-03 07:27:26 +0000101 Properly handle returning \cdata{Py_None} from within a C function.
Brett Cannon35d83602003-11-09 04:15:30 +0000102\end{csimplemacrodesc}
103
Fred Drake3adf79e2001-10-12 19:01:43 +0000104
105\section{Numeric Objects \label{numericObjects}}
106
107\obindex{numeric}
108
109
110\subsection{Plain Integer Objects \label{intObjects}}
111
112\obindex{integer}
113\begin{ctypedesc}{PyIntObject}
114 This subtype of \ctype{PyObject} represents a Python integer
115 object.
116\end{ctypedesc}
117
118\begin{cvardesc}{PyTypeObject}{PyInt_Type}
Tim Petersf582b822001-12-11 18:51:08 +0000119 This instance of \ctype{PyTypeObject} represents the Python plain
Fred Drake3adf79e2001-10-12 19:01:43 +0000120 integer type. This is the same object as \code{types.IntType}.
121 \withsubitem{(in modules types)}{\ttindex{IntType}}
122\end{cvardesc}
123
Andrew M. Kuchling4eb1a002004-08-07 20:19:24 +0000124\begin{cfuncdesc}{int}{PyInt_Check}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000125 Return true if \var{o} is of type \cdata{PyInt_Type} or a subtype
Fred Drake3adf79e2001-10-12 19:01:43 +0000126 of \cdata{PyInt_Type}.
127 \versionchanged[Allowed subtypes to be accepted]{2.2}
128\end{cfuncdesc}
129
Andrew M. Kuchling4eb1a002004-08-07 20:19:24 +0000130\begin{cfuncdesc}{int}{PyInt_CheckExact}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000131 Return true if \var{o} is of type \cdata{PyInt_Type}, but not a
Fred Drake3adf79e2001-10-12 19:01:43 +0000132 subtype of \cdata{PyInt_Type}.
133 \versionadded{2.2}
134\end{cfuncdesc}
135
Skip Montanaro1ff49a72003-02-03 05:13:24 +0000136\begin{cfuncdesc}{PyObject*}{PyInt_FromString}{char *str, char **pend,
137 int base}
138 Return a new \ctype{PyIntObject} or \ctype{PyLongObject} based on the
139 string value in \var{str}, which is interpreted according to the radix in
Tim Peters9ddf40b2004-06-20 22:41:32 +0000140 \var{base}. If \var{pend} is non-\NULL{}, \code{*\var{pend}} will point to
Skip Montanaro1ff49a72003-02-03 05:13:24 +0000141 the first character in \var{str} which follows the representation of the
142 number. If \var{base} is \code{0}, the radix will be determined based on
143 the leading characters of \var{str}: if \var{str} starts with \code{'0x'}
144 or \code{'0X'}, radix 16 will be used; if \var{str} starts with
145 \code{'0'}, radix 8 will be used; otherwise radix 10 will be used. If
146 \var{base} is not \code{0}, it must be between \code{2} and \code{36},
147 inclusive. Leading spaces are ignored. If there are no digits,
148 \exception{ValueError} will be raised. If the string represents a number
149 too large to be contained within the machine's \ctype{long int} type and
150 overflow warnings are being suppressed, a \ctype{PyLongObject} will be
151 returned. If overflow warnings are not being suppressed, \NULL{} will be
152 returned in this case.
153\end{cfuncdesc}
154
Fred Drake3adf79e2001-10-12 19:01:43 +0000155\begin{cfuncdesc}{PyObject*}{PyInt_FromLong}{long ival}
Georg Brandl99363b62005-09-03 07:27:26 +0000156 Create a new integer object with a value of \var{ival}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000157
158 The current implementation keeps an array of integer objects for all
159 integers between \code{-1} and \code{100}, when you create an int in
160 that range you actually just get back a reference to the existing
161 object. So it should be possible to change the value of \code{1}. I
162 suspect the behaviour of Python in this case is undefined. :-)
163\end{cfuncdesc}
164
165\begin{cfuncdesc}{long}{PyInt_AsLong}{PyObject *io}
166 Will first attempt to cast the object to a \ctype{PyIntObject}, if
167 it is not already one, and then return its value.
168\end{cfuncdesc}
169
170\begin{cfuncdesc}{long}{PyInt_AS_LONG}{PyObject *io}
Georg Brandl99363b62005-09-03 07:27:26 +0000171 Return the value of the object \var{io}. No error checking is
Fred Drake3adf79e2001-10-12 19:01:43 +0000172 performed.
173\end{cfuncdesc}
174
Thomas Heller34d7f092003-04-23 19:51:05 +0000175\begin{cfuncdesc}{unsigned long}{PyInt_AsUnsignedLongMask}{PyObject *io}
176 Will first attempt to cast the object to a \ctype{PyIntObject} or
Fred Drakec22b2992003-04-23 20:38:41 +0000177 \ctype{PyLongObject}, if it is not already one, and then return its
Thomas Heller34d7f092003-04-23 19:51:05 +0000178 value as unsigned long. This function does not check for overflow.
179 \versionadded{2.3}
180\end{cfuncdesc}
181
Thomas Hellerfe080832004-07-23 14:49:52 +0000182\begin{cfuncdesc}{unsigned long long}{PyInt_AsUnsignedLongLongMask}{PyObject *io}
Thomas Heller34d7f092003-04-23 19:51:05 +0000183 Will first attempt to cast the object to a \ctype{PyIntObject} or
Fred Drakec22b2992003-04-23 20:38:41 +0000184 \ctype{PyLongObject}, if it is not already one, and then return its
Thomas Heller34d7f092003-04-23 19:51:05 +0000185 value as unsigned long long, without checking for overflow.
186 \versionadded{2.3}
187\end{cfuncdesc}
188
Fred Drake3adf79e2001-10-12 19:01:43 +0000189\begin{cfuncdesc}{long}{PyInt_GetMax}{}
Georg Brandl99363b62005-09-03 07:27:26 +0000190 Return the system's idea of the largest integer it can handle
Fred Drake3adf79e2001-10-12 19:01:43 +0000191 (\constant{LONG_MAX}\ttindex{LONG_MAX}, as defined in the system
192 header files).
193\end{cfuncdesc}
194
Fred Drake2be406b2004-08-03 16:02:35 +0000195\subsection{Boolean Objects \label{boolObjects}}
Skip Montanaro33ee76a2004-07-28 14:17:04 +0000196
197Booleans in Python are implemented as a subclass of integers. There
198are only two booleans, \constant{Py_False} and \constant{Py_True}. As
199such, the normal creation and deletion functions don't apply to
200booleans. The following macros are available, however.
201
Andrew M. Kuchling4eb1a002004-08-07 20:19:24 +0000202\begin{cfuncdesc}{int}{PyBool_Check}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000203 Return true if \var{o} is of type \cdata{PyBool_Type}.
Skip Montanaro33ee76a2004-07-28 14:17:04 +0000204 \versionadded{2.3}
205\end{cfuncdesc}
206
Skip Montanaro6d3db702004-07-29 02:16:04 +0000207\begin{cvardesc}{PyObject*}{Py_False}
208 The Python \code{False} object. This object has no methods. It needs to
209 be treated just like any other object with respect to reference counts.
210\end{cvardesc}
Skip Montanaro33ee76a2004-07-28 14:17:04 +0000211
Skip Montanaro6d3db702004-07-29 02:16:04 +0000212\begin{cvardesc}{PyObject*}{Py_True}
213 The Python \code{True} object. This object has no methods. It needs to
214 be treated just like any other object with respect to reference counts.
215\end{cvardesc}
216
217\begin{csimplemacrodesc}{Py_RETURN_FALSE}
218 Return \constant{Py_False} from a function, properly incrementing its
219 reference count.
220\versionadded{2.4}
221\end{csimplemacrodesc}
222
223\begin{csimplemacrodesc}{Py_RETURN_TRUE}
Andrew M. Kuchling4eb1a002004-08-07 20:19:24 +0000224 Return \constant{Py_True} from a function, properly incrementing its
225 reference count.
Skip Montanaro33ee76a2004-07-28 14:17:04 +0000226\versionadded{2.4}
Skip Montanaro6d3db702004-07-29 02:16:04 +0000227\end{csimplemacrodesc}
Skip Montanaro33ee76a2004-07-28 14:17:04 +0000228
Georg Brandl99363b62005-09-03 07:27:26 +0000229\begin{cfuncdesc}{PyObject*}{PyBool_FromLong}{long v}
230 Return a new reference to \constant{Py_True} or \constant{Py_False}
231 depending on the truth value of \var{v}.
Skip Montanaro33ee76a2004-07-28 14:17:04 +0000232\versionadded{2.3}
233\end{cfuncdesc}
Fred Drake3adf79e2001-10-12 19:01:43 +0000234
235\subsection{Long Integer Objects \label{longObjects}}
236
237\obindex{long integer}
238\begin{ctypedesc}{PyLongObject}
239 This subtype of \ctype{PyObject} represents a Python long integer
240 object.
241\end{ctypedesc}
242
243\begin{cvardesc}{PyTypeObject}{PyLong_Type}
244 This instance of \ctype{PyTypeObject} represents the Python long
245 integer type. This is the same object as \code{types.LongType}.
246 \withsubitem{(in modules types)}{\ttindex{LongType}}
247\end{cvardesc}
248
249\begin{cfuncdesc}{int}{PyLong_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +0000250 Return true if its argument is a \ctype{PyLongObject} or a subtype
Fred Drake3adf79e2001-10-12 19:01:43 +0000251 of \ctype{PyLongObject}.
252 \versionchanged[Allowed subtypes to be accepted]{2.2}
253\end{cfuncdesc}
254
255\begin{cfuncdesc}{int}{PyLong_CheckExact}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +0000256 Return true if its argument is a \ctype{PyLongObject}, but not a
Fred Drake3adf79e2001-10-12 19:01:43 +0000257 subtype of \ctype{PyLongObject}.
258 \versionadded{2.2}
259\end{cfuncdesc}
260
261\begin{cfuncdesc}{PyObject*}{PyLong_FromLong}{long v}
Georg Brandl99363b62005-09-03 07:27:26 +0000262 Return a new \ctype{PyLongObject} object from \var{v}, or \NULL{}
Fred Drake3adf79e2001-10-12 19:01:43 +0000263 on failure.
264\end{cfuncdesc}
265
266\begin{cfuncdesc}{PyObject*}{PyLong_FromUnsignedLong}{unsigned long v}
Georg Brandl99363b62005-09-03 07:27:26 +0000267 Return a new \ctype{PyLongObject} object from a C \ctype{unsigned
Fred Drake3adf79e2001-10-12 19:01:43 +0000268 long}, or \NULL{} on failure.
269\end{cfuncdesc}
270
271\begin{cfuncdesc}{PyObject*}{PyLong_FromLongLong}{long long v}
Georg Brandl99363b62005-09-03 07:27:26 +0000272 Return a new \ctype{PyLongObject} object from a C \ctype{long long},
Fred Drake3adf79e2001-10-12 19:01:43 +0000273 or \NULL{} on failure.
274\end{cfuncdesc}
275
276\begin{cfuncdesc}{PyObject*}{PyLong_FromUnsignedLongLong}{unsigned long long v}
Georg Brandl99363b62005-09-03 07:27:26 +0000277 Return a new \ctype{PyLongObject} object from a C \ctype{unsigned
Fred Drake3adf79e2001-10-12 19:01:43 +0000278 long long}, or \NULL{} on failure.
279\end{cfuncdesc}
280
281\begin{cfuncdesc}{PyObject*}{PyLong_FromDouble}{double v}
Georg Brandl99363b62005-09-03 07:27:26 +0000282 Return a new \ctype{PyLongObject} object from the integer part of
Fred Drake3adf79e2001-10-12 19:01:43 +0000283 \var{v}, or \NULL{} on failure.
284\end{cfuncdesc}
285
286\begin{cfuncdesc}{PyObject*}{PyLong_FromString}{char *str, char **pend,
287 int base}
288 Return a new \ctype{PyLongObject} based on the string value in
289 \var{str}, which is interpreted according to the radix in
Tim Peters9ddf40b2004-06-20 22:41:32 +0000290 \var{base}. If \var{pend} is non-\NULL{}, \code{*\var{pend}} will
Fred Drake3adf79e2001-10-12 19:01:43 +0000291 point to the first character in \var{str} which follows the
292 representation of the number. If \var{base} is \code{0}, the radix
Skip Montanaro1ff49a72003-02-03 05:13:24 +0000293 will be determined based on the leading characters of \var{str}: if
Fred Drake3adf79e2001-10-12 19:01:43 +0000294 \var{str} starts with \code{'0x'} or \code{'0X'}, radix 16 will be
295 used; if \var{str} starts with \code{'0'}, radix 8 will be used;
296 otherwise radix 10 will be used. If \var{base} is not \code{0}, it
297 must be between \code{2} and \code{36}, inclusive. Leading spaces
298 are ignored. If there are no digits, \exception{ValueError} will be
299 raised.
300\end{cfuncdesc}
301
302\begin{cfuncdesc}{PyObject*}{PyLong_FromUnicode}{Py_UNICODE *u,
303 int length, int base}
304 Convert a sequence of Unicode digits to a Python long integer
305 value. The first parameter, \var{u}, points to the first character
306 of the Unicode string, \var{length} gives the number of characters,
307 and \var{base} is the radix for the conversion. The radix must be
308 in the range [2, 36]; if it is out of range, \exception{ValueError}
309 will be raised.
310 \versionadded{1.6}
311\end{cfuncdesc}
312
313\begin{cfuncdesc}{PyObject*}{PyLong_FromVoidPtr}{void *p}
314 Create a Python integer or long integer from the pointer \var{p}.
315 The pointer value can be retrieved from the resulting value using
316 \cfunction{PyLong_AsVoidPtr()}.
317 \versionadded{1.5.2}
318\end{cfuncdesc}
319
320\begin{cfuncdesc}{long}{PyLong_AsLong}{PyObject *pylong}
Georg Brandl99363b62005-09-03 07:27:26 +0000321 Return a C \ctype{long} representation of the contents of
Fred Drake3adf79e2001-10-12 19:01:43 +0000322 \var{pylong}. If \var{pylong} is greater than
323 \constant{LONG_MAX}\ttindex{LONG_MAX}, an \exception{OverflowError}
324 is raised.
325 \withsubitem{(built-in exception)}{\ttindex{OverflowError}}
326\end{cfuncdesc}
327
328\begin{cfuncdesc}{unsigned long}{PyLong_AsUnsignedLong}{PyObject *pylong}
Georg Brandl99363b62005-09-03 07:27:26 +0000329 Return a C \ctype{unsigned long} representation of the contents of
Fred Drake3adf79e2001-10-12 19:01:43 +0000330 \var{pylong}. If \var{pylong} is greater than
331 \constant{ULONG_MAX}\ttindex{ULONG_MAX}, an
332 \exception{OverflowError} is raised.
Tim Petersf582b822001-12-11 18:51:08 +0000333 \withsubitem{(built-in exception)}{\ttindex{OverflowError}}
Fred Drake3adf79e2001-10-12 19:01:43 +0000334\end{cfuncdesc}
335
336\begin{cfuncdesc}{long long}{PyLong_AsLongLong}{PyObject *pylong}
337 Return a C \ctype{long long} from a Python long integer. If
338 \var{pylong} cannot be represented as a \ctype{long long}, an
339 \exception{OverflowError} will be raised.
340 \versionadded{2.2}
341\end{cfuncdesc}
342
343\begin{cfuncdesc}{unsigned long long}{PyLong_AsUnsignedLongLong}{PyObject
344 *pylong}
345 Return a C \ctype{unsigned long long} from a Python long integer.
346 If \var{pylong} cannot be represented as an \ctype{unsigned long
347 long}, an \exception{OverflowError} will be raised if the value is
348 positive, or a \exception{TypeError} will be raised if the value is
349 negative.
350 \versionadded{2.2}
351\end{cfuncdesc}
352
Thomas Heller34d7f092003-04-23 19:51:05 +0000353\begin{cfuncdesc}{unsigned long}{PyLong_AsUnsignedLongMask}{PyObject *io}
354 Return a C \ctype{unsigned long} from a Python long integer, without
355 checking for overflow.
356 \versionadded{2.3}
357\end{cfuncdesc}
358
359\begin{cfuncdesc}{unsigned long}{PyLong_AsUnsignedLongLongMask}{PyObject *io}
360 Return a C \ctype{unsigned long long} from a Python long integer, without
361 checking for overflow.
362 \versionadded{2.3}
363\end{cfuncdesc}
364
Fred Drake3adf79e2001-10-12 19:01:43 +0000365\begin{cfuncdesc}{double}{PyLong_AsDouble}{PyObject *pylong}
Georg Brandl99363b62005-09-03 07:27:26 +0000366 Return a C \ctype{double} representation of the contents of
Fred Drake3adf79e2001-10-12 19:01:43 +0000367 \var{pylong}. If \var{pylong} cannot be approximately represented
368 as a \ctype{double}, an \exception{OverflowError} exception is
369 raised and \code{-1.0} will be returned.
370\end{cfuncdesc}
371
372\begin{cfuncdesc}{void*}{PyLong_AsVoidPtr}{PyObject *pylong}
373 Convert a Python integer or long integer \var{pylong} to a C
374 \ctype{void} pointer. If \var{pylong} cannot be converted, an
375 \exception{OverflowError} will be raised. This is only assured to
376 produce a usable \ctype{void} pointer for values created with
377 \cfunction{PyLong_FromVoidPtr()}.
378 \versionadded{1.5.2}
379\end{cfuncdesc}
380
381
382\subsection{Floating Point Objects \label{floatObjects}}
383
384\obindex{floating point}
385\begin{ctypedesc}{PyFloatObject}
386 This subtype of \ctype{PyObject} represents a Python floating point
387 object.
388\end{ctypedesc}
389
390\begin{cvardesc}{PyTypeObject}{PyFloat_Type}
391 This instance of \ctype{PyTypeObject} represents the Python floating
392 point type. This is the same object as \code{types.FloatType}.
393 \withsubitem{(in modules types)}{\ttindex{FloatType}}
394\end{cvardesc}
395
396\begin{cfuncdesc}{int}{PyFloat_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +0000397 Return true if its argument is a \ctype{PyFloatObject} or a subtype
Fred Drake3adf79e2001-10-12 19:01:43 +0000398 of \ctype{PyFloatObject}.
399 \versionchanged[Allowed subtypes to be accepted]{2.2}
400\end{cfuncdesc}
401
402\begin{cfuncdesc}{int}{PyFloat_CheckExact}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +0000403 Return true if its argument is a \ctype{PyFloatObject}, but not a
Fred Drake3adf79e2001-10-12 19:01:43 +0000404 subtype of \ctype{PyFloatObject}.
405 \versionadded{2.2}
406\end{cfuncdesc}
407
Skip Montanaro1ff49a72003-02-03 05:13:24 +0000408\begin{cfuncdesc}{PyObject*}{PyFloat_FromString}{PyObject *str, char **pend}
Georg Brandl99363b62005-09-03 07:27:26 +0000409 Create a \ctype{PyFloatObject} object based on the string value in
Skip Montanaro1ff49a72003-02-03 05:13:24 +0000410 \var{str}, or \NULL{} on failure. The \var{pend} argument is ignored. It
411 remains only for backward compatibility.
Skip Montanaroae31e9b2003-02-03 03:56:36 +0000412\end{cfuncdesc}
413
Fred Drake3adf79e2001-10-12 19:01:43 +0000414\begin{cfuncdesc}{PyObject*}{PyFloat_FromDouble}{double v}
Georg Brandl99363b62005-09-03 07:27:26 +0000415 Create a \ctype{PyFloatObject} object from \var{v}, or \NULL{} on
Fred Drake3adf79e2001-10-12 19:01:43 +0000416 failure.
417\end{cfuncdesc}
418
419\begin{cfuncdesc}{double}{PyFloat_AsDouble}{PyObject *pyfloat}
Georg Brandl99363b62005-09-03 07:27:26 +0000420 Return a C \ctype{double} representation of the contents of
Fred Drake3adf79e2001-10-12 19:01:43 +0000421 \var{pyfloat}.
422\end{cfuncdesc}
423
424\begin{cfuncdesc}{double}{PyFloat_AS_DOUBLE}{PyObject *pyfloat}
Georg Brandl99363b62005-09-03 07:27:26 +0000425 Return a C \ctype{double} representation of the contents of
Fred Drake3adf79e2001-10-12 19:01:43 +0000426 \var{pyfloat}, but without error checking.
427\end{cfuncdesc}
428
429
430\subsection{Complex Number Objects \label{complexObjects}}
431
432\obindex{complex number}
433Python's complex number objects are implemented as two distinct types
434when viewed from the C API: one is the Python object exposed to
435Python programs, and the other is a C structure which represents the
436actual complex number value. The API provides functions for working
437with both.
438
439\subsubsection{Complex Numbers as C Structures}
440
441Note that the functions which accept these structures as parameters
442and return them as results do so \emph{by value} rather than
443dereferencing them through pointers. This is consistent throughout
444the API.
445
446\begin{ctypedesc}{Py_complex}
447 The C structure which corresponds to the value portion of a Python
448 complex number object. Most of the functions for dealing with
449 complex number objects use structures of this type as input or
450 output values, as appropriate. It is defined as:
451
452\begin{verbatim}
453typedef struct {
454 double real;
455 double imag;
456} Py_complex;
457\end{verbatim}
458\end{ctypedesc}
459
460\begin{cfuncdesc}{Py_complex}{_Py_c_sum}{Py_complex left, Py_complex right}
461 Return the sum of two complex numbers, using the C
462 \ctype{Py_complex} representation.
463\end{cfuncdesc}
464
465\begin{cfuncdesc}{Py_complex}{_Py_c_diff}{Py_complex left, Py_complex right}
466 Return the difference between two complex numbers, using the C
467 \ctype{Py_complex} representation.
468\end{cfuncdesc}
469
470\begin{cfuncdesc}{Py_complex}{_Py_c_neg}{Py_complex complex}
471 Return the negation of the complex number \var{complex}, using the C
472 \ctype{Py_complex} representation.
473\end{cfuncdesc}
474
475\begin{cfuncdesc}{Py_complex}{_Py_c_prod}{Py_complex left, Py_complex right}
476 Return the product of two complex numbers, using the C
477 \ctype{Py_complex} representation.
478\end{cfuncdesc}
479
480\begin{cfuncdesc}{Py_complex}{_Py_c_quot}{Py_complex dividend,
481 Py_complex divisor}
482 Return the quotient of two complex numbers, using the C
483 \ctype{Py_complex} representation.
484\end{cfuncdesc}
485
486\begin{cfuncdesc}{Py_complex}{_Py_c_pow}{Py_complex num, Py_complex exp}
487 Return the exponentiation of \var{num} by \var{exp}, using the C
488 \ctype{Py_complex} representation.
489\end{cfuncdesc}
490
491
492\subsubsection{Complex Numbers as Python Objects}
493
494\begin{ctypedesc}{PyComplexObject}
495 This subtype of \ctype{PyObject} represents a Python complex number
496 object.
497\end{ctypedesc}
498
499\begin{cvardesc}{PyTypeObject}{PyComplex_Type}
500 This instance of \ctype{PyTypeObject} represents the Python complex
501 number type.
502\end{cvardesc}
503
504\begin{cfuncdesc}{int}{PyComplex_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +0000505 Return true if its argument is a \ctype{PyComplexObject} or a
Fred Drake3adf79e2001-10-12 19:01:43 +0000506 subtype of \ctype{PyComplexObject}.
507 \versionchanged[Allowed subtypes to be accepted]{2.2}
508\end{cfuncdesc}
509
510\begin{cfuncdesc}{int}{PyComplex_CheckExact}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +0000511 Return true if its argument is a \ctype{PyComplexObject}, but not a
Fred Drake3adf79e2001-10-12 19:01:43 +0000512 subtype of \ctype{PyComplexObject}.
513 \versionadded{2.2}
514\end{cfuncdesc}
515
516\begin{cfuncdesc}{PyObject*}{PyComplex_FromCComplex}{Py_complex v}
517 Create a new Python complex number object from a C
518 \ctype{Py_complex} value.
519\end{cfuncdesc}
520
521\begin{cfuncdesc}{PyObject*}{PyComplex_FromDoubles}{double real, double imag}
Georg Brandl99363b62005-09-03 07:27:26 +0000522 Return a new \ctype{PyComplexObject} object from \var{real} and
Fred Drake3adf79e2001-10-12 19:01:43 +0000523 \var{imag}.
524\end{cfuncdesc}
525
526\begin{cfuncdesc}{double}{PyComplex_RealAsDouble}{PyObject *op}
Georg Brandl99363b62005-09-03 07:27:26 +0000527 Return the real part of \var{op} as a C \ctype{double}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000528\end{cfuncdesc}
529
530\begin{cfuncdesc}{double}{PyComplex_ImagAsDouble}{PyObject *op}
Georg Brandl99363b62005-09-03 07:27:26 +0000531 Return the imaginary part of \var{op} as a C \ctype{double}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000532\end{cfuncdesc}
533
534\begin{cfuncdesc}{Py_complex}{PyComplex_AsCComplex}{PyObject *op}
Georg Brandl99363b62005-09-03 07:27:26 +0000535 Return the \ctype{Py_complex} value of the complex number
Fred Drake3adf79e2001-10-12 19:01:43 +0000536 \var{op}.
537\end{cfuncdesc}
538
539
540
541\section{Sequence Objects \label{sequenceObjects}}
542
543\obindex{sequence}
Tim Petersf582b822001-12-11 18:51:08 +0000544Generic operations on sequence objects were discussed in the previous
545chapter; this section deals with the specific kinds of sequence
Fred Drake3adf79e2001-10-12 19:01:43 +0000546objects that are intrinsic to the Python language.
547
548
549\subsection{String Objects \label{stringObjects}}
550
551These functions raise \exception{TypeError} when expecting a string
552parameter and are called with a non-string parameter.
553
554\obindex{string}
555\begin{ctypedesc}{PyStringObject}
556 This subtype of \ctype{PyObject} represents a Python string object.
557\end{ctypedesc}
558
559\begin{cvardesc}{PyTypeObject}{PyString_Type}
560 This instance of \ctype{PyTypeObject} represents the Python string
561 type; it is the same object as \code{types.TypeType} in the Python
562 layer.
563 \withsubitem{(in module types)}{\ttindex{StringType}}.
564\end{cvardesc}
565
566\begin{cfuncdesc}{int}{PyString_Check}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000567 Return true if the object \var{o} is a string object or an instance
Fred Drake3adf79e2001-10-12 19:01:43 +0000568 of a subtype of the string type.
569 \versionchanged[Allowed subtypes to be accepted]{2.2}
570\end{cfuncdesc}
571
572\begin{cfuncdesc}{int}{PyString_CheckExact}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000573 Return true if the object \var{o} is a string object, but not an
Fred Drake3adf79e2001-10-12 19:01:43 +0000574 instance of a subtype of the string type.
575 \versionadded{2.2}
576\end{cfuncdesc}
577
578\begin{cfuncdesc}{PyObject*}{PyString_FromString}{const char *v}
Georg Brandl99363b62005-09-03 07:27:26 +0000579 Return a new string object with the value \var{v} on success, and
Tim Peters9ddf40b2004-06-20 22:41:32 +0000580 \NULL{} on failure. The parameter \var{v} must not be \NULL{}; it
Fred Drake32a35872001-12-06 20:38:15 +0000581 will not be checked.
Fred Drake3adf79e2001-10-12 19:01:43 +0000582\end{cfuncdesc}
583
584\begin{cfuncdesc}{PyObject*}{PyString_FromStringAndSize}{const char *v,
585 int len}
Georg Brandl99363b62005-09-03 07:27:26 +0000586 Return a new string object with the value \var{v} and length
Fred Drake3adf79e2001-10-12 19:01:43 +0000587 \var{len} on success, and \NULL{} on failure. If \var{v} is
Tim Peters9ddf40b2004-06-20 22:41:32 +0000588 \NULL{}, the contents of the string are uninitialized.
Fred Drake3adf79e2001-10-12 19:01:43 +0000589\end{cfuncdesc}
590
591\begin{cfuncdesc}{PyObject*}{PyString_FromFormat}{const char *format, ...}
Georg Brandl99363b62005-09-03 07:27:26 +0000592 Take a C \cfunction{printf()}-style \var{format} string and a
593 variable number of arguments, calculate the size of the resulting
594 Python string and return a string with the values formatted into
Fred Drake3adf79e2001-10-12 19:01:43 +0000595 it. The variable arguments must be C types and must correspond
596 exactly to the format characters in the \var{format} string. The
597 following format characters are allowed:
598
599 \begin{tableiii}{l|l|l}{member}{Format Characters}{Type}{Comment}
600 \lineiii{\%\%}{\emph{n/a}}{The literal \% character.}
601 \lineiii{\%c}{int}{A single character, represented as an C int.}
602 \lineiii{\%d}{int}{Exactly equivalent to \code{printf("\%d")}.}
603 \lineiii{\%ld}{long}{Exactly equivalent to \code{printf("\%ld")}.}
604 \lineiii{\%i}{int}{Exactly equivalent to \code{printf("\%i")}.}
605 \lineiii{\%x}{int}{Exactly equivalent to \code{printf("\%x")}.}
606 \lineiii{\%s}{char*}{A null-terminated C character array.}
607 \lineiii{\%p}{void*}{The hex representation of a C pointer.
608 Mostly equivalent to \code{printf("\%p")} except that it is
609 guaranteed to start with the literal \code{0x} regardless of
610 what the platform's \code{printf} yields.}
611 \end{tableiii}
612\end{cfuncdesc}
613
614\begin{cfuncdesc}{PyObject*}{PyString_FromFormatV}{const char *format,
615 va_list vargs}
616 Identical to \function{PyString_FromFormat()} except that it takes
617 exactly two arguments.
618\end{cfuncdesc}
619
620\begin{cfuncdesc}{int}{PyString_Size}{PyObject *string}
Georg Brandl99363b62005-09-03 07:27:26 +0000621 Return the length of the string in string object \var{string}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000622\end{cfuncdesc}
623
624\begin{cfuncdesc}{int}{PyString_GET_SIZE}{PyObject *string}
625 Macro form of \cfunction{PyString_Size()} but without error
626 checking.
627\end{cfuncdesc}
628
629\begin{cfuncdesc}{char*}{PyString_AsString}{PyObject *string}
Georg Brandl99363b62005-09-03 07:27:26 +0000630 Return a NUL-terminated representation of the contents of
Fred Drake3adf79e2001-10-12 19:01:43 +0000631 \var{string}. The pointer refers to the internal buffer of
632 \var{string}, not a copy. The data must not be modified in any way,
633 unless the string was just created using
634 \code{PyString_FromStringAndSize(NULL, \var{size})}.
Fred Drake4b247262002-10-22 20:20:20 +0000635 It must not be deallocated. If \var{string} is a Unicode object,
636 this function computes the default encoding of \var{string} and
637 operates on that. If \var{string} is not a string object at all,
638 \cfunction{PyString_AsString()} returns \NULL{} and raises
639 \exception{TypeError}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000640\end{cfuncdesc}
641
642\begin{cfuncdesc}{char*}{PyString_AS_STRING}{PyObject *string}
643 Macro form of \cfunction{PyString_AsString()} but without error
Fred Drake4b247262002-10-22 20:20:20 +0000644 checking. Only string objects are supported; no Unicode objects
645 should be passed.
Fred Drake3adf79e2001-10-12 19:01:43 +0000646\end{cfuncdesc}
647
648\begin{cfuncdesc}{int}{PyString_AsStringAndSize}{PyObject *obj,
649 char **buffer,
650 int *length}
Georg Brandl99363b62005-09-03 07:27:26 +0000651 Return a NUL-terminated representation of the contents of the
Fred Drake3adf79e2001-10-12 19:01:43 +0000652 object \var{obj} through the output variables \var{buffer} and
653 \var{length}.
654
655 The function accepts both string and Unicode objects as input. For
656 Unicode objects it returns the default encoded version of the
Tim Peters9ddf40b2004-06-20 22:41:32 +0000657 object. If \var{length} is \NULL{}, the resulting buffer may not
Fred Drake4b247262002-10-22 20:20:20 +0000658 contain NUL characters; if it does, the function returns \code{-1}
659 and a \exception{TypeError} is raised.
Fred Drake3adf79e2001-10-12 19:01:43 +0000660
661 The buffer refers to an internal string buffer of \var{obj}, not a
662 copy. The data must not be modified in any way, unless the string
663 was just created using \code{PyString_FromStringAndSize(NULL,
Fred Drake4b247262002-10-22 20:20:20 +0000664 \var{size})}. It must not be deallocated. If \var{string} is a
665 Unicode object, this function computes the default encoding of
666 \var{string} and operates on that. If \var{string} is not a string
Georg Brandle53475d2005-09-28 12:53:12 +0000667 object at all, \cfunction{PyString_AsStringAndSize()} returns
668 \code{-1} and raises \exception{TypeError}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000669\end{cfuncdesc}
670
671\begin{cfuncdesc}{void}{PyString_Concat}{PyObject **string,
672 PyObject *newpart}
Georg Brandl99363b62005-09-03 07:27:26 +0000673 Create a new string object in \var{*string} containing the contents
Fred Drake3adf79e2001-10-12 19:01:43 +0000674 of \var{newpart} appended to \var{string}; the caller will own the
675 new reference. The reference to the old value of \var{string} will
676 be stolen. If the new string cannot be created, the old reference
677 to \var{string} will still be discarded and the value of
Tim Peters9ddf40b2004-06-20 22:41:32 +0000678 \var{*string} will be set to \NULL{}; the appropriate exception will
Fred Drake3adf79e2001-10-12 19:01:43 +0000679 be set.
680\end{cfuncdesc}
681
682\begin{cfuncdesc}{void}{PyString_ConcatAndDel}{PyObject **string,
683 PyObject *newpart}
Georg Brandl99363b62005-09-03 07:27:26 +0000684 Create a new string object in \var{*string} containing the contents
Fred Drake3adf79e2001-10-12 19:01:43 +0000685 of \var{newpart} appended to \var{string}. This version decrements
686 the reference count of \var{newpart}.
687\end{cfuncdesc}
688
689\begin{cfuncdesc}{int}{_PyString_Resize}{PyObject **string, int newsize}
690 A way to resize a string object even though it is ``immutable''.
691 Only use this to build up a brand new string object; don't use this
Tim Peters5de98422002-04-27 18:44:32 +0000692 if the string may already be known in other parts of the code. It
693 is an error to call this function if the refcount on the input string
694 object is not one.
695 Pass the address of an existing string object as an lvalue (it may
696 be written into), and the new size desired. On success, \var{*string}
Fred Drake432425e2002-04-29 15:17:16 +0000697 holds the resized string object and \code{0} is returned; the address in
Tim Peters5de98422002-04-27 18:44:32 +0000698 \var{*string} may differ from its input value. If the
699 reallocation fails, the original string object at \var{*string} is
700 deallocated, \var{*string} is set to \NULL{}, a memory exception is set,
Fred Drake432425e2002-04-29 15:17:16 +0000701 and \code{-1} is returned.
Fred Drake3adf79e2001-10-12 19:01:43 +0000702\end{cfuncdesc}
703
704\begin{cfuncdesc}{PyObject*}{PyString_Format}{PyObject *format,
705 PyObject *args}
Georg Brandl99363b62005-09-03 07:27:26 +0000706 Return a new string object from \var{format} and \var{args}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000707 Analogous to \code{\var{format} \%\ \var{args}}. The \var{args}
708 argument must be a tuple.
709\end{cfuncdesc}
710
711\begin{cfuncdesc}{void}{PyString_InternInPlace}{PyObject **string}
712 Intern the argument \var{*string} in place. The argument must be
713 the address of a pointer variable pointing to a Python string
714 object. If there is an existing interned string that is the same as
715 \var{*string}, it sets \var{*string} to it (decrementing the
716 reference count of the old string object and incrementing the
717 reference count of the interned string object), otherwise it leaves
718 \var{*string} alone and interns it (incrementing its reference
719 count). (Clarification: even though there is a lot of talk about
720 reference counts, think of this function as reference-count-neutral;
721 you own the object after the call if and only if you owned it before
722 the call.)
723\end{cfuncdesc}
724
725\begin{cfuncdesc}{PyObject*}{PyString_InternFromString}{const char *v}
726 A combination of \cfunction{PyString_FromString()} and
727 \cfunction{PyString_InternInPlace()}, returning either a new string
728 object that has been interned, or a new (``owned'') reference to an
729 earlier interned string object with the same value.
730\end{cfuncdesc}
731
732\begin{cfuncdesc}{PyObject*}{PyString_Decode}{const char *s,
733 int size,
734 const char *encoding,
735 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +0000736 Create an object by decoding \var{size} bytes of the encoded
Fred Drake3adf79e2001-10-12 19:01:43 +0000737 buffer \var{s} using the codec registered for
738 \var{encoding}. \var{encoding} and \var{errors} have the same
739 meaning as the parameters of the same name in the
740 \function{unicode()} built-in function. The codec to be used is
Georg Brandl99363b62005-09-03 07:27:26 +0000741 looked up using the Python codec registry. Return \NULL{} if
Fred Drake3adf79e2001-10-12 19:01:43 +0000742 an exception was raised by the codec.
743\end{cfuncdesc}
744
745\begin{cfuncdesc}{PyObject*}{PyString_AsDecodedObject}{PyObject *str,
746 const char *encoding,
747 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +0000748 Decode a string object by passing it to the codec registered for
749 \var{encoding} and return the result as Python
Fred Drake3adf79e2001-10-12 19:01:43 +0000750 object. \var{encoding} and \var{errors} have the same meaning as the
751 parameters of the same name in the string \method{encode()} method.
752 The codec to be used is looked up using the Python codec registry.
Georg Brandl99363b62005-09-03 07:27:26 +0000753 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +0000754\end{cfuncdesc}
755
756\begin{cfuncdesc}{PyObject*}{PyString_Encode}{const char *s,
757 int size,
758 const char *encoding,
759 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +0000760 Encode the \ctype{char} buffer of the given size by passing it to
761 the codec registered for \var{encoding} and return a Python object.
Fred Drake3adf79e2001-10-12 19:01:43 +0000762 \var{encoding} and \var{errors} have the same meaning as the
763 parameters of the same name in the string \method{encode()} method.
764 The codec to be used is looked up using the Python codec
Georg Brandl99363b62005-09-03 07:27:26 +0000765 registry. Return \NULL{} if an exception was raised by the
Fred Drake3adf79e2001-10-12 19:01:43 +0000766 codec.
767\end{cfuncdesc}
768
769\begin{cfuncdesc}{PyObject*}{PyString_AsEncodedObject}{PyObject *str,
770 const char *encoding,
771 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +0000772 Encode a string object using the codec registered for
773 \var{encoding} and return the result as Python object.
Fred Drake3adf79e2001-10-12 19:01:43 +0000774 \var{encoding} and \var{errors} have the same meaning as the
775 parameters of the same name in the string \method{encode()} method.
776 The codec to be used is looked up using the Python codec registry.
Georg Brandl99363b62005-09-03 07:27:26 +0000777 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +0000778\end{cfuncdesc}
779
780
781\subsection{Unicode Objects \label{unicodeObjects}}
782\sectionauthor{Marc-Andre Lemburg}{mal@lemburg.com}
783
784%--- Unicode Type -------------------------------------------------------
785
786These are the basic Unicode object types used for the Unicode
787implementation in Python:
788
789\begin{ctypedesc}{Py_UNICODE}
790 This type represents a 16-bit unsigned storage type which is used by
791 Python internally as basis for holding Unicode ordinals. On
792 platforms where \ctype{wchar_t} is available and also has 16-bits,
793 \ctype{Py_UNICODE} is a typedef alias for \ctype{wchar_t} to enhance
794 native platform compatibility. On all other platforms,
795 \ctype{Py_UNICODE} is a typedef alias for \ctype{unsigned short}.
796\end{ctypedesc}
797
798\begin{ctypedesc}{PyUnicodeObject}
799 This subtype of \ctype{PyObject} represents a Python Unicode object.
800\end{ctypedesc}
801
802\begin{cvardesc}{PyTypeObject}{PyUnicode_Type}
803 This instance of \ctype{PyTypeObject} represents the Python Unicode
804 type.
805\end{cvardesc}
806
807The following APIs are really C macros and can be used to do fast
808checks and to access internal read-only data of Unicode objects:
809
810\begin{cfuncdesc}{int}{PyUnicode_Check}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000811 Return true if the object \var{o} is a Unicode object or an
Fred Drake3adf79e2001-10-12 19:01:43 +0000812 instance of a Unicode subtype.
813 \versionchanged[Allowed subtypes to be accepted]{2.2}
814\end{cfuncdesc}
815
816\begin{cfuncdesc}{int}{PyUnicode_CheckExact}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000817 Return true if the object \var{o} is a Unicode object, but not an
Fred Drake3adf79e2001-10-12 19:01:43 +0000818 instance of a subtype.
819 \versionadded{2.2}
820\end{cfuncdesc}
821
822\begin{cfuncdesc}{int}{PyUnicode_GET_SIZE}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000823 Return the size of the object. \var{o} has to be a
Fred Drake3adf79e2001-10-12 19:01:43 +0000824 \ctype{PyUnicodeObject} (not checked).
825\end{cfuncdesc}
826
827\begin{cfuncdesc}{int}{PyUnicode_GET_DATA_SIZE}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000828 Return the size of the object's internal buffer in bytes. \var{o}
Fred Drake3adf79e2001-10-12 19:01:43 +0000829 has to be a \ctype{PyUnicodeObject} (not checked).
830\end{cfuncdesc}
831
832\begin{cfuncdesc}{Py_UNICODE*}{PyUnicode_AS_UNICODE}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000833 Return a pointer to the internal \ctype{Py_UNICODE} buffer of the
Fred Drake3adf79e2001-10-12 19:01:43 +0000834 object. \var{o} has to be a \ctype{PyUnicodeObject} (not checked).
835\end{cfuncdesc}
836
837\begin{cfuncdesc}{const char*}{PyUnicode_AS_DATA}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000838 Return a pointer to the internal buffer of the object.
Fred Drake3adf79e2001-10-12 19:01:43 +0000839 \var{o} has to be a \ctype{PyUnicodeObject} (not checked).
840\end{cfuncdesc}
841
842% --- Unicode character properties ---------------------------------------
843
844Unicode provides many different character properties. The most often
845needed ones are available through these macros which are mapped to C
846functions depending on the Python configuration.
847
848\begin{cfuncdesc}{int}{Py_UNICODE_ISSPACE}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000849 Return 1 or 0 depending on whether \var{ch} is a whitespace
Fred Drake3adf79e2001-10-12 19:01:43 +0000850 character.
851\end{cfuncdesc}
852
853\begin{cfuncdesc}{int}{Py_UNICODE_ISLOWER}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000854 Return 1 or 0 depending on whether \var{ch} is a lowercase character.
Fred Drake3adf79e2001-10-12 19:01:43 +0000855\end{cfuncdesc}
856
857\begin{cfuncdesc}{int}{Py_UNICODE_ISUPPER}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000858 Return 1 or 0 depending on whether \var{ch} is an uppercase
Fred Drake3adf79e2001-10-12 19:01:43 +0000859 character.
860\end{cfuncdesc}
861
862\begin{cfuncdesc}{int}{Py_UNICODE_ISTITLE}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000863 Return 1 or 0 depending on whether \var{ch} is a titlecase character.
Fred Drake3adf79e2001-10-12 19:01:43 +0000864\end{cfuncdesc}
865
866\begin{cfuncdesc}{int}{Py_UNICODE_ISLINEBREAK}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000867 Return 1 or 0 depending on whether \var{ch} is a linebreak character.
Fred Drake3adf79e2001-10-12 19:01:43 +0000868\end{cfuncdesc}
869
870\begin{cfuncdesc}{int}{Py_UNICODE_ISDECIMAL}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000871 Return 1 or 0 depending on whether \var{ch} is a decimal character.
Fred Drake3adf79e2001-10-12 19:01:43 +0000872\end{cfuncdesc}
873
874\begin{cfuncdesc}{int}{Py_UNICODE_ISDIGIT}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000875 Return 1 or 0 depending on whether \var{ch} is a digit character.
Fred Drake3adf79e2001-10-12 19:01:43 +0000876\end{cfuncdesc}
877
878\begin{cfuncdesc}{int}{Py_UNICODE_ISNUMERIC}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000879 Return 1 or 0 depending on whether \var{ch} is a numeric character.
Fred Drake3adf79e2001-10-12 19:01:43 +0000880\end{cfuncdesc}
881
882\begin{cfuncdesc}{int}{Py_UNICODE_ISALPHA}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000883 Return 1 or 0 depending on whether \var{ch} is an alphabetic
Fred Drake3adf79e2001-10-12 19:01:43 +0000884 character.
885\end{cfuncdesc}
886
887\begin{cfuncdesc}{int}{Py_UNICODE_ISALNUM}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000888 Return 1 or 0 depending on whether \var{ch} is an alphanumeric
Fred Drake3adf79e2001-10-12 19:01:43 +0000889 character.
890\end{cfuncdesc}
891
892These APIs can be used for fast direct character conversions:
893
894\begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOLOWER}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000895 Return the character \var{ch} converted to lower case.
Fred Drake3adf79e2001-10-12 19:01:43 +0000896\end{cfuncdesc}
897
898\begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOUPPER}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000899 Return the character \var{ch} converted to upper case.
Fred Drake3adf79e2001-10-12 19:01:43 +0000900\end{cfuncdesc}
901
902\begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOTITLE}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000903 Return the character \var{ch} converted to title case.
Fred Drake3adf79e2001-10-12 19:01:43 +0000904\end{cfuncdesc}
905
906\begin{cfuncdesc}{int}{Py_UNICODE_TODECIMAL}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000907 Return the character \var{ch} converted to a decimal positive
908 integer. Return \code{-1} if this is not possible. This macro
909 does not raise exceptions.
Fred Drake3adf79e2001-10-12 19:01:43 +0000910\end{cfuncdesc}
911
912\begin{cfuncdesc}{int}{Py_UNICODE_TODIGIT}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000913 Return the character \var{ch} converted to a single digit integer.
914 Return \code{-1} if this is not possible. This macro does not raise
Fred Drake3adf79e2001-10-12 19:01:43 +0000915 exceptions.
916\end{cfuncdesc}
917
918\begin{cfuncdesc}{double}{Py_UNICODE_TONUMERIC}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000919 Return the character \var{ch} converted to a (positive) double.
920 Return \code{-1.0} if this is not possible. This macro does not raise
Fred Drake3adf79e2001-10-12 19:01:43 +0000921 exceptions.
922\end{cfuncdesc}
923
924% --- Plain Py_UNICODE ---------------------------------------------------
925
926To create Unicode objects and access their basic sequence properties,
927use these APIs:
928
929\begin{cfuncdesc}{PyObject*}{PyUnicode_FromUnicode}{const Py_UNICODE *u,
Tim Petersf582b822001-12-11 18:51:08 +0000930 int size}
Fred Drake3adf79e2001-10-12 19:01:43 +0000931 Create a Unicode Object from the Py_UNICODE buffer \var{u} of the
932 given size. \var{u} may be \NULL{} which causes the contents to be
933 undefined. It is the user's responsibility to fill in the needed
934 data. The buffer is copied into the new object. If the buffer is
Tim Peters9ddf40b2004-06-20 22:41:32 +0000935 not \NULL{}, the return value might be a shared object. Therefore,
Fred Drake3adf79e2001-10-12 19:01:43 +0000936 modification of the resulting Unicode object is only allowed when
Tim Peters9ddf40b2004-06-20 22:41:32 +0000937 \var{u} is \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000938\end{cfuncdesc}
939
940\begin{cfuncdesc}{Py_UNICODE*}{PyUnicode_AsUnicode}{PyObject *unicode}
941 Return a read-only pointer to the Unicode object's internal
942 \ctype{Py_UNICODE} buffer, \NULL{} if \var{unicode} is not a Unicode
943 object.
944\end{cfuncdesc}
945
946\begin{cfuncdesc}{int}{PyUnicode_GetSize}{PyObject *unicode}
947 Return the length of the Unicode object.
948\end{cfuncdesc}
949
950\begin{cfuncdesc}{PyObject*}{PyUnicode_FromEncodedObject}{PyObject *obj,
951 const char *encoding,
952 const char *errors}
953 Coerce an encoded object \var{obj} to an Unicode object and return a
954 reference with incremented refcount.
955
956 Coercion is done in the following way:
957
958\begin{enumerate}
959\item Unicode objects are passed back as-is with incremented
960 refcount. \note{These cannot be decoded; passing a non-\NULL{}
961 value for encoding will result in a \exception{TypeError}.}
962
963\item String and other char buffer compatible objects are decoded
964 according to the given encoding and using the error handling
965 defined by errors. Both can be \NULL{} to have the interface
966 use the default values (see the next section for details).
967
968\item All other objects cause an exception.
969\end{enumerate}
970
971 The API returns \NULL{} if there was an error. The caller is
972 responsible for decref'ing the returned objects.
973\end{cfuncdesc}
974
975\begin{cfuncdesc}{PyObject*}{PyUnicode_FromObject}{PyObject *obj}
976 Shortcut for \code{PyUnicode_FromEncodedObject(obj, NULL, "strict")}
977 which is used throughout the interpreter whenever coercion to
978 Unicode is needed.
979\end{cfuncdesc}
980
981% --- wchar_t support for platforms which support it ---------------------
982
983If the platform supports \ctype{wchar_t} and provides a header file
984wchar.h, Python can interface directly to this type using the
985following functions. Support is optimized if Python's own
986\ctype{Py_UNICODE} type is identical to the system's \ctype{wchar_t}.
987
988\begin{cfuncdesc}{PyObject*}{PyUnicode_FromWideChar}{const wchar_t *w,
989 int size}
Thomas Heller541703b2002-04-29 17:28:43 +0000990 Create a Unicode object from the \ctype{wchar_t} buffer \var{w} of
Georg Brandl99363b62005-09-03 07:27:26 +0000991 the given size. Return \NULL{} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +0000992\end{cfuncdesc}
993
994\begin{cfuncdesc}{int}{PyUnicode_AsWideChar}{PyUnicodeObject *unicode,
995 wchar_t *w,
996 int size}
Georg Brandl99363b62005-09-03 07:27:26 +0000997 Copy the Unicode object contents into the \ctype{wchar_t} buffer
Marc-André Lemburga9cadcd2004-11-22 13:02:31 +0000998 \var{w}. At most \var{size} \ctype{wchar_t} characters are copied
Georg Brandl99363b62005-09-03 07:27:26 +0000999 (excluding a possibly trailing 0-termination character). Return
Marc-André Lemburga9cadcd2004-11-22 13:02:31 +00001000 the number of \ctype{wchar_t} characters copied or -1 in case of an
1001 error. Note that the resulting \ctype{wchar_t} string may or may
1002 not be 0-terminated. It is the responsibility of the caller to make
1003 sure that the \ctype{wchar_t} string is 0-terminated in case this is
1004 required by the application.
Fred Drake3adf79e2001-10-12 19:01:43 +00001005\end{cfuncdesc}
1006
1007
1008\subsubsection{Built-in Codecs \label{builtinCodecs}}
1009
1010Python provides a set of builtin codecs which are written in C
1011for speed. All of these codecs are directly usable via the
1012following functions.
1013
1014Many of the following APIs take two arguments encoding and
1015errors. These parameters encoding and errors have the same semantics
1016as the ones of the builtin unicode() Unicode object constructor.
1017
1018Setting encoding to \NULL{} causes the default encoding to be used
1019which is \ASCII. The file system calls should use
1020\cdata{Py_FileSystemDefaultEncoding} as the encoding for file
1021names. This variable should be treated as read-only: On some systems,
1022it will be a pointer to a static string, on others, it will change at
Raymond Hettingercb2da432003-10-12 18:24:34 +00001023run-time (such as when the application invokes setlocale).
Fred Drake3adf79e2001-10-12 19:01:43 +00001024
1025Error handling is set by errors which may also be set to \NULL{}
1026meaning to use the default handling defined for the codec. Default
1027error handling for all builtin codecs is ``strict''
1028(\exception{ValueError} is raised).
1029
1030The codecs all use a similar interface. Only deviation from the
1031following generic ones are documented for simplicity.
1032
1033% --- Generic Codecs -----------------------------------------------------
1034
1035These are the generic codec APIs:
1036
1037\begin{cfuncdesc}{PyObject*}{PyUnicode_Decode}{const char *s,
1038 int size,
1039 const char *encoding,
1040 const char *errors}
1041 Create a Unicode object by decoding \var{size} bytes of the encoded
1042 string \var{s}. \var{encoding} and \var{errors} have the same
1043 meaning as the parameters of the same name in the
1044 \function{unicode()} builtin function. The codec to be used is
Georg Brandl99363b62005-09-03 07:27:26 +00001045 looked up using the Python codec registry. Return \NULL{} if an
Fred Drake3adf79e2001-10-12 19:01:43 +00001046 exception was raised by the codec.
1047\end{cfuncdesc}
1048
1049\begin{cfuncdesc}{PyObject*}{PyUnicode_Encode}{const Py_UNICODE *s,
1050 int size,
1051 const char *encoding,
1052 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001053 Encode the \ctype{Py_UNICODE} buffer of the given size and return
Fred Drake3adf79e2001-10-12 19:01:43 +00001054 a Python string object. \var{encoding} and \var{errors} have the
1055 same meaning as the parameters of the same name in the Unicode
1056 \method{encode()} method. The codec to be used is looked up using
Georg Brandl99363b62005-09-03 07:27:26 +00001057 the Python codec registry. Return \NULL{} if an exception was
Fred Drake3adf79e2001-10-12 19:01:43 +00001058 raised by the codec.
1059\end{cfuncdesc}
1060
1061\begin{cfuncdesc}{PyObject*}{PyUnicode_AsEncodedString}{PyObject *unicode,
1062 const char *encoding,
1063 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001064 Encode a Unicode object and return the result as Python string
Fred Drake3adf79e2001-10-12 19:01:43 +00001065 object. \var{encoding} and \var{errors} have the same meaning as the
1066 parameters of the same name in the Unicode \method{encode()} method.
1067 The codec to be used is looked up using the Python codec registry.
Georg Brandl99363b62005-09-03 07:27:26 +00001068 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +00001069\end{cfuncdesc}
1070
1071% --- UTF-8 Codecs -------------------------------------------------------
1072
1073These are the UTF-8 codec APIs:
1074
1075\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF8}{const char *s,
1076 int size,
1077 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001078 Create a Unicode object by decoding \var{size} bytes of the UTF-8
1079 encoded string \var{s}. Return \NULL{} if an exception was raised
Fred Drake3adf79e2001-10-12 19:01:43 +00001080 by the codec.
1081\end{cfuncdesc}
1082
Walter Dörwald69652032004-09-07 20:24:22 +00001083\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF8Stateful}{const char *s,
1084 int size,
1085 const char *errors,
1086 int *consumed}
Georg Brandl99363b62005-09-03 07:27:26 +00001087 If \var{consumed} is \NULL{}, behave like \cfunction{PyUnicode_DecodeUTF8()}.
Walter Dörwald69652032004-09-07 20:24:22 +00001088 If \var{consumed} is not \NULL{}, trailing incomplete UTF-8 byte sequences
1089 will not be treated as an error. Those bytes will not be decoded and the
1090 number of bytes that have been decoded will be stored in \var{consumed}.
1091 \versionadded{2.4}
1092\end{cfuncdesc}
1093
Fred Drake3adf79e2001-10-12 19:01:43 +00001094\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUTF8}{const Py_UNICODE *s,
1095 int size,
1096 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001097 Encode the \ctype{Py_UNICODE} buffer of the given size using UTF-8
1098 and return a Python string object. Return \NULL{} if an exception
Fred Drake3adf79e2001-10-12 19:01:43 +00001099 was raised by the codec.
1100\end{cfuncdesc}
1101
1102\begin{cfuncdesc}{PyObject*}{PyUnicode_AsUTF8String}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001103 Encode a Unicode objects using UTF-8 and return the result as
1104 Python string object. Error handling is ``strict''. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00001105 \NULL{} if an exception was raised by the codec.
1106\end{cfuncdesc}
1107
1108% --- UTF-16 Codecs ------------------------------------------------------ */
1109
1110These are the UTF-16 codec APIs:
1111
1112\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF16}{const char *s,
1113 int size,
1114 const char *errors,
1115 int *byteorder}
Georg Brandl99363b62005-09-03 07:27:26 +00001116 Decode \var{length} bytes from a UTF-16 encoded buffer string and
1117 return the corresponding Unicode object. \var{errors} (if
Tim Peters9ddf40b2004-06-20 22:41:32 +00001118 non-\NULL{}) defines the error handling. It defaults to ``strict''.
Fred Drake3adf79e2001-10-12 19:01:43 +00001119
Tim Peters9ddf40b2004-06-20 22:41:32 +00001120 If \var{byteorder} is non-\NULL{}, the decoder starts decoding using
Fred Drake3adf79e2001-10-12 19:01:43 +00001121 the given byte order:
1122
1123\begin{verbatim}
1124 *byteorder == -1: little endian
1125 *byteorder == 0: native order
1126 *byteorder == 1: big endian
1127\end{verbatim}
1128
1129 and then switches according to all byte order marks (BOM) it finds
1130 in the input data. BOMs are not copied into the resulting Unicode
1131 string. After completion, \var{*byteorder} is set to the current
1132 byte order at the end of input data.
1133
Tim Peters9ddf40b2004-06-20 22:41:32 +00001134 If \var{byteorder} is \NULL{}, the codec starts in native order mode.
Fred Drake3adf79e2001-10-12 19:01:43 +00001135
Georg Brandl99363b62005-09-03 07:27:26 +00001136 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +00001137\end{cfuncdesc}
1138
Walter Dörwald69652032004-09-07 20:24:22 +00001139\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF16Stateful}{const char *s,
1140 int size,
1141 const char *errors,
1142 int *byteorder,
1143 int *consumed}
Georg Brandl99363b62005-09-03 07:27:26 +00001144 If \var{consumed} is \NULL{}, behave like
Walter Dörwald69652032004-09-07 20:24:22 +00001145 \cfunction{PyUnicode_DecodeUTF16()}. If \var{consumed} is not \NULL{},
1146 \cfunction{PyUnicode_DecodeUTF16Stateful()} will not treat trailing incomplete
Raymond Hettinger0c230b92005-08-17 10:05:22 +00001147 UTF-16 byte sequences (such as an odd number of bytes or a split surrogate pair)
Walter Dörwald69652032004-09-07 20:24:22 +00001148 as an error. Those bytes will not be decoded and the number of bytes that
1149 have been decoded will be stored in \var{consumed}.
1150 \versionadded{2.4}
1151\end{cfuncdesc}
1152
Fred Drake3adf79e2001-10-12 19:01:43 +00001153\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUTF16}{const Py_UNICODE *s,
1154 int size,
1155 const char *errors,
1156 int byteorder}
Georg Brandl99363b62005-09-03 07:27:26 +00001157 Return a Python string object holding the UTF-16 encoded value of
Fred Drake3adf79e2001-10-12 19:01:43 +00001158 the Unicode data in \var{s}. If \var{byteorder} is not \code{0},
1159 output is written according to the following byte order:
1160
1161\begin{verbatim}
1162 byteorder == -1: little endian
1163 byteorder == 0: native byte order (writes a BOM mark)
1164 byteorder == 1: big endian
1165\end{verbatim}
1166
1167 If byteorder is \code{0}, the output string will always start with
1168 the Unicode BOM mark (U+FEFF). In the other two modes, no BOM mark
1169 is prepended.
1170
Martin v. Löwis9bc4f2d2004-06-03 09:55:28 +00001171 If \var{Py_UNICODE_WIDE} is defined, a single \ctype{Py_UNICODE}
1172 value may get represented as a surrogate pair. If it is not
1173 defined, each \ctype{Py_UNICODE} values is interpreted as an
1174 UCS-2 character.
Fred Drake3adf79e2001-10-12 19:01:43 +00001175
Georg Brandl99363b62005-09-03 07:27:26 +00001176 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +00001177\end{cfuncdesc}
1178
1179\begin{cfuncdesc}{PyObject*}{PyUnicode_AsUTF16String}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001180 Return a Python string using the UTF-16 encoding in native byte
Fred Drake3adf79e2001-10-12 19:01:43 +00001181 order. The string always starts with a BOM mark. Error handling is
Georg Brandl99363b62005-09-03 07:27:26 +00001182 ``strict''. Return \NULL{} if an exception was raised by the
Fred Drake3adf79e2001-10-12 19:01:43 +00001183 codec.
1184\end{cfuncdesc}
1185
1186% --- Unicode-Escape Codecs ----------------------------------------------
1187
Martin v. Löwis95cf84a2003-10-19 07:32:24 +00001188These are the ``Unicode Escape'' codec APIs:
Fred Drake3adf79e2001-10-12 19:01:43 +00001189
1190\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUnicodeEscape}{const char *s,
1191 int size,
1192 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001193 Create a Unicode object by decoding \var{size} bytes of the
1194 Unicode-Escape encoded string \var{s}. Return \NULL{} if an
Fred Drake3adf79e2001-10-12 19:01:43 +00001195 exception was raised by the codec.
1196\end{cfuncdesc}
1197
1198\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUnicodeEscape}{const Py_UNICODE *s,
1199 int size,
1200 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001201 Encode the \ctype{Py_UNICODE} buffer of the given size using
1202 Unicode-Escape and return a Python string object. Return \NULL{}
Fred Drake3adf79e2001-10-12 19:01:43 +00001203 if an exception was raised by the codec.
1204\end{cfuncdesc}
1205
1206\begin{cfuncdesc}{PyObject*}{PyUnicode_AsUnicodeEscapeString}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001207 Encode a Unicode objects using Unicode-Escape and return the
Fred Drake3adf79e2001-10-12 19:01:43 +00001208 result as Python string object. Error handling is ``strict''.
Georg Brandl99363b62005-09-03 07:27:26 +00001209 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +00001210\end{cfuncdesc}
1211
1212% --- Raw-Unicode-Escape Codecs ------------------------------------------
1213
Martin v. Löwis95cf84a2003-10-19 07:32:24 +00001214These are the ``Raw Unicode Escape'' codec APIs:
Fred Drake3adf79e2001-10-12 19:01:43 +00001215
1216\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeRawUnicodeEscape}{const char *s,
1217 int size,
1218 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001219 Create a Unicode object by decoding \var{size} bytes of the
1220 Raw-Unicode-Escape encoded string \var{s}. Return \NULL{} if an
Fred Drake3adf79e2001-10-12 19:01:43 +00001221 exception was raised by the codec.
1222\end{cfuncdesc}
1223
1224\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeRawUnicodeEscape}{const Py_UNICODE *s,
1225 int size,
1226 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001227 Encode the \ctype{Py_UNICODE} buffer of the given size using
1228 Raw-Unicode-Escape and return a Python string object. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00001229 \NULL{} if an exception was raised by the codec.
1230\end{cfuncdesc}
1231
1232\begin{cfuncdesc}{PyObject*}{PyUnicode_AsRawUnicodeEscapeString}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001233 Encode a Unicode objects using Raw-Unicode-Escape and return the
Fred Drake3adf79e2001-10-12 19:01:43 +00001234 result as Python string object. Error handling is ``strict''.
Georg Brandl99363b62005-09-03 07:27:26 +00001235 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +00001236\end{cfuncdesc}
1237
Tim Petersf582b822001-12-11 18:51:08 +00001238% --- Latin-1 Codecs -----------------------------------------------------
Fred Drake3adf79e2001-10-12 19:01:43 +00001239
1240These are the Latin-1 codec APIs:
1241Latin-1 corresponds to the first 256 Unicode ordinals and only these
1242are accepted by the codecs during encoding.
1243
1244\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeLatin1}{const char *s,
1245 int size,
1246 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001247 Create a Unicode object by decoding \var{size} bytes of the Latin-1
1248 encoded string \var{s}. Return \NULL{} if an exception was raised
Fred Drake3adf79e2001-10-12 19:01:43 +00001249 by the codec.
1250\end{cfuncdesc}
1251
1252\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeLatin1}{const Py_UNICODE *s,
1253 int size,
1254 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001255 Encode the \ctype{Py_UNICODE} buffer of the given size using
1256 Latin-1 and return a Python string object. Return \NULL{} if an
Fred Drake3adf79e2001-10-12 19:01:43 +00001257 exception was raised by the codec.
1258\end{cfuncdesc}
1259
1260\begin{cfuncdesc}{PyObject*}{PyUnicode_AsLatin1String}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001261 Encode a Unicode objects using Latin-1 and return the result as
1262 Python string object. Error handling is ``strict''. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00001263 \NULL{} if an exception was raised by the codec.
1264\end{cfuncdesc}
1265
Tim Petersf582b822001-12-11 18:51:08 +00001266% --- ASCII Codecs -------------------------------------------------------
Fred Drake3adf79e2001-10-12 19:01:43 +00001267
1268These are the \ASCII{} codec APIs. Only 7-bit \ASCII{} data is
1269accepted. All other codes generate errors.
1270
1271\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeASCII}{const char *s,
1272 int size,
1273 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001274 Create a Unicode object by decoding \var{size} bytes of the
1275 \ASCII{} encoded string \var{s}. Return \NULL{} if an exception
Fred Drake3adf79e2001-10-12 19:01:43 +00001276 was raised by the codec.
1277\end{cfuncdesc}
1278
1279\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeASCII}{const Py_UNICODE *s,
1280 int size,
1281 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001282 Encode the \ctype{Py_UNICODE} buffer of the given size using
1283 \ASCII{} and return a Python string object. Return \NULL{} if an
Fred Drake3adf79e2001-10-12 19:01:43 +00001284 exception was raised by the codec.
1285\end{cfuncdesc}
1286
1287\begin{cfuncdesc}{PyObject*}{PyUnicode_AsASCIIString}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001288 Encode a Unicode objects using \ASCII{} and return the result as
1289 Python string object. Error handling is ``strict''. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00001290 \NULL{} if an exception was raised by the codec.
1291\end{cfuncdesc}
1292
Tim Petersf582b822001-12-11 18:51:08 +00001293% --- Character Map Codecs -----------------------------------------------
Fred Drake3adf79e2001-10-12 19:01:43 +00001294
1295These are the mapping codec APIs:
1296
1297This codec is special in that it can be used to implement many
1298different codecs (and this is in fact what was done to obtain most of
1299the standard codecs included in the \module{encodings} package). The
1300codec uses mapping to encode and decode characters.
1301
1302Decoding mappings must map single string characters to single Unicode
1303characters, integers (which are then interpreted as Unicode ordinals)
Tim Petersf582b822001-12-11 18:51:08 +00001304or None (meaning "undefined mapping" and causing an error).
Fred Drake3adf79e2001-10-12 19:01:43 +00001305
1306Encoding mappings must map single Unicode characters to single string
1307characters, integers (which are then interpreted as Latin-1 ordinals)
1308or None (meaning "undefined mapping" and causing an error).
1309
1310The mapping objects provided must only support the __getitem__ mapping
1311interface.
1312
1313If a character lookup fails with a LookupError, the character is
1314copied as-is meaning that its ordinal value will be interpreted as
1315Unicode or Latin-1 ordinal resp. Because of this, mappings only need
1316to contain those mappings which map characters to different code
1317points.
1318
1319\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeCharmap}{const char *s,
1320 int size,
1321 PyObject *mapping,
1322 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001323 Create a Unicode object by decoding \var{size} bytes of the encoded
1324 string \var{s} using the given \var{mapping} object. Return
Walter Dörwaldd1c1e102005-10-06 20:29:57 +00001325 \NULL{} if an exception was raised by the codec. If \var{mapping} is \NULL{}
1326 latin-1 decoding will be done. Else it can be a dictionary mapping byte or a
1327 unicode string, which is treated as a lookup table. Byte values greater
1328 that the length of the string and U+FFFE "characters" are treated as
1329 "undefined mapping".
1330 \versionchanged[Allowed unicode string as mapping argument]{2.4}
Fred Drake3adf79e2001-10-12 19:01:43 +00001331\end{cfuncdesc}
1332
1333\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeCharmap}{const Py_UNICODE *s,
1334 int size,
1335 PyObject *mapping,
1336 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001337 Encode the \ctype{Py_UNICODE} buffer of the given size using the
1338 given \var{mapping} object and return a Python string object.
1339 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +00001340\end{cfuncdesc}
1341
1342\begin{cfuncdesc}{PyObject*}{PyUnicode_AsCharmapString}{PyObject *unicode,
1343 PyObject *mapping}
Georg Brandl99363b62005-09-03 07:27:26 +00001344 Encode a Unicode objects using the given \var{mapping} object and
1345 return the result as Python string object. Error handling is
1346 ``strict''. Return \NULL{} if an exception was raised by the
Fred Drake3adf79e2001-10-12 19:01:43 +00001347 codec.
1348\end{cfuncdesc}
1349
1350The following codec API is special in that maps Unicode to Unicode.
1351
1352\begin{cfuncdesc}{PyObject*}{PyUnicode_TranslateCharmap}{const Py_UNICODE *s,
1353 int size,
1354 PyObject *table,
1355 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001356 Translate a \ctype{Py_UNICODE} buffer of the given length by
1357 applying a character mapping \var{table} to it and return the
1358 resulting Unicode object. Return \NULL{} when an exception was
Fred Drake3adf79e2001-10-12 19:01:43 +00001359 raised by the codec.
1360
1361 The \var{mapping} table must map Unicode ordinal integers to Unicode
1362 ordinal integers or None (causing deletion of the character).
1363
1364 Mapping tables need only provide the method{__getitem__()}
1365 interface; dictionaries and sequences work well. Unmapped character
1366 ordinals (ones which cause a \exception{LookupError}) are left
1367 untouched and are copied as-is.
1368\end{cfuncdesc}
1369
1370% --- MBCS codecs for Windows --------------------------------------------
1371
1372These are the MBCS codec APIs. They are currently only available on
1373Windows and use the Win32 MBCS converters to implement the
1374conversions. Note that MBCS (or DBCS) is a class of encodings, not
1375just one. The target encoding is defined by the user settings on the
1376machine running the codec.
1377
1378\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeMBCS}{const char *s,
1379 int size,
1380 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001381 Create a Unicode object by decoding \var{size} bytes of the MBCS
1382 encoded string \var{s}. Return \NULL{} if an exception was
Fred Drake3adf79e2001-10-12 19:01:43 +00001383 raised by the codec.
1384\end{cfuncdesc}
1385
1386\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeMBCS}{const Py_UNICODE *s,
1387 int size,
1388 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001389 Encode the \ctype{Py_UNICODE} buffer of the given size using MBCS
1390 and return a Python string object. Return \NULL{} if an exception
Fred Drake3adf79e2001-10-12 19:01:43 +00001391 was raised by the codec.
1392\end{cfuncdesc}
1393
1394\begin{cfuncdesc}{PyObject*}{PyUnicode_AsMBCSString}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001395 Encode a Unicode objects using MBCS and return the result as
1396 Python string object. Error handling is ``strict''. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00001397 \NULL{} if an exception was raised by the codec.
1398\end{cfuncdesc}
1399
1400% --- Methods & Slots ----------------------------------------------------
1401
1402\subsubsection{Methods and Slot Functions \label{unicodeMethodsAndSlots}}
1403
1404The following APIs are capable of handling Unicode objects and strings
1405on input (we refer to them as strings in the descriptions) and return
Martin v. Löwis95cf84a2003-10-19 07:32:24 +00001406Unicode objects or integers as appropriate.
Fred Drake3adf79e2001-10-12 19:01:43 +00001407
1408They all return \NULL{} or \code{-1} if an exception occurs.
1409
1410\begin{cfuncdesc}{PyObject*}{PyUnicode_Concat}{PyObject *left,
1411 PyObject *right}
1412 Concat two strings giving a new Unicode string.
1413\end{cfuncdesc}
1414
1415\begin{cfuncdesc}{PyObject*}{PyUnicode_Split}{PyObject *s,
1416 PyObject *sep,
1417 int maxsplit}
Tim Peters9ddf40b2004-06-20 22:41:32 +00001418 Split a string giving a list of Unicode strings. If sep is \NULL{},
Fred Drake3adf79e2001-10-12 19:01:43 +00001419 splitting will be done at all whitespace substrings. Otherwise,
1420 splits occur at the given separator. At most \var{maxsplit} splits
1421 will be done. If negative, no limit is set. Separators are not
1422 included in the resulting list.
1423\end{cfuncdesc}
1424
1425\begin{cfuncdesc}{PyObject*}{PyUnicode_Splitlines}{PyObject *s,
Martin v. Löwis24b88812003-03-30 16:40:42 +00001426 int keepend}
Fred Drake3adf79e2001-10-12 19:01:43 +00001427 Split a Unicode string at line breaks, returning a list of Unicode
Martin v. Löwis24b88812003-03-30 16:40:42 +00001428 strings. CRLF is considered to be one line break. If \var{keepend}
1429 is 0, the Line break characters are not included in the resulting
1430 strings.
Fred Drake3adf79e2001-10-12 19:01:43 +00001431\end{cfuncdesc}
1432
1433\begin{cfuncdesc}{PyObject*}{PyUnicode_Translate}{PyObject *str,
1434 PyObject *table,
1435 const char *errors}
1436 Translate a string by applying a character mapping table to it and
1437 return the resulting Unicode object.
1438
1439 The mapping table must map Unicode ordinal integers to Unicode
1440 ordinal integers or None (causing deletion of the character).
1441
1442 Mapping tables need only provide the \method{__getitem__()}
1443 interface; dictionaries and sequences work well. Unmapped character
1444 ordinals (ones which cause a \exception{LookupError}) are left
1445 untouched and are copied as-is.
1446
1447 \var{errors} has the usual meaning for codecs. It may be \NULL{}
1448 which indicates to use the default error handling.
1449\end{cfuncdesc}
1450
1451\begin{cfuncdesc}{PyObject*}{PyUnicode_Join}{PyObject *separator,
1452 PyObject *seq}
1453 Join a sequence of strings using the given separator and return the
1454 resulting Unicode string.
1455\end{cfuncdesc}
1456
Raymond Hettinger8ef9b3e2004-12-10 17:12:32 +00001457\begin{cfuncdesc}{int}{PyUnicode_Tailmatch}{PyObject *str,
Fred Drake3adf79e2001-10-12 19:01:43 +00001458 PyObject *substr,
1459 int start,
1460 int end,
1461 int direction}
1462 Return 1 if \var{substr} matches \var{str}[\var{start}:\var{end}] at
1463 the given tail end (\var{direction} == -1 means to do a prefix
1464 match, \var{direction} == 1 a suffix match), 0 otherwise.
Georg Brandl99363b62005-09-03 07:27:26 +00001465 Return \code{-1} if an error occurred.
Fred Drake3adf79e2001-10-12 19:01:43 +00001466\end{cfuncdesc}
1467
Fred Drake1d1e1db2002-06-20 22:07:04 +00001468\begin{cfuncdesc}{int}{PyUnicode_Find}{PyObject *str,
1469 PyObject *substr,
1470 int start,
1471 int end,
1472 int direction}
Fred Drake3adf79e2001-10-12 19:01:43 +00001473 Return the first position of \var{substr} in
1474 \var{str}[\var{start}:\var{end}] using the given \var{direction}
1475 (\var{direction} == 1 means to do a forward search,
Fred Drake1d1e1db2002-06-20 22:07:04 +00001476 \var{direction} == -1 a backward search). The return value is the
1477 index of the first match; a value of \code{-1} indicates that no
1478 match was found, and \code{-2} indicates that an error occurred and
1479 an exception has been set.
Fred Drake3adf79e2001-10-12 19:01:43 +00001480\end{cfuncdesc}
1481
Fred Drake1d1e1db2002-06-20 22:07:04 +00001482\begin{cfuncdesc}{int}{PyUnicode_Count}{PyObject *str,
1483 PyObject *substr,
1484 int start,
1485 int end}
1486 Return the number of non-overlapping occurrences of \var{substr} in
Georg Brandl99363b62005-09-03 07:27:26 +00001487 \code{\var{str}[\var{start}:\var{end}]}. Return \code{-1} if an
Fred Drake1d1e1db2002-06-20 22:07:04 +00001488 error occurred.
Fred Drake3adf79e2001-10-12 19:01:43 +00001489\end{cfuncdesc}
1490
1491\begin{cfuncdesc}{PyObject*}{PyUnicode_Replace}{PyObject *str,
1492 PyObject *substr,
1493 PyObject *replstr,
1494 int maxcount}
1495 Replace at most \var{maxcount} occurrences of \var{substr} in
1496 \var{str} with \var{replstr} and return the resulting Unicode object.
1497 \var{maxcount} == -1 means replace all occurrences.
1498\end{cfuncdesc}
1499
1500\begin{cfuncdesc}{int}{PyUnicode_Compare}{PyObject *left, PyObject *right}
1501 Compare two strings and return -1, 0, 1 for less than, equal, and
1502 greater than, respectively.
1503\end{cfuncdesc}
1504
1505\begin{cfuncdesc}{PyObject*}{PyUnicode_Format}{PyObject *format,
1506 PyObject *args}
Georg Brandl99363b62005-09-03 07:27:26 +00001507 Return a new string object from \var{format} and \var{args}; this
Fred Drake3adf79e2001-10-12 19:01:43 +00001508 is analogous to \code{\var{format} \%\ \var{args}}. The
1509 \var{args} argument must be a tuple.
1510\end{cfuncdesc}
1511
1512\begin{cfuncdesc}{int}{PyUnicode_Contains}{PyObject *container,
1513 PyObject *element}
Georg Brandl99363b62005-09-03 07:27:26 +00001514 Check whether \var{element} is contained in \var{container} and
1515 return true or false accordingly.
Fred Drake3adf79e2001-10-12 19:01:43 +00001516
1517 \var{element} has to coerce to a one element Unicode
1518 string. \code{-1} is returned if there was an error.
1519\end{cfuncdesc}
1520
1521
1522\subsection{Buffer Objects \label{bufferObjects}}
1523\sectionauthor{Greg Stein}{gstein@lyra.org}
1524
1525\obindex{buffer}
1526Python objects implemented in C can export a group of functions called
1527the ``buffer\index{buffer interface} interface.'' These functions can
1528be used by an object to expose its data in a raw, byte-oriented
1529format. Clients of the object can use the buffer interface to access
1530the object data directly, without needing to copy it first.
1531
Tim Petersf582b822001-12-11 18:51:08 +00001532Two examples of objects that support
1533the buffer interface are strings and arrays. The string object exposes
Fred Drake3adf79e2001-10-12 19:01:43 +00001534the character contents in the buffer interface's byte-oriented
1535form. An array can also expose its contents, but it should be noted
1536that array elements may be multi-byte values.
1537
1538An example user of the buffer interface is the file object's
1539\method{write()} method. Any object that can export a series of bytes
1540through the buffer interface can be written to a file. There are a
Tim Petersf582b822001-12-11 18:51:08 +00001541number of format codes to \cfunction{PyArg_ParseTuple()} that operate
Fred Drake3adf79e2001-10-12 19:01:43 +00001542against an object's buffer interface, returning data from the target
1543object.
1544
1545More information on the buffer interface is provided in the section
Fred Drake54e62942001-12-11 19:40:16 +00001546``Buffer Object Structures'' (section~\ref{buffer-structs}), under
Fred Drake3adf79e2001-10-12 19:01:43 +00001547the description for \ctype{PyBufferProcs}\ttindex{PyBufferProcs}.
1548
1549A ``buffer object'' is defined in the \file{bufferobject.h} header
1550(included by \file{Python.h}). These objects look very similar to
1551string objects at the Python programming level: they support slicing,
1552indexing, concatenation, and some other standard string
1553operations. However, their data can come from one of two sources: from
1554a block of memory, or from another object which exports the buffer
1555interface.
1556
1557Buffer objects are useful as a way to expose the data from another
1558object's buffer interface to the Python programmer. They can also be
1559used as a zero-copy slicing mechanism. Using their ability to
1560reference a block of memory, it is possible to expose any data to the
1561Python programmer quite easily. The memory could be a large, constant
1562array in a C extension, it could be a raw block of memory for
1563manipulation before passing to an operating system library, or it
1564could be used to pass around structured data in its native, in-memory
1565format.
1566
1567\begin{ctypedesc}{PyBufferObject}
1568 This subtype of \ctype{PyObject} represents a buffer object.
1569\end{ctypedesc}
1570
1571\begin{cvardesc}{PyTypeObject}{PyBuffer_Type}
1572 The instance of \ctype{PyTypeObject} which represents the Python
1573 buffer type; it is the same object as \code{types.BufferType} in the
1574 Python layer.\withsubitem{(in module types)}{\ttindex{BufferType}}.
1575\end{cvardesc}
1576
1577\begin{cvardesc}{int}{Py_END_OF_BUFFER}
1578 This constant may be passed as the \var{size} parameter to
1579 \cfunction{PyBuffer_FromObject()} or
1580 \cfunction{PyBuffer_FromReadWriteObject()}. It indicates that the
1581 new \ctype{PyBufferObject} should refer to \var{base} object from
1582 the specified \var{offset} to the end of its exported buffer. Using
1583 this enables the caller to avoid querying the \var{base} object for
1584 its length.
1585\end{cvardesc}
1586
1587\begin{cfuncdesc}{int}{PyBuffer_Check}{PyObject *p}
1588 Return true if the argument has type \cdata{PyBuffer_Type}.
1589\end{cfuncdesc}
1590
1591\begin{cfuncdesc}{PyObject*}{PyBuffer_FromObject}{PyObject *base,
1592 int offset, int size}
1593 Return a new read-only buffer object. This raises
1594 \exception{TypeError} if \var{base} doesn't support the read-only
1595 buffer protocol or doesn't provide exactly one buffer segment, or it
1596 raises \exception{ValueError} if \var{offset} is less than zero. The
1597 buffer will hold a reference to the \var{base} object, and the
1598 buffer's contents will refer to the \var{base} object's buffer
1599 interface, starting as position \var{offset} and extending for
1600 \var{size} bytes. If \var{size} is \constant{Py_END_OF_BUFFER}, then
1601 the new buffer's contents extend to the length of the \var{base}
1602 object's exported buffer data.
1603\end{cfuncdesc}
1604
1605\begin{cfuncdesc}{PyObject*}{PyBuffer_FromReadWriteObject}{PyObject *base,
1606 int offset,
1607 int size}
1608 Return a new writable buffer object. Parameters and exceptions are
1609 similar to those for \cfunction{PyBuffer_FromObject()}. If the
1610 \var{base} object does not export the writeable buffer protocol,
1611 then \exception{TypeError} is raised.
1612\end{cfuncdesc}
1613
1614\begin{cfuncdesc}{PyObject*}{PyBuffer_FromMemory}{void *ptr, int size}
1615 Return a new read-only buffer object that reads from a specified
1616 location in memory, with a specified size. The caller is
1617 responsible for ensuring that the memory buffer, passed in as
1618 \var{ptr}, is not deallocated while the returned buffer object
1619 exists. Raises \exception{ValueError} if \var{size} is less than
1620 zero. Note that \constant{Py_END_OF_BUFFER} may \emph{not} be
1621 passed for the \var{size} parameter; \exception{ValueError} will be
1622 raised in that case.
1623\end{cfuncdesc}
1624
1625\begin{cfuncdesc}{PyObject*}{PyBuffer_FromReadWriteMemory}{void *ptr, int size}
1626 Similar to \cfunction{PyBuffer_FromMemory()}, but the returned
1627 buffer is writable.
1628\end{cfuncdesc}
1629
1630\begin{cfuncdesc}{PyObject*}{PyBuffer_New}{int size}
Georg Brandl99363b62005-09-03 07:27:26 +00001631 Return a new writable buffer object that maintains its own memory
Fred Drake3adf79e2001-10-12 19:01:43 +00001632 buffer of \var{size} bytes. \exception{ValueError} is returned if
Neil Schemenauerd68d3ee2004-06-08 02:58:50 +00001633 \var{size} is not zero or positive. Note that the memory buffer (as
1634 returned by \cfunction{PyObject_AsWriteBuffer()}) is not specifically
1635 aligned.
Fred Drake3adf79e2001-10-12 19:01:43 +00001636\end{cfuncdesc}
1637
1638
1639\subsection{Tuple Objects \label{tupleObjects}}
1640
1641\obindex{tuple}
1642\begin{ctypedesc}{PyTupleObject}
1643 This subtype of \ctype{PyObject} represents a Python tuple object.
1644\end{ctypedesc}
1645
1646\begin{cvardesc}{PyTypeObject}{PyTuple_Type}
1647 This instance of \ctype{PyTypeObject} represents the Python tuple
1648 type; it is the same object as \code{types.TupleType} in the Python
1649 layer.\withsubitem{(in module types)}{\ttindex{TupleType}}.
1650\end{cvardesc}
1651
1652\begin{cfuncdesc}{int}{PyTuple_Check}{PyObject *p}
1653 Return true if \var{p} is a tuple object or an instance of a subtype
1654 of the tuple type.
1655 \versionchanged[Allowed subtypes to be accepted]{2.2}
1656\end{cfuncdesc}
1657
1658\begin{cfuncdesc}{int}{PyTuple_CheckExact}{PyObject *p}
1659 Return true if \var{p} is a tuple object, but not an instance of a
1660 subtype of the tuple type.
1661 \versionadded{2.2}
1662\end{cfuncdesc}
1663
1664\begin{cfuncdesc}{PyObject*}{PyTuple_New}{int len}
1665 Return a new tuple object of size \var{len}, or \NULL{} on failure.
1666\end{cfuncdesc}
1667
Raymond Hettingercb2da432003-10-12 18:24:34 +00001668\begin{cfuncdesc}{PyObject*}{PyTuple_Pack}{int n, \moreargs}
1669 Return a new tuple object of size \var{n}, or \NULL{} on failure.
1670 The tuple values are initialized to the subsequent \var{n} C arguments
1671 pointing to Python objects. \samp{PyTuple_Pack(2, \var{a}, \var{b})}
1672 is equivalent to \samp{Py_BuildValue("(OO)", \var{a}, \var{b})}.
Tim Peters9ddf40b2004-06-20 22:41:32 +00001673 \versionadded{2.4}
Raymond Hettingercb2da432003-10-12 18:24:34 +00001674\end{cfuncdesc}
1675
Fred Drake3adf79e2001-10-12 19:01:43 +00001676\begin{cfuncdesc}{int}{PyTuple_Size}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00001677 Take a pointer to a tuple object, and return the size of that
Fred Drake3adf79e2001-10-12 19:01:43 +00001678 tuple.
1679\end{cfuncdesc}
1680
1681\begin{cfuncdesc}{int}{PyTuple_GET_SIZE}{PyObject *p}
1682 Return the size of the tuple \var{p}, which must be non-\NULL{} and
1683 point to a tuple; no error checking is performed.
1684\end{cfuncdesc}
1685
1686\begin{cfuncdesc}{PyObject*}{PyTuple_GetItem}{PyObject *p, int pos}
Georg Brandl99363b62005-09-03 07:27:26 +00001687 Return the object at position \var{pos} in the tuple pointed to by
1688 \var{p}. If \var{pos} is out of bounds, return \NULL{} and sets an
Fred Drake3adf79e2001-10-12 19:01:43 +00001689 \exception{IndexError} exception.
1690\end{cfuncdesc}
1691
1692\begin{cfuncdesc}{PyObject*}{PyTuple_GET_ITEM}{PyObject *p, int pos}
1693 Like \cfunction{PyTuple_GetItem()}, but does no checking of its
1694 arguments.
1695\end{cfuncdesc}
1696
1697\begin{cfuncdesc}{PyObject*}{PyTuple_GetSlice}{PyObject *p,
1698 int low, int high}
Georg Brandl99363b62005-09-03 07:27:26 +00001699 Take a slice of the tuple pointed to by \var{p} from \var{low} to
1700 \var{high} and return it as a new tuple.
Fred Drake3adf79e2001-10-12 19:01:43 +00001701\end{cfuncdesc}
1702
1703\begin{cfuncdesc}{int}{PyTuple_SetItem}{PyObject *p,
1704 int pos, PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +00001705 Insert a reference to object \var{o} at position \var{pos} of the
1706 tuple pointed to by \var{p}. Return \code{0} on success.
Fred Drake3adf79e2001-10-12 19:01:43 +00001707 \note{This function ``steals'' a reference to \var{o}.}
1708\end{cfuncdesc}
1709
1710\begin{cfuncdesc}{void}{PyTuple_SET_ITEM}{PyObject *p,
1711 int pos, PyObject *o}
1712 Like \cfunction{PyTuple_SetItem()}, but does no error checking, and
1713 should \emph{only} be used to fill in brand new tuples. \note{This
1714 function ``steals'' a reference to \var{o}.}
1715\end{cfuncdesc}
1716
1717\begin{cfuncdesc}{int}{_PyTuple_Resize}{PyObject **p, int newsize}
1718 Can be used to resize a tuple. \var{newsize} will be the new length
1719 of the tuple. Because tuples are \emph{supposed} to be immutable,
1720 this should only be used if there is only one reference to the
1721 object. Do \emph{not} use this if the tuple may already be known to
1722 some other part of the code. The tuple will always grow or shrink
1723 at the end. Think of this as destroying the old tuple and creating
1724 a new one, only more efficiently. Returns \code{0} on success.
1725 Client code should never assume that the resulting value of
1726 \code{*\var{p}} will be the same as before calling this function.
1727 If the object referenced by \code{*\var{p}} is replaced, the
1728 original \code{*\var{p}} is destroyed. On failure, returns
Tim Peters9ddf40b2004-06-20 22:41:32 +00001729 \code{-1} and sets \code{*\var{p}} to \NULL{}, and raises
Fred Drake3adf79e2001-10-12 19:01:43 +00001730 \exception{MemoryError} or
1731 \exception{SystemError}.
Tim Petersf582b822001-12-11 18:51:08 +00001732 \versionchanged[Removed unused third parameter, \var{last_is_sticky}]{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +00001733\end{cfuncdesc}
1734
1735
1736\subsection{List Objects \label{listObjects}}
1737
1738\obindex{list}
1739\begin{ctypedesc}{PyListObject}
1740 This subtype of \ctype{PyObject} represents a Python list object.
1741\end{ctypedesc}
1742
1743\begin{cvardesc}{PyTypeObject}{PyList_Type}
1744 This instance of \ctype{PyTypeObject} represents the Python list
1745 type. This is the same object as \code{types.ListType}.
1746 \withsubitem{(in module types)}{\ttindex{ListType}}
1747\end{cvardesc}
1748
1749\begin{cfuncdesc}{int}{PyList_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00001750 Return true if \var{p} is a list object or an instance of a
Andrew MacIntyre13cd8892003-12-25 23:57:52 +00001751 subtype of the list type.
1752 \versionchanged[Allowed subtypes to be accepted]{2.2}
1753\end{cfuncdesc}
1754
1755\begin{cfuncdesc}{int}{PyList_CheckExact}{PyObject *p}
1756 Return true if \var{p} is a list object, but not an instance of a
1757 subtype of the list type.
1758 \versionadded{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +00001759\end{cfuncdesc}
1760
1761\begin{cfuncdesc}{PyObject*}{PyList_New}{int len}
Georg Brandl99363b62005-09-03 07:27:26 +00001762 Return a new list of length \var{len} on success, or \NULL{} on
Fred Drake3adf79e2001-10-12 19:01:43 +00001763 failure.
1764\end{cfuncdesc}
1765
1766\begin{cfuncdesc}{int}{PyList_Size}{PyObject *list}
Georg Brandl99363b62005-09-03 07:27:26 +00001767 Return the length of the list object in \var{list}; this is
Fred Drake3adf79e2001-10-12 19:01:43 +00001768 equivalent to \samp{len(\var{list})} on a list object.
1769 \bifuncindex{len}
1770\end{cfuncdesc}
1771
1772\begin{cfuncdesc}{int}{PyList_GET_SIZE}{PyObject *list}
1773 Macro form of \cfunction{PyList_Size()} without error checking.
1774\end{cfuncdesc}
1775
1776\begin{cfuncdesc}{PyObject*}{PyList_GetItem}{PyObject *list, int index}
Georg Brandl99363b62005-09-03 07:27:26 +00001777 Return the object at position \var{pos} in the list pointed to by
1778 \var{p}. If \var{pos} is out of bounds, return \NULL{} and set an
Fred Drake3adf79e2001-10-12 19:01:43 +00001779 \exception{IndexError} exception.
1780\end{cfuncdesc}
1781
1782\begin{cfuncdesc}{PyObject*}{PyList_GET_ITEM}{PyObject *list, int i}
1783 Macro form of \cfunction{PyList_GetItem()} without error checking.
1784\end{cfuncdesc}
1785
1786\begin{cfuncdesc}{int}{PyList_SetItem}{PyObject *list, int index,
1787 PyObject *item}
Georg Brandl99363b62005-09-03 07:27:26 +00001788 Set the item at index \var{index} in list to \var{item}. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00001789 \code{0} on success or \code{-1} on failure. \note{This function
1790 ``steals'' a reference to \var{item} and discards a reference to an
1791 item already in the list at the affected position.}
1792\end{cfuncdesc}
1793
1794\begin{cfuncdesc}{void}{PyList_SET_ITEM}{PyObject *list, int i,
1795 PyObject *o}
1796 Macro form of \cfunction{PyList_SetItem()} without error checking.
1797 This is normally only used to fill in new lists where there is no
1798 previous content.
1799 \note{This function ``steals'' a reference to \var{item}, and,
1800 unlike \cfunction{PyList_SetItem()}, does \emph{not} discard a
1801 reference to any item that it being replaced; any reference in
1802 \var{list} at position \var{i} will be leaked.}
1803\end{cfuncdesc}
1804
1805\begin{cfuncdesc}{int}{PyList_Insert}{PyObject *list, int index,
1806 PyObject *item}
Georg Brandl99363b62005-09-03 07:27:26 +00001807 Insert the item \var{item} into list \var{list} in front of index
1808 \var{index}. Return \code{0} if successful; return \code{-1} and
1809 set an exception if unsuccessful. Analogous to
Fred Drake3adf79e2001-10-12 19:01:43 +00001810 \code{\var{list}.insert(\var{index}, \var{item})}.
1811\end{cfuncdesc}
1812
1813\begin{cfuncdesc}{int}{PyList_Append}{PyObject *list, PyObject *item}
Georg Brandl99363b62005-09-03 07:27:26 +00001814 Append the object \var{item} at the end of list \var{list}.
1815 Return \code{0} if successful; return \code{-1} and set an
Fred Drake3adf79e2001-10-12 19:01:43 +00001816 exception if unsuccessful. Analogous to
1817 \code{\var{list}.append(\var{item})}.
1818\end{cfuncdesc}
1819
1820\begin{cfuncdesc}{PyObject*}{PyList_GetSlice}{PyObject *list,
1821 int low, int high}
Georg Brandl99363b62005-09-03 07:27:26 +00001822 Return a list of the objects in \var{list} containing the objects
1823 \emph{between} \var{low} and \var{high}. Return \NULL{} and set
Fred Drake3adf79e2001-10-12 19:01:43 +00001824 an exception if unsuccessful.
1825 Analogous to \code{\var{list}[\var{low}:\var{high}]}.
1826\end{cfuncdesc}
1827
1828\begin{cfuncdesc}{int}{PyList_SetSlice}{PyObject *list,
1829 int low, int high,
1830 PyObject *itemlist}
Georg Brandl99363b62005-09-03 07:27:26 +00001831 Set the slice of \var{list} between \var{low} and \var{high} to the
Fred Drake3adf79e2001-10-12 19:01:43 +00001832 contents of \var{itemlist}. Analogous to
Raymond Hettinger9c7ed4c2003-10-26 17:20:07 +00001833 \code{\var{list}[\var{low}:\var{high}] = \var{itemlist}}.
1834 The \var{itemlist} may be \NULL{}, indicating the assignment
1835 of an empty list (slice deletion).
Georg Brandl99363b62005-09-03 07:27:26 +00001836 Return \code{0} on success, \code{-1} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00001837\end{cfuncdesc}
1838
1839\begin{cfuncdesc}{int}{PyList_Sort}{PyObject *list}
Georg Brandl99363b62005-09-03 07:27:26 +00001840 Sort the items of \var{list} in place. Return \code{0} on
Fred Drake3adf79e2001-10-12 19:01:43 +00001841 success, \code{-1} on failure. This is equivalent to
1842 \samp{\var{list}.sort()}.
1843\end{cfuncdesc}
1844
1845\begin{cfuncdesc}{int}{PyList_Reverse}{PyObject *list}
Georg Brandl99363b62005-09-03 07:27:26 +00001846 Reverse the items of \var{list} in place. Return \code{0} on
Fred Drake3adf79e2001-10-12 19:01:43 +00001847 success, \code{-1} on failure. This is the equivalent of
1848 \samp{\var{list}.reverse()}.
1849\end{cfuncdesc}
1850
1851\begin{cfuncdesc}{PyObject*}{PyList_AsTuple}{PyObject *list}
Georg Brandl99363b62005-09-03 07:27:26 +00001852 Return a new tuple object containing the contents of \var{list};
Fred Drake3adf79e2001-10-12 19:01:43 +00001853 equivalent to \samp{tuple(\var{list})}.\bifuncindex{tuple}
1854\end{cfuncdesc}
1855
1856
1857\section{Mapping Objects \label{mapObjects}}
1858
1859\obindex{mapping}
1860
1861
1862\subsection{Dictionary Objects \label{dictObjects}}
1863
1864\obindex{dictionary}
1865\begin{ctypedesc}{PyDictObject}
1866 This subtype of \ctype{PyObject} represents a Python dictionary
1867 object.
1868\end{ctypedesc}
1869
1870\begin{cvardesc}{PyTypeObject}{PyDict_Type}
1871 This instance of \ctype{PyTypeObject} represents the Python
1872 dictionary type. This is exposed to Python programs as
1873 \code{types.DictType} and \code{types.DictionaryType}.
1874 \withsubitem{(in module types)}{\ttindex{DictType}\ttindex{DictionaryType}}
1875\end{cvardesc}
1876
1877\begin{cfuncdesc}{int}{PyDict_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00001878 Return true if \var{p} is a dict object or an instance of a
Andrew MacIntyre13cd8892003-12-25 23:57:52 +00001879 subtype of the dict type.
1880 \versionchanged[Allowed subtypes to be accepted]{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +00001881\end{cfuncdesc}
1882
Andrew MacIntyref72af652003-12-26 00:07:51 +00001883\begin{cfuncdesc}{int}{PyDict_CheckExact}{PyObject *p}
1884 Return true if \var{p} is a dict object, but not an instance of a
1885 subtype of the dict type.
1886 \versionadded{2.4}
1887\end{cfuncdesc}
1888
Fred Drake3adf79e2001-10-12 19:01:43 +00001889\begin{cfuncdesc}{PyObject*}{PyDict_New}{}
Georg Brandl99363b62005-09-03 07:27:26 +00001890 Return a new empty dictionary, or \NULL{} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00001891\end{cfuncdesc}
1892
1893\begin{cfuncdesc}{PyObject*}{PyDictProxy_New}{PyObject *dict}
1894 Return a proxy object for a mapping which enforces read-only
1895 behavior. This is normally used to create a proxy to prevent
1896 modification of the dictionary for non-dynamic class types.
1897 \versionadded{2.2}
1898\end{cfuncdesc}
1899
1900\begin{cfuncdesc}{void}{PyDict_Clear}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00001901 Empty an existing dictionary of all key-value pairs.
Fred Drake3adf79e2001-10-12 19:01:43 +00001902\end{cfuncdesc}
1903
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00001904\begin{cfuncdesc}{int}{PyDict_Contains}{PyObject *p, PyObject *key}
1905 Determine if dictionary \var{p} contains \var{key}. If an item
1906 in \var{p} is matches \var{key}, return \code{1}, otherwise return
1907 \code{0}. On error, return \code{-1}. This is equivalent to the
1908 Python expression \samp{\var{key} in \var{p}}.
Tim Peters9ddf40b2004-06-20 22:41:32 +00001909 \versionadded{2.4}
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00001910\end{cfuncdesc}
1911
Fred Drake3adf79e2001-10-12 19:01:43 +00001912\begin{cfuncdesc}{PyObject*}{PyDict_Copy}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00001913 Return a new dictionary that contains the same key-value pairs as
Fred Drake3adf79e2001-10-12 19:01:43 +00001914 \var{p}.
1915 \versionadded{1.6}
1916\end{cfuncdesc}
1917
1918\begin{cfuncdesc}{int}{PyDict_SetItem}{PyObject *p, PyObject *key,
1919 PyObject *val}
Georg Brandl99363b62005-09-03 07:27:26 +00001920 Insert \var{value} into the dictionary \var{p} with a key of
Fred Drake3adf79e2001-10-12 19:01:43 +00001921 \var{key}. \var{key} must be hashable; if it isn't,
1922 \exception{TypeError} will be raised.
Georg Brandl99363b62005-09-03 07:27:26 +00001923 Return \code{0} on success or \code{-1} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00001924\end{cfuncdesc}
1925
1926\begin{cfuncdesc}{int}{PyDict_SetItemString}{PyObject *p,
1927 char *key,
1928 PyObject *val}
Georg Brandl99363b62005-09-03 07:27:26 +00001929 Insert \var{value} into the dictionary \var{p} using \var{key} as a
Fred Drake3adf79e2001-10-12 19:01:43 +00001930 key. \var{key} should be a \ctype{char*}. The key object is created
Georg Brandl99363b62005-09-03 07:27:26 +00001931 using \code{PyString_FromString(\var{key})}. Return \code{0} on
Fred Drake3adf79e2001-10-12 19:01:43 +00001932 success or \code{-1} on failure.
1933 \ttindex{PyString_FromString()}
1934\end{cfuncdesc}
1935
1936\begin{cfuncdesc}{int}{PyDict_DelItem}{PyObject *p, PyObject *key}
Georg Brandl99363b62005-09-03 07:27:26 +00001937 Remove the entry in dictionary \var{p} with key \var{key}.
Fred Drake3adf79e2001-10-12 19:01:43 +00001938 \var{key} must be hashable; if it isn't, \exception{TypeError} is
Georg Brandl99363b62005-09-03 07:27:26 +00001939 raised. Return \code{0} on success or \code{-1} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00001940\end{cfuncdesc}
1941
1942\begin{cfuncdesc}{int}{PyDict_DelItemString}{PyObject *p, char *key}
Georg Brandl99363b62005-09-03 07:27:26 +00001943 Remove the entry in dictionary \var{p} which has a key specified by
1944 the string \var{key}. Return \code{0} on success or \code{-1} on
Fred Drake3adf79e2001-10-12 19:01:43 +00001945 failure.
1946\end{cfuncdesc}
1947
1948\begin{cfuncdesc}{PyObject*}{PyDict_GetItem}{PyObject *p, PyObject *key}
Georg Brandl99363b62005-09-03 07:27:26 +00001949 Return the object from dictionary \var{p} which has a key
1950 \var{key}. Return \NULL{} if the key \var{key} is not present, but
Fred Drake3adf79e2001-10-12 19:01:43 +00001951 \emph{without} setting an exception.
1952\end{cfuncdesc}
1953
1954\begin{cfuncdesc}{PyObject*}{PyDict_GetItemString}{PyObject *p, char *key}
1955 This is the same as \cfunction{PyDict_GetItem()}, but \var{key} is
1956 specified as a \ctype{char*}, rather than a \ctype{PyObject*}.
1957\end{cfuncdesc}
1958
1959\begin{cfuncdesc}{PyObject*}{PyDict_Items}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00001960 Return a \ctype{PyListObject} containing all the items from the
Nicholas Bastin975e7252004-09-29 21:39:26 +00001961 dictionary, as in the dictionary method \method{items()} (see the
Fred Drake3adf79e2001-10-12 19:01:43 +00001962 \citetitle[../lib/lib.html]{Python Library Reference}).
1963\end{cfuncdesc}
1964
1965\begin{cfuncdesc}{PyObject*}{PyDict_Keys}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00001966 Return a \ctype{PyListObject} containing all the keys from the
Fred Drake3adf79e2001-10-12 19:01:43 +00001967 dictionary, as in the dictionary method \method{keys()} (see the
1968 \citetitle[../lib/lib.html]{Python Library Reference}).
1969\end{cfuncdesc}
1970
1971\begin{cfuncdesc}{PyObject*}{PyDict_Values}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00001972 Return a \ctype{PyListObject} containing all the values from the
Fred Drake3adf79e2001-10-12 19:01:43 +00001973 dictionary \var{p}, as in the dictionary method \method{values()}
1974 (see the \citetitle[../lib/lib.html]{Python Library Reference}).
1975\end{cfuncdesc}
1976
1977\begin{cfuncdesc}{int}{PyDict_Size}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00001978 Return the number of items in the dictionary. This is equivalent
Fred Drake3adf79e2001-10-12 19:01:43 +00001979 to \samp{len(\var{p})} on a dictionary.\bifuncindex{len}
1980\end{cfuncdesc}
1981
1982\begin{cfuncdesc}{int}{PyDict_Next}{PyObject *p, int *ppos,
1983 PyObject **pkey, PyObject **pvalue}
1984 Iterate over all key-value pairs in the dictionary \var{p}. The
1985 \ctype{int} referred to by \var{ppos} must be initialized to
1986 \code{0} prior to the first call to this function to start the
1987 iteration; the function returns true for each pair in the
1988 dictionary, and false once all pairs have been reported. The
1989 parameters \var{pkey} and \var{pvalue} should either point to
1990 \ctype{PyObject*} variables that will be filled in with each key and
Tim Peters9ddf40b2004-06-20 22:41:32 +00001991 value, respectively, or may be \NULL{}. Any references returned through
Raymond Hettinger54693242003-12-13 19:48:41 +00001992 them are borrowed. \var{ppos} should not be altered during iteration.
1993 Its value represents offsets within the internal dictionary structure,
1994 and since the structure is sparse, the offsets are not consecutive.
Fred Drake3adf79e2001-10-12 19:01:43 +00001995
1996 For example:
1997
1998\begin{verbatim}
1999PyObject *key, *value;
2000int pos = 0;
2001
2002while (PyDict_Next(self->dict, &pos, &key, &value)) {
2003 /* do something interesting with the values... */
2004 ...
2005}
2006\end{verbatim}
2007
2008 The dictionary \var{p} should not be mutated during iteration. It
2009 is safe (since Python 2.1) to modify the values of the keys as you
2010 iterate over the dictionary, but only so long as the set of keys
2011 does not change. For example:
2012
2013\begin{verbatim}
2014PyObject *key, *value;
2015int pos = 0;
2016
2017while (PyDict_Next(self->dict, &pos, &key, &value)) {
2018 int i = PyInt_AS_LONG(value) + 1;
2019 PyObject *o = PyInt_FromLong(i);
2020 if (o == NULL)
2021 return -1;
2022 if (PyDict_SetItem(self->dict, key, o) < 0) {
2023 Py_DECREF(o);
2024 return -1;
2025 }
2026 Py_DECREF(o);
2027}
2028\end{verbatim}
2029\end{cfuncdesc}
2030
2031\begin{cfuncdesc}{int}{PyDict_Merge}{PyObject *a, PyObject *b, int override}
Tim Petersf582b822001-12-11 18:51:08 +00002032 Iterate over mapping object \var{b} adding key-value pairs to dictionary
2033 \var{a}.
2034 \var{b} may be a dictionary, or any object supporting
2035 \function{PyMapping_Keys()} and \function{PyObject_GetItem()}.
2036 If \var{override} is true, existing pairs in \var{a} will
Fred Drake3adf79e2001-10-12 19:01:43 +00002037 be replaced if a matching key is found in \var{b}, otherwise pairs
2038 will only be added if there is not a matching key in \var{a}.
Tim Petersf582b822001-12-11 18:51:08 +00002039 Return \code{0} on success or \code{-1} if an exception was
Fred Drake3adf79e2001-10-12 19:01:43 +00002040 raised.
2041\versionadded{2.2}
2042\end{cfuncdesc}
2043
2044\begin{cfuncdesc}{int}{PyDict_Update}{PyObject *a, PyObject *b}
2045 This is the same as \code{PyDict_Merge(\var{a}, \var{b}, 1)} in C,
Tim Petersf582b822001-12-11 18:51:08 +00002046 or \code{\var{a}.update(\var{b})} in Python. Return \code{0} on
Fred Drake3adf79e2001-10-12 19:01:43 +00002047 success or \code{-1} if an exception was raised.
2048 \versionadded{2.2}
2049\end{cfuncdesc}
2050
Tim Petersf582b822001-12-11 18:51:08 +00002051\begin{cfuncdesc}{int}{PyDict_MergeFromSeq2}{PyObject *a, PyObject *seq2,
2052 int override}
2053 Update or merge into dictionary \var{a}, from the key-value pairs in
2054 \var{seq2}. \var{seq2} must be an iterable object producing
2055 iterable objects of length 2, viewed as key-value pairs. In case of
2056 duplicate keys, the last wins if \var{override} is true, else the
2057 first wins.
2058 Return \code{0} on success or \code{-1} if an exception
2059 was raised.
2060 Equivalent Python (except for the return value):
2061
2062\begin{verbatim}
2063def PyDict_MergeFromSeq2(a, seq2, override):
2064 for key, value in seq2:
2065 if override or key not in a:
2066 a[key] = value
2067\end{verbatim}
2068
2069 \versionadded{2.2}
2070\end{cfuncdesc}
Fred Drake3adf79e2001-10-12 19:01:43 +00002071
Fred Drake54e62942001-12-11 19:40:16 +00002072
Fred Drake3adf79e2001-10-12 19:01:43 +00002073\section{Other Objects \label{otherObjects}}
2074
2075\subsection{File Objects \label{fileObjects}}
2076
2077\obindex{file}
2078Python's built-in file objects are implemented entirely on the
2079\ctype{FILE*} support from the C standard library. This is an
2080implementation detail and may change in future releases of Python.
2081
2082\begin{ctypedesc}{PyFileObject}
2083 This subtype of \ctype{PyObject} represents a Python file object.
2084\end{ctypedesc}
2085
2086\begin{cvardesc}{PyTypeObject}{PyFile_Type}
2087 This instance of \ctype{PyTypeObject} represents the Python file
2088 type. This is exposed to Python programs as \code{types.FileType}.
2089 \withsubitem{(in module types)}{\ttindex{FileType}}
2090\end{cvardesc}
2091
2092\begin{cfuncdesc}{int}{PyFile_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002093 Return true if its argument is a \ctype{PyFileObject} or a subtype
Fred Drake3adf79e2001-10-12 19:01:43 +00002094 of \ctype{PyFileObject}.
2095 \versionchanged[Allowed subtypes to be accepted]{2.2}
2096\end{cfuncdesc}
2097
2098\begin{cfuncdesc}{int}{PyFile_CheckExact}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002099 Return true if its argument is a \ctype{PyFileObject}, but not a
Fred Drake3adf79e2001-10-12 19:01:43 +00002100 subtype of \ctype{PyFileObject}.
2101 \versionadded{2.2}
2102\end{cfuncdesc}
2103
2104\begin{cfuncdesc}{PyObject*}{PyFile_FromString}{char *filename, char *mode}
Georg Brandl99363b62005-09-03 07:27:26 +00002105 On success, return a new file object that is opened on the file
Fred Drake3adf79e2001-10-12 19:01:43 +00002106 given by \var{filename}, with a file mode given by \var{mode}, where
2107 \var{mode} has the same semantics as the standard C routine
Georg Brandl99363b62005-09-03 07:27:26 +00002108 \cfunction{fopen()}\ttindex{fopen()}. On failure, return \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002109\end{cfuncdesc}
2110
2111\begin{cfuncdesc}{PyObject*}{PyFile_FromFile}{FILE *fp,
2112 char *name, char *mode,
2113 int (*close)(FILE*)}
Georg Brandl99363b62005-09-03 07:27:26 +00002114 Create a new \ctype{PyFileObject} from the already-open standard C
Fred Drake3adf79e2001-10-12 19:01:43 +00002115 file pointer, \var{fp}. The function \var{close} will be called
Georg Brandl99363b62005-09-03 07:27:26 +00002116 when the file should be closed. Return \NULL{} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00002117\end{cfuncdesc}
2118
2119\begin{cfuncdesc}{FILE*}{PyFile_AsFile}{PyFileObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002120 Return the file object associated with \var{p} as a \ctype{FILE*}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002121\end{cfuncdesc}
2122
2123\begin{cfuncdesc}{PyObject*}{PyFile_GetLine}{PyObject *p, int n}
2124 Equivalent to \code{\var{p}.readline(\optional{\var{n}})}, this
2125 function reads one line from the object \var{p}. \var{p} may be a
2126 file object or any object with a \method{readline()} method. If
2127 \var{n} is \code{0}, exactly one line is read, regardless of the
2128 length of the line. If \var{n} is greater than \code{0}, no more
2129 than \var{n} bytes will be read from the file; a partial line can be
2130 returned. In both cases, an empty string is returned if the end of
2131 the file is reached immediately. If \var{n} is less than \code{0},
2132 however, one line is read regardless of length, but
2133 \exception{EOFError} is raised if the end of the file is reached
2134 immediately.
2135 \withsubitem{(built-in exception)}{\ttindex{EOFError}}
2136\end{cfuncdesc}
2137
2138\begin{cfuncdesc}{PyObject*}{PyFile_Name}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002139 Return the name of the file specified by \var{p} as a string
Fred Drake3adf79e2001-10-12 19:01:43 +00002140 object.
2141\end{cfuncdesc}
2142
2143\begin{cfuncdesc}{void}{PyFile_SetBufSize}{PyFileObject *p, int n}
2144 Available on systems with \cfunction{setvbuf()}\ttindex{setvbuf()}
2145 only. This should only be called immediately after file object
2146 creation.
2147\end{cfuncdesc}
2148
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00002149\begin{cfuncdesc}{int}{PyFile_Encoding}{PyFileObject *p, char *enc}
2150 Set the file's encoding for Unicode output to \var{enc}. Return
2151 1 on success and 0 on failure.
2152 \versionadded{2.3}
2153\end{cfuncdesc}
2154
Fred Drake3adf79e2001-10-12 19:01:43 +00002155\begin{cfuncdesc}{int}{PyFile_SoftSpace}{PyObject *p, int newflag}
Georg Brandl99363b62005-09-03 07:27:26 +00002156 This function exists for internal use by the interpreter. Set the
Fred Drake3adf79e2001-10-12 19:01:43 +00002157 \member{softspace} attribute of \var{p} to \var{newflag} and
Georg Brandl99363b62005-09-03 07:27:26 +00002158 \withsubitem{(file attribute)}{\ttindex{softspace}}return the
Fred Drake3adf79e2001-10-12 19:01:43 +00002159 previous value. \var{p} does not have to be a file object for this
2160 function to work properly; any object is supported (thought its only
2161 interesting if the \member{softspace} attribute can be set). This
2162 function clears any errors, and will return \code{0} as the previous
2163 value if the attribute either does not exist or if there were errors
2164 in retrieving it. There is no way to detect errors from this
2165 function, but doing so should not be needed.
2166\end{cfuncdesc}
2167
2168\begin{cfuncdesc}{int}{PyFile_WriteObject}{PyObject *obj, PyFileObject *p,
2169 int flags}
Georg Brandl99363b62005-09-03 07:27:26 +00002170 Write object \var{obj} to file object \var{p}. The only supported
Fred Drake3adf79e2001-10-12 19:01:43 +00002171 flag for \var{flags} is
2172 \constant{Py_PRINT_RAW}\ttindex{Py_PRINT_RAW}; if given, the
2173 \function{str()} of the object is written instead of the
Georg Brandl99363b62005-09-03 07:27:26 +00002174 \function{repr()}. Return \code{0} on success or \code{-1} on
Fred Drake3adf79e2001-10-12 19:01:43 +00002175 failure; the appropriate exception will be set.
2176\end{cfuncdesc}
2177
Fred Drake454af892001-11-29 22:42:59 +00002178\begin{cfuncdesc}{int}{PyFile_WriteString}{const char *s, PyFileObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002179 Write string \var{s} to file object \var{p}. Return \code{0} on
Fred Drake3adf79e2001-10-12 19:01:43 +00002180 success or \code{-1} on failure; the appropriate exception will be
2181 set.
2182\end{cfuncdesc}
2183
2184
2185\subsection{Instance Objects \label{instanceObjects}}
2186
2187\obindex{instance}
2188There are very few functions specific to instance objects.
2189
2190\begin{cvardesc}{PyTypeObject}{PyInstance_Type}
2191 Type object for class instances.
2192\end{cvardesc}
2193
2194\begin{cfuncdesc}{int}{PyInstance_Check}{PyObject *obj}
Georg Brandl99363b62005-09-03 07:27:26 +00002195 Return true if \var{obj} is an instance.
Fred Drake3adf79e2001-10-12 19:01:43 +00002196\end{cfuncdesc}
2197
2198\begin{cfuncdesc}{PyObject*}{PyInstance_New}{PyObject *class,
2199 PyObject *arg,
2200 PyObject *kw}
2201 Create a new instance of a specific class. The parameters \var{arg}
2202 and \var{kw} are used as the positional and keyword parameters to
2203 the object's constructor.
2204\end{cfuncdesc}
2205
2206\begin{cfuncdesc}{PyObject*}{PyInstance_NewRaw}{PyObject *class,
2207 PyObject *dict}
Neil Schemenauerc4932292005-06-18 17:54:13 +00002208 Create a new instance of a specific class without calling its
Fred Drake3adf79e2001-10-12 19:01:43 +00002209 constructor. \var{class} is the class of new object. The
2210 \var{dict} parameter will be used as the object's \member{__dict__};
Tim Peters9ddf40b2004-06-20 22:41:32 +00002211 if \NULL{}, a new dictionary will be created for the instance.
Fred Drake3adf79e2001-10-12 19:01:43 +00002212\end{cfuncdesc}
2213
2214
2215\subsection{Method Objects \label{method-objects}}
2216
2217\obindex{method}
2218There are some useful functions that are useful for working with
2219method objects.
2220
2221\begin{cvardesc}{PyTypeObject}{PyMethod_Type}
2222 This instance of \ctype{PyTypeObject} represents the Python method
2223 type. This is exposed to Python programs as \code{types.MethodType}.
2224 \withsubitem{(in module types)}{\ttindex{MethodType}}
2225\end{cvardesc}
2226
2227\begin{cfuncdesc}{int}{PyMethod_Check}{PyObject *o}
2228 Return true if \var{o} is a method object (has type
Tim Peters9ddf40b2004-06-20 22:41:32 +00002229 \cdata{PyMethod_Type}). The parameter must not be \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002230\end{cfuncdesc}
2231
2232\begin{cfuncdesc}{PyObject*}{PyMethod_New}{PyObject *func.
2233 PyObject *self, PyObject *class}
2234 Return a new method object, with \var{func} being any callable
2235 object; this is the function that will be called when the method is
2236 called. If this method should be bound to an instance, \var{self}
2237 should be the instance and \var{class} should be the class of
2238 \var{self}, otherwise \var{self} should be \NULL{} and \var{class}
2239 should be the class which provides the unbound method..
2240\end{cfuncdesc}
2241
2242\begin{cfuncdesc}{PyObject*}{PyMethod_Class}{PyObject *meth}
2243 Return the class object from which the method \var{meth} was
2244 created; if this was created from an instance, it will be the class
2245 of the instance.
2246\end{cfuncdesc}
2247
2248\begin{cfuncdesc}{PyObject*}{PyMethod_GET_CLASS}{PyObject *meth}
2249 Macro version of \cfunction{PyMethod_Class()} which avoids error
2250 checking.
2251\end{cfuncdesc}
2252
2253\begin{cfuncdesc}{PyObject*}{PyMethod_Function}{PyObject *meth}
2254 Return the function object associated with the method \var{meth}.
2255\end{cfuncdesc}
2256
2257\begin{cfuncdesc}{PyObject*}{PyMethod_GET_FUNCTION}{PyObject *meth}
2258 Macro version of \cfunction{PyMethod_Function()} which avoids error
2259 checking.
2260\end{cfuncdesc}
2261
2262\begin{cfuncdesc}{PyObject*}{PyMethod_Self}{PyObject *meth}
2263 Return the instance associated with the method \var{meth} if it is
Tim Peters9ddf40b2004-06-20 22:41:32 +00002264 bound, otherwise return \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002265\end{cfuncdesc}
2266
2267\begin{cfuncdesc}{PyObject*}{PyMethod_GET_SELF}{PyObject *meth}
2268 Macro version of \cfunction{PyMethod_Self()} which avoids error
2269 checking.
2270\end{cfuncdesc}
2271
2272
2273\subsection{Module Objects \label{moduleObjects}}
2274
2275\obindex{module}
2276There are only a few functions special to module objects.
2277
2278\begin{cvardesc}{PyTypeObject}{PyModule_Type}
2279 This instance of \ctype{PyTypeObject} represents the Python module
2280 type. This is exposed to Python programs as
2281 \code{types.ModuleType}.
2282 \withsubitem{(in module types)}{\ttindex{ModuleType}}
2283\end{cvardesc}
2284
2285\begin{cfuncdesc}{int}{PyModule_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002286 Return true if \var{p} is a module object, or a subtype of a module
Fred Drake3adf79e2001-10-12 19:01:43 +00002287 object.
2288 \versionchanged[Allowed subtypes to be accepted]{2.2}
2289\end{cfuncdesc}
2290
2291\begin{cfuncdesc}{int}{PyModule_CheckExact}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002292 Return true if \var{p} is a module object, but not a subtype of
Fred Drake3adf79e2001-10-12 19:01:43 +00002293 \cdata{PyModule_Type}.
2294 \versionadded{2.2}
2295\end{cfuncdesc}
2296
2297\begin{cfuncdesc}{PyObject*}{PyModule_New}{char *name}
2298 Return a new module object with the \member{__name__} attribute set
2299 to \var{name}. Only the module's \member{__doc__} and
2300 \member{__name__} attributes are filled in; the caller is
2301 responsible for providing a \member{__file__} attribute.
2302 \withsubitem{(module attribute)}{
2303 \ttindex{__name__}\ttindex{__doc__}\ttindex{__file__}}
2304\end{cfuncdesc}
2305
2306\begin{cfuncdesc}{PyObject*}{PyModule_GetDict}{PyObject *module}
2307 Return the dictionary object that implements \var{module}'s
2308 namespace; this object is the same as the \member{__dict__}
2309 attribute of the module object. This function never fails.
2310 \withsubitem{(module attribute)}{\ttindex{__dict__}}
Fred Drakef495ef72002-04-12 19:32:07 +00002311 It is recommended extensions use other \cfunction{PyModule_*()}
2312 and \cfunction{PyObject_*()} functions rather than directly
2313 manipulate a module's \member{__dict__}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002314\end{cfuncdesc}
2315
2316\begin{cfuncdesc}{char*}{PyModule_GetName}{PyObject *module}
2317 Return \var{module}'s \member{__name__} value. If the module does
2318 not provide one, or if it is not a string, \exception{SystemError}
2319 is raised and \NULL{} is returned.
2320 \withsubitem{(module attribute)}{\ttindex{__name__}}
2321 \withsubitem{(built-in exception)}{\ttindex{SystemError}}
2322\end{cfuncdesc}
2323
2324\begin{cfuncdesc}{char*}{PyModule_GetFilename}{PyObject *module}
2325 Return the name of the file from which \var{module} was loaded using
2326 \var{module}'s \member{__file__} attribute. If this is not defined,
2327 or if it is not a string, raise \exception{SystemError} and return
Tim Peters9ddf40b2004-06-20 22:41:32 +00002328 \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002329 \withsubitem{(module attribute)}{\ttindex{__file__}}
2330 \withsubitem{(built-in exception)}{\ttindex{SystemError}}
2331\end{cfuncdesc}
2332
2333\begin{cfuncdesc}{int}{PyModule_AddObject}{PyObject *module,
2334 char *name, PyObject *value}
2335 Add an object to \var{module} as \var{name}. This is a convenience
2336 function which can be used from the module's initialization
Georg Brandl99363b62005-09-03 07:27:26 +00002337 function. This steals a reference to \var{value}. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00002338 \code{-1} on error, \code{0} on success.
2339 \versionadded{2.0}
2340\end{cfuncdesc}
2341
2342\begin{cfuncdesc}{int}{PyModule_AddIntConstant}{PyObject *module,
Martin v. Löwisdd07e592004-06-02 12:45:27 +00002343 char *name, long value}
Fred Drake3adf79e2001-10-12 19:01:43 +00002344 Add an integer constant to \var{module} as \var{name}. This
2345 convenience function can be used from the module's initialization
Georg Brandl99363b62005-09-03 07:27:26 +00002346 function. Return \code{-1} on error, \code{0} on success.
Fred Drake3adf79e2001-10-12 19:01:43 +00002347 \versionadded{2.0}
2348\end{cfuncdesc}
2349
2350\begin{cfuncdesc}{int}{PyModule_AddStringConstant}{PyObject *module,
2351 char *name, char *value}
2352 Add a string constant to \var{module} as \var{name}. This
2353 convenience function can be used from the module's initialization
Georg Brandl99363b62005-09-03 07:27:26 +00002354 function. The string \var{value} must be null-terminated. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00002355 \code{-1} on error, \code{0} on success.
2356 \versionadded{2.0}
2357\end{cfuncdesc}
2358
2359
2360\subsection{Iterator Objects \label{iterator-objects}}
2361
2362Python provides two general-purpose iterator objects. The first, a
2363sequence iterator, works with an arbitrary sequence supporting the
2364\method{__getitem__()} method. The second works with a callable
2365object and a sentinel value, calling the callable for each item in the
2366sequence, and ending the iteration when the sentinel value is
2367returned.
2368
2369\begin{cvardesc}{PyTypeObject}{PySeqIter_Type}
2370 Type object for iterator objects returned by
2371 \cfunction{PySeqIter_New()} and the one-argument form of the
2372 \function{iter()} built-in function for built-in sequence types.
2373 \versionadded{2.2}
2374\end{cvardesc}
2375
2376\begin{cfuncdesc}{int}{PySeqIter_Check}{op}
2377 Return true if the type of \var{op} is \cdata{PySeqIter_Type}.
2378 \versionadded{2.2}
2379\end{cfuncdesc}
2380
2381\begin{cfuncdesc}{PyObject*}{PySeqIter_New}{PyObject *seq}
2382 Return an iterator that works with a general sequence object,
2383 \var{seq}. The iteration ends when the sequence raises
2384 \exception{IndexError} for the subscripting operation.
2385 \versionadded{2.2}
2386\end{cfuncdesc}
2387
2388\begin{cvardesc}{PyTypeObject}{PyCallIter_Type}
2389 Type object for iterator objects returned by
2390 \cfunction{PyCallIter_New()} and the two-argument form of the
2391 \function{iter()} built-in function.
2392 \versionadded{2.2}
2393\end{cvardesc}
2394
2395\begin{cfuncdesc}{int}{PyCallIter_Check}{op}
2396 Return true if the type of \var{op} is \cdata{PyCallIter_Type}.
2397 \versionadded{2.2}
2398\end{cfuncdesc}
2399
2400\begin{cfuncdesc}{PyObject*}{PyCallIter_New}{PyObject *callable,
2401 PyObject *sentinel}
2402 Return a new iterator. The first parameter, \var{callable}, can be
2403 any Python callable object that can be called with no parameters;
2404 each call to it should return the next item in the iteration. When
2405 \var{callable} returns a value equal to \var{sentinel}, the
2406 iteration will be terminated.
2407 \versionadded{2.2}
2408\end{cfuncdesc}
2409
2410
2411\subsection{Descriptor Objects \label{descriptor-objects}}
2412
Fred Drake54e62942001-12-11 19:40:16 +00002413``Descriptors'' are objects that describe some attribute of an object.
2414They are found in the dictionary of type objects.
2415
Fred Drake3adf79e2001-10-12 19:01:43 +00002416\begin{cvardesc}{PyTypeObject}{PyProperty_Type}
Fred Drake54e62942001-12-11 19:40:16 +00002417 The type object for the built-in descriptor types.
Fred Drake3adf79e2001-10-12 19:01:43 +00002418 \versionadded{2.2}
2419\end{cvardesc}
2420
2421\begin{cfuncdesc}{PyObject*}{PyDescr_NewGetSet}{PyTypeObject *type,
2422 PyGetSetDef *getset}
2423 \versionadded{2.2}
2424\end{cfuncdesc}
2425
2426\begin{cfuncdesc}{PyObject*}{PyDescr_NewMember}{PyTypeObject *type,
2427 PyMemberDef *meth}
2428 \versionadded{2.2}
2429\end{cfuncdesc}
2430
2431\begin{cfuncdesc}{PyObject*}{PyDescr_NewMethod}{PyTypeObject *type,
2432 PyMethodDef *meth}
2433 \versionadded{2.2}
2434\end{cfuncdesc}
2435
2436\begin{cfuncdesc}{PyObject*}{PyDescr_NewWrapper}{PyTypeObject *type,
2437 struct wrapperbase *wrapper,
2438 void *wrapped}
2439 \versionadded{2.2}
2440\end{cfuncdesc}
2441
Thomas Heller8178a222004-02-09 10:47:11 +00002442\begin{cfuncdesc}{PyObject*}{PyDescr_NewClassMethod}{PyTypeObject *type,
2443 PyMethodDef *method}
2444 \versionadded{2.3}
2445\end{cfuncdesc}
2446
Fred Drake3adf79e2001-10-12 19:01:43 +00002447\begin{cfuncdesc}{int}{PyDescr_IsData}{PyObject *descr}
Georg Brandl99363b62005-09-03 07:27:26 +00002448 Return true if the descriptor objects \var{descr} describes a data
Fred Drake3adf79e2001-10-12 19:01:43 +00002449 attribute, or false if it describes a method. \var{descr} must be a
2450 descriptor object; there is no error checking.
2451 \versionadded{2.2}
2452\end{cfuncdesc}
2453
2454\begin{cfuncdesc}{PyObject*}{PyWrapper_New}{PyObject *, PyObject *}
2455 \versionadded{2.2}
2456\end{cfuncdesc}
2457
2458
2459\subsection{Slice Objects \label{slice-objects}}
2460
2461\begin{cvardesc}{PyTypeObject}{PySlice_Type}
2462 The type object for slice objects. This is the same as
2463 \code{types.SliceType}.
2464 \withsubitem{(in module types)}{\ttindex{SliceType}}
2465\end{cvardesc}
2466
2467\begin{cfuncdesc}{int}{PySlice_Check}{PyObject *ob}
Georg Brandl99363b62005-09-03 07:27:26 +00002468 Return true if \var{ob} is a slice object; \var{ob} must not be
Tim Peters9ddf40b2004-06-20 22:41:32 +00002469 \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002470\end{cfuncdesc}
2471
2472\begin{cfuncdesc}{PyObject*}{PySlice_New}{PyObject *start, PyObject *stop,
2473 PyObject *step}
2474 Return a new slice object with the given values. The \var{start},
2475 \var{stop}, and \var{step} parameters are used as the values of the
2476 slice object attributes of the same names. Any of the values may be
Tim Peters9ddf40b2004-06-20 22:41:32 +00002477 \NULL{}, in which case the \code{None} will be used for the
Georg Brandl99363b62005-09-03 07:27:26 +00002478 corresponding attribute. Return \NULL{} if the new object could
Fred Drake3adf79e2001-10-12 19:01:43 +00002479 not be allocated.
2480\end{cfuncdesc}
2481
2482\begin{cfuncdesc}{int}{PySlice_GetIndices}{PySliceObject *slice, int length,
2483 int *start, int *stop, int *step}
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002484Retrieve the start, stop and step indices from the slice object
2485\var{slice}, assuming a sequence of length \var{length}. Treats
2486indices greater than \var{length} as errors.
2487
2488Returns 0 on success and -1 on error with no exception set (unless one
2489of the indices was not \constant{None} and failed to be converted to
2490an integer, in which case -1 is returned with an exception set).
2491
2492You probably do not want to use this function. If you want to use
2493slice objects in versions of Python prior to 2.3, you would probably
2494do well to incorporate the source of \cfunction{PySlice_GetIndicesEx},
2495suitably renamed, in the source of your extension.
2496\end{cfuncdesc}
2497
2498\begin{cfuncdesc}{int}{PySlice_GetIndicesEx}{PySliceObject *slice, int length,
Tim Peters9ddf40b2004-06-20 22:41:32 +00002499 int *start, int *stop, int *step,
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002500 int *slicelength}
2501Usable replacement for \cfunction{PySlice_GetIndices}. Retrieve the
2502start, stop, and step indices from the slice object \var{slice}
2503assuming a sequence of length \var{length}, and store the length of
2504the slice in \var{slicelength}. Out of bounds indices are clipped in
2505a manner consistent with the handling of normal slices.
2506
2507Returns 0 on success and -1 on error with exception set.
2508
2509\versionadded{2.3}
Fred Drake3adf79e2001-10-12 19:01:43 +00002510\end{cfuncdesc}
2511
2512
2513\subsection{Weak Reference Objects \label{weakref-objects}}
2514
2515Python supports \emph{weak references} as first-class objects. There
2516are two specific object types which directly implement weak
2517references. The first is a simple reference object, and the second
2518acts as a proxy for the original object as much as it can.
2519
2520\begin{cfuncdesc}{int}{PyWeakref_Check}{ob}
2521 Return true if \var{ob} is either a reference or proxy object.
2522 \versionadded{2.2}
2523\end{cfuncdesc}
2524
2525\begin{cfuncdesc}{int}{PyWeakref_CheckRef}{ob}
2526 Return true if \var{ob} is a reference object.
2527 \versionadded{2.2}
2528\end{cfuncdesc}
2529
2530\begin{cfuncdesc}{int}{PyWeakref_CheckProxy}{ob}
2531 Return true if \var{ob} is a proxy object.
2532 \versionadded{2.2}
2533\end{cfuncdesc}
2534
2535\begin{cfuncdesc}{PyObject*}{PyWeakref_NewRef}{PyObject *ob,
2536 PyObject *callback}
2537 Return a weak reference object for the object \var{ob}. This will
2538 always return a new reference, but is not guaranteed to create a new
2539 object; an existing reference object may be returned. The second
2540 parameter, \var{callback}, can be a callable object that receives
2541 notification when \var{ob} is garbage collected; it should accept a
Raymond Hettinger5232f502004-03-25 08:51:36 +00002542 single parameter, which will be the weak reference object itself.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002543 \var{callback} may also be \code{None} or \NULL{}. If \var{ob}
Fred Drake3adf79e2001-10-12 19:01:43 +00002544 is not a weakly-referencable object, or if \var{callback} is not
Tim Peters9ddf40b2004-06-20 22:41:32 +00002545 callable, \code{None}, or \NULL{}, this will return \NULL{} and
Fred Drake3adf79e2001-10-12 19:01:43 +00002546 raise \exception{TypeError}.
2547 \versionadded{2.2}
2548\end{cfuncdesc}
2549
2550\begin{cfuncdesc}{PyObject*}{PyWeakref_NewProxy}{PyObject *ob,
2551 PyObject *callback}
2552 Return a weak reference proxy object for the object \var{ob}. This
2553 will always return a new reference, but is not guaranteed to create
2554 a new object; an existing proxy object may be returned. The second
2555 parameter, \var{callback}, can be a callable object that receives
2556 notification when \var{ob} is garbage collected; it should accept a
Raymond Hettinger5232f502004-03-25 08:51:36 +00002557 single parameter, which will be the weak reference object itself.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002558 \var{callback} may also be \code{None} or \NULL{}. If \var{ob} is not
Fred Drake3adf79e2001-10-12 19:01:43 +00002559 a weakly-referencable object, or if \var{callback} is not callable,
Tim Peters9ddf40b2004-06-20 22:41:32 +00002560 \code{None}, or \NULL{}, this will return \NULL{} and raise
Fred Drake3adf79e2001-10-12 19:01:43 +00002561 \exception{TypeError}.
2562 \versionadded{2.2}
2563\end{cfuncdesc}
2564
2565\begin{cfuncdesc}{PyObject*}{PyWeakref_GetObject}{PyObject *ref}
Georg Brandl99363b62005-09-03 07:27:26 +00002566 Return the referenced object from a weak reference, \var{ref}. If
Ka-Ping Yeebd379e92003-03-28 18:07:16 +00002567 the referent is no longer live, returns \code{None}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002568 \versionadded{2.2}
2569\end{cfuncdesc}
2570
2571\begin{cfuncdesc}{PyObject*}{PyWeakref_GET_OBJECT}{PyObject *ref}
2572 Similar to \cfunction{PyWeakref_GetObject()}, but implemented as a
2573 macro that does no error checking.
2574 \versionadded{2.2}
2575\end{cfuncdesc}
2576
2577
2578\subsection{CObjects \label{cObjects}}
2579
2580\obindex{CObject}
2581Refer to \emph{Extending and Embedding the Python Interpreter},
Fred Drake54e62942001-12-11 19:40:16 +00002582section~1.12, ``Providing a C API for an Extension Module,'' for more
Fred Drake3adf79e2001-10-12 19:01:43 +00002583information on using these objects.
2584
2585
2586\begin{ctypedesc}{PyCObject}
2587 This subtype of \ctype{PyObject} represents an opaque value, useful
2588 for C extension modules who need to pass an opaque value (as a
2589 \ctype{void*} pointer) through Python code to other C code. It is
2590 often used to make a C function pointer defined in one module
2591 available to other modules, so the regular import mechanism can be
2592 used to access C APIs defined in dynamically loaded modules.
2593\end{ctypedesc}
2594
2595\begin{cfuncdesc}{int}{PyCObject_Check}{PyObject *p}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002596 Return true if its argument is a \ctype{PyCObject}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002597\end{cfuncdesc}
2598
Tim Petersf582b822001-12-11 18:51:08 +00002599\begin{cfuncdesc}{PyObject*}{PyCObject_FromVoidPtr}{void* cobj,
Fred Drake54e62942001-12-11 19:40:16 +00002600 void (*destr)(void *)}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002601 Create a \ctype{PyCObject} from the \code{void *}\var{cobj}. The
Fred Drake3adf79e2001-10-12 19:01:43 +00002602 \var{destr} function will be called when the object is reclaimed,
Tim Peters9ddf40b2004-06-20 22:41:32 +00002603 unless it is \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002604\end{cfuncdesc}
2605
2606\begin{cfuncdesc}{PyObject*}{PyCObject_FromVoidPtrAndDesc}{void* cobj,
2607 void* desc, void (*destr)(void *, void *)}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002608 Create a \ctype{PyCObject} from the \ctype{void *}\var{cobj}. The
Fred Drake3adf79e2001-10-12 19:01:43 +00002609 \var{destr} function will be called when the object is reclaimed.
2610 The \var{desc} argument can be used to pass extra callback data for
2611 the destructor function.
2612\end{cfuncdesc}
2613
2614\begin{cfuncdesc}{void*}{PyCObject_AsVoidPtr}{PyObject* self}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002615 Return the object \ctype{void *} that the \ctype{PyCObject}
Fred Drake3adf79e2001-10-12 19:01:43 +00002616 \var{self} was created with.
2617\end{cfuncdesc}
2618
2619\begin{cfuncdesc}{void*}{PyCObject_GetDesc}{PyObject* self}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002620 Return the description \ctype{void *} that the \ctype{PyCObject}
Fred Drake3adf79e2001-10-12 19:01:43 +00002621 \var{self} was created with.
2622\end{cfuncdesc}
Fred Drakecd8474e2001-11-26 21:29:17 +00002623
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002624\begin{cfuncdesc}{int}{PyCObject_SetVoidPtr}{PyObject* self, void* cobj}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002625 Set the void pointer inside \var{self} to \var{cobj}.
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002626 The \ctype{PyCObject} must not have an associated destructor.
2627 Return true on success, false on failure.
2628\end{cfuncdesc}
2629
Fred Drakecd8474e2001-11-26 21:29:17 +00002630
2631\subsection{Cell Objects \label{cell-objects}}
2632
2633``Cell'' objects are used to implement variables referenced by
2634multiple scopes. For each such variable, a cell object is created to
2635store the value; the local variables of each stack frame that
2636references the value contains a reference to the cells from outer
2637scopes which also use that variable. When the value is accessed, the
2638value contained in the cell is used instead of the cell object
2639itself. This de-referencing of the cell object requires support from
2640the generated byte-code; these are not automatically de-referenced
2641when accessed. Cell objects are not likely to be useful elsewhere.
2642
Fred Drake54e62942001-12-11 19:40:16 +00002643\begin{ctypedesc}{PyCellObject}
2644 The C structure used for cell objects.
2645\end{ctypedesc}
2646
Fred Drakecd8474e2001-11-26 21:29:17 +00002647\begin{cvardesc}{PyTypeObject}{PyCell_Type}
2648 The type object corresponding to cell objects
2649\end{cvardesc}
2650
2651\begin{cfuncdesc}{int}{PyCell_Check}{ob}
2652 Return true if \var{ob} is a cell object; \var{ob} must not be
Tim Peters9ddf40b2004-06-20 22:41:32 +00002653 \NULL{}.
Fred Drakecd8474e2001-11-26 21:29:17 +00002654\end{cfuncdesc}
2655
2656\begin{cfuncdesc}{PyObject*}{PyCell_New}{PyObject *ob}
2657 Create and return a new cell object containing the value \var{ob}.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002658 The parameter may be \NULL{}.
Fred Drakecd8474e2001-11-26 21:29:17 +00002659\end{cfuncdesc}
2660
2661\begin{cfuncdesc}{PyObject*}{PyCell_Get}{PyObject *cell}
2662 Return the contents of the cell \var{cell}.
2663\end{cfuncdesc}
2664
2665\begin{cfuncdesc}{PyObject*}{PyCell_GET}{PyObject *cell}
2666 Return the contents of the cell \var{cell}, but without checking
Raymond Hettingerf4bb1f92003-08-23 03:38:11 +00002667 that \var{cell} is non-\NULL{} and a cell object.
Fred Drakecd8474e2001-11-26 21:29:17 +00002668\end{cfuncdesc}
2669
2670\begin{cfuncdesc}{int}{PyCell_Set}{PyObject *cell, PyObject *value}
2671 Set the contents of the cell object \var{cell} to \var{value}. This
2672 releases the reference to any current content of the cell.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002673 \var{value} may be \NULL{}. \var{cell} must be non-\NULL{}; if it is
Fred Drakecd8474e2001-11-26 21:29:17 +00002674 not a cell object, \code{-1} will be returned. On success, \code{0}
2675 will be returned.
2676\end{cfuncdesc}
2677
2678\begin{cfuncdesc}{void}{PyCell_SET}{PyObject *cell, PyObject *value}
2679 Sets the value of the cell object \var{cell} to \var{value}. No
2680 reference counts are adjusted, and no checks are made for safety;
2681 \var{cell} must be non-\NULL{} and must be a cell object.
2682\end{cfuncdesc}
Martin v. Löwise440e472004-06-01 15:22:42 +00002683
2684
2685\subsection{Generator Objects \label{gen-objects}}
2686
2687Generator objects are what Python uses to implement generator iterators.
2688They are normally created by iterating over a function that yields values,
2689rather than explicitly calling \cfunction{PyGen_New}.
2690
2691\begin{ctypedesc}{PyGenObject}
2692 The C structure used for generator objects.
2693\end{ctypedesc}
2694
2695\begin{cvardesc}{PyTypeObject}{PyGen_Type}
2696 The type object corresponding to generator objects
2697\end{cvardesc}
2698
2699\begin{cfuncdesc}{int}{PyGen_Check}{ob}
2700 Return true if \var{ob} is a generator object; \var{ob} must not be
Tim Peters9ddf40b2004-06-20 22:41:32 +00002701 \NULL{}.
Martin v. Löwise440e472004-06-01 15:22:42 +00002702\end{cfuncdesc}
2703
2704\begin{cfuncdesc}{int}{PyGen_CheckExact}{ob}
2705 Return true if \var{ob}'s type is \var{PyGen_Type}
2706 is a generator object; \var{ob} must not be
Tim Peters9ddf40b2004-06-20 22:41:32 +00002707 \NULL{}.
Martin v. Löwise440e472004-06-01 15:22:42 +00002708\end{cfuncdesc}
2709
2710\begin{cfuncdesc}{PyObject*}{PyGen_New}{PyFrameObject *frame}
2711 Create and return a new generator object based on the \var{frame} object.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002712 The parameter must not be \NULL{}.
2713\end{cfuncdesc}
2714
2715
2716\subsection{DateTime Objects \label{datetime-objects}}
2717
2718Various date and time objects are supplied by the \module{datetime}
2719module. Before using any of these functions, the header file
2720\file{datetime.h} must be included in your source (note that this is
2721not include by \file{Python.h}), and macro \cfunction{PyDateTime_IMPORT()}
2722must be invoked. The macro arranges to put a pointer to a C structure
2723in a static variable \code{PyDateTimeAPI}, which is used by the following
Tim Peters183dabc2004-07-11 19:26:19 +00002724macros.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002725
Tim Peters183dabc2004-07-11 19:26:19 +00002726Type-check macros:
2727
2728\begin{cfuncdesc}{int}{PyDate_Check}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002729 Return true if \var{ob} is of type \cdata{PyDateTime_DateType} or
2730 a subtype of \cdata{PyDateTime_DateType}. \var{ob} must not be
2731 \NULL{}.
2732 \versionadded{2.4}
2733\end{cfuncdesc}
2734
Tim Peters183dabc2004-07-11 19:26:19 +00002735\begin{cfuncdesc}{int}{PyDate_CheckExact}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002736 Return true if \var{ob} is of type \cdata{PyDateTime_DateType}.
2737 \var{ob} must not be \NULL{}.
2738 \versionadded{2.4}
2739\end{cfuncdesc}
2740
Tim Peters183dabc2004-07-11 19:26:19 +00002741\begin{cfuncdesc}{int}{PyDateTime_Check}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002742 Return true if \var{ob} is of type \cdata{PyDateTime_DateTimeType} or
2743 a subtype of \cdata{PyDateTime_DateTimeType}. \var{ob} must not be
2744 \NULL{}.
2745 \versionadded{2.4}
2746\end{cfuncdesc}
2747
Tim Peters183dabc2004-07-11 19:26:19 +00002748\begin{cfuncdesc}{int}{PyDateTime_CheckExact}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002749 Return true if \var{ob} is of type \cdata{PyDateTime_DateTimeType}.
2750 \var{ob} must not be \NULL{}.
2751 \versionadded{2.4}
2752\end{cfuncdesc}
2753
Tim Peters183dabc2004-07-11 19:26:19 +00002754\begin{cfuncdesc}{int}{PyTime_Check}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002755 Return true if \var{ob} is of type \cdata{PyDateTime_TimeType} or
2756 a subtype of \cdata{PyDateTime_TimeType}. \var{ob} must not be
2757 \NULL{}.
2758 \versionadded{2.4}
2759\end{cfuncdesc}
2760
Tim Peters183dabc2004-07-11 19:26:19 +00002761\begin{cfuncdesc}{int}{PyTime_CheckExact}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002762 Return true if \var{ob} is of type \cdata{PyDateTime_TimeType}.
2763 \var{ob} must not be \NULL{}.
2764 \versionadded{2.4}
2765\end{cfuncdesc}
2766
Tim Peters183dabc2004-07-11 19:26:19 +00002767\begin{cfuncdesc}{int}{PyDelta_Check}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002768 Return true if \var{ob} is of type \cdata{PyDateTime_DeltaType} or
2769 a subtype of \cdata{PyDateTime_DeltaType}. \var{ob} must not be
2770 \NULL{}.
2771 \versionadded{2.4}
2772\end{cfuncdesc}
2773
Tim Peters183dabc2004-07-11 19:26:19 +00002774\begin{cfuncdesc}{int}{PyDelta_CheckExact}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002775 Return true if \var{ob} is of type \cdata{PyDateTime_DeltaType}.
2776 \var{ob} must not be \NULL{}.
2777 \versionadded{2.4}
2778\end{cfuncdesc}
2779
Tim Peters183dabc2004-07-11 19:26:19 +00002780\begin{cfuncdesc}{int}{PyTZInfo_Check}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002781 Return true if \var{ob} is of type \cdata{PyDateTime_TZInfoType} or
2782 a subtype of \cdata{PyDateTime_TZInfoType}. \var{ob} must not be
2783 \NULL{}.
2784 \versionadded{2.4}
2785\end{cfuncdesc}
2786
Tim Peters183dabc2004-07-11 19:26:19 +00002787\begin{cfuncdesc}{int}{PyTZInfo_CheckExact}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002788 Return true if \var{ob} is of type \cdata{PyDateTime_TZInfoType}.
2789 \var{ob} must not be \NULL{}.
2790 \versionadded{2.4}
2791\end{cfuncdesc}
2792
Tim Peters183dabc2004-07-11 19:26:19 +00002793Macros to create objects:
2794
Tim Peters9ddf40b2004-06-20 22:41:32 +00002795\begin{cfuncdesc}{PyObject*}{PyDate_FromDate}{int year, int month, int day}
2796 Return a \code{datetime.date} object with the specified year, month
2797 and day.
2798 \versionadded{2.4}
2799\end{cfuncdesc}
2800
Brett Cannon5bbe6ad2005-02-17 05:17:17 +00002801\begin{cfuncdesc}{PyObject*}{PyDateTime_FromDateAndTime}{int year, int month,
Tim Peters9ddf40b2004-06-20 22:41:32 +00002802 int day, int hour, int minute, int second, int usecond}
2803 Return a \code{datetime.datetime} object with the specified year, month,
2804 day, hour, minute, second and microsecond.
2805 \versionadded{2.4}
2806\end{cfuncdesc}
2807
2808\begin{cfuncdesc}{PyObject*}{PyTime_FromTime}{int hour, int minute,
2809 int second, int usecond}
2810 Return a \code{datetime.time} object with the specified hour, minute,
2811 second and microsecond.
2812 \versionadded{2.4}
2813\end{cfuncdesc}
2814
2815\begin{cfuncdesc}{PyObject*}{PyDelta_FromDSU}{int days, int seconds,
2816 int useconds}
2817 Return a \code{datetime.timedelta} object representing the given number
2818 of days, seconds and microseconds. Normalization is performed so that
2819 the resulting number of microseconds and seconds lie in the ranges
2820 documented for \code{datetime.timedelta} objects.
2821 \versionadded{2.4}
2822\end{cfuncdesc}
2823
Tim Peters8ff9f9f2004-07-17 01:42:26 +00002824Macros to extract fields from date objects. The argument must be an
Tim Peters183dabc2004-07-11 19:26:19 +00002825instance of \cdata{PyDateTime_Date}, including subclasses (such as
2826\cdata{PyDateTime_DateTime}). The argument must not be \NULL{}, and
2827the type is not checked:
2828
2829\begin{cfuncdesc}{int}{PyDateTime_GET_YEAR}{PyDateTime_Date *o}
2830 Return the year, as a positive int.
2831 \versionadded{2.4}
2832\end{cfuncdesc}
2833
2834\begin{cfuncdesc}{int}{PyDateTime_GET_MONTH}{PyDateTime_Date *o}
2835 Return the month, as an int from 1 through 12.
2836 \versionadded{2.4}
2837\end{cfuncdesc}
2838
2839\begin{cfuncdesc}{int}{PyDateTime_GET_DAY}{PyDateTime_Date *o}
2840 Return the day, as an int from 1 through 31.
2841 \versionadded{2.4}
2842\end{cfuncdesc}
2843
Tim Peters8ff9f9f2004-07-17 01:42:26 +00002844Macros to extract fields from datetime objects. The argument must be an
Tim Peters183dabc2004-07-11 19:26:19 +00002845instance of \cdata{PyDateTime_DateTime}, including subclasses.
2846The argument must not be \NULL{}, and the type is not checked:
2847
2848\begin{cfuncdesc}{int}{PyDateTime_DATE_GET_HOUR}{PyDateTime_DateTime *o}
Neal Norwitz7fdd92f2004-08-02 21:56:33 +00002849 Return the hour, as an int from 0 through 23.
Tim Peters183dabc2004-07-11 19:26:19 +00002850 \versionadded{2.4}
2851\end{cfuncdesc}
2852
2853\begin{cfuncdesc}{int}{PyDateTime_DATE_GET_MINUTE}{PyDateTime_DateTime *o}
2854 Return the minute, as an int from 0 through 59.
2855 \versionadded{2.4}
2856\end{cfuncdesc}
2857
2858\begin{cfuncdesc}{int}{PyDateTime_DATE_GET_SECOND}{PyDateTime_DateTime *o}
2859 Return the second, as an int from 0 through 59.
2860 \versionadded{2.4}
2861\end{cfuncdesc}
2862
2863\begin{cfuncdesc}{int}{PyDateTime_DATE_GET_MICROSECOND}{PyDateTime_DateTime *o}
2864 Return the microsecond, as an int from 0 through 999999.
2865 \versionadded{2.4}
2866\end{cfuncdesc}
2867
Tim Peters8ff9f9f2004-07-17 01:42:26 +00002868Macros to extract fields from time objects. The argument must be an
Tim Peters183dabc2004-07-11 19:26:19 +00002869instance of \cdata{PyDateTime_Time}, including subclasses.
2870The argument must not be \NULL{}, and the type is not checked:
2871
2872\begin{cfuncdesc}{int}{PyDateTime_TIME_GET_HOUR}{PyDateTime_Time *o}
Neal Norwitz7fdd92f2004-08-02 21:56:33 +00002873 Return the hour, as an int from 0 through 23.
Tim Peters183dabc2004-07-11 19:26:19 +00002874 \versionadded{2.4}
2875\end{cfuncdesc}
2876
2877\begin{cfuncdesc}{int}{PyDateTime_TIME_GET_MINUTE}{PyDateTime_Time *o}
2878 Return the minute, as an int from 0 through 59.
2879 \versionadded{2.4}
2880\end{cfuncdesc}
2881
2882\begin{cfuncdesc}{int}{PyDateTime_TIME_GET_SECOND}{PyDateTime_Time *o}
2883 Return the second, as an int from 0 through 59.
2884 \versionadded{2.4}
2885\end{cfuncdesc}
2886
2887\begin{cfuncdesc}{int}{PyDateTime_TIME_GET_MICROSECOND}{PyDateTime_Time *o}
2888 Return the microsecond, as an int from 0 through 999999.
2889 \versionadded{2.4}
2890\end{cfuncdesc}
2891
2892Macros for the convenience of modules implementing the DB API:
2893
Tim Peters9ddf40b2004-06-20 22:41:32 +00002894\begin{cfuncdesc}{PyObject*}{PyDateTime_FromTimestamp}{PyObject *args}
2895 Create and return a new \code{datetime.datetime} object given an argument
2896 tuple suitable for passing to \code{datetime.datetime.fromtimestamp()}.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002897 \versionadded{2.4}
2898\end{cfuncdesc}
2899
2900\begin{cfuncdesc}{PyObject*}{PyDate_FromTimestamp}{PyObject *args}
2901 Create and return a new \code{datetime.date} object given an argument
2902 tuple suitable for passing to \code{datetime.date.fromtimestamp()}.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002903 \versionadded{2.4}
Martin v. Löwise440e472004-06-01 15:22:42 +00002904\end{cfuncdesc}
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002905
2906
2907\subsection{Set Objects \label{setObjects}}
2908\sectionauthor{Raymond D. Hettinger}{python@rcn.com}
2909
2910\obindex{set}
2911\obindex{frozenset}
2912\versionadded{2.5}
2913
2914This section details the public API for \class{set} and \class{frozenset}
2915objects. Any functionality not listed below is best accessed using the
Raymond Hettinger0c230b92005-08-17 10:05:22 +00002916either the abstract object protocol (including
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002917\cfunction{PyObject_CallMethod()}, \cfunction{PyObject_RichCompareBool()},
2918\cfunction{PyObject_Hash()}, \cfunction{PyObject_Repr()},
2919\cfunction{PyObject_IsTrue()}, \cfunction{PyObject_Print()}, and
Raymond Hettinger0c230b92005-08-17 10:05:22 +00002920\cfunction{PyObject_GetIter()})
2921or the abstract number protocol (including
2922\cfunction{PyNumber_Add()}, \cfunction{PyNumber_Subtract()},
2923\cfunction{PyNumber_Or()}, \cfunction{PyNumber_Xor()},
2924\cfunction{PyNumber_InplaceAdd()}, \cfunction{PyNumber_InplaceSubtract()},
2925\cfunction{PyNumber_InplaceOr()}, and \cfunction{PyNumber_InplaceXor()}).
Raymond Hettinger94fedf92005-08-17 12:23:45 +00002926Note, \cfunction{PyNumber_InplaceSubtract()} is also useful clearing
2927clearing a set (\code{s-=s}).
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002928
2929\begin{ctypedesc}{PySetObject}
2930 This subtype of \ctype{PyObject} is used to hold the internal data for
2931 both \class{set} and \class{frozenset} objects. It is like a
2932 \ctype{PyDictObject} in that it is a fixed size for small sets
2933 (much like tuple storage) and will point to a separate, variable sized
2934 block of memory for medium and large sized sets (much like list storage).
2935 None of the fields of this structure should be considered public and
2936 are subject to change. All access should be done through the
Raymond Hettinger94fedf92005-08-17 12:23:45 +00002937 documented API rather than by manipulating the values in the structure.
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002938
2939\end{ctypedesc}
2940
2941\begin{cvardesc}{PyTypeObject}{PySet_Type}
2942 This is an instance of \ctype{PyTypeObject} representing the Python
2943 \class{set} type.
2944\end{cvardesc}
2945
2946\begin{cvardesc}{PyTypeObject}{PyFrozenSet_Type}
2947 This is an instance of \ctype{PyTypeObject} representing the Python
2948 \class{frozenset} type.
2949\end{cvardesc}
2950
2951
2952The following type check macros work on pointers to any Python object.
2953Likewise, the constructor functions work with any iterable Python object.
2954
2955\begin{cfuncdesc}{int}{PyAnySet_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002956 Return true if \var{p} is a \class{set} object, a \class{frozenset}
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002957 object, or an instance of a subtype.
2958\end{cfuncdesc}
2959
2960\begin{cfuncdesc}{int}{PyAnySet_CheckExact}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002961 Return true if \var{p} is a \class{set} object or a \class{frozenset}
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002962 object but not an instance of a subtype.
2963\end{cfuncdesc}
2964
2965\begin{cfuncdesc}{int}{PyFrozenSet_CheckExact}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002966 Return true if \var{p} is a \class{frozenset} object
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002967 but not an instance of a subtype.
2968\end{cfuncdesc}
2969
2970\begin{cfuncdesc}{PyObject*}{PySet_New}{PyObject *iterable}
Georg Brandl99363b62005-09-03 07:27:26 +00002971 Return a new \class{set} containing objects returned by the
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002972 \var{iterable}. The \var{iterable} may be \NULL{} to create a
Georg Brandl99363b62005-09-03 07:27:26 +00002973 new empty set. Return the new set on success or \NULL{} on
2974 failure. Raise \exception{TypeError} if \var{iterable} is
Raymond Hettinger94fedf92005-08-17 12:23:45 +00002975 not actually iterable. The constructor is also useful for
2976 copying a set (\code{c=set(s)}).
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002977\end{cfuncdesc}
2978
2979\begin{cfuncdesc}{PyObject*}{PyFrozenSet_New}{PyObject *iterable}
Georg Brandl99363b62005-09-03 07:27:26 +00002980 Return a new \class{frozenset} containing objects returned by the
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002981 \var{iterable}. The \var{iterable} may be \NULL{} to create a
Georg Brandl99363b62005-09-03 07:27:26 +00002982 new empty frozenset. Return the new set on success or \NULL{} on
2983 failure. Raise \exception{TypeError} if \var{iterable} is
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002984 not actually iterable.
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002985\end{cfuncdesc}
2986
2987
2988The following functions and macros are available for instances of
2989\class{set} or \class{frozenset} or instances of their subtypes.
2990
2991\begin{cfuncdesc}{int}{PySet_Size}{PyObject *anyset}
Georg Brandl99363b62005-09-03 07:27:26 +00002992 Return the length of a \class{set} or \class{frozenset} object.
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002993 Equivalent to \samp{len(\var{anyset})}. Raises a
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00002994 \exception{PyExc_SystemError} if \var{anyset} is not a \class{set},
Raymond Hettingerbeb31012005-08-16 03:47:52 +00002995 \class{frozenset}, or an instance of a subtype.
2996 \bifuncindex{len}
2997\end{cfuncdesc}
2998
2999\begin{cfuncdesc}{int}{PySet_GET_SIZE}{PyObject *anyset}
3000 Macro form of \cfunction{PySet_Size()} without error checking.
3001\end{cfuncdesc}
3002
3003\begin{cfuncdesc}{int}{PySet_Contains}{PyObject *anyset, PyObject *key}
Georg Brandl99363b62005-09-03 07:27:26 +00003004 Return 1 if found, 0 if not found, and -1 if an error is
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003005 encountered. Unlike the Python \method{__contains__()} method, this
3006 function does not automatically convert unhashable sets into temporary
Georg Brandl99363b62005-09-03 07:27:26 +00003007 frozensets. Raise a \exception{TypeError} if the \var{key} is unhashable.
3008 Raise \exception{PyExc_SystemError} if \var{anyset} is not a \class{set},
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00003009 \class{frozenset}, or an instance of a subtype.
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003010\end{cfuncdesc}
3011
3012
3013The following functions are available for instances of \class{set} or
3014its subtypes but not for instances of \class{frozenset} or its subtypes.
3015
3016\begin{cfuncdesc}{int}{PySet_Add}{PyObject *set, PyObject *key}
Georg Brandl99363b62005-09-03 07:27:26 +00003017 Add \var{key} to a \class{set} instance. Does not apply to
3018 \class{frozenset} instances. Return 0 on success or -1 on failure.
3019 Raise a \exception{TypeError} if the \var{key} is unhashable.
3020 Raise a \exception{MemoryError} if there is no room to grow.
3021 Raise a \exception{SystemError} if \var{set} is an not an instance
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003022 of \class{set} or its subtype.
3023\end{cfuncdesc}
3024
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00003025\begin{cfuncdesc}{int}{PySet_Discard}{PyObject *set, PyObject *key}
Georg Brandl99363b62005-09-03 07:27:26 +00003026 Return 1 if found and removed, 0 if not found (no action taken),
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00003027 and -1 if an error is encountered. Does not raise \exception{KeyError}
Georg Brandl99363b62005-09-03 07:27:26 +00003028 for missing keys. Raise a \exception{TypeError} if the \var{key} is
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00003029 unhashable. Unlike the Python \method{discard()} method, this function
3030 does not automatically convert unhashable sets into temporary frozensets.
Georg Brandl99363b62005-09-03 07:27:26 +00003031 Raise \exception{PyExc_SystemError} if \var{set} is an not an instance
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00003032 of \class{set} or its subtype.
3033\end{cfuncdesc}
3034
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003035\begin{cfuncdesc}{PyObject*}{PySet_Pop}{PyObject *set}
Georg Brandl99363b62005-09-03 07:27:26 +00003036 Return a new reference to an arbitrary object in the \var{set},
3037 and removes the object from the \var{set}. Return \NULL{} on
3038 failure. Raise \exception{KeyError} if the set is empty.
3039 Raise a \exception{SystemError} if \var{set} is an not an instance
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003040 of \class{set} or its subtype.
3041\end{cfuncdesc}
3042
3043