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