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