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