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