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