1 /** 2 Mirror abstract.h 3 4 See_Also: 5 <a href="http://docs.python.org/c-api/abstract.html"> Abstract Objects Layer</a> 6 */ 7 module deimos.python.abstract_; 8 9 import deimos.python.pyport; 10 import deimos.python.object; 11 12 extern(C): 13 14 // D translations of C macros: 15 /// _ 16 int PyObject_DelAttrString()(PyObject* o, const(char) *a) { 17 return PyObject_SetAttrString(o, a, null); 18 } 19 /// _ 20 int PyObject_DelAttr()(PyObject* o, PyObject* a) { 21 return PyObject_SetAttr(o, a, null); 22 } 23 24 version(Python_3_0_Or_Later) { 25 }else{ 26 /// _ 27 int PyObject_Cmp(PyObject* o1, PyObject* o2, int *result); 28 } 29 30 //-////////////////////////////////////////////////////////////////////////// 31 // CALLABLES 32 //-////////////////////////////////////////////////////////////////////////// 33 34 /// _ 35 PyObject* PyObject_Call(PyObject* callable_object, PyObject* args, PyObject* kw); 36 /// _ 37 PyObject* PyObject_CallObject(PyObject* callable_object, PyObject* args); 38 /// _ 39 PyObject* PyObject_CallFunction(PyObject* callable_object, char* format, ...); 40 /// _ 41 PyObject* PyObject_CallMethod(PyObject* o, const(char)* m, const(char)* format, ...); 42 /// _ 43 PyObject* PyObject_CallFunctionObjArgs(PyObject* callable, ...); 44 /// _ 45 PyObject* PyObject_CallMethodObjArgs(PyObject* o,PyObject* m, ...); 46 47 //-////////////////////////////////////////////////////////////////////////// 48 // GENERIC 49 //-////////////////////////////////////////////////////////////////////////// 50 /// _ 51 PyObject* PyObject_Type(PyObject* o); 52 53 //-////////////////////////////////////////////////////////////////////////// 54 // CONTAINERS 55 //-////////////////////////////////////////////////////////////////////////// 56 57 /// _ 58 Py_ssize_t PyObject_Length(PyObject* o); 59 /// _ 60 alias PyObject_Length PyObject_Size; 61 62 version(Python_3_0_Or_Later) { 63 Py_ssize_t PyObject_LengthHint(PyObject*, Py_ssize_t); 64 }else version(Python_2_6_Or_Later){ 65 /** The length hint function returns a non-negative value from o.__len__() 66 or o.__length_hint__(). If those methods aren't found or return a negative 67 value, then the defaultvalue is returned. If one of the calls fails, 68 this function returns -1. 69 */ 70 Py_ssize_t _PyObject_LengthHint(PyObject*, Py_ssize_t); 71 }else version(Python_2_5_Or_Later){ 72 /** The length hint function returns a non-negative value from o.__len__() 73 or o.__length_hint__(). If those methods aren't found or return a negative 74 value, then the defaultvalue is returned. If one of the calls fails, 75 this function returns -1. 76 */ 77 Py_ssize_t _PyObject_LengthHint(PyObject*); 78 } 79 80 /// _ 81 PyObject* PyObject_GetItem(PyObject* o, PyObject* key); 82 /// _ 83 int PyObject_SetItem(PyObject* o, PyObject* key, PyObject* v); 84 /// _ 85 int PyObject_DelItemString(PyObject* o, char* key); 86 /// _ 87 int PyObject_DelItem(PyObject* o, PyObject* key); 88 /// _ 89 90 int PyObject_AsCharBuffer(PyObject* obj, const(char)** buffer, 91 Py_ssize_t* buffer_len); 92 /// _ 93 int PyObject_CheckReadBuffer(PyObject* obj); 94 /// _ 95 int PyObject_AsReadBuffer(PyObject* obj, void** buffer, Py_ssize_t* buffer_len); 96 /// _ 97 int PyObject_AsWriteBuffer(PyObject* obj, void** buffer, Py_ssize_t* buffer_len); 98 99 version(Python_2_6_Or_Later){ 100 /* new buffer API */ 101 102 /** Return 1 if the getbuffer function is available, otherwise 103 return 0 */ 104 int PyObject_CheckBuffer()(PyObject* obj) { 105 version(Python_3_0_Or_Later) { 106 return (obj.ob_type.tp_as_buffer !is null) && 107 (obj.ob_type.tp_as_buffer.bf_getbuffer !is null); 108 }else{ 109 return (obj.ob_type.tp_as_buffer !is null) && 110 PyType_HasFeature(obj.ob_type, Py_TPFLAGS_HAVE_NEWBUFFER) && 111 (obj.ob_type.tp_as_buffer.bf_getbuffer !is null); 112 } 113 } 114 115 /** This is a C-API version of the getbuffer function call. It checks 116 to make sure object has the required function pointer and issues the 117 call. Returns -1 and raises an error on failure and returns 0 on 118 success 119 */ 120 int PyObject_GetBuffer(PyObject* obj, Py_buffer* view, 121 int flags); 122 123 /** Get the memory area pointed to by the indices for the buffer given. 124 Note that view->ndim is the assumed size of indices 125 */ 126 void* PyBuffer_GetPointer(Py_buffer* view, Py_ssize_t* indices); 127 128 /** Return the implied itemsize of the data-format area from a 129 struct-style description 130 131 abstract.h lies; this function actually does not exist. We're lying too. 132 */ 133 int PyBuffer_SizeFromFormat(const(char) *); 134 135 /// _ 136 int PyBuffer_ToContiguous(void* buf, Py_buffer* view, 137 Py_ssize_t len, char fort); 138 139 /** Copy len bytes of data from the contiguous chunk of memory 140 pointed to by buf into the buffer exported by obj. Return 141 0 on success and return -1 and raise a PyBuffer_Error on 142 error (i.e. the object does not have a buffer interface or 143 it is not working). 144 145 If fort is 'F' and the object is multi-dimensional, 146 then the data will be copied into the array in 147 Fortran-style (first dimension varies the fastest). If 148 fort is 'C', then the data will be copied into the array 149 in C-style (last dimension varies the fastest). If fort 150 is 'A', then it does not matter and the copy will be made 151 in whatever way is more efficient. 152 153 */ 154 int PyBuffer_FromContiguous(Py_buffer* view, void* buf, 155 Py_ssize_t len, char fort); 156 157 158 159 /** Copy the data from the src buffer to the buffer of dest 160 */ 161 int PyObject_CopyData(PyObject* dest, PyObject* src); 162 163 164 /// _ 165 int PyBuffer_IsContiguous(Py_buffer* view, char fort); 166 167 168 /** Fill the strides array with byte-strides of a contiguous 169 (Fortran-style if fort is 'F' or C-style otherwise) 170 array of the given shape with the given number of bytes 171 per element. 172 */ 173 void PyBuffer_FillContiguousStrides(int ndims, 174 Py_ssize_t* shape, 175 Py_ssize_t* strides, 176 int itemsize, 177 char fort); 178 179 /** Fills in a buffer-info structure correctly for an exporter 180 that can only share a contiguous chunk of memory of 181 "unsigned bytes" of the given length. Returns 0 on success 182 and -1 (with raising an error) on error. 183 */ 184 int PyBuffer_FillInfo(Py_buffer* view, PyObject* o, void* buf, 185 Py_ssize_t len, int readonly, 186 int flags); 187 188 /** Releases a Py_buffer obtained from getbuffer ParseTuple's s*. 189 */ 190 void PyBuffer_Release(Py_buffer* view); 191 192 /** 193 Takes an arbitrary object and returns the result of 194 calling obj.__format__(format_spec). 195 */ 196 PyObject* PyObject_Format(PyObject* obj, 197 PyObject* format_spec); 198 199 } 200 201 //-////////////////////////////////////////////////////////////////////////// 202 // ITERATORS 203 //-////////////////////////////////////////////////////////////////////////// 204 205 /// _ 206 PyObject* PyObject_GetIter(PyObject*); 207 208 // D translation of C macro: 209 /// _ 210 int PyIter_Check()(PyObject* obj) { 211 version(Python_3_0_Or_Later) { 212 return obj.ob_type.tp_iternext != null && 213 obj.ob_type.tp_iternext != &_PyObject_NextNotImplemented; 214 }else version(Python_2_7_Or_Later) { 215 return PyType_HasFeature(obj.ob_type, Py_TPFLAGS_HAVE_ITER) 216 && obj.ob_type.tp_iternext != null && 217 obj.ob_type.tp_iternext != &_PyObject_NextNotImplemented; 218 }else { 219 return PyType_HasFeature(obj.ob_type, Py_TPFLAGS_HAVE_ITER) 220 && obj.ob_type.tp_iternext != null; 221 } 222 } 223 224 /// _ 225 PyObject* PyIter_Next(PyObject*); 226 227 ///////////////////////////////////////////////////////////////////////////// 228 // NUMBERS 229 ///////////////////////////////////////////////////////////////////////////// 230 231 int PyNumber_Check(PyObject* o); 232 233 /// _ 234 PyObject* PyNumber_Add(PyObject* o1, PyObject* o2); 235 /// _ 236 PyObject* PyNumber_Subtract(PyObject* o1, PyObject* o2); 237 /// _ 238 PyObject* PyNumber_Multiply(PyObject* o1, PyObject* o2); 239 240 version(Python_3_5_Or_Later) { 241 /// _ 242 PyObject* PyNumber_MatrixMultiply(PyObject* o1, PyObject* o2); 243 } 244 245 version(Python_3_0_Or_Later) { 246 }else{ 247 /// Availability: 2.* 248 PyObject* PyNumber_Divide(PyObject* o1, PyObject* o2); 249 } 250 /// _ 251 PyObject* PyNumber_FloorDivide(PyObject* o1, PyObject* o2); 252 /// _ 253 PyObject* PyNumber_TrueDivide(PyObject* o1, PyObject* o2); 254 /// _ 255 PyObject* PyNumber_Remainder(PyObject* o1, PyObject* o2); 256 /// _ 257 PyObject* PyNumber_Divmod(PyObject* o1, PyObject* o2); 258 /// _ 259 PyObject* PyNumber_Power(PyObject* o1, PyObject* o2, PyObject* o3); 260 /// _ 261 PyObject* PyNumber_Negative(PyObject* o); 262 /// _ 263 PyObject* PyNumber_Positive(PyObject* o); 264 /// _ 265 PyObject* PyNumber_Absolute(PyObject* o); 266 /// _ 267 PyObject* PyNumber_Invert(PyObject* o); 268 /// _ 269 PyObject* PyNumber_Lshift(PyObject* o1, PyObject* o2); 270 /// _ 271 PyObject* PyNumber_Rshift(PyObject* o1, PyObject* o2); 272 /// _ 273 PyObject* PyNumber_And(PyObject* o1, PyObject* o2); 274 /// _ 275 PyObject* PyNumber_Xor(PyObject* o1, PyObject* o2); 276 /// _ 277 PyObject* PyNumber_Or(PyObject* o1, PyObject* o2); 278 279 version(Python_2_5_Or_Later) { 280 /// Availability: >= 2.5 281 int PyIndex_Check()(PyObject* obj) { 282 version(Python_3_0_Or_Later) { 283 return obj.ob_type.tp_as_number !is null && 284 obj.ob_type.tp_as_number.nb_index !is null; 285 }else{ 286 return obj.ob_type.tp_as_number !is null && 287 PyType_HasFeature(obj.ob_type, Py_TPFLAGS_HAVE_INDEX) && 288 obj.ob_type.tp_as_number.nb_index !is null; 289 } 290 } 291 /// Availability: >= 2.5 292 PyObject* PyNumber_Index(PyObject* o); 293 /** 294 Returns the Integral instance converted to an int. The 295 instance is expected to be int or long or have an __int__ 296 method. Steals integral's reference. error_format will be 297 used to create the TypeError if integral isn't actually an 298 Integral instance. error_format should be a format string 299 that can accept a char* naming integral's type. 300 301 Availability: >= 2.5 302 */ 303 Py_ssize_t PyNumber_AsSsize_t(PyObject* o, PyObject* exc); 304 } 305 version(Python_2_6_Or_Later) { 306 /// Availability: >= 2.6 307 PyObject* _PyNumber_ConvertIntegralToInt( 308 PyObject* integral, 309 const(char)* error_format); 310 } 311 312 version(Python_3_0_Or_Later) { 313 }else { 314 /// Availability: 2.* 315 PyObject* PyNumber_Int(PyObject* o); 316 } 317 /// _ 318 PyObject* PyNumber_Long(PyObject* o); 319 /// _ 320 PyObject* PyNumber_Float(PyObject* o); 321 /// _ 322 PyObject* PyNumber_InPlaceAdd(PyObject* o1, PyObject* o2); 323 /// _ 324 PyObject* PyNumber_InPlaceSubtract(PyObject* o1, PyObject* o2); 325 /// _ 326 PyObject* PyNumber_InPlaceMultiply(PyObject* o1, PyObject* o2); 327 328 version(Python_3_5_Or_Later) { 329 /// _ 330 PyObject* PyNumber_InPlaceMatrixMultiply(PyObject* o1, PyObject* o2); 331 } 332 333 version(Python_3_0_Or_Later) { 334 }else{ 335 /// Availability: 2.* 336 PyObject* PyNumber_InPlaceDivide(PyObject* o1, PyObject* o2); 337 } 338 /// _ 339 PyObject* PyNumber_InPlaceFloorDivide(PyObject* o1, PyObject* o2); 340 /// _ 341 PyObject* PyNumber_InPlaceTrueDivide(PyObject* o1, PyObject* o2); 342 /// _ 343 PyObject* PyNumber_InPlaceRemainder(PyObject* o1, PyObject* o2); 344 /// _ 345 PyObject* PyNumber_InPlacePower(PyObject* o1, PyObject* o2, PyObject* o3); 346 /// _ 347 PyObject* PyNumber_InPlaceLshift(PyObject* o1, PyObject* o2); 348 /// _ 349 PyObject* PyNumber_InPlaceRshift(PyObject* o1, PyObject* o2); 350 /// _ 351 PyObject* PyNumber_InPlaceAnd(PyObject* o1, PyObject* o2); 352 /// _ 353 PyObject* PyNumber_InPlaceXor(PyObject* o1, PyObject* o2); 354 /// _ 355 PyObject* PyNumber_InPlaceOr(PyObject* o1, PyObject* o2); 356 357 version(Python_2_6_Or_Later){ 358 /** 359 Returns the integer n converted to a string with a base, with a base 360 marker of 0b, 0o or 0x prefixed if applicable. 361 If n is not an int object, it is converted with PyNumber_Index first. 362 363 Availability: >= 2.6 364 */ 365 PyObject* PyNumber_ToBase(PyObject* n, int base); 366 } 367 368 //-////////////////////////////////////////////////////////////////////////// 369 // SEQUENCES 370 //-////////////////////////////////////////////////////////////////////////// 371 372 /// _ 373 int PySequence_Check(PyObject* o); 374 /// _ 375 Py_ssize_t PySequence_Size(PyObject* o); 376 /// _ 377 alias PySequence_Size PySequence_Length; 378 /// _ 379 PyObject* PySequence_Concat(PyObject* o1, PyObject* o2); 380 /// _ 381 PyObject* PySequence_Repeat(PyObject* o, Py_ssize_t count); 382 /// _ 383 PyObject* PySequence_GetItem(PyObject* o, Py_ssize_t i); 384 /// _ 385 PyObject* PySequence_GetSlice(PyObject* o, Py_ssize_t i1, Py_ssize_t i2); 386 /// _ 387 int PySequence_SetItem(PyObject* o, Py_ssize_t i, PyObject* v); 388 /// _ 389 int PySequence_DelItem(PyObject* o, Py_ssize_t i); 390 /// _ 391 int PySequence_SetSlice(PyObject* o, Py_ssize_t i1, Py_ssize_t i2, PyObject* v); 392 /// _ 393 int PySequence_DelSlice(PyObject* o, Py_ssize_t i1, Py_ssize_t i2); 394 /// _ 395 PyObject* PySequence_Tuple(PyObject* o); 396 /// _ 397 PyObject* PySequence_List(PyObject* o); 398 /// _ 399 PyObject* PySequence_Fast(PyObject* o, const(char)* m); 400 // D translations of C macros: 401 /// _ 402 Py_ssize_t PySequence_Fast_GET_SIZE()(PyObject* o) { 403 return PyList_Check(o) ? cast(Py_ssize_t) PyList_GET_SIZE(o) : 404 cast(Py_ssize_t) PyTuple_GET_SIZE(o); 405 } 406 /// _ 407 PyObject* PySequence_Fast_GET_ITEM()(PyObject* o, Py_ssize_t i) { 408 return PyList_Check(o) ? PyList_GET_ITEM(o, i) : PyTuple_GET_ITEM(o, i); 409 } 410 /// _ 411 PyObject* PySequence_ITEM()(PyObject* o, Py_ssize_t i) { 412 return o.ob_type.tp_as_sequence.sq_item(o, i); 413 } 414 /// _ 415 PyObject** PySequence_Fast_ITEMS()(PyObject* sf) { 416 return 417 PyList_Check(sf) ? 418 (cast(PyListObject *)sf).ob_item 419 : (cast(PyTupleObject *)sf).ob_item 420 ; 421 } 422 /// _ 423 Py_ssize_t PySequence_Count(PyObject* o, PyObject* value); 424 425 /// _ 426 enum PY_ITERSEARCH_COUNT = 1; 427 /// _ 428 enum PY_ITERSEARCH_INDEX = 2; 429 /// _ 430 enum PY_ITERSEARCH_CONTAINS = 3; 431 /// _ 432 Py_ssize_t _PySequence_IterSearch(PyObject* seq, PyObject* obj, int operation); 433 434 /// _ 435 int PySequence_In(PyObject* o, PyObject* value); 436 /// _ 437 alias PySequence_In PySequence_Contains; 438 /// _ 439 Py_ssize_t PySequence_Index(PyObject* o, PyObject* value); 440 /// _ 441 PyObject* PySequence_InPlaceConcat(PyObject* o1, PyObject* o2); 442 /// _ 443 PyObject* PySequence_InPlaceRepeat(PyObject* o, Py_ssize_t count); 444 445 //-////////////////////////////////////////////////////////////////////////// 446 // MAPPINGS 447 //-////////////////////////////////////////////////////////////////////////// 448 /// _ 449 int PyMapping_Check(PyObject* o); 450 /// _ 451 Py_ssize_t PyMapping_Length(PyObject* o); 452 /// _ 453 alias PyMapping_Length PyMapping_Size; 454 455 // D translations of C macros: 456 /// _ 457 int PyMapping_DelItemString()(PyObject* o, char* k) { 458 return PyObject_DelItemString(o, k); 459 } 460 /// _ 461 int PyMapping_DelItem()(PyObject* o, PyObject* k) { 462 return PyObject_DelItem(o, k); 463 } 464 /// _ 465 int PyMapping_HasKeyString(PyObject* o, char* key); 466 /// _ 467 int PyMapping_HasKey(PyObject* o, PyObject* key); 468 469 version(Python_3_0_Or_Later) { 470 /// _ 471 PyObject* PyMapping_Keys(PyObject* o); 472 /// _ 473 PyObject* PyMapping_Values(PyObject* o); 474 /// _ 475 PyObject* PyMapping_Items(PyObject* o); 476 }else { 477 // D translations of C macros: 478 /// Availability: 2.* 479 PyObject* PyMapping_Keys()(PyObject* o) { 480 return PyObject_CallMethod(o, "keys", null); 481 } 482 /// Availability: 2.* 483 PyObject* PyMapping_Values()(PyObject* o) { 484 return PyObject_CallMethod(o, "values", null); 485 } 486 /// Availability: 2.* 487 PyObject* PyMapping_Items()(PyObject* o) { 488 return PyObject_CallMethod(o, "items", null); 489 } 490 } 491 /// _ 492 PyObject* PyMapping_GetItemString(PyObject* o, char* key); 493 /// _ 494 int PyMapping_SetItemString(PyObject* o, char* key, PyObject* value); 495 496 //-////////////////////////////////////////////////////////////////////////// 497 // GENERIC 498 //-////////////////////////////////////////////////////////////////////////// 499 int PyObject_IsInstance(PyObject* object, PyObject* typeorclass); 500 /// _ 501 int PyObject_IsSubclass(PyObject* object, PyObject* typeorclass); 502 version(Python_2_6_Or_Later){ 503 /// Availability: >= 2.6 504 int _PyObject_RealIsInstance(PyObject* inst, PyObject* cls); 505 /// Availability: >= 2.6 506 int _PyObject_RealIsSubclass(PyObject* derived, PyObject* cls); 507 } 508 509 version(Python_3_0_Or_Later) { 510 /// _ 511 const(char*)* _PySequence_BytesToCharpArray(PyObject* self); 512 /// _ 513 void _Py_FreeCharPArray(const(char*)* array); 514 } 515 516