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