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