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