abstract.c revision 226ae6ca122f814dabdc40178c7b9656caf729c2
1/*********************************************************** 2Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, 3The Netherlands. 4 5 All Rights Reserved 6 7Permission to use, copy, modify, and distribute this software and its 8documentation for any purpose and without fee is hereby granted, 9provided that the above copyright notice appear in all copies and that 10both that copyright notice and this permission notice appear in 11supporting documentation, and that the names of Stichting Mathematisch 12Centrum or CWI or Corporation for National Research Initiatives or 13CNRI not be used in advertising or publicity pertaining to 14distribution of the software without specific, written prior 15permission. 16 17While CWI is the initial source for this software, a modified version 18is made available by the Corporation for National Research Initiatives 19(CNRI) at the Internet address ftp://ftp.python.org. 20 21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH 22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF 23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH 24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL 25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 28PERFORMANCE OF THIS SOFTWARE. 29 30******************************************************************/ 31 32/* Abstract Object Interface (many thanks to Jim Fulton) */ 33 34#include "Python.h" 35#include <ctype.h> 36 37/* Shorthands to return certain errors */ 38 39static PyObject * 40type_error(msg) 41 char *msg; 42{ 43 PyErr_SetString(PyExc_TypeError, msg); 44 return NULL; 45} 46 47static PyObject * 48null_error() 49{ 50 if (!PyErr_Occurred()) 51 PyErr_SetString(PyExc_SystemError, 52 "null argument to internal routine"); 53 return NULL; 54} 55 56/* Operations on any object */ 57 58int 59PyObject_Cmp(o1, o2, result) 60 PyObject *o1; 61 PyObject *o2; 62 int *result; 63{ 64 int r; 65 66 if (o1 == NULL || o2 == NULL) { 67 null_error(); 68 return -1; 69 } 70 r = PyObject_Compare(o1, o2); 71 if (PyErr_Occurred()) 72 return -1; 73 *result = r; 74 return 0; 75} 76 77PyObject * 78PyObject_Type(o) 79 PyObject *o; 80{ 81 PyObject *v; 82 83 if (o == NULL) 84 return null_error(); 85 v = (PyObject *)o->ob_type; 86 Py_INCREF(v); 87 return v; 88} 89 90int 91PyObject_Length(o) 92 PyObject *o; 93{ 94 PySequenceMethods *m; 95 96 if (o == NULL) { 97 null_error(); 98 return -1; 99 } 100 101 m = o->ob_type->tp_as_sequence; 102 if (m && m->sq_length) 103 return m->sq_length(o); 104 105 return PyMapping_Length(o); 106} 107 108PyObject * 109PyObject_GetItem(o, key) 110 PyObject *o; 111 PyObject *key; 112{ 113 PyMappingMethods *m; 114 115 if (o == NULL || key == NULL) 116 return null_error(); 117 118 m = o->ob_type->tp_as_mapping; 119 if (m && m->mp_subscript) 120 return m->mp_subscript(o, key); 121 122 if (o->ob_type->tp_as_sequence) { 123 if (PyInt_Check(key)) 124 return PySequence_GetItem(o, PyInt_AsLong(key)); 125 return type_error("sequence index must be integer"); 126 } 127 128 return type_error("unsubscriptable object"); 129} 130 131int 132PyObject_SetItem(o, key, value) 133 PyObject *o; 134 PyObject *key; 135 PyObject *value; 136{ 137 PyMappingMethods *m; 138 139 if (o == NULL || key == NULL || value == NULL) { 140 null_error(); 141 return -1; 142 } 143 m = o->ob_type->tp_as_mapping; 144 if (m && m->mp_ass_subscript) 145 return m->mp_ass_subscript(o, key, value); 146 147 if (o->ob_type->tp_as_sequence) { 148 if (PyInt_Check(key)) 149 return PySequence_SetItem(o, PyInt_AsLong(key), value); 150 type_error("sequence index must be integer"); 151 return -1; 152 } 153 154 type_error("object does not support item assignment"); 155 return -1; 156} 157 158int 159PyObject_DelItem(o, key) 160 PyObject *o; 161 PyObject *key; 162{ 163 PyMappingMethods *m; 164 165 if (o == NULL || key == NULL) { 166 null_error(); 167 return -1; 168 } 169 m = o->ob_type->tp_as_mapping; 170 if (m && m->mp_ass_subscript) 171 return m->mp_ass_subscript(o, key, (PyObject*)NULL); 172 173 if (o->ob_type->tp_as_sequence) { 174 if (PyInt_Check(key)) 175 return PySequence_DelItem(o, PyInt_AsLong(key)); 176 type_error("sequence index must be integer"); 177 return -1; 178 } 179 180 type_error("object does not support item deletion"); 181 return -1; 182} 183 184/* Operations on numbers */ 185 186int 187PyNumber_Check(o) 188 PyObject *o; 189{ 190 return o && o->ob_type->tp_as_number; 191} 192 193/* Binary operators */ 194 195#define BINOP(v, w, opname, ropname, thisfunc) \ 196 if (PyInstance_Check(v) || PyInstance_Check(w)) \ 197 return PyInstance_DoBinOp(v, w, opname, ropname, thisfunc) 198 199PyObject * 200PyNumber_Or(v, w) 201 PyObject *v, *w; 202{ 203 extern int PyNumber_Coerce(); 204 205 BINOP(v, w, "__or__", "__ror__", PyNumber_Or); 206 if (v->ob_type->tp_as_number != NULL) { 207 PyObject *x = NULL; 208 PyObject * (*f) Py_FPROTO((PyObject *, PyObject *)); 209 if (PyNumber_Coerce(&v, &w) != 0) 210 return NULL; 211 if ((f = v->ob_type->tp_as_number->nb_or) != NULL) 212 x = (*f)(v, w); 213 Py_DECREF(v); 214 Py_DECREF(w); 215 if (f != NULL) 216 return x; 217 } 218 return type_error("bad operand type(s) for |"); 219} 220 221PyObject * 222PyNumber_Xor(v, w) 223 PyObject *v, *w; 224{ 225 extern int PyNumber_Coerce(); 226 227 BINOP(v, w, "__xor__", "__rxor__", PyNumber_Xor); 228 if (v->ob_type->tp_as_number != NULL) { 229 PyObject *x = NULL; 230 PyObject * (*f) Py_FPROTO((PyObject *, PyObject *)); 231 if (PyNumber_Coerce(&v, &w) != 0) 232 return NULL; 233 if ((f = v->ob_type->tp_as_number->nb_xor) != NULL) 234 x = (*f)(v, w); 235 Py_DECREF(v); 236 Py_DECREF(w); 237 if (f != NULL) 238 return x; 239 } 240 return type_error("bad operand type(s) for ^"); 241} 242 243PyObject * 244PyNumber_And(v, w) 245 PyObject *v, *w; 246{ 247 BINOP(v, w, "__and__", "__rand__", PyNumber_And); 248 if (v->ob_type->tp_as_number != NULL) { 249 PyObject *x = NULL; 250 PyObject * (*f) Py_FPROTO((PyObject *, PyObject *)); 251 if (PyNumber_Coerce(&v, &w) != 0) 252 return NULL; 253 if ((f = v->ob_type->tp_as_number->nb_and) != NULL) 254 x = (*f)(v, w); 255 Py_DECREF(v); 256 Py_DECREF(w); 257 if (f != NULL) 258 return x; 259 } 260 return type_error("bad operand type(s) for &"); 261} 262 263PyObject * 264PyNumber_Lshift(v, w) 265 PyObject *v, *w; 266{ 267 BINOP(v, w, "__lshift__", "__rlshift__", PyNumber_Lshift); 268 if (v->ob_type->tp_as_number != NULL) { 269 PyObject *x = NULL; 270 PyObject * (*f) Py_FPROTO((PyObject *, PyObject *)); 271 if (PyNumber_Coerce(&v, &w) != 0) 272 return NULL; 273 if ((f = v->ob_type->tp_as_number->nb_lshift) != NULL) 274 x = (*f)(v, w); 275 Py_DECREF(v); 276 Py_DECREF(w); 277 if (f != NULL) 278 return x; 279 } 280 return type_error("bad operand type(s) for <<"); 281} 282 283PyObject * 284PyNumber_Rshift(v, w) 285 PyObject *v, *w; 286{ 287 BINOP(v, w, "__rshift__", "__rrshift__", PyNumber_Rshift); 288 if (v->ob_type->tp_as_number != NULL) { 289 PyObject *x = NULL; 290 PyObject * (*f) Py_FPROTO((PyObject *, PyObject *)); 291 if (PyNumber_Coerce(&v, &w) != 0) 292 return NULL; 293 if ((f = v->ob_type->tp_as_number->nb_rshift) != NULL) 294 x = (*f)(v, w); 295 Py_DECREF(v); 296 Py_DECREF(w); 297 if (f != NULL) 298 return x; 299 } 300 return type_error("bad operand type(s) for >>"); 301} 302 303PyObject * 304PyNumber_Add(v, w) 305 PyObject *v, *w; 306{ 307 PySequenceMethods *m; 308 309 BINOP(v, w, "__add__", "__radd__", PyNumber_Add); 310 m = v->ob_type->tp_as_sequence; 311 if (m && m->sq_concat) 312 return (*m->sq_concat)(v, w); 313 else if (v->ob_type->tp_as_number != NULL) { 314 PyObject *x = NULL; 315 PyObject * (*f) Py_FPROTO((PyObject *, PyObject *)); 316 if (PyNumber_Coerce(&v, &w) != 0) 317 return NULL; 318 if ((f = v->ob_type->tp_as_number->nb_add) != NULL) 319 x = (*f)(v, w); 320 Py_DECREF(v); 321 Py_DECREF(w); 322 if (f != NULL) 323 return x; 324 } 325 return type_error("bad operand type(s) for +"); 326} 327 328PyObject * 329PyNumber_Subtract(v, w) 330 PyObject *v, *w; 331{ 332 BINOP(v, w, "__sub__", "__rsub__", PyNumber_Subtract); 333 if (v->ob_type->tp_as_number != NULL) { 334 PyObject *x = NULL; 335 PyObject * (*f) Py_FPROTO((PyObject *, PyObject *)); 336 if (PyNumber_Coerce(&v, &w) != 0) 337 return NULL; 338 if ((f = v->ob_type->tp_as_number->nb_subtract) != NULL) 339 x = (*f)(v, w); 340 Py_DECREF(v); 341 Py_DECREF(w); 342 if (f != NULL) 343 return x; 344 } 345 return type_error("bad operand type(s) for -"); 346} 347 348PyObject * 349PyNumber_Multiply(v, w) 350 PyObject *v, *w; 351{ 352 PyTypeObject *tp = v->ob_type; 353 PySequenceMethods *m; 354 355 BINOP(v, w, "__mul__", "__rmul__", PyNumber_Multiply); 356 if (tp->tp_as_number != NULL && 357 w->ob_type->tp_as_sequence != NULL && 358 !PyInstance_Check(v)) { 359 /* number*sequence -- swap v and w */ 360 PyObject *tmp = v; 361 v = w; 362 w = tmp; 363 tp = v->ob_type; 364 } 365 if (tp->tp_as_number != NULL) { 366 PyObject *x = NULL; 367 PyObject * (*f) Py_FPROTO((PyObject *, PyObject *)); 368 if (PyInstance_Check(v)) { 369 /* Instances of user-defined classes get their 370 other argument uncoerced, so they may 371 implement sequence*number as well as 372 number*number. */ 373 Py_INCREF(v); 374 Py_INCREF(w); 375 } 376 else if (PyNumber_Coerce(&v, &w) != 0) 377 return NULL; 378 if ((f = v->ob_type->tp_as_number->nb_multiply) != NULL) 379 x = (*f)(v, w); 380 Py_DECREF(v); 381 Py_DECREF(w); 382 if (f != NULL) 383 return x; 384 } 385 m = tp->tp_as_sequence; 386 if (m && m->sq_repeat) { 387 if (!PyInt_Check(w)) 388 return type_error( 389 "can't multiply sequence with non-int"); 390 return (*m->sq_repeat)(v, (int)PyInt_AsLong(w)); 391 } 392 return type_error("bad operand type(s) for *"); 393} 394 395PyObject * 396PyNumber_Divide(v, w) 397 PyObject *v, *w; 398{ 399 BINOP(v, w, "__div__", "__rdiv__", PyNumber_Divide); 400 if (v->ob_type->tp_as_number != NULL) { 401 PyObject *x = NULL; 402 PyObject * (*f) Py_FPROTO((PyObject *, PyObject *)); 403 if (PyNumber_Coerce(&v, &w) != 0) 404 return NULL; 405 if ((f = v->ob_type->tp_as_number->nb_divide) != NULL) 406 x = (*f)(v, w); 407 Py_DECREF(v); 408 Py_DECREF(w); 409 if (f != NULL) 410 return x; 411 } 412 return type_error("bad operand type(s) for /"); 413} 414 415PyObject * 416PyNumber_Remainder(v, w) 417 PyObject *v, *w; 418{ 419 if (PyString_Check(v)) 420 return PyString_Format(v, w); 421 BINOP(v, w, "__mod__", "__rmod__", PyNumber_Remainder); 422 if (v->ob_type->tp_as_number != NULL) { 423 PyObject *x = NULL; 424 PyObject * (*f) Py_FPROTO((PyObject *, PyObject *)); 425 if (PyNumber_Coerce(&v, &w) != 0) 426 return NULL; 427 if ((f = v->ob_type->tp_as_number->nb_remainder) != NULL) 428 x = (*f)(v, w); 429 Py_DECREF(v); 430 Py_DECREF(w); 431 if (f != NULL) 432 return x; 433 } 434 return type_error("bad operand type(s) for %"); 435} 436 437PyObject * 438PyNumber_Divmod(v, w) 439 PyObject *v, *w; 440{ 441 BINOP(v, w, "__divmod__", "__rdivmod__", PyNumber_Divmod); 442 if (v->ob_type->tp_as_number != NULL) { 443 PyObject *x = NULL; 444 PyObject * (*f) Py_FPROTO((PyObject *, PyObject *)); 445 if (PyNumber_Coerce(&v, &w) != 0) 446 return NULL; 447 if ((f = v->ob_type->tp_as_number->nb_divmod) != NULL) 448 x = (*f)(v, w); 449 Py_DECREF(v); 450 Py_DECREF(w); 451 if (f != NULL) 452 return x; 453 } 454 return type_error("bad operand type(s) for divmod()"); 455} 456 457/* Power (binary or ternary) */ 458 459static PyObject * 460do_pow(v, w) 461 PyObject *v, *w; 462{ 463 PyObject *res; 464 PyObject * (*f) Py_FPROTO((PyObject *, PyObject *, PyObject *)); 465 BINOP(v, w, "__pow__", "__rpow__", do_pow); 466 if (v->ob_type->tp_as_number == NULL || 467 w->ob_type->tp_as_number == NULL) { 468 PyErr_SetString(PyExc_TypeError, 469 "pow(x, y) requires numeric arguments"); 470 return NULL; 471 } 472 if (PyNumber_Coerce(&v, &w) != 0) 473 return NULL; 474 if ((f = v->ob_type->tp_as_number->nb_power) != NULL) 475 res = (*f)(v, w, Py_None); 476 else 477 res = type_error("pow(x, y) not defined for these operands"); 478 Py_DECREF(v); 479 Py_DECREF(w); 480 return res; 481} 482 483PyObject * 484PyNumber_Power(v, w, z) 485 PyObject *v, *w, *z; 486{ 487 PyObject *res; 488 PyObject *v1, *z1, *w2, *z2; 489 PyObject * (*f) Py_FPROTO((PyObject *, PyObject *, PyObject *)); 490 491 if (z == Py_None) 492 return do_pow(v, w); 493 /* XXX The ternary version doesn't do class instance coercions */ 494 if (PyInstance_Check(v)) 495 return v->ob_type->tp_as_number->nb_power(v, w, z); 496 if (v->ob_type->tp_as_number == NULL || 497 z->ob_type->tp_as_number == NULL || 498 w->ob_type->tp_as_number == NULL) { 499 return type_error("pow(x, y, z) requires numeric arguments"); 500 } 501 if (PyNumber_Coerce(&v, &w) != 0) 502 return NULL; 503 res = NULL; 504 v1 = v; 505 z1 = z; 506 if (PyNumber_Coerce(&v1, &z1) != 0) 507 goto error2; 508 w2 = w; 509 z2 = z1; 510 if (PyNumber_Coerce(&w2, &z2) != 0) 511 goto error1; 512 if ((f = v1->ob_type->tp_as_number->nb_power) != NULL) 513 res = (*f)(v1, w2, z2); 514 else 515 res = type_error( 516 "pow(x, y, z) not defined for these operands"); 517 Py_DECREF(w2); 518 Py_DECREF(z2); 519 error1: 520 Py_DECREF(v1); 521 Py_DECREF(z1); 522 error2: 523 Py_DECREF(v); 524 Py_DECREF(w); 525 return res; 526} 527 528/* Unary operators and functions */ 529 530PyObject * 531PyNumber_Negative(o) 532 PyObject *o; 533{ 534 PyNumberMethods *m; 535 536 if (o == NULL) 537 return null_error(); 538 m = o->ob_type->tp_as_number; 539 if (m && m->nb_negative) 540 return (*m->nb_negative)(o); 541 542 return type_error("bad operand type for unary -"); 543} 544 545PyObject * 546PyNumber_Positive(o) 547 PyObject *o; 548{ 549 PyNumberMethods *m; 550 551 if (o == NULL) 552 return null_error(); 553 m = o->ob_type->tp_as_number; 554 if (m && m->nb_positive) 555 return (*m->nb_positive)(o); 556 557 return type_error("bad operand type for unary +"); 558} 559 560PyObject * 561PyNumber_Invert(o) 562 PyObject *o; 563{ 564 PyNumberMethods *m; 565 566 if (o == NULL) 567 return null_error(); 568 m = o->ob_type->tp_as_number; 569 if (m && m->nb_invert) 570 return (*m->nb_invert)(o); 571 572 return type_error("bad operand type for unary ~"); 573} 574 575PyObject * 576PyNumber_Absolute(o) 577 PyObject *o; 578{ 579 PyNumberMethods *m; 580 581 if (o == NULL) 582 return null_error(); 583 m = o->ob_type->tp_as_number; 584 if (m && m->nb_absolute) 585 return m->nb_absolute(o); 586 587 return type_error("bad operand type for abs()"); 588} 589 590PyObject * 591PyNumber_Int(o) 592 PyObject *o; 593{ 594 PyNumberMethods *m; 595 596 if (o == NULL) 597 return null_error(); 598 if (PyString_Check(o)) 599 return PyInt_FromString(PyString_AS_STRING(o), NULL, 10); 600 m = o->ob_type->tp_as_number; 601 if (m && m->nb_int) 602 return m->nb_int(o); 603 604 return type_error("object can't be converted to int"); 605} 606 607/* There are two C API functions for converting a string to a long, 608 * PyNumber_Long() and PyLong_FromString(). Both are used in builtin_long, 609 * reachable from Python with the built-in function long(). 610 * 611 * The difference is this: PyNumber_Long will raise an exception when the 612 * string cannot be converted to a long. The most common situation is 613 * where a float string is passed in; this raises a ValueError. 614 * PyLong_FromString does not raise an exception; it silently truncates the 615 * float to an integer. 616 * 617 * You can see the different behavior from Python with the following: 618 * 619 * long('9.5') 620 * => ValueError: invalid literal for long(): 9.5 621 * 622 * long('9.5', 10) 623 * => 9L 624 * 625 * The first example ends up calling PyNumber_Long(), while the second one 626 * calls PyLong_FromString(). 627 */ 628static PyObject * 629long_from_string(v) 630 PyObject *v; 631{ 632 char *s, *end; 633 PyObject *x; 634 char buffer[256]; /* For errors */ 635 636 s = PyString_AS_STRING(v); 637 while (*s && isspace(Py_CHARMASK(*s))) 638 s++; 639 x = PyLong_FromString(s, &end, 10); 640 if (x == NULL) { 641 if (PyErr_ExceptionMatches(PyExc_ValueError)) 642 goto bad; 643 return NULL; 644 } 645 while (*end && isspace(Py_CHARMASK(*end))) 646 end++; 647 if (*end != '\0') { 648 bad: 649 sprintf(buffer, "invalid literal for long(): %.200s", s); 650 PyErr_SetString(PyExc_ValueError, buffer); 651 Py_XDECREF(x); 652 return NULL; 653 } 654 else if (end != PyString_AS_STRING(v) + PyString_GET_SIZE(v)) { 655 PyErr_SetString(PyExc_ValueError, 656 "null byte in argument for long()"); 657 return NULL; 658 } 659 return x; 660} 661 662PyObject * 663PyNumber_Long(o) 664 PyObject *o; 665{ 666 PyNumberMethods *m; 667 668 if (o == NULL) 669 return null_error(); 670 if (PyString_Check(o)) 671 /* need to do extra error checking that PyLong_FromString() 672 * doesn't do. In particular long('9.5') must raise an 673 * exception, not truncate the float. 674 */ 675 return long_from_string(o); 676 m = o->ob_type->tp_as_number; 677 if (m && m->nb_long) 678 return m->nb_long(o); 679 680 return type_error("object can't be converted to long"); 681} 682 683PyObject * 684PyNumber_Float(o) 685 PyObject *o; 686{ 687 PyNumberMethods *m; 688 689 if (o == NULL) 690 return null_error(); 691 if (PyString_Check(o)) 692 return PyFloat_FromString(o, NULL); 693 m = o->ob_type->tp_as_number; 694 if (m && m->nb_float) 695 return m->nb_float(o); 696 697 return type_error("object can't be converted to float"); 698} 699 700/* Operations on sequences */ 701 702int 703PySequence_Check(s) 704 PyObject *s; 705{ 706 return s != NULL && s->ob_type->tp_as_sequence; 707} 708 709int 710PySequence_Length(s) 711 PyObject *s; 712{ 713 PySequenceMethods *m; 714 715 if (s == NULL) { 716 null_error(); 717 return -1; 718 } 719 720 m = s->ob_type->tp_as_sequence; 721 if (m && m->sq_length) 722 return m->sq_length(s); 723 724 type_error("len() of unsized object"); 725 return -1; 726} 727 728PyObject * 729PySequence_Concat(s, o) 730 PyObject *s; 731 PyObject *o; 732{ 733 PySequenceMethods *m; 734 735 if (s == NULL || o == NULL) 736 return null_error(); 737 738 m = s->ob_type->tp_as_sequence; 739 if (m && m->sq_concat) 740 return m->sq_concat(s, o); 741 742 return type_error("object can't be concatenated"); 743} 744 745PyObject * 746PySequence_Repeat(o, count) 747 PyObject *o; 748 int count; 749{ 750 PySequenceMethods *m; 751 752 if (o == NULL) 753 return null_error(); 754 755 m = o->ob_type->tp_as_sequence; 756 if (m && m->sq_repeat) 757 return m->sq_repeat(o, count); 758 759 return type_error("object can't be repeated"); 760} 761 762PyObject * 763PySequence_GetItem(s, i) 764 PyObject *s; 765 int i; 766{ 767 PySequenceMethods *m; 768 769 if (s == NULL) 770 return null_error(); 771 772 m = s->ob_type->tp_as_sequence; 773 if (m && m->sq_item) { 774 if (i < 0) { 775 if (m->sq_length) { 776 int l = (*m->sq_length)(s); 777 if (l < 0) 778 return NULL; 779 i += l; 780 } 781 } 782 return m->sq_item(s, i); 783 } 784 785 return type_error("unindexable object"); 786} 787 788PyObject * 789PySequence_GetSlice(s, i1, i2) 790 PyObject *s; 791 int i1; 792 int i2; 793{ 794 PySequenceMethods *m; 795 796 if (!s) return null_error(); 797 798 m = s->ob_type->tp_as_sequence; 799 if (m && m->sq_slice) { 800 if (i1 < 0 || i2 < 0) { 801 if (m->sq_length) { 802 int l = (*m->sq_length)(s); 803 if (l < 0) 804 return NULL; 805 if (i1 < 0) 806 i1 += l; 807 if (i2 < 0) 808 i2 += l; 809 } 810 } 811 return m->sq_slice(s, i1, i2); 812 } 813 814 return type_error("unsliceable object"); 815} 816 817int 818PySequence_SetItem(s, i, o) 819 PyObject *s; 820 int i; 821 PyObject *o; 822{ 823 PySequenceMethods *m; 824 825 if (s == NULL) { 826 null_error(); 827 return -1; 828 } 829 830 m = s->ob_type->tp_as_sequence; 831 if (m && m->sq_ass_item) { 832 if (i < 0) { 833 if (m->sq_length) { 834 int l = (*m->sq_length)(s); 835 if (l < 0) 836 return -1; 837 i += l; 838 } 839 } 840 return m->sq_ass_item(s, i, o); 841 } 842 843 type_error("object doesn't support item assignment"); 844 return -1; 845} 846 847int 848PySequence_DelItem(s, i) 849 PyObject *s; 850 int i; 851{ 852 PySequenceMethods *m; 853 854 if (s == NULL) { 855 null_error(); 856 return -1; 857 } 858 859 m = s->ob_type->tp_as_sequence; 860 if (m && m->sq_ass_item) { 861 if (i < 0) { 862 if (m->sq_length) { 863 int l = (*m->sq_length)(s); 864 if (l < 0) 865 return -1; 866 i += l; 867 } 868 } 869 return m->sq_ass_item(s, i, (PyObject *)NULL); 870 } 871 872 type_error("object doesn't support item deletion"); 873 return -1; 874} 875 876int 877PySequence_SetSlice(s, i1, i2, o) 878 PyObject *s; 879 int i1; 880 int i2; 881 PyObject *o; 882{ 883 PySequenceMethods *m; 884 885 if (s == NULL) { 886 null_error(); 887 return -1; 888 } 889 890 m = s->ob_type->tp_as_sequence; 891 if (m && m->sq_ass_slice) { 892 if (i1 < 0 || i2 < 0) { 893 if (m->sq_length) { 894 int l = (*m->sq_length)(s); 895 if (l < 0) 896 return -1; 897 if (i1 < 0) 898 i1 += l; 899 if (i2 < 0) 900 i2 += l; 901 } 902 } 903 return m->sq_ass_slice(s, i1, i2, o); 904 } 905 type_error("object doesn't support slice assignment"); 906 return -1; 907} 908 909int 910PySequence_DelSlice(s, i1, i2) 911 PyObject *s; 912 int i1; 913 int i2; 914{ 915 PySequenceMethods *m; 916 917 if (s == NULL) { 918 null_error(); 919 return -1; 920 } 921 922 m = s->ob_type->tp_as_sequence; 923 if (m && m->sq_ass_slice) { 924 if (i1 < 0 || i2 < 0) { 925 if (m->sq_length) { 926 int l = (*m->sq_length)(s); 927 if (l < 0) 928 return -1; 929 if (i1 < 0) 930 i1 += l; 931 if (i2 < 0) 932 i2 += l; 933 } 934 } 935 return m->sq_ass_slice(s, i1, i2, (PyObject *)NULL); 936 } 937 type_error("object doesn't support slice deletion"); 938 return -1; 939} 940 941PyObject * 942PySequence_Tuple(v) 943 PyObject *v; 944{ 945 PySequenceMethods *m; 946 947 if (v == NULL) 948 return null_error(); 949 950 if (PyTuple_Check(v)) { 951 Py_INCREF(v); 952 return v; 953 } 954 955 if (PyList_Check(v)) 956 return PyList_AsTuple(v); 957 958 /* There used to be code for strings here, but tuplifying strings is 959 not a common activity, so I nuked it. Down with code bloat! */ 960 961 /* Generic sequence object */ 962 m = v->ob_type->tp_as_sequence; 963 if (m && m->sq_item) { 964 int i; 965 PyObject *t; 966 int n = PySequence_Length(v); 967 if (n < 0) 968 return NULL; 969 t = PyTuple_New(n); 970 if (t == NULL) 971 return NULL; 972 for (i = 0; ; i++) { 973 PyObject *item = (*m->sq_item)(v, i); 974 if (item == NULL) { 975 if (PyErr_ExceptionMatches(PyExc_IndexError)) 976 PyErr_Clear(); 977 else { 978 Py_DECREF(t); 979 t = NULL; 980 } 981 break; 982 } 983 if (i >= n) { 984 if (n < 500) 985 n += 10; 986 else 987 n += 100; 988 if (_PyTuple_Resize(&t, n, 0) != 0) 989 break; 990 } 991 PyTuple_SET_ITEM(t, i, item); 992 } 993 if (i < n && t != NULL) 994 _PyTuple_Resize(&t, i, 0); 995 return t; 996 } 997 998 /* None of the above */ 999 return type_error("tuple() argument must be a sequence"); 1000} 1001 1002PyObject * 1003PySequence_List(v) 1004 PyObject *v; 1005{ 1006 PySequenceMethods *m; 1007 1008 if (v == NULL) 1009 return null_error(); 1010 1011 if (PyList_Check(v)) 1012 return PyList_GetSlice(v, 0, PyList_GET_SIZE(v)); 1013 1014 m = v->ob_type->tp_as_sequence; 1015 if (m && m->sq_item) { 1016 int i; 1017 PyObject *l; 1018 int n = PySequence_Length(v); 1019 if (n < 0) 1020 return NULL; 1021 l = PyList_New(n); 1022 if (l == NULL) 1023 return NULL; 1024 for (i = 0; ; i++) { 1025 PyObject *item = (*m->sq_item)(v, i); 1026 if (item == NULL) { 1027 if (PyErr_ExceptionMatches(PyExc_IndexError)) 1028 PyErr_Clear(); 1029 else { 1030 Py_DECREF(l); 1031 l = NULL; 1032 } 1033 break; 1034 } 1035 if (i < n) 1036 PyList_SET_ITEM(l, i, item); 1037 else if (PyList_Append(l, item) < 0) { 1038 Py_DECREF(l); 1039 l = NULL; 1040 break; 1041 } 1042 } 1043 if (i < n && l != NULL) { 1044 if (PyList_SetSlice(l, i, n, (PyObject *)NULL) != 0) { 1045 Py_DECREF(l); 1046 l = NULL; 1047 } 1048 } 1049 return l; 1050 } 1051 return type_error("list() argument must be a sequence"); 1052} 1053 1054int 1055PySequence_Count(s, o) 1056 PyObject *s; 1057 PyObject *o; 1058{ 1059 int l, i, n, cmp, err; 1060 PyObject *item; 1061 1062 if (s == NULL || o == NULL) { 1063 null_error(); 1064 return -1; 1065 } 1066 1067 l = PySequence_Length(s); 1068 if (l < 0) 1069 return -1; 1070 1071 n = 0; 1072 for (i = 0; i < l; i++) { 1073 item = PySequence_GetItem(s, i); 1074 if (item == NULL) 1075 return -1; 1076 err = PyObject_Cmp(item, o, &cmp); 1077 Py_DECREF(item); 1078 if (err < 0) 1079 return err; 1080 if (cmp == 0) 1081 n++; 1082 } 1083 return n; 1084} 1085 1086int 1087PySequence_Contains(w, v) /* v in w */ 1088 PyObject *w; 1089 PyObject *v; 1090{ 1091 int i, cmp; 1092 PyObject *x; 1093 PySequenceMethods *sq; 1094 1095 /* Special case for char in string */ 1096 if (PyString_Check(w)) { 1097 register char *s, *end; 1098 register char c; 1099 if (!PyString_Check(v) || PyString_Size(v) != 1) { 1100 PyErr_SetString(PyExc_TypeError, 1101 "string member test needs char left operand"); 1102 return -1; 1103 } 1104 c = PyString_AsString(v)[0]; 1105 s = PyString_AsString(w); 1106 end = s + PyString_Size(w); 1107 while (s < end) { 1108 if (c == *s++) 1109 return 1; 1110 } 1111 return 0; 1112 } 1113 1114 sq = w->ob_type->tp_as_sequence; 1115 if (sq == NULL || sq->sq_item == NULL) { 1116 PyErr_SetString(PyExc_TypeError, 1117 "'in' or 'not in' needs sequence right argument"); 1118 return -1; 1119 } 1120 1121 for (i = 0; ; i++) { 1122 x = (*sq->sq_item)(w, i); 1123 if (x == NULL) { 1124 if (PyErr_ExceptionMatches(PyExc_IndexError)) { 1125 PyErr_Clear(); 1126 break; 1127 } 1128 return -1; 1129 } 1130 cmp = PyObject_Compare(v, x); 1131 Py_XDECREF(x); 1132 if (cmp == 0) 1133 return 1; 1134 if (PyErr_Occurred()) 1135 return -1; 1136 } 1137 1138 return 0; 1139} 1140 1141/* Backwards compatibility */ 1142#undef PySequence_In 1143int 1144PySequence_In(w, v) 1145 PyObject *w; 1146 PyObject *v; 1147{ 1148 return PySequence_Contains(w, v); 1149} 1150 1151int 1152PySequence_Index(s, o) 1153 PyObject *s; 1154 PyObject *o; 1155{ 1156 int l, i, cmp, err; 1157 PyObject *item; 1158 1159 if (s == NULL || o == NULL) { 1160 null_error(); 1161 return -1; 1162 } 1163 1164 l = PySequence_Length(s); 1165 if (l < 0) 1166 return -1; 1167 1168 for (i = 0; i < l; i++) { 1169 item = PySequence_GetItem(s, i); 1170 if (item == NULL) 1171 return -1; 1172 err = PyObject_Cmp(item, o, &cmp); 1173 Py_DECREF(item); 1174 if (err < 0) 1175 return err; 1176 if (cmp == 0) 1177 return i; 1178 } 1179 1180 PyErr_SetString(PyExc_ValueError, "sequence.index(x): x not in list"); 1181 return -1; 1182} 1183 1184/* Operations on mappings */ 1185 1186int 1187PyMapping_Check(o) 1188 PyObject *o; 1189{ 1190 return o && o->ob_type->tp_as_mapping; 1191} 1192 1193int 1194PyMapping_Length(o) 1195 PyObject *o; 1196{ 1197 PyMappingMethods *m; 1198 1199 if (o == NULL) { 1200 null_error(); 1201 return -1; 1202 } 1203 1204 m = o->ob_type->tp_as_mapping; 1205 if (m && m->mp_length) 1206 return m->mp_length(o); 1207 1208 type_error("len() of unsized object"); 1209 return -1; 1210} 1211 1212PyObject * 1213PyMapping_GetItemString(o, key) 1214 PyObject *o; 1215 char *key; 1216{ 1217 PyObject *okey, *r; 1218 1219 if (key == NULL) 1220 return null_error(); 1221 1222 okey = PyString_FromString(key); 1223 if (okey == NULL) 1224 return NULL; 1225 r = PyObject_GetItem(o, okey); 1226 Py_DECREF(okey); 1227 return r; 1228} 1229 1230int 1231PyMapping_SetItemString(o, key, value) 1232 PyObject *o; 1233 char *key; 1234 PyObject *value; 1235{ 1236 PyObject *okey; 1237 int r; 1238 1239 if (key == NULL) { 1240 null_error(); 1241 return -1; 1242 } 1243 1244 okey = PyString_FromString(key); 1245 if (okey == NULL) 1246 return -1; 1247 r = PyObject_SetItem(o, okey, value); 1248 Py_DECREF(okey); 1249 return r; 1250} 1251 1252int 1253PyMapping_HasKeyString(o, key) 1254 PyObject *o; 1255 char *key; 1256{ 1257 PyObject *v; 1258 1259 v = PyMapping_GetItemString(o, key); 1260 if (v) { 1261 Py_DECREF(v); 1262 return 1; 1263 } 1264 PyErr_Clear(); 1265 return 0; 1266} 1267 1268int 1269PyMapping_HasKey(o, key) 1270 PyObject *o; 1271 PyObject *key; 1272{ 1273 PyObject *v; 1274 1275 v = PyObject_GetItem(o, key); 1276 if (v) { 1277 Py_DECREF(v); 1278 return 1; 1279 } 1280 PyErr_Clear(); 1281 return 0; 1282} 1283 1284/* Operations on callable objects */ 1285 1286/* XXX PyCallable_Check() is in object.c */ 1287 1288PyObject * 1289PyObject_CallObject(o, a) 1290 PyObject *o, *a; 1291{ 1292 PyObject *r; 1293 PyObject *args = a; 1294 1295 if (args == NULL) { 1296 args = PyTuple_New(0); 1297 if (args == NULL) 1298 return NULL; 1299 } 1300 1301 r = PyEval_CallObject(o, args); 1302 1303 if (args != a) { 1304 Py_DECREF(args); 1305 } 1306 1307 return r; 1308} 1309 1310PyObject * 1311#ifdef HAVE_STDARG_PROTOTYPES 1312/* VARARGS 2 */ 1313PyObject_CallFunction(PyObject *callable, char *format, ...) 1314#else 1315/* VARARGS */ 1316 PyObject_CallFunction(va_alist) va_dcl 1317#endif 1318{ 1319 va_list va; 1320 PyObject *args, *retval; 1321#ifdef HAVE_STDARG_PROTOTYPES 1322 va_start(va, format); 1323#else 1324 PyObject *callable; 1325 char *format; 1326 va_start(va); 1327 callable = va_arg(va, PyObject *); 1328 format = va_arg(va, char *); 1329#endif 1330 1331 if (callable == NULL) { 1332 va_end(va); 1333 return null_error(); 1334 } 1335 1336 if (format) 1337 args = Py_VaBuildValue(format, va); 1338 else 1339 args = PyTuple_New(0); 1340 1341 va_end(va); 1342 1343 if (args == NULL) 1344 return NULL; 1345 1346 if (!PyTuple_Check(args)) { 1347 PyObject *a; 1348 1349 a = PyTuple_New(1); 1350 if (a == NULL) 1351 return NULL; 1352 if (PyTuple_SetItem(a, 0, args) < 0) 1353 return NULL; 1354 args = a; 1355 } 1356 retval = PyObject_CallObject(callable, args); 1357 1358 Py_DECREF(args); 1359 1360 return retval; 1361} 1362 1363PyObject * 1364#ifdef HAVE_STDARG_PROTOTYPES 1365/* VARARGS 2 */ 1366PyObject_CallMethod(PyObject *o, char *name, char *format, ...) 1367#else 1368/* VARARGS */ 1369 PyObject_CallMethod(va_alist) va_dcl 1370#endif 1371{ 1372 va_list va; 1373 PyObject *args, *func = 0, *retval; 1374#ifdef HAVE_STDARG_PROTOTYPES 1375 va_start(va, format); 1376#else 1377 PyObject *o; 1378 char *name; 1379 char *format; 1380 va_start(va); 1381 o = va_arg(va, PyObject *); 1382 name = va_arg(va, char *); 1383 format = va_arg(va, char *); 1384#endif 1385 1386 if (o == NULL || name == NULL) { 1387 va_end(va); 1388 return null_error(); 1389 } 1390 1391 func = PyObject_GetAttrString(o, name); 1392 if (func == NULL) { 1393 va_end(va); 1394 PyErr_SetString(PyExc_AttributeError, name); 1395 return 0; 1396 } 1397 1398 if (!PyCallable_Check(func)) { 1399 va_end(va); 1400 return type_error("call of non-callable attribute"); 1401 } 1402 1403 if (format && *format) 1404 args = Py_VaBuildValue(format, va); 1405 else 1406 args = PyTuple_New(0); 1407 1408 va_end(va); 1409 1410 if (!args) 1411 return NULL; 1412 1413 if (!PyTuple_Check(args)) { 1414 PyObject *a; 1415 1416 a = PyTuple_New(1); 1417 if (a == NULL) 1418 return NULL; 1419 if (PyTuple_SetItem(a, 0, args) < 0) 1420 return NULL; 1421 args = a; 1422 } 1423 1424 retval = PyObject_CallObject(func, args); 1425 1426 Py_DECREF(args); 1427 Py_DECREF(func); 1428 1429 return retval; 1430} 1431