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