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