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