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 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 10 | |
| 11 | /* |
| 12 | There are four kinds of slots in the table: |
| 13 | |
| 14 | 1. Unused. me_key == me_value == NULL |
| 15 | Does not hold an active (key, value) pair now and never did. Unused can |
| 16 | transition to Active upon key insertion. This is the only case in which |
| 17 | me_key is NULL, and is each slot's initial state. |
| 18 | |
| 19 | 2. Active. me_key != NULL and me_key != dummy and me_value != NULL |
| 20 | Holds an active (key, value) pair. Active can transition to Dummy or |
| 21 | Pending upon key deletion (for combined and split tables respectively). |
| 22 | This is the only case in which me_value != NULL. |
| 23 | |
| 24 | 3. Dummy. me_key == dummy and me_value == NULL |
| 25 | Previously held an active (key, value) pair, but that was deleted and an |
| 26 | active pair has not yet overwritten the slot. Dummy can transition to |
| 27 | Active upon key insertion. Dummy slots cannot be made Unused again |
| 28 | (cannot have me_key set to NULL), else the probe sequence in case of |
| 29 | collision would have no way to know they were once active. |
| 30 | |
| 31 | 4. Pending. Not yet inserted or deleted from a split-table. |
| 32 | key != NULL, key != dummy and value == NULL |
| 33 | |
| 34 | The DictObject can be in one of two forms. |
| 35 | Either: |
| 36 | A combined table: |
| 37 | ma_values == NULL, dk_refcnt == 1. |
| 38 | Values are stored in the me_value field of the PyDictKeysObject. |
| 39 | Slot kind 4 is not allowed i.e. |
| 40 | key != NULL, key != dummy and value == NULL is illegal. |
| 41 | Or: |
| 42 | A split table: |
| 43 | ma_values != NULL, dk_refcnt >= 1 |
| 44 | Values are stored in the ma_values array. |
| 45 | Only string (unicode) keys are allowed, no <dummy> keys are present. |
| 46 | |
| 47 | Note: .popitem() abuses the me_hash field of an Unused or Dummy slot to |
| 48 | hold a search finger. The me_hash field of Unused or Dummy slots has no |
| 49 | meaning otherwise. As a consequence of this popitem always converts the dict |
| 50 | to the combined-table form. |
| 51 | */ |
| 52 | |
| 53 | /* PyDict_MINSIZE_SPLIT is the minimum size of a split dictionary. |
| 54 | * It must be a power of 2, and at least 4. |
| 55 | * Resizing of split dictionaries is very rare, so the saving memory is more |
| 56 | * important than the cost of resizing. |
| 57 | */ |
| 58 | #define PyDict_MINSIZE_SPLIT 4 |
| 59 | |
| 60 | /* PyDict_MINSIZE_COMBINED is the starting size for any new, non-split dict. |
| 61 | * 8 allows dicts with no more than 5 active entries; experiments suggested |
| 62 | * this suffices for the majority of dicts (consisting mostly of usually-small |
| 63 | * dicts created to pass keyword arguments). |
| 64 | * Making this 8, rather than 4 reduces the number of resizes for most |
| 65 | * dictionaries, without any significant extra memory use. |
| 66 | */ |
| 67 | #define PyDict_MINSIZE_COMBINED 8 |
| 68 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 69 | #include "Python.h" |
Christian Heimes | 0ded5b5 | 2007-12-10 15:50:56 +0000 | [diff] [blame] | 70 | #include "stringlib/eq.h" |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 71 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 72 | typedef struct { |
| 73 | /* Cached hash code of me_key. */ |
| 74 | Py_hash_t me_hash; |
| 75 | PyObject *me_key; |
| 76 | PyObject *me_value; /* This field is only meaningful for combined tables */ |
| 77 | } PyDictKeyEntry; |
| 78 | |
| 79 | typedef PyDictKeyEntry *(*dict_lookup_func) |
| 80 | (PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject ***value_addr); |
| 81 | |
| 82 | struct _dictkeysobject { |
| 83 | Py_ssize_t dk_refcnt; |
| 84 | Py_ssize_t dk_size; |
| 85 | dict_lookup_func dk_lookup; |
| 86 | Py_ssize_t dk_usable; |
| 87 | PyDictKeyEntry dk_entries[1]; |
| 88 | }; |
| 89 | |
| 90 | |
| 91 | /* |
| 92 | To ensure the lookup algorithm terminates, there must be at least one Unused |
| 93 | slot (NULL key) in the table. |
| 94 | To avoid slowing down lookups on a near-full table, we resize the table when |
| 95 | it's USABLE_FRACTION (currently two-thirds) full. |
| 96 | */ |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 97 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 98 | /* Set a key error with the specified argument, wrapping it in a |
| 99 | * tuple automatically so that tuple keys are not unpacked as the |
| 100 | * exception arguments. */ |
| 101 | static void |
| 102 | set_key_error(PyObject *arg) |
| 103 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 104 | PyObject *tup; |
| 105 | tup = PyTuple_Pack(1, arg); |
| 106 | if (!tup) |
| 107 | return; /* caller will expect error to be set anyway */ |
| 108 | PyErr_SetObject(PyExc_KeyError, tup); |
| 109 | Py_DECREF(tup); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 112 | #define PERTURB_SHIFT 5 |
| 113 | |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 114 | /* |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 115 | Major subtleties ahead: Most hash schemes depend on having a "good" hash |
| 116 | function, in the sense of simulating randomness. Python doesn't: its most |
| 117 | important hash functions (for strings and ints) are very regular in common |
| 118 | cases: |
Tim Peters | 15d4929 | 2001-05-27 07:39:22 +0000 | [diff] [blame] | 119 | |
Guido van Rossum | dc5f6b2 | 2006-08-24 21:29:26 +0000 | [diff] [blame] | 120 | >>> map(hash, (0, 1, 2, 3)) |
| 121 | [0, 1, 2, 3] |
| 122 | >>> map(hash, ("namea", "nameb", "namec", "named")) |
| 123 | [-1658398457, -1658398460, -1658398459, -1658398462] |
| 124 | >>> |
Tim Peters | 15d4929 | 2001-05-27 07:39:22 +0000 | [diff] [blame] | 125 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 126 | This isn't necessarily bad! To the contrary, in a table of size 2**i, taking |
| 127 | the low-order i bits as the initial table index is extremely fast, and there |
| 128 | are no collisions at all for dicts indexed by a contiguous range of ints. |
| 129 | The same is approximately true when keys are "consecutive" strings. So this |
| 130 | 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] | 131 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 132 | OTOH, when collisions occur, the tendency to fill contiguous slices of the |
| 133 | hash table makes a good collision resolution strategy crucial. Taking only |
| 134 | the last i bits of the hash code is also vulnerable: for example, consider |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 135 | the list [i << 16 for i in range(20000)] as a set of keys. Since ints are |
Guido van Rossum | dc5f6b2 | 2006-08-24 21:29:26 +0000 | [diff] [blame] | 136 | their own hash codes, and this fits in a dict of size 2**15, the last 15 bits |
| 137 | of every 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] | 138 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 139 | But catering to unusual cases should not slow the usual ones, so we just take |
| 140 | the last i bits anyway. It's up to collision resolution to do the rest. If |
| 141 | we *usually* find the key we're looking for on the first try (and, it turns |
| 142 | out, we usually do -- the table load factor is kept under 2/3, so the odds |
| 143 | are solidly in our favor), then it makes best sense to keep the initial index |
| 144 | computation dirt cheap. |
Tim Peters | 15d4929 | 2001-05-27 07:39:22 +0000 | [diff] [blame] | 145 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 146 | The first half of collision resolution is to visit table indices via this |
| 147 | recurrence: |
Tim Peters | 15d4929 | 2001-05-27 07:39:22 +0000 | [diff] [blame] | 148 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 149 | j = ((5*j) + 1) mod 2**i |
Tim Peters | 15d4929 | 2001-05-27 07:39:22 +0000 | [diff] [blame] | 150 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 151 | For any initial j in range(2**i), repeating that 2**i times generates each |
| 152 | int in range(2**i) exactly once (see any text on random-number generation for |
| 153 | proof). By itself, this doesn't help much: like linear probing (setting |
| 154 | j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed |
| 155 | order. This would be bad, except that's not the only thing we do, and it's |
| 156 | actually *good* in the common cases where hash keys are consecutive. In an |
| 157 | example that's really too small to make this entirely clear, for a table of |
| 158 | size 2**3 the order of indices is: |
Tim Peters | 15d4929 | 2001-05-27 07:39:22 +0000 | [diff] [blame] | 159 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 160 | 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating] |
| 161 | |
| 162 | If two things come in at index 5, the first place we look after is index 2, |
| 163 | not 6, so if another comes in at index 6 the collision at 5 didn't hurt it. |
| 164 | Linear probing is deadly in this case because there the fixed probe order |
| 165 | is the *same* as the order consecutive keys are likely to arrive. But it's |
| 166 | extremely unlikely hash codes will follow a 5*j+1 recurrence by accident, |
| 167 | and certain that consecutive hash codes do not. |
| 168 | |
| 169 | The other half of the strategy is to get the other bits of the hash code |
| 170 | into play. This is done by initializing a (unsigned) vrbl "perturb" to the |
| 171 | full hash code, and changing the recurrence to: |
| 172 | |
| 173 | j = (5*j) + 1 + perturb; |
| 174 | perturb >>= PERTURB_SHIFT; |
| 175 | use j % 2**i as the next table index; |
| 176 | |
| 177 | Now the probe sequence depends (eventually) on every bit in the hash code, |
| 178 | and the pseudo-scrambling property of recurring on 5*j+1 is more valuable, |
| 179 | because it quickly magnifies small differences in the bits that didn't affect |
| 180 | the initial index. Note that because perturb is unsigned, if the recurrence |
| 181 | is executed often enough perturb eventually becomes and remains 0. At that |
| 182 | point (very rarely reached) the recurrence is on (just) 5*j+1 again, and |
| 183 | that's certain to find an empty slot eventually (since it generates every int |
| 184 | in range(2**i), and we make sure there's always at least one empty slot). |
| 185 | |
| 186 | Selecting a good value for PERTURB_SHIFT is a balancing act. You want it |
| 187 | small so that the high bits of the hash code continue to affect the probe |
| 188 | sequence across iterations; but you want it large so that in really bad cases |
| 189 | the high-order hash bits have an effect on early iterations. 5 was "the |
| 190 | best" in minimizing total collisions across experiments Tim Peters ran (on |
| 191 | both normal and pathological cases), but 4 and 6 weren't significantly worse. |
| 192 | |
Guido van Rossum | dc5f6b2 | 2006-08-24 21:29:26 +0000 | [diff] [blame] | 193 | Historical: Reimer Behrends contributed the idea of using a polynomial-based |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 194 | approach, using repeated multiplication by x in GF(2**n) where an irreducible |
| 195 | polynomial for each table size was chosen such that x was a primitive root. |
| 196 | Christian Tismer later extended that to use division by x instead, as an |
| 197 | efficient way to get the high bits of the hash code into play. This scheme |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 198 | also gave excellent collision statistics, but was more expensive: two |
| 199 | if-tests were required inside the loop; computing "the next" index took about |
| 200 | the same number of operations but without as much potential parallelism |
| 201 | (e.g., computing 5*j can go on at the same time as computing 1+perturb in the |
| 202 | above, and then shifting perturb can be done while the table index is being |
| 203 | masked); and the PyDictObject struct required a member to hold the table's |
| 204 | polynomial. In Tim's experiments the current scheme ran faster, produced |
| 205 | equally good collision statistics, needed less code & used less memory. |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 206 | |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 207 | */ |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 208 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 209 | /* Object used as dummy key to fill deleted entries |
| 210 | * This could be any unique object, |
| 211 | * use a custom type in order to minimise coupling. |
| 212 | */ |
| 213 | static PyObject _dummy_struct; |
| 214 | |
| 215 | #define dummy (&_dummy_struct) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 216 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 217 | #ifdef Py_REF_DEBUG |
| 218 | PyObject * |
| 219 | _PyDict_Dummy(void) |
| 220 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 221 | return dummy; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 222 | } |
| 223 | #endif |
| 224 | |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 225 | /* forward declarations */ |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 226 | static PyDictKeyEntry *lookdict(PyDictObject *mp, PyObject *key, |
| 227 | Py_hash_t hash, PyObject ***value_addr); |
| 228 | static PyDictKeyEntry *lookdict_unicode(PyDictObject *mp, PyObject *key, |
| 229 | Py_hash_t hash, PyObject ***value_addr); |
| 230 | static PyDictKeyEntry * |
| 231 | lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key, |
| 232 | Py_hash_t hash, PyObject ***value_addr); |
| 233 | static PyDictKeyEntry *lookdict_split(PyDictObject *mp, PyObject *key, |
| 234 | Py_hash_t hash, PyObject ***value_addr); |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 235 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 236 | static int dictresize(PyDictObject *mp, Py_ssize_t minused); |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 237 | |
Raymond Hettinger | 4344278 | 2004-03-17 21:55:03 +0000 | [diff] [blame] | 238 | /* Dictionary reuse scheme to save calls to malloc, free, and memset */ |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 239 | #ifndef PyDict_MAXFREELIST |
| 240 | #define PyDict_MAXFREELIST 80 |
| 241 | #endif |
| 242 | static PyDictObject *free_list[PyDict_MAXFREELIST]; |
| 243 | static int numfree = 0; |
Raymond Hettinger | 4344278 | 2004-03-17 21:55:03 +0000 | [diff] [blame] | 244 | |
Antoine Pitrou | 9a812cb | 2011-11-15 00:00:12 +0100 | [diff] [blame] | 245 | int |
| 246 | PyDict_ClearFreeList(void) |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 247 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 248 | PyDictObject *op; |
Antoine Pitrou | 9a812cb | 2011-11-15 00:00:12 +0100 | [diff] [blame] | 249 | int ret = numfree; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 250 | while (numfree) { |
| 251 | op = free_list[--numfree]; |
| 252 | assert(PyDict_CheckExact(op)); |
| 253 | PyObject_GC_Del(op); |
| 254 | } |
Antoine Pitrou | 9a812cb | 2011-11-15 00:00:12 +0100 | [diff] [blame] | 255 | return ret; |
| 256 | } |
| 257 | |
| 258 | void |
| 259 | PyDict_Fini(void) |
| 260 | { |
| 261 | PyDict_ClearFreeList(); |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 264 | #define DK_INCREF(dk) (++(dk)->dk_refcnt) |
| 265 | #define DK_DECREF(dk) if ((--(dk)->dk_refcnt) == 0) free_keys_object(dk) |
| 266 | #define DK_SIZE(dk) ((dk)->dk_size) |
| 267 | #define DK_MASK(dk) (((dk)->dk_size)-1) |
| 268 | #define IS_POWER_OF_2(x) (((x) & (x-1)) == 0) |
| 269 | |
| 270 | /* USABLE_FRACTION must obey the following: |
| 271 | * (0 < USABLE_FRACTION(n) < n) for all n >= 2 |
| 272 | * |
| 273 | * USABLE_FRACTION should be very quick to calculate. |
| 274 | * Fractions around 5/8 to 2/3 seem to work well in practice. |
| 275 | */ |
| 276 | |
| 277 | /* Use (2n+1)/3 rather than 2n+3 because: it makes no difference for |
| 278 | * combined tables (the two fractions round to the same number n < ), |
| 279 | * but 2*4/3 is 2 whereas (2*4+1)/3 is 3 which potentially saves quite |
| 280 | * a lot of space for small, split tables */ |
| 281 | #define USABLE_FRACTION(n) ((((n) << 1)+1)/3) |
| 282 | |
| 283 | /* Alternative fraction that is otherwise close enough to (2n+1)/3 to make |
| 284 | * little difference. 8 * 2/3 == 8 * 5/8 == 5. 16 * 2/3 == 16 * 5/8 == 10. |
| 285 | * 32 * 2/3 = 21, 32 * 5/8 = 20. |
| 286 | * Its advantage is that it is faster to compute on machines with slow division. |
| 287 | * #define USABLE_FRACTION(n) (((n) >> 1) + ((n) >> 2) - ((n) >> 3)) |
| 288 | */ |
| 289 | |
| 290 | |
| 291 | #define ENSURE_ALLOWS_DELETIONS(d) \ |
| 292 | if ((d)->ma_keys->dk_lookup == lookdict_unicode_nodummy) { \ |
| 293 | (d)->ma_keys->dk_lookup = lookdict_unicode; \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 294 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 295 | |
| 296 | /* This immutable, empty PyDictKeysObject is used for PyDict_Clear() |
| 297 | * (which cannot fail and thus can do no allocation). |
| 298 | */ |
| 299 | static PyDictKeysObject empty_keys_struct = { |
| 300 | 2, /* dk_refcnt 1 for this struct, 1 for dummy_struct */ |
| 301 | 1, /* dk_size */ |
| 302 | lookdict_split, /* dk_lookup */ |
| 303 | 0, /* dk_usable (immutable) */ |
| 304 | { |
| 305 | { 0, 0, 0 } /* dk_entries (empty) */ |
| 306 | } |
| 307 | }; |
| 308 | |
| 309 | static PyObject *empty_values[1] = { NULL }; |
| 310 | |
| 311 | #define Py_EMPTY_KEYS &empty_keys_struct |
| 312 | |
| 313 | static PyDictKeysObject *new_keys_object(Py_ssize_t size) |
| 314 | { |
| 315 | PyDictKeysObject *dk; |
| 316 | Py_ssize_t i; |
| 317 | PyDictKeyEntry *ep0; |
| 318 | |
| 319 | assert(size >= PyDict_MINSIZE_SPLIT); |
| 320 | assert(IS_POWER_OF_2(size)); |
| 321 | dk = PyMem_MALLOC(sizeof(PyDictKeysObject) + |
| 322 | sizeof(PyDictKeyEntry) * (size-1)); |
| 323 | if (dk == NULL) { |
| 324 | PyErr_NoMemory(); |
| 325 | return NULL; |
| 326 | } |
| 327 | dk->dk_refcnt = 1; |
| 328 | dk->dk_size = size; |
| 329 | dk->dk_usable = USABLE_FRACTION(size); |
| 330 | ep0 = &dk->dk_entries[0]; |
| 331 | /* Hash value of slot 0 is used by popitem, so it must be initialized */ |
| 332 | ep0->me_hash = 0; |
| 333 | for (i = 0; i < size; i++) { |
| 334 | ep0[i].me_key = NULL; |
| 335 | ep0[i].me_value = NULL; |
| 336 | } |
| 337 | dk->dk_lookup = lookdict_unicode_nodummy; |
| 338 | return dk; |
| 339 | } |
| 340 | |
| 341 | static void |
| 342 | free_keys_object(PyDictKeysObject *keys) |
| 343 | { |
| 344 | PyDictKeyEntry *entries = &keys->dk_entries[0]; |
| 345 | Py_ssize_t i, n; |
| 346 | for (i = 0, n = DK_SIZE(keys); i < n; i++) { |
| 347 | Py_XDECREF(entries[i].me_key); |
| 348 | Py_XDECREF(entries[i].me_value); |
| 349 | } |
| 350 | PyMem_FREE(keys); |
| 351 | } |
| 352 | |
| 353 | #define new_values(size) PyMem_NEW(PyObject *, size) |
| 354 | |
| 355 | #define free_values(values) PyMem_FREE(values) |
| 356 | |
| 357 | /* Consumes a reference to the keys object */ |
| 358 | static PyObject * |
| 359 | new_dict(PyDictKeysObject *keys, PyObject **values) |
| 360 | { |
| 361 | PyDictObject *mp; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 362 | if (numfree) { |
| 363 | mp = free_list[--numfree]; |
| 364 | assert (mp != NULL); |
| 365 | assert (Py_TYPE(mp) == &PyDict_Type); |
| 366 | _Py_NewReference((PyObject *)mp); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 367 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 368 | else { |
| 369 | mp = PyObject_GC_New(PyDictObject, &PyDict_Type); |
| 370 | if (mp == NULL) { |
| 371 | DK_DECREF(keys); |
| 372 | free_values(values); |
| 373 | return NULL; |
| 374 | } |
| 375 | } |
| 376 | mp->ma_keys = keys; |
| 377 | mp->ma_values = values; |
| 378 | mp->ma_used = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 379 | return (PyObject *)mp; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 382 | /* Consumes a reference to the keys object */ |
| 383 | static PyObject * |
| 384 | new_dict_with_shared_keys(PyDictKeysObject *keys) |
| 385 | { |
| 386 | PyObject **values; |
| 387 | Py_ssize_t i, size; |
| 388 | |
| 389 | size = DK_SIZE(keys); |
| 390 | values = new_values(size); |
| 391 | if (values == NULL) { |
| 392 | DK_DECREF(keys); |
| 393 | return PyErr_NoMemory(); |
| 394 | } |
| 395 | for (i = 0; i < size; i++) { |
| 396 | values[i] = NULL; |
| 397 | } |
| 398 | return new_dict(keys, values); |
| 399 | } |
| 400 | |
| 401 | PyObject * |
| 402 | PyDict_New(void) |
| 403 | { |
| 404 | return new_dict(new_keys_object(PyDict_MINSIZE_COMBINED), NULL); |
| 405 | } |
| 406 | |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 407 | /* |
| 408 | The basic lookup function used by all operations. |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 409 | 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] | 410 | Open addressing is preferred over chaining since the link overhead for |
| 411 | chaining would be substantial (100% with typical malloc overhead). |
| 412 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 413 | The initial probe index is computed as hash mod the table size. Subsequent |
| 414 | probe indices are computed as explained earlier. |
Guido van Rossum | 2bc1379 | 1999-03-24 19:06:42 +0000 | [diff] [blame] | 415 | |
| 416 | All arithmetic on hash should ignore overflow. |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 417 | |
Guido van Rossum | dc5f6b2 | 2006-08-24 21:29:26 +0000 | [diff] [blame] | 418 | The details in this version are due to Tim Peters, building on many past |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 419 | contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and |
Guido van Rossum | dc5f6b2 | 2006-08-24 21:29:26 +0000 | [diff] [blame] | 420 | Christian Tismer. |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 421 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 422 | lookdict() is general-purpose, and may return NULL if (and only if) a |
| 423 | comparison raises an exception (this was new in Python 2.5). |
Guido van Rossum | 89d8c60 | 2007-09-18 17:26:56 +0000 | [diff] [blame] | 424 | lookdict_unicode() below is specialized to string keys, comparison of which can |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 425 | never raise an exception; that function can never return NULL. |
| 426 | lookdict_unicode_nodummy is further specialized for string keys that cannot be |
| 427 | the <dummy> value. |
| 428 | For both, when the key isn't found a PyDictEntry* is returned |
| 429 | where the key would have been found, *value_addr points to the matching value |
| 430 | slot. |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 431 | */ |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 432 | static PyDictKeyEntry * |
| 433 | lookdict(PyDictObject *mp, PyObject *key, |
| 434 | Py_hash_t hash, PyObject ***value_addr) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 435 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 436 | register size_t i; |
| 437 | register size_t perturb; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 438 | register PyDictKeyEntry *freeslot; |
| 439 | register size_t mask = DK_MASK(mp->ma_keys); |
| 440 | PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0]; |
| 441 | register PyDictKeyEntry *ep; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 442 | register int cmp; |
| 443 | PyObject *startkey; |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 444 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 445 | i = (size_t)hash & mask; |
| 446 | ep = &ep0[i]; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 447 | if (ep->me_key == NULL || ep->me_key == key) { |
| 448 | *value_addr = &ep->me_value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 449 | return ep; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 450 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 451 | if (ep->me_key == dummy) |
| 452 | freeslot = ep; |
| 453 | else { |
| 454 | if (ep->me_hash == hash) { |
| 455 | startkey = ep->me_key; |
| 456 | Py_INCREF(startkey); |
| 457 | cmp = PyObject_RichCompareBool(startkey, key, Py_EQ); |
| 458 | Py_DECREF(startkey); |
| 459 | if (cmp < 0) |
| 460 | return NULL; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 461 | if (ep0 == mp->ma_keys->dk_entries && ep->me_key == startkey) { |
| 462 | if (cmp > 0) { |
| 463 | *value_addr = &ep->me_value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 464 | return ep; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 465 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 466 | } |
| 467 | else { |
Victor Stinner | 198b291 | 2012-03-06 01:03:13 +0100 | [diff] [blame] | 468 | PyErr_SetString(PyExc_RuntimeError, |
| 469 | "dictionary changed size during lookup"); |
| 470 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | freeslot = NULL; |
| 474 | } |
Tim Peters | 15d4929 | 2001-05-27 07:39:22 +0000 | [diff] [blame] | 475 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 476 | /* In the loop, me_key == dummy is by far (factor of 100s) the |
| 477 | least likely outcome, so test for that last. */ |
| 478 | for (perturb = hash; ; perturb >>= PERTURB_SHIFT) { |
| 479 | i = (i << 2) + i + perturb + 1; |
| 480 | ep = &ep0[i & mask]; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 481 | if (ep->me_key == NULL) { |
| 482 | if (freeslot == NULL) { |
| 483 | *value_addr = &ep->me_value; |
| 484 | return ep; |
| 485 | } else { |
| 486 | *value_addr = &freeslot->me_value; |
| 487 | return freeslot; |
| 488 | } |
| 489 | } |
| 490 | if (ep->me_key == key) { |
| 491 | *value_addr = &ep->me_value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 492 | return ep; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 493 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 494 | if (ep->me_hash == hash && ep->me_key != dummy) { |
| 495 | startkey = ep->me_key; |
| 496 | Py_INCREF(startkey); |
| 497 | cmp = PyObject_RichCompareBool(startkey, key, Py_EQ); |
| 498 | Py_DECREF(startkey); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 499 | if (cmp < 0) { |
| 500 | *value_addr = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 501 | return NULL; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 502 | } |
| 503 | if (ep0 == mp->ma_keys->dk_entries && ep->me_key == startkey) { |
| 504 | if (cmp > 0) { |
| 505 | *value_addr = &ep->me_value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 506 | return ep; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 507 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 508 | } |
| 509 | else { |
Victor Stinner | 198b291 | 2012-03-06 01:03:13 +0100 | [diff] [blame] | 510 | PyErr_SetString(PyExc_RuntimeError, |
| 511 | "dictionary changed size during lookup"); |
| 512 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 513 | } |
| 514 | } |
| 515 | else if (ep->me_key == dummy && freeslot == NULL) |
| 516 | freeslot = ep; |
| 517 | } |
| 518 | assert(0); /* NOT REACHED */ |
| 519 | return 0; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 522 | /* Specialized version for string-only keys */ |
| 523 | static PyDictKeyEntry * |
| 524 | lookdict_unicode(PyDictObject *mp, PyObject *key, |
| 525 | Py_hash_t hash, PyObject ***value_addr) |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 526 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 527 | register size_t i; |
| 528 | register size_t perturb; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 529 | register PyDictKeyEntry *freeslot; |
| 530 | register size_t mask = DK_MASK(mp->ma_keys); |
| 531 | PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0]; |
| 532 | register PyDictKeyEntry *ep; |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 533 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 534 | /* Make sure this function doesn't have to handle non-unicode keys, |
| 535 | including subclasses of str; e.g., one reason to subclass |
| 536 | unicodes is to override __eq__, and for speed we don't cater to |
| 537 | that here. */ |
| 538 | if (!PyUnicode_CheckExact(key)) { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 539 | mp->ma_keys->dk_lookup = lookdict; |
| 540 | return lookdict(mp, key, hash, value_addr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 541 | } |
Mark Dickinson | 57e683e | 2011-09-24 18:18:40 +0100 | [diff] [blame] | 542 | i = (size_t)hash & mask; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 543 | ep = &ep0[i]; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 544 | if (ep->me_key == NULL || ep->me_key == key) { |
| 545 | *value_addr = &ep->me_value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 546 | return ep; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 547 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 548 | if (ep->me_key == dummy) |
| 549 | freeslot = ep; |
| 550 | else { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 551 | if (ep->me_hash == hash && unicode_eq(ep->me_key, key)) { |
| 552 | *value_addr = &ep->me_value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 553 | return ep; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 554 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 555 | freeslot = NULL; |
| 556 | } |
Tim Peters | 15d4929 | 2001-05-27 07:39:22 +0000 | [diff] [blame] | 557 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 558 | /* In the loop, me_key == dummy is by far (factor of 100s) the |
| 559 | least likely outcome, so test for that last. */ |
| 560 | for (perturb = hash; ; perturb >>= PERTURB_SHIFT) { |
| 561 | i = (i << 2) + i + perturb + 1; |
| 562 | ep = &ep0[i & mask]; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 563 | if (ep->me_key == NULL) { |
| 564 | if (freeslot == NULL) { |
| 565 | *value_addr = &ep->me_value; |
| 566 | return ep; |
| 567 | } else { |
| 568 | *value_addr = &freeslot->me_value; |
| 569 | return freeslot; |
| 570 | } |
| 571 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 572 | if (ep->me_key == key |
| 573 | || (ep->me_hash == hash |
| 574 | && ep->me_key != dummy |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 575 | && unicode_eq(ep->me_key, key))) { |
| 576 | *value_addr = &ep->me_value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 577 | return ep; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 578 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 579 | if (ep->me_key == dummy && freeslot == NULL) |
| 580 | freeslot = ep; |
| 581 | } |
| 582 | assert(0); /* NOT REACHED */ |
| 583 | return 0; |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 586 | /* Faster version of lookdict_unicode when it is known that no <dummy> keys |
| 587 | * will be present. */ |
| 588 | static PyDictKeyEntry * |
| 589 | lookdict_unicode_nodummy(PyDictObject *mp, PyObject *key, |
| 590 | Py_hash_t hash, PyObject ***value_addr) |
| 591 | { |
| 592 | register size_t i; |
| 593 | register size_t perturb; |
| 594 | register size_t mask = DK_MASK(mp->ma_keys); |
| 595 | PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0]; |
| 596 | register PyDictKeyEntry *ep; |
| 597 | |
| 598 | /* Make sure this function doesn't have to handle non-unicode keys, |
| 599 | including subclasses of str; e.g., one reason to subclass |
| 600 | unicodes is to override __eq__, and for speed we don't cater to |
| 601 | that here. */ |
| 602 | if (!PyUnicode_CheckExact(key)) { |
| 603 | mp->ma_keys->dk_lookup = lookdict; |
| 604 | return lookdict(mp, key, hash, value_addr); |
| 605 | } |
| 606 | i = (size_t)hash & mask; |
| 607 | ep = &ep0[i]; |
| 608 | assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key)); |
| 609 | if (ep->me_key == NULL || ep->me_key == key || |
| 610 | (ep->me_hash == hash && unicode_eq(ep->me_key, key))) { |
| 611 | *value_addr = &ep->me_value; |
| 612 | return ep; |
| 613 | } |
| 614 | for (perturb = hash; ; perturb >>= PERTURB_SHIFT) { |
| 615 | i = (i << 2) + i + perturb + 1; |
| 616 | ep = &ep0[i & mask]; |
| 617 | assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key)); |
| 618 | if (ep->me_key == NULL || ep->me_key == key || |
| 619 | (ep->me_hash == hash && unicode_eq(ep->me_key, key))) { |
| 620 | *value_addr = &ep->me_value; |
| 621 | return ep; |
| 622 | } |
| 623 | } |
| 624 | assert(0); /* NOT REACHED */ |
| 625 | return 0; |
| 626 | } |
| 627 | |
| 628 | /* Version of lookdict for split tables. |
| 629 | * All split tables and only split tables use this lookup function. |
| 630 | * Split tables only contain unicode keys and no dummy keys, |
| 631 | * so algorithm is the same as lookdict_unicode_nodummy. |
| 632 | */ |
| 633 | static PyDictKeyEntry * |
| 634 | lookdict_split(PyDictObject *mp, PyObject *key, |
| 635 | Py_hash_t hash, PyObject ***value_addr) |
| 636 | { |
| 637 | register size_t i; |
| 638 | register size_t perturb; |
| 639 | register size_t mask = DK_MASK(mp->ma_keys); |
| 640 | PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0]; |
| 641 | register PyDictKeyEntry *ep; |
| 642 | |
| 643 | if (!PyUnicode_CheckExact(key)) { |
Benjamin Peterson | db780d0 | 2012-04-23 13:44:32 -0400 | [diff] [blame] | 644 | ep = lookdict(mp, key, hash, value_addr); |
| 645 | /* lookdict expects a combined-table, so fix value_addr */ |
| 646 | i = ep - ep0; |
| 647 | *value_addr = &mp->ma_values[i]; |
| 648 | return ep; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 649 | } |
| 650 | i = (size_t)hash & mask; |
| 651 | ep = &ep0[i]; |
| 652 | assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key)); |
| 653 | if (ep->me_key == NULL || ep->me_key == key || |
| 654 | (ep->me_hash == hash && unicode_eq(ep->me_key, key))) { |
| 655 | *value_addr = &mp->ma_values[i]; |
| 656 | return ep; |
| 657 | } |
| 658 | for (perturb = hash; ; perturb >>= PERTURB_SHIFT) { |
| 659 | i = (i << 2) + i + perturb + 1; |
| 660 | ep = &ep0[i & mask]; |
| 661 | assert(ep->me_key == NULL || PyUnicode_CheckExact(ep->me_key)); |
| 662 | if (ep->me_key == NULL || ep->me_key == key || |
| 663 | (ep->me_hash == hash && unicode_eq(ep->me_key, key))) { |
| 664 | *value_addr = &mp->ma_values[i & mask]; |
| 665 | return ep; |
| 666 | } |
| 667 | } |
| 668 | assert(0); /* NOT REACHED */ |
| 669 | return 0; |
| 670 | } |
| 671 | |
Benjamin Peterson | fb88636 | 2010-04-24 18:21:17 +0000 | [diff] [blame] | 672 | int |
| 673 | _PyDict_HasOnlyStringKeys(PyObject *dict) |
| 674 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 675 | Py_ssize_t pos = 0; |
| 676 | PyObject *key, *value; |
Benjamin Peterson | f609654 | 2010-11-17 22:33:12 +0000 | [diff] [blame] | 677 | assert(PyDict_Check(dict)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 678 | /* Shortcut */ |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 679 | if (((PyDictObject *)dict)->ma_keys->dk_lookup != lookdict) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 680 | return 1; |
| 681 | while (PyDict_Next(dict, &pos, &key, &value)) |
| 682 | if (!PyUnicode_Check(key)) |
| 683 | return 0; |
| 684 | return 1; |
Benjamin Peterson | fb88636 | 2010-04-24 18:21:17 +0000 | [diff] [blame] | 685 | } |
| 686 | |
Antoine Pitrou | 3a652b1 | 2009-03-23 18:52:06 +0000 | [diff] [blame] | 687 | #define MAINTAIN_TRACKING(mp, key, value) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 688 | do { \ |
| 689 | if (!_PyObject_GC_IS_TRACKED(mp)) { \ |
| 690 | if (_PyObject_GC_MAY_BE_TRACKED(key) || \ |
| 691 | _PyObject_GC_MAY_BE_TRACKED(value)) { \ |
| 692 | _PyObject_GC_TRACK(mp); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 693 | } \ |
| 694 | } \ |
| 695 | } while(0) |
Antoine Pitrou | 3a652b1 | 2009-03-23 18:52:06 +0000 | [diff] [blame] | 696 | |
| 697 | void |
| 698 | _PyDict_MaybeUntrack(PyObject *op) |
| 699 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 700 | PyDictObject *mp; |
| 701 | PyObject *value; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 702 | Py_ssize_t i, size; |
Antoine Pitrou | 3a652b1 | 2009-03-23 18:52:06 +0000 | [diff] [blame] | 703 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 704 | if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op)) |
| 705 | return; |
| 706 | |
| 707 | mp = (PyDictObject *) op; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 708 | size = DK_SIZE(mp->ma_keys); |
| 709 | if (_PyDict_HasSplitTable(mp)) { |
| 710 | for (i = 0; i < size; i++) { |
| 711 | if ((value = mp->ma_values[i]) == NULL) |
| 712 | continue; |
| 713 | if (_PyObject_GC_MAY_BE_TRACKED(value)) { |
| 714 | assert(!_PyObject_GC_MAY_BE_TRACKED( |
| 715 | mp->ma_keys->dk_entries[i].me_key)); |
| 716 | return; |
| 717 | } |
| 718 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 719 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 720 | else { |
| 721 | PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0]; |
| 722 | for (i = 0; i < size; i++) { |
| 723 | if ((value = ep0[i].me_value) == NULL) |
| 724 | continue; |
| 725 | if (_PyObject_GC_MAY_BE_TRACKED(value) || |
| 726 | _PyObject_GC_MAY_BE_TRACKED(ep0[i].me_key)) |
| 727 | return; |
| 728 | } |
| 729 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 730 | _PyObject_GC_UNTRACK(op); |
Antoine Pitrou | 3a652b1 | 2009-03-23 18:52:06 +0000 | [diff] [blame] | 731 | } |
| 732 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 733 | /* Internal function to find slot for an item from its hash |
| 734 | * when it is known that the key is not present in the dict. |
| 735 | */ |
| 736 | static PyDictKeyEntry * |
| 737 | find_empty_slot(PyDictObject *mp, PyObject *key, Py_hash_t hash, |
| 738 | PyObject ***value_addr) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 739 | { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 740 | size_t i; |
| 741 | size_t perturb; |
| 742 | size_t mask = DK_MASK(mp->ma_keys); |
| 743 | PyDictKeyEntry *ep0 = &mp->ma_keys->dk_entries[0]; |
| 744 | PyDictKeyEntry *ep; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 745 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 746 | assert(key != NULL); |
| 747 | if (!PyUnicode_CheckExact(key)) |
| 748 | mp->ma_keys->dk_lookup = lookdict; |
| 749 | i = hash & mask; |
| 750 | ep = &ep0[i]; |
| 751 | for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) { |
| 752 | i = (i << 2) + i + perturb + 1; |
| 753 | ep = &ep0[i & mask]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 754 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 755 | assert(ep->me_value == NULL); |
| 756 | if (mp->ma_values) |
| 757 | *value_addr = &mp->ma_values[i & mask]; |
| 758 | else |
| 759 | *value_addr = &ep->me_value; |
| 760 | return ep; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 761 | } |
| 762 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 763 | static int |
| 764 | insertion_resize(PyDictObject *mp) |
| 765 | { |
| 766 | /* |
| 767 | * Double the size of the dict, |
| 768 | * Previous versions quadrupled size, but doing so may result in excessive |
| 769 | * memory use. Doubling keeps the number of resizes low without wasting |
| 770 | * too much memory. |
| 771 | */ |
| 772 | return dictresize(mp, 2 * mp->ma_used); |
| 773 | } |
Antoine Pitrou | e965d97 | 2012-02-27 00:45:12 +0100 | [diff] [blame] | 774 | |
| 775 | /* |
| 776 | Internal routine to insert a new item into the table. |
| 777 | Used both by the internal resize routine and by the public insert routine. |
Antoine Pitrou | e965d97 | 2012-02-27 00:45:12 +0100 | [diff] [blame] | 778 | Returns -1 if an error occurred, or 0 on success. |
| 779 | */ |
| 780 | static int |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 781 | insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value) |
Antoine Pitrou | e965d97 | 2012-02-27 00:45:12 +0100 | [diff] [blame] | 782 | { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 783 | PyObject *old_value; |
| 784 | PyObject **value_addr; |
| 785 | PyDictKeyEntry *ep; |
| 786 | assert(key != dummy); |
Antoine Pitrou | e965d97 | 2012-02-27 00:45:12 +0100 | [diff] [blame] | 787 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 788 | if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) { |
| 789 | if (insertion_resize(mp) < 0) |
| 790 | return -1; |
| 791 | } |
| 792 | |
| 793 | ep = mp->ma_keys->dk_lookup(mp, key, hash, &value_addr); |
Antoine Pitrou | e965d97 | 2012-02-27 00:45:12 +0100 | [diff] [blame] | 794 | if (ep == NULL) { |
Antoine Pitrou | e965d97 | 2012-02-27 00:45:12 +0100 | [diff] [blame] | 795 | return -1; |
| 796 | } |
Benjamin Peterson | a6f195e | 2012-04-30 10:23:40 -0400 | [diff] [blame^] | 797 | Py_INCREF(value); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 798 | MAINTAIN_TRACKING(mp, key, value); |
| 799 | old_value = *value_addr; |
| 800 | if (old_value != NULL) { |
| 801 | assert(ep->me_key != NULL && ep->me_key != dummy); |
| 802 | *value_addr = value; |
| 803 | Py_DECREF(old_value); /* which **CAN** re-enter */ |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 804 | } |
| 805 | else { |
| 806 | if (ep->me_key == NULL) { |
Benjamin Peterson | a6f195e | 2012-04-30 10:23:40 -0400 | [diff] [blame^] | 807 | Py_INCREF(key); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 808 | if (mp->ma_keys->dk_usable <= 0) { |
| 809 | /* Need to resize. */ |
| 810 | if (insertion_resize(mp) < 0) { |
| 811 | Py_DECREF(key); |
| 812 | Py_DECREF(value); |
| 813 | return -1; |
| 814 | } |
| 815 | ep = find_empty_slot(mp, key, hash, &value_addr); |
| 816 | } |
| 817 | mp->ma_keys->dk_usable--; |
| 818 | assert(mp->ma_keys->dk_usable >= 0); |
| 819 | ep->me_key = key; |
| 820 | ep->me_hash = hash; |
| 821 | } |
| 822 | else { |
| 823 | if (ep->me_key == dummy) { |
Benjamin Peterson | a6f195e | 2012-04-30 10:23:40 -0400 | [diff] [blame^] | 824 | Py_INCREF(key); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 825 | ep->me_key = key; |
| 826 | ep->me_hash = hash; |
| 827 | Py_DECREF(dummy); |
| 828 | } else { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 829 | assert(_PyDict_HasSplitTable(mp)); |
| 830 | } |
| 831 | } |
| 832 | mp->ma_used++; |
| 833 | *value_addr = value; |
| 834 | } |
| 835 | assert(ep->me_key != NULL && ep->me_key != dummy); |
| 836 | assert(PyUnicode_CheckExact(key) || mp->ma_keys->dk_lookup == lookdict); |
| 837 | return 0; |
Antoine Pitrou | e965d97 | 2012-02-27 00:45:12 +0100 | [diff] [blame] | 838 | } |
| 839 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 840 | /* |
| 841 | Internal routine used by dictresize() to insert an item which is |
| 842 | known to be absent from the dict. This routine also assumes that |
| 843 | the dict contains no deleted entries. Besides the performance benefit, |
| 844 | using insertdict() in dictresize() is dangerous (SF bug #1456209). |
| 845 | Note that no refcounts are changed by this routine; if needed, the caller |
| 846 | is responsible for incref'ing `key` and `value`. |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 847 | Neither mp->ma_used nor k->dk_usable are modified by this routine; the caller |
| 848 | must set them correctly |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 849 | */ |
| 850 | static void |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 851 | insertdict_clean(PyDictObject *mp, PyObject *key, Py_hash_t hash, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 852 | PyObject *value) |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 853 | { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 854 | size_t i; |
| 855 | size_t perturb; |
| 856 | PyDictKeysObject *k = mp->ma_keys; |
| 857 | size_t mask = (size_t)DK_SIZE(k)-1; |
| 858 | PyDictKeyEntry *ep0 = &k->dk_entries[0]; |
| 859 | PyDictKeyEntry *ep; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 860 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 861 | assert(k->dk_lookup != NULL); |
| 862 | assert(value != NULL); |
| 863 | assert(key != NULL); |
| 864 | assert(key != dummy); |
| 865 | assert(PyUnicode_CheckExact(key) || k->dk_lookup == lookdict); |
| 866 | i = hash & mask; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 867 | ep = &ep0[i]; |
| 868 | for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) { |
| 869 | i = (i << 2) + i + perturb + 1; |
| 870 | ep = &ep0[i & mask]; |
| 871 | } |
| 872 | assert(ep->me_value == NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 873 | ep->me_key = key; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 874 | ep->me_hash = hash; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 875 | ep->me_value = value; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | /* |
| 879 | Restructure the table by allocating a new table and reinserting all |
| 880 | items again. When entries have been deleted, the new table may |
| 881 | actually be smaller than the old one. |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 882 | If a table is split (its keys and hashes are shared, its values are not), |
| 883 | then the values are temporarily copied into the table, it is resized as |
| 884 | a combined table, then the me_value slots in the old table are NULLed out. |
| 885 | After resizing a table is always combined, |
| 886 | but can be resplit by make_keys_shared(). |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 887 | */ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 888 | static int |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 889 | dictresize(PyDictObject *mp, Py_ssize_t minused) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 890 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 891 | Py_ssize_t newsize; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 892 | PyDictKeysObject *oldkeys; |
| 893 | PyObject **oldvalues; |
| 894 | Py_ssize_t i, oldsize; |
Tim Peters | 91a364d | 2001-05-19 07:04:38 +0000 | [diff] [blame] | 895 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 896 | /* Find the smallest table size > minused. */ |
| 897 | for (newsize = PyDict_MINSIZE_COMBINED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 898 | newsize <= minused && newsize > 0; |
| 899 | newsize <<= 1) |
| 900 | ; |
| 901 | if (newsize <= 0) { |
| 902 | PyErr_NoMemory(); |
| 903 | return -1; |
| 904 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 905 | oldkeys = mp->ma_keys; |
| 906 | oldvalues = mp->ma_values; |
| 907 | /* Allocate a new table. */ |
| 908 | mp->ma_keys = new_keys_object(newsize); |
| 909 | if (mp->ma_keys == NULL) { |
| 910 | mp->ma_keys = oldkeys; |
| 911 | return -1; |
| 912 | } |
| 913 | if (oldkeys->dk_lookup == lookdict) |
| 914 | mp->ma_keys->dk_lookup = lookdict; |
| 915 | oldsize = DK_SIZE(oldkeys); |
| 916 | mp->ma_values = NULL; |
| 917 | /* If empty then nothing to copy so just return */ |
| 918 | if (oldsize == 1) { |
| 919 | assert(oldkeys == Py_EMPTY_KEYS); |
| 920 | DK_DECREF(oldkeys); |
| 921 | return 0; |
| 922 | } |
| 923 | /* Main loop below assumes we can transfer refcount to new keys |
| 924 | * and that value is stored in me_value. |
| 925 | * Increment ref-counts and copy values here to compensate |
| 926 | * This (resizing a split table) should be relatively rare */ |
| 927 | if (oldvalues != NULL) { |
| 928 | for (i = 0; i < oldsize; i++) { |
| 929 | if (oldvalues[i] != NULL) { |
| 930 | Py_INCREF(oldkeys->dk_entries[i].me_key); |
| 931 | oldkeys->dk_entries[i].me_value = oldvalues[i]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 932 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 933 | } |
| 934 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 935 | /* Main loop */ |
| 936 | for (i = 0; i < oldsize; i++) { |
| 937 | PyDictKeyEntry *ep = &oldkeys->dk_entries[i]; |
| 938 | if (ep->me_value != NULL) { |
| 939 | assert(ep->me_key != dummy); |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 940 | insertdict_clean(mp, ep->me_key, ep->me_hash, ep->me_value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 941 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 942 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 943 | mp->ma_keys->dk_usable -= mp->ma_used; |
| 944 | if (oldvalues != NULL) { |
| 945 | /* NULL out me_value slot in oldkeys, in case it was shared */ |
| 946 | for (i = 0; i < oldsize; i++) |
| 947 | oldkeys->dk_entries[i].me_value = NULL; |
| 948 | assert(oldvalues != empty_values); |
| 949 | free_values(oldvalues); |
| 950 | DK_DECREF(oldkeys); |
| 951 | } |
| 952 | else { |
| 953 | assert(oldkeys->dk_lookup != lookdict_split); |
| 954 | if (oldkeys->dk_lookup != lookdict_unicode_nodummy) { |
| 955 | PyDictKeyEntry *ep0 = &oldkeys->dk_entries[0]; |
| 956 | for (i = 0; i < oldsize; i++) { |
| 957 | if (ep0[i].me_key == dummy) |
| 958 | Py_DECREF(dummy); |
| 959 | } |
| 960 | } |
| 961 | assert(oldkeys->dk_refcnt == 1); |
| 962 | PyMem_FREE(oldkeys); |
| 963 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 964 | return 0; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 965 | } |
| 966 | |
Benjamin Peterson | 15ee821 | 2012-04-24 14:44:18 -0400 | [diff] [blame] | 967 | /* Returns NULL if unable to split table. |
| 968 | * A NULL return does not necessarily indicate an error */ |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 969 | static PyDictKeysObject * |
| 970 | make_keys_shared(PyObject *op) |
| 971 | { |
| 972 | Py_ssize_t i; |
| 973 | Py_ssize_t size; |
| 974 | PyDictObject *mp = (PyDictObject *)op; |
| 975 | |
Benjamin Peterson | 15ee821 | 2012-04-24 14:44:18 -0400 | [diff] [blame] | 976 | if (!PyDict_CheckExact(op)) |
| 977 | return NULL; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 978 | if (!_PyDict_HasSplitTable(mp)) { |
| 979 | PyDictKeyEntry *ep0; |
| 980 | PyObject **values; |
| 981 | assert(mp->ma_keys->dk_refcnt == 1); |
| 982 | if (mp->ma_keys->dk_lookup == lookdict) { |
| 983 | return NULL; |
| 984 | } |
| 985 | else if (mp->ma_keys->dk_lookup == lookdict_unicode) { |
| 986 | /* Remove dummy keys */ |
| 987 | if (dictresize(mp, DK_SIZE(mp->ma_keys))) |
| 988 | return NULL; |
| 989 | } |
| 990 | assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy); |
| 991 | /* Copy values into a new array */ |
| 992 | ep0 = &mp->ma_keys->dk_entries[0]; |
| 993 | size = DK_SIZE(mp->ma_keys); |
| 994 | values = new_values(size); |
| 995 | if (values == NULL) { |
| 996 | PyErr_SetString(PyExc_MemoryError, |
| 997 | "Not enough memory to allocate new values array"); |
| 998 | return NULL; |
| 999 | } |
| 1000 | for (i = 0; i < size; i++) { |
| 1001 | values[i] = ep0[i].me_value; |
| 1002 | ep0[i].me_value = NULL; |
| 1003 | } |
| 1004 | mp->ma_keys->dk_lookup = lookdict_split; |
| 1005 | mp->ma_values = values; |
| 1006 | } |
| 1007 | DK_INCREF(mp->ma_keys); |
| 1008 | return mp->ma_keys; |
| 1009 | } |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 1010 | |
| 1011 | PyObject * |
| 1012 | _PyDict_NewPresized(Py_ssize_t minused) |
| 1013 | { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1014 | Py_ssize_t newsize; |
| 1015 | PyDictKeysObject *new_keys; |
| 1016 | for (newsize = PyDict_MINSIZE_COMBINED; |
| 1017 | newsize <= minused && newsize > 0; |
| 1018 | newsize <<= 1) |
| 1019 | ; |
| 1020 | new_keys = new_keys_object(newsize); |
| 1021 | if (new_keys == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1022 | return NULL; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1023 | return new_dict(new_keys, NULL); |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 1024 | } |
| 1025 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1026 | /* Note that, for historical reasons, PyDict_GetItem() suppresses all errors |
| 1027 | * that may occur (originally dicts supported only string keys, and exceptions |
| 1028 | * weren't possible). So, while the original intent was that a NULL return |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1029 | * meant the key wasn't present, in reality it can mean that, or that an error |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1030 | * (suppressed) occurred while computing the key's hash, or that some error |
| 1031 | * (suppressed) occurred when comparing keys in the dict's internal probe |
| 1032 | * sequence. A nasty example of the latter is when a Python-coded comparison |
| 1033 | * function hits a stack-depth error, which can cause this to return NULL |
| 1034 | * even if the key is present. |
| 1035 | */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1036 | PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 1037 | PyDict_GetItem(PyObject *op, PyObject *key) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1038 | { |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 1039 | Py_hash_t hash; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1040 | PyDictObject *mp = (PyDictObject *)op; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1041 | PyDictKeyEntry *ep; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1042 | PyThreadState *tstate; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1043 | PyObject **value_addr; |
| 1044 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1045 | if (!PyDict_Check(op)) |
| 1046 | return NULL; |
| 1047 | if (!PyUnicode_CheckExact(key) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1048 | (hash = ((PyASCIIObject *) key)->hash) == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1049 | { |
| 1050 | hash = PyObject_Hash(key); |
| 1051 | if (hash == -1) { |
| 1052 | PyErr_Clear(); |
| 1053 | return NULL; |
| 1054 | } |
| 1055 | } |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1056 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1057 | /* We can arrive here with a NULL tstate during initialization: try |
| 1058 | running "python -Wi" for an example related to string interning. |
| 1059 | Let's just hope that no exception occurs then... This must be |
| 1060 | _PyThreadState_Current and not PyThreadState_GET() because in debug |
| 1061 | mode, the latter complains if tstate is NULL. */ |
| 1062 | tstate = (PyThreadState*)_Py_atomic_load_relaxed( |
| 1063 | &_PyThreadState_Current); |
| 1064 | if (tstate != NULL && tstate->curexc_type != NULL) { |
| 1065 | /* preserve the existing exception */ |
| 1066 | PyObject *err_type, *err_value, *err_tb; |
| 1067 | PyErr_Fetch(&err_type, &err_value, &err_tb); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1068 | ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1069 | /* ignore errors */ |
| 1070 | PyErr_Restore(err_type, err_value, err_tb); |
| 1071 | if (ep == NULL) |
| 1072 | return NULL; |
| 1073 | } |
| 1074 | else { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1075 | ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1076 | if (ep == NULL) { |
| 1077 | PyErr_Clear(); |
| 1078 | return NULL; |
| 1079 | } |
| 1080 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1081 | return *value_addr; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1082 | } |
| 1083 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 1084 | /* Variant of PyDict_GetItem() that doesn't suppress exceptions. |
| 1085 | This returns NULL *with* an exception set if an exception occurred. |
| 1086 | It returns NULL *without* an exception set if the key wasn't present. |
| 1087 | */ |
| 1088 | PyObject * |
| 1089 | PyDict_GetItemWithError(PyObject *op, PyObject *key) |
| 1090 | { |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 1091 | Py_hash_t hash; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1092 | PyDictObject*mp = (PyDictObject *)op; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1093 | PyDictKeyEntry *ep; |
| 1094 | PyObject **value_addr; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 1095 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1096 | if (!PyDict_Check(op)) { |
| 1097 | PyErr_BadInternalCall(); |
| 1098 | return NULL; |
| 1099 | } |
| 1100 | if (!PyUnicode_CheckExact(key) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1101 | (hash = ((PyASCIIObject *) key)->hash) == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1102 | { |
| 1103 | hash = PyObject_Hash(key); |
| 1104 | if (hash == -1) { |
| 1105 | return NULL; |
| 1106 | } |
| 1107 | } |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 1108 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1109 | ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1110 | if (ep == NULL) |
| 1111 | return NULL; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1112 | return *value_addr; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1115 | PyObject * |
| 1116 | _PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key) |
| 1117 | { |
| 1118 | PyObject *kv; |
| 1119 | kv = _PyUnicode_FromId(key); /* borrowed */ |
| 1120 | if (kv == NULL) |
| 1121 | return NULL; |
| 1122 | return PyDict_GetItemWithError(dp, kv); |
| 1123 | } |
| 1124 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1125 | /* Fast version of global value lookup. |
| 1126 | * Lookup in globals, then builtins. |
| 1127 | */ |
| 1128 | PyObject * |
| 1129 | _PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1130 | { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1131 | PyObject *x; |
| 1132 | if (PyUnicode_CheckExact(key)) { |
| 1133 | PyObject **value_addr; |
| 1134 | Py_hash_t hash = ((PyASCIIObject *)key)->hash; |
| 1135 | if (hash != -1) { |
| 1136 | PyDictKeyEntry *e; |
| 1137 | e = globals->ma_keys->dk_lookup(globals, key, hash, &value_addr); |
| 1138 | if (e == NULL) { |
| 1139 | return NULL; |
| 1140 | } |
| 1141 | x = *value_addr; |
| 1142 | if (x != NULL) |
| 1143 | return x; |
| 1144 | e = builtins->ma_keys->dk_lookup(builtins, key, hash, &value_addr); |
| 1145 | if (e == NULL) { |
| 1146 | return NULL; |
| 1147 | } |
| 1148 | x = *value_addr; |
| 1149 | return x; |
| 1150 | } |
Antoine Pitrou | e965d97 | 2012-02-27 00:45:12 +0100 | [diff] [blame] | 1151 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1152 | x = PyDict_GetItemWithError((PyObject *)globals, key); |
| 1153 | if (x != NULL) |
| 1154 | return x; |
| 1155 | if (PyErr_Occurred()) |
| 1156 | return NULL; |
| 1157 | return PyDict_GetItemWithError((PyObject *)builtins, key); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
Antoine Pitrou | e965d97 | 2012-02-27 00:45:12 +0100 | [diff] [blame] | 1160 | /* CAUTION: PyDict_SetItem() must guarantee that it won't resize the |
| 1161 | * dictionary if it's merely replacing the value for an existing key. |
| 1162 | * This means that it's safe to loop over a dictionary with PyDict_Next() |
| 1163 | * and occasionally replace a value -- but you can't insert new keys or |
| 1164 | * remove them. |
| 1165 | */ |
| 1166 | int |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1167 | PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value) |
Antoine Pitrou | e965d97 | 2012-02-27 00:45:12 +0100 | [diff] [blame] | 1168 | { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1169 | PyDictObject *mp; |
| 1170 | Py_hash_t hash; |
Antoine Pitrou | e965d97 | 2012-02-27 00:45:12 +0100 | [diff] [blame] | 1171 | if (!PyDict_Check(op)) { |
| 1172 | PyErr_BadInternalCall(); |
| 1173 | return -1; |
| 1174 | } |
| 1175 | assert(key); |
| 1176 | assert(value); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1177 | mp = (PyDictObject *)op; |
| 1178 | if (!PyUnicode_CheckExact(key) || |
| 1179 | (hash = ((PyASCIIObject *) key)->hash) == -1) |
| 1180 | { |
Antoine Pitrou | e965d97 | 2012-02-27 00:45:12 +0100 | [diff] [blame] | 1181 | hash = PyObject_Hash(key); |
| 1182 | if (hash == -1) |
| 1183 | return -1; |
| 1184 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1185 | |
| 1186 | /* insertdict() handles any resizing that might be necessary */ |
| 1187 | return insertdict(mp, key, hash, value); |
Antoine Pitrou | e965d97 | 2012-02-27 00:45:12 +0100 | [diff] [blame] | 1188 | } |
| 1189 | |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1190 | int |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 1191 | PyDict_DelItem(PyObject *op, PyObject *key) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1192 | { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1193 | PyDictObject *mp; |
| 1194 | Py_hash_t hash; |
| 1195 | PyDictKeyEntry *ep; |
| 1196 | PyObject *old_key, *old_value; |
| 1197 | PyObject **value_addr; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 1198 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1199 | if (!PyDict_Check(op)) { |
| 1200 | PyErr_BadInternalCall(); |
| 1201 | return -1; |
| 1202 | } |
| 1203 | assert(key); |
| 1204 | if (!PyUnicode_CheckExact(key) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1205 | (hash = ((PyASCIIObject *) key)->hash) == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1206 | hash = PyObject_Hash(key); |
| 1207 | if (hash == -1) |
| 1208 | return -1; |
| 1209 | } |
| 1210 | mp = (PyDictObject *)op; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1211 | ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1212 | if (ep == NULL) |
| 1213 | return -1; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1214 | if (*value_addr == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1215 | set_key_error(key); |
| 1216 | return -1; |
| 1217 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1218 | old_value = *value_addr; |
| 1219 | *value_addr = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1220 | mp->ma_used--; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1221 | if (!_PyDict_HasSplitTable(mp)) { |
| 1222 | ENSURE_ALLOWS_DELETIONS(mp); |
| 1223 | old_key = ep->me_key; |
| 1224 | Py_INCREF(dummy); |
| 1225 | ep->me_key = dummy; |
| 1226 | Py_DECREF(old_key); |
| 1227 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1228 | Py_DECREF(old_value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1229 | return 0; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1232 | void |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 1233 | PyDict_Clear(PyObject *op) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1234 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1235 | PyDictObject *mp; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1236 | PyDictKeysObject *oldkeys; |
| 1237 | PyObject **oldvalues; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1238 | Py_ssize_t i, n; |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 1239 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1240 | if (!PyDict_Check(op)) |
| 1241 | return; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1242 | mp = ((PyDictObject *)op); |
| 1243 | oldkeys = mp->ma_keys; |
| 1244 | oldvalues = mp->ma_values; |
| 1245 | if (oldvalues == empty_values) |
| 1246 | return; |
| 1247 | /* Empty the dict... */ |
| 1248 | DK_INCREF(Py_EMPTY_KEYS); |
| 1249 | mp->ma_keys = Py_EMPTY_KEYS; |
| 1250 | mp->ma_values = empty_values; |
| 1251 | mp->ma_used = 0; |
| 1252 | /* ...then clear the keys and values */ |
| 1253 | if (oldvalues != NULL) { |
| 1254 | n = DK_SIZE(oldkeys); |
| 1255 | for (i = 0; i < n; i++) |
| 1256 | Py_CLEAR(oldvalues[i]); |
| 1257 | free_values(oldvalues); |
| 1258 | DK_DECREF(oldkeys); |
| 1259 | } |
| 1260 | else { |
| 1261 | assert(oldkeys->dk_refcnt == 1); |
| 1262 | free_keys_object(oldkeys); |
| 1263 | } |
| 1264 | } |
| 1265 | |
| 1266 | /* Returns -1 if no more items (or op is not a dict), |
| 1267 | * index of item otherwise. Stores value in pvalue |
| 1268 | */ |
| 1269 | Py_LOCAL_INLINE(Py_ssize_t) |
| 1270 | dict_next(PyObject *op, Py_ssize_t i, PyObject **pvalue) |
| 1271 | { |
| 1272 | Py_ssize_t mask, offset; |
| 1273 | PyDictObject *mp; |
| 1274 | PyObject **value_ptr; |
| 1275 | |
| 1276 | |
| 1277 | if (!PyDict_Check(op)) |
| 1278 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1279 | mp = (PyDictObject *)op; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1280 | if (i < 0) |
| 1281 | return -1; |
| 1282 | if (mp->ma_values) { |
| 1283 | value_ptr = &mp->ma_values[i]; |
| 1284 | offset = sizeof(PyObject *); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1285 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1286 | else { |
| 1287 | value_ptr = &mp->ma_keys->dk_entries[i].me_value; |
| 1288 | offset = sizeof(PyDictKeyEntry); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1289 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1290 | mask = DK_MASK(mp->ma_keys); |
| 1291 | while (i <= mask && *value_ptr == NULL) { |
| 1292 | value_ptr = (PyObject **)(((char *)value_ptr) + offset); |
| 1293 | i++; |
| 1294 | } |
| 1295 | if (i > mask) |
| 1296 | return -1; |
| 1297 | if (pvalue) |
| 1298 | *pvalue = *value_ptr; |
| 1299 | return i; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1300 | } |
| 1301 | |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 1302 | /* |
| 1303 | * Iterate over a dict. Use like so: |
| 1304 | * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1305 | * Py_ssize_t i; |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 1306 | * PyObject *key, *value; |
| 1307 | * i = 0; # important! i should not otherwise be changed by you |
Neal Norwitz | 0732301 | 2003-02-15 14:45:12 +0000 | [diff] [blame] | 1308 | * while (PyDict_Next(yourdict, &i, &key, &value)) { |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 1309 | * Refer to borrowed references in key and value. |
| 1310 | * } |
| 1311 | * |
| 1312 | * 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] | 1313 | * mutates the dict. One exception: it is safe if the loop merely changes |
| 1314 | * the values associated with the keys (but doesn't insert new keys or |
| 1315 | * delete keys), via PyDict_SetItem(). |
| 1316 | */ |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1317 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1318 | 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] | 1319 | { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1320 | PyDictObject *mp; |
| 1321 | Py_ssize_t i = dict_next(op, *ppos, pvalue); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1322 | if (i < 0) |
| 1323 | return 0; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1324 | mp = (PyDictObject *)op; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1325 | *ppos = i+1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1326 | if (pkey) |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1327 | *pkey = mp->ma_keys->dk_entries[i].me_key; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1328 | return 1; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1329 | } |
| 1330 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1331 | /* Internal version of PyDict_Next that returns a hash value in addition |
| 1332 | * to the key and value. |
| 1333 | */ |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1334 | int |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1335 | _PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, |
| 1336 | PyObject **pvalue, Py_hash_t *phash) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1337 | { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1338 | PyDictObject *mp; |
| 1339 | Py_ssize_t i = dict_next(op, *ppos, pvalue); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1340 | if (i < 0) |
| 1341 | return 0; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1342 | mp = (PyDictObject *)op; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1343 | *ppos = i+1; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1344 | *phash = mp->ma_keys->dk_entries[i].me_hash; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1345 | if (pkey) |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1346 | *pkey = mp->ma_keys->dk_entries[i].me_key; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1347 | return 1; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1348 | } |
| 1349 | |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1350 | /* Methods */ |
| 1351 | |
| 1352 | static void |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1353 | dict_dealloc(PyDictObject *mp) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1354 | { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1355 | PyObject **values = mp->ma_values; |
| 1356 | PyDictKeysObject *keys = mp->ma_keys; |
| 1357 | Py_ssize_t i, n; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1358 | PyObject_GC_UnTrack(mp); |
| 1359 | Py_TRASHCAN_SAFE_BEGIN(mp) |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1360 | if (values != NULL) { |
| 1361 | if (values != empty_values) { |
| 1362 | for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) { |
| 1363 | Py_XDECREF(values[i]); |
| 1364 | } |
| 1365 | free_values(values); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1366 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1367 | DK_DECREF(keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1368 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1369 | else { |
| 1370 | free_keys_object(keys); |
| 1371 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1372 | if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type) |
| 1373 | free_list[numfree++] = mp; |
| 1374 | else |
| 1375 | Py_TYPE(mp)->tp_free((PyObject *)mp); |
| 1376 | Py_TRASHCAN_SAFE_END(mp) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1379 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1380 | static PyObject * |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1381 | dict_repr(PyDictObject *mp) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1382 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1383 | Py_ssize_t i; |
| 1384 | PyObject *s, *temp, *colon = NULL; |
| 1385 | PyObject *pieces = NULL, *result = NULL; |
| 1386 | PyObject *key, *value; |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 1387 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1388 | i = Py_ReprEnter((PyObject *)mp); |
| 1389 | if (i != 0) { |
| 1390 | return i > 0 ? PyUnicode_FromString("{...}") : NULL; |
| 1391 | } |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 1392 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1393 | if (mp->ma_used == 0) { |
| 1394 | result = PyUnicode_FromString("{}"); |
| 1395 | goto Done; |
| 1396 | } |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1397 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1398 | pieces = PyList_New(0); |
| 1399 | if (pieces == NULL) |
| 1400 | goto Done; |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1401 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1402 | colon = PyUnicode_FromString(": "); |
| 1403 | if (colon == NULL) |
| 1404 | goto Done; |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1405 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1406 | /* Do repr() on each key+value pair, and insert ": " between them. |
| 1407 | Note that repr may mutate the dict. */ |
| 1408 | i = 0; |
| 1409 | while (PyDict_Next((PyObject *)mp, &i, &key, &value)) { |
| 1410 | int status; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1411 | /* Prevent repr from deleting key or value during key format. */ |
| 1412 | Py_INCREF(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1413 | Py_INCREF(value); |
| 1414 | s = PyObject_Repr(key); |
| 1415 | PyUnicode_Append(&s, colon); |
| 1416 | PyUnicode_AppendAndDel(&s, PyObject_Repr(value)); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1417 | Py_DECREF(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1418 | Py_DECREF(value); |
| 1419 | if (s == NULL) |
| 1420 | goto Done; |
| 1421 | status = PyList_Append(pieces, s); |
| 1422 | Py_DECREF(s); /* append created a new ref */ |
| 1423 | if (status < 0) |
| 1424 | goto Done; |
| 1425 | } |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1426 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1427 | /* Add "{}" decorations to the first and last items. */ |
| 1428 | assert(PyList_GET_SIZE(pieces) > 0); |
| 1429 | s = PyUnicode_FromString("{"); |
| 1430 | if (s == NULL) |
| 1431 | goto Done; |
| 1432 | temp = PyList_GET_ITEM(pieces, 0); |
| 1433 | PyUnicode_AppendAndDel(&s, temp); |
| 1434 | PyList_SET_ITEM(pieces, 0, s); |
| 1435 | if (s == NULL) |
| 1436 | goto Done; |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1437 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1438 | s = PyUnicode_FromString("}"); |
| 1439 | if (s == NULL) |
| 1440 | goto Done; |
| 1441 | temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1); |
| 1442 | PyUnicode_AppendAndDel(&temp, s); |
| 1443 | PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp); |
| 1444 | if (temp == NULL) |
| 1445 | goto Done; |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1446 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1447 | /* Paste them all together with ", " between. */ |
| 1448 | s = PyUnicode_FromString(", "); |
| 1449 | if (s == NULL) |
| 1450 | goto Done; |
| 1451 | result = PyUnicode_Join(s, pieces); |
| 1452 | Py_DECREF(s); |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1453 | |
| 1454 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1455 | Py_XDECREF(pieces); |
| 1456 | Py_XDECREF(colon); |
| 1457 | Py_ReprLeave((PyObject *)mp); |
| 1458 | return result; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1459 | } |
| 1460 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1461 | static Py_ssize_t |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1462 | dict_length(PyDictObject *mp) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1463 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1464 | return mp->ma_used; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1465 | } |
| 1466 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1467 | static PyObject * |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1468 | dict_subscript(PyDictObject *mp, register PyObject *key) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1469 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1470 | PyObject *v; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 1471 | Py_hash_t hash; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1472 | PyDictKeyEntry *ep; |
| 1473 | PyObject **value_addr; |
| 1474 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1475 | if (!PyUnicode_CheckExact(key) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1476 | (hash = ((PyASCIIObject *) key)->hash) == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1477 | hash = PyObject_Hash(key); |
| 1478 | if (hash == -1) |
| 1479 | return NULL; |
| 1480 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1481 | ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1482 | if (ep == NULL) |
| 1483 | return NULL; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1484 | v = *value_addr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1485 | if (v == NULL) { |
| 1486 | if (!PyDict_CheckExact(mp)) { |
| 1487 | /* Look up __missing__ method if we're a subclass. */ |
| 1488 | PyObject *missing, *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1489 | _Py_IDENTIFIER(__missing__); |
| 1490 | missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1491 | if (missing != NULL) { |
| 1492 | res = PyObject_CallFunctionObjArgs(missing, |
| 1493 | key, NULL); |
| 1494 | Py_DECREF(missing); |
| 1495 | return res; |
| 1496 | } |
| 1497 | else if (PyErr_Occurred()) |
| 1498 | return NULL; |
| 1499 | } |
| 1500 | set_key_error(key); |
| 1501 | return NULL; |
| 1502 | } |
| 1503 | else |
| 1504 | Py_INCREF(v); |
| 1505 | return v; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1506 | } |
| 1507 | |
| 1508 | static int |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1509 | dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1510 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1511 | if (w == NULL) |
| 1512 | return PyDict_DelItem((PyObject *)mp, v); |
| 1513 | else |
| 1514 | return PyDict_SetItem((PyObject *)mp, v, w); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1515 | } |
| 1516 | |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 1517 | static PyMappingMethods dict_as_mapping = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1518 | (lenfunc)dict_length, /*mp_length*/ |
| 1519 | (binaryfunc)dict_subscript, /*mp_subscript*/ |
| 1520 | (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1521 | }; |
| 1522 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1523 | static PyObject * |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1524 | dict_keys(register PyDictObject *mp) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1525 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1526 | register PyObject *v; |
| 1527 | register Py_ssize_t i, j; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1528 | PyDictKeyEntry *ep; |
| 1529 | Py_ssize_t size, n, offset; |
| 1530 | PyObject **value_ptr; |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1531 | |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1532 | again: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1533 | n = mp->ma_used; |
| 1534 | v = PyList_New(n); |
| 1535 | if (v == NULL) |
| 1536 | return NULL; |
| 1537 | if (n != mp->ma_used) { |
| 1538 | /* Durnit. The allocations caused the dict to resize. |
| 1539 | * Just start over, this shouldn't normally happen. |
| 1540 | */ |
| 1541 | Py_DECREF(v); |
| 1542 | goto again; |
| 1543 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1544 | ep = &mp->ma_keys->dk_entries[0]; |
| 1545 | size = DK_SIZE(mp->ma_keys); |
| 1546 | if (mp->ma_values) { |
| 1547 | value_ptr = mp->ma_values; |
| 1548 | offset = sizeof(PyObject *); |
| 1549 | } |
| 1550 | else { |
| 1551 | value_ptr = &ep[0].me_value; |
| 1552 | offset = sizeof(PyDictKeyEntry); |
| 1553 | } |
| 1554 | for (i = 0, j = 0; i < size; i++) { |
| 1555 | if (*value_ptr != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1556 | PyObject *key = ep[i].me_key; |
| 1557 | Py_INCREF(key); |
| 1558 | PyList_SET_ITEM(v, j, key); |
| 1559 | j++; |
| 1560 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1561 | value_ptr = (PyObject **)(((char *)value_ptr) + offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1562 | } |
| 1563 | assert(j == n); |
| 1564 | return v; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1565 | } |
| 1566 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1567 | static PyObject * |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1568 | dict_values(register PyDictObject *mp) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1569 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1570 | register PyObject *v; |
| 1571 | register Py_ssize_t i, j; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1572 | Py_ssize_t size, n, offset; |
| 1573 | PyObject **value_ptr; |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1574 | |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1575 | again: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1576 | n = mp->ma_used; |
| 1577 | v = PyList_New(n); |
| 1578 | if (v == NULL) |
| 1579 | return NULL; |
| 1580 | if (n != mp->ma_used) { |
| 1581 | /* Durnit. The allocations caused the dict to resize. |
| 1582 | * Just start over, this shouldn't normally happen. |
| 1583 | */ |
| 1584 | Py_DECREF(v); |
| 1585 | goto again; |
| 1586 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1587 | size = DK_SIZE(mp->ma_keys); |
| 1588 | if (mp->ma_values) { |
| 1589 | value_ptr = mp->ma_values; |
| 1590 | offset = sizeof(PyObject *); |
| 1591 | } |
| 1592 | else { |
| 1593 | value_ptr = &mp->ma_keys->dk_entries[0].me_value; |
| 1594 | offset = sizeof(PyDictKeyEntry); |
| 1595 | } |
| 1596 | for (i = 0, j = 0; i < size; i++) { |
| 1597 | PyObject *value = *value_ptr; |
| 1598 | value_ptr = (PyObject **)(((char *)value_ptr) + offset); |
| 1599 | if (value != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1600 | Py_INCREF(value); |
| 1601 | PyList_SET_ITEM(v, j, value); |
| 1602 | j++; |
| 1603 | } |
| 1604 | } |
| 1605 | assert(j == n); |
| 1606 | return v; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1607 | } |
| 1608 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1609 | static PyObject * |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1610 | dict_items(register PyDictObject *mp) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1611 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1612 | register PyObject *v; |
| 1613 | register Py_ssize_t i, j, n; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1614 | Py_ssize_t size, offset; |
| 1615 | PyObject *item, *key; |
| 1616 | PyDictKeyEntry *ep; |
| 1617 | PyObject **value_ptr; |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1618 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1619 | /* Preallocate the list of tuples, to avoid allocations during |
| 1620 | * the loop over the items, which could trigger GC, which |
| 1621 | * could resize the dict. :-( |
| 1622 | */ |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1623 | again: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1624 | n = mp->ma_used; |
| 1625 | v = PyList_New(n); |
| 1626 | if (v == NULL) |
| 1627 | return NULL; |
| 1628 | for (i = 0; i < n; i++) { |
| 1629 | item = PyTuple_New(2); |
| 1630 | if (item == NULL) { |
| 1631 | Py_DECREF(v); |
| 1632 | return NULL; |
| 1633 | } |
| 1634 | PyList_SET_ITEM(v, i, item); |
| 1635 | } |
| 1636 | if (n != mp->ma_used) { |
| 1637 | /* Durnit. The allocations caused the dict to resize. |
| 1638 | * Just start over, this shouldn't normally happen. |
| 1639 | */ |
| 1640 | Py_DECREF(v); |
| 1641 | goto again; |
| 1642 | } |
| 1643 | /* Nothing we do below makes any function calls. */ |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1644 | ep = mp->ma_keys->dk_entries; |
| 1645 | size = DK_SIZE(mp->ma_keys); |
| 1646 | if (mp->ma_values) { |
| 1647 | value_ptr = mp->ma_values; |
| 1648 | offset = sizeof(PyObject *); |
| 1649 | } |
| 1650 | else { |
| 1651 | value_ptr = &ep[0].me_value; |
| 1652 | offset = sizeof(PyDictKeyEntry); |
| 1653 | } |
| 1654 | for (i = 0, j = 0; i < size; i++) { |
| 1655 | PyObject *value = *value_ptr; |
| 1656 | value_ptr = (PyObject **)(((char *)value_ptr) + offset); |
| 1657 | if (value != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1658 | key = ep[i].me_key; |
| 1659 | item = PyList_GET_ITEM(v, j); |
| 1660 | Py_INCREF(key); |
| 1661 | PyTuple_SET_ITEM(item, 0, key); |
| 1662 | Py_INCREF(value); |
| 1663 | PyTuple_SET_ITEM(item, 1, value); |
| 1664 | j++; |
| 1665 | } |
| 1666 | } |
| 1667 | assert(j == n); |
| 1668 | return v; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1669 | } |
| 1670 | |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 1671 | static PyObject * |
Tim Peters | bca1cbc | 2002-12-09 22:56:13 +0000 | [diff] [blame] | 1672 | dict_fromkeys(PyObject *cls, PyObject *args) |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 1673 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1674 | PyObject *seq; |
| 1675 | PyObject *value = Py_None; |
| 1676 | PyObject *it; /* iter(seq) */ |
| 1677 | PyObject *key; |
| 1678 | PyObject *d; |
| 1679 | int status; |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 1680 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1681 | if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value)) |
| 1682 | return NULL; |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 1683 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1684 | d = PyObject_CallObject(cls, NULL); |
| 1685 | if (d == NULL) |
| 1686 | return NULL; |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 1687 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1688 | if (PyDict_CheckExact(d) && PyDict_CheckExact(seq)) { |
| 1689 | PyDictObject *mp = (PyDictObject *)d; |
| 1690 | PyObject *oldvalue; |
| 1691 | Py_ssize_t pos = 0; |
| 1692 | PyObject *key; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 1693 | Py_hash_t hash; |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 1694 | |
Petri Lehtinen | a94200e | 2011-10-24 21:12:58 +0300 | [diff] [blame] | 1695 | if (dictresize(mp, Py_SIZE(seq))) { |
| 1696 | Py_DECREF(d); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1697 | return NULL; |
Petri Lehtinen | a94200e | 2011-10-24 21:12:58 +0300 | [diff] [blame] | 1698 | } |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 1699 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1700 | while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) { |
Petri Lehtinen | a94200e | 2011-10-24 21:12:58 +0300 | [diff] [blame] | 1701 | if (insertdict(mp, key, hash, value)) { |
| 1702 | Py_DECREF(d); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1703 | return NULL; |
Petri Lehtinen | a94200e | 2011-10-24 21:12:58 +0300 | [diff] [blame] | 1704 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1705 | } |
| 1706 | return d; |
| 1707 | } |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 1708 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1709 | if (PyDict_CheckExact(d) && PyAnySet_CheckExact(seq)) { |
| 1710 | PyDictObject *mp = (PyDictObject *)d; |
| 1711 | Py_ssize_t pos = 0; |
| 1712 | PyObject *key; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 1713 | Py_hash_t hash; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1714 | |
Petri Lehtinen | a94200e | 2011-10-24 21:12:58 +0300 | [diff] [blame] | 1715 | if (dictresize(mp, PySet_GET_SIZE(seq))) { |
| 1716 | Py_DECREF(d); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1717 | return NULL; |
Petri Lehtinen | a94200e | 2011-10-24 21:12:58 +0300 | [diff] [blame] | 1718 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1719 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1720 | while (_PySet_NextEntry(seq, &pos, &key, &hash)) { |
Petri Lehtinen | a94200e | 2011-10-24 21:12:58 +0300 | [diff] [blame] | 1721 | if (insertdict(mp, key, hash, value)) { |
| 1722 | Py_DECREF(d); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1723 | return NULL; |
Petri Lehtinen | a94200e | 2011-10-24 21:12:58 +0300 | [diff] [blame] | 1724 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1725 | } |
| 1726 | return d; |
| 1727 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1728 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1729 | it = PyObject_GetIter(seq); |
| 1730 | if (it == NULL){ |
| 1731 | Py_DECREF(d); |
| 1732 | return NULL; |
| 1733 | } |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 1734 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1735 | if (PyDict_CheckExact(d)) { |
| 1736 | while ((key = PyIter_Next(it)) != NULL) { |
| 1737 | status = PyDict_SetItem(d, key, value); |
| 1738 | Py_DECREF(key); |
| 1739 | if (status < 0) |
| 1740 | goto Fail; |
| 1741 | } |
| 1742 | } else { |
| 1743 | while ((key = PyIter_Next(it)) != NULL) { |
| 1744 | status = PyObject_SetItem(d, key, value); |
| 1745 | Py_DECREF(key); |
| 1746 | if (status < 0) |
| 1747 | goto Fail; |
| 1748 | } |
| 1749 | } |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 1750 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1751 | if (PyErr_Occurred()) |
| 1752 | goto Fail; |
| 1753 | Py_DECREF(it); |
| 1754 | return d; |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 1755 | |
| 1756 | Fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1757 | Py_DECREF(it); |
| 1758 | Py_DECREF(d); |
| 1759 | return NULL; |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 1760 | } |
| 1761 | |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 1762 | static int |
| 1763 | dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname) |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 1764 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1765 | PyObject *arg = NULL; |
| 1766 | int result = 0; |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 1767 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1768 | if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg)) |
| 1769 | result = -1; |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 1770 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1771 | else if (arg != NULL) { |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 1772 | _Py_IDENTIFIER(keys); |
| 1773 | if (_PyObject_HasAttrId(arg, &PyId_keys)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1774 | result = PyDict_Merge(self, arg, 1); |
| 1775 | else |
| 1776 | result = PyDict_MergeFromSeq2(self, arg, 1); |
| 1777 | } |
| 1778 | if (result == 0 && kwds != NULL) { |
| 1779 | if (PyArg_ValidateKeywordArguments(kwds)) |
| 1780 | result = PyDict_Merge(self, kwds, 1); |
| 1781 | else |
| 1782 | result = -1; |
| 1783 | } |
| 1784 | return result; |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 1785 | } |
| 1786 | |
| 1787 | static PyObject * |
| 1788 | dict_update(PyObject *self, PyObject *args, PyObject *kwds) |
| 1789 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1790 | if (dict_update_common(self, args, kwds, "update") != -1) |
| 1791 | Py_RETURN_NONE; |
| 1792 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1793 | } |
| 1794 | |
Guido van Rossum | 05ac6de | 2001-08-10 20:28:28 +0000 | [diff] [blame] | 1795 | /* Update unconditionally replaces existing items. |
| 1796 | Merge has a 3rd argument 'override'; if set, it acts like Update, |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1797 | otherwise it leaves existing items unchanged. |
| 1798 | |
| 1799 | PyDict_{Update,Merge} update/merge from a mapping object. |
| 1800 | |
Tim Peters | f582b82 | 2001-12-11 18:51:08 +0000 | [diff] [blame] | 1801 | PyDict_MergeFromSeq2 updates/merges from any iterable object |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1802 | producing iterable objects of length 2. |
| 1803 | */ |
| 1804 | |
Tim Peters | f582b82 | 2001-12-11 18:51:08 +0000 | [diff] [blame] | 1805 | int |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1806 | PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override) |
| 1807 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1808 | PyObject *it; /* iter(seq2) */ |
| 1809 | Py_ssize_t i; /* index into seq2 of current element */ |
| 1810 | PyObject *item; /* seq2[i] */ |
| 1811 | PyObject *fast; /* item as a 2-tuple or 2-list */ |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1812 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1813 | assert(d != NULL); |
| 1814 | assert(PyDict_Check(d)); |
| 1815 | assert(seq2 != NULL); |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1816 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1817 | it = PyObject_GetIter(seq2); |
| 1818 | if (it == NULL) |
| 1819 | return -1; |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1820 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1821 | for (i = 0; ; ++i) { |
| 1822 | PyObject *key, *value; |
| 1823 | Py_ssize_t n; |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1824 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1825 | fast = NULL; |
| 1826 | item = PyIter_Next(it); |
| 1827 | if (item == NULL) { |
| 1828 | if (PyErr_Occurred()) |
| 1829 | goto Fail; |
| 1830 | break; |
| 1831 | } |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1832 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1833 | /* Convert item to sequence, and verify length 2. */ |
| 1834 | fast = PySequence_Fast(item, ""); |
| 1835 | if (fast == NULL) { |
| 1836 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 1837 | PyErr_Format(PyExc_TypeError, |
| 1838 | "cannot convert dictionary update " |
| 1839 | "sequence element #%zd to a sequence", |
| 1840 | i); |
| 1841 | goto Fail; |
| 1842 | } |
| 1843 | n = PySequence_Fast_GET_SIZE(fast); |
| 1844 | if (n != 2) { |
| 1845 | PyErr_Format(PyExc_ValueError, |
| 1846 | "dictionary update sequence element #%zd " |
| 1847 | "has length %zd; 2 is required", |
| 1848 | i, n); |
| 1849 | goto Fail; |
| 1850 | } |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1851 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1852 | /* Update/merge with this (key, value) pair. */ |
| 1853 | key = PySequence_Fast_GET_ITEM(fast, 0); |
| 1854 | value = PySequence_Fast_GET_ITEM(fast, 1); |
| 1855 | if (override || PyDict_GetItem(d, key) == NULL) { |
| 1856 | int status = PyDict_SetItem(d, key, value); |
| 1857 | if (status < 0) |
| 1858 | goto Fail; |
| 1859 | } |
| 1860 | Py_DECREF(fast); |
| 1861 | Py_DECREF(item); |
| 1862 | } |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1863 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1864 | i = 0; |
| 1865 | goto Return; |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1866 | Fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1867 | Py_XDECREF(item); |
| 1868 | Py_XDECREF(fast); |
| 1869 | i = -1; |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1870 | Return: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1871 | Py_DECREF(it); |
| 1872 | return Py_SAFE_DOWNCAST(i, Py_ssize_t, int); |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1873 | } |
| 1874 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1875 | int |
| 1876 | PyDict_Update(PyObject *a, PyObject *b) |
| 1877 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1878 | return PyDict_Merge(a, b, 1); |
Guido van Rossum | 05ac6de | 2001-08-10 20:28:28 +0000 | [diff] [blame] | 1879 | } |
| 1880 | |
| 1881 | int |
| 1882 | PyDict_Merge(PyObject *a, PyObject *b, int override) |
| 1883 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1884 | register PyDictObject *mp, *other; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1885 | register Py_ssize_t i, n; |
| 1886 | PyDictKeyEntry *entry; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1887 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1888 | /* We accept for the argument either a concrete dictionary object, |
| 1889 | * or an abstract "mapping" object. For the former, we can do |
| 1890 | * things quite efficiently. For the latter, we only require that |
| 1891 | * PyMapping_Keys() and PyObject_GetItem() be supported. |
| 1892 | */ |
| 1893 | if (a == NULL || !PyDict_Check(a) || b == NULL) { |
| 1894 | PyErr_BadInternalCall(); |
| 1895 | return -1; |
| 1896 | } |
| 1897 | mp = (PyDictObject*)a; |
| 1898 | if (PyDict_Check(b)) { |
| 1899 | other = (PyDictObject*)b; |
| 1900 | if (other == mp || other->ma_used == 0) |
| 1901 | /* a.update(a) or a.update({}); nothing to do */ |
| 1902 | return 0; |
| 1903 | if (mp->ma_used == 0) |
| 1904 | /* Since the target dict is empty, PyDict_GetItem() |
| 1905 | * always returns NULL. Setting override to 1 |
| 1906 | * skips the unnecessary test. |
| 1907 | */ |
| 1908 | override = 1; |
| 1909 | /* Do one big resize at the start, rather than |
| 1910 | * incrementally resizing as we insert new items. Expect |
| 1911 | * that there will be no (or few) overlapping keys. |
| 1912 | */ |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1913 | if (mp->ma_keys->dk_usable * 3 < other->ma_used * 2) |
| 1914 | if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1915 | return -1; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1916 | for (i = 0, n = DK_SIZE(other->ma_keys); i < n; i++) { |
| 1917 | PyObject *value; |
| 1918 | entry = &other->ma_keys->dk_entries[i]; |
| 1919 | if (other->ma_values) |
| 1920 | value = other->ma_values[i]; |
| 1921 | else |
| 1922 | value = entry->me_value; |
| 1923 | |
| 1924 | if (value != NULL && |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1925 | (override || |
| 1926 | PyDict_GetItem(a, entry->me_key) == NULL)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1927 | if (insertdict(mp, entry->me_key, |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 1928 | entry->me_hash, |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1929 | value) != 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1930 | return -1; |
| 1931 | } |
| 1932 | } |
| 1933 | } |
| 1934 | else { |
| 1935 | /* Do it the generic, slower way */ |
| 1936 | PyObject *keys = PyMapping_Keys(b); |
| 1937 | PyObject *iter; |
| 1938 | PyObject *key, *value; |
| 1939 | int status; |
Barry Warsaw | 66a0d1d | 2001-06-26 20:08:32 +0000 | [diff] [blame] | 1940 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1941 | if (keys == NULL) |
| 1942 | /* Docstring says this is equivalent to E.keys() so |
| 1943 | * if E doesn't have a .keys() method we want |
| 1944 | * AttributeError to percolate up. Might as well |
| 1945 | * do the same for any other error. |
| 1946 | */ |
| 1947 | return -1; |
Barry Warsaw | 66a0d1d | 2001-06-26 20:08:32 +0000 | [diff] [blame] | 1948 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1949 | iter = PyObject_GetIter(keys); |
| 1950 | Py_DECREF(keys); |
| 1951 | if (iter == NULL) |
| 1952 | return -1; |
Barry Warsaw | 66a0d1d | 2001-06-26 20:08:32 +0000 | [diff] [blame] | 1953 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1954 | for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) { |
| 1955 | if (!override && PyDict_GetItem(a, key) != NULL) { |
| 1956 | Py_DECREF(key); |
| 1957 | continue; |
| 1958 | } |
| 1959 | value = PyObject_GetItem(b, key); |
| 1960 | if (value == NULL) { |
| 1961 | Py_DECREF(iter); |
| 1962 | Py_DECREF(key); |
| 1963 | return -1; |
| 1964 | } |
| 1965 | status = PyDict_SetItem(a, key, value); |
| 1966 | Py_DECREF(key); |
| 1967 | Py_DECREF(value); |
| 1968 | if (status < 0) { |
| 1969 | Py_DECREF(iter); |
| 1970 | return -1; |
| 1971 | } |
| 1972 | } |
| 1973 | Py_DECREF(iter); |
| 1974 | if (PyErr_Occurred()) |
| 1975 | /* Iterator completed, via error */ |
| 1976 | return -1; |
| 1977 | } |
| 1978 | return 0; |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 1979 | } |
| 1980 | |
| 1981 | static PyObject * |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1982 | dict_copy(register PyDictObject *mp) |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 1983 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1984 | return PyDict_Copy((PyObject*)mp); |
Jeremy Hylton | a12c7a7 | 2000-03-30 22:27:31 +0000 | [diff] [blame] | 1985 | } |
| 1986 | |
| 1987 | PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 1988 | PyDict_Copy(PyObject *o) |
Jeremy Hylton | a12c7a7 | 2000-03-30 22:27:31 +0000 | [diff] [blame] | 1989 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1990 | PyObject *copy; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1991 | PyDictObject *mp; |
| 1992 | Py_ssize_t i, n; |
Jeremy Hylton | a12c7a7 | 2000-03-30 22:27:31 +0000 | [diff] [blame] | 1993 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1994 | if (o == NULL || !PyDict_Check(o)) { |
| 1995 | PyErr_BadInternalCall(); |
| 1996 | return NULL; |
| 1997 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1998 | mp = (PyDictObject *)o; |
| 1999 | if (_PyDict_HasSplitTable(mp)) { |
| 2000 | PyDictObject *split_copy; |
| 2001 | PyObject **newvalues = new_values(DK_SIZE(mp->ma_keys)); |
| 2002 | if (newvalues == NULL) |
| 2003 | return PyErr_NoMemory(); |
| 2004 | split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type); |
| 2005 | if (split_copy == NULL) { |
| 2006 | free_values(newvalues); |
| 2007 | return NULL; |
| 2008 | } |
| 2009 | split_copy->ma_values = newvalues; |
| 2010 | split_copy->ma_keys = mp->ma_keys; |
| 2011 | split_copy->ma_used = mp->ma_used; |
| 2012 | DK_INCREF(mp->ma_keys); |
| 2013 | for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) { |
| 2014 | PyObject *value = mp->ma_values[i]; |
| 2015 | Py_XINCREF(value); |
| 2016 | split_copy->ma_values[i] = value; |
| 2017 | } |
Benjamin Peterson | 7ce67e4 | 2012-04-24 10:32:57 -0400 | [diff] [blame] | 2018 | if (_PyObject_GC_IS_TRACKED(mp)) |
| 2019 | _PyObject_GC_TRACK(split_copy); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2020 | return (PyObject *)split_copy; |
| 2021 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2022 | copy = PyDict_New(); |
| 2023 | if (copy == NULL) |
| 2024 | return NULL; |
| 2025 | if (PyDict_Merge(copy, o, 1) == 0) |
| 2026 | return copy; |
| 2027 | Py_DECREF(copy); |
| 2028 | return NULL; |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 2029 | } |
| 2030 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2031 | Py_ssize_t |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 2032 | PyDict_Size(PyObject *mp) |
Guido van Rossum | 4199fac | 1993-11-05 10:18:44 +0000 | [diff] [blame] | 2033 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2034 | if (mp == NULL || !PyDict_Check(mp)) { |
| 2035 | PyErr_BadInternalCall(); |
| 2036 | return -1; |
| 2037 | } |
| 2038 | return ((PyDictObject *)mp)->ma_used; |
Guido van Rossum | 4199fac | 1993-11-05 10:18:44 +0000 | [diff] [blame] | 2039 | } |
| 2040 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2041 | PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 2042 | PyDict_Keys(PyObject *mp) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2043 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2044 | if (mp == NULL || !PyDict_Check(mp)) { |
| 2045 | PyErr_BadInternalCall(); |
| 2046 | return NULL; |
| 2047 | } |
| 2048 | return dict_keys((PyDictObject *)mp); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2049 | } |
| 2050 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2051 | PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 2052 | PyDict_Values(PyObject *mp) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2053 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2054 | if (mp == NULL || !PyDict_Check(mp)) { |
| 2055 | PyErr_BadInternalCall(); |
| 2056 | return NULL; |
| 2057 | } |
| 2058 | return dict_values((PyDictObject *)mp); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2059 | } |
| 2060 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2061 | PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 2062 | PyDict_Items(PyObject *mp) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2063 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2064 | if (mp == NULL || !PyDict_Check(mp)) { |
| 2065 | PyErr_BadInternalCall(); |
| 2066 | return NULL; |
| 2067 | } |
| 2068 | return dict_items((PyDictObject *)mp); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2069 | } |
| 2070 | |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 2071 | /* Return 1 if dicts equal, 0 if not, -1 if error. |
| 2072 | * Gets out as soon as any difference is detected. |
| 2073 | * Uses only Py_EQ comparison. |
| 2074 | */ |
| 2075 | static int |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2076 | dict_equal(PyDictObject *a, PyDictObject *b) |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 2077 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2078 | Py_ssize_t i; |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 2079 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2080 | if (a->ma_used != b->ma_used) |
| 2081 | /* can't be equal if # of entries differ */ |
| 2082 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2083 | /* Same # of entries -- check all of 'em. Exit early on any diff. */ |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2084 | for (i = 0; i < DK_SIZE(a->ma_keys); i++) { |
| 2085 | PyDictKeyEntry *ep = &a->ma_keys->dk_entries[i]; |
| 2086 | PyObject *aval; |
| 2087 | if (a->ma_values) |
| 2088 | aval = a->ma_values[i]; |
| 2089 | else |
| 2090 | aval = ep->me_value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2091 | if (aval != NULL) { |
| 2092 | int cmp; |
| 2093 | PyObject *bval; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2094 | PyObject *key = ep->me_key; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2095 | /* temporarily bump aval's refcount to ensure it stays |
| 2096 | alive until we're done with it */ |
| 2097 | Py_INCREF(aval); |
| 2098 | /* ditto for key */ |
| 2099 | Py_INCREF(key); |
| 2100 | bval = PyDict_GetItemWithError((PyObject *)b, key); |
| 2101 | Py_DECREF(key); |
| 2102 | if (bval == NULL) { |
| 2103 | Py_DECREF(aval); |
| 2104 | if (PyErr_Occurred()) |
| 2105 | return -1; |
| 2106 | return 0; |
| 2107 | } |
| 2108 | cmp = PyObject_RichCompareBool(aval, bval, Py_EQ); |
| 2109 | Py_DECREF(aval); |
| 2110 | if (cmp <= 0) /* error or not equal */ |
| 2111 | return cmp; |
| 2112 | } |
| 2113 | } |
| 2114 | return 1; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2115 | } |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 2116 | |
| 2117 | static PyObject * |
| 2118 | dict_richcompare(PyObject *v, PyObject *w, int op) |
| 2119 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2120 | int cmp; |
| 2121 | PyObject *res; |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 2122 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2123 | if (!PyDict_Check(v) || !PyDict_Check(w)) { |
| 2124 | res = Py_NotImplemented; |
| 2125 | } |
| 2126 | else if (op == Py_EQ || op == Py_NE) { |
| 2127 | cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w); |
| 2128 | if (cmp < 0) |
| 2129 | return NULL; |
| 2130 | res = (cmp == (op == Py_EQ)) ? Py_True : Py_False; |
| 2131 | } |
| 2132 | else |
| 2133 | res = Py_NotImplemented; |
| 2134 | Py_INCREF(res); |
| 2135 | return res; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2136 | } |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 2137 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2138 | static PyObject * |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2139 | dict_contains(register PyDictObject *mp, PyObject *key) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2140 | { |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2141 | Py_hash_t hash; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2142 | PyDictKeyEntry *ep; |
| 2143 | PyObject **value_addr; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2144 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2145 | if (!PyUnicode_CheckExact(key) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2146 | (hash = ((PyASCIIObject *) key)->hash) == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2147 | hash = PyObject_Hash(key); |
| 2148 | if (hash == -1) |
| 2149 | return NULL; |
| 2150 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2151 | ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2152 | if (ep == NULL) |
| 2153 | return NULL; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2154 | return PyBool_FromLong(*value_addr != NULL); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2155 | } |
| 2156 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2157 | static PyObject * |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2158 | dict_get(register PyDictObject *mp, PyObject *args) |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 2159 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2160 | PyObject *key; |
| 2161 | PyObject *failobj = Py_None; |
| 2162 | PyObject *val = NULL; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2163 | Py_hash_t hash; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2164 | PyDictKeyEntry *ep; |
| 2165 | PyObject **value_addr; |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 2166 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2167 | if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj)) |
| 2168 | return NULL; |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 2169 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2170 | if (!PyUnicode_CheckExact(key) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2171 | (hash = ((PyASCIIObject *) key)->hash) == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2172 | hash = PyObject_Hash(key); |
| 2173 | if (hash == -1) |
| 2174 | return NULL; |
| 2175 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2176 | ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2177 | if (ep == NULL) |
| 2178 | return NULL; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2179 | val = *value_addr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2180 | if (val == NULL) |
| 2181 | val = failobj; |
| 2182 | Py_INCREF(val); |
| 2183 | return val; |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 2184 | } |
| 2185 | |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 2186 | static PyObject * |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2187 | dict_setdefault(register PyDictObject *mp, PyObject *args) |
Guido van Rossum | 164452c | 2000-08-08 16:12:54 +0000 | [diff] [blame] | 2188 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2189 | PyObject *key; |
| 2190 | PyObject *failobj = Py_None; |
| 2191 | PyObject *val = NULL; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2192 | Py_hash_t hash; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2193 | PyDictKeyEntry *ep; |
| 2194 | PyObject **value_addr; |
Guido van Rossum | 164452c | 2000-08-08 16:12:54 +0000 | [diff] [blame] | 2195 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2196 | if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj)) |
| 2197 | return NULL; |
Guido van Rossum | 164452c | 2000-08-08 16:12:54 +0000 | [diff] [blame] | 2198 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2199 | if (!PyUnicode_CheckExact(key) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2200 | (hash = ((PyASCIIObject *) key)->hash) == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2201 | hash = PyObject_Hash(key); |
| 2202 | if (hash == -1) |
| 2203 | return NULL; |
| 2204 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2205 | ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2206 | if (ep == NULL) |
| 2207 | return NULL; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2208 | val = *value_addr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2209 | if (val == NULL) { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2210 | Py_INCREF(failobj); |
| 2211 | Py_INCREF(key); |
| 2212 | if (mp->ma_keys->dk_usable <= 0) { |
| 2213 | /* Need to resize. */ |
| 2214 | if (insertion_resize(mp) < 0) |
| 2215 | return NULL; |
| 2216 | ep = find_empty_slot(mp, key, hash, &value_addr); |
| 2217 | } |
Benjamin Peterson | 7ce67e4 | 2012-04-24 10:32:57 -0400 | [diff] [blame] | 2218 | MAINTAIN_TRACKING(mp, key, failobj); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2219 | ep->me_key = key; |
| 2220 | ep->me_hash = hash; |
| 2221 | *value_addr = failobj; |
| 2222 | val = failobj; |
| 2223 | mp->ma_keys->dk_usable--; |
| 2224 | mp->ma_used++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2225 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2226 | Py_INCREF(val); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2227 | return val; |
Guido van Rossum | 164452c | 2000-08-08 16:12:54 +0000 | [diff] [blame] | 2228 | } |
| 2229 | |
| 2230 | |
| 2231 | static PyObject * |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2232 | dict_clear(register PyDictObject *mp) |
Guido van Rossum | fb8f1ca | 1997-03-21 21:55:12 +0000 | [diff] [blame] | 2233 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2234 | PyDict_Clear((PyObject *)mp); |
| 2235 | Py_RETURN_NONE; |
Guido van Rossum | fb8f1ca | 1997-03-21 21:55:12 +0000 | [diff] [blame] | 2236 | } |
| 2237 | |
Guido van Rossum | ba6ab84 | 2000-12-12 22:02:18 +0000 | [diff] [blame] | 2238 | static PyObject * |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2239 | dict_pop(PyDictObject *mp, PyObject *args) |
Guido van Rossum | e027d98 | 2002-04-12 15:11:59 +0000 | [diff] [blame] | 2240 | { |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2241 | Py_hash_t hash; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2242 | PyObject *old_value, *old_key; |
| 2243 | PyObject *key, *deflt = NULL; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2244 | PyDictKeyEntry *ep; |
| 2245 | PyObject **value_addr; |
Guido van Rossum | e027d98 | 2002-04-12 15:11:59 +0000 | [diff] [blame] | 2246 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2247 | if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt)) |
| 2248 | return NULL; |
| 2249 | if (mp->ma_used == 0) { |
| 2250 | if (deflt) { |
| 2251 | Py_INCREF(deflt); |
| 2252 | return deflt; |
| 2253 | } |
Raymond Hettinger | dd42154 | 2010-10-30 08:10:29 +0000 | [diff] [blame] | 2254 | set_key_error(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2255 | return NULL; |
| 2256 | } |
| 2257 | if (!PyUnicode_CheckExact(key) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2258 | (hash = ((PyASCIIObject *) key)->hash) == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2259 | hash = PyObject_Hash(key); |
| 2260 | if (hash == -1) |
| 2261 | return NULL; |
| 2262 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2263 | ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2264 | if (ep == NULL) |
| 2265 | return NULL; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2266 | old_value = *value_addr; |
| 2267 | if (old_value == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2268 | if (deflt) { |
| 2269 | Py_INCREF(deflt); |
| 2270 | return deflt; |
| 2271 | } |
| 2272 | set_key_error(key); |
| 2273 | return NULL; |
| 2274 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2275 | *value_addr = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2276 | mp->ma_used--; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2277 | if (!_PyDict_HasSplitTable(mp)) { |
| 2278 | ENSURE_ALLOWS_DELETIONS(mp); |
| 2279 | old_key = ep->me_key; |
| 2280 | Py_INCREF(dummy); |
| 2281 | ep->me_key = dummy; |
| 2282 | Py_DECREF(old_key); |
| 2283 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2284 | return old_value; |
Guido van Rossum | e027d98 | 2002-04-12 15:11:59 +0000 | [diff] [blame] | 2285 | } |
| 2286 | |
| 2287 | static PyObject * |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2288 | dict_popitem(PyDictObject *mp) |
Guido van Rossum | ba6ab84 | 2000-12-12 22:02:18 +0000 | [diff] [blame] | 2289 | { |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2290 | Py_hash_t i = 0; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2291 | PyDictKeyEntry *ep; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2292 | PyObject *res; |
Guido van Rossum | ba6ab84 | 2000-12-12 22:02:18 +0000 | [diff] [blame] | 2293 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2294 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2295 | /* Allocate the result tuple before checking the size. Believe it |
| 2296 | * or not, this allocation could trigger a garbage collection which |
| 2297 | * could empty the dict, so if we checked the size first and that |
| 2298 | * happened, the result would be an infinite loop (searching for an |
| 2299 | * entry that no longer exists). Note that the usual popitem() |
| 2300 | * idiom is "while d: k, v = d.popitem()". so needing to throw the |
| 2301 | * tuple away if the dict *is* empty isn't a significant |
| 2302 | * inefficiency -- possible, but unlikely in practice. |
| 2303 | */ |
| 2304 | res = PyTuple_New(2); |
| 2305 | if (res == NULL) |
| 2306 | return NULL; |
| 2307 | if (mp->ma_used == 0) { |
| 2308 | Py_DECREF(res); |
| 2309 | PyErr_SetString(PyExc_KeyError, |
| 2310 | "popitem(): dictionary is empty"); |
| 2311 | return NULL; |
| 2312 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2313 | /* Convert split table to combined table */ |
| 2314 | if (mp->ma_keys->dk_lookup == lookdict_split) { |
| 2315 | if (dictresize(mp, DK_SIZE(mp->ma_keys))) { |
| 2316 | Py_DECREF(res); |
| 2317 | return NULL; |
| 2318 | } |
| 2319 | } |
| 2320 | ENSURE_ALLOWS_DELETIONS(mp); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2321 | /* Set ep to "the first" dict entry with a value. We abuse the hash |
| 2322 | * field of slot 0 to hold a search finger: |
| 2323 | * If slot 0 has a value, use slot 0. |
| 2324 | * Else slot 0 is being used to hold a search finger, |
| 2325 | * and we use its hash value as the first index to look. |
| 2326 | */ |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2327 | ep = &mp->ma_keys->dk_entries[0]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2328 | if (ep->me_value == NULL) { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2329 | Py_ssize_t mask = DK_MASK(mp->ma_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2330 | i = ep->me_hash; |
| 2331 | /* The hash field may be a real hash value, or it may be a |
| 2332 | * legit search finger, or it may be a once-legit search |
| 2333 | * finger that's out of bounds now because it wrapped around |
| 2334 | * or the table shrunk -- simply make sure it's in bounds now. |
| 2335 | */ |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2336 | if (i > mask || i < 1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2337 | i = 1; /* skip slot 0 */ |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2338 | while ((ep = &mp->ma_keys->dk_entries[i])->me_value == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2339 | i++; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2340 | if (i > mask) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2341 | i = 1; |
| 2342 | } |
| 2343 | } |
| 2344 | PyTuple_SET_ITEM(res, 0, ep->me_key); |
| 2345 | PyTuple_SET_ITEM(res, 1, ep->me_value); |
| 2346 | Py_INCREF(dummy); |
| 2347 | ep->me_key = dummy; |
| 2348 | ep->me_value = NULL; |
| 2349 | mp->ma_used--; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2350 | assert(mp->ma_keys->dk_entries[0].me_value == NULL); |
| 2351 | mp->ma_keys->dk_entries[0].me_hash = i + 1; /* next place to start */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2352 | return res; |
Guido van Rossum | ba6ab84 | 2000-12-12 22:02:18 +0000 | [diff] [blame] | 2353 | } |
| 2354 | |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 2355 | static int |
| 2356 | dict_traverse(PyObject *op, visitproc visit, void *arg) |
| 2357 | { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2358 | Py_ssize_t i, n; |
| 2359 | PyDictObject *mp = (PyDictObject *)op; |
| 2360 | if (mp->ma_keys->dk_lookup == lookdict) { |
| 2361 | for (i = 0; i < DK_SIZE(mp->ma_keys); i++) { |
| 2362 | if (mp->ma_keys->dk_entries[i].me_value != NULL) { |
| 2363 | Py_VISIT(mp->ma_keys->dk_entries[i].me_value); |
| 2364 | Py_VISIT(mp->ma_keys->dk_entries[i].me_key); |
| 2365 | } |
| 2366 | } |
| 2367 | } else { |
| 2368 | if (mp->ma_values != NULL) { |
| 2369 | for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) { |
| 2370 | Py_VISIT(mp->ma_values[i]); |
| 2371 | } |
| 2372 | } |
| 2373 | else { |
| 2374 | for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) { |
| 2375 | Py_VISIT(mp->ma_keys->dk_entries[i].me_value); |
| 2376 | } |
| 2377 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2378 | } |
| 2379 | return 0; |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 2380 | } |
| 2381 | |
| 2382 | static int |
| 2383 | dict_tp_clear(PyObject *op) |
| 2384 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2385 | PyDict_Clear(op); |
| 2386 | return 0; |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 2387 | } |
| 2388 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2389 | static PyObject *dictiter_new(PyDictObject *, PyTypeObject *); |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 2390 | |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 2391 | static PyObject * |
| 2392 | dict_sizeof(PyDictObject *mp) |
| 2393 | { |
Martin v. Loewis | 4f2f3b6 | 2012-04-24 19:13:57 +0200 | [diff] [blame] | 2394 | Py_ssize_t size, res; |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 2395 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2396 | size = DK_SIZE(mp->ma_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2397 | res = sizeof(PyDictObject); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2398 | if (mp->ma_values) |
| 2399 | res += size * sizeof(PyObject*); |
Martin v. Loewis | 4f2f3b6 | 2012-04-24 19:13:57 +0200 | [diff] [blame] | 2400 | /* If the dictionary is split, the keys portion is accounted-for |
| 2401 | in the type object. */ |
| 2402 | if (mp->ma_keys->dk_refcnt == 1) |
| 2403 | res += sizeof(PyDictKeysObject) + (size-1) * sizeof(PyDictKeyEntry); |
| 2404 | return PyLong_FromSsize_t(res); |
| 2405 | } |
| 2406 | |
| 2407 | Py_ssize_t |
| 2408 | _PyDict_KeysSize(PyDictKeysObject *keys) |
| 2409 | { |
| 2410 | return sizeof(PyDictKeysObject) + (DK_SIZE(keys)-1) * sizeof(PyDictKeyEntry); |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 2411 | } |
| 2412 | |
Raymond Hettinger | 8f5cdaa | 2003-12-13 11:26:12 +0000 | [diff] [blame] | 2413 | PyDoc_STRVAR(contains__doc__, |
| 2414 | "D.__contains__(k) -> True if D has a key k, else False"); |
| 2415 | |
| 2416 | PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]"); |
| 2417 | |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 2418 | PyDoc_STRVAR(sizeof__doc__, |
| 2419 | "D.__sizeof__() -> size of D in memory, in bytes"); |
| 2420 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2421 | PyDoc_STRVAR(get__doc__, |
Guido van Rossum | efae886 | 2002-09-04 11:29:45 +0000 | [diff] [blame] | 2422 | "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] | 2423 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2424 | PyDoc_STRVAR(setdefault_doc__, |
Guido van Rossum | efae886 | 2002-09-04 11:29:45 +0000 | [diff] [blame] | 2425 | "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] | 2426 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2427 | PyDoc_STRVAR(pop__doc__, |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 2428 | "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] | 2429 | 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] | 2430 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2431 | PyDoc_STRVAR(popitem__doc__, |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2432 | "D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 2433 | 2-tuple; but raise KeyError if D is empty."); |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2434 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2435 | PyDoc_STRVAR(update__doc__, |
Georg Brandl | ac0675c | 2011-12-18 19:30:55 +0100 | [diff] [blame] | 2436 | "D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n" |
| 2437 | "If E present and has a .keys() method, does: for k in E: D[k] = E[k]\n\ |
| 2438 | If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v\n\ |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 2439 | 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] | 2440 | |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 2441 | PyDoc_STRVAR(fromkeys__doc__, |
| 2442 | "dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\ |
| 2443 | v defaults to None."); |
| 2444 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2445 | PyDoc_STRVAR(clear__doc__, |
| 2446 | "D.clear() -> None. Remove all items from D."); |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2447 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2448 | PyDoc_STRVAR(copy__doc__, |
| 2449 | "D.copy() -> a shallow copy of D"); |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2450 | |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 2451 | /* Forward */ |
| 2452 | static PyObject *dictkeys_new(PyObject *); |
| 2453 | static PyObject *dictitems_new(PyObject *); |
| 2454 | static PyObject *dictvalues_new(PyObject *); |
| 2455 | |
Guido van Rossum | 45c85d1 | 2007-07-27 16:31:40 +0000 | [diff] [blame] | 2456 | PyDoc_STRVAR(keys__doc__, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2457 | "D.keys() -> a set-like object providing a view on D's keys"); |
Guido van Rossum | 45c85d1 | 2007-07-27 16:31:40 +0000 | [diff] [blame] | 2458 | PyDoc_STRVAR(items__doc__, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2459 | "D.items() -> a set-like object providing a view on D's items"); |
Guido van Rossum | 45c85d1 | 2007-07-27 16:31:40 +0000 | [diff] [blame] | 2460 | PyDoc_STRVAR(values__doc__, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2461 | "D.values() -> an object providing a view on D's values"); |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 2462 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2463 | static PyMethodDef mapp_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2464 | {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST, |
| 2465 | contains__doc__}, |
| 2466 | {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST, |
| 2467 | getitem__doc__}, |
| 2468 | {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS, |
| 2469 | sizeof__doc__}, |
| 2470 | {"get", (PyCFunction)dict_get, METH_VARARGS, |
| 2471 | get__doc__}, |
| 2472 | {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS, |
| 2473 | setdefault_doc__}, |
| 2474 | {"pop", (PyCFunction)dict_pop, METH_VARARGS, |
| 2475 | pop__doc__}, |
| 2476 | {"popitem", (PyCFunction)dict_popitem, METH_NOARGS, |
| 2477 | popitem__doc__}, |
| 2478 | {"keys", (PyCFunction)dictkeys_new, METH_NOARGS, |
| 2479 | keys__doc__}, |
| 2480 | {"items", (PyCFunction)dictitems_new, METH_NOARGS, |
| 2481 | items__doc__}, |
| 2482 | {"values", (PyCFunction)dictvalues_new, METH_NOARGS, |
| 2483 | values__doc__}, |
| 2484 | {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS, |
| 2485 | update__doc__}, |
| 2486 | {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS, |
| 2487 | fromkeys__doc__}, |
| 2488 | {"clear", (PyCFunction)dict_clear, METH_NOARGS, |
| 2489 | clear__doc__}, |
| 2490 | {"copy", (PyCFunction)dict_copy, METH_NOARGS, |
| 2491 | copy__doc__}, |
| 2492 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2493 | }; |
| 2494 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2495 | /* 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] | 2496 | int |
| 2497 | PyDict_Contains(PyObject *op, PyObject *key) |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 2498 | { |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2499 | Py_hash_t hash; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2500 | PyDictObject *mp = (PyDictObject *)op; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2501 | PyDictKeyEntry *ep; |
| 2502 | PyObject **value_addr; |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 2503 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2504 | if (!PyUnicode_CheckExact(key) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2505 | (hash = ((PyASCIIObject *) key)->hash) == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2506 | hash = PyObject_Hash(key); |
| 2507 | if (hash == -1) |
| 2508 | return -1; |
| 2509 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2510 | ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr); |
| 2511 | return (ep == NULL) ? -1 : (*value_addr != NULL); |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 2512 | } |
| 2513 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2514 | /* Internal version of PyDict_Contains used when the hash value is already known */ |
| 2515 | int |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2516 | _PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2517 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2518 | PyDictObject *mp = (PyDictObject *)op; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2519 | PyDictKeyEntry *ep; |
| 2520 | PyObject **value_addr; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2521 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2522 | ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr); |
| 2523 | return (ep == NULL) ? -1 : (*value_addr != NULL); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2524 | } |
| 2525 | |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 2526 | /* Hack to implement "key in dict" */ |
| 2527 | static PySequenceMethods dict_as_sequence = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2528 | 0, /* sq_length */ |
| 2529 | 0, /* sq_concat */ |
| 2530 | 0, /* sq_repeat */ |
| 2531 | 0, /* sq_item */ |
| 2532 | 0, /* sq_slice */ |
| 2533 | 0, /* sq_ass_item */ |
| 2534 | 0, /* sq_ass_slice */ |
| 2535 | PyDict_Contains, /* sq_contains */ |
| 2536 | 0, /* sq_inplace_concat */ |
| 2537 | 0, /* sq_inplace_repeat */ |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 2538 | }; |
| 2539 | |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 2540 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2541 | dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2542 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2543 | PyObject *self; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2544 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2545 | assert(type != NULL && type->tp_alloc != NULL); |
| 2546 | self = type->tp_alloc(type, 0); |
| 2547 | if (self != NULL) { |
| 2548 | PyDictObject *d = (PyDictObject *)self; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2549 | d->ma_keys = new_keys_object(PyDict_MINSIZE_COMBINED); |
| 2550 | /* XXX - Should we raise a no-memory error? */ |
| 2551 | if (d->ma_keys == NULL) { |
| 2552 | DK_INCREF(Py_EMPTY_KEYS); |
| 2553 | d->ma_keys = Py_EMPTY_KEYS; |
| 2554 | d->ma_values = empty_values; |
| 2555 | } |
| 2556 | d->ma_used = 0; |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 2557 | /* The object has been implicitly tracked by tp_alloc */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2558 | if (type == &PyDict_Type) |
| 2559 | _PyObject_GC_UNTRACK(d); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2560 | } |
| 2561 | return self; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2562 | } |
| 2563 | |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 2564 | static int |
| 2565 | dict_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 2566 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2567 | return dict_update_common(self, args, kwds, "dict"); |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 2568 | } |
| 2569 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2570 | static PyObject * |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2571 | dict_iter(PyDictObject *dict) |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 2572 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2573 | return dictiter_new(dict, &PyDictIterKey_Type); |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 2574 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2575 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2576 | PyDoc_STRVAR(dictionary_doc, |
Ezio Melotti | 7f807b7 | 2010-03-01 04:08:34 +0000 | [diff] [blame] | 2577 | "dict() -> new empty dictionary\n" |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 2578 | "dict(mapping) -> new dictionary initialized from a mapping object's\n" |
Ezio Melotti | 7f807b7 | 2010-03-01 04:08:34 +0000 | [diff] [blame] | 2579 | " (key, value) pairs\n" |
| 2580 | "dict(iterable) -> new dictionary initialized as if via:\n" |
Tim Peters | 4d85953 | 2001-10-27 18:27:48 +0000 | [diff] [blame] | 2581 | " d = {}\n" |
Ezio Melotti | 7f807b7 | 2010-03-01 04:08:34 +0000 | [diff] [blame] | 2582 | " for k, v in iterable:\n" |
Just van Rossum | a797d81 | 2002-11-23 09:45:04 +0000 | [diff] [blame] | 2583 | " d[k] = v\n" |
| 2584 | "dict(**kwargs) -> new dictionary initialized with the name=value pairs\n" |
| 2585 | " in the keyword argument list. For example: dict(one=1, two=2)"); |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 2586 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2587 | PyTypeObject PyDict_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2588 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2589 | "dict", |
| 2590 | sizeof(PyDictObject), |
| 2591 | 0, |
| 2592 | (destructor)dict_dealloc, /* tp_dealloc */ |
| 2593 | 0, /* tp_print */ |
| 2594 | 0, /* tp_getattr */ |
| 2595 | 0, /* tp_setattr */ |
| 2596 | 0, /* tp_reserved */ |
| 2597 | (reprfunc)dict_repr, /* tp_repr */ |
| 2598 | 0, /* tp_as_number */ |
| 2599 | &dict_as_sequence, /* tp_as_sequence */ |
| 2600 | &dict_as_mapping, /* tp_as_mapping */ |
Georg Brandl | 00da4e0 | 2010-10-18 07:32:48 +0000 | [diff] [blame] | 2601 | PyObject_HashNotImplemented, /* tp_hash */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2602 | 0, /* tp_call */ |
| 2603 | 0, /* tp_str */ |
| 2604 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2605 | 0, /* tp_setattro */ |
| 2606 | 0, /* tp_as_buffer */ |
| 2607 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 2608 | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */ |
| 2609 | dictionary_doc, /* tp_doc */ |
| 2610 | dict_traverse, /* tp_traverse */ |
| 2611 | dict_tp_clear, /* tp_clear */ |
| 2612 | dict_richcompare, /* tp_richcompare */ |
| 2613 | 0, /* tp_weaklistoffset */ |
| 2614 | (getiterfunc)dict_iter, /* tp_iter */ |
| 2615 | 0, /* tp_iternext */ |
| 2616 | mapp_methods, /* tp_methods */ |
| 2617 | 0, /* tp_members */ |
| 2618 | 0, /* tp_getset */ |
| 2619 | 0, /* tp_base */ |
| 2620 | 0, /* tp_dict */ |
| 2621 | 0, /* tp_descr_get */ |
| 2622 | 0, /* tp_descr_set */ |
| 2623 | 0, /* tp_dictoffset */ |
| 2624 | dict_init, /* tp_init */ |
| 2625 | PyType_GenericAlloc, /* tp_alloc */ |
| 2626 | dict_new, /* tp_new */ |
| 2627 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2628 | }; |
| 2629 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2630 | PyObject * |
| 2631 | _PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key) |
| 2632 | { |
| 2633 | PyObject *kv; |
| 2634 | kv = _PyUnicode_FromId(key); /* borrowed */ |
| 2635 | if (kv == NULL) |
| 2636 | return NULL; |
| 2637 | return PyDict_GetItem(dp, kv); |
| 2638 | } |
| 2639 | |
Guido van Rossum | 3cca245 | 1997-05-16 14:23:33 +0000 | [diff] [blame] | 2640 | /* For backward compatibility with old dictionary interface */ |
| 2641 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2642 | PyObject * |
Martin v. Löwis | 32b4a1b | 2002-12-11 13:21:12 +0000 | [diff] [blame] | 2643 | PyDict_GetItemString(PyObject *v, const char *key) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2644 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2645 | PyObject *kv, *rv; |
| 2646 | kv = PyUnicode_FromString(key); |
| 2647 | if (kv == NULL) |
| 2648 | return NULL; |
| 2649 | rv = PyDict_GetItem(v, kv); |
| 2650 | Py_DECREF(kv); |
| 2651 | return rv; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2652 | } |
| 2653 | |
| 2654 | int |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2655 | _PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item) |
| 2656 | { |
| 2657 | PyObject *kv; |
| 2658 | kv = _PyUnicode_FromId(key); /* borrowed */ |
| 2659 | if (kv == NULL) |
| 2660 | return -1; |
| 2661 | return PyDict_SetItem(v, kv, item); |
| 2662 | } |
| 2663 | |
| 2664 | int |
Martin v. Löwis | 32b4a1b | 2002-12-11 13:21:12 +0000 | [diff] [blame] | 2665 | PyDict_SetItemString(PyObject *v, const char *key, PyObject *item) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2666 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2667 | PyObject *kv; |
| 2668 | int err; |
| 2669 | kv = PyUnicode_FromString(key); |
| 2670 | if (kv == NULL) |
| 2671 | return -1; |
| 2672 | PyUnicode_InternInPlace(&kv); /* XXX Should we really? */ |
| 2673 | err = PyDict_SetItem(v, kv, item); |
| 2674 | Py_DECREF(kv); |
| 2675 | return err; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2676 | } |
| 2677 | |
| 2678 | int |
Martin v. Löwis | 32b4a1b | 2002-12-11 13:21:12 +0000 | [diff] [blame] | 2679 | PyDict_DelItemString(PyObject *v, const char *key) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2680 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2681 | PyObject *kv; |
| 2682 | int err; |
| 2683 | kv = PyUnicode_FromString(key); |
| 2684 | if (kv == NULL) |
| 2685 | return -1; |
| 2686 | err = PyDict_DelItem(v, kv); |
| 2687 | Py_DECREF(kv); |
| 2688 | return err; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2689 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2690 | |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2691 | /* Dictionary iterator types */ |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2692 | |
| 2693 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2694 | PyObject_HEAD |
| 2695 | PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */ |
| 2696 | Py_ssize_t di_used; |
| 2697 | Py_ssize_t di_pos; |
| 2698 | PyObject* di_result; /* reusable result tuple for iteritems */ |
| 2699 | Py_ssize_t len; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2700 | } dictiterobject; |
| 2701 | |
| 2702 | static PyObject * |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2703 | dictiter_new(PyDictObject *dict, PyTypeObject *itertype) |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2704 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2705 | dictiterobject *di; |
| 2706 | di = PyObject_GC_New(dictiterobject, itertype); |
| 2707 | if (di == NULL) |
| 2708 | return NULL; |
| 2709 | Py_INCREF(dict); |
| 2710 | di->di_dict = dict; |
| 2711 | di->di_used = dict->ma_used; |
| 2712 | di->di_pos = 0; |
| 2713 | di->len = dict->ma_used; |
| 2714 | if (itertype == &PyDictIterItem_Type) { |
| 2715 | di->di_result = PyTuple_Pack(2, Py_None, Py_None); |
| 2716 | if (di->di_result == NULL) { |
| 2717 | Py_DECREF(di); |
| 2718 | return NULL; |
| 2719 | } |
| 2720 | } |
| 2721 | else |
| 2722 | di->di_result = NULL; |
| 2723 | _PyObject_GC_TRACK(di); |
| 2724 | return (PyObject *)di; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2725 | } |
| 2726 | |
| 2727 | static void |
| 2728 | dictiter_dealloc(dictiterobject *di) |
| 2729 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2730 | Py_XDECREF(di->di_dict); |
| 2731 | Py_XDECREF(di->di_result); |
| 2732 | PyObject_GC_Del(di); |
Antoine Pitrou | 7ddda78 | 2009-01-01 15:35:33 +0000 | [diff] [blame] | 2733 | } |
| 2734 | |
| 2735 | static int |
| 2736 | dictiter_traverse(dictiterobject *di, visitproc visit, void *arg) |
| 2737 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2738 | Py_VISIT(di->di_dict); |
| 2739 | Py_VISIT(di->di_result); |
| 2740 | return 0; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2741 | } |
| 2742 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 2743 | static PyObject * |
Raymond Hettinger | 0ce6dc8 | 2004-03-18 08:38:00 +0000 | [diff] [blame] | 2744 | dictiter_len(dictiterobject *di) |
| 2745 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2746 | Py_ssize_t len = 0; |
| 2747 | if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used) |
| 2748 | len = di->len; |
| 2749 | return PyLong_FromSize_t(len); |
Raymond Hettinger | 0ce6dc8 | 2004-03-18 08:38:00 +0000 | [diff] [blame] | 2750 | } |
| 2751 | |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 2752 | PyDoc_STRVAR(length_hint_doc, |
| 2753 | "Private method returning an estimate of len(list(it))."); |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 2754 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2755 | static PyObject * |
| 2756 | dictiter_reduce(dictiterobject *di); |
| 2757 | |
| 2758 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 2759 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 2760 | static PyMethodDef dictiter_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2761 | {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS, |
| 2762 | length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2763 | {"__reduce__", (PyCFunction)dictiter_reduce, METH_NOARGS, |
| 2764 | reduce_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2765 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | 0ce6dc8 | 2004-03-18 08:38:00 +0000 | [diff] [blame] | 2766 | }; |
| 2767 | |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2768 | static PyObject *dictiter_iternextkey(dictiterobject *di) |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2769 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2770 | PyObject *key; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2771 | register Py_ssize_t i, mask, offset; |
| 2772 | register PyDictKeysObject *k; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2773 | PyDictObject *d = di->di_dict; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2774 | PyObject **value_ptr; |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2775 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2776 | if (d == NULL) |
| 2777 | return NULL; |
| 2778 | assert (PyDict_Check(d)); |
Guido van Rossum | 2147df7 | 2002-07-16 20:30:22 +0000 | [diff] [blame] | 2779 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2780 | if (di->di_used != d->ma_used) { |
| 2781 | PyErr_SetString(PyExc_RuntimeError, |
| 2782 | "dictionary changed size during iteration"); |
| 2783 | di->di_used = -1; /* Make this state sticky */ |
| 2784 | return NULL; |
| 2785 | } |
Guido van Rossum | 2147df7 | 2002-07-16 20:30:22 +0000 | [diff] [blame] | 2786 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2787 | i = di->di_pos; |
| 2788 | if (i < 0) |
| 2789 | goto fail; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2790 | k = d->ma_keys; |
| 2791 | if (d->ma_values) { |
| 2792 | value_ptr = &d->ma_values[i]; |
| 2793 | offset = sizeof(PyObject *); |
| 2794 | } |
| 2795 | else { |
| 2796 | value_ptr = &k->dk_entries[i].me_value; |
| 2797 | offset = sizeof(PyDictKeyEntry); |
| 2798 | } |
| 2799 | mask = DK_SIZE(k)-1; |
| 2800 | while (i <= mask && *value_ptr == NULL) { |
| 2801 | value_ptr = (PyObject **)(((char *)value_ptr) + offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2802 | i++; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2803 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2804 | di->di_pos = i+1; |
| 2805 | if (i > mask) |
| 2806 | goto fail; |
| 2807 | di->len--; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2808 | key = k->dk_entries[i].me_key; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2809 | Py_INCREF(key); |
| 2810 | return key; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2811 | |
| 2812 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2813 | Py_DECREF(d); |
| 2814 | di->di_dict = NULL; |
| 2815 | return NULL; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2816 | } |
| 2817 | |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2818 | PyTypeObject PyDictIterKey_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2819 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2820 | "dict_keyiterator", /* tp_name */ |
| 2821 | sizeof(dictiterobject), /* tp_basicsize */ |
| 2822 | 0, /* tp_itemsize */ |
| 2823 | /* methods */ |
| 2824 | (destructor)dictiter_dealloc, /* tp_dealloc */ |
| 2825 | 0, /* tp_print */ |
| 2826 | 0, /* tp_getattr */ |
| 2827 | 0, /* tp_setattr */ |
| 2828 | 0, /* tp_reserved */ |
| 2829 | 0, /* tp_repr */ |
| 2830 | 0, /* tp_as_number */ |
| 2831 | 0, /* tp_as_sequence */ |
| 2832 | 0, /* tp_as_mapping */ |
| 2833 | 0, /* tp_hash */ |
| 2834 | 0, /* tp_call */ |
| 2835 | 0, /* tp_str */ |
| 2836 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2837 | 0, /* tp_setattro */ |
| 2838 | 0, /* tp_as_buffer */ |
| 2839 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 2840 | 0, /* tp_doc */ |
| 2841 | (traverseproc)dictiter_traverse, /* tp_traverse */ |
| 2842 | 0, /* tp_clear */ |
| 2843 | 0, /* tp_richcompare */ |
| 2844 | 0, /* tp_weaklistoffset */ |
| 2845 | PyObject_SelfIter, /* tp_iter */ |
| 2846 | (iternextfunc)dictiter_iternextkey, /* tp_iternext */ |
| 2847 | dictiter_methods, /* tp_methods */ |
| 2848 | 0, |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2849 | }; |
| 2850 | |
| 2851 | static PyObject *dictiter_iternextvalue(dictiterobject *di) |
| 2852 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2853 | PyObject *value; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2854 | register Py_ssize_t i, mask, offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2855 | PyDictObject *d = di->di_dict; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2856 | PyObject **value_ptr; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2857 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2858 | if (d == NULL) |
| 2859 | return NULL; |
| 2860 | assert (PyDict_Check(d)); |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2861 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2862 | if (di->di_used != d->ma_used) { |
| 2863 | PyErr_SetString(PyExc_RuntimeError, |
| 2864 | "dictionary changed size during iteration"); |
| 2865 | di->di_used = -1; /* Make this state sticky */ |
| 2866 | return NULL; |
| 2867 | } |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2868 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2869 | i = di->di_pos; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2870 | mask = DK_SIZE(d->ma_keys)-1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2871 | if (i < 0 || i > mask) |
| 2872 | goto fail; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2873 | if (d->ma_values) { |
| 2874 | value_ptr = &d->ma_values[i]; |
| 2875 | offset = sizeof(PyObject *); |
| 2876 | } |
| 2877 | else { |
| 2878 | value_ptr = &d->ma_keys->dk_entries[i].me_value; |
| 2879 | offset = sizeof(PyDictKeyEntry); |
| 2880 | } |
| 2881 | while (i <= mask && *value_ptr == NULL) { |
| 2882 | value_ptr = (PyObject **)(((char *)value_ptr) + offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2883 | i++; |
| 2884 | if (i > mask) |
| 2885 | goto fail; |
| 2886 | } |
| 2887 | di->di_pos = i+1; |
| 2888 | di->len--; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2889 | value = *value_ptr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2890 | Py_INCREF(value); |
| 2891 | return value; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2892 | |
| 2893 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2894 | Py_DECREF(d); |
| 2895 | di->di_dict = NULL; |
| 2896 | return NULL; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2897 | } |
| 2898 | |
| 2899 | PyTypeObject PyDictIterValue_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2900 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2901 | "dict_valueiterator", /* tp_name */ |
| 2902 | sizeof(dictiterobject), /* tp_basicsize */ |
| 2903 | 0, /* tp_itemsize */ |
| 2904 | /* methods */ |
| 2905 | (destructor)dictiter_dealloc, /* tp_dealloc */ |
| 2906 | 0, /* tp_print */ |
| 2907 | 0, /* tp_getattr */ |
| 2908 | 0, /* tp_setattr */ |
| 2909 | 0, /* tp_reserved */ |
| 2910 | 0, /* tp_repr */ |
| 2911 | 0, /* tp_as_number */ |
| 2912 | 0, /* tp_as_sequence */ |
| 2913 | 0, /* tp_as_mapping */ |
| 2914 | 0, /* tp_hash */ |
| 2915 | 0, /* tp_call */ |
| 2916 | 0, /* tp_str */ |
| 2917 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2918 | 0, /* tp_setattro */ |
| 2919 | 0, /* tp_as_buffer */ |
| 2920 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 2921 | 0, /* tp_doc */ |
| 2922 | (traverseproc)dictiter_traverse, /* tp_traverse */ |
| 2923 | 0, /* tp_clear */ |
| 2924 | 0, /* tp_richcompare */ |
| 2925 | 0, /* tp_weaklistoffset */ |
| 2926 | PyObject_SelfIter, /* tp_iter */ |
| 2927 | (iternextfunc)dictiter_iternextvalue, /* tp_iternext */ |
| 2928 | dictiter_methods, /* tp_methods */ |
| 2929 | 0, |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2930 | }; |
| 2931 | |
| 2932 | static PyObject *dictiter_iternextitem(dictiterobject *di) |
| 2933 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2934 | PyObject *key, *value, *result = di->di_result; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2935 | register Py_ssize_t i, mask, offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2936 | PyDictObject *d = di->di_dict; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2937 | PyObject **value_ptr; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2938 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2939 | if (d == NULL) |
| 2940 | return NULL; |
| 2941 | assert (PyDict_Check(d)); |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2942 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2943 | if (di->di_used != d->ma_used) { |
| 2944 | PyErr_SetString(PyExc_RuntimeError, |
| 2945 | "dictionary changed size during iteration"); |
| 2946 | di->di_used = -1; /* Make this state sticky */ |
| 2947 | return NULL; |
| 2948 | } |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2949 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2950 | i = di->di_pos; |
| 2951 | if (i < 0) |
| 2952 | goto fail; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2953 | mask = DK_SIZE(d->ma_keys)-1; |
| 2954 | if (d->ma_values) { |
| 2955 | value_ptr = &d->ma_values[i]; |
| 2956 | offset = sizeof(PyObject *); |
| 2957 | } |
| 2958 | else { |
| 2959 | value_ptr = &d->ma_keys->dk_entries[i].me_value; |
| 2960 | offset = sizeof(PyDictKeyEntry); |
| 2961 | } |
| 2962 | while (i <= mask && *value_ptr == NULL) { |
| 2963 | value_ptr = (PyObject **)(((char *)value_ptr) + offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2964 | i++; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2965 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2966 | di->di_pos = i+1; |
| 2967 | if (i > mask) |
| 2968 | goto fail; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2969 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2970 | if (result->ob_refcnt == 1) { |
| 2971 | Py_INCREF(result); |
| 2972 | Py_DECREF(PyTuple_GET_ITEM(result, 0)); |
| 2973 | Py_DECREF(PyTuple_GET_ITEM(result, 1)); |
| 2974 | } else { |
| 2975 | result = PyTuple_New(2); |
| 2976 | if (result == NULL) |
| 2977 | return NULL; |
| 2978 | } |
| 2979 | di->len--; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2980 | key = d->ma_keys->dk_entries[i].me_key; |
| 2981 | value = *value_ptr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2982 | Py_INCREF(key); |
| 2983 | Py_INCREF(value); |
| 2984 | PyTuple_SET_ITEM(result, 0, key); |
| 2985 | PyTuple_SET_ITEM(result, 1, value); |
| 2986 | return result; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2987 | |
| 2988 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2989 | Py_DECREF(d); |
| 2990 | di->di_dict = NULL; |
| 2991 | return NULL; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2992 | } |
| 2993 | |
| 2994 | PyTypeObject PyDictIterItem_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2995 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2996 | "dict_itemiterator", /* tp_name */ |
| 2997 | sizeof(dictiterobject), /* tp_basicsize */ |
| 2998 | 0, /* tp_itemsize */ |
| 2999 | /* methods */ |
| 3000 | (destructor)dictiter_dealloc, /* tp_dealloc */ |
| 3001 | 0, /* tp_print */ |
| 3002 | 0, /* tp_getattr */ |
| 3003 | 0, /* tp_setattr */ |
| 3004 | 0, /* tp_reserved */ |
| 3005 | 0, /* tp_repr */ |
| 3006 | 0, /* tp_as_number */ |
| 3007 | 0, /* tp_as_sequence */ |
| 3008 | 0, /* tp_as_mapping */ |
| 3009 | 0, /* tp_hash */ |
| 3010 | 0, /* tp_call */ |
| 3011 | 0, /* tp_str */ |
| 3012 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3013 | 0, /* tp_setattro */ |
| 3014 | 0, /* tp_as_buffer */ |
| 3015 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 3016 | 0, /* tp_doc */ |
| 3017 | (traverseproc)dictiter_traverse, /* tp_traverse */ |
| 3018 | 0, /* tp_clear */ |
| 3019 | 0, /* tp_richcompare */ |
| 3020 | 0, /* tp_weaklistoffset */ |
| 3021 | PyObject_SelfIter, /* tp_iter */ |
| 3022 | (iternextfunc)dictiter_iternextitem, /* tp_iternext */ |
| 3023 | dictiter_methods, /* tp_methods */ |
| 3024 | 0, |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3025 | }; |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3026 | |
| 3027 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3028 | static PyObject * |
| 3029 | dictiter_reduce(dictiterobject *di) |
| 3030 | { |
| 3031 | PyObject *list; |
| 3032 | dictiterobject tmp; |
| 3033 | |
| 3034 | list = PyList_New(0); |
| 3035 | if (!list) |
| 3036 | return NULL; |
| 3037 | |
| 3038 | /* copy the itertor state */ |
| 3039 | tmp = *di; |
| 3040 | Py_XINCREF(tmp.di_dict); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 3041 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3042 | /* iterate the temporary into a list */ |
| 3043 | for(;;) { |
| 3044 | PyObject *element = 0; |
| 3045 | if (Py_TYPE(di) == &PyDictIterItem_Type) |
| 3046 | element = dictiter_iternextitem(&tmp); |
| 3047 | else if (Py_TYPE(di) == &PyDictIterKey_Type) |
| 3048 | element = dictiter_iternextkey(&tmp); |
| 3049 | else if (Py_TYPE(di) == &PyDictIterValue_Type) |
| 3050 | element = dictiter_iternextvalue(&tmp); |
| 3051 | else |
| 3052 | assert(0); |
| 3053 | if (element) { |
| 3054 | if (PyList_Append(list, element)) { |
| 3055 | Py_DECREF(element); |
| 3056 | Py_DECREF(list); |
| 3057 | Py_XDECREF(tmp.di_dict); |
| 3058 | return NULL; |
| 3059 | } |
| 3060 | Py_DECREF(element); |
| 3061 | } else |
| 3062 | break; |
| 3063 | } |
| 3064 | Py_XDECREF(tmp.di_dict); |
| 3065 | /* check for error */ |
| 3066 | if (tmp.di_dict != NULL) { |
| 3067 | /* we have an error */ |
| 3068 | Py_DECREF(list); |
| 3069 | return NULL; |
| 3070 | } |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 3071 | return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3072 | } |
| 3073 | |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 3074 | /***********************************************/ |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3075 | /* View objects for keys(), items(), values(). */ |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 3076 | /***********************************************/ |
| 3077 | |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3078 | /* The instance lay-out is the same for all three; but the type differs. */ |
| 3079 | |
| 3080 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3081 | PyObject_HEAD |
| 3082 | PyDictObject *dv_dict; |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3083 | } dictviewobject; |
| 3084 | |
| 3085 | |
| 3086 | static void |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 3087 | dictview_dealloc(dictviewobject *dv) |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3088 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3089 | Py_XDECREF(dv->dv_dict); |
| 3090 | PyObject_GC_Del(dv); |
Antoine Pitrou | 7ddda78 | 2009-01-01 15:35:33 +0000 | [diff] [blame] | 3091 | } |
| 3092 | |
| 3093 | static int |
| 3094 | dictview_traverse(dictviewobject *dv, visitproc visit, void *arg) |
| 3095 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3096 | Py_VISIT(dv->dv_dict); |
| 3097 | return 0; |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3098 | } |
| 3099 | |
Guido van Rossum | 83825ac | 2007-02-10 04:54:19 +0000 | [diff] [blame] | 3100 | static Py_ssize_t |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 3101 | dictview_len(dictviewobject *dv) |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3102 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3103 | Py_ssize_t len = 0; |
| 3104 | if (dv->dv_dict != NULL) |
| 3105 | len = dv->dv_dict->ma_used; |
| 3106 | return len; |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3107 | } |
| 3108 | |
| 3109 | static PyObject * |
| 3110 | dictview_new(PyObject *dict, PyTypeObject *type) |
| 3111 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3112 | dictviewobject *dv; |
| 3113 | if (dict == NULL) { |
| 3114 | PyErr_BadInternalCall(); |
| 3115 | return NULL; |
| 3116 | } |
| 3117 | if (!PyDict_Check(dict)) { |
| 3118 | /* XXX Get rid of this restriction later */ |
| 3119 | PyErr_Format(PyExc_TypeError, |
| 3120 | "%s() requires a dict argument, not '%s'", |
| 3121 | type->tp_name, dict->ob_type->tp_name); |
| 3122 | return NULL; |
| 3123 | } |
| 3124 | dv = PyObject_GC_New(dictviewobject, type); |
| 3125 | if (dv == NULL) |
| 3126 | return NULL; |
| 3127 | Py_INCREF(dict); |
| 3128 | dv->dv_dict = (PyDictObject *)dict; |
| 3129 | _PyObject_GC_TRACK(dv); |
| 3130 | return (PyObject *)dv; |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3131 | } |
| 3132 | |
Neal Norwitz | e36f2ba | 2007-02-26 23:12:28 +0000 | [diff] [blame] | 3133 | /* TODO(guido): The views objects are not complete: |
| 3134 | |
| 3135 | * support more set operations |
| 3136 | * support arbitrary mappings? |
| 3137 | - either these should be static or exported in dictobject.h |
| 3138 | - if public then they should probably be in builtins |
| 3139 | */ |
| 3140 | |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 3141 | /* Return 1 if self is a subset of other, iterating over self; |
| 3142 | 0 if not; -1 if an error occurred. */ |
Guido van Rossum | d9214d1 | 2007-02-12 02:23:40 +0000 | [diff] [blame] | 3143 | static int |
| 3144 | all_contained_in(PyObject *self, PyObject *other) |
| 3145 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3146 | PyObject *iter = PyObject_GetIter(self); |
| 3147 | int ok = 1; |
Guido van Rossum | d9214d1 | 2007-02-12 02:23:40 +0000 | [diff] [blame] | 3148 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3149 | if (iter == NULL) |
| 3150 | return -1; |
| 3151 | for (;;) { |
| 3152 | PyObject *next = PyIter_Next(iter); |
| 3153 | if (next == NULL) { |
| 3154 | if (PyErr_Occurred()) |
| 3155 | ok = -1; |
| 3156 | break; |
| 3157 | } |
| 3158 | ok = PySequence_Contains(other, next); |
| 3159 | Py_DECREF(next); |
| 3160 | if (ok <= 0) |
| 3161 | break; |
| 3162 | } |
| 3163 | Py_DECREF(iter); |
| 3164 | return ok; |
Guido van Rossum | d9214d1 | 2007-02-12 02:23:40 +0000 | [diff] [blame] | 3165 | } |
| 3166 | |
| 3167 | static PyObject * |
| 3168 | dictview_richcompare(PyObject *self, PyObject *other, int op) |
| 3169 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3170 | Py_ssize_t len_self, len_other; |
| 3171 | int ok; |
| 3172 | PyObject *result; |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 3173 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3174 | assert(self != NULL); |
| 3175 | assert(PyDictViewSet_Check(self)); |
| 3176 | assert(other != NULL); |
Guido van Rossum | d9214d1 | 2007-02-12 02:23:40 +0000 | [diff] [blame] | 3177 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 3178 | if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other)) |
| 3179 | Py_RETURN_NOTIMPLEMENTED; |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 3180 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3181 | len_self = PyObject_Size(self); |
| 3182 | if (len_self < 0) |
| 3183 | return NULL; |
| 3184 | len_other = PyObject_Size(other); |
| 3185 | if (len_other < 0) |
| 3186 | return NULL; |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 3187 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3188 | ok = 0; |
| 3189 | switch(op) { |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 3190 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3191 | case Py_NE: |
| 3192 | case Py_EQ: |
| 3193 | if (len_self == len_other) |
| 3194 | ok = all_contained_in(self, other); |
| 3195 | if (op == Py_NE && ok >= 0) |
| 3196 | ok = !ok; |
| 3197 | break; |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 3198 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3199 | case Py_LT: |
| 3200 | if (len_self < len_other) |
| 3201 | ok = all_contained_in(self, other); |
| 3202 | break; |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 3203 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3204 | case Py_LE: |
| 3205 | if (len_self <= len_other) |
| 3206 | ok = all_contained_in(self, other); |
| 3207 | break; |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 3208 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3209 | case Py_GT: |
| 3210 | if (len_self > len_other) |
| 3211 | ok = all_contained_in(other, self); |
| 3212 | break; |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 3213 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3214 | case Py_GE: |
| 3215 | if (len_self >= len_other) |
| 3216 | ok = all_contained_in(other, self); |
| 3217 | break; |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 3218 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3219 | } |
| 3220 | if (ok < 0) |
| 3221 | return NULL; |
| 3222 | result = ok ? Py_True : Py_False; |
| 3223 | Py_INCREF(result); |
| 3224 | return result; |
Guido van Rossum | d9214d1 | 2007-02-12 02:23:40 +0000 | [diff] [blame] | 3225 | } |
| 3226 | |
Raymond Hettinger | b0d56af | 2009-03-03 10:52:49 +0000 | [diff] [blame] | 3227 | static PyObject * |
| 3228 | dictview_repr(dictviewobject *dv) |
| 3229 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3230 | PyObject *seq; |
| 3231 | PyObject *result; |
Raymond Hettinger | b0d56af | 2009-03-03 10:52:49 +0000 | [diff] [blame] | 3232 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3233 | seq = PySequence_List((PyObject *)dv); |
| 3234 | if (seq == NULL) |
| 3235 | return NULL; |
| 3236 | |
| 3237 | result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq); |
| 3238 | Py_DECREF(seq); |
| 3239 | return result; |
Raymond Hettinger | b0d56af | 2009-03-03 10:52:49 +0000 | [diff] [blame] | 3240 | } |
| 3241 | |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 3242 | /*** dict_keys ***/ |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3243 | |
| 3244 | static PyObject * |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 3245 | dictkeys_iter(dictviewobject *dv) |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3246 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3247 | if (dv->dv_dict == NULL) { |
| 3248 | Py_RETURN_NONE; |
| 3249 | } |
| 3250 | return dictiter_new(dv->dv_dict, &PyDictIterKey_Type); |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 3251 | } |
| 3252 | |
| 3253 | static int |
| 3254 | dictkeys_contains(dictviewobject *dv, PyObject *obj) |
| 3255 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3256 | if (dv->dv_dict == NULL) |
| 3257 | return 0; |
| 3258 | return PyDict_Contains((PyObject *)dv->dv_dict, obj); |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3259 | } |
| 3260 | |
Guido van Rossum | 83825ac | 2007-02-10 04:54:19 +0000 | [diff] [blame] | 3261 | static PySequenceMethods dictkeys_as_sequence = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3262 | (lenfunc)dictview_len, /* sq_length */ |
| 3263 | 0, /* sq_concat */ |
| 3264 | 0, /* sq_repeat */ |
| 3265 | 0, /* sq_item */ |
| 3266 | 0, /* sq_slice */ |
| 3267 | 0, /* sq_ass_item */ |
| 3268 | 0, /* sq_ass_slice */ |
| 3269 | (objobjproc)dictkeys_contains, /* sq_contains */ |
Guido van Rossum | 83825ac | 2007-02-10 04:54:19 +0000 | [diff] [blame] | 3270 | }; |
| 3271 | |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3272 | static PyObject* |
| 3273 | dictviews_sub(PyObject* self, PyObject *other) |
| 3274 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3275 | PyObject *result = PySet_New(self); |
| 3276 | PyObject *tmp; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3277 | _Py_IDENTIFIER(difference_update); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 3278 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3279 | if (result == NULL) |
| 3280 | return NULL; |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3281 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 3282 | tmp = _PyObject_CallMethodId(result, &PyId_difference_update, "O", other); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3283 | if (tmp == NULL) { |
| 3284 | Py_DECREF(result); |
| 3285 | return NULL; |
| 3286 | } |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3287 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3288 | Py_DECREF(tmp); |
| 3289 | return result; |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3290 | } |
| 3291 | |
| 3292 | static PyObject* |
| 3293 | dictviews_and(PyObject* self, PyObject *other) |
| 3294 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3295 | PyObject *result = PySet_New(self); |
| 3296 | PyObject *tmp; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3297 | _Py_IDENTIFIER(intersection_update); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 3298 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3299 | if (result == NULL) |
| 3300 | return NULL; |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3301 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 3302 | tmp = _PyObject_CallMethodId(result, &PyId_intersection_update, "O", other); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3303 | if (tmp == NULL) { |
| 3304 | Py_DECREF(result); |
| 3305 | return NULL; |
| 3306 | } |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3307 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3308 | Py_DECREF(tmp); |
| 3309 | return result; |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3310 | } |
| 3311 | |
| 3312 | static PyObject* |
| 3313 | dictviews_or(PyObject* self, PyObject *other) |
| 3314 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3315 | PyObject *result = PySet_New(self); |
| 3316 | PyObject *tmp; |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 3317 | _Py_IDENTIFIER(update); |
Victor Stinner | d1a9cc2 | 2011-10-13 22:51:17 +0200 | [diff] [blame] | 3318 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3319 | if (result == NULL) |
| 3320 | return NULL; |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3321 | |
Victor Stinner | d1a9cc2 | 2011-10-13 22:51:17 +0200 | [diff] [blame] | 3322 | tmp = _PyObject_CallMethodId(result, &PyId_update, "O", other); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3323 | if (tmp == NULL) { |
| 3324 | Py_DECREF(result); |
| 3325 | return NULL; |
| 3326 | } |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3327 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3328 | Py_DECREF(tmp); |
| 3329 | return result; |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3330 | } |
| 3331 | |
| 3332 | static PyObject* |
| 3333 | dictviews_xor(PyObject* self, PyObject *other) |
| 3334 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3335 | PyObject *result = PySet_New(self); |
| 3336 | PyObject *tmp; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3337 | _Py_IDENTIFIER(symmetric_difference_update); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 3338 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3339 | if (result == NULL) |
| 3340 | return NULL; |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3341 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 3342 | tmp = _PyObject_CallMethodId(result, &PyId_symmetric_difference_update, "O", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3343 | other); |
| 3344 | if (tmp == NULL) { |
| 3345 | Py_DECREF(result); |
| 3346 | return NULL; |
| 3347 | } |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3348 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3349 | Py_DECREF(tmp); |
| 3350 | return result; |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3351 | } |
| 3352 | |
| 3353 | static PyNumberMethods dictviews_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3354 | 0, /*nb_add*/ |
| 3355 | (binaryfunc)dictviews_sub, /*nb_subtract*/ |
| 3356 | 0, /*nb_multiply*/ |
| 3357 | 0, /*nb_remainder*/ |
| 3358 | 0, /*nb_divmod*/ |
| 3359 | 0, /*nb_power*/ |
| 3360 | 0, /*nb_negative*/ |
| 3361 | 0, /*nb_positive*/ |
| 3362 | 0, /*nb_absolute*/ |
| 3363 | 0, /*nb_bool*/ |
| 3364 | 0, /*nb_invert*/ |
| 3365 | 0, /*nb_lshift*/ |
| 3366 | 0, /*nb_rshift*/ |
| 3367 | (binaryfunc)dictviews_and, /*nb_and*/ |
| 3368 | (binaryfunc)dictviews_xor, /*nb_xor*/ |
| 3369 | (binaryfunc)dictviews_or, /*nb_or*/ |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3370 | }; |
| 3371 | |
Daniel Stutzbach | 045b3ba | 2010-09-02 15:06:06 +0000 | [diff] [blame] | 3372 | static PyObject* |
| 3373 | dictviews_isdisjoint(PyObject *self, PyObject *other) |
| 3374 | { |
| 3375 | PyObject *it; |
| 3376 | PyObject *item = NULL; |
| 3377 | |
| 3378 | if (self == other) { |
| 3379 | if (dictview_len((dictviewobject *)self) == 0) |
| 3380 | Py_RETURN_TRUE; |
| 3381 | else |
| 3382 | Py_RETURN_FALSE; |
| 3383 | } |
| 3384 | |
| 3385 | /* Iterate over the shorter object (only if other is a set, |
| 3386 | * because PySequence_Contains may be expensive otherwise): */ |
| 3387 | if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) { |
| 3388 | Py_ssize_t len_self = dictview_len((dictviewobject *)self); |
| 3389 | Py_ssize_t len_other = PyObject_Size(other); |
| 3390 | if (len_other == -1) |
| 3391 | return NULL; |
| 3392 | |
| 3393 | if ((len_other > len_self)) { |
| 3394 | PyObject *tmp = other; |
| 3395 | other = self; |
| 3396 | self = tmp; |
| 3397 | } |
| 3398 | } |
| 3399 | |
| 3400 | it = PyObject_GetIter(other); |
| 3401 | if (it == NULL) |
| 3402 | return NULL; |
| 3403 | |
| 3404 | while ((item = PyIter_Next(it)) != NULL) { |
| 3405 | int contains = PySequence_Contains(self, item); |
| 3406 | Py_DECREF(item); |
| 3407 | if (contains == -1) { |
| 3408 | Py_DECREF(it); |
| 3409 | return NULL; |
| 3410 | } |
| 3411 | |
| 3412 | if (contains) { |
| 3413 | Py_DECREF(it); |
| 3414 | Py_RETURN_FALSE; |
| 3415 | } |
| 3416 | } |
| 3417 | Py_DECREF(it); |
| 3418 | if (PyErr_Occurred()) |
| 3419 | return NULL; /* PyIter_Next raised an exception. */ |
| 3420 | Py_RETURN_TRUE; |
| 3421 | } |
| 3422 | |
| 3423 | PyDoc_STRVAR(isdisjoint_doc, |
| 3424 | "Return True if the view and the given iterable have a null intersection."); |
| 3425 | |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3426 | static PyMethodDef dictkeys_methods[] = { |
Daniel Stutzbach | 045b3ba | 2010-09-02 15:06:06 +0000 | [diff] [blame] | 3427 | {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O, |
| 3428 | isdisjoint_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3429 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3430 | }; |
| 3431 | |
| 3432 | PyTypeObject PyDictKeys_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3433 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 3434 | "dict_keys", /* tp_name */ |
| 3435 | sizeof(dictviewobject), /* tp_basicsize */ |
| 3436 | 0, /* tp_itemsize */ |
| 3437 | /* methods */ |
| 3438 | (destructor)dictview_dealloc, /* tp_dealloc */ |
| 3439 | 0, /* tp_print */ |
| 3440 | 0, /* tp_getattr */ |
| 3441 | 0, /* tp_setattr */ |
| 3442 | 0, /* tp_reserved */ |
| 3443 | (reprfunc)dictview_repr, /* tp_repr */ |
| 3444 | &dictviews_as_number, /* tp_as_number */ |
| 3445 | &dictkeys_as_sequence, /* tp_as_sequence */ |
| 3446 | 0, /* tp_as_mapping */ |
| 3447 | 0, /* tp_hash */ |
| 3448 | 0, /* tp_call */ |
| 3449 | 0, /* tp_str */ |
| 3450 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3451 | 0, /* tp_setattro */ |
| 3452 | 0, /* tp_as_buffer */ |
| 3453 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 3454 | 0, /* tp_doc */ |
| 3455 | (traverseproc)dictview_traverse, /* tp_traverse */ |
| 3456 | 0, /* tp_clear */ |
| 3457 | dictview_richcompare, /* tp_richcompare */ |
| 3458 | 0, /* tp_weaklistoffset */ |
| 3459 | (getiterfunc)dictkeys_iter, /* tp_iter */ |
| 3460 | 0, /* tp_iternext */ |
| 3461 | dictkeys_methods, /* tp_methods */ |
| 3462 | 0, |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3463 | }; |
| 3464 | |
| 3465 | static PyObject * |
| 3466 | dictkeys_new(PyObject *dict) |
| 3467 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3468 | return dictview_new(dict, &PyDictKeys_Type); |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3469 | } |
| 3470 | |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 3471 | /*** dict_items ***/ |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3472 | |
| 3473 | static PyObject * |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 3474 | dictitems_iter(dictviewobject *dv) |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3475 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3476 | if (dv->dv_dict == NULL) { |
| 3477 | Py_RETURN_NONE; |
| 3478 | } |
| 3479 | return dictiter_new(dv->dv_dict, &PyDictIterItem_Type); |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 3480 | } |
| 3481 | |
| 3482 | static int |
| 3483 | dictitems_contains(dictviewobject *dv, PyObject *obj) |
| 3484 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3485 | PyObject *key, *value, *found; |
| 3486 | if (dv->dv_dict == NULL) |
| 3487 | return 0; |
| 3488 | if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2) |
| 3489 | return 0; |
| 3490 | key = PyTuple_GET_ITEM(obj, 0); |
| 3491 | value = PyTuple_GET_ITEM(obj, 1); |
| 3492 | found = PyDict_GetItem((PyObject *)dv->dv_dict, key); |
| 3493 | if (found == NULL) { |
| 3494 | if (PyErr_Occurred()) |
| 3495 | return -1; |
| 3496 | return 0; |
| 3497 | } |
| 3498 | return PyObject_RichCompareBool(value, found, Py_EQ); |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3499 | } |
| 3500 | |
Guido van Rossum | 83825ac | 2007-02-10 04:54:19 +0000 | [diff] [blame] | 3501 | static PySequenceMethods dictitems_as_sequence = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3502 | (lenfunc)dictview_len, /* sq_length */ |
| 3503 | 0, /* sq_concat */ |
| 3504 | 0, /* sq_repeat */ |
| 3505 | 0, /* sq_item */ |
| 3506 | 0, /* sq_slice */ |
| 3507 | 0, /* sq_ass_item */ |
| 3508 | 0, /* sq_ass_slice */ |
| 3509 | (objobjproc)dictitems_contains, /* sq_contains */ |
Guido van Rossum | 83825ac | 2007-02-10 04:54:19 +0000 | [diff] [blame] | 3510 | }; |
| 3511 | |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3512 | static PyMethodDef dictitems_methods[] = { |
Daniel Stutzbach | 045b3ba | 2010-09-02 15:06:06 +0000 | [diff] [blame] | 3513 | {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O, |
| 3514 | isdisjoint_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3515 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3516 | }; |
| 3517 | |
| 3518 | PyTypeObject PyDictItems_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3519 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 3520 | "dict_items", /* tp_name */ |
| 3521 | sizeof(dictviewobject), /* tp_basicsize */ |
| 3522 | 0, /* tp_itemsize */ |
| 3523 | /* methods */ |
| 3524 | (destructor)dictview_dealloc, /* tp_dealloc */ |
| 3525 | 0, /* tp_print */ |
| 3526 | 0, /* tp_getattr */ |
| 3527 | 0, /* tp_setattr */ |
| 3528 | 0, /* tp_reserved */ |
| 3529 | (reprfunc)dictview_repr, /* tp_repr */ |
| 3530 | &dictviews_as_number, /* tp_as_number */ |
| 3531 | &dictitems_as_sequence, /* tp_as_sequence */ |
| 3532 | 0, /* tp_as_mapping */ |
| 3533 | 0, /* tp_hash */ |
| 3534 | 0, /* tp_call */ |
| 3535 | 0, /* tp_str */ |
| 3536 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3537 | 0, /* tp_setattro */ |
| 3538 | 0, /* tp_as_buffer */ |
| 3539 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 3540 | 0, /* tp_doc */ |
| 3541 | (traverseproc)dictview_traverse, /* tp_traverse */ |
| 3542 | 0, /* tp_clear */ |
| 3543 | dictview_richcompare, /* tp_richcompare */ |
| 3544 | 0, /* tp_weaklistoffset */ |
| 3545 | (getiterfunc)dictitems_iter, /* tp_iter */ |
| 3546 | 0, /* tp_iternext */ |
| 3547 | dictitems_methods, /* tp_methods */ |
| 3548 | 0, |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3549 | }; |
| 3550 | |
| 3551 | static PyObject * |
| 3552 | dictitems_new(PyObject *dict) |
| 3553 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3554 | return dictview_new(dict, &PyDictItems_Type); |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3555 | } |
| 3556 | |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 3557 | /*** dict_values ***/ |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3558 | |
| 3559 | static PyObject * |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 3560 | dictvalues_iter(dictviewobject *dv) |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3561 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3562 | if (dv->dv_dict == NULL) { |
| 3563 | Py_RETURN_NONE; |
| 3564 | } |
| 3565 | return dictiter_new(dv->dv_dict, &PyDictIterValue_Type); |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3566 | } |
| 3567 | |
Guido van Rossum | 83825ac | 2007-02-10 04:54:19 +0000 | [diff] [blame] | 3568 | static PySequenceMethods dictvalues_as_sequence = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3569 | (lenfunc)dictview_len, /* sq_length */ |
| 3570 | 0, /* sq_concat */ |
| 3571 | 0, /* sq_repeat */ |
| 3572 | 0, /* sq_item */ |
| 3573 | 0, /* sq_slice */ |
| 3574 | 0, /* sq_ass_item */ |
| 3575 | 0, /* sq_ass_slice */ |
| 3576 | (objobjproc)0, /* sq_contains */ |
Guido van Rossum | 83825ac | 2007-02-10 04:54:19 +0000 | [diff] [blame] | 3577 | }; |
| 3578 | |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3579 | static PyMethodDef dictvalues_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3580 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3581 | }; |
| 3582 | |
| 3583 | PyTypeObject PyDictValues_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3584 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 3585 | "dict_values", /* tp_name */ |
| 3586 | sizeof(dictviewobject), /* tp_basicsize */ |
| 3587 | 0, /* tp_itemsize */ |
| 3588 | /* methods */ |
| 3589 | (destructor)dictview_dealloc, /* tp_dealloc */ |
| 3590 | 0, /* tp_print */ |
| 3591 | 0, /* tp_getattr */ |
| 3592 | 0, /* tp_setattr */ |
| 3593 | 0, /* tp_reserved */ |
| 3594 | (reprfunc)dictview_repr, /* tp_repr */ |
| 3595 | 0, /* tp_as_number */ |
| 3596 | &dictvalues_as_sequence, /* tp_as_sequence */ |
| 3597 | 0, /* tp_as_mapping */ |
| 3598 | 0, /* tp_hash */ |
| 3599 | 0, /* tp_call */ |
| 3600 | 0, /* tp_str */ |
| 3601 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3602 | 0, /* tp_setattro */ |
| 3603 | 0, /* tp_as_buffer */ |
| 3604 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 3605 | 0, /* tp_doc */ |
| 3606 | (traverseproc)dictview_traverse, /* tp_traverse */ |
| 3607 | 0, /* tp_clear */ |
| 3608 | 0, /* tp_richcompare */ |
| 3609 | 0, /* tp_weaklistoffset */ |
| 3610 | (getiterfunc)dictvalues_iter, /* tp_iter */ |
| 3611 | 0, /* tp_iternext */ |
| 3612 | dictvalues_methods, /* tp_methods */ |
| 3613 | 0, |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3614 | }; |
| 3615 | |
| 3616 | static PyObject * |
| 3617 | dictvalues_new(PyObject *dict) |
| 3618 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3619 | return dictview_new(dict, &PyDictValues_Type); |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3620 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 3621 | |
| 3622 | /* Returns NULL if cannot allocate a new PyDictKeysObject, |
| 3623 | but does not set an error */ |
| 3624 | PyDictKeysObject * |
| 3625 | _PyDict_NewKeysForClass(void) |
| 3626 | { |
| 3627 | PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE_SPLIT); |
| 3628 | if (keys == NULL) |
| 3629 | PyErr_Clear(); |
| 3630 | else |
| 3631 | keys->dk_lookup = lookdict_split; |
| 3632 | return keys; |
| 3633 | } |
| 3634 | |
| 3635 | #define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys) |
| 3636 | |
| 3637 | PyObject * |
| 3638 | PyObject_GenericGetDict(PyObject *obj, void *context) |
| 3639 | { |
| 3640 | PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj); |
| 3641 | if (dictptr == NULL) { |
| 3642 | PyErr_SetString(PyExc_AttributeError, |
| 3643 | "This object has no __dict__"); |
| 3644 | return NULL; |
| 3645 | } |
| 3646 | dict = *dictptr; |
| 3647 | if (dict == NULL) { |
| 3648 | PyTypeObject *tp = Py_TYPE(obj); |
| 3649 | if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) { |
| 3650 | DK_INCREF(CACHED_KEYS(tp)); |
| 3651 | *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp)); |
| 3652 | } |
| 3653 | else { |
| 3654 | *dictptr = dict = PyDict_New(); |
| 3655 | } |
| 3656 | } |
| 3657 | Py_XINCREF(dict); |
| 3658 | return dict; |
| 3659 | } |
| 3660 | |
| 3661 | int |
| 3662 | _PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr, |
| 3663 | PyObject *key, PyObject *value) |
| 3664 | { |
| 3665 | PyObject *dict; |
| 3666 | int res; |
| 3667 | PyDictKeysObject *cached; |
| 3668 | |
| 3669 | assert(dictptr != NULL); |
| 3670 | if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) { |
| 3671 | assert(dictptr != NULL); |
| 3672 | dict = *dictptr; |
| 3673 | if (dict == NULL) { |
| 3674 | DK_INCREF(cached); |
| 3675 | dict = new_dict_with_shared_keys(cached); |
| 3676 | if (dict == NULL) |
| 3677 | return -1; |
| 3678 | *dictptr = dict; |
| 3679 | } |
| 3680 | if (value == NULL) { |
| 3681 | res = PyDict_DelItem(dict, key); |
| 3682 | if (cached != ((PyDictObject *)dict)->ma_keys) { |
| 3683 | CACHED_KEYS(tp) = NULL; |
| 3684 | DK_DECREF(cached); |
| 3685 | } |
| 3686 | } else { |
| 3687 | res = PyDict_SetItem(dict, key, value); |
| 3688 | if (cached != ((PyDictObject *)dict)->ma_keys) { |
| 3689 | /* Either update tp->ht_cached_keys or delete it */ |
Benjamin Peterson | 15ee821 | 2012-04-24 14:44:18 -0400 | [diff] [blame] | 3690 | if (cached->dk_refcnt == 1) { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 3691 | CACHED_KEYS(tp) = make_keys_shared(dict); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 3692 | } else { |
| 3693 | CACHED_KEYS(tp) = NULL; |
| 3694 | } |
| 3695 | DK_DECREF(cached); |
Benjamin Peterson | 15ee821 | 2012-04-24 14:44:18 -0400 | [diff] [blame] | 3696 | if (CACHED_KEYS(tp) == NULL && PyErr_Occurred()) |
| 3697 | return -1; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 3698 | } |
| 3699 | } |
| 3700 | } else { |
| 3701 | dict = *dictptr; |
| 3702 | if (dict == NULL) { |
| 3703 | dict = PyDict_New(); |
| 3704 | if (dict == NULL) |
| 3705 | return -1; |
| 3706 | *dictptr = dict; |
| 3707 | } |
| 3708 | if (value == NULL) { |
| 3709 | res = PyDict_DelItem(dict, key); |
| 3710 | } else { |
| 3711 | res = PyDict_SetItem(dict, key, value); |
| 3712 | } |
| 3713 | } |
| 3714 | return res; |
| 3715 | } |
| 3716 | |
| 3717 | void |
| 3718 | _PyDictKeys_DecRef(PyDictKeysObject *keys) |
| 3719 | { |
| 3720 | DK_DECREF(keys); |
| 3721 | } |
| 3722 | |
| 3723 | |
| 3724 | /* ARGSUSED */ |
| 3725 | static PyObject * |
| 3726 | dummy_repr(PyObject *op) |
| 3727 | { |
| 3728 | return PyUnicode_FromString("<dummy key>"); |
| 3729 | } |
| 3730 | |
| 3731 | /* ARGUSED */ |
| 3732 | static void |
| 3733 | dummy_dealloc(PyObject* ignore) |
| 3734 | { |
| 3735 | /* This should never get called, but we also don't want to SEGV if |
| 3736 | * we accidentally decref dummy-key out of existence. |
| 3737 | */ |
| 3738 | Py_FatalError("deallocating <dummy key>"); |
| 3739 | } |
| 3740 | |
| 3741 | static PyTypeObject PyDictDummy_Type = { |
| 3742 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 3743 | "<dummy key> type", |
| 3744 | 0, |
| 3745 | 0, |
| 3746 | dummy_dealloc, /*tp_dealloc*/ /*never called*/ |
| 3747 | 0, /*tp_print*/ |
| 3748 | 0, /*tp_getattr*/ |
| 3749 | 0, /*tp_setattr*/ |
| 3750 | 0, /*tp_reserved*/ |
| 3751 | dummy_repr, /*tp_repr*/ |
| 3752 | 0, /*tp_as_number*/ |
| 3753 | 0, /*tp_as_sequence*/ |
| 3754 | 0, /*tp_as_mapping*/ |
| 3755 | 0, /*tp_hash */ |
| 3756 | 0, /*tp_call */ |
| 3757 | 0, /*tp_str */ |
| 3758 | 0, /*tp_getattro */ |
| 3759 | 0, /*tp_setattro */ |
| 3760 | 0, /*tp_as_buffer */ |
| 3761 | Py_TPFLAGS_DEFAULT, /*tp_flags */ |
| 3762 | }; |
| 3763 | |
| 3764 | static PyObject _dummy_struct = { |
| 3765 | _PyObject_EXTRA_INIT |
| 3766 | 2, &PyDictDummy_Type |
| 3767 | }; |
| 3768 | |