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