| 1 | /* Abstract Object Interface (many thanks to Jim Fulton) */
|
|---|
| 2 |
|
|---|
| 3 | #include "Python.h"
|
|---|
| 4 | #include <ctype.h>
|
|---|
| 5 | #include "structmember.h" /* we need the offsetof() macro from there */
|
|---|
| 6 | #include "longintrepr.h"
|
|---|
| 7 |
|
|---|
| 8 | #define NEW_STYLE_NUMBER(o) PyType_HasFeature((o)->ob_type, \
|
|---|
| 9 | Py_TPFLAGS_CHECKTYPES)
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 | /* Shorthands to return certain errors */
|
|---|
| 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 | /* Operations on any object */
|
|---|
| 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 | Py_ssize_t
|
|---|
| 86 | _PyObject_LengthHint(PyObject *o)
|
|---|
| 87 | {
|
|---|
| 88 | Py_ssize_t rv = PyObject_Size(o);
|
|---|
| 89 | if (rv != -1)
|
|---|
| 90 | return rv;
|
|---|
| 91 | if (PyErr_ExceptionMatches(PyExc_TypeError) ||
|
|---|
| 92 | PyErr_ExceptionMatches(PyExc_AttributeError)) {
|
|---|
| 93 | PyObject *err_type, *err_value, *err_tb, *ro;
|
|---|
| 94 |
|
|---|
| 95 | PyErr_Fetch(&err_type, &err_value, &err_tb);
|
|---|
| 96 | ro = PyObject_CallMethod(o, "__length_hint__", NULL);
|
|---|
| 97 | if (ro != NULL) {
|
|---|
| 98 | rv = PyInt_AsLong(ro);
|
|---|
| 99 | Py_DECREF(ro);
|
|---|
| 100 | Py_XDECREF(err_type);
|
|---|
| 101 | Py_XDECREF(err_value);
|
|---|
| 102 | Py_XDECREF(err_tb);
|
|---|
| 103 | return rv;
|
|---|
| 104 | }
|
|---|
| 105 | PyErr_Restore(err_type, err_value, err_tb);
|
|---|
| 106 | }
|
|---|
| 107 | return -1;
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | PyObject *
|
|---|
| 111 | PyObject_GetItem(PyObject *o, 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 (PyIndex_Check(key)) {
|
|---|
| 124 | Py_ssize_t key_value;
|
|---|
| 125 | key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);
|
|---|
| 126 | if (key_value == -1 && PyErr_Occurred())
|
|---|
| 127 | return NULL;
|
|---|
| 128 | return PySequence_GetItem(o, key_value);
|
|---|
| 129 | }
|
|---|
| 130 | else if (o->ob_type->tp_as_sequence->sq_item)
|
|---|
| 131 | return type_error("sequence index must "
|
|---|
| 132 | "be integer, not '%.200s'", key);
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | return type_error("'%.200s' object is unsubscriptable", o);
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | int
|
|---|
| 139 | PyObject_SetItem(PyObject *o, PyObject *key, PyObject *value)
|
|---|
| 140 | {
|
|---|
| 141 | PyMappingMethods *m;
|
|---|
| 142 |
|
|---|
| 143 | if (o == NULL || key == NULL || value == NULL) {
|
|---|
| 144 | null_error();
|
|---|
| 145 | return -1;
|
|---|
| 146 | }
|
|---|
| 147 | m = o->ob_type->tp_as_mapping;
|
|---|
| 148 | if (m && m->mp_ass_subscript)
|
|---|
| 149 | return m->mp_ass_subscript(o, key, value);
|
|---|
| 150 |
|
|---|
| 151 | if (o->ob_type->tp_as_sequence) {
|
|---|
| 152 | if (PyIndex_Check(key)) {
|
|---|
| 153 | Py_ssize_t key_value;
|
|---|
| 154 | key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);
|
|---|
| 155 | if (key_value == -1 && PyErr_Occurred())
|
|---|
| 156 | return -1;
|
|---|
| 157 | return PySequence_SetItem(o, key_value, value);
|
|---|
| 158 | }
|
|---|
| 159 | else if (o->ob_type->tp_as_sequence->sq_ass_item) {
|
|---|
| 160 | type_error("sequence index must be "
|
|---|
| 161 | "integer, not '%.200s'", key);
|
|---|
| 162 | return -1;
|
|---|
| 163 | }
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | type_error("'%.200s' object does not support item assignment", o);
|
|---|
| 167 | return -1;
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | int
|
|---|
| 171 | PyObject_DelItem(PyObject *o, PyObject *key)
|
|---|
| 172 | {
|
|---|
| 173 | PyMappingMethods *m;
|
|---|
| 174 |
|
|---|
| 175 | if (o == NULL || key == NULL) {
|
|---|
| 176 | null_error();
|
|---|
| 177 | return -1;
|
|---|
| 178 | }
|
|---|
| 179 | m = o->ob_type->tp_as_mapping;
|
|---|
| 180 | if (m && m->mp_ass_subscript)
|
|---|
| 181 | return m->mp_ass_subscript(o, key, (PyObject*)NULL);
|
|---|
| 182 |
|
|---|
| 183 | if (o->ob_type->tp_as_sequence) {
|
|---|
| 184 | if (PyIndex_Check(key)) {
|
|---|
| 185 | Py_ssize_t key_value;
|
|---|
| 186 | key_value = PyNumber_AsSsize_t(key, PyExc_IndexError);
|
|---|
| 187 | if (key_value == -1 && PyErr_Occurred())
|
|---|
| 188 | return -1;
|
|---|
| 189 | return PySequence_DelItem(o, key_value);
|
|---|
| 190 | }
|
|---|
| 191 | else if (o->ob_type->tp_as_sequence->sq_ass_item) {
|
|---|
| 192 | type_error("sequence index must be "
|
|---|
| 193 | "integer, not '%.200s'", key);
|
|---|
| 194 | return -1;
|
|---|
| 195 | }
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | type_error("'%.200s' object does not support item deletion", o);
|
|---|
| 199 | return -1;
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | int
|
|---|
| 203 | PyObject_DelItemString(PyObject *o, char *key)
|
|---|
| 204 | {
|
|---|
| 205 | PyObject *okey;
|
|---|
| 206 | int ret;
|
|---|
| 207 |
|
|---|
| 208 | if (o == NULL || key == NULL) {
|
|---|
| 209 | null_error();
|
|---|
| 210 | return -1;
|
|---|
| 211 | }
|
|---|
| 212 | okey = PyString_FromString(key);
|
|---|
| 213 | if (okey == NULL)
|
|---|
| 214 | return -1;
|
|---|
| 215 | ret = PyObject_DelItem(o, okey);
|
|---|
| 216 | Py_DECREF(okey);
|
|---|
| 217 | return ret;
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | int
|
|---|
| 221 | PyObject_AsCharBuffer(PyObject *obj,
|
|---|
| 222 | const char **buffer,
|
|---|
| 223 | Py_ssize_t *buffer_len)
|
|---|
| 224 | {
|
|---|
| 225 | PyBufferProcs *pb;
|
|---|
| 226 | char *pp;
|
|---|
| 227 | Py_ssize_t len;
|
|---|
| 228 |
|
|---|
| 229 | if (obj == NULL || buffer == NULL || buffer_len == NULL) {
|
|---|
| 230 | null_error();
|
|---|
| 231 | return -1;
|
|---|
| 232 | }
|
|---|
| 233 | pb = obj->ob_type->tp_as_buffer;
|
|---|
| 234 | if (pb == NULL ||
|
|---|
| 235 | pb->bf_getcharbuffer == NULL ||
|
|---|
| 236 | pb->bf_getsegcount == NULL) {
|
|---|
| 237 | PyErr_SetString(PyExc_TypeError,
|
|---|
| 238 | "expected a character buffer object");
|
|---|
| 239 | return -1;
|
|---|
| 240 | }
|
|---|
| 241 | if ((*pb->bf_getsegcount)(obj,NULL) != 1) {
|
|---|
| 242 | PyErr_SetString(PyExc_TypeError,
|
|---|
| 243 | "expected a single-segment buffer object");
|
|---|
| 244 | return -1;
|
|---|
| 245 | }
|
|---|
| 246 | len = (*pb->bf_getcharbuffer)(obj, 0, &pp);
|
|---|
| 247 | if (len < 0)
|
|---|
| 248 | return -1;
|
|---|
| 249 | *buffer = pp;
|
|---|
| 250 | *buffer_len = len;
|
|---|
| 251 | return 0;
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | int
|
|---|
| 255 | PyObject_CheckReadBuffer(PyObject *obj)
|
|---|
| 256 | {
|
|---|
| 257 | PyBufferProcs *pb = obj->ob_type->tp_as_buffer;
|
|---|
| 258 |
|
|---|
| 259 | if (pb == NULL ||
|
|---|
| 260 | pb->bf_getreadbuffer == NULL ||
|
|---|
| 261 | pb->bf_getsegcount == NULL ||
|
|---|
| 262 | (*pb->bf_getsegcount)(obj, NULL) != 1)
|
|---|
| 263 | return 0;
|
|---|
| 264 | return 1;
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | int PyObject_AsReadBuffer(PyObject *obj,
|
|---|
| 268 | const void **buffer,
|
|---|
| 269 | Py_ssize_t *buffer_len)
|
|---|
| 270 | {
|
|---|
| 271 | PyBufferProcs *pb;
|
|---|
| 272 | void *pp;
|
|---|
| 273 | Py_ssize_t len;
|
|---|
| 274 |
|
|---|
| 275 | if (obj == NULL || buffer == NULL || buffer_len == NULL) {
|
|---|
| 276 | null_error();
|
|---|
| 277 | return -1;
|
|---|
| 278 | }
|
|---|
| 279 | pb = obj->ob_type->tp_as_buffer;
|
|---|
| 280 | if (pb == NULL ||
|
|---|
| 281 | pb->bf_getreadbuffer == NULL ||
|
|---|
| 282 | pb->bf_getsegcount == NULL) {
|
|---|
| 283 | PyErr_SetString(PyExc_TypeError,
|
|---|
| 284 | "expected a readable buffer object");
|
|---|
| 285 | return -1;
|
|---|
| 286 | }
|
|---|
| 287 | if ((*pb->bf_getsegcount)(obj, NULL) != 1) {
|
|---|
| 288 | PyErr_SetString(PyExc_TypeError,
|
|---|
| 289 | "expected a single-segment buffer object");
|
|---|
| 290 | return -1;
|
|---|
| 291 | }
|
|---|
| 292 | len = (*pb->bf_getreadbuffer)(obj, 0, &pp);
|
|---|
| 293 | if (len < 0)
|
|---|
| 294 | return -1;
|
|---|
| 295 | *buffer = pp;
|
|---|
| 296 | *buffer_len = len;
|
|---|
| 297 | return 0;
|
|---|
| 298 | }
|
|---|
| 299 |
|
|---|
| 300 | int PyObject_AsWriteBuffer(PyObject *obj,
|
|---|
| 301 | void **buffer,
|
|---|
| 302 | Py_ssize_t *buffer_len)
|
|---|
| 303 | {
|
|---|
| 304 | PyBufferProcs *pb;
|
|---|
| 305 | void*pp;
|
|---|
| 306 | Py_ssize_t len;
|
|---|
| 307 |
|
|---|
| 308 | if (obj == NULL || buffer == NULL || buffer_len == NULL) {
|
|---|
| 309 | null_error();
|
|---|
| 310 | return -1;
|
|---|
| 311 | }
|
|---|
| 312 | pb = obj->ob_type->tp_as_buffer;
|
|---|
| 313 | if (pb == NULL ||
|
|---|
| 314 | pb->bf_getwritebuffer == NULL ||
|
|---|
| 315 | pb->bf_getsegcount == NULL) {
|
|---|
| 316 | PyErr_SetString(PyExc_TypeError,
|
|---|
| 317 | "expected a writeable buffer object");
|
|---|
| 318 | return -1;
|
|---|
| 319 | }
|
|---|
| 320 | if ((*pb->bf_getsegcount)(obj, NULL) != 1) {
|
|---|
| 321 | PyErr_SetString(PyExc_TypeError,
|
|---|
| 322 | "expected a single-segment buffer object");
|
|---|
| 323 | return -1;
|
|---|
| 324 | }
|
|---|
| 325 | len = (*pb->bf_getwritebuffer)(obj,0,&pp);
|
|---|
| 326 | if (len < 0)
|
|---|
| 327 | return -1;
|
|---|
| 328 | *buffer = pp;
|
|---|
| 329 | *buffer_len = len;
|
|---|
| 330 | return 0;
|
|---|
| 331 | }
|
|---|
| 332 |
|
|---|
| 333 | /* Operations on numbers */
|
|---|
| 334 |
|
|---|
| 335 | int
|
|---|
| 336 | PyNumber_Check(PyObject *o)
|
|---|
| 337 | {
|
|---|
| 338 | return o && o->ob_type->tp_as_number &&
|
|---|
| 339 | (o->ob_type->tp_as_number->nb_int ||
|
|---|
| 340 | o->ob_type->tp_as_number->nb_float);
|
|---|
| 341 | }
|
|---|
| 342 |
|
|---|
| 343 | /* Binary operators */
|
|---|
| 344 |
|
|---|
| 345 | /* New style number protocol support */
|
|---|
| 346 |
|
|---|
| 347 | #define NB_SLOT(x) offsetof(PyNumberMethods, x)
|
|---|
| 348 | #define NB_BINOP(nb_methods, slot) \
|
|---|
| 349 | (*(binaryfunc*)(& ((char*)nb_methods)[slot]))
|
|---|
| 350 | #define NB_TERNOP(nb_methods, slot) \
|
|---|
| 351 | (*(ternaryfunc*)(& ((char*)nb_methods)[slot]))
|
|---|
| 352 |
|
|---|
| 353 | /*
|
|---|
| 354 | Calling scheme used for binary operations:
|
|---|
| 355 |
|
|---|
| 356 | v w Action
|
|---|
| 357 | -------------------------------------------------------------------
|
|---|
| 358 | new new w.op(v,w)[*], v.op(v,w), w.op(v,w)
|
|---|
| 359 | new old v.op(v,w), coerce(v,w), v.op(v,w)
|
|---|
| 360 | old new w.op(v,w), coerce(v,w), v.op(v,w)
|
|---|
| 361 | old old coerce(v,w), v.op(v,w)
|
|---|
| 362 |
|
|---|
| 363 | [*] only when v->ob_type != w->ob_type && w->ob_type is a subclass of
|
|---|
| 364 | v->ob_type
|
|---|
| 365 |
|
|---|
| 366 | Legend:
|
|---|
| 367 | -------
|
|---|
| 368 | * new == new style number
|
|---|
| 369 | * old == old style number
|
|---|
| 370 | * Action indicates the order in which operations are tried until either
|
|---|
| 371 | a valid result is produced or an error occurs.
|
|---|
| 372 |
|
|---|
| 373 | */
|
|---|
| 374 |
|
|---|
| 375 | static PyObject *
|
|---|
| 376 | binary_op1(PyObject *v, PyObject *w, const int op_slot)
|
|---|
| 377 | {
|
|---|
| 378 | PyObject *x;
|
|---|
| 379 | binaryfunc slotv = NULL;
|
|---|
| 380 | binaryfunc slotw = NULL;
|
|---|
| 381 |
|
|---|
| 382 | if (v->ob_type->tp_as_number != NULL && NEW_STYLE_NUMBER(v))
|
|---|
| 383 | slotv = NB_BINOP(v->ob_type->tp_as_number, op_slot);
|
|---|
| 384 | if (w->ob_type != v->ob_type &&
|
|---|
| 385 | w->ob_type->tp_as_number != NULL && NEW_STYLE_NUMBER(w)) {
|
|---|
| 386 | slotw = NB_BINOP(w->ob_type->tp_as_number, op_slot);
|
|---|
| 387 | if (slotw == slotv)
|
|---|
| 388 | slotw = NULL;
|
|---|
| 389 | }
|
|---|
| 390 | if (slotv) {
|
|---|
| 391 | if (slotw && PyType_IsSubtype(w->ob_type, v->ob_type)) {
|
|---|
| 392 | x = slotw(v, w);
|
|---|
| 393 | if (x != Py_NotImplemented)
|
|---|
| 394 | return x;
|
|---|
| 395 | Py_DECREF(x); /* can't do it */
|
|---|
| 396 | slotw = NULL;
|
|---|
| 397 | }
|
|---|
| 398 | x = slotv(v, w);
|
|---|
| 399 | if (x != Py_NotImplemented)
|
|---|
| 400 | return x;
|
|---|
| 401 | Py_DECREF(x); /* can't do it */
|
|---|
| 402 | }
|
|---|
| 403 | if (slotw) {
|
|---|
| 404 | x = slotw(v, w);
|
|---|
| 405 | if (x != Py_NotImplemented)
|
|---|
| 406 | return x;
|
|---|
| 407 | Py_DECREF(x); /* can't do it */
|
|---|
| 408 | }
|
|---|
| 409 | if (!NEW_STYLE_NUMBER(v) || !NEW_STYLE_NUMBER(w)) {
|
|---|
| 410 | int err = PyNumber_CoerceEx(&v, &w);
|
|---|
| 411 | if (err < 0) {
|
|---|
| 412 | return NULL;
|
|---|
| 413 | }
|
|---|
| 414 | if (err == 0) {
|
|---|
| 415 | PyNumberMethods *mv = v->ob_type->tp_as_number;
|
|---|
| 416 | if (mv) {
|
|---|
| 417 | binaryfunc slot;
|
|---|
| 418 | slot = NB_BINOP(mv, op_slot);
|
|---|
| 419 | if (slot) {
|
|---|
| 420 | x = slot(v, w);
|
|---|
| 421 | Py_DECREF(v);
|
|---|
| 422 | Py_DECREF(w);
|
|---|
| 423 | return x;
|
|---|
| 424 | }
|
|---|
| 425 | }
|
|---|
| 426 | /* CoerceEx incremented the reference counts */
|
|---|
| 427 | Py_DECREF(v);
|
|---|
| 428 | Py_DECREF(w);
|
|---|
| 429 | }
|
|---|
| 430 | }
|
|---|
| 431 | Py_INCREF(Py_NotImplemented);
|
|---|
| 432 | return Py_NotImplemented;
|
|---|
| 433 | }
|
|---|
| 434 |
|
|---|
| 435 | static PyObject *
|
|---|
| 436 | binop_type_error(PyObject *v, PyObject *w, const char *op_name)
|
|---|
| 437 | {
|
|---|
| 438 | PyErr_Format(PyExc_TypeError,
|
|---|
| 439 | "unsupported operand type(s) for %.100s: "
|
|---|
| 440 | "'%.100s' and '%.100s'",
|
|---|
| 441 | op_name,
|
|---|
| 442 | v->ob_type->tp_name,
|
|---|
| 443 | w->ob_type->tp_name);
|
|---|
| 444 | return NULL;
|
|---|
| 445 | }
|
|---|
| 446 |
|
|---|
| 447 | static PyObject *
|
|---|
| 448 | binary_op(PyObject *v, PyObject *w, const int op_slot, const char *op_name)
|
|---|
| 449 | {
|
|---|
| 450 | PyObject *result = binary_op1(v, w, op_slot);
|
|---|
| 451 | if (result == Py_NotImplemented) {
|
|---|
| 452 | Py_DECREF(result);
|
|---|
| 453 | return binop_type_error(v, w, op_name);
|
|---|
| 454 | }
|
|---|
| 455 | return result;
|
|---|
| 456 | }
|
|---|
| 457 |
|
|---|
| 458 |
|
|---|
| 459 | /*
|
|---|
| 460 | Calling scheme used for ternary operations:
|
|---|
| 461 |
|
|---|
| 462 | *** In some cases, w.op is called before v.op; see binary_op1. ***
|
|---|
| 463 |
|
|---|
| 464 | v w z Action
|
|---|
| 465 | -------------------------------------------------------------------
|
|---|
| 466 | new new new v.op(v,w,z), w.op(v,w,z), z.op(v,w,z)
|
|---|
| 467 | new old new v.op(v,w,z), z.op(v,w,z), coerce(v,w,z), v.op(v,w,z)
|
|---|
| 468 | old new new w.op(v,w,z), z.op(v,w,z), coerce(v,w,z), v.op(v,w,z)
|
|---|
| 469 | old old new z.op(v,w,z), coerce(v,w,z), v.op(v,w,z)
|
|---|
| 470 | new new old v.op(v,w,z), w.op(v,w,z), coerce(v,w,z), v.op(v,w,z)
|
|---|
| 471 | new old old v.op(v,w,z), coerce(v,w,z), v.op(v,w,z)
|
|---|
| 472 | old new old w.op(v,w,z), coerce(v,w,z), v.op(v,w,z)
|
|---|
| 473 | old old old coerce(v,w,z), v.op(v,w,z)
|
|---|
| 474 |
|
|---|
| 475 | Legend:
|
|---|
| 476 | -------
|
|---|
| 477 | * new == new style number
|
|---|
| 478 | * old == old style number
|
|---|
| 479 | * Action indicates the order in which operations are tried until either
|
|---|
| 480 | a valid result is produced or an error occurs.
|
|---|
| 481 | * coerce(v,w,z) actually does: coerce(v,w), coerce(v,z), coerce(w,z) and
|
|---|
| 482 | only if z != Py_None; if z == Py_None, then it is treated as absent
|
|---|
| 483 | variable and only coerce(v,w) is tried.
|
|---|
| 484 |
|
|---|
| 485 | */
|
|---|
| 486 |
|
|---|
| 487 | static PyObject *
|
|---|
| 488 | ternary_op(PyObject *v,
|
|---|
| 489 | PyObject *w,
|
|---|
| 490 | PyObject *z,
|
|---|
| 491 | const int op_slot,
|
|---|
| 492 | const char *op_name)
|
|---|
| 493 | {
|
|---|
| 494 | PyNumberMethods *mv, *mw, *mz;
|
|---|
| 495 | PyObject *x = NULL;
|
|---|
| 496 | ternaryfunc slotv = NULL;
|
|---|
| 497 | ternaryfunc slotw = NULL;
|
|---|
| 498 | ternaryfunc slotz = NULL;
|
|---|
| 499 |
|
|---|
| 500 | mv = v->ob_type->tp_as_number;
|
|---|
| 501 | mw = w->ob_type->tp_as_number;
|
|---|
| 502 | if (mv != NULL && NEW_STYLE_NUMBER(v))
|
|---|
| 503 | slotv = NB_TERNOP(mv, op_slot);
|
|---|
| 504 | if (w->ob_type != v->ob_type &&
|
|---|
| 505 | mw != NULL && NEW_STYLE_NUMBER(w)) {
|
|---|
| 506 | slotw = NB_TERNOP(mw, op_slot);
|
|---|
| 507 | if (slotw == slotv)
|
|---|
| 508 | slotw = NULL;
|
|---|
| 509 | }
|
|---|
| 510 | if (slotv) {
|
|---|
| 511 | if (slotw && PyType_IsSubtype(w->ob_type, v->ob_type)) {
|
|---|
| 512 | x = slotw(v, w, z);
|
|---|
| 513 | if (x != Py_NotImplemented)
|
|---|
| 514 | return x;
|
|---|
| 515 | Py_DECREF(x); /* can't do it */
|
|---|
| 516 | slotw = NULL;
|
|---|
| 517 | }
|
|---|
| 518 | x = slotv(v, w, z);
|
|---|
| 519 | if (x != Py_NotImplemented)
|
|---|
| 520 | return x;
|
|---|
| 521 | Py_DECREF(x); /* can't do it */
|
|---|
| 522 | }
|
|---|
| 523 | if (slotw) {
|
|---|
| 524 | x = slotw(v, w, z);
|
|---|
| 525 | if (x != Py_NotImplemented)
|
|---|
| 526 | return x;
|
|---|
| 527 | Py_DECREF(x); /* can't do it */
|
|---|
| 528 | }
|
|---|
| 529 | mz = z->ob_type->tp_as_number;
|
|---|
| 530 | if (mz != NULL && NEW_STYLE_NUMBER(z)) {
|
|---|
| 531 | slotz = NB_TERNOP(mz, op_slot);
|
|---|
| 532 | if (slotz == slotv || slotz == slotw)
|
|---|
| 533 | slotz = NULL;
|
|---|
| 534 | if (slotz) {
|
|---|
| 535 | x = slotz(v, w, z);
|
|---|
| 536 | if (x != Py_NotImplemented)
|
|---|
| 537 | return x;
|
|---|
| 538 | Py_DECREF(x); /* can't do it */
|
|---|
| 539 | }
|
|---|
| 540 | }
|
|---|
| 541 |
|
|---|
| 542 | if (!NEW_STYLE_NUMBER(v) || !NEW_STYLE_NUMBER(w) ||
|
|---|
| 543 | (z != Py_None && !NEW_STYLE_NUMBER(z))) {
|
|---|
| 544 | /* we have an old style operand, coerce */
|
|---|
| 545 | PyObject *v1, *z1, *w2, *z2;
|
|---|
| 546 | int c;
|
|---|
| 547 |
|
|---|
| 548 | c = PyNumber_Coerce(&v, &w);
|
|---|
| 549 | if (c != 0)
|
|---|
| 550 | goto error3;
|
|---|
| 551 |
|
|---|
| 552 | /* Special case: if the third argument is None, it is
|
|---|
| 553 | treated as absent argument and not coerced. */
|
|---|
| 554 | if (z == Py_None) {
|
|---|
| 555 | if (v->ob_type->tp_as_number) {
|
|---|
| 556 | slotz = NB_TERNOP(v->ob_type->tp_as_number,
|
|---|
| 557 | op_slot);
|
|---|
| 558 | if (slotz)
|
|---|
| 559 | x = slotz(v, w, z);
|
|---|
| 560 | else
|
|---|
| 561 | c = -1;
|
|---|
| 562 | }
|
|---|
| 563 | else
|
|---|
| 564 | c = -1;
|
|---|
| 565 | goto error2;
|
|---|
| 566 | }
|
|---|
| 567 | v1 = v;
|
|---|
| 568 | z1 = z;
|
|---|
| 569 | c = PyNumber_Coerce(&v1, &z1);
|
|---|
| 570 | if (c != 0)
|
|---|
| 571 | goto error2;
|
|---|
| 572 | w2 = w;
|
|---|
| 573 | z2 = z1;
|
|---|
| 574 | c = PyNumber_Coerce(&w2, &z2);
|
|---|
| 575 | if (c != 0)
|
|---|
| 576 | goto error1;
|
|---|
| 577 |
|
|---|
| 578 | if (v1->ob_type->tp_as_number != NULL) {
|
|---|
| 579 | slotv = NB_TERNOP(v1->ob_type->tp_as_number,
|
|---|
| 580 | op_slot);
|
|---|
| 581 | if (slotv)
|
|---|
| 582 | x = slotv(v1, w2, z2);
|
|---|
| 583 | else
|
|---|
| 584 | c = -1;
|
|---|
| 585 | }
|
|---|
| 586 | else
|
|---|
| 587 | c = -1;
|
|---|
| 588 |
|
|---|
| 589 | Py_DECREF(w2);
|
|---|
| 590 | Py_DECREF(z2);
|
|---|
| 591 | error1:
|
|---|
| 592 | Py_DECREF(v1);
|
|---|
| 593 | Py_DECREF(z1);
|
|---|
| 594 | error2:
|
|---|
| 595 | Py_DECREF(v);
|
|---|
| 596 | Py_DECREF(w);
|
|---|
| 597 | error3:
|
|---|
| 598 | if (c >= 0)
|
|---|
| 599 | return x;
|
|---|
| 600 | }
|
|---|
| 601 |
|
|---|
| 602 | if (z == Py_None)
|
|---|
| 603 | PyErr_Format(
|
|---|
| 604 | PyExc_TypeError,
|
|---|
| 605 | "unsupported operand type(s) for ** or pow(): "
|
|---|
| 606 | "'%.100s' and '%.100s'",
|
|---|
| 607 | v->ob_type->tp_name,
|
|---|
| 608 | w->ob_type->tp_name);
|
|---|
| 609 | else
|
|---|
| 610 | PyErr_Format(
|
|---|
| 611 | PyExc_TypeError,
|
|---|
| 612 | "unsupported operand type(s) for pow(): "
|
|---|
| 613 | "'%.100s', '%.100s', '%.100s'",
|
|---|
| 614 | v->ob_type->tp_name,
|
|---|
| 615 | w->ob_type->tp_name,
|
|---|
| 616 | z->ob_type->tp_name);
|
|---|
| 617 | return NULL;
|
|---|
| 618 | }
|
|---|
| 619 |
|
|---|
| 620 | #define BINARY_FUNC(func, op, op_name) \
|
|---|
| 621 | PyObject * \
|
|---|
| 622 | func(PyObject *v, PyObject *w) { \
|
|---|
| 623 | return binary_op(v, w, NB_SLOT(op), op_name); \
|
|---|
| 624 | }
|
|---|
| 625 |
|
|---|
| 626 | BINARY_FUNC(PyNumber_Or, nb_or, "|")
|
|---|
| 627 | BINARY_FUNC(PyNumber_Xor, nb_xor, "^")
|
|---|
| 628 | BINARY_FUNC(PyNumber_And, nb_and, "&")
|
|---|
| 629 | BINARY_FUNC(PyNumber_Lshift, nb_lshift, "<<")
|
|---|
| 630 | BINARY_FUNC(PyNumber_Rshift, nb_rshift, ">>")
|
|---|
| 631 | BINARY_FUNC(PyNumber_Subtract, nb_subtract, "-")
|
|---|
| 632 | BINARY_FUNC(PyNumber_Divide, nb_divide, "/")
|
|---|
| 633 | BINARY_FUNC(PyNumber_Divmod, nb_divmod, "divmod()")
|
|---|
| 634 |
|
|---|
| 635 | PyObject *
|
|---|
| 636 | PyNumber_Add(PyObject *v, PyObject *w)
|
|---|
| 637 | {
|
|---|
| 638 | PyObject *result = binary_op1(v, w, NB_SLOT(nb_add));
|
|---|
| 639 | if (result == Py_NotImplemented) {
|
|---|
| 640 | PySequenceMethods *m = v->ob_type->tp_as_sequence;
|
|---|
| 641 | Py_DECREF(result);
|
|---|
| 642 | if (m && m->sq_concat) {
|
|---|
| 643 | return (*m->sq_concat)(v, w);
|
|---|
| 644 | }
|
|---|
| 645 | result = binop_type_error(v, w, "+");
|
|---|
| 646 | }
|
|---|
| 647 | return result;
|
|---|
| 648 | }
|
|---|
| 649 |
|
|---|
| 650 | static PyObject *
|
|---|
| 651 | sequence_repeat(ssizeargfunc repeatfunc, PyObject *seq, PyObject *n)
|
|---|
| 652 | {
|
|---|
| 653 | Py_ssize_t count;
|
|---|
| 654 | if (PyIndex_Check(n)) {
|
|---|
| 655 | count = PyNumber_AsSsize_t(n, PyExc_OverflowError);
|
|---|
| 656 | if (count == -1 && PyErr_Occurred())
|
|---|
| 657 | return NULL;
|
|---|
| 658 | }
|
|---|
| 659 | else {
|
|---|
| 660 | return type_error("can't multiply sequence by "
|
|---|
| 661 | "non-int of type '%.200s'", n);
|
|---|
| 662 | }
|
|---|
| 663 | return (*repeatfunc)(seq, count);
|
|---|
| 664 | }
|
|---|
| 665 |
|
|---|
| 666 | PyObject *
|
|---|
| 667 | PyNumber_Multiply(PyObject *v, PyObject *w)
|
|---|
| 668 | {
|
|---|
| 669 | PyObject *result = binary_op1(v, w, NB_SLOT(nb_multiply));
|
|---|
| 670 | if (result == Py_NotImplemented) {
|
|---|
| 671 | PySequenceMethods *mv = v->ob_type->tp_as_sequence;
|
|---|
| 672 | PySequenceMethods *mw = w->ob_type->tp_as_sequence;
|
|---|
| 673 | Py_DECREF(result);
|
|---|
| 674 | if (mv && mv->sq_repeat) {
|
|---|
| 675 | return sequence_repeat(mv->sq_repeat, v, w);
|
|---|
| 676 | }
|
|---|
| 677 | else if (mw && mw->sq_repeat) {
|
|---|
| 678 | return sequence_repeat(mw->sq_repeat, w, v);
|
|---|
| 679 | }
|
|---|
| 680 | result = binop_type_error(v, w, "*");
|
|---|
| 681 | }
|
|---|
| 682 | return result;
|
|---|
| 683 | }
|
|---|
| 684 |
|
|---|
| 685 | PyObject *
|
|---|
| 686 | PyNumber_FloorDivide(PyObject *v, PyObject *w)
|
|---|
| 687 | {
|
|---|
| 688 | /* XXX tp_flags test */
|
|---|
| 689 | return binary_op(v, w, NB_SLOT(nb_floor_divide), "//");
|
|---|
| 690 | }
|
|---|
| 691 |
|
|---|
| 692 | PyObject *
|
|---|
| 693 | PyNumber_TrueDivide(PyObject *v, PyObject *w)
|
|---|
| 694 | {
|
|---|
| 695 | /* XXX tp_flags test */
|
|---|
| 696 | return binary_op(v, w, NB_SLOT(nb_true_divide), "/");
|
|---|
| 697 | }
|
|---|
| 698 |
|
|---|
| 699 | PyObject *
|
|---|
| 700 | PyNumber_Remainder(PyObject *v, PyObject *w)
|
|---|
| 701 | {
|
|---|
| 702 | return binary_op(v, w, NB_SLOT(nb_remainder), "%");
|
|---|
| 703 | }
|
|---|
| 704 |
|
|---|
| 705 | PyObject *
|
|---|
| 706 | PyNumber_Power(PyObject *v, PyObject *w, PyObject *z)
|
|---|
| 707 | {
|
|---|
| 708 | return ternary_op(v, w, z, NB_SLOT(nb_power), "** or pow()");
|
|---|
| 709 | }
|
|---|
| 710 |
|
|---|
| 711 | /* Binary in-place operators */
|
|---|
| 712 |
|
|---|
| 713 | /* The in-place operators are defined to fall back to the 'normal',
|
|---|
| 714 | non in-place operations, if the in-place methods are not in place.
|
|---|
| 715 |
|
|---|
| 716 | - If the left hand object has the appropriate struct members, and
|
|---|
| 717 | they are filled, call the appropriate function and return the
|
|---|
| 718 | result. No coercion is done on the arguments; the left-hand object
|
|---|
| 719 | is the one the operation is performed on, and it's up to the
|
|---|
| 720 | function to deal with the right-hand object.
|
|---|
| 721 |
|
|---|
| 722 | - Otherwise, in-place modification is not supported. Handle it exactly as
|
|---|
| 723 | a non in-place operation of the same kind.
|
|---|
| 724 |
|
|---|
| 725 | */
|
|---|
| 726 |
|
|---|
| 727 | #define HASINPLACE(t) \
|
|---|
| 728 | PyType_HasFeature((t)->ob_type, Py_TPFLAGS_HAVE_INPLACEOPS)
|
|---|
| 729 |
|
|---|
| 730 | static PyObject *
|
|---|
| 731 | binary_iop1(PyObject *v, PyObject *w, const int iop_slot, const int op_slot)
|
|---|
| 732 | {
|
|---|
| 733 | PyNumberMethods *mv = v->ob_type->tp_as_number;
|
|---|
| 734 | if (mv != NULL && HASINPLACE(v)) {
|
|---|
| 735 | binaryfunc slot = NB_BINOP(mv, iop_slot);
|
|---|
| 736 | if (slot) {
|
|---|
| 737 | PyObject *x = (slot)(v, w);
|
|---|
| 738 | if (x != Py_NotImplemented) {
|
|---|
| 739 | return x;
|
|---|
| 740 | }
|
|---|
| 741 | Py_DECREF(x);
|
|---|
| 742 | }
|
|---|
| 743 | }
|
|---|
| 744 | return binary_op1(v, w, op_slot);
|
|---|
| 745 | }
|
|---|
| 746 |
|
|---|
| 747 | static PyObject *
|
|---|
| 748 | binary_iop(PyObject *v, PyObject *w, const int iop_slot, const int op_slot,
|
|---|
| 749 | const char *op_name)
|
|---|
| 750 | {
|
|---|
| 751 | PyObject *result = binary_iop1(v, w, iop_slot, op_slot);
|
|---|
| 752 | if (result == Py_NotImplemented) {
|
|---|
| 753 | Py_DECREF(result);
|
|---|
| 754 | return binop_type_error(v, w, op_name);
|
|---|
| 755 | }
|
|---|
| 756 | return result;
|
|---|
| 757 | }
|
|---|
| 758 |
|
|---|
| 759 | #define INPLACE_BINOP(func, iop, op, op_name) \
|
|---|
| 760 | PyObject * \
|
|---|
| 761 | func(PyObject *v, PyObject *w) { \
|
|---|
| 762 | return binary_iop(v, w, NB_SLOT(iop), NB_SLOT(op), op_name); \
|
|---|
| 763 | }
|
|---|
| 764 |
|
|---|
| 765 | INPLACE_BINOP(PyNumber_InPlaceOr, nb_inplace_or, nb_or, "|=")
|
|---|
| 766 | INPLACE_BINOP(PyNumber_InPlaceXor, nb_inplace_xor, nb_xor, "^=")
|
|---|
| 767 | INPLACE_BINOP(PyNumber_InPlaceAnd, nb_inplace_and, nb_and, "&=")
|
|---|
| 768 | INPLACE_BINOP(PyNumber_InPlaceLshift, nb_inplace_lshift, nb_lshift, "<<=")
|
|---|
| 769 | INPLACE_BINOP(PyNumber_InPlaceRshift, nb_inplace_rshift, nb_rshift, ">>=")
|
|---|
| 770 | INPLACE_BINOP(PyNumber_InPlaceSubtract, nb_inplace_subtract, nb_subtract, "-=")
|
|---|
| 771 | INPLACE_BINOP(PyNumber_InPlaceDivide, nb_inplace_divide, nb_divide, "/=")
|
|---|
| 772 |
|
|---|
| 773 | PyObject *
|
|---|
| 774 | PyNumber_InPlaceFloorDivide(PyObject *v, PyObject *w)
|
|---|
| 775 | {
|
|---|
| 776 | /* XXX tp_flags test */
|
|---|
| 777 | return binary_iop(v, w, NB_SLOT(nb_inplace_floor_divide),
|
|---|
| 778 | NB_SLOT(nb_floor_divide), "//=");
|
|---|
| 779 | }
|
|---|
| 780 |
|
|---|
| 781 | PyObject *
|
|---|
| 782 | PyNumber_InPlaceTrueDivide(PyObject *v, PyObject *w)
|
|---|
| 783 | {
|
|---|
| 784 | /* XXX tp_flags test */
|
|---|
| 785 | return binary_iop(v, w, NB_SLOT(nb_inplace_true_divide),
|
|---|
| 786 | NB_SLOT(nb_true_divide), "/=");
|
|---|
| 787 | }
|
|---|
| 788 |
|
|---|
| 789 | PyObject *
|
|---|
| 790 | PyNumber_InPlaceAdd(PyObject *v, PyObject *w)
|
|---|
| 791 | {
|
|---|
| 792 | PyObject *result = binary_iop1(v, w, NB_SLOT(nb_inplace_add),
|
|---|
| 793 | NB_SLOT(nb_add));
|
|---|
| 794 | if (result == Py_NotImplemented) {
|
|---|
| 795 | PySequenceMethods *m = v->ob_type->tp_as_sequence;
|
|---|
| 796 | Py_DECREF(result);
|
|---|
| 797 | if (m != NULL) {
|
|---|
| 798 | binaryfunc f = NULL;
|
|---|
| 799 | if (HASINPLACE(v))
|
|---|
| 800 | f = m->sq_inplace_concat;
|
|---|
| 801 | if (f == NULL)
|
|---|
| 802 | f = m->sq_concat;
|
|---|
| 803 | if (f != NULL)
|
|---|
| 804 | return (*f)(v, w);
|
|---|
| 805 | }
|
|---|
| 806 | result = binop_type_error(v, w, "+=");
|
|---|
| 807 | }
|
|---|
| 808 | return result;
|
|---|
| 809 | }
|
|---|
| 810 |
|
|---|
| 811 | PyObject *
|
|---|
| 812 | PyNumber_InPlaceMultiply(PyObject *v, PyObject *w)
|
|---|
| 813 | {
|
|---|
| 814 | PyObject *result = binary_iop1(v, w, NB_SLOT(nb_inplace_multiply),
|
|---|
| 815 | NB_SLOT(nb_multiply));
|
|---|
| 816 | if (result == Py_NotImplemented) {
|
|---|
| 817 | ssizeargfunc f = NULL;
|
|---|
| 818 | PySequenceMethods *mv = v->ob_type->tp_as_sequence;
|
|---|
| 819 | PySequenceMethods *mw = w->ob_type->tp_as_sequence;
|
|---|
| 820 | Py_DECREF(result);
|
|---|
| 821 | if (mv != NULL) {
|
|---|
| 822 | if (HASINPLACE(v))
|
|---|
| 823 | f = mv->sq_inplace_repeat;
|
|---|
| 824 | if (f == NULL)
|
|---|
| 825 | f = mv->sq_repeat;
|
|---|
| 826 | if (f != NULL)
|
|---|
| 827 | return sequence_repeat(f, v, w);
|
|---|
| 828 | }
|
|---|
| 829 | else if (mw != NULL) {
|
|---|
| 830 | /* Note that the right hand operand should not be
|
|---|
| 831 | * mutated in this case so sq_inplace_repeat is not
|
|---|
| 832 | * used. */
|
|---|
| 833 | if (mw->sq_repeat)
|
|---|
| 834 | return sequence_repeat(mw->sq_repeat, w, v);
|
|---|
| 835 | }
|
|---|
| 836 | result = binop_type_error(v, w, "*=");
|
|---|
| 837 | }
|
|---|
| 838 | return result;
|
|---|
| 839 | }
|
|---|
| 840 |
|
|---|
| 841 | PyObject *
|
|---|
| 842 | PyNumber_InPlaceRemainder(PyObject *v, PyObject *w)
|
|---|
| 843 | {
|
|---|
| 844 | return binary_iop(v, w, NB_SLOT(nb_inplace_remainder),
|
|---|
| 845 | NB_SLOT(nb_remainder), "%=");
|
|---|
| 846 | }
|
|---|
| 847 |
|
|---|
| 848 | PyObject *
|
|---|
| 849 | PyNumber_InPlacePower(PyObject *v, PyObject *w, PyObject *z)
|
|---|
| 850 | {
|
|---|
| 851 | if (HASINPLACE(v) && v->ob_type->tp_as_number &&
|
|---|
| 852 | v->ob_type->tp_as_number->nb_inplace_power != NULL) {
|
|---|
| 853 | return ternary_op(v, w, z, NB_SLOT(nb_inplace_power), "**=");
|
|---|
| 854 | }
|
|---|
| 855 | else {
|
|---|
| 856 | return ternary_op(v, w, z, NB_SLOT(nb_power), "**=");
|
|---|
| 857 | }
|
|---|
| 858 | }
|
|---|
| 859 |
|
|---|
| 860 |
|
|---|
| 861 | /* Unary operators and functions */
|
|---|
| 862 |
|
|---|
| 863 | PyObject *
|
|---|
| 864 | PyNumber_Negative(PyObject *o)
|
|---|
| 865 | {
|
|---|
| 866 | PyNumberMethods *m;
|
|---|
| 867 |
|
|---|
| 868 | if (o == NULL)
|
|---|
| 869 | return null_error();
|
|---|
| 870 | m = o->ob_type->tp_as_number;
|
|---|
| 871 | if (m && m->nb_negative)
|
|---|
| 872 | return (*m->nb_negative)(o);
|
|---|
| 873 |
|
|---|
| 874 | return type_error("bad operand type for unary -: '%.200s'", o);
|
|---|
| 875 | }
|
|---|
| 876 |
|
|---|
| 877 | PyObject *
|
|---|
| 878 | PyNumber_Positive(PyObject *o)
|
|---|
| 879 | {
|
|---|
| 880 | PyNumberMethods *m;
|
|---|
| 881 |
|
|---|
| 882 | if (o == NULL)
|
|---|
| 883 | return null_error();
|
|---|
| 884 | m = o->ob_type->tp_as_number;
|
|---|
| 885 | if (m && m->nb_positive)
|
|---|
| 886 | return (*m->nb_positive)(o);
|
|---|
| 887 |
|
|---|
| 888 | return type_error("bad operand type for unary +: '%.200s'", o);
|
|---|
| 889 | }
|
|---|
| 890 |
|
|---|
| 891 | PyObject *
|
|---|
| 892 | PyNumber_Invert(PyObject *o)
|
|---|
| 893 | {
|
|---|
| 894 | PyNumberMethods *m;
|
|---|
| 895 |
|
|---|
| 896 | if (o == NULL)
|
|---|
| 897 | return null_error();
|
|---|
| 898 | m = o->ob_type->tp_as_number;
|
|---|
| 899 | if (m && m->nb_invert)
|
|---|
| 900 | return (*m->nb_invert)(o);
|
|---|
| 901 |
|
|---|
| 902 | return type_error("bad operand type for unary ~: '%.200s'", o);
|
|---|
| 903 | }
|
|---|
| 904 |
|
|---|
| 905 | PyObject *
|
|---|
| 906 | PyNumber_Absolute(PyObject *o)
|
|---|
| 907 | {
|
|---|
| 908 | PyNumberMethods *m;
|
|---|
| 909 |
|
|---|
| 910 | if (o == NULL)
|
|---|
| 911 | return null_error();
|
|---|
| 912 | m = o->ob_type->tp_as_number;
|
|---|
| 913 | if (m && m->nb_absolute)
|
|---|
| 914 | return m->nb_absolute(o);
|
|---|
| 915 |
|
|---|
| 916 | return type_error("bad operand type for abs(): '%.200s'", o);
|
|---|
| 917 | }
|
|---|
| 918 |
|
|---|
| 919 | /* Add a check for embedded NULL-bytes in the argument. */
|
|---|
| 920 | static PyObject *
|
|---|
| 921 | int_from_string(const char *s, Py_ssize_t len)
|
|---|
| 922 | {
|
|---|
| 923 | char *end;
|
|---|
| 924 | PyObject *x;
|
|---|
| 925 |
|
|---|
| 926 | x = PyInt_FromString((char*)s, &end, 10);
|
|---|
| 927 | if (x == NULL)
|
|---|
| 928 | return NULL;
|
|---|
| 929 | if (end != s + len) {
|
|---|
| 930 | PyErr_SetString(PyExc_ValueError,
|
|---|
| 931 | "null byte in argument for int()");
|
|---|
| 932 | Py_DECREF(x);
|
|---|
| 933 | return NULL;
|
|---|
| 934 | }
|
|---|
| 935 | return x;
|
|---|
| 936 | }
|
|---|
| 937 |
|
|---|
| 938 | /* Return a Python Int or Long from the object item
|
|---|
| 939 | Raise TypeError if the result is not an int-or-long
|
|---|
| 940 | or if the object cannot be interpreted as an index.
|
|---|
| 941 | */
|
|---|
| 942 | PyObject *
|
|---|
| 943 | PyNumber_Index(PyObject *item)
|
|---|
| 944 | {
|
|---|
| 945 | PyObject *result = NULL;
|
|---|
| 946 | if (item == NULL)
|
|---|
| 947 | return null_error();
|
|---|
| 948 | if (PyInt_Check(item) || PyLong_Check(item)) {
|
|---|
| 949 | Py_INCREF(item);
|
|---|
| 950 | return item;
|
|---|
| 951 | }
|
|---|
| 952 | if (PyIndex_Check(item)) {
|
|---|
| 953 | result = item->ob_type->tp_as_number->nb_index(item);
|
|---|
| 954 | if (result &&
|
|---|
| 955 | !PyInt_Check(result) && !PyLong_Check(result)) {
|
|---|
| 956 | PyErr_Format(PyExc_TypeError,
|
|---|
| 957 | "__index__ returned non-(int,long) " \
|
|---|
| 958 | "(type %.200s)",
|
|---|
| 959 | result->ob_type->tp_name);
|
|---|
| 960 | Py_DECREF(result);
|
|---|
| 961 | return NULL;
|
|---|
| 962 | }
|
|---|
| 963 | }
|
|---|
| 964 | else {
|
|---|
| 965 | PyErr_Format(PyExc_TypeError,
|
|---|
| 966 | "'%.200s' object cannot be interpreted "
|
|---|
| 967 | "as an index", item->ob_type->tp_name);
|
|---|
| 968 | }
|
|---|
| 969 | return result;
|
|---|
| 970 | }
|
|---|
| 971 |
|
|---|
| 972 | /* Return an error on Overflow only if err is not NULL*/
|
|---|
| 973 |
|
|---|
| 974 | Py_ssize_t
|
|---|
| 975 | PyNumber_AsSsize_t(PyObject *item, PyObject *err)
|
|---|
| 976 | {
|
|---|
| 977 | Py_ssize_t result;
|
|---|
| 978 | PyObject *runerr;
|
|---|
| 979 | PyObject *value = PyNumber_Index(item);
|
|---|
| 980 | if (value == NULL)
|
|---|
| 981 | return -1;
|
|---|
| 982 |
|
|---|
| 983 | /* We're done if PyInt_AsSsize_t() returns without error. */
|
|---|
| 984 | result = PyInt_AsSsize_t(value);
|
|---|
| 985 | if (result != -1 || !(runerr = PyErr_Occurred()))
|
|---|
| 986 | goto finish;
|
|---|
| 987 |
|
|---|
| 988 | /* Error handling code -- only manage OverflowError differently */
|
|---|
| 989 | if (!PyErr_GivenExceptionMatches(runerr, PyExc_OverflowError))
|
|---|
| 990 | goto finish;
|
|---|
| 991 |
|
|---|
| 992 | PyErr_Clear();
|
|---|
| 993 | /* If no error-handling desired then the default clipping
|
|---|
| 994 | is sufficient.
|
|---|
| 995 | */
|
|---|
| 996 | if (!err) {
|
|---|
| 997 | assert(PyLong_Check(value));
|
|---|
| 998 | /* Whether or not it is less than or equal to
|
|---|
| 999 | zero is determined by the sign of ob_size
|
|---|
| 1000 | */
|
|---|
| 1001 | if (_PyLong_Sign(value) < 0)
|
|---|
| 1002 | result = PY_SSIZE_T_MIN;
|
|---|
| 1003 | else
|
|---|
| 1004 | result = PY_SSIZE_T_MAX;
|
|---|
| 1005 | }
|
|---|
| 1006 | else {
|
|---|
| 1007 | /* Otherwise replace the error with caller's error object. */
|
|---|
| 1008 | PyErr_Format(err,
|
|---|
| 1009 | "cannot fit '%.200s' into an index-sized integer",
|
|---|
| 1010 | item->ob_type->tp_name);
|
|---|
| 1011 | }
|
|---|
| 1012 |
|
|---|
| 1013 | finish:
|
|---|
| 1014 | Py_DECREF(value);
|
|---|
| 1015 | return result;
|
|---|
| 1016 | }
|
|---|
| 1017 |
|
|---|
| 1018 |
|
|---|
| 1019 | PyObject *
|
|---|
| 1020 | PyNumber_Int(PyObject *o)
|
|---|
| 1021 | {
|
|---|
| 1022 | PyNumberMethods *m;
|
|---|
| 1023 | const char *buffer;
|
|---|
| 1024 | Py_ssize_t buffer_len;
|
|---|
| 1025 |
|
|---|
| 1026 | if (o == NULL)
|
|---|
| 1027 | return null_error();
|
|---|
| 1028 | if (PyInt_CheckExact(o)) {
|
|---|
| 1029 | Py_INCREF(o);
|
|---|
| 1030 | return o;
|
|---|
| 1031 | }
|
|---|
| 1032 | m = o->ob_type->tp_as_number;
|
|---|
| 1033 | if (m && m->nb_int) { /* This should include subclasses of int */
|
|---|
| 1034 | PyObject *res = m->nb_int(o);
|
|---|
| 1035 | if (res && (!PyInt_Check(res) && !PyLong_Check(res))) {
|
|---|
| 1036 | PyErr_Format(PyExc_TypeError,
|
|---|
| 1037 | "__int__ returned non-int (type %.200s)",
|
|---|
| 1038 | res->ob_type->tp_name);
|
|---|
| 1039 | Py_DECREF(res);
|
|---|
| 1040 | return NULL;
|
|---|
| 1041 | }
|
|---|
| 1042 | return res;
|
|---|
| 1043 | }
|
|---|
| 1044 | if (PyInt_Check(o)) { /* A int subclass without nb_int */
|
|---|
| 1045 | PyIntObject *io = (PyIntObject*)o;
|
|---|
| 1046 | return PyInt_FromLong(io->ob_ival);
|
|---|
| 1047 | }
|
|---|
| 1048 | if (PyString_Check(o))
|
|---|
| 1049 | return int_from_string(PyString_AS_STRING(o),
|
|---|
| 1050 | PyString_GET_SIZE(o));
|
|---|
| 1051 | #ifdef Py_USING_UNICODE
|
|---|
| 1052 | if (PyUnicode_Check(o))
|
|---|
| 1053 | return PyInt_FromUnicode(PyUnicode_AS_UNICODE(o),
|
|---|
| 1054 | PyUnicode_GET_SIZE(o),
|
|---|
| 1055 | 10);
|
|---|
| 1056 | #endif
|
|---|
| 1057 | if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len))
|
|---|
| 1058 | return int_from_string((char*)buffer, buffer_len);
|
|---|
| 1059 |
|
|---|
| 1060 | return type_error("int() argument must be a string or a "
|
|---|
| 1061 | "number, not '%.200s'", o);
|
|---|
| 1062 | }
|
|---|
| 1063 |
|
|---|
| 1064 | /* Add a check for embedded NULL-bytes in the argument. */
|
|---|
| 1065 | static PyObject *
|
|---|
| 1066 | long_from_string(const char *s, Py_ssize_t len)
|
|---|
| 1067 | {
|
|---|
| 1068 | char *end;
|
|---|
| 1069 | PyObject *x;
|
|---|
| 1070 |
|
|---|
| 1071 | x = PyLong_FromString((char*)s, &end, 10);
|
|---|
| 1072 | if (x == NULL)
|
|---|
| 1073 | return NULL;
|
|---|
| 1074 | if (end != s + len) {
|
|---|
| 1075 | PyErr_SetString(PyExc_ValueError,
|
|---|
| 1076 | "null byte in argument for long()");
|
|---|
| 1077 | Py_DECREF(x);
|
|---|
| 1078 | return NULL;
|
|---|
| 1079 | }
|
|---|
| 1080 | return x;
|
|---|
| 1081 | }
|
|---|
| 1082 |
|
|---|
| 1083 | PyObject *
|
|---|
| 1084 | PyNumber_Long(PyObject *o)
|
|---|
| 1085 | {
|
|---|
| 1086 | PyNumberMethods *m;
|
|---|
| 1087 | const char *buffer;
|
|---|
| 1088 | Py_ssize_t buffer_len;
|
|---|
| 1089 |
|
|---|
| 1090 | if (o == NULL)
|
|---|
| 1091 | return null_error();
|
|---|
| 1092 | m = o->ob_type->tp_as_number;
|
|---|
| 1093 | if (m && m->nb_long) { /* This should include subclasses of long */
|
|---|
| 1094 | PyObject *res = m->nb_long(o);
|
|---|
| 1095 | if (res && (!PyInt_Check(res) && !PyLong_Check(res))) {
|
|---|
| 1096 | PyErr_Format(PyExc_TypeError,
|
|---|
| 1097 | "__long__ returned non-long (type %.200s)",
|
|---|
| 1098 | res->ob_type->tp_name);
|
|---|
| 1099 | Py_DECREF(res);
|
|---|
| 1100 | return NULL;
|
|---|
| 1101 | }
|
|---|
| 1102 | return res;
|
|---|
| 1103 | }
|
|---|
| 1104 | if (PyLong_Check(o)) /* A long subclass without nb_long */
|
|---|
| 1105 | return _PyLong_Copy((PyLongObject *)o);
|
|---|
| 1106 | if (PyString_Check(o))
|
|---|
| 1107 | /* need to do extra error checking that PyLong_FromString()
|
|---|
| 1108 | * doesn't do. In particular long('9.5') must raise an
|
|---|
| 1109 | * exception, not truncate the float.
|
|---|
| 1110 | */
|
|---|
| 1111 | return long_from_string(PyString_AS_STRING(o),
|
|---|
| 1112 | PyString_GET_SIZE(o));
|
|---|
| 1113 | #ifdef Py_USING_UNICODE
|
|---|
| 1114 | if (PyUnicode_Check(o))
|
|---|
| 1115 | /* The above check is done in PyLong_FromUnicode(). */
|
|---|
| 1116 | return PyLong_FromUnicode(PyUnicode_AS_UNICODE(o),
|
|---|
| 1117 | PyUnicode_GET_SIZE(o),
|
|---|
| 1118 | 10);
|
|---|
| 1119 | #endif
|
|---|
| 1120 | if (!PyObject_AsCharBuffer(o, &buffer, &buffer_len))
|
|---|
| 1121 | return long_from_string(buffer, buffer_len);
|
|---|
| 1122 |
|
|---|
| 1123 | return type_error("long() argument must be a string or a "
|
|---|
| 1124 | "number, not '%.200s'", o);
|
|---|
| 1125 | }
|
|---|
| 1126 |
|
|---|
| 1127 | PyObject *
|
|---|
| 1128 | PyNumber_Float(PyObject *o)
|
|---|
| 1129 | {
|
|---|
| 1130 | PyNumberMethods *m;
|
|---|
| 1131 |
|
|---|
| 1132 | if (o == NULL)
|
|---|
| 1133 | return null_error();
|
|---|
| 1134 | m = o->ob_type->tp_as_number;
|
|---|
| 1135 | if (m && m->nb_float) { /* This should include subclasses of float */
|
|---|
| 1136 | PyObject *res = m->nb_float(o);
|
|---|
| 1137 | if (res && !PyFloat_Check(res)) {
|
|---|
| 1138 | PyErr_Format(PyExc_TypeError,
|
|---|
| 1139 | "__float__ returned non-float (type %.200s)",
|
|---|
| 1140 | res->ob_type->tp_name);
|
|---|
| 1141 | Py_DECREF(res);
|
|---|
| 1142 | return NULL;
|
|---|
| 1143 | }
|
|---|
| 1144 | return res;
|
|---|
| 1145 | }
|
|---|
| 1146 | if (PyFloat_Check(o)) { /* A float subclass with nb_float == NULL */
|
|---|
| 1147 | PyFloatObject *po = (PyFloatObject *)o;
|
|---|
| 1148 | return PyFloat_FromDouble(po->ob_fval);
|
|---|
| 1149 | }
|
|---|
| 1150 | return PyFloat_FromString(o, NULL);
|
|---|
| 1151 | }
|
|---|
| 1152 |
|
|---|
| 1153 | /* Operations on sequences */
|
|---|
| 1154 |
|
|---|
| 1155 | int
|
|---|
| 1156 | PySequence_Check(PyObject *s)
|
|---|
| 1157 | {
|
|---|
| 1158 | if (s && PyInstance_Check(s))
|
|---|
| 1159 | return PyObject_HasAttrString(s, "__getitem__");
|
|---|
| 1160 | return s != NULL && s->ob_type->tp_as_sequence &&
|
|---|
| 1161 | s->ob_type->tp_as_sequence->sq_item != NULL;
|
|---|
| 1162 | }
|
|---|
| 1163 |
|
|---|
| 1164 | Py_ssize_t
|
|---|
| 1165 | PySequence_Size(PyObject *s)
|
|---|
| 1166 | {
|
|---|
| 1167 | PySequenceMethods *m;
|
|---|
| 1168 |
|
|---|
| 1169 | if (s == NULL) {
|
|---|
| 1170 | null_error();
|
|---|
| 1171 | return -1;
|
|---|
| 1172 | }
|
|---|
| 1173 |
|
|---|
| 1174 | m = s->ob_type->tp_as_sequence;
|
|---|
| 1175 | if (m && m->sq_length)
|
|---|
| 1176 | return m->sq_length(s);
|
|---|
| 1177 |
|
|---|
| 1178 | type_error("object of type '%.200s' has no len()", s);
|
|---|
| 1179 | return -1;
|
|---|
| 1180 | }
|
|---|
| 1181 |
|
|---|
| 1182 | #undef PySequence_Length
|
|---|
| 1183 | Py_ssize_t
|
|---|
| 1184 | PySequence_Length(PyObject *s)
|
|---|
| 1185 | {
|
|---|
| 1186 | return PySequence_Size(s);
|
|---|
| 1187 | }
|
|---|
| 1188 | #define PySequence_Length PySequence_Size
|
|---|
| 1189 |
|
|---|
| 1190 | PyObject *
|
|---|
| 1191 | PySequence_Concat(PyObject *s, PyObject *o)
|
|---|
| 1192 | {
|
|---|
| 1193 | PySequenceMethods *m;
|
|---|
| 1194 |
|
|---|
| 1195 | if (s == NULL || o == NULL)
|
|---|
| 1196 | return null_error();
|
|---|
| 1197 |
|
|---|
| 1198 | m = s->ob_type->tp_as_sequence;
|
|---|
| 1199 | if (m && m->sq_concat)
|
|---|
| 1200 | return m->sq_concat(s, o);
|
|---|
| 1201 |
|
|---|
| 1202 | /* Instances of user classes defining an __add__() method only
|
|---|
| 1203 | have an nb_add slot, not an sq_concat slot. So we fall back
|
|---|
| 1204 | to nb_add if both arguments appear to be sequences. */
|
|---|
| 1205 | if (PySequence_Check(s) && PySequence_Check(o)) {
|
|---|
| 1206 | PyObject *result = binary_op1(s, o, NB_SLOT(nb_add));
|
|---|
| 1207 | if (result != Py_NotImplemented)
|
|---|
| 1208 | return result;
|
|---|
| 1209 | Py_DECREF(result);
|
|---|
| 1210 | }
|
|---|
| 1211 | return type_error("'%.200s' object can't be concatenated", s);
|
|---|
| 1212 | }
|
|---|
| 1213 |
|
|---|
| 1214 | PyObject *
|
|---|
| 1215 | PySequence_Repeat(PyObject *o, Py_ssize_t count)
|
|---|
| 1216 | {
|
|---|
| 1217 | PySequenceMethods *m;
|
|---|
| 1218 |
|
|---|
| 1219 | if (o == NULL)
|
|---|
| 1220 | return null_error();
|
|---|
| 1221 |
|
|---|
| 1222 | m = o->ob_type->tp_as_sequence;
|
|---|
| 1223 | if (m && m->sq_repeat)
|
|---|
| 1224 | return m->sq_repeat(o, count);
|
|---|
| 1225 |
|
|---|
| 1226 | /* Instances of user classes defining a __mul__() method only
|
|---|
| 1227 | have an nb_multiply slot, not an sq_repeat slot. so we fall back
|
|---|
| 1228 | to nb_multiply if o appears to be a sequence. */
|
|---|
| 1229 | if (PySequence_Check(o)) {
|
|---|
| 1230 | PyObject *n, *result;
|
|---|
| 1231 | n = PyInt_FromSsize_t(count);
|
|---|
| 1232 | if (n == NULL)
|
|---|
| 1233 | return NULL;
|
|---|
| 1234 | result = binary_op1(o, n, NB_SLOT(nb_multiply));
|
|---|
| 1235 | Py_DECREF(n);
|
|---|
| 1236 | if (result != Py_NotImplemented)
|
|---|
| 1237 | return result;
|
|---|
| 1238 | Py_DECREF(result);
|
|---|
| 1239 | }
|
|---|
| 1240 | return type_error("'%.200s' object can't be repeated", o);
|
|---|
| 1241 | }
|
|---|
| 1242 |
|
|---|
| 1243 | PyObject *
|
|---|
| 1244 | PySequence_InPlaceConcat(PyObject *s, PyObject *o)
|
|---|
| 1245 | {
|
|---|
| 1246 | PySequenceMethods *m;
|
|---|
| 1247 |
|
|---|
| 1248 | if (s == NULL || o == NULL)
|
|---|
| 1249 | return null_error();
|
|---|
| 1250 |
|
|---|
| 1251 | m = s->ob_type->tp_as_sequence;
|
|---|
| 1252 | if (m && HASINPLACE(s) && m->sq_inplace_concat)
|
|---|
| 1253 | return m->sq_inplace_concat(s, o);
|
|---|
| 1254 | if (m && m->sq_concat)
|
|---|
| 1255 | return m->sq_concat(s, o);
|
|---|
| 1256 |
|
|---|
| 1257 | if (PySequence_Check(s) && PySequence_Check(o)) {
|
|---|
| 1258 | PyObject *result = binary_iop1(s, o, NB_SLOT(nb_inplace_add),
|
|---|
| 1259 | NB_SLOT(nb_add));
|
|---|
| 1260 | if (result != Py_NotImplemented)
|
|---|
| 1261 | return result;
|
|---|
| 1262 | Py_DECREF(result);
|
|---|
| 1263 | }
|
|---|
| 1264 | return type_error("'%.200s' object can't be concatenated", s);
|
|---|
| 1265 | }
|
|---|
| 1266 |
|
|---|
| 1267 | PyObject *
|
|---|
| 1268 | PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count)
|
|---|
| 1269 | {
|
|---|
| 1270 | PySequenceMethods *m;
|
|---|
| 1271 |
|
|---|
| 1272 | if (o == NULL)
|
|---|
| 1273 | return null_error();
|
|---|
| 1274 |
|
|---|
| 1275 | m = o->ob_type->tp_as_sequence;
|
|---|
| 1276 | if (m && HASINPLACE(o) && m->sq_inplace_repeat)
|
|---|
| 1277 | return m->sq_inplace_repeat(o, count);
|
|---|
| 1278 | if (m && m->sq_repeat)
|
|---|
| 1279 | return m->sq_repeat(o, count);
|
|---|
| 1280 |
|
|---|
| 1281 | if (PySequence_Check(o)) {
|
|---|
| 1282 | PyObject *n, *result;
|
|---|
| 1283 | n = PyInt_FromSsize_t(count);
|
|---|
| 1284 | if (n == NULL)
|
|---|
| 1285 | return NULL;
|
|---|
| 1286 | result = binary_iop1(o, n, NB_SLOT(nb_inplace_multiply),
|
|---|
| 1287 | NB_SLOT(nb_multiply));
|
|---|
| 1288 | Py_DECREF(n);
|
|---|
| 1289 | if (result != Py_NotImplemented)
|
|---|
| 1290 | return result;
|
|---|
| 1291 | Py_DECREF(result);
|
|---|
| 1292 | }
|
|---|
| 1293 | return type_error("'%.200s' object can't be repeated", o);
|
|---|
| 1294 | }
|
|---|
| 1295 |
|
|---|
| 1296 | PyObject *
|
|---|
| 1297 | PySequence_GetItem(PyObject *s, Py_ssize_t i)
|
|---|
| 1298 | {
|
|---|
| 1299 | PySequenceMethods *m;
|
|---|
| 1300 |
|
|---|
| 1301 | if (s == NULL)
|
|---|
| 1302 | return null_error();
|
|---|
| 1303 |
|
|---|
| 1304 | m = s->ob_type->tp_as_sequence;
|
|---|
| 1305 | if (m && m->sq_item) {
|
|---|
| 1306 | if (i < 0) {
|
|---|
| 1307 | if (m->sq_length) {
|
|---|
| 1308 | Py_ssize_t l = (*m->sq_length)(s);
|
|---|
| 1309 | if (l < 0)
|
|---|
| 1310 | return NULL;
|
|---|
| 1311 | i += l;
|
|---|
| 1312 | }
|
|---|
| 1313 | }
|
|---|
| 1314 | return m->sq_item(s, i);
|
|---|
| 1315 | }
|
|---|
| 1316 |
|
|---|
| 1317 | return type_error("'%.200s' object is unindexable", s);
|
|---|
| 1318 | }
|
|---|
| 1319 |
|
|---|
| 1320 | PyObject *
|
|---|
| 1321 | PySequence_GetSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2)
|
|---|
| 1322 | {
|
|---|
| 1323 | PySequenceMethods *m;
|
|---|
| 1324 | PyMappingMethods *mp;
|
|---|
| 1325 |
|
|---|
| 1326 | if (!s) return null_error();
|
|---|
| 1327 |
|
|---|
| 1328 | m = s->ob_type->tp_as_sequence;
|
|---|
| 1329 | if (m && m->sq_slice) {
|
|---|
| 1330 | if (i1 < 0 || i2 < 0) {
|
|---|
| 1331 | if (m->sq_length) {
|
|---|
| 1332 | Py_ssize_t l = (*m->sq_length)(s);
|
|---|
| 1333 | if (l < 0)
|
|---|
| 1334 | return NULL;
|
|---|
| 1335 | if (i1 < 0)
|
|---|
| 1336 | i1 += l;
|
|---|
| 1337 | if (i2 < 0)
|
|---|
| 1338 | i2 += l;
|
|---|
| 1339 | }
|
|---|
| 1340 | }
|
|---|
| 1341 | return m->sq_slice(s, i1, i2);
|
|---|
| 1342 | } else if ((mp = s->ob_type->tp_as_mapping) && mp->mp_subscript) {
|
|---|
| 1343 | PyObject *res;
|
|---|
| 1344 | PyObject *slice = _PySlice_FromIndices(i1, i2);
|
|---|
| 1345 | if (!slice)
|
|---|
| 1346 | return NULL;
|
|---|
| 1347 | res = mp->mp_subscript(s, slice);
|
|---|
| 1348 | Py_DECREF(slice);
|
|---|
| 1349 | return res;
|
|---|
| 1350 | }
|
|---|
| 1351 |
|
|---|
| 1352 | return type_error("'%.200s' object is unsliceable", s);
|
|---|
| 1353 | }
|
|---|
| 1354 |
|
|---|
| 1355 | int
|
|---|
| 1356 | PySequence_SetItem(PyObject *s, Py_ssize_t i, PyObject *o)
|
|---|
| 1357 | {
|
|---|
| 1358 | PySequenceMethods *m;
|
|---|
| 1359 |
|
|---|
| 1360 | if (s == NULL) {
|
|---|
| 1361 | null_error();
|
|---|
| 1362 | return -1;
|
|---|
| 1363 | }
|
|---|
| 1364 |
|
|---|
| 1365 | m = s->ob_type->tp_as_sequence;
|
|---|
| 1366 | if (m && m->sq_ass_item) {
|
|---|
| 1367 | if (i < 0) {
|
|---|
| 1368 | if (m->sq_length) {
|
|---|
| 1369 | Py_ssize_t l = (*m->sq_length)(s);
|
|---|
| 1370 | if (l < 0)
|
|---|
| 1371 | return -1;
|
|---|
| 1372 | i += l;
|
|---|
| 1373 | }
|
|---|
| 1374 | }
|
|---|
| 1375 | return m->sq_ass_item(s, i, o);
|
|---|
| 1376 | }
|
|---|
| 1377 |
|
|---|
| 1378 | type_error("'%.200s' object does not support item assignment", s);
|
|---|
| 1379 | return -1;
|
|---|
| 1380 | }
|
|---|
| 1381 |
|
|---|
| 1382 | int
|
|---|
| 1383 | PySequence_DelItem(PyObject *s, Py_ssize_t i)
|
|---|
| 1384 | {
|
|---|
| 1385 | PySequenceMethods *m;
|
|---|
| 1386 |
|
|---|
| 1387 | if (s == NULL) {
|
|---|
| 1388 | null_error();
|
|---|
| 1389 | return -1;
|
|---|
| 1390 | }
|
|---|
| 1391 |
|
|---|
| 1392 | m = s->ob_type->tp_as_sequence;
|
|---|
| 1393 | if (m && m->sq_ass_item) {
|
|---|
| 1394 | if (i < 0) {
|
|---|
| 1395 | if (m->sq_length) {
|
|---|
| 1396 | Py_ssize_t l = (*m->sq_length)(s);
|
|---|
| 1397 | if (l < 0)
|
|---|
| 1398 | return -1;
|
|---|
| 1399 | i += l;
|
|---|
| 1400 | }
|
|---|
| 1401 | }
|
|---|
| 1402 | return m->sq_ass_item(s, i, (PyObject *)NULL);
|
|---|
| 1403 | }
|
|---|
| 1404 |
|
|---|
| 1405 | type_error("'%.200s' object doesn't support item deletion", s);
|
|---|
| 1406 | return -1;
|
|---|
| 1407 | }
|
|---|
| 1408 |
|
|---|
| 1409 | int
|
|---|
| 1410 | PySequence_SetSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2, PyObject *o)
|
|---|
| 1411 | {
|
|---|
| 1412 | PySequenceMethods *m;
|
|---|
| 1413 | PyMappingMethods *mp;
|
|---|
| 1414 |
|
|---|
| 1415 | if (s == NULL) {
|
|---|
| 1416 | null_error();
|
|---|
| 1417 | return -1;
|
|---|
| 1418 | }
|
|---|
| 1419 |
|
|---|
| 1420 | m = s->ob_type->tp_as_sequence;
|
|---|
| 1421 | if (m && m->sq_ass_slice) {
|
|---|
| 1422 | if (i1 < 0 || i2 < 0) {
|
|---|
| 1423 | if (m->sq_length) {
|
|---|
| 1424 | Py_ssize_t l = (*m->sq_length)(s);
|
|---|
| 1425 | if (l < 0)
|
|---|
| 1426 | return -1;
|
|---|
| 1427 | if (i1 < 0)
|
|---|
| 1428 | i1 += l;
|
|---|
| 1429 | if (i2 < 0)
|
|---|
| 1430 | i2 += l;
|
|---|
| 1431 | }
|
|---|
| 1432 | }
|
|---|
| 1433 | return m->sq_ass_slice(s, i1, i2, o);
|
|---|
| 1434 | } else if ((mp = s->ob_type->tp_as_mapping) && mp->mp_ass_subscript) {
|
|---|
| 1435 | int res;
|
|---|
| 1436 | PyObject *slice = _PySlice_FromIndices(i1, i2);
|
|---|
| 1437 | if (!slice)
|
|---|
| 1438 | return -1;
|
|---|
| 1439 | res = mp->mp_ass_subscript(s, slice, o);
|
|---|
| 1440 | Py_DECREF(slice);
|
|---|
| 1441 | return res;
|
|---|
| 1442 | }
|
|---|
| 1443 |
|
|---|
| 1444 | type_error("'%.200s' object doesn't support slice assignment", s);
|
|---|
| 1445 | return -1;
|
|---|
| 1446 | }
|
|---|
| 1447 |
|
|---|
| 1448 | int
|
|---|
| 1449 | PySequence_DelSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2)
|
|---|
| 1450 | {
|
|---|
| 1451 | PySequenceMethods *m;
|
|---|
| 1452 |
|
|---|
| 1453 | if (s == NULL) {
|
|---|
| 1454 | null_error();
|
|---|
| 1455 | return -1;
|
|---|
| 1456 | }
|
|---|
| 1457 |
|
|---|
| 1458 | m = s->ob_type->tp_as_sequence;
|
|---|
| 1459 | if (m && m->sq_ass_slice) {
|
|---|
| 1460 | if (i1 < 0 || i2 < 0) {
|
|---|
| 1461 | if (m->sq_length) {
|
|---|
| 1462 | Py_ssize_t l = (*m->sq_length)(s);
|
|---|
| 1463 | if (l < 0)
|
|---|
| 1464 | return -1;
|
|---|
| 1465 | if (i1 < 0)
|
|---|
| 1466 | i1 += l;
|
|---|
| 1467 | if (i2 < 0)
|
|---|
| 1468 | i2 += l;
|
|---|
| 1469 | }
|
|---|
| 1470 | }
|
|---|
| 1471 | return m->sq_ass_slice(s, i1, i2, (PyObject *)NULL);
|
|---|
| 1472 | }
|
|---|
| 1473 | type_error("'%.200s' object doesn't support slice deletion", s);
|
|---|
| 1474 | return -1;
|
|---|
| 1475 | }
|
|---|
| 1476 |
|
|---|
| 1477 | PyObject *
|
|---|
| 1478 | PySequence_Tuple(PyObject *v)
|
|---|
| 1479 | {
|
|---|
| 1480 | PyObject *it; /* iter(v) */
|
|---|
| 1481 | Py_ssize_t n; /* guess for result tuple size */
|
|---|
| 1482 | PyObject *result;
|
|---|
| 1483 | Py_ssize_t j;
|
|---|
| 1484 |
|
|---|
| 1485 | if (v == NULL)
|
|---|
| 1486 | return null_error();
|
|---|
| 1487 |
|
|---|
| 1488 | /* Special-case the common tuple and list cases, for efficiency. */
|
|---|
| 1489 | if (PyTuple_CheckExact(v)) {
|
|---|
| 1490 | /* Note that we can't know whether it's safe to return
|
|---|
| 1491 | a tuple *subclass* instance as-is, hence the restriction
|
|---|
| 1492 | to exact tuples here. In contrast, lists always make
|
|---|
| 1493 | a copy, so there's no need for exactness below. */
|
|---|
| 1494 | Py_INCREF(v);
|
|---|
| 1495 | return v;
|
|---|
| 1496 | }
|
|---|
| 1497 | if (PyList_Check(v))
|
|---|
| 1498 | return PyList_AsTuple(v);
|
|---|
| 1499 |
|
|---|
| 1500 | /* Get iterator. */
|
|---|
| 1501 | it = PyObject_GetIter(v);
|
|---|
| 1502 | if (it == NULL)
|
|---|
| 1503 | return NULL;
|
|---|
| 1504 |
|
|---|
| 1505 | /* Guess result size and allocate space. */
|
|---|
| 1506 | n = _PyObject_LengthHint(v);
|
|---|
| 1507 | if (n < 0) {
|
|---|
| 1508 | if (!PyErr_ExceptionMatches(PyExc_TypeError) &&
|
|---|
| 1509 | !PyErr_ExceptionMatches(PyExc_AttributeError)) {
|
|---|
| 1510 | Py_DECREF(it);
|
|---|
| 1511 | return NULL;
|
|---|
| 1512 | }
|
|---|
| 1513 | PyErr_Clear();
|
|---|
| 1514 | n = 10; /* arbitrary */
|
|---|
| 1515 | }
|
|---|
| 1516 | result = PyTuple_New(n);
|
|---|
| 1517 | if (result == NULL)
|
|---|
| 1518 | goto Fail;
|
|---|
| 1519 |
|
|---|
| 1520 | /* Fill the tuple. */
|
|---|
| 1521 | for (j = 0; ; ++j) {
|
|---|
| 1522 | PyObject *item = PyIter_Next(it);
|
|---|
| 1523 | if (item == NULL) {
|
|---|
| 1524 | if (PyErr_Occurred())
|
|---|
| 1525 | goto Fail;
|
|---|
| 1526 | break;
|
|---|
| 1527 | }
|
|---|
| 1528 | if (j >= n) {
|
|---|
| 1529 | Py_ssize_t oldn = n;
|
|---|
| 1530 | /* The over-allocation strategy can grow a bit faster
|
|---|
| 1531 | than for lists because unlike lists the
|
|---|
| 1532 | over-allocation isn't permanent -- we reclaim
|
|---|
| 1533 | the excess before the end of this routine.
|
|---|
| 1534 | So, grow by ten and then add 25%.
|
|---|
| 1535 | */
|
|---|
| 1536 | n += 10;
|
|---|
| 1537 | n += n >> 2;
|
|---|
| 1538 | if (n < oldn) {
|
|---|
| 1539 | /* Check for overflow */
|
|---|
| 1540 | PyErr_NoMemory();
|
|---|
| 1541 | Py_DECREF(item);
|
|---|
| 1542 | goto Fail;
|
|---|
| 1543 | }
|
|---|
| 1544 | if (_PyTuple_Resize(&result, n) != 0) {
|
|---|
| 1545 | Py_DECREF(item);
|
|---|
| 1546 | goto Fail;
|
|---|
| 1547 | }
|
|---|
| 1548 | }
|
|---|
| 1549 | PyTuple_SET_ITEM(result, j, item);
|
|---|
| 1550 | }
|
|---|
| 1551 |
|
|---|
| 1552 | /* Cut tuple back if guess was too large. */
|
|---|
| 1553 | if (j < n &&
|
|---|
| 1554 | _PyTuple_Resize(&result, j) != 0)
|
|---|
| 1555 | goto Fail;
|
|---|
| 1556 |
|
|---|
| 1557 | Py_DECREF(it);
|
|---|
| 1558 | return result;
|
|---|
| 1559 |
|
|---|
| 1560 | Fail:
|
|---|
| 1561 | Py_XDECREF(result);
|
|---|
| 1562 | Py_DECREF(it);
|
|---|
| 1563 | return NULL;
|
|---|
| 1564 | }
|
|---|
| 1565 |
|
|---|
| 1566 | PyObject *
|
|---|
| 1567 | PySequence_List(PyObject *v)
|
|---|
| 1568 | {
|
|---|
| 1569 | PyObject *result; /* result list */
|
|---|
| 1570 | PyObject *rv; /* return value from PyList_Extend */
|
|---|
| 1571 |
|
|---|
| 1572 | if (v == NULL)
|
|---|
| 1573 | return null_error();
|
|---|
| 1574 |
|
|---|
| 1575 | result = PyList_New(0);
|
|---|
| 1576 | if (result == NULL)
|
|---|
| 1577 | return NULL;
|
|---|
| 1578 |
|
|---|
| 1579 | rv = _PyList_Extend((PyListObject *)result, v);
|
|---|
| 1580 | if (rv == NULL) {
|
|---|
| 1581 | Py_DECREF(result);
|
|---|
| 1582 | return NULL;
|
|---|
| 1583 | }
|
|---|
| 1584 | Py_DECREF(rv);
|
|---|
| 1585 | return result;
|
|---|
| 1586 | }
|
|---|
| 1587 |
|
|---|
| 1588 | PyObject *
|
|---|
| 1589 | PySequence_Fast(PyObject *v, const char *m)
|
|---|
| 1590 | {
|
|---|
| 1591 | PyObject *it;
|
|---|
| 1592 |
|
|---|
| 1593 | if (v == NULL)
|
|---|
| 1594 | return null_error();
|
|---|
| 1595 |
|
|---|
| 1596 | if (PyList_CheckExact(v) || PyTuple_CheckExact(v)) {
|
|---|
| 1597 | Py_INCREF(v);
|
|---|
| 1598 | return v;
|
|---|
| 1599 | }
|
|---|
| 1600 |
|
|---|
| 1601 | it = PyObject_GetIter(v);
|
|---|
| 1602 | if (it == NULL) {
|
|---|
| 1603 | if (PyErr_ExceptionMatches(PyExc_TypeError))
|
|---|
| 1604 | PyErr_SetString(PyExc_TypeError, m);
|
|---|
| 1605 | return NULL;
|
|---|
| 1606 | }
|
|---|
| 1607 |
|
|---|
| 1608 | v = PySequence_List(it);
|
|---|
| 1609 | Py_DECREF(it);
|
|---|
| 1610 |
|
|---|
| 1611 | return v;
|
|---|
| 1612 | }
|
|---|
| 1613 |
|
|---|
| 1614 | /* Iterate over seq. Result depends on the operation:
|
|---|
| 1615 | PY_ITERSEARCH_COUNT: -1 if error, else # of times obj appears in seq.
|
|---|
| 1616 | PY_ITERSEARCH_INDEX: 0-based index of first occurence of obj in seq;
|
|---|
| 1617 | set ValueError and return -1 if none found; also return -1 on error.
|
|---|
| 1618 | Py_ITERSEARCH_CONTAINS: return 1 if obj in seq, else 0; -1 on error.
|
|---|
| 1619 | */
|
|---|
| 1620 | Py_ssize_t
|
|---|
| 1621 | _PySequence_IterSearch(PyObject *seq, PyObject *obj, int operation)
|
|---|
| 1622 | {
|
|---|
| 1623 | Py_ssize_t n;
|
|---|
| 1624 | int wrapped; /* for PY_ITERSEARCH_INDEX, true iff n wrapped around */
|
|---|
| 1625 | PyObject *it; /* iter(seq) */
|
|---|
| 1626 |
|
|---|
| 1627 | if (seq == NULL || obj == NULL) {
|
|---|
| 1628 | null_error();
|
|---|
| 1629 | return -1;
|
|---|
| 1630 | }
|
|---|
| 1631 |
|
|---|
| 1632 | it = PyObject_GetIter(seq);
|
|---|
| 1633 | if (it == NULL) {
|
|---|
| 1634 | type_error("argument of type '%.200s' is not iterable", seq);
|
|---|
| 1635 | return -1;
|
|---|
| 1636 | }
|
|---|
| 1637 |
|
|---|
| 1638 | n = wrapped = 0;
|
|---|
| 1639 | for (;;) {
|
|---|
| 1640 | int cmp;
|
|---|
| 1641 | PyObject *item = PyIter_Next(it);
|
|---|
| 1642 | if (item == NULL) {
|
|---|
| 1643 | if (PyErr_Occurred())
|
|---|
| 1644 | goto Fail;
|
|---|
| 1645 | break;
|
|---|
| 1646 | }
|
|---|
| 1647 |
|
|---|
| 1648 | cmp = PyObject_RichCompareBool(obj, item, Py_EQ);
|
|---|
| 1649 | Py_DECREF(item);
|
|---|
| 1650 | if (cmp < 0)
|
|---|
| 1651 | goto Fail;
|
|---|
| 1652 | if (cmp > 0) {
|
|---|
| 1653 | switch (operation) {
|
|---|
| 1654 | case PY_ITERSEARCH_COUNT:
|
|---|
| 1655 | ++n;
|
|---|
| 1656 | if (n <= 0) {
|
|---|
| 1657 | /* XXX(nnorwitz): int means ssize_t */
|
|---|
| 1658 | PyErr_SetString(PyExc_OverflowError,
|
|---|
| 1659 | "count exceeds C int size");
|
|---|
| 1660 | goto Fail;
|
|---|
| 1661 | }
|
|---|
| 1662 | break;
|
|---|
| 1663 |
|
|---|
| 1664 | case PY_ITERSEARCH_INDEX:
|
|---|
| 1665 | if (wrapped) {
|
|---|
| 1666 | /* XXX(nnorwitz): int means ssize_t */
|
|---|
| 1667 | PyErr_SetString(PyExc_OverflowError,
|
|---|
| 1668 | "index exceeds C int size");
|
|---|
| 1669 | goto Fail;
|
|---|
| 1670 | }
|
|---|
| 1671 | goto Done;
|
|---|
| 1672 |
|
|---|
| 1673 | case PY_ITERSEARCH_CONTAINS:
|
|---|
| 1674 | n = 1;
|
|---|
| 1675 | goto Done;
|
|---|
| 1676 |
|
|---|
| 1677 | default:
|
|---|
| 1678 | assert(!"unknown operation");
|
|---|
| 1679 | }
|
|---|
| 1680 | }
|
|---|
| 1681 |
|
|---|
| 1682 | if (operation == PY_ITERSEARCH_INDEX) {
|
|---|
| 1683 | ++n;
|
|---|
| 1684 | if (n <= 0)
|
|---|
| 1685 | wrapped = 1;
|
|---|
| 1686 | }
|
|---|
| 1687 | }
|
|---|
| 1688 |
|
|---|
| 1689 | if (operation != PY_ITERSEARCH_INDEX)
|
|---|
| 1690 | goto Done;
|
|---|
| 1691 |
|
|---|
| 1692 | PyErr_SetString(PyExc_ValueError,
|
|---|
| 1693 | "sequence.index(x): x not in sequence");
|
|---|
| 1694 | /* fall into failure code */
|
|---|
| 1695 | Fail:
|
|---|
| 1696 | n = -1;
|
|---|
| 1697 | /* fall through */
|
|---|
| 1698 | Done:
|
|---|
| 1699 | Py_DECREF(it);
|
|---|
| 1700 | return n;
|
|---|
| 1701 |
|
|---|
| 1702 | }
|
|---|
| 1703 |
|
|---|
| 1704 | /* Return # of times o appears in s. */
|
|---|
| 1705 | Py_ssize_t
|
|---|
| 1706 | PySequence_Count(PyObject *s, PyObject *o)
|
|---|
| 1707 | {
|
|---|
| 1708 | return _PySequence_IterSearch(s, o, PY_ITERSEARCH_COUNT);
|
|---|
| 1709 | }
|
|---|
| 1710 |
|
|---|
| 1711 | /* Return -1 if error; 1 if ob in seq; 0 if ob not in seq.
|
|---|
| 1712 | * Use sq_contains if possible, else defer to _PySequence_IterSearch().
|
|---|
| 1713 | */
|
|---|
| 1714 | int
|
|---|
| 1715 | PySequence_Contains(PyObject *seq, PyObject *ob)
|
|---|
| 1716 | {
|
|---|
| 1717 | Py_ssize_t result;
|
|---|
| 1718 | if (PyType_HasFeature(seq->ob_type, Py_TPFLAGS_HAVE_SEQUENCE_IN)) {
|
|---|
| 1719 | PySequenceMethods *sqm = seq->ob_type->tp_as_sequence;
|
|---|
| 1720 | if (sqm != NULL && sqm->sq_contains != NULL)
|
|---|
| 1721 | return (*sqm->sq_contains)(seq, ob);
|
|---|
| 1722 | }
|
|---|
| 1723 | result = _PySequence_IterSearch(seq, ob, PY_ITERSEARCH_CONTAINS);
|
|---|
| 1724 | return Py_SAFE_DOWNCAST(result, Py_ssize_t, int);
|
|---|
| 1725 | }
|
|---|
| 1726 |
|
|---|
| 1727 | /* Backwards compatibility */
|
|---|
| 1728 | #undef PySequence_In
|
|---|
| 1729 | int
|
|---|
| 1730 | PySequence_In(PyObject *w, PyObject *v)
|
|---|
| 1731 | {
|
|---|
| 1732 | return PySequence_Contains(w, v);
|
|---|
| 1733 | }
|
|---|
| 1734 |
|
|---|
| 1735 | Py_ssize_t
|
|---|
| 1736 | PySequence_Index(PyObject *s, PyObject *o)
|
|---|
| 1737 | {
|
|---|
| 1738 | return _PySequence_IterSearch(s, o, PY_ITERSEARCH_INDEX);
|
|---|
| 1739 | }
|
|---|
| 1740 |
|
|---|
| 1741 | /* Operations on mappings */
|
|---|
| 1742 |
|
|---|
| 1743 | int
|
|---|
| 1744 | PyMapping_Check(PyObject *o)
|
|---|
| 1745 | {
|
|---|
| 1746 | if (o && PyInstance_Check(o))
|
|---|
| 1747 | return PyObject_HasAttrString(o, "__getitem__");
|
|---|
| 1748 |
|
|---|
| 1749 | return o && o->ob_type->tp_as_mapping &&
|
|---|
| 1750 | o->ob_type->tp_as_mapping->mp_subscript &&
|
|---|
| 1751 | !(o->ob_type->tp_as_sequence &&
|
|---|
| 1752 | o->ob_type->tp_as_sequence->sq_slice);
|
|---|
| 1753 | }
|
|---|
| 1754 |
|
|---|
| 1755 | Py_ssize_t
|
|---|
| 1756 | PyMapping_Size(PyObject *o)
|
|---|
| 1757 | {
|
|---|
| 1758 | PyMappingMethods *m;
|
|---|
| 1759 |
|
|---|
| 1760 | if (o == NULL) {
|
|---|
| 1761 | null_error();
|
|---|
| 1762 | return -1;
|
|---|
| 1763 | }
|
|---|
| 1764 |
|
|---|
| 1765 | m = o->ob_type->tp_as_mapping;
|
|---|
| 1766 | if (m && m->mp_length)
|
|---|
| 1767 | return m->mp_length(o);
|
|---|
| 1768 |
|
|---|
| 1769 | type_error("object of type '%.200s' has no len()", o);
|
|---|
| 1770 | return -1;
|
|---|
| 1771 | }
|
|---|
| 1772 |
|
|---|
| 1773 | #undef PyMapping_Length
|
|---|
| 1774 | Py_ssize_t
|
|---|
| 1775 | PyMapping_Length(PyObject *o)
|
|---|
| 1776 | {
|
|---|
| 1777 | return PyMapping_Size(o);
|
|---|
| 1778 | }
|
|---|
| 1779 | #define PyMapping_Length PyMapping_Size
|
|---|
| 1780 |
|
|---|
| 1781 | PyObject *
|
|---|
| 1782 | PyMapping_GetItemString(PyObject *o, char *key)
|
|---|
| 1783 | {
|
|---|
| 1784 | PyObject *okey, *r;
|
|---|
| 1785 |
|
|---|
| 1786 | if (key == NULL)
|
|---|
| 1787 | return null_error();
|
|---|
| 1788 |
|
|---|
| 1789 | okey = PyString_FromString(key);
|
|---|
| 1790 | if (okey == NULL)
|
|---|
| 1791 | return NULL;
|
|---|
| 1792 | r = PyObject_GetItem(o, okey);
|
|---|
| 1793 | Py_DECREF(okey);
|
|---|
| 1794 | return r;
|
|---|
| 1795 | }
|
|---|
| 1796 |
|
|---|
| 1797 | int
|
|---|
| 1798 | PyMapping_SetItemString(PyObject *o, char *key, PyObject *value)
|
|---|
| 1799 | {
|
|---|
| 1800 | PyObject *okey;
|
|---|
| 1801 | int r;
|
|---|
| 1802 |
|
|---|
| 1803 | if (key == NULL) {
|
|---|
| 1804 | null_error();
|
|---|
| 1805 | return -1;
|
|---|
| 1806 | }
|
|---|
| 1807 |
|
|---|
| 1808 | okey = PyString_FromString(key);
|
|---|
| 1809 | if (okey == NULL)
|
|---|
| 1810 | return -1;
|
|---|
| 1811 | r = PyObject_SetItem(o, okey, value);
|
|---|
| 1812 | Py_DECREF(okey);
|
|---|
| 1813 | return r;
|
|---|
| 1814 | }
|
|---|
| 1815 |
|
|---|
| 1816 | int
|
|---|
| 1817 | PyMapping_HasKeyString(PyObject *o, char *key)
|
|---|
| 1818 | {
|
|---|
| 1819 | PyObject *v;
|
|---|
| 1820 |
|
|---|
| 1821 | v = PyMapping_GetItemString(o, key);
|
|---|
| 1822 | if (v) {
|
|---|
| 1823 | Py_DECREF(v);
|
|---|
| 1824 | return 1;
|
|---|
| 1825 | }
|
|---|
| 1826 | PyErr_Clear();
|
|---|
| 1827 | return 0;
|
|---|
| 1828 | }
|
|---|
| 1829 |
|
|---|
| 1830 | int
|
|---|
| 1831 | PyMapping_HasKey(PyObject *o, PyObject *key)
|
|---|
| 1832 | {
|
|---|
| 1833 | PyObject *v;
|
|---|
| 1834 |
|
|---|
| 1835 | v = PyObject_GetItem(o, key);
|
|---|
| 1836 | if (v) {
|
|---|
| 1837 | Py_DECREF(v);
|
|---|
| 1838 | return 1;
|
|---|
| 1839 | }
|
|---|
| 1840 | PyErr_Clear();
|
|---|
| 1841 | return 0;
|
|---|
| 1842 | }
|
|---|
| 1843 |
|
|---|
| 1844 | /* Operations on callable objects */
|
|---|
| 1845 |
|
|---|
| 1846 | /* XXX PyCallable_Check() is in object.c */
|
|---|
| 1847 |
|
|---|
| 1848 | PyObject *
|
|---|
| 1849 | PyObject_CallObject(PyObject *o, PyObject *a)
|
|---|
| 1850 | {
|
|---|
| 1851 | return PyEval_CallObjectWithKeywords(o, a, NULL);
|
|---|
| 1852 | }
|
|---|
| 1853 |
|
|---|
| 1854 | PyObject *
|
|---|
| 1855 | PyObject_Call(PyObject *func, PyObject *arg, PyObject *kw)
|
|---|
| 1856 | {
|
|---|
| 1857 | ternaryfunc call;
|
|---|
| 1858 |
|
|---|
| 1859 | if ((call = func->ob_type->tp_call) != NULL) {
|
|---|
| 1860 | PyObject *result = (*call)(func, arg, kw);
|
|---|
| 1861 | if (result == NULL && !PyErr_Occurred())
|
|---|
| 1862 | PyErr_SetString(
|
|---|
| 1863 | PyExc_SystemError,
|
|---|
| 1864 | "NULL result without error in PyObject_Call");
|
|---|
| 1865 | return result;
|
|---|
| 1866 | }
|
|---|
| 1867 | PyErr_Format(PyExc_TypeError, "'%.200s' object is not callable",
|
|---|
| 1868 | func->ob_type->tp_name);
|
|---|
| 1869 | return NULL;
|
|---|
| 1870 | }
|
|---|
| 1871 |
|
|---|
| 1872 | static PyObject*
|
|---|
| 1873 | call_function_tail(PyObject *callable, PyObject *args)
|
|---|
| 1874 | {
|
|---|
| 1875 | PyObject *retval;
|
|---|
| 1876 |
|
|---|
| 1877 | if (args == NULL)
|
|---|
| 1878 | return NULL;
|
|---|
| 1879 |
|
|---|
| 1880 | if (!PyTuple_Check(args)) {
|
|---|
| 1881 | PyObject *a;
|
|---|
| 1882 |
|
|---|
| 1883 | a = PyTuple_New(1);
|
|---|
| 1884 | if (a == NULL) {
|
|---|
| 1885 | Py_DECREF(args);
|
|---|
| 1886 | return NULL;
|
|---|
| 1887 | }
|
|---|
| 1888 | PyTuple_SET_ITEM(a, 0, args);
|
|---|
| 1889 | args = a;
|
|---|
| 1890 | }
|
|---|
| 1891 | retval = PyObject_Call(callable, args, NULL);
|
|---|
| 1892 |
|
|---|
| 1893 | Py_DECREF(args);
|
|---|
| 1894 |
|
|---|
| 1895 | return retval;
|
|---|
| 1896 | }
|
|---|
| 1897 |
|
|---|
| 1898 | PyObject *
|
|---|
| 1899 | PyObject_CallFunction(PyObject *callable, char *format, ...)
|
|---|
| 1900 | {
|
|---|
| 1901 | va_list va;
|
|---|
| 1902 | PyObject *args;
|
|---|
| 1903 |
|
|---|
| 1904 | if (callable == NULL)
|
|---|
| 1905 | return null_error();
|
|---|
| 1906 |
|
|---|
| 1907 | if (format && *format) {
|
|---|
| 1908 | va_start(va, format);
|
|---|
| 1909 | args = Py_VaBuildValue(format, va);
|
|---|
| 1910 | va_end(va);
|
|---|
| 1911 | }
|
|---|
| 1912 | else
|
|---|
| 1913 | args = PyTuple_New(0);
|
|---|
| 1914 |
|
|---|
| 1915 | return call_function_tail(callable, args);
|
|---|
| 1916 | }
|
|---|
| 1917 |
|
|---|
| 1918 | PyObject *
|
|---|
| 1919 | _PyObject_CallFunction_SizeT(PyObject *callable, char *format, ...)
|
|---|
| 1920 | {
|
|---|
| 1921 | va_list va;
|
|---|
| 1922 | PyObject *args;
|
|---|
| 1923 |
|
|---|
| 1924 | if (callable == NULL)
|
|---|
| 1925 | return null_error();
|
|---|
| 1926 |
|
|---|
| 1927 | if (format && *format) {
|
|---|
| 1928 | va_start(va, format);
|
|---|
| 1929 | args = _Py_VaBuildValue_SizeT(format, va);
|
|---|
| 1930 | va_end(va);
|
|---|
| 1931 | }
|
|---|
| 1932 | else
|
|---|
| 1933 | args = PyTuple_New(0);
|
|---|
| 1934 |
|
|---|
| 1935 | return call_function_tail(callable, args);
|
|---|
| 1936 | }
|
|---|
| 1937 |
|
|---|
| 1938 | PyObject *
|
|---|
| 1939 | PyObject_CallMethod(PyObject *o, char *name, char *format, ...)
|
|---|
| 1940 | {
|
|---|
| 1941 | va_list va;
|
|---|
| 1942 | PyObject *args;
|
|---|
| 1943 | PyObject *func = NULL;
|
|---|
| 1944 | PyObject *retval = NULL;
|
|---|
| 1945 |
|
|---|
| 1946 | if (o == NULL || name == NULL)
|
|---|
| 1947 | return null_error();
|
|---|
| 1948 |
|
|---|
| 1949 | func = PyObject_GetAttrString(o, name);
|
|---|
| 1950 | if (func == NULL) {
|
|---|
| 1951 | PyErr_SetString(PyExc_AttributeError, name);
|
|---|
| 1952 | return 0;
|
|---|
| 1953 | }
|
|---|
| 1954 |
|
|---|
| 1955 | if (!PyCallable_Check(func)) {
|
|---|
| 1956 | type_error("attribute of type '%.200s' is not callable", func);
|
|---|
| 1957 | goto exit;
|
|---|
| 1958 | }
|
|---|
| 1959 |
|
|---|
| 1960 | if (format && *format) {
|
|---|
| 1961 | va_start(va, format);
|
|---|
| 1962 | args = Py_VaBuildValue(format, va);
|
|---|
| 1963 | va_end(va);
|
|---|
| 1964 | }
|
|---|
| 1965 | else
|
|---|
| 1966 | args = PyTuple_New(0);
|
|---|
| 1967 |
|
|---|
| 1968 | retval = call_function_tail(func, args);
|
|---|
| 1969 |
|
|---|
| 1970 | exit:
|
|---|
| 1971 | /* args gets consumed in call_function_tail */
|
|---|
| 1972 | Py_XDECREF(func);
|
|---|
| 1973 |
|
|---|
| 1974 | return retval;
|
|---|
| 1975 | }
|
|---|
| 1976 |
|
|---|
| 1977 | PyObject *
|
|---|
| 1978 | _PyObject_CallMethod_SizeT(PyObject *o, char *name, char *format, ...)
|
|---|
| 1979 | {
|
|---|
| 1980 | va_list va;
|
|---|
| 1981 | PyObject *args;
|
|---|
| 1982 | PyObject *func = NULL;
|
|---|
| 1983 | PyObject *retval = NULL;
|
|---|
| 1984 |
|
|---|
| 1985 | if (o == NULL || name == NULL)
|
|---|
| 1986 | return null_error();
|
|---|
| 1987 |
|
|---|
| 1988 | func = PyObject_GetAttrString(o, name);
|
|---|
| 1989 | if (func == NULL) {
|
|---|
| 1990 | PyErr_SetString(PyExc_AttributeError, name);
|
|---|
| 1991 | return 0;
|
|---|
| 1992 | }
|
|---|
| 1993 |
|
|---|
| 1994 | if (!PyCallable_Check(func)) {
|
|---|
| 1995 | type_error("attribute of type '%.200s' is not callable", func);
|
|---|
| 1996 | goto exit;
|
|---|
| 1997 | }
|
|---|
| 1998 |
|
|---|
| 1999 | if (format && *format) {
|
|---|
| 2000 | va_start(va, format);
|
|---|
| 2001 | args = _Py_VaBuildValue_SizeT(format, va);
|
|---|
| 2002 | va_end(va);
|
|---|
| 2003 | }
|
|---|
| 2004 | else
|
|---|
| 2005 | args = PyTuple_New(0);
|
|---|
| 2006 |
|
|---|
| 2007 | retval = call_function_tail(func, args);
|
|---|
| 2008 |
|
|---|
| 2009 | exit:
|
|---|
| 2010 | /* args gets consumed in call_function_tail */
|
|---|
| 2011 | Py_XDECREF(func);
|
|---|
| 2012 |
|
|---|
| 2013 | return retval;
|
|---|
| 2014 | }
|
|---|
| 2015 |
|
|---|
| 2016 |
|
|---|
| 2017 | static PyObject *
|
|---|
| 2018 | objargs_mktuple(va_list va)
|
|---|
| 2019 | {
|
|---|
| 2020 | int i, n = 0;
|
|---|
| 2021 | va_list countva;
|
|---|
| 2022 | PyObject *result, *tmp;
|
|---|
| 2023 |
|
|---|
| 2024 | #ifdef VA_LIST_IS_ARRAY
|
|---|
| 2025 | memcpy(countva, va, sizeof(va_list));
|
|---|
| 2026 | #else
|
|---|
| 2027 | #ifdef __va_copy
|
|---|
| 2028 | __va_copy(countva, va);
|
|---|
| 2029 | #else
|
|---|
| 2030 | countva = va;
|
|---|
| 2031 | #endif
|
|---|
| 2032 | #endif
|
|---|
| 2033 |
|
|---|
| 2034 | while (((PyObject *)va_arg(countva, PyObject *)) != NULL)
|
|---|
| 2035 | ++n;
|
|---|
| 2036 | result = PyTuple_New(n);
|
|---|
| 2037 | if (result != NULL && n > 0) {
|
|---|
| 2038 | for (i = 0; i < n; ++i) {
|
|---|
| 2039 | tmp = (PyObject *)va_arg(va, PyObject *);
|
|---|
| 2040 | PyTuple_SET_ITEM(result, i, tmp);
|
|---|
| 2041 | Py_INCREF(tmp);
|
|---|
| 2042 | }
|
|---|
| 2043 | }
|
|---|
| 2044 | return result;
|
|---|
| 2045 | }
|
|---|
| 2046 |
|
|---|
| 2047 | PyObject *
|
|---|
| 2048 | PyObject_CallMethodObjArgs(PyObject *callable, PyObject *name, ...)
|
|---|
| 2049 | {
|
|---|
| 2050 | PyObject *args, *tmp;
|
|---|
| 2051 | va_list vargs;
|
|---|
| 2052 |
|
|---|
| 2053 | if (callable == NULL || name == NULL)
|
|---|
| 2054 | return null_error();
|
|---|
| 2055 |
|
|---|
| 2056 | callable = PyObject_GetAttr(callable, name);
|
|---|
| 2057 | if (callable == NULL)
|
|---|
| 2058 | return NULL;
|
|---|
| 2059 |
|
|---|
| 2060 | /* count the args */
|
|---|
| 2061 | va_start(vargs, name);
|
|---|
| 2062 | args = objargs_mktuple(vargs);
|
|---|
| 2063 | va_end(vargs);
|
|---|
| 2064 | if (args == NULL) {
|
|---|
| 2065 | Py_DECREF(callable);
|
|---|
| 2066 | return NULL;
|
|---|
| 2067 | }
|
|---|
| 2068 | tmp = PyObject_Call(callable, args, NULL);
|
|---|
| 2069 | Py_DECREF(args);
|
|---|
| 2070 | Py_DECREF(callable);
|
|---|
| 2071 |
|
|---|
| 2072 | return tmp;
|
|---|
| 2073 | }
|
|---|
| 2074 |
|
|---|
| 2075 | PyObject *
|
|---|
| 2076 | PyObject_CallFunctionObjArgs(PyObject *callable, ...)
|
|---|
| 2077 | {
|
|---|
| 2078 | PyObject *args, *tmp;
|
|---|
| 2079 | va_list vargs;
|
|---|
| 2080 |
|
|---|
| 2081 | if (callable == NULL)
|
|---|
| 2082 | return null_error();
|
|---|
| 2083 |
|
|---|
| 2084 | /* count the args */
|
|---|
| 2085 | va_start(vargs, callable);
|
|---|
| 2086 | args = objargs_mktuple(vargs);
|
|---|
| 2087 | va_end(vargs);
|
|---|
| 2088 | if (args == NULL)
|
|---|
| 2089 | return NULL;
|
|---|
| 2090 | tmp = PyObject_Call(callable, args, NULL);
|
|---|
| 2091 | Py_DECREF(args);
|
|---|
| 2092 |
|
|---|
| 2093 | return tmp;
|
|---|
| 2094 | }
|
|---|
| 2095 |
|
|---|
| 2096 |
|
|---|
| 2097 | /* isinstance(), issubclass() */
|
|---|
| 2098 |
|
|---|
| 2099 | /* abstract_get_bases() has logically 4 return states, with a sort of 0th
|
|---|
| 2100 | * state that will almost never happen.
|
|---|
| 2101 | *
|
|---|
| 2102 | * 0. creating the __bases__ static string could get a MemoryError
|
|---|
| 2103 | * 1. getattr(cls, '__bases__') could raise an AttributeError
|
|---|
| 2104 | * 2. getattr(cls, '__bases__') could raise some other exception
|
|---|
| 2105 | * 3. getattr(cls, '__bases__') could return a tuple
|
|---|
| 2106 | * 4. getattr(cls, '__bases__') could return something other than a tuple
|
|---|
| 2107 | *
|
|---|
| 2108 | * Only state #3 is a non-error state and only it returns a non-NULL object
|
|---|
| 2109 | * (it returns the retrieved tuple).
|
|---|
| 2110 | *
|
|---|
| 2111 | * Any raised AttributeErrors are masked by clearing the exception and
|
|---|
| 2112 | * returning NULL. If an object other than a tuple comes out of __bases__,
|
|---|
| 2113 | * then again, the return value is NULL. So yes, these two situations
|
|---|
| 2114 | * produce exactly the same results: NULL is returned and no error is set.
|
|---|
| 2115 | *
|
|---|
| 2116 | * If some exception other than AttributeError is raised, then NULL is also
|
|---|
| 2117 | * returned, but the exception is not cleared. That's because we want the
|
|---|
| 2118 | * exception to be propagated along.
|
|---|
| 2119 | *
|
|---|
| 2120 | * Callers are expected to test for PyErr_Occurred() when the return value
|
|---|
| 2121 | * is NULL to decide whether a valid exception should be propagated or not.
|
|---|
| 2122 | * When there's no exception to propagate, it's customary for the caller to
|
|---|
| 2123 | * set a TypeError.
|
|---|
| 2124 | */
|
|---|
| 2125 | static PyObject *
|
|---|
| 2126 | abstract_get_bases(PyObject *cls)
|
|---|
| 2127 | {
|
|---|
| 2128 | static PyObject *__bases__ = NULL;
|
|---|
| 2129 | PyObject *bases;
|
|---|
| 2130 |
|
|---|
| 2131 | if (__bases__ == NULL) {
|
|---|
| 2132 | __bases__ = PyString_FromString("__bases__");
|
|---|
| 2133 | if (__bases__ == NULL)
|
|---|
| 2134 | return NULL;
|
|---|
| 2135 | }
|
|---|
| 2136 | bases = PyObject_GetAttr(cls, __bases__);
|
|---|
| 2137 | if (bases == NULL) {
|
|---|
| 2138 | if (PyErr_ExceptionMatches(PyExc_AttributeError))
|
|---|
| 2139 | PyErr_Clear();
|
|---|
| 2140 | return NULL;
|
|---|
| 2141 | }
|
|---|
| 2142 | if (!PyTuple_Check(bases)) {
|
|---|
| 2143 | Py_DECREF(bases);
|
|---|
| 2144 | return NULL;
|
|---|
| 2145 | }
|
|---|
| 2146 | return bases;
|
|---|
| 2147 | }
|
|---|
| 2148 |
|
|---|
| 2149 |
|
|---|
| 2150 | static int
|
|---|
| 2151 | abstract_issubclass(PyObject *derived, PyObject *cls)
|
|---|
| 2152 | {
|
|---|
| 2153 | PyObject *bases;
|
|---|
| 2154 | Py_ssize_t i, n;
|
|---|
| 2155 | int r = 0;
|
|---|
| 2156 |
|
|---|
| 2157 |
|
|---|
| 2158 | if (derived == cls)
|
|---|
| 2159 | return 1;
|
|---|
| 2160 |
|
|---|
| 2161 | if (PyTuple_Check(cls)) {
|
|---|
| 2162 | /* Not a general sequence -- that opens up the road to
|
|---|
| 2163 | recursion and stack overflow. */
|
|---|
| 2164 | n = PyTuple_GET_SIZE(cls);
|
|---|
| 2165 | for (i = 0; i < n; i++) {
|
|---|
| 2166 | if (derived == PyTuple_GET_ITEM(cls, i))
|
|---|
| 2167 | return 1;
|
|---|
| 2168 | }
|
|---|
| 2169 | }
|
|---|
| 2170 | bases = abstract_get_bases(derived);
|
|---|
| 2171 | if (bases == NULL) {
|
|---|
| 2172 | if (PyErr_Occurred())
|
|---|
| 2173 | return -1;
|
|---|
| 2174 | return 0;
|
|---|
| 2175 | }
|
|---|
| 2176 | n = PyTuple_GET_SIZE(bases);
|
|---|
| 2177 | for (i = 0; i < n; i++) {
|
|---|
| 2178 | r = abstract_issubclass(PyTuple_GET_ITEM(bases, i), cls);
|
|---|
| 2179 | if (r != 0)
|
|---|
| 2180 | break;
|
|---|
| 2181 | }
|
|---|
| 2182 |
|
|---|
| 2183 | Py_DECREF(bases);
|
|---|
| 2184 |
|
|---|
| 2185 | return r;
|
|---|
| 2186 | }
|
|---|
| 2187 |
|
|---|
| 2188 | static int
|
|---|
| 2189 | check_class(PyObject *cls, const char *error)
|
|---|
| 2190 | {
|
|---|
| 2191 | PyObject *bases = abstract_get_bases(cls);
|
|---|
| 2192 | if (bases == NULL) {
|
|---|
| 2193 | /* Do not mask errors. */
|
|---|
| 2194 | if (!PyErr_Occurred())
|
|---|
| 2195 | PyErr_SetString(PyExc_TypeError, error);
|
|---|
| 2196 | return 0;
|
|---|
| 2197 | }
|
|---|
| 2198 | Py_DECREF(bases);
|
|---|
| 2199 | return -1;
|
|---|
| 2200 | }
|
|---|
| 2201 |
|
|---|
| 2202 | static int
|
|---|
| 2203 | recursive_isinstance(PyObject *inst, PyObject *cls, int recursion_depth)
|
|---|
| 2204 | {
|
|---|
| 2205 | PyObject *icls;
|
|---|
| 2206 | static PyObject *__class__ = NULL;
|
|---|
| 2207 | int retval = 0;
|
|---|
| 2208 |
|
|---|
| 2209 | if (__class__ == NULL) {
|
|---|
| 2210 | __class__ = PyString_FromString("__class__");
|
|---|
| 2211 | if (__class__ == NULL)
|
|---|
| 2212 | return -1;
|
|---|
| 2213 | }
|
|---|
| 2214 |
|
|---|
| 2215 | if (PyClass_Check(cls) && PyInstance_Check(inst)) {
|
|---|
| 2216 | PyObject *inclass =
|
|---|
| 2217 | (PyObject*)((PyInstanceObject*)inst)->in_class;
|
|---|
| 2218 | retval = PyClass_IsSubclass(inclass, cls);
|
|---|
| 2219 | }
|
|---|
| 2220 | else if (PyType_Check(cls)) {
|
|---|
| 2221 | retval = PyObject_TypeCheck(inst, (PyTypeObject *)cls);
|
|---|
| 2222 | if (retval == 0) {
|
|---|
| 2223 | PyObject *c = PyObject_GetAttr(inst, __class__);
|
|---|
| 2224 | if (c == NULL) {
|
|---|
| 2225 | PyErr_Clear();
|
|---|
| 2226 | }
|
|---|
| 2227 | else {
|
|---|
| 2228 | if (c != (PyObject *)(inst->ob_type) &&
|
|---|
| 2229 | PyType_Check(c))
|
|---|
| 2230 | retval = PyType_IsSubtype(
|
|---|
| 2231 | (PyTypeObject *)c,
|
|---|
| 2232 | (PyTypeObject *)cls);
|
|---|
| 2233 | Py_DECREF(c);
|
|---|
| 2234 | }
|
|---|
| 2235 | }
|
|---|
| 2236 | }
|
|---|
| 2237 | else if (PyTuple_Check(cls)) {
|
|---|
| 2238 | Py_ssize_t i, n;
|
|---|
| 2239 |
|
|---|
| 2240 | if (!recursion_depth) {
|
|---|
| 2241 | PyErr_SetString(PyExc_RuntimeError,
|
|---|
| 2242 | "nest level of tuple too deep");
|
|---|
| 2243 | return -1;
|
|---|
| 2244 | }
|
|---|
| 2245 |
|
|---|
| 2246 | n = PyTuple_GET_SIZE(cls);
|
|---|
| 2247 | for (i = 0; i < n; i++) {
|
|---|
| 2248 | retval = recursive_isinstance(
|
|---|
| 2249 | inst,
|
|---|
| 2250 | PyTuple_GET_ITEM(cls, i),
|
|---|
| 2251 | recursion_depth-1);
|
|---|
| 2252 | if (retval != 0)
|
|---|
| 2253 | break;
|
|---|
| 2254 | }
|
|---|
| 2255 | }
|
|---|
| 2256 | else {
|
|---|
| 2257 | if (!check_class(cls,
|
|---|
| 2258 | "isinstance() arg 2 must be a class, type,"
|
|---|
| 2259 | " or tuple of classes and types"))
|
|---|
| 2260 | return -1;
|
|---|
| 2261 | icls = PyObject_GetAttr(inst, __class__);
|
|---|
| 2262 | if (icls == NULL) {
|
|---|
| 2263 | PyErr_Clear();
|
|---|
| 2264 | retval = 0;
|
|---|
| 2265 | }
|
|---|
| 2266 | else {
|
|---|
| 2267 | retval = abstract_issubclass(icls, cls);
|
|---|
| 2268 | Py_DECREF(icls);
|
|---|
| 2269 | }
|
|---|
| 2270 | }
|
|---|
| 2271 |
|
|---|
| 2272 | return retval;
|
|---|
| 2273 | }
|
|---|
| 2274 |
|
|---|
| 2275 | int
|
|---|
| 2276 | PyObject_IsInstance(PyObject *inst, PyObject *cls)
|
|---|
| 2277 | {
|
|---|
| 2278 | return recursive_isinstance(inst, cls, Py_GetRecursionLimit());
|
|---|
| 2279 | }
|
|---|
| 2280 |
|
|---|
| 2281 | static int
|
|---|
| 2282 | recursive_issubclass(PyObject *derived, PyObject *cls, int recursion_depth)
|
|---|
| 2283 | {
|
|---|
| 2284 | int retval;
|
|---|
| 2285 |
|
|---|
| 2286 | if (!PyClass_Check(derived) || !PyClass_Check(cls)) {
|
|---|
| 2287 | if (!check_class(derived,
|
|---|
| 2288 | "issubclass() arg 1 must be a class"))
|
|---|
| 2289 | return -1;
|
|---|
| 2290 |
|
|---|
| 2291 | if (PyTuple_Check(cls)) {
|
|---|
| 2292 | Py_ssize_t i;
|
|---|
| 2293 | Py_ssize_t n = PyTuple_GET_SIZE(cls);
|
|---|
| 2294 |
|
|---|
| 2295 | if (!recursion_depth) {
|
|---|
| 2296 | PyErr_SetString(PyExc_RuntimeError,
|
|---|
| 2297 | "nest level of tuple too deep");
|
|---|
| 2298 | return -1;
|
|---|
| 2299 | }
|
|---|
| 2300 | for (i = 0; i < n; ++i) {
|
|---|
| 2301 | retval = recursive_issubclass(
|
|---|
| 2302 | derived,
|
|---|
| 2303 | PyTuple_GET_ITEM(cls, i),
|
|---|
| 2304 | recursion_depth-1);
|
|---|
| 2305 | if (retval != 0) {
|
|---|
| 2306 | /* either found it, or got an error */
|
|---|
| 2307 | return retval;
|
|---|
| 2308 | }
|
|---|
| 2309 | }
|
|---|
| 2310 | return 0;
|
|---|
| 2311 | }
|
|---|
| 2312 | else {
|
|---|
| 2313 | if (!check_class(cls,
|
|---|
| 2314 | "issubclass() arg 2 must be a class"
|
|---|
| 2315 | " or tuple of classes"))
|
|---|
| 2316 | return -1;
|
|---|
| 2317 | }
|
|---|
| 2318 |
|
|---|
| 2319 | retval = abstract_issubclass(derived, cls);
|
|---|
| 2320 | }
|
|---|
| 2321 | else {
|
|---|
| 2322 | /* shortcut */
|
|---|
| 2323 | if (!(retval = (derived == cls)))
|
|---|
| 2324 | retval = PyClass_IsSubclass(derived, cls);
|
|---|
| 2325 | }
|
|---|
| 2326 |
|
|---|
| 2327 | return retval;
|
|---|
| 2328 | }
|
|---|
| 2329 |
|
|---|
| 2330 | int
|
|---|
| 2331 | PyObject_IsSubclass(PyObject *derived, PyObject *cls)
|
|---|
| 2332 | {
|
|---|
| 2333 | return recursive_issubclass(derived, cls, Py_GetRecursionLimit());
|
|---|
| 2334 | }
|
|---|
| 2335 |
|
|---|
| 2336 |
|
|---|
| 2337 | PyObject *
|
|---|
| 2338 | PyObject_GetIter(PyObject *o)
|
|---|
| 2339 | {
|
|---|
| 2340 | PyTypeObject *t = o->ob_type;
|
|---|
| 2341 | getiterfunc f = NULL;
|
|---|
| 2342 | if (PyType_HasFeature(t, Py_TPFLAGS_HAVE_ITER))
|
|---|
| 2343 | f = t->tp_iter;
|
|---|
| 2344 | if (f == NULL) {
|
|---|
| 2345 | if (PySequence_Check(o))
|
|---|
| 2346 | return PySeqIter_New(o);
|
|---|
| 2347 | return type_error("'%.200s' object is not iterable", o);
|
|---|
| 2348 | }
|
|---|
| 2349 | else {
|
|---|
| 2350 | PyObject *res = (*f)(o);
|
|---|
| 2351 | if (res != NULL && !PyIter_Check(res)) {
|
|---|
| 2352 | PyErr_Format(PyExc_TypeError,
|
|---|
| 2353 | "iter() returned non-iterator "
|
|---|
| 2354 | "of type '%.100s'",
|
|---|
| 2355 | res->ob_type->tp_name);
|
|---|
| 2356 | Py_DECREF(res);
|
|---|
| 2357 | res = NULL;
|
|---|
| 2358 | }
|
|---|
| 2359 | return res;
|
|---|
| 2360 | }
|
|---|
| 2361 | }
|
|---|
| 2362 |
|
|---|
| 2363 | /* Return next item.
|
|---|
| 2364 | * If an error occurs, return NULL. PyErr_Occurred() will be true.
|
|---|
| 2365 | * If the iteration terminates normally, return NULL and clear the
|
|---|
| 2366 | * PyExc_StopIteration exception (if it was set). PyErr_Occurred()
|
|---|
| 2367 | * will be false.
|
|---|
| 2368 | * Else return the next object. PyErr_Occurred() will be false.
|
|---|
| 2369 | */
|
|---|
| 2370 | PyObject *
|
|---|
| 2371 | PyIter_Next(PyObject *iter)
|
|---|
| 2372 | {
|
|---|
| 2373 | PyObject *result;
|
|---|
| 2374 | assert(PyIter_Check(iter));
|
|---|
| 2375 | result = (*iter->ob_type->tp_iternext)(iter);
|
|---|
| 2376 | if (result == NULL &&
|
|---|
| 2377 | PyErr_Occurred() &&
|
|---|
| 2378 | PyErr_ExceptionMatches(PyExc_StopIteration))
|
|---|
| 2379 | PyErr_Clear();
|
|---|
| 2380 | return result;
|
|---|
| 2381 | }
|
|---|