blob: b00b9151ad8ccb81c6e4bb2a537993048dfe0b6c [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
88\obindex{None@\texttt{None}}
89Note 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
100
101\section{Numeric Objects \label{numericObjects}}
102
103\obindex{numeric}
104
105
106\subsection{Plain Integer Objects \label{intObjects}}
107
108\obindex{integer}
109\begin{ctypedesc}{PyIntObject}
110 This subtype of \ctype{PyObject} represents a Python integer
111 object.
112\end{ctypedesc}
113
114\begin{cvardesc}{PyTypeObject}{PyInt_Type}
Tim Petersf582b822001-12-11 18:51:08 +0000115 This instance of \ctype{PyTypeObject} represents the Python plain
Fred Drake3adf79e2001-10-12 19:01:43 +0000116 integer type. This is the same object as \code{types.IntType}.
117 \withsubitem{(in modules types)}{\ttindex{IntType}}
118\end{cvardesc}
119
120\begin{cfuncdesc}{int}{PyInt_Check}{PyObject* o}
121 Returns true if \var{o} is of type \cdata{PyInt_Type} or a subtype
122 of \cdata{PyInt_Type}.
123 \versionchanged[Allowed subtypes to be accepted]{2.2}
124\end{cfuncdesc}
125
126\begin{cfuncdesc}{int}{PyInt_CheckExact}{PyObject* o}
127 Returns true if \var{o} is of type \cdata{PyInt_Type}, but not a
128 subtype of \cdata{PyInt_Type}.
129 \versionadded{2.2}
130\end{cfuncdesc}
131
Skip Montanaro1ff49a72003-02-03 05:13:24 +0000132\begin{cfuncdesc}{PyObject*}{PyInt_FromString}{char *str, char **pend,
133 int base}
134 Return a new \ctype{PyIntObject} or \ctype{PyLongObject} based on the
135 string value in \var{str}, which is interpreted according to the radix in
136 \var{base}. If \var{pend} is non-\NULL, \code{*\var{pend}} will point to
137 the first character in \var{str} which follows the representation of the
138 number. If \var{base} is \code{0}, the radix will be determined based on
139 the leading characters of \var{str}: if \var{str} starts with \code{'0x'}
140 or \code{'0X'}, radix 16 will be used; if \var{str} starts with
141 \code{'0'}, radix 8 will be used; otherwise radix 10 will be used. If
142 \var{base} is not \code{0}, it must be between \code{2} and \code{36},
143 inclusive. Leading spaces are ignored. If there are no digits,
144 \exception{ValueError} will be raised. If the string represents a number
145 too large to be contained within the machine's \ctype{long int} type and
146 overflow warnings are being suppressed, a \ctype{PyLongObject} will be
147 returned. If overflow warnings are not being suppressed, \NULL{} will be
148 returned in this case.
149\end{cfuncdesc}
150
Fred Drake3adf79e2001-10-12 19:01:43 +0000151\begin{cfuncdesc}{PyObject*}{PyInt_FromLong}{long ival}
152 Creates a new integer object with a value of \var{ival}.
153
154 The current implementation keeps an array of integer objects for all
155 integers between \code{-1} and \code{100}, when you create an int in
156 that range you actually just get back a reference to the existing
157 object. So it should be possible to change the value of \code{1}. I
158 suspect the behaviour of Python in this case is undefined. :-)
159\end{cfuncdesc}
160
161\begin{cfuncdesc}{long}{PyInt_AsLong}{PyObject *io}
162 Will first attempt to cast the object to a \ctype{PyIntObject}, if
163 it is not already one, and then return its value.
164\end{cfuncdesc}
165
166\begin{cfuncdesc}{long}{PyInt_AS_LONG}{PyObject *io}
167 Returns the value of the object \var{io}. No error checking is
168 performed.
169\end{cfuncdesc}
170
Thomas Heller34d7f092003-04-23 19:51:05 +0000171\begin{cfuncdesc}{unsigned long}{PyInt_AsUnsignedLongMask}{PyObject *io}
172 Will first attempt to cast the object to a \ctype{PyIntObject} or
Fred Drakec22b2992003-04-23 20:38:41 +0000173 \ctype{PyLongObject}, if it is not already one, and then return its
Thomas Heller34d7f092003-04-23 19:51:05 +0000174 value as unsigned long. This function does not check for overflow.
175 \versionadded{2.3}
176\end{cfuncdesc}
177
178\begin{cfuncdesc}{unsigned long}{PyInt_AsUnsignedLongLongMask}{PyObject *io}
179 Will first attempt to cast the object to a \ctype{PyIntObject} or
Fred Drakec22b2992003-04-23 20:38:41 +0000180 \ctype{PyLongObject}, if it is not already one, and then return its
Thomas Heller34d7f092003-04-23 19:51:05 +0000181 value as unsigned long long, without checking for overflow.
182 \versionadded{2.3}
183\end{cfuncdesc}
184
Fred Drake3adf79e2001-10-12 19:01:43 +0000185\begin{cfuncdesc}{long}{PyInt_GetMax}{}
186 Returns the system's idea of the largest integer it can handle
187 (\constant{LONG_MAX}\ttindex{LONG_MAX}, as defined in the system
188 header files).
189\end{cfuncdesc}
190
191
192\subsection{Long Integer Objects \label{longObjects}}
193
194\obindex{long integer}
195\begin{ctypedesc}{PyLongObject}
196 This subtype of \ctype{PyObject} represents a Python long integer
197 object.
198\end{ctypedesc}
199
200\begin{cvardesc}{PyTypeObject}{PyLong_Type}
201 This instance of \ctype{PyTypeObject} represents the Python long
202 integer type. This is the same object as \code{types.LongType}.
203 \withsubitem{(in modules types)}{\ttindex{LongType}}
204\end{cvardesc}
205
206\begin{cfuncdesc}{int}{PyLong_Check}{PyObject *p}
207 Returns true if its argument is a \ctype{PyLongObject} or a subtype
208 of \ctype{PyLongObject}.
209 \versionchanged[Allowed subtypes to be accepted]{2.2}
210\end{cfuncdesc}
211
212\begin{cfuncdesc}{int}{PyLong_CheckExact}{PyObject *p}
213 Returns true if its argument is a \ctype{PyLongObject}, but not a
214 subtype of \ctype{PyLongObject}.
215 \versionadded{2.2}
216\end{cfuncdesc}
217
218\begin{cfuncdesc}{PyObject*}{PyLong_FromLong}{long v}
219 Returns a new \ctype{PyLongObject} object from \var{v}, or \NULL{}
220 on failure.
221\end{cfuncdesc}
222
223\begin{cfuncdesc}{PyObject*}{PyLong_FromUnsignedLong}{unsigned long v}
224 Returns a new \ctype{PyLongObject} object from a C \ctype{unsigned
225 long}, or \NULL{} on failure.
226\end{cfuncdesc}
227
228\begin{cfuncdesc}{PyObject*}{PyLong_FromLongLong}{long long v}
229 Returns a new \ctype{PyLongObject} object from a C \ctype{long long},
230 or \NULL{} on failure.
231\end{cfuncdesc}
232
233\begin{cfuncdesc}{PyObject*}{PyLong_FromUnsignedLongLong}{unsigned long long v}
234 Returns a new \ctype{PyLongObject} object from a C \ctype{unsigned
235 long long}, or \NULL{} on failure.
236\end{cfuncdesc}
237
238\begin{cfuncdesc}{PyObject*}{PyLong_FromDouble}{double v}
239 Returns a new \ctype{PyLongObject} object from the integer part of
240 \var{v}, or \NULL{} on failure.
241\end{cfuncdesc}
242
243\begin{cfuncdesc}{PyObject*}{PyLong_FromString}{char *str, char **pend,
244 int base}
245 Return a new \ctype{PyLongObject} based on the string value in
246 \var{str}, which is interpreted according to the radix in
247 \var{base}. If \var{pend} is non-\NULL, \code{*\var{pend}} will
248 point to the first character in \var{str} which follows the
249 representation of the number. If \var{base} is \code{0}, the radix
Skip Montanaro1ff49a72003-02-03 05:13:24 +0000250 will be determined based on the leading characters of \var{str}: if
Fred Drake3adf79e2001-10-12 19:01:43 +0000251 \var{str} starts with \code{'0x'} or \code{'0X'}, radix 16 will be
252 used; if \var{str} starts with \code{'0'}, radix 8 will be used;
253 otherwise radix 10 will be used. If \var{base} is not \code{0}, it
254 must be between \code{2} and \code{36}, inclusive. Leading spaces
255 are ignored. If there are no digits, \exception{ValueError} will be
256 raised.
257\end{cfuncdesc}
258
259\begin{cfuncdesc}{PyObject*}{PyLong_FromUnicode}{Py_UNICODE *u,
260 int length, int base}
261 Convert a sequence of Unicode digits to a Python long integer
262 value. The first parameter, \var{u}, points to the first character
263 of the Unicode string, \var{length} gives the number of characters,
264 and \var{base} is the radix for the conversion. The radix must be
265 in the range [2, 36]; if it is out of range, \exception{ValueError}
266 will be raised.
267 \versionadded{1.6}
268\end{cfuncdesc}
269
270\begin{cfuncdesc}{PyObject*}{PyLong_FromVoidPtr}{void *p}
271 Create a Python integer or long integer from the pointer \var{p}.
272 The pointer value can be retrieved from the resulting value using
273 \cfunction{PyLong_AsVoidPtr()}.
274 \versionadded{1.5.2}
275\end{cfuncdesc}
276
277\begin{cfuncdesc}{long}{PyLong_AsLong}{PyObject *pylong}
278 Returns a C \ctype{long} representation of the contents of
279 \var{pylong}. If \var{pylong} is greater than
280 \constant{LONG_MAX}\ttindex{LONG_MAX}, an \exception{OverflowError}
281 is raised.
282 \withsubitem{(built-in exception)}{\ttindex{OverflowError}}
283\end{cfuncdesc}
284
285\begin{cfuncdesc}{unsigned long}{PyLong_AsUnsignedLong}{PyObject *pylong}
286 Returns a C \ctype{unsigned long} representation of the contents of
287 \var{pylong}. If \var{pylong} is greater than
288 \constant{ULONG_MAX}\ttindex{ULONG_MAX}, an
289 \exception{OverflowError} is raised.
Tim Petersf582b822001-12-11 18:51:08 +0000290 \withsubitem{(built-in exception)}{\ttindex{OverflowError}}
Fred Drake3adf79e2001-10-12 19:01:43 +0000291\end{cfuncdesc}
292
293\begin{cfuncdesc}{long long}{PyLong_AsLongLong}{PyObject *pylong}
294 Return a C \ctype{long long} from a Python long integer. If
295 \var{pylong} cannot be represented as a \ctype{long long}, an
296 \exception{OverflowError} will be raised.
297 \versionadded{2.2}
298\end{cfuncdesc}
299
300\begin{cfuncdesc}{unsigned long long}{PyLong_AsUnsignedLongLong}{PyObject
301 *pylong}
302 Return a C \ctype{unsigned long long} from a Python long integer.
303 If \var{pylong} cannot be represented as an \ctype{unsigned long
304 long}, an \exception{OverflowError} will be raised if the value is
305 positive, or a \exception{TypeError} will be raised if the value is
306 negative.
307 \versionadded{2.2}
308\end{cfuncdesc}
309
Thomas Heller34d7f092003-04-23 19:51:05 +0000310\begin{cfuncdesc}{unsigned long}{PyLong_AsUnsignedLongMask}{PyObject *io}
311 Return a C \ctype{unsigned long} from a Python long integer, without
312 checking for overflow.
313 \versionadded{2.3}
314\end{cfuncdesc}
315
316\begin{cfuncdesc}{unsigned long}{PyLong_AsUnsignedLongLongMask}{PyObject *io}
317 Return a C \ctype{unsigned long long} from a Python long integer, without
318 checking for overflow.
319 \versionadded{2.3}
320\end{cfuncdesc}
321
Fred Drake3adf79e2001-10-12 19:01:43 +0000322\begin{cfuncdesc}{double}{PyLong_AsDouble}{PyObject *pylong}
323 Returns a C \ctype{double} representation of the contents of
324 \var{pylong}. If \var{pylong} cannot be approximately represented
325 as a \ctype{double}, an \exception{OverflowError} exception is
326 raised and \code{-1.0} will be returned.
327\end{cfuncdesc}
328
329\begin{cfuncdesc}{void*}{PyLong_AsVoidPtr}{PyObject *pylong}
330 Convert a Python integer or long integer \var{pylong} to a C
331 \ctype{void} pointer. If \var{pylong} cannot be converted, an
332 \exception{OverflowError} will be raised. This is only assured to
333 produce a usable \ctype{void} pointer for values created with
334 \cfunction{PyLong_FromVoidPtr()}.
335 \versionadded{1.5.2}
336\end{cfuncdesc}
337
338
339\subsection{Floating Point Objects \label{floatObjects}}
340
341\obindex{floating point}
342\begin{ctypedesc}{PyFloatObject}
343 This subtype of \ctype{PyObject} represents a Python floating point
344 object.
345\end{ctypedesc}
346
347\begin{cvardesc}{PyTypeObject}{PyFloat_Type}
348 This instance of \ctype{PyTypeObject} represents the Python floating
349 point type. This is the same object as \code{types.FloatType}.
350 \withsubitem{(in modules types)}{\ttindex{FloatType}}
351\end{cvardesc}
352
353\begin{cfuncdesc}{int}{PyFloat_Check}{PyObject *p}
354 Returns true if its argument is a \ctype{PyFloatObject} or a subtype
355 of \ctype{PyFloatObject}.
356 \versionchanged[Allowed subtypes to be accepted]{2.2}
357\end{cfuncdesc}
358
359\begin{cfuncdesc}{int}{PyFloat_CheckExact}{PyObject *p}
360 Returns true if its argument is a \ctype{PyFloatObject}, but not a
361 subtype of \ctype{PyFloatObject}.
362 \versionadded{2.2}
363\end{cfuncdesc}
364
Skip Montanaro1ff49a72003-02-03 05:13:24 +0000365\begin{cfuncdesc}{PyObject*}{PyFloat_FromString}{PyObject *str, char **pend}
Skip Montanaroae31e9b2003-02-03 03:56:36 +0000366 Creates a \ctype{PyFloatObject} object based on the string value in
Skip Montanaro1ff49a72003-02-03 05:13:24 +0000367 \var{str}, or \NULL{} on failure. The \var{pend} argument is ignored. It
368 remains only for backward compatibility.
Skip Montanaroae31e9b2003-02-03 03:56:36 +0000369\end{cfuncdesc}
370
Fred Drake3adf79e2001-10-12 19:01:43 +0000371\begin{cfuncdesc}{PyObject*}{PyFloat_FromDouble}{double v}
372 Creates a \ctype{PyFloatObject} object from \var{v}, or \NULL{} on
373 failure.
374\end{cfuncdesc}
375
376\begin{cfuncdesc}{double}{PyFloat_AsDouble}{PyObject *pyfloat}
377 Returns a C \ctype{double} representation of the contents of
378 \var{pyfloat}.
379\end{cfuncdesc}
380
381\begin{cfuncdesc}{double}{PyFloat_AS_DOUBLE}{PyObject *pyfloat}
382 Returns a C \ctype{double} representation of the contents of
383 \var{pyfloat}, but without error checking.
384\end{cfuncdesc}
385
386
387\subsection{Complex Number Objects \label{complexObjects}}
388
389\obindex{complex number}
390Python's complex number objects are implemented as two distinct types
391when viewed from the C API: one is the Python object exposed to
392Python programs, and the other is a C structure which represents the
393actual complex number value. The API provides functions for working
394with both.
395
396\subsubsection{Complex Numbers as C Structures}
397
398Note that the functions which accept these structures as parameters
399and return them as results do so \emph{by value} rather than
400dereferencing them through pointers. This is consistent throughout
401the API.
402
403\begin{ctypedesc}{Py_complex}
404 The C structure which corresponds to the value portion of a Python
405 complex number object. Most of the functions for dealing with
406 complex number objects use structures of this type as input or
407 output values, as appropriate. It is defined as:
408
409\begin{verbatim}
410typedef struct {
411 double real;
412 double imag;
413} Py_complex;
414\end{verbatim}
415\end{ctypedesc}
416
417\begin{cfuncdesc}{Py_complex}{_Py_c_sum}{Py_complex left, Py_complex right}
418 Return the sum of two complex numbers, using the C
419 \ctype{Py_complex} representation.
420\end{cfuncdesc}
421
422\begin{cfuncdesc}{Py_complex}{_Py_c_diff}{Py_complex left, Py_complex right}
423 Return the difference between two complex numbers, using the C
424 \ctype{Py_complex} representation.
425\end{cfuncdesc}
426
427\begin{cfuncdesc}{Py_complex}{_Py_c_neg}{Py_complex complex}
428 Return the negation of the complex number \var{complex}, using the C
429 \ctype{Py_complex} representation.
430\end{cfuncdesc}
431
432\begin{cfuncdesc}{Py_complex}{_Py_c_prod}{Py_complex left, Py_complex right}
433 Return the product of two complex numbers, using the C
434 \ctype{Py_complex} representation.
435\end{cfuncdesc}
436
437\begin{cfuncdesc}{Py_complex}{_Py_c_quot}{Py_complex dividend,
438 Py_complex divisor}
439 Return the quotient of two complex numbers, using the C
440 \ctype{Py_complex} representation.
441\end{cfuncdesc}
442
443\begin{cfuncdesc}{Py_complex}{_Py_c_pow}{Py_complex num, Py_complex exp}
444 Return the exponentiation of \var{num} by \var{exp}, using the C
445 \ctype{Py_complex} representation.
446\end{cfuncdesc}
447
448
449\subsubsection{Complex Numbers as Python Objects}
450
451\begin{ctypedesc}{PyComplexObject}
452 This subtype of \ctype{PyObject} represents a Python complex number
453 object.
454\end{ctypedesc}
455
456\begin{cvardesc}{PyTypeObject}{PyComplex_Type}
457 This instance of \ctype{PyTypeObject} represents the Python complex
458 number type.
459\end{cvardesc}
460
461\begin{cfuncdesc}{int}{PyComplex_Check}{PyObject *p}
462 Returns true if its argument is a \ctype{PyComplexObject} or a
463 subtype of \ctype{PyComplexObject}.
464 \versionchanged[Allowed subtypes to be accepted]{2.2}
465\end{cfuncdesc}
466
467\begin{cfuncdesc}{int}{PyComplex_CheckExact}{PyObject *p}
468 Returns true if its argument is a \ctype{PyComplexObject}, but not a
469 subtype of \ctype{PyComplexObject}.
470 \versionadded{2.2}
471\end{cfuncdesc}
472
473\begin{cfuncdesc}{PyObject*}{PyComplex_FromCComplex}{Py_complex v}
474 Create a new Python complex number object from a C
475 \ctype{Py_complex} value.
476\end{cfuncdesc}
477
478\begin{cfuncdesc}{PyObject*}{PyComplex_FromDoubles}{double real, double imag}
479 Returns a new \ctype{PyComplexObject} object from \var{real} and
480 \var{imag}.
481\end{cfuncdesc}
482
483\begin{cfuncdesc}{double}{PyComplex_RealAsDouble}{PyObject *op}
484 Returns the real part of \var{op} as a C \ctype{double}.
485\end{cfuncdesc}
486
487\begin{cfuncdesc}{double}{PyComplex_ImagAsDouble}{PyObject *op}
488 Returns the imaginary part of \var{op} as a C \ctype{double}.
489\end{cfuncdesc}
490
491\begin{cfuncdesc}{Py_complex}{PyComplex_AsCComplex}{PyObject *op}
492 Returns the \ctype{Py_complex} value of the complex number
493 \var{op}.
494\end{cfuncdesc}
495
496
497
498\section{Sequence Objects \label{sequenceObjects}}
499
500\obindex{sequence}
Tim Petersf582b822001-12-11 18:51:08 +0000501Generic operations on sequence objects were discussed in the previous
502chapter; this section deals with the specific kinds of sequence
Fred Drake3adf79e2001-10-12 19:01:43 +0000503objects that are intrinsic to the Python language.
504
505
506\subsection{String Objects \label{stringObjects}}
507
508These functions raise \exception{TypeError} when expecting a string
509parameter and are called with a non-string parameter.
510
511\obindex{string}
512\begin{ctypedesc}{PyStringObject}
513 This subtype of \ctype{PyObject} represents a Python string object.
514\end{ctypedesc}
515
516\begin{cvardesc}{PyTypeObject}{PyString_Type}
517 This instance of \ctype{PyTypeObject} represents the Python string
518 type; it is the same object as \code{types.TypeType} in the Python
519 layer.
520 \withsubitem{(in module types)}{\ttindex{StringType}}.
521\end{cvardesc}
522
523\begin{cfuncdesc}{int}{PyString_Check}{PyObject *o}
524 Returns true if the object \var{o} is a string object or an instance
525 of a subtype of the string type.
526 \versionchanged[Allowed subtypes to be accepted]{2.2}
527\end{cfuncdesc}
528
529\begin{cfuncdesc}{int}{PyString_CheckExact}{PyObject *o}
530 Returns true if the object \var{o} is a string object, but not an
531 instance of a subtype of the string type.
532 \versionadded{2.2}
533\end{cfuncdesc}
534
535\begin{cfuncdesc}{PyObject*}{PyString_FromString}{const char *v}
536 Returns a new string object with the value \var{v} on success, and
Fred Drake32a35872001-12-06 20:38:15 +0000537 \NULL{} on failure. The parameter \var{v} must not be \NULL; it
538 will not be checked.
Fred Drake3adf79e2001-10-12 19:01:43 +0000539\end{cfuncdesc}
540
541\begin{cfuncdesc}{PyObject*}{PyString_FromStringAndSize}{const char *v,
542 int len}
543 Returns a new string object with the value \var{v} and length
544 \var{len} on success, and \NULL{} on failure. If \var{v} is
545 \NULL, the contents of the string are uninitialized.
546\end{cfuncdesc}
547
548\begin{cfuncdesc}{PyObject*}{PyString_FromFormat}{const char *format, ...}
549 Takes a C \cfunction{printf()}-style \var{format} string and a
550 variable number of arguments, calculates the size of the resulting
551 Python string and returns a string with the values formatted into
552 it. The variable arguments must be C types and must correspond
553 exactly to the format characters in the \var{format} string. The
554 following format characters are allowed:
555
556 \begin{tableiii}{l|l|l}{member}{Format Characters}{Type}{Comment}
557 \lineiii{\%\%}{\emph{n/a}}{The literal \% character.}
558 \lineiii{\%c}{int}{A single character, represented as an C int.}
559 \lineiii{\%d}{int}{Exactly equivalent to \code{printf("\%d")}.}
560 \lineiii{\%ld}{long}{Exactly equivalent to \code{printf("\%ld")}.}
561 \lineiii{\%i}{int}{Exactly equivalent to \code{printf("\%i")}.}
562 \lineiii{\%x}{int}{Exactly equivalent to \code{printf("\%x")}.}
563 \lineiii{\%s}{char*}{A null-terminated C character array.}
564 \lineiii{\%p}{void*}{The hex representation of a C pointer.
565 Mostly equivalent to \code{printf("\%p")} except that it is
566 guaranteed to start with the literal \code{0x} regardless of
567 what the platform's \code{printf} yields.}
568 \end{tableiii}
569\end{cfuncdesc}
570
571\begin{cfuncdesc}{PyObject*}{PyString_FromFormatV}{const char *format,
572 va_list vargs}
573 Identical to \function{PyString_FromFormat()} except that it takes
574 exactly two arguments.
575\end{cfuncdesc}
576
577\begin{cfuncdesc}{int}{PyString_Size}{PyObject *string}
578 Returns the length of the string in string object \var{string}.
579\end{cfuncdesc}
580
581\begin{cfuncdesc}{int}{PyString_GET_SIZE}{PyObject *string}
582 Macro form of \cfunction{PyString_Size()} but without error
583 checking.
584\end{cfuncdesc}
585
586\begin{cfuncdesc}{char*}{PyString_AsString}{PyObject *string}
Fred Drake4b247262002-10-22 20:20:20 +0000587 Returns a NUL-terminated representation of the contents of
Fred Drake3adf79e2001-10-12 19:01:43 +0000588 \var{string}. The pointer refers to the internal buffer of
589 \var{string}, not a copy. The data must not be modified in any way,
590 unless the string was just created using
591 \code{PyString_FromStringAndSize(NULL, \var{size})}.
Fred Drake4b247262002-10-22 20:20:20 +0000592 It must not be deallocated. If \var{string} is a Unicode object,
593 this function computes the default encoding of \var{string} and
594 operates on that. If \var{string} is not a string object at all,
595 \cfunction{PyString_AsString()} returns \NULL{} and raises
596 \exception{TypeError}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000597\end{cfuncdesc}
598
599\begin{cfuncdesc}{char*}{PyString_AS_STRING}{PyObject *string}
600 Macro form of \cfunction{PyString_AsString()} but without error
Fred Drake4b247262002-10-22 20:20:20 +0000601 checking. Only string objects are supported; no Unicode objects
602 should be passed.
Fred Drake3adf79e2001-10-12 19:01:43 +0000603\end{cfuncdesc}
604
605\begin{cfuncdesc}{int}{PyString_AsStringAndSize}{PyObject *obj,
606 char **buffer,
607 int *length}
Fred Drake4b247262002-10-22 20:20:20 +0000608 Returns a NUL-terminated representation of the contents of the
Fred Drake3adf79e2001-10-12 19:01:43 +0000609 object \var{obj} through the output variables \var{buffer} and
610 \var{length}.
611
612 The function accepts both string and Unicode objects as input. For
613 Unicode objects it returns the default encoded version of the
Fred Drake4b247262002-10-22 20:20:20 +0000614 object. If \var{length} is \NULL, the resulting buffer may not
615 contain NUL characters; if it does, the function returns \code{-1}
616 and a \exception{TypeError} is raised.
Fred Drake3adf79e2001-10-12 19:01:43 +0000617
618 The buffer refers to an internal string buffer of \var{obj}, not a
619 copy. The data must not be modified in any way, unless the string
620 was just created using \code{PyString_FromStringAndSize(NULL,
Fred Drake4b247262002-10-22 20:20:20 +0000621 \var{size})}. It must not be deallocated. If \var{string} is a
622 Unicode object, this function computes the default encoding of
623 \var{string} and operates on that. If \var{string} is not a string
624 object at all, \cfunction{PyString_AsString()} returns \NULL{} and
625 raises \exception{TypeError}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000626\end{cfuncdesc}
627
628\begin{cfuncdesc}{void}{PyString_Concat}{PyObject **string,
629 PyObject *newpart}
630 Creates a new string object in \var{*string} containing the contents
631 of \var{newpart} appended to \var{string}; the caller will own the
632 new reference. The reference to the old value of \var{string} will
633 be stolen. If the new string cannot be created, the old reference
634 to \var{string} will still be discarded and the value of
635 \var{*string} will be set to \NULL; the appropriate exception will
636 be set.
637\end{cfuncdesc}
638
639\begin{cfuncdesc}{void}{PyString_ConcatAndDel}{PyObject **string,
640 PyObject *newpart}
641 Creates a new string object in \var{*string} containing the contents
642 of \var{newpart} appended to \var{string}. This version decrements
643 the reference count of \var{newpart}.
644\end{cfuncdesc}
645
646\begin{cfuncdesc}{int}{_PyString_Resize}{PyObject **string, int newsize}
647 A way to resize a string object even though it is ``immutable''.
648 Only use this to build up a brand new string object; don't use this
Tim Peters5de98422002-04-27 18:44:32 +0000649 if the string may already be known in other parts of the code. It
650 is an error to call this function if the refcount on the input string
651 object is not one.
652 Pass the address of an existing string object as an lvalue (it may
653 be written into), and the new size desired. On success, \var{*string}
Fred Drake432425e2002-04-29 15:17:16 +0000654 holds the resized string object and \code{0} is returned; the address in
Tim Peters5de98422002-04-27 18:44:32 +0000655 \var{*string} may differ from its input value. If the
656 reallocation fails, the original string object at \var{*string} is
657 deallocated, \var{*string} is set to \NULL{}, a memory exception is set,
Fred Drake432425e2002-04-29 15:17:16 +0000658 and \code{-1} is returned.
Fred Drake3adf79e2001-10-12 19:01:43 +0000659\end{cfuncdesc}
660
661\begin{cfuncdesc}{PyObject*}{PyString_Format}{PyObject *format,
662 PyObject *args}
663 Returns a new string object from \var{format} and \var{args}.
664 Analogous to \code{\var{format} \%\ \var{args}}. The \var{args}
665 argument must be a tuple.
666\end{cfuncdesc}
667
668\begin{cfuncdesc}{void}{PyString_InternInPlace}{PyObject **string}
669 Intern the argument \var{*string} in place. The argument must be
670 the address of a pointer variable pointing to a Python string
671 object. If there is an existing interned string that is the same as
672 \var{*string}, it sets \var{*string} to it (decrementing the
673 reference count of the old string object and incrementing the
674 reference count of the interned string object), otherwise it leaves
675 \var{*string} alone and interns it (incrementing its reference
676 count). (Clarification: even though there is a lot of talk about
677 reference counts, think of this function as reference-count-neutral;
678 you own the object after the call if and only if you owned it before
679 the call.)
680\end{cfuncdesc}
681
682\begin{cfuncdesc}{PyObject*}{PyString_InternFromString}{const char *v}
683 A combination of \cfunction{PyString_FromString()} and
684 \cfunction{PyString_InternInPlace()}, returning either a new string
685 object that has been interned, or a new (``owned'') reference to an
686 earlier interned string object with the same value.
687\end{cfuncdesc}
688
689\begin{cfuncdesc}{PyObject*}{PyString_Decode}{const char *s,
690 int size,
691 const char *encoding,
692 const char *errors}
693 Creates an object by decoding \var{size} bytes of the encoded
694 buffer \var{s} using the codec registered for
695 \var{encoding}. \var{encoding} and \var{errors} have the same
696 meaning as the parameters of the same name in the
697 \function{unicode()} built-in function. The codec to be used is
698 looked up using the Python codec registry. Returns \NULL{} if
699 an exception was raised by the codec.
700\end{cfuncdesc}
701
702\begin{cfuncdesc}{PyObject*}{PyString_AsDecodedObject}{PyObject *str,
703 const char *encoding,
704 const char *errors}
705 Decodes a string object by passing it to the codec registered for
706 \var{encoding} and returns the result as Python
707 object. \var{encoding} and \var{errors} have the same meaning as the
708 parameters of the same name in the string \method{encode()} method.
709 The codec to be used is looked up using the Python codec registry.
710 Returns \NULL{} if an exception was raised by the codec.
711\end{cfuncdesc}
712
713\begin{cfuncdesc}{PyObject*}{PyString_Encode}{const char *s,
714 int size,
715 const char *encoding,
716 const char *errors}
717 Encodes the \ctype{char} buffer of the given size by passing it to
718 the codec registered for \var{encoding} and returns a Python object.
719 \var{encoding} and \var{errors} have the same meaning as the
720 parameters of the same name in the string \method{encode()} method.
721 The codec to be used is looked up using the Python codec
722 registry. Returns \NULL{} if an exception was raised by the
723 codec.
724\end{cfuncdesc}
725
726\begin{cfuncdesc}{PyObject*}{PyString_AsEncodedObject}{PyObject *str,
727 const char *encoding,
728 const char *errors}
729 Encodes a string object using the codec registered for
730 \var{encoding} and returns the result as Python object.
731 \var{encoding} and \var{errors} have the same meaning as the
732 parameters of the same name in the string \method{encode()} method.
733 The codec to be used is looked up using the Python codec registry.
734 Returns \NULL{} if an exception was raised by the codec.
735\end{cfuncdesc}
736
737
738\subsection{Unicode Objects \label{unicodeObjects}}
739\sectionauthor{Marc-Andre Lemburg}{mal@lemburg.com}
740
741%--- Unicode Type -------------------------------------------------------
742
743These are the basic Unicode object types used for the Unicode
744implementation in Python:
745
746\begin{ctypedesc}{Py_UNICODE}
747 This type represents a 16-bit unsigned storage type which is used by
748 Python internally as basis for holding Unicode ordinals. On
749 platforms where \ctype{wchar_t} is available and also has 16-bits,
750 \ctype{Py_UNICODE} is a typedef alias for \ctype{wchar_t} to enhance
751 native platform compatibility. On all other platforms,
752 \ctype{Py_UNICODE} is a typedef alias for \ctype{unsigned short}.
753\end{ctypedesc}
754
755\begin{ctypedesc}{PyUnicodeObject}
756 This subtype of \ctype{PyObject} represents a Python Unicode object.
757\end{ctypedesc}
758
759\begin{cvardesc}{PyTypeObject}{PyUnicode_Type}
760 This instance of \ctype{PyTypeObject} represents the Python Unicode
761 type.
762\end{cvardesc}
763
764The following APIs are really C macros and can be used to do fast
765checks and to access internal read-only data of Unicode objects:
766
767\begin{cfuncdesc}{int}{PyUnicode_Check}{PyObject *o}
768 Returns true if the object \var{o} is a Unicode object or an
769 instance of a Unicode subtype.
770 \versionchanged[Allowed subtypes to be accepted]{2.2}
771\end{cfuncdesc}
772
773\begin{cfuncdesc}{int}{PyUnicode_CheckExact}{PyObject *o}
774 Returns true if the object \var{o} is a Unicode object, but not an
775 instance of a subtype.
776 \versionadded{2.2}
777\end{cfuncdesc}
778
779\begin{cfuncdesc}{int}{PyUnicode_GET_SIZE}{PyObject *o}
780 Returns the size of the object. \var{o} has to be a
781 \ctype{PyUnicodeObject} (not checked).
782\end{cfuncdesc}
783
784\begin{cfuncdesc}{int}{PyUnicode_GET_DATA_SIZE}{PyObject *o}
785 Returns the size of the object's internal buffer in bytes. \var{o}
786 has to be a \ctype{PyUnicodeObject} (not checked).
787\end{cfuncdesc}
788
789\begin{cfuncdesc}{Py_UNICODE*}{PyUnicode_AS_UNICODE}{PyObject *o}
790 Returns a pointer to the internal \ctype{Py_UNICODE} buffer of the
791 object. \var{o} has to be a \ctype{PyUnicodeObject} (not checked).
792\end{cfuncdesc}
793
794\begin{cfuncdesc}{const char*}{PyUnicode_AS_DATA}{PyObject *o}
795 Returns a pointer to the internal buffer of the object.
796 \var{o} has to be a \ctype{PyUnicodeObject} (not checked).
797\end{cfuncdesc}
798
799% --- Unicode character properties ---------------------------------------
800
801Unicode provides many different character properties. The most often
802needed ones are available through these macros which are mapped to C
803functions depending on the Python configuration.
804
805\begin{cfuncdesc}{int}{Py_UNICODE_ISSPACE}{Py_UNICODE ch}
806 Returns 1/0 depending on whether \var{ch} is a whitespace
807 character.
808\end{cfuncdesc}
809
810\begin{cfuncdesc}{int}{Py_UNICODE_ISLOWER}{Py_UNICODE ch}
811 Returns 1/0 depending on whether \var{ch} is a lowercase character.
812\end{cfuncdesc}
813
814\begin{cfuncdesc}{int}{Py_UNICODE_ISUPPER}{Py_UNICODE ch}
815 Returns 1/0 depending on whether \var{ch} is an uppercase
816 character.
817\end{cfuncdesc}
818
819\begin{cfuncdesc}{int}{Py_UNICODE_ISTITLE}{Py_UNICODE ch}
820 Returns 1/0 depending on whether \var{ch} is a titlecase character.
821\end{cfuncdesc}
822
823\begin{cfuncdesc}{int}{Py_UNICODE_ISLINEBREAK}{Py_UNICODE ch}
824 Returns 1/0 depending on whether \var{ch} is a linebreak character.
825\end{cfuncdesc}
826
827\begin{cfuncdesc}{int}{Py_UNICODE_ISDECIMAL}{Py_UNICODE ch}
828 Returns 1/0 depending on whether \var{ch} is a decimal character.
829\end{cfuncdesc}
830
831\begin{cfuncdesc}{int}{Py_UNICODE_ISDIGIT}{Py_UNICODE ch}
832 Returns 1/0 depending on whether \var{ch} is a digit character.
833\end{cfuncdesc}
834
835\begin{cfuncdesc}{int}{Py_UNICODE_ISNUMERIC}{Py_UNICODE ch}
836 Returns 1/0 depending on whether \var{ch} is a numeric character.
837\end{cfuncdesc}
838
839\begin{cfuncdesc}{int}{Py_UNICODE_ISALPHA}{Py_UNICODE ch}
840 Returns 1/0 depending on whether \var{ch} is an alphabetic
841 character.
842\end{cfuncdesc}
843
844\begin{cfuncdesc}{int}{Py_UNICODE_ISALNUM}{Py_UNICODE ch}
845 Returns 1/0 depending on whether \var{ch} is an alphanumeric
846 character.
847\end{cfuncdesc}
848
849These APIs can be used for fast direct character conversions:
850
851\begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOLOWER}{Py_UNICODE ch}
852 Returns the character \var{ch} converted to lower case.
853\end{cfuncdesc}
854
855\begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOUPPER}{Py_UNICODE ch}
856 Returns the character \var{ch} converted to upper case.
857\end{cfuncdesc}
858
859\begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOTITLE}{Py_UNICODE ch}
860 Returns the character \var{ch} converted to title case.
861\end{cfuncdesc}
862
863\begin{cfuncdesc}{int}{Py_UNICODE_TODECIMAL}{Py_UNICODE ch}
864 Returns the character \var{ch} converted to a decimal positive
865 integer. Returns \code{-1} if this is not possible. Does not raise
866 exceptions.
867\end{cfuncdesc}
868
869\begin{cfuncdesc}{int}{Py_UNICODE_TODIGIT}{Py_UNICODE ch}
870 Returns the character \var{ch} converted to a single digit integer.
871 Returns \code{-1} if this is not possible. Does not raise
872 exceptions.
873\end{cfuncdesc}
874
875\begin{cfuncdesc}{double}{Py_UNICODE_TONUMERIC}{Py_UNICODE ch}
876 Returns the character \var{ch} converted to a (positive) double.
877 Returns \code{-1.0} if this is not possible. Does not raise
878 exceptions.
879\end{cfuncdesc}
880
881% --- Plain Py_UNICODE ---------------------------------------------------
882
883To create Unicode objects and access their basic sequence properties,
884use these APIs:
885
886\begin{cfuncdesc}{PyObject*}{PyUnicode_FromUnicode}{const Py_UNICODE *u,
Tim Petersf582b822001-12-11 18:51:08 +0000887 int size}
Fred Drake3adf79e2001-10-12 19:01:43 +0000888 Create a Unicode Object from the Py_UNICODE buffer \var{u} of the
889 given size. \var{u} may be \NULL{} which causes the contents to be
890 undefined. It is the user's responsibility to fill in the needed
891 data. The buffer is copied into the new object. If the buffer is
892 not \NULL, the return value might be a shared object. Therefore,
893 modification of the resulting Unicode object is only allowed when
894 \var{u} is \NULL.
895\end{cfuncdesc}
896
897\begin{cfuncdesc}{Py_UNICODE*}{PyUnicode_AsUnicode}{PyObject *unicode}
898 Return a read-only pointer to the Unicode object's internal
899 \ctype{Py_UNICODE} buffer, \NULL{} if \var{unicode} is not a Unicode
900 object.
901\end{cfuncdesc}
902
903\begin{cfuncdesc}{int}{PyUnicode_GetSize}{PyObject *unicode}
904 Return the length of the Unicode object.
905\end{cfuncdesc}
906
907\begin{cfuncdesc}{PyObject*}{PyUnicode_FromEncodedObject}{PyObject *obj,
908 const char *encoding,
909 const char *errors}
910 Coerce an encoded object \var{obj} to an Unicode object and return a
911 reference with incremented refcount.
912
913 Coercion is done in the following way:
914
915\begin{enumerate}
916\item Unicode objects are passed back as-is with incremented
917 refcount. \note{These cannot be decoded; passing a non-\NULL{}
918 value for encoding will result in a \exception{TypeError}.}
919
920\item String and other char buffer compatible objects are decoded
921 according to the given encoding and using the error handling
922 defined by errors. Both can be \NULL{} to have the interface
923 use the default values (see the next section for details).
924
925\item All other objects cause an exception.
926\end{enumerate}
927
928 The API returns \NULL{} if there was an error. The caller is
929 responsible for decref'ing the returned objects.
930\end{cfuncdesc}
931
932\begin{cfuncdesc}{PyObject*}{PyUnicode_FromObject}{PyObject *obj}
933 Shortcut for \code{PyUnicode_FromEncodedObject(obj, NULL, "strict")}
934 which is used throughout the interpreter whenever coercion to
935 Unicode is needed.
936\end{cfuncdesc}
937
938% --- wchar_t support for platforms which support it ---------------------
939
940If the platform supports \ctype{wchar_t} and provides a header file
941wchar.h, Python can interface directly to this type using the
942following functions. Support is optimized if Python's own
943\ctype{Py_UNICODE} type is identical to the system's \ctype{wchar_t}.
944
945\begin{cfuncdesc}{PyObject*}{PyUnicode_FromWideChar}{const wchar_t *w,
946 int size}
Thomas Heller541703b2002-04-29 17:28:43 +0000947 Create a Unicode object from the \ctype{wchar_t} buffer \var{w} of
Fred Drake3adf79e2001-10-12 19:01:43 +0000948 the given size. Returns \NULL{} on failure.
949\end{cfuncdesc}
950
951\begin{cfuncdesc}{int}{PyUnicode_AsWideChar}{PyUnicodeObject *unicode,
952 wchar_t *w,
953 int size}
Thomas Heller541703b2002-04-29 17:28:43 +0000954 Copies the Unicode object contents into the \ctype{wchar_t} buffer
955 \var{w}. At most \var{size} \ctype{wchar_t} characters are copied.
956 Returns the number of \ctype{wchar_t} characters copied or -1 in
Fred Drake3adf79e2001-10-12 19:01:43 +0000957 case of an error.
958\end{cfuncdesc}
959
960
961\subsubsection{Built-in Codecs \label{builtinCodecs}}
962
963Python provides a set of builtin codecs which are written in C
964for speed. All of these codecs are directly usable via the
965following functions.
966
967Many of the following APIs take two arguments encoding and
968errors. These parameters encoding and errors have the same semantics
969as the ones of the builtin unicode() Unicode object constructor.
970
971Setting encoding to \NULL{} causes the default encoding to be used
972which is \ASCII. The file system calls should use
973\cdata{Py_FileSystemDefaultEncoding} as the encoding for file
974names. This variable should be treated as read-only: On some systems,
975it will be a pointer to a static string, on others, it will change at
Raymond Hettingercb2da432003-10-12 18:24:34 +0000976run-time (such as when the application invokes setlocale).
Fred Drake3adf79e2001-10-12 19:01:43 +0000977
978Error handling is set by errors which may also be set to \NULL{}
979meaning to use the default handling defined for the codec. Default
980error handling for all builtin codecs is ``strict''
981(\exception{ValueError} is raised).
982
983The codecs all use a similar interface. Only deviation from the
984following generic ones are documented for simplicity.
985
986% --- Generic Codecs -----------------------------------------------------
987
988These are the generic codec APIs:
989
990\begin{cfuncdesc}{PyObject*}{PyUnicode_Decode}{const char *s,
991 int size,
992 const char *encoding,
993 const char *errors}
994 Create a Unicode object by decoding \var{size} bytes of the encoded
995 string \var{s}. \var{encoding} and \var{errors} have the same
996 meaning as the parameters of the same name in the
997 \function{unicode()} builtin function. The codec to be used is
998 looked up using the Python codec registry. Returns \NULL{} if an
999 exception was raised by the codec.
1000\end{cfuncdesc}
1001
1002\begin{cfuncdesc}{PyObject*}{PyUnicode_Encode}{const Py_UNICODE *s,
1003 int size,
1004 const char *encoding,
1005 const char *errors}
1006 Encodes the \ctype{Py_UNICODE} buffer of the given size and returns
1007 a Python string object. \var{encoding} and \var{errors} have the
1008 same meaning as the parameters of the same name in the Unicode
1009 \method{encode()} method. The codec to be used is looked up using
1010 the Python codec registry. Returns \NULL{} if an exception was
1011 raised by the codec.
1012\end{cfuncdesc}
1013
1014\begin{cfuncdesc}{PyObject*}{PyUnicode_AsEncodedString}{PyObject *unicode,
1015 const char *encoding,
1016 const char *errors}
1017 Encodes a Unicode object and returns the result as Python string
1018 object. \var{encoding} and \var{errors} have the same meaning as the
1019 parameters of the same name in the Unicode \method{encode()} method.
1020 The codec to be used is looked up using the Python codec registry.
1021 Returns \NULL{} if an exception was raised by the codec.
1022\end{cfuncdesc}
1023
1024% --- UTF-8 Codecs -------------------------------------------------------
1025
1026These are the UTF-8 codec APIs:
1027
1028\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF8}{const char *s,
1029 int size,
1030 const char *errors}
1031 Creates a Unicode object by decoding \var{size} bytes of the UTF-8
1032 encoded string \var{s}. Returns \NULL{} if an exception was raised
1033 by the codec.
1034\end{cfuncdesc}
1035
1036\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUTF8}{const Py_UNICODE *s,
1037 int size,
1038 const char *errors}
1039 Encodes the \ctype{Py_UNICODE} buffer of the given size using UTF-8
1040 and returns a Python string object. Returns \NULL{} if an exception
1041 was raised by the codec.
1042\end{cfuncdesc}
1043
1044\begin{cfuncdesc}{PyObject*}{PyUnicode_AsUTF8String}{PyObject *unicode}
1045 Encodes a Unicode objects using UTF-8 and returns the result as
1046 Python string object. Error handling is ``strict''. Returns
1047 \NULL{} if an exception was raised by the codec.
1048\end{cfuncdesc}
1049
1050% --- UTF-16 Codecs ------------------------------------------------------ */
1051
1052These are the UTF-16 codec APIs:
1053
1054\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF16}{const char *s,
1055 int size,
1056 const char *errors,
1057 int *byteorder}
1058 Decodes \var{length} bytes from a UTF-16 encoded buffer string and
1059 returns the corresponding Unicode object. \var{errors} (if
1060 non-\NULL) defines the error handling. It defaults to ``strict''.
1061
1062 If \var{byteorder} is non-\NULL, the decoder starts decoding using
1063 the given byte order:
1064
1065\begin{verbatim}
1066 *byteorder == -1: little endian
1067 *byteorder == 0: native order
1068 *byteorder == 1: big endian
1069\end{verbatim}
1070
1071 and then switches according to all byte order marks (BOM) it finds
1072 in the input data. BOMs are not copied into the resulting Unicode
1073 string. After completion, \var{*byteorder} is set to the current
1074 byte order at the end of input data.
1075
1076 If \var{byteorder} is \NULL, the codec starts in native order mode.
1077
1078 Returns \NULL{} if an exception was raised by the codec.
1079\end{cfuncdesc}
1080
1081\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUTF16}{const Py_UNICODE *s,
1082 int size,
1083 const char *errors,
1084 int byteorder}
1085 Returns a Python string object holding the UTF-16 encoded value of
1086 the Unicode data in \var{s}. If \var{byteorder} is not \code{0},
1087 output is written according to the following byte order:
1088
1089\begin{verbatim}
1090 byteorder == -1: little endian
1091 byteorder == 0: native byte order (writes a BOM mark)
1092 byteorder == 1: big endian
1093\end{verbatim}
1094
1095 If byteorder is \code{0}, the output string will always start with
1096 the Unicode BOM mark (U+FEFF). In the other two modes, no BOM mark
1097 is prepended.
1098
1099 Note that \ctype{Py_UNICODE} data is being interpreted as UTF-16
1100 reduced to UCS-2. This trick makes it possible to add full UTF-16
1101 capabilities at a later point without comprimising the APIs.
1102
1103 Returns \NULL{} if an exception was raised by the codec.
1104\end{cfuncdesc}
1105
1106\begin{cfuncdesc}{PyObject*}{PyUnicode_AsUTF16String}{PyObject *unicode}
1107 Returns a Python string using the UTF-16 encoding in native byte
1108 order. The string always starts with a BOM mark. Error handling is
1109 ``strict''. Returns \NULL{} if an exception was raised by the
1110 codec.
1111\end{cfuncdesc}
1112
1113% --- Unicode-Escape Codecs ----------------------------------------------
1114
Martin v. Löwis95cf84a2003-10-19 07:32:24 +00001115These are the ``Unicode Escape'' codec APIs:
Fred Drake3adf79e2001-10-12 19:01:43 +00001116
1117\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUnicodeEscape}{const char *s,
1118 int size,
1119 const char *errors}
1120 Creates a Unicode object by decoding \var{size} bytes of the
1121 Unicode-Escape encoded string \var{s}. Returns \NULL{} if an
1122 exception was raised by the codec.
1123\end{cfuncdesc}
1124
1125\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUnicodeEscape}{const Py_UNICODE *s,
1126 int size,
1127 const char *errors}
1128 Encodes the \ctype{Py_UNICODE} buffer of the given size using
1129 Unicode-Escape and returns a Python string object. Returns \NULL{}
1130 if an exception was raised by the codec.
1131\end{cfuncdesc}
1132
1133\begin{cfuncdesc}{PyObject*}{PyUnicode_AsUnicodeEscapeString}{PyObject *unicode}
1134 Encodes a Unicode objects using Unicode-Escape and returns the
1135 result as Python string object. Error handling is ``strict''.
1136 Returns \NULL{} if an exception was raised by the codec.
1137\end{cfuncdesc}
1138
1139% --- Raw-Unicode-Escape Codecs ------------------------------------------
1140
Martin v. Löwis95cf84a2003-10-19 07:32:24 +00001141These are the ``Raw Unicode Escape'' codec APIs:
Fred Drake3adf79e2001-10-12 19:01:43 +00001142
1143\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeRawUnicodeEscape}{const char *s,
1144 int size,
1145 const char *errors}
1146 Creates a Unicode object by decoding \var{size} bytes of the
Martin v. Löwis95cf84a2003-10-19 07:32:24 +00001147 Raw-Unicode-Escape encoded string \var{s}. Returns \NULL{} if an
Fred Drake3adf79e2001-10-12 19:01:43 +00001148 exception was raised by the codec.
1149\end{cfuncdesc}
1150
1151\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeRawUnicodeEscape}{const Py_UNICODE *s,
1152 int size,
1153 const char *errors}
1154 Encodes the \ctype{Py_UNICODE} buffer of the given size using
1155 Raw-Unicode-Escape and returns a Python string object. Returns
1156 \NULL{} if an exception was raised by the codec.
1157\end{cfuncdesc}
1158
1159\begin{cfuncdesc}{PyObject*}{PyUnicode_AsRawUnicodeEscapeString}{PyObject *unicode}
1160 Encodes a Unicode objects using Raw-Unicode-Escape and returns the
1161 result as Python string object. Error handling is ``strict''.
1162 Returns \NULL{} if an exception was raised by the codec.
1163\end{cfuncdesc}
1164
Tim Petersf582b822001-12-11 18:51:08 +00001165% --- Latin-1 Codecs -----------------------------------------------------
Fred Drake3adf79e2001-10-12 19:01:43 +00001166
1167These are the Latin-1 codec APIs:
1168Latin-1 corresponds to the first 256 Unicode ordinals and only these
1169are accepted by the codecs during encoding.
1170
1171\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeLatin1}{const char *s,
1172 int size,
1173 const char *errors}
1174 Creates a Unicode object by decoding \var{size} bytes of the Latin-1
1175 encoded string \var{s}. Returns \NULL{} if an exception was raised
1176 by the codec.
1177\end{cfuncdesc}
1178
1179\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeLatin1}{const Py_UNICODE *s,
1180 int size,
1181 const char *errors}
1182 Encodes the \ctype{Py_UNICODE} buffer of the given size using
1183 Latin-1 and returns a Python string object. Returns \NULL{} if an
1184 exception was raised by the codec.
1185\end{cfuncdesc}
1186
1187\begin{cfuncdesc}{PyObject*}{PyUnicode_AsLatin1String}{PyObject *unicode}
1188 Encodes a Unicode objects using Latin-1 and returns the result as
1189 Python string object. Error handling is ``strict''. Returns
1190 \NULL{} if an exception was raised by the codec.
1191\end{cfuncdesc}
1192
Tim Petersf582b822001-12-11 18:51:08 +00001193% --- ASCII Codecs -------------------------------------------------------
Fred Drake3adf79e2001-10-12 19:01:43 +00001194
1195These are the \ASCII{} codec APIs. Only 7-bit \ASCII{} data is
1196accepted. All other codes generate errors.
1197
1198\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeASCII}{const char *s,
1199 int size,
1200 const char *errors}
1201 Creates a Unicode object by decoding \var{size} bytes of the
1202 \ASCII{} encoded string \var{s}. Returns \NULL{} if an exception
1203 was raised by the codec.
1204\end{cfuncdesc}
1205
1206\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeASCII}{const Py_UNICODE *s,
1207 int size,
1208 const char *errors}
1209 Encodes the \ctype{Py_UNICODE} buffer of the given size using
1210 \ASCII{} and returns a Python string object. Returns \NULL{} if an
1211 exception was raised by the codec.
1212\end{cfuncdesc}
1213
1214\begin{cfuncdesc}{PyObject*}{PyUnicode_AsASCIIString}{PyObject *unicode}
1215 Encodes a Unicode objects using \ASCII{} and returns the result as
1216 Python string object. Error handling is ``strict''. Returns
1217 \NULL{} if an exception was raised by the codec.
1218\end{cfuncdesc}
1219
Tim Petersf582b822001-12-11 18:51:08 +00001220% --- Character Map Codecs -----------------------------------------------
Fred Drake3adf79e2001-10-12 19:01:43 +00001221
1222These are the mapping codec APIs:
1223
1224This codec is special in that it can be used to implement many
1225different codecs (and this is in fact what was done to obtain most of
1226the standard codecs included in the \module{encodings} package). The
1227codec uses mapping to encode and decode characters.
1228
1229Decoding mappings must map single string characters to single Unicode
1230characters, integers (which are then interpreted as Unicode ordinals)
Tim Petersf582b822001-12-11 18:51:08 +00001231or None (meaning "undefined mapping" and causing an error).
Fred Drake3adf79e2001-10-12 19:01:43 +00001232
1233Encoding mappings must map single Unicode characters to single string
1234characters, integers (which are then interpreted as Latin-1 ordinals)
1235or None (meaning "undefined mapping" and causing an error).
1236
1237The mapping objects provided must only support the __getitem__ mapping
1238interface.
1239
1240If a character lookup fails with a LookupError, the character is
1241copied as-is meaning that its ordinal value will be interpreted as
1242Unicode or Latin-1 ordinal resp. Because of this, mappings only need
1243to contain those mappings which map characters to different code
1244points.
1245
1246\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeCharmap}{const char *s,
1247 int size,
1248 PyObject *mapping,
1249 const char *errors}
1250 Creates a Unicode object by decoding \var{size} bytes of the encoded
1251 string \var{s} using the given \var{mapping} object. Returns
1252 \NULL{} if an exception was raised by the codec.
1253\end{cfuncdesc}
1254
1255\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeCharmap}{const Py_UNICODE *s,
1256 int size,
1257 PyObject *mapping,
1258 const char *errors}
1259 Encodes the \ctype{Py_UNICODE} buffer of the given size using the
1260 given \var{mapping} object and returns a Python string object.
1261 Returns \NULL{} if an exception was raised by the codec.
1262\end{cfuncdesc}
1263
1264\begin{cfuncdesc}{PyObject*}{PyUnicode_AsCharmapString}{PyObject *unicode,
1265 PyObject *mapping}
1266 Encodes a Unicode objects using the given \var{mapping} object and
1267 returns the result as Python string object. Error handling is
1268 ``strict''. Returns \NULL{} if an exception was raised by the
1269 codec.
1270\end{cfuncdesc}
1271
1272The following codec API is special in that maps Unicode to Unicode.
1273
1274\begin{cfuncdesc}{PyObject*}{PyUnicode_TranslateCharmap}{const Py_UNICODE *s,
1275 int size,
1276 PyObject *table,
1277 const char *errors}
1278 Translates a \ctype{Py_UNICODE} buffer of the given length by
1279 applying a character mapping \var{table} to it and returns the
1280 resulting Unicode object. Returns \NULL{} when an exception was
1281 raised by the codec.
1282
1283 The \var{mapping} table must map Unicode ordinal integers to Unicode
1284 ordinal integers or None (causing deletion of the character).
1285
1286 Mapping tables need only provide the method{__getitem__()}
1287 interface; dictionaries and sequences work well. Unmapped character
1288 ordinals (ones which cause a \exception{LookupError}) are left
1289 untouched and are copied as-is.
1290\end{cfuncdesc}
1291
1292% --- MBCS codecs for Windows --------------------------------------------
1293
1294These are the MBCS codec APIs. They are currently only available on
1295Windows and use the Win32 MBCS converters to implement the
1296conversions. Note that MBCS (or DBCS) is a class of encodings, not
1297just one. The target encoding is defined by the user settings on the
1298machine running the codec.
1299
1300\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeMBCS}{const char *s,
1301 int size,
1302 const char *errors}
1303 Creates a Unicode object by decoding \var{size} bytes of the MBCS
1304 encoded string \var{s}. Returns \NULL{} if an exception was
1305 raised by the codec.
1306\end{cfuncdesc}
1307
1308\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeMBCS}{const Py_UNICODE *s,
1309 int size,
1310 const char *errors}
1311 Encodes the \ctype{Py_UNICODE} buffer of the given size using MBCS
1312 and returns a Python string object. Returns \NULL{} if an exception
1313 was raised by the codec.
1314\end{cfuncdesc}
1315
1316\begin{cfuncdesc}{PyObject*}{PyUnicode_AsMBCSString}{PyObject *unicode}
1317 Encodes a Unicode objects using MBCS and returns the result as
1318 Python string object. Error handling is ``strict''. Returns
1319 \NULL{} if an exception was raised by the codec.
1320\end{cfuncdesc}
1321
1322% --- Methods & Slots ----------------------------------------------------
1323
1324\subsubsection{Methods and Slot Functions \label{unicodeMethodsAndSlots}}
1325
1326The following APIs are capable of handling Unicode objects and strings
1327on input (we refer to them as strings in the descriptions) and return
Martin v. Löwis95cf84a2003-10-19 07:32:24 +00001328Unicode objects or integers as appropriate.
Fred Drake3adf79e2001-10-12 19:01:43 +00001329
1330They all return \NULL{} or \code{-1} if an exception occurs.
1331
1332\begin{cfuncdesc}{PyObject*}{PyUnicode_Concat}{PyObject *left,
1333 PyObject *right}
1334 Concat two strings giving a new Unicode string.
1335\end{cfuncdesc}
1336
1337\begin{cfuncdesc}{PyObject*}{PyUnicode_Split}{PyObject *s,
1338 PyObject *sep,
1339 int maxsplit}
1340 Split a string giving a list of Unicode strings. If sep is \NULL,
1341 splitting will be done at all whitespace substrings. Otherwise,
1342 splits occur at the given separator. At most \var{maxsplit} splits
1343 will be done. If negative, no limit is set. Separators are not
1344 included in the resulting list.
1345\end{cfuncdesc}
1346
1347\begin{cfuncdesc}{PyObject*}{PyUnicode_Splitlines}{PyObject *s,
Martin v. Löwis24b88812003-03-30 16:40:42 +00001348 int keepend}
Fred Drake3adf79e2001-10-12 19:01:43 +00001349 Split a Unicode string at line breaks, returning a list of Unicode
Martin v. Löwis24b88812003-03-30 16:40:42 +00001350 strings. CRLF is considered to be one line break. If \var{keepend}
1351 is 0, the Line break characters are not included in the resulting
1352 strings.
Fred Drake3adf79e2001-10-12 19:01:43 +00001353\end{cfuncdesc}
1354
1355\begin{cfuncdesc}{PyObject*}{PyUnicode_Translate}{PyObject *str,
1356 PyObject *table,
1357 const char *errors}
1358 Translate a string by applying a character mapping table to it and
1359 return the resulting Unicode object.
1360
1361 The mapping table must map Unicode ordinal integers to Unicode
1362 ordinal integers or None (causing deletion of the character).
1363
1364 Mapping tables need only provide the \method{__getitem__()}
1365 interface; dictionaries and sequences work well. Unmapped character
1366 ordinals (ones which cause a \exception{LookupError}) are left
1367 untouched and are copied as-is.
1368
1369 \var{errors} has the usual meaning for codecs. It may be \NULL{}
1370 which indicates to use the default error handling.
1371\end{cfuncdesc}
1372
1373\begin{cfuncdesc}{PyObject*}{PyUnicode_Join}{PyObject *separator,
1374 PyObject *seq}
1375 Join a sequence of strings using the given separator and return the
1376 resulting Unicode string.
1377\end{cfuncdesc}
1378
1379\begin{cfuncdesc}{PyObject*}{PyUnicode_Tailmatch}{PyObject *str,
1380 PyObject *substr,
1381 int start,
1382 int end,
1383 int direction}
1384 Return 1 if \var{substr} matches \var{str}[\var{start}:\var{end}] at
1385 the given tail end (\var{direction} == -1 means to do a prefix
1386 match, \var{direction} == 1 a suffix match), 0 otherwise.
1387\end{cfuncdesc}
1388
Fred Drake1d1e1db2002-06-20 22:07:04 +00001389\begin{cfuncdesc}{int}{PyUnicode_Find}{PyObject *str,
1390 PyObject *substr,
1391 int start,
1392 int end,
1393 int direction}
Fred Drake3adf79e2001-10-12 19:01:43 +00001394 Return the first position of \var{substr} in
1395 \var{str}[\var{start}:\var{end}] using the given \var{direction}
1396 (\var{direction} == 1 means to do a forward search,
Fred Drake1d1e1db2002-06-20 22:07:04 +00001397 \var{direction} == -1 a backward search). The return value is the
1398 index of the first match; a value of \code{-1} indicates that no
1399 match was found, and \code{-2} indicates that an error occurred and
1400 an exception has been set.
Fred Drake3adf79e2001-10-12 19:01:43 +00001401\end{cfuncdesc}
1402
Fred Drake1d1e1db2002-06-20 22:07:04 +00001403\begin{cfuncdesc}{int}{PyUnicode_Count}{PyObject *str,
1404 PyObject *substr,
1405 int start,
1406 int end}
1407 Return the number of non-overlapping occurrences of \var{substr} in
1408 \code{\var{str}[\var{start}:\var{end}]}. Returns \code{-1} if an
1409 error occurred.
Fred Drake3adf79e2001-10-12 19:01:43 +00001410\end{cfuncdesc}
1411
1412\begin{cfuncdesc}{PyObject*}{PyUnicode_Replace}{PyObject *str,
1413 PyObject *substr,
1414 PyObject *replstr,
1415 int maxcount}
1416 Replace at most \var{maxcount} occurrences of \var{substr} in
1417 \var{str} with \var{replstr} and return the resulting Unicode object.
1418 \var{maxcount} == -1 means replace all occurrences.
1419\end{cfuncdesc}
1420
1421\begin{cfuncdesc}{int}{PyUnicode_Compare}{PyObject *left, PyObject *right}
1422 Compare two strings and return -1, 0, 1 for less than, equal, and
1423 greater than, respectively.
1424\end{cfuncdesc}
1425
1426\begin{cfuncdesc}{PyObject*}{PyUnicode_Format}{PyObject *format,
1427 PyObject *args}
1428 Returns a new string object from \var{format} and \var{args}; this
1429 is analogous to \code{\var{format} \%\ \var{args}}. The
1430 \var{args} argument must be a tuple.
1431\end{cfuncdesc}
1432
1433\begin{cfuncdesc}{int}{PyUnicode_Contains}{PyObject *container,
1434 PyObject *element}
1435 Checks whether \var{element} is contained in \var{container} and
1436 returns true or false accordingly.
1437
1438 \var{element} has to coerce to a one element Unicode
1439 string. \code{-1} is returned if there was an error.
1440\end{cfuncdesc}
1441
1442
1443\subsection{Buffer Objects \label{bufferObjects}}
1444\sectionauthor{Greg Stein}{gstein@lyra.org}
1445
1446\obindex{buffer}
1447Python objects implemented in C can export a group of functions called
1448the ``buffer\index{buffer interface} interface.'' These functions can
1449be used by an object to expose its data in a raw, byte-oriented
1450format. Clients of the object can use the buffer interface to access
1451the object data directly, without needing to copy it first.
1452
Tim Petersf582b822001-12-11 18:51:08 +00001453Two examples of objects that support
1454the buffer interface are strings and arrays. The string object exposes
Fred Drake3adf79e2001-10-12 19:01:43 +00001455the character contents in the buffer interface's byte-oriented
1456form. An array can also expose its contents, but it should be noted
1457that array elements may be multi-byte values.
1458
1459An example user of the buffer interface is the file object's
1460\method{write()} method. Any object that can export a series of bytes
1461through the buffer interface can be written to a file. There are a
Tim Petersf582b822001-12-11 18:51:08 +00001462number of format codes to \cfunction{PyArg_ParseTuple()} that operate
Fred Drake3adf79e2001-10-12 19:01:43 +00001463against an object's buffer interface, returning data from the target
1464object.
1465
1466More information on the buffer interface is provided in the section
Fred Drake54e62942001-12-11 19:40:16 +00001467``Buffer Object Structures'' (section~\ref{buffer-structs}), under
Fred Drake3adf79e2001-10-12 19:01:43 +00001468the description for \ctype{PyBufferProcs}\ttindex{PyBufferProcs}.
1469
1470A ``buffer object'' is defined in the \file{bufferobject.h} header
1471(included by \file{Python.h}). These objects look very similar to
1472string objects at the Python programming level: they support slicing,
1473indexing, concatenation, and some other standard string
1474operations. However, their data can come from one of two sources: from
1475a block of memory, or from another object which exports the buffer
1476interface.
1477
1478Buffer objects are useful as a way to expose the data from another
1479object's buffer interface to the Python programmer. They can also be
1480used as a zero-copy slicing mechanism. Using their ability to
1481reference a block of memory, it is possible to expose any data to the
1482Python programmer quite easily. The memory could be a large, constant
1483array in a C extension, it could be a raw block of memory for
1484manipulation before passing to an operating system library, or it
1485could be used to pass around structured data in its native, in-memory
1486format.
1487
1488\begin{ctypedesc}{PyBufferObject}
1489 This subtype of \ctype{PyObject} represents a buffer object.
1490\end{ctypedesc}
1491
1492\begin{cvardesc}{PyTypeObject}{PyBuffer_Type}
1493 The instance of \ctype{PyTypeObject} which represents the Python
1494 buffer type; it is the same object as \code{types.BufferType} in the
1495 Python layer.\withsubitem{(in module types)}{\ttindex{BufferType}}.
1496\end{cvardesc}
1497
1498\begin{cvardesc}{int}{Py_END_OF_BUFFER}
1499 This constant may be passed as the \var{size} parameter to
1500 \cfunction{PyBuffer_FromObject()} or
1501 \cfunction{PyBuffer_FromReadWriteObject()}. It indicates that the
1502 new \ctype{PyBufferObject} should refer to \var{base} object from
1503 the specified \var{offset} to the end of its exported buffer. Using
1504 this enables the caller to avoid querying the \var{base} object for
1505 its length.
1506\end{cvardesc}
1507
1508\begin{cfuncdesc}{int}{PyBuffer_Check}{PyObject *p}
1509 Return true if the argument has type \cdata{PyBuffer_Type}.
1510\end{cfuncdesc}
1511
1512\begin{cfuncdesc}{PyObject*}{PyBuffer_FromObject}{PyObject *base,
1513 int offset, int size}
1514 Return a new read-only buffer object. This raises
1515 \exception{TypeError} if \var{base} doesn't support the read-only
1516 buffer protocol or doesn't provide exactly one buffer segment, or it
1517 raises \exception{ValueError} if \var{offset} is less than zero. The
1518 buffer will hold a reference to the \var{base} object, and the
1519 buffer's contents will refer to the \var{base} object's buffer
1520 interface, starting as position \var{offset} and extending for
1521 \var{size} bytes. If \var{size} is \constant{Py_END_OF_BUFFER}, then
1522 the new buffer's contents extend to the length of the \var{base}
1523 object's exported buffer data.
1524\end{cfuncdesc}
1525
1526\begin{cfuncdesc}{PyObject*}{PyBuffer_FromReadWriteObject}{PyObject *base,
1527 int offset,
1528 int size}
1529 Return a new writable buffer object. Parameters and exceptions are
1530 similar to those for \cfunction{PyBuffer_FromObject()}. If the
1531 \var{base} object does not export the writeable buffer protocol,
1532 then \exception{TypeError} is raised.
1533\end{cfuncdesc}
1534
1535\begin{cfuncdesc}{PyObject*}{PyBuffer_FromMemory}{void *ptr, int size}
1536 Return a new read-only buffer object that reads from a specified
1537 location in memory, with a specified size. The caller is
1538 responsible for ensuring that the memory buffer, passed in as
1539 \var{ptr}, is not deallocated while the returned buffer object
1540 exists. Raises \exception{ValueError} if \var{size} is less than
1541 zero. Note that \constant{Py_END_OF_BUFFER} may \emph{not} be
1542 passed for the \var{size} parameter; \exception{ValueError} will be
1543 raised in that case.
1544\end{cfuncdesc}
1545
1546\begin{cfuncdesc}{PyObject*}{PyBuffer_FromReadWriteMemory}{void *ptr, int size}
1547 Similar to \cfunction{PyBuffer_FromMemory()}, but the returned
1548 buffer is writable.
1549\end{cfuncdesc}
1550
1551\begin{cfuncdesc}{PyObject*}{PyBuffer_New}{int size}
1552 Returns a new writable buffer object that maintains its own memory
1553 buffer of \var{size} bytes. \exception{ValueError} is returned if
1554 \var{size} is not zero or positive.
1555\end{cfuncdesc}
1556
1557
1558\subsection{Tuple Objects \label{tupleObjects}}
1559
1560\obindex{tuple}
1561\begin{ctypedesc}{PyTupleObject}
1562 This subtype of \ctype{PyObject} represents a Python tuple object.
1563\end{ctypedesc}
1564
1565\begin{cvardesc}{PyTypeObject}{PyTuple_Type}
1566 This instance of \ctype{PyTypeObject} represents the Python tuple
1567 type; it is the same object as \code{types.TupleType} in the Python
1568 layer.\withsubitem{(in module types)}{\ttindex{TupleType}}.
1569\end{cvardesc}
1570
1571\begin{cfuncdesc}{int}{PyTuple_Check}{PyObject *p}
1572 Return true if \var{p} is a tuple object or an instance of a subtype
1573 of the tuple type.
1574 \versionchanged[Allowed subtypes to be accepted]{2.2}
1575\end{cfuncdesc}
1576
1577\begin{cfuncdesc}{int}{PyTuple_CheckExact}{PyObject *p}
1578 Return true if \var{p} is a tuple object, but not an instance of a
1579 subtype of the tuple type.
1580 \versionadded{2.2}
1581\end{cfuncdesc}
1582
1583\begin{cfuncdesc}{PyObject*}{PyTuple_New}{int len}
1584 Return a new tuple object of size \var{len}, or \NULL{} on failure.
1585\end{cfuncdesc}
1586
Raymond Hettingercb2da432003-10-12 18:24:34 +00001587\begin{cfuncdesc}{PyObject*}{PyTuple_Pack}{int n, \moreargs}
1588 Return a new tuple object of size \var{n}, or \NULL{} on failure.
1589 The tuple values are initialized to the subsequent \var{n} C arguments
1590 pointing to Python objects. \samp{PyTuple_Pack(2, \var{a}, \var{b})}
1591 is equivalent to \samp{Py_BuildValue("(OO)", \var{a}, \var{b})}.
1592 \versionadded{2.4}
1593\end{cfuncdesc}
1594
Fred Drake3adf79e2001-10-12 19:01:43 +00001595\begin{cfuncdesc}{int}{PyTuple_Size}{PyObject *p}
1596 Takes a pointer to a tuple object, and returns the size of that
1597 tuple.
1598\end{cfuncdesc}
1599
1600\begin{cfuncdesc}{int}{PyTuple_GET_SIZE}{PyObject *p}
1601 Return the size of the tuple \var{p}, which must be non-\NULL{} and
1602 point to a tuple; no error checking is performed.
1603\end{cfuncdesc}
1604
1605\begin{cfuncdesc}{PyObject*}{PyTuple_GetItem}{PyObject *p, int pos}
1606 Returns the object at position \var{pos} in the tuple pointed to by
1607 \var{p}. If \var{pos} is out of bounds, returns \NULL{} and sets an
1608 \exception{IndexError} exception.
1609\end{cfuncdesc}
1610
1611\begin{cfuncdesc}{PyObject*}{PyTuple_GET_ITEM}{PyObject *p, int pos}
1612 Like \cfunction{PyTuple_GetItem()}, but does no checking of its
1613 arguments.
1614\end{cfuncdesc}
1615
1616\begin{cfuncdesc}{PyObject*}{PyTuple_GetSlice}{PyObject *p,
1617 int low, int high}
1618 Takes a slice of the tuple pointed to by \var{p} from \var{low} to
1619 \var{high} and returns it as a new tuple.
1620\end{cfuncdesc}
1621
1622\begin{cfuncdesc}{int}{PyTuple_SetItem}{PyObject *p,
1623 int pos, PyObject *o}
1624 Inserts a reference to object \var{o} at position \var{pos} of the
1625 tuple pointed to by \var{p}. It returns \code{0} on success.
1626 \note{This function ``steals'' a reference to \var{o}.}
1627\end{cfuncdesc}
1628
1629\begin{cfuncdesc}{void}{PyTuple_SET_ITEM}{PyObject *p,
1630 int pos, PyObject *o}
1631 Like \cfunction{PyTuple_SetItem()}, but does no error checking, and
1632 should \emph{only} be used to fill in brand new tuples. \note{This
1633 function ``steals'' a reference to \var{o}.}
1634\end{cfuncdesc}
1635
1636\begin{cfuncdesc}{int}{_PyTuple_Resize}{PyObject **p, int newsize}
1637 Can be used to resize a tuple. \var{newsize} will be the new length
1638 of the tuple. Because tuples are \emph{supposed} to be immutable,
1639 this should only be used if there is only one reference to the
1640 object. Do \emph{not} use this if the tuple may already be known to
1641 some other part of the code. The tuple will always grow or shrink
1642 at the end. Think of this as destroying the old tuple and creating
1643 a new one, only more efficiently. Returns \code{0} on success.
1644 Client code should never assume that the resulting value of
1645 \code{*\var{p}} will be the same as before calling this function.
1646 If the object referenced by \code{*\var{p}} is replaced, the
1647 original \code{*\var{p}} is destroyed. On failure, returns
1648 \code{-1} and sets \code{*\var{p}} to \NULL, and raises
1649 \exception{MemoryError} or
1650 \exception{SystemError}.
Tim Petersf582b822001-12-11 18:51:08 +00001651 \versionchanged[Removed unused third parameter, \var{last_is_sticky}]{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +00001652\end{cfuncdesc}
1653
1654
1655\subsection{List Objects \label{listObjects}}
1656
1657\obindex{list}
1658\begin{ctypedesc}{PyListObject}
1659 This subtype of \ctype{PyObject} represents a Python list object.
1660\end{ctypedesc}
1661
1662\begin{cvardesc}{PyTypeObject}{PyList_Type}
1663 This instance of \ctype{PyTypeObject} represents the Python list
1664 type. This is the same object as \code{types.ListType}.
1665 \withsubitem{(in module types)}{\ttindex{ListType}}
1666\end{cvardesc}
1667
1668\begin{cfuncdesc}{int}{PyList_Check}{PyObject *p}
1669 Returns true if its argument is a \ctype{PyListObject}.
1670\end{cfuncdesc}
1671
1672\begin{cfuncdesc}{PyObject*}{PyList_New}{int len}
1673 Returns a new list of length \var{len} on success, or \NULL{} on
1674 failure.
1675\end{cfuncdesc}
1676
1677\begin{cfuncdesc}{int}{PyList_Size}{PyObject *list}
1678 Returns the length of the list object in \var{list}; this is
1679 equivalent to \samp{len(\var{list})} on a list object.
1680 \bifuncindex{len}
1681\end{cfuncdesc}
1682
1683\begin{cfuncdesc}{int}{PyList_GET_SIZE}{PyObject *list}
1684 Macro form of \cfunction{PyList_Size()} without error checking.
1685\end{cfuncdesc}
1686
1687\begin{cfuncdesc}{PyObject*}{PyList_GetItem}{PyObject *list, int index}
1688 Returns the object at position \var{pos} in the list pointed to by
1689 \var{p}. If \var{pos} is out of bounds, returns \NULL{} and sets an
1690 \exception{IndexError} exception.
1691\end{cfuncdesc}
1692
1693\begin{cfuncdesc}{PyObject*}{PyList_GET_ITEM}{PyObject *list, int i}
1694 Macro form of \cfunction{PyList_GetItem()} without error checking.
1695\end{cfuncdesc}
1696
1697\begin{cfuncdesc}{int}{PyList_SetItem}{PyObject *list, int index,
1698 PyObject *item}
1699 Sets the item at index \var{index} in list to \var{item}. Returns
1700 \code{0} on success or \code{-1} on failure. \note{This function
1701 ``steals'' a reference to \var{item} and discards a reference to an
1702 item already in the list at the affected position.}
1703\end{cfuncdesc}
1704
1705\begin{cfuncdesc}{void}{PyList_SET_ITEM}{PyObject *list, int i,
1706 PyObject *o}
1707 Macro form of \cfunction{PyList_SetItem()} without error checking.
1708 This is normally only used to fill in new lists where there is no
1709 previous content.
1710 \note{This function ``steals'' a reference to \var{item}, and,
1711 unlike \cfunction{PyList_SetItem()}, does \emph{not} discard a
1712 reference to any item that it being replaced; any reference in
1713 \var{list} at position \var{i} will be leaked.}
1714\end{cfuncdesc}
1715
1716\begin{cfuncdesc}{int}{PyList_Insert}{PyObject *list, int index,
1717 PyObject *item}
1718 Inserts the item \var{item} into list \var{list} in front of index
1719 \var{index}. Returns \code{0} if successful; returns \code{-1} and
1720 raises an exception if unsuccessful. Analogous to
1721 \code{\var{list}.insert(\var{index}, \var{item})}.
1722\end{cfuncdesc}
1723
1724\begin{cfuncdesc}{int}{PyList_Append}{PyObject *list, PyObject *item}
1725 Appends the object \var{item} at the end of list \var{list}.
1726 Returns \code{0} if successful; returns \code{-1} and sets an
1727 exception if unsuccessful. Analogous to
1728 \code{\var{list}.append(\var{item})}.
1729\end{cfuncdesc}
1730
1731\begin{cfuncdesc}{PyObject*}{PyList_GetSlice}{PyObject *list,
1732 int low, int high}
1733 Returns a list of the objects in \var{list} containing the objects
1734 \emph{between} \var{low} and \var{high}. Returns \NULL{} and sets
1735 an exception if unsuccessful.
1736 Analogous to \code{\var{list}[\var{low}:\var{high}]}.
1737\end{cfuncdesc}
1738
1739\begin{cfuncdesc}{int}{PyList_SetSlice}{PyObject *list,
1740 int low, int high,
1741 PyObject *itemlist}
1742 Sets the slice of \var{list} between \var{low} and \var{high} to the
1743 contents of \var{itemlist}. Analogous to
Raymond Hettinger9c7ed4c2003-10-26 17:20:07 +00001744 \code{\var{list}[\var{low}:\var{high}] = \var{itemlist}}.
1745 The \var{itemlist} may be \NULL{}, indicating the assignment
1746 of an empty list (slice deletion).
1747 Returns \code{0} on success, \code{-1} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00001748\end{cfuncdesc}
1749
1750\begin{cfuncdesc}{int}{PyList_Sort}{PyObject *list}
1751 Sorts the items of \var{list} in place. Returns \code{0} on
1752 success, \code{-1} on failure. This is equivalent to
1753 \samp{\var{list}.sort()}.
1754\end{cfuncdesc}
1755
1756\begin{cfuncdesc}{int}{PyList_Reverse}{PyObject *list}
1757 Reverses the items of \var{list} in place. Returns \code{0} on
1758 success, \code{-1} on failure. This is the equivalent of
1759 \samp{\var{list}.reverse()}.
1760\end{cfuncdesc}
1761
1762\begin{cfuncdesc}{PyObject*}{PyList_AsTuple}{PyObject *list}
1763 Returns a new tuple object containing the contents of \var{list};
1764 equivalent to \samp{tuple(\var{list})}.\bifuncindex{tuple}
1765\end{cfuncdesc}
1766
1767
1768\section{Mapping Objects \label{mapObjects}}
1769
1770\obindex{mapping}
1771
1772
1773\subsection{Dictionary Objects \label{dictObjects}}
1774
1775\obindex{dictionary}
1776\begin{ctypedesc}{PyDictObject}
1777 This subtype of \ctype{PyObject} represents a Python dictionary
1778 object.
1779\end{ctypedesc}
1780
1781\begin{cvardesc}{PyTypeObject}{PyDict_Type}
1782 This instance of \ctype{PyTypeObject} represents the Python
1783 dictionary type. This is exposed to Python programs as
1784 \code{types.DictType} and \code{types.DictionaryType}.
1785 \withsubitem{(in module types)}{\ttindex{DictType}\ttindex{DictionaryType}}
1786\end{cvardesc}
1787
1788\begin{cfuncdesc}{int}{PyDict_Check}{PyObject *p}
1789 Returns true if its argument is a \ctype{PyDictObject}.
1790\end{cfuncdesc}
1791
1792\begin{cfuncdesc}{PyObject*}{PyDict_New}{}
1793 Returns a new empty dictionary, or \NULL{} on failure.
1794\end{cfuncdesc}
1795
1796\begin{cfuncdesc}{PyObject*}{PyDictProxy_New}{PyObject *dict}
1797 Return a proxy object for a mapping which enforces read-only
1798 behavior. This is normally used to create a proxy to prevent
1799 modification of the dictionary for non-dynamic class types.
1800 \versionadded{2.2}
1801\end{cfuncdesc}
1802
1803\begin{cfuncdesc}{void}{PyDict_Clear}{PyObject *p}
1804 Empties an existing dictionary of all key-value pairs.
1805\end{cfuncdesc}
1806
1807\begin{cfuncdesc}{PyObject*}{PyDict_Copy}{PyObject *p}
1808 Returns a new dictionary that contains the same key-value pairs as
1809 \var{p}.
1810 \versionadded{1.6}
1811\end{cfuncdesc}
1812
1813\begin{cfuncdesc}{int}{PyDict_SetItem}{PyObject *p, PyObject *key,
1814 PyObject *val}
1815 Inserts \var{value} into the dictionary \var{p} with a key of
1816 \var{key}. \var{key} must be hashable; if it isn't,
1817 \exception{TypeError} will be raised.
1818 Returns \code{0} on success or \code{-1} on failure.
1819\end{cfuncdesc}
1820
1821\begin{cfuncdesc}{int}{PyDict_SetItemString}{PyObject *p,
1822 char *key,
1823 PyObject *val}
1824 Inserts \var{value} into the dictionary \var{p} using \var{key} as a
1825 key. \var{key} should be a \ctype{char*}. The key object is created
1826 using \code{PyString_FromString(\var{key})}. Returns \code{0} on
1827 success or \code{-1} on failure.
1828 \ttindex{PyString_FromString()}
1829\end{cfuncdesc}
1830
1831\begin{cfuncdesc}{int}{PyDict_DelItem}{PyObject *p, PyObject *key}
1832 Removes the entry in dictionary \var{p} with key \var{key}.
1833 \var{key} must be hashable; if it isn't, \exception{TypeError} is
Skip Montanaroa23bc422002-01-23 08:18:30 +00001834 raised. Returns \code{0} on success or \code{-1} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00001835\end{cfuncdesc}
1836
1837\begin{cfuncdesc}{int}{PyDict_DelItemString}{PyObject *p, char *key}
1838 Removes the entry in dictionary \var{p} which has a key specified by
1839 the string \var{key}. Returns \code{0} on success or \code{-1} on
1840 failure.
1841\end{cfuncdesc}
1842
1843\begin{cfuncdesc}{PyObject*}{PyDict_GetItem}{PyObject *p, PyObject *key}
1844 Returns the object from dictionary \var{p} which has a key
1845 \var{key}. Returns \NULL{} if the key \var{key} is not present, but
1846 \emph{without} setting an exception.
1847\end{cfuncdesc}
1848
1849\begin{cfuncdesc}{PyObject*}{PyDict_GetItemString}{PyObject *p, char *key}
1850 This is the same as \cfunction{PyDict_GetItem()}, but \var{key} is
1851 specified as a \ctype{char*}, rather than a \ctype{PyObject*}.
1852\end{cfuncdesc}
1853
1854\begin{cfuncdesc}{PyObject*}{PyDict_Items}{PyObject *p}
1855 Returns a \ctype{PyListObject} containing all the items from the
1856 dictionary, as in the dictinoary method \method{items()} (see the
1857 \citetitle[../lib/lib.html]{Python Library Reference}).
1858\end{cfuncdesc}
1859
1860\begin{cfuncdesc}{PyObject*}{PyDict_Keys}{PyObject *p}
1861 Returns a \ctype{PyListObject} containing all the keys from the
1862 dictionary, as in the dictionary method \method{keys()} (see the
1863 \citetitle[../lib/lib.html]{Python Library Reference}).
1864\end{cfuncdesc}
1865
1866\begin{cfuncdesc}{PyObject*}{PyDict_Values}{PyObject *p}
1867 Returns a \ctype{PyListObject} containing all the values from the
1868 dictionary \var{p}, as in the dictionary method \method{values()}
1869 (see the \citetitle[../lib/lib.html]{Python Library Reference}).
1870\end{cfuncdesc}
1871
1872\begin{cfuncdesc}{int}{PyDict_Size}{PyObject *p}
1873 Returns the number of items in the dictionary. This is equivalent
1874 to \samp{len(\var{p})} on a dictionary.\bifuncindex{len}
1875\end{cfuncdesc}
1876
1877\begin{cfuncdesc}{int}{PyDict_Next}{PyObject *p, int *ppos,
1878 PyObject **pkey, PyObject **pvalue}
1879 Iterate over all key-value pairs in the dictionary \var{p}. The
1880 \ctype{int} referred to by \var{ppos} must be initialized to
1881 \code{0} prior to the first call to this function to start the
1882 iteration; the function returns true for each pair in the
1883 dictionary, and false once all pairs have been reported. The
1884 parameters \var{pkey} and \var{pvalue} should either point to
1885 \ctype{PyObject*} variables that will be filled in with each key and
Skip Montanaroea3ceaa2002-01-23 10:54:41 +00001886 value, respectively, or may be \NULL. Any references returned through
1887 them are borrowed.
Fred Drake3adf79e2001-10-12 19:01:43 +00001888
1889 For example:
1890
1891\begin{verbatim}
1892PyObject *key, *value;
1893int pos = 0;
1894
1895while (PyDict_Next(self->dict, &pos, &key, &value)) {
1896 /* do something interesting with the values... */
1897 ...
1898}
1899\end{verbatim}
1900
1901 The dictionary \var{p} should not be mutated during iteration. It
1902 is safe (since Python 2.1) to modify the values of the keys as you
1903 iterate over the dictionary, but only so long as the set of keys
1904 does not change. For example:
1905
1906\begin{verbatim}
1907PyObject *key, *value;
1908int pos = 0;
1909
1910while (PyDict_Next(self->dict, &pos, &key, &value)) {
1911 int i = PyInt_AS_LONG(value) + 1;
1912 PyObject *o = PyInt_FromLong(i);
1913 if (o == NULL)
1914 return -1;
1915 if (PyDict_SetItem(self->dict, key, o) < 0) {
1916 Py_DECREF(o);
1917 return -1;
1918 }
1919 Py_DECREF(o);
1920}
1921\end{verbatim}
1922\end{cfuncdesc}
1923
1924\begin{cfuncdesc}{int}{PyDict_Merge}{PyObject *a, PyObject *b, int override}
Tim Petersf582b822001-12-11 18:51:08 +00001925 Iterate over mapping object \var{b} adding key-value pairs to dictionary
1926 \var{a}.
1927 \var{b} may be a dictionary, or any object supporting
1928 \function{PyMapping_Keys()} and \function{PyObject_GetItem()}.
1929 If \var{override} is true, existing pairs in \var{a} will
Fred Drake3adf79e2001-10-12 19:01:43 +00001930 be replaced if a matching key is found in \var{b}, otherwise pairs
1931 will only be added if there is not a matching key in \var{a}.
Tim Petersf582b822001-12-11 18:51:08 +00001932 Return \code{0} on success or \code{-1} if an exception was
Fred Drake3adf79e2001-10-12 19:01:43 +00001933 raised.
1934\versionadded{2.2}
1935\end{cfuncdesc}
1936
1937\begin{cfuncdesc}{int}{PyDict_Update}{PyObject *a, PyObject *b}
1938 This is the same as \code{PyDict_Merge(\var{a}, \var{b}, 1)} in C,
Tim Petersf582b822001-12-11 18:51:08 +00001939 or \code{\var{a}.update(\var{b})} in Python. Return \code{0} on
Fred Drake3adf79e2001-10-12 19:01:43 +00001940 success or \code{-1} if an exception was raised.
1941 \versionadded{2.2}
1942\end{cfuncdesc}
1943
Tim Petersf582b822001-12-11 18:51:08 +00001944\begin{cfuncdesc}{int}{PyDict_MergeFromSeq2}{PyObject *a, PyObject *seq2,
1945 int override}
1946 Update or merge into dictionary \var{a}, from the key-value pairs in
1947 \var{seq2}. \var{seq2} must be an iterable object producing
1948 iterable objects of length 2, viewed as key-value pairs. In case of
1949 duplicate keys, the last wins if \var{override} is true, else the
1950 first wins.
1951 Return \code{0} on success or \code{-1} if an exception
1952 was raised.
1953 Equivalent Python (except for the return value):
1954
1955\begin{verbatim}
1956def PyDict_MergeFromSeq2(a, seq2, override):
1957 for key, value in seq2:
1958 if override or key not in a:
1959 a[key] = value
1960\end{verbatim}
1961
1962 \versionadded{2.2}
1963\end{cfuncdesc}
Fred Drake3adf79e2001-10-12 19:01:43 +00001964
Fred Drake54e62942001-12-11 19:40:16 +00001965
Fred Drake3adf79e2001-10-12 19:01:43 +00001966\section{Other Objects \label{otherObjects}}
1967
1968\subsection{File Objects \label{fileObjects}}
1969
1970\obindex{file}
1971Python's built-in file objects are implemented entirely on the
1972\ctype{FILE*} support from the C standard library. This is an
1973implementation detail and may change in future releases of Python.
1974
1975\begin{ctypedesc}{PyFileObject}
1976 This subtype of \ctype{PyObject} represents a Python file object.
1977\end{ctypedesc}
1978
1979\begin{cvardesc}{PyTypeObject}{PyFile_Type}
1980 This instance of \ctype{PyTypeObject} represents the Python file
1981 type. This is exposed to Python programs as \code{types.FileType}.
1982 \withsubitem{(in module types)}{\ttindex{FileType}}
1983\end{cvardesc}
1984
1985\begin{cfuncdesc}{int}{PyFile_Check}{PyObject *p}
1986 Returns true if its argument is a \ctype{PyFileObject} or a subtype
1987 of \ctype{PyFileObject}.
1988 \versionchanged[Allowed subtypes to be accepted]{2.2}
1989\end{cfuncdesc}
1990
1991\begin{cfuncdesc}{int}{PyFile_CheckExact}{PyObject *p}
1992 Returns true if its argument is a \ctype{PyFileObject}, but not a
1993 subtype of \ctype{PyFileObject}.
1994 \versionadded{2.2}
1995\end{cfuncdesc}
1996
1997\begin{cfuncdesc}{PyObject*}{PyFile_FromString}{char *filename, char *mode}
1998 On success, returns a new file object that is opened on the file
1999 given by \var{filename}, with a file mode given by \var{mode}, where
2000 \var{mode} has the same semantics as the standard C routine
2001 \cfunction{fopen()}\ttindex{fopen()}. On failure, returns \NULL.
2002\end{cfuncdesc}
2003
2004\begin{cfuncdesc}{PyObject*}{PyFile_FromFile}{FILE *fp,
2005 char *name, char *mode,
2006 int (*close)(FILE*)}
2007 Creates a new \ctype{PyFileObject} from the already-open standard C
2008 file pointer, \var{fp}. The function \var{close} will be called
2009 when the file should be closed. Returns \NULL{} on failure.
2010\end{cfuncdesc}
2011
2012\begin{cfuncdesc}{FILE*}{PyFile_AsFile}{PyFileObject *p}
2013 Returns the file object associated with \var{p} as a \ctype{FILE*}.
2014\end{cfuncdesc}
2015
2016\begin{cfuncdesc}{PyObject*}{PyFile_GetLine}{PyObject *p, int n}
2017 Equivalent to \code{\var{p}.readline(\optional{\var{n}})}, this
2018 function reads one line from the object \var{p}. \var{p} may be a
2019 file object or any object with a \method{readline()} method. If
2020 \var{n} is \code{0}, exactly one line is read, regardless of the
2021 length of the line. If \var{n} is greater than \code{0}, no more
2022 than \var{n} bytes will be read from the file; a partial line can be
2023 returned. In both cases, an empty string is returned if the end of
2024 the file is reached immediately. If \var{n} is less than \code{0},
2025 however, one line is read regardless of length, but
2026 \exception{EOFError} is raised if the end of the file is reached
2027 immediately.
2028 \withsubitem{(built-in exception)}{\ttindex{EOFError}}
2029\end{cfuncdesc}
2030
2031\begin{cfuncdesc}{PyObject*}{PyFile_Name}{PyObject *p}
2032 Returns the name of the file specified by \var{p} as a string
2033 object.
2034\end{cfuncdesc}
2035
2036\begin{cfuncdesc}{void}{PyFile_SetBufSize}{PyFileObject *p, int n}
2037 Available on systems with \cfunction{setvbuf()}\ttindex{setvbuf()}
2038 only. This should only be called immediately after file object
2039 creation.
2040\end{cfuncdesc}
2041
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00002042\begin{cfuncdesc}{int}{PyFile_Encoding}{PyFileObject *p, char *enc}
2043 Set the file's encoding for Unicode output to \var{enc}. Return
2044 1 on success and 0 on failure.
2045 \versionadded{2.3}
2046\end{cfuncdesc}
2047
Fred Drake3adf79e2001-10-12 19:01:43 +00002048\begin{cfuncdesc}{int}{PyFile_SoftSpace}{PyObject *p, int newflag}
2049 This function exists for internal use by the interpreter. Sets the
2050 \member{softspace} attribute of \var{p} to \var{newflag} and
2051 \withsubitem{(file attribute)}{\ttindex{softspace}}returns the
2052 previous value. \var{p} does not have to be a file object for this
2053 function to work properly; any object is supported (thought its only
2054 interesting if the \member{softspace} attribute can be set). This
2055 function clears any errors, and will return \code{0} as the previous
2056 value if the attribute either does not exist or if there were errors
2057 in retrieving it. There is no way to detect errors from this
2058 function, but doing so should not be needed.
2059\end{cfuncdesc}
2060
2061\begin{cfuncdesc}{int}{PyFile_WriteObject}{PyObject *obj, PyFileObject *p,
2062 int flags}
2063 Writes object \var{obj} to file object \var{p}. The only supported
2064 flag for \var{flags} is
2065 \constant{Py_PRINT_RAW}\ttindex{Py_PRINT_RAW}; if given, the
2066 \function{str()} of the object is written instead of the
2067 \function{repr()}. Returns \code{0} on success or \code{-1} on
2068 failure; the appropriate exception will be set.
2069\end{cfuncdesc}
2070
Fred Drake454af892001-11-29 22:42:59 +00002071\begin{cfuncdesc}{int}{PyFile_WriteString}{const char *s, PyFileObject *p}
Fred Drake3adf79e2001-10-12 19:01:43 +00002072 Writes string \var{s} to file object \var{p}. Returns \code{0} on
2073 success or \code{-1} on failure; the appropriate exception will be
2074 set.
2075\end{cfuncdesc}
2076
2077
2078\subsection{Instance Objects \label{instanceObjects}}
2079
2080\obindex{instance}
2081There are very few functions specific to instance objects.
2082
2083\begin{cvardesc}{PyTypeObject}{PyInstance_Type}
2084 Type object for class instances.
2085\end{cvardesc}
2086
2087\begin{cfuncdesc}{int}{PyInstance_Check}{PyObject *obj}
2088 Returns true if \var{obj} is an instance.
2089\end{cfuncdesc}
2090
2091\begin{cfuncdesc}{PyObject*}{PyInstance_New}{PyObject *class,
2092 PyObject *arg,
2093 PyObject *kw}
2094 Create a new instance of a specific class. The parameters \var{arg}
2095 and \var{kw} are used as the positional and keyword parameters to
2096 the object's constructor.
2097\end{cfuncdesc}
2098
2099\begin{cfuncdesc}{PyObject*}{PyInstance_NewRaw}{PyObject *class,
2100 PyObject *dict}
2101 Create a new instance of a specific class without calling it's
2102 constructor. \var{class} is the class of new object. The
2103 \var{dict} parameter will be used as the object's \member{__dict__};
2104 if \NULL, a new dictionary will be created for the instance.
2105\end{cfuncdesc}
2106
2107
2108\subsection{Method Objects \label{method-objects}}
2109
2110\obindex{method}
2111There are some useful functions that are useful for working with
2112method objects.
2113
2114\begin{cvardesc}{PyTypeObject}{PyMethod_Type}
2115 This instance of \ctype{PyTypeObject} represents the Python method
2116 type. This is exposed to Python programs as \code{types.MethodType}.
2117 \withsubitem{(in module types)}{\ttindex{MethodType}}
2118\end{cvardesc}
2119
2120\begin{cfuncdesc}{int}{PyMethod_Check}{PyObject *o}
2121 Return true if \var{o} is a method object (has type
2122 \cdata{PyMethod_Type}). The parameter must not be \NULL.
2123\end{cfuncdesc}
2124
2125\begin{cfuncdesc}{PyObject*}{PyMethod_New}{PyObject *func.
2126 PyObject *self, PyObject *class}
2127 Return a new method object, with \var{func} being any callable
2128 object; this is the function that will be called when the method is
2129 called. If this method should be bound to an instance, \var{self}
2130 should be the instance and \var{class} should be the class of
2131 \var{self}, otherwise \var{self} should be \NULL{} and \var{class}
2132 should be the class which provides the unbound method..
2133\end{cfuncdesc}
2134
2135\begin{cfuncdesc}{PyObject*}{PyMethod_Class}{PyObject *meth}
2136 Return the class object from which the method \var{meth} was
2137 created; if this was created from an instance, it will be the class
2138 of the instance.
2139\end{cfuncdesc}
2140
2141\begin{cfuncdesc}{PyObject*}{PyMethod_GET_CLASS}{PyObject *meth}
2142 Macro version of \cfunction{PyMethod_Class()} which avoids error
2143 checking.
2144\end{cfuncdesc}
2145
2146\begin{cfuncdesc}{PyObject*}{PyMethod_Function}{PyObject *meth}
2147 Return the function object associated with the method \var{meth}.
2148\end{cfuncdesc}
2149
2150\begin{cfuncdesc}{PyObject*}{PyMethod_GET_FUNCTION}{PyObject *meth}
2151 Macro version of \cfunction{PyMethod_Function()} which avoids error
2152 checking.
2153\end{cfuncdesc}
2154
2155\begin{cfuncdesc}{PyObject*}{PyMethod_Self}{PyObject *meth}
2156 Return the instance associated with the method \var{meth} if it is
2157 bound, otherwise return \NULL.
2158\end{cfuncdesc}
2159
2160\begin{cfuncdesc}{PyObject*}{PyMethod_GET_SELF}{PyObject *meth}
2161 Macro version of \cfunction{PyMethod_Self()} which avoids error
2162 checking.
2163\end{cfuncdesc}
2164
2165
2166\subsection{Module Objects \label{moduleObjects}}
2167
2168\obindex{module}
2169There are only a few functions special to module objects.
2170
2171\begin{cvardesc}{PyTypeObject}{PyModule_Type}
2172 This instance of \ctype{PyTypeObject} represents the Python module
2173 type. This is exposed to Python programs as
2174 \code{types.ModuleType}.
2175 \withsubitem{(in module types)}{\ttindex{ModuleType}}
2176\end{cvardesc}
2177
2178\begin{cfuncdesc}{int}{PyModule_Check}{PyObject *p}
2179 Returns true if \var{p} is a module object, or a subtype of a module
2180 object.
2181 \versionchanged[Allowed subtypes to be accepted]{2.2}
2182\end{cfuncdesc}
2183
2184\begin{cfuncdesc}{int}{PyModule_CheckExact}{PyObject *p}
2185 Returns true if \var{p} is a module object, but not a subtype of
2186 \cdata{PyModule_Type}.
2187 \versionadded{2.2}
2188\end{cfuncdesc}
2189
2190\begin{cfuncdesc}{PyObject*}{PyModule_New}{char *name}
2191 Return a new module object with the \member{__name__} attribute set
2192 to \var{name}. Only the module's \member{__doc__} and
2193 \member{__name__} attributes are filled in; the caller is
2194 responsible for providing a \member{__file__} attribute.
2195 \withsubitem{(module attribute)}{
2196 \ttindex{__name__}\ttindex{__doc__}\ttindex{__file__}}
2197\end{cfuncdesc}
2198
2199\begin{cfuncdesc}{PyObject*}{PyModule_GetDict}{PyObject *module}
2200 Return the dictionary object that implements \var{module}'s
2201 namespace; this object is the same as the \member{__dict__}
2202 attribute of the module object. This function never fails.
2203 \withsubitem{(module attribute)}{\ttindex{__dict__}}
Fred Drakef495ef72002-04-12 19:32:07 +00002204 It is recommended extensions use other \cfunction{PyModule_*()}
2205 and \cfunction{PyObject_*()} functions rather than directly
2206 manipulate a module's \member{__dict__}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002207\end{cfuncdesc}
2208
2209\begin{cfuncdesc}{char*}{PyModule_GetName}{PyObject *module}
2210 Return \var{module}'s \member{__name__} value. If the module does
2211 not provide one, or if it is not a string, \exception{SystemError}
2212 is raised and \NULL{} is returned.
2213 \withsubitem{(module attribute)}{\ttindex{__name__}}
2214 \withsubitem{(built-in exception)}{\ttindex{SystemError}}
2215\end{cfuncdesc}
2216
2217\begin{cfuncdesc}{char*}{PyModule_GetFilename}{PyObject *module}
2218 Return the name of the file from which \var{module} was loaded using
2219 \var{module}'s \member{__file__} attribute. If this is not defined,
2220 or if it is not a string, raise \exception{SystemError} and return
2221 \NULL.
2222 \withsubitem{(module attribute)}{\ttindex{__file__}}
2223 \withsubitem{(built-in exception)}{\ttindex{SystemError}}
2224\end{cfuncdesc}
2225
2226\begin{cfuncdesc}{int}{PyModule_AddObject}{PyObject *module,
2227 char *name, PyObject *value}
2228 Add an object to \var{module} as \var{name}. This is a convenience
2229 function which can be used from the module's initialization
2230 function. This steals a reference to \var{value}. Returns
2231 \code{-1} on error, \code{0} on success.
2232 \versionadded{2.0}
2233\end{cfuncdesc}
2234
2235\begin{cfuncdesc}{int}{PyModule_AddIntConstant}{PyObject *module,
2236 char *name, int value}
2237 Add an integer constant to \var{module} as \var{name}. This
2238 convenience function can be used from the module's initialization
2239 function. Returns \code{-1} on error, \code{0} on success.
2240 \versionadded{2.0}
2241\end{cfuncdesc}
2242
2243\begin{cfuncdesc}{int}{PyModule_AddStringConstant}{PyObject *module,
2244 char *name, char *value}
2245 Add a string constant to \var{module} as \var{name}. This
2246 convenience function can be used from the module's initialization
2247 function. The string \var{value} must be null-terminated. Returns
2248 \code{-1} on error, \code{0} on success.
2249 \versionadded{2.0}
2250\end{cfuncdesc}
2251
2252
2253\subsection{Iterator Objects \label{iterator-objects}}
2254
2255Python provides two general-purpose iterator objects. The first, a
2256sequence iterator, works with an arbitrary sequence supporting the
2257\method{__getitem__()} method. The second works with a callable
2258object and a sentinel value, calling the callable for each item in the
2259sequence, and ending the iteration when the sentinel value is
2260returned.
2261
2262\begin{cvardesc}{PyTypeObject}{PySeqIter_Type}
2263 Type object for iterator objects returned by
2264 \cfunction{PySeqIter_New()} and the one-argument form of the
2265 \function{iter()} built-in function for built-in sequence types.
2266 \versionadded{2.2}
2267\end{cvardesc}
2268
2269\begin{cfuncdesc}{int}{PySeqIter_Check}{op}
2270 Return true if the type of \var{op} is \cdata{PySeqIter_Type}.
2271 \versionadded{2.2}
2272\end{cfuncdesc}
2273
2274\begin{cfuncdesc}{PyObject*}{PySeqIter_New}{PyObject *seq}
2275 Return an iterator that works with a general sequence object,
2276 \var{seq}. The iteration ends when the sequence raises
2277 \exception{IndexError} for the subscripting operation.
2278 \versionadded{2.2}
2279\end{cfuncdesc}
2280
2281\begin{cvardesc}{PyTypeObject}{PyCallIter_Type}
2282 Type object for iterator objects returned by
2283 \cfunction{PyCallIter_New()} and the two-argument form of the
2284 \function{iter()} built-in function.
2285 \versionadded{2.2}
2286\end{cvardesc}
2287
2288\begin{cfuncdesc}{int}{PyCallIter_Check}{op}
2289 Return true if the type of \var{op} is \cdata{PyCallIter_Type}.
2290 \versionadded{2.2}
2291\end{cfuncdesc}
2292
2293\begin{cfuncdesc}{PyObject*}{PyCallIter_New}{PyObject *callable,
2294 PyObject *sentinel}
2295 Return a new iterator. The first parameter, \var{callable}, can be
2296 any Python callable object that can be called with no parameters;
2297 each call to it should return the next item in the iteration. When
2298 \var{callable} returns a value equal to \var{sentinel}, the
2299 iteration will be terminated.
2300 \versionadded{2.2}
2301\end{cfuncdesc}
2302
2303
2304\subsection{Descriptor Objects \label{descriptor-objects}}
2305
Fred Drake54e62942001-12-11 19:40:16 +00002306``Descriptors'' are objects that describe some attribute of an object.
2307They are found in the dictionary of type objects.
2308
Fred Drake3adf79e2001-10-12 19:01:43 +00002309\begin{cvardesc}{PyTypeObject}{PyProperty_Type}
Fred Drake54e62942001-12-11 19:40:16 +00002310 The type object for the built-in descriptor types.
Fred Drake3adf79e2001-10-12 19:01:43 +00002311 \versionadded{2.2}
2312\end{cvardesc}
2313
2314\begin{cfuncdesc}{PyObject*}{PyDescr_NewGetSet}{PyTypeObject *type,
2315 PyGetSetDef *getset}
2316 \versionadded{2.2}
2317\end{cfuncdesc}
2318
2319\begin{cfuncdesc}{PyObject*}{PyDescr_NewMember}{PyTypeObject *type,
2320 PyMemberDef *meth}
2321 \versionadded{2.2}
2322\end{cfuncdesc}
2323
2324\begin{cfuncdesc}{PyObject*}{PyDescr_NewMethod}{PyTypeObject *type,
2325 PyMethodDef *meth}
2326 \versionadded{2.2}
2327\end{cfuncdesc}
2328
2329\begin{cfuncdesc}{PyObject*}{PyDescr_NewWrapper}{PyTypeObject *type,
2330 struct wrapperbase *wrapper,
2331 void *wrapped}
2332 \versionadded{2.2}
2333\end{cfuncdesc}
2334
2335\begin{cfuncdesc}{int}{PyDescr_IsData}{PyObject *descr}
2336 Returns true if the descriptor objects \var{descr} describes a data
2337 attribute, or false if it describes a method. \var{descr} must be a
2338 descriptor object; there is no error checking.
2339 \versionadded{2.2}
2340\end{cfuncdesc}
2341
2342\begin{cfuncdesc}{PyObject*}{PyWrapper_New}{PyObject *, PyObject *}
2343 \versionadded{2.2}
2344\end{cfuncdesc}
2345
2346
2347\subsection{Slice Objects \label{slice-objects}}
2348
2349\begin{cvardesc}{PyTypeObject}{PySlice_Type}
2350 The type object for slice objects. This is the same as
2351 \code{types.SliceType}.
2352 \withsubitem{(in module types)}{\ttindex{SliceType}}
2353\end{cvardesc}
2354
2355\begin{cfuncdesc}{int}{PySlice_Check}{PyObject *ob}
2356 Returns true if \var{ob} is a slice object; \var{ob} must not be
2357 \NULL.
2358\end{cfuncdesc}
2359
2360\begin{cfuncdesc}{PyObject*}{PySlice_New}{PyObject *start, PyObject *stop,
2361 PyObject *step}
2362 Return a new slice object with the given values. The \var{start},
2363 \var{stop}, and \var{step} parameters are used as the values of the
2364 slice object attributes of the same names. Any of the values may be
2365 \NULL, in which case the \code{None} will be used for the
2366 corresponding attribute. Returns \NULL{} if the new object could
2367 not be allocated.
2368\end{cfuncdesc}
2369
2370\begin{cfuncdesc}{int}{PySlice_GetIndices}{PySliceObject *slice, int length,
2371 int *start, int *stop, int *step}
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002372Retrieve the start, stop and step indices from the slice object
2373\var{slice}, assuming a sequence of length \var{length}. Treats
2374indices greater than \var{length} as errors.
2375
2376Returns 0 on success and -1 on error with no exception set (unless one
2377of the indices was not \constant{None} and failed to be converted to
2378an integer, in which case -1 is returned with an exception set).
2379
2380You probably do not want to use this function. If you want to use
2381slice objects in versions of Python prior to 2.3, you would probably
2382do well to incorporate the source of \cfunction{PySlice_GetIndicesEx},
2383suitably renamed, in the source of your extension.
2384\end{cfuncdesc}
2385
2386\begin{cfuncdesc}{int}{PySlice_GetIndicesEx}{PySliceObject *slice, int length,
2387 int *start, int *stop, int *step,
2388 int *slicelength}
2389Usable replacement for \cfunction{PySlice_GetIndices}. Retrieve the
2390start, stop, and step indices from the slice object \var{slice}
2391assuming a sequence of length \var{length}, and store the length of
2392the slice in \var{slicelength}. Out of bounds indices are clipped in
2393a manner consistent with the handling of normal slices.
2394
2395Returns 0 on success and -1 on error with exception set.
2396
2397\versionadded{2.3}
Fred Drake3adf79e2001-10-12 19:01:43 +00002398\end{cfuncdesc}
2399
2400
2401\subsection{Weak Reference Objects \label{weakref-objects}}
2402
2403Python supports \emph{weak references} as first-class objects. There
2404are two specific object types which directly implement weak
2405references. The first is a simple reference object, and the second
2406acts as a proxy for the original object as much as it can.
2407
2408\begin{cfuncdesc}{int}{PyWeakref_Check}{ob}
2409 Return true if \var{ob} is either a reference or proxy object.
2410 \versionadded{2.2}
2411\end{cfuncdesc}
2412
2413\begin{cfuncdesc}{int}{PyWeakref_CheckRef}{ob}
2414 Return true if \var{ob} is a reference object.
2415 \versionadded{2.2}
2416\end{cfuncdesc}
2417
2418\begin{cfuncdesc}{int}{PyWeakref_CheckProxy}{ob}
2419 Return true if \var{ob} is a proxy object.
2420 \versionadded{2.2}
2421\end{cfuncdesc}
2422
2423\begin{cfuncdesc}{PyObject*}{PyWeakref_NewRef}{PyObject *ob,
2424 PyObject *callback}
2425 Return a weak reference object for the object \var{ob}. This will
2426 always return a new reference, but is not guaranteed to create a new
2427 object; an existing reference object may be returned. The second
2428 parameter, \var{callback}, can be a callable object that receives
2429 notification when \var{ob} is garbage collected; it should accept a
2430 single paramter, which will be the weak reference object itself.
2431 \var{callback} may also be \code{None} or \NULL. If \var{ob}
2432 is not a weakly-referencable object, or if \var{callback} is not
2433 callable, \code{None}, or \NULL, this will return \NULL{} and
2434 raise \exception{TypeError}.
2435 \versionadded{2.2}
2436\end{cfuncdesc}
2437
2438\begin{cfuncdesc}{PyObject*}{PyWeakref_NewProxy}{PyObject *ob,
2439 PyObject *callback}
2440 Return a weak reference proxy object for the object \var{ob}. This
2441 will always return a new reference, but is not guaranteed to create
2442 a new object; an existing proxy object may be returned. The second
2443 parameter, \var{callback}, can be a callable object that receives
2444 notification when \var{ob} is garbage collected; it should accept a
2445 single paramter, which will be the weak reference object itself.
2446 \var{callback} may also be \code{None} or \NULL. If \var{ob} is not
2447 a weakly-referencable object, or if \var{callback} is not callable,
2448 \code{None}, or \NULL, this will return \NULL{} and raise
2449 \exception{TypeError}.
2450 \versionadded{2.2}
2451\end{cfuncdesc}
2452
2453\begin{cfuncdesc}{PyObject*}{PyWeakref_GetObject}{PyObject *ref}
2454 Returns the referenced object from a weak reference, \var{ref}. If
Ka-Ping Yeebd379e92003-03-28 18:07:16 +00002455 the referent is no longer live, returns \code{None}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002456 \versionadded{2.2}
2457\end{cfuncdesc}
2458
2459\begin{cfuncdesc}{PyObject*}{PyWeakref_GET_OBJECT}{PyObject *ref}
2460 Similar to \cfunction{PyWeakref_GetObject()}, but implemented as a
2461 macro that does no error checking.
2462 \versionadded{2.2}
2463\end{cfuncdesc}
2464
2465
2466\subsection{CObjects \label{cObjects}}
2467
2468\obindex{CObject}
2469Refer to \emph{Extending and Embedding the Python Interpreter},
Fred Drake54e62942001-12-11 19:40:16 +00002470section~1.12, ``Providing a C API for an Extension Module,'' for more
Fred Drake3adf79e2001-10-12 19:01:43 +00002471information on using these objects.
2472
2473
2474\begin{ctypedesc}{PyCObject}
2475 This subtype of \ctype{PyObject} represents an opaque value, useful
2476 for C extension modules who need to pass an opaque value (as a
2477 \ctype{void*} pointer) through Python code to other C code. It is
2478 often used to make a C function pointer defined in one module
2479 available to other modules, so the regular import mechanism can be
2480 used to access C APIs defined in dynamically loaded modules.
2481\end{ctypedesc}
2482
2483\begin{cfuncdesc}{int}{PyCObject_Check}{PyObject *p}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002484 Return true if its argument is a \ctype{PyCObject}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002485\end{cfuncdesc}
2486
Tim Petersf582b822001-12-11 18:51:08 +00002487\begin{cfuncdesc}{PyObject*}{PyCObject_FromVoidPtr}{void* cobj,
Fred Drake54e62942001-12-11 19:40:16 +00002488 void (*destr)(void *)}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002489 Create a \ctype{PyCObject} from the \code{void *}\var{cobj}. The
Fred Drake3adf79e2001-10-12 19:01:43 +00002490 \var{destr} function will be called when the object is reclaimed,
2491 unless it is \NULL.
2492\end{cfuncdesc}
2493
2494\begin{cfuncdesc}{PyObject*}{PyCObject_FromVoidPtrAndDesc}{void* cobj,
2495 void* desc, void (*destr)(void *, void *)}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002496 Create a \ctype{PyCObject} from the \ctype{void *}\var{cobj}. The
Fred Drake3adf79e2001-10-12 19:01:43 +00002497 \var{destr} function will be called when the object is reclaimed.
2498 The \var{desc} argument can be used to pass extra callback data for
2499 the destructor function.
2500\end{cfuncdesc}
2501
2502\begin{cfuncdesc}{void*}{PyCObject_AsVoidPtr}{PyObject* self}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002503 Return the object \ctype{void *} that the \ctype{PyCObject}
Fred Drake3adf79e2001-10-12 19:01:43 +00002504 \var{self} was created with.
2505\end{cfuncdesc}
2506
2507\begin{cfuncdesc}{void*}{PyCObject_GetDesc}{PyObject* self}
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002508 Return the description \ctype{void *} that the \ctype{PyCObject}
Fred Drake3adf79e2001-10-12 19:01:43 +00002509 \var{self} was created with.
2510\end{cfuncdesc}
Fred Drakecd8474e2001-11-26 21:29:17 +00002511
Martin v. Löwis01a74b22003-10-19 18:30:01 +00002512\begin{cfuncdesc}{int}{PyCObject_SetVoidPtr}{PyObject* self, void* cobj}
2513 Set the void pointer inside \var{self} to \var{cobj}.
2514 The \ctype{PyCObject} must not have an associated destructor.
2515 Return true on success, false on failure.
2516\end{cfuncdesc}
2517
Fred Drakecd8474e2001-11-26 21:29:17 +00002518
2519\subsection{Cell Objects \label{cell-objects}}
2520
2521``Cell'' objects are used to implement variables referenced by
2522multiple scopes. For each such variable, a cell object is created to
2523store the value; the local variables of each stack frame that
2524references the value contains a reference to the cells from outer
2525scopes which also use that variable. When the value is accessed, the
2526value contained in the cell is used instead of the cell object
2527itself. This de-referencing of the cell object requires support from
2528the generated byte-code; these are not automatically de-referenced
2529when accessed. Cell objects are not likely to be useful elsewhere.
2530
Fred Drake54e62942001-12-11 19:40:16 +00002531\begin{ctypedesc}{PyCellObject}
2532 The C structure used for cell objects.
2533\end{ctypedesc}
2534
Fred Drakecd8474e2001-11-26 21:29:17 +00002535\begin{cvardesc}{PyTypeObject}{PyCell_Type}
2536 The type object corresponding to cell objects
2537\end{cvardesc}
2538
2539\begin{cfuncdesc}{int}{PyCell_Check}{ob}
2540 Return true if \var{ob} is a cell object; \var{ob} must not be
2541 \NULL.
2542\end{cfuncdesc}
2543
2544\begin{cfuncdesc}{PyObject*}{PyCell_New}{PyObject *ob}
2545 Create and return a new cell object containing the value \var{ob}.
2546 The parameter may be \NULL.
2547\end{cfuncdesc}
2548
2549\begin{cfuncdesc}{PyObject*}{PyCell_Get}{PyObject *cell}
2550 Return the contents of the cell \var{cell}.
2551\end{cfuncdesc}
2552
2553\begin{cfuncdesc}{PyObject*}{PyCell_GET}{PyObject *cell}
2554 Return the contents of the cell \var{cell}, but without checking
Raymond Hettingerf4bb1f92003-08-23 03:38:11 +00002555 that \var{cell} is non-\NULL{} and a cell object.
Fred Drakecd8474e2001-11-26 21:29:17 +00002556\end{cfuncdesc}
2557
2558\begin{cfuncdesc}{int}{PyCell_Set}{PyObject *cell, PyObject *value}
2559 Set the contents of the cell object \var{cell} to \var{value}. This
2560 releases the reference to any current content of the cell.
2561 \var{value} may be \NULL. \var{cell} must be non-\NULL; if it is
2562 not a cell object, \code{-1} will be returned. On success, \code{0}
2563 will be returned.
2564\end{cfuncdesc}
2565
2566\begin{cfuncdesc}{void}{PyCell_SET}{PyObject *cell, PyObject *value}
2567 Sets the value of the cell object \var{cell} to \var{value}. No
2568 reference counts are adjusted, and no checks are made for safety;
2569 \var{cell} must be non-\NULL{} and must be a cell object.
2570\end{cfuncdesc}