Ignore:
Timestamp:
Mar 19, 2014, 11:11:30 AM (11 years ago)
Author:
dmik
Message:

python: Update vendor to 2.7.6.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/vendor/current/Objects/setobject.c

    r2 r388  
    11
    2 /* set object implementation 
     2/* set object implementation
    33   Written and maintained by Raymond D. Hettinger <python@rcn.com>
    44   Derived from Lib/sets.py and Objects/dictobject.c.
     
    1717set_key_error(PyObject *arg)
    1818{
    19         PyObject *tup;
    20         tup = PyTuple_Pack(1, arg);
    21         if (!tup)
    22                 return; /* caller will expect error to be set anyway */
    23         PyErr_SetObject(PyExc_KeyError, tup);
    24         Py_DECREF(tup);
     19    PyObject *tup;
     20    tup = PyTuple_Pack(1, arg);
     21    if (!tup)
     22        return; /* caller will expect error to be set anyway */
     23    PyErr_SetObject(PyExc_KeyError, tup);
     24    Py_DECREF(tup);
    2525}
    2626
     
    3535_PySet_Dummy(void)
    3636{
    37         return dummy;
     37    return dummy;
    3838}
    3939#endif
    4040
    41 #define INIT_NONZERO_SET_SLOTS(so) do {                         \
    42         (so)->table = (so)->smalltable;                         \
    43         (so)->mask = PySet_MINSIZE - 1;                         \
    44         (so)->hash = -1;                                        \
     41#define INIT_NONZERO_SET_SLOTS(so) do {                         \
     42    (so)->table = (so)->smalltable;                             \
     43    (so)->mask = PySet_MINSIZE - 1;                             \
     44    (so)->hash = -1;                                            \
    4545    } while(0)
    4646
    47 #define EMPTY_TO_MINSIZE(so) do {                               \
    48         memset((so)->smalltable, 0, sizeof((so)->smalltable));  \
    49         (so)->used = (so)->fill = 0;                            \
    50         INIT_NONZERO_SET_SLOTS(so);                             \
     47#define EMPTY_TO_MINSIZE(so) do {                               \
     48    memset((so)->smalltable, 0, sizeof((so)->smalltable));      \
     49    (so)->used = (so)->fill = 0;                                \
     50    INIT_NONZERO_SET_SLOTS(so);                                 \
    5151    } while(0)
    5252
     
    7676set_lookkey(PySetObject *so, PyObject *key, register long hash)
    7777{
    78         register Py_ssize_t i;
    79         register size_t perturb;
    80         register setentry *freeslot;
    81         register size_t mask = so->mask;
    82         setentry *table = so->table;
    83         register setentry *entry;
    84         register int cmp;
    85         PyObject *startkey;
    86 
    87         i = hash & mask;
    88         entry = &table[i];
    89         if (entry->key == NULL || entry->key == key)
    90                 return entry;
    91 
    92         if (entry->key == dummy)
    93                 freeslot = entry;
    94         else {
    95                 if (entry->hash == hash) {
    96                         startkey = entry->key;
    97                         Py_INCREF(startkey);
    98                         cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
    99                         Py_DECREF(startkey);
    100                         if (cmp < 0)
    101                                 return NULL;
    102                         if (table == so->table && entry->key == startkey) {
    103                                 if (cmp > 0)
    104                                         return entry;
    105                         }
    106                         else {
    107                                 /* The compare did major nasty stuff to the
    108                                 * set:  start over.
    109                                  */
    110                                 return set_lookkey(so, key, hash);
    111                         }
    112                 }
    113                 freeslot = NULL;
    114         }
    115 
    116         /* In the loop, key == dummy is by far (factor of 100s) the
    117            least likely outcome, so test for that last. */
    118         for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
    119                 i = (i << 2) + i + perturb + 1;
    120                 entry = &table[i & mask];
    121                 if (entry->key == NULL) {
    122                         if (freeslot != NULL)
    123                                 entry = freeslot;
    124                         break;
    125                 }
    126                 if (entry->key == key)
    127                         break;
    128                 if (entry->hash == hash && entry->key != dummy) {
    129                         startkey = entry->key;
    130                         Py_INCREF(startkey);
    131                         cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
    132                         Py_DECREF(startkey);
    133                         if (cmp < 0)
    134                                 return NULL;
    135                         if (table == so->table && entry->key == startkey) {
    136                                 if (cmp > 0)
    137                                         break;
    138                         }
    139                         else {
    140                                 /* The compare did major nasty stuff to the
    141                                 * set:  start over.
    142                                  */
    143                                 return set_lookkey(so, key, hash);
    144                         }
    145                 }
    146                 else if (entry->key == dummy && freeslot == NULL)
    147                         freeslot = entry;
    148         }
    149         return entry;
     78    register Py_ssize_t i;
     79    register size_t perturb;
     80    register setentry *freeslot;
     81    register size_t mask = so->mask;
     82    setentry *table = so->table;
     83    register setentry *entry;
     84    register int cmp;
     85    PyObject *startkey;
     86
     87    i = hash & mask;
     88    entry = &table[i];
     89    if (entry->key == NULL || entry->key == key)
     90        return entry;
     91
     92    if (entry->key == dummy)
     93        freeslot = entry;
     94    else {
     95        if (entry->hash == hash) {
     96            startkey = entry->key;
     97            Py_INCREF(startkey);
     98            cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
     99            Py_DECREF(startkey);
     100            if (cmp < 0)
     101                return NULL;
     102            if (table == so->table && entry->key == startkey) {
     103                if (cmp > 0)
     104                    return entry;
     105            }
     106            else {
     107                /* The compare did major nasty stuff to the
     108                * set:  start over.
     109                 */
     110                return set_lookkey(so, key, hash);
     111            }
     112        }
     113        freeslot = NULL;
     114    }
     115
     116    /* In the loop, key == dummy is by far (factor of 100s) the
     117       least likely outcome, so test for that last. */
     118    for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
     119        i = (i << 2) + i + perturb + 1;
     120        entry = &table[i & mask];
     121        if (entry->key == NULL) {
     122            if (freeslot != NULL)
     123                entry = freeslot;
     124            break;
     125        }
     126        if (entry->key == key)
     127            break;
     128        if (entry->hash == hash && entry->key != dummy) {
     129            startkey = entry->key;
     130            Py_INCREF(startkey);
     131            cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
     132            Py_DECREF(startkey);
     133            if (cmp < 0)
     134                return NULL;
     135            if (table == so->table && entry->key == startkey) {
     136                if (cmp > 0)
     137                    break;
     138            }
     139            else {
     140                /* The compare did major nasty stuff to the
     141                * set:  start over.
     142                 */
     143                return set_lookkey(so, key, hash);
     144            }
     145        }
     146        else if (entry->key == dummy && freeslot == NULL)
     147            freeslot = entry;
     148    }
     149    return entry;
    150150}
    151151
     
    158158set_lookkey_string(PySetObject *so, PyObject *key, register long hash)
    159159{
    160         register Py_ssize_t i;
    161         register size_t perturb;
    162         register setentry *freeslot;
    163         register size_t mask = so->mask;
    164         setentry *table = so->table;
    165         register setentry *entry;
    166 
    167         /* Make sure this function doesn't have to handle non-string keys,
    168            including subclasses of str; e.g., one reason to subclass
    169            strings is to override __eq__, and for speed we don't cater to
    170            that here. */
    171         if (!PyString_CheckExact(key)) {
    172                 so->lookup = set_lookkey;
    173                 return set_lookkey(so, key, hash);
    174         }
    175         i = hash & mask;
    176         entry = &table[i];
    177         if (entry->key == NULL || entry->key == key)
    178                 return entry;
    179         if (entry->key == dummy)
    180                 freeslot = entry;
    181         else {
    182                 if (entry->hash == hash && _PyString_Eq(entry->key, key))
    183                         return entry;
    184                 freeslot = NULL;
    185         }
    186 
    187         /* In the loop, key == dummy is by far (factor of 100s) the
    188            least likely outcome, so test for that last. */
    189         for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
    190                 i = (i << 2) + i + perturb + 1;
    191                 entry = &table[i & mask];
    192                 if (entry->key == NULL)
    193                         return freeslot == NULL ? entry : freeslot;
    194                 if (entry->key == key
    195                     || (entry->hash == hash
    196                         && entry->key != dummy
    197                         && _PyString_Eq(entry->key, key)))
    198                         return entry;
    199                 if (entry->key == dummy && freeslot == NULL)
    200                         freeslot = entry;
    201         }
    202         assert(0);      /* NOT REACHED */
    203         return 0;
     160    register Py_ssize_t i;
     161    register size_t perturb;
     162    register setentry *freeslot;
     163    register size_t mask = so->mask;
     164    setentry *table = so->table;
     165    register setentry *entry;
     166
     167    /* Make sure this function doesn't have to handle non-string keys,
     168       including subclasses of str; e.g., one reason to subclass
     169       strings is to override __eq__, and for speed we don't cater to
     170       that here. */
     171    if (!PyString_CheckExact(key)) {
     172        so->lookup = set_lookkey;
     173        return set_lookkey(so, key, hash);
     174    }
     175    i = hash & mask;
     176    entry = &table[i];
     177    if (entry->key == NULL || entry->key == key)
     178        return entry;
     179    if (entry->key == dummy)
     180        freeslot = entry;
     181    else {
     182        if (entry->hash == hash && _PyString_Eq(entry->key, key))
     183            return entry;
     184        freeslot = NULL;
     185    }
     186
     187    /* In the loop, key == dummy is by far (factor of 100s) the
     188       least likely outcome, so test for that last. */
     189    for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
     190        i = (i << 2) + i + perturb + 1;
     191        entry = &table[i & mask];
     192        if (entry->key == NULL)
     193            return freeslot == NULL ? entry : freeslot;
     194        if (entry->key == key
     195            || (entry->hash == hash
     196            && entry->key != dummy
     197            && _PyString_Eq(entry->key, key)))
     198            return entry;
     199        if (entry->key == dummy && freeslot == NULL)
     200            freeslot = entry;
     201    }
     202    assert(0);          /* NOT REACHED */
     203    return 0;
    204204}
    205205
     
    212212set_insert_key(register PySetObject *so, PyObject *key, long hash)
    213213{
    214         register setentry *entry;
    215         typedef setentry *(*lookupfunc)(PySetObject *, PyObject *, long);
    216 
    217         assert(so->lookup != NULL);
    218         entry = so->lookup(so, key, hash);
    219         if (entry == NULL)
    220                 return -1;
    221         if (entry->key == NULL) {
    222                 /* UNUSED */
    223                 so->fill++;
    224                 entry->key = key;
    225                 entry->hash = hash;
    226                 so->used++;
    227         } else if (entry->key == dummy) {
    228                 /* DUMMY */
    229                 entry->key = key;
    230                 entry->hash = hash;
    231                 so->used++;
    232                 Py_DECREF(dummy);
    233         } else {
    234                 /* ACTIVE */
    235                 Py_DECREF(key);
    236         }
    237         return 0;
     214    register setentry *entry;
     215
     216    assert(so->lookup != NULL);
     217    entry = so->lookup(so, key, hash);
     218    if (entry == NULL)
     219        return -1;
     220    if (entry->key == NULL) {
     221        /* UNUSED */
     222        so->fill++;
     223        entry->key = key;
     224        entry->hash = hash;
     225        so->used++;
     226    } else if (entry->key == dummy) {
     227        /* DUMMY */
     228        entry->key = key;
     229        entry->hash = hash;
     230        so->used++;
     231        Py_DECREF(dummy);
     232    } else {
     233        /* ACTIVE */
     234        Py_DECREF(key);
     235    }
     236    return 0;
    238237}
    239238
     
    249248set_insert_clean(register PySetObject *so, PyObject *key, long hash)
    250249{
    251         register size_t i;
    252         register size_t perturb;
    253         register size_t mask = (size_t)so->mask;
    254         setentry *table = so->table;
    255         register setentry *entry;
    256 
    257         i = hash & mask;
    258         entry = &table[i];
    259         for (perturb = hash; entry->key != NULL; perturb >>= PERTURB_SHIFT) {
    260                 i = (i << 2) + i + perturb + 1;
    261                 entry = &table[i & mask];
    262         }
    263         so->fill++;
    264         entry->key = key;
    265         entry->hash = hash;
    266         so->used++;
     250    register size_t i;
     251    register size_t perturb;
     252    register size_t mask = (size_t)so->mask;
     253    setentry *table = so->table;
     254    register setentry *entry;
     255
     256    i = hash & mask;
     257    entry = &table[i];
     258    for (perturb = hash; entry->key != NULL; perturb >>= PERTURB_SHIFT) {
     259        i = (i << 2) + i + perturb + 1;
     260        entry = &table[i & mask];
     261    }
     262    so->fill++;
     263    entry->key = key;
     264    entry->hash = hash;
     265    so->used++;
    267266}
    268267
     
    275274set_table_resize(PySetObject *so, Py_ssize_t minused)
    276275{
    277         Py_ssize_t newsize;
    278         setentry *oldtable, *newtable, *entry;
    279         Py_ssize_t i;
    280         int is_oldtable_malloced;
    281         setentry small_copy[PySet_MINSIZE];
    282 
    283         assert(minused >= 0);
    284 
    285         /* Find the smallest table size > minused. */
    286         for (newsize = PySet_MINSIZE;
    287              newsize <= minused && newsize > 0;
    288              newsize <<= 1)
    289                 ;
    290         if (newsize <= 0) {
    291                 PyErr_NoMemory();
    292                 return -1;
    293         }
    294 
    295         /* Get space for a new table. */
    296         oldtable = so->table;
    297         assert(oldtable != NULL);
    298         is_oldtable_malloced = oldtable != so->smalltable;
    299 
    300         if (newsize == PySet_MINSIZE) {
    301                 /* A large table is shrinking, or we can't get any smaller. */
    302                 newtable = so->smalltable;
    303                 if (newtable == oldtable) {
    304                         if (so->fill == so->used) {
    305                                 /* No dummies, so no point doing anything. */
    306                                 return 0;
    307                         }
    308                         /* We're not going to resize it, but rebuild the
    309                            table anyway to purge old dummy entries.
    310                            Subtle:  This is *necessary* if fill==size,
    311                            as set_lookkey needs at least one virgin slot to
    312                            terminate failing searches.  If fill < size, it's
    313                            merely desirable, as dummies slow searches. */
    314                         assert(so->fill > so->used);
    315                         memcpy(small_copy, oldtable, sizeof(small_copy));
    316                         oldtable = small_copy;
    317                 }
    318         }
    319         else {
    320                 newtable = PyMem_NEW(setentry, newsize);
    321                 if (newtable == NULL) {
    322                         PyErr_NoMemory();
    323                         return -1;
    324                 }
    325         }
    326 
    327         /* Make the set empty, using the new table. */
    328         assert(newtable != oldtable);
    329         so->table = newtable;
    330         so->mask = newsize - 1;
    331         memset(newtable, 0, sizeof(setentry) * newsize);
    332         so->used = 0;
    333         i = so->fill;
    334         so->fill = 0;
    335 
    336         /* Copy the data over; this is refcount-neutral for active entries;
    337            dummy entries aren't copied over, of course */
    338         for (entry = oldtable; i > 0; entry++) {
    339                 if (entry->key == NULL) {
    340                         /* UNUSED */
    341                         ;
    342                 } else if (entry->key == dummy) {
    343                         /* DUMMY */
    344                         --i;
    345                         assert(entry->key == dummy);
    346                         Py_DECREF(entry->key);
    347                 } else {
    348                         /* ACTIVE */
    349                         --i;
    350                         set_insert_clean(so, entry->key, entry->hash);
    351                 }
    352         }
    353 
    354         if (is_oldtable_malloced)
    355                 PyMem_DEL(oldtable);
    356         return 0;
     276    Py_ssize_t newsize;
     277    setentry *oldtable, *newtable, *entry;
     278    Py_ssize_t i;
     279    int is_oldtable_malloced;
     280    setentry small_copy[PySet_MINSIZE];
     281
     282    assert(minused >= 0);
     283
     284    /* Find the smallest table size > minused. */
     285    for (newsize = PySet_MINSIZE;
     286         newsize <= minused && newsize > 0;
     287         newsize <<= 1)
     288        ;
     289    if (newsize <= 0) {
     290        PyErr_NoMemory();
     291        return -1;
     292    }
     293
     294    /* Get space for a new table. */
     295    oldtable = so->table;
     296    assert(oldtable != NULL);
     297    is_oldtable_malloced = oldtable != so->smalltable;
     298
     299    if (newsize == PySet_MINSIZE) {
     300        /* A large table is shrinking, or we can't get any smaller. */
     301        newtable = so->smalltable;
     302        if (newtable == oldtable) {
     303            if (so->fill == so->used) {
     304                /* No dummies, so no point doing anything. */
     305                return 0;
     306            }
     307            /* We're not going to resize it, but rebuild the
     308               table anyway to purge old dummy entries.
     309               Subtle:  This is *necessary* if fill==size,
     310               as set_lookkey needs at least one virgin slot to
     311               terminate failing searches.  If fill < size, it's
     312               merely desirable, as dummies slow searches. */
     313            assert(so->fill > so->used);
     314            memcpy(small_copy, oldtable, sizeof(small_copy));
     315            oldtable = small_copy;
     316        }
     317    }
     318    else {
     319        newtable = PyMem_NEW(setentry, newsize);
     320        if (newtable == NULL) {
     321            PyErr_NoMemory();
     322            return -1;
     323        }
     324    }
     325
     326    /* Make the set empty, using the new table. */
     327    assert(newtable != oldtable);
     328    so->table = newtable;
     329    so->mask = newsize - 1;
     330    memset(newtable, 0, sizeof(setentry) * newsize);
     331    so->used = 0;
     332    i = so->fill;
     333    so->fill = 0;
     334
     335    /* Copy the data over; this is refcount-neutral for active entries;
     336       dummy entries aren't copied over, of course */
     337    for (entry = oldtable; i > 0; entry++) {
     338        if (entry->key == NULL) {
     339            /* UNUSED */
     340            ;
     341        } else if (entry->key == dummy) {
     342            /* DUMMY */
     343            --i;
     344            assert(entry->key == dummy);
     345            Py_DECREF(entry->key);
     346        } else {
     347            /* ACTIVE */
     348            --i;
     349            set_insert_clean(so, entry->key, entry->hash);
     350        }
     351    }
     352
     353    if (is_oldtable_malloced)
     354        PyMem_DEL(oldtable);
     355    return 0;
    357356}
    358357
     
    362361set_add_entry(register PySetObject *so, setentry *entry)
    363362{
    364         register Py_ssize_t n_used;
    365 
    366         assert(so->fill <= so->mask);  /* at least one empty slot */
    367         n_used = so->used;
    368         Py_INCREF(entry->key);
    369         if (set_insert_key(so, entry->key, entry->hash) == -1) {
    370                 Py_DECREF(entry->key);
    371                 return -1;
    372         }
    373         if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
    374                 return 0;
    375         return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
     363    register Py_ssize_t n_used;
     364    PyObject *key = entry->key;
     365    long hash = entry->hash;
     366
     367    assert(so->fill <= so->mask);  /* at least one empty slot */
     368    n_used = so->used;
     369    Py_INCREF(key);
     370    if (set_insert_key(so, key, hash) == -1) {
     371        Py_DECREF(key);
     372        return -1;
     373    }
     374    if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
     375        return 0;
     376    return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
    376377}
    377378
     
    379380set_add_key(register PySetObject *so, PyObject *key)
    380381{
    381         register long hash;
    382         register Py_ssize_t n_used;
    383 
    384         if (!PyString_CheckExact(key) ||
    385             (hash = ((PyStringObject *) key)->ob_shash) == -1) {
    386                 hash = PyObject_Hash(key);
    387                 if (hash == -1)
    388                         return -1;
    389         }
    390         assert(so->fill <= so->mask);  /* at least one empty slot */
    391         n_used = so->used;
    392         Py_INCREF(key);
    393         if (set_insert_key(so, key, hash) == -1) {
    394                 Py_DECREF(key);
    395                 return -1;
    396         }
    397         if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
    398                 return 0;
    399         return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
     382    register long hash;
     383    register Py_ssize_t n_used;
     384
     385    if (!PyString_CheckExact(key) ||
     386        (hash = ((PyStringObject *) key)->ob_shash) == -1) {
     387        hash = PyObject_Hash(key);
     388        if (hash == -1)
     389            return -1;
     390    }
     391    assert(so->fill <= so->mask);  /* at least one empty slot */
     392    n_used = so->used;
     393    Py_INCREF(key);
     394    if (set_insert_key(so, key, hash) == -1) {
     395        Py_DECREF(key);
     396        return -1;
     397    }
     398    if (!(so->used > n_used && so->fill*3 >= (so->mask+1)*2))
     399        return 0;
     400    return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
    400401}
    401402
     
    405406static int
    406407set_discard_entry(PySetObject *so, setentry *oldentry)
    407 {       register setentry *entry;
    408         PyObject *old_key;
    409 
    410         entry = (so->lookup)(so, oldentry->key, oldentry->hash);
    411         if (entry == NULL)
    412                 return -1;
    413         if (entry->key == NULL  ||  entry->key == dummy)
    414                 return DISCARD_NOTFOUND;
    415         old_key = entry->key;
    416         Py_INCREF(dummy);
    417         entry->key = dummy;
    418         so->used--;
    419         Py_DECREF(old_key);
    420         return DISCARD_FOUND;
     408{       register setentry *entry;
     409    PyObject *old_key;
     410
     411    entry = (so->lookup)(so, oldentry->key, oldentry->hash);
     412    if (entry == NULL)
     413        return -1;
     414    if (entry->key == NULL  ||  entry->key == dummy)
     415        return DISCARD_NOTFOUND;
     416    old_key = entry->key;
     417    Py_INCREF(dummy);
     418    entry->key = dummy;
     419    so->used--;
     420    Py_DECREF(old_key);
     421    return DISCARD_FOUND;
    421422}
    422423
     
    424425set_discard_key(PySetObject *so, PyObject *key)
    425426{
    426         register long hash;
    427         register setentry *entry;
    428         PyObject *old_key;
    429 
    430         assert (PyAnySet_Check(so));
    431         if (!PyString_CheckExact(key) ||
    432             (hash = ((PyStringObject *) key)->ob_shash) == -1) {
    433                 hash = PyObject_Hash(key);
    434                 if (hash == -1)
    435                         return -1;
    436         }
    437         entry = (so->lookup)(so, key, hash);
    438         if (entry == NULL)
    439                 return -1;
    440         if (entry->key == NULL  ||  entry->key == dummy)
    441                 return DISCARD_NOTFOUND;
    442         old_key = entry->key;
    443         Py_INCREF(dummy);
    444         entry->key = dummy;
    445         so->used--;
    446         Py_DECREF(old_key);
    447         return DISCARD_FOUND;
     427    register long hash;
     428    register setentry *entry;
     429    PyObject *old_key;
     430
     431    assert (PyAnySet_Check(so));
     432    if (!PyString_CheckExact(key) ||
     433        (hash = ((PyStringObject *) key)->ob_shash) == -1) {
     434        hash = PyObject_Hash(key);
     435        if (hash == -1)
     436            return -1;
     437    }
     438    entry = (so->lookup)(so, key, hash);
     439    if (entry == NULL)
     440        return -1;
     441    if (entry->key == NULL  ||  entry->key == dummy)
     442        return DISCARD_NOTFOUND;
     443    old_key = entry->key;
     444    Py_INCREF(dummy);
     445    entry->key = dummy;
     446    so->used--;
     447    Py_DECREF(old_key);
     448    return DISCARD_FOUND;
    448449}
    449450
     
    451452set_clear_internal(PySetObject *so)
    452453{
    453         setentry *entry, *table;
    454         int table_is_malloced;
    455         Py_ssize_t fill;
    456         setentry small_copy[PySet_MINSIZE];
     454    setentry *entry, *table;
     455    int table_is_malloced;
     456    Py_ssize_t fill;
     457    setentry small_copy[PySet_MINSIZE];
    457458#ifdef Py_DEBUG
    458         Py_ssize_t i, n;
    459         assert (PyAnySet_Check(so));
    460 
    461         n = so->mask + 1;
    462         i = 0;
     459    Py_ssize_t i, n;
     460    assert (PyAnySet_Check(so));
     461
     462    n = so->mask + 1;
     463    i = 0;
    463464#endif
    464465
    465         table = so->table;
    466         assert(table != NULL);
    467         table_is_malloced = table != so->smalltable;
    468 
    469         /* This is delicate.  During the process of clearing the set,
    470         * decrefs can cause the set to mutate.  To avoid fatal confusion
    471         * (voice of experience), we have to make the set empty before
    472         * clearing the slots, and never refer to anything via so->ref while
    473         * clearing.
    474         */
    475         fill = so->fill;
    476         if (table_is_malloced)
    477                 EMPTY_TO_MINSIZE(so);
    478 
    479         else if (fill > 0) {
    480                 /* It's a small table with something that needs to be cleared.
    481                 * Afraid the only safe way is to copy the set entries into
    482                 * another small table first.
    483                 */
    484                 memcpy(small_copy, table, sizeof(small_copy));
    485                 table = small_copy;
    486                 EMPTY_TO_MINSIZE(so);
    487         }
    488         /* else it's a small table that's already empty */
    489 
    490         /* Now we can finally clear things.  If C had refcounts, we could
    491         * assert that the refcount on table is 1 now, i.e. that this function
    492         * has unique access to it, so decref side-effects can't alter it.
    493         */
    494         for (entry = table; fill > 0; ++entry) {
     466    table = so->table;
     467    assert(table != NULL);
     468    table_is_malloced = table != so->smalltable;
     469
     470    /* This is delicate.  During the process of clearing the set,
     471    * decrefs can cause the set to mutate.  To avoid fatal confusion
     472    * (voice of experience), we have to make the set empty before
     473    * clearing the slots, and never refer to anything via so->ref while
     474    * clearing.
     475    */
     476    fill = so->fill;
     477    if (table_is_malloced)
     478        EMPTY_TO_MINSIZE(so);
     479
     480    else if (fill > 0) {
     481        /* It's a small table with something that needs to be cleared.
     482        * Afraid the only safe way is to copy the set entries into
     483        * another small table first.
     484        */
     485        memcpy(small_copy, table, sizeof(small_copy));
     486        table = small_copy;
     487        EMPTY_TO_MINSIZE(so);
     488    }
     489    /* else it's a small table that's already empty */
     490
     491    /* Now we can finally clear things.  If C had refcounts, we could
     492    * assert that the refcount on table is 1 now, i.e. that this function
     493    * has unique access to it, so decref side-effects can't alter it.
     494    */
     495    for (entry = table; fill > 0; ++entry) {
    495496#ifdef Py_DEBUG
    496                 assert(i < n);
    497                 ++i;
     497        assert(i < n);
     498        ++i;
    498499#endif
    499                 if (entry->key) {
    500                         --fill;
    501                         Py_DECREF(entry->key);
    502                 }
     500        if (entry->key) {
     501            --fill;
     502            Py_DECREF(entry->key);
     503        }
    503504#ifdef Py_DEBUG
    504                 else
    505                         assert(entry->key == NULL);
     505        else
     506            assert(entry->key == NULL);
    506507#endif
    507         }
    508 
    509         if (table_is_malloced)
    510                 PyMem_DEL(table);
    511         return 0;
     508    }
     509
     510    if (table_is_malloced)
     511        PyMem_DEL(table);
     512    return 0;
    512513}
    513514
     
    523524 *
    524525 * CAUTION:  In general, it isn't safe to use set_next in a loop that
    525  * mutates the table. 
     526 * mutates the table.
    526527 */
    527528static int
    528529set_next(PySetObject *so, Py_ssize_t *pos_ptr, setentry **entry_ptr)
    529530{
    530         Py_ssize_t i;
    531         Py_ssize_t mask;
    532         register setentry *table;
    533 
    534         assert (PyAnySet_Check(so));
    535         i = *pos_ptr;
    536         assert(i >= 0);
    537         table = so->table;
    538         mask = so->mask;
    539         while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
    540                 i++;
    541         *pos_ptr = i+1;
    542         if (i > mask)
    543                 return 0;
    544         assert(table[i].key != NULL);
    545         *entry_ptr = &table[i];
    546         return 1;
     531    Py_ssize_t i;
     532    Py_ssize_t mask;
     533    register setentry *table;
     534
     535    assert (PyAnySet_Check(so));
     536    i = *pos_ptr;
     537    assert(i >= 0);
     538    table = so->table;
     539    mask = so->mask;
     540    while (i <= mask && (table[i].key == NULL || table[i].key == dummy))
     541        i++;
     542    *pos_ptr = i+1;
     543    if (i > mask)
     544        return 0;
     545    assert(table[i].key != NULL);
     546    *entry_ptr = &table[i];
     547    return 1;
    547548}
    548549
     
    550551set_dealloc(PySetObject *so)
    551552{
    552         register setentry *entry;
    553         Py_ssize_t fill = so->fill;
    554         PyObject_GC_UnTrack(so);
    555         Py_TRASHCAN_SAFE_BEGIN(so)
    556         if (so->weakreflist != NULL)
    557                 PyObject_ClearWeakRefs((PyObject *) so);
    558 
    559         for (entry = so->table; fill > 0; entry++) {
    560                 if (entry->key) {
    561                         --fill;
    562                         Py_DECREF(entry->key);
    563                 }
    564         }
    565         if (so->table != so->smalltable)
    566                 PyMem_DEL(so->table);
    567         if (numfree < PySet_MAXFREELIST && PyAnySet_CheckExact(so))
    568                 free_list[numfree++] = so;
    569         else
    570                 Py_TYPE(so)->tp_free(so);
    571         Py_TRASHCAN_SAFE_END(so)
     553    register setentry *entry;
     554    Py_ssize_t fill = so->fill;
     555    PyObject_GC_UnTrack(so);
     556    Py_TRASHCAN_SAFE_BEGIN(so)
     557    if (so->weakreflist != NULL)
     558        PyObject_ClearWeakRefs((PyObject *) so);
     559
     560    for (entry = so->table; fill > 0; entry++) {
     561        if (entry->key) {
     562            --fill;
     563            Py_DECREF(entry->key);
     564        }
     565    }
     566    if (so->table != so->smalltable)
     567        PyMem_DEL(so->table);
     568    if (numfree < PySet_MAXFREELIST && PyAnySet_CheckExact(so))
     569        free_list[numfree++] = so;
     570    else
     571        Py_TYPE(so)->tp_free(so);
     572    Py_TRASHCAN_SAFE_END(so)
    572573}
    573574
     
    575576set_tp_print(PySetObject *so, FILE *fp, int flags)
    576577{
    577         setentry *entry;
    578         Py_ssize_t pos=0;
    579         char *emit = "";        /* No separator emitted on first pass */
    580         char *separator = ", ";
    581         int status = Py_ReprEnter((PyObject*)so);
    582 
    583         if (status != 0) {
    584                 if (status < 0)
    585                         return status;
    586                 Py_BEGIN_ALLOW_THREADS
    587                 fprintf(fp, "%s(...)", so->ob_type->tp_name);
    588                 Py_END_ALLOW_THREADS
    589                 return 0;
    590         }       
    591 
    592         Py_BEGIN_ALLOW_THREADS
    593         fprintf(fp, "%s([", so->ob_type->tp_name);
    594         Py_END_ALLOW_THREADS
    595         while (set_next(so, &pos, &entry)) {
    596                 Py_BEGIN_ALLOW_THREADS
    597                 fputs(emit, fp);
    598                 Py_END_ALLOW_THREADS
    599                 emit = separator;
    600                 if (PyObject_Print(entry->key, fp, 0) != 0) {
    601                         Py_ReprLeave((PyObject*)so);
    602                         return -1;
    603                 }
    604         }
    605         Py_BEGIN_ALLOW_THREADS
    606         fputs("])", fp);
    607         Py_END_ALLOW_THREADS
    608         Py_ReprLeave((PyObject*)so);       
    609         return 0;
     578    setentry *entry;
     579    Py_ssize_t pos=0;
     580    char *emit = "";            /* No separator emitted on first pass */
     581    char *separator = ", ";
     582    int status = Py_ReprEnter((PyObject*)so);
     583
     584    if (status != 0) {
     585        if (status < 0)
     586            return status;
     587        Py_BEGIN_ALLOW_THREADS
     588        fprintf(fp, "%s(...)", so->ob_type->tp_name);
     589        Py_END_ALLOW_THREADS
     590        return 0;
     591    }
     592
     593    Py_BEGIN_ALLOW_THREADS
     594    fprintf(fp, "%s([", so->ob_type->tp_name);
     595    Py_END_ALLOW_THREADS
     596    while (set_next(so, &pos, &entry)) {
     597        Py_BEGIN_ALLOW_THREADS
     598        fputs(emit, fp);
     599        Py_END_ALLOW_THREADS
     600        emit = separator;
     601        if (PyObject_Print(entry->key, fp, 0) != 0) {
     602            Py_ReprLeave((PyObject*)so);
     603            return -1;
     604        }
     605    }
     606    Py_BEGIN_ALLOW_THREADS
     607    fputs("])", fp);
     608    Py_END_ALLOW_THREADS
     609    Py_ReprLeave((PyObject*)so);
     610    return 0;
    610611}
    611612
     
    613614set_repr(PySetObject *so)
    614615{
    615         PyObject *keys, *result=NULL, *listrepr;
    616         int status = Py_ReprEnter((PyObject*)so);
    617 
    618         if (status != 0) {
    619                 if (status < 0)
    620                         return NULL;
    621                 return PyString_FromFormat("%s(...)", so->ob_type->tp_name);
    622         }
    623 
    624         keys = PySequence_List((PyObject *)so);
    625         if (keys == NULL)
    626                 goto done;
    627         listrepr = PyObject_Repr(keys);
    628         Py_DECREF(keys);
    629         if (listrepr == NULL)
    630                 goto done;
    631 
    632         result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
    633                 PyString_AS_STRING(listrepr));
    634         Py_DECREF(listrepr);
     616    PyObject *keys, *result=NULL, *listrepr;
     617    int status = Py_ReprEnter((PyObject*)so);
     618
     619    if (status != 0) {
     620        if (status < 0)
     621            return NULL;
     622        return PyString_FromFormat("%s(...)", so->ob_type->tp_name);
     623    }
     624
     625    keys = PySequence_List((PyObject *)so);
     626    if (keys == NULL)
     627        goto done;
     628    listrepr = PyObject_Repr(keys);
     629    Py_DECREF(keys);
     630    if (listrepr == NULL)
     631        goto done;
     632
     633    result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
     634        PyString_AS_STRING(listrepr));
     635    Py_DECREF(listrepr);
    635636done:
    636         Py_ReprLeave((PyObject*)so);
    637         return result;
     637    Py_ReprLeave((PyObject*)so);
     638    return result;
    638639}
    639640
     
    641642set_len(PyObject *so)
    642643{
    643         return ((PySetObject *)so)->used;
     644    return ((PySetObject *)so)->used;
    644645}
    645646
     
    647648set_merge(PySetObject *so, PyObject *otherset)
    648649{
    649         PySetObject *other;
    650         register Py_ssize_t i;
    651         register setentry *entry;
    652 
    653         assert (PyAnySet_Check(so));
    654         assert (PyAnySet_Check(otherset));
    655 
    656         other = (PySetObject*)otherset;
    657         if (other == so || other->used == 0)
    658                 /* a.update(a) or a.update({}); nothing to do */
    659                 return 0;
    660         /* Do one big resize at the start, rather than
    661          * incrementally resizing as we insert new keys.  Expect
    662          * that there will be no (or few) overlapping keys.
    663          */
    664         if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
    665            if (set_table_resize(so, (so->used + other->used)*2) != 0)
    666                    return -1;
    667         }
    668         for (i = 0; i <= other->mask; i++) {
    669                 entry = &other->table[i];
    670                 if (entry->key != NULL &&
    671                     entry->key != dummy) {
    672                         Py_INCREF(entry->key);
    673                         if (set_insert_key(so, entry->key, entry->hash) == -1) {
    674                                 Py_DECREF(entry->key);
    675                                 return -1;
    676                         }
    677                 }
    678         }
    679         return 0;
     650    PySetObject *other;
     651    PyObject *key;
     652    long hash;
     653    register Py_ssize_t i;
     654    register setentry *entry;
     655
     656    assert (PyAnySet_Check(so));
     657    assert (PyAnySet_Check(otherset));
     658
     659    other = (PySetObject*)otherset;
     660    if (other == so || other->used == 0)
     661        /* a.update(a) or a.update({}); nothing to do */
     662        return 0;
     663    /* Do one big resize at the start, rather than
     664     * incrementally resizing as we insert new keys.  Expect
     665     * that there will be no (or few) overlapping keys.
     666     */
     667    if ((so->fill + other->used)*3 >= (so->mask+1)*2) {
     668       if (set_table_resize(so, (so->used + other->used)*2) != 0)
     669           return -1;
     670    }
     671    for (i = 0; i <= other->mask; i++) {
     672        entry = &other->table[i];
     673        key = entry->key;
     674        hash = entry->hash;
     675        if (key != NULL &&
     676            key != dummy) {
     677            Py_INCREF(key);
     678            if (set_insert_key(so, key, hash) == -1) {
     679                Py_DECREF(key);
     680                return -1;
     681            }
     682        }
     683    }
     684    return 0;
    680685}
    681686
     
    683688set_contains_key(PySetObject *so, PyObject *key)
    684689{
    685         long hash;
    686         setentry *entry;
    687 
    688         if (!PyString_CheckExact(key) ||
    689             (hash = ((PyStringObject *) key)->ob_shash) == -1) {
    690                 hash = PyObject_Hash(key);
    691                 if (hash == -1)
    692                         return -1;
    693         }
    694         entry = (so->lookup)(so, key, hash);
    695         if (entry == NULL)
    696                 return -1;
    697         key = entry->key;
    698         return key != NULL && key != dummy;
     690    long hash;
     691    setentry *entry;
     692
     693    if (!PyString_CheckExact(key) ||
     694        (hash = ((PyStringObject *) key)->ob_shash) == -1) {
     695        hash = PyObject_Hash(key);
     696        if (hash == -1)
     697            return -1;
     698    }
     699    entry = (so->lookup)(so, key, hash);
     700    if (entry == NULL)
     701        return -1;
     702    key = entry->key;
     703    return key != NULL && key != dummy;
    699704}
    700705
     
    702707set_contains_entry(PySetObject *so, setentry *entry)
    703708{
    704         PyObject *key;
    705         setentry *lu_entry;
    706 
    707         lu_entry = (so->lookup)(so, entry->key, entry->hash);
    708         if (lu_entry == NULL)
    709                 return -1;
    710         key = lu_entry->key;
    711         return key != NULL && key != dummy;
     709    PyObject *key;
     710    setentry *lu_entry;
     711
     712    lu_entry = (so->lookup)(so, entry->key, entry->hash);
     713    if (lu_entry == NULL)
     714        return -1;
     715    key = lu_entry->key;
     716    return key != NULL && key != dummy;
    712717}
    713718
     
    715720set_pop(PySetObject *so)
    716721{
    717         register Py_ssize_t i = 0;
    718         register setentry *entry;
    719         PyObject *key;
    720 
    721         assert (PyAnySet_Check(so));
    722         if (so->used == 0) {
    723                 PyErr_SetString(PyExc_KeyError, "pop from an empty set");
    724                 return NULL;
    725         }
    726 
    727         /* Set entry to "the first" unused or dummy set entry.  We abuse
    728         * the hash field of slot 0 to hold a search finger:
    729         * If slot 0 has a value, use slot 0.
    730         * Else slot 0 is being used to hold a search finger,
    731         * and we use its hash value as the first index to look.
    732         */
    733         entry = &so->table[0];
    734         if (entry->key == NULL || entry->key == dummy) {
    735                 i = entry->hash;
    736                 /* The hash field may be a real hash value, or it may be a
    737                 * legit search finger, or it may be a once-legit search
    738                 * finger that's out of bounds now because it wrapped around
    739                 * or the table shrunk -- simply make sure it's in bounds now.
    740                 */
    741                 if (i > so->mask || i < 1)
    742                         i = 1;  /* skip slot 0 */
    743                 while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
    744                         i++;
    745                         if (i > so->mask)
    746                                 i = 1;
    747                 }
    748         }
    749         key = entry->key;
    750         Py_INCREF(dummy);
    751         entry->key = dummy;
    752         so->used--;
    753         so->table[0].hash = i + 1;  /* next place to start */
    754         return key;
     722    register Py_ssize_t i = 0;
     723    register setentry *entry;
     724    PyObject *key;
     725
     726    assert (PyAnySet_Check(so));
     727    if (so->used == 0) {
     728        PyErr_SetString(PyExc_KeyError, "pop from an empty set");
     729        return NULL;
     730    }
     731
     732    /* Set entry to "the first" unused or dummy set entry.  We abuse
     733    * the hash field of slot 0 to hold a search finger:
     734    * If slot 0 has a value, use slot 0.
     735    * Else slot 0 is being used to hold a search finger,
     736    * and we use its hash value as the first index to look.
     737    */
     738    entry = &so->table[0];
     739    if (entry->key == NULL || entry->key == dummy) {
     740        i = entry->hash;
     741        /* The hash field may be a real hash value, or it may be a
     742        * legit search finger, or it may be a once-legit search
     743        * finger that's out of bounds now because it wrapped around
     744        * or the table shrunk -- simply make sure it's in bounds now.
     745        */
     746        if (i > so->mask || i < 1)
     747            i = 1;              /* skip slot 0 */
     748        while ((entry = &so->table[i])->key == NULL || entry->key==dummy) {
     749            i++;
     750            if (i > so->mask)
     751                i = 1;
     752        }
     753    }
     754    key = entry->key;
     755    Py_INCREF(dummy);
     756    entry->key = dummy;
     757    so->used--;
     758    so->table[0].hash = i + 1;  /* next place to start */
     759    return key;
    755760}
    756761
     
    761766set_traverse(PySetObject *so, visitproc visit, void *arg)
    762767{
    763         Py_ssize_t pos = 0;
    764         setentry *entry;
    765 
    766         while (set_next(so, &pos, &entry))
    767                 Py_VISIT(entry->key);
    768         return 0;
     768    Py_ssize_t pos = 0;
     769    setentry *entry;
     770
     771    while (set_next(so, &pos, &entry))
     772        Py_VISIT(entry->key);
     773    return 0;
    769774}
    770775
     
    772777frozenset_hash(PyObject *self)
    773778{
    774         PySetObject *so = (PySetObject *)self;
    775         long h, hash = 1927868237L;
    776         setentry *entry;
    777         Py_ssize_t pos = 0;
    778 
    779         if (so->hash != -1)
    780                 return so->hash;
    781 
    782         hash *= PySet_GET_SIZE(self) + 1;
    783         while (set_next(so, &pos, &entry)) {
    784                 /* Work to increase the bit dispersion for closely spaced hash
    785                    values.  The is important because some use cases have many
    786                    combinations of a small number of elements with nearby
    787                    hashes so that many distinct combinations collapse to only
    788                    a handful of distinct hash values. */
    789                 h = entry->hash;
    790                 hash ^= (h ^ (h << 16) ^ 89869747L)  * 3644798167u;
    791         }
    792         hash = hash * 69069L + 907133923L;
    793         if (hash == -1)
    794                 hash = 590923713L;
    795         so->hash = hash;
    796         return hash;
     779    PySetObject *so = (PySetObject *)self;
     780    long h, hash = 1927868237L;
     781    setentry *entry;
     782    Py_ssize_t pos = 0;
     783
     784    if (so->hash != -1)
     785        return so->hash;
     786
     787    hash *= PySet_GET_SIZE(self) + 1;
     788    while (set_next(so, &pos, &entry)) {
     789        /* Work to increase the bit dispersion for closely spaced hash
     790           values.  The is important because some use cases have many
     791           combinations of a small number of elements with nearby
     792           hashes so that many distinct combinations collapse to only
     793           a handful of distinct hash values. */
     794        h = entry->hash;
     795        hash ^= (h ^ (h << 16) ^ 89869747L)  * 3644798167u;
     796    }
     797    hash = hash * 69069L + 907133923L;
     798    if (hash == -1)
     799        hash = 590923713L;
     800    so->hash = hash;
     801    return hash;
    797802}
    798803
     
    800805
    801806typedef struct {
    802         PyObject_HEAD
    803         PySetObject *si_set; /* Set to NULL when iterator is exhausted */
    804         Py_ssize_t si_used;
    805         Py_ssize_t si_pos;
    806         Py_ssize_t len;
     807    PyObject_HEAD
     808    PySetObject *si_set; /* Set to NULL when iterator is exhausted */
     809    Py_ssize_t si_used;
     810    Py_ssize_t si_pos;
     811    Py_ssize_t len;
    807812} setiterobject;
    808813
     
    810815setiter_dealloc(setiterobject *si)
    811816{
    812         Py_XDECREF(si->si_set);
    813         PyObject_GC_Del(si);
     817    Py_XDECREF(si->si_set);
     818    PyObject_GC_Del(si);
    814819}
    815820
     
    817822setiter_traverse(setiterobject *si, visitproc visit, void *arg)
    818823{
    819         Py_VISIT(si->si_set);
    820         return 0;
     824    Py_VISIT(si->si_set);
     825    return 0;
    821826}
    822827
     
    824829setiter_len(setiterobject *si)
    825830{
    826         Py_ssize_t len = 0;
    827         if (si->si_set != NULL && si->si_used == si->si_set->used)
    828                 len = si->len;
    829         return PyInt_FromLong(len);
     831    Py_ssize_t len = 0;
     832    if (si->si_set != NULL && si->si_used == si->si_set->used)
     833        len = si->len;
     834    return PyInt_FromLong(len);
    830835}
    831836
     
    833838
    834839static PyMethodDef setiter_methods[] = {
    835         {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
    836         {NULL,          NULL}           /* sentinel */
     840    {"__length_hint__", (PyCFunction)setiter_len, METH_NOARGS, length_hint_doc},
     841    {NULL,              NULL}           /* sentinel */
    837842};
    838843
    839844static PyObject *setiter_iternext(setiterobject *si)
    840845{
    841         PyObject *key;
    842         register Py_ssize_t i, mask;
    843         register setentry *entry;
    844         PySetObject *so = si->si_set;
    845 
    846         if (so == NULL)
    847                 return NULL;
    848         assert (PyAnySet_Check(so));
    849 
    850         if (si->si_used != so->used) {
    851                 PyErr_SetString(PyExc_RuntimeError,
    852                                 "Set changed size during iteration");
    853                 si->si_used = -1; /* Make this state sticky */
    854                 return NULL;
    855         }
    856 
    857         i = si->si_pos;
    858         assert(i>=0);
    859         entry = so->table;
    860         mask = so->mask;
    861         while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
    862                 i++;
    863         si->si_pos = i+1;
    864         if (i > mask)
    865                 goto fail;
    866         si->len--;
    867         key = entry[i].key;
    868         Py_INCREF(key);
    869         return key;
     846    PyObject *key;
     847    register Py_ssize_t i, mask;
     848    register setentry *entry;
     849    PySetObject *so = si->si_set;
     850
     851    if (so == NULL)
     852        return NULL;
     853    assert (PyAnySet_Check(so));
     854
     855    if (si->si_used != so->used) {
     856        PyErr_SetString(PyExc_RuntimeError,
     857                        "Set changed size during iteration");
     858        si->si_used = -1; /* Make this state sticky */
     859        return NULL;
     860    }
     861
     862    i = si->si_pos;
     863    assert(i>=0);
     864    entry = so->table;
     865    mask = so->mask;
     866    while (i <= mask && (entry[i].key == NULL || entry[i].key == dummy))
     867        i++;
     868    si->si_pos = i+1;
     869    if (i > mask)
     870        goto fail;
     871    si->len--;
     872    key = entry[i].key;
     873    Py_INCREF(key);
     874    return key;
    870875
    871876fail:
    872         Py_DECREF(so);
    873         si->si_set = NULL;
    874         return NULL;
     877    Py_DECREF(so);
     878    si->si_set = NULL;
     879    return NULL;
    875880}
    876881
    877882static PyTypeObject PySetIter_Type = {
    878         PyVarObject_HEAD_INIT(&PyType_Type, 0)
    879         "setiterator",                          /* tp_name */
    880         sizeof(setiterobject),                  /* tp_basicsize */
    881         0,                                      /* tp_itemsize */
    882         /* methods */
    883         (destructor)setiter_dealloc,            /* tp_dealloc */
    884         0,                                      /* tp_print */
    885         0,                                      /* tp_getattr */
    886         0,                                      /* tp_setattr */
    887         0,                                      /* tp_compare */
    888         0,                                      /* tp_repr */
    889         0,                                      /* tp_as_number */
    890         0,                                      /* tp_as_sequence */
    891         0,                                      /* tp_as_mapping */
    892         0,                                      /* tp_hash */
    893         0,                                      /* tp_call */
    894         0,                                      /* tp_str */
    895         PyObject_GenericGetAttr,                /* tp_getattro */
    896         0,                                      /* tp_setattro */
    897         0,                                      /* tp_as_buffer */
    898         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
    899         0,                                      /* tp_doc */
    900         (traverseproc)setiter_traverse,         /* tp_traverse */
    901         0,                                      /* tp_clear */
    902         0,                                      /* tp_richcompare */
    903         0,                                      /* tp_weaklistoffset */
    904         PyObject_SelfIter,                      /* tp_iter */
    905         (iternextfunc)setiter_iternext,         /* tp_iternext */
    906         setiter_methods,                        /* tp_methods */
    907         0,
     883    PyVarObject_HEAD_INIT(&PyType_Type, 0)
     884    "setiterator",                              /* tp_name */
     885    sizeof(setiterobject),                      /* tp_basicsize */
     886    0,                                          /* tp_itemsize */
     887    /* methods */
     888    (destructor)setiter_dealloc,                /* tp_dealloc */
     889    0,                                          /* tp_print */
     890    0,                                          /* tp_getattr */
     891    0,                                          /* tp_setattr */
     892    0,                                          /* tp_compare */
     893    0,                                          /* tp_repr */
     894    0,                                          /* tp_as_number */
     895    0,                                          /* tp_as_sequence */
     896    0,                                          /* tp_as_mapping */
     897    0,                                          /* tp_hash */
     898    0,                                          /* tp_call */
     899    0,                                          /* tp_str */
     900    PyObject_GenericGetAttr,                    /* tp_getattro */
     901    0,                                          /* tp_setattro */
     902    0,                                          /* tp_as_buffer */
     903    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
     904    0,                                          /* tp_doc */
     905    (traverseproc)setiter_traverse,             /* tp_traverse */
     906    0,                                          /* tp_clear */
     907    0,                                          /* tp_richcompare */
     908    0,                                          /* tp_weaklistoffset */
     909    PyObject_SelfIter,                          /* tp_iter */
     910    (iternextfunc)setiter_iternext,             /* tp_iternext */
     911    setiter_methods,                            /* tp_methods */
     912    0,
    908913};
    909914
     
    911916set_iter(PySetObject *so)
    912917{
    913         setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
    914         if (si == NULL)
    915                 return NULL;
    916         Py_INCREF(so);
    917         si->si_set = so;
    918         si->si_used = so->used;
    919         si->si_pos = 0;
    920         si->len = so->used;
    921         _PyObject_GC_TRACK(si);
    922         return (PyObject *)si;
     918    setiterobject *si = PyObject_GC_New(setiterobject, &PySetIter_Type);
     919    if (si == NULL)
     920        return NULL;
     921    Py_INCREF(so);
     922    si->si_set = so;
     923    si->si_used = so->used;
     924    si->si_pos = 0;
     925    si->len = so->used;
     926    _PyObject_GC_TRACK(si);
     927    return (PyObject *)si;
    923928}
    924929
     
    926931set_update_internal(PySetObject *so, PyObject *other)
    927932{
    928         PyObject *key, *it;
    929 
    930         if (PyAnySet_Check(other))
    931                 return set_merge(so, other);
    932 
    933         if (PyDict_CheckExact(other)) {
    934                 PyObject *value;
    935                 Py_ssize_t pos = 0;
    936                 long hash;
    937                 Py_ssize_t dictsize = PyDict_Size(other);
    938 
    939                 /* Do one big resize at the start, rather than
    940                 * incrementally resizing as we insert new keys.  Expect
    941                 * that there will be no (or few) overlapping keys.
    942                 */
    943                 if (dictsize == -1)
    944                         return -1;
    945                 if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
    946                         if (set_table_resize(so, (so->used + dictsize)*2) != 0)
    947                                 return -1;
    948                 }
    949                 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
    950                         setentry an_entry;
    951 
    952                         an_entry.hash = hash;
    953                         an_entry.key = key;
    954                         if (set_add_entry(so, &an_entry) == -1)
    955                                 return -1;
    956                 }
    957                 return 0;
    958         }
    959 
    960         it = PyObject_GetIter(other);
    961         if (it == NULL)
    962                 return -1;
    963 
    964         while ((key = PyIter_Next(it)) != NULL) {
    965                 if (set_add_key(so, key) == -1) {
    966                         Py_DECREF(it);
    967                         Py_DECREF(key);
    968                         return -1;
    969                 }
    970                 Py_DECREF(key);
    971         }
    972         Py_DECREF(it);
    973         if (PyErr_Occurred())
    974                 return -1;
    975         return 0;
     933    PyObject *key, *it;
     934
     935    if (PyAnySet_Check(other))
     936        return set_merge(so, other);
     937
     938    if (PyDict_CheckExact(other)) {
     939        PyObject *value;
     940        Py_ssize_t pos = 0;
     941        long hash;
     942        Py_ssize_t dictsize = PyDict_Size(other);
     943
     944        /* Do one big resize at the start, rather than
     945        * incrementally resizing as we insert new keys.  Expect
     946        * that there will be no (or few) overlapping keys.
     947        */
     948        if (dictsize == -1)
     949            return -1;
     950        if ((so->fill + dictsize)*3 >= (so->mask+1)*2) {
     951            if (set_table_resize(so, (so->used + dictsize)*2) != 0)
     952                return -1;
     953        }
     954        while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
     955            setentry an_entry;
     956
     957            an_entry.hash = hash;
     958            an_entry.key = key;
     959            if (set_add_entry(so, &an_entry) == -1)
     960                return -1;
     961        }
     962        return 0;
     963    }
     964
     965    it = PyObject_GetIter(other);
     966    if (it == NULL)
     967        return -1;
     968
     969    while ((key = PyIter_Next(it)) != NULL) {
     970        if (set_add_key(so, key) == -1) {
     971            Py_DECREF(it);
     972            Py_DECREF(key);
     973            return -1;
     974        }
     975        Py_DECREF(key);
     976    }
     977    Py_DECREF(it);
     978    if (PyErr_Occurred())
     979        return -1;
     980    return 0;
    976981}
    977982
     
    979984set_update(PySetObject *so, PyObject *args)
    980985{
    981         Py_ssize_t i;
    982 
    983         for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
    984                 PyObject *other = PyTuple_GET_ITEM(args, i);
    985                 if (set_update_internal(so, other) == -1)
    986                         return NULL;
    987         }
    988         Py_RETURN_NONE;
    989 }
    990 
    991 PyDoc_STRVAR(update_doc, 
     986    Py_ssize_t i;
     987
     988    for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
     989        PyObject *other = PyTuple_GET_ITEM(args, i);
     990        if (set_update_internal(so, other) == -1)
     991            return NULL;
     992    }
     993    Py_RETURN_NONE;
     994}
     995
     996PyDoc_STRVAR(update_doc,
    992997"Update a set with the union of itself and others.");
    993998
     
    9951000make_new_set(PyTypeObject *type, PyObject *iterable)
    9961001{
    997         register PySetObject *so = NULL;
    998 
    999         if (dummy == NULL) { /* Auto-initialize dummy */
    1000                 dummy = PyString_FromString("<dummy key>");
    1001                 if (dummy == NULL)
    1002                         return NULL;
    1003         }
    1004 
    1005         /* create PySetObject structure */
    1006         if (numfree &&
    1007             (type == &PySet_Type  ||  type == &PyFrozenSet_Type)) {
    1008                 so = free_list[--numfree];
    1009                 assert (so != NULL && PyAnySet_CheckExact(so));
    1010                 Py_TYPE(so) = type;
    1011                 _Py_NewReference((PyObject *)so);
    1012                 EMPTY_TO_MINSIZE(so);
    1013                 PyObject_GC_Track(so);
    1014         } else {
    1015                 so = (PySetObject *)type->tp_alloc(type, 0);
    1016                 if (so == NULL)
    1017                         return NULL;
    1018                 /* tp_alloc has already zeroed the structure */
    1019                 assert(so->table == NULL && so->fill == 0 && so->used == 0);
    1020                 INIT_NONZERO_SET_SLOTS(so);
    1021         }
    1022 
    1023         so->lookup = set_lookkey_string;
    1024         so->weakreflist = NULL;
    1025 
    1026         if (iterable != NULL) {
    1027                 if (set_update_internal(so, iterable) == -1) {
    1028                         Py_DECREF(so);
    1029                         return NULL;
    1030                 }
    1031         }
    1032 
    1033         return (PyObject *)so;
     1002    register PySetObject *so = NULL;
     1003
     1004    if (dummy == NULL) { /* Auto-initialize dummy */
     1005        dummy = PyString_FromString("<dummy key>");
     1006        if (dummy == NULL)
     1007            return NULL;
     1008    }
     1009
     1010    /* create PySetObject structure */
     1011    if (numfree &&
     1012        (type == &PySet_Type  ||  type == &PyFrozenSet_Type)) {
     1013        so = free_list[--numfree];
     1014        assert (so != NULL && PyAnySet_CheckExact(so));
     1015        Py_TYPE(so) = type;
     1016        _Py_NewReference((PyObject *)so);
     1017        EMPTY_TO_MINSIZE(so);
     1018        PyObject_GC_Track(so);
     1019    } else {
     1020        so = (PySetObject *)type->tp_alloc(type, 0);
     1021        if (so == NULL)
     1022            return NULL;
     1023        /* tp_alloc has already zeroed the structure */
     1024        assert(so->table == NULL && so->fill == 0 && so->used == 0);
     1025        INIT_NONZERO_SET_SLOTS(so);
     1026    }
     1027
     1028    so->lookup = set_lookkey_string;
     1029    so->weakreflist = NULL;
     1030
     1031    if (iterable != NULL) {
     1032        if (set_update_internal(so, iterable) == -1) {
     1033            Py_DECREF(so);
     1034            return NULL;
     1035        }
     1036    }
     1037
     1038    return (PyObject *)so;
    10341039}
    10351040
     
    10401045frozenset_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
    10411046{
    1042         PyObject *iterable = NULL, *result;
    1043 
    1044         if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
    1045                 return NULL;
    1046 
    1047         if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
    1048                 return NULL;
    1049 
    1050         if (type != &PyFrozenSet_Type)
    1051                 return make_new_set(type, iterable);
    1052 
    1053         if (iterable != NULL) {
    1054                 /* frozenset(f) is idempotent */
    1055                 if (PyFrozenSet_CheckExact(iterable)) {
    1056                         Py_INCREF(iterable);
    1057                         return iterable;
    1058                 }
    1059                 result = make_new_set(type, iterable);
    1060                 if (result == NULL || PySet_GET_SIZE(result))
    1061                         return result;
    1062                 Py_DECREF(result);
    1063         }
    1064         /* The empty frozenset is a singleton */
    1065         if (emptyfrozenset == NULL)
    1066                 emptyfrozenset = make_new_set(type, NULL);
    1067         Py_XINCREF(emptyfrozenset);
    1068         return emptyfrozenset;
     1047    PyObject *iterable = NULL, *result;
     1048
     1049    if (type == &PyFrozenSet_Type && !_PyArg_NoKeywords("frozenset()", kwds))
     1050        return NULL;
     1051
     1052    if (!PyArg_UnpackTuple(args, type->tp_name, 0, 1, &iterable))
     1053        return NULL;
     1054
     1055    if (type != &PyFrozenSet_Type)
     1056        return make_new_set(type, iterable);
     1057
     1058    if (iterable != NULL) {
     1059        /* frozenset(f) is idempotent */
     1060        if (PyFrozenSet_CheckExact(iterable)) {
     1061            Py_INCREF(iterable);
     1062            return iterable;
     1063        }
     1064        result = make_new_set(type, iterable);
     1065        if (result == NULL || PySet_GET_SIZE(result))
     1066            return result;
     1067        Py_DECREF(result);
     1068    }
     1069    /* The empty frozenset is a singleton */
     1070    if (emptyfrozenset == NULL)
     1071        emptyfrozenset = make_new_set(type, NULL);
     1072    Py_XINCREF(emptyfrozenset);
     1073    return emptyfrozenset;
    10691074}
    10701075
     
    10721077PySet_Fini(void)
    10731078{
    1074         PySetObject *so;
    1075 
    1076         while (numfree) {
    1077                 numfree--;
    1078                 so = free_list[numfree];
    1079                 PyObject_GC_Del(so);
    1080         }
    1081         Py_CLEAR(dummy);
    1082         Py_CLEAR(emptyfrozenset);
     1079    PySetObject *so;
     1080
     1081    while (numfree) {
     1082        numfree--;
     1083        so = free_list[numfree];
     1084        PyObject_GC_Del(so);
     1085    }
     1086    Py_CLEAR(dummy);
     1087    Py_CLEAR(emptyfrozenset);
    10831088}
    10841089
     
    10861091set_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
    10871092{
    1088         if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
    1089                 return NULL;
    1090        
    1091         return make_new_set(type, NULL);
     1093    if (type == &PySet_Type && !_PyArg_NoKeywords("set()", kwds))
     1094        return NULL;
     1095
     1096    return make_new_set(type, NULL);
    10921097}
    10931098
     
    10991104
    11001105   The function always succeeds and it leaves both objects in a stable state.
    1101    Useful for creating temporary frozensets from sets for membership testing 
     1106   Useful for creating temporary frozensets from sets for membership testing
    11021107   in __contains__(), discard(), and remove().  Also useful for operations
    1103    that update in-place (by allowing an intermediate result to be swapped 
     1108   that update in-place (by allowing an intermediate result to be swapped
    11041109   into one of the original inputs).
    11051110*/
     
    11081113set_swap_bodies(PySetObject *a, PySetObject *b)
    11091114{
    1110         Py_ssize_t t;
    1111         setentry *u;
    1112         setentry *(*f)(PySetObject *so, PyObject *key, long hash);
    1113         setentry tab[PySet_MINSIZE];
    1114         long h;
    1115 
    1116         t = a->fill;     a->fill   = b->fill;        b->fill  = t;
    1117         t = a->used;     a->used   = b->used;        b->used  = t;
    1118         t = a->mask;     a->mask   = b->mask;        b->mask  = t;
    1119 
    1120         u = a->table;
    1121         if (a->table == a->smalltable)
    1122                 u = b->smalltable;
    1123         a->table  = b->table;
    1124         if (b->table == b->smalltable)
    1125                 a->table = a->smalltable;
    1126         b->table = u;
    1127 
    1128         f = a->lookup;   a->lookup = b->lookup;      b->lookup = f;
    1129 
    1130         if (a->table == a->smalltable || b->table == b->smalltable) {
    1131                 memcpy(tab, a->smalltable, sizeof(tab));
    1132                 memcpy(a->smalltable, b->smalltable, sizeof(tab));
    1133                 memcpy(b->smalltable, tab, sizeof(tab));
    1134         }
    1135 
    1136         if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type)  &&
    1137             PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
    1138                 h = a->hash;     a->hash = b->hash;  b->hash = h;
    1139         } else {
    1140                 a->hash = -1;
    1141                 b->hash = -1;
    1142         }
     1115    Py_ssize_t t;
     1116    setentry *u;
     1117    setentry *(*f)(PySetObject *so, PyObject *key, long hash);
     1118    setentry tab[PySet_MINSIZE];
     1119    long h;
     1120
     1121    t = a->fill;     a->fill   = b->fill;        b->fill  = t;
     1122    t = a->used;     a->used   = b->used;        b->used  = t;
     1123    t = a->mask;     a->mask   = b->mask;        b->mask  = t;
     1124
     1125    u = a->table;
     1126    if (a->table == a->smalltable)
     1127        u = b->smalltable;
     1128    a->table  = b->table;
     1129    if (b->table == b->smalltable)
     1130        a->table = a->smalltable;
     1131    b->table = u;
     1132
     1133    f = a->lookup;   a->lookup = b->lookup;      b->lookup = f;
     1134
     1135    if (a->table == a->smalltable || b->table == b->smalltable) {
     1136        memcpy(tab, a->smalltable, sizeof(tab));
     1137        memcpy(a->smalltable, b->smalltable, sizeof(tab));
     1138        memcpy(b->smalltable, tab, sizeof(tab));
     1139    }
     1140
     1141    if (PyType_IsSubtype(Py_TYPE(a), &PyFrozenSet_Type)  &&
     1142        PyType_IsSubtype(Py_TYPE(b), &PyFrozenSet_Type)) {
     1143        h = a->hash;     a->hash = b->hash;  b->hash = h;
     1144    } else {
     1145        a->hash = -1;
     1146        b->hash = -1;
     1147    }
    11431148}
    11441149
     
    11461151set_copy(PySetObject *so)
    11471152{
    1148         return make_new_set(Py_TYPE(so), (PyObject *)so);
     1153    return make_new_set(Py_TYPE(so), (PyObject *)so);
    11491154}
    11501155
     
    11521157frozenset_copy(PySetObject *so)
    11531158{
    1154         if (PyFrozenSet_CheckExact(so)) {
    1155                 Py_INCREF(so);
    1156                 return (PyObject *)so;
    1157         }
    1158         return set_copy(so);
     1159    if (PyFrozenSet_CheckExact(so)) {
     1160        Py_INCREF(so);
     1161        return (PyObject *)so;
     1162    }
     1163    return set_copy(so);
    11591164}
    11601165
     
    11641169set_clear(PySetObject *so)
    11651170{
    1166         set_clear_internal(so);
    1167         Py_RETURN_NONE;
     1171    set_clear_internal(so);
     1172    Py_RETURN_NONE;
    11681173}
    11691174
     
    11731178set_union(PySetObject *so, PyObject *args)
    11741179{
    1175         PySetObject *result;
    1176         PyObject *other;
    1177         Py_ssize_t i;
    1178 
    1179         result = (PySetObject *)set_copy(so);
    1180         if (result == NULL)
    1181                 return NULL;
    1182 
    1183         for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
    1184                 other = PyTuple_GET_ITEM(args, i);
    1185                 if ((PyObject *)so == other)
    1186                         continue;
    1187                 if (set_update_internal(result, other) == -1) {
    1188                         Py_DECREF(result);
    1189                         return NULL;
    1190                 }
    1191         }
    1192         return (PyObject *)result;
     1180    PySetObject *result;
     1181    PyObject *other;
     1182    Py_ssize_t i;
     1183
     1184    result = (PySetObject *)set_copy(so);
     1185    if (result == NULL)
     1186        return NULL;
     1187
     1188    for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
     1189        other = PyTuple_GET_ITEM(args, i);
     1190        if ((PyObject *)so == other)
     1191            continue;
     1192        if (set_update_internal(result, other) == -1) {
     1193            Py_DECREF(result);
     1194            return NULL;
     1195        }
     1196    }
     1197    return (PyObject *)result;
    11931198}
    11941199
     
    12011206set_or(PySetObject *so, PyObject *other)
    12021207{
    1203         PySetObject *result;
    1204 
    1205         if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
    1206                 Py_INCREF(Py_NotImplemented);
    1207                 return Py_NotImplemented;
    1208         }
    1209 
    1210         result = (PySetObject *)set_copy(so);
    1211         if (result == NULL)
    1212                 return NULL;
    1213         if ((PyObject *)so == other)
    1214                 return (PyObject *)result;
    1215         if (set_update_internal(result, other) == -1) {
    1216                 Py_DECREF(result);
    1217                 return NULL;
    1218         }
    1219         return (PyObject *)result;
     1208    PySetObject *result;
     1209
     1210    if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
     1211        Py_INCREF(Py_NotImplemented);
     1212        return Py_NotImplemented;
     1213    }
     1214
     1215    result = (PySetObject *)set_copy(so);
     1216    if (result == NULL)
     1217        return NULL;
     1218    if ((PyObject *)so == other)
     1219        return (PyObject *)result;
     1220    if (set_update_internal(result, other) == -1) {
     1221        Py_DECREF(result);
     1222        return NULL;
     1223    }
     1224    return (PyObject *)result;
    12201225}
    12211226
     
    12231228set_ior(PySetObject *so, PyObject *other)
    12241229{
    1225         if (!PyAnySet_Check(other)) {
    1226                 Py_INCREF(Py_NotImplemented);
    1227                 return Py_NotImplemented;
    1228         }
    1229         if (set_update_internal(so, other) == -1)
    1230                 return NULL;
    1231         Py_INCREF(so);
    1232         return (PyObject *)so;
     1230    if (!PyAnySet_Check(other)) {
     1231        Py_INCREF(Py_NotImplemented);
     1232        return Py_NotImplemented;
     1233    }
     1234    if (set_update_internal(so, other) == -1)
     1235        return NULL;
     1236    Py_INCREF(so);
     1237    return (PyObject *)so;
    12331238}
    12341239
     
    12361241set_intersection(PySetObject *so, PyObject *other)
    12371242{
    1238         PySetObject *result;
    1239         PyObject *key, *it, *tmp;
    1240 
    1241         if ((PyObject *)so == other)
    1242                 return set_copy(so);
    1243 
    1244         result = (PySetObject *)make_new_set(Py_TYPE(so), NULL);
    1245         if (result == NULL)
    1246                 return NULL;
    1247 
    1248         if (PyAnySet_Check(other)) {           
    1249                 Py_ssize_t pos = 0;
    1250                 setentry *entry;
    1251 
    1252                 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
    1253                         tmp = (PyObject *)so;
    1254                         so = (PySetObject *)other;
    1255                         other = tmp;
    1256                 }
    1257 
    1258                 while (set_next((PySetObject *)other, &pos, &entry)) {
    1259                         int rv = set_contains_entry(so, entry);
    1260                         if (rv == -1) {
    1261                                 Py_DECREF(result);
    1262                                 return NULL;
    1263                         }
    1264                         if (rv) {
    1265                                 if (set_add_entry(result, entry) == -1) {
    1266                                         Py_DECREF(result);
    1267                                         return NULL;
    1268                                 }
    1269                         }
    1270                 }
    1271                 return (PyObject *)result;
    1272         }
    1273 
    1274         it = PyObject_GetIter(other);
    1275         if (it == NULL) {
    1276                 Py_DECREF(result);
    1277                 return NULL;
    1278         }
    1279 
    1280         while ((key = PyIter_Next(it)) != NULL) {
    1281                 int rv;
    1282                 setentry entry;
    1283                 long hash = PyObject_Hash(key);
    1284 
    1285                 if (hash == -1) {
    1286                         Py_DECREF(it);
    1287                         Py_DECREF(result);
    1288                         Py_DECREF(key);
    1289                         return NULL;
    1290                 }
    1291                 entry.hash = hash;
    1292                 entry.key = key;
    1293                 rv = set_contains_entry(so, &entry);
    1294                 if (rv == -1) {
    1295                         Py_DECREF(it);
    1296                         Py_DECREF(result);
    1297                         Py_DECREF(key);
    1298                         return NULL;
    1299                 }
    1300                 if (rv) {
    1301                         if (set_add_entry(result, &entry) == -1) {
    1302                                 Py_DECREF(it);
    1303                                 Py_DECREF(result);
    1304                                 Py_DECREF(key);
    1305                                 return NULL;
    1306                         }
    1307                 }
    1308                 Py_DECREF(key);
    1309         }
    1310         Py_DECREF(it);
    1311         if (PyErr_Occurred()) {
    1312                 Py_DECREF(result);
    1313                 return NULL;
    1314         }
    1315         return (PyObject *)result;
     1243    PySetObject *result;
     1244    PyObject *key, *it, *tmp;
     1245
     1246    if ((PyObject *)so == other)
     1247        return set_copy(so);
     1248
     1249    result = (PySetObject *)make_new_set(Py_TYPE(so), NULL);
     1250    if (result == NULL)
     1251        return NULL;
     1252
     1253    if (PyAnySet_Check(other)) {
     1254        Py_ssize_t pos = 0;
     1255        setentry *entry;
     1256
     1257        if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
     1258            tmp = (PyObject *)so;
     1259            so = (PySetObject *)other;
     1260            other = tmp;
     1261        }
     1262
     1263        while (set_next((PySetObject *)other, &pos, &entry)) {
     1264            int rv = set_contains_entry(so, entry);
     1265            if (rv == -1) {
     1266                Py_DECREF(result);
     1267                return NULL;
     1268            }
     1269            if (rv) {
     1270                if (set_add_entry(result, entry) == -1) {
     1271                    Py_DECREF(result);
     1272                    return NULL;
     1273                }
     1274            }
     1275        }
     1276        return (PyObject *)result;
     1277    }
     1278
     1279    it = PyObject_GetIter(other);
     1280    if (it == NULL) {
     1281        Py_DECREF(result);
     1282        return NULL;
     1283    }
     1284
     1285    while ((key = PyIter_Next(it)) != NULL) {
     1286        int rv;
     1287        setentry entry;
     1288        long hash = PyObject_Hash(key);
     1289
     1290        if (hash == -1) {
     1291            Py_DECREF(it);
     1292            Py_DECREF(result);
     1293            Py_DECREF(key);
     1294            return NULL;
     1295        }
     1296        entry.hash = hash;
     1297        entry.key = key;
     1298        rv = set_contains_entry(so, &entry);
     1299        if (rv == -1) {
     1300            Py_DECREF(it);
     1301            Py_DECREF(result);
     1302            Py_DECREF(key);
     1303            return NULL;
     1304        }
     1305        if (rv) {
     1306            if (set_add_entry(result, &entry) == -1) {
     1307                Py_DECREF(it);
     1308                Py_DECREF(result);
     1309                Py_DECREF(key);
     1310                return NULL;
     1311            }
     1312        }
     1313        Py_DECREF(key);
     1314    }
     1315    Py_DECREF(it);
     1316    if (PyErr_Occurred()) {
     1317        Py_DECREF(result);
     1318        return NULL;
     1319    }
     1320    return (PyObject *)result;
    13161321}
    13171322
     
    13191324set_intersection_multi(PySetObject *so, PyObject *args)
    13201325{
    1321         Py_ssize_t i;
    1322         PyObject *result = (PyObject *)so;
    1323 
    1324         if (PyTuple_GET_SIZE(args) == 0)
    1325                 return set_copy(so);
    1326 
    1327         Py_INCREF(so);
    1328         for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
    1329                 PyObject *other = PyTuple_GET_ITEM(args, i);
    1330                 PyObject *newresult = set_intersection((PySetObject *)result, other);
    1331                 if (newresult == NULL) {
    1332                         Py_DECREF(result);
    1333                         return NULL;
    1334                 }
    1335                 Py_DECREF(result);
    1336                 result = newresult;
    1337         }
    1338         return result;
     1326    Py_ssize_t i;
     1327    PyObject *result = (PyObject *)so;
     1328
     1329    if (PyTuple_GET_SIZE(args) == 0)
     1330        return set_copy(so);
     1331
     1332    Py_INCREF(so);
     1333    for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
     1334        PyObject *other = PyTuple_GET_ITEM(args, i);
     1335        PyObject *newresult = set_intersection((PySetObject *)result, other);
     1336        if (newresult == NULL) {
     1337            Py_DECREF(result);
     1338            return NULL;
     1339        }
     1340        Py_DECREF(result);
     1341        result = newresult;
     1342    }
     1343    return result;
    13391344}
    13401345
     
    13471352set_intersection_update(PySetObject *so, PyObject *other)
    13481353{
    1349         PyObject *tmp;
    1350 
    1351         tmp = set_intersection(so, other);
    1352         if (tmp == NULL)
    1353                 return NULL;
    1354         set_swap_bodies(so, (PySetObject *)tmp);
    1355         Py_DECREF(tmp);
    1356         Py_RETURN_NONE;
     1354    PyObject *tmp;
     1355
     1356    tmp = set_intersection(so, other);
     1357    if (tmp == NULL)
     1358        return NULL;
     1359    set_swap_bodies(so, (PySetObject *)tmp);
     1360    Py_DECREF(tmp);
     1361    Py_RETURN_NONE;
    13571362}
    13581363
     
    13601365set_intersection_update_multi(PySetObject *so, PyObject *args)
    13611366{
    1362         PyObject *tmp;
    1363 
    1364         tmp = set_intersection_multi(so, args);
    1365         if (tmp == NULL)
    1366                 return NULL;
    1367         set_swap_bodies(so, (PySetObject *)tmp);
    1368         Py_DECREF(tmp);
    1369         Py_RETURN_NONE;
     1367    PyObject *tmp;
     1368
     1369    tmp = set_intersection_multi(so, args);
     1370    if (tmp == NULL)
     1371        return NULL;
     1372    set_swap_bodies(so, (PySetObject *)tmp);
     1373    Py_DECREF(tmp);
     1374    Py_RETURN_NONE;
    13701375}
    13711376
     
    13761381set_and(PySetObject *so, PyObject *other)
    13771382{
    1378         if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
    1379                 Py_INCREF(Py_NotImplemented);
    1380                 return Py_NotImplemented;
    1381         }
    1382         return set_intersection(so, other);
     1383    if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
     1384        Py_INCREF(Py_NotImplemented);
     1385        return Py_NotImplemented;
     1386    }
     1387    return set_intersection(so, other);
    13831388}
    13841389
     
    13861391set_iand(PySetObject *so, PyObject *other)
    13871392{
    1388         PyObject *result;
    1389 
    1390         if (!PyAnySet_Check(other)) {
    1391                 Py_INCREF(Py_NotImplemented);
    1392                 return Py_NotImplemented;
    1393         }
    1394         result = set_intersection_update(so, other);
    1395         if (result == NULL)
    1396                 return NULL;
    1397         Py_DECREF(result);
    1398         Py_INCREF(so);
    1399         return (PyObject *)so;
     1393    PyObject *result;
     1394
     1395    if (!PyAnySet_Check(other)) {
     1396        Py_INCREF(Py_NotImplemented);
     1397        return Py_NotImplemented;
     1398    }
     1399    result = set_intersection_update(so, other);
     1400    if (result == NULL)
     1401        return NULL;
     1402    Py_DECREF(result);
     1403    Py_INCREF(so);
     1404    return (PyObject *)so;
    14001405}
    14011406
     
    14031408set_isdisjoint(PySetObject *so, PyObject *other)
    14041409{
    1405         PyObject *key, *it, *tmp;
    1406 
    1407         if ((PyObject *)so == other) {
    1408                 if (PySet_GET_SIZE(so) == 0)
    1409                         Py_RETURN_TRUE;
    1410                 else
    1411                         Py_RETURN_FALSE;
    1412         }
    1413 
    1414         if (PyAnySet_CheckExact(other)) {               
    1415                 Py_ssize_t pos = 0;
    1416                 setentry *entry;
    1417 
    1418                 if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
    1419                         tmp = (PyObject *)so;
    1420                         so = (PySetObject *)other;
    1421                         other = tmp;
    1422                 }
    1423                 while (set_next((PySetObject *)other, &pos, &entry)) {
    1424                         int rv = set_contains_entry(so, entry);
    1425                         if (rv == -1)
    1426                                 return NULL;
    1427                         if (rv)
    1428                                 Py_RETURN_FALSE;
    1429                 }
    1430                 Py_RETURN_TRUE;
    1431         }
    1432 
    1433         it = PyObject_GetIter(other);
    1434         if (it == NULL)
    1435                 return NULL;
    1436 
    1437         while ((key = PyIter_Next(it)) != NULL) {
    1438                 int rv;
    1439                 setentry entry;
    1440                 long hash = PyObject_Hash(key);
    1441 
    1442                 if (hash == -1) {
    1443                         Py_DECREF(key);
    1444                         Py_DECREF(it);
    1445                         return NULL;
    1446                 }
    1447                 entry.hash = hash;
    1448                 entry.key = key;
    1449                 rv = set_contains_entry(so, &entry);
    1450                 Py_DECREF(key);
    1451                 if (rv == -1) {
    1452                         Py_DECREF(it);
    1453                         return NULL;
    1454                 }
    1455                 if (rv) {
    1456                         Py_DECREF(it);
    1457                         Py_RETURN_FALSE;
    1458                 }
    1459         }
    1460         Py_DECREF(it);
    1461         if (PyErr_Occurred())
    1462                 return NULL;
    1463         Py_RETURN_TRUE;
     1410    PyObject *key, *it, *tmp;
     1411
     1412    if ((PyObject *)so == other) {
     1413        if (PySet_GET_SIZE(so) == 0)
     1414            Py_RETURN_TRUE;
     1415        else
     1416            Py_RETURN_FALSE;
     1417    }
     1418
     1419    if (PyAnySet_CheckExact(other)) {
     1420        Py_ssize_t pos = 0;
     1421        setentry *entry;
     1422
     1423        if (PySet_GET_SIZE(other) > PySet_GET_SIZE(so)) {
     1424            tmp = (PyObject *)so;
     1425            so = (PySetObject *)other;
     1426            other = tmp;
     1427        }
     1428        while (set_next((PySetObject *)other, &pos, &entry)) {
     1429            int rv = set_contains_entry(so, entry);
     1430            if (rv == -1)
     1431                return NULL;
     1432            if (rv)
     1433                Py_RETURN_FALSE;
     1434        }
     1435        Py_RETURN_TRUE;
     1436    }
     1437
     1438    it = PyObject_GetIter(other);
     1439    if (it == NULL)
     1440        return NULL;
     1441
     1442    while ((key = PyIter_Next(it)) != NULL) {
     1443        int rv;
     1444        setentry entry;
     1445        long hash = PyObject_Hash(key);
     1446
     1447        if (hash == -1) {
     1448            Py_DECREF(key);
     1449            Py_DECREF(it);
     1450            return NULL;
     1451        }
     1452        entry.hash = hash;
     1453        entry.key = key;
     1454        rv = set_contains_entry(so, &entry);
     1455        Py_DECREF(key);
     1456        if (rv == -1) {
     1457            Py_DECREF(it);
     1458            return NULL;
     1459        }
     1460        if (rv) {
     1461            Py_DECREF(it);
     1462            Py_RETURN_FALSE;
     1463        }
     1464    }
     1465    Py_DECREF(it);
     1466    if (PyErr_Occurred())
     1467        return NULL;
     1468    Py_RETURN_TRUE;
    14641469}
    14651470
     
    14701475set_difference_update_internal(PySetObject *so, PyObject *other)
    14711476{
    1472         if ((PyObject *)so == other)
    1473                 return set_clear_internal(so);
    1474        
    1475         if (PyAnySet_Check(other)) {
    1476                 setentry *entry;
    1477                 Py_ssize_t pos = 0;
    1478 
    1479                 while (set_next((PySetObject *)other, &pos, &entry))
    1480                         if (set_discard_entry(so, entry) == -1)
    1481                                 return -1;
    1482         } else {
    1483                 PyObject *key, *it;
    1484                 it = PyObject_GetIter(other);
    1485                 if (it == NULL)
    1486                         return -1;
    1487 
    1488                 while ((key = PyIter_Next(it)) != NULL) {
    1489                         if (set_discard_key(so, key) == -1) {
    1490                                 Py_DECREF(it);
    1491                                 Py_DECREF(key);
    1492                                 return -1;
    1493                         }
    1494                         Py_DECREF(key);
    1495                 }
    1496                 Py_DECREF(it);
    1497                 if (PyErr_Occurred())
    1498                         return -1;
    1499         }
    1500         /* If more than 1/5 are dummies, then resize them away. */
    1501         if ((so->fill - so->used) * 5 < so->mask)
    1502                 return 0;
    1503         return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
     1477    if ((PyObject *)so == other)
     1478        return set_clear_internal(so);
     1479
     1480    if (PyAnySet_Check(other)) {
     1481        setentry *entry;
     1482        Py_ssize_t pos = 0;
     1483
     1484        while (set_next((PySetObject *)other, &pos, &entry))
     1485            if (set_discard_entry(so, entry) == -1)
     1486                return -1;
     1487    } else {
     1488        PyObject *key, *it;
     1489        it = PyObject_GetIter(other);
     1490        if (it == NULL)
     1491            return -1;
     1492
     1493        while ((key = PyIter_Next(it)) != NULL) {
     1494            if (set_discard_key(so, key) == -1) {
     1495                Py_DECREF(it);
     1496                Py_DECREF(key);
     1497                return -1;
     1498            }
     1499            Py_DECREF(key);
     1500        }
     1501        Py_DECREF(it);
     1502        if (PyErr_Occurred())
     1503            return -1;
     1504    }
     1505    /* If more than 1/5 are dummies, then resize them away. */
     1506    if ((so->fill - so->used) * 5 < so->mask)
     1507        return 0;
     1508    return set_table_resize(so, so->used>50000 ? so->used*2 : so->used*4);
    15041509}
    15051510
     
    15071512set_difference_update(PySetObject *so, PyObject *args)
    15081513{
    1509         Py_ssize_t i;
    1510 
    1511         for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
    1512                 PyObject *other = PyTuple_GET_ITEM(args, i);
    1513                 if (set_difference_update_internal(so, other) == -1)
    1514                         return NULL;
    1515         }
    1516         Py_RETURN_NONE;
     1514    Py_ssize_t i;
     1515
     1516    for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
     1517        PyObject *other = PyTuple_GET_ITEM(args, i);
     1518        if (set_difference_update_internal(so, other) == -1)
     1519            return NULL;
     1520    }
     1521    Py_RETURN_NONE;
    15171522}
    15181523
     
    15231528set_difference(PySetObject *so, PyObject *other)
    15241529{
    1525         PyObject *result;
    1526         setentry *entry;
    1527         Py_ssize_t pos = 0;
    1528 
    1529         if (!PyAnySet_Check(other)  && !PyDict_CheckExact(other)) {
    1530                 result = set_copy(so);
    1531                 if (result == NULL)
    1532                         return NULL;
    1533                 if (set_difference_update_internal((PySetObject *)result, other) != -1)
    1534                         return result;
    1535                 Py_DECREF(result);
    1536                 return NULL;
    1537         }
    1538        
    1539         result = make_new_set(Py_TYPE(so), NULL);
    1540         if (result == NULL)
    1541                 return NULL;
    1542 
    1543         if (PyDict_CheckExact(other)) {
    1544                 while (set_next(so, &pos, &entry)) {
    1545                         setentry entrycopy;
    1546                         entrycopy.hash = entry->hash;
    1547                         entrycopy.key = entry->key;
    1548                         if (!_PyDict_Contains(other, entry->key, entry->hash)) {
    1549                                 if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
    1550                                         Py_DECREF(result);
    1551                                         return NULL;
    1552                                 }
    1553                         }
    1554                 }
    1555                 return result;
    1556         }
    1557 
    1558         while (set_next(so, &pos, &entry)) {
    1559                 int rv = set_contains_entry((PySetObject *)other, entry);
    1560                 if (rv == -1) {
    1561                         Py_DECREF(result);
    1562                         return NULL;
    1563                 }
    1564                 if (!rv) {
    1565                         if (set_add_entry((PySetObject *)result, entry) == -1) {
    1566                                 Py_DECREF(result);
    1567                                 return NULL;
    1568                         }
    1569                 }
    1570         }
    1571         return result;
     1530    PyObject *result;
     1531    setentry *entry;
     1532    Py_ssize_t pos = 0;
     1533
     1534    if (!PyAnySet_Check(other)  && !PyDict_CheckExact(other)) {
     1535        result = set_copy(so);
     1536        if (result == NULL)
     1537            return NULL;
     1538        if (set_difference_update_internal((PySetObject *)result, other) != -1)
     1539            return result;
     1540        Py_DECREF(result);
     1541        return NULL;
     1542    }
     1543
     1544    result = make_new_set(Py_TYPE(so), NULL);
     1545    if (result == NULL)
     1546        return NULL;
     1547
     1548    if (PyDict_CheckExact(other)) {
     1549        while (set_next(so, &pos, &entry)) {
     1550            setentry entrycopy;
     1551            entrycopy.hash = entry->hash;
     1552            entrycopy.key = entry->key;
     1553            if (!_PyDict_Contains(other, entry->key, entry->hash)) {
     1554                if (set_add_entry((PySetObject *)result, &entrycopy) == -1) {
     1555                    Py_DECREF(result);
     1556                    return NULL;
     1557                }
     1558            }
     1559        }
     1560        return result;
     1561    }
     1562
     1563    while (set_next(so, &pos, &entry)) {
     1564        int rv = set_contains_entry((PySetObject *)other, entry);
     1565        if (rv == -1) {
     1566            Py_DECREF(result);
     1567            return NULL;
     1568        }
     1569        if (!rv) {
     1570            if (set_add_entry((PySetObject *)result, entry) == -1) {
     1571                Py_DECREF(result);
     1572                return NULL;
     1573            }
     1574        }
     1575    }
     1576    return result;
    15721577}
    15731578
     
    15751580set_difference_multi(PySetObject *so, PyObject *args)
    15761581{
    1577         Py_ssize_t i;
    1578         PyObject *result, *other;
    1579 
    1580         if (PyTuple_GET_SIZE(args) == 0)
    1581                 return set_copy(so);
    1582 
    1583         other = PyTuple_GET_ITEM(args, 0);
    1584         result = set_difference(so, other);
    1585         if (result == NULL)
    1586                 return NULL;
    1587 
    1588         for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
    1589                 other = PyTuple_GET_ITEM(args, i);
    1590                 if (set_difference_update_internal((PySetObject *)result, other) == -1) {
    1591                         Py_DECREF(result);
    1592                         return NULL;
    1593                 }
    1594         }
    1595         return result;
     1582    Py_ssize_t i;
     1583    PyObject *result, *other;
     1584
     1585    if (PyTuple_GET_SIZE(args) == 0)
     1586        return set_copy(so);
     1587
     1588    other = PyTuple_GET_ITEM(args, 0);
     1589    result = set_difference(so, other);
     1590    if (result == NULL)
     1591        return NULL;
     1592
     1593    for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
     1594        other = PyTuple_GET_ITEM(args, i);
     1595        if (set_difference_update_internal((PySetObject *)result, other) == -1) {
     1596            Py_DECREF(result);
     1597            return NULL;
     1598        }
     1599    }
     1600    return result;
    15961601}
    15971602
     
    16031608set_sub(PySetObject *so, PyObject *other)
    16041609{
    1605         if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
    1606                 Py_INCREF(Py_NotImplemented);
    1607                 return Py_NotImplemented;
    1608         }
    1609         return set_difference(so, other);
     1610    if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
     1611        Py_INCREF(Py_NotImplemented);
     1612        return Py_NotImplemented;
     1613    }
     1614    return set_difference(so, other);
    16101615}
    16111616
     
    16131618set_isub(PySetObject *so, PyObject *other)
    16141619{
    1615         if (!PyAnySet_Check(other)) {
    1616                 Py_INCREF(Py_NotImplemented);
    1617                 return Py_NotImplemented;
    1618         }
    1619         if (set_difference_update_internal(so, other) == -1)
    1620                 return NULL;
    1621         Py_INCREF(so);
    1622         return (PyObject *)so;
     1620    if (!PyAnySet_Check(other)) {
     1621        Py_INCREF(Py_NotImplemented);
     1622        return Py_NotImplemented;
     1623    }
     1624    if (set_difference_update_internal(so, other) == -1)
     1625        return NULL;
     1626    Py_INCREF(so);
     1627    return (PyObject *)so;
    16231628}
    16241629
     
    16261631set_symmetric_difference_update(PySetObject *so, PyObject *other)
    16271632{
    1628         PySetObject *otherset;
    1629         PyObject *key;
    1630         Py_ssize_t pos = 0;
    1631         setentry *entry;
    1632 
    1633         if ((PyObject *)so == other)
    1634                 return set_clear(so);
    1635 
    1636         if (PyDict_CheckExact(other)) {
    1637                 PyObject *value;
    1638                 int rv;
    1639                 long hash;
    1640                 while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
    1641                         setentry an_entry;
    1642 
    1643                         an_entry.hash = hash;
    1644                         an_entry.key = key;
    1645                         rv = set_discard_entry(so, &an_entry);
    1646                         if (rv == -1)
    1647                                 return NULL;
    1648                         if (rv == DISCARD_NOTFOUND) {
    1649                                 if (set_add_entry(so, &an_entry) == -1)
    1650                                         return NULL;
    1651                         }
    1652                 }
    1653                 Py_RETURN_NONE;
    1654         }
    1655 
    1656         if (PyAnySet_Check(other)) {
    1657                 Py_INCREF(other);
    1658                 otherset = (PySetObject *)other;
    1659         } else {
    1660                 otherset = (PySetObject *)make_new_set(Py_TYPE(so), other);
    1661                 if (otherset == NULL)
    1662                         return NULL;
    1663         }
    1664 
    1665         while (set_next(otherset, &pos, &entry)) {
    1666                 int rv = set_discard_entry(so, entry);
    1667                 if (rv == -1) {
    1668                         Py_DECREF(otherset);
    1669                         return NULL;
    1670                 }
    1671                 if (rv == DISCARD_NOTFOUND) {
    1672                         if (set_add_entry(so, entry) == -1) {
    1673                                 Py_DECREF(otherset);
    1674                                 return NULL;
    1675                         }
    1676                 }
    1677         }
    1678         Py_DECREF(otherset);
    1679         Py_RETURN_NONE;
     1633    PySetObject *otherset;
     1634    PyObject *key;
     1635    Py_ssize_t pos = 0;
     1636    setentry *entry;
     1637
     1638    if ((PyObject *)so == other)
     1639        return set_clear(so);
     1640
     1641    if (PyDict_CheckExact(other)) {
     1642        PyObject *value;
     1643        int rv;
     1644        long hash;
     1645        while (_PyDict_Next(other, &pos, &key, &value, &hash)) {
     1646            setentry an_entry;
     1647
     1648            Py_INCREF(key);
     1649            an_entry.hash = hash;
     1650            an_entry.key = key;
     1651
     1652            rv = set_discard_entry(so, &an_entry);
     1653            if (rv == -1) {
     1654                Py_DECREF(key);
     1655                return NULL;
     1656            }
     1657            if (rv == DISCARD_NOTFOUND) {
     1658                if (set_add_entry(so, &an_entry) == -1) {
     1659                    Py_DECREF(key);
     1660                    return NULL;
     1661                }
     1662            }
     1663            Py_DECREF(key);
     1664        }
     1665        Py_RETURN_NONE;
     1666    }
     1667
     1668    if (PyAnySet_Check(other)) {
     1669        Py_INCREF(other);
     1670        otherset = (PySetObject *)other;
     1671    } else {
     1672        otherset = (PySetObject *)make_new_set(Py_TYPE(so), other);
     1673        if (otherset == NULL)
     1674            return NULL;
     1675    }
     1676
     1677    while (set_next(otherset, &pos, &entry)) {
     1678        int rv = set_discard_entry(so, entry);
     1679        if (rv == -1) {
     1680            Py_DECREF(otherset);
     1681            return NULL;
     1682        }
     1683        if (rv == DISCARD_NOTFOUND) {
     1684            if (set_add_entry(so, entry) == -1) {
     1685                Py_DECREF(otherset);
     1686                return NULL;
     1687            }
     1688        }
     1689    }
     1690    Py_DECREF(otherset);
     1691    Py_RETURN_NONE;
    16801692}
    16811693
     
    16861698set_symmetric_difference(PySetObject *so, PyObject *other)
    16871699{
    1688         PyObject *rv;
    1689         PySetObject *otherset;
    1690 
    1691         otherset = (PySetObject *)make_new_set(Py_TYPE(so), other);
    1692         if (otherset == NULL)
    1693                 return NULL;
    1694         rv = set_symmetric_difference_update(otherset, (PyObject *)so);
    1695         if (rv == NULL)
    1696                 return NULL;
    1697         Py_DECREF(rv);
    1698         return (PyObject *)otherset;
     1700    PyObject *rv;
     1701    PySetObject *otherset;
     1702
     1703    otherset = (PySetObject *)make_new_set(Py_TYPE(so), other);
     1704    if (otherset == NULL)
     1705        return NULL;
     1706    rv = set_symmetric_difference_update(otherset, (PyObject *)so);
     1707    if (rv == NULL)
     1708        return NULL;
     1709    Py_DECREF(rv);
     1710    return (PyObject *)otherset;
    16991711}
    17001712
     
    17071719set_xor(PySetObject *so, PyObject *other)
    17081720{
    1709         if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
    1710                 Py_INCREF(Py_NotImplemented);
    1711                 return Py_NotImplemented;
    1712         }
    1713         return set_symmetric_difference(so, other);
     1721    if (!PyAnySet_Check(so) || !PyAnySet_Check(other)) {
     1722        Py_INCREF(Py_NotImplemented);
     1723        return Py_NotImplemented;
     1724    }
     1725    return set_symmetric_difference(so, other);
    17141726}
    17151727
     
    17171729set_ixor(PySetObject *so, PyObject *other)
    17181730{
    1719         PyObject *result;
    1720 
    1721         if (!PyAnySet_Check(other)) {
    1722                 Py_INCREF(Py_NotImplemented);
    1723                 return Py_NotImplemented;
    1724         }
    1725         result = set_symmetric_difference_update(so, other);
    1726         if (result == NULL)
    1727                 return NULL;
    1728         Py_DECREF(result);
    1729         Py_INCREF(so);
    1730         return (PyObject *)so;
     1731    PyObject *result;
     1732
     1733    if (!PyAnySet_Check(other)) {
     1734        Py_INCREF(Py_NotImplemented);
     1735        return Py_NotImplemented;
     1736    }
     1737    result = set_symmetric_difference_update(so, other);
     1738    if (result == NULL)
     1739        return NULL;
     1740    Py_DECREF(result);
     1741    Py_INCREF(so);
     1742    return (PyObject *)so;
    17311743}
    17321744
     
    17341746set_issubset(PySetObject *so, PyObject *other)
    17351747{
    1736         setentry *entry;
    1737         Py_ssize_t pos = 0;
    1738 
    1739         if (!PyAnySet_Check(other)) {
    1740                 PyObject *tmp, *result;
    1741                 tmp = make_new_set(&PySet_Type, other);
    1742                 if (tmp == NULL)
    1743                         return NULL;
    1744                 result = set_issubset(so, tmp);
    1745                 Py_DECREF(tmp);
    1746                 return result;
    1747         }
    1748         if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
    1749                 Py_RETURN_FALSE;
    1750 
    1751         while (set_next(so, &pos, &entry)) {
    1752                 int rv = set_contains_entry((PySetObject *)other, entry);
    1753                 if (rv == -1)
    1754                         return NULL;
    1755                 if (!rv)
    1756                         Py_RETURN_FALSE;
    1757         }
    1758         Py_RETURN_TRUE;
     1748    setentry *entry;
     1749    Py_ssize_t pos = 0;
     1750
     1751    if (!PyAnySet_Check(other)) {
     1752        PyObject *tmp, *result;
     1753        tmp = make_new_set(&PySet_Type, other);
     1754        if (tmp == NULL)
     1755            return NULL;
     1756        result = set_issubset(so, tmp);
     1757        Py_DECREF(tmp);
     1758        return result;
     1759    }
     1760    if (PySet_GET_SIZE(so) > PySet_GET_SIZE(other))
     1761        Py_RETURN_FALSE;
     1762
     1763    while (set_next(so, &pos, &entry)) {
     1764        int rv = set_contains_entry((PySetObject *)other, entry);
     1765        if (rv == -1)
     1766            return NULL;
     1767        if (!rv)
     1768            Py_RETURN_FALSE;
     1769    }
     1770    Py_RETURN_TRUE;
    17591771}
    17601772
     
    17641776set_issuperset(PySetObject *so, PyObject *other)
    17651777{
    1766         PyObject *tmp, *result;
    1767 
    1768         if (!PyAnySet_Check(other)) {
    1769                 tmp = make_new_set(&PySet_Type, other);
    1770                 if (tmp == NULL)
    1771                         return NULL;
    1772                 result = set_issuperset(so, tmp);
    1773                 Py_DECREF(tmp);
    1774                 return result;
    1775         }
    1776         return set_issubset((PySetObject *)other, (PyObject *)so);
     1778    PyObject *tmp, *result;
     1779
     1780    if (!PyAnySet_Check(other)) {
     1781        tmp = make_new_set(&PySet_Type, other);
     1782        if (tmp == NULL)
     1783            return NULL;
     1784        result = set_issuperset(so, tmp);
     1785        Py_DECREF(tmp);
     1786        return result;
     1787    }
     1788    return set_issubset((PySetObject *)other, (PyObject *)so);
    17771789}
    17781790
     
    17821794set_richcompare(PySetObject *v, PyObject *w, int op)
    17831795{
    1784         PyObject *r1, *r2;
    1785 
    1786         if(!PyAnySet_Check(w)) {
    1787                 if (op == Py_EQ)
    1788                         Py_RETURN_FALSE;
    1789                 if (op == Py_NE)
    1790                         Py_RETURN_TRUE;
    1791                 PyErr_SetString(PyExc_TypeError, "can only compare to a set");
    1792                 return NULL;
    1793         }
    1794         switch (op) {
    1795         case Py_EQ:
    1796                 if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
    1797                         Py_RETURN_FALSE;
    1798                 if (v->hash != -1  &&
    1799                     ((PySetObject *)w)->hash != -1 &&
    1800                     v->hash != ((PySetObject *)w)->hash)
    1801                         Py_RETURN_FALSE;
    1802                 return set_issubset(v, w);
    1803         case Py_NE:
    1804                 r1 = set_richcompare(v, w, Py_EQ);
    1805                 if (r1 == NULL)
    1806                         return NULL;
    1807                 r2 = PyBool_FromLong(PyObject_Not(r1));
    1808                 Py_DECREF(r1);
    1809                 return r2;
    1810         case Py_LE:
    1811                 return set_issubset(v, w);
    1812         case Py_GE:
    1813                 return set_issuperset(v, w);
    1814         case Py_LT:
    1815                 if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
    1816                         Py_RETURN_FALSE;               
    1817                 return set_issubset(v, w);
    1818         case Py_GT:
    1819                 if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
    1820                         Py_RETURN_FALSE;
    1821                 return set_issuperset(v, w);
    1822         }
    1823         Py_INCREF(Py_NotImplemented);
    1824         return Py_NotImplemented;
     1796    PyObject *r1, *r2;
     1797
     1798    if(!PyAnySet_Check(w)) {
     1799        if (op == Py_EQ)
     1800            Py_RETURN_FALSE;
     1801        if (op == Py_NE)
     1802            Py_RETURN_TRUE;
     1803        PyErr_SetString(PyExc_TypeError, "can only compare to a set");
     1804        return NULL;
     1805    }
     1806    switch (op) {
     1807    case Py_EQ:
     1808        if (PySet_GET_SIZE(v) != PySet_GET_SIZE(w))
     1809            Py_RETURN_FALSE;
     1810        if (v->hash != -1  &&
     1811            ((PySetObject *)w)->hash != -1 &&
     1812            v->hash != ((PySetObject *)w)->hash)
     1813            Py_RETURN_FALSE;
     1814        return set_issubset(v, w);
     1815    case Py_NE:
     1816        r1 = set_richcompare(v, w, Py_EQ);
     1817        if (r1 == NULL)
     1818            return NULL;
     1819        r2 = PyBool_FromLong(PyObject_Not(r1));
     1820        Py_DECREF(r1);
     1821        return r2;
     1822    case Py_LE:
     1823        return set_issubset(v, w);
     1824    case Py_GE:
     1825        return set_issuperset(v, w);
     1826    case Py_LT:
     1827        if (PySet_GET_SIZE(v) >= PySet_GET_SIZE(w))
     1828            Py_RETURN_FALSE;
     1829        return set_issubset(v, w);
     1830    case Py_GT:
     1831        if (PySet_GET_SIZE(v) <= PySet_GET_SIZE(w))
     1832            Py_RETURN_FALSE;
     1833        return set_issuperset(v, w);
     1834    }
     1835    Py_INCREF(Py_NotImplemented);
     1836    return Py_NotImplemented;
    18251837}
    18261838
     
    18281840set_nocmp(PyObject *self, PyObject *other)
    18291841{
    1830         PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
    1831         return -1;
     1842    PyErr_SetString(PyExc_TypeError, "cannot compare sets using cmp()");
     1843    return -1;
    18321844}
    18331845
     
    18351847set_add(PySetObject *so, PyObject *key)
    18361848{
    1837         if (set_add_key(so, key) == -1)
    1838                 return NULL;
    1839         Py_RETURN_NONE;
    1840 }
    1841 
    1842 PyDoc_STRVAR(add_doc, 
     1849    if (set_add_key(so, key) == -1)
     1850        return NULL;
     1851    Py_RETURN_NONE;
     1852}
     1853
     1854PyDoc_STRVAR(add_doc,
    18431855"Add an element to a set.\n\
    18441856\n\
     
    18481860set_contains(PySetObject *so, PyObject *key)
    18491861{
    1850         PyObject *tmpkey;
    1851         int rv;
    1852 
    1853         rv = set_contains_key(so, key);
    1854         if (rv == -1) {
    1855                 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
    1856                         return -1;
    1857                 PyErr_Clear();
    1858                 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
    1859                 if (tmpkey == NULL)
    1860                         return -1;
    1861                 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
    1862                 rv = set_contains(so, tmpkey);
    1863                 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
    1864                 Py_DECREF(tmpkey);
    1865         }
    1866         return rv;
     1862    PyObject *tmpkey;
     1863    int rv;
     1864
     1865    rv = set_contains_key(so, key);
     1866    if (rv == -1) {
     1867        if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
     1868            return -1;
     1869        PyErr_Clear();
     1870        tmpkey = make_new_set(&PyFrozenSet_Type, key);
     1871        if (tmpkey == NULL)
     1872            return -1;
     1873        rv = set_contains_key(so, tmpkey);
     1874        Py_DECREF(tmpkey);
     1875    }
     1876    return rv;
    18671877}
    18681878
     
    18701880set_direct_contains(PySetObject *so, PyObject *key)
    18711881{
    1872         long result;
    1873 
    1874         result = set_contains(so, key);
    1875         if (result == -1)
    1876                 return NULL;
    1877         return PyBool_FromLong(result);
     1882    long result;
     1883
     1884    result = set_contains(so, key);
     1885    if (result == -1)
     1886        return NULL;
     1887    return PyBool_FromLong(result);
    18781888}
    18791889
     
    18831893set_remove(PySetObject *so, PyObject *key)
    18841894{
    1885         PyObject *tmpkey;
    1886         int rv;
    1887 
    1888         rv = set_discard_key(so, key);
    1889         if (rv == -1) {
    1890                 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
    1891                         return NULL;
    1892                 PyErr_Clear();
    1893                 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
    1894                 if (tmpkey == NULL)
    1895                         return NULL;
    1896                 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
    1897                 rv = set_discard_key(so, tmpkey);
    1898                 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
    1899                 Py_DECREF(tmpkey);
    1900                 if (rv == -1)
    1901                         return NULL;
    1902         }
    1903 
    1904         if (rv == DISCARD_NOTFOUND) {
    1905                 set_key_error(key);
    1906                 return NULL;
    1907         }
    1908         Py_RETURN_NONE;
     1895    PyObject *tmpkey;
     1896    int rv;
     1897
     1898    rv = set_discard_key(so, key);
     1899    if (rv == -1) {
     1900        if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
     1901            return NULL;
     1902        PyErr_Clear();
     1903        tmpkey = make_new_set(&PyFrozenSet_Type, key);
     1904        if (tmpkey == NULL)
     1905            return NULL;
     1906        rv = set_discard_key(so, tmpkey);
     1907        Py_DECREF(tmpkey);
     1908        if (rv == -1)
     1909            return NULL;
     1910    }
     1911
     1912    if (rv == DISCARD_NOTFOUND) {
     1913        set_key_error(key);
     1914        return NULL;
     1915    }
     1916    Py_RETURN_NONE;
    19091917}
    19101918
     
    19171925set_discard(PySetObject *so, PyObject *key)
    19181926{
    1919         PyObject *tmpkey, *result;
    1920         int rv;
    1921 
    1922         rv = set_discard_key(so, key);
    1923         if (rv == -1) {
    1924                 if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
    1925                         return NULL;
    1926                 PyErr_Clear();
    1927                 tmpkey = make_new_set(&PyFrozenSet_Type, NULL);
    1928                 if (tmpkey == NULL)
    1929                         return NULL;
    1930                 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
    1931                 result = set_discard(so, tmpkey);
    1932                 set_swap_bodies((PySetObject *)tmpkey, (PySetObject *)key);
    1933                 Py_DECREF(tmpkey);
    1934                 return result;
    1935         }
    1936         Py_RETURN_NONE;
     1927    PyObject *tmpkey;
     1928    int rv;
     1929
     1930    rv = set_discard_key(so, key);
     1931    if (rv == -1) {
     1932        if (!PySet_Check(key) || !PyErr_ExceptionMatches(PyExc_TypeError))
     1933            return NULL;
     1934        PyErr_Clear();
     1935        tmpkey = make_new_set(&PyFrozenSet_Type, key);
     1936        if (tmpkey == NULL)
     1937            return NULL;
     1938        rv = set_discard_key(so, tmpkey);
     1939        Py_DECREF(tmpkey);
     1940        if (rv == -1)
     1941            return NULL;
     1942    }
     1943    Py_RETURN_NONE;
    19371944}
    19381945
     
    19401947"Remove an element from a set if it is a member.\n\
    19411948\n\
    1942 If the element is not a member, do nothing."); 
     1949If the element is not a member, do nothing.");
    19431950
    19441951static PyObject *
    19451952set_reduce(PySetObject *so)
    19461953{
    1947         PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
    1948 
    1949         keys = PySequence_List((PyObject *)so);
    1950         if (keys == NULL)
    1951                 goto done;
    1952         args = PyTuple_Pack(1, keys);
    1953         if (args == NULL)
    1954                 goto done;
    1955         dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
    1956         if (dict == NULL) {
    1957                 PyErr_Clear();
    1958                 dict = Py_None;
    1959                 Py_INCREF(dict);
    1960         }
    1961         result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
     1954    PyObject *keys=NULL, *args=NULL, *result=NULL, *dict=NULL;
     1955
     1956    keys = PySequence_List((PyObject *)so);
     1957    if (keys == NULL)
     1958        goto done;
     1959    args = PyTuple_Pack(1, keys);
     1960    if (args == NULL)
     1961        goto done;
     1962    dict = PyObject_GetAttrString((PyObject *)so, "__dict__");
     1963    if (dict == NULL) {
     1964        PyErr_Clear();
     1965        dict = Py_None;
     1966        Py_INCREF(dict);
     1967    }
     1968    result = PyTuple_Pack(3, Py_TYPE(so), args, dict);
    19621969done:
    1963         Py_XDECREF(args);
    1964         Py_XDECREF(keys);
    1965         Py_XDECREF(dict);
    1966         return result;
     1970    Py_XDECREF(args);
     1971    Py_XDECREF(keys);
     1972    Py_XDECREF(dict);
     1973    return result;
    19671974}
    19681975
     
    19721979set_sizeof(PySetObject *so)
    19731980{
    1974         Py_ssize_t res;
    1975 
    1976         res = sizeof(PySetObject);
    1977         if (so->table != so->smalltable)
    1978                 res = res + (so->mask + 1) * sizeof(setentry);
    1979         return PyInt_FromSsize_t(res);
     1981    Py_ssize_t res;
     1982
     1983    res = sizeof(PySetObject);
     1984    if (so->table != so->smalltable)
     1985        res = res + (so->mask + 1) * sizeof(setentry);
     1986    return PyInt_FromSsize_t(res);
    19801987}
    19811988
     
    19841991set_init(PySetObject *self, PyObject *args, PyObject *kwds)
    19851992{
    1986         PyObject *iterable = NULL;
    1987 
    1988         if (!PyAnySet_Check(self))
    1989                 return -1;
    1990         if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
    1991                 return -1;
    1992         set_clear_internal(self);
    1993         self->hash = -1;
    1994         if (iterable == NULL)
    1995                 return 0;
    1996         return set_update_internal(self, iterable);
     1993    PyObject *iterable = NULL;
     1994
     1995    if (!PyAnySet_Check(self))
     1996        return -1;
     1997    if (PySet_Check(self) && !_PyArg_NoKeywords("set()", kwds))
     1998        return -1;
     1999    if (!PyArg_UnpackTuple(args, Py_TYPE(self)->tp_name, 0, 1, &iterable))
     2000        return -1;
     2001    set_clear_internal(self);
     2002    self->hash = -1;
     2003    if (iterable == NULL)
     2004        return 0;
     2005    return set_update_internal(self, iterable);
    19972006}
    19982007
    19992008static PySequenceMethods set_as_sequence = {
    2000         set_len,                        /* sq_length */
    2001         0,                              /* sq_concat */
    2002         0,                              /* sq_repeat */
    2003         0,                              /* sq_item */
    2004         0,                              /* sq_slice */
    2005         0,                              /* sq_ass_item */
    2006         0,                              /* sq_ass_slice */
    2007         (objobjproc)set_contains,       /* sq_contains */
     2009    set_len,                            /* sq_length */
     2010    0,                                  /* sq_concat */
     2011    0,                                  /* sq_repeat */
     2012    0,                                  /* sq_item */
     2013    0,                                  /* sq_slice */
     2014    0,                                  /* sq_ass_item */
     2015    0,                                  /* sq_ass_slice */
     2016    (objobjproc)set_contains,           /* sq_contains */
    20082017};
    20092018
     
    20182027
    20192028static PyMethodDef set_methods[] = {
    2020         {"add",         (PyCFunction)set_add,           METH_O,
    2021         add_doc},
    2022         {"clear",       (PyCFunction)set_clear,         METH_NOARGS,
    2023         clear_doc},
    2024         {"__contains__",(PyCFunction)set_direct_contains,       METH_O | METH_COEXIST,
    2025         contains_doc},
    2026         {"copy",        (PyCFunction)set_copy,          METH_NOARGS,
    2027         copy_doc},
    2028         {"discard",     (PyCFunction)set_discard,       METH_O,
    2029         discard_doc},
    2030         {"difference",  (PyCFunction)set_difference_multi,      METH_VARARGS,
    2031         difference_doc},
    2032         {"difference_update",   (PyCFunction)set_difference_update,     METH_VARARGS,
    2033         difference_update_doc},
    2034         {"intersection",(PyCFunction)set_intersection_multi,    METH_VARARGS,
    2035         intersection_doc},
    2036         {"intersection_update",(PyCFunction)set_intersection_update_multi,      METH_VARARGS,
    2037         intersection_update_doc},
    2038         {"isdisjoint",  (PyCFunction)set_isdisjoint,    METH_O,
    2039         isdisjoint_doc},
    2040         {"issubset",    (PyCFunction)set_issubset,      METH_O,
    2041         issubset_doc},
    2042         {"issuperset",  (PyCFunction)set_issuperset,    METH_O,
    2043         issuperset_doc},
    2044         {"pop",         (PyCFunction)set_pop,           METH_NOARGS,
    2045         pop_doc},
    2046         {"__reduce__",  (PyCFunction)set_reduce,        METH_NOARGS,
    2047         reduce_doc},
    2048         {"remove",      (PyCFunction)set_remove,        METH_O,
    2049         remove_doc},
    2050         {"__sizeof__",  (PyCFunction)set_sizeof,        METH_NOARGS,
    2051         sizeof_doc},
    2052         {"symmetric_difference",(PyCFunction)set_symmetric_difference,  METH_O,
    2053         symmetric_difference_doc},
    2054         {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update,    METH_O,
    2055         symmetric_difference_update_doc},
     2029    {"add",             (PyCFunction)set_add,           METH_O,
     2030    add_doc},
     2031    {"clear",           (PyCFunction)set_clear,         METH_NOARGS,
     2032    clear_doc},
     2033    {"__contains__",(PyCFunction)set_direct_contains,           METH_O | METH_COEXIST,
     2034    contains_doc},
     2035    {"copy",            (PyCFunction)set_copy,          METH_NOARGS,
     2036    copy_doc},
     2037    {"discard",         (PyCFunction)set_discard,       METH_O,
     2038    discard_doc},
     2039    {"difference",      (PyCFunction)set_difference_multi,      METH_VARARGS,
     2040    difference_doc},
     2041    {"difference_update",       (PyCFunction)set_difference_update,     METH_VARARGS,
     2042    difference_update_doc},
     2043    {"intersection",(PyCFunction)set_intersection_multi,        METH_VARARGS,
     2044    intersection_doc},
     2045    {"intersection_update",(PyCFunction)set_intersection_update_multi,          METH_VARARGS,
     2046    intersection_update_doc},
     2047    {"isdisjoint",      (PyCFunction)set_isdisjoint,    METH_O,
     2048    isdisjoint_doc},
     2049    {"issubset",        (PyCFunction)set_issubset,      METH_O,
     2050    issubset_doc},
     2051    {"issuperset",      (PyCFunction)set_issuperset,    METH_O,
     2052    issuperset_doc},
     2053    {"pop",             (PyCFunction)set_pop,           METH_NOARGS,
     2054    pop_doc},
     2055    {"__reduce__",      (PyCFunction)set_reduce,        METH_NOARGS,
     2056    reduce_doc},
     2057    {"remove",          (PyCFunction)set_remove,        METH_O,
     2058    remove_doc},
     2059    {"__sizeof__",      (PyCFunction)set_sizeof,        METH_NOARGS,
     2060    sizeof_doc},
     2061    {"symmetric_difference",(PyCFunction)set_symmetric_difference,      METH_O,
     2062    symmetric_difference_doc},
     2063    {"symmetric_difference_update",(PyCFunction)set_symmetric_difference_update,        METH_O,
     2064    symmetric_difference_update_doc},
    20562065#ifdef Py_DEBUG
    2057         {"test_c_api",  (PyCFunction)test_c_api,        METH_NOARGS,
    2058         test_c_api_doc},
     2066    {"test_c_api",      (PyCFunction)test_c_api,        METH_NOARGS,
     2067    test_c_api_doc},
    20592068#endif
    2060         {"union",       (PyCFunction)set_union,         METH_VARARGS,
    2061         union_doc},
    2062         {"update",      (PyCFunction)set_update,        METH_VARARGS,
    2063         update_doc},
    2064         {NULL,          NULL}   /* sentinel */
     2069    {"union",           (PyCFunction)set_union,         METH_VARARGS,
     2070    union_doc},
     2071    {"update",          (PyCFunction)set_update,        METH_VARARGS,
     2072    update_doc},
     2073    {NULL,              NULL}   /* sentinel */
    20652074};
    20662075
    20672076static PyNumberMethods set_as_number = {
    2068         0,                              /*nb_add*/
    2069         (binaryfunc)set_sub,            /*nb_subtract*/
    2070         0,                              /*nb_multiply*/
    2071         0,                              /*nb_divide*/
    2072         0,                              /*nb_remainder*/
    2073         0,                              /*nb_divmod*/
    2074         0,                              /*nb_power*/
    2075         0,                              /*nb_negative*/
    2076         0,                              /*nb_positive*/
    2077         0,                              /*nb_absolute*/
    2078         0,                              /*nb_nonzero*/
    2079         0,                              /*nb_invert*/
    2080         0,                              /*nb_lshift*/
    2081         0,                              /*nb_rshift*/
    2082         (binaryfunc)set_and,            /*nb_and*/
    2083         (binaryfunc)set_xor,            /*nb_xor*/
    2084         (binaryfunc)set_or,             /*nb_or*/
    2085         0,                              /*nb_coerce*/
    2086         0,                              /*nb_int*/
    2087         0,                              /*nb_long*/
    2088         0,                              /*nb_float*/
    2089         0,                              /*nb_oct*/
    2090         0,                              /*nb_hex*/
    2091         0,                              /*nb_inplace_add*/
    2092         (binaryfunc)set_isub,           /*nb_inplace_subtract*/
    2093         0,                              /*nb_inplace_multiply*/
    2094         0,                              /*nb_inplace_divide*/
    2095         0,                              /*nb_inplace_remainder*/
    2096         0,                              /*nb_inplace_power*/
    2097         0,                              /*nb_inplace_lshift*/
    2098         0,                              /*nb_inplace_rshift*/
    2099         (binaryfunc)set_iand,           /*nb_inplace_and*/
    2100         (binaryfunc)set_ixor,           /*nb_inplace_xor*/
    2101         (binaryfunc)set_ior,            /*nb_inplace_or*/
     2077    0,                                  /*nb_add*/
     2078    (binaryfunc)set_sub,                /*nb_subtract*/
     2079    0,                                  /*nb_multiply*/
     2080    0,                                  /*nb_divide*/
     2081    0,                                  /*nb_remainder*/
     2082    0,                                  /*nb_divmod*/
     2083    0,                                  /*nb_power*/
     2084    0,                                  /*nb_negative*/
     2085    0,                                  /*nb_positive*/
     2086    0,                                  /*nb_absolute*/
     2087    0,                                  /*nb_nonzero*/
     2088    0,                                  /*nb_invert*/
     2089    0,                                  /*nb_lshift*/
     2090    0,                                  /*nb_rshift*/
     2091    (binaryfunc)set_and,                /*nb_and*/
     2092    (binaryfunc)set_xor,                /*nb_xor*/
     2093    (binaryfunc)set_or,                 /*nb_or*/
     2094    0,                                  /*nb_coerce*/
     2095    0,                                  /*nb_int*/
     2096    0,                                  /*nb_long*/
     2097    0,                                  /*nb_float*/
     2098    0,                                  /*nb_oct*/
     2099    0,                                  /*nb_hex*/
     2100    0,                                  /*nb_inplace_add*/
     2101    (binaryfunc)set_isub,               /*nb_inplace_subtract*/
     2102    0,                                  /*nb_inplace_multiply*/
     2103    0,                                  /*nb_inplace_divide*/
     2104    0,                                  /*nb_inplace_remainder*/
     2105    0,                                  /*nb_inplace_power*/
     2106    0,                                  /*nb_inplace_lshift*/
     2107    0,                                  /*nb_inplace_rshift*/
     2108    (binaryfunc)set_iand,               /*nb_inplace_and*/
     2109    (binaryfunc)set_ixor,               /*nb_inplace_xor*/
     2110    (binaryfunc)set_ior,                /*nb_inplace_or*/
    21022111};
    21032112
     
    21092118
    21102119PyTypeObject PySet_Type = {
    2111         PyVarObject_HEAD_INIT(&PyType_Type, 0)
    2112         "set",                          /* tp_name */
    2113         sizeof(PySetObject),            /* tp_basicsize */
    2114         0,                              /* tp_itemsize */
    2115         /* methods */
    2116         (destructor)set_dealloc,        /* tp_dealloc */
    2117         (printfunc)set_tp_print,        /* tp_print */
    2118         0,                              /* tp_getattr */
    2119         0,                              /* tp_setattr */
    2120         set_nocmp,                      /* tp_compare */
    2121         (reprfunc)set_repr,             /* tp_repr */
    2122         &set_as_number,                 /* tp_as_number */
    2123         &set_as_sequence,               /* tp_as_sequence */
    2124         0,                              /* tp_as_mapping */
    2125         (hashfunc)PyObject_HashNotImplemented,  /* tp_hash */
    2126         0,                              /* tp_call */
    2127         0,                              /* tp_str */
    2128         PyObject_GenericGetAttr,        /* tp_getattro */
    2129         0,                              /* tp_setattro */
    2130         0,                              /* tp_as_buffer */
    2131         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
    2132                 Py_TPFLAGS_BASETYPE,    /* tp_flags */
    2133         set_doc,                        /* tp_doc */
    2134         (traverseproc)set_traverse,     /* tp_traverse */
    2135         (inquiry)set_clear_internal,    /* tp_clear */
    2136         (richcmpfunc)set_richcompare,   /* tp_richcompare */
    2137         offsetof(PySetObject, weakreflist),     /* tp_weaklistoffset */
    2138         (getiterfunc)set_iter,  /* tp_iter */
    2139         0,                              /* tp_iternext */
    2140         set_methods,                    /* tp_methods */
    2141         0,                              /* tp_members */
    2142         0,                              /* tp_getset */
    2143         0,                              /* tp_base */
    2144         0,                              /* tp_dict */
    2145         0,                              /* tp_descr_get */
    2146         0,                              /* tp_descr_set */
    2147         0,                              /* tp_dictoffset */
    2148         (initproc)set_init,             /* tp_init */
    2149         PyType_GenericAlloc,            /* tp_alloc */
    2150         set_new,                        /* tp_new */
    2151         PyObject_GC_Del,                /* tp_free */
     2120    PyVarObject_HEAD_INIT(&PyType_Type, 0)
     2121    "set",                              /* tp_name */
     2122    sizeof(PySetObject),                /* tp_basicsize */
     2123    0,                                  /* tp_itemsize */
     2124    /* methods */
     2125    (destructor)set_dealloc,            /* tp_dealloc */
     2126    (printfunc)set_tp_print,            /* tp_print */
     2127    0,                                  /* tp_getattr */
     2128    0,                                  /* tp_setattr */
     2129    set_nocmp,                          /* tp_compare */
     2130    (reprfunc)set_repr,                 /* tp_repr */
     2131    &set_as_number,                     /* tp_as_number */
     2132    &set_as_sequence,                   /* tp_as_sequence */
     2133    0,                                  /* tp_as_mapping */
     2134    (hashfunc)PyObject_HashNotImplemented,      /* tp_hash */
     2135    0,                                  /* tp_call */
     2136    0,                                  /* tp_str */
     2137    PyObject_GenericGetAttr,            /* tp_getattro */
     2138    0,                                  /* tp_setattro */
     2139    0,                                  /* tp_as_buffer */
     2140    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
     2141        Py_TPFLAGS_BASETYPE,            /* tp_flags */
     2142    set_doc,                            /* tp_doc */
     2143    (traverseproc)set_traverse,         /* tp_traverse */
     2144    (inquiry)set_clear_internal,        /* tp_clear */
     2145    (richcmpfunc)set_richcompare,       /* tp_richcompare */
     2146    offsetof(PySetObject, weakreflist),         /* tp_weaklistoffset */
     2147    (getiterfunc)set_iter,      /* tp_iter */
     2148    0,                                  /* tp_iternext */
     2149    set_methods,                        /* tp_methods */
     2150    0,                                  /* tp_members */
     2151    0,                                  /* tp_getset */
     2152    0,                                  /* tp_base */
     2153    0,                                  /* tp_dict */
     2154    0,                                  /* tp_descr_get */
     2155    0,                                  /* tp_descr_set */
     2156    0,                                  /* tp_dictoffset */
     2157    (initproc)set_init,                 /* tp_init */
     2158    PyType_GenericAlloc,                /* tp_alloc */
     2159    set_new,                            /* tp_new */
     2160    PyObject_GC_Del,                    /* tp_free */
    21522161};
    21532162
     
    21562165
    21572166static PyMethodDef frozenset_methods[] = {
    2158         {"__contains__",(PyCFunction)set_direct_contains,       METH_O | METH_COEXIST,
    2159         contains_doc},
    2160         {"copy",        (PyCFunction)frozenset_copy,    METH_NOARGS,
    2161         copy_doc},
    2162         {"difference",  (PyCFunction)set_difference_multi,      METH_VARARGS,
    2163         difference_doc},
    2164         {"intersection",(PyCFunction)set_intersection_multi,    METH_VARARGS,
    2165         intersection_doc},
    2166         {"isdisjoint",  (PyCFunction)set_isdisjoint,    METH_O,
    2167         isdisjoint_doc},
    2168         {"issubset",    (PyCFunction)set_issubset,      METH_O,
    2169         issubset_doc},
    2170         {"issuperset",  (PyCFunction)set_issuperset,    METH_O,
    2171         issuperset_doc},
    2172         {"__reduce__",  (PyCFunction)set_reduce,        METH_NOARGS,
    2173         reduce_doc},
    2174         {"__sizeof__",  (PyCFunction)set_sizeof,        METH_NOARGS,
    2175         sizeof_doc},
    2176         {"symmetric_difference",(PyCFunction)set_symmetric_difference,  METH_O,
    2177         symmetric_difference_doc},
    2178         {"union",       (PyCFunction)set_union,         METH_VARARGS,
    2179         union_doc},
    2180         {NULL,          NULL}   /* sentinel */
     2167    {"__contains__",(PyCFunction)set_direct_contains,           METH_O | METH_COEXIST,
     2168    contains_doc},
     2169    {"copy",            (PyCFunction)frozenset_copy,    METH_NOARGS,
     2170    copy_doc},
     2171    {"difference",      (PyCFunction)set_difference_multi,      METH_VARARGS,
     2172    difference_doc},
     2173    {"intersection",(PyCFunction)set_intersection_multi,        METH_VARARGS,
     2174    intersection_doc},
     2175    {"isdisjoint",      (PyCFunction)set_isdisjoint,    METH_O,
     2176    isdisjoint_doc},
     2177    {"issubset",        (PyCFunction)set_issubset,      METH_O,
     2178    issubset_doc},
     2179    {"issuperset",      (PyCFunction)set_issuperset,    METH_O,
     2180    issuperset_doc},
     2181    {"__reduce__",      (PyCFunction)set_reduce,        METH_NOARGS,
     2182    reduce_doc},
     2183    {"__sizeof__",      (PyCFunction)set_sizeof,        METH_NOARGS,
     2184    sizeof_doc},
     2185    {"symmetric_difference",(PyCFunction)set_symmetric_difference,      METH_O,
     2186    symmetric_difference_doc},
     2187    {"union",           (PyCFunction)set_union,         METH_VARARGS,
     2188    union_doc},
     2189    {NULL,              NULL}   /* sentinel */
    21812190};
    21822191
    21832192static PyNumberMethods frozenset_as_number = {
    2184         0,                              /*nb_add*/
    2185         (binaryfunc)set_sub,            /*nb_subtract*/
    2186         0,                              /*nb_multiply*/
    2187         0,                              /*nb_divide*/
    2188         0,                              /*nb_remainder*/
    2189         0,                              /*nb_divmod*/
    2190         0,                              /*nb_power*/
    2191         0,                              /*nb_negative*/
    2192         0,                              /*nb_positive*/
    2193         0,                              /*nb_absolute*/
    2194         0,                              /*nb_nonzero*/
    2195         0,                              /*nb_invert*/
    2196         0,                              /*nb_lshift*/
    2197         0,                              /*nb_rshift*/
    2198         (binaryfunc)set_and,            /*nb_and*/
    2199         (binaryfunc)set_xor,            /*nb_xor*/
    2200         (binaryfunc)set_or,             /*nb_or*/
     2193    0,                                  /*nb_add*/
     2194    (binaryfunc)set_sub,                /*nb_subtract*/
     2195    0,                                  /*nb_multiply*/
     2196    0,                                  /*nb_divide*/
     2197    0,                                  /*nb_remainder*/
     2198    0,                                  /*nb_divmod*/
     2199    0,                                  /*nb_power*/
     2200    0,                                  /*nb_negative*/
     2201    0,                                  /*nb_positive*/
     2202    0,                                  /*nb_absolute*/
     2203    0,                                  /*nb_nonzero*/
     2204    0,                                  /*nb_invert*/
     2205    0,                                  /*nb_lshift*/
     2206    0,                                  /*nb_rshift*/
     2207    (binaryfunc)set_and,                /*nb_and*/
     2208    (binaryfunc)set_xor,                /*nb_xor*/
     2209    (binaryfunc)set_or,                 /*nb_or*/
    22012210};
    22022211
     
    22082217
    22092218PyTypeObject PyFrozenSet_Type = {
    2210         PyVarObject_HEAD_INIT(&PyType_Type, 0)
    2211         "frozenset",                    /* tp_name */
    2212         sizeof(PySetObject),            /* tp_basicsize */
    2213         0,                              /* tp_itemsize */
    2214         /* methods */
    2215         (destructor)set_dealloc,        /* tp_dealloc */
    2216         (printfunc)set_tp_print,        /* tp_print */
    2217         0,                              /* tp_getattr */
    2218         0,                              /* tp_setattr */
    2219         set_nocmp,                      /* tp_compare */
    2220         (reprfunc)set_repr,             /* tp_repr */
    2221         &frozenset_as_number,           /* tp_as_number */
    2222         &set_as_sequence,               /* tp_as_sequence */
    2223         0,                              /* tp_as_mapping */
    2224         frozenset_hash,                 /* tp_hash */
    2225         0,                              /* tp_call */
    2226         0,                              /* tp_str */
    2227         PyObject_GenericGetAttr,        /* tp_getattro */
    2228         0,                              /* tp_setattro */
    2229         0,                              /* tp_as_buffer */
    2230         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
    2231                 Py_TPFLAGS_BASETYPE,    /* tp_flags */
    2232         frozenset_doc,                  /* tp_doc */
    2233         (traverseproc)set_traverse,     /* tp_traverse */
    2234         (inquiry)set_clear_internal,    /* tp_clear */
    2235         (richcmpfunc)set_richcompare,   /* tp_richcompare */
    2236         offsetof(PySetObject, weakreflist),     /* tp_weaklistoffset */
    2237         (getiterfunc)set_iter,          /* tp_iter */
    2238         0,                              /* tp_iternext */
    2239         frozenset_methods,              /* tp_methods */
    2240         0,                              /* tp_members */
    2241         0,                              /* tp_getset */
    2242         0,                              /* tp_base */
    2243         0,                              /* tp_dict */
    2244         0,                              /* tp_descr_get */
    2245         0,                              /* tp_descr_set */
    2246         0,                              /* tp_dictoffset */
    2247         0,                              /* tp_init */
    2248         PyType_GenericAlloc,            /* tp_alloc */
    2249         frozenset_new,                  /* tp_new */
    2250         PyObject_GC_Del,                /* tp_free */
     2219    PyVarObject_HEAD_INIT(&PyType_Type, 0)
     2220    "frozenset",                        /* tp_name */
     2221    sizeof(PySetObject),                /* tp_basicsize */
     2222    0,                                  /* tp_itemsize */
     2223    /* methods */
     2224    (destructor)set_dealloc,            /* tp_dealloc */
     2225    (printfunc)set_tp_print,            /* tp_print */
     2226    0,                                  /* tp_getattr */
     2227    0,                                  /* tp_setattr */
     2228    set_nocmp,                          /* tp_compare */
     2229    (reprfunc)set_repr,                 /* tp_repr */
     2230    &frozenset_as_number,               /* tp_as_number */
     2231    &set_as_sequence,                   /* tp_as_sequence */
     2232    0,                                  /* tp_as_mapping */
     2233    frozenset_hash,                     /* tp_hash */
     2234    0,                                  /* tp_call */
     2235    0,                                  /* tp_str */
     2236    PyObject_GenericGetAttr,            /* tp_getattro */
     2237    0,                                  /* tp_setattro */
     2238    0,                                  /* tp_as_buffer */
     2239    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES |
     2240        Py_TPFLAGS_BASETYPE,            /* tp_flags */
     2241    frozenset_doc,                      /* tp_doc */
     2242    (traverseproc)set_traverse,         /* tp_traverse */
     2243    (inquiry)set_clear_internal,        /* tp_clear */
     2244    (richcmpfunc)set_richcompare,       /* tp_richcompare */
     2245    offsetof(PySetObject, weakreflist),         /* tp_weaklistoffset */
     2246    (getiterfunc)set_iter,              /* tp_iter */
     2247    0,                                  /* tp_iternext */
     2248    frozenset_methods,                  /* tp_methods */
     2249    0,                                  /* tp_members */
     2250    0,                                  /* tp_getset */
     2251    0,                                  /* tp_base */
     2252    0,                                  /* tp_dict */
     2253    0,                                  /* tp_descr_get */
     2254    0,                                  /* tp_descr_set */
     2255    0,                                  /* tp_dictoffset */
     2256    0,                                  /* tp_init */
     2257    PyType_GenericAlloc,                /* tp_alloc */
     2258    frozenset_new,                      /* tp_new */
     2259    PyObject_GC_Del,                    /* tp_free */
    22512260};
    22522261
     
    22572266PySet_New(PyObject *iterable)
    22582267{
    2259         return make_new_set(&PySet_Type, iterable);
     2268    return make_new_set(&PySet_Type, iterable);
    22602269}
    22612270
     
    22632272PyFrozenSet_New(PyObject *iterable)
    22642273{
    2265         return make_new_set(&PyFrozenSet_Type, iterable);
     2274    return make_new_set(&PyFrozenSet_Type, iterable);
    22662275}
    22672276
     
    22692278PySet_Size(PyObject *anyset)
    22702279{
    2271         if (!PyAnySet_Check(anyset)) {
    2272                 PyErr_BadInternalCall();
    2273                 return -1;
    2274         }
    2275         return PySet_GET_SIZE(anyset);
     2280    if (!PyAnySet_Check(anyset)) {
     2281        PyErr_BadInternalCall();
     2282        return -1;
     2283    }
     2284    return PySet_GET_SIZE(anyset);
    22762285}
    22772286
     
    22792288PySet_Clear(PyObject *set)
    22802289{
    2281         if (!PySet_Check(set)) {
    2282                 PyErr_BadInternalCall();
    2283                 return -1;
    2284         }
    2285         return set_clear_internal((PySetObject *)set);
     2290    if (!PySet_Check(set)) {
     2291        PyErr_BadInternalCall();
     2292        return -1;
     2293    }
     2294    return set_clear_internal((PySetObject *)set);
    22862295}
    22872296
     
    22892298PySet_Contains(PyObject *anyset, PyObject *key)
    22902299{
    2291         if (!PyAnySet_Check(anyset)) {
    2292                 PyErr_BadInternalCall();
    2293                 return -1;
    2294         }
    2295         return set_contains_key((PySetObject *)anyset, key);
     2300    if (!PyAnySet_Check(anyset)) {
     2301        PyErr_BadInternalCall();
     2302        return -1;
     2303    }
     2304    return set_contains_key((PySetObject *)anyset, key);
    22962305}
    22972306
     
    22992308PySet_Discard(PyObject *set, PyObject *key)
    23002309{
    2301         if (!PySet_Check(set)) {
    2302                 PyErr_BadInternalCall();
    2303                 return -1;
    2304         }
    2305         return set_discard_key((PySetObject *)set, key);
     2310    if (!PySet_Check(set)) {
     2311        PyErr_BadInternalCall();
     2312        return -1;
     2313    }
     2314    return set_discard_key((PySetObject *)set, key);
    23062315}
    23072316
     
    23092318PySet_Add(PyObject *anyset, PyObject *key)
    23102319{
    2311         if (!PySet_Check(anyset) &&
    2312             (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
    2313                 PyErr_BadInternalCall();
    2314                 return -1;
    2315         }
    2316         return set_add_key((PySetObject *)anyset, key);
     2320    if (!PySet_Check(anyset) &&
     2321        (!PyFrozenSet_Check(anyset) || Py_REFCNT(anyset) != 1)) {
     2322        PyErr_BadInternalCall();
     2323        return -1;
     2324    }
     2325    return set_add_key((PySetObject *)anyset, key);
    23172326}
    23182327
     
    23202329_PySet_Next(PyObject *set, Py_ssize_t *pos, PyObject **key)
    23212330{
    2322         setentry *entry_ptr;
    2323 
    2324         if (!PyAnySet_Check(set)) {
    2325                 PyErr_BadInternalCall();
    2326                 return -1;
    2327         }
    2328         if (set_next((PySetObject *)set, pos, &entry_ptr) == 0)
    2329                 return 0;
    2330         *key = entry_ptr->key;
    2331         return 1;
     2331    setentry *entry_ptr;
     2332
     2333    if (!PyAnySet_Check(set)) {
     2334        PyErr_BadInternalCall();
     2335        return -1;
     2336    }
     2337    if (set_next((PySetObject *)set, pos, &entry_ptr) == 0)
     2338        return 0;
     2339    *key = entry_ptr->key;
     2340    return 1;
    23322341}
    23332342
     
    23352344_PySet_NextEntry(PyObject *set, Py_ssize_t *pos, PyObject **key, long *hash)
    23362345{
    2337         setentry *entry;
    2338 
    2339         if (!PyAnySet_Check(set)) {
    2340                 PyErr_BadInternalCall();
    2341                 return -1;
    2342         }
    2343         if (set_next((PySetObject *)set, pos, &entry) == 0)
    2344                 return 0;
    2345         *key = entry->key;
    2346         *hash = entry->hash;
    2347         return 1;
     2346    setentry *entry;
     2347
     2348    if (!PyAnySet_Check(set)) {
     2349        PyErr_BadInternalCall();
     2350        return -1;
     2351    }
     2352    if (set_next((PySetObject *)set, pos, &entry) == 0)
     2353        return 0;
     2354    *key = entry->key;
     2355    *hash = entry->hash;
     2356    return 1;
    23482357}
    23492358
     
    23512360PySet_Pop(PyObject *set)
    23522361{
    2353         if (!PySet_Check(set)) {
    2354                 PyErr_BadInternalCall();
    2355                 return NULL;
    2356         }
    2357         return set_pop((PySetObject *)set);
     2362    if (!PySet_Check(set)) {
     2363        PyErr_BadInternalCall();
     2364        return NULL;
     2365    }
     2366    return set_pop((PySetObject *)set);
    23582367}
    23592368
     
    23612370_PySet_Update(PyObject *set, PyObject *iterable)
    23622371{
    2363         if (!PySet_Check(set)) {
    2364                 PyErr_BadInternalCall();
    2365                 return -1;
    2366         }
    2367         return set_update_internal((PySetObject *)set, iterable);
     2372    if (!PySet_Check(set)) {
     2373        PyErr_BadInternalCall();
     2374        return -1;
     2375    }
     2376    return set_update_internal((PySetObject *)set, iterable);
    23682377}
    23692378
    23702379#ifdef Py_DEBUG
    23712380
    2372 /* Test code to be called with any three element set. 
     2381/* Test code to be called with any three element set.
    23732382   Returns True and original set is restored. */
    23742383
    2375 #define assertRaises(call_return_value, exception)              \
    2376         do {                                                    \
    2377                 assert(call_return_value);                      \
    2378                 assert(PyErr_ExceptionMatches(exception));      \
    2379                 PyErr_Clear();                                  \
    2380         } while(0)
     2384#define assertRaises(call_return_value, exception)              \
     2385    do {                                                        \
     2386        assert(call_return_value);                              \
     2387        assert(PyErr_ExceptionMatches(exception));              \
     2388        PyErr_Clear();                                          \
     2389    } while(0)
    23812390
    23822391static PyObject *
    23832392test_c_api(PySetObject *so)
    23842393{
    2385         Py_ssize_t count;
    2386         char *s;
    2387         Py_ssize_t i;
    2388         PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x;
    2389         PyObject *ob = (PyObject *)so;
    2390 
    2391         /* Verify preconditions and exercise type/size checks */
    2392         assert(PyAnySet_Check(ob));
    2393         assert(PyAnySet_CheckExact(ob));
    2394         assert(!PyFrozenSet_CheckExact(ob));
    2395         assert(PySet_Size(ob) == 3);
    2396         assert(PySet_GET_SIZE(ob) == 3);
    2397 
    2398         /* Raise TypeError for non-iterable constructor arguments */
    2399         assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
    2400         assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
    2401 
    2402         /* Raise TypeError for unhashable key */
    2403         dup = PySet_New(ob);
    2404         assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
    2405         assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
    2406         assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
    2407 
    2408         /* Exercise successful pop, contains, add, and discard */
    2409         elem = PySet_Pop(ob);
    2410         assert(PySet_Contains(ob, elem) == 0);
    2411         assert(PySet_GET_SIZE(ob) == 2);
    2412         assert(PySet_Add(ob, elem) == 0);
    2413         assert(PySet_Contains(ob, elem) == 1);
    2414         assert(PySet_GET_SIZE(ob) == 3);
    2415         assert(PySet_Discard(ob, elem) == 1);
    2416         assert(PySet_GET_SIZE(ob) == 2);
    2417         assert(PySet_Discard(ob, elem) == 0);
    2418         assert(PySet_GET_SIZE(ob) == 2);
    2419 
    2420         /* Exercise clear */
    2421         dup2 = PySet_New(dup);
    2422         assert(PySet_Clear(dup2) == 0);
    2423         assert(PySet_Size(dup2) == 0);
    2424         Py_DECREF(dup2);
    2425 
    2426         /* Raise SystemError on clear or update of frozen set */
    2427         f = PyFrozenSet_New(dup);
    2428         assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
    2429         assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
    2430         assert(PySet_Add(f, elem) == 0);
    2431         Py_INCREF(f);
    2432         assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
    2433         Py_DECREF(f);
    2434         Py_DECREF(f);
    2435 
    2436         /* Exercise direct iteration */
    2437         i = 0, count = 0;
    2438         while (_PySet_Next((PyObject *)dup, &i, &x)) {
    2439                 s = PyString_AsString(x);
    2440                 assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
    2441                 count++;
    2442         }
    2443         assert(count == 3);
    2444 
    2445         /* Exercise updates */
    2446         dup2 = PySet_New(NULL);
    2447         assert(_PySet_Update(dup2, dup) == 0);
    2448         assert(PySet_Size(dup2) == 3);
    2449         assert(_PySet_Update(dup2, dup) == 0);
    2450         assert(PySet_Size(dup2) == 3);
    2451         Py_DECREF(dup2);
    2452 
    2453         /* Raise SystemError when self argument is not a set or frozenset. */
    2454         t = PyTuple_New(0);
    2455         assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
    2456         assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
    2457         Py_DECREF(t);
    2458 
    2459         /* Raise SystemError when self argument is not a set. */
    2460         f = PyFrozenSet_New(dup);
    2461         assert(PySet_Size(f) == 3);
    2462         assert(PyFrozenSet_CheckExact(f));
    2463         assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
    2464         assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
    2465         Py_DECREF(f);
    2466 
    2467         /* Raise KeyError when popping from an empty set */
    2468         assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
    2469         Py_DECREF(ob);
    2470         assert(PySet_GET_SIZE(ob) == 0);
    2471         assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
    2472 
    2473         /* Restore the set from the copy using the PyNumber API */
    2474         assert(PyNumber_InPlaceOr(ob, dup) == ob);
    2475         Py_DECREF(ob);
    2476 
    2477         /* Verify constructors accept NULL arguments */
    2478         f = PySet_New(NULL);
    2479         assert(f != NULL);
    2480         assert(PySet_GET_SIZE(f) == 0);
    2481         Py_DECREF(f);
    2482         f = PyFrozenSet_New(NULL);
    2483         assert(f != NULL);
    2484         assert(PyFrozenSet_CheckExact(f));
    2485         assert(PySet_GET_SIZE(f) == 0);
    2486         Py_DECREF(f);
    2487 
    2488         Py_DECREF(elem);
    2489         Py_DECREF(dup);
    2490         Py_RETURN_TRUE;
     2394    Py_ssize_t count;
     2395    char *s;
     2396    Py_ssize_t i;
     2397    PyObject *elem=NULL, *dup=NULL, *t, *f, *dup2, *x;
     2398    PyObject *ob = (PyObject *)so;
     2399    PyObject *str;
     2400
     2401    /* Verify preconditions */
     2402    assert(PyAnySet_Check(ob));
     2403    assert(PyAnySet_CheckExact(ob));
     2404    assert(!PyFrozenSet_CheckExact(ob));
     2405
     2406    /* so.clear(); so |= set("abc"); */
     2407    str = PyString_FromString("abc");
     2408    if (str == NULL)
     2409        return NULL;
     2410    set_clear_internal(so);
     2411    if (set_update_internal(so, str) == -1) {
     2412        Py_DECREF(str);
     2413        return NULL;
     2414    }
     2415    Py_DECREF(str);
     2416
     2417    /* Exercise type/size checks */
     2418    assert(PySet_Size(ob) == 3);
     2419    assert(PySet_GET_SIZE(ob) == 3);
     2420
     2421    /* Raise TypeError for non-iterable constructor arguments */
     2422    assertRaises(PySet_New(Py_None) == NULL, PyExc_TypeError);
     2423    assertRaises(PyFrozenSet_New(Py_None) == NULL, PyExc_TypeError);
     2424
     2425    /* Raise TypeError for unhashable key */
     2426    dup = PySet_New(ob);
     2427    assertRaises(PySet_Discard(ob, dup) == -1, PyExc_TypeError);
     2428    assertRaises(PySet_Contains(ob, dup) == -1, PyExc_TypeError);
     2429    assertRaises(PySet_Add(ob, dup) == -1, PyExc_TypeError);
     2430
     2431    /* Exercise successful pop, contains, add, and discard */
     2432    elem = PySet_Pop(ob);
     2433    assert(PySet_Contains(ob, elem) == 0);
     2434    assert(PySet_GET_SIZE(ob) == 2);
     2435    assert(PySet_Add(ob, elem) == 0);
     2436    assert(PySet_Contains(ob, elem) == 1);
     2437    assert(PySet_GET_SIZE(ob) == 3);
     2438    assert(PySet_Discard(ob, elem) == 1);
     2439    assert(PySet_GET_SIZE(ob) == 2);
     2440    assert(PySet_Discard(ob, elem) == 0);
     2441    assert(PySet_GET_SIZE(ob) == 2);
     2442
     2443    /* Exercise clear */
     2444    dup2 = PySet_New(dup);
     2445    assert(PySet_Clear(dup2) == 0);
     2446    assert(PySet_Size(dup2) == 0);
     2447    Py_DECREF(dup2);
     2448
     2449    /* Raise SystemError on clear or update of frozen set */
     2450    f = PyFrozenSet_New(dup);
     2451    assertRaises(PySet_Clear(f) == -1, PyExc_SystemError);
     2452    assertRaises(_PySet_Update(f, dup) == -1, PyExc_SystemError);
     2453    assert(PySet_Add(f, elem) == 0);
     2454    Py_INCREF(f);
     2455    assertRaises(PySet_Add(f, elem) == -1, PyExc_SystemError);
     2456    Py_DECREF(f);
     2457    Py_DECREF(f);
     2458
     2459    /* Exercise direct iteration */
     2460    i = 0, count = 0;
     2461    while (_PySet_Next((PyObject *)dup, &i, &x)) {
     2462        s = PyString_AsString(x);
     2463        assert(s && (s[0] == 'a' || s[0] == 'b' || s[0] == 'c'));
     2464        count++;
     2465    }
     2466    assert(count == 3);
     2467
     2468    /* Exercise updates */
     2469    dup2 = PySet_New(NULL);
     2470    assert(_PySet_Update(dup2, dup) == 0);
     2471    assert(PySet_Size(dup2) == 3);
     2472    assert(_PySet_Update(dup2, dup) == 0);
     2473    assert(PySet_Size(dup2) == 3);
     2474    Py_DECREF(dup2);
     2475
     2476    /* Raise SystemError when self argument is not a set or frozenset. */
     2477    t = PyTuple_New(0);
     2478    assertRaises(PySet_Size(t) == -1, PyExc_SystemError);
     2479    assertRaises(PySet_Contains(t, elem) == -1, PyExc_SystemError);
     2480    Py_DECREF(t);
     2481
     2482    /* Raise SystemError when self argument is not a set. */
     2483    f = PyFrozenSet_New(dup);
     2484    assert(PySet_Size(f) == 3);
     2485    assert(PyFrozenSet_CheckExact(f));
     2486    assertRaises(PySet_Discard(f, elem) == -1, PyExc_SystemError);
     2487    assertRaises(PySet_Pop(f) == NULL, PyExc_SystemError);
     2488    Py_DECREF(f);
     2489
     2490    /* Raise KeyError when popping from an empty set */
     2491    assert(PyNumber_InPlaceSubtract(ob, ob) == ob);
     2492    Py_DECREF(ob);
     2493    assert(PySet_GET_SIZE(ob) == 0);
     2494    assertRaises(PySet_Pop(ob) == NULL, PyExc_KeyError);
     2495
     2496    /* Restore the set from the copy using the PyNumber API */
     2497    assert(PyNumber_InPlaceOr(ob, dup) == ob);
     2498    Py_DECREF(ob);
     2499
     2500    /* Verify constructors accept NULL arguments */
     2501    f = PySet_New(NULL);
     2502    assert(f != NULL);
     2503    assert(PySet_GET_SIZE(f) == 0);
     2504    Py_DECREF(f);
     2505    f = PyFrozenSet_New(NULL);
     2506    assert(f != NULL);
     2507    assert(PyFrozenSet_CheckExact(f));
     2508    assert(PySet_GET_SIZE(f) == 0);
     2509    Py_DECREF(f);
     2510
     2511    Py_DECREF(elem);
     2512    Py_DECREF(dup);
     2513    Py_RETURN_TRUE;
    24912514}
    24922515
Note: See TracChangeset for help on using the changeset viewer.