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 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 10 | #include "Python.h" |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 11 | |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 12 | |
Georg Brandl | b9f4ad3 | 2006-10-29 18:31:42 +0000 | [diff] [blame] | 13 | /* Set a key error with the specified argument, wrapping it in a |
| 14 | * tuple automatically so that tuple keys are not unpacked as the |
| 15 | * exception arguments. */ |
| 16 | static void |
| 17 | set_key_error(PyObject *arg) |
| 18 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 19 | PyObject *tup; |
| 20 | tup = PyTuple_Pack(1, arg); |
| 21 | if (!tup) |
| 22 | return; /* caller will expect error to be set anyway */ |
| 23 | PyErr_SetObject(PyExc_KeyError, tup); |
| 24 | Py_DECREF(tup); |
Georg Brandl | b9f4ad3 | 2006-10-29 18:31:42 +0000 | [diff] [blame] | 25 | } |
| 26 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 27 | /* Define this out if you don't want conversion statistics on exit. */ |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 28 | #undef SHOW_CONVERSION_COUNTS |
| 29 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 30 | /* See large comment block below. This must be >= 1. */ |
| 31 | #define PERTURB_SHIFT 5 |
| 32 | |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 33 | /* |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 34 | Major subtleties ahead: Most hash schemes depend on having a "good" hash |
| 35 | function, in the sense of simulating randomness. Python doesn't: its most |
| 36 | important hash functions (for strings and ints) are very regular in common |
| 37 | cases: |
Tim Peters | 15d4929 | 2001-05-27 07:39:22 +0000 | [diff] [blame] | 38 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 39 | >>> map(hash, (0, 1, 2, 3)) |
| 40 | [0, 1, 2, 3] |
| 41 | >>> map(hash, ("namea", "nameb", "namec", "named")) |
| 42 | [-1658398457, -1658398460, -1658398459, -1658398462] |
| 43 | >>> |
Tim Peters | 15d4929 | 2001-05-27 07:39:22 +0000 | [diff] [blame] | 44 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 45 | This isn't necessarily bad! To the contrary, in a table of size 2**i, taking |
| 46 | the low-order i bits as the initial table index is extremely fast, and there |
| 47 | are no collisions at all for dicts indexed by a contiguous range of ints. |
| 48 | The same is approximately true when keys are "consecutive" strings. So this |
| 49 | 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] | 50 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 51 | OTOH, when collisions occur, the tendency to fill contiguous slices of the |
| 52 | hash table makes a good collision resolution strategy crucial. Taking only |
| 53 | the last i bits of the hash code is also vulnerable: for example, consider |
| 54 | [i << 16 for i in range(20000)] as a set of keys. Since ints are their own |
| 55 | hash codes, and this fits in a dict of size 2**15, the last 15 bits of every |
| 56 | 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] | 57 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 58 | But catering to unusual cases should not slow the usual ones, so we just take |
| 59 | the last i bits anyway. It's up to collision resolution to do the rest. If |
| 60 | we *usually* find the key we're looking for on the first try (and, it turns |
| 61 | out, we usually do -- the table load factor is kept under 2/3, so the odds |
| 62 | are solidly in our favor), then it makes best sense to keep the initial index |
| 63 | computation dirt cheap. |
Tim Peters | 15d4929 | 2001-05-27 07:39:22 +0000 | [diff] [blame] | 64 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 65 | The first half of collision resolution is to visit table indices via this |
| 66 | recurrence: |
Tim Peters | 15d4929 | 2001-05-27 07:39:22 +0000 | [diff] [blame] | 67 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 68 | j = ((5*j) + 1) mod 2**i |
Tim Peters | 15d4929 | 2001-05-27 07:39:22 +0000 | [diff] [blame] | 69 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 70 | For any initial j in range(2**i), repeating that 2**i times generates each |
| 71 | int in range(2**i) exactly once (see any text on random-number generation for |
| 72 | proof). By itself, this doesn't help much: like linear probing (setting |
| 73 | j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed |
| 74 | order. This would be bad, except that's not the only thing we do, and it's |
| 75 | actually *good* in the common cases where hash keys are consecutive. In an |
| 76 | example that's really too small to make this entirely clear, for a table of |
| 77 | size 2**3 the order of indices is: |
Tim Peters | 15d4929 | 2001-05-27 07:39:22 +0000 | [diff] [blame] | 78 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 79 | 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating] |
| 80 | |
| 81 | If two things come in at index 5, the first place we look after is index 2, |
| 82 | not 6, so if another comes in at index 6 the collision at 5 didn't hurt it. |
| 83 | Linear probing is deadly in this case because there the fixed probe order |
| 84 | is the *same* as the order consecutive keys are likely to arrive. But it's |
| 85 | extremely unlikely hash codes will follow a 5*j+1 recurrence by accident, |
| 86 | and certain that consecutive hash codes do not. |
| 87 | |
| 88 | The other half of the strategy is to get the other bits of the hash code |
| 89 | into play. This is done by initializing a (unsigned) vrbl "perturb" to the |
| 90 | full hash code, and changing the recurrence to: |
| 91 | |
| 92 | j = (5*j) + 1 + perturb; |
| 93 | perturb >>= PERTURB_SHIFT; |
| 94 | use j % 2**i as the next table index; |
| 95 | |
| 96 | Now the probe sequence depends (eventually) on every bit in the hash code, |
| 97 | and the pseudo-scrambling property of recurring on 5*j+1 is more valuable, |
| 98 | because it quickly magnifies small differences in the bits that didn't affect |
| 99 | the initial index. Note that because perturb is unsigned, if the recurrence |
| 100 | is executed often enough perturb eventually becomes and remains 0. At that |
| 101 | point (very rarely reached) the recurrence is on (just) 5*j+1 again, and |
| 102 | that's certain to find an empty slot eventually (since it generates every int |
| 103 | in range(2**i), and we make sure there's always at least one empty slot). |
| 104 | |
| 105 | Selecting a good value for PERTURB_SHIFT is a balancing act. You want it |
| 106 | small so that the high bits of the hash code continue to affect the probe |
| 107 | sequence across iterations; but you want it large so that in really bad cases |
| 108 | the high-order hash bits have an effect on early iterations. 5 was "the |
| 109 | best" in minimizing total collisions across experiments Tim Peters ran (on |
| 110 | both normal and pathological cases), but 4 and 6 weren't significantly worse. |
| 111 | |
| 112 | Historical: Reimer Behrends contributed the idea of using a polynomial-based |
| 113 | approach, using repeated multiplication by x in GF(2**n) where an irreducible |
| 114 | polynomial for each table size was chosen such that x was a primitive root. |
| 115 | Christian Tismer later extended that to use division by x instead, as an |
| 116 | efficient way to get the high bits of the hash code into play. This scheme |
| 117 | also gave excellent collision statistics, but was more expensive: two |
| 118 | if-tests were required inside the loop; computing "the next" index took about |
| 119 | the same number of operations but without as much potential parallelism |
| 120 | (e.g., computing 5*j can go on at the same time as computing 1+perturb in the |
| 121 | above, and then shifting perturb can be done while the table index is being |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 122 | masked); and the PyDictObject struct required a member to hold the table's |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 123 | polynomial. In Tim's experiments the current scheme ran faster, produced |
| 124 | equally good collision statistics, needed less code & used less memory. |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 125 | |
| 126 | Theoretical Python 2.5 headache: hash codes are only C "long", but |
| 127 | sizeof(Py_ssize_t) > sizeof(long) may be possible. In that case, and if a |
| 128 | dict is genuinely huge, then only the slots directly reachable via indexing |
| 129 | by a C long can be the first slot in a probe sequence. The probe sequence |
| 130 | will still eventually reach every slot in the table, but the collision rate |
| 131 | on initial probes may be much higher than this scheme was designed for. |
| 132 | Getting a hash code as fat as Py_ssize_t is the only real cure. But in |
| 133 | practice, this probably won't make a lick of difference for many years (at |
| 134 | which point everyone will have terabytes of RAM on 64-bit boxes). |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 135 | */ |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 136 | |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 137 | /* Object used as dummy key to fill deleted entries */ |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 138 | static PyObject *dummy = NULL; /* Initialized by first call to newPyDictObject() */ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 139 | |
Armin Rigo | e170937 | 2006-04-12 17:06:05 +0000 | [diff] [blame] | 140 | #ifdef Py_REF_DEBUG |
| 141 | PyObject * |
| 142 | _PyDict_Dummy(void) |
| 143 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 144 | return dummy; |
Armin Rigo | e170937 | 2006-04-12 17:06:05 +0000 | [diff] [blame] | 145 | } |
| 146 | #endif |
| 147 | |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 148 | /* forward declarations */ |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 149 | static PyDictEntry * |
| 150 | lookdict_string(PyDictObject *mp, PyObject *key, long hash); |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 151 | |
| 152 | #ifdef SHOW_CONVERSION_COUNTS |
| 153 | static long created = 0L; |
| 154 | static long converted = 0L; |
| 155 | |
| 156 | static void |
| 157 | show_counts(void) |
| 158 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 159 | fprintf(stderr, "created %ld string dicts\n", created); |
| 160 | fprintf(stderr, "converted %ld to normal dicts\n", converted); |
| 161 | fprintf(stderr, "%.2f%% conversion rate\n", (100.0*converted)/created); |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 162 | } |
| 163 | #endif |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 164 | |
Christian Heimes | b4ee4a1 | 2008-02-07 17:15:30 +0000 | [diff] [blame] | 165 | /* Debug statistic to compare allocations with reuse through the free list */ |
| 166 | #undef SHOW_ALLOC_COUNT |
| 167 | #ifdef SHOW_ALLOC_COUNT |
| 168 | static size_t count_alloc = 0; |
| 169 | static size_t count_reuse = 0; |
| 170 | |
| 171 | static void |
| 172 | show_alloc(void) |
| 173 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 174 | fprintf(stderr, "Dict allocations: %" PY_FORMAT_SIZE_T "d\n", |
| 175 | count_alloc); |
| 176 | fprintf(stderr, "Dict reuse through freelist: %" PY_FORMAT_SIZE_T |
| 177 | "d\n", count_reuse); |
| 178 | fprintf(stderr, "%.2f%% reuse rate\n\n", |
| 179 | (100.0*count_reuse/(count_alloc+count_reuse))); |
Christian Heimes | b4ee4a1 | 2008-02-07 17:15:30 +0000 | [diff] [blame] | 180 | } |
| 181 | #endif |
| 182 | |
Antoine Pitrou | f8387af | 2009-03-23 18:41:45 +0000 | [diff] [blame] | 183 | /* Debug statistic to count GC tracking of dicts */ |
| 184 | #ifdef SHOW_TRACK_COUNT |
| 185 | static Py_ssize_t count_untracked = 0; |
| 186 | static Py_ssize_t count_tracked = 0; |
| 187 | |
| 188 | static void |
| 189 | show_track(void) |
| 190 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 191 | fprintf(stderr, "Dicts created: %" PY_FORMAT_SIZE_T "d\n", |
| 192 | count_tracked + count_untracked); |
| 193 | fprintf(stderr, "Dicts tracked by the GC: %" PY_FORMAT_SIZE_T |
| 194 | "d\n", count_tracked); |
| 195 | fprintf(stderr, "%.2f%% dict tracking rate\n\n", |
| 196 | (100.0*count_tracked/(count_untracked+count_tracked))); |
Antoine Pitrou | f8387af | 2009-03-23 18:41:45 +0000 | [diff] [blame] | 197 | } |
| 198 | #endif |
| 199 | |
| 200 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 201 | /* Initialization macros. |
| 202 | There are two ways to create a dict: PyDict_New() is the main C API |
| 203 | function, and the tp_new slot maps to dict_new(). In the latter case we |
| 204 | can save a little time over what PyDict_New does because it's guaranteed |
| 205 | that the PyDictObject struct is already zeroed out. |
| 206 | Everyone except dict_new() should use EMPTY_TO_MINSIZE (unless they have |
| 207 | an excellent reason not to). |
| 208 | */ |
| 209 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 210 | #define INIT_NONZERO_DICT_SLOTS(mp) do { \ |
| 211 | (mp)->ma_table = (mp)->ma_smalltable; \ |
| 212 | (mp)->ma_mask = PyDict_MINSIZE - 1; \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 213 | } while(0) |
| 214 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 215 | #define EMPTY_TO_MINSIZE(mp) do { \ |
| 216 | memset((mp)->ma_smalltable, 0, sizeof((mp)->ma_smalltable)); \ |
| 217 | (mp)->ma_used = (mp)->ma_fill = 0; \ |
| 218 | INIT_NONZERO_DICT_SLOTS(mp); \ |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 219 | } while(0) |
| 220 | |
Raymond Hettinger | 4344278 | 2004-03-17 21:55:03 +0000 | [diff] [blame] | 221 | /* Dictionary reuse scheme to save calls to malloc, free, and memset */ |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 222 | #ifndef PyDict_MAXFREELIST |
| 223 | #define PyDict_MAXFREELIST 80 |
| 224 | #endif |
| 225 | static PyDictObject *free_list[PyDict_MAXFREELIST]; |
| 226 | static int numfree = 0; |
Raymond Hettinger | 4344278 | 2004-03-17 21:55:03 +0000 | [diff] [blame] | 227 | |
Christian Heimes | f75dbef | 2008-02-08 00:11:31 +0000 | [diff] [blame] | 228 | void |
| 229 | PyDict_Fini(void) |
| 230 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 231 | PyDictObject *op; |
Christian Heimes | f75dbef | 2008-02-08 00:11:31 +0000 | [diff] [blame] | 232 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 233 | while (numfree) { |
| 234 | op = free_list[--numfree]; |
| 235 | assert(PyDict_CheckExact(op)); |
| 236 | PyObject_GC_Del(op); |
| 237 | } |
Christian Heimes | f75dbef | 2008-02-08 00:11:31 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 240 | PyObject * |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 241 | PyDict_New(void) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 242 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 243 | register PyDictObject *mp; |
| 244 | if (dummy == NULL) { /* Auto-initialize dummy */ |
| 245 | dummy = PyString_FromString("<dummy key>"); |
| 246 | if (dummy == NULL) |
| 247 | return NULL; |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 248 | #ifdef SHOW_CONVERSION_COUNTS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 249 | Py_AtExit(show_counts); |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 250 | #endif |
Christian Heimes | b4ee4a1 | 2008-02-07 17:15:30 +0000 | [diff] [blame] | 251 | #ifdef SHOW_ALLOC_COUNT |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 252 | Py_AtExit(show_alloc); |
Christian Heimes | b4ee4a1 | 2008-02-07 17:15:30 +0000 | [diff] [blame] | 253 | #endif |
Antoine Pitrou | f8387af | 2009-03-23 18:41:45 +0000 | [diff] [blame] | 254 | #ifdef SHOW_TRACK_COUNT |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 255 | Py_AtExit(show_track); |
Antoine Pitrou | f8387af | 2009-03-23 18:41:45 +0000 | [diff] [blame] | 256 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 257 | } |
| 258 | if (numfree) { |
| 259 | mp = free_list[--numfree]; |
| 260 | assert (mp != NULL); |
| 261 | assert (Py_TYPE(mp) == &PyDict_Type); |
| 262 | _Py_NewReference((PyObject *)mp); |
| 263 | if (mp->ma_fill) { |
| 264 | EMPTY_TO_MINSIZE(mp); |
| 265 | } else { |
| 266 | /* At least set ma_table and ma_mask; these are wrong |
| 267 | if an empty but presized dict is added to freelist */ |
| 268 | INIT_NONZERO_DICT_SLOTS(mp); |
| 269 | } |
| 270 | assert (mp->ma_used == 0); |
| 271 | assert (mp->ma_table == mp->ma_smalltable); |
| 272 | assert (mp->ma_mask == PyDict_MINSIZE - 1); |
Christian Heimes | b4ee4a1 | 2008-02-07 17:15:30 +0000 | [diff] [blame] | 273 | #ifdef SHOW_ALLOC_COUNT |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 274 | count_reuse++; |
Christian Heimes | b4ee4a1 | 2008-02-07 17:15:30 +0000 | [diff] [blame] | 275 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 276 | } else { |
| 277 | mp = PyObject_GC_New(PyDictObject, &PyDict_Type); |
| 278 | if (mp == NULL) |
| 279 | return NULL; |
| 280 | EMPTY_TO_MINSIZE(mp); |
Christian Heimes | b4ee4a1 | 2008-02-07 17:15:30 +0000 | [diff] [blame] | 281 | #ifdef SHOW_ALLOC_COUNT |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 282 | count_alloc++; |
Christian Heimes | b4ee4a1 | 2008-02-07 17:15:30 +0000 | [diff] [blame] | 283 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 284 | } |
| 285 | mp->ma_lookup = lookdict_string; |
Antoine Pitrou | f8387af | 2009-03-23 18:41:45 +0000 | [diff] [blame] | 286 | #ifdef SHOW_TRACK_COUNT |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 287 | count_untracked++; |
Antoine Pitrou | f8387af | 2009-03-23 18:41:45 +0000 | [diff] [blame] | 288 | #endif |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 289 | #ifdef SHOW_CONVERSION_COUNTS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 290 | ++created; |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 291 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 292 | return (PyObject *)mp; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | /* |
| 296 | The basic lookup function used by all operations. |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 297 | 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] | 298 | Open addressing is preferred over chaining since the link overhead for |
| 299 | chaining would be substantial (100% with typical malloc overhead). |
| 300 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 301 | The initial probe index is computed as hash mod the table size. Subsequent |
| 302 | probe indices are computed as explained earlier. |
Guido van Rossum | 2bc1379 | 1999-03-24 19:06:42 +0000 | [diff] [blame] | 303 | |
| 304 | All arithmetic on hash should ignore overflow. |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 305 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 306 | (The details in this version are due to Tim Peters, building on many past |
| 307 | contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and |
| 308 | Christian Tismer). |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 309 | |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 310 | lookdict() is general-purpose, and may return NULL if (and only if) a |
| 311 | comparison raises an exception (this was new in Python 2.5). |
| 312 | lookdict_string() below is specialized to string keys, comparison of which can |
| 313 | never raise an exception; that function can never return NULL. For both, when |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 314 | the key isn't found a PyDictEntry* is returned for which the me_value field is |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 315 | NULL; this is the slot in the dict at which the key would have been found, and |
| 316 | the caller can (if it wishes) add the <key, value> pair to the returned |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 317 | PyDictEntry*. |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 318 | */ |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 319 | static PyDictEntry * |
| 320 | lookdict(PyDictObject *mp, PyObject *key, register long hash) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 321 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 322 | register size_t i; |
| 323 | register size_t perturb; |
| 324 | register PyDictEntry *freeslot; |
| 325 | register size_t mask = (size_t)mp->ma_mask; |
| 326 | PyDictEntry *ep0 = mp->ma_table; |
| 327 | register PyDictEntry *ep; |
| 328 | register int cmp; |
| 329 | PyObject *startkey; |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 330 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 331 | i = (size_t)hash & mask; |
| 332 | ep = &ep0[i]; |
| 333 | if (ep->me_key == NULL || ep->me_key == key) |
| 334 | return ep; |
Tim Peters | 7b5d0af | 2001-06-03 04:14:43 +0000 | [diff] [blame] | 335 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 336 | if (ep->me_key == dummy) |
| 337 | freeslot = ep; |
| 338 | else { |
| 339 | if (ep->me_hash == hash) { |
| 340 | startkey = ep->me_key; |
| 341 | Py_INCREF(startkey); |
| 342 | cmp = PyObject_RichCompareBool(startkey, key, Py_EQ); |
| 343 | Py_DECREF(startkey); |
| 344 | if (cmp < 0) |
| 345 | return NULL; |
| 346 | if (ep0 == mp->ma_table && ep->me_key == startkey) { |
| 347 | if (cmp > 0) |
| 348 | return ep; |
| 349 | } |
| 350 | else { |
| 351 | /* The compare did major nasty stuff to the |
| 352 | * dict: start over. |
| 353 | * XXX A clever adversary could prevent this |
| 354 | * XXX from terminating. |
| 355 | */ |
| 356 | return lookdict(mp, key, hash); |
| 357 | } |
| 358 | } |
| 359 | freeslot = NULL; |
| 360 | } |
Tim Peters | 15d4929 | 2001-05-27 07:39:22 +0000 | [diff] [blame] | 361 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 362 | /* In the loop, me_key == dummy is by far (factor of 100s) the |
| 363 | least likely outcome, so test for that last. */ |
| 364 | for (perturb = hash; ; perturb >>= PERTURB_SHIFT) { |
| 365 | i = (i << 2) + i + perturb + 1; |
| 366 | ep = &ep0[i & mask]; |
| 367 | if (ep->me_key == NULL) |
| 368 | return freeslot == NULL ? ep : freeslot; |
| 369 | if (ep->me_key == key) |
| 370 | return ep; |
| 371 | if (ep->me_hash == hash && ep->me_key != dummy) { |
| 372 | startkey = ep->me_key; |
| 373 | Py_INCREF(startkey); |
| 374 | cmp = PyObject_RichCompareBool(startkey, key, Py_EQ); |
| 375 | Py_DECREF(startkey); |
| 376 | if (cmp < 0) |
| 377 | return NULL; |
| 378 | if (ep0 == mp->ma_table && ep->me_key == startkey) { |
| 379 | if (cmp > 0) |
| 380 | return ep; |
| 381 | } |
| 382 | else { |
| 383 | /* The compare did major nasty stuff to the |
| 384 | * dict: start over. |
| 385 | * XXX A clever adversary could prevent this |
| 386 | * XXX from terminating. |
| 387 | */ |
| 388 | return lookdict(mp, key, hash); |
| 389 | } |
| 390 | } |
| 391 | else if (ep->me_key == dummy && freeslot == NULL) |
| 392 | freeslot = ep; |
| 393 | } |
| 394 | assert(0); /* NOT REACHED */ |
| 395 | return 0; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | /* |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 399 | * Hacked up version of lookdict which can assume keys are always strings; |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 400 | * this assumption allows testing for errors during PyObject_RichCompareBool() |
| 401 | * to be dropped; string-string comparisons never raise exceptions. This also |
| 402 | * means we don't need to go through PyObject_RichCompareBool(); we can always |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 403 | * use _PyString_Eq() directly. |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 404 | * |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 405 | * This is valuable because dicts with only string keys are very common. |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 406 | */ |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 407 | static PyDictEntry * |
| 408 | lookdict_string(PyDictObject *mp, PyObject *key, register long hash) |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 409 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 410 | register size_t i; |
| 411 | register size_t perturb; |
| 412 | register PyDictEntry *freeslot; |
| 413 | register size_t mask = (size_t)mp->ma_mask; |
| 414 | PyDictEntry *ep0 = mp->ma_table; |
| 415 | register PyDictEntry *ep; |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 416 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 417 | /* Make sure this function doesn't have to handle non-string keys, |
| 418 | including subclasses of str; e.g., one reason to subclass |
| 419 | strings is to override __eq__, and for speed we don't cater to |
| 420 | that here. */ |
| 421 | if (!PyString_CheckExact(key)) { |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 422 | #ifdef SHOW_CONVERSION_COUNTS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 423 | ++converted; |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 424 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 425 | mp->ma_lookup = lookdict; |
| 426 | return lookdict(mp, key, hash); |
| 427 | } |
| 428 | i = hash & mask; |
| 429 | ep = &ep0[i]; |
| 430 | if (ep->me_key == NULL || ep->me_key == key) |
| 431 | return ep; |
| 432 | if (ep->me_key == dummy) |
| 433 | freeslot = ep; |
| 434 | else { |
| 435 | if (ep->me_hash == hash && _PyString_Eq(ep->me_key, key)) |
| 436 | return ep; |
| 437 | freeslot = NULL; |
| 438 | } |
Tim Peters | 15d4929 | 2001-05-27 07:39:22 +0000 | [diff] [blame] | 439 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 440 | /* In the loop, me_key == dummy is by far (factor of 100s) the |
| 441 | least likely outcome, so test for that last. */ |
| 442 | for (perturb = hash; ; perturb >>= PERTURB_SHIFT) { |
| 443 | i = (i << 2) + i + perturb + 1; |
| 444 | ep = &ep0[i & mask]; |
| 445 | if (ep->me_key == NULL) |
| 446 | return freeslot == NULL ? ep : freeslot; |
| 447 | if (ep->me_key == key |
| 448 | || (ep->me_hash == hash |
| 449 | && ep->me_key != dummy |
| 450 | && _PyString_Eq(ep->me_key, key))) |
| 451 | return ep; |
| 452 | if (ep->me_key == dummy && freeslot == NULL) |
| 453 | freeslot = ep; |
| 454 | } |
| 455 | assert(0); /* NOT REACHED */ |
| 456 | return 0; |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Antoine Pitrou | f8387af | 2009-03-23 18:41:45 +0000 | [diff] [blame] | 459 | #ifdef SHOW_TRACK_COUNT |
| 460 | #define INCREASE_TRACK_COUNT \ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 461 | (count_tracked++, count_untracked--); |
Antoine Pitrou | f8387af | 2009-03-23 18:41:45 +0000 | [diff] [blame] | 462 | #define DECREASE_TRACK_COUNT \ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 463 | (count_tracked--, count_untracked++); |
Antoine Pitrou | f8387af | 2009-03-23 18:41:45 +0000 | [diff] [blame] | 464 | #else |
| 465 | #define INCREASE_TRACK_COUNT |
| 466 | #define DECREASE_TRACK_COUNT |
| 467 | #endif |
| 468 | |
| 469 | #define MAINTAIN_TRACKING(mp, key, value) \ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 470 | do { \ |
| 471 | if (!_PyObject_GC_IS_TRACKED(mp)) { \ |
| 472 | if (_PyObject_GC_MAY_BE_TRACKED(key) || \ |
| 473 | _PyObject_GC_MAY_BE_TRACKED(value)) { \ |
| 474 | _PyObject_GC_TRACK(mp); \ |
| 475 | INCREASE_TRACK_COUNT \ |
| 476 | } \ |
| 477 | } \ |
| 478 | } while(0) |
Antoine Pitrou | f8387af | 2009-03-23 18:41:45 +0000 | [diff] [blame] | 479 | |
| 480 | void |
| 481 | _PyDict_MaybeUntrack(PyObject *op) |
| 482 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 483 | PyDictObject *mp; |
| 484 | PyObject *value; |
| 485 | Py_ssize_t mask, i; |
| 486 | PyDictEntry *ep; |
Antoine Pitrou | f8387af | 2009-03-23 18:41:45 +0000 | [diff] [blame] | 487 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 488 | if (!PyDict_CheckExact(op) || !_PyObject_GC_IS_TRACKED(op)) |
| 489 | return; |
| 490 | |
| 491 | mp = (PyDictObject *) op; |
| 492 | ep = mp->ma_table; |
| 493 | mask = mp->ma_mask; |
| 494 | for (i = 0; i <= mask; i++) { |
| 495 | if ((value = ep[i].me_value) == NULL) |
| 496 | continue; |
| 497 | if (_PyObject_GC_MAY_BE_TRACKED(value) || |
| 498 | _PyObject_GC_MAY_BE_TRACKED(ep[i].me_key)) |
| 499 | return; |
| 500 | } |
| 501 | DECREASE_TRACK_COUNT |
| 502 | _PyObject_GC_UNTRACK(op); |
Antoine Pitrou | f8387af | 2009-03-23 18:41:45 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 505 | /* |
Antoine Pitrou | 6a1cd1b | 2012-02-27 00:45:12 +0100 | [diff] [blame] | 506 | Internal routine to insert a new item into the table when you have entry object. |
| 507 | Used by insertdict. |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 508 | */ |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 509 | static int |
Antoine Pitrou | 6a1cd1b | 2012-02-27 00:45:12 +0100 | [diff] [blame] | 510 | insertdict_by_entry(register PyDictObject *mp, PyObject *key, long hash, |
| 511 | PyDictEntry *ep, PyObject *value) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 512 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 513 | PyObject *old_value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 514 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 515 | MAINTAIN_TRACKING(mp, key, value); |
| 516 | if (ep->me_value != NULL) { |
| 517 | old_value = ep->me_value; |
| 518 | ep->me_value = value; |
| 519 | Py_DECREF(old_value); /* which **CAN** re-enter */ |
| 520 | Py_DECREF(key); |
| 521 | } |
| 522 | else { |
| 523 | if (ep->me_key == NULL) |
| 524 | mp->ma_fill++; |
| 525 | else { |
| 526 | assert(ep->me_key == dummy); |
| 527 | Py_DECREF(dummy); |
| 528 | } |
| 529 | ep->me_key = key; |
| 530 | ep->me_hash = (Py_ssize_t)hash; |
| 531 | ep->me_value = value; |
| 532 | mp->ma_used++; |
| 533 | } |
| 534 | return 0; |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 535 | } |
| 536 | |
Antoine Pitrou | 6a1cd1b | 2012-02-27 00:45:12 +0100 | [diff] [blame] | 537 | |
| 538 | /* |
| 539 | Internal routine to insert a new item into the table. |
| 540 | Used both by the internal resize routine and by the public insert routine. |
| 541 | Eats a reference to key and one to value. |
| 542 | Returns -1 if an error occurred, or 0 on success. |
| 543 | */ |
| 544 | static int |
| 545 | insertdict(register PyDictObject *mp, PyObject *key, long hash, PyObject *value) |
| 546 | { |
| 547 | register PyDictEntry *ep; |
| 548 | |
| 549 | assert(mp->ma_lookup != NULL); |
| 550 | ep = mp->ma_lookup(mp, key, hash); |
| 551 | if (ep == NULL) { |
| 552 | Py_DECREF(key); |
| 553 | Py_DECREF(value); |
| 554 | return -1; |
| 555 | } |
| 556 | return insertdict_by_entry(mp, key, hash, ep, value); |
| 557 | } |
| 558 | |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 559 | /* |
| 560 | Internal routine used by dictresize() to insert an item which is |
| 561 | known to be absent from the dict. This routine also assumes that |
| 562 | the dict contains no deleted entries. Besides the performance benefit, |
| 563 | using insertdict() in dictresize() is dangerous (SF bug #1456209). |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 564 | Note that no refcounts are changed by this routine; if needed, the caller |
| 565 | is responsible for incref'ing `key` and `value`. |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 566 | */ |
| 567 | static void |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 568 | insertdict_clean(register PyDictObject *mp, PyObject *key, long hash, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 569 | PyObject *value) |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 570 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 571 | register size_t i; |
| 572 | register size_t perturb; |
| 573 | register size_t mask = (size_t)mp->ma_mask; |
| 574 | PyDictEntry *ep0 = mp->ma_table; |
| 575 | register PyDictEntry *ep; |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 576 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 577 | MAINTAIN_TRACKING(mp, key, value); |
| 578 | i = hash & mask; |
| 579 | ep = &ep0[i]; |
| 580 | for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) { |
| 581 | i = (i << 2) + i + perturb + 1; |
| 582 | ep = &ep0[i & mask]; |
| 583 | } |
| 584 | assert(ep->me_value == NULL); |
| 585 | mp->ma_fill++; |
| 586 | ep->me_key = key; |
| 587 | ep->me_hash = (Py_ssize_t)hash; |
| 588 | ep->me_value = value; |
| 589 | mp->ma_used++; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | /* |
| 593 | Restructure the table by allocating a new table and reinserting all |
| 594 | items again. When entries have been deleted, the new table may |
| 595 | actually be smaller than the old one. |
| 596 | */ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 597 | static int |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 598 | dictresize(PyDictObject *mp, Py_ssize_t minused) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 599 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 600 | Py_ssize_t newsize; |
| 601 | PyDictEntry *oldtable, *newtable, *ep; |
| 602 | Py_ssize_t i; |
| 603 | int is_oldtable_malloced; |
| 604 | PyDictEntry small_copy[PyDict_MINSIZE]; |
Tim Peters | 91a364d | 2001-05-19 07:04:38 +0000 | [diff] [blame] | 605 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 606 | assert(minused >= 0); |
Tim Peters | 0c6010b | 2001-05-23 23:33:57 +0000 | [diff] [blame] | 607 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 608 | /* Find the smallest table size > minused. */ |
| 609 | for (newsize = PyDict_MINSIZE; |
| 610 | newsize <= minused && newsize > 0; |
| 611 | newsize <<= 1) |
| 612 | ; |
| 613 | if (newsize <= 0) { |
| 614 | PyErr_NoMemory(); |
| 615 | return -1; |
| 616 | } |
Tim Peters | 0c6010b | 2001-05-23 23:33:57 +0000 | [diff] [blame] | 617 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 618 | /* Get space for a new table. */ |
| 619 | oldtable = mp->ma_table; |
| 620 | assert(oldtable != NULL); |
| 621 | is_oldtable_malloced = oldtable != mp->ma_smalltable; |
Tim Peters | 0c6010b | 2001-05-23 23:33:57 +0000 | [diff] [blame] | 622 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 623 | if (newsize == PyDict_MINSIZE) { |
| 624 | /* A large table is shrinking, or we can't get any smaller. */ |
| 625 | newtable = mp->ma_smalltable; |
| 626 | if (newtable == oldtable) { |
| 627 | if (mp->ma_fill == mp->ma_used) { |
| 628 | /* No dummies, so no point doing anything. */ |
| 629 | return 0; |
| 630 | } |
| 631 | /* We're not going to resize it, but rebuild the |
| 632 | table anyway to purge old dummy entries. |
| 633 | Subtle: This is *necessary* if fill==size, |
| 634 | as lookdict needs at least one virgin slot to |
| 635 | terminate failing searches. If fill < size, it's |
| 636 | merely desirable, as dummies slow searches. */ |
| 637 | assert(mp->ma_fill > mp->ma_used); |
| 638 | memcpy(small_copy, oldtable, sizeof(small_copy)); |
| 639 | oldtable = small_copy; |
| 640 | } |
| 641 | } |
| 642 | else { |
| 643 | newtable = PyMem_NEW(PyDictEntry, newsize); |
| 644 | if (newtable == NULL) { |
| 645 | PyErr_NoMemory(); |
| 646 | return -1; |
| 647 | } |
| 648 | } |
Tim Peters | 0c6010b | 2001-05-23 23:33:57 +0000 | [diff] [blame] | 649 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 650 | /* Make the dict empty, using the new table. */ |
| 651 | assert(newtable != oldtable); |
| 652 | mp->ma_table = newtable; |
| 653 | mp->ma_mask = newsize - 1; |
| 654 | memset(newtable, 0, sizeof(PyDictEntry) * newsize); |
| 655 | mp->ma_used = 0; |
| 656 | i = mp->ma_fill; |
| 657 | mp->ma_fill = 0; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 658 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 659 | /* Copy the data over; this is refcount-neutral for active entries; |
| 660 | dummy entries aren't copied over, of course */ |
| 661 | for (ep = oldtable; i > 0; ep++) { |
| 662 | if (ep->me_value != NULL) { /* active entry */ |
| 663 | --i; |
| 664 | insertdict_clean(mp, ep->me_key, (long)ep->me_hash, |
| 665 | ep->me_value); |
| 666 | } |
| 667 | else if (ep->me_key != NULL) { /* dummy entry */ |
| 668 | --i; |
| 669 | assert(ep->me_key == dummy); |
| 670 | Py_DECREF(ep->me_key); |
| 671 | } |
| 672 | /* else key == value == NULL: nothing to do */ |
| 673 | } |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 674 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 675 | if (is_oldtable_malloced) |
| 676 | PyMem_DEL(oldtable); |
| 677 | return 0; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 678 | } |
| 679 | |
Raymond Hettinger | fd7ed40 | 2007-12-18 21:24:09 +0000 | [diff] [blame] | 680 | /* Create a new dictionary pre-sized to hold an estimated number of elements. |
| 681 | Underestimates are okay because the dictionary will resize as necessary. |
| 682 | Overestimates just mean the dictionary will be more sparse than usual. |
| 683 | */ |
| 684 | |
| 685 | PyObject * |
| 686 | _PyDict_NewPresized(Py_ssize_t minused) |
| 687 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 688 | PyObject *op = PyDict_New(); |
Raymond Hettinger | fd7ed40 | 2007-12-18 21:24:09 +0000 | [diff] [blame] | 689 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 690 | if (minused>5 && op != NULL && dictresize((PyDictObject *)op, minused) == -1) { |
| 691 | Py_DECREF(op); |
| 692 | return NULL; |
| 693 | } |
| 694 | return op; |
Raymond Hettinger | fd7ed40 | 2007-12-18 21:24:09 +0000 | [diff] [blame] | 695 | } |
| 696 | |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 697 | /* Note that, for historical reasons, PyDict_GetItem() suppresses all errors |
| 698 | * that may occur (originally dicts supported only string keys, and exceptions |
| 699 | * weren't possible). So, while the original intent was that a NULL return |
Andrew M. Kuchling | 0067b5f | 2006-08-04 20:37:43 +0000 | [diff] [blame] | 700 | * meant the key wasn't present, in reality it can mean that, or that an error |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 701 | * (suppressed) occurred while computing the key's hash, or that some error |
| 702 | * (suppressed) occurred when comparing keys in the dict's internal probe |
| 703 | * sequence. A nasty example of the latter is when a Python-coded comparison |
| 704 | * function hits a stack-depth error, which can cause this to return NULL |
| 705 | * even if the key is present. |
| 706 | */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 707 | PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 708 | PyDict_GetItem(PyObject *op, PyObject *key) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 709 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 710 | long hash; |
| 711 | PyDictObject *mp = (PyDictObject *)op; |
| 712 | PyDictEntry *ep; |
| 713 | PyThreadState *tstate; |
| 714 | if (!PyDict_Check(op)) |
| 715 | return NULL; |
| 716 | if (!PyString_CheckExact(key) || |
| 717 | (hash = ((PyStringObject *) key)->ob_shash) == -1) |
| 718 | { |
| 719 | hash = PyObject_Hash(key); |
| 720 | if (hash == -1) { |
| 721 | PyErr_Clear(); |
| 722 | return NULL; |
| 723 | } |
| 724 | } |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 725 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 726 | /* We can arrive here with a NULL tstate during initialization: try |
| 727 | running "python -Wi" for an example related to string interning. |
| 728 | Let's just hope that no exception occurs then... This must be |
| 729 | _PyThreadState_Current and not PyThreadState_GET() because in debug |
| 730 | mode, the latter complains if tstate is NULL. */ |
| 731 | tstate = _PyThreadState_Current; |
| 732 | if (tstate != NULL && tstate->curexc_type != NULL) { |
| 733 | /* preserve the existing exception */ |
| 734 | PyObject *err_type, *err_value, *err_tb; |
| 735 | PyErr_Fetch(&err_type, &err_value, &err_tb); |
| 736 | ep = (mp->ma_lookup)(mp, key, hash); |
| 737 | /* ignore errors */ |
| 738 | PyErr_Restore(err_type, err_value, err_tb); |
| 739 | if (ep == NULL) |
| 740 | return NULL; |
| 741 | } |
| 742 | else { |
| 743 | ep = (mp->ma_lookup)(mp, key, hash); |
| 744 | if (ep == NULL) { |
| 745 | PyErr_Clear(); |
| 746 | return NULL; |
| 747 | } |
| 748 | } |
| 749 | return ep->me_value; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 750 | } |
| 751 | |
Serhiy Storchaka | 1c49617 | 2016-02-10 10:28:06 +0200 | [diff] [blame] | 752 | /* Variant of PyDict_GetItem() that doesn't suppress exceptions. |
| 753 | This returns NULL *with* an exception set if an exception occurred. |
| 754 | It returns NULL *without* an exception set if the key wasn't present. |
| 755 | */ |
| 756 | PyObject * |
| 757 | _PyDict_GetItemWithError(PyObject *op, PyObject *key) |
| 758 | { |
| 759 | long hash; |
| 760 | PyDictObject *mp = (PyDictObject *)op; |
| 761 | PyDictEntry *ep; |
| 762 | if (!PyDict_Check(op)) { |
| 763 | PyErr_BadInternalCall(); |
| 764 | return NULL; |
| 765 | } |
| 766 | if (!PyString_CheckExact(key) || |
| 767 | (hash = ((PyStringObject *) key)->ob_shash) == -1) |
| 768 | { |
| 769 | hash = PyObject_Hash(key); |
| 770 | if (hash == -1) { |
| 771 | return NULL; |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | ep = (mp->ma_lookup)(mp, key, hash); |
| 776 | if (ep == NULL) { |
| 777 | return NULL; |
| 778 | } |
| 779 | return ep->me_value; |
| 780 | } |
| 781 | |
Antoine Pitrou | 6a1cd1b | 2012-02-27 00:45:12 +0100 | [diff] [blame] | 782 | static int |
| 783 | dict_set_item_by_hash_or_entry(register PyObject *op, PyObject *key, |
| 784 | long hash, PyDictEntry *ep, PyObject *value) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 785 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 786 | register PyDictObject *mp; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 787 | register Py_ssize_t n_used; |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 788 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 789 | mp = (PyDictObject *)op; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 790 | assert(mp->ma_fill <= mp->ma_mask); /* at least one empty slot */ |
| 791 | n_used = mp->ma_used; |
| 792 | Py_INCREF(value); |
| 793 | Py_INCREF(key); |
Antoine Pitrou | 6a1cd1b | 2012-02-27 00:45:12 +0100 | [diff] [blame] | 794 | if (ep == NULL) { |
| 795 | if (insertdict(mp, key, hash, value) != 0) |
| 796 | return -1; |
| 797 | } |
| 798 | else { |
| 799 | if (insertdict_by_entry(mp, key, hash, ep, value) != 0) |
| 800 | return -1; |
| 801 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 802 | /* If we added a key, we can safely resize. Otherwise just return! |
| 803 | * If fill >= 2/3 size, adjust size. Normally, this doubles or |
| 804 | * quaduples the size, but it's also possible for the dict to shrink |
| 805 | * (if ma_fill is much larger than ma_used, meaning a lot of dict |
| 806 | * keys have been * deleted). |
| 807 | * |
| 808 | * Quadrupling the size improves average dictionary sparseness |
| 809 | * (reducing collisions) at the cost of some memory and iteration |
| 810 | * speed (which loops over every possible entry). It also halves |
| 811 | * the number of expensive resize operations in a growing dictionary. |
| 812 | * |
| 813 | * Very large dictionaries (over 50K items) use doubling instead. |
| 814 | * This may help applications with severe memory constraints. |
| 815 | */ |
| 816 | if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2)) |
| 817 | return 0; |
| 818 | return dictresize(mp, (mp->ma_used > 50000 ? 2 : 4) * mp->ma_used); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 819 | } |
| 820 | |
Antoine Pitrou | 6a1cd1b | 2012-02-27 00:45:12 +0100 | [diff] [blame] | 821 | /* CAUTION: PyDict_SetItem() must guarantee that it won't resize the |
| 822 | * dictionary if it's merely replacing the value for an existing key. |
| 823 | * This means that it's safe to loop over a dictionary with PyDict_Next() |
| 824 | * and occasionally replace a value -- but you can't insert new keys or |
| 825 | * remove them. |
| 826 | */ |
| 827 | int |
| 828 | PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value) |
| 829 | { |
| 830 | register long hash; |
| 831 | |
| 832 | if (!PyDict_Check(op)) { |
| 833 | PyErr_BadInternalCall(); |
| 834 | return -1; |
| 835 | } |
| 836 | assert(key); |
| 837 | assert(value); |
| 838 | if (PyString_CheckExact(key)) { |
| 839 | hash = ((PyStringObject *)key)->ob_shash; |
| 840 | if (hash == -1) |
| 841 | hash = PyObject_Hash(key); |
| 842 | } |
| 843 | else { |
| 844 | hash = PyObject_Hash(key); |
| 845 | if (hash == -1) |
| 846 | return -1; |
| 847 | } |
| 848 | return dict_set_item_by_hash_or_entry(op, key, hash, NULL, value); |
| 849 | } |
| 850 | |
Antoine Pitrou | f939b3c | 2016-12-27 15:08:27 +0100 | [diff] [blame] | 851 | static int |
| 852 | delitem_common(PyDictObject *mp, PyDictEntry *ep) |
| 853 | { |
| 854 | PyObject *old_value, *old_key; |
| 855 | |
| 856 | old_key = ep->me_key; |
| 857 | Py_INCREF(dummy); |
| 858 | ep->me_key = dummy; |
| 859 | old_value = ep->me_value; |
| 860 | ep->me_value = NULL; |
| 861 | mp->ma_used--; |
| 862 | Py_DECREF(old_value); |
| 863 | Py_DECREF(old_key); |
| 864 | return 0; |
| 865 | } |
| 866 | |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 867 | int |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 868 | PyDict_DelItem(PyObject *op, PyObject *key) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 869 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 870 | register PyDictObject *mp; |
| 871 | register long hash; |
| 872 | register PyDictEntry *ep; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 873 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 874 | if (!PyDict_Check(op)) { |
| 875 | PyErr_BadInternalCall(); |
| 876 | return -1; |
| 877 | } |
| 878 | assert(key); |
| 879 | if (!PyString_CheckExact(key) || |
| 880 | (hash = ((PyStringObject *) key)->ob_shash) == -1) { |
| 881 | hash = PyObject_Hash(key); |
| 882 | if (hash == -1) |
| 883 | return -1; |
| 884 | } |
| 885 | mp = (PyDictObject *)op; |
| 886 | ep = (mp->ma_lookup)(mp, key, hash); |
| 887 | if (ep == NULL) |
| 888 | return -1; |
| 889 | if (ep->me_value == NULL) { |
| 890 | set_key_error(key); |
| 891 | return -1; |
| 892 | } |
Antoine Pitrou | f939b3c | 2016-12-27 15:08:27 +0100 | [diff] [blame] | 893 | |
| 894 | return delitem_common(mp, ep); |
| 895 | } |
| 896 | |
| 897 | int |
| 898 | _PyDict_DelItemIf(PyObject *op, PyObject *key, |
| 899 | int (*predicate)(PyObject *value)) |
| 900 | { |
| 901 | register PyDictObject *mp; |
| 902 | register long hash; |
| 903 | register PyDictEntry *ep; |
| 904 | int res; |
| 905 | |
| 906 | if (!PyDict_Check(op)) { |
| 907 | PyErr_BadInternalCall(); |
| 908 | return -1; |
| 909 | } |
| 910 | assert(key); |
| 911 | if (!PyString_CheckExact(key) || |
| 912 | (hash = ((PyStringObject *) key)->ob_shash) == -1) { |
| 913 | hash = PyObject_Hash(key); |
| 914 | if (hash == -1) |
| 915 | return -1; |
| 916 | } |
| 917 | mp = (PyDictObject *)op; |
| 918 | ep = (mp->ma_lookup)(mp, key, hash); |
| 919 | if (ep == NULL) |
| 920 | return -1; |
| 921 | if (ep->me_value == NULL) { |
| 922 | set_key_error(key); |
| 923 | return -1; |
| 924 | } |
| 925 | res = predicate(ep->me_value); |
| 926 | if (res == -1) |
| 927 | return -1; |
| 928 | if (res > 0) |
| 929 | return delitem_common(mp, ep); |
| 930 | else |
| 931 | return 0; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 932 | } |
| 933 | |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 934 | void |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 935 | PyDict_Clear(PyObject *op) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 936 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 937 | PyDictObject *mp; |
| 938 | PyDictEntry *ep, *table; |
| 939 | int table_is_malloced; |
| 940 | Py_ssize_t fill; |
| 941 | PyDictEntry small_copy[PyDict_MINSIZE]; |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 942 | #ifdef Py_DEBUG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 943 | Py_ssize_t i, n; |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 944 | #endif |
| 945 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 946 | if (!PyDict_Check(op)) |
| 947 | return; |
| 948 | mp = (PyDictObject *)op; |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 949 | #ifdef Py_DEBUG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 950 | n = mp->ma_mask + 1; |
| 951 | i = 0; |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 952 | #endif |
| 953 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 954 | table = mp->ma_table; |
| 955 | assert(table != NULL); |
| 956 | table_is_malloced = table != mp->ma_smalltable; |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 957 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 958 | /* This is delicate. During the process of clearing the dict, |
| 959 | * decrefs can cause the dict to mutate. To avoid fatal confusion |
| 960 | * (voice of experience), we have to make the dict empty before |
| 961 | * clearing the slots, and never refer to anything via mp->xxx while |
| 962 | * clearing. |
| 963 | */ |
| 964 | fill = mp->ma_fill; |
| 965 | if (table_is_malloced) |
| 966 | EMPTY_TO_MINSIZE(mp); |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 967 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 968 | else if (fill > 0) { |
| 969 | /* It's a small table with something that needs to be cleared. |
| 970 | * Afraid the only safe way is to copy the dict entries into |
| 971 | * another small table first. |
| 972 | */ |
| 973 | memcpy(small_copy, table, sizeof(small_copy)); |
| 974 | table = small_copy; |
| 975 | EMPTY_TO_MINSIZE(mp); |
| 976 | } |
| 977 | /* else it's a small table that's already empty */ |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 978 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 979 | /* Now we can finally clear things. If C had refcounts, we could |
| 980 | * assert that the refcount on table is 1 now, i.e. that this function |
| 981 | * has unique access to it, so decref side-effects can't alter it. |
| 982 | */ |
| 983 | for (ep = table; fill > 0; ++ep) { |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 984 | #ifdef Py_DEBUG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 985 | assert(i < n); |
| 986 | ++i; |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 987 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 988 | if (ep->me_key) { |
| 989 | --fill; |
| 990 | Py_DECREF(ep->me_key); |
| 991 | Py_XDECREF(ep->me_value); |
| 992 | } |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 993 | #ifdef Py_DEBUG |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 994 | else |
| 995 | assert(ep->me_value == NULL); |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 996 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 997 | } |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 998 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 999 | if (table_is_malloced) |
| 1000 | PyMem_DEL(table); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 1003 | /* |
| 1004 | * Iterate over a dict. Use like so: |
| 1005 | * |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 1006 | * Py_ssize_t i; |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 1007 | * PyObject *key, *value; |
| 1008 | * i = 0; # important! i should not otherwise be changed by you |
Neal Norwitz | 0732301 | 2003-02-15 14:45:12 +0000 | [diff] [blame] | 1009 | * while (PyDict_Next(yourdict, &i, &key, &value)) { |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 1010 | * Refer to borrowed references in key and value. |
| 1011 | * } |
| 1012 | * |
| 1013 | * 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] | 1014 | * mutates the dict. One exception: it is safe if the loop merely changes |
| 1015 | * the values associated with the keys (but doesn't insert new keys or |
| 1016 | * delete keys), via PyDict_SetItem(). |
| 1017 | */ |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1018 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1019 | 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] | 1020 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1021 | register Py_ssize_t i; |
| 1022 | register Py_ssize_t mask; |
| 1023 | register PyDictEntry *ep; |
Raymond Hettinger | 4344278 | 2004-03-17 21:55:03 +0000 | [diff] [blame] | 1024 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1025 | if (!PyDict_Check(op)) |
| 1026 | return 0; |
| 1027 | i = *ppos; |
| 1028 | if (i < 0) |
| 1029 | return 0; |
| 1030 | ep = ((PyDictObject *)op)->ma_table; |
| 1031 | mask = ((PyDictObject *)op)->ma_mask; |
| 1032 | while (i <= mask && ep[i].me_value == NULL) |
| 1033 | i++; |
| 1034 | *ppos = i+1; |
| 1035 | if (i > mask) |
| 1036 | return 0; |
| 1037 | if (pkey) |
| 1038 | *pkey = ep[i].me_key; |
| 1039 | if (pvalue) |
| 1040 | *pvalue = ep[i].me_value; |
| 1041 | return 1; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1042 | } |
| 1043 | |
Raymond Hettinger | d6fc72a | 2007-02-19 02:03:19 +0000 | [diff] [blame] | 1044 | /* Internal version of PyDict_Next that returns a hash value in addition to the key and value.*/ |
| 1045 | int |
| 1046 | _PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue, long *phash) |
| 1047 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1048 | register Py_ssize_t i; |
| 1049 | register Py_ssize_t mask; |
| 1050 | register PyDictEntry *ep; |
Raymond Hettinger | d6fc72a | 2007-02-19 02:03:19 +0000 | [diff] [blame] | 1051 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1052 | if (!PyDict_Check(op)) |
| 1053 | return 0; |
| 1054 | i = *ppos; |
| 1055 | if (i < 0) |
| 1056 | return 0; |
| 1057 | ep = ((PyDictObject *)op)->ma_table; |
| 1058 | mask = ((PyDictObject *)op)->ma_mask; |
| 1059 | while (i <= mask && ep[i].me_value == NULL) |
| 1060 | i++; |
| 1061 | *ppos = i+1; |
| 1062 | if (i > mask) |
| 1063 | return 0; |
| 1064 | *phash = (long)(ep[i].me_hash); |
| 1065 | if (pkey) |
| 1066 | *pkey = ep[i].me_key; |
| 1067 | if (pvalue) |
| 1068 | *pvalue = ep[i].me_value; |
| 1069 | return 1; |
Raymond Hettinger | d6fc72a | 2007-02-19 02:03:19 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1072 | /* Methods */ |
| 1073 | |
| 1074 | static void |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1075 | dict_dealloc(register PyDictObject *mp) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1076 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1077 | register PyDictEntry *ep; |
| 1078 | Py_ssize_t fill = mp->ma_fill; |
| 1079 | PyObject_GC_UnTrack(mp); |
| 1080 | Py_TRASHCAN_SAFE_BEGIN(mp) |
| 1081 | for (ep = mp->ma_table; fill > 0; ep++) { |
| 1082 | if (ep->me_key) { |
| 1083 | --fill; |
| 1084 | Py_DECREF(ep->me_key); |
| 1085 | Py_XDECREF(ep->me_value); |
| 1086 | } |
| 1087 | } |
| 1088 | if (mp->ma_table != mp->ma_smalltable) |
| 1089 | PyMem_DEL(mp->ma_table); |
| 1090 | if (numfree < PyDict_MAXFREELIST && Py_TYPE(mp) == &PyDict_Type) |
| 1091 | free_list[numfree++] = mp; |
| 1092 | else |
| 1093 | Py_TYPE(mp)->tp_free((PyObject *)mp); |
| 1094 | Py_TRASHCAN_SAFE_END(mp) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
| 1097 | static int |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1098 | dict_print(register PyDictObject *mp, register FILE *fp, register int flags) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1099 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1100 | register Py_ssize_t i; |
| 1101 | register Py_ssize_t any; |
| 1102 | int status; |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 1103 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1104 | status = Py_ReprEnter((PyObject*)mp); |
| 1105 | if (status != 0) { |
| 1106 | if (status < 0) |
| 1107 | return status; |
| 1108 | Py_BEGIN_ALLOW_THREADS |
| 1109 | fprintf(fp, "{...}"); |
| 1110 | Py_END_ALLOW_THREADS |
| 1111 | return 0; |
| 1112 | } |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 1113 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1114 | Py_BEGIN_ALLOW_THREADS |
| 1115 | fprintf(fp, "{"); |
| 1116 | Py_END_ALLOW_THREADS |
| 1117 | any = 0; |
| 1118 | for (i = 0; i <= mp->ma_mask; i++) { |
| 1119 | PyDictEntry *ep = mp->ma_table + i; |
| 1120 | PyObject *pvalue = ep->me_value; |
| 1121 | if (pvalue != NULL) { |
| 1122 | /* Prevent PyObject_Repr from deleting value during |
| 1123 | key format */ |
| 1124 | Py_INCREF(pvalue); |
| 1125 | if (any++ > 0) { |
| 1126 | Py_BEGIN_ALLOW_THREADS |
| 1127 | fprintf(fp, ", "); |
| 1128 | Py_END_ALLOW_THREADS |
| 1129 | } |
| 1130 | if (PyObject_Print((PyObject *)ep->me_key, fp, 0)!=0) { |
| 1131 | Py_DECREF(pvalue); |
| 1132 | Py_ReprLeave((PyObject*)mp); |
| 1133 | return -1; |
| 1134 | } |
| 1135 | Py_BEGIN_ALLOW_THREADS |
| 1136 | fprintf(fp, ": "); |
| 1137 | Py_END_ALLOW_THREADS |
| 1138 | if (PyObject_Print(pvalue, fp, 0) != 0) { |
| 1139 | Py_DECREF(pvalue); |
| 1140 | Py_ReprLeave((PyObject*)mp); |
| 1141 | return -1; |
| 1142 | } |
| 1143 | Py_DECREF(pvalue); |
| 1144 | } |
| 1145 | } |
| 1146 | Py_BEGIN_ALLOW_THREADS |
| 1147 | fprintf(fp, "}"); |
| 1148 | Py_END_ALLOW_THREADS |
| 1149 | Py_ReprLeave((PyObject*)mp); |
| 1150 | return 0; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1153 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1154 | dict_repr(PyDictObject *mp) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1155 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1156 | Py_ssize_t i; |
| 1157 | PyObject *s, *temp, *colon = NULL; |
| 1158 | PyObject *pieces = NULL, *result = NULL; |
| 1159 | PyObject *key, *value; |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 1160 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1161 | i = Py_ReprEnter((PyObject *)mp); |
| 1162 | if (i != 0) { |
| 1163 | return i > 0 ? PyString_FromString("{...}") : NULL; |
| 1164 | } |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 1165 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1166 | if (mp->ma_used == 0) { |
| 1167 | result = PyString_FromString("{}"); |
| 1168 | goto Done; |
| 1169 | } |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1170 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1171 | pieces = PyList_New(0); |
| 1172 | if (pieces == NULL) |
| 1173 | goto Done; |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1174 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1175 | colon = PyString_FromString(": "); |
| 1176 | if (colon == NULL) |
| 1177 | goto Done; |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1178 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1179 | /* Do repr() on each key+value pair, and insert ": " between them. |
| 1180 | Note that repr may mutate the dict. */ |
| 1181 | i = 0; |
| 1182 | while (PyDict_Next((PyObject *)mp, &i, &key, &value)) { |
| 1183 | int status; |
| 1184 | /* Prevent repr from deleting value during key format. */ |
| 1185 | Py_INCREF(value); |
| 1186 | s = PyObject_Repr(key); |
| 1187 | PyString_Concat(&s, colon); |
| 1188 | PyString_ConcatAndDel(&s, PyObject_Repr(value)); |
| 1189 | Py_DECREF(value); |
| 1190 | if (s == NULL) |
| 1191 | goto Done; |
| 1192 | status = PyList_Append(pieces, s); |
| 1193 | Py_DECREF(s); /* append created a new ref */ |
| 1194 | if (status < 0) |
| 1195 | goto Done; |
| 1196 | } |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1197 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1198 | /* Add "{}" decorations to the first and last items. */ |
| 1199 | assert(PyList_GET_SIZE(pieces) > 0); |
| 1200 | s = PyString_FromString("{"); |
| 1201 | if (s == NULL) |
| 1202 | goto Done; |
| 1203 | temp = PyList_GET_ITEM(pieces, 0); |
| 1204 | PyString_ConcatAndDel(&s, temp); |
| 1205 | PyList_SET_ITEM(pieces, 0, s); |
| 1206 | if (s == NULL) |
| 1207 | goto Done; |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1208 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1209 | s = PyString_FromString("}"); |
| 1210 | if (s == NULL) |
| 1211 | goto Done; |
| 1212 | temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1); |
| 1213 | PyString_ConcatAndDel(&temp, s); |
| 1214 | PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp); |
| 1215 | if (temp == NULL) |
| 1216 | goto Done; |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1217 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1218 | /* Paste them all together with ", " between. */ |
| 1219 | s = PyString_FromString(", "); |
| 1220 | if (s == NULL) |
| 1221 | goto Done; |
| 1222 | result = _PyString_Join(s, pieces); |
| 1223 | Py_DECREF(s); |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1224 | |
| 1225 | Done: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1226 | Py_XDECREF(pieces); |
| 1227 | Py_XDECREF(colon); |
| 1228 | Py_ReprLeave((PyObject *)mp); |
| 1229 | return result; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1232 | static Py_ssize_t |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1233 | dict_length(PyDictObject *mp) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1234 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1235 | return mp->ma_used; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1238 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1239 | dict_subscript(PyDictObject *mp, register PyObject *key) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1240 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1241 | PyObject *v; |
| 1242 | long hash; |
| 1243 | PyDictEntry *ep; |
| 1244 | assert(mp->ma_table != NULL); |
| 1245 | if (!PyString_CheckExact(key) || |
| 1246 | (hash = ((PyStringObject *) key)->ob_shash) == -1) { |
| 1247 | hash = PyObject_Hash(key); |
| 1248 | if (hash == -1) |
| 1249 | return NULL; |
| 1250 | } |
| 1251 | ep = (mp->ma_lookup)(mp, key, hash); |
| 1252 | if (ep == NULL) |
| 1253 | return NULL; |
| 1254 | v = ep->me_value; |
| 1255 | if (v == NULL) { |
| 1256 | if (!PyDict_CheckExact(mp)) { |
| 1257 | /* Look up __missing__ method if we're a subclass. */ |
| 1258 | PyObject *missing, *res; |
| 1259 | static PyObject *missing_str = NULL; |
| 1260 | missing = _PyObject_LookupSpecial((PyObject *)mp, |
| 1261 | "__missing__", |
| 1262 | &missing_str); |
| 1263 | if (missing != NULL) { |
| 1264 | res = PyObject_CallFunctionObjArgs(missing, |
| 1265 | key, NULL); |
| 1266 | Py_DECREF(missing); |
| 1267 | return res; |
| 1268 | } |
| 1269 | else if (PyErr_Occurred()) |
| 1270 | return NULL; |
| 1271 | } |
| 1272 | set_key_error(key); |
| 1273 | return NULL; |
| 1274 | } |
| 1275 | else |
| 1276 | Py_INCREF(v); |
| 1277 | return v; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
| 1280 | static int |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1281 | dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1282 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1283 | if (w == NULL) |
| 1284 | return PyDict_DelItem((PyObject *)mp, v); |
| 1285 | else |
| 1286 | return PyDict_SetItem((PyObject *)mp, v, w); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 1289 | static PyMappingMethods dict_as_mapping = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1290 | (lenfunc)dict_length, /*mp_length*/ |
| 1291 | (binaryfunc)dict_subscript, /*mp_subscript*/ |
| 1292 | (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1293 | }; |
| 1294 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1295 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1296 | dict_keys(register PyDictObject *mp) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1297 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1298 | register PyObject *v; |
| 1299 | register Py_ssize_t i, j; |
| 1300 | PyDictEntry *ep; |
| 1301 | Py_ssize_t mask, n; |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1302 | |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1303 | again: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1304 | n = mp->ma_used; |
| 1305 | v = PyList_New(n); |
| 1306 | if (v == NULL) |
| 1307 | return NULL; |
| 1308 | if (n != mp->ma_used) { |
| 1309 | /* Durnit. The allocations caused the dict to resize. |
| 1310 | * Just start over, this shouldn't normally happen. |
| 1311 | */ |
| 1312 | Py_DECREF(v); |
| 1313 | goto again; |
| 1314 | } |
| 1315 | ep = mp->ma_table; |
| 1316 | mask = mp->ma_mask; |
| 1317 | for (i = 0, j = 0; i <= mask; i++) { |
| 1318 | if (ep[i].me_value != NULL) { |
| 1319 | PyObject *key = ep[i].me_key; |
| 1320 | Py_INCREF(key); |
| 1321 | PyList_SET_ITEM(v, j, key); |
| 1322 | j++; |
| 1323 | } |
| 1324 | } |
| 1325 | assert(j == n); |
| 1326 | return v; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1327 | } |
| 1328 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1329 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1330 | dict_values(register PyDictObject *mp) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1331 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1332 | register PyObject *v; |
| 1333 | register Py_ssize_t i, j; |
| 1334 | PyDictEntry *ep; |
| 1335 | Py_ssize_t mask, n; |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1336 | |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1337 | again: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1338 | n = mp->ma_used; |
| 1339 | v = PyList_New(n); |
| 1340 | if (v == NULL) |
| 1341 | return NULL; |
| 1342 | if (n != mp->ma_used) { |
| 1343 | /* Durnit. The allocations caused the dict to resize. |
| 1344 | * Just start over, this shouldn't normally happen. |
| 1345 | */ |
| 1346 | Py_DECREF(v); |
| 1347 | goto again; |
| 1348 | } |
| 1349 | ep = mp->ma_table; |
| 1350 | mask = mp->ma_mask; |
| 1351 | for (i = 0, j = 0; i <= mask; i++) { |
| 1352 | if (ep[i].me_value != NULL) { |
| 1353 | PyObject *value = ep[i].me_value; |
| 1354 | Py_INCREF(value); |
| 1355 | PyList_SET_ITEM(v, j, value); |
| 1356 | j++; |
| 1357 | } |
| 1358 | } |
| 1359 | assert(j == n); |
| 1360 | return v; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1363 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1364 | dict_items(register PyDictObject *mp) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1365 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1366 | register PyObject *v; |
| 1367 | register Py_ssize_t i, j, n; |
| 1368 | Py_ssize_t mask; |
| 1369 | PyObject *item, *key, *value; |
| 1370 | PyDictEntry *ep; |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1371 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1372 | /* Preallocate the list of tuples, to avoid allocations during |
| 1373 | * the loop over the items, which could trigger GC, which |
| 1374 | * could resize the dict. :-( |
| 1375 | */ |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1376 | again: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1377 | n = mp->ma_used; |
| 1378 | v = PyList_New(n); |
| 1379 | if (v == NULL) |
| 1380 | return NULL; |
| 1381 | for (i = 0; i < n; i++) { |
| 1382 | item = PyTuple_New(2); |
| 1383 | if (item == NULL) { |
| 1384 | Py_DECREF(v); |
| 1385 | return NULL; |
| 1386 | } |
| 1387 | PyList_SET_ITEM(v, i, item); |
| 1388 | } |
| 1389 | if (n != mp->ma_used) { |
| 1390 | /* Durnit. The allocations caused the dict to resize. |
| 1391 | * Just start over, this shouldn't normally happen. |
| 1392 | */ |
| 1393 | Py_DECREF(v); |
| 1394 | goto again; |
| 1395 | } |
| 1396 | /* Nothing we do below makes any function calls. */ |
| 1397 | ep = mp->ma_table; |
| 1398 | mask = mp->ma_mask; |
| 1399 | for (i = 0, j = 0; i <= mask; i++) { |
| 1400 | if ((value=ep[i].me_value) != NULL) { |
| 1401 | key = ep[i].me_key; |
| 1402 | item = PyList_GET_ITEM(v, j); |
| 1403 | Py_INCREF(key); |
| 1404 | PyTuple_SET_ITEM(item, 0, key); |
| 1405 | Py_INCREF(value); |
| 1406 | PyTuple_SET_ITEM(item, 1, value); |
| 1407 | j++; |
| 1408 | } |
| 1409 | } |
| 1410 | assert(j == n); |
| 1411 | return v; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1412 | } |
| 1413 | |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 1414 | static PyObject * |
Tim Peters | bca1cbc | 2002-12-09 22:56:13 +0000 | [diff] [blame] | 1415 | dict_fromkeys(PyObject *cls, PyObject *args) |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 1416 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1417 | PyObject *seq; |
| 1418 | PyObject *value = Py_None; |
| 1419 | PyObject *it; /* iter(seq) */ |
| 1420 | PyObject *key; |
| 1421 | PyObject *d; |
| 1422 | int status; |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 1423 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1424 | if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value)) |
| 1425 | return NULL; |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 1426 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1427 | d = PyObject_CallObject(cls, NULL); |
| 1428 | if (d == NULL) |
| 1429 | return NULL; |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 1430 | |
Benjamin Peterson | 47fa4d5 | 2012-10-31 14:22:12 -0400 | [diff] [blame] | 1431 | if (PyDict_CheckExact(d) && ((PyDictObject *)d)->ma_used == 0) { |
Benjamin Peterson | 0ec820f | 2012-10-31 14:05:55 -0400 | [diff] [blame] | 1432 | if (PyDict_CheckExact(seq)) { |
| 1433 | PyDictObject *mp = (PyDictObject *)d; |
| 1434 | PyObject *oldvalue; |
| 1435 | Py_ssize_t pos = 0; |
| 1436 | PyObject *key; |
| 1437 | long hash; |
Raymond Hettinger | cdcf887 | 2007-11-07 02:26:17 +0000 | [diff] [blame] | 1438 | |
INADA Naoki | e126f98 | 2016-12-20 16:07:18 +0900 | [diff] [blame] | 1439 | if (dictresize(mp, ((PyDictObject *)seq)->ma_used / 2 * 3)) { |
Petri Lehtinen | 8ffbab8 | 2011-10-24 20:59:29 +0300 | [diff] [blame] | 1440 | Py_DECREF(d); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1441 | return NULL; |
Petri Lehtinen | 8ffbab8 | 2011-10-24 20:59:29 +0300 | [diff] [blame] | 1442 | } |
Benjamin Peterson | 0ec820f | 2012-10-31 14:05:55 -0400 | [diff] [blame] | 1443 | |
| 1444 | while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) { |
| 1445 | Py_INCREF(key); |
| 1446 | Py_INCREF(value); |
| 1447 | if (insertdict(mp, key, hash, value)) { |
| 1448 | Py_DECREF(d); |
| 1449 | return NULL; |
| 1450 | } |
| 1451 | } |
| 1452 | return d; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1453 | } |
Benjamin Peterson | 0ec820f | 2012-10-31 14:05:55 -0400 | [diff] [blame] | 1454 | if (PyAnySet_CheckExact(seq)) { |
| 1455 | PyDictObject *mp = (PyDictObject *)d; |
| 1456 | Py_ssize_t pos = 0; |
| 1457 | PyObject *key; |
| 1458 | long hash; |
Raymond Hettinger | cdcf887 | 2007-11-07 02:26:17 +0000 | [diff] [blame] | 1459 | |
Raymond Hettinger | 77b3ae5 | 2015-05-13 03:13:28 -0700 | [diff] [blame] | 1460 | if (dictresize(mp, PySet_GET_SIZE(seq) / 2 * 3)) { |
Petri Lehtinen | 8ffbab8 | 2011-10-24 20:59:29 +0300 | [diff] [blame] | 1461 | Py_DECREF(d); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1462 | return NULL; |
Petri Lehtinen | 8ffbab8 | 2011-10-24 20:59:29 +0300 | [diff] [blame] | 1463 | } |
Benjamin Peterson | 0ec820f | 2012-10-31 14:05:55 -0400 | [diff] [blame] | 1464 | |
| 1465 | while (_PySet_NextEntry(seq, &pos, &key, &hash)) { |
| 1466 | Py_INCREF(key); |
| 1467 | Py_INCREF(value); |
| 1468 | if (insertdict(mp, key, hash, value)) { |
| 1469 | Py_DECREF(d); |
| 1470 | return NULL; |
| 1471 | } |
| 1472 | } |
| 1473 | return d; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1474 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1475 | } |
Raymond Hettinger | 0bbbfc4 | 2007-03-20 21:27:24 +0000 | [diff] [blame] | 1476 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1477 | it = PyObject_GetIter(seq); |
| 1478 | if (it == NULL){ |
| 1479 | Py_DECREF(d); |
| 1480 | return NULL; |
| 1481 | } |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 1482 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1483 | if (PyDict_CheckExact(d)) { |
| 1484 | while ((key = PyIter_Next(it)) != NULL) { |
| 1485 | status = PyDict_SetItem(d, key, value); |
| 1486 | Py_DECREF(key); |
| 1487 | if (status < 0) |
| 1488 | goto Fail; |
| 1489 | } |
| 1490 | } else { |
| 1491 | while ((key = PyIter_Next(it)) != NULL) { |
| 1492 | status = PyObject_SetItem(d, key, value); |
| 1493 | Py_DECREF(key); |
| 1494 | if (status < 0) |
| 1495 | goto Fail; |
| 1496 | } |
| 1497 | } |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 1498 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1499 | if (PyErr_Occurred()) |
| 1500 | goto Fail; |
| 1501 | Py_DECREF(it); |
| 1502 | return d; |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 1503 | |
| 1504 | Fail: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1505 | Py_DECREF(it); |
| 1506 | Py_DECREF(d); |
| 1507 | return NULL; |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 1508 | } |
| 1509 | |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 1510 | static int |
| 1511 | dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname) |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 1512 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1513 | PyObject *arg = NULL; |
| 1514 | int result = 0; |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 1515 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1516 | if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg)) |
| 1517 | result = -1; |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 1518 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1519 | else if (arg != NULL) { |
| 1520 | if (PyObject_HasAttrString(arg, "keys")) |
| 1521 | result = PyDict_Merge(self, arg, 1); |
| 1522 | else |
| 1523 | result = PyDict_MergeFromSeq2(self, arg, 1); |
| 1524 | } |
| 1525 | if (result == 0 && kwds != NULL) |
| 1526 | result = PyDict_Merge(self, kwds, 1); |
| 1527 | return result; |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
| 1530 | static PyObject * |
| 1531 | dict_update(PyObject *self, PyObject *args, PyObject *kwds) |
| 1532 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1533 | if (dict_update_common(self, args, kwds, "update") != -1) |
| 1534 | Py_RETURN_NONE; |
| 1535 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1536 | } |
| 1537 | |
Guido van Rossum | 05ac6de | 2001-08-10 20:28:28 +0000 | [diff] [blame] | 1538 | /* Update unconditionally replaces existing items. |
| 1539 | Merge has a 3rd argument 'override'; if set, it acts like Update, |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1540 | otherwise it leaves existing items unchanged. |
| 1541 | |
| 1542 | PyDict_{Update,Merge} update/merge from a mapping object. |
| 1543 | |
Tim Peters | f582b82 | 2001-12-11 18:51:08 +0000 | [diff] [blame] | 1544 | PyDict_MergeFromSeq2 updates/merges from any iterable object |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1545 | producing iterable objects of length 2. |
| 1546 | */ |
| 1547 | |
Tim Peters | f582b82 | 2001-12-11 18:51:08 +0000 | [diff] [blame] | 1548 | int |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1549 | PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override) |
| 1550 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1551 | PyObject *it; /* iter(seq2) */ |
| 1552 | Py_ssize_t i; /* index into seq2 of current element */ |
| 1553 | PyObject *item; /* seq2[i] */ |
| 1554 | PyObject *fast; /* item as a 2-tuple or 2-list */ |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1555 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1556 | assert(d != NULL); |
| 1557 | assert(PyDict_Check(d)); |
| 1558 | assert(seq2 != NULL); |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1559 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1560 | it = PyObject_GetIter(seq2); |
| 1561 | if (it == NULL) |
| 1562 | return -1; |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1563 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1564 | for (i = 0; ; ++i) { |
| 1565 | PyObject *key, *value; |
| 1566 | Py_ssize_t n; |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1567 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1568 | fast = NULL; |
| 1569 | item = PyIter_Next(it); |
| 1570 | if (item == NULL) { |
| 1571 | if (PyErr_Occurred()) |
| 1572 | goto Fail; |
| 1573 | break; |
| 1574 | } |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1575 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1576 | /* Convert item to sequence, and verify length 2. */ |
| 1577 | fast = PySequence_Fast(item, ""); |
| 1578 | if (fast == NULL) { |
| 1579 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 1580 | PyErr_Format(PyExc_TypeError, |
| 1581 | "cannot convert dictionary update " |
| 1582 | "sequence element #%zd to a sequence", |
| 1583 | i); |
| 1584 | goto Fail; |
| 1585 | } |
| 1586 | n = PySequence_Fast_GET_SIZE(fast); |
| 1587 | if (n != 2) { |
| 1588 | PyErr_Format(PyExc_ValueError, |
| 1589 | "dictionary update sequence element #%zd " |
| 1590 | "has length %zd; 2 is required", |
| 1591 | i, n); |
| 1592 | goto Fail; |
| 1593 | } |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1594 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1595 | /* Update/merge with this (key, value) pair. */ |
| 1596 | key = PySequence_Fast_GET_ITEM(fast, 0); |
| 1597 | value = PySequence_Fast_GET_ITEM(fast, 1); |
| 1598 | if (override || PyDict_GetItem(d, key) == NULL) { |
| 1599 | int status = PyDict_SetItem(d, key, value); |
| 1600 | if (status < 0) |
| 1601 | goto Fail; |
| 1602 | } |
| 1603 | Py_DECREF(fast); |
| 1604 | Py_DECREF(item); |
| 1605 | } |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1606 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1607 | i = 0; |
| 1608 | goto Return; |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1609 | Fail: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1610 | Py_XDECREF(item); |
| 1611 | Py_XDECREF(fast); |
| 1612 | i = -1; |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1613 | Return: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1614 | Py_DECREF(it); |
| 1615 | return Py_SAFE_DOWNCAST(i, Py_ssize_t, int); |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1616 | } |
| 1617 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1618 | int |
| 1619 | PyDict_Update(PyObject *a, PyObject *b) |
| 1620 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1621 | return PyDict_Merge(a, b, 1); |
Guido van Rossum | 05ac6de | 2001-08-10 20:28:28 +0000 | [diff] [blame] | 1622 | } |
| 1623 | |
| 1624 | int |
| 1625 | PyDict_Merge(PyObject *a, PyObject *b, int override) |
| 1626 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1627 | register PyDictObject *mp, *other; |
| 1628 | register Py_ssize_t i; |
| 1629 | PyDictEntry *entry; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1630 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1631 | /* We accept for the argument either a concrete dictionary object, |
| 1632 | * or an abstract "mapping" object. For the former, we can do |
| 1633 | * things quite efficiently. For the latter, we only require that |
| 1634 | * PyMapping_Keys() and PyObject_GetItem() be supported. |
| 1635 | */ |
| 1636 | if (a == NULL || !PyDict_Check(a) || b == NULL) { |
| 1637 | PyErr_BadInternalCall(); |
| 1638 | return -1; |
| 1639 | } |
| 1640 | mp = (PyDictObject*)a; |
| 1641 | if (PyDict_Check(b)) { |
| 1642 | other = (PyDictObject*)b; |
| 1643 | if (other == mp || other->ma_used == 0) |
| 1644 | /* a.update(a) or a.update({}); nothing to do */ |
| 1645 | return 0; |
| 1646 | if (mp->ma_used == 0) |
| 1647 | /* Since the target dict is empty, PyDict_GetItem() |
| 1648 | * always returns NULL. Setting override to 1 |
| 1649 | * skips the unnecessary test. |
| 1650 | */ |
| 1651 | override = 1; |
| 1652 | /* Do one big resize at the start, rather than |
| 1653 | * incrementally resizing as we insert new items. Expect |
| 1654 | * that there will be no (or few) overlapping keys. |
| 1655 | */ |
| 1656 | if ((mp->ma_fill + other->ma_used)*3 >= (mp->ma_mask+1)*2) { |
| 1657 | if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0) |
| 1658 | return -1; |
| 1659 | } |
| 1660 | for (i = 0; i <= other->ma_mask; i++) { |
| 1661 | entry = &other->ma_table[i]; |
| 1662 | if (entry->me_value != NULL && |
| 1663 | (override || |
| 1664 | PyDict_GetItem(a, entry->me_key) == NULL)) { |
| 1665 | Py_INCREF(entry->me_key); |
| 1666 | Py_INCREF(entry->me_value); |
| 1667 | if (insertdict(mp, entry->me_key, |
| 1668 | (long)entry->me_hash, |
| 1669 | entry->me_value) != 0) |
| 1670 | return -1; |
| 1671 | } |
| 1672 | } |
| 1673 | } |
| 1674 | else { |
| 1675 | /* Do it the generic, slower way */ |
| 1676 | PyObject *keys = PyMapping_Keys(b); |
| 1677 | PyObject *iter; |
| 1678 | PyObject *key, *value; |
| 1679 | int status; |
Barry Warsaw | 66a0d1d | 2001-06-26 20:08:32 +0000 | [diff] [blame] | 1680 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1681 | if (keys == NULL) |
| 1682 | /* Docstring says this is equivalent to E.keys() so |
| 1683 | * if E doesn't have a .keys() method we want |
| 1684 | * AttributeError to percolate up. Might as well |
| 1685 | * do the same for any other error. |
| 1686 | */ |
| 1687 | return -1; |
Barry Warsaw | 66a0d1d | 2001-06-26 20:08:32 +0000 | [diff] [blame] | 1688 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1689 | iter = PyObject_GetIter(keys); |
| 1690 | Py_DECREF(keys); |
| 1691 | if (iter == NULL) |
| 1692 | return -1; |
Barry Warsaw | 66a0d1d | 2001-06-26 20:08:32 +0000 | [diff] [blame] | 1693 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1694 | for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) { |
| 1695 | if (!override && PyDict_GetItem(a, key) != NULL) { |
| 1696 | Py_DECREF(key); |
| 1697 | continue; |
| 1698 | } |
| 1699 | value = PyObject_GetItem(b, key); |
| 1700 | if (value == NULL) { |
| 1701 | Py_DECREF(iter); |
| 1702 | Py_DECREF(key); |
| 1703 | return -1; |
| 1704 | } |
| 1705 | status = PyDict_SetItem(a, key, value); |
| 1706 | Py_DECREF(key); |
| 1707 | Py_DECREF(value); |
| 1708 | if (status < 0) { |
| 1709 | Py_DECREF(iter); |
| 1710 | return -1; |
| 1711 | } |
| 1712 | } |
| 1713 | Py_DECREF(iter); |
| 1714 | if (PyErr_Occurred()) |
| 1715 | /* Iterator completed, via error */ |
| 1716 | return -1; |
| 1717 | } |
| 1718 | return 0; |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
| 1721 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1722 | dict_copy(register PyDictObject *mp) |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 1723 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1724 | return PyDict_Copy((PyObject*)mp); |
Jeremy Hylton | a12c7a7 | 2000-03-30 22:27:31 +0000 | [diff] [blame] | 1725 | } |
| 1726 | |
| 1727 | PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 1728 | PyDict_Copy(PyObject *o) |
Jeremy Hylton | a12c7a7 | 2000-03-30 22:27:31 +0000 | [diff] [blame] | 1729 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1730 | PyObject *copy; |
Jeremy Hylton | a12c7a7 | 2000-03-30 22:27:31 +0000 | [diff] [blame] | 1731 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1732 | if (o == NULL || !PyDict_Check(o)) { |
| 1733 | PyErr_BadInternalCall(); |
| 1734 | return NULL; |
| 1735 | } |
| 1736 | copy = PyDict_New(); |
| 1737 | if (copy == NULL) |
| 1738 | return NULL; |
| 1739 | if (PyDict_Merge(copy, o, 1) == 0) |
| 1740 | return copy; |
| 1741 | Py_DECREF(copy); |
| 1742 | return NULL; |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 1743 | } |
| 1744 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1745 | Py_ssize_t |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 1746 | PyDict_Size(PyObject *mp) |
Guido van Rossum | 4199fac | 1993-11-05 10:18:44 +0000 | [diff] [blame] | 1747 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1748 | if (mp == NULL || !PyDict_Check(mp)) { |
| 1749 | PyErr_BadInternalCall(); |
| 1750 | return -1; |
| 1751 | } |
| 1752 | return ((PyDictObject *)mp)->ma_used; |
Guido van Rossum | 4199fac | 1993-11-05 10:18:44 +0000 | [diff] [blame] | 1753 | } |
| 1754 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1755 | PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 1756 | PyDict_Keys(PyObject *mp) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1757 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1758 | if (mp == NULL || !PyDict_Check(mp)) { |
| 1759 | PyErr_BadInternalCall(); |
| 1760 | return NULL; |
| 1761 | } |
| 1762 | return dict_keys((PyDictObject *)mp); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1763 | } |
| 1764 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1765 | PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 1766 | PyDict_Values(PyObject *mp) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1767 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1768 | if (mp == NULL || !PyDict_Check(mp)) { |
| 1769 | PyErr_BadInternalCall(); |
| 1770 | return NULL; |
| 1771 | } |
| 1772 | return dict_values((PyDictObject *)mp); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1773 | } |
| 1774 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1775 | PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 1776 | PyDict_Items(PyObject *mp) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1777 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1778 | if (mp == NULL || !PyDict_Check(mp)) { |
| 1779 | PyErr_BadInternalCall(); |
| 1780 | return NULL; |
| 1781 | } |
| 1782 | return dict_items((PyDictObject *)mp); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1783 | } |
| 1784 | |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1785 | /* Subroutine which returns the smallest key in a for which b's value |
| 1786 | is different or absent. The value is returned too, through the |
Tim Peters | 95bf939 | 2001-05-10 08:32:44 +0000 | [diff] [blame] | 1787 | pval argument. Both are NULL if no key in a is found for which b's status |
| 1788 | differs. The refcounts on (and only on) non-NULL *pval and function return |
| 1789 | values must be decremented by the caller (characterize() increments them |
| 1790 | to ensure that mutating comparison and PyDict_GetItem calls can't delete |
| 1791 | them before the caller is done looking at them). */ |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1792 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1793 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1794 | characterize(PyDictObject *a, PyDictObject *b, PyObject **pval) |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1795 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1796 | PyObject *akey = NULL; /* smallest key in a s.t. a[akey] != b[akey] */ |
| 1797 | PyObject *aval = NULL; /* a[akey] */ |
| 1798 | Py_ssize_t i; |
| 1799 | int cmp; |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1800 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1801 | for (i = 0; i <= a->ma_mask; i++) { |
| 1802 | PyObject *thiskey, *thisaval, *thisbval; |
| 1803 | if (a->ma_table[i].me_value == NULL) |
| 1804 | continue; |
| 1805 | thiskey = a->ma_table[i].me_key; |
| 1806 | Py_INCREF(thiskey); /* keep alive across compares */ |
| 1807 | if (akey != NULL) { |
| 1808 | cmp = PyObject_RichCompareBool(akey, thiskey, Py_LT); |
| 1809 | if (cmp < 0) { |
| 1810 | Py_DECREF(thiskey); |
| 1811 | goto Fail; |
| 1812 | } |
| 1813 | if (cmp > 0 || |
| 1814 | i > a->ma_mask || |
| 1815 | a->ma_table[i].me_value == NULL) |
| 1816 | { |
| 1817 | /* Not the *smallest* a key; or maybe it is |
| 1818 | * but the compare shrunk the dict so we can't |
| 1819 | * find its associated value anymore; or |
| 1820 | * maybe it is but the compare deleted the |
| 1821 | * a[thiskey] entry. |
| 1822 | */ |
| 1823 | Py_DECREF(thiskey); |
| 1824 | continue; |
| 1825 | } |
| 1826 | } |
Tim Peters | 95bf939 | 2001-05-10 08:32:44 +0000 | [diff] [blame] | 1827 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1828 | /* Compare a[thiskey] to b[thiskey]; cmp <- true iff equal. */ |
| 1829 | thisaval = a->ma_table[i].me_value; |
| 1830 | assert(thisaval); |
| 1831 | Py_INCREF(thisaval); /* keep alive */ |
| 1832 | thisbval = PyDict_GetItem((PyObject *)b, thiskey); |
| 1833 | if (thisbval == NULL) |
| 1834 | cmp = 0; |
| 1835 | else { |
| 1836 | /* both dicts have thiskey: same values? */ |
| 1837 | cmp = PyObject_RichCompareBool( |
| 1838 | thisaval, thisbval, Py_EQ); |
| 1839 | if (cmp < 0) { |
| 1840 | Py_DECREF(thiskey); |
| 1841 | Py_DECREF(thisaval); |
| 1842 | goto Fail; |
| 1843 | } |
| 1844 | } |
| 1845 | if (cmp == 0) { |
| 1846 | /* New winner. */ |
| 1847 | Py_XDECREF(akey); |
| 1848 | Py_XDECREF(aval); |
| 1849 | akey = thiskey; |
| 1850 | aval = thisaval; |
| 1851 | } |
| 1852 | else { |
| 1853 | Py_DECREF(thiskey); |
| 1854 | Py_DECREF(thisaval); |
| 1855 | } |
| 1856 | } |
| 1857 | *pval = aval; |
| 1858 | return akey; |
Tim Peters | 95bf939 | 2001-05-10 08:32:44 +0000 | [diff] [blame] | 1859 | |
| 1860 | Fail: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1861 | Py_XDECREF(akey); |
| 1862 | Py_XDECREF(aval); |
| 1863 | *pval = NULL; |
| 1864 | return NULL; |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1865 | } |
| 1866 | |
| 1867 | static int |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1868 | dict_compare(PyDictObject *a, PyDictObject *b) |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1869 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1870 | PyObject *adiff, *bdiff, *aval, *bval; |
| 1871 | int res; |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1872 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1873 | /* Compare lengths first */ |
| 1874 | if (a->ma_used < b->ma_used) |
| 1875 | return -1; /* a is shorter */ |
| 1876 | else if (a->ma_used > b->ma_used) |
| 1877 | return 1; /* b is shorter */ |
Tim Peters | 95bf939 | 2001-05-10 08:32:44 +0000 | [diff] [blame] | 1878 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1879 | /* Same length -- check all keys */ |
| 1880 | bdiff = bval = NULL; |
| 1881 | adiff = characterize(a, b, &aval); |
| 1882 | if (adiff == NULL) { |
| 1883 | assert(!aval); |
| 1884 | /* Either an error, or a is a subset with the same length so |
| 1885 | * must be equal. |
| 1886 | */ |
| 1887 | res = PyErr_Occurred() ? -1 : 0; |
| 1888 | goto Finished; |
| 1889 | } |
| 1890 | bdiff = characterize(b, a, &bval); |
| 1891 | if (bdiff == NULL && PyErr_Occurred()) { |
| 1892 | assert(!bval); |
| 1893 | res = -1; |
| 1894 | goto Finished; |
| 1895 | } |
| 1896 | res = 0; |
| 1897 | if (bdiff) { |
| 1898 | /* bdiff == NULL "should be" impossible now, but perhaps |
| 1899 | * the last comparison done by the characterize() on a had |
| 1900 | * the side effect of making the dicts equal! |
| 1901 | */ |
| 1902 | res = PyObject_Compare(adiff, bdiff); |
| 1903 | } |
| 1904 | if (res == 0 && bval != NULL) |
| 1905 | res = PyObject_Compare(aval, bval); |
Tim Peters | 95bf939 | 2001-05-10 08:32:44 +0000 | [diff] [blame] | 1906 | |
| 1907 | Finished: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1908 | Py_XDECREF(adiff); |
| 1909 | Py_XDECREF(bdiff); |
| 1910 | Py_XDECREF(aval); |
| 1911 | Py_XDECREF(bval); |
| 1912 | return res; |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1913 | } |
| 1914 | |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 1915 | /* Return 1 if dicts equal, 0 if not, -1 if error. |
| 1916 | * Gets out as soon as any difference is detected. |
| 1917 | * Uses only Py_EQ comparison. |
| 1918 | */ |
| 1919 | static int |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1920 | dict_equal(PyDictObject *a, PyDictObject *b) |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 1921 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1922 | Py_ssize_t i; |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 1923 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1924 | if (a->ma_used != b->ma_used) |
| 1925 | /* can't be equal if # of entries differ */ |
| 1926 | return 0; |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 1927 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1928 | /* Same # of entries -- check all of 'em. Exit early on any diff. */ |
| 1929 | for (i = 0; i <= a->ma_mask; i++) { |
| 1930 | PyObject *aval = a->ma_table[i].me_value; |
| 1931 | if (aval != NULL) { |
| 1932 | int cmp; |
| 1933 | PyObject *bval; |
| 1934 | PyObject *key = a->ma_table[i].me_key; |
| 1935 | /* temporarily bump aval's refcount to ensure it stays |
| 1936 | alive until we're done with it */ |
| 1937 | Py_INCREF(aval); |
| 1938 | /* ditto for key */ |
| 1939 | Py_INCREF(key); |
| 1940 | bval = PyDict_GetItem((PyObject *)b, key); |
| 1941 | Py_DECREF(key); |
| 1942 | if (bval == NULL) { |
| 1943 | Py_DECREF(aval); |
| 1944 | return 0; |
| 1945 | } |
| 1946 | cmp = PyObject_RichCompareBool(aval, bval, Py_EQ); |
| 1947 | Py_DECREF(aval); |
| 1948 | if (cmp <= 0) /* error or not equal */ |
| 1949 | return cmp; |
| 1950 | } |
| 1951 | } |
| 1952 | return 1; |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 1953 | } |
| 1954 | |
| 1955 | static PyObject * |
| 1956 | dict_richcompare(PyObject *v, PyObject *w, int op) |
| 1957 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1958 | int cmp; |
| 1959 | PyObject *res; |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 1960 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1961 | if (!PyDict_Check(v) || !PyDict_Check(w)) { |
| 1962 | res = Py_NotImplemented; |
| 1963 | } |
| 1964 | else if (op == Py_EQ || op == Py_NE) { |
| 1965 | cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w); |
| 1966 | if (cmp < 0) |
| 1967 | return NULL; |
| 1968 | res = (cmp == (op == Py_EQ)) ? Py_True : Py_False; |
| 1969 | } |
| 1970 | else { |
| 1971 | /* Py3K warning if comparison isn't == or != */ |
| 1972 | if (PyErr_WarnPy3k("dict inequality comparisons not supported " |
| 1973 | "in 3.x", 1) < 0) { |
| 1974 | return NULL; |
| 1975 | } |
| 1976 | res = Py_NotImplemented; |
| 1977 | } |
| 1978 | Py_INCREF(res); |
| 1979 | return res; |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 1980 | } |
| 1981 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1982 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1983 | dict_contains(register PyDictObject *mp, PyObject *key) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1984 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1985 | long hash; |
| 1986 | PyDictEntry *ep; |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 1987 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1988 | if (!PyString_CheckExact(key) || |
| 1989 | (hash = ((PyStringObject *) key)->ob_shash) == -1) { |
| 1990 | hash = PyObject_Hash(key); |
| 1991 | if (hash == -1) |
| 1992 | return NULL; |
| 1993 | } |
| 1994 | ep = (mp->ma_lookup)(mp, key, hash); |
| 1995 | if (ep == NULL) |
| 1996 | return NULL; |
| 1997 | return PyBool_FromLong(ep->me_value != NULL); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1998 | } |
| 1999 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2000 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 2001 | dict_has_key(register PyDictObject *mp, PyObject *key) |
Neal Norwitz | 8b2bfbc | 2007-05-23 06:35:32 +0000 | [diff] [blame] | 2002 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2003 | if (PyErr_WarnPy3k("dict.has_key() not supported in 3.x; " |
| 2004 | "use the in operator", 1) < 0) |
| 2005 | return NULL; |
| 2006 | return dict_contains(mp, key); |
Neal Norwitz | 8b2bfbc | 2007-05-23 06:35:32 +0000 | [diff] [blame] | 2007 | } |
| 2008 | |
| 2009 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 2010 | dict_get(register PyDictObject *mp, PyObject *args) |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 2011 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2012 | PyObject *key; |
| 2013 | PyObject *failobj = Py_None; |
| 2014 | PyObject *val = NULL; |
| 2015 | long hash; |
| 2016 | PyDictEntry *ep; |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 2017 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2018 | if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj)) |
| 2019 | return NULL; |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 2020 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2021 | if (!PyString_CheckExact(key) || |
| 2022 | (hash = ((PyStringObject *) key)->ob_shash) == -1) { |
| 2023 | hash = PyObject_Hash(key); |
| 2024 | if (hash == -1) |
| 2025 | return NULL; |
| 2026 | } |
| 2027 | ep = (mp->ma_lookup)(mp, key, hash); |
| 2028 | if (ep == NULL) |
| 2029 | return NULL; |
| 2030 | val = ep->me_value; |
| 2031 | if (val == NULL) |
| 2032 | val = failobj; |
| 2033 | Py_INCREF(val); |
| 2034 | return val; |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 2035 | } |
| 2036 | |
| 2037 | |
| 2038 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 2039 | dict_setdefault(register PyDictObject *mp, PyObject *args) |
Guido van Rossum | 164452c | 2000-08-08 16:12:54 +0000 | [diff] [blame] | 2040 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2041 | PyObject *key; |
| 2042 | PyObject *failobj = Py_None; |
| 2043 | PyObject *val = NULL; |
| 2044 | long hash; |
| 2045 | PyDictEntry *ep; |
Guido van Rossum | 164452c | 2000-08-08 16:12:54 +0000 | [diff] [blame] | 2046 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2047 | if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj)) |
| 2048 | return NULL; |
Guido van Rossum | 164452c | 2000-08-08 16:12:54 +0000 | [diff] [blame] | 2049 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2050 | if (!PyString_CheckExact(key) || |
| 2051 | (hash = ((PyStringObject *) key)->ob_shash) == -1) { |
| 2052 | hash = PyObject_Hash(key); |
| 2053 | if (hash == -1) |
| 2054 | return NULL; |
| 2055 | } |
| 2056 | ep = (mp->ma_lookup)(mp, key, hash); |
| 2057 | if (ep == NULL) |
| 2058 | return NULL; |
| 2059 | val = ep->me_value; |
| 2060 | if (val == NULL) { |
Antoine Pitrou | 6a1cd1b | 2012-02-27 00:45:12 +0100 | [diff] [blame] | 2061 | if (dict_set_item_by_hash_or_entry((PyObject*)mp, key, hash, ep, |
| 2062 | failobj) == 0) |
| 2063 | val = failobj; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2064 | } |
| 2065 | Py_XINCREF(val); |
| 2066 | return val; |
Guido van Rossum | 164452c | 2000-08-08 16:12:54 +0000 | [diff] [blame] | 2067 | } |
| 2068 | |
| 2069 | |
| 2070 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 2071 | dict_clear(register PyDictObject *mp) |
Guido van Rossum | fb8f1ca | 1997-03-21 21:55:12 +0000 | [diff] [blame] | 2072 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2073 | PyDict_Clear((PyObject *)mp); |
| 2074 | Py_RETURN_NONE; |
Guido van Rossum | fb8f1ca | 1997-03-21 21:55:12 +0000 | [diff] [blame] | 2075 | } |
| 2076 | |
Guido van Rossum | ba6ab84 | 2000-12-12 22:02:18 +0000 | [diff] [blame] | 2077 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 2078 | dict_pop(PyDictObject *mp, PyObject *args) |
Guido van Rossum | e027d98 | 2002-04-12 15:11:59 +0000 | [diff] [blame] | 2079 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2080 | long hash; |
| 2081 | PyDictEntry *ep; |
| 2082 | PyObject *old_value, *old_key; |
| 2083 | PyObject *key, *deflt = NULL; |
Guido van Rossum | e027d98 | 2002-04-12 15:11:59 +0000 | [diff] [blame] | 2084 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2085 | if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt)) |
| 2086 | return NULL; |
| 2087 | if (mp->ma_used == 0) { |
| 2088 | if (deflt) { |
| 2089 | Py_INCREF(deflt); |
| 2090 | return deflt; |
| 2091 | } |
Raymond Hettinger | 2ad17e1 | 2010-10-30 08:17:46 +0000 | [diff] [blame] | 2092 | set_key_error(key); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2093 | return NULL; |
| 2094 | } |
| 2095 | if (!PyString_CheckExact(key) || |
| 2096 | (hash = ((PyStringObject *) key)->ob_shash) == -1) { |
| 2097 | hash = PyObject_Hash(key); |
| 2098 | if (hash == -1) |
| 2099 | return NULL; |
| 2100 | } |
| 2101 | ep = (mp->ma_lookup)(mp, key, hash); |
| 2102 | if (ep == NULL) |
| 2103 | return NULL; |
| 2104 | if (ep->me_value == NULL) { |
| 2105 | if (deflt) { |
| 2106 | Py_INCREF(deflt); |
| 2107 | return deflt; |
| 2108 | } |
| 2109 | set_key_error(key); |
| 2110 | return NULL; |
| 2111 | } |
| 2112 | old_key = ep->me_key; |
| 2113 | Py_INCREF(dummy); |
| 2114 | ep->me_key = dummy; |
| 2115 | old_value = ep->me_value; |
| 2116 | ep->me_value = NULL; |
| 2117 | mp->ma_used--; |
| 2118 | Py_DECREF(old_key); |
| 2119 | return old_value; |
Guido van Rossum | e027d98 | 2002-04-12 15:11:59 +0000 | [diff] [blame] | 2120 | } |
| 2121 | |
| 2122 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 2123 | dict_popitem(PyDictObject *mp) |
Guido van Rossum | ba6ab84 | 2000-12-12 22:02:18 +0000 | [diff] [blame] | 2124 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2125 | Py_ssize_t i = 0; |
| 2126 | PyDictEntry *ep; |
| 2127 | PyObject *res; |
Guido van Rossum | ba6ab84 | 2000-12-12 22:02:18 +0000 | [diff] [blame] | 2128 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2129 | /* Allocate the result tuple before checking the size. Believe it |
| 2130 | * or not, this allocation could trigger a garbage collection which |
| 2131 | * could empty the dict, so if we checked the size first and that |
| 2132 | * happened, the result would be an infinite loop (searching for an |
| 2133 | * entry that no longer exists). Note that the usual popitem() |
| 2134 | * idiom is "while d: k, v = d.popitem()". so needing to throw the |
| 2135 | * tuple away if the dict *is* empty isn't a significant |
| 2136 | * inefficiency -- possible, but unlikely in practice. |
| 2137 | */ |
| 2138 | res = PyTuple_New(2); |
| 2139 | if (res == NULL) |
| 2140 | return NULL; |
| 2141 | if (mp->ma_used == 0) { |
| 2142 | Py_DECREF(res); |
| 2143 | PyErr_SetString(PyExc_KeyError, |
| 2144 | "popitem(): dictionary is empty"); |
| 2145 | return NULL; |
| 2146 | } |
| 2147 | /* Set ep to "the first" dict entry with a value. We abuse the hash |
| 2148 | * field of slot 0 to hold a search finger: |
| 2149 | * If slot 0 has a value, use slot 0. |
| 2150 | * Else slot 0 is being used to hold a search finger, |
| 2151 | * and we use its hash value as the first index to look. |
| 2152 | */ |
| 2153 | ep = &mp->ma_table[0]; |
| 2154 | if (ep->me_value == NULL) { |
| 2155 | i = ep->me_hash; |
| 2156 | /* The hash field may be a real hash value, or it may be a |
| 2157 | * legit search finger, or it may be a once-legit search |
| 2158 | * finger that's out of bounds now because it wrapped around |
| 2159 | * or the table shrunk -- simply make sure it's in bounds now. |
| 2160 | */ |
| 2161 | if (i > mp->ma_mask || i < 1) |
| 2162 | i = 1; /* skip slot 0 */ |
| 2163 | while ((ep = &mp->ma_table[i])->me_value == NULL) { |
| 2164 | i++; |
| 2165 | if (i > mp->ma_mask) |
| 2166 | i = 1; |
| 2167 | } |
| 2168 | } |
| 2169 | PyTuple_SET_ITEM(res, 0, ep->me_key); |
| 2170 | PyTuple_SET_ITEM(res, 1, ep->me_value); |
| 2171 | Py_INCREF(dummy); |
| 2172 | ep->me_key = dummy; |
| 2173 | ep->me_value = NULL; |
| 2174 | mp->ma_used--; |
| 2175 | assert(mp->ma_table[0].me_value == NULL); |
| 2176 | mp->ma_table[0].me_hash = i + 1; /* next place to start */ |
| 2177 | return res; |
Guido van Rossum | ba6ab84 | 2000-12-12 22:02:18 +0000 | [diff] [blame] | 2178 | } |
| 2179 | |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 2180 | static int |
| 2181 | dict_traverse(PyObject *op, visitproc visit, void *arg) |
| 2182 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2183 | Py_ssize_t i = 0; |
| 2184 | PyObject *pk; |
| 2185 | PyObject *pv; |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 2186 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2187 | while (PyDict_Next(op, &i, &pk, &pv)) { |
| 2188 | Py_VISIT(pk); |
| 2189 | Py_VISIT(pv); |
| 2190 | } |
| 2191 | return 0; |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 2192 | } |
| 2193 | |
| 2194 | static int |
| 2195 | dict_tp_clear(PyObject *op) |
| 2196 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2197 | PyDict_Clear(op); |
| 2198 | return 0; |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 2199 | } |
| 2200 | |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2201 | |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2202 | extern PyTypeObject PyDictIterKey_Type; /* Forward */ |
| 2203 | extern PyTypeObject PyDictIterValue_Type; /* Forward */ |
| 2204 | extern PyTypeObject PyDictIterItem_Type; /* Forward */ |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 2205 | static PyObject *dictiter_new(PyDictObject *, PyTypeObject *); |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 2206 | |
| 2207 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 2208 | dict_iterkeys(PyDictObject *dict) |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 2209 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2210 | return dictiter_new(dict, &PyDictIterKey_Type); |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 2211 | } |
| 2212 | |
| 2213 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 2214 | dict_itervalues(PyDictObject *dict) |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 2215 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2216 | return dictiter_new(dict, &PyDictIterValue_Type); |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 2217 | } |
| 2218 | |
| 2219 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 2220 | dict_iteritems(PyDictObject *dict) |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 2221 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2222 | return dictiter_new(dict, &PyDictIterItem_Type); |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 2223 | } |
| 2224 | |
Robert Schuppenies | 51df064 | 2008-06-01 16:16:17 +0000 | [diff] [blame] | 2225 | static PyObject * |
| 2226 | dict_sizeof(PyDictObject *mp) |
| 2227 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2228 | Py_ssize_t res; |
Robert Schuppenies | 51df064 | 2008-06-01 16:16:17 +0000 | [diff] [blame] | 2229 | |
Serhiy Storchaka | c06a6d0 | 2015-12-19 20:07:48 +0200 | [diff] [blame] | 2230 | res = _PyObject_SIZE(Py_TYPE(mp)); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2231 | if (mp->ma_table != mp->ma_smalltable) |
| 2232 | res = res + (mp->ma_mask + 1) * sizeof(PyDictEntry); |
| 2233 | return PyInt_FromSsize_t(res); |
Robert Schuppenies | 51df064 | 2008-06-01 16:16:17 +0000 | [diff] [blame] | 2234 | } |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 2235 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2236 | PyDoc_STRVAR(has_key__doc__, |
Raymond Hettinger | 574aa32 | 2003-09-01 22:12:08 +0000 | [diff] [blame] | 2237 | "D.has_key(k) -> True if D has a key k, else False"); |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2238 | |
Raymond Hettinger | 8f5cdaa | 2003-12-13 11:26:12 +0000 | [diff] [blame] | 2239 | PyDoc_STRVAR(contains__doc__, |
| 2240 | "D.__contains__(k) -> True if D has a key k, else False"); |
| 2241 | |
| 2242 | PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]"); |
| 2243 | |
Robert Schuppenies | 51df064 | 2008-06-01 16:16:17 +0000 | [diff] [blame] | 2244 | PyDoc_STRVAR(sizeof__doc__, |
Georg Brandl | 7a6de8b | 2008-06-01 16:42:16 +0000 | [diff] [blame] | 2245 | "D.__sizeof__() -> size of D in memory, in bytes"); |
Robert Schuppenies | 51df064 | 2008-06-01 16:16:17 +0000 | [diff] [blame] | 2246 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2247 | PyDoc_STRVAR(get__doc__, |
Guido van Rossum | efae886 | 2002-09-04 11:29:45 +0000 | [diff] [blame] | 2248 | "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] | 2249 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2250 | PyDoc_STRVAR(setdefault_doc__, |
Guido van Rossum | efae886 | 2002-09-04 11:29:45 +0000 | [diff] [blame] | 2251 | "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] | 2252 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2253 | PyDoc_STRVAR(pop__doc__, |
Andrew M. Kuchling | a412717 | 2008-10-04 21:51:59 +0000 | [diff] [blame] | 2254 | "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] | 2255 | 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] | 2256 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2257 | PyDoc_STRVAR(popitem__doc__, |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2258 | "D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\ |
Andrew M. Kuchling | a412717 | 2008-10-04 21:51:59 +0000 | [diff] [blame] | 2259 | 2-tuple; but raise KeyError if D is empty."); |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2260 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2261 | PyDoc_STRVAR(keys__doc__, |
| 2262 | "D.keys() -> list of D's keys"); |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2263 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2264 | PyDoc_STRVAR(items__doc__, |
| 2265 | "D.items() -> list of D's (key, value) pairs, as 2-tuples"); |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2266 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2267 | PyDoc_STRVAR(values__doc__, |
| 2268 | "D.values() -> list of D's values"); |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2269 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2270 | PyDoc_STRVAR(update__doc__, |
Georg Brandl | 6f14c33 | 2011-12-18 19:30:55 +0100 | [diff] [blame] | 2271 | "D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n" |
| 2272 | "If E present and has a .keys() method, does: for k in E: D[k] = E[k]\n\ |
| 2273 | If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v\n\ |
Andrew M. Kuchling | a412717 | 2008-10-04 21:51:59 +0000 | [diff] [blame] | 2274 | 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] | 2275 | |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 2276 | PyDoc_STRVAR(fromkeys__doc__, |
| 2277 | "dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\ |
| 2278 | v defaults to None."); |
| 2279 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2280 | PyDoc_STRVAR(clear__doc__, |
| 2281 | "D.clear() -> None. Remove all items from D."); |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2282 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2283 | PyDoc_STRVAR(copy__doc__, |
| 2284 | "D.copy() -> a shallow copy of D"); |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2285 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2286 | PyDoc_STRVAR(iterkeys__doc__, |
| 2287 | "D.iterkeys() -> an iterator over the keys of D"); |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 2288 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2289 | PyDoc_STRVAR(itervalues__doc__, |
| 2290 | "D.itervalues() -> an iterator over the values of D"); |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 2291 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2292 | PyDoc_STRVAR(iteritems__doc__, |
| 2293 | "D.iteritems() -> an iterator over the (key, value) items of D"); |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 2294 | |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 2295 | /* Forward */ |
| 2296 | static PyObject *dictkeys_new(PyObject *); |
| 2297 | static PyObject *dictitems_new(PyObject *); |
| 2298 | static PyObject *dictvalues_new(PyObject *); |
| 2299 | |
| 2300 | PyDoc_STRVAR(viewkeys__doc__, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2301 | "D.viewkeys() -> a set-like object providing a view on D's keys"); |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 2302 | PyDoc_STRVAR(viewitems__doc__, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2303 | "D.viewitems() -> a set-like object providing a view on D's items"); |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 2304 | PyDoc_STRVAR(viewvalues__doc__, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2305 | "D.viewvalues() -> an object providing a view on D's values"); |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 2306 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2307 | static PyMethodDef mapp_methods[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2308 | {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST, |
| 2309 | contains__doc__}, |
| 2310 | {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST, |
| 2311 | getitem__doc__}, |
| 2312 | {"__sizeof__", (PyCFunction)dict_sizeof, METH_NOARGS, |
| 2313 | sizeof__doc__}, |
| 2314 | {"has_key", (PyCFunction)dict_has_key, METH_O, |
| 2315 | has_key__doc__}, |
| 2316 | {"get", (PyCFunction)dict_get, METH_VARARGS, |
| 2317 | get__doc__}, |
| 2318 | {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS, |
| 2319 | setdefault_doc__}, |
| 2320 | {"pop", (PyCFunction)dict_pop, METH_VARARGS, |
| 2321 | pop__doc__}, |
| 2322 | {"popitem", (PyCFunction)dict_popitem, METH_NOARGS, |
| 2323 | popitem__doc__}, |
| 2324 | {"keys", (PyCFunction)dict_keys, METH_NOARGS, |
| 2325 | keys__doc__}, |
| 2326 | {"items", (PyCFunction)dict_items, METH_NOARGS, |
| 2327 | items__doc__}, |
| 2328 | {"values", (PyCFunction)dict_values, METH_NOARGS, |
| 2329 | values__doc__}, |
| 2330 | {"viewkeys", (PyCFunction)dictkeys_new, METH_NOARGS, |
| 2331 | viewkeys__doc__}, |
| 2332 | {"viewitems", (PyCFunction)dictitems_new, METH_NOARGS, |
| 2333 | viewitems__doc__}, |
| 2334 | {"viewvalues", (PyCFunction)dictvalues_new, METH_NOARGS, |
| 2335 | viewvalues__doc__}, |
| 2336 | {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS, |
| 2337 | update__doc__}, |
| 2338 | {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS, |
| 2339 | fromkeys__doc__}, |
| 2340 | {"clear", (PyCFunction)dict_clear, METH_NOARGS, |
| 2341 | clear__doc__}, |
| 2342 | {"copy", (PyCFunction)dict_copy, METH_NOARGS, |
| 2343 | copy__doc__}, |
| 2344 | {"iterkeys", (PyCFunction)dict_iterkeys, METH_NOARGS, |
| 2345 | iterkeys__doc__}, |
| 2346 | {"itervalues", (PyCFunction)dict_itervalues, METH_NOARGS, |
| 2347 | itervalues__doc__}, |
| 2348 | {"iteritems", (PyCFunction)dict_iteritems, METH_NOARGS, |
| 2349 | iteritems__doc__}, |
| 2350 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2351 | }; |
| 2352 | |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 2353 | /* 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] | 2354 | int |
| 2355 | PyDict_Contains(PyObject *op, PyObject *key) |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 2356 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2357 | long hash; |
| 2358 | PyDictObject *mp = (PyDictObject *)op; |
| 2359 | PyDictEntry *ep; |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 2360 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2361 | if (!PyString_CheckExact(key) || |
| 2362 | (hash = ((PyStringObject *) key)->ob_shash) == -1) { |
| 2363 | hash = PyObject_Hash(key); |
| 2364 | if (hash == -1) |
| 2365 | return -1; |
| 2366 | } |
| 2367 | ep = (mp->ma_lookup)(mp, key, hash); |
| 2368 | return ep == NULL ? -1 : (ep->me_value != NULL); |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 2369 | } |
| 2370 | |
Raymond Hettinger | d6fc72a | 2007-02-19 02:03:19 +0000 | [diff] [blame] | 2371 | /* Internal version of PyDict_Contains used when the hash value is already known */ |
| 2372 | int |
| 2373 | _PyDict_Contains(PyObject *op, PyObject *key, long hash) |
| 2374 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2375 | PyDictObject *mp = (PyDictObject *)op; |
| 2376 | PyDictEntry *ep; |
Raymond Hettinger | d6fc72a | 2007-02-19 02:03:19 +0000 | [diff] [blame] | 2377 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2378 | ep = (mp->ma_lookup)(mp, key, hash); |
| 2379 | return ep == NULL ? -1 : (ep->me_value != NULL); |
Raymond Hettinger | d6fc72a | 2007-02-19 02:03:19 +0000 | [diff] [blame] | 2380 | } |
| 2381 | |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 2382 | /* Hack to implement "key in dict" */ |
| 2383 | static PySequenceMethods dict_as_sequence = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2384 | 0, /* sq_length */ |
| 2385 | 0, /* sq_concat */ |
| 2386 | 0, /* sq_repeat */ |
| 2387 | 0, /* sq_item */ |
| 2388 | 0, /* sq_slice */ |
| 2389 | 0, /* sq_ass_item */ |
| 2390 | 0, /* sq_ass_slice */ |
| 2391 | PyDict_Contains, /* sq_contains */ |
| 2392 | 0, /* sq_inplace_concat */ |
| 2393 | 0, /* sq_inplace_repeat */ |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 2394 | }; |
| 2395 | |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 2396 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2397 | dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2398 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2399 | PyObject *self; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2400 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2401 | assert(type != NULL && type->tp_alloc != NULL); |
| 2402 | self = type->tp_alloc(type, 0); |
| 2403 | if (self != NULL) { |
| 2404 | PyDictObject *d = (PyDictObject *)self; |
| 2405 | /* It's guaranteed that tp->alloc zeroed out the struct. */ |
| 2406 | assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0); |
| 2407 | INIT_NONZERO_DICT_SLOTS(d); |
| 2408 | d->ma_lookup = lookdict_string; |
Ezio Melotti | c2077b0 | 2011-03-16 12:34:31 +0200 | [diff] [blame] | 2409 | /* The object has been implicitly tracked by tp_alloc */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2410 | if (type == &PyDict_Type) |
| 2411 | _PyObject_GC_UNTRACK(d); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2412 | #ifdef SHOW_CONVERSION_COUNTS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2413 | ++created; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2414 | #endif |
Antoine Pitrou | f8387af | 2009-03-23 18:41:45 +0000 | [diff] [blame] | 2415 | #ifdef SHOW_TRACK_COUNT |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2416 | if (_PyObject_GC_IS_TRACKED(d)) |
| 2417 | count_tracked++; |
| 2418 | else |
| 2419 | count_untracked++; |
Antoine Pitrou | f8387af | 2009-03-23 18:41:45 +0000 | [diff] [blame] | 2420 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2421 | } |
| 2422 | return self; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2423 | } |
| 2424 | |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 2425 | static int |
| 2426 | dict_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 2427 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2428 | return dict_update_common(self, args, kwds, "dict"); |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 2429 | } |
| 2430 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2431 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 2432 | dict_iter(PyDictObject *dict) |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 2433 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2434 | return dictiter_new(dict, &PyDictIterKey_Type); |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 2435 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2436 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2437 | PyDoc_STRVAR(dictionary_doc, |
Ezio Melotti | fb50112 | 2010-02-28 23:59:00 +0000 | [diff] [blame] | 2438 | "dict() -> new empty dictionary\n" |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 2439 | "dict(mapping) -> new dictionary initialized from a mapping object's\n" |
Ezio Melotti | fb50112 | 2010-02-28 23:59:00 +0000 | [diff] [blame] | 2440 | " (key, value) pairs\n" |
Georg Brandl | bca1169 | 2010-02-28 18:19:17 +0000 | [diff] [blame] | 2441 | "dict(iterable) -> new dictionary initialized as if via:\n" |
Tim Peters | 4d85953 | 2001-10-27 18:27:48 +0000 | [diff] [blame] | 2442 | " d = {}\n" |
Georg Brandl | bca1169 | 2010-02-28 18:19:17 +0000 | [diff] [blame] | 2443 | " for k, v in iterable:\n" |
Just van Rossum | a797d81 | 2002-11-23 09:45:04 +0000 | [diff] [blame] | 2444 | " d[k] = v\n" |
| 2445 | "dict(**kwargs) -> new dictionary initialized with the name=value pairs\n" |
| 2446 | " in the keyword argument list. For example: dict(one=1, two=2)"); |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 2447 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2448 | PyTypeObject PyDict_Type = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2449 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2450 | "dict", |
| 2451 | sizeof(PyDictObject), |
| 2452 | 0, |
| 2453 | (destructor)dict_dealloc, /* tp_dealloc */ |
| 2454 | (printfunc)dict_print, /* tp_print */ |
| 2455 | 0, /* tp_getattr */ |
| 2456 | 0, /* tp_setattr */ |
| 2457 | (cmpfunc)dict_compare, /* tp_compare */ |
| 2458 | (reprfunc)dict_repr, /* tp_repr */ |
| 2459 | 0, /* tp_as_number */ |
| 2460 | &dict_as_sequence, /* tp_as_sequence */ |
| 2461 | &dict_as_mapping, /* tp_as_mapping */ |
| 2462 | (hashfunc)PyObject_HashNotImplemented, /* tp_hash */ |
| 2463 | 0, /* tp_call */ |
| 2464 | 0, /* tp_str */ |
| 2465 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2466 | 0, /* tp_setattro */ |
| 2467 | 0, /* tp_as_buffer */ |
| 2468 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 2469 | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */ |
| 2470 | dictionary_doc, /* tp_doc */ |
| 2471 | dict_traverse, /* tp_traverse */ |
| 2472 | dict_tp_clear, /* tp_clear */ |
| 2473 | dict_richcompare, /* tp_richcompare */ |
| 2474 | 0, /* tp_weaklistoffset */ |
| 2475 | (getiterfunc)dict_iter, /* tp_iter */ |
| 2476 | 0, /* tp_iternext */ |
| 2477 | mapp_methods, /* tp_methods */ |
| 2478 | 0, /* tp_members */ |
| 2479 | 0, /* tp_getset */ |
| 2480 | 0, /* tp_base */ |
| 2481 | 0, /* tp_dict */ |
| 2482 | 0, /* tp_descr_get */ |
| 2483 | 0, /* tp_descr_set */ |
| 2484 | 0, /* tp_dictoffset */ |
| 2485 | dict_init, /* tp_init */ |
| 2486 | PyType_GenericAlloc, /* tp_alloc */ |
| 2487 | dict_new, /* tp_new */ |
| 2488 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2489 | }; |
| 2490 | |
Guido van Rossum | 3cca245 | 1997-05-16 14:23:33 +0000 | [diff] [blame] | 2491 | /* For backward compatibility with old dictionary interface */ |
| 2492 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2493 | PyObject * |
Martin v. Löwis | 32b4a1b | 2002-12-11 13:21:12 +0000 | [diff] [blame] | 2494 | PyDict_GetItemString(PyObject *v, const char *key) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2495 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2496 | PyObject *kv, *rv; |
| 2497 | kv = PyString_FromString(key); |
| 2498 | if (kv == NULL) |
| 2499 | return NULL; |
| 2500 | rv = PyDict_GetItem(v, kv); |
| 2501 | Py_DECREF(kv); |
| 2502 | return rv; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2503 | } |
| 2504 | |
| 2505 | int |
Martin v. Löwis | 32b4a1b | 2002-12-11 13:21:12 +0000 | [diff] [blame] | 2506 | PyDict_SetItemString(PyObject *v, const char *key, PyObject *item) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2507 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2508 | PyObject *kv; |
| 2509 | int err; |
| 2510 | kv = PyString_FromString(key); |
| 2511 | if (kv == NULL) |
| 2512 | return -1; |
| 2513 | PyString_InternInPlace(&kv); /* XXX Should we really? */ |
| 2514 | err = PyDict_SetItem(v, kv, item); |
| 2515 | Py_DECREF(kv); |
| 2516 | return err; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2517 | } |
| 2518 | |
| 2519 | int |
Martin v. Löwis | 32b4a1b | 2002-12-11 13:21:12 +0000 | [diff] [blame] | 2520 | PyDict_DelItemString(PyObject *v, const char *key) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2521 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2522 | PyObject *kv; |
| 2523 | int err; |
| 2524 | kv = PyString_FromString(key); |
| 2525 | if (kv == NULL) |
| 2526 | return -1; |
| 2527 | err = PyDict_DelItem(v, kv); |
| 2528 | Py_DECREF(kv); |
| 2529 | return err; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2530 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2531 | |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2532 | /* Dictionary iterator types */ |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2533 | |
| 2534 | typedef struct { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2535 | PyObject_HEAD |
| 2536 | PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */ |
| 2537 | Py_ssize_t di_used; |
| 2538 | Py_ssize_t di_pos; |
| 2539 | PyObject* di_result; /* reusable result tuple for iteritems */ |
| 2540 | Py_ssize_t len; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2541 | } dictiterobject; |
| 2542 | |
| 2543 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 2544 | dictiter_new(PyDictObject *dict, PyTypeObject *itertype) |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2545 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2546 | dictiterobject *di; |
| 2547 | di = PyObject_GC_New(dictiterobject, itertype); |
| 2548 | if (di == NULL) |
| 2549 | return NULL; |
| 2550 | Py_INCREF(dict); |
| 2551 | di->di_dict = dict; |
| 2552 | di->di_used = dict->ma_used; |
| 2553 | di->di_pos = 0; |
| 2554 | di->len = dict->ma_used; |
| 2555 | if (itertype == &PyDictIterItem_Type) { |
| 2556 | di->di_result = PyTuple_Pack(2, Py_None, Py_None); |
| 2557 | if (di->di_result == NULL) { |
| 2558 | Py_DECREF(di); |
| 2559 | return NULL; |
| 2560 | } |
| 2561 | } |
| 2562 | else |
| 2563 | di->di_result = NULL; |
| 2564 | _PyObject_GC_TRACK(di); |
| 2565 | return (PyObject *)di; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2566 | } |
| 2567 | |
| 2568 | static void |
| 2569 | dictiter_dealloc(dictiterobject *di) |
| 2570 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2571 | Py_XDECREF(di->di_dict); |
| 2572 | Py_XDECREF(di->di_result); |
| 2573 | PyObject_GC_Del(di); |
Antoine Pitrou | aa68790 | 2009-01-01 14:11:22 +0000 | [diff] [blame] | 2574 | } |
| 2575 | |
| 2576 | static int |
| 2577 | dictiter_traverse(dictiterobject *di, visitproc visit, void *arg) |
| 2578 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2579 | Py_VISIT(di->di_dict); |
| 2580 | Py_VISIT(di->di_result); |
| 2581 | return 0; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2582 | } |
| 2583 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 2584 | static PyObject * |
Raymond Hettinger | 0ce6dc8 | 2004-03-18 08:38:00 +0000 | [diff] [blame] | 2585 | dictiter_len(dictiterobject *di) |
| 2586 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2587 | Py_ssize_t len = 0; |
| 2588 | if (di->di_dict != NULL && di->di_used == di->di_dict->ma_used) |
| 2589 | len = di->len; |
| 2590 | return PyInt_FromSize_t(len); |
Raymond Hettinger | 0ce6dc8 | 2004-03-18 08:38:00 +0000 | [diff] [blame] | 2591 | } |
| 2592 | |
Armin Rigo | f5b3e36 | 2006-02-11 21:32:43 +0000 | [diff] [blame] | 2593 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 2594 | |
| 2595 | static PyMethodDef dictiter_methods[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2596 | {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS, length_hint_doc}, |
| 2597 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | 0ce6dc8 | 2004-03-18 08:38:00 +0000 | [diff] [blame] | 2598 | }; |
| 2599 | |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2600 | static PyObject *dictiter_iternextkey(dictiterobject *di) |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2601 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2602 | PyObject *key; |
| 2603 | register Py_ssize_t i, mask; |
| 2604 | register PyDictEntry *ep; |
| 2605 | PyDictObject *d = di->di_dict; |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2606 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2607 | if (d == NULL) |
| 2608 | return NULL; |
| 2609 | assert (PyDict_Check(d)); |
Guido van Rossum | 2147df7 | 2002-07-16 20:30:22 +0000 | [diff] [blame] | 2610 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2611 | if (di->di_used != d->ma_used) { |
| 2612 | PyErr_SetString(PyExc_RuntimeError, |
| 2613 | "dictionary changed size during iteration"); |
| 2614 | di->di_used = -1; /* Make this state sticky */ |
| 2615 | return NULL; |
| 2616 | } |
Guido van Rossum | 2147df7 | 2002-07-16 20:30:22 +0000 | [diff] [blame] | 2617 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2618 | i = di->di_pos; |
| 2619 | if (i < 0) |
| 2620 | goto fail; |
| 2621 | ep = d->ma_table; |
| 2622 | mask = d->ma_mask; |
| 2623 | while (i <= mask && ep[i].me_value == NULL) |
| 2624 | i++; |
| 2625 | di->di_pos = i+1; |
| 2626 | if (i > mask) |
| 2627 | goto fail; |
| 2628 | di->len--; |
| 2629 | key = ep[i].me_key; |
| 2630 | Py_INCREF(key); |
| 2631 | return key; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2632 | |
| 2633 | fail: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2634 | di->di_dict = NULL; |
Serhiy Storchaka | 14a7d63 | 2016-03-30 20:43:06 +0300 | [diff] [blame] | 2635 | Py_DECREF(d); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2636 | return NULL; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2637 | } |
| 2638 | |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2639 | PyTypeObject PyDictIterKey_Type = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2640 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2641 | "dictionary-keyiterator", /* tp_name */ |
| 2642 | sizeof(dictiterobject), /* tp_basicsize */ |
| 2643 | 0, /* tp_itemsize */ |
| 2644 | /* methods */ |
| 2645 | (destructor)dictiter_dealloc, /* tp_dealloc */ |
| 2646 | 0, /* tp_print */ |
| 2647 | 0, /* tp_getattr */ |
| 2648 | 0, /* tp_setattr */ |
| 2649 | 0, /* tp_compare */ |
| 2650 | 0, /* tp_repr */ |
| 2651 | 0, /* tp_as_number */ |
| 2652 | 0, /* tp_as_sequence */ |
| 2653 | 0, /* tp_as_mapping */ |
| 2654 | 0, /* tp_hash */ |
| 2655 | 0, /* tp_call */ |
| 2656 | 0, /* tp_str */ |
| 2657 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2658 | 0, /* tp_setattro */ |
| 2659 | 0, /* tp_as_buffer */ |
| 2660 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 2661 | 0, /* tp_doc */ |
| 2662 | (traverseproc)dictiter_traverse, /* tp_traverse */ |
| 2663 | 0, /* tp_clear */ |
| 2664 | 0, /* tp_richcompare */ |
| 2665 | 0, /* tp_weaklistoffset */ |
| 2666 | PyObject_SelfIter, /* tp_iter */ |
| 2667 | (iternextfunc)dictiter_iternextkey, /* tp_iternext */ |
| 2668 | dictiter_methods, /* tp_methods */ |
| 2669 | 0, |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2670 | }; |
| 2671 | |
| 2672 | static PyObject *dictiter_iternextvalue(dictiterobject *di) |
| 2673 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2674 | PyObject *value; |
| 2675 | register Py_ssize_t i, mask; |
| 2676 | register PyDictEntry *ep; |
| 2677 | PyDictObject *d = di->di_dict; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2678 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2679 | if (d == NULL) |
| 2680 | return NULL; |
| 2681 | assert (PyDict_Check(d)); |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2682 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2683 | if (di->di_used != d->ma_used) { |
| 2684 | PyErr_SetString(PyExc_RuntimeError, |
| 2685 | "dictionary changed size during iteration"); |
| 2686 | di->di_used = -1; /* Make this state sticky */ |
| 2687 | return NULL; |
| 2688 | } |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2689 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2690 | i = di->di_pos; |
| 2691 | mask = d->ma_mask; |
| 2692 | if (i < 0 || i > mask) |
| 2693 | goto fail; |
| 2694 | ep = d->ma_table; |
| 2695 | while ((value=ep[i].me_value) == NULL) { |
| 2696 | i++; |
| 2697 | if (i > mask) |
| 2698 | goto fail; |
| 2699 | } |
| 2700 | di->di_pos = i+1; |
| 2701 | di->len--; |
| 2702 | Py_INCREF(value); |
| 2703 | return value; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2704 | |
| 2705 | fail: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2706 | di->di_dict = NULL; |
Serhiy Storchaka | 14a7d63 | 2016-03-30 20:43:06 +0300 | [diff] [blame] | 2707 | Py_DECREF(d); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2708 | return NULL; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2709 | } |
| 2710 | |
| 2711 | PyTypeObject PyDictIterValue_Type = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2712 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2713 | "dictionary-valueiterator", /* tp_name */ |
| 2714 | sizeof(dictiterobject), /* tp_basicsize */ |
| 2715 | 0, /* tp_itemsize */ |
| 2716 | /* methods */ |
| 2717 | (destructor)dictiter_dealloc, /* tp_dealloc */ |
| 2718 | 0, /* tp_print */ |
| 2719 | 0, /* tp_getattr */ |
| 2720 | 0, /* tp_setattr */ |
| 2721 | 0, /* tp_compare */ |
| 2722 | 0, /* tp_repr */ |
| 2723 | 0, /* tp_as_number */ |
| 2724 | 0, /* tp_as_sequence */ |
| 2725 | 0, /* tp_as_mapping */ |
| 2726 | 0, /* tp_hash */ |
| 2727 | 0, /* tp_call */ |
| 2728 | 0, /* tp_str */ |
| 2729 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2730 | 0, /* tp_setattro */ |
| 2731 | 0, /* tp_as_buffer */ |
| 2732 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 2733 | 0, /* tp_doc */ |
| 2734 | (traverseproc)dictiter_traverse, /* tp_traverse */ |
| 2735 | 0, /* tp_clear */ |
| 2736 | 0, /* tp_richcompare */ |
| 2737 | 0, /* tp_weaklistoffset */ |
| 2738 | PyObject_SelfIter, /* tp_iter */ |
| 2739 | (iternextfunc)dictiter_iternextvalue, /* tp_iternext */ |
| 2740 | dictiter_methods, /* tp_methods */ |
| 2741 | 0, |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2742 | }; |
| 2743 | |
| 2744 | static PyObject *dictiter_iternextitem(dictiterobject *di) |
| 2745 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2746 | PyObject *key, *value, *result = di->di_result; |
| 2747 | register Py_ssize_t i, mask; |
| 2748 | register PyDictEntry *ep; |
| 2749 | PyDictObject *d = di->di_dict; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2750 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2751 | if (d == NULL) |
| 2752 | return NULL; |
| 2753 | assert (PyDict_Check(d)); |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2754 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2755 | if (di->di_used != d->ma_used) { |
| 2756 | PyErr_SetString(PyExc_RuntimeError, |
| 2757 | "dictionary changed size during iteration"); |
| 2758 | di->di_used = -1; /* Make this state sticky */ |
| 2759 | return NULL; |
| 2760 | } |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2761 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2762 | i = di->di_pos; |
| 2763 | if (i < 0) |
| 2764 | goto fail; |
| 2765 | ep = d->ma_table; |
| 2766 | mask = d->ma_mask; |
| 2767 | while (i <= mask && ep[i].me_value == NULL) |
| 2768 | i++; |
| 2769 | di->di_pos = i+1; |
| 2770 | if (i > mask) |
| 2771 | goto fail; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2772 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2773 | if (result->ob_refcnt == 1) { |
| 2774 | Py_INCREF(result); |
| 2775 | Py_DECREF(PyTuple_GET_ITEM(result, 0)); |
| 2776 | Py_DECREF(PyTuple_GET_ITEM(result, 1)); |
| 2777 | } else { |
| 2778 | result = PyTuple_New(2); |
| 2779 | if (result == NULL) |
| 2780 | return NULL; |
| 2781 | } |
| 2782 | di->len--; |
| 2783 | key = ep[i].me_key; |
| 2784 | value = ep[i].me_value; |
| 2785 | Py_INCREF(key); |
| 2786 | Py_INCREF(value); |
| 2787 | PyTuple_SET_ITEM(result, 0, key); |
| 2788 | PyTuple_SET_ITEM(result, 1, value); |
| 2789 | return result; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2790 | |
| 2791 | fail: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2792 | di->di_dict = NULL; |
Serhiy Storchaka | 14a7d63 | 2016-03-30 20:43:06 +0300 | [diff] [blame] | 2793 | Py_DECREF(d); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2794 | return NULL; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2795 | } |
| 2796 | |
| 2797 | PyTypeObject PyDictIterItem_Type = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2798 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2799 | "dictionary-itemiterator", /* tp_name */ |
| 2800 | sizeof(dictiterobject), /* tp_basicsize */ |
| 2801 | 0, /* tp_itemsize */ |
| 2802 | /* methods */ |
| 2803 | (destructor)dictiter_dealloc, /* tp_dealloc */ |
| 2804 | 0, /* tp_print */ |
| 2805 | 0, /* tp_getattr */ |
| 2806 | 0, /* tp_setattr */ |
| 2807 | 0, /* tp_compare */ |
| 2808 | 0, /* tp_repr */ |
| 2809 | 0, /* tp_as_number */ |
| 2810 | 0, /* tp_as_sequence */ |
| 2811 | 0, /* tp_as_mapping */ |
| 2812 | 0, /* tp_hash */ |
| 2813 | 0, /* tp_call */ |
| 2814 | 0, /* tp_str */ |
| 2815 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2816 | 0, /* tp_setattro */ |
| 2817 | 0, /* tp_as_buffer */ |
| 2818 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 2819 | 0, /* tp_doc */ |
| 2820 | (traverseproc)dictiter_traverse, /* tp_traverse */ |
| 2821 | 0, /* tp_clear */ |
| 2822 | 0, /* tp_richcompare */ |
| 2823 | 0, /* tp_weaklistoffset */ |
| 2824 | PyObject_SelfIter, /* tp_iter */ |
| 2825 | (iternextfunc)dictiter_iternextitem, /* tp_iternext */ |
| 2826 | dictiter_methods, /* tp_methods */ |
| 2827 | 0, |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2828 | }; |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 2829 | |
| 2830 | /***********************************************/ |
| 2831 | /* View objects for keys(), items(), values(). */ |
| 2832 | /***********************************************/ |
| 2833 | |
| 2834 | /* The instance lay-out is the same for all three; but the type differs. */ |
| 2835 | |
| 2836 | typedef struct { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2837 | PyObject_HEAD |
| 2838 | PyDictObject *dv_dict; |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 2839 | } dictviewobject; |
| 2840 | |
| 2841 | |
| 2842 | static void |
| 2843 | dictview_dealloc(dictviewobject *dv) |
| 2844 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2845 | Py_XDECREF(dv->dv_dict); |
| 2846 | PyObject_GC_Del(dv); |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 2847 | } |
| 2848 | |
| 2849 | static int |
| 2850 | dictview_traverse(dictviewobject *dv, visitproc visit, void *arg) |
| 2851 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2852 | Py_VISIT(dv->dv_dict); |
| 2853 | return 0; |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 2854 | } |
| 2855 | |
| 2856 | static Py_ssize_t |
| 2857 | dictview_len(dictviewobject *dv) |
| 2858 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2859 | Py_ssize_t len = 0; |
| 2860 | if (dv->dv_dict != NULL) |
| 2861 | len = dv->dv_dict->ma_used; |
| 2862 | return len; |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 2863 | } |
| 2864 | |
| 2865 | static PyObject * |
| 2866 | dictview_new(PyObject *dict, PyTypeObject *type) |
| 2867 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2868 | dictviewobject *dv; |
| 2869 | if (dict == NULL) { |
| 2870 | PyErr_BadInternalCall(); |
| 2871 | return NULL; |
| 2872 | } |
| 2873 | if (!PyDict_Check(dict)) { |
| 2874 | /* XXX Get rid of this restriction later */ |
| 2875 | PyErr_Format(PyExc_TypeError, |
| 2876 | "%s() requires a dict argument, not '%s'", |
| 2877 | type->tp_name, dict->ob_type->tp_name); |
| 2878 | return NULL; |
| 2879 | } |
| 2880 | dv = PyObject_GC_New(dictviewobject, type); |
| 2881 | if (dv == NULL) |
| 2882 | return NULL; |
| 2883 | Py_INCREF(dict); |
| 2884 | dv->dv_dict = (PyDictObject *)dict; |
| 2885 | _PyObject_GC_TRACK(dv); |
| 2886 | return (PyObject *)dv; |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 2887 | } |
| 2888 | |
| 2889 | /* TODO(guido): The views objects are not complete: |
| 2890 | |
| 2891 | * support more set operations |
| 2892 | * support arbitrary mappings? |
| 2893 | - either these should be static or exported in dictobject.h |
| 2894 | - if public then they should probably be in builtins |
| 2895 | */ |
| 2896 | |
| 2897 | /* Return 1 if self is a subset of other, iterating over self; |
| 2898 | 0 if not; -1 if an error occurred. */ |
| 2899 | static int |
| 2900 | all_contained_in(PyObject *self, PyObject *other) |
| 2901 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2902 | PyObject *iter = PyObject_GetIter(self); |
| 2903 | int ok = 1; |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 2904 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2905 | if (iter == NULL) |
| 2906 | return -1; |
| 2907 | for (;;) { |
| 2908 | PyObject *next = PyIter_Next(iter); |
| 2909 | if (next == NULL) { |
| 2910 | if (PyErr_Occurred()) |
| 2911 | ok = -1; |
| 2912 | break; |
| 2913 | } |
| 2914 | ok = PySequence_Contains(other, next); |
| 2915 | Py_DECREF(next); |
| 2916 | if (ok <= 0) |
| 2917 | break; |
| 2918 | } |
| 2919 | Py_DECREF(iter); |
| 2920 | return ok; |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 2921 | } |
| 2922 | |
| 2923 | static PyObject * |
| 2924 | dictview_richcompare(PyObject *self, PyObject *other, int op) |
| 2925 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2926 | Py_ssize_t len_self, len_other; |
| 2927 | int ok; |
| 2928 | PyObject *result; |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 2929 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2930 | assert(self != NULL); |
| 2931 | assert(PyDictViewSet_Check(self)); |
| 2932 | assert(other != NULL); |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 2933 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2934 | if (!PyAnySet_Check(other) && !PyDictViewSet_Check(other)) { |
| 2935 | Py_INCREF(Py_NotImplemented); |
| 2936 | return Py_NotImplemented; |
| 2937 | } |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 2938 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2939 | len_self = PyObject_Size(self); |
| 2940 | if (len_self < 0) |
| 2941 | return NULL; |
| 2942 | len_other = PyObject_Size(other); |
| 2943 | if (len_other < 0) |
| 2944 | return NULL; |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 2945 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2946 | ok = 0; |
| 2947 | switch(op) { |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 2948 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2949 | case Py_NE: |
| 2950 | case Py_EQ: |
| 2951 | if (len_self == len_other) |
| 2952 | ok = all_contained_in(self, other); |
| 2953 | if (op == Py_NE && ok >= 0) |
| 2954 | ok = !ok; |
| 2955 | break; |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 2956 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2957 | case Py_LT: |
| 2958 | if (len_self < len_other) |
| 2959 | ok = all_contained_in(self, other); |
| 2960 | break; |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 2961 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2962 | case Py_LE: |
| 2963 | if (len_self <= len_other) |
| 2964 | ok = all_contained_in(self, other); |
| 2965 | break; |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 2966 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2967 | case Py_GT: |
| 2968 | if (len_self > len_other) |
| 2969 | ok = all_contained_in(other, self); |
| 2970 | break; |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 2971 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2972 | case Py_GE: |
| 2973 | if (len_self >= len_other) |
| 2974 | ok = all_contained_in(other, self); |
| 2975 | break; |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 2976 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2977 | } |
| 2978 | if (ok < 0) |
| 2979 | return NULL; |
| 2980 | result = ok ? Py_True : Py_False; |
| 2981 | Py_INCREF(result); |
| 2982 | return result; |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 2983 | } |
| 2984 | |
| 2985 | static PyObject * |
| 2986 | dictview_repr(dictviewobject *dv) |
| 2987 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2988 | PyObject *seq; |
| 2989 | PyObject *seq_str; |
| 2990 | PyObject *result; |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 2991 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2992 | seq = PySequence_List((PyObject *)dv); |
| 2993 | if (seq == NULL) |
| 2994 | return NULL; |
| 2995 | |
| 2996 | seq_str = PyObject_Repr(seq); |
Benjamin Peterson | b91ef00 | 2013-05-19 19:38:12 -0700 | [diff] [blame] | 2997 | if (seq_str == NULL) { |
| 2998 | Py_DECREF(seq); |
| 2999 | return NULL; |
| 3000 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3001 | result = PyString_FromFormat("%s(%s)", Py_TYPE(dv)->tp_name, |
| 3002 | PyString_AS_STRING(seq_str)); |
| 3003 | Py_DECREF(seq_str); |
| 3004 | Py_DECREF(seq); |
| 3005 | return result; |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 3006 | } |
| 3007 | |
| 3008 | /*** dict_keys ***/ |
| 3009 | |
| 3010 | static PyObject * |
| 3011 | dictkeys_iter(dictviewobject *dv) |
| 3012 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3013 | if (dv->dv_dict == NULL) { |
| 3014 | Py_RETURN_NONE; |
| 3015 | } |
| 3016 | return dictiter_new(dv->dv_dict, &PyDictIterKey_Type); |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 3017 | } |
| 3018 | |
| 3019 | static int |
| 3020 | dictkeys_contains(dictviewobject *dv, PyObject *obj) |
| 3021 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3022 | if (dv->dv_dict == NULL) |
| 3023 | return 0; |
| 3024 | return PyDict_Contains((PyObject *)dv->dv_dict, obj); |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 3025 | } |
| 3026 | |
| 3027 | static PySequenceMethods dictkeys_as_sequence = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3028 | (lenfunc)dictview_len, /* sq_length */ |
| 3029 | 0, /* sq_concat */ |
| 3030 | 0, /* sq_repeat */ |
| 3031 | 0, /* sq_item */ |
| 3032 | 0, /* sq_slice */ |
| 3033 | 0, /* sq_ass_item */ |
| 3034 | 0, /* sq_ass_slice */ |
| 3035 | (objobjproc)dictkeys_contains, /* sq_contains */ |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 3036 | }; |
| 3037 | |
| 3038 | static PyObject* |
| 3039 | dictviews_sub(PyObject* self, PyObject *other) |
| 3040 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3041 | PyObject *result = PySet_New(self); |
| 3042 | PyObject *tmp; |
| 3043 | if (result == NULL) |
| 3044 | return NULL; |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 3045 | |
Benjamin Peterson | 4ddb44a | 2016-03-03 22:05:36 -0800 | [diff] [blame] | 3046 | |
| 3047 | tmp = PyObject_CallMethod(result, "difference_update", "(O)", other); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3048 | if (tmp == NULL) { |
| 3049 | Py_DECREF(result); |
| 3050 | return NULL; |
| 3051 | } |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 3052 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3053 | Py_DECREF(tmp); |
| 3054 | return result; |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 3055 | } |
| 3056 | |
| 3057 | static PyObject* |
| 3058 | dictviews_and(PyObject* self, PyObject *other) |
| 3059 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3060 | PyObject *result = PySet_New(self); |
| 3061 | PyObject *tmp; |
| 3062 | if (result == NULL) |
| 3063 | return NULL; |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 3064 | |
Benjamin Peterson | 4ddb44a | 2016-03-03 22:05:36 -0800 | [diff] [blame] | 3065 | tmp = PyObject_CallMethod(result, "intersection_update", "(O)", other); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3066 | if (tmp == NULL) { |
| 3067 | Py_DECREF(result); |
| 3068 | return NULL; |
| 3069 | } |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 3070 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3071 | Py_DECREF(tmp); |
| 3072 | return result; |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 3073 | } |
| 3074 | |
| 3075 | static PyObject* |
| 3076 | dictviews_or(PyObject* self, PyObject *other) |
| 3077 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3078 | PyObject *result = PySet_New(self); |
| 3079 | PyObject *tmp; |
| 3080 | if (result == NULL) |
| 3081 | return NULL; |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 3082 | |
Benjamin Peterson | 4ddb44a | 2016-03-03 22:05:36 -0800 | [diff] [blame] | 3083 | tmp = PyObject_CallMethod(result, "update", "(O)", other); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3084 | if (tmp == NULL) { |
| 3085 | Py_DECREF(result); |
| 3086 | return NULL; |
| 3087 | } |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 3088 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3089 | Py_DECREF(tmp); |
| 3090 | return result; |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 3091 | } |
| 3092 | |
| 3093 | static PyObject* |
| 3094 | dictviews_xor(PyObject* self, PyObject *other) |
| 3095 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3096 | PyObject *result = PySet_New(self); |
| 3097 | PyObject *tmp; |
| 3098 | if (result == NULL) |
| 3099 | return NULL; |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 3100 | |
Benjamin Peterson | 4ddb44a | 2016-03-03 22:05:36 -0800 | [diff] [blame] | 3101 | tmp = PyObject_CallMethod(result, "symmetric_difference_update", "(O)", other); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3102 | if (tmp == NULL) { |
| 3103 | Py_DECREF(result); |
| 3104 | return NULL; |
| 3105 | } |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 3106 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3107 | Py_DECREF(tmp); |
| 3108 | return result; |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 3109 | } |
| 3110 | |
| 3111 | static PyNumberMethods dictviews_as_number = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3112 | 0, /*nb_add*/ |
| 3113 | (binaryfunc)dictviews_sub, /*nb_subtract*/ |
| 3114 | 0, /*nb_multiply*/ |
| 3115 | 0, /*nb_divide*/ |
| 3116 | 0, /*nb_remainder*/ |
| 3117 | 0, /*nb_divmod*/ |
| 3118 | 0, /*nb_power*/ |
| 3119 | 0, /*nb_negative*/ |
| 3120 | 0, /*nb_positive*/ |
| 3121 | 0, /*nb_absolute*/ |
| 3122 | 0, /*nb_nonzero*/ |
| 3123 | 0, /*nb_invert*/ |
| 3124 | 0, /*nb_lshift*/ |
| 3125 | 0, /*nb_rshift*/ |
| 3126 | (binaryfunc)dictviews_and, /*nb_and*/ |
| 3127 | (binaryfunc)dictviews_xor, /*nb_xor*/ |
| 3128 | (binaryfunc)dictviews_or, /*nb_or*/ |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 3129 | }; |
| 3130 | |
| 3131 | static PyMethodDef dictkeys_methods[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3132 | {NULL, NULL} /* sentinel */ |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 3133 | }; |
| 3134 | |
| 3135 | PyTypeObject PyDictKeys_Type = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3136 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 3137 | "dict_keys", /* tp_name */ |
| 3138 | sizeof(dictviewobject), /* tp_basicsize */ |
| 3139 | 0, /* tp_itemsize */ |
| 3140 | /* methods */ |
| 3141 | (destructor)dictview_dealloc, /* tp_dealloc */ |
| 3142 | 0, /* tp_print */ |
| 3143 | 0, /* tp_getattr */ |
| 3144 | 0, /* tp_setattr */ |
| 3145 | 0, /* tp_reserved */ |
| 3146 | (reprfunc)dictview_repr, /* tp_repr */ |
| 3147 | &dictviews_as_number, /* tp_as_number */ |
| 3148 | &dictkeys_as_sequence, /* tp_as_sequence */ |
| 3149 | 0, /* tp_as_mapping */ |
| 3150 | 0, /* tp_hash */ |
| 3151 | 0, /* tp_call */ |
| 3152 | 0, /* tp_str */ |
| 3153 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3154 | 0, /* tp_setattro */ |
| 3155 | 0, /* tp_as_buffer */ |
| 3156 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 3157 | Py_TPFLAGS_CHECKTYPES, /* tp_flags */ |
| 3158 | 0, /* tp_doc */ |
| 3159 | (traverseproc)dictview_traverse, /* tp_traverse */ |
| 3160 | 0, /* tp_clear */ |
| 3161 | dictview_richcompare, /* tp_richcompare */ |
| 3162 | 0, /* tp_weaklistoffset */ |
| 3163 | (getiterfunc)dictkeys_iter, /* tp_iter */ |
| 3164 | 0, /* tp_iternext */ |
| 3165 | dictkeys_methods, /* tp_methods */ |
| 3166 | 0, |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 3167 | }; |
| 3168 | |
| 3169 | static PyObject * |
| 3170 | dictkeys_new(PyObject *dict) |
| 3171 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3172 | return dictview_new(dict, &PyDictKeys_Type); |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 3173 | } |
| 3174 | |
| 3175 | /*** dict_items ***/ |
| 3176 | |
| 3177 | static PyObject * |
| 3178 | dictitems_iter(dictviewobject *dv) |
| 3179 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3180 | if (dv->dv_dict == NULL) { |
| 3181 | Py_RETURN_NONE; |
| 3182 | } |
| 3183 | return dictiter_new(dv->dv_dict, &PyDictIterItem_Type); |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 3184 | } |
| 3185 | |
| 3186 | static int |
| 3187 | dictitems_contains(dictviewobject *dv, PyObject *obj) |
| 3188 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3189 | PyObject *key, *value, *found; |
| 3190 | if (dv->dv_dict == NULL) |
| 3191 | return 0; |
| 3192 | if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 2) |
| 3193 | return 0; |
| 3194 | key = PyTuple_GET_ITEM(obj, 0); |
| 3195 | value = PyTuple_GET_ITEM(obj, 1); |
| 3196 | found = PyDict_GetItem((PyObject *)dv->dv_dict, key); |
| 3197 | if (found == NULL) { |
| 3198 | if (PyErr_Occurred()) |
| 3199 | return -1; |
| 3200 | return 0; |
| 3201 | } |
| 3202 | return PyObject_RichCompareBool(value, found, Py_EQ); |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 3203 | } |
| 3204 | |
| 3205 | static PySequenceMethods dictitems_as_sequence = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3206 | (lenfunc)dictview_len, /* sq_length */ |
| 3207 | 0, /* sq_concat */ |
| 3208 | 0, /* sq_repeat */ |
| 3209 | 0, /* sq_item */ |
| 3210 | 0, /* sq_slice */ |
| 3211 | 0, /* sq_ass_item */ |
| 3212 | 0, /* sq_ass_slice */ |
| 3213 | (objobjproc)dictitems_contains, /* sq_contains */ |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 3214 | }; |
| 3215 | |
| 3216 | static PyMethodDef dictitems_methods[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3217 | {NULL, NULL} /* sentinel */ |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 3218 | }; |
| 3219 | |
| 3220 | PyTypeObject PyDictItems_Type = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3221 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 3222 | "dict_items", /* tp_name */ |
| 3223 | sizeof(dictviewobject), /* tp_basicsize */ |
| 3224 | 0, /* tp_itemsize */ |
| 3225 | /* methods */ |
| 3226 | (destructor)dictview_dealloc, /* tp_dealloc */ |
| 3227 | 0, /* tp_print */ |
| 3228 | 0, /* tp_getattr */ |
| 3229 | 0, /* tp_setattr */ |
| 3230 | 0, /* tp_reserved */ |
| 3231 | (reprfunc)dictview_repr, /* tp_repr */ |
| 3232 | &dictviews_as_number, /* tp_as_number */ |
| 3233 | &dictitems_as_sequence, /* tp_as_sequence */ |
| 3234 | 0, /* tp_as_mapping */ |
| 3235 | 0, /* tp_hash */ |
| 3236 | 0, /* tp_call */ |
| 3237 | 0, /* tp_str */ |
| 3238 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3239 | 0, /* tp_setattro */ |
| 3240 | 0, /* tp_as_buffer */ |
| 3241 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 3242 | Py_TPFLAGS_CHECKTYPES, /* tp_flags */ |
| 3243 | 0, /* tp_doc */ |
| 3244 | (traverseproc)dictview_traverse, /* tp_traverse */ |
| 3245 | 0, /* tp_clear */ |
| 3246 | dictview_richcompare, /* tp_richcompare */ |
| 3247 | 0, /* tp_weaklistoffset */ |
| 3248 | (getiterfunc)dictitems_iter, /* tp_iter */ |
| 3249 | 0, /* tp_iternext */ |
| 3250 | dictitems_methods, /* tp_methods */ |
| 3251 | 0, |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 3252 | }; |
| 3253 | |
| 3254 | static PyObject * |
| 3255 | dictitems_new(PyObject *dict) |
| 3256 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3257 | return dictview_new(dict, &PyDictItems_Type); |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 3258 | } |
| 3259 | |
| 3260 | /*** dict_values ***/ |
| 3261 | |
| 3262 | static PyObject * |
| 3263 | dictvalues_iter(dictviewobject *dv) |
| 3264 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3265 | if (dv->dv_dict == NULL) { |
| 3266 | Py_RETURN_NONE; |
| 3267 | } |
| 3268 | return dictiter_new(dv->dv_dict, &PyDictIterValue_Type); |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 3269 | } |
| 3270 | |
| 3271 | static PySequenceMethods dictvalues_as_sequence = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3272 | (lenfunc)dictview_len, /* sq_length */ |
| 3273 | 0, /* sq_concat */ |
| 3274 | 0, /* sq_repeat */ |
| 3275 | 0, /* sq_item */ |
| 3276 | 0, /* sq_slice */ |
| 3277 | 0, /* sq_ass_item */ |
| 3278 | 0, /* sq_ass_slice */ |
| 3279 | (objobjproc)0, /* sq_contains */ |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 3280 | }; |
| 3281 | |
| 3282 | static PyMethodDef dictvalues_methods[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3283 | {NULL, NULL} /* sentinel */ |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 3284 | }; |
| 3285 | |
| 3286 | PyTypeObject PyDictValues_Type = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3287 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 3288 | "dict_values", /* tp_name */ |
| 3289 | sizeof(dictviewobject), /* tp_basicsize */ |
| 3290 | 0, /* tp_itemsize */ |
| 3291 | /* methods */ |
| 3292 | (destructor)dictview_dealloc, /* tp_dealloc */ |
| 3293 | 0, /* tp_print */ |
| 3294 | 0, /* tp_getattr */ |
| 3295 | 0, /* tp_setattr */ |
| 3296 | 0, /* tp_reserved */ |
| 3297 | (reprfunc)dictview_repr, /* tp_repr */ |
| 3298 | 0, /* tp_as_number */ |
| 3299 | &dictvalues_as_sequence, /* tp_as_sequence */ |
| 3300 | 0, /* tp_as_mapping */ |
| 3301 | 0, /* tp_hash */ |
| 3302 | 0, /* tp_call */ |
| 3303 | 0, /* tp_str */ |
| 3304 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3305 | 0, /* tp_setattro */ |
| 3306 | 0, /* tp_as_buffer */ |
| 3307 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 3308 | 0, /* tp_doc */ |
| 3309 | (traverseproc)dictview_traverse, /* tp_traverse */ |
| 3310 | 0, /* tp_clear */ |
| 3311 | 0, /* tp_richcompare */ |
| 3312 | 0, /* tp_weaklistoffset */ |
| 3313 | (getiterfunc)dictvalues_iter, /* tp_iter */ |
| 3314 | 0, /* tp_iternext */ |
| 3315 | dictvalues_methods, /* tp_methods */ |
| 3316 | 0, |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 3317 | }; |
| 3318 | |
| 3319 | static PyObject * |
| 3320 | dictvalues_new(PyObject *dict) |
| 3321 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 3322 | return dictview_new(dict, &PyDictValues_Type); |
Alexandre Vassalotti | 69eb516 | 2010-01-11 23:17:10 +0000 | [diff] [blame] | 3323 | } |