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