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 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1710 | if (PyDict_CheckExact(d) && PyDict_CheckExact(seq)) { |
| 1711 | PyDictObject *mp = (PyDictObject *)d; |
| 1712 | PyObject *oldvalue; |
| 1713 | Py_ssize_t pos = 0; |
| 1714 | PyObject *key; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 1715 | Py_hash_t hash; |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 1716 | |
Petri Lehtinen | a94200e | 2011-10-24 21:12:58 +0300 | [diff] [blame] | 1717 | if (dictresize(mp, Py_SIZE(seq))) { |
| 1718 | Py_DECREF(d); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1719 | return NULL; |
Petri Lehtinen | a94200e | 2011-10-24 21:12:58 +0300 | [diff] [blame] | 1720 | } |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 1721 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1722 | while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) { |
Petri Lehtinen | a94200e | 2011-10-24 21:12:58 +0300 | [diff] [blame] | 1723 | if (insertdict(mp, key, hash, value)) { |
| 1724 | Py_DECREF(d); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1725 | return NULL; |
Petri Lehtinen | a94200e | 2011-10-24 21:12:58 +0300 | [diff] [blame] | 1726 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1727 | } |
| 1728 | return d; |
| 1729 | } |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 1730 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1731 | if (PyDict_CheckExact(d) && PyAnySet_CheckExact(seq)) { |
| 1732 | PyDictObject *mp = (PyDictObject *)d; |
| 1733 | Py_ssize_t pos = 0; |
| 1734 | PyObject *key; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 1735 | Py_hash_t hash; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1736 | |
Petri Lehtinen | a94200e | 2011-10-24 21:12:58 +0300 | [diff] [blame] | 1737 | if (dictresize(mp, PySet_GET_SIZE(seq))) { |
| 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 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1741 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1742 | while (_PySet_NextEntry(seq, &pos, &key, &hash)) { |
Petri Lehtinen | a94200e | 2011-10-24 21:12:58 +0300 | [diff] [blame] | 1743 | if (insertdict(mp, key, hash, value)) { |
| 1744 | Py_DECREF(d); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1745 | return NULL; |
Petri Lehtinen | a94200e | 2011-10-24 21:12:58 +0300 | [diff] [blame] | 1746 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1747 | } |
| 1748 | return d; |
| 1749 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1750 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1751 | it = PyObject_GetIter(seq); |
| 1752 | if (it == NULL){ |
| 1753 | Py_DECREF(d); |
| 1754 | return NULL; |
| 1755 | } |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 1756 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1757 | if (PyDict_CheckExact(d)) { |
| 1758 | while ((key = PyIter_Next(it)) != NULL) { |
| 1759 | status = PyDict_SetItem(d, key, value); |
| 1760 | Py_DECREF(key); |
| 1761 | if (status < 0) |
| 1762 | goto Fail; |
| 1763 | } |
| 1764 | } else { |
| 1765 | while ((key = PyIter_Next(it)) != NULL) { |
| 1766 | status = PyObject_SetItem(d, key, value); |
| 1767 | Py_DECREF(key); |
| 1768 | if (status < 0) |
| 1769 | goto Fail; |
| 1770 | } |
| 1771 | } |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 1772 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1773 | if (PyErr_Occurred()) |
| 1774 | goto Fail; |
| 1775 | Py_DECREF(it); |
| 1776 | return d; |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 1777 | |
| 1778 | Fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1779 | Py_DECREF(it); |
| 1780 | Py_DECREF(d); |
| 1781 | return NULL; |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 1782 | } |
| 1783 | |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 1784 | static int |
| 1785 | dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname) |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 1786 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1787 | PyObject *arg = NULL; |
| 1788 | int result = 0; |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 1789 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1790 | if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg)) |
| 1791 | result = -1; |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 1792 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1793 | else if (arg != NULL) { |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 1794 | _Py_IDENTIFIER(keys); |
| 1795 | if (_PyObject_HasAttrId(arg, &PyId_keys)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1796 | result = PyDict_Merge(self, arg, 1); |
| 1797 | else |
| 1798 | result = PyDict_MergeFromSeq2(self, arg, 1); |
| 1799 | } |
| 1800 | if (result == 0 && kwds != NULL) { |
| 1801 | if (PyArg_ValidateKeywordArguments(kwds)) |
| 1802 | result = PyDict_Merge(self, kwds, 1); |
| 1803 | else |
| 1804 | result = -1; |
| 1805 | } |
| 1806 | return result; |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 1807 | } |
| 1808 | |
| 1809 | static PyObject * |
| 1810 | dict_update(PyObject *self, PyObject *args, PyObject *kwds) |
| 1811 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1812 | if (dict_update_common(self, args, kwds, "update") != -1) |
| 1813 | Py_RETURN_NONE; |
| 1814 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1815 | } |
| 1816 | |
Guido van Rossum | 05ac6de | 2001-08-10 20:28:28 +0000 | [diff] [blame] | 1817 | /* Update unconditionally replaces existing items. |
| 1818 | Merge has a 3rd argument 'override'; if set, it acts like Update, |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1819 | otherwise it leaves existing items unchanged. |
| 1820 | |
| 1821 | PyDict_{Update,Merge} update/merge from a mapping object. |
| 1822 | |
Tim Peters | f582b82 | 2001-12-11 18:51:08 +0000 | [diff] [blame] | 1823 | PyDict_MergeFromSeq2 updates/merges from any iterable object |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1824 | producing iterable objects of length 2. |
| 1825 | */ |
| 1826 | |
Tim Peters | f582b82 | 2001-12-11 18:51:08 +0000 | [diff] [blame] | 1827 | int |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1828 | PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override) |
| 1829 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1830 | PyObject *it; /* iter(seq2) */ |
| 1831 | Py_ssize_t i; /* index into seq2 of current element */ |
| 1832 | PyObject *item; /* seq2[i] */ |
| 1833 | PyObject *fast; /* item as a 2-tuple or 2-list */ |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1834 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1835 | assert(d != NULL); |
| 1836 | assert(PyDict_Check(d)); |
| 1837 | assert(seq2 != NULL); |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1838 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1839 | it = PyObject_GetIter(seq2); |
| 1840 | if (it == NULL) |
| 1841 | return -1; |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1842 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1843 | for (i = 0; ; ++i) { |
| 1844 | PyObject *key, *value; |
| 1845 | Py_ssize_t n; |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1846 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1847 | fast = NULL; |
| 1848 | item = PyIter_Next(it); |
| 1849 | if (item == NULL) { |
| 1850 | if (PyErr_Occurred()) |
| 1851 | goto Fail; |
| 1852 | break; |
| 1853 | } |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1854 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1855 | /* Convert item to sequence, and verify length 2. */ |
| 1856 | fast = PySequence_Fast(item, ""); |
| 1857 | if (fast == NULL) { |
| 1858 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 1859 | PyErr_Format(PyExc_TypeError, |
| 1860 | "cannot convert dictionary update " |
| 1861 | "sequence element #%zd to a sequence", |
| 1862 | i); |
| 1863 | goto Fail; |
| 1864 | } |
| 1865 | n = PySequence_Fast_GET_SIZE(fast); |
| 1866 | if (n != 2) { |
| 1867 | PyErr_Format(PyExc_ValueError, |
| 1868 | "dictionary update sequence element #%zd " |
| 1869 | "has length %zd; 2 is required", |
| 1870 | i, n); |
| 1871 | goto Fail; |
| 1872 | } |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1873 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1874 | /* Update/merge with this (key, value) pair. */ |
| 1875 | key = PySequence_Fast_GET_ITEM(fast, 0); |
| 1876 | value = PySequence_Fast_GET_ITEM(fast, 1); |
| 1877 | if (override || PyDict_GetItem(d, key) == NULL) { |
| 1878 | int status = PyDict_SetItem(d, key, value); |
| 1879 | if (status < 0) |
| 1880 | goto Fail; |
| 1881 | } |
| 1882 | Py_DECREF(fast); |
| 1883 | Py_DECREF(item); |
| 1884 | } |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1885 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1886 | i = 0; |
| 1887 | goto Return; |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1888 | Fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1889 | Py_XDECREF(item); |
| 1890 | Py_XDECREF(fast); |
| 1891 | i = -1; |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1892 | Return: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1893 | Py_DECREF(it); |
| 1894 | return Py_SAFE_DOWNCAST(i, Py_ssize_t, int); |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1895 | } |
| 1896 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1897 | int |
| 1898 | PyDict_Update(PyObject *a, PyObject *b) |
| 1899 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1900 | return PyDict_Merge(a, b, 1); |
Guido van Rossum | 05ac6de | 2001-08-10 20:28:28 +0000 | [diff] [blame] | 1901 | } |
| 1902 | |
| 1903 | int |
| 1904 | PyDict_Merge(PyObject *a, PyObject *b, int override) |
| 1905 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1906 | register PyDictObject *mp, *other; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1907 | register Py_ssize_t i, n; |
| 1908 | PyDictKeyEntry *entry; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1909 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1910 | /* We accept for the argument either a concrete dictionary object, |
| 1911 | * or an abstract "mapping" object. For the former, we can do |
| 1912 | * things quite efficiently. For the latter, we only require that |
| 1913 | * PyMapping_Keys() and PyObject_GetItem() be supported. |
| 1914 | */ |
| 1915 | if (a == NULL || !PyDict_Check(a) || b == NULL) { |
| 1916 | PyErr_BadInternalCall(); |
| 1917 | return -1; |
| 1918 | } |
| 1919 | mp = (PyDictObject*)a; |
| 1920 | if (PyDict_Check(b)) { |
| 1921 | other = (PyDictObject*)b; |
| 1922 | if (other == mp || other->ma_used == 0) |
| 1923 | /* a.update(a) or a.update({}); nothing to do */ |
| 1924 | return 0; |
| 1925 | if (mp->ma_used == 0) |
| 1926 | /* Since the target dict is empty, PyDict_GetItem() |
| 1927 | * always returns NULL. Setting override to 1 |
| 1928 | * skips the unnecessary test. |
| 1929 | */ |
| 1930 | override = 1; |
| 1931 | /* Do one big resize at the start, rather than |
| 1932 | * incrementally resizing as we insert new items. Expect |
| 1933 | * that there will be no (or few) overlapping keys. |
| 1934 | */ |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1935 | if (mp->ma_keys->dk_usable * 3 < other->ma_used * 2) |
| 1936 | if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1937 | return -1; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1938 | for (i = 0, n = DK_SIZE(other->ma_keys); i < n; i++) { |
| 1939 | PyObject *value; |
| 1940 | entry = &other->ma_keys->dk_entries[i]; |
| 1941 | if (other->ma_values) |
| 1942 | value = other->ma_values[i]; |
| 1943 | else |
| 1944 | value = entry->me_value; |
| 1945 | |
| 1946 | if (value != NULL && |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1947 | (override || |
| 1948 | PyDict_GetItem(a, entry->me_key) == NULL)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1949 | if (insertdict(mp, entry->me_key, |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 1950 | entry->me_hash, |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 1951 | value) != 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1952 | return -1; |
| 1953 | } |
| 1954 | } |
| 1955 | } |
| 1956 | else { |
| 1957 | /* Do it the generic, slower way */ |
| 1958 | PyObject *keys = PyMapping_Keys(b); |
| 1959 | PyObject *iter; |
| 1960 | PyObject *key, *value; |
| 1961 | int status; |
Barry Warsaw | 66a0d1d | 2001-06-26 20:08:32 +0000 | [diff] [blame] | 1962 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1963 | if (keys == NULL) |
| 1964 | /* Docstring says this is equivalent to E.keys() so |
| 1965 | * if E doesn't have a .keys() method we want |
| 1966 | * AttributeError to percolate up. Might as well |
| 1967 | * do the same for any other error. |
| 1968 | */ |
| 1969 | return -1; |
Barry Warsaw | 66a0d1d | 2001-06-26 20:08:32 +0000 | [diff] [blame] | 1970 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1971 | iter = PyObject_GetIter(keys); |
| 1972 | Py_DECREF(keys); |
| 1973 | if (iter == NULL) |
| 1974 | return -1; |
Barry Warsaw | 66a0d1d | 2001-06-26 20:08:32 +0000 | [diff] [blame] | 1975 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1976 | for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) { |
| 1977 | if (!override && PyDict_GetItem(a, key) != NULL) { |
| 1978 | Py_DECREF(key); |
| 1979 | continue; |
| 1980 | } |
| 1981 | value = PyObject_GetItem(b, key); |
| 1982 | if (value == NULL) { |
| 1983 | Py_DECREF(iter); |
| 1984 | Py_DECREF(key); |
| 1985 | return -1; |
| 1986 | } |
| 1987 | status = PyDict_SetItem(a, key, value); |
| 1988 | Py_DECREF(key); |
| 1989 | Py_DECREF(value); |
| 1990 | if (status < 0) { |
| 1991 | Py_DECREF(iter); |
| 1992 | return -1; |
| 1993 | } |
| 1994 | } |
| 1995 | Py_DECREF(iter); |
| 1996 | if (PyErr_Occurred()) |
| 1997 | /* Iterator completed, via error */ |
| 1998 | return -1; |
| 1999 | } |
| 2000 | return 0; |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 2001 | } |
| 2002 | |
| 2003 | static PyObject * |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2004 | dict_copy(register PyDictObject *mp) |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 2005 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2006 | return PyDict_Copy((PyObject*)mp); |
Jeremy Hylton | a12c7a7 | 2000-03-30 22:27:31 +0000 | [diff] [blame] | 2007 | } |
| 2008 | |
| 2009 | PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 2010 | PyDict_Copy(PyObject *o) |
Jeremy Hylton | a12c7a7 | 2000-03-30 22:27:31 +0000 | [diff] [blame] | 2011 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2012 | PyObject *copy; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2013 | PyDictObject *mp; |
| 2014 | Py_ssize_t i, n; |
Jeremy Hylton | a12c7a7 | 2000-03-30 22:27:31 +0000 | [diff] [blame] | 2015 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2016 | if (o == NULL || !PyDict_Check(o)) { |
| 2017 | PyErr_BadInternalCall(); |
| 2018 | return NULL; |
| 2019 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2020 | mp = (PyDictObject *)o; |
| 2021 | if (_PyDict_HasSplitTable(mp)) { |
| 2022 | PyDictObject *split_copy; |
| 2023 | PyObject **newvalues = new_values(DK_SIZE(mp->ma_keys)); |
| 2024 | if (newvalues == NULL) |
| 2025 | return PyErr_NoMemory(); |
| 2026 | split_copy = PyObject_GC_New(PyDictObject, &PyDict_Type); |
| 2027 | if (split_copy == NULL) { |
| 2028 | free_values(newvalues); |
| 2029 | return NULL; |
| 2030 | } |
| 2031 | split_copy->ma_values = newvalues; |
| 2032 | split_copy->ma_keys = mp->ma_keys; |
| 2033 | split_copy->ma_used = mp->ma_used; |
| 2034 | DK_INCREF(mp->ma_keys); |
| 2035 | for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) { |
| 2036 | PyObject *value = mp->ma_values[i]; |
| 2037 | Py_XINCREF(value); |
| 2038 | split_copy->ma_values[i] = value; |
| 2039 | } |
Benjamin Peterson | 7ce67e4 | 2012-04-24 10:32:57 -0400 | [diff] [blame] | 2040 | if (_PyObject_GC_IS_TRACKED(mp)) |
| 2041 | _PyObject_GC_TRACK(split_copy); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2042 | return (PyObject *)split_copy; |
| 2043 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2044 | copy = PyDict_New(); |
| 2045 | if (copy == NULL) |
| 2046 | return NULL; |
| 2047 | if (PyDict_Merge(copy, o, 1) == 0) |
| 2048 | return copy; |
| 2049 | Py_DECREF(copy); |
| 2050 | return NULL; |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 2051 | } |
| 2052 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2053 | Py_ssize_t |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 2054 | PyDict_Size(PyObject *mp) |
Guido van Rossum | 4199fac | 1993-11-05 10:18:44 +0000 | [diff] [blame] | 2055 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2056 | if (mp == NULL || !PyDict_Check(mp)) { |
| 2057 | PyErr_BadInternalCall(); |
| 2058 | return -1; |
| 2059 | } |
| 2060 | return ((PyDictObject *)mp)->ma_used; |
Guido van Rossum | 4199fac | 1993-11-05 10:18:44 +0000 | [diff] [blame] | 2061 | } |
| 2062 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2063 | PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 2064 | PyDict_Keys(PyObject *mp) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2065 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2066 | if (mp == NULL || !PyDict_Check(mp)) { |
| 2067 | PyErr_BadInternalCall(); |
| 2068 | return NULL; |
| 2069 | } |
| 2070 | return dict_keys((PyDictObject *)mp); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2071 | } |
| 2072 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2073 | PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 2074 | PyDict_Values(PyObject *mp) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2075 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2076 | if (mp == NULL || !PyDict_Check(mp)) { |
| 2077 | PyErr_BadInternalCall(); |
| 2078 | return NULL; |
| 2079 | } |
| 2080 | return dict_values((PyDictObject *)mp); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2081 | } |
| 2082 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2083 | PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 2084 | PyDict_Items(PyObject *mp) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2085 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2086 | if (mp == NULL || !PyDict_Check(mp)) { |
| 2087 | PyErr_BadInternalCall(); |
| 2088 | return NULL; |
| 2089 | } |
| 2090 | return dict_items((PyDictObject *)mp); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2091 | } |
| 2092 | |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 2093 | /* Return 1 if dicts equal, 0 if not, -1 if error. |
| 2094 | * Gets out as soon as any difference is detected. |
| 2095 | * Uses only Py_EQ comparison. |
| 2096 | */ |
| 2097 | static int |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2098 | dict_equal(PyDictObject *a, PyDictObject *b) |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 2099 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2100 | Py_ssize_t i; |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 2101 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2102 | if (a->ma_used != b->ma_used) |
| 2103 | /* can't be equal if # of entries differ */ |
| 2104 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2105 | /* Same # of entries -- check all of 'em. Exit early on any diff. */ |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2106 | for (i = 0; i < DK_SIZE(a->ma_keys); i++) { |
| 2107 | PyDictKeyEntry *ep = &a->ma_keys->dk_entries[i]; |
| 2108 | PyObject *aval; |
| 2109 | if (a->ma_values) |
| 2110 | aval = a->ma_values[i]; |
| 2111 | else |
| 2112 | aval = ep->me_value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2113 | if (aval != NULL) { |
| 2114 | int cmp; |
| 2115 | PyObject *bval; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2116 | PyObject *key = ep->me_key; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2117 | /* temporarily bump aval's refcount to ensure it stays |
| 2118 | alive until we're done with it */ |
| 2119 | Py_INCREF(aval); |
| 2120 | /* ditto for key */ |
| 2121 | Py_INCREF(key); |
| 2122 | bval = PyDict_GetItemWithError((PyObject *)b, key); |
| 2123 | Py_DECREF(key); |
| 2124 | if (bval == NULL) { |
| 2125 | Py_DECREF(aval); |
| 2126 | if (PyErr_Occurred()) |
| 2127 | return -1; |
| 2128 | return 0; |
| 2129 | } |
| 2130 | cmp = PyObject_RichCompareBool(aval, bval, Py_EQ); |
| 2131 | Py_DECREF(aval); |
| 2132 | if (cmp <= 0) /* error or not equal */ |
| 2133 | return cmp; |
| 2134 | } |
| 2135 | } |
| 2136 | return 1; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2137 | } |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 2138 | |
| 2139 | static PyObject * |
| 2140 | dict_richcompare(PyObject *v, PyObject *w, int op) |
| 2141 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2142 | int cmp; |
| 2143 | PyObject *res; |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 2144 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2145 | if (!PyDict_Check(v) || !PyDict_Check(w)) { |
| 2146 | res = Py_NotImplemented; |
| 2147 | } |
| 2148 | else if (op == Py_EQ || op == Py_NE) { |
| 2149 | cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w); |
| 2150 | if (cmp < 0) |
| 2151 | return NULL; |
| 2152 | res = (cmp == (op == Py_EQ)) ? Py_True : Py_False; |
| 2153 | } |
| 2154 | else |
| 2155 | res = Py_NotImplemented; |
| 2156 | Py_INCREF(res); |
| 2157 | return res; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2158 | } |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 2159 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2160 | static PyObject * |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2161 | dict_contains(register PyDictObject *mp, PyObject *key) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2162 | { |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2163 | Py_hash_t hash; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2164 | PyDictKeyEntry *ep; |
| 2165 | PyObject **value_addr; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2166 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2167 | if (!PyUnicode_CheckExact(key) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2168 | (hash = ((PyASCIIObject *) key)->hash) == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2169 | hash = PyObject_Hash(key); |
| 2170 | if (hash == -1) |
| 2171 | return NULL; |
| 2172 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2173 | ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2174 | if (ep == NULL) |
| 2175 | return NULL; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2176 | return PyBool_FromLong(*value_addr != NULL); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2177 | } |
| 2178 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2179 | static PyObject * |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2180 | dict_get(register PyDictObject *mp, PyObject *args) |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 2181 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2182 | PyObject *key; |
| 2183 | PyObject *failobj = Py_None; |
| 2184 | PyObject *val = NULL; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2185 | Py_hash_t hash; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2186 | PyDictKeyEntry *ep; |
| 2187 | PyObject **value_addr; |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 2188 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2189 | if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj)) |
| 2190 | return NULL; |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 2191 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2192 | if (!PyUnicode_CheckExact(key) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2193 | (hash = ((PyASCIIObject *) key)->hash) == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2194 | hash = PyObject_Hash(key); |
| 2195 | if (hash == -1) |
| 2196 | return NULL; |
| 2197 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2198 | ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2199 | if (ep == NULL) |
| 2200 | return NULL; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2201 | val = *value_addr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2202 | if (val == NULL) |
| 2203 | val = failobj; |
| 2204 | Py_INCREF(val); |
| 2205 | return val; |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 2206 | } |
| 2207 | |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 2208 | static PyObject * |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2209 | dict_setdefault(register PyDictObject *mp, PyObject *args) |
Guido van Rossum | 164452c | 2000-08-08 16:12:54 +0000 | [diff] [blame] | 2210 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2211 | PyObject *key; |
| 2212 | PyObject *failobj = Py_None; |
| 2213 | PyObject *val = NULL; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2214 | Py_hash_t hash; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2215 | PyDictKeyEntry *ep; |
| 2216 | PyObject **value_addr; |
Guido van Rossum | 164452c | 2000-08-08 16:12:54 +0000 | [diff] [blame] | 2217 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2218 | if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj)) |
| 2219 | return NULL; |
Guido van Rossum | 164452c | 2000-08-08 16:12:54 +0000 | [diff] [blame] | 2220 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2221 | if (!PyUnicode_CheckExact(key) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2222 | (hash = ((PyASCIIObject *) key)->hash) == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2223 | hash = PyObject_Hash(key); |
| 2224 | if (hash == -1) |
| 2225 | return NULL; |
| 2226 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2227 | ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2228 | if (ep == NULL) |
| 2229 | return NULL; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2230 | val = *value_addr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2231 | if (val == NULL) { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2232 | Py_INCREF(failobj); |
| 2233 | Py_INCREF(key); |
| 2234 | if (mp->ma_keys->dk_usable <= 0) { |
| 2235 | /* Need to resize. */ |
| 2236 | if (insertion_resize(mp) < 0) |
| 2237 | return NULL; |
| 2238 | ep = find_empty_slot(mp, key, hash, &value_addr); |
| 2239 | } |
Benjamin Peterson | 7ce67e4 | 2012-04-24 10:32:57 -0400 | [diff] [blame] | 2240 | MAINTAIN_TRACKING(mp, key, failobj); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2241 | ep->me_key = key; |
| 2242 | ep->me_hash = hash; |
| 2243 | *value_addr = failobj; |
| 2244 | val = failobj; |
| 2245 | mp->ma_keys->dk_usable--; |
| 2246 | mp->ma_used++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2247 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2248 | Py_INCREF(val); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2249 | return val; |
Guido van Rossum | 164452c | 2000-08-08 16:12:54 +0000 | [diff] [blame] | 2250 | } |
| 2251 | |
| 2252 | |
| 2253 | static PyObject * |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2254 | dict_clear(register PyDictObject *mp) |
Guido van Rossum | fb8f1ca | 1997-03-21 21:55:12 +0000 | [diff] [blame] | 2255 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2256 | PyDict_Clear((PyObject *)mp); |
| 2257 | Py_RETURN_NONE; |
Guido van Rossum | fb8f1ca | 1997-03-21 21:55:12 +0000 | [diff] [blame] | 2258 | } |
| 2259 | |
Guido van Rossum | ba6ab84 | 2000-12-12 22:02:18 +0000 | [diff] [blame] | 2260 | static PyObject * |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2261 | dict_pop(PyDictObject *mp, PyObject *args) |
Guido van Rossum | e027d98 | 2002-04-12 15:11:59 +0000 | [diff] [blame] | 2262 | { |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2263 | Py_hash_t hash; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2264 | PyObject *old_value, *old_key; |
| 2265 | PyObject *key, *deflt = NULL; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2266 | PyDictKeyEntry *ep; |
| 2267 | PyObject **value_addr; |
Guido van Rossum | e027d98 | 2002-04-12 15:11:59 +0000 | [diff] [blame] | 2268 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2269 | if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt)) |
| 2270 | return NULL; |
| 2271 | if (mp->ma_used == 0) { |
| 2272 | if (deflt) { |
| 2273 | Py_INCREF(deflt); |
| 2274 | return deflt; |
| 2275 | } |
Raymond Hettinger | dd42154 | 2010-10-30 08:10:29 +0000 | [diff] [blame] | 2276 | set_key_error(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2277 | return NULL; |
| 2278 | } |
| 2279 | if (!PyUnicode_CheckExact(key) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2280 | (hash = ((PyASCIIObject *) key)->hash) == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2281 | hash = PyObject_Hash(key); |
| 2282 | if (hash == -1) |
| 2283 | return NULL; |
| 2284 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2285 | ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2286 | if (ep == NULL) |
| 2287 | return NULL; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2288 | old_value = *value_addr; |
| 2289 | if (old_value == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2290 | if (deflt) { |
| 2291 | Py_INCREF(deflt); |
| 2292 | return deflt; |
| 2293 | } |
| 2294 | set_key_error(key); |
| 2295 | return NULL; |
| 2296 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2297 | *value_addr = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2298 | mp->ma_used--; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2299 | if (!_PyDict_HasSplitTable(mp)) { |
| 2300 | ENSURE_ALLOWS_DELETIONS(mp); |
| 2301 | old_key = ep->me_key; |
| 2302 | Py_INCREF(dummy); |
| 2303 | ep->me_key = dummy; |
| 2304 | Py_DECREF(old_key); |
| 2305 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2306 | return old_value; |
Guido van Rossum | e027d98 | 2002-04-12 15:11:59 +0000 | [diff] [blame] | 2307 | } |
| 2308 | |
| 2309 | static PyObject * |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2310 | dict_popitem(PyDictObject *mp) |
Guido van Rossum | ba6ab84 | 2000-12-12 22:02:18 +0000 | [diff] [blame] | 2311 | { |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2312 | Py_hash_t i = 0; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2313 | PyDictKeyEntry *ep; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2314 | PyObject *res; |
Guido van Rossum | ba6ab84 | 2000-12-12 22:02:18 +0000 | [diff] [blame] | 2315 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2316 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2317 | /* Allocate the result tuple before checking the size. Believe it |
| 2318 | * or not, this allocation could trigger a garbage collection which |
| 2319 | * could empty the dict, so if we checked the size first and that |
| 2320 | * happened, the result would be an infinite loop (searching for an |
| 2321 | * entry that no longer exists). Note that the usual popitem() |
| 2322 | * idiom is "while d: k, v = d.popitem()". so needing to throw the |
| 2323 | * tuple away if the dict *is* empty isn't a significant |
| 2324 | * inefficiency -- possible, but unlikely in practice. |
| 2325 | */ |
| 2326 | res = PyTuple_New(2); |
| 2327 | if (res == NULL) |
| 2328 | return NULL; |
| 2329 | if (mp->ma_used == 0) { |
| 2330 | Py_DECREF(res); |
| 2331 | PyErr_SetString(PyExc_KeyError, |
| 2332 | "popitem(): dictionary is empty"); |
| 2333 | return NULL; |
| 2334 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2335 | /* Convert split table to combined table */ |
| 2336 | if (mp->ma_keys->dk_lookup == lookdict_split) { |
| 2337 | if (dictresize(mp, DK_SIZE(mp->ma_keys))) { |
| 2338 | Py_DECREF(res); |
| 2339 | return NULL; |
| 2340 | } |
| 2341 | } |
| 2342 | ENSURE_ALLOWS_DELETIONS(mp); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2343 | /* Set ep to "the first" dict entry with a value. We abuse the hash |
| 2344 | * field of slot 0 to hold a search finger: |
| 2345 | * If slot 0 has a value, use slot 0. |
| 2346 | * Else slot 0 is being used to hold a search finger, |
| 2347 | * and we use its hash value as the first index to look. |
| 2348 | */ |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2349 | ep = &mp->ma_keys->dk_entries[0]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2350 | if (ep->me_value == NULL) { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2351 | Py_ssize_t mask = DK_MASK(mp->ma_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2352 | i = ep->me_hash; |
| 2353 | /* The hash field may be a real hash value, or it may be a |
| 2354 | * legit search finger, or it may be a once-legit search |
| 2355 | * finger that's out of bounds now because it wrapped around |
| 2356 | * or the table shrunk -- simply make sure it's in bounds now. |
| 2357 | */ |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2358 | if (i > mask || i < 1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2359 | i = 1; /* skip slot 0 */ |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2360 | while ((ep = &mp->ma_keys->dk_entries[i])->me_value == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2361 | i++; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2362 | if (i > mask) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2363 | i = 1; |
| 2364 | } |
| 2365 | } |
| 2366 | PyTuple_SET_ITEM(res, 0, ep->me_key); |
| 2367 | PyTuple_SET_ITEM(res, 1, ep->me_value); |
| 2368 | Py_INCREF(dummy); |
| 2369 | ep->me_key = dummy; |
| 2370 | ep->me_value = NULL; |
| 2371 | mp->ma_used--; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2372 | assert(mp->ma_keys->dk_entries[0].me_value == NULL); |
| 2373 | 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] | 2374 | return res; |
Guido van Rossum | ba6ab84 | 2000-12-12 22:02:18 +0000 | [diff] [blame] | 2375 | } |
| 2376 | |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 2377 | static int |
| 2378 | dict_traverse(PyObject *op, visitproc visit, void *arg) |
| 2379 | { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2380 | Py_ssize_t i, n; |
| 2381 | PyDictObject *mp = (PyDictObject *)op; |
| 2382 | if (mp->ma_keys->dk_lookup == lookdict) { |
| 2383 | for (i = 0; i < DK_SIZE(mp->ma_keys); i++) { |
| 2384 | if (mp->ma_keys->dk_entries[i].me_value != NULL) { |
| 2385 | Py_VISIT(mp->ma_keys->dk_entries[i].me_value); |
| 2386 | Py_VISIT(mp->ma_keys->dk_entries[i].me_key); |
| 2387 | } |
| 2388 | } |
| 2389 | } else { |
| 2390 | if (mp->ma_values != NULL) { |
| 2391 | for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) { |
| 2392 | Py_VISIT(mp->ma_values[i]); |
| 2393 | } |
| 2394 | } |
| 2395 | else { |
| 2396 | for (i = 0, n = DK_SIZE(mp->ma_keys); i < n; i++) { |
| 2397 | Py_VISIT(mp->ma_keys->dk_entries[i].me_value); |
| 2398 | } |
| 2399 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2400 | } |
| 2401 | return 0; |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 2402 | } |
| 2403 | |
| 2404 | static int |
| 2405 | dict_tp_clear(PyObject *op) |
| 2406 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2407 | PyDict_Clear(op); |
| 2408 | return 0; |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 2409 | } |
| 2410 | |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2411 | static PyObject *dictiter_new(PyDictObject *, PyTypeObject *); |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 2412 | |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 2413 | static PyObject * |
| 2414 | dict_sizeof(PyDictObject *mp) |
| 2415 | { |
Martin v. Loewis | 4f2f3b6 | 2012-04-24 19:13:57 +0200 | [diff] [blame] | 2416 | Py_ssize_t size, res; |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 2417 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2418 | size = DK_SIZE(mp->ma_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2419 | res = sizeof(PyDictObject); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2420 | if (mp->ma_values) |
| 2421 | res += size * sizeof(PyObject*); |
Martin v. Loewis | 4f2f3b6 | 2012-04-24 19:13:57 +0200 | [diff] [blame] | 2422 | /* If the dictionary is split, the keys portion is accounted-for |
| 2423 | in the type object. */ |
| 2424 | if (mp->ma_keys->dk_refcnt == 1) |
| 2425 | res += sizeof(PyDictKeysObject) + (size-1) * sizeof(PyDictKeyEntry); |
| 2426 | return PyLong_FromSsize_t(res); |
| 2427 | } |
| 2428 | |
| 2429 | Py_ssize_t |
| 2430 | _PyDict_KeysSize(PyDictKeysObject *keys) |
| 2431 | { |
| 2432 | return sizeof(PyDictKeysObject) + (DK_SIZE(keys)-1) * sizeof(PyDictKeyEntry); |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 2433 | } |
| 2434 | |
Raymond Hettinger | 8f5cdaa | 2003-12-13 11:26:12 +0000 | [diff] [blame] | 2435 | PyDoc_STRVAR(contains__doc__, |
| 2436 | "D.__contains__(k) -> True if D has a key k, else False"); |
| 2437 | |
| 2438 | PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]"); |
| 2439 | |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 2440 | PyDoc_STRVAR(sizeof__doc__, |
| 2441 | "D.__sizeof__() -> size of D in memory, in bytes"); |
| 2442 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2443 | PyDoc_STRVAR(get__doc__, |
Guido van Rossum | efae886 | 2002-09-04 11:29:45 +0000 | [diff] [blame] | 2444 | "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] | 2445 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2446 | PyDoc_STRVAR(setdefault_doc__, |
Guido van Rossum | efae886 | 2002-09-04 11:29:45 +0000 | [diff] [blame] | 2447 | "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] | 2448 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2449 | PyDoc_STRVAR(pop__doc__, |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 2450 | "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] | 2451 | 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] | 2452 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2453 | PyDoc_STRVAR(popitem__doc__, |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2454 | "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] | 2455 | 2-tuple; but raise KeyError if D is empty."); |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2456 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2457 | PyDoc_STRVAR(update__doc__, |
Georg Brandl | ac0675c | 2011-12-18 19:30:55 +0100 | [diff] [blame] | 2458 | "D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n" |
| 2459 | "If E present and has a .keys() method, does: for k in E: D[k] = E[k]\n\ |
| 2460 | 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] | 2461 | 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] | 2462 | |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 2463 | PyDoc_STRVAR(fromkeys__doc__, |
| 2464 | "dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\ |
| 2465 | v defaults to None."); |
| 2466 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2467 | PyDoc_STRVAR(clear__doc__, |
| 2468 | "D.clear() -> None. Remove all items from D."); |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2469 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2470 | PyDoc_STRVAR(copy__doc__, |
| 2471 | "D.copy() -> a shallow copy of D"); |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2472 | |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 2473 | /* Forward */ |
| 2474 | static PyObject *dictkeys_new(PyObject *); |
| 2475 | static PyObject *dictitems_new(PyObject *); |
| 2476 | static PyObject *dictvalues_new(PyObject *); |
| 2477 | |
Guido van Rossum | 45c85d1 | 2007-07-27 16:31:40 +0000 | [diff] [blame] | 2478 | PyDoc_STRVAR(keys__doc__, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2479 | "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] | 2480 | PyDoc_STRVAR(items__doc__, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2481 | "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] | 2482 | PyDoc_STRVAR(values__doc__, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2483 | "D.values() -> an object providing a view on D's values"); |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 2484 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2485 | static PyMethodDef mapp_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2486 | {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST, |
| 2487 | contains__doc__}, |
| 2488 | {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST, |
| 2489 | getitem__doc__}, |
| 2490 | {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS, |
| 2491 | sizeof__doc__}, |
| 2492 | {"get", (PyCFunction)dict_get, METH_VARARGS, |
| 2493 | get__doc__}, |
| 2494 | {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS, |
| 2495 | setdefault_doc__}, |
| 2496 | {"pop", (PyCFunction)dict_pop, METH_VARARGS, |
| 2497 | pop__doc__}, |
| 2498 | {"popitem", (PyCFunction)dict_popitem, METH_NOARGS, |
| 2499 | popitem__doc__}, |
| 2500 | {"keys", (PyCFunction)dictkeys_new, METH_NOARGS, |
| 2501 | keys__doc__}, |
| 2502 | {"items", (PyCFunction)dictitems_new, METH_NOARGS, |
| 2503 | items__doc__}, |
| 2504 | {"values", (PyCFunction)dictvalues_new, METH_NOARGS, |
| 2505 | values__doc__}, |
| 2506 | {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS, |
| 2507 | update__doc__}, |
| 2508 | {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS, |
| 2509 | fromkeys__doc__}, |
| 2510 | {"clear", (PyCFunction)dict_clear, METH_NOARGS, |
| 2511 | clear__doc__}, |
| 2512 | {"copy", (PyCFunction)dict_copy, METH_NOARGS, |
| 2513 | copy__doc__}, |
| 2514 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2515 | }; |
| 2516 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2517 | /* 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] | 2518 | int |
| 2519 | PyDict_Contains(PyObject *op, PyObject *key) |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 2520 | { |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2521 | Py_hash_t hash; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2522 | PyDictObject *mp = (PyDictObject *)op; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2523 | PyDictKeyEntry *ep; |
| 2524 | PyObject **value_addr; |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 2525 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2526 | if (!PyUnicode_CheckExact(key) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2527 | (hash = ((PyASCIIObject *) key)->hash) == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2528 | hash = PyObject_Hash(key); |
| 2529 | if (hash == -1) |
| 2530 | return -1; |
| 2531 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2532 | ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr); |
| 2533 | return (ep == NULL) ? -1 : (*value_addr != NULL); |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 2534 | } |
| 2535 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2536 | /* Internal version of PyDict_Contains used when the hash value is already known */ |
| 2537 | int |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 2538 | _PyDict_Contains(PyObject *op, PyObject *key, Py_hash_t hash) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2539 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2540 | PyDictObject *mp = (PyDictObject *)op; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2541 | PyDictKeyEntry *ep; |
| 2542 | PyObject **value_addr; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2543 | |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2544 | ep = (mp->ma_keys->dk_lookup)(mp, key, hash, &value_addr); |
| 2545 | return (ep == NULL) ? -1 : (*value_addr != NULL); |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 2546 | } |
| 2547 | |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 2548 | /* Hack to implement "key in dict" */ |
| 2549 | static PySequenceMethods dict_as_sequence = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2550 | 0, /* sq_length */ |
| 2551 | 0, /* sq_concat */ |
| 2552 | 0, /* sq_repeat */ |
| 2553 | 0, /* sq_item */ |
| 2554 | 0, /* sq_slice */ |
| 2555 | 0, /* sq_ass_item */ |
| 2556 | 0, /* sq_ass_slice */ |
| 2557 | PyDict_Contains, /* sq_contains */ |
| 2558 | 0, /* sq_inplace_concat */ |
| 2559 | 0, /* sq_inplace_repeat */ |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 2560 | }; |
| 2561 | |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 2562 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2563 | dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2564 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2565 | PyObject *self; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2566 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2567 | assert(type != NULL && type->tp_alloc != NULL); |
| 2568 | self = type->tp_alloc(type, 0); |
| 2569 | if (self != NULL) { |
| 2570 | PyDictObject *d = (PyDictObject *)self; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2571 | d->ma_keys = new_keys_object(PyDict_MINSIZE_COMBINED); |
| 2572 | /* XXX - Should we raise a no-memory error? */ |
| 2573 | if (d->ma_keys == NULL) { |
| 2574 | DK_INCREF(Py_EMPTY_KEYS); |
| 2575 | d->ma_keys = Py_EMPTY_KEYS; |
| 2576 | d->ma_values = empty_values; |
| 2577 | } |
| 2578 | d->ma_used = 0; |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 2579 | /* The object has been implicitly tracked by tp_alloc */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2580 | if (type == &PyDict_Type) |
| 2581 | _PyObject_GC_UNTRACK(d); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2582 | } |
| 2583 | return self; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2584 | } |
| 2585 | |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 2586 | static int |
| 2587 | dict_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 2588 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2589 | return dict_update_common(self, args, kwds, "dict"); |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 2590 | } |
| 2591 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2592 | static PyObject * |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2593 | dict_iter(PyDictObject *dict) |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 2594 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2595 | return dictiter_new(dict, &PyDictIterKey_Type); |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 2596 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2597 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2598 | PyDoc_STRVAR(dictionary_doc, |
Ezio Melotti | 7f807b7 | 2010-03-01 04:08:34 +0000 | [diff] [blame] | 2599 | "dict() -> new empty dictionary\n" |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 2600 | "dict(mapping) -> new dictionary initialized from a mapping object's\n" |
Ezio Melotti | 7f807b7 | 2010-03-01 04:08:34 +0000 | [diff] [blame] | 2601 | " (key, value) pairs\n" |
| 2602 | "dict(iterable) -> new dictionary initialized as if via:\n" |
Tim Peters | 4d85953 | 2001-10-27 18:27:48 +0000 | [diff] [blame] | 2603 | " d = {}\n" |
Ezio Melotti | 7f807b7 | 2010-03-01 04:08:34 +0000 | [diff] [blame] | 2604 | " for k, v in iterable:\n" |
Just van Rossum | a797d81 | 2002-11-23 09:45:04 +0000 | [diff] [blame] | 2605 | " d[k] = v\n" |
| 2606 | "dict(**kwargs) -> new dictionary initialized with the name=value pairs\n" |
| 2607 | " in the keyword argument list. For example: dict(one=1, two=2)"); |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 2608 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2609 | PyTypeObject PyDict_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2610 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2611 | "dict", |
| 2612 | sizeof(PyDictObject), |
| 2613 | 0, |
| 2614 | (destructor)dict_dealloc, /* tp_dealloc */ |
| 2615 | 0, /* tp_print */ |
| 2616 | 0, /* tp_getattr */ |
| 2617 | 0, /* tp_setattr */ |
| 2618 | 0, /* tp_reserved */ |
| 2619 | (reprfunc)dict_repr, /* tp_repr */ |
| 2620 | 0, /* tp_as_number */ |
| 2621 | &dict_as_sequence, /* tp_as_sequence */ |
| 2622 | &dict_as_mapping, /* tp_as_mapping */ |
Georg Brandl | 00da4e0 | 2010-10-18 07:32:48 +0000 | [diff] [blame] | 2623 | PyObject_HashNotImplemented, /* tp_hash */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2624 | 0, /* tp_call */ |
| 2625 | 0, /* tp_str */ |
| 2626 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2627 | 0, /* tp_setattro */ |
| 2628 | 0, /* tp_as_buffer */ |
| 2629 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 2630 | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */ |
| 2631 | dictionary_doc, /* tp_doc */ |
| 2632 | dict_traverse, /* tp_traverse */ |
| 2633 | dict_tp_clear, /* tp_clear */ |
| 2634 | dict_richcompare, /* tp_richcompare */ |
| 2635 | 0, /* tp_weaklistoffset */ |
| 2636 | (getiterfunc)dict_iter, /* tp_iter */ |
| 2637 | 0, /* tp_iternext */ |
| 2638 | mapp_methods, /* tp_methods */ |
| 2639 | 0, /* tp_members */ |
| 2640 | 0, /* tp_getset */ |
| 2641 | 0, /* tp_base */ |
| 2642 | 0, /* tp_dict */ |
| 2643 | 0, /* tp_descr_get */ |
| 2644 | 0, /* tp_descr_set */ |
| 2645 | 0, /* tp_dictoffset */ |
| 2646 | dict_init, /* tp_init */ |
| 2647 | PyType_GenericAlloc, /* tp_alloc */ |
| 2648 | dict_new, /* tp_new */ |
| 2649 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2650 | }; |
| 2651 | |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2652 | PyObject * |
| 2653 | _PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key) |
| 2654 | { |
| 2655 | PyObject *kv; |
| 2656 | kv = _PyUnicode_FromId(key); /* borrowed */ |
| 2657 | if (kv == NULL) |
| 2658 | return NULL; |
| 2659 | return PyDict_GetItem(dp, kv); |
| 2660 | } |
| 2661 | |
Guido van Rossum | 3cca245 | 1997-05-16 14:23:33 +0000 | [diff] [blame] | 2662 | /* For backward compatibility with old dictionary interface */ |
| 2663 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2664 | PyObject * |
Martin v. Löwis | 32b4a1b | 2002-12-11 13:21:12 +0000 | [diff] [blame] | 2665 | PyDict_GetItemString(PyObject *v, const char *key) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2666 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2667 | PyObject *kv, *rv; |
| 2668 | kv = PyUnicode_FromString(key); |
| 2669 | if (kv == NULL) |
| 2670 | return NULL; |
| 2671 | rv = PyDict_GetItem(v, kv); |
| 2672 | Py_DECREF(kv); |
| 2673 | return rv; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2674 | } |
| 2675 | |
| 2676 | int |
Victor Stinner | 3c1e481 | 2012-03-26 22:10:51 +0200 | [diff] [blame] | 2677 | _PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item) |
| 2678 | { |
| 2679 | PyObject *kv; |
| 2680 | kv = _PyUnicode_FromId(key); /* borrowed */ |
| 2681 | if (kv == NULL) |
| 2682 | return -1; |
| 2683 | return PyDict_SetItem(v, kv, item); |
| 2684 | } |
| 2685 | |
| 2686 | int |
Martin v. Löwis | 32b4a1b | 2002-12-11 13:21:12 +0000 | [diff] [blame] | 2687 | PyDict_SetItemString(PyObject *v, const char *key, PyObject *item) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2688 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2689 | PyObject *kv; |
| 2690 | int err; |
| 2691 | kv = PyUnicode_FromString(key); |
| 2692 | if (kv == NULL) |
| 2693 | return -1; |
| 2694 | PyUnicode_InternInPlace(&kv); /* XXX Should we really? */ |
| 2695 | err = PyDict_SetItem(v, kv, item); |
| 2696 | Py_DECREF(kv); |
| 2697 | return err; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2698 | } |
| 2699 | |
| 2700 | int |
Martin v. Löwis | 32b4a1b | 2002-12-11 13:21:12 +0000 | [diff] [blame] | 2701 | PyDict_DelItemString(PyObject *v, const char *key) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2702 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2703 | PyObject *kv; |
| 2704 | int err; |
| 2705 | kv = PyUnicode_FromString(key); |
| 2706 | if (kv == NULL) |
| 2707 | return -1; |
| 2708 | err = PyDict_DelItem(v, kv); |
| 2709 | Py_DECREF(kv); |
| 2710 | return err; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2711 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2712 | |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2713 | /* Dictionary iterator types */ |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2714 | |
| 2715 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2716 | PyObject_HEAD |
| 2717 | PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */ |
| 2718 | Py_ssize_t di_used; |
| 2719 | Py_ssize_t di_pos; |
| 2720 | PyObject* di_result; /* reusable result tuple for iteritems */ |
| 2721 | Py_ssize_t len; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2722 | } dictiterobject; |
| 2723 | |
| 2724 | static PyObject * |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 2725 | dictiter_new(PyDictObject *dict, PyTypeObject *itertype) |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2726 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2727 | dictiterobject *di; |
| 2728 | di = PyObject_GC_New(dictiterobject, itertype); |
| 2729 | if (di == NULL) |
| 2730 | return NULL; |
| 2731 | Py_INCREF(dict); |
| 2732 | di->di_dict = dict; |
| 2733 | di->di_used = dict->ma_used; |
| 2734 | di->di_pos = 0; |
| 2735 | di->len = dict->ma_used; |
| 2736 | if (itertype == &PyDictIterItem_Type) { |
| 2737 | di->di_result = PyTuple_Pack(2, Py_None, Py_None); |
| 2738 | if (di->di_result == NULL) { |
| 2739 | Py_DECREF(di); |
| 2740 | return NULL; |
| 2741 | } |
| 2742 | } |
| 2743 | else |
| 2744 | di->di_result = NULL; |
| 2745 | _PyObject_GC_TRACK(di); |
| 2746 | return (PyObject *)di; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2747 | } |
| 2748 | |
| 2749 | static void |
| 2750 | dictiter_dealloc(dictiterobject *di) |
| 2751 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2752 | Py_XDECREF(di->di_dict); |
| 2753 | Py_XDECREF(di->di_result); |
| 2754 | PyObject_GC_Del(di); |
Antoine Pitrou | 7ddda78 | 2009-01-01 15:35:33 +0000 | [diff] [blame] | 2755 | } |
| 2756 | |
| 2757 | static int |
| 2758 | dictiter_traverse(dictiterobject *di, visitproc visit, void *arg) |
| 2759 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2760 | Py_VISIT(di->di_dict); |
| 2761 | Py_VISIT(di->di_result); |
| 2762 | return 0; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2763 | } |
| 2764 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 2765 | static PyObject * |
Raymond Hettinger | 0ce6dc8 | 2004-03-18 08:38:00 +0000 | [diff] [blame] | 2766 | dictiter_len(dictiterobject *di) |
| 2767 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2768 | Py_ssize_t len = 0; |
| 2769 | if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used) |
| 2770 | len = di->len; |
| 2771 | return PyLong_FromSize_t(len); |
Raymond Hettinger | 0ce6dc8 | 2004-03-18 08:38:00 +0000 | [diff] [blame] | 2772 | } |
| 2773 | |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 2774 | PyDoc_STRVAR(length_hint_doc, |
| 2775 | "Private method returning an estimate of len(list(it))."); |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 2776 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2777 | static PyObject * |
| 2778 | dictiter_reduce(dictiterobject *di); |
| 2779 | |
| 2780 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 2781 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 2782 | static PyMethodDef dictiter_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2783 | {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS, |
| 2784 | length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2785 | {"__reduce__", (PyCFunction)dictiter_reduce, METH_NOARGS, |
| 2786 | reduce_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2787 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | 0ce6dc8 | 2004-03-18 08:38:00 +0000 | [diff] [blame] | 2788 | }; |
| 2789 | |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2790 | static PyObject *dictiter_iternextkey(dictiterobject *di) |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2791 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2792 | PyObject *key; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2793 | register Py_ssize_t i, mask, offset; |
| 2794 | register PyDictKeysObject *k; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2795 | PyDictObject *d = di->di_dict; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2796 | PyObject **value_ptr; |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2797 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2798 | if (d == NULL) |
| 2799 | return NULL; |
| 2800 | assert (PyDict_Check(d)); |
Guido van Rossum | 2147df7 | 2002-07-16 20:30:22 +0000 | [diff] [blame] | 2801 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2802 | if (di->di_used != d->ma_used) { |
| 2803 | PyErr_SetString(PyExc_RuntimeError, |
| 2804 | "dictionary changed size during iteration"); |
| 2805 | di->di_used = -1; /* Make this state sticky */ |
| 2806 | return NULL; |
| 2807 | } |
Guido van Rossum | 2147df7 | 2002-07-16 20:30:22 +0000 | [diff] [blame] | 2808 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2809 | i = di->di_pos; |
| 2810 | if (i < 0) |
| 2811 | goto fail; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2812 | k = d->ma_keys; |
| 2813 | if (d->ma_values) { |
| 2814 | value_ptr = &d->ma_values[i]; |
| 2815 | offset = sizeof(PyObject *); |
| 2816 | } |
| 2817 | else { |
| 2818 | value_ptr = &k->dk_entries[i].me_value; |
| 2819 | offset = sizeof(PyDictKeyEntry); |
| 2820 | } |
| 2821 | mask = DK_SIZE(k)-1; |
| 2822 | while (i <= mask && *value_ptr == NULL) { |
| 2823 | value_ptr = (PyObject **)(((char *)value_ptr) + offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2824 | i++; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2825 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2826 | di->di_pos = i+1; |
| 2827 | if (i > mask) |
| 2828 | goto fail; |
| 2829 | di->len--; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2830 | key = k->dk_entries[i].me_key; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2831 | Py_INCREF(key); |
| 2832 | return key; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2833 | |
| 2834 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2835 | Py_DECREF(d); |
| 2836 | di->di_dict = NULL; |
| 2837 | return NULL; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2838 | } |
| 2839 | |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2840 | PyTypeObject PyDictIterKey_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2841 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2842 | "dict_keyiterator", /* tp_name */ |
| 2843 | sizeof(dictiterobject), /* tp_basicsize */ |
| 2844 | 0, /* tp_itemsize */ |
| 2845 | /* methods */ |
| 2846 | (destructor)dictiter_dealloc, /* tp_dealloc */ |
| 2847 | 0, /* tp_print */ |
| 2848 | 0, /* tp_getattr */ |
| 2849 | 0, /* tp_setattr */ |
| 2850 | 0, /* tp_reserved */ |
| 2851 | 0, /* tp_repr */ |
| 2852 | 0, /* tp_as_number */ |
| 2853 | 0, /* tp_as_sequence */ |
| 2854 | 0, /* tp_as_mapping */ |
| 2855 | 0, /* tp_hash */ |
| 2856 | 0, /* tp_call */ |
| 2857 | 0, /* tp_str */ |
| 2858 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2859 | 0, /* tp_setattro */ |
| 2860 | 0, /* tp_as_buffer */ |
| 2861 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 2862 | 0, /* tp_doc */ |
| 2863 | (traverseproc)dictiter_traverse, /* tp_traverse */ |
| 2864 | 0, /* tp_clear */ |
| 2865 | 0, /* tp_richcompare */ |
| 2866 | 0, /* tp_weaklistoffset */ |
| 2867 | PyObject_SelfIter, /* tp_iter */ |
| 2868 | (iternextfunc)dictiter_iternextkey, /* tp_iternext */ |
| 2869 | dictiter_methods, /* tp_methods */ |
| 2870 | 0, |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2871 | }; |
| 2872 | |
| 2873 | static PyObject *dictiter_iternextvalue(dictiterobject *di) |
| 2874 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2875 | PyObject *value; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2876 | register Py_ssize_t i, mask, offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2877 | PyDictObject *d = di->di_dict; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2878 | PyObject **value_ptr; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2879 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2880 | if (d == NULL) |
| 2881 | return NULL; |
| 2882 | assert (PyDict_Check(d)); |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2883 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2884 | if (di->di_used != d->ma_used) { |
| 2885 | PyErr_SetString(PyExc_RuntimeError, |
| 2886 | "dictionary changed size during iteration"); |
| 2887 | di->di_used = -1; /* Make this state sticky */ |
| 2888 | return NULL; |
| 2889 | } |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2890 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2891 | i = di->di_pos; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2892 | mask = DK_SIZE(d->ma_keys)-1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2893 | if (i < 0 || i > mask) |
| 2894 | goto fail; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2895 | if (d->ma_values) { |
| 2896 | value_ptr = &d->ma_values[i]; |
| 2897 | offset = sizeof(PyObject *); |
| 2898 | } |
| 2899 | else { |
| 2900 | value_ptr = &d->ma_keys->dk_entries[i].me_value; |
| 2901 | offset = sizeof(PyDictKeyEntry); |
| 2902 | } |
| 2903 | while (i <= mask && *value_ptr == NULL) { |
| 2904 | value_ptr = (PyObject **)(((char *)value_ptr) + offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2905 | i++; |
| 2906 | if (i > mask) |
| 2907 | goto fail; |
| 2908 | } |
| 2909 | di->di_pos = i+1; |
| 2910 | di->len--; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2911 | value = *value_ptr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2912 | Py_INCREF(value); |
| 2913 | return value; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2914 | |
| 2915 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2916 | Py_DECREF(d); |
| 2917 | di->di_dict = NULL; |
| 2918 | return NULL; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2919 | } |
| 2920 | |
| 2921 | PyTypeObject PyDictIterValue_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2922 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2923 | "dict_valueiterator", /* tp_name */ |
| 2924 | sizeof(dictiterobject), /* tp_basicsize */ |
| 2925 | 0, /* tp_itemsize */ |
| 2926 | /* methods */ |
| 2927 | (destructor)dictiter_dealloc, /* tp_dealloc */ |
| 2928 | 0, /* tp_print */ |
| 2929 | 0, /* tp_getattr */ |
| 2930 | 0, /* tp_setattr */ |
| 2931 | 0, /* tp_reserved */ |
| 2932 | 0, /* tp_repr */ |
| 2933 | 0, /* tp_as_number */ |
| 2934 | 0, /* tp_as_sequence */ |
| 2935 | 0, /* tp_as_mapping */ |
| 2936 | 0, /* tp_hash */ |
| 2937 | 0, /* tp_call */ |
| 2938 | 0, /* tp_str */ |
| 2939 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2940 | 0, /* tp_setattro */ |
| 2941 | 0, /* tp_as_buffer */ |
| 2942 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 2943 | 0, /* tp_doc */ |
| 2944 | (traverseproc)dictiter_traverse, /* tp_traverse */ |
| 2945 | 0, /* tp_clear */ |
| 2946 | 0, /* tp_richcompare */ |
| 2947 | 0, /* tp_weaklistoffset */ |
| 2948 | PyObject_SelfIter, /* tp_iter */ |
| 2949 | (iternextfunc)dictiter_iternextvalue, /* tp_iternext */ |
| 2950 | dictiter_methods, /* tp_methods */ |
| 2951 | 0, |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2952 | }; |
| 2953 | |
| 2954 | static PyObject *dictiter_iternextitem(dictiterobject *di) |
| 2955 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2956 | PyObject *key, *value, *result = di->di_result; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2957 | register Py_ssize_t i, mask, offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2958 | PyDictObject *d = di->di_dict; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2959 | PyObject **value_ptr; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2960 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2961 | if (d == NULL) |
| 2962 | return NULL; |
| 2963 | assert (PyDict_Check(d)); |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2964 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2965 | if (di->di_used != d->ma_used) { |
| 2966 | PyErr_SetString(PyExc_RuntimeError, |
| 2967 | "dictionary changed size during iteration"); |
| 2968 | di->di_used = -1; /* Make this state sticky */ |
| 2969 | return NULL; |
| 2970 | } |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2971 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2972 | i = di->di_pos; |
| 2973 | if (i < 0) |
| 2974 | goto fail; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2975 | mask = DK_SIZE(d->ma_keys)-1; |
| 2976 | if (d->ma_values) { |
| 2977 | value_ptr = &d->ma_values[i]; |
| 2978 | offset = sizeof(PyObject *); |
| 2979 | } |
| 2980 | else { |
| 2981 | value_ptr = &d->ma_keys->dk_entries[i].me_value; |
| 2982 | offset = sizeof(PyDictKeyEntry); |
| 2983 | } |
| 2984 | while (i <= mask && *value_ptr == NULL) { |
| 2985 | value_ptr = (PyObject **)(((char *)value_ptr) + offset); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2986 | i++; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 2987 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2988 | di->di_pos = i+1; |
| 2989 | if (i > mask) |
| 2990 | goto fail; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2991 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2992 | if (result->ob_refcnt == 1) { |
| 2993 | Py_INCREF(result); |
| 2994 | Py_DECREF(PyTuple_GET_ITEM(result, 0)); |
| 2995 | Py_DECREF(PyTuple_GET_ITEM(result, 1)); |
| 2996 | } else { |
| 2997 | result = PyTuple_New(2); |
| 2998 | if (result == NULL) |
| 2999 | return NULL; |
| 3000 | } |
| 3001 | di->len--; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 3002 | key = d->ma_keys->dk_entries[i].me_key; |
| 3003 | value = *value_ptr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3004 | Py_INCREF(key); |
| 3005 | Py_INCREF(value); |
| 3006 | PyTuple_SET_ITEM(result, 0, key); |
| 3007 | PyTuple_SET_ITEM(result, 1, value); |
| 3008 | return result; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 3009 | |
| 3010 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3011 | Py_DECREF(d); |
| 3012 | di->di_dict = NULL; |
| 3013 | return NULL; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 3014 | } |
| 3015 | |
| 3016 | PyTypeObject PyDictIterItem_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3017 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 3018 | "dict_itemiterator", /* tp_name */ |
| 3019 | sizeof(dictiterobject), /* tp_basicsize */ |
| 3020 | 0, /* tp_itemsize */ |
| 3021 | /* methods */ |
| 3022 | (destructor)dictiter_dealloc, /* tp_dealloc */ |
| 3023 | 0, /* tp_print */ |
| 3024 | 0, /* tp_getattr */ |
| 3025 | 0, /* tp_setattr */ |
| 3026 | 0, /* tp_reserved */ |
| 3027 | 0, /* tp_repr */ |
| 3028 | 0, /* tp_as_number */ |
| 3029 | 0, /* tp_as_sequence */ |
| 3030 | 0, /* tp_as_mapping */ |
| 3031 | 0, /* tp_hash */ |
| 3032 | 0, /* tp_call */ |
| 3033 | 0, /* tp_str */ |
| 3034 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3035 | 0, /* tp_setattro */ |
| 3036 | 0, /* tp_as_buffer */ |
| 3037 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 3038 | 0, /* tp_doc */ |
| 3039 | (traverseproc)dictiter_traverse, /* tp_traverse */ |
| 3040 | 0, /* tp_clear */ |
| 3041 | 0, /* tp_richcompare */ |
| 3042 | 0, /* tp_weaklistoffset */ |
| 3043 | PyObject_SelfIter, /* tp_iter */ |
| 3044 | (iternextfunc)dictiter_iternextitem, /* tp_iternext */ |
| 3045 | dictiter_methods, /* tp_methods */ |
| 3046 | 0, |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 3047 | }; |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3048 | |
| 3049 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3050 | static PyObject * |
| 3051 | dictiter_reduce(dictiterobject *di) |
| 3052 | { |
| 3053 | PyObject *list; |
| 3054 | dictiterobject tmp; |
| 3055 | |
| 3056 | list = PyList_New(0); |
| 3057 | if (!list) |
| 3058 | return NULL; |
| 3059 | |
| 3060 | /* copy the itertor state */ |
| 3061 | tmp = *di; |
| 3062 | Py_XINCREF(tmp.di_dict); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 3063 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3064 | /* iterate the temporary into a list */ |
| 3065 | for(;;) { |
| 3066 | PyObject *element = 0; |
| 3067 | if (Py_TYPE(di) == &PyDictIterItem_Type) |
| 3068 | element = dictiter_iternextitem(&tmp); |
| 3069 | else if (Py_TYPE(di) == &PyDictIterKey_Type) |
| 3070 | element = dictiter_iternextkey(&tmp); |
| 3071 | else if (Py_TYPE(di) == &PyDictIterValue_Type) |
| 3072 | element = dictiter_iternextvalue(&tmp); |
| 3073 | else |
| 3074 | assert(0); |
| 3075 | if (element) { |
| 3076 | if (PyList_Append(list, element)) { |
| 3077 | Py_DECREF(element); |
| 3078 | Py_DECREF(list); |
| 3079 | Py_XDECREF(tmp.di_dict); |
| 3080 | return NULL; |
| 3081 | } |
| 3082 | Py_DECREF(element); |
| 3083 | } else |
| 3084 | break; |
| 3085 | } |
| 3086 | Py_XDECREF(tmp.di_dict); |
| 3087 | /* check for error */ |
| 3088 | if (tmp.di_dict != NULL) { |
| 3089 | /* we have an error */ |
| 3090 | Py_DECREF(list); |
| 3091 | return NULL; |
| 3092 | } |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 3093 | return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), list); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 3094 | } |
| 3095 | |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 3096 | /***********************************************/ |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3097 | /* View objects for keys(), items(), values(). */ |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 3098 | /***********************************************/ |
| 3099 | |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3100 | /* The instance lay-out is the same for all three; but the type differs. */ |
| 3101 | |
| 3102 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3103 | PyObject_HEAD |
| 3104 | PyDictObject *dv_dict; |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3105 | } dictviewobject; |
| 3106 | |
| 3107 | |
| 3108 | static void |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 3109 | dictview_dealloc(dictviewobject *dv) |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3110 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3111 | Py_XDECREF(dv->dv_dict); |
| 3112 | PyObject_GC_Del(dv); |
Antoine Pitrou | 7ddda78 | 2009-01-01 15:35:33 +0000 | [diff] [blame] | 3113 | } |
| 3114 | |
| 3115 | static int |
| 3116 | dictview_traverse(dictviewobject *dv, visitproc visit, void *arg) |
| 3117 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3118 | Py_VISIT(dv->dv_dict); |
| 3119 | return 0; |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3120 | } |
| 3121 | |
Guido van Rossum | 83825ac | 2007-02-10 04:54:19 +0000 | [diff] [blame] | 3122 | static Py_ssize_t |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 3123 | dictview_len(dictviewobject *dv) |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3124 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3125 | Py_ssize_t len = 0; |
| 3126 | if (dv->dv_dict != NULL) |
| 3127 | len = dv->dv_dict->ma_used; |
| 3128 | return len; |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3129 | } |
| 3130 | |
| 3131 | static PyObject * |
| 3132 | dictview_new(PyObject *dict, PyTypeObject *type) |
| 3133 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3134 | dictviewobject *dv; |
| 3135 | if (dict == NULL) { |
| 3136 | PyErr_BadInternalCall(); |
| 3137 | return NULL; |
| 3138 | } |
| 3139 | if (!PyDict_Check(dict)) { |
| 3140 | /* XXX Get rid of this restriction later */ |
| 3141 | PyErr_Format(PyExc_TypeError, |
| 3142 | "%s() requires a dict argument, not '%s'", |
| 3143 | type->tp_name, dict->ob_type->tp_name); |
| 3144 | return NULL; |
| 3145 | } |
| 3146 | dv = PyObject_GC_New(dictviewobject, type); |
| 3147 | if (dv == NULL) |
| 3148 | return NULL; |
| 3149 | Py_INCREF(dict); |
| 3150 | dv->dv_dict = (PyDictObject *)dict; |
| 3151 | _PyObject_GC_TRACK(dv); |
| 3152 | return (PyObject *)dv; |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3153 | } |
| 3154 | |
Neal Norwitz | e36f2ba | 2007-02-26 23:12:28 +0000 | [diff] [blame] | 3155 | /* TODO(guido): The views objects are not complete: |
| 3156 | |
| 3157 | * support more set operations |
| 3158 | * support arbitrary mappings? |
| 3159 | - either these should be static or exported in dictobject.h |
| 3160 | - if public then they should probably be in builtins |
| 3161 | */ |
| 3162 | |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 3163 | /* Return 1 if self is a subset of other, iterating over self; |
| 3164 | 0 if not; -1 if an error occurred. */ |
Guido van Rossum | d9214d1 | 2007-02-12 02:23:40 +0000 | [diff] [blame] | 3165 | static int |
| 3166 | all_contained_in(PyObject *self, PyObject *other) |
| 3167 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3168 | PyObject *iter = PyObject_GetIter(self); |
| 3169 | int ok = 1; |
Guido van Rossum | d9214d1 | 2007-02-12 02:23:40 +0000 | [diff] [blame] | 3170 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3171 | if (iter == NULL) |
| 3172 | return -1; |
| 3173 | for (;;) { |
| 3174 | PyObject *next = PyIter_Next(iter); |
| 3175 | if (next == NULL) { |
| 3176 | if (PyErr_Occurred()) |
| 3177 | ok = -1; |
| 3178 | break; |
| 3179 | } |
| 3180 | ok = PySequence_Contains(other, next); |
| 3181 | Py_DECREF(next); |
| 3182 | if (ok <= 0) |
| 3183 | break; |
| 3184 | } |
| 3185 | Py_DECREF(iter); |
| 3186 | return ok; |
Guido van Rossum | d9214d1 | 2007-02-12 02:23:40 +0000 | [diff] [blame] | 3187 | } |
| 3188 | |
| 3189 | static PyObject * |
| 3190 | dictview_richcompare(PyObject *self, PyObject *other, int op) |
| 3191 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3192 | Py_ssize_t len_self, len_other; |
| 3193 | int ok; |
| 3194 | PyObject *result; |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 3195 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3196 | assert(self != NULL); |
| 3197 | assert(PyDictViewSet_Check(self)); |
| 3198 | assert(other != NULL); |
Guido van Rossum | d9214d1 | 2007-02-12 02:23:40 +0000 | [diff] [blame] | 3199 | |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 3200 | if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other)) |
| 3201 | Py_RETURN_NOTIMPLEMENTED; |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 3202 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3203 | len_self = PyObject_Size(self); |
| 3204 | if (len_self < 0) |
| 3205 | return NULL; |
| 3206 | len_other = PyObject_Size(other); |
| 3207 | if (len_other < 0) |
| 3208 | return NULL; |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 3209 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3210 | ok = 0; |
| 3211 | switch(op) { |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 3212 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3213 | case Py_NE: |
| 3214 | case Py_EQ: |
| 3215 | if (len_self == len_other) |
| 3216 | ok = all_contained_in(self, other); |
| 3217 | if (op == Py_NE && ok >= 0) |
| 3218 | ok = !ok; |
| 3219 | break; |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 3220 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3221 | case Py_LT: |
| 3222 | if (len_self < len_other) |
| 3223 | ok = all_contained_in(self, other); |
| 3224 | break; |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 3225 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3226 | case Py_LE: |
| 3227 | if (len_self <= len_other) |
| 3228 | ok = all_contained_in(self, other); |
| 3229 | break; |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 3230 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3231 | case Py_GT: |
| 3232 | if (len_self > len_other) |
| 3233 | ok = all_contained_in(other, self); |
| 3234 | break; |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 3235 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3236 | case Py_GE: |
| 3237 | if (len_self >= len_other) |
| 3238 | ok = all_contained_in(other, self); |
| 3239 | break; |
Guido van Rossum | aac530c | 2007-08-24 22:33:45 +0000 | [diff] [blame] | 3240 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3241 | } |
| 3242 | if (ok < 0) |
| 3243 | return NULL; |
| 3244 | result = ok ? Py_True : Py_False; |
| 3245 | Py_INCREF(result); |
| 3246 | return result; |
Guido van Rossum | d9214d1 | 2007-02-12 02:23:40 +0000 | [diff] [blame] | 3247 | } |
| 3248 | |
Raymond Hettinger | b0d56af | 2009-03-03 10:52:49 +0000 | [diff] [blame] | 3249 | static PyObject * |
| 3250 | dictview_repr(dictviewobject *dv) |
| 3251 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3252 | PyObject *seq; |
| 3253 | PyObject *result; |
Raymond Hettinger | b0d56af | 2009-03-03 10:52:49 +0000 | [diff] [blame] | 3254 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3255 | seq = PySequence_List((PyObject *)dv); |
| 3256 | if (seq == NULL) |
| 3257 | return NULL; |
| 3258 | |
| 3259 | result = PyUnicode_FromFormat("%s(%R)", Py_TYPE(dv)->tp_name, seq); |
| 3260 | Py_DECREF(seq); |
| 3261 | return result; |
Raymond Hettinger | b0d56af | 2009-03-03 10:52:49 +0000 | [diff] [blame] | 3262 | } |
| 3263 | |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 3264 | /*** dict_keys ***/ |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3265 | |
| 3266 | static PyObject * |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 3267 | dictkeys_iter(dictviewobject *dv) |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3268 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3269 | if (dv->dv_dict == NULL) { |
| 3270 | Py_RETURN_NONE; |
| 3271 | } |
| 3272 | return dictiter_new(dv->dv_dict, &PyDictIterKey_Type); |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 3273 | } |
| 3274 | |
| 3275 | static int |
| 3276 | dictkeys_contains(dictviewobject *dv, PyObject *obj) |
| 3277 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3278 | if (dv->dv_dict == NULL) |
| 3279 | return 0; |
| 3280 | return PyDict_Contains((PyObject *)dv->dv_dict, obj); |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3281 | } |
| 3282 | |
Guido van Rossum | 83825ac | 2007-02-10 04:54:19 +0000 | [diff] [blame] | 3283 | static PySequenceMethods dictkeys_as_sequence = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3284 | (lenfunc)dictview_len, /* sq_length */ |
| 3285 | 0, /* sq_concat */ |
| 3286 | 0, /* sq_repeat */ |
| 3287 | 0, /* sq_item */ |
| 3288 | 0, /* sq_slice */ |
| 3289 | 0, /* sq_ass_item */ |
| 3290 | 0, /* sq_ass_slice */ |
| 3291 | (objobjproc)dictkeys_contains, /* sq_contains */ |
Guido van Rossum | 83825ac | 2007-02-10 04:54:19 +0000 | [diff] [blame] | 3292 | }; |
| 3293 | |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3294 | static PyObject* |
| 3295 | dictviews_sub(PyObject* self, PyObject *other) |
| 3296 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3297 | PyObject *result = PySet_New(self); |
| 3298 | PyObject *tmp; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3299 | _Py_IDENTIFIER(difference_update); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 3300 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3301 | if (result == NULL) |
| 3302 | return NULL; |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3303 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 3304 | tmp = _PyObject_CallMethodId(result, &PyId_difference_update, "O", other); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3305 | if (tmp == NULL) { |
| 3306 | Py_DECREF(result); |
| 3307 | return NULL; |
| 3308 | } |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3309 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3310 | Py_DECREF(tmp); |
| 3311 | return result; |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3312 | } |
| 3313 | |
| 3314 | static PyObject* |
| 3315 | dictviews_and(PyObject* self, PyObject *other) |
| 3316 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3317 | PyObject *result = PySet_New(self); |
| 3318 | PyObject *tmp; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3319 | _Py_IDENTIFIER(intersection_update); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 3320 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3321 | if (result == NULL) |
| 3322 | return NULL; |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3323 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 3324 | tmp = _PyObject_CallMethodId(result, &PyId_intersection_update, "O", other); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3325 | if (tmp == NULL) { |
| 3326 | Py_DECREF(result); |
| 3327 | return NULL; |
| 3328 | } |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3329 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3330 | Py_DECREF(tmp); |
| 3331 | return result; |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3332 | } |
| 3333 | |
| 3334 | static PyObject* |
| 3335 | dictviews_or(PyObject* self, PyObject *other) |
| 3336 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3337 | PyObject *result = PySet_New(self); |
| 3338 | PyObject *tmp; |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 3339 | _Py_IDENTIFIER(update); |
Victor Stinner | d1a9cc2 | 2011-10-13 22:51:17 +0200 | [diff] [blame] | 3340 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3341 | if (result == NULL) |
| 3342 | return NULL; |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3343 | |
Victor Stinner | d1a9cc2 | 2011-10-13 22:51:17 +0200 | [diff] [blame] | 3344 | tmp = _PyObject_CallMethodId(result, &PyId_update, "O", other); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3345 | if (tmp == NULL) { |
| 3346 | Py_DECREF(result); |
| 3347 | return NULL; |
| 3348 | } |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3349 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3350 | Py_DECREF(tmp); |
| 3351 | return result; |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3352 | } |
| 3353 | |
| 3354 | static PyObject* |
| 3355 | dictviews_xor(PyObject* self, PyObject *other) |
| 3356 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3357 | PyObject *result = PySet_New(self); |
| 3358 | PyObject *tmp; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3359 | _Py_IDENTIFIER(symmetric_difference_update); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 3360 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3361 | if (result == NULL) |
| 3362 | return NULL; |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3363 | |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 3364 | tmp = _PyObject_CallMethodId(result, &PyId_symmetric_difference_update, "O", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3365 | other); |
| 3366 | if (tmp == NULL) { |
| 3367 | Py_DECREF(result); |
| 3368 | return NULL; |
| 3369 | } |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3370 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3371 | Py_DECREF(tmp); |
| 3372 | return result; |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3373 | } |
| 3374 | |
| 3375 | static PyNumberMethods dictviews_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3376 | 0, /*nb_add*/ |
| 3377 | (binaryfunc)dictviews_sub, /*nb_subtract*/ |
| 3378 | 0, /*nb_multiply*/ |
| 3379 | 0, /*nb_remainder*/ |
| 3380 | 0, /*nb_divmod*/ |
| 3381 | 0, /*nb_power*/ |
| 3382 | 0, /*nb_negative*/ |
| 3383 | 0, /*nb_positive*/ |
| 3384 | 0, /*nb_absolute*/ |
| 3385 | 0, /*nb_bool*/ |
| 3386 | 0, /*nb_invert*/ |
| 3387 | 0, /*nb_lshift*/ |
| 3388 | 0, /*nb_rshift*/ |
| 3389 | (binaryfunc)dictviews_and, /*nb_and*/ |
| 3390 | (binaryfunc)dictviews_xor, /*nb_xor*/ |
| 3391 | (binaryfunc)dictviews_or, /*nb_or*/ |
Guido van Rossum | 523259b | 2007-08-24 23:41:22 +0000 | [diff] [blame] | 3392 | }; |
| 3393 | |
Daniel Stutzbach | 045b3ba | 2010-09-02 15:06:06 +0000 | [diff] [blame] | 3394 | static PyObject* |
| 3395 | dictviews_isdisjoint(PyObject *self, PyObject *other) |
| 3396 | { |
| 3397 | PyObject *it; |
| 3398 | PyObject *item = NULL; |
| 3399 | |
| 3400 | if (self == other) { |
| 3401 | if (dictview_len((dictviewobject *)self) == 0) |
| 3402 | Py_RETURN_TRUE; |
| 3403 | else |
| 3404 | Py_RETURN_FALSE; |
| 3405 | } |
| 3406 | |
| 3407 | /* Iterate over the shorter object (only if other is a set, |
| 3408 | * because PySequence_Contains may be expensive otherwise): */ |
| 3409 | if (PyAnySet_Check(other) || PyDictViewSet_Check(other)) { |
| 3410 | Py_ssize_t len_self = dictview_len((dictviewobject *)self); |
| 3411 | Py_ssize_t len_other = PyObject_Size(other); |
| 3412 | if (len_other == -1) |
| 3413 | return NULL; |
| 3414 | |
| 3415 | if ((len_other > len_self)) { |
| 3416 | PyObject *tmp = other; |
| 3417 | other = self; |
| 3418 | self = tmp; |
| 3419 | } |
| 3420 | } |
| 3421 | |
| 3422 | it = PyObject_GetIter(other); |
| 3423 | if (it == NULL) |
| 3424 | return NULL; |
| 3425 | |
| 3426 | while ((item = PyIter_Next(it)) != NULL) { |
| 3427 | int contains = PySequence_Contains(self, item); |
| 3428 | Py_DECREF(item); |
| 3429 | if (contains == -1) { |
| 3430 | Py_DECREF(it); |
| 3431 | return NULL; |
| 3432 | } |
| 3433 | |
| 3434 | if (contains) { |
| 3435 | Py_DECREF(it); |
| 3436 | Py_RETURN_FALSE; |
| 3437 | } |
| 3438 | } |
| 3439 | Py_DECREF(it); |
| 3440 | if (PyErr_Occurred()) |
| 3441 | return NULL; /* PyIter_Next raised an exception. */ |
| 3442 | Py_RETURN_TRUE; |
| 3443 | } |
| 3444 | |
| 3445 | PyDoc_STRVAR(isdisjoint_doc, |
| 3446 | "Return True if the view and the given iterable have a null intersection."); |
| 3447 | |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3448 | static PyMethodDef dictkeys_methods[] = { |
Daniel Stutzbach | 045b3ba | 2010-09-02 15:06:06 +0000 | [diff] [blame] | 3449 | {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O, |
| 3450 | isdisjoint_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3451 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3452 | }; |
| 3453 | |
| 3454 | PyTypeObject PyDictKeys_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3455 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 3456 | "dict_keys", /* tp_name */ |
| 3457 | sizeof(dictviewobject), /* tp_basicsize */ |
| 3458 | 0, /* tp_itemsize */ |
| 3459 | /* methods */ |
| 3460 | (destructor)dictview_dealloc, /* tp_dealloc */ |
| 3461 | 0, /* tp_print */ |
| 3462 | 0, /* tp_getattr */ |
| 3463 | 0, /* tp_setattr */ |
| 3464 | 0, /* tp_reserved */ |
| 3465 | (reprfunc)dictview_repr, /* tp_repr */ |
| 3466 | &dictviews_as_number, /* tp_as_number */ |
| 3467 | &dictkeys_as_sequence, /* tp_as_sequence */ |
| 3468 | 0, /* tp_as_mapping */ |
| 3469 | 0, /* tp_hash */ |
| 3470 | 0, /* tp_call */ |
| 3471 | 0, /* tp_str */ |
| 3472 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3473 | 0, /* tp_setattro */ |
| 3474 | 0, /* tp_as_buffer */ |
| 3475 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 3476 | 0, /* tp_doc */ |
| 3477 | (traverseproc)dictview_traverse, /* tp_traverse */ |
| 3478 | 0, /* tp_clear */ |
| 3479 | dictview_richcompare, /* tp_richcompare */ |
| 3480 | 0, /* tp_weaklistoffset */ |
| 3481 | (getiterfunc)dictkeys_iter, /* tp_iter */ |
| 3482 | 0, /* tp_iternext */ |
| 3483 | dictkeys_methods, /* tp_methods */ |
| 3484 | 0, |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3485 | }; |
| 3486 | |
| 3487 | static PyObject * |
| 3488 | dictkeys_new(PyObject *dict) |
| 3489 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3490 | return dictview_new(dict, &PyDictKeys_Type); |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3491 | } |
| 3492 | |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 3493 | /*** dict_items ***/ |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3494 | |
| 3495 | static PyObject * |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 3496 | dictitems_iter(dictviewobject *dv) |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3497 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3498 | if (dv->dv_dict == NULL) { |
| 3499 | Py_RETURN_NONE; |
| 3500 | } |
| 3501 | return dictiter_new(dv->dv_dict, &PyDictIterItem_Type); |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 3502 | } |
| 3503 | |
| 3504 | static int |
| 3505 | dictitems_contains(dictviewobject *dv, PyObject *obj) |
| 3506 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3507 | PyObject *key, *value, *found; |
| 3508 | if (dv->dv_dict == NULL) |
| 3509 | return 0; |
| 3510 | if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2) |
| 3511 | return 0; |
| 3512 | key = PyTuple_GET_ITEM(obj, 0); |
| 3513 | value = PyTuple_GET_ITEM(obj, 1); |
| 3514 | found = PyDict_GetItem((PyObject *)dv->dv_dict, key); |
| 3515 | if (found == NULL) { |
| 3516 | if (PyErr_Occurred()) |
| 3517 | return -1; |
| 3518 | return 0; |
| 3519 | } |
| 3520 | return PyObject_RichCompareBool(value, found, Py_EQ); |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3521 | } |
| 3522 | |
Guido van Rossum | 83825ac | 2007-02-10 04:54:19 +0000 | [diff] [blame] | 3523 | static PySequenceMethods dictitems_as_sequence = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3524 | (lenfunc)dictview_len, /* sq_length */ |
| 3525 | 0, /* sq_concat */ |
| 3526 | 0, /* sq_repeat */ |
| 3527 | 0, /* sq_item */ |
| 3528 | 0, /* sq_slice */ |
| 3529 | 0, /* sq_ass_item */ |
| 3530 | 0, /* sq_ass_slice */ |
| 3531 | (objobjproc)dictitems_contains, /* sq_contains */ |
Guido van Rossum | 83825ac | 2007-02-10 04:54:19 +0000 | [diff] [blame] | 3532 | }; |
| 3533 | |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3534 | static PyMethodDef dictitems_methods[] = { |
Daniel Stutzbach | 045b3ba | 2010-09-02 15:06:06 +0000 | [diff] [blame] | 3535 | {"isdisjoint", (PyCFunction)dictviews_isdisjoint, METH_O, |
| 3536 | isdisjoint_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3537 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3538 | }; |
| 3539 | |
| 3540 | PyTypeObject PyDictItems_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3541 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 3542 | "dict_items", /* tp_name */ |
| 3543 | sizeof(dictviewobject), /* tp_basicsize */ |
| 3544 | 0, /* tp_itemsize */ |
| 3545 | /* methods */ |
| 3546 | (destructor)dictview_dealloc, /* tp_dealloc */ |
| 3547 | 0, /* tp_print */ |
| 3548 | 0, /* tp_getattr */ |
| 3549 | 0, /* tp_setattr */ |
| 3550 | 0, /* tp_reserved */ |
| 3551 | (reprfunc)dictview_repr, /* tp_repr */ |
| 3552 | &dictviews_as_number, /* tp_as_number */ |
| 3553 | &dictitems_as_sequence, /* tp_as_sequence */ |
| 3554 | 0, /* tp_as_mapping */ |
| 3555 | 0, /* tp_hash */ |
| 3556 | 0, /* tp_call */ |
| 3557 | 0, /* tp_str */ |
| 3558 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3559 | 0, /* tp_setattro */ |
| 3560 | 0, /* tp_as_buffer */ |
| 3561 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 3562 | 0, /* tp_doc */ |
| 3563 | (traverseproc)dictview_traverse, /* tp_traverse */ |
| 3564 | 0, /* tp_clear */ |
| 3565 | dictview_richcompare, /* tp_richcompare */ |
| 3566 | 0, /* tp_weaklistoffset */ |
| 3567 | (getiterfunc)dictitems_iter, /* tp_iter */ |
| 3568 | 0, /* tp_iternext */ |
| 3569 | dictitems_methods, /* tp_methods */ |
| 3570 | 0, |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3571 | }; |
| 3572 | |
| 3573 | static PyObject * |
| 3574 | dictitems_new(PyObject *dict) |
| 3575 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3576 | return dictview_new(dict, &PyDictItems_Type); |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3577 | } |
| 3578 | |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 3579 | /*** dict_values ***/ |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3580 | |
| 3581 | static PyObject * |
Guido van Rossum | 3ac6741 | 2007-02-10 18:55:06 +0000 | [diff] [blame] | 3582 | dictvalues_iter(dictviewobject *dv) |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3583 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3584 | if (dv->dv_dict == NULL) { |
| 3585 | Py_RETURN_NONE; |
| 3586 | } |
| 3587 | return dictiter_new(dv->dv_dict, &PyDictIterValue_Type); |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3588 | } |
| 3589 | |
Guido van Rossum | 83825ac | 2007-02-10 04:54:19 +0000 | [diff] [blame] | 3590 | static PySequenceMethods dictvalues_as_sequence = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3591 | (lenfunc)dictview_len, /* sq_length */ |
| 3592 | 0, /* sq_concat */ |
| 3593 | 0, /* sq_repeat */ |
| 3594 | 0, /* sq_item */ |
| 3595 | 0, /* sq_slice */ |
| 3596 | 0, /* sq_ass_item */ |
| 3597 | 0, /* sq_ass_slice */ |
| 3598 | (objobjproc)0, /* sq_contains */ |
Guido van Rossum | 83825ac | 2007-02-10 04:54:19 +0000 | [diff] [blame] | 3599 | }; |
| 3600 | |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3601 | static PyMethodDef dictvalues_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3602 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3603 | }; |
| 3604 | |
| 3605 | PyTypeObject PyDictValues_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3606 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 3607 | "dict_values", /* tp_name */ |
| 3608 | sizeof(dictviewobject), /* tp_basicsize */ |
| 3609 | 0, /* tp_itemsize */ |
| 3610 | /* methods */ |
| 3611 | (destructor)dictview_dealloc, /* tp_dealloc */ |
| 3612 | 0, /* tp_print */ |
| 3613 | 0, /* tp_getattr */ |
| 3614 | 0, /* tp_setattr */ |
| 3615 | 0, /* tp_reserved */ |
| 3616 | (reprfunc)dictview_repr, /* tp_repr */ |
| 3617 | 0, /* tp_as_number */ |
| 3618 | &dictvalues_as_sequence, /* tp_as_sequence */ |
| 3619 | 0, /* tp_as_mapping */ |
| 3620 | 0, /* tp_hash */ |
| 3621 | 0, /* tp_call */ |
| 3622 | 0, /* tp_str */ |
| 3623 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3624 | 0, /* tp_setattro */ |
| 3625 | 0, /* tp_as_buffer */ |
| 3626 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 3627 | 0, /* tp_doc */ |
| 3628 | (traverseproc)dictview_traverse, /* tp_traverse */ |
| 3629 | 0, /* tp_clear */ |
| 3630 | 0, /* tp_richcompare */ |
| 3631 | 0, /* tp_weaklistoffset */ |
| 3632 | (getiterfunc)dictvalues_iter, /* tp_iter */ |
| 3633 | 0, /* tp_iternext */ |
| 3634 | dictvalues_methods, /* tp_methods */ |
| 3635 | 0, |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3636 | }; |
| 3637 | |
| 3638 | static PyObject * |
| 3639 | dictvalues_new(PyObject *dict) |
| 3640 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3641 | return dictview_new(dict, &PyDictValues_Type); |
Guido van Rossum | b90c848 | 2007-02-10 01:11:45 +0000 | [diff] [blame] | 3642 | } |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 3643 | |
| 3644 | /* Returns NULL if cannot allocate a new PyDictKeysObject, |
| 3645 | but does not set an error */ |
| 3646 | PyDictKeysObject * |
| 3647 | _PyDict_NewKeysForClass(void) |
| 3648 | { |
| 3649 | PyDictKeysObject *keys = new_keys_object(PyDict_MINSIZE_SPLIT); |
| 3650 | if (keys == NULL) |
| 3651 | PyErr_Clear(); |
| 3652 | else |
| 3653 | keys->dk_lookup = lookdict_split; |
| 3654 | return keys; |
| 3655 | } |
| 3656 | |
| 3657 | #define CACHED_KEYS(tp) (((PyHeapTypeObject*)tp)->ht_cached_keys) |
| 3658 | |
| 3659 | PyObject * |
| 3660 | PyObject_GenericGetDict(PyObject *obj, void *context) |
| 3661 | { |
| 3662 | PyObject *dict, **dictptr = _PyObject_GetDictPtr(obj); |
| 3663 | if (dictptr == NULL) { |
| 3664 | PyErr_SetString(PyExc_AttributeError, |
| 3665 | "This object has no __dict__"); |
| 3666 | return NULL; |
| 3667 | } |
| 3668 | dict = *dictptr; |
| 3669 | if (dict == NULL) { |
| 3670 | PyTypeObject *tp = Py_TYPE(obj); |
| 3671 | if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && CACHED_KEYS(tp)) { |
| 3672 | DK_INCREF(CACHED_KEYS(tp)); |
| 3673 | *dictptr = dict = new_dict_with_shared_keys(CACHED_KEYS(tp)); |
| 3674 | } |
| 3675 | else { |
| 3676 | *dictptr = dict = PyDict_New(); |
| 3677 | } |
| 3678 | } |
| 3679 | Py_XINCREF(dict); |
| 3680 | return dict; |
| 3681 | } |
| 3682 | |
| 3683 | int |
| 3684 | _PyObjectDict_SetItem(PyTypeObject *tp, PyObject **dictptr, |
| 3685 | PyObject *key, PyObject *value) |
| 3686 | { |
| 3687 | PyObject *dict; |
| 3688 | int res; |
| 3689 | PyDictKeysObject *cached; |
| 3690 | |
| 3691 | assert(dictptr != NULL); |
| 3692 | if ((tp->tp_flags & Py_TPFLAGS_HEAPTYPE) && (cached = CACHED_KEYS(tp))) { |
| 3693 | assert(dictptr != NULL); |
| 3694 | dict = *dictptr; |
| 3695 | if (dict == NULL) { |
| 3696 | DK_INCREF(cached); |
| 3697 | dict = new_dict_with_shared_keys(cached); |
| 3698 | if (dict == NULL) |
| 3699 | return -1; |
| 3700 | *dictptr = dict; |
| 3701 | } |
| 3702 | if (value == NULL) { |
| 3703 | res = PyDict_DelItem(dict, key); |
| 3704 | if (cached != ((PyDictObject *)dict)->ma_keys) { |
| 3705 | CACHED_KEYS(tp) = NULL; |
| 3706 | DK_DECREF(cached); |
| 3707 | } |
| 3708 | } else { |
| 3709 | res = PyDict_SetItem(dict, key, value); |
| 3710 | if (cached != ((PyDictObject *)dict)->ma_keys) { |
| 3711 | /* Either update tp->ht_cached_keys or delete it */ |
Benjamin Peterson | 15ee821 | 2012-04-24 14:44:18 -0400 | [diff] [blame] | 3712 | if (cached->dk_refcnt == 1) { |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 3713 | CACHED_KEYS(tp) = make_keys_shared(dict); |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 3714 | } else { |
| 3715 | CACHED_KEYS(tp) = NULL; |
| 3716 | } |
| 3717 | DK_DECREF(cached); |
Benjamin Peterson | 15ee821 | 2012-04-24 14:44:18 -0400 | [diff] [blame] | 3718 | if (CACHED_KEYS(tp) == NULL && PyErr_Occurred()) |
| 3719 | return -1; |
Benjamin Peterson | 7d95e40 | 2012-04-23 11:24:50 -0400 | [diff] [blame] | 3720 | } |
| 3721 | } |
| 3722 | } else { |
| 3723 | dict = *dictptr; |
| 3724 | if (dict == NULL) { |
| 3725 | dict = PyDict_New(); |
| 3726 | if (dict == NULL) |
| 3727 | return -1; |
| 3728 | *dictptr = dict; |
| 3729 | } |
| 3730 | if (value == NULL) { |
| 3731 | res = PyDict_DelItem(dict, key); |
| 3732 | } else { |
| 3733 | res = PyDict_SetItem(dict, key, value); |
| 3734 | } |
| 3735 | } |
| 3736 | return res; |
| 3737 | } |
| 3738 | |
| 3739 | void |
| 3740 | _PyDictKeys_DecRef(PyDictKeysObject *keys) |
| 3741 | { |
| 3742 | DK_DECREF(keys); |
| 3743 | } |
| 3744 | |
| 3745 | |
| 3746 | /* ARGSUSED */ |
| 3747 | static PyObject * |
| 3748 | dummy_repr(PyObject *op) |
| 3749 | { |
| 3750 | return PyUnicode_FromString("<dummy key>"); |
| 3751 | } |
| 3752 | |
| 3753 | /* ARGUSED */ |
| 3754 | static void |
| 3755 | dummy_dealloc(PyObject* ignore) |
| 3756 | { |
| 3757 | /* This should never get called, but we also don't want to SEGV if |
| 3758 | * we accidentally decref dummy-key out of existence. |
| 3759 | */ |
| 3760 | Py_FatalError("deallocating <dummy key>"); |
| 3761 | } |
| 3762 | |
| 3763 | static PyTypeObject PyDictDummy_Type = { |
| 3764 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 3765 | "<dummy key> type", |
| 3766 | 0, |
| 3767 | 0, |
| 3768 | dummy_dealloc, /*tp_dealloc*/ /*never called*/ |
| 3769 | 0, /*tp_print*/ |
| 3770 | 0, /*tp_getattr*/ |
| 3771 | 0, /*tp_setattr*/ |
| 3772 | 0, /*tp_reserved*/ |
| 3773 | dummy_repr, /*tp_repr*/ |
| 3774 | 0, /*tp_as_number*/ |
| 3775 | 0, /*tp_as_sequence*/ |
| 3776 | 0, /*tp_as_mapping*/ |
| 3777 | 0, /*tp_hash */ |
| 3778 | 0, /*tp_call */ |
| 3779 | 0, /*tp_str */ |
| 3780 | 0, /*tp_getattro */ |
| 3781 | 0, /*tp_setattro */ |
| 3782 | 0, /*tp_as_buffer */ |
| 3783 | Py_TPFLAGS_DEFAULT, /*tp_flags */ |
| 3784 | }; |
| 3785 | |
| 3786 | static PyObject _dummy_struct = { |
| 3787 | _PyObject_EXTRA_INIT |
| 3788 | 2, &PyDictDummy_Type |
| 3789 | }; |
| 3790 | |