| 1 |
|
| 2 |
|
| 3 |
#include "Python.h" |
| 4 |
#include <ctype.h> |
| 5 |
#include "structmember.h" |
| 6 |
#include "longintrepr.h" |
| 7 |
|
| 8 |
#define NEW_STYLE_NUMBER(o) PyType_HasFeature((o)->ob_type, \ |
| 9 |
Py_TPFLAGS_CHECKTYPES) |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
static PyObject * |
| 15 |
type_error(const char *msg, PyObject *obj) |
| 16 |
{ |
| 17 |
PyErr_Format(PyExc_TypeError, msg, obj->ob_type->tp_name); |
| 18 |
return NULL; |
| 19 |
} |
| 20 |
|
| 21 |
static PyObject * |
| 22 |
null_error(void) |
| 23 |
{ |
| 24 |
if (!PyErr_Occurred()) |
| 25 |
PyErr_SetString(PyExc_SystemError, |
| 26 |
"null argument to internal routine"); |
| 27 |
return NULL; |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
int |
| 33 |
PyObject_Cmp(PyObject *o1, PyObject *o2, int *result) |
| 34 |
{ |
| 35 |
int r; |
| 36 |
|
| 37 |
if (o1 == NULL || o2 == NULL) { |
| 38 |
null_error(); |
| 39 |
return -1; |
| 40 |
} |
| 41 |
r = PyObject_Compare(o1, o2); |
| 42 |
if (PyErr_Occurred()) |
| 43 |
return -1; |
| 44 |
*result = r; |
| 45 |
return 0; |
| 46 |
} |
| 47 |
|
| 48 |
PyObject * |
| 49 |
PyObject_Type(PyObject *o) |
| 50 |
{ |
| 51 |
PyObject *v; |
| 52 |
|
| 53 |
if (o == NULL) |
| 54 |
return null_error(); |
| 55 |
v = (PyObject *)o->ob_type; |
| 56 |
Py_INCREF(v); |
| 57 |
return v; |
| 58 |
} |
| 59 |
|
| 60 |
Py_ssize_t |
| 61 |
PyObject_Size(PyObject *o) |
| 62 |
{ |
| 63 |
PySequenceMethods *m; |
| 64 |
|
| 65 |
if (o == NULL) { |
| 66 |
null_error(); |
| 67 |
return -1; |
| 68 |
} |
| 69 |
|
| 70 |
m = o->ob_type->tp_as_sequence; |
| 71 |
if (m && m->sq_length) |
| 72 |
return m->sq_length(o); |
| 73 |
|
| 74 |
return PyMapping_Size(o); |
| 75 |
} |
| 76 |
|
| 77 |
#undef PyObject_Length |
| 78 |
Py_ssize_t |
| 79 |
PyObject_Length(PyObject *o) |
| 80 |
{ |
| 81 |
return PyObject_Size(o); |
| 82 |
} |
| 83 |
#define PyObject_Length PyObject_Size |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
Py_ssize_t |
| 93 |
_PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue) |
| 94 |
{ |
| 95 |
static PyObject *hintstrobj = NULL; |
| 96 |
PyObject *ro, *hintmeth; |
| 97 |
Py_ssize_t rv; |
| 98 |
|
| 99 |
|
| 100 |
rv = PyObject_Size(o); |
| 101 |
if (rv >= 0) |
| 102 |
return rv; |
| 103 |
if (PyErr_Occurred()) { |
| 104 |
if (!PyErr_ExceptionMatches(PyExc_TypeError) && |
| 105 |
!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 106 |
return -1; |
| 107 |
PyErr_Clear(); |
| 108 |
} |
| 109 |
|
| 110 |
if (PyInstance_Check(o)) |
| 111 |
return defaultvalue; |
| 112 |
|
| 113 |
hintmeth = _PyObject_LookupSpecial(o, "__length_hint__", &hintstrobj); |
| 114 |
if (hintmeth == NULL) { |
| 115 |
if (PyErr_Occurred()) |
| 116 |
return -1; |
| 117 |
else |
| 118 |
return defaultvalue; |
| 119 |
} |
| 120 |
ro = PyObject_CallFunctionObjArgs(hintmeth, NULL); |
| 121 |
Py_DECREF(hintmeth); |
| 122 |
if (ro == NULL) { |
| 123 |
if (!PyErr_ExceptionMatches(PyExc_TypeError) && |
| 124 |
!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 125 |
return -1; |
| 126 |
PyErr_Clear(); |
| 127 |
return defaultvalue; |
| 128 |
} |
| 129 |
rv = PyNumber_Check(ro) ? PyInt_AsSsize_t(ro) : defaultvalue; |
| 130 |
Py_DECREF(ro); |
| 131 |
return rv; |
| 132 |
} |
| 133 |
|
| 134 |
PyObject * |
| 135 |
PyObject_GetItem(PyObject *o, PyObject *key) |
| 136 |
{ |
| 137 |
PyMappingMethods *m; |
| 138 |
|
| 139 |
if (o == NULL || key == NULL) |
| 140 |
return null_error(); |
| 141 |
|
| 142 |
m = o->ob_type->tp_as_mapping; |
| 143 |
if (m && m->mp_subscript) |
| 144 |
return m->mp_subscript(o, key); |
| 145 |
|
| 146 |
if (o->ob_type->tp_as_sequence) { |
| 147 |
if (PyIndex_Check(key)) { |
| 148 |
Py_ssize_t key_value; |
| 149 |
key_value = PyNumber_AsSsize_t(key, PyExc_IndexError); |
| 150 |
if (key_value == -1 && PyErr_Occurred()) |
| 151 |
return NULL; |
| 152 |
return PySequence_GetItem(o, key_value); |
| 153 |
} |
| 154 |
else if (o->ob_type->tp_as_sequence->sq_item) |
| 155 |
return type_error("sequence index must " |
| 156 |
"be integer, not '%.200s'", key); |
| 157 |
} |
| 158 |
|
| 159 |
return type_error("'%.200s' object has no attribute '__getitem__'", o); |
| 160 |
} |
| 161 |
|
| 162 |
int |
| 163 |
PyObject_SetItem(PyObject *o, PyObject *key, PyObject *value) |
| 164 |
{ |
| 165 |
PyMappingMethods *m; |
| 166 |
|
| 167 |
if (o == NULL || key == NULL || value == NULL) { |
| 168 |
null_error(); |
| 169 |
return -1; |
| 170 |
} |
| 171 |
m = o->ob_type->tp_as_mapping; |
| 172 |
if (m && m->mp_ass_subscript) |
| 173 |
return m->mp_ass_subscript(o, key, value); |
| 174 |
|
| 175 |
if (o->ob_type->tp_as_sequence) { |
| 176 |
if (PyIndex_Check(key)) { |
| 177 |
Py_ssize_t key_value; |
| 178 |
key_value = PyNumber_AsSsize_t(key, PyExc_IndexError); |
| 179 |
if (key_value == -1 && PyErr_Occurred()) |
| 180 |
return -1; |
| 181 |
return PySequence_SetItem(o, key_value, value); |
| 182 |
} |
| 183 |
else if (o->ob_type->tp_as_sequence->sq_ass_item) { |
| 184 |
type_error("sequence index must be " |
| 185 |
"integer, not '%.200s'", key); |
| 186 |
return -1; |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
type_error("'%.200s' object does not support item assignment", o); |
| 191 |
return -1; |
| 192 |
} |
| 193 |
|
| 194 |
int |
| 195 |
PyObject_DelItem(PyObject *o, PyObject *key) |
| 196 |
{ |
| 197 |
PyMappingMethods *m; |
| 198 |
|
| 199 |
if (o == NULL || key == NULL) { |
| 200 |
null_error(); |
| 201 |
return -1; |
| 202 |
} |
| 203 |
m = o->ob_type->tp_as_mapping; |
| 204 |
if (m && m->mp_ass_subscript) |
| 205 |
return m->mp_ass_subscript(o, key, (PyObject*)NULL); |
| 206 |
|
| 207 |
if (o->ob_type->tp_as_sequence) { |
| 208 |
if (PyIndex_Check(key)) { |
| 209 |
Py_ssize_t key_value; |
| 210 |
key_value = PyNumber_AsSsize_t(key, PyExc_IndexError); |
| 211 |
if (key_value == -1 && PyErr_Occurred()) |
| 212 |
return -1; |
| 213 |
return PySequence_DelItem(o, key_value); |
| 214 |
} |
| 215 |
else if (o->ob_type->tp_as_sequence->sq_ass_item) { |
| 216 |
type_error("sequence index must be " |
| 217 |
"integer, not '%.200s'", key); |
| 218 |
return -1; |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
type_error("'%.200s' object does not support item deletion", o); |
| 223 |
return -1; |
| 224 |
} |
| 225 |
|
| 226 |
int |
| 227 |
PyObject_DelItemString(PyObject *o, char *key) |
| 228 |
{ |
| 229 |
PyObject *okey; |
| 230 |
int ret; |
| 231 |
|
| 232 |
if (o == NULL || key == NULL) { |
| 233 |
null_error(); |
| 234 |
return -1; |
| 235 |
} |
| 236 |
okey = PyString_FromString(key); |
| 237 |
if (okey == NULL) |
| 238 |
return -1; |
| 239 |
ret = PyObject_DelItem(o, okey); |
| 240 |
Py_DECREF(okey); |
| 241 |
return ret; |
| 242 |
} |
| 243 |
|
| 244 |
int |
| 245 |
PyObject_AsCharBuffer(PyObject *obj, |
| 246 |
const char **buffer, |
| 247 |
Py_ssize_t *buffer_len) |
| 248 |
{ |
| 249 |
PyBufferProcs *pb; |
| 250 |
char *pp; |
| 251 |
Py_ssize_t len; |
| 252 |
|
| 253 |
if (obj == NULL || buffer == NULL || buffer_len == NULL) { |
| 254 |
null_error(); |
| 255 |
return -1; |
| 256 |
} |
| 257 |
pb = obj->ob_type->tp_as_buffer; |
| 258 |
if (pb == NULL || |
| 259 |
pb->bf_getcharbuffer == NULL || |
| 260 |
pb->bf_getsegcount == NULL) { |
| 261 |
PyErr_SetString(PyExc_TypeError, |
| 262 |
"expected a string or other character buffer object"); |
| 263 |
return -1; |
| 264 |
} |
| 265 |
if ((*pb->bf_getsegcount)(obj,NULL) != 1) { |
| 266 |
PyErr_SetString(PyExc_TypeError, |
| 267 |
"expected a single-segment buffer object"); |
| 268 |
return -1; |
| 269 |
} |
| 270 |
len = (*pb->bf_getcharbuffer)(obj, 0, &pp); |
| 271 |
if (len < 0) |
| 272 |
return -1; |
| 273 |
*buffer = pp; |
| 274 |
*buffer_len = len; |
| 275 |
return 0; |
| 276 |
} |
| 277 |
|
| 278 |
int |
| 279 |
PyObject_CheckReadBuffer(PyObject *obj) |
| 280 |
{ |
| 281 |
PyBufferProcs *pb = obj->ob_type->tp_as_buffer; |
| 282 |
|
| 283 |
if (pb == NULL || |
| 284 |
pb->bf_getreadbuffer == NULL || |
| 285 |
pb->bf_getsegcount == NULL || |
| 286 |
(*pb->bf_getsegcount)(obj, NULL) != 1) |
| 287 |
return 0; |
| 288 |
return 1; |
| 289 |
} |
| 290 |
|
| 291 |
int PyObject_AsReadBuffer(PyObject *obj, |
| 292 |
const void **buffer, |
| 293 |
Py_ssize_t *buffer_len) |
| 294 |
{ |
| 295 |
PyBufferProcs *pb; |
| 296 |
void *pp; |
| 297 |
Py_ssize_t len; |
| 298 |
|
| 299 |
if (obj == NULL || buffer == NULL || buffer_len == NULL) { |
| 300 |
null_error(); |
| 301 |
return -1; |
| 302 |
} |
| 303 |
pb = obj->ob_type->tp_as_buffer; |
| 304 |
if (pb == NULL || |
| 305 |
pb->bf_getreadbuffer == NULL || |
| 306 |
pb->bf_getsegcount == NULL) { |
| 307 |
PyErr_SetString(PyExc_TypeError, |
| 308 |
"expected a readable buffer object"); |
| 309 |
return -1; |
| 310 |
} |
| 311 |
if ((*pb->bf_getsegcount)(obj, NULL) != 1) { |
| 312 |
PyErr_SetString(PyExc_TypeError, |
| 313 |
"expected a single-segment buffer object"); |
| 314 |
return -1; |
| 315 |
} |
| 316 |
len = (*pb->bf_getreadbuffer)(obj, 0, &pp); |
| 317 |
if (len < 0) |
| 318 |
return -1; |
| 319 |
*buffer = pp; |
| 320 |
*buffer_len = len; |
| 321 |
return 0; |
| 322 |
} |
| 323 |
|
| 324 |
int PyObject_AsWriteBuffer(PyObject *obj, |
| 325 |
void **buffer, |
| 326 |
Py_ssize_t *buffer_len) |
| 327 |
{ |
| 328 |
PyBufferProcs *pb; |
| 329 |
void*pp; |
| 330 |
Py_ssize_t len; |
| 331 |
|
| 332 |
if (obj == NULL || buffer == NULL || buffer_len == NULL) { |
| 333 |
null_error(); |
| 334 |
return -1; |
| 335 |
} |
| 336 |
pb = obj->ob_type->tp_as_buffer; |
| 337 |
if (pb == NULL || |
| 338 |
pb->bf_getwritebuffer == NULL || |
| 339 |
pb->bf_getsegcount == NULL) { |
| 340 |
PyErr_SetString(PyExc_TypeError, |
| 341 |
"expected a writeable buffer object"); |
| 342 |
return -1; |
| 343 |
} |
| 344 |
if ((*pb->bf_getsegcount)(obj, NULL) != 1) { |
| 345 |
PyErr_SetString(PyExc_TypeError, |
| 346 |
"expected a single-segment buffer object"); |
| 347 |
return -1; |
| 348 |
} |
| 349 |
len = (*pb->bf_getwritebuffer)(obj,0,&pp); |
| 350 |
if (len < 0) |
| 351 |
return -1; |
| 352 |
*buffer = pp; |
| 353 |
*buffer_len = len; |
| 354 |
return 0; |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
int |
| 360 |
PyObject_GetBuffer(PyObject *obj, Py_buffer *view, int flags) |
| 361 |
{ |
| 362 |
if (!PyObject_CheckBuffer(obj)) { |
| 363 |
PyErr_Format(PyExc_TypeError, |
| 364 |
"'%100s' does not have the buffer interface", |
| 365 |
Py_TYPE(obj)->tp_name); |
| 366 |
return -1; |
| 367 |
} |
| 368 |
return (*(obj->ob_type->tp_as_buffer->bf_getbuffer))(obj, view, flags); |
| 369 |
} |
| 370 |
|
| 371 |
static int |
| 372 |
_IsFortranContiguous(Py_buffer *view) |
| 373 |
{ |
| 374 |
Py_ssize_t sd, dim; |
| 375 |
int i; |
| 376 |
|
| 377 |
if (view->ndim == 0) return 1; |
| 378 |
if (view->strides == NULL) return (view->ndim == 1); |
| 379 |
|
| 380 |
sd = view->itemsize; |
| 381 |
if (view->ndim == 1) return (view->shape[0] == 1 || |
| 382 |
sd == view->strides[0]); |
| 383 |
for (i=0; i<view->ndim; i++) { |
| 384 |
dim = view->shape[i]; |
| 385 |
if (dim == 0) return 1; |
| 386 |
if (view->strides[i] != sd) return 0; |
| 387 |
sd *= dim; |
| 388 |
} |
| 389 |
return 1; |
| 390 |
} |
| 391 |
|
| 392 |
static int |
| 393 |
_IsCContiguous(Py_buffer *view) |
| 394 |
{ |
| 395 |
Py_ssize_t sd, dim; |
| 396 |
int i; |
| 397 |
|
| 398 |
if (view->ndim == 0) return 1; |
| 399 |
if (view->strides == NULL) return 1; |
| 400 |
|
| 401 |
sd = view->itemsize; |
| 402 |
if (view->ndim == 1) return (view->shape[0] == 1 || |
| 403 |
sd == view->strides[0]); |
| 404 |
for (i=view->ndim-1; i>=0; i--) { |
| 405 |
dim = view->shape[i]; |
| 406 |
if (dim == 0) return 1; |
| 407 |
if (view->strides[i] != sd) return 0; |
| 408 |
sd *= dim; |
| 409 |
} |
| 410 |
return 1; |
| 411 |
} |
| 412 |
|
| 413 |
int |
| 414 |
PyBuffer_IsContiguous(Py_buffer *view, char fort) |
| 415 |
{ |
| 416 |
|
| 417 |
if (view->suboffsets != NULL) return 0; |
| 418 |
|
| 419 |
if (fort == 'C') |
| 420 |
return _IsCContiguous(view); |
| 421 |
else if (fort == 'F') |
| 422 |
return _IsFortranContiguous(view); |
| 423 |
else if (fort == 'A') |
| 424 |
return (_IsCContiguous(view) || _IsFortranContiguous(view)); |
| 425 |
return 0; |
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
void* |
| 430 |
PyBuffer_GetPointer(Py_buffer *view, Py_ssize_t *indices) |
| 431 |
{ |
| 432 |
char* pointer; |
| 433 |
int i; |
| 434 |
pointer = (char *)view->buf; |
| 435 |
for (i = 0; i < view->ndim; i++) { |
| 436 |
pointer += view->strides[i]*indices[i]; |
| 437 |
if ((view->suboffsets != NULL) && (view->suboffsets[i] >= 0)) { |
| 438 |
pointer = *((char**)pointer) + view->suboffsets[i]; |
| 439 |
} |
| 440 |
} |
| 441 |
return (void*)pointer; |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
void |
| 446 |
_Py_add_one_to_index_F(int nd, Py_ssize_t *index, const Py_ssize_t *shape) |
| 447 |
{ |
| 448 |
int k; |
| 449 |
|
| 450 |
for (k=0; k<nd; k++) { |
| 451 |
if (index[k] < shape[k]-1) { |
| 452 |
index[k]++; |
| 453 |
break; |
| 454 |
} |
| 455 |
else { |
| 456 |
index[k] = 0; |
| 457 |
} |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
void |
| 462 |
_Py_add_one_to_index_C(int nd, Py_ssize_t *index, const Py_ssize_t *shape) |
| 463 |
{ |
| 464 |
int k; |
| 465 |
|
| 466 |
for (k=nd-1; k>=0; k--) { |
| 467 |
if (index[k] < shape[k]-1) { |
| 468 |
index[k]++; |
| 469 |
break; |
| 470 |
} |
| 471 |
else { |
| 472 |
index[k] = 0; |
| 473 |
} |
| 474 |
} |
| 475 |
} |
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
int |
| 483 |
PyBuffer_ToContiguous(void *buf, Py_buffer *view, Py_ssize_t len, char fort) |
| 484 |
{ |
| 485 |
int k; |
| 486 |
void (*addone)(int, Py_ssize_t *, const Py_ssize_t *); |
| 487 |
Py_ssize_t *indices, elements; |
| 488 |
char *dest, *ptr; |
| 489 |
|
| 490 |
if (len > view->len) { |
| 491 |
len = view->len; |
| 492 |
} |
| 493 |
|
| 494 |
if (PyBuffer_IsContiguous(view, fort)) { |
| 495 |
|
| 496 |
memcpy(buf, view->buf, len); |
| 497 |
return 0; |
| 498 |
} |
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*(view->ndim)); |
| 504 |
if (indices == NULL) { |
| 505 |
PyErr_NoMemory(); |
| 506 |
return -1; |
| 507 |
} |
| 508 |
for (k=0; k<view->ndim;k++) { |
| 509 |
indices[k] = 0; |
| 510 |
} |
| 511 |
|
| 512 |
if (fort == 'F') { |
| 513 |
addone = _Py_add_one_to_index_F; |
| 514 |
} |
| 515 |
else { |
| 516 |
addone = _Py_add_one_to_index_C; |
| 517 |
} |
| 518 |
dest = buf; |
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
elements = len / view->itemsize; |
| 523 |
while (elements--) { |
| 524 |
ptr = PyBuffer_GetPointer(view, indices); |
| 525 |
memcpy(dest, ptr, view->itemsize); |
| 526 |
dest += view->itemsize; |
| 527 |
addone(view->ndim, indices, view->shape); |
| 528 |
} |
| 529 |
PyMem_Free(indices); |
| 530 |
return 0; |
| 531 |
} |
| 532 |
|
| 533 |
int |
| 534 |
PyBuffer_FromContiguous(Py_buffer *view, void *buf, Py_ssize_t len, char fort) |
| 535 |
{ |
| 536 |
int k; |
| 537 |
void (*addone)(int, Py_ssize_t *, const Py_ssize_t *); |
| 538 |
Py_ssize_t *indices, elements; |
| 539 |
char *src, *ptr; |
| 540 |
|
| 541 |
if (len > view->len) { |
| 542 |
len = view->len; |
| 543 |
} |
| 544 |
|
| 545 |
if (PyBuffer_IsContiguous(view, fort)) { |
| 546 |
|
| 547 |
memcpy(view->buf, buf, len); |
| 548 |
return 0; |
| 549 |
} |
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*(view->ndim)); |
| 555 |
if (indices == NULL) { |
| 556 |
PyErr_NoMemory(); |
| 557 |
return -1; |
| 558 |
} |
| 559 |
for (k=0; k<view->ndim;k++) { |
| 560 |
indices[k] = 0; |
| 561 |
} |
| 562 |
|
| 563 |
if (fort == 'F') { |
| 564 |
addone = _Py_add_one_to_index_F; |
| 565 |
} |
| 566 |
else { |
| 567 |
addone = _Py_add_one_to_index_C; |
| 568 |
} |
| 569 |
src = buf; |
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
elements = len / view->itemsize; |
| 574 |
while (elements--) { |
| 575 |
ptr = PyBuffer_GetPointer(view, indices); |
| 576 |
memcpy(ptr, src, view->itemsize); |
| 577 |
src += view->itemsize; |
| 578 |
addone(view->ndim, indices, view->shape); |
| 579 |
} |
| 580 |
|
| 581 |
PyMem_Free(indices); |
| 582 |
return 0; |
| 583 |
} |
| 584 |
|
| 585 |
int PyObject_CopyData(PyObject *dest, PyObject *src) |
| 586 |
{ |
| 587 |
Py_buffer view_dest, view_src; |
| 588 |
int k; |
| 589 |
Py_ssize_t *indices, elements; |
| 590 |
char *dptr, *sptr; |
| 591 |
|
| 592 |
if (!PyObject_CheckBuffer(dest) || |
| 593 |
!PyObject_CheckBuffer(src)) { |
| 594 |
PyErr_SetString(PyExc_TypeError, |
| 595 |
"both destination and source must have the "\ |
| 596 |
"buffer interface"); |
| 597 |
return -1; |
| 598 |
} |
| 599 |
|
| 600 |
if (PyObject_GetBuffer(dest, &view_dest, PyBUF_FULL) != 0) return -1; |
| 601 |
if (PyObject_GetBuffer(src, &view_src, PyBUF_FULL_RO) != 0) { |
| 602 |
PyBuffer_Release(&view_dest); |
| 603 |
return -1; |
| 604 |
} |
| 605 |
|
| 606 |
if (view_dest.len < view_src.len) { |
| 607 |
PyErr_SetString(PyExc_BufferError, |
| 608 |
"destination is too small to receive data from source"); |
| 609 |
PyBuffer_Release(&view_dest); |
| 610 |
PyBuffer_Release(&view_src); |
| 611 |
return -1; |
| 612 |
} |
| 613 |
|
| 614 |
if ((PyBuffer_IsContiguous(&view_dest, 'C') && |
| 615 |
PyBuffer_IsContiguous(&view_src, 'C')) || |
| 616 |
(PyBuffer_IsContiguous(&view_dest, 'F') && |
| 617 |
PyBuffer_IsContiguous(&view_src, 'F'))) { |
| 618 |
|
| 619 |
memcpy(view_dest.buf, view_src.buf, view_src.len); |
| 620 |
PyBuffer_Release(&view_dest); |
| 621 |
PyBuffer_Release(&view_src); |
| 622 |
return 0; |
| 623 |
} |
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
indices = (Py_ssize_t *)PyMem_Malloc(sizeof(Py_ssize_t)*view_src.ndim); |
| 629 |
if (indices == NULL) { |
| 630 |
PyErr_NoMemory(); |
| 631 |
PyBuffer_Release(&view_dest); |
| 632 |
PyBuffer_Release(&view_src); |
| 633 |
return -1; |
| 634 |
} |
| 635 |
for (k=0; k<view_src.ndim;k++) { |
| 636 |
indices[k] = 0; |
| 637 |
} |
| 638 |
elements = 1; |
| 639 |
for (k=0; k<view_src.ndim; k++) { |
| 640 |
|
| 641 |
elements *= view_src.shape[k]; |
| 642 |
} |
| 643 |
while (elements--) { |
| 644 |
_Py_add_one_to_index_C(view_src.ndim, indices, view_src.shape); |
| 645 |
dptr = PyBuffer_GetPointer(&view_dest, indices); |
| 646 |
sptr = PyBuffer_GetPointer(&view_src, indices); |
| 647 |
memcpy(dptr, sptr, view_src.itemsize); |
| 648 |
} |
| 649 |
PyMem_Free(indices); |
| 650 |
PyBuffer_Release(&view_dest); |
| 651 |
PyBuffer_Release(&view_src); |
| 652 |
return 0; |
| 653 |
} |
| 654 |
|
| 655 |
void |
| 656 |
PyBuffer_FillContiguousStrides(int nd, Py_ssize_t *shape, |
| 657 |
Py_ssize_t *strides, int itemsize, |
| 658 |
char fort) |
| 659 |
{ |
| 660 |
int k; |
| 661 |
Py_ssize_t sd; |
| 662 |
|
| 663 |
sd = itemsize; |
| 664 |
if (fort == 'F') { |
| 665 |
for (k=0; k<nd; k++) { |
| 666 |
strides[k] = sd; |
| 667 |
sd *= shape[k]; |
| 668 |
} |
| 669 |
} |
| 670 |
else { |
| 671 |
for (k=nd-1; k>=0; k--) { |
| 672 |
strides[k] = sd; |
| 673 |
sd *= shape[k]; |
| 674 |
} |
| 675 |
} |
| 676 |
return; |
| 677 |
} |
| 678 |
|
| 679 |
int |
| 680 |
PyBuffer_FillInfo(Py_buffer *view, PyObject *obj, void *buf, Py_ssize_t len, |
| 681 |
int readonly, int flags) |
| 682 |
{ |
| 683 |
if (view == NULL) return 0; |
| 684 |
if (((flags & PyBUF_WRITABLE) == PyBUF_WRITABLE) && |
| 685 |
(readonly == 1)) { |
| 686 |
PyErr_SetString(PyExc_BufferError, |
| 687 |
"Object is not writable."); |
| 688 |
return -1; |
| 689 |
} |
| 690 |
|
| 691 |
view->obj = obj; |
| 692 |
if (obj) |
| 693 |
Py_INCREF(obj); |
| 694 |
view->buf = buf; |
| 695 |
view->len = len; |
| 696 |
view->readonly = readonly; |
| 697 |
view->itemsize = 1; |
| 698 |
view->format = NULL; |
| 699 |
if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) |
| 700 |
view->format = "B"; |
| 701 |
view->ndim = 1; |
| 702 |
view->shape = NULL; |
| 703 |
if ((flags & PyBUF_ND) == PyBUF_ND) |
| 704 |
view->shape = &(view->len); |
| 705 |
view->strides = NULL; |
| 706 |
if ((flags & PyBUF_STRIDES) == PyBUF_STRIDES) |
| 707 |
view->strides = &(view->itemsize); |
| 708 |
view->suboffsets = NULL; |
| 709 |
view->internal = NULL; |
| 710 |
return 0; |
| 711 |
} |
| 712 |
|
| 713 |
void |
| 714 |
PyBuffer_Release(Py_buffer *view) |
| 715 |
{ |
| 716 |
PyObject *obj = view->obj; |
| 717 |
if (obj && Py_TYPE(obj)->tp_as_buffer && Py_TYPE(obj)->tp_as_buffer->bf_releasebuffer) |
| 718 |
Py_TYPE(obj)->tp_as_buffer->bf_releasebuffer(obj, view); |
| 719 |
Py_XDECREF(obj); |
| 720 |
view->obj = NULL; |
| 721 |
} |
| 722 |
|
| 723 |
PyObject * |
| 724 |
PyObject_Format(PyObject* obj, PyObject *format_spec) |
| 725 |
{ |
| 726 |
PyObject *empty = NULL; |
| 727 |
PyObject *result = NULL; |
| 728 |
#ifdef Py_USING_UNICODE |
| 729 |
int spec_is_unicode; |
| 730 |
int result_is_unicode; |
| 731 |
#endif |
| 732 |
|
| 733 |
|
| 734 |
if (format_spec == NULL) { |
| 735 |
empty = PyString_FromStringAndSize(NULL, 0); |
| 736 |
format_spec = empty; |
| 737 |
} |
| 738 |
|
| 739 |
|
| 740 |
#ifdef Py_USING_UNICODE |
| 741 |
if (PyUnicode_Check(format_spec)) |
| 742 |
spec_is_unicode = 1; |
| 743 |
else if (PyString_Check(format_spec)) |
| 744 |
spec_is_unicode = 0; |
| 745 |
else { |
| 746 |
#else |
| 747 |
if (!PyString_Check(format_spec)) { |
| 748 |
#endif |
| 749 |
PyErr_Format(PyExc_TypeError, |
| 750 |
"format expects arg 2 to be string " |
| 751 |
"or unicode, not %.100s", Py_TYPE(format_spec)->tp_name); |
| 752 |
goto done; |
| 753 |
} |
| 754 |
|
| 755 |
|
| 756 |
if (PyInstance_Check(obj)) { |
| 757 |
|
| 758 |
PyObject *bound_method = PyObject_GetAttrString(obj, "__format__"); |
| 759 |
if (bound_method != NULL) { |
| 760 |
result = PyObject_CallFunctionObjArgs(bound_method, |
| 761 |
format_spec, |
| 762 |
NULL); |
| 763 |
Py_DECREF(bound_method); |
| 764 |
} else { |
| 765 |
PyObject *self_as_str = NULL; |
| 766 |
PyObject *format_method = NULL; |
| 767 |
Py_ssize_t format_len; |
| 768 |
|
| 769 |
PyErr_Clear(); |
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
#ifdef Py_USING_UNICODE |
| 775 |
if (spec_is_unicode) { |
| 776 |
format_len = PyUnicode_GET_SIZE(format_spec); |
| 777 |
self_as_str = PyObject_Unicode(obj); |
| 778 |
} else |
| 779 |
#endif |
| 780 |
{ |
| 781 |
format_len = PyString_GET_SIZE(format_spec); |
| 782 |
self_as_str = PyObject_Str(obj); |
| 783 |
} |
| 784 |
if (self_as_str == NULL) |
| 785 |
goto done1; |
| 786 |
|
| 787 |
if (format_len > 0) { |
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
if (PyErr_WarnEx( |
| 792 |
PyExc_PendingDeprecationWarning, |
| 793 |
"object.__format__ with a non-empty " |
| 794 |
"format string is deprecated", 1) |
| 795 |
< 0) { |
| 796 |
goto done1; |
| 797 |
} |
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
} |
| 806 |
|
| 807 |
|
| 808 |
format_method = PyObject_GetAttrString(self_as_str, "__format__"); |
| 809 |
if (format_method == NULL) { |
| 810 |
goto done1; |
| 811 |
} |
| 812 |
result = PyObject_CallFunctionObjArgs(format_method, |
| 813 |
format_spec, |
| 814 |
NULL); |
| 815 |
done1: |
| 816 |
Py_XDECREF(self_as_str); |
| 817 |
Py_XDECREF(format_method); |
| 818 |
if (result == NULL) |
| 819 |
goto done; |
| 820 |
} |
| 821 |
} else { |
| 822 |
|
| 823 |
|
| 824 |
static PyObject *format_cache = NULL; |
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
PyObject *method = _PyObject_LookupSpecial(obj, "__format__", |
| 829 |
&format_cache); |
| 830 |
if (method == NULL) { |
| 831 |
if (!PyErr_Occurred()) |
| 832 |
PyErr_Format(PyExc_TypeError, |
| 833 |
"Type %.100s doesn't define __format__", |
| 834 |
Py_TYPE(obj)->tp_name); |
| 835 |
goto done; |
| 836 |
} |
| 837 |
|
| 838 |
result = PyObject_CallFunctionObjArgs(method, format_spec, NULL); |
| 839 |
Py_DECREF(method); |
| 840 |
} |
| 841 |
|
| 842 |
if (result == NULL) |
| 843 |
goto done; |
| 844 |
|
| 845 |
|
| 846 |
#ifdef Py_USING_UNICODE |
| 847 |
if (PyUnicode_Check(result)) |
| 848 |
result_is_unicode = 1; |
| 849 |
else if (PyString_Check(result)) |
| 850 |
result_is_unicode = 0; |
| 851 |
else { |
| 852 |
#else |
| 853 |
if (!PyString_Check(result)) { |
| 854 |
#endif |
| 855 |
PyErr_Format(PyExc_TypeError, |
| 856 |
"%.100s.__format__ must return string or " |
| 857 |
"unicode, not %.100s", Py_TYPE(obj)->tp_name, |
| 858 |
Py_TYPE(result)->tp_name); |
| 859 |
Py_DECREF(result); |
| 860 |
result = NULL; |
| 861 |
goto done; |
| 862 |
} |
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
#ifdef Py_USING_UNICODE |
| 867 |
if (spec_is_unicode && !result_is_unicode) { |
| 868 |
PyObject *tmp = PyObject_Unicode(result); |
| 869 |
|
| 870 |
Py_DECREF(result); |
| 871 |
result = tmp; |
| 872 |
} |
| 873 |
#endif |
| 874 |
|
| 875 |
done: |
| 876 |
Py_XDECREF(empty); |
| 877 |
return result; |
| 878 |
} |
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
int |
| 883 |
PyNumber_Check(PyObject *o) |
| 884 |
{ |
| 885 |
return o && o->ob_type->tp_as_number && |
| 886 |
(o->ob_type->tp_as_number->nb_int || |
| 887 |
o->ob_type->tp_as_number->nb_float); |
| 888 |
} |
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
#define NB_SLOT(x) offsetof(PyNumberMethods, x) |
| 895 |
#define NB_BINOP(nb_methods, slot) \ |
| 896 |
(*(binaryfunc*)(& ((char*)nb_methods)[slot])) |
| 897 |
#define NB_TERNOP(nb_methods, slot) \ |
| 898 |
(*(ternaryfunc*)(& ((char*)nb_methods)[slot])) |
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
static PyObject * |
| 923 |
binary_op1(PyObject *v, PyObject *w, const int op_slot) |
| 924 |
{ |
| 925 |
PyObject *x; |
| 926 |
binaryfunc slotv = NULL; |
| 927 |
binaryfunc slotw = NULL; |
| 928 |
|
| 929 |
if (v->ob_type->tp_as_number != NULL && NEW_STYLE_NUMBER(v)) |
| 930 |
slotv = NB_BINOP(v->ob_type->tp_as_number, op_slot); |
| 931 |
if (w->ob_type != v->ob_type && |
| 932 |
w->ob_type->tp_as_number != NULL && NEW_STYLE_NUMBER(w)) { |
| 933 |
slotw = NB_BINOP(w->ob_type->tp_as_number, op_slot); |
| 934 |
if (slotw == slotv) |
| 935 |
slotw = NULL; |
| 936 |
} |
| 937 |
if (slotv) { |
| 938 |
if (slotw && PyType_IsSubtype(w->ob_type, v->ob_type)) { |
| 939 |
x = slotw(v, w); |
| 940 |
if (x != Py_NotImplemented) |
| 941 |
return x; |
| 942 |
Py_DECREF(x); |
| 943 |
slotw = NULL; |
| 944 |
} |
| 945 |
x = slotv(v, w); |
| 946 |
if (x != Py_NotImplemented) |
| 947 |
return x; |
| 948 |
Py_DECREF(x); |
| 949 |
} |
| 950 |
if (slotw) { |
| 951 |
x = slotw(v, w); |
| 952 |
if (x != Py_NotImplemented) |
| 953 |
return x; |
| 954 |
Py_DECREF(x); |
| 955 |
} |
| 956 |
if (!NEW_STYLE_NUMBER(v) || !NEW_STYLE_NUMBER(w)) { |
| 957 |
int err = PyNumber_CoerceEx(&v, &w); |
| 958 |
if (err < 0) { |
| 959 |
return NULL; |
| 960 |
} |
| 961 |
if (err == 0) { |
| 962 |
PyNumberMethods *mv = v->ob_type->tp_as_number; |
| 963 |
if (mv) { |
| 964 |
binaryfunc slot; |
| 965 |
slot = NB_BINOP(mv, op_slot); |
| 966 |
if (slot) { |
| 967 |
x = slot(v, w); |
| 968 |
Py_DECREF(v); |
| 969 |
Py_DECREF(w); |
| 970 |
return x; |
| 971 |
} |
| 972 |
} |
| 973 |
|
| 974 |
Py_DECREF(v); |
| 975 |
Py_DECREF(w); |
| 976 |
} |
| 977 |
} |
| 978 |
Py_INCREF(Py_NotImplemented); |
| 979 |
return Py_NotImplemented; |
| 980 |
} |
| 981 |
|
| 982 |
static PyObject * |
| 983 |
binop_type_error(PyObject *v, PyObject *w, const char *op_name) |
| 984 |
{ |
| 985 |
PyErr_Format(PyExc_TypeError, |
| 986 |
"unsupported operand type(s) for %.100s: " |
| 987 |
"'%.100s' and '%.100s'", |
| 988 |
op_name, |
| 989 |
v->ob_type->tp_name, |
| 990 |
w->ob_type->tp_name); |
| 991 |
return NULL; |
| 992 |
} |
| 993 |
|
| 994 |
static PyObject * |
| 995 |
binary_op(PyObject *v, PyObject *w, const int op_slot, const char *op_name) |
| 996 |
{ |
| 997 |
PyObject *result = binary_op1(v, w, op_slot); |
| 998 |
if (result == Py_NotImplemented) { |
| 999 |
Py_DECREF(result); |
| 1000 |
return binop_type_error(v, w, op_name); |
| 1001 |
} |
| 1002 |
return result; |
| 1003 |
} |
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
static PyObject * |
| 1035 |
ternary_op(PyObject *v, |
| 1036 |
PyObject *w, |
| 1037 |
PyObject *z, |
| 1038 |
const int op_slot, |
| 1039 |
const char *op_name) |
| 1040 |
{ |
| 1041 |
PyNumberMethods *mv, *mw, *mz; |
| 1042 |
PyObject *x = NULL; |
| 1043 |
ternaryfunc slotv = NULL; |
| 1044 |
ternaryfunc slotw = NULL; |
| 1045 |
ternaryfunc slotz = NULL; |
| 1046 |
|
| 1047 |
mv = v->ob_type->tp_as_number; |
| 1048 |
mw = w->ob_type->tp_as_number; |
| 1049 |
if (mv != NULL && NEW_STYLE_NUMBER(v)) |
| 1050 |
slotv = NB_TERNOP(mv, op_slot); |
| 1051 |
if (w->ob_type != v->ob_type && |
| 1052 |
mw != NULL && NEW_STYLE_NUMBER(w)) { |
| 1053 |
slotw = NB_TERNOP(mw, op_slot); |
| 1054 |
if (slotw == slotv) |
| 1055 |
slotw = NULL; |
| 1056 |
} |
| 1057 |
if (slotv) { |
| 1058 |
if (slotw && PyType_IsSubtype(w->ob_type, v->ob_type)) { |
| 1059 |
x = slotw(v, w, z); |
| 1060 |
if (x != Py_NotImplemented) |
| 1061 |
return x; |
| 1062 |
Py_DECREF(x); |
| 1063 |
slotw = NULL; |
| 1064 |
} |
| 1065 |
x = slotv(v, w, z); |
| 1066 |
if (x != Py_NotImplemented) |
| 1067 |
return x; |
| 1068 |
Py_DECREF(x); |
| 1069 |
} |
| 1070 |
if (slotw) { |
| 1071 |
x = slotw(v, w, z); |
| 1072 |
if (x != Py_NotImplemented) |
| 1073 |
return x; |
| 1074 |
Py_DECREF(x); |
| 1075 |
} |
| 1076 |
mz = z->ob_type->tp_as_number; |
| 1077 |
if (mz != NULL && NEW_STYLE_NUMBER(z)) { |
| 1078 |
slotz = NB_TERNOP(mz, op_slot); |
| 1079 |
if (slotz == slotv || slotz == slotw) |
| 1080 |
slotz = NULL; |
| 1081 |
if (slotz) { |
| 1082 |
x = slotz(v, w, z); |
| 1083 |
if (x != Py_NotImplemented) |
| 1084 |
return x; |
| 1085 |
Py_DECREF(x); |
| 1086 |
} |
| 1087 |
} |
| 1088 |
|
| 1089 |
if (!NEW_STYLE_NUMBER(v) || !NEW_STYLE_NUMBER(w) || |
| 1090 |
(z != Py_None && !NEW_STYLE_NUMBER(z))) { |
| 1091 |
|
| 1092 |
PyObject *v1, *z1, *w2, *z2; |
| 1093 |
int c; |
| 1094 |
|
| 1095 |
c = PyNumber_Coerce(&v, &w); |
| 1096 |
if (c != 0) |
| 1097 |
goto error3; |
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
if (z == Py_None) { |
| 1102 |
if (v->ob_type->tp_as_number) { |
| 1103 |
slotz = NB_TERNOP(v->ob_type->tp_as_number, |
| 1104 |
op_slot); |
| 1105 |
if (slotz) |
| 1106 |
x = slotz(v, w, z); |
| 1107 |
else |
| 1108 |
c = -1; |
| 1109 |
} |
| 1110 |
else |
| 1111 |
c = -1; |
| 1112 |
goto error2; |
| 1113 |
} |
| 1114 |
v1 = v; |
| 1115 |
z1 = z; |
| 1116 |
c = PyNumber_Coerce(&v1, &z1); |
| 1117 |
if (c != 0) |
| 1118 |
goto error2; |
| 1119 |
w2 = w; |
| 1120 |
z2 = z1; |
| 1121 |
c = PyNumber_Coerce(&w2, &z2); |
| 1122 |
if (c != 0) |
| 1123 |
goto error1; |
| 1124 |
|
| 1125 |
if (v1->ob_type->tp_as_number != NULL) { |
| 1126 |
slotv = NB_TERNOP(v1->ob_type->tp_as_number, |
| 1127 |
op_slot); |
| 1128 |
if (slotv) |
| 1129 |
x = slotv(v1, w2, z2); |
| 1130 |
else |
| 1131 |
c = -1; |
| 1132 |
} |
| 1133 |
else |
| 1134 |
c = -1; |
| 1135 |
|
| 1136 |
Py_DECREF(w2); |
| 1137 |
Py_DECREF(z2); |
| 1138 |
error1: |
| 1139 |
Py_DECREF(v1); |
| 1140 |
Py_DECREF(z1); |
| 1141 |
error2: |
| 1142 |
Py_DECREF(v); |
| 1143 |
Py_DECREF(w); |
| 1144 |
error3: |
| 1145 |
if (c >= 0) |
| 1146 |
return x; |
| 1147 |
} |
| 1148 |
|
| 1149 |
if (z == Py_None) |
| 1150 |
PyErr_Format( |
| 1151 |
PyExc_TypeError, |
| 1152 |
"unsupported operand type(s) for ** or pow(): " |
| 1153 |
"'%.100s' and '%.100s'", |
| 1154 |
v->ob_type->tp_name, |
| 1155 |
w->ob_type->tp_name); |
| 1156 |
else |
| 1157 |
PyErr_Format( |
| 1158 |
PyExc_TypeError, |
| 1159 |
"unsupported operand type(s) for pow(): " |
| 1160 |
"'%.100s', '%.100s', '%.100s'", |
| 1161 |
v->ob_type->tp_name, |
| 1162 |
w->ob_type->tp_name, |
| 1163 |
z->ob_type->tp_name); |
| 1164 |
return NULL; |
| 1165 |
} |
| 1166 |
|
| 1167 |
#define BINARY_FUNC(func, op, op_name) \ |
| 1168 |
PyObject * \ |
| 1169 |
func(PyObject *v, PyObject *w) { \ |
| 1170 |
return binary_op(v, w, NB_SLOT(op), op_name); \ |
| 1171 |
} |
| 1172 |
|
| 1173 |
BINARY_FUNC(PyNumber_Or, nb_or, "|") |
| 1174 |
BINARY_FUNC(PyNumber_Xor, nb_xor, "^") |
| 1175 |
BINARY_FUNC(PyNumber_And, nb_and, "&") |
| 1176 |
BINARY_FUNC(PyNumber_Lshift, nb_lshift, "<<") |
| 1177 |
BINARY_FUNC(PyNumber_Rshift, nb_rshift, ">>") |
| 1178 |
BINARY_FUNC(PyNumber_Subtract, nb_subtract, "-") |
| 1179 |
BINARY_FUNC(PyNumber_Divide, nb_divide, "/") |
| 1180 |
BINARY_FUNC(PyNumber_Divmod, nb_divmod, "divmod()") |
| 1181 |
|
| 1182 |
PyObject * |
| 1183 |
PyNumber_Add(PyObject *v, PyObject *w) |
| 1184 |
{ |
| 1185 |
PyObject *result = binary_op1(v, w, NB_SLOT(nb_add)); |
| 1186 |
if (result == Py_NotImplemented) { |
| 1187 |
PySequenceMethods *m = v->ob_type->tp_as_sequence; |
| 1188 |
Py_DECREF(result); |
| 1189 |
if (m && m->sq_concat) { |
| 1190 |
return (*m->sq_concat)(v, w); |
| 1191 |
} |
| 1192 |
result = binop_type_error(v, w, "+"); |
| 1193 |
} |
| 1194 |
return result; |
| 1195 |
} |
| 1196 |
|
| 1197 |
static PyObject * |
| 1198 |
sequence_repeat(ssizeargfunc repeatfunc, PyObject *seq, PyObject *n) |
| 1199 |
{ |
| 1200 |
Py_ssize_t count; |
| 1201 |
if (PyIndex_Check(n)) { |
| 1202 |
count = PyNumber_AsSsize_t(n, PyExc_OverflowError); |
| 1203 |
if (count == -1 && PyErr_Occurred()) |
| 1204 |
return NULL; |
| 1205 |
} |
| 1206 |
else { |
| 1207 |
return type_error("can't multiply sequence by " |
| 1208 |
"non-int of type '%.200s'", n); |
| 1209 |
} |
| 1210 |
return (*repeatfunc)(seq, count); |
| 1211 |
} |
| 1212 |
|
| 1213 |
PyObject * |
| 1214 |
PyNumber_Multiply(PyObject *v, PyObject *w) |
| 1215 |
{ |
| 1216 |
PyObject *result = binary_op1(v, w, NB_SLOT(nb_multiply)); |
| 1217 |
if (result == Py_NotImplemented) { |
| 1218 |
PySequenceMethods *mv = v->ob_type->tp_as_sequence; |
| 1219 |
PySequenceMethods *mw = w->ob_type->tp_as_sequence; |
| 1220 |
Py_DECREF(result); |
| 1221 |
if (mv && mv->sq_repeat) { |
| 1222 |
return sequence_repeat(mv->sq_repeat, v, w); |
| 1223 |
} |
| 1224 |
else if (mw && mw->sq_repeat) { |
| 1225 |
return sequence_repeat(mw->sq_repeat, w, v); |
| 1226 |
} |
| 1227 |
result = binop_type_error(v, w, "*"); |
| 1228 |
} |
| 1229 |
return result; |
| 1230 |
} |
| 1231 |
|
| 1232 |
PyObject * |
| 1233 |
PyNumber_FloorDivide(PyObject *v, PyObject *w) |
| 1234 |
{ |
| 1235 |
|
| 1236 |
return binary_op(v, w, NB_SLOT(nb_floor_divide), "//"); |
| 1237 |
} |
| 1238 |
|
| 1239 |
PyObject * |
| 1240 |
PyNumber_TrueDivide(PyObject *v, PyObject *w) |
| 1241 |
{ |
| 1242 |
|
| 1243 |
return binary_op(v, w, NB_SLOT(nb_true_divide), "/"); |
| 1244 |
} |
| 1245 |
|
| 1246 |
PyObject * |
| 1247 |
PyNumber_Remainder(PyObject *v, PyObject *w) |
| 1248 |
{ |
| 1249 |
return binary_op(v, w, NB_SLOT(nb_remainder), "%"); |
| 1250 |
} |
| 1251 |
|
| 1252 |
PyObject * |
| 1253 |
PyNumber_Power(PyObject *v, PyObject *w, PyObject *z) |
| 1254 |
{ |
| 1255 |
return ternary_op(v, w, z, NB_SLOT(nb_power), "** or pow()"); |
| 1256 |
} |
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
|
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
|
| 1269 |
|
| 1270 |
|
| 1271 |
|
| 1272 |
|
| 1273 |
|
| 1274 |
#define HASINPLACE(t) \ |
| 1275 |
PyType_HasFeature((t)->ob_type, Py_TPFLAGS_HAVE_INPLACEOPS) |
| 1276 |
|
| 1277 |
static PyObject * |
| 1278 |
binary_iop1(PyObject *v, PyObject *w, const int iop_slot, const int op_slot) |
| 1279 |
{ |
| 1280 |
PyNumberMethods *mv = v->ob_type->tp_as_number; |
| 1281 |
if (mv != NULL && HASINPLACE(v)) { |
| 1282 |
binaryfunc slot = NB_BINOP(mv, iop_slot); |
| 1283 |
if (slot) { |
| 1284 |
PyObject *x = (slot)(v, w); |
| 1285 |
if (x != Py_NotImplemented) { |
| 1286 |
return x; |
| 1287 |
} |
| 1288 |
Py_DECREF(x); |
| 1289 |
} |
| 1290 |
} |
| 1291 |
return binary_op1(v, w, op_slot); |
| 1292 |
} |
| 1293 |
|
| 1294 |
static PyObject * |
| 1295 |
binary_iop(PyObject *v, PyObject *w, const int iop_slot, const int op_slot, |
| 1296 |
const char *op_name) |
| 1297 |
{ |
| 1298 |
PyObject *result = binary_iop1(v, w, iop_slot, op_slot); |
| 1299 |
if (result == Py_NotImplemented) { |
| 1300 |
Py_DECREF(result); |
| 1301 |
return binop_type_error(v, w, op_name); |
| 1302 |
} |
| 1303 |
return result; |
| 1304 |
} |
| 1305 |
|
| 1306 |
#define INPLACE_BINOP(func, iop, op, op_name) \ |
| 1307 |
PyObject * \ |
| 1308 |
func(PyObject *v, PyObject *w) { \ |
| 1309 |
return binary_iop(v, w, NB_SLOT(iop), NB_SLOT(op), op_name); \ |
| 1310 |
} |
| 1311 |
|
| 1312 |
INPLACE_BINOP(PyNumber_InPlaceOr, nb_inplace_or, nb_or, "|=") |
| 1313 |
INPLACE_BINOP(PyNumber_InPlaceXor, nb_inplace_xor, nb_xor, "^=") |
| 1314 |
INPLACE_BINOP(PyNumber_InPlaceAnd, nb_inplace_and, nb_and, "&=") |
| 1315 |
INPLACE_BINOP(PyNumber_InPlaceLshift, nb_inplace_lshift, nb_lshift, "<<=") |
| 1316 |
INPLACE_BINOP(PyNumber_InPlaceRshift, nb_inplace_rshift, nb_rshift, ">>=") |
| 1317 |
INPLACE_BINOP(PyNumber_InPlaceSubtract, nb_inplace_subtract, nb_subtract, "-=") |
| 1318 |
INPLACE_BINOP(PyNumber_InPlaceDivide, nb_inplace_divide, nb_divide, "/=") |
| 1319 |
|
| 1320 |
PyObject * |
| 1321 |
PyNumber_InPlaceFloorDivide(PyObject *v, PyObject *w) |
| 1322 |
{ |
| 1323 |
|
| 1324 |
return binary_iop(v, w, NB_SLOT(nb_inplace_floor_divide), |
| 1325 |
NB_SLOT(nb_floor_divide), "//="); |
| 1326 |
} |
| 1327 |
|
| 1328 |
PyObject * |
| 1329 |
PyNumber_InPlaceTrueDivide(PyObject *v, PyObject *w) |
| 1330 |
{ |
| 1331 |
|
| 1332 |
return binary_iop(v, w, NB_SLOT(nb_inplace_true_divide), |
| 1333 |
NB_SLOT(nb_true_divide), "/="); |
| 1334 |
} |
| 1335 |
|
| 1336 |
PyObject * |
| 1337 |
PyNumber_InPlaceAdd(PyObject *v, PyObject *w) |
| 1338 |
{ |
| 1339 |
PyObject *result = binary_iop1(v, w, NB_SLOT(nb_inplace_add), |
| 1340 |
NB_SLOT(nb_add)); |
| 1341 |
if (result == Py_NotImplemented) { |
| 1342 |
PySequenceMethods *m = v->ob_type->tp_as_sequence; |
| 1343 |
Py_DECREF(result); |
| 1344 |
if (m != NULL) { |
| 1345 |
binaryfunc f = NULL; |
| 1346 |
if (HASINPLACE(v)) |
| 1347 |
f = m->sq_inplace_concat; |
| 1348 |
if (f == NULL) |
| 1349 |
f = m->sq_concat; |
| 1350 |
if (f != NULL) |
| 1351 |
return (*f)(v, w); |
| 1352 |
} |
| 1353 |
result = binop_type_error(v, w, "+="); |
| 1354 |
} |
| 1355 |
return result; |
| 1356 |
} |
| 1357 |
|
| 1358 |
PyObject * |
| 1359 |
PyNumber_InPlaceMultiply(PyObject *v, PyObject *w) |
| 1360 |
{ |
| 1361 |
PyObject *result = binary_iop1(v, w, NB_SLOT(nb_inplace_multiply), |
| 1362 |
NB_SLOT(nb_multiply)); |
| 1363 |
if (result == Py_NotImplemented) { |
| 1364 |
ssizeargfunc f = NULL; |
| 1365 |
PySequenceMethods *mv = v->ob_type->tp_as_sequence; |
| 1366 |
PySequenceMethods *mw = w->ob_type->tp_as_sequence; |
| 1367 |
Py_DECREF(result); |
| 1368 |
if (mv != NULL) { |
| 1369 |
if (HASINPLACE(v)) |
| 1370 |
f = mv->sq_inplace_repeat; |
| 1371 |
if (f == NULL) |
| 1372 |
f = mv->sq_repeat; |
| 1373 |
if (f != NULL) |
| 1374 |
return sequence_repeat(f, v, w); |
| 1375 |
} |
| 1376 |
else if (mw != NULL) { |
| 1377 |
|
| 1378 |
|
| 1379 |
|
| 1380 |
if (mw->sq_repeat) |
| 1381 |
return sequence_repeat(mw->sq_repeat, w, v); |
| 1382 |
} |
| 1383 |
result = binop_type_error(v, w, "*="); |
| 1384 |
} |
| 1385 |
return result; |
| 1386 |
} |
| 1387 |
|
| 1388 |
PyObject * |
| 1389 |
PyNumber_InPlaceRemainder(PyObject *v, PyObject *w) |
| 1390 |
{ |
| 1391 |
return binary_iop(v, w, NB_SLOT(nb_inplace_remainder), |
| 1392 |
NB_SLOT(nb_remainder), "%="); |
| 1393 |
} |
| 1394 |
|
| 1395 |
PyObject * |
| 1396 |
PyNumber_InPlacePower(PyObject *v, PyObject *w, PyObject *z) |
| 1397 |
{ |
| 1398 |
if (HASINPLACE(v) && v->ob_type->tp_as_number && |
| 1399 |
v->ob_type->tp_as_number->nb_inplace_power != NULL) { |
| 1400 |
return ternary_op(v, w, z, NB_SLOT(nb_inplace_power), "**="); |
| 1401 |
} |
| 1402 |
else { |
| 1403 |
return ternary_op(v, w, z, NB_SLOT(nb_power), "**="); |
| 1404 |
} |
| 1405 |
} |
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
PyObject * |
| 1411 |
PyNumber_Negative(PyObject *o) |
| 1412 |
{ |
| 1413 |
PyNumberMethods *m; |
| 1414 |
|
| 1415 |
if (o == NULL) |
| 1416 |
return null_error(); |
| 1417 |
m = o->ob_type->tp_as_number; |
| 1418 |
if (m && m->nb_negative) |
| 1419 |
return (*m->nb_negative)(o); |
| 1420 |
|
| 1421 |
return type_error("bad operand type for unary -: '%.200s'", o); |
| 1422 |
} |
| 1423 |
|
| 1424 |
PyObject * |
| 1425 |
PyNumber_Positive(PyObject *o) |
| 1426 |
{ |
| 1427 |
PyNumberMethods *m; |
| 1428 |
|
| 1429 |
if (o == NULL) |
| 1430 |
return null_error(); |
| 1431 |
m = o->ob_type->tp_as_number; |
| 1432 |
if (m && m->nb_positive) |
| 1433 |
return (*m->nb_positive)(o); |
| 1434 |
|
| 1435 |
return type_error("bad operand type for unary +: '%.200s'", o); |
| 1436 |
} |
| 1437 |
|
| 1438 |
PyObject * |
| 1439 |
PyNumber_Invert(PyObject *o) |
| 1440 |
{ |
| 1441 |
PyNumberMethods *m; |
| 1442 |
|
| 1443 |
if (o == NULL) |
| 1444 |
return null_error(); |
| 1445 |
m = o->ob_type->tp_as_number; |
| 1446 |
if (m && m->nb_invert) |
| 1447 |
return (*m->nb_invert)(o); |
| 1448 |
|
| 1449 |
return type_error("bad operand type for unary ~: '%.200s'", o); |
| 1450 |
} |
| 1451 |
|
| 1452 |
PyObject * |
| 1453 |
PyNumber_Absolute(PyObject *o) |
| 1454 |
{ |
| 1455 |
PyNumberMethods *m; |
| 1456 |
|
| 1457 |
if (o == NULL) |
| 1458 |
return null_error(); |
| 1459 |
m = o->ob_type->tp_as_number; |
| 1460 |
if (m && m->nb_absolute) |
| 1461 |
return m->nb_absolute(o); |
| 1462 |
|
| 1463 |
return type_error("bad operand type for abs(): '%.200s'", o); |
| 1464 |
} |
| 1465 |
|
| 1466 |
|
| 1467 |
static PyObject * |
| 1468 |
int_from_string(const char *s, Py_ssize_t len) |
| 1469 |
{ |
| 1470 |
char *end; |
| 1471 |
PyObject *x; |
| 1472 |
|
| 1473 |
x = PyInt_FromString((char*)s, &end, 10); |
| 1474 |
if (x == NULL) |
| 1475 |
return NULL; |
| 1476 |
if (end != s + len) { |
| 1477 |
PyErr_SetString(PyExc_ValueError, |
| 1478 |
"null byte in argument for int()"); |
| 1479 |
Py_DECREF(x); |
| 1480 |
return NULL; |
| 1481 |
} |
| 1482 |
return x; |
| 1483 |
} |
| 1484 |
|
| 1485 |
|
| 1486 |
|
| 1487 |
|
| 1488 |
|
| 1489 |
PyObject * |
| 1490 |
PyNumber_Index(PyObject *item) |
| 1491 |
{ |
| 1492 |
PyObject *result = NULL; |
| 1493 |
if (item == NULL) |
| 1494 |
return null_error(); |
| 1495 |
if (_PyAnyInt_Check(item)) { |
| 1496 |
Py_INCREF(item); |
| 1497 |
return item; |
| 1498 |
} |
| 1499 |
if (PyIndex_Check(item)) { |
| 1500 |
result = item->ob_type->tp_as_number->nb_index(item); |
| 1501 |
if (result && !_PyAnyInt_Check(result)) { |
| 1502 |
PyErr_Format(PyExc_TypeError, |
| 1503 |
"__index__ returned non-(int,long) " \ |
| 1504 |
"(type %.200s)", |
| 1505 |
result->ob_type->tp_name); |
| 1506 |
Py_DECREF(result); |
| 1507 |
return NULL; |
| 1508 |
} |
| 1509 |
} |
| 1510 |
else { |
| 1511 |
PyErr_Format(PyExc_TypeError, |
| 1512 |
"'%.200s' object cannot be interpreted " |
| 1513 |
"as an index", item->ob_type->tp_name); |
| 1514 |
} |
| 1515 |
return result; |
| 1516 |
} |
| 1517 |
|
| 1518 |
|
| 1519 |
|
| 1520 |
Py_ssize_t |
| 1521 |
PyNumber_AsSsize_t(PyObject *item, PyObject *err) |
| 1522 |
{ |
| 1523 |
Py_ssize_t result; |
| 1524 |
PyObject *runerr; |
| 1525 |
PyObject *value = PyNumber_Index(item); |
| 1526 |
if (value == NULL) |
| 1527 |
return -1; |
| 1528 |
|
| 1529 |
|
| 1530 |
result = PyInt_AsSsize_t(value); |
| 1531 |
if (result != -1 || !(runerr = PyErr_Occurred())) |
| 1532 |
goto finish; |
| 1533 |
|
| 1534 |
|
| 1535 |
if (!PyErr_GivenExceptionMatches(runerr, PyExc_OverflowError)) |
| 1536 |
goto finish; |
| 1537 |
|
| 1538 |
PyErr_Clear(); |
| 1539 |
|
| 1540 |
|
| 1541 |
|
| 1542 |
if (!err) { |
| 1543 |
assert(PyLong_Check(value)); |
| 1544 |
|
| 1545 |
|
| 1546 |
|
| 1547 |
if (_PyLong_Sign(value) < 0) |
| 1548 |
result = PY_SSIZE_T_MIN; |
| 1549 |
else |
| 1550 |
result = PY_SSIZE_T_MAX; |
| 1551 |
} |
| 1552 |
else { |
| 1553 |
|
| 1554 |
PyErr_Format(err, |
| 1555 |
"cannot fit '%.200s' into an index-sized integer", |
| 1556 |
item->ob_type->tp_name); |
| 1557 |
} |
| 1558 |
|
| 1559 |
finish: |
| 1560 |
Py_DECREF(value); |
| 1561 |
return result; |
| 1562 |
} |
| 1563 |
|
| 1564 |
|
| 1565 |
PyObject * |
| 1566 |
_PyNumber_ConvertIntegralToInt(PyObject *integral, const char* error_format) |
| 1567 |
{ |
| 1568 |
const char *type_name; |
| 1569 |
static PyObject *int_name = NULL; |
| 1570 |
if (int_name == NULL) { |
| 1571 |
int_name = PyString_InternFromString("__int__"); |
| 1572 |
if (int_name == NULL) |
| 1573 |
return NULL; |
| 1574 |
} |
| 1575 |
|
| 1576 |
if (integral && !_PyAnyInt_Check(integral)) { |
| 1577 |
|
| 1578 |
|
| 1579 |
PyObject *int_func = PyObject_GetAttr(integral, int_name); |
| 1580 |
if (int_func == NULL) { |
| 1581 |
PyErr_Clear(); |
| 1582 |
goto non_integral_error; |
| 1583 |
} |
| 1584 |
Py_DECREF(integral); |
| 1585 |
integral = PyEval_CallObject(int_func, NULL); |
| 1586 |
Py_DECREF(int_func); |
| 1587 |
if (integral && !_PyAnyInt_Check(integral)) { |
| 1588 |
goto non_integral_error; |
| 1589 |
} |
| 1590 |
} |
| 1591 |
return integral; |
| 1592 |
|
| 1593 |
non_integral_error: |
| 1594 |
if (PyInstance_Check(integral)) { |
| 1595 |
type_name = PyString_AS_STRING(((PyInstanceObject *)integral) |
| 1596 |
->in_class->cl_name); |
| 1597 |
} |
| 1598 |
else { |
| 1599 |
type_name = integral->ob_type->tp_name; |
| 1600 |
} |
| 1601 |
PyErr_Format(PyExc_TypeError, error_format, type_name); |
| 1602 |
Py_DECREF(integral); |
| 1603 |
return NULL; |
| 1604 |
} |
| 1605 |
|
| 1606 |
|
| 1607 |
PyObject * |
| 1608 |
PyNumber_Int(PyObject *o) |
| 1609 |
{ |
| 1610 |
PyNumberMethods *m; |
| 1611 |
static PyObject *trunc_name = NULL; |
| 1612 |
PyObject *trunc_func; |
| 1613 |
const char *buffer; |
| 1614 |
Py_ssize_t buffer_len; |
| 1615 |
|
| 1616 |
if (trunc_name == NULL) { |
| 1617 |
trunc_name = PyString_InternFromString("__trunc__"); |
| 1618 |
if (trunc_name == NULL) |
| 1619 |
return NULL; |
| 1620 |
} |
| 1621 |
|
| 1622 |
if (o == NULL) |
| 1623 |
return null_error(); |
| 1624 |
if (PyInt_CheckExact(o)) { |
| 1625 |
Py_INCREF(o); |
| 1626 |
return o; |
| 1627 |
} |
| 1628 |
m = o->ob_type->tp_as_number; |
| 1629 |
if (m && m->nb_int) { |
| 1630 |
|
| 1631 |
PyObject *res = m->nb_int(o); |
| 1632 |
if (res && !_PyAnyInt_Check(res)) { |
| 1633 |
PyErr_Format(PyExc_TypeError, |
| 1634 |
"__int__ returned non-int (type %.200s)", |
| 1635 |
res->ob_type->tp_name); |
| 1636 |
Py_DECREF(res); |
| 1637 |
return NULL; |
| 1638 |
} |
| 1639 |
return res; |
| 1640 |
} |
| 1641 |
if (PyInt_Check(o)) { |
| 1642 |
PyIntObject *io = (PyIntObject*)o; |
| 1643 |
return PyInt_FromLong(io->ob_ival); |
| 1644 |
} |
| 1645 |
trunc_func = PyObject_GetAttr(o, trunc_name); |
| 1646 |
if (trunc_func) { |
| 1647 |
PyObject *truncated = PyEval_CallObject(trunc_func, NULL); |
| 1648 |
Py_DECREF(trunc_func); |
| 1649 |
|
| 1650 |
|
| 1651 |
return _PyNumber_ConvertIntegralToInt( |
| 1652 |
truncated, |
| 1653 |
"__trunc__ returned non-Integral (type %.200s)"); |
| 1654 |
} |
| 1655 |
PyErr_Clear(); |
| 1656 |
|
| 1657 |
if (PyString_Check(o)) |
| 1658 |
return int_from_string(PyString_AS_STRING(o), |
| 1659 |
PyString_GET_SIZE(o)); |
| 1660 |
#ifdef Py_USING_UNICODE |
| 1661 |
if (PyUnicode_Check(o)) |
| 1662 |
return PyInt_FromUnicode(PyUnicode_AS_UNICODE(o), |
| 1663 |
PyUnicode_GET_SIZE(o), |
| 1664 |
10); |
| 1665 |
#endif |
| 1666 |
if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len)) { |
| 1667 |
PyObject *result, *str; |
| 1668 |
|
| 1669 |
|
| 1670 |
str = PyString_FromStringAndSize((const char *)buffer, buffer_len); |
| 1671 |
if (str == NULL) |
| 1672 |
return NULL; |
| 1673 |
result = int_from_string(PyString_AS_STRING(str), buffer_len); |
| 1674 |
Py_DECREF(str); |
| 1675 |
return result; |
| 1676 |
} |
| 1677 |
|
| 1678 |
return type_error("int() argument must be a string or a " |
| 1679 |
"number, not '%.200s'", o); |
| 1680 |
} |
| 1681 |
|
| 1682 |
|
| 1683 |
static PyObject * |
| 1684 |
long_from_string(const char *s, Py_ssize_t len) |
| 1685 |
{ |
| 1686 |
char *end; |
| 1687 |
PyObject *x; |
| 1688 |
|
| 1689 |
x = PyLong_FromString((char*)s, &end, 10); |
| 1690 |
if (x == NULL) |
| 1691 |
return NULL; |
| 1692 |
if (end != s + len) { |
| 1693 |
PyErr_SetString(PyExc_ValueError, |
| 1694 |
"null byte in argument for long()"); |
| 1695 |
Py_DECREF(x); |
| 1696 |
return NULL; |
| 1697 |
} |
| 1698 |
return x; |
| 1699 |
} |
| 1700 |
|
| 1701 |
PyObject * |
| 1702 |
PyNumber_Long(PyObject *o) |
| 1703 |
{ |
| 1704 |
PyNumberMethods *m; |
| 1705 |
static PyObject *trunc_name = NULL; |
| 1706 |
PyObject *trunc_func; |
| 1707 |
const char *buffer; |
| 1708 |
Py_ssize_t buffer_len; |
| 1709 |
|
| 1710 |
if (trunc_name == NULL) { |
| 1711 |
trunc_name = PyString_InternFromString("__trunc__"); |
| 1712 |
if (trunc_name == NULL) |
| 1713 |
return NULL; |
| 1714 |
} |
| 1715 |
|
| 1716 |
if (o == NULL) |
| 1717 |
return null_error(); |
| 1718 |
m = o->ob_type->tp_as_number; |
| 1719 |
if (m && m->nb_long) { |
| 1720 |
|
| 1721 |
PyObject *res = m->nb_long(o); |
| 1722 |
if (res == NULL) |
| 1723 |
return NULL; |
| 1724 |
if (PyInt_Check(res)) { |
| 1725 |
long value = PyInt_AS_LONG(res); |
| 1726 |
Py_DECREF(res); |
| 1727 |
return PyLong_FromLong(value); |
| 1728 |
} |
| 1729 |
else if (!PyLong_Check(res)) { |
| 1730 |
PyErr_Format(PyExc_TypeError, |
| 1731 |
"__long__ returned non-long (type %.200s)", |
| 1732 |
res->ob_type->tp_name); |
| 1733 |
Py_DECREF(res); |
| 1734 |
return NULL; |
| 1735 |
} |
| 1736 |
return res; |
| 1737 |
} |
| 1738 |
if (PyLong_Check(o)) |
| 1739 |
return _PyLong_Copy((PyLongObject *)o); |
| 1740 |
trunc_func = PyObject_GetAttr(o, trunc_name); |
| 1741 |
if (trunc_func) { |
| 1742 |
PyObject *truncated = PyEval_CallObject(trunc_func, NULL); |
| 1743 |
PyObject *int_instance; |
| 1744 |
Py_DECREF(trunc_func); |
| 1745 |
|
| 1746 |
|
| 1747 |
int_instance = _PyNumber_ConvertIntegralToInt( |
| 1748 |
truncated, |
| 1749 |
"__trunc__ returned non-Integral (type %.200s)"); |
| 1750 |
if (int_instance && PyInt_Check(int_instance)) { |
| 1751 |
|
| 1752 |
long value = PyInt_AS_LONG(int_instance); |
| 1753 |
Py_DECREF(int_instance); |
| 1754 |
return PyLong_FromLong(value); |
| 1755 |
} |
| 1756 |
return int_instance; |
| 1757 |
} |
| 1758 |
PyErr_Clear(); |
| 1759 |
|
| 1760 |
if (PyString_Check(o)) |
| 1761 |
|
| 1762 |
|
| 1763 |
|
| 1764 |
|
| 1765 |
return long_from_string(PyString_AS_STRING(o), |
| 1766 |
PyString_GET_SIZE(o)); |
| 1767 |
#ifdef Py_USING_UNICODE |
| 1768 |
if (PyUnicode_Check(o)) |
| 1769 |
|
| 1770 |
return PyLong_FromUnicode(PyUnicode_AS_UNICODE(o), |
| 1771 |
PyUnicode_GET_SIZE(o), |
| 1772 |
10); |
| 1773 |
#endif |
| 1774 |
if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len)) { |
| 1775 |
PyObject *result, *str; |
| 1776 |
|
| 1777 |
|
| 1778 |
str = PyString_FromStringAndSize((const char *)buffer, buffer_len); |
| 1779 |
if (str == NULL) |
| 1780 |
return NULL; |
| 1781 |
result = long_from_string(PyString_AS_STRING(str), buffer_len); |
| 1782 |
Py_DECREF(str); |
| 1783 |
return result; |
| 1784 |
} |
| 1785 |
return type_error("long() argument must be a string or a " |
| 1786 |
"number, not '%.200s'", o); |
| 1787 |
} |
| 1788 |
|
| 1789 |
PyObject * |
| 1790 |
PyNumber_Float(PyObject *o) |
| 1791 |
{ |
| 1792 |
PyNumberMethods *m; |
| 1793 |
|
| 1794 |
if (o == NULL) |
| 1795 |
return null_error(); |
| 1796 |
m = o->ob_type->tp_as_number; |
| 1797 |
if (m && m->nb_float) { |
| 1798 |
PyObject *res = m->nb_float(o); |
| 1799 |
if (res && !PyFloat_Check(res)) { |
| 1800 |
PyErr_Format(PyExc_TypeError, |
| 1801 |
"__float__ returned non-float (type %.200s)", |
| 1802 |
res->ob_type->tp_name); |
| 1803 |
Py_DECREF(res); |
| 1804 |
return NULL; |
| 1805 |
} |
| 1806 |
return res; |
| 1807 |
} |
| 1808 |
if (PyFloat_Check(o)) { |
| 1809 |
PyFloatObject *po = (PyFloatObject *)o; |
| 1810 |
return PyFloat_FromDouble(po->ob_fval); |
| 1811 |
} |
| 1812 |
return PyFloat_FromString(o, NULL); |
| 1813 |
} |
| 1814 |
|
| 1815 |
PyObject * |
| 1816 |
PyNumber_ToBase(PyObject *n, int base) |
| 1817 |
{ |
| 1818 |
PyObject *res = NULL; |
| 1819 |
PyObject *index = PyNumber_Index(n); |
| 1820 |
|
| 1821 |
if (!index) |
| 1822 |
return NULL; |
| 1823 |
if (PyLong_Check(index)) |
| 1824 |
res = _PyLong_Format(index, base, 0, 1); |
| 1825 |
else if (PyInt_Check(index)) |
| 1826 |
res = _PyInt_Format((PyIntObject*)index, base, 1); |
| 1827 |
else |
| 1828 |
|
| 1829 |
|
| 1830 |
|
| 1831 |
PyErr_SetString(PyExc_ValueError, "PyNumber_ToBase: index not " |
| 1832 |
"int or long"); |
| 1833 |
Py_DECREF(index); |
| 1834 |
return res; |
| 1835 |
} |
| 1836 |
|
| 1837 |
|
| 1838 |
|
| 1839 |
|
| 1840 |
int |
| 1841 |
PySequence_Check(PyObject *s) |
| 1842 |
{ |
| 1843 |
if (s == NULL) |
| 1844 |
return 0; |
| 1845 |
if (PyInstance_Check(s)) |
| 1846 |
return PyObject_HasAttrString(s, "__getitem__"); |
| 1847 |
if (PyDict_Check(s)) |
| 1848 |
return 0; |
| 1849 |
return s->ob_type->tp_as_sequence && |
| 1850 |
s->ob_type->tp_as_sequence->sq_item != NULL; |
| 1851 |
} |
| 1852 |
|
| 1853 |
Py_ssize_t |
| 1854 |
PySequence_Size(PyObject *s) |
| 1855 |
{ |
| 1856 |
PySequenceMethods *m; |
| 1857 |
|
| 1858 |
if (s == NULL) { |
| 1859 |
null_error(); |
| 1860 |
return -1; |
| 1861 |
} |
| 1862 |
|
| 1863 |
m = s->ob_type->tp_as_sequence; |
| 1864 |
if (m && m->sq_length) |
| 1865 |
return m->sq_length(s); |
| 1866 |
|
| 1867 |
type_error("object of type '%.200s' has no len()", s); |
| 1868 |
return -1; |
| 1869 |
} |
| 1870 |
|
| 1871 |
#undef PySequence_Length |
| 1872 |
Py_ssize_t |
| 1873 |
PySequence_Length(PyObject *s) |
| 1874 |
{ |
| 1875 |
return PySequence_Size(s); |
| 1876 |
} |
| 1877 |
#define PySequence_Length PySequence_Size |
| 1878 |
|
| 1879 |
PyObject * |
| 1880 |
PySequence_Concat(PyObject *s, PyObject *o) |
| 1881 |
{ |
| 1882 |
PySequenceMethods *m; |
| 1883 |
|
| 1884 |
if (s == NULL || o == NULL) |
| 1885 |
return null_error(); |
| 1886 |
|
| 1887 |
m = s->ob_type->tp_as_sequence; |
| 1888 |
if (m && m->sq_concat) |
| 1889 |
return m->sq_concat(s, o); |
| 1890 |
|
| 1891 |
|
| 1892 |
|
| 1893 |
|
| 1894 |
if (PySequence_Check(s) && PySequence_Check(o)) { |
| 1895 |
PyObject *result = binary_op1(s, o, NB_SLOT(nb_add)); |
| 1896 |
if (result != Py_NotImplemented) |
| 1897 |
return result; |
| 1898 |
Py_DECREF(result); |
| 1899 |
} |
| 1900 |
return type_error("'%.200s' object can't be concatenated", s); |
| 1901 |
} |
| 1902 |
|
| 1903 |
PyObject * |
| 1904 |
PySequence_Repeat(PyObject *o, Py_ssize_t count) |
| 1905 |
{ |
| 1906 |
PySequenceMethods *m; |
| 1907 |
|
| 1908 |
if (o == NULL) |
| 1909 |
return null_error(); |
| 1910 |
|
| 1911 |
m = o->ob_type->tp_as_sequence; |
| 1912 |
if (m && m->sq_repeat) |
| 1913 |
return m->sq_repeat(o, count); |
| 1914 |
|
| 1915 |
|
| 1916 |
|
| 1917 |
|
| 1918 |
if (PySequence_Check(o)) { |
| 1919 |
PyObject *n, *result; |
| 1920 |
n = PyInt_FromSsize_t(count); |
| 1921 |
if (n == NULL) |
| 1922 |
return NULL; |
| 1923 |
result = binary_op1(o, n, NB_SLOT(nb_multiply)); |
| 1924 |
Py_DECREF(n); |
| 1925 |
if (result != Py_NotImplemented) |
| 1926 |
return result; |
| 1927 |
Py_DECREF(result); |
| 1928 |
} |
| 1929 |
return type_error("'%.200s' object can't be repeated", o); |
| 1930 |
} |
| 1931 |
|
| 1932 |
PyObject * |
| 1933 |
PySequence_InPlaceConcat(PyObject *s, PyObject *o) |
| 1934 |
{ |
| 1935 |
PySequenceMethods *m; |
| 1936 |
|
| 1937 |
if (s == NULL || o == NULL) |
| 1938 |
return null_error(); |
| 1939 |
|
| 1940 |
m = s->ob_type->tp_as_sequence; |
| 1941 |
if (m && HASINPLACE(s) && m->sq_inplace_concat) |
| 1942 |
return m->sq_inplace_concat(s, o); |
| 1943 |
if (m && m->sq_concat) |
| 1944 |
return m->sq_concat(s, o); |
| 1945 |
|
| 1946 |
if (PySequence_Check(s) && PySequence_Check(o)) { |
| 1947 |
PyObject *result = binary_iop1(s, o, NB_SLOT(nb_inplace_add), |
| 1948 |
NB_SLOT(nb_add)); |
| 1949 |
if (result != Py_NotImplemented) |
| 1950 |
return result; |
| 1951 |
Py_DECREF(result); |
| 1952 |
} |
| 1953 |
return type_error("'%.200s' object can't be concatenated", s); |
| 1954 |
} |
| 1955 |
|
| 1956 |
PyObject * |
| 1957 |
PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count) |
| 1958 |
{ |
| 1959 |
PySequenceMethods *m; |
| 1960 |
|
| 1961 |
if (o == NULL) |
| 1962 |
return null_error(); |
| 1963 |
|
| 1964 |
m = o->ob_type->tp_as_sequence; |
| 1965 |
if (m && HASINPLACE(o) && m->sq_inplace_repeat) |
| 1966 |
return m->sq_inplace_repeat(o, count); |
| 1967 |
if (m && m->sq_repeat) |
| 1968 |
return m->sq_repeat(o, count); |
| 1969 |
|
| 1970 |
if (PySequence_Check(o)) { |
| 1971 |
PyObject *n, *result; |
| 1972 |
n = PyInt_FromSsize_t(count); |
| 1973 |
if (n == NULL) |
| 1974 |
return NULL; |
| 1975 |
result = binary_iop1(o, n, NB_SLOT(nb_inplace_multiply), |
| 1976 |
NB_SLOT(nb_multiply)); |
| 1977 |
Py_DECREF(n); |
| 1978 |
if (result != Py_NotImplemented) |
| 1979 |
return result; |
| 1980 |
Py_DECREF(result); |
| 1981 |
} |
| 1982 |
return type_error("'%.200s' object can't be repeated", o); |
| 1983 |
} |
| 1984 |
|
| 1985 |
PyObject * |
| 1986 |
PySequence_GetItem(PyObject *s, Py_ssize_t i) |
| 1987 |
{ |
| 1988 |
PySequenceMethods *m; |
| 1989 |
|
| 1990 |
if (s == NULL) |
| 1991 |
return null_error(); |
| 1992 |
|
| 1993 |
m = s->ob_type->tp_as_sequence; |
| 1994 |
if (m && m->sq_item) { |
| 1995 |
if (i < 0) { |
| 1996 |
if (m->sq_length) { |
| 1997 |
Py_ssize_t l = (*m->sq_length)(s); |
| 1998 |
if (l < 0) |
| 1999 |
return NULL; |
| 2000 |
i += l; |
| 2001 |
} |
| 2002 |
} |
| 2003 |
return m->sq_item(s, i); |
| 2004 |
} |
| 2005 |
|
| 2006 |
return type_error("'%.200s' object does not support indexing", s); |
| 2007 |
} |
| 2008 |
|
| 2009 |
PyObject * |
| 2010 |
PySequence_GetSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2) |
| 2011 |
{ |
| 2012 |
PySequenceMethods *m; |
| 2013 |
PyMappingMethods *mp; |
| 2014 |
|
| 2015 |
if (!s) return null_error(); |
| 2016 |
|
| 2017 |
m = s->ob_type->tp_as_sequence; |
| 2018 |
if (m && m->sq_slice) { |
| 2019 |
if (i1 < 0 || i2 < 0) { |
| 2020 |
if (m->sq_length) { |
| 2021 |
Py_ssize_t l = (*m->sq_length)(s); |
| 2022 |
if (l < 0) |
| 2023 |
return NULL; |
| 2024 |
if (i1 < 0) |
| 2025 |
i1 += l; |
| 2026 |
if (i2 < 0) |
| 2027 |
i2 += l; |
| 2028 |
} |
| 2029 |
} |
| 2030 |
return m->sq_slice(s, i1, i2); |
| 2031 |
} else if ((mp = s->ob_type->tp_as_mapping) && mp->mp_subscript) { |
| 2032 |
PyObject *res; |
| 2033 |
PyObject *slice = _PySlice_FromIndices(i1, i2); |
| 2034 |
if (!slice) |
| 2035 |
return NULL; |
| 2036 |
res = mp->mp_subscript(s, slice); |
| 2037 |
Py_DECREF(slice); |
| 2038 |
return res; |
| 2039 |
} |
| 2040 |
|
| 2041 |
return type_error("'%.200s' object is unsliceable", s); |
| 2042 |
} |
| 2043 |
|
| 2044 |
int |
| 2045 |
PySequence_SetItem(PyObject *s, Py_ssize_t i, PyObject *o) |
| 2046 |
{ |
| 2047 |
PySequenceMethods *m; |
| 2048 |
|
| 2049 |
if (s == NULL) { |
| 2050 |
null_error(); |
| 2051 |
return -1; |
| 2052 |
} |
| 2053 |
|
| 2054 |
m = s->ob_type->tp_as_sequence; |
| 2055 |
if (m && m->sq_ass_item) { |
| 2056 |
if (i < 0) { |
| 2057 |
if (m->sq_length) { |
| 2058 |
Py_ssize_t l = (*m->sq_length)(s); |
| 2059 |
if (l < 0) |
| 2060 |
return -1; |
| 2061 |
i += l; |
| 2062 |
} |
| 2063 |
} |
| 2064 |
return m->sq_ass_item(s, i, o); |
| 2065 |
} |
| 2066 |
|
| 2067 |
type_error("'%.200s' object does not support item assignment", s); |
| 2068 |
return -1; |
| 2069 |
} |
| 2070 |
|
| 2071 |
int |
| 2072 |
PySequence_DelItem(PyObject *s, Py_ssize_t i) |
| 2073 |
{ |
| 2074 |
PySequenceMethods *m; |
| 2075 |
|
| 2076 |
if (s == NULL) { |
| 2077 |
null_error(); |
| 2078 |
return -1; |
| 2079 |
} |
| 2080 |
|
| 2081 |
m = s->ob_type->tp_as_sequence; |
| 2082 |
if (m && m->sq_ass_item) { |
| 2083 |
if (i < 0) { |
| 2084 |
if (m->sq_length) { |
| 2085 |
Py_ssize_t l = (*m->sq_length)(s); |
| 2086 |
if (l < 0) |
| 2087 |
return -1; |
| 2088 |
i += l; |
| 2089 |
} |
| 2090 |
} |
| 2091 |
return m->sq_ass_item(s, i, (PyObject *)NULL); |
| 2092 |
} |
| 2093 |
|
| 2094 |
type_error("'%.200s' object doesn't support item deletion", s); |
| 2095 |
return -1; |
| 2096 |
} |
| 2097 |
|
| 2098 |
int |
| 2099 |
PySequence_SetSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2, PyObject *o) |
| 2100 |
{ |
| 2101 |
PySequenceMethods *m; |
| 2102 |
PyMappingMethods *mp; |
| 2103 |
|
| 2104 |
if (s == NULL) { |
| 2105 |
null_error(); |
| 2106 |
return -1; |
| 2107 |
} |
| 2108 |
|
| 2109 |
m = s->ob_type->tp_as_sequence; |
| 2110 |
if (m && m->sq_ass_slice) { |
| 2111 |
if (i1 < 0 || i2 < 0) { |
| 2112 |
if (m->sq_length) { |
| 2113 |
Py_ssize_t l = (*m->sq_length)(s); |
| 2114 |
if (l < 0) |
| 2115 |
return -1; |
| 2116 |
if (i1 < 0) |
| 2117 |
i1 += l; |
| 2118 |
if (i2 < 0) |
| 2119 |
i2 += l; |
| 2120 |
} |
| 2121 |
} |
| 2122 |
return m->sq_ass_slice(s, i1, i2, o); |
| 2123 |
} else if ((mp = s->ob_type->tp_as_mapping) && mp->mp_ass_subscript) { |
| 2124 |
int res; |
| 2125 |
PyObject *slice = _PySlice_FromIndices(i1, i2); |
| 2126 |
if (!slice) |
| 2127 |
return -1; |
| 2128 |
res = mp->mp_ass_subscript(s, slice, o); |
| 2129 |
Py_DECREF(slice); |
| 2130 |
return res; |
| 2131 |
} |
| 2132 |
|
| 2133 |
type_error("'%.200s' object doesn't support slice assignment", s); |
| 2134 |
return -1; |
| 2135 |
} |
| 2136 |
|
| 2137 |
int |
| 2138 |
PySequence_DelSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2) |
| 2139 |
{ |
| 2140 |
PySequenceMethods *m; |
| 2141 |
|
| 2142 |
if (s == NULL) { |
| 2143 |
null_error(); |
| 2144 |
return -1; |
| 2145 |
} |
| 2146 |
|
| 2147 |
m = s->ob_type->tp_as_sequence; |
| 2148 |
if (m && m->sq_ass_slice) { |
| 2149 |
if (i1 < 0 || i2 < 0) { |
| 2150 |
if (m->sq_length) { |
| 2151 |
Py_ssize_t l = (*m->sq_length)(s); |
| 2152 |
if (l < 0) |
| 2153 |
return -1; |
| 2154 |
if (i1 < 0) |
| 2155 |
i1 += l; |
| 2156 |
if (i2 < 0) |
| 2157 |
i2 += l; |
| 2158 |
} |
| 2159 |
} |
| 2160 |
return m->sq_ass_slice(s, i1, i2, (PyObject *)NULL); |
| 2161 |
} |
| 2162 |
type_error("'%.200s' object doesn't support slice deletion", s); |
| 2163 |
return -1; |
| 2164 |
} |
| 2165 |
|
| 2166 |
PyObject * |
| 2167 |
PySequence_Tuple(PyObject *v) |
| 2168 |
{ |
| 2169 |
PyObject *it; |
| 2170 |
Py_ssize_t n; |
| 2171 |
PyObject *result = NULL; |
| 2172 |
Py_ssize_t j; |
| 2173 |
|
| 2174 |
if (v == NULL) |
| 2175 |
return null_error(); |
| 2176 |
|
| 2177 |
|
| 2178 |
if (PyTuple_CheckExact(v)) { |
| 2179 |
|
| 2180 |
|
| 2181 |
|
| 2182 |
|
| 2183 |
Py_INCREF(v); |
| 2184 |
return v; |
| 2185 |
} |
| 2186 |
if (PyList_CheckExact(v)) |
| 2187 |
return PyList_AsTuple(v); |
| 2188 |
|
| 2189 |
|
| 2190 |
it = PyObject_GetIter(v); |
| 2191 |
if (it == NULL) |
| 2192 |
return NULL; |
| 2193 |
|
| 2194 |
|
| 2195 |
n = _PyObject_LengthHint(v, 10); |
| 2196 |
if (n == -1) |
| 2197 |
goto Fail; |
| 2198 |
result = PyTuple_New(n); |
| 2199 |
if (result == NULL) |
| 2200 |
goto Fail; |
| 2201 |
|
| 2202 |
|
| 2203 |
for (j = 0; ; ++j) { |
| 2204 |
PyObject *item = PyIter_Next(it); |
| 2205 |
if (item == NULL) { |
| 2206 |
if (PyErr_Occurred()) |
| 2207 |
goto Fail; |
| 2208 |
break; |
| 2209 |
} |
| 2210 |
if (j >= n) { |
| 2211 |
size_t newn = (size_t)n; |
| 2212 |
|
| 2213 |
|
| 2214 |
|
| 2215 |
|
| 2216 |
|
| 2217 |
|
| 2218 |
newn += 10u; |
| 2219 |
newn += newn >> 2; |
| 2220 |
if (newn > PY_SSIZE_T_MAX) { |
| 2221 |
|
| 2222 |
PyErr_NoMemory(); |
| 2223 |
Py_DECREF(item); |
| 2224 |
goto Fail; |
| 2225 |
} |
| 2226 |
n = (Py_ssize_t)newn; |
| 2227 |
if (_PyTuple_Resize(&result, n) != 0) { |
| 2228 |
Py_DECREF(item); |
| 2229 |
goto Fail; |
| 2230 |
} |
| 2231 |
} |
| 2232 |
PyTuple_SET_ITEM(result, j, item); |
| 2233 |
} |
| 2234 |
|
| 2235 |
|
| 2236 |
if (j < n && |
| 2237 |
_PyTuple_Resize(&result, j) != 0) |
| 2238 |
goto Fail; |
| 2239 |
|
| 2240 |
Py_DECREF(it); |
| 2241 |
return result; |
| 2242 |
|
| 2243 |
Fail: |
| 2244 |
Py_XDECREF(result); |
| 2245 |
Py_DECREF(it); |
| 2246 |
return NULL; |
| 2247 |
} |
| 2248 |
|
| 2249 |
PyObject * |
| 2250 |
PySequence_List(PyObject *v) |
| 2251 |
{ |
| 2252 |
PyObject *result; |
| 2253 |
PyObject *rv; |
| 2254 |
|
| 2255 |
if (v == NULL) |
| 2256 |
return null_error(); |
| 2257 |
|
| 2258 |
result = PyList_New(0); |
| 2259 |
if (result == NULL) |
| 2260 |
return NULL; |
| 2261 |
|
| 2262 |
rv = _PyList_Extend((PyListObject *)result, v); |
| 2263 |
if (rv == NULL) { |
| 2264 |
Py_DECREF(result); |
| 2265 |
return NULL; |
| 2266 |
} |
| 2267 |
Py_DECREF(rv); |
| 2268 |
return result; |
| 2269 |
} |
| 2270 |
|
| 2271 |
PyObject * |
| 2272 |
PySequence_Fast(PyObject *v, const char *m) |
| 2273 |
{ |
| 2274 |
PyObject *it; |
| 2275 |
|
| 2276 |
if (v == NULL) |
| 2277 |
return null_error(); |
| 2278 |
|
| 2279 |
if (PyList_CheckExact(v) || PyTuple_CheckExact(v)) { |
| 2280 |
Py_INCREF(v); |
| 2281 |
return v; |
| 2282 |
} |
| 2283 |
|
| 2284 |
it = PyObject_GetIter(v); |
| 2285 |
if (it == NULL) { |
| 2286 |
if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 2287 |
PyErr_SetString(PyExc_TypeError, m); |
| 2288 |
return NULL; |
| 2289 |
} |
| 2290 |
|
| 2291 |
v = PySequence_List(it); |
| 2292 |
Py_DECREF(it); |
| 2293 |
|
| 2294 |
return v; |
| 2295 |
} |
| 2296 |
|
| 2297 |
|
| 2298 |
|
| 2299 |
|
| 2300 |
|
| 2301 |
|
| 2302 |
|
| 2303 |
Py_ssize_t |
| 2304 |
_PySequence_IterSearch(PyObject *seq, PyObject *obj, int operation) |
| 2305 |
{ |
| 2306 |
Py_ssize_t n; |
| 2307 |
int wrapped; |
| 2308 |
PyObject *it; |
| 2309 |
|
| 2310 |
if (seq == NULL || obj == NULL) { |
| 2311 |
null_error(); |
| 2312 |
return -1; |
| 2313 |
} |
| 2314 |
|
| 2315 |
it = PyObject_GetIter(seq); |
| 2316 |
if (it == NULL) { |
| 2317 |
type_error("argument of type '%.200s' is not iterable", seq); |
| 2318 |
return -1; |
| 2319 |
} |
| 2320 |
|
| 2321 |
n = wrapped = 0; |
| 2322 |
for (;;) { |
| 2323 |
int cmp; |
| 2324 |
PyObject *item = PyIter_Next(it); |
| 2325 |
if (item == NULL) { |
| 2326 |
if (PyErr_Occurred()) |
| 2327 |
goto Fail; |
| 2328 |
break; |
| 2329 |
} |
| 2330 |
|
| 2331 |
cmp = PyObject_RichCompareBool(obj, item, Py_EQ); |
| 2332 |
Py_DECREF(item); |
| 2333 |
if (cmp < 0) |
| 2334 |
goto Fail; |
| 2335 |
if (cmp > 0) { |
| 2336 |
switch (operation) { |
| 2337 |
case PY_ITERSEARCH_COUNT: |
| 2338 |
if (n == PY_SSIZE_T_MAX) { |
| 2339 |
PyErr_SetString(PyExc_OverflowError, |
| 2340 |
"count exceeds C integer size"); |
| 2341 |
goto Fail; |
| 2342 |
} |
| 2343 |
++n; |
| 2344 |
break; |
| 2345 |
|
| 2346 |
case PY_ITERSEARCH_INDEX: |
| 2347 |
if (wrapped) { |
| 2348 |
PyErr_SetString(PyExc_OverflowError, |
| 2349 |
"index exceeds C integer size"); |
| 2350 |
goto Fail; |
| 2351 |
} |
| 2352 |
goto Done; |
| 2353 |
|
| 2354 |
case PY_ITERSEARCH_CONTAINS: |
| 2355 |
n = 1; |
| 2356 |
goto Done; |
| 2357 |
|
| 2358 |
default: |
| 2359 |
assert(!"unknown operation"); |
| 2360 |
} |
| 2361 |
} |
| 2362 |
|
| 2363 |
if (operation == PY_ITERSEARCH_INDEX) { |
| 2364 |
if (n == PY_SSIZE_T_MAX) |
| 2365 |
wrapped = 1; |
| 2366 |
++n; |
| 2367 |
} |
| 2368 |
} |
| 2369 |
|
| 2370 |
if (operation != PY_ITERSEARCH_INDEX) |
| 2371 |
goto Done; |
| 2372 |
|
| 2373 |
PyErr_SetString(PyExc_ValueError, |
| 2374 |
"sequence.index(x): x not in sequence"); |
| 2375 |
|
| 2376 |
Fail: |
| 2377 |
n = -1; |
| 2378 |
|
| 2379 |
Done: |
| 2380 |
Py_DECREF(it); |
| 2381 |
return n; |
| 2382 |
|
| 2383 |
} |
| 2384 |
|
| 2385 |
|
| 2386 |
Py_ssize_t |
| 2387 |
PySequence_Count(PyObject *s, PyObject *o) |
| 2388 |
{ |
| 2389 |
return _PySequence_IterSearch(s, o, PY_ITERSEARCH_COUNT); |
| 2390 |
} |
| 2391 |
|
| 2392 |
|
| 2393 |
|
| 2394 |
|
| 2395 |
int |
| 2396 |
PySequence_Contains(PyObject *seq, PyObject *ob) |
| 2397 |
{ |
| 2398 |
Py_ssize_t result; |
| 2399 |
if (PyType_HasFeature(seq->ob_type, Py_TPFLAGS_HAVE_SEQUENCE_IN)) { |
| 2400 |
PySequenceMethods *sqm = seq->ob_type->tp_as_sequence; |
| 2401 |
if (sqm != NULL && sqm->sq_contains != NULL) |
| 2402 |
return (*sqm->sq_contains)(seq, ob); |
| 2403 |
} |
| 2404 |
result = _PySequence_IterSearch(seq, ob, PY_ITERSEARCH_CONTAINS); |
| 2405 |
return Py_SAFE_DOWNCAST(result, Py_ssize_t, int); |
| 2406 |
} |
| 2407 |
|
| 2408 |
|
| 2409 |
#undef PySequence_In |
| 2410 |
int |
| 2411 |
PySequence_In(PyObject *w, PyObject *v) |
| 2412 |
{ |
| 2413 |
return PySequence_Contains(w, v); |
| 2414 |
} |
| 2415 |
|
| 2416 |
Py_ssize_t |
| 2417 |
PySequence_Index(PyObject *s, PyObject *o) |
| 2418 |
{ |
| 2419 |
return _PySequence_IterSearch(s, o, PY_ITERSEARCH_INDEX); |
| 2420 |
} |
| 2421 |
|
| 2422 |
|
| 2423 |
|
| 2424 |
int |
| 2425 |
PyMapping_Check(PyObject *o) |
| 2426 |
{ |
| 2427 |
if (o && PyInstance_Check(o)) |
| 2428 |
return PyObject_HasAttrString(o, "__getitem__"); |
| 2429 |
|
| 2430 |
return o && o->ob_type->tp_as_mapping && |
| 2431 |
o->ob_type->tp_as_mapping->mp_subscript && |
| 2432 |
!(o->ob_type->tp_as_sequence && |
| 2433 |
o->ob_type->tp_as_sequence->sq_slice); |
| 2434 |
} |
| 2435 |
|
| 2436 |
Py_ssize_t |
| 2437 |
PyMapping_Size(PyObject *o) |
| 2438 |
{ |
| 2439 |
PyMappingMethods *m; |
| 2440 |
|
| 2441 |
if (o == NULL) { |
| 2442 |
null_error(); |
| 2443 |
return -1; |
| 2444 |
} |
| 2445 |
|
| 2446 |
m = o->ob_type->tp_as_mapping; |
| 2447 |
if (m && m->mp_length) |
| 2448 |
return m->mp_length(o); |
| 2449 |
|
| 2450 |
type_error("object of type '%.200s' has no len()", o); |
| 2451 |
return -1; |
| 2452 |
} |
| 2453 |
|
| 2454 |
#undef PyMapping_Length |
| 2455 |
Py_ssize_t |
| 2456 |
PyMapping_Length(PyObject *o) |
| 2457 |
{ |
| 2458 |
return PyMapping_Size(o); |
| 2459 |
} |
| 2460 |
#define PyMapping_Length PyMapping_Size |
| 2461 |
|
| 2462 |
PyObject * |
| 2463 |
PyMapping_GetItemString(PyObject *o, char *key) |
| 2464 |
{ |
| 2465 |
PyObject *okey, *r; |
| 2466 |
|
| 2467 |
if (key == NULL) |
| 2468 |
return null_error(); |
| 2469 |
|
| 2470 |
okey = PyString_FromString(key); |
| 2471 |
if (okey == NULL) |
| 2472 |
return NULL; |
| 2473 |
r = PyObject_GetItem(o, okey); |
| 2474 |
Py_DECREF(okey); |
| 2475 |
return r; |
| 2476 |
} |
| 2477 |
|
| 2478 |
int |
| 2479 |
PyMapping_SetItemString(PyObject *o, char *key, PyObject *value) |
| 2480 |
{ |
| 2481 |
PyObject *okey; |
| 2482 |
int r; |
| 2483 |
|
| 2484 |
if (key == NULL) { |
| 2485 |
null_error(); |
| 2486 |
return -1; |
| 2487 |
} |
| 2488 |
|
| 2489 |
okey = PyString_FromString(key); |
| 2490 |
if (okey == NULL) |
| 2491 |
return -1; |
| 2492 |
r = PyObject_SetItem(o, okey, value); |
| 2493 |
Py_DECREF(okey); |
| 2494 |
return r; |
| 2495 |
} |
| 2496 |
|
| 2497 |
int |
| 2498 |
PyMapping_HasKeyString(PyObject *o, char *key) |
| 2499 |
{ |
| 2500 |
PyObject *v; |
| 2501 |
|
| 2502 |
v = PyMapping_GetItemString(o, key); |
| 2503 |
if (v) { |
| 2504 |
Py_DECREF(v); |
| 2505 |
return 1; |
| 2506 |
} |
| 2507 |
PyErr_Clear(); |
| 2508 |
return 0; |
| 2509 |
} |
| 2510 |
|
| 2511 |
int |
| 2512 |
PyMapping_HasKey(PyObject *o, PyObject *key) |
| 2513 |
{ |
| 2514 |
PyObject *v; |
| 2515 |
|
| 2516 |
v = PyObject_GetItem(o, key); |
| 2517 |
if (v) { |
| 2518 |
Py_DECREF(v); |
| 2519 |
return 1; |
| 2520 |
} |
| 2521 |
PyErr_Clear(); |
| 2522 |
return 0; |
| 2523 |
} |
| 2524 |
|
| 2525 |
|
| 2526 |
|
| 2527 |
|
| 2528 |
|
| 2529 |
PyObject * |
| 2530 |
PyObject_CallObject(PyObject *o, PyObject *a) |
| 2531 |
{ |
| 2532 |
return PyEval_CallObjectWithKeywords(o, a, NULL); |
| 2533 |
} |
| 2534 |
|
| 2535 |
PyObject * |
| 2536 |
PyObject_Call(PyObject *func, PyObject *arg, PyObject *kw) |
| 2537 |
{ |
| 2538 |
ternaryfunc call; |
| 2539 |
|
| 2540 |
if ((call = func->ob_type->tp_call) != NULL) { |
| 2541 |
PyObject *result; |
| 2542 |
if (Py_EnterRecursiveCall(" while calling a Python object")) |
| 2543 |
return NULL; |
| 2544 |
result = (*call)(func, arg, kw); |
| 2545 |
Py_LeaveRecursiveCall(); |
| 2546 |
if (result == NULL && !PyErr_Occurred()) |
| 2547 |
PyErr_SetString( |
| 2548 |
PyExc_SystemError, |
| 2549 |
"NULL result without error in PyObject_Call"); |
| 2550 |
return result; |
| 2551 |
} |
| 2552 |
PyErr_Format(PyExc_TypeError, "'%.200s' object is not callable", |
| 2553 |
func->ob_type->tp_name); |
| 2554 |
return NULL; |
| 2555 |
} |
| 2556 |
|
| 2557 |
static PyObject* |
| 2558 |
call_function_tail(PyObject *callable, PyObject *args) |
| 2559 |
{ |
| 2560 |
PyObject *retval; |
| 2561 |
|
| 2562 |
if (args == NULL) |
| 2563 |
return NULL; |
| 2564 |
|
| 2565 |
if (!PyTuple_Check(args)) { |
| 2566 |
PyObject *a; |
| 2567 |
|
| 2568 |
a = PyTuple_New(1); |
| 2569 |
if (a == NULL) { |
| 2570 |
Py_DECREF(args); |
| 2571 |
return NULL; |
| 2572 |
} |
| 2573 |
PyTuple_SET_ITEM(a, 0, args); |
| 2574 |
args = a; |
| 2575 |
} |
| 2576 |
retval = PyObject_Call(callable, args, NULL); |
| 2577 |
|
| 2578 |
Py_DECREF(args); |
| 2579 |
|
| 2580 |
return retval; |
| 2581 |
} |
| 2582 |
|
| 2583 |
PyObject * |
| 2584 |
PyObject_CallFunction(PyObject *callable, char *format, ...) |
| 2585 |
{ |
| 2586 |
va_list va; |
| 2587 |
PyObject *args; |
| 2588 |
|
| 2589 |
if (callable == NULL) |
| 2590 |
return null_error(); |
| 2591 |
|
| 2592 |
if (format && *format) { |
| 2593 |
va_start(va, format); |
| 2594 |
args = Py_VaBuildValue(format, va); |
| 2595 |
va_end(va); |
| 2596 |
} |
| 2597 |
else |
| 2598 |
args = PyTuple_New(0); |
| 2599 |
|
| 2600 |
return call_function_tail(callable, args); |
| 2601 |
} |
| 2602 |
|
| 2603 |
PyObject * |
| 2604 |
_PyObject_CallFunction_SizeT(PyObject *callable, char *format, ...) |
| 2605 |
{ |
| 2606 |
va_list va; |
| 2607 |
PyObject *args; |
| 2608 |
|
| 2609 |
if (callable == NULL) |
| 2610 |
return null_error(); |
| 2611 |
|
| 2612 |
if (format && *format) { |
| 2613 |
va_start(va, format); |
| 2614 |
args = _Py_VaBuildValue_SizeT(format, va); |
| 2615 |
va_end(va); |
| 2616 |
} |
| 2617 |
else |
| 2618 |
args = PyTuple_New(0); |
| 2619 |
|
| 2620 |
return call_function_tail(callable, args); |
| 2621 |
} |
| 2622 |
|
| 2623 |
PyObject * |
| 2624 |
PyObject_CallMethod(PyObject *o, char *name, char *format, ...) |
| 2625 |
{ |
| 2626 |
va_list va; |
| 2627 |
PyObject *args; |
| 2628 |
PyObject *func = NULL; |
| 2629 |
PyObject *retval = NULL; |
| 2630 |
|
| 2631 |
if (o == NULL || name == NULL) |
| 2632 |
return null_error(); |
| 2633 |
|
| 2634 |
func = PyObject_GetAttrString(o, name); |
| 2635 |
if (func == NULL) |
| 2636 |
return NULL; |
| 2637 |
|
| 2638 |
if (!PyCallable_Check(func)) { |
| 2639 |
type_error("attribute of type '%.200s' is not callable", func); |
| 2640 |
goto exit; |
| 2641 |
} |
| 2642 |
|
| 2643 |
if (format && *format) { |
| 2644 |
va_start(va, format); |
| 2645 |
args = Py_VaBuildValue(format, va); |
| 2646 |
va_end(va); |
| 2647 |
} |
| 2648 |
else |
| 2649 |
args = PyTuple_New(0); |
| 2650 |
|
| 2651 |
retval = call_function_tail(func, args); |
| 2652 |
|
| 2653 |
exit: |
| 2654 |
|
| 2655 |
Py_XDECREF(func); |
| 2656 |
|
| 2657 |
return retval; |
| 2658 |
} |
| 2659 |
|
| 2660 |
PyObject * |
| 2661 |
_PyObject_CallMethod_SizeT(PyObject *o, char *name, char *format, ...) |
| 2662 |
{ |
| 2663 |
va_list va; |
| 2664 |
PyObject *args; |
| 2665 |
PyObject *func = NULL; |
| 2666 |
PyObject *retval = NULL; |
| 2667 |
|
| 2668 |
if (o == NULL || name == NULL) |
| 2669 |
return null_error(); |
| 2670 |
|
| 2671 |
func = PyObject_GetAttrString(o, name); |
| 2672 |
if (func == NULL) |
| 2673 |
return NULL; |
| 2674 |
|
| 2675 |
if (!PyCallable_Check(func)) { |
| 2676 |
type_error("attribute of type '%.200s' is not callable", func); |
| 2677 |
goto exit; |
| 2678 |
} |
| 2679 |
|
| 2680 |
if (format && *format) { |
| 2681 |
va_start(va, format); |
| 2682 |
args = _Py_VaBuildValue_SizeT(format, va); |
| 2683 |
va_end(va); |
| 2684 |
} |
| 2685 |
else |
| 2686 |
args = PyTuple_New(0); |
| 2687 |
|
| 2688 |
retval = call_function_tail(func, args); |
| 2689 |
|
| 2690 |
exit: |
| 2691 |
|
| 2692 |
Py_XDECREF(func); |
| 2693 |
|
| 2694 |
return retval; |
| 2695 |
} |
| 2696 |
|
| 2697 |
|
| 2698 |
static PyObject * |
| 2699 |
objargs_mktuple(va_list va) |
| 2700 |
{ |
| 2701 |
int i, n = 0; |
| 2702 |
va_list countva; |
| 2703 |
PyObject *result, *tmp; |
| 2704 |
|
| 2705 |
#ifdef VA_LIST_IS_ARRAY |
| 2706 |
memcpy(countva, va, sizeof(va_list)); |
| 2707 |
#else |
| 2708 |
#ifdef __va_copy |
| 2709 |
__va_copy(countva, va); |
| 2710 |
#else |
| 2711 |
countva = va; |
| 2712 |
#endif |
| 2713 |
#endif |
| 2714 |
|
| 2715 |
while (((PyObject *)va_arg(countva, PyObject *)) != NULL) |
| 2716 |
++n; |
| 2717 |
result = PyTuple_New(n); |
| 2718 |
if (result != NULL && n > 0) { |
| 2719 |
for (i = 0; i < n; ++i) { |
| 2720 |
tmp = (PyObject *)va_arg(va, PyObject *); |
| 2721 |
PyTuple_SET_ITEM(result, i, tmp); |
| 2722 |
Py_INCREF(tmp); |
| 2723 |
} |
| 2724 |
} |
| 2725 |
return result; |
| 2726 |
} |
| 2727 |
|
| 2728 |
PyObject * |
| 2729 |
PyObject_CallMethodObjArgs(PyObject *callable, PyObject *name, ...) |
| 2730 |
{ |
| 2731 |
PyObject *args, *tmp; |
| 2732 |
va_list vargs; |
| 2733 |
|
| 2734 |
if (callable == NULL || name == NULL) |
| 2735 |
return null_error(); |
| 2736 |
|
| 2737 |
callable = PyObject_GetAttr(callable, name); |
| 2738 |
if (callable == NULL) |
| 2739 |
return NULL; |
| 2740 |
|
| 2741 |
|
| 2742 |
va_start(vargs, name); |
| 2743 |
args = objargs_mktuple(vargs); |
| 2744 |
va_end(vargs); |
| 2745 |
if (args == NULL) { |
| 2746 |
Py_DECREF(callable); |
| 2747 |
return NULL; |
| 2748 |
} |
| 2749 |
tmp = PyObject_Call(callable, args, NULL); |
| 2750 |
Py_DECREF(args); |
| 2751 |
Py_DECREF(callable); |
| 2752 |
|
| 2753 |
return tmp; |
| 2754 |
} |
| 2755 |
|
| 2756 |
PyObject * |
| 2757 |
PyObject_CallFunctionObjArgs(PyObject *callable, ...) |
| 2758 |
{ |
| 2759 |
PyObject *args, *tmp; |
| 2760 |
va_list vargs; |
| 2761 |
|
| 2762 |
if (callable == NULL) |
| 2763 |
return null_error(); |
| 2764 |
|
| 2765 |
|
| 2766 |
va_start(vargs, callable); |
| 2767 |
args = objargs_mktuple(vargs); |
| 2768 |
va_end(vargs); |
| 2769 |
if (args == NULL) |
| 2770 |
return NULL; |
| 2771 |
tmp = PyObject_Call(callable, args, NULL); |
| 2772 |
Py_DECREF(args); |
| 2773 |
|
| 2774 |
return tmp; |
| 2775 |
} |
| 2776 |
|
| 2777 |
|
| 2778 |
|
| 2779 |
|
| 2780 |
|
| 2781 |
|
| 2782 |
|
| 2783 |
|
| 2784 |
|
| 2785 |
|
| 2786 |
|
| 2787 |
|
| 2788 |
|
| 2789 |
|
| 2790 |
|
| 2791 |
|
| 2792 |
|
| 2793 |
|
| 2794 |
|
| 2795 |
|
| 2796 |
|
| 2797 |
|
| 2798 |
|
| 2799 |
|
| 2800 |
|
| 2801 |
|
| 2802 |
|
| 2803 |
|
| 2804 |
|
| 2805 |
|
| 2806 |
static PyObject * |
| 2807 |
abstract_get_bases(PyObject *cls) |
| 2808 |
{ |
| 2809 |
static PyObject *__bases__ = NULL; |
| 2810 |
PyObject *bases; |
| 2811 |
|
| 2812 |
if (__bases__ == NULL) { |
| 2813 |
__bases__ = PyString_InternFromString("__bases__"); |
| 2814 |
if (__bases__ == NULL) |
| 2815 |
return NULL; |
| 2816 |
} |
| 2817 |
bases = PyObject_GetAttr(cls, __bases__); |
| 2818 |
if (bases == NULL) { |
| 2819 |
if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 2820 |
PyErr_Clear(); |
| 2821 |
return NULL; |
| 2822 |
} |
| 2823 |
if (!PyTuple_Check(bases)) { |
| 2824 |
Py_DECREF(bases); |
| 2825 |
return NULL; |
| 2826 |
} |
| 2827 |
return bases; |
| 2828 |
} |
| 2829 |
|
| 2830 |
|
| 2831 |
static int |
| 2832 |
abstract_issubclass(PyObject *derived, PyObject *cls) |
| 2833 |
{ |
| 2834 |
PyObject *bases = NULL; |
| 2835 |
Py_ssize_t i, n; |
| 2836 |
int r = 0; |
| 2837 |
|
| 2838 |
while (1) { |
| 2839 |
if (derived == cls) |
| 2840 |
return 1; |
| 2841 |
bases = abstract_get_bases(derived); |
| 2842 |
if (bases == NULL) { |
| 2843 |
if (PyErr_Occurred()) |
| 2844 |
return -1; |
| 2845 |
return 0; |
| 2846 |
} |
| 2847 |
n = PyTuple_GET_SIZE(bases); |
| 2848 |
if (n == 0) { |
| 2849 |
Py_DECREF(bases); |
| 2850 |
return 0; |
| 2851 |
} |
| 2852 |
|
| 2853 |
if (n == 1) { |
| 2854 |
derived = PyTuple_GET_ITEM(bases, 0); |
| 2855 |
Py_DECREF(bases); |
| 2856 |
continue; |
| 2857 |
} |
| 2858 |
for (i = 0; i < n; i++) { |
| 2859 |
r = abstract_issubclass(PyTuple_GET_ITEM(bases, i), cls); |
| 2860 |
if (r != 0) |
| 2861 |
break; |
| 2862 |
} |
| 2863 |
Py_DECREF(bases); |
| 2864 |
return r; |
| 2865 |
} |
| 2866 |
} |
| 2867 |
|
| 2868 |
static int |
| 2869 |
check_class(PyObject *cls, const char *error) |
| 2870 |
{ |
| 2871 |
PyObject *bases = abstract_get_bases(cls); |
| 2872 |
if (bases == NULL) { |
| 2873 |
|
| 2874 |
if (!PyErr_Occurred()) |
| 2875 |
PyErr_SetString(PyExc_TypeError, error); |
| 2876 |
return 0; |
| 2877 |
} |
| 2878 |
Py_DECREF(bases); |
| 2879 |
return -1; |
| 2880 |
} |
| 2881 |
|
| 2882 |
static int |
| 2883 |
recursive_isinstance(PyObject *inst, PyObject *cls) |
| 2884 |
{ |
| 2885 |
PyObject *icls; |
| 2886 |
static PyObject *__class__ = NULL; |
| 2887 |
int retval = 0; |
| 2888 |
|
| 2889 |
if (__class__ == NULL) { |
| 2890 |
__class__ = PyString_InternFromString("__class__"); |
| 2891 |
if (__class__ == NULL) |
| 2892 |
return -1; |
| 2893 |
} |
| 2894 |
|
| 2895 |
if (PyClass_Check(cls) && PyInstance_Check(inst)) { |
| 2896 |
PyObject *inclass = |
| 2897 |
(PyObject*)((PyInstanceObject*)inst)->in_class; |
| 2898 |
retval = PyClass_IsSubclass(inclass, cls); |
| 2899 |
} |
| 2900 |
else if (PyType_Check(cls)) { |
| 2901 |
retval = PyObject_TypeCheck(inst, (PyTypeObject *)cls); |
| 2902 |
if (retval == 0) { |
| 2903 |
PyObject *c = PyObject_GetAttr(inst, __class__); |
| 2904 |
if (c == NULL) { |
| 2905 |
PyErr_Clear(); |
| 2906 |
} |
| 2907 |
else { |
| 2908 |
if (c != (PyObject *)(inst->ob_type) && |
| 2909 |
PyType_Check(c)) |
| 2910 |
retval = PyType_IsSubtype( |
| 2911 |
(PyTypeObject *)c, |
| 2912 |
(PyTypeObject *)cls); |
| 2913 |
Py_DECREF(c); |
| 2914 |
} |
| 2915 |
} |
| 2916 |
} |
| 2917 |
else { |
| 2918 |
if (!check_class(cls, |
| 2919 |
"isinstance() arg 2 must be a class, type," |
| 2920 |
" or tuple of classes and types")) |
| 2921 |
return -1; |
| 2922 |
icls = PyObject_GetAttr(inst, __class__); |
| 2923 |
if (icls == NULL) { |
| 2924 |
PyErr_Clear(); |
| 2925 |
retval = 0; |
| 2926 |
} |
| 2927 |
else { |
| 2928 |
retval = abstract_issubclass(icls, cls); |
| 2929 |
Py_DECREF(icls); |
| 2930 |
} |
| 2931 |
} |
| 2932 |
|
| 2933 |
return retval; |
| 2934 |
} |
| 2935 |
|
| 2936 |
int |
| 2937 |
PyObject_IsInstance(PyObject *inst, PyObject *cls) |
| 2938 |
{ |
| 2939 |
static PyObject *name = NULL; |
| 2940 |
|
| 2941 |
|
| 2942 |
if (Py_TYPE(inst) == (PyTypeObject *)cls) |
| 2943 |
return 1; |
| 2944 |
|
| 2945 |
if (PyTuple_Check(cls)) { |
| 2946 |
Py_ssize_t i; |
| 2947 |
Py_ssize_t n; |
| 2948 |
int r = 0; |
| 2949 |
|
| 2950 |
if (Py_EnterRecursiveCall(" in __instancecheck__")) |
| 2951 |
return -1; |
| 2952 |
n = PyTuple_GET_SIZE(cls); |
| 2953 |
for (i = 0; i < n; ++i) { |
| 2954 |
PyObject *item = PyTuple_GET_ITEM(cls, i); |
| 2955 |
r = PyObject_IsInstance(inst, item); |
| 2956 |
if (r != 0) |
| 2957 |
|
| 2958 |
break; |
| 2959 |
} |
| 2960 |
Py_LeaveRecursiveCall(); |
| 2961 |
return r; |
| 2962 |
} |
| 2963 |
|
| 2964 |
if (!(PyClass_Check(cls) || PyInstance_Check(cls))) { |
| 2965 |
PyObject *checker; |
| 2966 |
checker = _PyObject_LookupSpecial(cls, "__instancecheck__", &name); |
| 2967 |
if (checker != NULL) { |
| 2968 |
PyObject *res; |
| 2969 |
int ok = -1; |
| 2970 |
if (Py_EnterRecursiveCall(" in __instancecheck__")) { |
| 2971 |
Py_DECREF(checker); |
| 2972 |
return ok; |
| 2973 |
} |
| 2974 |
res = PyObject_CallFunctionObjArgs(checker, inst, NULL); |
| 2975 |
Py_LeaveRecursiveCall(); |
| 2976 |
Py_DECREF(checker); |
| 2977 |
if (res != NULL) { |
| 2978 |
ok = PyObject_IsTrue(res); |
| 2979 |
Py_DECREF(res); |
| 2980 |
} |
| 2981 |
return ok; |
| 2982 |
} |
| 2983 |
else if (PyErr_Occurred()) |
| 2984 |
return -1; |
| 2985 |
} |
| 2986 |
return recursive_isinstance(inst, cls); |
| 2987 |
} |
| 2988 |
|
| 2989 |
static int |
| 2990 |
recursive_issubclass(PyObject *derived, PyObject *cls) |
| 2991 |
{ |
| 2992 |
int retval; |
| 2993 |
|
| 2994 |
if (PyType_Check(cls) && PyType_Check(derived)) { |
| 2995 |
|
| 2996 |
return PyType_IsSubtype( |
| 2997 |
(PyTypeObject *)derived, (PyTypeObject *)cls); |
| 2998 |
} |
| 2999 |
if (!PyClass_Check(derived) || !PyClass_Check(cls)) { |
| 3000 |
if (!check_class(derived, |
| 3001 |
"issubclass() arg 1 must be a class")) |
| 3002 |
return -1; |
| 3003 |
|
| 3004 |
if (!check_class(cls, |
| 3005 |
"issubclass() arg 2 must be a class" |
| 3006 |
" or tuple of classes")) |
| 3007 |
return -1; |
| 3008 |
retval = abstract_issubclass(derived, cls); |
| 3009 |
} |
| 3010 |
else { |
| 3011 |
|
| 3012 |
if (!(retval = (derived == cls))) |
| 3013 |
retval = PyClass_IsSubclass(derived, cls); |
| 3014 |
} |
| 3015 |
|
| 3016 |
return retval; |
| 3017 |
} |
| 3018 |
|
| 3019 |
int |
| 3020 |
PyObject_IsSubclass(PyObject *derived, PyObject *cls) |
| 3021 |
{ |
| 3022 |
static PyObject *name = NULL; |
| 3023 |
|
| 3024 |
if (PyTuple_Check(cls)) { |
| 3025 |
Py_ssize_t i; |
| 3026 |
Py_ssize_t n; |
| 3027 |
int r = 0; |
| 3028 |
|
| 3029 |
if (Py_EnterRecursiveCall(" in __subclasscheck__")) |
| 3030 |
return -1; |
| 3031 |
n = PyTuple_GET_SIZE(cls); |
| 3032 |
for (i = 0; i < n; ++i) { |
| 3033 |
PyObject *item = PyTuple_GET_ITEM(cls, i); |
| 3034 |
r = PyObject_IsSubclass(derived, item); |
| 3035 |
if (r != 0) |
| 3036 |
|
| 3037 |
break; |
| 3038 |
} |
| 3039 |
Py_LeaveRecursiveCall(); |
| 3040 |
return r; |
| 3041 |
} |
| 3042 |
if (!(PyClass_Check(cls) || PyInstance_Check(cls))) { |
| 3043 |
PyObject *checker; |
| 3044 |
checker = _PyObject_LookupSpecial(cls, "__subclasscheck__", &name); |
| 3045 |
if (checker != NULL) { |
| 3046 |
PyObject *res; |
| 3047 |
int ok = -1; |
| 3048 |
if (Py_EnterRecursiveCall(" in __subclasscheck__")) { |
| 3049 |
Py_DECREF(checker); |
| 3050 |
return ok; |
| 3051 |
} |
| 3052 |
res = PyObject_CallFunctionObjArgs(checker, derived, NULL); |
| 3053 |
Py_LeaveRecursiveCall(); |
| 3054 |
Py_DECREF(checker); |
| 3055 |
if (res != NULL) { |
| 3056 |
ok = PyObject_IsTrue(res); |
| 3057 |
Py_DECREF(res); |
| 3058 |
} |
| 3059 |
return ok; |
| 3060 |
} |
| 3061 |
else if (PyErr_Occurred()) { |
| 3062 |
return -1; |
| 3063 |
} |
| 3064 |
} |
| 3065 |
return recursive_issubclass(derived, cls); |
| 3066 |
} |
| 3067 |
|
| 3068 |
int |
| 3069 |
_PyObject_RealIsInstance(PyObject *inst, PyObject *cls) |
| 3070 |
{ |
| 3071 |
return recursive_isinstance(inst, cls); |
| 3072 |
} |
| 3073 |
|
| 3074 |
int |
| 3075 |
_PyObject_RealIsSubclass(PyObject *derived, PyObject *cls) |
| 3076 |
{ |
| 3077 |
return recursive_issubclass(derived, cls); |
| 3078 |
} |
| 3079 |
|
| 3080 |
|
| 3081 |
PyObject * |
| 3082 |
PyObject_GetIter(PyObject *o) |
| 3083 |
{ |
| 3084 |
PyTypeObject *t = o->ob_type; |
| 3085 |
getiterfunc f = NULL; |
| 3086 |
if (PyType_HasFeature(t, Py_TPFLAGS_HAVE_ITER)) |
| 3087 |
f = t->tp_iter; |
| 3088 |
if (f == NULL) { |
| 3089 |
if (PySequence_Check(o)) |
| 3090 |
return PySeqIter_New(o); |
| 3091 |
return type_error("'%.200s' object is not iterable", o); |
| 3092 |
} |
| 3093 |
else { |
| 3094 |
PyObject *res = (*f)(o); |
| 3095 |
if (res != NULL && !PyIter_Check(res)) { |
| 3096 |
PyErr_Format(PyExc_TypeError, |
| 3097 |
"iter() returned non-iterator " |
| 3098 |
"of type '%.100s'", |
| 3099 |
res->ob_type->tp_name); |
| 3100 |
Py_DECREF(res); |
| 3101 |
res = NULL; |
| 3102 |
} |
| 3103 |
return res; |
| 3104 |
} |
| 3105 |
} |
| 3106 |
|
| 3107 |
|
| 3108 |
|
| 3109 |
|
| 3110 |
|
| 3111 |
|
| 3112 |
|
| 3113 |
|
| 3114 |
PyObject * |
| 3115 |
PyIter_Next(PyObject *iter) |
| 3116 |
{ |
| 3117 |
PyObject *result; |
| 3118 |
result = (*iter->ob_type->tp_iternext)(iter); |
| 3119 |
if (result == NULL && |
| 3120 |
PyErr_Occurred() && |
| 3121 |
PyErr_ExceptionMatches(PyExc_StopIteration)) |
| 3122 |
PyErr_Clear(); |
| 3123 |
return result; |
| 3124 |
} |