blob: 992442dc28682ff1a440a81cb89477bc6253a3d1 [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
Skip Montanaro33ee76a2004-07-28 14:17:04 +0000195\subsubsection{Boolean Objects \label{boolObjects}}
196
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
207\begin{cfuncdesc}{Py_RETURN_FALSE}
208Return Py_False from a function, properly incrementing its reference
209count.
210\versionadded{2.4}
211\end{cfuncdesc}
212
213\begin{cfuncdesc}{Py_RETURN_TRUE}
214Return Py_True from a function, properly incrementing its reference
215count.
216\versionadded{2.4}
217\end{cfuncdesc}
218
219\begin{cfuncdesc}{int}{PyBool_Check}{PyObject* o}
220 Returns true if \var{o} is of type \cdata{PyBool_Type}.
221 \versionadded{2.3}
222\end{cfuncdesc}
223
224\begin{cfuncdesc}{int}{PyBool_FromLong}{long v}
225Returns \constant{Py_True} or \constant{Py_False} depending on the
226truth value of \var{v}.
227\versionadded{2.3}
228\end{cfuncdesc}
Fred Drake3adf79e2001-10-12 19:01:43 +0000229
230\subsection{Long Integer Objects \label{longObjects}}
231
232\obindex{long integer}
233\begin{ctypedesc}{PyLongObject}
234 This subtype of \ctype{PyObject} represents a Python long integer
235 object.
236\end{ctypedesc}
237
238\begin{cvardesc}{PyTypeObject}{PyLong_Type}
239 This instance of \ctype{PyTypeObject} represents the Python long
240 integer type. This is the same object as \code{types.LongType}.
241 \withsubitem{(in modules types)}{\ttindex{LongType}}
242\end{cvardesc}
243
244\begin{cfuncdesc}{int}{PyLong_Check}{PyObject *p}
245 Returns true if its argument is a \ctype{PyLongObject} or a subtype
246 of \ctype{PyLongObject}.
247 \versionchanged[Allowed subtypes to be accepted]{2.2}
248\end{cfuncdesc}
249
250\begin{cfuncdesc}{int}{PyLong_CheckExact}{PyObject *p}
251 Returns true if its argument is a \ctype{PyLongObject}, but not a
252 subtype of \ctype{PyLongObject}.
253 \versionadded{2.2}
254\end{cfuncdesc}
255
256\begin{cfuncdesc}{PyObject*}{PyLong_FromLong}{long v}
257 Returns a new \ctype{PyLongObject} object from \var{v}, or \NULL{}
258 on failure.
259\end{cfuncdesc}
260
261\begin{cfuncdesc}{PyObject*}{PyLong_FromUnsignedLong}{unsigned long v}
262 Returns a new \ctype{PyLongObject} object from a C \ctype{unsigned
263 long}, or \NULL{} on failure.
264\end{cfuncdesc}
265
266\begin{cfuncdesc}{PyObject*}{PyLong_FromLongLong}{long long v}
267 Returns a new \ctype{PyLongObject} object from a C \ctype{long long},
268 or \NULL{} on failure.
269\end{cfuncdesc}
270
271\begin{cfuncdesc}{PyObject*}{PyLong_FromUnsignedLongLong}{unsigned long long v}
272 Returns a new \ctype{PyLongObject} object from a C \ctype{unsigned
273 long long}, or \NULL{} on failure.
274\end{cfuncdesc}
275
276\begin{cfuncdesc}{PyObject*}{PyLong_FromDouble}{double v}
277 Returns a new \ctype{PyLongObject} object from the integer part of
278 \var{v}, or \NULL{} on failure.
279\end{cfuncdesc}
280
281\begin{cfuncdesc}{PyObject*}{PyLong_FromString}{char *str, char **pend,
282 int base}
283 Return a new \ctype{PyLongObject} based on the string value in
284 \var{str}, which is interpreted according to the radix in
Tim Peters9ddf40b2004-06-20 22:41:32 +0000285 \var{base}. If \var{pend} is non-\NULL{}, \code{*\var{pend}} will
Fred Drake3adf79e2001-10-12 19:01:43 +0000286 point to the first character in \var{str} which follows the
287 representation of the number. If \var{base} is \code{0}, the radix
Skip Montanaro1ff49a72003-02-03 05:13:24 +0000288 will be determined based on the leading characters of \var{str}: if
Fred Drake3adf79e2001-10-12 19:01:43 +0000289 \var{str} starts with \code{'0x'} or \code{'0X'}, radix 16 will be
290 used; if \var{str} starts with \code{'0'}, radix 8 will be used;
291 otherwise radix 10 will be used. If \var{base} is not \code{0}, it
292 must be between \code{2} and \code{36}, inclusive. Leading spaces
293 are ignored. If there are no digits, \exception{ValueError} will be
294 raised.
295\end{cfuncdesc}
296
297\begin{cfuncdesc}{PyObject*}{PyLong_FromUnicode}{Py_UNICODE *u,
298 int length, int base}
299 Convert a sequence of Unicode digits to a Python long integer
300 value. The first parameter, \var{u}, points to the first character
301 of the Unicode string, \var{length} gives the number of characters,
302 and \var{base} is the radix for the conversion. The radix must be
303 in the range [2, 36]; if it is out of range, \exception{ValueError}
304 will be raised.
305 \versionadded{1.6}
306\end{cfuncdesc}
307
308\begin{cfuncdesc}{PyObject*}{PyLong_FromVoidPtr}{void *p}
309 Create a Python integer or long integer from the pointer \var{p}.
310 The pointer value can be retrieved from the resulting value using
311 \cfunction{PyLong_AsVoidPtr()}.
312 \versionadded{1.5.2}
313\end{cfuncdesc}
314
315\begin{cfuncdesc}{long}{PyLong_AsLong}{PyObject *pylong}
316 Returns a C \ctype{long} representation of the contents of
317 \var{pylong}. If \var{pylong} is greater than
318 \constant{LONG_MAX}\ttindex{LONG_MAX}, an \exception{OverflowError}
319 is raised.
320 \withsubitem{(built-in exception)}{\ttindex{OverflowError}}
321\end{cfuncdesc}
322
323\begin{cfuncdesc}{unsigned long}{PyLong_AsUnsignedLong}{PyObject *pylong}
324 Returns a C \ctype{unsigned long} representation of the contents of
325 \var{pylong}. If \var{pylong} is greater than
326 \constant{ULONG_MAX}\ttindex{ULONG_MAX}, an
327 \exception{OverflowError} is raised.
Tim Petersf582b822001-12-11 18:51:08 +0000328 \withsubitem{(built-in exception)}{\ttindex{OverflowError}}
Fred Drake3adf79e2001-10-12 19:01:43 +0000329\end{cfuncdesc}
330
331\begin{cfuncdesc}{long long}{PyLong_AsLongLong}{PyObject *pylong}
332 Return a C \ctype{long long} from a Python long integer. If
333 \var{pylong} cannot be represented as a \ctype{long long}, an
334 \exception{OverflowError} will be raised.
335 \versionadded{2.2}
336\end{cfuncdesc}
337
338\begin{cfuncdesc}{unsigned long long}{PyLong_AsUnsignedLongLong}{PyObject
339 *pylong}
340 Return a C \ctype{unsigned long long} from a Python long integer.
341 If \var{pylong} cannot be represented as an \ctype{unsigned long
342 long}, an \exception{OverflowError} will be raised if the value is
343 positive, or a \exception{TypeError} will be raised if the value is
344 negative.
345 \versionadded{2.2}
346\end{cfuncdesc}
347
Thomas Heller34d7f092003-04-23 19:51:05 +0000348\begin{cfuncdesc}{unsigned long}{PyLong_AsUnsignedLongMask}{PyObject *io}
349 Return a C \ctype{unsigned long} from a Python long integer, without
350 checking for overflow.
351 \versionadded{2.3}
352\end{cfuncdesc}
353
354\begin{cfuncdesc}{unsigned long}{PyLong_AsUnsignedLongLongMask}{PyObject *io}
355 Return a C \ctype{unsigned long long} from a Python long integer, without
356 checking for overflow.
357 \versionadded{2.3}
358\end{cfuncdesc}
359
Fred Drake3adf79e2001-10-12 19:01:43 +0000360\begin{cfuncdesc}{double}{PyLong_AsDouble}{PyObject *pylong}
361 Returns a C \ctype{double} representation of the contents of
362 \var{pylong}. If \var{pylong} cannot be approximately represented
363 as a \ctype{double}, an \exception{OverflowError} exception is
364 raised and \code{-1.0} will be returned.
365\end{cfuncdesc}
366
367\begin{cfuncdesc}{void*}{PyLong_AsVoidPtr}{PyObject *pylong}
368 Convert a Python integer or long integer \var{pylong} to a C
369 \ctype{void} pointer. If \var{pylong} cannot be converted, an
370 \exception{OverflowError} will be raised. This is only assured to
371 produce a usable \ctype{void} pointer for values created with
372 \cfunction{PyLong_FromVoidPtr()}.
373 \versionadded{1.5.2}
374\end{cfuncdesc}
375
376
377\subsection{Floating Point Objects \label{floatObjects}}
378
379\obindex{floating point}
380\begin{ctypedesc}{PyFloatObject}
381 This subtype of \ctype{PyObject} represents a Python floating point
382 object.
383\end{ctypedesc}
384
385\begin{cvardesc}{PyTypeObject}{PyFloat_Type}
386 This instance of \ctype{PyTypeObject} represents the Python floating
387 point type. This is the same object as \code{types.FloatType}.
388 \withsubitem{(in modules types)}{\ttindex{FloatType}}
389\end{cvardesc}
390
391\begin{cfuncdesc}{int}{PyFloat_Check}{PyObject *p}
392 Returns true if its argument is a \ctype{PyFloatObject} or a subtype
393 of \ctype{PyFloatObject}.
394 \versionchanged[Allowed subtypes to be accepted]{2.2}
395\end{cfuncdesc}
396
397\begin{cfuncdesc}{int}{PyFloat_CheckExact}{PyObject *p}
398 Returns true if its argument is a \ctype{PyFloatObject}, but not a
399 subtype of \ctype{PyFloatObject}.
400 \versionadded{2.2}
401\end{cfuncdesc}
402
Skip Montanaro1ff49a72003-02-03 05:13:24 +0000403\begin{cfuncdesc}{PyObject*}{PyFloat_FromString}{PyObject *str, char **pend}
Skip Montanaroae31e9b2003-02-03 03:56:36 +0000404 Creates a \ctype{PyFloatObject} object based on the string value in
Skip Montanaro1ff49a72003-02-03 05:13:24 +0000405 \var{str}, or \NULL{} on failure. The \var{pend} argument is ignored. It
406 remains only for backward compatibility.
Skip Montanaroae31e9b2003-02-03 03:56:36 +0000407\end{cfuncdesc}
408
Fred Drake3adf79e2001-10-12 19:01:43 +0000409\begin{cfuncdesc}{PyObject*}{PyFloat_FromDouble}{double v}
410 Creates a \ctype{PyFloatObject} object from \var{v}, or \NULL{} on
411 failure.
412\end{cfuncdesc}
413
414\begin{cfuncdesc}{double}{PyFloat_AsDouble}{PyObject *pyfloat}
415 Returns a C \ctype{double} representation of the contents of
416 \var{pyfloat}.
417\end{cfuncdesc}
418
419\begin{cfuncdesc}{double}{PyFloat_AS_DOUBLE}{PyObject *pyfloat}
420 Returns a C \ctype{double} representation of the contents of
421 \var{pyfloat}, but without error checking.
422\end{cfuncdesc}
423
424
425\subsection{Complex Number Objects \label{complexObjects}}
426
427\obindex{complex number}
428Python's complex number objects are implemented as two distinct types
429when viewed from the C API: one is the Python object exposed to
430Python programs, and the other is a C structure which represents the
431actual complex number value. The API provides functions for working
432with both.
433
434\subsubsection{Complex Numbers as C Structures}
435
436Note that the functions which accept these structures as parameters
437and return them as results do so \emph{by value} rather than
438dereferencing them through pointers. This is consistent throughout
439the API.
440
441\begin{ctypedesc}{Py_complex}
442 The C structure which corresponds to the value portion of a Python
443 complex number object. Most of the functions for dealing with
444 complex number objects use structures of this type as input or
445 output values, as appropriate. It is defined as:
446
447\begin{verbatim}
448typedef struct {
449 double real;
450 double imag;
451} Py_complex;
452\end{verbatim}
453\end{ctypedesc}
454
455\begin{cfuncdesc}{Py_complex}{_Py_c_sum}{Py_complex left, Py_complex right}
456 Return the sum of two complex numbers, using the C
457 \ctype{Py_complex} representation.
458\end{cfuncdesc}
459
460\begin{cfuncdesc}{Py_complex}{_Py_c_diff}{Py_complex left, Py_complex right}
461 Return the difference between two complex numbers, using the C
462 \ctype{Py_complex} representation.
463\end{cfuncdesc}
464
465\begin{cfuncdesc}{Py_complex}{_Py_c_neg}{Py_complex complex}
466 Return the negation of the complex number \var{complex}, using the C
467 \ctype{Py_complex} representation.
468\end{cfuncdesc}
469
470\begin{cfuncdesc}{Py_complex}{_Py_c_prod}{Py_complex left, Py_complex right}
471 Return the product of two complex numbers, using the C
472 \ctype{Py_complex} representation.
473\end{cfuncdesc}
474
475\begin{cfuncdesc}{Py_complex}{_Py_c_quot}{Py_complex dividend,
476 Py_complex divisor}
477 Return the quotient of two complex numbers, using the C
478 \ctype{Py_complex} representation.
479\end{cfuncdesc}
480
481\begin{cfuncdesc}{Py_complex}{_Py_c_pow}{Py_complex num, Py_complex exp}
482 Return the exponentiation of \var{num} by \var{exp}, using the C
483 \ctype{Py_complex} representation.
484\end{cfuncdesc}
485
486
487\subsubsection{Complex Numbers as Python Objects}
488
489\begin{ctypedesc}{PyComplexObject}
490 This subtype of \ctype{PyObject} represents a Python complex number
491 object.
492\end{ctypedesc}
493
494\begin{cvardesc}{PyTypeObject}{PyComplex_Type}
495 This instance of \ctype{PyTypeObject} represents the Python complex
496 number type.
497\end{cvardesc}
498
499\begin{cfuncdesc}{int}{PyComplex_Check}{PyObject *p}
500 Returns true if its argument is a \ctype{PyComplexObject} or a
501 subtype of \ctype{PyComplexObject}.
502 \versionchanged[Allowed subtypes to be accepted]{2.2}
503\end{cfuncdesc}
504
505\begin{cfuncdesc}{int}{PyComplex_CheckExact}{PyObject *p}
506 Returns true if its argument is a \ctype{PyComplexObject}, but not a
507 subtype of \ctype{PyComplexObject}.
508 \versionadded{2.2}
509\end{cfuncdesc}
510
511\begin{cfuncdesc}{PyObject*}{PyComplex_FromCComplex}{Py_complex v}
512 Create a new Python complex number object from a C
513 \ctype{Py_complex} value.
514\end{cfuncdesc}
515
516\begin{cfuncdesc}{PyObject*}{PyComplex_FromDoubles}{double real, double imag}
517 Returns a new \ctype{PyComplexObject} object from \var{real} and
518 \var{imag}.
519\end{cfuncdesc}
520
521\begin{cfuncdesc}{double}{PyComplex_RealAsDouble}{PyObject *op}
522 Returns the real part of \var{op} as a C \ctype{double}.
523\end{cfuncdesc}
524
525\begin{cfuncdesc}{double}{PyComplex_ImagAsDouble}{PyObject *op}
526 Returns the imaginary part of \var{op} as a C \ctype{double}.
527\end{cfuncdesc}
528
529\begin{cfuncdesc}{Py_complex}{PyComplex_AsCComplex}{PyObject *op}
530 Returns the \ctype{Py_complex} value of the complex number
531 \var{op}.
532\end{cfuncdesc}
533
534
535
536\section{Sequence Objects \label{sequenceObjects}}
537
538\obindex{sequence}
Tim Petersf582b822001-12-11 18:51:08 +0000539Generic operations on sequence objects were discussed in the previous
540chapter; this section deals with the specific kinds of sequence
Fred Drake3adf79e2001-10-12 19:01:43 +0000541objects that are intrinsic to the Python language.
542
543
544\subsection{String Objects \label{stringObjects}}
545
546These functions raise \exception{TypeError} when expecting a string
547parameter and are called with a non-string parameter.
548
549\obindex{string}
550\begin{ctypedesc}{PyStringObject}
551 This subtype of \ctype{PyObject} represents a Python string object.
552\end{ctypedesc}
553
554\begin{cvardesc}{PyTypeObject}{PyString_Type}
555 This instance of \ctype{PyTypeObject} represents the Python string
556 type; it is the same object as \code{types.TypeType} in the Python
557 layer.
558 \withsubitem{(in module types)}{\ttindex{StringType}}.
559\end{cvardesc}
560
561\begin{cfuncdesc}{int}{PyString_Check}{PyObject *o}
562 Returns true if the object \var{o} is a string object or an instance
563 of a subtype of the string type.
564 \versionchanged[Allowed subtypes to be accepted]{2.2}
565\end{cfuncdesc}
566
567\begin{cfuncdesc}{int}{PyString_CheckExact}{PyObject *o}
568 Returns true if the object \var{o} is a string object, but not an
569 instance of a subtype of the string type.
570 \versionadded{2.2}
571\end{cfuncdesc}
572
573\begin{cfuncdesc}{PyObject*}{PyString_FromString}{const char *v}
574 Returns a new string object with the value \var{v} on success, and
Tim Peters9ddf40b2004-06-20 22:41:32 +0000575 \NULL{} on failure. The parameter \var{v} must not be \NULL{}; it
Fred Drake32a35872001-12-06 20:38:15 +0000576 will not be checked.
Fred Drake3adf79e2001-10-12 19:01:43 +0000577\end{cfuncdesc}
578
579\begin{cfuncdesc}{PyObject*}{PyString_FromStringAndSize}{const char *v,
580 int len}
581 Returns a new string object with the value \var{v} and length
582 \var{len} on success, and \NULL{} on failure. If \var{v} is
Tim Peters9ddf40b2004-06-20 22:41:32 +0000583 \NULL{}, the contents of the string are uninitialized.
Fred Drake3adf79e2001-10-12 19:01:43 +0000584\end{cfuncdesc}
585
586\begin{cfuncdesc}{PyObject*}{PyString_FromFormat}{const char *format, ...}
587 Takes a C \cfunction{printf()}-style \var{format} string and a
588 variable number of arguments, calculates the size of the resulting
589 Python string and returns a string with the values formatted into
590 it. The variable arguments must be C types and must correspond
591 exactly to the format characters in the \var{format} string. The
592 following format characters are allowed:
593
594 \begin{tableiii}{l|l|l}{member}{Format Characters}{Type}{Comment}
595 \lineiii{\%\%}{\emph{n/a}}{The literal \% character.}
596 \lineiii{\%c}{int}{A single character, represented as an C int.}
597 \lineiii{\%d}{int}{Exactly equivalent to \code{printf("\%d")}.}
598 \lineiii{\%ld}{long}{Exactly equivalent to \code{printf("\%ld")}.}
599 \lineiii{\%i}{int}{Exactly equivalent to \code{printf("\%i")}.}
600 \lineiii{\%x}{int}{Exactly equivalent to \code{printf("\%x")}.}
601 \lineiii{\%s}{char*}{A null-terminated C character array.}
602 \lineiii{\%p}{void*}{The hex representation of a C pointer.
603 Mostly equivalent to \code{printf("\%p")} except that it is
604 guaranteed to start with the literal \code{0x} regardless of
605 what the platform's \code{printf} yields.}
606 \end{tableiii}
607\end{cfuncdesc}
608
609\begin{cfuncdesc}{PyObject*}{PyString_FromFormatV}{const char *format,
610 va_list vargs}
611 Identical to \function{PyString_FromFormat()} except that it takes
612 exactly two arguments.
613\end{cfuncdesc}
614
615\begin{cfuncdesc}{int}{PyString_Size}{PyObject *string}
616 Returns the length of the string in string object \var{string}.
617\end{cfuncdesc}
618
619\begin{cfuncdesc}{int}{PyString_GET_SIZE}{PyObject *string}
620 Macro form of \cfunction{PyString_Size()} but without error
621 checking.
622\end{cfuncdesc}
623
624\begin{cfuncdesc}{char*}{PyString_AsString}{PyObject *string}
Fred Drake4b247262002-10-22 20:20:20 +0000625 Returns a NUL-terminated representation of the contents of
Fred Drake3adf79e2001-10-12 19:01:43 +0000626 \var{string}. The pointer refers to the internal buffer of
627 \var{string}, not a copy. The data must not be modified in any way,
628 unless the string was just created using
629 \code{PyString_FromStringAndSize(NULL, \var{size})}.
Fred Drake4b247262002-10-22 20:20:20 +0000630 It must not be deallocated. If \var{string} is a Unicode object,
631 this function computes the default encoding of \var{string} and
632 operates on that. If \var{string} is not a string object at all,
633 \cfunction{PyString_AsString()} returns \NULL{} and raises
634 \exception{TypeError}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000635\end{cfuncdesc}
636
637\begin{cfuncdesc}{char*}{PyString_AS_STRING}{PyObject *string}
638 Macro form of \cfunction{PyString_AsString()} but without error
Fred Drake4b247262002-10-22 20:20:20 +0000639 checking. Only string objects are supported; no Unicode objects
640 should be passed.
Fred Drake3adf79e2001-10-12 19:01:43 +0000641\end{cfuncdesc}
642
643\begin{cfuncdesc}{int}{PyString_AsStringAndSize}{PyObject *obj,
644 char **buffer,
645 int *length}
Fred Drake4b247262002-10-22 20:20:20 +0000646 Returns a NUL-terminated representation of the contents of the
Fred Drake3adf79e2001-10-12 19:01:43 +0000647 object \var{obj} through the output variables \var{buffer} and
648 \var{length}.
649
650 The function accepts both string and Unicode objects as input. For
651 Unicode objects it returns the default encoded version of the
Tim Peters9ddf40b2004-06-20 22:41:32 +0000652 object. If \var{length} is \NULL{}, the resulting buffer may not
Fred Drake4b247262002-10-22 20:20:20 +0000653 contain NUL characters; if it does, the function returns \code{-1}
654 and a \exception{TypeError} is raised.
Fred Drake3adf79e2001-10-12 19:01:43 +0000655
656 The buffer refers to an internal string buffer of \var{obj}, not a
657 copy. The data must not be modified in any way, unless the string
658 was just created using \code{PyString_FromStringAndSize(NULL,
Fred Drake4b247262002-10-22 20:20:20 +0000659 \var{size})}. It must not be deallocated. If \var{string} is a
660 Unicode object, this function computes the default encoding of
661 \var{string} and operates on that. If \var{string} is not a string
662 object at all, \cfunction{PyString_AsString()} returns \NULL{} and
663 raises \exception{TypeError}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000664\end{cfuncdesc}
665
666\begin{cfuncdesc}{void}{PyString_Concat}{PyObject **string,
667 PyObject *newpart}
668 Creates a new string object in \var{*string} containing the contents
669 of \var{newpart} appended to \var{string}; the caller will own the
670 new reference. The reference to the old value of \var{string} will
671 be stolen. If the new string cannot be created, the old reference
672 to \var{string} will still be discarded and the value of
Tim Peters9ddf40b2004-06-20 22:41:32 +0000673 \var{*string} will be set to \NULL{}; the appropriate exception will
Fred Drake3adf79e2001-10-12 19:01:43 +0000674 be set.
675\end{cfuncdesc}
676
677\begin{cfuncdesc}{void}{PyString_ConcatAndDel}{PyObject **string,
678 PyObject *newpart}
679 Creates a new string object in \var{*string} containing the contents
680 of \var{newpart} appended to \var{string}. This version decrements
681 the reference count of \var{newpart}.
682\end{cfuncdesc}
683
684\begin{cfuncdesc}{int}{_PyString_Resize}{PyObject **string, int newsize}
685 A way to resize a string object even though it is ``immutable''.
686 Only use this to build up a brand new string object; don't use this
Tim Peters5de98422002-04-27 18:44:32 +0000687 if the string may already be known in other parts of the code. It
688 is an error to call this function if the refcount on the input string
689 object is not one.
690 Pass the address of an existing string object as an lvalue (it may
691 be written into), and the new size desired. On success, \var{*string}
Fred Drake432425e2002-04-29 15:17:16 +0000692 holds the resized string object and \code{0} is returned; the address in
Tim Peters5de98422002-04-27 18:44:32 +0000693 \var{*string} may differ from its input value. If the
694 reallocation fails, the original string object at \var{*string} is
695 deallocated, \var{*string} is set to \NULL{}, a memory exception is set,
Fred Drake432425e2002-04-29 15:17:16 +0000696 and \code{-1} is returned.
Fred Drake3adf79e2001-10-12 19:01:43 +0000697\end{cfuncdesc}
698
699\begin{cfuncdesc}{PyObject*}{PyString_Format}{PyObject *format,
700 PyObject *args}
701 Returns a new string object from \var{format} and \var{args}.
702 Analogous to \code{\var{format} \%\ \var{args}}. The \var{args}
703 argument must be a tuple.
704\end{cfuncdesc}
705
706\begin{cfuncdesc}{void}{PyString_InternInPlace}{PyObject **string}
707 Intern the argument \var{*string} in place. The argument must be
708 the address of a pointer variable pointing to a Python string
709 object. If there is an existing interned string that is the same as
710 \var{*string}, it sets \var{*string} to it (decrementing the
711 reference count of the old string object and incrementing the
712 reference count of the interned string object), otherwise it leaves
713 \var{*string} alone and interns it (incrementing its reference
714 count). (Clarification: even though there is a lot of talk about
715 reference counts, think of this function as reference-count-neutral;
716 you own the object after the call if and only if you owned it before
717 the call.)
718\end{cfuncdesc}
719
720\begin{cfuncdesc}{PyObject*}{PyString_InternFromString}{const char *v}
721 A combination of \cfunction{PyString_FromString()} and
722 \cfunction{PyString_InternInPlace()}, returning either a new string
723 object that has been interned, or a new (``owned'') reference to an
724 earlier interned string object with the same value.
725\end{cfuncdesc}
726
727\begin{cfuncdesc}{PyObject*}{PyString_Decode}{const char *s,
728 int size,
729 const char *encoding,
730 const char *errors}
731 Creates an object by decoding \var{size} bytes of the encoded
732 buffer \var{s} using the codec registered for
733 \var{encoding}. \var{encoding} and \var{errors} have the same
734 meaning as the parameters of the same name in the
735 \function{unicode()} built-in function. The codec to be used is
736 looked up using the Python codec registry. Returns \NULL{} if
737 an exception was raised by the codec.
738\end{cfuncdesc}
739
740\begin{cfuncdesc}{PyObject*}{PyString_AsDecodedObject}{PyObject *str,
741 const char *encoding,
742 const char *errors}
743 Decodes a string object by passing it to the codec registered for
744 \var{encoding} and returns the result as Python
745 object. \var{encoding} and \var{errors} have the same meaning as the
746 parameters of the same name in the string \method{encode()} method.
747 The codec to be used is looked up using the Python codec registry.
748 Returns \NULL{} if an exception was raised by the codec.
749\end{cfuncdesc}
750
751\begin{cfuncdesc}{PyObject*}{PyString_Encode}{const char *s,
752 int size,
753 const char *encoding,
754 const char *errors}
755 Encodes the \ctype{char} buffer of the given size by passing it to
756 the codec registered for \var{encoding} and returns a Python object.
757 \var{encoding} and \var{errors} have the same meaning as the
758 parameters of the same name in the string \method{encode()} method.
759 The codec to be used is looked up using the Python codec
760 registry. Returns \NULL{} if an exception was raised by the
761 codec.
762\end{cfuncdesc}
763
764\begin{cfuncdesc}{PyObject*}{PyString_AsEncodedObject}{PyObject *str,
765 const char *encoding,
766 const char *errors}
767 Encodes a string object using the codec registered for
768 \var{encoding} and returns the result as Python object.
769 \var{encoding} and \var{errors} have the same meaning as the
770 parameters of the same name in the string \method{encode()} method.
771 The codec to be used is looked up using the Python codec registry.
772 Returns \NULL{} if an exception was raised by the codec.
773\end{cfuncdesc}
774
775
776\subsection{Unicode Objects \label{unicodeObjects}}
777\sectionauthor{Marc-Andre Lemburg}{mal@lemburg.com}
778
779%--- Unicode Type -------------------------------------------------------
780
781These are the basic Unicode object types used for the Unicode
782implementation in Python:
783
784\begin{ctypedesc}{Py_UNICODE}
785 This type represents a 16-bit unsigned storage type which is used by
786 Python internally as basis for holding Unicode ordinals. On
787 platforms where \ctype{wchar_t} is available and also has 16-bits,
788 \ctype{Py_UNICODE} is a typedef alias for \ctype{wchar_t} to enhance
789 native platform compatibility. On all other platforms,
790 \ctype{Py_UNICODE} is a typedef alias for \ctype{unsigned short}.
791\end{ctypedesc}
792
793\begin{ctypedesc}{PyUnicodeObject}
794 This subtype of \ctype{PyObject} represents a Python Unicode object.
795\end{ctypedesc}
796
797\begin{cvardesc}{PyTypeObject}{PyUnicode_Type}
798 This instance of \ctype{PyTypeObject} represents the Python Unicode
799 type.
800\end{cvardesc}
801
802The following APIs are really C macros and can be used to do fast
803checks and to access internal read-only data of Unicode objects:
804
805\begin{cfuncdesc}{int}{PyUnicode_Check}{PyObject *o}
806 Returns true if the object \var{o} is a Unicode object or an
807 instance of a Unicode subtype.
808 \versionchanged[Allowed subtypes to be accepted]{2.2}
809\end{cfuncdesc}
810
811\begin{cfuncdesc}{int}{PyUnicode_CheckExact}{PyObject *o}
812 Returns true if the object \var{o} is a Unicode object, but not an
813 instance of a subtype.
814 \versionadded{2.2}
815\end{cfuncdesc}
816
817\begin{cfuncdesc}{int}{PyUnicode_GET_SIZE}{PyObject *o}
818 Returns the size of the object. \var{o} has to be a
819 \ctype{PyUnicodeObject} (not checked).
820\end{cfuncdesc}
821
822\begin{cfuncdesc}{int}{PyUnicode_GET_DATA_SIZE}{PyObject *o}
823 Returns the size of the object's internal buffer in bytes. \var{o}
824 has to be a \ctype{PyUnicodeObject} (not checked).
825\end{cfuncdesc}
826
827\begin{cfuncdesc}{Py_UNICODE*}{PyUnicode_AS_UNICODE}{PyObject *o}
828 Returns a pointer to the internal \ctype{Py_UNICODE} buffer of the
829 object. \var{o} has to be a \ctype{PyUnicodeObject} (not checked).
830\end{cfuncdesc}
831
832\begin{cfuncdesc}{const char*}{PyUnicode_AS_DATA}{PyObject *o}
833 Returns a pointer to the internal buffer of the object.
834 \var{o} has to be a \ctype{PyUnicodeObject} (not checked).
835\end{cfuncdesc}
836
837% --- Unicode character properties ---------------------------------------
838
839Unicode provides many different character properties. The most often
840needed ones are available through these macros which are mapped to C
841functions depending on the Python configuration.
842
843\begin{cfuncdesc}{int}{Py_UNICODE_ISSPACE}{Py_UNICODE ch}
844 Returns 1/0 depending on whether \var{ch} is a whitespace
845 character.
846\end{cfuncdesc}
847
848\begin{cfuncdesc}{int}{Py_UNICODE_ISLOWER}{Py_UNICODE ch}
849 Returns 1/0 depending on whether \var{ch} is a lowercase character.
850\end{cfuncdesc}
851
852\begin{cfuncdesc}{int}{Py_UNICODE_ISUPPER}{Py_UNICODE ch}
853 Returns 1/0 depending on whether \var{ch} is an uppercase
854 character.
855\end{cfuncdesc}
856
857\begin{cfuncdesc}{int}{Py_UNICODE_ISTITLE}{Py_UNICODE ch}
858 Returns 1/0 depending on whether \var{ch} is a titlecase character.
859\end{cfuncdesc}
860
861\begin{cfuncdesc}{int}{Py_UNICODE_ISLINEBREAK}{Py_UNICODE ch}
862 Returns 1/0 depending on whether \var{ch} is a linebreak character.
863\end{cfuncdesc}
864
865\begin{cfuncdesc}{int}{Py_UNICODE_ISDECIMAL}{Py_UNICODE ch}
866 Returns 1/0 depending on whether \var{ch} is a decimal character.
867\end{cfuncdesc}
868
869\begin{cfuncdesc}{int}{Py_UNICODE_ISDIGIT}{Py_UNICODE ch}
870 Returns 1/0 depending on whether \var{ch} is a digit character.
871\end{cfuncdesc}
872
873\begin{cfuncdesc}{int}{Py_UNICODE_ISNUMERIC}{Py_UNICODE ch}
874 Returns 1/0 depending on whether \var{ch} is a numeric character.
875\end{cfuncdesc}
876
877\begin{cfuncdesc}{int}{Py_UNICODE_ISALPHA}{Py_UNICODE ch}
878 Returns 1/0 depending on whether \var{ch} is an alphabetic
879 character.
880\end{cfuncdesc}
881
882\begin{cfuncdesc}{int}{Py_UNICODE_ISALNUM}{Py_UNICODE ch}
883 Returns 1/0 depending on whether \var{ch} is an alphanumeric
884 character.
885\end{cfuncdesc}
886
Hye-Shik Chang974ed7c2004-06-02 16:49:17 +0000887\begin{cfuncdesc}{int}{Py_UNICODE_ISWIDE}{Py_UNICODE ch}
888 Returns 1/0 depending on whether \var{ch} is a wide or full-width
889 character.
890\end{cfuncdesc}
891
Fred Drake3adf79e2001-10-12 19:01:43 +0000892These APIs can be used for fast direct character conversions:
893
894\begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOLOWER}{Py_UNICODE ch}
895 Returns the character \var{ch} converted to lower case.
896\end{cfuncdesc}
897
898\begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOUPPER}{Py_UNICODE ch}
899 Returns the character \var{ch} converted to upper case.
900\end{cfuncdesc}
901
902\begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOTITLE}{Py_UNICODE ch}
903 Returns the character \var{ch} converted to title case.
904\end{cfuncdesc}
905
906\begin{cfuncdesc}{int}{Py_UNICODE_TODECIMAL}{Py_UNICODE ch}
907 Returns the character \var{ch} converted to a decimal positive
908 integer. Returns \code{-1} if this is not possible. Does not raise
909 exceptions.
910\end{cfuncdesc}
911
912\begin{cfuncdesc}{int}{Py_UNICODE_TODIGIT}{Py_UNICODE ch}
913 Returns the character \var{ch} converted to a single digit integer.
914 Returns \code{-1} if this is not possible. Does not raise
915 exceptions.
916\end{cfuncdesc}
917
918\begin{cfuncdesc}{double}{Py_UNICODE_TONUMERIC}{Py_UNICODE ch}
919 Returns the character \var{ch} converted to a (positive) double.
920 Returns \code{-1.0} if this is not possible. Does not raise
921 exceptions.
922\end{cfuncdesc}
923
924% --- Plain Py_UNICODE ---------------------------------------------------
925
926To create Unicode objects and access their basic sequence properties,
927use these APIs:
928
929\begin{cfuncdesc}{PyObject*}{PyUnicode_FromUnicode}{const Py_UNICODE *u,
Tim Petersf582b822001-12-11 18:51:08 +0000930 int size}
Fred Drake3adf79e2001-10-12 19:01:43 +0000931 Create a Unicode Object from the Py_UNICODE buffer \var{u} of the
932 given size. \var{u} may be \NULL{} which causes the contents to be
933 undefined. It is the user's responsibility to fill in the needed
934 data. The buffer is copied into the new object. If the buffer is
Tim Peters9ddf40b2004-06-20 22:41:32 +0000935 not \NULL{}, the return value might be a shared object. Therefore,
Fred Drake3adf79e2001-10-12 19:01:43 +0000936 modification of the resulting Unicode object is only allowed when
Tim Peters9ddf40b2004-06-20 22:41:32 +0000937 \var{u} is \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000938\end{cfuncdesc}
939
940\begin{cfuncdesc}{Py_UNICODE*}{PyUnicode_AsUnicode}{PyObject *unicode}
941 Return a read-only pointer to the Unicode object's internal
942 \ctype{Py_UNICODE} buffer, \NULL{} if \var{unicode} is not a Unicode
943 object.
944\end{cfuncdesc}
945
946\begin{cfuncdesc}{int}{PyUnicode_GetSize}{PyObject *unicode}
947 Return the length of the Unicode object.
948\end{cfuncdesc}
949
Hye-Shik Chang974ed7c2004-06-02 16:49:17 +0000950\begin{cfuncdesc}{int}{PyUnicode_GetWidth}{PyObject *unicode}
951 Return the fixed-width representation length of the Unicode object.
952\end{cfuncdesc}
953
Fred Drake3adf79e2001-10-12 19:01:43 +0000954\begin{cfuncdesc}{PyObject*}{PyUnicode_FromEncodedObject}{PyObject *obj,
955 const char *encoding,
956 const char *errors}
957 Coerce an encoded object \var{obj} to an Unicode object and return a
958 reference with incremented refcount.
959
960 Coercion is done in the following way:
961
962\begin{enumerate}
963\item Unicode objects are passed back as-is with incremented
964 refcount. \note{These cannot be decoded; passing a non-\NULL{}
965 value for encoding will result in a \exception{TypeError}.}
966
967\item String and other char buffer compatible objects are decoded
968 according to the given encoding and using the error handling
969 defined by errors. Both can be \NULL{} to have the interface
970 use the default values (see the next section for details).
971
972\item All other objects cause an exception.
973\end{enumerate}
974
975 The API returns \NULL{} if there was an error. The caller is
976 responsible for decref'ing the returned objects.
977\end{cfuncdesc}
978
979\begin{cfuncdesc}{PyObject*}{PyUnicode_FromObject}{PyObject *obj}
980 Shortcut for \code{PyUnicode_FromEncodedObject(obj, NULL, "strict")}
981 which is used throughout the interpreter whenever coercion to
982 Unicode is needed.
983\end{cfuncdesc}
984
985% --- wchar_t support for platforms which support it ---------------------
986
987If the platform supports \ctype{wchar_t} and provides a header file
988wchar.h, Python can interface directly to this type using the
989following functions. Support is optimized if Python's own
990\ctype{Py_UNICODE} type is identical to the system's \ctype{wchar_t}.
991
992\begin{cfuncdesc}{PyObject*}{PyUnicode_FromWideChar}{const wchar_t *w,
993 int size}
Thomas Heller541703b2002-04-29 17:28:43 +0000994 Create a Unicode object from the \ctype{wchar_t} buffer \var{w} of
Fred Drake3adf79e2001-10-12 19:01:43 +0000995 the given size. Returns \NULL{} on failure.
996\end{cfuncdesc}
997
998\begin{cfuncdesc}{int}{PyUnicode_AsWideChar}{PyUnicodeObject *unicode,
999 wchar_t *w,
1000 int size}
Thomas Heller541703b2002-04-29 17:28:43 +00001001 Copies the Unicode object contents into the \ctype{wchar_t} buffer
1002 \var{w}. At most \var{size} \ctype{wchar_t} characters are copied.
1003 Returns the number of \ctype{wchar_t} characters copied or -1 in
Fred Drake3adf79e2001-10-12 19:01:43 +00001004 case of an error.
1005\end{cfuncdesc}
1006
1007
1008\subsubsection{Built-in Codecs \label{builtinCodecs}}
1009
1010Python provides a set of builtin codecs which are written in C
1011for speed. All of these codecs are directly usable via the
1012following functions.
1013
1014Many of the following APIs take two arguments encoding and
1015errors. These parameters encoding and errors have the same semantics
1016as the ones of the builtin unicode() Unicode object constructor.
1017
1018Setting encoding to \NULL{} causes the default encoding to be used
1019which is \ASCII. The file system calls should use
1020\cdata{Py_FileSystemDefaultEncoding} as the encoding for file
1021names. This variable should be treated as read-only: On some systems,
1022it will be a pointer to a static string, on others, it will change at
Raymond Hettingercb2da432003-10-12 18:24:34 +00001023run-time (such as when the application invokes setlocale).
Fred Drake3adf79e2001-10-12 19:01:43 +00001024
1025Error handling is set by errors which may also be set to \NULL{}
1026meaning to use the default handling defined for the codec. Default
1027error handling for all builtin codecs is ``strict''
1028(\exception{ValueError} is raised).
1029
1030The codecs all use a similar interface. Only deviation from the
1031following generic ones are documented for simplicity.
1032
1033% --- Generic Codecs -----------------------------------------------------
1034
1035These are the generic codec APIs:
1036
1037\begin{cfuncdesc}{PyObject*}{PyUnicode_Decode}{const char *s,
1038 int size,
1039 const char *encoding,
1040 const char *errors}
1041 Create a Unicode object by decoding \var{size} bytes of the encoded
1042 string \var{s}. \var{encoding} and \var{errors} have the same
1043 meaning as the parameters of the same name in the
1044 \function{unicode()} builtin function. The codec to be used is
1045 looked up using the Python codec registry. Returns \NULL{} if an
1046 exception was raised by the codec.
1047\end{cfuncdesc}
1048
1049\begin{cfuncdesc}{PyObject*}{PyUnicode_Encode}{const Py_UNICODE *s,
1050 int size,
1051 const char *encoding,
1052 const char *errors}
1053 Encodes the \ctype{Py_UNICODE} buffer of the given size and returns
1054 a Python string object. \var{encoding} and \var{errors} have the
1055 same meaning as the parameters of the same name in the Unicode
1056 \method{encode()} method. The codec to be used is looked up using
1057 the Python codec registry. Returns \NULL{} if an exception was
1058 raised by the codec.
1059\end{cfuncdesc}
1060
1061\begin{cfuncdesc}{PyObject*}{PyUnicode_AsEncodedString}{PyObject *unicode,
1062 const char *encoding,
1063 const char *errors}
1064 Encodes a Unicode object and returns the result as Python string
1065 object. \var{encoding} and \var{errors} have the same meaning as the
1066 parameters of the same name in the Unicode \method{encode()} method.
1067 The codec to be used is looked up using the Python codec registry.
1068 Returns \NULL{} if an exception was raised by the codec.
1069\end{cfuncdesc}
1070
1071% --- UTF-8 Codecs -------------------------------------------------------
1072
1073These are the UTF-8 codec APIs:
1074
1075\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF8}{const char *s,
1076 int size,
1077 const char *errors}
1078 Creates a Unicode object by decoding \var{size} bytes of the UTF-8
1079 encoded string \var{s}. Returns \NULL{} if an exception was raised
1080 by the codec.
1081\end{cfuncdesc}
1082
1083\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUTF8}{const Py_UNICODE *s,
1084 int size,
1085 const char *errors}
1086 Encodes the \ctype{Py_UNICODE} buffer of the given size using UTF-8
1087 and returns a Python string object. Returns \NULL{} if an exception
1088 was raised by the codec.
1089\end{cfuncdesc}
1090
1091\begin{cfuncdesc}{PyObject*}{PyUnicode_AsUTF8String}{PyObject *unicode}
1092 Encodes a Unicode objects using UTF-8 and returns the result as
1093 Python string object. Error handling is ``strict''. Returns
1094 \NULL{} if an exception was raised by the codec.
1095\end{cfuncdesc}
1096
1097% --- UTF-16 Codecs ------------------------------------------------------ */
1098
1099These are the UTF-16 codec APIs:
1100
1101\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF16}{const char *s,
1102 int size,
1103 const char *errors,
1104 int *byteorder}
1105 Decodes \var{length} bytes from a UTF-16 encoded buffer string and
1106 returns the corresponding Unicode object. \var{errors} (if
Tim Peters9ddf40b2004-06-20 22:41:32 +00001107 non-\NULL{}) defines the error handling. It defaults to ``strict''.
Fred Drake3adf79e2001-10-12 19:01:43 +00001108
Tim Peters9ddf40b2004-06-20 22:41:32 +00001109 If \var{byteorder} is non-\NULL{}, the decoder starts decoding using
Fred Drake3adf79e2001-10-12 19:01:43 +00001110 the given byte order:
1111
1112\begin{verbatim}
1113 *byteorder == -1: little endian
1114 *byteorder == 0: native order
1115 *byteorder == 1: big endian
1116\end{verbatim}
1117
1118 and then switches according to all byte order marks (BOM) it finds
1119 in the input data. BOMs are not copied into the resulting Unicode
1120 string. After completion, \var{*byteorder} is set to the current
1121 byte order at the end of input data.
1122
Tim Peters9ddf40b2004-06-20 22:41:32 +00001123 If \var{byteorder} is \NULL{}, the codec starts in native order mode.
Fred Drake3adf79e2001-10-12 19:01:43 +00001124
1125 Returns \NULL{} if an exception was raised by the codec.
1126\end{cfuncdesc}
1127
1128\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUTF16}{const Py_UNICODE *s,
1129 int size,
1130 const char *errors,
1131 int byteorder}
1132 Returns a Python string object holding the UTF-16 encoded value of
1133 the Unicode data in \var{s}. If \var{byteorder} is not \code{0},
1134 output is written according to the following byte order:
1135
1136\begin{verbatim}
1137 byteorder == -1: little endian
1138 byteorder == 0: native byte order (writes a BOM mark)
1139 byteorder == 1: big endian
1140\end{verbatim}
1141
1142 If byteorder is \code{0}, the output string will always start with
1143 the Unicode BOM mark (U+FEFF). In the other two modes, no BOM mark
1144 is prepended.
1145
Martin v. Löwis9bc4f2d2004-06-03 09:55:28 +00001146 If \var{Py_UNICODE_WIDE} is defined, a single \ctype{Py_UNICODE}
1147 value may get represented as a surrogate pair. If it is not
1148 defined, each \ctype{Py_UNICODE} values is interpreted as an
1149 UCS-2 character.
Fred Drake3adf79e2001-10-12 19:01:43 +00001150
1151 Returns \NULL{} if an exception was raised by the codec.
1152\end{cfuncdesc}
1153
1154\begin{cfuncdesc}{PyObject*}{PyUnicode_AsUTF16String}{PyObject *unicode}
1155 Returns a Python string using the UTF-16 encoding in native byte
1156 order. The string always starts with a BOM mark. Error handling is
1157 ``strict''. Returns \NULL{} if an exception was raised by the
1158 codec.
1159\end{cfuncdesc}
1160
1161% --- Unicode-Escape Codecs ----------------------------------------------
1162
Martin v. Löwis95cf84a2003-10-19 07:32:24 +00001163These are the ``Unicode Escape'' codec APIs:
Fred Drake3adf79e2001-10-12 19:01:43 +00001164
1165\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUnicodeEscape}{const char *s,
1166 int size,
1167 const char *errors}
1168 Creates a Unicode object by decoding \var{size} bytes of the
1169 Unicode-Escape encoded string \var{s}. Returns \NULL{} if an
1170 exception was raised by the codec.
1171\end{cfuncdesc}
1172
1173\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUnicodeEscape}{const Py_UNICODE *s,
1174 int size,
1175 const char *errors}
1176 Encodes the \ctype{Py_UNICODE} buffer of the given size using
1177 Unicode-Escape and returns a Python string object. Returns \NULL{}
1178 if an exception was raised by the codec.
1179\end{cfuncdesc}
1180
1181\begin{cfuncdesc}{PyObject*}{PyUnicode_AsUnicodeEscapeString}{PyObject *unicode}
1182 Encodes a Unicode objects using Unicode-Escape and returns the
1183 result as Python string object. Error handling is ``strict''.
1184 Returns \NULL{} if an exception was raised by the codec.
1185\end{cfuncdesc}
1186
1187% --- Raw-Unicode-Escape Codecs ------------------------------------------
1188
Martin v. Löwis95cf84a2003-10-19 07:32:24 +00001189These are the ``Raw Unicode Escape'' codec APIs:
Fred Drake3adf79e2001-10-12 19:01:43 +00001190
1191\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeRawUnicodeEscape}{const char *s,
1192 int size,
1193 const char *errors}
1194 Creates a Unicode object by decoding \var{size} bytes of the
Martin v. Löwis95cf84a2003-10-19 07:32:24 +00001195 Raw-Unicode-Escape encoded string \var{s}. Returns \NULL{} if an
Fred Drake3adf79e2001-10-12 19:01:43 +00001196 exception was raised by the codec.
1197\end{cfuncdesc}
1198
1199\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeRawUnicodeEscape}{const Py_UNICODE *s,
1200 int size,
1201 const char *errors}
1202 Encodes the \ctype{Py_UNICODE} buffer of the given size using
1203 Raw-Unicode-Escape and returns a Python string object. Returns
1204 \NULL{} if an exception was raised by the codec.
1205\end{cfuncdesc}
1206
1207\begin{cfuncdesc}{PyObject*}{PyUnicode_AsRawUnicodeEscapeString}{PyObject *unicode}
1208 Encodes a Unicode objects using Raw-Unicode-Escape and returns the
1209 result as Python string object. Error handling is ``strict''.
1210 Returns \NULL{} if an exception was raised by the codec.
1211\end{cfuncdesc}
1212
Tim Petersf582b822001-12-11 18:51:08 +00001213% --- Latin-1 Codecs -----------------------------------------------------
Fred Drake3adf79e2001-10-12 19:01:43 +00001214
1215These are the Latin-1 codec APIs:
1216Latin-1 corresponds to the first 256 Unicode ordinals and only these
1217are accepted by the codecs during encoding.
1218
1219\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeLatin1}{const char *s,
1220 int size,
1221 const char *errors}
1222 Creates a Unicode object by decoding \var{size} bytes of the Latin-1
1223 encoded string \var{s}. Returns \NULL{} if an exception was raised
1224 by the codec.
1225\end{cfuncdesc}
1226
1227\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeLatin1}{const Py_UNICODE *s,
1228 int size,
1229 const char *errors}
1230 Encodes the \ctype{Py_UNICODE} buffer of the given size using
1231 Latin-1 and returns a Python string object. Returns \NULL{} if an
1232 exception was raised by the codec.
1233\end{cfuncdesc}
1234
1235\begin{cfuncdesc}{PyObject*}{PyUnicode_AsLatin1String}{PyObject *unicode}
1236 Encodes a Unicode objects using Latin-1 and returns the result as
1237 Python string object. Error handling is ``strict''. Returns
1238 \NULL{} if an exception was raised by the codec.
1239\end{cfuncdesc}
1240
Tim Petersf582b822001-12-11 18:51:08 +00001241% --- ASCII Codecs -------------------------------------------------------
Fred Drake3adf79e2001-10-12 19:01:43 +00001242
1243These are the \ASCII{} codec APIs. Only 7-bit \ASCII{} data is
1244accepted. All other codes generate errors.
1245
1246\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeASCII}{const char *s,
1247 int size,
1248 const char *errors}
1249 Creates a Unicode object by decoding \var{size} bytes of the
1250 \ASCII{} encoded string \var{s}. Returns \NULL{} if an exception
1251 was raised by the codec.
1252\end{cfuncdesc}
1253
1254\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeASCII}{const Py_UNICODE *s,
1255 int size,
1256 const char *errors}
1257 Encodes the \ctype{Py_UNICODE} buffer of the given size using
1258 \ASCII{} and returns a Python string object. Returns \NULL{} if an
1259 exception was raised by the codec.
1260\end{cfuncdesc}
1261
1262\begin{cfuncdesc}{PyObject*}{PyUnicode_AsASCIIString}{PyObject *unicode}
1263 Encodes a Unicode objects using \ASCII{} and returns the result as
1264 Python string object. Error handling is ``strict''. Returns
1265 \NULL{} if an exception was raised by the codec.
1266\end{cfuncdesc}
1267
Tim Petersf582b822001-12-11 18:51:08 +00001268% --- Character Map Codecs -----------------------------------------------
Fred Drake3adf79e2001-10-12 19:01:43 +00001269
1270These are the mapping codec APIs:
1271
1272This codec is special in that it can be used to implement many
1273different codecs (and this is in fact what was done to obtain most of
1274the standard codecs included in the \module{encodings} package). The
1275codec uses mapping to encode and decode characters.
1276
1277Decoding mappings must map single string characters to single Unicode
1278characters, integers (which are then interpreted as Unicode ordinals)
Tim Petersf582b822001-12-11 18:51:08 +00001279or None (meaning "undefined mapping" and causing an error).
Fred Drake3adf79e2001-10-12 19:01:43 +00001280
1281Encoding mappings must map single Unicode characters to single string
1282characters, integers (which are then interpreted as Latin-1 ordinals)
1283or None (meaning "undefined mapping" and causing an error).
1284
1285The mapping objects provided must only support the __getitem__ mapping
1286interface.
1287
1288If a character lookup fails with a LookupError, the character is
1289copied as-is meaning that its ordinal value will be interpreted as
1290Unicode or Latin-1 ordinal resp. Because of this, mappings only need
1291to contain those mappings which map characters to different code
1292points.
1293
1294\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeCharmap}{const char *s,
1295 int size,
1296 PyObject *mapping,
1297 const char *errors}
1298 Creates a Unicode object by decoding \var{size} bytes of the encoded
1299 string \var{s} using the given \var{mapping} object. Returns
1300 \NULL{} if an exception was raised by the codec.
1301\end{cfuncdesc}
1302
1303\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeCharmap}{const Py_UNICODE *s,
1304 int size,
1305 PyObject *mapping,
1306 const char *errors}
1307 Encodes the \ctype{Py_UNICODE} buffer of the given size using the
1308 given \var{mapping} object and returns a Python string object.
1309 Returns \NULL{} if an exception was raised by the codec.
1310\end{cfuncdesc}
1311
1312\begin{cfuncdesc}{PyObject*}{PyUnicode_AsCharmapString}{PyObject *unicode,
1313 PyObject *mapping}
1314 Encodes a Unicode objects using the given \var{mapping} object and
1315 returns the result as Python string object. Error handling is
1316 ``strict''. Returns \NULL{} if an exception was raised by the
1317 codec.
1318\end{cfuncdesc}
1319
1320The following codec API is special in that maps Unicode to Unicode.
1321
1322\begin{cfuncdesc}{PyObject*}{PyUnicode_TranslateCharmap}{const Py_UNICODE *s,
1323 int size,
1324 PyObject *table,
1325 const char *errors}
1326 Translates a \ctype{Py_UNICODE} buffer of the given length by
1327 applying a character mapping \var{table} to it and returns the
1328 resulting Unicode object. Returns \NULL{} when an exception was
1329 raised by the codec.
1330
1331 The \var{mapping} table must map Unicode ordinal integers to Unicode
1332 ordinal integers or None (causing deletion of the character).
1333
1334 Mapping tables need only provide the method{__getitem__()}
1335 interface; dictionaries and sequences work well. Unmapped character
1336 ordinals (ones which cause a \exception{LookupError}) are left
1337 untouched and are copied as-is.
1338\end{cfuncdesc}
1339
1340% --- MBCS codecs for Windows --------------------------------------------
1341
1342These are the MBCS codec APIs. They are currently only available on
1343Windows and use the Win32 MBCS converters to implement the
1344conversions. Note that MBCS (or DBCS) is a class of encodings, not
1345just one. The target encoding is defined by the user settings on the
1346machine running the codec.
1347
1348\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeMBCS}{const char *s,
1349 int size,
1350 const char *errors}
1351 Creates a Unicode object by decoding \var{size} bytes of the MBCS
1352 encoded string \var{s}. Returns \NULL{} if an exception was
1353 raised by the codec.
1354\end{cfuncdesc}
1355
1356\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeMBCS}{const Py_UNICODE *s,
1357 int size,
1358 const char *errors}
1359 Encodes the \ctype{Py_UNICODE} buffer of the given size using MBCS
1360 and returns a Python string object. Returns \NULL{} if an exception
1361 was raised by the codec.
1362\end{cfuncdesc}
1363
1364\begin{cfuncdesc}{PyObject*}{PyUnicode_AsMBCSString}{PyObject *unicode}
1365 Encodes a Unicode objects using MBCS and returns the result as
1366 Python string object. Error handling is ``strict''. Returns
1367 \NULL{} if an exception was raised by the codec.
1368\end{cfuncdesc}
1369
1370% --- Methods & Slots ----------------------------------------------------
1371
1372\subsubsection{Methods and Slot Functions \label{unicodeMethodsAndSlots}}
1373
1374The following APIs are capable of handling Unicode objects and strings
1375on input (we refer to them as strings in the descriptions) and return
Martin v. Löwis95cf84a2003-10-19 07:32:24 +00001376Unicode objects or integers as appropriate.
Fred Drake3adf79e2001-10-12 19:01:43 +00001377
1378They all return \NULL{} or \code{-1} if an exception occurs.
1379
1380\begin{cfuncdesc}{PyObject*}{PyUnicode_Concat}{PyObject *left,
1381 PyObject *right}
1382 Concat two strings giving a new Unicode string.
1383\end{cfuncdesc}
1384
1385\begin{cfuncdesc}{PyObject*}{PyUnicode_Split}{PyObject *s,
1386 PyObject *sep,
1387 int maxsplit}
Tim Peters9ddf40b2004-06-20 22:41:32 +00001388 Split a string giving a list of Unicode strings. If sep is \NULL{},
Fred Drake3adf79e2001-10-12 19:01:43 +00001389 splitting will be done at all whitespace substrings. Otherwise,
1390 splits occur at the given separator. At most \var{maxsplit} splits
1391 will be done. If negative, no limit is set. Separators are not
1392 included in the resulting list.
1393\end{cfuncdesc}
1394
1395\begin{cfuncdesc}{PyObject*}{PyUnicode_Splitlines}{PyObject *s,
Martin v. Löwis24b88812003-03-30 16:40:42 +00001396 int keepend}
Fred Drake3adf79e2001-10-12 19:01:43 +00001397 Split a Unicode string at line breaks, returning a list of Unicode
Martin v. Löwis24b88812003-03-30 16:40:42 +00001398 strings. CRLF is considered to be one line break. If \var{keepend}
1399 is 0, the Line break characters are not included in the resulting
1400 strings.
Fred Drake3adf79e2001-10-12 19:01:43 +00001401\end{cfuncdesc}
1402
1403\begin{cfuncdesc}{PyObject*}{PyUnicode_Translate}{PyObject *str,
1404 PyObject *table,
1405 const char *errors}
1406 Translate a string by applying a character mapping table to it and
1407 return the resulting Unicode object.
1408
1409 The mapping table must map Unicode ordinal integers to Unicode
1410 ordinal integers or None (causing deletion of the character).
1411
1412 Mapping tables need only provide the \method{__getitem__()}
1413 interface; dictionaries and sequences work well. Unmapped character
1414 ordinals (ones which cause a \exception{LookupError}) are left
1415 untouched and are copied as-is.
1416
1417 \var{errors} has the usual meaning for codecs. It may be \NULL{}
1418 which indicates to use the default error handling.
1419\end{cfuncdesc}
1420
1421\begin{cfuncdesc}{PyObject*}{PyUnicode_Join}{PyObject *separator,
1422 PyObject *seq}
1423 Join a sequence of strings using the given separator and return the
1424 resulting Unicode string.
1425\end{cfuncdesc}
1426
1427\begin{cfuncdesc}{PyObject*}{PyUnicode_Tailmatch}{PyObject *str,
1428 PyObject *substr,
1429 int start,
1430 int end,
1431 int direction}
1432 Return 1 if \var{substr} matches \var{str}[\var{start}:\var{end}] at
1433 the given tail end (\var{direction} == -1 means to do a prefix
1434 match, \var{direction} == 1 a suffix match), 0 otherwise.
1435\end{cfuncdesc}
1436
Fred Drake1d1e1db2002-06-20 22:07:04 +00001437\begin{cfuncdesc}{int}{PyUnicode_Find}{PyObject *str,
1438 PyObject *substr,
1439 int start,
1440 int end,
1441 int direction}
Fred Drake3adf79e2001-10-12 19:01:43 +00001442 Return the first position of \var{substr} in
1443 \var{str}[\var{start}:\var{end}] using the given \var{direction}
1444 (\var{direction} == 1 means to do a forward search,
Fred Drake1d1e1db2002-06-20 22:07:04 +00001445 \var{direction} == -1 a backward search). The return value is the
1446 index of the first match; a value of \code{-1} indicates that no
1447 match was found, and \code{-2} indicates that an error occurred and
1448 an exception has been set.
Fred Drake3adf79e2001-10-12 19:01:43 +00001449\end{cfuncdesc}
1450
Fred Drake1d1e1db2002-06-20 22:07:04 +00001451\begin{cfuncdesc}{int}{PyUnicode_Count}{PyObject *str,
1452 PyObject *substr,
1453 int start,
1454 int end}
1455 Return the number of non-overlapping occurrences of \var{substr} in
1456 \code{\var{str}[\var{start}:\var{end}]}. Returns \code{-1} if an
1457 error occurred.
Fred Drake3adf79e2001-10-12 19:01:43 +00001458\end{cfuncdesc}
1459
1460\begin{cfuncdesc}{PyObject*}{PyUnicode_Replace}{PyObject *str,
1461 PyObject *substr,
1462 PyObject *replstr,
1463 int maxcount}
1464 Replace at most \var{maxcount} occurrences of \var{substr} in
1465 \var{str} with \var{replstr} and return the resulting Unicode object.
1466 \var{maxcount} == -1 means replace all occurrences.
1467\end{cfuncdesc}
1468
1469\begin{cfuncdesc}{int}{PyUnicode_Compare}{PyObject *left, PyObject *right}
1470 Compare two strings and return -1, 0, 1 for less than, equal, and
1471 greater than, respectively.
1472\end{cfuncdesc}
1473
1474\begin{cfuncdesc}{PyObject*}{PyUnicode_Format}{PyObject *format,
1475 PyObject *args}
1476 Returns a new string object from \var{format} and \var{args}; this
1477 is analogous to \code{\var{format} \%\ \var{args}}. The
1478 \var{args} argument must be a tuple.
1479\end{cfuncdesc}
1480
1481\begin{cfuncdesc}{int}{PyUnicode_Contains}{PyObject *container,
1482 PyObject *element}
1483 Checks whether \var{element} is contained in \var{container} and
1484 returns true or false accordingly.
1485
1486 \var{element} has to coerce to a one element Unicode
1487 string. \code{-1} is returned if there was an error.
1488\end{cfuncdesc}
1489
1490
1491\subsection{Buffer Objects \label{bufferObjects}}
1492\sectionauthor{Greg Stein}{gstein@lyra.org}
1493
1494\obindex{buffer}
1495Python objects implemented in C can export a group of functions called
1496the ``buffer\index{buffer interface} interface.'' These functions can
1497be used by an object to expose its data in a raw, byte-oriented
1498format. Clients of the object can use the buffer interface to access
1499the object data directly, without needing to copy it first.
1500
Tim Petersf582b822001-12-11 18:51:08 +00001501Two examples of objects that support
1502the buffer interface are strings and arrays. The string object exposes
Fred Drake3adf79e2001-10-12 19:01:43 +00001503the character contents in the buffer interface's byte-oriented
1504form. An array can also expose its contents, but it should be noted
1505that array elements may be multi-byte values.
1506
1507An example user of the buffer interface is the file object's
1508\method{write()} method. Any object that can export a series of bytes
1509through the buffer interface can be written to a file. There are a
Tim Petersf582b822001-12-11 18:51:08 +00001510number of format codes to \cfunction{PyArg_ParseTuple()} that operate
Fred Drake3adf79e2001-10-12 19:01:43 +00001511against an object's buffer interface, returning data from the target
1512object.
1513
1514More information on the buffer interface is provided in the section
Fred Drake54e62942001-12-11 19:40:16 +00001515``Buffer Object Structures'' (section~\ref{buffer-structs}), under
Fred Drake3adf79e2001-10-12 19:01:43 +00001516the description for \ctype{PyBufferProcs}\ttindex{PyBufferProcs}.
1517
1518A ``buffer object'' is defined in the \file{bufferobject.h} header
1519(included by \file{Python.h}). These objects look very similar to
1520string objects at the Python programming level: they support slicing,
1521indexing, concatenation, and some other standard string
1522operations. However, their data can come from one of two sources: from
1523a block of memory, or from another object which exports the buffer
1524interface.
1525
1526Buffer objects are useful as a way to expose the data from another
1527object's buffer interface to the Python programmer. They can also be
1528used as a zero-copy slicing mechanism. Using their ability to
1529reference a block of memory, it is possible to expose any data to the
1530Python programmer quite easily. The memory could be a large, constant
1531array in a C extension, it could be a raw block of memory for
1532manipulation before passing to an operating system library, or it
1533could be used to pass around structured data in its native, in-memory
1534format.
1535
1536\begin{ctypedesc}{PyBufferObject}
1537 This subtype of \ctype{PyObject} represents a buffer object.
1538\end{ctypedesc}
1539
1540\begin{cvardesc}{PyTypeObject}{PyBuffer_Type}
1541 The instance of \ctype{PyTypeObject} which represents the Python
1542 buffer type; it is the same object as \code{types.BufferType} in the
1543 Python layer.\withsubitem{(in module types)}{\ttindex{BufferType}}.
1544\end{cvardesc}
1545
1546\begin{cvardesc}{int}{Py_END_OF_BUFFER}
1547 This constant may be passed as the \var{size} parameter to
1548 \cfunction{PyBuffer_FromObject()} or
1549 \cfunction{PyBuffer_FromReadWriteObject()}. It indicates that the
1550 new \ctype{PyBufferObject} should refer to \var{base} object from
1551 the specified \var{offset} to the end of its exported buffer. Using
1552 this enables the caller to avoid querying the \var{base} object for
1553 its length.
1554\end{cvardesc}
1555
1556\begin{cfuncdesc}{int}{PyBuffer_Check}{PyObject *p}
1557 Return true if the argument has type \cdata{PyBuffer_Type}.
1558\end{cfuncdesc}
1559
1560\begin{cfuncdesc}{PyObject*}{PyBuffer_FromObject}{PyObject *base,
1561 int offset, int size}
1562 Return a new read-only buffer object. This raises
1563 \exception{TypeError} if \var{base} doesn't support the read-only
1564 buffer protocol or doesn't provide exactly one buffer segment, or it
1565 raises \exception{ValueError} if \var{offset} is less than zero. The
1566 buffer will hold a reference to the \var{base} object, and the
1567 buffer's contents will refer to the \var{base} object's buffer
1568 interface, starting as position \var{offset} and extending for
1569 \var{size} bytes. If \var{size} is \constant{Py_END_OF_BUFFER}, then
1570 the new buffer's contents extend to the length of the \var{base}
1571 object's exported buffer data.
1572\end{cfuncdesc}
1573
1574\begin{cfuncdesc}{PyObject*}{PyBuffer_FromReadWriteObject}{PyObject *base,
1575 int offset,
1576 int size}
1577 Return a new writable buffer object. Parameters and exceptions are
1578 similar to those for \cfunction{PyBuffer_FromObject()}. If the
1579 \var{base} object does not export the writeable buffer protocol,
1580 then \exception{TypeError} is raised.
1581\end{cfuncdesc}
1582
1583\begin{cfuncdesc}{PyObject*}{PyBuffer_FromMemory}{void *ptr, int size}
1584 Return a new read-only buffer object that reads from a specified
1585 location in memory, with a specified size. The caller is
1586 responsible for ensuring that the memory buffer, passed in as
1587 \var{ptr}, is not deallocated while the returned buffer object
1588 exists. Raises \exception{ValueError} if \var{size} is less than
1589 zero. Note that \constant{Py_END_OF_BUFFER} may \emph{not} be
1590 passed for the \var{size} parameter; \exception{ValueError} will be
1591 raised in that case.
1592\end{cfuncdesc}
1593
1594\begin{cfuncdesc}{PyObject*}{PyBuffer_FromReadWriteMemory}{void *ptr, int size}
1595 Similar to \cfunction{PyBuffer_FromMemory()}, but the returned
1596 buffer is writable.
1597\end{cfuncdesc}
1598
1599\begin{cfuncdesc}{PyObject*}{PyBuffer_New}{int size}
1600 Returns a new writable buffer object that maintains its own memory
1601 buffer of \var{size} bytes. \exception{ValueError} is returned if
Neil Schemenauerd68d3ee2004-06-08 02:58:50 +00001602 \var{size} is not zero or positive. Note that the memory buffer (as
1603 returned by \cfunction{PyObject_AsWriteBuffer()}) is not specifically
1604 aligned.
Fred Drake3adf79e2001-10-12 19:01:43 +00001605\end{cfuncdesc}
1606
1607
1608\subsection{Tuple Objects \label{tupleObjects}}
1609
1610\obindex{tuple}
1611\begin{ctypedesc}{PyTupleObject}
1612 This subtype of \ctype{PyObject} represents a Python tuple object.
1613\end{ctypedesc}
1614
1615\begin{cvardesc}{PyTypeObject}{PyTuple_Type}
1616 This instance of \ctype{PyTypeObject} represents the Python tuple
1617 type; it is the same object as \code{types.TupleType} in the Python
1618 layer.\withsubitem{(in module types)}{\ttindex{TupleType}}.
1619\end{cvardesc}
1620
1621\begin{cfuncdesc}{int}{PyTuple_Check}{PyObject *p}
1622 Return true if \var{p} is a tuple object or an instance of a subtype
1623 of the tuple type.
1624 \versionchanged[Allowed subtypes to be accepted]{2.2}
1625\end{cfuncdesc}
1626
1627\begin{cfuncdesc}{int}{PyTuple_CheckExact}{PyObject *p}
1628 Return true if \var{p} is a tuple object, but not an instance of a
1629 subtype of the tuple type.
1630 \versionadded{2.2}
1631\end{cfuncdesc}
1632
1633\begin{cfuncdesc}{PyObject*}{PyTuple_New}{int len}
1634 Return a new tuple object of size \var{len}, or \NULL{} on failure.
1635\end{cfuncdesc}
1636
Raymond Hettingercb2da432003-10-12 18:24:34 +00001637\begin{cfuncdesc}{PyObject*}{PyTuple_Pack}{int n, \moreargs}
1638 Return a new tuple object of size \var{n}, or \NULL{} on failure.
1639 The tuple values are initialized to the subsequent \var{n} C arguments
1640 pointing to Python objects. \samp{PyTuple_Pack(2, \var{a}, \var{b})}
1641 is equivalent to \samp{Py_BuildValue("(OO)", \var{a}, \var{b})}.
Tim Peters9ddf40b2004-06-20 22:41:32 +00001642 \versionadded{2.4}
Raymond Hettingercb2da432003-10-12 18:24:34 +00001643\end{cfuncdesc}
1644
Fred Drake3adf79e2001-10-12 19:01:43 +00001645\begin{cfuncdesc}{int}{PyTuple_Size}{PyObject *p}
1646 Takes a pointer to a tuple object, and returns the size of that
1647 tuple.
1648\end{cfuncdesc}
1649
1650\begin{cfuncdesc}{int}{PyTuple_GET_SIZE}{PyObject *p}
1651 Return the size of the tuple \var{p}, which must be non-\NULL{} and
1652 point to a tuple; no error checking is performed.
1653\end{cfuncdesc}
1654
1655\begin{cfuncdesc}{PyObject*}{PyTuple_GetItem}{PyObject *p, int pos}
1656 Returns the object at position \var{pos} in the tuple pointed to by
1657 \var{p}. If \var{pos} is out of bounds, returns \NULL{} and sets an
1658 \exception{IndexError} exception.
1659\end{cfuncdesc}
1660
1661\begin{cfuncdesc}{PyObject*}{PyTuple_GET_ITEM}{PyObject *p, int pos}
1662 Like \cfunction{PyTuple_GetItem()}, but does no checking of its
1663 arguments.
1664\end{cfuncdesc}
1665
1666\begin{cfuncdesc}{PyObject*}{PyTuple_GetSlice}{PyObject *p,
1667 int low, int high}
1668 Takes a slice of the tuple pointed to by \var{p} from \var{low} to
1669 \var{high} and returns it as a new tuple.
1670\end{cfuncdesc}
1671
1672\begin{cfuncdesc}{int}{PyTuple_SetItem}{PyObject *p,
1673 int pos, PyObject *o}
1674 Inserts a reference to object \var{o} at position \var{pos} of the
1675 tuple pointed to by \var{p}. It returns \code{0} on success.
1676 \note{This function ``steals'' a reference to \var{o}.}
1677\end{cfuncdesc}
1678
1679\begin{cfuncdesc}{void}{PyTuple_SET_ITEM}{PyObject *p,
1680 int pos, PyObject *o}
1681 Like \cfunction{PyTuple_SetItem()}, but does no error checking, and
1682 should \emph{only} be used to fill in brand new tuples. \note{This
1683 function ``steals'' a reference to \var{o}.}
1684\end{cfuncdesc}
1685
1686\begin{cfuncdesc}{int}{_PyTuple_Resize}{PyObject **p, int newsize}
1687 Can be used to resize a tuple. \var{newsize} will be the new length
1688 of the tuple. Because tuples are \emph{supposed} to be immutable,
1689 this should only be used if there is only one reference to the
1690 object. Do \emph{not} use this if the tuple may already be known to
1691 some other part of the code. The tuple will always grow or shrink
1692 at the end. Think of this as destroying the old tuple and creating
1693 a new one, only more efficiently. Returns \code{0} on success.
1694 Client code should never assume that the resulting value of
1695 \code{*\var{p}} will be the same as before calling this function.
1696 If the object referenced by \code{*\var{p}} is replaced, the
1697 original \code{*\var{p}} is destroyed. On failure, returns
Tim Peters9ddf40b2004-06-20 22:41:32 +00001698 \code{-1} and sets \code{*\var{p}} to \NULL{}, and raises
Fred Drake3adf79e2001-10-12 19:01:43 +00001699 \exception{MemoryError} or
1700 \exception{SystemError}.
Tim Petersf582b822001-12-11 18:51:08 +00001701 \versionchanged[Removed unused third parameter, \var{last_is_sticky}]{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +00001702\end{cfuncdesc}
1703
1704
1705\subsection{List Objects \label{listObjects}}
1706
1707\obindex{list}
1708\begin{ctypedesc}{PyListObject}
1709 This subtype of \ctype{PyObject} represents a Python list object.
1710\end{ctypedesc}
1711
1712\begin{cvardesc}{PyTypeObject}{PyList_Type}
1713 This instance of \ctype{PyTypeObject} represents the Python list
1714 type. This is the same object as \code{types.ListType}.
1715 \withsubitem{(in module types)}{\ttindex{ListType}}
1716\end{cvardesc}
1717
1718\begin{cfuncdesc}{int}{PyList_Check}{PyObject *p}
Andrew MacIntyre13cd8892003-12-25 23:57:52 +00001719 Returns true if \var{p} is a list object or an instance of a
1720 subtype of the list type.
1721 \versionchanged[Allowed subtypes to be accepted]{2.2}
1722\end{cfuncdesc}
1723
1724\begin{cfuncdesc}{int}{PyList_CheckExact}{PyObject *p}
1725 Return true if \var{p} is a list object, but not an instance of a
1726 subtype of the list type.
1727 \versionadded{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +00001728\end{cfuncdesc}
1729
1730\begin{cfuncdesc}{PyObject*}{PyList_New}{int len}
1731 Returns a new list of length \var{len} on success, or \NULL{} on
1732 failure.
1733\end{cfuncdesc}
1734
1735\begin{cfuncdesc}{int}{PyList_Size}{PyObject *list}
1736 Returns the length of the list object in \var{list}; this is
1737 equivalent to \samp{len(\var{list})} on a list object.
1738 \bifuncindex{len}
1739\end{cfuncdesc}
1740
1741\begin{cfuncdesc}{int}{PyList_GET_SIZE}{PyObject *list}
1742 Macro form of \cfunction{PyList_Size()} without error checking.
1743\end{cfuncdesc}
1744
1745\begin{cfuncdesc}{PyObject*}{PyList_GetItem}{PyObject *list, int index}
1746 Returns the object at position \var{pos} in the list pointed to by
1747 \var{p}. If \var{pos} is out of bounds, returns \NULL{} and sets an
1748 \exception{IndexError} exception.
1749\end{cfuncdesc}
1750
1751\begin{cfuncdesc}{PyObject*}{PyList_GET_ITEM}{PyObject *list, int i}
1752 Macro form of \cfunction{PyList_GetItem()} without error checking.
1753\end{cfuncdesc}
1754
1755\begin{cfuncdesc}{int}{PyList_SetItem}{PyObject *list, int index,
1756 PyObject *item}
1757 Sets the item at index \var{index} in list to \var{item}. Returns
1758 \code{0} on success or \code{-1} on failure. \note{This function
1759 ``steals'' a reference to \var{item} and discards a reference to an
1760 item already in the list at the affected position.}
1761\end{cfuncdesc}
1762
1763\begin{cfuncdesc}{void}{PyList_SET_ITEM}{PyObject *list, int i,
1764 PyObject *o}
1765 Macro form of \cfunction{PyList_SetItem()} without error checking.
1766 This is normally only used to fill in new lists where there is no
1767 previous content.
1768 \note{This function ``steals'' a reference to \var{item}, and,
1769 unlike \cfunction{PyList_SetItem()}, does \emph{not} discard a
1770 reference to any item that it being replaced; any reference in
1771 \var{list} at position \var{i} will be leaked.}
1772\end{cfuncdesc}
1773
1774\begin{cfuncdesc}{int}{PyList_Insert}{PyObject *list, int index,
1775 PyObject *item}
1776 Inserts the item \var{item} into list \var{list} in front of index
1777 \var{index}. Returns \code{0} if successful; returns \code{-1} and
1778 raises an exception if unsuccessful. Analogous to
1779 \code{\var{list}.insert(\var{index}, \var{item})}.
1780\end{cfuncdesc}
1781
1782\begin{cfuncdesc}{int}{PyList_Append}{PyObject *list, PyObject *item}
1783 Appends the object \var{item} at the end of list \var{list}.
1784 Returns \code{0} if successful; returns \code{-1} and sets an
1785 exception if unsuccessful. Analogous to
1786 \code{\var{list}.append(\var{item})}.
1787\end{cfuncdesc}
1788
1789\begin{cfuncdesc}{PyObject*}{PyList_GetSlice}{PyObject *list,
1790 int low, int high}
1791 Returns a list of the objects in \var{list} containing the objects
1792 \emph{between} \var{low} and \var{high}. Returns \NULL{} and sets
1793 an exception if unsuccessful.
1794 Analogous to \code{\var{list}[\var{low}:\var{high}]}.
1795\end{cfuncdesc}
1796
1797\begin{cfuncdesc}{int}{PyList_SetSlice}{PyObject *list,
1798 int low, int high,
1799 PyObject *itemlist}
1800 Sets the slice of \var{list} between \var{low} and \var{high} to the
1801 contents of \var{itemlist}. Analogous to
Raymond Hettinger9c7ed4c2003-10-26 17:20:07 +00001802 \code{\var{list}[\var{low}:\var{high}] = \var{itemlist}}.
1803 The \var{itemlist} may be \NULL{}, indicating the assignment
1804 of an empty list (slice deletion).
1805 Returns \code{0} on success, \code{-1} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00001806\end{cfuncdesc}
1807
1808\begin{cfuncdesc}{int}{PyList_Sort}{PyObject *list}
1809 Sorts the items of \var{list} in place. Returns \code{0} on
1810 success, \code{-1} on failure. This is equivalent to
1811 \samp{\var{list}.sort()}.
1812\end{cfuncdesc}
1813
1814\begin{cfuncdesc}{int}{PyList_Reverse}{PyObject *list}
1815 Reverses the items of \var{list} in place. Returns \code{0} on
1816 success, \code{-1} on failure. This is the equivalent of
1817 \samp{\var{list}.reverse()}.
1818\end{cfuncdesc}
1819
1820\begin{cfuncdesc}{PyObject*}{PyList_AsTuple}{PyObject *list}
1821 Returns a new tuple object containing the contents of \var{list};
1822 equivalent to \samp{tuple(\var{list})}.\bifuncindex{tuple}
1823\end{cfuncdesc}
1824
1825
1826\section{Mapping Objects \label{mapObjects}}
1827
1828\obindex{mapping}
1829
1830
1831\subsection{Dictionary Objects \label{dictObjects}}
1832
1833\obindex{dictionary}
1834\begin{ctypedesc}{PyDictObject}
1835 This subtype of \ctype{PyObject} represents a Python dictionary
1836 object.
1837\end{ctypedesc}
1838
1839\begin{cvardesc}{PyTypeObject}{PyDict_Type}
1840 This instance of \ctype{PyTypeObject} represents the Python
1841 dictionary type. This is exposed to Python programs as
1842 \code{types.DictType} and \code{types.DictionaryType}.
1843 \withsubitem{(in module types)}{\ttindex{DictType}\ttindex{DictionaryType}}
1844\end{cvardesc}
1845
1846\begin{cfuncdesc}{int}{PyDict_Check}{PyObject *p}
Andrew MacIntyre13cd8892003-12-25 23:57:52 +00001847 Returns true if \var{p} is a dict object or an instance of a
1848 subtype of the dict type.
1849 \versionchanged[Allowed subtypes to be accepted]{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +00001850\end{cfuncdesc}
1851
Andrew MacIntyref72af652003-12-26 00:07:51 +00001852\begin{cfuncdesc}{int}{PyDict_CheckExact}{PyObject *p}
1853 Return true if \var{p} is a dict object, but not an instance of a
1854 subtype of the dict type.
1855 \versionadded{2.4}
1856\end{cfuncdesc}
1857
Fred Drake3adf79e2001-10-12 19:01:43 +00001858\begin{cfuncdesc}{PyObject*}{PyDict_New}{}
1859 Returns a new empty dictionary, or \NULL{} on failure.
1860\end{cfuncdesc}
1861
1862\begin{cfuncdesc}{PyObject*}{PyDictProxy_New}{PyObject *dict}
1863 Return a proxy object for a mapping which enforces read-only
1864 behavior. This is normally used to create a proxy to prevent
1865 modification of the dictionary for non-dynamic class types.
1866 \versionadded{2.2}
1867\end{cfuncdesc}
1868
1869\begin{cfuncdesc}{void}{PyDict_Clear}{PyObject *p}
1870 Empties an existing dictionary of all key-value pairs.
1871\end{cfuncdesc}
1872
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00001873\begin{cfuncdesc}{int}{PyDict_Contains}{PyObject *p, PyObject *key}
1874 Determine if dictionary \var{p} contains \var{key}. If an item
1875 in \var{p} is matches \var{key}, return \code{1}, otherwise return
1876 \code{0}. On error, return \code{-1}. This is equivalent to the
1877 Python expression \samp{\var{key} in \var{p}}.
Tim Peters9ddf40b2004-06-20 22:41:32 +00001878 \versionadded{2.4}
Raymond Hettingerbc0f2ab2003-11-25 21:12:14 +00001879\end{cfuncdesc}
1880
Fred Drake3adf79e2001-10-12 19:01:43 +00001881\begin{cfuncdesc}{PyObject*}{PyDict_Copy}{PyObject *p}
1882 Returns a new dictionary that contains the same key-value pairs as
1883 \var{p}.
1884 \versionadded{1.6}
1885\end{cfuncdesc}
1886
1887\begin{cfuncdesc}{int}{PyDict_SetItem}{PyObject *p, PyObject *key,
1888 PyObject *val}
1889 Inserts \var{value} into the dictionary \var{p} with a key of
1890 \var{key}. \var{key} must be hashable; if it isn't,
1891 \exception{TypeError} will be raised.
1892 Returns \code{0} on success or \code{-1} on failure.
1893\end{cfuncdesc}
1894
1895\begin{cfuncdesc}{int}{PyDict_SetItemString}{PyObject *p,
1896 char *key,
1897 PyObject *val}
1898 Inserts \var{value} into the dictionary \var{p} using \var{key} as a
1899 key. \var{key} should be a \ctype{char*}. The key object is created
1900 using \code{PyString_FromString(\var{key})}. Returns \code{0} on
1901 success or \code{-1} on failure.
1902 \ttindex{PyString_FromString()}
1903\end{cfuncdesc}
1904
1905\begin{cfuncdesc}{int}{PyDict_DelItem}{PyObject *p, PyObject *key}
1906 Removes the entry in dictionary \var{p} with key \var{key}.
1907 \var{key} must be hashable; if it isn't, \exception{TypeError} is
Skip Montanaroa23bc422002-01-23 08:18:30 +00001908 raised. Returns \code{0} on success or \code{-1} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00001909\end{cfuncdesc}
1910
1911\begin{cfuncdesc}{int}{PyDict_DelItemString}{PyObject *p, char *key}
1912 Removes the entry in dictionary \var{p} which has a key specified by
1913 the string \var{key}. Returns \code{0} on success or \code{-1} on
1914 failure.
1915\end{cfuncdesc}
1916
1917\begin{cfuncdesc}{PyObject*}{PyDict_GetItem}{PyObject *p, PyObject *key}
1918 Returns the object from dictionary \var{p} which has a key
1919 \var{key}. Returns \NULL{} if the key \var{key} is not present, but
1920 \emph{without} setting an exception.
1921\end{cfuncdesc}
1922
1923\begin{cfuncdesc}{PyObject*}{PyDict_GetItemString}{PyObject *p, char *key}
1924 This is the same as \cfunction{PyDict_GetItem()}, but \var{key} is
1925 specified as a \ctype{char*}, rather than a \ctype{PyObject*}.
1926\end{cfuncdesc}
1927
1928\begin{cfuncdesc}{PyObject*}{PyDict_Items}{PyObject *p}
1929 Returns a \ctype{PyListObject} containing all the items from the
1930 dictionary, as in the dictinoary method \method{items()} (see the
1931 \citetitle[../lib/lib.html]{Python Library Reference}).
1932\end{cfuncdesc}
1933
1934\begin{cfuncdesc}{PyObject*}{PyDict_Keys}{PyObject *p}
1935 Returns a \ctype{PyListObject} containing all the keys from the
1936 dictionary, as in the dictionary method \method{keys()} (see the
1937 \citetitle[../lib/lib.html]{Python Library Reference}).
1938\end{cfuncdesc}
1939
1940\begin{cfuncdesc}{PyObject*}{PyDict_Values}{PyObject *p}
1941 Returns a \ctype{PyListObject} containing all the values from the
1942 dictionary \var{p}, as in the dictionary method \method{values()}
1943 (see the \citetitle[../lib/lib.html]{Python Library Reference}).
1944\end{cfuncdesc}
1945
1946\begin{cfuncdesc}{int}{PyDict_Size}{PyObject *p}
1947 Returns the number of items in the dictionary. This is equivalent
1948 to \samp{len(\var{p})} on a dictionary.\bifuncindex{len}
1949\end{cfuncdesc}
1950
1951\begin{cfuncdesc}{int}{PyDict_Next}{PyObject *p, int *ppos,
1952 PyObject **pkey, PyObject **pvalue}
1953 Iterate over all key-value pairs in the dictionary \var{p}. The
1954 \ctype{int} referred to by \var{ppos} must be initialized to
1955 \code{0} prior to the first call to this function to start the
1956 iteration; the function returns true for each pair in the
1957 dictionary, and false once all pairs have been reported. The
1958 parameters \var{pkey} and \var{pvalue} should either point to
1959 \ctype{PyObject*} variables that will be filled in with each key and
Tim Peters9ddf40b2004-06-20 22:41:32 +00001960 value, respectively, or may be \NULL{}. Any references returned through
Raymond Hettinger54693242003-12-13 19:48:41 +00001961 them are borrowed. \var{ppos} should not be altered during iteration.
1962 Its value represents offsets within the internal dictionary structure,
1963 and since the structure is sparse, the offsets are not consecutive.
Fred Drake3adf79e2001-10-12 19:01:43 +00001964
1965 For example:
1966
1967\begin{verbatim}
1968PyObject *key, *value;
1969int pos = 0;
1970
1971while (PyDict_Next(self->dict, &pos, &key, &value)) {
1972 /* do something interesting with the values... */
1973 ...
1974}
1975\end{verbatim}
1976
1977 The dictionary \var{p} should not be mutated during iteration. It
1978 is safe (since Python 2.1) to modify the values of the keys as you
1979 iterate over the dictionary, but only so long as the set of keys
1980 does not change. For example:
1981
1982\begin{verbatim}
1983PyObject *key, *value;
1984int pos = 0;
1985
1986while (PyDict_Next(self->dict, &pos, &key, &value)) {
1987 int i = PyInt_AS_LONG(value) + 1;
1988 PyObject *o = PyInt_FromLong(i);
1989 if (o == NULL)
1990 return -1;
1991 if (PyDict_SetItem(self->dict, key, o) < 0) {
1992 Py_DECREF(o);
1993 return -1;
1994 }
1995 Py_DECREF(o);
1996}
1997\end{verbatim}
1998\end{cfuncdesc}
1999
2000\begin{cfuncdesc}{int}{PyDict_Merge}{PyObject *a, PyObject *b, int override}
Tim Petersf582b822001-12-11 18:51:08 +00002001 Iterate over mapping object \var{b} adding key-value pairs to dictionary
2002 \var{a}.
2003 \var{b} may be a dictionary, or any object supporting
2004 \function{PyMapping_Keys()} and \function{PyObject_GetItem()}.
2005 If \var{override} is true, existing pairs in \var{a} will
Fred Drake3adf79e2001-10-12 19:01:43 +00002006 be replaced if a matching key is found in \var{b}, otherwise pairs
2007 will only be added if there is not a matching key in \var{a}.
Tim Petersf582b822001-12-11 18:51:08 +00002008 Return \code{0} on success or \code{-1} if an exception was
Fred Drake3adf79e2001-10-12 19:01:43 +00002009 raised.
2010\versionadded{2.2}
2011\end{cfuncdesc}
2012
2013\begin{cfuncdesc}{int}{PyDict_Update}{PyObject *a, PyObject *b}
2014 This is the same as \code{PyDict_Merge(\var{a}, \var{b}, 1)} in C,
Tim Petersf582b822001-12-11 18:51:08 +00002015 or \code{\var{a}.update(\var{b})} in Python. Return \code{0} on
Fred Drake3adf79e2001-10-12 19:01:43 +00002016 success or \code{-1} if an exception was raised.
2017 \versionadded{2.2}
2018\end{cfuncdesc}
2019
Tim Petersf582b822001-12-11 18:51:08 +00002020\begin{cfuncdesc}{int}{PyDict_MergeFromSeq2}{PyObject *a, PyObject *seq2,
2021 int override}
2022 Update or merge into dictionary \var{a}, from the key-value pairs in
2023 \var{seq2}. \var{seq2} must be an iterable object producing
2024 iterable objects of length 2, viewed as key-value pairs. In case of
2025 duplicate keys, the last wins if \var{override} is true, else the
2026 first wins.
2027 Return \code{0} on success or \code{-1} if an exception
2028 was raised.
2029 Equivalent Python (except for the return value):
2030
2031\begin{verbatim}
2032def PyDict_MergeFromSeq2(a, seq2, override):
2033 for key, value in seq2:
2034 if override or key not in a:
2035 a[key] = value
2036\end{verbatim}
2037
2038 \versionadded{2.2}
2039\end{cfuncdesc}
Fred Drake3adf79e2001-10-12 19:01:43 +00002040
Fred Drake54e62942001-12-11 19:40:16 +00002041
Fred Drake3adf79e2001-10-12 19:01:43 +00002042\section{Other Objects \label{otherObjects}}
2043
2044\subsection{File Objects \label{fileObjects}}
2045
2046\obindex{file}
2047Python's built-in file objects are implemented entirely on the
2048\ctype{FILE*} support from the C standard library. This is an
2049implementation detail and may change in future releases of Python.
2050
2051\begin{ctypedesc}{PyFileObject}
2052 This subtype of \ctype{PyObject} represents a Python file object.
2053\end{ctypedesc}
2054
2055\begin{cvardesc}{PyTypeObject}{PyFile_Type}
2056 This instance of \ctype{PyTypeObject} represents the Python file
2057 type. This is exposed to Python programs as \code{types.FileType}.
2058 \withsubitem{(in module types)}{\ttindex{FileType}}
2059\end{cvardesc}
2060
2061\begin{cfuncdesc}{int}{PyFile_Check}{PyObject *p}
2062 Returns true if its argument is a \ctype{PyFileObject} or a subtype
2063 of \ctype{PyFileObject}.
2064 \versionchanged[Allowed subtypes to be accepted]{2.2}
2065\end{cfuncdesc}
2066
2067\begin{cfuncdesc}{int}{PyFile_CheckExact}{PyObject *p}
2068 Returns true if its argument is a \ctype{PyFileObject}, but not a
2069 subtype of \ctype{PyFileObject}.
2070 \versionadded{2.2}
2071\end{cfuncdesc}
2072
2073\begin{cfuncdesc}{PyObject*}{PyFile_FromString}{char *filename, char *mode}
2074 On success, returns a new file object that is opened on the file
2075 given by \var{filename}, with a file mode given by \var{mode}, where
2076 \var{mode} has the same semantics as the standard C routine
Tim Peters9ddf40b2004-06-20 22:41:32 +00002077 \cfunction{fopen()}\ttindex{fopen()}. On failure, returns \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002078\end{cfuncdesc}
2079
2080\begin{cfuncdesc}{PyObject*}{PyFile_FromFile}{FILE *fp,
2081 char *name, char *mode,
2082 int (*close)(FILE*)}
2083 Creates a new \ctype{PyFileObject} from the already-open standard C
2084 file pointer, \var{fp}. The function \var{close} will be called
2085 when the file should be closed. Returns \NULL{} on failure.
2086\end{cfuncdesc}
2087
2088\begin{cfuncdesc}{FILE*}{PyFile_AsFile}{PyFileObject *p}
2089 Returns the file object associated with \var{p} as a \ctype{FILE*}.
2090\end{cfuncdesc}
2091
2092\begin{cfuncdesc}{PyObject*}{PyFile_GetLine}{PyObject *p, int n}
2093 Equivalent to \code{\var{p}.readline(\optional{\var{n}})}, this
2094 function reads one line from the object \var{p}. \var{p} may be a
2095 file object or any object with a \method{readline()} method. If
2096 \var{n} is \code{0}, exactly one line is read, regardless of the
2097 length of the line. If \var{n} is greater than \code{0}, no more
2098 than \var{n} bytes will be read from the file; a partial line can be
2099 returned. In both cases, an empty string is returned if the end of
2100 the file is reached immediately. If \var{n} is less than \code{0},
2101 however, one line is read regardless of length, but
2102 \exception{EOFError} is raised if the end of the file is reached
2103 immediately.
2104 \withsubitem{(built-in exception)}{\ttindex{EOFError}}
2105\end{cfuncdesc}
2106
2107\begin{cfuncdesc}{PyObject*}{PyFile_Name}{PyObject *p}
2108 Returns the name of the file specified by \var{p} as a string
2109 object.
2110\end{cfuncdesc}
2111
2112\begin{cfuncdesc}{void}{PyFile_SetBufSize}{PyFileObject *p, int n}
2113 Available on systems with \cfunction{setvbuf()}\ttindex{setvbuf()}
2114 only. This should only be called immediately after file object
2115 creation.
2116\end{cfuncdesc}
2117
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00002118\begin{cfuncdesc}{int}{PyFile_Encoding}{PyFileObject *p, char *enc}
2119 Set the file's encoding for Unicode output to \var{enc}. Return
2120 1 on success and 0 on failure.
2121 \versionadded{2.3}
2122\end{cfuncdesc}
2123
Fred Drake3adf79e2001-10-12 19:01:43 +00002124\begin{cfuncdesc}{int}{PyFile_SoftSpace}{PyObject *p, int newflag}
2125 This function exists for internal use by the interpreter. Sets the
2126 \member{softspace} attribute of \var{p} to \var{newflag} and
2127 \withsubitem{(file attribute)}{\ttindex{softspace}}returns the
2128 previous value. \var{p} does not have to be a file object for this
2129 function to work properly; any object is supported (thought its only
2130 interesting if the \member{softspace} attribute can be set). This
2131 function clears any errors, and will return \code{0} as the previous
2132 value if the attribute either does not exist or if there were errors
2133 in retrieving it. There is no way to detect errors from this
2134 function, but doing so should not be needed.
2135\end{cfuncdesc}
2136
2137\begin{cfuncdesc}{int}{PyFile_WriteObject}{PyObject *obj, PyFileObject *p,
2138 int flags}
2139 Writes object \var{obj} to file object \var{p}. The only supported
2140 flag for \var{flags} is
2141 \constant{Py_PRINT_RAW}\ttindex{Py_PRINT_RAW}; if given, the
2142 \function{str()} of the object is written instead of the
2143 \function{repr()}. Returns \code{0} on success or \code{-1} on
2144 failure; the appropriate exception will be set.
2145\end{cfuncdesc}
2146
Fred Drake454af892001-11-29 22:42:59 +00002147\begin{cfuncdesc}{int}{PyFile_WriteString}{const char *s, PyFileObject *p}
Fred Drake3adf79e2001-10-12 19:01:43 +00002148 Writes string \var{s} to file object \var{p}. Returns \code{0} on
2149 success or \code{-1} on failure; the appropriate exception will be
2150 set.
2151\end{cfuncdesc}
2152
2153
2154\subsection{Instance Objects \label{instanceObjects}}
2155
2156\obindex{instance}
2157There are very few functions specific to instance objects.
2158
2159\begin{cvardesc}{PyTypeObject}{PyInstance_Type}
2160 Type object for class instances.
2161\end{cvardesc}
2162
2163\begin{cfuncdesc}{int}{PyInstance_Check}{PyObject *obj}
2164 Returns true if \var{obj} is an instance.
2165\end{cfuncdesc}
2166
2167\begin{cfuncdesc}{PyObject*}{PyInstance_New}{PyObject *class,
2168 PyObject *arg,
2169 PyObject *kw}
2170 Create a new instance of a specific class. The parameters \var{arg}
2171 and \var{kw} are used as the positional and keyword parameters to
2172 the object's constructor.
2173\end{cfuncdesc}
2174
2175\begin{cfuncdesc}{PyObject*}{PyInstance_NewRaw}{PyObject *class,
2176 PyObject *dict}
2177 Create a new instance of a specific class without calling it's
2178 constructor. \var{class} is the class of new object. The
2179 \var{dict} parameter will be used as the object's \member{__dict__};
Tim Peters9ddf40b2004-06-20 22:41:32 +00002180 if \NULL{}, a new dictionary will be created for the instance.
Fred Drake3adf79e2001-10-12 19:01:43 +00002181\end{cfuncdesc}
2182
2183
2184\subsection{Method Objects \label{method-objects}}
2185
2186\obindex{method}
2187There are some useful functions that are useful for working with
2188method objects.
2189
2190\begin{cvardesc}{PyTypeObject}{PyMethod_Type}
2191 This instance of \ctype{PyTypeObject} represents the Python method
2192 type. This is exposed to Python programs as \code{types.MethodType}.
2193 \withsubitem{(in module types)}{\ttindex{MethodType}}
2194\end{cvardesc}
2195
2196\begin{cfuncdesc}{int}{PyMethod_Check}{PyObject *o}
2197 Return true if \var{o} is a method object (has type
Tim Peters9ddf40b2004-06-20 22:41:32 +00002198 \cdata{PyMethod_Type}). The parameter must not be \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002199\end{cfuncdesc}
2200
2201\begin{cfuncdesc}{PyObject*}{PyMethod_New}{PyObject *func.
2202 PyObject *self, PyObject *class}
2203 Return a new method object, with \var{func} being any callable
2204 object; this is the function that will be called when the method is
2205 called. If this method should be bound to an instance, \var{self}
2206 should be the instance and \var{class} should be the class of
2207 \var{self}, otherwise \var{self} should be \NULL{} and \var{class}
2208 should be the class which provides the unbound method..
2209\end{cfuncdesc}
2210
2211\begin{cfuncdesc}{PyObject*}{PyMethod_Class}{PyObject *meth}
2212 Return the class object from which the method \var{meth} was
2213 created; if this was created from an instance, it will be the class
2214 of the instance.
2215\end{cfuncdesc}
2216
2217\begin{cfuncdesc}{PyObject*}{PyMethod_GET_CLASS}{PyObject *meth}
2218 Macro version of \cfunction{PyMethod_Class()} which avoids error
2219 checking.
2220\end{cfuncdesc}
2221
2222\begin{cfuncdesc}{PyObject*}{PyMethod_Function}{PyObject *meth}
2223 Return the function object associated with the method \var{meth}.
2224\end{cfuncdesc}
2225
2226\begin{cfuncdesc}{PyObject*}{PyMethod_GET_FUNCTION}{PyObject *meth}
2227 Macro version of \cfunction{PyMethod_Function()} which avoids error
2228 checking.
2229\end{cfuncdesc}
2230
2231\begin{cfuncdesc}{PyObject*}{PyMethod_Self}{PyObject *meth}
2232 Return the instance associated with the method \var{meth} if it is
Tim Peters9ddf40b2004-06-20 22:41:32 +00002233 bound, otherwise return \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002234\end{cfuncdesc}
2235
2236\begin{cfuncdesc}{PyObject*}{PyMethod_GET_SELF}{PyObject *meth}
2237 Macro version of \cfunction{PyMethod_Self()} which avoids error
2238 checking.
2239\end{cfuncdesc}
2240
2241
2242\subsection{Module Objects \label{moduleObjects}}
2243
2244\obindex{module}
2245There are only a few functions special to module objects.
2246
2247\begin{cvardesc}{PyTypeObject}{PyModule_Type}
2248 This instance of \ctype{PyTypeObject} represents the Python module
2249 type. This is exposed to Python programs as
2250 \code{types.ModuleType}.
2251 \withsubitem{(in module types)}{\ttindex{ModuleType}}
2252\end{cvardesc}
2253
2254\begin{cfuncdesc}{int}{PyModule_Check}{PyObject *p}
2255 Returns true if \var{p} is a module object, or a subtype of a module
2256 object.
2257 \versionchanged[Allowed subtypes to be accepted]{2.2}
2258\end{cfuncdesc}
2259
2260\begin{cfuncdesc}{int}{PyModule_CheckExact}{PyObject *p}
2261 Returns true if \var{p} is a module object, but not a subtype of
2262 \cdata{PyModule_Type}.
2263 \versionadded{2.2}
2264\end{cfuncdesc}
2265
2266\begin{cfuncdesc}{PyObject*}{PyModule_New}{char *name}
2267 Return a new module object with the \member{__name__} attribute set
2268 to \var{name}. Only the module's \member{__doc__} and
2269 \member{__name__} attributes are filled in; the caller is
2270 responsible for providing a \member{__file__} attribute.
2271 \withsubitem{(module attribute)}{
2272 \ttindex{__name__}\ttindex{__doc__}\ttindex{__file__}}
2273\end{cfuncdesc}
2274
2275\begin{cfuncdesc}{PyObject*}{PyModule_GetDict}{PyObject *module}
2276 Return the dictionary object that implements \var{module}'s
2277 namespace; this object is the same as the \member{__dict__}
2278 attribute of the module object. This function never fails.
2279 \withsubitem{(module attribute)}{\ttindex{__dict__}}
Fred Drakef495ef72002-04-12 19:32:07 +00002280 It is recommended extensions use other \cfunction{PyModule_*()}
2281 and \cfunction{PyObject_*()} functions rather than directly
2282 manipulate a module's \member{__dict__}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002283\end{cfuncdesc}
2284
2285\begin{cfuncdesc}{char*}{PyModule_GetName}{PyObject *module}
2286 Return \var{module}'s \member{__name__} value. If the module does
2287 not provide one, or if it is not a string, \exception{SystemError}
2288 is raised and \NULL{} is returned.
2289 \withsubitem{(module attribute)}{\ttindex{__name__}}
2290 \withsubitem{(built-in exception)}{\ttindex{SystemError}}
2291\end{cfuncdesc}
2292
2293\begin{cfuncdesc}{char*}{PyModule_GetFilename}{PyObject *module}
2294 Return the name of the file from which \var{module} was loaded using
2295 \var{module}'s \member{__file__} attribute. If this is not defined,
2296 or if it is not a string, raise \exception{SystemError} and return
Tim Peters9ddf40b2004-06-20 22:41:32 +00002297 \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002298 \withsubitem{(module attribute)}{\ttindex{__file__}}
2299 \withsubitem{(built-in exception)}{\ttindex{SystemError}}
2300\end{cfuncdesc}
2301
2302\begin{cfuncdesc}{int}{PyModule_AddObject}{PyObject *module,
2303 char *name, PyObject *value}
2304 Add an object to \var{module} as \var{name}. This is a convenience
2305 function which can be used from the module's initialization
2306 function. This steals a reference to \var{value}. Returns
2307 \code{-1} on error, \code{0} on success.
2308 \versionadded{2.0}
2309\end{cfuncdesc}
2310
2311\begin{cfuncdesc}{int}{PyModule_AddIntConstant}{PyObject *module,
Martin v. Löwisdd07e592004-06-02 12:45:27 +00002312 char *name, long value}
Fred Drake3adf79e2001-10-12 19:01:43 +00002313 Add an integer constant to \var{module} as \var{name}. This
2314 convenience function can be used from the module's initialization
2315 function. Returns \code{-1} on error, \code{0} on success.
2316 \versionadded{2.0}
2317\end{cfuncdesc}
2318
2319\begin{cfuncdesc}{int}{PyModule_AddStringConstant}{PyObject *module,
2320 char *name, char *value}
2321 Add a string constant to \var{module} as \var{name}. This
2322 convenience function can be used from the module's initialization
2323 function. The string \var{value} must be null-terminated. Returns
2324 \code{-1} on error, \code{0} on success.
2325 \versionadded{2.0}
2326\end{cfuncdesc}
2327
2328
2329\subsection{Iterator Objects \label{iterator-objects}}
2330
2331Python provides two general-purpose iterator objects. The first, a
2332sequence iterator, works with an arbitrary sequence supporting the
2333\method{__getitem__()} method. The second works with a callable
2334object and a sentinel value, calling the callable for each item in the
2335sequence, and ending the iteration when the sentinel value is
2336returned.
2337
2338\begin{cvardesc}{PyTypeObject}{PySeqIter_Type}
2339 Type object for iterator objects returned by
2340 \cfunction{PySeqIter_New()} and the one-argument form of the
2341 \function{iter()} built-in function for built-in sequence types.
2342 \versionadded{2.2}
2343\end{cvardesc}
2344
2345\begin{cfuncdesc}{int}{PySeqIter_Check}{op}
2346 Return true if the type of \var{op} is \cdata{PySeqIter_Type}.
2347 \versionadded{2.2}
2348\end{cfuncdesc}
2349
2350\begin{cfuncdesc}{PyObject*}{PySeqIter_New}{PyObject *seq}
2351 Return an iterator that works with a general sequence object,
2352 \var{seq}. The iteration ends when the sequence raises
2353 \exception{IndexError} for the subscripting operation.
2354 \versionadded{2.2}
2355\end{cfuncdesc}
2356
2357\begin{cvardesc}{PyTypeObject}{PyCallIter_Type}
2358 Type object for iterator objects returned by
2359 \cfunction{PyCallIter_New()} and the two-argument form of the
2360 \function{iter()} built-in function.
2361 \versionadded{2.2}
2362\end{cvardesc}
2363
2364\begin{cfuncdesc}{int}{PyCallIter_Check}{op}
2365 Return true if the type of \var{op} is \cdata{PyCallIter_Type}.
2366 \versionadded{2.2}
2367\end{cfuncdesc}
2368
2369\begin{cfuncdesc}{PyObject*}{PyCallIter_New}{PyObject *callable,
2370 PyObject *sentinel}
2371 Return a new iterator. The first parameter, \var{callable}, can be
2372 any Python callable object that can be called with no parameters;
2373 each call to it should return the next item in the iteration. When
2374 \var{callable} returns a value equal to \var{sentinel}, the
2375 iteration will be terminated.
2376 \versionadded{2.2}
2377\end{cfuncdesc}
2378
2379
2380\subsection{Descriptor Objects \label{descriptor-objects}}
2381
Fred Drake54e62942001-12-11 19:40:16 +00002382``Descriptors'' are objects that describe some attribute of an object.
2383They are found in the dictionary of type objects.
2384
Fred Drake3adf79e2001-10-12 19:01:43 +00002385\begin{cvardesc}{PyTypeObject}{PyProperty_Type}
Fred Drake54e62942001-12-11 19:40:16 +00002386 The type object for the built-in descriptor types.
Fred Drake3adf79e2001-10-12 19:01:43 +00002387 \versionadded{2.2}
2388\end{cvardesc}
2389
2390\begin{cfuncdesc}{PyObject*}{PyDescr_NewGetSet}{PyTypeObject *type,
2391 PyGetSetDef *getset}
2392 \versionadded{2.2}
2393\end{cfuncdesc}
2394
2395\begin{cfuncdesc}{PyObject*}{PyDescr_NewMember}{PyTypeObject *type,
2396 PyMemberDef *meth}
2397 \versionadded{2.2}
2398\end{cfuncdesc}
2399
2400\begin{cfuncdesc}{PyObject*}{PyDescr_NewMethod}{PyTypeObject *type,
2401 PyMethodDef *meth}
2402 \versionadded{2.2}
2403\end{cfuncdesc}
2404
2405\begin{cfuncdesc}{PyObject*}{PyDescr_NewWrapper}{PyTypeObject *type,
2406 struct wrapperbase *wrapper,
2407 void *wrapped}
2408 \versionadded{2.2}
2409\end{cfuncdesc}
2410
Thomas Heller8178a222004-02-09 10:47:11 +00002411\begin{cfuncdesc}{PyObject*}{PyDescr_NewClassMethod}{PyTypeObject *type,
2412 PyMethodDef *method}
2413 \versionadded{2.3}
2414\end{cfuncdesc}
2415
Fred Drake3adf79e2001-10-12 19:01:43 +00002416\begin{cfuncdesc}{int}{PyDescr_IsData}{PyObject *descr}
2417 Returns true if the descriptor objects \var{descr} describes a data
2418 attribute, or false if it describes a method. \var{descr} must be a
2419 descriptor object; there is no error checking.
2420 \versionadded{2.2}
2421\end{cfuncdesc}
2422
2423\begin{cfuncdesc}{PyObject*}{PyWrapper_New}{PyObject *, PyObject *}
2424 \versionadded{2.2}
2425\end{cfuncdesc}
2426
2427
2428\subsection{Slice Objects \label{slice-objects}}
2429
2430\begin{cvardesc}{PyTypeObject}{PySlice_Type}
2431 The type object for slice objects. This is the same as
2432 \code{types.SliceType}.
2433 \withsubitem{(in module types)}{\ttindex{SliceType}}
2434\end{cvardesc}
2435
2436\begin{cfuncdesc}{int}{PySlice_Check}{PyObject *ob}
2437 Returns true if \var{ob} is a slice object; \var{ob} must not be
Tim Peters9ddf40b2004-06-20 22:41:32 +00002438 \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002439\end{cfuncdesc}
2440
2441\begin{cfuncdesc}{PyObject*}{PySlice_New}{PyObject *start, PyObject *stop,
2442 PyObject *step}
2443 Return a new slice object with the given values. The \var{start},
2444 \var{stop}, and \var{step} parameters are used as the values of the
2445 slice object attributes of the same names. Any of the values may be
Tim Peters9ddf40b2004-06-20 22:41:32 +00002446 \NULL{}, in which case the \code{None} will be used for the
Fred Drake3adf79e2001-10-12 19:01:43 +00002447 corresponding attribute. Returns \NULL{} if the new object could
2448 not be allocated.
2449\end{cfuncdesc}
2450
2451\begin{cfuncdesc}{int}{PySlice_GetIndices}{PySliceObject *slice, int length,
2452 int *start, int *stop, int *step}
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002453Retrieve the start, stop and step indices from the slice object
2454\var{slice}, assuming a sequence of length \var{length}. Treats
2455indices greater than \var{length} as errors.
2456
2457Returns 0 on success and -1 on error with no exception set (unless one
2458of the indices was not \constant{None} and failed to be converted to
2459an integer, in which case -1 is returned with an exception set).
2460
2461You probably do not want to use this function. If you want to use
2462slice objects in versions of Python prior to 2.3, you would probably
2463do well to incorporate the source of \cfunction{PySlice_GetIndicesEx},
2464suitably renamed, in the source of your extension.
2465\end{cfuncdesc}
2466
2467\begin{cfuncdesc}{int}{PySlice_GetIndicesEx}{PySliceObject *slice, int length,
Tim Peters9ddf40b2004-06-20 22:41:32 +00002468 int *start, int *stop, int *step,
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002469 int *slicelength}
2470Usable replacement for \cfunction{PySlice_GetIndices}. Retrieve the
2471start, stop, and step indices from the slice object \var{slice}
2472assuming a sequence of length \var{length}, and store the length of
2473the slice in \var{slicelength}. Out of bounds indices are clipped in
2474a manner consistent with the handling of normal slices.
2475
2476Returns 0 on success and -1 on error with exception set.
2477
2478\versionadded{2.3}
Fred Drake3adf79e2001-10-12 19:01:43 +00002479\end{cfuncdesc}
2480
2481
2482\subsection{Weak Reference Objects \label{weakref-objects}}
2483
2484Python supports \emph{weak references} as first-class objects. There
2485are two specific object types which directly implement weak
2486references. The first is a simple reference object, and the second
2487acts as a proxy for the original object as much as it can.
2488
2489\begin{cfuncdesc}{int}{PyWeakref_Check}{ob}
2490 Return true if \var{ob} is either a reference or proxy object.
2491 \versionadded{2.2}
2492\end{cfuncdesc}
2493
2494\begin{cfuncdesc}{int}{PyWeakref_CheckRef}{ob}
2495 Return true if \var{ob} is a reference object.
2496 \versionadded{2.2}
2497\end{cfuncdesc}
2498
2499\begin{cfuncdesc}{int}{PyWeakref_CheckProxy}{ob}
2500 Return true if \var{ob} is a proxy object.
2501 \versionadded{2.2}
2502\end{cfuncdesc}
2503
2504\begin{cfuncdesc}{PyObject*}{PyWeakref_NewRef}{PyObject *ob,
2505 PyObject *callback}
2506 Return a weak reference object for the object \var{ob}. This will
2507 always return a new reference, but is not guaranteed to create a new
2508 object; an existing reference object may be returned. The second
2509 parameter, \var{callback}, can be a callable object that receives
2510 notification when \var{ob} is garbage collected; it should accept a
Raymond Hettinger5232f502004-03-25 08:51:36 +00002511 single parameter, which will be the weak reference object itself.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002512 \var{callback} may also be \code{None} or \NULL{}. If \var{ob}
Fred Drake3adf79e2001-10-12 19:01:43 +00002513 is not a weakly-referencable object, or if \var{callback} is not
Tim Peters9ddf40b2004-06-20 22:41:32 +00002514 callable, \code{None}, or \NULL{}, this will return \NULL{} and
Fred Drake3adf79e2001-10-12 19:01:43 +00002515 raise \exception{TypeError}.
2516 \versionadded{2.2}
2517\end{cfuncdesc}
2518
2519\begin{cfuncdesc}{PyObject*}{PyWeakref_NewProxy}{PyObject *ob,
2520 PyObject *callback}
2521 Return a weak reference proxy object for the object \var{ob}. This
2522 will always return a new reference, but is not guaranteed to create
2523 a new object; an existing proxy object may be returned. The second
2524 parameter, \var{callback}, can be a callable object that receives
2525 notification when \var{ob} is garbage collected; it should accept a
Raymond Hettinger5232f502004-03-25 08:51:36 +00002526 single parameter, which will be the weak reference object itself.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002527 \var{callback} may also be \code{None} or \NULL{}. If \var{ob} is not
Fred Drake3adf79e2001-10-12 19:01:43 +00002528 a weakly-referencable object, or if \var{callback} is not callable,
Tim Peters9ddf40b2004-06-20 22:41:32 +00002529 \code{None}, or \NULL{}, this will return \NULL{} and raise
Fred Drake3adf79e2001-10-12 19:01:43 +00002530 \exception{TypeError}.
2531 \versionadded{2.2}
2532\end{cfuncdesc}
2533
2534\begin{cfuncdesc}{PyObject*}{PyWeakref_GetObject}{PyObject *ref}
2535 Returns the referenced object from a weak reference, \var{ref}. If
Ka-Ping Yeebd379e92003-03-28 18:07:16 +00002536 the referent is no longer live, returns \code{None}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002537 \versionadded{2.2}
2538\end{cfuncdesc}
2539
2540\begin{cfuncdesc}{PyObject*}{PyWeakref_GET_OBJECT}{PyObject *ref}
2541 Similar to \cfunction{PyWeakref_GetObject()}, but implemented as a
2542 macro that does no error checking.
2543 \versionadded{2.2}
2544\end{cfuncdesc}
2545
2546
2547\subsection{CObjects \label{cObjects}}
2548
2549\obindex{CObject}
2550Refer to \emph{Extending and Embedding the Python Interpreter},
Fred Drake54e62942001-12-11 19:40:16 +00002551section~1.12, ``Providing a C API for an Extension Module,'' for more
Fred Drake3adf79e2001-10-12 19:01:43 +00002552information on using these objects.
2553
2554
2555\begin{ctypedesc}{PyCObject}
2556 This subtype of \ctype{PyObject} represents an opaque value, useful
2557 for C extension modules who need to pass an opaque value (as a
2558 \ctype{void*} pointer) through Python code to other C code. It is
2559 often used to make a C function pointer defined in one module
2560 available to other modules, so the regular import mechanism can be
2561 used to access C APIs defined in dynamically loaded modules.
2562\end{ctypedesc}
2563
2564\begin{cfuncdesc}{int}{PyCObject_Check}{PyObject *p}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002565 Return true if its argument is a \ctype{PyCObject}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002566\end{cfuncdesc}
2567
Tim Petersf582b822001-12-11 18:51:08 +00002568\begin{cfuncdesc}{PyObject*}{PyCObject_FromVoidPtr}{void* cobj,
Fred Drake54e62942001-12-11 19:40:16 +00002569 void (*destr)(void *)}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002570 Create a \ctype{PyCObject} from the \code{void *}\var{cobj}. The
Fred Drake3adf79e2001-10-12 19:01:43 +00002571 \var{destr} function will be called when the object is reclaimed,
Tim Peters9ddf40b2004-06-20 22:41:32 +00002572 unless it is \NULL{}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002573\end{cfuncdesc}
2574
2575\begin{cfuncdesc}{PyObject*}{PyCObject_FromVoidPtrAndDesc}{void* cobj,
2576 void* desc, void (*destr)(void *, void *)}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002577 Create a \ctype{PyCObject} from the \ctype{void *}\var{cobj}. The
Fred Drake3adf79e2001-10-12 19:01:43 +00002578 \var{destr} function will be called when the object is reclaimed.
2579 The \var{desc} argument can be used to pass extra callback data for
2580 the destructor function.
2581\end{cfuncdesc}
2582
2583\begin{cfuncdesc}{void*}{PyCObject_AsVoidPtr}{PyObject* self}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002584 Return the object \ctype{void *} that the \ctype{PyCObject}
Fred Drake3adf79e2001-10-12 19:01:43 +00002585 \var{self} was created with.
2586\end{cfuncdesc}
2587
2588\begin{cfuncdesc}{void*}{PyCObject_GetDesc}{PyObject* self}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002589 Return the description \ctype{void *} that the \ctype{PyCObject}
Fred Drake3adf79e2001-10-12 19:01:43 +00002590 \var{self} was created with.
2591\end{cfuncdesc}
Fred Drakecd8474e2001-11-26 21:29:17 +00002592
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002593\begin{cfuncdesc}{int}{PyCObject_SetVoidPtr}{PyObject* self, void* cobj}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002594 Set the void pointer inside \var{self} to \var{cobj}.
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002595 The \ctype{PyCObject} must not have an associated destructor.
2596 Return true on success, false on failure.
2597\end{cfuncdesc}
2598
Fred Drakecd8474e2001-11-26 21:29:17 +00002599
2600\subsection{Cell Objects \label{cell-objects}}
2601
2602``Cell'' objects are used to implement variables referenced by
2603multiple scopes. For each such variable, a cell object is created to
2604store the value; the local variables of each stack frame that
2605references the value contains a reference to the cells from outer
2606scopes which also use that variable. When the value is accessed, the
2607value contained in the cell is used instead of the cell object
2608itself. This de-referencing of the cell object requires support from
2609the generated byte-code; these are not automatically de-referenced
2610when accessed. Cell objects are not likely to be useful elsewhere.
2611
Fred Drake54e62942001-12-11 19:40:16 +00002612\begin{ctypedesc}{PyCellObject}
2613 The C structure used for cell objects.
2614\end{ctypedesc}
2615
Fred Drakecd8474e2001-11-26 21:29:17 +00002616\begin{cvardesc}{PyTypeObject}{PyCell_Type}
2617 The type object corresponding to cell objects
2618\end{cvardesc}
2619
2620\begin{cfuncdesc}{int}{PyCell_Check}{ob}
2621 Return true if \var{ob} is a cell object; \var{ob} must not be
Tim Peters9ddf40b2004-06-20 22:41:32 +00002622 \NULL{}.
Fred Drakecd8474e2001-11-26 21:29:17 +00002623\end{cfuncdesc}
2624
2625\begin{cfuncdesc}{PyObject*}{PyCell_New}{PyObject *ob}
2626 Create and return a new cell object containing the value \var{ob}.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002627 The parameter may be \NULL{}.
Fred Drakecd8474e2001-11-26 21:29:17 +00002628\end{cfuncdesc}
2629
2630\begin{cfuncdesc}{PyObject*}{PyCell_Get}{PyObject *cell}
2631 Return the contents of the cell \var{cell}.
2632\end{cfuncdesc}
2633
2634\begin{cfuncdesc}{PyObject*}{PyCell_GET}{PyObject *cell}
2635 Return the contents of the cell \var{cell}, but without checking
Raymond Hettingerf4bb1f92003-08-23 03:38:11 +00002636 that \var{cell} is non-\NULL{} and a cell object.
Fred Drakecd8474e2001-11-26 21:29:17 +00002637\end{cfuncdesc}
2638
2639\begin{cfuncdesc}{int}{PyCell_Set}{PyObject *cell, PyObject *value}
2640 Set the contents of the cell object \var{cell} to \var{value}. This
2641 releases the reference to any current content of the cell.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002642 \var{value} may be \NULL{}. \var{cell} must be non-\NULL{}; if it is
Fred Drakecd8474e2001-11-26 21:29:17 +00002643 not a cell object, \code{-1} will be returned. On success, \code{0}
2644 will be returned.
2645\end{cfuncdesc}
2646
2647\begin{cfuncdesc}{void}{PyCell_SET}{PyObject *cell, PyObject *value}
2648 Sets the value of the cell object \var{cell} to \var{value}. No
2649 reference counts are adjusted, and no checks are made for safety;
2650 \var{cell} must be non-\NULL{} and must be a cell object.
2651\end{cfuncdesc}
Martin v. Löwise440e472004-06-01 15:22:42 +00002652
2653
2654\subsection{Generator Objects \label{gen-objects}}
2655
2656Generator objects are what Python uses to implement generator iterators.
2657They are normally created by iterating over a function that yields values,
2658rather than explicitly calling \cfunction{PyGen_New}.
2659
2660\begin{ctypedesc}{PyGenObject}
2661 The C structure used for generator objects.
2662\end{ctypedesc}
2663
2664\begin{cvardesc}{PyTypeObject}{PyGen_Type}
2665 The type object corresponding to generator objects
2666\end{cvardesc}
2667
2668\begin{cfuncdesc}{int}{PyGen_Check}{ob}
2669 Return true if \var{ob} is a generator object; \var{ob} must not be
Tim Peters9ddf40b2004-06-20 22:41:32 +00002670 \NULL{}.
Martin v. Löwise440e472004-06-01 15:22:42 +00002671\end{cfuncdesc}
2672
2673\begin{cfuncdesc}{int}{PyGen_CheckExact}{ob}
2674 Return true if \var{ob}'s type is \var{PyGen_Type}
2675 is a generator object; \var{ob} must not be
Tim Peters9ddf40b2004-06-20 22:41:32 +00002676 \NULL{}.
Martin v. Löwise440e472004-06-01 15:22:42 +00002677\end{cfuncdesc}
2678
2679\begin{cfuncdesc}{PyObject*}{PyGen_New}{PyFrameObject *frame}
2680 Create and return a new generator object based on the \var{frame} object.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002681 The parameter must not be \NULL{}.
2682\end{cfuncdesc}
2683
2684
2685\subsection{DateTime Objects \label{datetime-objects}}
2686
2687Various date and time objects are supplied by the \module{datetime}
2688module. Before using any of these functions, the header file
2689\file{datetime.h} must be included in your source (note that this is
2690not include by \file{Python.h}), and macro \cfunction{PyDateTime_IMPORT()}
2691must be invoked. The macro arranges to put a pointer to a C structure
2692in a static variable \code{PyDateTimeAPI}, which is used by the following
Tim Peters183dabc2004-07-11 19:26:19 +00002693macros.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002694
Tim Peters183dabc2004-07-11 19:26:19 +00002695Type-check macros:
2696
2697\begin{cfuncdesc}{int}{PyDate_Check}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002698 Return true if \var{ob} is of type \cdata{PyDateTime_DateType} or
2699 a subtype of \cdata{PyDateTime_DateType}. \var{ob} must not be
2700 \NULL{}.
2701 \versionadded{2.4}
2702\end{cfuncdesc}
2703
Tim Peters183dabc2004-07-11 19:26:19 +00002704\begin{cfuncdesc}{int}{PyDate_CheckExact}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002705 Return true if \var{ob} is of type \cdata{PyDateTime_DateType}.
2706 \var{ob} must not be \NULL{}.
2707 \versionadded{2.4}
2708\end{cfuncdesc}
2709
Tim Peters183dabc2004-07-11 19:26:19 +00002710\begin{cfuncdesc}{int}{PyDateTime_Check}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002711 Return true if \var{ob} is of type \cdata{PyDateTime_DateTimeType} or
2712 a subtype of \cdata{PyDateTime_DateTimeType}. \var{ob} must not be
2713 \NULL{}.
2714 \versionadded{2.4}
2715\end{cfuncdesc}
2716
Tim Peters183dabc2004-07-11 19:26:19 +00002717\begin{cfuncdesc}{int}{PyDateTime_CheckExact}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002718 Return true if \var{ob} is of type \cdata{PyDateTime_DateTimeType}.
2719 \var{ob} must not be \NULL{}.
2720 \versionadded{2.4}
2721\end{cfuncdesc}
2722
Tim Peters183dabc2004-07-11 19:26:19 +00002723\begin{cfuncdesc}{int}{PyTime_Check}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002724 Return true if \var{ob} is of type \cdata{PyDateTime_TimeType} or
2725 a subtype of \cdata{PyDateTime_TimeType}. \var{ob} must not be
2726 \NULL{}.
2727 \versionadded{2.4}
2728\end{cfuncdesc}
2729
Tim Peters183dabc2004-07-11 19:26:19 +00002730\begin{cfuncdesc}{int}{PyTime_CheckExact}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002731 Return true if \var{ob} is of type \cdata{PyDateTime_TimeType}.
2732 \var{ob} must not be \NULL{}.
2733 \versionadded{2.4}
2734\end{cfuncdesc}
2735
Tim Peters183dabc2004-07-11 19:26:19 +00002736\begin{cfuncdesc}{int}{PyDelta_Check}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002737 Return true if \var{ob} is of type \cdata{PyDateTime_DeltaType} or
2738 a subtype of \cdata{PyDateTime_DeltaType}. \var{ob} must not be
2739 \NULL{}.
2740 \versionadded{2.4}
2741\end{cfuncdesc}
2742
Tim Peters183dabc2004-07-11 19:26:19 +00002743\begin{cfuncdesc}{int}{PyDelta_CheckExact}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002744 Return true if \var{ob} is of type \cdata{PyDateTime_DeltaType}.
2745 \var{ob} must not be \NULL{}.
2746 \versionadded{2.4}
2747\end{cfuncdesc}
2748
Tim Peters183dabc2004-07-11 19:26:19 +00002749\begin{cfuncdesc}{int}{PyTZInfo_Check}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002750 Return true if \var{ob} is of type \cdata{PyDateTime_TZInfoType} or
2751 a subtype of \cdata{PyDateTime_TZInfoType}. \var{ob} must not be
2752 \NULL{}.
2753 \versionadded{2.4}
2754\end{cfuncdesc}
2755
Tim Peters183dabc2004-07-11 19:26:19 +00002756\begin{cfuncdesc}{int}{PyTZInfo_CheckExact}{PyObject *ob}
Tim Peters9ddf40b2004-06-20 22:41:32 +00002757 Return true if \var{ob} is of type \cdata{PyDateTime_TZInfoType}.
2758 \var{ob} must not be \NULL{}.
2759 \versionadded{2.4}
2760\end{cfuncdesc}
2761
Tim Peters183dabc2004-07-11 19:26:19 +00002762Macros to create objects:
2763
Tim Peters9ddf40b2004-06-20 22:41:32 +00002764\begin{cfuncdesc}{PyObject*}{PyDate_FromDate}{int year, int month, int day}
2765 Return a \code{datetime.date} object with the specified year, month
2766 and day.
2767 \versionadded{2.4}
2768\end{cfuncdesc}
2769
2770\begin{cfuncdesc}{PyObject*}{PyDate_FromDateAndTime}{int year, int month,
2771 int day, int hour, int minute, int second, int usecond}
2772 Return a \code{datetime.datetime} object with the specified year, month,
2773 day, hour, minute, second and microsecond.
2774 \versionadded{2.4}
2775\end{cfuncdesc}
2776
2777\begin{cfuncdesc}{PyObject*}{PyTime_FromTime}{int hour, int minute,
2778 int second, int usecond}
2779 Return a \code{datetime.time} object with the specified hour, minute,
2780 second and microsecond.
2781 \versionadded{2.4}
2782\end{cfuncdesc}
2783
2784\begin{cfuncdesc}{PyObject*}{PyDelta_FromDSU}{int days, int seconds,
2785 int useconds}
2786 Return a \code{datetime.timedelta} object representing the given number
2787 of days, seconds and microseconds. Normalization is performed so that
2788 the resulting number of microseconds and seconds lie in the ranges
2789 documented for \code{datetime.timedelta} objects.
2790 \versionadded{2.4}
2791\end{cfuncdesc}
2792
Tim Peters8ff9f9f2004-07-17 01:42:26 +00002793Macros to extract fields from date objects. The argument must be an
Tim Peters183dabc2004-07-11 19:26:19 +00002794instance of \cdata{PyDateTime_Date}, including subclasses (such as
2795\cdata{PyDateTime_DateTime}). The argument must not be \NULL{}, and
2796the type is not checked:
2797
2798\begin{cfuncdesc}{int}{PyDateTime_GET_YEAR}{PyDateTime_Date *o}
2799 Return the year, as a positive int.
2800 \versionadded{2.4}
2801\end{cfuncdesc}
2802
2803\begin{cfuncdesc}{int}{PyDateTime_GET_MONTH}{PyDateTime_Date *o}
2804 Return the month, as an int from 1 through 12.
2805 \versionadded{2.4}
2806\end{cfuncdesc}
2807
2808\begin{cfuncdesc}{int}{PyDateTime_GET_DAY}{PyDateTime_Date *o}
2809 Return the day, as an int from 1 through 31.
2810 \versionadded{2.4}
2811\end{cfuncdesc}
2812
Tim Peters8ff9f9f2004-07-17 01:42:26 +00002813Macros to extract fields from datetime objects. The argument must be an
Tim Peters183dabc2004-07-11 19:26:19 +00002814instance of \cdata{PyDateTime_DateTime}, including subclasses.
2815The argument must not be \NULL{}, and the type is not checked:
2816
2817\begin{cfuncdesc}{int}{PyDateTime_DATE_GET_HOUR}{PyDateTime_DateTime *o}
2818 Return the hour, an an int from 0 though 23.
2819 \versionadded{2.4}
2820\end{cfuncdesc}
2821
2822\begin{cfuncdesc}{int}{PyDateTime_DATE_GET_MINUTE}{PyDateTime_DateTime *o}
2823 Return the minute, as an int from 0 through 59.
2824 \versionadded{2.4}
2825\end{cfuncdesc}
2826
2827\begin{cfuncdesc}{int}{PyDateTime_DATE_GET_SECOND}{PyDateTime_DateTime *o}
2828 Return the second, as an int from 0 through 59.
2829 \versionadded{2.4}
2830\end{cfuncdesc}
2831
2832\begin{cfuncdesc}{int}{PyDateTime_DATE_GET_MICROSECOND}{PyDateTime_DateTime *o}
2833 Return the microsecond, as an int from 0 through 999999.
2834 \versionadded{2.4}
2835\end{cfuncdesc}
2836
Tim Peters8ff9f9f2004-07-17 01:42:26 +00002837Macros to extract fields from time objects. The argument must be an
Tim Peters183dabc2004-07-11 19:26:19 +00002838instance of \cdata{PyDateTime_Time}, including subclasses.
2839The argument must not be \NULL{}, and the type is not checked:
2840
2841\begin{cfuncdesc}{int}{PyDateTime_TIME_GET_HOUR}{PyDateTime_Time *o}
2842 Return the hour, as an int from 0 though 23.
2843 \versionadded{2.4}
2844\end{cfuncdesc}
2845
2846\begin{cfuncdesc}{int}{PyDateTime_TIME_GET_MINUTE}{PyDateTime_Time *o}
2847 Return the minute, as an int from 0 through 59.
2848 \versionadded{2.4}
2849\end{cfuncdesc}
2850
2851\begin{cfuncdesc}{int}{PyDateTime_TIME_GET_SECOND}{PyDateTime_Time *o}
2852 Return the second, as an int from 0 through 59.
2853 \versionadded{2.4}
2854\end{cfuncdesc}
2855
2856\begin{cfuncdesc}{int}{PyDateTime_TIME_GET_MICROSECOND}{PyDateTime_Time *o}
2857 Return the microsecond, as an int from 0 through 999999.
2858 \versionadded{2.4}
2859\end{cfuncdesc}
2860
2861Macros for the convenience of modules implementing the DB API:
2862
Tim Peters9ddf40b2004-06-20 22:41:32 +00002863\begin{cfuncdesc}{PyObject*}{PyDateTime_FromTimestamp}{PyObject *args}
2864 Create and return a new \code{datetime.datetime} object given an argument
2865 tuple suitable for passing to \code{datetime.datetime.fromtimestamp()}.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002866 \versionadded{2.4}
2867\end{cfuncdesc}
2868
2869\begin{cfuncdesc}{PyObject*}{PyDate_FromTimestamp}{PyObject *args}
2870 Create and return a new \code{datetime.date} object given an argument
2871 tuple suitable for passing to \code{datetime.date.fromtimestamp()}.
Tim Peters9ddf40b2004-06-20 22:41:32 +00002872 \versionadded{2.4}
Martin v. Löwise440e472004-06-01 15:22:42 +00002873\end{cfuncdesc}