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