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