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. |
| 778 | Eats a reference to key and one to value. |
| 779 | Returns -1 if an error occurred, or 0 on success. |
| 780 | */ |
| 781 | static int |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 782 | insertdict(PyDictObject *mp, PyObject *key, Py_hash_t hash, PyObject *value) |
Antoine Pitrou | e965d97 | 2012-02-27 00:45:12 +0100 | [diff] [blame] | 783 | { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 784 | PyObject *old_value; |
| 785 | PyObject **value_addr; |
| 786 | PyDictKeyEntry *ep; |
| 787 | assert(key != dummy); |
Antoine Pitrou | e965d97 | 2012-02-27 00:45:12 +0100 | [diff] [blame] | 788 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 789 | if (mp->ma_values != NULL && !PyUnicode_CheckExact(key)) { |
| 790 | if (insertion_resize(mp) < 0) |
| 791 | return -1; |
| 792 | } |
| 793 | |
| 794 | ep = mp->ma_keys->dk_lookup(mp, key, hash, &value_addr); |
Antoine Pitrou | e965d97 | 2012-02-27 00:45:12 +0100 | [diff] [blame] | 795 | if (ep == NULL) { |
| 796 | Py_DECREF(key); |
| 797 | Py_DECREF(value); |
| 798 | return -1; |
| 799 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 800 | MAINTAIN_TRACKING(mp, key, value); |
| 801 | old_value = *value_addr; |
| 802 | if (old_value != NULL) { |
| 803 | assert(ep->me_key != NULL && ep->me_key != dummy); |
| 804 | *value_addr = value; |
| 805 | Py_DECREF(old_value); /* which **CAN** re-enter */ |
| 806 | Py_DECREF(key); |
| 807 | } |
| 808 | else { |
| 809 | if (ep->me_key == NULL) { |
| 810 | if (mp->ma_keys->dk_usable <= 0) { |
| 811 | /* Need to resize. */ |
| 812 | if (insertion_resize(mp) < 0) { |
| 813 | Py_DECREF(key); |
| 814 | Py_DECREF(value); |
| 815 | return -1; |
| 816 | } |
| 817 | ep = find_empty_slot(mp, key, hash, &value_addr); |
| 818 | } |
| 819 | mp->ma_keys->dk_usable--; |
| 820 | assert(mp->ma_keys->dk_usable >= 0); |
| 821 | ep->me_key = key; |
| 822 | ep->me_hash = hash; |
| 823 | } |
| 824 | else { |
| 825 | if (ep->me_key == dummy) { |
| 826 | ep->me_key = key; |
| 827 | ep->me_hash = hash; |
| 828 | Py_DECREF(dummy); |
| 829 | } else { |
| 830 | Py_DECREF(key); |
| 831 | assert(_PyDict_HasSplitTable(mp)); |
| 832 | } |
| 833 | } |
| 834 | mp->ma_used++; |
| 835 | *value_addr = value; |
| 836 | } |
| 837 | assert(ep->me_key != NULL && ep->me_key != dummy); |
| 838 | assert(PyUnicode_CheckExact(key) || mp->ma_keys->dk_lookup == lookdict); |
| 839 | return 0; |
Antoine Pitrou | e965d97 | 2012-02-27 00:45:12 +0100 | [diff] [blame] | 840 | } |
| 841 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 842 | /* |
| 843 | Internal routine used by dictresize() to insert an item which is |
| 844 | known to be absent from the dict. This routine also assumes that |
| 845 | the dict contains no deleted entries. Besides the performance benefit, |
| 846 | using insertdict() in dictresize() is dangerous (SF bug #1456209). |
| 847 | Note that no refcounts are changed by this routine; if needed, the caller |
| 848 | is responsible for incref'ing `key` and `value`. |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 849 | Neither mp->ma_used nor k->dk_usable are modified by this routine; the caller |
| 850 | must set them correctly |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 851 | */ |
| 852 | static void |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 853 | insertdict_clean(PyDictObject *mp, PyObject *key, Py_hash_t hash, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 854 | PyObject *value) |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 855 | { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 856 | size_t i; |
| 857 | size_t perturb; |
| 858 | PyDictKeysObject *k = mp->ma_keys; |
| 859 | size_t mask = (size_t)DK_SIZE(k)-1; |
| 860 | PyDictKeyEntry *ep0 = &k->dk_entries[0]; |
| 861 | PyDictKeyEntry *ep; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 862 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 863 | assert(k->dk_lookup != NULL); |
| 864 | assert(value != NULL); |
| 865 | assert(key != NULL); |
| 866 | assert(key != dummy); |
| 867 | assert(PyUnicode_CheckExact(key) || k->dk_lookup == lookdict); |
| 868 | i = hash & mask; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 869 | ep = &ep0[i]; |
| 870 | for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) { |
| 871 | i = (i << 2) + i + perturb + 1; |
| 872 | ep = &ep0[i & mask]; |
| 873 | } |
| 874 | assert(ep->me_value == NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 875 | ep->me_key = key; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 876 | ep->me_hash = hash; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 877 | ep->me_value = value; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | /* |
| 881 | Restructure the table by allocating a new table and reinserting all |
| 882 | items again. When entries have been deleted, the new table may |
| 883 | actually be smaller than the old one. |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 884 | If a table is split (its keys and hashes are shared, its values are not), |
| 885 | then the values are temporarily copied into the table, it is resized as |
| 886 | a combined table, then the me_value slots in the old table are NULLed out. |
| 887 | After resizing a table is always combined, |
| 888 | but can be resplit by make_keys_shared(). |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 889 | */ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 890 | static int |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 891 | dictresize(PyDictObject *mp, Py_ssize_t minused) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 892 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 893 | Py_ssize_t newsize; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 894 | PyDictKeysObject *oldkeys; |
| 895 | PyObject **oldvalues; |
| 896 | Py_ssize_t i, oldsize; |
Tim Peters | 91a364d | 2001-05-19 07:04:38 +0000 | [diff] [blame] | 897 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 898 | /* Find the smallest table size > minused. */ |
| 899 | for (newsize = PyDict_MINSIZE_COMBINED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 900 | newsize <= minused && newsize > 0; |
| 901 | newsize <<= 1) |
| 902 | ; |
| 903 | if (newsize <= 0) { |
| 904 | PyErr_NoMemory(); |
| 905 | return -1; |
| 906 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 907 | oldkeys = mp->ma_keys; |
| 908 | oldvalues = mp->ma_values; |
| 909 | /* Allocate a new table. */ |
| 910 | mp->ma_keys = new_keys_object(newsize); |
| 911 | if (mp->ma_keys == NULL) { |
| 912 | mp->ma_keys = oldkeys; |
| 913 | return -1; |
| 914 | } |
| 915 | if (oldkeys->dk_lookup == lookdict) |
| 916 | mp->ma_keys->dk_lookup = lookdict; |
| 917 | oldsize = DK_SIZE(oldkeys); |
| 918 | mp->ma_values = NULL; |
| 919 | /* If empty then nothing to copy so just return */ |
| 920 | if (oldsize == 1) { |
| 921 | assert(oldkeys == Py_EMPTY_KEYS); |
| 922 | DK_DECREF(oldkeys); |
| 923 | return 0; |
| 924 | } |
| 925 | /* Main loop below assumes we can transfer refcount to new keys |
| 926 | * and that value is stored in me_value. |
| 927 | * Increment ref-counts and copy values here to compensate |
| 928 | * This (resizing a split table) should be relatively rare */ |
| 929 | if (oldvalues != NULL) { |
| 930 | for (i = 0; i < oldsize; i++) { |
| 931 | if (oldvalues[i] != NULL) { |
| 932 | Py_INCREF(oldkeys->dk_entries[i].me_key); |
| 933 | oldkeys->dk_entries[i].me_value = oldvalues[i]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 934 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 935 | } |
| 936 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 937 | /* Main loop */ |
| 938 | for (i = 0; i < oldsize; i++) { |
| 939 | PyDictKeyEntry *ep = &oldkeys->dk_entries[i]; |
| 940 | if (ep->me_value != NULL) { |
| 941 | assert(ep->me_key != dummy); |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 942 | insertdict_clean(mp, ep->me_key, ep->me_hash, ep->me_value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 943 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 944 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 945 | mp->ma_keys->dk_usable -= mp->ma_used; |
| 946 | if (oldvalues != NULL) { |
| 947 | /* NULL out me_value slot in oldkeys, in case it was shared */ |
| 948 | for (i = 0; i < oldsize; i++) |
| 949 | oldkeys->dk_entries[i].me_value = NULL; |
| 950 | assert(oldvalues != empty_values); |
| 951 | free_values(oldvalues); |
| 952 | DK_DECREF(oldkeys); |
| 953 | } |
| 954 | else { |
| 955 | assert(oldkeys->dk_lookup != lookdict_split); |
| 956 | if (oldkeys->dk_lookup != lookdict_unicode_nodummy) { |
| 957 | PyDictKeyEntry *ep0 = &oldkeys->dk_entries[0]; |
| 958 | for (i = 0; i < oldsize; i++) { |
| 959 | if (ep0[i].me_key == dummy) |
| 960 | Py_DECREF(dummy); |
| 961 | } |
| 962 | } |
| 963 | assert(oldkeys->dk_refcnt == 1); |
| 964 | PyMem_FREE(oldkeys); |
| 965 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 966 | return 0; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 967 | } |
| 968 | |
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 | |
| 976 | assert(PyDict_CheckExact(op)); |
| 977 | if (!_PyDict_HasSplitTable(mp)) { |
| 978 | PyDictKeyEntry *ep0; |
| 979 | PyObject **values; |
| 980 | assert(mp->ma_keys->dk_refcnt == 1); |
| 981 | if (mp->ma_keys->dk_lookup == lookdict) { |
| 982 | return NULL; |
| 983 | } |
| 984 | else if (mp->ma_keys->dk_lookup == lookdict_unicode) { |
| 985 | /* Remove dummy keys */ |
| 986 | if (dictresize(mp, DK_SIZE(mp->ma_keys))) |
| 987 | return NULL; |
| 988 | } |
| 989 | assert(mp->ma_keys->dk_lookup == lookdict_unicode_nodummy); |
| 990 | /* Copy values into a new array */ |
| 991 | ep0 = &mp->ma_keys->dk_entries[0]; |
| 992 | size = DK_SIZE(mp->ma_keys); |
| 993 | values = new_values(size); |
| 994 | if (values == NULL) { |
| 995 | PyErr_SetString(PyExc_MemoryError, |
| 996 | "Not enough memory to allocate new values array"); |
| 997 | return NULL; |
| 998 | } |
| 999 | for (i = 0; i < size; i++) { |
| 1000 | values[i] = ep0[i].me_value; |
| 1001 | ep0[i].me_value = NULL; |
| 1002 | } |
| 1003 | mp->ma_keys->dk_lookup = lookdict_split; |
| 1004 | mp->ma_values = values; |
| 1005 | } |
| 1006 | DK_INCREF(mp->ma_keys); |
| 1007 | return mp->ma_keys; |
| 1008 | } |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 1009 | |
| 1010 | PyObject * |
| 1011 | _PyDict_NewPresized(Py_ssize_t minused) |
| 1012 | { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1013 | Py_ssize_t newsize; |
| 1014 | PyDictKeysObject *new_keys; |
| 1015 | for (newsize = PyDict_MINSIZE_COMBINED; |
| 1016 | newsize <= minused && newsize > 0; |
| 1017 | newsize <<= 1) |
| 1018 | ; |
| 1019 | new_keys = new_keys_object(newsize); |
| 1020 | if (new_keys == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1021 | return NULL; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1022 | return new_dict(new_keys, NULL); |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 1023 | } |
| 1024 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1025 | /* Note that, for historical reasons, PyDict_GetItem() suppresses all errors |
| 1026 | * that may occur (originally dicts supported only string keys, and exceptions |
| 1027 | * 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] | 1028 | * 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] | 1029 | * (suppressed) occurred while computing the key's hash, or that some error |
| 1030 | * (suppressed) occurred when comparing keys in the dict's internal probe |
| 1031 | * sequence. A nasty example of the latter is when a Python-coded comparison |
| 1032 | * function hits a stack-depth error, which can cause this to return NULL |
| 1033 | * even if the key is present. |
| 1034 | */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1035 | PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 1036 | PyDict_GetItem(PyObject *op, PyObject *key) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1037 | { |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 1038 | Py_hash_t hash; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1039 | PyDictObject *mp = (PyDictObject *)op; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1040 | PyDictKeyEntry *ep; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1041 | PyThreadState *tstate; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1042 | PyObject **value_addr; |
| 1043 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1044 | if (!PyDict_Check(op)) |
| 1045 | return NULL; |
| 1046 | if (!PyUnicode_CheckExact(key) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1047 | (hash = ((PyASCIIObject *) key)->hash) == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1048 | { |
| 1049 | hash = PyObject_Hash(key); |
| 1050 | if (hash == -1) { |
| 1051 | PyErr_Clear(); |
| 1052 | return NULL; |
| 1053 | } |
| 1054 | } |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1055 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1056 | /* We can arrive here with a NULL tstate during initialization: try |
| 1057 | running "python -Wi" for an example related to string interning. |
| 1058 | Let's just hope that no exception occurs then... This must be |
| 1059 | _PyThreadState_Current and not PyThreadState_GET() because in debug |
| 1060 | mode, the latter complains if tstate is NULL. */ |
| 1061 | tstate = (PyThreadState*)_Py_atomic_load_relaxed( |
| 1062 | &_PyThreadState_Current); |
| 1063 | if (tstate != NULL && tstate->curexc_type != NULL) { |
| 1064 | /* preserve the existing exception */ |
| 1065 | PyObject *err_type, *err_value, *err_tb; |
| 1066 | PyErr_Fetch(&err_type, &err_value, &err_tb); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1067 | ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1068 | /* ignore errors */ |
| 1069 | PyErr_Restore(err_type, err_value, err_tb); |
| 1070 | if (ep == NULL) |
| 1071 | return NULL; |
| 1072 | } |
| 1073 | else { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1074 | ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1075 | if (ep == NULL) { |
| 1076 | PyErr_Clear(); |
| 1077 | return NULL; |
| 1078 | } |
| 1079 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1080 | return *value_addr; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 1083 | /* Variant of PyDict_GetItem() that doesn't suppress exceptions. |
| 1084 | This returns NULL *with* an exception set if an exception occurred. |
| 1085 | It returns NULL *without* an exception set if the key wasn't present. |
| 1086 | */ |
| 1087 | PyObject * |
| 1088 | PyDict_GetItemWithError(PyObject *op, PyObject *key) |
| 1089 | { |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 1090 | Py_hash_t hash; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1091 | PyDictObject*mp = (PyDictObject *)op; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1092 | PyDictKeyEntry *ep; |
| 1093 | PyObject **value_addr; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 1094 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1095 | if (!PyDict_Check(op)) { |
| 1096 | PyErr_BadInternalCall(); |
| 1097 | return NULL; |
| 1098 | } |
| 1099 | if (!PyUnicode_CheckExact(key) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1100 | (hash = ((PyASCIIObject *) key)->hash) == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1101 | { |
| 1102 | hash = PyObject_Hash(key); |
| 1103 | if (hash == -1) { |
| 1104 | return NULL; |
| 1105 | } |
| 1106 | } |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 1107 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1108 | ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1109 | if (ep == NULL) |
| 1110 | return NULL; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1111 | return *value_addr; |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
Brett Cannon | fd07415 | 2012-04-14 14:10:13 -0400 | [diff] [blame] | 1114 | PyObject * |
| 1115 | _PyDict_GetItemIdWithError(PyObject *dp, struct _Py_Identifier *key) |
| 1116 | { |
| 1117 | PyObject *kv; |
| 1118 | kv = _PyUnicode_FromId(key); /* borrowed */ |
| 1119 | if (kv == NULL) |
| 1120 | return NULL; |
| 1121 | return PyDict_GetItemWithError(dp, kv); |
| 1122 | } |
| 1123 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1124 | /* Fast version of global value lookup. |
| 1125 | * Lookup in globals, then builtins. |
| 1126 | */ |
| 1127 | PyObject * |
| 1128 | _PyDict_LoadGlobal(PyDictObject *globals, PyDictObject *builtins, PyObject *key) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1129 | { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1130 | PyObject *x; |
| 1131 | if (PyUnicode_CheckExact(key)) { |
| 1132 | PyObject **value_addr; |
| 1133 | Py_hash_t hash = ((PyASCIIObject *)key)->hash; |
| 1134 | if (hash != -1) { |
| 1135 | PyDictKeyEntry *e; |
| 1136 | e = globals->ma_keys->dk_lookup(globals, key, hash, &value_addr); |
| 1137 | if (e == NULL) { |
| 1138 | return NULL; |
| 1139 | } |
| 1140 | x = *value_addr; |
| 1141 | if (x != NULL) |
| 1142 | return x; |
| 1143 | e = builtins->ma_keys->dk_lookup(builtins, key, hash, &value_addr); |
| 1144 | if (e == NULL) { |
| 1145 | return NULL; |
| 1146 | } |
| 1147 | x = *value_addr; |
| 1148 | return x; |
| 1149 | } |
Antoine Pitrou | e965d97 | 2012-02-27 00:45:12 +0100 | [diff] [blame] | 1150 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1151 | x = PyDict_GetItemWithError((PyObject *)globals, key); |
| 1152 | if (x != NULL) |
| 1153 | return x; |
| 1154 | if (PyErr_Occurred()) |
| 1155 | return NULL; |
| 1156 | return PyDict_GetItemWithError((PyObject *)builtins, key); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
Antoine Pitrou | e965d97 | 2012-02-27 00:45:12 +0100 | [diff] [blame] | 1159 | /* CAUTION: PyDict_SetItem() must guarantee that it won't resize the |
| 1160 | * dictionary if it's merely replacing the value for an existing key. |
| 1161 | * This means that it's safe to loop over a dictionary with PyDict_Next() |
| 1162 | * and occasionally replace a value -- but you can't insert new keys or |
| 1163 | * remove them. |
| 1164 | */ |
| 1165 | int |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1166 | PyDict_SetItem(PyObject *op, PyObject *key, PyObject *value) |
Antoine Pitrou | e965d97 | 2012-02-27 00:45:12 +0100 | [diff] [blame] | 1167 | { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1168 | PyDictObject *mp; |
| 1169 | Py_hash_t hash; |
Antoine Pitrou | e965d97 | 2012-02-27 00:45:12 +0100 | [diff] [blame] | 1170 | if (!PyDict_Check(op)) { |
| 1171 | PyErr_BadInternalCall(); |
| 1172 | return -1; |
| 1173 | } |
| 1174 | assert(key); |
| 1175 | assert(value); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1176 | mp = (PyDictObject *)op; |
| 1177 | if (!PyUnicode_CheckExact(key) || |
| 1178 | (hash = ((PyASCIIObject *) key)->hash) == -1) |
| 1179 | { |
Antoine Pitrou | e965d97 | 2012-02-27 00:45:12 +0100 | [diff] [blame] | 1180 | hash = PyObject_Hash(key); |
| 1181 | if (hash == -1) |
| 1182 | return -1; |
| 1183 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1184 | Py_INCREF(value); |
| 1185 | Py_INCREF(key); |
| 1186 | |
| 1187 | /* insertdict() handles any resizing that might be necessary */ |
| 1188 | return insertdict(mp, key, hash, value); |
Antoine Pitrou | e965d97 | 2012-02-27 00:45:12 +0100 | [diff] [blame] | 1189 | } |
| 1190 | |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1191 | int |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 1192 | PyDict_DelItem(PyObject *op, PyObject *key) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1193 | { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1194 | PyDictObject *mp; |
| 1195 | Py_hash_t hash; |
| 1196 | PyDictKeyEntry *ep; |
| 1197 | PyObject *old_key, *old_value; |
| 1198 | PyObject **value_addr; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 1199 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1200 | if (!PyDict_Check(op)) { |
| 1201 | PyErr_BadInternalCall(); |
| 1202 | return -1; |
| 1203 | } |
| 1204 | assert(key); |
| 1205 | if (!PyUnicode_CheckExact(key) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1206 | (hash = ((PyASCIIObject *) key)->hash) == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1207 | hash = PyObject_Hash(key); |
| 1208 | if (hash == -1) |
| 1209 | return -1; |
| 1210 | } |
| 1211 | mp = (PyDictObject *)op; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1212 | ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1213 | if (ep == NULL) |
| 1214 | return -1; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1215 | if (*value_addr == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1216 | set_key_error(key); |
| 1217 | return -1; |
| 1218 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1219 | old_value = *value_addr; |
| 1220 | *value_addr = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1221 | mp->ma_used--; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1222 | if (!_PyDict_HasSplitTable(mp)) { |
| 1223 | ENSURE_ALLOWS_DELETIONS(mp); |
| 1224 | old_key = ep->me_key; |
| 1225 | Py_INCREF(dummy); |
| 1226 | ep->me_key = dummy; |
| 1227 | Py_DECREF(old_key); |
| 1228 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1229 | Py_DECREF(old_value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1230 | return 0; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1231 | } |
| 1232 | |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1233 | void |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 1234 | PyDict_Clear(PyObject *op) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1235 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1236 | PyDictObject *mp; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1237 | PyDictKeysObject *oldkeys; |
| 1238 | PyObject **oldvalues; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1239 | Py_ssize_t i, n; |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 1240 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1241 | if (!PyDict_Check(op)) |
| 1242 | return; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1243 | mp = ((PyDictObject *)op); |
| 1244 | oldkeys = mp->ma_keys; |
| 1245 | oldvalues = mp->ma_values; |
| 1246 | if (oldvalues == empty_values) |
| 1247 | return; |
| 1248 | /* Empty the dict... */ |
| 1249 | DK_INCREF(Py_EMPTY_KEYS); |
| 1250 | mp->ma_keys = Py_EMPTY_KEYS; |
| 1251 | mp->ma_values = empty_values; |
| 1252 | mp->ma_used = 0; |
| 1253 | /* ...then clear the keys and values */ |
| 1254 | if (oldvalues != NULL) { |
| 1255 | n = DK_SIZE(oldkeys); |
| 1256 | for (i = 0; i < n; i++) |
| 1257 | Py_CLEAR(oldvalues[i]); |
| 1258 | free_values(oldvalues); |
| 1259 | DK_DECREF(oldkeys); |
| 1260 | } |
| 1261 | else { |
| 1262 | assert(oldkeys->dk_refcnt == 1); |
| 1263 | free_keys_object(oldkeys); |
| 1264 | } |
| 1265 | } |
| 1266 | |
| 1267 | /* Returns -1 if no more items (or op is not a dict), |
| 1268 | * index of item otherwise. Stores value in pvalue |
| 1269 | */ |
| 1270 | Py_LOCAL_INLINE(Py_ssize_t) |
| 1271 | dict_next(PyObject *op, Py_ssize_t i, PyObject **pvalue) |
| 1272 | { |
| 1273 | Py_ssize_t mask, offset; |
| 1274 | PyDictObject *mp; |
| 1275 | PyObject **value_ptr; |
| 1276 | |
| 1277 | |
| 1278 | if (!PyDict_Check(op)) |
| 1279 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1280 | mp = (PyDictObject *)op; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1281 | if (i < 0) |
| 1282 | return -1; |
| 1283 | if (mp->ma_values) { |
| 1284 | value_ptr = &mp->ma_values[i]; |
| 1285 | offset = sizeof(PyObject *); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1286 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1287 | else { |
| 1288 | value_ptr = &mp->ma_keys->dk_entries[i].me_value; |
| 1289 | offset = sizeof(PyDictKeyEntry); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1290 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1291 | mask = DK_MASK(mp->ma_keys); |
| 1292 | while (i <= mask && *value_ptr == NULL) { |
| 1293 | value_ptr = (PyObject **)(((char *)value_ptr) + offset); |
| 1294 | i++; |
| 1295 | } |
| 1296 | if (i > mask) |
| 1297 | return -1; |
| 1298 | if (pvalue) |
| 1299 | *pvalue = *value_ptr; |
| 1300 | return i; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1301 | } |
| 1302 | |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 1303 | /* |
| 1304 | * Iterate over a dict. Use like so: |
| 1305 | * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1306 | * Py_ssize_t i; |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 1307 | * PyObject *key, *value; |
| 1308 | * i = 0; # important! i should not otherwise be changed by you |
Neal Norwitz | 0732301 | 2003-02-15 14:45:12 +0000 | [diff] [blame] | 1309 | * while (PyDict_Next(yourdict, &i, &key, &value)) { |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 1310 | * Refer to borrowed references in key and value. |
| 1311 | * } |
| 1312 | * |
| 1313 | * 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] | 1314 | * mutates the dict. One exception: it is safe if the loop merely changes |
| 1315 | * the values associated with the keys (but doesn't insert new keys or |
| 1316 | * delete keys), via PyDict_SetItem(). |
| 1317 | */ |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1318 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1319 | 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] | 1320 | { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1321 | PyDictObject *mp; |
| 1322 | Py_ssize_t i = dict_next(op, *ppos, pvalue); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1323 | if (i < 0) |
| 1324 | return 0; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1325 | mp = (PyDictObject *)op; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1326 | *ppos = i+1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1327 | if (pkey) |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1328 | *pkey = mp->ma_keys->dk_entries[i].me_key; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1329 | return 1; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1330 | } |
| 1331 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1332 | /* Internal version of PyDict_Next that returns a hash value in addition |
| 1333 | * to the key and value. |
| 1334 | */ |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1335 | int |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1336 | _PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, |
| 1337 | PyObject **pvalue, Py_hash_t *phash) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1338 | { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1339 | PyDictObject *mp; |
| 1340 | Py_ssize_t i = dict_next(op, *ppos, pvalue); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1341 | if (i < 0) |
| 1342 | return 0; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1343 | mp = (PyDictObject *)op; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1344 | *ppos = i+1; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1345 | *phash = mp->ma_keys->dk_entries[i].me_hash; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1346 | if (pkey) |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1347 | *pkey = mp->ma_keys->dk_entries[i].me_key; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1348 | return 1; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 1349 | } |
| 1350 | |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1351 | /* Methods */ |
| 1352 | |
| 1353 | static void |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1354 | dict_dealloc(PyDictObject *mp) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1355 | { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1356 | PyObject **values = mp->ma_values; |
| 1357 | PyDictKeysObject *keys = mp->ma_keys; |
| 1358 | Py_ssize_t i, n; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1359 | PyObject_GC_UnTrack(mp); |
| 1360 | Py_TRASHCAN_SAFE_BEGIN(mp) |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1361 | if (values != NULL) { |
| 1362 | if (values != empty_values) { |
| 1363 | for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) { |
| 1364 | Py_XDECREF(values[i]); |
| 1365 | } |
| 1366 | free_values(values); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1367 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1368 | DK_DECREF(keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1369 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1370 | else { |
| 1371 | free_keys_object(keys); |
| 1372 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1373 | if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type) |
| 1374 | free_list[numfree++] = mp; |
| 1375 | else |
| 1376 | Py_TYPE(mp)->tp_free((PyObject *)mp); |
| 1377 | Py_TRASHCAN_SAFE_END(mp) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1380 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1381 | static PyObject * |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1382 | dict_repr(PyDictObject *mp) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1383 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1384 | Py_ssize_t i; |
| 1385 | PyObject *s, *temp, *colon = NULL; |
| 1386 | PyObject *pieces = NULL, *result = NULL; |
| 1387 | PyObject *key, *value; |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 1388 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1389 | i = Py_ReprEnter((PyObject *)mp); |
| 1390 | if (i != 0) { |
| 1391 | return i > 0 ? PyUnicode_FromString("{...}") : NULL; |
| 1392 | } |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 1393 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1394 | if (mp->ma_used == 0) { |
| 1395 | result = PyUnicode_FromString("{}"); |
| 1396 | goto Done; |
| 1397 | } |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1398 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1399 | pieces = PyList_New(0); |
| 1400 | if (pieces == NULL) |
| 1401 | goto Done; |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1402 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1403 | colon = PyUnicode_FromString(": "); |
| 1404 | if (colon == NULL) |
| 1405 | goto Done; |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1406 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1407 | /* Do repr() on each key+value pair, and insert ": " between them. |
| 1408 | Note that repr may mutate the dict. */ |
| 1409 | i = 0; |
| 1410 | while (PyDict_Next((PyObject *)mp, &i, &key, &value)) { |
| 1411 | int status; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1412 | /* Prevent repr from deleting key or value during key format. */ |
| 1413 | Py_INCREF(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1414 | Py_INCREF(value); |
| 1415 | s = PyObject_Repr(key); |
| 1416 | PyUnicode_Append(&s, colon); |
| 1417 | PyUnicode_AppendAndDel(&s, PyObject_Repr(value)); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1418 | Py_DECREF(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1419 | Py_DECREF(value); |
| 1420 | if (s == NULL) |
| 1421 | goto Done; |
| 1422 | status = PyList_Append(pieces, s); |
| 1423 | Py_DECREF(s); /* append created a new ref */ |
| 1424 | if (status < 0) |
| 1425 | goto Done; |
| 1426 | } |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1427 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1428 | /* Add "{}" decorations to the first and last items. */ |
| 1429 | assert(PyList_GET_SIZE(pieces) > 0); |
| 1430 | s = PyUnicode_FromString("{"); |
| 1431 | if (s == NULL) |
| 1432 | goto Done; |
| 1433 | temp = PyList_GET_ITEM(pieces, 0); |
| 1434 | PyUnicode_AppendAndDel(&s, temp); |
| 1435 | PyList_SET_ITEM(pieces, 0, s); |
| 1436 | if (s == NULL) |
| 1437 | goto Done; |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1438 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1439 | s = PyUnicode_FromString("}"); |
| 1440 | if (s == NULL) |
| 1441 | goto Done; |
| 1442 | temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1); |
| 1443 | PyUnicode_AppendAndDel(&temp, s); |
| 1444 | PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp); |
| 1445 | if (temp == NULL) |
| 1446 | goto Done; |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1447 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1448 | /* Paste them all together with ", " between. */ |
| 1449 | s = PyUnicode_FromString(", "); |
| 1450 | if (s == NULL) |
| 1451 | goto Done; |
| 1452 | result = PyUnicode_Join(s, pieces); |
| 1453 | Py_DECREF(s); |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1454 | |
| 1455 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1456 | Py_XDECREF(pieces); |
| 1457 | Py_XDECREF(colon); |
| 1458 | Py_ReprLeave((PyObject *)mp); |
| 1459 | return result; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1460 | } |
| 1461 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1462 | static Py_ssize_t |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1463 | dict_length(PyDictObject *mp) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1464 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1465 | return mp->ma_used; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1468 | static PyObject * |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1469 | dict_subscript(PyDictObject *mp, register PyObject *key) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1470 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1471 | PyObject *v; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 1472 | Py_hash_t hash; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1473 | PyDictKeyEntry *ep; |
| 1474 | PyObject **value_addr; |
| 1475 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1476 | if (!PyUnicode_CheckExact(key) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1477 | (hash = ((PyASCIIObject *) key)->hash) == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1478 | hash = PyObject_Hash(key); |
| 1479 | if (hash == -1) |
| 1480 | return NULL; |
| 1481 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1482 | ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1483 | if (ep == NULL) |
| 1484 | return NULL; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1485 | v = *value_addr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1486 | if (v == NULL) { |
| 1487 | if (!PyDict_CheckExact(mp)) { |
| 1488 | /* Look up __missing__ method if we're a subclass. */ |
| 1489 | PyObject *missing, *res; |
Benjamin Peterson | ce79852 | 2012-01-22 11:24:29 -0500 | [diff] [blame] | 1490 | _Py_IDENTIFIER(__missing__); |
| 1491 | missing = _PyObject_LookupSpecial((PyObject *)mp, &PyId___missing__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1492 | if (missing != NULL) { |
| 1493 | res = PyObject_CallFunctionObjArgs(missing, |
| 1494 | key, NULL); |
| 1495 | Py_DECREF(missing); |
| 1496 | return res; |
| 1497 | } |
| 1498 | else if (PyErr_Occurred()) |
| 1499 | return NULL; |
| 1500 | } |
| 1501 | set_key_error(key); |
| 1502 | return NULL; |
| 1503 | } |
| 1504 | else |
| 1505 | Py_INCREF(v); |
| 1506 | return v; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1507 | } |
| 1508 | |
| 1509 | static int |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1510 | dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1511 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1512 | if (w == NULL) |
| 1513 | return PyDict_DelItem((PyObject *)mp, v); |
| 1514 | else |
| 1515 | return PyDict_SetItem((PyObject *)mp, v, w); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1516 | } |
| 1517 | |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 1518 | static PyMappingMethods dict_as_mapping = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1519 | (lenfunc)dict_length, /*mp_length*/ |
| 1520 | (binaryfunc)dict_subscript, /*mp_subscript*/ |
| 1521 | (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1522 | }; |
| 1523 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1524 | static PyObject * |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1525 | dict_keys(register PyDictObject *mp) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1526 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1527 | register PyObject *v; |
| 1528 | register Py_ssize_t i, j; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1529 | PyDictKeyEntry *ep; |
| 1530 | Py_ssize_t size, n, offset; |
| 1531 | PyObject **value_ptr; |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1532 | |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1533 | again: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1534 | n = mp->ma_used; |
| 1535 | v = PyList_New(n); |
| 1536 | if (v == NULL) |
| 1537 | return NULL; |
| 1538 | if (n != mp->ma_used) { |
| 1539 | /* Durnit. The allocations caused the dict to resize. |
| 1540 | * Just start over, this shouldn't normally happen. |
| 1541 | */ |
| 1542 | Py_DECREF(v); |
| 1543 | goto again; |
| 1544 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1545 | ep = &mp->ma_keys->dk_entries[0]; |
| 1546 | size = DK_SIZE(mp->ma_keys); |
| 1547 | if (mp->ma_values) { |
| 1548 | value_ptr = mp->ma_values; |
| 1549 | offset = sizeof(PyObject *); |
| 1550 | } |
| 1551 | else { |
| 1552 | value_ptr = &ep[0].me_value; |
| 1553 | offset = sizeof(PyDictKeyEntry); |
| 1554 | } |
| 1555 | for (i = 0, j = 0; i < size; i++) { |
| 1556 | if (*value_ptr != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1557 | PyObject *key = ep[i].me_key; |
| 1558 | Py_INCREF(key); |
| 1559 | PyList_SET_ITEM(v, j, key); |
| 1560 | j++; |
| 1561 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1562 | value_ptr = (PyObject **)(((char *)value_ptr) + offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1563 | } |
| 1564 | assert(j == n); |
| 1565 | return v; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1566 | } |
| 1567 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1568 | static PyObject * |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1569 | dict_values(register PyDictObject *mp) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1570 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1571 | register PyObject *v; |
| 1572 | register Py_ssize_t i, j; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1573 | Py_ssize_t size, n, offset; |
| 1574 | PyObject **value_ptr; |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1575 | |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1576 | again: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1577 | n = mp->ma_used; |
| 1578 | v = PyList_New(n); |
| 1579 | if (v == NULL) |
| 1580 | return NULL; |
| 1581 | if (n != mp->ma_used) { |
| 1582 | /* Durnit. The allocations caused the dict to resize. |
| 1583 | * Just start over, this shouldn't normally happen. |
| 1584 | */ |
| 1585 | Py_DECREF(v); |
| 1586 | goto again; |
| 1587 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1588 | size = DK_SIZE(mp->ma_keys); |
| 1589 | if (mp->ma_values) { |
| 1590 | value_ptr = mp->ma_values; |
| 1591 | offset = sizeof(PyObject *); |
| 1592 | } |
| 1593 | else { |
| 1594 | value_ptr = &mp->ma_keys->dk_entries[0].me_value; |
| 1595 | offset = sizeof(PyDictKeyEntry); |
| 1596 | } |
| 1597 | for (i = 0, j = 0; i < size; i++) { |
| 1598 | PyObject *value = *value_ptr; |
| 1599 | value_ptr = (PyObject **)(((char *)value_ptr) + offset); |
| 1600 | if (value != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1601 | Py_INCREF(value); |
| 1602 | PyList_SET_ITEM(v, j, value); |
| 1603 | j++; |
| 1604 | } |
| 1605 | } |
| 1606 | assert(j == n); |
| 1607 | return v; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1608 | } |
| 1609 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1610 | static PyObject * |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1611 | dict_items(register PyDictObject *mp) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1612 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1613 | register PyObject *v; |
| 1614 | register Py_ssize_t i, j, n; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1615 | Py_ssize_t size, offset; |
| 1616 | PyObject *item, *key; |
| 1617 | PyDictKeyEntry *ep; |
| 1618 | PyObject **value_ptr; |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1619 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1620 | /* Preallocate the list of tuples, to avoid allocations during |
| 1621 | * the loop over the items, which could trigger GC, which |
| 1622 | * could resize the dict. :-( |
| 1623 | */ |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1624 | again: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1625 | n = mp->ma_used; |
| 1626 | v = PyList_New(n); |
| 1627 | if (v == NULL) |
| 1628 | return NULL; |
| 1629 | for (i = 0; i < n; i++) { |
| 1630 | item = PyTuple_New(2); |
| 1631 | if (item == NULL) { |
| 1632 | Py_DECREF(v); |
| 1633 | return NULL; |
| 1634 | } |
| 1635 | PyList_SET_ITEM(v, i, item); |
| 1636 | } |
| 1637 | if (n != mp->ma_used) { |
| 1638 | /* Durnit. The allocations caused the dict to resize. |
| 1639 | * Just start over, this shouldn't normally happen. |
| 1640 | */ |
| 1641 | Py_DECREF(v); |
| 1642 | goto again; |
| 1643 | } |
| 1644 | /* Nothing we do below makes any function calls. */ |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1645 | ep = mp->ma_keys->dk_entries; |
| 1646 | size = DK_SIZE(mp->ma_keys); |
| 1647 | if (mp->ma_values) { |
| 1648 | value_ptr = mp->ma_values; |
| 1649 | offset = sizeof(PyObject *); |
| 1650 | } |
| 1651 | else { |
| 1652 | value_ptr = &ep[0].me_value; |
| 1653 | offset = sizeof(PyDictKeyEntry); |
| 1654 | } |
| 1655 | for (i = 0, j = 0; i < size; i++) { |
| 1656 | PyObject *value = *value_ptr; |
| 1657 | value_ptr = (PyObject **)(((char *)value_ptr) + offset); |
| 1658 | if (value != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1659 | key = ep[i].me_key; |
| 1660 | item = PyList_GET_ITEM(v, j); |
| 1661 | Py_INCREF(key); |
| 1662 | PyTuple_SET_ITEM(item, 0, key); |
| 1663 | Py_INCREF(value); |
| 1664 | PyTuple_SET_ITEM(item, 1, value); |
| 1665 | j++; |
| 1666 | } |
| 1667 | } |
| 1668 | assert(j == n); |
| 1669 | return v; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1670 | } |
| 1671 | |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 1672 | static PyObject * |
Tim Peters | bca1cbc | 2002-12-09 22:56:13 +0000 | [diff] [blame] | 1673 | dict_fromkeys(PyObject *cls, PyObject *args) |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 1674 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1675 | PyObject *seq; |
| 1676 | PyObject *value = Py_None; |
| 1677 | PyObject *it; /* iter(seq) */ |
| 1678 | PyObject *key; |
| 1679 | PyObject *d; |
| 1680 | int status; |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 1681 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1682 | if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value)) |
| 1683 | return NULL; |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 1684 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1685 | d = PyObject_CallObject(cls, NULL); |
| 1686 | if (d == NULL) |
| 1687 | return NULL; |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 1688 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1689 | if (PyDict_CheckExact(d) && PyDict_CheckExact(seq)) { |
| 1690 | PyDictObject *mp = (PyDictObject *)d; |
| 1691 | PyObject *oldvalue; |
| 1692 | Py_ssize_t pos = 0; |
| 1693 | PyObject *key; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 1694 | Py_hash_t hash; |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 1695 | |
Petri Lehtinen | a94200e | 2011-10-24 21:12:58 +0300 | [diff] [blame] | 1696 | if (dictresize(mp, Py_SIZE(seq))) { |
| 1697 | Py_DECREF(d); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1698 | return NULL; |
Petri Lehtinen | a94200e | 2011-10-24 21:12:58 +0300 | [diff] [blame] | 1699 | } |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 1700 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1701 | while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) { |
| 1702 | Py_INCREF(key); |
| 1703 | Py_INCREF(value); |
Petri Lehtinen | a94200e | 2011-10-24 21:12:58 +0300 | [diff] [blame] | 1704 | if (insertdict(mp, key, hash, value)) { |
| 1705 | Py_DECREF(d); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1706 | return NULL; |
Petri Lehtinen | a94200e | 2011-10-24 21:12:58 +0300 | [diff] [blame] | 1707 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1708 | } |
| 1709 | return d; |
| 1710 | } |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 1711 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1712 | if (PyDict_CheckExact(d) && PyAnySet_CheckExact(seq)) { |
| 1713 | PyDictObject *mp = (PyDictObject *)d; |
| 1714 | Py_ssize_t pos = 0; |
| 1715 | PyObject *key; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 1716 | Py_hash_t hash; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1717 | |
Petri Lehtinen | a94200e | 2011-10-24 21:12:58 +0300 | [diff] [blame] | 1718 | if (dictresize(mp, PySet_GET_SIZE(seq))) { |
| 1719 | Py_DECREF(d); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1720 | return NULL; |
Petri Lehtinen | a94200e | 2011-10-24 21:12:58 +0300 | [diff] [blame] | 1721 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1722 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1723 | while (_PySet_NextEntry(seq, &pos, &key, &hash)) { |
| 1724 | Py_INCREF(key); |
| 1725 | Py_INCREF(value); |
Petri Lehtinen | a94200e | 2011-10-24 21:12:58 +0300 | [diff] [blame] | 1726 | if (insertdict(mp, key, hash, value)) { |
| 1727 | Py_DECREF(d); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1728 | return NULL; |
Petri Lehtinen | a94200e | 2011-10-24 21:12:58 +0300 | [diff] [blame] | 1729 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1730 | } |
| 1731 | return d; |
| 1732 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1733 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1734 | it = PyObject_GetIter(seq); |
| 1735 | if (it == NULL){ |
| 1736 | Py_DECREF(d); |
| 1737 | return NULL; |
| 1738 | } |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 1739 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1740 | if (PyDict_CheckExact(d)) { |
| 1741 | while ((key = PyIter_Next(it)) != NULL) { |
| 1742 | status = PyDict_SetItem(d, key, value); |
| 1743 | Py_DECREF(key); |
| 1744 | if (status < 0) |
| 1745 | goto Fail; |
| 1746 | } |
| 1747 | } else { |
| 1748 | while ((key = PyIter_Next(it)) != NULL) { |
| 1749 | status = PyObject_SetItem(d, key, value); |
| 1750 | Py_DECREF(key); |
| 1751 | if (status < 0) |
| 1752 | goto Fail; |
| 1753 | } |
| 1754 | } |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 1755 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1756 | if (PyErr_Occurred()) |
| 1757 | goto Fail; |
| 1758 | Py_DECREF(it); |
| 1759 | return d; |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 1760 | |
| 1761 | Fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1762 | Py_DECREF(it); |
| 1763 | Py_DECREF(d); |
| 1764 | return NULL; |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 1765 | } |
| 1766 | |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 1767 | static int |
| 1768 | dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname) |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 1769 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1770 | PyObject *arg = NULL; |
| 1771 | int result = 0; |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 1772 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1773 | if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg)) |
| 1774 | result = -1; |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 1775 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1776 | else if (arg != NULL) { |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 1777 | _Py_IDENTIFIER(keys); |
| 1778 | if (_PyObject_HasAttrId(arg, &PyId_keys)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1779 | result = PyDict_Merge(self, arg, 1); |
| 1780 | else |
| 1781 | result = PyDict_MergeFromSeq2(self, arg, 1); |
| 1782 | } |
| 1783 | if (result == 0 && kwds != NULL) { |
| 1784 | if (PyArg_ValidateKeywordArguments(kwds)) |
| 1785 | result = PyDict_Merge(self, kwds, 1); |
| 1786 | else |
| 1787 | result = -1; |
| 1788 | } |
| 1789 | return result; |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 1790 | } |
| 1791 | |
| 1792 | static PyObject * |
| 1793 | dict_update(PyObject *self, PyObject *args, PyObject *kwds) |
| 1794 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1795 | if (dict_update_common(self, args, kwds, "update") != -1) |
| 1796 | Py_RETURN_NONE; |
| 1797 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1798 | } |
| 1799 | |
Guido van Rossum | 05ac6de | 2001-08-10 20:28:28 +0000 | [diff] [blame] | 1800 | /* Update unconditionally replaces existing items. |
| 1801 | Merge has a 3rd argument 'override'; if set, it acts like Update, |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1802 | otherwise it leaves existing items unchanged. |
| 1803 | |
| 1804 | PyDict_{Update,Merge} update/merge from a mapping object. |
| 1805 | |
Tim Peters | f582b82 | 2001-12-11 18:51:08 +0000 | [diff] [blame] | 1806 | PyDict_MergeFromSeq2 updates/merges from any iterable object |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1807 | producing iterable objects of length 2. |
| 1808 | */ |
| 1809 | |
Tim Peters | f582b82 | 2001-12-11 18:51:08 +0000 | [diff] [blame] | 1810 | int |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1811 | PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override) |
| 1812 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1813 | PyObject *it; /* iter(seq2) */ |
| 1814 | Py_ssize_t i; /* index into seq2 of current element */ |
| 1815 | PyObject *item; /* seq2[i] */ |
| 1816 | PyObject *fast; /* item as a 2-tuple or 2-list */ |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1817 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1818 | assert(d != NULL); |
| 1819 | assert(PyDict_Check(d)); |
| 1820 | assert(seq2 != NULL); |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1821 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1822 | it = PyObject_GetIter(seq2); |
| 1823 | if (it == NULL) |
| 1824 | return -1; |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1825 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1826 | for (i = 0; ; ++i) { |
| 1827 | PyObject *key, *value; |
| 1828 | Py_ssize_t n; |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1829 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1830 | fast = NULL; |
| 1831 | item = PyIter_Next(it); |
| 1832 | if (item == NULL) { |
| 1833 | if (PyErr_Occurred()) |
| 1834 | goto Fail; |
| 1835 | break; |
| 1836 | } |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1837 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1838 | /* Convert item to sequence, and verify length 2. */ |
| 1839 | fast = PySequence_Fast(item, ""); |
| 1840 | if (fast == NULL) { |
| 1841 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 1842 | PyErr_Format(PyExc_TypeError, |
| 1843 | "cannot convert dictionary update " |
| 1844 | "sequence element #%zd to a sequence", |
| 1845 | i); |
| 1846 | goto Fail; |
| 1847 | } |
| 1848 | n = PySequence_Fast_GET_SIZE(fast); |
| 1849 | if (n != 2) { |
| 1850 | PyErr_Format(PyExc_ValueError, |
| 1851 | "dictionary update sequence element #%zd " |
| 1852 | "has length %zd; 2 is required", |
| 1853 | i, n); |
| 1854 | goto Fail; |
| 1855 | } |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1856 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1857 | /* Update/merge with this (key, value) pair. */ |
| 1858 | key = PySequence_Fast_GET_ITEM(fast, 0); |
| 1859 | value = PySequence_Fast_GET_ITEM(fast, 1); |
| 1860 | if (override || PyDict_GetItem(d, key) == NULL) { |
| 1861 | int status = PyDict_SetItem(d, key, value); |
| 1862 | if (status < 0) |
| 1863 | goto Fail; |
| 1864 | } |
| 1865 | Py_DECREF(fast); |
| 1866 | Py_DECREF(item); |
| 1867 | } |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1868 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1869 | i = 0; |
| 1870 | goto Return; |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1871 | Fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1872 | Py_XDECREF(item); |
| 1873 | Py_XDECREF(fast); |
| 1874 | i = -1; |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1875 | Return: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1876 | Py_DECREF(it); |
| 1877 | return Py_SAFE_DOWNCAST(i, Py_ssize_t, int); |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1878 | } |
| 1879 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1880 | int |
| 1881 | PyDict_Update(PyObject *a, PyObject *b) |
| 1882 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1883 | return PyDict_Merge(a, b, 1); |
Guido van Rossum | 05ac6de | 2001-08-10 20:28:28 +0000 | [diff] [blame] | 1884 | } |
| 1885 | |
| 1886 | int |
| 1887 | PyDict_Merge(PyObject *a, PyObject *b, int override) |
| 1888 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1889 | register PyDictObject *mp, *other; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1890 | register Py_ssize_t i, n; |
| 1891 | PyDictKeyEntry *entry; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1892 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1893 | /* We accept for the argument either a concrete dictionary object, |
| 1894 | * or an abstract "mapping" object. For the former, we can do |
| 1895 | * things quite efficiently. For the latter, we only require that |
| 1896 | * PyMapping_Keys() and PyObject_GetItem() be supported. |
| 1897 | */ |
| 1898 | if (a == NULL || !PyDict_Check(a) || b == NULL) { |
| 1899 | PyErr_BadInternalCall(); |
| 1900 | return -1; |
| 1901 | } |
| 1902 | mp = (PyDictObject*)a; |
| 1903 | if (PyDict_Check(b)) { |
| 1904 | other = (PyDictObject*)b; |
| 1905 | if (other == mp || other->ma_used == 0) |
| 1906 | /* a.update(a) or a.update({}); nothing to do */ |
| 1907 | return 0; |
| 1908 | if (mp->ma_used == 0) |
| 1909 | /* Since the target dict is empty, PyDict_GetItem() |
| 1910 | * always returns NULL. Setting override to 1 |
| 1911 | * skips the unnecessary test. |
| 1912 | */ |
| 1913 | override = 1; |
| 1914 | /* Do one big resize at the start, rather than |
| 1915 | * incrementally resizing as we insert new items. Expect |
| 1916 | * that there will be no (or few) overlapping keys. |
| 1917 | */ |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1918 | if (mp->ma_keys->dk_usable * 3 < other->ma_used * 2) |
| 1919 | if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1920 | return -1; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1921 | for (i = 0, n = DK_SIZE(other->ma_keys); i < n; i++) { |
| 1922 | PyObject *value; |
| 1923 | entry = &other->ma_keys->dk_entries[i]; |
| 1924 | if (other->ma_values) |
| 1925 | value = other->ma_values[i]; |
| 1926 | else |
| 1927 | value = entry->me_value; |
| 1928 | |
| 1929 | if (value != NULL && |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1930 | (override || |
| 1931 | PyDict_GetItem(a, entry->me_key) == NULL)) { |
| 1932 | Py_INCREF(entry->me_key); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1933 | Py_INCREF(value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1934 | if (insertdict(mp, entry->me_key, |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 1935 | entry->me_hash, |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1936 | value) != 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1937 | return -1; |
| 1938 | } |
| 1939 | } |
| 1940 | } |
| 1941 | else { |
| 1942 | /* Do it the generic, slower way */ |
| 1943 | PyObject *keys = PyMapping_Keys(b); |
| 1944 | PyObject *iter; |
| 1945 | PyObject *key, *value; |
| 1946 | int status; |
Barry Warsaw | 66a0d1d | 2001-06-26 20:08:32 +0000 | [diff] [blame] | 1947 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1948 | if (keys == NULL) |
| 1949 | /* Docstring says this is equivalent to E.keys() so |
| 1950 | * if E doesn't have a .keys() method we want |
| 1951 | * AttributeError to percolate up. Might as well |
| 1952 | * do the same for any other error. |
| 1953 | */ |
| 1954 | return -1; |
Barry Warsaw | 66a0d1d | 2001-06-26 20:08:32 +0000 | [diff] [blame] | 1955 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1956 | iter = PyObject_GetIter(keys); |
| 1957 | Py_DECREF(keys); |
| 1958 | if (iter == NULL) |
| 1959 | return -1; |
Barry Warsaw | 66a0d1d | 2001-06-26 20:08:32 +0000 | [diff] [blame] | 1960 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1961 | for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) { |
| 1962 | if (!override && PyDict_GetItem(a, key) != NULL) { |
| 1963 | Py_DECREF(key); |
| 1964 | continue; |
| 1965 | } |
| 1966 | value = PyObject_GetItem(b, key); |
| 1967 | if (value == NULL) { |
| 1968 | Py_DECREF(iter); |
| 1969 | Py_DECREF(key); |
| 1970 | return -1; |
| 1971 | } |
| 1972 | status = PyDict_SetItem(a, key, value); |
| 1973 | Py_DECREF(key); |
| 1974 | Py_DECREF(value); |
| 1975 | if (status < 0) { |
| 1976 | Py_DECREF(iter); |
| 1977 | return -1; |
| 1978 | } |
| 1979 | } |
| 1980 | Py_DECREF(iter); |
| 1981 | if (PyErr_Occurred()) |
| 1982 | /* Iterator completed, via error */ |
| 1983 | return -1; |
| 1984 | } |
| 1985 | return 0; |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 1986 | } |
| 1987 | |
| 1988 | static PyObject * |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 1989 | dict_copy(register PyDictObject *mp) |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 1990 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1991 | return PyDict_Copy((PyObject*)mp); |
Jeremy Hylton | a12c7a7 | 2000-03-30 22:27:31 +0000 | [diff] [blame] | 1992 | } |
| 1993 | |
| 1994 | PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 1995 | PyDict_Copy(PyObject *o) |
Jeremy Hylton | a12c7a7 | 2000-03-30 22:27:31 +0000 | [diff] [blame] | 1996 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1997 | PyObject *copy; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1998 | PyDictObject *mp; |
| 1999 | Py_ssize_t i, n; |
Jeremy Hylton | a12c7a7 | 2000-03-30 22:27:31 +0000 | [diff] [blame] | 2000 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2001 | if (o == NULL || !PyDict_Check(o)) { |
| 2002 | PyErr_BadInternalCall(); |
| 2003 | return NULL; |
| 2004 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2005 | mp = (PyDictObject *)o; |
| 2006 | if (_PyDict_HasSplitTable(mp)) { |
| 2007 | PyDictObject *split_copy; |
| 2008 | PyObject **newvalues = new_values(DK_SIZE(mp->ma_keys)); |
| 2009 | if (newvalues == NULL) |
| 2010 | return PyErr_NoMemory(); |
| 2011 | split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type); |
| 2012 | if (split_copy == NULL) { |
| 2013 | free_values(newvalues); |
| 2014 | return NULL; |
| 2015 | } |
| 2016 | split_copy->ma_values = newvalues; |
| 2017 | split_copy->ma_keys = mp->ma_keys; |
| 2018 | split_copy->ma_used = mp->ma_used; |
| 2019 | DK_INCREF(mp->ma_keys); |
| 2020 | for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) { |
| 2021 | PyObject *value = mp->ma_values[i]; |
| 2022 | Py_XINCREF(value); |
| 2023 | split_copy->ma_values[i] = value; |
| 2024 | } |
Benjamin Peterson | 7ce67e4 | 2012-04-24 10:32:57 -0400 | [diff] [blame^] | 2025 | if (_PyObject_GC_IS_TRACKED(mp)) |
| 2026 | _PyObject_GC_TRACK(split_copy); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2027 | return (PyObject *)split_copy; |
| 2028 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2029 | copy = PyDict_New(); |
| 2030 | if (copy == NULL) |
| 2031 | return NULL; |
| 2032 | if (PyDict_Merge(copy, o, 1) == 0) |
| 2033 | return copy; |
| 2034 | Py_DECREF(copy); |
| 2035 | return NULL; |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 2036 | } |
| 2037 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2038 | Py_ssize_t |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 2039 | PyDict_Size(PyObject *mp) |
Guido van Rossum | 4199fac | 1993-11-05 10:18:44 +0000 | [diff] [blame] | 2040 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2041 | if (mp == NULL || !PyDict_Check(mp)) { |
| 2042 | PyErr_BadInternalCall(); |
| 2043 | return -1; |
| 2044 | } |
| 2045 | return ((PyDictObject *)mp)->ma_used; |
Guido van Rossum | 4199fac | 1993-11-05 10:18:44 +0000 | [diff] [blame] | 2046 | } |
| 2047 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2048 | PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 2049 | PyDict_Keys(PyObject *mp) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2050 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2051 | if (mp == NULL || !PyDict_Check(mp)) { |
| 2052 | PyErr_BadInternalCall(); |
| 2053 | return NULL; |
| 2054 | } |
| 2055 | return dict_keys((PyDictObject *)mp); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2056 | } |
| 2057 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2058 | PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 2059 | PyDict_Values(PyObject *mp) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2060 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2061 | if (mp == NULL || !PyDict_Check(mp)) { |
| 2062 | PyErr_BadInternalCall(); |
| 2063 | return NULL; |
| 2064 | } |
| 2065 | return dict_values((PyDictObject *)mp); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2066 | } |
| 2067 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2068 | PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 2069 | PyDict_Items(PyObject *mp) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2070 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2071 | if (mp == NULL || !PyDict_Check(mp)) { |
| 2072 | PyErr_BadInternalCall(); |
| 2073 | return NULL; |
| 2074 | } |
| 2075 | return dict_items((PyDictObject *)mp); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2076 | } |
| 2077 | |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 2078 | /* Return 1 if dicts equal, 0 if not, -1 if error. |
| 2079 | * Gets out as soon as any difference is detected. |
| 2080 | * Uses only Py_EQ comparison. |
| 2081 | */ |
| 2082 | static int |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2083 | dict_equal(PyDictObject *a, PyDictObject *b) |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 2084 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2085 | Py_ssize_t i; |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 2086 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2087 | if (a->ma_used != b->ma_used) |
| 2088 | /* can't be equal if # of entries differ */ |
| 2089 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2090 | /* Same # of entries -- check all of 'em. Exit early on any diff. */ |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2091 | for (i = 0; i < DK_SIZE(a->ma_keys); i++) { |
| 2092 | PyDictKeyEntry *ep = &a->ma_keys->dk_entries[i]; |
| 2093 | PyObject *aval; |
| 2094 | if (a->ma_values) |
| 2095 | aval = a->ma_values[i]; |
| 2096 | else |
| 2097 | aval = ep->me_value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2098 | if (aval != NULL) { |
| 2099 | int cmp; |
| 2100 | PyObject *bval; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2101 | PyObject *key = ep->me_key; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2102 | /* temporarily bump aval's refcount to ensure it stays |
| 2103 | alive until we're done with it */ |
| 2104 | Py_INCREF(aval); |
| 2105 | /* ditto for key */ |
| 2106 | Py_INCREF(key); |
| 2107 | bval = PyDict_GetItemWithError((PyObject *)b, key); |
| 2108 | Py_DECREF(key); |
| 2109 | if (bval == NULL) { |
| 2110 | Py_DECREF(aval); |
| 2111 | if (PyErr_Occurred()) |
| 2112 | return -1; |
| 2113 | return 0; |
| 2114 | } |
| 2115 | cmp = PyObject_RichCompareBool(aval, bval, Py_EQ); |
| 2116 | Py_DECREF(aval); |
| 2117 | if (cmp <= 0) /* error or not equal */ |
| 2118 | return cmp; |
| 2119 | } |
| 2120 | } |
| 2121 | return 1; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2122 | } |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 2123 | |
| 2124 | static PyObject * |
| 2125 | dict_richcompare(PyObject *v, PyObject *w, int op) |
| 2126 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2127 | int cmp; |
| 2128 | PyObject *res; |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 2129 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2130 | if (!PyDict_Check(v) || !PyDict_Check(w)) { |
| 2131 | res = Py_NotImplemented; |
| 2132 | } |
| 2133 | else if (op == Py_EQ || op == Py_NE) { |
| 2134 | cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w); |
| 2135 | if (cmp < 0) |
| 2136 | return NULL; |
| 2137 | res = (cmp == (op == Py_EQ)) ? Py_True : Py_False; |
| 2138 | } |
| 2139 | else |
| 2140 | res = Py_NotImplemented; |
| 2141 | Py_INCREF(res); |
| 2142 | return res; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2143 | } |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 2144 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2145 | static PyObject * |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2146 | dict_contains(register PyDictObject *mp, PyObject *key) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2147 | { |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2148 | Py_hash_t hash; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2149 | PyDictKeyEntry *ep; |
| 2150 | PyObject **value_addr; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2151 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2152 | if (!PyUnicode_CheckExact(key) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2153 | (hash = ((PyASCIIObject *) key)->hash) == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2154 | hash = PyObject_Hash(key); |
| 2155 | if (hash == -1) |
| 2156 | return NULL; |
| 2157 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2158 | ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2159 | if (ep == NULL) |
| 2160 | return NULL; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2161 | return PyBool_FromLong(*value_addr != NULL); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2162 | } |
| 2163 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2164 | static PyObject * |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2165 | dict_get(register PyDictObject *mp, PyObject *args) |
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 | PyObject *key; |
| 2168 | PyObject *failobj = Py_None; |
| 2169 | PyObject *val = NULL; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2170 | Py_hash_t hash; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2171 | PyDictKeyEntry *ep; |
| 2172 | PyObject **value_addr; |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 2173 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2174 | if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj)) |
| 2175 | return NULL; |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 2176 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2177 | if (!PyUnicode_CheckExact(key) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2178 | (hash = ((PyASCIIObject *) key)->hash) == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2179 | hash = PyObject_Hash(key); |
| 2180 | if (hash == -1) |
| 2181 | return NULL; |
| 2182 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2183 | ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2184 | if (ep == NULL) |
| 2185 | return NULL; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2186 | val = *value_addr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2187 | if (val == NULL) |
| 2188 | val = failobj; |
| 2189 | Py_INCREF(val); |
| 2190 | return val; |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 2191 | } |
| 2192 | |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 2193 | static PyObject * |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2194 | dict_setdefault(register PyDictObject *mp, PyObject *args) |
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 | PyObject *key; |
| 2197 | PyObject *failobj = Py_None; |
| 2198 | PyObject *val = NULL; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2199 | Py_hash_t hash; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2200 | PyDictKeyEntry *ep; |
| 2201 | PyObject **value_addr; |
Guido van Rossum | 164452c | 2000-08-08 16:12:54 +0000 | [diff] [blame] | 2202 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2203 | if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj)) |
| 2204 | return NULL; |
Guido van Rossum | 164452c | 2000-08-08 16:12:54 +0000 | [diff] [blame] | 2205 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2206 | if (!PyUnicode_CheckExact(key) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2207 | (hash = ((PyASCIIObject *) key)->hash) == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2208 | hash = PyObject_Hash(key); |
| 2209 | if (hash == -1) |
| 2210 | return NULL; |
| 2211 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2212 | ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2213 | if (ep == NULL) |
| 2214 | return NULL; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2215 | val = *value_addr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2216 | if (val == NULL) { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2217 | Py_INCREF(failobj); |
| 2218 | Py_INCREF(key); |
| 2219 | if (mp->ma_keys->dk_usable <= 0) { |
| 2220 | /* Need to resize. */ |
| 2221 | if (insertion_resize(mp) < 0) |
| 2222 | return NULL; |
| 2223 | ep = find_empty_slot(mp, key, hash, &value_addr); |
| 2224 | } |
Benjamin Peterson | 7ce67e4 | 2012-04-24 10:32:57 -0400 | [diff] [blame^] | 2225 | MAINTAIN_TRACKING(mp, key, failobj); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2226 | ep->me_key = key; |
| 2227 | ep->me_hash = hash; |
| 2228 | *value_addr = failobj; |
| 2229 | val = failobj; |
| 2230 | mp->ma_keys->dk_usable--; |
| 2231 | mp->ma_used++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2232 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2233 | Py_INCREF(val); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2234 | return val; |
Guido van Rossum | 164452c | 2000-08-08 16:12:54 +0000 | [diff] [blame] | 2235 | } |
| 2236 | |
| 2237 | |
| 2238 | static PyObject * |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2239 | dict_clear(register PyDictObject *mp) |
Guido van Rossum | fb8f1ca | 1997-03-21 21:55:12 +0000 | [diff] [blame] | 2240 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2241 | PyDict_Clear((PyObject *)mp); |
| 2242 | Py_RETURN_NONE; |
Guido van Rossum | fb8f1ca | 1997-03-21 21:55:12 +0000 | [diff] [blame] | 2243 | } |
| 2244 | |
Guido van Rossum | ba6ab84 | 2000-12-12 22:02:18 +0000 | [diff] [blame] | 2245 | static PyObject * |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2246 | dict_pop(PyDictObject *mp, PyObject *args) |
Guido van Rossum | e027d98 | 2002-04-12 15:11:59 +0000 | [diff] [blame] | 2247 | { |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2248 | Py_hash_t hash; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2249 | PyObject *old_value, *old_key; |
| 2250 | PyObject *key, *deflt = NULL; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2251 | PyDictKeyEntry *ep; |
| 2252 | PyObject **value_addr; |
Guido van Rossum | e027d98 | 2002-04-12 15:11:59 +0000 | [diff] [blame] | 2253 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2254 | if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt)) |
| 2255 | return NULL; |
| 2256 | if (mp->ma_used == 0) { |
| 2257 | if (deflt) { |
| 2258 | Py_INCREF(deflt); |
| 2259 | return deflt; |
| 2260 | } |
Raymond Hettinger | dd42154 | 2010-10-30 08:10:29 +0000 | [diff] [blame] | 2261 | set_key_error(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2262 | return NULL; |
| 2263 | } |
| 2264 | if (!PyUnicode_CheckExact(key) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2265 | (hash = ((PyASCIIObject *) key)->hash) == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2266 | hash = PyObject_Hash(key); |
| 2267 | if (hash == -1) |
| 2268 | return NULL; |
| 2269 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2270 | ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2271 | if (ep == NULL) |
| 2272 | return NULL; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2273 | old_value = *value_addr; |
| 2274 | if (old_value == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2275 | if (deflt) { |
| 2276 | Py_INCREF(deflt); |
| 2277 | return deflt; |
| 2278 | } |
| 2279 | set_key_error(key); |
| 2280 | return NULL; |
| 2281 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2282 | *value_addr = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2283 | mp->ma_used--; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2284 | if (!_PyDict_HasSplitTable(mp)) { |
| 2285 | ENSURE_ALLOWS_DELETIONS(mp); |
| 2286 | old_key = ep->me_key; |
| 2287 | Py_INCREF(dummy); |
| 2288 | ep->me_key = dummy; |
| 2289 | Py_DECREF(old_key); |
| 2290 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2291 | return old_value; |
Guido van Rossum | e027d98 | 2002-04-12 15:11:59 +0000 | [diff] [blame] | 2292 | } |
| 2293 | |
| 2294 | static PyObject * |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2295 | dict_popitem(PyDictObject *mp) |
Guido van Rossum | ba6ab84 | 2000-12-12 22:02:18 +0000 | [diff] [blame] | 2296 | { |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2297 | Py_hash_t i = 0; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2298 | PyDictKeyEntry *ep; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2299 | PyObject *res; |
Guido van Rossum | ba6ab84 | 2000-12-12 22:02:18 +0000 | [diff] [blame] | 2300 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2301 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2302 | /* Allocate the result tuple before checking the size. Believe it |
| 2303 | * or not, this allocation could trigger a garbage collection which |
| 2304 | * could empty the dict, so if we checked the size first and that |
| 2305 | * happened, the result would be an infinite loop (searching for an |
| 2306 | * entry that no longer exists). Note that the usual popitem() |
| 2307 | * idiom is "while d: k, v = d.popitem()". so needing to throw the |
| 2308 | * tuple away if the dict *is* empty isn't a significant |
| 2309 | * inefficiency -- possible, but unlikely in practice. |
| 2310 | */ |
| 2311 | res = PyTuple_New(2); |
| 2312 | if (res == NULL) |
| 2313 | return NULL; |
| 2314 | if (mp->ma_used == 0) { |
| 2315 | Py_DECREF(res); |
| 2316 | PyErr_SetString(PyExc_KeyError, |
| 2317 | "popitem(): dictionary is empty"); |
| 2318 | return NULL; |
| 2319 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2320 | /* Convert split table to combined table */ |
| 2321 | if (mp->ma_keys->dk_lookup == lookdict_split) { |
| 2322 | if (dictresize(mp, DK_SIZE(mp->ma_keys))) { |
| 2323 | Py_DECREF(res); |
| 2324 | return NULL; |
| 2325 | } |
| 2326 | } |
| 2327 | ENSURE_ALLOWS_DELETIONS(mp); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2328 | /* Set ep to "the first" dict entry with a value. We abuse the hash |
| 2329 | * field of slot 0 to hold a search finger: |
| 2330 | * If slot 0 has a value, use slot 0. |
| 2331 | * Else slot 0 is being used to hold a search finger, |
| 2332 | * and we use its hash value as the first index to look. |
| 2333 | */ |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2334 | ep = &mp->ma_keys->dk_entries[0]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2335 | if (ep->me_value == NULL) { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2336 | Py_ssize_t mask = DK_MASK(mp->ma_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2337 | i = ep->me_hash; |
| 2338 | /* The hash field may be a real hash value, or it may be a |
| 2339 | * legit search finger, or it may be a once-legit search |
| 2340 | * finger that's out of bounds now because it wrapped around |
| 2341 | * or the table shrunk -- simply make sure it's in bounds now. |
| 2342 | */ |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2343 | if (i > mask || i < 1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2344 | i = 1; /* skip slot 0 */ |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2345 | while ((ep = &mp->ma_keys->dk_entries[i])->me_value == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2346 | i++; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2347 | if (i > mask) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2348 | i = 1; |
| 2349 | } |
| 2350 | } |
| 2351 | PyTuple_SET_ITEM(res, 0, ep->me_key); |
| 2352 | PyTuple_SET_ITEM(res, 1, ep->me_value); |
| 2353 | Py_INCREF(dummy); |
| 2354 | ep->me_key = dummy; |
| 2355 | ep->me_value = NULL; |
| 2356 | mp->ma_used--; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2357 | assert(mp->ma_keys->dk_entries[0].me_value == NULL); |
| 2358 | 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] | 2359 | return res; |
Guido van Rossum | ba6ab84 | 2000-12-12 22:02:18 +0000 | [diff] [blame] | 2360 | } |
| 2361 | |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 2362 | static int |
| 2363 | dict_traverse(PyObject *op, visitproc visit, void *arg) |
| 2364 | { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2365 | Py_ssize_t i, n; |
| 2366 | PyDictObject *mp = (PyDictObject *)op; |
| 2367 | if (mp->ma_keys->dk_lookup == lookdict) { |
| 2368 | for (i = 0; i < DK_SIZE(mp->ma_keys); i++) { |
| 2369 | if (mp->ma_keys->dk_entries[i].me_value != NULL) { |
| 2370 | Py_VISIT(mp->ma_keys->dk_entries[i].me_value); |
| 2371 | Py_VISIT(mp->ma_keys->dk_entries[i].me_key); |
| 2372 | } |
| 2373 | } |
| 2374 | } else { |
| 2375 | if (mp->ma_values != NULL) { |
| 2376 | for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) { |
| 2377 | Py_VISIT(mp->ma_values[i]); |
| 2378 | } |
| 2379 | } |
| 2380 | else { |
| 2381 | for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) { |
| 2382 | Py_VISIT(mp->ma_keys->dk_entries[i].me_value); |
| 2383 | } |
| 2384 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2385 | } |
| 2386 | return 0; |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 2387 | } |
| 2388 | |
| 2389 | static int |
| 2390 | dict_tp_clear(PyObject *op) |
| 2391 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2392 | PyDict_Clear(op); |
| 2393 | return 0; |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 2394 | } |
| 2395 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2396 | static PyObject *dictiter_new(PyDictObject *, PyTypeObject *); |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 2397 | |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 2398 | static PyObject * |
| 2399 | dict_sizeof(PyDictObject *mp) |
| 2400 | { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2401 | Py_ssize_t size; |
| 2402 | double res, keys_size; |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 2403 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2404 | size = DK_SIZE(mp->ma_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2405 | res = sizeof(PyDictObject); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2406 | if (mp->ma_values) |
| 2407 | res += size * sizeof(PyObject*); |
| 2408 | /* Count our share of the keys object -- with rounding errors. */ |
| 2409 | keys_size = sizeof(PyDictKeysObject) + (size-1) * sizeof(PyDictKeyEntry); |
| 2410 | /* If refcnt > 1, then one count is (probably) held by a type */ |
| 2411 | /* XXX This is somewhat approximate :) */ |
| 2412 | if (mp->ma_keys->dk_refcnt < 3) |
| 2413 | res += keys_size; |
| 2414 | else |
| 2415 | res += keys_size / (mp->ma_keys->dk_refcnt - 1); |
| 2416 | return PyFloat_FromDouble(res); |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 2417 | } |
| 2418 | |
Raymond Hettinger | 8f5cdaa | 2003-12-13 11:26:12 +0000 | [diff] [blame] | 2419 | PyDoc_STRVAR(contains__doc__, |
| 2420 | "D.__contains__(k) -> True if D has a key k, else False"); |
| 2421 | |
| 2422 | PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]"); |
| 2423 | |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 2424 | PyDoc_STRVAR(sizeof__doc__, |
| 2425 | "D.__sizeof__() -> size of D in memory, in bytes"); |
| 2426 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2427 | PyDoc_STRVAR(get__doc__, |
Guido van Rossum | efae886 | 2002-09-04 11:29:45 +0000 | [diff] [blame] | 2428 | "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] | 2429 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2430 | PyDoc_STRVAR(setdefault_doc__, |
Guido van Rossum | efae886 | 2002-09-04 11:29:45 +0000 | [diff] [blame] | 2431 | "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] | 2432 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2433 | PyDoc_STRVAR(pop__doc__, |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 2434 | "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] | 2435 | 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] | 2436 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2437 | PyDoc_STRVAR(popitem__doc__, |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2438 | "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] | 2439 | 2-tuple; but raise KeyError if D is empty."); |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2440 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2441 | PyDoc_STRVAR(update__doc__, |
Georg Brandl | ac0675c | 2011-12-18 19:30:55 +0100 | [diff] [blame] | 2442 | "D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n" |
| 2443 | "If E present and has a .keys() method, does: for k in E: D[k] = E[k]\n\ |
| 2444 | 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] | 2445 | 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] | 2446 | |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 2447 | PyDoc_STRVAR(fromkeys__doc__, |
| 2448 | "dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\ |
| 2449 | v defaults to None."); |
| 2450 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2451 | PyDoc_STRVAR(clear__doc__, |
| 2452 | "D.clear() -> None. Remove all items from D."); |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2453 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2454 | PyDoc_STRVAR(copy__doc__, |
| 2455 | "D.copy() -> a shallow copy of D"); |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2456 | |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 2457 | /* Forward */ |
| 2458 | static PyObject *dictkeys_new(PyObject *); |
| 2459 | static PyObject *dictitems_new(PyObject *); |
| 2460 | static PyObject *dictvalues_new(PyObject *); |
| 2461 | |
Guido van Rossum | 45c85d1 | 2007-07-27 16:31:40 +0000 | [diff] [blame] | 2462 | PyDoc_STRVAR(keys__doc__, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2463 | "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] | 2464 | PyDoc_STRVAR(items__doc__, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2465 | "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] | 2466 | PyDoc_STRVAR(values__doc__, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2467 | "D.values() -> an object providing a view on D's values"); |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 2468 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2469 | static PyMethodDef mapp_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2470 | {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST, |
| 2471 | contains__doc__}, |
| 2472 | {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST, |
| 2473 | getitem__doc__}, |
| 2474 | {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS, |
| 2475 | sizeof__doc__}, |
| 2476 | {"get", (PyCFunction)dict_get, METH_VARARGS, |
| 2477 | get__doc__}, |
| 2478 | {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS, |
| 2479 | setdefault_doc__}, |
| 2480 | {"pop", (PyCFunction)dict_pop, METH_VARARGS, |
| 2481 | pop__doc__}, |
| 2482 | {"popitem", (PyCFunction)dict_popitem, METH_NOARGS, |
| 2483 | popitem__doc__}, |
| 2484 | {"keys", (PyCFunction)dictkeys_new, METH_NOARGS, |
| 2485 | keys__doc__}, |
| 2486 | {"items", (PyCFunction)dictitems_new, METH_NOARGS, |
| 2487 | items__doc__}, |
| 2488 | {"values", (PyCFunction)dictvalues_new, METH_NOARGS, |
| 2489 | values__doc__}, |
| 2490 | {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS, |
| 2491 | update__doc__}, |
| 2492 | {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS, |
| 2493 | fromkeys__doc__}, |
| 2494 | {"clear", (PyCFunction)dict_clear, METH_NOARGS, |
| 2495 | clear__doc__}, |
| 2496 | {"copy", (PyCFunction)dict_copy, METH_NOARGS, |
| 2497 | copy__doc__}, |
| 2498 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2499 | }; |
| 2500 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2501 | /* 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] | 2502 | int |
| 2503 | PyDict_Contains(PyObject *op, PyObject *key) |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 2504 | { |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2505 | Py_hash_t hash; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2506 | PyDictObject *mp = (PyDictObject *)op; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2507 | PyDictKeyEntry *ep; |
| 2508 | PyObject **value_addr; |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 2509 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2510 | if (!PyUnicode_CheckExact(key) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2511 | (hash = ((PyASCIIObject *) key)->hash) == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2512 | hash = PyObject_Hash(key); |
| 2513 | if (hash == -1) |
| 2514 | return -1; |
| 2515 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2516 | ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr); |
| 2517 | return (ep == NULL) ? -1 : (*value_addr != NULL); |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 2518 | } |
| 2519 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2520 | /* Internal version of PyDict_Contains used when the hash value is already known */ |
| 2521 | int |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2522 | _PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2523 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2524 | PyDictObject *mp = (PyDictObject *)op; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2525 | PyDictKeyEntry *ep; |
| 2526 | PyObject **value_addr; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2527 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2528 | ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr); |
| 2529 | return (ep == NULL) ? -1 : (*value_addr != NULL); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2530 | } |
| 2531 | |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 2532 | /* Hack to implement "key in dict" */ |
| 2533 | static PySequenceMethods dict_as_sequence = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2534 | 0, /* sq_length */ |
| 2535 | 0, /* sq_concat */ |
| 2536 | 0, /* sq_repeat */ |
| 2537 | 0, /* sq_item */ |
| 2538 | 0, /* sq_slice */ |
| 2539 | 0, /* sq_ass_item */ |
| 2540 | 0, /* sq_ass_slice */ |
| 2541 | PyDict_Contains, /* sq_contains */ |
| 2542 | 0, /* sq_inplace_concat */ |
| 2543 | 0, /* sq_inplace_repeat */ |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 2544 | }; |
| 2545 | |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 2546 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2547 | dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2548 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2549 | PyObject *self; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2550 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2551 | assert(type != NULL && type->tp_alloc != NULL); |
| 2552 | self = type->tp_alloc(type, 0); |
| 2553 | if (self != NULL) { |
| 2554 | PyDictObject *d = (PyDictObject *)self; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2555 | d->ma_keys = new_keys_object(PyDict_MINSIZE_COMBINED); |
| 2556 | /* XXX - Should we raise a no-memory error? */ |
| 2557 | if (d->ma_keys == NULL) { |
| 2558 | DK_INCREF(Py_EMPTY_KEYS); |
| 2559 | d->ma_keys = Py_EMPTY_KEYS; |
| 2560 | d->ma_values = empty_values; |
| 2561 | } |
| 2562 | d->ma_used = 0; |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 2563 | /* The object has been implicitly tracked by tp_alloc */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2564 | if (type == &PyDict_Type) |
| 2565 | _PyObject_GC_UNTRACK(d); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2566 | } |
| 2567 | return self; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2568 | } |
| 2569 | |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 2570 | static int |
| 2571 | dict_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 2572 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2573 | return dict_update_common(self, args, kwds, "dict"); |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 2574 | } |
| 2575 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2576 | static PyObject * |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2577 | dict_iter(PyDictObject *dict) |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 2578 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2579 | return dictiter_new(dict, &PyDictIterKey_Type); |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 2580 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2581 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2582 | PyDoc_STRVAR(dictionary_doc, |
Ezio Melotti | 7f807b7 | 2010-03-01 04:08:34 +0000 | [diff] [blame] | 2583 | "dict() -> new empty dictionary\n" |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 2584 | "dict(mapping) -> new dictionary initialized from a mapping object's\n" |
Ezio Melotti | 7f807b7 | 2010-03-01 04:08:34 +0000 | [diff] [blame] | 2585 | " (key, value) pairs\n" |
| 2586 | "dict(iterable) -> new dictionary initialized as if via:\n" |
Tim Peters | 4d85953 | 2001-10-27 18:27:48 +0000 | [diff] [blame] | 2587 | " d = {}\n" |
Ezio Melotti | 7f807b7 | 2010-03-01 04:08:34 +0000 | [diff] [blame] | 2588 | " for k, v in iterable:\n" |
Just van Rossum | a797d81 | 2002-11-23 09:45:04 +0000 | [diff] [blame] | 2589 | " d[k] = v\n" |
| 2590 | "dict(**kwargs) -> new dictionary initialized with the name=value pairs\n" |
| 2591 | " in the keyword argument list. For example: dict(one=1, two=2)"); |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 2592 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2593 | PyTypeObject PyDict_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2594 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2595 | "dict", |
| 2596 | sizeof(PyDictObject), |
| 2597 | 0, |
| 2598 | (destructor)dict_dealloc, /* tp_dealloc */ |
| 2599 | 0, /* tp_print */ |
| 2600 | 0, /* tp_getattr */ |
| 2601 | 0, /* tp_setattr */ |
| 2602 | 0, /* tp_reserved */ |
| 2603 | (reprfunc)dict_repr, /* tp_repr */ |
| 2604 | 0, /* tp_as_number */ |
| 2605 | &dict_as_sequence, /* tp_as_sequence */ |
| 2606 | &dict_as_mapping, /* tp_as_mapping */ |
Georg Brandl | 00da4e0 | 2010-10-18 07:32:48 +0000 | [diff] [blame] | 2607 | PyObject_HashNotImplemented, /* tp_hash */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2608 | 0, /* tp_call */ |
| 2609 | 0, /* tp_str */ |
| 2610 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2611 | 0, /* tp_setattro */ |
| 2612 | 0, /* tp_as_buffer */ |
| 2613 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 2614 | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */ |
| 2615 | dictionary_doc, /* tp_doc */ |
| 2616 | dict_traverse, /* tp_traverse */ |
| 2617 | dict_tp_clear, /* tp_clear */ |
| 2618 | dict_richcompare, /* tp_richcompare */ |
| 2619 | 0, /* tp_weaklistoffset */ |
| 2620 | (getiterfunc)dict_iter, /* tp_iter */ |
| 2621 | 0, /* tp_iternext */ |
| 2622 | mapp_methods, /* tp_methods */ |
| 2623 | 0, /* tp_members */ |
| 2624 | 0, /* tp_getset */ |
| 2625 | 0, /* tp_base */ |
| 2626 | 0, /* tp_dict */ |
| 2627 | 0, /* tp_descr_get */ |
| 2628 | 0, /* tp_descr_set */ |
| 2629 | 0, /* tp_dictoffset */ |
| 2630 | dict_init, /* tp_init */ |
| 2631 | PyType_GenericAlloc, /* tp_alloc */ |
| 2632 | dict_new, /* tp_new */ |
| 2633 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2634 | }; |
| 2635 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2636 | PyObject * |
| 2637 | _PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key) |
| 2638 | { |
| 2639 | PyObject *kv; |
| 2640 | kv = _PyUnicode_FromId(key); /* borrowed */ |
| 2641 | if (kv == NULL) |
| 2642 | return NULL; |
| 2643 | return PyDict_GetItem(dp, kv); |
| 2644 | } |
| 2645 | |
Guido van Rossum | 3cca245 | 1997-05-16 14:23:33 +0000 | [diff] [blame] | 2646 | /* For backward compatibility with old dictionary interface */ |
| 2647 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2648 | PyObject * |
Martin v. Löwis | 32b4a1b | 2002-12-11 13:21:12 +0000 | [diff] [blame] | 2649 | PyDict_GetItemString(PyObject *v, const char *key) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2650 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2651 | PyObject *kv, *rv; |
| 2652 | kv = PyUnicode_FromString(key); |
| 2653 | if (kv == NULL) |
| 2654 | return NULL; |
| 2655 | rv = PyDict_GetItem(v, kv); |
| 2656 | Py_DECREF(kv); |
| 2657 | return rv; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2658 | } |
| 2659 | |
| 2660 | int |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2661 | _PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item) |
| 2662 | { |
| 2663 | PyObject *kv; |
| 2664 | kv = _PyUnicode_FromId(key); /* borrowed */ |
| 2665 | if (kv == NULL) |
| 2666 | return -1; |
| 2667 | return PyDict_SetItem(v, kv, item); |
| 2668 | } |
| 2669 | |
| 2670 | int |
Martin v. Löwis | 32b4a1b | 2002-12-11 13:21:12 +0000 | [diff] [blame] | 2671 | PyDict_SetItemString(PyObject *v, const char *key, PyObject *item) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2672 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2673 | PyObject *kv; |
| 2674 | int err; |
| 2675 | kv = PyUnicode_FromString(key); |
| 2676 | if (kv == NULL) |
| 2677 | return -1; |
| 2678 | PyUnicode_InternInPlace(&kv); /* XXX Should we really? */ |
| 2679 | err = PyDict_SetItem(v, kv, item); |
| 2680 | Py_DECREF(kv); |
| 2681 | return err; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2682 | } |
| 2683 | |
| 2684 | int |
Martin v. Löwis | 32b4a1b | 2002-12-11 13:21:12 +0000 | [diff] [blame] | 2685 | PyDict_DelItemString(PyObject *v, const char *key) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2686 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2687 | PyObject *kv; |
| 2688 | int err; |
| 2689 | kv = PyUnicode_FromString(key); |
| 2690 | if (kv == NULL) |
| 2691 | return -1; |
| 2692 | err = PyDict_DelItem(v, kv); |
| 2693 | Py_DECREF(kv); |
| 2694 | return err; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2695 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2696 | |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2697 | /* Dictionary iterator types */ |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2698 | |
| 2699 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2700 | PyObject_HEAD |
| 2701 | PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */ |
| 2702 | Py_ssize_t di_used; |
| 2703 | Py_ssize_t di_pos; |
| 2704 | PyObject* di_result; /* reusable result tuple for iteritems */ |
| 2705 | Py_ssize_t len; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2706 | } dictiterobject; |
| 2707 | |
| 2708 | static PyObject * |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2709 | dictiter_new(PyDictObject *dict, PyTypeObject *itertype) |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2710 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2711 | dictiterobject *di; |
| 2712 | di = PyObject_GC_New(dictiterobject, itertype); |
| 2713 | if (di == NULL) |
| 2714 | return NULL; |
| 2715 | Py_INCREF(dict); |
| 2716 | di->di_dict = dict; |
| 2717 | di->di_used = dict->ma_used; |
| 2718 | di->di_pos = 0; |
| 2719 | di->len = dict->ma_used; |
| 2720 | if (itertype == &PyDictIterItem_Type) { |
| 2721 | di->di_result = PyTuple_Pack(2, Py_None, Py_None); |
| 2722 | if (di->di_result == NULL) { |
| 2723 | Py_DECREF(di); |
| 2724 | return NULL; |
| 2725 | } |
| 2726 | } |
| 2727 | else |
| 2728 | di->di_result = NULL; |
| 2729 | _PyObject_GC_TRACK(di); |
| 2730 | return (PyObject *)di; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2731 | } |
| 2732 | |
| 2733 | static void |
| 2734 | dictiter_dealloc(dictiterobject *di) |
| 2735 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2736 | Py_XDECREF(di->di_dict); |
| 2737 | Py_XDECREF(di->di_result); |
| 2738 | PyObject_GC_Del(di); |
Antoine Pitrou | 7ddda78 | 2009-01-01 15:35:33 +0000 | [diff] [blame] | 2739 | } |
| 2740 | |
| 2741 | static int |
| 2742 | dictiter_traverse(dictiterobject *di, visitproc visit, void *arg) |
| 2743 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2744 | Py_VISIT(di->di_dict); |
| 2745 | Py_VISIT(di->di_result); |
| 2746 | return 0; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2747 | } |
| 2748 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 2749 | static PyObject * |
Raymond Hettinger | 0ce6dc8 | 2004-03-18 08:38:00 +0000 | [diff] [blame] | 2750 | dictiter_len(dictiterobject *di) |
| 2751 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2752 | Py_ssize_t len = 0; |
| 2753 | if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used) |
| 2754 | len = di->len; |
| 2755 | return PyLong_FromSize_t(len); |
Raymond Hettinger | 0ce6dc8 | 2004-03-18 08:38:00 +0000 | [diff] [blame] | 2756 | } |
| 2757 | |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 2758 | PyDoc_STRVAR(length_hint_doc, |
| 2759 | "Private method returning an estimate of len(list(it))."); |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 2760 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2761 | static PyObject * |
| 2762 | dictiter_reduce(dictiterobject *di); |
| 2763 | |
| 2764 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 2765 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 2766 | static PyMethodDef dictiter_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2767 | {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS, |
| 2768 | length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2769 | {"__reduce__", (PyCFunction)dictiter_reduce, METH_NOARGS, |
| 2770 | reduce_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2771 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | 0ce6dc8 | 2004-03-18 08:38:00 +0000 | [diff] [blame] | 2772 | }; |
| 2773 | |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2774 | static PyObject *dictiter_iternextkey(dictiterobject *di) |
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 | PyObject *key; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2777 | register Py_ssize_t i, mask, offset; |
| 2778 | register PyDictKeysObject *k; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2779 | PyDictObject *d = di->di_dict; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2780 | PyObject **value_ptr; |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2781 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2782 | if (d == NULL) |
| 2783 | return NULL; |
| 2784 | assert (PyDict_Check(d)); |
Guido van Rossum | 2147df7 | 2002-07-16 20:30:22 +0000 | [diff] [blame] | 2785 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2786 | if (di->di_used != d->ma_used) { |
| 2787 | PyErr_SetString(PyExc_RuntimeError, |
| 2788 | "dictionary changed size during iteration"); |
| 2789 | di->di_used = -1; /* Make this state sticky */ |
| 2790 | return NULL; |
| 2791 | } |
Guido van Rossum | 2147df7 | 2002-07-16 20:30:22 +0000 | [diff] [blame] | 2792 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2793 | i = di->di_pos; |
| 2794 | if (i < 0) |
| 2795 | goto fail; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2796 | k = d->ma_keys; |
| 2797 | if (d->ma_values) { |
| 2798 | value_ptr = &d->ma_values[i]; |
| 2799 | offset = sizeof(PyObject *); |
| 2800 | } |
| 2801 | else { |
| 2802 | value_ptr = &k->dk_entries[i].me_value; |
| 2803 | offset = sizeof(PyDictKeyEntry); |
| 2804 | } |
| 2805 | mask = DK_SIZE(k)-1; |
| 2806 | while (i <= mask && *value_ptr == NULL) { |
| 2807 | value_ptr = (PyObject **)(((char *)value_ptr) + offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2808 | i++; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2809 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2810 | di->di_pos = i+1; |
| 2811 | if (i > mask) |
| 2812 | goto fail; |
| 2813 | di->len--; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2814 | key = k->dk_entries[i].me_key; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2815 | Py_INCREF(key); |
| 2816 | return key; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2817 | |
| 2818 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2819 | Py_DECREF(d); |
| 2820 | di->di_dict = NULL; |
| 2821 | return NULL; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2822 | } |
| 2823 | |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2824 | PyTypeObject PyDictIterKey_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2825 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2826 | "dict_keyiterator", /* tp_name */ |
| 2827 | sizeof(dictiterobject), /* tp_basicsize */ |
| 2828 | 0, /* tp_itemsize */ |
| 2829 | /* methods */ |
| 2830 | (destructor)dictiter_dealloc, /* tp_dealloc */ |
| 2831 | 0, /* tp_print */ |
| 2832 | 0, /* tp_getattr */ |
| 2833 | 0, /* tp_setattr */ |
| 2834 | 0, /* tp_reserved */ |
| 2835 | 0, /* tp_repr */ |
| 2836 | 0, /* tp_as_number */ |
| 2837 | 0, /* tp_as_sequence */ |
| 2838 | 0, /* tp_as_mapping */ |
| 2839 | 0, /* tp_hash */ |
| 2840 | 0, /* tp_call */ |
| 2841 | 0, /* tp_str */ |
| 2842 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2843 | 0, /* tp_setattro */ |
| 2844 | 0, /* tp_as_buffer */ |
| 2845 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 2846 | 0, /* tp_doc */ |
| 2847 | (traverseproc)dictiter_traverse, /* tp_traverse */ |
| 2848 | 0, /* tp_clear */ |
| 2849 | 0, /* tp_richcompare */ |
| 2850 | 0, /* tp_weaklistoffset */ |
| 2851 | PyObject_SelfIter, /* tp_iter */ |
| 2852 | (iternextfunc)dictiter_iternextkey, /* tp_iternext */ |
| 2853 | dictiter_methods, /* tp_methods */ |
| 2854 | 0, |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2855 | }; |
| 2856 | |
| 2857 | static PyObject *dictiter_iternextvalue(dictiterobject *di) |
| 2858 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2859 | PyObject *value; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2860 | register Py_ssize_t i, mask, offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2861 | PyDictObject *d = di->di_dict; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2862 | PyObject **value_ptr; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2863 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2864 | if (d == NULL) |
| 2865 | return NULL; |
| 2866 | assert (PyDict_Check(d)); |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2867 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2868 | if (di->di_used != d->ma_used) { |
| 2869 | PyErr_SetString(PyExc_RuntimeError, |
| 2870 | "dictionary changed size during iteration"); |
| 2871 | di->di_used = -1; /* Make this state sticky */ |
| 2872 | return NULL; |
| 2873 | } |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2874 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2875 | i = di->di_pos; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2876 | mask = DK_SIZE(d->ma_keys)-1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2877 | if (i < 0 || i > mask) |
| 2878 | goto fail; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2879 | if (d->ma_values) { |
| 2880 | value_ptr = &d->ma_values[i]; |
| 2881 | offset = sizeof(PyObject *); |
| 2882 | } |
| 2883 | else { |
| 2884 | value_ptr = &d->ma_keys->dk_entries[i].me_value; |
| 2885 | offset = sizeof(PyDictKeyEntry); |
| 2886 | } |
| 2887 | while (i <= mask && *value_ptr == NULL) { |
| 2888 | value_ptr = (PyObject **)(((char *)value_ptr) + offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2889 | i++; |
| 2890 | if (i > mask) |
| 2891 | goto fail; |
| 2892 | } |
| 2893 | di->di_pos = i+1; |
| 2894 | di->len--; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2895 | value = *value_ptr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2896 | Py_INCREF(value); |
| 2897 | return value; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2898 | |
| 2899 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2900 | Py_DECREF(d); |
| 2901 | di->di_dict = NULL; |
| 2902 | return NULL; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2903 | } |
| 2904 | |
| 2905 | PyTypeObject PyDictIterValue_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2906 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2907 | "dict_valueiterator", /* tp_name */ |
| 2908 | sizeof(dictiterobject), /* tp_basicsize */ |
| 2909 | 0, /* tp_itemsize */ |
| 2910 | /* methods */ |
| 2911 | (destructor)dictiter_dealloc, /* tp_dealloc */ |
| 2912 | 0, /* tp_print */ |
| 2913 | 0, /* tp_getattr */ |
| 2914 | 0, /* tp_setattr */ |
| 2915 | 0, /* tp_reserved */ |
| 2916 | 0, /* tp_repr */ |
| 2917 | 0, /* tp_as_number */ |
| 2918 | 0, /* tp_as_sequence */ |
| 2919 | 0, /* tp_as_mapping */ |
| 2920 | 0, /* tp_hash */ |
| 2921 | 0, /* tp_call */ |
| 2922 | 0, /* tp_str */ |
| 2923 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2924 | 0, /* tp_setattro */ |
| 2925 | 0, /* tp_as_buffer */ |
| 2926 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 2927 | 0, /* tp_doc */ |
| 2928 | (traverseproc)dictiter_traverse, /* tp_traverse */ |
| 2929 | 0, /* tp_clear */ |
| 2930 | 0, /* tp_richcompare */ |
| 2931 | 0, /* tp_weaklistoffset */ |
| 2932 | PyObject_SelfIter, /* tp_iter */ |
| 2933 | (iternextfunc)dictiter_iternextvalue, /* tp_iternext */ |
| 2934 | dictiter_methods, /* tp_methods */ |
| 2935 | 0, |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2936 | }; |
| 2937 | |
| 2938 | static PyObject *dictiter_iternextitem(dictiterobject *di) |
| 2939 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2940 | PyObject *key, *value, *result = di->di_result; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2941 | register Py_ssize_t i, mask, offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2942 | PyDictObject *d = di->di_dict; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2943 | PyObject **value_ptr; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2944 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2945 | if (d == NULL) |
| 2946 | return NULL; |
| 2947 | assert (PyDict_Check(d)); |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2948 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2949 | if (di->di_used != d->ma_used) { |
| 2950 | PyErr_SetString(PyExc_RuntimeError, |
| 2951 | "dictionary changed size during iteration"); |
| 2952 | di->di_used = -1; /* Make this state sticky */ |
| 2953 | return NULL; |
| 2954 | } |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2955 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2956 | i = di->di_pos; |
| 2957 | if (i < 0) |
| 2958 | goto fail; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2959 | mask = DK_SIZE(d->ma_keys)-1; |
| 2960 | if (d->ma_values) { |
| 2961 | value_ptr = &d->ma_values[i]; |
| 2962 | offset = sizeof(PyObject *); |
| 2963 | } |
| 2964 | else { |
| 2965 | value_ptr = &d->ma_keys->dk_entries[i].me_value; |
| 2966 | offset = sizeof(PyDictKeyEntry); |
| 2967 | } |
| 2968 | while (i <= mask && *value_ptr == NULL) { |
| 2969 | value_ptr = (PyObject **)(((char *)value_ptr) + offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2970 | i++; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2971 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2972 | di->di_pos = i+1; |
| 2973 | if (i > mask) |
| 2974 | goto fail; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2975 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2976 | if (result->ob_refcnt == 1) { |
| 2977 | Py_INCREF(result); |
| 2978 | Py_DECREF(PyTuple_GET_ITEM(result, 0)); |
| 2979 | Py_DECREF(PyTuple_GET_ITEM(result, 1)); |
| 2980 | } else { |
| 2981 | result = PyTuple_New(2); |
| 2982 | if (result == NULL) |
| 2983 | return NULL; |
| 2984 | } |
| 2985 | di->len--; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2986 | key = d->ma_keys->dk_entries[i].me_key; |
| 2987 | value = *value_ptr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2988 | Py_INCREF(key); |
| 2989 | Py_INCREF(value); |
| 2990 | PyTuple_SET_ITEM(result, 0, key); |
| 2991 | PyTuple_SET_ITEM(result, 1, value); |
| 2992 | return result; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2993 | |
| 2994 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2995 | Py_DECREF(d); |
| 2996 | di->di_dict = NULL; |
| 2997 | return NULL; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2998 | } |
| 2999 | |
| 3000 | PyTypeObject PyDictIterItem_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3001 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 3002 | "dict_itemiterator", /* tp_name */ |
| 3003 | sizeof(dictiterobject), /* tp_basicsize */ |
| 3004 | 0, /* tp_itemsize */ |
| 3005 | /* methods */ |
| 3006 | (destructor)dictiter_dealloc, /* tp_dealloc */ |
| 3007 | 0, /* tp_print */ |
| 3008 | 0, /* tp_getattr */ |
| 3009 | 0, /* tp_setattr */ |
| 3010 | 0, /* tp_reserved */ |
| 3011 | 0, /* tp_repr */ |
| 3012 | 0, /* tp_as_number */ |
| 3013 | 0, /* tp_as_sequence */ |
| 3014 | 0, /* tp_as_mapping */ |
| 3015 | 0, /* tp_hash */ |
| 3016 | 0, /* tp_call */ |
| 3017 | 0, /* tp_str */ |
| 3018 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3019 | 0, /* tp_setattro */ |
| 3020 | 0, /* tp_as_buffer */ |
| 3021 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 3022 | 0, /* tp_doc */ |
| 3023 | (traverseproc)dictiter_traverse, /* tp_traverse */ |
| 3024 | 0, /* tp_clear */ |
| 3025 | 0, /* tp_richcompare */ |
| 3026 | 0, /* tp_weaklistoffset */ |
| 3027 | PyObject_SelfIter, /* tp_iter */ |
| 3028 | (iternextfunc)dictiter_iternextitem, /* tp_iternext */ |
| 3029 | dictiter_methods, /* tp_methods */ |
| 3030 | 0, |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3031 | }; |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3032 | |
| 3033 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3034 | static PyObject * |
| 3035 | dictiter_reduce(dictiterobject *di) |
| 3036 | { |
| 3037 | PyObject *list; |
| 3038 | dictiterobject tmp; |
| 3039 | |
| 3040 | list = PyList_New(0); |
| 3041 | if (!list) |
| 3042 | return NULL; |
| 3043 | |
| 3044 | /* copy the itertor state */ |
| 3045 | tmp = *di; |
| 3046 | Py_XINCREF(tmp.di_dict); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 3047 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3048 | /* iterate the temporary into a list */ |
| 3049 | for(;;) { |
| 3050 | PyObject *element = 0; |
| 3051 | if (Py_TYPE(di) == &PyDictIterItem_Type) |
| 3052 | element = dictiter_iternextitem(&tmp); |
| 3053 | else if (Py_TYPE(di) == &PyDictIterKey_Type) |
| 3054 | element = dictiter_iternextkey(&tmp); |
| 3055 | else if (Py_TYPE(di) == &PyDictIterValue_Type) |
| 3056 | element = dictiter_iternextvalue(&tmp); |
| 3057 | else |
| 3058 | assert(0); |
| 3059 | if (element) { |
| 3060 | if (PyList_Append(list, element)) { |
| 3061 | Py_DECREF(element); |
| 3062 | Py_DECREF(list); |
| 3063 | Py_XDECREF(tmp.di_dict); |
| 3064 | return NULL; |
| 3065 | } |
| 3066 | Py_DECREF(element); |
| 3067 | } else |
| 3068 | break; |
| 3069 | } |
| 3070 | Py_XDECREF(tmp.di_dict); |
| 3071 | /* check for error */ |
| 3072 | if (tmp.di_dict != NULL) { |
| 3073 | /* we have an error */ |
| 3074 | Py_DECREF(list); |
| 3075 | return NULL; |
| 3076 | } |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 3077 | return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3078 | } |
| 3079 | |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 3080 | /***********************************************/ |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3081 | /* View objects for keys(), items(), values(). */ |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 3082 | /***********************************************/ |
| 3083 | |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3084 | /* The instance lay-out is the same for all three; but the type differs. */ |
| 3085 | |
| 3086 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3087 | PyObject_HEAD |
| 3088 | PyDictObject *dv_dict; |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3089 | } dictviewobject; |
| 3090 | |
| 3091 | |
| 3092 | static void |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 3093 | dictview_dealloc(dictviewobject *dv) |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3094 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3095 | Py_XDECREF(dv->dv_dict); |
| 3096 | PyObject_GC_Del(dv); |
Antoine Pitrou | 7ddda78 | 2009-01-01 15:35:33 +0000 | [diff] [blame] | 3097 | } |
| 3098 | |
| 3099 | static int |
| 3100 | dictview_traverse(dictviewobject *dv, visitproc visit, void *arg) |
| 3101 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3102 | Py_VISIT(dv->dv_dict); |
| 3103 | return 0; |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3104 | } |
| 3105 | |
Guido van Rossum | 83825ac | 2007-02-10 04:54:19 +0000 | [diff] [blame] | 3106 | static Py_ssize_t |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 3107 | dictview_len(dictviewobject *dv) |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3108 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3109 | Py_ssize_t len = 0; |
| 3110 | if (dv->dv_dict != NULL) |
| 3111 | len = dv->dv_dict->ma_used; |
| 3112 | return len; |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3113 | } |
| 3114 | |
| 3115 | static PyObject * |
| 3116 | dictview_new(PyObject *dict, PyTypeObject *type) |
| 3117 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3118 | dictviewobject *dv; |
| 3119 | if (dict == NULL) { |
| 3120 | PyErr_BadInternalCall(); |
| 3121 | return NULL; |
| 3122 | } |
| 3123 | if (!PyDict_Check(dict)) { |
| 3124 | /* XXX Get rid of this restriction later */ |
| 3125 | PyErr_Format(PyExc_TypeError, |
| 3126 | "%s() requires a dict argument, not '%s'", |
| 3127 | type->tp_name, dict->ob_type->tp_name); |
| 3128 | return NULL; |
| 3129 | } |
| 3130 | dv = PyObject_GC_New(dictviewobject, type); |
| 3131 | if (dv == NULL) |
| 3132 | return NULL; |
| 3133 | Py_INCREF(dict); |
| 3134 | dv->dv_dict = (PyDictObject *)dict; |
| 3135 | _PyObject_GC_TRACK(dv); |
| 3136 | return (PyObject *)dv; |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3137 | } |
| 3138 | |
Neal Norwitz | e36f2ba | 2007-02-26 23:12:28 +0000 | [diff] [blame] | 3139 | /* TODO(guido): The views objects are not complete: |
| 3140 | |
| 3141 | * support more set operations |
| 3142 | * support arbitrary mappings? |
| 3143 | - either these should be static or exported in dictobject.h |
| 3144 | - if public then they should probably be in builtins |
| 3145 | */ |
| 3146 | |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 3147 | /* Return 1 if self is a subset of other, iterating over self; |
| 3148 | 0 if not; -1 if an error occurred. */ |
Guido van Rossum | d9214d1 | 2007-02-12 02:23:40 +0000 | [diff] [blame] | 3149 | static int |
| 3150 | all_contained_in(PyObject *self, PyObject *other) |
| 3151 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3152 | PyObject *iter = PyObject_GetIter(self); |
| 3153 | int ok = 1; |
Guido van Rossum | d9214d1 | 2007-02-12 02:23:40 +0000 | [diff] [blame] | 3154 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3155 | if (iter == NULL) |
| 3156 | return -1; |
| 3157 | for (;;) { |
| 3158 | PyObject *next = PyIter_Next(iter); |
| 3159 | if (next == NULL) { |
| 3160 | if (PyErr_Occurred()) |
| 3161 | ok = -1; |
| 3162 | break; |
| 3163 | } |
| 3164 | ok = PySequence_Contains(other, next); |
| 3165 | Py_DECREF(next); |
| 3166 | if (ok <= 0) |
| 3167 | break; |
| 3168 | } |
| 3169 | Py_DECREF(iter); |
| 3170 | return ok; |
Guido van Rossum | d9214d1 | 2007-02-12 02:23:40 +0000 | [diff] [blame] | 3171 | } |
| 3172 | |
| 3173 | static PyObject * |
| 3174 | dictview_richcompare(PyObject *self, PyObject *other, int op) |
| 3175 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3176 | Py_ssize_t len_self, len_other; |
| 3177 | int ok; |
| 3178 | PyObject *result; |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 3179 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3180 | assert(self != NULL); |
| 3181 | assert(PyDictViewSet_Check(self)); |
| 3182 | assert(other != NULL); |
Guido van Rossum | d9214d1 | 2007-02-12 02:23:40 +0000 | [diff] [blame] | 3183 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 3184 | if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other)) |
| 3185 | Py_RETURN_NOTIMPLEMENTED; |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 3186 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3187 | len_self = PyObject_Size(self); |
| 3188 | if (len_self < 0) |
| 3189 | return NULL; |
| 3190 | len_other = PyObject_Size(other); |
| 3191 | if (len_other < 0) |
| 3192 | return NULL; |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 3193 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3194 | ok = 0; |
| 3195 | switch(op) { |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 3196 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3197 | case Py_NE: |
| 3198 | case Py_EQ: |
| 3199 | if (len_self == len_other) |
| 3200 | ok = all_contained_in(self, other); |
| 3201 | if (op == Py_NE && ok >= 0) |
| 3202 | ok = !ok; |
| 3203 | break; |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 3204 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3205 | case Py_LT: |
| 3206 | if (len_self < len_other) |
| 3207 | ok = all_contained_in(self, other); |
| 3208 | break; |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 3209 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3210 | case Py_LE: |
| 3211 | if (len_self <= len_other) |
| 3212 | ok = all_contained_in(self, other); |
| 3213 | break; |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 3214 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3215 | case Py_GT: |
| 3216 | if (len_self > len_other) |
| 3217 | ok = all_contained_in(other, self); |
| 3218 | break; |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 3219 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3220 | case Py_GE: |
| 3221 | if (len_self >= len_other) |
| 3222 | ok = all_contained_in(other, self); |
| 3223 | break; |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 3224 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3225 | } |
| 3226 | if (ok < 0) |
| 3227 | return NULL; |
| 3228 | result = ok ? Py_True : Py_False; |
| 3229 | Py_INCREF(result); |
| 3230 | return result; |
Guido van Rossum | d9214d1 | 2007-02-12 02:23:40 +0000 | [diff] [blame] | 3231 | } |
| 3232 | |
Raymond Hettinger | b0d56af | 2009-03-03 10:52:49 +0000 | [diff] [blame] | 3233 | static PyObject * |
| 3234 | dictview_repr(dictviewobject *dv) |
| 3235 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3236 | PyObject *seq; |
| 3237 | PyObject *result; |
Raymond Hettinger | b0d56af | 2009-03-03 10:52:49 +0000 | [diff] [blame] | 3238 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3239 | seq = PySequence_List((PyObject *)dv); |
| 3240 | if (seq == NULL) |
| 3241 | return NULL; |
| 3242 | |
| 3243 | result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq); |
| 3244 | Py_DECREF(seq); |
| 3245 | return result; |
Raymond Hettinger | b0d56af | 2009-03-03 10:52:49 +0000 | [diff] [blame] | 3246 | } |
| 3247 | |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 3248 | /*** dict_keys ***/ |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3249 | |
| 3250 | static PyObject * |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 3251 | dictkeys_iter(dictviewobject *dv) |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3252 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3253 | if (dv->dv_dict == NULL) { |
| 3254 | Py_RETURN_NONE; |
| 3255 | } |
| 3256 | return dictiter_new(dv->dv_dict, &PyDictIterKey_Type); |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 3257 | } |
| 3258 | |
| 3259 | static int |
| 3260 | dictkeys_contains(dictviewobject *dv, PyObject *obj) |
| 3261 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3262 | if (dv->dv_dict == NULL) |
| 3263 | return 0; |
| 3264 | return PyDict_Contains((PyObject *)dv->dv_dict, obj); |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3265 | } |
| 3266 | |
Guido van Rossum | 83825ac | 2007-02-10 04:54:19 +0000 | [diff] [blame] | 3267 | static PySequenceMethods dictkeys_as_sequence = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3268 | (lenfunc)dictview_len, /* sq_length */ |
| 3269 | 0, /* sq_concat */ |
| 3270 | 0, /* sq_repeat */ |
| 3271 | 0, /* sq_item */ |
| 3272 | 0, /* sq_slice */ |
| 3273 | 0, /* sq_ass_item */ |
| 3274 | 0, /* sq_ass_slice */ |
| 3275 | (objobjproc)dictkeys_contains, /* sq_contains */ |
Guido van Rossum | 83825ac | 2007-02-10 04:54:19 +0000 | [diff] [blame] | 3276 | }; |
| 3277 | |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3278 | static PyObject* |
| 3279 | dictviews_sub(PyObject* self, PyObject *other) |
| 3280 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3281 | PyObject *result = PySet_New(self); |
| 3282 | PyObject *tmp; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3283 | _Py_IDENTIFIER(difference_update); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 3284 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3285 | if (result == NULL) |
| 3286 | return NULL; |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3287 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 3288 | tmp = _PyObject_CallMethodId(result, &PyId_difference_update, "O", other); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3289 | if (tmp == NULL) { |
| 3290 | Py_DECREF(result); |
| 3291 | return NULL; |
| 3292 | } |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3293 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3294 | Py_DECREF(tmp); |
| 3295 | return result; |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3296 | } |
| 3297 | |
| 3298 | static PyObject* |
| 3299 | dictviews_and(PyObject* self, PyObject *other) |
| 3300 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3301 | PyObject *result = PySet_New(self); |
| 3302 | PyObject *tmp; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3303 | _Py_IDENTIFIER(intersection_update); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 3304 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3305 | if (result == NULL) |
| 3306 | return NULL; |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3307 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 3308 | tmp = _PyObject_CallMethodId(result, &PyId_intersection_update, "O", other); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3309 | if (tmp == NULL) { |
| 3310 | Py_DECREF(result); |
| 3311 | return NULL; |
| 3312 | } |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3313 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3314 | Py_DECREF(tmp); |
| 3315 | return result; |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3316 | } |
| 3317 | |
| 3318 | static PyObject* |
| 3319 | dictviews_or(PyObject* self, PyObject *other) |
| 3320 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3321 | PyObject *result = PySet_New(self); |
| 3322 | PyObject *tmp; |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 3323 | _Py_IDENTIFIER(update); |
Victor Stinner | d1a9cc2 | 2011-10-13 22:51:17 +0200 | [diff] [blame] | 3324 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3325 | if (result == NULL) |
| 3326 | return NULL; |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3327 | |
Victor Stinner | d1a9cc2 | 2011-10-13 22:51:17 +0200 | [diff] [blame] | 3328 | tmp = _PyObject_CallMethodId(result, &PyId_update, "O", other); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3329 | if (tmp == NULL) { |
| 3330 | Py_DECREF(result); |
| 3331 | return NULL; |
| 3332 | } |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3333 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3334 | Py_DECREF(tmp); |
| 3335 | return result; |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3336 | } |
| 3337 | |
| 3338 | static PyObject* |
| 3339 | dictviews_xor(PyObject* self, PyObject *other) |
| 3340 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3341 | PyObject *result = PySet_New(self); |
| 3342 | PyObject *tmp; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3343 | _Py_IDENTIFIER(symmetric_difference_update); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 3344 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3345 | if (result == NULL) |
| 3346 | return NULL; |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3347 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 3348 | tmp = _PyObject_CallMethodId(result, &PyId_symmetric_difference_update, "O", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3349 | other); |
| 3350 | if (tmp == NULL) { |
| 3351 | Py_DECREF(result); |
| 3352 | return NULL; |
| 3353 | } |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3354 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3355 | Py_DECREF(tmp); |
| 3356 | return result; |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3357 | } |
| 3358 | |
| 3359 | static PyNumberMethods dictviews_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3360 | 0, /*nb_add*/ |
| 3361 | (binaryfunc)dictviews_sub, /*nb_subtract*/ |
| 3362 | 0, /*nb_multiply*/ |
| 3363 | 0, /*nb_remainder*/ |
| 3364 | 0, /*nb_divmod*/ |
| 3365 | 0, /*nb_power*/ |
| 3366 | 0, /*nb_negative*/ |
| 3367 | 0, /*nb_positive*/ |
| 3368 | 0, /*nb_absolute*/ |
| 3369 | 0, /*nb_bool*/ |
| 3370 | 0, /*nb_invert*/ |
| 3371 | 0, /*nb_lshift*/ |
| 3372 | 0, /*nb_rshift*/ |
| 3373 | (binaryfunc)dictviews_and, /*nb_and*/ |
| 3374 | (binaryfunc)dictviews_xor, /*nb_xor*/ |
| 3375 | (binaryfunc)dictviews_or, /*nb_or*/ |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3376 | }; |
| 3377 | |
Daniel Stutzbach | 045b3ba | 2010-09-02 15:06:06 +0000 | [diff] [blame] | 3378 | static PyObject* |
| 3379 | dictviews_isdisjoint(PyObject *self, PyObject *other) |
| 3380 | { |
| 3381 | PyObject *it; |
| 3382 | PyObject *item = NULL; |
| 3383 | |
| 3384 | if (self == other) { |
| 3385 | if (dictview_len((dictviewobject *)self) == 0) |
| 3386 | Py_RETURN_TRUE; |
| 3387 | else |
| 3388 | Py_RETURN_FALSE; |
| 3389 | } |
| 3390 | |
| 3391 | /* Iterate over the shorter object (only if other is a set, |
| 3392 | * because PySequence_Contains may be expensive otherwise): */ |
| 3393 | if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) { |
| 3394 | Py_ssize_t len_self = dictview_len((dictviewobject *)self); |
| 3395 | Py_ssize_t len_other = PyObject_Size(other); |
| 3396 | if (len_other == -1) |
| 3397 | return NULL; |
| 3398 | |
| 3399 | if ((len_other > len_self)) { |
| 3400 | PyObject *tmp = other; |
| 3401 | other = self; |
| 3402 | self = tmp; |
| 3403 | } |
| 3404 | } |
| 3405 | |
| 3406 | it = PyObject_GetIter(other); |
| 3407 | if (it == NULL) |
| 3408 | return NULL; |
| 3409 | |
| 3410 | while ((item = PyIter_Next(it)) != NULL) { |
| 3411 | int contains = PySequence_Contains(self, item); |
| 3412 | Py_DECREF(item); |
| 3413 | if (contains == -1) { |
| 3414 | Py_DECREF(it); |
| 3415 | return NULL; |
| 3416 | } |
| 3417 | |
| 3418 | if (contains) { |
| 3419 | Py_DECREF(it); |
| 3420 | Py_RETURN_FALSE; |
| 3421 | } |
| 3422 | } |
| 3423 | Py_DECREF(it); |
| 3424 | if (PyErr_Occurred()) |
| 3425 | return NULL; /* PyIter_Next raised an exception. */ |
| 3426 | Py_RETURN_TRUE; |
| 3427 | } |
| 3428 | |
| 3429 | PyDoc_STRVAR(isdisjoint_doc, |
| 3430 | "Return True if the view and the given iterable have a null intersection."); |
| 3431 | |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3432 | static PyMethodDef dictkeys_methods[] = { |
Daniel Stutzbach | 045b3ba | 2010-09-02 15:06:06 +0000 | [diff] [blame] | 3433 | {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O, |
| 3434 | isdisjoint_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3435 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3436 | }; |
| 3437 | |
| 3438 | PyTypeObject PyDictKeys_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3439 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 3440 | "dict_keys", /* tp_name */ |
| 3441 | sizeof(dictviewobject), /* tp_basicsize */ |
| 3442 | 0, /* tp_itemsize */ |
| 3443 | /* methods */ |
| 3444 | (destructor)dictview_dealloc, /* tp_dealloc */ |
| 3445 | 0, /* tp_print */ |
| 3446 | 0, /* tp_getattr */ |
| 3447 | 0, /* tp_setattr */ |
| 3448 | 0, /* tp_reserved */ |
| 3449 | (reprfunc)dictview_repr, /* tp_repr */ |
| 3450 | &dictviews_as_number, /* tp_as_number */ |
| 3451 | &dictkeys_as_sequence, /* tp_as_sequence */ |
| 3452 | 0, /* tp_as_mapping */ |
| 3453 | 0, /* tp_hash */ |
| 3454 | 0, /* tp_call */ |
| 3455 | 0, /* tp_str */ |
| 3456 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3457 | 0, /* tp_setattro */ |
| 3458 | 0, /* tp_as_buffer */ |
| 3459 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 3460 | 0, /* tp_doc */ |
| 3461 | (traverseproc)dictview_traverse, /* tp_traverse */ |
| 3462 | 0, /* tp_clear */ |
| 3463 | dictview_richcompare, /* tp_richcompare */ |
| 3464 | 0, /* tp_weaklistoffset */ |
| 3465 | (getiterfunc)dictkeys_iter, /* tp_iter */ |
| 3466 | 0, /* tp_iternext */ |
| 3467 | dictkeys_methods, /* tp_methods */ |
| 3468 | 0, |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3469 | }; |
| 3470 | |
| 3471 | static PyObject * |
| 3472 | dictkeys_new(PyObject *dict) |
| 3473 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3474 | return dictview_new(dict, &PyDictKeys_Type); |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3475 | } |
| 3476 | |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 3477 | /*** dict_items ***/ |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3478 | |
| 3479 | static PyObject * |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 3480 | dictitems_iter(dictviewobject *dv) |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3481 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3482 | if (dv->dv_dict == NULL) { |
| 3483 | Py_RETURN_NONE; |
| 3484 | } |
| 3485 | return dictiter_new(dv->dv_dict, &PyDictIterItem_Type); |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 3486 | } |
| 3487 | |
| 3488 | static int |
| 3489 | dictitems_contains(dictviewobject *dv, PyObject *obj) |
| 3490 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3491 | PyObject *key, *value, *found; |
| 3492 | if (dv->dv_dict == NULL) |
| 3493 | return 0; |
| 3494 | if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2) |
| 3495 | return 0; |
| 3496 | key = PyTuple_GET_ITEM(obj, 0); |
| 3497 | value = PyTuple_GET_ITEM(obj, 1); |
| 3498 | found = PyDict_GetItem((PyObject *)dv->dv_dict, key); |
| 3499 | if (found == NULL) { |
| 3500 | if (PyErr_Occurred()) |
| 3501 | return -1; |
| 3502 | return 0; |
| 3503 | } |
| 3504 | return PyObject_RichCompareBool(value, found, Py_EQ); |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3505 | } |
| 3506 | |
Guido van Rossum | 83825ac | 2007-02-10 04:54:19 +0000 | [diff] [blame] | 3507 | static PySequenceMethods dictitems_as_sequence = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3508 | (lenfunc)dictview_len, /* sq_length */ |
| 3509 | 0, /* sq_concat */ |
| 3510 | 0, /* sq_repeat */ |
| 3511 | 0, /* sq_item */ |
| 3512 | 0, /* sq_slice */ |
| 3513 | 0, /* sq_ass_item */ |
| 3514 | 0, /* sq_ass_slice */ |
| 3515 | (objobjproc)dictitems_contains, /* sq_contains */ |
Guido van Rossum | 83825ac | 2007-02-10 04:54:19 +0000 | [diff] [blame] | 3516 | }; |
| 3517 | |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3518 | static PyMethodDef dictitems_methods[] = { |
Daniel Stutzbach | 045b3ba | 2010-09-02 15:06:06 +0000 | [diff] [blame] | 3519 | {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O, |
| 3520 | isdisjoint_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3521 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3522 | }; |
| 3523 | |
| 3524 | PyTypeObject PyDictItems_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3525 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 3526 | "dict_items", /* tp_name */ |
| 3527 | sizeof(dictviewobject), /* tp_basicsize */ |
| 3528 | 0, /* tp_itemsize */ |
| 3529 | /* methods */ |
| 3530 | (destructor)dictview_dealloc, /* tp_dealloc */ |
| 3531 | 0, /* tp_print */ |
| 3532 | 0, /* tp_getattr */ |
| 3533 | 0, /* tp_setattr */ |
| 3534 | 0, /* tp_reserved */ |
| 3535 | (reprfunc)dictview_repr, /* tp_repr */ |
| 3536 | &dictviews_as_number, /* tp_as_number */ |
| 3537 | &dictitems_as_sequence, /* tp_as_sequence */ |
| 3538 | 0, /* tp_as_mapping */ |
| 3539 | 0, /* tp_hash */ |
| 3540 | 0, /* tp_call */ |
| 3541 | 0, /* tp_str */ |
| 3542 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3543 | 0, /* tp_setattro */ |
| 3544 | 0, /* tp_as_buffer */ |
| 3545 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 3546 | 0, /* tp_doc */ |
| 3547 | (traverseproc)dictview_traverse, /* tp_traverse */ |
| 3548 | 0, /* tp_clear */ |
| 3549 | dictview_richcompare, /* tp_richcompare */ |
| 3550 | 0, /* tp_weaklistoffset */ |
| 3551 | (getiterfunc)dictitems_iter, /* tp_iter */ |
| 3552 | 0, /* tp_iternext */ |
| 3553 | dictitems_methods, /* tp_methods */ |
| 3554 | 0, |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3555 | }; |
| 3556 | |
| 3557 | static PyObject * |
| 3558 | dictitems_new(PyObject *dict) |
| 3559 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3560 | return dictview_new(dict, &PyDictItems_Type); |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3561 | } |
| 3562 | |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 3563 | /*** dict_values ***/ |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3564 | |
| 3565 | static PyObject * |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 3566 | dictvalues_iter(dictviewobject *dv) |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3567 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3568 | if (dv->dv_dict == NULL) { |
| 3569 | Py_RETURN_NONE; |
| 3570 | } |
| 3571 | return dictiter_new(dv->dv_dict, &PyDictIterValue_Type); |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3572 | } |
| 3573 | |
Guido van Rossum | 83825ac | 2007-02-10 04:54:19 +0000 | [diff] [blame] | 3574 | static PySequenceMethods dictvalues_as_sequence = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3575 | (lenfunc)dictview_len, /* sq_length */ |
| 3576 | 0, /* sq_concat */ |
| 3577 | 0, /* sq_repeat */ |
| 3578 | 0, /* sq_item */ |
| 3579 | 0, /* sq_slice */ |
| 3580 | 0, /* sq_ass_item */ |
| 3581 | 0, /* sq_ass_slice */ |
| 3582 | (objobjproc)0, /* sq_contains */ |
Guido van Rossum | 83825ac | 2007-02-10 04:54:19 +0000 | [diff] [blame] | 3583 | }; |
| 3584 | |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3585 | static PyMethodDef dictvalues_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3586 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3587 | }; |
| 3588 | |
| 3589 | PyTypeObject PyDictValues_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3590 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 3591 | "dict_values", /* tp_name */ |
| 3592 | sizeof(dictviewobject), /* tp_basicsize */ |
| 3593 | 0, /* tp_itemsize */ |
| 3594 | /* methods */ |
| 3595 | (destructor)dictview_dealloc, /* tp_dealloc */ |
| 3596 | 0, /* tp_print */ |
| 3597 | 0, /* tp_getattr */ |
| 3598 | 0, /* tp_setattr */ |
| 3599 | 0, /* tp_reserved */ |
| 3600 | (reprfunc)dictview_repr, /* tp_repr */ |
| 3601 | 0, /* tp_as_number */ |
| 3602 | &dictvalues_as_sequence, /* tp_as_sequence */ |
| 3603 | 0, /* tp_as_mapping */ |
| 3604 | 0, /* tp_hash */ |
| 3605 | 0, /* tp_call */ |
| 3606 | 0, /* tp_str */ |
| 3607 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3608 | 0, /* tp_setattro */ |
| 3609 | 0, /* tp_as_buffer */ |
| 3610 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 3611 | 0, /* tp_doc */ |
| 3612 | (traverseproc)dictview_traverse, /* tp_traverse */ |
| 3613 | 0, /* tp_clear */ |
| 3614 | 0, /* tp_richcompare */ |
| 3615 | 0, /* tp_weaklistoffset */ |
| 3616 | (getiterfunc)dictvalues_iter, /* tp_iter */ |
| 3617 | 0, /* tp_iternext */ |
| 3618 | dictvalues_methods, /* tp_methods */ |
| 3619 | 0, |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3620 | }; |
| 3621 | |
| 3622 | static PyObject * |
| 3623 | dictvalues_new(PyObject *dict) |
| 3624 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3625 | return dictview_new(dict, &PyDictValues_Type); |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3626 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 3627 | |
| 3628 | /* Returns NULL if cannot allocate a new PyDictKeysObject, |
| 3629 | but does not set an error */ |
| 3630 | PyDictKeysObject * |
| 3631 | _PyDict_NewKeysForClass(void) |
| 3632 | { |
| 3633 | PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE_SPLIT); |
| 3634 | if (keys == NULL) |
| 3635 | PyErr_Clear(); |
| 3636 | else |
| 3637 | keys->dk_lookup = lookdict_split; |
| 3638 | return keys; |
| 3639 | } |
| 3640 | |
| 3641 | #define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys) |
| 3642 | |
| 3643 | PyObject * |
| 3644 | PyObject_GenericGetDict(PyObject *obj, void *context) |
| 3645 | { |
| 3646 | PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj); |
| 3647 | if (dictptr == NULL) { |
| 3648 | PyErr_SetString(PyExc_AttributeError, |
| 3649 | "This object has no __dict__"); |
| 3650 | return NULL; |
| 3651 | } |
| 3652 | dict = *dictptr; |
| 3653 | if (dict == NULL) { |
| 3654 | PyTypeObject *tp = Py_TYPE(obj); |
| 3655 | if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) { |
| 3656 | DK_INCREF(CACHED_KEYS(tp)); |
| 3657 | *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp)); |
| 3658 | } |
| 3659 | else { |
| 3660 | *dictptr = dict = PyDict_New(); |
| 3661 | } |
| 3662 | } |
| 3663 | Py_XINCREF(dict); |
| 3664 | return dict; |
| 3665 | } |
| 3666 | |
| 3667 | int |
| 3668 | _PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr, |
| 3669 | PyObject *key, PyObject *value) |
| 3670 | { |
| 3671 | PyObject *dict; |
| 3672 | int res; |
| 3673 | PyDictKeysObject *cached; |
| 3674 | |
| 3675 | assert(dictptr != NULL); |
| 3676 | if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) { |
| 3677 | assert(dictptr != NULL); |
| 3678 | dict = *dictptr; |
| 3679 | if (dict == NULL) { |
| 3680 | DK_INCREF(cached); |
| 3681 | dict = new_dict_with_shared_keys(cached); |
| 3682 | if (dict == NULL) |
| 3683 | return -1; |
| 3684 | *dictptr = dict; |
| 3685 | } |
| 3686 | if (value == NULL) { |
| 3687 | res = PyDict_DelItem(dict, key); |
| 3688 | if (cached != ((PyDictObject *)dict)->ma_keys) { |
| 3689 | CACHED_KEYS(tp) = NULL; |
| 3690 | DK_DECREF(cached); |
| 3691 | } |
| 3692 | } else { |
| 3693 | res = PyDict_SetItem(dict, key, value); |
| 3694 | if (cached != ((PyDictObject *)dict)->ma_keys) { |
| 3695 | /* Either update tp->ht_cached_keys or delete it */ |
Benjamin Peterson | 53b9771 | 2012-04-23 11:50:47 -0400 | [diff] [blame] | 3696 | if (cached->dk_refcnt == 1 && PyDict_CheckExact(dict)) { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 3697 | CACHED_KEYS(tp) = make_keys_shared(dict); |
| 3698 | if (CACHED_KEYS(tp) == NULL) |
| 3699 | return -1; |
| 3700 | } else { |
| 3701 | CACHED_KEYS(tp) = NULL; |
| 3702 | } |
| 3703 | DK_DECREF(cached); |
| 3704 | } |
| 3705 | } |
| 3706 | } else { |
| 3707 | dict = *dictptr; |
| 3708 | if (dict == NULL) { |
| 3709 | dict = PyDict_New(); |
| 3710 | if (dict == NULL) |
| 3711 | return -1; |
| 3712 | *dictptr = dict; |
| 3713 | } |
| 3714 | if (value == NULL) { |
| 3715 | res = PyDict_DelItem(dict, key); |
| 3716 | } else { |
| 3717 | res = PyDict_SetItem(dict, key, value); |
| 3718 | } |
| 3719 | } |
| 3720 | return res; |
| 3721 | } |
| 3722 | |
| 3723 | void |
| 3724 | _PyDictKeys_DecRef(PyDictKeysObject *keys) |
| 3725 | { |
| 3726 | DK_DECREF(keys); |
| 3727 | } |
| 3728 | |
| 3729 | |
| 3730 | /* ARGSUSED */ |
| 3731 | static PyObject * |
| 3732 | dummy_repr(PyObject *op) |
| 3733 | { |
| 3734 | return PyUnicode_FromString("<dummy key>"); |
| 3735 | } |
| 3736 | |
| 3737 | /* ARGUSED */ |
| 3738 | static void |
| 3739 | dummy_dealloc(PyObject* ignore) |
| 3740 | { |
| 3741 | /* This should never get called, but we also don't want to SEGV if |
| 3742 | * we accidentally decref dummy-key out of existence. |
| 3743 | */ |
| 3744 | Py_FatalError("deallocating <dummy key>"); |
| 3745 | } |
| 3746 | |
| 3747 | static PyTypeObject PyDictDummy_Type = { |
| 3748 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 3749 | "<dummy key> type", |
| 3750 | 0, |
| 3751 | 0, |
| 3752 | dummy_dealloc, /*tp_dealloc*/ /*never called*/ |
| 3753 | 0, /*tp_print*/ |
| 3754 | 0, /*tp_getattr*/ |
| 3755 | 0, /*tp_setattr*/ |
| 3756 | 0, /*tp_reserved*/ |
| 3757 | dummy_repr, /*tp_repr*/ |
| 3758 | 0, /*tp_as_number*/ |
| 3759 | 0, /*tp_as_sequence*/ |
| 3760 | 0, /*tp_as_mapping*/ |
| 3761 | 0, /*tp_hash */ |
| 3762 | 0, /*tp_call */ |
| 3763 | 0, /*tp_str */ |
| 3764 | 0, /*tp_getattro */ |
| 3765 | 0, /*tp_setattro */ |
| 3766 | 0, /*tp_as_buffer */ |
| 3767 | Py_TPFLAGS_DEFAULT, /*tp_flags */ |
| 3768 | }; |
| 3769 | |
| 3770 | static PyObject _dummy_struct = { |
| 3771 | _PyObject_EXTRA_INIT |
| 3772 | 2, &PyDictDummy_Type |
| 3773 | }; |
| 3774 | |