blob: cb065ffa999c5980f9bbda8e51eda858e7aa2b82 [file] [log] [blame]
Fred Drake3adf79e2001-10-12 19:01:43 +00001\chapter{Concrete Objects Layer \label{concrete}}
2
3
4The functions in this chapter are specific to certain Python object
5types. Passing them an object of the wrong type is not a good idea;
6if you receive an object from a Python program and you are not sure
7that it has the right type, you must perform a type check first;
8for example, to check that an object is a dictionary, use
9\cfunction{PyDict_Check()}. The chapter is structured like the
10``family tree'' of Python object types.
11
12\warning{While the functions described in this chapter carefully check
13the type of the objects which are passed in, many of them do not check
14for \NULL{} being passed instead of a valid object. Allowing \NULL{}
15to be passed in can cause memory access violations and immediate
16termination of the interpreter.}
17
18
19\section{Fundamental Objects \label{fundamental}}
20
Tim Petersf582b822001-12-11 18:51:08 +000021This section describes Python type objects and the singleton object
Fred Drake3adf79e2001-10-12 19:01:43 +000022\code{None}.
23
24
25\subsection{Type Objects \label{typeObjects}}
26
27\obindex{type}
28\begin{ctypedesc}{PyTypeObject}
29 The C structure of the objects used to describe built-in types.
30\end{ctypedesc}
31
32\begin{cvardesc}{PyObject*}{PyType_Type}
33 This is the type object for type objects; it is the same object as
34 \code{types.TypeType} in the Python layer.
35 \withsubitem{(in module types)}{\ttindex{TypeType}}
36\end{cvardesc}
37
38\begin{cfuncdesc}{int}{PyType_Check}{PyObject *o}
Fred Drakee3c764b2002-04-10 17:52:52 +000039 Returns true if the object \var{o} is a type object, including
40 instances of types derived from the standard type object. Returns
41 false in all other cases.
42\end{cfuncdesc}
43
44\begin{cfuncdesc}{int}{PyType_CheckExact}{PyObject *o}
45 Returns true if the object \var{o} is a type object, but not a
46 subtype of the standard type object. Returns false in all other
47 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}
52 Returns true if the type object \var{o} sets the feature
53 \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}
63 Returns true if \var{a} is a subtype of \var{b}.
64 \versionadded{2.2}
65\end{cfuncdesc}
66
67\begin{cfuncdesc}{PyObject*}{PyType_GenericAlloc}{PyTypeObject *type,
68 int nitems}
69 \versionadded{2.2}
70\end{cfuncdesc}
71
72\begin{cfuncdesc}{PyObject*}{PyType_GenericNew}{PyTypeObject *type,
73 PyObject *args, PyObject *kwds}
74 \versionadded{2.2}
75\end{cfuncdesc}
76
77\begin{cfuncdesc}{int}{PyType_Ready}{PyTypeObject *type}
Fred Drake28de8d42002-04-12 16:15:10 +000078 Finalize a type object. This should be called on all type objects
79 to finish their initialization. This function is responsible for
80 adding inherited slots from a type's base class. Returns \code{0}
81 on success, or returns \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}
Fred Drake28eae082003-11-10 14:48:48 +0000101 Properly handles returning \cdata{Py_None} from within a C function.
Brett Cannon35d83602003-11-09 04:15:30 +0000102\end{csimplemacrodesc}
103
Fred Drake3adf79e2001-10-12 19:01:43 +0000104
105\section{Numeric Objects \label{numericObjects}}
106
107\obindex{numeric}
108
109
110\subsection{Plain Integer Objects \label{intObjects}}
111
112\obindex{integer}
113\begin{ctypedesc}{PyIntObject}
114 This subtype of \ctype{PyObject} represents a Python integer
115 object.
116\end{ctypedesc}
117
118\begin{cvardesc}{PyTypeObject}{PyInt_Type}
Tim Petersf582b822001-12-11 18:51:08 +0000119 This instance of \ctype{PyTypeObject} represents the Python plain
Fred Drake3adf79e2001-10-12 19:01:43 +0000120 integer type. This is the same object as \code{types.IntType}.
121 \withsubitem{(in modules types)}{\ttindex{IntType}}
122\end{cvardesc}
123
124\begin{cfuncdesc}{int}{PyInt_Check}{PyObject* o}
125 Returns true if \var{o} is of type \cdata{PyInt_Type} or a subtype
126 of \cdata{PyInt_Type}.
127 \versionchanged[Allowed subtypes to be accepted]{2.2}
128\end{cfuncdesc}
129
130\begin{cfuncdesc}{int}{PyInt_CheckExact}{PyObject* o}
131 Returns true if \var{o} is of type \cdata{PyInt_Type}, but not a
132 subtype of \cdata{PyInt_Type}.
133 \versionadded{2.2}
134\end{cfuncdesc}
135
Skip Montanaro1ff49a72003-02-03 05:13:24 +0000136\begin{cfuncdesc}{PyObject*}{PyInt_FromString}{char *str, char **pend,
137 int base}
138 Return a new \ctype{PyIntObject} or \ctype{PyLongObject} based on the
139 string value in \var{str}, which is interpreted according to the radix in
Tim Peters9ddf40b2004-06-20 22:41:32 +0000140 \var{base}. If \var{pend} is non-\NULL{}, \code{*\var{pend}} will point to
Skip Montanaro1ff49a72003-02-03 05:13:24 +0000141 the first character in \var{str} which follows the representation of the
142 number. If \var{base} is \code{0}, the radix will be determined based on
143 the leading characters of \var{str}: if \var{str} starts with \code{'0x'}
144 or \code{'0X'}, radix 16 will be used; if \var{str} starts with
145 \code{'0'}, radix 8 will be used; otherwise radix 10 will be used. If
146 \var{base} is not \code{0}, it must be between \code{2} and \code{36},
147 inclusive. Leading spaces are ignored. If there are no digits,
148 \exception{ValueError} will be raised. If the string represents a number
149 too large to be contained within the machine's \ctype{long int} type and
150 overflow warnings are being suppressed, a \ctype{PyLongObject} will be
151 returned. If overflow warnings are not being suppressed, \NULL{} will be
152 returned in this case.
153\end{cfuncdesc}
154
Fred Drake3adf79e2001-10-12 19:01:43 +0000155\begin{cfuncdesc}{PyObject*}{PyInt_FromLong}{long ival}
156 Creates a new integer object with a value of \var{ival}.
157
158 The current implementation keeps an array of integer objects for all
159 integers between \code{-1} and \code{100}, when you create an int in
160 that range you actually just get back a reference to the existing
161 object. So it should be possible to change the value of \code{1}. I
162 suspect the behaviour of Python in this case is undefined. :-)
163\end{cfuncdesc}
164
165\begin{cfuncdesc}{long}{PyInt_AsLong}{PyObject *io}
166 Will first attempt to cast the object to a \ctype{PyIntObject}, if
167 it is not already one, and then return its value.
168\end{cfuncdesc}
169
170\begin{cfuncdesc}{long}{PyInt_AS_LONG}{PyObject *io}
171 Returns the value of the object \var{io}. No error checking is
172 performed.
173\end{cfuncdesc}
174
Thomas Heller34d7f092003-04-23 19:51:05 +0000175\begin{cfuncdesc}{unsigned long}{PyInt_AsUnsignedLongMask}{PyObject *io}
176 Will first attempt to cast the object to a \ctype{PyIntObject} or
Fred Drakec22b2992003-04-23 20:38:41 +0000177 \ctype{PyLongObject}, if it is not already one, and then return its
Thomas Heller34d7f092003-04-23 19:51:05 +0000178 value as unsigned long. This function does not check for overflow.
179 \versionadded{2.3}
180\end{cfuncdesc}
181
Thomas Hellerfe080832004-07-23 14:49:52 +0000182\begin{cfuncdesc}{unsigned long long}{PyInt_AsUnsignedLongLongMask}{PyObject *io}
Thomas Heller34d7f092003-04-23 19:51:05 +0000183 Will first attempt to cast the object to a \ctype{PyIntObject} or
Fred Drakec22b2992003-04-23 20:38:41 +0000184 \ctype{PyLongObject}, if it is not already one, and then return its
Thomas Heller34d7f092003-04-23 19:51:05 +0000185 value as unsigned long long, without checking for overflow.
186 \versionadded{2.3}
187\end{cfuncdesc}
188
Fred Drake3adf79e2001-10-12 19:01:43 +0000189\begin{cfuncdesc}{long}{PyInt_GetMax}{}
190 Returns the system's idea of the largest integer it can handle
191 (\constant{LONG_MAX}\ttindex{LONG_MAX}, as defined in the system
192 header files).
193\end{cfuncdesc}
194
Fred Drake2be406b2004-08-03 16:02:35 +0000195\subsection{Boolean Objects \label{boolObjects}}
Skip Montanaro33ee76a2004-07-28 14:17:04 +0000196
197Booleans in Python are implemented as a subclass of integers. There
198are only two booleans, \constant{Py_False} and \constant{Py_True}. As
199such, the normal creation and deletion functions don't apply to
200booleans. The following macros are available, however.
201
202\begin{cfuncdesc}{int}{PyBool_Check}{PyObject* o}
203 Returns true if \var{o} is of type \cdata{PyBool_Type}.
204 \versionadded{2.3}
205\end{cfuncdesc}
206
Skip Montanaro6d3db702004-07-29 02:16:04 +0000207\begin{cvardesc}{PyObject*}{Py_False}
208 The Python \code{False} object. This object has no methods. It needs to
209 be treated just like any other object with respect to reference counts.
210\end{cvardesc}
Skip Montanaro33ee76a2004-07-28 14:17:04 +0000211
Skip Montanaro6d3db702004-07-29 02:16:04 +0000212\begin{cvardesc}{PyObject*}{Py_True}
213 The Python \code{True} object. This object has no methods. It needs to
214 be treated just like any other object with respect to reference counts.
215\end{cvardesc}
216
217\begin{csimplemacrodesc}{Py_RETURN_FALSE}
218 Return \constant{Py_False} from a function, properly incrementing its
219 reference count.
220\versionadded{2.4}
221\end{csimplemacrodesc}
222
223\begin{csimplemacrodesc}{Py_RETURN_TRUE}
Skip Montanaro33ee76a2004-07-28 14:17:04 +0000224Return Py_True from a function, properly incrementing its reference
225count.
226\versionadded{2.4}
Skip Montanaro6d3db702004-07-29 02:16:04 +0000227\end{csimplemacrodesc}
Skip Montanaro33ee76a2004-07-28 14:17:04 +0000228
229\begin{cfuncdesc}{int}{PyBool_Check}{PyObject* o}
230 Returns true if \var{o} is of type \cdata{PyBool_Type}.
231 \versionadded{2.3}
232\end{cfuncdesc}
233
234\begin{cfuncdesc}{int}{PyBool_FromLong}{long v}
235Returns \constant{Py_True} or \constant{Py_False} depending on the
236truth value of \var{v}.
237\versionadded{2.3}
238\end{cfuncdesc}
Fred Drake3adf79e2001-10-12 19:01:43 +0000239
240\subsection{Long Integer Objects \label{longObjects}}
241
242\obindex{long integer}
243\begin{ctypedesc}{PyLongObject}
244 This subtype of \ctype{PyObject} represents a Python long integer
245 object.
246\end{ctypedesc}
247
248\begin{cvardesc}{PyTypeObject}{PyLong_Type}
249 This instance of \ctype{PyTypeObject} represents the Python long
250 integer type. This is the same object as \code{types.LongType}.
251 \withsubitem{(in modules types)}{\ttindex{LongType}}
252\end{cvardesc}
253
254\begin{cfuncdesc}{int}{PyLong_Check}{PyObject *p}
255 Returns true if its argument is a \ctype{PyLongObject} or a subtype
256 of \ctype{PyLongObject}.
257 \versionchanged[Allowed subtypes to be accepted]{2.2}
258\end{cfuncdesc}
259
260\begin{cfuncdesc}{int}{PyLong_CheckExact}{PyObject *p}
261 Returns true if its argument is a \ctype{PyLongObject}, but not a
262 subtype of \ctype{PyLongObject}.
263 \versionadded{2.2}
264\end{cfuncdesc}
265
266\begin{cfuncdesc}{PyObject*}{PyLong_FromLong}{long v}
267 Returns a new \ctype{PyLongObject} object from \var{v}, or \NULL{}
268 on failure.
269\end{cfuncdesc}
270
271\begin{cfuncdesc}{PyObject*}{PyLong_FromUnsignedLong}{unsigned long v}
272 Returns a new \ctype{PyLongObject} object from a C \ctype{unsigned
273 long}, or \NULL{} on failure.
274\end{cfuncdesc}
275
276\begin{cfuncdesc}{PyObject*}{PyLong_FromLongLong}{long long v}
277 Returns a new \ctype{PyLongObject} object from a C \ctype{long long},
278 or \NULL{} on failure.
279\end{cfuncdesc}
280
281\begin{cfuncdesc}{PyObject*}{PyLong_FromUnsignedLongLong}{unsigned long long v}
282 Returns a new \ctype{PyLongObject} object from a C \ctype{unsigned
283 long long}, or \NULL{} on failure.
284\end{cfuncdesc}
285
286\begin{cfuncdesc}{PyObject*}{PyLong_FromDouble}{double v}
287 Returns a new \ctype{PyLongObject} object from the integer part of
288 \var{v}, or \NULL{} on failure.
289\end{cfuncdesc}
290
291\begin{cfuncdesc}{PyObject*}{PyLong_FromString}{char *str, char **pend,
292 int base}
293 Return a new \ctype{PyLongObject} based on the string value in
294 \var{str}, which is interpreted according to the radix in
Tim Peters9ddf40b2004-06-20 22:41:32 +0000295 \var{base}. If \var{pend} is non-\NULL{}, \code{*\var{pend}} will
Fred Drake3adf79e2001-10-12 19:01:43 +0000296 point to the first character in \var{str} which follows the
297 representation of the number. If \var{base} is \code{0}, the radix
Skip Montanaro1ff49a72003-02-03 05:13:24 +0000298 will be determined based on the leading characters of \var{str}: if
Fred Drake3adf79e2001-10-12 19:01:43 +0000299 \var{str} starts with \code{'0x'} or \code{'0X'}, radix 16 will be
300 used; if \var{str} starts with \code{'0'}, radix 8 will be used;
301 otherwise radix 10 will be used. If \var{base} is not \code{0}, it
302 must be between \code{2} and \code{36}, inclusive. Leading spaces
303 are ignored. If there are no digits, \exception{ValueError} will be
304 raised.
305\end{cfuncdesc}
306
307\begin{cfuncdesc}{PyObject*}{PyLong_FromUnicode}{Py_UNICODE *u,
308 int length, int base}
309 Convert a sequence of Unicode digits to a Python long integer
310 value. The first parameter, \var{u}, points to the first character
311 of the Unicode string, \var{length} gives the number of characters,
312 and \var{base} is the radix for the conversion. The radix must be
313 in the range [2, 36]; if it is out of range, \exception{ValueError}
314 will be raised.
315 \versionadded{1.6}
316\end{cfuncdesc}
317
318\begin{cfuncdesc}{PyObject*}{PyLong_FromVoidPtr}{void *p}
319 Create a Python integer or long integer from the pointer \var{p}.
320 The pointer value can be retrieved from the resulting value using
321 \cfunction{PyLong_AsVoidPtr()}.
322 \versionadded{1.5.2}
323\end{cfuncdesc}
324
325\begin{cfuncdesc}{long}{PyLong_AsLong}{PyObject *pylong}
326 Returns a C \ctype{long} representation of the contents of
327 \var{pylong}. If \var{pylong} is greater than
328 \constant{LONG_MAX}\ttindex{LONG_MAX}, an \exception{OverflowError}
329 is raised.
330 \withsubitem{(built-in exception)}{\ttindex{OverflowError}}
331\end{cfuncdesc}
332
333\begin{cfuncdesc}{unsigned long}{PyLong_AsUnsignedLong}{PyObject *pylong}
334 Returns a C \ctype{unsigned long} representation of the contents of
335 \var{pylong}. If \var{pylong} is greater than
336 \constant{ULONG_MAX}\ttindex{ULONG_MAX}, an
337 \exception{OverflowError} is raised.
Tim Petersf582b822001-12-11 18:51:08 +0000338 \withsubitem{(built-in exception)}{\ttindex{OverflowError}}
Fred Drake3adf79e2001-10-12 19:01:43 +0000339\end{cfuncdesc}
340
341\begin{cfuncdesc}{long long}{PyLong_AsLongLong}{PyObject *pylong}
342 Return a C \ctype{long long} from a Python long integer. If
343 \var{pylong} cannot be represented as a \ctype{long long}, an
344 \exception{OverflowError} will be raised.
345 \versionadded{2.2}
346\end{cfuncdesc}
347
348\begin{cfuncdesc}{unsigned long long}{PyLong_AsUnsignedLongLong}{PyObject
349 *pylong}
350 Return a C \ctype{unsigned long long} from a Python long integer.
351 If \var{pylong} cannot be represented as an \ctype{unsigned long
352 long}, an \exception{OverflowError} will be raised if the value is
353 positive, or a \exception{TypeError} will be raised if the value is
354 negative.
355 \versionadded{2.2}
356\end{cfuncdesc}
357
Thomas Heller34d7f092003-04-23 19:51:05 +0000358\begin{cfuncdesc}{unsigned long}{PyLong_AsUnsignedLongMask}{PyObject *io}
359 Return a C \ctype{unsigned long} from a Python long integer, without
360 checking for overflow.
361 \versionadded{2.3}
362\end{cfuncdesc}
363
364\begin{cfuncdesc}{unsigned long}{PyLong_AsUnsignedLongLongMask}{PyObject *io}
365 Return a C \ctype{unsigned long long} from a Python long integer, without
366 checking for overflow.
367 \versionadded{2.3}
368\end{cfuncdesc}
369
Fred Drake3adf79e2001-10-12 19:01:43 +0000370\begin{cfuncdesc}{double}{PyLong_AsDouble}{PyObject *pylong}
371 Returns a C \ctype{double} representation of the contents of
372 \var{pylong}. If \var{pylong} cannot be approximately represented
373 as a \ctype{double}, an \exception{OverflowError} exception is
374 raised and \code{-1.0} will be returned.
375\end{cfuncdesc}
376
377\begin{cfuncdesc}{void*}{PyLong_AsVoidPtr}{PyObject *pylong}
378 Convert a Python integer or long integer \var{pylong} to a C
379 \ctype{void} pointer. If \var{pylong} cannot be converted, an
380 \exception{OverflowError} will be raised. This is only assured to
381 produce a usable \ctype{void} pointer for values created with
382 \cfunction{PyLong_FromVoidPtr()}.
383 \versionadded{1.5.2}
384\end{cfuncdesc}
385
386
387\subsection{Floating Point Objects \label{floatObjects}}
388
389\obindex{floating point}
390\begin{ctypedesc}{PyFloatObject}
391 This subtype of \ctype{PyObject} represents a Python floating point
392 object.
393\end{ctypedesc}
394
395\begin{cvardesc}{PyTypeObject}{PyFloat_Type}
396 This instance of \ctype{PyTypeObject} represents the Python floating
397 point type. This is the same object as \code{types.FloatType}.
398 \withsubitem{(in modules types)}{\ttindex{FloatType}}
399\end{cvardesc}
400
401\begin{cfuncdesc}{int}{PyFloat_Check}{PyObject *p}
402 Returns true if its argument is a \ctype{PyFloatObject} or a subtype
403 of \ctype{PyFloatObject}.
404 \versionchanged[Allowed subtypes to be accepted]{2.2}
405\end{cfuncdesc}
406
407\begin{cfuncdesc}{int}{PyFloat_CheckExact}{PyObject *p}
408 Returns true if its argument is a \ctype{PyFloatObject}, but not a
409 subtype of \ctype{PyFloatObject}.
410 \versionadded{2.2}
411\end{cfuncdesc}
412
Skip Montanaro1ff49a72003-02-03 05:13:24 +0000413\begin{cfuncdesc}{PyObject*}{PyFloat_FromString}{PyObject *str, char **pend}
Skip Montanaroae31e9b2003-02-03 03:56:36 +0000414 Creates a \ctype{PyFloatObject} object based on the string value in
Skip Montanaro1ff49a72003-02-03 05:13:24 +0000415 \var{str}, or \NULL{} on failure. The \var{pend} argument is ignored. It
416 remains only for backward compatibility.
Skip Montanaroae31e9b2003-02-03 03:56:36 +0000417\end{cfuncdesc}
418
Fred Drake3adf79e2001-10-12 19:01:43 +0000419\begin{cfuncdesc}{PyObject*}{PyFloat_FromDouble}{double v}
420 Creates a \ctype{PyFloatObject} object from \var{v}, or \NULL{} on
421 failure.
422\end{cfuncdesc}
423
424\begin{cfuncdesc}{double}{PyFloat_AsDouble}{PyObject *pyfloat}
425 Returns a C \ctype{double} representation of the contents of
426 \var{pyfloat}.
427\end{cfuncdesc}
428
429\begin{cfuncdesc}{double}{PyFloat_AS_DOUBLE}{PyObject *pyfloat}
430 Returns a C \ctype{double} representation of the contents of
431 \var{pyfloat}, but without error checking.
432\end{cfuncdesc}
433
434
435\subsection{Complex Number Objects \label{complexObjects}}
436
437\obindex{complex number}
438Python's complex number objects are implemented as two distinct types
439when viewed from the C API: one is the Python object exposed to
440Python programs, and the other is a C structure which represents the
441actual complex number value. The API provides functions for working
442with both.
443
444\subsubsection{Complex Numbers as C Structures}
445
446Note that the functions which accept these structures as parameters
447and return them as results do so \emph{by value} rather than
448dereferencing them through pointers. This is consistent throughout
449the API.
450
451\begin{ctypedesc}{Py_complex}
452 The C structure which corresponds to the value portion of a Python
453 complex number object. Most of the functions for dealing with
454 complex number objects use structures of this type as input or
455 output values, as appropriate. It is defined as:
456
457\begin{verbatim}
458typedef struct {
459 double real;
460 double imag;
461} Py_complex;
462\end{verbatim}
463\end{ctypedesc}
464
465\begin{cfuncdesc}{Py_complex}{_Py_c_sum}{Py_complex left, Py_complex right}
466 Return the sum of two complex numbers, using the C
467 \ctype{Py_complex} representation.
468\end{cfuncdesc}
469
470\begin{cfuncdesc}{Py_complex}{_Py_c_diff}{Py_complex left, Py_complex right}
471 Return the difference between two complex numbers, using the C
472 \ctype{Py_complex} representation.
473\end{cfuncdesc}
474
475\begin{cfuncdesc}{Py_complex}{_Py_c_neg}{Py_complex complex}
476 Return the negation of the complex number \var{complex}, using the C
477 \ctype{Py_complex} representation.
478\end{cfuncdesc}
479
480\begin{cfuncdesc}{Py_complex}{_Py_c_prod}{Py_complex left, Py_complex right}
481 Return the product of two complex numbers, using the C
482 \ctype{Py_complex} representation.
483\end{cfuncdesc}
484
485\begin{cfuncdesc}{Py_complex}{_Py_c_quot}{Py_complex dividend,
486 Py_complex divisor}
487 Return the quotient of two complex numbers, using the C
488 \ctype{Py_complex} representation.
489\end{cfuncdesc}
490
491\begin{cfuncdesc}{Py_complex}{_Py_c_pow}{Py_complex num, Py_complex exp}
492 Return the exponentiation of \var{num} by \var{exp}, using the C
493 \ctype{Py_complex} representation.
494\end{cfuncdesc}
495
496
497\subsubsection{Complex Numbers as Python Objects}
498
499\begin{ctypedesc}{PyComplexObject}
500 This subtype of \ctype{PyObject} represents a Python complex number
501 object.
502\end{ctypedesc}
503
504\begin{cvardesc}{PyTypeObject}{PyComplex_Type}
505 This instance of \ctype{PyTypeObject} represents the Python complex
506 number type.
507\end{cvardesc}
508
509\begin{cfuncdesc}{int}{PyComplex_Check}{PyObject *p}
510 Returns true if its argument is a \ctype{PyComplexObject} or a
511 subtype of \ctype{PyComplexObject}.
512 \versionchanged[Allowed subtypes to be accepted]{2.2}
513\end{cfuncdesc}
514
515\begin{cfuncdesc}{int}{PyComplex_CheckExact}{PyObject *p}
516 Returns true if its argument is a \ctype{PyComplexObject}, but not a
517 subtype of \ctype{PyComplexObject}.
518 \versionadded{2.2}
519\end{cfuncdesc}
520
521\begin{cfuncdesc}{PyObject*}{PyComplex_FromCComplex}{Py_complex v}
522 Create a new Python complex number object from a C
523 \ctype{Py_complex} value.
524\end{cfuncdesc}
525
526\begin{cfuncdesc}{PyObject*}{PyComplex_FromDoubles}{double real, double imag}
527 Returns a new \ctype{PyComplexObject} object from \var{real} and
528 \var{imag}.
529\end{cfuncdesc}
530
531\begin{cfuncdesc}{double}{PyComplex_RealAsDouble}{PyObject *op}
532 Returns the real part of \var{op} as a C \ctype{double}.
533\end{cfuncdesc}
534
535\begin{cfuncdesc}{double}{PyComplex_ImagAsDouble}{PyObject *op}
536 Returns the imaginary part of \var{op} as a C \ctype{double}.
537\end{cfuncdesc}
538
539\begin{cfuncdesc}{Py_complex}{PyComplex_AsCComplex}{PyObject *op}
540 Returns the \ctype{Py_complex} value of the complex number
541 \var{op}.
542\end{cfuncdesc}
543
544
545
546\section{Sequence Objects \label{sequenceObjects}}
547
548\obindex{sequence}
Tim Petersf582b822001-12-11 18:51:08 +0000549Generic operations on sequence objects were discussed in the previous
550chapter; this section deals with the specific kinds of sequence
Fred Drake3adf79e2001-10-12 19:01:43 +0000551objects that are intrinsic to the Python language.
552
553
554\subsection{String Objects \label{stringObjects}}
555
556These functions raise \exception{TypeError} when expecting a string
557parameter and are called with a non-string parameter.
558
559\obindex{string}
560\begin{ctypedesc}{PyStringObject}
561 This subtype of \ctype{PyObject} represents a Python string object.
562\end{ctypedesc}
563
564\begin{cvardesc}{PyTypeObject}{PyString_Type}
565 This instance of \ctype{PyTypeObject} represents the Python string
566 type; it is the same object as \code{types.TypeType} in the Python
567 layer.
568 \withsubitem{(in module types)}{\ttindex{StringType}}.
569\end{cvardesc}
570
571\begin{cfuncdesc}{int}{PyString_Check}{PyObject *o}
572 Returns true if the object \var{o} is a string object or an instance
573 of a subtype of the string type.
574 \versionchanged[Allowed subtypes to be accepted]{2.2}
575\end{cfuncdesc}
576
577\begin{cfuncdesc}{int}{PyString_CheckExact}{PyObject *o}
578 Returns true if the object \var{o} is a string object, but not an
579 instance of a subtype of the string type.
580 \versionadded{2.2}
581\end{cfuncdesc}
582
583\begin{cfuncdesc}{PyObject*}{PyString_FromString}{const char *v}
584 Returns a new string object with the value \var{v} on success, and
Tim Peters9ddf40b2004-06-20 22:41:32 +0000585 \NULL{} on failure. The parameter \var{v} must not be \NULL{}; it
Fred Drake32a35872001-12-06 20:38:15 +0000586 will not be checked.
Fred Drake3adf79e2001-10-12 19:01:43 +0000587\end{cfuncdesc}
588
589\begin{cfuncdesc}{PyObject*}{PyString_FromStringAndSize}{const char *v,
590 int len}
591 Returns a new string object with the value \var{v} and length
592 \var{len} on success, and \NULL{} on failure. If \var{v} is
Tim Peters9ddf40b2004-06-20 22:41:32 +0000593 \NULL{}, the contents of the string are uninitialized.
Fred Drake3adf79e2001-10-12 19:01:43 +0000594\end{cfuncdesc}
595
596\begin{cfuncdesc}{PyObject*}{PyString_FromFormat}{const char *format, ...}
597 Takes a C \cfunction{printf()}-style \var{format} string and a
598 variable number of arguments, calculates the size of the resulting
599 Python string and returns a string with the values formatted into
600 it. The variable arguments must be C types and must correspond
601 exactly to the format characters in the \var{format} string. The
602 following format characters are allowed:
603
604 \begin{tableiii}{l|l|l}{member}{Format Characters}{Type}{Comment}
605 \lineiii{\%\%}{\emph{n/a}}{The literal \% character.}
606 \lineiii{\%c}{int}{A single character, represented as an C int.}
607 \lineiii{\%d}{int}{Exactly equivalent to \code{printf("\%d")}.}
608 \lineiii{\%ld}{long}{Exactly equivalent to \code{printf("\%ld")}.}
609 \lineiii{\%i}{int}{Exactly equivalent to \code{printf("\%i")}.}
610 \lineiii{\%x}{int}{Exactly equivalent to \code{printf("\%x")}.}
611 \lineiii{\%s}{char*}{A null-terminated C character array.}
612 \lineiii{\%p}{void*}{The hex representation of a C pointer.
613 Mostly equivalent to \code{printf("\%p")} except that it is
614 guaranteed to start with the literal \code{0x} regardless of
615 what the platform's \code{printf} yields.}
616 \end{tableiii}
617\end{cfuncdesc}
618
619\begin{cfuncdesc}{PyObject*}{PyString_FromFormatV}{const char *format,
620 va_list vargs}
621 Identical to \function{PyString_FromFormat()} except that it takes
622 exactly two arguments.
623\end{cfuncdesc}
624
625\begin{cfuncdesc}{int}{PyString_Size}{PyObject *string}
626 Returns the length of the string in string object \var{string}.
627\end{cfuncdesc}
628
629\begin{cfuncdesc}{int}{PyString_GET_SIZE}{PyObject *string}
630 Macro form of \cfunction{PyString_Size()} but without error
631 checking.
632\end{cfuncdesc}
633
634\begin{cfuncdesc}{char*}{PyString_AsString}{PyObject *string}
Fred Drake4b247262002-10-22 20:20:20 +0000635 Returns a NUL-terminated representation of the contents of
Fred Drake3adf79e2001-10-12 19:01:43 +0000636 \var{string}. The pointer refers to the internal buffer of
637 \var{string}, not a copy. The data must not be modified in any way,
638 unless the string was just created using
639 \code{PyString_FromStringAndSize(NULL, \var{size})}.
Fred Drake4b247262002-10-22 20:20:20 +0000640 It must not be deallocated. If \var{string} is a Unicode object,
641 this function computes the default encoding of \var{string} and
642 operates on that. If \var{string} is not a string object at all,
643 \cfunction{PyString_AsString()} returns \NULL{} and raises
644 \exception{TypeError}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000645\end{cfuncdesc}
646
647\begin{cfuncdesc}{char*}{PyString_AS_STRING}{PyObject *string}
648 Macro form of \cfunction{PyString_AsString()} but without error
Fred Drake4b247262002-10-22 20:20:20 +0000649 checking. Only string objects are supported; no Unicode objects
650 should be passed.
Fred Drake3adf79e2001-10-12 19:01:43 +0000651\end{cfuncdesc}
652
653\begin{cfuncdesc}{int}{PyString_AsStringAndSize}{PyObject *obj,
654 char **buffer,
655 int *length}
Fred Drake4b247262002-10-22 20:20:20 +0000656 Returns a NUL-terminated representation of the contents of the
Fred Drake3adf79e2001-10-12 19:01:43 +0000657 object \var{obj} through the output variables \var{buffer} and
658 \var{length}.
659
660 The function accepts both string and Unicode objects as input. For
661 Unicode objects it returns the default encoded version of the
Tim Peters9ddf40b2004-06-20 22:41:32 +0000662 object. If \var{length} is \NULL{}, the resulting buffer may not
Fred Drake4b247262002-10-22 20:20:20 +0000663 contain NUL characters; if it does, the function returns \code{-1}
664 and a \exception{TypeError} is raised.
Fred Drake3adf79e2001-10-12 19:01:43 +0000665
666 The buffer refers to an internal string buffer of \var{obj}, not a
667 copy. The data must not be modified in any way, unless the string
668 was just created using \code{PyString_FromStringAndSize(NULL,
Fred Drake4b247262002-10-22 20:20:20 +0000669 \var{size})}. It must not be deallocated. If \var{string} is a
670 Unicode object, this function computes the default encoding of
671 \var{string} and operates on that. If \var{string} is not a string
672 object at all, \cfunction{PyString_AsString()} returns \NULL{} and
673 raises \exception{TypeError}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000674\end{cfuncdesc}
675
676\begin{cfuncdesc}{void}{PyString_Concat}{PyObject **string,
677 PyObject *newpart}
678 Creates a new string object in \var{*string} containing the contents
679 of \var{newpart} appended to \var{string}; the caller will own the
680 new reference. The reference to the old value of \var{string} will
681 be stolen. If the new string cannot be created, the old reference
682 to \var{string} will still be discarded and the value of
Tim Peters9ddf40b2004-06-20 22:41:32 +0000683 \var{*string} will be set to \NULL{}; the appropriate exception will
Fred Drake3adf79e2001-10-12 19:01:43 +0000684 be set.
685\end{cfuncdesc}
686
687\begin{cfuncdesc}{void}{PyString_ConcatAndDel}{PyObject **string,
688 PyObject *newpart}
689 Creates a new string object in \var{*string} containing the contents
690 of \var{newpart} appended to \var{string}. This version decrements
691 the reference count of \var{newpart}.
692\end{cfuncdesc}
693
694\begin{cfuncdesc}{int}{_PyString_Resize}{PyObject **string, int newsize}
695 A way to resize a string object even though it is ``immutable''.
696 Only use this to build up a brand new string object; don't use this
Tim Peters5de98422002-04-27 18:44:32 +0000697 if the string may already be known in other parts of the code. It
698 is an error to call this function if the refcount on the input string
699 object is not one.
700 Pass the address of an existing string object as an lvalue (it may
701 be written into), and the new size desired. On success, \var{*string}
Fred Drake432425e2002-04-29 15:17:16 +0000702 holds the resized string object and \code{0} is returned; the address in
Tim Peters5de98422002-04-27 18:44:32 +0000703 \var{*string} may differ from its input value. If the
704 reallocation fails, the original string object at \var{*string} is
705 deallocated, \var{*string} is set to \NULL{}, a memory exception is set,
Fred Drake432425e2002-04-29 15:17:16 +0000706 and \code{-1} is returned.
Fred Drake3adf79e2001-10-12 19:01:43 +0000707\end{cfuncdesc}
708
709\begin{cfuncdesc}{PyObject*}{PyString_Format}{PyObject *format,
710 PyObject *args}
711 Returns a new string object from \var{format} and \var{args}.
712 Analogous to \code{\var{format} \%\ \var{args}}. The \var{args}
713 argument must be a tuple.
714\end{cfuncdesc}
715
716\begin{cfuncdesc}{void}{PyString_InternInPlace}{PyObject **string}
717 Intern the argument \var{*string} in place. The argument must be
718 the address of a pointer variable pointing to a Python string
719 object. If there is an existing interned string that is the same as
720 \var{*string}, it sets \var{*string} to it (decrementing the
721 reference count of the old string object and incrementing the
722 reference count of the interned string object), otherwise it leaves
723 \var{*string} alone and interns it (incrementing its reference
724 count). (Clarification: even though there is a lot of talk about
725 reference counts, think of this function as reference-count-neutral;
726 you own the object after the call if and only if you owned it before
727 the call.)
728\end{cfuncdesc}
729
730\begin{cfuncdesc}{PyObject*}{PyString_InternFromString}{const char *v}
731 A combination of \cfunction{PyString_FromString()} and
732 \cfunction{PyString_InternInPlace()}, returning either a new string
733 object that has been interned, or a new (``owned'') reference to an
734 earlier interned string object with the same value.
735\end{cfuncdesc}
736
737\begin{cfuncdesc}{PyObject*}{PyString_Decode}{const char *s,
738 int size,
739 const char *encoding,
740 const char *errors}
741 Creates an object by decoding \var{size} bytes of the encoded
742 buffer \var{s} using the codec registered for
743 \var{encoding}. \var{encoding} and \var{errors} have the same
744 meaning as the parameters of the same name in the
745 \function{unicode()} built-in function. The codec to be used is
746 looked up using the Python codec registry. Returns \NULL{} if
747 an exception was raised by the codec.
748\end{cfuncdesc}
749
750\begin{cfuncdesc}{PyObject*}{PyString_AsDecodedObject}{PyObject *str,
751 const char *encoding,
752 const char *errors}
753 Decodes a string object by passing it to the codec registered for
754 \var{encoding} and returns the result as Python
755 object. \var{encoding} and \var{errors} have the same meaning as the
756 parameters of the same name in the string \method{encode()} method.
757 The codec to be used is looked up using the Python codec registry.
758 Returns \NULL{} if an exception was raised by the codec.
759\end{cfuncdesc}
760
761\begin{cfuncdesc}{PyObject*}{PyString_Encode}{const char *s,
762 int size,
763 const char *encoding,
764 const char *errors}
765 Encodes the \ctype{char} buffer of the given size by passing it to
766 the codec registered for \var{encoding} and returns a Python object.
767 \var{encoding} and \var{errors} have the same meaning as the
768 parameters of the same name in the string \method{encode()} method.
769 The codec to be used is looked up using the Python codec
770 registry. Returns \NULL{} if an exception was raised by the
771 codec.
772\end{cfuncdesc}
773
774\begin{cfuncdesc}{PyObject*}{PyString_AsEncodedObject}{PyObject *str,
775 const char *encoding,
776 const char *errors}
777 Encodes a string object using the codec registered for
778 \var{encoding} and returns the result as Python object.
779 \var{encoding} and \var{errors} have the same meaning as the
780 parameters of the same name in the string \method{encode()} method.
781 The codec to be used is looked up using the Python codec registry.
782 Returns \NULL{} if an exception was raised by the codec.
783\end{cfuncdesc}
784
785
786\subsection{Unicode Objects \label{unicodeObjects}}
787\sectionauthor{Marc-Andre Lemburg}{mal@lemburg.com}
788
789%--- Unicode Type -------------------------------------------------------
790
791These are the basic Unicode object types used for the Unicode
792implementation in Python:
793
794\begin{ctypedesc}{Py_UNICODE}
795 This type represents a 16-bit unsigned storage type which is used by
796 Python internally as basis for holding Unicode ordinals. On
797 platforms where \ctype{wchar_t} is available and also has 16-bits,
798 \ctype{Py_UNICODE} is a typedef alias for \ctype{wchar_t} to enhance
799 native platform compatibility. On all other platforms,
800 \ctype{Py_UNICODE} is a typedef alias for \ctype{unsigned short}.
801\end{ctypedesc}
802
803\begin{ctypedesc}{PyUnicodeObject}
804 This subtype of \ctype{PyObject} represents a Python Unicode object.
805\end{ctypedesc}
806
807\begin{cvardesc}{PyTypeObject}{PyUnicode_Type}
808 This instance of \ctype{PyTypeObject} represents the Python Unicode
809 type.
810\end{cvardesc}
811
812The following APIs are really C macros and can be used to do fast
813checks and to access internal read-only data of Unicode objects:
814
815\begin{cfuncdesc}{int}{PyUnicode_Check}{PyObject *o}
816 Returns true if the object \var{o} is a Unicode object or an
817 instance of a Unicode subtype.
818 \versionchanged[Allowed subtypes to be accepted]{2.2}
819\end{cfuncdesc}
820
821\begin{cfuncdesc}{int}{PyUnicode_CheckExact}{PyObject *o}
822 Returns true if the object \var{o} is a Unicode object, but not an
823 instance of a subtype.
824 \versionadded{2.2}
825\end{cfuncdesc}
826
827\begin{cfuncdesc}{int}{PyUnicode_GET_SIZE}{PyObject *o}
828 Returns the size of the object. \var{o} has to be a
829 \ctype{PyUnicodeObject} (not checked).
830\end{cfuncdesc}
831
832\begin{cfuncdesc}{int}{PyUnicode_GET_DATA_SIZE}{PyObject *o}
833 Returns the size of the object's internal buffer in bytes. \var{o}
834 has to be a \ctype{PyUnicodeObject} (not checked).
835\end{cfuncdesc}
836
837\begin{cfuncdesc}{Py_UNICODE*}{PyUnicode_AS_UNICODE}{PyObject *o}
838 Returns a pointer to the internal \ctype{Py_UNICODE} buffer of the
839 object. \var{o} has to be a \ctype{PyUnicodeObject} (not checked).
840\end{cfuncdesc}
841
842\begin{cfuncdesc}{const char*}{PyUnicode_AS_DATA}{PyObject *o}
843 Returns a pointer to the internal buffer of the object.
844 \var{o} has to be a \ctype{PyUnicodeObject} (not checked).
845\end{cfuncdesc}
846
847% --- Unicode character properties ---------------------------------------
848
849Unicode provides many different character properties. The most often
850needed ones are available through these macros which are mapped to C
851functions depending on the Python configuration.
852
853\begin{cfuncdesc}{int}{Py_UNICODE_ISSPACE}{Py_UNICODE ch}
854 Returns 1/0 depending on whether \var{ch} is a whitespace
855 character.
856\end{cfuncdesc}
857
858\begin{cfuncdesc}{int}{Py_UNICODE_ISLOWER}{Py_UNICODE ch}
859 Returns 1/0 depending on whether \var{ch} is a lowercase character.
860\end{cfuncdesc}
861
862\begin{cfuncdesc}{int}{Py_UNICODE_ISUPPER}{Py_UNICODE ch}
863 Returns 1/0 depending on whether \var{ch} is an uppercase
864 character.
865\end{cfuncdesc}
866
867\begin{cfuncdesc}{int}{Py_UNICODE_ISTITLE}{Py_UNICODE ch}
868 Returns 1/0 depending on whether \var{ch} is a titlecase character.
869\end{cfuncdesc}
870
871\begin{cfuncdesc}{int}{Py_UNICODE_ISLINEBREAK}{Py_UNICODE ch}
872 Returns 1/0 depending on whether \var{ch} is a linebreak character.
873\end{cfuncdesc}
874
875\begin{cfuncdesc}{int}{Py_UNICODE_ISDECIMAL}{Py_UNICODE ch}
876 Returns 1/0 depending on whether \var{ch} is a decimal character.
877\end{cfuncdesc}
878
879\begin{cfuncdesc}{int}{Py_UNICODE_ISDIGIT}{Py_UNICODE ch}
880 Returns 1/0 depending on whether \var{ch} is a digit character.
881\end{cfuncdesc}
882
883\begin{cfuncdesc}{int}{Py_UNICODE_ISNUMERIC}{Py_UNICODE ch}
884 Returns 1/0 depending on whether \var{ch} is a numeric character.
885\end{cfuncdesc}
886
887\begin{cfuncdesc}{int}{Py_UNICODE_ISALPHA}{Py_UNICODE ch}
888 Returns 1/0 depending on whether \var{ch} is an alphabetic
889 character.
890\end{cfuncdesc}
891
892\begin{cfuncdesc}{int}{Py_UNICODE_ISALNUM}{Py_UNICODE ch}
893 Returns 1/0 depending on whether \var{ch} is an alphanumeric
894 character.
895\end{cfuncdesc}
896
Hye-Shik Chang974ed7c2004-06-02 16:49:17 +0000897\begin{cfuncdesc}{int}{Py_UNICODE_ISWIDE}{Py_UNICODE ch}
898 Returns 1/0 depending on whether \var{ch} is a wide or full-width
899 character.
900\end{cfuncdesc}
901
Fred Drake3adf79e2001-10-12 19:01:43 +0000902These APIs can be used for fast direct character conversions:
903
904\begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOLOWER}{Py_UNICODE ch}
905 Returns the character \var{ch} converted to lower case.
906\end{cfuncdesc}
907
908\begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOUPPER}{Py_UNICODE ch}
909 Returns the character \var{ch} converted to upper case.
910\end{cfuncdesc}
911
912\begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOTITLE}{Py_UNICODE ch}
913 Returns the character \var{ch} converted to title case.
914\end{cfuncdesc}
915
916\begin{cfuncdesc}{int}{Py_UNICODE_TODECIMAL}{Py_UNICODE ch}
917 Returns the character \var{ch} converted to a decimal positive
918 integer. Returns \code{-1} if this is not possible. Does not raise
919 exceptions.
920\end{cfuncdesc}
921
922\begin{cfuncdesc}{int}{Py_UNICODE_TODIGIT}{Py_UNICODE ch}
923 Returns the character \var{ch} converted to a single digit integer.
924 Returns \code{-1} if this is not possible. Does not raise
925 exceptions.
926\end{cfuncdesc}
927
928\begin{cfuncdesc}{double}{Py_UNICODE_TONUMERIC}{Py_UNICODE ch}
929 Returns the character \var{ch} converted to a (positive) double.
930 Returns \code{-1.0} if this is not possible. Does not raise
931 exceptions.
932\end{cfuncdesc}
933
934% --- Plain Py_UNICODE ---------------------------------------------------
935
936To create Unicode objects and access their basic sequence properties,
937use these APIs:
938
939\begin{cfuncdesc}{PyObject*}{PyUnicode_FromUnicode}{const Py_UNICODE *u,
Tim Petersf582b822001-12-11 18:51:08 +0000940 int size}
Fred Drake3adf79e2001-10-12 19:01:43 +0000941 Create a Unicode Object from the Py_UNICODE buffer \var{u} of the
942 given size. \var{u} may be \NULL{} which causes the contents to be
943 undefined. It is the user's responsibility to fill in the needed
944 data. The buffer is copied into the new object. If the buffer is
Tim Peters9ddf40b2004-06-20 22:41:32 +0000945 not \NULL{}, the return value might be a shared object. Therefore,
Fred Drake3adf79e2001-10-12 19:01:43 +0000946 modification of the resulting Unicode object is only allowed when
Tim Peters9ddf40b2004-06-20 22:41:32 +0000947 \var{u} is \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000948\end{cfuncdesc}
949
950\begin{cfuncdesc}{Py_UNICODE*}{PyUnicode_AsUnicode}{PyObject *unicode}
951 Return a read-only pointer to the Unicode object's internal
952 \ctype{Py_UNICODE} buffer, \NULL{} if \var{unicode} is not a Unicode
953 object.
954\end{cfuncdesc}
955
956\begin{cfuncdesc}{int}{PyUnicode_GetSize}{PyObject *unicode}
957 Return the length of the Unicode object.
958\end{cfuncdesc}
959
Hye-Shik Chang974ed7c2004-06-02 16:49:17 +0000960\begin{cfuncdesc}{int}{PyUnicode_GetWidth}{PyObject *unicode}
961 Return the fixed-width representation length of the Unicode object.
962\end{cfuncdesc}
963
Fred Drake3adf79e2001-10-12 19:01:43 +0000964\begin{cfuncdesc}{PyObject*}{PyUnicode_FromEncodedObject}{PyObject *obj,
965 const char *encoding,
966 const char *errors}
967 Coerce an encoded object \var{obj} to an Unicode object and return a
968 reference with incremented refcount.
969
970 Coercion is done in the following way:
971
972\begin{enumerate}
973\item Unicode objects are passed back as-is with incremented
974 refcount. \note{These cannot be decoded; passing a non-\NULL{}
975 value for encoding will result in a \exception{TypeError}.}
976
977\item String and other char buffer compatible objects are decoded
978 according to the given encoding and using the error handling
979 defined by errors. Both can be \NULL{} to have the interface
980 use the default values (see the next section for details).
981
982\item All other objects cause an exception.
983\end{enumerate}
984
985 The API returns \NULL{} if there was an error. The caller is
986 responsible for decref'ing the returned objects.
987\end{cfuncdesc}
988
989\begin{cfuncdesc}{PyObject*}{PyUnicode_FromObject}{PyObject *obj}
990 Shortcut for \code{PyUnicode_FromEncodedObject(obj, NULL, "strict")}
991 which is used throughout the interpreter whenever coercion to
992 Unicode is needed.
993\end{cfuncdesc}
994
995% --- wchar_t support for platforms which support it ---------------------
996
997If the platform supports \ctype{wchar_t} and provides a header file
998wchar.h, Python can interface directly to this type using the
999following functions. Support is optimized if Python's own
1000\ctype{Py_UNICODE} type is identical to the system's \ctype{wchar_t}.
1001
1002\begin{cfuncdesc}{PyObject*}{PyUnicode_FromWideChar}{const wchar_t *w,
1003 int size}
Thomas Heller541703b2002-04-29 17:28:43 +00001004 Create a Unicode object from the \ctype{wchar_t} buffer \var{w} of
Fred Drake3adf79e2001-10-12 19:01:43 +00001005 the given size. Returns \NULL{} on failure.
1006\end{cfuncdesc}
1007
1008\begin{cfuncdesc}{int}{PyUnicode_AsWideChar}{PyUnicodeObject *unicode,
1009 wchar_t *w,
1010 int size}
Thomas Heller541703b2002-04-29 17:28:43 +00001011 Copies the Unicode object contents into the \ctype{wchar_t} buffer
1012 \var{w}. At most \var{size} \ctype{wchar_t} characters are copied.
1013 Returns the number of \ctype{wchar_t} characters copied or -1 in
Fred Drake3adf79e2001-10-12 19:01:43 +00001014 case of an error.
1015\end{cfuncdesc}
1016
1017
1018\subsubsection{Built-in Codecs \label{builtinCodecs}}
1019
1020Python provides a set of builtin codecs which are written in C
1021for speed. All of these codecs are directly usable via the
1022following functions.
1023
1024Many of the following APIs take two arguments encoding and
1025errors. These parameters encoding and errors have the same semantics
1026as the ones of the builtin unicode() Unicode object constructor.
1027
1028Setting encoding to \NULL{} causes the default encoding to be used
1029which is \ASCII. The file system calls should use
1030\cdata{Py_FileSystemDefaultEncoding} as the encoding for file
1031names. This variable should be treated as read-only: On some systems,
1032it will be a pointer to a static string, on others, it will change at
Raymond Hettingercb2da432003-10-12 18:24:34 +00001033run-time (such as when the application invokes setlocale).
Fred Drake3adf79e2001-10-12 19:01:43 +00001034
1035Error handling is set by errors which may also be set to \NULL{}
1036meaning to use the default handling defined for the codec. Default
1037error handling for all builtin codecs is ``strict''
1038(\exception{ValueError} is raised).
1039
1040The codecs all use a similar interface. Only deviation from the
1041following generic ones are documented for simplicity.
1042
1043% --- Generic Codecs -----------------------------------------------------
1044
1045These are the generic codec APIs:
1046
1047\begin{cfuncdesc}{PyObject*}{PyUnicode_Decode}{const char *s,
1048 int size,
1049 const char *encoding,
1050 const char *errors}
1051 Create a Unicode object by decoding \var{size} bytes of the encoded
1052 string \var{s}. \var{encoding} and \var{errors} have the same
1053 meaning as the parameters of the same name in the
1054 \function{unicode()} builtin function. The codec to be used is
1055 looked up using the Python codec registry. Returns \NULL{} if an
1056 exception was raised by the codec.
1057\end{cfuncdesc}
1058
1059\begin{cfuncdesc}{PyObject*}{PyUnicode_Encode}{const Py_UNICODE *s,
1060 int size,
1061 const char *encoding,
1062 const char *errors}
1063 Encodes the \ctype{Py_UNICODE} buffer of the given size and returns
1064 a Python string object. \var{encoding} and \var{errors} have the
1065 same meaning as the parameters of the same name in the Unicode
1066 \method{encode()} method. The codec to be used is looked up using
1067 the Python codec registry. Returns \NULL{} if an exception was
1068 raised by the codec.
1069\end{cfuncdesc}
1070
1071\begin{cfuncdesc}{PyObject*}{PyUnicode_AsEncodedString}{PyObject *unicode,
1072 const char *encoding,
1073 const char *errors}
1074 Encodes a Unicode object and returns the result as Python string
1075 object. \var{encoding} and \var{errors} have the same meaning as the
1076 parameters of the same name in the Unicode \method{encode()} method.
1077 The codec to be used is looked up using the Python codec registry.
1078 Returns \NULL{} if an exception was raised by the codec.
1079\end{cfuncdesc}
1080
1081% --- UTF-8 Codecs -------------------------------------------------------
1082
1083These are the UTF-8 codec APIs:
1084
1085\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF8}{const char *s,
1086 int size,
1087 const char *errors}
1088 Creates a Unicode object by decoding \var{size} bytes of the UTF-8
1089 encoded string \var{s}. Returns \NULL{} if an exception was raised
1090 by the codec.
1091\end{cfuncdesc}
1092
1093\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUTF8}{const Py_UNICODE *s,
1094 int size,
1095 const char *errors}
1096 Encodes the \ctype{Py_UNICODE} buffer of the given size using UTF-8
1097 and returns a Python string object. Returns \NULL{} if an exception
1098 was raised by the codec.
1099\end{cfuncdesc}
1100
1101\begin{cfuncdesc}{PyObject*}{PyUnicode_AsUTF8String}{PyObject *unicode}
1102 Encodes a Unicode objects using UTF-8 and returns the result as
1103 Python string object. Error handling is ``strict''. Returns
1104 \NULL{} if an exception was raised by the codec.
1105\end{cfuncdesc}
1106
1107% --- UTF-16 Codecs ------------------------------------------------------ */
1108
1109These are the UTF-16 codec APIs:
1110
1111\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF16}{const char *s,
1112 int size,
1113 const char *errors,
1114 int *byteorder}
1115 Decodes \var{length} bytes from a UTF-16 encoded buffer string and
1116 returns the corresponding Unicode object. \var{errors} (if
Tim Peters9ddf40b2004-06-20 22:41:32 +00001117 non-\NULL{}) defines the error handling. It defaults to ``strict''.
Fred Drake3adf79e2001-10-12 19:01:43 +00001118
Tim Peters9ddf40b2004-06-20 22:41:32 +00001119 If \var{byteorder} is non-\NULL{}, the decoder starts decoding using
Fred Drake3adf79e2001-10-12 19:01:43 +00001120 the given byte order:
1121
1122\begin{verbatim}
1123 *byteorder == -1: little endian
1124 *byteorder == 0: native order
1125 *byteorder == 1: big endian
1126\end{verbatim}
1127
1128 and then switches according to all byte order marks (BOM) it finds
1129 in the input data. BOMs are not copied into the resulting Unicode
1130 string. After completion, \var{*byteorder} is set to the current
1131 byte order at the end of input data.
1132
Tim Peters9ddf40b2004-06-20 22:41:32 +00001133 If \var{byteorder} is \NULL{}, the codec starts in native order mode.
Fred Drake3adf79e2001-10-12 19:01:43 +00001134
1135 Returns \NULL{} if an exception was raised by the codec.
1136\end{cfuncdesc}
1137
1138\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUTF16}{const Py_UNICODE *s,
1139 int size,
1140 const char *errors,
1141 int byteorder}
1142 Returns a Python string object holding the UTF-16 encoded value of
1143 the Unicode data in \var{s}. If \var{byteorder} is not \code{0},
1144 output is written according to the following byte order:
1145
1146\begin{verbatim}
1147 byteorder == -1: little endian
1148 byteorder == 0: native byte order (writes a BOM mark)
1149 byteorder == 1: big endian
1150\end{verbatim}
1151
1152 If byteorder is \code{0}, the output string will always start with
1153 the Unicode BOM mark (U+FEFF). In the other two modes, no BOM mark
1154 is prepended.
1155
Martin v. Löwis9bc4f2d2004-06-03 09:55:28 +00001156 If \var{Py_UNICODE_WIDE} is defined, a single \ctype{Py_UNICODE}
1157 value may get represented as a surrogate pair. If it is not
1158 defined, each \ctype{Py_UNICODE} values is interpreted as an
1159 UCS-2 character.
Fred Drake3adf79e2001-10-12 19:01:43 +00001160
1161 Returns \NULL{} if an exception was raised by the codec.
1162\end{cfuncdesc}
1163
1164\begin{cfuncdesc}{PyObject*}{PyUnicode_AsUTF16String}{PyObject *unicode}
1165 Returns a Python string using the UTF-16 encoding in native byte
1166 order. The string always starts with a BOM mark. Error handling is
1167 ``strict''. Returns \NULL{} if an exception was raised by the
1168 codec.
1169\end{cfuncdesc}
1170
1171% --- Unicode-Escape Codecs ----------------------------------------------
1172
Martin v. Löwis95cf84a2003-10-19 07:32:24 +00001173These are the ``Unicode Escape'' codec APIs:
Fred Drake3adf79e2001-10-12 19:01:43 +00001174
1175\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUnicodeEscape}{const char *s,
1176 int size,
1177 const char *errors}
1178 Creates a Unicode object by decoding \var{size} bytes of the
1179 Unicode-Escape encoded string \var{s}. Returns \NULL{} if an
1180 exception was raised by the codec.
1181\end{cfuncdesc}
1182
1183\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUnicodeEscape}{const Py_UNICODE *s,
1184 int size,
1185 const char *errors}
1186 Encodes the \ctype{Py_UNICODE} buffer of the given size using
1187 Unicode-Escape and returns a Python string object. Returns \NULL{}
1188 if an exception was raised by the codec.
1189\end{cfuncdesc}
1190
1191\begin{cfuncdesc}{PyObject*}{PyUnicode_AsUnicodeEscapeString}{PyObject *unicode}
1192 Encodes a Unicode objects using Unicode-Escape and returns the
1193 result as Python string object. Error handling is ``strict''.
1194 Returns \NULL{} if an exception was raised by the codec.
1195\end{cfuncdesc}
1196
1197% --- Raw-Unicode-Escape Codecs ------------------------------------------
1198
Martin v. Löwis95cf84a2003-10-19 07:32:24 +00001199These are the ``Raw Unicode Escape'' codec APIs:
Fred Drake3adf79e2001-10-12 19:01:43 +00001200
1201\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeRawUnicodeEscape}{const char *s,
1202 int size,
1203 const char *errors}
1204 Creates a Unicode object by decoding \var{size} bytes of the
Martin v. Löwis95cf84a2003-10-19 07:32:24 +00001205 Raw-Unicode-Escape encoded string \var{s}. Returns \NULL{} if an
Fred Drake3adf79e2001-10-12 19:01:43 +00001206 exception was raised by the codec.
1207\end{cfuncdesc}
1208
1209\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeRawUnicodeEscape}{const Py_UNICODE *s,
1210 int size,
1211 const char *errors}
1212 Encodes the \ctype{Py_UNICODE} buffer of the given size using
1213 Raw-Unicode-Escape and returns a Python string object. Returns
1214 \NULL{} if an exception was raised by the codec.
1215\end{cfuncdesc}
1216
1217\begin{cfuncdesc}{PyObject*}{PyUnicode_AsRawUnicodeEscapeString}{PyObject *unicode}
1218 Encodes a Unicode objects using Raw-Unicode-Escape and returns the
1219 result as Python string object. Error handling is ``strict''.
1220 Returns \NULL{} if an exception was raised by the codec.
1221\end{cfuncdesc}
1222
Tim Petersf582b822001-12-11 18:51:08 +00001223% --- Latin-1 Codecs -----------------------------------------------------
Fred Drake3adf79e2001-10-12 19:01:43 +00001224
1225These are the Latin-1 codec APIs:
1226Latin-1 corresponds to the first 256 Unicode ordinals and only these
1227are accepted by the codecs during encoding.
1228
1229\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeLatin1}{const char *s,
1230 int size,
1231 const char *errors}
1232 Creates a Unicode object by decoding \var{size} bytes of the Latin-1
1233 encoded string \var{s}. Returns \NULL{} if an exception was raised
1234 by the codec.
1235\end{cfuncdesc}
1236
1237\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeLatin1}{const Py_UNICODE *s,
1238 int size,
1239 const char *errors}
1240 Encodes the \ctype{Py_UNICODE} buffer of the given size using
1241 Latin-1 and returns a Python string object. Returns \NULL{} if an
1242 exception was raised by the codec.
1243\end{cfuncdesc}
1244
1245\begin{cfuncdesc}{PyObject*}{PyUnicode_AsLatin1String}{PyObject *unicode}
1246 Encodes a Unicode objects using Latin-1 and returns the result as
1247 Python string object. Error handling is ``strict''. Returns
1248 \NULL{} if an exception was raised by the codec.
1249\end{cfuncdesc}
1250
Tim Petersf582b822001-12-11 18:51:08 +00001251% --- ASCII Codecs -------------------------------------------------------
Fred Drake3adf79e2001-10-12 19:01:43 +00001252
1253These are the \ASCII{} codec APIs. Only 7-bit \ASCII{} data is
1254accepted. All other codes generate errors.
1255
1256\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeASCII}{const char *s,
1257 int size,
1258 const char *errors}
1259 Creates a Unicode object by decoding \var{size} bytes of the
1260 \ASCII{} encoded string \var{s}. Returns \NULL{} if an exception
1261 was raised by the codec.
1262\end{cfuncdesc}
1263
1264\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeASCII}{const Py_UNICODE *s,
1265 int size,
1266 const char *errors}
1267 Encodes the \ctype{Py_UNICODE} buffer of the given size using
1268 \ASCII{} and returns a Python string object. Returns \NULL{} if an
1269 exception was raised by the codec.
1270\end{cfuncdesc}
1271
1272\begin{cfuncdesc}{PyObject*}{PyUnicode_AsASCIIString}{PyObject *unicode}
1273 Encodes a Unicode objects using \ASCII{} and returns the result as
1274 Python string object. Error handling is ``strict''. Returns
1275 \NULL{} if an exception was raised by the codec.
1276\end{cfuncdesc}
1277
Tim Petersf582b822001-12-11 18:51:08 +00001278% --- Character Map Codecs -----------------------------------------------
Fred Drake3adf79e2001-10-12 19:01:43 +00001279
1280These are the mapping codec APIs:
1281
1282This codec is special in that it can be used to implement many
1283different codecs (and this is in fact what was done to obtain most of
1284the standard codecs included in the \module{encodings} package). The
1285codec uses mapping to encode and decode characters.
1286
1287Decoding mappings must map single string characters to single Unicode
1288characters, integers (which are then interpreted as Unicode ordinals)
Tim Petersf582b822001-12-11 18:51:08 +00001289or None (meaning "undefined mapping" and causing an error).
Fred Drake3adf79e2001-10-12 19:01:43 +00001290
1291Encoding mappings must map single Unicode characters to single string
1292characters, integers (which are then interpreted as Latin-1 ordinals)
1293or None (meaning "undefined mapping" and causing an error).
1294
1295The mapping objects provided must only support the __getitem__ mapping
1296interface.
1297
1298If a character lookup fails with a LookupError, the character is
1299copied as-is meaning that its ordinal value will be interpreted as
1300Unicode or Latin-1 ordinal resp. Because of this, mappings only need
1301to contain those mappings which map characters to different code
1302points.
1303
1304\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeCharmap}{const char *s,
1305 int size,
1306 PyObject *mapping,
1307 const char *errors}
1308 Creates a Unicode object by decoding \var{size} bytes of the encoded
1309 string \var{s} using the given \var{mapping} object. Returns
1310 \NULL{} if an exception was raised by the codec.
1311\end{cfuncdesc}
1312
1313\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeCharmap}{const Py_UNICODE *s,
1314 int size,
1315 PyObject *mapping,
1316 const char *errors}
1317 Encodes the \ctype{Py_UNICODE} buffer of the given size using the
1318 given \var{mapping} object and returns a Python string object.
1319 Returns \NULL{} if an exception was raised by the codec.
1320\end{cfuncdesc}
1321
1322\begin{cfuncdesc}{PyObject*}{PyUnicode_AsCharmapString}{PyObject *unicode,
1323 PyObject *mapping}
1324 Encodes a Unicode objects using the given \var{mapping} object and
1325 returns the result as Python string object. Error handling is
1326 ``strict''. Returns \NULL{} if an exception was raised by the
1327 codec.
1328\end{cfuncdesc}
1329
1330The following codec API is special in that maps Unicode to Unicode.
1331
1332\begin{cfuncdesc}{PyObject*}{PyUnicode_TranslateCharmap}{const Py_UNICODE *s,
1333 int size,
1334 PyObject *table,
1335 const char *errors}
1336 Translates a \ctype{Py_UNICODE} buffer of the given length by
1337 applying a character mapping \var{table} to it and returns the
1338 resulting Unicode object. Returns \NULL{} when an exception was
1339 raised by the codec.
1340
1341 The \var{mapping} table must map Unicode ordinal integers to Unicode
1342 ordinal integers or None (causing deletion of the character).
1343
1344 Mapping tables need only provide the method{__getitem__()}
1345 interface; dictionaries and sequences work well. Unmapped character
1346 ordinals (ones which cause a \exception{LookupError}) are left
1347 untouched and are copied as-is.
1348\end{cfuncdesc}
1349
1350% --- MBCS codecs for Windows --------------------------------------------
1351
1352These are the MBCS codec APIs. They are currently only available on
1353Windows and use the Win32 MBCS converters to implement the
1354conversions. Note that MBCS (or DBCS) is a class of encodings, not
1355just one. The target encoding is defined by the user settings on the
1356machine running the codec.
1357
1358\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeMBCS}{const char *s,
1359 int size,
1360 const char *errors}
1361 Creates a Unicode object by decoding \var{size} bytes of the MBCS
1362 encoded string \var{s}. Returns \NULL{} if an exception was
1363 raised by the codec.
1364\end{cfuncdesc}
1365
1366\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeMBCS}{const Py_UNICODE *s,
1367 int size,
1368 const char *errors}
1369 Encodes the \ctype{Py_UNICODE} buffer of the given size using MBCS
1370 and returns a Python string object. Returns \NULL{} if an exception
1371 was raised by the codec.
1372\end{cfuncdesc}
1373
1374\begin{cfuncdesc}{PyObject*}{PyUnicode_AsMBCSString}{PyObject *unicode}
1375 Encodes a Unicode objects using MBCS and returns the result as
1376 Python string object. Error handling is ``strict''. Returns
1377 \NULL{} if an exception was raised by the codec.
1378\end{cfuncdesc}
1379
1380% --- Methods & Slots ----------------------------------------------------
1381
1382\subsubsection{Methods and Slot Functions \label{unicodeMethodsAndSlots}}
1383
1384The following APIs are capable of handling Unicode objects and strings
1385on input (we refer to them as strings in the descriptions) and return
Martin v. Löwis95cf84a2003-10-19 07:32:24 +00001386Unicode objects or integers as appropriate.
Fred Drake3adf79e2001-10-12 19:01:43 +00001387
1388They all return \NULL{} or \code{-1} if an exception occurs.
1389
1390\begin{cfuncdesc}{PyObject*}{PyUnicode_Concat}{PyObject *left,
1391 PyObject *right}
1392 Concat two strings giving a new Unicode string.
1393\end{cfuncdesc}
1394
1395\begin{cfuncdesc}{PyObject*}{PyUnicode_Split}{PyObject *s,
1396 PyObject *sep,
1397 int maxsplit}
Tim Peters9ddf40b2004-06-20 22:41:32 +00001398 Split a string giving a list of Unicode strings. If sep is \NULL{},
Fred Drake3adf79e2001-10-12 19:01:43 +00001399 splitting will be done at all whitespace substrings. Otherwise,
1400 splits occur at the given separator. At most \var{maxsplit} splits
1401 will be done. If negative, no limit is set. Separators are not
1402 included in the resulting list.
1403\end{cfuncdesc}
1404
1405\begin{cfuncdesc}{PyObject*}{PyUnicode_Splitlines}{PyObject *s,
Martin v. Löwis24b88812003-03-30 16:40:42 +00001406 int keepend}
Fred Drake3adf79e2001-10-12 19:01:43 +00001407 Split a Unicode string at line breaks, returning a list of Unicode
Martin v. Löwis24b88812003-03-30 16:40:42 +00001408 strings. CRLF is considered to be one line break. If \var{keepend}
1409 is 0, the Line break characters are not included in the resulting
1410 strings.
Fred Drake3adf79e2001-10-12 19:01:43 +00001411\end{cfuncdesc}
1412
1413\begin{cfuncdesc}{PyObject*}{PyUnicode_Translate}{PyObject *str,
1414 PyObject *table,
1415 const char *errors}
1416 Translate a string by applying a character mapping table to it and
1417 return the resulting Unicode object.
1418
1419 The mapping table must map Unicode ordinal integers to Unicode
1420 ordinal integers or None (causing deletion of the character).
1421
1422 Mapping tables need only provide the \method{__getitem__()}
1423 interface; dictionaries and sequences work well. Unmapped character
1424 ordinals (ones which cause a \exception{LookupError}) are left
1425 untouched and are copied as-is.
1426
1427 \var{errors} has the usual meaning for codecs. It may be \NULL{}
1428 which indicates to use the default error handling.
1429\end{cfuncdesc}
1430
1431\begin{cfuncdesc}{PyObject*}{PyUnicode_Join}{PyObject *separator,
1432 PyObject *seq}
1433 Join a sequence of strings using the given separator and return the
1434 resulting Unicode string.
1435\end{cfuncdesc}
1436
1437\begin{cfuncdesc}{PyObject*}{PyUnicode_Tailmatch}{PyObject *str,
1438 PyObject *substr,
1439 int start,
1440 int end,
1441 int direction}
1442 Return 1 if \var{substr} matches \var{str}[\var{start}:\var{end}] at
1443 the given tail end (\var{direction} == -1 means to do a prefix
1444 match, \var{direction} == 1 a suffix match), 0 otherwise.
1445\end{cfuncdesc}
1446
Fred Drake1d1e1db2002-06-20 22:07:04 +00001447\begin{cfuncdesc}{int}{PyUnicode_Find}{PyObject *str,
1448 PyObject *substr,
1449 int start,
1450 int end,
1451 int direction}
Fred Drake3adf79e2001-10-12 19:01:43 +00001452 Return the first position of \var{substr} in
1453 \var{str}[\var{start}:\var{end}] using the given \var{direction}
1454 (\var{direction} == 1 means to do a forward search,
Fred Drake1d1e1db2002-06-20 22:07:04 +00001455 \var{direction} == -1 a backward search). The return value is the
1456 index of the first match; a value of \code{-1} indicates that no
1457 match was found, and \code{-2} indicates that an error occurred and
1458 an exception has been set.
Fred Drake3adf79e2001-10-12 19:01:43 +00001459\end{cfuncdesc}
1460
Fred Drake1d1e1db2002-06-20 22:07:04 +00001461\begin{cfuncdesc}{int}{PyUnicode_Count}{PyObject *str,
1462 PyObject *substr,
1463 int start,
1464 int end}
1465 Return the number of non-overlapping occurrences of \var{substr} in
1466 \code{\var{str}[\var{start}:\var{end}]}. Returns \code{-1} if an
1467 error occurred.
Fred Drake3adf79e2001-10-12 19:01:43 +00001468\end{cfuncdesc}
1469
1470\begin{cfuncdesc}{PyObject*}{PyUnicode_Replace}{PyObject *str,
1471 PyObject *substr,
1472 PyObject *replstr,
1473 int maxcount}
1474 Replace at most \var{maxcount} occurrences of \var{substr} in
1475 \var{str} with \var{replstr} and return the resulting Unicode object.
1476 \var{maxcount} == -1 means replace all occurrences.
1477\end{cfuncdesc}
1478
1479\begin{cfuncdesc}{int}{PyUnicode_Compare}{PyObject *left, PyObject *right}
1480 Compare two strings and return -1, 0, 1 for less than, equal, and
1481 greater than, respectively.
1482\end{cfuncdesc}
1483
1484\begin{cfuncdesc}{PyObject*}{PyUnicode_Format}{PyObject *format,
1485 PyObject *args}
1486 Returns a new string object from \var{format} and \var{args}; this
1487 is analogous to \code{\var{format} \%\ \var{args}}. The
1488 \var{args} argument must be a tuple.
1489\end{cfuncdesc}
1490
1491\begin{cfuncdesc}{int}{PyUnicode_Contains}{PyObject *container,
1492 PyObject *element}
1493 Checks whether \var{element} is contained in \var{container} and
1494 returns true or false accordingly.
1495
1496 \var{element} has to coerce to a one element Unicode
1497 string. \code{-1} is returned if there was an error.
1498\end{cfuncdesc}
1499
1500
1501\subsection{Buffer Objects \label{bufferObjects}}
1502\sectionauthor{Greg Stein}{gstein@lyra.org}
1503
1504\obindex{buffer}
1505Python objects implemented in C can export a group of functions called
1506the ``buffer\index{buffer interface} interface.'' These functions can
1507be used by an object to expose its data in a raw, byte-oriented
1508format. Clients of the object can use the buffer interface to access
1509the object data directly, without needing to copy it first.
1510
Tim Petersf582b822001-12-11 18:51:08 +00001511Two examples of objects that support
1512the buffer interface are strings and arrays. The string object exposes
Fred Drake3adf79e2001-10-12 19:01:43 +00001513the character contents in the buffer interface's byte-oriented
1514form. An array can also expose its contents, but it should be noted
1515that array elements may be multi-byte values.
1516
1517An example user of the buffer interface is the file object's
1518\method{write()} method. Any object that can export a series of bytes
1519through the buffer interface can be written to a file. There are a
Tim Petersf582b822001-12-11 18:51:08 +00001520number of format codes to \cfunction{PyArg_ParseTuple()} that operate
Fred Drake3adf79e2001-10-12 19:01:43 +00001521against an object's buffer interface, returning data from the target
1522object.
1523
1524More information on the buffer interface is provided in the section
Fred Drake54e62942001-12-11 19:40:16 +00001525``Buffer Object Structures'' (section~\ref{buffer-structs}), under
Fred Drake3adf79e2001-10-12 19:01:43 +00001526the description for \ctype{PyBufferProcs}\ttindex{PyBufferProcs}.
1527
1528A ``buffer object'' is defined in the \file{bufferobject.h} header
1529(included by \file{Python.h}). These objects look very similar to
1530string objects at the Python programming level: they support slicing,
1531indexing, concatenation, and some other standard string
1532operations. However, their data can come from one of two sources: from
1533a block of memory, or from another object which exports the buffer
1534interface.
1535
1536Buffer objects are useful as a way to expose the data from another
1537object's buffer interface to the Python programmer. They can also be
1538used as a zero-copy slicing mechanism. Using their ability to
1539reference a block of memory, it is possible to expose any data to the
1540Python programmer quite easily. The memory could be a large, constant
1541array in a C extension, it could be a raw block of memory for
1542manipulation before passing to an operating system library, or it
1543could be used to pass around structured data in its native, in-memory
1544format.
1545
1546\begin{ctypedesc}{PyBufferObject}
1547 This subtype of \ctype{PyObject} represents a buffer object.
1548\end{ctypedesc}
1549
1550\begin{cvardesc}{PyTypeObject}{PyBuffer_Type}
1551 The instance of \ctype{PyTypeObject} which represents the Python
1552 buffer type; it is the same object as \code{types.BufferType} in the
1553 Python layer.\withsubitem{(in module types)}{\ttindex{BufferType}}.
1554\end{cvardesc}
1555
1556\begin{cvardesc}{int}{Py_END_OF_BUFFER}
1557 This constant may be passed as the \var{size} parameter to
1558 \cfunction{PyBuffer_FromObject()} or
1559 \cfunction{PyBuffer_FromReadWriteObject()}. It indicates that the
1560 new \ctype{PyBufferObject} should refer to \var{base} object from
1561 the specified \var{offset} to the end of its exported buffer. Using
1562 this enables the caller to avoid querying the \var{base} object for
1563 its length.
1564\end{cvardesc}
1565
1566\begin{cfuncdesc}{int}{PyBuffer_Check}{PyObject *p}
1567 Return true if the argument has type \cdata{PyBuffer_Type}.
1568\end{cfuncdesc}
1569
1570\begin{cfuncdesc}{PyObject*}{PyBuffer_FromObject}{PyObject *base,
1571 int offset, int size}
1572 Return a new read-only buffer object. This raises
1573 \exception{TypeError} if \var{base} doesn't support the read-only
1574 buffer protocol or doesn't provide exactly one buffer segment, or it
1575 raises \exception{ValueError} if \var{offset} is less than zero. The
1576 buffer will hold a reference to the \var{base} object, and the
1577 buffer's contents will refer to the \var{base} object's buffer
1578 interface, starting as position \var{offset} and extending for
1579 \var{size} bytes. If \var{size} is \constant{Py_END_OF_BUFFER}, then
1580 the new buffer's contents extend to the length of the \var{base}
1581 object's exported buffer data.
1582\end{cfuncdesc}
1583
1584\begin{cfuncdesc}{PyObject*}{PyBuffer_FromReadWriteObject}{PyObject *base,
1585 int offset,
1586 int size}
1587 Return a new writable buffer object. Parameters and exceptions are
1588 similar to those for \cfunction{PyBuffer_FromObject()}. If the
1589 \var{base} object does not export the writeable buffer protocol,
1590 then \exception{TypeError} is raised.
1591\end{cfuncdesc}
1592
1593\begin{cfuncdesc}{PyObject*}{PyBuffer_FromMemory}{void *ptr, int size}
1594 Return a new read-only buffer object that reads from a specified
1595 location in memory, with a specified size. The caller is
1596 responsible for ensuring that the memory buffer, passed in as
1597 \var{ptr}, is not deallocated while the returned buffer object
1598 exists. Raises \exception{ValueError} if \var{size} is less than
1599 zero. Note that \constant{Py_END_OF_BUFFER} may \emph{not} be
1600 passed for the \var{size} parameter; \exception{ValueError} will be
1601 raised in that case.
1602\end{cfuncdesc}
1603
1604\begin{cfuncdesc}{PyObject*}{PyBuffer_FromReadWriteMemory}{void *ptr, int size}
1605 Similar to \cfunction{PyBuffer_FromMemory()}, but the returned
1606 buffer is writable.
1607\end{cfuncdesc}
1608
1609\begin{cfuncdesc}{PyObject*}{PyBuffer_New}{int size}
1610 Returns a new writable buffer object that maintains its own memory
1611 buffer of \var{size} bytes. \exception{ValueError} is returned if
Neil Schemenauerd68d3ee2004-06-08 02:58:50 +00001612 \var{size} is not zero or positive. Note that the memory buffer (as
1613 returned by \cfunction{PyObject_AsWriteBuffer()}) is not specifically
1614 aligned.
Fred Drake3adf79e2001-10-12 19:01:43 +00001615\end{cfuncdesc}
1616
1617
1618\subsection{Tuple Objects \label{tupleObjects}}
1619
1620\obindex{tuple}
1621\begin{ctypedesc}{PyTupleObject}
1622 This subtype of \ctype{PyObject} represents a Python tuple object.
1623\end{ctypedesc}
1624
1625\begin{cvardesc}{PyTypeObject}{PyTuple_Type}
1626 This instance of \ctype{PyTypeObject} represents the Python tuple
1627 type; it is the same object as \code{types.TupleType} in the Python
1628 layer.\withsubitem{(in module types)}{\ttindex{TupleType}}.
1629\end{cvardesc}
1630
1631\begin{cfuncdesc}{int}{PyTuple_Check}{PyObject *p}
1632 Return true if \var{p} is a tuple object or an instance of a subtype
1633 of the tuple type.
1634 \versionchanged[Allowed subtypes to be accepted]{2.2}
1635\end{cfuncdesc}
1636
1637\begin{cfuncdesc}{int}{PyTuple_CheckExact}{PyObject *p}
1638 Return true if \var{p} is a tuple object, but not an instance of a
1639 subtype of the tuple type.
1640 \versionadded{2.2}
1641\end{cfuncdesc}
1642
1643\begin{cfuncdesc}{PyObject*}{PyTuple_New}{int len}
1644 Return a new tuple object of size \var{len}, or \NULL{} on failure.
1645\end{cfuncdesc}
1646
Raymond Hettingercb2da432003-10-12 18:24:34 +00001647\begin{cfuncdesc}{PyObject*}{PyTuple_Pack}{int n, \moreargs}
1648 Return a new tuple object of size \var{n}, or \NULL{} on failure.
1649 The tuple values are initialized to the subsequent \var{n} C arguments
1650 pointing to Python objects. \samp{PyTuple_Pack(2, \var{a}, \var{b})}
1651 is equivalent to \samp{Py_BuildValue("(OO)", \var{a}, \var{b})}.
Tim Peters9ddf40b2004-06-20 22:41:32 +00001652 \versionadded{2.4}
Raymond Hettingercb2da432003-10-12 18:24:34 +00001653\end{cfuncdesc}
1654
Fred Drake3adf79e2001-10-12 19:01:43 +00001655\begin{cfuncdesc}{int}{PyTuple_Size}{PyObject *p}
1656 Takes a pointer to a tuple object, and returns the size of that
1657 tuple.
1658\end{cfuncdesc}
1659
1660\begin{cfuncdesc}{int}{PyTuple_GET_SIZE}{PyObject *p}
1661 Return the size of the tuple \var{p}, which must be non-\NULL{} and
1662 point to a tuple; no error checking is performed.
1663\end{cfuncdesc}
1664
1665\begin{cfuncdesc}{PyObject*}{PyTuple_GetItem}{PyObject *p, int pos}
1666 Returns the object at position \var{pos} in the tuple pointed to by
1667 \var{p}. If \var{pos} is out of bounds, returns \NULL{} and sets an
1668 \exception{IndexError} exception.
1669\end{cfuncdesc}
1670
1671\begin{cfuncdesc}{PyObject*}{PyTuple_GET_ITEM}{PyObject *p, int pos}
1672 Like \cfunction{PyTuple_GetItem()}, but does no checking of its
1673 arguments.
1674\end{cfuncdesc}
1675
1676\begin{cfuncdesc}{PyObject*}{PyTuple_GetSlice}{PyObject *p,
1677 int low, int high}
1678 Takes a slice of the tuple pointed to by \var{p} from \var{low} to
1679 \var{high} and returns it as a new tuple.
1680\end{cfuncdesc}
1681
1682\begin{cfuncdesc}{int}{PyTuple_SetItem}{PyObject *p,
1683 int pos, PyObject *o}
1684 Inserts a reference to object \var{o} at position \var{pos} of the
1685 tuple pointed to by \var{p}. It returns \code{0} on success.
1686 \note{This function ``steals'' a reference to \var{o}.}
1687\end{cfuncdesc}
1688
1689\begin{cfuncdesc}{void}{PyTuple_SET_ITEM}{PyObject *p,
1690 int pos, PyObject *o}
1691 Like \cfunction{PyTuple_SetItem()}, but does no error checking, and
1692 should \emph{only} be used to fill in brand new tuples. \note{This
1693 function ``steals'' a reference to \var{o}.}
1694\end{cfuncdesc}
1695
1696\begin{cfuncdesc}{int}{_PyTuple_Resize}{PyObject **p, int newsize}
1697 Can be used to resize a tuple. \var{newsize} will be the new length
1698 of the tuple. Because tuples are \emph{supposed} to be immutable,
1699 this should only be used if there is only one reference to the
1700 object. Do \emph{not} use this if the tuple may already be known to
1701 some other part of the code. The tuple will always grow or shrink
1702 at the end. Think of this as destroying the old tuple and creating
1703 a new one, only more efficiently. Returns \code{0} on success.
1704 Client code should never assume that the resulting value of
1705 \code{*\var{p}} will be the same as before calling this function.
1706 If the object referenced by \code{*\var{p}} is replaced, the
1707 original \code{*\var{p}} is destroyed. On failure, returns
Tim Peters9ddf40b2004-06-20 22:41:32 +00001708 \code{-1} and sets \code{*\var{p}} to \NULL{}, and raises
Fred Drake3adf79e2001-10-12 19:01:43 +00001709 \exception{MemoryError} or
1710 \exception{SystemError}.
Tim Petersf582b822001-12-11 18:51:08 +00001711 \versionchanged[Removed unused third parameter, \var{last_is_sticky}]{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +00001712\end{cfuncdesc}
1713
1714
1715\subsection{List Objects \label{listObjects}}
1716
1717\obindex{list}
1718\begin{ctypedesc}{PyListObject}
1719 This subtype of \ctype{PyObject} represents a Python list object.
1720\end{ctypedesc}
1721
1722\begin{cvardesc}{PyTypeObject}{PyList_Type}
1723 This instance of \ctype{PyTypeObject} represents the Python list
1724 type. This is the same object as \code{types.ListType}.
1725 \withsubitem{(in module types)}{\ttindex{ListType}}
1726\end{cvardesc}
1727
1728\begin{cfuncdesc}{int}{PyList_Check}{PyObject *p}
Andrew MacIntyre13cd8892003-12-25 23:57:52 +00001729 Returns true if \var{p} is a list object or an instance of a
1730 subtype of the list type.
1731 \versionchanged[Allowed subtypes to be accepted]{2.2}
1732\end{cfuncdesc}
1733
1734\begin{cfuncdesc}{int}{PyList_CheckExact}{PyObject *p}
1735 Return true if \var{p} is a list object, but not an instance of a
1736 subtype of the list type.
1737 \versionadded{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +00001738\end{cfuncdesc}
1739
1740\begin{cfuncdesc}{PyObject*}{PyList_New}{int len}
1741 Returns a new list of length \var{len} on success, or \NULL{} on
1742 failure.
1743\end{cfuncdesc}
1744
1745\begin{cfuncdesc}{int}{PyList_Size}{PyObject *list}
1746 Returns the length of the list object in \var{list}; this is
1747 equivalent to \samp{len(\var{list})} on a list object.
1748 \bifuncindex{len}
1749\end{cfuncdesc}
1750
1751\begin{cfuncdesc}{int}{PyList_GET_SIZE}{PyObject *list}
1752 Macro form of \cfunction{PyList_Size()} without error checking.
1753\end{cfuncdesc}
1754
1755\begin{cfuncdesc}{PyObject*}{PyList_GetItem}{PyObject *list, int index}
1756 Returns the object at position \var{pos} in the list pointed to by
1757 \var{p}. If \var{pos} is out of bounds, returns \NULL{} and sets an
1758 \exception{IndexError} exception.
1759\end{cfuncdesc}
1760
1761\begin{cfuncdesc}{PyObject*}{PyList_GET_ITEM}{PyObject *list, int i}
1762 Macro form of \cfunction{PyList_GetItem()} without error checking.
1763\end{cfuncdesc}
1764
1765\begin{cfuncdesc}{int}{PyList_SetItem}{PyObject *list, int index,
1766 PyObject *item}
1767 Sets the item at index \var{index} in list to \var{item}. Returns
1768 \code{0} on success or \code{-1} on failure. \note{This function
1769 ``steals'' a reference to \var{item} and discards a reference to an
1770 item already in the list at the affected position.}
1771\end{cfuncdesc}
1772
1773\begin{cfuncdesc}{void}{PyList_SET_ITEM}{PyObject *list, int i,
1774 PyObject *o}
1775 Macro form of \cfunction{PyList_SetItem()} without error checking.
1776 This is normally only used to fill in new lists where there is no
1777 previous content.
1778 \note{This function ``steals'' a reference to \var{item}, and,
1779 unlike \cfunction{PyList_SetItem()}, does \emph{not} discard a
1780 reference to any item that it being replaced; any reference in
1781 \var{list} at position \var{i} will be leaked.}
1782\end{cfuncdesc}
1783
1784\begin{cfuncdesc}{int}{PyList_Insert}{PyObject *list, int index,
1785 PyObject *item}
1786 Inserts the item \var{item} into list \var{list} in front of index
1787 \var{index}. Returns \code{0} if successful; returns \code{-1} and
1788 raises an exception if unsuccessful. Analogous to
1789 \code{\var{list}.insert(\var{index}, \var{item})}.
1790\end{cfuncdesc}
1791
1792\begin{cfuncdesc}{int}{PyList_Append}{PyObject *list, PyObject *item}
1793 Appends the object \var{item} at the end of list \var{list}.
1794 Returns \code{0} if successful; returns \code{-1} and sets an
1795 exception if unsuccessful. Analogous to
1796 \code{\var{list}.append(\var{item})}.
1797\end{cfuncdesc}
1798
1799\begin{cfuncdesc}{PyObject*}{PyList_GetSlice}{PyObject *list,
1800 int low, int high}
1801 Returns a list of the objects in \var{list} containing the objects
1802 \emph{between} \var{low} and \var{high}. Returns \NULL{} and sets
1803 an exception if unsuccessful.
1804 Analogous to \code{\var{list}[\var{low}:\var{high}]}.
1805\end{cfuncdesc}
1806
1807\begin{cfuncdesc}{int}{PyList_SetSlice}{PyObject *list,
1808 int low, int high,
1809 PyObject *itemlist}
1810 Sets the slice of \var{list} between \var{low} and \var{high} to the
1811 contents of \var{itemlist}. Analogous to
Raymond Hettinger9c7ed4c2003-10-26 17:20:07 +00001812 \code{\var{list}[\var{low}:\var{high}] = \var{itemlist}}.
1813 The \var{itemlist} may be \NULL{}, indicating the assignment
1814 of an empty list (slice deletion).
1815 Returns \code{0} on success, \code{-1} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00001816\end{cfuncdesc}
1817
1818\begin{cfuncdesc}{int}{PyList_Sort}{PyObject *list}
1819 Sorts the items of \var{list} in place. Returns \code{0} on
1820 success, \code{-1} on failure. This is equivalent to
1821 \samp{\var{list}.sort()}.
1822\end{cfuncdesc}
1823
1824\begin{cfuncdesc}{int}{PyList_Reverse}{PyObject *list}
1825 Reverses the items of \var{list} in place. Returns \code{0} on
1826 success, \code{-1} on failure. This is the equivalent of
1827 \samp{\var{list}.reverse()}.
1828\end{cfuncdesc}
1829
1830\begin{cfuncdesc}{PyObject*}{PyList_AsTuple}{PyObject *list}
1831 Returns a new tuple object containing the contents of \var{list};
1832 equivalent to \samp{tuple(\var{list})}.\bifuncindex{tuple}
1833\end{cfuncdesc}
1834
1835
1836\section{Mapping Objects \label{mapObjects}}
1837
1838\obindex{mapping}
1839
1840
1841\subsection{Dictionary Objects \label{dictObjects}}
1842
1843\obindex{dictionary}
1844\begin{ctypedesc}{PyDictObject}
1845 This subtype of \ctype{PyObject} represents a Python dictionary
1846 object.
1847\end{ctypedesc}
1848
1849\begin{cvardesc}{PyTypeObject}{PyDict_Type}
1850 This instance of \ctype{PyTypeObject} represents the Python
1851 dictionary type. This is exposed to Python programs as
1852 \code{types.DictType} and \code{types.DictionaryType}.
1853 \withsubitem{(in module types)}{\ttindex{DictType}\ttindex{DictionaryType}}
1854\end{cvardesc}
1855
1856\begin{cfuncdesc}{int}{PyDict_Check}{PyObject *p}
Andrew MacIntyre13cd8892003-12-25 23:57:52 +00001857 Returns true if \var{p} is a dict object or an instance of a
1858 subtype of the dict type.
1859 \versionchanged[Allowed subtypes to be accepted]{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +00001860\end{cfuncdesc}
1861
Andrew MacIntyref72af652003-12-26 00:07:51 +00001862\begin{cfuncdesc}{int}{PyDict_CheckExact}{PyObject *p}
1863 Return true if \var{p} is a dict object, but not an instance of a
1864 subtype of the dict type.
1865 \versionadded{2.4}
1866\end{cfuncdesc}
1867
Fred Drake3adf79e2001-10-12 19:01:43 +00001868\begin{cfuncdesc}{PyObject*}{PyDict_New}{}
1869 Returns a new empty dictionary, or \NULL{} on failure.
1870\end{cfuncdesc}
1871
1872\begin{cfuncdesc}{PyObject*}{PyDictProxy_New}{PyObject *dict}
1873 Return a proxy object for a mapping which enforces read-only
1874 behavior. This is normally used to create a proxy to prevent
1875 modification of the dictionary for non-dynamic class types.
1876 \versionadded{2.2}
1877\end{cfuncdesc}
1878
1879\begin{cfuncdesc}{void}{PyDict_Clear}{PyObject *p}
1880 Empties an existing dictionary of all key-value pairs.
1881\end{cfuncdesc}
1882
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00001883\begin{cfuncdesc}{int}{PyDict_Contains}{PyObject *p, PyObject *key}
1884 Determine if dictionary \var{p} contains \var{key}. If an item
1885 in \var{p} is matches \var{key}, return \code{1}, otherwise return
1886 \code{0}. On error, return \code{-1}. This is equivalent to the
1887 Python expression \samp{\var{key} in \var{p}}.
Tim Peters9ddf40b2004-06-20 22:41:32 +00001888 \versionadded{2.4}
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00001889\end{cfuncdesc}
1890
Fred Drake3adf79e2001-10-12 19:01:43 +00001891\begin{cfuncdesc}{PyObject*}{PyDict_Copy}{PyObject *p}
1892 Returns a new dictionary that contains the same key-value pairs as
1893 \var{p}.
1894 \versionadded{1.6}
1895\end{cfuncdesc}
1896
1897\begin{cfuncdesc}{int}{PyDict_SetItem}{PyObject *p, PyObject *key,
1898 PyObject *val}
1899 Inserts \var{value} into the dictionary \var{p} with a key of
1900 \var{key}. \var{key} must be hashable; if it isn't,
1901 \exception{TypeError} will be raised.
1902 Returns \code{0} on success or \code{-1} on failure.
1903\end{cfuncdesc}
1904
1905\begin{cfuncdesc}{int}{PyDict_SetItemString}{PyObject *p,
1906 char *key,
1907 PyObject *val}
1908 Inserts \var{value} into the dictionary \var{p} using \var{key} as a
1909 key. \var{key} should be a \ctype{char*}. The key object is created
1910 using \code{PyString_FromString(\var{key})}. Returns \code{0} on
1911 success or \code{-1} on failure.
1912 \ttindex{PyString_FromString()}
1913\end{cfuncdesc}
1914
1915\begin{cfuncdesc}{int}{PyDict_DelItem}{PyObject *p, PyObject *key}
1916 Removes the entry in dictionary \var{p} with key \var{key}.
1917 \var{key} must be hashable; if it isn't, \exception{TypeError} is
Skip Montanaroa23bc422002-01-23 08:18:30 +00001918 raised. Returns \code{0} on success or \code{-1} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00001919\end{cfuncdesc}
1920
1921\begin{cfuncdesc}{int}{PyDict_DelItemString}{PyObject *p, char *key}
1922 Removes the entry in dictionary \var{p} which has a key specified by
1923 the string \var{key}. Returns \code{0} on success or \code{-1} on
1924 failure.
1925\end{cfuncdesc}
1926
1927\begin{cfuncdesc}{PyObject*}{PyDict_GetItem}{PyObject *p, PyObject *key}
1928 Returns the object from dictionary \var{p} which has a key
1929 \var{key}. Returns \NULL{} if the key \var{key} is not present, but
1930 \emph{without} setting an exception.
1931\end{cfuncdesc}
1932
1933\begin{cfuncdesc}{PyObject*}{PyDict_GetItemString}{PyObject *p, char *key}
1934 This is the same as \cfunction{PyDict_GetItem()}, but \var{key} is
1935 specified as a \ctype{char*}, rather than a \ctype{PyObject*}.
1936\end{cfuncdesc}
1937
1938\begin{cfuncdesc}{PyObject*}{PyDict_Items}{PyObject *p}
1939 Returns a \ctype{PyListObject} containing all the items from the
1940 dictionary, as in the dictinoary method \method{items()} (see the
1941 \citetitle[../lib/lib.html]{Python Library Reference}).
1942\end{cfuncdesc}
1943
1944\begin{cfuncdesc}{PyObject*}{PyDict_Keys}{PyObject *p}
1945 Returns a \ctype{PyListObject} containing all the keys from the
1946 dictionary, as in the dictionary method \method{keys()} (see the
1947 \citetitle[../lib/lib.html]{Python Library Reference}).
1948\end{cfuncdesc}
1949
1950\begin{cfuncdesc}{PyObject*}{PyDict_Values}{PyObject *p}
1951 Returns a \ctype{PyListObject} containing all the values from the
1952 dictionary \var{p}, as in the dictionary method \method{values()}
1953 (see the \citetitle[../lib/lib.html]{Python Library Reference}).
1954\end{cfuncdesc}
1955
1956\begin{cfuncdesc}{int}{PyDict_Size}{PyObject *p}
1957 Returns the number of items in the dictionary. This is equivalent
1958 to \samp{len(\var{p})} on a dictionary.\bifuncindex{len}
1959\end{cfuncdesc}
1960
1961\begin{cfuncdesc}{int}{PyDict_Next}{PyObject *p, int *ppos,
1962 PyObject **pkey, PyObject **pvalue}
1963 Iterate over all key-value pairs in the dictionary \var{p}. The
1964 \ctype{int} referred to by \var{ppos} must be initialized to
1965 \code{0} prior to the first call to this function to start the
1966 iteration; the function returns true for each pair in the
1967 dictionary, and false once all pairs have been reported. The
1968 parameters \var{pkey} and \var{pvalue} should either point to
1969 \ctype{PyObject*} variables that will be filled in with each key and
Tim Peters9ddf40b2004-06-20 22:41:32 +00001970 value, respectively, or may be \NULL{}. Any references returned through
Raymond Hettinger54693242003-12-13 19:48:41 +00001971 them are borrowed. \var{ppos} should not be altered during iteration.
1972 Its value represents offsets within the internal dictionary structure,
1973 and since the structure is sparse, the offsets are not consecutive.
Fred Drake3adf79e2001-10-12 19:01:43 +00001974
1975 For example:
1976
1977\begin{verbatim}
1978PyObject *key, *value;
1979int pos = 0;
1980
1981while (PyDict_Next(self->dict, &pos, &key, &value)) {
1982 /* do something interesting with the values... */
1983 ...
1984}
1985\end{verbatim}
1986
1987 The dictionary \var{p} should not be mutated during iteration. It
1988 is safe (since Python 2.1) to modify the values of the keys as you
1989 iterate over the dictionary, but only so long as the set of keys
1990 does not change. For example:
1991
1992\begin{verbatim}
1993PyObject *key, *value;
1994int pos = 0;
1995
1996while (PyDict_Next(self->dict, &pos, &key, &value)) {
1997 int i = PyInt_AS_LONG(value) + 1;
1998 PyObject *o = PyInt_FromLong(i);
1999 if (o == NULL)
2000 return -1;
2001 if (PyDict_SetItem(self->dict, key, o) < 0) {
2002 Py_DECREF(o);
2003 return -1;
2004 }
2005 Py_DECREF(o);
2006}
2007\end{verbatim}
2008\end{cfuncdesc}
2009
2010\begin{cfuncdesc}{int}{PyDict_Merge}{PyObject *a, PyObject *b, int override}
Tim Petersf582b822001-12-11 18:51:08 +00002011 Iterate over mapping object \var{b} adding key-value pairs to dictionary
2012 \var{a}.
2013 \var{b} may be a dictionary, or any object supporting
2014 \function{PyMapping_Keys()} and \function{PyObject_GetItem()}.
2015 If \var{override} is true, existing pairs in \var{a} will
Fred Drake3adf79e2001-10-12 19:01:43 +00002016 be replaced if a matching key is found in \var{b}, otherwise pairs
2017 will only be added if there is not a matching key in \var{a}.
Tim Petersf582b822001-12-11 18:51:08 +00002018 Return \code{0} on success or \code{-1} if an exception was
Fred Drake3adf79e2001-10-12 19:01:43 +00002019 raised.
2020\versionadded{2.2}
2021\end{cfuncdesc}
2022
2023\begin{cfuncdesc}{int}{PyDict_Update}{PyObject *a, PyObject *b}
2024 This is the same as \code{PyDict_Merge(\var{a}, \var{b}, 1)} in C,
Tim Petersf582b822001-12-11 18:51:08 +00002025 or \code{\var{a}.update(\var{b})} in Python. Return \code{0} on
Fred Drake3adf79e2001-10-12 19:01:43 +00002026 success or \code{-1} if an exception was raised.
2027 \versionadded{2.2}
2028\end{cfuncdesc}
2029
Tim Petersf582b822001-12-11 18:51:08 +00002030\begin{cfuncdesc}{int}{PyDict_MergeFromSeq2}{PyObject *a, PyObject *seq2,
2031 int override}
2032 Update or merge into dictionary \var{a}, from the key-value pairs in
2033 \var{seq2}. \var{seq2} must be an iterable object producing
2034 iterable objects of length 2, viewed as key-value pairs. In case of
2035 duplicate keys, the last wins if \var{override} is true, else the
2036 first wins.
2037 Return \code{0} on success or \code{-1} if an exception
2038 was raised.
2039 Equivalent Python (except for the return value):
2040
2041\begin{verbatim}
2042def PyDict_MergeFromSeq2(a, seq2, override):
2043 for key, value in seq2:
2044 if override or key not in a:
2045 a[key] = value
2046\end{verbatim}
2047
2048 \versionadded{2.2}
2049\end{cfuncdesc}
Fred Drake3adf79e2001-10-12 19:01:43 +00002050
Fred Drake54e62942001-12-11 19:40:16 +00002051
Fred Drake3adf79e2001-10-12 19:01:43 +00002052\section{Other Objects \label{otherObjects}}
2053
2054\subsection{File Objects \label{fileObjects}}
2055
2056\obindex{file}
2057Python's built-in file objects are implemented entirely on the
2058\ctype{FILE*} support from the C standard library. This is an
2059implementation detail and may change in future releases of Python.
2060
2061\begin{ctypedesc}{PyFileObject}
2062 This subtype of \ctype{PyObject} represents a Python file object.
2063\end{ctypedesc}
2064
2065\begin{cvardesc}{PyTypeObject}{PyFile_Type}
2066 This instance of \ctype{PyTypeObject} represents the Python file
2067 type. This is exposed to Python programs as \code{types.FileType}.
2068 \withsubitem{(in module types)}{\ttindex{FileType}}
2069\end{cvardesc}
2070
2071\begin{cfuncdesc}{int}{PyFile_Check}{PyObject *p}
2072 Returns true if its argument is a \ctype{PyFileObject} or a subtype
2073 of \ctype{PyFileObject}.
2074 \versionchanged[Allowed subtypes to be accepted]{2.2}
2075\end{cfuncdesc}
2076
2077\begin{cfuncdesc}{int}{PyFile_CheckExact}{PyObject *p}
2078 Returns true if its argument is a \ctype{PyFileObject}, but not a
2079 subtype of \ctype{PyFileObject}.
2080 \versionadded{2.2}
2081\end{cfuncdesc}
2082
2083\begin{cfuncdesc}{PyObject*}{PyFile_FromString}{char *filename, char *mode}
2084 On success, returns a new file object that is opened on the file
2085 given by \var{filename}, with a file mode given by \var{mode}, where
2086 \var{mode} has the same semantics as the standard C routine
Tim Peters9ddf40b2004-06-20 22:41:32 +00002087 \cfunction{fopen()}\ttindex{fopen()}. On failure, returns \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002088\end{cfuncdesc}
2089
2090\begin{cfuncdesc}{PyObject*}{PyFile_FromFile}{FILE *fp,
2091 char *name, char *mode,
2092 int (*close)(FILE*)}
2093 Creates a new \ctype{PyFileObject} from the already-open standard C
2094 file pointer, \var{fp}. The function \var{close} will be called
2095 when the file should be closed. Returns \NULL{} on failure.
2096\end{cfuncdesc}
2097
2098\begin{cfuncdesc}{FILE*}{PyFile_AsFile}{PyFileObject *p}
2099 Returns the file object associated with \var{p} as a \ctype{FILE*}.
2100\end{cfuncdesc}
2101
2102\begin{cfuncdesc}{PyObject*}{PyFile_GetLine}{PyObject *p, int n}
2103 Equivalent to \code{\var{p}.readline(\optional{\var{n}})}, this
2104 function reads one line from the object \var{p}. \var{p} may be a
2105 file object or any object with a \method{readline()} method. If
2106 \var{n} is \code{0}, exactly one line is read, regardless of the
2107 length of the line. If \var{n} is greater than \code{0}, no more
2108 than \var{n} bytes will be read from the file; a partial line can be
2109 returned. In both cases, an empty string is returned if the end of
2110 the file is reached immediately. If \var{n} is less than \code{0},
2111 however, one line is read regardless of length, but
2112 \exception{EOFError} is raised if the end of the file is reached
2113 immediately.
2114 \withsubitem{(built-in exception)}{\ttindex{EOFError}}
2115\end{cfuncdesc}
2116
2117\begin{cfuncdesc}{PyObject*}{PyFile_Name}{PyObject *p}
2118 Returns the name of the file specified by \var{p} as a string
2119 object.
2120\end{cfuncdesc}
2121
2122\begin{cfuncdesc}{void}{PyFile_SetBufSize}{PyFileObject *p, int n}
2123 Available on systems with \cfunction{setvbuf()}\ttindex{setvbuf()}
2124 only. This should only be called immediately after file object
2125 creation.
2126\end{cfuncdesc}
2127
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00002128\begin{cfuncdesc}{int}{PyFile_Encoding}{PyFileObject *p, char *enc}
2129 Set the file's encoding for Unicode output to \var{enc}. Return
2130 1 on success and 0 on failure.
2131 \versionadded{2.3}
2132\end{cfuncdesc}
2133
Fred Drake3adf79e2001-10-12 19:01:43 +00002134\begin{cfuncdesc}{int}{PyFile_SoftSpace}{PyObject *p, int newflag}
2135 This function exists for internal use by the interpreter. Sets the
2136 \member{softspace} attribute of \var{p} to \var{newflag} and
2137 \withsubitem{(file attribute)}{\ttindex{softspace}}returns the
2138 previous value. \var{p} does not have to be a file object for this
2139 function to work properly; any object is supported (thought its only
2140 interesting if the \member{softspace} attribute can be set). This
2141 function clears any errors, and will return \code{0} as the previous
2142 value if the attribute either does not exist or if there were errors
2143 in retrieving it. There is no way to detect errors from this
2144 function, but doing so should not be needed.
2145\end{cfuncdesc}
2146
2147\begin{cfuncdesc}{int}{PyFile_WriteObject}{PyObject *obj, PyFileObject *p,
2148 int flags}
2149 Writes object \var{obj} to file object \var{p}. The only supported
2150 flag for \var{flags} is
2151 \constant{Py_PRINT_RAW}\ttindex{Py_PRINT_RAW}; if given, the
2152 \function{str()} of the object is written instead of the
2153 \function{repr()}. Returns \code{0} on success or \code{-1} on
2154 failure; the appropriate exception will be set.
2155\end{cfuncdesc}
2156
Fred Drake454af892001-11-29 22:42:59 +00002157\begin{cfuncdesc}{int}{PyFile_WriteString}{const char *s, PyFileObject *p}
Fred Drake3adf79e2001-10-12 19:01:43 +00002158 Writes string \var{s} to file object \var{p}. Returns \code{0} on
2159 success or \code{-1} on failure; the appropriate exception will be
2160 set.
2161\end{cfuncdesc}
2162
2163
2164\subsection{Instance Objects \label{instanceObjects}}
2165
2166\obindex{instance}
2167There are very few functions specific to instance objects.
2168
2169\begin{cvardesc}{PyTypeObject}{PyInstance_Type}
2170 Type object for class instances.
2171\end{cvardesc}
2172
2173\begin{cfuncdesc}{int}{PyInstance_Check}{PyObject *obj}
2174 Returns true if \var{obj} is an instance.
2175\end{cfuncdesc}
2176
2177\begin{cfuncdesc}{PyObject*}{PyInstance_New}{PyObject *class,
2178 PyObject *arg,
2179 PyObject *kw}
2180 Create a new instance of a specific class. The parameters \var{arg}
2181 and \var{kw} are used as the positional and keyword parameters to
2182 the object's constructor.
2183\end{cfuncdesc}
2184
2185\begin{cfuncdesc}{PyObject*}{PyInstance_NewRaw}{PyObject *class,
2186 PyObject *dict}
2187 Create a new instance of a specific class without calling it's
2188 constructor. \var{class} is the class of new object. The
2189 \var{dict} parameter will be used as the object's \member{__dict__};
Tim Peters9ddf40b2004-06-20 22:41:32 +00002190 if \NULL{}, a new dictionary will be created for the instance.
Fred Drake3adf79e2001-10-12 19:01:43 +00002191\end{cfuncdesc}
2192
2193
2194\subsection{Method Objects \label{method-objects}}
2195
2196\obindex{method}
2197There are some useful functions that are useful for working with
2198method objects.
2199
2200\begin{cvardesc}{PyTypeObject}{PyMethod_Type}
2201 This instance of \ctype{PyTypeObject} represents the Python method
2202 type. This is exposed to Python programs as \code{types.MethodType}.
2203 \withsubitem{(in module types)}{\ttindex{MethodType}}
2204\end{cvardesc}
2205
2206\begin{cfuncdesc}{int}{PyMethod_Check}{PyObject *o}
2207 Return true if \var{o} is a method object (has type
Tim Peters9ddf40b2004-06-20 22:41:32 +00002208 \cdata{PyMethod_Type}). The parameter must not be \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002209\end{cfuncdesc}
2210
2211\begin{cfuncdesc}{PyObject*}{PyMethod_New}{PyObject *func.
2212 PyObject *self, PyObject *class}
2213 Return a new method object, with \var{func} being any callable
2214 object; this is the function that will be called when the method is
2215 called. If this method should be bound to an instance, \var{self}
2216 should be the instance and \var{class} should be the class of
2217 \var{self}, otherwise \var{self} should be \NULL{} and \var{class}
2218 should be the class which provides the unbound method..
2219\end{cfuncdesc}
2220
2221\begin{cfuncdesc}{PyObject*}{PyMethod_Class}{PyObject *meth}
2222 Return the class object from which the method \var{meth} was
2223 created; if this was created from an instance, it will be the class
2224 of the instance.
2225\end{cfuncdesc}
2226
2227\begin{cfuncdesc}{PyObject*}{PyMethod_GET_CLASS}{PyObject *meth}
2228 Macro version of \cfunction{PyMethod_Class()} which avoids error
2229 checking.
2230\end{cfuncdesc}
2231
2232\begin{cfuncdesc}{PyObject*}{PyMethod_Function}{PyObject *meth}
2233 Return the function object associated with the method \var{meth}.
2234\end{cfuncdesc}
2235
2236\begin{cfuncdesc}{PyObject*}{PyMethod_GET_FUNCTION}{PyObject *meth}
2237 Macro version of \cfunction{PyMethod_Function()} which avoids error
2238 checking.
2239\end{cfuncdesc}
2240
2241\begin{cfuncdesc}{PyObject*}{PyMethod_Self}{PyObject *meth}
2242 Return the instance associated with the method \var{meth} if it is
Tim Peters9ddf40b2004-06-20 22:41:32 +00002243 bound, otherwise return \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002244\end{cfuncdesc}
2245
2246\begin{cfuncdesc}{PyObject*}{PyMethod_GET_SELF}{PyObject *meth}
2247 Macro version of \cfunction{PyMethod_Self()} which avoids error
2248 checking.
2249\end{cfuncdesc}
2250
2251
2252\subsection{Module Objects \label{moduleObjects}}
2253
2254\obindex{module}
2255There are only a few functions special to module objects.
2256
2257\begin{cvardesc}{PyTypeObject}{PyModule_Type}
2258 This instance of \ctype{PyTypeObject} represents the Python module
2259 type. This is exposed to Python programs as
2260 \code{types.ModuleType}.
2261 \withsubitem{(in module types)}{\ttindex{ModuleType}}
2262\end{cvardesc}
2263
2264\begin{cfuncdesc}{int}{PyModule_Check}{PyObject *p}
2265 Returns true if \var{p} is a module object, or a subtype of a module
2266 object.
2267 \versionchanged[Allowed subtypes to be accepted]{2.2}
2268\end{cfuncdesc}
2269
2270\begin{cfuncdesc}{int}{PyModule_CheckExact}{PyObject *p}
2271 Returns true if \var{p} is a module object, but not a subtype of
2272 \cdata{PyModule_Type}.
2273 \versionadded{2.2}
2274\end{cfuncdesc}
2275
2276\begin{cfuncdesc}{PyObject*}{PyModule_New}{char *name}
2277 Return a new module object with the \member{__name__} attribute set
2278 to \var{name}. Only the module's \member{__doc__} and
2279 \member{__name__} attributes are filled in; the caller is
2280 responsible for providing a \member{__file__} attribute.
2281 \withsubitem{(module attribute)}{
2282 \ttindex{__name__}\ttindex{__doc__}\ttindex{__file__}}
2283\end{cfuncdesc}
2284
2285\begin{cfuncdesc}{PyObject*}{PyModule_GetDict}{PyObject *module}
2286 Return the dictionary object that implements \var{module}'s
2287 namespace; this object is the same as the \member{__dict__}
2288 attribute of the module object. This function never fails.
2289 \withsubitem{(module attribute)}{\ttindex{__dict__}}
Fred Drakef495ef72002-04-12 19:32:07 +00002290 It is recommended extensions use other \cfunction{PyModule_*()}
2291 and \cfunction{PyObject_*()} functions rather than directly
2292 manipulate a module's \member{__dict__}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002293\end{cfuncdesc}
2294
2295\begin{cfuncdesc}{char*}{PyModule_GetName}{PyObject *module}
2296 Return \var{module}'s \member{__name__} value. If the module does
2297 not provide one, or if it is not a string, \exception{SystemError}
2298 is raised and \NULL{} is returned.
2299 \withsubitem{(module attribute)}{\ttindex{__name__}}
2300 \withsubitem{(built-in exception)}{\ttindex{SystemError}}
2301\end{cfuncdesc}
2302
2303\begin{cfuncdesc}{char*}{PyModule_GetFilename}{PyObject *module}
2304 Return the name of the file from which \var{module} was loaded using
2305 \var{module}'s \member{__file__} attribute. If this is not defined,
2306 or if it is not a string, raise \exception{SystemError} and return
Tim Peters9ddf40b2004-06-20 22:41:32 +00002307 \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002308 \withsubitem{(module attribute)}{\ttindex{__file__}}
2309 \withsubitem{(built-in exception)}{\ttindex{SystemError}}
2310\end{cfuncdesc}
2311
2312\begin{cfuncdesc}{int}{PyModule_AddObject}{PyObject *module,
2313 char *name, PyObject *value}
2314 Add an object to \var{module} as \var{name}. This is a convenience
2315 function which can be used from the module's initialization
2316 function. This steals a reference to \var{value}. Returns
2317 \code{-1} on error, \code{0} on success.
2318 \versionadded{2.0}
2319\end{cfuncdesc}
2320
2321\begin{cfuncdesc}{int}{PyModule_AddIntConstant}{PyObject *module,
Martin v. Löwisdd07e592004-06-02 12:45:27 +00002322 char *name, long value}
Fred Drake3adf79e2001-10-12 19:01:43 +00002323 Add an integer constant to \var{module} as \var{name}. This
2324 convenience function can be used from the module's initialization
2325 function. Returns \code{-1} on error, \code{0} on success.
2326 \versionadded{2.0}
2327\end{cfuncdesc}
2328
2329\begin{cfuncdesc}{int}{PyModule_AddStringConstant}{PyObject *module,
2330 char *name, char *value}
2331 Add a string constant to \var{module} as \var{name}. This
2332 convenience function can be used from the module's initialization
2333 function. The string \var{value} must be null-terminated. Returns
2334 \code{-1} on error, \code{0} on success.
2335 \versionadded{2.0}
2336\end{cfuncdesc}
2337
2338
2339\subsection{Iterator Objects \label{iterator-objects}}
2340
2341Python provides two general-purpose iterator objects. The first, a
2342sequence iterator, works with an arbitrary sequence supporting the
2343\method{__getitem__()} method. The second works with a callable
2344object and a sentinel value, calling the callable for each item in the
2345sequence, and ending the iteration when the sentinel value is
2346returned.
2347
2348\begin{cvardesc}{PyTypeObject}{PySeqIter_Type}
2349 Type object for iterator objects returned by
2350 \cfunction{PySeqIter_New()} and the one-argument form of the
2351 \function{iter()} built-in function for built-in sequence types.
2352 \versionadded{2.2}
2353\end{cvardesc}
2354
2355\begin{cfuncdesc}{int}{PySeqIter_Check}{op}
2356 Return true if the type of \var{op} is \cdata{PySeqIter_Type}.
2357 \versionadded{2.2}
2358\end{cfuncdesc}
2359
2360\begin{cfuncdesc}{PyObject*}{PySeqIter_New}{PyObject *seq}
2361 Return an iterator that works with a general sequence object,
2362 \var{seq}. The iteration ends when the sequence raises
2363 \exception{IndexError} for the subscripting operation.
2364 \versionadded{2.2}
2365\end{cfuncdesc}
2366
2367\begin{cvardesc}{PyTypeObject}{PyCallIter_Type}
2368 Type object for iterator objects returned by
2369 \cfunction{PyCallIter_New()} and the two-argument form of the
2370 \function{iter()} built-in function.
2371 \versionadded{2.2}
2372\end{cvardesc}
2373
2374\begin{cfuncdesc}{int}{PyCallIter_Check}{op}
2375 Return true if the type of \var{op} is \cdata{PyCallIter_Type}.
2376 \versionadded{2.2}
2377\end{cfuncdesc}
2378
2379\begin{cfuncdesc}{PyObject*}{PyCallIter_New}{PyObject *callable,
2380 PyObject *sentinel}
2381 Return a new iterator. The first parameter, \var{callable}, can be
2382 any Python callable object that can be called with no parameters;
2383 each call to it should return the next item in the iteration. When
2384 \var{callable} returns a value equal to \var{sentinel}, the
2385 iteration will be terminated.
2386 \versionadded{2.2}
2387\end{cfuncdesc}
2388
2389
2390\subsection{Descriptor Objects \label{descriptor-objects}}
2391
Fred Drake54e62942001-12-11 19:40:16 +00002392``Descriptors'' are objects that describe some attribute of an object.
2393They are found in the dictionary of type objects.
2394
Fred Drake3adf79e2001-10-12 19:01:43 +00002395\begin{cvardesc}{PyTypeObject}{PyProperty_Type}
Fred Drake54e62942001-12-11 19:40:16 +00002396 The type object for the built-in descriptor types.
Fred Drake3adf79e2001-10-12 19:01:43 +00002397 \versionadded{2.2}
2398\end{cvardesc}
2399
2400\begin{cfuncdesc}{PyObject*}{PyDescr_NewGetSet}{PyTypeObject *type,
2401 PyGetSetDef *getset}
2402 \versionadded{2.2}
2403\end{cfuncdesc}
2404
2405\begin{cfuncdesc}{PyObject*}{PyDescr_NewMember}{PyTypeObject *type,
2406 PyMemberDef *meth}
2407 \versionadded{2.2}
2408\end{cfuncdesc}
2409
2410\begin{cfuncdesc}{PyObject*}{PyDescr_NewMethod}{PyTypeObject *type,
2411 PyMethodDef *meth}
2412 \versionadded{2.2}
2413\end{cfuncdesc}
2414
2415\begin{cfuncdesc}{PyObject*}{PyDescr_NewWrapper}{PyTypeObject *type,
2416 struct wrapperbase *wrapper,
2417 void *wrapped}
2418 \versionadded{2.2}
2419\end{cfuncdesc}
2420
Thomas Heller8178a222004-02-09 10:47:11 +00002421\begin{cfuncdesc}{PyObject*}{PyDescr_NewClassMethod}{PyTypeObject *type,
2422 PyMethodDef *method}
2423 \versionadded{2.3}
2424\end{cfuncdesc}
2425
Fred Drake3adf79e2001-10-12 19:01:43 +00002426\begin{cfuncdesc}{int}{PyDescr_IsData}{PyObject *descr}
2427 Returns true if the descriptor objects \var{descr} describes a data
2428 attribute, or false if it describes a method. \var{descr} must be a
2429 descriptor object; there is no error checking.
2430 \versionadded{2.2}
2431\end{cfuncdesc}
2432
2433\begin{cfuncdesc}{PyObject*}{PyWrapper_New}{PyObject *, PyObject *}
2434 \versionadded{2.2}
2435\end{cfuncdesc}
2436
2437
2438\subsection{Slice Objects \label{slice-objects}}
2439
2440\begin{cvardesc}{PyTypeObject}{PySlice_Type}
2441 The type object for slice objects. This is the same as
2442 \code{types.SliceType}.
2443 \withsubitem{(in module types)}{\ttindex{SliceType}}
2444\end{cvardesc}
2445
2446\begin{cfuncdesc}{int}{PySlice_Check}{PyObject *ob}
2447 Returns true if \var{ob} is a slice object; \var{ob} must not be
Tim Peters9ddf40b2004-06-20 22:41:32 +00002448 \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002449\end{cfuncdesc}
2450
2451\begin{cfuncdesc}{PyObject*}{PySlice_New}{PyObject *start, PyObject *stop,
2452 PyObject *step}
2453 Return a new slice object with the given values. The \var{start},
2454 \var{stop}, and \var{step} parameters are used as the values of the
2455 slice object attributes of the same names. Any of the values may be
Tim Peters9ddf40b2004-06-20 22:41:32 +00002456 \NULL{}, in which case the \code{None} will be used for the
Fred Drake3adf79e2001-10-12 19:01:43 +00002457 corresponding attribute. Returns \NULL{} if the new object could
2458 not be allocated.
2459\end{cfuncdesc}
2460
2461\begin{cfuncdesc}{int}{PySlice_GetIndices}{PySliceObject *slice, int length,
2462 int *start, int *stop, int *step}
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002463Retrieve the start, stop and step indices from the slice object
2464\var{slice}, assuming a sequence of length \var{length}. Treats
2465indices greater than \var{length} as errors.
2466
2467Returns 0 on success and -1 on error with no exception set (unless one
2468of the indices was not \constant{None} and failed to be converted to
2469an integer, in which case -1 is returned with an exception set).
2470
2471You probably do not want to use this function. If you want to use
2472slice objects in versions of Python prior to 2.3, you would probably
2473do well to incorporate the source of \cfunction{PySlice_GetIndicesEx},
2474suitably renamed, in the source of your extension.
2475\end{cfuncdesc}
2476
2477\begin{cfuncdesc}{int}{PySlice_GetIndicesEx}{PySliceObject *slice, int length,
Tim Peters9ddf40b2004-06-20 22:41:32 +00002478 int *start, int *stop, int *step,
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002479 int *slicelength}
2480Usable replacement for \cfunction{PySlice_GetIndices}. Retrieve the
2481start, stop, and step indices from the slice object \var{slice}
2482assuming a sequence of length \var{length}, and store the length of
2483the slice in \var{slicelength}. Out of bounds indices are clipped in
2484a manner consistent with the handling of normal slices.
2485
2486Returns 0 on success and -1 on error with exception set.
2487
2488\versionadded{2.3}
Fred Drake3adf79e2001-10-12 19:01:43 +00002489\end{cfuncdesc}
2490
2491
2492\subsection{Weak Reference Objects \label{weakref-objects}}
2493
2494Python supports \emph{weak references} as first-class objects. There
2495are two specific object types which directly implement weak
2496references. The first is a simple reference object, and the second
2497acts as a proxy for the original object as much as it can.
2498
2499\begin{cfuncdesc}{int}{PyWeakref_Check}{ob}
2500 Return true if \var{ob} is either a reference or proxy object.
2501 \versionadded{2.2}
2502\end{cfuncdesc}
2503
2504\begin{cfuncdesc}{int}{PyWeakref_CheckRef}{ob}
2505 Return true if \var{ob} is a reference object.
2506 \versionadded{2.2}
2507\end{cfuncdesc}
2508
2509\begin{cfuncdesc}{int}{PyWeakref_CheckProxy}{ob}
2510 Return true if \var{ob} is a proxy object.
2511 \versionadded{2.2}
2512\end{cfuncdesc}
2513
2514\begin{cfuncdesc}{PyObject*}{PyWeakref_NewRef}{PyObject *ob,
2515 PyObject *callback}
2516 Return a weak reference object for the object \var{ob}. This will
2517 always return a new reference, but is not guaranteed to create a new
2518 object; an existing reference object may be returned. The second
2519 parameter, \var{callback}, can be a callable object that receives
2520 notification when \var{ob} is garbage collected; it should accept a
Raymond Hettinger5232f502004-03-25 08:51:36 +00002521 single parameter, which will be the weak reference object itself.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002522 \var{callback} may also be \code{None} or \NULL{}. If \var{ob}
Fred Drake3adf79e2001-10-12 19:01:43 +00002523 is not a weakly-referencable object, or if \var{callback} is not
Tim Peters9ddf40b2004-06-20 22:41:32 +00002524 callable, \code{None}, or \NULL{}, this will return \NULL{} and
Fred Drake3adf79e2001-10-12 19:01:43 +00002525 raise \exception{TypeError}.
2526 \versionadded{2.2}
2527\end{cfuncdesc}
2528
2529\begin{cfuncdesc}{PyObject*}{PyWeakref_NewProxy}{PyObject *ob,
2530 PyObject *callback}
2531 Return a weak reference proxy object for the object \var{ob}. This
2532 will always return a new reference, but is not guaranteed to create
2533 a new object; an existing proxy object may be returned. The second
2534 parameter, \var{callback}, can be a callable object that receives
2535 notification when \var{ob} is garbage collected; it should accept a
Raymond Hettinger5232f502004-03-25 08:51:36 +00002536 single parameter, which will be the weak reference object itself.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002537 \var{callback} may also be \code{None} or \NULL{}. If \var{ob} is not
Fred Drake3adf79e2001-10-12 19:01:43 +00002538 a weakly-referencable object, or if \var{callback} is not callable,
Tim Peters9ddf40b2004-06-20 22:41:32 +00002539 \code{None}, or \NULL{}, this will return \NULL{} and raise
Fred Drake3adf79e2001-10-12 19:01:43 +00002540 \exception{TypeError}.
2541 \versionadded{2.2}
2542\end{cfuncdesc}
2543
2544\begin{cfuncdesc}{PyObject*}{PyWeakref_GetObject}{PyObject *ref}
2545 Returns the referenced object from a weak reference, \var{ref}. If
Ka-Ping Yeebd379e92003-03-28 18:07:16 +00002546 the referent is no longer live, returns \code{None}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002547 \versionadded{2.2}
2548\end{cfuncdesc}
2549
2550\begin{cfuncdesc}{PyObject*}{PyWeakref_GET_OBJECT}{PyObject *ref}
2551 Similar to \cfunction{PyWeakref_GetObject()}, but implemented as a
2552 macro that does no error checking.
2553 \versionadded{2.2}
2554\end{cfuncdesc}
2555
2556
2557\subsection{CObjects \label{cObjects}}
2558
2559\obindex{CObject}
2560Refer to \emph{Extending and Embedding the Python Interpreter},
Fred Drake54e62942001-12-11 19:40:16 +00002561section~1.12, ``Providing a C API for an Extension Module,'' for more
Fred Drake3adf79e2001-10-12 19:01:43 +00002562information on using these objects.
2563
2564
2565\begin{ctypedesc}{PyCObject}
2566 This subtype of \ctype{PyObject} represents an opaque value, useful
2567 for C extension modules who need to pass an opaque value (as a
2568 \ctype{void*} pointer) through Python code to other C code. It is
2569 often used to make a C function pointer defined in one module
2570 available to other modules, so the regular import mechanism can be
2571 used to access C APIs defined in dynamically loaded modules.
2572\end{ctypedesc}
2573
2574\begin{cfuncdesc}{int}{PyCObject_Check}{PyObject *p}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002575 Return true if its argument is a \ctype{PyCObject}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002576\end{cfuncdesc}
2577
Tim Petersf582b822001-12-11 18:51:08 +00002578\begin{cfuncdesc}{PyObject*}{PyCObject_FromVoidPtr}{void* cobj,
Fred Drake54e62942001-12-11 19:40:16 +00002579 void (*destr)(void *)}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002580 Create a \ctype{PyCObject} from the \code{void *}\var{cobj}. The
Fred Drake3adf79e2001-10-12 19:01:43 +00002581 \var{destr} function will be called when the object is reclaimed,
Tim Peters9ddf40b2004-06-20 22:41:32 +00002582 unless it is \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002583\end{cfuncdesc}
2584
2585\begin{cfuncdesc}{PyObject*}{PyCObject_FromVoidPtrAndDesc}{void* cobj,
2586 void* desc, void (*destr)(void *, void *)}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002587 Create a \ctype{PyCObject} from the \ctype{void *}\var{cobj}. The
Fred Drake3adf79e2001-10-12 19:01:43 +00002588 \var{destr} function will be called when the object is reclaimed.
2589 The \var{desc} argument can be used to pass extra callback data for
2590 the destructor function.
2591\end{cfuncdesc}
2592
2593\begin{cfuncdesc}{void*}{PyCObject_AsVoidPtr}{PyObject* self}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002594 Return the object \ctype{void *} that the \ctype{PyCObject}
Fred Drake3adf79e2001-10-12 19:01:43 +00002595 \var{self} was created with.
2596\end{cfuncdesc}
2597
2598\begin{cfuncdesc}{void*}{PyCObject_GetDesc}{PyObject* self}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002599 Return the description \ctype{void *} that the \ctype{PyCObject}
Fred Drake3adf79e2001-10-12 19:01:43 +00002600 \var{self} was created with.
2601\end{cfuncdesc}
Fred Drakecd8474e2001-11-26 21:29:17 +00002602
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002603\begin{cfuncdesc}{int}{PyCObject_SetVoidPtr}{PyObject* self, void* cobj}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002604 Set the void pointer inside \var{self} to \var{cobj}.
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002605 The \ctype{PyCObject} must not have an associated destructor.
2606 Return true on success, false on failure.
2607\end{cfuncdesc}
2608
Fred Drakecd8474e2001-11-26 21:29:17 +00002609
2610\subsection{Cell Objects \label{cell-objects}}
2611
2612``Cell'' objects are used to implement variables referenced by
2613multiple scopes. For each such variable, a cell object is created to
2614store the value; the local variables of each stack frame that
2615references the value contains a reference to the cells from outer
2616scopes which also use that variable. When the value is accessed, the
2617value contained in the cell is used instead of the cell object
2618itself. This de-referencing of the cell object requires support from
2619the generated byte-code; these are not automatically de-referenced
2620when accessed. Cell objects are not likely to be useful elsewhere.
2621
Fred Drake54e62942001-12-11 19:40:16 +00002622\begin{ctypedesc}{PyCellObject}
2623 The C structure used for cell objects.
2624\end{ctypedesc}
2625
Fred Drakecd8474e2001-11-26 21:29:17 +00002626\begin{cvardesc}{PyTypeObject}{PyCell_Type}
2627 The type object corresponding to cell objects
2628\end{cvardesc}
2629
2630\begin{cfuncdesc}{int}{PyCell_Check}{ob}
2631 Return true if \var{ob} is a cell object; \var{ob} must not be
Tim Peters9ddf40b2004-06-20 22:41:32 +00002632 \NULL{}.
Fred Drakecd8474e2001-11-26 21:29:17 +00002633\end{cfuncdesc}
2634
2635\begin{cfuncdesc}{PyObject*}{PyCell_New}{PyObject *ob}
2636 Create and return a new cell object containing the value \var{ob}.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002637 The parameter may be \NULL{}.
Fred Drakecd8474e2001-11-26 21:29:17 +00002638\end{cfuncdesc}
2639
2640\begin{cfuncdesc}{PyObject*}{PyCell_Get}{PyObject *cell}
2641 Return the contents of the cell \var{cell}.
2642\end{cfuncdesc}
2643
2644\begin{cfuncdesc}{PyObject*}{PyCell_GET}{PyObject *cell}
2645 Return the contents of the cell \var{cell}, but without checking
Raymond Hettingerf4bb1f92003-08-23 03:38:11 +00002646 that \var{cell} is non-\NULL{} and a cell object.
Fred Drakecd8474e2001-11-26 21:29:17 +00002647\end{cfuncdesc}
2648
2649\begin{cfuncdesc}{int}{PyCell_Set}{PyObject *cell, PyObject *value}
2650 Set the contents of the cell object \var{cell} to \var{value}. This
2651 releases the reference to any current content of the cell.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002652 \var{value} may be \NULL{}. \var{cell} must be non-\NULL{}; if it is
Fred Drakecd8474e2001-11-26 21:29:17 +00002653 not a cell object, \code{-1} will be returned. On success, \code{0}
2654 will be returned.
2655\end{cfuncdesc}
2656
2657\begin{cfuncdesc}{void}{PyCell_SET}{PyObject *cell, PyObject *value}
2658 Sets the value of the cell object \var{cell} to \var{value}. No
2659 reference counts are adjusted, and no checks are made for safety;
2660 \var{cell} must be non-\NULL{} and must be a cell object.
2661\end{cfuncdesc}
Martin v. Löwise440e472004-06-01 15:22:42 +00002662
2663
2664\subsection{Generator Objects \label{gen-objects}}
2665
2666Generator objects are what Python uses to implement generator iterators.
2667They are normally created by iterating over a function that yields values,
2668rather than explicitly calling \cfunction{PyGen_New}.
2669
2670\begin{ctypedesc}{PyGenObject}
2671 The C structure used for generator objects.
2672\end{ctypedesc}
2673
2674\begin{cvardesc}{PyTypeObject}{PyGen_Type}
2675 The type object corresponding to generator objects
2676\end{cvardesc}
2677
2678\begin{cfuncdesc}{int}{PyGen_Check}{ob}
2679 Return true if \var{ob} is a generator object; \var{ob} must not be
Tim Peters9ddf40b2004-06-20 22:41:32 +00002680 \NULL{}.
Martin v. Löwise440e472004-06-01 15:22:42 +00002681\end{cfuncdesc}
2682
2683\begin{cfuncdesc}{int}{PyGen_CheckExact}{ob}
2684 Return true if \var{ob}'s type is \var{PyGen_Type}
2685 is a generator object; \var{ob} must not be
Tim Peters9ddf40b2004-06-20 22:41:32 +00002686 \NULL{}.
Martin v. Löwise440e472004-06-01 15:22:42 +00002687\end{cfuncdesc}
2688
2689\begin{cfuncdesc}{PyObject*}{PyGen_New}{PyFrameObject *frame}
2690 Create and return a new generator object based on the \var{frame} object.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002691 The parameter must not be \NULL{}.
2692\end{cfuncdesc}
2693
2694
2695\subsection{DateTime Objects \label{datetime-objects}}
2696
2697Various date and time objects are supplied by the \module{datetime}
2698module. Before using any of these functions, the header file
2699\file{datetime.h} must be included in your source (note that this is
2700not include by \file{Python.h}), and macro \cfunction{PyDateTime_IMPORT()}
2701must be invoked. The macro arranges to put a pointer to a C structure
2702in a static variable \code{PyDateTimeAPI}, which is used by the following
Tim Peters183dabc2004-07-11 19:26:19 +00002703macros.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002704
Tim Peters183dabc2004-07-11 19:26:19 +00002705Type-check macros:
2706
2707\begin{cfuncdesc}{int}{PyDate_Check}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002708 Return true if \var{ob} is of type \cdata{PyDateTime_DateType} or
2709 a subtype of \cdata{PyDateTime_DateType}. \var{ob} must not be
2710 \NULL{}.
2711 \versionadded{2.4}
2712\end{cfuncdesc}
2713
Tim Peters183dabc2004-07-11 19:26:19 +00002714\begin{cfuncdesc}{int}{PyDate_CheckExact}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002715 Return true if \var{ob} is of type \cdata{PyDateTime_DateType}.
2716 \var{ob} must not be \NULL{}.
2717 \versionadded{2.4}
2718\end{cfuncdesc}
2719
Tim Peters183dabc2004-07-11 19:26:19 +00002720\begin{cfuncdesc}{int}{PyDateTime_Check}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002721 Return true if \var{ob} is of type \cdata{PyDateTime_DateTimeType} or
2722 a subtype of \cdata{PyDateTime_DateTimeType}. \var{ob} must not be
2723 \NULL{}.
2724 \versionadded{2.4}
2725\end{cfuncdesc}
2726
Tim Peters183dabc2004-07-11 19:26:19 +00002727\begin{cfuncdesc}{int}{PyDateTime_CheckExact}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002728 Return true if \var{ob} is of type \cdata{PyDateTime_DateTimeType}.
2729 \var{ob} must not be \NULL{}.
2730 \versionadded{2.4}
2731\end{cfuncdesc}
2732
Tim Peters183dabc2004-07-11 19:26:19 +00002733\begin{cfuncdesc}{int}{PyTime_Check}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002734 Return true if \var{ob} is of type \cdata{PyDateTime_TimeType} or
2735 a subtype of \cdata{PyDateTime_TimeType}. \var{ob} must not be
2736 \NULL{}.
2737 \versionadded{2.4}
2738\end{cfuncdesc}
2739
Tim Peters183dabc2004-07-11 19:26:19 +00002740\begin{cfuncdesc}{int}{PyTime_CheckExact}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002741 Return true if \var{ob} is of type \cdata{PyDateTime_TimeType}.
2742 \var{ob} must not be \NULL{}.
2743 \versionadded{2.4}
2744\end{cfuncdesc}
2745
Tim Peters183dabc2004-07-11 19:26:19 +00002746\begin{cfuncdesc}{int}{PyDelta_Check}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002747 Return true if \var{ob} is of type \cdata{PyDateTime_DeltaType} or
2748 a subtype of \cdata{PyDateTime_DeltaType}. \var{ob} must not be
2749 \NULL{}.
2750 \versionadded{2.4}
2751\end{cfuncdesc}
2752
Tim Peters183dabc2004-07-11 19:26:19 +00002753\begin{cfuncdesc}{int}{PyDelta_CheckExact}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002754 Return true if \var{ob} is of type \cdata{PyDateTime_DeltaType}.
2755 \var{ob} must not be \NULL{}.
2756 \versionadded{2.4}
2757\end{cfuncdesc}
2758
Tim Peters183dabc2004-07-11 19:26:19 +00002759\begin{cfuncdesc}{int}{PyTZInfo_Check}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002760 Return true if \var{ob} is of type \cdata{PyDateTime_TZInfoType} or
2761 a subtype of \cdata{PyDateTime_TZInfoType}. \var{ob} must not be
2762 \NULL{}.
2763 \versionadded{2.4}
2764\end{cfuncdesc}
2765
Tim Peters183dabc2004-07-11 19:26:19 +00002766\begin{cfuncdesc}{int}{PyTZInfo_CheckExact}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002767 Return true if \var{ob} is of type \cdata{PyDateTime_TZInfoType}.
2768 \var{ob} must not be \NULL{}.
2769 \versionadded{2.4}
2770\end{cfuncdesc}
2771
Tim Peters183dabc2004-07-11 19:26:19 +00002772Macros to create objects:
2773
Tim Peters9ddf40b2004-06-20 22:41:32 +00002774\begin{cfuncdesc}{PyObject*}{PyDate_FromDate}{int year, int month, int day}
2775 Return a \code{datetime.date} object with the specified year, month
2776 and day.
2777 \versionadded{2.4}
2778\end{cfuncdesc}
2779
2780\begin{cfuncdesc}{PyObject*}{PyDate_FromDateAndTime}{int year, int month,
2781 int day, int hour, int minute, int second, int usecond}
2782 Return a \code{datetime.datetime} object with the specified year, month,
2783 day, hour, minute, second and microsecond.
2784 \versionadded{2.4}
2785\end{cfuncdesc}
2786
2787\begin{cfuncdesc}{PyObject*}{PyTime_FromTime}{int hour, int minute,
2788 int second, int usecond}
2789 Return a \code{datetime.time} object with the specified hour, minute,
2790 second and microsecond.
2791 \versionadded{2.4}
2792\end{cfuncdesc}
2793
2794\begin{cfuncdesc}{PyObject*}{PyDelta_FromDSU}{int days, int seconds,
2795 int useconds}
2796 Return a \code{datetime.timedelta} object representing the given number
2797 of days, seconds and microseconds. Normalization is performed so that
2798 the resulting number of microseconds and seconds lie in the ranges
2799 documented for \code{datetime.timedelta} objects.
2800 \versionadded{2.4}
2801\end{cfuncdesc}
2802
Tim Peters8ff9f9f2004-07-17 01:42:26 +00002803Macros to extract fields from date objects. The argument must be an
Tim Peters183dabc2004-07-11 19:26:19 +00002804instance of \cdata{PyDateTime_Date}, including subclasses (such as
2805\cdata{PyDateTime_DateTime}). The argument must not be \NULL{}, and
2806the type is not checked:
2807
2808\begin{cfuncdesc}{int}{PyDateTime_GET_YEAR}{PyDateTime_Date *o}
2809 Return the year, as a positive int.
2810 \versionadded{2.4}
2811\end{cfuncdesc}
2812
2813\begin{cfuncdesc}{int}{PyDateTime_GET_MONTH}{PyDateTime_Date *o}
2814 Return the month, as an int from 1 through 12.
2815 \versionadded{2.4}
2816\end{cfuncdesc}
2817
2818\begin{cfuncdesc}{int}{PyDateTime_GET_DAY}{PyDateTime_Date *o}
2819 Return the day, as an int from 1 through 31.
2820 \versionadded{2.4}
2821\end{cfuncdesc}
2822
Tim Peters8ff9f9f2004-07-17 01:42:26 +00002823Macros to extract fields from datetime objects. The argument must be an
Tim Peters183dabc2004-07-11 19:26:19 +00002824instance of \cdata{PyDateTime_DateTime}, including subclasses.
2825The argument must not be \NULL{}, and the type is not checked:
2826
2827\begin{cfuncdesc}{int}{PyDateTime_DATE_GET_HOUR}{PyDateTime_DateTime *o}
Neal Norwitz7fdd92f2004-08-02 21:56:33 +00002828 Return the hour, as an int from 0 through 23.
Tim Peters183dabc2004-07-11 19:26:19 +00002829 \versionadded{2.4}
2830\end{cfuncdesc}
2831
2832\begin{cfuncdesc}{int}{PyDateTime_DATE_GET_MINUTE}{PyDateTime_DateTime *o}
2833 Return the minute, as an int from 0 through 59.
2834 \versionadded{2.4}
2835\end{cfuncdesc}
2836
2837\begin{cfuncdesc}{int}{PyDateTime_DATE_GET_SECOND}{PyDateTime_DateTime *o}
2838 Return the second, as an int from 0 through 59.
2839 \versionadded{2.4}
2840\end{cfuncdesc}
2841
2842\begin{cfuncdesc}{int}{PyDateTime_DATE_GET_MICROSECOND}{PyDateTime_DateTime *o}
2843 Return the microsecond, as an int from 0 through 999999.
2844 \versionadded{2.4}
2845\end{cfuncdesc}
2846
Tim Peters8ff9f9f2004-07-17 01:42:26 +00002847Macros to extract fields from time objects. The argument must be an
Tim Peters183dabc2004-07-11 19:26:19 +00002848instance of \cdata{PyDateTime_Time}, including subclasses.
2849The argument must not be \NULL{}, and the type is not checked:
2850
2851\begin{cfuncdesc}{int}{PyDateTime_TIME_GET_HOUR}{PyDateTime_Time *o}
Neal Norwitz7fdd92f2004-08-02 21:56:33 +00002852 Return the hour, as an int from 0 through 23.
Tim Peters183dabc2004-07-11 19:26:19 +00002853 \versionadded{2.4}
2854\end{cfuncdesc}
2855
2856\begin{cfuncdesc}{int}{PyDateTime_TIME_GET_MINUTE}{PyDateTime_Time *o}
2857 Return the minute, as an int from 0 through 59.
2858 \versionadded{2.4}
2859\end{cfuncdesc}
2860
2861\begin{cfuncdesc}{int}{PyDateTime_TIME_GET_SECOND}{PyDateTime_Time *o}
2862 Return the second, as an int from 0 through 59.
2863 \versionadded{2.4}
2864\end{cfuncdesc}
2865
2866\begin{cfuncdesc}{int}{PyDateTime_TIME_GET_MICROSECOND}{PyDateTime_Time *o}
2867 Return the microsecond, as an int from 0 through 999999.
2868 \versionadded{2.4}
2869\end{cfuncdesc}
2870
2871Macros for the convenience of modules implementing the DB API:
2872
Tim Peters9ddf40b2004-06-20 22:41:32 +00002873\begin{cfuncdesc}{PyObject*}{PyDateTime_FromTimestamp}{PyObject *args}
2874 Create and return a new \code{datetime.datetime} object given an argument
2875 tuple suitable for passing to \code{datetime.datetime.fromtimestamp()}.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002876 \versionadded{2.4}
2877\end{cfuncdesc}
2878
2879\begin{cfuncdesc}{PyObject*}{PyDate_FromTimestamp}{PyObject *args}
2880 Create and return a new \code{datetime.date} object given an argument
2881 tuple suitable for passing to \code{datetime.date.fromtimestamp()}.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002882 \versionadded{2.4}
Martin v. Löwise440e472004-06-01 15:22:42 +00002883\end{cfuncdesc}