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