blob: 3ffcf5a8b92d4c73159fe7de67a7c0c15a2243ac [file] [log] [blame]
Fred Drake3adf79e2001-10-12 19:01:43 +00001\chapter{Concrete Objects Layer \label{concrete}}
2
3
4The functions in this chapter are specific to certain Python object
5types. Passing them an object of the wrong type is not a good idea;
6if you receive an object from a Python program and you are not sure
7that it has the right type, you must perform a type check first;
8for example, to check that an object is a dictionary, use
9\cfunction{PyDict_Check()}. The chapter is structured like the
10``family tree'' of Python object types.
11
12\warning{While the functions described in this chapter carefully check
13the type of the objects which are passed in, many of them do not check
14for \NULL{} being passed instead of a valid object. Allowing \NULL{}
15to be passed in can cause memory access violations and immediate
16termination of the interpreter.}
17
18
19\section{Fundamental Objects \label{fundamental}}
20
Tim Petersf582b822001-12-11 18:51:08 +000021This section describes Python type objects and the singleton object
Fred Drake3adf79e2001-10-12 19:01:43 +000022\code{None}.
23
24
25\subsection{Type Objects \label{typeObjects}}
26
27\obindex{type}
28\begin{ctypedesc}{PyTypeObject}
29 The C structure of the objects used to describe built-in types.
30\end{ctypedesc}
31
32\begin{cvardesc}{PyObject*}{PyType_Type}
33 This is the type object for type objects; it is the same object as
Georg Brandl7572f032006-08-08 20:48:10 +000034 \code{type} and \code{types.TypeType} in the Python layer.
Fred Drake3adf79e2001-10-12 19:01:43 +000035 \withsubitem{(in module types)}{\ttindex{TypeType}}
36\end{cvardesc}
37
38\begin{cfuncdesc}{int}{PyType_Check}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +000039 Return true if the object \var{o} is a type object, including
40 instances of types derived from the standard type object. Return
Fred Drakee3c764b2002-04-10 17:52:52 +000041 false in all other cases.
42\end{cfuncdesc}
43
44\begin{cfuncdesc}{int}{PyType_CheckExact}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +000045 Return true if the object \var{o} is a type object, but not a
46 subtype of the standard type object. Return false in all other
Fred Drakee3c764b2002-04-10 17:52:52 +000047 cases.
48 \versionadded{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +000049\end{cfuncdesc}
50
51\begin{cfuncdesc}{int}{PyType_HasFeature}{PyObject *o, int feature}
Georg Brandl99363b62005-09-03 07:27:26 +000052 Return true if the type object \var{o} sets the feature
Fred Drake3adf79e2001-10-12 19:01:43 +000053 \var{feature}. Type features are denoted by single bit flags.
54\end{cfuncdesc}
55
Fred Drakee3c764b2002-04-10 17:52:52 +000056\begin{cfuncdesc}{int}{PyType_IS_GC}{PyObject *o}
57 Return true if the type object includes support for the cycle
58 detector; this tests the type flag \constant{Py_TPFLAGS_HAVE_GC}.
59 \versionadded{2.0}
60\end{cfuncdesc}
61
Fred Drake3adf79e2001-10-12 19:01:43 +000062\begin{cfuncdesc}{int}{PyType_IsSubtype}{PyTypeObject *a, PyTypeObject *b}
Georg Brandl99363b62005-09-03 07:27:26 +000063 Return true if \var{a} is a subtype of \var{b}.
Fred Drake3adf79e2001-10-12 19:01:43 +000064 \versionadded{2.2}
65\end{cfuncdesc}
66
67\begin{cfuncdesc}{PyObject*}{PyType_GenericAlloc}{PyTypeObject *type,
Martin v. Löwis29fafd82006-03-01 05:16:03 +000068 Py_ssize_t nitems}
Fred Drake3adf79e2001-10-12 19:01:43 +000069 \versionadded{2.2}
70\end{cfuncdesc}
71
72\begin{cfuncdesc}{PyObject*}{PyType_GenericNew}{PyTypeObject *type,
73 PyObject *args, PyObject *kwds}
74 \versionadded{2.2}
75\end{cfuncdesc}
76
77\begin{cfuncdesc}{int}{PyType_Ready}{PyTypeObject *type}
Fred Drake28de8d42002-04-12 16:15:10 +000078 Finalize a type object. This should be called on all type objects
79 to finish their initialization. This function is responsible for
Georg Brandl99363b62005-09-03 07:27:26 +000080 adding inherited slots from a type's base class. Return \code{0}
81 on success, or return \code{-1} and sets an exception on error.
Fred Drake3adf79e2001-10-12 19:01:43 +000082 \versionadded{2.2}
83\end{cfuncdesc}
84
85
86\subsection{The None Object \label{noneObject}}
87
Fred Drake7a700b82004-01-01 05:43:53 +000088\obindex{None}
Fred Drake3adf79e2001-10-12 19:01:43 +000089Note that the \ctype{PyTypeObject} for \code{None} is not directly
90exposed in the Python/C API. Since \code{None} is a singleton,
91testing for object identity (using \samp{==} in C) is sufficient.
92There is no \cfunction{PyNone_Check()} function for the same reason.
93
94\begin{cvardesc}{PyObject*}{Py_None}
95 The Python \code{None} object, denoting lack of value. This object
Fred Drake6ccdccd2002-03-12 20:12:54 +000096 has no methods. It needs to be treated just like any other object
97 with respect to reference counts.
Fred Drake3adf79e2001-10-12 19:01:43 +000098\end{cvardesc}
99
Brett Cannon35d83602003-11-09 04:15:30 +0000100\begin{csimplemacrodesc}{Py_RETURN_NONE}
Georg Brandl99363b62005-09-03 07:27:26 +0000101 Properly handle returning \cdata{Py_None} from within a C function.
Georg Brandl15eb97b2007-06-16 17:10:26 +0000102 \versionadded{2.4}
Brett Cannon35d83602003-11-09 04:15:30 +0000103\end{csimplemacrodesc}
104
Fred Drake3adf79e2001-10-12 19:01:43 +0000105
106\section{Numeric Objects \label{numericObjects}}
107
108\obindex{numeric}
109
110
111\subsection{Plain Integer Objects \label{intObjects}}
112
113\obindex{integer}
114\begin{ctypedesc}{PyIntObject}
115 This subtype of \ctype{PyObject} represents a Python integer
116 object.
117\end{ctypedesc}
118
119\begin{cvardesc}{PyTypeObject}{PyInt_Type}
Tim Petersf582b822001-12-11 18:51:08 +0000120 This instance of \ctype{PyTypeObject} represents the Python plain
Georg Brandl7572f032006-08-08 20:48:10 +0000121 integer type. This is the same object as \code{int} and
122 \code{types.IntType}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000123 \withsubitem{(in modules types)}{\ttindex{IntType}}
124\end{cvardesc}
125
Andrew M. Kuchling4eb1a002004-08-07 20:19:24 +0000126\begin{cfuncdesc}{int}{PyInt_Check}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000127 Return true if \var{o} is of type \cdata{PyInt_Type} or a subtype
Fred Drake3adf79e2001-10-12 19:01:43 +0000128 of \cdata{PyInt_Type}.
129 \versionchanged[Allowed subtypes to be accepted]{2.2}
130\end{cfuncdesc}
131
Andrew M. Kuchling4eb1a002004-08-07 20:19:24 +0000132\begin{cfuncdesc}{int}{PyInt_CheckExact}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000133 Return true if \var{o} is of type \cdata{PyInt_Type}, but not a
Fred Drake3adf79e2001-10-12 19:01:43 +0000134 subtype of \cdata{PyInt_Type}.
135 \versionadded{2.2}
136\end{cfuncdesc}
137
Skip Montanaro1ff49a72003-02-03 05:13:24 +0000138\begin{cfuncdesc}{PyObject*}{PyInt_FromString}{char *str, char **pend,
139 int base}
140 Return a new \ctype{PyIntObject} or \ctype{PyLongObject} based on the
141 string value in \var{str}, which is interpreted according to the radix in
Tim Peters9ddf40b2004-06-20 22:41:32 +0000142 \var{base}. If \var{pend} is non-\NULL{}, \code{*\var{pend}} will point to
Skip Montanaro1ff49a72003-02-03 05:13:24 +0000143 the first character in \var{str} which follows the representation of the
144 number. If \var{base} is \code{0}, the radix will be determined based on
145 the leading characters of \var{str}: if \var{str} starts with \code{'0x'}
146 or \code{'0X'}, radix 16 will be used; if \var{str} starts with
147 \code{'0'}, radix 8 will be used; otherwise radix 10 will be used. If
148 \var{base} is not \code{0}, it must be between \code{2} and \code{36},
149 inclusive. Leading spaces are ignored. If there are no digits,
150 \exception{ValueError} will be raised. If the string represents a number
151 too large to be contained within the machine's \ctype{long int} type and
152 overflow warnings are being suppressed, a \ctype{PyLongObject} will be
153 returned. If overflow warnings are not being suppressed, \NULL{} will be
154 returned in this case.
155\end{cfuncdesc}
156
Fred Drake3adf79e2001-10-12 19:01:43 +0000157\begin{cfuncdesc}{PyObject*}{PyInt_FromLong}{long ival}
Georg Brandl99363b62005-09-03 07:27:26 +0000158 Create a new integer object with a value of \var{ival}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000159
160 The current implementation keeps an array of integer objects for all
Georg Brandlb6e92c42006-03-27 22:09:16 +0000161 integers between \code{-5} and \code{256}, when you create an int in
Fred Drake3adf79e2001-10-12 19:01:43 +0000162 that range you actually just get back a reference to the existing
163 object. So it should be possible to change the value of \code{1}. I
164 suspect the behaviour of Python in this case is undefined. :-)
165\end{cfuncdesc}
166
Martin v. Löwis3b197542006-03-01 05:47:11 +0000167\begin{cfuncdesc}{PyObject*}{PyInt_FromSsize_t}{Py_ssize_t ival}
168 Create a new integer object with a value of \var{ival}.
169 If the value exceeds \code{LONG_MAX}, a long integer object is
170 returned.
171
172 \versionadded{2.5}
173\end{cfuncdesc}
174
Fred Drake3adf79e2001-10-12 19:01:43 +0000175\begin{cfuncdesc}{long}{PyInt_AsLong}{PyObject *io}
176 Will first attempt to cast the object to a \ctype{PyIntObject}, if
Martin v. Löwis3b197542006-03-01 05:47:11 +0000177 it is not already one, and then return its value. If there is an
178 error, \code{-1} is returned, and the caller should check
179 \code{PyErr_Occurred()} to find out whether there was an error, or
180 whether the value just happened to be -1.
Fred Drake3adf79e2001-10-12 19:01:43 +0000181\end{cfuncdesc}
182
183\begin{cfuncdesc}{long}{PyInt_AS_LONG}{PyObject *io}
Georg Brandl99363b62005-09-03 07:27:26 +0000184 Return the value of the object \var{io}. No error checking is
Fred Drake3adf79e2001-10-12 19:01:43 +0000185 performed.
186\end{cfuncdesc}
187
Thomas Heller34d7f092003-04-23 19:51:05 +0000188\begin{cfuncdesc}{unsigned long}{PyInt_AsUnsignedLongMask}{PyObject *io}
189 Will first attempt to cast the object to a \ctype{PyIntObject} or
Fred Drakec22b2992003-04-23 20:38:41 +0000190 \ctype{PyLongObject}, if it is not already one, and then return its
Thomas Heller34d7f092003-04-23 19:51:05 +0000191 value as unsigned long. This function does not check for overflow.
192 \versionadded{2.3}
193\end{cfuncdesc}
194
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000195\begin{cfuncdesc}{unsigned PY_LONG_LONG}{PyInt_AsUnsignedLongLongMask}{PyObject *io}
Thomas Heller34d7f092003-04-23 19:51:05 +0000196 Will first attempt to cast the object to a \ctype{PyIntObject} or
Fred Drakec22b2992003-04-23 20:38:41 +0000197 \ctype{PyLongObject}, if it is not already one, and then return its
Thomas Heller34d7f092003-04-23 19:51:05 +0000198 value as unsigned long long, without checking for overflow.
199 \versionadded{2.3}
200\end{cfuncdesc}
201
Martin v. Löwis3b197542006-03-01 05:47:11 +0000202\begin{cfuncdesc}{Py_ssize_t}{PyInt_AsSsize_t}{PyObject *io}
203 Will first attempt to cast the object to a \ctype{PyIntObject} or
204 \ctype{PyLongObject}, if it is not already one, and then return its
205 value as \ctype{Py_ssize_t}.
206 \versionadded{2.5}
207\end{cfuncdesc}
208
Fred Drake3adf79e2001-10-12 19:01:43 +0000209\begin{cfuncdesc}{long}{PyInt_GetMax}{}
Georg Brandl99363b62005-09-03 07:27:26 +0000210 Return the system's idea of the largest integer it can handle
Fred Drake3adf79e2001-10-12 19:01:43 +0000211 (\constant{LONG_MAX}\ttindex{LONG_MAX}, as defined in the system
212 header files).
213\end{cfuncdesc}
214
Fred Drake2be406b2004-08-03 16:02:35 +0000215\subsection{Boolean Objects \label{boolObjects}}
Skip Montanaro33ee76a2004-07-28 14:17:04 +0000216
217Booleans in Python are implemented as a subclass of integers. There
218are only two booleans, \constant{Py_False} and \constant{Py_True}. As
219such, the normal creation and deletion functions don't apply to
220booleans. The following macros are available, however.
221
Andrew M. Kuchling4eb1a002004-08-07 20:19:24 +0000222\begin{cfuncdesc}{int}{PyBool_Check}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000223 Return true if \var{o} is of type \cdata{PyBool_Type}.
Skip Montanaro33ee76a2004-07-28 14:17:04 +0000224 \versionadded{2.3}
225\end{cfuncdesc}
226
Skip Montanaro6d3db702004-07-29 02:16:04 +0000227\begin{cvardesc}{PyObject*}{Py_False}
228 The Python \code{False} object. This object has no methods. It needs to
229 be treated just like any other object with respect to reference counts.
230\end{cvardesc}
Skip Montanaro33ee76a2004-07-28 14:17:04 +0000231
Skip Montanaro6d3db702004-07-29 02:16:04 +0000232\begin{cvardesc}{PyObject*}{Py_True}
233 The Python \code{True} object. This object has no methods. It needs to
234 be treated just like any other object with respect to reference counts.
235\end{cvardesc}
236
237\begin{csimplemacrodesc}{Py_RETURN_FALSE}
238 Return \constant{Py_False} from a function, properly incrementing its
239 reference count.
240\versionadded{2.4}
241\end{csimplemacrodesc}
242
243\begin{csimplemacrodesc}{Py_RETURN_TRUE}
Andrew M. Kuchling4eb1a002004-08-07 20:19:24 +0000244 Return \constant{Py_True} from a function, properly incrementing its
245 reference count.
Skip Montanaro33ee76a2004-07-28 14:17:04 +0000246\versionadded{2.4}
Skip Montanaro6d3db702004-07-29 02:16:04 +0000247\end{csimplemacrodesc}
Skip Montanaro33ee76a2004-07-28 14:17:04 +0000248
Georg Brandl99363b62005-09-03 07:27:26 +0000249\begin{cfuncdesc}{PyObject*}{PyBool_FromLong}{long v}
Tim Peters8931ff12006-05-13 23:28:20 +0000250 Return a new reference to \constant{Py_True} or \constant{Py_False}
Georg Brandl99363b62005-09-03 07:27:26 +0000251 depending on the truth value of \var{v}.
Skip Montanaro33ee76a2004-07-28 14:17:04 +0000252\versionadded{2.3}
253\end{cfuncdesc}
Fred Drake3adf79e2001-10-12 19:01:43 +0000254
255\subsection{Long Integer Objects \label{longObjects}}
256
257\obindex{long integer}
258\begin{ctypedesc}{PyLongObject}
259 This subtype of \ctype{PyObject} represents a Python long integer
260 object.
261\end{ctypedesc}
262
263\begin{cvardesc}{PyTypeObject}{PyLong_Type}
264 This instance of \ctype{PyTypeObject} represents the Python long
Georg Brandl7572f032006-08-08 20:48:10 +0000265 integer type. This is the same object as \code{long} and
266 \code{types.LongType}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000267 \withsubitem{(in modules types)}{\ttindex{LongType}}
268\end{cvardesc}
269
270\begin{cfuncdesc}{int}{PyLong_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +0000271 Return true if its argument is a \ctype{PyLongObject} or a subtype
Fred Drake3adf79e2001-10-12 19:01:43 +0000272 of \ctype{PyLongObject}.
273 \versionchanged[Allowed subtypes to be accepted]{2.2}
274\end{cfuncdesc}
275
276\begin{cfuncdesc}{int}{PyLong_CheckExact}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +0000277 Return true if its argument is a \ctype{PyLongObject}, but not a
Fred Drake3adf79e2001-10-12 19:01:43 +0000278 subtype of \ctype{PyLongObject}.
279 \versionadded{2.2}
280\end{cfuncdesc}
281
282\begin{cfuncdesc}{PyObject*}{PyLong_FromLong}{long v}
Georg Brandl99363b62005-09-03 07:27:26 +0000283 Return a new \ctype{PyLongObject} object from \var{v}, or \NULL{}
Fred Drake3adf79e2001-10-12 19:01:43 +0000284 on failure.
285\end{cfuncdesc}
286
287\begin{cfuncdesc}{PyObject*}{PyLong_FromUnsignedLong}{unsigned long v}
Georg Brandl99363b62005-09-03 07:27:26 +0000288 Return a new \ctype{PyLongObject} object from a C \ctype{unsigned
Fred Drake3adf79e2001-10-12 19:01:43 +0000289 long}, or \NULL{} on failure.
290\end{cfuncdesc}
291
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000292\begin{cfuncdesc}{PyObject*}{PyLong_FromLongLong}{PY_LONG_LONG v}
Georg Brandl99363b62005-09-03 07:27:26 +0000293 Return a new \ctype{PyLongObject} object from a C \ctype{long long},
Fred Drake3adf79e2001-10-12 19:01:43 +0000294 or \NULL{} on failure.
295\end{cfuncdesc}
296
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000297\begin{cfuncdesc}{PyObject*}{PyLong_FromUnsignedLongLong}{unsigned PY_LONG_LONG v}
Georg Brandl99363b62005-09-03 07:27:26 +0000298 Return a new \ctype{PyLongObject} object from a C \ctype{unsigned
Fred Drake3adf79e2001-10-12 19:01:43 +0000299 long long}, or \NULL{} on failure.
300\end{cfuncdesc}
301
302\begin{cfuncdesc}{PyObject*}{PyLong_FromDouble}{double v}
Georg Brandl99363b62005-09-03 07:27:26 +0000303 Return a new \ctype{PyLongObject} object from the integer part of
Fred Drake3adf79e2001-10-12 19:01:43 +0000304 \var{v}, or \NULL{} on failure.
305\end{cfuncdesc}
306
307\begin{cfuncdesc}{PyObject*}{PyLong_FromString}{char *str, char **pend,
308 int base}
309 Return a new \ctype{PyLongObject} based on the string value in
310 \var{str}, which is interpreted according to the radix in
Tim Peters9ddf40b2004-06-20 22:41:32 +0000311 \var{base}. If \var{pend} is non-\NULL{}, \code{*\var{pend}} will
Fred Drake3adf79e2001-10-12 19:01:43 +0000312 point to the first character in \var{str} which follows the
313 representation of the number. If \var{base} is \code{0}, the radix
Skip Montanaro1ff49a72003-02-03 05:13:24 +0000314 will be determined based on the leading characters of \var{str}: if
Fred Drake3adf79e2001-10-12 19:01:43 +0000315 \var{str} starts with \code{'0x'} or \code{'0X'}, radix 16 will be
316 used; if \var{str} starts with \code{'0'}, radix 8 will be used;
317 otherwise radix 10 will be used. If \var{base} is not \code{0}, it
318 must be between \code{2} and \code{36}, inclusive. Leading spaces
319 are ignored. If there are no digits, \exception{ValueError} will be
320 raised.
321\end{cfuncdesc}
322
323\begin{cfuncdesc}{PyObject*}{PyLong_FromUnicode}{Py_UNICODE *u,
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000324 Py_ssize_t length, int base}
Fred Drake3adf79e2001-10-12 19:01:43 +0000325 Convert a sequence of Unicode digits to a Python long integer
326 value. The first parameter, \var{u}, points to the first character
327 of the Unicode string, \var{length} gives the number of characters,
328 and \var{base} is the radix for the conversion. The radix must be
329 in the range [2, 36]; if it is out of range, \exception{ValueError}
330 will be raised.
331 \versionadded{1.6}
332\end{cfuncdesc}
333
334\begin{cfuncdesc}{PyObject*}{PyLong_FromVoidPtr}{void *p}
335 Create a Python integer or long integer from the pointer \var{p}.
336 The pointer value can be retrieved from the resulting value using
337 \cfunction{PyLong_AsVoidPtr()}.
338 \versionadded{1.5.2}
Martin v. Löwis0bc2ab92006-04-10 20:28:17 +0000339 \versionchanged[If the integer is larger than LONG_MAX,
340 a positive long integer is returned]{2.5}
341 \end{cfuncdesc}
Fred Drake3adf79e2001-10-12 19:01:43 +0000342
343\begin{cfuncdesc}{long}{PyLong_AsLong}{PyObject *pylong}
Georg Brandl99363b62005-09-03 07:27:26 +0000344 Return a C \ctype{long} representation of the contents of
Fred Drake3adf79e2001-10-12 19:01:43 +0000345 \var{pylong}. If \var{pylong} is greater than
346 \constant{LONG_MAX}\ttindex{LONG_MAX}, an \exception{OverflowError}
347 is raised.
348 \withsubitem{(built-in exception)}{\ttindex{OverflowError}}
349\end{cfuncdesc}
350
351\begin{cfuncdesc}{unsigned long}{PyLong_AsUnsignedLong}{PyObject *pylong}
Georg Brandl99363b62005-09-03 07:27:26 +0000352 Return a C \ctype{unsigned long} representation of the contents of
Fred Drake3adf79e2001-10-12 19:01:43 +0000353 \var{pylong}. If \var{pylong} is greater than
354 \constant{ULONG_MAX}\ttindex{ULONG_MAX}, an
355 \exception{OverflowError} is raised.
Tim Petersf582b822001-12-11 18:51:08 +0000356 \withsubitem{(built-in exception)}{\ttindex{OverflowError}}
Fred Drake3adf79e2001-10-12 19:01:43 +0000357\end{cfuncdesc}
358
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000359\begin{cfuncdesc}{PY_LONG_LONG}{PyLong_AsLongLong}{PyObject *pylong}
Fred Drake3adf79e2001-10-12 19:01:43 +0000360 Return a C \ctype{long long} from a Python long integer. If
361 \var{pylong} cannot be represented as a \ctype{long long}, an
362 \exception{OverflowError} will be raised.
363 \versionadded{2.2}
364\end{cfuncdesc}
365
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000366\begin{cfuncdesc}{unsigned PY_LONG_LONG}{PyLong_AsUnsignedLongLong}{PyObject
Fred Drake3adf79e2001-10-12 19:01:43 +0000367 *pylong}
368 Return a C \ctype{unsigned long long} from a Python long integer.
369 If \var{pylong} cannot be represented as an \ctype{unsigned long
370 long}, an \exception{OverflowError} will be raised if the value is
371 positive, or a \exception{TypeError} will be raised if the value is
372 negative.
373 \versionadded{2.2}
374\end{cfuncdesc}
375
Thomas Heller34d7f092003-04-23 19:51:05 +0000376\begin{cfuncdesc}{unsigned long}{PyLong_AsUnsignedLongMask}{PyObject *io}
377 Return a C \ctype{unsigned long} from a Python long integer, without
378 checking for overflow.
379 \versionadded{2.3}
380\end{cfuncdesc}
381
Thomas Heller653f23c2006-07-06 15:06:05 +0000382\begin{cfuncdesc}{unsigned PY_LONG_LONG}{PyLong_AsUnsignedLongLongMask}{PyObject *io}
Thomas Heller34d7f092003-04-23 19:51:05 +0000383 Return a C \ctype{unsigned long long} from a Python long integer, without
384 checking for overflow.
385 \versionadded{2.3}
386\end{cfuncdesc}
387
Fred Drake3adf79e2001-10-12 19:01:43 +0000388\begin{cfuncdesc}{double}{PyLong_AsDouble}{PyObject *pylong}
Georg Brandl99363b62005-09-03 07:27:26 +0000389 Return a C \ctype{double} representation of the contents of
Fred Drake3adf79e2001-10-12 19:01:43 +0000390 \var{pylong}. If \var{pylong} cannot be approximately represented
391 as a \ctype{double}, an \exception{OverflowError} exception is
392 raised and \code{-1.0} will be returned.
393\end{cfuncdesc}
394
395\begin{cfuncdesc}{void*}{PyLong_AsVoidPtr}{PyObject *pylong}
396 Convert a Python integer or long integer \var{pylong} to a C
397 \ctype{void} pointer. If \var{pylong} cannot be converted, an
398 \exception{OverflowError} will be raised. This is only assured to
399 produce a usable \ctype{void} pointer for values created with
400 \cfunction{PyLong_FromVoidPtr()}.
401 \versionadded{1.5.2}
Martin v. Löwis0bc2ab92006-04-10 20:28:17 +0000402 \versionchanged[For values outside 0..LONG_MAX, both signed and
403 unsigned integers are acccepted]{2.5}
Fred Drake3adf79e2001-10-12 19:01:43 +0000404\end{cfuncdesc}
405
406
407\subsection{Floating Point Objects \label{floatObjects}}
408
409\obindex{floating point}
410\begin{ctypedesc}{PyFloatObject}
411 This subtype of \ctype{PyObject} represents a Python floating point
412 object.
413\end{ctypedesc}
414
415\begin{cvardesc}{PyTypeObject}{PyFloat_Type}
416 This instance of \ctype{PyTypeObject} represents the Python floating
Georg Brandl7572f032006-08-08 20:48:10 +0000417 point type. This is the same object as \code{float} and
418 \code{types.FloatType}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000419 \withsubitem{(in modules types)}{\ttindex{FloatType}}
420\end{cvardesc}
421
422\begin{cfuncdesc}{int}{PyFloat_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +0000423 Return true if its argument is a \ctype{PyFloatObject} or a subtype
Fred Drake3adf79e2001-10-12 19:01:43 +0000424 of \ctype{PyFloatObject}.
425 \versionchanged[Allowed subtypes to be accepted]{2.2}
426\end{cfuncdesc}
427
428\begin{cfuncdesc}{int}{PyFloat_CheckExact}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +0000429 Return true if its argument is a \ctype{PyFloatObject}, but not a
Fred Drake3adf79e2001-10-12 19:01:43 +0000430 subtype of \ctype{PyFloatObject}.
431 \versionadded{2.2}
432\end{cfuncdesc}
433
Skip Montanaro1ff49a72003-02-03 05:13:24 +0000434\begin{cfuncdesc}{PyObject*}{PyFloat_FromString}{PyObject *str, char **pend}
Georg Brandl99363b62005-09-03 07:27:26 +0000435 Create a \ctype{PyFloatObject} object based on the string value in
Skip Montanaro1ff49a72003-02-03 05:13:24 +0000436 \var{str}, or \NULL{} on failure. The \var{pend} argument is ignored. It
437 remains only for backward compatibility.
Skip Montanaroae31e9b2003-02-03 03:56:36 +0000438\end{cfuncdesc}
439
Fred Drake3adf79e2001-10-12 19:01:43 +0000440\begin{cfuncdesc}{PyObject*}{PyFloat_FromDouble}{double v}
Georg Brandl99363b62005-09-03 07:27:26 +0000441 Create a \ctype{PyFloatObject} object from \var{v}, or \NULL{} on
Fred Drake3adf79e2001-10-12 19:01:43 +0000442 failure.
443\end{cfuncdesc}
444
445\begin{cfuncdesc}{double}{PyFloat_AsDouble}{PyObject *pyfloat}
Georg Brandl99363b62005-09-03 07:27:26 +0000446 Return a C \ctype{double} representation of the contents of
Fred Drake3adf79e2001-10-12 19:01:43 +0000447 \var{pyfloat}.
448\end{cfuncdesc}
449
450\begin{cfuncdesc}{double}{PyFloat_AS_DOUBLE}{PyObject *pyfloat}
Georg Brandl99363b62005-09-03 07:27:26 +0000451 Return a C \ctype{double} representation of the contents of
Fred Drake3adf79e2001-10-12 19:01:43 +0000452 \var{pyfloat}, but without error checking.
453\end{cfuncdesc}
454
455
456\subsection{Complex Number Objects \label{complexObjects}}
457
458\obindex{complex number}
459Python's complex number objects are implemented as two distinct types
460when viewed from the C API: one is the Python object exposed to
461Python programs, and the other is a C structure which represents the
462actual complex number value. The API provides functions for working
463with both.
464
465\subsubsection{Complex Numbers as C Structures}
466
467Note that the functions which accept these structures as parameters
468and return them as results do so \emph{by value} rather than
469dereferencing them through pointers. This is consistent throughout
470the API.
471
472\begin{ctypedesc}{Py_complex}
473 The C structure which corresponds to the value portion of a Python
474 complex number object. Most of the functions for dealing with
475 complex number objects use structures of this type as input or
476 output values, as appropriate. It is defined as:
477
478\begin{verbatim}
479typedef struct {
480 double real;
481 double imag;
482} Py_complex;
483\end{verbatim}
484\end{ctypedesc}
485
486\begin{cfuncdesc}{Py_complex}{_Py_c_sum}{Py_complex left, Py_complex right}
487 Return the sum of two complex numbers, using the C
488 \ctype{Py_complex} representation.
489\end{cfuncdesc}
490
491\begin{cfuncdesc}{Py_complex}{_Py_c_diff}{Py_complex left, Py_complex right}
492 Return the difference between two complex numbers, using the C
493 \ctype{Py_complex} representation.
494\end{cfuncdesc}
495
496\begin{cfuncdesc}{Py_complex}{_Py_c_neg}{Py_complex complex}
497 Return the negation of the complex number \var{complex}, using the C
498 \ctype{Py_complex} representation.
499\end{cfuncdesc}
500
501\begin{cfuncdesc}{Py_complex}{_Py_c_prod}{Py_complex left, Py_complex right}
502 Return the product of two complex numbers, using the C
503 \ctype{Py_complex} representation.
504\end{cfuncdesc}
505
506\begin{cfuncdesc}{Py_complex}{_Py_c_quot}{Py_complex dividend,
507 Py_complex divisor}
508 Return the quotient of two complex numbers, using the C
509 \ctype{Py_complex} representation.
510\end{cfuncdesc}
511
512\begin{cfuncdesc}{Py_complex}{_Py_c_pow}{Py_complex num, Py_complex exp}
513 Return the exponentiation of \var{num} by \var{exp}, using the C
514 \ctype{Py_complex} representation.
515\end{cfuncdesc}
516
517
518\subsubsection{Complex Numbers as Python Objects}
519
520\begin{ctypedesc}{PyComplexObject}
521 This subtype of \ctype{PyObject} represents a Python complex number
522 object.
523\end{ctypedesc}
524
525\begin{cvardesc}{PyTypeObject}{PyComplex_Type}
526 This instance of \ctype{PyTypeObject} represents the Python complex
Georg Brandl7572f032006-08-08 20:48:10 +0000527 number type. It is the same object as \code{complex} and
528 \code{types.ComplexType}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000529\end{cvardesc}
530
531\begin{cfuncdesc}{int}{PyComplex_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +0000532 Return true if its argument is a \ctype{PyComplexObject} or a
Fred Drake3adf79e2001-10-12 19:01:43 +0000533 subtype of \ctype{PyComplexObject}.
534 \versionchanged[Allowed subtypes to be accepted]{2.2}
535\end{cfuncdesc}
536
537\begin{cfuncdesc}{int}{PyComplex_CheckExact}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +0000538 Return true if its argument is a \ctype{PyComplexObject}, but not a
Fred Drake3adf79e2001-10-12 19:01:43 +0000539 subtype of \ctype{PyComplexObject}.
540 \versionadded{2.2}
541\end{cfuncdesc}
542
543\begin{cfuncdesc}{PyObject*}{PyComplex_FromCComplex}{Py_complex v}
544 Create a new Python complex number object from a C
545 \ctype{Py_complex} value.
546\end{cfuncdesc}
547
548\begin{cfuncdesc}{PyObject*}{PyComplex_FromDoubles}{double real, double imag}
Georg Brandl99363b62005-09-03 07:27:26 +0000549 Return a new \ctype{PyComplexObject} object from \var{real} and
Fred Drake3adf79e2001-10-12 19:01:43 +0000550 \var{imag}.
551\end{cfuncdesc}
552
553\begin{cfuncdesc}{double}{PyComplex_RealAsDouble}{PyObject *op}
Georg Brandl99363b62005-09-03 07:27:26 +0000554 Return the real part of \var{op} as a C \ctype{double}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000555\end{cfuncdesc}
556
557\begin{cfuncdesc}{double}{PyComplex_ImagAsDouble}{PyObject *op}
Georg Brandl99363b62005-09-03 07:27:26 +0000558 Return the imaginary part of \var{op} as a C \ctype{double}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000559\end{cfuncdesc}
560
561\begin{cfuncdesc}{Py_complex}{PyComplex_AsCComplex}{PyObject *op}
Georg Brandl99363b62005-09-03 07:27:26 +0000562 Return the \ctype{Py_complex} value of the complex number
Fred Drake3adf79e2001-10-12 19:01:43 +0000563 \var{op}.
564\end{cfuncdesc}
565
566
567
568\section{Sequence Objects \label{sequenceObjects}}
569
570\obindex{sequence}
Tim Petersf582b822001-12-11 18:51:08 +0000571Generic operations on sequence objects were discussed in the previous
572chapter; this section deals with the specific kinds of sequence
Fred Drake3adf79e2001-10-12 19:01:43 +0000573objects that are intrinsic to the Python language.
574
575
576\subsection{String Objects \label{stringObjects}}
577
578These functions raise \exception{TypeError} when expecting a string
579parameter and are called with a non-string parameter.
580
581\obindex{string}
582\begin{ctypedesc}{PyStringObject}
583 This subtype of \ctype{PyObject} represents a Python string object.
584\end{ctypedesc}
585
586\begin{cvardesc}{PyTypeObject}{PyString_Type}
587 This instance of \ctype{PyTypeObject} represents the Python string
Georg Brandl7572f032006-08-08 20:48:10 +0000588 type; it is the same object as \code{str} and \code{types.StringType}
589 in the Python layer.
Fred Drake3adf79e2001-10-12 19:01:43 +0000590 \withsubitem{(in module types)}{\ttindex{StringType}}.
591\end{cvardesc}
592
593\begin{cfuncdesc}{int}{PyString_Check}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000594 Return true if the object \var{o} is a string object or an instance
Fred Drake3adf79e2001-10-12 19:01:43 +0000595 of a subtype of the string type.
596 \versionchanged[Allowed subtypes to be accepted]{2.2}
597\end{cfuncdesc}
598
599\begin{cfuncdesc}{int}{PyString_CheckExact}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000600 Return true if the object \var{o} is a string object, but not an
Fred Drake3adf79e2001-10-12 19:01:43 +0000601 instance of a subtype of the string type.
602 \versionadded{2.2}
603\end{cfuncdesc}
604
605\begin{cfuncdesc}{PyObject*}{PyString_FromString}{const char *v}
Georg Brandl09889042006-09-30 12:03:02 +0000606 Return a new string object with a copy of the string \var{v} as value
607 on success, and \NULL{} on failure. The parameter \var{v} must not be
608 \NULL{}; it will not be checked.
Fred Drake3adf79e2001-10-12 19:01:43 +0000609\end{cfuncdesc}
610
611\begin{cfuncdesc}{PyObject*}{PyString_FromStringAndSize}{const char *v,
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000612 Py_ssize_t len}
Georg Brandl09889042006-09-30 12:03:02 +0000613 Return a new string object with a copy of the string \var{v} as value
614 and length \var{len} on success, and \NULL{} on failure. If \var{v} is
Tim Peters9ddf40b2004-06-20 22:41:32 +0000615 \NULL{}, the contents of the string are uninitialized.
Fred Drake3adf79e2001-10-12 19:01:43 +0000616\end{cfuncdesc}
617
618\begin{cfuncdesc}{PyObject*}{PyString_FromFormat}{const char *format, ...}
Georg Brandl99363b62005-09-03 07:27:26 +0000619 Take a C \cfunction{printf()}-style \var{format} string and a
620 variable number of arguments, calculate the size of the resulting
621 Python string and return a string with the values formatted into
Fred Drake3adf79e2001-10-12 19:01:43 +0000622 it. The variable arguments must be C types and must correspond
623 exactly to the format characters in the \var{format} string. The
624 following format characters are allowed:
625
Tim Peters8931ff12006-05-13 23:28:20 +0000626 % This should be exactly the same as the table in PyErr_Format.
627 % One should just refer to the other.
628
629 % The descriptions for %zd and %zu are wrong, but the truth is complicated
630 % because not all compilers support the %z width modifier -- we fake it
631 % when necessary via interpolating PY_FORMAT_SIZE_T.
632
633 % %u, %lu, %zu should have "new in Python 2.5" blurbs.
634
Fred Drake3adf79e2001-10-12 19:01:43 +0000635 \begin{tableiii}{l|l|l}{member}{Format Characters}{Type}{Comment}
636 \lineiii{\%\%}{\emph{n/a}}{The literal \% character.}
637 \lineiii{\%c}{int}{A single character, represented as an C int.}
638 \lineiii{\%d}{int}{Exactly equivalent to \code{printf("\%d")}.}
Tim Peters8931ff12006-05-13 23:28:20 +0000639 \lineiii{\%u}{unsigned int}{Exactly equivalent to \code{printf("\%u")}.}
Fred Drake3adf79e2001-10-12 19:01:43 +0000640 \lineiii{\%ld}{long}{Exactly equivalent to \code{printf("\%ld")}.}
Tim Peters8931ff12006-05-13 23:28:20 +0000641 \lineiii{\%lu}{unsigned long}{Exactly equivalent to \code{printf("\%lu")}.}
642 \lineiii{\%zd}{Py_ssize_t}{Exactly equivalent to \code{printf("\%zd")}.}
Tim Peterse6d95062006-05-13 23:31:05 +0000643 \lineiii{\%zu}{size_t}{Exactly equivalent to \code{printf("\%zu")}.}
Fred Drake3adf79e2001-10-12 19:01:43 +0000644 \lineiii{\%i}{int}{Exactly equivalent to \code{printf("\%i")}.}
645 \lineiii{\%x}{int}{Exactly equivalent to \code{printf("\%x")}.}
646 \lineiii{\%s}{char*}{A null-terminated C character array.}
647 \lineiii{\%p}{void*}{The hex representation of a C pointer.
648 Mostly equivalent to \code{printf("\%p")} except that it is
649 guaranteed to start with the literal \code{0x} regardless of
650 what the platform's \code{printf} yields.}
651 \end{tableiii}
Tim Peters8931ff12006-05-13 23:28:20 +0000652
653 An unrecognized format character causes all the rest of the format
654 string to be copied as-is to the result string, and any extra
655 arguments discarded.
Fred Drake3adf79e2001-10-12 19:01:43 +0000656\end{cfuncdesc}
657
658\begin{cfuncdesc}{PyObject*}{PyString_FromFormatV}{const char *format,
659 va_list vargs}
660 Identical to \function{PyString_FromFormat()} except that it takes
661 exactly two arguments.
662\end{cfuncdesc}
663
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000664\begin{cfuncdesc}{Py_ssize_t}{PyString_Size}{PyObject *string}
Georg Brandl99363b62005-09-03 07:27:26 +0000665 Return the length of the string in string object \var{string}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000666\end{cfuncdesc}
667
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000668\begin{cfuncdesc}{Py_ssize_t}{PyString_GET_SIZE}{PyObject *string}
Fred Drake3adf79e2001-10-12 19:01:43 +0000669 Macro form of \cfunction{PyString_Size()} but without error
670 checking.
671\end{cfuncdesc}
672
673\begin{cfuncdesc}{char*}{PyString_AsString}{PyObject *string}
Georg Brandl99363b62005-09-03 07:27:26 +0000674 Return a NUL-terminated representation of the contents of
Fred Drake3adf79e2001-10-12 19:01:43 +0000675 \var{string}. The pointer refers to the internal buffer of
676 \var{string}, not a copy. The data must not be modified in any way,
677 unless the string was just created using
678 \code{PyString_FromStringAndSize(NULL, \var{size})}.
Fred Drake4b247262002-10-22 20:20:20 +0000679 It must not be deallocated. If \var{string} is a Unicode object,
680 this function computes the default encoding of \var{string} and
681 operates on that. If \var{string} is not a string object at all,
682 \cfunction{PyString_AsString()} returns \NULL{} and raises
683 \exception{TypeError}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000684\end{cfuncdesc}
685
686\begin{cfuncdesc}{char*}{PyString_AS_STRING}{PyObject *string}
687 Macro form of \cfunction{PyString_AsString()} but without error
Fred Drake4b247262002-10-22 20:20:20 +0000688 checking. Only string objects are supported; no Unicode objects
689 should be passed.
Fred Drake3adf79e2001-10-12 19:01:43 +0000690\end{cfuncdesc}
691
692\begin{cfuncdesc}{int}{PyString_AsStringAndSize}{PyObject *obj,
693 char **buffer,
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000694 Py_ssize_t *length}
Georg Brandl99363b62005-09-03 07:27:26 +0000695 Return a NUL-terminated representation of the contents of the
Fred Drake3adf79e2001-10-12 19:01:43 +0000696 object \var{obj} through the output variables \var{buffer} and
697 \var{length}.
698
699 The function accepts both string and Unicode objects as input. For
700 Unicode objects it returns the default encoded version of the
Tim Peters9ddf40b2004-06-20 22:41:32 +0000701 object. If \var{length} is \NULL{}, the resulting buffer may not
Fred Drake4b247262002-10-22 20:20:20 +0000702 contain NUL characters; if it does, the function returns \code{-1}
703 and a \exception{TypeError} is raised.
Fred Drake3adf79e2001-10-12 19:01:43 +0000704
705 The buffer refers to an internal string buffer of \var{obj}, not a
706 copy. The data must not be modified in any way, unless the string
707 was just created using \code{PyString_FromStringAndSize(NULL,
Fred Drake4b247262002-10-22 20:20:20 +0000708 \var{size})}. It must not be deallocated. If \var{string} is a
709 Unicode object, this function computes the default encoding of
710 \var{string} and operates on that. If \var{string} is not a string
Tim Peters8931ff12006-05-13 23:28:20 +0000711 object at all, \cfunction{PyString_AsStringAndSize()} returns
Georg Brandle53475d2005-09-28 12:53:12 +0000712 \code{-1} and raises \exception{TypeError}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000713\end{cfuncdesc}
714
715\begin{cfuncdesc}{void}{PyString_Concat}{PyObject **string,
716 PyObject *newpart}
Georg Brandl99363b62005-09-03 07:27:26 +0000717 Create a new string object in \var{*string} containing the contents
Fred Drake3adf79e2001-10-12 19:01:43 +0000718 of \var{newpart} appended to \var{string}; the caller will own the
719 new reference. The reference to the old value of \var{string} will
720 be stolen. If the new string cannot be created, the old reference
721 to \var{string} will still be discarded and the value of
Tim Peters9ddf40b2004-06-20 22:41:32 +0000722 \var{*string} will be set to \NULL{}; the appropriate exception will
Fred Drake3adf79e2001-10-12 19:01:43 +0000723 be set.
724\end{cfuncdesc}
725
726\begin{cfuncdesc}{void}{PyString_ConcatAndDel}{PyObject **string,
727 PyObject *newpart}
Georg Brandl99363b62005-09-03 07:27:26 +0000728 Create a new string object in \var{*string} containing the contents
Fred Drake3adf79e2001-10-12 19:01:43 +0000729 of \var{newpart} appended to \var{string}. This version decrements
730 the reference count of \var{newpart}.
731\end{cfuncdesc}
732
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000733\begin{cfuncdesc}{int}{_PyString_Resize}{PyObject **string, Py_ssize_t newsize}
Fred Drake3adf79e2001-10-12 19:01:43 +0000734 A way to resize a string object even though it is ``immutable''.
735 Only use this to build up a brand new string object; don't use this
Tim Peters5de98422002-04-27 18:44:32 +0000736 if the string may already be known in other parts of the code. It
737 is an error to call this function if the refcount on the input string
738 object is not one.
739 Pass the address of an existing string object as an lvalue (it may
740 be written into), and the new size desired. On success, \var{*string}
Fred Drake432425e2002-04-29 15:17:16 +0000741 holds the resized string object and \code{0} is returned; the address in
Tim Peters5de98422002-04-27 18:44:32 +0000742 \var{*string} may differ from its input value. If the
743 reallocation fails, the original string object at \var{*string} is
744 deallocated, \var{*string} is set to \NULL{}, a memory exception is set,
Fred Drake432425e2002-04-29 15:17:16 +0000745 and \code{-1} is returned.
Fred Drake3adf79e2001-10-12 19:01:43 +0000746\end{cfuncdesc}
747
748\begin{cfuncdesc}{PyObject*}{PyString_Format}{PyObject *format,
749 PyObject *args}
Georg Brandl99363b62005-09-03 07:27:26 +0000750 Return a new string object from \var{format} and \var{args}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000751 Analogous to \code{\var{format} \%\ \var{args}}. The \var{args}
752 argument must be a tuple.
753\end{cfuncdesc}
754
755\begin{cfuncdesc}{void}{PyString_InternInPlace}{PyObject **string}
756 Intern the argument \var{*string} in place. The argument must be
757 the address of a pointer variable pointing to a Python string
758 object. If there is an existing interned string that is the same as
759 \var{*string}, it sets \var{*string} to it (decrementing the
760 reference count of the old string object and incrementing the
761 reference count of the interned string object), otherwise it leaves
762 \var{*string} alone and interns it (incrementing its reference
763 count). (Clarification: even though there is a lot of talk about
764 reference counts, think of this function as reference-count-neutral;
765 you own the object after the call if and only if you owned it before
766 the call.)
767\end{cfuncdesc}
768
769\begin{cfuncdesc}{PyObject*}{PyString_InternFromString}{const char *v}
770 A combination of \cfunction{PyString_FromString()} and
771 \cfunction{PyString_InternInPlace()}, returning either a new string
772 object that has been interned, or a new (``owned'') reference to an
773 earlier interned string object with the same value.
774\end{cfuncdesc}
775
776\begin{cfuncdesc}{PyObject*}{PyString_Decode}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000777 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +0000778 const char *encoding,
779 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +0000780 Create an object by decoding \var{size} bytes of the encoded
Fred Drake3adf79e2001-10-12 19:01:43 +0000781 buffer \var{s} using the codec registered for
782 \var{encoding}. \var{encoding} and \var{errors} have the same
783 meaning as the parameters of the same name in the
784 \function{unicode()} built-in function. The codec to be used is
Georg Brandl99363b62005-09-03 07:27:26 +0000785 looked up using the Python codec registry. Return \NULL{} if
Fred Drake3adf79e2001-10-12 19:01:43 +0000786 an exception was raised by the codec.
787\end{cfuncdesc}
788
789\begin{cfuncdesc}{PyObject*}{PyString_AsDecodedObject}{PyObject *str,
790 const char *encoding,
791 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +0000792 Decode a string object by passing it to the codec registered for
793 \var{encoding} and return the result as Python
Fred Drake3adf79e2001-10-12 19:01:43 +0000794 object. \var{encoding} and \var{errors} have the same meaning as the
795 parameters of the same name in the string \method{encode()} method.
796 The codec to be used is looked up using the Python codec registry.
Georg Brandl99363b62005-09-03 07:27:26 +0000797 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +0000798\end{cfuncdesc}
799
800\begin{cfuncdesc}{PyObject*}{PyString_Encode}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000801 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +0000802 const char *encoding,
803 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +0000804 Encode the \ctype{char} buffer of the given size by passing it to
805 the codec registered for \var{encoding} and return a Python object.
Fred Drake3adf79e2001-10-12 19:01:43 +0000806 \var{encoding} and \var{errors} have the same meaning as the
807 parameters of the same name in the string \method{encode()} method.
808 The codec to be used is looked up using the Python codec
Georg Brandl99363b62005-09-03 07:27:26 +0000809 registry. Return \NULL{} if an exception was raised by the
Fred Drake3adf79e2001-10-12 19:01:43 +0000810 codec.
811\end{cfuncdesc}
812
813\begin{cfuncdesc}{PyObject*}{PyString_AsEncodedObject}{PyObject *str,
814 const char *encoding,
815 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +0000816 Encode a string object using the codec registered for
817 \var{encoding} and return the result as Python object.
Fred Drake3adf79e2001-10-12 19:01:43 +0000818 \var{encoding} and \var{errors} have the same meaning as the
819 parameters of the same name in the string \method{encode()} method.
820 The codec to be used is looked up using the Python codec registry.
Georg Brandl99363b62005-09-03 07:27:26 +0000821 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +0000822\end{cfuncdesc}
823
824
825\subsection{Unicode Objects \label{unicodeObjects}}
826\sectionauthor{Marc-Andre Lemburg}{mal@lemburg.com}
827
828%--- Unicode Type -------------------------------------------------------
829
830These are the basic Unicode object types used for the Unicode
831implementation in Python:
832
833\begin{ctypedesc}{Py_UNICODE}
Marc-André Lemburgdf4f6e92005-10-10 19:08:41 +0000834 This type represents the storage type which is used by Python
835 internally as basis for holding Unicode ordinals. Python's default
836 builds use a 16-bit type for \ctype{Py_UNICODE} and store Unicode
837 values internally as UCS2. It is also possible to build a UCS4
838 version of Python (most recent Linux distributions come with UCS4
839 builds of Python). These builds then use a 32-bit type for
840 \ctype{Py_UNICODE} and store Unicode data internally as UCS4. On
841 platforms where \ctype{wchar_t} is available and compatible with the
842 chosen Python Unicode build variant, \ctype{Py_UNICODE} is a typedef
843 alias for \ctype{wchar_t} to enhance native platform compatibility.
844 On all other platforms, \ctype{Py_UNICODE} is a typedef alias for
845 either \ctype{unsigned short} (UCS2) or \ctype{unsigned long}
846 (UCS4).
Fred Drake3adf79e2001-10-12 19:01:43 +0000847\end{ctypedesc}
848
Marc-André Lemburgdf4f6e92005-10-10 19:08:41 +0000849Note that UCS2 and UCS4 Python builds are not binary compatible.
850Please keep this in mind when writing extensions or interfaces.
851
Fred Drake3adf79e2001-10-12 19:01:43 +0000852\begin{ctypedesc}{PyUnicodeObject}
853 This subtype of \ctype{PyObject} represents a Python Unicode object.
854\end{ctypedesc}
855
856\begin{cvardesc}{PyTypeObject}{PyUnicode_Type}
857 This instance of \ctype{PyTypeObject} represents the Python Unicode
Georg Brandl7572f032006-08-08 20:48:10 +0000858 type. It is exposed to Python code as \code{unicode} and
859 \code{types.UnicodeType}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000860\end{cvardesc}
861
862The following APIs are really C macros and can be used to do fast
863checks and to access internal read-only data of Unicode objects:
864
865\begin{cfuncdesc}{int}{PyUnicode_Check}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000866 Return true if the object \var{o} is a Unicode object or an
Fred Drake3adf79e2001-10-12 19:01:43 +0000867 instance of a Unicode subtype.
868 \versionchanged[Allowed subtypes to be accepted]{2.2}
869\end{cfuncdesc}
870
871\begin{cfuncdesc}{int}{PyUnicode_CheckExact}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000872 Return true if the object \var{o} is a Unicode object, but not an
Fred Drake3adf79e2001-10-12 19:01:43 +0000873 instance of a subtype.
874 \versionadded{2.2}
875\end{cfuncdesc}
876
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000877\begin{cfuncdesc}{Py_ssize_t}{PyUnicode_GET_SIZE}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000878 Return the size of the object. \var{o} has to be a
Fred Drake3adf79e2001-10-12 19:01:43 +0000879 \ctype{PyUnicodeObject} (not checked).
880\end{cfuncdesc}
881
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000882\begin{cfuncdesc}{Py_ssize_t}{PyUnicode_GET_DATA_SIZE}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000883 Return the size of the object's internal buffer in bytes. \var{o}
Fred Drake3adf79e2001-10-12 19:01:43 +0000884 has to be a \ctype{PyUnicodeObject} (not checked).
885\end{cfuncdesc}
886
887\begin{cfuncdesc}{Py_UNICODE*}{PyUnicode_AS_UNICODE}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000888 Return a pointer to the internal \ctype{Py_UNICODE} buffer of the
Fred Drake3adf79e2001-10-12 19:01:43 +0000889 object. \var{o} has to be a \ctype{PyUnicodeObject} (not checked).
890\end{cfuncdesc}
891
892\begin{cfuncdesc}{const char*}{PyUnicode_AS_DATA}{PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +0000893 Return a pointer to the internal buffer of the object.
Fred Drake3adf79e2001-10-12 19:01:43 +0000894 \var{o} has to be a \ctype{PyUnicodeObject} (not checked).
895\end{cfuncdesc}
896
897% --- Unicode character properties ---------------------------------------
898
899Unicode provides many different character properties. The most often
900needed ones are available through these macros which are mapped to C
901functions depending on the Python configuration.
902
903\begin{cfuncdesc}{int}{Py_UNICODE_ISSPACE}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000904 Return 1 or 0 depending on whether \var{ch} is a whitespace
Fred Drake3adf79e2001-10-12 19:01:43 +0000905 character.
906\end{cfuncdesc}
907
908\begin{cfuncdesc}{int}{Py_UNICODE_ISLOWER}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000909 Return 1 or 0 depending on whether \var{ch} is a lowercase character.
Fred Drake3adf79e2001-10-12 19:01:43 +0000910\end{cfuncdesc}
911
912\begin{cfuncdesc}{int}{Py_UNICODE_ISUPPER}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000913 Return 1 or 0 depending on whether \var{ch} is an uppercase
Fred Drake3adf79e2001-10-12 19:01:43 +0000914 character.
915\end{cfuncdesc}
916
917\begin{cfuncdesc}{int}{Py_UNICODE_ISTITLE}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000918 Return 1 or 0 depending on whether \var{ch} is a titlecase character.
Fred Drake3adf79e2001-10-12 19:01:43 +0000919\end{cfuncdesc}
920
921\begin{cfuncdesc}{int}{Py_UNICODE_ISLINEBREAK}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000922 Return 1 or 0 depending on whether \var{ch} is a linebreak character.
Fred Drake3adf79e2001-10-12 19:01:43 +0000923\end{cfuncdesc}
924
925\begin{cfuncdesc}{int}{Py_UNICODE_ISDECIMAL}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000926 Return 1 or 0 depending on whether \var{ch} is a decimal character.
Fred Drake3adf79e2001-10-12 19:01:43 +0000927\end{cfuncdesc}
928
929\begin{cfuncdesc}{int}{Py_UNICODE_ISDIGIT}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000930 Return 1 or 0 depending on whether \var{ch} is a digit character.
Fred Drake3adf79e2001-10-12 19:01:43 +0000931\end{cfuncdesc}
932
933\begin{cfuncdesc}{int}{Py_UNICODE_ISNUMERIC}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000934 Return 1 or 0 depending on whether \var{ch} is a numeric character.
Fred Drake3adf79e2001-10-12 19:01:43 +0000935\end{cfuncdesc}
936
937\begin{cfuncdesc}{int}{Py_UNICODE_ISALPHA}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000938 Return 1 or 0 depending on whether \var{ch} is an alphabetic
Fred Drake3adf79e2001-10-12 19:01:43 +0000939 character.
940\end{cfuncdesc}
941
942\begin{cfuncdesc}{int}{Py_UNICODE_ISALNUM}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000943 Return 1 or 0 depending on whether \var{ch} is an alphanumeric
Fred Drake3adf79e2001-10-12 19:01:43 +0000944 character.
945\end{cfuncdesc}
946
947These APIs can be used for fast direct character conversions:
948
949\begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOLOWER}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000950 Return the character \var{ch} converted to lower case.
Fred Drake3adf79e2001-10-12 19:01:43 +0000951\end{cfuncdesc}
952
953\begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOUPPER}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000954 Return the character \var{ch} converted to upper case.
Fred Drake3adf79e2001-10-12 19:01:43 +0000955\end{cfuncdesc}
956
957\begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOTITLE}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000958 Return the character \var{ch} converted to title case.
Fred Drake3adf79e2001-10-12 19:01:43 +0000959\end{cfuncdesc}
960
961\begin{cfuncdesc}{int}{Py_UNICODE_TODECIMAL}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000962 Return the character \var{ch} converted to a decimal positive
963 integer. Return \code{-1} if this is not possible. This macro
964 does not raise exceptions.
Fred Drake3adf79e2001-10-12 19:01:43 +0000965\end{cfuncdesc}
966
967\begin{cfuncdesc}{int}{Py_UNICODE_TODIGIT}{Py_UNICODE ch}
Georg Brandl99363b62005-09-03 07:27:26 +0000968 Return the character \var{ch} converted to a single digit integer.
969 Return \code{-1} if this is not possible. This macro does not raise
Fred Drake3adf79e2001-10-12 19:01:43 +0000970 exceptions.
971\end{cfuncdesc}
972
973\begin{cfuncdesc}{double}{Py_UNICODE_TONUMERIC}{Py_UNICODE ch}
Martin v. Löwisd004fc82006-05-27 08:36:52 +0000974 Return the character \var{ch} converted to a double.
Georg Brandl99363b62005-09-03 07:27:26 +0000975 Return \code{-1.0} if this is not possible. This macro does not raise
Fred Drake3adf79e2001-10-12 19:01:43 +0000976 exceptions.
977\end{cfuncdesc}
978
979% --- Plain Py_UNICODE ---------------------------------------------------
980
981To create Unicode objects and access their basic sequence properties,
982use these APIs:
983
984\begin{cfuncdesc}{PyObject*}{PyUnicode_FromUnicode}{const Py_UNICODE *u,
Martin v. Löwis29fafd82006-03-01 05:16:03 +0000985 Py_ssize_t size}
Fred Drake3adf79e2001-10-12 19:01:43 +0000986 Create a Unicode Object from the Py_UNICODE buffer \var{u} of the
987 given size. \var{u} may be \NULL{} which causes the contents to be
988 undefined. It is the user's responsibility to fill in the needed
989 data. The buffer is copied into the new object. If the buffer is
Tim Peters9ddf40b2004-06-20 22:41:32 +0000990 not \NULL{}, the return value might be a shared object. Therefore,
Fred Drake3adf79e2001-10-12 19:01:43 +0000991 modification of the resulting Unicode object is only allowed when
Tim Peters9ddf40b2004-06-20 22:41:32 +0000992 \var{u} is \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000993\end{cfuncdesc}
994
995\begin{cfuncdesc}{Py_UNICODE*}{PyUnicode_AsUnicode}{PyObject *unicode}
996 Return a read-only pointer to the Unicode object's internal
997 \ctype{Py_UNICODE} buffer, \NULL{} if \var{unicode} is not a Unicode
998 object.
999\end{cfuncdesc}
1000
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001001\begin{cfuncdesc}{Py_ssize_t}{PyUnicode_GetSize}{PyObject *unicode}
Fred Drake3adf79e2001-10-12 19:01:43 +00001002 Return the length of the Unicode object.
1003\end{cfuncdesc}
1004
1005\begin{cfuncdesc}{PyObject*}{PyUnicode_FromEncodedObject}{PyObject *obj,
1006 const char *encoding,
1007 const char *errors}
1008 Coerce an encoded object \var{obj} to an Unicode object and return a
1009 reference with incremented refcount.
Georg Brandl69f61682006-06-14 16:46:43 +00001010
1011 String and other char buffer compatible objects are decoded
1012 according to the given encoding and using the error handling
1013 defined by errors. Both can be \NULL{} to have the interface
1014 use the default values (see the next section for details).
Fred Drake3adf79e2001-10-12 19:01:43 +00001015
Georg Brandl69f61682006-06-14 16:46:43 +00001016 All other objects, including Unicode objects, cause a
1017 \exception{TypeError} to be set.
Fred Drake3adf79e2001-10-12 19:01:43 +00001018
1019 The API returns \NULL{} if there was an error. The caller is
1020 responsible for decref'ing the returned objects.
1021\end{cfuncdesc}
1022
1023\begin{cfuncdesc}{PyObject*}{PyUnicode_FromObject}{PyObject *obj}
1024 Shortcut for \code{PyUnicode_FromEncodedObject(obj, NULL, "strict")}
1025 which is used throughout the interpreter whenever coercion to
1026 Unicode is needed.
1027\end{cfuncdesc}
1028
1029% --- wchar_t support for platforms which support it ---------------------
1030
1031If the platform supports \ctype{wchar_t} and provides a header file
1032wchar.h, Python can interface directly to this type using the
1033following functions. Support is optimized if Python's own
1034\ctype{Py_UNICODE} type is identical to the system's \ctype{wchar_t}.
1035
1036\begin{cfuncdesc}{PyObject*}{PyUnicode_FromWideChar}{const wchar_t *w,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001037 Py_ssize_t size}
Thomas Heller541703b2002-04-29 17:28:43 +00001038 Create a Unicode object from the \ctype{wchar_t} buffer \var{w} of
Georg Brandl99363b62005-09-03 07:27:26 +00001039 the given size. Return \NULL{} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00001040\end{cfuncdesc}
1041
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001042\begin{cfuncdesc}{Py_ssize_t}{PyUnicode_AsWideChar}{PyUnicodeObject *unicode,
Fred Drake3adf79e2001-10-12 19:01:43 +00001043 wchar_t *w,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001044 Py_ssize_t size}
Georg Brandl99363b62005-09-03 07:27:26 +00001045 Copy the Unicode object contents into the \ctype{wchar_t} buffer
Marc-André Lemburga9cadcd2004-11-22 13:02:31 +00001046 \var{w}. At most \var{size} \ctype{wchar_t} characters are copied
Georg Brandl99363b62005-09-03 07:27:26 +00001047 (excluding a possibly trailing 0-termination character). Return
Marc-André Lemburga9cadcd2004-11-22 13:02:31 +00001048 the number of \ctype{wchar_t} characters copied or -1 in case of an
1049 error. Note that the resulting \ctype{wchar_t} string may or may
1050 not be 0-terminated. It is the responsibility of the caller to make
1051 sure that the \ctype{wchar_t} string is 0-terminated in case this is
1052 required by the application.
Fred Drake3adf79e2001-10-12 19:01:43 +00001053\end{cfuncdesc}
1054
1055
1056\subsubsection{Built-in Codecs \label{builtinCodecs}}
1057
1058Python provides a set of builtin codecs which are written in C
1059for speed. All of these codecs are directly usable via the
1060following functions.
1061
1062Many of the following APIs take two arguments encoding and
1063errors. These parameters encoding and errors have the same semantics
1064as the ones of the builtin unicode() Unicode object constructor.
1065
1066Setting encoding to \NULL{} causes the default encoding to be used
1067which is \ASCII. The file system calls should use
1068\cdata{Py_FileSystemDefaultEncoding} as the encoding for file
1069names. This variable should be treated as read-only: On some systems,
1070it will be a pointer to a static string, on others, it will change at
Raymond Hettingercb2da432003-10-12 18:24:34 +00001071run-time (such as when the application invokes setlocale).
Fred Drake3adf79e2001-10-12 19:01:43 +00001072
1073Error handling is set by errors which may also be set to \NULL{}
1074meaning to use the default handling defined for the codec. Default
1075error handling for all builtin codecs is ``strict''
1076(\exception{ValueError} is raised).
1077
1078The codecs all use a similar interface. Only deviation from the
1079following generic ones are documented for simplicity.
1080
1081% --- Generic Codecs -----------------------------------------------------
1082
1083These are the generic codec APIs:
1084
1085\begin{cfuncdesc}{PyObject*}{PyUnicode_Decode}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001086 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001087 const char *encoding,
1088 const char *errors}
1089 Create a Unicode object by decoding \var{size} bytes of the encoded
1090 string \var{s}. \var{encoding} and \var{errors} have the same
1091 meaning as the parameters of the same name in the
1092 \function{unicode()} builtin function. The codec to be used is
Georg Brandl99363b62005-09-03 07:27:26 +00001093 looked up using the Python codec registry. Return \NULL{} if an
Fred Drake3adf79e2001-10-12 19:01:43 +00001094 exception was raised by the codec.
1095\end{cfuncdesc}
1096
1097\begin{cfuncdesc}{PyObject*}{PyUnicode_Encode}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001098 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001099 const char *encoding,
1100 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001101 Encode the \ctype{Py_UNICODE} buffer of the given size and return
Fred Drake3adf79e2001-10-12 19:01:43 +00001102 a Python string object. \var{encoding} and \var{errors} have the
1103 same meaning as the parameters of the same name in the Unicode
1104 \method{encode()} method. The codec to be used is looked up using
Georg Brandl99363b62005-09-03 07:27:26 +00001105 the Python codec registry. Return \NULL{} if an exception was
Fred Drake3adf79e2001-10-12 19:01:43 +00001106 raised by the codec.
1107\end{cfuncdesc}
1108
1109\begin{cfuncdesc}{PyObject*}{PyUnicode_AsEncodedString}{PyObject *unicode,
1110 const char *encoding,
1111 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001112 Encode a Unicode object and return the result as Python string
Fred Drake3adf79e2001-10-12 19:01:43 +00001113 object. \var{encoding} and \var{errors} have the same meaning as the
1114 parameters of the same name in the Unicode \method{encode()} method.
1115 The codec to be used is looked up using the Python codec registry.
Georg Brandl99363b62005-09-03 07:27:26 +00001116 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +00001117\end{cfuncdesc}
1118
1119% --- UTF-8 Codecs -------------------------------------------------------
1120
1121These are the UTF-8 codec APIs:
1122
1123\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF8}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001124 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001125 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001126 Create a Unicode object by decoding \var{size} bytes of the UTF-8
1127 encoded string \var{s}. Return \NULL{} if an exception was raised
Fred Drake3adf79e2001-10-12 19:01:43 +00001128 by the codec.
1129\end{cfuncdesc}
1130
Walter Dörwald69652032004-09-07 20:24:22 +00001131\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF8Stateful}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001132 Py_ssize_t size,
Walter Dörwald69652032004-09-07 20:24:22 +00001133 const char *errors,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001134 Py_ssize_t *consumed}
Georg Brandl99363b62005-09-03 07:27:26 +00001135 If \var{consumed} is \NULL{}, behave like \cfunction{PyUnicode_DecodeUTF8()}.
Walter Dörwald69652032004-09-07 20:24:22 +00001136 If \var{consumed} is not \NULL{}, trailing incomplete UTF-8 byte sequences
1137 will not be treated as an error. Those bytes will not be decoded and the
1138 number of bytes that have been decoded will be stored in \var{consumed}.
1139 \versionadded{2.4}
1140\end{cfuncdesc}
1141
Fred Drake3adf79e2001-10-12 19:01:43 +00001142\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUTF8}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001143 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001144 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001145 Encode the \ctype{Py_UNICODE} buffer of the given size using UTF-8
1146 and return a Python string object. Return \NULL{} if an exception
Fred Drake3adf79e2001-10-12 19:01:43 +00001147 was raised by the codec.
1148\end{cfuncdesc}
1149
1150\begin{cfuncdesc}{PyObject*}{PyUnicode_AsUTF8String}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001151 Encode a Unicode objects using UTF-8 and return the result as
1152 Python string object. Error handling is ``strict''. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00001153 \NULL{} if an exception was raised by the codec.
1154\end{cfuncdesc}
1155
1156% --- UTF-16 Codecs ------------------------------------------------------ */
1157
1158These are the UTF-16 codec APIs:
1159
1160\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF16}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001161 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001162 const char *errors,
1163 int *byteorder}
Georg Brandl99363b62005-09-03 07:27:26 +00001164 Decode \var{length} bytes from a UTF-16 encoded buffer string and
1165 return the corresponding Unicode object. \var{errors} (if
Tim Peters9ddf40b2004-06-20 22:41:32 +00001166 non-\NULL{}) defines the error handling. It defaults to ``strict''.
Fred Drake3adf79e2001-10-12 19:01:43 +00001167
Tim Peters9ddf40b2004-06-20 22:41:32 +00001168 If \var{byteorder} is non-\NULL{}, the decoder starts decoding using
Fred Drake3adf79e2001-10-12 19:01:43 +00001169 the given byte order:
1170
1171\begin{verbatim}
1172 *byteorder == -1: little endian
1173 *byteorder == 0: native order
1174 *byteorder == 1: big endian
1175\end{verbatim}
1176
Walter Dörwaldf8f68fb2007-05-03 15:16:16 +00001177 and then switches if the first two bytes of the input data are a byte order
1178 mark (BOM) and the specified byte order is native order. This BOM is not
1179 copied into the resulting Unicode string. After completion, \var{*byteorder}
1180 is set to the current byte order at the.
Fred Drake3adf79e2001-10-12 19:01:43 +00001181
Tim Peters9ddf40b2004-06-20 22:41:32 +00001182 If \var{byteorder} is \NULL{}, the codec starts in native order mode.
Fred Drake3adf79e2001-10-12 19:01:43 +00001183
Georg Brandl99363b62005-09-03 07:27:26 +00001184 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +00001185\end{cfuncdesc}
1186
Walter Dörwald69652032004-09-07 20:24:22 +00001187\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF16Stateful}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001188 Py_ssize_t size,
Walter Dörwald69652032004-09-07 20:24:22 +00001189 const char *errors,
1190 int *byteorder,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001191 Py_ssize_t *consumed}
Georg Brandl99363b62005-09-03 07:27:26 +00001192 If \var{consumed} is \NULL{}, behave like
Walter Dörwald69652032004-09-07 20:24:22 +00001193 \cfunction{PyUnicode_DecodeUTF16()}. If \var{consumed} is not \NULL{},
1194 \cfunction{PyUnicode_DecodeUTF16Stateful()} will not treat trailing incomplete
Raymond Hettinger0c230b92005-08-17 10:05:22 +00001195 UTF-16 byte sequences (such as an odd number of bytes or a split surrogate pair)
Walter Dörwald69652032004-09-07 20:24:22 +00001196 as an error. Those bytes will not be decoded and the number of bytes that
1197 have been decoded will be stored in \var{consumed}.
1198 \versionadded{2.4}
1199\end{cfuncdesc}
1200
Fred Drake3adf79e2001-10-12 19:01:43 +00001201\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUTF16}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001202 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001203 const char *errors,
1204 int byteorder}
Georg Brandl99363b62005-09-03 07:27:26 +00001205 Return a Python string object holding the UTF-16 encoded value of
Fred Drake3adf79e2001-10-12 19:01:43 +00001206 the Unicode data in \var{s}. If \var{byteorder} is not \code{0},
1207 output is written according to the following byte order:
1208
1209\begin{verbatim}
1210 byteorder == -1: little endian
1211 byteorder == 0: native byte order (writes a BOM mark)
1212 byteorder == 1: big endian
1213\end{verbatim}
1214
1215 If byteorder is \code{0}, the output string will always start with
1216 the Unicode BOM mark (U+FEFF). In the other two modes, no BOM mark
1217 is prepended.
1218
Martin v. Löwis9bc4f2d2004-06-03 09:55:28 +00001219 If \var{Py_UNICODE_WIDE} is defined, a single \ctype{Py_UNICODE}
1220 value may get represented as a surrogate pair. If it is not
1221 defined, each \ctype{Py_UNICODE} values is interpreted as an
1222 UCS-2 character.
Fred Drake3adf79e2001-10-12 19:01:43 +00001223
Georg Brandl99363b62005-09-03 07:27:26 +00001224 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +00001225\end{cfuncdesc}
1226
1227\begin{cfuncdesc}{PyObject*}{PyUnicode_AsUTF16String}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001228 Return a Python string using the UTF-16 encoding in native byte
Fred Drake3adf79e2001-10-12 19:01:43 +00001229 order. The string always starts with a BOM mark. Error handling is
Georg Brandl99363b62005-09-03 07:27:26 +00001230 ``strict''. Return \NULL{} if an exception was raised by the
Fred Drake3adf79e2001-10-12 19:01:43 +00001231 codec.
1232\end{cfuncdesc}
1233
1234% --- Unicode-Escape Codecs ----------------------------------------------
1235
Martin v. Löwis95cf84a2003-10-19 07:32:24 +00001236These are the ``Unicode Escape'' codec APIs:
Fred Drake3adf79e2001-10-12 19:01:43 +00001237
1238\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUnicodeEscape}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001239 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001240 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001241 Create a Unicode object by decoding \var{size} bytes of the
1242 Unicode-Escape encoded string \var{s}. Return \NULL{} if an
Fred Drake3adf79e2001-10-12 19:01:43 +00001243 exception was raised by the codec.
1244\end{cfuncdesc}
1245
1246\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUnicodeEscape}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001247 Py_ssize_t size}
Georg Brandl99363b62005-09-03 07:27:26 +00001248 Encode the \ctype{Py_UNICODE} buffer of the given size using
1249 Unicode-Escape and return a Python string object. Return \NULL{}
Fred Drake3adf79e2001-10-12 19:01:43 +00001250 if an exception was raised by the codec.
1251\end{cfuncdesc}
1252
1253\begin{cfuncdesc}{PyObject*}{PyUnicode_AsUnicodeEscapeString}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001254 Encode a Unicode objects using Unicode-Escape and return the
Fred Drake3adf79e2001-10-12 19:01:43 +00001255 result as Python string object. Error handling is ``strict''.
Georg Brandl99363b62005-09-03 07:27:26 +00001256 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +00001257\end{cfuncdesc}
1258
1259% --- Raw-Unicode-Escape Codecs ------------------------------------------
1260
Martin v. Löwis95cf84a2003-10-19 07:32:24 +00001261These are the ``Raw Unicode Escape'' codec APIs:
Fred Drake3adf79e2001-10-12 19:01:43 +00001262
1263\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeRawUnicodeEscape}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001264 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001265 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001266 Create a Unicode object by decoding \var{size} bytes of the
1267 Raw-Unicode-Escape encoded string \var{s}. Return \NULL{} if an
Fred Drake3adf79e2001-10-12 19:01:43 +00001268 exception was raised by the codec.
1269\end{cfuncdesc}
1270
1271\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeRawUnicodeEscape}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001272 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001273 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001274 Encode the \ctype{Py_UNICODE} buffer of the given size using
1275 Raw-Unicode-Escape and return a Python string object. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00001276 \NULL{} if an exception was raised by the codec.
1277\end{cfuncdesc}
1278
1279\begin{cfuncdesc}{PyObject*}{PyUnicode_AsRawUnicodeEscapeString}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001280 Encode a Unicode objects using Raw-Unicode-Escape and return the
Fred Drake3adf79e2001-10-12 19:01:43 +00001281 result as Python string object. Error handling is ``strict''.
Georg Brandl99363b62005-09-03 07:27:26 +00001282 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +00001283\end{cfuncdesc}
1284
Tim Petersf582b822001-12-11 18:51:08 +00001285% --- Latin-1 Codecs -----------------------------------------------------
Fred Drake3adf79e2001-10-12 19:01:43 +00001286
1287These are the Latin-1 codec APIs:
1288Latin-1 corresponds to the first 256 Unicode ordinals and only these
1289are accepted by the codecs during encoding.
1290
1291\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeLatin1}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001292 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001293 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001294 Create a Unicode object by decoding \var{size} bytes of the Latin-1
1295 encoded string \var{s}. Return \NULL{} if an exception was raised
Fred Drake3adf79e2001-10-12 19:01:43 +00001296 by the codec.
1297\end{cfuncdesc}
1298
1299\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeLatin1}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001300 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001301 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001302 Encode the \ctype{Py_UNICODE} buffer of the given size using
1303 Latin-1 and return a Python string object. Return \NULL{} if an
Fred Drake3adf79e2001-10-12 19:01:43 +00001304 exception was raised by the codec.
1305\end{cfuncdesc}
1306
1307\begin{cfuncdesc}{PyObject*}{PyUnicode_AsLatin1String}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001308 Encode a Unicode objects using Latin-1 and return the result as
1309 Python string object. Error handling is ``strict''. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00001310 \NULL{} if an exception was raised by the codec.
1311\end{cfuncdesc}
1312
Tim Petersf582b822001-12-11 18:51:08 +00001313% --- ASCII Codecs -------------------------------------------------------
Fred Drake3adf79e2001-10-12 19:01:43 +00001314
1315These are the \ASCII{} codec APIs. Only 7-bit \ASCII{} data is
1316accepted. All other codes generate errors.
1317
1318\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeASCII}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001319 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001320 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001321 Create a Unicode object by decoding \var{size} bytes of the
1322 \ASCII{} encoded string \var{s}. Return \NULL{} if an exception
Fred Drake3adf79e2001-10-12 19:01:43 +00001323 was raised by the codec.
1324\end{cfuncdesc}
1325
1326\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeASCII}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001327 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001328 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001329 Encode the \ctype{Py_UNICODE} buffer of the given size using
1330 \ASCII{} and return a Python string object. Return \NULL{} if an
Fred Drake3adf79e2001-10-12 19:01:43 +00001331 exception was raised by the codec.
1332\end{cfuncdesc}
1333
1334\begin{cfuncdesc}{PyObject*}{PyUnicode_AsASCIIString}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001335 Encode a Unicode objects using \ASCII{} and return the result as
1336 Python string object. Error handling is ``strict''. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00001337 \NULL{} if an exception was raised by the codec.
1338\end{cfuncdesc}
1339
Tim Petersf582b822001-12-11 18:51:08 +00001340% --- Character Map Codecs -----------------------------------------------
Fred Drake3adf79e2001-10-12 19:01:43 +00001341
1342These are the mapping codec APIs:
1343
1344This codec is special in that it can be used to implement many
1345different codecs (and this is in fact what was done to obtain most of
1346the standard codecs included in the \module{encodings} package). The
1347codec uses mapping to encode and decode characters.
1348
1349Decoding mappings must map single string characters to single Unicode
1350characters, integers (which are then interpreted as Unicode ordinals)
Tim Petersf582b822001-12-11 18:51:08 +00001351or None (meaning "undefined mapping" and causing an error).
Fred Drake3adf79e2001-10-12 19:01:43 +00001352
1353Encoding mappings must map single Unicode characters to single string
1354characters, integers (which are then interpreted as Latin-1 ordinals)
1355or None (meaning "undefined mapping" and causing an error).
1356
1357The mapping objects provided must only support the __getitem__ mapping
1358interface.
1359
1360If a character lookup fails with a LookupError, the character is
1361copied as-is meaning that its ordinal value will be interpreted as
1362Unicode or Latin-1 ordinal resp. Because of this, mappings only need
1363to contain those mappings which map characters to different code
1364points.
1365
1366\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeCharmap}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001367 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001368 PyObject *mapping,
1369 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001370 Create a Unicode object by decoding \var{size} bytes of the encoded
1371 string \var{s} using the given \var{mapping} object. Return
Walter Dörwaldd1c1e102005-10-06 20:29:57 +00001372 \NULL{} if an exception was raised by the codec. If \var{mapping} is \NULL{}
1373 latin-1 decoding will be done. Else it can be a dictionary mapping byte or a
1374 unicode string, which is treated as a lookup table. Byte values greater
1375 that the length of the string and U+FFFE "characters" are treated as
1376 "undefined mapping".
1377 \versionchanged[Allowed unicode string as mapping argument]{2.4}
Fred Drake3adf79e2001-10-12 19:01:43 +00001378\end{cfuncdesc}
1379
1380\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeCharmap}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001381 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001382 PyObject *mapping,
1383 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001384 Encode the \ctype{Py_UNICODE} buffer of the given size using the
1385 given \var{mapping} object and return a Python string object.
1386 Return \NULL{} if an exception was raised by the codec.
Fred Drake3adf79e2001-10-12 19:01:43 +00001387\end{cfuncdesc}
1388
1389\begin{cfuncdesc}{PyObject*}{PyUnicode_AsCharmapString}{PyObject *unicode,
1390 PyObject *mapping}
Georg Brandl99363b62005-09-03 07:27:26 +00001391 Encode a Unicode objects using the given \var{mapping} object and
1392 return the result as Python string object. Error handling is
1393 ``strict''. Return \NULL{} if an exception was raised by the
Fred Drake3adf79e2001-10-12 19:01:43 +00001394 codec.
1395\end{cfuncdesc}
1396
1397The following codec API is special in that maps Unicode to Unicode.
1398
1399\begin{cfuncdesc}{PyObject*}{PyUnicode_TranslateCharmap}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001400 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001401 PyObject *table,
1402 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001403 Translate a \ctype{Py_UNICODE} buffer of the given length by
1404 applying a character mapping \var{table} to it and return the
1405 resulting Unicode object. Return \NULL{} when an exception was
Fred Drake3adf79e2001-10-12 19:01:43 +00001406 raised by the codec.
1407
1408 The \var{mapping} table must map Unicode ordinal integers to Unicode
1409 ordinal integers or None (causing deletion of the character).
1410
George Yoshida9dea97a2006-04-28 16:09:45 +00001411 Mapping tables need only provide the \method{__getitem__()}
Fred Drake3adf79e2001-10-12 19:01:43 +00001412 interface; dictionaries and sequences work well. Unmapped character
1413 ordinals (ones which cause a \exception{LookupError}) are left
1414 untouched and are copied as-is.
1415\end{cfuncdesc}
1416
1417% --- MBCS codecs for Windows --------------------------------------------
1418
1419These are the MBCS codec APIs. They are currently only available on
1420Windows and use the Win32 MBCS converters to implement the
1421conversions. Note that MBCS (or DBCS) is a class of encodings, not
1422just one. The target encoding is defined by the user settings on the
1423machine running the codec.
1424
1425\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeMBCS}{const char *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001426 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001427 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001428 Create a Unicode object by decoding \var{size} bytes of the MBCS
1429 encoded string \var{s}. Return \NULL{} if an exception was
Fred Drake3adf79e2001-10-12 19:01:43 +00001430 raised by the codec.
1431\end{cfuncdesc}
1432
Martin v. Löwisd8251432006-06-14 05:21:04 +00001433\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeMBCSStateful}{const char *s,
1434 int size,
1435 const char *errors,
1436 int *consumed}
1437 If \var{consumed} is \NULL{}, behave like
1438 \cfunction{PyUnicode_DecodeMBCS()}. If \var{consumed} is not \NULL{},
1439 \cfunction{PyUnicode_DecodeMBCSStateful()} will not decode trailing lead
1440 byte and the number of bytes that have been decoded will be stored in
1441 \var{consumed}.
1442 \versionadded{2.5}
1443\end{cfuncdesc}
1444
Fred Drake3adf79e2001-10-12 19:01:43 +00001445\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeMBCS}{const Py_UNICODE *s,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001446 Py_ssize_t size,
Fred Drake3adf79e2001-10-12 19:01:43 +00001447 const char *errors}
Georg Brandl99363b62005-09-03 07:27:26 +00001448 Encode the \ctype{Py_UNICODE} buffer of the given size using MBCS
1449 and return a Python string object. Return \NULL{} if an exception
Fred Drake3adf79e2001-10-12 19:01:43 +00001450 was raised by the codec.
1451\end{cfuncdesc}
1452
1453\begin{cfuncdesc}{PyObject*}{PyUnicode_AsMBCSString}{PyObject *unicode}
Georg Brandl99363b62005-09-03 07:27:26 +00001454 Encode a Unicode objects using MBCS and return the result as
1455 Python string object. Error handling is ``strict''. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00001456 \NULL{} if an exception was raised by the codec.
1457\end{cfuncdesc}
1458
1459% --- Methods & Slots ----------------------------------------------------
1460
1461\subsubsection{Methods and Slot Functions \label{unicodeMethodsAndSlots}}
1462
1463The following APIs are capable of handling Unicode objects and strings
1464on input (we refer to them as strings in the descriptions) and return
Martin v. Löwis95cf84a2003-10-19 07:32:24 +00001465Unicode objects or integers as appropriate.
Fred Drake3adf79e2001-10-12 19:01:43 +00001466
1467They all return \NULL{} or \code{-1} if an exception occurs.
1468
1469\begin{cfuncdesc}{PyObject*}{PyUnicode_Concat}{PyObject *left,
1470 PyObject *right}
1471 Concat two strings giving a new Unicode string.
1472\end{cfuncdesc}
1473
1474\begin{cfuncdesc}{PyObject*}{PyUnicode_Split}{PyObject *s,
1475 PyObject *sep,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001476 Py_ssize_t maxsplit}
Tim Peters9ddf40b2004-06-20 22:41:32 +00001477 Split a string giving a list of Unicode strings. If sep is \NULL{},
Fred Drake3adf79e2001-10-12 19:01:43 +00001478 splitting will be done at all whitespace substrings. Otherwise,
1479 splits occur at the given separator. At most \var{maxsplit} splits
1480 will be done. If negative, no limit is set. Separators are not
1481 included in the resulting list.
1482\end{cfuncdesc}
1483
1484\begin{cfuncdesc}{PyObject*}{PyUnicode_Splitlines}{PyObject *s,
Martin v. Löwis24b88812003-03-30 16:40:42 +00001485 int keepend}
Fred Drake3adf79e2001-10-12 19:01:43 +00001486 Split a Unicode string at line breaks, returning a list of Unicode
Martin v. Löwis24b88812003-03-30 16:40:42 +00001487 strings. CRLF is considered to be one line break. If \var{keepend}
1488 is 0, the Line break characters are not included in the resulting
1489 strings.
Fred Drake3adf79e2001-10-12 19:01:43 +00001490\end{cfuncdesc}
1491
1492\begin{cfuncdesc}{PyObject*}{PyUnicode_Translate}{PyObject *str,
1493 PyObject *table,
1494 const char *errors}
1495 Translate a string by applying a character mapping table to it and
1496 return the resulting Unicode object.
1497
1498 The mapping table must map Unicode ordinal integers to Unicode
1499 ordinal integers or None (causing deletion of the character).
1500
1501 Mapping tables need only provide the \method{__getitem__()}
1502 interface; dictionaries and sequences work well. Unmapped character
1503 ordinals (ones which cause a \exception{LookupError}) are left
1504 untouched and are copied as-is.
1505
1506 \var{errors} has the usual meaning for codecs. It may be \NULL{}
1507 which indicates to use the default error handling.
1508\end{cfuncdesc}
1509
1510\begin{cfuncdesc}{PyObject*}{PyUnicode_Join}{PyObject *separator,
1511 PyObject *seq}
1512 Join a sequence of strings using the given separator and return the
1513 resulting Unicode string.
1514\end{cfuncdesc}
1515
Raymond Hettinger8ef9b3e2004-12-10 17:12:32 +00001516\begin{cfuncdesc}{int}{PyUnicode_Tailmatch}{PyObject *str,
Fred Drake3adf79e2001-10-12 19:01:43 +00001517 PyObject *substr,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001518 Py_ssize_t start,
1519 Py_ssize_t end,
Fred Drake3adf79e2001-10-12 19:01:43 +00001520 int direction}
1521 Return 1 if \var{substr} matches \var{str}[\var{start}:\var{end}] at
1522 the given tail end (\var{direction} == -1 means to do a prefix
1523 match, \var{direction} == 1 a suffix match), 0 otherwise.
Tim Peters8931ff12006-05-13 23:28:20 +00001524 Return \code{-1} if an error occurred.
Fred Drake3adf79e2001-10-12 19:01:43 +00001525\end{cfuncdesc}
1526
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001527\begin{cfuncdesc}{Py_ssize_t}{PyUnicode_Find}{PyObject *str,
Fred Drake1d1e1db2002-06-20 22:07:04 +00001528 PyObject *substr,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001529 Py_ssize_t start,
1530 Py_ssize_t end,
Fred Drake1d1e1db2002-06-20 22:07:04 +00001531 int direction}
Fred Drake3adf79e2001-10-12 19:01:43 +00001532 Return the first position of \var{substr} in
1533 \var{str}[\var{start}:\var{end}] using the given \var{direction}
1534 (\var{direction} == 1 means to do a forward search,
Fred Drake1d1e1db2002-06-20 22:07:04 +00001535 \var{direction} == -1 a backward search). The return value is the
1536 index of the first match; a value of \code{-1} indicates that no
1537 match was found, and \code{-2} indicates that an error occurred and
1538 an exception has been set.
Fred Drake3adf79e2001-10-12 19:01:43 +00001539\end{cfuncdesc}
1540
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001541\begin{cfuncdesc}{Py_ssize_t}{PyUnicode_Count}{PyObject *str,
Fred Drake1d1e1db2002-06-20 22:07:04 +00001542 PyObject *substr,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001543 Py_ssize_t start,
1544 Py_ssize_t end}
Fred Drake1d1e1db2002-06-20 22:07:04 +00001545 Return the number of non-overlapping occurrences of \var{substr} in
Georg Brandl99363b62005-09-03 07:27:26 +00001546 \code{\var{str}[\var{start}:\var{end}]}. Return \code{-1} if an
Fred Drake1d1e1db2002-06-20 22:07:04 +00001547 error occurred.
Fred Drake3adf79e2001-10-12 19:01:43 +00001548\end{cfuncdesc}
1549
1550\begin{cfuncdesc}{PyObject*}{PyUnicode_Replace}{PyObject *str,
1551 PyObject *substr,
1552 PyObject *replstr,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001553 Py_ssize_t maxcount}
Fred Drake3adf79e2001-10-12 19:01:43 +00001554 Replace at most \var{maxcount} occurrences of \var{substr} in
1555 \var{str} with \var{replstr} and return the resulting Unicode object.
1556 \var{maxcount} == -1 means replace all occurrences.
1557\end{cfuncdesc}
1558
1559\begin{cfuncdesc}{int}{PyUnicode_Compare}{PyObject *left, PyObject *right}
1560 Compare two strings and return -1, 0, 1 for less than, equal, and
1561 greater than, respectively.
1562\end{cfuncdesc}
1563
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00001564\begin{cfuncdesc}{int}{PyUnicode_RichCompare}{PyObject *left,
1565 PyObject *right,
1566 int op}
1567
Georg Brandl4873fb22006-08-14 12:36:06 +00001568 Rich compare two unicode strings and return one of the following:
1569 \begin{itemize}
1570 \item \code{NULL} in case an exception was raised
1571 \item \constant{Py_True} or \constant{Py_False} for successful comparisons
1572 \item \constant{Py_NotImplemented} in case the type combination is unknown
1573 \end{itemize}
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00001574
Georg Brandl4873fb22006-08-14 12:36:06 +00001575 Note that \constant{Py_EQ} and \constant{Py_NE} comparisons can cause a
1576 \exception{UnicodeWarning} in case the conversion of the arguments to
1577 Unicode fails with a \exception{UnicodeDecodeError}.
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00001578
Georg Brandl4873fb22006-08-14 12:36:06 +00001579 Possible values for \var{op} are
1580 \constant{Py_GT}, \constant{Py_GE}, \constant{Py_EQ},
1581 \constant{Py_NE}, \constant{Py_LT}, and \constant{Py_LE}.
Marc-André Lemburg040f76b2006-08-14 10:55:19 +00001582\end{cfuncdesc}
1583
Fred Drake3adf79e2001-10-12 19:01:43 +00001584\begin{cfuncdesc}{PyObject*}{PyUnicode_Format}{PyObject *format,
1585 PyObject *args}
Georg Brandl99363b62005-09-03 07:27:26 +00001586 Return a new string object from \var{format} and \var{args}; this
Fred Drake3adf79e2001-10-12 19:01:43 +00001587 is analogous to \code{\var{format} \%\ \var{args}}. The
1588 \var{args} argument must be a tuple.
1589\end{cfuncdesc}
1590
1591\begin{cfuncdesc}{int}{PyUnicode_Contains}{PyObject *container,
1592 PyObject *element}
Georg Brandl99363b62005-09-03 07:27:26 +00001593 Check whether \var{element} is contained in \var{container} and
1594 return true or false accordingly.
Fred Drake3adf79e2001-10-12 19:01:43 +00001595
1596 \var{element} has to coerce to a one element Unicode
1597 string. \code{-1} is returned if there was an error.
1598\end{cfuncdesc}
1599
1600
1601\subsection{Buffer Objects \label{bufferObjects}}
1602\sectionauthor{Greg Stein}{gstein@lyra.org}
1603
1604\obindex{buffer}
1605Python objects implemented in C can export a group of functions called
1606the ``buffer\index{buffer interface} interface.'' These functions can
1607be used by an object to expose its data in a raw, byte-oriented
1608format. Clients of the object can use the buffer interface to access
1609the object data directly, without needing to copy it first.
1610
Tim Petersf582b822001-12-11 18:51:08 +00001611Two examples of objects that support
1612the buffer interface are strings and arrays. The string object exposes
Fred Drake3adf79e2001-10-12 19:01:43 +00001613the character contents in the buffer interface's byte-oriented
1614form. An array can also expose its contents, but it should be noted
1615that array elements may be multi-byte values.
1616
1617An example user of the buffer interface is the file object's
1618\method{write()} method. Any object that can export a series of bytes
1619through the buffer interface can be written to a file. There are a
Tim Petersf582b822001-12-11 18:51:08 +00001620number of format codes to \cfunction{PyArg_ParseTuple()} that operate
Fred Drake3adf79e2001-10-12 19:01:43 +00001621against an object's buffer interface, returning data from the target
1622object.
1623
1624More information on the buffer interface is provided in the section
Fred Drake54e62942001-12-11 19:40:16 +00001625``Buffer Object Structures'' (section~\ref{buffer-structs}), under
Fred Drake3adf79e2001-10-12 19:01:43 +00001626the description for \ctype{PyBufferProcs}\ttindex{PyBufferProcs}.
1627
1628A ``buffer object'' is defined in the \file{bufferobject.h} header
1629(included by \file{Python.h}). These objects look very similar to
1630string objects at the Python programming level: they support slicing,
1631indexing, concatenation, and some other standard string
1632operations. However, their data can come from one of two sources: from
1633a block of memory, or from another object which exports the buffer
1634interface.
1635
1636Buffer objects are useful as a way to expose the data from another
1637object's buffer interface to the Python programmer. They can also be
1638used as a zero-copy slicing mechanism. Using their ability to
1639reference a block of memory, it is possible to expose any data to the
1640Python programmer quite easily. The memory could be a large, constant
1641array in a C extension, it could be a raw block of memory for
1642manipulation before passing to an operating system library, or it
1643could be used to pass around structured data in its native, in-memory
1644format.
1645
1646\begin{ctypedesc}{PyBufferObject}
1647 This subtype of \ctype{PyObject} represents a buffer object.
1648\end{ctypedesc}
1649
1650\begin{cvardesc}{PyTypeObject}{PyBuffer_Type}
1651 The instance of \ctype{PyTypeObject} which represents the Python
Georg Brandl7572f032006-08-08 20:48:10 +00001652 buffer type; it is the same object as \code{buffer} and
1653 \code{types.BufferType} in the Python layer.
1654 \withsubitem{(in module types)}{\ttindex{BufferType}}.
Fred Drake3adf79e2001-10-12 19:01:43 +00001655\end{cvardesc}
1656
1657\begin{cvardesc}{int}{Py_END_OF_BUFFER}
1658 This constant may be passed as the \var{size} parameter to
1659 \cfunction{PyBuffer_FromObject()} or
1660 \cfunction{PyBuffer_FromReadWriteObject()}. It indicates that the
1661 new \ctype{PyBufferObject} should refer to \var{base} object from
1662 the specified \var{offset} to the end of its exported buffer. Using
1663 this enables the caller to avoid querying the \var{base} object for
1664 its length.
1665\end{cvardesc}
1666
1667\begin{cfuncdesc}{int}{PyBuffer_Check}{PyObject *p}
1668 Return true if the argument has type \cdata{PyBuffer_Type}.
1669\end{cfuncdesc}
1670
1671\begin{cfuncdesc}{PyObject*}{PyBuffer_FromObject}{PyObject *base,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001672 Py_ssize_t offset, Py_ssize_t size}
Fred Drake3adf79e2001-10-12 19:01:43 +00001673 Return a new read-only buffer object. This raises
1674 \exception{TypeError} if \var{base} doesn't support the read-only
1675 buffer protocol or doesn't provide exactly one buffer segment, or it
1676 raises \exception{ValueError} if \var{offset} is less than zero. The
1677 buffer will hold a reference to the \var{base} object, and the
1678 buffer's contents will refer to the \var{base} object's buffer
1679 interface, starting as position \var{offset} and extending for
1680 \var{size} bytes. If \var{size} is \constant{Py_END_OF_BUFFER}, then
1681 the new buffer's contents extend to the length of the \var{base}
1682 object's exported buffer data.
1683\end{cfuncdesc}
1684
1685\begin{cfuncdesc}{PyObject*}{PyBuffer_FromReadWriteObject}{PyObject *base,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001686 Py_ssize_t offset,
1687 Py_ssize_t size}
Fred Drake3adf79e2001-10-12 19:01:43 +00001688 Return a new writable buffer object. Parameters and exceptions are
1689 similar to those for \cfunction{PyBuffer_FromObject()}. If the
1690 \var{base} object does not export the writeable buffer protocol,
1691 then \exception{TypeError} is raised.
1692\end{cfuncdesc}
1693
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001694\begin{cfuncdesc}{PyObject*}{PyBuffer_FromMemory}{void *ptr, Py_ssize_t size}
Fred Drake3adf79e2001-10-12 19:01:43 +00001695 Return a new read-only buffer object that reads from a specified
1696 location in memory, with a specified size. The caller is
1697 responsible for ensuring that the memory buffer, passed in as
1698 \var{ptr}, is not deallocated while the returned buffer object
1699 exists. Raises \exception{ValueError} if \var{size} is less than
1700 zero. Note that \constant{Py_END_OF_BUFFER} may \emph{not} be
1701 passed for the \var{size} parameter; \exception{ValueError} will be
1702 raised in that case.
1703\end{cfuncdesc}
1704
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001705\begin{cfuncdesc}{PyObject*}{PyBuffer_FromReadWriteMemory}{void *ptr, Py_ssize_t size}
Fred Drake3adf79e2001-10-12 19:01:43 +00001706 Similar to \cfunction{PyBuffer_FromMemory()}, but the returned
1707 buffer is writable.
1708\end{cfuncdesc}
1709
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001710\begin{cfuncdesc}{PyObject*}{PyBuffer_New}{Py_ssize_t size}
Georg Brandl99363b62005-09-03 07:27:26 +00001711 Return a new writable buffer object that maintains its own memory
Fred Drake3adf79e2001-10-12 19:01:43 +00001712 buffer of \var{size} bytes. \exception{ValueError} is returned if
Neil Schemenauerd68d3ee2004-06-08 02:58:50 +00001713 \var{size} is not zero or positive. Note that the memory buffer (as
1714 returned by \cfunction{PyObject_AsWriteBuffer()}) is not specifically
1715 aligned.
Fred Drake3adf79e2001-10-12 19:01:43 +00001716\end{cfuncdesc}
1717
1718
1719\subsection{Tuple Objects \label{tupleObjects}}
1720
1721\obindex{tuple}
1722\begin{ctypedesc}{PyTupleObject}
1723 This subtype of \ctype{PyObject} represents a Python tuple object.
1724\end{ctypedesc}
1725
1726\begin{cvardesc}{PyTypeObject}{PyTuple_Type}
1727 This instance of \ctype{PyTypeObject} represents the Python tuple
Georg Brandl7572f032006-08-08 20:48:10 +00001728 type; it is the same object as \code{tuple} and \code{types.TupleType}
1729 in the Python layer.\withsubitem{(in module types)}{\ttindex{TupleType}}.
Fred Drake3adf79e2001-10-12 19:01:43 +00001730\end{cvardesc}
1731
1732\begin{cfuncdesc}{int}{PyTuple_Check}{PyObject *p}
1733 Return true if \var{p} is a tuple object or an instance of a subtype
1734 of the tuple type.
1735 \versionchanged[Allowed subtypes to be accepted]{2.2}
1736\end{cfuncdesc}
1737
1738\begin{cfuncdesc}{int}{PyTuple_CheckExact}{PyObject *p}
1739 Return true if \var{p} is a tuple object, but not an instance of a
1740 subtype of the tuple type.
1741 \versionadded{2.2}
1742\end{cfuncdesc}
1743
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001744\begin{cfuncdesc}{PyObject*}{PyTuple_New}{Py_ssize_t len}
Fred Drake3adf79e2001-10-12 19:01:43 +00001745 Return a new tuple object of size \var{len}, or \NULL{} on failure.
1746\end{cfuncdesc}
1747
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001748\begin{cfuncdesc}{PyObject*}{PyTuple_Pack}{Py_ssize_t n, \moreargs}
Raymond Hettingercb2da432003-10-12 18:24:34 +00001749 Return a new tuple object of size \var{n}, or \NULL{} on failure.
1750 The tuple values are initialized to the subsequent \var{n} C arguments
1751 pointing to Python objects. \samp{PyTuple_Pack(2, \var{a}, \var{b})}
1752 is equivalent to \samp{Py_BuildValue("(OO)", \var{a}, \var{b})}.
Tim Peters9ddf40b2004-06-20 22:41:32 +00001753 \versionadded{2.4}
Raymond Hettingercb2da432003-10-12 18:24:34 +00001754\end{cfuncdesc}
1755
Georg Brandl5d30e752007-09-12 18:08:54 +00001756\begin{cfuncdesc}{Py_ssize_t}{PyTuple_Size}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00001757 Take a pointer to a tuple object, and return the size of that
Fred Drake3adf79e2001-10-12 19:01:43 +00001758 tuple.
1759\end{cfuncdesc}
1760
Georg Brandl5d30e752007-09-12 18:08:54 +00001761\begin{cfuncdesc}{Py_ssize_t}{PyTuple_GET_SIZE}{PyObject *p}
Fred Drake3adf79e2001-10-12 19:01:43 +00001762 Return the size of the tuple \var{p}, which must be non-\NULL{} and
1763 point to a tuple; no error checking is performed.
1764\end{cfuncdesc}
1765
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001766\begin{cfuncdesc}{PyObject*}{PyTuple_GetItem}{PyObject *p, Py_ssize_t pos}
Georg Brandl99363b62005-09-03 07:27:26 +00001767 Return the object at position \var{pos} in the tuple pointed to by
1768 \var{p}. If \var{pos} is out of bounds, return \NULL{} and sets an
Fred Drake3adf79e2001-10-12 19:01:43 +00001769 \exception{IndexError} exception.
1770\end{cfuncdesc}
1771
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001772\begin{cfuncdesc}{PyObject*}{PyTuple_GET_ITEM}{PyObject *p, Py_ssize_t pos}
Fred Drake3adf79e2001-10-12 19:01:43 +00001773 Like \cfunction{PyTuple_GetItem()}, but does no checking of its
1774 arguments.
1775\end{cfuncdesc}
1776
1777\begin{cfuncdesc}{PyObject*}{PyTuple_GetSlice}{PyObject *p,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001778 Py_ssize_t low, Py_ssize_t high}
Georg Brandl99363b62005-09-03 07:27:26 +00001779 Take a slice of the tuple pointed to by \var{p} from \var{low} to
1780 \var{high} and return it as a new tuple.
Fred Drake3adf79e2001-10-12 19:01:43 +00001781\end{cfuncdesc}
1782
1783\begin{cfuncdesc}{int}{PyTuple_SetItem}{PyObject *p,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001784 Py_ssize_t pos, PyObject *o}
Georg Brandl99363b62005-09-03 07:27:26 +00001785 Insert a reference to object \var{o} at position \var{pos} of the
1786 tuple pointed to by \var{p}. Return \code{0} on success.
Fred Drake3adf79e2001-10-12 19:01:43 +00001787 \note{This function ``steals'' a reference to \var{o}.}
1788\end{cfuncdesc}
1789
1790\begin{cfuncdesc}{void}{PyTuple_SET_ITEM}{PyObject *p,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001791 Py_ssize_t pos, PyObject *o}
Fred Drake3adf79e2001-10-12 19:01:43 +00001792 Like \cfunction{PyTuple_SetItem()}, but does no error checking, and
1793 should \emph{only} be used to fill in brand new tuples. \note{This
1794 function ``steals'' a reference to \var{o}.}
1795\end{cfuncdesc}
1796
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001797\begin{cfuncdesc}{int}{_PyTuple_Resize}{PyObject **p, Py_ssize_t newsize}
Fred Drake3adf79e2001-10-12 19:01:43 +00001798 Can be used to resize a tuple. \var{newsize} will be the new length
1799 of the tuple. Because tuples are \emph{supposed} to be immutable,
1800 this should only be used if there is only one reference to the
1801 object. Do \emph{not} use this if the tuple may already be known to
1802 some other part of the code. The tuple will always grow or shrink
1803 at the end. Think of this as destroying the old tuple and creating
1804 a new one, only more efficiently. Returns \code{0} on success.
1805 Client code should never assume that the resulting value of
1806 \code{*\var{p}} will be the same as before calling this function.
1807 If the object referenced by \code{*\var{p}} is replaced, the
1808 original \code{*\var{p}} is destroyed. On failure, returns
Tim Peters9ddf40b2004-06-20 22:41:32 +00001809 \code{-1} and sets \code{*\var{p}} to \NULL{}, and raises
Fred Drake3adf79e2001-10-12 19:01:43 +00001810 \exception{MemoryError} or
1811 \exception{SystemError}.
Tim Petersf582b822001-12-11 18:51:08 +00001812 \versionchanged[Removed unused third parameter, \var{last_is_sticky}]{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +00001813\end{cfuncdesc}
1814
1815
1816\subsection{List Objects \label{listObjects}}
1817
1818\obindex{list}
1819\begin{ctypedesc}{PyListObject}
1820 This subtype of \ctype{PyObject} represents a Python list object.
1821\end{ctypedesc}
1822
1823\begin{cvardesc}{PyTypeObject}{PyList_Type}
1824 This instance of \ctype{PyTypeObject} represents the Python list
Georg Brandl7572f032006-08-08 20:48:10 +00001825 type. This is the same object as \code{list} and \code{types.ListType}
1826 in the Python layer.\withsubitem{(in module types)}{\ttindex{ListType}}
Fred Drake3adf79e2001-10-12 19:01:43 +00001827\end{cvardesc}
1828
1829\begin{cfuncdesc}{int}{PyList_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00001830 Return true if \var{p} is a list object or an instance of a
Andrew MacIntyre13cd8892003-12-25 23:57:52 +00001831 subtype of the list type.
1832 \versionchanged[Allowed subtypes to be accepted]{2.2}
1833\end{cfuncdesc}
1834
1835\begin{cfuncdesc}{int}{PyList_CheckExact}{PyObject *p}
1836 Return true if \var{p} is a list object, but not an instance of a
1837 subtype of the list type.
1838 \versionadded{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +00001839\end{cfuncdesc}
1840
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001841\begin{cfuncdesc}{PyObject*}{PyList_New}{Py_ssize_t len}
Georg Brandl99363b62005-09-03 07:27:26 +00001842 Return a new list of length \var{len} on success, or \NULL{} on
Fred Drake3adf79e2001-10-12 19:01:43 +00001843 failure.
Georg Brandl595d9b62006-08-18 07:28:03 +00001844 \note{If \var{length} is greater than zero, the returned list object's
1845 items are set to \code{NULL}. Thus you cannot use abstract
Andrew M. Kuchlinge12b9f62006-08-18 13:54:33 +00001846 API functions such as \cfunction{PySequence_SetItem()}
1847 or expose the object to Python code before setting all items to a
Georg Brandl595d9b62006-08-18 07:28:03 +00001848 real object with \cfunction{PyList_SetItem()}.}
Fred Drake3adf79e2001-10-12 19:01:43 +00001849\end{cfuncdesc}
1850
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001851\begin{cfuncdesc}{Py_ssize_t}{PyList_Size}{PyObject *list}
Georg Brandl99363b62005-09-03 07:27:26 +00001852 Return the length of the list object in \var{list}; this is
Fred Drake3adf79e2001-10-12 19:01:43 +00001853 equivalent to \samp{len(\var{list})} on a list object.
1854 \bifuncindex{len}
1855\end{cfuncdesc}
1856
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001857\begin{cfuncdesc}{Py_ssize_t}{PyList_GET_SIZE}{PyObject *list}
Fred Drake3adf79e2001-10-12 19:01:43 +00001858 Macro form of \cfunction{PyList_Size()} without error checking.
1859\end{cfuncdesc}
1860
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001861\begin{cfuncdesc}{PyObject*}{PyList_GetItem}{PyObject *list, Py_ssize_t index}
Georg Brandl99363b62005-09-03 07:27:26 +00001862 Return the object at position \var{pos} in the list pointed to by
Georg Brandl4dce8e42006-04-06 12:45:51 +00001863 \var{p}. The position must be positive, indexing from the end of the
1864 list is not supported. If \var{pos} is out of bounds, return \NULL{}
1865 and set an \exception{IndexError} exception.
Fred Drake3adf79e2001-10-12 19:01:43 +00001866\end{cfuncdesc}
1867
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001868\begin{cfuncdesc}{PyObject*}{PyList_GET_ITEM}{PyObject *list, Py_ssize_t i}
Fred Drake3adf79e2001-10-12 19:01:43 +00001869 Macro form of \cfunction{PyList_GetItem()} without error checking.
1870\end{cfuncdesc}
1871
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001872\begin{cfuncdesc}{int}{PyList_SetItem}{PyObject *list, Py_ssize_t index,
Fred Drake3adf79e2001-10-12 19:01:43 +00001873 PyObject *item}
Georg Brandl99363b62005-09-03 07:27:26 +00001874 Set the item at index \var{index} in list to \var{item}. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00001875 \code{0} on success or \code{-1} on failure. \note{This function
1876 ``steals'' a reference to \var{item} and discards a reference to an
1877 item already in the list at the affected position.}
1878\end{cfuncdesc}
1879
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001880\begin{cfuncdesc}{void}{PyList_SET_ITEM}{PyObject *list, Py_ssize_t i,
Fred Drake3adf79e2001-10-12 19:01:43 +00001881 PyObject *o}
1882 Macro form of \cfunction{PyList_SetItem()} without error checking.
1883 This is normally only used to fill in new lists where there is no
1884 previous content.
1885 \note{This function ``steals'' a reference to \var{item}, and,
1886 unlike \cfunction{PyList_SetItem()}, does \emph{not} discard a
1887 reference to any item that it being replaced; any reference in
1888 \var{list} at position \var{i} will be leaked.}
1889\end{cfuncdesc}
1890
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001891\begin{cfuncdesc}{int}{PyList_Insert}{PyObject *list, Py_ssize_t index,
Fred Drake3adf79e2001-10-12 19:01:43 +00001892 PyObject *item}
Georg Brandl99363b62005-09-03 07:27:26 +00001893 Insert the item \var{item} into list \var{list} in front of index
1894 \var{index}. Return \code{0} if successful; return \code{-1} and
1895 set an exception if unsuccessful. Analogous to
Fred Drake3adf79e2001-10-12 19:01:43 +00001896 \code{\var{list}.insert(\var{index}, \var{item})}.
1897\end{cfuncdesc}
1898
1899\begin{cfuncdesc}{int}{PyList_Append}{PyObject *list, PyObject *item}
Georg Brandl99363b62005-09-03 07:27:26 +00001900 Append the object \var{item} at the end of list \var{list}.
1901 Return \code{0} if successful; return \code{-1} and set an
Fred Drake3adf79e2001-10-12 19:01:43 +00001902 exception if unsuccessful. Analogous to
1903 \code{\var{list}.append(\var{item})}.
1904\end{cfuncdesc}
1905
1906\begin{cfuncdesc}{PyObject*}{PyList_GetSlice}{PyObject *list,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001907 Py_ssize_t low, Py_ssize_t high}
Georg Brandl99363b62005-09-03 07:27:26 +00001908 Return a list of the objects in \var{list} containing the objects
1909 \emph{between} \var{low} and \var{high}. Return \NULL{} and set
Fred Drake3adf79e2001-10-12 19:01:43 +00001910 an exception if unsuccessful.
1911 Analogous to \code{\var{list}[\var{low}:\var{high}]}.
1912\end{cfuncdesc}
1913
1914\begin{cfuncdesc}{int}{PyList_SetSlice}{PyObject *list,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00001915 Py_ssize_t low, Py_ssize_t high,
Fred Drake3adf79e2001-10-12 19:01:43 +00001916 PyObject *itemlist}
Georg Brandl99363b62005-09-03 07:27:26 +00001917 Set the slice of \var{list} between \var{low} and \var{high} to the
Fred Drake3adf79e2001-10-12 19:01:43 +00001918 contents of \var{itemlist}. Analogous to
Raymond Hettinger9c7ed4c2003-10-26 17:20:07 +00001919 \code{\var{list}[\var{low}:\var{high}] = \var{itemlist}}.
1920 The \var{itemlist} may be \NULL{}, indicating the assignment
1921 of an empty list (slice deletion).
Georg Brandl99363b62005-09-03 07:27:26 +00001922 Return \code{0} on success, \code{-1} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00001923\end{cfuncdesc}
1924
1925\begin{cfuncdesc}{int}{PyList_Sort}{PyObject *list}
Georg Brandl99363b62005-09-03 07:27:26 +00001926 Sort the items of \var{list} in place. Return \code{0} on
Fred Drake3adf79e2001-10-12 19:01:43 +00001927 success, \code{-1} on failure. This is equivalent to
1928 \samp{\var{list}.sort()}.
1929\end{cfuncdesc}
1930
1931\begin{cfuncdesc}{int}{PyList_Reverse}{PyObject *list}
Georg Brandl99363b62005-09-03 07:27:26 +00001932 Reverse the items of \var{list} in place. Return \code{0} on
Fred Drake3adf79e2001-10-12 19:01:43 +00001933 success, \code{-1} on failure. This is the equivalent of
1934 \samp{\var{list}.reverse()}.
1935\end{cfuncdesc}
1936
1937\begin{cfuncdesc}{PyObject*}{PyList_AsTuple}{PyObject *list}
Georg Brandl99363b62005-09-03 07:27:26 +00001938 Return a new tuple object containing the contents of \var{list};
Fred Drake3adf79e2001-10-12 19:01:43 +00001939 equivalent to \samp{tuple(\var{list})}.\bifuncindex{tuple}
1940\end{cfuncdesc}
1941
1942
1943\section{Mapping Objects \label{mapObjects}}
1944
1945\obindex{mapping}
1946
1947
1948\subsection{Dictionary Objects \label{dictObjects}}
1949
1950\obindex{dictionary}
1951\begin{ctypedesc}{PyDictObject}
1952 This subtype of \ctype{PyObject} represents a Python dictionary
1953 object.
1954\end{ctypedesc}
1955
1956\begin{cvardesc}{PyTypeObject}{PyDict_Type}
1957 This instance of \ctype{PyTypeObject} represents the Python
1958 dictionary type. This is exposed to Python programs as
Georg Brandl7572f032006-08-08 20:48:10 +00001959 \code{dict} and \code{types.DictType}.
Fred Drake3adf79e2001-10-12 19:01:43 +00001960 \withsubitem{(in module types)}{\ttindex{DictType}\ttindex{DictionaryType}}
1961\end{cvardesc}
1962
1963\begin{cfuncdesc}{int}{PyDict_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00001964 Return true if \var{p} is a dict object or an instance of a
Andrew MacIntyre13cd8892003-12-25 23:57:52 +00001965 subtype of the dict type.
1966 \versionchanged[Allowed subtypes to be accepted]{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +00001967\end{cfuncdesc}
1968
Andrew MacIntyref72af652003-12-26 00:07:51 +00001969\begin{cfuncdesc}{int}{PyDict_CheckExact}{PyObject *p}
1970 Return true if \var{p} is a dict object, but not an instance of a
1971 subtype of the dict type.
1972 \versionadded{2.4}
1973\end{cfuncdesc}
1974
Fred Drake3adf79e2001-10-12 19:01:43 +00001975\begin{cfuncdesc}{PyObject*}{PyDict_New}{}
Georg Brandl99363b62005-09-03 07:27:26 +00001976 Return a new empty dictionary, or \NULL{} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00001977\end{cfuncdesc}
1978
1979\begin{cfuncdesc}{PyObject*}{PyDictProxy_New}{PyObject *dict}
1980 Return a proxy object for a mapping which enforces read-only
1981 behavior. This is normally used to create a proxy to prevent
1982 modification of the dictionary for non-dynamic class types.
1983 \versionadded{2.2}
1984\end{cfuncdesc}
1985
1986\begin{cfuncdesc}{void}{PyDict_Clear}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00001987 Empty an existing dictionary of all key-value pairs.
Fred Drake3adf79e2001-10-12 19:01:43 +00001988\end{cfuncdesc}
1989
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00001990\begin{cfuncdesc}{int}{PyDict_Contains}{PyObject *p, PyObject *key}
1991 Determine if dictionary \var{p} contains \var{key}. If an item
1992 in \var{p} is matches \var{key}, return \code{1}, otherwise return
1993 \code{0}. On error, return \code{-1}. This is equivalent to the
1994 Python expression \samp{\var{key} in \var{p}}.
Tim Peters9ddf40b2004-06-20 22:41:32 +00001995 \versionadded{2.4}
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00001996\end{cfuncdesc}
1997
Fred Drake3adf79e2001-10-12 19:01:43 +00001998\begin{cfuncdesc}{PyObject*}{PyDict_Copy}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00001999 Return a new dictionary that contains the same key-value pairs as
Fred Drake3adf79e2001-10-12 19:01:43 +00002000 \var{p}.
2001 \versionadded{1.6}
2002\end{cfuncdesc}
2003
2004\begin{cfuncdesc}{int}{PyDict_SetItem}{PyObject *p, PyObject *key,
2005 PyObject *val}
Georg Brandl99363b62005-09-03 07:27:26 +00002006 Insert \var{value} into the dictionary \var{p} with a key of
Fred Drake3adf79e2001-10-12 19:01:43 +00002007 \var{key}. \var{key} must be hashable; if it isn't,
2008 \exception{TypeError} will be raised.
Georg Brandl99363b62005-09-03 07:27:26 +00002009 Return \code{0} on success or \code{-1} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00002010\end{cfuncdesc}
2011
2012\begin{cfuncdesc}{int}{PyDict_SetItemString}{PyObject *p,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002013 const char *key,
Fred Drake3adf79e2001-10-12 19:01:43 +00002014 PyObject *val}
Georg Brandl99363b62005-09-03 07:27:26 +00002015 Insert \var{value} into the dictionary \var{p} using \var{key} as a
Fred Drake3adf79e2001-10-12 19:01:43 +00002016 key. \var{key} should be a \ctype{char*}. The key object is created
Georg Brandl99363b62005-09-03 07:27:26 +00002017 using \code{PyString_FromString(\var{key})}. Return \code{0} on
Fred Drake3adf79e2001-10-12 19:01:43 +00002018 success or \code{-1} on failure.
2019 \ttindex{PyString_FromString()}
2020\end{cfuncdesc}
2021
2022\begin{cfuncdesc}{int}{PyDict_DelItem}{PyObject *p, PyObject *key}
Georg Brandl99363b62005-09-03 07:27:26 +00002023 Remove the entry in dictionary \var{p} with key \var{key}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002024 \var{key} must be hashable; if it isn't, \exception{TypeError} is
Georg Brandl99363b62005-09-03 07:27:26 +00002025 raised. Return \code{0} on success or \code{-1} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00002026\end{cfuncdesc}
2027
2028\begin{cfuncdesc}{int}{PyDict_DelItemString}{PyObject *p, char *key}
Georg Brandl99363b62005-09-03 07:27:26 +00002029 Remove the entry in dictionary \var{p} which has a key specified by
2030 the string \var{key}. Return \code{0} on success or \code{-1} on
Fred Drake3adf79e2001-10-12 19:01:43 +00002031 failure.
2032\end{cfuncdesc}
2033
2034\begin{cfuncdesc}{PyObject*}{PyDict_GetItem}{PyObject *p, PyObject *key}
Georg Brandl99363b62005-09-03 07:27:26 +00002035 Return the object from dictionary \var{p} which has a key
2036 \var{key}. Return \NULL{} if the key \var{key} is not present, but
Fred Drake3adf79e2001-10-12 19:01:43 +00002037 \emph{without} setting an exception.
2038\end{cfuncdesc}
2039
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002040\begin{cfuncdesc}{PyObject*}{PyDict_GetItemString}{PyObject *p, const char *key}
Fred Drake3adf79e2001-10-12 19:01:43 +00002041 This is the same as \cfunction{PyDict_GetItem()}, but \var{key} is
2042 specified as a \ctype{char*}, rather than a \ctype{PyObject*}.
2043\end{cfuncdesc}
2044
2045\begin{cfuncdesc}{PyObject*}{PyDict_Items}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002046 Return a \ctype{PyListObject} containing all the items from the
Nicholas Bastin975e7252004-09-29 21:39:26 +00002047 dictionary, as in the dictionary method \method{items()} (see the
Fred Drake3adf79e2001-10-12 19:01:43 +00002048 \citetitle[../lib/lib.html]{Python Library Reference}).
2049\end{cfuncdesc}
2050
2051\begin{cfuncdesc}{PyObject*}{PyDict_Keys}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002052 Return a \ctype{PyListObject} containing all the keys from the
Fred Drake3adf79e2001-10-12 19:01:43 +00002053 dictionary, as in the dictionary method \method{keys()} (see the
2054 \citetitle[../lib/lib.html]{Python Library Reference}).
2055\end{cfuncdesc}
2056
2057\begin{cfuncdesc}{PyObject*}{PyDict_Values}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002058 Return a \ctype{PyListObject} containing all the values from the
Fred Drake3adf79e2001-10-12 19:01:43 +00002059 dictionary \var{p}, as in the dictionary method \method{values()}
2060 (see the \citetitle[../lib/lib.html]{Python Library Reference}).
2061\end{cfuncdesc}
2062
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002063\begin{cfuncdesc}{Py_ssize_t}{PyDict_Size}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002064 Return the number of items in the dictionary. This is equivalent
Fred Drake3adf79e2001-10-12 19:01:43 +00002065 to \samp{len(\var{p})} on a dictionary.\bifuncindex{len}
2066\end{cfuncdesc}
2067
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002068\begin{cfuncdesc}{int}{PyDict_Next}{PyObject *p, Py_ssize_t *ppos,
Fred Drake3adf79e2001-10-12 19:01:43 +00002069 PyObject **pkey, PyObject **pvalue}
2070 Iterate over all key-value pairs in the dictionary \var{p}. The
2071 \ctype{int} referred to by \var{ppos} must be initialized to
2072 \code{0} prior to the first call to this function to start the
2073 iteration; the function returns true for each pair in the
2074 dictionary, and false once all pairs have been reported. The
2075 parameters \var{pkey} and \var{pvalue} should either point to
2076 \ctype{PyObject*} variables that will be filled in with each key and
Tim Peters9ddf40b2004-06-20 22:41:32 +00002077 value, respectively, or may be \NULL{}. Any references returned through
Raymond Hettinger54693242003-12-13 19:48:41 +00002078 them are borrowed. \var{ppos} should not be altered during iteration.
2079 Its value represents offsets within the internal dictionary structure,
2080 and since the structure is sparse, the offsets are not consecutive.
Fred Drake3adf79e2001-10-12 19:01:43 +00002081
2082 For example:
2083
2084\begin{verbatim}
2085PyObject *key, *value;
Georg Brandleb681882007-01-17 21:20:01 +00002086Py_ssize_t pos = 0;
Fred Drake3adf79e2001-10-12 19:01:43 +00002087
2088while (PyDict_Next(self->dict, &pos, &key, &value)) {
2089 /* do something interesting with the values... */
2090 ...
2091}
2092\end{verbatim}
2093
2094 The dictionary \var{p} should not be mutated during iteration. It
2095 is safe (since Python 2.1) to modify the values of the keys as you
2096 iterate over the dictionary, but only so long as the set of keys
2097 does not change. For example:
2098
2099\begin{verbatim}
2100PyObject *key, *value;
Georg Brandleb681882007-01-17 21:20:01 +00002101Py_ssize_t pos = 0;
Fred Drake3adf79e2001-10-12 19:01:43 +00002102
2103while (PyDict_Next(self->dict, &pos, &key, &value)) {
2104 int i = PyInt_AS_LONG(value) + 1;
2105 PyObject *o = PyInt_FromLong(i);
2106 if (o == NULL)
2107 return -1;
2108 if (PyDict_SetItem(self->dict, key, o) < 0) {
2109 Py_DECREF(o);
2110 return -1;
2111 }
2112 Py_DECREF(o);
2113}
2114\end{verbatim}
2115\end{cfuncdesc}
2116
2117\begin{cfuncdesc}{int}{PyDict_Merge}{PyObject *a, PyObject *b, int override}
Tim Petersf582b822001-12-11 18:51:08 +00002118 Iterate over mapping object \var{b} adding key-value pairs to dictionary
2119 \var{a}.
2120 \var{b} may be a dictionary, or any object supporting
2121 \function{PyMapping_Keys()} and \function{PyObject_GetItem()}.
2122 If \var{override} is true, existing pairs in \var{a} will
Fred Drake3adf79e2001-10-12 19:01:43 +00002123 be replaced if a matching key is found in \var{b}, otherwise pairs
2124 will only be added if there is not a matching key in \var{a}.
Tim Petersf582b822001-12-11 18:51:08 +00002125 Return \code{0} on success or \code{-1} if an exception was
Fred Drake3adf79e2001-10-12 19:01:43 +00002126 raised.
2127\versionadded{2.2}
2128\end{cfuncdesc}
2129
2130\begin{cfuncdesc}{int}{PyDict_Update}{PyObject *a, PyObject *b}
2131 This is the same as \code{PyDict_Merge(\var{a}, \var{b}, 1)} in C,
Tim Petersf582b822001-12-11 18:51:08 +00002132 or \code{\var{a}.update(\var{b})} in Python. Return \code{0} on
Fred Drake3adf79e2001-10-12 19:01:43 +00002133 success or \code{-1} if an exception was raised.
2134 \versionadded{2.2}
2135\end{cfuncdesc}
2136
Tim Petersf582b822001-12-11 18:51:08 +00002137\begin{cfuncdesc}{int}{PyDict_MergeFromSeq2}{PyObject *a, PyObject *seq2,
2138 int override}
2139 Update or merge into dictionary \var{a}, from the key-value pairs in
2140 \var{seq2}. \var{seq2} must be an iterable object producing
2141 iterable objects of length 2, viewed as key-value pairs. In case of
2142 duplicate keys, the last wins if \var{override} is true, else the
2143 first wins.
2144 Return \code{0} on success or \code{-1} if an exception
2145 was raised.
2146 Equivalent Python (except for the return value):
2147
2148\begin{verbatim}
2149def PyDict_MergeFromSeq2(a, seq2, override):
2150 for key, value in seq2:
2151 if override or key not in a:
2152 a[key] = value
2153\end{verbatim}
2154
2155 \versionadded{2.2}
2156\end{cfuncdesc}
Fred Drake3adf79e2001-10-12 19:01:43 +00002157
Fred Drake54e62942001-12-11 19:40:16 +00002158
Fred Drake3adf79e2001-10-12 19:01:43 +00002159\section{Other Objects \label{otherObjects}}
2160
2161\subsection{File Objects \label{fileObjects}}
2162
2163\obindex{file}
2164Python's built-in file objects are implemented entirely on the
2165\ctype{FILE*} support from the C standard library. This is an
2166implementation detail and may change in future releases of Python.
2167
2168\begin{ctypedesc}{PyFileObject}
2169 This subtype of \ctype{PyObject} represents a Python file object.
2170\end{ctypedesc}
2171
2172\begin{cvardesc}{PyTypeObject}{PyFile_Type}
2173 This instance of \ctype{PyTypeObject} represents the Python file
Georg Brandl7572f032006-08-08 20:48:10 +00002174 type. This is exposed to Python programs as \code{file} and
2175 \code{types.FileType}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002176 \withsubitem{(in module types)}{\ttindex{FileType}}
2177\end{cvardesc}
2178
2179\begin{cfuncdesc}{int}{PyFile_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002180 Return true if its argument is a \ctype{PyFileObject} or a subtype
Fred Drake3adf79e2001-10-12 19:01:43 +00002181 of \ctype{PyFileObject}.
2182 \versionchanged[Allowed subtypes to be accepted]{2.2}
2183\end{cfuncdesc}
2184
2185\begin{cfuncdesc}{int}{PyFile_CheckExact}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002186 Return true if its argument is a \ctype{PyFileObject}, but not a
Fred Drake3adf79e2001-10-12 19:01:43 +00002187 subtype of \ctype{PyFileObject}.
2188 \versionadded{2.2}
2189\end{cfuncdesc}
2190
2191\begin{cfuncdesc}{PyObject*}{PyFile_FromString}{char *filename, char *mode}
Georg Brandl99363b62005-09-03 07:27:26 +00002192 On success, return a new file object that is opened on the file
Fred Drake3adf79e2001-10-12 19:01:43 +00002193 given by \var{filename}, with a file mode given by \var{mode}, where
2194 \var{mode} has the same semantics as the standard C routine
Georg Brandl99363b62005-09-03 07:27:26 +00002195 \cfunction{fopen()}\ttindex{fopen()}. On failure, return \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002196\end{cfuncdesc}
2197
2198\begin{cfuncdesc}{PyObject*}{PyFile_FromFile}{FILE *fp,
2199 char *name, char *mode,
2200 int (*close)(FILE*)}
Georg Brandl99363b62005-09-03 07:27:26 +00002201 Create a new \ctype{PyFileObject} from the already-open standard C
Fred Drake3adf79e2001-10-12 19:01:43 +00002202 file pointer, \var{fp}. The function \var{close} will be called
Georg Brandl99363b62005-09-03 07:27:26 +00002203 when the file should be closed. Return \NULL{} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00002204\end{cfuncdesc}
2205
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002206\begin{cfuncdesc}{FILE*}{PyFile_AsFile}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002207 Return the file object associated with \var{p} as a \ctype{FILE*}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002208\end{cfuncdesc}
2209
2210\begin{cfuncdesc}{PyObject*}{PyFile_GetLine}{PyObject *p, int n}
2211 Equivalent to \code{\var{p}.readline(\optional{\var{n}})}, this
2212 function reads one line from the object \var{p}. \var{p} may be a
2213 file object or any object with a \method{readline()} method. If
2214 \var{n} is \code{0}, exactly one line is read, regardless of the
2215 length of the line. If \var{n} is greater than \code{0}, no more
2216 than \var{n} bytes will be read from the file; a partial line can be
2217 returned. In both cases, an empty string is returned if the end of
2218 the file is reached immediately. If \var{n} is less than \code{0},
2219 however, one line is read regardless of length, but
2220 \exception{EOFError} is raised if the end of the file is reached
2221 immediately.
2222 \withsubitem{(built-in exception)}{\ttindex{EOFError}}
2223\end{cfuncdesc}
2224
2225\begin{cfuncdesc}{PyObject*}{PyFile_Name}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002226 Return the name of the file specified by \var{p} as a string
Fred Drake3adf79e2001-10-12 19:01:43 +00002227 object.
2228\end{cfuncdesc}
2229
2230\begin{cfuncdesc}{void}{PyFile_SetBufSize}{PyFileObject *p, int n}
2231 Available on systems with \cfunction{setvbuf()}\ttindex{setvbuf()}
2232 only. This should only be called immediately after file object
2233 creation.
2234\end{cfuncdesc}
2235
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00002236\begin{cfuncdesc}{int}{PyFile_Encoding}{PyFileObject *p, char *enc}
2237 Set the file's encoding for Unicode output to \var{enc}. Return
2238 1 on success and 0 on failure.
2239 \versionadded{2.3}
2240\end{cfuncdesc}
2241
Fred Drake3adf79e2001-10-12 19:01:43 +00002242\begin{cfuncdesc}{int}{PyFile_SoftSpace}{PyObject *p, int newflag}
Georg Brandl99363b62005-09-03 07:27:26 +00002243 This function exists for internal use by the interpreter. Set the
Fred Drake3adf79e2001-10-12 19:01:43 +00002244 \member{softspace} attribute of \var{p} to \var{newflag} and
Georg Brandl99363b62005-09-03 07:27:26 +00002245 \withsubitem{(file attribute)}{\ttindex{softspace}}return the
Fred Drake3adf79e2001-10-12 19:01:43 +00002246 previous value. \var{p} does not have to be a file object for this
2247 function to work properly; any object is supported (thought its only
2248 interesting if the \member{softspace} attribute can be set). This
2249 function clears any errors, and will return \code{0} as the previous
2250 value if the attribute either does not exist or if there were errors
2251 in retrieving it. There is no way to detect errors from this
2252 function, but doing so should not be needed.
2253\end{cfuncdesc}
2254
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002255\begin{cfuncdesc}{int}{PyFile_WriteObject}{PyObject *obj, PyObject *p,
Fred Drake3adf79e2001-10-12 19:01:43 +00002256 int flags}
Georg Brandl99363b62005-09-03 07:27:26 +00002257 Write object \var{obj} to file object \var{p}. The only supported
Fred Drake3adf79e2001-10-12 19:01:43 +00002258 flag for \var{flags} is
2259 \constant{Py_PRINT_RAW}\ttindex{Py_PRINT_RAW}; if given, the
2260 \function{str()} of the object is written instead of the
Georg Brandl99363b62005-09-03 07:27:26 +00002261 \function{repr()}. Return \code{0} on success or \code{-1} on
Fred Drake3adf79e2001-10-12 19:01:43 +00002262 failure; the appropriate exception will be set.
2263\end{cfuncdesc}
2264
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002265\begin{cfuncdesc}{int}{PyFile_WriteString}{const char *s, PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002266 Write string \var{s} to file object \var{p}. Return \code{0} on
Fred Drake3adf79e2001-10-12 19:01:43 +00002267 success or \code{-1} on failure; the appropriate exception will be
2268 set.
2269\end{cfuncdesc}
2270
2271
2272\subsection{Instance Objects \label{instanceObjects}}
2273
2274\obindex{instance}
2275There are very few functions specific to instance objects.
2276
2277\begin{cvardesc}{PyTypeObject}{PyInstance_Type}
2278 Type object for class instances.
2279\end{cvardesc}
2280
2281\begin{cfuncdesc}{int}{PyInstance_Check}{PyObject *obj}
Georg Brandl99363b62005-09-03 07:27:26 +00002282 Return true if \var{obj} is an instance.
Fred Drake3adf79e2001-10-12 19:01:43 +00002283\end{cfuncdesc}
2284
2285\begin{cfuncdesc}{PyObject*}{PyInstance_New}{PyObject *class,
2286 PyObject *arg,
2287 PyObject *kw}
2288 Create a new instance of a specific class. The parameters \var{arg}
2289 and \var{kw} are used as the positional and keyword parameters to
2290 the object's constructor.
2291\end{cfuncdesc}
2292
2293\begin{cfuncdesc}{PyObject*}{PyInstance_NewRaw}{PyObject *class,
2294 PyObject *dict}
Neil Schemenauerc4932292005-06-18 17:54:13 +00002295 Create a new instance of a specific class without calling its
Fred Drake3adf79e2001-10-12 19:01:43 +00002296 constructor. \var{class} is the class of new object. The
2297 \var{dict} parameter will be used as the object's \member{__dict__};
Tim Peters9ddf40b2004-06-20 22:41:32 +00002298 if \NULL{}, a new dictionary will be created for the instance.
Fred Drake3adf79e2001-10-12 19:01:43 +00002299\end{cfuncdesc}
2300
2301
Georg Brandl9b743f52006-02-20 12:57:53 +00002302\subsection{Function Objects \label{function-objects}}
2303
2304\obindex{function}
2305There are a few functions specific to Python functions.
2306
2307\begin{ctypedesc}{PyFunctionObject}
2308 The C structure used for functions.
2309\end{ctypedesc}
2310
2311\begin{cvardesc}{PyTypeObject}{PyFunction_Type}
2312 This is an instance of \ctype{PyTypeObject} and represents the
2313 Python function type. It is exposed to Python programmers as
2314 \code{types.FunctionType}.
2315 \withsubitem{(in module types)}{\ttindex{MethodType}}
2316\end{cvardesc}
2317
2318\begin{cfuncdesc}{int}{PyFunction_Check}{PyObject *o}
2319 Return true if \var{o} is a function object (has type
2320 \cdata{PyFunction_Type}). The parameter must not be \NULL{}.
2321\end{cfuncdesc}
2322
2323\begin{cfuncdesc}{PyObject*}{PyFunction_New}{PyObject *code,
2324 PyObject *globals}
2325 Return a new function object associated with the code object
Walter Dörwaldc44e14e2006-03-31 11:03:57 +00002326 \var{code}. \var{globals} must be a dictionary with the global
2327 variables accessible to the function.
Georg Brandl9b743f52006-02-20 12:57:53 +00002328
2329 The function's docstring, name and \var{__module__} are retrieved
2330 from the code object, the argument defaults and closure are set to
2331 \NULL{}.
2332\end{cfuncdesc}
2333
2334\begin{cfuncdesc}{PyObject*}{PyFunction_GetCode}{PyObject *op}
2335 Return the code object associated with the function object \var{op}.
2336\end{cfuncdesc}
2337
2338\begin{cfuncdesc}{PyObject*}{PyFunction_GetGlobals}{PyObject *op}
2339 Return the globals dictionary associated with the function object
2340 \var{op}.
2341\end{cfuncdesc}
2342
2343\begin{cfuncdesc}{PyObject*}{PyFunction_GetModule}{PyObject *op}
2344 Return the \var{__module__} attribute of the function object \var{op}.
2345 This is normally a string containing the module name, but can be set
2346 to any other object by Python code.
2347\end{cfuncdesc}
2348
2349\begin{cfuncdesc}{PyObject*}{PyFunction_GetDefaults}{PyObject *op}
2350 Return the argument default values of the function object \var{op}.
2351 This can be a tuple of arguments or \NULL{}.
2352\end{cfuncdesc}
2353
2354\begin{cfuncdesc}{int}{PyFunction_SetDefaults}{PyObject *op,
2355 PyObject *defaults}
2356 Set the argument default values for the function object \var{op}.
2357 \var{defaults} must be \var{Py_None} or a tuple.
2358
2359 Raises \exception{SystemError} and returns \code{-1} on failure.
2360\end{cfuncdesc}
2361
2362\begin{cfuncdesc}{PyObject*}{PyFunction_GetClosure}{PyObject *op}
2363 Return the closure associated with the function object \var{op}.
2364 This can be \NULL{} or a tuple of cell objects.
2365\end{cfuncdesc}
2366
2367\begin{cfuncdesc}{int}{PyFunction_SetClosure}{PyObject *op,
2368 PyObject *closure}
2369 Set the closure associated with the function object \var{op}.
2370 \var{closure} must be \var{Py_None} or a tuple of cell objects.
2371
2372 Raises \exception{SystemError} and returns \code{-1} on failure.
2373\end{cfuncdesc}
2374
2375
Fred Drake3adf79e2001-10-12 19:01:43 +00002376\subsection{Method Objects \label{method-objects}}
2377
2378\obindex{method}
2379There are some useful functions that are useful for working with
2380method objects.
2381
2382\begin{cvardesc}{PyTypeObject}{PyMethod_Type}
2383 This instance of \ctype{PyTypeObject} represents the Python method
2384 type. This is exposed to Python programs as \code{types.MethodType}.
2385 \withsubitem{(in module types)}{\ttindex{MethodType}}
2386\end{cvardesc}
2387
2388\begin{cfuncdesc}{int}{PyMethod_Check}{PyObject *o}
2389 Return true if \var{o} is a method object (has type
Tim Peters9ddf40b2004-06-20 22:41:32 +00002390 \cdata{PyMethod_Type}). The parameter must not be \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002391\end{cfuncdesc}
2392
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002393\begin{cfuncdesc}{PyObject*}{PyMethod_New}{PyObject *func,
Fred Drake3adf79e2001-10-12 19:01:43 +00002394 PyObject *self, PyObject *class}
2395 Return a new method object, with \var{func} being any callable
2396 object; this is the function that will be called when the method is
2397 called. If this method should be bound to an instance, \var{self}
2398 should be the instance and \var{class} should be the class of
2399 \var{self}, otherwise \var{self} should be \NULL{} and \var{class}
2400 should be the class which provides the unbound method..
2401\end{cfuncdesc}
2402
2403\begin{cfuncdesc}{PyObject*}{PyMethod_Class}{PyObject *meth}
2404 Return the class object from which the method \var{meth} was
2405 created; if this was created from an instance, it will be the class
2406 of the instance.
2407\end{cfuncdesc}
2408
2409\begin{cfuncdesc}{PyObject*}{PyMethod_GET_CLASS}{PyObject *meth}
2410 Macro version of \cfunction{PyMethod_Class()} which avoids error
2411 checking.
2412\end{cfuncdesc}
2413
2414\begin{cfuncdesc}{PyObject*}{PyMethod_Function}{PyObject *meth}
2415 Return the function object associated with the method \var{meth}.
2416\end{cfuncdesc}
2417
2418\begin{cfuncdesc}{PyObject*}{PyMethod_GET_FUNCTION}{PyObject *meth}
2419 Macro version of \cfunction{PyMethod_Function()} which avoids error
2420 checking.
2421\end{cfuncdesc}
2422
2423\begin{cfuncdesc}{PyObject*}{PyMethod_Self}{PyObject *meth}
2424 Return the instance associated with the method \var{meth} if it is
Tim Peters9ddf40b2004-06-20 22:41:32 +00002425 bound, otherwise return \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002426\end{cfuncdesc}
2427
2428\begin{cfuncdesc}{PyObject*}{PyMethod_GET_SELF}{PyObject *meth}
2429 Macro version of \cfunction{PyMethod_Self()} which avoids error
2430 checking.
2431\end{cfuncdesc}
2432
2433
2434\subsection{Module Objects \label{moduleObjects}}
2435
2436\obindex{module}
2437There are only a few functions special to module objects.
2438
2439\begin{cvardesc}{PyTypeObject}{PyModule_Type}
2440 This instance of \ctype{PyTypeObject} represents the Python module
2441 type. This is exposed to Python programs as
2442 \code{types.ModuleType}.
2443 \withsubitem{(in module types)}{\ttindex{ModuleType}}
2444\end{cvardesc}
2445
2446\begin{cfuncdesc}{int}{PyModule_Check}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002447 Return true if \var{p} is a module object, or a subtype of a module
Fred Drake3adf79e2001-10-12 19:01:43 +00002448 object.
2449 \versionchanged[Allowed subtypes to be accepted]{2.2}
2450\end{cfuncdesc}
2451
2452\begin{cfuncdesc}{int}{PyModule_CheckExact}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00002453 Return true if \var{p} is a module object, but not a subtype of
Fred Drake3adf79e2001-10-12 19:01:43 +00002454 \cdata{PyModule_Type}.
2455 \versionadded{2.2}
2456\end{cfuncdesc}
2457
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002458\begin{cfuncdesc}{PyObject*}{PyModule_New}{const char *name}
Fred Drake3adf79e2001-10-12 19:01:43 +00002459 Return a new module object with the \member{__name__} attribute set
2460 to \var{name}. Only the module's \member{__doc__} and
2461 \member{__name__} attributes are filled in; the caller is
2462 responsible for providing a \member{__file__} attribute.
2463 \withsubitem{(module attribute)}{
2464 \ttindex{__name__}\ttindex{__doc__}\ttindex{__file__}}
2465\end{cfuncdesc}
2466
2467\begin{cfuncdesc}{PyObject*}{PyModule_GetDict}{PyObject *module}
2468 Return the dictionary object that implements \var{module}'s
2469 namespace; this object is the same as the \member{__dict__}
2470 attribute of the module object. This function never fails.
2471 \withsubitem{(module attribute)}{\ttindex{__dict__}}
Fred Drakef495ef72002-04-12 19:32:07 +00002472 It is recommended extensions use other \cfunction{PyModule_*()}
2473 and \cfunction{PyObject_*()} functions rather than directly
2474 manipulate a module's \member{__dict__}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002475\end{cfuncdesc}
2476
2477\begin{cfuncdesc}{char*}{PyModule_GetName}{PyObject *module}
2478 Return \var{module}'s \member{__name__} value. If the module does
2479 not provide one, or if it is not a string, \exception{SystemError}
2480 is raised and \NULL{} is returned.
2481 \withsubitem{(module attribute)}{\ttindex{__name__}}
2482 \withsubitem{(built-in exception)}{\ttindex{SystemError}}
2483\end{cfuncdesc}
2484
2485\begin{cfuncdesc}{char*}{PyModule_GetFilename}{PyObject *module}
2486 Return the name of the file from which \var{module} was loaded using
2487 \var{module}'s \member{__file__} attribute. If this is not defined,
2488 or if it is not a string, raise \exception{SystemError} and return
Tim Peters9ddf40b2004-06-20 22:41:32 +00002489 \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002490 \withsubitem{(module attribute)}{\ttindex{__file__}}
2491 \withsubitem{(built-in exception)}{\ttindex{SystemError}}
2492\end{cfuncdesc}
2493
2494\begin{cfuncdesc}{int}{PyModule_AddObject}{PyObject *module,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002495 const char *name, PyObject *value}
Fred Drake3adf79e2001-10-12 19:01:43 +00002496 Add an object to \var{module} as \var{name}. This is a convenience
2497 function which can be used from the module's initialization
Georg Brandl99363b62005-09-03 07:27:26 +00002498 function. This steals a reference to \var{value}. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00002499 \code{-1} on error, \code{0} on success.
2500 \versionadded{2.0}
2501\end{cfuncdesc}
2502
2503\begin{cfuncdesc}{int}{PyModule_AddIntConstant}{PyObject *module,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002504 const char *name, long value}
Fred Drake3adf79e2001-10-12 19:01:43 +00002505 Add an integer constant to \var{module} as \var{name}. This
2506 convenience function can be used from the module's initialization
Georg Brandl99363b62005-09-03 07:27:26 +00002507 function. Return \code{-1} on error, \code{0} on success.
Fred Drake3adf79e2001-10-12 19:01:43 +00002508 \versionadded{2.0}
2509\end{cfuncdesc}
2510
2511\begin{cfuncdesc}{int}{PyModule_AddStringConstant}{PyObject *module,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002512 const char *name, const char *value}
Fred Drake3adf79e2001-10-12 19:01:43 +00002513 Add a string constant to \var{module} as \var{name}. This
2514 convenience function can be used from the module's initialization
Georg Brandl99363b62005-09-03 07:27:26 +00002515 function. The string \var{value} must be null-terminated. Return
Fred Drake3adf79e2001-10-12 19:01:43 +00002516 \code{-1} on error, \code{0} on success.
2517 \versionadded{2.0}
2518\end{cfuncdesc}
2519
2520
2521\subsection{Iterator Objects \label{iterator-objects}}
2522
2523Python provides two general-purpose iterator objects. The first, a
2524sequence iterator, works with an arbitrary sequence supporting the
2525\method{__getitem__()} method. The second works with a callable
2526object and a sentinel value, calling the callable for each item in the
2527sequence, and ending the iteration when the sentinel value is
2528returned.
2529
2530\begin{cvardesc}{PyTypeObject}{PySeqIter_Type}
2531 Type object for iterator objects returned by
2532 \cfunction{PySeqIter_New()} and the one-argument form of the
2533 \function{iter()} built-in function for built-in sequence types.
2534 \versionadded{2.2}
2535\end{cvardesc}
2536
2537\begin{cfuncdesc}{int}{PySeqIter_Check}{op}
2538 Return true if the type of \var{op} is \cdata{PySeqIter_Type}.
2539 \versionadded{2.2}
2540\end{cfuncdesc}
2541
2542\begin{cfuncdesc}{PyObject*}{PySeqIter_New}{PyObject *seq}
2543 Return an iterator that works with a general sequence object,
2544 \var{seq}. The iteration ends when the sequence raises
2545 \exception{IndexError} for the subscripting operation.
2546 \versionadded{2.2}
2547\end{cfuncdesc}
2548
2549\begin{cvardesc}{PyTypeObject}{PyCallIter_Type}
2550 Type object for iterator objects returned by
2551 \cfunction{PyCallIter_New()} and the two-argument form of the
2552 \function{iter()} built-in function.
2553 \versionadded{2.2}
2554\end{cvardesc}
2555
2556\begin{cfuncdesc}{int}{PyCallIter_Check}{op}
2557 Return true if the type of \var{op} is \cdata{PyCallIter_Type}.
2558 \versionadded{2.2}
2559\end{cfuncdesc}
2560
2561\begin{cfuncdesc}{PyObject*}{PyCallIter_New}{PyObject *callable,
2562 PyObject *sentinel}
2563 Return a new iterator. The first parameter, \var{callable}, can be
2564 any Python callable object that can be called with no parameters;
2565 each call to it should return the next item in the iteration. When
2566 \var{callable} returns a value equal to \var{sentinel}, the
2567 iteration will be terminated.
2568 \versionadded{2.2}
2569\end{cfuncdesc}
2570
2571
2572\subsection{Descriptor Objects \label{descriptor-objects}}
2573
Fred Drake54e62942001-12-11 19:40:16 +00002574``Descriptors'' are objects that describe some attribute of an object.
2575They are found in the dictionary of type objects.
2576
Fred Drake3adf79e2001-10-12 19:01:43 +00002577\begin{cvardesc}{PyTypeObject}{PyProperty_Type}
Fred Drake54e62942001-12-11 19:40:16 +00002578 The type object for the built-in descriptor types.
Fred Drake3adf79e2001-10-12 19:01:43 +00002579 \versionadded{2.2}
2580\end{cvardesc}
2581
2582\begin{cfuncdesc}{PyObject*}{PyDescr_NewGetSet}{PyTypeObject *type,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002583 struct PyGetSetDef *getset}
Fred Drake3adf79e2001-10-12 19:01:43 +00002584 \versionadded{2.2}
2585\end{cfuncdesc}
2586
2587\begin{cfuncdesc}{PyObject*}{PyDescr_NewMember}{PyTypeObject *type,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002588 struct PyMemberDef *meth}
Fred Drake3adf79e2001-10-12 19:01:43 +00002589 \versionadded{2.2}
2590\end{cfuncdesc}
2591
2592\begin{cfuncdesc}{PyObject*}{PyDescr_NewMethod}{PyTypeObject *type,
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002593 struct PyMethodDef *meth}
Fred Drake3adf79e2001-10-12 19:01:43 +00002594 \versionadded{2.2}
2595\end{cfuncdesc}
2596
2597\begin{cfuncdesc}{PyObject*}{PyDescr_NewWrapper}{PyTypeObject *type,
2598 struct wrapperbase *wrapper,
2599 void *wrapped}
2600 \versionadded{2.2}
2601\end{cfuncdesc}
2602
Thomas Heller8178a222004-02-09 10:47:11 +00002603\begin{cfuncdesc}{PyObject*}{PyDescr_NewClassMethod}{PyTypeObject *type,
2604 PyMethodDef *method}
2605 \versionadded{2.3}
2606\end{cfuncdesc}
2607
Fred Drake3adf79e2001-10-12 19:01:43 +00002608\begin{cfuncdesc}{int}{PyDescr_IsData}{PyObject *descr}
Georg Brandl99363b62005-09-03 07:27:26 +00002609 Return true if the descriptor objects \var{descr} describes a data
Fred Drake3adf79e2001-10-12 19:01:43 +00002610 attribute, or false if it describes a method. \var{descr} must be a
2611 descriptor object; there is no error checking.
2612 \versionadded{2.2}
2613\end{cfuncdesc}
2614
2615\begin{cfuncdesc}{PyObject*}{PyWrapper_New}{PyObject *, PyObject *}
2616 \versionadded{2.2}
2617\end{cfuncdesc}
2618
2619
2620\subsection{Slice Objects \label{slice-objects}}
2621
2622\begin{cvardesc}{PyTypeObject}{PySlice_Type}
2623 The type object for slice objects. This is the same as
Georg Brandl7572f032006-08-08 20:48:10 +00002624 \code{slice} and \code{types.SliceType}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002625 \withsubitem{(in module types)}{\ttindex{SliceType}}
2626\end{cvardesc}
2627
2628\begin{cfuncdesc}{int}{PySlice_Check}{PyObject *ob}
Georg Brandl99363b62005-09-03 07:27:26 +00002629 Return true if \var{ob} is a slice object; \var{ob} must not be
Tim Peters9ddf40b2004-06-20 22:41:32 +00002630 \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002631\end{cfuncdesc}
2632
2633\begin{cfuncdesc}{PyObject*}{PySlice_New}{PyObject *start, PyObject *stop,
2634 PyObject *step}
2635 Return a new slice object with the given values. The \var{start},
2636 \var{stop}, and \var{step} parameters are used as the values of the
2637 slice object attributes of the same names. Any of the values may be
Tim Peters9ddf40b2004-06-20 22:41:32 +00002638 \NULL{}, in which case the \code{None} will be used for the
Georg Brandl99363b62005-09-03 07:27:26 +00002639 corresponding attribute. Return \NULL{} if the new object could
Fred Drake3adf79e2001-10-12 19:01:43 +00002640 not be allocated.
2641\end{cfuncdesc}
2642
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002643\begin{cfuncdesc}{int}{PySlice_GetIndices}{PySliceObject *slice, Py_ssize_t length,
2644 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step}
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002645Retrieve the start, stop and step indices from the slice object
2646\var{slice}, assuming a sequence of length \var{length}. Treats
2647indices greater than \var{length} as errors.
2648
2649Returns 0 on success and -1 on error with no exception set (unless one
2650of the indices was not \constant{None} and failed to be converted to
2651an integer, in which case -1 is returned with an exception set).
2652
2653You probably do not want to use this function. If you want to use
2654slice objects in versions of Python prior to 2.3, you would probably
2655do well to incorporate the source of \cfunction{PySlice_GetIndicesEx},
2656suitably renamed, in the source of your extension.
2657\end{cfuncdesc}
2658
Martin v. Löwis29fafd82006-03-01 05:16:03 +00002659\begin{cfuncdesc}{int}{PySlice_GetIndicesEx}{PySliceObject *slice, Py_ssize_t length,
2660 Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step,
2661 Py_ssize_t *slicelength}
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002662Usable replacement for \cfunction{PySlice_GetIndices}. Retrieve the
2663start, stop, and step indices from the slice object \var{slice}
2664assuming a sequence of length \var{length}, and store the length of
2665the slice in \var{slicelength}. Out of bounds indices are clipped in
2666a manner consistent with the handling of normal slices.
2667
2668Returns 0 on success and -1 on error with exception set.
2669
2670\versionadded{2.3}
Fred Drake3adf79e2001-10-12 19:01:43 +00002671\end{cfuncdesc}
2672
2673
2674\subsection{Weak Reference Objects \label{weakref-objects}}
2675
2676Python supports \emph{weak references} as first-class objects. There
2677are two specific object types which directly implement weak
2678references. The first is a simple reference object, and the second
2679acts as a proxy for the original object as much as it can.
2680
2681\begin{cfuncdesc}{int}{PyWeakref_Check}{ob}
2682 Return true if \var{ob} is either a reference or proxy object.
2683 \versionadded{2.2}
2684\end{cfuncdesc}
2685
2686\begin{cfuncdesc}{int}{PyWeakref_CheckRef}{ob}
2687 Return true if \var{ob} is a reference object.
2688 \versionadded{2.2}
2689\end{cfuncdesc}
2690
2691\begin{cfuncdesc}{int}{PyWeakref_CheckProxy}{ob}
2692 Return true if \var{ob} is a proxy object.
2693 \versionadded{2.2}
2694\end{cfuncdesc}
2695
2696\begin{cfuncdesc}{PyObject*}{PyWeakref_NewRef}{PyObject *ob,
2697 PyObject *callback}
2698 Return a weak reference object for the object \var{ob}. This will
2699 always return a new reference, but is not guaranteed to create a new
2700 object; an existing reference object may be returned. The second
2701 parameter, \var{callback}, can be a callable object that receives
2702 notification when \var{ob} is garbage collected; it should accept a
Raymond Hettinger5232f502004-03-25 08:51:36 +00002703 single parameter, which will be the weak reference object itself.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002704 \var{callback} may also be \code{None} or \NULL{}. If \var{ob}
Fred Drake3adf79e2001-10-12 19:01:43 +00002705 is not a weakly-referencable object, or if \var{callback} is not
Tim Peters9ddf40b2004-06-20 22:41:32 +00002706 callable, \code{None}, or \NULL{}, this will return \NULL{} and
Fred Drake3adf79e2001-10-12 19:01:43 +00002707 raise \exception{TypeError}.
2708 \versionadded{2.2}
2709\end{cfuncdesc}
2710
2711\begin{cfuncdesc}{PyObject*}{PyWeakref_NewProxy}{PyObject *ob,
2712 PyObject *callback}
2713 Return a weak reference proxy object for the object \var{ob}. This
2714 will always return a new reference, but is not guaranteed to create
2715 a new object; an existing proxy object may be returned. The second
2716 parameter, \var{callback}, can be a callable object that receives
2717 notification when \var{ob} is garbage collected; it should accept a
Raymond Hettinger5232f502004-03-25 08:51:36 +00002718 single parameter, which will be the weak reference object itself.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002719 \var{callback} may also be \code{None} or \NULL{}. If \var{ob} is not
Fred Drake3adf79e2001-10-12 19:01:43 +00002720 a weakly-referencable object, or if \var{callback} is not callable,
Tim Peters9ddf40b2004-06-20 22:41:32 +00002721 \code{None}, or \NULL{}, this will return \NULL{} and raise
Fred Drake3adf79e2001-10-12 19:01:43 +00002722 \exception{TypeError}.
2723 \versionadded{2.2}
2724\end{cfuncdesc}
2725
2726\begin{cfuncdesc}{PyObject*}{PyWeakref_GetObject}{PyObject *ref}
Georg Brandl99363b62005-09-03 07:27:26 +00002727 Return the referenced object from a weak reference, \var{ref}. If
Ka-Ping Yeebd379e92003-03-28 18:07:16 +00002728 the referent is no longer live, returns \code{None}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002729 \versionadded{2.2}
2730\end{cfuncdesc}
2731
2732\begin{cfuncdesc}{PyObject*}{PyWeakref_GET_OBJECT}{PyObject *ref}
2733 Similar to \cfunction{PyWeakref_GetObject()}, but implemented as a
2734 macro that does no error checking.
2735 \versionadded{2.2}
2736\end{cfuncdesc}
2737
2738
2739\subsection{CObjects \label{cObjects}}
2740
2741\obindex{CObject}
2742Refer to \emph{Extending and Embedding the Python Interpreter},
Fred Drake54e62942001-12-11 19:40:16 +00002743section~1.12, ``Providing a C API for an Extension Module,'' for more
Fred Drake3adf79e2001-10-12 19:01:43 +00002744information on using these objects.
2745
2746
2747\begin{ctypedesc}{PyCObject}
2748 This subtype of \ctype{PyObject} represents an opaque value, useful
2749 for C extension modules who need to pass an opaque value (as a
2750 \ctype{void*} pointer) through Python code to other C code. It is
2751 often used to make a C function pointer defined in one module
2752 available to other modules, so the regular import mechanism can be
2753 used to access C APIs defined in dynamically loaded modules.
2754\end{ctypedesc}
2755
2756\begin{cfuncdesc}{int}{PyCObject_Check}{PyObject *p}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002757 Return true if its argument is a \ctype{PyCObject}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002758\end{cfuncdesc}
2759
Tim Petersf582b822001-12-11 18:51:08 +00002760\begin{cfuncdesc}{PyObject*}{PyCObject_FromVoidPtr}{void* cobj,
Fred Drake54e62942001-12-11 19:40:16 +00002761 void (*destr)(void *)}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002762 Create a \ctype{PyCObject} from the \code{void *}\var{cobj}. The
Fred Drake3adf79e2001-10-12 19:01:43 +00002763 \var{destr} function will be called when the object is reclaimed,
Tim Peters9ddf40b2004-06-20 22:41:32 +00002764 unless it is \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002765\end{cfuncdesc}
2766
2767\begin{cfuncdesc}{PyObject*}{PyCObject_FromVoidPtrAndDesc}{void* cobj,
2768 void* desc, void (*destr)(void *, void *)}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002769 Create a \ctype{PyCObject} from the \ctype{void *}\var{cobj}. The
Fred Drake3adf79e2001-10-12 19:01:43 +00002770 \var{destr} function will be called when the object is reclaimed.
2771 The \var{desc} argument can be used to pass extra callback data for
2772 the destructor function.
2773\end{cfuncdesc}
2774
2775\begin{cfuncdesc}{void*}{PyCObject_AsVoidPtr}{PyObject* self}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002776 Return the object \ctype{void *} that the \ctype{PyCObject}
Fred Drake3adf79e2001-10-12 19:01:43 +00002777 \var{self} was created with.
2778\end{cfuncdesc}
2779
2780\begin{cfuncdesc}{void*}{PyCObject_GetDesc}{PyObject* self}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002781 Return the description \ctype{void *} that the \ctype{PyCObject}
Fred Drake3adf79e2001-10-12 19:01:43 +00002782 \var{self} was created with.
2783\end{cfuncdesc}
Fred Drakecd8474e2001-11-26 21:29:17 +00002784
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002785\begin{cfuncdesc}{int}{PyCObject_SetVoidPtr}{PyObject* self, void* cobj}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002786 Set the void pointer inside \var{self} to \var{cobj}.
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002787 The \ctype{PyCObject} must not have an associated destructor.
2788 Return true on success, false on failure.
2789\end{cfuncdesc}
2790
Fred Drakecd8474e2001-11-26 21:29:17 +00002791
2792\subsection{Cell Objects \label{cell-objects}}
2793
2794``Cell'' objects are used to implement variables referenced by
2795multiple scopes. For each such variable, a cell object is created to
2796store the value; the local variables of each stack frame that
2797references the value contains a reference to the cells from outer
2798scopes which also use that variable. When the value is accessed, the
2799value contained in the cell is used instead of the cell object
2800itself. This de-referencing of the cell object requires support from
2801the generated byte-code; these are not automatically de-referenced
2802when accessed. Cell objects are not likely to be useful elsewhere.
2803
Fred Drake54e62942001-12-11 19:40:16 +00002804\begin{ctypedesc}{PyCellObject}
2805 The C structure used for cell objects.
2806\end{ctypedesc}
2807
Fred Drakecd8474e2001-11-26 21:29:17 +00002808\begin{cvardesc}{PyTypeObject}{PyCell_Type}
Georg Brandl9b743f52006-02-20 12:57:53 +00002809 The type object corresponding to cell objects.
Fred Drakecd8474e2001-11-26 21:29:17 +00002810\end{cvardesc}
2811
2812\begin{cfuncdesc}{int}{PyCell_Check}{ob}
2813 Return true if \var{ob} is a cell object; \var{ob} must not be
Tim Peters9ddf40b2004-06-20 22:41:32 +00002814 \NULL{}.
Fred Drakecd8474e2001-11-26 21:29:17 +00002815\end{cfuncdesc}
2816
2817\begin{cfuncdesc}{PyObject*}{PyCell_New}{PyObject *ob}
2818 Create and return a new cell object containing the value \var{ob}.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002819 The parameter may be \NULL{}.
Fred Drakecd8474e2001-11-26 21:29:17 +00002820\end{cfuncdesc}
2821
2822\begin{cfuncdesc}{PyObject*}{PyCell_Get}{PyObject *cell}
2823 Return the contents of the cell \var{cell}.
2824\end{cfuncdesc}
2825
2826\begin{cfuncdesc}{PyObject*}{PyCell_GET}{PyObject *cell}
2827 Return the contents of the cell \var{cell}, but without checking
Raymond Hettingerf4bb1f92003-08-23 03:38:11 +00002828 that \var{cell} is non-\NULL{} and a cell object.
Fred Drakecd8474e2001-11-26 21:29:17 +00002829\end{cfuncdesc}
2830
2831\begin{cfuncdesc}{int}{PyCell_Set}{PyObject *cell, PyObject *value}
2832 Set the contents of the cell object \var{cell} to \var{value}. This
2833 releases the reference to any current content of the cell.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002834 \var{value} may be \NULL{}. \var{cell} must be non-\NULL{}; if it is
Fred Drakecd8474e2001-11-26 21:29:17 +00002835 not a cell object, \code{-1} will be returned. On success, \code{0}
2836 will be returned.
2837\end{cfuncdesc}
2838
2839\begin{cfuncdesc}{void}{PyCell_SET}{PyObject *cell, PyObject *value}
2840 Sets the value of the cell object \var{cell} to \var{value}. No
2841 reference counts are adjusted, and no checks are made for safety;
2842 \var{cell} must be non-\NULL{} and must be a cell object.
2843\end{cfuncdesc}
Martin v. Löwise440e472004-06-01 15:22:42 +00002844
2845
2846\subsection{Generator Objects \label{gen-objects}}
2847
2848Generator objects are what Python uses to implement generator iterators.
2849They are normally created by iterating over a function that yields values,
2850rather than explicitly calling \cfunction{PyGen_New}.
2851
2852\begin{ctypedesc}{PyGenObject}
2853 The C structure used for generator objects.
2854\end{ctypedesc}
2855
2856\begin{cvardesc}{PyTypeObject}{PyGen_Type}
2857 The type object corresponding to generator objects
2858\end{cvardesc}
2859
2860\begin{cfuncdesc}{int}{PyGen_Check}{ob}
2861 Return true if \var{ob} is a generator object; \var{ob} must not be
Tim Peters9ddf40b2004-06-20 22:41:32 +00002862 \NULL{}.
Martin v. Löwise440e472004-06-01 15:22:42 +00002863\end{cfuncdesc}
2864
2865\begin{cfuncdesc}{int}{PyGen_CheckExact}{ob}
2866 Return true if \var{ob}'s type is \var{PyGen_Type}
2867 is a generator object; \var{ob} must not be
Tim Peters9ddf40b2004-06-20 22:41:32 +00002868 \NULL{}.
Martin v. Löwise440e472004-06-01 15:22:42 +00002869\end{cfuncdesc}
2870
2871\begin{cfuncdesc}{PyObject*}{PyGen_New}{PyFrameObject *frame}
2872 Create and return a new generator object based on the \var{frame} object.
Fred Drake3e482d92006-03-30 02:58:38 +00002873 A reference to \var{frame} is stolen by this function.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002874 The parameter must not be \NULL{}.
2875\end{cfuncdesc}
2876
2877
2878\subsection{DateTime Objects \label{datetime-objects}}
2879
2880Various date and time objects are supplied by the \module{datetime}
2881module. Before using any of these functions, the header file
2882\file{datetime.h} must be included in your source (note that this is
Andrew M. Kuchling1121e732006-10-26 19:11:06 +00002883not included by \file{Python.h}), and the macro
2884\cfunction{PyDateTime_IMPORT} must be invoked. The macro puts a
2885pointer to a C structure into a static variable,
2886\code{PyDateTimeAPI}, that is used by the following macros.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002887
Tim Peters183dabc2004-07-11 19:26:19 +00002888Type-check macros:
2889
2890\begin{cfuncdesc}{int}{PyDate_Check}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002891 Return true if \var{ob} is of type \cdata{PyDateTime_DateType} or
2892 a subtype of \cdata{PyDateTime_DateType}. \var{ob} must not be
2893 \NULL{}.
2894 \versionadded{2.4}
2895\end{cfuncdesc}
2896
Tim Peters183dabc2004-07-11 19:26:19 +00002897\begin{cfuncdesc}{int}{PyDate_CheckExact}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002898 Return true if \var{ob} is of type \cdata{PyDateTime_DateType}.
2899 \var{ob} must not be \NULL{}.
2900 \versionadded{2.4}
2901\end{cfuncdesc}
2902
Tim Peters183dabc2004-07-11 19:26:19 +00002903\begin{cfuncdesc}{int}{PyDateTime_Check}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002904 Return true if \var{ob} is of type \cdata{PyDateTime_DateTimeType} or
2905 a subtype of \cdata{PyDateTime_DateTimeType}. \var{ob} must not be
2906 \NULL{}.
2907 \versionadded{2.4}
2908\end{cfuncdesc}
2909
Tim Peters183dabc2004-07-11 19:26:19 +00002910\begin{cfuncdesc}{int}{PyDateTime_CheckExact}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002911 Return true if \var{ob} is of type \cdata{PyDateTime_DateTimeType}.
2912 \var{ob} must not be \NULL{}.
2913 \versionadded{2.4}
2914\end{cfuncdesc}
2915
Tim Peters183dabc2004-07-11 19:26:19 +00002916\begin{cfuncdesc}{int}{PyTime_Check}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002917 Return true if \var{ob} is of type \cdata{PyDateTime_TimeType} or
2918 a subtype of \cdata{PyDateTime_TimeType}. \var{ob} must not be
2919 \NULL{}.
2920 \versionadded{2.4}
2921\end{cfuncdesc}
2922
Tim Peters183dabc2004-07-11 19:26:19 +00002923\begin{cfuncdesc}{int}{PyTime_CheckExact}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002924 Return true if \var{ob} is of type \cdata{PyDateTime_TimeType}.
2925 \var{ob} must not be \NULL{}.
2926 \versionadded{2.4}
2927\end{cfuncdesc}
2928
Tim Peters183dabc2004-07-11 19:26:19 +00002929\begin{cfuncdesc}{int}{PyDelta_Check}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002930 Return true if \var{ob} is of type \cdata{PyDateTime_DeltaType} or
2931 a subtype of \cdata{PyDateTime_DeltaType}. \var{ob} must not be
2932 \NULL{}.
2933 \versionadded{2.4}
2934\end{cfuncdesc}
2935
Tim Peters183dabc2004-07-11 19:26:19 +00002936\begin{cfuncdesc}{int}{PyDelta_CheckExact}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002937 Return true if \var{ob} is of type \cdata{PyDateTime_DeltaType}.
2938 \var{ob} must not be \NULL{}.
2939 \versionadded{2.4}
2940\end{cfuncdesc}
2941
Tim Peters183dabc2004-07-11 19:26:19 +00002942\begin{cfuncdesc}{int}{PyTZInfo_Check}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002943 Return true if \var{ob} is of type \cdata{PyDateTime_TZInfoType} or
2944 a subtype of \cdata{PyDateTime_TZInfoType}. \var{ob} must not be
2945 \NULL{}.
2946 \versionadded{2.4}
2947\end{cfuncdesc}
2948
Tim Peters183dabc2004-07-11 19:26:19 +00002949\begin{cfuncdesc}{int}{PyTZInfo_CheckExact}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002950 Return true if \var{ob} is of type \cdata{PyDateTime_TZInfoType}.
2951 \var{ob} must not be \NULL{}.
2952 \versionadded{2.4}
2953\end{cfuncdesc}
2954
Tim Peters183dabc2004-07-11 19:26:19 +00002955Macros to create objects:
2956
Tim Peters9ddf40b2004-06-20 22:41:32 +00002957\begin{cfuncdesc}{PyObject*}{PyDate_FromDate}{int year, int month, int day}
2958 Return a \code{datetime.date} object with the specified year, month
2959 and day.
2960 \versionadded{2.4}
2961\end{cfuncdesc}
2962
Brett Cannon5bbe6ad2005-02-17 05:17:17 +00002963\begin{cfuncdesc}{PyObject*}{PyDateTime_FromDateAndTime}{int year, int month,
Tim Peters9ddf40b2004-06-20 22:41:32 +00002964 int day, int hour, int minute, int second, int usecond}
2965 Return a \code{datetime.datetime} object with the specified year, month,
2966 day, hour, minute, second and microsecond.
2967 \versionadded{2.4}
2968\end{cfuncdesc}
2969
2970\begin{cfuncdesc}{PyObject*}{PyTime_FromTime}{int hour, int minute,
2971 int second, int usecond}
2972 Return a \code{datetime.time} object with the specified hour, minute,
2973 second and microsecond.
2974 \versionadded{2.4}
2975\end{cfuncdesc}
2976
2977\begin{cfuncdesc}{PyObject*}{PyDelta_FromDSU}{int days, int seconds,
2978 int useconds}
2979 Return a \code{datetime.timedelta} object representing the given number
2980 of days, seconds and microseconds. Normalization is performed so that
2981 the resulting number of microseconds and seconds lie in the ranges
2982 documented for \code{datetime.timedelta} objects.
2983 \versionadded{2.4}
2984\end{cfuncdesc}
2985
Tim Peters8ff9f9f2004-07-17 01:42:26 +00002986Macros to extract fields from date objects. The argument must be an
Tim Peters183dabc2004-07-11 19:26:19 +00002987instance of \cdata{PyDateTime_Date}, including subclasses (such as
2988\cdata{PyDateTime_DateTime}). The argument must not be \NULL{}, and
2989the type is not checked:
2990
2991\begin{cfuncdesc}{int}{PyDateTime_GET_YEAR}{PyDateTime_Date *o}
2992 Return the year, as a positive int.
2993 \versionadded{2.4}
2994\end{cfuncdesc}
2995
2996\begin{cfuncdesc}{int}{PyDateTime_GET_MONTH}{PyDateTime_Date *o}
2997 Return the month, as an int from 1 through 12.
2998 \versionadded{2.4}
2999\end{cfuncdesc}
3000
3001\begin{cfuncdesc}{int}{PyDateTime_GET_DAY}{PyDateTime_Date *o}
3002 Return the day, as an int from 1 through 31.
3003 \versionadded{2.4}
3004\end{cfuncdesc}
3005
Tim Peters8ff9f9f2004-07-17 01:42:26 +00003006Macros to extract fields from datetime objects. The argument must be an
Tim Peters183dabc2004-07-11 19:26:19 +00003007instance of \cdata{PyDateTime_DateTime}, including subclasses.
3008The argument must not be \NULL{}, and the type is not checked:
3009
3010\begin{cfuncdesc}{int}{PyDateTime_DATE_GET_HOUR}{PyDateTime_DateTime *o}
Neal Norwitz7fdd92f2004-08-02 21:56:33 +00003011 Return the hour, as an int from 0 through 23.
Tim Peters183dabc2004-07-11 19:26:19 +00003012 \versionadded{2.4}
3013\end{cfuncdesc}
3014
3015\begin{cfuncdesc}{int}{PyDateTime_DATE_GET_MINUTE}{PyDateTime_DateTime *o}
3016 Return the minute, as an int from 0 through 59.
3017 \versionadded{2.4}
3018\end{cfuncdesc}
3019
3020\begin{cfuncdesc}{int}{PyDateTime_DATE_GET_SECOND}{PyDateTime_DateTime *o}
3021 Return the second, as an int from 0 through 59.
3022 \versionadded{2.4}
3023\end{cfuncdesc}
3024
3025\begin{cfuncdesc}{int}{PyDateTime_DATE_GET_MICROSECOND}{PyDateTime_DateTime *o}
3026 Return the microsecond, as an int from 0 through 999999.
3027 \versionadded{2.4}
3028\end{cfuncdesc}
3029
Tim Peters8ff9f9f2004-07-17 01:42:26 +00003030Macros to extract fields from time objects. The argument must be an
Tim Peters183dabc2004-07-11 19:26:19 +00003031instance of \cdata{PyDateTime_Time}, including subclasses.
3032The argument must not be \NULL{}, and the type is not checked:
3033
3034\begin{cfuncdesc}{int}{PyDateTime_TIME_GET_HOUR}{PyDateTime_Time *o}
Neal Norwitz7fdd92f2004-08-02 21:56:33 +00003035 Return the hour, as an int from 0 through 23.
Tim Peters183dabc2004-07-11 19:26:19 +00003036 \versionadded{2.4}
3037\end{cfuncdesc}
3038
3039\begin{cfuncdesc}{int}{PyDateTime_TIME_GET_MINUTE}{PyDateTime_Time *o}
3040 Return the minute, as an int from 0 through 59.
3041 \versionadded{2.4}
3042\end{cfuncdesc}
3043
3044\begin{cfuncdesc}{int}{PyDateTime_TIME_GET_SECOND}{PyDateTime_Time *o}
3045 Return the second, as an int from 0 through 59.
3046 \versionadded{2.4}
3047\end{cfuncdesc}
3048
3049\begin{cfuncdesc}{int}{PyDateTime_TIME_GET_MICROSECOND}{PyDateTime_Time *o}
3050 Return the microsecond, as an int from 0 through 999999.
3051 \versionadded{2.4}
3052\end{cfuncdesc}
3053
3054Macros for the convenience of modules implementing the DB API:
3055
Tim Peters9ddf40b2004-06-20 22:41:32 +00003056\begin{cfuncdesc}{PyObject*}{PyDateTime_FromTimestamp}{PyObject *args}
3057 Create and return a new \code{datetime.datetime} object given an argument
3058 tuple suitable for passing to \code{datetime.datetime.fromtimestamp()}.
Tim Peters9ddf40b2004-06-20 22:41:32 +00003059 \versionadded{2.4}
3060\end{cfuncdesc}
3061
3062\begin{cfuncdesc}{PyObject*}{PyDate_FromTimestamp}{PyObject *args}
3063 Create and return a new \code{datetime.date} object given an argument
3064 tuple suitable for passing to \code{datetime.date.fromtimestamp()}.
Tim Peters9ddf40b2004-06-20 22:41:32 +00003065 \versionadded{2.4}
Martin v. Löwise440e472004-06-01 15:22:42 +00003066\end{cfuncdesc}
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003067
3068
3069\subsection{Set Objects \label{setObjects}}
Tim Peters8931ff12006-05-13 23:28:20 +00003070\sectionauthor{Raymond D. Hettinger}{python@rcn.com}
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003071
3072\obindex{set}
3073\obindex{frozenset}
3074\versionadded{2.5}
3075
3076This section details the public API for \class{set} and \class{frozenset}
3077objects. Any functionality not listed below is best accessed using the
Raymond Hettinger0c230b92005-08-17 10:05:22 +00003078either the abstract object protocol (including
Tim Peters8931ff12006-05-13 23:28:20 +00003079\cfunction{PyObject_CallMethod()}, \cfunction{PyObject_RichCompareBool()},
3080\cfunction{PyObject_Hash()}, \cfunction{PyObject_Repr()},
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003081\cfunction{PyObject_IsTrue()}, \cfunction{PyObject_Print()}, and
Raymond Hettinger0c230b92005-08-17 10:05:22 +00003082\cfunction{PyObject_GetIter()})
3083or the abstract number protocol (including
3084\cfunction{PyNumber_Add()}, \cfunction{PyNumber_Subtract()},
3085\cfunction{PyNumber_Or()}, \cfunction{PyNumber_Xor()},
Georg Brandlb518d8c2006-02-22 11:46:55 +00003086\cfunction{PyNumber_InPlaceAdd()}, \cfunction{PyNumber_InPlaceSubtract()},
3087\cfunction{PyNumber_InPlaceOr()}, and \cfunction{PyNumber_InPlaceXor()}).
Raymond Hettinger66760f82006-03-20 18:35:55 +00003088
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003089\begin{ctypedesc}{PySetObject}
3090 This subtype of \ctype{PyObject} is used to hold the internal data for
3091 both \class{set} and \class{frozenset} objects. It is like a
3092 \ctype{PyDictObject} in that it is a fixed size for small sets
3093 (much like tuple storage) and will point to a separate, variable sized
3094 block of memory for medium and large sized sets (much like list storage).
3095 None of the fields of this structure should be considered public and
3096 are subject to change. All access should be done through the
Tim Peters8931ff12006-05-13 23:28:20 +00003097 documented API rather than by manipulating the values in the structure.
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003098
3099\end{ctypedesc}
3100
3101\begin{cvardesc}{PyTypeObject}{PySet_Type}
3102 This is an instance of \ctype{PyTypeObject} representing the Python
3103 \class{set} type.
3104\end{cvardesc}
3105
3106\begin{cvardesc}{PyTypeObject}{PyFrozenSet_Type}
3107 This is an instance of \ctype{PyTypeObject} representing the Python
3108 \class{frozenset} type.
3109\end{cvardesc}
3110
3111
3112The following type check macros work on pointers to any Python object.
3113Likewise, the constructor functions work with any iterable Python object.
3114
3115\begin{cfuncdesc}{int}{PyAnySet_Check}{PyObject *p}
Tim Peters8931ff12006-05-13 23:28:20 +00003116 Return true if \var{p} is a \class{set} object, a \class{frozenset}
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003117 object, or an instance of a subtype.
3118\end{cfuncdesc}
3119
3120\begin{cfuncdesc}{int}{PyAnySet_CheckExact}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00003121 Return true if \var{p} is a \class{set} object or a \class{frozenset}
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003122 object but not an instance of a subtype.
3123\end{cfuncdesc}
3124
3125\begin{cfuncdesc}{int}{PyFrozenSet_CheckExact}{PyObject *p}
Georg Brandl99363b62005-09-03 07:27:26 +00003126 Return true if \var{p} is a \class{frozenset} object
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003127 but not an instance of a subtype.
3128\end{cfuncdesc}
3129
3130\begin{cfuncdesc}{PyObject*}{PySet_New}{PyObject *iterable}
Georg Brandl99363b62005-09-03 07:27:26 +00003131 Return a new \class{set} containing objects returned by the
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003132 \var{iterable}. The \var{iterable} may be \NULL{} to create a
Georg Brandl99363b62005-09-03 07:27:26 +00003133 new empty set. Return the new set on success or \NULL{} on
3134 failure. Raise \exception{TypeError} if \var{iterable} is
Raymond Hettinger94fedf92005-08-17 12:23:45 +00003135 not actually iterable. The constructor is also useful for
3136 copying a set (\code{c=set(s)}).
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003137\end{cfuncdesc}
3138
3139\begin{cfuncdesc}{PyObject*}{PyFrozenSet_New}{PyObject *iterable}
Georg Brandl99363b62005-09-03 07:27:26 +00003140 Return a new \class{frozenset} containing objects returned by the
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003141 \var{iterable}. The \var{iterable} may be \NULL{} to create a
Georg Brandl99363b62005-09-03 07:27:26 +00003142 new empty frozenset. Return the new set on success or \NULL{} on
3143 failure. Raise \exception{TypeError} if \var{iterable} is
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00003144 not actually iterable.
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003145\end{cfuncdesc}
3146
3147
3148The following functions and macros are available for instances of
3149\class{set} or \class{frozenset} or instances of their subtypes.
3150
Georg Brandl5d30e752007-09-12 18:08:54 +00003151\begin{cfuncdesc}{Py_ssize_t}{PySet_Size}{PyObject *anyset}
Georg Brandl99363b62005-09-03 07:27:26 +00003152 Return the length of a \class{set} or \class{frozenset} object.
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003153 Equivalent to \samp{len(\var{anyset})}. Raises a
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00003154 \exception{PyExc_SystemError} if \var{anyset} is not a \class{set},
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003155 \class{frozenset}, or an instance of a subtype.
3156 \bifuncindex{len}
3157\end{cfuncdesc}
3158
Georg Brandl5d30e752007-09-12 18:08:54 +00003159\begin{cfuncdesc}{Py_ssize_t}{PySet_GET_SIZE}{PyObject *anyset}
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003160 Macro form of \cfunction{PySet_Size()} without error checking.
3161\end{cfuncdesc}
3162
3163\begin{cfuncdesc}{int}{PySet_Contains}{PyObject *anyset, PyObject *key}
Georg Brandl99363b62005-09-03 07:27:26 +00003164 Return 1 if found, 0 if not found, and -1 if an error is
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003165 encountered. Unlike the Python \method{__contains__()} method, this
3166 function does not automatically convert unhashable sets into temporary
Georg Brandl99363b62005-09-03 07:27:26 +00003167 frozensets. Raise a \exception{TypeError} if the \var{key} is unhashable.
3168 Raise \exception{PyExc_SystemError} if \var{anyset} is not a \class{set},
Tim Peters8931ff12006-05-13 23:28:20 +00003169 \class{frozenset}, or an instance of a subtype.
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003170\end{cfuncdesc}
3171
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003172The following functions are available for instances of \class{set} or
3173its subtypes but not for instances of \class{frozenset} or its subtypes.
3174
3175\begin{cfuncdesc}{int}{PySet_Add}{PyObject *set, PyObject *key}
Georg Brandl99363b62005-09-03 07:27:26 +00003176 Add \var{key} to a \class{set} instance. Does not apply to
3177 \class{frozenset} instances. Return 0 on success or -1 on failure.
3178 Raise a \exception{TypeError} if the \var{key} is unhashable.
3179 Raise a \exception{MemoryError} if there is no room to grow.
3180 Raise a \exception{SystemError} if \var{set} is an not an instance
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003181 of \class{set} or its subtype.
3182\end{cfuncdesc}
3183
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00003184\begin{cfuncdesc}{int}{PySet_Discard}{PyObject *set, PyObject *key}
Georg Brandl99363b62005-09-03 07:27:26 +00003185 Return 1 if found and removed, 0 if not found (no action taken),
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00003186 and -1 if an error is encountered. Does not raise \exception{KeyError}
Georg Brandl99363b62005-09-03 07:27:26 +00003187 for missing keys. Raise a \exception{TypeError} if the \var{key} is
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00003188 unhashable. Unlike the Python \method{discard()} method, this function
3189 does not automatically convert unhashable sets into temporary frozensets.
Georg Brandl99363b62005-09-03 07:27:26 +00003190 Raise \exception{PyExc_SystemError} if \var{set} is an not an instance
Tim Peters8931ff12006-05-13 23:28:20 +00003191 of \class{set} or its subtype.
Raymond Hettingerc47e01d2005-08-16 10:44:15 +00003192\end{cfuncdesc}
3193
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003194\begin{cfuncdesc}{PyObject*}{PySet_Pop}{PyObject *set}
Georg Brandl99363b62005-09-03 07:27:26 +00003195 Return a new reference to an arbitrary object in the \var{set},
3196 and removes the object from the \var{set}. Return \NULL{} on
3197 failure. Raise \exception{KeyError} if the set is empty.
3198 Raise a \exception{SystemError} if \var{set} is an not an instance
Tim Peters8931ff12006-05-13 23:28:20 +00003199 of \class{set} or its subtype.
Raymond Hettingerbeb31012005-08-16 03:47:52 +00003200\end{cfuncdesc}
3201
Barry Warsaw176014f2006-03-30 22:45:35 +00003202\begin{cfuncdesc}{int}{PySet_Clear}{PyObject *set}
3203 Empty an existing set of all elements.
3204\end{cfuncdesc}