Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Objects/listobject.c

    r2 r391  
    66#include <stddef.h>
    77#else
    8 #include <sys/types.h>          /* For size_t */
     8#include <sys/types.h>          /* For size_t */
    99#endif
    1010
     
    1212 * ob_size to newsize.  If newsize > ob_size on entry, the content
    1313 * of the new slots at exit is undefined heap trash; it's the caller's
    14  * responsiblity to overwrite them with sane values.
     14 * responsibility to overwrite them with sane values.
    1515 * The number of allocated elements may grow, shrink, or stay the same.
    1616 * Failure is impossible if newsize <= self.allocated on entry, although
     
    2525list_resize(PyListObject *self, Py_ssize_t newsize)
    2626{
    27         PyObject **items;
    28         size_t new_allocated;
    29         Py_ssize_t allocated = self->allocated;
    30 
    31         /* Bypass realloc() when a previous overallocation is large enough
    32            to accommodate the newsize.  If the newsize falls lower than half
    33            the allocated size, then proceed with the realloc() to shrink the list.
    34         */
    35         if (allocated >= newsize && newsize >= (allocated >> 1)) {
    36                 assert(self->ob_item != NULL || newsize == 0);
    37                 Py_SIZE(self) = newsize;
    38                 return 0;
    39         }
    40 
    41         /* This over-allocates proportional to the list size, making room
    42         * for additional growth.  The over-allocation is mild, but is
    43         * enough to give linear-time amortized behavior over a long
    44         * sequence of appends() in the presence of a poorly-performing
    45         * system realloc().
    46         * The growth pattern is:  0, 4, 8, 16, 25, 35, 46, 58, 72, 88, ...
    47         */
    48         new_allocated = (newsize >> 3) + (newsize < 9 ? 3 : 6);
    49 
    50         /* check for integer overflow */
    51         if (new_allocated > PY_SIZE_MAX - newsize) {
    52                 PyErr_NoMemory();
    53                 return -1;
    54         } else {
    55                 new_allocated += newsize;
    56         }
    57 
    58         if (newsize == 0)
    59                 new_allocated = 0;
    60         items = self->ob_item;
    61         if (new_allocated <= ((~(size_t)0) / sizeof(PyObject *)))
    62                 PyMem_RESIZE(items, PyObject *, new_allocated);
    63         else
    64                 items = NULL;
    65         if (items == NULL) {
    66                 PyErr_NoMemory();
    67                 return -1;
    68         }
    69         self->ob_item = items;
    70         Py_SIZE(self) = newsize;
    71         self->allocated = new_allocated;
    72         return 0;
     27    PyObject **items;
     28    size_t new_allocated;
     29    Py_ssize_t allocated = self->allocated;
     30
     31    /* Bypass realloc() when a previous overallocation is large enough
     32       to accommodate the newsize.  If the newsize falls lower than half
     33       the allocated size, then proceed with the realloc() to shrink the list.
     34    */
     35    if (allocated >= newsize && newsize >= (allocated >> 1)) {
     36        assert(self->ob_item != NULL || newsize == 0);
     37        Py_SIZE(self) = newsize;
     38        return 0;
     39    }
     40
     41    /* This over-allocates proportional to the list size, making room
     42    * for additional growth.  The over-allocation is mild, but is
     43    * enough to give linear-time amortized behavior over a long
     44    * sequence of appends() in the presence of a poorly-performing
     45    * system realloc().
     46    * The growth pattern is:  0, 4, 8, 16, 25, 35, 46, 58, 72, 88, ...
     47    */
     48    new_allocated = (newsize >> 3) + (newsize < 9 ? 3 : 6);
     49
     50    /* check for integer overflow */
     51    if (new_allocated > PY_SIZE_MAX - newsize) {
     52        PyErr_NoMemory();
     53        return -1;
     54    } else {
     55        new_allocated += newsize;
     56    }
     57
     58    if (newsize == 0)
     59        new_allocated = 0;
     60    items = self->ob_item;
     61    if (new_allocated <= (PY_SIZE_MAX / sizeof(PyObject *)))
     62        PyMem_RESIZE(items, PyObject *, new_allocated);
     63    else
     64        items = NULL;
     65    if (items == NULL) {
     66        PyErr_NoMemory();
     67        return -1;
     68    }
     69    self->ob_item = items;
     70    Py_SIZE(self) = newsize;
     71    self->allocated = new_allocated;
     72    return 0;
    7373}
    7474
     
    8282show_alloc(void)
    8383{
    84         fprintf(stderr, "List allocations: %" PY_FORMAT_SIZE_T "d\n",
    85                 count_alloc);
    86         fprintf(stderr, "List reuse through freelist: %" PY_FORMAT_SIZE_T
    87                 "d\n", count_reuse);
    88         fprintf(stderr, "%.2f%% reuse rate\n\n",
    89                 (100.0*count_reuse/(count_alloc+count_reuse)));
     84    fprintf(stderr, "List allocations: %" PY_FORMAT_SIZE_T "d\n",
     85        count_alloc);
     86    fprintf(stderr, "List reuse through freelist: %" PY_FORMAT_SIZE_T
     87        "d\n", count_reuse);
     88    fprintf(stderr, "%.2f%% reuse rate\n\n",
     89        (100.0*count_reuse/(count_alloc+count_reuse)));
    9090}
    9191#endif
     
    101101PyList_Fini(void)
    102102{
    103         PyListObject *op;
    104 
    105         while (numfree) {
    106                 op = free_list[--numfree];
    107                 assert(PyList_CheckExact(op));
    108                 PyObject_GC_Del(op);
    109         }
     103    PyListObject *op;
     104
     105    while (numfree) {
     106        op = free_list[--numfree];
     107        assert(PyList_CheckExact(op));
     108        PyObject_GC_Del(op);
     109    }
    110110}
    111111
     
    113113PyList_New(Py_ssize_t size)
    114114{
    115         PyListObject *op;
    116         size_t nbytes;
     115    PyListObject *op;
     116    size_t nbytes;
    117117#ifdef SHOW_ALLOC_COUNT
    118         static int initialized = 0;
    119         if (!initialized) {
    120                 Py_AtExit(show_alloc);
    121                 initialized = 1;
    122         }
     118    static int initialized = 0;
     119    if (!initialized) {
     120        Py_AtExit(show_alloc);
     121        initialized = 1;
     122    }
    123123#endif
    124124
    125         if (size < 0) {
    126                 PyErr_BadInternalCall();
    127                 return NULL;
    128         }
    129         /* Check for overflow without an actual overflow,
    130         *  which can cause compiler to optimise out */
    131         if ((size_t)size > PY_SIZE_MAX / sizeof(PyObject *))
    132                 return PyErr_NoMemory();
    133         nbytes = size * sizeof(PyObject *);
    134         if (numfree) {
    135                 numfree--;
    136                 op = free_list[numfree];
    137                 _Py_NewReference((PyObject *)op);
     125    if (size < 0) {
     126        PyErr_BadInternalCall();
     127        return NULL;
     128    }
     129    /* Check for overflow without an actual overflow,
     130    *  which can cause compiler to optimise out */
     131    if ((size_t)size > PY_SIZE_MAX / sizeof(PyObject *))
     132        return PyErr_NoMemory();
     133    nbytes = size * sizeof(PyObject *);
     134    if (numfree) {
     135        numfree--;
     136        op = free_list[numfree];
     137        _Py_NewReference((PyObject *)op);
    138138#ifdef SHOW_ALLOC_COUNT
    139                 count_reuse++;
     139        count_reuse++;
    140140#endif
    141         } else {
    142                 op = PyObject_GC_New(PyListObject, &PyList_Type);
    143                 if (op == NULL)
    144                         return NULL;
     141    } else {
     142        op = PyObject_GC_New(PyListObject, &PyList_Type);
     143        if (op == NULL)
     144            return NULL;
    145145#ifdef SHOW_ALLOC_COUNT
    146                 count_alloc++;
     146        count_alloc++;
    147147#endif
    148         }
    149         if (size <= 0)
    150                 op->ob_item = NULL;
    151         else {
    152                 op->ob_item = (PyObject **) PyMem_MALLOC(nbytes);
    153                 if (op->ob_item == NULL) {
    154                         Py_DECREF(op);
    155                         return PyErr_NoMemory();
    156                 }
    157                 memset(op->ob_item, 0, nbytes);
    158         }
    159         Py_SIZE(op) = size;
    160         op->allocated = size;
    161         _PyObject_GC_TRACK(op);
    162         return (PyObject *) op;
     148    }
     149    if (size <= 0)
     150        op->ob_item = NULL;
     151    else {
     152        op->ob_item = (PyObject **) PyMem_MALLOC(nbytes);
     153        if (op->ob_item == NULL) {
     154            Py_DECREF(op);
     155            return PyErr_NoMemory();
     156        }
     157        memset(op->ob_item, 0, nbytes);
     158    }
     159    Py_SIZE(op) = size;
     160    op->allocated = size;
     161    _PyObject_GC_TRACK(op);
     162    return (PyObject *) op;
    163163}
    164164
     
    166166PyList_Size(PyObject *op)
    167167{
    168         if (!PyList_Check(op)) {
    169                 PyErr_BadInternalCall();
    170                 return -1;
    171         }
    172         else
    173                 return Py_SIZE(op);
     168    if (!PyList_Check(op)) {
     169        PyErr_BadInternalCall();
     170        return -1;
     171    }
     172    else
     173        return Py_SIZE(op);
    174174}
    175175
     
    179179PyList_GetItem(PyObject *op, Py_ssize_t i)
    180180{
    181         if (!PyList_Check(op)) {
    182                 PyErr_BadInternalCall();
    183                 return NULL;
    184         }
    185         if (i < 0 || i >= Py_SIZE(op)) {
    186                 if (indexerr == NULL)
    187                         indexerr = PyString_FromString(
    188                                 "list index out of range");
    189                 PyErr_SetObject(PyExc_IndexError, indexerr);
    190                 return NULL;
    191         }
    192         return ((PyListObject *)op) -> ob_item[i];
     181    if (!PyList_Check(op)) {
     182        PyErr_BadInternalCall();
     183        return NULL;
     184    }
     185    if (i < 0 || i >= Py_SIZE(op)) {
     186        if (indexerr == NULL) {
     187            indexerr = PyString_FromString(
     188                "list index out of range");
     189            if (indexerr == NULL)
     190                return NULL;
     191        }
     192        PyErr_SetObject(PyExc_IndexError, indexerr);
     193        return NULL;
     194    }
     195    return ((PyListObject *)op) -> ob_item[i];
    193196}
    194197
     
    197200               register PyObject *newitem)
    198201{
    199         register PyObject *olditem;
    200         register PyObject **p;
    201         if (!PyList_Check(op)) {
    202                 Py_XDECREF(newitem);
    203                 PyErr_BadInternalCall();
    204                 return -1;
    205         }
    206         if (i < 0 || i >= Py_SIZE(op)) {
    207                 Py_XDECREF(newitem);
    208                 PyErr_SetString(PyExc_IndexError,
    209                                 "list assignment index out of range");
    210                 return -1;
    211         }
    212         p = ((PyListObject *)op) -> ob_item + i;
    213         olditem = *p;
    214         *p = newitem;
    215         Py_XDECREF(olditem);
    216         return 0;
     202    register PyObject *olditem;
     203    register PyObject **p;
     204    if (!PyList_Check(op)) {
     205        Py_XDECREF(newitem);
     206        PyErr_BadInternalCall();
     207        return -1;
     208    }
     209    if (i < 0 || i >= Py_SIZE(op)) {
     210        Py_XDECREF(newitem);
     211        PyErr_SetString(PyExc_IndexError,
     212                        "list assignment index out of range");
     213        return -1;
     214    }
     215    p = ((PyListObject *)op) -> ob_item + i;
     216    olditem = *p;
     217    *p = newitem;
     218    Py_XDECREF(olditem);
     219    return 0;
    217220}
    218221
     
    220223ins1(PyListObject *self, Py_ssize_t where, PyObject *v)
    221224{
    222         Py_ssize_t i, n = Py_SIZE(self);
    223         PyObject **items;
    224         if (v == NULL) {
    225                 PyErr_BadInternalCall();
    226                 return -1;
    227         }
    228         if (n == PY_SSIZE_T_MAX) {
    229                 PyErr_SetString(PyExc_OverflowError,
    230                         "cannot add more objects to list");
    231                 return -1;
    232         }
    233 
    234         if (list_resize(self, n+1) == -1)
    235                 return -1;
    236 
    237         if (where < 0) {
    238                 where += n;
    239                 if (where < 0)
    240                         where = 0;
    241         }
    242         if (where > n)
    243                 where = n;
    244         items = self->ob_item;
    245         for (i = n; --i >= where; )
    246                 items[i+1] = items[i];
    247         Py_INCREF(v);
    248         items[where] = v;
    249         return 0;
     225    Py_ssize_t i, n = Py_SIZE(self);
     226    PyObject **items;
     227    if (v == NULL) {
     228        PyErr_BadInternalCall();
     229        return -1;
     230    }
     231    if (n == PY_SSIZE_T_MAX) {
     232        PyErr_SetString(PyExc_OverflowError,
     233            "cannot add more objects to list");
     234        return -1;
     235    }
     236
     237    if (list_resize(self, n+1) == -1)
     238        return -1;
     239
     240    if (where < 0) {
     241        where += n;
     242        if (where < 0)
     243            where = 0;
     244    }
     245    if (where > n)
     246        where = n;
     247    items = self->ob_item;
     248    for (i = n; --i >= where; )
     249        items[i+1] = items[i];
     250    Py_INCREF(v);
     251    items[where] = v;
     252    return 0;
    250253}
    251254
     
    253256PyList_Insert(PyObject *op, Py_ssize_t where, PyObject *newitem)
    254257{
    255         if (!PyList_Check(op)) {
    256                 PyErr_BadInternalCall();
    257                 return -1;
    258         }
    259         return ins1((PyListObject *)op, where, newitem);
     258    if (!PyList_Check(op)) {
     259        PyErr_BadInternalCall();
     260        return -1;
     261    }
     262    return ins1((PyListObject *)op, where, newitem);
    260263}
    261264
     
    263266app1(PyListObject *self, PyObject *v)
    264267{
    265         Py_ssize_t n = PyList_GET_SIZE(self);
    266 
    267         assert (v != NULL);
    268         if (n == PY_SSIZE_T_MAX) {
    269                 PyErr_SetString(PyExc_OverflowError,
    270                         "cannot add more objects to list");
    271                 return -1;
    272         }
    273 
    274         if (list_resize(self, n+1) == -1)
    275                 return -1;
    276 
    277         Py_INCREF(v);
    278         PyList_SET_ITEM(self, n, v);
    279         return 0;
     268    Py_ssize_t n = PyList_GET_SIZE(self);
     269
     270    assert (v != NULL);
     271    if (n == PY_SSIZE_T_MAX) {
     272        PyErr_SetString(PyExc_OverflowError,
     273            "cannot add more objects to list");
     274        return -1;
     275    }
     276
     277    if (list_resize(self, n+1) == -1)
     278        return -1;
     279
     280    Py_INCREF(v);
     281    PyList_SET_ITEM(self, n, v);
     282    return 0;
    280283}
    281284
     
    283286PyList_Append(PyObject *op, PyObject *newitem)
    284287{
    285         if (PyList_Check(op) && (newitem != NULL))
    286                 return app1((PyListObject *)op, newitem);
    287         PyErr_BadInternalCall();
    288         return -1;
     288    if (PyList_Check(op) && (newitem != NULL))
     289        return app1((PyListObject *)op, newitem);
     290    PyErr_BadInternalCall();
     291    return -1;
    289292}
    290293
     
    294297list_dealloc(PyListObject *op)
    295298{
    296         Py_ssize_t i;
    297         PyObject_GC_UnTrack(op);
    298         Py_TRASHCAN_SAFE_BEGIN(op)
    299         if (op->ob_item != NULL) {
    300                 /* Do it backwards, for Christian Tismer.
    301                    There's a simple test case where somehow this reduces
    302                    thrashing when a *very* large list is created and
    303                    immediately deleted. */
    304                 i = Py_SIZE(op);
    305                 while (--i >= 0) {
    306                         Py_XDECREF(op->ob_item[i]);
    307                 }
    308                 PyMem_FREE(op->ob_item);
    309         }
    310         if (numfree < PyList_MAXFREELIST && PyList_CheckExact(op))
    311                 free_list[numfree++] = op;
    312         else
    313                 Py_TYPE(op)->tp_free((PyObject *)op);
    314         Py_TRASHCAN_SAFE_END(op)
     299    Py_ssize_t i;
     300    PyObject_GC_UnTrack(op);
     301    Py_TRASHCAN_SAFE_BEGIN(op)
     302    if (op->ob_item != NULL) {
     303        /* Do it backwards, for Christian Tismer.
     304           There's a simple test case where somehow this reduces
     305           thrashing when a *very* large list is created and
     306           immediately deleted. */
     307        i = Py_SIZE(op);
     308        while (--i >= 0) {
     309            Py_XDECREF(op->ob_item[i]);
     310        }
     311        PyMem_FREE(op->ob_item);
     312    }
     313    if (numfree < PyList_MAXFREELIST && PyList_CheckExact(op))
     314        free_list[numfree++] = op;
     315    else
     316        Py_TYPE(op)->tp_free((PyObject *)op);
     317    Py_TRASHCAN_SAFE_END(op)
    315318}
    316319
     
    318321list_print(PyListObject *op, FILE *fp, int flags)
    319322{
    320         int rc;
    321         Py_ssize_t i;
    322         PyObject *item;
    323 
    324         rc = Py_ReprEnter((PyObject*)op);
    325         if (rc != 0) {
    326                 if (rc < 0)
    327                         return rc;
    328                 Py_BEGIN_ALLOW_THREADS
    329                 fprintf(fp, "[...]");
    330                 Py_END_ALLOW_THREADS
    331                 return 0;
    332         }
    333         Py_BEGIN_ALLOW_THREADS
    334         fprintf(fp, "[");
    335         Py_END_ALLOW_THREADS
    336         for (i = 0; i < Py_SIZE(op); i++) {
    337                 item = op->ob_item[i];
    338                 Py_INCREF(item);
    339                 if (i > 0) {
    340                         Py_BEGIN_ALLOW_THREADS
    341                         fprintf(fp, ", ");
    342                         Py_END_ALLOW_THREADS
    343                 }
    344                 if (PyObject_Print(item, fp, 0) != 0) {
    345                         Py_DECREF(item);
    346                         Py_ReprLeave((PyObject *)op);
    347                         return -1;
    348                 }
    349                 Py_DECREF(item);
    350         }
    351         Py_BEGIN_ALLOW_THREADS
    352         fprintf(fp, "]");
    353         Py_END_ALLOW_THREADS
    354         Py_ReprLeave((PyObject *)op);
    355         return 0;
     323    int rc;
     324    Py_ssize_t i;
     325    PyObject *item;
     326
     327    rc = Py_ReprEnter((PyObject*)op);
     328    if (rc != 0) {
     329        if (rc < 0)
     330            return rc;
     331        Py_BEGIN_ALLOW_THREADS
     332        fprintf(fp, "[...]");
     333        Py_END_ALLOW_THREADS
     334        return 0;
     335    }
     336    Py_BEGIN_ALLOW_THREADS
     337    fprintf(fp, "[");
     338    Py_END_ALLOW_THREADS
     339    for (i = 0; i < Py_SIZE(op); i++) {
     340        item = op->ob_item[i];
     341        Py_INCREF(item);
     342        if (i > 0) {
     343            Py_BEGIN_ALLOW_THREADS
     344            fprintf(fp, ", ");
     345            Py_END_ALLOW_THREADS
     346        }
     347        if (PyObject_Print(item, fp, 0) != 0) {
     348            Py_DECREF(item);
     349            Py_ReprLeave((PyObject *)op);
     350            return -1;
     351        }
     352        Py_DECREF(item);
     353    }
     354    Py_BEGIN_ALLOW_THREADS
     355    fprintf(fp, "]");
     356    Py_END_ALLOW_THREADS
     357    Py_ReprLeave((PyObject *)op);
     358    return 0;
    356359}
    357360
     
    359362list_repr(PyListObject *v)
    360363{
    361         Py_ssize_t i;
    362         PyObject *s, *temp;
    363         PyObject *pieces = NULL, *result = NULL;
    364 
    365         i = Py_ReprEnter((PyObject*)v);
    366         if (i != 0) {
    367                 return i > 0 ? PyString_FromString("[...]") : NULL;
    368         }
    369 
    370         if (Py_SIZE(v) == 0) {
    371                 result = PyString_FromString("[]");
    372                 goto Done;
    373         }
    374 
    375         pieces = PyList_New(0);
    376         if (pieces == NULL)
    377                 goto Done;
    378 
    379         /* Do repr() on each element.  Note that this may mutate the list,
    380            so must refetch the list size on each iteration. */
    381         for (i = 0; i < Py_SIZE(v); ++i) {
    382                 int status;
    383                 if (Py_EnterRecursiveCall(" while getting the repr of a list"))
    384                         goto Done;
    385                 s = PyObject_Repr(v->ob_item[i]);
    386                 Py_LeaveRecursiveCall();
    387                 if (s == NULL)
    388                         goto Done;
    389                 status = PyList_Append(pieces, s);
    390                 Py_DECREF(s);  /* append created a new ref */
    391                 if (status < 0)
    392                         goto Done;
    393         }
    394 
    395         /* Add "[]" decorations to the first and last items. */
    396         assert(PyList_GET_SIZE(pieces) > 0);
    397         s = PyString_FromString("[");
    398         if (s == NULL)
    399                 goto Done;
    400         temp = PyList_GET_ITEM(pieces, 0);
    401         PyString_ConcatAndDel(&s, temp);
    402         PyList_SET_ITEM(pieces, 0, s);
    403         if (s == NULL)
    404                 goto Done;
    405 
    406         s = PyString_FromString("]");
    407         if (s == NULL)
    408                 goto Done;
    409         temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
    410         PyString_ConcatAndDel(&temp, s);
    411         PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
    412         if (temp == NULL)
    413                 goto Done;
    414 
    415         /* Paste them all together with ", " between. */
    416         s = PyString_FromString(", ");
    417         if (s == NULL)
    418                 goto Done;
    419         result = _PyString_Join(s, pieces);
    420         Py_DECREF(s);
     364    Py_ssize_t i;
     365    PyObject *s, *temp;
     366    PyObject *pieces = NULL, *result = NULL;
     367
     368    i = Py_ReprEnter((PyObject*)v);
     369    if (i != 0) {
     370        return i > 0 ? PyString_FromString("[...]") : NULL;
     371    }
     372
     373    if (Py_SIZE(v) == 0) {
     374        result = PyString_FromString("[]");
     375        goto Done;
     376    }
     377
     378    pieces = PyList_New(0);
     379    if (pieces == NULL)
     380        goto Done;
     381
     382    /* Do repr() on each element.  Note that this may mutate the list,
     383       so must refetch the list size on each iteration. */
     384    for (i = 0; i < Py_SIZE(v); ++i) {
     385        int status;
     386        if (Py_EnterRecursiveCall(" while getting the repr of a list"))
     387            goto Done;
     388        s = PyObject_Repr(v->ob_item[i]);
     389        Py_LeaveRecursiveCall();
     390        if (s == NULL)
     391            goto Done;
     392        status = PyList_Append(pieces, s);
     393        Py_DECREF(s);  /* append created a new ref */
     394        if (status < 0)
     395            goto Done;
     396    }
     397
     398    /* Add "[]" decorations to the first and last items. */
     399    assert(PyList_GET_SIZE(pieces) > 0);
     400    s = PyString_FromString("[");
     401    if (s == NULL)
     402        goto Done;
     403    temp = PyList_GET_ITEM(pieces, 0);
     404    PyString_ConcatAndDel(&s, temp);
     405    PyList_SET_ITEM(pieces, 0, s);
     406    if (s == NULL)
     407        goto Done;
     408
     409    s = PyString_FromString("]");
     410    if (s == NULL)
     411        goto Done;
     412    temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
     413    PyString_ConcatAndDel(&temp, s);
     414    PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
     415    if (temp == NULL)
     416        goto Done;
     417
     418    /* Paste them all together with ", " between. */
     419    s = PyString_FromString(", ");
     420    if (s == NULL)
     421        goto Done;
     422    result = _PyString_Join(s, pieces);
     423    Py_DECREF(s);
    421424
    422425Done:
    423         Py_XDECREF(pieces);
    424         Py_ReprLeave((PyObject *)v);
    425         return result;
     426    Py_XDECREF(pieces);
     427    Py_ReprLeave((PyObject *)v);
     428    return result;
    426429}
    427430
     
    429432list_length(PyListObject *a)
    430433{
    431         return Py_SIZE(a);
     434    return Py_SIZE(a);
    432435}
    433436
     
    435438list_contains(PyListObject *a, PyObject *el)
    436439{
    437         Py_ssize_t i;
    438         int cmp;
    439 
    440         for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
    441                 cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i),
    442                                                    Py_EQ);
    443         return cmp;
     440    Py_ssize_t i;
     441    int cmp;
     442
     443    for (i = 0, cmp = 0 ; cmp == 0 && i < Py_SIZE(a); ++i)
     444        cmp = PyObject_RichCompareBool(el, PyList_GET_ITEM(a, i),
     445                                           Py_EQ);
     446    return cmp;
    444447}
    445448
     
    447450list_item(PyListObject *a, Py_ssize_t i)
    448451{
    449         if (i < 0 || i >= Py_SIZE(a)) {
    450                 if (indexerr == NULL)
    451                         indexerr = PyString_FromString(
    452                                 "list index out of range");
    453                 PyErr_SetObject(PyExc_IndexError, indexerr);
    454                 return NULL;
    455         }
    456         Py_INCREF(a->ob_item[i]);
    457         return a->ob_item[i];
     452    if (i < 0 || i >= Py_SIZE(a)) {
     453        if (indexerr == NULL) {
     454            indexerr = PyString_FromString(
     455                "list index out of range");
     456            if (indexerr == NULL)
     457                return NULL;
     458        }
     459        PyErr_SetObject(PyExc_IndexError, indexerr);
     460        return NULL;
     461    }
     462    Py_INCREF(a->ob_item[i]);
     463    return a->ob_item[i];
    458464}
    459465
     
    461467list_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
    462468{
    463         PyListObject *np;
    464         PyObject **src, **dest;
    465         Py_ssize_t i, len;
    466         if (ilow < 0)
    467                 ilow = 0;
    468         else if (ilow > Py_SIZE(a))
    469                 ilow = Py_SIZE(a);
    470         if (ihigh < ilow)
    471                 ihigh = ilow;
    472         else if (ihigh > Py_SIZE(a))
    473                 ihigh = Py_SIZE(a);
    474         len = ihigh - ilow;
    475         np = (PyListObject *) PyList_New(len);
    476         if (np == NULL)
    477                 return NULL;
    478 
    479         src = a->ob_item + ilow;
    480         dest = np->ob_item;
    481         for (i = 0; i < len; i++) {
    482                 PyObject *v = src[i];
    483                 Py_INCREF(v);
    484                 dest[i] = v;
    485         }
    486         return (PyObject *)np;
     469    PyListObject *np;
     470    PyObject **src, **dest;
     471    Py_ssize_t i, len;
     472    if (ilow < 0)
     473        ilow = 0;
     474    else if (ilow > Py_SIZE(a))
     475        ilow = Py_SIZE(a);
     476    if (ihigh < ilow)
     477        ihigh = ilow;
     478    else if (ihigh > Py_SIZE(a))
     479        ihigh = Py_SIZE(a);
     480    len = ihigh - ilow;
     481    np = (PyListObject *) PyList_New(len);
     482    if (np == NULL)
     483        return NULL;
     484
     485    src = a->ob_item + ilow;
     486    dest = np->ob_item;
     487    for (i = 0; i < len; i++) {
     488        PyObject *v = src[i];
     489        Py_INCREF(v);
     490        dest[i] = v;
     491    }
     492    return (PyObject *)np;
    487493}
    488494
     
    490496PyList_GetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh)
    491497{
    492         if (!PyList_Check(a)) {
    493                 PyErr_BadInternalCall();
    494                 return NULL;
    495         }
    496         return list_slice((PyListObject *)a, ilow, ihigh);
     498    if (!PyList_Check(a)) {
     499        PyErr_BadInternalCall();
     500        return NULL;
     501    }
     502    return list_slice((PyListObject *)a, ilow, ihigh);
    497503}
    498504
     
    500506list_concat(PyListObject *a, PyObject *bb)
    501507{
    502         Py_ssize_t size;
    503         Py_ssize_t i;
    504         PyObject **src, **dest;
    505         PyListObject *np;
    506         if (!PyList_Check(bb)) {
    507                 PyErr_Format(PyExc_TypeError,
    508                           "can only concatenate list (not \"%.200s\") to list",
    509                           bb->ob_type->tp_name);
    510                 return NULL;
    511         }
     508    Py_ssize_t size;
     509    Py_ssize_t i;
     510    PyObject **src, **dest;
     511    PyListObject *np;
     512    if (!PyList_Check(bb)) {
     513        PyErr_Format(PyExc_TypeError,
     514                  "can only concatenate list (not \"%.200s\") to list",
     515                  bb->ob_type->tp_name);
     516        return NULL;
     517    }
    512518#define b ((PyListObject *)bb)
    513         size = Py_SIZE(a) + Py_SIZE(b);
    514         if (size < 0)
    515                 return PyErr_NoMemory();
    516         np = (PyListObject *) PyList_New(size);
    517         if (np == NULL) {
    518                 return NULL;
    519         }
    520         src = a->ob_item;
    521         dest = np->ob_item;
    522         for (i = 0; i < Py_SIZE(a); i++) {
    523                 PyObject *v = src[i];
    524                 Py_INCREF(v);
    525                 dest[i] = v;
    526         }
    527         src = b->ob_item;
    528         dest = np->ob_item + Py_SIZE(a);
    529         for (i = 0; i < Py_SIZE(b); i++) {
    530                 PyObject *v = src[i];
    531                 Py_INCREF(v);
    532                 dest[i] = v;
    533         }
    534         return (PyObject *)np;
     519    size = Py_SIZE(a) + Py_SIZE(b);
     520    if (size < 0)
     521        return PyErr_NoMemory();
     522    np = (PyListObject *) PyList_New(size);
     523    if (np == NULL) {
     524        return NULL;
     525    }
     526    src = a->ob_item;
     527    dest = np->ob_item;
     528    for (i = 0; i < Py_SIZE(a); i++) {
     529        PyObject *v = src[i];
     530        Py_INCREF(v);
     531        dest[i] = v;
     532    }
     533    src = b->ob_item;
     534    dest = np->ob_item + Py_SIZE(a);
     535    for (i = 0; i < Py_SIZE(b); i++) {
     536        PyObject *v = src[i];
     537        Py_INCREF(v);
     538        dest[i] = v;
     539    }
     540    return (PyObject *)np;
    535541#undef b
    536542}
     
    539545list_repeat(PyListObject *a, Py_ssize_t n)
    540546{
    541         Py_ssize_t i, j;
    542         Py_ssize_t size;
    543         PyListObject *np;
    544         PyObject **p, **items;
    545         PyObject *elem;
    546         if (n < 0)
    547                 n = 0;
    548         size = Py_SIZE(a) * n;
    549         if (n && size/n != Py_SIZE(a))
    550                 return PyErr_NoMemory();
    551         if (size == 0)
    552                 return PyList_New(0);
    553         np = (PyListObject *) PyList_New(size);
    554         if (np == NULL)
    555                 return NULL;
    556 
    557         items = np->ob_item;
    558         if (Py_SIZE(a) == 1) {
    559                 elem = a->ob_item[0];
    560                 for (i = 0; i < n; i++) {
    561                         items[i] = elem;
    562                         Py_INCREF(elem);
    563                 }
    564                 return (PyObject *) np;
    565         }
    566         p = np->ob_item;
    567         items = a->ob_item;
    568         for (i = 0; i < n; i++) {
    569                 for (j = 0; j < Py_SIZE(a); j++) {
    570                         *p = items[j];
    571                         Py_INCREF(*p);
    572                         p++;
    573                 }
    574         }
    575         return (PyObject *) np;
     547    Py_ssize_t i, j;
     548    Py_ssize_t size;
     549    PyListObject *np;
     550    PyObject **p, **items;
     551    PyObject *elem;
     552    if (n < 0)
     553        n = 0;
     554    if (n > 0 && Py_SIZE(a) > PY_SSIZE_T_MAX / n)
     555        return PyErr_NoMemory();
     556    size = Py_SIZE(a) * n;
     557    if (size == 0)
     558        return PyList_New(0);
     559    np = (PyListObject *) PyList_New(size);
     560    if (np == NULL)
     561        return NULL;
     562
     563    items = np->ob_item;
     564    if (Py_SIZE(a) == 1) {
     565        elem = a->ob_item[0];
     566        for (i = 0; i < n; i++) {
     567            items[i] = elem;
     568            Py_INCREF(elem);
     569        }
     570        return (PyObject *) np;
     571    }
     572    p = np->ob_item;
     573    items = a->ob_item;
     574    for (i = 0; i < n; i++) {
     575        for (j = 0; j < Py_SIZE(a); j++) {
     576            *p = items[j];
     577            Py_INCREF(*p);
     578            p++;
     579        }
     580    }
     581    return (PyObject *) np;
    576582}
    577583
     
    579585list_clear(PyListObject *a)
    580586{
    581         Py_ssize_t i;
    582         PyObject **item = a->ob_item;
    583         if (item != NULL) {
    584                 /* Because XDECREF can recursively invoke operations on
    585                    this list, we make it empty first. */
    586                 i = Py_SIZE(a);
    587                 Py_SIZE(a) = 0;
    588                 a->ob_item = NULL;
    589                 a->allocated = 0;
    590                 while (--i >= 0) {
    591                         Py_XDECREF(item[i]);
    592                 }
    593                 PyMem_FREE(item);
    594         }
    595         /* Never fails; the return value can be ignored.
    596            Note that there is no guarantee that the list is actually empty
    597            at this point, because XDECREF may have populated it again! */
    598         return 0;
     587    Py_ssize_t i;
     588    PyObject **item = a->ob_item;
     589    if (item != NULL) {
     590        /* Because XDECREF can recursively invoke operations on
     591           this list, we make it empty first. */
     592        i = Py_SIZE(a);
     593        Py_SIZE(a) = 0;
     594        a->ob_item = NULL;
     595        a->allocated = 0;
     596        while (--i >= 0) {
     597            Py_XDECREF(item[i]);
     598        }
     599        PyMem_FREE(item);
     600    }
     601    /* Never fails; the return value can be ignored.
     602       Note that there is no guarantee that the list is actually empty
     603       at this point, because XDECREF may have populated it again! */
     604    return 0;
    599605}
    600606
     
    608614list_ass_slice(PyListObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
    609615{
    610         /* Because [X]DECREF can recursively invoke list operations on
    611            this list, we must postpone all [X]DECREF activity until
    612            after the list is back in its canonical shape.  Therefore
    613            we must allocate an additional array, 'recycle', into which
    614            we temporarily copy the items that are deleted from the
    615            list. :-( */
    616         PyObject *recycle_on_stack[8];
    617         PyObject **recycle = recycle_on_stack; /* will allocate more if needed */
    618         PyObject **item;
    619         PyObject **vitem = NULL;
    620         PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */
    621         Py_ssize_t n; /* # of elements in replacement list */
    622         Py_ssize_t norig; /* # of elements in list getting replaced */
    623         Py_ssize_t d; /* Change in size */
    624         Py_ssize_t k;
    625         size_t s;
    626         int result = -1;        /* guilty until proved innocent */
     616    /* Because [X]DECREF can recursively invoke list operations on
     617       this list, we must postpone all [X]DECREF activity until
     618       after the list is back in its canonical shape.  Therefore
     619       we must allocate an additional array, 'recycle', into which
     620       we temporarily copy the items that are deleted from the
     621       list. :-( */
     622    PyObject *recycle_on_stack[8];
     623    PyObject **recycle = recycle_on_stack; /* will allocate more if needed */
     624    PyObject **item;
     625    PyObject **vitem = NULL;
     626    PyObject *v_as_SF = NULL; /* PySequence_Fast(v) */
     627    Py_ssize_t n; /* # of elements in replacement list */
     628    Py_ssize_t norig; /* # of elements in list getting replaced */
     629    Py_ssize_t d; /* Change in size */
     630    Py_ssize_t k;
     631    size_t s;
     632    int result = -1;            /* guilty until proved innocent */
    627633#define b ((PyListObject *)v)
    628         if (v == NULL)
    629                 n = 0;
    630         else {
    631                 if (a == b) {
    632                         /* Special case "a[i:j] = a" -- copy b first */
    633                         v = list_slice(b, 0, Py_SIZE(b));
    634                         if (v == NULL)
    635                                 return result;
    636                         result = list_ass_slice(a, ilow, ihigh, v);
    637                         Py_DECREF(v);
    638                         return result;
    639                 }
    640                 v_as_SF = PySequence_Fast(v, "can only assign an iterable");
    641                 if(v_as_SF == NULL)
    642                         goto Error;
    643                 n = PySequence_Fast_GET_SIZE(v_as_SF);
    644                 vitem = PySequence_Fast_ITEMS(v_as_SF);
    645         }
    646         if (ilow < 0)
    647                 ilow = 0;
    648         else if (ilow > Py_SIZE(a))
    649                 ilow = Py_SIZE(a);
    650 
    651         if (ihigh < ilow)
    652                 ihigh = ilow;
    653         else if (ihigh > Py_SIZE(a))
    654                 ihigh = Py_SIZE(a);
    655 
    656         norig = ihigh - ilow;
    657         assert(norig >= 0);
    658         d = n - norig;
    659         if (Py_SIZE(a) + d == 0) {
    660                 Py_XDECREF(v_as_SF);
    661                 return list_clear(a);
    662         }
    663         item = a->ob_item;
    664         /* recycle the items that we are about to remove */
    665         s = norig * sizeof(PyObject *);
    666         if (s > sizeof(recycle_on_stack)) {
    667                 recycle = (PyObject **)PyMem_MALLOC(s);
    668                 if (recycle == NULL) {
    669                         PyErr_NoMemory();
    670                         goto Error;
    671                 }
    672         }
    673         memcpy(recycle, &item[ilow], s);
    674 
    675         if (d < 0) { /* Delete -d items */
    676                 memmove(&item[ihigh+d], &item[ihigh],
    677                         (Py_SIZE(a) - ihigh)*sizeof(PyObject *));
    678                 list_resize(a, Py_SIZE(a) + d);
    679                 item = a->ob_item;
    680         }
    681         else if (d > 0) { /* Insert d items */
    682                 k = Py_SIZE(a);
    683                 if (list_resize(a, k+d) < 0)
    684                         goto Error;
    685                 item = a->ob_item;
    686                 memmove(&item[ihigh+d], &item[ihigh],
    687                         (k - ihigh)*sizeof(PyObject *));
    688         }
    689         for (k = 0; k < n; k++, ilow++) {
    690                 PyObject *w = vitem[k];
    691                 Py_XINCREF(w);
    692                 item[ilow] = w;
    693         }
    694         for (k = norig - 1; k >= 0; --k)
    695                 Py_XDECREF(recycle[k]);
    696         result = 0;
     634    if (v == NULL)
     635        n = 0;
     636    else {
     637        if (a == b) {
     638            /* Special case "a[i:j] = a" -- copy b first */
     639            v = list_slice(b, 0, Py_SIZE(b));
     640            if (v == NULL)
     641                return result;
     642            result = list_ass_slice(a, ilow, ihigh, v);
     643            Py_DECREF(v);
     644            return result;
     645        }
     646        v_as_SF = PySequence_Fast(v, "can only assign an iterable");
     647        if(v_as_SF == NULL)
     648            goto Error;
     649        n = PySequence_Fast_GET_SIZE(v_as_SF);
     650        vitem = PySequence_Fast_ITEMS(v_as_SF);
     651    }
     652    if (ilow < 0)
     653        ilow = 0;
     654    else if (ilow > Py_SIZE(a))
     655        ilow = Py_SIZE(a);
     656
     657    if (ihigh < ilow)
     658        ihigh = ilow;
     659    else if (ihigh > Py_SIZE(a))
     660        ihigh = Py_SIZE(a);
     661
     662    norig = ihigh - ilow;
     663    assert(norig >= 0);
     664    d = n - norig;
     665    if (Py_SIZE(a) + d == 0) {
     666        Py_XDECREF(v_as_SF);
     667        return list_clear(a);
     668    }
     669    item = a->ob_item;
     670    /* recycle the items that we are about to remove */
     671    s = norig * sizeof(PyObject *);
     672    if (s > sizeof(recycle_on_stack)) {
     673        recycle = (PyObject **)PyMem_MALLOC(s);
     674        if (recycle == NULL) {
     675            PyErr_NoMemory();
     676            goto Error;
     677        }
     678    }
     679    memcpy(recycle, &item[ilow], s);
     680
     681    if (d < 0) { /* Delete -d items */
     682        memmove(&item[ihigh+d], &item[ihigh],
     683            (Py_SIZE(a) - ihigh)*sizeof(PyObject *));
     684        list_resize(a, Py_SIZE(a) + d);
     685        item = a->ob_item;
     686    }
     687    else if (d > 0) { /* Insert d items */
     688        k = Py_SIZE(a);
     689        if (list_resize(a, k+d) < 0)
     690            goto Error;
     691        item = a->ob_item;
     692        memmove(&item[ihigh+d], &item[ihigh],
     693            (k - ihigh)*sizeof(PyObject *));
     694    }
     695    for (k = 0; k < n; k++, ilow++) {
     696        PyObject *w = vitem[k];
     697        Py_XINCREF(w);
     698        item[ilow] = w;
     699    }
     700    for (k = norig - 1; k >= 0; --k)
     701        Py_XDECREF(recycle[k]);
     702    result = 0;
    697703 Error:
    698         if (recycle != recycle_on_stack)
    699                 PyMem_FREE(recycle);
    700         Py_XDECREF(v_as_SF);
    701         return result;
     704    if (recycle != recycle_on_stack)
     705        PyMem_FREE(recycle);
     706    Py_XDECREF(v_as_SF);
     707    return result;
    702708#undef b
    703709}
     
    706712PyList_SetSlice(PyObject *a, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
    707713{
    708         if (!PyList_Check(a)) {
    709                 PyErr_BadInternalCall();
    710                 return -1;
    711         }
    712         return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
     714    if (!PyList_Check(a)) {
     715        PyErr_BadInternalCall();
     716        return -1;
     717    }
     718    return list_ass_slice((PyListObject *)a, ilow, ihigh, v);
    713719}
    714720
     
    716722list_inplace_repeat(PyListObject *self, Py_ssize_t n)
    717723{
    718         PyObject **items;
    719         Py_ssize_t size, i, j, p;
    720 
    721 
    722         size = PyList_GET_SIZE(self);
    723         if (size == 0 || n == 1) {
    724                 Py_INCREF(self);
    725                 return (PyObject *)self;
    726         }
    727 
    728         if (n < 1) {
    729                 (void)list_clear(self);
    730                 Py_INCREF(self);
    731                 return (PyObject *)self;
    732         }
    733 
    734         if (size > PY_SSIZE_T_MAX / n) {
    735                 return PyErr_NoMemory();
    736         }
    737 
    738         if (list_resize(self, size*n) == -1)
    739                 return NULL;
    740 
    741         p = size;
    742         items = self->ob_item;
    743         for (i = 1; i < n; i++) { /* Start counting at 1, not 0 */
    744                 for (j = 0; j < size; j++) {
    745                         PyObject *o = items[j];
    746                         Py_INCREF(o);
    747                         items[p++] = o;
    748                 }
    749         }
    750         Py_INCREF(self);
    751         return (PyObject *)self;
     724    PyObject **items;
     725    Py_ssize_t size, i, j, p;
     726
     727
     728    size = PyList_GET_SIZE(self);
     729    if (size == 0 || n == 1) {
     730        Py_INCREF(self);
     731        return (PyObject *)self;
     732    }
     733
     734    if (n < 1) {
     735        (void)list_clear(self);
     736        Py_INCREF(self);
     737        return (PyObject *)self;
     738    }
     739
     740    if (size > PY_SSIZE_T_MAX / n) {
     741        return PyErr_NoMemory();
     742    }
     743
     744    if (list_resize(self, size*n) == -1)
     745        return NULL;
     746
     747    p = size;
     748    items = self->ob_item;
     749    for (i = 1; i < n; i++) { /* Start counting at 1, not 0 */
     750        for (j = 0; j < size; j++) {
     751            PyObject *o = items[j];
     752            Py_INCREF(o);
     753            items[p++] = o;
     754        }
     755    }
     756    Py_INCREF(self);
     757    return (PyObject *)self;
    752758}
    753759
     
    755761list_ass_item(PyListObject *a, Py_ssize_t i, PyObject *v)
    756762{
    757         PyObject *old_value;
    758         if (i < 0 || i >= Py_SIZE(a)) {
    759                 PyErr_SetString(PyExc_IndexError,
    760                                 "list assignment index out of range");
    761                 return -1;
    762         }
    763         if (v == NULL)
    764                 return list_ass_slice(a, i, i+1, v);
    765         Py_INCREF(v);
    766         old_value = a->ob_item[i];
    767         a->ob_item[i] = v;
    768         Py_DECREF(old_value);
    769         return 0;
     763    PyObject *old_value;
     764    if (i < 0 || i >= Py_SIZE(a)) {
     765        PyErr_SetString(PyExc_IndexError,
     766                        "list assignment index out of range");
     767        return -1;
     768    }
     769    if (v == NULL)
     770        return list_ass_slice(a, i, i+1, v);
     771    Py_INCREF(v);
     772    old_value = a->ob_item[i];
     773    a->ob_item[i] = v;
     774    Py_DECREF(old_value);
     775    return 0;
    770776}
    771777
     
    773779listinsert(PyListObject *self, PyObject *args)
    774780{
    775         Py_ssize_t i;
    776         PyObject *v;
    777         if (!PyArg_ParseTuple(args, "nO:insert", &i, &v))
    778                 return NULL;
    779         if (ins1(self, i, v) == 0)
    780                 Py_RETURN_NONE;
    781         return NULL;
     781    Py_ssize_t i;
     782    PyObject *v;
     783    if (!PyArg_ParseTuple(args, "nO:insert", &i, &v))
     784        return NULL;
     785    if (ins1(self, i, v) == 0)
     786        Py_RETURN_NONE;
     787    return NULL;
    782788}
    783789
     
    785791listappend(PyListObject *self, PyObject *v)
    786792{
    787         if (app1(self, v) == 0)
    788                 Py_RETURN_NONE;
    789         return NULL;
     793    if (app1(self, v) == 0)
     794        Py_RETURN_NONE;
     795    return NULL;
    790796}
    791797
     
    793799listextend(PyListObject *self, PyObject *b)
    794800{
    795         PyObject *it;      /* iter(v) */
    796         Py_ssize_t m;              /* size of self */
    797         Py_ssize_t n;              /* guess for size of b */
    798         Py_ssize_t mn;             /* m + n */
    799         Py_ssize_t i;
    800         PyObject *(*iternext)(PyObject *);
    801 
    802         /* Special cases:
    803            1) lists and tuples which can use PySequence_Fast ops
    804            2) extending self to self requires making a copy first
    805         */
    806         if (PyList_CheckExact(b) || PyTuple_CheckExact(b) || (PyObject *)self == b) {
    807                 PyObject **src, **dest;
    808                 b = PySequence_Fast(b, "argument must be iterable");
    809                 if (!b)
    810                         return NULL;
    811                 n = PySequence_Fast_GET_SIZE(b);
    812                 if (n == 0) {
    813                         /* short circuit when b is empty */
    814                         Py_DECREF(b);
    815                         Py_RETURN_NONE;
    816                 }
    817                 m = Py_SIZE(self);
    818                 if (list_resize(self, m + n) == -1) {
    819                         Py_DECREF(b);
    820                         return NULL;
    821                 }
    822                 /* note that we may still have self == b here for the
    823                 * situation a.extend(a), but the following code works
    824                 * in that case too.  Just make sure to resize self
    825                 * before calling PySequence_Fast_ITEMS.
    826                 */
    827                 /* populate the end of self with b's items */
    828                 src = PySequence_Fast_ITEMS(b);
    829                 dest = self->ob_item + m;
    830                 for (i = 0; i < n; i++) {
    831                         PyObject *o = src[i];
    832                         Py_INCREF(o);
    833                         dest[i] = o;
    834                 }
    835                 Py_DECREF(b);
    836                 Py_RETURN_NONE;
    837         }
    838 
    839         it = PyObject_GetIter(b);
    840         if (it == NULL)
    841                 return NULL;
    842         iternext = *it->ob_type->tp_iternext;
    843 
    844         /* Guess a result list size. */
    845         n = _PyObject_LengthHint(b, 8);
    846         if (n == -1) {
    847                 Py_DECREF(it);
    848                 return NULL;
    849         }
    850         m = Py_SIZE(self);
    851         mn = m + n;
    852         if (mn >= m) {
    853                 /* Make room. */
    854                 if (list_resize(self, mn) == -1)
    855                         goto error;
    856                 /* Make the list sane again. */
    857                 Py_SIZE(self) = m;
    858         }
    859         /* Else m + n overflowed; on the chance that n lied, and there really
    860         * is enough room, ignore it.  If n was telling the truth, we'll
    861         * eventually run out of memory during the loop.
    862         */
    863 
    864         /* Run iterator to exhaustion. */
    865         for (;;) {
    866                 PyObject *item = iternext(it);
    867                 if (item == NULL) {
    868                         if (PyErr_Occurred()) {
    869                                 if (PyErr_ExceptionMatches(PyExc_StopIteration))
    870                                         PyErr_Clear();
    871                                 else
    872                                         goto error;
    873                         }
    874                         break;
    875                 }
    876                 if (Py_SIZE(self) < self->allocated) {
    877                         /* steals ref */
    878                         PyList_SET_ITEM(self, Py_SIZE(self), item);
    879                         ++Py_SIZE(self);
    880                 }
    881                 else {
    882                         int status = app1(self, item);
    883                         Py_DECREF(item);  /* append creates a new ref */
    884                         if (status < 0)
    885                                 goto error;
    886                 }
    887         }
    888 
    889         /* Cut back result list if initial guess was too large. */
    890         if (Py_SIZE(self) < self->allocated)
    891                 list_resize(self, Py_SIZE(self));  /* shrinking can't fail */
    892 
    893         Py_DECREF(it);
    894         Py_RETURN_NONE;
     801    PyObject *it;      /* iter(v) */
     802    Py_ssize_t m;                  /* size of self */
     803    Py_ssize_t n;                  /* guess for size of b */
     804    Py_ssize_t mn;                 /* m + n */
     805    Py_ssize_t i;
     806    PyObject *(*iternext)(PyObject *);
     807
     808    /* Special cases:
     809       1) lists and tuples which can use PySequence_Fast ops
     810       2) extending self to self requires making a copy first
     811    */
     812    if (PyList_CheckExact(b) || PyTuple_CheckExact(b) || (PyObject *)self == b) {
     813        PyObject **src, **dest;
     814        b = PySequence_Fast(b, "argument must be iterable");
     815        if (!b)
     816            return NULL;
     817        n = PySequence_Fast_GET_SIZE(b);
     818        if (n == 0) {
     819            /* short circuit when b is empty */
     820            Py_DECREF(b);
     821            Py_RETURN_NONE;
     822        }
     823        m = Py_SIZE(self);
     824        if (list_resize(self, m + n) == -1) {
     825            Py_DECREF(b);
     826            return NULL;
     827        }
     828        /* note that we may still have self == b here for the
     829        * situation a.extend(a), but the following code works
     830        * in that case too.  Just make sure to resize self
     831        * before calling PySequence_Fast_ITEMS.
     832        */
     833        /* populate the end of self with b's items */
     834        src = PySequence_Fast_ITEMS(b);
     835        dest = self->ob_item + m;
     836        for (i = 0; i < n; i++) {
     837            PyObject *o = src[i];
     838            Py_INCREF(o);
     839            dest[i] = o;
     840        }
     841        Py_DECREF(b);
     842        Py_RETURN_NONE;
     843    }
     844
     845    it = PyObject_GetIter(b);
     846    if (it == NULL)
     847        return NULL;
     848    iternext = *it->ob_type->tp_iternext;
     849
     850    /* Guess a result list size. */
     851    n = _PyObject_LengthHint(b, 8);
     852    if (n == -1) {
     853        Py_DECREF(it);
     854        return NULL;
     855    }
     856    m = Py_SIZE(self);
     857    mn = m + n;
     858    if (mn >= m) {
     859        /* Make room. */
     860        if (list_resize(self, mn) == -1)
     861            goto error;
     862        /* Make the list sane again. */
     863        Py_SIZE(self) = m;
     864    }
     865    /* Else m + n overflowed; on the chance that n lied, and there really
     866    * is enough room, ignore it.  If n was telling the truth, we'll
     867    * eventually run out of memory during the loop.
     868    */
     869
     870    /* Run iterator to exhaustion. */
     871    for (;;) {
     872        PyObject *item = iternext(it);
     873        if (item == NULL) {
     874            if (PyErr_Occurred()) {
     875                if (PyErr_ExceptionMatches(PyExc_StopIteration))
     876                    PyErr_Clear();
     877                else
     878                    goto error;
     879            }
     880            break;
     881        }
     882        if (Py_SIZE(self) < self->allocated) {
     883            /* steals ref */
     884            PyList_SET_ITEM(self, Py_SIZE(self), item);
     885            ++Py_SIZE(self);
     886        }
     887        else {
     888            int status = app1(self, item);
     889            Py_DECREF(item);  /* append creates a new ref */
     890            if (status < 0)
     891                goto error;
     892        }
     893    }
     894
     895    /* Cut back result list if initial guess was too large. */
     896    if (Py_SIZE(self) < self->allocated)
     897        list_resize(self, Py_SIZE(self));  /* shrinking can't fail */
     898
     899    Py_DECREF(it);
     900    Py_RETURN_NONE;
    895901
    896902  error:
    897         Py_DECREF(it);
    898         return NULL;
     903    Py_DECREF(it);
     904    return NULL;
    899905}
    900906
     
    902908_PyList_Extend(PyListObject *self, PyObject *b)
    903909{
    904         return listextend(self, b);
     910    return listextend(self, b);
    905911}
    906912
     
    908914list_inplace_concat(PyListObject *self, PyObject *other)
    909915{
    910         PyObject *result;
    911 
    912         result = listextend(self, other);
    913         if (result == NULL)
    914                 return result;
    915         Py_DECREF(result);
    916         Py_INCREF(self);
    917         return (PyObject *)self;
     916    PyObject *result;
     917
     918    result = listextend(self, other);
     919    if (result == NULL)
     920        return result;
     921    Py_DECREF(result);
     922    Py_INCREF(self);
     923    return (PyObject *)self;
    918924}
    919925
     
    921927listpop(PyListObject *self, PyObject *args)
    922928{
    923         Py_ssize_t i = -1;
    924         PyObject *v;
    925         int status;
    926 
    927         if (!PyArg_ParseTuple(args, "|n:pop", &i))
    928                 return NULL;
    929 
    930         if (Py_SIZE(self) == 0) {
    931                 /* Special-case most common failure cause */
    932                 PyErr_SetString(PyExc_IndexError, "pop from empty list");
    933                 return NULL;
    934         }
    935         if (i < 0)
    936                 i += Py_SIZE(self);
    937         if (i < 0 || i >= Py_SIZE(self)) {
    938                 PyErr_SetString(PyExc_IndexError, "pop index out of range");
    939                 return NULL;
    940         }
    941         v = self->ob_item[i];
    942         if (i == Py_SIZE(self) - 1) {
    943                 status = list_resize(self, Py_SIZE(self) - 1);
    944                 assert(status >= 0);
    945                 return v; /* and v now owns the reference the list had */
    946         }
    947         Py_INCREF(v);
    948         status = list_ass_slice(self, i, i+1, (PyObject *)NULL);
    949         assert(status >= 0);
    950         /* Use status, so that in a release build compilers don't
    951         * complain about the unused name.
    952         */
    953         (void) status;
    954 
    955         return v;
     929    Py_ssize_t i = -1;
     930    PyObject *v;
     931    int status;
     932
     933    if (!PyArg_ParseTuple(args, "|n:pop", &i))
     934        return NULL;
     935
     936    if (Py_SIZE(self) == 0) {
     937        /* Special-case most common failure cause */
     938        PyErr_SetString(PyExc_IndexError, "pop from empty list");
     939        return NULL;
     940    }
     941    if (i < 0)
     942        i += Py_SIZE(self);
     943    if (i < 0 || i >= Py_SIZE(self)) {
     944        PyErr_SetString(PyExc_IndexError, "pop index out of range");
     945        return NULL;
     946    }
     947    v = self->ob_item[i];
     948    if (i == Py_SIZE(self) - 1) {
     949        status = list_resize(self, Py_SIZE(self) - 1);
     950        assert(status >= 0);
     951        return v; /* and v now owns the reference the list had */
     952    }
     953    Py_INCREF(v);
     954    status = list_ass_slice(self, i, i+1, (PyObject *)NULL);
     955    assert(status >= 0);
     956    /* Use status, so that in a release build compilers don't
     957    * complain about the unused name.
     958    */
     959    (void) status;
     960
     961    return v;
    956962}
    957963
     
    960966reverse_slice(PyObject **lo, PyObject **hi)
    961967{
    962         assert(lo && hi);
    963 
    964         --hi;
    965         while (lo < hi) {
    966                 PyObject *t = *lo;
    967                 *lo = *hi;
    968                 *hi = t;
    969                 ++lo;
    970                 --hi;
    971         }
     968    assert(lo && hi);
     969
     970    --hi;
     971    while (lo < hi) {
     972        PyObject *t = *lo;
     973        *lo = *hi;
     974        *hi = t;
     975        ++lo;
     976        --hi;
     977    }
    972978}
    973979
     
    985991islt(PyObject *x, PyObject *y, PyObject *compare)
    986992{
    987         PyObject *res;
    988         PyObject *args;
    989         Py_ssize_t i;
    990 
    991         assert(compare != NULL);
    992         /* Call the user's comparison function and translate the 3-way
    993         * result into true or false (or error).
    994         */
    995         args = PyTuple_New(2);
    996         if (args == NULL)
    997                 return -1;
    998         Py_INCREF(x);
    999         Py_INCREF(y);
    1000         PyTuple_SET_ITEM(args, 0, x);
    1001         PyTuple_SET_ITEM(args, 1, y);
    1002         res = PyObject_Call(compare, args, NULL);
    1003         Py_DECREF(args);
    1004         if (res == NULL)
    1005                 return -1;
    1006         if (!PyInt_Check(res)) {
    1007                 PyErr_Format(PyExc_TypeError,
    1008                              "comparison function must return int, not %.200s",
    1009                              res->ob_type->tp_name);
    1010                 Py_DECREF(res);
    1011                 return -1;
    1012         }
    1013         i = PyInt_AsLong(res);
    1014         Py_DECREF(res);
    1015         return i < 0;
     993    PyObject *res;
     994    PyObject *args;
     995    Py_ssize_t i;
     996
     997    assert(compare != NULL);
     998    /* Call the user's comparison function and translate the 3-way
     999    * result into true or false (or error).
     1000    */
     1001    args = PyTuple_New(2);
     1002    if (args == NULL)
     1003        return -1;
     1004    Py_INCREF(x);
     1005    Py_INCREF(y);
     1006    PyTuple_SET_ITEM(args, 0, x);
     1007    PyTuple_SET_ITEM(args, 1, y);
     1008    res = PyObject_Call(compare, args, NULL);
     1009    Py_DECREF(args);
     1010    if (res == NULL)
     1011        return -1;
     1012    if (!PyInt_Check(res)) {
     1013        PyErr_Format(PyExc_TypeError,
     1014                     "comparison function must return int, not %.200s",
     1015                     res->ob_type->tp_name);
     1016        Py_DECREF(res);
     1017        return -1;
     1018    }
     1019    i = PyInt_AsLong(res);
     1020    Py_DECREF(res);
     1021    return i < 0;
    10161022}
    10171023
     
    10211027 * Returns -1 on error, 1 if x < y, 0 if x >= y.
    10221028 */
    1023 #define ISLT(X, Y, COMPARE) ((COMPARE) == NULL ?                        \
    1024                              PyObject_RichCompareBool(X, Y, Py_LT) :    \
    1025                              islt(X, Y, COMPARE))
     1029#define ISLT(X, Y, COMPARE) ((COMPARE) == NULL ?                        \
     1030                 PyObject_RichCompareBool(X, Y, Py_LT) :                \
     1031                 islt(X, Y, COMPARE))
    10261032
    10271033/* Compare X to Y via "<".  Goto "fail" if the comparison raises an
     
    10301036*/
    10311037#define IFLT(X, Y) if ((k = ISLT(X, Y, compare)) < 0) goto fail;  \
    1032                    if (k)
     1038           if (k)
    10331039
    10341040/* binarysort is the best method for sorting small arrays: it does
     
    10471053     /* compare -- comparison function object, or NULL for default */
    10481054{
    1049         register Py_ssize_t k;
    1050         register PyObject **l, **p, **r;
    1051         register PyObject *pivot;
    1052 
    1053         assert(lo <= start && start <= hi);
    1054         /* assert [lo, start) is sorted */
    1055         if (lo == start)
    1056                 ++start;
    1057         for (; start < hi; ++start) {
    1058                 /* set l to where *start belongs */
    1059                 l = lo;
    1060                 r = start;
    1061                 pivot = *r;
    1062                 /* Invariants:
    1063                 * pivot >= all in [lo, l).
    1064                 * pivot  < all in [r, start).
    1065                 * The second is vacuously true at the start.
    1066                 */
    1067                 assert(l < r);
    1068                 do {
    1069                         p = l + ((r - l) >> 1);
    1070                         IFLT(pivot, *p)
    1071                                 r = p;
    1072                         else
    1073                                 l = p+1;
    1074                 } while (l < r);
    1075                 assert(l == r);
    1076                 /* The invariants still hold, so pivot >= all in [lo, l) and
    1077                    pivot < all in [l, start), so pivot belongs at l.  Note
    1078                    that if there are elements equal to pivot, l points to the
    1079                    first slot after them -- that's why this sort is stable.
    1080                    Slide over to make room.
    1081                    Caution: using memmove is much slower under MSVC 5;
    1082                    we're not usually moving many slots. */
    1083                 for (p = start; p > l; --p)
    1084                         *p = *(p-1);
    1085                 *l = pivot;
    1086         }
    1087         return 0;
     1055    register Py_ssize_t k;
     1056    register PyObject **l, **p, **r;
     1057    register PyObject *pivot;
     1058
     1059    assert(lo <= start && start <= hi);
     1060    /* assert [lo, start) is sorted */
     1061    if (lo == start)
     1062        ++start;
     1063    for (; start < hi; ++start) {
     1064        /* set l to where *start belongs */
     1065        l = lo;
     1066        r = start;
     1067        pivot = *r;
     1068        /* Invariants:
     1069        * pivot >= all in [lo, l).
     1070        * pivot  < all in [r, start).
     1071        * The second is vacuously true at the start.
     1072        */
     1073        assert(l < r);
     1074        do {
     1075            p = l + ((r - l) >> 1);
     1076            IFLT(pivot, *p)
     1077                r = p;
     1078            else
     1079                l = p+1;
     1080        } while (l < r);
     1081        assert(l == r);
     1082        /* The invariants still hold, so pivot >= all in [lo, l) and
     1083           pivot < all in [l, start), so pivot belongs at l.  Note
     1084           that if there are elements equal to pivot, l points to the
     1085           first slot after them -- that's why this sort is stable.
     1086           Slide over to make room.
     1087           Caution: using memmove is much slower under MSVC 5;
     1088           we're not usually moving many slots. */
     1089        for (p = start; p > l; --p)
     1090            *p = *(p-1);
     1091        *l = pivot;
     1092    }
     1093    return 0;
    10881094
    10891095 fail:
    1090         return -1;
     1096    return -1;
    10911097}
    10921098
     
    11121118count_run(PyObject **lo, PyObject **hi, PyObject *compare, int *descending)
    11131119{
    1114         Py_ssize_t k;
    1115         Py_ssize_t n;
    1116 
    1117         assert(lo < hi);
    1118         *descending = 0;
    1119         ++lo;
    1120         if (lo == hi)
    1121                 return 1;
    1122 
    1123         n = 2;
    1124         IFLT(*lo, *(lo-1)) {
    1125                 *descending = 1;
    1126                 for (lo = lo+1; lo < hi; ++lo, ++n) {
    1127                         IFLT(*lo, *(lo-1))
    1128                                 ;
    1129                         else
    1130                                 break;
    1131                 }
    1132         }
    1133         else {
    1134                 for (lo = lo+1; lo < hi; ++lo, ++n) {
    1135                         IFLT(*lo, *(lo-1))
    1136                                 break;
    1137                 }
    1138         }
    1139 
    1140         return n;
     1120    Py_ssize_t k;
     1121    Py_ssize_t n;
     1122
     1123    assert(lo < hi);
     1124    *descending = 0;
     1125    ++lo;
     1126    if (lo == hi)
     1127        return 1;
     1128
     1129    n = 2;
     1130    IFLT(*lo, *(lo-1)) {
     1131        *descending = 1;
     1132        for (lo = lo+1; lo < hi; ++lo, ++n) {
     1133            IFLT(*lo, *(lo-1))
     1134                ;
     1135            else
     1136                break;
     1137        }
     1138    }
     1139    else {
     1140        for (lo = lo+1; lo < hi; ++lo, ++n) {
     1141            IFLT(*lo, *(lo-1))
     1142                break;
     1143        }
     1144    }
     1145
     1146    return n;
    11411147fail:
    1142         return -1;
     1148    return -1;
    11431149}
    11441150
     
    11671173gallop_left(PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint, PyObject *compare)
    11681174{
    1169         Py_ssize_t ofs;
    1170         Py_ssize_t lastofs;
    1171         Py_ssize_t k;
    1172 
    1173         assert(key && a && n > 0 && hint >= 0 && hint < n);
    1174 
    1175         a += hint;
    1176         lastofs = 0;
    1177         ofs = 1;
    1178         IFLT(*a, key) {
    1179                 /* a[hint] < key -- gallop right, until
    1180                 * a[hint + lastofs] < key <= a[hint + ofs]
    1181                 */
    1182                 const Py_ssize_t maxofs = n - hint;     /* &a[n-1] is highest */
    1183                 while (ofs < maxofs) {
    1184                         IFLT(a[ofs], key) {
    1185                                 lastofs = ofs;
    1186                                 ofs = (ofs << 1) + 1;
    1187                                 if (ofs <= 0)   /* int overflow */
    1188                                         ofs = maxofs;
    1189                         }
    1190                         else    /* key <= a[hint + ofs] */
    1191                                 break;
    1192                 }
    1193                 if (ofs > maxofs)
    1194                         ofs = maxofs;
    1195                 /* Translate back to offsets relative to &a[0]. */
    1196                 lastofs += hint;
    1197                 ofs += hint;
    1198         }
    1199         else {
    1200                 /* key <= a[hint] -- gallop left, until
    1201                 * a[hint - ofs] < key <= a[hint - lastofs]
    1202                 */
    1203                 const Py_ssize_t maxofs = hint + 1;     /* &a[0] is lowest */
    1204                 while (ofs < maxofs) {
    1205                         IFLT(*(a-ofs), key)
    1206                                 break;
    1207                         /* key <= a[hint - ofs] */
    1208                         lastofs = ofs;
    1209                         ofs = (ofs << 1) + 1;
    1210                         if (ofs <= 0)   /* int overflow */
    1211                                 ofs = maxofs;
    1212                 }
    1213                 if (ofs > maxofs)
    1214                         ofs = maxofs;
    1215                 /* Translate back to positive offsets relative to &a[0]. */
    1216                 k = lastofs;
    1217                 lastofs = hint - ofs;
    1218                 ofs = hint - k;
    1219         }
    1220         a -= hint;
    1221 
    1222         assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
    1223         /* Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the
    1224         * right of lastofs but no farther right than ofs.  Do a binary
    1225         * search, with invariant a[lastofs-1] < key <= a[ofs].
    1226         */
    1227         ++lastofs;
    1228         while (lastofs < ofs) {
    1229                 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
    1230 
    1231                 IFLT(a[m], key)
    1232                         lastofs = m+1;  /* a[m] < key */
    1233                 else
    1234                         ofs = m;        /* key <= a[m] */
    1235         }
    1236         assert(lastofs == ofs);         /* so a[ofs-1] < key <= a[ofs] */
    1237         return ofs;
     1175    Py_ssize_t ofs;
     1176    Py_ssize_t lastofs;
     1177    Py_ssize_t k;
     1178
     1179    assert(key && a && n > 0 && hint >= 0 && hint < n);
     1180
     1181    a += hint;
     1182    lastofs = 0;
     1183    ofs = 1;
     1184    IFLT(*a, key) {
     1185        /* a[hint] < key -- gallop right, until
     1186        * a[hint + lastofs] < key <= a[hint + ofs]
     1187        */
     1188        const Py_ssize_t maxofs = n - hint;             /* &a[n-1] is highest */
     1189        while (ofs < maxofs) {
     1190            IFLT(a[ofs], key) {
     1191                lastofs = ofs;
     1192                ofs = (ofs << 1) + 1;
     1193                if (ofs <= 0)                   /* int overflow */
     1194                    ofs = maxofs;
     1195            }
     1196            else                /* key <= a[hint + ofs] */
     1197                break;
     1198        }
     1199        if (ofs > maxofs)
     1200            ofs = maxofs;
     1201        /* Translate back to offsets relative to &a[0]. */
     1202        lastofs += hint;
     1203        ofs += hint;
     1204    }
     1205    else {
     1206        /* key <= a[hint] -- gallop left, until
     1207        * a[hint - ofs] < key <= a[hint - lastofs]
     1208        */
     1209        const Py_ssize_t maxofs = hint + 1;             /* &a[0] is lowest */
     1210        while (ofs < maxofs) {
     1211            IFLT(*(a-ofs), key)
     1212                break;
     1213            /* key <= a[hint - ofs] */
     1214            lastofs = ofs;
     1215            ofs = (ofs << 1) + 1;
     1216            if (ofs <= 0)               /* int overflow */
     1217                ofs = maxofs;
     1218        }
     1219        if (ofs > maxofs)
     1220            ofs = maxofs;
     1221        /* Translate back to positive offsets relative to &a[0]. */
     1222        k = lastofs;
     1223        lastofs = hint - ofs;
     1224        ofs = hint - k;
     1225    }
     1226    a -= hint;
     1227
     1228    assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
     1229    /* Now a[lastofs] < key <= a[ofs], so key belongs somewhere to the
     1230    * right of lastofs but no farther right than ofs.  Do a binary
     1231    * search, with invariant a[lastofs-1] < key <= a[ofs].
     1232    */
     1233    ++lastofs;
     1234    while (lastofs < ofs) {
     1235        Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
     1236
     1237        IFLT(a[m], key)
     1238            lastofs = m+1;              /* a[m] < key */
     1239        else
     1240            ofs = m;                    /* key <= a[m] */
     1241    }
     1242    assert(lastofs == ofs);             /* so a[ofs-1] < key <= a[ofs] */
     1243    return ofs;
    12381244
    12391245fail:
    1240         return -1;
     1246    return -1;
    12411247}
    12421248
     
    12581264gallop_right(PyObject *key, PyObject **a, Py_ssize_t n, Py_ssize_t hint, PyObject *compare)
    12591265{
    1260         Py_ssize_t ofs;
    1261         Py_ssize_t lastofs;
    1262         Py_ssize_t k;
    1263 
    1264         assert(key && a && n > 0 && hint >= 0 && hint < n);
    1265 
    1266         a += hint;
    1267         lastofs = 0;
    1268         ofs = 1;
    1269         IFLT(key, *a) {
    1270                 /* key < a[hint] -- gallop left, until
    1271                 * a[hint - ofs] <= key < a[hint - lastofs]
    1272                 */
    1273                 const Py_ssize_t maxofs = hint + 1;     /* &a[0] is lowest */
    1274                 while (ofs < maxofs) {
    1275                         IFLT(key, *(a-ofs)) {
    1276                                 lastofs = ofs;
    1277                                 ofs = (ofs << 1) + 1;
    1278                                 if (ofs <= 0)   /* int overflow */
    1279                                         ofs = maxofs;
    1280                         }
    1281                         else    /* a[hint - ofs] <= key */
    1282                                 break;
    1283                 }
    1284                 if (ofs > maxofs)
    1285                         ofs = maxofs;
    1286                 /* Translate back to positive offsets relative to &a[0]. */
    1287                 k = lastofs;
    1288                 lastofs = hint - ofs;
    1289                 ofs = hint - k;
    1290         }
    1291         else {
    1292                 /* a[hint] <= key -- gallop right, until
    1293                 * a[hint + lastofs] <= key < a[hint + ofs]
    1294                 */
    1295                 const Py_ssize_t maxofs = n - hint;     /* &a[n-1] is highest */
    1296                 while (ofs < maxofs) {
    1297                         IFLT(key, a[ofs])
    1298                                 break;
    1299                         /* a[hint + ofs] <= key */
    1300                         lastofs = ofs;
    1301                         ofs = (ofs << 1) + 1;
    1302                         if (ofs <= 0)   /* int overflow */
    1303                                 ofs = maxofs;
    1304                 }
    1305                 if (ofs > maxofs)
    1306                         ofs = maxofs;
    1307                 /* Translate back to offsets relative to &a[0]. */
    1308                 lastofs += hint;
    1309                 ofs += hint;
    1310         }
    1311         a -= hint;
    1312 
    1313         assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
    1314         /* Now a[lastofs] <= key < a[ofs], so key belongs somewhere to the
    1315         * right of lastofs but no farther right than ofs.  Do a binary
    1316         * search, with invariant a[lastofs-1] <= key < a[ofs].
    1317         */
    1318         ++lastofs;
    1319         while (lastofs < ofs) {
    1320                 Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
    1321 
    1322                 IFLT(key, a[m])
    1323                         ofs = m;        /* key < a[m] */
    1324                 else
    1325                         lastofs = m+1;  /* a[m] <= key */
    1326         }
    1327         assert(lastofs == ofs);         /* so a[ofs-1] <= key < a[ofs] */
    1328         return ofs;
     1266    Py_ssize_t ofs;
     1267    Py_ssize_t lastofs;
     1268    Py_ssize_t k;
     1269
     1270    assert(key && a && n > 0 && hint >= 0 && hint < n);
     1271
     1272    a += hint;
     1273    lastofs = 0;
     1274    ofs = 1;
     1275    IFLT(key, *a) {
     1276        /* key < a[hint] -- gallop left, until
     1277        * a[hint - ofs] <= key < a[hint - lastofs]
     1278        */
     1279        const Py_ssize_t maxofs = hint + 1;             /* &a[0] is lowest */
     1280        while (ofs < maxofs) {
     1281            IFLT(key, *(a-ofs)) {
     1282                lastofs = ofs;
     1283                ofs = (ofs << 1) + 1;
     1284                if (ofs <= 0)                   /* int overflow */
     1285                    ofs = maxofs;
     1286            }
     1287            else                /* a[hint - ofs] <= key */
     1288                break;
     1289        }
     1290        if (ofs > maxofs)
     1291            ofs = maxofs;
     1292        /* Translate back to positive offsets relative to &a[0]. */
     1293        k = lastofs;
     1294        lastofs = hint - ofs;
     1295        ofs = hint - k;
     1296    }
     1297    else {
     1298        /* a[hint] <= key -- gallop right, until
     1299        * a[hint + lastofs] <= key < a[hint + ofs]
     1300        */
     1301        const Py_ssize_t maxofs = n - hint;             /* &a[n-1] is highest */
     1302        while (ofs < maxofs) {
     1303            IFLT(key, a[ofs])
     1304                break;
     1305            /* a[hint + ofs] <= key */
     1306            lastofs = ofs;
     1307            ofs = (ofs << 1) + 1;
     1308            if (ofs <= 0)               /* int overflow */
     1309                ofs = maxofs;
     1310        }
     1311        if (ofs > maxofs)
     1312            ofs = maxofs;
     1313        /* Translate back to offsets relative to &a[0]. */
     1314        lastofs += hint;
     1315        ofs += hint;
     1316    }
     1317    a -= hint;
     1318
     1319    assert(-1 <= lastofs && lastofs < ofs && ofs <= n);
     1320    /* Now a[lastofs] <= key < a[ofs], so key belongs somewhere to the
     1321    * right of lastofs but no farther right than ofs.  Do a binary
     1322    * search, with invariant a[lastofs-1] <= key < a[ofs].
     1323    */
     1324    ++lastofs;
     1325    while (lastofs < ofs) {
     1326        Py_ssize_t m = lastofs + ((ofs - lastofs) >> 1);
     1327
     1328        IFLT(key, a[m])
     1329            ofs = m;                    /* key < a[m] */
     1330        else
     1331            lastofs = m+1;              /* a[m] <= key */
     1332    }
     1333    assert(lastofs == ofs);             /* so a[ofs-1] <= key < a[ofs] */
     1334    return ofs;
    13291335
    13301336fail:
    1331         return -1;
     1337    return -1;
    13321338}
    13331339
     
    13521358 */
    13531359struct s_slice {
    1354         PyObject **base;
    1355         Py_ssize_t len;
     1360    PyObject **base;
     1361    Py_ssize_t len;
    13561362};
    13571363
    13581364typedef struct s_MergeState {
    1359         /* The user-supplied comparison function. or NULL if none given. */
    1360         PyObject *compare;
    1361 
    1362         /* This controls when we get *into* galloping mode.  It's initialized
    1363         * to MIN_GALLOP.  merge_lo and merge_hi tend to nudge it higher for
    1364         * random data, and lower for highly structured data.
    1365         */
    1366         Py_ssize_t min_gallop;
    1367 
    1368         /* 'a' is temp storage to help with merges.  It contains room for
    1369         * alloced entries.
    1370         */
    1371         PyObject **a;   /* may point to temparray below */
    1372         Py_ssize_t alloced;
    1373 
    1374         /* A stack of n pending runs yet to be merged.  Run #i starts at
    1375         * address base[i] and extends for len[i] elements.  It's always
    1376         * true (so long as the indices are in bounds) that
    1377         *
    1378         *     pending[i].base + pending[i].len == pending[i+1].base
    1379         *
    1380         * so we could cut the storage for this, but it's a minor amount,
    1381         * and keeping all the info explicit simplifies the code.
    1382         */
    1383         int n;
    1384         struct s_slice pending[MAX_MERGE_PENDING];
    1385 
    1386         /* 'a' points to this when possible, rather than muck with malloc. */
    1387         PyObject *temparray[MERGESTATE_TEMP_SIZE];
     1365    /* The user-supplied comparison function. or NULL if none given. */
     1366    PyObject *compare;
     1367
     1368    /* This controls when we get *into* galloping mode.  It's initialized
     1369    * to MIN_GALLOP.  merge_lo and merge_hi tend to nudge it higher for
     1370    * random data, and lower for highly structured data.
     1371    */
     1372    Py_ssize_t min_gallop;
     1373
     1374    /* 'a' is temp storage to help with merges.  It contains room for
     1375    * alloced entries.
     1376    */
     1377    PyObject **a;       /* may point to temparray below */
     1378    Py_ssize_t alloced;
     1379
     1380    /* A stack of n pending runs yet to be merged.  Run #i starts at
     1381    * address base[i] and extends for len[i] elements.  It's always
     1382    * true (so long as the indices are in bounds) that
     1383    *
     1384    *     pending[i].base + pending[i].len == pending[i+1].base
     1385    *
     1386    * so we could cut the storage for this, but it's a minor amount,
     1387    * and keeping all the info explicit simplifies the code.
     1388    */
     1389    int n;
     1390    struct s_slice pending[MAX_MERGE_PENDING];
     1391
     1392    /* 'a' points to this when possible, rather than muck with malloc. */
     1393    PyObject *temparray[MERGESTATE_TEMP_SIZE];
    13881394} MergeState;
    13891395
     
    13921398merge_init(MergeState *ms, PyObject *compare)
    13931399{
    1394         assert(ms != NULL);
    1395         ms->compare = compare;
    1396         ms->a = ms->temparray;
    1397         ms->alloced = MERGESTATE_TEMP_SIZE;
    1398         ms->n = 0;
    1399         ms->min_gallop = MIN_GALLOP;
     1400    assert(ms != NULL);
     1401    ms->compare = compare;
     1402    ms->a = ms->temparray;
     1403    ms->alloced = MERGESTATE_TEMP_SIZE;
     1404    ms->n = 0;
     1405    ms->min_gallop = MIN_GALLOP;
    14001406}
    14011407
     
    14071413merge_freemem(MergeState *ms)
    14081414{
    1409         assert(ms != NULL);
    1410         if (ms->a != ms->temparray)
    1411                 PyMem_Free(ms->a);
    1412         ms->a = ms->temparray;
    1413         ms->alloced = MERGESTATE_TEMP_SIZE;
     1415    assert(ms != NULL);
     1416    if (ms->a != ms->temparray)
     1417        PyMem_Free(ms->a);
     1418    ms->a = ms->temparray;
     1419    ms->alloced = MERGESTATE_TEMP_SIZE;
    14141420}
    14151421
     
    14201426merge_getmem(MergeState *ms, Py_ssize_t need)
    14211427{
    1422         assert(ms != NULL);
    1423         if (need <= ms->alloced)
    1424                 return 0;
    1425         /* Don't realloc!  That can cost cycles to copy the old data, but
    1426         * we don't care what's in the block.
    1427         */
    1428         merge_freemem(ms);
    1429         if ((size_t)need > PY_SSIZE_T_MAX / sizeof(PyObject*)) {
    1430                 PyErr_NoMemory();
    1431                 return -1;
    1432         }
    1433         ms->a = (PyObject **)PyMem_Malloc(need * sizeof(PyObject*));
    1434         if (ms->a) {
    1435                 ms->alloced = need;
    1436                 return 0;
    1437         }
    1438         PyErr_NoMemory();
    1439         merge_freemem(ms);      /* reset to sane state */
    1440         return -1;
    1441 }
    1442 #define MERGE_GETMEM(MS, NEED) ((NEED) <= (MS)->alloced ? 0 :   \
    1443                                 merge_getmem(MS, NEED))
     1428    assert(ms != NULL);
     1429    if (need <= ms->alloced)
     1430        return 0;
     1431    /* Don't realloc!  That can cost cycles to copy the old data, but
     1432    * we don't care what's in the block.
     1433    */
     1434    merge_freemem(ms);
     1435    if ((size_t)need > PY_SSIZE_T_MAX / sizeof(PyObject*)) {
     1436        PyErr_NoMemory();
     1437        return -1;
     1438    }
     1439    ms->a = (PyObject **)PyMem_Malloc(need * sizeof(PyObject*));
     1440    if (ms->a) {
     1441        ms->alloced = need;
     1442        return 0;
     1443    }
     1444    PyErr_NoMemory();
     1445    merge_freemem(ms);          /* reset to sane state */
     1446    return -1;
     1447}
     1448#define MERGE_GETMEM(MS, NEED) ((NEED) <= (MS)->alloced ? 0 :   \
     1449                                merge_getmem(MS, NEED))
    14441450
    14451451/* Merge the na elements starting at pa with the nb elements starting at pb
     
    14531459                         PyObject **pb, Py_ssize_t nb)
    14541460{
    1455         Py_ssize_t k;
    1456         PyObject *compare;
    1457         PyObject **dest;
    1458         int result = -1;        /* guilty until proved innocent */
    1459         Py_ssize_t min_gallop;
    1460 
    1461         assert(ms && pa && pb && na > 0 && nb > 0 && pa + na == pb);
    1462         if (MERGE_GETMEM(ms, na) < 0)
    1463                 return -1;
    1464         memcpy(ms->a, pa, na * sizeof(PyObject*));
    1465         dest = pa;
    1466         pa = ms->a;
    1467 
    1468         *dest++ = *pb++;
    1469         --nb;
    1470         if (nb == 0)
    1471                 goto Succeed;
    1472         if (na == 1)
    1473                 goto CopyB;
    1474 
    1475         min_gallop = ms->min_gallop;
    1476         compare = ms->compare;
    1477         for (;;) {
    1478                 Py_ssize_t acount = 0;  /* # of times A won in a row */
    1479                 Py_ssize_t bcount = 0;  /* # of times B won in a row */
    1480 
    1481                 /* Do the straightforward thing until (if ever) one run
    1482                 * appears to win consistently.
    1483                 */
    1484                 for (;;) {
    1485                         assert(na > 1 && nb > 0);
    1486                         k = ISLT(*pb, *pa, compare);
    1487                         if (k) {
    1488                                 if (k < 0)
    1489                                         goto Fail;
    1490                                 *dest++ = *pb++;
    1491                                 ++bcount;
    1492                                 acount = 0;
    1493                                 --nb;
    1494                                 if (nb == 0)
    1495                                         goto Succeed;
    1496                                 if (bcount >= min_gallop)
    1497                                         break;
    1498                         }
    1499                         else {
    1500                                 *dest++ = *pa++;
    1501                                 ++acount;
    1502                                 bcount = 0;
    1503                                 --na;
    1504                                 if (na == 1)
    1505                                         goto CopyB;
    1506                                 if (acount >= min_gallop)
    1507                                         break;
    1508                         }
    1509                 }
    1510 
    1511                 /* One run is winning so consistently that galloping may
    1512                 * be a huge win.  So try that, and continue galloping until
    1513                 * (if ever) neither run appears to be winning consistently
    1514                 * anymore.
    1515                 */
    1516                 ++min_gallop;
    1517                 do {
    1518                         assert(na > 1 && nb > 0);
    1519                         min_gallop -= min_gallop > 1;
    1520                         ms->min_gallop = min_gallop;
    1521                         k = gallop_right(*pb, pa, na, 0, compare);
    1522                         acount = k;
    1523                         if (k) {
    1524                                 if (k < 0)
    1525                                         goto Fail;
    1526                                 memcpy(dest, pa, k * sizeof(PyObject *));
    1527                                 dest += k;
    1528                                 pa += k;
    1529                                 na -= k;
    1530                                 if (na == 1)
    1531                                         goto CopyB;
    1532                                 /* na==0 is impossible now if the comparison
    1533                                 * function is consistent, but we can't assume
    1534                                 * that it is.
    1535                                 */
    1536                                 if (na == 0)
    1537                                         goto Succeed;
    1538                         }
    1539                         *dest++ = *pb++;
    1540                         --nb;
    1541                         if (nb == 0)
    1542                                 goto Succeed;
    1543 
    1544                         k = gallop_left(*pa, pb, nb, 0, compare);
    1545                         bcount = k;
    1546                         if (k) {
    1547                                 if (k < 0)
    1548                                         goto Fail;
    1549                                 memmove(dest, pb, k * sizeof(PyObject *));
    1550                                 dest += k;
    1551                                 pb += k;
    1552                                 nb -= k;
    1553                                 if (nb == 0)
    1554                                         goto Succeed;
    1555                         }
    1556                         *dest++ = *pa++;
    1557                         --na;
    1558                         if (na == 1)
    1559                                 goto CopyB;
    1560                 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
    1561                 ++min_gallop;   /* penalize it for leaving galloping mode */
    1562                 ms->min_gallop = min_gallop;
    1563         }
     1461    Py_ssize_t k;
     1462    PyObject *compare;
     1463    PyObject **dest;
     1464    int result = -1;            /* guilty until proved innocent */
     1465    Py_ssize_t min_gallop;
     1466
     1467    assert(ms && pa && pb && na > 0 && nb > 0 && pa + na == pb);
     1468    if (MERGE_GETMEM(ms, na) < 0)
     1469        return -1;
     1470    memcpy(ms->a, pa, na * sizeof(PyObject*));
     1471    dest = pa;
     1472    pa = ms->a;
     1473
     1474    *dest++ = *pb++;
     1475    --nb;
     1476    if (nb == 0)
     1477        goto Succeed;
     1478    if (na == 1)
     1479        goto CopyB;
     1480
     1481    min_gallop = ms->min_gallop;
     1482    compare = ms->compare;
     1483    for (;;) {
     1484        Py_ssize_t acount = 0;          /* # of times A won in a row */
     1485        Py_ssize_t bcount = 0;          /* # of times B won in a row */
     1486
     1487        /* Do the straightforward thing until (if ever) one run
     1488        * appears to win consistently.
     1489        */
     1490        for (;;) {
     1491            assert(na > 1 && nb > 0);
     1492            k = ISLT(*pb, *pa, compare);
     1493            if (k) {
     1494                if (k < 0)
     1495                    goto Fail;
     1496                *dest++ = *pb++;
     1497                ++bcount;
     1498                acount = 0;
     1499                --nb;
     1500                if (nb == 0)
     1501                    goto Succeed;
     1502                if (bcount >= min_gallop)
     1503                    break;
     1504            }
     1505            else {
     1506                *dest++ = *pa++;
     1507                ++acount;
     1508                bcount = 0;
     1509                --na;
     1510                if (na == 1)
     1511                    goto CopyB;
     1512                if (acount >= min_gallop)
     1513                    break;
     1514            }
     1515        }
     1516
     1517        /* One run is winning so consistently that galloping may
     1518        * be a huge win.  So try that, and continue galloping until
     1519        * (if ever) neither run appears to be winning consistently
     1520        * anymore.
     1521        */
     1522        ++min_gallop;
     1523        do {
     1524            assert(na > 1 && nb > 0);
     1525            min_gallop -= min_gallop > 1;
     1526            ms->min_gallop = min_gallop;
     1527            k = gallop_right(*pb, pa, na, 0, compare);
     1528            acount = k;
     1529            if (k) {
     1530                if (k < 0)
     1531                    goto Fail;
     1532                memcpy(dest, pa, k * sizeof(PyObject *));
     1533                dest += k;
     1534                pa += k;
     1535                na -= k;
     1536                if (na == 1)
     1537                    goto CopyB;
     1538                /* na==0 is impossible now if the comparison
     1539                * function is consistent, but we can't assume
     1540                * that it is.
     1541                */
     1542                if (na == 0)
     1543                    goto Succeed;
     1544            }
     1545            *dest++ = *pb++;
     1546            --nb;
     1547            if (nb == 0)
     1548                goto Succeed;
     1549
     1550            k = gallop_left(*pa, pb, nb, 0, compare);
     1551            bcount = k;
     1552            if (k) {
     1553                if (k < 0)
     1554                    goto Fail;
     1555                memmove(dest, pb, k * sizeof(PyObject *));
     1556                dest += k;
     1557                pb += k;
     1558                nb -= k;
     1559                if (nb == 0)
     1560                    goto Succeed;
     1561            }
     1562            *dest++ = *pa++;
     1563            --na;
     1564            if (na == 1)
     1565                goto CopyB;
     1566        } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
     1567        ++min_gallop;           /* penalize it for leaving galloping mode */
     1568        ms->min_gallop = min_gallop;
     1569    }
    15641570Succeed:
    1565         result = 0;
     1571    result = 0;
    15661572Fail:
    1567         if (na)
    1568                 memcpy(dest, pa, na * sizeof(PyObject*));
    1569         return result;
     1573    if (na)
     1574        memcpy(dest, pa, na * sizeof(PyObject*));
     1575    return result;
    15701576CopyB:
    1571         assert(na == 1 && nb > 0);
    1572         /* The last element of pa belongs at the end of the merge. */
    1573         memmove(dest, pb, nb * sizeof(PyObject *));
    1574         dest[nb] = *pa;
    1575         return 0;
     1577    assert(na == 1 && nb > 0);
     1578    /* The last element of pa belongs at the end of the merge. */
     1579    memmove(dest, pb, nb * sizeof(PyObject *));
     1580    dest[nb] = *pa;
     1581    return 0;
    15761582}
    15771583
     
    15851591merge_hi(MergeState *ms, PyObject **pa, Py_ssize_t na, PyObject **pb, Py_ssize_t nb)
    15861592{
    1587         Py_ssize_t k;
    1588         PyObject *compare;
    1589         PyObject **dest;
    1590         int result = -1;        /* guilty until proved innocent */
    1591         PyObject **basea;
    1592         PyObject **baseb;
    1593         Py_ssize_t min_gallop;
    1594 
    1595         assert(ms && pa && pb && na > 0 && nb > 0 && pa + na == pb);
    1596         if (MERGE_GETMEM(ms, nb) < 0)
    1597                 return -1;
    1598         dest = pb + nb - 1;
    1599         memcpy(ms->a, pb, nb * sizeof(PyObject*));
    1600         basea = pa;
    1601         baseb = ms->a;
    1602         pb = ms->a + nb - 1;
    1603         pa += na - 1;
    1604 
    1605         *dest-- = *pa--;
    1606         --na;
    1607         if (na == 0)
    1608                 goto Succeed;
    1609         if (nb == 1)
    1610                 goto CopyA;
    1611 
    1612         min_gallop = ms->min_gallop;
    1613         compare = ms->compare;
    1614         for (;;) {
    1615                 Py_ssize_t acount = 0;  /* # of times A won in a row */
    1616                 Py_ssize_t bcount = 0;  /* # of times B won in a row */
    1617 
    1618                 /* Do the straightforward thing until (if ever) one run
    1619                 * appears to win consistently.
    1620                 */
    1621                 for (;;) {
    1622                         assert(na > 0 && nb > 1);
    1623                         k = ISLT(*pb, *pa, compare);
    1624                         if (k) {
    1625                                 if (k < 0)
    1626                                         goto Fail;
    1627                                 *dest-- = *pa--;
    1628                                 ++acount;
    1629                                 bcount = 0;
    1630                                 --na;
    1631                                 if (na == 0)
    1632                                         goto Succeed;
    1633                                 if (acount >= min_gallop)
    1634                                         break;
    1635                         }
    1636                         else {
    1637                                 *dest-- = *pb--;
    1638                                 ++bcount;
    1639                                 acount = 0;
    1640                                 --nb;
    1641                                 if (nb == 1)
    1642                                         goto CopyA;
    1643                                 if (bcount >= min_gallop)
    1644                                         break;
    1645                         }
    1646                 }
    1647 
    1648                 /* One run is winning so consistently that galloping may
    1649                 * be a huge win.  So try that, and continue galloping until
    1650                 * (if ever) neither run appears to be winning consistently
    1651                 * anymore.
    1652                 */
    1653                 ++min_gallop;
    1654                 do {
    1655                         assert(na > 0 && nb > 1);
    1656                         min_gallop -= min_gallop > 1;
    1657                         ms->min_gallop = min_gallop;
    1658                         k = gallop_right(*pb, basea, na, na-1, compare);
    1659                         if (k < 0)
    1660                                 goto Fail;
    1661                         k = na - k;
    1662                         acount = k;
    1663                         if (k) {
    1664                                 dest -= k;
    1665                                 pa -= k;
    1666                                 memmove(dest+1, pa+1, k * sizeof(PyObject *));
    1667                                 na -= k;
    1668                                 if (na == 0)
    1669                                         goto Succeed;
    1670                         }
    1671                         *dest-- = *pb--;
    1672                         --nb;
    1673                         if (nb == 1)
    1674                                 goto CopyA;
    1675 
    1676                         k = gallop_left(*pa, baseb, nb, nb-1, compare);
    1677                         if (k < 0)
    1678                                 goto Fail;
    1679                         k = nb - k;
    1680                         bcount = k;
    1681                         if (k) {
    1682                                 dest -= k;
    1683                                 pb -= k;
    1684                                 memcpy(dest+1, pb+1, k * sizeof(PyObject *));
    1685                                 nb -= k;
    1686                                 if (nb == 1)
    1687                                         goto CopyA;
    1688                                 /* nb==0 is impossible now if the comparison
    1689                                 * function is consistent, but we can't assume
    1690                                 * that it is.
    1691                                 */
    1692                                 if (nb == 0)
    1693                                         goto Succeed;
    1694                         }
    1695                         *dest-- = *pa--;
    1696                         --na;
    1697                         if (na == 0)
    1698                                 goto Succeed;
    1699                 } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
    1700                 ++min_gallop;   /* penalize it for leaving galloping mode */
    1701                 ms->min_gallop = min_gallop;
    1702         }
     1593    Py_ssize_t k;
     1594    PyObject *compare;
     1595    PyObject **dest;
     1596    int result = -1;            /* guilty until proved innocent */
     1597    PyObject **basea;
     1598    PyObject **baseb;
     1599    Py_ssize_t min_gallop;
     1600
     1601    assert(ms && pa && pb && na > 0 && nb > 0 && pa + na == pb);
     1602    if (MERGE_GETMEM(ms, nb) < 0)
     1603        return -1;
     1604    dest = pb + nb - 1;
     1605    memcpy(ms->a, pb, nb * sizeof(PyObject*));
     1606    basea = pa;
     1607    baseb = ms->a;
     1608    pb = ms->a + nb - 1;
     1609    pa += na - 1;
     1610
     1611    *dest-- = *pa--;
     1612    --na;
     1613    if (na == 0)
     1614        goto Succeed;
     1615    if (nb == 1)
     1616        goto CopyA;
     1617
     1618    min_gallop = ms->min_gallop;
     1619    compare = ms->compare;
     1620    for (;;) {
     1621        Py_ssize_t acount = 0;          /* # of times A won in a row */
     1622        Py_ssize_t bcount = 0;          /* # of times B won in a row */
     1623
     1624        /* Do the straightforward thing until (if ever) one run
     1625        * appears to win consistently.
     1626        */
     1627        for (;;) {
     1628            assert(na > 0 && nb > 1);
     1629            k = ISLT(*pb, *pa, compare);
     1630            if (k) {
     1631                if (k < 0)
     1632                    goto Fail;
     1633                *dest-- = *pa--;
     1634                ++acount;
     1635                bcount = 0;
     1636                --na;
     1637                if (na == 0)
     1638                    goto Succeed;
     1639                if (acount >= min_gallop)
     1640                    break;
     1641            }
     1642            else {
     1643                *dest-- = *pb--;
     1644                ++bcount;
     1645                acount = 0;
     1646                --nb;
     1647                if (nb == 1)
     1648                    goto CopyA;
     1649                if (bcount >= min_gallop)
     1650                    break;
     1651            }
     1652        }
     1653
     1654        /* One run is winning so consistently that galloping may
     1655        * be a huge win.  So try that, and continue galloping until
     1656        * (if ever) neither run appears to be winning consistently
     1657        * anymore.
     1658        */
     1659        ++min_gallop;
     1660        do {
     1661            assert(na > 0 && nb > 1);
     1662            min_gallop -= min_gallop > 1;
     1663            ms->min_gallop = min_gallop;
     1664            k = gallop_right(*pb, basea, na, na-1, compare);
     1665            if (k < 0)
     1666                goto Fail;
     1667            k = na - k;
     1668            acount = k;
     1669            if (k) {
     1670                dest -= k;
     1671                pa -= k;
     1672                memmove(dest+1, pa+1, k * sizeof(PyObject *));
     1673                na -= k;
     1674                if (na == 0)
     1675                    goto Succeed;
     1676            }
     1677            *dest-- = *pb--;
     1678            --nb;
     1679            if (nb == 1)
     1680                goto CopyA;
     1681
     1682            k = gallop_left(*pa, baseb, nb, nb-1, compare);
     1683            if (k < 0)
     1684                goto Fail;
     1685            k = nb - k;
     1686            bcount = k;
     1687            if (k) {
     1688                dest -= k;
     1689                pb -= k;
     1690                memcpy(dest+1, pb+1, k * sizeof(PyObject *));
     1691                nb -= k;
     1692                if (nb == 1)
     1693                    goto CopyA;
     1694                /* nb==0 is impossible now if the comparison
     1695                * function is consistent, but we can't assume
     1696                * that it is.
     1697                */
     1698                if (nb == 0)
     1699                    goto Succeed;
     1700            }
     1701            *dest-- = *pa--;
     1702            --na;
     1703            if (na == 0)
     1704                goto Succeed;
     1705        } while (acount >= MIN_GALLOP || bcount >= MIN_GALLOP);
     1706        ++min_gallop;           /* penalize it for leaving galloping mode */
     1707        ms->min_gallop = min_gallop;
     1708    }
    17031709Succeed:
    1704         result = 0;
     1710    result = 0;
    17051711Fail:
    1706         if (nb)
    1707                 memcpy(dest-(nb-1), baseb, nb * sizeof(PyObject*));
    1708         return result;
     1712    if (nb)
     1713        memcpy(dest-(nb-1), baseb, nb * sizeof(PyObject*));
     1714    return result;
    17091715CopyA:
    1710         assert(nb == 1 && na > 0);
    1711         /* The first element of pb belongs at the front of the merge. */
    1712         dest -= na;
    1713         pa -= na;
    1714         memmove(dest+1, pa+1, na * sizeof(PyObject *));
    1715         *dest = *pb;
    1716         return 0;
     1716    assert(nb == 1 && na > 0);
     1717    /* The first element of pb belongs at the front of the merge. */
     1718    dest -= na;
     1719    pa -= na;
     1720    memmove(dest+1, pa+1, na * sizeof(PyObject *));
     1721    *dest = *pb;
     1722    return 0;
    17171723}
    17181724
     
    17231729merge_at(MergeState *ms, Py_ssize_t i)
    17241730{
    1725         PyObject **pa, **pb;
    1726         Py_ssize_t na, nb;
    1727         Py_ssize_t k;
    1728         PyObject *compare;
    1729 
    1730         assert(ms != NULL);
    1731         assert(ms->n >= 2);
    1732         assert(i >= 0);
    1733         assert(i == ms->n - 2 || i == ms->n - 3);
    1734 
    1735         pa = ms->pending[i].base;
    1736         na = ms->pending[i].len;
    1737         pb = ms->pending[i+1].base;
    1738         nb = ms->pending[i+1].len;
    1739         assert(na > 0 && nb > 0);
    1740         assert(pa + na == pb);
    1741 
    1742         /* Record the length of the combined runs; if i is the 3rd-last
    1743         * run now, also slide over the last run (which isn't involved
    1744         * in this merge).  The current run i+1 goes away in any case.
    1745         */
    1746         ms->pending[i].len = na + nb;
    1747         if (i == ms->n - 3)
    1748                 ms->pending[i+1] = ms->pending[i+2];
    1749         --ms->n;
    1750 
    1751         /* Where does b start in a?  Elements in a before that can be
    1752         * ignored (already in place).
    1753         */
    1754         compare = ms->compare;
    1755         k = gallop_right(*pb, pa, na, 0, compare);
    1756         if (k < 0)
    1757                 return -1;
    1758         pa += k;
    1759         na -= k;
    1760         if (na == 0)
    1761                 return 0;
    1762 
    1763         /* Where does a end in b?  Elements in b after that can be
    1764         * ignored (already in place).
    1765         */
    1766         nb = gallop_left(pa[na-1], pb, nb, nb-1, compare);
    1767         if (nb <= 0)
    1768                 return nb;
    1769 
    1770         /* Merge what remains of the runs, using a temp array with
    1771         * min(na, nb) elements.
    1772         */
    1773         if (na <= nb)
    1774                 return merge_lo(ms, pa, na, pb, nb);
    1775         else
    1776                 return merge_hi(ms, pa, na, pb, nb);
     1731    PyObject **pa, **pb;
     1732    Py_ssize_t na, nb;
     1733    Py_ssize_t k;
     1734    PyObject *compare;
     1735
     1736    assert(ms != NULL);
     1737    assert(ms->n >= 2);
     1738    assert(i >= 0);
     1739    assert(i == ms->n - 2 || i == ms->n - 3);
     1740
     1741    pa = ms->pending[i].base;
     1742    na = ms->pending[i].len;
     1743    pb = ms->pending[i+1].base;
     1744    nb = ms->pending[i+1].len;
     1745    assert(na > 0 && nb > 0);
     1746    assert(pa + na == pb);
     1747
     1748    /* Record the length of the combined runs; if i is the 3rd-last
     1749    * run now, also slide over the last run (which isn't involved
     1750    * in this merge).  The current run i+1 goes away in any case.
     1751    */
     1752    ms->pending[i].len = na + nb;
     1753    if (i == ms->n - 3)
     1754        ms->pending[i+1] = ms->pending[i+2];
     1755    --ms->n;
     1756
     1757    /* Where does b start in a?  Elements in a before that can be
     1758    * ignored (already in place).
     1759    */
     1760    compare = ms->compare;
     1761    k = gallop_right(*pb, pa, na, 0, compare);
     1762    if (k < 0)
     1763        return -1;
     1764    pa += k;
     1765    na -= k;
     1766    if (na == 0)
     1767        return 0;
     1768
     1769    /* Where does a end in b?  Elements in b after that can be
     1770    * ignored (already in place).
     1771    */
     1772    nb = gallop_left(pa[na-1], pb, nb, nb-1, compare);
     1773    if (nb <= 0)
     1774        return nb;
     1775
     1776    /* Merge what remains of the runs, using a temp array with
     1777    * min(na, nb) elements.
     1778    */
     1779    if (na <= nb)
     1780        return merge_lo(ms, pa, na, pb, nb);
     1781    else
     1782        return merge_hi(ms, pa, na, pb, nb);
    17771783}
    17781784
     
    17901796merge_collapse(MergeState *ms)
    17911797{
    1792         struct s_slice *p = ms->pending;
    1793 
    1794         assert(ms);
    1795         while (ms->n > 1) {
    1796                 Py_ssize_t n = ms->n - 2;
    1797                 if (n > 0 && p[n-1].len <= p[n].len + p[n+1].len) {
    1798                         if (p[n-1].len < p[n+1].len)
    1799                                 --n;
    1800                         if (merge_at(ms, n) < 0)
    1801                                 return -1;
    1802                 }
    1803                 else if (p[n].len <= p[n+1].len) {
    1804                         if (merge_at(ms, n) < 0)
    1805                                 return -1;
    1806                 }
    1807                 else
    1808                         break;
    1809         }
    1810         return 0;
     1798    struct s_slice *p = ms->pending;
     1799
     1800    assert(ms);
     1801    while (ms->n > 1) {
     1802        Py_ssize_t n = ms->n - 2;
     1803        if (n > 0 && p[n-1].len <= p[n].len + p[n+1].len) {
     1804            if (p[n-1].len < p[n+1].len)
     1805                --n;
     1806            if (merge_at(ms, n) < 0)
     1807                return -1;
     1808        }
     1809        else if (p[n].len <= p[n+1].len) {
     1810                if (merge_at(ms, n) < 0)
     1811                        return -1;
     1812        }
     1813        else
     1814            break;
     1815    }
     1816    return 0;
    18111817}
    18121818
     
    18191825merge_force_collapse(MergeState *ms)
    18201826{
    1821         struct s_slice *p = ms->pending;
    1822 
    1823         assert(ms);
    1824         while (ms->n > 1) {
    1825                 Py_ssize_t n = ms->n - 2;
    1826                 if (n > 0 && p[n-1].len < p[n+1].len)
    1827                         --n;
    1828                 if (merge_at(ms, n) < 0)
    1829                         return -1;
    1830         }
    1831         return 0;
     1827    struct s_slice *p = ms->pending;
     1828
     1829    assert(ms);
     1830    while (ms->n > 1) {
     1831        Py_ssize_t n = ms->n - 2;
     1832        if (n > 0 && p[n-1].len < p[n+1].len)
     1833            --n;
     1834        if (merge_at(ms, n) < 0)
     1835            return -1;
     1836    }
     1837    return 0;
    18321838}
    18331839
     
    18451851merge_compute_minrun(Py_ssize_t n)
    18461852{
    1847         Py_ssize_t r = 0;       /* becomes 1 if any 1 bits are shifted off */
    1848 
    1849         assert(n >= 0);
    1850         while (n >= 64) {
    1851                 r |= n & 1;
    1852                 n >>= 1;
    1853         }
    1854         return n + r;
     1853    Py_ssize_t r = 0;           /* becomes 1 if any 1 bits are shifted off */
     1854
     1855    assert(n >= 0);
     1856    while (n >= 64) {
     1857        r |= n & 1;
     1858        n >>= 1;
     1859    }
     1860    return n + r;
    18551861}
    18561862
     
    18631869
    18641870typedef struct {
    1865         PyObject_HEAD
    1866         PyObject *key;
    1867         PyObject *value;
     1871    PyObject_HEAD
     1872    PyObject *key;
     1873    PyObject *value;
    18681874} sortwrapperobject;
    18691875
     
    18751881
    18761882static PyTypeObject sortwrapper_type = {
    1877         PyVarObject_HEAD_INIT(&PyType_Type, 0)
    1878         "sortwrapper",                          /* tp_name */
    1879         sizeof(sortwrapperobject),              /* tp_basicsize */
    1880         0,                                      /* tp_itemsize */
    1881         /* methods */
    1882         (destructor)sortwrapper_dealloc,        /* tp_dealloc */
    1883         0,                                      /* tp_print */
    1884         0,                                      /* tp_getattr */
    1885         0,                                      /* tp_setattr */
    1886         0,                                      /* tp_compare */
    1887         0,                                      /* tp_repr */
    1888         0,                                      /* tp_as_number */
    1889         0,                                      /* tp_as_sequence */
    1890         0,                                      /* tp_as_mapping */
    1891         0,                                      /* tp_hash */
    1892         0,                                      /* tp_call */
    1893         0,                                      /* tp_str */
    1894         PyObject_GenericGetAttr,                /* tp_getattro */
    1895         0,                                      /* tp_setattro */
    1896         0,                                      /* tp_as_buffer */
    1897         Py_TPFLAGS_DEFAULT |
    1898         Py_TPFLAGS_HAVE_RICHCOMPARE,            /* tp_flags */
    1899         sortwrapper_doc,                        /* tp_doc */
    1900         0,                                      /* tp_traverse */
    1901         0,                                      /* tp_clear */
    1902         (richcmpfunc)sortwrapper_richcompare,   /* tp_richcompare */
     1883    PyVarObject_HEAD_INIT(&PyType_Type, 0)
     1884    "sortwrapper",                              /* tp_name */
     1885    sizeof(sortwrapperobject),                  /* tp_basicsize */
     1886    0,                                          /* tp_itemsize */
     1887    /* methods */
     1888    (destructor)sortwrapper_dealloc,            /* tp_dealloc */
     1889    0,                                          /* tp_print */
     1890    0,                                          /* tp_getattr */
     1891    0,                                          /* tp_setattr */
     1892    0,                                          /* tp_compare */
     1893    0,                                          /* tp_repr */
     1894    0,                                          /* tp_as_number */
     1895    0,                                          /* tp_as_sequence */
     1896    0,                                          /* tp_as_mapping */
     1897    0,                                          /* tp_hash */
     1898    0,                                          /* tp_call */
     1899    0,                                          /* tp_str */
     1900    PyObject_GenericGetAttr,                    /* tp_getattro */
     1901    0,                                          /* tp_setattro */
     1902    0,                                          /* tp_as_buffer */
     1903    Py_TPFLAGS_DEFAULT |
     1904    Py_TPFLAGS_HAVE_RICHCOMPARE,                /* tp_flags */
     1905    sortwrapper_doc,                            /* tp_doc */
     1906    0,                                          /* tp_traverse */
     1907    0,                                          /* tp_clear */
     1908    (richcmpfunc)sortwrapper_richcompare,       /* tp_richcompare */
    19031909};
    19041910
     
    19071913sortwrapper_richcompare(sortwrapperobject *a, sortwrapperobject *b, int op)
    19081914{
    1909         if (!PyObject_TypeCheck(b, &sortwrapper_type)) {
    1910                 PyErr_SetString(PyExc_TypeError,
    1911                         "expected a sortwrapperobject");
    1912                 return NULL;
    1913         }
    1914         return PyObject_RichCompare(a->key, b->key, op);
     1915    if (!PyObject_TypeCheck(b, &sortwrapper_type)) {
     1916        PyErr_SetString(PyExc_TypeError,
     1917            "expected a sortwrapperobject");
     1918        return NULL;
     1919    }
     1920    return PyObject_RichCompare(a->key, b->key, op);
    19151921}
    19161922
     
    19181924sortwrapper_dealloc(sortwrapperobject *so)
    19191925{
    1920         Py_XDECREF(so->key);
    1921         Py_XDECREF(so->value);
    1922         PyObject_Del(so);
     1926    Py_XDECREF(so->key);
     1927    Py_XDECREF(so->value);
     1928    PyObject_Del(so);
    19231929}
    19241930
     
    19291935build_sortwrapper(PyObject *key, PyObject *value)
    19301936{
    1931         sortwrapperobject *so;
    1932 
    1933         so = PyObject_New(sortwrapperobject, &sortwrapper_type);
    1934         if (so == NULL)
    1935                 return NULL;
    1936         so->key = key;
    1937         so->value = value;
    1938         return (PyObject *)so;
     1937    sortwrapperobject *so;
     1938
     1939    so = PyObject_New(sortwrapperobject, &sortwrapper_type);
     1940    if (so == NULL)
     1941        return NULL;
     1942    so->key = key;
     1943    so->value = value;
     1944    return (PyObject *)so;
    19391945}
    19401946
     
    19431949sortwrapper_getvalue(PyObject *so)
    19441950{
    1945         PyObject *value;
    1946 
    1947         if (!PyObject_TypeCheck(so, &sortwrapper_type)) {
    1948                 PyErr_SetString(PyExc_TypeError,
    1949                         "expected a sortwrapperobject");
    1950                 return NULL;
    1951         }
    1952         value = ((sortwrapperobject *)so)->value;
    1953         Py_INCREF(value);
    1954         return value;
     1951    PyObject *value;
     1952
     1953    if (!PyObject_TypeCheck(so, &sortwrapper_type)) {
     1954        PyErr_SetString(PyExc_TypeError,
     1955            "expected a sortwrapperobject");
     1956        return NULL;
     1957    }
     1958    value = ((sortwrapperobject *)so)->value;
     1959    Py_INCREF(value);
     1960    return value;
    19551961}
    19561962
     
    19601966
    19611967typedef struct {
    1962         PyObject_HEAD
    1963         PyObject *func;
     1968    PyObject_HEAD
     1969    PyObject *func;
    19641970} cmpwrapperobject;
    19651971
     
    19671973cmpwrapper_dealloc(cmpwrapperobject *co)
    19681974{
    1969         Py_XDECREF(co->func);
    1970         PyObject_Del(co);
     1975    Py_XDECREF(co->func);
     1976    PyObject_Del(co);
    19711977}
    19721978
     
    19741980cmpwrapper_call(cmpwrapperobject *co, PyObject *args, PyObject *kwds)
    19751981{
    1976         PyObject *x, *y, *xx, *yy;
    1977 
    1978         if (!PyArg_UnpackTuple(args, "", 2, 2, &x, &y))
    1979                 return NULL;
    1980         if (!PyObject_TypeCheck(x, &sortwrapper_type) ||
    1981             !PyObject_TypeCheck(y, &sortwrapper_type)) {
    1982                 PyErr_SetString(PyExc_TypeError,
    1983                         "expected a sortwrapperobject");
    1984                 return NULL;
    1985         }
    1986         xx = ((sortwrapperobject *)x)->key;
    1987         yy = ((sortwrapperobject *)y)->key;
    1988         return PyObject_CallFunctionObjArgs(co->func, xx, yy, NULL);
     1982    PyObject *x, *y, *xx, *yy;
     1983
     1984    if (!PyArg_UnpackTuple(args, "", 2, 2, &x, &y))
     1985        return NULL;
     1986    if (!PyObject_TypeCheck(x, &sortwrapper_type) ||
     1987        !PyObject_TypeCheck(y, &sortwrapper_type)) {
     1988        PyErr_SetString(PyExc_TypeError,
     1989            "expected a sortwrapperobject");
     1990        return NULL;
     1991    }
     1992    xx = ((sortwrapperobject *)x)->key;
     1993    yy = ((sortwrapperobject *)y)->key;
     1994    return PyObject_CallFunctionObjArgs(co->func, xx, yy, NULL);
    19891995}
    19901996
     
    19921998
    19931999static PyTypeObject cmpwrapper_type = {
    1994         PyVarObject_HEAD_INIT(&PyType_Type, 0)
    1995         "cmpwrapper",                           /* tp_name */
    1996         sizeof(cmpwrapperobject),               /* tp_basicsize */
    1997         0,                                      /* tp_itemsize */
    1998         /* methods */
    1999         (destructor)cmpwrapper_dealloc,         /* tp_dealloc */
    2000         0,                                      /* tp_print */
    2001         0,                                      /* tp_getattr */
    2002         0,                                      /* tp_setattr */
    2003         0,                                      /* tp_compare */
    2004         0,                                      /* tp_repr */
    2005         0,                                      /* tp_as_number */
    2006         0,                                      /* tp_as_sequence */
    2007         0,                                      /* tp_as_mapping */
    2008         0,                                      /* tp_hash */
    2009         (ternaryfunc)cmpwrapper_call,           /* tp_call */
    2010         0,                                      /* tp_str */
    2011         PyObject_GenericGetAttr,                /* tp_getattro */
    2012         0,                                      /* tp_setattro */
    2013         0,                                      /* tp_as_buffer */
    2014         Py_TPFLAGS_DEFAULT,                     /* tp_flags */
    2015         cmpwrapper_doc,                         /* tp_doc */
     2000    PyVarObject_HEAD_INIT(&PyType_Type, 0)
     2001    "cmpwrapper",                               /* tp_name */
     2002    sizeof(cmpwrapperobject),                   /* tp_basicsize */
     2003    0,                                          /* tp_itemsize */
     2004    /* methods */
     2005    (destructor)cmpwrapper_dealloc,             /* tp_dealloc */
     2006    0,                                          /* tp_print */
     2007    0,                                          /* tp_getattr */
     2008    0,                                          /* tp_setattr */
     2009    0,                                          /* tp_compare */
     2010    0,                                          /* tp_repr */
     2011    0,                                          /* tp_as_number */
     2012    0,                                          /* tp_as_sequence */
     2013    0,                                          /* tp_as_mapping */
     2014    0,                                          /* tp_hash */
     2015    (ternaryfunc)cmpwrapper_call,               /* tp_call */
     2016    0,                                          /* tp_str */
     2017    PyObject_GenericGetAttr,                    /* tp_getattro */
     2018    0,                                          /* tp_setattro */
     2019    0,                                          /* tp_as_buffer */
     2020    Py_TPFLAGS_DEFAULT,                         /* tp_flags */
     2021    cmpwrapper_doc,                             /* tp_doc */
    20162022};
    20172023
     
    20192025build_cmpwrapper(PyObject *cmpfunc)
    20202026{
    2021         cmpwrapperobject *co;
    2022 
    2023         co = PyObject_New(cmpwrapperobject, &cmpwrapper_type);
    2024         if (co == NULL)
    2025                 return NULL;
    2026         Py_INCREF(cmpfunc);
    2027         co->func = cmpfunc;
    2028         return (PyObject *)co;
     2027    cmpwrapperobject *co;
     2028
     2029    co = PyObject_New(cmpwrapperobject, &cmpwrapper_type);
     2030    if (co == NULL)
     2031        return NULL;
     2032    Py_INCREF(cmpfunc);
     2033    co->func = cmpfunc;
     2034    return (PyObject *)co;
    20292035}
    20302036
     
    20372043listsort(PyListObject *self, PyObject *args, PyObject *kwds)
    20382044{
    2039         MergeState ms;
    2040         PyObject **lo, **hi;
    2041         Py_ssize_t nremaining;
    2042         Py_ssize_t minrun;
    2043         Py_ssize_t saved_ob_size, saved_allocated;
    2044         PyObject **saved_ob_item;
    2045         PyObject **final_ob_item;
    2046         PyObject *compare = NULL;
    2047         PyObject *result = NULL;        /* guilty until proved innocent */
    2048         int reverse = 0;
    2049         PyObject *keyfunc = NULL;
    2050         Py_ssize_t i;
    2051         PyObject *key, *value, *kvpair;
    2052         static char *kwlist[] = {"cmp", "key", "reverse", 0};
    2053 
    2054         assert(self != NULL);
    2055         assert (PyList_Check(self));
    2056         if (args != NULL) {
    2057                 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOi:sort",
    2058                         kwlist, &compare, &keyfunc, &reverse))
    2059                         return NULL;
    2060         }
    2061         if (compare == Py_None)
    2062                 compare = NULL;
    2063         if (compare != NULL &&
    2064             PyErr_WarnPy3k("the cmp argument is not supported in 3.x", 1) < 0)
    2065                 return NULL;
    2066         if (keyfunc == Py_None)
    2067                 keyfunc = NULL;
    2068         if (compare != NULL && keyfunc != NULL) {
    2069                 compare = build_cmpwrapper(compare);
    2070                 if (compare == NULL)
    2071                         return NULL;
    2072         } else
    2073                 Py_XINCREF(compare);
    2074 
    2075         /* The list is temporarily made empty, so that mutations performed
    2076         * by comparison functions can't affect the slice of memory we're
    2077         * sorting (allowing mutations during sorting is a core-dump
    2078         * factory, since ob_item may change).
    2079         */
    2080         saved_ob_size = Py_SIZE(self);
    2081         saved_ob_item = self->ob_item;
    2082         saved_allocated = self->allocated;
    2083         Py_SIZE(self) = 0;
    2084         self->ob_item = NULL;
    2085         self->allocated = -1; /* any operation will reset it to >= 0 */
    2086 
    2087         if (keyfunc != NULL) {
    2088                 for (i=0 ; i < saved_ob_size ; i++) {
    2089                         value = saved_ob_item[i];
    2090                         key = PyObject_CallFunctionObjArgs(keyfunc, value,
    2091                                                            NULL);
    2092                         if (key == NULL) {
    2093                                 for (i=i-1 ; i>=0 ; i--) {
    2094                                         kvpair = saved_ob_item[i];
    2095                                         value = sortwrapper_getvalue(kvpair);
    2096                                         saved_ob_item[i] = value;
    2097                                         Py_DECREF(kvpair);
    2098                                 }
    2099                                 goto dsu_fail;
    2100                         }
    2101                         kvpair = build_sortwrapper(key, value);
    2102                         if (kvpair == NULL)
    2103                                 goto dsu_fail;
    2104                         saved_ob_item[i] = kvpair;
    2105                 }
    2106         }
    2107 
    2108         /* Reverse sort stability achieved by initially reversing the list,
    2109         applying a stable forward sort, then reversing the final result. */
    2110         if (reverse && saved_ob_size > 1)
    2111                 reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
    2112 
    2113         merge_init(&ms, compare);
    2114 
    2115         nremaining = saved_ob_size;
    2116         if (nremaining < 2)
    2117                 goto succeed;
    2118 
    2119         /* March over the array once, left to right, finding natural runs,
    2120         * and extending short natural runs to minrun elements.
    2121         */
    2122         lo = saved_ob_item;
    2123         hi = lo + nremaining;
    2124         minrun = merge_compute_minrun(nremaining);
    2125         do {
    2126                 int descending;
    2127                 Py_ssize_t n;
    2128 
    2129                 /* Identify next run. */
    2130                 n = count_run(lo, hi, compare, &descending);
    2131                 if (n < 0)
    2132                         goto fail;
    2133                 if (descending)
    2134                         reverse_slice(lo, lo + n);
    2135                 /* If short, extend to min(minrun, nremaining). */
    2136                 if (n < minrun) {
    2137                         const Py_ssize_t force = nremaining <= minrun ?
    2138                                           nremaining : minrun;
    2139                         if (binarysort(lo, lo + force, lo + n, compare) < 0)
    2140                                 goto fail;
    2141                         n = force;
    2142                 }
    2143                 /* Push run onto pending-runs stack, and maybe merge. */
    2144                 assert(ms.n < MAX_MERGE_PENDING);
    2145                 ms.pending[ms.n].base = lo;
    2146                 ms.pending[ms.n].len = n;
    2147                 ++ms.n;
    2148                 if (merge_collapse(&ms) < 0)
    2149                         goto fail;
    2150                 /* Advance to find next run. */
    2151                 lo += n;
    2152                 nremaining -= n;
    2153         } while (nremaining);
    2154         assert(lo == hi);
    2155 
    2156         if (merge_force_collapse(&ms) < 0)
    2157                 goto fail;
    2158         assert(ms.n == 1);
    2159         assert(ms.pending[0].base == saved_ob_item);
    2160         assert(ms.pending[0].len == saved_ob_size);
     2045    MergeState ms;
     2046    PyObject **lo, **hi;
     2047    Py_ssize_t nremaining;
     2048    Py_ssize_t minrun;
     2049    Py_ssize_t saved_ob_size, saved_allocated;
     2050    PyObject **saved_ob_item;
     2051    PyObject **final_ob_item;
     2052    PyObject *compare = NULL;
     2053    PyObject *result = NULL;            /* guilty until proved innocent */
     2054    int reverse = 0;
     2055    PyObject *keyfunc = NULL;
     2056    Py_ssize_t i;
     2057    PyObject *key, *value, *kvpair;
     2058    static char *kwlist[] = {"cmp", "key", "reverse", 0};
     2059
     2060    assert(self != NULL);
     2061    assert (PyList_Check(self));
     2062    if (args != NULL) {
     2063        if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOi:sort",
     2064            kwlist, &compare, &keyfunc, &reverse))
     2065            return NULL;
     2066    }
     2067    if (compare == Py_None)
     2068        compare = NULL;
     2069    if (compare != NULL &&
     2070        PyErr_WarnPy3k("the cmp argument is not supported in 3.x", 1) < 0)
     2071        return NULL;
     2072    if (keyfunc == Py_None)
     2073        keyfunc = NULL;
     2074    if (compare != NULL && keyfunc != NULL) {
     2075        compare = build_cmpwrapper(compare);
     2076        if (compare == NULL)
     2077            return NULL;
     2078    } else
     2079        Py_XINCREF(compare);
     2080
     2081    /* The list is temporarily made empty, so that mutations performed
     2082    * by comparison functions can't affect the slice of memory we're
     2083    * sorting (allowing mutations during sorting is a core-dump
     2084    * factory, since ob_item may change).
     2085    */
     2086    saved_ob_size = Py_SIZE(self);
     2087    saved_ob_item = self->ob_item;
     2088    saved_allocated = self->allocated;
     2089    Py_SIZE(self) = 0;
     2090    self->ob_item = NULL;
     2091    self->allocated = -1; /* any operation will reset it to >= 0 */
     2092
     2093    if (keyfunc != NULL) {
     2094        for (i=0 ; i < saved_ob_size ; i++) {
     2095            value = saved_ob_item[i];
     2096            key = PyObject_CallFunctionObjArgs(keyfunc, value,
     2097                                               NULL);
     2098            if (key == NULL) {
     2099                for (i=i-1 ; i>=0 ; i--) {
     2100                    kvpair = saved_ob_item[i];
     2101                    value = sortwrapper_getvalue(kvpair);
     2102                    saved_ob_item[i] = value;
     2103                    Py_DECREF(kvpair);
     2104                }
     2105                goto dsu_fail;
     2106            }
     2107            kvpair = build_sortwrapper(key, value);
     2108            if (kvpair == NULL)
     2109                goto dsu_fail;
     2110            saved_ob_item[i] = kvpair;
     2111        }
     2112    }
     2113
     2114    /* Reverse sort stability achieved by initially reversing the list,
     2115    applying a stable forward sort, then reversing the final result. */
     2116    if (reverse && saved_ob_size > 1)
     2117        reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
     2118
     2119    merge_init(&ms, compare);
     2120
     2121    nremaining = saved_ob_size;
     2122    if (nremaining < 2)
     2123        goto succeed;
     2124
     2125    /* March over the array once, left to right, finding natural runs,
     2126    * and extending short natural runs to minrun elements.
     2127    */
     2128    lo = saved_ob_item;
     2129    hi = lo + nremaining;
     2130    minrun = merge_compute_minrun(nremaining);
     2131    do {
     2132        int descending;
     2133        Py_ssize_t n;
     2134
     2135        /* Identify next run. */
     2136        n = count_run(lo, hi, compare, &descending);
     2137        if (n < 0)
     2138            goto fail;
     2139        if (descending)
     2140            reverse_slice(lo, lo + n);
     2141        /* If short, extend to min(minrun, nremaining). */
     2142        if (n < minrun) {
     2143            const Py_ssize_t force = nremaining <= minrun ?
     2144                              nremaining : minrun;
     2145            if (binarysort(lo, lo + force, lo + n, compare) < 0)
     2146                goto fail;
     2147            n = force;
     2148        }
     2149        /* Push run onto pending-runs stack, and maybe merge. */
     2150        assert(ms.n < MAX_MERGE_PENDING);
     2151        ms.pending[ms.n].base = lo;
     2152        ms.pending[ms.n].len = n;
     2153        ++ms.n;
     2154        if (merge_collapse(&ms) < 0)
     2155            goto fail;
     2156        /* Advance to find next run. */
     2157        lo += n;
     2158        nremaining -= n;
     2159    } while (nremaining);
     2160    assert(lo == hi);
     2161
     2162    if (merge_force_collapse(&ms) < 0)
     2163        goto fail;
     2164    assert(ms.n == 1);
     2165    assert(ms.pending[0].base == saved_ob_item);
     2166    assert(ms.pending[0].len == saved_ob_size);
    21612167
    21622168succeed:
    2163         result = Py_None;
     2169    result = Py_None;
    21642170fail:
    2165         if (keyfunc != NULL) {
    2166                 for (i=0 ; i < saved_ob_size ; i++) {
    2167                         kvpair = saved_ob_item[i];
    2168                         value = sortwrapper_getvalue(kvpair);
    2169                         saved_ob_item[i] = value;
    2170                         Py_DECREF(kvpair);
    2171                 }
    2172         }
    2173 
    2174         if (self->allocated != -1 && result != NULL) {
    2175                 /* The user mucked with the list during the sort,
    2176                 * and we don't already have another error to report.
    2177                 */
    2178                 PyErr_SetString(PyExc_ValueError, "list modified during sort");
    2179                 result = NULL;
    2180         }
    2181 
    2182         if (reverse && saved_ob_size > 1)
    2183                 reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
    2184 
    2185         merge_freemem(&ms);
     2171    if (keyfunc != NULL) {
     2172        for (i=0 ; i < saved_ob_size ; i++) {
     2173            kvpair = saved_ob_item[i];
     2174            value = sortwrapper_getvalue(kvpair);
     2175            saved_ob_item[i] = value;
     2176            Py_DECREF(kvpair);
     2177        }
     2178    }
     2179
     2180    if (self->allocated != -1 && result != NULL) {
     2181        /* The user mucked with the list during the sort,
     2182        * and we don't already have another error to report.
     2183        */
     2184        PyErr_SetString(PyExc_ValueError, "list modified during sort");
     2185        result = NULL;
     2186    }
     2187
     2188    if (reverse && saved_ob_size > 1)
     2189        reverse_slice(saved_ob_item, saved_ob_item + saved_ob_size);
     2190
     2191    merge_freemem(&ms);
    21862192
    21872193dsu_fail:
    2188         final_ob_item = self->ob_item;
    2189         i = Py_SIZE(self);
    2190         Py_SIZE(self) = saved_ob_size;
    2191         self->ob_item = saved_ob_item;
    2192         self->allocated = saved_allocated;
    2193         if (final_ob_item != NULL) {
    2194                 /* we cannot use list_clear() for this because it does not
    2195                    guarantee that the list is really empty when it returns */
    2196                 while (--i >= 0) {
    2197                         Py_XDECREF(final_ob_item[i]);
    2198                 }
    2199                 PyMem_FREE(final_ob_item);
    2200         }
    2201         Py_XDECREF(compare);
    2202         Py_XINCREF(result);
    2203         return result;
     2194    final_ob_item = self->ob_item;
     2195    i = Py_SIZE(self);
     2196    Py_SIZE(self) = saved_ob_size;
     2197    self->ob_item = saved_ob_item;
     2198    self->allocated = saved_allocated;
     2199    if (final_ob_item != NULL) {
     2200        /* we cannot use list_clear() for this because it does not
     2201           guarantee that the list is really empty when it returns */
     2202        while (--i >= 0) {
     2203            Py_XDECREF(final_ob_item[i]);
     2204        }
     2205        PyMem_FREE(final_ob_item);
     2206    }
     2207    Py_XDECREF(compare);
     2208    Py_XINCREF(result);
     2209    return result;
    22042210}
    22052211#undef IFLT
     
    22092215PyList_Sort(PyObject *v)
    22102216{
    2211         if (v == NULL || !PyList_Check(v)) {
    2212                 PyErr_BadInternalCall();
    2213                 return -1;
    2214         }
    2215         v = listsort((PyListObject *)v, (PyObject *)NULL, (PyObject *)NULL);
    2216         if (v == NULL)
    2217                 return -1;
    2218         Py_DECREF(v);
    2219         return 0;
     2217    if (v == NULL || !PyList_Check(v)) {
     2218        PyErr_BadInternalCall();
     2219        return -1;
     2220    }
     2221    v = listsort((PyListObject *)v, (PyObject *)NULL, (PyObject *)NULL);
     2222    if (v == NULL)
     2223        return -1;
     2224    Py_DECREF(v);
     2225    return 0;
    22202226}
    22212227
     
    22232229listreverse(PyListObject *self)
    22242230{
    2225         if (Py_SIZE(self) > 1)
    2226                 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
    2227         Py_RETURN_NONE;
     2231    if (Py_SIZE(self) > 1)
     2232        reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
     2233    Py_RETURN_NONE;
    22282234}
    22292235
     
    22312237PyList_Reverse(PyObject *v)
    22322238{
    2233         PyListObject *self = (PyListObject *)v;
    2234 
    2235         if (v == NULL || !PyList_Check(v)) {
    2236                 PyErr_BadInternalCall();
    2237                 return -1;
    2238         }
    2239         if (Py_SIZE(self) > 1)
    2240                 reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
    2241         return 0;
     2239    PyListObject *self = (PyListObject *)v;
     2240
     2241    if (v == NULL || !PyList_Check(v)) {
     2242        PyErr_BadInternalCall();
     2243        return -1;
     2244    }
     2245    if (Py_SIZE(self) > 1)
     2246        reverse_slice(self->ob_item, self->ob_item + Py_SIZE(self));
     2247    return 0;
    22422248}
    22432249
     
    22452251PyList_AsTuple(PyObject *v)
    22462252{
    2247         PyObject *w;
    2248         PyObject **p, **q;
    2249         Py_ssize_t n;
    2250         if (v == NULL || !PyList_Check(v)) {
    2251                 PyErr_BadInternalCall();
    2252                 return NULL;
    2253         }
    2254         n = Py_SIZE(v);
    2255         w = PyTuple_New(n);
    2256         if (w == NULL)
    2257                 return NULL;
    2258         p = ((PyTupleObject *)w)->ob_item;
    2259         q = ((PyListObject *)v)->ob_item;
    2260         while (--n >= 0) {
    2261                 Py_INCREF(*q);
    2262                 *p = *q;
    2263                 p++;
    2264                 q++;
    2265         }
    2266         return w;
     2253    PyObject *w;
     2254    PyObject **p, **q;
     2255    Py_ssize_t n;
     2256    if (v == NULL || !PyList_Check(v)) {
     2257        PyErr_BadInternalCall();
     2258        return NULL;
     2259    }
     2260    n = Py_SIZE(v);
     2261    w = PyTuple_New(n);
     2262    if (w == NULL)
     2263        return NULL;
     2264    p = ((PyTupleObject *)w)->ob_item;
     2265    q = ((PyListObject *)v)->ob_item;
     2266    while (--n >= 0) {
     2267        Py_INCREF(*q);
     2268        *p = *q;
     2269        p++;
     2270        q++;
     2271    }
     2272    return w;
    22672273}
    22682274
     
    22702276listindex(PyListObject *self, PyObject *args)
    22712277{
    2272         Py_ssize_t i, start=0, stop=Py_SIZE(self);
    2273         PyObject *v;
    2274 
    2275         if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
    2276                                     _PyEval_SliceIndex, &start,
    2277                                     _PyEval_SliceIndex, &stop))
    2278                 return NULL;
    2279         if (start < 0) {
    2280                 start += Py_SIZE(self);
    2281                 if (start < 0)
    2282                         start = 0;
    2283         }
    2284         if (stop < 0) {
    2285                 stop += Py_SIZE(self);
    2286                 if (stop < 0)
    2287                         stop = 0;
    2288         }
    2289         for (i = start; i < stop && i < Py_SIZE(self); i++) {
    2290                 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
    2291                 if (cmp > 0)
    2292                         return PyInt_FromSsize_t(i);
    2293                 else if (cmp < 0)
    2294                         return NULL;
    2295         }
    2296         PyErr_SetString(PyExc_ValueError, "list.index(x): x not in list");
    2297         return NULL;
     2278    Py_ssize_t i, start=0, stop=Py_SIZE(self);
     2279    PyObject *v, *format_tuple, *err_string;
     2280    static PyObject *err_format = NULL;
     2281
     2282    if (!PyArg_ParseTuple(args, "O|O&O&:index", &v,
     2283                                _PyEval_SliceIndex, &start,
     2284                                _PyEval_SliceIndex, &stop))
     2285        return NULL;
     2286    if (start < 0) {
     2287        start += Py_SIZE(self);
     2288        if (start < 0)
     2289            start = 0;
     2290    }
     2291    if (stop < 0) {
     2292        stop += Py_SIZE(self);
     2293        if (stop < 0)
     2294            stop = 0;
     2295    }
     2296    for (i = start; i < stop && i < Py_SIZE(self); i++) {
     2297        int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
     2298        if (cmp > 0)
     2299            return PyInt_FromSsize_t(i);
     2300        else if (cmp < 0)
     2301            return NULL;
     2302    }
     2303    if (err_format == NULL) {
     2304        err_format = PyString_FromString("%r is not in list");
     2305        if (err_format == NULL)
     2306            return NULL;
     2307    }
     2308    format_tuple = PyTuple_Pack(1, v);
     2309    if (format_tuple == NULL)
     2310        return NULL;
     2311    err_string = PyString_Format(err_format, format_tuple);
     2312    Py_DECREF(format_tuple);
     2313    if (err_string == NULL)
     2314        return NULL;
     2315    PyErr_SetObject(PyExc_ValueError, err_string);
     2316    Py_DECREF(err_string);
     2317    return NULL;
    22982318}
    22992319
     
    23012321listcount(PyListObject *self, PyObject *v)
    23022322{
    2303         Py_ssize_t count = 0;
    2304         Py_ssize_t i;
    2305 
    2306         for (i = 0; i < Py_SIZE(self); i++) {
    2307                 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
    2308                 if (cmp > 0)
    2309                         count++;
    2310                 else if (cmp < 0)
    2311                         return NULL;
    2312         }
    2313         return PyInt_FromSsize_t(count);
     2323    Py_ssize_t count = 0;
     2324    Py_ssize_t i;
     2325
     2326    for (i = 0; i < Py_SIZE(self); i++) {
     2327        int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
     2328        if (cmp > 0)
     2329            count++;
     2330        else if (cmp < 0)
     2331            return NULL;
     2332    }
     2333    return PyInt_FromSsize_t(count);
    23142334}
    23152335
     
    23172337listremove(PyListObject *self, PyObject *v)
    23182338{
    2319         Py_ssize_t i;
    2320 
    2321         for (i = 0; i < Py_SIZE(self); i++) {
    2322                 int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
    2323                 if (cmp > 0) {
    2324                         if (list_ass_slice(self, i, i+1,
    2325                                            (PyObject *)NULL) == 0)
    2326                                 Py_RETURN_NONE;
    2327                         return NULL;
    2328                 }
    2329                 else if (cmp < 0)
    2330                         return NULL;
    2331         }
    2332         PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
    2333         return NULL;
     2339    Py_ssize_t i;
     2340
     2341    for (i = 0; i < Py_SIZE(self); i++) {
     2342        int cmp = PyObject_RichCompareBool(self->ob_item[i], v, Py_EQ);
     2343        if (cmp > 0) {
     2344            if (list_ass_slice(self, i, i+1,
     2345                               (PyObject *)NULL) == 0)
     2346                Py_RETURN_NONE;
     2347            return NULL;
     2348        }
     2349        else if (cmp < 0)
     2350            return NULL;
     2351    }
     2352    PyErr_SetString(PyExc_ValueError, "list.remove(x): x not in list");
     2353    return NULL;
    23342354}
    23352355
     
    23372357list_traverse(PyListObject *o, visitproc visit, void *arg)
    23382358{
    2339         Py_ssize_t i;
    2340 
    2341         for (i = Py_SIZE(o); --i >= 0; )
    2342                 Py_VISIT(o->ob_item[i]);
    2343         return 0;
     2359    Py_ssize_t i;
     2360
     2361    for (i = Py_SIZE(o); --i >= 0; )
     2362        Py_VISIT(o->ob_item[i]);
     2363    return 0;
    23442364}
    23452365
     
    23472367list_richcompare(PyObject *v, PyObject *w, int op)
    23482368{
    2349         PyListObject *vl, *wl;
    2350         Py_ssize_t i;
    2351 
    2352         if (!PyList_Check(v) || !PyList_Check(w)) {
    2353                 Py_INCREF(Py_NotImplemented);
    2354                 return Py_NotImplemented;
    2355         }
    2356 
    2357         vl = (PyListObject *)v;
    2358         wl = (PyListObject *)w;
    2359 
    2360         if (Py_SIZE(vl) != Py_SIZE(wl) && (op == Py_EQ || op == Py_NE)) {
    2361                 /* Shortcut: if the lengths differ, the lists differ */
    2362                 PyObject *res;
    2363                 if (op == Py_EQ)
    2364                         res = Py_False;
    2365                 else
    2366                         res = Py_True;
    2367                 Py_INCREF(res);
    2368                 return res;
    2369         }
    2370 
    2371         /* Search for the first index where items are different */
    2372         for (i = 0; i < Py_SIZE(vl) && i < Py_SIZE(wl); i++) {
    2373                 int k = PyObject_RichCompareBool(vl->ob_item[i],
    2374                                                 wl->ob_item[i], Py_EQ);
    2375                 if (k < 0)
    2376                         return NULL;
    2377                 if (!k)
    2378                         break;
    2379         }
    2380 
    2381         if (i >= Py_SIZE(vl) || i >= Py_SIZE(wl)) {
    2382                 /* No more items to compare -- compare sizes */
    2383                 Py_ssize_t vs = Py_SIZE(vl);
    2384                 Py_ssize_t ws = Py_SIZE(wl);
    2385                 int cmp;
    2386                 PyObject *res;
    2387                 switch (op) {
    2388                 case Py_LT: cmp = vs <  ws; break;
    2389                 case Py_LE: cmp = vs <= ws; break;
    2390                 case Py_EQ: cmp = vs == ws; break;
    2391                 case Py_NE: cmp = vs != ws; break;
    2392                 case Py_GT: cmp = vs >  ws; break;
    2393                 case Py_GE: cmp = vs >= ws; break;
    2394                 default: return NULL; /* cannot happen */
    2395                 }
    2396                 if (cmp)
    2397                         res = Py_True;
    2398                 else
    2399                         res = Py_False;
    2400                 Py_INCREF(res);
    2401                 return res;
    2402         }
    2403 
    2404         /* We have an item that differs -- shortcuts for EQ/NE */
    2405         if (op == Py_EQ) {
    2406                 Py_INCREF(Py_False);
    2407                 return Py_False;
    2408         }
    2409         if (op == Py_NE) {
    2410                 Py_INCREF(Py_True);
    2411                 return Py_True;
    2412         }
    2413 
    2414         /* Compare the final item again using the proper operator */
    2415         return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
     2369    PyListObject *vl, *wl;
     2370    Py_ssize_t i;
     2371
     2372    if (!PyList_Check(v) || !PyList_Check(w)) {
     2373        Py_INCREF(Py_NotImplemented);
     2374        return Py_NotImplemented;
     2375    }
     2376
     2377    vl = (PyListObject *)v;
     2378    wl = (PyListObject *)w;
     2379
     2380    if (Py_SIZE(vl) != Py_SIZE(wl) && (op == Py_EQ || op == Py_NE)) {
     2381        /* Shortcut: if the lengths differ, the lists differ */
     2382        PyObject *res;
     2383        if (op == Py_EQ)
     2384            res = Py_False;
     2385        else
     2386            res = Py_True;
     2387        Py_INCREF(res);
     2388        return res;
     2389    }
     2390
     2391    /* Search for the first index where items are different */
     2392    for (i = 0; i < Py_SIZE(vl) && i < Py_SIZE(wl); i++) {
     2393        int k = PyObject_RichCompareBool(vl->ob_item[i],
     2394                                        wl->ob_item[i], Py_EQ);
     2395        if (k < 0)
     2396            return NULL;
     2397        if (!k)
     2398            break;
     2399    }
     2400
     2401    if (i >= Py_SIZE(vl) || i >= Py_SIZE(wl)) {
     2402        /* No more items to compare -- compare sizes */
     2403        Py_ssize_t vs = Py_SIZE(vl);
     2404        Py_ssize_t ws = Py_SIZE(wl);
     2405        int cmp;
     2406        PyObject *res;
     2407        switch (op) {
     2408        case Py_LT: cmp = vs <  ws; break;
     2409        case Py_LE: cmp = vs <= ws; break;
     2410        case Py_EQ: cmp = vs == ws; break;
     2411        case Py_NE: cmp = vs != ws; break;
     2412        case Py_GT: cmp = vs >  ws; break;
     2413        case Py_GE: cmp = vs >= ws; break;
     2414        default: return NULL; /* cannot happen */
     2415        }
     2416        if (cmp)
     2417            res = Py_True;
     2418        else
     2419            res = Py_False;
     2420        Py_INCREF(res);
     2421        return res;
     2422    }
     2423
     2424    /* We have an item that differs -- shortcuts for EQ/NE */
     2425    if (op == Py_EQ) {
     2426        Py_INCREF(Py_False);
     2427        return Py_False;
     2428    }
     2429    if (op == Py_NE) {
     2430        Py_INCREF(Py_True);
     2431        return Py_True;
     2432    }
     2433
     2434    /* Compare the final item again using the proper operator */
     2435    return PyObject_RichCompare(vl->ob_item[i], wl->ob_item[i], op);
    24162436}
    24172437
     
    24192439list_init(PyListObject *self, PyObject *args, PyObject *kw)
    24202440{
    2421         PyObject *arg = NULL;
    2422         static char *kwlist[] = {"sequence", 0};
    2423 
    2424         if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:list", kwlist, &arg))
    2425                 return -1;
    2426 
    2427         /* Verify list invariants established by PyType_GenericAlloc() */
    2428         assert(0 <= Py_SIZE(self));
    2429         assert(Py_SIZE(self) <= self->allocated || self->allocated == -1);
    2430         assert(self->ob_item != NULL ||
    2431                self->allocated == 0 || self->allocated == -1);
    2432 
    2433         /* Empty previous contents */
    2434         if (self->ob_item != NULL) {
    2435                 (void)list_clear(self);
    2436         }
    2437         if (arg != NULL) {
    2438                 PyObject *rv = listextend(self, arg);
    2439                 if (rv == NULL)
    2440                         return -1;
    2441                 Py_DECREF(rv);
    2442         }
    2443         return 0;
     2441    PyObject *arg = NULL;
     2442    static char *kwlist[] = {"sequence", 0};
     2443
     2444    if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:list", kwlist, &arg))
     2445        return -1;
     2446
     2447    /* Verify list invariants established by PyType_GenericAlloc() */
     2448    assert(0 <= Py_SIZE(self));
     2449    assert(Py_SIZE(self) <= self->allocated || self->allocated == -1);
     2450    assert(self->ob_item != NULL ||
     2451           self->allocated == 0 || self->allocated == -1);
     2452
     2453    /* Empty previous contents */
     2454    if (self->ob_item != NULL) {
     2455        (void)list_clear(self);
     2456    }
     2457    if (arg != NULL) {
     2458        PyObject *rv = listextend(self, arg);
     2459        if (rv == NULL)
     2460            return -1;
     2461        Py_DECREF(rv);
     2462    }
     2463    return 0;
    24442464}
    24452465
     
    24472467list_sizeof(PyListObject *self)
    24482468{
    2449         Py_ssize_t res;
    2450 
    2451         res = sizeof(PyListObject) + self->allocated * sizeof(void*);
    2452         return PyInt_FromSsize_t(res);
     2469    Py_ssize_t res;
     2470
     2471    res = sizeof(PyListObject) + self->allocated * sizeof(void*);
     2472    return PyInt_FromSsize_t(res);
    24532473}
    24542474
     
    24882508
    24892509static PyMethodDef list_methods[] = {
    2490         {"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, getitem_doc},
    2491         {"__reversed__",(PyCFunction)list_reversed, METH_NOARGS, reversed_doc},
    2492         {"__sizeof__",  (PyCFunction)list_sizeof, METH_NOARGS, sizeof_doc},
    2493         {"append",      (PyCFunction)listappend,  METH_O, append_doc},
    2494         {"insert",      (PyCFunction)listinsert,  METH_VARARGS, insert_doc},
    2495         {"extend",      (PyCFunction)listextend,  METH_O, extend_doc},
    2496         {"pop",         (PyCFunction)listpop,     METH_VARARGS, pop_doc},
    2497         {"remove",      (PyCFunction)listremove,  METH_O, remove_doc},
    2498         {"index",       (PyCFunction)listindex,   METH_VARARGS, index_doc},
    2499         {"count",       (PyCFunction)listcount,   METH_O, count_doc},
    2500         {"reverse",     (PyCFunction)listreverse, METH_NOARGS, reverse_doc},
    2501         {"sort",        (PyCFunction)listsort,    METH_VARARGS | METH_KEYWORDS, sort_doc},
    2502         {NULL,          NULL}           /* sentinel */
     2510    {"__getitem__", (PyCFunction)list_subscript, METH_O|METH_COEXIST, getitem_doc},
     2511    {"__reversed__",(PyCFunction)list_reversed, METH_NOARGS, reversed_doc},
     2512    {"__sizeof__",  (PyCFunction)list_sizeof, METH_NOARGS, sizeof_doc},
     2513    {"append",          (PyCFunction)listappend,  METH_O, append_doc},
     2514    {"insert",          (PyCFunction)listinsert,  METH_VARARGS, insert_doc},
     2515    {"extend",      (PyCFunction)listextend,  METH_O, extend_doc},
     2516    {"pop",             (PyCFunction)listpop,     METH_VARARGS, pop_doc},
     2517    {"remove",          (PyCFunction)listremove,  METH_O, remove_doc},
     2518    {"index",           (PyCFunction)listindex,   METH_VARARGS, index_doc},
     2519    {"count",           (PyCFunction)listcount,   METH_O, count_doc},
     2520    {"reverse",         (PyCFunction)listreverse, METH_NOARGS, reverse_doc},
     2521    {"sort",            (PyCFunction)listsort,    METH_VARARGS | METH_KEYWORDS, sort_doc},
     2522    {NULL,              NULL}           /* sentinel */
    25032523};
    25042524
    25052525static PySequenceMethods list_as_sequence = {
    2506         (lenfunc)list_length,                   /* sq_length */
    2507         (binaryfunc)list_concat,                /* sq_concat */
    2508         (ssizeargfunc)list_repeat,              /* sq_repeat */
    2509         (ssizeargfunc)list_item,                /* sq_item */
    2510         (ssizessizeargfunc)list_slice,          /* sq_slice */
    2511         (ssizeobjargproc)list_ass_item,         /* sq_ass_item */
    2512         (ssizessizeobjargproc)list_ass_slice,   /* sq_ass_slice */
    2513         (objobjproc)list_contains,              /* sq_contains */
    2514         (binaryfunc)list_inplace_concat,        /* sq_inplace_concat */
    2515         (ssizeargfunc)list_inplace_repeat,      /* sq_inplace_repeat */
     2526    (lenfunc)list_length,                       /* sq_length */
     2527    (binaryfunc)list_concat,                    /* sq_concat */
     2528    (ssizeargfunc)list_repeat,                  /* sq_repeat */
     2529    (ssizeargfunc)list_item,                    /* sq_item */
     2530    (ssizessizeargfunc)list_slice,              /* sq_slice */
     2531    (ssizeobjargproc)list_ass_item,             /* sq_ass_item */
     2532    (ssizessizeobjargproc)list_ass_slice,       /* sq_ass_slice */
     2533    (objobjproc)list_contains,                  /* sq_contains */
     2534    (binaryfunc)list_inplace_concat,            /* sq_inplace_concat */
     2535    (ssizeargfunc)list_inplace_repeat,          /* sq_inplace_repeat */
    25162536};
    25172537
     
    25242544list_subscript(PyListObject* self, PyObject* item)
    25252545{
    2526         if (PyIndex_Check(item)) {
    2527                 Py_ssize_t i;
    2528                 i = PyNumber_AsSsize_t(item, PyExc_IndexError);
    2529                 if (i == -1 && PyErr_Occurred())
    2530                         return NULL;
    2531                 if (i < 0)
    2532                         i += PyList_GET_SIZE(self);
    2533                 return list_item(self, i);
    2534         }
    2535         else if (PySlice_Check(item)) {
    2536                 Py_ssize_t start, stop, step, slicelength, cur, i;
    2537                 PyObject* result;
    2538                 PyObject* it;
    2539                 PyObject **src, **dest;
    2540 
    2541                 if (PySlice_GetIndicesEx((PySliceObject*)item, Py_SIZE(self),
    2542                                 &start, &stop, &step, &slicelength) < 0) {
    2543                         return NULL;
    2544                 }
    2545 
    2546                 if (slicelength <= 0) {
    2547                         return PyList_New(0);
    2548                 }
    2549                 else if (step == 1) {
    2550                         return list_slice(self, start, stop);
    2551                 }
    2552                 else {
    2553                         result = PyList_New(slicelength);
    2554                         if (!result) return NULL;
    2555 
    2556                         src = self->ob_item;
    2557                         dest = ((PyListObject *)result)->ob_item;
    2558                         for (cur = start, i = 0; i < slicelength;
    2559                              cur += step, i++) {
    2560                                 it = src[cur];
    2561                                 Py_INCREF(it);
    2562                                 dest[i] = it;
    2563                         }
    2564 
    2565                         return result;
    2566                 }
    2567         }
    2568         else {
    2569                 PyErr_Format(PyExc_TypeError,
    2570                              "list indices must be integers, not %.200s",
    2571                              item->ob_type->tp_name);
    2572                 return NULL;
    2573         }
     2546    if (PyIndex_Check(item)) {
     2547        Py_ssize_t i;
     2548        i = PyNumber_AsSsize_t(item, PyExc_IndexError);
     2549        if (i == -1 && PyErr_Occurred())
     2550            return NULL;
     2551        if (i < 0)
     2552            i += PyList_GET_SIZE(self);
     2553        return list_item(self, i);
     2554    }
     2555    else if (PySlice_Check(item)) {
     2556        Py_ssize_t start, stop, step, slicelength, cur, i;
     2557        PyObject* result;
     2558        PyObject* it;
     2559        PyObject **src, **dest;
     2560
     2561        if (PySlice_GetIndicesEx((PySliceObject*)item, Py_SIZE(self),
     2562                        &start, &stop, &step, &slicelength) < 0) {
     2563            return NULL;
     2564        }
     2565
     2566        if (slicelength <= 0) {
     2567            return PyList_New(0);
     2568        }
     2569        else if (step == 1) {
     2570            return list_slice(self, start, stop);
     2571        }
     2572        else {
     2573            result = PyList_New(slicelength);
     2574            if (!result) return NULL;
     2575
     2576            src = self->ob_item;
     2577            dest = ((PyListObject *)result)->ob_item;
     2578            for (cur = start, i = 0; i < slicelength;
     2579                 cur += step, i++) {
     2580                it = src[cur];
     2581                Py_INCREF(it);
     2582                dest[i] = it;
     2583            }
     2584
     2585            return result;
     2586        }
     2587    }
     2588    else {
     2589        PyErr_Format(PyExc_TypeError,
     2590                     "list indices must be integers, not %.200s",
     2591                     item->ob_type->tp_name);
     2592        return NULL;
     2593    }
    25742594}
    25752595
     
    25772597list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
    25782598{
    2579         if (PyIndex_Check(item)) {
    2580                 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
    2581                 if (i == -1 && PyErr_Occurred())
    2582                         return -1;
    2583                 if (i < 0)
    2584                         i += PyList_GET_SIZE(self);
    2585                 return list_ass_item(self, i, value);
    2586         }
    2587         else if (PySlice_Check(item)) {
    2588                 Py_ssize_t start, stop, step, slicelength;
    2589 
    2590                 if (PySlice_GetIndicesEx((PySliceObject*)item, Py_SIZE(self),
    2591                                 &start, &stop, &step, &slicelength) < 0) {
    2592                         return -1;
    2593                 }
    2594 
    2595                 if (step == 1)
    2596                         return list_ass_slice(self, start, stop, value);
    2597 
    2598                 /* Make sure s[5:2] = [..] inserts at the right place:
    2599                    before 5, not before 2. */
    2600                 if ((step < 0 && start < stop) ||
    2601                     (step > 0 && start > stop))
    2602                         stop = start;
    2603 
    2604                 if (value == NULL) {
    2605                         /* delete slice */
    2606                         PyObject **garbage;
    2607                         size_t cur;
    2608                         Py_ssize_t i;
    2609 
    2610                         if (slicelength <= 0)
    2611                                 return 0;
    2612 
    2613                         if (step < 0) {
    2614                                 stop = start + 1;
    2615                                 start = stop + step*(slicelength - 1) - 1;
    2616                                 step = -step;
    2617                         }
    2618 
    2619                         assert((size_t)slicelength <=
    2620                                PY_SIZE_MAX / sizeof(PyObject*));
    2621 
    2622                         garbage = (PyObject**)
    2623                                 PyMem_MALLOC(slicelength*sizeof(PyObject*));
    2624                         if (!garbage) {
    2625                                 PyErr_NoMemory();
    2626                                 return -1;
    2627                         }
    2628 
    2629                         /* drawing pictures might help understand these for
    2630                            loops. Basically, we memmove the parts of the
    2631                            list that are *not* part of the slice: step-1
    2632                            items for each item that is part of the slice,
    2633                            and then tail end of the list that was not
    2634                            covered by the slice */
    2635                         for (cur = start, i = 0;
    2636                              cur < (size_t)stop;
    2637                              cur += step, i++) {
    2638                                 Py_ssize_t lim = step - 1;
    2639 
    2640                                 garbage[i] = PyList_GET_ITEM(self, cur);
    2641 
    2642                                 if (cur + step >= (size_t)Py_SIZE(self)) {
    2643                                         lim = Py_SIZE(self) - cur - 1;
    2644                                 }
    2645 
    2646                                 memmove(self->ob_item + cur - i,
    2647                                         self->ob_item + cur + 1,
    2648                                         lim * sizeof(PyObject *));
    2649                         }
    2650                         cur = start + slicelength*step;
    2651                         if (cur < (size_t)Py_SIZE(self)) {
    2652                                 memmove(self->ob_item + cur - slicelength,
    2653                                         self->ob_item + cur,
    2654                                         (Py_SIZE(self) - cur) *
    2655                                         sizeof(PyObject *));
    2656                         }
    2657 
    2658                         Py_SIZE(self) -= slicelength;
    2659                         list_resize(self, Py_SIZE(self));
    2660 
    2661                         for (i = 0; i < slicelength; i++) {
    2662                                 Py_DECREF(garbage[i]);
    2663                         }
    2664                         PyMem_FREE(garbage);
    2665 
    2666                         return 0;
    2667                 }
    2668                 else {
    2669                         /* assign slice */
    2670                         PyObject *ins, *seq;
    2671                         PyObject **garbage, **seqitems, **selfitems;
    2672                         Py_ssize_t cur, i;
    2673 
    2674                         /* protect against a[::-1] = a */
    2675                         if (self == (PyListObject*)value) {
    2676                                 seq = list_slice((PyListObject*)value, 0,
    2677                                                    PyList_GET_SIZE(value));
    2678                         }
    2679                         else {
    2680                                 seq = PySequence_Fast(value,
    2681                                                       "must assign iterable "
    2682                                                       "to extended slice");
    2683                         }
    2684                         if (!seq)
    2685                                 return -1;
    2686 
    2687                         if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
    2688                                 PyErr_Format(PyExc_ValueError,
    2689                                         "attempt to assign sequence of "
    2690                                         "size %zd to extended slice of "
    2691                                         "size %zd",
    2692                                              PySequence_Fast_GET_SIZE(seq),
    2693                                              slicelength);
    2694                                 Py_DECREF(seq);
    2695                                 return -1;
    2696                         }
    2697 
    2698                         if (!slicelength) {
    2699                                 Py_DECREF(seq);
    2700                                 return 0;
    2701                         }
    2702 
    2703                         garbage = (PyObject**)
    2704                                 PyMem_MALLOC(slicelength*sizeof(PyObject*));
    2705                         if (!garbage) {
    2706                                 Py_DECREF(seq);
    2707                                 PyErr_NoMemory();
    2708                                 return -1;
    2709                         }
    2710 
    2711                         selfitems = self->ob_item;
    2712                         seqitems = PySequence_Fast_ITEMS(seq);
    2713                         for (cur = start, i = 0; i < slicelength;
    2714                              cur += step, i++) {
    2715                                 garbage[i] = selfitems[cur];
    2716                                 ins = seqitems[i];
    2717                                 Py_INCREF(ins);
    2718                                 selfitems[cur] = ins;
    2719                         }
    2720 
    2721                         for (i = 0; i < slicelength; i++) {
    2722                                 Py_DECREF(garbage[i]);
    2723                         }
    2724 
    2725                         PyMem_FREE(garbage);
    2726                         Py_DECREF(seq);
    2727 
    2728                         return 0;
    2729                 }
    2730         }
    2731         else {
    2732                 PyErr_Format(PyExc_TypeError,
    2733                              "list indices must be integers, not %.200s",
    2734                              item->ob_type->tp_name);
    2735                 return -1;
    2736         }
     2599    if (PyIndex_Check(item)) {
     2600        Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
     2601        if (i == -1 && PyErr_Occurred())
     2602            return -1;
     2603        if (i < 0)
     2604            i += PyList_GET_SIZE(self);
     2605        return list_ass_item(self, i, value);
     2606    }
     2607    else if (PySlice_Check(item)) {
     2608        Py_ssize_t start, stop, step, slicelength;
     2609
     2610        if (PySlice_GetIndicesEx((PySliceObject*)item, Py_SIZE(self),
     2611                        &start, &stop, &step, &slicelength) < 0) {
     2612            return -1;
     2613        }
     2614
     2615        if (step == 1)
     2616            return list_ass_slice(self, start, stop, value);
     2617
     2618        /* Make sure s[5:2] = [..] inserts at the right place:
     2619           before 5, not before 2. */
     2620        if ((step < 0 && start < stop) ||
     2621            (step > 0 && start > stop))
     2622            stop = start;
     2623
     2624        if (value == NULL) {
     2625            /* delete slice */
     2626            PyObject **garbage;
     2627            size_t cur;
     2628            Py_ssize_t i;
     2629
     2630            if (slicelength <= 0)
     2631                return 0;
     2632
     2633            if (step < 0) {
     2634                stop = start + 1;
     2635                start = stop + step*(slicelength - 1) - 1;
     2636                step = -step;
     2637            }
     2638
     2639            assert((size_t)slicelength <=
     2640                   PY_SIZE_MAX / sizeof(PyObject*));
     2641
     2642            garbage = (PyObject**)
     2643                PyMem_MALLOC(slicelength*sizeof(PyObject*));
     2644            if (!garbage) {
     2645                PyErr_NoMemory();
     2646                return -1;
     2647            }
     2648
     2649            /* drawing pictures might help understand these for
     2650               loops. Basically, we memmove the parts of the
     2651               list that are *not* part of the slice: step-1
     2652               items for each item that is part of the slice,
     2653               and then tail end of the list that was not
     2654               covered by the slice */
     2655            for (cur = start, i = 0;
     2656                 cur < (size_t)stop;
     2657                 cur += step, i++) {
     2658                Py_ssize_t lim = step - 1;
     2659
     2660                garbage[i] = PyList_GET_ITEM(self, cur);
     2661
     2662                if (cur + step >= (size_t)Py_SIZE(self)) {
     2663                    lim = Py_SIZE(self) - cur - 1;
     2664                }
     2665
     2666                memmove(self->ob_item + cur - i,
     2667                    self->ob_item + cur + 1,
     2668                    lim * sizeof(PyObject *));
     2669            }
     2670            cur = start + slicelength*step;
     2671            if (cur < (size_t)Py_SIZE(self)) {
     2672                memmove(self->ob_item + cur - slicelength,
     2673                    self->ob_item + cur,
     2674                    (Py_SIZE(self) - cur) *
     2675                    sizeof(PyObject *));
     2676            }
     2677
     2678            Py_SIZE(self) -= slicelength;
     2679            list_resize(self, Py_SIZE(self));
     2680
     2681            for (i = 0; i < slicelength; i++) {
     2682                Py_DECREF(garbage[i]);
     2683            }
     2684            PyMem_FREE(garbage);
     2685
     2686            return 0;
     2687        }
     2688        else {
     2689            /* assign slice */
     2690            PyObject *ins, *seq;
     2691            PyObject **garbage, **seqitems, **selfitems;
     2692            Py_ssize_t cur, i;
     2693
     2694            /* protect against a[::-1] = a */
     2695            if (self == (PyListObject*)value) {
     2696                seq = list_slice((PyListObject*)value, 0,
     2697                                   PyList_GET_SIZE(value));
     2698            }
     2699            else {
     2700                seq = PySequence_Fast(value,
     2701                                      "must assign iterable "
     2702                                      "to extended slice");
     2703            }
     2704            if (!seq)
     2705                return -1;
     2706
     2707            if (PySequence_Fast_GET_SIZE(seq) != slicelength) {
     2708                PyErr_Format(PyExc_ValueError,
     2709                    "attempt to assign sequence of "
     2710                    "size %zd to extended slice of "
     2711                    "size %zd",
     2712                         PySequence_Fast_GET_SIZE(seq),
     2713                         slicelength);
     2714                Py_DECREF(seq);
     2715                return -1;
     2716            }
     2717
     2718            if (!slicelength) {
     2719                Py_DECREF(seq);
     2720                return 0;
     2721            }
     2722
     2723            garbage = (PyObject**)
     2724                PyMem_MALLOC(slicelength*sizeof(PyObject*));
     2725            if (!garbage) {
     2726                Py_DECREF(seq);
     2727                PyErr_NoMemory();
     2728                return -1;
     2729            }
     2730
     2731            selfitems = self->ob_item;
     2732            seqitems = PySequence_Fast_ITEMS(seq);
     2733            for (cur = start, i = 0; i < slicelength;
     2734                 cur += step, i++) {
     2735                garbage[i] = selfitems[cur];
     2736                ins = seqitems[i];
     2737                Py_INCREF(ins);
     2738                selfitems[cur] = ins;
     2739            }
     2740
     2741            for (i = 0; i < slicelength; i++) {
     2742                Py_DECREF(garbage[i]);
     2743            }
     2744
     2745            PyMem_FREE(garbage);
     2746            Py_DECREF(seq);
     2747
     2748            return 0;
     2749        }
     2750    }
     2751    else {
     2752        PyErr_Format(PyExc_TypeError,
     2753                     "list indices must be integers, not %.200s",
     2754                     item->ob_type->tp_name);
     2755        return -1;
     2756    }
    27372757}
    27382758
    27392759static PyMappingMethods list_as_mapping = {
    2740         (lenfunc)list_length,
    2741         (binaryfunc)list_subscript,
    2742         (objobjargproc)list_ass_subscript
     2760    (lenfunc)list_length,
     2761    (binaryfunc)list_subscript,
     2762    (objobjargproc)list_ass_subscript
    27432763};
    27442764
    27452765PyTypeObject PyList_Type = {
    2746         PyVarObject_HEAD_INIT(&PyType_Type, 0)
    2747         "list",
    2748         sizeof(PyListObject),
    2749         0,
    2750         (destructor)list_dealloc,               /* tp_dealloc */
    2751         (printfunc)list_print,                  /* tp_print */
    2752         0,                                      /* tp_getattr */
    2753         0,                                      /* tp_setattr */
    2754         0,                                      /* tp_compare */
    2755         (reprfunc)list_repr,                    /* tp_repr */
    2756         0,                                      /* tp_as_number */
    2757         &list_as_sequence,                      /* tp_as_sequence */
    2758         &list_as_mapping,                       /* tp_as_mapping */
    2759         (hashfunc)PyObject_HashNotImplemented,  /* tp_hash */
    2760         0,                                      /* tp_call */
    2761         0,                                      /* tp_str */
    2762         PyObject_GenericGetAttr,                /* tp_getattro */
    2763         0,                                      /* tp_setattro */
    2764         0,                                      /* tp_as_buffer */
    2765         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
    2766                 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LIST_SUBCLASS, /* tp_flags */
    2767         list_doc,                               /* tp_doc */
    2768         (traverseproc)list_traverse,            /* tp_traverse */
    2769         (inquiry)list_clear,                    /* tp_clear */
    2770         list_richcompare,                       /* tp_richcompare */
    2771         0,                                      /* tp_weaklistoffset */
    2772         list_iter,                              /* tp_iter */
    2773         0,                                      /* tp_iternext */
    2774         list_methods,                           /* tp_methods */
    2775         0,                                      /* tp_members */
    2776         0,                                      /* tp_getset */
    2777         0,                                      /* tp_base */
    2778         0,                                      /* tp_dict */
    2779         0,                                      /* tp_descr_get */
    2780         0,                                      /* tp_descr_set */
    2781         0,                                      /* tp_dictoffset */
    2782         (initproc)list_init,                    /* tp_init */
    2783         PyType_GenericAlloc,                    /* tp_alloc */
    2784         PyType_GenericNew,                      /* tp_new */
    2785         PyObject_GC_Del,                        /* tp_free */
     2766    PyVarObject_HEAD_INIT(&PyType_Type, 0)
     2767    "list",
     2768    sizeof(PyListObject),
     2769    0,
     2770    (destructor)list_dealloc,                   /* tp_dealloc */
     2771    (printfunc)list_print,                      /* tp_print */
     2772    0,                                          /* tp_getattr */
     2773    0,                                          /* tp_setattr */
     2774    0,                                          /* tp_compare */
     2775    (reprfunc)list_repr,                        /* tp_repr */
     2776    0,                                          /* tp_as_number */
     2777    &list_as_sequence,                          /* tp_as_sequence */
     2778    &list_as_mapping,                           /* tp_as_mapping */
     2779    (hashfunc)PyObject_HashNotImplemented,      /* tp_hash */
     2780    0,                                          /* tp_call */
     2781    0,                                          /* tp_str */
     2782    PyObject_GenericGetAttr,                    /* tp_getattro */
     2783    0,                                          /* tp_setattro */
     2784    0,                                          /* tp_as_buffer */
     2785    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
     2786        Py_TPFLAGS_BASETYPE | Py_TPFLAGS_LIST_SUBCLASS,         /* tp_flags */
     2787    list_doc,                                   /* tp_doc */
     2788    (traverseproc)list_traverse,                /* tp_traverse */
     2789    (inquiry)list_clear,                        /* tp_clear */
     2790    list_richcompare,                           /* tp_richcompare */
     2791    0,                                          /* tp_weaklistoffset */
     2792    list_iter,                                  /* tp_iter */
     2793    0,                                          /* tp_iternext */
     2794    list_methods,                               /* tp_methods */
     2795    0,                                          /* tp_members */
     2796    0,                                          /* tp_getset */
     2797    0,                                          /* tp_base */
     2798    0,                                          /* tp_dict */
     2799    0,                                          /* tp_descr_get */
     2800    0,                                          /* tp_descr_set */
     2801    0,                                          /* tp_dictoffset */
     2802    (initproc)list_init,                        /* tp_init */
     2803    PyType_GenericAlloc,                        /* tp_alloc */
     2804    PyType_GenericNew,                          /* tp_new */
     2805    PyObject_GC_Del,                            /* tp_free */
    27862806};
    27872807
     
    27902810
    27912811typedef struct {
    2792         PyObject_HEAD
    2793         long it_index;
    2794         PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
     2812    PyObject_HEAD
     2813    long it_index;
     2814    PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
    27952815} listiterobject;
    27962816
     
    28042824
    28052825static PyMethodDef listiter_methods[] = {
    2806         {"__length_hint__", (PyCFunction)listiter_len, METH_NOARGS, length_hint_doc},
    2807         {NULL,          NULL}           /* sentinel */
     2826    {"__length_hint__", (PyCFunction)listiter_len, METH_NOARGS, length_hint_doc},
     2827    {NULL,              NULL}           /* sentinel */
    28082828};
    28092829
    28102830PyTypeObject PyListIter_Type = {
    2811         PyVarObject_HEAD_INIT(&PyType_Type, 0)
    2812         "listiterator",                         /* tp_name */
    2813         sizeof(listiterobject),                 /* tp_basicsize */
    2814         0,                                      /* tp_itemsize */
    2815         /* methods */
    2816         (destructor)listiter_dealloc,           /* tp_dealloc */
    2817         0,                                      /* tp_print */
    2818         0,                                      /* tp_getattr */
    2819         0,                                      /* tp_setattr */
    2820         0,                                      /* tp_compare */
    2821         0,                                      /* tp_repr */
    2822         0,                                      /* tp_as_number */
    2823         0,                                      /* tp_as_sequence */
    2824         0,                                      /* tp_as_mapping */
    2825         0,                                      /* tp_hash */
    2826         0,                                      /* tp_call */
    2827         0,                                      /* tp_str */
    2828         PyObject_GenericGetAttr,                /* tp_getattro */
    2829         0,                                      /* tp_setattro */
    2830         0,                                      /* tp_as_buffer */
    2831         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
    2832         0,                                      /* tp_doc */
    2833         (traverseproc)listiter_traverse,        /* tp_traverse */
    2834         0,                                      /* tp_clear */
    2835         0,                                      /* tp_richcompare */
    2836         0,                                      /* tp_weaklistoffset */
    2837         PyObject_SelfIter,                      /* tp_iter */
    2838         (iternextfunc)listiter_next,            /* tp_iternext */
    2839         listiter_methods,                       /* tp_methods */
    2840         0,                                      /* tp_members */
     2831    PyVarObject_HEAD_INIT(&PyType_Type, 0)
     2832    "listiterator",                             /* tp_name */
     2833    sizeof(listiterobject),                     /* tp_basicsize */
     2834    0,                                          /* tp_itemsize */
     2835    /* methods */
     2836    (destructor)listiter_dealloc,               /* tp_dealloc */
     2837    0,                                          /* tp_print */
     2838    0,                                          /* tp_getattr */
     2839    0,                                          /* tp_setattr */
     2840    0,                                          /* tp_compare */
     2841    0,                                          /* tp_repr */
     2842    0,                                          /* tp_as_number */
     2843    0,                                          /* tp_as_sequence */
     2844    0,                                          /* tp_as_mapping */
     2845    0,                                          /* tp_hash */
     2846    0,                                          /* tp_call */
     2847    0,                                          /* tp_str */
     2848    PyObject_GenericGetAttr,                    /* tp_getattro */
     2849    0,                                          /* tp_setattro */
     2850    0,                                          /* tp_as_buffer */
     2851    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
     2852    0,                                          /* tp_doc */
     2853    (traverseproc)listiter_traverse,            /* tp_traverse */
     2854    0,                                          /* tp_clear */
     2855    0,                                          /* tp_richcompare */
     2856    0,                                          /* tp_weaklistoffset */
     2857    PyObject_SelfIter,                          /* tp_iter */
     2858    (iternextfunc)listiter_next,                /* tp_iternext */
     2859    listiter_methods,                           /* tp_methods */
     2860    0,                                          /* tp_members */
    28412861};
    28422862
     
    28452865list_iter(PyObject *seq)
    28462866{
    2847         listiterobject *it;
    2848 
    2849         if (!PyList_Check(seq)) {
    2850                 PyErr_BadInternalCall();
    2851                 return NULL;
    2852         }
    2853         it = PyObject_GC_New(listiterobject, &PyListIter_Type);
    2854         if (it == NULL)
    2855                 return NULL;
    2856         it->it_index = 0;
    2857         Py_INCREF(seq);
    2858         it->it_seq = (PyListObject *)seq;
    2859         _PyObject_GC_TRACK(it);
    2860         return (PyObject *)it;
     2867    listiterobject *it;
     2868
     2869    if (!PyList_Check(seq)) {
     2870        PyErr_BadInternalCall();
     2871        return NULL;
     2872    }
     2873    it = PyObject_GC_New(listiterobject, &PyListIter_Type);
     2874    if (it == NULL)
     2875        return NULL;
     2876    it->it_index = 0;
     2877    Py_INCREF(seq);
     2878    it->it_seq = (PyListObject *)seq;
     2879    _PyObject_GC_TRACK(it);
     2880    return (PyObject *)it;
    28612881}
    28622882
     
    28642884listiter_dealloc(listiterobject *it)
    28652885{
    2866         _PyObject_GC_UNTRACK(it);
    2867         Py_XDECREF(it->it_seq);
    2868         PyObject_GC_Del(it);
     2886    _PyObject_GC_UNTRACK(it);
     2887    Py_XDECREF(it->it_seq);
     2888    PyObject_GC_Del(it);
    28692889}
    28702890
     
    28722892listiter_traverse(listiterobject *it, visitproc visit, void *arg)
    28732893{
    2874         Py_VISIT(it->it_seq);
    2875         return 0;
     2894    Py_VISIT(it->it_seq);
     2895    return 0;
    28762896}
    28772897
     
    28792899listiter_next(listiterobject *it)
    28802900{
    2881         PyListObject *seq;
    2882         PyObject *item;
    2883 
    2884         assert(it != NULL);
    2885         seq = it->it_seq;
    2886         if (seq == NULL)
    2887                 return NULL;
    2888         assert(PyList_Check(seq));
    2889 
    2890         if (it->it_index < PyList_GET_SIZE(seq)) {
    2891                 item = PyList_GET_ITEM(seq, it->it_index);
    2892                 ++it->it_index;
    2893                 Py_INCREF(item);
    2894                 return item;
    2895         }
    2896 
    2897         Py_DECREF(seq);
    2898         it->it_seq = NULL;
    2899         return NULL;
     2901    PyListObject *seq;
     2902    PyObject *item;
     2903
     2904    assert(it != NULL);
     2905    seq = it->it_seq;
     2906    if (seq == NULL)
     2907        return NULL;
     2908    assert(PyList_Check(seq));
     2909
     2910    if (it->it_index < PyList_GET_SIZE(seq)) {
     2911        item = PyList_GET_ITEM(seq, it->it_index);
     2912        ++it->it_index;
     2913        Py_INCREF(item);
     2914        return item;
     2915    }
     2916
     2917    Py_DECREF(seq);
     2918    it->it_seq = NULL;
     2919    return NULL;
    29002920}
    29012921
     
    29032923listiter_len(listiterobject *it)
    29042924{
    2905         Py_ssize_t len;
    2906         if (it->it_seq) {
    2907                 len = PyList_GET_SIZE(it->it_seq) - it->it_index;
    2908                 if (len >= 0)
    2909                         return PyInt_FromSsize_t(len);
    2910         }
    2911         return PyInt_FromLong(0);
     2925    Py_ssize_t len;
     2926    if (it->it_seq) {
     2927        len = PyList_GET_SIZE(it->it_seq) - it->it_index;
     2928        if (len >= 0)
     2929            return PyInt_FromSsize_t(len);
     2930    }
     2931    return PyInt_FromLong(0);
    29122932}
    29132933/*********************** List Reverse Iterator **************************/
    29142934
    29152935typedef struct {
    2916         PyObject_HEAD
    2917         Py_ssize_t it_index;
    2918         PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
     2936    PyObject_HEAD
     2937    Py_ssize_t it_index;
     2938    PyListObject *it_seq; /* Set to NULL when iterator is exhausted */
    29192939} listreviterobject;
    29202940
     
    29262946
    29272947static PyMethodDef listreviter_methods[] = {
    2928         {"__length_hint__", (PyCFunction)listreviter_len, METH_NOARGS, length_hint_doc},
    2929         {NULL,          NULL}           /* sentinel */
     2948    {"__length_hint__", (PyCFunction)listreviter_len, METH_NOARGS, length_hint_doc},
     2949    {NULL,              NULL}           /* sentinel */
    29302950};
    29312951
    29322952PyTypeObject PyListRevIter_Type = {
    2933         PyVarObject_HEAD_INIT(&PyType_Type, 0)
    2934         "listreverseiterator",                  /* tp_name */
    2935         sizeof(listreviterobject),              /* tp_basicsize */
    2936         0,                                      /* tp_itemsize */
    2937         /* methods */
    2938         (destructor)listreviter_dealloc,        /* tp_dealloc */
    2939         0,                                      /* tp_print */
    2940         0,                                      /* tp_getattr */
    2941         0,                                      /* tp_setattr */
    2942         0,                                      /* tp_compare */
    2943         0,                                      /* tp_repr */
    2944         0,                                      /* tp_as_number */
    2945         0,                                      /* tp_as_sequence */
    2946         0,                                      /* tp_as_mapping */
    2947         0,                                      /* tp_hash */
    2948         0,                                      /* tp_call */
    2949         0,                                      /* tp_str */
    2950         PyObject_GenericGetAttr,                /* tp_getattro */
    2951         0,                                      /* tp_setattro */
    2952         0,                                      /* tp_as_buffer */
    2953         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
    2954         0,                                      /* tp_doc */
    2955         (traverseproc)listreviter_traverse,     /* tp_traverse */
    2956         0,                                      /* tp_clear */
    2957         0,                                      /* tp_richcompare */
    2958         0,                                      /* tp_weaklistoffset */
    2959         PyObject_SelfIter,                      /* tp_iter */
    2960         (iternextfunc)listreviter_next,         /* tp_iternext */
    2961         listreviter_methods,            /* tp_methods */
    2962         0,
     2953    PyVarObject_HEAD_INIT(&PyType_Type, 0)
     2954    "listreverseiterator",                      /* tp_name */
     2955    sizeof(listreviterobject),                  /* tp_basicsize */
     2956    0,                                          /* tp_itemsize */
     2957    /* methods */
     2958    (destructor)listreviter_dealloc,            /* tp_dealloc */
     2959    0,                                          /* tp_print */
     2960    0,                                          /* tp_getattr */
     2961    0,                                          /* tp_setattr */
     2962    0,                                          /* tp_compare */
     2963    0,                                          /* tp_repr */
     2964    0,                                          /* tp_as_number */
     2965    0,                                          /* tp_as_sequence */
     2966    0,                                          /* tp_as_mapping */
     2967    0,                                          /* tp_hash */
     2968    0,                                          /* tp_call */
     2969    0,                                          /* tp_str */
     2970    PyObject_GenericGetAttr,                    /* tp_getattro */
     2971    0,                                          /* tp_setattro */
     2972    0,                                          /* tp_as_buffer */
     2973    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
     2974    0,                                          /* tp_doc */
     2975    (traverseproc)listreviter_traverse,         /* tp_traverse */
     2976    0,                                          /* tp_clear */
     2977    0,                                          /* tp_richcompare */
     2978    0,                                          /* tp_weaklistoffset */
     2979    PyObject_SelfIter,                          /* tp_iter */
     2980    (iternextfunc)listreviter_next,             /* tp_iternext */
     2981    listreviter_methods,                /* tp_methods */
     2982    0,
    29632983};
    29642984
     
    29662986list_reversed(PyListObject *seq, PyObject *unused)
    29672987{
    2968         listreviterobject *it;
    2969 
    2970         it = PyObject_GC_New(listreviterobject, &PyListRevIter_Type);
    2971         if (it == NULL)
    2972                 return NULL;
    2973         assert(PyList_Check(seq));
    2974         it->it_index = PyList_GET_SIZE(seq) - 1;
    2975         Py_INCREF(seq);
    2976         it->it_seq = seq;
    2977         PyObject_GC_Track(it);
    2978         return (PyObject *)it;
     2988    listreviterobject *it;
     2989
     2990    it = PyObject_GC_New(listreviterobject, &PyListRevIter_Type);
     2991    if (it == NULL)
     2992        return NULL;
     2993    assert(PyList_Check(seq));
     2994    it->it_index = PyList_GET_SIZE(seq) - 1;
     2995    Py_INCREF(seq);
     2996    it->it_seq = seq;
     2997    PyObject_GC_Track(it);
     2998    return (PyObject *)it;
    29792999}
    29803000
     
    29823002listreviter_dealloc(listreviterobject *it)
    29833003{
    2984         PyObject_GC_UnTrack(it);
    2985         Py_XDECREF(it->it_seq);
    2986         PyObject_GC_Del(it);
     3004    PyObject_GC_UnTrack(it);
     3005    Py_XDECREF(it->it_seq);
     3006    PyObject_GC_Del(it);
    29873007}
    29883008
     
    29903010listreviter_traverse(listreviterobject *it, visitproc visit, void *arg)
    29913011{
    2992         Py_VISIT(it->it_seq);
    2993         return 0;
     3012    Py_VISIT(it->it_seq);
     3013    return 0;
    29943014}
    29953015
     
    29973017listreviter_next(listreviterobject *it)
    29983018{
    2999         PyObject *item;
    3000         Py_ssize_t index = it->it_index;
    3001         PyListObject *seq = it->it_seq;
    3002 
    3003         if (index>=0 && index < PyList_GET_SIZE(seq)) {
    3004                 item = PyList_GET_ITEM(seq, index);
    3005                 it->it_index--;
    3006                 Py_INCREF(item);
    3007                 return item;
    3008         }
    3009         it->it_index = -1;
    3010         if (seq != NULL) {
    3011                 it->it_seq = NULL;
    3012                 Py_DECREF(seq);
    3013         }
    3014         return NULL;
     3019    PyObject *item;
     3020    Py_ssize_t index = it->it_index;
     3021    PyListObject *seq = it->it_seq;
     3022
     3023    if (index>=0 && index < PyList_GET_SIZE(seq)) {
     3024        item = PyList_GET_ITEM(seq, index);
     3025        it->it_index--;
     3026        Py_INCREF(item);
     3027        return item;
     3028    }
     3029    it->it_index = -1;
     3030    if (seq != NULL) {
     3031        it->it_seq = NULL;
     3032        Py_DECREF(seq);
     3033    }
     3034    return NULL;
    30153035}
    30163036
     
    30183038listreviter_len(listreviterobject *it)
    30193039{
    3020         Py_ssize_t len = it->it_index + 1;
    3021         if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len)
    3022                 len = 0;
    3023         return PyLong_FromSsize_t(len);
    3024 }
     3040    Py_ssize_t len = it->it_index + 1;
     3041    if (it->it_seq == NULL || PyList_GET_SIZE(it->it_seq) < len)
     3042        len = 0;
     3043    return PyLong_FromSsize_t(len);
     3044}
Note: See TracChangeset for help on using the changeset viewer.