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