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