blob: 7b8409213e5ca5d7463317baf1102308ab3dbfa2 [file] [log] [blame]
Fred Drake3adf79e2001-10-12 19:01:43 +00001\chapter{Concrete Objects Layer \label{concrete}}
2
3
4The functions in this chapter are specific to certain Python object
5types. Passing them an object of the wrong type is not a good idea;
6if you receive an object from a Python program and you are not sure
7that it has the right type, you must perform a type check first;
8for example, to check that an object is a dictionary, use
9\cfunction{PyDict_Check()}. The chapter is structured like the
10``family tree'' of Python object types.
11
12\warning{While the functions described in this chapter carefully check
13the type of the objects which are passed in, many of them do not check
14for \NULL{} being passed instead of a valid object. Allowing \NULL{}
15to be passed in can cause memory access violations and immediate
16termination of the interpreter.}
17
18
19\section{Fundamental Objects \label{fundamental}}
20
Tim Petersf582b822001-12-11 18:51:08 +000021This section describes Python type objects and the singleton object
Fred Drake3adf79e2001-10-12 19:01:43 +000022\code{None}.
23
24
25\subsection{Type Objects \label{typeObjects}}
26
27\obindex{type}
28\begin{ctypedesc}{PyTypeObject}
29 The C structure of the objects used to describe built-in types.
30\end{ctypedesc}
31
32\begin{cvardesc}{PyObject*}{PyType_Type}
33 This is the type object for type objects; it is the same object as
34 \code{types.TypeType} in the Python layer.
35 \withsubitem{(in module types)}{\ttindex{TypeType}}
36\end{cvardesc}
37
38\begin{cfuncdesc}{int}{PyType_Check}{PyObject *o}
Fred Drakee3c764b2002-04-10 17:52:52 +000039 Returns true if the object \var{o} is a type object, including
40 instances of types derived from the standard type object. Returns
41 false in all other cases.
42\end{cfuncdesc}
43
44\begin{cfuncdesc}{int}{PyType_CheckExact}{PyObject *o}
45 Returns true if the object \var{o} is a type object, but not a
46 subtype of the standard type object. Returns false in all other
47 cases.
48 \versionadded{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +000049\end{cfuncdesc}
50
51\begin{cfuncdesc}{int}{PyType_HasFeature}{PyObject *o, int feature}
52 Returns true if the type object \var{o} sets the feature
53 \var{feature}. Type features are denoted by single bit flags.
54\end{cfuncdesc}
55
Fred Drakee3c764b2002-04-10 17:52:52 +000056\begin{cfuncdesc}{int}{PyType_IS_GC}{PyObject *o}
57 Return true if the type object includes support for the cycle
58 detector; this tests the type flag \constant{Py_TPFLAGS_HAVE_GC}.
59 \versionadded{2.0}
60\end{cfuncdesc}
61
Fred Drake3adf79e2001-10-12 19:01:43 +000062\begin{cfuncdesc}{int}{PyType_IsSubtype}{PyTypeObject *a, PyTypeObject *b}
63 Returns true if \var{a} is a subtype of \var{b}.
64 \versionadded{2.2}
65\end{cfuncdesc}
66
67\begin{cfuncdesc}{PyObject*}{PyType_GenericAlloc}{PyTypeObject *type,
68 int nitems}
69 \versionadded{2.2}
70\end{cfuncdesc}
71
72\begin{cfuncdesc}{PyObject*}{PyType_GenericNew}{PyTypeObject *type,
73 PyObject *args, PyObject *kwds}
74 \versionadded{2.2}
75\end{cfuncdesc}
76
77\begin{cfuncdesc}{int}{PyType_Ready}{PyTypeObject *type}
Fred Drake28de8d42002-04-12 16:15:10 +000078 Finalize a type object. This should be called on all type objects
79 to finish their initialization. This function is responsible for
80 adding inherited slots from a type's base class. Returns \code{0}
81 on success, or returns \code{-1} and sets an exception on error.
Fred Drake3adf79e2001-10-12 19:01:43 +000082 \versionadded{2.2}
83\end{cfuncdesc}
84
85
86\subsection{The None Object \label{noneObject}}
87
88\obindex{None@\texttt{None}}
89Note that the \ctype{PyTypeObject} for \code{None} is not directly
90exposed in the Python/C API. Since \code{None} is a singleton,
91testing for object identity (using \samp{==} in C) is sufficient.
92There is no \cfunction{PyNone_Check()} function for the same reason.
93
94\begin{cvardesc}{PyObject*}{Py_None}
95 The Python \code{None} object, denoting lack of value. This object
Fred Drake6ccdccd2002-03-12 20:12:54 +000096 has no methods. It needs to be treated just like any other object
97 with respect to reference counts.
Fred Drake3adf79e2001-10-12 19:01:43 +000098\end{cvardesc}
99
100
101\section{Numeric Objects \label{numericObjects}}
102
103\obindex{numeric}
104
105
106\subsection{Plain Integer Objects \label{intObjects}}
107
108\obindex{integer}
109\begin{ctypedesc}{PyIntObject}
110 This subtype of \ctype{PyObject} represents a Python integer
111 object.
112\end{ctypedesc}
113
114\begin{cvardesc}{PyTypeObject}{PyInt_Type}
Tim Petersf582b822001-12-11 18:51:08 +0000115 This instance of \ctype{PyTypeObject} represents the Python plain
Fred Drake3adf79e2001-10-12 19:01:43 +0000116 integer type. This is the same object as \code{types.IntType}.
117 \withsubitem{(in modules types)}{\ttindex{IntType}}
118\end{cvardesc}
119
120\begin{cfuncdesc}{int}{PyInt_Check}{PyObject* o}
121 Returns true if \var{o} is of type \cdata{PyInt_Type} or a subtype
122 of \cdata{PyInt_Type}.
123 \versionchanged[Allowed subtypes to be accepted]{2.2}
124\end{cfuncdesc}
125
126\begin{cfuncdesc}{int}{PyInt_CheckExact}{PyObject* o}
127 Returns true if \var{o} is of type \cdata{PyInt_Type}, but not a
128 subtype of \cdata{PyInt_Type}.
129 \versionadded{2.2}
130\end{cfuncdesc}
131
Skip Montanaro1ff49a72003-02-03 05:13:24 +0000132\begin{cfuncdesc}{PyObject*}{PyInt_FromString}{char *str, char **pend,
133 int base}
134 Return a new \ctype{PyIntObject} or \ctype{PyLongObject} based on the
135 string value in \var{str}, which is interpreted according to the radix in
136 \var{base}. If \var{pend} is non-\NULL, \code{*\var{pend}} will point to
137 the first character in \var{str} which follows the representation of the
138 number. If \var{base} is \code{0}, the radix will be determined based on
139 the leading characters of \var{str}: if \var{str} starts with \code{'0x'}
140 or \code{'0X'}, radix 16 will be used; if \var{str} starts with
141 \code{'0'}, radix 8 will be used; otherwise radix 10 will be used. If
142 \var{base} is not \code{0}, it must be between \code{2} and \code{36},
143 inclusive. Leading spaces are ignored. If there are no digits,
144 \exception{ValueError} will be raised. If the string represents a number
145 too large to be contained within the machine's \ctype{long int} type and
146 overflow warnings are being suppressed, a \ctype{PyLongObject} will be
147 returned. If overflow warnings are not being suppressed, \NULL{} will be
148 returned in this case.
149\end{cfuncdesc}
150
Fred Drake3adf79e2001-10-12 19:01:43 +0000151\begin{cfuncdesc}{PyObject*}{PyInt_FromLong}{long ival}
152 Creates a new integer object with a value of \var{ival}.
153
154 The current implementation keeps an array of integer objects for all
155 integers between \code{-1} and \code{100}, when you create an int in
156 that range you actually just get back a reference to the existing
157 object. So it should be possible to change the value of \code{1}. I
158 suspect the behaviour of Python in this case is undefined. :-)
159\end{cfuncdesc}
160
161\begin{cfuncdesc}{long}{PyInt_AsLong}{PyObject *io}
162 Will first attempt to cast the object to a \ctype{PyIntObject}, if
163 it is not already one, and then return its value.
164\end{cfuncdesc}
165
166\begin{cfuncdesc}{long}{PyInt_AS_LONG}{PyObject *io}
167 Returns the value of the object \var{io}. No error checking is
168 performed.
169\end{cfuncdesc}
170
Thomas Heller34d7f092003-04-23 19:51:05 +0000171\begin{cfuncdesc}{unsigned long}{PyInt_AsUnsignedLongMask}{PyObject *io}
172 Will first attempt to cast the object to a \ctype{PyIntObject} or
Fred Drakec22b2992003-04-23 20:38:41 +0000173 \ctype{PyLongObject}, if it is not already one, and then return its
Thomas Heller34d7f092003-04-23 19:51:05 +0000174 value as unsigned long. This function does not check for overflow.
175 \versionadded{2.3}
176\end{cfuncdesc}
177
178\begin{cfuncdesc}{unsigned long}{PyInt_AsUnsignedLongLongMask}{PyObject *io}
179 Will first attempt to cast the object to a \ctype{PyIntObject} or
Fred Drakec22b2992003-04-23 20:38:41 +0000180 \ctype{PyLongObject}, if it is not already one, and then return its
Thomas Heller34d7f092003-04-23 19:51:05 +0000181 value as unsigned long long, without checking for overflow.
182 \versionadded{2.3}
183\end{cfuncdesc}
184
Fred Drake3adf79e2001-10-12 19:01:43 +0000185\begin{cfuncdesc}{long}{PyInt_GetMax}{}
186 Returns the system's idea of the largest integer it can handle
187 (\constant{LONG_MAX}\ttindex{LONG_MAX}, as defined in the system
188 header files).
189\end{cfuncdesc}
190
191
192\subsection{Long Integer Objects \label{longObjects}}
193
194\obindex{long integer}
195\begin{ctypedesc}{PyLongObject}
196 This subtype of \ctype{PyObject} represents a Python long integer
197 object.
198\end{ctypedesc}
199
200\begin{cvardesc}{PyTypeObject}{PyLong_Type}
201 This instance of \ctype{PyTypeObject} represents the Python long
202 integer type. This is the same object as \code{types.LongType}.
203 \withsubitem{(in modules types)}{\ttindex{LongType}}
204\end{cvardesc}
205
206\begin{cfuncdesc}{int}{PyLong_Check}{PyObject *p}
207 Returns true if its argument is a \ctype{PyLongObject} or a subtype
208 of \ctype{PyLongObject}.
209 \versionchanged[Allowed subtypes to be accepted]{2.2}
210\end{cfuncdesc}
211
212\begin{cfuncdesc}{int}{PyLong_CheckExact}{PyObject *p}
213 Returns true if its argument is a \ctype{PyLongObject}, but not a
214 subtype of \ctype{PyLongObject}.
215 \versionadded{2.2}
216\end{cfuncdesc}
217
218\begin{cfuncdesc}{PyObject*}{PyLong_FromLong}{long v}
219 Returns a new \ctype{PyLongObject} object from \var{v}, or \NULL{}
220 on failure.
221\end{cfuncdesc}
222
223\begin{cfuncdesc}{PyObject*}{PyLong_FromUnsignedLong}{unsigned long v}
224 Returns a new \ctype{PyLongObject} object from a C \ctype{unsigned
225 long}, or \NULL{} on failure.
226\end{cfuncdesc}
227
228\begin{cfuncdesc}{PyObject*}{PyLong_FromLongLong}{long long v}
229 Returns a new \ctype{PyLongObject} object from a C \ctype{long long},
230 or \NULL{} on failure.
231\end{cfuncdesc}
232
233\begin{cfuncdesc}{PyObject*}{PyLong_FromUnsignedLongLong}{unsigned long long v}
234 Returns a new \ctype{PyLongObject} object from a C \ctype{unsigned
235 long long}, or \NULL{} on failure.
236\end{cfuncdesc}
237
238\begin{cfuncdesc}{PyObject*}{PyLong_FromDouble}{double v}
239 Returns a new \ctype{PyLongObject} object from the integer part of
240 \var{v}, or \NULL{} on failure.
241\end{cfuncdesc}
242
243\begin{cfuncdesc}{PyObject*}{PyLong_FromString}{char *str, char **pend,
244 int base}
245 Return a new \ctype{PyLongObject} based on the string value in
246 \var{str}, which is interpreted according to the radix in
247 \var{base}. If \var{pend} is non-\NULL, \code{*\var{pend}} will
248 point to the first character in \var{str} which follows the
249 representation of the number. If \var{base} is \code{0}, the radix
Skip Montanaro1ff49a72003-02-03 05:13:24 +0000250 will be determined based on the leading characters of \var{str}: if
Fred Drake3adf79e2001-10-12 19:01:43 +0000251 \var{str} starts with \code{'0x'} or \code{'0X'}, radix 16 will be
252 used; if \var{str} starts with \code{'0'}, radix 8 will be used;
253 otherwise radix 10 will be used. If \var{base} is not \code{0}, it
254 must be between \code{2} and \code{36}, inclusive. Leading spaces
255 are ignored. If there are no digits, \exception{ValueError} will be
256 raised.
257\end{cfuncdesc}
258
259\begin{cfuncdesc}{PyObject*}{PyLong_FromUnicode}{Py_UNICODE *u,
260 int length, int base}
261 Convert a sequence of Unicode digits to a Python long integer
262 value. The first parameter, \var{u}, points to the first character
263 of the Unicode string, \var{length} gives the number of characters,
264 and \var{base} is the radix for the conversion. The radix must be
265 in the range [2, 36]; if it is out of range, \exception{ValueError}
266 will be raised.
267 \versionadded{1.6}
268\end{cfuncdesc}
269
270\begin{cfuncdesc}{PyObject*}{PyLong_FromVoidPtr}{void *p}
271 Create a Python integer or long integer from the pointer \var{p}.
272 The pointer value can be retrieved from the resulting value using
273 \cfunction{PyLong_AsVoidPtr()}.
274 \versionadded{1.5.2}
275\end{cfuncdesc}
276
277\begin{cfuncdesc}{long}{PyLong_AsLong}{PyObject *pylong}
278 Returns a C \ctype{long} representation of the contents of
279 \var{pylong}. If \var{pylong} is greater than
280 \constant{LONG_MAX}\ttindex{LONG_MAX}, an \exception{OverflowError}
281 is raised.
282 \withsubitem{(built-in exception)}{\ttindex{OverflowError}}
283\end{cfuncdesc}
284
285\begin{cfuncdesc}{unsigned long}{PyLong_AsUnsignedLong}{PyObject *pylong}
286 Returns a C \ctype{unsigned long} representation of the contents of
287 \var{pylong}. If \var{pylong} is greater than
288 \constant{ULONG_MAX}\ttindex{ULONG_MAX}, an
289 \exception{OverflowError} is raised.
Tim Petersf582b822001-12-11 18:51:08 +0000290 \withsubitem{(built-in exception)}{\ttindex{OverflowError}}
Fred Drake3adf79e2001-10-12 19:01:43 +0000291\end{cfuncdesc}
292
293\begin{cfuncdesc}{long long}{PyLong_AsLongLong}{PyObject *pylong}
294 Return a C \ctype{long long} from a Python long integer. If
295 \var{pylong} cannot be represented as a \ctype{long long}, an
296 \exception{OverflowError} will be raised.
297 \versionadded{2.2}
298\end{cfuncdesc}
299
300\begin{cfuncdesc}{unsigned long long}{PyLong_AsUnsignedLongLong}{PyObject
301 *pylong}
302 Return a C \ctype{unsigned long long} from a Python long integer.
303 If \var{pylong} cannot be represented as an \ctype{unsigned long
304 long}, an \exception{OverflowError} will be raised if the value is
305 positive, or a \exception{TypeError} will be raised if the value is
306 negative.
307 \versionadded{2.2}
308\end{cfuncdesc}
309
Thomas Heller34d7f092003-04-23 19:51:05 +0000310\begin{cfuncdesc}{unsigned long}{PyLong_AsUnsignedLongMask}{PyObject *io}
311 Return a C \ctype{unsigned long} from a Python long integer, without
312 checking for overflow.
313 \versionadded{2.3}
314\end{cfuncdesc}
315
316\begin{cfuncdesc}{unsigned long}{PyLong_AsUnsignedLongLongMask}{PyObject *io}
317 Return a C \ctype{unsigned long long} from a Python long integer, without
318 checking for overflow.
319 \versionadded{2.3}
320\end{cfuncdesc}
321
Fred Drake3adf79e2001-10-12 19:01:43 +0000322\begin{cfuncdesc}{double}{PyLong_AsDouble}{PyObject *pylong}
323 Returns a C \ctype{double} representation of the contents of
324 \var{pylong}. If \var{pylong} cannot be approximately represented
325 as a \ctype{double}, an \exception{OverflowError} exception is
326 raised and \code{-1.0} will be returned.
327\end{cfuncdesc}
328
329\begin{cfuncdesc}{void*}{PyLong_AsVoidPtr}{PyObject *pylong}
330 Convert a Python integer or long integer \var{pylong} to a C
331 \ctype{void} pointer. If \var{pylong} cannot be converted, an
332 \exception{OverflowError} will be raised. This is only assured to
333 produce a usable \ctype{void} pointer for values created with
334 \cfunction{PyLong_FromVoidPtr()}.
335 \versionadded{1.5.2}
336\end{cfuncdesc}
337
338
339\subsection{Floating Point Objects \label{floatObjects}}
340
341\obindex{floating point}
342\begin{ctypedesc}{PyFloatObject}
343 This subtype of \ctype{PyObject} represents a Python floating point
344 object.
345\end{ctypedesc}
346
347\begin{cvardesc}{PyTypeObject}{PyFloat_Type}
348 This instance of \ctype{PyTypeObject} represents the Python floating
349 point type. This is the same object as \code{types.FloatType}.
350 \withsubitem{(in modules types)}{\ttindex{FloatType}}
351\end{cvardesc}
352
353\begin{cfuncdesc}{int}{PyFloat_Check}{PyObject *p}
354 Returns true if its argument is a \ctype{PyFloatObject} or a subtype
355 of \ctype{PyFloatObject}.
356 \versionchanged[Allowed subtypes to be accepted]{2.2}
357\end{cfuncdesc}
358
359\begin{cfuncdesc}{int}{PyFloat_CheckExact}{PyObject *p}
360 Returns true if its argument is a \ctype{PyFloatObject}, but not a
361 subtype of \ctype{PyFloatObject}.
362 \versionadded{2.2}
363\end{cfuncdesc}
364
Skip Montanaro1ff49a72003-02-03 05:13:24 +0000365\begin{cfuncdesc}{PyObject*}{PyFloat_FromString}{PyObject *str, char **pend}
Skip Montanaroae31e9b2003-02-03 03:56:36 +0000366 Creates a \ctype{PyFloatObject} object based on the string value in
Skip Montanaro1ff49a72003-02-03 05:13:24 +0000367 \var{str}, or \NULL{} on failure. The \var{pend} argument is ignored. It
368 remains only for backward compatibility.
Skip Montanaroae31e9b2003-02-03 03:56:36 +0000369\end{cfuncdesc}
370
Fred Drake3adf79e2001-10-12 19:01:43 +0000371\begin{cfuncdesc}{PyObject*}{PyFloat_FromDouble}{double v}
372 Creates a \ctype{PyFloatObject} object from \var{v}, or \NULL{} on
373 failure.
374\end{cfuncdesc}
375
376\begin{cfuncdesc}{double}{PyFloat_AsDouble}{PyObject *pyfloat}
377 Returns a C \ctype{double} representation of the contents of
378 \var{pyfloat}.
379\end{cfuncdesc}
380
381\begin{cfuncdesc}{double}{PyFloat_AS_DOUBLE}{PyObject *pyfloat}
382 Returns a C \ctype{double} representation of the contents of
383 \var{pyfloat}, but without error checking.
384\end{cfuncdesc}
385
386
387\subsection{Complex Number Objects \label{complexObjects}}
388
389\obindex{complex number}
390Python's complex number objects are implemented as two distinct types
391when viewed from the C API: one is the Python object exposed to
392Python programs, and the other is a C structure which represents the
393actual complex number value. The API provides functions for working
394with both.
395
396\subsubsection{Complex Numbers as C Structures}
397
398Note that the functions which accept these structures as parameters
399and return them as results do so \emph{by value} rather than
400dereferencing them through pointers. This is consistent throughout
401the API.
402
403\begin{ctypedesc}{Py_complex}
404 The C structure which corresponds to the value portion of a Python
405 complex number object. Most of the functions for dealing with
406 complex number objects use structures of this type as input or
407 output values, as appropriate. It is defined as:
408
409\begin{verbatim}
410typedef struct {
411 double real;
412 double imag;
413} Py_complex;
414\end{verbatim}
415\end{ctypedesc}
416
417\begin{cfuncdesc}{Py_complex}{_Py_c_sum}{Py_complex left, Py_complex right}
418 Return the sum of two complex numbers, using the C
419 \ctype{Py_complex} representation.
420\end{cfuncdesc}
421
422\begin{cfuncdesc}{Py_complex}{_Py_c_diff}{Py_complex left, Py_complex right}
423 Return the difference between two complex numbers, using the C
424 \ctype{Py_complex} representation.
425\end{cfuncdesc}
426
427\begin{cfuncdesc}{Py_complex}{_Py_c_neg}{Py_complex complex}
428 Return the negation of the complex number \var{complex}, using the C
429 \ctype{Py_complex} representation.
430\end{cfuncdesc}
431
432\begin{cfuncdesc}{Py_complex}{_Py_c_prod}{Py_complex left, Py_complex right}
433 Return the product of two complex numbers, using the C
434 \ctype{Py_complex} representation.
435\end{cfuncdesc}
436
437\begin{cfuncdesc}{Py_complex}{_Py_c_quot}{Py_complex dividend,
438 Py_complex divisor}
439 Return the quotient of two complex numbers, using the C
440 \ctype{Py_complex} representation.
441\end{cfuncdesc}
442
443\begin{cfuncdesc}{Py_complex}{_Py_c_pow}{Py_complex num, Py_complex exp}
444 Return the exponentiation of \var{num} by \var{exp}, using the C
445 \ctype{Py_complex} representation.
446\end{cfuncdesc}
447
448
449\subsubsection{Complex Numbers as Python Objects}
450
451\begin{ctypedesc}{PyComplexObject}
452 This subtype of \ctype{PyObject} represents a Python complex number
453 object.
454\end{ctypedesc}
455
456\begin{cvardesc}{PyTypeObject}{PyComplex_Type}
457 This instance of \ctype{PyTypeObject} represents the Python complex
458 number type.
459\end{cvardesc}
460
461\begin{cfuncdesc}{int}{PyComplex_Check}{PyObject *p}
462 Returns true if its argument is a \ctype{PyComplexObject} or a
463 subtype of \ctype{PyComplexObject}.
464 \versionchanged[Allowed subtypes to be accepted]{2.2}
465\end{cfuncdesc}
466
467\begin{cfuncdesc}{int}{PyComplex_CheckExact}{PyObject *p}
468 Returns true if its argument is a \ctype{PyComplexObject}, but not a
469 subtype of \ctype{PyComplexObject}.
470 \versionadded{2.2}
471\end{cfuncdesc}
472
473\begin{cfuncdesc}{PyObject*}{PyComplex_FromCComplex}{Py_complex v}
474 Create a new Python complex number object from a C
475 \ctype{Py_complex} value.
476\end{cfuncdesc}
477
478\begin{cfuncdesc}{PyObject*}{PyComplex_FromDoubles}{double real, double imag}
479 Returns a new \ctype{PyComplexObject} object from \var{real} and
480 \var{imag}.
481\end{cfuncdesc}
482
483\begin{cfuncdesc}{double}{PyComplex_RealAsDouble}{PyObject *op}
484 Returns the real part of \var{op} as a C \ctype{double}.
485\end{cfuncdesc}
486
487\begin{cfuncdesc}{double}{PyComplex_ImagAsDouble}{PyObject *op}
488 Returns the imaginary part of \var{op} as a C \ctype{double}.
489\end{cfuncdesc}
490
491\begin{cfuncdesc}{Py_complex}{PyComplex_AsCComplex}{PyObject *op}
492 Returns the \ctype{Py_complex} value of the complex number
493 \var{op}.
494\end{cfuncdesc}
495
496
497
498\section{Sequence Objects \label{sequenceObjects}}
499
500\obindex{sequence}
Tim Petersf582b822001-12-11 18:51:08 +0000501Generic operations on sequence objects were discussed in the previous
502chapter; this section deals with the specific kinds of sequence
Fred Drake3adf79e2001-10-12 19:01:43 +0000503objects that are intrinsic to the Python language.
504
505
506\subsection{String Objects \label{stringObjects}}
507
508These functions raise \exception{TypeError} when expecting a string
509parameter and are called with a non-string parameter.
510
511\obindex{string}
512\begin{ctypedesc}{PyStringObject}
513 This subtype of \ctype{PyObject} represents a Python string object.
514\end{ctypedesc}
515
516\begin{cvardesc}{PyTypeObject}{PyString_Type}
517 This instance of \ctype{PyTypeObject} represents the Python string
518 type; it is the same object as \code{types.TypeType} in the Python
519 layer.
520 \withsubitem{(in module types)}{\ttindex{StringType}}.
521\end{cvardesc}
522
523\begin{cfuncdesc}{int}{PyString_Check}{PyObject *o}
524 Returns true if the object \var{o} is a string object or an instance
525 of a subtype of the string type.
526 \versionchanged[Allowed subtypes to be accepted]{2.2}
527\end{cfuncdesc}
528
529\begin{cfuncdesc}{int}{PyString_CheckExact}{PyObject *o}
530 Returns true if the object \var{o} is a string object, but not an
531 instance of a subtype of the string type.
532 \versionadded{2.2}
533\end{cfuncdesc}
534
535\begin{cfuncdesc}{PyObject*}{PyString_FromString}{const char *v}
536 Returns a new string object with the value \var{v} on success, and
Fred Drake32a35872001-12-06 20:38:15 +0000537 \NULL{} on failure. The parameter \var{v} must not be \NULL; it
538 will not be checked.
Fred Drake3adf79e2001-10-12 19:01:43 +0000539\end{cfuncdesc}
540
541\begin{cfuncdesc}{PyObject*}{PyString_FromStringAndSize}{const char *v,
542 int len}
543 Returns a new string object with the value \var{v} and length
544 \var{len} on success, and \NULL{} on failure. If \var{v} is
545 \NULL, the contents of the string are uninitialized.
546\end{cfuncdesc}
547
548\begin{cfuncdesc}{PyObject*}{PyString_FromFormat}{const char *format, ...}
549 Takes a C \cfunction{printf()}-style \var{format} string and a
550 variable number of arguments, calculates the size of the resulting
551 Python string and returns a string with the values formatted into
552 it. The variable arguments must be C types and must correspond
553 exactly to the format characters in the \var{format} string. The
554 following format characters are allowed:
555
556 \begin{tableiii}{l|l|l}{member}{Format Characters}{Type}{Comment}
557 \lineiii{\%\%}{\emph{n/a}}{The literal \% character.}
558 \lineiii{\%c}{int}{A single character, represented as an C int.}
559 \lineiii{\%d}{int}{Exactly equivalent to \code{printf("\%d")}.}
560 \lineiii{\%ld}{long}{Exactly equivalent to \code{printf("\%ld")}.}
561 \lineiii{\%i}{int}{Exactly equivalent to \code{printf("\%i")}.}
562 \lineiii{\%x}{int}{Exactly equivalent to \code{printf("\%x")}.}
563 \lineiii{\%s}{char*}{A null-terminated C character array.}
564 \lineiii{\%p}{void*}{The hex representation of a C pointer.
565 Mostly equivalent to \code{printf("\%p")} except that it is
566 guaranteed to start with the literal \code{0x} regardless of
567 what the platform's \code{printf} yields.}
568 \end{tableiii}
569\end{cfuncdesc}
570
571\begin{cfuncdesc}{PyObject*}{PyString_FromFormatV}{const char *format,
572 va_list vargs}
573 Identical to \function{PyString_FromFormat()} except that it takes
574 exactly two arguments.
575\end{cfuncdesc}
576
577\begin{cfuncdesc}{int}{PyString_Size}{PyObject *string}
578 Returns the length of the string in string object \var{string}.
579\end{cfuncdesc}
580
581\begin{cfuncdesc}{int}{PyString_GET_SIZE}{PyObject *string}
582 Macro form of \cfunction{PyString_Size()} but without error
583 checking.
584\end{cfuncdesc}
585
586\begin{cfuncdesc}{char*}{PyString_AsString}{PyObject *string}
Fred Drake4b247262002-10-22 20:20:20 +0000587 Returns a NUL-terminated representation of the contents of
Fred Drake3adf79e2001-10-12 19:01:43 +0000588 \var{string}. The pointer refers to the internal buffer of
589 \var{string}, not a copy. The data must not be modified in any way,
590 unless the string was just created using
591 \code{PyString_FromStringAndSize(NULL, \var{size})}.
Fred Drake4b247262002-10-22 20:20:20 +0000592 It must not be deallocated. If \var{string} is a Unicode object,
593 this function computes the default encoding of \var{string} and
594 operates on that. If \var{string} is not a string object at all,
595 \cfunction{PyString_AsString()} returns \NULL{} and raises
596 \exception{TypeError}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000597\end{cfuncdesc}
598
599\begin{cfuncdesc}{char*}{PyString_AS_STRING}{PyObject *string}
600 Macro form of \cfunction{PyString_AsString()} but without error
Fred Drake4b247262002-10-22 20:20:20 +0000601 checking. Only string objects are supported; no Unicode objects
602 should be passed.
Fred Drake3adf79e2001-10-12 19:01:43 +0000603\end{cfuncdesc}
604
605\begin{cfuncdesc}{int}{PyString_AsStringAndSize}{PyObject *obj,
606 char **buffer,
607 int *length}
Fred Drake4b247262002-10-22 20:20:20 +0000608 Returns a NUL-terminated representation of the contents of the
Fred Drake3adf79e2001-10-12 19:01:43 +0000609 object \var{obj} through the output variables \var{buffer} and
610 \var{length}.
611
612 The function accepts both string and Unicode objects as input. For
613 Unicode objects it returns the default encoded version of the
Fred Drake4b247262002-10-22 20:20:20 +0000614 object. If \var{length} is \NULL, the resulting buffer may not
615 contain NUL characters; if it does, the function returns \code{-1}
616 and a \exception{TypeError} is raised.
Fred Drake3adf79e2001-10-12 19:01:43 +0000617
618 The buffer refers to an internal string buffer of \var{obj}, not a
619 copy. The data must not be modified in any way, unless the string
620 was just created using \code{PyString_FromStringAndSize(NULL,
Fred Drake4b247262002-10-22 20:20:20 +0000621 \var{size})}. It must not be deallocated. If \var{string} is a
622 Unicode object, this function computes the default encoding of
623 \var{string} and operates on that. If \var{string} is not a string
624 object at all, \cfunction{PyString_AsString()} returns \NULL{} and
625 raises \exception{TypeError}.
Fred Drake3adf79e2001-10-12 19:01:43 +0000626\end{cfuncdesc}
627
628\begin{cfuncdesc}{void}{PyString_Concat}{PyObject **string,
629 PyObject *newpart}
630 Creates a new string object in \var{*string} containing the contents
631 of \var{newpart} appended to \var{string}; the caller will own the
632 new reference. The reference to the old value of \var{string} will
633 be stolen. If the new string cannot be created, the old reference
634 to \var{string} will still be discarded and the value of
635 \var{*string} will be set to \NULL; the appropriate exception will
636 be set.
637\end{cfuncdesc}
638
639\begin{cfuncdesc}{void}{PyString_ConcatAndDel}{PyObject **string,
640 PyObject *newpart}
641 Creates a new string object in \var{*string} containing the contents
642 of \var{newpart} appended to \var{string}. This version decrements
643 the reference count of \var{newpart}.
644\end{cfuncdesc}
645
646\begin{cfuncdesc}{int}{_PyString_Resize}{PyObject **string, int newsize}
647 A way to resize a string object even though it is ``immutable''.
648 Only use this to build up a brand new string object; don't use this
Tim Peters5de98422002-04-27 18:44:32 +0000649 if the string may already be known in other parts of the code. It
650 is an error to call this function if the refcount on the input string
651 object is not one.
652 Pass the address of an existing string object as an lvalue (it may
653 be written into), and the new size desired. On success, \var{*string}
Fred Drake432425e2002-04-29 15:17:16 +0000654 holds the resized string object and \code{0} is returned; the address in
Tim Peters5de98422002-04-27 18:44:32 +0000655 \var{*string} may differ from its input value. If the
656 reallocation fails, the original string object at \var{*string} is
657 deallocated, \var{*string} is set to \NULL{}, a memory exception is set,
Fred Drake432425e2002-04-29 15:17:16 +0000658 and \code{-1} is returned.
Fred Drake3adf79e2001-10-12 19:01:43 +0000659\end{cfuncdesc}
660
661\begin{cfuncdesc}{PyObject*}{PyString_Format}{PyObject *format,
662 PyObject *args}
663 Returns a new string object from \var{format} and \var{args}.
664 Analogous to \code{\var{format} \%\ \var{args}}. The \var{args}
665 argument must be a tuple.
666\end{cfuncdesc}
667
668\begin{cfuncdesc}{void}{PyString_InternInPlace}{PyObject **string}
669 Intern the argument \var{*string} in place. The argument must be
670 the address of a pointer variable pointing to a Python string
671 object. If there is an existing interned string that is the same as
672 \var{*string}, it sets \var{*string} to it (decrementing the
673 reference count of the old string object and incrementing the
674 reference count of the interned string object), otherwise it leaves
675 \var{*string} alone and interns it (incrementing its reference
676 count). (Clarification: even though there is a lot of talk about
677 reference counts, think of this function as reference-count-neutral;
678 you own the object after the call if and only if you owned it before
679 the call.)
680\end{cfuncdesc}
681
682\begin{cfuncdesc}{PyObject*}{PyString_InternFromString}{const char *v}
683 A combination of \cfunction{PyString_FromString()} and
684 \cfunction{PyString_InternInPlace()}, returning either a new string
685 object that has been interned, or a new (``owned'') reference to an
686 earlier interned string object with the same value.
687\end{cfuncdesc}
688
689\begin{cfuncdesc}{PyObject*}{PyString_Decode}{const char *s,
690 int size,
691 const char *encoding,
692 const char *errors}
693 Creates an object by decoding \var{size} bytes of the encoded
694 buffer \var{s} using the codec registered for
695 \var{encoding}. \var{encoding} and \var{errors} have the same
696 meaning as the parameters of the same name in the
697 \function{unicode()} built-in function. The codec to be used is
698 looked up using the Python codec registry. Returns \NULL{} if
699 an exception was raised by the codec.
700\end{cfuncdesc}
701
702\begin{cfuncdesc}{PyObject*}{PyString_AsDecodedObject}{PyObject *str,
703 const char *encoding,
704 const char *errors}
705 Decodes a string object by passing it to the codec registered for
706 \var{encoding} and returns the result as Python
707 object. \var{encoding} and \var{errors} have the same meaning as the
708 parameters of the same name in the string \method{encode()} method.
709 The codec to be used is looked up using the Python codec registry.
710 Returns \NULL{} if an exception was raised by the codec.
711\end{cfuncdesc}
712
713\begin{cfuncdesc}{PyObject*}{PyString_Encode}{const char *s,
714 int size,
715 const char *encoding,
716 const char *errors}
717 Encodes the \ctype{char} buffer of the given size by passing it to
718 the codec registered for \var{encoding} and returns a Python object.
719 \var{encoding} and \var{errors} have the same meaning as the
720 parameters of the same name in the string \method{encode()} method.
721 The codec to be used is looked up using the Python codec
722 registry. Returns \NULL{} if an exception was raised by the
723 codec.
724\end{cfuncdesc}
725
726\begin{cfuncdesc}{PyObject*}{PyString_AsEncodedObject}{PyObject *str,
727 const char *encoding,
728 const char *errors}
729 Encodes a string object using the codec registered for
730 \var{encoding} and returns the result as Python object.
731 \var{encoding} and \var{errors} have the same meaning as the
732 parameters of the same name in the string \method{encode()} method.
733 The codec to be used is looked up using the Python codec registry.
734 Returns \NULL{} if an exception was raised by the codec.
735\end{cfuncdesc}
736
737
738\subsection{Unicode Objects \label{unicodeObjects}}
739\sectionauthor{Marc-Andre Lemburg}{mal@lemburg.com}
740
741%--- Unicode Type -------------------------------------------------------
742
743These are the basic Unicode object types used for the Unicode
744implementation in Python:
745
746\begin{ctypedesc}{Py_UNICODE}
747 This type represents a 16-bit unsigned storage type which is used by
748 Python internally as basis for holding Unicode ordinals. On
749 platforms where \ctype{wchar_t} is available and also has 16-bits,
750 \ctype{Py_UNICODE} is a typedef alias for \ctype{wchar_t} to enhance
751 native platform compatibility. On all other platforms,
752 \ctype{Py_UNICODE} is a typedef alias for \ctype{unsigned short}.
753\end{ctypedesc}
754
755\begin{ctypedesc}{PyUnicodeObject}
756 This subtype of \ctype{PyObject} represents a Python Unicode object.
757\end{ctypedesc}
758
759\begin{cvardesc}{PyTypeObject}{PyUnicode_Type}
760 This instance of \ctype{PyTypeObject} represents the Python Unicode
761 type.
762\end{cvardesc}
763
764The following APIs are really C macros and can be used to do fast
765checks and to access internal read-only data of Unicode objects:
766
767\begin{cfuncdesc}{int}{PyUnicode_Check}{PyObject *o}
768 Returns true if the object \var{o} is a Unicode object or an
769 instance of a Unicode subtype.
770 \versionchanged[Allowed subtypes to be accepted]{2.2}
771\end{cfuncdesc}
772
773\begin{cfuncdesc}{int}{PyUnicode_CheckExact}{PyObject *o}
774 Returns true if the object \var{o} is a Unicode object, but not an
775 instance of a subtype.
776 \versionadded{2.2}
777\end{cfuncdesc}
778
779\begin{cfuncdesc}{int}{PyUnicode_GET_SIZE}{PyObject *o}
780 Returns the size of the object. \var{o} has to be a
781 \ctype{PyUnicodeObject} (not checked).
782\end{cfuncdesc}
783
784\begin{cfuncdesc}{int}{PyUnicode_GET_DATA_SIZE}{PyObject *o}
785 Returns the size of the object's internal buffer in bytes. \var{o}
786 has to be a \ctype{PyUnicodeObject} (not checked).
787\end{cfuncdesc}
788
789\begin{cfuncdesc}{Py_UNICODE*}{PyUnicode_AS_UNICODE}{PyObject *o}
790 Returns a pointer to the internal \ctype{Py_UNICODE} buffer of the
791 object. \var{o} has to be a \ctype{PyUnicodeObject} (not checked).
792\end{cfuncdesc}
793
794\begin{cfuncdesc}{const char*}{PyUnicode_AS_DATA}{PyObject *o}
795 Returns a pointer to the internal buffer of the object.
796 \var{o} has to be a \ctype{PyUnicodeObject} (not checked).
797\end{cfuncdesc}
798
799% --- Unicode character properties ---------------------------------------
800
801Unicode provides many different character properties. The most often
802needed ones are available through these macros which are mapped to C
803functions depending on the Python configuration.
804
805\begin{cfuncdesc}{int}{Py_UNICODE_ISSPACE}{Py_UNICODE ch}
806 Returns 1/0 depending on whether \var{ch} is a whitespace
807 character.
808\end{cfuncdesc}
809
810\begin{cfuncdesc}{int}{Py_UNICODE_ISLOWER}{Py_UNICODE ch}
811 Returns 1/0 depending on whether \var{ch} is a lowercase character.
812\end{cfuncdesc}
813
814\begin{cfuncdesc}{int}{Py_UNICODE_ISUPPER}{Py_UNICODE ch}
815 Returns 1/0 depending on whether \var{ch} is an uppercase
816 character.
817\end{cfuncdesc}
818
819\begin{cfuncdesc}{int}{Py_UNICODE_ISTITLE}{Py_UNICODE ch}
820 Returns 1/0 depending on whether \var{ch} is a titlecase character.
821\end{cfuncdesc}
822
823\begin{cfuncdesc}{int}{Py_UNICODE_ISLINEBREAK}{Py_UNICODE ch}
824 Returns 1/0 depending on whether \var{ch} is a linebreak character.
825\end{cfuncdesc}
826
827\begin{cfuncdesc}{int}{Py_UNICODE_ISDECIMAL}{Py_UNICODE ch}
828 Returns 1/0 depending on whether \var{ch} is a decimal character.
829\end{cfuncdesc}
830
831\begin{cfuncdesc}{int}{Py_UNICODE_ISDIGIT}{Py_UNICODE ch}
832 Returns 1/0 depending on whether \var{ch} is a digit character.
833\end{cfuncdesc}
834
835\begin{cfuncdesc}{int}{Py_UNICODE_ISNUMERIC}{Py_UNICODE ch}
836 Returns 1/0 depending on whether \var{ch} is a numeric character.
837\end{cfuncdesc}
838
839\begin{cfuncdesc}{int}{Py_UNICODE_ISALPHA}{Py_UNICODE ch}
840 Returns 1/0 depending on whether \var{ch} is an alphabetic
841 character.
842\end{cfuncdesc}
843
844\begin{cfuncdesc}{int}{Py_UNICODE_ISALNUM}{Py_UNICODE ch}
845 Returns 1/0 depending on whether \var{ch} is an alphanumeric
846 character.
847\end{cfuncdesc}
848
849These APIs can be used for fast direct character conversions:
850
851\begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOLOWER}{Py_UNICODE ch}
852 Returns the character \var{ch} converted to lower case.
853\end{cfuncdesc}
854
855\begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOUPPER}{Py_UNICODE ch}
856 Returns the character \var{ch} converted to upper case.
857\end{cfuncdesc}
858
859\begin{cfuncdesc}{Py_UNICODE}{Py_UNICODE_TOTITLE}{Py_UNICODE ch}
860 Returns the character \var{ch} converted to title case.
861\end{cfuncdesc}
862
863\begin{cfuncdesc}{int}{Py_UNICODE_TODECIMAL}{Py_UNICODE ch}
864 Returns the character \var{ch} converted to a decimal positive
865 integer. Returns \code{-1} if this is not possible. Does not raise
866 exceptions.
867\end{cfuncdesc}
868
869\begin{cfuncdesc}{int}{Py_UNICODE_TODIGIT}{Py_UNICODE ch}
870 Returns the character \var{ch} converted to a single digit integer.
871 Returns \code{-1} if this is not possible. Does not raise
872 exceptions.
873\end{cfuncdesc}
874
875\begin{cfuncdesc}{double}{Py_UNICODE_TONUMERIC}{Py_UNICODE ch}
876 Returns the character \var{ch} converted to a (positive) double.
877 Returns \code{-1.0} if this is not possible. Does not raise
878 exceptions.
879\end{cfuncdesc}
880
881% --- Plain Py_UNICODE ---------------------------------------------------
882
883To create Unicode objects and access their basic sequence properties,
884use these APIs:
885
886\begin{cfuncdesc}{PyObject*}{PyUnicode_FromUnicode}{const Py_UNICODE *u,
Tim Petersf582b822001-12-11 18:51:08 +0000887 int size}
Fred Drake3adf79e2001-10-12 19:01:43 +0000888 Create a Unicode Object from the Py_UNICODE buffer \var{u} of the
889 given size. \var{u} may be \NULL{} which causes the contents to be
890 undefined. It is the user's responsibility to fill in the needed
891 data. The buffer is copied into the new object. If the buffer is
892 not \NULL, the return value might be a shared object. Therefore,
893 modification of the resulting Unicode object is only allowed when
894 \var{u} is \NULL.
895\end{cfuncdesc}
896
897\begin{cfuncdesc}{Py_UNICODE*}{PyUnicode_AsUnicode}{PyObject *unicode}
898 Return a read-only pointer to the Unicode object's internal
899 \ctype{Py_UNICODE} buffer, \NULL{} if \var{unicode} is not a Unicode
900 object.
901\end{cfuncdesc}
902
903\begin{cfuncdesc}{int}{PyUnicode_GetSize}{PyObject *unicode}
904 Return the length of the Unicode object.
905\end{cfuncdesc}
906
907\begin{cfuncdesc}{PyObject*}{PyUnicode_FromEncodedObject}{PyObject *obj,
908 const char *encoding,
909 const char *errors}
910 Coerce an encoded object \var{obj} to an Unicode object and return a
911 reference with incremented refcount.
912
913 Coercion is done in the following way:
914
915\begin{enumerate}
916\item Unicode objects are passed back as-is with incremented
917 refcount. \note{These cannot be decoded; passing a non-\NULL{}
918 value for encoding will result in a \exception{TypeError}.}
919
920\item String and other char buffer compatible objects are decoded
921 according to the given encoding and using the error handling
922 defined by errors. Both can be \NULL{} to have the interface
923 use the default values (see the next section for details).
924
925\item All other objects cause an exception.
926\end{enumerate}
927
928 The API returns \NULL{} if there was an error. The caller is
929 responsible for decref'ing the returned objects.
930\end{cfuncdesc}
931
932\begin{cfuncdesc}{PyObject*}{PyUnicode_FromObject}{PyObject *obj}
933 Shortcut for \code{PyUnicode_FromEncodedObject(obj, NULL, "strict")}
934 which is used throughout the interpreter whenever coercion to
935 Unicode is needed.
936\end{cfuncdesc}
937
938% --- wchar_t support for platforms which support it ---------------------
939
940If the platform supports \ctype{wchar_t} and provides a header file
941wchar.h, Python can interface directly to this type using the
942following functions. Support is optimized if Python's own
943\ctype{Py_UNICODE} type is identical to the system's \ctype{wchar_t}.
944
945\begin{cfuncdesc}{PyObject*}{PyUnicode_FromWideChar}{const wchar_t *w,
946 int size}
Thomas Heller541703b2002-04-29 17:28:43 +0000947 Create a Unicode object from the \ctype{wchar_t} buffer \var{w} of
Fred Drake3adf79e2001-10-12 19:01:43 +0000948 the given size. Returns \NULL{} on failure.
949\end{cfuncdesc}
950
951\begin{cfuncdesc}{int}{PyUnicode_AsWideChar}{PyUnicodeObject *unicode,
952 wchar_t *w,
953 int size}
Thomas Heller541703b2002-04-29 17:28:43 +0000954 Copies the Unicode object contents into the \ctype{wchar_t} buffer
955 \var{w}. At most \var{size} \ctype{wchar_t} characters are copied.
956 Returns the number of \ctype{wchar_t} characters copied or -1 in
Fred Drake3adf79e2001-10-12 19:01:43 +0000957 case of an error.
958\end{cfuncdesc}
959
960
961\subsubsection{Built-in Codecs \label{builtinCodecs}}
962
963Python provides a set of builtin codecs which are written in C
964for speed. All of these codecs are directly usable via the
965following functions.
966
967Many of the following APIs take two arguments encoding and
968errors. These parameters encoding and errors have the same semantics
969as the ones of the builtin unicode() Unicode object constructor.
970
971Setting encoding to \NULL{} causes the default encoding to be used
972which is \ASCII. The file system calls should use
973\cdata{Py_FileSystemDefaultEncoding} as the encoding for file
974names. This variable should be treated as read-only: On some systems,
975it will be a pointer to a static string, on others, it will change at
Raymond Hettingercb2da432003-10-12 18:24:34 +0000976run-time (such as when the application invokes setlocale).
Fred Drake3adf79e2001-10-12 19:01:43 +0000977
978Error handling is set by errors which may also be set to \NULL{}
979meaning to use the default handling defined for the codec. Default
980error handling for all builtin codecs is ``strict''
981(\exception{ValueError} is raised).
982
983The codecs all use a similar interface. Only deviation from the
984following generic ones are documented for simplicity.
985
986% --- Generic Codecs -----------------------------------------------------
987
988These are the generic codec APIs:
989
990\begin{cfuncdesc}{PyObject*}{PyUnicode_Decode}{const char *s,
991 int size,
992 const char *encoding,
993 const char *errors}
994 Create a Unicode object by decoding \var{size} bytes of the encoded
995 string \var{s}. \var{encoding} and \var{errors} have the same
996 meaning as the parameters of the same name in the
997 \function{unicode()} builtin function. The codec to be used is
998 looked up using the Python codec registry. Returns \NULL{} if an
999 exception was raised by the codec.
1000\end{cfuncdesc}
1001
1002\begin{cfuncdesc}{PyObject*}{PyUnicode_Encode}{const Py_UNICODE *s,
1003 int size,
1004 const char *encoding,
1005 const char *errors}
1006 Encodes the \ctype{Py_UNICODE} buffer of the given size and returns
1007 a Python string object. \var{encoding} and \var{errors} have the
1008 same meaning as the parameters of the same name in the Unicode
1009 \method{encode()} method. The codec to be used is looked up using
1010 the Python codec registry. Returns \NULL{} if an exception was
1011 raised by the codec.
1012\end{cfuncdesc}
1013
1014\begin{cfuncdesc}{PyObject*}{PyUnicode_AsEncodedString}{PyObject *unicode,
1015 const char *encoding,
1016 const char *errors}
1017 Encodes a Unicode object and returns the result as Python string
1018 object. \var{encoding} and \var{errors} have the same meaning as the
1019 parameters of the same name in the Unicode \method{encode()} method.
1020 The codec to be used is looked up using the Python codec registry.
1021 Returns \NULL{} if an exception was raised by the codec.
1022\end{cfuncdesc}
1023
1024% --- UTF-8 Codecs -------------------------------------------------------
1025
1026These are the UTF-8 codec APIs:
1027
1028\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF8}{const char *s,
1029 int size,
1030 const char *errors}
1031 Creates a Unicode object by decoding \var{size} bytes of the UTF-8
1032 encoded string \var{s}. Returns \NULL{} if an exception was raised
1033 by the codec.
1034\end{cfuncdesc}
1035
1036\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUTF8}{const Py_UNICODE *s,
1037 int size,
1038 const char *errors}
1039 Encodes the \ctype{Py_UNICODE} buffer of the given size using UTF-8
1040 and returns a Python string object. Returns \NULL{} if an exception
1041 was raised by the codec.
1042\end{cfuncdesc}
1043
1044\begin{cfuncdesc}{PyObject*}{PyUnicode_AsUTF8String}{PyObject *unicode}
1045 Encodes a Unicode objects using UTF-8 and returns the result as
1046 Python string object. Error handling is ``strict''. Returns
1047 \NULL{} if an exception was raised by the codec.
1048\end{cfuncdesc}
1049
1050% --- UTF-16 Codecs ------------------------------------------------------ */
1051
1052These are the UTF-16 codec APIs:
1053
1054\begin{cfuncdesc}{PyObject*}{PyUnicode_DecodeUTF16}{const char *s,
1055 int size,
1056 const char *errors,
1057 int *byteorder}
1058 Decodes \var{length} bytes from a UTF-16 encoded buffer string and
1059 returns the corresponding Unicode object. \var{errors} (if
1060 non-\NULL) defines the error handling. It defaults to ``strict''.
1061
1062 If \var{byteorder} is non-\NULL, the decoder starts decoding using
1063 the given byte order:
1064
1065\begin{verbatim}
1066 *byteorder == -1: little endian
1067 *byteorder == 0: native order
1068 *byteorder == 1: big endian
1069\end{verbatim}
1070
1071 and then switches according to all byte order marks (BOM) it finds
1072 in the input data. BOMs are not copied into the resulting Unicode
1073 string. After completion, \var{*byteorder} is set to the current
1074 byte order at the end of input data.
1075
1076 If \var{byteorder} is \NULL, the codec starts in native order mode.
1077
1078 Returns \NULL{} if an exception was raised by the codec.
1079\end{cfuncdesc}
1080
1081\begin{cfuncdesc}{PyObject*}{PyUnicode_EncodeUTF16}{const Py_UNICODE *s,
1082 int size,
1083 const char *errors,
1084 int byteorder}
1085 Returns a Python string object holding the UTF-16 encoded value of
1086 the Unicode data in \var{s}. If \var{byteorder} is not \code{0},
1087 output is written according to the following byte order:
1088
1089\begin{verbatim}
1090 byteorder == -1: little endian
1091 byteorder == 0: native byte order (writes a BOM mark)
1092 byteorder == 1: big endian
1093\end{verbatim}
1094
1095 If byteorder is \code{0}, the output string will always start with
1096 the Unicode BOM mark (U+FEFF). In the other two modes, no BOM mark
1097 is prepended.
1098
1099 Note that \ctype{Py_UNICODE} data is being interpreted as UTF-16
1100 reduced to UCS-2. This trick makes it possible to add full UTF-16
1101 capabilities at a later point without comprimising the APIs.
1102
1103 Returns \NULL{} if an exception was raised by the codec.
1104\end{cfuncdesc}
1105
1106\begin{cfuncdesc}{PyObject*}{PyUnicode_AsUTF16String}{PyObject *unicode}
1107 Returns a Python string using the UTF-16 encoding in native byte
1108 order. The string always starts with a BOM mark. Error handling is
1109 ``strict''. Returns \NULL{} if an exception was raised by the
1110 codec.
1111\end{cfuncdesc}
1112
1113% --- Unicode-Escape Codecs ----------------------------------------------
1114
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
Raymond Hettingercb2da432003-10-12 18:24:34 +00001587\begin{cfuncdesc}{PyObject*}{PyTuple_Pack}{int n, \moreargs}
1588 Return a new tuple object of size \var{n}, or \NULL{} on failure.
1589 The tuple values are initialized to the subsequent \var{n} C arguments
1590 pointing to Python objects. \samp{PyTuple_Pack(2, \var{a}, \var{b})}
1591 is equivalent to \samp{Py_BuildValue("(OO)", \var{a}, \var{b})}.
1592 \versionadded{2.4}
1593\end{cfuncdesc}
1594
Fred Drake3adf79e2001-10-12 19:01:43 +00001595\begin{cfuncdesc}{int}{PyTuple_Size}{PyObject *p}
1596 Takes a pointer to a tuple object, and returns the size of that
1597 tuple.
1598\end{cfuncdesc}
1599
1600\begin{cfuncdesc}{int}{PyTuple_GET_SIZE}{PyObject *p}
1601 Return the size of the tuple \var{p}, which must be non-\NULL{} and
1602 point to a tuple; no error checking is performed.
1603\end{cfuncdesc}
1604
1605\begin{cfuncdesc}{PyObject*}{PyTuple_GetItem}{PyObject *p, int pos}
1606 Returns the object at position \var{pos} in the tuple pointed to by
1607 \var{p}. If \var{pos} is out of bounds, returns \NULL{} and sets an
1608 \exception{IndexError} exception.
1609\end{cfuncdesc}
1610
1611\begin{cfuncdesc}{PyObject*}{PyTuple_GET_ITEM}{PyObject *p, int pos}
1612 Like \cfunction{PyTuple_GetItem()}, but does no checking of its
1613 arguments.
1614\end{cfuncdesc}
1615
1616\begin{cfuncdesc}{PyObject*}{PyTuple_GetSlice}{PyObject *p,
1617 int low, int high}
1618 Takes a slice of the tuple pointed to by \var{p} from \var{low} to
1619 \var{high} and returns it as a new tuple.
1620\end{cfuncdesc}
1621
1622\begin{cfuncdesc}{int}{PyTuple_SetItem}{PyObject *p,
1623 int pos, PyObject *o}
1624 Inserts a reference to object \var{o} at position \var{pos} of the
1625 tuple pointed to by \var{p}. It returns \code{0} on success.
1626 \note{This function ``steals'' a reference to \var{o}.}
1627\end{cfuncdesc}
1628
1629\begin{cfuncdesc}{void}{PyTuple_SET_ITEM}{PyObject *p,
1630 int pos, PyObject *o}
1631 Like \cfunction{PyTuple_SetItem()}, but does no error checking, and
1632 should \emph{only} be used to fill in brand new tuples. \note{This
1633 function ``steals'' a reference to \var{o}.}
1634\end{cfuncdesc}
1635
1636\begin{cfuncdesc}{int}{_PyTuple_Resize}{PyObject **p, int newsize}
1637 Can be used to resize a tuple. \var{newsize} will be the new length
1638 of the tuple. Because tuples are \emph{supposed} to be immutable,
1639 this should only be used if there is only one reference to the
1640 object. Do \emph{not} use this if the tuple may already be known to
1641 some other part of the code. The tuple will always grow or shrink
1642 at the end. Think of this as destroying the old tuple and creating
1643 a new one, only more efficiently. Returns \code{0} on success.
1644 Client code should never assume that the resulting value of
1645 \code{*\var{p}} will be the same as before calling this function.
1646 If the object referenced by \code{*\var{p}} is replaced, the
1647 original \code{*\var{p}} is destroyed. On failure, returns
1648 \code{-1} and sets \code{*\var{p}} to \NULL, and raises
1649 \exception{MemoryError} or
1650 \exception{SystemError}.
Tim Petersf582b822001-12-11 18:51:08 +00001651 \versionchanged[Removed unused third parameter, \var{last_is_sticky}]{2.2}
Fred Drake3adf79e2001-10-12 19:01:43 +00001652\end{cfuncdesc}
1653
1654
1655\subsection{List Objects \label{listObjects}}
1656
1657\obindex{list}
1658\begin{ctypedesc}{PyListObject}
1659 This subtype of \ctype{PyObject} represents a Python list object.
1660\end{ctypedesc}
1661
1662\begin{cvardesc}{PyTypeObject}{PyList_Type}
1663 This instance of \ctype{PyTypeObject} represents the Python list
1664 type. This is the same object as \code{types.ListType}.
1665 \withsubitem{(in module types)}{\ttindex{ListType}}
1666\end{cvardesc}
1667
1668\begin{cfuncdesc}{int}{PyList_Check}{PyObject *p}
1669 Returns true if its argument is a \ctype{PyListObject}.
1670\end{cfuncdesc}
1671
1672\begin{cfuncdesc}{PyObject*}{PyList_New}{int len}
1673 Returns a new list of length \var{len} on success, or \NULL{} on
1674 failure.
1675\end{cfuncdesc}
1676
1677\begin{cfuncdesc}{int}{PyList_Size}{PyObject *list}
1678 Returns the length of the list object in \var{list}; this is
1679 equivalent to \samp{len(\var{list})} on a list object.
1680 \bifuncindex{len}
1681\end{cfuncdesc}
1682
1683\begin{cfuncdesc}{int}{PyList_GET_SIZE}{PyObject *list}
1684 Macro form of \cfunction{PyList_Size()} without error checking.
1685\end{cfuncdesc}
1686
1687\begin{cfuncdesc}{PyObject*}{PyList_GetItem}{PyObject *list, int index}
1688 Returns the object at position \var{pos} in the list pointed to by
1689 \var{p}. If \var{pos} is out of bounds, returns \NULL{} and sets an
1690 \exception{IndexError} exception.
1691\end{cfuncdesc}
1692
1693\begin{cfuncdesc}{PyObject*}{PyList_GET_ITEM}{PyObject *list, int i}
1694 Macro form of \cfunction{PyList_GetItem()} without error checking.
1695\end{cfuncdesc}
1696
1697\begin{cfuncdesc}{int}{PyList_SetItem}{PyObject *list, int index,
1698 PyObject *item}
1699 Sets the item at index \var{index} in list to \var{item}. Returns
1700 \code{0} on success or \code{-1} on failure. \note{This function
1701 ``steals'' a reference to \var{item} and discards a reference to an
1702 item already in the list at the affected position.}
1703\end{cfuncdesc}
1704
1705\begin{cfuncdesc}{void}{PyList_SET_ITEM}{PyObject *list, int i,
1706 PyObject *o}
1707 Macro form of \cfunction{PyList_SetItem()} without error checking.
1708 This is normally only used to fill in new lists where there is no
1709 previous content.
1710 \note{This function ``steals'' a reference to \var{item}, and,
1711 unlike \cfunction{PyList_SetItem()}, does \emph{not} discard a
1712 reference to any item that it being replaced; any reference in
1713 \var{list} at position \var{i} will be leaked.}
1714\end{cfuncdesc}
1715
1716\begin{cfuncdesc}{int}{PyList_Insert}{PyObject *list, int index,
1717 PyObject *item}
1718 Inserts the item \var{item} into list \var{list} in front of index
1719 \var{index}. Returns \code{0} if successful; returns \code{-1} and
1720 raises an exception if unsuccessful. Analogous to
1721 \code{\var{list}.insert(\var{index}, \var{item})}.
1722\end{cfuncdesc}
1723
1724\begin{cfuncdesc}{int}{PyList_Append}{PyObject *list, PyObject *item}
1725 Appends the object \var{item} at the end of list \var{list}.
1726 Returns \code{0} if successful; returns \code{-1} and sets an
1727 exception if unsuccessful. Analogous to
1728 \code{\var{list}.append(\var{item})}.
1729\end{cfuncdesc}
1730
1731\begin{cfuncdesc}{PyObject*}{PyList_GetSlice}{PyObject *list,
1732 int low, int high}
1733 Returns a list of the objects in \var{list} containing the objects
1734 \emph{between} \var{low} and \var{high}. Returns \NULL{} and sets
1735 an exception if unsuccessful.
1736 Analogous to \code{\var{list}[\var{low}:\var{high}]}.
1737\end{cfuncdesc}
1738
1739\begin{cfuncdesc}{int}{PyList_SetSlice}{PyObject *list,
1740 int low, int high,
1741 PyObject *itemlist}
1742 Sets the slice of \var{list} between \var{low} and \var{high} to the
1743 contents of \var{itemlist}. Analogous to
1744 \code{\var{list}[\var{low}:\var{high}] = \var{itemlist}}. Returns
1745 \code{0} on success, \code{-1} on failure.
1746\end{cfuncdesc}
1747
1748\begin{cfuncdesc}{int}{PyList_Sort}{PyObject *list}
1749 Sorts the items of \var{list} in place. Returns \code{0} on
1750 success, \code{-1} on failure. This is equivalent to
1751 \samp{\var{list}.sort()}.
1752\end{cfuncdesc}
1753
1754\begin{cfuncdesc}{int}{PyList_Reverse}{PyObject *list}
1755 Reverses the items of \var{list} in place. Returns \code{0} on
1756 success, \code{-1} on failure. This is the equivalent of
1757 \samp{\var{list}.reverse()}.
1758\end{cfuncdesc}
1759
1760\begin{cfuncdesc}{PyObject*}{PyList_AsTuple}{PyObject *list}
1761 Returns a new tuple object containing the contents of \var{list};
1762 equivalent to \samp{tuple(\var{list})}.\bifuncindex{tuple}
1763\end{cfuncdesc}
1764
1765
1766\section{Mapping Objects \label{mapObjects}}
1767
1768\obindex{mapping}
1769
1770
1771\subsection{Dictionary Objects \label{dictObjects}}
1772
1773\obindex{dictionary}
1774\begin{ctypedesc}{PyDictObject}
1775 This subtype of \ctype{PyObject} represents a Python dictionary
1776 object.
1777\end{ctypedesc}
1778
1779\begin{cvardesc}{PyTypeObject}{PyDict_Type}
1780 This instance of \ctype{PyTypeObject} represents the Python
1781 dictionary type. This is exposed to Python programs as
1782 \code{types.DictType} and \code{types.DictionaryType}.
1783 \withsubitem{(in module types)}{\ttindex{DictType}\ttindex{DictionaryType}}
1784\end{cvardesc}
1785
1786\begin{cfuncdesc}{int}{PyDict_Check}{PyObject *p}
1787 Returns true if its argument is a \ctype{PyDictObject}.
1788\end{cfuncdesc}
1789
1790\begin{cfuncdesc}{PyObject*}{PyDict_New}{}
1791 Returns a new empty dictionary, or \NULL{} on failure.
1792\end{cfuncdesc}
1793
1794\begin{cfuncdesc}{PyObject*}{PyDictProxy_New}{PyObject *dict}
1795 Return a proxy object for a mapping which enforces read-only
1796 behavior. This is normally used to create a proxy to prevent
1797 modification of the dictionary for non-dynamic class types.
1798 \versionadded{2.2}
1799\end{cfuncdesc}
1800
1801\begin{cfuncdesc}{void}{PyDict_Clear}{PyObject *p}
1802 Empties an existing dictionary of all key-value pairs.
1803\end{cfuncdesc}
1804
1805\begin{cfuncdesc}{PyObject*}{PyDict_Copy}{PyObject *p}
1806 Returns a new dictionary that contains the same key-value pairs as
1807 \var{p}.
1808 \versionadded{1.6}
1809\end{cfuncdesc}
1810
1811\begin{cfuncdesc}{int}{PyDict_SetItem}{PyObject *p, PyObject *key,
1812 PyObject *val}
1813 Inserts \var{value} into the dictionary \var{p} with a key of
1814 \var{key}. \var{key} must be hashable; if it isn't,
1815 \exception{TypeError} will be raised.
1816 Returns \code{0} on success or \code{-1} on failure.
1817\end{cfuncdesc}
1818
1819\begin{cfuncdesc}{int}{PyDict_SetItemString}{PyObject *p,
1820 char *key,
1821 PyObject *val}
1822 Inserts \var{value} into the dictionary \var{p} using \var{key} as a
1823 key. \var{key} should be a \ctype{char*}. The key object is created
1824 using \code{PyString_FromString(\var{key})}. Returns \code{0} on
1825 success or \code{-1} on failure.
1826 \ttindex{PyString_FromString()}
1827\end{cfuncdesc}
1828
1829\begin{cfuncdesc}{int}{PyDict_DelItem}{PyObject *p, PyObject *key}
1830 Removes the entry in dictionary \var{p} with key \var{key}.
1831 \var{key} must be hashable; if it isn't, \exception{TypeError} is
Skip Montanaroa23bc422002-01-23 08:18:30 +00001832 raised. Returns \code{0} on success or \code{-1} on failure.
Fred Drake3adf79e2001-10-12 19:01:43 +00001833\end{cfuncdesc}
1834
1835\begin{cfuncdesc}{int}{PyDict_DelItemString}{PyObject *p, char *key}
1836 Removes the entry in dictionary \var{p} which has a key specified by
1837 the string \var{key}. Returns \code{0} on success or \code{-1} on
1838 failure.
1839\end{cfuncdesc}
1840
1841\begin{cfuncdesc}{PyObject*}{PyDict_GetItem}{PyObject *p, PyObject *key}
1842 Returns the object from dictionary \var{p} which has a key
1843 \var{key}. Returns \NULL{} if the key \var{key} is not present, but
1844 \emph{without} setting an exception.
1845\end{cfuncdesc}
1846
1847\begin{cfuncdesc}{PyObject*}{PyDict_GetItemString}{PyObject *p, char *key}
1848 This is the same as \cfunction{PyDict_GetItem()}, but \var{key} is
1849 specified as a \ctype{char*}, rather than a \ctype{PyObject*}.
1850\end{cfuncdesc}
1851
1852\begin{cfuncdesc}{PyObject*}{PyDict_Items}{PyObject *p}
1853 Returns a \ctype{PyListObject} containing all the items from the
1854 dictionary, as in the dictinoary method \method{items()} (see the
1855 \citetitle[../lib/lib.html]{Python Library Reference}).
1856\end{cfuncdesc}
1857
1858\begin{cfuncdesc}{PyObject*}{PyDict_Keys}{PyObject *p}
1859 Returns a \ctype{PyListObject} containing all the keys from the
1860 dictionary, as in the dictionary method \method{keys()} (see the
1861 \citetitle[../lib/lib.html]{Python Library Reference}).
1862\end{cfuncdesc}
1863
1864\begin{cfuncdesc}{PyObject*}{PyDict_Values}{PyObject *p}
1865 Returns a \ctype{PyListObject} containing all the values from the
1866 dictionary \var{p}, as in the dictionary method \method{values()}
1867 (see the \citetitle[../lib/lib.html]{Python Library Reference}).
1868\end{cfuncdesc}
1869
1870\begin{cfuncdesc}{int}{PyDict_Size}{PyObject *p}
1871 Returns the number of items in the dictionary. This is equivalent
1872 to \samp{len(\var{p})} on a dictionary.\bifuncindex{len}
1873\end{cfuncdesc}
1874
1875\begin{cfuncdesc}{int}{PyDict_Next}{PyObject *p, int *ppos,
1876 PyObject **pkey, PyObject **pvalue}
1877 Iterate over all key-value pairs in the dictionary \var{p}. The
1878 \ctype{int} referred to by \var{ppos} must be initialized to
1879 \code{0} prior to the first call to this function to start the
1880 iteration; the function returns true for each pair in the
1881 dictionary, and false once all pairs have been reported. The
1882 parameters \var{pkey} and \var{pvalue} should either point to
1883 \ctype{PyObject*} variables that will be filled in with each key and
Skip Montanaroea3ceaa2002-01-23 10:54:41 +00001884 value, respectively, or may be \NULL. Any references returned through
1885 them are borrowed.
Fred Drake3adf79e2001-10-12 19:01:43 +00001886
1887 For example:
1888
1889\begin{verbatim}
1890PyObject *key, *value;
1891int pos = 0;
1892
1893while (PyDict_Next(self->dict, &pos, &key, &value)) {
1894 /* do something interesting with the values... */
1895 ...
1896}
1897\end{verbatim}
1898
1899 The dictionary \var{p} should not be mutated during iteration. It
1900 is safe (since Python 2.1) to modify the values of the keys as you
1901 iterate over the dictionary, but only so long as the set of keys
1902 does not change. For example:
1903
1904\begin{verbatim}
1905PyObject *key, *value;
1906int pos = 0;
1907
1908while (PyDict_Next(self->dict, &pos, &key, &value)) {
1909 int i = PyInt_AS_LONG(value) + 1;
1910 PyObject *o = PyInt_FromLong(i);
1911 if (o == NULL)
1912 return -1;
1913 if (PyDict_SetItem(self->dict, key, o) < 0) {
1914 Py_DECREF(o);
1915 return -1;
1916 }
1917 Py_DECREF(o);
1918}
1919\end{verbatim}
1920\end{cfuncdesc}
1921
1922\begin{cfuncdesc}{int}{PyDict_Merge}{PyObject *a, PyObject *b, int override}
Tim Petersf582b822001-12-11 18:51:08 +00001923 Iterate over mapping object \var{b} adding key-value pairs to dictionary
1924 \var{a}.
1925 \var{b} may be a dictionary, or any object supporting
1926 \function{PyMapping_Keys()} and \function{PyObject_GetItem()}.
1927 If \var{override} is true, existing pairs in \var{a} will
Fred Drake3adf79e2001-10-12 19:01:43 +00001928 be replaced if a matching key is found in \var{b}, otherwise pairs
1929 will only be added if there is not a matching key in \var{a}.
Tim Petersf582b822001-12-11 18:51:08 +00001930 Return \code{0} on success or \code{-1} if an exception was
Fred Drake3adf79e2001-10-12 19:01:43 +00001931 raised.
1932\versionadded{2.2}
1933\end{cfuncdesc}
1934
1935\begin{cfuncdesc}{int}{PyDict_Update}{PyObject *a, PyObject *b}
1936 This is the same as \code{PyDict_Merge(\var{a}, \var{b}, 1)} in C,
Tim Petersf582b822001-12-11 18:51:08 +00001937 or \code{\var{a}.update(\var{b})} in Python. Return \code{0} on
Fred Drake3adf79e2001-10-12 19:01:43 +00001938 success or \code{-1} if an exception was raised.
1939 \versionadded{2.2}
1940\end{cfuncdesc}
1941
Tim Petersf582b822001-12-11 18:51:08 +00001942\begin{cfuncdesc}{int}{PyDict_MergeFromSeq2}{PyObject *a, PyObject *seq2,
1943 int override}
1944 Update or merge into dictionary \var{a}, from the key-value pairs in
1945 \var{seq2}. \var{seq2} must be an iterable object producing
1946 iterable objects of length 2, viewed as key-value pairs. In case of
1947 duplicate keys, the last wins if \var{override} is true, else the
1948 first wins.
1949 Return \code{0} on success or \code{-1} if an exception
1950 was raised.
1951 Equivalent Python (except for the return value):
1952
1953\begin{verbatim}
1954def PyDict_MergeFromSeq2(a, seq2, override):
1955 for key, value in seq2:
1956 if override or key not in a:
1957 a[key] = value
1958\end{verbatim}
1959
1960 \versionadded{2.2}
1961\end{cfuncdesc}
Fred Drake3adf79e2001-10-12 19:01:43 +00001962
Fred Drake54e62942001-12-11 19:40:16 +00001963
Fred Drake3adf79e2001-10-12 19:01:43 +00001964\section{Other Objects \label{otherObjects}}
1965
1966\subsection{File Objects \label{fileObjects}}
1967
1968\obindex{file}
1969Python's built-in file objects are implemented entirely on the
1970\ctype{FILE*} support from the C standard library. This is an
1971implementation detail and may change in future releases of Python.
1972
1973\begin{ctypedesc}{PyFileObject}
1974 This subtype of \ctype{PyObject} represents a Python file object.
1975\end{ctypedesc}
1976
1977\begin{cvardesc}{PyTypeObject}{PyFile_Type}
1978 This instance of \ctype{PyTypeObject} represents the Python file
1979 type. This is exposed to Python programs as \code{types.FileType}.
1980 \withsubitem{(in module types)}{\ttindex{FileType}}
1981\end{cvardesc}
1982
1983\begin{cfuncdesc}{int}{PyFile_Check}{PyObject *p}
1984 Returns true if its argument is a \ctype{PyFileObject} or a subtype
1985 of \ctype{PyFileObject}.
1986 \versionchanged[Allowed subtypes to be accepted]{2.2}
1987\end{cfuncdesc}
1988
1989\begin{cfuncdesc}{int}{PyFile_CheckExact}{PyObject *p}
1990 Returns true if its argument is a \ctype{PyFileObject}, but not a
1991 subtype of \ctype{PyFileObject}.
1992 \versionadded{2.2}
1993\end{cfuncdesc}
1994
1995\begin{cfuncdesc}{PyObject*}{PyFile_FromString}{char *filename, char *mode}
1996 On success, returns a new file object that is opened on the file
1997 given by \var{filename}, with a file mode given by \var{mode}, where
1998 \var{mode} has the same semantics as the standard C routine
1999 \cfunction{fopen()}\ttindex{fopen()}. On failure, returns \NULL.
2000\end{cfuncdesc}
2001
2002\begin{cfuncdesc}{PyObject*}{PyFile_FromFile}{FILE *fp,
2003 char *name, char *mode,
2004 int (*close)(FILE*)}
2005 Creates a new \ctype{PyFileObject} from the already-open standard C
2006 file pointer, \var{fp}. The function \var{close} will be called
2007 when the file should be closed. Returns \NULL{} on failure.
2008\end{cfuncdesc}
2009
2010\begin{cfuncdesc}{FILE*}{PyFile_AsFile}{PyFileObject *p}
2011 Returns the file object associated with \var{p} as a \ctype{FILE*}.
2012\end{cfuncdesc}
2013
2014\begin{cfuncdesc}{PyObject*}{PyFile_GetLine}{PyObject *p, int n}
2015 Equivalent to \code{\var{p}.readline(\optional{\var{n}})}, this
2016 function reads one line from the object \var{p}. \var{p} may be a
2017 file object or any object with a \method{readline()} method. If
2018 \var{n} is \code{0}, exactly one line is read, regardless of the
2019 length of the line. If \var{n} is greater than \code{0}, no more
2020 than \var{n} bytes will be read from the file; a partial line can be
2021 returned. In both cases, an empty string is returned if the end of
2022 the file is reached immediately. If \var{n} is less than \code{0},
2023 however, one line is read regardless of length, but
2024 \exception{EOFError} is raised if the end of the file is reached
2025 immediately.
2026 \withsubitem{(built-in exception)}{\ttindex{EOFError}}
2027\end{cfuncdesc}
2028
2029\begin{cfuncdesc}{PyObject*}{PyFile_Name}{PyObject *p}
2030 Returns the name of the file specified by \var{p} as a string
2031 object.
2032\end{cfuncdesc}
2033
2034\begin{cfuncdesc}{void}{PyFile_SetBufSize}{PyFileObject *p, int n}
2035 Available on systems with \cfunction{setvbuf()}\ttindex{setvbuf()}
2036 only. This should only be called immediately after file object
2037 creation.
2038\end{cfuncdesc}
2039
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00002040\begin{cfuncdesc}{int}{PyFile_Encoding}{PyFileObject *p, char *enc}
2041 Set the file's encoding for Unicode output to \var{enc}. Return
2042 1 on success and 0 on failure.
2043 \versionadded{2.3}
2044\end{cfuncdesc}
2045
Fred Drake3adf79e2001-10-12 19:01:43 +00002046\begin{cfuncdesc}{int}{PyFile_SoftSpace}{PyObject *p, int newflag}
2047 This function exists for internal use by the interpreter. Sets the
2048 \member{softspace} attribute of \var{p} to \var{newflag} and
2049 \withsubitem{(file attribute)}{\ttindex{softspace}}returns the
2050 previous value. \var{p} does not have to be a file object for this
2051 function to work properly; any object is supported (thought its only
2052 interesting if the \member{softspace} attribute can be set). This
2053 function clears any errors, and will return \code{0} as the previous
2054 value if the attribute either does not exist or if there were errors
2055 in retrieving it. There is no way to detect errors from this
2056 function, but doing so should not be needed.
2057\end{cfuncdesc}
2058
2059\begin{cfuncdesc}{int}{PyFile_WriteObject}{PyObject *obj, PyFileObject *p,
2060 int flags}
2061 Writes object \var{obj} to file object \var{p}. The only supported
2062 flag for \var{flags} is
2063 \constant{Py_PRINT_RAW}\ttindex{Py_PRINT_RAW}; if given, the
2064 \function{str()} of the object is written instead of the
2065 \function{repr()}. Returns \code{0} on success or \code{-1} on
2066 failure; the appropriate exception will be set.
2067\end{cfuncdesc}
2068
Fred Drake454af892001-11-29 22:42:59 +00002069\begin{cfuncdesc}{int}{PyFile_WriteString}{const char *s, PyFileObject *p}
Fred Drake3adf79e2001-10-12 19:01:43 +00002070 Writes string \var{s} to file object \var{p}. Returns \code{0} on
2071 success or \code{-1} on failure; the appropriate exception will be
2072 set.
2073\end{cfuncdesc}
2074
2075
2076\subsection{Instance Objects \label{instanceObjects}}
2077
2078\obindex{instance}
2079There are very few functions specific to instance objects.
2080
2081\begin{cvardesc}{PyTypeObject}{PyInstance_Type}
2082 Type object for class instances.
2083\end{cvardesc}
2084
2085\begin{cfuncdesc}{int}{PyInstance_Check}{PyObject *obj}
2086 Returns true if \var{obj} is an instance.
2087\end{cfuncdesc}
2088
2089\begin{cfuncdesc}{PyObject*}{PyInstance_New}{PyObject *class,
2090 PyObject *arg,
2091 PyObject *kw}
2092 Create a new instance of a specific class. The parameters \var{arg}
2093 and \var{kw} are used as the positional and keyword parameters to
2094 the object's constructor.
2095\end{cfuncdesc}
2096
2097\begin{cfuncdesc}{PyObject*}{PyInstance_NewRaw}{PyObject *class,
2098 PyObject *dict}
2099 Create a new instance of a specific class without calling it's
2100 constructor. \var{class} is the class of new object. The
2101 \var{dict} parameter will be used as the object's \member{__dict__};
2102 if \NULL, a new dictionary will be created for the instance.
2103\end{cfuncdesc}
2104
2105
2106\subsection{Method Objects \label{method-objects}}
2107
2108\obindex{method}
2109There are some useful functions that are useful for working with
2110method objects.
2111
2112\begin{cvardesc}{PyTypeObject}{PyMethod_Type}
2113 This instance of \ctype{PyTypeObject} represents the Python method
2114 type. This is exposed to Python programs as \code{types.MethodType}.
2115 \withsubitem{(in module types)}{\ttindex{MethodType}}
2116\end{cvardesc}
2117
2118\begin{cfuncdesc}{int}{PyMethod_Check}{PyObject *o}
2119 Return true if \var{o} is a method object (has type
2120 \cdata{PyMethod_Type}). The parameter must not be \NULL.
2121\end{cfuncdesc}
2122
2123\begin{cfuncdesc}{PyObject*}{PyMethod_New}{PyObject *func.
2124 PyObject *self, PyObject *class}
2125 Return a new method object, with \var{func} being any callable
2126 object; this is the function that will be called when the method is
2127 called. If this method should be bound to an instance, \var{self}
2128 should be the instance and \var{class} should be the class of
2129 \var{self}, otherwise \var{self} should be \NULL{} and \var{class}
2130 should be the class which provides the unbound method..
2131\end{cfuncdesc}
2132
2133\begin{cfuncdesc}{PyObject*}{PyMethod_Class}{PyObject *meth}
2134 Return the class object from which the method \var{meth} was
2135 created; if this was created from an instance, it will be the class
2136 of the instance.
2137\end{cfuncdesc}
2138
2139\begin{cfuncdesc}{PyObject*}{PyMethod_GET_CLASS}{PyObject *meth}
2140 Macro version of \cfunction{PyMethod_Class()} which avoids error
2141 checking.
2142\end{cfuncdesc}
2143
2144\begin{cfuncdesc}{PyObject*}{PyMethod_Function}{PyObject *meth}
2145 Return the function object associated with the method \var{meth}.
2146\end{cfuncdesc}
2147
2148\begin{cfuncdesc}{PyObject*}{PyMethod_GET_FUNCTION}{PyObject *meth}
2149 Macro version of \cfunction{PyMethod_Function()} which avoids error
2150 checking.
2151\end{cfuncdesc}
2152
2153\begin{cfuncdesc}{PyObject*}{PyMethod_Self}{PyObject *meth}
2154 Return the instance associated with the method \var{meth} if it is
2155 bound, otherwise return \NULL.
2156\end{cfuncdesc}
2157
2158\begin{cfuncdesc}{PyObject*}{PyMethod_GET_SELF}{PyObject *meth}
2159 Macro version of \cfunction{PyMethod_Self()} which avoids error
2160 checking.
2161\end{cfuncdesc}
2162
2163
2164\subsection{Module Objects \label{moduleObjects}}
2165
2166\obindex{module}
2167There are only a few functions special to module objects.
2168
2169\begin{cvardesc}{PyTypeObject}{PyModule_Type}
2170 This instance of \ctype{PyTypeObject} represents the Python module
2171 type. This is exposed to Python programs as
2172 \code{types.ModuleType}.
2173 \withsubitem{(in module types)}{\ttindex{ModuleType}}
2174\end{cvardesc}
2175
2176\begin{cfuncdesc}{int}{PyModule_Check}{PyObject *p}
2177 Returns true if \var{p} is a module object, or a subtype of a module
2178 object.
2179 \versionchanged[Allowed subtypes to be accepted]{2.2}
2180\end{cfuncdesc}
2181
2182\begin{cfuncdesc}{int}{PyModule_CheckExact}{PyObject *p}
2183 Returns true if \var{p} is a module object, but not a subtype of
2184 \cdata{PyModule_Type}.
2185 \versionadded{2.2}
2186\end{cfuncdesc}
2187
2188\begin{cfuncdesc}{PyObject*}{PyModule_New}{char *name}
2189 Return a new module object with the \member{__name__} attribute set
2190 to \var{name}. Only the module's \member{__doc__} and
2191 \member{__name__} attributes are filled in; the caller is
2192 responsible for providing a \member{__file__} attribute.
2193 \withsubitem{(module attribute)}{
2194 \ttindex{__name__}\ttindex{__doc__}\ttindex{__file__}}
2195\end{cfuncdesc}
2196
2197\begin{cfuncdesc}{PyObject*}{PyModule_GetDict}{PyObject *module}
2198 Return the dictionary object that implements \var{module}'s
2199 namespace; this object is the same as the \member{__dict__}
2200 attribute of the module object. This function never fails.
2201 \withsubitem{(module attribute)}{\ttindex{__dict__}}
Fred Drakef495ef72002-04-12 19:32:07 +00002202 It is recommended extensions use other \cfunction{PyModule_*()}
2203 and \cfunction{PyObject_*()} functions rather than directly
2204 manipulate a module's \member{__dict__}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002205\end{cfuncdesc}
2206
2207\begin{cfuncdesc}{char*}{PyModule_GetName}{PyObject *module}
2208 Return \var{module}'s \member{__name__} value. If the module does
2209 not provide one, or if it is not a string, \exception{SystemError}
2210 is raised and \NULL{} is returned.
2211 \withsubitem{(module attribute)}{\ttindex{__name__}}
2212 \withsubitem{(built-in exception)}{\ttindex{SystemError}}
2213\end{cfuncdesc}
2214
2215\begin{cfuncdesc}{char*}{PyModule_GetFilename}{PyObject *module}
2216 Return the name of the file from which \var{module} was loaded using
2217 \var{module}'s \member{__file__} attribute. If this is not defined,
2218 or if it is not a string, raise \exception{SystemError} and return
2219 \NULL.
2220 \withsubitem{(module attribute)}{\ttindex{__file__}}
2221 \withsubitem{(built-in exception)}{\ttindex{SystemError}}
2222\end{cfuncdesc}
2223
2224\begin{cfuncdesc}{int}{PyModule_AddObject}{PyObject *module,
2225 char *name, PyObject *value}
2226 Add an object to \var{module} as \var{name}. This is a convenience
2227 function which can be used from the module's initialization
2228 function. This steals a reference to \var{value}. Returns
2229 \code{-1} on error, \code{0} on success.
2230 \versionadded{2.0}
2231\end{cfuncdesc}
2232
2233\begin{cfuncdesc}{int}{PyModule_AddIntConstant}{PyObject *module,
2234 char *name, int value}
2235 Add an integer constant to \var{module} as \var{name}. This
2236 convenience function can be used from the module's initialization
2237 function. Returns \code{-1} on error, \code{0} on success.
2238 \versionadded{2.0}
2239\end{cfuncdesc}
2240
2241\begin{cfuncdesc}{int}{PyModule_AddStringConstant}{PyObject *module,
2242 char *name, char *value}
2243 Add a string constant to \var{module} as \var{name}. This
2244 convenience function can be used from the module's initialization
2245 function. The string \var{value} must be null-terminated. Returns
2246 \code{-1} on error, \code{0} on success.
2247 \versionadded{2.0}
2248\end{cfuncdesc}
2249
2250
2251\subsection{Iterator Objects \label{iterator-objects}}
2252
2253Python provides two general-purpose iterator objects. The first, a
2254sequence iterator, works with an arbitrary sequence supporting the
2255\method{__getitem__()} method. The second works with a callable
2256object and a sentinel value, calling the callable for each item in the
2257sequence, and ending the iteration when the sentinel value is
2258returned.
2259
2260\begin{cvardesc}{PyTypeObject}{PySeqIter_Type}
2261 Type object for iterator objects returned by
2262 \cfunction{PySeqIter_New()} and the one-argument form of the
2263 \function{iter()} built-in function for built-in sequence types.
2264 \versionadded{2.2}
2265\end{cvardesc}
2266
2267\begin{cfuncdesc}{int}{PySeqIter_Check}{op}
2268 Return true if the type of \var{op} is \cdata{PySeqIter_Type}.
2269 \versionadded{2.2}
2270\end{cfuncdesc}
2271
2272\begin{cfuncdesc}{PyObject*}{PySeqIter_New}{PyObject *seq}
2273 Return an iterator that works with a general sequence object,
2274 \var{seq}. The iteration ends when the sequence raises
2275 \exception{IndexError} for the subscripting operation.
2276 \versionadded{2.2}
2277\end{cfuncdesc}
2278
2279\begin{cvardesc}{PyTypeObject}{PyCallIter_Type}
2280 Type object for iterator objects returned by
2281 \cfunction{PyCallIter_New()} and the two-argument form of the
2282 \function{iter()} built-in function.
2283 \versionadded{2.2}
2284\end{cvardesc}
2285
2286\begin{cfuncdesc}{int}{PyCallIter_Check}{op}
2287 Return true if the type of \var{op} is \cdata{PyCallIter_Type}.
2288 \versionadded{2.2}
2289\end{cfuncdesc}
2290
2291\begin{cfuncdesc}{PyObject*}{PyCallIter_New}{PyObject *callable,
2292 PyObject *sentinel}
2293 Return a new iterator. The first parameter, \var{callable}, can be
2294 any Python callable object that can be called with no parameters;
2295 each call to it should return the next item in the iteration. When
2296 \var{callable} returns a value equal to \var{sentinel}, the
2297 iteration will be terminated.
2298 \versionadded{2.2}
2299\end{cfuncdesc}
2300
2301
2302\subsection{Descriptor Objects \label{descriptor-objects}}
2303
Fred Drake54e62942001-12-11 19:40:16 +00002304``Descriptors'' are objects that describe some attribute of an object.
2305They are found in the dictionary of type objects.
2306
Fred Drake3adf79e2001-10-12 19:01:43 +00002307\begin{cvardesc}{PyTypeObject}{PyProperty_Type}
Fred Drake54e62942001-12-11 19:40:16 +00002308 The type object for the built-in descriptor types.
Fred Drake3adf79e2001-10-12 19:01:43 +00002309 \versionadded{2.2}
2310\end{cvardesc}
2311
2312\begin{cfuncdesc}{PyObject*}{PyDescr_NewGetSet}{PyTypeObject *type,
2313 PyGetSetDef *getset}
2314 \versionadded{2.2}
2315\end{cfuncdesc}
2316
2317\begin{cfuncdesc}{PyObject*}{PyDescr_NewMember}{PyTypeObject *type,
2318 PyMemberDef *meth}
2319 \versionadded{2.2}
2320\end{cfuncdesc}
2321
2322\begin{cfuncdesc}{PyObject*}{PyDescr_NewMethod}{PyTypeObject *type,
2323 PyMethodDef *meth}
2324 \versionadded{2.2}
2325\end{cfuncdesc}
2326
2327\begin{cfuncdesc}{PyObject*}{PyDescr_NewWrapper}{PyTypeObject *type,
2328 struct wrapperbase *wrapper,
2329 void *wrapped}
2330 \versionadded{2.2}
2331\end{cfuncdesc}
2332
2333\begin{cfuncdesc}{int}{PyDescr_IsData}{PyObject *descr}
2334 Returns true if the descriptor objects \var{descr} describes a data
2335 attribute, or false if it describes a method. \var{descr} must be a
2336 descriptor object; there is no error checking.
2337 \versionadded{2.2}
2338\end{cfuncdesc}
2339
2340\begin{cfuncdesc}{PyObject*}{PyWrapper_New}{PyObject *, PyObject *}
2341 \versionadded{2.2}
2342\end{cfuncdesc}
2343
2344
2345\subsection{Slice Objects \label{slice-objects}}
2346
2347\begin{cvardesc}{PyTypeObject}{PySlice_Type}
2348 The type object for slice objects. This is the same as
2349 \code{types.SliceType}.
2350 \withsubitem{(in module types)}{\ttindex{SliceType}}
2351\end{cvardesc}
2352
2353\begin{cfuncdesc}{int}{PySlice_Check}{PyObject *ob}
2354 Returns true if \var{ob} is a slice object; \var{ob} must not be
2355 \NULL.
2356\end{cfuncdesc}
2357
2358\begin{cfuncdesc}{PyObject*}{PySlice_New}{PyObject *start, PyObject *stop,
2359 PyObject *step}
2360 Return a new slice object with the given values. The \var{start},
2361 \var{stop}, and \var{step} parameters are used as the values of the
2362 slice object attributes of the same names. Any of the values may be
2363 \NULL, in which case the \code{None} will be used for the
2364 corresponding attribute. Returns \NULL{} if the new object could
2365 not be allocated.
2366\end{cfuncdesc}
2367
2368\begin{cfuncdesc}{int}{PySlice_GetIndices}{PySliceObject *slice, int length,
2369 int *start, int *stop, int *step}
Michael W. Hudson5efaf7e2002-06-11 10:55:12 +00002370Retrieve the start, stop and step indices from the slice object
2371\var{slice}, assuming a sequence of length \var{length}. Treats
2372indices greater than \var{length} as errors.
2373
2374Returns 0 on success and -1 on error with no exception set (unless one
2375of the indices was not \constant{None} and failed to be converted to
2376an integer, in which case -1 is returned with an exception set).
2377
2378You probably do not want to use this function. If you want to use
2379slice objects in versions of Python prior to 2.3, you would probably
2380do well to incorporate the source of \cfunction{PySlice_GetIndicesEx},
2381suitably renamed, in the source of your extension.
2382\end{cfuncdesc}
2383
2384\begin{cfuncdesc}{int}{PySlice_GetIndicesEx}{PySliceObject *slice, int length,
2385 int *start, int *stop, int *step,
2386 int *slicelength}
2387Usable replacement for \cfunction{PySlice_GetIndices}. Retrieve the
2388start, stop, and step indices from the slice object \var{slice}
2389assuming a sequence of length \var{length}, and store the length of
2390the slice in \var{slicelength}. Out of bounds indices are clipped in
2391a manner consistent with the handling of normal slices.
2392
2393Returns 0 on success and -1 on error with exception set.
2394
2395\versionadded{2.3}
Fred Drake3adf79e2001-10-12 19:01:43 +00002396\end{cfuncdesc}
2397
2398
2399\subsection{Weak Reference Objects \label{weakref-objects}}
2400
2401Python supports \emph{weak references} as first-class objects. There
2402are two specific object types which directly implement weak
2403references. The first is a simple reference object, and the second
2404acts as a proxy for the original object as much as it can.
2405
2406\begin{cfuncdesc}{int}{PyWeakref_Check}{ob}
2407 Return true if \var{ob} is either a reference or proxy object.
2408 \versionadded{2.2}
2409\end{cfuncdesc}
2410
2411\begin{cfuncdesc}{int}{PyWeakref_CheckRef}{ob}
2412 Return true if \var{ob} is a reference object.
2413 \versionadded{2.2}
2414\end{cfuncdesc}
2415
2416\begin{cfuncdesc}{int}{PyWeakref_CheckProxy}{ob}
2417 Return true if \var{ob} is a proxy object.
2418 \versionadded{2.2}
2419\end{cfuncdesc}
2420
2421\begin{cfuncdesc}{PyObject*}{PyWeakref_NewRef}{PyObject *ob,
2422 PyObject *callback}
2423 Return a weak reference object for the object \var{ob}. This will
2424 always return a new reference, but is not guaranteed to create a new
2425 object; an existing reference object may be returned. The second
2426 parameter, \var{callback}, can be a callable object that receives
2427 notification when \var{ob} is garbage collected; it should accept a
2428 single paramter, which will be the weak reference object itself.
2429 \var{callback} may also be \code{None} or \NULL. If \var{ob}
2430 is not a weakly-referencable object, or if \var{callback} is not
2431 callable, \code{None}, or \NULL, this will return \NULL{} and
2432 raise \exception{TypeError}.
2433 \versionadded{2.2}
2434\end{cfuncdesc}
2435
2436\begin{cfuncdesc}{PyObject*}{PyWeakref_NewProxy}{PyObject *ob,
2437 PyObject *callback}
2438 Return a weak reference proxy object for the object \var{ob}. This
2439 will always return a new reference, but is not guaranteed to create
2440 a new object; an existing proxy object may be returned. The second
2441 parameter, \var{callback}, can be a callable object that receives
2442 notification when \var{ob} is garbage collected; it should accept a
2443 single paramter, which will be the weak reference object itself.
2444 \var{callback} may also be \code{None} or \NULL. If \var{ob} is not
2445 a weakly-referencable object, or if \var{callback} is not callable,
2446 \code{None}, or \NULL, this will return \NULL{} and raise
2447 \exception{TypeError}.
2448 \versionadded{2.2}
2449\end{cfuncdesc}
2450
2451\begin{cfuncdesc}{PyObject*}{PyWeakref_GetObject}{PyObject *ref}
2452 Returns the referenced object from a weak reference, \var{ref}. If
Ka-Ping Yeebd379e92003-03-28 18:07:16 +00002453 the referent is no longer live, returns \code{None}.
Fred Drake3adf79e2001-10-12 19:01:43 +00002454 \versionadded{2.2}
2455\end{cfuncdesc}
2456
2457\begin{cfuncdesc}{PyObject*}{PyWeakref_GET_OBJECT}{PyObject *ref}
2458 Similar to \cfunction{PyWeakref_GetObject()}, but implemented as a
2459 macro that does no error checking.
2460 \versionadded{2.2}
2461\end{cfuncdesc}
2462
2463
2464\subsection{CObjects \label{cObjects}}
2465
2466\obindex{CObject}
2467Refer to \emph{Extending and Embedding the Python Interpreter},
Fred Drake54e62942001-12-11 19:40:16 +00002468section~1.12, ``Providing a C API for an Extension Module,'' for more
Fred Drake3adf79e2001-10-12 19:01:43 +00002469information on using these objects.
2470
2471
2472\begin{ctypedesc}{PyCObject}
2473 This subtype of \ctype{PyObject} represents an opaque value, useful
2474 for C extension modules who need to pass an opaque value (as a
2475 \ctype{void*} pointer) through Python code to other C code. It is
2476 often used to make a C function pointer defined in one module
2477 available to other modules, so the regular import mechanism can be
2478 used to access C APIs defined in dynamically loaded modules.
2479\end{ctypedesc}
2480
2481\begin{cfuncdesc}{int}{PyCObject_Check}{PyObject *p}
2482 Returns true if its argument is a \ctype{PyCObject}.
2483\end{cfuncdesc}
2484
Tim Petersf582b822001-12-11 18:51:08 +00002485\begin{cfuncdesc}{PyObject*}{PyCObject_FromVoidPtr}{void* cobj,
Fred Drake54e62942001-12-11 19:40:16 +00002486 void (*destr)(void *)}
Fred Drake3adf79e2001-10-12 19:01:43 +00002487 Creates a \ctype{PyCObject} from the \code{void *}\var{cobj}. The
2488 \var{destr} function will be called when the object is reclaimed,
2489 unless it is \NULL.
2490\end{cfuncdesc}
2491
2492\begin{cfuncdesc}{PyObject*}{PyCObject_FromVoidPtrAndDesc}{void* cobj,
2493 void* desc, void (*destr)(void *, void *)}
2494 Creates a \ctype{PyCObject} from the \ctype{void *}\var{cobj}. The
2495 \var{destr} function will be called when the object is reclaimed.
2496 The \var{desc} argument can be used to pass extra callback data for
2497 the destructor function.
2498\end{cfuncdesc}
2499
2500\begin{cfuncdesc}{void*}{PyCObject_AsVoidPtr}{PyObject* self}
2501 Returns the object \ctype{void *} that the \ctype{PyCObject}
2502 \var{self} was created with.
2503\end{cfuncdesc}
2504
2505\begin{cfuncdesc}{void*}{PyCObject_GetDesc}{PyObject* self}
2506 Returns the description \ctype{void *} that the \ctype{PyCObject}
2507 \var{self} was created with.
2508\end{cfuncdesc}
Fred Drakecd8474e2001-11-26 21:29:17 +00002509
2510
2511\subsection{Cell Objects \label{cell-objects}}
2512
2513``Cell'' objects are used to implement variables referenced by
2514multiple scopes. For each such variable, a cell object is created to
2515store the value; the local variables of each stack frame that
2516references the value contains a reference to the cells from outer
2517scopes which also use that variable. When the value is accessed, the
2518value contained in the cell is used instead of the cell object
2519itself. This de-referencing of the cell object requires support from
2520the generated byte-code; these are not automatically de-referenced
2521when accessed. Cell objects are not likely to be useful elsewhere.
2522
Fred Drake54e62942001-12-11 19:40:16 +00002523\begin{ctypedesc}{PyCellObject}
2524 The C structure used for cell objects.
2525\end{ctypedesc}
2526
Fred Drakecd8474e2001-11-26 21:29:17 +00002527\begin{cvardesc}{PyTypeObject}{PyCell_Type}
2528 The type object corresponding to cell objects
2529\end{cvardesc}
2530
2531\begin{cfuncdesc}{int}{PyCell_Check}{ob}
2532 Return true if \var{ob} is a cell object; \var{ob} must not be
2533 \NULL.
2534\end{cfuncdesc}
2535
2536\begin{cfuncdesc}{PyObject*}{PyCell_New}{PyObject *ob}
2537 Create and return a new cell object containing the value \var{ob}.
2538 The parameter may be \NULL.
2539\end{cfuncdesc}
2540
2541\begin{cfuncdesc}{PyObject*}{PyCell_Get}{PyObject *cell}
2542 Return the contents of the cell \var{cell}.
2543\end{cfuncdesc}
2544
2545\begin{cfuncdesc}{PyObject*}{PyCell_GET}{PyObject *cell}
2546 Return the contents of the cell \var{cell}, but without checking
Raymond Hettingerf4bb1f92003-08-23 03:38:11 +00002547 that \var{cell} is non-\NULL{} and a cell object.
Fred Drakecd8474e2001-11-26 21:29:17 +00002548\end{cfuncdesc}
2549
2550\begin{cfuncdesc}{int}{PyCell_Set}{PyObject *cell, PyObject *value}
2551 Set the contents of the cell object \var{cell} to \var{value}. This
2552 releases the reference to any current content of the cell.
2553 \var{value} may be \NULL. \var{cell} must be non-\NULL; if it is
2554 not a cell object, \code{-1} will be returned. On success, \code{0}
2555 will be returned.
2556\end{cfuncdesc}
2557
2558\begin{cfuncdesc}{void}{PyCell_SET}{PyObject *cell, PyObject *value}
2559 Sets the value of the cell object \var{cell} to \var{value}. No
2560 reference counts are adjusted, and no checks are made for safety;
2561 \var{cell} must be non-\NULL{} and must be a cell object.
2562\end{cfuncdesc}