LCOV - code coverage report
Current view: top level - libreoffice/workdir/unxlngi6.pro/UnpackedTarball/python3/Objects - abstract.c (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 483 1408 34.3 %
Date: 2012-12-17 Functions: 66 129 51.2 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10