1 /* Abstract Object Interface (many thanks to Jim Fulton) */ 2 3 #ifndef Py_ABSTRACTOBJECT_H 4 #define Py_ABSTRACTOBJECT_H 5 #ifdef __cplusplus 6 extern "C" { 7 #endif 8 9 /* === Object Protocol ================================================== */ 10 11 /* Implemented elsewhere: 12 13 int PyObject_Print(PyObject *o, FILE *fp, int flags); 14 15 Print an object 'o' on file 'fp'. Returns -1 on error. The flags argument 16 is used to enable certain printing options. The only option currently 17 supported is Py_PRINT_RAW. By default (flags=0), PyObject_Print() formats 18 the object by calling PyObject_Repr(). If flags equals to Py_PRINT_RAW, it 19 formats the object by calling PyObject_Str(). */ 20 21 22 /* Implemented elsewhere: 23 24 int PyObject_HasAttrString(PyObject *o, const char *attr_name); 25 26 Returns 1 if object 'o' has the attribute attr_name, and 0 otherwise. 27 28 This is equivalent to the Python expression: hasattr(o,attr_name). 29 30 This function always succeeds. */ 31 32 33 /* Implemented elsewhere: 34 35 PyObject* PyObject_GetAttrString(PyObject *o, const char *attr_name); 36 37 Retrieve an attributed named attr_name form object o. 38 Returns the attribute value on success, or NULL on failure. 39 40 This is the equivalent of the Python expression: o.attr_name. */ 41 42 43 /* Implemented elsewhere: 44 45 int PyObject_HasAttr(PyObject *o, PyObject *attr_name); 46 47 Returns 1 if o has the attribute attr_name, and 0 otherwise. 48 49 This is equivalent to the Python expression: hasattr(o,attr_name). 50 51 This function always succeeds. */ 52 53 54 /* Implemented elsewhere: 55 56 int PyObject_HasAttrStringWithError(PyObject *o, const char *attr_name); 57 58 Returns 1 if object 'o' has the attribute attr_name, and 0 otherwise. 59 This is equivalent to the Python expression: hasattr(o,attr_name). 60 Returns -1 on failure. */ 61 62 63 /* Implemented elsewhere: 64 65 int PyObject_HasAttrWithError(PyObject *o, PyObject *attr_name); 66 67 Returns 1 if o has the attribute attr_name, and 0 otherwise. 68 This is equivalent to the Python expression: hasattr(o,attr_name). 69 Returns -1 on failure. */ 70 71 72 /* Implemented elsewhere: 73 74 PyObject* PyObject_GetAttr(PyObject *o, PyObject *attr_name); 75 76 Retrieve an attributed named 'attr_name' form object 'o'. 77 Returns the attribute value on success, or NULL on failure. 78 79 This is the equivalent of the Python expression: o.attr_name. */ 80 81 82 /* Implemented elsewhere: 83 84 int PyObject_GetOptionalAttr(PyObject *obj, PyObject *attr_name, PyObject **result); 85 86 Variant of PyObject_GetAttr() which doesn't raise AttributeError 87 if the attribute is not found. 88 89 If the attribute is found, return 1 and set *result to a new strong 90 reference to the attribute. 91 If the attribute is not found, return 0 and set *result to NULL; 92 the AttributeError is silenced. 93 If an error other than AttributeError is raised, return -1 and 94 set *result to NULL. 95 */ 96 97 98 /* Implemented elsewhere: 99 100 int PyObject_GetOptionalAttrString(PyObject *obj, const char *attr_name, PyObject **result); 101 102 Variant of PyObject_GetAttrString() which doesn't raise AttributeError 103 if the attribute is not found. 104 105 If the attribute is found, return 1 and set *result to a new strong 106 reference to the attribute. 107 If the attribute is not found, return 0 and set *result to NULL; 108 the AttributeError is silenced. 109 If an error other than AttributeError is raised, return -1 and 110 set *result to NULL. 111 */ 112 113 114 /* Implemented elsewhere: 115 116 int PyObject_SetAttrString(PyObject *o, const char *attr_name, PyObject *v); 117 118 Set the value of the attribute named attr_name, for object 'o', 119 to the value 'v'. Raise an exception and return -1 on failure; return 0 on 120 success. 121 122 This is the equivalent of the Python statement o.attr_name=v. */ 123 124 125 /* Implemented elsewhere: 126 127 int PyObject_SetAttr(PyObject *o, PyObject *attr_name, PyObject *v); 128 129 Set the value of the attribute named attr_name, for object 'o', to the value 130 'v'. an exception and return -1 on failure; return 0 on success. 131 132 This is the equivalent of the Python statement o.attr_name=v. */ 133 134 /* Implemented elsewhere: 135 136 int PyObject_DelAttrString(PyObject *o, const char *attr_name); 137 138 Delete attribute named attr_name, for object o. Returns 139 -1 on failure. 140 141 This is the equivalent of the Python statement: del o.attr_name. */ 142 143 144 /* Implemented elsewhere: 145 146 int PyObject_DelAttr(PyObject *o, PyObject *attr_name); 147 148 Delete attribute named attr_name, for object o. Returns -1 149 on failure. This is the equivalent of the Python 150 statement: del o.attr_name. */ 151 152 153 /* Implemented elsewhere: 154 155 PyObject *PyObject_Repr(PyObject *o); 156 157 Compute the string representation of object 'o'. Returns the 158 string representation on success, NULL on failure. 159 160 This is the equivalent of the Python expression: repr(o). 161 162 Called by the repr() built-in function. */ 163 164 165 /* Implemented elsewhere: 166 167 PyObject *PyObject_Str(PyObject *o); 168 169 Compute the string representation of object, o. Returns the 170 string representation on success, NULL on failure. 171 172 This is the equivalent of the Python expression: str(o). 173 174 Called by the str() and print() built-in functions. */ 175 176 177 /* Declared elsewhere 178 179 PyAPI_FUNC(int) PyCallable_Check(PyObject *o); 180 181 Determine if the object, o, is callable. Return 1 if the object is callable 182 and 0 otherwise. 183 184 This function always succeeds. */ 185 186 187 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03090000 188 /* Call a callable Python object without any arguments */ 189 PyAPI_FUNC(PyObject *) PyObject_CallNoArgs(PyObject *func); 190 #endif 191 192 193 /* Call a callable Python object 'callable' with arguments given by the 194 tuple 'args' and keywords arguments given by the dictionary 'kwargs'. 195 196 'args' must not be NULL, use an empty tuple if no arguments are 197 needed. If no named arguments are needed, 'kwargs' can be NULL. 198 199 This is the equivalent of the Python expression: 200 callable(*args, **kwargs). */ 201 PyAPI_FUNC(PyObject *) PyObject_Call(PyObject *callable, 202 PyObject *args, PyObject *kwargs); 203 204 205 /* Call a callable Python object 'callable', with arguments given by the 206 tuple 'args'. If no arguments are needed, then 'args' can be NULL. 207 208 Returns the result of the call on success, or NULL on failure. 209 210 This is the equivalent of the Python expression: 211 callable(*args). */ 212 PyAPI_FUNC(PyObject *) PyObject_CallObject(PyObject *callable, 213 PyObject *args); 214 215 /* Call a callable Python object, callable, with a variable number of C 216 arguments. The C arguments are described using a mkvalue-style format 217 string. 218 219 The format may be NULL, indicating that no arguments are provided. 220 221 Returns the result of the call on success, or NULL on failure. 222 223 This is the equivalent of the Python expression: 224 callable(arg1, arg2, ...). */ 225 PyAPI_FUNC(PyObject *) PyObject_CallFunction(PyObject *callable, 226 const char *format, ...); 227 228 /* Call the method named 'name' of object 'obj' with a variable number of 229 C arguments. The C arguments are described by a mkvalue format string. 230 231 The format can be NULL, indicating that no arguments are provided. 232 233 Returns the result of the call on success, or NULL on failure. 234 235 This is the equivalent of the Python expression: 236 obj.name(arg1, arg2, ...). */ 237 PyAPI_FUNC(PyObject *) PyObject_CallMethod(PyObject *obj, 238 const char *name, 239 const char *format, ...); 240 241 /* Call a callable Python object 'callable' with a variable number of C 242 arguments. The C arguments are provided as PyObject* values, terminated 243 by a NULL. 244 245 Returns the result of the call on success, or NULL on failure. 246 247 This is the equivalent of the Python expression: 248 callable(arg1, arg2, ...). */ 249 PyAPI_FUNC(PyObject *) PyObject_CallFunctionObjArgs(PyObject *callable, 250 ...); 251 252 /* Call the method named 'name' of object 'obj' with a variable number of 253 C arguments. The C arguments are provided as PyObject* values, terminated 254 by NULL. 255 256 Returns the result of the call on success, or NULL on failure. 257 258 This is the equivalent of the Python expression: obj.name(*args). */ 259 260 PyAPI_FUNC(PyObject *) PyObject_CallMethodObjArgs( 261 PyObject *obj, 262 PyObject *name, 263 ...); 264 265 /* Given a vectorcall nargsf argument, return the actual number of arguments. 266 * (For use outside the limited API, this is re-defined as a static inline 267 * function in cpython/abstract.h) 268 */ 269 PyAPI_FUNC(Py_ssize_t) PyVectorcall_NARGS(size_t nargsf); 270 271 /* Call "callable" (which must support vectorcall) with positional arguments 272 "tuple" and keyword arguments "dict". "dict" may also be NULL */ 273 PyAPI_FUNC(PyObject *) PyVectorcall_Call(PyObject *callable, PyObject *tuple, PyObject *dict); 274 275 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030C0000 276 #define PY_VECTORCALL_ARGUMENTS_OFFSET \ 277 (_Py_STATIC_CAST(size_t, 1) << (8 * sizeof(size_t) - 1)) 278 279 /* Perform a PEP 590-style vector call on 'callable' */ 280 PyAPI_FUNC(PyObject *) PyObject_Vectorcall( 281 PyObject *callable, 282 PyObject *const *args, 283 size_t nargsf, 284 PyObject *kwnames); 285 286 /* Call the method 'name' on args[0] with arguments in args[1..nargsf-1]. */ 287 PyAPI_FUNC(PyObject *) PyObject_VectorcallMethod( 288 PyObject *name, PyObject *const *args, 289 size_t nargsf, PyObject *kwnames); 290 #endif 291 292 /* Implemented elsewhere: 293 294 Py_hash_t PyObject_Hash(PyObject *o); 295 296 Compute and return the hash, hash_value, of an object, o. On 297 failure, return -1. 298 299 This is the equivalent of the Python expression: hash(o). */ 300 301 302 /* Implemented elsewhere: 303 304 int PyObject_IsTrue(PyObject *o); 305 306 Returns 1 if the object, o, is considered to be true, 0 if o is 307 considered to be false and -1 on failure. 308 309 This is equivalent to the Python expression: not not o. */ 310 311 312 /* Implemented elsewhere: 313 314 int PyObject_Not(PyObject *o); 315 316 Returns 0 if the object, o, is considered to be true, 1 if o is 317 considered to be false and -1 on failure. 318 319 This is equivalent to the Python expression: not o. */ 320 321 322 /* Get the type of an object. 323 324 On success, returns a type object corresponding to the object type of object 325 'o'. On failure, returns NULL. 326 327 This is equivalent to the Python expression: type(o) */ 328 PyAPI_FUNC(PyObject *) PyObject_Type(PyObject *o); 329 330 331 /* Return the size of object 'o'. If the object 'o' provides both sequence and 332 mapping protocols, the sequence size is returned. 333 334 On error, -1 is returned. 335 336 This is the equivalent to the Python expression: len(o) */ 337 PyAPI_FUNC(Py_ssize_t) PyObject_Size(PyObject *o); 338 339 340 /* For DLL compatibility */ 341 #undef PyObject_Length 342 PyAPI_FUNC(Py_ssize_t) PyObject_Length(PyObject *o); 343 #define PyObject_Length PyObject_Size 344 345 /* Return element of 'o' corresponding to the object 'key'. Return NULL 346 on failure. 347 348 This is the equivalent of the Python expression: o[key] */ 349 PyAPI_FUNC(PyObject *) PyObject_GetItem(PyObject *o, PyObject *key); 350 351 352 /* Map the object 'key' to the value 'v' into 'o'. 353 354 Raise an exception and return -1 on failure; return 0 on success. 355 356 This is the equivalent of the Python statement: o[key]=v. */ 357 PyAPI_FUNC(int) PyObject_SetItem(PyObject *o, PyObject *key, PyObject *v); 358 359 /* Remove the mapping for the string 'key' from the object 'o'. 360 Returns -1 on failure. 361 362 This is equivalent to the Python statement: del o[key]. */ 363 PyAPI_FUNC(int) PyObject_DelItemString(PyObject *o, const char *key); 364 365 /* Delete the mapping for the object 'key' from the object 'o'. 366 Returns -1 on failure. 367 368 This is the equivalent of the Python statement: del o[key]. */ 369 PyAPI_FUNC(int) PyObject_DelItem(PyObject *o, PyObject *key); 370 371 372 /* Takes an arbitrary object and returns the result of calling 373 obj.__format__(format_spec). */ 374 PyAPI_FUNC(PyObject *) PyObject_Format(PyObject *obj, 375 PyObject *format_spec); 376 377 378 /* ==== Iterators ================================================ */ 379 380 /* Takes an object and returns an iterator for it. 381 This is typically a new iterator but if the argument is an iterator, this 382 returns itself. */ 383 PyAPI_FUNC(PyObject *) PyObject_GetIter(PyObject *); 384 385 /* Takes an AsyncIterable object and returns an AsyncIterator for it. 386 This is typically a new iterator but if the argument is an AsyncIterator, 387 this returns itself. */ 388 PyAPI_FUNC(PyObject *) PyObject_GetAIter(PyObject *); 389 390 /* Returns non-zero if the object 'obj' provides iterator protocols, and 0 otherwise. 391 392 This function always succeeds. */ 393 PyAPI_FUNC(int) PyIter_Check(PyObject *); 394 395 /* Returns non-zero if the object 'obj' provides AsyncIterator protocols, and 0 otherwise. 396 397 This function always succeeds. */ 398 PyAPI_FUNC(int) PyAIter_Check(PyObject *); 399 400 /* Takes an iterator object and calls its tp_iternext slot, 401 returning the next value. 402 403 If the iterator is exhausted, this returns NULL without setting an 404 exception. 405 406 NULL with an exception means an error occurred. */ 407 PyAPI_FUNC(PyObject *) PyIter_Next(PyObject *); 408 409 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030A0000 410 411 /* Takes generator, coroutine or iterator object and sends the value into it. 412 Returns: 413 - PYGEN_RETURN (0) if generator has returned. 414 'result' parameter is filled with return value 415 - PYGEN_ERROR (-1) if exception was raised. 416 'result' parameter is NULL 417 - PYGEN_NEXT (1) if generator has yielded. 418 'result' parameter is filled with yielded value. */ 419 PyAPI_FUNC(PySendResult) PyIter_Send(PyObject *, PyObject *, PyObject **); 420 #endif 421 422 423 /* === Number Protocol ================================================== */ 424 425 /* Returns 1 if the object 'o' provides numeric protocols, and 0 otherwise. 426 427 This function always succeeds. */ 428 PyAPI_FUNC(int) PyNumber_Check(PyObject *o); 429 430 /* Returns the result of adding o1 and o2, or NULL on failure. 431 432 This is the equivalent of the Python expression: o1 + o2. */ 433 PyAPI_FUNC(PyObject *) PyNumber_Add(PyObject *o1, PyObject *o2); 434 435 /* Returns the result of subtracting o2 from o1, or NULL on failure. 436 437 This is the equivalent of the Python expression: o1 - o2. */ 438 PyAPI_FUNC(PyObject *) PyNumber_Subtract(PyObject *o1, PyObject *o2); 439 440 /* Returns the result of multiplying o1 and o2, or NULL on failure. 441 442 This is the equivalent of the Python expression: o1 * o2. */ 443 PyAPI_FUNC(PyObject *) PyNumber_Multiply(PyObject *o1, PyObject *o2); 444 445 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000 446 /* This is the equivalent of the Python expression: o1 @ o2. */ 447 PyAPI_FUNC(PyObject *) PyNumber_MatrixMultiply(PyObject *o1, PyObject *o2); 448 #endif 449 450 /* Returns the result of dividing o1 by o2 giving an integral result, 451 or NULL on failure. 452 453 This is the equivalent of the Python expression: o1 // o2. */ 454 PyAPI_FUNC(PyObject *) PyNumber_FloorDivide(PyObject *o1, PyObject *o2); 455 456 /* Returns the result of dividing o1 by o2 giving a float result, or NULL on 457 failure. 458 459 This is the equivalent of the Python expression: o1 / o2. */ 460 PyAPI_FUNC(PyObject *) PyNumber_TrueDivide(PyObject *o1, PyObject *o2); 461 462 /* Returns the remainder of dividing o1 by o2, or NULL on failure. 463 464 This is the equivalent of the Python expression: o1 % o2. */ 465 PyAPI_FUNC(PyObject *) PyNumber_Remainder(PyObject *o1, PyObject *o2); 466 467 /* See the built-in function divmod. 468 469 Returns NULL on failure. 470 471 This is the equivalent of the Python expression: divmod(o1, o2). */ 472 PyAPI_FUNC(PyObject *) PyNumber_Divmod(PyObject *o1, PyObject *o2); 473 474 /* See the built-in function pow. Returns NULL on failure. 475 476 This is the equivalent of the Python expression: pow(o1, o2, o3), 477 where o3 is optional. */ 478 PyAPI_FUNC(PyObject *) PyNumber_Power(PyObject *o1, PyObject *o2, 479 PyObject *o3); 480 481 /* Returns the negation of o on success, or NULL on failure. 482 483 This is the equivalent of the Python expression: -o. */ 484 PyAPI_FUNC(PyObject *) PyNumber_Negative(PyObject *o); 485 486 /* Returns the positive of o on success, or NULL on failure. 487 488 This is the equivalent of the Python expression: +o. */ 489 PyAPI_FUNC(PyObject *) PyNumber_Positive(PyObject *o); 490 491 /* Returns the absolute value of 'o', or NULL on failure. 492 493 This is the equivalent of the Python expression: abs(o). */ 494 PyAPI_FUNC(PyObject *) PyNumber_Absolute(PyObject *o); 495 496 /* Returns the bitwise negation of 'o' on success, or NULL on failure. 497 498 This is the equivalent of the Python expression: ~o. */ 499 PyAPI_FUNC(PyObject *) PyNumber_Invert(PyObject *o); 500 501 /* Returns the result of left shifting o1 by o2 on success, or NULL on failure. 502 503 This is the equivalent of the Python expression: o1 << o2. */ 504 PyAPI_FUNC(PyObject *) PyNumber_Lshift(PyObject *o1, PyObject *o2); 505 506 /* Returns the result of right shifting o1 by o2 on success, or NULL on 507 failure. 508 509 This is the equivalent of the Python expression: o1 >> o2. */ 510 PyAPI_FUNC(PyObject *) PyNumber_Rshift(PyObject *o1, PyObject *o2); 511 512 /* Returns the result of bitwise and of o1 and o2 on success, or NULL on 513 failure. 514 515 This is the equivalent of the Python expression: o1 & o2. */ 516 PyAPI_FUNC(PyObject *) PyNumber_And(PyObject *o1, PyObject *o2); 517 518 /* Returns the bitwise exclusive or of o1 by o2 on success, or NULL on failure. 519 520 This is the equivalent of the Python expression: o1 ^ o2. */ 521 PyAPI_FUNC(PyObject *) PyNumber_Xor(PyObject *o1, PyObject *o2); 522 523 /* Returns the result of bitwise or on o1 and o2 on success, or NULL on 524 failure. 525 526 This is the equivalent of the Python expression: o1 | o2. */ 527 PyAPI_FUNC(PyObject *) PyNumber_Or(PyObject *o1, PyObject *o2); 528 529 /* Returns 1 if obj is an index integer (has the nb_index slot of the 530 tp_as_number structure filled in), and 0 otherwise. */ 531 PyAPI_FUNC(int) PyIndex_Check(PyObject *); 532 533 /* Returns the object 'o' converted to a Python int, or NULL with an exception 534 raised on failure. */ 535 PyAPI_FUNC(PyObject *) PyNumber_Index(PyObject *o); 536 537 /* Returns the object 'o' converted to Py_ssize_t by going through 538 PyNumber_Index() first. 539 540 If an overflow error occurs while converting the int to Py_ssize_t, then the 541 second argument 'exc' is the error-type to return. If it is NULL, then the 542 overflow error is cleared and the value is clipped. */ 543 PyAPI_FUNC(Py_ssize_t) PyNumber_AsSsize_t(PyObject *o, PyObject *exc); 544 545 /* Returns the object 'o' converted to an integer object on success, or NULL 546 on failure. 547 548 This is the equivalent of the Python expression: int(o). */ 549 PyAPI_FUNC(PyObject *) PyNumber_Long(PyObject *o); 550 551 /* Returns the object 'o' converted to a float object on success, or NULL 552 on failure. 553 554 This is the equivalent of the Python expression: float(o). */ 555 PyAPI_FUNC(PyObject *) PyNumber_Float(PyObject *o); 556 557 558 /* --- In-place variants of (some of) the above number protocol functions -- */ 559 560 /* Returns the result of adding o2 to o1, possibly in-place, or NULL 561 on failure. 562 563 This is the equivalent of the Python expression: o1 += o2. */ 564 PyAPI_FUNC(PyObject *) PyNumber_InPlaceAdd(PyObject *o1, PyObject *o2); 565 566 /* Returns the result of subtracting o2 from o1, possibly in-place or 567 NULL on failure. 568 569 This is the equivalent of the Python expression: o1 -= o2. */ 570 PyAPI_FUNC(PyObject *) PyNumber_InPlaceSubtract(PyObject *o1, PyObject *o2); 571 572 /* Returns the result of multiplying o1 by o2, possibly in-place, or NULL on 573 failure. 574 575 This is the equivalent of the Python expression: o1 *= o2. */ 576 PyAPI_FUNC(PyObject *) PyNumber_InPlaceMultiply(PyObject *o1, PyObject *o2); 577 578 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000 579 /* This is the equivalent of the Python expression: o1 @= o2. */ 580 PyAPI_FUNC(PyObject *) PyNumber_InPlaceMatrixMultiply(PyObject *o1, PyObject *o2); 581 #endif 582 583 /* Returns the result of dividing o1 by o2 giving an integral result, possibly 584 in-place, or NULL on failure. 585 586 This is the equivalent of the Python expression: o1 /= o2. */ 587 PyAPI_FUNC(PyObject *) PyNumber_InPlaceFloorDivide(PyObject *o1, 588 PyObject *o2); 589 590 /* Returns the result of dividing o1 by o2 giving a float result, possibly 591 in-place, or null on failure. 592 593 This is the equivalent of the Python expression: o1 /= o2. */ 594 PyAPI_FUNC(PyObject *) PyNumber_InPlaceTrueDivide(PyObject *o1, 595 PyObject *o2); 596 597 /* Returns the remainder of dividing o1 by o2, possibly in-place, or NULL on 598 failure. 599 600 This is the equivalent of the Python expression: o1 %= o2. */ 601 PyAPI_FUNC(PyObject *) PyNumber_InPlaceRemainder(PyObject *o1, PyObject *o2); 602 603 /* Returns the result of raising o1 to the power of o2, possibly in-place, 604 or NULL on failure. 605 606 This is the equivalent of the Python expression: o1 **= o2, 607 or o1 = pow(o1, o2, o3) if o3 is present. */ 608 PyAPI_FUNC(PyObject *) PyNumber_InPlacePower(PyObject *o1, PyObject *o2, 609 PyObject *o3); 610 611 /* Returns the result of left shifting o1 by o2, possibly in-place, or NULL 612 on failure. 613 614 This is the equivalent of the Python expression: o1 <<= o2. */ 615 PyAPI_FUNC(PyObject *) PyNumber_InPlaceLshift(PyObject *o1, PyObject *o2); 616 617 /* Returns the result of right shifting o1 by o2, possibly in-place or NULL 618 on failure. 619 620 This is the equivalent of the Python expression: o1 >>= o2. */ 621 PyAPI_FUNC(PyObject *) PyNumber_InPlaceRshift(PyObject *o1, PyObject *o2); 622 623 /* Returns the result of bitwise and of o1 and o2, possibly in-place, or NULL 624 on failure. 625 626 This is the equivalent of the Python expression: o1 &= o2. */ 627 PyAPI_FUNC(PyObject *) PyNumber_InPlaceAnd(PyObject *o1, PyObject *o2); 628 629 /* Returns the bitwise exclusive or of o1 by o2, possibly in-place, or NULL 630 on failure. 631 632 This is the equivalent of the Python expression: o1 ^= o2. */ 633 PyAPI_FUNC(PyObject *) PyNumber_InPlaceXor(PyObject *o1, PyObject *o2); 634 635 /* Returns the result of bitwise or of o1 and o2, possibly in-place, 636 or NULL on failure. 637 638 This is the equivalent of the Python expression: o1 |= o2. */ 639 PyAPI_FUNC(PyObject *) PyNumber_InPlaceOr(PyObject *o1, PyObject *o2); 640 641 /* Returns the integer n converted to a string with a base, with a base 642 marker of 0b, 0o or 0x prefixed if applicable. 643 644 If n is not an int object, it is converted with PyNumber_Index first. */ 645 PyAPI_FUNC(PyObject *) PyNumber_ToBase(PyObject *n, int base); 646 647 648 /* === Sequence protocol ================================================ */ 649 650 /* Return 1 if the object provides sequence protocol, and zero 651 otherwise. 652 653 This function always succeeds. */ 654 PyAPI_FUNC(int) PySequence_Check(PyObject *o); 655 656 /* Return the size of sequence object o, or -1 on failure. */ 657 PyAPI_FUNC(Py_ssize_t) PySequence_Size(PyObject *o); 658 659 /* For DLL compatibility */ 660 #undef PySequence_Length 661 PyAPI_FUNC(Py_ssize_t) PySequence_Length(PyObject *o); 662 #define PySequence_Length PySequence_Size 663 664 665 /* Return the concatenation of o1 and o2 on success, and NULL on failure. 666 667 This is the equivalent of the Python expression: o1 + o2. */ 668 PyAPI_FUNC(PyObject *) PySequence_Concat(PyObject *o1, PyObject *o2); 669 670 /* Return the result of repeating sequence object 'o' 'count' times, 671 or NULL on failure. 672 673 This is the equivalent of the Python expression: o * count. */ 674 PyAPI_FUNC(PyObject *) PySequence_Repeat(PyObject *o, Py_ssize_t count); 675 676 /* Return the ith element of o, or NULL on failure. 677 678 This is the equivalent of the Python expression: o[i]. */ 679 PyAPI_FUNC(PyObject *) PySequence_GetItem(PyObject *o, Py_ssize_t i); 680 681 /* Return the slice of sequence object o between i1 and i2, or NULL on failure. 682 683 This is the equivalent of the Python expression: o[i1:i2]. */ 684 PyAPI_FUNC(PyObject *) PySequence_GetSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2); 685 686 /* Assign object 'v' to the ith element of the sequence 'o'. Raise an exception 687 and return -1 on failure; return 0 on success. 688 689 This is the equivalent of the Python statement o[i] = v. */ 690 PyAPI_FUNC(int) PySequence_SetItem(PyObject *o, Py_ssize_t i, PyObject *v); 691 692 /* Delete the 'i'-th element of the sequence 'v'. Returns -1 on failure. 693 694 This is the equivalent of the Python statement: del o[i]. */ 695 PyAPI_FUNC(int) PySequence_DelItem(PyObject *o, Py_ssize_t i); 696 697 /* Assign the sequence object 'v' to the slice in sequence object 'o', 698 from 'i1' to 'i2'. Returns -1 on failure. 699 700 This is the equivalent of the Python statement: o[i1:i2] = v. */ 701 PyAPI_FUNC(int) PySequence_SetSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2, 702 PyObject *v); 703 704 /* Delete the slice in sequence object 'o' from 'i1' to 'i2'. 705 Returns -1 on failure. 706 707 This is the equivalent of the Python statement: del o[i1:i2]. */ 708 PyAPI_FUNC(int) PySequence_DelSlice(PyObject *o, Py_ssize_t i1, Py_ssize_t i2); 709 710 /* Returns the sequence 'o' as a tuple on success, and NULL on failure. 711 712 This is equivalent to the Python expression: tuple(o). */ 713 PyAPI_FUNC(PyObject *) PySequence_Tuple(PyObject *o); 714 715 /* Returns the sequence 'o' as a list on success, and NULL on failure. 716 This is equivalent to the Python expression: list(o) */ 717 PyAPI_FUNC(PyObject *) PySequence_List(PyObject *o); 718 719 /* Return the sequence 'o' as a list, unless it's already a tuple or list. 720 721 Use PySequence_Fast_GET_ITEM to access the members of this list, and 722 PySequence_Fast_GET_SIZE to get its length. 723 724 Returns NULL on failure. If the object does not support iteration, raises a 725 TypeError exception with 'm' as the message text. */ 726 PyAPI_FUNC(PyObject *) PySequence_Fast(PyObject *o, const char* m); 727 728 /* Return the size of the sequence 'o', assuming that 'o' was returned by 729 PySequence_Fast and is not NULL. */ 730 #define PySequence_Fast_GET_SIZE(o) \ 731 (PyList_Check(o) ? PyList_GET_SIZE(o) : PyTuple_GET_SIZE(o)) 732 733 /* Return the 'i'-th element of the sequence 'o', assuming that o was returned 734 by PySequence_Fast, and that i is within bounds. */ 735 #define PySequence_Fast_GET_ITEM(o, i)\ 736 (PyList_Check(o) ? PyList_GET_ITEM((o), (i)) : PyTuple_GET_ITEM((o), (i))) 737 738 /* Return a pointer to the underlying item array for 739 an object returned by PySequence_Fast */ 740 #define PySequence_Fast_ITEMS(sf) \ 741 (PyList_Check(sf) ? ((PyListObject *)(sf))->ob_item \ 742 : ((PyTupleObject *)(sf))->ob_item) 743 744 /* Return the number of occurrences on value on 'o', that is, return 745 the number of keys for which o[key] == value. 746 747 On failure, return -1. This is equivalent to the Python expression: 748 o.count(value). */ 749 PyAPI_FUNC(Py_ssize_t) PySequence_Count(PyObject *o, PyObject *value); 750 751 /* Return 1 if 'ob' is in the sequence 'seq'; 0 if 'ob' is not in the sequence 752 'seq'; -1 on error. 753 754 Use __contains__ if possible, else _PySequence_IterSearch(). */ 755 PyAPI_FUNC(int) PySequence_Contains(PyObject *seq, PyObject *ob); 756 757 /* For DLL-level backwards compatibility */ 758 #undef PySequence_In 759 /* Determine if the sequence 'o' contains 'value'. If an item in 'o' is equal 760 to 'value', return 1, otherwise return 0. On error, return -1. 761 762 This is equivalent to the Python expression: value in o. */ 763 PyAPI_FUNC(int) PySequence_In(PyObject *o, PyObject *value); 764 765 /* For source-level backwards compatibility */ 766 #define PySequence_In PySequence_Contains 767 768 769 /* Return the first index for which o[i] == value. 770 On error, return -1. 771 772 This is equivalent to the Python expression: o.index(value). */ 773 PyAPI_FUNC(Py_ssize_t) PySequence_Index(PyObject *o, PyObject *value); 774 775 776 /* --- In-place versions of some of the above Sequence functions --- */ 777 778 /* Append sequence 'o2' to sequence 'o1', in-place when possible. Return the 779 resulting object, which could be 'o1', or NULL on failure. 780 781 This is the equivalent of the Python expression: o1 += o2. */ 782 PyAPI_FUNC(PyObject *) PySequence_InPlaceConcat(PyObject *o1, PyObject *o2); 783 784 /* Repeat sequence 'o' by 'count', in-place when possible. Return the resulting 785 object, which could be 'o', or NULL on failure. 786 787 This is the equivalent of the Python expression: o1 *= count. */ 788 PyAPI_FUNC(PyObject *) PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count); 789 790 791 /* === Mapping protocol ================================================= */ 792 793 /* Return 1 if the object provides mapping protocol, and 0 otherwise. 794 795 This function always succeeds. */ 796 PyAPI_FUNC(int) PyMapping_Check(PyObject *o); 797 798 /* Returns the number of keys in mapping object 'o' on success, and -1 on 799 failure. This is equivalent to the Python expression: len(o). */ 800 PyAPI_FUNC(Py_ssize_t) PyMapping_Size(PyObject *o); 801 802 /* For DLL compatibility */ 803 #undef PyMapping_Length 804 PyAPI_FUNC(Py_ssize_t) PyMapping_Length(PyObject *o); 805 #define PyMapping_Length PyMapping_Size 806 807 808 /* Implemented as a macro: 809 810 int PyMapping_DelItemString(PyObject *o, const char *key); 811 812 Remove the mapping for the string 'key' from the mapping 'o'. Returns -1 on 813 failure. 814 815 This is equivalent to the Python statement: del o[key]. */ 816 #define PyMapping_DelItemString(O, K) PyObject_DelItemString((O), (K)) 817 818 /* Implemented as a macro: 819 820 int PyMapping_DelItem(PyObject *o, PyObject *key); 821 822 Remove the mapping for the object 'key' from the mapping object 'o'. 823 Returns -1 on failure. 824 825 This is equivalent to the Python statement: del o[key]. */ 826 #define PyMapping_DelItem(O, K) PyObject_DelItem((O), (K)) 827 828 /* On success, return 1 if the mapping object 'o' has the key 'key', 829 and 0 otherwise. 830 831 This is equivalent to the Python expression: key in o. 832 833 This function always succeeds. */ 834 PyAPI_FUNC(int) PyMapping_HasKeyString(PyObject *o, const char *key); 835 836 /* Return 1 if the mapping object has the key 'key', and 0 otherwise. 837 838 This is equivalent to the Python expression: key in o. 839 840 This function always succeeds. */ 841 PyAPI_FUNC(int) PyMapping_HasKey(PyObject *o, PyObject *key); 842 843 /* Return 1 if the mapping object has the key 'key', and 0 otherwise. 844 This is equivalent to the Python expression: key in o. 845 On failure, return -1. */ 846 847 PyAPI_FUNC(int) PyMapping_HasKeyWithError(PyObject *o, PyObject *key); 848 849 /* Return 1 if the mapping object has the key 'key', and 0 otherwise. 850 This is equivalent to the Python expression: key in o. 851 On failure, return -1. */ 852 853 PyAPI_FUNC(int) PyMapping_HasKeyStringWithError(PyObject *o, const char *key); 854 855 /* On success, return a list or tuple of the keys in mapping object 'o'. 856 On failure, return NULL. */ 857 PyAPI_FUNC(PyObject *) PyMapping_Keys(PyObject *o); 858 859 /* On success, return a list or tuple of the values in mapping object 'o'. 860 On failure, return NULL. */ 861 PyAPI_FUNC(PyObject *) PyMapping_Values(PyObject *o); 862 863 /* On success, return a list or tuple of the items in mapping object 'o', 864 where each item is a tuple containing a key-value pair. On failure, return 865 NULL. */ 866 PyAPI_FUNC(PyObject *) PyMapping_Items(PyObject *o); 867 868 /* Return element of 'o' corresponding to the string 'key' or NULL on failure. 869 870 This is the equivalent of the Python expression: o[key]. */ 871 PyAPI_FUNC(PyObject *) PyMapping_GetItemString(PyObject *o, 872 const char *key); 873 874 /* Variants of PyObject_GetItem() and PyMapping_GetItemString() which don't 875 raise KeyError if the key is not found. 876 877 If the key is found, return 1 and set *result to a new strong 878 reference to the corresponding value. 879 If the key is not found, return 0 and set *result to NULL; 880 the KeyError is silenced. 881 If an error other than KeyError is raised, return -1 and 882 set *result to NULL. 883 */ 884 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030d0000 885 PyAPI_FUNC(int) PyMapping_GetOptionalItem(PyObject *, PyObject *, PyObject **); 886 PyAPI_FUNC(int) PyMapping_GetOptionalItemString(PyObject *, const char *, PyObject **); 887 #endif 888 889 /* Map the string 'key' to the value 'v' in the mapping 'o'. 890 Returns -1 on failure. 891 892 This is the equivalent of the Python statement: o[key]=v. */ 893 PyAPI_FUNC(int) PyMapping_SetItemString(PyObject *o, const char *key, 894 PyObject *value); 895 896 /* isinstance(object, typeorclass) */ 897 PyAPI_FUNC(int) PyObject_IsInstance(PyObject *object, PyObject *typeorclass); 898 899 /* issubclass(object, typeorclass) */ 900 PyAPI_FUNC(int) PyObject_IsSubclass(PyObject *object, PyObject *typeorclass); 901 902 #ifndef Py_LIMITED_API 903 # define Py_CPYTHON_ABSTRACTOBJECT_H 904 # include "cpython/abstract.h" 905 # undef Py_CPYTHON_ABSTRACTOBJECT_H 906 #endif 907 908 #ifdef __cplusplus 909 } 910 #endif 911 #endif /* Py_ABSTRACTOBJECT_H */ 912