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