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

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Objects/dictobject.c

    r2 r391  
    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
     
    142142_PyDict_Dummy(void)
    143143{
    144         return dummy;
     144    return dummy;
    145145}
    146146#endif
     
    157157show_counts(void)
    158158{
    159         fprintf(stderr, "created %ld string dicts\n", created);
    160         fprintf(stderr, "converted %ld to normal dicts\n", converted);
    161         fprintf(stderr, "%.2f%% conversion rate\n", (100.0*converted)/created);
     159    fprintf(stderr, "created %ld string dicts\n", created);
     160    fprintf(stderr, "converted %ld to normal dicts\n", converted);
     161    fprintf(stderr, "%.2f%% conversion rate\n", (100.0*converted)/created);
    162162}
    163163#endif
     
    172172show_alloc(void)
    173173{
    174         fprintf(stderr, "Dict allocations: %" PY_FORMAT_SIZE_T "d\n",
    175                 count_alloc);
    176         fprintf(stderr, "Dict reuse through freelist: %" PY_FORMAT_SIZE_T
    177                 "d\n", count_reuse);
    178         fprintf(stderr, "%.2f%% reuse rate\n\n",
    179                 (100.0*count_reuse/(count_alloc+count_reuse)));
     174    fprintf(stderr, "Dict allocations: %" PY_FORMAT_SIZE_T "d\n",
     175        count_alloc);
     176    fprintf(stderr, "Dict reuse through freelist: %" PY_FORMAT_SIZE_T
     177        "d\n", count_reuse);
     178    fprintf(stderr, "%.2f%% reuse rate\n\n",
     179        (100.0*count_reuse/(count_alloc+count_reuse)));
    180180}
    181181#endif
     182
     183/* Debug statistic to count GC tracking of dicts */
     184#ifdef SHOW_TRACK_COUNT
     185static Py_ssize_t count_untracked = 0;
     186static Py_ssize_t count_tracked = 0;
     187
     188static void
     189show_track(void)
     190{
     191    fprintf(stderr, "Dicts created: %" PY_FORMAT_SIZE_T "d\n",
     192        count_tracked + count_untracked);
     193    fprintf(stderr, "Dicts tracked by the GC: %" PY_FORMAT_SIZE_T
     194        "d\n", count_tracked);
     195    fprintf(stderr, "%.2f%% dict tracking rate\n\n",
     196        (100.0*count_tracked/(count_untracked+count_tracked)));
     197}
     198#endif
     199
    182200
    183201/* Initialization macros.
     
    190208*/
    191209
    192 #define INIT_NONZERO_DICT_SLOTS(mp) do {                                \
    193         (mp)->ma_table = (mp)->ma_smalltable;                           \
    194         (mp)->ma_mask = PyDict_MINSIZE - 1;                             \
     210#define INIT_NONZERO_DICT_SLOTS(mp) do {                                \
     211    (mp)->ma_table = (mp)->ma_smalltable;                               \
     212    (mp)->ma_mask = PyDict_MINSIZE - 1;                                 \
    195213    } while(0)
    196214
    197 #define EMPTY_TO_MINSIZE(mp) do {                                       \
    198         memset((mp)->ma_smalltable, 0, sizeof((mp)->ma_smalltable));    \
    199         (mp)->ma_used = (mp)->ma_fill = 0;                              \
    200         INIT_NONZERO_DICT_SLOTS(mp);                                    \
     215#define EMPTY_TO_MINSIZE(mp) do {                                       \
     216    memset((mp)->ma_smalltable, 0, sizeof((mp)->ma_smalltable));        \
     217    (mp)->ma_used = (mp)->ma_fill = 0;                                  \
     218    INIT_NONZERO_DICT_SLOTS(mp);                                        \
    201219    } while(0)
    202220
     
    211229PyDict_Fini(void)
    212230{
    213         PyDictObject *op;
    214 
    215         while (numfree) {
    216                 op = free_list[--numfree];
    217                 assert(PyDict_CheckExact(op));
    218                 PyObject_GC_Del(op);
    219         }
     231    PyDictObject *op;
     232
     233    while (numfree) {
     234        op = free_list[--numfree];
     235        assert(PyDict_CheckExact(op));
     236        PyObject_GC_Del(op);
     237    }
    220238}
    221239
     
    223241PyDict_New(void)
    224242{
    225         register PyDictObject *mp;
    226         if (dummy == NULL) { /* Auto-initialize dummy */
    227                 dummy = PyString_FromString("<dummy key>");
    228                 if (dummy == NULL)
    229                         return NULL;
     243    register PyDictObject *mp;
     244    if (dummy == NULL) { /* Auto-initialize dummy */
     245        dummy = PyString_FromString("<dummy key>");
     246        if (dummy == NULL)
     247            return NULL;
    230248#ifdef SHOW_CONVERSION_COUNTS
    231                 Py_AtExit(show_counts);
     249        Py_AtExit(show_counts);
    232250#endif
    233251#ifdef SHOW_ALLOC_COUNT
    234                 Py_AtExit(show_alloc);
     252        Py_AtExit(show_alloc);
    235253#endif
    236         }
    237         if (numfree) {
    238                 mp = free_list[--numfree];
    239                 assert (mp != NULL);
    240                 assert (Py_TYPE(mp) == &PyDict_Type);
    241                 _Py_NewReference((PyObject *)mp);
    242                 if (mp->ma_fill) {
    243                         EMPTY_TO_MINSIZE(mp);
    244                 } else {
    245                         /* At least set ma_table and ma_mask; these are wrong
    246                            if an empty but presized dict is added to freelist */
    247                         INIT_NONZERO_DICT_SLOTS(mp);
    248                 }
    249                 assert (mp->ma_used == 0);
    250                 assert (mp->ma_table == mp->ma_smalltable);
    251                 assert (mp->ma_mask == PyDict_MINSIZE - 1);
     254#ifdef SHOW_TRACK_COUNT
     255        Py_AtExit(show_track);
     256#endif
     257    }
     258    if (numfree) {
     259        mp = free_list[--numfree];
     260        assert (mp != NULL);
     261        assert (Py_TYPE(mp) == &PyDict_Type);
     262        _Py_NewReference((PyObject *)mp);
     263        if (mp->ma_fill) {
     264            EMPTY_TO_MINSIZE(mp);
     265        } else {
     266            /* At least set ma_table and ma_mask; these are wrong
     267               if an empty but presized dict is added to freelist */
     268            INIT_NONZERO_DICT_SLOTS(mp);
     269        }
     270        assert (mp->ma_used == 0);
     271        assert (mp->ma_table == mp->ma_smalltable);
     272        assert (mp->ma_mask == PyDict_MINSIZE - 1);
    252273#ifdef SHOW_ALLOC_COUNT
    253                 count_reuse++;
     274        count_reuse++;
    254275#endif
    255         } else {
    256                 mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
    257                 if (mp == NULL)
    258                         return NULL;
    259                 EMPTY_TO_MINSIZE(mp);
     276    } else {
     277        mp = PyObject_GC_New(PyDictObject, &PyDict_Type);
     278        if (mp == NULL)
     279            return NULL;
     280        EMPTY_TO_MINSIZE(mp);
    260281#ifdef SHOW_ALLOC_COUNT
    261                 count_alloc++;
     282        count_alloc++;
    262283#endif
    263         }
    264         mp->ma_lookup = lookdict_string;
     284    }
     285    mp->ma_lookup = lookdict_string;
     286#ifdef SHOW_TRACK_COUNT
     287    count_untracked++;
     288#endif
    265289#ifdef SHOW_CONVERSION_COUNTS
    266         ++created;
     290    ++created;
    267291#endif
    268         _PyObject_GC_TRACK(mp);
    269         return (PyObject *)mp;
     292    return (PyObject *)mp;
    270293}
    271294
     
    297320lookdict(PyDictObject *mp, PyObject *key, register long hash)
    298321{
    299         register size_t i;
    300         register size_t perturb;
    301         register PyDictEntry *freeslot;
    302         register size_t mask = (size_t)mp->ma_mask;
    303         PyDictEntry *ep0 = mp->ma_table;
    304         register PyDictEntry *ep;
    305         register int cmp;
    306         PyObject *startkey;
    307 
    308         i = (size_t)hash & mask;
    309         ep = &ep0[i];
    310         if (ep->me_key == NULL || ep->me_key == key)
    311                 return ep;
    312 
    313         if (ep->me_key == dummy)
    314                 freeslot = ep;
    315         else {
    316                 if (ep->me_hash == hash) {
    317                         startkey = ep->me_key;
    318                         Py_INCREF(startkey);
    319                         cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
    320                         Py_DECREF(startkey);
    321                         if (cmp < 0)
    322                                 return NULL;
    323                         if (ep0 == mp->ma_table && ep->me_key == startkey) {
    324                                 if (cmp > 0)
    325                                         return ep;
    326                         }
    327                         else {
    328                                 /* The compare did major nasty stuff to the
    329                                 * dict:  start over.
    330                                 * XXX A clever adversary could prevent this
    331                                 * XXX from terminating.
    332                                  */
    333                                 return lookdict(mp, key, hash);
    334                         }
    335                 }
    336                 freeslot = NULL;
    337         }
    338 
    339         /* In the loop, me_key == dummy is by far (factor of 100s) the
    340            least likely outcome, so test for that last. */
    341         for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
    342                 i = (i << 2) + i + perturb + 1;
    343                 ep = &ep0[i & mask];
    344                 if (ep->me_key == NULL)
    345                         return freeslot == NULL ? ep : freeslot;
    346                 if (ep->me_key == key)
    347                         return ep;
    348                 if (ep->me_hash == hash && ep->me_key != dummy) {
    349                         startkey = ep->me_key;
    350                         Py_INCREF(startkey);
    351                         cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
    352                         Py_DECREF(startkey);
    353                         if (cmp < 0)
    354                                 return NULL;
    355                         if (ep0 == mp->ma_table && ep->me_key == startkey) {
    356                                 if (cmp > 0)
    357                                         return ep;
    358                         }
    359                         else {
    360                                 /* The compare did major nasty stuff to the
    361                                 * dict:  start over.
    362                                 * XXX A clever adversary could prevent this
    363                                 * XXX from terminating.
    364                                  */
    365                                 return lookdict(mp, key, hash);
    366                         }
    367                 }
    368                 else if (ep->me_key == dummy && freeslot == NULL)
    369                         freeslot = ep;
    370         }
    371         assert(0);      /* NOT REACHED */
    372         return 0;
     322    register size_t i;
     323    register size_t perturb;
     324    register PyDictEntry *freeslot;
     325    register size_t mask = (size_t)mp->ma_mask;
     326    PyDictEntry *ep0 = mp->ma_table;
     327    register PyDictEntry *ep;
     328    register int cmp;
     329    PyObject *startkey;
     330
     331    i = (size_t)hash & mask;
     332    ep = &ep0[i];
     333    if (ep->me_key == NULL || ep->me_key == key)
     334        return ep;
     335
     336    if (ep->me_key == dummy)
     337        freeslot = ep;
     338    else {
     339        if (ep->me_hash == hash) {
     340            startkey = ep->me_key;
     341            Py_INCREF(startkey);
     342            cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
     343            Py_DECREF(startkey);
     344            if (cmp < 0)
     345                return NULL;
     346            if (ep0 == mp->ma_table && ep->me_key == startkey) {
     347                if (cmp > 0)
     348                    return ep;
     349            }
     350            else {
     351                /* The compare did major nasty stuff to the
     352                * dict:  start over.
     353                * XXX A clever adversary could prevent this
     354                * XXX from terminating.
     355                 */
     356                return lookdict(mp, key, hash);
     357            }
     358        }
     359        freeslot = NULL;
     360    }
     361
     362    /* In the loop, me_key == dummy is by far (factor of 100s) the
     363       least likely outcome, so test for that last. */
     364    for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
     365        i = (i << 2) + i + perturb + 1;
     366        ep = &ep0[i & mask];
     367        if (ep->me_key == NULL)
     368            return freeslot == NULL ? ep : freeslot;
     369        if (ep->me_key == key)
     370            return ep;
     371        if (ep->me_hash == hash && ep->me_key != dummy) {
     372            startkey = ep->me_key;
     373            Py_INCREF(startkey);
     374            cmp = PyObject_RichCompareBool(startkey, key, Py_EQ);
     375            Py_DECREF(startkey);
     376            if (cmp < 0)
     377                return NULL;
     378            if (ep0 == mp->ma_table && ep->me_key == startkey) {
     379                if (cmp > 0)
     380                    return ep;
     381            }
     382            else {
     383                /* The compare did major nasty stuff to the
     384                * dict:  start over.
     385                * XXX A clever adversary could prevent this
     386                * XXX from terminating.
     387                 */
     388                return lookdict(mp, key, hash);
     389            }
     390        }
     391        else if (ep->me_key == dummy && freeslot == NULL)
     392            freeslot = ep;
     393    }
     394    assert(0);          /* NOT REACHED */
     395    return 0;
    373396}
    374397
     
    385408lookdict_string(PyDictObject *mp, PyObject *key, register long hash)
    386409{
    387         register size_t i;
    388         register size_t perturb;
    389         register PyDictEntry *freeslot;
    390         register size_t mask = (size_t)mp->ma_mask;
    391         PyDictEntry *ep0 = mp->ma_table;
    392         register PyDictEntry *ep;
    393 
    394         /* Make sure this function doesn't have to handle non-string keys,
    395            including subclasses of str; e.g., one reason to subclass
    396            strings is to override __eq__, and for speed we don't cater to
    397            that here. */
    398         if (!PyString_CheckExact(key)) {
     410    register size_t i;
     411    register size_t perturb;
     412    register PyDictEntry *freeslot;
     413    register size_t mask = (size_t)mp->ma_mask;
     414    PyDictEntry *ep0 = mp->ma_table;
     415    register PyDictEntry *ep;
     416
     417    /* Make sure this function doesn't have to handle non-string keys,
     418       including subclasses of str; e.g., one reason to subclass
     419       strings is to override __eq__, and for speed we don't cater to
     420       that here. */
     421    if (!PyString_CheckExact(key)) {
    399422#ifdef SHOW_CONVERSION_COUNTS
    400                 ++converted;
     423        ++converted;
    401424#endif
    402                 mp->ma_lookup = lookdict;
    403                 return lookdict(mp, key, hash);
    404         }
    405         i = hash & mask;
    406         ep = &ep0[i];
    407         if (ep->me_key == NULL || ep->me_key == key)
    408                 return ep;
    409         if (ep->me_key == dummy)
    410                 freeslot = ep;
    411         else {
    412                 if (ep->me_hash == hash && _PyString_Eq(ep->me_key, key))
    413                         return ep;
    414                 freeslot = NULL;
    415         }
    416 
    417         /* In the loop, me_key == dummy is by far (factor of 100s) the
    418            least likely outcome, so test for that last. */
    419         for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
    420                 i = (i << 2) + i + perturb + 1;
    421                 ep = &ep0[i & mask];
    422                 if (ep->me_key == NULL)
    423                         return freeslot == NULL ? ep : freeslot;
    424                 if (ep->me_key == key
    425                     || (ep->me_hash == hash
    426                         && ep->me_key != dummy
    427                         && _PyString_Eq(ep->me_key, key)))
    428                         return ep;
    429                 if (ep->me_key == dummy && freeslot == NULL)
    430                         freeslot = ep;
    431         }
    432         assert(0);      /* NOT REACHED */
    433         return 0;
    434 }
     425        mp->ma_lookup = lookdict;
     426        return lookdict(mp, key, hash);
     427    }
     428    i = hash & mask;
     429    ep = &ep0[i];
     430    if (ep->me_key == NULL || ep->me_key == key)
     431        return ep;
     432    if (ep->me_key == dummy)
     433        freeslot = ep;
     434    else {
     435        if (ep->me_hash == hash && _PyString_Eq(ep->me_key, key))
     436            return ep;
     437        freeslot = NULL;
     438    }
     439
     440    /* In the loop, me_key == dummy is by far (factor of 100s) the
     441       least likely outcome, so test for that last. */
     442    for (perturb = hash; ; perturb >>= PERTURB_SHIFT) {
     443        i = (i << 2) + i + perturb + 1;
     444        ep = &ep0[i & mask];
     445        if (ep->me_key == NULL)
     446            return freeslot == NULL ? ep : freeslot;
     447        if (ep->me_key == key
     448            || (ep->me_hash == hash
     449            && ep->me_key != dummy
     450            && _PyString_Eq(ep->me_key, key)))
     451            return ep;
     452        if (ep->me_key == dummy && freeslot == NULL)
     453            freeslot = ep;
     454    }
     455    assert(0);          /* NOT REACHED */
     456    return 0;
     457}
     458
     459#ifdef SHOW_TRACK_COUNT
     460#define INCREASE_TRACK_COUNT \
     461    (count_tracked++, count_untracked--);
     462#define DECREASE_TRACK_COUNT \
     463    (count_tracked--, count_untracked++);
     464#else
     465#define INCREASE_TRACK_COUNT
     466#define DECREASE_TRACK_COUNT
     467#endif
     468
     469#define MAINTAIN_TRACKING(mp, key, value) \
     470    do { \
     471        if (!_PyObject_GC_IS_TRACKED(mp)) { \
     472            if (_PyObject_GC_MAY_BE_TRACKED(key) || \
     473                _PyObject_GC_MAY_BE_TRACKED(value)) { \
     474                _PyObject_GC_TRACK(mp); \
     475                INCREASE_TRACK_COUNT \
     476            } \
     477        } \
     478    } while(0)
     479
     480void
     481_PyDict_MaybeUntrack(PyObject *op)
     482{
     483    PyDictObject *mp;
     484    PyObject *value;
     485    Py_ssize_t mask, i;
     486    PyDictEntry *ep;
     487
     488    if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op))
     489        return;
     490
     491    mp = (PyDictObject *) op;
     492    ep = mp->ma_table;
     493    mask = mp->ma_mask;
     494    for (i = 0; i <= mask; i++) {
     495        if ((value = ep[i].me_value) == NULL)
     496            continue;
     497        if (_PyObject_GC_MAY_BE_TRACKED(value) ||
     498            _PyObject_GC_MAY_BE_TRACKED(ep[i].me_key))
     499            return;
     500    }
     501    DECREASE_TRACK_COUNT
     502    _PyObject_GC_UNTRACK(op);
     503}
     504
     505/*
     506Internal routine to insert a new item into the table when you have entry object.
     507Used by insertdict.
     508*/
     509static int
     510insertdict_by_entry(register PyDictObject *mp, PyObject *key, long hash,
     511                    PyDictEntry *ep, PyObject *value)
     512{
     513    PyObject *old_value;
     514
     515    MAINTAIN_TRACKING(mp, key, value);
     516    if (ep->me_value != NULL) {
     517        old_value = ep->me_value;
     518        ep->me_value = value;
     519        Py_DECREF(old_value); /* which **CAN** re-enter */
     520        Py_DECREF(key);
     521    }
     522    else {
     523        if (ep->me_key == NULL)
     524            mp->ma_fill++;
     525        else {
     526            assert(ep->me_key == dummy);
     527            Py_DECREF(dummy);
     528        }
     529        ep->me_key = key;
     530        ep->me_hash = (Py_ssize_t)hash;
     531        ep->me_value = value;
     532        mp->ma_used++;
     533    }
     534    return 0;
     535}
     536
    435537
    436538/*
     
    443545insertdict(register PyDictObject *mp, PyObject *key, long hash, PyObject *value)
    444546{
    445         PyObject *old_value;
    446         register PyDictEntry *ep;
    447         typedef PyDictEntry *(*lookupfunc)(PyDictObject *, PyObject *, long);
    448 
    449         assert(mp->ma_lookup != NULL);
    450         ep = mp->ma_lookup(mp, key, hash);
    451         if (ep == NULL) {
    452                 Py_DECREF(key);
    453                 Py_DECREF(value);
    454                 return -1;
    455         }
    456         if (ep->me_value != NULL) {
    457                 old_value = ep->me_value;
    458                 ep->me_value = value;
    459                 Py_DECREF(old_value); /* which **CAN** re-enter */
    460                 Py_DECREF(key);
    461         }
    462         else {
    463                 if (ep->me_key == NULL)
    464                         mp->ma_fill++;
    465                 else {
    466                         assert(ep->me_key == dummy);
    467                         Py_DECREF(dummy);
    468                 }
    469                 ep->me_key = key;
    470                 ep->me_hash = (Py_ssize_t)hash;
    471                 ep->me_value = value;
    472                 mp->ma_used++;
    473         }
    474         return 0;
     547    register PyDictEntry *ep;
     548
     549    assert(mp->ma_lookup != NULL);
     550    ep = mp->ma_lookup(mp, key, hash);
     551    if (ep == NULL) {
     552        Py_DECREF(key);
     553        Py_DECREF(value);
     554        return -1;
     555    }
     556    return insertdict_by_entry(mp, key, hash, ep, value);
    475557}
    476558
     
    485567static void
    486568insertdict_clean(register PyDictObject *mp, PyObject *key, long hash,
    487                  PyObject *value)
    488 {
    489         register size_t i;
    490         register size_t perturb;
    491         register size_t mask = (size_t)mp->ma_mask;
    492         PyDictEntry *ep0 = mp->ma_table;
    493         register PyDictEntry *ep;
    494 
    495         i = hash & mask;
    496         ep = &ep0[i];
    497         for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
    498                 i = (i << 2) + i + perturb + 1;
    499                 ep = &ep0[i & mask];
    500         }
    501         assert(ep->me_value == NULL);
    502         mp->ma_fill++;
    503         ep->me_key = key;
    504         ep->me_hash = (Py_ssize_t)hash;
    505         ep->me_value = value;
    506         mp->ma_used++;
     569                 PyObject *value)
     570{
     571    register size_t i;
     572    register size_t perturb;
     573    register size_t mask = (size_t)mp->ma_mask;
     574    PyDictEntry *ep0 = mp->ma_table;
     575    register PyDictEntry *ep;
     576
     577    MAINTAIN_TRACKING(mp, key, value);
     578    i = hash & mask;
     579    ep = &ep0[i];
     580    for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) {
     581        i = (i << 2) + i + perturb + 1;
     582        ep = &ep0[i & mask];
     583    }
     584    assert(ep->me_value == NULL);
     585    mp->ma_fill++;
     586    ep->me_key = key;
     587    ep->me_hash = (Py_ssize_t)hash;
     588    ep->me_value = value;
     589    mp->ma_used++;
    507590}
    508591
     
    515598dictresize(PyDictObject *mp, Py_ssize_t minused)
    516599{
    517         Py_ssize_t newsize;
    518         PyDictEntry *oldtable, *newtable, *ep;
    519         Py_ssize_t i;
    520         int is_oldtable_malloced;
    521         PyDictEntry small_copy[PyDict_MINSIZE];
    522 
    523         assert(minused >= 0);
    524 
    525         /* Find the smallest table size > minused. */
    526         for (newsize = PyDict_MINSIZE;
    527              newsize <= minused && newsize > 0;
    528              newsize <<= 1)
    529                 ;
    530         if (newsize <= 0) {
    531                 PyErr_NoMemory();
    532                 return -1;
    533         }
    534 
    535         /* Get space for a new table. */
    536         oldtable = mp->ma_table;
    537         assert(oldtable != NULL);
    538         is_oldtable_malloced = oldtable != mp->ma_smalltable;
    539 
    540         if (newsize == PyDict_MINSIZE) {
    541                 /* A large table is shrinking, or we can't get any smaller. */
    542                 newtable = mp->ma_smalltable;
    543                 if (newtable == oldtable) {
    544                         if (mp->ma_fill == mp->ma_used) {
    545                                 /* No dummies, so no point doing anything. */
    546                                 return 0;
    547                         }
    548                         /* We're not going to resize it, but rebuild the
    549                            table anyway to purge old dummy entries.
    550                            Subtle:  This is *necessary* if fill==size,
    551                            as lookdict needs at least one virgin slot to
    552                            terminate failing searches.  If fill < size, it's
    553                            merely desirable, as dummies slow searches. */
    554                         assert(mp->ma_fill > mp->ma_used);
    555                         memcpy(small_copy, oldtable, sizeof(small_copy));
    556                         oldtable = small_copy;
    557                 }
    558         }
    559         else {
    560                 newtable = PyMem_NEW(PyDictEntry, newsize);
    561                 if (newtable == NULL) {
    562                         PyErr_NoMemory();
    563                         return -1;
    564                 }
    565         }
    566 
    567         /* Make the dict empty, using the new table. */
    568         assert(newtable != oldtable);
    569         mp->ma_table = newtable;
    570         mp->ma_mask = newsize - 1;
    571         memset(newtable, 0, sizeof(PyDictEntry) * newsize);
    572         mp->ma_used = 0;
    573         i = mp->ma_fill;
    574         mp->ma_fill = 0;
    575 
    576         /* Copy the data over; this is refcount-neutral for active entries;
    577            dummy entries aren't copied over, of course */
    578         for (ep = oldtable; i > 0; ep++) {
    579                 if (ep->me_value != NULL) {     /* active entry */
    580                         --i;
    581                         insertdict_clean(mp, ep->me_key, (long)ep->me_hash,
    582                                         ep->me_value);
    583                 }
    584                 else if (ep->me_key != NULL) {  /* dummy entry */
    585                         --i;
    586                         assert(ep->me_key == dummy);
    587                         Py_DECREF(ep->me_key);
    588                 }
    589                 /* else key == value == NULL:  nothing to do */
    590         }
    591 
    592         if (is_oldtable_malloced)
    593                 PyMem_DEL(oldtable);
    594         return 0;
     600    Py_ssize_t newsize;
     601    PyDictEntry *oldtable, *newtable, *ep;
     602    Py_ssize_t i;
     603    int is_oldtable_malloced;
     604    PyDictEntry small_copy[PyDict_MINSIZE];
     605
     606    assert(minused >= 0);
     607
     608    /* Find the smallest table size > minused. */
     609    for (newsize = PyDict_MINSIZE;
     610         newsize <= minused && newsize > 0;
     611         newsize <<= 1)
     612        ;
     613    if (newsize <= 0) {
     614        PyErr_NoMemory();
     615        return -1;
     616    }
     617
     618    /* Get space for a new table. */
     619    oldtable = mp->ma_table;
     620    assert(oldtable != NULL);
     621    is_oldtable_malloced = oldtable != mp->ma_smalltable;
     622
     623    if (newsize == PyDict_MINSIZE) {
     624        /* A large table is shrinking, or we can't get any smaller. */
     625        newtable = mp->ma_smalltable;
     626        if (newtable == oldtable) {
     627            if (mp->ma_fill == mp->ma_used) {
     628                /* No dummies, so no point doing anything. */
     629                return 0;
     630            }
     631            /* We're not going to resize it, but rebuild the
     632               table anyway to purge old dummy entries.
     633               Subtle:  This is *necessary* if fill==size,
     634               as lookdict needs at least one virgin slot to
     635               terminate failing searches.  If fill < size, it's
     636               merely desirable, as dummies slow searches. */
     637            assert(mp->ma_fill > mp->ma_used);
     638            memcpy(small_copy, oldtable, sizeof(small_copy));
     639            oldtable = small_copy;
     640        }
     641    }
     642    else {
     643        newtable = PyMem_NEW(PyDictEntry, newsize);
     644        if (newtable == NULL) {
     645            PyErr_NoMemory();
     646            return -1;
     647        }
     648    }
     649
     650    /* Make the dict empty, using the new table. */
     651    assert(newtable != oldtable);
     652    mp->ma_table = newtable;
     653    mp->ma_mask = newsize - 1;
     654    memset(newtable, 0, sizeof(PyDictEntry) * newsize);
     655    mp->ma_used = 0;
     656    i = mp->ma_fill;
     657    mp->ma_fill = 0;
     658
     659    /* Copy the data over; this is refcount-neutral for active entries;
     660       dummy entries aren't copied over, of course */
     661    for (ep = oldtable; i > 0; ep++) {
     662        if (ep->me_value != NULL) {             /* active entry */
     663            --i;
     664            insertdict_clean(mp, ep->me_key, (long)ep->me_hash,
     665                            ep->me_value);
     666        }
     667        else if (ep->me_key != NULL) {          /* dummy entry */
     668            --i;
     669            assert(ep->me_key == dummy);
     670            Py_DECREF(ep->me_key);
     671        }
     672        /* else key == value == NULL:  nothing to do */
     673    }
     674
     675    if (is_oldtable_malloced)
     676        PyMem_DEL(oldtable);
     677    return 0;
    595678}
    596679
     
    603686_PyDict_NewPresized(Py_ssize_t minused)
    604687{
    605         PyObject *op = PyDict_New();
    606 
    607         if (minused>5 && op != NULL && dictresize((PyDictObject *)op, minused) == -1) {
    608                 Py_DECREF(op);
    609                 return NULL;
    610         }
    611         return op;
     688    PyObject *op = PyDict_New();
     689
     690    if (minused>5 && op != NULL && dictresize((PyDictObject *)op, minused) == -1) {
     691        Py_DECREF(op);
     692        return NULL;
     693    }
     694    return op;
    612695}
    613696
     
    625708PyDict_GetItem(PyObject *op, PyObject *key)
    626709{
    627         long hash;
    628         PyDictObject *mp = (PyDictObject *)op;
    629         PyDictEntry *ep;
    630         PyThreadState *tstate;
    631         if (!PyDict_Check(op))
    632                 return NULL;
    633         if (!PyString_CheckExact(key) ||
    634             (hash = ((PyStringObject *) key)->ob_shash) == -1)
    635         {
    636                 hash = PyObject_Hash(key);
    637                 if (hash == -1) {
    638                         PyErr_Clear();
    639                         return NULL;
    640                 }
    641         }
    642 
    643         /* We can arrive here with a NULL tstate during initialization:
    644            try running "python -Wi" for an example related to string
    645            interning.  Let's just hope that no exception occurs then... */
    646         tstate = _PyThreadState_Current;
    647         if (tstate != NULL && tstate->curexc_type != NULL) {
    648                 /* preserve the existing exception */
    649                 PyObject *err_type, *err_value, *err_tb;
    650                 PyErr_Fetch(&err_type, &err_value, &err_tb);
    651                 ep = (mp->ma_lookup)(mp, key, hash);
    652                 /* ignore errors */
    653                 PyErr_Restore(err_type, err_value, err_tb);
    654                 if (ep == NULL)
    655                         return NULL;
    656         }
    657         else {
    658                 ep = (mp->ma_lookup)(mp, key, hash);
    659                 if (ep == NULL) {
    660                         PyErr_Clear();
    661                         return NULL;
    662                 }
    663         }
    664         return ep->me_value;
     710    long hash;
     711    PyDictObject *mp = (PyDictObject *)op;
     712    PyDictEntry *ep;
     713    PyThreadState *tstate;
     714    if (!PyDict_Check(op))
     715        return NULL;
     716    if (!PyString_CheckExact(key) ||
     717        (hash = ((PyStringObject *) key)->ob_shash) == -1)
     718    {
     719        hash = PyObject_Hash(key);
     720        if (hash == -1) {
     721            PyErr_Clear();
     722            return NULL;
     723        }
     724    }
     725
     726    /* We can arrive here with a NULL tstate during initialization: try
     727       running "python -Wi" for an example related to string interning.
     728       Let's just hope that no exception occurs then...  This must be
     729       _PyThreadState_Current and not PyThreadState_GET() because in debug
     730       mode, the latter complains if tstate is NULL. */
     731    tstate = _PyThreadState_Current;
     732    if (tstate != NULL && tstate->curexc_type != NULL) {
     733        /* preserve the existing exception */
     734        PyObject *err_type, *err_value, *err_tb;
     735        PyErr_Fetch(&err_type, &err_value, &err_tb);
     736        ep = (mp->ma_lookup)(mp, key, hash);
     737        /* ignore errors */
     738        PyErr_Restore(err_type, err_value, err_tb);
     739        if (ep == NULL)
     740            return NULL;
     741    }
     742    else {
     743        ep = (mp->ma_lookup)(mp, key, hash);
     744        if (ep == NULL) {
     745            PyErr_Clear();
     746            return NULL;
     747        }
     748    }
     749    return ep->me_value;
     750}
     751
     752static int
     753dict_set_item_by_hash_or_entry(register PyObject *op, PyObject *key,
     754                               long hash, PyDictEntry *ep, PyObject *value)
     755{
     756    register PyDictObject *mp;
     757    register Py_ssize_t n_used;
     758
     759    mp = (PyDictObject *)op;
     760    assert(mp->ma_fill <= mp->ma_mask);  /* at least one empty slot */
     761    n_used = mp->ma_used;
     762    Py_INCREF(value);
     763    Py_INCREF(key);
     764    if (ep == NULL) {
     765        if (insertdict(mp, key, hash, value) != 0)
     766            return -1;
     767    }
     768    else {
     769        if (insertdict_by_entry(mp, key, hash, ep, value) != 0)
     770            return -1;
     771    }
     772    /* If we added a key, we can safely resize.  Otherwise just return!
     773     * If fill >= 2/3 size, adjust size.  Normally, this doubles or
     774     * quaduples the size, but it's also possible for the dict to shrink
     775     * (if ma_fill is much larger than ma_used, meaning a lot of dict
     776     * keys have been * deleted).
     777     *
     778     * Quadrupling the size improves average dictionary sparseness
     779     * (reducing collisions) at the cost of some memory and iteration
     780     * speed (which loops over every possible entry).  It also halves
     781     * the number of expensive resize operations in a growing dictionary.
     782     *
     783     * Very large dictionaries (over 50K items) use doubling instead.
     784     * This may help applications with severe memory constraints.
     785     */
     786    if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2))
     787        return 0;
     788    return dictresize(mp, (mp->ma_used > 50000 ? 2 : 4) * mp->ma_used);
    665789}
    666790
     
    674798PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value)
    675799{
    676         register PyDictObject *mp;
    677         register long hash;
    678         register Py_ssize_t n_used;
    679 
    680         if (!PyDict_Check(op)) {
    681                 PyErr_BadInternalCall();
    682                 return -1;
    683         }
    684         assert(key);
    685         assert(value);
    686         mp = (PyDictObject *)op;
    687         if (PyString_CheckExact(key)) {
    688                 hash = ((PyStringObject *)key)->ob_shash;
    689                 if (hash == -1)
    690                         hash = PyObject_Hash(key);
    691         }
    692         else {
    693                 hash = PyObject_Hash(key);
    694                 if (hash == -1)
    695                         return -1;
    696         }
    697         assert(mp->ma_fill <= mp->ma_mask);  /* at least one empty slot */
    698         n_used = mp->ma_used;
    699         Py_INCREF(value);
    700         Py_INCREF(key);
    701         if (insertdict(mp, key, hash, value) != 0)
    702                 return -1;
    703         /* If we added a key, we can safely resize.  Otherwise just return!
    704          * If fill >= 2/3 size, adjust size.  Normally, this doubles or
    705          * quaduples the size, but it's also possible for the dict to shrink
    706          * (if ma_fill is much larger than ma_used, meaning a lot of dict
    707          * keys have been * deleted).
    708          *
    709          * Quadrupling the size improves average dictionary sparseness
    710          * (reducing collisions) at the cost of some memory and iteration
    711          * speed (which loops over every possible entry).  It also halves
    712          * the number of expensive resize operations in a growing dictionary.
    713          *
    714          * Very large dictionaries (over 50K items) use doubling instead.
    715          * This may help applications with severe memory constraints.
    716          */
    717         if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2))
    718                 return 0;
    719         return dictresize(mp, (mp->ma_used > 50000 ? 2 : 4) * mp->ma_used);
     800    register long hash;
     801
     802    if (!PyDict_Check(op)) {
     803        PyErr_BadInternalCall();
     804        return -1;
     805    }
     806    assert(key);
     807    assert(value);
     808    if (PyString_CheckExact(key)) {
     809        hash = ((PyStringObject *)key)->ob_shash;
     810        if (hash == -1)
     811            hash = PyObject_Hash(key);
     812    }
     813    else {
     814        hash = PyObject_Hash(key);
     815        if (hash == -1)
     816            return -1;
     817    }
     818    return dict_set_item_by_hash_or_entry(op, key, hash, NULL, value);
    720819}
    721820
     
    723822PyDict_DelItem(PyObject *op, PyObject *key)
    724823{
    725         register PyDictObject *mp;
    726         register long hash;
    727         register PyDictEntry *ep;
    728         PyObject *old_value, *old_key;
    729 
    730         if (!PyDict_Check(op)) {
    731                 PyErr_BadInternalCall();
    732                 return -1;
    733         }
    734         assert(key);
    735         if (!PyString_CheckExact(key) ||
    736             (hash = ((PyStringObject *) key)->ob_shash) == -1) {
    737                 hash = PyObject_Hash(key);
    738                 if (hash == -1)
    739                         return -1;
    740         }
    741         mp = (PyDictObject *)op;
    742         ep = (mp->ma_lookup)(mp, key, hash);
    743         if (ep == NULL)
    744                 return -1;
    745         if (ep->me_value == NULL) {
    746                 set_key_error(key);
    747                 return -1;
    748         }
    749         old_key = ep->me_key;
    750         Py_INCREF(dummy);
    751         ep->me_key = dummy;
    752         old_value = ep->me_value;
    753         ep->me_value = NULL;
    754         mp->ma_used--;
    755         Py_DECREF(old_value);
    756         Py_DECREF(old_key);
    757         return 0;
     824    register PyDictObject *mp;
     825    register long hash;
     826    register PyDictEntry *ep;
     827    PyObject *old_value, *old_key;
     828
     829    if (!PyDict_Check(op)) {
     830        PyErr_BadInternalCall();
     831        return -1;
     832    }
     833    assert(key);
     834    if (!PyString_CheckExact(key) ||
     835        (hash = ((PyStringObject *) key)->ob_shash) == -1) {
     836        hash = PyObject_Hash(key);
     837        if (hash == -1)
     838            return -1;
     839    }
     840    mp = (PyDictObject *)op;
     841    ep = (mp->ma_lookup)(mp, key, hash);
     842    if (ep == NULL)
     843        return -1;
     844    if (ep->me_value == NULL) {
     845        set_key_error(key);
     846        return -1;
     847    }
     848    old_key = ep->me_key;
     849    Py_INCREF(dummy);
     850    ep->me_key = dummy;
     851    old_value = ep->me_value;
     852    ep->me_value = NULL;
     853    mp->ma_used--;
     854    Py_DECREF(old_value);
     855    Py_DECREF(old_key);
     856    return 0;
    758857}
    759858
     
    761860PyDict_Clear(PyObject *op)
    762861{
    763         PyDictObject *mp;
    764         PyDictEntry *ep, *table;
    765         int table_is_malloced;
    766         Py_ssize_t fill;
    767         PyDictEntry small_copy[PyDict_MINSIZE];
     862    PyDictObject *mp;
     863    PyDictEntry *ep, *table;
     864    int table_is_malloced;
     865    Py_ssize_t fill;
     866    PyDictEntry small_copy[PyDict_MINSIZE];
    768867#ifdef Py_DEBUG
    769         Py_ssize_t i, n;
     868    Py_ssize_t i, n;
    770869#endif
    771870
    772         if (!PyDict_Check(op))
    773                 return;
    774         mp = (PyDictObject *)op;
     871    if (!PyDict_Check(op))
     872        return;
     873    mp = (PyDictObject *)op;
    775874#ifdef Py_DEBUG
    776         n = mp->ma_mask + 1;
    777         i = 0;
     875    n = mp->ma_mask + 1;
     876    i = 0;
    778877#endif
    779878
    780         table = mp->ma_table;
    781         assert(table != NULL);
    782         table_is_malloced = table != mp->ma_smalltable;
    783 
    784         /* This is delicate.  During the process of clearing the dict,
    785         * decrefs can cause the dict to mutate.  To avoid fatal confusion
    786         * (voice of experience), we have to make the dict empty before
    787         * clearing the slots, and never refer to anything via mp->xxx while
    788         * clearing.
    789         */
    790         fill = mp->ma_fill;
    791         if (table_is_malloced)
    792                 EMPTY_TO_MINSIZE(mp);
    793 
    794         else if (fill > 0) {
    795                 /* It's a small table with something that needs to be cleared.
    796                 * Afraid the only safe way is to copy the dict entries into
    797                 * another small table first.
    798                 */
    799                 memcpy(small_copy, table, sizeof(small_copy));
    800                 table = small_copy;
    801                 EMPTY_TO_MINSIZE(mp);
    802         }
    803         /* else it's a small table that's already empty */
    804 
    805         /* Now we can finally clear things.  If C had refcounts, we could
    806         * assert that the refcount on table is 1 now, i.e. that this function
    807         * has unique access to it, so decref side-effects can't alter it.
    808         */
    809         for (ep = table; fill > 0; ++ep) {
     879    table = mp->ma_table;
     880    assert(table != NULL);
     881    table_is_malloced = table != mp->ma_smalltable;
     882
     883    /* This is delicate.  During the process of clearing the dict,
     884    * decrefs can cause the dict to mutate.  To avoid fatal confusion
     885    * (voice of experience), we have to make the dict empty before
     886    * clearing the slots, and never refer to anything via mp->xxx while
     887    * clearing.
     888    */
     889    fill = mp->ma_fill;
     890    if (table_is_malloced)
     891        EMPTY_TO_MINSIZE(mp);
     892
     893    else if (fill > 0) {
     894        /* It's a small table with something that needs to be cleared.
     895        * Afraid the only safe way is to copy the dict entries into
     896        * another small table first.
     897        */
     898        memcpy(small_copy, table, sizeof(small_copy));
     899        table = small_copy;
     900        EMPTY_TO_MINSIZE(mp);
     901    }
     902    /* else it's a small table that's already empty */
     903
     904    /* Now we can finally clear things.  If C had refcounts, we could
     905    * assert that the refcount on table is 1 now, i.e. that this function
     906    * has unique access to it, so decref side-effects can't alter it.
     907    */
     908    for (ep = table; fill > 0; ++ep) {
    810909#ifdef Py_DEBUG
    811                 assert(i < n);
    812                 ++i;
     910        assert(i < n);
     911        ++i;
    813912#endif
    814                 if (ep->me_key) {
    815                         --fill;
    816                         Py_DECREF(ep->me_key);
    817                         Py_XDECREF(ep->me_value);
    818                 }
     913        if (ep->me_key) {
     914            --fill;
     915            Py_DECREF(ep->me_key);
     916            Py_XDECREF(ep->me_value);
     917        }
    819918#ifdef Py_DEBUG
    820                 else
    821                         assert(ep->me_value == NULL);
     919        else
     920            assert(ep->me_value == NULL);
    822921#endif
    823         }
    824 
    825         if (table_is_malloced)
    826                 PyMem_DEL(table);
     922    }
     923
     924    if (table_is_malloced)
     925        PyMem_DEL(table);
    827926}
    828927
     
    845944PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue)
    846945{
    847         register Py_ssize_t i;
    848         register Py_ssize_t mask;
    849         register PyDictEntry *ep;
    850 
    851         if (!PyDict_Check(op))
    852                 return 0;
    853         i = *ppos;
    854         if (i < 0)
    855                 return 0;
    856         ep = ((PyDictObject *)op)->ma_table;
    857         mask = ((PyDictObject *)op)->ma_mask;
    858         while (i <= mask && ep[i].me_value == NULL)
    859                 i++;
    860         *ppos = i+1;
    861         if (i > mask)
    862                 return 0;
    863         if (pkey)
    864                 *pkey = ep[i].me_key;
    865         if (pvalue)
    866                 *pvalue = ep[i].me_value;
    867         return 1;
     946    register Py_ssize_t i;
     947    register Py_ssize_t mask;
     948    register PyDictEntry *ep;
     949
     950    if (!PyDict_Check(op))
     951        return 0;
     952    i = *ppos;
     953    if (i < 0)
     954        return 0;
     955    ep = ((PyDictObject *)op)->ma_table;
     956    mask = ((PyDictObject *)op)->ma_mask;
     957    while (i <= mask && ep[i].me_value == NULL)
     958        i++;
     959    *ppos = i+1;
     960    if (i > mask)
     961        return 0;
     962    if (pkey)
     963        *pkey = ep[i].me_key;
     964    if (pvalue)
     965        *pvalue = ep[i].me_value;
     966    return 1;
    868967}
    869968
     
    872971_PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue, long *phash)
    873972{
    874         register Py_ssize_t i;
    875         register Py_ssize_t mask;
    876         register PyDictEntry *ep;
    877 
    878         if (!PyDict_Check(op))
    879                 return 0;
    880         i = *ppos;
    881         if (i < 0)
    882                 return 0;
    883         ep = ((PyDictObject *)op)->ma_table;
    884         mask = ((PyDictObject *)op)->ma_mask;
    885         while (i <= mask && ep[i].me_value == NULL)
    886                 i++;
    887         *ppos = i+1;
    888         if (i > mask)
    889                 return 0;
    890         *phash = (long)(ep[i].me_hash);
    891         if (pkey)
    892                 *pkey = ep[i].me_key;
    893         if (pvalue)
    894                 *pvalue = ep[i].me_value;
    895         return 1;
     973    register Py_ssize_t i;
     974    register Py_ssize_t mask;
     975    register PyDictEntry *ep;
     976
     977    if (!PyDict_Check(op))
     978        return 0;
     979    i = *ppos;
     980    if (i < 0)
     981        return 0;
     982    ep = ((PyDictObject *)op)->ma_table;
     983    mask = ((PyDictObject *)op)->ma_mask;
     984    while (i <= mask && ep[i].me_value == NULL)
     985        i++;
     986    *ppos = i+1;
     987    if (i > mask)
     988        return 0;
     989    *phash = (long)(ep[i].me_hash);
     990    if (pkey)
     991        *pkey = ep[i].me_key;
     992    if (pvalue)
     993        *pvalue = ep[i].me_value;
     994    return 1;
    896995}
    897996
     
    9011000dict_dealloc(register PyDictObject *mp)
    9021001{
    903         register PyDictEntry *ep;
    904         Py_ssize_t fill = mp->ma_fill;
    905         PyObject_GC_UnTrack(mp);
    906         Py_TRASHCAN_SAFE_BEGIN(mp)
    907         for (ep = mp->ma_table; fill > 0; ep++) {
    908                 if (ep->me_key) {
    909                         --fill;
    910                         Py_DECREF(ep->me_key);
    911                         Py_XDECREF(ep->me_value);
    912                 }
    913         }
    914         if (mp->ma_table != mp->ma_smalltable)
    915                 PyMem_DEL(mp->ma_table);
    916         if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
    917                 free_list[numfree++] = mp;
    918         else
    919                 Py_TYPE(mp)->tp_free((PyObject *)mp);
    920         Py_TRASHCAN_SAFE_END(mp)
     1002    register PyDictEntry *ep;
     1003    Py_ssize_t fill = mp->ma_fill;
     1004    PyObject_GC_UnTrack(mp);
     1005    Py_TRASHCAN_SAFE_BEGIN(mp)
     1006    for (ep = mp->ma_table; fill > 0; ep++) {
     1007        if (ep->me_key) {
     1008            --fill;
     1009            Py_DECREF(ep->me_key);
     1010            Py_XDECREF(ep->me_value);
     1011        }
     1012    }
     1013    if (mp->ma_table != mp->ma_smalltable)
     1014        PyMem_DEL(mp->ma_table);
     1015    if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type)
     1016        free_list[numfree++] = mp;
     1017    else
     1018        Py_TYPE(mp)->tp_free((PyObject *)mp);
     1019    Py_TRASHCAN_SAFE_END(mp)
    9211020}
    9221021
     
    9241023dict_print(register PyDictObject *mp, register FILE *fp, register int flags)
    9251024{
    926         register Py_ssize_t i;
    927         register Py_ssize_t any;
    928         int status;
    929 
    930         status = Py_ReprEnter((PyObject*)mp);
    931         if (status != 0) {
    932                 if (status < 0)
    933                         return status;
    934                 Py_BEGIN_ALLOW_THREADS
    935                 fprintf(fp, "{...}");
    936                 Py_END_ALLOW_THREADS
    937                 return 0;
    938         }
    939 
    940         Py_BEGIN_ALLOW_THREADS
    941         fprintf(fp, "{");
    942         Py_END_ALLOW_THREADS
    943         any = 0;
    944         for (i = 0; i <= mp->ma_mask; i++) {
    945                 PyDictEntry *ep = mp->ma_table + i;
    946                 PyObject *pvalue = ep->me_value;
    947                 if (pvalue != NULL) {
    948                         /* Prevent PyObject_Repr from deleting value during
    949                            key format */
    950                         Py_INCREF(pvalue);
    951                         if (any++ > 0) {
    952                                 Py_BEGIN_ALLOW_THREADS
    953                                 fprintf(fp, ", ");
    954                                 Py_END_ALLOW_THREADS
    955                         }
    956                         if (PyObject_Print((PyObject *)ep->me_key, fp, 0)!=0) {
    957                                 Py_DECREF(pvalue);
    958                                 Py_ReprLeave((PyObject*)mp);
    959                                 return -1;
    960                         }
    961                         Py_BEGIN_ALLOW_THREADS
    962                         fprintf(fp, ": ");
    963                         Py_END_ALLOW_THREADS
    964                         if (PyObject_Print(pvalue, fp, 0) != 0) {
    965                                 Py_DECREF(pvalue);
    966                                 Py_ReprLeave((PyObject*)mp);
    967                                 return -1;
    968                         }
    969                         Py_DECREF(pvalue);
    970                 }
    971         }
    972         Py_BEGIN_ALLOW_THREADS
    973         fprintf(fp, "}");
    974         Py_END_ALLOW_THREADS
    975         Py_ReprLeave((PyObject*)mp);
    976         return 0;
     1025    register Py_ssize_t i;
     1026    register Py_ssize_t any;
     1027    int status;
     1028
     1029    status = Py_ReprEnter((PyObject*)mp);
     1030    if (status != 0) {
     1031        if (status < 0)
     1032            return status;
     1033        Py_BEGIN_ALLOW_THREADS
     1034        fprintf(fp, "{...}");
     1035        Py_END_ALLOW_THREADS
     1036        return 0;
     1037    }
     1038
     1039    Py_BEGIN_ALLOW_THREADS
     1040    fprintf(fp, "{");
     1041    Py_END_ALLOW_THREADS
     1042    any = 0;
     1043    for (i = 0; i <= mp->ma_mask; i++) {
     1044        PyDictEntry *ep = mp->ma_table + i;
     1045        PyObject *pvalue = ep->me_value;
     1046        if (pvalue != NULL) {
     1047            /* Prevent PyObject_Repr from deleting value during
     1048               key format */
     1049            Py_INCREF(pvalue);
     1050            if (any++ > 0) {
     1051                Py_BEGIN_ALLOW_THREADS
     1052                fprintf(fp, ", ");
     1053                Py_END_ALLOW_THREADS
     1054            }
     1055            if (PyObject_Print((PyObject *)ep->me_key, fp, 0)!=0) {
     1056                Py_DECREF(pvalue);
     1057                Py_ReprLeave((PyObject*)mp);
     1058                return -1;
     1059            }
     1060            Py_BEGIN_ALLOW_THREADS
     1061            fprintf(fp, ": ");
     1062            Py_END_ALLOW_THREADS
     1063            if (PyObject_Print(pvalue, fp, 0) != 0) {
     1064                Py_DECREF(pvalue);
     1065                Py_ReprLeave((PyObject*)mp);
     1066                return -1;
     1067            }
     1068            Py_DECREF(pvalue);
     1069        }
     1070    }
     1071    Py_BEGIN_ALLOW_THREADS
     1072    fprintf(fp, "}");
     1073    Py_END_ALLOW_THREADS
     1074    Py_ReprLeave((PyObject*)mp);
     1075    return 0;
    9771076}
    9781077
     
    9801079dict_repr(PyDictObject *mp)
    9811080{
    982         Py_ssize_t i;
    983         PyObject *s, *temp, *colon = NULL;
    984         PyObject *pieces = NULL, *result = NULL;
    985         PyObject *key, *value;
    986 
    987         i = Py_ReprEnter((PyObject *)mp);
    988         if (i != 0) {
    989                 return i > 0 ? PyString_FromString("{...}") : NULL;
    990         }
    991 
    992         if (mp->ma_used == 0) {
    993                 result = PyString_FromString("{}");
    994                 goto Done;
    995         }
    996 
    997         pieces = PyList_New(0);
    998         if (pieces == NULL)
    999                 goto Done;
    1000 
    1001         colon = PyString_FromString(": ");
    1002         if (colon == NULL)
    1003                 goto Done;
    1004 
    1005         /* Do repr() on each key+value pair, and insert ": " between them.
    1006            Note that repr may mutate the dict. */
    1007         i = 0;
    1008         while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
    1009                 int status;
    1010                 /* Prevent repr from deleting value during key format. */
    1011                 Py_INCREF(value);
    1012                 s = PyObject_Repr(key);
    1013                 PyString_Concat(&s, colon);
    1014                 PyString_ConcatAndDel(&s, PyObject_Repr(value));
    1015                 Py_DECREF(value);
    1016                 if (s == NULL)
    1017                         goto Done;
    1018                 status = PyList_Append(pieces, s);
    1019                 Py_DECREF(s);  /* append created a new ref */
    1020                 if (status < 0)
    1021                         goto Done;
    1022         }
    1023 
    1024         /* Add "{}" decorations to the first and last items. */
    1025         assert(PyList_GET_SIZE(pieces) > 0);
    1026         s = PyString_FromString("{");
    1027         if (s == NULL)
    1028                 goto Done;
    1029         temp = PyList_GET_ITEM(pieces, 0);
    1030         PyString_ConcatAndDel(&s, temp);
    1031         PyList_SET_ITEM(pieces, 0, s);
    1032         if (s == NULL)
    1033                 goto Done;
    1034 
    1035         s = PyString_FromString("}");
    1036         if (s == NULL)
    1037                 goto Done;
    1038         temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
    1039         PyString_ConcatAndDel(&temp, s);
    1040         PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
    1041         if (temp == NULL)
    1042                 goto Done;
    1043 
    1044         /* Paste them all together with ", " between. */
    1045         s = PyString_FromString(", ");
    1046         if (s == NULL)
    1047                 goto Done;
    1048         result = _PyString_Join(s, pieces);
    1049         Py_DECREF(s);
     1081    Py_ssize_t i;
     1082    PyObject *s, *temp, *colon = NULL;
     1083    PyObject *pieces = NULL, *result = NULL;
     1084    PyObject *key, *value;
     1085
     1086    i = Py_ReprEnter((PyObject *)mp);
     1087    if (i != 0) {
     1088        return i > 0 ? PyString_FromString("{...}") : NULL;
     1089    }
     1090
     1091    if (mp->ma_used == 0) {
     1092        result = PyString_FromString("{}");
     1093        goto Done;
     1094    }
     1095
     1096    pieces = PyList_New(0);
     1097    if (pieces == NULL)
     1098        goto Done;
     1099
     1100    colon = PyString_FromString(": ");
     1101    if (colon == NULL)
     1102        goto Done;
     1103
     1104    /* Do repr() on each key+value pair, and insert ": " between them.
     1105       Note that repr may mutate the dict. */
     1106    i = 0;
     1107    while (PyDict_Next((PyObject *)mp, &i, &key, &value)) {
     1108        int status;
     1109        /* Prevent repr from deleting value during key format. */
     1110        Py_INCREF(value);
     1111        s = PyObject_Repr(key);
     1112        PyString_Concat(&s, colon);
     1113        PyString_ConcatAndDel(&s, PyObject_Repr(value));
     1114        Py_DECREF(value);
     1115        if (s == NULL)
     1116            goto Done;
     1117        status = PyList_Append(pieces, s);
     1118        Py_DECREF(s);  /* append created a new ref */
     1119        if (status < 0)
     1120            goto Done;
     1121    }
     1122
     1123    /* Add "{}" decorations to the first and last items. */
     1124    assert(PyList_GET_SIZE(pieces) > 0);
     1125    s = PyString_FromString("{");
     1126    if (s == NULL)
     1127        goto Done;
     1128    temp = PyList_GET_ITEM(pieces, 0);
     1129    PyString_ConcatAndDel(&s, temp);
     1130    PyList_SET_ITEM(pieces, 0, s);
     1131    if (s == NULL)
     1132        goto Done;
     1133
     1134    s = PyString_FromString("}");
     1135    if (s == NULL)
     1136        goto Done;
     1137    temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1);
     1138    PyString_ConcatAndDel(&temp, s);
     1139    PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp);
     1140    if (temp == NULL)
     1141        goto Done;
     1142
     1143    /* Paste them all together with ", " between. */
     1144    s = PyString_FromString(", ");
     1145    if (s == NULL)
     1146        goto Done;
     1147    result = _PyString_Join(s, pieces);
     1148    Py_DECREF(s);
    10501149
    10511150Done:
    1052         Py_XDECREF(pieces);
    1053         Py_XDECREF(colon);
    1054         Py_ReprLeave((PyObject *)mp);
    1055         return result;
     1151    Py_XDECREF(pieces);
     1152    Py_XDECREF(colon);
     1153    Py_ReprLeave((PyObject *)mp);
     1154    return result;
    10561155}
    10571156
     
    10591158dict_length(PyDictObject *mp)
    10601159{
    1061         return mp->ma_used;
     1160    return mp->ma_used;
    10621161}
    10631162
     
    10651164dict_subscript(PyDictObject *mp, register PyObject *key)
    10661165{
    1067         PyObject *v;
    1068         long hash;
    1069         PyDictEntry *ep;
    1070         assert(mp->ma_table != NULL);
    1071         if (!PyString_CheckExact(key) ||
    1072             (hash = ((PyStringObject *) key)->ob_shash) == -1) {
    1073                 hash = PyObject_Hash(key);
    1074                 if (hash == -1)
    1075                         return NULL;
    1076         }
    1077         ep = (mp->ma_lookup)(mp, key, hash);
    1078         if (ep == NULL)
    1079                 return NULL;
    1080         v = ep->me_value;
    1081         if (v == NULL) {
    1082                 if (!PyDict_CheckExact(mp)) {
    1083                         /* Look up __missing__ method if we're a subclass. */
    1084                         PyObject *missing;
    1085                         static PyObject *missing_str = NULL;
    1086                         if (missing_str == NULL)
    1087                                 missing_str =
    1088                                   PyString_InternFromString("__missing__");
    1089                         missing = _PyType_Lookup(Py_TYPE(mp), missing_str);
    1090                         if (missing != NULL)
    1091                                 return PyObject_CallFunctionObjArgs(missing,
    1092                                         (PyObject *)mp, key, NULL);
    1093                 }
    1094                 set_key_error(key);
    1095                 return NULL;
    1096         }
    1097         else
    1098                 Py_INCREF(v);
    1099         return v;
     1166    PyObject *v;
     1167    long hash;
     1168    PyDictEntry *ep;
     1169    assert(mp->ma_table != NULL);
     1170    if (!PyString_CheckExact(key) ||
     1171        (hash = ((PyStringObject *) key)->ob_shash) == -1) {
     1172        hash = PyObject_Hash(key);
     1173        if (hash == -1)
     1174            return NULL;
     1175    }
     1176    ep = (mp->ma_lookup)(mp, key, hash);
     1177    if (ep == NULL)
     1178        return NULL;
     1179    v = ep->me_value;
     1180    if (v == NULL) {
     1181        if (!PyDict_CheckExact(mp)) {
     1182            /* Look up __missing__ method if we're a subclass. */
     1183            PyObject *missing, *res;
     1184            static PyObject *missing_str = NULL;
     1185            missing = _PyObject_LookupSpecial((PyObject *)mp,
     1186                                              "__missing__",
     1187                                              &missing_str);
     1188            if (missing != NULL) {
     1189                res = PyObject_CallFunctionObjArgs(missing,
     1190                                                   key, NULL);
     1191                Py_DECREF(missing);
     1192                return res;
     1193            }
     1194            else if (PyErr_Occurred())
     1195                return NULL;
     1196        }
     1197        set_key_error(key);
     1198        return NULL;
     1199    }
     1200    else
     1201        Py_INCREF(v);
     1202    return v;
    11001203}
    11011204
     
    11031206dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w)
    11041207{
    1105         if (w == NULL)
    1106                 return PyDict_DelItem((PyObject *)mp, v);
    1107         else
    1108                 return PyDict_SetItem((PyObject *)mp, v, w);
     1208    if (w == NULL)
     1209        return PyDict_DelItem((PyObject *)mp, v);
     1210    else
     1211        return PyDict_SetItem((PyObject *)mp, v, w);
    11091212}
    11101213
    11111214static PyMappingMethods dict_as_mapping = {
    1112         (lenfunc)dict_length, /*mp_length*/
    1113         (binaryfunc)dict_subscript, /*mp_subscript*/
    1114         (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
     1215    (lenfunc)dict_length, /*mp_length*/
     1216    (binaryfunc)dict_subscript, /*mp_subscript*/
     1217    (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/
    11151218};
    11161219
     
    11181221dict_keys(register PyDictObject *mp)
    11191222{
    1120         register PyObject *v;
    1121         register Py_ssize_t i, j;
    1122         PyDictEntry *ep;
    1123         Py_ssize_t mask, n;
     1223    register PyObject *v;
     1224    register Py_ssize_t i, j;
     1225    PyDictEntry *ep;
     1226    Py_ssize_t mask, n;
    11241227
    11251228  again:
    1126         n = mp->ma_used;
    1127         v = PyList_New(n);
    1128         if (v == NULL)
    1129                 return NULL;
    1130         if (n != mp->ma_used) {
    1131                 /* Durnit.  The allocations caused the dict to resize.
    1132                 * Just start over, this shouldn't normally happen.
    1133                 */
    1134                 Py_DECREF(v);
    1135                 goto again;
    1136         }
    1137         ep = mp->ma_table;
    1138         mask = mp->ma_mask;
    1139         for (i = 0, j = 0; i <= mask; i++) {
    1140                 if (ep[i].me_value != NULL) {
    1141                         PyObject *key = ep[i].me_key;
    1142                         Py_INCREF(key);
    1143                         PyList_SET_ITEM(v, j, key);
    1144                         j++;
    1145                 }
    1146         }
    1147         assert(j == n);
    1148         return v;
     1229    n = mp->ma_used;
     1230    v = PyList_New(n);
     1231    if (v == NULL)
     1232        return NULL;
     1233    if (n != mp->ma_used) {
     1234        /* Durnit.  The allocations caused the dict to resize.
     1235        * Just start over, this shouldn't normally happen.
     1236        */
     1237        Py_DECREF(v);
     1238        goto again;
     1239    }
     1240    ep = mp->ma_table;
     1241    mask = mp->ma_mask;
     1242    for (i = 0, j = 0; i <= mask; i++) {
     1243        if (ep[i].me_value != NULL) {
     1244            PyObject *key = ep[i].me_key;
     1245            Py_INCREF(key);
     1246            PyList_SET_ITEM(v, j, key);
     1247            j++;
     1248        }
     1249    }
     1250    assert(j == n);
     1251    return v;
    11491252}
    11501253
     
    11521255dict_values(register PyDictObject *mp)
    11531256{
    1154         register PyObject *v;
    1155         register Py_ssize_t i, j;
    1156         PyDictEntry *ep;
    1157         Py_ssize_t mask, n;
     1257    register PyObject *v;
     1258    register Py_ssize_t i, j;
     1259    PyDictEntry *ep;
     1260    Py_ssize_t mask, n;
    11581261
    11591262  again:
    1160         n = mp->ma_used;
    1161         v = PyList_New(n);
    1162         if (v == NULL)
    1163                 return NULL;
    1164         if (n != mp->ma_used) {
    1165                 /* Durnit.  The allocations caused the dict to resize.
    1166                 * Just start over, this shouldn't normally happen.
    1167                 */
    1168                 Py_DECREF(v);
    1169                 goto again;
    1170         }
    1171         ep = mp->ma_table;
    1172         mask = mp->ma_mask;
    1173         for (i = 0, j = 0; i <= mask; i++) {
    1174                 if (ep[i].me_value != NULL) {
    1175                         PyObject *value = ep[i].me_value;
    1176                         Py_INCREF(value);
    1177                         PyList_SET_ITEM(v, j, value);
    1178                         j++;
    1179                 }
    1180         }
    1181         assert(j == n);
    1182         return v;
     1263    n = mp->ma_used;
     1264    v = PyList_New(n);
     1265    if (v == NULL)
     1266        return NULL;
     1267    if (n != mp->ma_used) {
     1268        /* Durnit.  The allocations caused the dict to resize.
     1269        * Just start over, this shouldn't normally happen.
     1270        */
     1271        Py_DECREF(v);
     1272        goto again;
     1273    }
     1274    ep = mp->ma_table;
     1275    mask = mp->ma_mask;
     1276    for (i = 0, j = 0; i <= mask; i++) {
     1277        if (ep[i].me_value != NULL) {
     1278            PyObject *value = ep[i].me_value;
     1279            Py_INCREF(value);
     1280            PyList_SET_ITEM(v, j, value);
     1281            j++;
     1282        }
     1283    }
     1284    assert(j == n);
     1285    return v;
    11831286}
    11841287
     
    11861289dict_items(register PyDictObject *mp)
    11871290{
    1188         register PyObject *v;
    1189         register Py_ssize_t i, j, n;
    1190         Py_ssize_t mask;
    1191         PyObject *item, *key, *value;
    1192         PyDictEntry *ep;
    1193 
    1194         /* Preallocate the list of tuples, to avoid allocations during
    1195         * the loop over the items, which could trigger GC, which
    1196         * could resize the dict. :-(
    1197         */
     1291    register PyObject *v;
     1292    register Py_ssize_t i, j, n;
     1293    Py_ssize_t mask;
     1294    PyObject *item, *key, *value;
     1295    PyDictEntry *ep;
     1296
     1297    /* Preallocate the list of tuples, to avoid allocations during
     1298    * the loop over the items, which could trigger GC, which
     1299    * could resize the dict. :-(
     1300    */
    11981301  again:
    1199         n = mp->ma_used;
    1200         v = PyList_New(n);
    1201         if (v == NULL)
    1202                 return NULL;
    1203         for (i = 0; i < n; i++) {
    1204                 item = PyTuple_New(2);
    1205                 if (item == NULL) {
    1206                         Py_DECREF(v);
    1207                         return NULL;
    1208                 }
    1209                 PyList_SET_ITEM(v, i, item);
    1210         }
    1211         if (n != mp->ma_used) {
    1212                 /* Durnit.  The allocations caused the dict to resize.
    1213                 * Just start over, this shouldn't normally happen.
    1214                 */
    1215                 Py_DECREF(v);
    1216                 goto again;
    1217         }
    1218         /* Nothing we do below makes any function calls. */
    1219         ep = mp->ma_table;
    1220         mask = mp->ma_mask;
    1221         for (i = 0, j = 0; i <= mask; i++) {
    1222                 if ((value=ep[i].me_value) != NULL) {
    1223                         key = ep[i].me_key;
    1224                         item = PyList_GET_ITEM(v, j);
    1225                         Py_INCREF(key);
    1226                         PyTuple_SET_ITEM(item, 0, key);
    1227                         Py_INCREF(value);
    1228                         PyTuple_SET_ITEM(item, 1, value);
    1229                         j++;
    1230                 }
    1231         }
    1232         assert(j == n);
    1233         return v;
     1302    n = mp->ma_used;
     1303    v = PyList_New(n);
     1304    if (v == NULL)
     1305        return NULL;
     1306    for (i = 0; i < n; i++) {
     1307        item = PyTuple_New(2);
     1308        if (item == NULL) {
     1309            Py_DECREF(v);
     1310            return NULL;
     1311        }
     1312        PyList_SET_ITEM(v, i, item);
     1313    }
     1314    if (n != mp->ma_used) {
     1315        /* Durnit.  The allocations caused the dict to resize.
     1316        * Just start over, this shouldn't normally happen.
     1317        */
     1318        Py_DECREF(v);
     1319        goto again;
     1320    }
     1321    /* Nothing we do below makes any function calls. */
     1322    ep = mp->ma_table;
     1323    mask = mp->ma_mask;
     1324    for (i = 0, j = 0; i <= mask; i++) {
     1325        if ((value=ep[i].me_value) != NULL) {
     1326            key = ep[i].me_key;
     1327            item = PyList_GET_ITEM(v, j);
     1328            Py_INCREF(key);
     1329            PyTuple_SET_ITEM(item, 0, key);
     1330            Py_INCREF(value);
     1331            PyTuple_SET_ITEM(item, 1, value);
     1332            j++;
     1333        }
     1334    }
     1335    assert(j == n);
     1336    return v;
    12341337}
    12351338
     
    12371340dict_fromkeys(PyObject *cls, PyObject *args)
    12381341{
    1239         PyObject *seq;
    1240         PyObject *value = Py_None;
    1241         PyObject *it;   /* iter(seq) */
    1242         PyObject *key;
    1243         PyObject *d;
    1244         int status;
    1245 
    1246         if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
    1247                 return NULL;
    1248 
    1249         d = PyObject_CallObject(cls, NULL);
    1250         if (d == NULL)
    1251                 return NULL;
    1252 
    1253         if (PyDict_CheckExact(d) && PyDict_CheckExact(seq)) {
    1254                 PyDictObject *mp = (PyDictObject *)d;
    1255                 PyObject *oldvalue;
    1256                 Py_ssize_t pos = 0;
    1257                 PyObject *key;
    1258                 long hash;
    1259 
    1260                 if (dictresize(mp, Py_SIZE(seq)))
    1261                         return NULL;
    1262 
    1263                 while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) {
    1264                         Py_INCREF(key);
    1265                         Py_INCREF(value);
    1266                         if (insertdict(mp, key, hash, value))
    1267                                 return NULL;
    1268                 }
    1269                 return d;
    1270         }
    1271 
    1272         if (PyDict_CheckExact(d) && PyAnySet_CheckExact(seq)) {
    1273                 PyDictObject *mp = (PyDictObject *)d;
    1274                 Py_ssize_t pos = 0;
    1275                 PyObject *key;
    1276                 long hash;
    1277 
    1278                 if (dictresize(mp, PySet_GET_SIZE(seq)))
    1279                         return NULL;
    1280 
    1281                 while (_PySet_NextEntry(seq, &pos, &key, &hash)) {
    1282                         Py_INCREF(key);
    1283                         Py_INCREF(value);
    1284                         if (insertdict(mp, key, hash, value))
    1285                                 return NULL;
    1286                 }
    1287                 return d;
    1288         }
    1289 
    1290         it = PyObject_GetIter(seq);
    1291         if (it == NULL){
    1292                 Py_DECREF(d);
    1293                 return NULL;
    1294         }
    1295 
    1296         if (PyDict_CheckExact(d)) {
    1297                 while ((key = PyIter_Next(it)) != NULL) {
    1298                         status = PyDict_SetItem(d, key, value);
    1299                         Py_DECREF(key);
    1300                         if (status < 0)
    1301                                 goto Fail;
    1302                 }
    1303         } else {
    1304                 while ((key = PyIter_Next(it)) != NULL) {
    1305                         status = PyObject_SetItem(d, key, value);
    1306                         Py_DECREF(key);
    1307                         if (status < 0)
    1308                                 goto Fail;
    1309                 }
    1310         }
    1311 
    1312         if (PyErr_Occurred())
    1313                 goto Fail;
    1314         Py_DECREF(it);
    1315         return d;
     1342    PyObject *seq;
     1343    PyObject *value = Py_None;
     1344    PyObject *it;       /* iter(seq) */
     1345    PyObject *key;
     1346    PyObject *d;
     1347    int status;
     1348
     1349    if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value))
     1350        return NULL;
     1351
     1352    d = PyObject_CallObject(cls, NULL);
     1353    if (d == NULL)
     1354        return NULL;
     1355
     1356    if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) {
     1357        if (PyDict_CheckExact(seq)) {
     1358            PyDictObject *mp = (PyDictObject *)d;
     1359            PyObject *oldvalue;
     1360            Py_ssize_t pos = 0;
     1361            PyObject *key;
     1362            long hash;
     1363
     1364            if (dictresize(mp, Py_SIZE(seq))) {
     1365                Py_DECREF(d);
     1366                return NULL;
     1367            }
     1368
     1369            while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) {
     1370                Py_INCREF(key);
     1371                Py_INCREF(value);
     1372                if (insertdict(mp, key, hash, value)) {
     1373                    Py_DECREF(d);
     1374                    return NULL;
     1375                }
     1376            }
     1377            return d;
     1378        }
     1379        if (PyAnySet_CheckExact(seq)) {
     1380            PyDictObject *mp = (PyDictObject *)d;
     1381            Py_ssize_t pos = 0;
     1382            PyObject *key;
     1383            long hash;
     1384
     1385            if (dictresize(mp, PySet_GET_SIZE(seq))) {
     1386                Py_DECREF(d);
     1387                return NULL;
     1388            }
     1389
     1390            while (_PySet_NextEntry(seq, &pos, &key, &hash)) {
     1391                Py_INCREF(key);
     1392                Py_INCREF(value);
     1393                if (insertdict(mp, key, hash, value)) {
     1394                    Py_DECREF(d);
     1395                    return NULL;
     1396                }
     1397            }
     1398            return d;
     1399        }
     1400    }
     1401
     1402    it = PyObject_GetIter(seq);
     1403    if (it == NULL){
     1404        Py_DECREF(d);
     1405        return NULL;
     1406    }
     1407
     1408    if (PyDict_CheckExact(d)) {
     1409        while ((key = PyIter_Next(it)) != NULL) {
     1410            status = PyDict_SetItem(d, key, value);
     1411            Py_DECREF(key);
     1412            if (status < 0)
     1413                goto Fail;
     1414        }
     1415    } else {
     1416        while ((key = PyIter_Next(it)) != NULL) {
     1417            status = PyObject_SetItem(d, key, value);
     1418            Py_DECREF(key);
     1419            if (status < 0)
     1420                goto Fail;
     1421        }
     1422    }
     1423
     1424    if (PyErr_Occurred())
     1425        goto Fail;
     1426    Py_DECREF(it);
     1427    return d;
    13161428
    13171429Fail:
    1318         Py_DECREF(it);
    1319         Py_DECREF(d);
    1320         return NULL;
     1430    Py_DECREF(it);
     1431    Py_DECREF(d);
     1432    return NULL;
    13211433}
    13221434
     
    13241436dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname)
    13251437{
    1326         PyObject *arg = NULL;
    1327         int result = 0;
    1328 
    1329         if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
    1330                 result = -1;
    1331 
    1332         else if (arg != NULL) {
    1333                 if (PyObject_HasAttrString(arg, "keys"))
    1334                         result = PyDict_Merge(self, arg, 1);
    1335                 else
    1336                         result = PyDict_MergeFromSeq2(self, arg, 1);
    1337         }
    1338         if (result == 0 && kwds != NULL)
    1339                 result = PyDict_Merge(self, kwds, 1);
    1340         return result;
     1438    PyObject *arg = NULL;
     1439    int result = 0;
     1440
     1441    if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg))
     1442        result = -1;
     1443
     1444    else if (arg != NULL) {
     1445        if (PyObject_HasAttrString(arg, "keys"))
     1446            result = PyDict_Merge(self, arg, 1);
     1447        else
     1448            result = PyDict_MergeFromSeq2(self, arg, 1);
     1449    }
     1450    if (result == 0 && kwds != NULL)
     1451        result = PyDict_Merge(self, kwds, 1);
     1452    return result;
    13411453}
    13421454
     
    13441456dict_update(PyObject *self, PyObject *args, PyObject *kwds)
    13451457{
    1346         if (dict_update_common(self, args, kwds, "update") != -1)
    1347                 Py_RETURN_NONE;
    1348         return NULL;
     1458    if (dict_update_common(self, args, kwds, "update") != -1)
     1459        Py_RETURN_NONE;
     1460    return NULL;
    13491461}
    13501462
     
    13621474PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override)
    13631475{
    1364         PyObject *it;   /* iter(seq2) */
    1365         Py_ssize_t i;   /* index into seq2 of current element */
    1366         PyObject *item; /* seq2[i] */
    1367         PyObject *fast; /* item as a 2-tuple or 2-list */
    1368 
    1369         assert(d != NULL);
    1370         assert(PyDict_Check(d));
    1371         assert(seq2 != NULL);
    1372 
    1373         it = PyObject_GetIter(seq2);
    1374         if (it == NULL)
    1375                 return -1;
    1376 
    1377         for (i = 0; ; ++i) {
    1378                 PyObject *key, *value;
    1379                 Py_ssize_t n;
    1380 
    1381                 fast = NULL;
    1382                 item = PyIter_Next(it);
    1383                 if (item == NULL) {
    1384                         if (PyErr_Occurred())
    1385                                 goto Fail;
    1386                         break;
    1387                 }
    1388 
    1389                 /* Convert item to sequence, and verify length 2. */
    1390                 fast = PySequence_Fast(item, "");
    1391                 if (fast == NULL) {
    1392                         if (PyErr_ExceptionMatches(PyExc_TypeError))
    1393                                 PyErr_Format(PyExc_TypeError,
    1394                                         "cannot convert dictionary update "
    1395                                         "sequence element #%zd to a sequence",
    1396                                         i);
    1397                         goto Fail;
    1398                 }
    1399                 n = PySequence_Fast_GET_SIZE(fast);
    1400                 if (n != 2) {
    1401                         PyErr_Format(PyExc_ValueError,
    1402                                      "dictionary update sequence element #%zd "
    1403                                      "has length %zd; 2 is required",
    1404                                      i, n);
    1405                         goto Fail;
    1406                 }
    1407 
    1408                 /* Update/merge with this (key, value) pair. */
    1409                 key = PySequence_Fast_GET_ITEM(fast, 0);
    1410                 value = PySequence_Fast_GET_ITEM(fast, 1);
    1411                 if (override || PyDict_GetItem(d, key) == NULL) {
    1412                         int status = PyDict_SetItem(d, key, value);
    1413                         if (status < 0)
    1414                                 goto Fail;
    1415                 }
    1416                 Py_DECREF(fast);
    1417                 Py_DECREF(item);
    1418         }
    1419 
    1420         i = 0;
    1421         goto Return;
     1476    PyObject *it;       /* iter(seq2) */
     1477    Py_ssize_t i;       /* index into seq2 of current element */
     1478    PyObject *item;     /* seq2[i] */
     1479    PyObject *fast;     /* item as a 2-tuple or 2-list */
     1480
     1481    assert(d != NULL);
     1482    assert(PyDict_Check(d));
     1483    assert(seq2 != NULL);
     1484
     1485    it = PyObject_GetIter(seq2);
     1486    if (it == NULL)
     1487        return -1;
     1488
     1489    for (i = 0; ; ++i) {
     1490        PyObject *key, *value;
     1491        Py_ssize_t n;
     1492
     1493        fast = NULL;
     1494        item = PyIter_Next(it);
     1495        if (item == NULL) {
     1496            if (PyErr_Occurred())
     1497                goto Fail;
     1498            break;
     1499        }
     1500
     1501        /* Convert item to sequence, and verify length 2. */
     1502        fast = PySequence_Fast(item, "");
     1503        if (fast == NULL) {
     1504            if (PyErr_ExceptionMatches(PyExc_TypeError))
     1505                PyErr_Format(PyExc_TypeError,
     1506                    "cannot convert dictionary update "
     1507                    "sequence element #%zd to a sequence",
     1508                    i);
     1509            goto Fail;
     1510        }
     1511        n = PySequence_Fast_GET_SIZE(fast);
     1512        if (n != 2) {
     1513            PyErr_Format(PyExc_ValueError,
     1514                         "dictionary update sequence element #%zd "
     1515                         "has length %zd; 2 is required",
     1516                         i, n);
     1517            goto Fail;
     1518        }
     1519
     1520        /* Update/merge with this (key, value) pair. */
     1521        key = PySequence_Fast_GET_ITEM(fast, 0);
     1522        value = PySequence_Fast_GET_ITEM(fast, 1);
     1523        if (override || PyDict_GetItem(d, key) == NULL) {
     1524            int status = PyDict_SetItem(d, key, value);
     1525            if (status < 0)
     1526                goto Fail;
     1527        }
     1528        Py_DECREF(fast);
     1529        Py_DECREF(item);
     1530    }
     1531
     1532    i = 0;
     1533    goto Return;
    14221534Fail:
    1423         Py_XDECREF(item);
    1424         Py_XDECREF(fast);
    1425         i = -1;
     1535    Py_XDECREF(item);
     1536    Py_XDECREF(fast);
     1537    i = -1;
    14261538Return:
    1427         Py_DECREF(it);
    1428         return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
     1539    Py_DECREF(it);
     1540    return Py_SAFE_DOWNCAST(i, Py_ssize_t, int);
    14291541}
    14301542
     
    14321544PyDict_Update(PyObject *a, PyObject *b)
    14331545{
    1434         return PyDict_Merge(a, b, 1);
     1546    return PyDict_Merge(a, b, 1);
    14351547}
    14361548
     
    14381550PyDict_Merge(PyObject *a, PyObject *b, int override)
    14391551{
    1440         register PyDictObject *mp, *other;
    1441         register Py_ssize_t i;
    1442         PyDictEntry *entry;
    1443 
    1444         /* We accept for the argument either a concrete dictionary object,
    1445         * or an abstract "mapping" object.  For the former, we can do
    1446         * things quite efficiently.  For the latter, we only require that
    1447         * PyMapping_Keys() and PyObject_GetItem() be supported.
    1448         */
    1449         if (a == NULL || !PyDict_Check(a) || b == NULL) {
    1450                 PyErr_BadInternalCall();
    1451                 return -1;
    1452         }
    1453         mp = (PyDictObject*)a;
    1454         if (PyDict_Check(b)) {
    1455                 other = (PyDictObject*)b;
    1456                 if (other == mp || other->ma_used == 0)
    1457                         /* a.update(a) or a.update({}); nothing to do */
    1458                         return 0;
    1459                 if (mp->ma_used == 0)
    1460                         /* Since the target dict is empty, PyDict_GetItem()
    1461                         * always returns NULL.  Setting override to 1
    1462                         * skips the unnecessary test.
    1463                         */
    1464                         override = 1;
    1465                 /* Do one big resize at the start, rather than
    1466                 * incrementally resizing as we insert new items.  Expect
    1467                 * that there will be no (or few) overlapping keys.
    1468                 */
    1469                 if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
    1470                    if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
    1471                            return -1;
    1472                 }
    1473                 for (i = 0; i <= other->ma_mask; i++) {
    1474                         entry = &other->ma_table[i];
    1475                         if (entry->me_value != NULL &&
    1476                             (override ||
    1477                              PyDict_GetItem(a, entry->me_key) == NULL)) {
    1478                                 Py_INCREF(entry->me_key);
    1479                                 Py_INCREF(entry->me_value);
    1480                                 if (insertdict(mp, entry->me_key,
    1481                                                (long)entry->me_hash,
    1482                                                entry->me_value) != 0)
    1483                                         return -1;
    1484                         }
    1485                 }
    1486         }
    1487         else {
    1488                 /* Do it the generic, slower way */
    1489                 PyObject *keys = PyMapping_Keys(b);
    1490                 PyObject *iter;
    1491                 PyObject *key, *value;
    1492                 int status;
    1493 
    1494                 if (keys == NULL)
    1495                         /* Docstring says this is equivalent to E.keys() so
    1496                         * if E doesn't have a .keys() method we want
    1497                         * AttributeError to percolate up.  Might as well
    1498                         * do the same for any other error.
    1499                         */
    1500                         return -1;
    1501 
    1502                 iter = PyObject_GetIter(keys);
    1503                 Py_DECREF(keys);
    1504                 if (iter == NULL)
    1505                         return -1;
    1506 
    1507                 for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
    1508                         if (!override && PyDict_GetItem(a, key) != NULL) {
    1509                                 Py_DECREF(key);
    1510                                 continue;
    1511                         }
    1512                         value = PyObject_GetItem(b, key);
    1513                         if (value == NULL) {
    1514                                 Py_DECREF(iter);
    1515                                 Py_DECREF(key);
    1516                                 return -1;
    1517                         }
    1518                         status = PyDict_SetItem(a, key, value);
    1519                         Py_DECREF(key);
    1520                         Py_DECREF(value);
    1521                         if (status < 0) {
    1522                                 Py_DECREF(iter);
    1523                                 return -1;
    1524                         }
    1525                 }
    1526                 Py_DECREF(iter);
    1527                 if (PyErr_Occurred())
    1528                         /* Iterator completed, via error */
    1529                         return -1;
    1530         }
    1531         return 0;
     1552    register PyDictObject *mp, *other;
     1553    register Py_ssize_t i;
     1554    PyDictEntry *entry;
     1555
     1556    /* We accept for the argument either a concrete dictionary object,
     1557    * or an abstract "mapping" object.  For the former, we can do
     1558    * things quite efficiently.  For the latter, we only require that
     1559    * PyMapping_Keys() and PyObject_GetItem() be supported.
     1560    */
     1561    if (a == NULL || !PyDict_Check(a) || b == NULL) {
     1562        PyErr_BadInternalCall();
     1563        return -1;
     1564    }
     1565    mp = (PyDictObject*)a;
     1566    if (PyDict_Check(b)) {
     1567        other = (PyDictObject*)b;
     1568        if (other == mp || other->ma_used == 0)
     1569            /* a.update(a) or a.update({}); nothing to do */
     1570            return 0;
     1571        if (mp->ma_used == 0)
     1572            /* Since the target dict is empty, PyDict_GetItem()
     1573            * always returns NULL.  Setting override to 1
     1574            * skips the unnecessary test.
     1575            */
     1576            override = 1;
     1577        /* Do one big resize at the start, rather than
     1578        * incrementally resizing as we insert new items.  Expect
     1579        * that there will be no (or few) overlapping keys.
     1580        */
     1581        if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) {
     1582           if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0)
     1583               return -1;
     1584        }
     1585        for (i = 0; i <= other->ma_mask; i++) {
     1586            entry = &other->ma_table[i];
     1587            if (entry->me_value != NULL &&
     1588                (override ||
     1589                 PyDict_GetItem(a, entry->me_key) == NULL)) {
     1590                Py_INCREF(entry->me_key);
     1591                Py_INCREF(entry->me_value);
     1592                if (insertdict(mp, entry->me_key,
     1593                               (long)entry->me_hash,
     1594                               entry->me_value) != 0)
     1595                    return -1;
     1596            }
     1597        }
     1598    }
     1599    else {
     1600        /* Do it the generic, slower way */
     1601        PyObject *keys = PyMapping_Keys(b);
     1602        PyObject *iter;
     1603        PyObject *key, *value;
     1604        int status;
     1605
     1606        if (keys == NULL)
     1607            /* Docstring says this is equivalent to E.keys() so
     1608            * if E doesn't have a .keys() method we want
     1609            * AttributeError to percolate up.  Might as well
     1610            * do the same for any other error.
     1611            */
     1612            return -1;
     1613
     1614        iter = PyObject_GetIter(keys);
     1615        Py_DECREF(keys);
     1616        if (iter == NULL)
     1617            return -1;
     1618
     1619        for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) {
     1620            if (!override && PyDict_GetItem(a, key) != NULL) {
     1621                Py_DECREF(key);
     1622                continue;
     1623            }
     1624            value = PyObject_GetItem(b, key);
     1625            if (value == NULL) {
     1626                Py_DECREF(iter);
     1627                Py_DECREF(key);
     1628                return -1;
     1629            }
     1630            status = PyDict_SetItem(a, key, value);
     1631            Py_DECREF(key);
     1632            Py_DECREF(value);
     1633            if (status < 0) {
     1634                Py_DECREF(iter);
     1635                return -1;
     1636            }
     1637        }
     1638        Py_DECREF(iter);
     1639        if (PyErr_Occurred())
     1640            /* Iterator completed, via error */
     1641            return -1;
     1642    }
     1643    return 0;
    15321644}
    15331645
     
    15351647dict_copy(register PyDictObject *mp)
    15361648{
    1537         return PyDict_Copy((PyObject*)mp);
     1649    return PyDict_Copy((PyObject*)mp);
    15381650}
    15391651
     
    15411653PyDict_Copy(PyObject *o)
    15421654{
    1543         PyObject *copy;
    1544 
    1545         if (o == NULL || !PyDict_Check(o)) {
    1546                 PyErr_BadInternalCall();
    1547                 return NULL;
    1548         }
    1549         copy = PyDict_New();
    1550         if (copy == NULL)
    1551                 return NULL;
    1552         if (PyDict_Merge(copy, o, 1) == 0)
    1553                 return copy;
    1554         Py_DECREF(copy);
    1555         return NULL;
     1655    PyObject *copy;
     1656
     1657    if (o == NULL || !PyDict_Check(o)) {
     1658        PyErr_BadInternalCall();
     1659        return NULL;
     1660    }
     1661    copy = PyDict_New();
     1662    if (copy == NULL)
     1663        return NULL;
     1664    if (PyDict_Merge(copy, o, 1) == 0)
     1665        return copy;
     1666    Py_DECREF(copy);
     1667    return NULL;
    15561668}
    15571669
     
    15591671PyDict_Size(PyObject *mp)
    15601672{
    1561         if (mp == NULL || !PyDict_Check(mp)) {
    1562                 PyErr_BadInternalCall();
    1563                 return -1;
    1564         }
    1565         return ((PyDictObject *)mp)->ma_used;
     1673    if (mp == NULL || !PyDict_Check(mp)) {
     1674        PyErr_BadInternalCall();
     1675        return -1;
     1676    }
     1677    return ((PyDictObject *)mp)->ma_used;
    15661678}
    15671679
     
    15691681PyDict_Keys(PyObject *mp)
    15701682{
    1571         if (mp == NULL || !PyDict_Check(mp)) {
    1572                 PyErr_BadInternalCall();
    1573                 return NULL;
    1574         }
    1575         return dict_keys((PyDictObject *)mp);
     1683    if (mp == NULL || !PyDict_Check(mp)) {
     1684        PyErr_BadInternalCall();
     1685        return NULL;
     1686    }
     1687    return dict_keys((PyDictObject *)mp);
    15761688}
    15771689
     
    15791691PyDict_Values(PyObject *mp)
    15801692{
    1581         if (mp == NULL || !PyDict_Check(mp)) {
    1582                 PyErr_BadInternalCall();
    1583                 return NULL;
    1584         }
    1585         return dict_values((PyDictObject *)mp);
     1693    if (mp == NULL || !PyDict_Check(mp)) {
     1694        PyErr_BadInternalCall();
     1695        return NULL;
     1696    }
     1697    return dict_values((PyDictObject *)mp);
    15861698}
    15871699
     
    15891701PyDict_Items(PyObject *mp)
    15901702{
    1591         if (mp == NULL || !PyDict_Check(mp)) {
    1592                 PyErr_BadInternalCall();
    1593                 return NULL;
    1594         }
    1595         return dict_items((PyDictObject *)mp);
     1703    if (mp == NULL || !PyDict_Check(mp)) {
     1704        PyErr_BadInternalCall();
     1705        return NULL;
     1706    }
     1707    return dict_items((PyDictObject *)mp);
    15961708}
    15971709
     
    16071719characterize(PyDictObject *a, PyDictObject *b, PyObject **pval)
    16081720{
    1609         PyObject *akey = NULL; /* smallest key in a s.t. a[akey] != b[akey] */
    1610         PyObject *aval = NULL; /* a[akey] */
    1611         Py_ssize_t i;
    1612         int cmp;
    1613 
    1614         for (i = 0; i <= a->ma_mask; i++) {
    1615                 PyObject *thiskey, *thisaval, *thisbval;
    1616                 if (a->ma_table[i].me_value == NULL)
    1617                         continue;
    1618                 thiskey = a->ma_table[i].me_key;
    1619                 Py_INCREF(thiskey);  /* keep alive across compares */
    1620                 if (akey != NULL) {
    1621                         cmp = PyObject_RichCompareBool(akey, thiskey, Py_LT);
    1622                         if (cmp < 0) {
    1623                                 Py_DECREF(thiskey);
    1624                                 goto Fail;
    1625                         }
    1626                         if (cmp > 0 ||
    1627                             i > a->ma_mask ||
    1628                             a->ma_table[i].me_value == NULL)
    1629                         {
    1630                                 /* Not the *smallest* a key; or maybe it is
    1631                                 * but the compare shrunk the dict so we can't
    1632                                 * find its associated value anymore; or
    1633                                 * maybe it is but the compare deleted the
    1634                                 * a[thiskey] entry.
    1635                                 */
    1636                                 Py_DECREF(thiskey);
    1637                                 continue;
    1638                         }
    1639                 }
    1640 
    1641                 /* Compare a[thiskey] to b[thiskey]; cmp <- true iff equal. */
    1642                 thisaval = a->ma_table[i].me_value;
    1643                 assert(thisaval);
    1644                 Py_INCREF(thisaval);   /* keep alive */
    1645                 thisbval = PyDict_GetItem((PyObject *)b, thiskey);
    1646                 if (thisbval == NULL)
    1647                         cmp = 0;
    1648                 else {
    1649                         /* both dicts have thiskey:  same values? */
    1650                         cmp = PyObject_RichCompareBool(
    1651                                                 thisaval, thisbval, Py_EQ);
    1652                         if (cmp < 0) {
    1653                                 Py_DECREF(thiskey);
    1654                                 Py_DECREF(thisaval);
    1655                                 goto Fail;
    1656                         }
    1657                 }
    1658                 if (cmp == 0) {
    1659                         /* New winner. */
    1660                         Py_XDECREF(akey);
    1661                         Py_XDECREF(aval);
    1662                         akey = thiskey;
    1663                         aval = thisaval;
    1664                 }
    1665                 else {
    1666                         Py_DECREF(thiskey);
    1667                         Py_DECREF(thisaval);
    1668                 }
    1669         }
    1670         *pval = aval;
    1671         return akey;
     1721    PyObject *akey = NULL; /* smallest key in a s.t. a[akey] != b[akey] */
     1722    PyObject *aval = NULL; /* a[akey] */
     1723    Py_ssize_t i;
     1724    int cmp;
     1725
     1726    for (i = 0; i <= a->ma_mask; i++) {
     1727        PyObject *thiskey, *thisaval, *thisbval;
     1728        if (a->ma_table[i].me_value == NULL)
     1729            continue;
     1730        thiskey = a->ma_table[i].me_key;
     1731        Py_INCREF(thiskey);  /* keep alive across compares */
     1732        if (akey != NULL) {
     1733            cmp = PyObject_RichCompareBool(akey, thiskey, Py_LT);
     1734            if (cmp < 0) {
     1735                Py_DECREF(thiskey);
     1736                goto Fail;
     1737            }
     1738            if (cmp > 0 ||
     1739                i > a->ma_mask ||
     1740                a->ma_table[i].me_value == NULL)
     1741            {
     1742                /* Not the *smallest* a key; or maybe it is
     1743                * but the compare shrunk the dict so we can't
     1744                * find its associated value anymore; or
     1745                * maybe it is but the compare deleted the
     1746                * a[thiskey] entry.
     1747                */
     1748                Py_DECREF(thiskey);
     1749                continue;
     1750            }
     1751        }
     1752
     1753        /* Compare a[thiskey] to b[thiskey]; cmp <- true iff equal. */
     1754        thisaval = a->ma_table[i].me_value;
     1755        assert(thisaval);
     1756        Py_INCREF(thisaval);   /* keep alive */
     1757        thisbval = PyDict_GetItem((PyObject *)b, thiskey);
     1758        if (thisbval == NULL)
     1759            cmp = 0;
     1760        else {
     1761            /* both dicts have thiskey:  same values? */
     1762            cmp = PyObject_RichCompareBool(
     1763                                    thisaval, thisbval, Py_EQ);
     1764            if (cmp < 0) {
     1765                Py_DECREF(thiskey);
     1766                Py_DECREF(thisaval);
     1767                goto Fail;
     1768            }
     1769        }
     1770        if (cmp == 0) {
     1771            /* New winner. */
     1772            Py_XDECREF(akey);
     1773            Py_XDECREF(aval);
     1774            akey = thiskey;
     1775            aval = thisaval;
     1776        }
     1777        else {
     1778            Py_DECREF(thiskey);
     1779            Py_DECREF(thisaval);
     1780        }
     1781    }
     1782    *pval = aval;
     1783    return akey;
    16721784
    16731785Fail:
    1674         Py_XDECREF(akey);
    1675         Py_XDECREF(aval);
    1676         *pval = NULL;
    1677         return NULL;
     1786    Py_XDECREF(akey);
     1787    Py_XDECREF(aval);
     1788    *pval = NULL;
     1789    return NULL;
    16781790}
    16791791
     
    16811793dict_compare(PyDictObject *a, PyDictObject *b)
    16821794{
    1683         PyObject *adiff, *bdiff, *aval, *bval;
    1684         int res;
    1685 
    1686         /* Compare lengths first */
    1687         if (a->ma_used < b->ma_used)
    1688                 return -1;      /* a is shorter */
    1689         else if (a->ma_used > b->ma_used)
    1690                 return 1;       /* b is shorter */
    1691 
    1692         /* Same length -- check all keys */
    1693         bdiff = bval = NULL;
    1694         adiff = characterize(a, b, &aval);
    1695         if (adiff == NULL) {
    1696                 assert(!aval);
    1697                 /* Either an error, or a is a subset with the same length so
    1698                 * must be equal.
    1699                 */
    1700                 res = PyErr_Occurred() ? -1 : 0;
    1701                 goto Finished;
    1702         }
    1703         bdiff = characterize(b, a, &bval);
    1704         if (bdiff == NULL && PyErr_Occurred()) {
    1705                 assert(!bval);
    1706                 res = -1;
    1707                 goto Finished;
    1708         }
    1709         res = 0;
    1710         if (bdiff) {
    1711                 /* bdiff == NULL "should be" impossible now, but perhaps
    1712                 * the last comparison done by the characterize() on a had
    1713                 * the side effect of making the dicts equal!
    1714                 */
    1715                 res = PyObject_Compare(adiff, bdiff);
    1716         }
    1717         if (res == 0 && bval != NULL)
    1718                 res = PyObject_Compare(aval, bval);
     1795    PyObject *adiff, *bdiff, *aval, *bval;
     1796    int res;
     1797
     1798    /* Compare lengths first */
     1799    if (a->ma_used < b->ma_used)
     1800        return -1;              /* a is shorter */
     1801    else if (a->ma_used > b->ma_used)
     1802        return 1;               /* b is shorter */
     1803
     1804    /* Same length -- check all keys */
     1805    bdiff = bval = NULL;
     1806    adiff = characterize(a, b, &aval);
     1807    if (adiff == NULL) {
     1808        assert(!aval);
     1809        /* Either an error, or a is a subset with the same length so
     1810        * must be equal.
     1811        */
     1812        res = PyErr_Occurred() ? -1 : 0;
     1813        goto Finished;
     1814    }
     1815    bdiff = characterize(b, a, &bval);
     1816    if (bdiff == NULL && PyErr_Occurred()) {
     1817        assert(!bval);
     1818        res = -1;
     1819        goto Finished;
     1820    }
     1821    res = 0;
     1822    if (bdiff) {
     1823        /* bdiff == NULL "should be" impossible now, but perhaps
     1824        * the last comparison done by the characterize() on a had
     1825        * the side effect of making the dicts equal!
     1826        */
     1827        res = PyObject_Compare(adiff, bdiff);
     1828    }
     1829    if (res == 0 && bval != NULL)
     1830        res = PyObject_Compare(aval, bval);
    17191831
    17201832Finished:
    1721         Py_XDECREF(adiff);
    1722         Py_XDECREF(bdiff);
    1723         Py_XDECREF(aval);
    1724         Py_XDECREF(bval);
    1725         return res;
     1833    Py_XDECREF(adiff);
     1834    Py_XDECREF(bdiff);
     1835    Py_XDECREF(aval);
     1836    Py_XDECREF(bval);
     1837    return res;
    17261838}
    17271839
     
    17331845dict_equal(PyDictObject *a, PyDictObject *b)
    17341846{
    1735         Py_ssize_t i;
    1736 
    1737         if (a->ma_used != b->ma_used)
    1738                 /* can't be equal if # of entries differ */
    1739                 return 0;
    1740 
    1741         /* Same # of entries -- check all of 'em.  Exit early on any diff. */
    1742         for (i = 0; i <= a->ma_mask; i++) {
    1743                 PyObject *aval = a->ma_table[i].me_value;
    1744                 if (aval != NULL) {
    1745                         int cmp;
    1746                         PyObject *bval;
    1747                         PyObject *key = a->ma_table[i].me_key;
    1748                         /* temporarily bump aval's refcount to ensure it stays
    1749                            alive until we're done with it */
    1750                         Py_INCREF(aval);
    1751                         /* ditto for key */
    1752                         Py_INCREF(key);
    1753                         bval = PyDict_GetItem((PyObject *)b, key);
    1754                         Py_DECREF(key);
    1755                         if (bval == NULL) {
    1756                                 Py_DECREF(aval);
    1757                                 return 0;
    1758                         }
    1759                         cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
    1760                         Py_DECREF(aval);
    1761                         if (cmp <= 0)  /* error or not equal */
    1762                                 return cmp;
    1763                 }
    1764         }
    1765         return 1;
     1847    Py_ssize_t i;
     1848
     1849    if (a->ma_used != b->ma_used)
     1850        /* can't be equal if # of entries differ */
     1851        return 0;
     1852
     1853    /* Same # of entries -- check all of 'em.  Exit early on any diff. */
     1854    for (i = 0; i <= a->ma_mask; i++) {
     1855        PyObject *aval = a->ma_table[i].me_value;
     1856        if (aval != NULL) {
     1857            int cmp;
     1858            PyObject *bval;
     1859            PyObject *key = a->ma_table[i].me_key;
     1860            /* temporarily bump aval's refcount to ensure it stays
     1861               alive until we're done with it */
     1862            Py_INCREF(aval);
     1863            /* ditto for key */
     1864            Py_INCREF(key);
     1865            bval = PyDict_GetItem((PyObject *)b, key);
     1866            Py_DECREF(key);
     1867            if (bval == NULL) {
     1868                Py_DECREF(aval);
     1869                return 0;
     1870            }
     1871            cmp = PyObject_RichCompareBool(aval, bval, Py_EQ);
     1872            Py_DECREF(aval);
     1873            if (cmp <= 0)  /* error or not equal */
     1874                return cmp;
     1875        }
     1876    }
     1877    return 1;
    17661878 }
    17671879
     
    17691881dict_richcompare(PyObject *v, PyObject *w, int op)
    17701882{
    1771         int cmp;
    1772         PyObject *res;
    1773 
    1774         if (!PyDict_Check(v) || !PyDict_Check(w)) {
    1775                 res = Py_NotImplemented;
    1776         }
    1777         else if (op == Py_EQ || op == Py_NE) {
    1778                 cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
    1779                 if (cmp < 0)
    1780                         return NULL;
    1781                 res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
    1782         }
    1783         else {
    1784                 /* Py3K warning if comparison isn't == or !=  */
    1785                 if (PyErr_WarnPy3k("dict inequality comparisons not supported "
    1786                                    "in 3.x", 1) < 0) {
    1787                         return NULL;
    1788                 }
    1789                 res = Py_NotImplemented;
    1790         }
    1791         Py_INCREF(res);
    1792         return res;
     1883    int cmp;
     1884    PyObject *res;
     1885
     1886    if (!PyDict_Check(v) || !PyDict_Check(w)) {
     1887        res = Py_NotImplemented;
     1888    }
     1889    else if (op == Py_EQ || op == Py_NE) {
     1890        cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w);
     1891        if (cmp < 0)
     1892            return NULL;
     1893        res = (cmp == (op == Py_EQ)) ? Py_True : Py_False;
     1894    }
     1895    else {
     1896        /* Py3K warning if comparison isn't == or !=  */
     1897        if (PyErr_WarnPy3k("dict inequality comparisons not supported "
     1898                           "in 3.x", 1) < 0) {
     1899            return NULL;
     1900        }
     1901        res = Py_NotImplemented;
     1902    }
     1903    Py_INCREF(res);
     1904    return res;
    17931905 }
    17941906
     
    17961908dict_contains(register PyDictObject *mp, PyObject *key)
    17971909{
    1798         long hash;
    1799         PyDictEntry *ep;
    1800 
    1801         if (!PyString_CheckExact(key) ||
    1802             (hash = ((PyStringObject *) key)->ob_shash) == -1) {
    1803                 hash = PyObject_Hash(key);
    1804                 if (hash == -1)
    1805                         return NULL;
    1806         }
    1807         ep = (mp->ma_lookup)(mp, key, hash);
    1808         if (ep == NULL)
    1809                 return NULL;
    1810         return PyBool_FromLong(ep->me_value != NULL);
     1910    long hash;
     1911    PyDictEntry *ep;
     1912
     1913    if (!PyString_CheckExact(key) ||
     1914        (hash = ((PyStringObject *) key)->ob_shash) == -1) {
     1915        hash = PyObject_Hash(key);
     1916        if (hash == -1)
     1917            return NULL;
     1918    }
     1919    ep = (mp->ma_lookup)(mp, key, hash);
     1920    if (ep == NULL)
     1921        return NULL;
     1922    return PyBool_FromLong(ep->me_value != NULL);
    18111923}
    18121924
     
    18141926dict_has_key(register PyDictObject *mp, PyObject *key)
    18151927{
    1816         if (PyErr_WarnPy3k("dict.has_key() not supported in 3.x; "
    1817                            "use the in operator", 1) < 0)
    1818                 return NULL;
    1819         return dict_contains(mp, key);
     1928    if (PyErr_WarnPy3k("dict.has_key() not supported in 3.x; "
     1929                       "use the in operator", 1) < 0)
     1930        return NULL;
     1931    return dict_contains(mp, key);
    18201932}
    18211933
     
    18231935dict_get(register PyDictObject *mp, PyObject *args)
    18241936{
    1825         PyObject *key;
    1826         PyObject *failobj = Py_None;
    1827         PyObject *val = NULL;
    1828         long hash;
    1829         PyDictEntry *ep;
    1830 
    1831         if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
    1832                 return NULL;
    1833 
    1834         if (!PyString_CheckExact(key) ||
    1835             (hash = ((PyStringObject *) key)->ob_shash) == -1) {
    1836                 hash = PyObject_Hash(key);
    1837                 if (hash == -1)
    1838                         return NULL;
    1839         }
    1840         ep = (mp->ma_lookup)(mp, key, hash);
    1841         if (ep == NULL)
    1842                 return NULL;
    1843         val = ep->me_value;
    1844         if (val == NULL)
    1845                 val = failobj;
    1846         Py_INCREF(val);
    1847         return val;
     1937    PyObject *key;
     1938    PyObject *failobj = Py_None;
     1939    PyObject *val = NULL;
     1940    long hash;
     1941    PyDictEntry *ep;
     1942
     1943    if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj))
     1944        return NULL;
     1945
     1946    if (!PyString_CheckExact(key) ||
     1947        (hash = ((PyStringObject *) key)->ob_shash) == -1) {
     1948        hash = PyObject_Hash(key);
     1949        if (hash == -1)
     1950            return NULL;
     1951    }
     1952    ep = (mp->ma_lookup)(mp, key, hash);
     1953    if (ep == NULL)
     1954        return NULL;
     1955    val = ep->me_value;
     1956    if (val == NULL)
     1957        val = failobj;
     1958    Py_INCREF(val);
     1959    return val;
    18481960}
    18491961
     
    18521964dict_setdefault(register PyDictObject *mp, PyObject *args)
    18531965{
    1854         PyObject *key;
    1855         PyObject *failobj = Py_None;
    1856         PyObject *val = NULL;
    1857         long hash;
    1858         PyDictEntry *ep;
    1859 
    1860         if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
    1861                 return NULL;
    1862 
    1863         if (!PyString_CheckExact(key) ||
    1864             (hash = ((PyStringObject *) key)->ob_shash) == -1) {
    1865                 hash = PyObject_Hash(key);
    1866                 if (hash == -1)
    1867                         return NULL;
    1868         }
    1869         ep = (mp->ma_lookup)(mp, key, hash);
    1870         if (ep == NULL)
    1871                 return NULL;
    1872         val = ep->me_value;
    1873         if (val == NULL) {
    1874                 val = failobj;
    1875                 if (PyDict_SetItem((PyObject*)mp, key, failobj))
    1876                         val = NULL;
    1877         }
    1878         Py_XINCREF(val);
    1879         return val;
     1966    PyObject *key;
     1967    PyObject *failobj = Py_None;
     1968    PyObject *val = NULL;
     1969    long hash;
     1970    PyDictEntry *ep;
     1971
     1972    if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj))
     1973        return NULL;
     1974
     1975    if (!PyString_CheckExact(key) ||
     1976        (hash = ((PyStringObject *) key)->ob_shash) == -1) {
     1977        hash = PyObject_Hash(key);
     1978        if (hash == -1)
     1979            return NULL;
     1980    }
     1981    ep = (mp->ma_lookup)(mp, key, hash);
     1982    if (ep == NULL)
     1983        return NULL;
     1984    val = ep->me_value;
     1985    if (val == NULL) {
     1986        if (dict_set_item_by_hash_or_entry((PyObject*)mp, key, hash, ep,
     1987                                           failobj) == 0)
     1988            val = failobj;
     1989    }
     1990    Py_XINCREF(val);
     1991    return val;
    18801992}
    18811993
     
    18841996dict_clear(register PyDictObject *mp)
    18851997{
    1886         PyDict_Clear((PyObject *)mp);
    1887         Py_RETURN_NONE;
     1998    PyDict_Clear((PyObject *)mp);
     1999    Py_RETURN_NONE;
    18882000}
    18892001
     
    18912003dict_pop(PyDictObject *mp, PyObject *args)
    18922004{
    1893         long hash;
    1894         PyDictEntry *ep;
    1895         PyObject *old_value, *old_key;
    1896         PyObject *key, *deflt = NULL;
    1897 
    1898         if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
    1899                 return NULL;
    1900         if (mp->ma_used == 0) {
    1901                 if (deflt) {
    1902                         Py_INCREF(deflt);
    1903                         return deflt;
    1904                 }
    1905                 PyErr_SetString(PyExc_KeyError,
    1906                                 "pop(): dictionary is empty");
    1907                 return NULL;
    1908         }
    1909         if (!PyString_CheckExact(key) ||
    1910             (hash = ((PyStringObject *) key)->ob_shash) == -1) {
    1911                 hash = PyObject_Hash(key);
    1912                 if (hash == -1)
    1913                         return NULL;
    1914         }
    1915         ep = (mp->ma_lookup)(mp, key, hash);
    1916         if (ep == NULL)
    1917                 return NULL;
    1918         if (ep->me_value == NULL) {
    1919                 if (deflt) {
    1920                         Py_INCREF(deflt);
    1921                         return deflt;
    1922                 }
    1923                 set_key_error(key);
    1924                 return NULL;
    1925         }
    1926         old_key = ep->me_key;
    1927         Py_INCREF(dummy);
    1928         ep->me_key = dummy;
    1929         old_value = ep->me_value;
    1930         ep->me_value = NULL;
    1931         mp->ma_used--;
    1932         Py_DECREF(old_key);
    1933         return old_value;
     2005    long hash;
     2006    PyDictEntry *ep;
     2007    PyObject *old_value, *old_key;
     2008    PyObject *key, *deflt = NULL;
     2009
     2010    if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt))
     2011        return NULL;
     2012    if (mp->ma_used == 0) {
     2013        if (deflt) {
     2014            Py_INCREF(deflt);
     2015            return deflt;
     2016        }
     2017        set_key_error(key);
     2018        return NULL;
     2019    }
     2020    if (!PyString_CheckExact(key) ||
     2021        (hash = ((PyStringObject *) key)->ob_shash) == -1) {
     2022        hash = PyObject_Hash(key);
     2023        if (hash == -1)
     2024            return NULL;
     2025    }
     2026    ep = (mp->ma_lookup)(mp, key, hash);
     2027    if (ep == NULL)
     2028        return NULL;
     2029    if (ep->me_value == NULL) {
     2030        if (deflt) {
     2031            Py_INCREF(deflt);
     2032            return deflt;
     2033        }
     2034        set_key_error(key);
     2035        return NULL;
     2036    }
     2037    old_key = ep->me_key;
     2038    Py_INCREF(dummy);
     2039    ep->me_key = dummy;
     2040    old_value = ep->me_value;
     2041    ep->me_value = NULL;
     2042    mp->ma_used--;
     2043    Py_DECREF(old_key);
     2044    return old_value;
    19342045}
    19352046
     
    19372048dict_popitem(PyDictObject *mp)
    19382049{
    1939         Py_ssize_t i = 0;
    1940         PyDictEntry *ep;
    1941         PyObject *res;
    1942 
    1943         /* Allocate the result tuple before checking the size.  Believe it
    1944         * or not, this allocation could trigger a garbage collection which
    1945         * could empty the dict, so if we checked the size first and that
    1946         * happened, the result would be an infinite loop (searching for an
    1947         * entry that no longer exists).  Note that the usual popitem()
    1948         * idiom is "while d: k, v = d.popitem()". so needing to throw the
    1949         * tuple away if the dict *is* empty isn't a significant
    1950         * inefficiency -- possible, but unlikely in practice.
    1951         */
    1952         res = PyTuple_New(2);
    1953         if (res == NULL)
    1954                 return NULL;
    1955         if (mp->ma_used == 0) {
    1956                 Py_DECREF(res);
    1957                 PyErr_SetString(PyExc_KeyError,
    1958                                 "popitem(): dictionary is empty");
    1959                 return NULL;
    1960         }
    1961         /* Set ep to "the first" dict entry with a value.  We abuse the hash
    1962         * field of slot 0 to hold a search finger:
    1963         * If slot 0 has a value, use slot 0.
    1964         * Else slot 0 is being used to hold a search finger,
    1965         * and we use its hash value as the first index to look.
    1966         */
    1967         ep = &mp->ma_table[0];
    1968         if (ep->me_value == NULL) {
    1969                 i = ep->me_hash;
    1970                 /* The hash field may be a real hash value, or it may be a
    1971                 * legit search finger, or it may be a once-legit search
    1972                 * finger that's out of bounds now because it wrapped around
    1973                 * or the table shrunk -- simply make sure it's in bounds now.
    1974                 */
    1975                 if (i > mp->ma_mask || i < 1)
    1976                         i = 1;  /* skip slot 0 */
    1977                 while ((ep = &mp->ma_table[i])->me_value == NULL) {
    1978                         i++;
    1979                         if (i > mp->ma_mask)
    1980                                 i = 1;
    1981                 }
    1982         }
    1983         PyTuple_SET_ITEM(res, 0, ep->me_key);
    1984         PyTuple_SET_ITEM(res, 1, ep->me_value);
    1985         Py_INCREF(dummy);
    1986         ep->me_key = dummy;
    1987         ep->me_value = NULL;
    1988         mp->ma_used--;
    1989         assert(mp->ma_table[0].me_value == NULL);
    1990         mp->ma_table[0].me_hash = i + 1;  /* next place to start */
    1991         return res;
     2050    Py_ssize_t i = 0;
     2051    PyDictEntry *ep;
     2052    PyObject *res;
     2053
     2054    /* Allocate the result tuple before checking the size.  Believe it
     2055    * or not, this allocation could trigger a garbage collection which
     2056    * could empty the dict, so if we checked the size first and that
     2057    * happened, the result would be an infinite loop (searching for an
     2058    * entry that no longer exists).  Note that the usual popitem()
     2059    * idiom is "while d: k, v = d.popitem()". so needing to throw the
     2060    * tuple away if the dict *is* empty isn't a significant
     2061    * inefficiency -- possible, but unlikely in practice.
     2062    */
     2063    res = PyTuple_New(2);
     2064    if (res == NULL)
     2065        return NULL;
     2066    if (mp->ma_used == 0) {
     2067        Py_DECREF(res);
     2068        PyErr_SetString(PyExc_KeyError,
     2069                        "popitem(): dictionary is empty");
     2070        return NULL;
     2071    }
     2072    /* Set ep to "the first" dict entry with a value.  We abuse the hash
     2073    * field of slot 0 to hold a search finger:
     2074    * If slot 0 has a value, use slot 0.
     2075    * Else slot 0 is being used to hold a search finger,
     2076    * and we use its hash value as the first index to look.
     2077    */
     2078    ep = &mp->ma_table[0];
     2079    if (ep->me_value == NULL) {
     2080        i = ep->me_hash;
     2081        /* The hash field may be a real hash value, or it may be a
     2082        * legit search finger, or it may be a once-legit search
     2083        * finger that's out of bounds now because it wrapped around
     2084        * or the table shrunk -- simply make sure it's in bounds now.
     2085        */
     2086        if (i > mp->ma_mask || i < 1)
     2087            i = 1;              /* skip slot 0 */
     2088        while ((ep = &mp->ma_table[i])->me_value == NULL) {
     2089            i++;
     2090            if (i > mp->ma_mask)
     2091                i = 1;
     2092        }
     2093    }
     2094    PyTuple_SET_ITEM(res, 0, ep->me_key);
     2095    PyTuple_SET_ITEM(res, 1, ep->me_value);
     2096    Py_INCREF(dummy);
     2097    ep->me_key = dummy;
     2098    ep->me_value = NULL;
     2099    mp->ma_used--;
     2100    assert(mp->ma_table[0].me_value == NULL);
     2101    mp->ma_table[0].me_hash = i + 1;  /* next place to start */
     2102    return res;
    19922103}
    19932104
     
    19952106dict_traverse(PyObject *op, visitproc visit, void *arg)
    19962107{
    1997         Py_ssize_t i = 0;
    1998         PyObject *pk;
    1999         PyObject *pv;
    2000 
    2001         while (PyDict_Next(op, &i, &pk, &pv)) {
    2002                 Py_VISIT(pk);
    2003                 Py_VISIT(pv);
    2004         }
    2005         return 0;
     2108    Py_ssize_t i = 0;
     2109    PyObject *pk;
     2110    PyObject *pv;
     2111
     2112    while (PyDict_Next(op, &i, &pk, &pv)) {
     2113        Py_VISIT(pk);
     2114        Py_VISIT(pv);
     2115    }
     2116    return 0;
    20062117}
    20072118
     
    20092120dict_tp_clear(PyObject *op)
    20102121{
    2011         PyDict_Clear(op);
    2012         return 0;
     2122    PyDict_Clear(op);
     2123    return 0;
    20132124}
    20142125
     
    20222133dict_iterkeys(PyDictObject *dict)
    20232134{
    2024         return dictiter_new(dict, &PyDictIterKey_Type);
     2135    return dictiter_new(dict, &PyDictIterKey_Type);
    20252136}
    20262137
     
    20282139dict_itervalues(PyDictObject *dict)
    20292140{
    2030         return dictiter_new(dict, &PyDictIterValue_Type);
     2141    return dictiter_new(dict, &PyDictIterValue_Type);
    20312142}
    20322143
     
    20342145dict_iteritems(PyDictObject *dict)
    20352146{
    2036         return dictiter_new(dict, &PyDictIterItem_Type);
     2147    return dictiter_new(dict, &PyDictIterItem_Type);
    20372148}
    20382149
     
    20402151dict_sizeof(PyDictObject *mp)
    20412152{
    2042         Py_ssize_t res;
    2043 
    2044         res = sizeof(PyDictObject);
    2045         if (mp->ma_table != mp->ma_smalltable)
    2046                 res = res + (mp->ma_mask + 1) * sizeof(PyDictEntry);
    2047         return PyInt_FromSsize_t(res);
     2153    Py_ssize_t res;
     2154
     2155    res = sizeof(PyDictObject);
     2156    if (mp->ma_table != mp->ma_smalltable)
     2157        res = res + (mp->ma_mask + 1) * sizeof(PyDictEntry);
     2158    return PyInt_FromSsize_t(res);
    20482159}
    20492160
     
    20832194
    20842195PyDoc_STRVAR(update__doc__,
    2085 "D.update(E, **F) -> None.  Update D from dict/iterable E and F.\n"
    2086 "If E has a .keys() method, does:     for k in E: D[k] = E[k]\n\
    2087 If E lacks .keys() method, does:     for (k, v) in E: D[k] = v\n\
     2196"D.update([E, ]**F) -> None.  Update D from dict/iterable E and F.\n"
     2197"If E present and has a .keys() method, does:     for k in E: D[k] = E[k]\n\
     2198If E present and lacks .keys() method, does:     for (k, v) in E: D[k] = v\n\
    20882199In either case, this is followed by: for k in F: D[k] = F[k]");
    20892200
     
    21072218"D.iteritems() -> an iterator over the (key, value) items of D");
    21082219
     2220/* Forward */
     2221static PyObject *dictkeys_new(PyObject *);
     2222static PyObject *dictitems_new(PyObject *);
     2223static PyObject *dictvalues_new(PyObject *);
     2224
     2225PyDoc_STRVAR(viewkeys__doc__,
     2226             "D.viewkeys() -> a set-like object providing a view on D's keys");
     2227PyDoc_STRVAR(viewitems__doc__,
     2228             "D.viewitems() -> a set-like object providing a view on D's items");
     2229PyDoc_STRVAR(viewvalues__doc__,
     2230             "D.viewvalues() -> an object providing a view on D's values");
     2231
    21092232static PyMethodDef mapp_methods[] = {
    2110         {"__contains__",(PyCFunction)dict_contains,     METH_O | METH_COEXIST,
    2111          contains__doc__},
    2112         {"__getitem__", (PyCFunction)dict_subscript,    METH_O | METH_COEXIST,
    2113          getitem__doc__},
    2114         {"__sizeof__",  (PyCFunction)dict_sizeof,       METH_NOARGS,
    2115          sizeof__doc__},
    2116         {"has_key",     (PyCFunction)dict_has_key,      METH_O,
    2117          has_key__doc__},
    2118         {"get",         (PyCFunction)dict_get,          METH_VARARGS,
    2119          get__doc__},
    2120         {"setdefault",  (PyCFunction)dict_setdefault,   METH_VARARGS,
    2121          setdefault_doc__},
    2122         {"pop",         (PyCFunction)dict_pop,          METH_VARARGS,
    2123          pop__doc__},
    2124         {"popitem",     (PyCFunction)dict_popitem,      METH_NOARGS,
    2125          popitem__doc__},
    2126         {"keys",        (PyCFunction)dict_keys,         METH_NOARGS,
    2127         keys__doc__},
    2128         {"items",       (PyCFunction)dict_items,        METH_NOARGS,
    2129          items__doc__},
    2130         {"values",      (PyCFunction)dict_values,       METH_NOARGS,
    2131          values__doc__},
    2132         {"update",      (PyCFunction)dict_update,       METH_VARARGS | METH_KEYWORDS,
    2133          update__doc__},
    2134         {"fromkeys",    (PyCFunction)dict_fromkeys,     METH_VARARGS | METH_CLASS,
    2135          fromkeys__doc__},
    2136         {"clear",       (PyCFunction)dict_clear,        METH_NOARGS,
    2137          clear__doc__},
    2138         {"copy",        (PyCFunction)dict_copy,         METH_NOARGS,
    2139          copy__doc__},
    2140         {"iterkeys",    (PyCFunction)dict_iterkeys,     METH_NOARGS,
    2141          iterkeys__doc__},
    2142         {"itervalues",  (PyCFunction)dict_itervalues,   METH_NOARGS,
    2143          itervalues__doc__},
    2144         {"iteritems",   (PyCFunction)dict_iteritems,    METH_NOARGS,
    2145          iteritems__doc__},
    2146         {NULL,          NULL}   /* sentinel */
     2233    {"__contains__",(PyCFunction)dict_contains,         METH_O | METH_COEXIST,
     2234     contains__doc__},
     2235    {"__getitem__", (PyCFunction)dict_subscript,        METH_O | METH_COEXIST,
     2236     getitem__doc__},
     2237    {"__sizeof__",      (PyCFunction)dict_sizeof,       METH_NOARGS,
     2238     sizeof__doc__},
     2239    {"has_key",         (PyCFunction)dict_has_key,      METH_O,
     2240     has_key__doc__},
     2241    {"get",         (PyCFunction)dict_get,          METH_VARARGS,
     2242     get__doc__},
     2243    {"setdefault",  (PyCFunction)dict_setdefault,   METH_VARARGS,
     2244     setdefault_doc__},
     2245    {"pop",         (PyCFunction)dict_pop,          METH_VARARGS,
     2246     pop__doc__},
     2247    {"popitem",         (PyCFunction)dict_popitem,      METH_NOARGS,
     2248     popitem__doc__},
     2249    {"keys",            (PyCFunction)dict_keys,         METH_NOARGS,
     2250    keys__doc__},
     2251    {"items",           (PyCFunction)dict_items,        METH_NOARGS,
     2252     items__doc__},
     2253    {"values",          (PyCFunction)dict_values,       METH_NOARGS,
     2254     values__doc__},
     2255    {"viewkeys",        (PyCFunction)dictkeys_new,      METH_NOARGS,
     2256     viewkeys__doc__},
     2257    {"viewitems",       (PyCFunction)dictitems_new,     METH_NOARGS,
     2258     viewitems__doc__},
     2259    {"viewvalues",      (PyCFunction)dictvalues_new,    METH_NOARGS,
     2260     viewvalues__doc__},
     2261    {"update",          (PyCFunction)dict_update,       METH_VARARGS | METH_KEYWORDS,
     2262     update__doc__},
     2263    {"fromkeys",        (PyCFunction)dict_fromkeys,     METH_VARARGS | METH_CLASS,
     2264     fromkeys__doc__},
     2265    {"clear",           (PyCFunction)dict_clear,        METH_NOARGS,
     2266     clear__doc__},
     2267    {"copy",            (PyCFunction)dict_copy,         METH_NOARGS,
     2268     copy__doc__},
     2269    {"iterkeys",        (PyCFunction)dict_iterkeys,     METH_NOARGS,
     2270     iterkeys__doc__},
     2271    {"itervalues",      (PyCFunction)dict_itervalues,   METH_NOARGS,
     2272     itervalues__doc__},
     2273    {"iteritems",       (PyCFunction)dict_iteritems,    METH_NOARGS,
     2274     iteritems__doc__},
     2275    {NULL,              NULL}   /* sentinel */
    21472276};
    21482277
     
    21512280PyDict_Contains(PyObject *op, PyObject *key)
    21522281{
    2153         long hash;
    2154         PyDictObject *mp = (PyDictObject *)op;
    2155         PyDictEntry *ep;
    2156 
    2157         if (!PyString_CheckExact(key) ||
    2158             (hash = ((PyStringObject *) key)->ob_shash) == -1) {
    2159                 hash = PyObject_Hash(key);
    2160                 if (hash == -1)
    2161                         return -1;
    2162         }
    2163         ep = (mp->ma_lookup)(mp, key, hash);
    2164         return ep == NULL ? -1 : (ep->me_value != NULL);
     2282    long hash;
     2283    PyDictObject *mp = (PyDictObject *)op;
     2284    PyDictEntry *ep;
     2285
     2286    if (!PyString_CheckExact(key) ||
     2287        (hash = ((PyStringObject *) key)->ob_shash) == -1) {
     2288        hash = PyObject_Hash(key);
     2289        if (hash == -1)
     2290            return -1;
     2291    }
     2292    ep = (mp->ma_lookup)(mp, key, hash);
     2293    return ep == NULL ? -1 : (ep->me_value != NULL);
    21652294}
    21662295
     
    21692298_PyDict_Contains(PyObject *op, PyObject *key, long hash)
    21702299{
    2171         PyDictObject *mp = (PyDictObject *)op;
    2172         PyDictEntry *ep;
    2173 
    2174         ep = (mp->ma_lookup)(mp, key, hash);
    2175         return ep == NULL ? -1 : (ep->me_value != NULL);
     2300    PyDictObject *mp = (PyDictObject *)op;
     2301    PyDictEntry *ep;
     2302
     2303    ep = (mp->ma_lookup)(mp, key, hash);
     2304    return ep == NULL ? -1 : (ep->me_value != NULL);
    21762305}
    21772306
    21782307/* Hack to implement "key in dict" */
    21792308static PySequenceMethods dict_as_sequence = {
    2180         0,                      /* sq_length */
    2181         0,                      /* sq_concat */
    2182         0,                      /* sq_repeat */
    2183         0,                      /* sq_item */
    2184         0,                      /* sq_slice */
    2185         0,                      /* sq_ass_item */
    2186         0,                      /* sq_ass_slice */
    2187         PyDict_Contains,        /* sq_contains */
    2188         0,                      /* sq_inplace_concat */
    2189         0,                      /* sq_inplace_repeat */
     2309    0,                          /* sq_length */
     2310    0,                          /* sq_concat */
     2311    0,                          /* sq_repeat */
     2312    0,                          /* sq_item */
     2313    0,                          /* sq_slice */
     2314    0,                          /* sq_ass_item */
     2315    0,                          /* sq_ass_slice */
     2316    PyDict_Contains,            /* sq_contains */
     2317    0,                          /* sq_inplace_concat */
     2318    0,                          /* sq_inplace_repeat */
    21902319};
    21912320
     
    21932322dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
    21942323{
    2195         PyObject *self;
    2196 
    2197         assert(type != NULL && type->tp_alloc != NULL);
    2198         self = type->tp_alloc(type, 0);
    2199         if (self != NULL) {
    2200                 PyDictObject *d = (PyDictObject *)self;
    2201                 /* It's guaranteed that tp->alloc zeroed out the struct. */
    2202                 assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
    2203                 INIT_NONZERO_DICT_SLOTS(d);
    2204                 d->ma_lookup = lookdict_string;
     2324    PyObject *self;
     2325
     2326    assert(type != NULL && type->tp_alloc != NULL);
     2327    self = type->tp_alloc(type, 0);
     2328    if (self != NULL) {
     2329        PyDictObject *d = (PyDictObject *)self;
     2330        /* It's guaranteed that tp->alloc zeroed out the struct. */
     2331        assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0);
     2332        INIT_NONZERO_DICT_SLOTS(d);
     2333        d->ma_lookup = lookdict_string;
     2334        /* The object has been implicitly tracked by tp_alloc */
     2335        if (type == &PyDict_Type)
     2336            _PyObject_GC_UNTRACK(d);
    22052337#ifdef SHOW_CONVERSION_COUNTS
    2206                 ++created;
     2338        ++created;
    22072339#endif
    2208         }
    2209         return self;
     2340#ifdef SHOW_TRACK_COUNT
     2341        if (_PyObject_GC_IS_TRACKED(d))
     2342            count_tracked++;
     2343        else
     2344            count_untracked++;
     2345#endif
     2346    }
     2347    return self;
    22102348}
    22112349
     
    22132351dict_init(PyObject *self, PyObject *args, PyObject *kwds)
    22142352{
    2215         return dict_update_common(self, args, kwds, "dict");
     2353    return dict_update_common(self, args, kwds, "dict");
    22162354}
    22172355
     
    22192357dict_iter(PyDictObject *dict)
    22202358{
    2221         return dictiter_new(dict, &PyDictIterKey_Type);
     2359    return dictiter_new(dict, &PyDictIterKey_Type);
    22222360}
    22232361
     
    22342372
    22352373PyTypeObject PyDict_Type = {
    2236         PyVarObject_HEAD_INIT(&PyType_Type, 0)
    2237         "dict",
    2238         sizeof(PyDictObject),
    2239         0,
    2240         (destructor)dict_dealloc,               /* tp_dealloc */
    2241         (printfunc)dict_print,                  /* tp_print */
    2242         0,                                      /* tp_getattr */
    2243         0,                                      /* tp_setattr */
    2244         (cmpfunc)dict_compare,                  /* tp_compare */
    2245         (reprfunc)dict_repr,                    /* tp_repr */
    2246         0,                                      /* tp_as_number */
    2247         &dict_as_sequence,                      /* tp_as_sequence */
    2248         &dict_as_mapping,                       /* tp_as_mapping */
    2249         (hashfunc)PyObject_HashNotImplemented,  /* tp_hash */
    2250         0,                                      /* tp_call */
    2251         0,                                      /* tp_str */
    2252         PyObject_GenericGetAttr,                /* tp_getattro */
    2253         0,                                      /* tp_setattro */
    2254         0,                                      /* tp_as_buffer */
    2255         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
    2256                 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */
    2257         dictionary_doc,                         /* tp_doc */
    2258         dict_traverse,                          /* tp_traverse */
    2259         dict_tp_clear,                          /* tp_clear */
    2260         dict_richcompare,                       /* tp_richcompare */
    2261         0,                                      /* tp_weaklistoffset */
    2262         (getiterfunc)dict_iter,                 /* tp_iter */
    2263         0,                                      /* tp_iternext */
    2264         mapp_methods,                           /* tp_methods */
    2265         0,                                      /* tp_members */
    2266         0,                                      /* tp_getset */
    2267         0,                                      /* tp_base */
    2268         0,                                      /* tp_dict */
    2269         0,                                      /* tp_descr_get */
    2270         0,                                      /* tp_descr_set */
    2271         0,                                      /* tp_dictoffset */
    2272         dict_init,                              /* tp_init */
    2273         PyType_GenericAlloc,                    /* tp_alloc */
    2274         dict_new,                               /* tp_new */
    2275         PyObject_GC_Del,                        /* tp_free */
     2374    PyVarObject_HEAD_INIT(&PyType_Type, 0)
     2375    "dict",
     2376    sizeof(PyDictObject),
     2377    0,
     2378    (destructor)dict_dealloc,                   /* tp_dealloc */
     2379    (printfunc)dict_print,                      /* tp_print */
     2380    0,                                          /* tp_getattr */
     2381    0,                                          /* tp_setattr */
     2382    (cmpfunc)dict_compare,                      /* tp_compare */
     2383    (reprfunc)dict_repr,                        /* tp_repr */
     2384    0,                                          /* tp_as_number */
     2385    &dict_as_sequence,                          /* tp_as_sequence */
     2386    &dict_as_mapping,                           /* tp_as_mapping */
     2387    (hashfunc)PyObject_HashNotImplemented,      /* tp_hash */
     2388    0,                                          /* tp_call */
     2389    0,                                          /* tp_str */
     2390    PyObject_GenericGetAttr,                    /* tp_getattro */
     2391    0,                                          /* tp_setattro */
     2392    0,                                          /* tp_as_buffer */
     2393    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
     2394        Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS,         /* tp_flags */
     2395    dictionary_doc,                             /* tp_doc */
     2396    dict_traverse,                              /* tp_traverse */
     2397    dict_tp_clear,                              /* tp_clear */
     2398    dict_richcompare,                           /* tp_richcompare */
     2399    0,                                          /* tp_weaklistoffset */
     2400    (getiterfunc)dict_iter,                     /* tp_iter */
     2401    0,                                          /* tp_iternext */
     2402    mapp_methods,                               /* tp_methods */
     2403    0,                                          /* tp_members */
     2404    0,                                          /* tp_getset */
     2405    0,                                          /* tp_base */
     2406    0,                                          /* tp_dict */
     2407    0,                                          /* tp_descr_get */
     2408    0,                                          /* tp_descr_set */
     2409    0,                                          /* tp_dictoffset */
     2410    dict_init,                                  /* tp_init */
     2411    PyType_GenericAlloc,                        /* tp_alloc */
     2412    dict_new,                                   /* tp_new */
     2413    PyObject_GC_Del,                            /* tp_free */
    22762414};
    22772415
     
    22812419PyDict_GetItemString(PyObject *v, const char *key)
    22822420{
    2283         PyObject *kv, *rv;
    2284         kv = PyString_FromString(key);
    2285         if (kv == NULL)
    2286                 return NULL;
    2287         rv = PyDict_GetItem(v, kv);
    2288         Py_DECREF(kv);
    2289         return rv;
     2421    PyObject *kv, *rv;
     2422    kv = PyString_FromString(key);
     2423    if (kv == NULL)
     2424        return NULL;
     2425    rv = PyDict_GetItem(v, kv);
     2426    Py_DECREF(kv);
     2427    return rv;
    22902428}
    22912429
     
    22932431PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
    22942432{
    2295         PyObject *kv;
    2296         int err;
    2297         kv = PyString_FromString(key);
    2298         if (kv == NULL)
    2299                 return -1;
    2300         PyString_InternInPlace(&kv); /* XXX Should we really? */
    2301         err = PyDict_SetItem(v, kv, item);
    2302         Py_DECREF(kv);
    2303         return err;
     2433    PyObject *kv;
     2434    int err;
     2435    kv = PyString_FromString(key);
     2436    if (kv == NULL)
     2437        return -1;
     2438    PyString_InternInPlace(&kv); /* XXX Should we really? */
     2439    err = PyDict_SetItem(v, kv, item);
     2440    Py_DECREF(kv);
     2441    return err;
    23042442}
    23052443
     
    23072445PyDict_DelItemString(PyObject *v, const char *key)
    23082446{
    2309         PyObject *kv;
    2310         int err;
    2311         kv = PyString_FromString(key);
    2312         if (kv == NULL)
    2313                 return -1;
    2314         err = PyDict_DelItem(v, kv);
    2315         Py_DECREF(kv);
    2316         return err;
     2447    PyObject *kv;
     2448    int err;
     2449    kv = PyString_FromString(key);
     2450    if (kv == NULL)
     2451        return -1;
     2452    err = PyDict_DelItem(v, kv);
     2453    Py_DECREF(kv);
     2454    return err;
    23172455}
    23182456
     
    23202458
    23212459typedef struct {
    2322         PyObject_HEAD
    2323         PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
    2324         Py_ssize_t di_used;
    2325         Py_ssize_t di_pos;
    2326         PyObject* di_result; /* reusable result tuple for iteritems */
    2327         Py_ssize_t len;
     2460    PyObject_HEAD
     2461    PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */
     2462    Py_ssize_t di_used;
     2463    Py_ssize_t di_pos;
     2464    PyObject* di_result; /* reusable result tuple for iteritems */
     2465    Py_ssize_t len;
    23282466} dictiterobject;
    23292467
     
    23312469dictiter_new(PyDictObject *dict, PyTypeObject *itertype)
    23322470{
    2333         dictiterobject *di;
    2334         di = PyObject_GC_New(dictiterobject, itertype);
    2335         if (di == NULL)
    2336                 return NULL;
    2337         Py_INCREF(dict);
    2338         di->di_dict = dict;
    2339         di->di_used = dict->ma_used;
    2340         di->di_pos = 0;
    2341         di->len = dict->ma_used;
    2342         if (itertype == &PyDictIterItem_Type) {
    2343                 di->di_result = PyTuple_Pack(2, Py_None, Py_None);
    2344                 if (di->di_result == NULL) {
    2345                         Py_DECREF(di);
    2346                         return NULL;
    2347                 }
    2348         }
    2349         else
    2350                 di->di_result = NULL;
    2351         _PyObject_GC_TRACK(di);
    2352         return (PyObject *)di;
     2471    dictiterobject *di;
     2472    di = PyObject_GC_New(dictiterobject, itertype);
     2473    if (di == NULL)
     2474        return NULL;
     2475    Py_INCREF(dict);
     2476    di->di_dict = dict;
     2477    di->di_used = dict->ma_used;
     2478    di->di_pos = 0;
     2479    di->len = dict->ma_used;
     2480    if (itertype == &PyDictIterItem_Type) {
     2481        di->di_result = PyTuple_Pack(2, Py_None, Py_None);
     2482        if (di->di_result == NULL) {
     2483            Py_DECREF(di);
     2484            return NULL;
     2485        }
     2486    }
     2487    else
     2488        di->di_result = NULL;
     2489    _PyObject_GC_TRACK(di);
     2490    return (PyObject *)di;
    23532491}
    23542492
     
    23562494dictiter_dealloc(dictiterobject *di)
    23572495{
    2358         Py_XDECREF(di->di_dict);
    2359         Py_XDECREF(di->di_result);
    2360         PyObject_GC_Del(di);
     2496    Py_XDECREF(di->di_dict);
     2497    Py_XDECREF(di->di_result);
     2498    PyObject_GC_Del(di);
    23612499}
    23622500
     
    23642502dictiter_traverse(dictiterobject *di, visitproc visit, void *arg)
    23652503{
    2366         Py_VISIT(di->di_dict);
    2367         Py_VISIT(di->di_result);
    2368         return 0;
     2504    Py_VISIT(di->di_dict);
     2505    Py_VISIT(di->di_result);
     2506    return 0;
    23692507}
    23702508
     
    23722510dictiter_len(dictiterobject *di)
    23732511{
    2374         Py_ssize_t len = 0;
    2375         if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
    2376                 len = di->len;
    2377         return PyInt_FromSize_t(len);
     2512    Py_ssize_t len = 0;
     2513    if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used)
     2514        len = di->len;
     2515    return PyInt_FromSize_t(len);
    23782516}
    23792517
     
    23812519
    23822520static PyMethodDef dictiter_methods[] = {
    2383         {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS, length_hint_doc},
    2384         {NULL,          NULL}           /* sentinel */
     2521    {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS, length_hint_doc},
     2522    {NULL,              NULL}           /* sentinel */
    23852523};
    23862524
    23872525static PyObject *dictiter_iternextkey(dictiterobject *di)
    23882526{
    2389         PyObject *key;
    2390         register Py_ssize_t i, mask;
    2391         register PyDictEntry *ep;
    2392         PyDictObject *d = di->di_dict;
    2393 
    2394         if (d == NULL)
    2395                 return NULL;
    2396         assert (PyDict_Check(d));
    2397 
    2398         if (di->di_used != d->ma_used) {
    2399                 PyErr_SetString(PyExc_RuntimeError,
    2400                                 "dictionary changed size during iteration");
    2401                 di->di_used = -1; /* Make this state sticky */
    2402                 return NULL;
    2403         }
    2404 
    2405         i = di->di_pos;
    2406         if (i < 0)
    2407                 goto fail;
    2408         ep = d->ma_table;
    2409         mask = d->ma_mask;
    2410         while (i <= mask && ep[i].me_value == NULL)
    2411                 i++;
    2412         di->di_pos = i+1;
    2413         if (i > mask)
    2414                 goto fail;
    2415         di->len--;
    2416         key = ep[i].me_key;
    2417         Py_INCREF(key);
    2418         return key;
     2527    PyObject *key;
     2528    register Py_ssize_t i, mask;
     2529    register PyDictEntry *ep;
     2530    PyDictObject *d = di->di_dict;
     2531
     2532    if (d == NULL)
     2533        return NULL;
     2534    assert (PyDict_Check(d));
     2535
     2536    if (di->di_used != d->ma_used) {
     2537        PyErr_SetString(PyExc_RuntimeError,
     2538                        "dictionary changed size during iteration");
     2539        di->di_used = -1; /* Make this state sticky */
     2540        return NULL;
     2541    }
     2542
     2543    i = di->di_pos;
     2544    if (i < 0)
     2545        goto fail;
     2546    ep = d->ma_table;
     2547    mask = d->ma_mask;
     2548    while (i <= mask && ep[i].me_value == NULL)
     2549        i++;
     2550    di->di_pos = i+1;
     2551    if (i > mask)
     2552        goto fail;
     2553    di->len--;
     2554    key = ep[i].me_key;
     2555    Py_INCREF(key);
     2556    return key;
    24192557
    24202558fail:
    2421         Py_DECREF(d);
    2422         di->di_dict = NULL;
    2423         return NULL;
     2559    Py_DECREF(d);
     2560    di->di_dict = NULL;
     2561    return NULL;
    24242562}
    24252563
    24262564PyTypeObject PyDictIterKey_Type = {
    2427         PyVarObject_HEAD_INIT(&PyType_Type, 0)
    2428         "dictionary-keyiterator",               /* tp_name */
    2429         sizeof(dictiterobject),                 /* tp_basicsize */
    2430         0,                                      /* tp_itemsize */
    2431         /* methods */
    2432         (destructor)dictiter_dealloc,           /* tp_dealloc */
    2433         0,                                      /* tp_print */
    2434         0,                                      /* tp_getattr */
    2435         0,                                      /* tp_setattr */
    2436         0,                                      /* tp_compare */
    2437         0,                                      /* tp_repr */
    2438         0,                                      /* tp_as_number */
    2439         0,                                      /* tp_as_sequence */
    2440         0,                                      /* tp_as_mapping */
    2441         0,                                      /* tp_hash */
    2442         0,                                      /* tp_call */
    2443         0,                                      /* tp_str */
    2444         PyObject_GenericGetAttr,                /* tp_getattro */
    2445         0,                                      /* tp_setattro */
    2446         0,                                      /* tp_as_buffer */
    2447         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
    2448         0,                                      /* tp_doc */
    2449         (traverseproc)dictiter_traverse,        /* tp_traverse */
    2450         0,                                      /* tp_clear */
    2451         0,                                      /* tp_richcompare */
    2452         0,                                      /* tp_weaklistoffset */
    2453         PyObject_SelfIter,                      /* tp_iter */
    2454         (iternextfunc)dictiter_iternextkey,     /* tp_iternext */
    2455         dictiter_methods,                       /* tp_methods */
    2456         0,
     2565    PyVarObject_HEAD_INIT(&PyType_Type, 0)
     2566    "dictionary-keyiterator",                   /* tp_name */
     2567    sizeof(dictiterobject),                     /* tp_basicsize */
     2568    0,                                          /* tp_itemsize */
     2569    /* methods */
     2570    (destructor)dictiter_dealloc,               /* tp_dealloc */
     2571    0,                                          /* tp_print */
     2572    0,                                          /* tp_getattr */
     2573    0,                                          /* tp_setattr */
     2574    0,                                          /* tp_compare */
     2575    0,                                          /* tp_repr */
     2576    0,                                          /* tp_as_number */
     2577    0,                                          /* tp_as_sequence */
     2578    0,                                          /* tp_as_mapping */
     2579    0,                                          /* tp_hash */
     2580    0,                                          /* tp_call */
     2581    0,                                          /* tp_str */
     2582    PyObject_GenericGetAttr,                    /* tp_getattro */
     2583    0,                                          /* tp_setattro */
     2584    0,                                          /* tp_as_buffer */
     2585    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
     2586    0,                                          /* tp_doc */
     2587    (traverseproc)dictiter_traverse,            /* tp_traverse */
     2588    0,                                          /* tp_clear */
     2589    0,                                          /* tp_richcompare */
     2590    0,                                          /* tp_weaklistoffset */
     2591    PyObject_SelfIter,                          /* tp_iter */
     2592    (iternextfunc)dictiter_iternextkey,         /* tp_iternext */
     2593    dictiter_methods,                           /* tp_methods */
     2594    0,
    24572595};
    24582596
    24592597static PyObject *dictiter_iternextvalue(dictiterobject *di)
    24602598{
    2461         PyObject *value;
    2462         register Py_ssize_t i, mask;
    2463         register PyDictEntry *ep;
    2464         PyDictObject *d = di->di_dict;
    2465 
    2466         if (d == NULL)
    2467                 return NULL;
    2468         assert (PyDict_Check(d));
    2469 
    2470         if (di->di_used != d->ma_used) {
    2471                 PyErr_SetString(PyExc_RuntimeError,
    2472                                 "dictionary changed size during iteration");
    2473                 di->di_used = -1; /* Make this state sticky */
    2474                 return NULL;
    2475         }
    2476 
    2477         i = di->di_pos;
    2478         mask = d->ma_mask;
    2479         if (i < 0 || i > mask)
    2480                 goto fail;
    2481         ep = d->ma_table;
    2482         while ((value=ep[i].me_value) == NULL) {
    2483                 i++;
    2484                 if (i > mask)
    2485                         goto fail;
    2486         }
    2487         di->di_pos = i+1;
    2488         di->len--;
    2489         Py_INCREF(value);
    2490         return value;
     2599    PyObject *value;
     2600    register Py_ssize_t i, mask;
     2601    register PyDictEntry *ep;
     2602    PyDictObject *d = di->di_dict;
     2603
     2604    if (d == NULL)
     2605        return NULL;
     2606    assert (PyDict_Check(d));
     2607
     2608    if (di->di_used != d->ma_used) {
     2609        PyErr_SetString(PyExc_RuntimeError,
     2610                        "dictionary changed size during iteration");
     2611        di->di_used = -1; /* Make this state sticky */
     2612        return NULL;
     2613    }
     2614
     2615    i = di->di_pos;
     2616    mask = d->ma_mask;
     2617    if (i < 0 || i > mask)
     2618        goto fail;
     2619    ep = d->ma_table;
     2620    while ((value=ep[i].me_value) == NULL) {
     2621        i++;
     2622        if (i > mask)
     2623            goto fail;
     2624    }
     2625    di->di_pos = i+1;
     2626    di->len--;
     2627    Py_INCREF(value);
     2628    return value;
    24912629
    24922630fail:
    2493         Py_DECREF(d);
    2494         di->di_dict = NULL;
    2495         return NULL;
     2631    Py_DECREF(d);
     2632    di->di_dict = NULL;
     2633    return NULL;
    24962634}
    24972635
    24982636PyTypeObject PyDictIterValue_Type = {
    2499         PyVarObject_HEAD_INIT(&PyType_Type, 0)
    2500         "dictionary-valueiterator",             /* tp_name */
    2501         sizeof(dictiterobject),                 /* tp_basicsize */
    2502         0,                                      /* tp_itemsize */
    2503         /* methods */
    2504         (destructor)dictiter_dealloc,           /* tp_dealloc */
    2505         0,                                      /* tp_print */
    2506         0,                                      /* tp_getattr */
    2507         0,                                      /* tp_setattr */
    2508         0,                                      /* tp_compare */
    2509         0,                                      /* tp_repr */
    2510         0,                                      /* tp_as_number */
    2511         0,                                      /* tp_as_sequence */
    2512         0,                                      /* tp_as_mapping */
    2513         0,                                      /* tp_hash */
    2514         0,                                      /* tp_call */
    2515         0,                                      /* tp_str */
    2516         PyObject_GenericGetAttr,                /* tp_getattro */
    2517         0,                                      /* tp_setattro */
    2518         0,                                      /* tp_as_buffer */
    2519         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
    2520         0,                                      /* tp_doc */
    2521         (traverseproc)dictiter_traverse,        /* tp_traverse */
    2522         0,                                      /* tp_clear */
    2523         0,                                      /* tp_richcompare */
    2524         0,                                      /* tp_weaklistoffset */
    2525         PyObject_SelfIter,                      /* tp_iter */
    2526         (iternextfunc)dictiter_iternextvalue,   /* tp_iternext */
    2527         dictiter_methods,                       /* tp_methods */
    2528         0,
     2637    PyVarObject_HEAD_INIT(&PyType_Type, 0)
     2638    "dictionary-valueiterator",                 /* tp_name */
     2639    sizeof(dictiterobject),                     /* tp_basicsize */
     2640    0,                                          /* tp_itemsize */
     2641    /* methods */
     2642    (destructor)dictiter_dealloc,               /* tp_dealloc */
     2643    0,                                          /* tp_print */
     2644    0,                                          /* tp_getattr */
     2645    0,                                          /* tp_setattr */
     2646    0,                                          /* tp_compare */
     2647    0,                                          /* tp_repr */
     2648    0,                                          /* tp_as_number */
     2649    0,                                          /* tp_as_sequence */
     2650    0,                                          /* tp_as_mapping */
     2651    0,                                          /* tp_hash */
     2652    0,                                          /* tp_call */
     2653    0,                                          /* tp_str */
     2654    PyObject_GenericGetAttr,                    /* tp_getattro */
     2655    0,                                          /* tp_setattro */
     2656    0,                                          /* tp_as_buffer */
     2657    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
     2658    0,                                          /* tp_doc */
     2659    (traverseproc)dictiter_traverse,            /* tp_traverse */
     2660    0,                                          /* tp_clear */
     2661    0,                                          /* tp_richcompare */
     2662    0,                                          /* tp_weaklistoffset */
     2663    PyObject_SelfIter,                          /* tp_iter */
     2664    (iternextfunc)dictiter_iternextvalue,       /* tp_iternext */
     2665    dictiter_methods,                           /* tp_methods */
     2666    0,
    25292667};
    25302668
    25312669static PyObject *dictiter_iternextitem(dictiterobject *di)
    25322670{
    2533         PyObject *key, *value, *result = di->di_result;
    2534         register Py_ssize_t i, mask;
    2535         register PyDictEntry *ep;
    2536         PyDictObject *d = di->di_dict;
    2537 
    2538         if (d == NULL)
    2539                 return NULL;
    2540         assert (PyDict_Check(d));
    2541 
    2542         if (di->di_used != d->ma_used) {
    2543                 PyErr_SetString(PyExc_RuntimeError,
    2544                                 "dictionary changed size during iteration");
    2545                 di->di_used = -1; /* Make this state sticky */
    2546                 return NULL;
    2547         }
    2548 
    2549         i = di->di_pos;
    2550         if (i < 0)
    2551                 goto fail;
    2552         ep = d->ma_table;
    2553         mask = d->ma_mask;
    2554         while (i <= mask && ep[i].me_value == NULL)
    2555                 i++;
    2556         di->di_pos = i+1;
    2557         if (i > mask)
    2558                 goto fail;
    2559 
    2560         if (result->ob_refcnt == 1) {
    2561                 Py_INCREF(result);
    2562                 Py_DECREF(PyTuple_GET_ITEM(result, 0));
    2563                 Py_DECREF(PyTuple_GET_ITEM(result, 1));
    2564         } else {
    2565                 result = PyTuple_New(2);
    2566                 if (result == NULL)
    2567                         return NULL;
    2568         }
    2569         di->len--;
    2570         key = ep[i].me_key;
    2571         value = ep[i].me_value;
    2572         Py_INCREF(key);
    2573         Py_INCREF(value);
    2574         PyTuple_SET_ITEM(result, 0, key);
    2575         PyTuple_SET_ITEM(result, 1, value);
    2576         return result;
     2671    PyObject *key, *value, *result = di->di_result;
     2672    register Py_ssize_t i, mask;
     2673    register PyDictEntry *ep;
     2674    PyDictObject *d = di->di_dict;
     2675
     2676    if (d == NULL)
     2677        return NULL;
     2678    assert (PyDict_Check(d));
     2679
     2680    if (di->di_used != d->ma_used) {
     2681        PyErr_SetString(PyExc_RuntimeError,
     2682                        "dictionary changed size during iteration");
     2683        di->di_used = -1; /* Make this state sticky */
     2684        return NULL;
     2685    }
     2686
     2687    i = di->di_pos;
     2688    if (i < 0)
     2689        goto fail;
     2690    ep = d->ma_table;
     2691    mask = d->ma_mask;
     2692    while (i <= mask && ep[i].me_value == NULL)
     2693        i++;
     2694    di->di_pos = i+1;
     2695    if (i > mask)
     2696        goto fail;
     2697
     2698    if (result->ob_refcnt == 1) {
     2699        Py_INCREF(result);
     2700        Py_DECREF(PyTuple_GET_ITEM(result, 0));
     2701        Py_DECREF(PyTuple_GET_ITEM(result, 1));
     2702    } else {
     2703        result = PyTuple_New(2);
     2704        if (result == NULL)
     2705            return NULL;
     2706    }
     2707    di->len--;
     2708    key = ep[i].me_key;
     2709    value = ep[i].me_value;
     2710    Py_INCREF(key);
     2711    Py_INCREF(value);
     2712    PyTuple_SET_ITEM(result, 0, key);
     2713    PyTuple_SET_ITEM(result, 1, value);
     2714    return result;
    25772715
    25782716fail:
    2579         Py_DECREF(d);
    2580         di->di_dict = NULL;
    2581         return NULL;
     2717    Py_DECREF(d);
     2718    di->di_dict = NULL;
     2719    return NULL;
    25822720}
    25832721
    25842722PyTypeObject PyDictIterItem_Type = {
    2585         PyVarObject_HEAD_INIT(&PyType_Type, 0)
    2586         "dictionary-itemiterator",              /* tp_name */
    2587         sizeof(dictiterobject),                 /* tp_basicsize */
    2588         0,                                      /* tp_itemsize */
    2589         /* methods */
    2590         (destructor)dictiter_dealloc,           /* tp_dealloc */
    2591         0,                                      /* tp_print */
    2592         0,                                      /* tp_getattr */
    2593         0,                                      /* tp_setattr */
    2594         0,                                      /* tp_compare */
    2595         0,                                      /* tp_repr */
    2596         0,                                      /* tp_as_number */
    2597         0,                                      /* tp_as_sequence */
    2598         0,                                      /* tp_as_mapping */
    2599         0,                                      /* tp_hash */
    2600         0,                                      /* tp_call */
    2601         0,                                      /* tp_str */
    2602         PyObject_GenericGetAttr,                /* tp_getattro */
    2603         0,                                      /* tp_setattro */
    2604         0,                                      /* tp_as_buffer */
    2605         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
    2606         0,                                      /* tp_doc */
    2607         (traverseproc)dictiter_traverse,        /* tp_traverse */
    2608         0,                                      /* tp_clear */
    2609         0,                                      /* tp_richcompare */
    2610         0,                                      /* tp_weaklistoffset */
    2611         PyObject_SelfIter,                      /* tp_iter */
    2612         (iternextfunc)dictiter_iternextitem,    /* tp_iternext */
    2613         dictiter_methods,                       /* tp_methods */
    2614         0,
     2723    PyVarObject_HEAD_INIT(&PyType_Type, 0)
     2724    "dictionary-itemiterator",                  /* tp_name */
     2725    sizeof(dictiterobject),                     /* tp_basicsize */
     2726    0,                                          /* tp_itemsize */
     2727    /* methods */
     2728    (destructor)dictiter_dealloc,               /* tp_dealloc */
     2729    0,                                          /* tp_print */
     2730    0,                                          /* tp_getattr */
     2731    0,                                          /* tp_setattr */
     2732    0,                                          /* tp_compare */
     2733    0,                                          /* tp_repr */
     2734    0,                                          /* tp_as_number */
     2735    0,                                          /* tp_as_sequence */
     2736    0,                                          /* tp_as_mapping */
     2737    0,                                          /* tp_hash */
     2738    0,                                          /* tp_call */
     2739    0,                                          /* tp_str */
     2740    PyObject_GenericGetAttr,                    /* tp_getattro */
     2741    0,                                          /* tp_setattro */
     2742    0,                                          /* tp_as_buffer */
     2743    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
     2744    0,                                          /* tp_doc */
     2745    (traverseproc)dictiter_traverse,            /* tp_traverse */
     2746    0,                                          /* tp_clear */
     2747    0,                                          /* tp_richcompare */
     2748    0,                                          /* tp_weaklistoffset */
     2749    PyObject_SelfIter,                          /* tp_iter */
     2750    (iternextfunc)dictiter_iternextitem,        /* tp_iternext */
     2751    dictiter_methods,                           /* tp_methods */
     2752    0,
    26152753};
     2754
     2755/***********************************************/
     2756/* View objects for keys(), items(), values(). */
     2757/***********************************************/
     2758
     2759/* The instance lay-out is the same for all three; but the type differs. */
     2760
     2761typedef struct {
     2762    PyObject_HEAD
     2763    PyDictObject *dv_dict;
     2764} dictviewobject;
     2765
     2766
     2767static void
     2768dictview_dealloc(dictviewobject *dv)
     2769{
     2770    Py_XDECREF(dv->dv_dict);
     2771    PyObject_GC_Del(dv);
     2772}
     2773
     2774static int
     2775dictview_traverse(dictviewobject *dv, visitproc visit, void *arg)
     2776{
     2777    Py_VISIT(dv->dv_dict);
     2778    return 0;
     2779}
     2780
     2781static Py_ssize_t
     2782dictview_len(dictviewobject *dv)
     2783{
     2784    Py_ssize_t len = 0;
     2785    if (dv->dv_dict != NULL)
     2786        len = dv->dv_dict->ma_used;
     2787    return len;
     2788}
     2789
     2790static PyObject *
     2791dictview_new(PyObject *dict, PyTypeObject *type)
     2792{
     2793    dictviewobject *dv;
     2794    if (dict == NULL) {
     2795        PyErr_BadInternalCall();
     2796        return NULL;
     2797    }
     2798    if (!PyDict_Check(dict)) {
     2799        /* XXX Get rid of this restriction later */
     2800        PyErr_Format(PyExc_TypeError,
     2801                     "%s() requires a dict argument, not '%s'",
     2802                     type->tp_name, dict->ob_type->tp_name);
     2803        return NULL;
     2804    }
     2805    dv = PyObject_GC_New(dictviewobject, type);
     2806    if (dv == NULL)
     2807        return NULL;
     2808    Py_INCREF(dict);
     2809    dv->dv_dict = (PyDictObject *)dict;
     2810    _PyObject_GC_TRACK(dv);
     2811    return (PyObject *)dv;
     2812}
     2813
     2814/* TODO(guido): The views objects are not complete:
     2815
     2816 * support more set operations
     2817 * support arbitrary mappings?
     2818   - either these should be static or exported in dictobject.h
     2819   - if public then they should probably be in builtins
     2820*/
     2821
     2822/* Return 1 if self is a subset of other, iterating over self;
     2823   0 if not; -1 if an error occurred. */
     2824static int
     2825all_contained_in(PyObject *self, PyObject *other)
     2826{
     2827    PyObject *iter = PyObject_GetIter(self);
     2828    int ok = 1;
     2829
     2830    if (iter == NULL)
     2831        return -1;
     2832    for (;;) {
     2833        PyObject *next = PyIter_Next(iter);
     2834        if (next == NULL) {
     2835            if (PyErr_Occurred())
     2836                ok = -1;
     2837            break;
     2838        }
     2839        ok = PySequence_Contains(other, next);
     2840        Py_DECREF(next);
     2841        if (ok <= 0)
     2842            break;
     2843    }
     2844    Py_DECREF(iter);
     2845    return ok;
     2846}
     2847
     2848static PyObject *
     2849dictview_richcompare(PyObject *self, PyObject *other, int op)
     2850{
     2851    Py_ssize_t len_self, len_other;
     2852    int ok;
     2853    PyObject *result;
     2854
     2855    assert(self != NULL);
     2856    assert(PyDictViewSet_Check(self));
     2857    assert(other != NULL);
     2858
     2859    if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other)) {
     2860        Py_INCREF(Py_NotImplemented);
     2861        return Py_NotImplemented;
     2862    }
     2863
     2864    len_self = PyObject_Size(self);
     2865    if (len_self < 0)
     2866        return NULL;
     2867    len_other = PyObject_Size(other);
     2868    if (len_other < 0)
     2869        return NULL;
     2870
     2871    ok = 0;
     2872    switch(op) {
     2873
     2874    case Py_NE:
     2875    case Py_EQ:
     2876        if (len_self == len_other)
     2877            ok = all_contained_in(self, other);
     2878        if (op == Py_NE && ok >= 0)
     2879            ok = !ok;
     2880        break;
     2881
     2882    case Py_LT:
     2883        if (len_self < len_other)
     2884            ok = all_contained_in(self, other);
     2885        break;
     2886
     2887      case Py_LE:
     2888          if (len_self <= len_other)
     2889              ok = all_contained_in(self, other);
     2890          break;
     2891
     2892    case Py_GT:
     2893        if (len_self > len_other)
     2894            ok = all_contained_in(other, self);
     2895        break;
     2896
     2897    case Py_GE:
     2898        if (len_self >= len_other)
     2899            ok = all_contained_in(other, self);
     2900        break;
     2901
     2902    }
     2903    if (ok < 0)
     2904        return NULL;
     2905    result = ok ? Py_True : Py_False;
     2906    Py_INCREF(result);
     2907    return result;
     2908}
     2909
     2910static PyObject *
     2911dictview_repr(dictviewobject *dv)
     2912{
     2913    PyObject *seq;
     2914    PyObject *seq_str;
     2915    PyObject *result;
     2916
     2917    seq = PySequence_List((PyObject *)dv);
     2918    if (seq == NULL)
     2919        return NULL;
     2920
     2921    seq_str = PyObject_Repr(seq);
     2922    if (seq_str == NULL) {
     2923        Py_DECREF(seq);
     2924        return NULL;
     2925    }
     2926    result = PyString_FromFormat("%s(%s)", Py_TYPE(dv)->tp_name,
     2927                                 PyString_AS_STRING(seq_str));
     2928    Py_DECREF(seq_str);
     2929    Py_DECREF(seq);
     2930    return result;
     2931}
     2932
     2933/*** dict_keys ***/
     2934
     2935static PyObject *
     2936dictkeys_iter(dictviewobject *dv)
     2937{
     2938    if (dv->dv_dict == NULL) {
     2939        Py_RETURN_NONE;
     2940    }
     2941    return dictiter_new(dv->dv_dict, &PyDictIterKey_Type);
     2942}
     2943
     2944static int
     2945dictkeys_contains(dictviewobject *dv, PyObject *obj)
     2946{
     2947    if (dv->dv_dict == NULL)
     2948        return 0;
     2949    return PyDict_Contains((PyObject *)dv->dv_dict, obj);
     2950}
     2951
     2952static PySequenceMethods dictkeys_as_sequence = {
     2953    (lenfunc)dictview_len,              /* sq_length */
     2954    0,                                  /* sq_concat */
     2955    0,                                  /* sq_repeat */
     2956    0,                                  /* sq_item */
     2957    0,                                  /* sq_slice */
     2958    0,                                  /* sq_ass_item */
     2959    0,                                  /* sq_ass_slice */
     2960    (objobjproc)dictkeys_contains,      /* sq_contains */
     2961};
     2962
     2963static PyObject*
     2964dictviews_sub(PyObject* self, PyObject *other)
     2965{
     2966    PyObject *result = PySet_New(self);
     2967    PyObject *tmp;
     2968    if (result == NULL)
     2969        return NULL;
     2970
     2971    tmp = PyObject_CallMethod(result, "difference_update", "O", other);
     2972    if (tmp == NULL) {
     2973        Py_DECREF(result);
     2974        return NULL;
     2975    }
     2976
     2977    Py_DECREF(tmp);
     2978    return result;
     2979}
     2980
     2981static PyObject*
     2982dictviews_and(PyObject* self, PyObject *other)
     2983{
     2984    PyObject *result = PySet_New(self);
     2985    PyObject *tmp;
     2986    if (result == NULL)
     2987        return NULL;
     2988
     2989    tmp = PyObject_CallMethod(result, "intersection_update", "O", other);
     2990    if (tmp == NULL) {
     2991        Py_DECREF(result);
     2992        return NULL;
     2993    }
     2994
     2995    Py_DECREF(tmp);
     2996    return result;
     2997}
     2998
     2999static PyObject*
     3000dictviews_or(PyObject* self, PyObject *other)
     3001{
     3002    PyObject *result = PySet_New(self);
     3003    PyObject *tmp;
     3004    if (result == NULL)
     3005        return NULL;
     3006
     3007    tmp = PyObject_CallMethod(result, "update", "O", other);
     3008    if (tmp == NULL) {
     3009        Py_DECREF(result);
     3010        return NULL;
     3011    }
     3012
     3013    Py_DECREF(tmp);
     3014    return result;
     3015}
     3016
     3017static PyObject*
     3018dictviews_xor(PyObject* self, PyObject *other)
     3019{
     3020    PyObject *result = PySet_New(self);
     3021    PyObject *tmp;
     3022    if (result == NULL)
     3023        return NULL;
     3024
     3025    tmp = PyObject_CallMethod(result, "symmetric_difference_update", "O",
     3026                              other);
     3027    if (tmp == NULL) {
     3028        Py_DECREF(result);
     3029        return NULL;
     3030    }
     3031
     3032    Py_DECREF(tmp);
     3033    return result;
     3034}
     3035
     3036static PyNumberMethods dictviews_as_number = {
     3037    0,                                  /*nb_add*/
     3038    (binaryfunc)dictviews_sub,          /*nb_subtract*/
     3039    0,                                  /*nb_multiply*/
     3040    0,                                  /*nb_divide*/
     3041    0,                                  /*nb_remainder*/
     3042    0,                                  /*nb_divmod*/
     3043    0,                                  /*nb_power*/
     3044    0,                                  /*nb_negative*/
     3045    0,                                  /*nb_positive*/
     3046    0,                                  /*nb_absolute*/
     3047    0,                                  /*nb_nonzero*/
     3048    0,                                  /*nb_invert*/
     3049    0,                                  /*nb_lshift*/
     3050    0,                                  /*nb_rshift*/
     3051    (binaryfunc)dictviews_and,          /*nb_and*/
     3052    (binaryfunc)dictviews_xor,          /*nb_xor*/
     3053    (binaryfunc)dictviews_or,           /*nb_or*/
     3054};
     3055
     3056static PyMethodDef dictkeys_methods[] = {
     3057    {NULL,              NULL}           /* sentinel */
     3058};
     3059
     3060PyTypeObject PyDictKeys_Type = {
     3061    PyVarObject_HEAD_INIT(&PyType_Type, 0)
     3062    "dict_keys",                                /* tp_name */
     3063    sizeof(dictviewobject),                     /* tp_basicsize */
     3064    0,                                          /* tp_itemsize */
     3065    /* methods */
     3066    (destructor)dictview_dealloc,               /* tp_dealloc */
     3067    0,                                          /* tp_print */
     3068    0,                                          /* tp_getattr */
     3069    0,                                          /* tp_setattr */
     3070    0,                                          /* tp_reserved */
     3071    (reprfunc)dictview_repr,                    /* tp_repr */
     3072    &dictviews_as_number,                       /* tp_as_number */
     3073    &dictkeys_as_sequence,                      /* tp_as_sequence */
     3074    0,                                          /* tp_as_mapping */
     3075    0,                                          /* tp_hash */
     3076    0,                                          /* tp_call */
     3077    0,                                          /* tp_str */
     3078    PyObject_GenericGetAttr,                    /* tp_getattro */
     3079    0,                                          /* tp_setattro */
     3080    0,                                          /* tp_as_buffer */
     3081    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
     3082        Py_TPFLAGS_CHECKTYPES,                  /* tp_flags */
     3083    0,                                          /* tp_doc */
     3084    (traverseproc)dictview_traverse,            /* tp_traverse */
     3085    0,                                          /* tp_clear */
     3086    dictview_richcompare,                       /* tp_richcompare */
     3087    0,                                          /* tp_weaklistoffset */
     3088    (getiterfunc)dictkeys_iter,                 /* tp_iter */
     3089    0,                                          /* tp_iternext */
     3090    dictkeys_methods,                           /* tp_methods */
     3091    0,
     3092};
     3093
     3094static PyObject *
     3095dictkeys_new(PyObject *dict)
     3096{
     3097    return dictview_new(dict, &PyDictKeys_Type);
     3098}
     3099
     3100/*** dict_items ***/
     3101
     3102static PyObject *
     3103dictitems_iter(dictviewobject *dv)
     3104{
     3105    if (dv->dv_dict == NULL) {
     3106        Py_RETURN_NONE;
     3107    }
     3108    return dictiter_new(dv->dv_dict, &PyDictIterItem_Type);
     3109}
     3110
     3111static int
     3112dictitems_contains(dictviewobject *dv, PyObject *obj)
     3113{
     3114    PyObject *key, *value, *found;
     3115    if (dv->dv_dict == NULL)
     3116        return 0;
     3117    if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2)
     3118        return 0;
     3119    key = PyTuple_GET_ITEM(obj, 0);
     3120    value = PyTuple_GET_ITEM(obj, 1);
     3121    found = PyDict_GetItem((PyObject *)dv->dv_dict, key);
     3122    if (found == NULL) {
     3123        if (PyErr_Occurred())
     3124            return -1;
     3125        return 0;
     3126    }
     3127    return PyObject_RichCompareBool(value, found, Py_EQ);
     3128}
     3129
     3130static PySequenceMethods dictitems_as_sequence = {
     3131    (lenfunc)dictview_len,              /* sq_length */
     3132    0,                                  /* sq_concat */
     3133    0,                                  /* sq_repeat */
     3134    0,                                  /* sq_item */
     3135    0,                                  /* sq_slice */
     3136    0,                                  /* sq_ass_item */
     3137    0,                                  /* sq_ass_slice */
     3138    (objobjproc)dictitems_contains,     /* sq_contains */
     3139};
     3140
     3141static PyMethodDef dictitems_methods[] = {
     3142    {NULL,              NULL}           /* sentinel */
     3143};
     3144
     3145PyTypeObject PyDictItems_Type = {
     3146    PyVarObject_HEAD_INIT(&PyType_Type, 0)
     3147    "dict_items",                               /* tp_name */
     3148    sizeof(dictviewobject),                     /* tp_basicsize */
     3149    0,                                          /* tp_itemsize */
     3150    /* methods */
     3151    (destructor)dictview_dealloc,               /* tp_dealloc */
     3152    0,                                          /* tp_print */
     3153    0,                                          /* tp_getattr */
     3154    0,                                          /* tp_setattr */
     3155    0,                                          /* tp_reserved */
     3156    (reprfunc)dictview_repr,                    /* tp_repr */
     3157    &dictviews_as_number,                       /* tp_as_number */
     3158    &dictitems_as_sequence,                     /* tp_as_sequence */
     3159    0,                                          /* tp_as_mapping */
     3160    0,                                          /* tp_hash */
     3161    0,                                          /* tp_call */
     3162    0,                                          /* tp_str */
     3163    PyObject_GenericGetAttr,                    /* tp_getattro */
     3164    0,                                          /* tp_setattro */
     3165    0,                                          /* tp_as_buffer */
     3166    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
     3167        Py_TPFLAGS_CHECKTYPES,                  /* tp_flags */
     3168    0,                                          /* tp_doc */
     3169    (traverseproc)dictview_traverse,            /* tp_traverse */
     3170    0,                                          /* tp_clear */
     3171    dictview_richcompare,                       /* tp_richcompare */
     3172    0,                                          /* tp_weaklistoffset */
     3173    (getiterfunc)dictitems_iter,                /* tp_iter */
     3174    0,                                          /* tp_iternext */
     3175    dictitems_methods,                          /* tp_methods */
     3176    0,
     3177};
     3178
     3179static PyObject *
     3180dictitems_new(PyObject *dict)
     3181{
     3182    return dictview_new(dict, &PyDictItems_Type);
     3183}
     3184
     3185/*** dict_values ***/
     3186
     3187static PyObject *
     3188dictvalues_iter(dictviewobject *dv)
     3189{
     3190    if (dv->dv_dict == NULL) {
     3191        Py_RETURN_NONE;
     3192    }
     3193    return dictiter_new(dv->dv_dict, &PyDictIterValue_Type);
     3194}
     3195
     3196static PySequenceMethods dictvalues_as_sequence = {
     3197    (lenfunc)dictview_len,              /* sq_length */
     3198    0,                                  /* sq_concat */
     3199    0,                                  /* sq_repeat */
     3200    0,                                  /* sq_item */
     3201    0,                                  /* sq_slice */
     3202    0,                                  /* sq_ass_item */
     3203    0,                                  /* sq_ass_slice */
     3204    (objobjproc)0,                      /* sq_contains */
     3205};
     3206
     3207static PyMethodDef dictvalues_methods[] = {
     3208    {NULL,              NULL}           /* sentinel */
     3209};
     3210
     3211PyTypeObject PyDictValues_Type = {
     3212    PyVarObject_HEAD_INIT(&PyType_Type, 0)
     3213    "dict_values",                              /* tp_name */
     3214    sizeof(dictviewobject),                     /* tp_basicsize */
     3215    0,                                          /* tp_itemsize */
     3216    /* methods */
     3217    (destructor)dictview_dealloc,               /* tp_dealloc */
     3218    0,                                          /* tp_print */
     3219    0,                                          /* tp_getattr */
     3220    0,                                          /* tp_setattr */
     3221    0,                                          /* tp_reserved */
     3222    (reprfunc)dictview_repr,                    /* tp_repr */
     3223    0,                                          /* tp_as_number */
     3224    &dictvalues_as_sequence,                    /* tp_as_sequence */
     3225    0,                                          /* tp_as_mapping */
     3226    0,                                          /* tp_hash */
     3227    0,                                          /* tp_call */
     3228    0,                                          /* tp_str */
     3229    PyObject_GenericGetAttr,                    /* tp_getattro */
     3230    0,                                          /* tp_setattro */
     3231    0,                                          /* tp_as_buffer */
     3232    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
     3233    0,                                          /* tp_doc */
     3234    (traverseproc)dictview_traverse,            /* tp_traverse */
     3235    0,                                          /* tp_clear */
     3236    0,                                          /* tp_richcompare */
     3237    0,                                          /* tp_weaklistoffset */
     3238    (getiterfunc)dictvalues_iter,               /* tp_iter */
     3239    0,                                          /* tp_iternext */
     3240    dictvalues_methods,                         /* tp_methods */
     3241    0,
     3242};
     3243
     3244static PyObject *
     3245dictvalues_new(PyObject *dict)
     3246{
     3247    return dictview_new(dict, &PyDictValues_Type);
     3248}
Note: See TracChangeset for help on using the changeset viewer.