1 /** 2 Mirror _object.h 3 4 Object and type object interface 5 6 Objects are structures allocated on the heap. Special rules apply to 7 the use of objects to ensure they are properly garbage-collected. 8 Objects are never allocated statically or on the stack; they must be 9 accessed through special macros and functions only. (Type objects are 10 exceptions to the first rule; the standard types are represented by 11 statically initialized type objects, although work on type/class unification 12 for Python 2.2 made it possible to have heap-allocated type objects too). 13 14 An object has a 'reference count' that is increased or decreased when a 15 pointer to the object is copied or deleted; when the reference count 16 reaches zero there are no references to the object left and it can be 17 removed from the heap. 18 19 An object has a 'type' that determines what it represents and what kind 20 of data it contains. An object's type is fixed when it is created. 21 Types themselves are represented as objects; an object contains a 22 pointer to the corresponding type object. The type itself has a type 23 pointer pointing to the object representing the type 'type', which 24 contains a pointer to itself!$(RPAREN). 25 26 Objects do not float around in memory; once allocated an object keeps 27 the same size and address. Objects that must hold variable-size data 28 can contain pointers to variable-size parts of the object. Not all 29 objects of the same type have the same size; but the size cannot change 30 after allocation. (These restrictions are made so a reference to an 31 object can be simply a pointer -- moving an object would require 32 updating all the pointers, and changing an object's size would require 33 moving it if there was another object right next to it.) 34 35 Objects are always accessed through pointers of the type 'PyObject *'. 36 The type 'PyObject' is a structure that only contains the reference count 37 and the type pointer. The actual memory allocated for an object 38 contains other data that can only be accessed after casting the pointer 39 to a pointer to a longer structure type. This longer type must start 40 with the reference count and type fields; the macro PyObject_HEAD should be 41 used for this (to accommodate for future changes). The implementation 42 of a particular object type can cast the object pointer to the proper 43 type and back. 44 45 A standard interface exists for objects that contain an array of items 46 whose size is determined when the object is allocated. 47 */ 48 module deimos.python.object; 49 50 import core.stdc.stdio; 51 import deimos.python.pyport; 52 import deimos.python.methodobject; 53 import deimos.python.structmember; 54 import deimos.python.descrobject; 55 56 extern(C): 57 // Python-header-file: Include/object.h: 58 59 // XXX:Conditionalize in if running debug build of Python interpreter: 60 /* 61 version (Python_Debug_Build) { 62 template _PyObject_HEAD_EXTRA() { 63 PyObject *_ob_next; 64 PyObject *_ob_prev; 65 } 66 } else { 67 */ 68 /// _ 69 template _PyObject_HEAD_EXTRA() {} 70 /*}*/ 71 72 version(Python_3_0_Or_Later) { 73 /// _ 74 mixin template PyObject_HEAD() { 75 /// _ 76 PyObject ob_base; 77 } 78 }else{ 79 /// _ 80 mixin template PyObject_HEAD() { 81 mixin _PyObject_HEAD_EXTRA; 82 /// _ 83 Py_ssize_t ob_refcnt; 84 /// _ 85 PyTypeObject* ob_type; 86 } 87 } 88 89 /** Nothing is actually declared to be a PyObject, but every pointer to 90 * a Python object can be cast to a PyObject*. This is inheritance built 91 * by hand. Similarly every pointer to a variable-size Python object can, 92 * in addition, be cast to PyVarObject*. 93 */ 94 struct PyObject { 95 version(Python_3_0_Or_Later) { 96 version(Issue7758Fixed) { 97 mixin _PyObject_HEAD_EXTRA; 98 } 99 Py_ssize_t ob_refcnt; 100 PyTypeObject* ob_type; 101 }else { 102 mixin PyObject_HEAD; 103 } 104 } 105 106 /** 107 Denotes a borrowed reference. 108 (Not part of Python api) 109 110 Intended use: An api function Foo returning a borrowed reference will 111 have return type Borrowed!PyObject* instead of PyObject*. Py_INCREF can 112 be used to get the original type back. 113 114 Params: 115 T = Python object type (PyObject, PyTypeObject, etc) 116 117 Example: 118 --- 119 Borrowed!PyObject* borrowed = PyTuple_GetItem(tuple, 0); 120 PyObject* item = Py_XINCREF(borrowed); 121 --- 122 */ 123 struct Borrowed(T) { } 124 alias Borrowed!PyObject PyObject_BorrowedRef; 125 /** 126 Convert a python reference to borrowed reference. 127 (Not part of Python api) 128 */ 129 Borrowed!T* borrowed(T)(T* obj) { 130 return cast(Borrowed!T*) obj; 131 } 132 133 version(Python_3_0_Or_Later) { 134 /// _ 135 mixin template PyObject_VAR_HEAD() { 136 /// _ 137 PyVarObject ob_base; 138 } 139 }else { 140 /** PyObject_VAR_HEAD defines the initial segment of all variable-size 141 * container objects. These end with a declaration of an array with 1 142 * element, but enough space is malloc'ed so that the array actually 143 * has room for ob_size elements. Note that ob_size is an element count, 144 * not necessarily a byte count. 145 */ 146 mixin template PyObject_VAR_HEAD() { 147 mixin PyObject_HEAD; 148 /// _ 149 Py_ssize_t ob_size; /* Number of items in variable part */ 150 } 151 } 152 153 /// _ 154 struct PyVarObject { 155 version(Python_3_0_Or_Later) { 156 version(Issue7758Fixed) { 157 mixin PyObject_HEAD; 158 }else{ 159 PyObject ob_base; 160 } 161 Py_ssize_t ob_size; /* Number of items in variable part */ 162 }else{ 163 mixin PyObject_VAR_HEAD; 164 } 165 } 166 167 /// _ 168 auto Py_REFCNT(T)(T* ob) { 169 return (cast(PyObject*)ob).ob_refcnt; 170 } 171 /// _ 172 auto Py_TYPE(T)(T* ob) { 173 return (cast(PyObject*)ob).ob_type; 174 } 175 /// _ 176 auto Py_SIZE(T)(T* ob) { 177 return (cast(PyVarObject*)ob).ob_size; 178 } 179 180 /// Not part of the python api, but annoying to do without. 181 void Py_SET_REFCNT(T)(T* ob, int refcnt) { 182 (cast(PyObject*) ob).ob_refcnt = refcnt; 183 } 184 /// ditto 185 void Py_SET_TYPE(T)(T* ob, PyTypeObject* tipo) { 186 (cast(PyObject*)ob).ob_type = tipo; 187 } 188 /// ditto 189 void Py_SET_SIZE(T)(T* ob, Py_ssize_t size) { 190 (cast(PyVarObject*)ob).ob_size = size; 191 } 192 193 /// _ 194 alias PyObject* function(PyObject*) unaryfunc; 195 /// _ 196 alias PyObject* function(PyObject*, PyObject*) binaryfunc; 197 /// _ 198 alias PyObject* function(PyObject*, PyObject*, PyObject*) ternaryfunc; 199 /// _ 200 alias Py_ssize_t function(PyObject*) lenfunc; 201 /// _ 202 alias lenfunc inquiry; 203 version(Python_3_0_Or_Later) { 204 }else{ 205 /// Availability: 2.* 206 alias int function(PyObject**, PyObject**) coercion; 207 } 208 /// _ 209 alias PyObject* function(PyObject*, Py_ssize_t) ssizeargfunc; 210 /// _ 211 alias PyObject* function(PyObject*, Py_ssize_t, Py_ssize_t) ssizessizeargfunc; 212 version(Python_2_5_Or_Later){ 213 }else{ 214 /// Availability: 2.4 215 alias ssizeargfunc intargfunc; 216 /// Availability: 2.4 217 alias ssizessizeargfunc intintargfunc; 218 } 219 /// _ 220 alias int function(PyObject*, Py_ssize_t, PyObject*) ssizeobjargproc; 221 /// _ 222 alias int function(PyObject*, Py_ssize_t, Py_ssize_t, PyObject*) ssizessizeobjargproc; 223 version(Python_2_5_Or_Later){ 224 }else{ 225 /// Availability: 2.4 226 alias ssizeobjargproc intobjargproc; 227 /// Availability: 2.4 228 alias ssizessizeobjargproc intintobjargproc; 229 } 230 /// _ 231 alias int function(PyObject*, PyObject*, PyObject*) objobjargproc; 232 233 version(Python_3_0_Or_Later) { 234 }else{ 235 /// ssize_t-based buffer interface 236 /// Availability: 2.* 237 alias Py_ssize_t function(PyObject*, Py_ssize_t, void**) readbufferproc; 238 /// ditto 239 alias Py_ssize_t function(PyObject*, Py_ssize_t, void**) writebufferproc; 240 /// ditto 241 alias Py_ssize_t function(PyObject*, Py_ssize_t*) segcountproc; 242 /// ditto 243 alias Py_ssize_t function(PyObject*, Py_ssize_t, char**) charbufferproc; 244 } 245 version(Python_2_5_Or_Later){ 246 }else{ 247 /// int-based buffer interface 248 /// Availability: 2.4 249 alias readbufferproc getreadbufferproc; 250 /// ditto 251 alias writebufferproc getwritebufferproc; 252 /// ditto 253 alias segcountproc getsegcountproc; 254 /// ditto 255 alias charbufferproc getcharbufferproc; 256 } 257 258 version(Python_2_6_Or_Later){ 259 /** Py3k buffer interface */ 260 /// Availability: >= 2.6 261 struct Py_buffer{ 262 void* buf; 263 /** borrowed reference */ 264 Borrowed!PyObject* obj; 265 /// _ 266 Py_ssize_t len; 267 /** This is Py_ssize_t so it can be 268 pointed to by strides in simple case.*/ 269 Py_ssize_t itemsize; 270 /// _ 271 int readonly; 272 /// _ 273 int ndim; 274 /// _ 275 char* format; 276 /// _ 277 Py_ssize_t* shape; 278 /// _ 279 Py_ssize_t* strides; 280 /// _ 281 Py_ssize_t* suboffsets; 282 version(Python_3_4_Or_Later) { 283 }else version(Python_2_7_Or_Later) { 284 /** static store for shape and strides of 285 mono-dimensional buffers. */ 286 /// Availability: >= 2.7 < 3.4 287 Py_ssize_t[2] smalltable; 288 } 289 /// _ 290 void* internal; 291 }; 292 293 /// Availability: >= 2.6 294 alias int function(PyObject*, Py_buffer*, int) getbufferproc; 295 /// Availability: >= 2.6 296 alias void function(PyObject*, Py_buffer*) releasebufferproc; 297 298 version(Python_3_8_Or_Later) { 299 alias PyObject* function(PyObject* callable, const(PyObject*)* args, 300 size_t nargsf, PyObject* kwnames) vectorcallfunc; 301 } 302 303 /** Flags for getting buffers */ 304 /// Availability: >= 2.6 305 enum PyBUF_SIMPLE = 0; 306 /// ditto 307 enum PyBUF_WRITABLE = 0x0001; 308 /* we used to include an E, backwards compatible alias */ 309 /// ditto 310 enum PyBUF_WRITEABLE = PyBUF_WRITABLE; 311 /// ditto 312 enum PyBUF_FORMAT = 0x0004; 313 /// ditto 314 enum PyBUF_ND = 0x0008; 315 /// ditto 316 enum PyBUF_STRIDES = (0x0010 | PyBUF_ND); 317 /// ditto 318 enum PyBUF_C_CONTIGUOUS = (0x0020 | PyBUF_STRIDES); 319 /// ditto 320 enum PyBUF_F_CONTIGUOUS = (0x0040 | PyBUF_STRIDES); 321 /// ditto 322 enum PyBUF_ANY_CONTIGUOUS = (0x0080 | PyBUF_STRIDES); 323 /// ditto 324 enum PyBUF_INDIRECT = (0x0100 | PyBUF_STRIDES); 325 326 /// ditto 327 enum PyBUF_CONTIG = (PyBUF_ND | PyBUF_WRITABLE); 328 /// ditto 329 enum PyBUF_CONTIG_RO = (PyBUF_ND); 330 331 /// ditto 332 enum PyBUF_STRIDED = (PyBUF_STRIDES | PyBUF_WRITABLE); 333 /// ditto 334 enum PyBUF_STRIDED_RO = (PyBUF_STRIDES); 335 336 /// ditto 337 enum PyBUF_RECORDS = (PyBUF_STRIDES | PyBUF_WRITABLE | PyBUF_FORMAT); 338 /// ditto 339 enum PyBUF_RECORDS_RO = (PyBUF_STRIDES | PyBUF_FORMAT); 340 341 /// ditto 342 enum PyBUF_FULL = (PyBUF_INDIRECT | PyBUF_WRITABLE | PyBUF_FORMAT); 343 /// ditto 344 enum PyBUF_FULL_RO = (PyBUF_INDIRECT | PyBUF_FORMAT); 345 346 347 /// ditto 348 enum PyBUF_READ = 0x100; 349 /// ditto 350 enum PyBUF_WRITE = 0x200; 351 /// ditto 352 enum PyBUF_SHADOW = 0x400; 353 /* end Py3k buffer interface */ 354 } 355 356 /// _ 357 alias int function(PyObject*, PyObject*) objobjproc; 358 /// _ 359 alias int function(PyObject*, void*) visitproc; 360 /// _ 361 alias int function(PyObject*, visitproc, void*) traverseproc; 362 363 /** For numbers without flag bit Py_TPFLAGS_CHECKTYPES set, all 364 arguments are guaranteed to be of the object's type (modulo 365 coercion hacks -- i.e. if the type's coercion function 366 returns other types, then these are allowed as well). Numbers that 367 have the Py_TPFLAGS_CHECKTYPES flag bit set should check *both* 368 arguments for proper type and implement the necessary conversions 369 in the slot functions themselves. */ 370 struct PyNumberMethods { 371 binaryfunc nb_add; 372 binaryfunc nb_subtract; 373 binaryfunc nb_multiply; 374 version(Python_3_0_Or_Later) { 375 }else { 376 binaryfunc nb_divide; 377 } 378 binaryfunc nb_remainder; 379 binaryfunc nb_divmod; 380 ternaryfunc nb_power; 381 unaryfunc nb_negative; 382 unaryfunc nb_positive; 383 unaryfunc nb_absolute; 384 version(Python_3_0_Or_Later) { 385 inquiry nb_bool; 386 }else { 387 inquiry nb_nonzero; 388 } 389 unaryfunc nb_invert; 390 binaryfunc nb_lshift; 391 binaryfunc nb_rshift; 392 binaryfunc nb_and; 393 binaryfunc nb_xor; 394 binaryfunc nb_or; 395 version(Python_3_0_Or_Later) { 396 }else{ 397 coercion nb_coerce; 398 } 399 unaryfunc nb_int; 400 version(Python_3_0_Or_Later) { 401 void* nb_reserved; /* the slot formerly known as nb_long */ 402 }else{ 403 unaryfunc nb_long; 404 } 405 unaryfunc nb_float; 406 version(Python_3_0_Or_Later) { 407 }else{ 408 unaryfunc nb_oct; 409 unaryfunc nb_hex; 410 } 411 412 binaryfunc nb_inplace_add; 413 binaryfunc nb_inplace_subtract; 414 binaryfunc nb_inplace_multiply; 415 version(Python_3_0_Or_Later) { 416 }else{ 417 binaryfunc nb_inplace_divide; 418 } 419 binaryfunc nb_inplace_remainder; 420 ternaryfunc nb_inplace_power; 421 binaryfunc nb_inplace_lshift; 422 binaryfunc nb_inplace_rshift; 423 binaryfunc nb_inplace_and; 424 binaryfunc nb_inplace_xor; 425 binaryfunc nb_inplace_or; 426 427 /** These require the Py_TPFLAGS_HAVE_CLASS flag */ 428 binaryfunc nb_floor_divide; 429 ///ditto 430 binaryfunc nb_true_divide; 431 ///ditto 432 binaryfunc nb_inplace_floor_divide; 433 ///ditto 434 binaryfunc nb_inplace_true_divide; 435 436 version(Python_2_5_Or_Later){ 437 /// Availability: >= 2.5 438 unaryfunc nb_index; 439 } 440 441 version(Python_3_5_Or_Later) { 442 binaryfunc nb_matrix_multiply; 443 binaryfunc nb_inplace_matrix_multiply; 444 } 445 } 446 447 /// _ 448 struct PySequenceMethods { 449 /// _ 450 lenfunc sq_length; 451 /// _ 452 binaryfunc sq_concat; 453 /// _ 454 ssizeargfunc sq_repeat; 455 /// _ 456 ssizeargfunc sq_item; 457 version(Python_3_0_Or_Later) { 458 /// _ 459 void* was_sq_slice; 460 }else{ 461 /// Availability: 2.* 462 ssizessizeargfunc sq_slice; 463 } 464 /// _ 465 ssizeobjargproc sq_ass_item; 466 version(Python_3_0_Or_Later) { 467 /// _ 468 void* was_sq_ass_slice; 469 }else{ 470 /// Availability: 2.* 471 ssizessizeobjargproc sq_ass_slice; 472 } 473 /// _ 474 objobjproc sq_contains; 475 /// _ 476 binaryfunc sq_inplace_concat; 477 /// _ 478 ssizeargfunc sq_inplace_repeat; 479 } 480 481 /// _ 482 struct PyMappingMethods { 483 /// _ 484 lenfunc mp_length; 485 /// _ 486 binaryfunc mp_subscript; 487 /// _ 488 objobjargproc mp_ass_subscript; 489 } 490 491 version(Python_3_5_Or_Later) { 492 /// _ 493 struct PyAsyncMethods { 494 unaryfunc am_await; 495 unaryfunc am_aiter; 496 unaryfunc am_anext; 497 } 498 } 499 500 /// _ 501 struct PyBufferProcs { 502 version(Python_3_0_Or_Later) { 503 }else{ 504 /// Availability: 2.* 505 readbufferproc bf_getreadbuffer; 506 /// Availability: 2.* 507 writebufferproc bf_getwritebuffer; 508 /// Availability: 2.* 509 segcountproc bf_getsegcount; 510 /// Availability: 2.* 511 charbufferproc bf_getcharbuffer; 512 } 513 version(Python_2_6_Or_Later){ 514 /// Availability: 2.6+ 515 getbufferproc bf_getbuffer; 516 /// Availability: 2.6+ 517 releasebufferproc bf_releasebuffer; 518 } 519 } 520 521 522 /// _ 523 alias void function(void*) freefunc; 524 /// _ 525 alias void function(PyObject*) destructor; 526 /// _ 527 alias int function(PyObject*, FILE*, int) printfunc; 528 /// _ 529 alias PyObject* function(PyObject*, char*) getattrfunc; 530 /// _ 531 alias PyObject* function(PyObject*, PyObject*) getattrofunc; 532 /// _ 533 alias int function(PyObject*, char*, PyObject*) setattrfunc; 534 /// _ 535 alias int function(PyObject*, PyObject*, PyObject*) setattrofunc; 536 version(Python_3_0_Or_Later) { 537 }else{ 538 /// Availability: 2.* 539 alias int function(PyObject*, PyObject*) cmpfunc; 540 } 541 /// _ 542 alias PyObject* function(PyObject*) reprfunc; 543 /// _ 544 alias Py_hash_t function(PyObject*) hashfunc; 545 /// _ 546 alias PyObject* function(PyObject*, PyObject*, int) richcmpfunc; 547 /// _ 548 alias PyObject* function(PyObject*) getiterfunc; 549 /// _ 550 alias PyObject* function(PyObject*) iternextfunc; 551 /// _ 552 alias PyObject* function(PyObject*, PyObject*, PyObject*) descrgetfunc; 553 /// _ 554 alias int function(PyObject*, PyObject*, PyObject*) descrsetfunc; 555 /// _ 556 alias int function(PyObject*, PyObject*, PyObject*) initproc; 557 /// _ 558 alias PyObject* function(PyTypeObject*, PyObject*, PyObject*) newfunc; 559 /// _ 560 alias PyObject* function(PyTypeObject*, Py_ssize_t) allocfunc; 561 562 /** 563 Type objects contain a string containing the type name (to help somewhat 564 in debugging), the allocation parameters (see PyObject_New() and 565 PyObject_NewVar()), 566 and methods for accessing objects of the type. Methods are optional, a 567 nil pointer meaning that particular kind of access is not available for 568 this type. The Py_DECREF() macro uses the tp_dealloc method without 569 checking for a nil pointer; it should always be implemented except if 570 the implementation can guarantee that the reference count will never 571 reach zero (e.g., for statically allocated type objects). 572 573 NB: the methods for certain type groups are now contained in separate 574 method blocks. 575 */ 576 struct PyTypeObject { 577 version(Issue7758Fixed) { 578 mixin PyObject_VAR_HEAD; 579 }else{ 580 version(Python_3_0_Or_Later) { 581 PyVarObject ob_base; 582 }else { 583 Py_ssize_t ob_refcnt; 584 PyTypeObject* ob_type; 585 Py_ssize_t ob_size; /* Number of items in variable part */ 586 } 587 } 588 /** For printing, in format "<module>.<name>" */ 589 const(char)* tp_name; 590 /** For allocation */ 591 Py_ssize_t tp_basicsize, tp_itemsize; 592 593 /** Methods to implement standard operations */ 594 destructor tp_dealloc; 595 version(Python_3_8_Or_Later) { 596 /// ditto 597 Py_ssize_t tp_vectorcall_offset; 598 }else{ 599 /// ditto 600 printfunc tp_print; 601 } 602 /// ditto 603 getattrfunc tp_getattr; 604 /// ditto 605 setattrfunc tp_setattr; 606 /// ditto 607 version(Python_3_5_Or_Later) { 608 PyAsyncMethods* tp_as_async; 609 }else version(Python_3_0_Or_Later) { 610 void* tp_reserved; 611 }else{ 612 cmpfunc tp_compare; 613 } 614 /// ditto 615 reprfunc tp_repr; 616 617 /** Method suites for standard classes */ 618 PyNumberMethods* tp_as_number; 619 /// ditto 620 PySequenceMethods* tp_as_sequence; 621 /// ditto 622 PyMappingMethods* tp_as_mapping; 623 624 /** More standard operations (here for binary compatibility) */ 625 hashfunc tp_hash; 626 /// ditto 627 ternaryfunc tp_call; 628 /// ditto 629 reprfunc tp_str; 630 /// ditto 631 getattrofunc tp_getattro; 632 /// ditto 633 setattrofunc tp_setattro; 634 635 /** Functions to access object as input/output buffer */ 636 PyBufferProcs* tp_as_buffer; 637 638 /** Flags to define presence of optional/expanded features */ 639 C_ulong tp_flags; 640 641 /** Documentation string */ 642 const(char)* tp_doc; 643 644 /** call function for all accessible objects */ 645 traverseproc tp_traverse; 646 647 /** delete references to contained objects */ 648 inquiry tp_clear; 649 650 /** rich comparisons */ 651 richcmpfunc tp_richcompare; 652 653 /** weak reference enabler */ 654 version(Python_2_5_Or_Later){ 655 Py_ssize_t tp_weaklistoffset; 656 }else{ 657 C_long tp_weaklistoffset; 658 } 659 660 /** Iterators */ 661 getiterfunc tp_iter; 662 /// ditto 663 iternextfunc tp_iternext; 664 665 /** Attribute descriptor and subclassing stuff */ 666 PyMethodDef* tp_methods; 667 /// ditto 668 PyMemberDef* tp_members; 669 /// ditto 670 PyGetSetDef* tp_getset; 671 /// ditto 672 PyTypeObject* tp_base; 673 /// ditto 674 PyObject* tp_dict; 675 /// ditto 676 descrgetfunc tp_descr_get; 677 /// ditto 678 descrsetfunc tp_descr_set; 679 /// ditto 680 version(Python_2_5_Or_Later){ 681 Py_ssize_t tp_dictoffset; 682 }else{ 683 C_long tp_dictoffset; 684 } 685 /// ditto 686 initproc tp_init; 687 /// ditto 688 allocfunc tp_alloc; 689 /// ditto 690 newfunc tp_new; 691 /** Low-level free-memory routine */ 692 freefunc tp_free; 693 /** For PyObject_IS_GC */ 694 inquiry tp_is_gc; 695 /// _ 696 PyObject* tp_bases; 697 /** method resolution order */ 698 PyObject* tp_mro; 699 /// _ 700 PyObject* tp_cache; 701 /// _ 702 PyObject* tp_subclasses; 703 /// _ 704 PyObject* tp_weaklist; 705 /// _ 706 destructor tp_del; 707 version(Python_2_6_Or_Later){ 708 /** Type attribute cache version tag. Added in version 2.6 */ 709 uint tp_version_tag; 710 } 711 712 version(Python_3_0_Or_Later) { 713 /// Availability: 3.?? 714 destructor tp_finalize; 715 } 716 717 version(Python_3_8_Or_Later) { 718 vectorcallfunc tp_vectorcall; 719 int function(PyObject*, FILE*, int) tp_print; 720 } 721 } 722 723 version(Python_3_0_Or_Later) { 724 /// Availability: 3.* 725 struct PyType_Slot{ 726 /** slot id, see below */ 727 int slot; 728 /** function pointer */ 729 void* pfunc; 730 } 731 732 /// Availability: 3.* 733 struct PyType_Spec{ 734 /// _ 735 const(char)* name; 736 /// _ 737 int basicsize; 738 /// _ 739 int itemsize; 740 /// _ 741 int flags; 742 /** terminated by slot==0. */ 743 PyType_Slot* slots; 744 } 745 746 /// Availability: 3.* 747 PyObject* PyType_FromSpec(PyType_Spec*); 748 } 749 750 /** The *real* layout of a type object when allocated on the heap */ 751 struct PyHeapTypeObject { 752 version(Python_2_5_Or_Later){ 753 /// Availability: >= 2.5 754 PyTypeObject ht_type; 755 }else{ 756 /// Availability: 2.4 757 PyTypeObject type; 758 } 759 version(Python_3_5_Or_Later) { 760 /// Availability: >= 3.5 761 PyAsyncMethods as_async; 762 } 763 /// _ 764 PyNumberMethods as_number; 765 /// _ 766 PyMappingMethods as_mapping; 767 /** as_sequence comes after as_mapping, 768 so that the mapping wins when both 769 the mapping and the sequence define 770 a given operator (e.g. __getitem__). 771 see add_operators() in typeobject.c . */ 772 PySequenceMethods as_sequence; 773 /// _ 774 PyBufferProcs as_buffer; 775 version(Python_2_5_Or_Later){ 776 /// Availability: >= 2.5 777 PyObject* ht_name; 778 /// Availability: >= 2.5 779 PyObject* ht_slots; 780 }else{ 781 /// Availability: 2.4 782 PyObject* name; 783 /// Availability: 2.4 784 PyObject* slots; 785 } 786 787 version(Python_3_5_Or_Later) { 788 PyObject* ht_qualname; 789 void* ht_cached_keys; 790 } 791 } 792 793 /** Generic type check */ 794 int PyType_IsSubtype(PyTypeObject*, PyTypeObject*); 795 796 // D translation of C macro: 797 /// _ 798 int PyObject_TypeCheck()(PyObject* ob, PyTypeObject* tp) { 799 return (ob.ob_type == tp || PyType_IsSubtype(ob.ob_type, tp)); 800 } 801 802 /** built-in 'type' */ 803 mixin(PyAPI_DATA!"PyTypeObject PyType_Type"); 804 /** built-in 'object' */ 805 mixin(PyAPI_DATA!"PyTypeObject PyBaseObject_Type"); 806 /** built-in 'super' */ 807 mixin(PyAPI_DATA!"PyTypeObject PySuper_Type"); 808 809 version(Python_3_2_Or_Later) { 810 /// Availability: >= 3.2 811 C_long PyType_GetFlags(PyTypeObject*); 812 } 813 814 // D translation of C macro: 815 /// _ 816 int PyType_Check()(PyObject* op) { 817 return PyObject_TypeCheck(op, &PyType_Type); 818 } 819 // D translation of C macro: 820 /// _ 821 int PyType_CheckExact()(PyObject* op) { 822 return op.ob_type == &PyType_Type; 823 } 824 825 /// _ 826 int PyType_Ready(PyTypeObject*); 827 /// _ 828 PyObject* PyType_GenericAlloc(PyTypeObject*, Py_ssize_t); 829 /// _ 830 PyObject* PyType_GenericNew(PyTypeObject*, PyObject*, PyObject*); 831 /// _ 832 PyObject* _PyType_Lookup(PyTypeObject*, PyObject*); 833 /// _ 834 version(Python_2_7_Or_Later) { 835 /// Availability: >= 2.7 836 PyObject* _PyObject_LookupSpecial(PyObject*, char*, PyObject**); 837 } 838 version(Python_3_0_Or_Later) { 839 /// Availability: 3.* 840 PyTypeObject* _PyType_CalculateMetaclass(PyTypeObject*, PyObject*); 841 } 842 version(Python_2_6_Or_Later){ 843 /// Availability: >= 2.6 844 uint PyType_ClearCache(); 845 /// Availability: >= 2.6 846 void PyType_Modified(PyTypeObject *); 847 } 848 849 /// _ 850 int PyObject_Print(PyObject*, FILE*, int); 851 version(Python_3_0_Or_Later) { 852 /// Availability: 3.* 853 void _Py_BreakPoint(); 854 } 855 /// _ 856 PyObject* PyObject_Repr(PyObject*); 857 version(Python_3_0_Or_Later) { 858 }else version(Python_2_5_Or_Later) { 859 /// Availability: 2.5, 2.6, 2.7 860 PyObject* _PyObject_Str(PyObject*); 861 } 862 /// _ 863 PyObject* PyObject_Str(PyObject*); 864 865 version(Python_3_0_Or_Later) { 866 /// Availability: 3.* 867 PyObject* PyObject_ASCII(PyObject*); 868 /// Availability: 3.* 869 PyObject* PyObject_Bytes(PyObject*); 870 }else{ 871 /// Availability: 2.* 872 alias PyObject_Str PyObject_Bytes; 873 /// Availability: 2.* 874 PyObject * PyObject_Unicode(PyObject*); 875 /// Availability: 2.* 876 int PyObject_Compare(PyObject*, PyObject*); 877 } 878 /// _ 879 PyObject* PyObject_RichCompare(PyObject*, PyObject*, int); 880 /// _ 881 int PyObject_RichCompareBool(PyObject*, PyObject*, int); 882 /// _ 883 PyObject* PyObject_GetAttrString(PyObject*, const(char)*); 884 /// _ 885 int PyObject_SetAttrString(PyObject*, const(char)*, PyObject*); 886 /// _ 887 int PyObject_HasAttrString(PyObject*, const(char)*); 888 /// _ 889 PyObject* PyObject_GetAttr(PyObject*, PyObject*); 890 /// _ 891 int PyObject_SetAttr(PyObject*, PyObject*, PyObject*); 892 /// _ 893 int PyObject_HasAttr(PyObject*, PyObject*); 894 /// _ 895 PyObject* PyObject_SelfIter(PyObject*); 896 /// _ 897 PyObject* PyObject_GenericGetAttr(PyObject*, PyObject*); 898 /// _ 899 int PyObject_GenericSetAttr(PyObject*, 900 PyObject*, PyObject*); 901 /// _ 902 Py_hash_t PyObject_Hash(PyObject*); 903 version(Python_2_6_Or_Later) { 904 /// Availability: >= 2.6 905 Py_hash_t PyObject_HashNotImplemented(PyObject*); 906 } 907 /// _ 908 int PyObject_IsTrue(PyObject*); 909 /// _ 910 int PyObject_Not(PyObject*); 911 /// _ 912 int PyCallable_Check(PyObject*); 913 version(Python_3_0_Or_Later) { 914 }else{ 915 /// Availability: 2.* 916 int PyNumber_Coerce(PyObject**, PyObject**); 917 /// Availability: 2.* 918 int PyNumber_CoerceEx(PyObject**, PyObject**); 919 } 920 921 /// _ 922 void PyObject_ClearWeakRefs(PyObject*); 923 924 /** PyObject_Dir(obj) acts like Python __builtin__.dir(obj), returning a 925 list of strings. PyObject_Dir(NULL) is like __builtin__.dir(), 926 returning the names of the current locals. In this case, if there are 927 no current locals, NULL is returned, and PyErr_Occurred() is false. 928 */ 929 PyObject * PyObject_Dir(PyObject *); 930 931 /** Helpers for printing recursive container types */ 932 int Py_ReprEnter(PyObject *); 933 /// ditto 934 void Py_ReprLeave(PyObject *); 935 936 /// _ 937 Py_hash_t _Py_HashDouble(double); 938 /// _ 939 Py_hash_t _Py_HashPointer(void*); 940 941 version(Python_3_1_Or_Later) { 942 version = Py_HashSecret; 943 }else version(Python_3_0_Or_Later) { 944 }else version(Python_2_7_Or_Later) { 945 version = Py_HashSecret; 946 } 947 version(Py_HashSecret) { 948 /// Availability: 2.7, >= 3.1 949 struct _Py_HashSecret_t{ 950 /// _ 951 Py_hash_t prefix; 952 /// _ 953 Py_hash_t suffix; 954 } 955 /// Availability: 2.7, >= 3.1 956 mixin(PyAPI_DATA!"_Py_HashSecret_t _Py_HashSecret"); 957 } 958 959 /// _ 960 auto PyObject_REPR()(PyObject* obj) { 961 version(Python_3_0_Or_Later) { 962 import deimos.python.unicodeobject; 963 return _PyUnicode_AsString(PyObject_Repr(obj)); 964 }else{ 965 import deimos.python.stringobject; 966 return PyString_AS_STRING(PyObject_Repr(obj)); 967 } 968 } 969 /// _ 970 enum int Py_PRINT_RAW = 1; 971 972 973 version(Python_3_0_Or_Later) { 974 }else{ 975 /** PyBufferProcs contains bf_getcharbuffer */ 976 /// Availability: 2.* 977 enum int Py_TPFLAGS_HAVE_GETCHARBUFFER = 1L<<0; 978 /** PySequenceMethods contains sq_contains */ 979 /// Availability: 2.* 980 enum int Py_TPFLAGS_HAVE_SEQUENCE_IN = 1L<<1; 981 /** This is here for backwards compatibility. 982 Extensions that use the old GC 983 API will still compile but the objects will not be tracked by the GC. */ 984 /// Availability: 2.* 985 enum int Py_TPFLAGS_GC = 0; 986 /** PySequenceMethods and PyNumberMethods contain in-place operators */ 987 /// Availability: 2.* 988 enum int Py_TPFLAGS_HAVE_INPLACEOPS = 1L<<3; 989 /** PyNumberMethods do their own coercion */ 990 /// Availability: 2.* 991 enum int Py_TPFLAGS_CHECKTYPES = 1L<<4; 992 /** tp_richcompare is defined */ 993 /// Availability: 2.* 994 enum int Py_TPFLAGS_HAVE_RICHCOMPARE = 1L<<5; 995 /** Objects which are weakly referencable if their tp_weaklistoffset is >0 */ 996 /// Availability: 2.* 997 enum int Py_TPFLAGS_HAVE_WEAKREFS = 1L<<6; 998 /** tp_iter is defined */ 999 /// Availability: 2.* 1000 enum int Py_TPFLAGS_HAVE_ITER = 1L<<7; 1001 /** New members introduced by Python 2.2 exist */ 1002 /// Availability: 2.* 1003 enum int Py_TPFLAGS_HAVE_CLASS = 1L<<8; 1004 } 1005 /** Set if the type object is dynamically allocated */ 1006 enum int Py_TPFLAGS_HEAPTYPE = 1L<<9; 1007 /** Set if the type allows subclassing */ 1008 enum int Py_TPFLAGS_BASETYPE = 1L<<10; 1009 /** Set if the type is 'ready' -- fully initialized */ 1010 enum int Py_TPFLAGS_READY = 1L<<12; 1011 /** Set while the type is being 'readied', to prevent recursive ready calls */ 1012 enum int Py_TPFLAGS_READYING = 1L<<13; 1013 /** Objects support garbage collection (see objimp.h) */ 1014 enum int Py_TPFLAGS_HAVE_GC = 1L<<14; 1015 1016 // YYY: Should conditionalize for stackless: 1017 //#ifdef STACKLESS 1018 //#define Py_TPFLAGS_HAVE_STACKLESS_EXTENSION (3L<<15) 1019 //#else 1020 /// _ 1021 enum int Py_TPFLAGS_HAVE_STACKLESS_EXTENSION = 0; 1022 //#endif 1023 version(Python_3_0_Or_Later) { 1024 }else version(Python_2_5_Or_Later){ 1025 /** Objects support nb_index in PyNumberMethods */ 1026 /// Availability: 2.* 1027 enum Py_TPFLAGS_HAVE_INDEX = 1L<<17; 1028 } 1029 version(Python_2_6_Or_Later){ 1030 /** Objects support type attribute cache */ 1031 /// Availability: >= 2.6 1032 enum Py_TPFLAGS_HAVE_VERSION_TAG = (1L<<18); 1033 /// ditto 1034 enum Py_TPFLAGS_VALID_VERSION_TAG = (1L<<19); 1035 1036 /** Type is abstract and cannot be instantiated */ 1037 /// Availability: >= 2.6 1038 enum Py_TPFLAGS_IS_ABSTRACT = (1L<<20); 1039 1040 version(Python_3_0_Or_Later) { 1041 }else { 1042 /** Has the new buffer protocol */ 1043 /// Availability: 2.6,2.7 1044 enum Py_TPFLAGS_HAVE_NEWBUFFER = (1L<<21); 1045 } 1046 1047 /** These flags are used to determine if a type is a subclass. */ 1048 /// Availability: >= 2.6 1049 enum Py_TPFLAGS_INT_SUBCLASS =(1L<<23); 1050 /// ditto 1051 enum Py_TPFLAGS_LONG_SUBCLASS =(1L<<24); 1052 /// ditto 1053 enum Py_TPFLAGS_LIST_SUBCLASS =(1L<<25); 1054 /// ditto 1055 enum Py_TPFLAGS_TUPLE_SUBCLASS =(1L<<26); 1056 /// ditto 1057 version(Python_3_0_Or_Later) { 1058 enum Py_TPFLAGS_BYTES_SUBCLASS =(1L<<27); 1059 }else{ 1060 enum Py_TPFLAGS_STRING_SUBCLASS =(1L<<27); 1061 } 1062 /// ditto 1063 enum Py_TPFLAGS_UNICODE_SUBCLASS =(1L<<28); 1064 /// ditto 1065 enum Py_TPFLAGS_DICT_SUBCLASS =(1L<<29); 1066 /// ditto 1067 enum Py_TPFLAGS_BASE_EXC_SUBCLASS =(1L<<30); 1068 /// ditto 1069 enum Py_TPFLAGS_TYPE_SUBCLASS =(1L<<31); 1070 } 1071 1072 version(Python_3_0_Or_Later) { 1073 /// _ 1074 enum Py_TPFLAGS_DEFAULT = Py_TPFLAGS_HAVE_STACKLESS_EXTENSION | 1075 Py_TPFLAGS_HAVE_VERSION_TAG; 1076 }else version(Python_2_5_Or_Later){ 1077 /// _ 1078 enum Py_TPFLAGS_DEFAULT = 1079 Py_TPFLAGS_HAVE_GETCHARBUFFER | 1080 Py_TPFLAGS_HAVE_SEQUENCE_IN | 1081 Py_TPFLAGS_HAVE_INPLACEOPS | 1082 Py_TPFLAGS_HAVE_RICHCOMPARE | 1083 Py_TPFLAGS_HAVE_WEAKREFS | 1084 Py_TPFLAGS_HAVE_ITER | 1085 Py_TPFLAGS_HAVE_CLASS | 1086 Py_TPFLAGS_HAVE_STACKLESS_EXTENSION | 1087 Py_TPFLAGS_HAVE_INDEX | 1088 0 1089 ; 1090 version(Python_2_6_Or_Later) { 1091 // meh 1092 enum Py_TPFLAGS_DEFAULT_EXTERNAL = Py_TPFLAGS_DEFAULT; 1093 } 1094 }else{ 1095 /// _ 1096 enum int Py_TPFLAGS_DEFAULT = 1097 Py_TPFLAGS_HAVE_GETCHARBUFFER | 1098 Py_TPFLAGS_HAVE_SEQUENCE_IN | 1099 Py_TPFLAGS_HAVE_INPLACEOPS | 1100 Py_TPFLAGS_HAVE_RICHCOMPARE | 1101 Py_TPFLAGS_HAVE_WEAKREFS | 1102 Py_TPFLAGS_HAVE_ITER | 1103 Py_TPFLAGS_HAVE_CLASS | 1104 Py_TPFLAGS_HAVE_STACKLESS_EXTENSION | 1105 0 1106 ; 1107 } 1108 1109 // D translation of C macro: 1110 /// _ 1111 int PyType_HasFeature()(PyTypeObject* t, int f) { 1112 version(Python_3_2_Or_Later) { 1113 return (PyType_GetFlags(t) & f) != 0; 1114 }else{ 1115 return (t.tp_flags & f) != 0; 1116 } 1117 } 1118 1119 version(Python_2_6_Or_Later){ 1120 alias PyType_HasFeature PyType_FastSubclass; 1121 } 1122 1123 /** 1124 Initializes reference counts to 1, and 1125 in special builds (Py_REF_DEBUG, Py_TRACE_REFS) performs additional 1126 bookkeeping appropriate to the special build. 1127 */ 1128 void _Py_NewReference()(PyObject* op) { 1129 Py_SET_REFCNT(op, 1); 1130 } 1131 1132 /** 1133 Increment reference counts. Can be used wherever a void expression is allowed. 1134 The argument must not be a NULL pointer. If it may be NULL, use 1135 Py_XINCREF instead. 1136 1137 In addition, converts and returns Borrowed references to their base types. 1138 */ 1139 auto Py_INCREF(T)(T op) 1140 if(is(T == PyObject*) || is(T _unused : Borrowed!P*, P)) 1141 { 1142 static if(is(T _unused : Borrowed!P*, P)) { 1143 PyObject* pop = cast(PyObject*) op; 1144 ++pop.ob_refcnt; 1145 return cast(P*) pop; 1146 }else { 1147 ++op.ob_refcnt; 1148 } 1149 } 1150 1151 /** 1152 Increment reference counts. Can be used wherever a void expression is allowed. 1153 The argument may be a NULL pointer. 1154 1155 In addition, converts and returns Borrowed references to their base types. 1156 The argument may not be null. 1157 */ 1158 auto Py_XINCREF(T)(T op) { 1159 if (op == null) { 1160 //static if(is(typeof(return) == void)) 1161 static if(is(typeof(Py_INCREF!T(op)) == void)) 1162 return; 1163 else { 1164 assert(0, "INCREF on null"); 1165 } 1166 } 1167 return Py_INCREF(op); 1168 } 1169 1170 /** 1171 Used to decrement reference counts. Calls the object's deallocator function 1172 when the refcount falls to 0; for objects that don't contain references to 1173 other objects or heap memory this can be the standard function free(). 1174 Can be used wherever a void expression is allowed. The argument must not be a 1175 NULL pointer. If it may be NULL, use Py_XDECREF instead. 1176 */ 1177 void Py_DECREF()(PyObject *op) { 1178 // version(PY_REF_DEBUG) _Py_RefTotal++ 1179 --op.ob_refcnt; 1180 1181 // EMN: this is a horrible idea because it takes forever to figure out 1182 // what's going on if this is being called from within the garbage 1183 // collector. 1184 1185 // EMN: if we do keep it, don't change the assert! 1186 // assert(0) or assert(condition) mess up linking somehow. 1187 if(op.ob_refcnt < 0) assert (0, "refcount negative"); 1188 if(op.ob_refcnt != 0) { 1189 // version(PY_REF_DEBUG) _Py_NegativeRefcount(__FILE__, __LINE__, cast(PyObject*)op); 1190 }else { 1191 op.ob_type.tp_dealloc(op); 1192 } 1193 } 1194 1195 /** Same as Py_DECREF, except is a no-op if op is null. 1196 */ 1197 void Py_XDECREF()(PyObject* op) 1198 { 1199 if(op == null) { 1200 return; 1201 } 1202 1203 Py_DECREF(op); 1204 } 1205 1206 /** 1207 These are provided as conveniences to Python runtime embedders, so that 1208 they can have object code that is not dependent on Python compilation flags. 1209 */ 1210 void Py_IncRef(PyObject *); 1211 /// ditto 1212 void Py_DecRef(PyObject *); 1213 1214 mixin(PyAPI_DATA!"PyObject _Py_NoneStruct"); 1215 1216 // issue 8683 gets in the way of this being a property 1217 Borrowed!PyObject* Py_None()() { 1218 return borrowed(&_Py_NoneStruct); 1219 } 1220 /** Rich comparison opcodes */ 1221 enum Py_LT = 0; 1222 /// ditto 1223 enum Py_LE = 1; 1224 /// ditto 1225 enum Py_EQ = 2; 1226 /// ditto 1227 enum Py_NE = 3; 1228 /// ditto 1229 enum Py_GT = 4; 1230 /// ditto 1231 enum Py_GE = 5; 1232 1233 version(Python_3_0_Or_Later) { 1234 /// Availability: 3.* 1235 void _Py_Dealloc(PyObject*); 1236 }