Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 1 | #ifndef Py_ABSTRACTOBJECT_H |
| 2 | #define Py_ABSTRACTOBJECT_H |
| 3 | #ifdef __cplusplus |
| 4 | extern "C" { |
| 5 | #endif |
| 6 | |
| 7 | /*********************************************************** |
| 8 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 9 | The Netherlands. |
| 10 | |
| 11 | All Rights Reserved |
| 12 | |
| 13 | Permission to use, copy, modify, and distribute this software and its |
| 14 | documentation for any purpose and without fee is hereby granted, |
| 15 | provided that the above copyright notice appear in all copies and that |
| 16 | both that copyright notice and this permission notice appear in |
| 17 | supporting documentation, and that the names of Stichting Mathematisch |
| 18 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 19 | distribution of the software without specific, written prior permission. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 22 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 23 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 24 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 25 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 26 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 27 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 28 | |
| 29 | ******************************************************************/ |
| 30 | |
| 31 | /* Abstract Object Interface (many thanks to Jim Fulton) */ |
| 32 | |
| 33 | /* |
| 34 | PROPOSAL: A Generic Python Object Interface for Python C Modules |
| 35 | |
| 36 | Problem |
| 37 | |
| 38 | Python modules written in C that must access Python objects must do |
| 39 | so through routines whose interfaces are described by a set of |
| 40 | include files. Unfortunately, these routines vary according to the |
| 41 | object accessed. To use these routines, the C programmer must check |
| 42 | the type of the object being used and must call a routine based on |
| 43 | the object type. For example, to access an element of a sequence, |
| 44 | the programmer must determine whether the sequence is a list or a |
| 45 | tuple: |
| 46 | |
| 47 | if(is_tupleobject(o)) |
| 48 | e=gettupleitem(o,i) |
| 49 | else if(is_listitem(o)) |
| 50 | e=getlistitem(o,i) |
| 51 | |
| 52 | If the programmer wants to get an item from another type of object |
| 53 | that provides sequence behavior, there is no clear way to do it |
| 54 | correctly. |
| 55 | |
| 56 | The persistent programmer may peruse object.h and find that the |
| 57 | _typeobject structure provides a means of invoking up to (currently |
| 58 | about) 41 special operators. So, for example, a routine can get an |
| 59 | item from any object that provides sequence behavior. However, to |
| 60 | use this mechanism, the programmer must make their code dependent on |
| 61 | the current Python implementation. |
| 62 | |
| 63 | Also, certain semantics, especially memory management semantics, may |
| 64 | differ by the type of object being used. Unfortunately, these |
| 65 | semantics are not clearly described in the current include files. |
| 66 | An abstract interface providing more consistent semantics is needed. |
| 67 | |
| 68 | Proposal |
| 69 | |
| 70 | I propose the creation of a standard interface (with an associated |
| 71 | library of routines and/or macros) for generically obtaining the |
| 72 | services of Python objects. This proposal can be viewed as one |
| 73 | components of a Python C interface consisting of several components. |
| 74 | |
| 75 | From the viewpoint of of C access to Python services, we have (as |
| 76 | suggested by Guido in off-line discussions): |
| 77 | |
| 78 | - "Very high level layer": two or three functions that let you exec or |
| 79 | eval arbitrary Python code given as a string in a module whose name is |
| 80 | given, passing C values in and getting C values out using |
| 81 | mkvalue/getargs style format strings. This does not require the user |
| 82 | to declare any variables of type "PyObject *". This should be enough |
| 83 | to write a simple application that gets Python code from the user, |
| 84 | execs it, and returns the output or errors. (Error handling must also |
| 85 | be part of this API.) |
| 86 | |
| 87 | - "Abstract objects layer": which is the subject of this proposal. |
| 88 | It has many functions operating on objects, and lest you do many |
| 89 | things from C that you can also write in Python, without going |
| 90 | through the Python parser. |
| 91 | |
| 92 | - "Concrete objects layer": This is the public type-dependent |
| 93 | interface provided by the standard built-in types, such as floats, |
| 94 | strings, and lists. This interface exists and is currently |
| 95 | documented by the collection of include files provides with the |
| 96 | Python distributions. |
| 97 | |
| 98 | From the point of view of Python accessing services provided by C |
| 99 | modules: |
| 100 | |
| 101 | - "Python module interface": this interface consist of the basic |
| 102 | routines used to define modules and their members. Most of the |
| 103 | current extensions-writing guide deals with this interface. |
| 104 | |
| 105 | - "Built-in object interface": this is the interface that a new |
| 106 | built-in type must provide and the mechanisms and rules that a |
| 107 | developer of a new built-in type must use and follow. |
| 108 | |
| 109 | This proposal is a "first-cut" that is intended to spur |
| 110 | discussion. See especially the lists of notes. |
| 111 | |
| 112 | The Python C object interface will provide four protocols: object, |
| 113 | numeric, sequence, and mapping. Each protocol consists of a |
| 114 | collection of related operations. If an operation that is not |
| 115 | provided by a particular type is invoked, then a standard exception, |
| 116 | NotImplementedError is raised with a operation name as an argument. |
| 117 | In addition, for convenience this interface defines a set of |
| 118 | constructors for building objects of built-in types. This is needed |
| 119 | so new objects can be returned from C functions that otherwise treat |
| 120 | objects generically. |
| 121 | |
| 122 | Memory Management |
| 123 | |
| 124 | For all of the functions described in this proposal, if a function |
| 125 | retains a reference to a Python object passed as an argument, then the |
| 126 | function will increase the reference count of the object. It is |
| 127 | unnecessary for the caller to increase the reference count of an |
| 128 | argument in anticipation of the object's retention. |
| 129 | |
| 130 | All Python objects returned from functions should be treated as new |
| 131 | objects. Functions that return objects assume that the caller will |
| 132 | retain a reference and the reference count of the object has already |
| 133 | been incremented to account for this fact. A caller that does not |
| 134 | retain a reference to an object that is returned from a function |
| 135 | must decrement the reference count of the object (using |
| 136 | DECREF(object)) to prevent memory leaks. |
| 137 | |
| 138 | Note that the behavior mentioned here is different from the current |
| 139 | behavior for some objects (e.g. lists and tuples) when certain |
| 140 | type-specific routines are called directly (e.g. setlistitem). The |
| 141 | proposed abstraction layer will provide a consistent memory |
| 142 | management interface, correcting for inconsistent behavior for some |
| 143 | built-in types. |
| 144 | |
| 145 | Protocols |
| 146 | |
| 147 | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/ |
| 148 | |
| 149 | /* Object Protocol: */ |
| 150 | |
| 151 | /* Implemented elsewhere: |
| 152 | |
| 153 | int PyObject_Print(PyObject *o, FILE *fp, int flags); |
| 154 | |
| 155 | Print an object, o, on file, fp. Returns -1 on |
| 156 | error. The flags argument is used to enable certain printing |
| 157 | options. The only option currently supported is Py_Print_RAW. |
| 158 | |
| 159 | (What should be said about Py_Print_RAW?) |
| 160 | |
| 161 | */ |
| 162 | |
| 163 | /* Implemented elsewhere: |
| 164 | |
| 165 | int PyObject_HasAttrString(PyObject *o, char *attr_name); |
| 166 | |
| 167 | Returns 1 if o has the attribute attr_name, and 0 otherwise. |
| 168 | This is equivalent to the Python expression: |
| 169 | hasattr(o,attr_name). |
| 170 | |
| 171 | This function always succeeds. |
| 172 | |
| 173 | */ |
| 174 | |
| 175 | /* Implemented elsewhere: |
| 176 | |
| 177 | PyObject* PyObject_GetAttrString(PyObject *o, char *attr_name); |
| 178 | |
| 179 | Retrieve an attributed named attr_name form object o. |
| 180 | Returns the attribute value on success, or NULL on failure. |
| 181 | This is the equivalent of the Python expression: o.attr_name. |
| 182 | |
| 183 | */ |
| 184 | |
| 185 | /* Implemented elsewhere: |
| 186 | |
| 187 | int PyObject_HasAttr(PyObject *o, PyObject *attr_name); |
| 188 | |
| 189 | Returns 1 if o has the attribute attr_name, and 0 otherwise. |
| 190 | This is equivalent to the Python expression: |
| 191 | hasattr(o,attr_name). |
| 192 | |
| 193 | This function always succeeds. |
| 194 | |
| 195 | */ |
| 196 | |
| 197 | /* Implemented elsewhere: |
| 198 | |
| 199 | PyObject* PyObject_GetAttr(PyObject *o, PyObject *attr_name); |
| 200 | |
| 201 | Retrieve an attributed named attr_name form object o. |
| 202 | Returns the attribute value on success, or NULL on failure. |
| 203 | This is the equivalent of the Python expression: o.attr_name. |
| 204 | |
| 205 | */ |
| 206 | |
| 207 | |
| 208 | /* Implemented elsewhere: |
| 209 | |
| 210 | int PyObject_SetAttrString(PyObject *o, char *attr_name, PyObject *v); |
| 211 | |
| 212 | Set the value of the attribute named attr_name, for object o, |
| 213 | to the value, v. Returns -1 on failure. This is |
| 214 | the equivalent of the Python statement: o.attr_name=v. |
| 215 | |
| 216 | */ |
| 217 | |
| 218 | /* Implemented elsewhere: |
| 219 | |
| 220 | int PyObject_SetAttr(PyObject *o, PyObject *attr_name, PyObject *v); |
| 221 | |
| 222 | Set the value of the attribute named attr_name, for object o, |
| 223 | to the value, v. Returns -1 on failure. This is |
| 224 | the equivalent of the Python statement: o.attr_name=v. |
| 225 | |
| 226 | */ |
| 227 | |
| 228 | /* implemented as a macro: |
| 229 | |
| 230 | int PyObject_DelAttrString(PyObject *o, char *attr_name); |
| 231 | |
| 232 | Delete attribute named attr_name, for object o. Returns |
| 233 | -1 on failure. This is the equivalent of the Python |
| 234 | statement: del o.attr_name. |
| 235 | |
| 236 | */ |
| 237 | #define PyObject_DelAttrString(O,A) PyObject_SetAttrString((O),(A),NULL) |
| 238 | |
| 239 | /* implemented as a macro: |
| 240 | |
| 241 | int PyObject_DelAttr(PyObject *o, PyObject *attr_name); |
| 242 | |
| 243 | Delete attribute named attr_name, for object o. Returns -1 |
| 244 | on failure. This is the equivalent of the Python |
| 245 | statement: del o.attr_name. |
| 246 | |
| 247 | */ |
| 248 | #define PyObject_DelAttr(O,A) PyObject_SetAttr((O),(A),NULL) |
| 249 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 250 | int PyObject_Cmp Py_PROTO((PyObject *o1, PyObject *o2, int *result)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 251 | |
| 252 | /* |
| 253 | Compare the values of o1 and o2 using a routine provided by |
| 254 | o1, if one exists, otherwise with a routine provided by o2. |
| 255 | The result of the comparison is returned in result. Returns |
| 256 | -1 on failure. This is the equivalent of the Python |
| 257 | statement: result=cmp(o1,o2). |
| 258 | |
| 259 | */ |
| 260 | |
| 261 | /* Implemented elsewhere: |
| 262 | |
| 263 | int PyObject_Compare(PyObject *o1, PyObject *o2); |
| 264 | |
| 265 | Compare the values of o1 and o2 using a routine provided by |
| 266 | o1, if one exists, otherwise with a routine provided by o2. |
| 267 | Returns the result of the comparison on success. On error, |
| 268 | the value returned is undefined. This is equivalent to the |
| 269 | Python expression: cmp(o1,o2). |
| 270 | |
| 271 | */ |
| 272 | |
| 273 | /* Implemented elsewhere: |
| 274 | |
| 275 | PyObject *PyObject_Repr(PyObject *o); |
| 276 | |
| 277 | Compute the string representation of object, o. Returns the |
| 278 | string representation on success, NULL on failure. This is |
| 279 | the equivalent of the Python expression: repr(o). |
| 280 | |
| 281 | Called by the repr() built-in function and by reverse quotes. |
| 282 | |
| 283 | */ |
| 284 | |
| 285 | /* Implemented elsewhere: |
| 286 | |
| 287 | PyObject *PyObject_Str(PyObject *o); |
| 288 | |
| 289 | Compute the string representation of object, o. Returns the |
| 290 | string representation on success, NULL on failure. This is |
| 291 | the equivalent of the Python expression: str(o).) |
| 292 | |
| 293 | Called by the str() built-in function and by the print |
| 294 | statement. |
| 295 | |
| 296 | */ |
| 297 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 298 | int PyCallable_Check Py_PROTO((PyObject *o)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 299 | |
| 300 | /* |
| 301 | Determine if the object, o, is callable. Return 1 if the |
| 302 | object is callable and 0 otherwise. |
| 303 | |
| 304 | This function always succeeds. |
| 305 | |
| 306 | */ |
| 307 | |
| 308 | |
| 309 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 310 | PyObject *PyObject_CallObject Py_PROTO((PyObject *callable_object, |
| 311 | PyObject *args)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 312 | |
| 313 | /* |
| 314 | |
| 315 | Call a callable Python object, callable_object, with |
| 316 | arguments given by the tuple, args. If no arguments are |
| 317 | needed, then args may be NULL. Returns the result of the |
| 318 | call on success, or NULL on failure. This is the equivalent |
| 319 | of the Python expression: apply(o,args). |
| 320 | |
| 321 | */ |
| 322 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 323 | PyObject *PyObject_CallFunction Py_PROTO((PyObject *callable_object, |
| 324 | char *format, ...)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 325 | |
| 326 | /* |
| 327 | Call a callable Python object, callable_object, with a |
| 328 | variable number of C arguments. The C arguments are described |
| 329 | using a mkvalue-style format string. The format may be NULL, |
| 330 | indicating that no arguments are provided. Returns the |
| 331 | result of the call on success, or NULL on failure. This is |
| 332 | the equivalent of the Python expression: apply(o,args). |
| 333 | |
| 334 | */ |
| 335 | |
| 336 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 337 | PyObject *PyObject_CallMethod Py_PROTO((PyObject *o, char *m, |
| 338 | char *format, ...)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 339 | |
| 340 | /* |
| 341 | Call the method named m of object o with a variable number of |
| 342 | C arguments. The C arguments are described by a mkvalue |
| 343 | format string. The format may be NULL, indicating that no |
| 344 | arguments are provided. Returns the result of the call on |
| 345 | success, or NULL on failure. This is the equivalent of the |
| 346 | Python expression: o.method(args). |
| 347 | |
| 348 | Note that Special method names, such as "__add__", |
| 349 | "__getitem__", and so on are not supported. The specific |
| 350 | abstract-object routines for these must be used. |
| 351 | |
| 352 | */ |
| 353 | |
| 354 | |
| 355 | /* Implemented elsewhere: |
| 356 | |
| 357 | long PyObject_Hash(PyObject *o); |
| 358 | |
| 359 | Compute and return the hash, hash_value, of an object, o. On |
| 360 | failure, return -1. This is the equivalent of the Python |
| 361 | expression: hash(o). |
| 362 | |
| 363 | */ |
| 364 | |
| 365 | |
| 366 | /* Implemented elsewhere: |
| 367 | |
| 368 | int PyObject_IsTrue(PyObject *o); |
| 369 | |
| 370 | Returns 1 if the object, o, is considered to be true, and |
| 371 | 0 otherwise. This is equivalent to the Python expression: |
| 372 | not not o |
| 373 | |
| 374 | This function always succeeds. |
| 375 | |
| 376 | */ |
| 377 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 378 | PyObject *PyObject_Type Py_PROTO((PyObject *o)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 379 | |
| 380 | /* |
| 381 | On success, returns a type object corresponding to the object |
| 382 | type of object o. On failure, returns NULL. This is |
| 383 | equivalent to the Python expression: type(o). |
| 384 | */ |
| 385 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 386 | int PyObject_Length Py_PROTO((PyObject *o)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 387 | |
| 388 | /* |
| 389 | Return the length of object o. If the object, o, provides |
| 390 | both sequence and mapping protocols, the sequence length is |
| 391 | returned. On error, -1 is returned. This is the equivalent |
| 392 | to the Python expression: len(o). |
| 393 | |
| 394 | */ |
| 395 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 396 | PyObject *PyObject_GetItem Py_PROTO((PyObject *o, PyObject *key)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 397 | |
| 398 | /* |
| 399 | Return element of o corresponding to the object, key, or NULL |
| 400 | on failure. This is the equivalent of the Python expression: |
| 401 | o[key]. |
| 402 | |
| 403 | */ |
| 404 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 405 | int PyObject_SetItem Py_PROTO((PyObject *o, PyObject *key, PyObject *v)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 406 | |
| 407 | /* |
| 408 | Map the object, key, to the value, v. Returns |
| 409 | -1 on failure. This is the equivalent of the Python |
| 410 | statement: o[key]=v. |
| 411 | */ |
| 412 | |
| 413 | |
| 414 | /* Number Protocol:*/ |
| 415 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 416 | int PyNumber_Check Py_PROTO((PyObject *o)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 417 | |
| 418 | /* |
| 419 | Returns 1 if the object, o, provides numeric protocols, and |
| 420 | false otherwise. |
| 421 | |
| 422 | This function always succeeds. |
| 423 | |
| 424 | */ |
| 425 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 426 | PyObject *PyNumber_Add Py_PROTO((PyObject *o1, PyObject *o2)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 427 | |
| 428 | /* |
| 429 | Returns the result of adding o1 and o2, or null on failure. |
| 430 | This is the equivalent of the Python expression: o1+o2. |
| 431 | |
| 432 | |
| 433 | */ |
| 434 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 435 | PyObject *PyNumber_Subtract Py_PROTO((PyObject *o1, PyObject *o2)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 436 | |
| 437 | /* |
| 438 | Returns the result of subtracting o2 from o1, or null on |
| 439 | failure. This is the equivalent of the Python expression: |
| 440 | o1-o2. |
| 441 | |
| 442 | */ |
| 443 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 444 | PyObject *PyNumber_Multiply Py_PROTO((PyObject *o1, PyObject *o2)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 445 | |
| 446 | /* |
| 447 | Returns the result of multiplying o1 and o2, or null on |
| 448 | failure. This is the equivalent of the Python expression: |
| 449 | o1*o2. |
| 450 | |
| 451 | |
| 452 | */ |
| 453 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 454 | PyObject *PyNumber_Divide Py_PROTO((PyObject *o1, PyObject *o2)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 455 | |
| 456 | /* |
| 457 | Returns the result of dividing o1 by o2, or null on failure. |
| 458 | This is the equivalent of the Python expression: o1/o2. |
| 459 | |
| 460 | |
| 461 | */ |
| 462 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 463 | PyObject *PyNumber_Remainder Py_PROTO((PyObject *o1, PyObject *o2)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 464 | |
| 465 | /* |
| 466 | Returns the remainder of dividing o1 by o2, or null on |
| 467 | failure. This is the equivalent of the Python expression: |
| 468 | o1%o2. |
| 469 | |
| 470 | |
| 471 | */ |
| 472 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 473 | PyObject *PyNumber_Divmod Py_PROTO((PyObject *o1, PyObject *o2)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 474 | |
| 475 | /* |
| 476 | See the built-in function divmod. Returns NULL on failure. |
| 477 | This is the equivalent of the Python expression: |
| 478 | divmod(o1,o2). |
| 479 | |
| 480 | |
| 481 | */ |
| 482 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 483 | PyObject *PyNumber_Power Py_PROTO((PyObject *o1, PyObject *o2, PyObject *o3)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 484 | |
| 485 | /* |
| 486 | See the built-in function pow. Returns NULL on failure. |
| 487 | This is the equivalent of the Python expression: |
| 488 | pow(o1,o2,o3), where o3 is optional. |
| 489 | |
| 490 | */ |
| 491 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 492 | PyObject *PyNumber_Negative Py_PROTO((PyObject *o)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 493 | |
| 494 | /* |
| 495 | Returns the negation of o on success, or null on failure. |
| 496 | This is the equivalent of the Python expression: -o. |
| 497 | |
| 498 | */ |
| 499 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 500 | PyObject *PyNumber_Positive Py_PROTO((PyObject *o)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 501 | |
| 502 | /* |
| 503 | Returns the (what?) of o on success, or NULL on failure. |
| 504 | This is the equivalent of the Python expression: +o. |
| 505 | |
| 506 | */ |
| 507 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 508 | PyObject *PyNumber_Absolute Py_PROTO((PyObject *o)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 509 | |
| 510 | /* |
| 511 | Returns the absolute value of o, or null on failure. This is |
| 512 | the equivalent of the Python expression: abs(o). |
| 513 | |
| 514 | */ |
| 515 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 516 | PyObject *PyNumber_Invert Py_PROTO((PyObject *o)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 517 | |
| 518 | /* |
| 519 | Returns the bitwise negation of o on success, or NULL on |
| 520 | failure. This is the equivalent of the Python expression: |
| 521 | ~o. |
| 522 | |
| 523 | |
| 524 | */ |
| 525 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 526 | PyObject *PyNumber_Lshift Py_PROTO((PyObject *o1, PyObject *o2)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 527 | |
| 528 | /* |
| 529 | Returns the result of left shifting o1 by o2 on success, or |
| 530 | NULL on failure. This is the equivalent of the Python |
| 531 | expression: o1 << o2. |
| 532 | |
| 533 | |
| 534 | */ |
| 535 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 536 | PyObject *PyNumber_Rshift Py_PROTO((PyObject *o1, PyObject *o2)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 537 | |
| 538 | /* |
| 539 | Returns the result of right shifting o1 by o2 on success, or |
| 540 | NULL on failure. This is the equivalent of the Python |
| 541 | expression: o1 >> o2. |
| 542 | |
| 543 | */ |
| 544 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 545 | PyObject *PyNumber_And Py_PROTO((PyObject *o1, PyObject *o2)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 546 | |
| 547 | /* |
| 548 | Returns the result of "anding" o2 and o2 on success and NULL |
| 549 | on failure. This is the equivalent of the Python |
| 550 | expression: o1 and o2. |
| 551 | |
| 552 | |
| 553 | */ |
| 554 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 555 | PyObject *PyNumber_Xor Py_PROTO((PyObject *o1, PyObject *o2)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 556 | |
| 557 | /* |
| 558 | Returns the bitwise exclusive or of o1 by o2 on success, or |
| 559 | NULL on failure. This is the equivalent of the Python |
| 560 | expression: o1^o2. |
| 561 | |
| 562 | |
| 563 | */ |
| 564 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 565 | PyObject *PyNumber_Or Py_PROTO((PyObject *o1, PyObject *o2)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 566 | |
| 567 | /* |
| 568 | Returns the result or o1 and o2 on success, or NULL on |
| 569 | failure. This is the equivalent of the Python expression: |
| 570 | o1 or o2. |
| 571 | |
| 572 | */ |
| 573 | |
| 574 | /* Implemented elsewhere: |
| 575 | |
| 576 | int PyNumber_Coerce(PyObject *o1, PyObject *o2); |
| 577 | |
| 578 | On success, returns a tuple containing o1 and o2 converted to |
| 579 | a common numeric type, or None if no conversion is possible. |
| 580 | Returns -1 on failure. This is equivalent to the Python |
| 581 | expression: coerce(o1,o2). |
| 582 | |
| 583 | */ |
| 584 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 585 | PyObject *PyNumber_Int Py_PROTO((PyObject *o)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 586 | |
| 587 | /* |
| 588 | Returns the o converted to an integer object on success, or |
| 589 | NULL on failure. This is the equivalent of the Python |
| 590 | expression: int(o). |
| 591 | |
| 592 | */ |
| 593 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 594 | PyObject *PyNumber_Long Py_PROTO((PyObject *o)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 595 | |
| 596 | /* |
| 597 | Returns the o converted to a long integer object on success, |
| 598 | or NULL on failure. This is the equivalent of the Python |
| 599 | expression: long(o). |
| 600 | |
| 601 | */ |
| 602 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 603 | PyObject *PyNumber_Float Py_PROTO((PyObject *o)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 604 | |
| 605 | /* |
| 606 | Returns the o converted to a float object on success, or NULL |
| 607 | on failure. This is the equivalent of the Python expression: |
| 608 | float(o). |
| 609 | */ |
| 610 | |
| 611 | |
| 612 | /* Sequence protocol:*/ |
| 613 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 614 | int PySequence_Check Py_PROTO((PyObject *o)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 615 | |
| 616 | /* |
| 617 | Return 1 if the object provides sequence protocol, and zero |
| 618 | otherwise. |
| 619 | |
| 620 | This function always succeeds. |
| 621 | |
| 622 | */ |
| 623 | |
Guido van Rossum | 4f4ce68 | 1996-07-21 02:22:56 +0000 | [diff] [blame] | 624 | int PySequence_Lenth Py_PROTO((PyObject *o)); |
| 625 | |
| 626 | /* |
| 627 | Return the length of sequence object o, or -1 on failure. |
| 628 | |
| 629 | */ |
| 630 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 631 | PyObject *PySequence_Concat Py_PROTO((PyObject *o1, PyObject *o2)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 632 | |
| 633 | /* |
| 634 | Return the concatination of o1 and o2 on success, and NULL on |
| 635 | failure. This is the equivalent of the Python |
| 636 | expression: o1+o2. |
| 637 | |
| 638 | */ |
| 639 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 640 | PyObject *PySequence_Repeat Py_PROTO((PyObject *o, int count)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 641 | |
| 642 | /* |
| 643 | Return the result of repeating sequence object o count times, |
| 644 | or NULL on failure. This is the equivalent of the Python |
| 645 | expression: o1*count. |
| 646 | |
| 647 | */ |
| 648 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 649 | PyObject *PySequence_GetItem Py_PROTO((PyObject *o, int i)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 650 | |
| 651 | /* |
| 652 | Return the ith element of o, or NULL on failure. This is the |
| 653 | equivalent of the Python expression: o[i]. |
| 654 | |
| 655 | */ |
| 656 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 657 | PyObject *PySequence_GetSlice Py_PROTO((PyObject *o, int i1, int i2)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 658 | |
| 659 | /* |
| 660 | Return the slice of sequence object o between i1 and i2, or |
| 661 | NULL on failure. This is the equivalent of the Python |
| 662 | expression: o[i1:i2]. |
| 663 | |
| 664 | */ |
| 665 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 666 | int PySequence_SetItem Py_PROTO((PyObject *o, int i, PyObject *v)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 667 | |
| 668 | /* |
| 669 | Assign object v to the ith element of o. Returns |
| 670 | -1 on failure. This is the equivalent of the Python |
| 671 | statement: o[i]=v. |
| 672 | |
| 673 | */ |
| 674 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 675 | int PySequence_SetSlice Py_PROTO((PyObject *o, int i1, int i2, PyObject *v)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 676 | |
| 677 | /* |
| 678 | Assign the sequence object, v, to the slice in sequence |
| 679 | object, o, from i1 to i2. Returns -1 on failure. This is the |
| 680 | equivalent of the Python statement: o[i1:i2]=v. |
| 681 | */ |
| 682 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 683 | PyObject *PySequence_Tuple Py_PROTO((PyObject *o)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 684 | |
| 685 | /* |
| 686 | Returns the o as a tuple on success, and NULL on failure. |
| 687 | This is equivalent to the Python expression: tuple(o) |
| 688 | */ |
| 689 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 690 | int PySequence_Count Py_PROTO((PyObject *o, PyObject *value)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 691 | |
| 692 | /* |
| 693 | Return the number of occurrences on value on o, that is, |
| 694 | return the number of keys for which o[key]==value. On |
| 695 | failure, return -1. This is equivalent to the Python |
| 696 | expression: o.count(value). |
| 697 | */ |
| 698 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 699 | int PySequence_In Py_PROTO((PyObject *o, PyObject *value)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 700 | |
| 701 | /* |
| 702 | Determine if o contains value. If an item in o is equal to |
| 703 | X, return 1, otherwise return 0. On error, return -1. This |
| 704 | is equivalent to the Python expression: value in o. |
| 705 | */ |
| 706 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 707 | int PySequence_Index Py_PROTO((PyObject *o, PyObject *value)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 708 | |
| 709 | /* |
| 710 | Return the first index for which o[i]=value. On error, |
| 711 | return -1. This is equivalent to the Python |
| 712 | expression: o.index(value). |
| 713 | */ |
| 714 | |
| 715 | /* Mapping protocol:*/ |
| 716 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 717 | int PyMapping_Check Py_PROTO((PyObject *o)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 718 | |
| 719 | /* |
| 720 | Return 1 if the object provides mapping protocol, and zero |
| 721 | otherwise. |
| 722 | |
| 723 | This function always succeeds. |
| 724 | */ |
| 725 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 726 | int PyMapping_Length Py_PROTO((PyObject *o)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 727 | |
| 728 | /* |
| 729 | Returns the number of keys in object o on success, and -1 on |
| 730 | failure. For objects that do not provide sequence protocol, |
| 731 | this is equivalent to the Python expression: len(o). |
| 732 | */ |
| 733 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 734 | int PyMapping_DelItemString Py_PROTO((PyObject *o, char *key)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 735 | |
| 736 | /* |
| 737 | Remove the mapping for object, key, from the object *o. |
| 738 | Returns -1 on failure. This is equivalent to |
| 739 | the Python statement: del o[key]. |
| 740 | */ |
| 741 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 742 | int PyMapping_DelItem Py_PROTO((PyObject *o, PyObject *key)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 743 | |
| 744 | /* |
| 745 | Remove the mapping for object, key, from the object *o. |
| 746 | Returns -1 on failure. This is equivalent to |
| 747 | the Python statement: del o[key]. |
| 748 | */ |
| 749 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 750 | int PyMapping_HasKeyString Py_PROTO((PyObject *o, char *key)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 751 | |
| 752 | /* |
| 753 | On success, return 1 if the mapping object has the key, key, |
| 754 | and 0 otherwise. This is equivalent to the Python expression: |
| 755 | o.has_key(key). |
| 756 | |
| 757 | This function always succeeds. |
| 758 | */ |
| 759 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 760 | int PyMapping_HasKey Py_PROTO((PyObject *o, PyObject *key)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 761 | |
| 762 | /* |
| 763 | Return 1 if the mapping object has the key, key, |
| 764 | and 0 otherwise. This is equivalent to the Python expression: |
| 765 | o.has_key(key). |
| 766 | |
| 767 | This function always succeeds. |
| 768 | |
| 769 | */ |
| 770 | |
| 771 | /* Implemented as macro: |
| 772 | |
| 773 | PyObject *PyMapping_Keys(PyObject *o); |
| 774 | |
| 775 | On success, return a list of the keys in object o. On |
| 776 | failure, return NULL. This is equivalent to the Python |
| 777 | expression: o.keys(). |
| 778 | */ |
| 779 | #define PyMapping_Keys(O) PyObject_CallMethod(O,"keys",NULL) |
| 780 | |
| 781 | /* Implemented as macro: |
| 782 | |
| 783 | PyObject *PyMapping_Values(PyObject *o); |
| 784 | |
| 785 | On success, return a list of the values in object o. On |
| 786 | failure, return NULL. This is equivalent to the Python |
| 787 | expression: o.values(). |
| 788 | */ |
| 789 | #define PyMapping_Values(O) PyObject_CallMethod(O,"values",NULL) |
| 790 | |
| 791 | /* Implemented as macro: |
| 792 | |
| 793 | PyObject *PyMapping_Items(PyObject *o); |
| 794 | |
| 795 | On success, return a list of the items in object o, where |
| 796 | each item is a tuple containing a key-value pair. On |
| 797 | failure, return NULL. This is equivalent to the Python |
| 798 | expression: o.items(). |
| 799 | |
| 800 | */ |
| 801 | #define PyMapping_Items(O) PyObject_CallMethod(O,"items",NULL) |
| 802 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 803 | PyObject *PyMapping_GetItemString Py_PROTO((PyObject *o, char *key)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 804 | |
| 805 | /* |
| 806 | Return element of o corresponding to the object, key, or NULL |
| 807 | on failure. This is the equivalent of the Python expression: |
| 808 | o[key]. |
| 809 | */ |
| 810 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 811 | int PyMapping_SetItemString Py_PROTO((PyObject *o, char *key, |
| 812 | PyObject *value)); |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 813 | |
| 814 | /* |
| 815 | Map the object, key, to the value, v. Returns |
| 816 | -1 on failure. This is the equivalent of the Python |
| 817 | statement: o[key]=v. |
| 818 | */ |
| 819 | |
| 820 | |
Guido van Rossum | 8ca687a | 1995-09-18 21:20:02 +0000 | [diff] [blame] | 821 | #ifdef __cplusplus |
| 822 | } |
| 823 | #endif |
Guido van Rossum | a827537 | 1995-07-18 14:07:00 +0000 | [diff] [blame] | 824 | #endif /* Py_ABSTRACTOBJECT_H */ |