Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1 | \documentstyle[twoside,11pt,myformat]{report} |
| 2 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 3 | \title{Python-C API Reference} |
| 4 | |
| 5 | \input{boilerplate} |
| 6 | |
| 7 | \makeindex % tell \index to actually write the .idx file |
| 8 | |
| 9 | |
| 10 | \begin{document} |
| 11 | |
| 12 | \pagenumbering{roman} |
| 13 | |
| 14 | \maketitle |
| 15 | |
| 16 | \input{copyright} |
| 17 | |
| 18 | \begin{abstract} |
| 19 | |
| 20 | \noindent |
| 21 | This manual documents the API used by C (or C++) programmers who want |
| 22 | to write extension modules or embed Python. It is a companion to |
| 23 | ``Extending and Embedding the Python Interpreter'', which describes |
| 24 | the general principles of extension writing but does not document the |
| 25 | API functions in detail. |
| 26 | |
| 27 | \end{abstract} |
| 28 | |
| 29 | \pagebreak |
| 30 | |
| 31 | { |
| 32 | \parskip = 0mm |
| 33 | \tableofcontents |
| 34 | } |
| 35 | |
| 36 | \pagebreak |
| 37 | |
| 38 | \pagenumbering{arabic} |
| 39 | |
| 40 | |
| 41 | \chapter{Introduction} |
| 42 | |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 43 | (XXX This is the old introduction, mostly by Jim Fulton -- should be |
| 44 | rewritten.) |
| 45 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 46 | From the viewpoint of of C access to Python services, we have: |
| 47 | |
| 48 | \begin{enumerate} |
| 49 | |
| 50 | \item "Very high level layer": two or three functions that let you |
| 51 | exec or eval arbitrary Python code given as a string in a module whose |
| 52 | name is given, passing C values in and getting C values out using |
| 53 | mkvalue/getargs style format strings. This does not require the user |
| 54 | to declare any variables of type \code{PyObject *}. This should be |
| 55 | enough to write a simple application that gets Python code from the |
| 56 | user, execs it, and returns the output or errors. |
| 57 | |
| 58 | \item "Abstract objects layer": which is the subject of this chapter. |
| 59 | It has many functions operating on objects, and lest you do many |
| 60 | things from C that you can also write in Python, without going through |
| 61 | the Python parser. |
| 62 | |
| 63 | \item "Concrete objects layer": This is the public type-dependent |
| 64 | interface provided by the standard built-in types, such as floats, |
| 65 | strings, and lists. This interface exists and is currently documented |
| 66 | by the collection of include files provides with the Python |
| 67 | distributions. |
| 68 | |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 69 | \end{enumerate} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 70 | |
| 71 | From the point of view of Python accessing services provided by C |
| 72 | modules: |
| 73 | |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 74 | \begin{enumerate} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 75 | |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 76 | \item[4.] "Python module interface": this interface consist of the basic |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 77 | routines used to define modules and their members. Most of the |
| 78 | current extensions-writing guide deals with this interface. |
| 79 | |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 80 | \item[5.] "Built-in object interface": this is the interface that a new |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 81 | built-in type must provide and the mechanisms and rules that a |
| 82 | developer of a new built-in type must use and follow. |
| 83 | |
| 84 | \end{enumerate} |
| 85 | |
| 86 | The Python C API provides four groups of operations on objects, |
| 87 | corresponding to the same operations in the Python language: object, |
| 88 | numeric, sequence, and mapping. Each protocol consists of a |
| 89 | collection of related operations. If an operation that is not |
| 90 | provided by a particular type is invoked, then the standard exception |
| 91 | \code{TypeError} is raised with a operation name as an argument. |
| 92 | |
| 93 | In addition, for convenience this interface defines a set of |
| 94 | constructors for building objects of built-in types. This is needed |
| 95 | so new objects can be returned from C functions that otherwise treat |
| 96 | objects generically. |
| 97 | |
| 98 | \section{Reference Counting} |
| 99 | |
| 100 | For most of the functions in the Python-C API, if a function retains a |
| 101 | reference to a Python object passed as an argument, then the function |
| 102 | will increase the reference count of the object. It is unnecessary |
| 103 | for the caller to increase the reference count of an argument in |
| 104 | anticipation of the object's retention. |
| 105 | |
| 106 | Usually, Python objects returned from functions should be treated as |
| 107 | new objects. Functions that return objects assume that the caller |
| 108 | will retain a reference and the reference count of the object has |
| 109 | already been incremented to account for this fact. A caller that does |
| 110 | not retain a reference to an object that is returned from a function |
| 111 | must decrement the reference count of the object (using |
| 112 | \code{Py_DECREF()}) to prevent memory leaks. |
| 113 | |
| 114 | Exceptions to these rules will be noted with the individual functions. |
| 115 | |
| 116 | \section{Include Files} |
| 117 | |
| 118 | All function, type and macro definitions needed to use the Python-C |
| 119 | API are included in your code by the following line: |
| 120 | |
| 121 | \code{\#include "Python.h"} |
| 122 | |
| 123 | This implies inclusion of the following standard header files: |
| 124 | stdio.h, string.h, errno.h, and stdlib.h (if available). |
| 125 | |
| 126 | All user visible names defined by Python.h (except those defined by |
| 127 | the included standard headers) have one of the prefixes \code{Py} or |
| 128 | \code{_Py}. Names beginning with \code{_Py} are for internal use |
| 129 | only. |
| 130 | |
| 131 | |
| 132 | \chapter{Initialization and Shutdown of an Embedded Python Interpreter} |
| 133 | |
| 134 | When embedding the Python interpreter in a C or C++ program, the |
| 135 | interpreter must be initialized. |
| 136 | |
| 137 | \begin{cfuncdesc}{void}{PyInitialize}{} |
| 138 | This function initializes the interpreter. It must be called before |
| 139 | any interaction with the interpreter takes place. If it is called |
| 140 | more than once, the second and further calls have no effect. |
| 141 | |
| 142 | The function performs the following tasks: create an environment in |
| 143 | which modules can be imported and Python code can be executed; |
| 144 | initialize the \code{__builtin__} module; initialize the \code{sys} |
| 145 | module; initialize \code{sys.path}; initialize signal handling; and |
| 146 | create the empty \code{__main__} module. |
| 147 | |
| 148 | In the current system, there is no way to undo all these |
| 149 | initializations or to create additional interpreter environments. |
| 150 | \end{cfuncdesc} |
| 151 | |
| 152 | \begin{cfuncdesc}{int}{Py_AtExit}{void (*func) ()} |
| 153 | Register a cleanup function to be called when Python exits. The |
| 154 | cleanup function will be called with no arguments and should return no |
| 155 | value. At most 32 cleanup functions can be registered. When the |
| 156 | registration is successful, \code{Py_AtExit} returns 0; on failure, it |
| 157 | returns -1. Each cleanup function will be called t most once. The |
| 158 | cleanup function registered last is called first. |
| 159 | \end{cfuncdesc} |
| 160 | |
| 161 | \begin{cfuncdesc}{void}{Py_Exit}{int status} |
| 162 | Exit the current process. This calls \code{Py_Cleanup()} (see next |
| 163 | item) and performs additional cleanup (under some circumstances it |
| 164 | will attempt to delete all modules), and then calls the standard C |
| 165 | library function \code{exit(status)}. |
| 166 | \end{cfuncdesc} |
| 167 | |
| 168 | \begin{cfuncdesc}{void}{Py_Cleanup}{} |
| 169 | Perform some of the cleanup that \code{Py_Exit} performs, but don't |
| 170 | exit the process. In particular, this invokes the user's |
| 171 | \code{sys.exitfunc} function (if defined at all), and it invokes the |
| 172 | cleanup functions registered with \code{Py_AtExit()}, in reverse order |
| 173 | of their registration. |
| 174 | \end{cfuncdesc} |
| 175 | |
| 176 | \begin{cfuncdesc}{void}{Py_FatalError}{char *message} |
| 177 | Print a fatal error message and die. No cleanup is performed. This |
| 178 | function should only be invoked when a condition is detected that |
| 179 | would make it dangerous to continue using the Python interpreter; |
| 180 | e.g., when the object administration appears to be corrupted. |
| 181 | \end{cfuncdesc} |
| 182 | |
| 183 | \begin{cfuncdesc}{void}{PyImport_Init}{} |
| 184 | Initialize the module table. For internal use only. |
| 185 | \end{cfuncdesc} |
| 186 | |
| 187 | \begin{cfuncdesc}{void}{PyImport_Cleanup}{} |
| 188 | Empty the module table. For internal use only. |
| 189 | \end{cfuncdesc} |
| 190 | |
| 191 | \begin{cfuncdesc}{void}{PyBuiltin_Init}{} |
| 192 | Initialize the \code{__builtin__} module. For internal use only. |
| 193 | \end{cfuncdesc} |
| 194 | |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 195 | XXX Other init functions: PyEval_InitThreads, PyOS_InitInterrupts, |
| 196 | PyMarshal_Init, PySys_Init. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 197 | |
| 198 | \chapter{Reference Counting} |
| 199 | |
| 200 | The functions in this chapter are used for managing reference counts |
| 201 | of Python objects. |
| 202 | |
| 203 | \begin{cfuncdesc}{void}{Py_INCREF}{PyObject *o} |
| 204 | Increment the reference count for object \code{o}. The object must |
| 205 | not be \NULL{}; if you aren't sure that it isn't \NULL{}, use |
| 206 | \code{Py_XINCREF()}. |
| 207 | \end{cfuncdesc} |
| 208 | |
| 209 | \begin{cfuncdesc}{void}{Py_XINCREF}{PyObject *o} |
| 210 | Increment the reference count for object \code{o}. The object may be |
| 211 | \NULL{}, in which case the function has no effect. |
| 212 | \end{cfuncdesc} |
| 213 | |
| 214 | \begin{cfuncdesc}{void}{Py_DECREF}{PyObject *o} |
| 215 | Decrement the reference count for object \code{o}. The object must |
| 216 | not be \NULL{}; if you aren't sure that it isn't \NULL{}, use |
| 217 | \code{Py_XDECREF()}. If the reference count reaches zero, the object's |
| 218 | type's deallocation function (which must not be \NULL{}) is invoked. |
| 219 | |
| 220 | \strong{Warning:} The deallocation function can cause arbitrary Python |
| 221 | code to be invoked (e.g. when a class instance with a \code{__del__()} |
| 222 | method is deallocated). While exceptions in such code are not |
| 223 | propagated, the executed code has free access to all Python global |
| 224 | variables. This means that any object that is reachable from a global |
| 225 | variable should be in a consistent state before \code{Py_DECREF()} is |
| 226 | invoked. For example, code to delete an object from a list should |
| 227 | copy a reference to the deleted object in a temporary variable, update |
| 228 | the list data structure, and then call \code{Py_DECREF()} for the |
| 229 | temporary variable. |
| 230 | \end{cfuncdesc} |
| 231 | |
| 232 | \begin{cfuncdesc}{void}{Py_XDECREF}{PyObject *o} |
| 233 | Decrement the reference count for object \code{o}.The object may be |
| 234 | \NULL{}, in which case the function has no effect; otherwise the |
| 235 | effect is the same as for \code{Py_DECREF()}, and the same warning |
| 236 | applies. |
| 237 | \end{cfuncdesc} |
| 238 | |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 239 | The following functions are only for internal use: |
| 240 | \code{_Py_Dealloc}, \code{_Py_ForgetReference}, \code{_Py_NewReference}, |
| 241 | as well as the global variable \code{_Py_RefTotal}. |
| 242 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 243 | |
| 244 | \chapter{Exception Handling} |
| 245 | |
| 246 | The functions in this chapter will let you handle and raise Python |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 247 | exceptions. It is important to understand some of the basics of |
| 248 | Python exception handling. It works somewhat like the Unix |
| 249 | \code{errno} variable: there is a global indicator (per thread) of the |
| 250 | last error that occurred. Most functions don't clear this on success, |
| 251 | but will set it to indicate the cause of the error on failure. Most |
| 252 | functions also return an error indicator, usually \NULL{} if they are |
| 253 | supposed to return a pointer, or -1 if they return an integer |
| 254 | (exception: the \code{PyArg_Parse*()} functions return 1 for success and |
| 255 | 0 for failure). When a function must fail because of some function it |
| 256 | called failed, it generally doesn't set the error indicator; the |
| 257 | function it called already set it. |
| 258 | |
| 259 | The error indicator consists of three Python objects corresponding to |
| 260 | the Python variables \code{sys.exc_type}, \code{sys.exc_value} and |
| 261 | \code{sys.exc_traceback}. API functions exist to interact with the |
| 262 | error indicator in various ways. There is a separate error indicator |
| 263 | for each thread. |
| 264 | |
| 265 | % XXX Order of these should be more thoughtful. |
| 266 | % Either alphabetical or some kind of structure. |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 267 | |
| 268 | \begin{cfuncdesc}{void}{PyErr_Print}{} |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 269 | Print a standard traceback to \code{sys.stderr} and clear the error |
| 270 | indicator. Call this function only when the error indicator is set. |
| 271 | (Otherwise it will cause a fatal error!) |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 272 | \end{cfuncdesc} |
| 273 | |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 274 | \begin{cfuncdesc}{PyObject *}{PyErr_Occurred}{} |
| 275 | Test whether the error indicator is set. If set, return the exception |
| 276 | \code{type} (the first argument to the last call to one of the |
| 277 | \code{PyErr_Set*()} functions or to \code{PyErr_Restore()}). If not |
| 278 | set, return \NULL{}. You do not own a reference to the return value, |
| 279 | so you do not need to \code{Py_DECREF()} it. |
| 280 | \end{cfuncdesc} |
| 281 | |
| 282 | \begin{cfuncdesc}{void}{PyErr_Clear}{} |
| 283 | Clear the error indicator. If the error indicator is not set, there |
| 284 | is no effect. |
| 285 | \end{cfuncdesc} |
| 286 | |
| 287 | \begin{cfuncdesc}{void}{PyErr_Fetch}{PyObject **ptype, PyObject **pvalue, PyObject **ptraceback} |
| 288 | Retrieve the error indicator into three variables whose addresses are |
| 289 | passed. If the error indicator is not set, set all three variables to |
| 290 | \NULL{}. If it is set, it will be cleared and you own a reference to |
| 291 | each object retrieved. The value and traceback object may be \NULL{} |
| 292 | even when the type object is not. \strong{Note:} this function is |
| 293 | normally only used by code that needs to handle exceptions or by code |
| 294 | that needs to save and restore the error indicator temporarily. |
| 295 | \end{cfuncdesc} |
| 296 | |
| 297 | \begin{cfuncdesc}{void}{PyErr_Restore}{PyObject *type, PyObject *value, PyObject *traceback} |
| 298 | Set the error indicator from the three objects. If the error |
| 299 | indicator is already set, it is cleared first. If the objects are |
| 300 | \NULL{}, the error indicator is cleared. Do not pass a \NULL{} type |
| 301 | and non-\NULL{} value or traceback. The exception type should be a |
| 302 | string or class; if it is a class, the value should be an instance of |
| 303 | that class. Do not pass an invalid exception type or value. |
| 304 | (Violating these rules will cause subtle problems later.) This call |
| 305 | takes away a reference to each object, i.e. you must own a reference |
| 306 | to each object before the call and after the call you no longer own |
| 307 | these references. (If you don't understand this, don't use this |
| 308 | function. I warned you.) \strong{Note:} this function is normally |
| 309 | only used by code that needs to save and restore the error indicator |
| 310 | temporarily. |
| 311 | \end{cfuncdesc} |
| 312 | |
| 313 | \begin{cfuncdesc}{void}{PyErr_SetString}{PyObject *type, char *message} |
| 314 | This is the most common way to set the error indicator. The first |
| 315 | argument specifies the exception type; it is normally one of the |
| 316 | standard exceptions, e.g. \code{PyExc_RuntimeError}. You need not |
| 317 | increment its reference count. The second argument is an error |
| 318 | message; it is converted to a string object. |
| 319 | \end{cfuncdesc} |
| 320 | |
| 321 | \begin{cfuncdesc}{void}{PyErr_SetObject}{PyObject *type, PyObject *value} |
| 322 | This function is similar to \code{PyErr_SetString()} but lets you |
| 323 | specify an arbitrary Python object for the ``value'' of the exception. |
| 324 | You need not increment its reference count. |
| 325 | \end{cfuncdesc} |
| 326 | |
| 327 | \begin{cfuncdesc}{void}{PyErr_SetNone}{PyObject *type} |
| 328 | This is a shorthand for \code{PyErr_SetString(\var{type}, Py_None}. |
| 329 | \end{cfuncdesc} |
| 330 | |
| 331 | \begin{cfuncdesc}{int}{PyErr_BadArgument}{} |
| 332 | This is a shorthand for \code{PyErr_SetString(PyExc_TypeError, |
| 333 | \var{message})}, where \var{message} indicates that a built-in operation |
| 334 | was invoked with an illegal argument. It is mostly for internal use. |
| 335 | \end{cfuncdesc} |
| 336 | |
| 337 | \begin{cfuncdesc}{PyObject *}{PyErr_NoMemory}{} |
| 338 | This is a shorthand for \code{PyErr_SetNone(PyExc_MemoryError)}; it |
| 339 | returns \NULL{} so an object allocation function can write |
| 340 | \code{return PyErr_NoMemory();} when it runs out of memory. |
| 341 | \end{cfuncdesc} |
| 342 | |
| 343 | \begin{cfuncdesc}{PyObject *}{PyErr_SetFromErrno}{PyObject *type} |
| 344 | This is a convenience function to raise an exception when a C library |
| 345 | function has returned an error and set the C variable \code{errno}. |
| 346 | It constructs a tuple object whose first item is the integer |
| 347 | \code{errno} value and whose second item is the corresponding error |
| 348 | message (gotten from \code{strerror()}), and then calls |
| 349 | \code{PyErr_SetObject(\var{type}, \var{object})}. On \UNIX{}, when |
| 350 | the \code{errno} value is \code{EINTR}, indicating an interrupted |
| 351 | system call, this calls \code{PyErr_CheckSignals()}, and if that set |
| 352 | the error indicator, leaves it set to that. The function always |
| 353 | returns \NULL{}, so a wrapper function around a system call can write |
| 354 | \code{return PyErr_NoMemory();} when the system call returns an error. |
| 355 | \end{cfuncdesc} |
| 356 | |
| 357 | \begin{cfuncdesc}{void}{PyErr_BadInternalCall}{} |
| 358 | This is a shorthand for \code{PyErr_SetString(PyExc_TypeError, |
| 359 | \var{message})}, where \var{message} indicates that an internal |
| 360 | operation (e.g. a Python-C API function) was invoked with an illegal |
| 361 | argument. It is mostly for internal use. |
| 362 | \end{cfuncdesc} |
| 363 | |
| 364 | \begin{cfuncdesc}{int}{PyErr_CheckSignals}{} |
| 365 | This function interacts with Python's signal handling. It checks |
| 366 | whether a signal has been sent to the processes and if so, invokes the |
| 367 | corresponding signal handler. If the \code{signal} module is |
| 368 | supported, this can invoke a signal handler written in Python. In all |
| 369 | cases, the default effect for \code{SIGINT} is to raise the |
| 370 | \code{KeyboadInterrupt} exception. If an exception is raised the |
| 371 | error indicator is set and the function returns 1; otherwise the |
| 372 | function returns 0. The error indicator may or may not be cleared if |
| 373 | it was previously set. |
| 374 | \end{cfuncdesc} |
| 375 | |
| 376 | \begin{cfuncdesc}{void}{PyErr_SetInterrupt}{} |
| 377 | This function is obsolete (XXX or platform dependent?). It simulates |
| 378 | the effect of a \code{SIGINT} signal arriving -- the next time |
| 379 | \code{PyErr_CheckSignals()} is called, \code{KeyboadInterrupt} will be |
| 380 | raised. |
| 381 | \end{cfuncdesc} |
| 382 | |
| 383 | \section{Standard Exceptions} |
| 384 | |
| 385 | All standard Python exceptions are available as global variables whose |
| 386 | names are \code{PyExc_} followed by the Python exception name. |
| 387 | These have the type \code{PyObject *}; they are all string objects. |
| 388 | For completion, here are all the variables: |
| 389 | \code{PyExc_AccessError}, |
| 390 | \code{PyExc_AssertionError}, |
| 391 | \code{PyExc_AttributeError}, |
| 392 | \code{PyExc_EOFError}, |
| 393 | \code{PyExc_FloatingPointError}, |
| 394 | \code{PyExc_IOError}, |
| 395 | \code{PyExc_ImportError}, |
| 396 | \code{PyExc_IndexError}, |
| 397 | \code{PyExc_KeyError}, |
| 398 | \code{PyExc_KeyboardInterrupt}, |
| 399 | \code{PyExc_MemoryError}, |
| 400 | \code{PyExc_NameError}, |
| 401 | \code{PyExc_OverflowError}, |
| 402 | \code{PyExc_RuntimeError}, |
| 403 | \code{PyExc_SyntaxError}, |
| 404 | \code{PyExc_SystemError}, |
| 405 | \code{PyExc_SystemExit}, |
| 406 | \code{PyExc_TypeError}, |
| 407 | \code{PyExc_ValueError}, |
| 408 | \code{PyExc_ZeroDivisionError}. |
| 409 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 410 | |
| 411 | \chapter{Utilities} |
| 412 | |
| 413 | The functions in this chapter perform various utility tasks, such as |
| 414 | parsing function arguments and constructing Python values from C |
| 415 | values. |
| 416 | |
| 417 | \begin{cfuncdesc}{int}{Py_FdIsInteractive}{FILE *fp, char *filename} |
| 418 | Return true (nonzero) if the standard I/O file \code{fp} with name |
| 419 | \code{filename} is deemed interactive. This is the case for files for |
| 420 | which \code{isatty(fileno(fp))} is true. If the global flag |
| 421 | \code{Py_InteractiveFlag} is true, this function also returns true if |
| 422 | the \code{name} pointer is \NULL{} or if the name is equal to one of |
| 423 | the strings \code{"<stdin>"} or \code{"???"}. |
| 424 | \end{cfuncdesc} |
| 425 | |
| 426 | \begin{cfuncdesc}{long}{PyOS_GetLastModificationTime}{char *filename} |
| 427 | Return the time of last modification of the file \code{filename}. |
| 428 | The result is encoded in the same way as the timestamp returned by |
| 429 | the standard C library function \code{time()}. |
| 430 | \end{cfuncdesc} |
| 431 | |
| 432 | |
| 433 | \chapter{Debugging} |
| 434 | |
| 435 | XXX Explain Py_DEBUG, Py_TRACE_REFS, Py_REF_DEBUG. |
| 436 | |
| 437 | |
| 438 | \chapter{The Very High Level Layer} |
| 439 | |
| 440 | The functions in this chapter will let you execute Python source code |
| 441 | given in a file or a buffer, but they will not let you interact in a |
| 442 | more detailed way with the interpreter. |
| 443 | |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 444 | \begin{cfuncdesc}{int}{PyRun_AnyFile}{FILE *, char *} |
| 445 | \end{cfuncdesc} |
| 446 | |
| 447 | \begin{cfuncdesc}{int}{PyRun_SimpleString}{char *} |
| 448 | \end{cfuncdesc} |
| 449 | |
| 450 | \begin{cfuncdesc}{int}{PyRun_SimpleFile}{FILE *, char *} |
| 451 | \end{cfuncdesc} |
| 452 | |
| 453 | \begin{cfuncdesc}{int}{PyRun_InteractiveOne}{FILE *, char *} |
| 454 | \end{cfuncdesc} |
| 455 | |
| 456 | \begin{cfuncdesc}{int}{PyRun_InteractiveLoop}{FILE *, char *} |
| 457 | \end{cfuncdesc} |
| 458 | |
| 459 | \begin{cfuncdesc}{struct _node *}{PyParser_SimpleParseString}{char *, int} |
| 460 | \end{cfuncdesc} |
| 461 | |
| 462 | \begin{cfuncdesc}{struct _node *}{PyParser_SimpleParseFile}{FILE *, char *, int} |
| 463 | \end{cfuncdesc} |
| 464 | |
| 465 | \begin{cfuncdesc}{}{PyObject *PyRun}{ROTO((char *, int, PyObject *, PyObject *} |
| 466 | \end{cfuncdesc} |
| 467 | |
| 468 | \begin{cfuncdesc}{}{PyObject *PyRun}{ROTO((FILE *, char *, int, PyObject *, PyObject *} |
| 469 | \end{cfuncdesc} |
| 470 | |
| 471 | \begin{cfuncdesc}{}{PyObject *Py}{ROTO((char *, char *, int} |
| 472 | \end{cfuncdesc} |
| 473 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 474 | |
| 475 | \chapter{Abstract Objects Layer} |
| 476 | |
| 477 | The functions in this chapter interact with Python objects regardless |
| 478 | of their type, or with wide classes of object types (e.g. all |
| 479 | numerical types, or all sequence types). When used on object types |
| 480 | for which they do not apply, they will flag a Python exception. |
| 481 | |
| 482 | \section{Object Protocol} |
| 483 | |
| 484 | \begin{cfuncdesc}{int}{PyObject_Print}{PyObject *o, FILE *fp, int flags} |
| 485 | Print an object \code{o}, on file \code{fp}. Returns -1 on error |
| 486 | The flags argument is used to enable certain printing |
| 487 | options. The only option currently supported is \code{Py_Print_RAW}. |
| 488 | \end{cfuncdesc} |
| 489 | |
| 490 | \begin{cfuncdesc}{int}{PyObject_HasAttrString}{PyObject *o, char *attr_name} |
| 491 | Returns 1 if o has the attribute attr_name, and 0 otherwise. |
| 492 | This is equivalent to the Python expression: |
| 493 | \code{hasattr(o,attr_name)}. |
| 494 | This function always succeeds. |
| 495 | \end{cfuncdesc} |
| 496 | |
| 497 | \begin{cfuncdesc}{PyObject*}{PyObject_GetAttrString}{PyObject *o, char *attr_name} |
| 498 | Retrieve an attributed named attr_name form object o. |
| 499 | Returns the attribute value on success, or \NULL{} on failure. |
| 500 | This is the equivalent of the Python expression: \code{o.attr_name}. |
| 501 | \end{cfuncdesc} |
| 502 | |
| 503 | |
| 504 | \begin{cfuncdesc}{int}{PyObject_HasAttr}{PyObject *o, PyObject *attr_name} |
| 505 | Returns 1 if o has the attribute attr_name, and 0 otherwise. |
| 506 | This is equivalent to the Python expression: |
| 507 | \code{hasattr(o,attr_name)}. |
| 508 | This function always succeeds. |
| 509 | \end{cfuncdesc} |
| 510 | |
| 511 | |
| 512 | \begin{cfuncdesc}{PyObject*}{PyObject_GetAttr}{PyObject *o, PyObject *attr_name} |
| 513 | Retrieve an attributed named attr_name form object o. |
| 514 | Returns the attribute value on success, or \NULL{} on failure. |
| 515 | This is the equivalent of the Python expression: o.attr_name. |
| 516 | \end{cfuncdesc} |
| 517 | |
| 518 | |
| 519 | \begin{cfuncdesc}{int}{PyObject_SetAttrString}{PyObject *o, char *attr_name, PyObject *v} |
| 520 | Set the value of the attribute named \code{attr_name}, for object \code{o}, |
| 521 | to the value \code{v}. Returns -1 on failure. This is |
| 522 | the equivalent of the Python statement: \code{o.attr_name=v}. |
| 523 | \end{cfuncdesc} |
| 524 | |
| 525 | |
| 526 | \begin{cfuncdesc}{int}{PyObject_SetAttr}{PyObject *o, PyObject *attr_name, PyObject *v} |
| 527 | Set the value of the attribute named \code{attr_name}, for |
| 528 | object \code{o}, |
| 529 | to the value \code{v}. Returns -1 on failure. This is |
| 530 | the equivalent of the Python statement: \code{o.attr_name=v}. |
| 531 | \end{cfuncdesc} |
| 532 | |
| 533 | |
| 534 | \begin{cfuncdesc}{int}{PyObject_DelAttrString}{PyObject *o, char *attr_name} |
| 535 | Delete attribute named \code{attr_name}, for object \code{o}. Returns -1 on |
| 536 | failure. This is the equivalent of the Python |
| 537 | statement: \code{del o.attr_name}. |
| 538 | \end{cfuncdesc} |
| 539 | |
| 540 | |
| 541 | \begin{cfuncdesc}{int}{PyObject_DelAttr}{PyObject *o, PyObject *attr_name} |
| 542 | Delete attribute named \code{attr_name}, for object \code{o}. Returns -1 on |
| 543 | failure. This is the equivalent of the Python |
| 544 | statement: \code{del o.attr_name}. |
| 545 | \end{cfuncdesc} |
| 546 | |
| 547 | |
| 548 | \begin{cfuncdesc}{int}{PyObject_Cmp}{PyObject *o1, PyObject *o2, int *result} |
| 549 | Compare the values of \code{o1} and \code{o2} using a routine provided by |
| 550 | \code{o1}, if one exists, otherwise with a routine provided by \code{o2}. |
| 551 | The result of the comparison is returned in \code{result}. Returns |
| 552 | -1 on failure. This is the equivalent of the Python |
| 553 | statement: \code{result=cmp(o1,o2)}. |
| 554 | \end{cfuncdesc} |
| 555 | |
| 556 | |
| 557 | \begin{cfuncdesc}{int}{PyObject_Compare}{PyObject *o1, PyObject *o2} |
| 558 | Compare the values of \code{o1} and \code{o2} using a routine provided by |
| 559 | \code{o1}, if one exists, otherwise with a routine provided by \code{o2}. |
| 560 | Returns the result of the comparison on success. On error, |
| 561 | the value returned is undefined. This is equivalent to the |
| 562 | Python expression: \code{cmp(o1,o2)}. |
| 563 | \end{cfuncdesc} |
| 564 | |
| 565 | |
| 566 | \begin{cfuncdesc}{PyObject*}{PyObject_Repr}{PyObject *o} |
| 567 | Compute the string representation of object, \code{o}. Returns the |
| 568 | string representation on success, \NULL{} on failure. This is |
| 569 | the equivalent of the Python expression: \code{repr(o)}. |
| 570 | Called by the \code{repr()} built-in function and by reverse quotes. |
| 571 | \end{cfuncdesc} |
| 572 | |
| 573 | |
| 574 | \begin{cfuncdesc}{PyObject*}{PyObject_Str}{PyObject *o} |
| 575 | Compute the string representation of object, \code{o}. Returns the |
| 576 | string representation on success, \NULL{} on failure. This is |
| 577 | the equivalent of the Python expression: \code{str(o)}. |
| 578 | Called by the \code{str()} built-in function and by the \code{print} |
| 579 | statement. |
| 580 | \end{cfuncdesc} |
| 581 | |
| 582 | |
| 583 | \begin{cfuncdesc}{int}{PyCallable_Check}{PyObject *o} |
| 584 | Determine if the object \code{o}, is callable. Return 1 if the |
| 585 | object is callable and 0 otherwise. |
| 586 | This function always succeeds. |
| 587 | \end{cfuncdesc} |
| 588 | |
| 589 | |
| 590 | \begin{cfuncdesc}{PyObject*}{PyObject_CallObject}{PyObject *callable_object, PyObject *args} |
| 591 | Call a callable Python object \code{callable_object}, with |
| 592 | arguments given by the tuple \code{args}. If no arguments are |
| 593 | needed, then args may be \NULL{}. Returns the result of the |
| 594 | call on success, or \NULL{} on failure. This is the equivalent |
| 595 | of the Python expression: \code{apply(o, args)}. |
| 596 | \end{cfuncdesc} |
| 597 | |
| 598 | \begin{cfuncdesc}{PyObject*}{PyObject_CallFunction}{PyObject *callable_object, char *format, ...} |
| 599 | Call a callable Python object \code{callable_object}, with a |
| 600 | variable number of C arguments. The C arguments are described |
| 601 | using a mkvalue-style format string. The format may be \NULL{}, |
| 602 | indicating that no arguments are provided. Returns the |
| 603 | result of the call on success, or \NULL{} on failure. This is |
| 604 | the equivalent of the Python expression: \code{apply(o,args)}. |
| 605 | \end{cfuncdesc} |
| 606 | |
| 607 | |
| 608 | \begin{cfuncdesc}{PyObject*}{PyObject_CallMethod}{PyObject *o, char *m, char *format, ...} |
| 609 | Call the method named \code{m} of object \code{o} with a variable number of |
| 610 | C arguments. The C arguments are described by a mkvalue |
| 611 | format string. The format may be \NULL{}, indicating that no |
| 612 | arguments are provided. Returns the result of the call on |
| 613 | success, or \NULL{} on failure. This is the equivalent of the |
| 614 | Python expression: \code{o.method(args)}. |
| 615 | Note that Special method names, such as "\code{__add__}", |
| 616 | "\code{__getitem__}", and so on are not supported. The specific |
| 617 | abstract-object routines for these must be used. |
| 618 | \end{cfuncdesc} |
| 619 | |
| 620 | |
| 621 | \begin{cfuncdesc}{int}{PyObject_Hash}{PyObject *o} |
| 622 | Compute and return the hash value of an object \code{o}. On |
| 623 | failure, return -1. This is the equivalent of the Python |
| 624 | expression: \code{hash(o)}. |
| 625 | \end{cfuncdesc} |
| 626 | |
| 627 | |
| 628 | \begin{cfuncdesc}{int}{PyObject_IsTrue}{PyObject *o} |
| 629 | Returns 1 if the object \code{o} is considered to be true, and |
| 630 | 0 otherwise. This is equivalent to the Python expression: |
| 631 | \code{not not o}. |
| 632 | This function always succeeds. |
| 633 | \end{cfuncdesc} |
| 634 | |
| 635 | |
| 636 | \begin{cfuncdesc}{PyObject*}{PyObject_Type}{PyObject *o} |
| 637 | On success, returns a type object corresponding to the object |
| 638 | type of object \code{o}. On failure, returns \NULL{}. This is |
| 639 | equivalent to the Python expression: \code{type(o)}. |
| 640 | \end{cfuncdesc} |
| 641 | |
| 642 | \begin{cfuncdesc}{int}{PyObject_Length}{PyObject *o} |
| 643 | Return the length of object \code{o}. If the object \code{o} provides |
| 644 | both sequence and mapping protocols, the sequence length is |
| 645 | returned. On error, -1 is returned. This is the equivalent |
| 646 | to the Python expression: \code{len(o)}. |
| 647 | \end{cfuncdesc} |
| 648 | |
| 649 | |
| 650 | \begin{cfuncdesc}{PyObject*}{PyObject_GetItem}{PyObject *o, PyObject *key} |
| 651 | Return element of \code{o} corresponding to the object \code{key} or \NULL{} |
| 652 | on failure. This is the equivalent of the Python expression: |
| 653 | \code{o[key]}. |
| 654 | \end{cfuncdesc} |
| 655 | |
| 656 | |
| 657 | \begin{cfuncdesc}{int}{PyObject_SetItem}{PyObject *o, PyObject *key, PyObject *v} |
| 658 | Map the object \code{key} to the value \code{v}. |
| 659 | Returns -1 on failure. This is the equivalent |
| 660 | of the Python statement: \code{o[key]=v}. |
| 661 | \end{cfuncdesc} |
| 662 | |
| 663 | |
| 664 | \begin{cfuncdesc}{int}{PyObject_DelItem}{PyObject *o, PyObject *key, PyObject *v} |
| 665 | Delete the mapping for \code{key} from \code{*o}. Returns -1 |
| 666 | on failure. |
| 667 | This is the equivalent of the Python statement: del o[key]. |
| 668 | \end{cfuncdesc} |
| 669 | |
| 670 | |
| 671 | \section{Number Protocol} |
| 672 | |
| 673 | \begin{cfuncdesc}{int}{PyNumber_Check}{PyObject *o} |
| 674 | Returns 1 if the object \code{o} provides numeric protocols, and |
| 675 | false otherwise. |
| 676 | This function always succeeds. |
| 677 | \end{cfuncdesc} |
| 678 | |
| 679 | |
| 680 | \begin{cfuncdesc}{PyObject*}{PyNumber_Add}{PyObject *o1, PyObject *o2} |
| 681 | Returns the result of adding \code{o1} and \code{o2}, or null on failure. |
| 682 | This is the equivalent of the Python expression: \code{o1+o2}. |
| 683 | \end{cfuncdesc} |
| 684 | |
| 685 | |
| 686 | \begin{cfuncdesc}{PyObject*}{PyNumber_Subtract}{PyObject *o1, PyObject *o2} |
| 687 | Returns the result of subtracting \code{o2} from \code{o1}, or null on |
| 688 | failure. This is the equivalent of the Python expression: |
| 689 | \code{o1-o2}. |
| 690 | \end{cfuncdesc} |
| 691 | |
| 692 | |
| 693 | \begin{cfuncdesc}{PyObject*}{PyNumber_Multiply}{PyObject *o1, PyObject *o2} |
| 694 | Returns the result of multiplying \code{o1} and \code{o2}, or null on |
| 695 | failure. This is the equivalent of the Python expression: |
| 696 | \code{o1*o2}. |
| 697 | \end{cfuncdesc} |
| 698 | |
| 699 | |
| 700 | \begin{cfuncdesc}{PyObject*}{PyNumber_Divide}{PyObject *o1, PyObject *o2} |
| 701 | Returns the result of dividing \code{o1} by \code{o2}, or null on failure. |
| 702 | This is the equivalent of the Python expression: \code{o1/o2}. |
| 703 | \end{cfuncdesc} |
| 704 | |
| 705 | |
| 706 | \begin{cfuncdesc}{PyObject*}{PyNumber_Remainder}{PyObject *o1, PyObject *o2} |
| 707 | Returns the remainder of dividing \code{o1} by \code{o2}, or null on |
| 708 | failure. This is the equivalent of the Python expression: |
| 709 | \code{o1\%o2}. |
| 710 | \end{cfuncdesc} |
| 711 | |
| 712 | |
| 713 | \begin{cfuncdesc}{PyObject*}{PyNumber_Divmod}{PyObject *o1, PyObject *o2} |
| 714 | See the built-in function divmod. Returns \NULL{} on failure. |
| 715 | This is the equivalent of the Python expression: |
| 716 | \code{divmod(o1,o2)}. |
| 717 | \end{cfuncdesc} |
| 718 | |
| 719 | |
| 720 | \begin{cfuncdesc}{PyObject*}{PyNumber_Power}{PyObject *o1, PyObject *o2, PyObject *o3} |
| 721 | See the built-in function pow. Returns \NULL{} on failure. |
| 722 | This is the equivalent of the Python expression: |
| 723 | \code{pow(o1,o2,o3)}, where \code{o3} is optional. |
| 724 | \end{cfuncdesc} |
| 725 | |
| 726 | |
| 727 | \begin{cfuncdesc}{PyObject*}{PyNumber_Negative}{PyObject *o} |
| 728 | Returns the negation of \code{o} on success, or null on failure. |
| 729 | This is the equivalent of the Python expression: \code{-o}. |
| 730 | \end{cfuncdesc} |
| 731 | |
| 732 | |
| 733 | \begin{cfuncdesc}{PyObject*}{PyNumber_Positive}{PyObject *o} |
| 734 | Returns \code{o} on success, or \NULL{} on failure. |
| 735 | This is the equivalent of the Python expression: \code{+o}. |
| 736 | \end{cfuncdesc} |
| 737 | |
| 738 | |
| 739 | \begin{cfuncdesc}{PyObject*}{PyNumber_Absolute}{PyObject *o} |
| 740 | Returns the absolute value of \code{o}, or null on failure. This is |
| 741 | the equivalent of the Python expression: \code{abs(o)}. |
| 742 | \end{cfuncdesc} |
| 743 | |
| 744 | |
| 745 | \begin{cfuncdesc}{PyObject*}{PyNumber_Invert}{PyObject *o} |
| 746 | Returns the bitwise negation of \code{o} on success, or \NULL{} on |
| 747 | failure. This is the equivalent of the Python expression: |
| 748 | \code{~o}. |
| 749 | \end{cfuncdesc} |
| 750 | |
| 751 | |
| 752 | \begin{cfuncdesc}{PyObject*}{PyNumber_Lshift}{PyObject *o1, PyObject *o2} |
| 753 | Returns the result of left shifting \code{o1} by \code{o2} on success, or |
| 754 | \NULL{} on failure. This is the equivalent of the Python |
| 755 | expression: \code{o1 << o2}. |
| 756 | \end{cfuncdesc} |
| 757 | |
| 758 | |
| 759 | \begin{cfuncdesc}{PyObject*}{PyNumber_Rshift}{PyObject *o1, PyObject *o2} |
| 760 | Returns the result of right shifting \code{o1} by \code{o2} on success, or |
| 761 | \NULL{} on failure. This is the equivalent of the Python |
| 762 | expression: \code{o1 >> o2}. |
| 763 | \end{cfuncdesc} |
| 764 | |
| 765 | |
| 766 | \begin{cfuncdesc}{PyObject*}{PyNumber_And}{PyObject *o1, PyObject *o2} |
| 767 | Returns the result of "anding" \code{o2} and \code{o2} on success and \NULL{} |
| 768 | on failure. This is the equivalent of the Python |
| 769 | expression: \code{o1 and o2}. |
| 770 | \end{cfuncdesc} |
| 771 | |
| 772 | |
| 773 | \begin{cfuncdesc}{PyObject*}{PyNumber_Xor}{PyObject *o1, PyObject *o2} |
| 774 | Returns the bitwise exclusive or of \code{o1} by \code{o2} on success, or |
| 775 | \NULL{} on failure. This is the equivalent of the Python |
| 776 | expression: \code{o1\^{ }o2}. |
| 777 | \end{cfuncdesc} |
| 778 | |
| 779 | \begin{cfuncdesc}{PyObject*}{PyNumber_Or}{PyObject *o1, PyObject *o2} |
| 780 | Returns the result or \code{o1} and \code{o2} on success, or \NULL{} on |
| 781 | failure. This is the equivalent of the Python expression: |
| 782 | \code{o1 or o2}. |
| 783 | \end{cfuncdesc} |
| 784 | |
| 785 | |
| 786 | \begin{cfuncdesc}{PyObject*}{PyNumber_Coerce}{PyObject *o1, PyObject *o2} |
| 787 | This function takes the addresses of two variables of type |
| 788 | \code{PyObject*}. |
| 789 | |
| 790 | If the objects pointed to by \code{*p1} and \code{*p2} have the same type, |
| 791 | increment their reference count and return 0 (success). |
| 792 | If the objects can be converted to a common numeric type, |
| 793 | replace \code{*p1} and \code{*p2} by their converted value (with 'new' |
| 794 | reference counts), and return 0. |
| 795 | If no conversion is possible, or if some other error occurs, |
| 796 | return -1 (failure) and don't increment the reference counts. |
| 797 | The call \code{PyNumber_Coerce(\&o1, \&o2)} is equivalent to the Python |
| 798 | statement \code{o1, o2 = coerce(o1, o2)}. |
| 799 | \end{cfuncdesc} |
| 800 | |
| 801 | |
| 802 | \begin{cfuncdesc}{PyObject*}{PyNumber_Int}{PyObject *o} |
| 803 | Returns the \code{o} converted to an integer object on success, or |
| 804 | \NULL{} on failure. This is the equivalent of the Python |
| 805 | expression: \code{int(o)}. |
| 806 | \end{cfuncdesc} |
| 807 | |
| 808 | |
| 809 | \begin{cfuncdesc}{PyObject*}{PyNumber_Long}{PyObject *o} |
| 810 | Returns the \code{o} converted to a long integer object on success, |
| 811 | or \NULL{} on failure. This is the equivalent of the Python |
| 812 | expression: \code{long(o)}. |
| 813 | \end{cfuncdesc} |
| 814 | |
| 815 | |
| 816 | \begin{cfuncdesc}{PyObject*}{PyNumber_Float}{PyObject *o} |
| 817 | Returns the \code{o} converted to a float object on success, or \NULL{} |
| 818 | on failure. This is the equivalent of the Python expression: |
| 819 | \code{float(o)}. |
| 820 | \end{cfuncdesc} |
| 821 | |
| 822 | |
| 823 | \section{Sequence protocol} |
| 824 | |
| 825 | \begin{cfuncdesc}{int}{PySequence_Check}{PyObject *o} |
| 826 | Return 1 if the object provides sequence protocol, and 0 |
| 827 | otherwise. |
| 828 | This function always succeeds. |
| 829 | \end{cfuncdesc} |
| 830 | |
| 831 | |
| 832 | \begin{cfuncdesc}{PyObject*}{PySequence_Concat}{PyObject *o1, PyObject *o2} |
| 833 | Return the concatination of \code{o1} and \code{o2} on success, and \NULL{} on |
| 834 | failure. This is the equivalent of the Python |
| 835 | expression: \code{o1+o2}. |
| 836 | \end{cfuncdesc} |
| 837 | |
| 838 | |
| 839 | \begin{cfuncdesc}{PyObject*}{PySequence_Repeat}{PyObject *o, int count} |
| 840 | Return the result of repeating sequence object \code{o} count times, |
| 841 | or \NULL{} on failure. This is the equivalent of the Python |
| 842 | expression: \code{o*count}. |
| 843 | \end{cfuncdesc} |
| 844 | |
| 845 | |
| 846 | \begin{cfuncdesc}{PyObject*}{PySequence_GetItem}{PyObject *o, int i} |
| 847 | Return the ith element of \code{o}, or \NULL{} on failure. This is the |
| 848 | equivalent of the Python expression: \code{o[i]}. |
| 849 | \end{cfuncdesc} |
| 850 | |
| 851 | |
| 852 | \begin{cfuncdesc}{PyObject*}{PySequence_GetSlice}{PyObject *o, int i1, int i2} |
| 853 | Return the slice of sequence object \code{o} between \code{i1} and \code{i2}, or |
| 854 | \NULL{} on failure. This is the equivalent of the Python |
| 855 | expression, \code{o[i1:i2]}. |
| 856 | \end{cfuncdesc} |
| 857 | |
| 858 | |
| 859 | \begin{cfuncdesc}{int}{PySequence_SetItem}{PyObject *o, int i, PyObject *v} |
| 860 | Assign object \code{v} to the \code{i}th element of \code{o}. |
| 861 | Returns -1 on failure. This is the equivalent of the Python |
| 862 | statement, \code{o[i]=v}. |
| 863 | \end{cfuncdesc} |
| 864 | |
| 865 | \begin{cfuncdesc}{int}{PySequence_DelItem}{PyObject *o, int i} |
| 866 | Delete the \code{i}th element of object \code{v}. Returns |
| 867 | -1 on failure. This is the equivalent of the Python |
| 868 | statement: \code{del o[i]}. |
| 869 | \end{cfuncdesc} |
| 870 | |
| 871 | \begin{cfuncdesc}{int}{PySequence_SetSlice}{PyObject *o, int i1, int i2, PyObject *v} |
| 872 | Assign the sequence object \code{v} to the slice in sequence |
| 873 | object \code{o} from \code{i1} to \code{i2}. This is the equivalent of the Python |
| 874 | statement, \code{o[i1:i2]=v}. |
| 875 | \end{cfuncdesc} |
| 876 | |
| 877 | \begin{cfuncdesc}{int}{PySequence_DelSlice}{PyObject *o, int i1, int i2} |
| 878 | Delete the slice in sequence object, \code{o}, from \code{i1} to \code{i2}. |
| 879 | Returns -1 on failure. This is the equivalent of the Python |
| 880 | statement: \code{del o[i1:i2]}. |
| 881 | \end{cfuncdesc} |
| 882 | |
| 883 | \begin{cfuncdesc}{PyObject*}{PySequence_Tuple}{PyObject *o} |
| 884 | Returns the \code{o} as a tuple on success, and \NULL{} on failure. |
| 885 | This is equivalent to the Python expression: \code{tuple(o)}. |
| 886 | \end{cfuncdesc} |
| 887 | |
| 888 | \begin{cfuncdesc}{int}{PySequence_Count}{PyObject *o, PyObject *value} |
| 889 | Return the number of occurrences of \code{value} on \code{o}, that is, |
| 890 | return the number of keys for which \code{o[key]==value}. On |
| 891 | failure, return -1. This is equivalent to the Python |
| 892 | expression: \code{o.count(value)}. |
| 893 | \end{cfuncdesc} |
| 894 | |
| 895 | \begin{cfuncdesc}{int}{PySequence_In}{PyObject *o, PyObject *value} |
| 896 | Determine if \code{o} contains \code{value}. If an item in \code{o} is equal to |
| 897 | \code{value}, return 1, otherwise return 0. On error, return -1. This |
| 898 | is equivalent to the Python expression: \code{value in o}. |
| 899 | \end{cfuncdesc} |
| 900 | |
| 901 | \begin{cfuncdesc}{int}{PySequence_Index}{PyObject *o, PyObject *value} |
| 902 | Return the first index for which \code{o[i]=value}. On error, |
| 903 | return -1. This is equivalent to the Python |
| 904 | expression: \code{o.index(value)}. |
| 905 | \end{cfuncdesc} |
| 906 | |
| 907 | \section{Mapping protocol} |
| 908 | |
| 909 | \begin{cfuncdesc}{int}{PyMapping_Check}{PyObject *o} |
| 910 | Return 1 if the object provides mapping protocol, and 0 |
| 911 | otherwise. |
| 912 | This function always succeeds. |
| 913 | \end{cfuncdesc} |
| 914 | |
| 915 | |
| 916 | \begin{cfuncdesc}{int}{PyMapping_Length}{PyObject *o} |
| 917 | Returns the number of keys in object \code{o} on success, and -1 on |
| 918 | failure. For objects that do not provide sequence protocol, |
| 919 | this is equivalent to the Python expression: \code{len(o)}. |
| 920 | \end{cfuncdesc} |
| 921 | |
| 922 | |
| 923 | \begin{cfuncdesc}{int}{PyMapping_DelItemString}{PyObject *o, char *key} |
| 924 | Remove the mapping for object \code{key} from the object \code{o}. |
| 925 | Return -1 on failure. This is equivalent to |
| 926 | the Python statement: \code{del o[key]}. |
| 927 | \end{cfuncdesc} |
| 928 | |
| 929 | |
| 930 | \begin{cfuncdesc}{int}{PyMapping_DelItem}{PyObject *o, PyObject *key} |
| 931 | Remove the mapping for object \code{key} from the object \code{o}. |
| 932 | Return -1 on failure. This is equivalent to |
| 933 | the Python statement: \code{del o[key]}. |
| 934 | \end{cfuncdesc} |
| 935 | |
| 936 | |
| 937 | \begin{cfuncdesc}{int}{PyMapping_HasKeyString}{PyObject *o, char *key} |
| 938 | On success, return 1 if the mapping object has the key \code{key} |
| 939 | and 0 otherwise. This is equivalent to the Python expression: |
| 940 | \code{o.has_key(key)}. |
| 941 | This function always succeeds. |
| 942 | \end{cfuncdesc} |
| 943 | |
| 944 | |
| 945 | \begin{cfuncdesc}{int}{PyMapping_HasKey}{PyObject *o, PyObject *key} |
| 946 | Return 1 if the mapping object has the key \code{key} |
| 947 | and 0 otherwise. This is equivalent to the Python expression: |
| 948 | \code{o.has_key(key)}. |
| 949 | This function always succeeds. |
| 950 | \end{cfuncdesc} |
| 951 | |
| 952 | |
| 953 | \begin{cfuncdesc}{PyObject*}{PyMapping_Keys}{PyObject *o} |
| 954 | On success, return a list of the keys in object \code{o}. On |
| 955 | failure, return \NULL{}. This is equivalent to the Python |
| 956 | expression: \code{o.keys()}. |
| 957 | \end{cfuncdesc} |
| 958 | |
| 959 | |
| 960 | \begin{cfuncdesc}{PyObject*}{PyMapping_Values}{PyObject *o} |
| 961 | On success, return a list of the values in object \code{o}. On |
| 962 | failure, return \NULL{}. This is equivalent to the Python |
| 963 | expression: \code{o.values()}. |
| 964 | \end{cfuncdesc} |
| 965 | |
| 966 | |
| 967 | \begin{cfuncdesc}{PyObject*}{PyMapping_Items}{PyObject *o} |
| 968 | On success, return a list of the items in object \code{o}, where |
| 969 | each item is a tuple containing a key-value pair. On |
| 970 | failure, return \NULL{}. This is equivalent to the Python |
| 971 | expression: \code{o.items()}. |
| 972 | \end{cfuncdesc} |
| 973 | |
| 974 | \begin{cfuncdesc}{int}{PyMapping_Clear}{PyObject *o} |
| 975 | Make object \code{o} empty. Returns 1 on success and 0 on failure. |
| 976 | This is equivalent to the Python statement: |
| 977 | \code{for key in o.keys(): del o[key]} |
| 978 | \end{cfuncdesc} |
| 979 | |
| 980 | |
| 981 | \begin{cfuncdesc}{PyObject*}{PyMapping_GetItemString}{PyObject *o, char *key} |
| 982 | Return element of \code{o} corresponding to the object \code{key} or \NULL{} |
| 983 | on failure. This is the equivalent of the Python expression: |
| 984 | \code{o[key]}. |
| 985 | \end{cfuncdesc} |
| 986 | |
| 987 | \begin{cfuncdesc}{PyObject*}{PyMapping_SetItemString}{PyObject *o, char *key, PyObject *v} |
| 988 | Map the object \code{key} to the value \code{v} in object \code{o}. Returns |
| 989 | -1 on failure. This is the equivalent of the Python |
| 990 | statement: \code{o[key]=v}. |
| 991 | \end{cfuncdesc} |
| 992 | |
| 993 | |
| 994 | \section{Constructors} |
| 995 | |
| 996 | \begin{cfuncdesc}{PyObject*}{PyFile_FromString}{char *file_name, char *mode} |
| 997 | On success, returns a new file object that is opened on the |
| 998 | file given by \code{file_name}, with a file mode given by \code{mode}, |
| 999 | where \code{mode} has the same semantics as the standard C routine, |
| 1000 | fopen. On failure, return -1. |
| 1001 | \end{cfuncdesc} |
| 1002 | |
| 1003 | \begin{cfuncdesc}{PyObject*}{PyFile_FromFile}{FILE *fp, char *file_name, char *mode, int close_on_del} |
| 1004 | Return a new file object for an already opened standard C |
| 1005 | file pointer, \code{fp}. A file name, \code{file_name}, and open mode, |
| 1006 | \code{mode}, must be provided as well as a flag, \code{close_on_del}, that |
| 1007 | indicates whether the file is to be closed when the file |
| 1008 | object is destroyed. On failure, return -1. |
| 1009 | \end{cfuncdesc} |
| 1010 | |
| 1011 | \begin{cfuncdesc}{PyObject*}{PyFloat_FromDouble}{double v} |
| 1012 | Returns a new float object with the value \code{v} on success, and |
| 1013 | \NULL{} on failure. |
| 1014 | \end{cfuncdesc} |
| 1015 | |
| 1016 | \begin{cfuncdesc}{PyObject*}{PyInt_FromLong}{long v} |
| 1017 | Returns a new int object with the value \code{v} on success, and |
| 1018 | \NULL{} on failure. |
| 1019 | \end{cfuncdesc} |
| 1020 | |
| 1021 | \begin{cfuncdesc}{PyObject*}{PyList_New}{int l} |
| 1022 | Returns a new list of length \code{l} on success, and \NULL{} on |
| 1023 | failure. |
| 1024 | \end{cfuncdesc} |
| 1025 | |
| 1026 | \begin{cfuncdesc}{PyObject*}{PyLong_FromLong}{long v} |
| 1027 | Returns a new long object with the value \code{v} on success, and |
| 1028 | \NULL{} on failure. |
| 1029 | \end{cfuncdesc} |
| 1030 | |
| 1031 | \begin{cfuncdesc}{PyObject*}{PyLong_FromDouble}{double v} |
| 1032 | Returns a new long object with the value \code{v} on success, and |
| 1033 | \NULL{} on failure. |
| 1034 | \end{cfuncdesc} |
| 1035 | |
| 1036 | \begin{cfuncdesc}{PyObject*}{PyDict_New}{} |
| 1037 | Returns a new empty dictionary on success, and \NULL{} on |
| 1038 | failure. |
| 1039 | \end{cfuncdesc} |
| 1040 | |
| 1041 | \begin{cfuncdesc}{PyObject*}{PyString_FromString}{char *v} |
| 1042 | Returns a new string object with the value \code{v} on success, and |
| 1043 | \NULL{} on failure. |
| 1044 | \end{cfuncdesc} |
| 1045 | |
| 1046 | \begin{cfuncdesc}{PyObject*}{PyString_FromStringAndSize}{char *v, int l} |
| 1047 | Returns a new string object with the value \code{v} and length \code{l} |
| 1048 | on success, and \NULL{} on failure. |
| 1049 | \end{cfuncdesc} |
| 1050 | |
| 1051 | \begin{cfuncdesc}{PyObject*}{PyTuple_New}{int l} |
| 1052 | Returns a new tuple of length \code{l} on success, and \NULL{} on |
| 1053 | failure. |
| 1054 | \end{cfuncdesc} |
| 1055 | |
| 1056 | |
| 1057 | \chapter{Concrete Objects Layer} |
| 1058 | |
| 1059 | The functions in this chapter are specific to certain Python object |
| 1060 | types. Passing them an object of the wrong type is not a good idea; |
| 1061 | if you receive an object from a Python program and you are not sure |
| 1062 | that it has the right type, you must perform a type check first; |
| 1063 | e.g. to check that an object is a dictionary, use |
| 1064 | \code{PyDict_Check()}. |
| 1065 | |
| 1066 | |
| 1067 | \chapter{Defining New Object Types} |
| 1068 | |
| 1069 | \begin{cfuncdesc}{PyObject *}{_PyObject_New}{PyTypeObject *type} |
| 1070 | \end{cfuncdesc} |
| 1071 | |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 1072 | \begin{cfuncdesc}{PyObject *}{_PyObject_NewVar}{PyTypeObject *type, int size} |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1073 | \end{cfuncdesc} |
| 1074 | |
Guido van Rossum | ae110af | 1997-05-22 20:11:52 +0000 | [diff] [blame] | 1075 | \begin{cfuncdesc}{TYPE}{_PyObject_NEW}{TYPE, PyTypeObject *} |
| 1076 | \end{cfuncdesc} |
| 1077 | |
| 1078 | \begin{cfuncdesc}{TYPE}{_PyObject_NEW_VAR}{TYPE, PyTypeObject *, int size} |
| 1079 | \end{cfuncdesc} |
| 1080 | |
| 1081 | XXX To be done: |
| 1082 | |
| 1083 | PyObject, PyVarObject |
| 1084 | |
| 1085 | PyObject_HEAD, PyObject_HEAD_INIT, PyObject_VAR_HEAD |
| 1086 | |
| 1087 | Typedefs: |
| 1088 | unaryfunc, binaryfunc, ternaryfunc, inquiry, coercion, intargfunc, |
| 1089 | intintargfunc, intobjargproc, intintobjargproc, objobjargproc, |
| 1090 | getreadbufferproc, getwritebufferproc, getsegcountproc, |
| 1091 | destructor, printfunc, getattrfunc, getattrofunc, setattrfunc, |
| 1092 | setattrofunc, cmpfunc, reprfunc, hashfunc |
| 1093 | |
| 1094 | PyNumberMethods |
| 1095 | |
| 1096 | PySequenceMethods |
| 1097 | |
| 1098 | PyMappingMethods |
| 1099 | |
| 1100 | PyBufferProcs |
| 1101 | |
| 1102 | PyTypeObject |
| 1103 | |
| 1104 | DL_IMPORT |
| 1105 | |
| 1106 | PyType_Type |
| 1107 | |
| 1108 | Py*_Check |
| 1109 | |
| 1110 | Py_None, _Py_NoneStruct |
| 1111 | |
| 1112 | _PyObject_New, _PyObject_NewVar |
| 1113 | |
| 1114 | PyObject_NEW, PyObject_NEW_VAR |
| 1115 | |
| 1116 | |
| 1117 | \chapter{Specific Data Types} |
| 1118 | |
| 1119 | This chapter describes the functions that deal with specific types of |
| 1120 | Python objects. It is structured like the ``family tree'' of Python |
| 1121 | object types. |
| 1122 | |
| 1123 | |
| 1124 | \section{Fundamental Objects} |
| 1125 | |
| 1126 | This section describes Python type objects and the singleton object |
| 1127 | \code{None}. |
| 1128 | |
| 1129 | |
| 1130 | \subsection{Type Objects} |
| 1131 | |
| 1132 | \begin{ctypedesc}{PyTypeObject} |
| 1133 | |
| 1134 | \end{ctypedesc} |
| 1135 | |
| 1136 | \begin{cvardesc}{PyObject *}{PyType_Type} |
| 1137 | |
| 1138 | \end{cvardesc} |
| 1139 | |
| 1140 | |
| 1141 | \subsection{The None Object} |
| 1142 | |
| 1143 | \begin{cvardesc}{PyObject *}{Py_None} |
| 1144 | macro |
| 1145 | \end{cvardesc} |
| 1146 | |
| 1147 | |
| 1148 | \section{Sequence Objects} |
| 1149 | |
| 1150 | Generic operations on sequence objects were discussed in the previous |
| 1151 | chapter; this section deals with the specific kinds of sequence |
| 1152 | objects that are intrinsuc to the Python language. |
| 1153 | |
| 1154 | |
| 1155 | \subsection{String Objects} |
| 1156 | |
| 1157 | \begin{ctypedesc}{PyStringObject} |
| 1158 | This subtype of \code{PyObject} represents a Python string object. |
| 1159 | \end{ctypedesc} |
| 1160 | |
| 1161 | \begin{cvardesc}{PyTypeObject}{PyString_Type} |
| 1162 | This instance of \code{PyTypeObject} represents the Python string type. |
| 1163 | \end{cvardesc} |
| 1164 | |
| 1165 | \begin{cfuncdesc}{int}{PyString_Check}{PyObject *o} |
| 1166 | |
| 1167 | \end{cfuncdesc} |
| 1168 | |
| 1169 | \begin{cfuncdesc}{PyObject *}{PyString_FromStringAndSize}{const char *, int} |
| 1170 | |
| 1171 | \end{cfuncdesc} |
| 1172 | |
| 1173 | \begin{cfuncdesc}{PyObject *}{PyString_FromString}{const char *} |
| 1174 | |
| 1175 | \end{cfuncdesc} |
| 1176 | |
| 1177 | \begin{cfuncdesc}{int}{PyString_Size}{PyObject *} |
| 1178 | |
| 1179 | \end{cfuncdesc} |
| 1180 | |
| 1181 | \begin{cfuncdesc}{char *}{PyString_AsString}{PyObject *} |
| 1182 | |
| 1183 | \end{cfuncdesc} |
| 1184 | |
| 1185 | \begin{cfuncdesc}{void}{PyString_Concat}{PyObject **, PyObject *} |
| 1186 | |
| 1187 | \end{cfuncdesc} |
| 1188 | |
| 1189 | \begin{cfuncdesc}{void}{PyString_ConcatAndDel}{PyObject **, PyObject *} |
| 1190 | |
| 1191 | \end{cfuncdesc} |
| 1192 | |
| 1193 | \begin{cfuncdesc}{int}{_PyString_Resize}{PyObject **, int} |
| 1194 | |
| 1195 | \end{cfuncdesc} |
| 1196 | |
| 1197 | \begin{cfuncdesc}{PyObject *}{PyString_Format}{PyObject *, PyObject *} |
| 1198 | |
| 1199 | \end{cfuncdesc} |
| 1200 | |
| 1201 | \begin{cfuncdesc}{void}{PyString_InternInPlace}{PyObject **} |
| 1202 | |
| 1203 | \end{cfuncdesc} |
| 1204 | |
| 1205 | \begin{cfuncdesc}{PyObject *}{PyString_InternFromString}{const char *} |
| 1206 | |
| 1207 | \end{cfuncdesc} |
| 1208 | |
| 1209 | \begin{cfuncdesc}{char *}{PyString_AS_STRING}{PyStringObject *} |
| 1210 | |
| 1211 | \end{cfuncdesc} |
| 1212 | |
| 1213 | \begin{cfuncdesc}{int}{PyString_GET_SIZE}{PyStringObject *} |
| 1214 | |
| 1215 | \end{cfuncdesc} |
| 1216 | |
| 1217 | |
| 1218 | \subsection{Tuple Objects} |
| 1219 | |
| 1220 | \begin{ctypedesc}{PyTupleObject} |
| 1221 | This subtype of \code{PyObject} represents a Python tuple object. |
| 1222 | \end{ctypedesc} |
| 1223 | |
| 1224 | \begin{cvardesc}{PyTypeObject}{PyTuple_Type} |
| 1225 | This instance of \code{PyTypeObject} represents the Python tuple type. |
| 1226 | \end{cvardesc} |
| 1227 | |
| 1228 | \begin{cfuncdesc}{int}{PyTuple_Check}{PyObject *p} |
| 1229 | Return true if the argument is a tuple object. |
| 1230 | \end{cfuncdesc} |
| 1231 | |
| 1232 | \begin{cfuncdesc}{PyTupleObject *}{PyTuple_New}{int s} |
| 1233 | Return a new tuple object of size \code{s} |
| 1234 | \end{cfuncdesc} |
| 1235 | |
| 1236 | \begin{cfuncdesc}{int}{PyTuple_Size}{PyTupleObject *p} |
| 1237 | akes a pointer to a tuple object, and returns the size |
| 1238 | of that tuple. |
| 1239 | \end{cfuncdesc} |
| 1240 | |
| 1241 | \begin{cfuncdesc}{PyObject *}{PyTuple_GetItem}{PyTupleObject *p, int pos} |
| 1242 | returns the object at position \code{pos} in the tuple pointed |
| 1243 | to by \code{p}. |
| 1244 | \end{cfuncdesc} |
| 1245 | |
| 1246 | \begin{cfuncdesc}{PyObject *}{PyTuple_GET_ITEM}{PyTupleObject *p, int pos} |
| 1247 | does the same, but does no checking of it's |
| 1248 | arguments. |
| 1249 | \end{cfuncdesc} |
| 1250 | |
| 1251 | \begin{cfuncdesc}{PyTupleObject *}{PyTuple_GetSlice}{PyTupleObject *p, |
| 1252 | int low, |
| 1253 | int high} |
| 1254 | takes a slice of the tuple pointed to by \code{p} from |
| 1255 | \code{low} to \code{high} and returns it as a new tuple. |
| 1256 | \end{cfuncdesc} |
| 1257 | |
| 1258 | \begin{cfuncdesc}{int}{PyTuple_SetItem}{PyTupleObject *p, |
| 1259 | int pos, |
| 1260 | PyObject *o} |
| 1261 | inserts a reference to object \code{o} at position \code{pos} of |
| 1262 | the tuple pointed to by \code{p}. It returns 0 on success. |
| 1263 | \end{cfuncdesc} |
| 1264 | |
| 1265 | \begin{cfuncdesc}{void}{PyTuple_SET_ITEM}{PyTupleObject *p, |
| 1266 | int pos, |
| 1267 | PyObject *o} |
| 1268 | |
| 1269 | does the same, but does no error checking, and |
| 1270 | should \emph{only} be used to fill in brand new tuples. |
| 1271 | \end{cfuncdesc} |
| 1272 | |
| 1273 | \begin{cfuncdesc}{PyTupleObject *}{_PyTuple_Resize}{PyTupleObject *p, |
| 1274 | int new, |
| 1275 | int last_is_sticky} |
| 1276 | can be used to resize a tuple. Because tuples are |
| 1277 | \emph{supposed} to be immutable, this should only be used if there is only |
| 1278 | one module referencing the object. Do \emph{not} use this if the tuple may |
| 1279 | already be known to some other part of the code. \code{last_is_sticky} is |
| 1280 | a flag - if set, the tuple will grow or shrink at the front, otherwise |
| 1281 | it will grow or shrink at the end. Think of this as destroying the old |
| 1282 | tuple and creating a new one, only more efficiently. |
| 1283 | \end{cfuncdesc} |
| 1284 | |
| 1285 | |
| 1286 | \subsection{List Objects} |
| 1287 | |
| 1288 | \begin{ctypedesc}{PyListObject} |
| 1289 | This subtype of \code{PyObject} represents a Python list object. |
| 1290 | \end{ctypedesc} |
| 1291 | |
| 1292 | \begin{cvardesc}{PyTypeObject}{PyList_Type} |
| 1293 | This instance of \code{PyTypeObject} represents the Python list type. |
| 1294 | \end{cvardesc} |
| 1295 | |
| 1296 | \begin{cfuncdesc}{int}{PyList_Check}{PyObject *p} |
| 1297 | returns true if it's argument is a \code{PyListObject} |
| 1298 | \end{cfuncdesc} |
| 1299 | |
| 1300 | \begin{cfuncdesc}{PyObject *}{PyList_New}{int size} |
| 1301 | |
| 1302 | \end{cfuncdesc} |
| 1303 | |
| 1304 | \begin{cfuncdesc}{int}{PyList_Size}{PyObject *} |
| 1305 | |
| 1306 | \end{cfuncdesc} |
| 1307 | |
| 1308 | \begin{cfuncdesc}{PyObject *}{PyList_GetItem}{PyObject *, int} |
| 1309 | |
| 1310 | \end{cfuncdesc} |
| 1311 | |
| 1312 | \begin{cfuncdesc}{int}{PyList_SetItem}{PyObject *, int, PyObject *} |
| 1313 | |
| 1314 | \end{cfuncdesc} |
| 1315 | |
| 1316 | \begin{cfuncdesc}{int}{PyList_Insert}{PyObject *, int, PyObject *} |
| 1317 | |
| 1318 | \end{cfuncdesc} |
| 1319 | |
| 1320 | \begin{cfuncdesc}{int}{PyList_Append}{PyObject *, PyObject *} |
| 1321 | |
| 1322 | \end{cfuncdesc} |
| 1323 | |
| 1324 | \begin{cfuncdesc}{PyObject *}{PyList_GetSlice}{PyObject *, int, int} |
| 1325 | |
| 1326 | \end{cfuncdesc} |
| 1327 | |
| 1328 | \begin{cfuncdesc}{int}{PyList_SetSlice}{PyObject *, int, int, PyObject *} |
| 1329 | |
| 1330 | \end{cfuncdesc} |
| 1331 | |
| 1332 | \begin{cfuncdesc}{int}{PyList_Sort}{PyObject *} |
| 1333 | |
| 1334 | \end{cfuncdesc} |
| 1335 | |
| 1336 | \begin{cfuncdesc}{int}{PyList_Reverse}{PyObject *} |
| 1337 | |
| 1338 | \end{cfuncdesc} |
| 1339 | |
| 1340 | \begin{cfuncdesc}{PyObject *}{PyList_AsTuple}{PyObject *} |
| 1341 | |
| 1342 | \end{cfuncdesc} |
| 1343 | |
| 1344 | \begin{cfuncdesc}{PyObject *}{PyList_GET_ITEM}{PyObject *list, int i} |
| 1345 | |
| 1346 | \end{cfuncdesc} |
| 1347 | |
| 1348 | \begin{cfuncdesc}{int}{PyList_GET_SIZE}{PyObject *list} |
| 1349 | |
| 1350 | \end{cfuncdesc} |
| 1351 | |
| 1352 | |
| 1353 | \section{Mapping Objects} |
| 1354 | |
| 1355 | \subsection{Dictionary Objects} |
| 1356 | |
| 1357 | \begin{ctypedesc}{PyDictObject} |
| 1358 | This subtype of \code{PyObject} represents a Python dictionary object. |
| 1359 | \end{ctypedesc} |
| 1360 | |
| 1361 | \begin{cvardesc}{PyTypeObject}{PyDict_Type} |
| 1362 | This instance of \code{PyTypeObject} represents the Python dictionary type. |
| 1363 | \end{cvardesc} |
| 1364 | |
| 1365 | \begin{cfuncdesc}{int}{PyDict_Check}{PyObject *p} |
| 1366 | returns true if it's argument is a PyDictObject |
| 1367 | \end{cfuncdesc} |
| 1368 | |
| 1369 | \begin{cfuncdesc}{PyDictObject *}{PyDict_New}{} |
| 1370 | returns a new empty dictionary. |
| 1371 | \end{cfuncdesc} |
| 1372 | |
| 1373 | \begin{cfuncdesc}{void}{PyDict_Clear}{PyDictObject *p} |
| 1374 | empties an existing dictionary and deletes it. |
| 1375 | \end{cfuncdesc} |
| 1376 | |
| 1377 | \begin{cfuncdesc}{int}{PyDict_SetItem}{PyDictObject *p, |
| 1378 | PyObject *key, |
| 1379 | PyObject *val} |
| 1380 | inserts \code{value} into the dictionary with a key of |
| 1381 | \code{key}. Both \code{key} and \code{value} should be PyObjects, and \code{key} should |
| 1382 | be hashable. |
| 1383 | \end{cfuncdesc} |
| 1384 | |
| 1385 | \begin{cfuncdesc}{int}{PyDict_SetItemString}{PyDictObject *p, |
| 1386 | char *key, |
| 1387 | PyObject *val} |
| 1388 | inserts \code{value} into the dictionary using \code{key} |
| 1389 | as a key. \code{key} should be a char * |
| 1390 | \end{cfuncdesc} |
| 1391 | |
| 1392 | \begin{cfuncdesc}{int}{PyDict_DelItem}{PyDictObject *p, PyObject *key} |
| 1393 | removes the entry in dictionary \code{p} with key \code{key}. |
| 1394 | \code{key} is a PyObject. |
| 1395 | \end{cfuncdesc} |
| 1396 | |
| 1397 | \begin{cfuncdesc}{int}{PyDict_DelItemString}{PyDictObject *p, char *key} |
| 1398 | removes the entry in dictionary \code{p} which has a key |
| 1399 | specified by the \code{char *}\code{key}. |
| 1400 | \end{cfuncdesc} |
| 1401 | |
| 1402 | \begin{cfuncdesc}{PyObject *}{PyDict_GetItem}{PyDictObject *p, PyObject *key} |
| 1403 | returns the object from dictionary \code{p} which has a key |
| 1404 | \code{key}. |
| 1405 | \end{cfuncdesc} |
| 1406 | |
| 1407 | \begin{cfuncdesc}{PyObject *}{PyDict_GetItemString}{PyDictObject *p, char *key} |
| 1408 | does the same, but \code{key} is specified as a |
| 1409 | \code{char *}, rather than a \code{PyObject *}. |
| 1410 | \end{cfuncdesc} |
| 1411 | |
| 1412 | \begin{cfuncdesc}{PyListObject *}{PyDict_Items}{PyDictObject *p} |
| 1413 | returns a PyListObject containing all the items |
| 1414 | from the dictionary, as in the mapping method \code{items()} (see the Reference |
| 1415 | Guide) |
| 1416 | \end{cfuncdesc} |
| 1417 | |
| 1418 | \begin{cfuncdesc}{PyListObject *}{PyDict_Keys}{PyDictObject *p} |
| 1419 | returns a PyListObject containing all the keys |
| 1420 | from the dictionary, as in the mapping method \code{keys()} (see the Reference Guide) |
| 1421 | \end{cfuncdesc} |
| 1422 | |
| 1423 | \begin{cfuncdesc}{PyListObject *}{PyDict_Values}{PyDictObject *p} |
| 1424 | returns a PyListObject containing all the values |
| 1425 | from the dictionary, as in the mapping method \code{values()} (see the Reference Guide) |
| 1426 | \end{cfuncdesc} |
| 1427 | |
| 1428 | \begin{cfuncdesc}{int}{PyDict_Size}{PyDictObject *p} |
| 1429 | returns the number of items in the dictionary. |
| 1430 | \end{cfuncdesc} |
| 1431 | |
| 1432 | \begin{cfuncdesc}{int}{PyDict_Next}{PyDictObject *p, |
| 1433 | int ppos, |
| 1434 | PyObject **pkey, |
| 1435 | PyObject **pvalue} |
| 1436 | |
| 1437 | \end{cfuncdesc} |
| 1438 | |
| 1439 | |
| 1440 | \section{Numeric Objects} |
| 1441 | |
| 1442 | \subsection{Plain Integer Objects} |
| 1443 | |
| 1444 | \begin{ctypedesc}{PyIntObject} |
| 1445 | This subtype of \code{PyObject} represents a Python integer object. |
| 1446 | \end{ctypedesc} |
| 1447 | |
| 1448 | \begin{cvardesc}{PyTypeObject}{PyInt_Type} |
| 1449 | This instance of \code{PyTypeObject} represents the Python plain |
| 1450 | integer type. |
| 1451 | \end{cvardesc} |
| 1452 | |
| 1453 | \begin{cfuncdesc}{int}{PyInt_Check}{PyObject *} |
| 1454 | |
| 1455 | \end{cfuncdesc} |
| 1456 | |
| 1457 | \begin{cfuncdesc}{PyIntObject *}{PyInt_FromLong}{long ival} |
| 1458 | creates a new integer object with a value of \code{ival}. |
| 1459 | |
| 1460 | The current implementation keeps an array of integer objects for all |
| 1461 | integers between -1 and 100, when you create an int in that range you |
| 1462 | actually just get back a reference to the existing object. So it should |
| 1463 | be possible to change the value of 1. I suspect the behaviour of python |
| 1464 | in this case is undefined. :-) |
| 1465 | \end{cfuncdesc} |
| 1466 | |
| 1467 | \begin{cfuncdesc}{long}{PyInt_AS_LONG}{PyIntObject *io} |
| 1468 | returns the value of the object \code{io}. |
| 1469 | \end{cfuncdesc} |
| 1470 | |
| 1471 | \begin{cfuncdesc}{long}{PyInt_AsLong}{PyObject *io} |
| 1472 | will first attempt to cast the object to a PyIntObject, if |
| 1473 | it is not already one, and the return it's value. |
| 1474 | \end{cfuncdesc} |
| 1475 | |
| 1476 | \begin{cfuncdesc}{long}{PyInt_GetMax}{} |
| 1477 | returns the systems idea of the largest int it can handle |
| 1478 | (LONG_MAX, as defined in the system header files) |
| 1479 | \end{cfuncdesc} |
| 1480 | |
| 1481 | |
| 1482 | \subsection{Long Integer Objects} |
| 1483 | |
| 1484 | \begin{ctypedesc}{PyLongObject} |
| 1485 | This subtype of \code{PyObject} represents a Python long integer object. |
| 1486 | \end{ctypedesc} |
| 1487 | |
| 1488 | \begin{cvardesc}{PyTypeObject}{PyLong_Type} |
| 1489 | This instance of \code{PyTypeObject} represents the Python long integer type. |
| 1490 | \end{cvardesc} |
| 1491 | |
| 1492 | \begin{cfuncdesc}{int}{PyLong_Check}{PyObject *p} |
| 1493 | returns true if it's argument is a \code{PyLongObject} |
| 1494 | \end{cfuncdesc} |
| 1495 | |
| 1496 | \begin{cfuncdesc}{PyObject *}{PyLong_FromLong}{long} |
| 1497 | |
| 1498 | \end{cfuncdesc} |
| 1499 | |
| 1500 | \begin{cfuncdesc}{PyObject *}{PyLong_FromUnsignedLong}{unsigned long} |
| 1501 | |
| 1502 | \end{cfuncdesc} |
| 1503 | |
| 1504 | \begin{cfuncdesc}{PyObject *}{PyLong_FromDouble}{double} |
| 1505 | |
| 1506 | \end{cfuncdesc} |
| 1507 | |
| 1508 | \begin{cfuncdesc}{long}{PyLong_AsLong}{PyObject *} |
| 1509 | |
| 1510 | \end{cfuncdesc} |
| 1511 | |
| 1512 | \begin{cfuncdesc}{unsigned long}{PyLong_AsUnsignedLong}{PyObject } |
| 1513 | |
| 1514 | \end{cfuncdesc} |
| 1515 | |
| 1516 | \begin{cfuncdesc}{double}{PyLong_AsDouble}{PyObject *} |
| 1517 | |
| 1518 | \end{cfuncdesc} |
| 1519 | |
| 1520 | \begin{cfuncdesc}{PyObject *}{*PyLong_FromString}{char *, char **, int} |
| 1521 | |
| 1522 | \end{cfuncdesc} |
| 1523 | |
| 1524 | |
| 1525 | \subsection{Floating Point Objects} |
| 1526 | |
| 1527 | \begin{ctypedesc}{PyFloatObject} |
| 1528 | This subtype of \code{PyObject} represents a Python floating point object. |
| 1529 | \end{ctypedesc} |
| 1530 | |
| 1531 | \begin{cvardesc}{PyTypeObject}{PyFloat_Type} |
| 1532 | This instance of \code{PyTypeObject} represents the Python floating |
| 1533 | point type. |
| 1534 | \end{cvardesc} |
| 1535 | |
| 1536 | \begin{cfuncdesc}{int}{PyFloat_Check}{PyObject *p} |
| 1537 | returns true if it's argument is a \code{PyFloatObject} |
| 1538 | \end{cfuncdesc} |
| 1539 | |
| 1540 | \begin{cfuncdesc}{PyObject *}{PyFloat_FromDouble}{double} |
| 1541 | |
| 1542 | \end{cfuncdesc} |
| 1543 | |
| 1544 | \begin{cfuncdesc}{double}{PyFloat_AsDouble}{PyObject *} |
| 1545 | |
| 1546 | \end{cfuncdesc} |
| 1547 | |
| 1548 | \begin{cfuncdesc}{double}{PyFloat_AS_DOUBLE}{PyFloatObject *} |
| 1549 | |
| 1550 | \end{cfuncdesc} |
| 1551 | |
| 1552 | |
| 1553 | \subsection{Complex Number Objects} |
| 1554 | |
| 1555 | \begin{ctypedesc}{Py_complex} |
| 1556 | typedef struct { |
| 1557 | double real; |
| 1558 | double imag; |
| 1559 | } |
| 1560 | \end{ctypedesc} |
| 1561 | |
| 1562 | \begin{ctypedesc}{PyComplexObject} |
| 1563 | This subtype of \code{PyObject} represents a Python complex number object. |
| 1564 | \end{ctypedesc} |
| 1565 | |
| 1566 | \begin{cvardesc}{PyTypeObject}{PyComplex_Type} |
| 1567 | This instance of \code{PyTypeObject} represents the Python complex |
| 1568 | number type. |
| 1569 | \end{cvardesc} |
| 1570 | |
| 1571 | \begin{cfuncdesc}{int}{PyComplex_Check}{PyObject *p} |
| 1572 | returns true if it's argument is a \code{PyComplexObject} |
| 1573 | \end{cfuncdesc} |
| 1574 | |
| 1575 | \begin{cfuncdesc}{Py_complex}{_Py_c_sum}{Py_complex, Py_complex} |
| 1576 | |
| 1577 | \end{cfuncdesc} |
| 1578 | |
| 1579 | \begin{cfuncdesc}{Py_complex}{_Py_c_diff}{Py_complex, Py_complex} |
| 1580 | |
| 1581 | \end{cfuncdesc} |
| 1582 | |
| 1583 | \begin{cfuncdesc}{Py_complex}{_Py_c_neg}{Py_complex} |
| 1584 | |
| 1585 | \end{cfuncdesc} |
| 1586 | |
| 1587 | \begin{cfuncdesc}{Py_complex}{_Py_c_prod}{Py_complex, Py_complex} |
| 1588 | |
| 1589 | \end{cfuncdesc} |
| 1590 | |
| 1591 | \begin{cfuncdesc}{Py_complex}{_Py_c_quot}{Py_complex, Py_complex} |
| 1592 | |
| 1593 | \end{cfuncdesc} |
| 1594 | |
| 1595 | \begin{cfuncdesc}{Py_complex}{_Py_c_pow}{Py_complex, Py_complex} |
| 1596 | |
| 1597 | \end{cfuncdesc} |
| 1598 | |
| 1599 | \begin{cfuncdesc}{PyObject *}{PyComplex_FromCComplex}{Py_complex} |
| 1600 | |
| 1601 | \end{cfuncdesc} |
| 1602 | |
| 1603 | \begin{cfuncdesc}{PyObject *}{PyComplex_FromDoubles}{double real, double imag} |
| 1604 | |
| 1605 | \end{cfuncdesc} |
| 1606 | |
| 1607 | \begin{cfuncdesc}{double}{PyComplex_RealAsDouble}{PyObject *op} |
| 1608 | |
| 1609 | \end{cfuncdesc} |
| 1610 | |
| 1611 | \begin{cfuncdesc}{double}{PyComplex_ImagAsDouble}{PyObject *op} |
| 1612 | |
| 1613 | \end{cfuncdesc} |
| 1614 | |
| 1615 | \begin{cfuncdesc}{Py_complex}{PyComplex_AsCComplex}{PyObject *op} |
| 1616 | |
| 1617 | \end{cfuncdesc} |
| 1618 | |
| 1619 | |
| 1620 | |
| 1621 | \section{Other Objects} |
| 1622 | |
| 1623 | \subsection{File Objects} |
| 1624 | |
| 1625 | \begin{ctypedesc}{PyFileObject} |
| 1626 | This subtype of \code{PyObject} represents a Python file object. |
| 1627 | \end{ctypedesc} |
| 1628 | |
| 1629 | \begin{cvardesc}{PyTypeObject}{PyFile_Type} |
| 1630 | This instance of \code{PyTypeObject} represents the Python file type. |
| 1631 | \end{cvardesc} |
| 1632 | |
| 1633 | \begin{cfuncdesc}{int}{PyFile_Check}{PyObject *p} |
| 1634 | returns true if it's argument is a \code{PyFileObject} |
| 1635 | \end{cfuncdesc} |
| 1636 | |
| 1637 | \begin{cfuncdesc}{PyObject *}{PyFile_FromString}{char *name, char *mode} |
| 1638 | creates a new PyFileObject pointing to the file |
| 1639 | specified in \code{name} with the mode specified in \code{mode} |
| 1640 | \end{cfuncdesc} |
| 1641 | |
| 1642 | \begin{cfuncdesc}{PyObject *}{PyFile_FromFile}{FILE *fp, |
| 1643 | char *name, char *mode, int (*close}) |
| 1644 | creates a new PyFileObject from the already-open \code{fp}. |
| 1645 | The function \code{close} will be called when the file should be closed. |
| 1646 | \end{cfuncdesc} |
| 1647 | |
| 1648 | \begin{cfuncdesc}{FILE *}{PyFile_AsFile}{PyFileObject *p} |
| 1649 | returns the file object associated with \code{p} as a \code{FILE *} |
| 1650 | \end{cfuncdesc} |
| 1651 | |
| 1652 | \begin{cfuncdesc}{PyStringObject *}{PyFile_GetLine}{PyObject *p, int n} |
| 1653 | undocumented as yet |
| 1654 | \end{cfuncdesc} |
| 1655 | |
| 1656 | \begin{cfuncdesc}{PyStringObject *}{PyFile_Name}{PyObject *p} |
| 1657 | returns the name of the file specified by \code{p} as a |
| 1658 | PyStringObject |
| 1659 | \end{cfuncdesc} |
| 1660 | |
| 1661 | \begin{cfuncdesc}{void}{PyFile_SetBufSize}{PyFileObject *p, int n} |
| 1662 | on systems with \code{setvbuf} only |
| 1663 | \end{cfuncdesc} |
| 1664 | |
| 1665 | \begin{cfuncdesc}{int}{PyFile_SoftSpace}{PyFileObject *p, int newflag} |
| 1666 | same as the file object method \code{softspace} |
| 1667 | \end{cfuncdesc} |
| 1668 | |
| 1669 | \begin{cfuncdesc}{int}{PyFile_WriteObject}{PyObject *obj, PyFileObject *p} |
| 1670 | writes object \code{obj} to file object \code{p} |
| 1671 | \end{cfuncdesc} |
| 1672 | |
| 1673 | \begin{cfuncdesc}{int}{PyFile_WriteString}{char *s, PyFileObject *p} |
| 1674 | writes string \code{s} to file object \code{p} |
| 1675 | \end{cfuncdesc} |
| 1676 | |
| 1677 | |
Guido van Rossum | 9231c8f | 1997-05-15 21:43:21 +0000 | [diff] [blame] | 1678 | \input{api.ind} % Index -- must be last |
| 1679 | |
| 1680 | \end{document} |