Guido van Rossum | e15dee5 | 1995-07-18 14:12:02 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
| 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
| 25 | /* Abstract Object Interface (many thanks to Jim Fulton) */ |
| 26 | |
| 27 | #include "Python.h" |
| 28 | |
| 29 | #define Py_TRY(E) if(!(E)) return NULL |
| 30 | #define Py_ASSERT(EXP,E,V) if(!(EXP)) return PyErr_SetString(E,V), (void*)NULL |
| 31 | #define SPAM printf("line %d\n",__LINE__) |
| 32 | |
| 33 | static PyObject * |
| 34 | Py_ReturnMethodError(char *name) |
| 35 | { |
| 36 | if(! name) name = "Unknown Error"; |
| 37 | PyErr_SetString(PyExc_AttributeError,name); |
| 38 | return 0; |
| 39 | } |
| 40 | |
| 41 | PyObject * |
| 42 | Py_ReturnNullError() |
| 43 | { |
| 44 | if(! PyErr_Occurred()) |
| 45 | PyErr_SetString(PyExc_SystemError, |
| 46 | "null argument to internal routine"); |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | int |
| 51 | PyObject_Cmp(PyObject *o1, PyObject *o2, int *result) |
| 52 | { |
| 53 | int r; |
| 54 | |
| 55 | if(! o1 || ! o2) return Py_ReturnNullError(),-1; |
| 56 | r=PyObject_Compare(o1,o2); |
| 57 | if(PyErr_Occurred()) return -1; |
| 58 | *result=r; |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | #if 0 /* Already in object.c */ |
| 63 | int |
| 64 | PyCallable_Check(PyObject *x) |
| 65 | { |
| 66 | if (x == NULL) |
| 67 | return 0; |
| 68 | if (x->ob_type->tp_call != NULL || |
| 69 | PyFunction_Check(x) || |
| 70 | PyMethod_Check(x) || |
| 71 | PyCFunction_Check(x) || |
| 72 | PyClass_Check(x)) |
| 73 | return 1; |
| 74 | if (PyInstance_Check(x)) { |
| 75 | PyObject *call = PyObject_GetAttrString(x, "__call__"); |
| 76 | if (call == NULL) { |
| 77 | err_clear(); |
| 78 | return 0; |
| 79 | } |
| 80 | /* Could test recursively but don't, for fear of endless |
| 81 | recursion if some joker sets self.__call__ = self */ |
| 82 | Py_DECREF(call); |
| 83 | return 1; |
| 84 | } |
| 85 | return 0; |
| 86 | } |
| 87 | #endif |
| 88 | |
| 89 | PyObject * |
| 90 | PyObject_Type(PyObject *o) |
| 91 | { |
| 92 | PyObject *v; |
| 93 | |
| 94 | if(! o) return Py_ReturnNullError(); |
| 95 | v = (PyObject *)o->ob_type; |
| 96 | Py_INCREF(v); |
| 97 | return v; |
| 98 | } |
| 99 | |
| 100 | int |
| 101 | PyObject_Length(PyObject *o) |
| 102 | { |
| 103 | PySequenceMethods *m; |
| 104 | |
| 105 | if(! o) return Py_ReturnNullError(),-1; |
| 106 | |
| 107 | if((m=o->ob_type->tp_as_sequence) && m->sq_length) |
| 108 | return m->sq_length(o); |
| 109 | |
| 110 | return PyMapping_Length(o); |
| 111 | } |
| 112 | |
| 113 | PyObject * |
| 114 | PyObject_GetItem(PyObject *o, PyObject *key) |
| 115 | { |
| 116 | PyMappingMethods *m; |
| 117 | |
| 118 | if(! o || ! key) return Py_ReturnNullError(); |
| 119 | |
| 120 | if((m=o->ob_type->tp_as_mapping) && m->mp_subscript) |
| 121 | return m->mp_subscript(o,key); |
| 122 | |
| 123 | if(PyInt_Check(key)) |
| 124 | return PySequence_GetItem(o,PyInt_AsLong(key)); |
| 125 | |
| 126 | PyErr_SetString(PyExc_TypeError,"expected integer index"); |
| 127 | return NULL; |
| 128 | } |
| 129 | |
| 130 | int |
| 131 | PyObject_SetItem(PyObject *o, PyObject *key, PyObject *value) |
| 132 | { |
| 133 | PyMappingMethods *m; |
| 134 | |
| 135 | if(! o || ! key || ! value) return Py_ReturnNullError(),-1; |
| 136 | if((m=o->ob_type->tp_as_mapping) && m->mp_ass_subscript) |
| 137 | return m->mp_ass_subscript(o,key,value); |
| 138 | |
| 139 | if(PyInt_Check(key)) |
| 140 | return PySequence_SetItem(o,PyInt_AsLong(key),value); |
| 141 | |
| 142 | PyErr_SetString(PyExc_TypeError,"expeced integer index"); |
| 143 | return -1; |
| 144 | } |
| 145 | |
| 146 | int |
| 147 | PyNumber_Check(PyObject *o) |
| 148 | { |
| 149 | return o && o->ob_type->tp_as_number; |
| 150 | } |
| 151 | |
| 152 | |
| 153 | #define BINOP(opname, ropname, thisfunc) \ |
| 154 | if (!PyInstance_Check(v) && !PyInstance_Check(w)) \ |
| 155 | ; \ |
| 156 | else \ |
| 157 | return PyInstance_DoBinOp(v, w, opname, ropname, thisfunc) |
| 158 | |
| 159 | PyObject * |
| 160 | PyNumber_Or(v, w) |
| 161 | PyObject *v, *w; |
| 162 | { |
| 163 | extern int PyNumber_Coerce(); |
| 164 | |
| 165 | BINOP("__or__", "__ror__", PyNumber_Or); |
| 166 | if (v->ob_type->tp_as_number != NULL) { |
| 167 | PyObject *x; |
| 168 | PyObject * (*f) Py_FPROTO((PyObject *, PyObject *)); |
| 169 | if (PyNumber_Coerce(&v, &w) != 0) |
| 170 | return NULL; |
| 171 | if ((f = v->ob_type->tp_as_number->nb_or) != NULL) |
| 172 | x = (*f)(v, w); |
| 173 | Py_DECREF(v); |
| 174 | Py_DECREF(w); |
| 175 | if (f != NULL) |
| 176 | return x; |
| 177 | } |
| 178 | PyErr_SetString(PyExc_TypeError, "bad operand type(s) for |"); |
| 179 | return NULL; |
| 180 | } |
| 181 | |
| 182 | PyObject * |
| 183 | PyNumber_Xor(v, w) |
| 184 | PyObject *v, *w; |
| 185 | { |
| 186 | extern int PyNumber_Coerce(); |
| 187 | |
| 188 | BINOP("__xor__", "__rxor__", PyNumber_Xor); |
| 189 | if (v->ob_type->tp_as_number != NULL) { |
| 190 | PyObject *x; |
| 191 | PyObject * (*f) Py_FPROTO((PyObject *, PyObject *)); |
| 192 | if (PyNumber_Coerce(&v, &w) != 0) |
| 193 | return NULL; |
| 194 | if ((f = v->ob_type->tp_as_number->nb_xor) != NULL) |
| 195 | x = (*f)(v, w); |
| 196 | Py_DECREF(v); |
| 197 | Py_DECREF(w); |
| 198 | if (f != NULL) |
| 199 | return x; |
| 200 | } |
| 201 | PyErr_SetString(PyExc_TypeError, "bad operand type(s) for ^"); |
| 202 | return NULL; |
| 203 | } |
| 204 | |
| 205 | PyObject * |
| 206 | PyNumber_And(v, w) |
| 207 | PyObject *v, *w; |
| 208 | { |
| 209 | BINOP("__and__", "__rand__", PyNumber_And); |
| 210 | if (v->ob_type->tp_as_number != NULL) { |
| 211 | PyObject *x; |
| 212 | PyObject * (*f) Py_FPROTO((PyObject *, PyObject *)); |
| 213 | if (PyNumber_Coerce(&v, &w) != 0) |
| 214 | return NULL; |
| 215 | if ((f = v->ob_type->tp_as_number->nb_and) != NULL) |
| 216 | x = (*f)(v, w); |
| 217 | Py_DECREF(v); |
| 218 | Py_DECREF(w); |
| 219 | if (f != NULL) |
| 220 | return x; |
| 221 | } |
| 222 | PyErr_SetString(PyExc_TypeError, "bad operand type(s) for &"); |
| 223 | return NULL; |
| 224 | } |
| 225 | |
| 226 | PyObject * |
| 227 | PyNumber_Lshift(v, w) |
| 228 | PyObject *v, *w; |
| 229 | { |
| 230 | BINOP("__lshift__", "__rlshift__", PyNumber_Lshift); |
| 231 | if (v->ob_type->tp_as_number != NULL) { |
| 232 | PyObject *x; |
| 233 | PyObject * (*f) Py_FPROTO((PyObject *, PyObject *)); |
| 234 | if (PyNumber_Coerce(&v, &w) != 0) |
| 235 | return NULL; |
| 236 | if ((f = v->ob_type->tp_as_number->nb_lshift) != NULL) |
| 237 | x = (*f)(v, w); |
| 238 | Py_DECREF(v); |
| 239 | Py_DECREF(w); |
| 240 | if (f != NULL) |
| 241 | return x; |
| 242 | } |
| 243 | PyErr_SetString(PyExc_TypeError, "bad operand type(s) for <<"); |
| 244 | return NULL; |
| 245 | } |
| 246 | |
| 247 | PyObject * |
| 248 | PyNumber_Rshift(v, w) |
| 249 | PyObject *v, *w; |
| 250 | { |
| 251 | BINOP("__rshift__", "__rrshift__", PyNumber_Rshift); |
| 252 | if (v->ob_type->tp_as_number != NULL) { |
| 253 | PyObject *x; |
| 254 | PyObject * (*f) Py_FPROTO((PyObject *, PyObject *)); |
| 255 | if (PyNumber_Coerce(&v, &w) != 0) |
| 256 | return NULL; |
| 257 | if ((f = v->ob_type->tp_as_number->nb_rshift) != NULL) |
| 258 | x = (*f)(v, w); |
| 259 | Py_DECREF(v); |
| 260 | Py_DECREF(w); |
| 261 | if (f != NULL) |
| 262 | return x; |
| 263 | } |
| 264 | PyErr_SetString(PyExc_TypeError, "bad operand type(s) for >>"); |
| 265 | return NULL; |
| 266 | } |
| 267 | |
| 268 | PyObject * |
| 269 | PyNumber_Add(v, w) |
| 270 | PyObject *v, *w; |
| 271 | { |
| 272 | BINOP("__add__", "__radd__", PyNumber_Add); |
| 273 | if (v->ob_type->tp_as_sequence != NULL) |
| 274 | return (*v->ob_type->tp_as_sequence->sq_concat)(v, w); |
| 275 | else if (v->ob_type->tp_as_number != NULL) { |
| 276 | PyObject *x; |
| 277 | if (PyNumber_Coerce(&v, &w) != 0) |
| 278 | return NULL; |
| 279 | x = (*v->ob_type->tp_as_number->nb_add)(v, w); |
| 280 | Py_DECREF(v); |
| 281 | Py_DECREF(w); |
| 282 | return x; |
| 283 | } |
| 284 | PyErr_SetString(PyExc_TypeError, "bad operand type(s) for +"); |
| 285 | return NULL; |
| 286 | } |
| 287 | |
| 288 | PyObject * |
| 289 | PyNumber_Subtract(v, w) |
| 290 | PyObject *v, *w; |
| 291 | { |
| 292 | BINOP("__sub__", "__rsub__", PyNumber_Subtract); |
| 293 | if (v->ob_type->tp_as_number != NULL) { |
| 294 | PyObject *x; |
| 295 | if (PyNumber_Coerce(&v, &w) != 0) |
| 296 | return NULL; |
| 297 | x = (*v->ob_type->tp_as_number->nb_subtract)(v, w); |
| 298 | Py_DECREF(v); |
| 299 | Py_DECREF(w); |
| 300 | return x; |
| 301 | } |
| 302 | PyErr_SetString(PyExc_TypeError, "bad operand type(s) for -"); |
| 303 | return NULL; |
| 304 | } |
| 305 | |
| 306 | PyObject * |
| 307 | PyNumber_Multiply(v, w) |
| 308 | PyObject *v, *w; |
| 309 | { |
| 310 | PyTypeObject *tp; |
| 311 | tp = v->ob_type; |
| 312 | BINOP("__mul__", "__rmul__", PyNumber_Multiply); |
| 313 | if (tp->tp_as_number != NULL && |
| 314 | w->ob_type->tp_as_sequence != NULL && |
| 315 | !PyInstance_Check(v)) { |
| 316 | /* number*sequence -- swap v and w */ |
| 317 | PyObject *tmp = v; |
| 318 | v = w; |
| 319 | w = tmp; |
| 320 | tp = v->ob_type; |
| 321 | } |
| 322 | if (tp->tp_as_number != NULL) { |
| 323 | PyObject *x; |
| 324 | if (PyInstance_Check(v)) { |
| 325 | /* Instances of user-defined classes get their |
| 326 | other argument uncoerced, so they may |
| 327 | implement sequence*number as well as |
| 328 | number*number. */ |
| 329 | Py_INCREF(v); |
| 330 | Py_INCREF(w); |
| 331 | } |
| 332 | else if (PyNumber_Coerce(&v, &w) != 0) |
| 333 | return NULL; |
| 334 | x = (*v->ob_type->tp_as_number->nb_multiply)(v, w); |
| 335 | Py_DECREF(v); |
| 336 | Py_DECREF(w); |
| 337 | return x; |
| 338 | } |
| 339 | if (tp->tp_as_sequence != NULL) { |
| 340 | if (!PyInt_Check(w)) { |
| 341 | PyErr_SetString(PyExc_TypeError, |
| 342 | "can't multiply sequence with non-int"); |
| 343 | return NULL; |
| 344 | } |
| 345 | return (*tp->tp_as_sequence->sq_repeat) |
| 346 | (v, (int)PyInt_AsLong(w)); |
| 347 | } |
| 348 | PyErr_SetString(PyExc_TypeError, "bad operand type(s) for *"); |
| 349 | return NULL; |
| 350 | } |
| 351 | |
| 352 | PyObject * |
| 353 | PyNumber_Divide(v, w) |
| 354 | PyObject *v, *w; |
| 355 | { |
| 356 | BINOP("__div__", "__rdiv__", PyNumber_Divide); |
| 357 | if (v->ob_type->tp_as_number != NULL) { |
| 358 | PyObject *x; |
| 359 | if (PyNumber_Coerce(&v, &w) != 0) |
| 360 | return NULL; |
| 361 | x = (*v->ob_type->tp_as_number->nb_divide)(v, w); |
| 362 | Py_DECREF(v); |
| 363 | Py_DECREF(w); |
| 364 | return x; |
| 365 | } |
| 366 | PyErr_SetString(PyExc_TypeError, "bad operand type(s) for /"); |
| 367 | return NULL; |
| 368 | } |
| 369 | |
| 370 | PyObject * |
| 371 | PyNumber_Remainder(v, w) |
| 372 | PyObject *v, *w; |
| 373 | { |
| 374 | if (PyString_Check(v)) { |
| 375 | return PyString_Format(v, w); |
| 376 | } |
| 377 | BINOP("__mod__", "__rmod__", PyNumber_Remainder); |
| 378 | if (v->ob_type->tp_as_number != NULL) { |
| 379 | PyObject *x; |
| 380 | if (PyNumber_Coerce(&v, &w) != 0) |
| 381 | return NULL; |
| 382 | x = (*v->ob_type->tp_as_number->nb_remainder)(v, w); |
| 383 | Py_DECREF(v); |
| 384 | Py_DECREF(w); |
| 385 | return x; |
| 386 | } |
| 387 | PyErr_SetString(PyExc_TypeError, "bad operand type(s) for %"); |
| 388 | return NULL; |
| 389 | } |
| 390 | |
| 391 | PyObject * |
| 392 | PyNumber_Divmod(v, w) |
| 393 | PyObject *v, *w; |
| 394 | { |
| 395 | PyObject *res; |
| 396 | |
| 397 | if (PyInstance_Check(v) || PyInstance_Check(w)) |
| 398 | return PyInstance_DoBinOp(v, w, "__divmod__", "__rdivmod__", |
| 399 | PyNumber_Divmod); |
| 400 | if (v->ob_type->tp_as_number == NULL || |
| 401 | w->ob_type->tp_as_number == NULL) { |
| 402 | PyErr_SetString(PyExc_TypeError, |
| 403 | "divmod() requires numeric or class instance arguments"); |
| 404 | return NULL; |
| 405 | } |
| 406 | if (PyNumber_Coerce(&v, &w) != 0) |
| 407 | return NULL; |
| 408 | res = (*v->ob_type->tp_as_number->nb_divmod)(v, w); |
| 409 | Py_DECREF(v); |
| 410 | Py_DECREF(w); |
| 411 | return res; |
| 412 | } |
| 413 | |
| 414 | |
| 415 | static PyObject * |
| 416 | do_pow(v, w) |
| 417 | PyObject *v, *w; |
| 418 | { |
| 419 | PyObject *res; |
| 420 | if (PyInstance_Check(v) || PyInstance_Check(w)) |
| 421 | return PyInstance_DoBinOp(v, w, "__pow__", "__rpow__", do_pow); |
| 422 | if (v->ob_type->tp_as_number == NULL || |
| 423 | w->ob_type->tp_as_number == NULL) { |
| 424 | PyErr_SetString(PyExc_TypeError, |
| 425 | "pow() requires numeric arguments"); |
| 426 | return NULL; |
| 427 | } |
| 428 | if (PyFloat_Check(w) && PyFloat_AsDouble(v) < 0.0) { |
| 429 | if (!PyErr_Occurred()) |
| 430 | PyErr_SetString(PyExc_ValueError, |
| 431 | "negative number to float power"); |
| 432 | return NULL; |
| 433 | } |
| 434 | if (PyNumber_Coerce(&v, &w) != 0) |
| 435 | return NULL; |
| 436 | res = (*v->ob_type->tp_as_number->nb_power)(v, w, Py_None); |
| 437 | Py_DECREF(v); |
| 438 | Py_DECREF(w); |
| 439 | return res; |
| 440 | } |
| 441 | |
| 442 | PyObject * |
| 443 | PyNumber_Power(v,w,z) |
| 444 | PyObject *v, *w, *z; |
| 445 | { |
| 446 | PyObject *res; |
| 447 | PyObject *v1, *z1, *w2, *z2; |
| 448 | |
| 449 | if (z == Py_None) |
| 450 | return do_pow(v, w); |
| 451 | /* XXX The ternary version doesn't do class instance coercions */ |
| 452 | if (PyInstance_Check(v)) |
| 453 | return v->ob_type->tp_as_number->nb_power(v, w, z); |
| 454 | if (v->ob_type->tp_as_number == NULL || |
| 455 | z->ob_type->tp_as_number == NULL || |
| 456 | w->ob_type->tp_as_number == NULL) { |
| 457 | PyErr_SetString(PyExc_TypeError, "pow() requires numeric arguments"); |
| 458 | return NULL; |
| 459 | } |
| 460 | if (PyNumber_Coerce(&v, &w) != 0) |
| 461 | return NULL; |
| 462 | res = NULL; |
| 463 | v1 = v; |
| 464 | z1 = z; |
| 465 | if (PyNumber_Coerce(&v1, &z1) != 0) |
| 466 | goto error2; |
| 467 | w2 = w; |
| 468 | z2 = z1; |
| 469 | if (PyNumber_Coerce(&w2, &z2) != 0) |
| 470 | goto error1; |
| 471 | res = (*v1->ob_type->tp_as_number->nb_power)(v1, w2, z2); |
| 472 | Py_DECREF(w2); |
| 473 | Py_DECREF(z2); |
| 474 | error1: |
| 475 | Py_DECREF(v1); |
| 476 | Py_DECREF(z1); |
| 477 | error2: |
| 478 | Py_DECREF(v); |
| 479 | Py_DECREF(w); |
| 480 | return res; |
| 481 | } |
| 482 | |
| 483 | |
| 484 | PyObject * |
| 485 | PyNumber_Negative(v) |
| 486 | PyObject *v; |
| 487 | { |
| 488 | if (v->ob_type->tp_as_number != NULL) |
| 489 | return (*v->ob_type->tp_as_number->nb_negative)(v); |
| 490 | PyErr_SetString(PyExc_TypeError, "bad operand type(s) for unary -"); |
| 491 | return NULL; |
| 492 | } |
| 493 | |
| 494 | PyObject * |
| 495 | PyNumber_Positive(v) |
| 496 | PyObject *v; |
| 497 | { |
| 498 | if (v->ob_type->tp_as_number != NULL) |
| 499 | return (*v->ob_type->tp_as_number->nb_positive)(v); |
| 500 | PyErr_SetString(PyExc_TypeError, "bad operand type(s) for unary +"); |
| 501 | return NULL; |
| 502 | } |
| 503 | |
| 504 | PyObject * |
| 505 | PyNumber_Invert(v) |
| 506 | PyObject *v; |
| 507 | { |
| 508 | PyObject * (*f) Py_FPROTO((PyObject *)); |
| 509 | if (v->ob_type->tp_as_number != NULL && |
| 510 | (f = v->ob_type->tp_as_number->nb_invert) != NULL) |
| 511 | return (*f)(v); |
| 512 | PyErr_SetString(PyExc_TypeError, "bad operand type(s) for unary ~"); |
| 513 | return NULL; |
| 514 | } |
| 515 | |
| 516 | PyObject * |
| 517 | PyNumber_Absolute(PyObject *o) |
| 518 | { |
| 519 | PyNumberMethods *m; |
| 520 | |
| 521 | if(! o) return Py_ReturnNullError(); |
| 522 | if((m=o->ob_type->tp_as_number) && m->nb_absolute) |
| 523 | return m->nb_absolute(o); |
| 524 | |
| 525 | return Py_ReturnMethodError("__abs__"); |
| 526 | } |
| 527 | |
| 528 | PyObject * |
| 529 | PyNumber_Int(PyObject *o) |
| 530 | { |
| 531 | PyNumberMethods *m; |
| 532 | |
| 533 | if(! o) return Py_ReturnNullError(); |
| 534 | if((m=o->ob_type->tp_as_number) && m->nb_int) |
| 535 | return m->nb_int(o); |
| 536 | |
| 537 | return Py_ReturnMethodError("__int__"); |
| 538 | } |
| 539 | |
| 540 | PyObject * |
| 541 | PyNumber_Long(PyObject *o) |
| 542 | { |
| 543 | PyNumberMethods *m; |
| 544 | |
| 545 | if(! o) return Py_ReturnNullError(); |
| 546 | if((m=o->ob_type->tp_as_number) && m->nb_long) |
| 547 | return m->nb_long(o); |
| 548 | |
| 549 | return Py_ReturnMethodError("__long__"); |
| 550 | } |
| 551 | |
| 552 | PyObject * |
| 553 | PyNumber_Float(PyObject *o) |
| 554 | { |
| 555 | PyNumberMethods *m; |
| 556 | |
| 557 | if(! o) return Py_ReturnNullError(); |
| 558 | if((m=o->ob_type->tp_as_number) && m->nb_float) |
| 559 | return m->nb_float(o); |
| 560 | |
| 561 | return Py_ReturnMethodError("__float__"); |
| 562 | } |
| 563 | |
| 564 | |
| 565 | int |
| 566 | PySequence_Check(PyObject *o) |
| 567 | { |
| 568 | return o && o->ob_type->tp_as_sequence; |
| 569 | } |
| 570 | |
| 571 | int |
| 572 | PySequence_Length(PyObject *s) |
| 573 | { |
| 574 | PySequenceMethods *m; |
| 575 | |
| 576 | if(! s) return Py_ReturnNullError(),-1; |
| 577 | |
| 578 | if((m=s->ob_type->tp_as_sequence) && m->sq_length) |
| 579 | return m->sq_length(s); |
| 580 | |
| 581 | Py_ReturnMethodError("__len__"); |
| 582 | return -1; |
| 583 | } |
| 584 | |
| 585 | PyObject * |
| 586 | PySequence_Concat(PyObject *s, PyObject *o) |
| 587 | { |
| 588 | PySequenceMethods *m; |
| 589 | |
| 590 | if(! s || ! o) return Py_ReturnNullError(); |
| 591 | |
| 592 | if((m=s->ob_type->tp_as_sequence) && m->sq_concat) |
| 593 | return m->sq_concat(s,o); |
| 594 | |
| 595 | return Py_ReturnMethodError("__concat__"); |
| 596 | } |
| 597 | |
| 598 | PyObject * |
| 599 | PySequence_Repeat(PyObject *o, int count) |
| 600 | { |
| 601 | PySequenceMethods *m; |
| 602 | |
| 603 | if(! o) return Py_ReturnNullError(); |
| 604 | |
| 605 | if((m=o->ob_type->tp_as_sequence) && m->sq_repeat) |
| 606 | return m->sq_repeat(o,count); |
| 607 | |
| 608 | return Py_ReturnMethodError("__repeat__"); |
| 609 | } |
| 610 | |
| 611 | PyObject * |
| 612 | PySequence_GetItem(PyObject *s, int i) |
| 613 | { |
| 614 | PySequenceMethods *m; |
| 615 | int l; |
| 616 | |
| 617 | if(! s) return Py_ReturnNullError(); |
| 618 | |
| 619 | if(! ((m=s->ob_type->tp_as_sequence) && m->sq_length && m->sq_item)) |
| 620 | return Py_ReturnMethodError("__getitem__"); |
| 621 | |
| 622 | if(0 > (l=m->sq_length(s))) return NULL; |
| 623 | |
| 624 | if(i < 0) i += l; |
| 625 | |
| 626 | return m->sq_item(s,i); |
| 627 | } |
| 628 | |
| 629 | PyObject * |
| 630 | PySequence_GetSlice(PyObject *s, int i1, int i2) |
| 631 | { |
| 632 | PySequenceMethods *m; |
| 633 | int l; |
| 634 | |
| 635 | if(! s) return Py_ReturnNullError(); |
| 636 | |
| 637 | if(! ((m=s->ob_type->tp_as_sequence) && m->sq_length && m->sq_slice)) |
| 638 | return Py_ReturnMethodError("__getslice__"); |
| 639 | |
| 640 | if(0 > (l=m->sq_length(s))) return NULL; |
| 641 | |
| 642 | if(i1 < 0) i1 += l; |
| 643 | if(i2 < 0) i2 += l; |
| 644 | |
| 645 | return m->sq_slice(s,i1,i2); |
| 646 | } |
| 647 | |
| 648 | int |
| 649 | PySequence_SetItem(PyObject *s, int i, PyObject *o) |
| 650 | { |
| 651 | PySequenceMethods *m; |
| 652 | int l; |
| 653 | if(! s) return Py_ReturnNullError(),-1; |
| 654 | |
| 655 | if(! ((m=s->ob_type->tp_as_sequence) && m->sq_length && m->sq_ass_item)) |
| 656 | return Py_ReturnMethodError("__setitem__"),-1; |
| 657 | |
| 658 | if(i < 0) |
| 659 | { |
| 660 | if(0 > (l=m->sq_length(s))) return -1; |
| 661 | i += l; |
| 662 | } |
| 663 | |
| 664 | return m->sq_ass_item(s,i,o); |
| 665 | } |
| 666 | |
| 667 | int |
| 668 | PySequence_SetSlice(PyObject *s, int i1, int i2, PyObject *o) |
| 669 | { |
| 670 | PySequenceMethods *m; |
| 671 | int l; |
| 672 | |
| 673 | if(! s) return Py_ReturnNullError(),-1; |
| 674 | |
| 675 | if(! ((m=s->ob_type->tp_as_sequence) && m->sq_length && m->sq_ass_slice)) |
| 676 | return Py_ReturnMethodError("__setslice__"),-1; |
| 677 | |
| 678 | if(0 > (l=m->sq_length(s))) return -1; |
| 679 | |
| 680 | if(i1 < 0) i1 += l; |
| 681 | if(i2 < 0) i2 += l; |
| 682 | |
| 683 | return m->sq_ass_slice(s,i1,i2,o); |
| 684 | } |
| 685 | |
| 686 | PyObject * |
| 687 | PySequence_Tuple(PyObject *s) |
| 688 | { |
| 689 | int l, i; |
| 690 | PyObject *t, *item; |
| 691 | |
| 692 | if(! s) return Py_ReturnNullError(); |
| 693 | |
| 694 | Py_TRY((l=PySequence_Length(s)) != -1); |
| 695 | Py_TRY(t=PyTuple_New(l)); |
| 696 | |
| 697 | for(i=0; i < l; i++) |
| 698 | { |
| 699 | if(item=PySequence_GetItem(s,i)) |
| 700 | { |
| 701 | if(PyTuple_SetItem(t,i,item) == -1) |
| 702 | { |
| 703 | Py_DECREF(item); |
| 704 | Py_DECREF(t); |
| 705 | return NULL; |
| 706 | } |
| 707 | } |
| 708 | else |
| 709 | { |
| 710 | Py_DECREF(t); |
| 711 | return NULL; |
| 712 | } |
| 713 | } |
| 714 | return t; |
| 715 | } |
| 716 | |
| 717 | int |
| 718 | PySequence_Count(PyObject *s, PyObject *o) |
| 719 | { |
| 720 | int l, i, n=0, not_equal, err; |
| 721 | PyObject *item; |
| 722 | |
| 723 | if(! s || ! o) return Py_ReturnNullError(), -1; |
| 724 | Py_TRY((l=PySequence_Length(s)) != -1),-1; |
| 725 | |
| 726 | for(i=0; i < l; i++) |
| 727 | { |
| 728 | Py_TRY(item=PySequence_GetItem(s,i)),-1; |
| 729 | err=PyObject_Cmp(item,o,¬_equal) == -1; |
| 730 | Py_DECREF(item); |
| 731 | if(err) return -1; |
| 732 | n += ! not_equal; |
| 733 | } |
| 734 | return n; |
| 735 | } |
| 736 | |
| 737 | int |
| 738 | PySequence_In(PyObject *s, PyObject *o) |
| 739 | { |
| 740 | int l, i, not_equal, err; |
| 741 | PyObject *item; |
| 742 | |
| 743 | if(! o || ! s) return Py_ReturnNullError(), -1; |
| 744 | Py_TRY((l=PySequence_Length(s)) != -1),-1; |
| 745 | |
| 746 | for(i=0; i < l; i++) |
| 747 | { |
| 748 | Py_TRY(item=PySequence_GetItem(s,i)),-1; |
| 749 | err=PyObject_Cmp(item,o,¬_equal) == -1; |
| 750 | Py_DECREF(item); |
| 751 | if(err) return -1; |
| 752 | if(! not_equal) return 1; |
| 753 | } |
| 754 | return 0; |
| 755 | } |
| 756 | |
| 757 | int |
| 758 | PySequence_Index(PyObject *s, PyObject *o) |
| 759 | { |
| 760 | int l, i, n=0, not_equal, err; |
| 761 | PyObject *item; |
| 762 | |
| 763 | if(! s || ! o) return Py_ReturnNullError(), -1; |
| 764 | Py_TRY((l=PySequence_Length(s)) != -1),-1; |
| 765 | |
| 766 | for(i=0; i < l; i++) |
| 767 | { |
| 768 | Py_TRY(item=PySequence_GetItem(s,i)),-1; |
| 769 | err=PyObject_Cmp(item,o,¬_equal) == -1; |
| 770 | Py_DECREF(item); |
| 771 | if(err) return -1; |
| 772 | if(! not_equal) return n; |
| 773 | } |
| 774 | return -1; |
| 775 | } |
| 776 | |
| 777 | int |
| 778 | PyMapping_Check(PyObject *o) |
| 779 | { |
| 780 | return o && o->ob_type->tp_as_mapping; |
| 781 | } |
| 782 | |
| 783 | int |
| 784 | PyMapping_Length(PyObject *s) |
| 785 | { |
| 786 | PyMappingMethods *m; |
| 787 | |
| 788 | if(! s) return Py_ReturnNullError(),-1; |
| 789 | |
| 790 | if((m=s->ob_type->tp_as_mapping) && m->mp_length) |
| 791 | return m->mp_length(s); |
| 792 | |
| 793 | Py_ReturnMethodError("__len__"); |
| 794 | return -1; |
| 795 | } |
| 796 | |
| 797 | int |
| 798 | PyMapping_HasKeyString(PyObject *o, char *key) |
| 799 | { |
| 800 | PyObject *v; |
| 801 | |
| 802 | v=PyMapping_GetItemString(o,key); |
| 803 | if(v) return 1; |
| 804 | err_clear(); |
| 805 | return 0; |
| 806 | } |
| 807 | |
| 808 | int |
| 809 | PyMapping_HasKey(PyObject *o, PyObject *key) |
| 810 | { |
| 811 | PyObject *v; |
| 812 | |
| 813 | v=PyObject_GetItem(o,key); |
| 814 | if(v) return 1; |
| 815 | err_clear(); |
| 816 | return 0; |
| 817 | } |
| 818 | |
| 819 | PyObject * |
| 820 | PyObject_CallObject(o,a) |
| 821 | PyObject *o, *a; |
| 822 | { |
| 823 | PyObject *r; |
| 824 | |
| 825 | if(a) return PyEval_CallObject(o,a); |
| 826 | |
| 827 | if(! (a=PyTuple_New(0))) |
| 828 | return NULL; |
| 829 | r=PyEval_CallObject(o,a); |
| 830 | Py_DECREF(a); |
| 831 | return r; |
| 832 | } |
| 833 | |
| 834 | PyObject * |
| 835 | #ifdef HAVE_STDARG_PROTOTYPES |
| 836 | /* VARARGS 2 */ |
| 837 | PyObject_CallFunction(PyObject *callable, char *format, ...) |
| 838 | #else |
| 839 | /* VARARGS */ |
| 840 | PyObject_CallFunction(va_alist) va_dcl |
| 841 | #endif |
| 842 | { |
| 843 | va_list va; |
| 844 | PyObject *args, *retval; |
| 845 | #ifdef HAVE_STDARG_PROTOTYPES |
| 846 | va_start(va, format); |
| 847 | #else |
| 848 | PyObject *callable; |
| 849 | char *format; |
| 850 | va_start(va); |
| 851 | callable = va_arg(va, PyObject *); |
| 852 | format = va_arg(va, char *); |
| 853 | #endif |
| 854 | |
| 855 | if( ! callable) |
| 856 | { |
| 857 | va_end(va); |
| 858 | return Py_ReturnNullError(); |
| 859 | } |
| 860 | |
| 861 | if(format) |
| 862 | args = Py_VaBuildValue(format, va); |
| 863 | else |
| 864 | args = PyTuple_New(0); |
| 865 | |
| 866 | va_end(va); |
| 867 | if(! args) return NULL; |
| 868 | |
| 869 | if(! PyTuple_Check(args)) |
| 870 | { |
| 871 | PyObject *a; |
| 872 | |
| 873 | Py_TRY(a=PyTuple_New(1)); |
| 874 | Py_TRY(PyTuple_SetItem(a,0,args) != -1); |
| 875 | args=a; |
| 876 | } |
| 877 | retval = PyObject_CallObject(callable,args); |
| 878 | Py_DECREF(args); |
| 879 | return retval; |
| 880 | } |
| 881 | |
| 882 | PyObject * |
| 883 | #ifdef HAVE_STDARG_PROTOTYPES |
| 884 | /* VARARGS 2 */ |
| 885 | PyObject_CallMethod(PyObject *o, char *name, char *format, ...) |
| 886 | #else |
| 887 | /* VARARGS */ |
| 888 | PyObject_CallMethod(va_alist) va_dcl |
| 889 | #endif |
| 890 | { |
| 891 | va_list va; |
| 892 | PyObject *args, *method=0, *retval; |
| 893 | #ifdef HAVE_STDARG_PROTOTYPES |
| 894 | va_start(va, format); |
| 895 | #else |
| 896 | PyObject *o; |
| 897 | char *format; |
| 898 | va_start(va); |
| 899 | o = va_arg(va, PyObject *); |
| 900 | name = va_arg(va, char *); |
| 901 | format = va_arg(va, char *); |
| 902 | #endif |
| 903 | |
| 904 | if( ! o || ! name) |
| 905 | { |
| 906 | va_end(va); |
| 907 | return Py_ReturnNullError(); |
| 908 | } |
| 909 | |
| 910 | method=PyObject_GetAttrString(o,name); |
| 911 | if(! method) |
| 912 | { |
| 913 | va_end(va); |
| 914 | PyErr_SetString(PyExc_AttributeError,name); |
| 915 | return 0; |
| 916 | } |
| 917 | |
| 918 | if(! (PyCallable_Check(method))) |
| 919 | { |
| 920 | va_end(va); |
| 921 | PyErr_SetString(PyExc_TypeError,"call of non-callable attribute"); |
| 922 | return 0; |
| 923 | } |
| 924 | |
| 925 | if(format) |
| 926 | args = Py_VaBuildValue(format, va); |
| 927 | else |
| 928 | args = PyTuple_New(0); |
| 929 | |
| 930 | va_end(va); |
| 931 | |
| 932 | if(! args) return NULL; |
| 933 | |
| 934 | if(! PyTuple_Check(args)) |
| 935 | { |
| 936 | PyObject *a; |
| 937 | |
| 938 | Py_TRY(a=PyTuple_New(1)); |
| 939 | Py_TRY(PyTuple_SetItem(a,0,args) != -1); |
| 940 | args=a; |
| 941 | } |
| 942 | |
| 943 | retval = PyObject_CallObject(method,args); |
| 944 | Py_DECREF(args); |
| 945 | Py_DECREF(method); |
| 946 | return retval; |
| 947 | } |
| 948 | |
| 949 | PyObject * |
| 950 | PyMapping_GetItemString(PyObject *o, char *key) |
| 951 | { |
| 952 | PyObject *okey, *r; |
| 953 | |
| 954 | if( ! key) return Py_ReturnNullError(); |
| 955 | Py_TRY(okey=PyString_FromString(key)); |
| 956 | r = PyObject_GetItem(o,okey); |
| 957 | Py_DECREF(okey); |
| 958 | return r; |
| 959 | } |
| 960 | |
| 961 | int |
| 962 | PyMapping_SetItemString(PyObject *o, char *key, PyObject *value) |
| 963 | { |
| 964 | PyObject *okey; |
| 965 | int r; |
| 966 | |
| 967 | if( ! key) return Py_ReturnNullError(),-1; |
| 968 | if (!(okey=PyString_FromString(key))) return -1; |
| 969 | r = PyObject_SetItem(o,okey,value); |
| 970 | Py_DECREF(okey); |
| 971 | return r; |
| 972 | } |