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