Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 2bc1379 | 1999-03-24 19:06:42 +0000 | [diff] [blame] | 2 | /* Dictionary object implementation using a hash table */ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 3 | |
Raymond Hettinger | 930427b | 2003-05-03 06:51:59 +0000 | [diff] [blame] | 4 | /* The distribution includes a separate file, Objects/dictnotes.txt, |
Tim Peters | 60b2996 | 2006-01-01 01:19:23 +0000 | [diff] [blame] | 5 | describing explorations into dictionary design and optimization. |
Raymond Hettinger | 930427b | 2003-05-03 06:51:59 +0000 | [diff] [blame] | 6 | It covers typical dictionary use patterns, the parameters for |
| 7 | tuning dictionaries, and several ideas for possible optimizations. |
| 8 | */ |
| 9 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 10 | #include "Python.h" |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 11 | |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 12 | |
Georg Brandl | b9f4ad3 | 2006-10-29 18:31:42 +0000 | [diff] [blame] | 13 | /* Set a key error with the specified argument, wrapping it in a |
| 14 | * tuple automatically so that tuple keys are not unpacked as the |
| 15 | * exception arguments. */ |
| 16 | static void |
| 17 | set_key_error(PyObject *arg) |
| 18 | { |
| 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); |
Neal Norwitz | 7b932da | 2006-10-29 23:39:03 +0000 | [diff] [blame] | 24 | Py_DECREF(tup); |
Georg Brandl | b9f4ad3 | 2006-10-29 18:31:42 +0000 | [diff] [blame] | 25 | } |
| 26 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 27 | /* Define this out if you don't want conversion statistics on exit. */ |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 28 | #undef SHOW_CONVERSION_COUNTS |
| 29 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 30 | /* See large comment block below. This must be >= 1. */ |
| 31 | #define PERTURB_SHIFT 5 |
| 32 | |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 33 | /* |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 34 | Major subtleties ahead: Most hash schemes depend on having a "good" hash |
| 35 | function, in the sense of simulating randomness. Python doesn't: its most |
| 36 | important hash functions (for strings and ints) are very regular in common |
| 37 | cases: |
Tim Peters | 15d4929 | 2001-05-27 07:39:22 +0000 | [diff] [blame] | 38 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 39 | >>> map(hash, (0, 1, 2, 3)) |
| 40 | [0, 1, 2, 3] |
| 41 | >>> map(hash, ("namea", "nameb", "namec", "named")) |
| 42 | [-1658398457, -1658398460, -1658398459, -1658398462] |
| 43 | >>> |
Tim Peters | 15d4929 | 2001-05-27 07:39:22 +0000 | [diff] [blame] | 44 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 45 | This isn't necessarily bad! To the contrary, in a table of size 2**i, taking |
| 46 | the low-order i bits as the initial table index is extremely fast, and there |
| 47 | are no collisions at all for dicts indexed by a contiguous range of ints. |
| 48 | The same is approximately true when keys are "consecutive" strings. So this |
| 49 | gives better-than-random behavior in common cases, and that's very desirable. |
Tim Peters | 15d4929 | 2001-05-27 07:39:22 +0000 | [diff] [blame] | 50 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 51 | OTOH, when collisions occur, the tendency to fill contiguous slices of the |
| 52 | hash table makes a good collision resolution strategy crucial. Taking only |
| 53 | the last i bits of the hash code is also vulnerable: for example, consider |
| 54 | [i << 16 for i in range(20000)] as a set of keys. Since ints are their own |
| 55 | hash codes, and this fits in a dict of size 2**15, the last 15 bits of every |
| 56 | hash code are all 0: they *all* map to the same table index. |
Tim Peters | 15d4929 | 2001-05-27 07:39:22 +0000 | [diff] [blame] | 57 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 58 | But catering to unusual cases should not slow the usual ones, so we just take |
| 59 | the last i bits anyway. It's up to collision resolution to do the rest. If |
| 60 | we *usually* find the key we're looking for on the first try (and, it turns |
| 61 | out, we usually do -- the table load factor is kept under 2/3, so the odds |
| 62 | are solidly in our favor), then it makes best sense to keep the initial index |
| 63 | computation dirt cheap. |
Tim Peters | 15d4929 | 2001-05-27 07:39:22 +0000 | [diff] [blame] | 64 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 65 | The first half of collision resolution is to visit table indices via this |
| 66 | recurrence: |
Tim Peters | 15d4929 | 2001-05-27 07:39:22 +0000 | [diff] [blame] | 67 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 68 | j = ((5*j) + 1) mod 2**i |
Tim Peters | 15d4929 | 2001-05-27 07:39:22 +0000 | [diff] [blame] | 69 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 70 | For any initial j in range(2**i), repeating that 2**i times generates each |
| 71 | int in range(2**i) exactly once (see any text on random-number generation for |
| 72 | proof). By itself, this doesn't help much: like linear probing (setting |
| 73 | j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed |
| 74 | order. This would be bad, except that's not the only thing we do, and it's |
| 75 | actually *good* in the common cases where hash keys are consecutive. In an |
| 76 | example that's really too small to make this entirely clear, for a table of |
| 77 | size 2**3 the order of indices is: |
Tim Peters | 15d4929 | 2001-05-27 07:39:22 +0000 | [diff] [blame] | 78 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 79 | 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating] |
| 80 | |
| 81 | If two things come in at index 5, the first place we look after is index 2, |
| 82 | not 6, so if another comes in at index 6 the collision at 5 didn't hurt it. |
| 83 | Linear probing is deadly in this case because there the fixed probe order |
| 84 | is the *same* as the order consecutive keys are likely to arrive. But it's |
| 85 | extremely unlikely hash codes will follow a 5*j+1 recurrence by accident, |
| 86 | and certain that consecutive hash codes do not. |
| 87 | |
| 88 | The other half of the strategy is to get the other bits of the hash code |
| 89 | into play. This is done by initializing a (unsigned) vrbl "perturb" to the |
| 90 | full hash code, and changing the recurrence to: |
| 91 | |
| 92 | j = (5*j) + 1 + perturb; |
| 93 | perturb >>= PERTURB_SHIFT; |
| 94 | use j % 2**i as the next table index; |
| 95 | |
| 96 | Now the probe sequence depends (eventually) on every bit in the hash code, |
| 97 | and the pseudo-scrambling property of recurring on 5*j+1 is more valuable, |
| 98 | because it quickly magnifies small differences in the bits that didn't affect |
| 99 | the initial index. Note that because perturb is unsigned, if the recurrence |
| 100 | is executed often enough perturb eventually becomes and remains 0. At that |
| 101 | point (very rarely reached) the recurrence is on (just) 5*j+1 again, and |
| 102 | that's certain to find an empty slot eventually (since it generates every int |
| 103 | in range(2**i), and we make sure there's always at least one empty slot). |
| 104 | |
| 105 | Selecting a good value for PERTURB_SHIFT is a balancing act. You want it |
| 106 | small so that the high bits of the hash code continue to affect the probe |
| 107 | sequence across iterations; but you want it large so that in really bad cases |
| 108 | the high-order hash bits have an effect on early iterations. 5 was "the |
| 109 | best" in minimizing total collisions across experiments Tim Peters ran (on |
| 110 | both normal and pathological cases), but 4 and 6 weren't significantly worse. |
| 111 | |
| 112 | Historical: Reimer Behrends contributed the idea of using a polynomial-based |
| 113 | approach, using repeated multiplication by x in GF(2**n) where an irreducible |
| 114 | polynomial for each table size was chosen such that x was a primitive root. |
| 115 | Christian Tismer later extended that to use division by x instead, as an |
| 116 | efficient way to get the high bits of the hash code into play. This scheme |
| 117 | also gave excellent collision statistics, but was more expensive: two |
| 118 | if-tests were required inside the loop; computing "the next" index took about |
| 119 | the same number of operations but without as much potential parallelism |
| 120 | (e.g., computing 5*j can go on at the same time as computing 1+perturb in the |
| 121 | above, and then shifting perturb can be done while the table index is being |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 122 | masked); and the PyDictObject struct required a member to hold the table's |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 123 | polynomial. In Tim's experiments the current scheme ran faster, produced |
| 124 | equally good collision statistics, needed less code & used less memory. |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 125 | |
| 126 | Theoretical Python 2.5 headache: hash codes are only C "long", but |
| 127 | sizeof(Py_ssize_t) > sizeof(long) may be possible. In that case, and if a |
| 128 | dict is genuinely huge, then only the slots directly reachable via indexing |
| 129 | by a C long can be the first slot in a probe sequence. The probe sequence |
| 130 | will still eventually reach every slot in the table, but the collision rate |
| 131 | on initial probes may be much higher than this scheme was designed for. |
| 132 | Getting a hash code as fat as Py_ssize_t is the only real cure. But in |
| 133 | practice, this probably won't make a lick of difference for many years (at |
| 134 | which point everyone will have terabytes of RAM on 64-bit boxes). |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 135 | */ |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 136 | |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 137 | /* Object used as dummy key to fill deleted entries */ |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 138 | static PyObject *dummy = NULL; /* Initialized by first call to newPyDictObject() */ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 139 | |
Armin Rigo | e170937 | 2006-04-12 17:06:05 +0000 | [diff] [blame] | 140 | #ifdef Py_REF_DEBUG |
| 141 | PyObject * |
| 142 | _PyDict_Dummy(void) |
| 143 | { |
| 144 | return dummy; |
| 145 | } |
| 146 | #endif |
| 147 | |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 148 | /* forward declarations */ |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 149 | static PyDictEntry * |
| 150 | lookdict_string(PyDictObject *mp, PyObject *key, long hash); |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 151 | |
| 152 | #ifdef SHOW_CONVERSION_COUNTS |
| 153 | static long created = 0L; |
| 154 | static long converted = 0L; |
| 155 | |
| 156 | static void |
| 157 | show_counts(void) |
| 158 | { |
| 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); |
| 162 | } |
| 163 | #endif |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 164 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 165 | /* Initialization macros. |
| 166 | There are two ways to create a dict: PyDict_New() is the main C API |
| 167 | function, and the tp_new slot maps to dict_new(). In the latter case we |
| 168 | can save a little time over what PyDict_New does because it's guaranteed |
| 169 | that the PyDictObject struct is already zeroed out. |
| 170 | Everyone except dict_new() should use EMPTY_TO_MINSIZE (unless they have |
| 171 | an excellent reason not to). |
| 172 | */ |
| 173 | |
| 174 | #define INIT_NONZERO_DICT_SLOTS(mp) do { \ |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 175 | (mp)->ma_table = (mp)->ma_smalltable; \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 176 | (mp)->ma_mask = PyDict_MINSIZE - 1; \ |
| 177 | } while(0) |
| 178 | |
| 179 | #define EMPTY_TO_MINSIZE(mp) do { \ |
| 180 | memset((mp)->ma_smalltable, 0, sizeof((mp)->ma_smalltable)); \ |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 181 | (mp)->ma_used = (mp)->ma_fill = 0; \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 182 | INIT_NONZERO_DICT_SLOTS(mp); \ |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 183 | } while(0) |
| 184 | |
Raymond Hettinger | 4344278 | 2004-03-17 21:55:03 +0000 | [diff] [blame] | 185 | /* Dictionary reuse scheme to save calls to malloc, free, and memset */ |
| 186 | #define MAXFREEDICTS 80 |
| 187 | static PyDictObject *free_dicts[MAXFREEDICTS]; |
| 188 | static int num_free_dicts = 0; |
| 189 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 190 | PyObject * |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 191 | PyDict_New(void) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 192 | { |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 193 | register PyDictObject *mp; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 194 | if (dummy == NULL) { /* Auto-initialize dummy */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 195 | dummy = PyString_FromString("<dummy key>"); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 196 | if (dummy == NULL) |
| 197 | return NULL; |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 198 | #ifdef SHOW_CONVERSION_COUNTS |
| 199 | Py_AtExit(show_counts); |
| 200 | #endif |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 201 | } |
Raymond Hettinger | 4344278 | 2004-03-17 21:55:03 +0000 | [diff] [blame] | 202 | if (num_free_dicts) { |
| 203 | mp = free_dicts[--num_free_dicts]; |
| 204 | assert (mp != NULL); |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame^] | 205 | assert (Py_TYPE(mp) == &PyDict_Type); |
Raymond Hettinger | 4344278 | 2004-03-17 21:55:03 +0000 | [diff] [blame] | 206 | _Py_NewReference((PyObject *)mp); |
| 207 | if (mp->ma_fill) { |
| 208 | EMPTY_TO_MINSIZE(mp); |
| 209 | } |
| 210 | assert (mp->ma_used == 0); |
| 211 | assert (mp->ma_table == mp->ma_smalltable); |
| 212 | assert (mp->ma_mask == PyDict_MINSIZE - 1); |
| 213 | } else { |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 214 | mp = PyObject_GC_New(PyDictObject, &PyDict_Type); |
Raymond Hettinger | 4344278 | 2004-03-17 21:55:03 +0000 | [diff] [blame] | 215 | if (mp == NULL) |
| 216 | return NULL; |
| 217 | EMPTY_TO_MINSIZE(mp); |
| 218 | } |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 219 | mp->ma_lookup = lookdict_string; |
| 220 | #ifdef SHOW_CONVERSION_COUNTS |
| 221 | ++created; |
| 222 | #endif |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 223 | _PyObject_GC_TRACK(mp); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 224 | return (PyObject *)mp; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | /* |
| 228 | The basic lookup function used by all operations. |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 229 | This is based on Algorithm D from Knuth Vol. 3, Sec. 6.4. |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 230 | Open addressing is preferred over chaining since the link overhead for |
| 231 | chaining would be substantial (100% with typical malloc overhead). |
| 232 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 233 | The initial probe index is computed as hash mod the table size. Subsequent |
| 234 | probe indices are computed as explained earlier. |
Guido van Rossum | 2bc1379 | 1999-03-24 19:06:42 +0000 | [diff] [blame] | 235 | |
| 236 | All arithmetic on hash should ignore overflow. |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 237 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 238 | (The details in this version are due to Tim Peters, building on many past |
| 239 | contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and |
| 240 | Christian Tismer). |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 241 | |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 242 | lookdict() is general-purpose, and may return NULL if (and only if) a |
| 243 | comparison raises an exception (this was new in Python 2.5). |
| 244 | lookdict_string() below is specialized to string keys, comparison of which can |
| 245 | never raise an exception; that function can never return NULL. For both, when |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 246 | the key isn't found a PyDictEntry* is returned for which the me_value field is |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 247 | NULL; this is the slot in the dict at which the key would have been found, and |
| 248 | the caller can (if it wishes) add the <key, value> pair to the returned |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 249 | PyDictEntry*. |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 250 | */ |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 251 | static PyDictEntry * |
| 252 | lookdict(PyDictObject *mp, PyObject *key, register long hash) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 253 | { |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 254 | register size_t i; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 255 | register size_t perturb; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 256 | register PyDictEntry *freeslot; |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 257 | register size_t mask = (size_t)mp->ma_mask; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 258 | PyDictEntry *ep0 = mp->ma_table; |
| 259 | register PyDictEntry *ep; |
Fred Drake | c88b99c | 2000-08-31 19:04:07 +0000 | [diff] [blame] | 260 | register int cmp; |
Tim Peters | 453163d | 2001-06-03 04:54:32 +0000 | [diff] [blame] | 261 | PyObject *startkey; |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 262 | |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 263 | i = (size_t)hash & mask; |
Guido van Rossum | efb4609 | 1997-01-29 15:53:56 +0000 | [diff] [blame] | 264 | ep = &ep0[i]; |
Guido van Rossum | c1c7b1a | 1998-10-06 16:01:14 +0000 | [diff] [blame] | 265 | if (ep->me_key == NULL || ep->me_key == key) |
Guido van Rossum | 7d18159 | 1997-01-16 21:06:45 +0000 | [diff] [blame] | 266 | return ep; |
Tim Peters | 7b5d0af | 2001-06-03 04:14:43 +0000 | [diff] [blame] | 267 | |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 268 | if (ep->me_key == dummy) |
Guido van Rossum | 7d18159 | 1997-01-16 21:06:45 +0000 | [diff] [blame] | 269 | freeslot = ep; |
Guido van Rossum | fd7a0b8 | 1997-08-18 21:52:47 +0000 | [diff] [blame] | 270 | else { |
Fred Drake | c88b99c | 2000-08-31 19:04:07 +0000 | [diff] [blame] | 271 | if (ep->me_hash == hash) { |
Tim Peters | 453163d | 2001-06-03 04:54:32 +0000 | [diff] [blame] | 272 | startkey = ep->me_key; |
Georg Brandl | ede3a32 | 2007-11-29 18:33:01 +0000 | [diff] [blame] | 273 | Py_INCREF(startkey); |
Tim Peters | 453163d | 2001-06-03 04:54:32 +0000 | [diff] [blame] | 274 | cmp = PyObject_RichCompareBool(startkey, key, Py_EQ); |
Georg Brandl | ede3a32 | 2007-11-29 18:33:01 +0000 | [diff] [blame] | 275 | Py_DECREF(startkey); |
Tim Peters | 7b5d0af | 2001-06-03 04:14:43 +0000 | [diff] [blame] | 276 | if (cmp < 0) |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 277 | return NULL; |
Tim Peters | 453163d | 2001-06-03 04:54:32 +0000 | [diff] [blame] | 278 | if (ep0 == mp->ma_table && ep->me_key == startkey) { |
| 279 | if (cmp > 0) |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 280 | return ep; |
Tim Peters | 453163d | 2001-06-03 04:54:32 +0000 | [diff] [blame] | 281 | } |
| 282 | else { |
| 283 | /* The compare did major nasty stuff to the |
| 284 | * dict: start over. |
| 285 | * XXX A clever adversary could prevent this |
| 286 | * XXX from terminating. |
| 287 | */ |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 288 | return lookdict(mp, key, hash); |
Tim Peters | 453163d | 2001-06-03 04:54:32 +0000 | [diff] [blame] | 289 | } |
Guido van Rossum | fd7a0b8 | 1997-08-18 21:52:47 +0000 | [diff] [blame] | 290 | } |
| 291 | freeslot = NULL; |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 292 | } |
Tim Peters | 15d4929 | 2001-05-27 07:39:22 +0000 | [diff] [blame] | 293 | |
Tim Peters | 342c65e | 2001-05-13 06:43:53 +0000 | [diff] [blame] | 294 | /* In the loop, me_key == dummy is by far (factor of 100s) the |
| 295 | least likely outcome, so test for that last. */ |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 296 | for (perturb = hash; ; perturb >>= PERTURB_SHIFT) { |
| 297 | i = (i << 2) + i + perturb + 1; |
| 298 | ep = &ep0[i & mask]; |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 299 | if (ep->me_key == NULL) |
| 300 | return freeslot == NULL ? ep : freeslot; |
Tim Peters | 7b5d0af | 2001-06-03 04:14:43 +0000 | [diff] [blame] | 301 | if (ep->me_key == key) |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 302 | return ep; |
Tim Peters | 7b5d0af | 2001-06-03 04:14:43 +0000 | [diff] [blame] | 303 | if (ep->me_hash == hash && ep->me_key != dummy) { |
Tim Peters | 453163d | 2001-06-03 04:54:32 +0000 | [diff] [blame] | 304 | startkey = ep->me_key; |
Georg Brandl | ede3a32 | 2007-11-29 18:33:01 +0000 | [diff] [blame] | 305 | Py_INCREF(startkey); |
Tim Peters | 453163d | 2001-06-03 04:54:32 +0000 | [diff] [blame] | 306 | cmp = PyObject_RichCompareBool(startkey, key, Py_EQ); |
Georg Brandl | ede3a32 | 2007-11-29 18:33:01 +0000 | [diff] [blame] | 307 | Py_DECREF(startkey); |
Tim Peters | 7b5d0af | 2001-06-03 04:14:43 +0000 | [diff] [blame] | 308 | if (cmp < 0) |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 309 | return NULL; |
Tim Peters | 453163d | 2001-06-03 04:54:32 +0000 | [diff] [blame] | 310 | if (ep0 == mp->ma_table && ep->me_key == startkey) { |
| 311 | if (cmp > 0) |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 312 | return ep; |
Tim Peters | 453163d | 2001-06-03 04:54:32 +0000 | [diff] [blame] | 313 | } |
| 314 | else { |
| 315 | /* The compare did major nasty stuff to the |
| 316 | * dict: start over. |
| 317 | * XXX A clever adversary could prevent this |
| 318 | * XXX from terminating. |
| 319 | */ |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 320 | return lookdict(mp, key, hash); |
Tim Peters | 453163d | 2001-06-03 04:54:32 +0000 | [diff] [blame] | 321 | } |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 322 | } |
Tim Peters | 342c65e | 2001-05-13 06:43:53 +0000 | [diff] [blame] | 323 | else if (ep->me_key == dummy && freeslot == NULL) |
| 324 | freeslot = ep; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 325 | } |
Neal Norwitz | a5ccda9 | 2006-10-28 21:16:54 +0000 | [diff] [blame] | 326 | assert(0); /* NOT REACHED */ |
| 327 | return 0; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | /* |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 331 | * Hacked up version of lookdict which can assume keys are always strings; |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 332 | * this assumption allows testing for errors during PyObject_RichCompareBool() |
| 333 | * to be dropped; string-string comparisons never raise exceptions. This also |
| 334 | * means we don't need to go through PyObject_RichCompareBool(); we can always |
| 335 | * use _PyString_Eq() directly. |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 336 | * |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 337 | * This is valuable because dicts with only string keys are very common. |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 338 | */ |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 339 | static PyDictEntry * |
| 340 | lookdict_string(PyDictObject *mp, PyObject *key, register long hash) |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 341 | { |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 342 | register size_t i; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 343 | register size_t perturb; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 344 | register PyDictEntry *freeslot; |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 345 | register size_t mask = (size_t)mp->ma_mask; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 346 | PyDictEntry *ep0 = mp->ma_table; |
| 347 | register PyDictEntry *ep; |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 348 | |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 349 | /* Make sure this function doesn't have to handle non-string keys, |
| 350 | including subclasses of str; e.g., one reason to subclass |
| 351 | strings is to override __eq__, and for speed we don't cater to |
| 352 | that here. */ |
| 353 | if (!PyString_CheckExact(key)) { |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 354 | #ifdef SHOW_CONVERSION_COUNTS |
| 355 | ++converted; |
| 356 | #endif |
| 357 | mp->ma_lookup = lookdict; |
| 358 | return lookdict(mp, key, hash); |
| 359 | } |
Tim Peters | 2f228e7 | 2001-05-13 00:19:31 +0000 | [diff] [blame] | 360 | i = hash & mask; |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 361 | ep = &ep0[i]; |
| 362 | if (ep->me_key == NULL || ep->me_key == key) |
| 363 | return ep; |
| 364 | if (ep->me_key == dummy) |
| 365 | freeslot = ep; |
| 366 | else { |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 367 | if (ep->me_hash == hash && _PyString_Eq(ep->me_key, key)) |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 368 | return ep; |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 369 | freeslot = NULL; |
| 370 | } |
Tim Peters | 15d4929 | 2001-05-27 07:39:22 +0000 | [diff] [blame] | 371 | |
Tim Peters | 342c65e | 2001-05-13 06:43:53 +0000 | [diff] [blame] | 372 | /* In the loop, me_key == dummy is by far (factor of 100s) the |
| 373 | least likely outcome, so test for that last. */ |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 374 | for (perturb = hash; ; perturb >>= PERTURB_SHIFT) { |
| 375 | i = (i << 2) + i + perturb + 1; |
| 376 | ep = &ep0[i & mask]; |
Tim Peters | 342c65e | 2001-05-13 06:43:53 +0000 | [diff] [blame] | 377 | if (ep->me_key == NULL) |
| 378 | return freeslot == NULL ? ep : freeslot; |
| 379 | if (ep->me_key == key |
| 380 | || (ep->me_hash == hash |
| 381 | && ep->me_key != dummy |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 382 | && _PyString_Eq(ep->me_key, key))) |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 383 | return ep; |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 384 | if (ep->me_key == dummy && freeslot == NULL) |
Tim Peters | 342c65e | 2001-05-13 06:43:53 +0000 | [diff] [blame] | 385 | freeslot = ep; |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 386 | } |
Neal Norwitz | a5ccda9 | 2006-10-28 21:16:54 +0000 | [diff] [blame] | 387 | assert(0); /* NOT REACHED */ |
| 388 | return 0; |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | /* |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 392 | Internal routine to insert a new item into the table. |
| 393 | Used both by the internal resize routine and by the public insert routine. |
| 394 | Eats a reference to key and one to value. |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 395 | Returns -1 if an error occurred, or 0 on success. |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 396 | */ |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 397 | static int |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 398 | insertdict(register PyDictObject *mp, PyObject *key, long hash, PyObject *value) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 399 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 400 | PyObject *old_value; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 401 | register PyDictEntry *ep; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 402 | typedef PyDictEntry *(*lookupfunc)(PyDictObject *, PyObject *, long); |
| 403 | |
| 404 | assert(mp->ma_lookup != NULL); |
| 405 | ep = mp->ma_lookup(mp, key, hash); |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 406 | if (ep == NULL) { |
| 407 | Py_DECREF(key); |
| 408 | Py_DECREF(value); |
| 409 | return -1; |
| 410 | } |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 411 | if (ep->me_value != NULL) { |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 412 | old_value = ep->me_value; |
| 413 | ep->me_value = value; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 414 | Py_DECREF(old_value); /* which **CAN** re-enter */ |
| 415 | Py_DECREF(key); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 416 | } |
| 417 | else { |
| 418 | if (ep->me_key == NULL) |
| 419 | mp->ma_fill++; |
Raymond Hettinger | 07ead17 | 2005-02-05 23:42:57 +0000 | [diff] [blame] | 420 | else { |
| 421 | assert(ep->me_key == dummy); |
| 422 | Py_DECREF(dummy); |
| 423 | } |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 424 | ep->me_key = key; |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 425 | ep->me_hash = (Py_ssize_t)hash; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 426 | ep->me_value = value; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 427 | mp->ma_used++; |
| 428 | } |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 429 | return 0; |
| 430 | } |
| 431 | |
| 432 | /* |
| 433 | Internal routine used by dictresize() to insert an item which is |
| 434 | known to be absent from the dict. This routine also assumes that |
| 435 | the dict contains no deleted entries. Besides the performance benefit, |
| 436 | using insertdict() in dictresize() is dangerous (SF bug #1456209). |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 437 | Note that no refcounts are changed by this routine; if needed, the caller |
| 438 | is responsible for incref'ing `key` and `value`. |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 439 | */ |
| 440 | static void |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 441 | insertdict_clean(register PyDictObject *mp, PyObject *key, long hash, |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 442 | PyObject *value) |
| 443 | { |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 444 | register size_t i; |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 445 | register size_t perturb; |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 446 | register size_t mask = (size_t)mp->ma_mask; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 447 | PyDictEntry *ep0 = mp->ma_table; |
| 448 | register PyDictEntry *ep; |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 449 | |
| 450 | i = hash & mask; |
| 451 | ep = &ep0[i]; |
| 452 | for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) { |
| 453 | i = (i << 2) + i + perturb + 1; |
| 454 | ep = &ep0[i & mask]; |
| 455 | } |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 456 | assert(ep->me_value == NULL); |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 457 | mp->ma_fill++; |
| 458 | ep->me_key = key; |
| 459 | ep->me_hash = (Py_ssize_t)hash; |
| 460 | ep->me_value = value; |
| 461 | mp->ma_used++; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | /* |
| 465 | Restructure the table by allocating a new table and reinserting all |
| 466 | items again. When entries have been deleted, the new table may |
| 467 | actually be smaller than the old one. |
| 468 | */ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 469 | static int |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 470 | dictresize(PyDictObject *mp, Py_ssize_t minused) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 471 | { |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 472 | Py_ssize_t newsize; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 473 | PyDictEntry *oldtable, *newtable, *ep; |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 474 | Py_ssize_t i; |
Tim Peters | 0c6010b | 2001-05-23 23:33:57 +0000 | [diff] [blame] | 475 | int is_oldtable_malloced; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 476 | PyDictEntry small_copy[PyDict_MINSIZE]; |
Tim Peters | 91a364d | 2001-05-19 07:04:38 +0000 | [diff] [blame] | 477 | |
| 478 | assert(minused >= 0); |
Tim Peters | 0c6010b | 2001-05-23 23:33:57 +0000 | [diff] [blame] | 479 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 480 | /* Find the smallest table size > minused. */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 481 | for (newsize = PyDict_MINSIZE; |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 482 | newsize <= minused && newsize > 0; |
| 483 | newsize <<= 1) |
| 484 | ; |
| 485 | if (newsize <= 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 486 | PyErr_NoMemory(); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 487 | return -1; |
| 488 | } |
Tim Peters | 0c6010b | 2001-05-23 23:33:57 +0000 | [diff] [blame] | 489 | |
| 490 | /* Get space for a new table. */ |
| 491 | oldtable = mp->ma_table; |
| 492 | assert(oldtable != NULL); |
| 493 | is_oldtable_malloced = oldtable != mp->ma_smalltable; |
| 494 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 495 | if (newsize == PyDict_MINSIZE) { |
Tim Peters | f8a548c | 2001-05-24 16:26:40 +0000 | [diff] [blame] | 496 | /* A large table is shrinking, or we can't get any smaller. */ |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 497 | newtable = mp->ma_smalltable; |
Tim Peters | 0c6010b | 2001-05-23 23:33:57 +0000 | [diff] [blame] | 498 | if (newtable == oldtable) { |
Tim Peters | f8a548c | 2001-05-24 16:26:40 +0000 | [diff] [blame] | 499 | if (mp->ma_fill == mp->ma_used) { |
| 500 | /* No dummies, so no point doing anything. */ |
Tim Peters | 0c6010b | 2001-05-23 23:33:57 +0000 | [diff] [blame] | 501 | return 0; |
Tim Peters | f8a548c | 2001-05-24 16:26:40 +0000 | [diff] [blame] | 502 | } |
| 503 | /* We're not going to resize it, but rebuild the |
| 504 | table anyway to purge old dummy entries. |
| 505 | Subtle: This is *necessary* if fill==size, |
| 506 | as lookdict needs at least one virgin slot to |
| 507 | terminate failing searches. If fill < size, it's |
| 508 | merely desirable, as dummies slow searches. */ |
| 509 | assert(mp->ma_fill > mp->ma_used); |
Tim Peters | 0c6010b | 2001-05-23 23:33:57 +0000 | [diff] [blame] | 510 | memcpy(small_copy, oldtable, sizeof(small_copy)); |
| 511 | oldtable = small_copy; |
| 512 | } |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 513 | } |
| 514 | else { |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 515 | newtable = PyMem_NEW(PyDictEntry, newsize); |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 516 | if (newtable == NULL) { |
| 517 | PyErr_NoMemory(); |
| 518 | return -1; |
| 519 | } |
| 520 | } |
Tim Peters | 0c6010b | 2001-05-23 23:33:57 +0000 | [diff] [blame] | 521 | |
| 522 | /* Make the dict empty, using the new table. */ |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 523 | assert(newtable != oldtable); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 524 | mp->ma_table = newtable; |
Tim Peters | afb6ae8 | 2001-06-04 21:00:21 +0000 | [diff] [blame] | 525 | mp->ma_mask = newsize - 1; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 526 | memset(newtable, 0, sizeof(PyDictEntry) * newsize); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 527 | mp->ma_used = 0; |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 528 | i = mp->ma_fill; |
| 529 | mp->ma_fill = 0; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 530 | |
Tim Peters | 1928314 | 2001-05-17 22:25:34 +0000 | [diff] [blame] | 531 | /* Copy the data over; this is refcount-neutral for active entries; |
| 532 | dummy entries aren't copied over, of course */ |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 533 | for (ep = oldtable; i > 0; ep++) { |
| 534 | if (ep->me_value != NULL) { /* active entry */ |
| 535 | --i; |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 536 | insertdict_clean(mp, ep->me_key, (long)ep->me_hash, |
| 537 | ep->me_value); |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 538 | } |
Tim Peters | 1928314 | 2001-05-17 22:25:34 +0000 | [diff] [blame] | 539 | else if (ep->me_key != NULL) { /* dummy entry */ |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 540 | --i; |
Tim Peters | 1928314 | 2001-05-17 22:25:34 +0000 | [diff] [blame] | 541 | assert(ep->me_key == dummy); |
| 542 | Py_DECREF(ep->me_key); |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 543 | } |
Tim Peters | 1928314 | 2001-05-17 22:25:34 +0000 | [diff] [blame] | 544 | /* else key == value == NULL: nothing to do */ |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 545 | } |
| 546 | |
Tim Peters | 0c6010b | 2001-05-23 23:33:57 +0000 | [diff] [blame] | 547 | if (is_oldtable_malloced) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 548 | PyMem_DEL(oldtable); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 549 | return 0; |
| 550 | } |
| 551 | |
Raymond Hettinger | fd7ed40 | 2007-12-18 21:24:09 +0000 | [diff] [blame] | 552 | /* Create a new dictionary pre-sized to hold an estimated number of elements. |
| 553 | Underestimates are okay because the dictionary will resize as necessary. |
| 554 | Overestimates just mean the dictionary will be more sparse than usual. |
| 555 | */ |
| 556 | |
| 557 | PyObject * |
| 558 | _PyDict_NewPresized(Py_ssize_t minused) |
| 559 | { |
| 560 | PyObject *op = PyDict_New(); |
| 561 | |
| 562 | if (minused>5 && op != NULL && dictresize((PyDictObject *)op, minused) == -1) { |
| 563 | Py_DECREF(op); |
| 564 | return NULL; |
| 565 | } |
| 566 | return op; |
| 567 | } |
| 568 | |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 569 | /* Note that, for historical reasons, PyDict_GetItem() suppresses all errors |
| 570 | * that may occur (originally dicts supported only string keys, and exceptions |
| 571 | * weren't possible). So, while the original intent was that a NULL return |
Andrew M. Kuchling | 0067b5f | 2006-08-04 20:37:43 +0000 | [diff] [blame] | 572 | * meant the key wasn't present, in reality it can mean that, or that an error |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 573 | * (suppressed) occurred while computing the key's hash, or that some error |
| 574 | * (suppressed) occurred when comparing keys in the dict's internal probe |
| 575 | * sequence. A nasty example of the latter is when a Python-coded comparison |
| 576 | * function hits a stack-depth error, which can cause this to return NULL |
| 577 | * even if the key is present. |
| 578 | */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 579 | PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 580 | PyDict_GetItem(PyObject *op, PyObject *key) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 581 | { |
| 582 | long hash; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 583 | PyDictObject *mp = (PyDictObject *)op; |
| 584 | PyDictEntry *ep; |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 585 | PyThreadState *tstate; |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 586 | if (!PyDict_Check(op)) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 587 | return NULL; |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 588 | if (!PyString_CheckExact(key) || |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 589 | (hash = ((PyStringObject *) key)->ob_shash) == -1) |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 590 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 591 | hash = PyObject_Hash(key); |
Guido van Rossum | 474b19e | 1998-05-14 01:00:51 +0000 | [diff] [blame] | 592 | if (hash == -1) { |
| 593 | PyErr_Clear(); |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 594 | return NULL; |
Guido van Rossum | 474b19e | 1998-05-14 01:00:51 +0000 | [diff] [blame] | 595 | } |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 596 | } |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 597 | |
| 598 | /* We can arrive here with a NULL tstate during initialization: |
| 599 | try running "python -Wi" for an example related to string |
| 600 | interning. Let's just hope that no exception occurs then... */ |
Armin Rigo | acd0d6d | 2006-06-10 10:57:40 +0000 | [diff] [blame] | 601 | tstate = _PyThreadState_Current; |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 602 | if (tstate != NULL && tstate->curexc_type != NULL) { |
| 603 | /* preserve the existing exception */ |
| 604 | PyObject *err_type, *err_value, *err_tb; |
| 605 | PyErr_Fetch(&err_type, &err_value, &err_tb); |
| 606 | ep = (mp->ma_lookup)(mp, key, hash); |
| 607 | /* ignore errors */ |
| 608 | PyErr_Restore(err_type, err_value, err_tb); |
| 609 | if (ep == NULL) |
| 610 | return NULL; |
| 611 | } |
| 612 | else { |
| 613 | ep = (mp->ma_lookup)(mp, key, hash); |
| 614 | if (ep == NULL) { |
| 615 | PyErr_Clear(); |
| 616 | return NULL; |
| 617 | } |
| 618 | } |
| 619 | return ep->me_value; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 620 | } |
| 621 | |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 622 | /* CAUTION: PyDict_SetItem() must guarantee that it won't resize the |
Tim Peters | 60b2996 | 2006-01-01 01:19:23 +0000 | [diff] [blame] | 623 | * dictionary if it's merely replacing the value for an existing key. |
| 624 | * This means that it's safe to loop over a dictionary with PyDict_Next() |
| 625 | * and occasionally replace a value -- but you can't insert new keys or |
| 626 | * remove them. |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 627 | */ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 628 | int |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 629 | PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 630 | { |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 631 | register PyDictObject *mp; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 632 | register long hash; |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 633 | register Py_ssize_t n_used; |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 634 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 635 | if (!PyDict_Check(op)) { |
| 636 | PyErr_BadInternalCall(); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 637 | return -1; |
| 638 | } |
Neal Norwitz | 48808a1 | 2006-07-21 05:29:58 +0000 | [diff] [blame] | 639 | assert(key); |
| 640 | assert(value); |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 641 | mp = (PyDictObject *)op; |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 642 | if (PyString_CheckExact(key)) { |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 643 | hash = ((PyStringObject *)key)->ob_shash; |
| 644 | if (hash == -1) |
| 645 | hash = PyObject_Hash(key); |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 646 | } |
Tim Peters | 1f7df35 | 2002-03-29 03:29:08 +0000 | [diff] [blame] | 647 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 648 | hash = PyObject_Hash(key); |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 649 | if (hash == -1) |
| 650 | return -1; |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 651 | } |
Tim Peters | afb6ae8 | 2001-06-04 21:00:21 +0000 | [diff] [blame] | 652 | assert(mp->ma_fill <= mp->ma_mask); /* at least one empty slot */ |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 653 | n_used = mp->ma_used; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 654 | Py_INCREF(value); |
| 655 | Py_INCREF(key); |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 656 | if (insertdict(mp, key, hash, value) != 0) |
| 657 | return -1; |
Raymond Hettinger | 3539f6b | 2003-05-05 22:22:10 +0000 | [diff] [blame] | 658 | /* If we added a key, we can safely resize. Otherwise just return! |
| 659 | * If fill >= 2/3 size, adjust size. Normally, this doubles or |
| 660 | * quaduples the size, but it's also possible for the dict to shrink |
Tim Peters | 60b2996 | 2006-01-01 01:19:23 +0000 | [diff] [blame] | 661 | * (if ma_fill is much larger than ma_used, meaning a lot of dict |
Raymond Hettinger | 3539f6b | 2003-05-05 22:22:10 +0000 | [diff] [blame] | 662 | * keys have been * deleted). |
Tim Peters | 60b2996 | 2006-01-01 01:19:23 +0000 | [diff] [blame] | 663 | * |
Raymond Hettinger | 3539f6b | 2003-05-05 22:22:10 +0000 | [diff] [blame] | 664 | * Quadrupling the size improves average dictionary sparseness |
| 665 | * (reducing collisions) at the cost of some memory and iteration |
| 666 | * speed (which loops over every possible entry). It also halves |
Neal Norwitz | 80af59c | 2006-05-30 04:19:21 +0000 | [diff] [blame] | 667 | * the number of expensive resize operations in a growing dictionary. |
Tim Peters | 60b2996 | 2006-01-01 01:19:23 +0000 | [diff] [blame] | 668 | * |
| 669 | * Very large dictionaries (over 50K items) use doubling instead. |
Raymond Hettinger | 3539f6b | 2003-05-05 22:22:10 +0000 | [diff] [blame] | 670 | * This may help applications with severe memory constraints. |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 671 | */ |
Raymond Hettinger | 3539f6b | 2003-05-05 22:22:10 +0000 | [diff] [blame] | 672 | if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2)) |
| 673 | return 0; |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 674 | return dictresize(mp, (mp->ma_used > 50000 ? 2 : 4) * mp->ma_used); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | int |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 678 | PyDict_DelItem(PyObject *op, PyObject *key) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 679 | { |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 680 | register PyDictObject *mp; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 681 | register long hash; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 682 | register PyDictEntry *ep; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 683 | PyObject *old_value, *old_key; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 684 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 685 | if (!PyDict_Check(op)) { |
| 686 | PyErr_BadInternalCall(); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 687 | return -1; |
| 688 | } |
Neal Norwitz | 48808a1 | 2006-07-21 05:29:58 +0000 | [diff] [blame] | 689 | assert(key); |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 690 | if (!PyString_CheckExact(key) || |
Tim Peters | 1f7df35 | 2002-03-29 03:29:08 +0000 | [diff] [blame] | 691 | (hash = ((PyStringObject *) key)->ob_shash) == -1) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 692 | hash = PyObject_Hash(key); |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 693 | if (hash == -1) |
| 694 | return -1; |
| 695 | } |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 696 | mp = (PyDictObject *)op; |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 697 | ep = (mp->ma_lookup)(mp, key, hash); |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 698 | if (ep == NULL) |
| 699 | return -1; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 700 | if (ep->me_value == NULL) { |
Georg Brandl | b9f4ad3 | 2006-10-29 18:31:42 +0000 | [diff] [blame] | 701 | set_key_error(key); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 702 | return -1; |
| 703 | } |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 704 | old_key = ep->me_key; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 705 | Py_INCREF(dummy); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 706 | ep->me_key = dummy; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 707 | old_value = ep->me_value; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 708 | ep->me_value = NULL; |
| 709 | mp->ma_used--; |
Tim Peters | ea8f2bf | 2000-12-13 01:02:46 +0000 | [diff] [blame] | 710 | Py_DECREF(old_value); |
| 711 | Py_DECREF(old_key); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 712 | return 0; |
| 713 | } |
| 714 | |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 715 | void |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 716 | PyDict_Clear(PyObject *op) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 717 | { |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 718 | PyDictObject *mp; |
| 719 | PyDictEntry *ep, *table; |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 720 | int table_is_malloced; |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 721 | Py_ssize_t fill; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 722 | PyDictEntry small_copy[PyDict_MINSIZE]; |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 723 | #ifdef Py_DEBUG |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 724 | Py_ssize_t i, n; |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 725 | #endif |
| 726 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 727 | if (!PyDict_Check(op)) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 728 | return; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 729 | mp = (PyDictObject *)op; |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 730 | #ifdef Py_DEBUG |
Tim Peters | afb6ae8 | 2001-06-04 21:00:21 +0000 | [diff] [blame] | 731 | n = mp->ma_mask + 1; |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 732 | i = 0; |
| 733 | #endif |
| 734 | |
| 735 | table = mp->ma_table; |
| 736 | assert(table != NULL); |
| 737 | table_is_malloced = table != mp->ma_smalltable; |
| 738 | |
| 739 | /* This is delicate. During the process of clearing the dict, |
| 740 | * decrefs can cause the dict to mutate. To avoid fatal confusion |
| 741 | * (voice of experience), we have to make the dict empty before |
| 742 | * clearing the slots, and never refer to anything via mp->xxx while |
| 743 | * clearing. |
| 744 | */ |
| 745 | fill = mp->ma_fill; |
| 746 | if (table_is_malloced) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 747 | EMPTY_TO_MINSIZE(mp); |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 748 | |
| 749 | else if (fill > 0) { |
| 750 | /* It's a small table with something that needs to be cleared. |
| 751 | * Afraid the only safe way is to copy the dict entries into |
| 752 | * another small table first. |
| 753 | */ |
| 754 | memcpy(small_copy, table, sizeof(small_copy)); |
| 755 | table = small_copy; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 756 | EMPTY_TO_MINSIZE(mp); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 757 | } |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 758 | /* else it's a small table that's already empty */ |
| 759 | |
| 760 | /* Now we can finally clear things. If C had refcounts, we could |
| 761 | * assert that the refcount on table is 1 now, i.e. that this function |
| 762 | * has unique access to it, so decref side-effects can't alter it. |
| 763 | */ |
| 764 | for (ep = table; fill > 0; ++ep) { |
| 765 | #ifdef Py_DEBUG |
| 766 | assert(i < n); |
| 767 | ++i; |
| 768 | #endif |
| 769 | if (ep->me_key) { |
| 770 | --fill; |
| 771 | Py_DECREF(ep->me_key); |
| 772 | Py_XDECREF(ep->me_value); |
| 773 | } |
| 774 | #ifdef Py_DEBUG |
| 775 | else |
| 776 | assert(ep->me_value == NULL); |
| 777 | #endif |
| 778 | } |
| 779 | |
| 780 | if (table_is_malloced) |
| 781 | PyMem_DEL(table); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 782 | } |
| 783 | |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 784 | /* |
| 785 | * Iterate over a dict. Use like so: |
| 786 | * |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 787 | * Py_ssize_t i; |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 788 | * PyObject *key, *value; |
| 789 | * i = 0; # important! i should not otherwise be changed by you |
Neal Norwitz | 0732301 | 2003-02-15 14:45:12 +0000 | [diff] [blame] | 790 | * while (PyDict_Next(yourdict, &i, &key, &value)) { |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 791 | * Refer to borrowed references in key and value. |
| 792 | * } |
| 793 | * |
| 794 | * CAUTION: In general, it isn't safe to use PyDict_Next in a loop that |
Tim Peters | 6783070 | 2001-03-21 19:23:56 +0000 | [diff] [blame] | 795 | * mutates the dict. One exception: it is safe if the loop merely changes |
| 796 | * the values associated with the keys (but doesn't insert new keys or |
| 797 | * delete keys), via PyDict_SetItem(). |
| 798 | */ |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 799 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 800 | PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 801 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 802 | register Py_ssize_t i; |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 803 | register Py_ssize_t mask; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 804 | register PyDictEntry *ep; |
Raymond Hettinger | 4344278 | 2004-03-17 21:55:03 +0000 | [diff] [blame] | 805 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 806 | if (!PyDict_Check(op)) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 807 | return 0; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 808 | i = *ppos; |
| 809 | if (i < 0) |
| 810 | return 0; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 811 | ep = ((PyDictObject *)op)->ma_table; |
| 812 | mask = ((PyDictObject *)op)->ma_mask; |
Raymond Hettinger | 4344278 | 2004-03-17 21:55:03 +0000 | [diff] [blame] | 813 | while (i <= mask && ep[i].me_value == NULL) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 814 | i++; |
| 815 | *ppos = i+1; |
Raymond Hettinger | 4344278 | 2004-03-17 21:55:03 +0000 | [diff] [blame] | 816 | if (i > mask) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 817 | return 0; |
| 818 | if (pkey) |
Raymond Hettinger | 4344278 | 2004-03-17 21:55:03 +0000 | [diff] [blame] | 819 | *pkey = ep[i].me_key; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 820 | if (pvalue) |
Raymond Hettinger | 4344278 | 2004-03-17 21:55:03 +0000 | [diff] [blame] | 821 | *pvalue = ep[i].me_value; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 822 | return 1; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 823 | } |
| 824 | |
Raymond Hettinger | d6fc72a | 2007-02-19 02:03:19 +0000 | [diff] [blame] | 825 | /* Internal version of PyDict_Next that returns a hash value in addition to the key and value.*/ |
| 826 | int |
| 827 | _PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue, long *phash) |
| 828 | { |
| 829 | register Py_ssize_t i; |
| 830 | register Py_ssize_t mask; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 831 | register PyDictEntry *ep; |
Raymond Hettinger | d6fc72a | 2007-02-19 02:03:19 +0000 | [diff] [blame] | 832 | |
| 833 | if (!PyDict_Check(op)) |
| 834 | return 0; |
| 835 | i = *ppos; |
| 836 | if (i < 0) |
| 837 | return 0; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 838 | ep = ((PyDictObject *)op)->ma_table; |
| 839 | mask = ((PyDictObject *)op)->ma_mask; |
Raymond Hettinger | d6fc72a | 2007-02-19 02:03:19 +0000 | [diff] [blame] | 840 | while (i <= mask && ep[i].me_value == NULL) |
| 841 | i++; |
| 842 | *ppos = i+1; |
| 843 | if (i > mask) |
| 844 | return 0; |
| 845 | *phash = (long)(ep[i].me_hash); |
| 846 | if (pkey) |
| 847 | *pkey = ep[i].me_key; |
| 848 | if (pvalue) |
| 849 | *pvalue = ep[i].me_value; |
| 850 | return 1; |
| 851 | } |
| 852 | |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 853 | /* Methods */ |
| 854 | |
| 855 | static void |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 856 | dict_dealloc(register PyDictObject *mp) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 857 | { |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 858 | register PyDictEntry *ep; |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 859 | Py_ssize_t fill = mp->ma_fill; |
Guido van Rossum | ff413af | 2002-03-28 20:34:59 +0000 | [diff] [blame] | 860 | PyObject_GC_UnTrack(mp); |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 861 | Py_TRASHCAN_SAFE_BEGIN(mp) |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 862 | for (ep = mp->ma_table; fill > 0; ep++) { |
| 863 | if (ep->me_key) { |
| 864 | --fill; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 865 | Py_DECREF(ep->me_key); |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 866 | Py_XDECREF(ep->me_value); |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 867 | } |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 868 | } |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 869 | if (mp->ma_table != mp->ma_smalltable) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 870 | PyMem_DEL(mp->ma_table); |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame^] | 871 | if (num_free_dicts < MAXFREEDICTS && Py_TYPE(mp) == &PyDict_Type) |
Raymond Hettinger | 4344278 | 2004-03-17 21:55:03 +0000 | [diff] [blame] | 872 | free_dicts[num_free_dicts++] = mp; |
Tim Peters | 60b2996 | 2006-01-01 01:19:23 +0000 | [diff] [blame] | 873 | else |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame^] | 874 | Py_TYPE(mp)->tp_free((PyObject *)mp); |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 875 | Py_TRASHCAN_SAFE_END(mp) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | static int |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 879 | dict_print(register PyDictObject *mp, register FILE *fp, register int flags) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 880 | { |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 881 | register Py_ssize_t i; |
| 882 | register Py_ssize_t any; |
Tim Peters | 33f4a6a | 2006-05-30 05:23:59 +0000 | [diff] [blame] | 883 | int status; |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 884 | |
Tim Peters | 33f4a6a | 2006-05-30 05:23:59 +0000 | [diff] [blame] | 885 | status = Py_ReprEnter((PyObject*)mp); |
| 886 | if (status != 0) { |
| 887 | if (status < 0) |
| 888 | return status; |
Brett Cannon | 0153159 | 2007-09-17 03:28:34 +0000 | [diff] [blame] | 889 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 890 | fprintf(fp, "{...}"); |
Brett Cannon | 0153159 | 2007-09-17 03:28:34 +0000 | [diff] [blame] | 891 | Py_END_ALLOW_THREADS |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 892 | return 0; |
| 893 | } |
| 894 | |
Brett Cannon | 0153159 | 2007-09-17 03:28:34 +0000 | [diff] [blame] | 895 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 896 | fprintf(fp, "{"); |
Brett Cannon | 0153159 | 2007-09-17 03:28:34 +0000 | [diff] [blame] | 897 | Py_END_ALLOW_THREADS |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 898 | any = 0; |
Tim Peters | afb6ae8 | 2001-06-04 21:00:21 +0000 | [diff] [blame] | 899 | for (i = 0; i <= mp->ma_mask; i++) { |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 900 | PyDictEntry *ep = mp->ma_table + i; |
Tim Peters | 23cf6be | 2001-06-02 08:02:56 +0000 | [diff] [blame] | 901 | PyObject *pvalue = ep->me_value; |
| 902 | if (pvalue != NULL) { |
| 903 | /* Prevent PyObject_Repr from deleting value during |
| 904 | key format */ |
| 905 | Py_INCREF(pvalue); |
Brett Cannon | 0153159 | 2007-09-17 03:28:34 +0000 | [diff] [blame] | 906 | if (any++ > 0) { |
| 907 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 908 | fprintf(fp, ", "); |
Brett Cannon | 0153159 | 2007-09-17 03:28:34 +0000 | [diff] [blame] | 909 | Py_END_ALLOW_THREADS |
| 910 | } |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 911 | if (PyObject_Print((PyObject *)ep->me_key, fp, 0)!=0) { |
Tim Peters | 23cf6be | 2001-06-02 08:02:56 +0000 | [diff] [blame] | 912 | Py_DECREF(pvalue); |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 913 | Py_ReprLeave((PyObject*)mp); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 914 | return -1; |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 915 | } |
Brett Cannon | 0153159 | 2007-09-17 03:28:34 +0000 | [diff] [blame] | 916 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 917 | fprintf(fp, ": "); |
Brett Cannon | 0153159 | 2007-09-17 03:28:34 +0000 | [diff] [blame] | 918 | Py_END_ALLOW_THREADS |
Tim Peters | 19b77cf | 2001-06-02 08:27:39 +0000 | [diff] [blame] | 919 | if (PyObject_Print(pvalue, fp, 0) != 0) { |
Tim Peters | 23cf6be | 2001-06-02 08:02:56 +0000 | [diff] [blame] | 920 | Py_DECREF(pvalue); |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 921 | Py_ReprLeave((PyObject*)mp); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 922 | return -1; |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 923 | } |
Tim Peters | 23cf6be | 2001-06-02 08:02:56 +0000 | [diff] [blame] | 924 | Py_DECREF(pvalue); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 925 | } |
| 926 | } |
Brett Cannon | 0153159 | 2007-09-17 03:28:34 +0000 | [diff] [blame] | 927 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 928 | fprintf(fp, "}"); |
Brett Cannon | 0153159 | 2007-09-17 03:28:34 +0000 | [diff] [blame] | 929 | Py_END_ALLOW_THREADS |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 930 | Py_ReprLeave((PyObject*)mp); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 931 | return 0; |
| 932 | } |
| 933 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 934 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 935 | dict_repr(PyDictObject *mp) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 936 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 937 | Py_ssize_t i; |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 938 | PyObject *s, *temp, *colon = NULL; |
| 939 | PyObject *pieces = NULL, *result = NULL; |
| 940 | PyObject *key, *value; |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 941 | |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 942 | i = Py_ReprEnter((PyObject *)mp); |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 943 | if (i != 0) { |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 944 | return i > 0 ? PyString_FromString("{...}") : NULL; |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 945 | } |
| 946 | |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 947 | if (mp->ma_used == 0) { |
| 948 | result = PyString_FromString("{}"); |
| 949 | goto Done; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 950 | } |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 951 | |
| 952 | pieces = PyList_New(0); |
| 953 | if (pieces == NULL) |
| 954 | goto Done; |
| 955 | |
| 956 | colon = PyString_FromString(": "); |
| 957 | if (colon == NULL) |
| 958 | goto Done; |
| 959 | |
| 960 | /* Do repr() on each key+value pair, and insert ": " between them. |
| 961 | Note that repr may mutate the dict. */ |
Tim Peters | c605784 | 2001-06-16 07:52:53 +0000 | [diff] [blame] | 962 | i = 0; |
| 963 | while (PyDict_Next((PyObject *)mp, &i, &key, &value)) { |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 964 | int status; |
| 965 | /* Prevent repr from deleting value during key format. */ |
| 966 | Py_INCREF(value); |
| 967 | s = PyObject_Repr(key); |
| 968 | PyString_Concat(&s, colon); |
| 969 | PyString_ConcatAndDel(&s, PyObject_Repr(value)); |
| 970 | Py_DECREF(value); |
| 971 | if (s == NULL) |
| 972 | goto Done; |
| 973 | status = PyList_Append(pieces, s); |
| 974 | Py_DECREF(s); /* append created a new ref */ |
| 975 | if (status < 0) |
| 976 | goto Done; |
| 977 | } |
| 978 | |
| 979 | /* Add "{}" decorations to the first and last items. */ |
| 980 | assert(PyList_GET_SIZE(pieces) > 0); |
| 981 | s = PyString_FromString("{"); |
| 982 | if (s == NULL) |
| 983 | goto Done; |
| 984 | temp = PyList_GET_ITEM(pieces, 0); |
| 985 | PyString_ConcatAndDel(&s, temp); |
| 986 | PyList_SET_ITEM(pieces, 0, s); |
| 987 | if (s == NULL) |
| 988 | goto Done; |
| 989 | |
| 990 | s = PyString_FromString("}"); |
| 991 | if (s == NULL) |
| 992 | goto Done; |
| 993 | temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1); |
| 994 | PyString_ConcatAndDel(&temp, s); |
| 995 | PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp); |
| 996 | if (temp == NULL) |
| 997 | goto Done; |
| 998 | |
| 999 | /* Paste them all together with ", " between. */ |
| 1000 | s = PyString_FromString(", "); |
| 1001 | if (s == NULL) |
| 1002 | goto Done; |
| 1003 | result = _PyString_Join(s, pieces); |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 1004 | Py_DECREF(s); |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1005 | |
| 1006 | Done: |
| 1007 | Py_XDECREF(pieces); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1008 | Py_XDECREF(colon); |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1009 | Py_ReprLeave((PyObject *)mp); |
| 1010 | return result; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1013 | static Py_ssize_t |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1014 | dict_length(PyDictObject *mp) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1015 | { |
| 1016 | return mp->ma_used; |
| 1017 | } |
| 1018 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1019 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1020 | dict_subscript(PyDictObject *mp, register PyObject *key) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1021 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1022 | PyObject *v; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 1023 | long hash; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1024 | PyDictEntry *ep; |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 1025 | assert(mp->ma_table != NULL); |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 1026 | if (!PyString_CheckExact(key) || |
Tim Peters | 1f7df35 | 2002-03-29 03:29:08 +0000 | [diff] [blame] | 1027 | (hash = ((PyStringObject *) key)->ob_shash) == -1) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1028 | hash = PyObject_Hash(key); |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 1029 | if (hash == -1) |
| 1030 | return NULL; |
| 1031 | } |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 1032 | ep = (mp->ma_lookup)(mp, key, hash); |
| 1033 | if (ep == NULL) |
| 1034 | return NULL; |
| 1035 | v = ep->me_value; |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1036 | if (v == NULL) { |
| 1037 | if (!PyDict_CheckExact(mp)) { |
| 1038 | /* Look up __missing__ method if we're a subclass. */ |
Guido van Rossum | 4b92a82 | 2006-02-25 23:32:30 +0000 | [diff] [blame] | 1039 | PyObject *missing; |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1040 | static PyObject *missing_str = NULL; |
| 1041 | if (missing_str == NULL) |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 1042 | missing_str = |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1043 | PyString_InternFromString("__missing__"); |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame^] | 1044 | missing = _PyType_Lookup(Py_TYPE(mp), missing_str); |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1045 | if (missing != NULL) |
| 1046 | return PyObject_CallFunctionObjArgs(missing, |
| 1047 | (PyObject *)mp, key, NULL); |
| 1048 | } |
Georg Brandl | b9f4ad3 | 2006-10-29 18:31:42 +0000 | [diff] [blame] | 1049 | set_key_error(key); |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1050 | return NULL; |
| 1051 | } |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1052 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1053 | Py_INCREF(v); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1054 | return v; |
| 1055 | } |
| 1056 | |
| 1057 | static int |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1058 | dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1059 | { |
| 1060 | if (w == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1061 | return PyDict_DelItem((PyObject *)mp, v); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1062 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1063 | return PyDict_SetItem((PyObject *)mp, v, w); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 1066 | static PyMappingMethods dict_as_mapping = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1067 | (lenfunc)dict_length, /*mp_length*/ |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 1068 | (binaryfunc)dict_subscript, /*mp_subscript*/ |
| 1069 | (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1070 | }; |
| 1071 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1072 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1073 | dict_keys(register PyDictObject *mp) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1074 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1075 | register PyObject *v; |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 1076 | register Py_ssize_t i, j; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1077 | PyDictEntry *ep; |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 1078 | Py_ssize_t mask, n; |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1079 | |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1080 | again: |
| 1081 | n = mp->ma_used; |
| 1082 | v = PyList_New(n); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1083 | if (v == NULL) |
| 1084 | return NULL; |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1085 | if (n != mp->ma_used) { |
| 1086 | /* Durnit. The allocations caused the dict to resize. |
| 1087 | * Just start over, this shouldn't normally happen. |
| 1088 | */ |
| 1089 | Py_DECREF(v); |
| 1090 | goto again; |
| 1091 | } |
Raymond Hettinger | 4344278 | 2004-03-17 21:55:03 +0000 | [diff] [blame] | 1092 | ep = mp->ma_table; |
| 1093 | mask = mp->ma_mask; |
| 1094 | for (i = 0, j = 0; i <= mask; i++) { |
| 1095 | if (ep[i].me_value != NULL) { |
| 1096 | PyObject *key = ep[i].me_key; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1097 | Py_INCREF(key); |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1098 | PyList_SET_ITEM(v, j, key); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1099 | j++; |
| 1100 | } |
| 1101 | } |
Raymond Hettinger | 4344278 | 2004-03-17 21:55:03 +0000 | [diff] [blame] | 1102 | assert(j == n); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1103 | return v; |
| 1104 | } |
| 1105 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1106 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1107 | dict_values(register PyDictObject *mp) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1108 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1109 | register PyObject *v; |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 1110 | register Py_ssize_t i, j; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1111 | PyDictEntry *ep; |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 1112 | Py_ssize_t mask, n; |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1113 | |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1114 | again: |
| 1115 | n = mp->ma_used; |
| 1116 | v = PyList_New(n); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1117 | if (v == NULL) |
| 1118 | return NULL; |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1119 | if (n != mp->ma_used) { |
| 1120 | /* Durnit. The allocations caused the dict to resize. |
| 1121 | * Just start over, this shouldn't normally happen. |
| 1122 | */ |
| 1123 | Py_DECREF(v); |
| 1124 | goto again; |
| 1125 | } |
Raymond Hettinger | 4344278 | 2004-03-17 21:55:03 +0000 | [diff] [blame] | 1126 | ep = mp->ma_table; |
| 1127 | mask = mp->ma_mask; |
| 1128 | for (i = 0, j = 0; i <= mask; i++) { |
| 1129 | if (ep[i].me_value != NULL) { |
| 1130 | PyObject *value = ep[i].me_value; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1131 | Py_INCREF(value); |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1132 | PyList_SET_ITEM(v, j, value); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1133 | j++; |
| 1134 | } |
| 1135 | } |
Raymond Hettinger | 4344278 | 2004-03-17 21:55:03 +0000 | [diff] [blame] | 1136 | assert(j == n); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1137 | return v; |
| 1138 | } |
| 1139 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1140 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1141 | dict_items(register PyDictObject *mp) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1142 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1143 | register PyObject *v; |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 1144 | register Py_ssize_t i, j, n; |
| 1145 | Py_ssize_t mask; |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1146 | PyObject *item, *key, *value; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1147 | PyDictEntry *ep; |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1148 | |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1149 | /* Preallocate the list of tuples, to avoid allocations during |
| 1150 | * the loop over the items, which could trigger GC, which |
| 1151 | * could resize the dict. :-( |
| 1152 | */ |
| 1153 | again: |
| 1154 | n = mp->ma_used; |
| 1155 | v = PyList_New(n); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1156 | if (v == NULL) |
| 1157 | return NULL; |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1158 | for (i = 0; i < n; i++) { |
| 1159 | item = PyTuple_New(2); |
| 1160 | if (item == NULL) { |
| 1161 | Py_DECREF(v); |
| 1162 | return NULL; |
| 1163 | } |
| 1164 | PyList_SET_ITEM(v, i, item); |
| 1165 | } |
| 1166 | if (n != mp->ma_used) { |
| 1167 | /* Durnit. The allocations caused the dict to resize. |
| 1168 | * Just start over, this shouldn't normally happen. |
| 1169 | */ |
| 1170 | Py_DECREF(v); |
| 1171 | goto again; |
| 1172 | } |
| 1173 | /* Nothing we do below makes any function calls. */ |
Raymond Hettinger | 4344278 | 2004-03-17 21:55:03 +0000 | [diff] [blame] | 1174 | ep = mp->ma_table; |
| 1175 | mask = mp->ma_mask; |
| 1176 | for (i = 0, j = 0; i <= mask; i++) { |
Raymond Hettinger | 0690512 | 2004-03-19 10:30:00 +0000 | [diff] [blame] | 1177 | if ((value=ep[i].me_value) != NULL) { |
Raymond Hettinger | 4344278 | 2004-03-17 21:55:03 +0000 | [diff] [blame] | 1178 | key = ep[i].me_key; |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1179 | item = PyList_GET_ITEM(v, j); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1180 | Py_INCREF(key); |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1181 | PyTuple_SET_ITEM(item, 0, key); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1182 | Py_INCREF(value); |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1183 | PyTuple_SET_ITEM(item, 1, value); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1184 | j++; |
| 1185 | } |
| 1186 | } |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1187 | assert(j == n); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1188 | return v; |
| 1189 | } |
| 1190 | |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 1191 | static PyObject * |
Tim Peters | bca1cbc | 2002-12-09 22:56:13 +0000 | [diff] [blame] | 1192 | dict_fromkeys(PyObject *cls, PyObject *args) |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 1193 | { |
| 1194 | PyObject *seq; |
| 1195 | PyObject *value = Py_None; |
| 1196 | PyObject *it; /* iter(seq) */ |
| 1197 | PyObject *key; |
| 1198 | PyObject *d; |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 1199 | int status; |
| 1200 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 1201 | if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value)) |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 1202 | return NULL; |
| 1203 | |
| 1204 | d = PyObject_CallObject(cls, NULL); |
| 1205 | if (d == NULL) |
| 1206 | return NULL; |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 1207 | |
Raymond Hettinger | cdcf887 | 2007-11-07 02:26:17 +0000 | [diff] [blame] | 1208 | if (PyDict_CheckExact(d) && PyDict_CheckExact(seq)) { |
| 1209 | PyDictObject *mp = (PyDictObject *)d; |
| 1210 | PyObject *oldvalue; |
| 1211 | Py_ssize_t pos = 0; |
| 1212 | PyObject *key; |
| 1213 | long hash; |
| 1214 | |
Raymond Hettinger | 3444879 | 2007-11-09 23:14:44 +0000 | [diff] [blame] | 1215 | if (dictresize(mp, PySet_GET_SIZE(seq))) |
Raymond Hettinger | cdcf887 | 2007-11-07 02:26:17 +0000 | [diff] [blame] | 1216 | return NULL; |
| 1217 | |
| 1218 | while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) { |
| 1219 | Py_INCREF(key); |
| 1220 | Py_INCREF(value); |
| 1221 | if (insertdict(mp, key, hash, value)) |
| 1222 | return NULL; |
| 1223 | } |
| 1224 | return d; |
| 1225 | } |
| 1226 | |
Raymond Hettinger | 0bbbfc4 | 2007-03-20 21:27:24 +0000 | [diff] [blame] | 1227 | if (PyDict_CheckExact(d) && PyAnySet_CheckExact(seq)) { |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1228 | PyDictObject *mp = (PyDictObject *)d; |
Raymond Hettinger | 0bbbfc4 | 2007-03-20 21:27:24 +0000 | [diff] [blame] | 1229 | Py_ssize_t pos = 0; |
| 1230 | PyObject *key; |
| 1231 | long hash; |
| 1232 | |
| 1233 | if (dictresize(mp, PySet_GET_SIZE(seq))) |
| 1234 | return NULL; |
| 1235 | |
| 1236 | while (_PySet_NextEntry(seq, &pos, &key, &hash)) { |
| 1237 | Py_INCREF(key); |
Raymond Hettinger | e3146f5 | 2007-03-21 20:33:57 +0000 | [diff] [blame] | 1238 | Py_INCREF(value); |
| 1239 | if (insertdict(mp, key, hash, value)) |
Raymond Hettinger | 0bbbfc4 | 2007-03-20 21:27:24 +0000 | [diff] [blame] | 1240 | return NULL; |
| 1241 | } |
| 1242 | return d; |
| 1243 | } |
| 1244 | |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 1245 | it = PyObject_GetIter(seq); |
| 1246 | if (it == NULL){ |
| 1247 | Py_DECREF(d); |
| 1248 | return NULL; |
| 1249 | } |
| 1250 | |
Raymond Hettinger | 3444879 | 2007-11-09 23:14:44 +0000 | [diff] [blame] | 1251 | if (PyDict_CheckExact(d)) { |
| 1252 | while ((key = PyIter_Next(it)) != NULL) { |
| 1253 | status = PyDict_SetItem(d, key, value); |
| 1254 | Py_DECREF(key); |
| 1255 | if (status < 0) |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 1256 | goto Fail; |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 1257 | } |
Raymond Hettinger | 3444879 | 2007-11-09 23:14:44 +0000 | [diff] [blame] | 1258 | } else { |
| 1259 | while ((key = PyIter_Next(it)) != NULL) { |
| 1260 | status = PyObject_SetItem(d, key, value); |
| 1261 | Py_DECREF(key); |
| 1262 | if (status < 0) |
| 1263 | goto Fail; |
| 1264 | } |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 1265 | } |
| 1266 | |
Raymond Hettinger | 3444879 | 2007-11-09 23:14:44 +0000 | [diff] [blame] | 1267 | if (PyErr_Occurred()) |
| 1268 | goto Fail; |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 1269 | Py_DECREF(it); |
| 1270 | return d; |
| 1271 | |
| 1272 | Fail: |
| 1273 | Py_DECREF(it); |
| 1274 | Py_DECREF(d); |
| 1275 | return NULL; |
| 1276 | } |
| 1277 | |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 1278 | static int |
| 1279 | dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname) |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 1280 | { |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 1281 | PyObject *arg = NULL; |
| 1282 | int result = 0; |
| 1283 | |
| 1284 | if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg)) |
| 1285 | result = -1; |
| 1286 | |
| 1287 | else if (arg != NULL) { |
| 1288 | if (PyObject_HasAttrString(arg, "keys")) |
| 1289 | result = PyDict_Merge(self, arg, 1); |
| 1290 | else |
| 1291 | result = PyDict_MergeFromSeq2(self, arg, 1); |
| 1292 | } |
| 1293 | if (result == 0 && kwds != NULL) |
| 1294 | result = PyDict_Merge(self, kwds, 1); |
| 1295 | return result; |
| 1296 | } |
| 1297 | |
| 1298 | static PyObject * |
| 1299 | dict_update(PyObject *self, PyObject *args, PyObject *kwds) |
| 1300 | { |
Raymond Hettinger | 7892b1c | 2004-04-12 18:10:01 +0000 | [diff] [blame] | 1301 | if (dict_update_common(self, args, kwds, "update") != -1) |
| 1302 | Py_RETURN_NONE; |
| 1303 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1304 | } |
| 1305 | |
Guido van Rossum | 05ac6de | 2001-08-10 20:28:28 +0000 | [diff] [blame] | 1306 | /* Update unconditionally replaces existing items. |
| 1307 | Merge has a 3rd argument 'override'; if set, it acts like Update, |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1308 | otherwise it leaves existing items unchanged. |
| 1309 | |
| 1310 | PyDict_{Update,Merge} update/merge from a mapping object. |
| 1311 | |
Tim Peters | f582b82 | 2001-12-11 18:51:08 +0000 | [diff] [blame] | 1312 | PyDict_MergeFromSeq2 updates/merges from any iterable object |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1313 | producing iterable objects of length 2. |
| 1314 | */ |
| 1315 | |
Tim Peters | f582b82 | 2001-12-11 18:51:08 +0000 | [diff] [blame] | 1316 | int |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1317 | PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override) |
| 1318 | { |
| 1319 | PyObject *it; /* iter(seq2) */ |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 1320 | Py_ssize_t i; /* index into seq2 of current element */ |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1321 | PyObject *item; /* seq2[i] */ |
| 1322 | PyObject *fast; /* item as a 2-tuple or 2-list */ |
| 1323 | |
| 1324 | assert(d != NULL); |
| 1325 | assert(PyDict_Check(d)); |
| 1326 | assert(seq2 != NULL); |
| 1327 | |
| 1328 | it = PyObject_GetIter(seq2); |
| 1329 | if (it == NULL) |
| 1330 | return -1; |
| 1331 | |
| 1332 | for (i = 0; ; ++i) { |
| 1333 | PyObject *key, *value; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1334 | Py_ssize_t n; |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1335 | |
| 1336 | fast = NULL; |
| 1337 | item = PyIter_Next(it); |
| 1338 | if (item == NULL) { |
| 1339 | if (PyErr_Occurred()) |
| 1340 | goto Fail; |
| 1341 | break; |
| 1342 | } |
| 1343 | |
| 1344 | /* Convert item to sequence, and verify length 2. */ |
| 1345 | fast = PySequence_Fast(item, ""); |
| 1346 | if (fast == NULL) { |
| 1347 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 1348 | PyErr_Format(PyExc_TypeError, |
| 1349 | "cannot convert dictionary update " |
Neal Norwitz | d3881b0 | 2006-05-30 04:25:05 +0000 | [diff] [blame] | 1350 | "sequence element #%zd to a sequence", |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1351 | i); |
| 1352 | goto Fail; |
| 1353 | } |
| 1354 | n = PySequence_Fast_GET_SIZE(fast); |
| 1355 | if (n != 2) { |
| 1356 | PyErr_Format(PyExc_ValueError, |
Neal Norwitz | d3881b0 | 2006-05-30 04:25:05 +0000 | [diff] [blame] | 1357 | "dictionary update sequence element #%zd " |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 1358 | "has length %zd; 2 is required", |
Martin v. Löwis | e0e89f7 | 2006-02-16 06:59:22 +0000 | [diff] [blame] | 1359 | i, n); |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1360 | goto Fail; |
| 1361 | } |
| 1362 | |
| 1363 | /* Update/merge with this (key, value) pair. */ |
| 1364 | key = PySequence_Fast_GET_ITEM(fast, 0); |
| 1365 | value = PySequence_Fast_GET_ITEM(fast, 1); |
| 1366 | if (override || PyDict_GetItem(d, key) == NULL) { |
| 1367 | int status = PyDict_SetItem(d, key, value); |
| 1368 | if (status < 0) |
| 1369 | goto Fail; |
| 1370 | } |
| 1371 | Py_DECREF(fast); |
| 1372 | Py_DECREF(item); |
| 1373 | } |
| 1374 | |
| 1375 | i = 0; |
| 1376 | goto Return; |
| 1377 | Fail: |
| 1378 | Py_XDECREF(item); |
| 1379 | Py_XDECREF(fast); |
| 1380 | i = -1; |
| 1381 | Return: |
| 1382 | Py_DECREF(it); |
Neal Norwitz | d3881b0 | 2006-05-30 04:25:05 +0000 | [diff] [blame] | 1383 | return Py_SAFE_DOWNCAST(i, Py_ssize_t, int); |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1386 | int |
| 1387 | PyDict_Update(PyObject *a, PyObject *b) |
| 1388 | { |
Guido van Rossum | 05ac6de | 2001-08-10 20:28:28 +0000 | [diff] [blame] | 1389 | return PyDict_Merge(a, b, 1); |
| 1390 | } |
| 1391 | |
| 1392 | int |
| 1393 | PyDict_Merge(PyObject *a, PyObject *b, int override) |
| 1394 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1395 | register PyDictObject *mp, *other; |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 1396 | register Py_ssize_t i; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1397 | PyDictEntry *entry; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1398 | |
Barry Warsaw | 66a0d1d | 2001-06-26 20:08:32 +0000 | [diff] [blame] | 1399 | /* We accept for the argument either a concrete dictionary object, |
| 1400 | * or an abstract "mapping" object. For the former, we can do |
| 1401 | * things quite efficiently. For the latter, we only require that |
| 1402 | * PyMapping_Keys() and PyObject_GetItem() be supported. |
| 1403 | */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1404 | if (a == NULL || !PyDict_Check(a) || b == NULL) { |
| 1405 | PyErr_BadInternalCall(); |
| 1406 | return -1; |
| 1407 | } |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1408 | mp = (PyDictObject*)a; |
Raymond Hettinger | 0922d71 | 2007-02-07 20:08:22 +0000 | [diff] [blame] | 1409 | if (PyDict_CheckExact(b)) { |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1410 | other = (PyDictObject*)b; |
Barry Warsaw | 66a0d1d | 2001-06-26 20:08:32 +0000 | [diff] [blame] | 1411 | if (other == mp || other->ma_used == 0) |
| 1412 | /* a.update(a) or a.update({}); nothing to do */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1413 | return 0; |
Raymond Hettinger | 186e739 | 2005-05-14 18:08:25 +0000 | [diff] [blame] | 1414 | if (mp->ma_used == 0) |
| 1415 | /* Since the target dict is empty, PyDict_GetItem() |
| 1416 | * always returns NULL. Setting override to 1 |
| 1417 | * skips the unnecessary test. |
| 1418 | */ |
| 1419 | override = 1; |
Barry Warsaw | 66a0d1d | 2001-06-26 20:08:32 +0000 | [diff] [blame] | 1420 | /* Do one big resize at the start, rather than |
| 1421 | * incrementally resizing as we insert new items. Expect |
| 1422 | * that there will be no (or few) overlapping keys. |
| 1423 | */ |
| 1424 | if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) { |
Raymond Hettinger | c8d2290 | 2003-05-07 00:49:40 +0000 | [diff] [blame] | 1425 | if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1426 | return -1; |
Barry Warsaw | 66a0d1d | 2001-06-26 20:08:32 +0000 | [diff] [blame] | 1427 | } |
| 1428 | for (i = 0; i <= other->ma_mask; i++) { |
| 1429 | entry = &other->ma_table[i]; |
Guido van Rossum | 05ac6de | 2001-08-10 20:28:28 +0000 | [diff] [blame] | 1430 | if (entry->me_value != NULL && |
| 1431 | (override || |
| 1432 | PyDict_GetItem(a, entry->me_key) == NULL)) { |
Barry Warsaw | 66a0d1d | 2001-06-26 20:08:32 +0000 | [diff] [blame] | 1433 | Py_INCREF(entry->me_key); |
| 1434 | Py_INCREF(entry->me_value); |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 1435 | if (insertdict(mp, entry->me_key, |
| 1436 | (long)entry->me_hash, |
| 1437 | entry->me_value) != 0) |
| 1438 | return -1; |
Barry Warsaw | 66a0d1d | 2001-06-26 20:08:32 +0000 | [diff] [blame] | 1439 | } |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 1440 | } |
| 1441 | } |
Barry Warsaw | 66a0d1d | 2001-06-26 20:08:32 +0000 | [diff] [blame] | 1442 | else { |
| 1443 | /* Do it the generic, slower way */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1444 | PyObject *keys = PyMapping_Keys(b); |
Barry Warsaw | 66a0d1d | 2001-06-26 20:08:32 +0000 | [diff] [blame] | 1445 | PyObject *iter; |
| 1446 | PyObject *key, *value; |
| 1447 | int status; |
| 1448 | |
| 1449 | if (keys == NULL) |
| 1450 | /* Docstring says this is equivalent to E.keys() so |
| 1451 | * if E doesn't have a .keys() method we want |
| 1452 | * AttributeError to percolate up. Might as well |
| 1453 | * do the same for any other error. |
| 1454 | */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1455 | return -1; |
Barry Warsaw | 66a0d1d | 2001-06-26 20:08:32 +0000 | [diff] [blame] | 1456 | |
| 1457 | iter = PyObject_GetIter(keys); |
| 1458 | Py_DECREF(keys); |
| 1459 | if (iter == NULL) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1460 | return -1; |
Barry Warsaw | 66a0d1d | 2001-06-26 20:08:32 +0000 | [diff] [blame] | 1461 | |
| 1462 | for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) { |
Guido van Rossum | 05ac6de | 2001-08-10 20:28:28 +0000 | [diff] [blame] | 1463 | if (!override && PyDict_GetItem(a, key) != NULL) { |
| 1464 | Py_DECREF(key); |
| 1465 | continue; |
| 1466 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1467 | value = PyObject_GetItem(b, key); |
Barry Warsaw | 66a0d1d | 2001-06-26 20:08:32 +0000 | [diff] [blame] | 1468 | if (value == NULL) { |
| 1469 | Py_DECREF(iter); |
| 1470 | Py_DECREF(key); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1471 | return -1; |
Barry Warsaw | 66a0d1d | 2001-06-26 20:08:32 +0000 | [diff] [blame] | 1472 | } |
Guido van Rossum | 05ac6de | 2001-08-10 20:28:28 +0000 | [diff] [blame] | 1473 | status = PyDict_SetItem(a, key, value); |
Barry Warsaw | 66a0d1d | 2001-06-26 20:08:32 +0000 | [diff] [blame] | 1474 | Py_DECREF(key); |
| 1475 | Py_DECREF(value); |
| 1476 | if (status < 0) { |
| 1477 | Py_DECREF(iter); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1478 | return -1; |
Barry Warsaw | 66a0d1d | 2001-06-26 20:08:32 +0000 | [diff] [blame] | 1479 | } |
| 1480 | } |
| 1481 | Py_DECREF(iter); |
| 1482 | if (PyErr_Occurred()) |
| 1483 | /* Iterator completed, via error */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1484 | return -1; |
Barry Warsaw | 66a0d1d | 2001-06-26 20:08:32 +0000 | [diff] [blame] | 1485 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1486 | return 0; |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 1487 | } |
| 1488 | |
| 1489 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1490 | dict_copy(register PyDictObject *mp) |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 1491 | { |
Jeremy Hylton | a12c7a7 | 2000-03-30 22:27:31 +0000 | [diff] [blame] | 1492 | return PyDict_Copy((PyObject*)mp); |
| 1493 | } |
| 1494 | |
| 1495 | PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 1496 | PyDict_Copy(PyObject *o) |
Jeremy Hylton | a12c7a7 | 2000-03-30 22:27:31 +0000 | [diff] [blame] | 1497 | { |
Raymond Hettinger | ebedb2f | 2004-03-08 04:19:01 +0000 | [diff] [blame] | 1498 | PyObject *copy; |
Jeremy Hylton | a12c7a7 | 2000-03-30 22:27:31 +0000 | [diff] [blame] | 1499 | |
| 1500 | if (o == NULL || !PyDict_Check(o)) { |
| 1501 | PyErr_BadInternalCall(); |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 1502 | return NULL; |
Jeremy Hylton | a12c7a7 | 2000-03-30 22:27:31 +0000 | [diff] [blame] | 1503 | } |
Raymond Hettinger | ebedb2f | 2004-03-08 04:19:01 +0000 | [diff] [blame] | 1504 | copy = PyDict_New(); |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 1505 | if (copy == NULL) |
| 1506 | return NULL; |
Raymond Hettinger | ebedb2f | 2004-03-08 04:19:01 +0000 | [diff] [blame] | 1507 | if (PyDict_Merge(copy, o, 1) == 0) |
| 1508 | return copy; |
| 1509 | Py_DECREF(copy); |
Raymond Hettinger | 1356f78 | 2005-04-15 15:58:42 +0000 | [diff] [blame] | 1510 | return NULL; |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 1511 | } |
| 1512 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1513 | Py_ssize_t |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 1514 | PyDict_Size(PyObject *mp) |
Guido van Rossum | 4199fac | 1993-11-05 10:18:44 +0000 | [diff] [blame] | 1515 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1516 | if (mp == NULL || !PyDict_Check(mp)) { |
| 1517 | PyErr_BadInternalCall(); |
Jeremy Hylton | 7083bb7 | 2004-02-17 20:10:11 +0000 | [diff] [blame] | 1518 | return -1; |
Guido van Rossum | 4199fac | 1993-11-05 10:18:44 +0000 | [diff] [blame] | 1519 | } |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1520 | return ((PyDictObject *)mp)->ma_used; |
Guido van Rossum | 4199fac | 1993-11-05 10:18:44 +0000 | [diff] [blame] | 1521 | } |
| 1522 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1523 | PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 1524 | PyDict_Keys(PyObject *mp) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1525 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1526 | if (mp == NULL || !PyDict_Check(mp)) { |
| 1527 | PyErr_BadInternalCall(); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1528 | return NULL; |
| 1529 | } |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1530 | return dict_keys((PyDictObject *)mp); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1531 | } |
| 1532 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1533 | PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 1534 | PyDict_Values(PyObject *mp) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1535 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1536 | if (mp == NULL || !PyDict_Check(mp)) { |
| 1537 | PyErr_BadInternalCall(); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1538 | return NULL; |
| 1539 | } |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1540 | return dict_values((PyDictObject *)mp); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1541 | } |
| 1542 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1543 | PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 1544 | PyDict_Items(PyObject *mp) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1545 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1546 | if (mp == NULL || !PyDict_Check(mp)) { |
| 1547 | PyErr_BadInternalCall(); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1548 | return NULL; |
| 1549 | } |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1550 | return dict_items((PyDictObject *)mp); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1553 | /* Subroutine which returns the smallest key in a for which b's value |
| 1554 | is different or absent. The value is returned too, through the |
Tim Peters | 95bf939 | 2001-05-10 08:32:44 +0000 | [diff] [blame] | 1555 | pval argument. Both are NULL if no key in a is found for which b's status |
| 1556 | differs. The refcounts on (and only on) non-NULL *pval and function return |
| 1557 | values must be decremented by the caller (characterize() increments them |
| 1558 | to ensure that mutating comparison and PyDict_GetItem calls can't delete |
| 1559 | them before the caller is done looking at them). */ |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1560 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1561 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1562 | characterize(PyDictObject *a, PyDictObject *b, PyObject **pval) |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1563 | { |
Tim Peters | 95bf939 | 2001-05-10 08:32:44 +0000 | [diff] [blame] | 1564 | PyObject *akey = NULL; /* smallest key in a s.t. a[akey] != b[akey] */ |
| 1565 | PyObject *aval = NULL; /* a[akey] */ |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 1566 | Py_ssize_t i; |
| 1567 | int cmp; |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1568 | |
Tim Peters | afb6ae8 | 2001-06-04 21:00:21 +0000 | [diff] [blame] | 1569 | for (i = 0; i <= a->ma_mask; i++) { |
Tim Peters | 95bf939 | 2001-05-10 08:32:44 +0000 | [diff] [blame] | 1570 | PyObject *thiskey, *thisaval, *thisbval; |
| 1571 | if (a->ma_table[i].me_value == NULL) |
| 1572 | continue; |
| 1573 | thiskey = a->ma_table[i].me_key; |
| 1574 | Py_INCREF(thiskey); /* keep alive across compares */ |
| 1575 | if (akey != NULL) { |
| 1576 | cmp = PyObject_RichCompareBool(akey, thiskey, Py_LT); |
| 1577 | if (cmp < 0) { |
| 1578 | Py_DECREF(thiskey); |
| 1579 | goto Fail; |
| 1580 | } |
| 1581 | if (cmp > 0 || |
Tim Peters | afb6ae8 | 2001-06-04 21:00:21 +0000 | [diff] [blame] | 1582 | i > a->ma_mask || |
Tim Peters | 95bf939 | 2001-05-10 08:32:44 +0000 | [diff] [blame] | 1583 | a->ma_table[i].me_value == NULL) |
| 1584 | { |
| 1585 | /* Not the *smallest* a key; or maybe it is |
| 1586 | * but the compare shrunk the dict so we can't |
| 1587 | * find its associated value anymore; or |
| 1588 | * maybe it is but the compare deleted the |
| 1589 | * a[thiskey] entry. |
Neal Norwitz | d3881b0 | 2006-05-30 04:25:05 +0000 | [diff] [blame] | 1590 | */ |
Tim Peters | 95bf939 | 2001-05-10 08:32:44 +0000 | [diff] [blame] | 1591 | Py_DECREF(thiskey); |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1592 | continue; |
Guido van Rossum | b932420 | 2001-01-18 00:39:02 +0000 | [diff] [blame] | 1593 | } |
Tim Peters | 95bf939 | 2001-05-10 08:32:44 +0000 | [diff] [blame] | 1594 | } |
| 1595 | |
| 1596 | /* Compare a[thiskey] to b[thiskey]; cmp <- true iff equal. */ |
| 1597 | thisaval = a->ma_table[i].me_value; |
| 1598 | assert(thisaval); |
| 1599 | Py_INCREF(thisaval); /* keep alive */ |
| 1600 | thisbval = PyDict_GetItem((PyObject *)b, thiskey); |
| 1601 | if (thisbval == NULL) |
| 1602 | cmp = 0; |
| 1603 | else { |
| 1604 | /* both dicts have thiskey: same values? */ |
| 1605 | cmp = PyObject_RichCompareBool( |
| 1606 | thisaval, thisbval, Py_EQ); |
| 1607 | if (cmp < 0) { |
| 1608 | Py_DECREF(thiskey); |
| 1609 | Py_DECREF(thisaval); |
| 1610 | goto Fail; |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1611 | } |
| 1612 | } |
Tim Peters | 95bf939 | 2001-05-10 08:32:44 +0000 | [diff] [blame] | 1613 | if (cmp == 0) { |
| 1614 | /* New winner. */ |
| 1615 | Py_XDECREF(akey); |
| 1616 | Py_XDECREF(aval); |
| 1617 | akey = thiskey; |
| 1618 | aval = thisaval; |
| 1619 | } |
| 1620 | else { |
| 1621 | Py_DECREF(thiskey); |
| 1622 | Py_DECREF(thisaval); |
| 1623 | } |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1624 | } |
Tim Peters | 95bf939 | 2001-05-10 08:32:44 +0000 | [diff] [blame] | 1625 | *pval = aval; |
| 1626 | return akey; |
| 1627 | |
| 1628 | Fail: |
| 1629 | Py_XDECREF(akey); |
| 1630 | Py_XDECREF(aval); |
| 1631 | *pval = NULL; |
| 1632 | return NULL; |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1633 | } |
| 1634 | |
| 1635 | static int |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1636 | dict_compare(PyDictObject *a, PyDictObject *b) |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1637 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1638 | PyObject *adiff, *bdiff, *aval, *bval; |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1639 | int res; |
| 1640 | |
| 1641 | /* Compare lengths first */ |
| 1642 | if (a->ma_used < b->ma_used) |
| 1643 | return -1; /* a is shorter */ |
| 1644 | else if (a->ma_used > b->ma_used) |
| 1645 | return 1; /* b is shorter */ |
Tim Peters | 95bf939 | 2001-05-10 08:32:44 +0000 | [diff] [blame] | 1646 | |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1647 | /* Same length -- check all keys */ |
Tim Peters | 95bf939 | 2001-05-10 08:32:44 +0000 | [diff] [blame] | 1648 | bdiff = bval = NULL; |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1649 | adiff = characterize(a, b, &aval); |
Tim Peters | 95bf939 | 2001-05-10 08:32:44 +0000 | [diff] [blame] | 1650 | if (adiff == NULL) { |
| 1651 | assert(!aval); |
Tim Peters | 3918fb2 | 2001-05-10 18:58:31 +0000 | [diff] [blame] | 1652 | /* Either an error, or a is a subset with the same length so |
Tim Peters | 95bf939 | 2001-05-10 08:32:44 +0000 | [diff] [blame] | 1653 | * must be equal. |
| 1654 | */ |
| 1655 | res = PyErr_Occurred() ? -1 : 0; |
| 1656 | goto Finished; |
| 1657 | } |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1658 | bdiff = characterize(b, a, &bval); |
Tim Peters | 95bf939 | 2001-05-10 08:32:44 +0000 | [diff] [blame] | 1659 | if (bdiff == NULL && PyErr_Occurred()) { |
| 1660 | assert(!bval); |
| 1661 | res = -1; |
| 1662 | goto Finished; |
| 1663 | } |
| 1664 | res = 0; |
| 1665 | if (bdiff) { |
| 1666 | /* bdiff == NULL "should be" impossible now, but perhaps |
| 1667 | * the last comparison done by the characterize() on a had |
| 1668 | * the side effect of making the dicts equal! |
| 1669 | */ |
| 1670 | res = PyObject_Compare(adiff, bdiff); |
| 1671 | } |
| 1672 | if (res == 0 && bval != NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1673 | res = PyObject_Compare(aval, bval); |
Tim Peters | 95bf939 | 2001-05-10 08:32:44 +0000 | [diff] [blame] | 1674 | |
| 1675 | Finished: |
| 1676 | Py_XDECREF(adiff); |
| 1677 | Py_XDECREF(bdiff); |
| 1678 | Py_XDECREF(aval); |
| 1679 | Py_XDECREF(bval); |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1680 | return res; |
| 1681 | } |
| 1682 | |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 1683 | /* Return 1 if dicts equal, 0 if not, -1 if error. |
| 1684 | * Gets out as soon as any difference is detected. |
| 1685 | * Uses only Py_EQ comparison. |
| 1686 | */ |
| 1687 | static int |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1688 | dict_equal(PyDictObject *a, PyDictObject *b) |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 1689 | { |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 1690 | Py_ssize_t i; |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 1691 | |
| 1692 | if (a->ma_used != b->ma_used) |
| 1693 | /* can't be equal if # of entries differ */ |
| 1694 | return 0; |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 1695 | |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 1696 | /* Same # of entries -- check all of 'em. Exit early on any diff. */ |
Tim Peters | afb6ae8 | 2001-06-04 21:00:21 +0000 | [diff] [blame] | 1697 | for (i = 0; i <= a->ma_mask; i++) { |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 1698 | PyObject *aval = a->ma_table[i].me_value; |
| 1699 | if (aval != NULL) { |
| 1700 | int cmp; |
| 1701 | PyObject *bval; |
| 1702 | PyObject *key = a->ma_table[i].me_key; |
| 1703 | /* temporarily bump aval's refcount to ensure it stays |
| 1704 | alive until we're done with it */ |
| 1705 | Py_INCREF(aval); |
Neal Norwitz | a22975f | 2006-09-05 02:24:03 +0000 | [diff] [blame] | 1706 | /* ditto for key */ |
| 1707 | Py_INCREF(key); |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 1708 | bval = PyDict_GetItem((PyObject *)b, key); |
Neal Norwitz | a22975f | 2006-09-05 02:24:03 +0000 | [diff] [blame] | 1709 | Py_DECREF(key); |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 1710 | if (bval == NULL) { |
| 1711 | Py_DECREF(aval); |
| 1712 | return 0; |
| 1713 | } |
| 1714 | cmp = PyObject_RichCompareBool(aval, bval, Py_EQ); |
| 1715 | Py_DECREF(aval); |
| 1716 | if (cmp <= 0) /* error or not equal */ |
| 1717 | return cmp; |
| 1718 | } |
| 1719 | } |
| 1720 | return 1; |
| 1721 | } |
| 1722 | |
| 1723 | static PyObject * |
| 1724 | dict_richcompare(PyObject *v, PyObject *w, int op) |
| 1725 | { |
| 1726 | int cmp; |
| 1727 | PyObject *res; |
| 1728 | |
| 1729 | if (!PyDict_Check(v) || !PyDict_Check(w)) { |
| 1730 | res = Py_NotImplemented; |
| 1731 | } |
| 1732 | else if (op == Py_EQ || op == Py_NE) { |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1733 | cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w); |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 1734 | if (cmp < 0) |
| 1735 | return NULL; |
| 1736 | res = (cmp == (op == Py_EQ)) ? Py_True : Py_False; |
| 1737 | } |
Tim Peters | 4fa58bf | 2001-05-10 21:45:19 +0000 | [diff] [blame] | 1738 | else |
| 1739 | res = Py_NotImplemented; |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 1740 | Py_INCREF(res); |
| 1741 | return res; |
| 1742 | } |
| 1743 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1744 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1745 | dict_contains(register PyDictObject *mp, PyObject *key) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1746 | { |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1747 | long hash; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1748 | PyDictEntry *ep; |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 1749 | |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 1750 | if (!PyString_CheckExact(key) || |
Tim Peters | 1f7df35 | 2002-03-29 03:29:08 +0000 | [diff] [blame] | 1751 | (hash = ((PyStringObject *) key)->ob_shash) == -1) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1752 | hash = PyObject_Hash(key); |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 1753 | if (hash == -1) |
| 1754 | return NULL; |
| 1755 | } |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 1756 | ep = (mp->ma_lookup)(mp, key, hash); |
| 1757 | if (ep == NULL) |
| 1758 | return NULL; |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 1759 | return PyBool_FromLong(ep->me_value != NULL); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1760 | } |
| 1761 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1762 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1763 | dict_has_key(register PyDictObject *mp, PyObject *key) |
Neal Norwitz | 8b2bfbc | 2007-05-23 06:35:32 +0000 | [diff] [blame] | 1764 | { |
| 1765 | if (Py_Py3kWarningFlag && |
| 1766 | PyErr_Warn(PyExc_DeprecationWarning, |
| 1767 | "dict.has_key() not supported in 3.x") < 0) |
| 1768 | return NULL; |
| 1769 | return dict_contains(mp, key); |
| 1770 | } |
| 1771 | |
| 1772 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1773 | dict_get(register PyDictObject *mp, PyObject *args) |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 1774 | { |
| 1775 | PyObject *key; |
Barry Warsaw | 320ac33 | 1997-10-20 17:26:25 +0000 | [diff] [blame] | 1776 | PyObject *failobj = Py_None; |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 1777 | PyObject *val = NULL; |
| 1778 | long hash; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1779 | PyDictEntry *ep; |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 1780 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 1781 | if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj)) |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 1782 | return NULL; |
| 1783 | |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 1784 | if (!PyString_CheckExact(key) || |
Tim Peters | 1f7df35 | 2002-03-29 03:29:08 +0000 | [diff] [blame] | 1785 | (hash = ((PyStringObject *) key)->ob_shash) == -1) { |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 1786 | hash = PyObject_Hash(key); |
| 1787 | if (hash == -1) |
| 1788 | return NULL; |
| 1789 | } |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 1790 | ep = (mp->ma_lookup)(mp, key, hash); |
| 1791 | if (ep == NULL) |
| 1792 | return NULL; |
| 1793 | val = ep->me_value; |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 1794 | if (val == NULL) |
| 1795 | val = failobj; |
| 1796 | Py_INCREF(val); |
| 1797 | return val; |
| 1798 | } |
| 1799 | |
| 1800 | |
| 1801 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1802 | dict_setdefault(register PyDictObject *mp, PyObject *args) |
Guido van Rossum | 164452c | 2000-08-08 16:12:54 +0000 | [diff] [blame] | 1803 | { |
| 1804 | PyObject *key; |
| 1805 | PyObject *failobj = Py_None; |
| 1806 | PyObject *val = NULL; |
| 1807 | long hash; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1808 | PyDictEntry *ep; |
Guido van Rossum | 164452c | 2000-08-08 16:12:54 +0000 | [diff] [blame] | 1809 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 1810 | if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj)) |
Guido van Rossum | 164452c | 2000-08-08 16:12:54 +0000 | [diff] [blame] | 1811 | return NULL; |
Guido van Rossum | 164452c | 2000-08-08 16:12:54 +0000 | [diff] [blame] | 1812 | |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 1813 | if (!PyString_CheckExact(key) || |
Tim Peters | 1f7df35 | 2002-03-29 03:29:08 +0000 | [diff] [blame] | 1814 | (hash = ((PyStringObject *) key)->ob_shash) == -1) { |
Guido van Rossum | 164452c | 2000-08-08 16:12:54 +0000 | [diff] [blame] | 1815 | hash = PyObject_Hash(key); |
| 1816 | if (hash == -1) |
| 1817 | return NULL; |
| 1818 | } |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 1819 | ep = (mp->ma_lookup)(mp, key, hash); |
| 1820 | if (ep == NULL) |
| 1821 | return NULL; |
| 1822 | val = ep->me_value; |
Guido van Rossum | 164452c | 2000-08-08 16:12:54 +0000 | [diff] [blame] | 1823 | if (val == NULL) { |
| 1824 | val = failobj; |
| 1825 | if (PyDict_SetItem((PyObject*)mp, key, failobj)) |
| 1826 | val = NULL; |
| 1827 | } |
| 1828 | Py_XINCREF(val); |
| 1829 | return val; |
| 1830 | } |
| 1831 | |
| 1832 | |
| 1833 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1834 | dict_clear(register PyDictObject *mp) |
Guido van Rossum | fb8f1ca | 1997-03-21 21:55:12 +0000 | [diff] [blame] | 1835 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1836 | PyDict_Clear((PyObject *)mp); |
Raymond Hettinger | 7892b1c | 2004-04-12 18:10:01 +0000 | [diff] [blame] | 1837 | Py_RETURN_NONE; |
Guido van Rossum | fb8f1ca | 1997-03-21 21:55:12 +0000 | [diff] [blame] | 1838 | } |
| 1839 | |
Guido van Rossum | ba6ab84 | 2000-12-12 22:02:18 +0000 | [diff] [blame] | 1840 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1841 | dict_pop(PyDictObject *mp, PyObject *args) |
Guido van Rossum | e027d98 | 2002-04-12 15:11:59 +0000 | [diff] [blame] | 1842 | { |
| 1843 | long hash; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1844 | PyDictEntry *ep; |
Guido van Rossum | e027d98 | 2002-04-12 15:11:59 +0000 | [diff] [blame] | 1845 | PyObject *old_value, *old_key; |
Raymond Hettinger | a3e1e4c | 2003-03-06 23:54:28 +0000 | [diff] [blame] | 1846 | PyObject *key, *deflt = NULL; |
Guido van Rossum | e027d98 | 2002-04-12 15:11:59 +0000 | [diff] [blame] | 1847 | |
Raymond Hettinger | a3e1e4c | 2003-03-06 23:54:28 +0000 | [diff] [blame] | 1848 | if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt)) |
| 1849 | return NULL; |
Guido van Rossum | e027d98 | 2002-04-12 15:11:59 +0000 | [diff] [blame] | 1850 | if (mp->ma_used == 0) { |
Raymond Hettinger | a3e1e4c | 2003-03-06 23:54:28 +0000 | [diff] [blame] | 1851 | if (deflt) { |
| 1852 | Py_INCREF(deflt); |
| 1853 | return deflt; |
| 1854 | } |
Guido van Rossum | e027d98 | 2002-04-12 15:11:59 +0000 | [diff] [blame] | 1855 | PyErr_SetString(PyExc_KeyError, |
| 1856 | "pop(): dictionary is empty"); |
| 1857 | return NULL; |
| 1858 | } |
| 1859 | if (!PyString_CheckExact(key) || |
| 1860 | (hash = ((PyStringObject *) key)->ob_shash) == -1) { |
| 1861 | hash = PyObject_Hash(key); |
| 1862 | if (hash == -1) |
| 1863 | return NULL; |
| 1864 | } |
| 1865 | ep = (mp->ma_lookup)(mp, key, hash); |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 1866 | if (ep == NULL) |
| 1867 | return NULL; |
Guido van Rossum | e027d98 | 2002-04-12 15:11:59 +0000 | [diff] [blame] | 1868 | if (ep->me_value == NULL) { |
Raymond Hettinger | a3e1e4c | 2003-03-06 23:54:28 +0000 | [diff] [blame] | 1869 | if (deflt) { |
| 1870 | Py_INCREF(deflt); |
| 1871 | return deflt; |
| 1872 | } |
Georg Brandl | b9f4ad3 | 2006-10-29 18:31:42 +0000 | [diff] [blame] | 1873 | set_key_error(key); |
Guido van Rossum | e027d98 | 2002-04-12 15:11:59 +0000 | [diff] [blame] | 1874 | return NULL; |
| 1875 | } |
| 1876 | old_key = ep->me_key; |
| 1877 | Py_INCREF(dummy); |
| 1878 | ep->me_key = dummy; |
| 1879 | old_value = ep->me_value; |
| 1880 | ep->me_value = NULL; |
| 1881 | mp->ma_used--; |
| 1882 | Py_DECREF(old_key); |
| 1883 | return old_value; |
| 1884 | } |
| 1885 | |
| 1886 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1887 | dict_popitem(PyDictObject *mp) |
Guido van Rossum | ba6ab84 | 2000-12-12 22:02:18 +0000 | [diff] [blame] | 1888 | { |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 1889 | Py_ssize_t i = 0; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1890 | PyDictEntry *ep; |
Guido van Rossum | ba6ab84 | 2000-12-12 22:02:18 +0000 | [diff] [blame] | 1891 | PyObject *res; |
| 1892 | |
Tim Peters | f4b33f6 | 2001-06-02 05:42:29 +0000 | [diff] [blame] | 1893 | /* Allocate the result tuple before checking the size. Believe it |
| 1894 | * or not, this allocation could trigger a garbage collection which |
| 1895 | * could empty the dict, so if we checked the size first and that |
| 1896 | * happened, the result would be an infinite loop (searching for an |
| 1897 | * entry that no longer exists). Note that the usual popitem() |
| 1898 | * idiom is "while d: k, v = d.popitem()". so needing to throw the |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 1899 | * tuple away if the dict *is* empty isn't a significant |
Tim Peters | f4b33f6 | 2001-06-02 05:42:29 +0000 | [diff] [blame] | 1900 | * inefficiency -- possible, but unlikely in practice. |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1901 | */ |
| 1902 | res = PyTuple_New(2); |
| 1903 | if (res == NULL) |
| 1904 | return NULL; |
Guido van Rossum | e04eaec | 2001-04-16 00:02:32 +0000 | [diff] [blame] | 1905 | if (mp->ma_used == 0) { |
| 1906 | Py_DECREF(res); |
| 1907 | PyErr_SetString(PyExc_KeyError, |
| 1908 | "popitem(): dictionary is empty"); |
| 1909 | return NULL; |
| 1910 | } |
Guido van Rossum | ba6ab84 | 2000-12-12 22:02:18 +0000 | [diff] [blame] | 1911 | /* Set ep to "the first" dict entry with a value. We abuse the hash |
| 1912 | * field of slot 0 to hold a search finger: |
| 1913 | * If slot 0 has a value, use slot 0. |
| 1914 | * Else slot 0 is being used to hold a search finger, |
Neal Norwitz | d3881b0 | 2006-05-30 04:25:05 +0000 | [diff] [blame] | 1915 | * and we use its hash value as the first index to look. |
Guido van Rossum | ba6ab84 | 2000-12-12 22:02:18 +0000 | [diff] [blame] | 1916 | */ |
| 1917 | ep = &mp->ma_table[0]; |
| 1918 | if (ep->me_value == NULL) { |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 1919 | i = ep->me_hash; |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 1920 | /* The hash field may be a real hash value, or it may be a |
| 1921 | * legit search finger, or it may be a once-legit search |
| 1922 | * finger that's out of bounds now because it wrapped around |
| 1923 | * or the table shrunk -- simply make sure it's in bounds now. |
Guido van Rossum | ba6ab84 | 2000-12-12 22:02:18 +0000 | [diff] [blame] | 1924 | */ |
Tim Peters | afb6ae8 | 2001-06-04 21:00:21 +0000 | [diff] [blame] | 1925 | if (i > mp->ma_mask || i < 1) |
Guido van Rossum | ba6ab84 | 2000-12-12 22:02:18 +0000 | [diff] [blame] | 1926 | i = 1; /* skip slot 0 */ |
| 1927 | while ((ep = &mp->ma_table[i])->me_value == NULL) { |
| 1928 | i++; |
Tim Peters | afb6ae8 | 2001-06-04 21:00:21 +0000 | [diff] [blame] | 1929 | if (i > mp->ma_mask) |
Guido van Rossum | ba6ab84 | 2000-12-12 22:02:18 +0000 | [diff] [blame] | 1930 | i = 1; |
| 1931 | } |
| 1932 | } |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1933 | PyTuple_SET_ITEM(res, 0, ep->me_key); |
| 1934 | PyTuple_SET_ITEM(res, 1, ep->me_value); |
| 1935 | Py_INCREF(dummy); |
| 1936 | ep->me_key = dummy; |
| 1937 | ep->me_value = NULL; |
| 1938 | mp->ma_used--; |
| 1939 | assert(mp->ma_table[0].me_value == NULL); |
| 1940 | mp->ma_table[0].me_hash = i + 1; /* next place to start */ |
Guido van Rossum | ba6ab84 | 2000-12-12 22:02:18 +0000 | [diff] [blame] | 1941 | return res; |
| 1942 | } |
| 1943 | |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 1944 | static int |
| 1945 | dict_traverse(PyObject *op, visitproc visit, void *arg) |
| 1946 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1947 | Py_ssize_t i = 0; |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 1948 | PyObject *pk; |
| 1949 | PyObject *pv; |
| 1950 | |
| 1951 | while (PyDict_Next(op, &i, &pk, &pv)) { |
Thomas Wouters | c6e5506 | 2006-04-15 21:47:09 +0000 | [diff] [blame] | 1952 | Py_VISIT(pk); |
| 1953 | Py_VISIT(pv); |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 1954 | } |
| 1955 | return 0; |
| 1956 | } |
| 1957 | |
| 1958 | static int |
| 1959 | dict_tp_clear(PyObject *op) |
| 1960 | { |
| 1961 | PyDict_Clear(op); |
| 1962 | return 0; |
| 1963 | } |
| 1964 | |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 1965 | |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 1966 | extern PyTypeObject PyDictIterKey_Type; /* Forward */ |
| 1967 | extern PyTypeObject PyDictIterValue_Type; /* Forward */ |
| 1968 | extern PyTypeObject PyDictIterItem_Type; /* Forward */ |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1969 | static PyObject *dictiter_new(PyDictObject *, PyTypeObject *); |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 1970 | |
| 1971 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1972 | dict_iterkeys(PyDictObject *dict) |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 1973 | { |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 1974 | return dictiter_new(dict, &PyDictIterKey_Type); |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 1975 | } |
| 1976 | |
| 1977 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1978 | dict_itervalues(PyDictObject *dict) |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 1979 | { |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 1980 | return dictiter_new(dict, &PyDictIterValue_Type); |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 1981 | } |
| 1982 | |
| 1983 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1984 | dict_iteritems(PyDictObject *dict) |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 1985 | { |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 1986 | return dictiter_new(dict, &PyDictIterItem_Type); |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 1987 | } |
| 1988 | |
| 1989 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1990 | PyDoc_STRVAR(has_key__doc__, |
Raymond Hettinger | 574aa32 | 2003-09-01 22:12:08 +0000 | [diff] [blame] | 1991 | "D.has_key(k) -> True if D has a key k, else False"); |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 1992 | |
Raymond Hettinger | 8f5cdaa | 2003-12-13 11:26:12 +0000 | [diff] [blame] | 1993 | PyDoc_STRVAR(contains__doc__, |
| 1994 | "D.__contains__(k) -> True if D has a key k, else False"); |
| 1995 | |
| 1996 | PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]"); |
| 1997 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1998 | PyDoc_STRVAR(get__doc__, |
Guido van Rossum | efae886 | 2002-09-04 11:29:45 +0000 | [diff] [blame] | 1999 | "D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None."); |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2000 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2001 | PyDoc_STRVAR(setdefault_doc__, |
Guido van Rossum | efae886 | 2002-09-04 11:29:45 +0000 | [diff] [blame] | 2002 | "D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D"); |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2003 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2004 | PyDoc_STRVAR(pop__doc__, |
Raymond Hettinger | a3e1e4c | 2003-03-06 23:54:28 +0000 | [diff] [blame] | 2005 | "D.pop(k[,d]) -> v, remove specified key and return the corresponding value\n\ |
| 2006 | If key is not found, d is returned if given, otherwise KeyError is raised"); |
Guido van Rossum | e027d98 | 2002-04-12 15:11:59 +0000 | [diff] [blame] | 2007 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2008 | PyDoc_STRVAR(popitem__doc__, |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2009 | "D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2010 | 2-tuple; but raise KeyError if D is empty"); |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2011 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2012 | PyDoc_STRVAR(keys__doc__, |
| 2013 | "D.keys() -> list of D's keys"); |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2014 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2015 | PyDoc_STRVAR(items__doc__, |
| 2016 | "D.items() -> list of D's (key, value) pairs, as 2-tuples"); |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2017 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2018 | PyDoc_STRVAR(values__doc__, |
| 2019 | "D.values() -> list of D's values"); |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2020 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2021 | PyDoc_STRVAR(update__doc__, |
Walter Dörwald | d70ad8a | 2004-05-28 20:59:21 +0000 | [diff] [blame] | 2022 | "D.update(E, **F) -> None. Update D from E and F: for k in E: D[k] = E[k]\n\ |
| 2023 | (if E has keys else: for (k, v) in E: D[k] = v) then: for k in F: D[k] = F[k]"); |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2024 | |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 2025 | PyDoc_STRVAR(fromkeys__doc__, |
| 2026 | "dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\ |
| 2027 | v defaults to None."); |
| 2028 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2029 | PyDoc_STRVAR(clear__doc__, |
| 2030 | "D.clear() -> None. Remove all items from D."); |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2031 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2032 | PyDoc_STRVAR(copy__doc__, |
| 2033 | "D.copy() -> a shallow copy of D"); |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2034 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2035 | PyDoc_STRVAR(iterkeys__doc__, |
| 2036 | "D.iterkeys() -> an iterator over the keys of D"); |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 2037 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2038 | PyDoc_STRVAR(itervalues__doc__, |
| 2039 | "D.itervalues() -> an iterator over the values of D"); |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 2040 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2041 | PyDoc_STRVAR(iteritems__doc__, |
| 2042 | "D.iteritems() -> an iterator over the (key, value) items of D"); |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 2043 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2044 | static PyMethodDef mapp_methods[] = { |
Neal Norwitz | c792629 | 2007-05-23 06:57:35 +0000 | [diff] [blame] | 2045 | {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST, |
Raymond Hettinger | 8f5cdaa | 2003-12-13 11:26:12 +0000 | [diff] [blame] | 2046 | contains__doc__}, |
Raymond Hettinger | 0c66967 | 2003-12-13 13:31:55 +0000 | [diff] [blame] | 2047 | {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST, |
Raymond Hettinger | 8f5cdaa | 2003-12-13 11:26:12 +0000 | [diff] [blame] | 2048 | getitem__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2049 | {"has_key", (PyCFunction)dict_has_key, METH_O, |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2050 | has_key__doc__}, |
| 2051 | {"get", (PyCFunction)dict_get, METH_VARARGS, |
| 2052 | get__doc__}, |
| 2053 | {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS, |
| 2054 | setdefault_doc__}, |
Raymond Hettinger | a3e1e4c | 2003-03-06 23:54:28 +0000 | [diff] [blame] | 2055 | {"pop", (PyCFunction)dict_pop, METH_VARARGS, |
Just van Rossum | a797d81 | 2002-11-23 09:45:04 +0000 | [diff] [blame] | 2056 | pop__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2057 | {"popitem", (PyCFunction)dict_popitem, METH_NOARGS, |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2058 | popitem__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2059 | {"keys", (PyCFunction)dict_keys, METH_NOARGS, |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2060 | keys__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2061 | {"items", (PyCFunction)dict_items, METH_NOARGS, |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2062 | items__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2063 | {"values", (PyCFunction)dict_values, METH_NOARGS, |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2064 | values__doc__}, |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 2065 | {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS, |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2066 | update__doc__}, |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 2067 | {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS, |
| 2068 | fromkeys__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2069 | {"clear", (PyCFunction)dict_clear, METH_NOARGS, |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2070 | clear__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2071 | {"copy", (PyCFunction)dict_copy, METH_NOARGS, |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2072 | copy__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2073 | {"iterkeys", (PyCFunction)dict_iterkeys, METH_NOARGS, |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 2074 | iterkeys__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2075 | {"itervalues", (PyCFunction)dict_itervalues, METH_NOARGS, |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 2076 | itervalues__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2077 | {"iteritems", (PyCFunction)dict_iteritems, METH_NOARGS, |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 2078 | iteritems__doc__}, |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2079 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2080 | }; |
| 2081 | |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 2082 | /* Return 1 if `key` is in dict `op`, 0 if not, and -1 on error. */ |
Raymond Hettinger | bc0f2ab | 2003-11-25 21:12:14 +0000 | [diff] [blame] | 2083 | int |
| 2084 | PyDict_Contains(PyObject *op, PyObject *key) |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 2085 | { |
| 2086 | long hash; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 2087 | PyDictObject *mp = (PyDictObject *)op; |
| 2088 | PyDictEntry *ep; |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 2089 | |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 2090 | if (!PyString_CheckExact(key) || |
Tim Peters | 1f7df35 | 2002-03-29 03:29:08 +0000 | [diff] [blame] | 2091 | (hash = ((PyStringObject *) key)->ob_shash) == -1) { |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 2092 | hash = PyObject_Hash(key); |
| 2093 | if (hash == -1) |
| 2094 | return -1; |
| 2095 | } |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 2096 | ep = (mp->ma_lookup)(mp, key, hash); |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 2097 | return ep == NULL ? -1 : (ep->me_value != NULL); |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 2098 | } |
| 2099 | |
Raymond Hettinger | d6fc72a | 2007-02-19 02:03:19 +0000 | [diff] [blame] | 2100 | /* Internal version of PyDict_Contains used when the hash value is already known */ |
| 2101 | int |
| 2102 | _PyDict_Contains(PyObject *op, PyObject *key, long hash) |
| 2103 | { |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 2104 | PyDictObject *mp = (PyDictObject *)op; |
| 2105 | PyDictEntry *ep; |
Raymond Hettinger | d6fc72a | 2007-02-19 02:03:19 +0000 | [diff] [blame] | 2106 | |
| 2107 | ep = (mp->ma_lookup)(mp, key, hash); |
| 2108 | return ep == NULL ? -1 : (ep->me_value != NULL); |
| 2109 | } |
| 2110 | |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 2111 | /* Hack to implement "key in dict" */ |
| 2112 | static PySequenceMethods dict_as_sequence = { |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 2113 | 0, /* sq_length */ |
| 2114 | 0, /* sq_concat */ |
| 2115 | 0, /* sq_repeat */ |
| 2116 | 0, /* sq_item */ |
| 2117 | 0, /* sq_slice */ |
| 2118 | 0, /* sq_ass_item */ |
| 2119 | 0, /* sq_ass_slice */ |
| 2120 | PyDict_Contains, /* sq_contains */ |
| 2121 | 0, /* sq_inplace_concat */ |
| 2122 | 0, /* sq_inplace_repeat */ |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 2123 | }; |
| 2124 | |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 2125 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2126 | dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2127 | { |
| 2128 | PyObject *self; |
| 2129 | |
| 2130 | assert(type != NULL && type->tp_alloc != NULL); |
| 2131 | self = type->tp_alloc(type, 0); |
| 2132 | if (self != NULL) { |
| 2133 | PyDictObject *d = (PyDictObject *)self; |
| 2134 | /* It's guaranteed that tp->alloc zeroed out the struct. */ |
| 2135 | assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0); |
| 2136 | INIT_NONZERO_DICT_SLOTS(d); |
| 2137 | d->ma_lookup = lookdict_string; |
| 2138 | #ifdef SHOW_CONVERSION_COUNTS |
| 2139 | ++created; |
| 2140 | #endif |
| 2141 | } |
| 2142 | return self; |
| 2143 | } |
| 2144 | |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 2145 | static int |
| 2146 | dict_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 2147 | { |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 2148 | return dict_update_common(self, args, kwds, "dict"); |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 2149 | } |
| 2150 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2151 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 2152 | dict_iter(PyDictObject *dict) |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 2153 | { |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2154 | return dictiter_new(dict, &PyDictIterKey_Type); |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 2155 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2156 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2157 | PyDoc_STRVAR(dictionary_doc, |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 2158 | "dict() -> new empty dictionary.\n" |
| 2159 | "dict(mapping) -> new dictionary initialized from a mapping object's\n" |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 2160 | " (key, value) pairs.\n" |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 2161 | "dict(seq) -> new dictionary initialized as if via:\n" |
Tim Peters | 4d85953 | 2001-10-27 18:27:48 +0000 | [diff] [blame] | 2162 | " d = {}\n" |
| 2163 | " for k, v in seq:\n" |
Just van Rossum | a797d81 | 2002-11-23 09:45:04 +0000 | [diff] [blame] | 2164 | " d[k] = v\n" |
| 2165 | "dict(**kwargs) -> new dictionary initialized with the name=value pairs\n" |
| 2166 | " in the keyword argument list. For example: dict(one=1, two=2)"); |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 2167 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2168 | PyTypeObject PyDict_Type = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 2169 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 2170 | "dict", |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 2171 | sizeof(PyDictObject), |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2172 | 0, |
Guido van Rossum | b932420 | 2001-01-18 00:39:02 +0000 | [diff] [blame] | 2173 | (destructor)dict_dealloc, /* tp_dealloc */ |
| 2174 | (printfunc)dict_print, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2175 | 0, /* tp_getattr */ |
Guido van Rossum | b932420 | 2001-01-18 00:39:02 +0000 | [diff] [blame] | 2176 | 0, /* tp_setattr */ |
Tim Peters | 4fa58bf | 2001-05-10 21:45:19 +0000 | [diff] [blame] | 2177 | (cmpfunc)dict_compare, /* tp_compare */ |
Guido van Rossum | b932420 | 2001-01-18 00:39:02 +0000 | [diff] [blame] | 2178 | (reprfunc)dict_repr, /* tp_repr */ |
| 2179 | 0, /* tp_as_number */ |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 2180 | &dict_as_sequence, /* tp_as_sequence */ |
Guido van Rossum | b932420 | 2001-01-18 00:39:02 +0000 | [diff] [blame] | 2181 | &dict_as_mapping, /* tp_as_mapping */ |
Guido van Rossum | 64c06e3 | 2007-11-22 00:55:51 +0000 | [diff] [blame] | 2182 | 0, /* tp_hash */ |
Guido van Rossum | b932420 | 2001-01-18 00:39:02 +0000 | [diff] [blame] | 2183 | 0, /* tp_call */ |
| 2184 | 0, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2185 | PyObject_GenericGetAttr, /* tp_getattro */ |
Guido van Rossum | b932420 | 2001-01-18 00:39:02 +0000 | [diff] [blame] | 2186 | 0, /* tp_setattro */ |
| 2187 | 0, /* tp_as_buffer */ |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 2188 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
Neal Norwitz | ee3a1b5 | 2007-02-25 19:44:48 +0000 | [diff] [blame] | 2189 | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */ |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 2190 | dictionary_doc, /* tp_doc */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 2191 | dict_traverse, /* tp_traverse */ |
| 2192 | dict_tp_clear, /* tp_clear */ |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 2193 | dict_richcompare, /* tp_richcompare */ |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2194 | 0, /* tp_weaklistoffset */ |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 2195 | (getiterfunc)dict_iter, /* tp_iter */ |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2196 | 0, /* tp_iternext */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2197 | mapp_methods, /* tp_methods */ |
| 2198 | 0, /* tp_members */ |
| 2199 | 0, /* tp_getset */ |
| 2200 | 0, /* tp_base */ |
| 2201 | 0, /* tp_dict */ |
| 2202 | 0, /* tp_descr_get */ |
| 2203 | 0, /* tp_descr_set */ |
| 2204 | 0, /* tp_dictoffset */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 2205 | dict_init, /* tp_init */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2206 | PyType_GenericAlloc, /* tp_alloc */ |
| 2207 | dict_new, /* tp_new */ |
Neil Schemenauer | 6189b89 | 2002-04-12 02:43:00 +0000 | [diff] [blame] | 2208 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2209 | }; |
| 2210 | |
Guido van Rossum | 3cca245 | 1997-05-16 14:23:33 +0000 | [diff] [blame] | 2211 | /* For backward compatibility with old dictionary interface */ |
| 2212 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2213 | PyObject * |
Martin v. Löwis | 32b4a1b | 2002-12-11 13:21:12 +0000 | [diff] [blame] | 2214 | PyDict_GetItemString(PyObject *v, const char *key) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2215 | { |
Guido van Rossum | 3cca245 | 1997-05-16 14:23:33 +0000 | [diff] [blame] | 2216 | PyObject *kv, *rv; |
| 2217 | kv = PyString_FromString(key); |
| 2218 | if (kv == NULL) |
| 2219 | return NULL; |
Guido van Rossum | 3cca245 | 1997-05-16 14:23:33 +0000 | [diff] [blame] | 2220 | rv = PyDict_GetItem(v, kv); |
| 2221 | Py_DECREF(kv); |
| 2222 | return rv; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2223 | } |
| 2224 | |
| 2225 | int |
Martin v. Löwis | 32b4a1b | 2002-12-11 13:21:12 +0000 | [diff] [blame] | 2226 | PyDict_SetItemString(PyObject *v, const char *key, PyObject *item) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2227 | { |
Guido van Rossum | 3cca245 | 1997-05-16 14:23:33 +0000 | [diff] [blame] | 2228 | PyObject *kv; |
| 2229 | int err; |
| 2230 | kv = PyString_FromString(key); |
| 2231 | if (kv == NULL) |
Guido van Rossum | 037b220 | 1997-05-20 18:35:19 +0000 | [diff] [blame] | 2232 | return -1; |
Guido van Rossum | 4f3bf1e | 1997-09-29 23:31:11 +0000 | [diff] [blame] | 2233 | PyString_InternInPlace(&kv); /* XXX Should we really? */ |
Guido van Rossum | 3cca245 | 1997-05-16 14:23:33 +0000 | [diff] [blame] | 2234 | err = PyDict_SetItem(v, kv, item); |
| 2235 | Py_DECREF(kv); |
| 2236 | return err; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2237 | } |
| 2238 | |
| 2239 | int |
Martin v. Löwis | 32b4a1b | 2002-12-11 13:21:12 +0000 | [diff] [blame] | 2240 | PyDict_DelItemString(PyObject *v, const char *key) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2241 | { |
Guido van Rossum | 3cca245 | 1997-05-16 14:23:33 +0000 | [diff] [blame] | 2242 | PyObject *kv; |
| 2243 | int err; |
| 2244 | kv = PyString_FromString(key); |
| 2245 | if (kv == NULL) |
Guido van Rossum | 037b220 | 1997-05-20 18:35:19 +0000 | [diff] [blame] | 2246 | return -1; |
Guido van Rossum | 3cca245 | 1997-05-16 14:23:33 +0000 | [diff] [blame] | 2247 | err = PyDict_DelItem(v, kv); |
| 2248 | Py_DECREF(kv); |
| 2249 | return err; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2250 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2251 | |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2252 | /* Dictionary iterator types */ |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2253 | |
| 2254 | typedef struct { |
| 2255 | PyObject_HEAD |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 2256 | PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */ |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 2257 | Py_ssize_t di_used; |
| 2258 | Py_ssize_t di_pos; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2259 | PyObject* di_result; /* reusable result tuple for iteritems */ |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 2260 | Py_ssize_t len; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2261 | } dictiterobject; |
| 2262 | |
| 2263 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 2264 | dictiter_new(PyDictObject *dict, PyTypeObject *itertype) |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2265 | { |
| 2266 | dictiterobject *di; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2267 | di = PyObject_New(dictiterobject, itertype); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2268 | if (di == NULL) |
| 2269 | return NULL; |
| 2270 | Py_INCREF(dict); |
| 2271 | di->di_dict = dict; |
Guido van Rossum | b1f35bf | 2001-05-02 15:13:44 +0000 | [diff] [blame] | 2272 | di->di_used = dict->ma_used; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2273 | di->di_pos = 0; |
Raymond Hettinger | 0ce6dc8 | 2004-03-18 08:38:00 +0000 | [diff] [blame] | 2274 | di->len = dict->ma_used; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2275 | if (itertype == &PyDictIterItem_Type) { |
| 2276 | di->di_result = PyTuple_Pack(2, Py_None, Py_None); |
| 2277 | if (di->di_result == NULL) { |
| 2278 | Py_DECREF(di); |
| 2279 | return NULL; |
| 2280 | } |
| 2281 | } |
| 2282 | else |
| 2283 | di->di_result = NULL; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2284 | return (PyObject *)di; |
| 2285 | } |
| 2286 | |
| 2287 | static void |
| 2288 | dictiter_dealloc(dictiterobject *di) |
| 2289 | { |
Guido van Rossum | 2147df7 | 2002-07-16 20:30:22 +0000 | [diff] [blame] | 2290 | Py_XDECREF(di->di_dict); |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2291 | Py_XDECREF(di->di_result); |
Neil Schemenauer | 6189b89 | 2002-04-12 02:43:00 +0000 | [diff] [blame] | 2292 | PyObject_Del(di); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2293 | } |
| 2294 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 2295 | static PyObject * |
Raymond Hettinger | 0ce6dc8 | 2004-03-18 08:38:00 +0000 | [diff] [blame] | 2296 | dictiter_len(dictiterobject *di) |
| 2297 | { |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 2298 | Py_ssize_t len = 0; |
Raymond Hettinger | 7892b1c | 2004-04-12 18:10:01 +0000 | [diff] [blame] | 2299 | if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used) |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 2300 | len = di->len; |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 2301 | return PyInt_FromSize_t(len); |
Raymond Hettinger | 0ce6dc8 | 2004-03-18 08:38:00 +0000 | [diff] [blame] | 2302 | } |
| 2303 | |
Armin Rigo | f5b3e36 | 2006-02-11 21:32:43 +0000 | [diff] [blame] | 2304 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 2305 | |
| 2306 | static PyMethodDef dictiter_methods[] = { |
Armin Rigo | f5b3e36 | 2006-02-11 21:32:43 +0000 | [diff] [blame] | 2307 | {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS, length_hint_doc}, |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 2308 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | 0ce6dc8 | 2004-03-18 08:38:00 +0000 | [diff] [blame] | 2309 | }; |
| 2310 | |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2311 | static PyObject *dictiter_iternextkey(dictiterobject *di) |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2312 | { |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2313 | PyObject *key; |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 2314 | register Py_ssize_t i, mask; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 2315 | register PyDictEntry *ep; |
| 2316 | PyDictObject *d = di->di_dict; |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2317 | |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2318 | if (d == NULL) |
Guido van Rossum | 2147df7 | 2002-07-16 20:30:22 +0000 | [diff] [blame] | 2319 | return NULL; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2320 | assert (PyDict_Check(d)); |
Guido van Rossum | 2147df7 | 2002-07-16 20:30:22 +0000 | [diff] [blame] | 2321 | |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2322 | if (di->di_used != d->ma_used) { |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2323 | PyErr_SetString(PyExc_RuntimeError, |
| 2324 | "dictionary changed size during iteration"); |
Guido van Rossum | 2147df7 | 2002-07-16 20:30:22 +0000 | [diff] [blame] | 2325 | di->di_used = -1; /* Make this state sticky */ |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2326 | return NULL; |
| 2327 | } |
Guido van Rossum | 2147df7 | 2002-07-16 20:30:22 +0000 | [diff] [blame] | 2328 | |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2329 | i = di->di_pos; |
| 2330 | if (i < 0) |
| 2331 | goto fail; |
| 2332 | ep = d->ma_table; |
| 2333 | mask = d->ma_mask; |
| 2334 | while (i <= mask && ep[i].me_value == NULL) |
| 2335 | i++; |
| 2336 | di->di_pos = i+1; |
| 2337 | if (i > mask) |
| 2338 | goto fail; |
Raymond Hettinger | 0ce6dc8 | 2004-03-18 08:38:00 +0000 | [diff] [blame] | 2339 | di->len--; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2340 | key = ep[i].me_key; |
| 2341 | Py_INCREF(key); |
| 2342 | return key; |
| 2343 | |
| 2344 | fail: |
| 2345 | Py_DECREF(d); |
Guido van Rossum | 2147df7 | 2002-07-16 20:30:22 +0000 | [diff] [blame] | 2346 | di->di_dict = NULL; |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2347 | return NULL; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2348 | } |
| 2349 | |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2350 | PyTypeObject PyDictIterKey_Type = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 2351 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Raymond Hettinger | 0ce6dc8 | 2004-03-18 08:38:00 +0000 | [diff] [blame] | 2352 | "dictionary-keyiterator", /* tp_name */ |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2353 | sizeof(dictiterobject), /* tp_basicsize */ |
| 2354 | 0, /* tp_itemsize */ |
| 2355 | /* methods */ |
| 2356 | (destructor)dictiter_dealloc, /* tp_dealloc */ |
| 2357 | 0, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2358 | 0, /* tp_getattr */ |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2359 | 0, /* tp_setattr */ |
| 2360 | 0, /* tp_compare */ |
| 2361 | 0, /* tp_repr */ |
| 2362 | 0, /* tp_as_number */ |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 2363 | 0, /* tp_as_sequence */ |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2364 | 0, /* tp_as_mapping */ |
| 2365 | 0, /* tp_hash */ |
| 2366 | 0, /* tp_call */ |
| 2367 | 0, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2368 | PyObject_GenericGetAttr, /* tp_getattro */ |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2369 | 0, /* tp_setattro */ |
| 2370 | 0, /* tp_as_buffer */ |
| 2371 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 2372 | 0, /* tp_doc */ |
| 2373 | 0, /* tp_traverse */ |
| 2374 | 0, /* tp_clear */ |
| 2375 | 0, /* tp_richcompare */ |
| 2376 | 0, /* tp_weaklistoffset */ |
Raymond Hettinger | 1da1dbf | 2003-03-17 19:46:11 +0000 | [diff] [blame] | 2377 | PyObject_SelfIter, /* tp_iter */ |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2378 | (iternextfunc)dictiter_iternextkey, /* tp_iternext */ |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 2379 | dictiter_methods, /* tp_methods */ |
| 2380 | 0, |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2381 | }; |
| 2382 | |
| 2383 | static PyObject *dictiter_iternextvalue(dictiterobject *di) |
| 2384 | { |
| 2385 | PyObject *value; |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 2386 | register Py_ssize_t i, mask; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 2387 | register PyDictEntry *ep; |
| 2388 | PyDictObject *d = di->di_dict; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2389 | |
| 2390 | if (d == NULL) |
| 2391 | return NULL; |
| 2392 | assert (PyDict_Check(d)); |
| 2393 | |
| 2394 | if (di->di_used != d->ma_used) { |
| 2395 | PyErr_SetString(PyExc_RuntimeError, |
| 2396 | "dictionary changed size during iteration"); |
| 2397 | di->di_used = -1; /* Make this state sticky */ |
| 2398 | return NULL; |
| 2399 | } |
| 2400 | |
| 2401 | i = di->di_pos; |
Guido van Rossum | 09240f6 | 2004-03-20 19:11:58 +0000 | [diff] [blame] | 2402 | mask = d->ma_mask; |
| 2403 | if (i < 0 || i > mask) |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2404 | goto fail; |
| 2405 | ep = d->ma_table; |
Guido van Rossum | 09240f6 | 2004-03-20 19:11:58 +0000 | [diff] [blame] | 2406 | while ((value=ep[i].me_value) == NULL) { |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2407 | i++; |
Guido van Rossum | 09240f6 | 2004-03-20 19:11:58 +0000 | [diff] [blame] | 2408 | if (i > mask) |
| 2409 | goto fail; |
| 2410 | } |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2411 | di->di_pos = i+1; |
Raymond Hettinger | 0ce6dc8 | 2004-03-18 08:38:00 +0000 | [diff] [blame] | 2412 | di->len--; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2413 | Py_INCREF(value); |
| 2414 | return value; |
| 2415 | |
| 2416 | fail: |
| 2417 | Py_DECREF(d); |
| 2418 | di->di_dict = NULL; |
| 2419 | return NULL; |
| 2420 | } |
| 2421 | |
| 2422 | PyTypeObject PyDictIterValue_Type = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 2423 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2424 | "dictionary-valueiterator", /* tp_name */ |
| 2425 | sizeof(dictiterobject), /* tp_basicsize */ |
| 2426 | 0, /* tp_itemsize */ |
| 2427 | /* methods */ |
| 2428 | (destructor)dictiter_dealloc, /* tp_dealloc */ |
| 2429 | 0, /* tp_print */ |
| 2430 | 0, /* tp_getattr */ |
| 2431 | 0, /* tp_setattr */ |
| 2432 | 0, /* tp_compare */ |
| 2433 | 0, /* tp_repr */ |
| 2434 | 0, /* tp_as_number */ |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 2435 | 0, /* tp_as_sequence */ |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2436 | 0, /* tp_as_mapping */ |
| 2437 | 0, /* tp_hash */ |
| 2438 | 0, /* tp_call */ |
| 2439 | 0, /* tp_str */ |
| 2440 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2441 | 0, /* tp_setattro */ |
| 2442 | 0, /* tp_as_buffer */ |
| 2443 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 2444 | 0, /* tp_doc */ |
| 2445 | 0, /* tp_traverse */ |
| 2446 | 0, /* tp_clear */ |
| 2447 | 0, /* tp_richcompare */ |
| 2448 | 0, /* tp_weaklistoffset */ |
| 2449 | PyObject_SelfIter, /* tp_iter */ |
| 2450 | (iternextfunc)dictiter_iternextvalue, /* tp_iternext */ |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 2451 | dictiter_methods, /* tp_methods */ |
| 2452 | 0, |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2453 | }; |
| 2454 | |
| 2455 | static PyObject *dictiter_iternextitem(dictiterobject *di) |
| 2456 | { |
| 2457 | PyObject *key, *value, *result = di->di_result; |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 2458 | register Py_ssize_t i, mask; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 2459 | register PyDictEntry *ep; |
| 2460 | PyDictObject *d = di->di_dict; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2461 | |
| 2462 | if (d == NULL) |
| 2463 | return NULL; |
| 2464 | assert (PyDict_Check(d)); |
| 2465 | |
| 2466 | if (di->di_used != d->ma_used) { |
| 2467 | PyErr_SetString(PyExc_RuntimeError, |
| 2468 | "dictionary changed size during iteration"); |
| 2469 | di->di_used = -1; /* Make this state sticky */ |
| 2470 | return NULL; |
| 2471 | } |
| 2472 | |
| 2473 | i = di->di_pos; |
| 2474 | if (i < 0) |
| 2475 | goto fail; |
| 2476 | ep = d->ma_table; |
| 2477 | mask = d->ma_mask; |
| 2478 | while (i <= mask && ep[i].me_value == NULL) |
| 2479 | i++; |
| 2480 | di->di_pos = i+1; |
| 2481 | if (i > mask) |
| 2482 | goto fail; |
| 2483 | |
| 2484 | if (result->ob_refcnt == 1) { |
| 2485 | Py_INCREF(result); |
| 2486 | Py_DECREF(PyTuple_GET_ITEM(result, 0)); |
| 2487 | Py_DECREF(PyTuple_GET_ITEM(result, 1)); |
| 2488 | } else { |
| 2489 | result = PyTuple_New(2); |
Tim Peters | 60b2996 | 2006-01-01 01:19:23 +0000 | [diff] [blame] | 2490 | if (result == NULL) |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2491 | return NULL; |
| 2492 | } |
Raymond Hettinger | 0ce6dc8 | 2004-03-18 08:38:00 +0000 | [diff] [blame] | 2493 | di->len--; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2494 | key = ep[i].me_key; |
| 2495 | value = ep[i].me_value; |
| 2496 | Py_INCREF(key); |
| 2497 | Py_INCREF(value); |
| 2498 | PyTuple_SET_ITEM(result, 0, key); |
| 2499 | PyTuple_SET_ITEM(result, 1, value); |
| 2500 | return result; |
| 2501 | |
| 2502 | fail: |
| 2503 | Py_DECREF(d); |
| 2504 | di->di_dict = NULL; |
| 2505 | return NULL; |
| 2506 | } |
| 2507 | |
| 2508 | PyTypeObject PyDictIterItem_Type = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 2509 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2510 | "dictionary-itemiterator", /* tp_name */ |
| 2511 | sizeof(dictiterobject), /* tp_basicsize */ |
| 2512 | 0, /* tp_itemsize */ |
| 2513 | /* methods */ |
| 2514 | (destructor)dictiter_dealloc, /* tp_dealloc */ |
| 2515 | 0, /* tp_print */ |
| 2516 | 0, /* tp_getattr */ |
| 2517 | 0, /* tp_setattr */ |
| 2518 | 0, /* tp_compare */ |
| 2519 | 0, /* tp_repr */ |
| 2520 | 0, /* tp_as_number */ |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 2521 | 0, /* tp_as_sequence */ |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2522 | 0, /* tp_as_mapping */ |
| 2523 | 0, /* tp_hash */ |
| 2524 | 0, /* tp_call */ |
| 2525 | 0, /* tp_str */ |
| 2526 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2527 | 0, /* tp_setattro */ |
| 2528 | 0, /* tp_as_buffer */ |
| 2529 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 2530 | 0, /* tp_doc */ |
| 2531 | 0, /* tp_traverse */ |
| 2532 | 0, /* tp_clear */ |
| 2533 | 0, /* tp_richcompare */ |
| 2534 | 0, /* tp_weaklistoffset */ |
| 2535 | PyObject_SelfIter, /* tp_iter */ |
| 2536 | (iternextfunc)dictiter_iternextitem, /* tp_iternext */ |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 2537 | dictiter_methods, /* tp_methods */ |
| 2538 | 0, |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2539 | }; |