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