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