• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Abstract Object Interface (many thanks to Jim Fulton) */
2 
3 #include "Python.h"
4 #include "pycore_abstract.h"      // _PyIndex_Check()
5 #include "pycore_ceval.h"         // _Py_EnterRecursiveCall()
6 #include "pycore_object.h"        // _Py_CheckSlotResult()
7 #include "pycore_pyerrors.h"      // _PyErr_Occurred()
8 #include "pycore_pystate.h"       // _PyThreadState_GET()
9 #include "pycore_unionobject.h"   // _PyUnion_Check()
10 #include <ctype.h>
11 #include <stddef.h>               // offsetof()
12 #include "longintrepr.h"
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_CallNoArg(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 /* Binary in-place operators */
1156 
1157 /* The in-place operators are defined to fall back to the 'normal',
1158    non in-place operations, if the in-place methods are not in place.
1159 
1160    - If the left hand object has the appropriate struct members, and
1161      they are filled, call the appropriate function and return the
1162      result.  No coercion is done on the arguments; the left-hand object
1163      is the one the operation is performed on, and it's up to the
1164      function to deal with the right-hand object.
1165 
1166    - Otherwise, in-place modification is not supported. Handle it exactly as
1167      a non in-place operation of the same kind.
1168 
1169    */
1170 
1171 static PyObject *
binary_iop1(PyObject * v,PyObject * w,const int iop_slot,const int op_slot,const char * op_name)1172 binary_iop1(PyObject *v, PyObject *w, const int iop_slot, const int op_slot
1173 #ifndef NDEBUG
1174             , const char *op_name
1175 #endif
1176             )
1177 {
1178     PyNumberMethods *mv = Py_TYPE(v)->tp_as_number;
1179     if (mv != NULL) {
1180         binaryfunc slot = NB_BINOP(mv, iop_slot);
1181         if (slot) {
1182             PyObject *x = (slot)(v, w);
1183             assert(_Py_CheckSlotResult(v, op_name, x != NULL));
1184             if (x != Py_NotImplemented) {
1185                 return x;
1186             }
1187             Py_DECREF(x);
1188         }
1189     }
1190 #ifdef NDEBUG
1191     return binary_op1(v, w, op_slot);
1192 #else
1193     return binary_op1(v, w, op_slot, op_name);
1194 #endif
1195 }
1196 
1197 #ifdef NDEBUG
1198 #  define BINARY_IOP1(v, w, iop_slot, op_slot, op_name) binary_iop1(v, w, iop_slot, op_slot)
1199 #else
1200 #  define BINARY_IOP1(v, w, iop_slot, op_slot, op_name) binary_iop1(v, w, iop_slot, op_slot, op_name)
1201 #endif
1202 
1203 static PyObject *
binary_iop(PyObject * v,PyObject * w,const int iop_slot,const int op_slot,const char * op_name)1204 binary_iop(PyObject *v, PyObject *w, const int iop_slot, const int op_slot,
1205                 const char *op_name)
1206 {
1207     PyObject *result = BINARY_IOP1(v, w, iop_slot, op_slot, op_name);
1208     if (result == Py_NotImplemented) {
1209         Py_DECREF(result);
1210         return binop_type_error(v, w, op_name);
1211     }
1212     return result;
1213 }
1214 
1215 static PyObject *
ternary_iop(PyObject * v,PyObject * w,PyObject * z,const int iop_slot,const int op_slot,const char * op_name)1216 ternary_iop(PyObject *v, PyObject *w, PyObject *z, const int iop_slot, const int op_slot,
1217                 const char *op_name)
1218 {
1219     PyNumberMethods *mv = Py_TYPE(v)->tp_as_number;
1220     if (mv != NULL) {
1221         ternaryfunc slot = NB_TERNOP(mv, iop_slot);
1222         if (slot) {
1223             PyObject *x = (slot)(v, w, z);
1224             if (x != Py_NotImplemented) {
1225                 return x;
1226             }
1227             Py_DECREF(x);
1228         }
1229     }
1230     return ternary_op(v, w, z, op_slot, op_name);
1231 }
1232 
1233 #define INPLACE_BINOP(func, iop, op, op_name) \
1234     PyObject * \
1235     func(PyObject *v, PyObject *w) { \
1236         return binary_iop(v, w, NB_SLOT(iop), NB_SLOT(op), op_name); \
1237     }
1238 
1239 INPLACE_BINOP(PyNumber_InPlaceOr, nb_inplace_or, nb_or, "|=")
1240 INPLACE_BINOP(PyNumber_InPlaceXor, nb_inplace_xor, nb_xor, "^=")
1241 INPLACE_BINOP(PyNumber_InPlaceAnd, nb_inplace_and, nb_and, "&=")
1242 INPLACE_BINOP(PyNumber_InPlaceLshift, nb_inplace_lshift, nb_lshift, "<<=")
1243 INPLACE_BINOP(PyNumber_InPlaceRshift, nb_inplace_rshift, nb_rshift, ">>=")
1244 INPLACE_BINOP(PyNumber_InPlaceSubtract, nb_inplace_subtract, nb_subtract, "-=")
1245 INPLACE_BINOP(PyNumber_InMatrixMultiply, nb_inplace_matrix_multiply, nb_matrix_multiply, "@=")
1246 
1247 PyObject *
PyNumber_InPlaceFloorDivide(PyObject * v,PyObject * w)1248 PyNumber_InPlaceFloorDivide(PyObject *v, PyObject *w)
1249 {
1250     return binary_iop(v, w, NB_SLOT(nb_inplace_floor_divide),
1251                       NB_SLOT(nb_floor_divide), "//=");
1252 }
1253 
1254 PyObject *
PyNumber_InPlaceTrueDivide(PyObject * v,PyObject * w)1255 PyNumber_InPlaceTrueDivide(PyObject *v, PyObject *w)
1256 {
1257     return binary_iop(v, w, NB_SLOT(nb_inplace_true_divide),
1258                       NB_SLOT(nb_true_divide), "/=");
1259 }
1260 
1261 PyObject *
PyNumber_InPlaceAdd(PyObject * v,PyObject * w)1262 PyNumber_InPlaceAdd(PyObject *v, PyObject *w)
1263 {
1264     PyObject *result = BINARY_IOP1(v, w, NB_SLOT(nb_inplace_add),
1265                                    NB_SLOT(nb_add), "+=");
1266     if (result == Py_NotImplemented) {
1267         PySequenceMethods *m = Py_TYPE(v)->tp_as_sequence;
1268         Py_DECREF(result);
1269         if (m != NULL) {
1270             binaryfunc func = m->sq_inplace_concat;
1271             if (func == NULL)
1272                 func = m->sq_concat;
1273             if (func != NULL) {
1274                 result = func(v, w);
1275                 assert(_Py_CheckSlotResult(v, "+=", result != NULL));
1276                 return result;
1277             }
1278         }
1279         result = binop_type_error(v, w, "+=");
1280     }
1281     return result;
1282 }
1283 
1284 PyObject *
PyNumber_InPlaceMultiply(PyObject * v,PyObject * w)1285 PyNumber_InPlaceMultiply(PyObject *v, PyObject *w)
1286 {
1287     PyObject *result = BINARY_IOP1(v, w, NB_SLOT(nb_inplace_multiply),
1288                                    NB_SLOT(nb_multiply), "*=");
1289     if (result == Py_NotImplemented) {
1290         ssizeargfunc f = NULL;
1291         PySequenceMethods *mv = Py_TYPE(v)->tp_as_sequence;
1292         PySequenceMethods *mw = Py_TYPE(w)->tp_as_sequence;
1293         Py_DECREF(result);
1294         if (mv != NULL) {
1295             f = mv->sq_inplace_repeat;
1296             if (f == NULL)
1297                 f = mv->sq_repeat;
1298             if (f != NULL)
1299                 return sequence_repeat(f, v, w);
1300         }
1301         else if (mw != NULL) {
1302             /* Note that the right hand operand should not be
1303              * mutated in this case so sq_inplace_repeat is not
1304              * used. */
1305             if (mw->sq_repeat)
1306                 return sequence_repeat(mw->sq_repeat, w, v);
1307         }
1308         result = binop_type_error(v, w, "*=");
1309     }
1310     return result;
1311 }
1312 
1313 PyObject *
PyNumber_InPlaceMatrixMultiply(PyObject * v,PyObject * w)1314 PyNumber_InPlaceMatrixMultiply(PyObject *v, PyObject *w)
1315 {
1316     return binary_iop(v, w, NB_SLOT(nb_inplace_matrix_multiply),
1317                       NB_SLOT(nb_matrix_multiply), "@=");
1318 }
1319 
1320 PyObject *
PyNumber_InPlaceRemainder(PyObject * v,PyObject * w)1321 PyNumber_InPlaceRemainder(PyObject *v, PyObject *w)
1322 {
1323     return binary_iop(v, w, NB_SLOT(nb_inplace_remainder),
1324                             NB_SLOT(nb_remainder), "%=");
1325 }
1326 
1327 PyObject *
PyNumber_InPlacePower(PyObject * v,PyObject * w,PyObject * z)1328 PyNumber_InPlacePower(PyObject *v, PyObject *w, PyObject *z)
1329 {
1330     return ternary_iop(v, w, z, NB_SLOT(nb_inplace_power),
1331                                 NB_SLOT(nb_power), "**=");
1332 }
1333 
1334 
1335 /* Unary operators and functions */
1336 
1337 PyObject *
PyNumber_Negative(PyObject * o)1338 PyNumber_Negative(PyObject *o)
1339 {
1340     if (o == NULL) {
1341         return null_error();
1342     }
1343 
1344     PyNumberMethods *m = Py_TYPE(o)->tp_as_number;
1345     if (m && m->nb_negative) {
1346         PyObject *res = (*m->nb_negative)(o);
1347         assert(_Py_CheckSlotResult(o, "__neg__", res != NULL));
1348         return res;
1349     }
1350 
1351     return type_error("bad operand type for unary -: '%.200s'", o);
1352 }
1353 
1354 PyObject *
PyNumber_Positive(PyObject * o)1355 PyNumber_Positive(PyObject *o)
1356 {
1357     if (o == NULL) {
1358         return null_error();
1359     }
1360 
1361     PyNumberMethods *m = Py_TYPE(o)->tp_as_number;
1362     if (m && m->nb_positive) {
1363         PyObject *res = (*m->nb_positive)(o);
1364         assert(_Py_CheckSlotResult(o, "__pos__", res != NULL));
1365         return res;
1366     }
1367 
1368     return type_error("bad operand type for unary +: '%.200s'", o);
1369 }
1370 
1371 PyObject *
PyNumber_Invert(PyObject * o)1372 PyNumber_Invert(PyObject *o)
1373 {
1374     if (o == NULL) {
1375         return null_error();
1376     }
1377 
1378     PyNumberMethods *m = Py_TYPE(o)->tp_as_number;
1379     if (m && m->nb_invert) {
1380         PyObject *res = (*m->nb_invert)(o);
1381         assert(_Py_CheckSlotResult(o, "__invert__", res != NULL));
1382         return res;
1383     }
1384 
1385     return type_error("bad operand type for unary ~: '%.200s'", o);
1386 }
1387 
1388 PyObject *
PyNumber_Absolute(PyObject * o)1389 PyNumber_Absolute(PyObject *o)
1390 {
1391     if (o == NULL) {
1392         return null_error();
1393     }
1394 
1395     PyNumberMethods *m = Py_TYPE(o)->tp_as_number;
1396     if (m && m->nb_absolute) {
1397         PyObject *res = m->nb_absolute(o);
1398         assert(_Py_CheckSlotResult(o, "__abs__", res != NULL));
1399         return res;
1400     }
1401 
1402     return type_error("bad operand type for abs(): '%.200s'", o);
1403 }
1404 
1405 
1406 int
PyIndex_Check(PyObject * obj)1407 PyIndex_Check(PyObject *obj)
1408 {
1409     return _PyIndex_Check(obj);
1410 }
1411 
1412 
1413 /* Return a Python int from the object item.
1414    Can return an instance of int subclass.
1415    Raise TypeError if the result is not an int
1416    or if the object cannot be interpreted as an index.
1417 */
1418 PyObject *
_PyNumber_Index(PyObject * item)1419 _PyNumber_Index(PyObject *item)
1420 {
1421     if (item == NULL) {
1422         return null_error();
1423     }
1424 
1425     if (PyLong_Check(item)) {
1426         Py_INCREF(item);
1427         return item;
1428     }
1429     if (!_PyIndex_Check(item)) {
1430         PyErr_Format(PyExc_TypeError,
1431                      "'%.200s' object cannot be interpreted "
1432                      "as an integer", Py_TYPE(item)->tp_name);
1433         return NULL;
1434     }
1435 
1436     PyObject *result = Py_TYPE(item)->tp_as_number->nb_index(item);
1437     assert(_Py_CheckSlotResult(item, "__index__", result != NULL));
1438     if (!result || PyLong_CheckExact(result)) {
1439         return result;
1440     }
1441 
1442     if (!PyLong_Check(result)) {
1443         PyErr_Format(PyExc_TypeError,
1444                      "__index__ returned non-int (type %.200s)",
1445                      Py_TYPE(result)->tp_name);
1446         Py_DECREF(result);
1447         return NULL;
1448     }
1449     /* Issue #17576: warn if 'result' not of exact type int. */
1450     if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
1451             "__index__ returned non-int (type %.200s).  "
1452             "The ability to return an instance of a strict subclass of int "
1453             "is deprecated, and may be removed in a future version of Python.",
1454             Py_TYPE(result)->tp_name)) {
1455         Py_DECREF(result);
1456         return NULL;
1457     }
1458     return result;
1459 }
1460 
1461 /* Return an exact Python int from the object item.
1462    Raise TypeError if the result is not an int
1463    or if the object cannot be interpreted as an index.
1464 */
1465 PyObject *
PyNumber_Index(PyObject * item)1466 PyNumber_Index(PyObject *item)
1467 {
1468     PyObject *result = _PyNumber_Index(item);
1469     if (result != NULL && !PyLong_CheckExact(result)) {
1470         Py_SETREF(result, _PyLong_Copy((PyLongObject *)result));
1471     }
1472     return result;
1473 }
1474 
1475 /* Return an error on Overflow only if err is not NULL*/
1476 
1477 Py_ssize_t
PyNumber_AsSsize_t(PyObject * item,PyObject * err)1478 PyNumber_AsSsize_t(PyObject *item, PyObject *err)
1479 {
1480     Py_ssize_t result;
1481     PyObject *runerr;
1482     PyObject *value = _PyNumber_Index(item);
1483     if (value == NULL)
1484         return -1;
1485 
1486     /* We're done if PyLong_AsSsize_t() returns without error. */
1487     result = PyLong_AsSsize_t(value);
1488     if (result != -1)
1489         goto finish;
1490 
1491     PyThreadState *tstate = _PyThreadState_GET();
1492     runerr = _PyErr_Occurred(tstate);
1493     if (!runerr) {
1494         goto finish;
1495     }
1496 
1497     /* Error handling code -- only manage OverflowError differently */
1498     if (!PyErr_GivenExceptionMatches(runerr, PyExc_OverflowError)) {
1499         goto finish;
1500     }
1501     _PyErr_Clear(tstate);
1502 
1503     /* If no error-handling desired then the default clipping
1504        is sufficient. */
1505     if (!err) {
1506         assert(PyLong_Check(value));
1507         /* Whether or not it is less than or equal to
1508            zero is determined by the sign of ob_size
1509         */
1510         if (_PyLong_Sign(value) < 0)
1511             result = PY_SSIZE_T_MIN;
1512         else
1513             result = PY_SSIZE_T_MAX;
1514     }
1515     else {
1516         /* Otherwise replace the error with caller's error object. */
1517         _PyErr_Format(tstate, err,
1518                       "cannot fit '%.200s' into an index-sized integer",
1519                       Py_TYPE(item)->tp_name);
1520     }
1521 
1522  finish:
1523     Py_DECREF(value);
1524     return result;
1525 }
1526 
1527 
1528 PyObject *
PyNumber_Long(PyObject * o)1529 PyNumber_Long(PyObject *o)
1530 {
1531     PyObject *result;
1532     PyNumberMethods *m;
1533     PyObject *trunc_func;
1534     Py_buffer view;
1535     _Py_IDENTIFIER(__trunc__);
1536 
1537     if (o == NULL) {
1538         return null_error();
1539     }
1540 
1541     if (PyLong_CheckExact(o)) {
1542         Py_INCREF(o);
1543         return o;
1544     }
1545     m = Py_TYPE(o)->tp_as_number;
1546     if (m && m->nb_int) { /* This should include subclasses of int */
1547         /* Convert using the nb_int slot, which should return something
1548            of exact type int. */
1549         result = m->nb_int(o);
1550         assert(_Py_CheckSlotResult(o, "__int__", result != NULL));
1551         if (!result || PyLong_CheckExact(result)) {
1552             return result;
1553         }
1554 
1555         if (!PyLong_Check(result)) {
1556             PyErr_Format(PyExc_TypeError,
1557                          "__int__ returned non-int (type %.200s)",
1558                          Py_TYPE(result)->tp_name);
1559             Py_DECREF(result);
1560             return NULL;
1561         }
1562         /* Issue #17576: warn if 'result' not of exact type int. */
1563         if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
1564                 "__int__ returned non-int (type %.200s).  "
1565                 "The ability to return an instance of a strict subclass of int "
1566                 "is deprecated, and may be removed in a future version of Python.",
1567                 Py_TYPE(result)->tp_name)) {
1568             Py_DECREF(result);
1569             return NULL;
1570         }
1571         Py_SETREF(result, _PyLong_Copy((PyLongObject *)result));
1572         return result;
1573     }
1574     if (m && m->nb_index) {
1575         return PyNumber_Index(o);
1576     }
1577     trunc_func = _PyObject_LookupSpecial(o, &PyId___trunc__);
1578     if (trunc_func) {
1579         result = _PyObject_CallNoArg(trunc_func);
1580         Py_DECREF(trunc_func);
1581         if (result == NULL || PyLong_CheckExact(result)) {
1582             return result;
1583         }
1584         if (PyLong_Check(result)) {
1585             Py_SETREF(result, _PyLong_Copy((PyLongObject *)result));
1586             return result;
1587         }
1588         /* __trunc__ is specified to return an Integral type,
1589            but int() needs to return an int. */
1590         if (!PyIndex_Check(result)) {
1591             PyErr_Format(
1592                 PyExc_TypeError,
1593                 "__trunc__ returned non-Integral (type %.200s)",
1594                 Py_TYPE(result)->tp_name);
1595             Py_DECREF(result);
1596             return NULL;
1597         }
1598         Py_SETREF(result, PyNumber_Index(result));
1599         return result;
1600     }
1601     if (PyErr_Occurred())
1602         return NULL;
1603 
1604     if (PyUnicode_Check(o))
1605         /* The below check is done in PyLong_FromUnicodeObject(). */
1606         return PyLong_FromUnicodeObject(o, 10);
1607 
1608     if (PyBytes_Check(o))
1609         /* need to do extra error checking that PyLong_FromString()
1610          * doesn't do.  In particular int('9\x005') must raise an
1611          * exception, not truncate at the null.
1612          */
1613         return _PyLong_FromBytes(PyBytes_AS_STRING(o),
1614                                  PyBytes_GET_SIZE(o), 10);
1615 
1616     if (PyByteArray_Check(o))
1617         return _PyLong_FromBytes(PyByteArray_AS_STRING(o),
1618                                  PyByteArray_GET_SIZE(o), 10);
1619 
1620     if (PyObject_GetBuffer(o, &view, PyBUF_SIMPLE) == 0) {
1621         PyObject *bytes;
1622 
1623         /* Copy to NUL-terminated buffer. */
1624         bytes = PyBytes_FromStringAndSize((const char *)view.buf, view.len);
1625         if (bytes == NULL) {
1626             PyBuffer_Release(&view);
1627             return NULL;
1628         }
1629         result = _PyLong_FromBytes(PyBytes_AS_STRING(bytes),
1630                                    PyBytes_GET_SIZE(bytes), 10);
1631         Py_DECREF(bytes);
1632         PyBuffer_Release(&view);
1633         return result;
1634     }
1635 
1636     return type_error("int() argument must be a string, a bytes-like object "
1637                       "or a real number, not '%.200s'", o);
1638 }
1639 
1640 PyObject *
PyNumber_Float(PyObject * o)1641 PyNumber_Float(PyObject *o)
1642 {
1643     if (o == NULL) {
1644         return null_error();
1645     }
1646 
1647     if (PyFloat_CheckExact(o)) {
1648         return Py_NewRef(o);
1649     }
1650 
1651     PyNumberMethods *m = Py_TYPE(o)->tp_as_number;
1652     if (m && m->nb_float) { /* This should include subclasses of float */
1653         PyObject *res = m->nb_float(o);
1654         assert(_Py_CheckSlotResult(o, "__float__", res != NULL));
1655         if (!res || PyFloat_CheckExact(res)) {
1656             return res;
1657         }
1658 
1659         if (!PyFloat_Check(res)) {
1660             PyErr_Format(PyExc_TypeError,
1661                          "%.50s.__float__ returned non-float (type %.50s)",
1662                          Py_TYPE(o)->tp_name, Py_TYPE(res)->tp_name);
1663             Py_DECREF(res);
1664             return NULL;
1665         }
1666         /* Issue #26983: warn if 'res' not of exact type float. */
1667         if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
1668                 "%.50s.__float__ returned non-float (type %.50s).  "
1669                 "The ability to return an instance of a strict subclass of float "
1670                 "is deprecated, and may be removed in a future version of Python.",
1671                 Py_TYPE(o)->tp_name, Py_TYPE(res)->tp_name)) {
1672             Py_DECREF(res);
1673             return NULL;
1674         }
1675         double val = PyFloat_AS_DOUBLE(res);
1676         Py_DECREF(res);
1677         return PyFloat_FromDouble(val);
1678     }
1679 
1680     if (m && m->nb_index) {
1681         PyObject *res = _PyNumber_Index(o);
1682         if (!res) {
1683             return NULL;
1684         }
1685         double val = PyLong_AsDouble(res);
1686         Py_DECREF(res);
1687         if (val == -1.0 && PyErr_Occurred()) {
1688             return NULL;
1689         }
1690         return PyFloat_FromDouble(val);
1691     }
1692 
1693     /* A float subclass with nb_float == NULL */
1694     if (PyFloat_Check(o)) {
1695         return PyFloat_FromDouble(PyFloat_AS_DOUBLE(o));
1696     }
1697     return PyFloat_FromString(o);
1698 }
1699 
1700 
1701 PyObject *
PyNumber_ToBase(PyObject * n,int base)1702 PyNumber_ToBase(PyObject *n, int base)
1703 {
1704     if (!(base == 2 || base == 8 || base == 10 || base == 16)) {
1705         PyErr_SetString(PyExc_SystemError,
1706                         "PyNumber_ToBase: base must be 2, 8, 10 or 16");
1707         return NULL;
1708     }
1709     PyObject *index = _PyNumber_Index(n);
1710     if (!index)
1711         return NULL;
1712     PyObject *res = _PyLong_Format(index, base);
1713     Py_DECREF(index);
1714     return res;
1715 }
1716 
1717 
1718 /* Operations on sequences */
1719 
1720 int
PySequence_Check(PyObject * s)1721 PySequence_Check(PyObject *s)
1722 {
1723     if (PyDict_Check(s))
1724         return 0;
1725     return Py_TYPE(s)->tp_as_sequence &&
1726         Py_TYPE(s)->tp_as_sequence->sq_item != NULL;
1727 }
1728 
1729 Py_ssize_t
PySequence_Size(PyObject * s)1730 PySequence_Size(PyObject *s)
1731 {
1732     if (s == NULL) {
1733         null_error();
1734         return -1;
1735     }
1736 
1737     PySequenceMethods *m = Py_TYPE(s)->tp_as_sequence;
1738     if (m && m->sq_length) {
1739         Py_ssize_t len = m->sq_length(s);
1740         assert(_Py_CheckSlotResult(s, "__len__", len >= 0));
1741         return len;
1742     }
1743 
1744     if (Py_TYPE(s)->tp_as_mapping && Py_TYPE(s)->tp_as_mapping->mp_length) {
1745         type_error("%.200s is not a sequence", s);
1746         return -1;
1747     }
1748     type_error("object of type '%.200s' has no len()", s);
1749     return -1;
1750 }
1751 
1752 #undef PySequence_Length
1753 Py_ssize_t
PySequence_Length(PyObject * s)1754 PySequence_Length(PyObject *s)
1755 {
1756     return PySequence_Size(s);
1757 }
1758 #define PySequence_Length PySequence_Size
1759 
1760 PyObject *
PySequence_Concat(PyObject * s,PyObject * o)1761 PySequence_Concat(PyObject *s, PyObject *o)
1762 {
1763     if (s == NULL || o == NULL) {
1764         return null_error();
1765     }
1766 
1767     PySequenceMethods *m = Py_TYPE(s)->tp_as_sequence;
1768     if (m && m->sq_concat) {
1769         PyObject *res = m->sq_concat(s, o);
1770         assert(_Py_CheckSlotResult(s, "+", res != NULL));
1771         return res;
1772     }
1773 
1774     /* Instances of user classes defining an __add__() method only
1775        have an nb_add slot, not an sq_concat slot.      So we fall back
1776        to nb_add if both arguments appear to be sequences. */
1777     if (PySequence_Check(s) && PySequence_Check(o)) {
1778         PyObject *result = BINARY_OP1(s, o, NB_SLOT(nb_add), "+");
1779         if (result != Py_NotImplemented)
1780             return result;
1781         Py_DECREF(result);
1782     }
1783     return type_error("'%.200s' object can't be concatenated", s);
1784 }
1785 
1786 PyObject *
PySequence_Repeat(PyObject * o,Py_ssize_t count)1787 PySequence_Repeat(PyObject *o, Py_ssize_t count)
1788 {
1789     if (o == NULL) {
1790         return null_error();
1791     }
1792 
1793     PySequenceMethods *m = Py_TYPE(o)->tp_as_sequence;
1794     if (m && m->sq_repeat) {
1795         PyObject *res = m->sq_repeat(o, count);
1796         assert(_Py_CheckSlotResult(o, "*", res != NULL));
1797         return res;
1798     }
1799 
1800     /* Instances of user classes defining a __mul__() method only
1801        have an nb_multiply slot, not an sq_repeat slot. so we fall back
1802        to nb_multiply if o appears to be a sequence. */
1803     if (PySequence_Check(o)) {
1804         PyObject *n, *result;
1805         n = PyLong_FromSsize_t(count);
1806         if (n == NULL)
1807             return NULL;
1808         result = BINARY_OP1(o, n, NB_SLOT(nb_multiply), "*");
1809         Py_DECREF(n);
1810         if (result != Py_NotImplemented)
1811             return result;
1812         Py_DECREF(result);
1813     }
1814     return type_error("'%.200s' object can't be repeated", o);
1815 }
1816 
1817 PyObject *
PySequence_InPlaceConcat(PyObject * s,PyObject * o)1818 PySequence_InPlaceConcat(PyObject *s, PyObject *o)
1819 {
1820     if (s == NULL || o == NULL) {
1821         return null_error();
1822     }
1823 
1824     PySequenceMethods *m = Py_TYPE(s)->tp_as_sequence;
1825     if (m && m->sq_inplace_concat) {
1826         PyObject *res = m->sq_inplace_concat(s, o);
1827         assert(_Py_CheckSlotResult(s, "+=", res != NULL));
1828         return res;
1829     }
1830     if (m && m->sq_concat) {
1831         PyObject *res = m->sq_concat(s, o);
1832         assert(_Py_CheckSlotResult(s, "+", res != NULL));
1833         return res;
1834     }
1835 
1836     if (PySequence_Check(s) && PySequence_Check(o)) {
1837         PyObject *result = BINARY_IOP1(s, o, NB_SLOT(nb_inplace_add),
1838                                        NB_SLOT(nb_add), "+=");
1839         if (result != Py_NotImplemented)
1840             return result;
1841         Py_DECREF(result);
1842     }
1843     return type_error("'%.200s' object can't be concatenated", s);
1844 }
1845 
1846 PyObject *
PySequence_InPlaceRepeat(PyObject * o,Py_ssize_t count)1847 PySequence_InPlaceRepeat(PyObject *o, Py_ssize_t count)
1848 {
1849     if (o == NULL) {
1850         return null_error();
1851     }
1852 
1853     PySequenceMethods *m = Py_TYPE(o)->tp_as_sequence;
1854     if (m && m->sq_inplace_repeat) {
1855         PyObject *res = m->sq_inplace_repeat(o, count);
1856         assert(_Py_CheckSlotResult(o, "*=", res != NULL));
1857         return res;
1858     }
1859     if (m && m->sq_repeat) {
1860         PyObject *res = m->sq_repeat(o, count);
1861         assert(_Py_CheckSlotResult(o, "*", res != NULL));
1862         return res;
1863     }
1864 
1865     if (PySequence_Check(o)) {
1866         PyObject *n, *result;
1867         n = PyLong_FromSsize_t(count);
1868         if (n == NULL)
1869             return NULL;
1870         result = BINARY_IOP1(o, n, NB_SLOT(nb_inplace_multiply),
1871                              NB_SLOT(nb_multiply), "*=");
1872         Py_DECREF(n);
1873         if (result != Py_NotImplemented)
1874             return result;
1875         Py_DECREF(result);
1876     }
1877     return type_error("'%.200s' object can't be repeated", o);
1878 }
1879 
1880 PyObject *
PySequence_GetItem(PyObject * s,Py_ssize_t i)1881 PySequence_GetItem(PyObject *s, Py_ssize_t i)
1882 {
1883     if (s == NULL) {
1884         return null_error();
1885     }
1886 
1887     PySequenceMethods *m = Py_TYPE(s)->tp_as_sequence;
1888     if (m && m->sq_item) {
1889         if (i < 0) {
1890             if (m->sq_length) {
1891                 Py_ssize_t l = (*m->sq_length)(s);
1892                 assert(_Py_CheckSlotResult(s, "__len__", l >= 0));
1893                 if (l < 0) {
1894                     return NULL;
1895                 }
1896                 i += l;
1897             }
1898         }
1899         PyObject *res = m->sq_item(s, i);
1900         assert(_Py_CheckSlotResult(s, "__getitem__", res != NULL));
1901         return res;
1902     }
1903 
1904     if (Py_TYPE(s)->tp_as_mapping && Py_TYPE(s)->tp_as_mapping->mp_subscript) {
1905         return type_error("%.200s is not a sequence", s);
1906     }
1907     return type_error("'%.200s' object does not support indexing", s);
1908 }
1909 
1910 PyObject *
PySequence_GetSlice(PyObject * s,Py_ssize_t i1,Py_ssize_t i2)1911 PySequence_GetSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2)
1912 {
1913     if (!s) {
1914         return null_error();
1915     }
1916 
1917     PyMappingMethods *mp = Py_TYPE(s)->tp_as_mapping;
1918     if (mp && mp->mp_subscript) {
1919         PyObject *slice = _PySlice_FromIndices(i1, i2);
1920         if (!slice) {
1921             return NULL;
1922         }
1923         PyObject *res = mp->mp_subscript(s, slice);
1924         assert(_Py_CheckSlotResult(s, "__getitem__", res != NULL));
1925         Py_DECREF(slice);
1926         return res;
1927     }
1928 
1929     return type_error("'%.200s' object is unsliceable", s);
1930 }
1931 
1932 int
PySequence_SetItem(PyObject * s,Py_ssize_t i,PyObject * o)1933 PySequence_SetItem(PyObject *s, Py_ssize_t i, PyObject *o)
1934 {
1935     if (s == NULL) {
1936         null_error();
1937         return -1;
1938     }
1939 
1940     PySequenceMethods *m = Py_TYPE(s)->tp_as_sequence;
1941     if (m && m->sq_ass_item) {
1942         if (i < 0) {
1943             if (m->sq_length) {
1944                 Py_ssize_t l = (*m->sq_length)(s);
1945                 assert(_Py_CheckSlotResult(s, "__len__", l >= 0));
1946                 if (l < 0) {
1947                     return -1;
1948                 }
1949                 i += l;
1950             }
1951         }
1952         int res = m->sq_ass_item(s, i, o);
1953         assert(_Py_CheckSlotResult(s, "__setitem__", res >= 0));
1954         return res;
1955     }
1956 
1957     if (Py_TYPE(s)->tp_as_mapping && Py_TYPE(s)->tp_as_mapping->mp_ass_subscript) {
1958         type_error("%.200s is not a sequence", s);
1959         return -1;
1960     }
1961     type_error("'%.200s' object does not support item assignment", s);
1962     return -1;
1963 }
1964 
1965 int
PySequence_DelItem(PyObject * s,Py_ssize_t i)1966 PySequence_DelItem(PyObject *s, Py_ssize_t i)
1967 {
1968     if (s == NULL) {
1969         null_error();
1970         return -1;
1971     }
1972 
1973     PySequenceMethods *m = Py_TYPE(s)->tp_as_sequence;
1974     if (m && m->sq_ass_item) {
1975         if (i < 0) {
1976             if (m->sq_length) {
1977                 Py_ssize_t l = (*m->sq_length)(s);
1978                 assert(_Py_CheckSlotResult(s, "__len__", l >= 0));
1979                 if (l < 0) {
1980                     return -1;
1981                 }
1982                 i += l;
1983             }
1984         }
1985         int res = m->sq_ass_item(s, i, (PyObject *)NULL);
1986         assert(_Py_CheckSlotResult(s, "__delitem__", res >= 0));
1987         return res;
1988     }
1989 
1990     if (Py_TYPE(s)->tp_as_mapping && Py_TYPE(s)->tp_as_mapping->mp_ass_subscript) {
1991         type_error("%.200s is not a sequence", s);
1992         return -1;
1993     }
1994     type_error("'%.200s' object doesn't support item deletion", s);
1995     return -1;
1996 }
1997 
1998 int
PySequence_SetSlice(PyObject * s,Py_ssize_t i1,Py_ssize_t i2,PyObject * o)1999 PySequence_SetSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2, PyObject *o)
2000 {
2001     if (s == NULL) {
2002         null_error();
2003         return -1;
2004     }
2005 
2006     PyMappingMethods *mp = Py_TYPE(s)->tp_as_mapping;
2007     if (mp && mp->mp_ass_subscript) {
2008         PyObject *slice = _PySlice_FromIndices(i1, i2);
2009         if (!slice)
2010             return -1;
2011         int res = mp->mp_ass_subscript(s, slice, o);
2012         assert(_Py_CheckSlotResult(s, "__setitem__", res >= 0));
2013         Py_DECREF(slice);
2014         return res;
2015     }
2016 
2017     type_error("'%.200s' object doesn't support slice assignment", s);
2018     return -1;
2019 }
2020 
2021 int
PySequence_DelSlice(PyObject * s,Py_ssize_t i1,Py_ssize_t i2)2022 PySequence_DelSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2)
2023 {
2024     if (s == NULL) {
2025         null_error();
2026         return -1;
2027     }
2028 
2029     PyMappingMethods *mp = Py_TYPE(s)->tp_as_mapping;
2030     if (mp && mp->mp_ass_subscript) {
2031         PyObject *slice = _PySlice_FromIndices(i1, i2);
2032         if (!slice) {
2033             return -1;
2034         }
2035         int res = mp->mp_ass_subscript(s, slice, NULL);
2036         assert(_Py_CheckSlotResult(s, "__delitem__", res >= 0));
2037         Py_DECREF(slice);
2038         return res;
2039     }
2040     type_error("'%.200s' object doesn't support slice deletion", s);
2041     return -1;
2042 }
2043 
2044 PyObject *
PySequence_Tuple(PyObject * v)2045 PySequence_Tuple(PyObject *v)
2046 {
2047     PyObject *it;  /* iter(v) */
2048     Py_ssize_t n;             /* guess for result tuple size */
2049     PyObject *result = NULL;
2050     Py_ssize_t j;
2051 
2052     if (v == NULL) {
2053         return null_error();
2054     }
2055 
2056     /* Special-case the common tuple and list cases, for efficiency. */
2057     if (PyTuple_CheckExact(v)) {
2058         /* Note that we can't know whether it's safe to return
2059            a tuple *subclass* instance as-is, hence the restriction
2060            to exact tuples here.  In contrast, lists always make
2061            a copy, so there's no need for exactness below. */
2062         Py_INCREF(v);
2063         return v;
2064     }
2065     if (PyList_CheckExact(v))
2066         return PyList_AsTuple(v);
2067 
2068     /* Get iterator. */
2069     it = PyObject_GetIter(v);
2070     if (it == NULL)
2071         return NULL;
2072 
2073     /* Guess result size and allocate space. */
2074     n = PyObject_LengthHint(v, 10);
2075     if (n == -1)
2076         goto Fail;
2077     result = PyTuple_New(n);
2078     if (result == NULL)
2079         goto Fail;
2080 
2081     /* Fill the tuple. */
2082     for (j = 0; ; ++j) {
2083         PyObject *item = PyIter_Next(it);
2084         if (item == NULL) {
2085             if (PyErr_Occurred())
2086                 goto Fail;
2087             break;
2088         }
2089         if (j >= n) {
2090             size_t newn = (size_t)n;
2091             /* The over-allocation strategy can grow a bit faster
2092                than for lists because unlike lists the
2093                over-allocation isn't permanent -- we reclaim
2094                the excess before the end of this routine.
2095                So, grow by ten and then add 25%.
2096             */
2097             newn += 10u;
2098             newn += newn >> 2;
2099             if (newn > PY_SSIZE_T_MAX) {
2100                 /* Check for overflow */
2101                 PyErr_NoMemory();
2102                 Py_DECREF(item);
2103                 goto Fail;
2104             }
2105             n = (Py_ssize_t)newn;
2106             if (_PyTuple_Resize(&result, n) != 0) {
2107                 Py_DECREF(item);
2108                 goto Fail;
2109             }
2110         }
2111         PyTuple_SET_ITEM(result, j, item);
2112     }
2113 
2114     /* Cut tuple back if guess was too large. */
2115     if (j < n &&
2116         _PyTuple_Resize(&result, j) != 0)
2117         goto Fail;
2118 
2119     Py_DECREF(it);
2120     return result;
2121 
2122 Fail:
2123     Py_XDECREF(result);
2124     Py_DECREF(it);
2125     return NULL;
2126 }
2127 
2128 PyObject *
PySequence_List(PyObject * v)2129 PySequence_List(PyObject *v)
2130 {
2131     PyObject *result;  /* result list */
2132     PyObject *rv;          /* return value from PyList_Extend */
2133 
2134     if (v == NULL) {
2135         return null_error();
2136     }
2137 
2138     result = PyList_New(0);
2139     if (result == NULL)
2140         return NULL;
2141 
2142     rv = _PyList_Extend((PyListObject *)result, v);
2143     if (rv == NULL) {
2144         Py_DECREF(result);
2145         return NULL;
2146     }
2147     Py_DECREF(rv);
2148     return result;
2149 }
2150 
2151 PyObject *
PySequence_Fast(PyObject * v,const char * m)2152 PySequence_Fast(PyObject *v, const char *m)
2153 {
2154     PyObject *it;
2155 
2156     if (v == NULL) {
2157         return null_error();
2158     }
2159 
2160     if (PyList_CheckExact(v) || PyTuple_CheckExact(v)) {
2161         Py_INCREF(v);
2162         return v;
2163     }
2164 
2165     it = PyObject_GetIter(v);
2166     if (it == NULL) {
2167         PyThreadState *tstate = _PyThreadState_GET();
2168         if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError)) {
2169             _PyErr_SetString(tstate, PyExc_TypeError, m);
2170         }
2171         return NULL;
2172     }
2173 
2174     v = PySequence_List(it);
2175     Py_DECREF(it);
2176 
2177     return v;
2178 }
2179 
2180 /* Iterate over seq.  Result depends on the operation:
2181    PY_ITERSEARCH_COUNT:  -1 if error, else # of times obj appears in seq.
2182    PY_ITERSEARCH_INDEX:  0-based index of first occurrence of obj in seq;
2183     set ValueError and return -1 if none found; also return -1 on error.
2184    Py_ITERSEARCH_CONTAINS:  return 1 if obj in seq, else 0; -1 on error.
2185 */
2186 Py_ssize_t
_PySequence_IterSearch(PyObject * seq,PyObject * obj,int operation)2187 _PySequence_IterSearch(PyObject *seq, PyObject *obj, int operation)
2188 {
2189     Py_ssize_t n;
2190     int wrapped;  /* for PY_ITERSEARCH_INDEX, true iff n wrapped around */
2191     PyObject *it;  /* iter(seq) */
2192 
2193     if (seq == NULL || obj == NULL) {
2194         null_error();
2195         return -1;
2196     }
2197 
2198     it = PyObject_GetIter(seq);
2199     if (it == NULL) {
2200         if (PyErr_ExceptionMatches(PyExc_TypeError)) {
2201             type_error("argument of type '%.200s' is not iterable", seq);
2202         }
2203         return -1;
2204     }
2205 
2206     n = wrapped = 0;
2207     for (;;) {
2208         int cmp;
2209         PyObject *item = PyIter_Next(it);
2210         if (item == NULL) {
2211             if (PyErr_Occurred())
2212                 goto Fail;
2213             break;
2214         }
2215 
2216         cmp = PyObject_RichCompareBool(item, obj, Py_EQ);
2217         Py_DECREF(item);
2218         if (cmp < 0)
2219             goto Fail;
2220         if (cmp > 0) {
2221             switch (operation) {
2222             case PY_ITERSEARCH_COUNT:
2223                 if (n == PY_SSIZE_T_MAX) {
2224                     PyErr_SetString(PyExc_OverflowError,
2225                            "count exceeds C integer size");
2226                     goto Fail;
2227                 }
2228                 ++n;
2229                 break;
2230 
2231             case PY_ITERSEARCH_INDEX:
2232                 if (wrapped) {
2233                     PyErr_SetString(PyExc_OverflowError,
2234                            "index exceeds C integer size");
2235                     goto Fail;
2236                 }
2237                 goto Done;
2238 
2239             case PY_ITERSEARCH_CONTAINS:
2240                 n = 1;
2241                 goto Done;
2242 
2243             default:
2244                 Py_UNREACHABLE();
2245             }
2246         }
2247 
2248         if (operation == PY_ITERSEARCH_INDEX) {
2249             if (n == PY_SSIZE_T_MAX)
2250                 wrapped = 1;
2251             ++n;
2252         }
2253     }
2254 
2255     if (operation != PY_ITERSEARCH_INDEX)
2256         goto Done;
2257 
2258     PyErr_SetString(PyExc_ValueError,
2259                     "sequence.index(x): x not in sequence");
2260     /* fall into failure code */
2261 Fail:
2262     n = -1;
2263     /* fall through */
2264 Done:
2265     Py_DECREF(it);
2266     return n;
2267 
2268 }
2269 
2270 /* Return # of times o appears in s. */
2271 Py_ssize_t
PySequence_Count(PyObject * s,PyObject * o)2272 PySequence_Count(PyObject *s, PyObject *o)
2273 {
2274     return _PySequence_IterSearch(s, o, PY_ITERSEARCH_COUNT);
2275 }
2276 
2277 /* Return -1 if error; 1 if ob in seq; 0 if ob not in seq.
2278  * Use sq_contains if possible, else defer to _PySequence_IterSearch().
2279  */
2280 int
PySequence_Contains(PyObject * seq,PyObject * ob)2281 PySequence_Contains(PyObject *seq, PyObject *ob)
2282 {
2283     PySequenceMethods *sqm = Py_TYPE(seq)->tp_as_sequence;
2284     if (sqm != NULL && sqm->sq_contains != NULL) {
2285         int res = (*sqm->sq_contains)(seq, ob);
2286         assert(_Py_CheckSlotResult(seq, "__contains__", res >= 0));
2287         return res;
2288     }
2289     Py_ssize_t result = _PySequence_IterSearch(seq, ob, PY_ITERSEARCH_CONTAINS);
2290     return Py_SAFE_DOWNCAST(result, Py_ssize_t, int);
2291 }
2292 
2293 /* Backwards compatibility */
2294 #undef PySequence_In
2295 int
PySequence_In(PyObject * w,PyObject * v)2296 PySequence_In(PyObject *w, PyObject *v)
2297 {
2298     return PySequence_Contains(w, v);
2299 }
2300 
2301 Py_ssize_t
PySequence_Index(PyObject * s,PyObject * o)2302 PySequence_Index(PyObject *s, PyObject *o)
2303 {
2304     return _PySequence_IterSearch(s, o, PY_ITERSEARCH_INDEX);
2305 }
2306 
2307 /* Operations on mappings */
2308 
2309 int
PyMapping_Check(PyObject * o)2310 PyMapping_Check(PyObject *o)
2311 {
2312     return o && Py_TYPE(o)->tp_as_mapping &&
2313         Py_TYPE(o)->tp_as_mapping->mp_subscript;
2314 }
2315 
2316 Py_ssize_t
PyMapping_Size(PyObject * o)2317 PyMapping_Size(PyObject *o)
2318 {
2319     if (o == NULL) {
2320         null_error();
2321         return -1;
2322     }
2323 
2324     PyMappingMethods *m = Py_TYPE(o)->tp_as_mapping;
2325     if (m && m->mp_length) {
2326         Py_ssize_t len = m->mp_length(o);
2327         assert(_Py_CheckSlotResult(o, "__len__", len >= 0));
2328         return len;
2329     }
2330 
2331     if (Py_TYPE(o)->tp_as_sequence && Py_TYPE(o)->tp_as_sequence->sq_length) {
2332         type_error("%.200s is not a mapping", o);
2333         return -1;
2334     }
2335     /* PyMapping_Size() can be called from PyObject_Size(). */
2336     type_error("object of type '%.200s' has no len()", o);
2337     return -1;
2338 }
2339 
2340 #undef PyMapping_Length
2341 Py_ssize_t
PyMapping_Length(PyObject * o)2342 PyMapping_Length(PyObject *o)
2343 {
2344     return PyMapping_Size(o);
2345 }
2346 #define PyMapping_Length PyMapping_Size
2347 
2348 PyObject *
PyMapping_GetItemString(PyObject * o,const char * key)2349 PyMapping_GetItemString(PyObject *o, const char *key)
2350 {
2351     PyObject *okey, *r;
2352 
2353     if (key == NULL) {
2354         return null_error();
2355     }
2356 
2357     okey = PyUnicode_FromString(key);
2358     if (okey == NULL)
2359         return NULL;
2360     r = PyObject_GetItem(o, okey);
2361     Py_DECREF(okey);
2362     return r;
2363 }
2364 
2365 int
PyMapping_SetItemString(PyObject * o,const char * key,PyObject * value)2366 PyMapping_SetItemString(PyObject *o, const char *key, PyObject *value)
2367 {
2368     PyObject *okey;
2369     int r;
2370 
2371     if (key == NULL) {
2372         null_error();
2373         return -1;
2374     }
2375 
2376     okey = PyUnicode_FromString(key);
2377     if (okey == NULL)
2378         return -1;
2379     r = PyObject_SetItem(o, okey, value);
2380     Py_DECREF(okey);
2381     return r;
2382 }
2383 
2384 int
PyMapping_HasKeyString(PyObject * o,const char * key)2385 PyMapping_HasKeyString(PyObject *o, const char *key)
2386 {
2387     PyObject *v;
2388 
2389     v = PyMapping_GetItemString(o, key);
2390     if (v) {
2391         Py_DECREF(v);
2392         return 1;
2393     }
2394     PyErr_Clear();
2395     return 0;
2396 }
2397 
2398 int
PyMapping_HasKey(PyObject * o,PyObject * key)2399 PyMapping_HasKey(PyObject *o, PyObject *key)
2400 {
2401     PyObject *v;
2402 
2403     v = PyObject_GetItem(o, key);
2404     if (v) {
2405         Py_DECREF(v);
2406         return 1;
2407     }
2408     PyErr_Clear();
2409     return 0;
2410 }
2411 
2412 /* This function is quite similar to PySequence_Fast(), but specialized to be
2413    a helper for PyMapping_Keys(), PyMapping_Items() and PyMapping_Values().
2414  */
2415 static PyObject *
method_output_as_list(PyObject * o,_Py_Identifier * meth_id)2416 method_output_as_list(PyObject *o, _Py_Identifier *meth_id)
2417 {
2418     PyObject *it, *result, *meth_output;
2419 
2420     assert(o != NULL);
2421     meth_output = _PyObject_CallMethodIdNoArgs(o, meth_id);
2422     if (meth_output == NULL || PyList_CheckExact(meth_output)) {
2423         return meth_output;
2424     }
2425     it = PyObject_GetIter(meth_output);
2426     if (it == NULL) {
2427         PyThreadState *tstate = _PyThreadState_GET();
2428         if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError)) {
2429             _PyErr_Format(tstate, PyExc_TypeError,
2430                           "%.200s.%U() returned a non-iterable (type %.200s)",
2431                           Py_TYPE(o)->tp_name,
2432                           _PyUnicode_FromId(meth_id),
2433                           Py_TYPE(meth_output)->tp_name);
2434         }
2435         Py_DECREF(meth_output);
2436         return NULL;
2437     }
2438     Py_DECREF(meth_output);
2439     result = PySequence_List(it);
2440     Py_DECREF(it);
2441     return result;
2442 }
2443 
2444 PyObject *
PyMapping_Keys(PyObject * o)2445 PyMapping_Keys(PyObject *o)
2446 {
2447     _Py_IDENTIFIER(keys);
2448 
2449     if (o == NULL) {
2450         return null_error();
2451     }
2452     if (PyDict_CheckExact(o)) {
2453         return PyDict_Keys(o);
2454     }
2455     return method_output_as_list(o, &PyId_keys);
2456 }
2457 
2458 PyObject *
PyMapping_Items(PyObject * o)2459 PyMapping_Items(PyObject *o)
2460 {
2461     _Py_IDENTIFIER(items);
2462 
2463     if (o == NULL) {
2464         return null_error();
2465     }
2466     if (PyDict_CheckExact(o)) {
2467         return PyDict_Items(o);
2468     }
2469     return method_output_as_list(o, &PyId_items);
2470 }
2471 
2472 PyObject *
PyMapping_Values(PyObject * o)2473 PyMapping_Values(PyObject *o)
2474 {
2475     _Py_IDENTIFIER(values);
2476 
2477     if (o == NULL) {
2478         return null_error();
2479     }
2480     if (PyDict_CheckExact(o)) {
2481         return PyDict_Values(o);
2482     }
2483     return method_output_as_list(o, &PyId_values);
2484 }
2485 
2486 /* isinstance(), issubclass() */
2487 
2488 /* abstract_get_bases() has logically 4 return states:
2489  *
2490  * 1. getattr(cls, '__bases__') could raise an AttributeError
2491  * 2. getattr(cls, '__bases__') could raise some other exception
2492  * 3. getattr(cls, '__bases__') could return a tuple
2493  * 4. getattr(cls, '__bases__') could return something other than a tuple
2494  *
2495  * Only state #3 is a non-error state and only it returns a non-NULL object
2496  * (it returns the retrieved tuple).
2497  *
2498  * Any raised AttributeErrors are masked by clearing the exception and
2499  * returning NULL.  If an object other than a tuple comes out of __bases__,
2500  * then again, the return value is NULL.  So yes, these two situations
2501  * produce exactly the same results: NULL is returned and no error is set.
2502  *
2503  * If some exception other than AttributeError is raised, then NULL is also
2504  * returned, but the exception is not cleared.  That's because we want the
2505  * exception to be propagated along.
2506  *
2507  * Callers are expected to test for PyErr_Occurred() when the return value
2508  * is NULL to decide whether a valid exception should be propagated or not.
2509  * When there's no exception to propagate, it's customary for the caller to
2510  * set a TypeError.
2511  */
2512 static PyObject *
abstract_get_bases(PyObject * cls)2513 abstract_get_bases(PyObject *cls)
2514 {
2515     _Py_IDENTIFIER(__bases__);
2516     PyObject *bases;
2517 
2518     (void)_PyObject_LookupAttrId(cls, &PyId___bases__, &bases);
2519     if (bases != NULL && !PyTuple_Check(bases)) {
2520         Py_DECREF(bases);
2521         return NULL;
2522     }
2523     return bases;
2524 }
2525 
2526 
2527 static int
abstract_issubclass(PyObject * derived,PyObject * cls)2528 abstract_issubclass(PyObject *derived, PyObject *cls)
2529 {
2530     PyObject *bases = NULL;
2531     Py_ssize_t i, n;
2532     int r = 0;
2533 
2534     while (1) {
2535         if (derived == cls) {
2536             Py_XDECREF(bases); /* See below comment */
2537             return 1;
2538         }
2539         /* Use XSETREF to drop bases reference *after* finishing with
2540            derived; bases might be the only reference to it.
2541            XSETREF is used instead of SETREF, because bases is NULL on the
2542            first iteration of the loop.
2543         */
2544         Py_XSETREF(bases, abstract_get_bases(derived));
2545         if (bases == NULL) {
2546             if (PyErr_Occurred())
2547                 return -1;
2548             return 0;
2549         }
2550         n = PyTuple_GET_SIZE(bases);
2551         if (n == 0) {
2552             Py_DECREF(bases);
2553             return 0;
2554         }
2555         /* Avoid recursivity in the single inheritance case */
2556         if (n == 1) {
2557             derived = PyTuple_GET_ITEM(bases, 0);
2558             continue;
2559         }
2560         break;
2561     }
2562     assert(n >= 2);
2563     if (Py_EnterRecursiveCall(" in __issubclass__")) {
2564         Py_DECREF(bases);
2565         return -1;
2566     }
2567     for (i = 0; i < n; i++) {
2568         r = abstract_issubclass(PyTuple_GET_ITEM(bases, i), cls);
2569         if (r != 0) {
2570             break;
2571         }
2572     }
2573     Py_LeaveRecursiveCall();
2574     Py_DECREF(bases);
2575     return r;
2576 }
2577 
2578 static int
check_class(PyObject * cls,const char * error)2579 check_class(PyObject *cls, const char *error)
2580 {
2581     PyObject *bases = abstract_get_bases(cls);
2582     if (bases == NULL) {
2583         /* Do not mask errors. */
2584         PyThreadState *tstate = _PyThreadState_GET();
2585         if (!_PyErr_Occurred(tstate)) {
2586             _PyErr_SetString(tstate, PyExc_TypeError, error);
2587         }
2588         return 0;
2589     }
2590     Py_DECREF(bases);
2591     return -1;
2592 }
2593 
2594 static int
object_isinstance(PyObject * inst,PyObject * cls)2595 object_isinstance(PyObject *inst, PyObject *cls)
2596 {
2597     PyObject *icls;
2598     int retval;
2599     _Py_IDENTIFIER(__class__);
2600     if (PyType_Check(cls)) {
2601         retval = PyObject_TypeCheck(inst, (PyTypeObject *)cls);
2602         if (retval == 0) {
2603             retval = _PyObject_LookupAttrId(inst, &PyId___class__, &icls);
2604             if (icls != NULL) {
2605                 if (icls != (PyObject *)(Py_TYPE(inst)) && PyType_Check(icls)) {
2606                     retval = PyType_IsSubtype(
2607                         (PyTypeObject *)icls,
2608                         (PyTypeObject *)cls);
2609                 }
2610                 else {
2611                     retval = 0;
2612                 }
2613                 Py_DECREF(icls);
2614             }
2615         }
2616     }
2617     else {
2618         if (!check_class(cls,
2619             "isinstance() arg 2 must be a type, a tuple of types, or a union"))
2620             return -1;
2621         retval = _PyObject_LookupAttrId(inst, &PyId___class__, &icls);
2622         if (icls != NULL) {
2623             retval = abstract_issubclass(icls, cls);
2624             Py_DECREF(icls);
2625         }
2626     }
2627 
2628     return retval;
2629 }
2630 
2631 static int
object_recursive_isinstance(PyThreadState * tstate,PyObject * inst,PyObject * cls)2632 object_recursive_isinstance(PyThreadState *tstate, PyObject *inst, PyObject *cls)
2633 {
2634     _Py_IDENTIFIER(__instancecheck__);
2635 
2636     /* Quick test for an exact match */
2637     if (Py_IS_TYPE(inst, (PyTypeObject *)cls)) {
2638         return 1;
2639     }
2640 
2641     /* We know what type's __instancecheck__ does. */
2642     if (PyType_CheckExact(cls)) {
2643         return object_isinstance(inst, cls);
2644     }
2645 
2646     if (PyTuple_Check(cls)) {
2647         /* Not a general sequence -- that opens up the road to
2648            recursion and stack overflow. */
2649         if (_Py_EnterRecursiveCall(tstate, " in __instancecheck__")) {
2650             return -1;
2651         }
2652         Py_ssize_t n = PyTuple_GET_SIZE(cls);
2653         int r = 0;
2654         for (Py_ssize_t i = 0; i < n; ++i) {
2655             PyObject *item = PyTuple_GET_ITEM(cls, i);
2656             r = object_recursive_isinstance(tstate, inst, item);
2657             if (r != 0) {
2658                 /* either found it, or got an error */
2659                 break;
2660             }
2661         }
2662         _Py_LeaveRecursiveCall(tstate);
2663         return r;
2664     }
2665 
2666     PyObject *checker = _PyObject_LookupSpecial(cls, &PyId___instancecheck__);
2667     if (checker != NULL) {
2668         if (_Py_EnterRecursiveCall(tstate, " in __instancecheck__")) {
2669             Py_DECREF(checker);
2670             return -1;
2671         }
2672 
2673         PyObject *res = PyObject_CallOneArg(checker, inst);
2674         _Py_LeaveRecursiveCall(tstate);
2675         Py_DECREF(checker);
2676 
2677         if (res == NULL) {
2678             return -1;
2679         }
2680         int ok = PyObject_IsTrue(res);
2681         Py_DECREF(res);
2682 
2683         return ok;
2684     }
2685     else if (_PyErr_Occurred(tstate)) {
2686         return -1;
2687     }
2688 
2689     /* cls has no __instancecheck__() method */
2690     return object_isinstance(inst, cls);
2691 }
2692 
2693 
2694 int
PyObject_IsInstance(PyObject * inst,PyObject * cls)2695 PyObject_IsInstance(PyObject *inst, PyObject *cls)
2696 {
2697     PyThreadState *tstate = _PyThreadState_GET();
2698     return object_recursive_isinstance(tstate, inst, cls);
2699 }
2700 
2701 
2702 static  int
recursive_issubclass(PyObject * derived,PyObject * cls)2703 recursive_issubclass(PyObject *derived, PyObject *cls)
2704 {
2705     if (PyType_Check(cls) && PyType_Check(derived)) {
2706         /* Fast path (non-recursive) */
2707         return PyType_IsSubtype((PyTypeObject *)derived, (PyTypeObject *)cls);
2708     }
2709     if (!check_class(derived,
2710                      "issubclass() arg 1 must be a class"))
2711         return -1;
2712 
2713     if (!_PyUnion_Check(cls) && !check_class(cls,
2714                             "issubclass() arg 2 must be a class,"
2715                             " a tuple of classes, or a union")) {
2716         return -1;
2717     }
2718 
2719     return abstract_issubclass(derived, cls);
2720 }
2721 
2722 static int
object_issubclass(PyThreadState * tstate,PyObject * derived,PyObject * cls)2723 object_issubclass(PyThreadState *tstate, PyObject *derived, PyObject *cls)
2724 {
2725     _Py_IDENTIFIER(__subclasscheck__);
2726     PyObject *checker;
2727 
2728     /* We know what type's __subclasscheck__ does. */
2729     if (PyType_CheckExact(cls)) {
2730         /* Quick test for an exact match */
2731         if (derived == cls)
2732             return 1;
2733         return recursive_issubclass(derived, cls);
2734     }
2735 
2736     if (PyTuple_Check(cls)) {
2737 
2738         if (_Py_EnterRecursiveCall(tstate, " in __subclasscheck__")) {
2739             return -1;
2740         }
2741         Py_ssize_t n = PyTuple_GET_SIZE(cls);
2742         int r = 0;
2743         for (Py_ssize_t i = 0; i < n; ++i) {
2744             PyObject *item = PyTuple_GET_ITEM(cls, i);
2745             r = object_issubclass(tstate, derived, item);
2746             if (r != 0)
2747                 /* either found it, or got an error */
2748                 break;
2749         }
2750         _Py_LeaveRecursiveCall(tstate);
2751         return r;
2752     }
2753 
2754     checker = _PyObject_LookupSpecial(cls, &PyId___subclasscheck__);
2755     if (checker != NULL) {
2756         int ok = -1;
2757         if (_Py_EnterRecursiveCall(tstate, " in __subclasscheck__")) {
2758             Py_DECREF(checker);
2759             return ok;
2760         }
2761         PyObject *res = PyObject_CallOneArg(checker, derived);
2762         _Py_LeaveRecursiveCall(tstate);
2763         Py_DECREF(checker);
2764         if (res != NULL) {
2765             ok = PyObject_IsTrue(res);
2766             Py_DECREF(res);
2767         }
2768         return ok;
2769     }
2770     else if (_PyErr_Occurred(tstate)) {
2771         return -1;
2772     }
2773 
2774     /* Probably never reached anymore. */
2775     return recursive_issubclass(derived, cls);
2776 }
2777 
2778 
2779 int
PyObject_IsSubclass(PyObject * derived,PyObject * cls)2780 PyObject_IsSubclass(PyObject *derived, PyObject *cls)
2781 {
2782     PyThreadState *tstate = _PyThreadState_GET();
2783     return object_issubclass(tstate, derived, cls);
2784 }
2785 
2786 
2787 int
_PyObject_RealIsInstance(PyObject * inst,PyObject * cls)2788 _PyObject_RealIsInstance(PyObject *inst, PyObject *cls)
2789 {
2790     return object_isinstance(inst, cls);
2791 }
2792 
2793 int
_PyObject_RealIsSubclass(PyObject * derived,PyObject * cls)2794 _PyObject_RealIsSubclass(PyObject *derived, PyObject *cls)
2795 {
2796     return recursive_issubclass(derived, cls);
2797 }
2798 
2799 
2800 PyObject *
PyObject_GetIter(PyObject * o)2801 PyObject_GetIter(PyObject *o)
2802 {
2803     PyTypeObject *t = Py_TYPE(o);
2804     getiterfunc f;
2805 
2806     f = t->tp_iter;
2807     if (f == NULL) {
2808         if (PySequence_Check(o))
2809             return PySeqIter_New(o);
2810         return type_error("'%.200s' object is not iterable", o);
2811     }
2812     else {
2813         PyObject *res = (*f)(o);
2814         if (res != NULL && !PyIter_Check(res)) {
2815             PyErr_Format(PyExc_TypeError,
2816                          "iter() returned non-iterator "
2817                          "of type '%.100s'",
2818                          Py_TYPE(res)->tp_name);
2819             Py_DECREF(res);
2820             res = NULL;
2821         }
2822         return res;
2823     }
2824 }
2825 
2826 PyObject *
PyObject_GetAIter(PyObject * o)2827 PyObject_GetAIter(PyObject *o) {
2828     PyTypeObject *t = Py_TYPE(o);
2829     unaryfunc f;
2830 
2831     if (t->tp_as_async == NULL || t->tp_as_async->am_aiter == NULL) {
2832         return type_error("'%.200s' object is not an async iterable", o);
2833     }
2834     f = t->tp_as_async->am_aiter;
2835     PyObject *it = (*f)(o);
2836     if (it != NULL && !PyAIter_Check(it)) {
2837         PyErr_Format(PyExc_TypeError,
2838                      "aiter() returned not an async iterator of type '%.100s'",
2839                      Py_TYPE(it)->tp_name);
2840         Py_DECREF(it);
2841         it = NULL;
2842     }
2843     return it;
2844 }
2845 
2846 int
PyIter_Check(PyObject * obj)2847 PyIter_Check(PyObject *obj)
2848 {
2849     PyTypeObject *tp = Py_TYPE(obj);
2850     return (tp->tp_iternext != NULL &&
2851             tp->tp_iternext != &_PyObject_NextNotImplemented);
2852 }
2853 
2854 int
PyAIter_Check(PyObject * obj)2855 PyAIter_Check(PyObject *obj)
2856 {
2857     PyTypeObject *tp = Py_TYPE(obj);
2858     return (tp->tp_as_async != NULL &&
2859             tp->tp_as_async->am_anext != NULL &&
2860             tp->tp_as_async->am_anext != &_PyObject_NextNotImplemented);
2861 }
2862 
2863 /* Return next item.
2864  * If an error occurs, return NULL.  PyErr_Occurred() will be true.
2865  * If the iteration terminates normally, return NULL and clear the
2866  * PyExc_StopIteration exception (if it was set).  PyErr_Occurred()
2867  * will be false.
2868  * Else return the next object.  PyErr_Occurred() will be false.
2869  */
2870 PyObject *
PyIter_Next(PyObject * iter)2871 PyIter_Next(PyObject *iter)
2872 {
2873     PyObject *result;
2874     result = (*Py_TYPE(iter)->tp_iternext)(iter);
2875     if (result == NULL) {
2876         PyThreadState *tstate = _PyThreadState_GET();
2877         if (_PyErr_Occurred(tstate)
2878             && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
2879         {
2880             _PyErr_Clear(tstate);
2881         }
2882     }
2883     return result;
2884 }
2885 
2886 PySendResult
PyIter_Send(PyObject * iter,PyObject * arg,PyObject ** result)2887 PyIter_Send(PyObject *iter, PyObject *arg, PyObject **result)
2888 {
2889     _Py_IDENTIFIER(send);
2890     assert(arg != NULL);
2891     assert(result != NULL);
2892     if (Py_TYPE(iter)->tp_as_async && Py_TYPE(iter)->tp_as_async->am_send) {
2893         PySendResult res = Py_TYPE(iter)->tp_as_async->am_send(iter, arg, result);
2894         assert(_Py_CheckSlotResult(iter, "am_send", res != PYGEN_ERROR));
2895         return res;
2896     }
2897     if (arg == Py_None && PyIter_Check(iter)) {
2898         *result = Py_TYPE(iter)->tp_iternext(iter);
2899     }
2900     else {
2901         *result = _PyObject_CallMethodIdOneArg(iter, &PyId_send, arg);
2902     }
2903     if (*result != NULL) {
2904         return PYGEN_NEXT;
2905     }
2906     if (_PyGen_FetchStopIterationValue(result) == 0) {
2907         return PYGEN_RETURN;
2908     }
2909     return PYGEN_ERROR;
2910 }
2911 
2912 /*
2913  * Flatten a sequence of bytes() objects into a C array of
2914  * NULL terminated string pointers with a NULL char* terminating the array.
2915  * (ie: an argv or env list)
2916  *
2917  * Memory allocated for the returned list is allocated using PyMem_Malloc()
2918  * and MUST be freed by _Py_FreeCharPArray().
2919  */
2920 char *const *
_PySequence_BytesToCharpArray(PyObject * self)2921 _PySequence_BytesToCharpArray(PyObject* self)
2922 {
2923     char **array;
2924     Py_ssize_t i, argc;
2925     PyObject *item = NULL;
2926     Py_ssize_t size;
2927 
2928     argc = PySequence_Size(self);
2929     if (argc == -1)
2930         return NULL;
2931 
2932     assert(argc >= 0);
2933 
2934     if ((size_t)argc > (PY_SSIZE_T_MAX-sizeof(char *)) / sizeof(char *)) {
2935         PyErr_NoMemory();
2936         return NULL;
2937     }
2938 
2939     array = PyMem_Malloc((argc + 1) * sizeof(char *));
2940     if (array == NULL) {
2941         PyErr_NoMemory();
2942         return NULL;
2943     }
2944     for (i = 0; i < argc; ++i) {
2945         char *data;
2946         item = PySequence_GetItem(self, i);
2947         if (item == NULL) {
2948             /* NULL terminate before freeing. */
2949             array[i] = NULL;
2950             goto fail;
2951         }
2952         /* check for embedded null bytes */
2953         if (PyBytes_AsStringAndSize(item, &data, NULL) < 0) {
2954             /* NULL terminate before freeing. */
2955             array[i] = NULL;
2956             goto fail;
2957         }
2958         size = PyBytes_GET_SIZE(item) + 1;
2959         array[i] = PyMem_Malloc(size);
2960         if (!array[i]) {
2961             PyErr_NoMemory();
2962             goto fail;
2963         }
2964         memcpy(array[i], data, size);
2965         Py_DECREF(item);
2966     }
2967     array[argc] = NULL;
2968 
2969     return array;
2970 
2971 fail:
2972     Py_XDECREF(item);
2973     _Py_FreeCharPArray(array);
2974     return NULL;
2975 }
2976 
2977 
2978 /* Free's a NULL terminated char** array of C strings. */
2979 void
_Py_FreeCharPArray(char * const array[])2980 _Py_FreeCharPArray(char *const array[])
2981 {
2982     Py_ssize_t i;
2983     for (i = 0; array[i] != NULL; ++i) {
2984         PyMem_Free(array[i]);
2985     }
2986     PyMem_Free((void*)array);
2987 }
2988