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