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