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