Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1 | \documentstyle[twoside,11pt,myformat]{report} |
| 2 | |
| 3 | % NOTE: this file controls which chapters/sections of the library |
| 4 | % manual are actually printed. It is easy to customize your manual |
| 5 | % by commenting out sections that you're not interested in. |
| 6 | |
| 7 | \title{Python-C API Reference} |
| 8 | |
| 9 | \input{boilerplate} |
| 10 | |
| 11 | \makeindex % tell \index to actually write the .idx file |
| 12 | |
| 13 | |
| 14 | \begin{document} |
| 15 | |
| 16 | \pagenumbering{roman} |
| 17 | |
| 18 | \maketitle |
| 19 | |
| 20 | \input{copyright} |
| 21 | |
| 22 | \begin{abstract} |
| 23 | |
| 24 | \noindent |
| 25 | This manual documents the API used by C (or C++) programmers who want |
| 26 | to write extension modules or embed Python. It is a companion to |
| 27 | ``Extending and Embedding the Python Interpreter'', which describes |
| 28 | the general principles of extension writing but does not document the |
| 29 | API functions in detail. |
| 30 | |
| 31 | \end{abstract} |
| 32 | |
| 33 | \pagebreak |
| 34 | |
| 35 | { |
| 36 | \parskip = 0mm |
| 37 | \tableofcontents |
| 38 | } |
| 39 | |
| 40 | \pagebreak |
| 41 | |
| 42 | \pagenumbering{arabic} |
| 43 | |
| 44 | |
| 45 | \chapter{Introduction} |
| 46 | |
| 47 | From the viewpoint of of C access to Python services, we have: |
| 48 | |
| 49 | \begin{enumerate} |
| 50 | |
| 51 | \item "Very high level layer": two or three functions that let you |
| 52 | exec or eval arbitrary Python code given as a string in a module whose |
| 53 | name is given, passing C values in and getting C values out using |
| 54 | mkvalue/getargs style format strings. This does not require the user |
| 55 | to declare any variables of type \code{PyObject *}. This should be |
| 56 | enough to write a simple application that gets Python code from the |
| 57 | user, execs it, and returns the output or errors. |
| 58 | |
| 59 | \item "Abstract objects layer": which is the subject of this chapter. |
| 60 | It has many functions operating on objects, and lest you do many |
| 61 | things from C that you can also write in Python, without going through |
| 62 | the Python parser. |
| 63 | |
| 64 | \item "Concrete objects layer": This is the public type-dependent |
| 65 | interface provided by the standard built-in types, such as floats, |
| 66 | strings, and lists. This interface exists and is currently documented |
| 67 | by the collection of include files provides with the Python |
| 68 | distributions. |
| 69 | |
| 70 | \begin{enumerate} |
| 71 | |
| 72 | From the point of view of Python accessing services provided by C |
| 73 | modules: |
| 74 | |
| 75 | \end{enumerate} |
| 76 | |
| 77 | \item[4] "Python module interface": this interface consist of the basic |
| 78 | routines used to define modules and their members. Most of the |
| 79 | current extensions-writing guide deals with this interface. |
| 80 | |
| 81 | \item[5] "Built-in object interface": this is the interface that a new |
| 82 | built-in type must provide and the mechanisms and rules that a |
| 83 | developer of a new built-in type must use and follow. |
| 84 | |
| 85 | \end{enumerate} |
| 86 | |
| 87 | The Python C API provides four groups of operations on objects, |
| 88 | corresponding to the same operations in the Python language: object, |
| 89 | numeric, sequence, and mapping. Each protocol consists of a |
| 90 | collection of related operations. If an operation that is not |
| 91 | provided by a particular type is invoked, then the standard exception |
| 92 | \code{TypeError} is raised with a operation name as an argument. |
| 93 | |
| 94 | In addition, for convenience this interface defines a set of |
| 95 | constructors for building objects of built-in types. This is needed |
| 96 | so new objects can be returned from C functions that otherwise treat |
| 97 | objects generically. |
| 98 | |
| 99 | \section{Reference Counting} |
| 100 | |
| 101 | For most of the functions in the Python-C API, if a function retains a |
| 102 | reference to a Python object passed as an argument, then the function |
| 103 | will increase the reference count of the object. It is unnecessary |
| 104 | for the caller to increase the reference count of an argument in |
| 105 | anticipation of the object's retention. |
| 106 | |
| 107 | Usually, Python objects returned from functions should be treated as |
| 108 | new objects. Functions that return objects assume that the caller |
| 109 | will retain a reference and the reference count of the object has |
| 110 | already been incremented to account for this fact. A caller that does |
| 111 | not retain a reference to an object that is returned from a function |
| 112 | must decrement the reference count of the object (using |
| 113 | \code{Py_DECREF()}) to prevent memory leaks. |
| 114 | |
| 115 | Exceptions to these rules will be noted with the individual functions. |
| 116 | |
| 117 | \section{Include Files} |
| 118 | |
| 119 | All function, type and macro definitions needed to use the Python-C |
| 120 | API are included in your code by the following line: |
| 121 | |
| 122 | \code{\#include "Python.h"} |
| 123 | |
| 124 | This implies inclusion of the following standard header files: |
| 125 | stdio.h, string.h, errno.h, and stdlib.h (if available). |
| 126 | |
| 127 | All user visible names defined by Python.h (except those defined by |
| 128 | the included standard headers) have one of the prefixes \code{Py} or |
| 129 | \code{_Py}. Names beginning with \code{_Py} are for internal use |
| 130 | only. |
| 131 | |
| 132 | |
| 133 | \chapter{Initialization and Shutdown of an Embedded Python Interpreter} |
| 134 | |
| 135 | When embedding the Python interpreter in a C or C++ program, the |
| 136 | interpreter must be initialized. |
| 137 | |
| 138 | \begin{cfuncdesc}{void}{PyInitialize}{} |
| 139 | This function initializes the interpreter. It must be called before |
| 140 | any interaction with the interpreter takes place. If it is called |
| 141 | more than once, the second and further calls have no effect. |
| 142 | |
| 143 | The function performs the following tasks: create an environment in |
| 144 | which modules can be imported and Python code can be executed; |
| 145 | initialize the \code{__builtin__} module; initialize the \code{sys} |
| 146 | module; initialize \code{sys.path}; initialize signal handling; and |
| 147 | create the empty \code{__main__} module. |
| 148 | |
| 149 | In the current system, there is no way to undo all these |
| 150 | initializations or to create additional interpreter environments. |
| 151 | \end{cfuncdesc} |
| 152 | |
| 153 | \begin{cfuncdesc}{int}{Py_AtExit}{void (*func) ()} |
| 154 | Register a cleanup function to be called when Python exits. The |
| 155 | cleanup function will be called with no arguments and should return no |
| 156 | value. At most 32 cleanup functions can be registered. When the |
| 157 | registration is successful, \code{Py_AtExit} returns 0; on failure, it |
| 158 | returns -1. Each cleanup function will be called t most once. The |
| 159 | cleanup function registered last is called first. |
| 160 | \end{cfuncdesc} |
| 161 | |
| 162 | \begin{cfuncdesc}{void}{Py_Exit}{int status} |
| 163 | Exit the current process. This calls \code{Py_Cleanup()} (see next |
| 164 | item) and performs additional cleanup (under some circumstances it |
| 165 | will attempt to delete all modules), and then calls the standard C |
| 166 | library function \code{exit(status)}. |
| 167 | \end{cfuncdesc} |
| 168 | |
| 169 | \begin{cfuncdesc}{void}{Py_Cleanup}{} |
| 170 | Perform some of the cleanup that \code{Py_Exit} performs, but don't |
| 171 | exit the process. In particular, this invokes the user's |
| 172 | \code{sys.exitfunc} function (if defined at all), and it invokes the |
| 173 | cleanup functions registered with \code{Py_AtExit()}, in reverse order |
| 174 | of their registration. |
| 175 | \end{cfuncdesc} |
| 176 | |
| 177 | \begin{cfuncdesc}{void}{Py_FatalError}{char *message} |
| 178 | Print a fatal error message and die. No cleanup is performed. This |
| 179 | function should only be invoked when a condition is detected that |
| 180 | would make it dangerous to continue using the Python interpreter; |
| 181 | e.g., when the object administration appears to be corrupted. |
| 182 | \end{cfuncdesc} |
| 183 | |
| 184 | \begin{cfuncdesc}{void}{PyImport_Init}{} |
| 185 | Initialize the module table. For internal use only. |
| 186 | \end{cfuncdesc} |
| 187 | |
| 188 | \begin{cfuncdesc}{void}{PyImport_Cleanup}{} |
| 189 | Empty the module table. For internal use only. |
| 190 | \end{cfuncdesc} |
| 191 | |
| 192 | \begin{cfuncdesc}{void}{PyBuiltin_Init}{} |
| 193 | Initialize the \code{__builtin__} module. For internal use only. |
| 194 | \end{cfuncdesc} |
| 195 | |
| 196 | |
| 197 | \chapter{Reference Counting} |
| 198 | |
| 199 | The functions in this chapter are used for managing reference counts |
| 200 | of Python objects. |
| 201 | |
| 202 | \begin{cfuncdesc}{void}{Py_INCREF}{PyObject *o} |
| 203 | Increment the reference count for object \code{o}. The object must |
| 204 | not be \NULL{}; if you aren't sure that it isn't \NULL{}, use |
| 205 | \code{Py_XINCREF()}. |
| 206 | \end{cfuncdesc} |
| 207 | |
| 208 | \begin{cfuncdesc}{void}{Py_XINCREF}{PyObject *o} |
| 209 | Increment the reference count for object \code{o}. The object may be |
| 210 | \NULL{}, in which case the function has no effect. |
| 211 | \end{cfuncdesc} |
| 212 | |
| 213 | \begin{cfuncdesc}{void}{Py_DECREF}{PyObject *o} |
| 214 | Decrement the reference count for object \code{o}. The object must |
| 215 | not be \NULL{}; if you aren't sure that it isn't \NULL{}, use |
| 216 | \code{Py_XDECREF()}. If the reference count reaches zero, the object's |
| 217 | type's deallocation function (which must not be \NULL{}) is invoked. |
| 218 | |
| 219 | \strong{Warning:} The deallocation function can cause arbitrary Python |
| 220 | code to be invoked (e.g. when a class instance with a \code{__del__()} |
| 221 | method is deallocated). While exceptions in such code are not |
| 222 | propagated, the executed code has free access to all Python global |
| 223 | variables. This means that any object that is reachable from a global |
| 224 | variable should be in a consistent state before \code{Py_DECREF()} is |
| 225 | invoked. For example, code to delete an object from a list should |
| 226 | copy a reference to the deleted object in a temporary variable, update |
| 227 | the list data structure, and then call \code{Py_DECREF()} for the |
| 228 | temporary variable. |
| 229 | \end{cfuncdesc} |
| 230 | |
| 231 | \begin{cfuncdesc}{void}{Py_XDECREF}{PyObject *o} |
| 232 | Decrement the reference count for object \code{o}.The object may be |
| 233 | \NULL{}, in which case the function has no effect; otherwise the |
| 234 | effect is the same as for \code{Py_DECREF()}, and the same warning |
| 235 | applies. |
| 236 | \end{cfuncdesc} |
| 237 | |
| 238 | |
| 239 | \chapter{Exception Handling} |
| 240 | |
| 241 | The functions in this chapter will let you handle and raise Python |
| 242 | exceptions. |
| 243 | |
| 244 | \begin{cfuncdesc}{void}{PyErr_Print}{} |
| 245 | \end{cfuncdesc} |
| 246 | |
| 247 | |
| 248 | \chapter{Utilities} |
| 249 | |
| 250 | The functions in this chapter perform various utility tasks, such as |
| 251 | parsing function arguments and constructing Python values from C |
| 252 | values. |
| 253 | |
| 254 | \begin{cfuncdesc}{int}{Py_FdIsInteractive}{FILE *fp, char *filename} |
| 255 | Return true (nonzero) if the standard I/O file \code{fp} with name |
| 256 | \code{filename} is deemed interactive. This is the case for files for |
| 257 | which \code{isatty(fileno(fp))} is true. If the global flag |
| 258 | \code{Py_InteractiveFlag} is true, this function also returns true if |
| 259 | the \code{name} pointer is \NULL{} or if the name is equal to one of |
| 260 | the strings \code{"<stdin>"} or \code{"???"}. |
| 261 | \end{cfuncdesc} |
| 262 | |
| 263 | \begin{cfuncdesc}{long}{PyOS_GetLastModificationTime}{char *filename} |
| 264 | Return the time of last modification of the file \code{filename}. |
| 265 | The result is encoded in the same way as the timestamp returned by |
| 266 | the standard C library function \code{time()}. |
| 267 | \end{cfuncdesc} |
| 268 | |
| 269 | |
| 270 | \chapter{Debugging} |
| 271 | |
| 272 | XXX Explain Py_DEBUG, Py_TRACE_REFS, Py_REF_DEBUG. |
| 273 | |
| 274 | |
| 275 | \chapter{The Very High Level Layer} |
| 276 | |
| 277 | The functions in this chapter will let you execute Python source code |
| 278 | given in a file or a buffer, but they will not let you interact in a |
| 279 | more detailed way with the interpreter. |
| 280 | |
| 281 | |
| 282 | \chapter{Abstract Objects Layer} |
| 283 | |
| 284 | The functions in this chapter interact with Python objects regardless |
| 285 | of their type, or with wide classes of object types (e.g. all |
| 286 | numerical types, or all sequence types). When used on object types |
| 287 | for which they do not apply, they will flag a Python exception. |
| 288 | |
| 289 | \section{Object Protocol} |
| 290 | |
| 291 | \begin{cfuncdesc}{int}{PyObject_Print}{PyObject *o, FILE *fp, int flags} |
| 292 | Print an object \code{o}, on file \code{fp}. Returns -1 on error |
| 293 | The flags argument is used to enable certain printing |
| 294 | options. The only option currently supported is \code{Py_Print_RAW}. |
| 295 | \end{cfuncdesc} |
| 296 | |
| 297 | \begin{cfuncdesc}{int}{PyObject_HasAttrString}{PyObject *o, char *attr_name} |
| 298 | Returns 1 if o has the attribute attr_name, and 0 otherwise. |
| 299 | This is equivalent to the Python expression: |
| 300 | \code{hasattr(o,attr_name)}. |
| 301 | This function always succeeds. |
| 302 | \end{cfuncdesc} |
| 303 | |
| 304 | \begin{cfuncdesc}{PyObject*}{PyObject_GetAttrString}{PyObject *o, char *attr_name} |
| 305 | Retrieve an attributed named attr_name form object o. |
| 306 | Returns the attribute value on success, or \NULL{} on failure. |
| 307 | This is the equivalent of the Python expression: \code{o.attr_name}. |
| 308 | \end{cfuncdesc} |
| 309 | |
| 310 | |
| 311 | \begin{cfuncdesc}{int}{PyObject_HasAttr}{PyObject *o, PyObject *attr_name} |
| 312 | Returns 1 if o has the attribute attr_name, and 0 otherwise. |
| 313 | This is equivalent to the Python expression: |
| 314 | \code{hasattr(o,attr_name)}. |
| 315 | This function always succeeds. |
| 316 | \end{cfuncdesc} |
| 317 | |
| 318 | |
| 319 | \begin{cfuncdesc}{PyObject*}{PyObject_GetAttr}{PyObject *o, PyObject *attr_name} |
| 320 | Retrieve an attributed named attr_name form object o. |
| 321 | Returns the attribute value on success, or \NULL{} on failure. |
| 322 | This is the equivalent of the Python expression: o.attr_name. |
| 323 | \end{cfuncdesc} |
| 324 | |
| 325 | |
| 326 | \begin{cfuncdesc}{int}{PyObject_SetAttrString}{PyObject *o, char *attr_name, PyObject *v} |
| 327 | Set the value of the attribute named \code{attr_name}, for object \code{o}, |
| 328 | to the value \code{v}. Returns -1 on failure. This is |
| 329 | the equivalent of the Python statement: \code{o.attr_name=v}. |
| 330 | \end{cfuncdesc} |
| 331 | |
| 332 | |
| 333 | \begin{cfuncdesc}{int}{PyObject_SetAttr}{PyObject *o, PyObject *attr_name, PyObject *v} |
| 334 | Set the value of the attribute named \code{attr_name}, for |
| 335 | object \code{o}, |
| 336 | to the value \code{v}. Returns -1 on failure. This is |
| 337 | the equivalent of the Python statement: \code{o.attr_name=v}. |
| 338 | \end{cfuncdesc} |
| 339 | |
| 340 | |
| 341 | \begin{cfuncdesc}{int}{PyObject_DelAttrString}{PyObject *o, char *attr_name} |
| 342 | Delete attribute named \code{attr_name}, for object \code{o}. Returns -1 on |
| 343 | failure. This is the equivalent of the Python |
| 344 | statement: \code{del o.attr_name}. |
| 345 | \end{cfuncdesc} |
| 346 | |
| 347 | |
| 348 | \begin{cfuncdesc}{int}{PyObject_DelAttr}{PyObject *o, PyObject *attr_name} |
| 349 | Delete attribute named \code{attr_name}, for object \code{o}. Returns -1 on |
| 350 | failure. This is the equivalent of the Python |
| 351 | statement: \code{del o.attr_name}. |
| 352 | \end{cfuncdesc} |
| 353 | |
| 354 | |
| 355 | \begin{cfuncdesc}{int}{PyObject_Cmp}{PyObject *o1, PyObject *o2, int *result} |
| 356 | Compare the values of \code{o1} and \code{o2} using a routine provided by |
| 357 | \code{o1}, if one exists, otherwise with a routine provided by \code{o2}. |
| 358 | The result of the comparison is returned in \code{result}. Returns |
| 359 | -1 on failure. This is the equivalent of the Python |
| 360 | statement: \code{result=cmp(o1,o2)}. |
| 361 | \end{cfuncdesc} |
| 362 | |
| 363 | |
| 364 | \begin{cfuncdesc}{int}{PyObject_Compare}{PyObject *o1, PyObject *o2} |
| 365 | Compare the values of \code{o1} and \code{o2} using a routine provided by |
| 366 | \code{o1}, if one exists, otherwise with a routine provided by \code{o2}. |
| 367 | Returns the result of the comparison on success. On error, |
| 368 | the value returned is undefined. This is equivalent to the |
| 369 | Python expression: \code{cmp(o1,o2)}. |
| 370 | \end{cfuncdesc} |
| 371 | |
| 372 | |
| 373 | \begin{cfuncdesc}{PyObject*}{PyObject_Repr}{PyObject *o} |
| 374 | Compute the string representation of object, \code{o}. Returns the |
| 375 | string representation on success, \NULL{} on failure. This is |
| 376 | the equivalent of the Python expression: \code{repr(o)}. |
| 377 | Called by the \code{repr()} built-in function and by reverse quotes. |
| 378 | \end{cfuncdesc} |
| 379 | |
| 380 | |
| 381 | \begin{cfuncdesc}{PyObject*}{PyObject_Str}{PyObject *o} |
| 382 | Compute the string representation of object, \code{o}. Returns the |
| 383 | string representation on success, \NULL{} on failure. This is |
| 384 | the equivalent of the Python expression: \code{str(o)}. |
| 385 | Called by the \code{str()} built-in function and by the \code{print} |
| 386 | statement. |
| 387 | \end{cfuncdesc} |
| 388 | |
| 389 | |
| 390 | \begin{cfuncdesc}{int}{PyCallable_Check}{PyObject *o} |
| 391 | Determine if the object \code{o}, is callable. Return 1 if the |
| 392 | object is callable and 0 otherwise. |
| 393 | This function always succeeds. |
| 394 | \end{cfuncdesc} |
| 395 | |
| 396 | |
| 397 | \begin{cfuncdesc}{PyObject*}{PyObject_CallObject}{PyObject *callable_object, PyObject *args} |
| 398 | Call a callable Python object \code{callable_object}, with |
| 399 | arguments given by the tuple \code{args}. If no arguments are |
| 400 | needed, then args may be \NULL{}. Returns the result of the |
| 401 | call on success, or \NULL{} on failure. This is the equivalent |
| 402 | of the Python expression: \code{apply(o, args)}. |
| 403 | \end{cfuncdesc} |
| 404 | |
| 405 | \begin{cfuncdesc}{PyObject*}{PyObject_CallFunction}{PyObject *callable_object, char *format, ...} |
| 406 | Call a callable Python object \code{callable_object}, with a |
| 407 | variable number of C arguments. The C arguments are described |
| 408 | using a mkvalue-style format string. The format may be \NULL{}, |
| 409 | indicating that no arguments are provided. Returns the |
| 410 | result of the call on success, or \NULL{} on failure. This is |
| 411 | the equivalent of the Python expression: \code{apply(o,args)}. |
| 412 | \end{cfuncdesc} |
| 413 | |
| 414 | |
| 415 | \begin{cfuncdesc}{PyObject*}{PyObject_CallMethod}{PyObject *o, char *m, char *format, ...} |
| 416 | Call the method named \code{m} of object \code{o} with a variable number of |
| 417 | C arguments. The C arguments are described by a mkvalue |
| 418 | format string. The format may be \NULL{}, indicating that no |
| 419 | arguments are provided. Returns the result of the call on |
| 420 | success, or \NULL{} on failure. This is the equivalent of the |
| 421 | Python expression: \code{o.method(args)}. |
| 422 | Note that Special method names, such as "\code{__add__}", |
| 423 | "\code{__getitem__}", and so on are not supported. The specific |
| 424 | abstract-object routines for these must be used. |
| 425 | \end{cfuncdesc} |
| 426 | |
| 427 | |
| 428 | \begin{cfuncdesc}{int}{PyObject_Hash}{PyObject *o} |
| 429 | Compute and return the hash value of an object \code{o}. On |
| 430 | failure, return -1. This is the equivalent of the Python |
| 431 | expression: \code{hash(o)}. |
| 432 | \end{cfuncdesc} |
| 433 | |
| 434 | |
| 435 | \begin{cfuncdesc}{int}{PyObject_IsTrue}{PyObject *o} |
| 436 | Returns 1 if the object \code{o} is considered to be true, and |
| 437 | 0 otherwise. This is equivalent to the Python expression: |
| 438 | \code{not not o}. |
| 439 | This function always succeeds. |
| 440 | \end{cfuncdesc} |
| 441 | |
| 442 | |
| 443 | \begin{cfuncdesc}{PyObject*}{PyObject_Type}{PyObject *o} |
| 444 | On success, returns a type object corresponding to the object |
| 445 | type of object \code{o}. On failure, returns \NULL{}. This is |
| 446 | equivalent to the Python expression: \code{type(o)}. |
| 447 | \end{cfuncdesc} |
| 448 | |
| 449 | \begin{cfuncdesc}{int}{PyObject_Length}{PyObject *o} |
| 450 | Return the length of object \code{o}. If the object \code{o} provides |
| 451 | both sequence and mapping protocols, the sequence length is |
| 452 | returned. On error, -1 is returned. This is the equivalent |
| 453 | to the Python expression: \code{len(o)}. |
| 454 | \end{cfuncdesc} |
| 455 | |
| 456 | |
| 457 | \begin{cfuncdesc}{PyObject*}{PyObject_GetItem}{PyObject *o, PyObject *key} |
| 458 | Return element of \code{o} corresponding to the object \code{key} or \NULL{} |
| 459 | on failure. This is the equivalent of the Python expression: |
| 460 | \code{o[key]}. |
| 461 | \end{cfuncdesc} |
| 462 | |
| 463 | |
| 464 | \begin{cfuncdesc}{int}{PyObject_SetItem}{PyObject *o, PyObject *key, PyObject *v} |
| 465 | Map the object \code{key} to the value \code{v}. |
| 466 | Returns -1 on failure. This is the equivalent |
| 467 | of the Python statement: \code{o[key]=v}. |
| 468 | \end{cfuncdesc} |
| 469 | |
| 470 | |
| 471 | \begin{cfuncdesc}{int}{PyObject_DelItem}{PyObject *o, PyObject *key, PyObject *v} |
| 472 | Delete the mapping for \code{key} from \code{*o}. Returns -1 |
| 473 | on failure. |
| 474 | This is the equivalent of the Python statement: del o[key]. |
| 475 | \end{cfuncdesc} |
| 476 | |
| 477 | |
| 478 | \section{Number Protocol} |
| 479 | |
| 480 | \begin{cfuncdesc}{int}{PyNumber_Check}{PyObject *o} |
| 481 | Returns 1 if the object \code{o} provides numeric protocols, and |
| 482 | false otherwise. |
| 483 | This function always succeeds. |
| 484 | \end{cfuncdesc} |
| 485 | |
| 486 | |
| 487 | \begin{cfuncdesc}{PyObject*}{PyNumber_Add}{PyObject *o1, PyObject *o2} |
| 488 | Returns the result of adding \code{o1} and \code{o2}, or null on failure. |
| 489 | This is the equivalent of the Python expression: \code{o1+o2}. |
| 490 | \end{cfuncdesc} |
| 491 | |
| 492 | |
| 493 | \begin{cfuncdesc}{PyObject*}{PyNumber_Subtract}{PyObject *o1, PyObject *o2} |
| 494 | Returns the result of subtracting \code{o2} from \code{o1}, or null on |
| 495 | failure. This is the equivalent of the Python expression: |
| 496 | \code{o1-o2}. |
| 497 | \end{cfuncdesc} |
| 498 | |
| 499 | |
| 500 | \begin{cfuncdesc}{PyObject*}{PyNumber_Multiply}{PyObject *o1, PyObject *o2} |
| 501 | Returns the result of multiplying \code{o1} and \code{o2}, or null on |
| 502 | failure. This is the equivalent of the Python expression: |
| 503 | \code{o1*o2}. |
| 504 | \end{cfuncdesc} |
| 505 | |
| 506 | |
| 507 | \begin{cfuncdesc}{PyObject*}{PyNumber_Divide}{PyObject *o1, PyObject *o2} |
| 508 | Returns the result of dividing \code{o1} by \code{o2}, or null on failure. |
| 509 | This is the equivalent of the Python expression: \code{o1/o2}. |
| 510 | \end{cfuncdesc} |
| 511 | |
| 512 | |
| 513 | \begin{cfuncdesc}{PyObject*}{PyNumber_Remainder}{PyObject *o1, PyObject *o2} |
| 514 | Returns the remainder of dividing \code{o1} by \code{o2}, or null on |
| 515 | failure. This is the equivalent of the Python expression: |
| 516 | \code{o1\%o2}. |
| 517 | \end{cfuncdesc} |
| 518 | |
| 519 | |
| 520 | \begin{cfuncdesc}{PyObject*}{PyNumber_Divmod}{PyObject *o1, PyObject *o2} |
| 521 | See the built-in function divmod. Returns \NULL{} on failure. |
| 522 | This is the equivalent of the Python expression: |
| 523 | \code{divmod(o1,o2)}. |
| 524 | \end{cfuncdesc} |
| 525 | |
| 526 | |
| 527 | \begin{cfuncdesc}{PyObject*}{PyNumber_Power}{PyObject *o1, PyObject *o2, PyObject *o3} |
| 528 | See the built-in function pow. Returns \NULL{} on failure. |
| 529 | This is the equivalent of the Python expression: |
| 530 | \code{pow(o1,o2,o3)}, where \code{o3} is optional. |
| 531 | \end{cfuncdesc} |
| 532 | |
| 533 | |
| 534 | \begin{cfuncdesc}{PyObject*}{PyNumber_Negative}{PyObject *o} |
| 535 | Returns the negation of \code{o} on success, or null on failure. |
| 536 | This is the equivalent of the Python expression: \code{-o}. |
| 537 | \end{cfuncdesc} |
| 538 | |
| 539 | |
| 540 | \begin{cfuncdesc}{PyObject*}{PyNumber_Positive}{PyObject *o} |
| 541 | Returns \code{o} on success, or \NULL{} on failure. |
| 542 | This is the equivalent of the Python expression: \code{+o}. |
| 543 | \end{cfuncdesc} |
| 544 | |
| 545 | |
| 546 | \begin{cfuncdesc}{PyObject*}{PyNumber_Absolute}{PyObject *o} |
| 547 | Returns the absolute value of \code{o}, or null on failure. This is |
| 548 | the equivalent of the Python expression: \code{abs(o)}. |
| 549 | \end{cfuncdesc} |
| 550 | |
| 551 | |
| 552 | \begin{cfuncdesc}{PyObject*}{PyNumber_Invert}{PyObject *o} |
| 553 | Returns the bitwise negation of \code{o} on success, or \NULL{} on |
| 554 | failure. This is the equivalent of the Python expression: |
| 555 | \code{~o}. |
| 556 | \end{cfuncdesc} |
| 557 | |
| 558 | |
| 559 | \begin{cfuncdesc}{PyObject*}{PyNumber_Lshift}{PyObject *o1, PyObject *o2} |
| 560 | Returns the result of left shifting \code{o1} by \code{o2} on success, or |
| 561 | \NULL{} on failure. This is the equivalent of the Python |
| 562 | expression: \code{o1 << o2}. |
| 563 | \end{cfuncdesc} |
| 564 | |
| 565 | |
| 566 | \begin{cfuncdesc}{PyObject*}{PyNumber_Rshift}{PyObject *o1, PyObject *o2} |
| 567 | Returns the result of right shifting \code{o1} by \code{o2} on success, or |
| 568 | \NULL{} on failure. This is the equivalent of the Python |
| 569 | expression: \code{o1 >> o2}. |
| 570 | \end{cfuncdesc} |
| 571 | |
| 572 | |
| 573 | \begin{cfuncdesc}{PyObject*}{PyNumber_And}{PyObject *o1, PyObject *o2} |
| 574 | Returns the result of "anding" \code{o2} and \code{o2} on success and \NULL{} |
| 575 | on failure. This is the equivalent of the Python |
| 576 | expression: \code{o1 and o2}. |
| 577 | \end{cfuncdesc} |
| 578 | |
| 579 | |
| 580 | \begin{cfuncdesc}{PyObject*}{PyNumber_Xor}{PyObject *o1, PyObject *o2} |
| 581 | Returns the bitwise exclusive or of \code{o1} by \code{o2} on success, or |
| 582 | \NULL{} on failure. This is the equivalent of the Python |
| 583 | expression: \code{o1\^{ }o2}. |
| 584 | \end{cfuncdesc} |
| 585 | |
| 586 | \begin{cfuncdesc}{PyObject*}{PyNumber_Or}{PyObject *o1, PyObject *o2} |
| 587 | Returns the result or \code{o1} and \code{o2} on success, or \NULL{} on |
| 588 | failure. This is the equivalent of the Python expression: |
| 589 | \code{o1 or o2}. |
| 590 | \end{cfuncdesc} |
| 591 | |
| 592 | |
| 593 | \begin{cfuncdesc}{PyObject*}{PyNumber_Coerce}{PyObject *o1, PyObject *o2} |
| 594 | This function takes the addresses of two variables of type |
| 595 | \code{PyObject*}. |
| 596 | |
| 597 | If the objects pointed to by \code{*p1} and \code{*p2} have the same type, |
| 598 | increment their reference count and return 0 (success). |
| 599 | If the objects can be converted to a common numeric type, |
| 600 | replace \code{*p1} and \code{*p2} by their converted value (with 'new' |
| 601 | reference counts), and return 0. |
| 602 | If no conversion is possible, or if some other error occurs, |
| 603 | return -1 (failure) and don't increment the reference counts. |
| 604 | The call \code{PyNumber_Coerce(\&o1, \&o2)} is equivalent to the Python |
| 605 | statement \code{o1, o2 = coerce(o1, o2)}. |
| 606 | \end{cfuncdesc} |
| 607 | |
| 608 | |
| 609 | \begin{cfuncdesc}{PyObject*}{PyNumber_Int}{PyObject *o} |
| 610 | Returns the \code{o} converted to an integer object on success, or |
| 611 | \NULL{} on failure. This is the equivalent of the Python |
| 612 | expression: \code{int(o)}. |
| 613 | \end{cfuncdesc} |
| 614 | |
| 615 | |
| 616 | \begin{cfuncdesc}{PyObject*}{PyNumber_Long}{PyObject *o} |
| 617 | Returns the \code{o} converted to a long integer object on success, |
| 618 | or \NULL{} on failure. This is the equivalent of the Python |
| 619 | expression: \code{long(o)}. |
| 620 | \end{cfuncdesc} |
| 621 | |
| 622 | |
| 623 | \begin{cfuncdesc}{PyObject*}{PyNumber_Float}{PyObject *o} |
| 624 | Returns the \code{o} converted to a float object on success, or \NULL{} |
| 625 | on failure. This is the equivalent of the Python expression: |
| 626 | \code{float(o)}. |
| 627 | \end{cfuncdesc} |
| 628 | |
| 629 | |
| 630 | \section{Sequence protocol} |
| 631 | |
| 632 | \begin{cfuncdesc}{int}{PySequence_Check}{PyObject *o} |
| 633 | Return 1 if the object provides sequence protocol, and 0 |
| 634 | otherwise. |
| 635 | This function always succeeds. |
| 636 | \end{cfuncdesc} |
| 637 | |
| 638 | |
| 639 | \begin{cfuncdesc}{PyObject*}{PySequence_Concat}{PyObject *o1, PyObject *o2} |
| 640 | Return the concatination of \code{o1} and \code{o2} on success, and \NULL{} on |
| 641 | failure. This is the equivalent of the Python |
| 642 | expression: \code{o1+o2}. |
| 643 | \end{cfuncdesc} |
| 644 | |
| 645 | |
| 646 | \begin{cfuncdesc}{PyObject*}{PySequence_Repeat}{PyObject *o, int count} |
| 647 | Return the result of repeating sequence object \code{o} count times, |
| 648 | or \NULL{} on failure. This is the equivalent of the Python |
| 649 | expression: \code{o*count}. |
| 650 | \end{cfuncdesc} |
| 651 | |
| 652 | |
| 653 | \begin{cfuncdesc}{PyObject*}{PySequence_GetItem}{PyObject *o, int i} |
| 654 | Return the ith element of \code{o}, or \NULL{} on failure. This is the |
| 655 | equivalent of the Python expression: \code{o[i]}. |
| 656 | \end{cfuncdesc} |
| 657 | |
| 658 | |
| 659 | \begin{cfuncdesc}{PyObject*}{PySequence_GetSlice}{PyObject *o, int i1, int i2} |
| 660 | Return the slice of sequence object \code{o} between \code{i1} and \code{i2}, or |
| 661 | \NULL{} on failure. This is the equivalent of the Python |
| 662 | expression, \code{o[i1:i2]}. |
| 663 | \end{cfuncdesc} |
| 664 | |
| 665 | |
| 666 | \begin{cfuncdesc}{int}{PySequence_SetItem}{PyObject *o, int i, PyObject *v} |
| 667 | Assign object \code{v} to the \code{i}th element of \code{o}. |
| 668 | Returns -1 on failure. This is the equivalent of the Python |
| 669 | statement, \code{o[i]=v}. |
| 670 | \end{cfuncdesc} |
| 671 | |
| 672 | \begin{cfuncdesc}{int}{PySequence_DelItem}{PyObject *o, int i} |
| 673 | Delete the \code{i}th element of object \code{v}. Returns |
| 674 | -1 on failure. This is the equivalent of the Python |
| 675 | statement: \code{del o[i]}. |
| 676 | \end{cfuncdesc} |
| 677 | |
| 678 | \begin{cfuncdesc}{int}{PySequence_SetSlice}{PyObject *o, int i1, int i2, PyObject *v} |
| 679 | Assign the sequence object \code{v} to the slice in sequence |
| 680 | object \code{o} from \code{i1} to \code{i2}. This is the equivalent of the Python |
| 681 | statement, \code{o[i1:i2]=v}. |
| 682 | \end{cfuncdesc} |
| 683 | |
| 684 | \begin{cfuncdesc}{int}{PySequence_DelSlice}{PyObject *o, int i1, int i2} |
| 685 | Delete the slice in sequence object, \code{o}, from \code{i1} to \code{i2}. |
| 686 | Returns -1 on failure. This is the equivalent of the Python |
| 687 | statement: \code{del o[i1:i2]}. |
| 688 | \end{cfuncdesc} |
| 689 | |
| 690 | \begin{cfuncdesc}{PyObject*}{PySequence_Tuple}{PyObject *o} |
| 691 | Returns the \code{o} as a tuple on success, and \NULL{} on failure. |
| 692 | This is equivalent to the Python expression: \code{tuple(o)}. |
| 693 | \end{cfuncdesc} |
| 694 | |
| 695 | \begin{cfuncdesc}{int}{PySequence_Count}{PyObject *o, PyObject *value} |
| 696 | Return the number of occurrences of \code{value} on \code{o}, that is, |
| 697 | return the number of keys for which \code{o[key]==value}. On |
| 698 | failure, return -1. This is equivalent to the Python |
| 699 | expression: \code{o.count(value)}. |
| 700 | \end{cfuncdesc} |
| 701 | |
| 702 | \begin{cfuncdesc}{int}{PySequence_In}{PyObject *o, PyObject *value} |
| 703 | Determine if \code{o} contains \code{value}. If an item in \code{o} is equal to |
| 704 | \code{value}, return 1, otherwise return 0. On error, return -1. This |
| 705 | is equivalent to the Python expression: \code{value in o}. |
| 706 | \end{cfuncdesc} |
| 707 | |
| 708 | \begin{cfuncdesc}{int}{PySequence_Index}{PyObject *o, PyObject *value} |
| 709 | Return the first index for which \code{o[i]=value}. On error, |
| 710 | return -1. This is equivalent to the Python |
| 711 | expression: \code{o.index(value)}. |
| 712 | \end{cfuncdesc} |
| 713 | |
| 714 | \section{Mapping protocol} |
| 715 | |
| 716 | \begin{cfuncdesc}{int}{PyMapping_Check}{PyObject *o} |
| 717 | Return 1 if the object provides mapping protocol, and 0 |
| 718 | otherwise. |
| 719 | This function always succeeds. |
| 720 | \end{cfuncdesc} |
| 721 | |
| 722 | |
| 723 | \begin{cfuncdesc}{int}{PyMapping_Length}{PyObject *o} |
| 724 | Returns the number of keys in object \code{o} on success, and -1 on |
| 725 | failure. For objects that do not provide sequence protocol, |
| 726 | this is equivalent to the Python expression: \code{len(o)}. |
| 727 | \end{cfuncdesc} |
| 728 | |
| 729 | |
| 730 | \begin{cfuncdesc}{int}{PyMapping_DelItemString}{PyObject *o, char *key} |
| 731 | Remove the mapping for object \code{key} from the object \code{o}. |
| 732 | Return -1 on failure. This is equivalent to |
| 733 | the Python statement: \code{del o[key]}. |
| 734 | \end{cfuncdesc} |
| 735 | |
| 736 | |
| 737 | \begin{cfuncdesc}{int}{PyMapping_DelItem}{PyObject *o, PyObject *key} |
| 738 | Remove the mapping for object \code{key} from the object \code{o}. |
| 739 | Return -1 on failure. This is equivalent to |
| 740 | the Python statement: \code{del o[key]}. |
| 741 | \end{cfuncdesc} |
| 742 | |
| 743 | |
| 744 | \begin{cfuncdesc}{int}{PyMapping_HasKeyString}{PyObject *o, char *key} |
| 745 | On success, return 1 if the mapping object has the key \code{key} |
| 746 | and 0 otherwise. This is equivalent to the Python expression: |
| 747 | \code{o.has_key(key)}. |
| 748 | This function always succeeds. |
| 749 | \end{cfuncdesc} |
| 750 | |
| 751 | |
| 752 | \begin{cfuncdesc}{int}{PyMapping_HasKey}{PyObject *o, PyObject *key} |
| 753 | Return 1 if the mapping object has the key \code{key} |
| 754 | and 0 otherwise. This is equivalent to the Python expression: |
| 755 | \code{o.has_key(key)}. |
| 756 | This function always succeeds. |
| 757 | \end{cfuncdesc} |
| 758 | |
| 759 | |
| 760 | \begin{cfuncdesc}{PyObject*}{PyMapping_Keys}{PyObject *o} |
| 761 | On success, return a list of the keys in object \code{o}. On |
| 762 | failure, return \NULL{}. This is equivalent to the Python |
| 763 | expression: \code{o.keys()}. |
| 764 | \end{cfuncdesc} |
| 765 | |
| 766 | |
| 767 | \begin{cfuncdesc}{PyObject*}{PyMapping_Values}{PyObject *o} |
| 768 | On success, return a list of the values in object \code{o}. On |
| 769 | failure, return \NULL{}. This is equivalent to the Python |
| 770 | expression: \code{o.values()}. |
| 771 | \end{cfuncdesc} |
| 772 | |
| 773 | |
| 774 | \begin{cfuncdesc}{PyObject*}{PyMapping_Items}{PyObject *o} |
| 775 | On success, return a list of the items in object \code{o}, where |
| 776 | each item is a tuple containing a key-value pair. On |
| 777 | failure, return \NULL{}. This is equivalent to the Python |
| 778 | expression: \code{o.items()}. |
| 779 | \end{cfuncdesc} |
| 780 | |
| 781 | \begin{cfuncdesc}{int}{PyMapping_Clear}{PyObject *o} |
| 782 | Make object \code{o} empty. Returns 1 on success and 0 on failure. |
| 783 | This is equivalent to the Python statement: |
| 784 | \code{for key in o.keys(): del o[key]} |
| 785 | \end{cfuncdesc} |
| 786 | |
| 787 | |
| 788 | \begin{cfuncdesc}{PyObject*}{PyMapping_GetItemString}{PyObject *o, char *key} |
| 789 | Return element of \code{o} corresponding to the object \code{key} or \NULL{} |
| 790 | on failure. This is the equivalent of the Python expression: |
| 791 | \code{o[key]}. |
| 792 | \end{cfuncdesc} |
| 793 | |
| 794 | \begin{cfuncdesc}{PyObject*}{PyMapping_SetItemString}{PyObject *o, char *key, PyObject *v} |
| 795 | Map the object \code{key} to the value \code{v} in object \code{o}. Returns |
| 796 | -1 on failure. This is the equivalent of the Python |
| 797 | statement: \code{o[key]=v}. |
| 798 | \end{cfuncdesc} |
| 799 | |
| 800 | |
| 801 | \section{Constructors} |
| 802 | |
| 803 | \begin{cfuncdesc}{PyObject*}{PyFile_FromString}{char *file_name, char *mode} |
| 804 | On success, returns a new file object that is opened on the |
| 805 | file given by \code{file_name}, with a file mode given by \code{mode}, |
| 806 | where \code{mode} has the same semantics as the standard C routine, |
| 807 | fopen. On failure, return -1. |
| 808 | \end{cfuncdesc} |
| 809 | |
| 810 | \begin{cfuncdesc}{PyObject*}{PyFile_FromFile}{FILE *fp, char *file_name, char *mode, int close_on_del} |
| 811 | Return a new file object for an already opened standard C |
| 812 | file pointer, \code{fp}. A file name, \code{file_name}, and open mode, |
| 813 | \code{mode}, must be provided as well as a flag, \code{close_on_del}, that |
| 814 | indicates whether the file is to be closed when the file |
| 815 | object is destroyed. On failure, return -1. |
| 816 | \end{cfuncdesc} |
| 817 | |
| 818 | \begin{cfuncdesc}{PyObject*}{PyFloat_FromDouble}{double v} |
| 819 | Returns a new float object with the value \code{v} on success, and |
| 820 | \NULL{} on failure. |
| 821 | \end{cfuncdesc} |
| 822 | |
| 823 | \begin{cfuncdesc}{PyObject*}{PyInt_FromLong}{long v} |
| 824 | Returns a new int object with the value \code{v} on success, and |
| 825 | \NULL{} on failure. |
| 826 | \end{cfuncdesc} |
| 827 | |
| 828 | \begin{cfuncdesc}{PyObject*}{PyList_New}{int l} |
| 829 | Returns a new list of length \code{l} on success, and \NULL{} on |
| 830 | failure. |
| 831 | \end{cfuncdesc} |
| 832 | |
| 833 | \begin{cfuncdesc}{PyObject*}{PyLong_FromLong}{long v} |
| 834 | Returns a new long object with the value \code{v} on success, and |
| 835 | \NULL{} on failure. |
| 836 | \end{cfuncdesc} |
| 837 | |
| 838 | \begin{cfuncdesc}{PyObject*}{PyLong_FromDouble}{double v} |
| 839 | Returns a new long object with the value \code{v} on success, and |
| 840 | \NULL{} on failure. |
| 841 | \end{cfuncdesc} |
| 842 | |
| 843 | \begin{cfuncdesc}{PyObject*}{PyDict_New}{} |
| 844 | Returns a new empty dictionary on success, and \NULL{} on |
| 845 | failure. |
| 846 | \end{cfuncdesc} |
| 847 | |
| 848 | \begin{cfuncdesc}{PyObject*}{PyString_FromString}{char *v} |
| 849 | Returns a new string object with the value \code{v} on success, and |
| 850 | \NULL{} on failure. |
| 851 | \end{cfuncdesc} |
| 852 | |
| 853 | \begin{cfuncdesc}{PyObject*}{PyString_FromStringAndSize}{char *v, int l} |
| 854 | Returns a new string object with the value \code{v} and length \code{l} |
| 855 | on success, and \NULL{} on failure. |
| 856 | \end{cfuncdesc} |
| 857 | |
| 858 | \begin{cfuncdesc}{PyObject*}{PyTuple_New}{int l} |
| 859 | Returns a new tuple of length \code{l} on success, and \NULL{} on |
| 860 | failure. |
| 861 | \end{cfuncdesc} |
| 862 | |
| 863 | |
| 864 | \chapter{Concrete Objects Layer} |
| 865 | |
| 866 | The functions in this chapter are specific to certain Python object |
| 867 | types. Passing them an object of the wrong type is not a good idea; |
| 868 | if you receive an object from a Python program and you are not sure |
| 869 | that it has the right type, you must perform a type check first; |
| 870 | e.g. to check that an object is a dictionary, use |
| 871 | \code{PyDict_Check()}. |
| 872 | |
| 873 | |
| 874 | \chapter{Defining New Object Types} |
| 875 | |
| 876 | \begin{cfuncdesc}{PyObject *}{_PyObject_New}{PyTypeObject *type} |
| 877 | \end{cfuncdesc} |
| 878 | |
| 879 | \begin{cfuncdesc}{PyObject *}{_PyObject_New}{PyTypeObject *type} |
| 880 | \end{cfuncdesc} |
| 881 | |
| 882 | \input{api.ind} % Index -- must be last |
| 883 | |
| 884 | \end{document} |