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