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