Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 1 | \chapter{Abstract Objects Layer \label{abstract}} |
| 2 | |
| 3 | The functions in this chapter interact with Python objects regardless |
| 4 | of their type, or with wide classes of object types (e.g. all |
| 5 | numerical types, or all sequence types). When used on object types |
| 6 | for which they do not apply, they will raise a Python exception. |
| 7 | |
| 8 | |
| 9 | \section{Object Protocol \label{object}} |
| 10 | |
| 11 | \begin{cfuncdesc}{int}{PyObject_Print}{PyObject *o, FILE *fp, int flags} |
| 12 | Print an object \var{o}, on file \var{fp}. Returns \code{-1} on |
| 13 | error. The flags argument is used to enable certain printing |
| 14 | options. The only option currently supported is |
| 15 | \constant{Py_PRINT_RAW}; if given, the \function{str()} of the |
| 16 | object is written instead of the \function{repr()}. |
| 17 | \end{cfuncdesc} |
| 18 | |
| 19 | \begin{cfuncdesc}{int}{PyObject_HasAttrString}{PyObject *o, char *attr_name} |
| 20 | Returns \code{1} if \var{o} has the attribute \var{attr_name}, and |
| 21 | \code{0} otherwise. This is equivalent to the Python expression |
| 22 | \samp{hasattr(\var{o}, \var{attr_name})}. This function always |
| 23 | succeeds. |
| 24 | \end{cfuncdesc} |
| 25 | |
| 26 | \begin{cfuncdesc}{PyObject*}{PyObject_GetAttrString}{PyObject *o, |
| 27 | char *attr_name} |
| 28 | Retrieve an attribute named \var{attr_name} from object \var{o}. |
| 29 | Returns the attribute value on success, or \NULL{} on failure. |
| 30 | This is the equivalent of the Python expression |
| 31 | \samp{\var{o}.\var{attr_name}}. |
| 32 | \end{cfuncdesc} |
| 33 | |
| 34 | |
| 35 | \begin{cfuncdesc}{int}{PyObject_HasAttr}{PyObject *o, PyObject *attr_name} |
| 36 | Returns \code{1} if \var{o} has the attribute \var{attr_name}, and |
| 37 | \code{0} otherwise. This is equivalent to the Python expression |
| 38 | \samp{hasattr(\var{o}, \var{attr_name})}. This function always |
| 39 | succeeds. |
| 40 | \end{cfuncdesc} |
| 41 | |
| 42 | |
| 43 | \begin{cfuncdesc}{PyObject*}{PyObject_GetAttr}{PyObject *o, |
| 44 | PyObject *attr_name} |
| 45 | Retrieve an attribute named \var{attr_name} from object \var{o}. |
| 46 | Returns the attribute value on success, or \NULL{} on failure. This |
| 47 | is the equivalent of the Python expression |
| 48 | \samp{\var{o}.\var{attr_name}}. |
| 49 | \end{cfuncdesc} |
| 50 | |
| 51 | |
| 52 | \begin{cfuncdesc}{int}{PyObject_SetAttrString}{PyObject *o, |
| 53 | char *attr_name, PyObject *v} |
| 54 | Set the value of the attribute named \var{attr_name}, for object |
| 55 | \var{o}, to the value \var{v}. Returns \code{-1} on failure. This |
| 56 | is the equivalent of the Python statement |
| 57 | \samp{\var{o}.\var{attr_name} = \var{v}}. |
| 58 | \end{cfuncdesc} |
| 59 | |
| 60 | |
| 61 | \begin{cfuncdesc}{int}{PyObject_SetAttr}{PyObject *o, |
| 62 | PyObject *attr_name, PyObject *v} |
| 63 | Set the value of the attribute named \var{attr_name}, for object |
| 64 | \var{o}, to the value \var{v}. Returns \code{-1} on failure. This |
| 65 | is the equivalent of the Python statement |
| 66 | \samp{\var{o}.\var{attr_name} = \var{v}}. |
| 67 | \end{cfuncdesc} |
| 68 | |
| 69 | |
| 70 | \begin{cfuncdesc}{int}{PyObject_DelAttrString}{PyObject *o, char *attr_name} |
| 71 | Delete attribute named \var{attr_name}, for object \var{o}. Returns |
| 72 | \code{-1} on failure. This is the equivalent of the Python |
| 73 | statement: \samp{del \var{o}.\var{attr_name}}. |
| 74 | \end{cfuncdesc} |
| 75 | |
| 76 | |
| 77 | \begin{cfuncdesc}{int}{PyObject_DelAttr}{PyObject *o, PyObject *attr_name} |
| 78 | Delete attribute named \var{attr_name}, for object \var{o}. Returns |
| 79 | \code{-1} on failure. This is the equivalent of the Python |
| 80 | statement \samp{del \var{o}.\var{attr_name}}. |
| 81 | \end{cfuncdesc} |
| 82 | |
| 83 | |
| 84 | \begin{cfuncdesc}{int}{PyObject_Cmp}{PyObject *o1, PyObject *o2, int *result} |
| 85 | Compare the values of \var{o1} and \var{o2} using a routine provided |
| 86 | by \var{o1}, if one exists, otherwise with a routine provided by |
| 87 | \var{o2}. The result of the comparison is returned in |
| 88 | \var{result}. Returns \code{-1} on failure. This is the equivalent |
| 89 | of the Python statement\bifuncindex{cmp} \samp{\var{result} = |
| 90 | cmp(\var{o1}, \var{o2})}. |
| 91 | \end{cfuncdesc} |
| 92 | |
| 93 | |
| 94 | \begin{cfuncdesc}{int}{PyObject_Compare}{PyObject *o1, PyObject *o2} |
| 95 | Compare the values of \var{o1} and \var{o2} using a routine provided |
| 96 | by \var{o1}, if one exists, otherwise with a routine provided by |
| 97 | \var{o2}. Returns the result of the comparison on success. On |
| 98 | error, the value returned is undefined; use |
| 99 | \cfunction{PyErr_Occurred()} to detect an error. This is equivalent |
| 100 | to the Python expression\bifuncindex{cmp} \samp{cmp(\var{o1}, |
| 101 | \var{o2})}. |
| 102 | \end{cfuncdesc} |
| 103 | |
| 104 | |
| 105 | \begin{cfuncdesc}{PyObject*}{PyObject_Repr}{PyObject *o} |
| 106 | Compute a string representation of object \var{o}. Returns the |
| 107 | string representation on success, \NULL{} on failure. This is the |
| 108 | equivalent of the Python expression \samp{repr(\var{o})}. Called by |
| 109 | the \function{repr()}\bifuncindex{repr} built-in function and by |
| 110 | reverse quotes. |
| 111 | \end{cfuncdesc} |
| 112 | |
| 113 | |
| 114 | \begin{cfuncdesc}{PyObject*}{PyObject_Str}{PyObject *o} |
| 115 | Compute a string representation of object \var{o}. Returns the |
| 116 | string representation on success, \NULL{} on failure. This is the |
| 117 | equivalent of the Python expression \samp{str(\var{o})}. Called by |
| 118 | the \function{str()}\bifuncindex{str} built-in function and by the |
| 119 | \keyword{print} statement. |
| 120 | \end{cfuncdesc} |
| 121 | |
| 122 | |
| 123 | \begin{cfuncdesc}{PyObject*}{PyObject_Unicode}{PyObject *o} |
| 124 | Compute a Unicode string representation of object \var{o}. Returns |
| 125 | the Unicode string representation on success, \NULL{} on failure. |
| 126 | This is the equivalent of the Python expression |
| 127 | \samp{unistr(\var{o})}. Called by the |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 128 | \function{unistr()}\bifuncindex{unistr} built-in function. |
Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 129 | \end{cfuncdesc} |
| 130 | |
| 131 | \begin{cfuncdesc}{int}{PyObject_IsInstance}{PyObject *inst, PyObject *cls} |
| 132 | Return \code{1} if \var{inst} is an instance of the class \var{cls} |
| 133 | or a subclass of \var{cls}. If \var{cls} is a type object rather |
| 134 | than a class object, \cfunction{PyObject_IsInstance()} returns |
| 135 | \code{1} if \var{inst} is of type \var{cls}. If \var{inst} is not a |
| 136 | class instance and \var{cls} is neither a type object or class |
| 137 | object, \var{inst} must have a \member{__class__} attribute --- the |
| 138 | class relationship of the value of that attribute with \var{cls} |
| 139 | will be used to determine the result of this function. |
| 140 | \versionadded{2.1} |
| 141 | \end{cfuncdesc} |
| 142 | |
| 143 | Subclass determination is done in a fairly straightforward way, but |
| 144 | includes a wrinkle that implementors of extensions to the class system |
| 145 | may want to be aware of. If \class{A} and \class{B} are class |
| 146 | objects, \class{B} is a subclass of \class{A} if it inherits from |
| 147 | \class{A} either directly or indirectly. If either is not a class |
| 148 | object, a more general mechanism is used to determine the class |
| 149 | relationship of the two objects. When testing if \var{B} is a |
| 150 | subclass of \var{A}, if \var{A} is \var{B}, |
| 151 | \cfunction{PyObject_IsSubclass()} returns true. If \var{A} and |
| 152 | \var{B} are different objects, \var{B}'s \member{__bases__} attribute |
| 153 | is searched in a depth-first fashion for \var{A} --- the presence of |
| 154 | the \member{__bases__} attribute is considered sufficient for this |
| 155 | determination. |
| 156 | |
| 157 | \begin{cfuncdesc}{int}{PyObject_IsSubclass}{PyObject *derived, |
| 158 | PyObject *cls} |
| 159 | Returns \code{1} if the class \var{derived} is identical to or |
| 160 | derived from the class \var{cls}, otherwise returns \code{0}. In |
| 161 | case of an error, returns \code{-1}. If either \var{derived} or |
| 162 | \var{cls} is not an actual class object, this function uses the |
| 163 | generic algorithm described above. |
| 164 | \versionadded{2.1} |
| 165 | \end{cfuncdesc} |
| 166 | |
| 167 | |
| 168 | \begin{cfuncdesc}{int}{PyCallable_Check}{PyObject *o} |
| 169 | Determine if the object \var{o} is callable. Return \code{1} if the |
| 170 | object is callable and \code{0} otherwise. This function always |
| 171 | succeeds. |
| 172 | \end{cfuncdesc} |
| 173 | |
| 174 | |
| 175 | \begin{cfuncdesc}{PyObject*}{PyObject_CallObject}{PyObject *callable_object, |
| 176 | PyObject *args} |
| 177 | Call a callable Python object \var{callable_object}, with arguments |
| 178 | given by the tuple \var{args}. If no arguments are needed, then |
| 179 | \var{args} may be \NULL. Returns the result of the call on |
| 180 | success, or \NULL{} on failure. This is the equivalent of the |
| 181 | Python expression \samp{apply(\var{callable_object}, \var{args})} or |
| 182 | \samp{\var{callable_object}(*\var{args})}. |
| 183 | \bifuncindex{apply} |
| 184 | \end{cfuncdesc} |
| 185 | |
Fred Drake | c44e9ec | 2001-10-26 16:38:38 +0000 | [diff] [blame] | 186 | \begin{cfuncdesc}{PyObject*}{PyObject_CallFunction}{PyObject *callable, |
| 187 | char *format, \moreargs} |
| 188 | Call a callable Python object \var{callable}, with a variable |
Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 189 | number of C arguments. The C arguments are described using a |
| 190 | \cfunction{Py_BuildValue()} style format string. The format may be |
| 191 | \NULL, indicating that no arguments are provided. Returns the |
| 192 | result of the call on success, or \NULL{} on failure. This is the |
Fred Drake | c44e9ec | 2001-10-26 16:38:38 +0000 | [diff] [blame] | 193 | equivalent of the Python expression \samp{apply(\var{callable}, |
| 194 | \var{args})} or \samp{\var{callable}(*\var{args})}. |
Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 195 | \bifuncindex{apply} |
| 196 | \end{cfuncdesc} |
| 197 | |
| 198 | |
| 199 | \begin{cfuncdesc}{PyObject*}{PyObject_CallMethod}{PyObject *o, |
Fred Drake | c44e9ec | 2001-10-26 16:38:38 +0000 | [diff] [blame] | 200 | char *method, char *format, |
| 201 | \moreargs} |
Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 202 | Call the method named \var{m} of object \var{o} with a variable |
| 203 | number of C arguments. The C arguments are described by a |
| 204 | \cfunction{Py_BuildValue()} format string. The format may be \NULL, |
| 205 | indicating that no arguments are provided. Returns the result of the |
| 206 | call on success, or \NULL{} on failure. This is the equivalent of |
Fred Drake | c44e9ec | 2001-10-26 16:38:38 +0000 | [diff] [blame] | 207 | the Python expression \samp{\var{o}.\var{method}(\var{args})}. |
| 208 | \end{cfuncdesc} |
| 209 | |
| 210 | |
Fred Drake | b0c079e | 2001-10-28 02:39:03 +0000 | [diff] [blame] | 211 | \begin{cfuncdesc}{PyObject*}{PyObject_CallFunctionObjArgs}{PyObject *callable, |
| 212 | \moreargs, |
| 213 | \code{NULL}} |
Fred Drake | c44e9ec | 2001-10-26 16:38:38 +0000 | [diff] [blame] | 214 | Call a callable Python object \var{callable}, with a variable |
| 215 | number of \ctype{PyObject*} arguments. The arguments are provided |
| 216 | as a variable number of parameters followed by \NULL. |
| 217 | Returns the result of the call on success, or \NULL{} on failure. |
| 218 | \versionadded{2.2} |
| 219 | \end{cfuncdesc} |
| 220 | |
| 221 | |
Fred Drake | b0c079e | 2001-10-28 02:39:03 +0000 | [diff] [blame] | 222 | \begin{cfuncdesc}{PyObject*}{PyObject_CallMethodObjArgs}{PyObject *o, |
| 223 | PyObject *name, |
| 224 | \moreargs, |
| 225 | \code{NULL}} |
Fred Drake | c44e9ec | 2001-10-26 16:38:38 +0000 | [diff] [blame] | 226 | Calls a method of the object \var{o}, where the name of the method |
| 227 | is given as a Python string object in \var{name}. It is called with |
| 228 | a variable number of \ctype{PyObject*} arguments. The arguments are |
| 229 | provided as a variable number of parameters followed by \NULL. |
| 230 | Returns the result of the call on success, or \NULL{} on failure. |
| 231 | \versionadded{2.2} |
Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 232 | \end{cfuncdesc} |
| 233 | |
| 234 | |
| 235 | \begin{cfuncdesc}{int}{PyObject_Hash}{PyObject *o} |
| 236 | Compute and return the hash value of an object \var{o}. On failure, |
| 237 | return \code{-1}. This is the equivalent of the Python expression |
| 238 | \samp{hash(\var{o})}.\bifuncindex{hash} |
| 239 | \end{cfuncdesc} |
| 240 | |
| 241 | |
| 242 | \begin{cfuncdesc}{int}{PyObject_IsTrue}{PyObject *o} |
| 243 | Returns \code{1} if the object \var{o} is considered to be true, and |
| 244 | \code{0} otherwise. This is equivalent to the Python expression |
| 245 | \samp{not not \var{o}}. This function always succeeds. |
| 246 | \end{cfuncdesc} |
| 247 | |
| 248 | |
| 249 | \begin{cfuncdesc}{PyObject*}{PyObject_Type}{PyObject *o} |
| 250 | When \var{o} is non-\NULL, returns a type object corresponding to |
| 251 | the object type of object \var{o}. On failure, raises |
| 252 | \exception{SystemError} and returns \NULL. This is equivalent to |
| 253 | the Python expression \code{type(\var{o})}. |
| 254 | \bifuncindex{type} |
| 255 | \end{cfuncdesc} |
| 256 | |
| 257 | \begin{cfuncdesc}{int}{PyObject_TypeCheck}{PyObject *o, PyTypeObject *type} |
| 258 | Return true if the object \var{o} is of type \var{type} or a subtype |
| 259 | of \var{type}. Both parameters must be non-\NULL. |
| 260 | \versionadded{2.2} |
| 261 | \end{cfuncdesc} |
| 262 | |
| 263 | \begin{cfuncdesc}{int}{PyObject_Length}{PyObject *o} |
| 264 | Return the length of object \var{o}. If the object \var{o} provides |
| 265 | both sequence and mapping protocols, the sequence length is |
| 266 | returned. On error, \code{-1} is returned. This is the equivalent |
| 267 | to the Python expression \samp{len(\var{o})}.\bifuncindex{len} |
| 268 | \end{cfuncdesc} |
| 269 | |
| 270 | |
| 271 | \begin{cfuncdesc}{PyObject*}{PyObject_GetItem}{PyObject *o, PyObject *key} |
| 272 | Return element of \var{o} corresponding to the object \var{key} or |
| 273 | \NULL{} on failure. This is the equivalent of the Python expression |
| 274 | \samp{\var{o}[\var{key}]}. |
| 275 | \end{cfuncdesc} |
| 276 | |
| 277 | |
| 278 | \begin{cfuncdesc}{int}{PyObject_SetItem}{PyObject *o, |
| 279 | PyObject *key, PyObject *v} |
| 280 | Map the object \var{key} to the value \var{v}. Returns \code{-1} on |
| 281 | failure. This is the equivalent of the Python statement |
| 282 | \samp{\var{o}[\var{key}] = \var{v}}. |
| 283 | \end{cfuncdesc} |
| 284 | |
| 285 | |
| 286 | \begin{cfuncdesc}{int}{PyObject_DelItem}{PyObject *o, PyObject *key} |
| 287 | Delete the mapping for \var{key} from \var{o}. Returns \code{-1} on |
| 288 | failure. This is the equivalent of the Python statement \samp{del |
| 289 | \var{o}[\var{key}]}. |
| 290 | \end{cfuncdesc} |
| 291 | |
| 292 | \begin{cfuncdesc}{int}{PyObject_AsFileDescriptor}{PyObject *o} |
| 293 | Derives a file-descriptor from a Python object. If the object is an |
| 294 | integer or long integer, its value is returned. If not, the |
| 295 | object's \method{fileno()} method is called if it exists; the method |
| 296 | must return an integer or long integer, which is returned as the |
| 297 | file descriptor value. Returns \code{-1} on failure. |
| 298 | \end{cfuncdesc} |
| 299 | |
| 300 | \begin{cfuncdesc}{PyObject*}{PyObject_Dir}{PyObject *o} |
| 301 | This is equivalent to the Python expression \samp{dir(\var{o})}, |
| 302 | returning a (possibly empty) list of strings appropriate for the |
| 303 | object argument, or \NULL{} if there was an error. If the argument |
| 304 | is \NULL, this is like the Python \samp{dir()}, returning the names |
| 305 | of the current locals; in this case, if no execution frame is active |
| 306 | then \NULL{} is returned but \cfunction{PyErr_Occurred()} will |
| 307 | return false. |
| 308 | \end{cfuncdesc} |
| 309 | |
| 310 | |
| 311 | \section{Number Protocol \label{number}} |
| 312 | |
| 313 | \begin{cfuncdesc}{int}{PyNumber_Check}{PyObject *o} |
| 314 | Returns \code{1} if the object \var{o} provides numeric protocols, |
| 315 | and false otherwise. This function always succeeds. |
| 316 | \end{cfuncdesc} |
| 317 | |
| 318 | |
| 319 | \begin{cfuncdesc}{PyObject*}{PyNumber_Add}{PyObject *o1, PyObject *o2} |
| 320 | Returns the result of adding \var{o1} and \var{o2}, or \NULL{} on |
| 321 | failure. This is the equivalent of the Python expression |
| 322 | \samp{\var{o1} + \var{o2}}. |
| 323 | \end{cfuncdesc} |
| 324 | |
| 325 | |
| 326 | \begin{cfuncdesc}{PyObject*}{PyNumber_Subtract}{PyObject *o1, PyObject *o2} |
| 327 | Returns the result of subtracting \var{o2} from \var{o1}, or \NULL{} |
| 328 | on failure. This is the equivalent of the Python expression |
| 329 | \samp{\var{o1} - \var{o2}}. |
| 330 | \end{cfuncdesc} |
| 331 | |
| 332 | |
| 333 | \begin{cfuncdesc}{PyObject*}{PyNumber_Multiply}{PyObject *o1, PyObject *o2} |
| 334 | Returns the result of multiplying \var{o1} and \var{o2}, or \NULL{} |
| 335 | on failure. This is the equivalent of the Python expression |
| 336 | \samp{\var{o1} * \var{o2}}. |
| 337 | \end{cfuncdesc} |
| 338 | |
| 339 | |
| 340 | \begin{cfuncdesc}{PyObject*}{PyNumber_Divide}{PyObject *o1, PyObject *o2} |
| 341 | Returns the result of dividing \var{o1} by \var{o2}, or \NULL{} on |
| 342 | failure. This is the equivalent of the Python expression |
| 343 | \samp{\var{o1} / \var{o2}}. |
| 344 | \end{cfuncdesc} |
| 345 | |
| 346 | |
| 347 | \begin{cfuncdesc}{PyObject*}{PyNumber_FloorDivide}{PyObject *o1, PyObject *o2} |
| 348 | Return the floor of \var{o1} divided by \var{o2}, or \NULL{} on |
| 349 | failure. This is equivalent to the ``classic'' division of |
| 350 | integers. |
| 351 | \versionadded{2.2} |
| 352 | \end{cfuncdesc} |
| 353 | |
| 354 | |
| 355 | \begin{cfuncdesc}{PyObject*}{PyNumber_TrueDivide}{PyObject *o1, PyObject *o2} |
| 356 | Return a reasonable approximation for the mathematical value of |
| 357 | \var{o1} divided by \var{o2}, or \NULL{} on failure. The return |
| 358 | value is ``approximate'' because binary floating point numbers are |
| 359 | approximate; it is not possible to represent all real numbers in |
| 360 | base two. This function can return a floating point value when |
| 361 | passed two integers. |
| 362 | \versionadded{2.2} |
| 363 | \end{cfuncdesc} |
| 364 | |
| 365 | |
| 366 | \begin{cfuncdesc}{PyObject*}{PyNumber_Remainder}{PyObject *o1, PyObject *o2} |
| 367 | Returns the remainder of dividing \var{o1} by \var{o2}, or \NULL{} |
| 368 | on failure. This is the equivalent of the Python expression |
| 369 | \samp{\var{o1} \%\ \var{o2}}. |
| 370 | \end{cfuncdesc} |
| 371 | |
| 372 | |
| 373 | \begin{cfuncdesc}{PyObject*}{PyNumber_Divmod}{PyObject *o1, PyObject *o2} |
| 374 | See the built-in function \function{divmod()}\bifuncindex{divmod}. |
| 375 | Returns \NULL{} on failure. This is the equivalent of the Python |
| 376 | expression \samp{divmod(\var{o1}, \var{o2})}. |
| 377 | \end{cfuncdesc} |
| 378 | |
| 379 | |
| 380 | \begin{cfuncdesc}{PyObject*}{PyNumber_Power}{PyObject *o1, |
| 381 | PyObject *o2, PyObject *o3} |
| 382 | See the built-in function \function{pow()}\bifuncindex{pow}. |
| 383 | Returns \NULL{} on failure. This is the equivalent of the Python |
| 384 | expression \samp{pow(\var{o1}, \var{o2}, \var{o3})}, where \var{o3} |
| 385 | is optional. If \var{o3} is to be ignored, pass \cdata{Py_None} in |
| 386 | its place (passing \NULL{} for \var{o3} would cause an illegal |
| 387 | memory access). |
| 388 | \end{cfuncdesc} |
| 389 | |
| 390 | |
| 391 | \begin{cfuncdesc}{PyObject*}{PyNumber_Negative}{PyObject *o} |
| 392 | Returns the negation of \var{o} on success, or \NULL{} on failure. |
| 393 | This is the equivalent of the Python expression \samp{-\var{o}}. |
| 394 | \end{cfuncdesc} |
| 395 | |
| 396 | |
| 397 | \begin{cfuncdesc}{PyObject*}{PyNumber_Positive}{PyObject *o} |
| 398 | Returns \var{o} on success, or \NULL{} on failure. This is the |
| 399 | equivalent of the Python expression \samp{+\var{o}}. |
| 400 | \end{cfuncdesc} |
| 401 | |
| 402 | |
| 403 | \begin{cfuncdesc}{PyObject*}{PyNumber_Absolute}{PyObject *o} |
| 404 | Returns the absolute value of \var{o}, or \NULL{} on failure. This |
| 405 | is the equivalent of the Python expression \samp{abs(\var{o})}. |
| 406 | \bifuncindex{abs} |
| 407 | \end{cfuncdesc} |
| 408 | |
| 409 | |
| 410 | \begin{cfuncdesc}{PyObject*}{PyNumber_Invert}{PyObject *o} |
| 411 | Returns the bitwise negation of \var{o} on success, or \NULL{} on |
| 412 | failure. This is the equivalent of the Python expression |
| 413 | \samp{\~\var{o}}. |
| 414 | \end{cfuncdesc} |
| 415 | |
| 416 | |
| 417 | \begin{cfuncdesc}{PyObject*}{PyNumber_Lshift}{PyObject *o1, PyObject *o2} |
| 418 | Returns the result of left shifting \var{o1} by \var{o2} on success, |
| 419 | or \NULL{} on failure. This is the equivalent of the Python |
| 420 | expression \samp{\var{o1} <\code{<} \var{o2}}. |
| 421 | \end{cfuncdesc} |
| 422 | |
| 423 | |
| 424 | \begin{cfuncdesc}{PyObject*}{PyNumber_Rshift}{PyObject *o1, PyObject *o2} |
| 425 | Returns the result of right shifting \var{o1} by \var{o2} on |
| 426 | success, or \NULL{} on failure. This is the equivalent of the |
| 427 | Python expression \samp{\var{o1} >\code{>} \var{o2}}. |
| 428 | \end{cfuncdesc} |
| 429 | |
| 430 | |
| 431 | \begin{cfuncdesc}{PyObject*}{PyNumber_And}{PyObject *o1, PyObject *o2} |
| 432 | Returns the ``bitwise and'' of \var{o2} and \var{o2} on success and |
| 433 | \NULL{} on failure. This is the equivalent of the Python expression |
| 434 | \samp{\var{o1} \&\ \var{o2}}. |
| 435 | \end{cfuncdesc} |
| 436 | |
| 437 | |
| 438 | \begin{cfuncdesc}{PyObject*}{PyNumber_Xor}{PyObject *o1, PyObject *o2} |
| 439 | Returns the ``bitwise exclusive or'' of \var{o1} by \var{o2} on |
| 440 | success, or \NULL{} on failure. This is the equivalent of the |
| 441 | Python expression \samp{\var{o1} \textasciicircum{} \var{o2}}. |
| 442 | \end{cfuncdesc} |
| 443 | |
| 444 | \begin{cfuncdesc}{PyObject*}{PyNumber_Or}{PyObject *o1, PyObject *o2} |
| 445 | Returns the ``bitwise or'' of \var{o1} and \var{o2} on success, or |
| 446 | \NULL{} on failure. This is the equivalent of the Python expression |
| 447 | \samp{\var{o1} | \var{o2}}. |
| 448 | \end{cfuncdesc} |
| 449 | |
| 450 | |
| 451 | \begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceAdd}{PyObject *o1, PyObject *o2} |
| 452 | Returns the result of adding \var{o1} and \var{o2}, or \NULL{} on |
| 453 | failure. The operation is done \emph{in-place} when \var{o1} |
| 454 | supports it. This is the equivalent of the Python statement |
| 455 | \samp{\var{o1} += \var{o2}}. |
| 456 | \end{cfuncdesc} |
| 457 | |
| 458 | |
| 459 | \begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceSubtract}{PyObject *o1, |
| 460 | PyObject *o2} |
| 461 | Returns the result of subtracting \var{o2} from \var{o1}, or \NULL{} |
| 462 | on failure. The operation is done \emph{in-place} when \var{o1} |
| 463 | supports it. This is the equivalent of the Python statement |
| 464 | \samp{\var{o1} -= \var{o2}}. |
| 465 | \end{cfuncdesc} |
| 466 | |
| 467 | |
| 468 | \begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceMultiply}{PyObject *o1, |
| 469 | PyObject *o2} |
| 470 | Returns the result of multiplying \var{o1} and \var{o2}, or \NULL{} |
| 471 | on failure. The operation is done \emph{in-place} when \var{o1} |
| 472 | supports it. This is the equivalent of the Python statement |
| 473 | \samp{\var{o1} *= \var{o2}}. |
| 474 | \end{cfuncdesc} |
| 475 | |
| 476 | |
| 477 | \begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceDivide}{PyObject *o1, |
| 478 | PyObject *o2} |
| 479 | Returns the result of dividing \var{o1} by \var{o2}, or \NULL{} on |
| 480 | failure. The operation is done \emph{in-place} when \var{o1} |
| 481 | supports it. This is the equivalent of the Python statement |
| 482 | \samp{\var{o1} /= \var{o2}}. |
| 483 | \end{cfuncdesc} |
| 484 | |
| 485 | |
| 486 | \begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceFloorDivide}{PyObject *o1, |
| 487 | PyObject *o2} |
| 488 | Returns the mathematical of dividing \var{o1} by \var{o2}, or |
| 489 | \NULL{} on failure. The operation is done \emph{in-place} when |
| 490 | \var{o1} supports it. This is the equivalent of the Python |
| 491 | statement \samp{\var{o1} //= \var{o2}}. |
| 492 | \versionadded{2.2} |
| 493 | \end{cfuncdesc} |
| 494 | |
| 495 | |
| 496 | \begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceTrueDivide}{PyObject *o1, |
| 497 | PyObject *o2} |
| 498 | Return a reasonable approximation for the mathematical value of |
| 499 | \var{o1} divided by \var{o2}, or \NULL{} on failure. The return |
| 500 | value is ``approximate'' because binary floating point numbers are |
| 501 | approximate; it is not possible to represent all real numbers in |
| 502 | base two. This function can return a floating point value when |
| 503 | passed two integers. The operation is done \emph{in-place} when |
| 504 | \var{o1} supports it. |
| 505 | \versionadded{2.2} |
| 506 | \end{cfuncdesc} |
| 507 | |
| 508 | |
| 509 | \begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceRemainder}{PyObject *o1, |
| 510 | PyObject *o2} |
| 511 | Returns the remainder of dividing \var{o1} by \var{o2}, or \NULL{} |
| 512 | on failure. The operation is done \emph{in-place} when \var{o1} |
| 513 | supports it. This is the equivalent of the Python statement |
| 514 | \samp{\var{o1} \%= \var{o2}}. |
| 515 | \end{cfuncdesc} |
| 516 | |
| 517 | |
| 518 | \begin{cfuncdesc}{PyObject*}{PyNumber_InPlacePower}{PyObject *o1, |
| 519 | PyObject *o2, PyObject *o3} |
| 520 | See the built-in function \function{pow()}.\bifuncindex{pow} |
| 521 | Returns \NULL{} on failure. The operation is done \emph{in-place} |
| 522 | when \var{o1} supports it. This is the equivalent of the Python |
| 523 | statement \samp{\var{o1} **= \var{o2}} when o3 is \cdata{Py_None}, |
| 524 | or an in-place variant of \samp{pow(\var{o1}, \var{o2}, \var{o3})} |
| 525 | otherwise. If \var{o3} is to be ignored, pass \cdata{Py_None} in its |
| 526 | place (passing \NULL{} for \var{o3} would cause an illegal memory |
| 527 | access). |
| 528 | \end{cfuncdesc} |
| 529 | |
| 530 | \begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceLshift}{PyObject *o1, |
| 531 | PyObject *o2} |
| 532 | Returns the result of left shifting \var{o1} by \var{o2} on success, |
| 533 | or \NULL{} on failure. The operation is done \emph{in-place} when |
| 534 | \var{o1} supports it. This is the equivalent of the Python |
| 535 | statement \samp{\var{o1} <\code{<=} \var{o2}}. |
| 536 | \end{cfuncdesc} |
| 537 | |
| 538 | |
| 539 | \begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceRshift}{PyObject *o1, |
| 540 | PyObject *o2} |
| 541 | Returns the result of right shifting \var{o1} by \var{o2} on |
| 542 | success, or \NULL{} on failure. The operation is done |
| 543 | \emph{in-place} when \var{o1} supports it. This is the equivalent |
| 544 | of the Python statement \samp{\var{o1} >\code{>=} \var{o2}}. |
| 545 | \end{cfuncdesc} |
| 546 | |
| 547 | |
| 548 | \begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceAnd}{PyObject *o1, PyObject *o2} |
| 549 | Returns the ``bitwise and'' of \var{o1} and \var{o2} on success and |
| 550 | \NULL{} on failure. The operation is done \emph{in-place} when |
| 551 | \var{o1} supports it. This is the equivalent of the Python |
| 552 | statement \samp{\var{o1} \&= \var{o2}}. |
| 553 | \end{cfuncdesc} |
| 554 | |
| 555 | |
| 556 | \begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceXor}{PyObject *o1, PyObject *o2} |
| 557 | Returns the ``bitwise exclusive or'' of \var{o1} by \var{o2} on |
| 558 | success, or \NULL{} on failure. The operation is done |
| 559 | \emph{in-place} when \var{o1} supports it. This is the equivalent |
| 560 | of the Python statement \samp{\var{o1} \textasciicircum= \var{o2}}. |
| 561 | \end{cfuncdesc} |
| 562 | |
| 563 | \begin{cfuncdesc}{PyObject*}{PyNumber_InPlaceOr}{PyObject *o1, PyObject *o2} |
| 564 | Returns the ``bitwise or'' of \var{o1} and \var{o2} on success, or |
| 565 | \NULL{} on failure. The operation is done \emph{in-place} when |
| 566 | \var{o1} supports it. This is the equivalent of the Python |
| 567 | statement \samp{\var{o1} |= \var{o2}}. |
| 568 | \end{cfuncdesc} |
| 569 | |
| 570 | \begin{cfuncdesc}{int}{PyNumber_Coerce}{PyObject **p1, PyObject **p2} |
| 571 | This function takes the addresses of two variables of type |
| 572 | \ctype{PyObject*}. If the objects pointed to by \code{*\var{p1}} |
| 573 | and \code{*\var{p2}} have the same type, increment their reference |
| 574 | count and return \code{0} (success). If the objects can be converted |
| 575 | to a common numeric type, replace \code{*p1} and \code{*p2} by their |
| 576 | converted value (with 'new' reference counts), and return \code{0}. |
| 577 | If no conversion is possible, or if some other error occurs, return |
| 578 | \code{-1} (failure) and don't increment the reference counts. The |
| 579 | call \code{PyNumber_Coerce(\&o1, \&o2)} is equivalent to the Python |
| 580 | statement \samp{\var{o1}, \var{o2} = coerce(\var{o1}, \var{o2})}. |
| 581 | \bifuncindex{coerce} |
| 582 | \end{cfuncdesc} |
| 583 | |
| 584 | \begin{cfuncdesc}{PyObject*}{PyNumber_Int}{PyObject *o} |
| 585 | Returns the \var{o} converted to an integer object on success, or |
| 586 | \NULL{} on failure. This is the equivalent of the Python expression |
| 587 | \samp{int(\var{o})}.\bifuncindex{int} |
| 588 | \end{cfuncdesc} |
| 589 | |
| 590 | \begin{cfuncdesc}{PyObject*}{PyNumber_Long}{PyObject *o} |
| 591 | Returns the \var{o} converted to a long integer object on success, |
| 592 | or \NULL{} on failure. This is the equivalent of the Python |
| 593 | expression \samp{long(\var{o})}.\bifuncindex{long} |
| 594 | \end{cfuncdesc} |
| 595 | |
| 596 | \begin{cfuncdesc}{PyObject*}{PyNumber_Float}{PyObject *o} |
| 597 | Returns the \var{o} converted to a float object on success, or |
| 598 | \NULL{} on failure. This is the equivalent of the Python expression |
| 599 | \samp{float(\var{o})}.\bifuncindex{float} |
| 600 | \end{cfuncdesc} |
| 601 | |
| 602 | |
| 603 | \section{Sequence Protocol \label{sequence}} |
| 604 | |
| 605 | \begin{cfuncdesc}{int}{PySequence_Check}{PyObject *o} |
| 606 | Return \code{1} if the object provides sequence protocol, and |
| 607 | \code{0} otherwise. This function always succeeds. |
| 608 | \end{cfuncdesc} |
| 609 | |
| 610 | \begin{cfuncdesc}{int}{PySequence_Size}{PyObject *o} |
| 611 | Returns the number of objects in sequence \var{o} on success, and |
| 612 | \code{-1} on failure. For objects that do not provide sequence |
| 613 | protocol, this is equivalent to the Python expression |
| 614 | \samp{len(\var{o})}.\bifuncindex{len} |
| 615 | \end{cfuncdesc} |
| 616 | |
| 617 | \begin{cfuncdesc}{int}{PySequence_Length}{PyObject *o} |
| 618 | Alternate name for \cfunction{PySequence_Size()}. |
| 619 | \end{cfuncdesc} |
| 620 | |
| 621 | \begin{cfuncdesc}{PyObject*}{PySequence_Concat}{PyObject *o1, PyObject *o2} |
| 622 | Return the concatenation of \var{o1} and \var{o2} on success, and |
| 623 | \NULL{} on failure. This is the equivalent of the Python |
| 624 | expression \samp{\var{o1} + \var{o2}}. |
| 625 | \end{cfuncdesc} |
| 626 | |
| 627 | |
| 628 | \begin{cfuncdesc}{PyObject*}{PySequence_Repeat}{PyObject *o, int count} |
| 629 | Return the result of repeating sequence object \var{o} \var{count} |
| 630 | times, or \NULL{} on failure. This is the equivalent of the Python |
| 631 | expression \samp{\var{o} * \var{count}}. |
| 632 | \end{cfuncdesc} |
| 633 | |
| 634 | \begin{cfuncdesc}{PyObject*}{PySequence_InPlaceConcat}{PyObject *o1, |
| 635 | PyObject *o2} |
| 636 | Return the concatenation of \var{o1} and \var{o2} on success, and |
| 637 | \NULL{} on failure. The operation is done \emph{in-place} when |
| 638 | \var{o1} supports it. This is the equivalent of the Python |
| 639 | expression \samp{\var{o1} += \var{o2}}. |
| 640 | \end{cfuncdesc} |
| 641 | |
| 642 | |
| 643 | \begin{cfuncdesc}{PyObject*}{PySequence_InPlaceRepeat}{PyObject *o, int count} |
| 644 | Return the result of repeating sequence object \var{o} \var{count} |
| 645 | times, or \NULL{} on failure. The operation is done \emph{in-place} |
| 646 | when \var{o} supports it. This is the equivalent of the Python |
| 647 | expression \samp{\var{o} *= \var{count}}. |
| 648 | \end{cfuncdesc} |
| 649 | |
| 650 | |
| 651 | \begin{cfuncdesc}{PyObject*}{PySequence_GetItem}{PyObject *o, int i} |
| 652 | Return the \var{i}th element of \var{o}, or \NULL{} on failure. |
| 653 | This is the equivalent of the Python expression |
| 654 | \samp{\var{o}[\var{i}]}. |
| 655 | \end{cfuncdesc} |
| 656 | |
| 657 | |
| 658 | \begin{cfuncdesc}{PyObject*}{PySequence_GetSlice}{PyObject *o, int i1, int i2} |
| 659 | Return the slice of sequence object \var{o} between \var{i1} and |
| 660 | \var{i2}, or \NULL{} on failure. This is the equivalent of the |
| 661 | Python expression \samp{\var{o}[\var{i1}:\var{i2}]}. |
| 662 | \end{cfuncdesc} |
| 663 | |
| 664 | |
| 665 | \begin{cfuncdesc}{int}{PySequence_SetItem}{PyObject *o, int i, PyObject *v} |
| 666 | Assign object \var{v} to the \var{i}th element of \var{o}. Returns |
| 667 | \code{-1} on failure. This is the equivalent of the Python |
| 668 | statement \samp{\var{o}[\var{i}] = \var{v}}. |
| 669 | \end{cfuncdesc} |
| 670 | |
| 671 | \begin{cfuncdesc}{int}{PySequence_DelItem}{PyObject *o, int i} |
| 672 | Delete the \var{i}th element of object \var{o}. Returns \code{-1} |
| 673 | on failure. This is the equivalent of the Python statement |
| 674 | \samp{del \var{o}[\var{i}]}. |
| 675 | \end{cfuncdesc} |
| 676 | |
| 677 | \begin{cfuncdesc}{int}{PySequence_SetSlice}{PyObject *o, int i1, |
| 678 | int i2, PyObject *v} |
| 679 | Assign the sequence object \var{v} to the slice in sequence object |
| 680 | \var{o} from \var{i1} to \var{i2}. This is the equivalent of the |
| 681 | Python statement \samp{\var{o}[\var{i1}:\var{i2}] = \var{v}}. |
| 682 | \end{cfuncdesc} |
| 683 | |
| 684 | \begin{cfuncdesc}{int}{PySequence_DelSlice}{PyObject *o, int i1, int i2} |
| 685 | Delete the slice in sequence object \var{o} from \var{i1} to |
| 686 | \var{i2}. Returns \code{-1} on failure. This is the equivalent of |
| 687 | the Python statement \samp{del \var{o}[\var{i1}:\var{i2}]}. |
| 688 | \end{cfuncdesc} |
| 689 | |
| 690 | \begin{cfuncdesc}{PyObject*}{PySequence_Tuple}{PyObject *o} |
| 691 | Returns the \var{o} as a tuple on success, and \NULL{} on failure. |
| 692 | This is equivalent to the Python expression \samp{tuple(\var{o})}. |
| 693 | \bifuncindex{tuple} |
| 694 | \end{cfuncdesc} |
| 695 | |
| 696 | \begin{cfuncdesc}{int}{PySequence_Count}{PyObject *o, PyObject *value} |
| 697 | Return the number of occurrences of \var{value} in \var{o}, that is, |
| 698 | return the number of keys for which \code{\var{o}[\var{key}] == |
| 699 | \var{value}}. On failure, return \code{-1}. This is equivalent to |
| 700 | the Python expression \samp{\var{o}.count(\var{value})}. |
| 701 | \end{cfuncdesc} |
| 702 | |
| 703 | \begin{cfuncdesc}{int}{PySequence_Contains}{PyObject *o, PyObject *value} |
| 704 | Determine if \var{o} contains \var{value}. If an item in \var{o} is |
| 705 | equal to \var{value}, return \code{1}, otherwise return \code{0}. |
| 706 | On error, return \code{-1}. This is equivalent to the Python |
| 707 | expression \samp{\var{value} in \var{o}}. |
| 708 | \end{cfuncdesc} |
| 709 | |
| 710 | \begin{cfuncdesc}{int}{PySequence_Index}{PyObject *o, PyObject *value} |
| 711 | Return the first index \var{i} for which \code{\var{o}[\var{i}] == |
| 712 | \var{value}}. On error, return \code{-1}. This is equivalent to |
| 713 | the Python expression \samp{\var{o}.index(\var{value})}. |
| 714 | \end{cfuncdesc} |
| 715 | |
| 716 | \begin{cfuncdesc}{PyObject*}{PySequence_List}{PyObject *o} |
| 717 | Return a list object with the same contents as the arbitrary |
| 718 | sequence \var{o}. The returned list is guaranteed to be new. |
| 719 | \end{cfuncdesc} |
| 720 | |
| 721 | \begin{cfuncdesc}{PyObject*}{PySequence_Tuple}{PyObject *o} |
| 722 | Return a tuple object with the same contents as the arbitrary |
| 723 | sequence \var{o}. If \var{o} is a tuple, a new reference will be |
| 724 | returned, otherwise a tuple will be constructed with the appropriate |
| 725 | contents. |
| 726 | \end{cfuncdesc} |
| 727 | |
| 728 | \begin{cfuncdesc}{PyObject*}{PySequence_Fast}{PyObject *o, const char *m} |
| 729 | Returns the sequence \var{o} as a tuple, unless it is already a |
| 730 | tuple or list, in which case \var{o} is returned. Use |
| 731 | \cfunction{PySequence_Fast_GET_ITEM()} to access the members of the |
| 732 | result. Returns \NULL{} on failure. If the object is not a |
| 733 | sequence, raises \exception{TypeError} with \var{m} as the message |
| 734 | text. |
| 735 | \end{cfuncdesc} |
| 736 | |
| 737 | \begin{cfuncdesc}{PyObject*}{PySequence_Fast_GET_ITEM}{PyObject *o, int i} |
| 738 | Return the \var{i}th element of \var{o}, assuming that \var{o} was |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 739 | returned by \cfunction{PySequence_Fast()}, \var{o} is not \NULL, |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 740 | and that \var{i} is within bounds. |
| 741 | \end{cfuncdesc} |
| 742 | |
| 743 | \begin{cfuncdesc}{int}{PySequence_Fast_GET_SIZE}{PyObject *o} |
| 744 | Returns the length of \var{o}, assuming that \var{o} was |
| 745 | returned by \cfunction{PySequence_Fast()} and that \var{o} is |
Fred Drake | c37b65e | 2001-11-28 07:26:15 +0000 | [diff] [blame] | 746 | not \NULL. The size can also be gotten by calling |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 747 | \cfunction{PySequence_Size()} on \var{o}, but |
| 748 | \cfunction{PySequence_Fast_GET_SIZE()} is faster because it can |
| 749 | assume \var{o} is a list or tuple. |
Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 750 | \end{cfuncdesc} |
| 751 | |
| 752 | |
| 753 | \section{Mapping Protocol \label{mapping}} |
| 754 | |
| 755 | \begin{cfuncdesc}{int}{PyMapping_Check}{PyObject *o} |
| 756 | Return \code{1} if the object provides mapping protocol, and |
| 757 | \code{0} otherwise. This function always succeeds. |
| 758 | \end{cfuncdesc} |
| 759 | |
| 760 | |
| 761 | \begin{cfuncdesc}{int}{PyMapping_Length}{PyObject *o} |
| 762 | Returns the number of keys in object \var{o} on success, and |
| 763 | \code{-1} on failure. For objects that do not provide mapping |
| 764 | protocol, this is equivalent to the Python expression |
| 765 | \samp{len(\var{o})}.\bifuncindex{len} |
| 766 | \end{cfuncdesc} |
| 767 | |
| 768 | |
| 769 | \begin{cfuncdesc}{int}{PyMapping_DelItemString}{PyObject *o, char *key} |
| 770 | Remove the mapping for object \var{key} from the object \var{o}. |
| 771 | Return \code{-1} on failure. This is equivalent to the Python |
| 772 | statement \samp{del \var{o}[\var{key}]}. |
| 773 | \end{cfuncdesc} |
| 774 | |
| 775 | |
| 776 | \begin{cfuncdesc}{int}{PyMapping_DelItem}{PyObject *o, PyObject *key} |
| 777 | Remove the mapping for object \var{key} from the object \var{o}. |
| 778 | Return \code{-1} on failure. This is equivalent to the Python |
| 779 | statement \samp{del \var{o}[\var{key}]}. |
| 780 | \end{cfuncdesc} |
| 781 | |
| 782 | |
| 783 | \begin{cfuncdesc}{int}{PyMapping_HasKeyString}{PyObject *o, char *key} |
| 784 | On success, return \code{1} if the mapping object has the key |
| 785 | \var{key} and \code{0} otherwise. This is equivalent to the Python |
| 786 | expression \samp{\var{o}.has_key(\var{key})}. This function always |
| 787 | succeeds. |
| 788 | \end{cfuncdesc} |
| 789 | |
| 790 | |
| 791 | \begin{cfuncdesc}{int}{PyMapping_HasKey}{PyObject *o, PyObject *key} |
| 792 | Return \code{1} if the mapping object has the key \var{key} and |
| 793 | \code{0} otherwise. This is equivalent to the Python expression |
| 794 | \samp{\var{o}.has_key(\var{key})}. This function always succeeds. |
| 795 | \end{cfuncdesc} |
| 796 | |
| 797 | |
| 798 | \begin{cfuncdesc}{PyObject*}{PyMapping_Keys}{PyObject *o} |
| 799 | On success, return a list of the keys in object \var{o}. On |
| 800 | failure, return \NULL. This is equivalent to the Python expression |
| 801 | \samp{\var{o}.keys()}. |
| 802 | \end{cfuncdesc} |
| 803 | |
| 804 | |
| 805 | \begin{cfuncdesc}{PyObject*}{PyMapping_Values}{PyObject *o} |
| 806 | On success, return a list of the values in object \var{o}. On |
| 807 | failure, return \NULL. This is equivalent to the Python expression |
| 808 | \samp{\var{o}.values()}. |
| 809 | \end{cfuncdesc} |
| 810 | |
| 811 | |
| 812 | \begin{cfuncdesc}{PyObject*}{PyMapping_Items}{PyObject *o} |
| 813 | On success, return a list of the items in object \var{o}, where each |
| 814 | item is a tuple containing a key-value pair. On failure, return |
| 815 | \NULL. This is equivalent to the Python expression |
| 816 | \samp{\var{o}.items()}. |
| 817 | \end{cfuncdesc} |
| 818 | |
| 819 | |
| 820 | \begin{cfuncdesc}{PyObject*}{PyMapping_GetItemString}{PyObject *o, char *key} |
| 821 | Return element of \var{o} corresponding to the object \var{key} or |
| 822 | \NULL{} on failure. This is the equivalent of the Python expression |
| 823 | \samp{\var{o}[\var{key}]}. |
| 824 | \end{cfuncdesc} |
| 825 | |
| 826 | \begin{cfuncdesc}{int}{PyMapping_SetItemString}{PyObject *o, char *key, |
| 827 | PyObject *v} |
| 828 | Map the object \var{key} to the value \var{v} in object \var{o}. |
| 829 | Returns \code{-1} on failure. This is the equivalent of the Python |
| 830 | statement \samp{\var{o}[\var{key}] = \var{v}}. |
| 831 | \end{cfuncdesc} |
| 832 | |
| 833 | |
| 834 | \section{Iterator Protocol \label{iterator}} |
| 835 | |
| 836 | \versionadded{2.2} |
| 837 | |
| 838 | There are only a couple of functions specifically for working with |
| 839 | iterators. |
| 840 | |
| 841 | \begin{cfuncdesc}{int}{PyIter_Check}{PyObject *o} |
| 842 | Return true if the object \var{o} supports the iterator protocol. |
| 843 | \end{cfuncdesc} |
| 844 | |
| 845 | \begin{cfuncdesc}{PyObject*}{PyIter_Next}{PyObject *o} |
| 846 | Return the next value from the iteration \var{o}. If the object is |
| 847 | an iterator, this retrieves the next value from the iteration, and |
| 848 | returns \NULL{} with no exception set if there are no remaining |
| 849 | items. If the object is not an iterator, \exception{TypeError} is |
| 850 | raised, or if there is an error in retrieving the item, returns |
| 851 | \NULL{} and passes along the exception. |
| 852 | \end{cfuncdesc} |
| 853 | |
| 854 | To write a loop which iterates over an iterator, the C code should |
| 855 | look something like this: |
| 856 | |
| 857 | \begin{verbatim} |
| 858 | PyObject *iterator = ...; |
| 859 | PyObject *item; |
| 860 | |
| 861 | while (item = PyIter_Next(iter)) { |
| 862 | /* do something with item */ |
| 863 | } |
| 864 | if (PyErr_Occurred()) { |
| 865 | /* propogate error */ |
| 866 | } |
| 867 | else { |
| 868 | /* continue doing useful work */ |
| 869 | } |
Jeremy Hylton | 89c3a22 | 2001-11-09 21:59:42 +0000 | [diff] [blame] | 870 | |
Fred Drake | 94ead57 | 2001-11-09 23:34:26 +0000 | [diff] [blame] | 871 | \section{Buffer Protocol \label{abstract-buffer}} |
Jeremy Hylton | 89c3a22 | 2001-11-09 21:59:42 +0000 | [diff] [blame] | 872 | |
| 873 | \begin{cfuncdesc}{int}{PyObject_AsCharBuffer}{PyObject *obj, |
Fred Drake | 94ead57 | 2001-11-09 23:34:26 +0000 | [diff] [blame] | 874 | const char **buffer, |
| 875 | int *buffer_len} |
Jeremy Hylton | 89c3a22 | 2001-11-09 21:59:42 +0000 | [diff] [blame] | 876 | Returns a pointer to a read-only memory location useable as character- |
| 877 | based input. The \var{obj} argument must support the single-segment |
| 878 | character buffer interface. On success, returns \code{1}, sets |
Thomas Heller | bfeeeee | 2001-11-12 07:46:31 +0000 | [diff] [blame] | 879 | \var{buffer} to the memory location and \var{buffer_len} to the buffer |
Jeremy Hylton | 89c3a22 | 2001-11-09 21:59:42 +0000 | [diff] [blame] | 880 | length. Returns \code{0} and sets a \exception{TypeError} on error. |
Fred Drake | 94ead57 | 2001-11-09 23:34:26 +0000 | [diff] [blame] | 881 | \versionadded{1.6} |
Jeremy Hylton | 89c3a22 | 2001-11-09 21:59:42 +0000 | [diff] [blame] | 882 | \end{cfuncdesc} |
| 883 | |
| 884 | \begin{cfuncdesc}{int}{PyObject_AsReadBuffer}{PyObject *obj, |
Fred Drake | 94ead57 | 2001-11-09 23:34:26 +0000 | [diff] [blame] | 885 | const char **buffer, |
| 886 | int *buffer_len} |
Jeremy Hylton | 89c3a22 | 2001-11-09 21:59:42 +0000 | [diff] [blame] | 887 | Returns a pointer to a read-only memory location containing |
| 888 | arbitrary data. The \var{obj} argument must support the |
| 889 | single-segment readable buffer interface. On success, returns |
Thomas Heller | bfeeeee | 2001-11-12 07:46:31 +0000 | [diff] [blame] | 890 | \code{1}, sets \var{buffer} to the memory location and \var{buffer_len} |
Jeremy Hylton | 89c3a22 | 2001-11-09 21:59:42 +0000 | [diff] [blame] | 891 | to the buffer length. Returns \code{0} and sets a |
| 892 | \exception{TypeError} on error. |
Fred Drake | 94ead57 | 2001-11-09 23:34:26 +0000 | [diff] [blame] | 893 | \versionadded{1.6} |
Jeremy Hylton | 89c3a22 | 2001-11-09 21:59:42 +0000 | [diff] [blame] | 894 | \end{cfuncdesc} |
| 895 | |
| 896 | \begin{cfuncdesc}{int}{PyObject_CheckReadBuffer}{PyObject *o} |
| 897 | Returns \code{1} if \var{o} supports the single-segment readable |
| 898 | buffer interface. Otherwise returns \code{0}. |
Fred Drake | 94ead57 | 2001-11-09 23:34:26 +0000 | [diff] [blame] | 899 | \versionadded{2.2} |
Jeremy Hylton | 89c3a22 | 2001-11-09 21:59:42 +0000 | [diff] [blame] | 900 | \enc{cfuncdesc} |
| 901 | |
| 902 | \begin{cfuncdesc}{int}{PyObject_AsWriteBuffer}{PyObject *obj, |
Fred Drake | 94ead57 | 2001-11-09 23:34:26 +0000 | [diff] [blame] | 903 | const char **buffer, |
| 904 | int *buffer_len} |
Jeremy Hylton | 89c3a22 | 2001-11-09 21:59:42 +0000 | [diff] [blame] | 905 | Returns a pointer to a writeable memory location. The \var{obj} |
| 906 | argument must support the single-segment, character buffer |
| 907 | interface. On success, returns \code{1}, sets \var{buffer} to the |
Thomas Heller | bfeeeee | 2001-11-12 07:46:31 +0000 | [diff] [blame] | 908 | memory location and \var{buffer_len} to the buffer length. Returns |
Jeremy Hylton | 89c3a22 | 2001-11-09 21:59:42 +0000 | [diff] [blame] | 909 | \code{0} and sets a \exception{TypeError} on error. |
Fred Drake | 94ead57 | 2001-11-09 23:34:26 +0000 | [diff] [blame] | 910 | \versionadded{1.6} |
Jeremy Hylton | 89c3a22 | 2001-11-09 21:59:42 +0000 | [diff] [blame] | 911 | \end{cfuncdesc} |
| 912 | |
Fred Drake | 3adf79e | 2001-10-12 19:01:43 +0000 | [diff] [blame] | 913 | \end{verbatim} |