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