blob: f8cbc2834b1590ed6625add86ac64cbccff4c179 [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
976run-time, e.g. when the application invokes setlocale.
977
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
1115These are the ``Unicode Esacpe'' codec APIs:
1116
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
1141These are the ``Raw Unicode Esacpe'' codec APIs:
1142
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
1147 Raw-Unicode-Esacpe encoded string \var{s}. Returns \NULL{} if an
1148 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
1328Unicode objects or integers as apporpriate.
1329
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
1587\begin{cfuncdesc}{int}{PyTuple_Size}{PyObject *p}
1588 Takes a pointer to a tuple object, and returns the size of that
1589 tuple.
1590\end{cfuncdesc}
1591
1592\begin{cfuncdesc}{int}{PyTuple_GET_SIZE}{PyObject *p}
1593 Return the size of the tuple \var{p}, which must be non-\NULL{} and
1594 point to a tuple; no error checking is performed.
1595\end{cfuncdesc}
1596
1597\begin{cfuncdesc}{PyObject*}{PyTuple_GetItem}{PyObject *p, int pos}
1598 Returns the object at position \var{pos} in the tuple pointed to by
1599 \var{p}. If \var{pos} is out of bounds, returns \NULL{} and sets an
1600 \exception{IndexError} exception.
1601\end{cfuncdesc}
1602
1603\begin{cfuncdesc}{PyObject*}{PyTuple_GET_ITEM}{PyObject *p, int pos}
1604 Like \cfunction{PyTuple_GetItem()}, but does no checking of its
1605 arguments.
1606\end{cfuncdesc}
1607
1608\begin{cfuncdesc}{PyObject*}{PyTuple_GetSlice}{PyObject *p,
1609 int low, int high}
1610 Takes a slice of the tuple pointed to by \var{p} from \var{low} to
1611 \var{high} and returns it as a new tuple.
1612\end{cfuncdesc}
1613
1614\begin{cfuncdesc}{int}{PyTuple_SetItem}{PyObject *p,
1615 int pos, PyObject *o}
1616 Inserts a reference to object \var{o} at position \var{pos} of the
1617 tuple pointed to by \var{p}. It returns \code{0} on success.
1618 \note{This function ``steals'' a reference to \var{o}.}
1619\end{cfuncdesc}
1620
1621\begin{cfuncdesc}{void}{PyTuple_SET_ITEM}{PyObject *p,
1622 int pos, PyObject *o}
1623 Like \cfunction{PyTuple_SetItem()}, but does no error checking, and
1624 should \emph{only} be used to fill in brand new tuples. \note{This
1625 function ``steals'' a reference to \var{o}.}
1626\end{cfuncdesc}
1627
1628\begin{cfuncdesc}{int}{_PyTuple_Resize}{PyObject **p, int newsize}
1629 Can be used to resize a tuple. \var{newsize} will be the new length
1630 of the tuple. Because tuples are \emph{supposed} to be immutable,
1631 this should only be used if there is only one reference to the
1632 object. Do \emph{not} use this if the tuple may already be known to
1633 some other part of the code. The tuple will always grow or shrink
1634 at the end. Think of this as destroying the old tuple and creating
1635 a new one, only more efficiently. Returns \code{0} on success.
1636 Client code should never assume that the resulting value of
1637 \code{*\var{p}} will be the same as before calling this function.
1638 If the object referenced by \code{*\var{p}} is replaced, the
1639 original \code{*\var{p}} is destroyed. On failure, returns
1640 \code{-1} and sets \code{*\var{p}} to \NULL, and raises
1641 \exception{MemoryError} or
1642 \exception{SystemError}.
Tim Petersf582b822001-12-11 18:51:08 +00001643 \versionchanged[Removed unused third parameter, \var{last_is_sticky}]{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +00001644\end{cfuncdesc}
1645
1646
1647\subsection{List Objects \label{listObjects}}
1648
1649\obindex{list}
1650\begin{ctypedesc}{PyListObject}
1651 This subtype of \ctype{PyObject} represents a Python list object.
1652\end{ctypedesc}
1653
1654\begin{cvardesc}{PyTypeObject}{PyList_Type}
1655 This instance of \ctype{PyTypeObject} represents the Python list
1656 type. This is the same object as \code{types.ListType}.
1657 \withsubitem{(in module types)}{\ttindex{ListType}}
1658\end{cvardesc}
1659
1660\begin{cfuncdesc}{int}{PyList_Check}{PyObject *p}
1661 Returns true if its argument is a \ctype{PyListObject}.
1662\end{cfuncdesc}
1663
1664\begin{cfuncdesc}{PyObject*}{PyList_New}{int len}
1665 Returns a new list of length \var{len} on success, or \NULL{} on
1666 failure.
1667\end{cfuncdesc}
1668
1669\begin{cfuncdesc}{int}{PyList_Size}{PyObject *list}
1670 Returns the length of the list object in \var{list}; this is
1671 equivalent to \samp{len(\var{list})} on a list object.
1672 \bifuncindex{len}
1673\end{cfuncdesc}
1674
1675\begin{cfuncdesc}{int}{PyList_GET_SIZE}{PyObject *list}
1676 Macro form of \cfunction{PyList_Size()} without error checking.
1677\end{cfuncdesc}
1678
1679\begin{cfuncdesc}{PyObject*}{PyList_GetItem}{PyObject *list, int index}
1680 Returns the object at position \var{pos} in the list pointed to by
1681 \var{p}. If \var{pos} is out of bounds, returns \NULL{} and sets an
1682 \exception{IndexError} exception.
1683\end{cfuncdesc}
1684
1685\begin{cfuncdesc}{PyObject*}{PyList_GET_ITEM}{PyObject *list, int i}
1686 Macro form of \cfunction{PyList_GetItem()} without error checking.
1687\end{cfuncdesc}
1688
1689\begin{cfuncdesc}{int}{PyList_SetItem}{PyObject *list, int index,
1690 PyObject *item}
1691 Sets the item at index \var{index} in list to \var{item}. Returns
1692 \code{0} on success or \code{-1} on failure. \note{This function
1693 ``steals'' a reference to \var{item} and discards a reference to an
1694 item already in the list at the affected position.}
1695\end{cfuncdesc}
1696
1697\begin{cfuncdesc}{void}{PyList_SET_ITEM}{PyObject *list, int i,
1698 PyObject *o}
1699 Macro form of \cfunction{PyList_SetItem()} without error checking.
1700 This is normally only used to fill in new lists where there is no
1701 previous content.
1702 \note{This function ``steals'' a reference to \var{item}, and,
1703 unlike \cfunction{PyList_SetItem()}, does \emph{not} discard a
1704 reference to any item that it being replaced; any reference in
1705 \var{list} at position \var{i} will be leaked.}
1706\end{cfuncdesc}
1707
1708\begin{cfuncdesc}{int}{PyList_Insert}{PyObject *list, int index,
1709 PyObject *item}
1710 Inserts the item \var{item} into list \var{list} in front of index
1711 \var{index}. Returns \code{0} if successful; returns \code{-1} and
1712 raises an exception if unsuccessful. Analogous to
1713 \code{\var{list}.insert(\var{index}, \var{item})}.
1714\end{cfuncdesc}
1715
1716\begin{cfuncdesc}{int}{PyList_Append}{PyObject *list, PyObject *item}
1717 Appends the object \var{item} at the end of list \var{list}.
1718 Returns \code{0} if successful; returns \code{-1} and sets an
1719 exception if unsuccessful. Analogous to
1720 \code{\var{list}.append(\var{item})}.
1721\end{cfuncdesc}
1722
1723\begin{cfuncdesc}{PyObject*}{PyList_GetSlice}{PyObject *list,
1724 int low, int high}
1725 Returns a list of the objects in \var{list} containing the objects
1726 \emph{between} \var{low} and \var{high}. Returns \NULL{} and sets
1727 an exception if unsuccessful.
1728 Analogous to \code{\var{list}[\var{low}:\var{high}]}.
1729\end{cfuncdesc}
1730
1731\begin{cfuncdesc}{int}{PyList_SetSlice}{PyObject *list,
1732 int low, int high,
1733 PyObject *itemlist}
1734 Sets the slice of \var{list} between \var{low} and \var{high} to the
1735 contents of \var{itemlist}. Analogous to
1736 \code{\var{list}[\var{low}:\var{high}] = \var{itemlist}}. Returns
1737 \code{0} on success, \code{-1} on failure.
1738\end{cfuncdesc}
1739
1740\begin{cfuncdesc}{int}{PyList_Sort}{PyObject *list}
1741 Sorts the items of \var{list} in place. Returns \code{0} on
1742 success, \code{-1} on failure. This is equivalent to
1743 \samp{\var{list}.sort()}.
1744\end{cfuncdesc}
1745
1746\begin{cfuncdesc}{int}{PyList_Reverse}{PyObject *list}
1747 Reverses the items of \var{list} in place. Returns \code{0} on
1748 success, \code{-1} on failure. This is the equivalent of
1749 \samp{\var{list}.reverse()}.
1750\end{cfuncdesc}
1751
1752\begin{cfuncdesc}{PyObject*}{PyList_AsTuple}{PyObject *list}
1753 Returns a new tuple object containing the contents of \var{list};
1754 equivalent to \samp{tuple(\var{list})}.\bifuncindex{tuple}
1755\end{cfuncdesc}
1756
1757
1758\section{Mapping Objects \label{mapObjects}}
1759
1760\obindex{mapping}
1761
1762
1763\subsection{Dictionary Objects \label{dictObjects}}
1764
1765\obindex{dictionary}
1766\begin{ctypedesc}{PyDictObject}
1767 This subtype of \ctype{PyObject} represents a Python dictionary
1768 object.
1769\end{ctypedesc}
1770
1771\begin{cvardesc}{PyTypeObject}{PyDict_Type}
1772 This instance of \ctype{PyTypeObject} represents the Python
1773 dictionary type. This is exposed to Python programs as
1774 \code{types.DictType} and \code{types.DictionaryType}.
1775 \withsubitem{(in module types)}{\ttindex{DictType}\ttindex{DictionaryType}}
1776\end{cvardesc}
1777
1778\begin{cfuncdesc}{int}{PyDict_Check}{PyObject *p}
1779 Returns true if its argument is a \ctype{PyDictObject}.
1780\end{cfuncdesc}
1781
1782\begin{cfuncdesc}{PyObject*}{PyDict_New}{}
1783 Returns a new empty dictionary, or \NULL{} on failure.
1784\end{cfuncdesc}
1785
1786\begin{cfuncdesc}{PyObject*}{PyDictProxy_New}{PyObject *dict}
1787 Return a proxy object for a mapping which enforces read-only
1788 behavior. This is normally used to create a proxy to prevent
1789 modification of the dictionary for non-dynamic class types.
1790 \versionadded{2.2}
1791\end{cfuncdesc}
1792
1793\begin{cfuncdesc}{void}{PyDict_Clear}{PyObject *p}
1794 Empties an existing dictionary of all key-value pairs.
1795\end{cfuncdesc}
1796
1797\begin{cfuncdesc}{PyObject*}{PyDict_Copy}{PyObject *p}
1798 Returns a new dictionary that contains the same key-value pairs as
1799 \var{p}.
1800 \versionadded{1.6}
1801\end{cfuncdesc}
1802
1803\begin{cfuncdesc}{int}{PyDict_SetItem}{PyObject *p, PyObject *key,
1804 PyObject *val}
1805 Inserts \var{value} into the dictionary \var{p} with a key of
1806 \var{key}. \var{key} must be hashable; if it isn't,
1807 \exception{TypeError} will be raised.
1808 Returns \code{0} on success or \code{-1} on failure.
1809\end{cfuncdesc}
1810
1811\begin{cfuncdesc}{int}{PyDict_SetItemString}{PyObject *p,
1812 char *key,
1813 PyObject *val}
1814 Inserts \var{value} into the dictionary \var{p} using \var{key} as a
1815 key. \var{key} should be a \ctype{char*}. The key object is created
1816 using \code{PyString_FromString(\var{key})}. Returns \code{0} on
1817 success or \code{-1} on failure.
1818 \ttindex{PyString_FromString()}
1819\end{cfuncdesc}
1820
1821\begin{cfuncdesc}{int}{PyDict_DelItem}{PyObject *p, PyObject *key}
1822 Removes the entry in dictionary \var{p} with key \var{key}.
1823 \var{key} must be hashable; if it isn't, \exception{TypeError} is
Skip Montanaroa23bc422002-01-23 08:18:30 +00001824 raised. Returns \code{0} on success or \code{-1} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00001825\end{cfuncdesc}
1826
1827\begin{cfuncdesc}{int}{PyDict_DelItemString}{PyObject *p, char *key}
1828 Removes the entry in dictionary \var{p} which has a key specified by
1829 the string \var{key}. Returns \code{0} on success or \code{-1} on
1830 failure.
1831\end{cfuncdesc}
1832
1833\begin{cfuncdesc}{PyObject*}{PyDict_GetItem}{PyObject *p, PyObject *key}
1834 Returns the object from dictionary \var{p} which has a key
1835 \var{key}. Returns \NULL{} if the key \var{key} is not present, but
1836 \emph{without} setting an exception.
1837\end{cfuncdesc}
1838
1839\begin{cfuncdesc}{PyObject*}{PyDict_GetItemString}{PyObject *p, char *key}
1840 This is the same as \cfunction{PyDict_GetItem()}, but \var{key} is
1841 specified as a \ctype{char*}, rather than a \ctype{PyObject*}.
1842\end{cfuncdesc}
1843
1844\begin{cfuncdesc}{PyObject*}{PyDict_Items}{PyObject *p}
1845 Returns a \ctype{PyListObject} containing all the items from the
1846 dictionary, as in the dictinoary method \method{items()} (see the
1847 \citetitle[../lib/lib.html]{Python Library Reference}).
1848\end{cfuncdesc}
1849
1850\begin{cfuncdesc}{PyObject*}{PyDict_Keys}{PyObject *p}
1851 Returns a \ctype{PyListObject} containing all the keys from the
1852 dictionary, as in the dictionary method \method{keys()} (see the
1853 \citetitle[../lib/lib.html]{Python Library Reference}).
1854\end{cfuncdesc}
1855
1856\begin{cfuncdesc}{PyObject*}{PyDict_Values}{PyObject *p}
1857 Returns a \ctype{PyListObject} containing all the values from the
1858 dictionary \var{p}, as in the dictionary method \method{values()}
1859 (see the \citetitle[../lib/lib.html]{Python Library Reference}).
1860\end{cfuncdesc}
1861
1862\begin{cfuncdesc}{int}{PyDict_Size}{PyObject *p}
1863 Returns the number of items in the dictionary. This is equivalent
1864 to \samp{len(\var{p})} on a dictionary.\bifuncindex{len}
1865\end{cfuncdesc}
1866
1867\begin{cfuncdesc}{int}{PyDict_Next}{PyObject *p, int *ppos,
1868 PyObject **pkey, PyObject **pvalue}
1869 Iterate over all key-value pairs in the dictionary \var{p}. The
1870 \ctype{int} referred to by \var{ppos} must be initialized to
1871 \code{0} prior to the first call to this function to start the
1872 iteration; the function returns true for each pair in the
1873 dictionary, and false once all pairs have been reported. The
1874 parameters \var{pkey} and \var{pvalue} should either point to
1875 \ctype{PyObject*} variables that will be filled in with each key and
Skip Montanaroea3ceaa2002-01-23 10:54:41 +00001876 value, respectively, or may be \NULL. Any references returned through
1877 them are borrowed.
Fred Drake3adf79e2001-10-12 19:01:43 +00001878
1879 For example:
1880
1881\begin{verbatim}
1882PyObject *key, *value;
1883int pos = 0;
1884
1885while (PyDict_Next(self->dict, &pos, &key, &value)) {
1886 /* do something interesting with the values... */
1887 ...
1888}
1889\end{verbatim}
1890
1891 The dictionary \var{p} should not be mutated during iteration. It
1892 is safe (since Python 2.1) to modify the values of the keys as you
1893 iterate over the dictionary, but only so long as the set of keys
1894 does not change. For example:
1895
1896\begin{verbatim}
1897PyObject *key, *value;
1898int pos = 0;
1899
1900while (PyDict_Next(self->dict, &pos, &key, &value)) {
1901 int i = PyInt_AS_LONG(value) + 1;
1902 PyObject *o = PyInt_FromLong(i);
1903 if (o == NULL)
1904 return -1;
1905 if (PyDict_SetItem(self->dict, key, o) < 0) {
1906 Py_DECREF(o);
1907 return -1;
1908 }
1909 Py_DECREF(o);
1910}
1911\end{verbatim}
1912\end{cfuncdesc}
1913
1914\begin{cfuncdesc}{int}{PyDict_Merge}{PyObject *a, PyObject *b, int override}
Tim Petersf582b822001-12-11 18:51:08 +00001915 Iterate over mapping object \var{b} adding key-value pairs to dictionary
1916 \var{a}.
1917 \var{b} may be a dictionary, or any object supporting
1918 \function{PyMapping_Keys()} and \function{PyObject_GetItem()}.
1919 If \var{override} is true, existing pairs in \var{a} will
Fred Drake3adf79e2001-10-12 19:01:43 +00001920 be replaced if a matching key is found in \var{b}, otherwise pairs
1921 will only be added if there is not a matching key in \var{a}.
Tim Petersf582b822001-12-11 18:51:08 +00001922 Return \code{0} on success or \code{-1} if an exception was
Fred Drake3adf79e2001-10-12 19:01:43 +00001923 raised.
1924\versionadded{2.2}
1925\end{cfuncdesc}
1926
1927\begin{cfuncdesc}{int}{PyDict_Update}{PyObject *a, PyObject *b}
1928 This is the same as \code{PyDict_Merge(\var{a}, \var{b}, 1)} in C,
Tim Petersf582b822001-12-11 18:51:08 +00001929 or \code{\var{a}.update(\var{b})} in Python. Return \code{0} on
Fred Drake3adf79e2001-10-12 19:01:43 +00001930 success or \code{-1} if an exception was raised.
1931 \versionadded{2.2}
1932\end{cfuncdesc}
1933
Tim Petersf582b822001-12-11 18:51:08 +00001934\begin{cfuncdesc}{int}{PyDict_MergeFromSeq2}{PyObject *a, PyObject *seq2,
1935 int override}
1936 Update or merge into dictionary \var{a}, from the key-value pairs in
1937 \var{seq2}. \var{seq2} must be an iterable object producing
1938 iterable objects of length 2, viewed as key-value pairs. In case of
1939 duplicate keys, the last wins if \var{override} is true, else the
1940 first wins.
1941 Return \code{0} on success or \code{-1} if an exception
1942 was raised.
1943 Equivalent Python (except for the return value):
1944
1945\begin{verbatim}
1946def PyDict_MergeFromSeq2(a, seq2, override):
1947 for key, value in seq2:
1948 if override or key not in a:
1949 a[key] = value
1950\end{verbatim}
1951
1952 \versionadded{2.2}
1953\end{cfuncdesc}
Fred Drake3adf79e2001-10-12 19:01:43 +00001954
Fred Drake54e62942001-12-11 19:40:16 +00001955
Fred Drake3adf79e2001-10-12 19:01:43 +00001956\section{Other Objects \label{otherObjects}}
1957
1958\subsection{File Objects \label{fileObjects}}
1959
1960\obindex{file}
1961Python's built-in file objects are implemented entirely on the
1962\ctype{FILE*} support from the C standard library. This is an
1963implementation detail and may change in future releases of Python.
1964
1965\begin{ctypedesc}{PyFileObject}
1966 This subtype of \ctype{PyObject} represents a Python file object.
1967\end{ctypedesc}
1968
1969\begin{cvardesc}{PyTypeObject}{PyFile_Type}
1970 This instance of \ctype{PyTypeObject} represents the Python file
1971 type. This is exposed to Python programs as \code{types.FileType}.
1972 \withsubitem{(in module types)}{\ttindex{FileType}}
1973\end{cvardesc}
1974
1975\begin{cfuncdesc}{int}{PyFile_Check}{PyObject *p}
1976 Returns true if its argument is a \ctype{PyFileObject} or a subtype
1977 of \ctype{PyFileObject}.
1978 \versionchanged[Allowed subtypes to be accepted]{2.2}
1979\end{cfuncdesc}
1980
1981\begin{cfuncdesc}{int}{PyFile_CheckExact}{PyObject *p}
1982 Returns true if its argument is a \ctype{PyFileObject}, but not a
1983 subtype of \ctype{PyFileObject}.
1984 \versionadded{2.2}
1985\end{cfuncdesc}
1986
1987\begin{cfuncdesc}{PyObject*}{PyFile_FromString}{char *filename, char *mode}
1988 On success, returns a new file object that is opened on the file
1989 given by \var{filename}, with a file mode given by \var{mode}, where
1990 \var{mode} has the same semantics as the standard C routine
1991 \cfunction{fopen()}\ttindex{fopen()}. On failure, returns \NULL.
1992\end{cfuncdesc}
1993
1994\begin{cfuncdesc}{PyObject*}{PyFile_FromFile}{FILE *fp,
1995 char *name, char *mode,
1996 int (*close)(FILE*)}
1997 Creates a new \ctype{PyFileObject} from the already-open standard C
1998 file pointer, \var{fp}. The function \var{close} will be called
1999 when the file should be closed. Returns \NULL{} on failure.
2000\end{cfuncdesc}
2001
2002\begin{cfuncdesc}{FILE*}{PyFile_AsFile}{PyFileObject *p}
2003 Returns the file object associated with \var{p} as a \ctype{FILE*}.
2004\end{cfuncdesc}
2005
2006\begin{cfuncdesc}{PyObject*}{PyFile_GetLine}{PyObject *p, int n}
2007 Equivalent to \code{\var{p}.readline(\optional{\var{n}})}, this
2008 function reads one line from the object \var{p}. \var{p} may be a
2009 file object or any object with a \method{readline()} method. If
2010 \var{n} is \code{0}, exactly one line is read, regardless of the
2011 length of the line. If \var{n} is greater than \code{0}, no more
2012 than \var{n} bytes will be read from the file; a partial line can be
2013 returned. In both cases, an empty string is returned if the end of
2014 the file is reached immediately. If \var{n} is less than \code{0},
2015 however, one line is read regardless of length, but
2016 \exception{EOFError} is raised if the end of the file is reached
2017 immediately.
2018 \withsubitem{(built-in exception)}{\ttindex{EOFError}}
2019\end{cfuncdesc}
2020
2021\begin{cfuncdesc}{PyObject*}{PyFile_Name}{PyObject *p}
2022 Returns the name of the file specified by \var{p} as a string
2023 object.
2024\end{cfuncdesc}
2025
2026\begin{cfuncdesc}{void}{PyFile_SetBufSize}{PyFileObject *p, int n}
2027 Available on systems with \cfunction{setvbuf()}\ttindex{setvbuf()}
2028 only. This should only be called immediately after file object
2029 creation.
2030\end{cfuncdesc}
2031
2032\begin{cfuncdesc}{int}{PyFile_SoftSpace}{PyObject *p, int newflag}
2033 This function exists for internal use by the interpreter. Sets the
2034 \member{softspace} attribute of \var{p} to \var{newflag} and
2035 \withsubitem{(file attribute)}{\ttindex{softspace}}returns the
2036 previous value. \var{p} does not have to be a file object for this
2037 function to work properly; any object is supported (thought its only
2038 interesting if the \member{softspace} attribute can be set). This
2039 function clears any errors, and will return \code{0} as the previous
2040 value if the attribute either does not exist or if there were errors
2041 in retrieving it. There is no way to detect errors from this
2042 function, but doing so should not be needed.
2043\end{cfuncdesc}
2044
2045\begin{cfuncdesc}{int}{PyFile_WriteObject}{PyObject *obj, PyFileObject *p,
2046 int flags}
2047 Writes object \var{obj} to file object \var{p}. The only supported
2048 flag for \var{flags} is
2049 \constant{Py_PRINT_RAW}\ttindex{Py_PRINT_RAW}; if given, the
2050 \function{str()} of the object is written instead of the
2051 \function{repr()}. Returns \code{0} on success or \code{-1} on
2052 failure; the appropriate exception will be set.
2053\end{cfuncdesc}
2054
Fred Drake454af892001-11-29 22:42:59 +00002055\begin{cfuncdesc}{int}{PyFile_WriteString}{const char *s, PyFileObject *p}
Fred Drake3adf79e2001-10-12 19:01:43 +00002056 Writes string \var{s} to file object \var{p}. Returns \code{0} on
2057 success or \code{-1} on failure; the appropriate exception will be
2058 set.
2059\end{cfuncdesc}
2060
2061
2062\subsection{Instance Objects \label{instanceObjects}}
2063
2064\obindex{instance}
2065There are very few functions specific to instance objects.
2066
2067\begin{cvardesc}{PyTypeObject}{PyInstance_Type}
2068 Type object for class instances.
2069\end{cvardesc}
2070
2071\begin{cfuncdesc}{int}{PyInstance_Check}{PyObject *obj}
2072 Returns true if \var{obj} is an instance.
2073\end{cfuncdesc}
2074
2075\begin{cfuncdesc}{PyObject*}{PyInstance_New}{PyObject *class,
2076 PyObject *arg,
2077 PyObject *kw}
2078 Create a new instance of a specific class. The parameters \var{arg}
2079 and \var{kw} are used as the positional and keyword parameters to
2080 the object's constructor.
2081\end{cfuncdesc}
2082
2083\begin{cfuncdesc}{PyObject*}{PyInstance_NewRaw}{PyObject *class,
2084 PyObject *dict}
2085 Create a new instance of a specific class without calling it's
2086 constructor. \var{class} is the class of new object. The
2087 \var{dict} parameter will be used as the object's \member{__dict__};
2088 if \NULL, a new dictionary will be created for the instance.
2089\end{cfuncdesc}
2090
2091
2092\subsection{Method Objects \label{method-objects}}
2093
2094\obindex{method}
2095There are some useful functions that are useful for working with
2096method objects.
2097
2098\begin{cvardesc}{PyTypeObject}{PyMethod_Type}
2099 This instance of \ctype{PyTypeObject} represents the Python method
2100 type. This is exposed to Python programs as \code{types.MethodType}.
2101 \withsubitem{(in module types)}{\ttindex{MethodType}}
2102\end{cvardesc}
2103
2104\begin{cfuncdesc}{int}{PyMethod_Check}{PyObject *o}
2105 Return true if \var{o} is a method object (has type
2106 \cdata{PyMethod_Type}). The parameter must not be \NULL.
2107\end{cfuncdesc}
2108
2109\begin{cfuncdesc}{PyObject*}{PyMethod_New}{PyObject *func.
2110 PyObject *self, PyObject *class}
2111 Return a new method object, with \var{func} being any callable
2112 object; this is the function that will be called when the method is
2113 called. If this method should be bound to an instance, \var{self}
2114 should be the instance and \var{class} should be the class of
2115 \var{self}, otherwise \var{self} should be \NULL{} and \var{class}
2116 should be the class which provides the unbound method..
2117\end{cfuncdesc}
2118
2119\begin{cfuncdesc}{PyObject*}{PyMethod_Class}{PyObject *meth}
2120 Return the class object from which the method \var{meth} was
2121 created; if this was created from an instance, it will be the class
2122 of the instance.
2123\end{cfuncdesc}
2124
2125\begin{cfuncdesc}{PyObject*}{PyMethod_GET_CLASS}{PyObject *meth}
2126 Macro version of \cfunction{PyMethod_Class()} which avoids error
2127 checking.
2128\end{cfuncdesc}
2129
2130\begin{cfuncdesc}{PyObject*}{PyMethod_Function}{PyObject *meth}
2131 Return the function object associated with the method \var{meth}.
2132\end{cfuncdesc}
2133
2134\begin{cfuncdesc}{PyObject*}{PyMethod_GET_FUNCTION}{PyObject *meth}
2135 Macro version of \cfunction{PyMethod_Function()} which avoids error
2136 checking.
2137\end{cfuncdesc}
2138
2139\begin{cfuncdesc}{PyObject*}{PyMethod_Self}{PyObject *meth}
2140 Return the instance associated with the method \var{meth} if it is
2141 bound, otherwise return \NULL.
2142\end{cfuncdesc}
2143
2144\begin{cfuncdesc}{PyObject*}{PyMethod_GET_SELF}{PyObject *meth}
2145 Macro version of \cfunction{PyMethod_Self()} which avoids error
2146 checking.
2147\end{cfuncdesc}
2148
2149
2150\subsection{Module Objects \label{moduleObjects}}
2151
2152\obindex{module}
2153There are only a few functions special to module objects.
2154
2155\begin{cvardesc}{PyTypeObject}{PyModule_Type}
2156 This instance of \ctype{PyTypeObject} represents the Python module
2157 type. This is exposed to Python programs as
2158 \code{types.ModuleType}.
2159 \withsubitem{(in module types)}{\ttindex{ModuleType}}
2160\end{cvardesc}
2161
2162\begin{cfuncdesc}{int}{PyModule_Check}{PyObject *p}
2163 Returns true if \var{p} is a module object, or a subtype of a module
2164 object.
2165 \versionchanged[Allowed subtypes to be accepted]{2.2}
2166\end{cfuncdesc}
2167
2168\begin{cfuncdesc}{int}{PyModule_CheckExact}{PyObject *p}
2169 Returns true if \var{p} is a module object, but not a subtype of
2170 \cdata{PyModule_Type}.
2171 \versionadded{2.2}
2172\end{cfuncdesc}
2173
2174\begin{cfuncdesc}{PyObject*}{PyModule_New}{char *name}
2175 Return a new module object with the \member{__name__} attribute set
2176 to \var{name}. Only the module's \member{__doc__} and
2177 \member{__name__} attributes are filled in; the caller is
2178 responsible for providing a \member{__file__} attribute.
2179 \withsubitem{(module attribute)}{
2180 \ttindex{__name__}\ttindex{__doc__}\ttindex{__file__}}
2181\end{cfuncdesc}
2182
2183\begin{cfuncdesc}{PyObject*}{PyModule_GetDict}{PyObject *module}
2184 Return the dictionary object that implements \var{module}'s
2185 namespace; this object is the same as the \member{__dict__}
2186 attribute of the module object. This function never fails.
2187 \withsubitem{(module attribute)}{\ttindex{__dict__}}
Fred Drakef495ef72002-04-12 19:32:07 +00002188 It is recommended extensions use other \cfunction{PyModule_*()}
2189 and \cfunction{PyObject_*()} functions rather than directly
2190 manipulate a module's \member{__dict__}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002191\end{cfuncdesc}
2192
2193\begin{cfuncdesc}{char*}{PyModule_GetName}{PyObject *module}
2194 Return \var{module}'s \member{__name__} value. If the module does
2195 not provide one, or if it is not a string, \exception{SystemError}
2196 is raised and \NULL{} is returned.
2197 \withsubitem{(module attribute)}{\ttindex{__name__}}
2198 \withsubitem{(built-in exception)}{\ttindex{SystemError}}
2199\end{cfuncdesc}
2200
2201\begin{cfuncdesc}{char*}{PyModule_GetFilename}{PyObject *module}
2202 Return the name of the file from which \var{module} was loaded using
2203 \var{module}'s \member{__file__} attribute. If this is not defined,
2204 or if it is not a string, raise \exception{SystemError} and return
2205 \NULL.
2206 \withsubitem{(module attribute)}{\ttindex{__file__}}
2207 \withsubitem{(built-in exception)}{\ttindex{SystemError}}
2208\end{cfuncdesc}
2209
2210\begin{cfuncdesc}{int}{PyModule_AddObject}{PyObject *module,
2211 char *name, PyObject *value}
2212 Add an object to \var{module} as \var{name}. This is a convenience
2213 function which can be used from the module's initialization
2214 function. This steals a reference to \var{value}. Returns
2215 \code{-1} on error, \code{0} on success.
2216 \versionadded{2.0}
2217\end{cfuncdesc}
2218
2219\begin{cfuncdesc}{int}{PyModule_AddIntConstant}{PyObject *module,
2220 char *name, int value}
2221 Add an integer constant to \var{module} as \var{name}. This
2222 convenience function can be used from the module's initialization
2223 function. Returns \code{-1} on error, \code{0} on success.
2224 \versionadded{2.0}
2225\end{cfuncdesc}
2226
2227\begin{cfuncdesc}{int}{PyModule_AddStringConstant}{PyObject *module,
2228 char *name, char *value}
2229 Add a string constant to \var{module} as \var{name}. This
2230 convenience function can be used from the module's initialization
2231 function. The string \var{value} must be null-terminated. Returns
2232 \code{-1} on error, \code{0} on success.
2233 \versionadded{2.0}
2234\end{cfuncdesc}
2235
2236
2237\subsection{Iterator Objects \label{iterator-objects}}
2238
2239Python provides two general-purpose iterator objects. The first, a
2240sequence iterator, works with an arbitrary sequence supporting the
2241\method{__getitem__()} method. The second works with a callable
2242object and a sentinel value, calling the callable for each item in the
2243sequence, and ending the iteration when the sentinel value is
2244returned.
2245
2246\begin{cvardesc}{PyTypeObject}{PySeqIter_Type}
2247 Type object for iterator objects returned by
2248 \cfunction{PySeqIter_New()} and the one-argument form of the
2249 \function{iter()} built-in function for built-in sequence types.
2250 \versionadded{2.2}
2251\end{cvardesc}
2252
2253\begin{cfuncdesc}{int}{PySeqIter_Check}{op}
2254 Return true if the type of \var{op} is \cdata{PySeqIter_Type}.
2255 \versionadded{2.2}
2256\end{cfuncdesc}
2257
2258\begin{cfuncdesc}{PyObject*}{PySeqIter_New}{PyObject *seq}
2259 Return an iterator that works with a general sequence object,
2260 \var{seq}. The iteration ends when the sequence raises
2261 \exception{IndexError} for the subscripting operation.
2262 \versionadded{2.2}
2263\end{cfuncdesc}
2264
2265\begin{cvardesc}{PyTypeObject}{PyCallIter_Type}
2266 Type object for iterator objects returned by
2267 \cfunction{PyCallIter_New()} and the two-argument form of the
2268 \function{iter()} built-in function.
2269 \versionadded{2.2}
2270\end{cvardesc}
2271
2272\begin{cfuncdesc}{int}{PyCallIter_Check}{op}
2273 Return true if the type of \var{op} is \cdata{PyCallIter_Type}.
2274 \versionadded{2.2}
2275\end{cfuncdesc}
2276
2277\begin{cfuncdesc}{PyObject*}{PyCallIter_New}{PyObject *callable,
2278 PyObject *sentinel}
2279 Return a new iterator. The first parameter, \var{callable}, can be
2280 any Python callable object that can be called with no parameters;
2281 each call to it should return the next item in the iteration. When
2282 \var{callable} returns a value equal to \var{sentinel}, the
2283 iteration will be terminated.
2284 \versionadded{2.2}
2285\end{cfuncdesc}
2286
2287
2288\subsection{Descriptor Objects \label{descriptor-objects}}
2289
Fred Drake54e62942001-12-11 19:40:16 +00002290``Descriptors'' are objects that describe some attribute of an object.
2291They are found in the dictionary of type objects.
2292
Fred Drake3adf79e2001-10-12 19:01:43 +00002293\begin{cvardesc}{PyTypeObject}{PyProperty_Type}
Fred Drake54e62942001-12-11 19:40:16 +00002294 The type object for the built-in descriptor types.
Fred Drake3adf79e2001-10-12 19:01:43 +00002295 \versionadded{2.2}
2296\end{cvardesc}
2297
2298\begin{cfuncdesc}{PyObject*}{PyDescr_NewGetSet}{PyTypeObject *type,
2299 PyGetSetDef *getset}
2300 \versionadded{2.2}
2301\end{cfuncdesc}
2302
2303\begin{cfuncdesc}{PyObject*}{PyDescr_NewMember}{PyTypeObject *type,
2304 PyMemberDef *meth}
2305 \versionadded{2.2}
2306\end{cfuncdesc}
2307
2308\begin{cfuncdesc}{PyObject*}{PyDescr_NewMethod}{PyTypeObject *type,
2309 PyMethodDef *meth}
2310 \versionadded{2.2}
2311\end{cfuncdesc}
2312
2313\begin{cfuncdesc}{PyObject*}{PyDescr_NewWrapper}{PyTypeObject *type,
2314 struct wrapperbase *wrapper,
2315 void *wrapped}
2316 \versionadded{2.2}
2317\end{cfuncdesc}
2318
2319\begin{cfuncdesc}{int}{PyDescr_IsData}{PyObject *descr}
2320 Returns true if the descriptor objects \var{descr} describes a data
2321 attribute, or false if it describes a method. \var{descr} must be a
2322 descriptor object; there is no error checking.
2323 \versionadded{2.2}
2324\end{cfuncdesc}
2325
2326\begin{cfuncdesc}{PyObject*}{PyWrapper_New}{PyObject *, PyObject *}
2327 \versionadded{2.2}
2328\end{cfuncdesc}
2329
2330
2331\subsection{Slice Objects \label{slice-objects}}
2332
2333\begin{cvardesc}{PyTypeObject}{PySlice_Type}
2334 The type object for slice objects. This is the same as
2335 \code{types.SliceType}.
2336 \withsubitem{(in module types)}{\ttindex{SliceType}}
2337\end{cvardesc}
2338
2339\begin{cfuncdesc}{int}{PySlice_Check}{PyObject *ob}
2340 Returns true if \var{ob} is a slice object; \var{ob} must not be
2341 \NULL.
2342\end{cfuncdesc}
2343
2344\begin{cfuncdesc}{PyObject*}{PySlice_New}{PyObject *start, PyObject *stop,
2345 PyObject *step}
2346 Return a new slice object with the given values. The \var{start},
2347 \var{stop}, and \var{step} parameters are used as the values of the
2348 slice object attributes of the same names. Any of the values may be
2349 \NULL, in which case the \code{None} will be used for the
2350 corresponding attribute. Returns \NULL{} if the new object could
2351 not be allocated.
2352\end{cfuncdesc}
2353
2354\begin{cfuncdesc}{int}{PySlice_GetIndices}{PySliceObject *slice, int length,
2355 int *start, int *stop, int *step}
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002356Retrieve the start, stop and step indices from the slice object
2357\var{slice}, assuming a sequence of length \var{length}. Treats
2358indices greater than \var{length} as errors.
2359
2360Returns 0 on success and -1 on error with no exception set (unless one
2361of the indices was not \constant{None} and failed to be converted to
2362an integer, in which case -1 is returned with an exception set).
2363
2364You probably do not want to use this function. If you want to use
2365slice objects in versions of Python prior to 2.3, you would probably
2366do well to incorporate the source of \cfunction{PySlice_GetIndicesEx},
2367suitably renamed, in the source of your extension.
2368\end{cfuncdesc}
2369
2370\begin{cfuncdesc}{int}{PySlice_GetIndicesEx}{PySliceObject *slice, int length,
2371 int *start, int *stop, int *step,
2372 int *slicelength}
2373Usable replacement for \cfunction{PySlice_GetIndices}. Retrieve the
2374start, stop, and step indices from the slice object \var{slice}
2375assuming a sequence of length \var{length}, and store the length of
2376the slice in \var{slicelength}. Out of bounds indices are clipped in
2377a manner consistent with the handling of normal slices.
2378
2379Returns 0 on success and -1 on error with exception set.
2380
2381\versionadded{2.3}
Fred Drake3adf79e2001-10-12 19:01:43 +00002382\end{cfuncdesc}
2383
2384
2385\subsection{Weak Reference Objects \label{weakref-objects}}
2386
2387Python supports \emph{weak references} as first-class objects. There
2388are two specific object types which directly implement weak
2389references. The first is a simple reference object, and the second
2390acts as a proxy for the original object as much as it can.
2391
2392\begin{cfuncdesc}{int}{PyWeakref_Check}{ob}
2393 Return true if \var{ob} is either a reference or proxy object.
2394 \versionadded{2.2}
2395\end{cfuncdesc}
2396
2397\begin{cfuncdesc}{int}{PyWeakref_CheckRef}{ob}
2398 Return true if \var{ob} is a reference object.
2399 \versionadded{2.2}
2400\end{cfuncdesc}
2401
2402\begin{cfuncdesc}{int}{PyWeakref_CheckProxy}{ob}
2403 Return true if \var{ob} is a proxy object.
2404 \versionadded{2.2}
2405\end{cfuncdesc}
2406
2407\begin{cfuncdesc}{PyObject*}{PyWeakref_NewRef}{PyObject *ob,
2408 PyObject *callback}
2409 Return a weak reference object for the object \var{ob}. This will
2410 always return a new reference, but is not guaranteed to create a new
2411 object; an existing reference object may be returned. The second
2412 parameter, \var{callback}, can be a callable object that receives
2413 notification when \var{ob} is garbage collected; it should accept a
2414 single paramter, which will be the weak reference object itself.
2415 \var{callback} may also be \code{None} or \NULL. If \var{ob}
2416 is not a weakly-referencable object, or if \var{callback} is not
2417 callable, \code{None}, or \NULL, this will return \NULL{} and
2418 raise \exception{TypeError}.
2419 \versionadded{2.2}
2420\end{cfuncdesc}
2421
2422\begin{cfuncdesc}{PyObject*}{PyWeakref_NewProxy}{PyObject *ob,
2423 PyObject *callback}
2424 Return a weak reference proxy object for the object \var{ob}. This
2425 will always return a new reference, but is not guaranteed to create
2426 a new object; an existing proxy object may be returned. The second
2427 parameter, \var{callback}, can be a callable object that receives
2428 notification when \var{ob} is garbage collected; it should accept a
2429 single paramter, which will be the weak reference object itself.
2430 \var{callback} may also be \code{None} or \NULL. If \var{ob} is not
2431 a weakly-referencable object, or if \var{callback} is not callable,
2432 \code{None}, or \NULL, this will return \NULL{} and raise
2433 \exception{TypeError}.
2434 \versionadded{2.2}
2435\end{cfuncdesc}
2436
2437\begin{cfuncdesc}{PyObject*}{PyWeakref_GetObject}{PyObject *ref}
2438 Returns the referenced object from a weak reference, \var{ref}. If
Ka-Ping Yeebd379e92003-03-28 18:07:16 +00002439 the referent is no longer live, returns \code{None}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002440 \versionadded{2.2}
2441\end{cfuncdesc}
2442
2443\begin{cfuncdesc}{PyObject*}{PyWeakref_GET_OBJECT}{PyObject *ref}
2444 Similar to \cfunction{PyWeakref_GetObject()}, but implemented as a
2445 macro that does no error checking.
2446 \versionadded{2.2}
2447\end{cfuncdesc}
2448
2449
2450\subsection{CObjects \label{cObjects}}
2451
2452\obindex{CObject}
2453Refer to \emph{Extending and Embedding the Python Interpreter},
Fred Drake54e62942001-12-11 19:40:16 +00002454section~1.12, ``Providing a C API for an Extension Module,'' for more
Fred Drake3adf79e2001-10-12 19:01:43 +00002455information on using these objects.
2456
2457
2458\begin{ctypedesc}{PyCObject}
2459 This subtype of \ctype{PyObject} represents an opaque value, useful
2460 for C extension modules who need to pass an opaque value (as a
2461 \ctype{void*} pointer) through Python code to other C code. It is
2462 often used to make a C function pointer defined in one module
2463 available to other modules, so the regular import mechanism can be
2464 used to access C APIs defined in dynamically loaded modules.
2465\end{ctypedesc}
2466
2467\begin{cfuncdesc}{int}{PyCObject_Check}{PyObject *p}
2468 Returns true if its argument is a \ctype{PyCObject}.
2469\end{cfuncdesc}
2470
Tim Petersf582b822001-12-11 18:51:08 +00002471\begin{cfuncdesc}{PyObject*}{PyCObject_FromVoidPtr}{void* cobj,
Fred Drake54e62942001-12-11 19:40:16 +00002472 void (*destr)(void *)}
Fred Drake3adf79e2001-10-12 19:01:43 +00002473 Creates a \ctype{PyCObject} from the \code{void *}\var{cobj}. The
2474 \var{destr} function will be called when the object is reclaimed,
2475 unless it is \NULL.
2476\end{cfuncdesc}
2477
2478\begin{cfuncdesc}{PyObject*}{PyCObject_FromVoidPtrAndDesc}{void* cobj,
2479 void* desc, void (*destr)(void *, void *)}
2480 Creates a \ctype{PyCObject} from the \ctype{void *}\var{cobj}. The
2481 \var{destr} function will be called when the object is reclaimed.
2482 The \var{desc} argument can be used to pass extra callback data for
2483 the destructor function.
2484\end{cfuncdesc}
2485
2486\begin{cfuncdesc}{void*}{PyCObject_AsVoidPtr}{PyObject* self}
2487 Returns the object \ctype{void *} that the \ctype{PyCObject}
2488 \var{self} was created with.
2489\end{cfuncdesc}
2490
2491\begin{cfuncdesc}{void*}{PyCObject_GetDesc}{PyObject* self}
2492 Returns the description \ctype{void *} that the \ctype{PyCObject}
2493 \var{self} was created with.
2494\end{cfuncdesc}
Fred Drakecd8474e2001-11-26 21:29:17 +00002495
2496
2497\subsection{Cell Objects \label{cell-objects}}
2498
2499``Cell'' objects are used to implement variables referenced by
2500multiple scopes. For each such variable, a cell object is created to
2501store the value; the local variables of each stack frame that
2502references the value contains a reference to the cells from outer
2503scopes which also use that variable. When the value is accessed, the
2504value contained in the cell is used instead of the cell object
2505itself. This de-referencing of the cell object requires support from
2506the generated byte-code; these are not automatically de-referenced
2507when accessed. Cell objects are not likely to be useful elsewhere.
2508
Fred Drake54e62942001-12-11 19:40:16 +00002509\begin{ctypedesc}{PyCellObject}
2510 The C structure used for cell objects.
2511\end{ctypedesc}
2512
Fred Drakecd8474e2001-11-26 21:29:17 +00002513\begin{cvardesc}{PyTypeObject}{PyCell_Type}
2514 The type object corresponding to cell objects
2515\end{cvardesc}
2516
2517\begin{cfuncdesc}{int}{PyCell_Check}{ob}
2518 Return true if \var{ob} is a cell object; \var{ob} must not be
2519 \NULL.
2520\end{cfuncdesc}
2521
2522\begin{cfuncdesc}{PyObject*}{PyCell_New}{PyObject *ob}
2523 Create and return a new cell object containing the value \var{ob}.
2524 The parameter may be \NULL.
2525\end{cfuncdesc}
2526
2527\begin{cfuncdesc}{PyObject*}{PyCell_Get}{PyObject *cell}
2528 Return the contents of the cell \var{cell}.
2529\end{cfuncdesc}
2530
2531\begin{cfuncdesc}{PyObject*}{PyCell_GET}{PyObject *cell}
2532 Return the contents of the cell \var{cell}, but without checking
2533 that \var{cell} is non-\NULL{} and a call object.
2534\end{cfuncdesc}
2535
2536\begin{cfuncdesc}{int}{PyCell_Set}{PyObject *cell, PyObject *value}
2537 Set the contents of the cell object \var{cell} to \var{value}. This
2538 releases the reference to any current content of the cell.
2539 \var{value} may be \NULL. \var{cell} must be non-\NULL; if it is
2540 not a cell object, \code{-1} will be returned. On success, \code{0}
2541 will be returned.
2542\end{cfuncdesc}
2543
2544\begin{cfuncdesc}{void}{PyCell_SET}{PyObject *cell, PyObject *value}
2545 Sets the value of the cell object \var{cell} to \var{value}. No
2546 reference counts are adjusted, and no checks are made for safety;
2547 \var{cell} must be non-\NULL{} and must be a cell object.
2548\end{cfuncdesc}