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