Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 2bc1379 | 1999-03-24 19:06:42 +0000 | [diff] [blame] | 2 | /* Dictionary object implementation using a hash table */ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 3 | |
Raymond Hettinger | 930427b | 2003-05-03 06:51:59 +0000 | [diff] [blame] | 4 | /* The distribution includes a separate file, Objects/dictnotes.txt, |
Tim Peters | 60b2996 | 2006-01-01 01:19:23 +0000 | [diff] [blame] | 5 | describing explorations into dictionary design and optimization. |
Raymond Hettinger | 930427b | 2003-05-03 06:51:59 +0000 | [diff] [blame] | 6 | It covers typical dictionary use patterns, the parameters for |
| 7 | tuning dictionaries, and several ideas for possible optimizations. |
| 8 | */ |
| 9 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 10 | #include "Python.h" |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 11 | |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 12 | |
Georg Brandl | b9f4ad3 | 2006-10-29 18:31:42 +0000 | [diff] [blame] | 13 | /* Set a key error with the specified argument, wrapping it in a |
| 14 | * tuple automatically so that tuple keys are not unpacked as the |
| 15 | * exception arguments. */ |
| 16 | static void |
| 17 | set_key_error(PyObject *arg) |
| 18 | { |
| 19 | PyObject *tup; |
| 20 | tup = PyTuple_Pack(1, arg); |
| 21 | if (!tup) |
| 22 | return; /* caller will expect error to be set anyway */ |
| 23 | PyErr_SetObject(PyExc_KeyError, tup); |
Neal Norwitz | 7b932da | 2006-10-29 23:39:03 +0000 | [diff] [blame] | 24 | Py_DECREF(tup); |
Georg Brandl | b9f4ad3 | 2006-10-29 18:31:42 +0000 | [diff] [blame] | 25 | } |
| 26 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 27 | /* Define this out if you don't want conversion statistics on exit. */ |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 28 | #undef SHOW_CONVERSION_COUNTS |
| 29 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 30 | /* See large comment block below. This must be >= 1. */ |
| 31 | #define PERTURB_SHIFT 5 |
| 32 | |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 33 | /* |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 34 | Major subtleties ahead: Most hash schemes depend on having a "good" hash |
| 35 | function, in the sense of simulating randomness. Python doesn't: its most |
| 36 | important hash functions (for strings and ints) are very regular in common |
| 37 | cases: |
Tim Peters | 15d4929 | 2001-05-27 07:39:22 +0000 | [diff] [blame] | 38 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 39 | >>> map(hash, (0, 1, 2, 3)) |
| 40 | [0, 1, 2, 3] |
| 41 | >>> map(hash, ("namea", "nameb", "namec", "named")) |
| 42 | [-1658398457, -1658398460, -1658398459, -1658398462] |
| 43 | >>> |
Tim Peters | 15d4929 | 2001-05-27 07:39:22 +0000 | [diff] [blame] | 44 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 45 | This isn't necessarily bad! To the contrary, in a table of size 2**i, taking |
| 46 | the low-order i bits as the initial table index is extremely fast, and there |
| 47 | are no collisions at all for dicts indexed by a contiguous range of ints. |
| 48 | The same is approximately true when keys are "consecutive" strings. So this |
| 49 | gives better-than-random behavior in common cases, and that's very desirable. |
Tim Peters | 15d4929 | 2001-05-27 07:39:22 +0000 | [diff] [blame] | 50 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 51 | OTOH, when collisions occur, the tendency to fill contiguous slices of the |
| 52 | hash table makes a good collision resolution strategy crucial. Taking only |
| 53 | the last i bits of the hash code is also vulnerable: for example, consider |
| 54 | [i << 16 for i in range(20000)] as a set of keys. Since ints are their own |
| 55 | hash codes, and this fits in a dict of size 2**15, the last 15 bits of every |
| 56 | hash code are all 0: they *all* map to the same table index. |
Tim Peters | 15d4929 | 2001-05-27 07:39:22 +0000 | [diff] [blame] | 57 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 58 | But catering to unusual cases should not slow the usual ones, so we just take |
| 59 | the last i bits anyway. It's up to collision resolution to do the rest. If |
| 60 | we *usually* find the key we're looking for on the first try (and, it turns |
| 61 | out, we usually do -- the table load factor is kept under 2/3, so the odds |
| 62 | are solidly in our favor), then it makes best sense to keep the initial index |
| 63 | computation dirt cheap. |
Tim Peters | 15d4929 | 2001-05-27 07:39:22 +0000 | [diff] [blame] | 64 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 65 | The first half of collision resolution is to visit table indices via this |
| 66 | recurrence: |
Tim Peters | 15d4929 | 2001-05-27 07:39:22 +0000 | [diff] [blame] | 67 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 68 | j = ((5*j) + 1) mod 2**i |
Tim Peters | 15d4929 | 2001-05-27 07:39:22 +0000 | [diff] [blame] | 69 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 70 | For any initial j in range(2**i), repeating that 2**i times generates each |
| 71 | int in range(2**i) exactly once (see any text on random-number generation for |
| 72 | proof). By itself, this doesn't help much: like linear probing (setting |
| 73 | j += 1, or j -= 1, on each loop trip), it scans the table entries in a fixed |
| 74 | order. This would be bad, except that's not the only thing we do, and it's |
| 75 | actually *good* in the common cases where hash keys are consecutive. In an |
| 76 | example that's really too small to make this entirely clear, for a table of |
| 77 | size 2**3 the order of indices is: |
Tim Peters | 15d4929 | 2001-05-27 07:39:22 +0000 | [diff] [blame] | 78 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 79 | 0 -> 1 -> 6 -> 7 -> 4 -> 5 -> 2 -> 3 -> 0 [and here it's repeating] |
| 80 | |
| 81 | If two things come in at index 5, the first place we look after is index 2, |
| 82 | not 6, so if another comes in at index 6 the collision at 5 didn't hurt it. |
| 83 | Linear probing is deadly in this case because there the fixed probe order |
| 84 | is the *same* as the order consecutive keys are likely to arrive. But it's |
| 85 | extremely unlikely hash codes will follow a 5*j+1 recurrence by accident, |
| 86 | and certain that consecutive hash codes do not. |
| 87 | |
| 88 | The other half of the strategy is to get the other bits of the hash code |
| 89 | into play. This is done by initializing a (unsigned) vrbl "perturb" to the |
| 90 | full hash code, and changing the recurrence to: |
| 91 | |
| 92 | j = (5*j) + 1 + perturb; |
| 93 | perturb >>= PERTURB_SHIFT; |
| 94 | use j % 2**i as the next table index; |
| 95 | |
| 96 | Now the probe sequence depends (eventually) on every bit in the hash code, |
| 97 | and the pseudo-scrambling property of recurring on 5*j+1 is more valuable, |
| 98 | because it quickly magnifies small differences in the bits that didn't affect |
| 99 | the initial index. Note that because perturb is unsigned, if the recurrence |
| 100 | is executed often enough perturb eventually becomes and remains 0. At that |
| 101 | point (very rarely reached) the recurrence is on (just) 5*j+1 again, and |
| 102 | that's certain to find an empty slot eventually (since it generates every int |
| 103 | in range(2**i), and we make sure there's always at least one empty slot). |
| 104 | |
| 105 | Selecting a good value for PERTURB_SHIFT is a balancing act. You want it |
| 106 | small so that the high bits of the hash code continue to affect the probe |
| 107 | sequence across iterations; but you want it large so that in really bad cases |
| 108 | the high-order hash bits have an effect on early iterations. 5 was "the |
| 109 | best" in minimizing total collisions across experiments Tim Peters ran (on |
| 110 | both normal and pathological cases), but 4 and 6 weren't significantly worse. |
| 111 | |
| 112 | Historical: Reimer Behrends contributed the idea of using a polynomial-based |
| 113 | approach, using repeated multiplication by x in GF(2**n) where an irreducible |
| 114 | polynomial for each table size was chosen such that x was a primitive root. |
| 115 | Christian Tismer later extended that to use division by x instead, as an |
| 116 | efficient way to get the high bits of the hash code into play. This scheme |
| 117 | also gave excellent collision statistics, but was more expensive: two |
| 118 | if-tests were required inside the loop; computing "the next" index took about |
| 119 | the same number of operations but without as much potential parallelism |
| 120 | (e.g., computing 5*j can go on at the same time as computing 1+perturb in the |
| 121 | above, and then shifting perturb can be done while the table index is being |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 122 | masked); and the PyDictObject struct required a member to hold the table's |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 123 | polynomial. In Tim's experiments the current scheme ran faster, produced |
| 124 | equally good collision statistics, needed less code & used less memory. |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 125 | |
| 126 | Theoretical Python 2.5 headache: hash codes are only C "long", but |
| 127 | sizeof(Py_ssize_t) > sizeof(long) may be possible. In that case, and if a |
| 128 | dict is genuinely huge, then only the slots directly reachable via indexing |
| 129 | by a C long can be the first slot in a probe sequence. The probe sequence |
| 130 | will still eventually reach every slot in the table, but the collision rate |
| 131 | on initial probes may be much higher than this scheme was designed for. |
| 132 | Getting a hash code as fat as Py_ssize_t is the only real cure. But in |
| 133 | practice, this probably won't make a lick of difference for many years (at |
| 134 | which point everyone will have terabytes of RAM on 64-bit boxes). |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 135 | */ |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 136 | |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 137 | /* Object used as dummy key to fill deleted entries */ |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 138 | static PyObject *dummy = NULL; /* Initialized by first call to newPyDictObject() */ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 139 | |
Armin Rigo | e170937 | 2006-04-12 17:06:05 +0000 | [diff] [blame] | 140 | #ifdef Py_REF_DEBUG |
| 141 | PyObject * |
| 142 | _PyDict_Dummy(void) |
| 143 | { |
| 144 | return dummy; |
| 145 | } |
| 146 | #endif |
| 147 | |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 148 | /* forward declarations */ |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 149 | static PyDictEntry * |
| 150 | lookdict_string(PyDictObject *mp, PyObject *key, long hash); |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 151 | |
| 152 | #ifdef SHOW_CONVERSION_COUNTS |
| 153 | static long created = 0L; |
| 154 | static long converted = 0L; |
| 155 | |
| 156 | static void |
| 157 | show_counts(void) |
| 158 | { |
| 159 | fprintf(stderr, "created %ld string dicts\n", created); |
| 160 | fprintf(stderr, "converted %ld to normal dicts\n", converted); |
| 161 | fprintf(stderr, "%.2f%% conversion rate\n", (100.0*converted)/created); |
| 162 | } |
| 163 | #endif |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 164 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 165 | /* Initialization macros. |
| 166 | There are two ways to create a dict: PyDict_New() is the main C API |
| 167 | function, and the tp_new slot maps to dict_new(). In the latter case we |
| 168 | can save a little time over what PyDict_New does because it's guaranteed |
| 169 | that the PyDictObject struct is already zeroed out. |
| 170 | Everyone except dict_new() should use EMPTY_TO_MINSIZE (unless they have |
| 171 | an excellent reason not to). |
| 172 | */ |
| 173 | |
| 174 | #define INIT_NONZERO_DICT_SLOTS(mp) do { \ |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 175 | (mp)->ma_table = (mp)->ma_smalltable; \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 176 | (mp)->ma_mask = PyDict_MINSIZE - 1; \ |
| 177 | } while(0) |
| 178 | |
| 179 | #define EMPTY_TO_MINSIZE(mp) do { \ |
| 180 | memset((mp)->ma_smalltable, 0, sizeof((mp)->ma_smalltable)); \ |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 181 | (mp)->ma_used = (mp)->ma_fill = 0; \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 182 | INIT_NONZERO_DICT_SLOTS(mp); \ |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 183 | } while(0) |
| 184 | |
Raymond Hettinger | 4344278 | 2004-03-17 21:55:03 +0000 | [diff] [blame] | 185 | /* Dictionary reuse scheme to save calls to malloc, free, and memset */ |
| 186 | #define MAXFREEDICTS 80 |
| 187 | static PyDictObject *free_dicts[MAXFREEDICTS]; |
| 188 | static int num_free_dicts = 0; |
| 189 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 190 | PyObject * |
Thomas Wouters | 7889010 | 2000-07-22 19:25:51 +0000 | [diff] [blame] | 191 | PyDict_New(void) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 192 | { |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 193 | register PyDictObject *mp; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 194 | if (dummy == NULL) { /* Auto-initialize dummy */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 195 | dummy = PyString_FromString("<dummy key>"); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 196 | if (dummy == NULL) |
| 197 | return NULL; |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 198 | #ifdef SHOW_CONVERSION_COUNTS |
| 199 | Py_AtExit(show_counts); |
| 200 | #endif |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 201 | } |
Raymond Hettinger | 4344278 | 2004-03-17 21:55:03 +0000 | [diff] [blame] | 202 | if (num_free_dicts) { |
| 203 | mp = free_dicts[--num_free_dicts]; |
| 204 | assert (mp != NULL); |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 205 | assert (Py_Type(mp) == &PyDict_Type); |
Raymond Hettinger | 4344278 | 2004-03-17 21:55:03 +0000 | [diff] [blame] | 206 | _Py_NewReference((PyObject *)mp); |
| 207 | if (mp->ma_fill) { |
| 208 | EMPTY_TO_MINSIZE(mp); |
| 209 | } |
| 210 | assert (mp->ma_used == 0); |
| 211 | assert (mp->ma_table == mp->ma_smalltable); |
| 212 | assert (mp->ma_mask == PyDict_MINSIZE - 1); |
| 213 | } else { |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 214 | mp = PyObject_GC_New(PyDictObject, &PyDict_Type); |
Raymond Hettinger | 4344278 | 2004-03-17 21:55:03 +0000 | [diff] [blame] | 215 | if (mp == NULL) |
| 216 | return NULL; |
| 217 | EMPTY_TO_MINSIZE(mp); |
| 218 | } |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 219 | mp->ma_lookup = lookdict_string; |
| 220 | #ifdef SHOW_CONVERSION_COUNTS |
| 221 | ++created; |
| 222 | #endif |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 223 | _PyObject_GC_TRACK(mp); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 224 | return (PyObject *)mp; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | /* |
| 228 | The basic lookup function used by all operations. |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 229 | 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] | 230 | Open addressing is preferred over chaining since the link overhead for |
| 231 | chaining would be substantial (100% with typical malloc overhead). |
| 232 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 233 | The initial probe index is computed as hash mod the table size. Subsequent |
| 234 | probe indices are computed as explained earlier. |
Guido van Rossum | 2bc1379 | 1999-03-24 19:06:42 +0000 | [diff] [blame] | 235 | |
| 236 | All arithmetic on hash should ignore overflow. |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 237 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 238 | (The details in this version are due to Tim Peters, building on many past |
| 239 | contributions by Reimer Behrends, Jyrki Alakuijala, Vladimir Marangozov and |
| 240 | Christian Tismer). |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 241 | |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 242 | lookdict() is general-purpose, and may return NULL if (and only if) a |
| 243 | comparison raises an exception (this was new in Python 2.5). |
| 244 | lookdict_string() below is specialized to string keys, comparison of which can |
| 245 | never raise an exception; that function can never return NULL. For both, when |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 246 | the key isn't found a PyDictEntry* is returned for which the me_value field is |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 247 | NULL; this is the slot in the dict at which the key would have been found, and |
| 248 | the caller can (if it wishes) add the <key, value> pair to the returned |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 249 | PyDictEntry*. |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 250 | */ |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 251 | static PyDictEntry * |
| 252 | lookdict(PyDictObject *mp, PyObject *key, register long hash) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 253 | { |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 254 | register size_t i; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 255 | register size_t perturb; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 256 | register PyDictEntry *freeslot; |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 257 | register size_t mask = (size_t)mp->ma_mask; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 258 | PyDictEntry *ep0 = mp->ma_table; |
| 259 | register PyDictEntry *ep; |
Fred Drake | c88b99c | 2000-08-31 19:04:07 +0000 | [diff] [blame] | 260 | register int cmp; |
Tim Peters | 453163d | 2001-06-03 04:54:32 +0000 | [diff] [blame] | 261 | PyObject *startkey; |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 262 | |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 263 | i = (size_t)hash & mask; |
Guido van Rossum | efb4609 | 1997-01-29 15:53:56 +0000 | [diff] [blame] | 264 | ep = &ep0[i]; |
Guido van Rossum | c1c7b1a | 1998-10-06 16:01:14 +0000 | [diff] [blame] | 265 | if (ep->me_key == NULL || ep->me_key == key) |
Guido van Rossum | 7d18159 | 1997-01-16 21:06:45 +0000 | [diff] [blame] | 266 | return ep; |
Tim Peters | 7b5d0af | 2001-06-03 04:14:43 +0000 | [diff] [blame] | 267 | |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 268 | if (ep->me_key == dummy) |
Guido van Rossum | 7d18159 | 1997-01-16 21:06:45 +0000 | [diff] [blame] | 269 | freeslot = ep; |
Guido van Rossum | fd7a0b8 | 1997-08-18 21:52:47 +0000 | [diff] [blame] | 270 | else { |
Fred Drake | c88b99c | 2000-08-31 19:04:07 +0000 | [diff] [blame] | 271 | if (ep->me_hash == hash) { |
Tim Peters | 453163d | 2001-06-03 04:54:32 +0000 | [diff] [blame] | 272 | startkey = ep->me_key; |
| 273 | cmp = PyObject_RichCompareBool(startkey, key, Py_EQ); |
Tim Peters | 7b5d0af | 2001-06-03 04:14:43 +0000 | [diff] [blame] | 274 | if (cmp < 0) |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 275 | return NULL; |
Tim Peters | 453163d | 2001-06-03 04:54:32 +0000 | [diff] [blame] | 276 | if (ep0 == mp->ma_table && ep->me_key == startkey) { |
| 277 | if (cmp > 0) |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 278 | return ep; |
Tim Peters | 453163d | 2001-06-03 04:54:32 +0000 | [diff] [blame] | 279 | } |
| 280 | else { |
| 281 | /* The compare did major nasty stuff to the |
| 282 | * dict: start over. |
| 283 | * XXX A clever adversary could prevent this |
| 284 | * XXX from terminating. |
| 285 | */ |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 286 | return lookdict(mp, key, hash); |
Tim Peters | 453163d | 2001-06-03 04:54:32 +0000 | [diff] [blame] | 287 | } |
Guido van Rossum | fd7a0b8 | 1997-08-18 21:52:47 +0000 | [diff] [blame] | 288 | } |
| 289 | freeslot = NULL; |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 290 | } |
Tim Peters | 15d4929 | 2001-05-27 07:39:22 +0000 | [diff] [blame] | 291 | |
Tim Peters | 342c65e | 2001-05-13 06:43:53 +0000 | [diff] [blame] | 292 | /* In the loop, me_key == dummy is by far (factor of 100s) the |
| 293 | least likely outcome, so test for that last. */ |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 294 | for (perturb = hash; ; perturb >>= PERTURB_SHIFT) { |
| 295 | i = (i << 2) + i + perturb + 1; |
| 296 | ep = &ep0[i & mask]; |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 297 | if (ep->me_key == NULL) |
| 298 | return freeslot == NULL ? ep : freeslot; |
Tim Peters | 7b5d0af | 2001-06-03 04:14:43 +0000 | [diff] [blame] | 299 | if (ep->me_key == key) |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 300 | return ep; |
Tim Peters | 7b5d0af | 2001-06-03 04:14:43 +0000 | [diff] [blame] | 301 | if (ep->me_hash == hash && ep->me_key != dummy) { |
Tim Peters | 453163d | 2001-06-03 04:54:32 +0000 | [diff] [blame] | 302 | startkey = ep->me_key; |
| 303 | cmp = PyObject_RichCompareBool(startkey, key, Py_EQ); |
Tim Peters | 7b5d0af | 2001-06-03 04:14:43 +0000 | [diff] [blame] | 304 | if (cmp < 0) |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 305 | return NULL; |
Tim Peters | 453163d | 2001-06-03 04:54:32 +0000 | [diff] [blame] | 306 | if (ep0 == mp->ma_table && ep->me_key == startkey) { |
| 307 | if (cmp > 0) |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 308 | return ep; |
Tim Peters | 453163d | 2001-06-03 04:54:32 +0000 | [diff] [blame] | 309 | } |
| 310 | else { |
| 311 | /* The compare did major nasty stuff to the |
| 312 | * dict: start over. |
| 313 | * XXX A clever adversary could prevent this |
| 314 | * XXX from terminating. |
| 315 | */ |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 316 | return lookdict(mp, key, hash); |
Tim Peters | 453163d | 2001-06-03 04:54:32 +0000 | [diff] [blame] | 317 | } |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 318 | } |
Tim Peters | 342c65e | 2001-05-13 06:43:53 +0000 | [diff] [blame] | 319 | else if (ep->me_key == dummy && freeslot == NULL) |
| 320 | freeslot = ep; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 321 | } |
Neal Norwitz | a5ccda9 | 2006-10-28 21:16:54 +0000 | [diff] [blame] | 322 | assert(0); /* NOT REACHED */ |
| 323 | return 0; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | /* |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 327 | * Hacked up version of lookdict which can assume keys are always strings; |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 328 | * this assumption allows testing for errors during PyObject_RichCompareBool() |
| 329 | * to be dropped; string-string comparisons never raise exceptions. This also |
| 330 | * means we don't need to go through PyObject_RichCompareBool(); we can always |
| 331 | * use _PyString_Eq() directly. |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 332 | * |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 333 | * This is valuable because dicts with only string keys are very common. |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 334 | */ |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 335 | static PyDictEntry * |
| 336 | lookdict_string(PyDictObject *mp, PyObject *key, register long hash) |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 337 | { |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 338 | register size_t i; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 339 | register size_t perturb; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 340 | register PyDictEntry *freeslot; |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 341 | register size_t mask = (size_t)mp->ma_mask; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 342 | PyDictEntry *ep0 = mp->ma_table; |
| 343 | register PyDictEntry *ep; |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 344 | |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 345 | /* Make sure this function doesn't have to handle non-string keys, |
| 346 | including subclasses of str; e.g., one reason to subclass |
| 347 | strings is to override __eq__, and for speed we don't cater to |
| 348 | that here. */ |
| 349 | if (!PyString_CheckExact(key)) { |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 350 | #ifdef SHOW_CONVERSION_COUNTS |
| 351 | ++converted; |
| 352 | #endif |
| 353 | mp->ma_lookup = lookdict; |
| 354 | return lookdict(mp, key, hash); |
| 355 | } |
Tim Peters | 2f228e7 | 2001-05-13 00:19:31 +0000 | [diff] [blame] | 356 | i = hash & mask; |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 357 | ep = &ep0[i]; |
| 358 | if (ep->me_key == NULL || ep->me_key == key) |
| 359 | return ep; |
| 360 | if (ep->me_key == dummy) |
| 361 | freeslot = ep; |
| 362 | else { |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 363 | if (ep->me_hash == hash && _PyString_Eq(ep->me_key, key)) |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 364 | return ep; |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 365 | freeslot = NULL; |
| 366 | } |
Tim Peters | 15d4929 | 2001-05-27 07:39:22 +0000 | [diff] [blame] | 367 | |
Tim Peters | 342c65e | 2001-05-13 06:43:53 +0000 | [diff] [blame] | 368 | /* In the loop, me_key == dummy is by far (factor of 100s) the |
| 369 | least likely outcome, so test for that last. */ |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 370 | for (perturb = hash; ; perturb >>= PERTURB_SHIFT) { |
| 371 | i = (i << 2) + i + perturb + 1; |
| 372 | ep = &ep0[i & mask]; |
Tim Peters | 342c65e | 2001-05-13 06:43:53 +0000 | [diff] [blame] | 373 | if (ep->me_key == NULL) |
| 374 | return freeslot == NULL ? ep : freeslot; |
| 375 | if (ep->me_key == key |
| 376 | || (ep->me_hash == hash |
| 377 | && ep->me_key != dummy |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 378 | && _PyString_Eq(ep->me_key, key))) |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 379 | return ep; |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 380 | if (ep->me_key == dummy && freeslot == NULL) |
Tim Peters | 342c65e | 2001-05-13 06:43:53 +0000 | [diff] [blame] | 381 | freeslot = ep; |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 382 | } |
Neal Norwitz | a5ccda9 | 2006-10-28 21:16:54 +0000 | [diff] [blame] | 383 | assert(0); /* NOT REACHED */ |
| 384 | return 0; |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | /* |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 388 | Internal routine to insert a new item into the table. |
| 389 | Used both by the internal resize routine and by the public insert routine. |
| 390 | Eats a reference to key and one to value. |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 391 | Returns -1 if an error occurred, or 0 on success. |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 392 | */ |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 393 | static int |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 394 | insertdict(register PyDictObject *mp, PyObject *key, long hash, PyObject *value) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 395 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 396 | PyObject *old_value; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 397 | register PyDictEntry *ep; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 398 | typedef PyDictEntry *(*lookupfunc)(PyDictObject *, PyObject *, long); |
| 399 | |
| 400 | assert(mp->ma_lookup != NULL); |
| 401 | ep = mp->ma_lookup(mp, key, hash); |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 402 | if (ep == NULL) { |
| 403 | Py_DECREF(key); |
| 404 | Py_DECREF(value); |
| 405 | return -1; |
| 406 | } |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 407 | if (ep->me_value != NULL) { |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 408 | old_value = ep->me_value; |
| 409 | ep->me_value = value; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 410 | Py_DECREF(old_value); /* which **CAN** re-enter */ |
| 411 | Py_DECREF(key); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 412 | } |
| 413 | else { |
| 414 | if (ep->me_key == NULL) |
| 415 | mp->ma_fill++; |
Raymond Hettinger | 07ead17 | 2005-02-05 23:42:57 +0000 | [diff] [blame] | 416 | else { |
| 417 | assert(ep->me_key == dummy); |
| 418 | Py_DECREF(dummy); |
| 419 | } |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 420 | ep->me_key = key; |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 421 | ep->me_hash = (Py_ssize_t)hash; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 422 | ep->me_value = value; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 423 | mp->ma_used++; |
| 424 | } |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | /* |
| 429 | Internal routine used by dictresize() to insert an item which is |
| 430 | known to be absent from the dict. This routine also assumes that |
| 431 | the dict contains no deleted entries. Besides the performance benefit, |
| 432 | using insertdict() in dictresize() is dangerous (SF bug #1456209). |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 433 | Note that no refcounts are changed by this routine; if needed, the caller |
| 434 | is responsible for incref'ing `key` and `value`. |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 435 | */ |
| 436 | static void |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 437 | insertdict_clean(register PyDictObject *mp, PyObject *key, long hash, |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 438 | PyObject *value) |
| 439 | { |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 440 | register size_t i; |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 441 | register size_t perturb; |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 442 | register size_t mask = (size_t)mp->ma_mask; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 443 | PyDictEntry *ep0 = mp->ma_table; |
| 444 | register PyDictEntry *ep; |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 445 | |
| 446 | i = hash & mask; |
| 447 | ep = &ep0[i]; |
| 448 | for (perturb = hash; ep->me_key != NULL; perturb >>= PERTURB_SHIFT) { |
| 449 | i = (i << 2) + i + perturb + 1; |
| 450 | ep = &ep0[i & mask]; |
| 451 | } |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 452 | assert(ep->me_value == NULL); |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 453 | mp->ma_fill++; |
| 454 | ep->me_key = key; |
| 455 | ep->me_hash = (Py_ssize_t)hash; |
| 456 | ep->me_value = value; |
| 457 | mp->ma_used++; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | /* |
| 461 | Restructure the table by allocating a new table and reinserting all |
| 462 | items again. When entries have been deleted, the new table may |
| 463 | actually be smaller than the old one. |
| 464 | */ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 465 | static int |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 466 | dictresize(PyDictObject *mp, Py_ssize_t minused) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 467 | { |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 468 | Py_ssize_t newsize; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 469 | PyDictEntry *oldtable, *newtable, *ep; |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 470 | Py_ssize_t i; |
Tim Peters | 0c6010b | 2001-05-23 23:33:57 +0000 | [diff] [blame] | 471 | int is_oldtable_malloced; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 472 | PyDictEntry small_copy[PyDict_MINSIZE]; |
Tim Peters | 91a364d | 2001-05-19 07:04:38 +0000 | [diff] [blame] | 473 | |
| 474 | assert(minused >= 0); |
Tim Peters | 0c6010b | 2001-05-23 23:33:57 +0000 | [diff] [blame] | 475 | |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 476 | /* Find the smallest table size > minused. */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 477 | for (newsize = PyDict_MINSIZE; |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 478 | newsize <= minused && newsize > 0; |
| 479 | newsize <<= 1) |
| 480 | ; |
| 481 | if (newsize <= 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 482 | PyErr_NoMemory(); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 483 | return -1; |
| 484 | } |
Tim Peters | 0c6010b | 2001-05-23 23:33:57 +0000 | [diff] [blame] | 485 | |
| 486 | /* Get space for a new table. */ |
| 487 | oldtable = mp->ma_table; |
| 488 | assert(oldtable != NULL); |
| 489 | is_oldtable_malloced = oldtable != mp->ma_smalltable; |
| 490 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 491 | if (newsize == PyDict_MINSIZE) { |
Tim Peters | f8a548c | 2001-05-24 16:26:40 +0000 | [diff] [blame] | 492 | /* A large table is shrinking, or we can't get any smaller. */ |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 493 | newtable = mp->ma_smalltable; |
Tim Peters | 0c6010b | 2001-05-23 23:33:57 +0000 | [diff] [blame] | 494 | if (newtable == oldtable) { |
Tim Peters | f8a548c | 2001-05-24 16:26:40 +0000 | [diff] [blame] | 495 | if (mp->ma_fill == mp->ma_used) { |
| 496 | /* No dummies, so no point doing anything. */ |
Tim Peters | 0c6010b | 2001-05-23 23:33:57 +0000 | [diff] [blame] | 497 | return 0; |
Tim Peters | f8a548c | 2001-05-24 16:26:40 +0000 | [diff] [blame] | 498 | } |
| 499 | /* We're not going to resize it, but rebuild the |
| 500 | table anyway to purge old dummy entries. |
| 501 | Subtle: This is *necessary* if fill==size, |
| 502 | as lookdict needs at least one virgin slot to |
| 503 | terminate failing searches. If fill < size, it's |
| 504 | merely desirable, as dummies slow searches. */ |
| 505 | assert(mp->ma_fill > mp->ma_used); |
Tim Peters | 0c6010b | 2001-05-23 23:33:57 +0000 | [diff] [blame] | 506 | memcpy(small_copy, oldtable, sizeof(small_copy)); |
| 507 | oldtable = small_copy; |
| 508 | } |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 509 | } |
| 510 | else { |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 511 | newtable = PyMem_NEW(PyDictEntry, newsize); |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 512 | if (newtable == NULL) { |
| 513 | PyErr_NoMemory(); |
| 514 | return -1; |
| 515 | } |
| 516 | } |
Tim Peters | 0c6010b | 2001-05-23 23:33:57 +0000 | [diff] [blame] | 517 | |
| 518 | /* Make the dict empty, using the new table. */ |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 519 | assert(newtable != oldtable); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 520 | mp->ma_table = newtable; |
Tim Peters | afb6ae8 | 2001-06-04 21:00:21 +0000 | [diff] [blame] | 521 | mp->ma_mask = newsize - 1; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 522 | memset(newtable, 0, sizeof(PyDictEntry) * newsize); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 523 | mp->ma_used = 0; |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 524 | i = mp->ma_fill; |
| 525 | mp->ma_fill = 0; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 526 | |
Tim Peters | 1928314 | 2001-05-17 22:25:34 +0000 | [diff] [blame] | 527 | /* Copy the data over; this is refcount-neutral for active entries; |
| 528 | dummy entries aren't copied over, of course */ |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 529 | for (ep = oldtable; i > 0; ep++) { |
| 530 | if (ep->me_value != NULL) { /* active entry */ |
| 531 | --i; |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 532 | insertdict_clean(mp, ep->me_key, (long)ep->me_hash, |
| 533 | ep->me_value); |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 534 | } |
Tim Peters | 1928314 | 2001-05-17 22:25:34 +0000 | [diff] [blame] | 535 | else if (ep->me_key != NULL) { /* dummy entry */ |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 536 | --i; |
Tim Peters | 1928314 | 2001-05-17 22:25:34 +0000 | [diff] [blame] | 537 | assert(ep->me_key == dummy); |
| 538 | Py_DECREF(ep->me_key); |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 539 | } |
Tim Peters | 1928314 | 2001-05-17 22:25:34 +0000 | [diff] [blame] | 540 | /* else key == value == NULL: nothing to do */ |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 541 | } |
| 542 | |
Tim Peters | 0c6010b | 2001-05-23 23:33:57 +0000 | [diff] [blame] | 543 | if (is_oldtable_malloced) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 544 | PyMem_DEL(oldtable); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 545 | return 0; |
| 546 | } |
| 547 | |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 548 | /* Note that, for historical reasons, PyDict_GetItem() suppresses all errors |
| 549 | * that may occur (originally dicts supported only string keys, and exceptions |
| 550 | * weren't possible). So, while the original intent was that a NULL return |
Andrew M. Kuchling | 0067b5f | 2006-08-04 20:37:43 +0000 | [diff] [blame] | 551 | * meant the key wasn't present, in reality it can mean that, or that an error |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 552 | * (suppressed) occurred while computing the key's hash, or that some error |
| 553 | * (suppressed) occurred when comparing keys in the dict's internal probe |
| 554 | * sequence. A nasty example of the latter is when a Python-coded comparison |
| 555 | * function hits a stack-depth error, which can cause this to return NULL |
| 556 | * even if the key is present. |
| 557 | */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 558 | PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 559 | PyDict_GetItem(PyObject *op, PyObject *key) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 560 | { |
| 561 | long hash; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 562 | PyDictObject *mp = (PyDictObject *)op; |
| 563 | PyDictEntry *ep; |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 564 | PyThreadState *tstate; |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 565 | if (!PyDict_Check(op)) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 566 | return NULL; |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 567 | if (!PyString_CheckExact(key) || |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 568 | (hash = ((PyStringObject *) key)->ob_shash) == -1) |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 569 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 570 | hash = PyObject_Hash(key); |
Guido van Rossum | 474b19e | 1998-05-14 01:00:51 +0000 | [diff] [blame] | 571 | if (hash == -1) { |
| 572 | PyErr_Clear(); |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 573 | return NULL; |
Guido van Rossum | 474b19e | 1998-05-14 01:00:51 +0000 | [diff] [blame] | 574 | } |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 575 | } |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 576 | |
| 577 | /* We can arrive here with a NULL tstate during initialization: |
| 578 | try running "python -Wi" for an example related to string |
| 579 | interning. Let's just hope that no exception occurs then... */ |
Armin Rigo | acd0d6d | 2006-06-10 10:57:40 +0000 | [diff] [blame] | 580 | tstate = _PyThreadState_Current; |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 581 | if (tstate != NULL && tstate->curexc_type != NULL) { |
| 582 | /* preserve the existing exception */ |
| 583 | PyObject *err_type, *err_value, *err_tb; |
| 584 | PyErr_Fetch(&err_type, &err_value, &err_tb); |
| 585 | ep = (mp->ma_lookup)(mp, key, hash); |
| 586 | /* ignore errors */ |
| 587 | PyErr_Restore(err_type, err_value, err_tb); |
| 588 | if (ep == NULL) |
| 589 | return NULL; |
| 590 | } |
| 591 | else { |
| 592 | ep = (mp->ma_lookup)(mp, key, hash); |
| 593 | if (ep == NULL) { |
| 594 | PyErr_Clear(); |
| 595 | return NULL; |
| 596 | } |
| 597 | } |
| 598 | return ep->me_value; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 599 | } |
| 600 | |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 601 | /* CAUTION: PyDict_SetItem() must guarantee that it won't resize the |
Tim Peters | 60b2996 | 2006-01-01 01:19:23 +0000 | [diff] [blame] | 602 | * dictionary if it's merely replacing the value for an existing key. |
| 603 | * This means that it's safe to loop over a dictionary with PyDict_Next() |
| 604 | * and occasionally replace a value -- but you can't insert new keys or |
| 605 | * remove them. |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 606 | */ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 607 | int |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 608 | PyDict_SetItem(register PyObject *op, PyObject *key, PyObject *value) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 609 | { |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 610 | register PyDictObject *mp; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 611 | register long hash; |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 612 | register Py_ssize_t n_used; |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 613 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 614 | if (!PyDict_Check(op)) { |
| 615 | PyErr_BadInternalCall(); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 616 | return -1; |
| 617 | } |
Neal Norwitz | 48808a1 | 2006-07-21 05:29:58 +0000 | [diff] [blame] | 618 | assert(key); |
| 619 | assert(value); |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 620 | mp = (PyDictObject *)op; |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 621 | if (PyString_CheckExact(key)) { |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 622 | hash = ((PyStringObject *)key)->ob_shash; |
| 623 | if (hash == -1) |
| 624 | hash = PyObject_Hash(key); |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 625 | } |
Tim Peters | 1f7df35 | 2002-03-29 03:29:08 +0000 | [diff] [blame] | 626 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 627 | hash = PyObject_Hash(key); |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 628 | if (hash == -1) |
| 629 | return -1; |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 630 | } |
Tim Peters | afb6ae8 | 2001-06-04 21:00:21 +0000 | [diff] [blame] | 631 | 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] | 632 | n_used = mp->ma_used; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 633 | Py_INCREF(value); |
| 634 | Py_INCREF(key); |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 635 | if (insertdict(mp, key, hash, value) != 0) |
| 636 | return -1; |
Raymond Hettinger | 3539f6b | 2003-05-05 22:22:10 +0000 | [diff] [blame] | 637 | /* If we added a key, we can safely resize. Otherwise just return! |
| 638 | * If fill >= 2/3 size, adjust size. Normally, this doubles or |
| 639 | * 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] | 640 | * (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] | 641 | * keys have been * deleted). |
Tim Peters | 60b2996 | 2006-01-01 01:19:23 +0000 | [diff] [blame] | 642 | * |
Raymond Hettinger | 3539f6b | 2003-05-05 22:22:10 +0000 | [diff] [blame] | 643 | * Quadrupling the size improves average dictionary sparseness |
| 644 | * (reducing collisions) at the cost of some memory and iteration |
| 645 | * speed (which loops over every possible entry). It also halves |
Neal Norwitz | 80af59c | 2006-05-30 04:19:21 +0000 | [diff] [blame] | 646 | * the number of expensive resize operations in a growing dictionary. |
Tim Peters | 60b2996 | 2006-01-01 01:19:23 +0000 | [diff] [blame] | 647 | * |
| 648 | * Very large dictionaries (over 50K items) use doubling instead. |
Raymond Hettinger | 3539f6b | 2003-05-05 22:22:10 +0000 | [diff] [blame] | 649 | * This may help applications with severe memory constraints. |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 650 | */ |
Raymond Hettinger | 3539f6b | 2003-05-05 22:22:10 +0000 | [diff] [blame] | 651 | if (!(mp->ma_used > n_used && mp->ma_fill*3 >= (mp->ma_mask+1)*2)) |
| 652 | return 0; |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 653 | 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] | 654 | } |
| 655 | |
| 656 | int |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 657 | PyDict_DelItem(PyObject *op, PyObject *key) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 658 | { |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 659 | register PyDictObject *mp; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 660 | register long hash; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 661 | register PyDictEntry *ep; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 662 | PyObject *old_value, *old_key; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 663 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 664 | if (!PyDict_Check(op)) { |
| 665 | PyErr_BadInternalCall(); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 666 | return -1; |
| 667 | } |
Neal Norwitz | 48808a1 | 2006-07-21 05:29:58 +0000 | [diff] [blame] | 668 | assert(key); |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 669 | if (!PyString_CheckExact(key) || |
Tim Peters | 1f7df35 | 2002-03-29 03:29:08 +0000 | [diff] [blame] | 670 | (hash = ((PyStringObject *) key)->ob_shash) == -1) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 671 | hash = PyObject_Hash(key); |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 672 | if (hash == -1) |
| 673 | return -1; |
| 674 | } |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 675 | mp = (PyDictObject *)op; |
Fred Drake | 1bff34a | 2000-08-31 19:31:38 +0000 | [diff] [blame] | 676 | ep = (mp->ma_lookup)(mp, key, hash); |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 677 | if (ep == NULL) |
| 678 | return -1; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 679 | if (ep->me_value == NULL) { |
Georg Brandl | b9f4ad3 | 2006-10-29 18:31:42 +0000 | [diff] [blame] | 680 | set_key_error(key); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 681 | return -1; |
| 682 | } |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 683 | old_key = ep->me_key; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 684 | Py_INCREF(dummy); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 685 | ep->me_key = dummy; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 686 | old_value = ep->me_value; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 687 | ep->me_value = NULL; |
| 688 | mp->ma_used--; |
Tim Peters | ea8f2bf | 2000-12-13 01:02:46 +0000 | [diff] [blame] | 689 | Py_DECREF(old_value); |
| 690 | Py_DECREF(old_key); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 691 | return 0; |
| 692 | } |
| 693 | |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 694 | void |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 695 | PyDict_Clear(PyObject *op) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 696 | { |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 697 | PyDictObject *mp; |
| 698 | PyDictEntry *ep, *table; |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 699 | int table_is_malloced; |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 700 | Py_ssize_t fill; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 701 | PyDictEntry small_copy[PyDict_MINSIZE]; |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 702 | #ifdef Py_DEBUG |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 703 | Py_ssize_t i, n; |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 704 | #endif |
| 705 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 706 | if (!PyDict_Check(op)) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 707 | return; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 708 | mp = (PyDictObject *)op; |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 709 | #ifdef Py_DEBUG |
Tim Peters | afb6ae8 | 2001-06-04 21:00:21 +0000 | [diff] [blame] | 710 | n = mp->ma_mask + 1; |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 711 | i = 0; |
| 712 | #endif |
| 713 | |
| 714 | table = mp->ma_table; |
| 715 | assert(table != NULL); |
| 716 | table_is_malloced = table != mp->ma_smalltable; |
| 717 | |
| 718 | /* This is delicate. During the process of clearing the dict, |
| 719 | * decrefs can cause the dict to mutate. To avoid fatal confusion |
| 720 | * (voice of experience), we have to make the dict empty before |
| 721 | * clearing the slots, and never refer to anything via mp->xxx while |
| 722 | * clearing. |
| 723 | */ |
| 724 | fill = mp->ma_fill; |
| 725 | if (table_is_malloced) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 726 | EMPTY_TO_MINSIZE(mp); |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 727 | |
| 728 | else if (fill > 0) { |
| 729 | /* It's a small table with something that needs to be cleared. |
| 730 | * Afraid the only safe way is to copy the dict entries into |
| 731 | * another small table first. |
| 732 | */ |
| 733 | memcpy(small_copy, table, sizeof(small_copy)); |
| 734 | table = small_copy; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 735 | EMPTY_TO_MINSIZE(mp); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 736 | } |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 737 | /* else it's a small table that's already empty */ |
| 738 | |
| 739 | /* Now we can finally clear things. If C had refcounts, we could |
| 740 | * assert that the refcount on table is 1 now, i.e. that this function |
| 741 | * has unique access to it, so decref side-effects can't alter it. |
| 742 | */ |
| 743 | for (ep = table; fill > 0; ++ep) { |
| 744 | #ifdef Py_DEBUG |
| 745 | assert(i < n); |
| 746 | ++i; |
| 747 | #endif |
| 748 | if (ep->me_key) { |
| 749 | --fill; |
| 750 | Py_DECREF(ep->me_key); |
| 751 | Py_XDECREF(ep->me_value); |
| 752 | } |
| 753 | #ifdef Py_DEBUG |
| 754 | else |
| 755 | assert(ep->me_value == NULL); |
| 756 | #endif |
| 757 | } |
| 758 | |
| 759 | if (table_is_malloced) |
| 760 | PyMem_DEL(table); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 761 | } |
| 762 | |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 763 | /* |
| 764 | * Iterate over a dict. Use like so: |
| 765 | * |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 766 | * Py_ssize_t i; |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 767 | * PyObject *key, *value; |
| 768 | * i = 0; # important! i should not otherwise be changed by you |
Neal Norwitz | 0732301 | 2003-02-15 14:45:12 +0000 | [diff] [blame] | 769 | * while (PyDict_Next(yourdict, &i, &key, &value)) { |
Tim Peters | 080c88b | 2003-02-15 03:01:11 +0000 | [diff] [blame] | 770 | * Refer to borrowed references in key and value. |
| 771 | * } |
| 772 | * |
| 773 | * 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] | 774 | * mutates the dict. One exception: it is safe if the loop merely changes |
| 775 | * the values associated with the keys (but doesn't insert new keys or |
| 776 | * delete keys), via PyDict_SetItem(). |
| 777 | */ |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 778 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 779 | 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] | 780 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 781 | register Py_ssize_t i; |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 782 | register Py_ssize_t mask; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 783 | register PyDictEntry *ep; |
Raymond Hettinger | 4344278 | 2004-03-17 21:55:03 +0000 | [diff] [blame] | 784 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 785 | if (!PyDict_Check(op)) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 786 | return 0; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 787 | i = *ppos; |
| 788 | if (i < 0) |
| 789 | return 0; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 790 | ep = ((PyDictObject *)op)->ma_table; |
| 791 | mask = ((PyDictObject *)op)->ma_mask; |
Raymond Hettinger | 4344278 | 2004-03-17 21:55:03 +0000 | [diff] [blame] | 792 | while (i <= mask && ep[i].me_value == NULL) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 793 | i++; |
| 794 | *ppos = i+1; |
Raymond Hettinger | 4344278 | 2004-03-17 21:55:03 +0000 | [diff] [blame] | 795 | if (i > mask) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 796 | return 0; |
| 797 | if (pkey) |
Raymond Hettinger | 4344278 | 2004-03-17 21:55:03 +0000 | [diff] [blame] | 798 | *pkey = ep[i].me_key; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 799 | if (pvalue) |
Raymond Hettinger | 4344278 | 2004-03-17 21:55:03 +0000 | [diff] [blame] | 800 | *pvalue = ep[i].me_value; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 801 | return 1; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 802 | } |
| 803 | |
Raymond Hettinger | d6fc72a | 2007-02-19 02:03:19 +0000 | [diff] [blame] | 804 | /* Internal version of PyDict_Next that returns a hash value in addition to the key and value.*/ |
| 805 | int |
| 806 | _PyDict_Next(PyObject *op, Py_ssize_t *ppos, PyObject **pkey, PyObject **pvalue, long *phash) |
| 807 | { |
| 808 | register Py_ssize_t i; |
| 809 | register Py_ssize_t mask; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 810 | register PyDictEntry *ep; |
Raymond Hettinger | d6fc72a | 2007-02-19 02:03:19 +0000 | [diff] [blame] | 811 | |
| 812 | if (!PyDict_Check(op)) |
| 813 | return 0; |
| 814 | i = *ppos; |
| 815 | if (i < 0) |
| 816 | return 0; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 817 | ep = ((PyDictObject *)op)->ma_table; |
| 818 | mask = ((PyDictObject *)op)->ma_mask; |
Raymond Hettinger | d6fc72a | 2007-02-19 02:03:19 +0000 | [diff] [blame] | 819 | while (i <= mask && ep[i].me_value == NULL) |
| 820 | i++; |
| 821 | *ppos = i+1; |
| 822 | if (i > mask) |
| 823 | return 0; |
| 824 | *phash = (long)(ep[i].me_hash); |
| 825 | if (pkey) |
| 826 | *pkey = ep[i].me_key; |
| 827 | if (pvalue) |
| 828 | *pvalue = ep[i].me_value; |
| 829 | return 1; |
| 830 | } |
| 831 | |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 832 | /* Methods */ |
| 833 | |
| 834 | static void |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 835 | dict_dealloc(register PyDictObject *mp) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 836 | { |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 837 | register PyDictEntry *ep; |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 838 | Py_ssize_t fill = mp->ma_fill; |
Guido van Rossum | ff413af | 2002-03-28 20:34:59 +0000 | [diff] [blame] | 839 | PyObject_GC_UnTrack(mp); |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 840 | Py_TRASHCAN_SAFE_BEGIN(mp) |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 841 | for (ep = mp->ma_table; fill > 0; ep++) { |
| 842 | if (ep->me_key) { |
| 843 | --fill; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 844 | Py_DECREF(ep->me_key); |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 845 | Py_XDECREF(ep->me_value); |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 846 | } |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 847 | } |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 848 | if (mp->ma_table != mp->ma_smalltable) |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 849 | PyMem_DEL(mp->ma_table); |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 850 | if (num_free_dicts < MAXFREEDICTS && Py_Type(mp) == &PyDict_Type) |
Raymond Hettinger | 4344278 | 2004-03-17 21:55:03 +0000 | [diff] [blame] | 851 | free_dicts[num_free_dicts++] = mp; |
Tim Peters | 60b2996 | 2006-01-01 01:19:23 +0000 | [diff] [blame] | 852 | else |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 853 | Py_Type(mp)->tp_free((PyObject *)mp); |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 854 | Py_TRASHCAN_SAFE_END(mp) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | static int |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 858 | dict_print(register PyDictObject *mp, register FILE *fp, register int flags) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 859 | { |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 860 | register Py_ssize_t i; |
| 861 | register Py_ssize_t any; |
Tim Peters | 33f4a6a | 2006-05-30 05:23:59 +0000 | [diff] [blame] | 862 | int status; |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 863 | |
Tim Peters | 33f4a6a | 2006-05-30 05:23:59 +0000 | [diff] [blame] | 864 | status = Py_ReprEnter((PyObject*)mp); |
| 865 | if (status != 0) { |
| 866 | if (status < 0) |
| 867 | return status; |
Brett Cannon | 0153159 | 2007-09-17 03:28:34 +0000 | [diff] [blame] | 868 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 869 | fprintf(fp, "{...}"); |
Brett Cannon | 0153159 | 2007-09-17 03:28:34 +0000 | [diff] [blame] | 870 | Py_END_ALLOW_THREADS |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 871 | return 0; |
| 872 | } |
| 873 | |
Brett Cannon | 0153159 | 2007-09-17 03:28:34 +0000 | [diff] [blame] | 874 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 875 | fprintf(fp, "{"); |
Brett Cannon | 0153159 | 2007-09-17 03:28:34 +0000 | [diff] [blame] | 876 | Py_END_ALLOW_THREADS |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 877 | any = 0; |
Tim Peters | afb6ae8 | 2001-06-04 21:00:21 +0000 | [diff] [blame] | 878 | for (i = 0; i <= mp->ma_mask; i++) { |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 879 | PyDictEntry *ep = mp->ma_table + i; |
Tim Peters | 23cf6be | 2001-06-02 08:02:56 +0000 | [diff] [blame] | 880 | PyObject *pvalue = ep->me_value; |
| 881 | if (pvalue != NULL) { |
| 882 | /* Prevent PyObject_Repr from deleting value during |
| 883 | key format */ |
| 884 | Py_INCREF(pvalue); |
Brett Cannon | 0153159 | 2007-09-17 03:28:34 +0000 | [diff] [blame] | 885 | if (any++ > 0) { |
| 886 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 887 | fprintf(fp, ", "); |
Brett Cannon | 0153159 | 2007-09-17 03:28:34 +0000 | [diff] [blame] | 888 | Py_END_ALLOW_THREADS |
| 889 | } |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 890 | if (PyObject_Print((PyObject *)ep->me_key, fp, 0)!=0) { |
Tim Peters | 23cf6be | 2001-06-02 08:02:56 +0000 | [diff] [blame] | 891 | Py_DECREF(pvalue); |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 892 | Py_ReprLeave((PyObject*)mp); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 893 | return -1; |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 894 | } |
Brett Cannon | 0153159 | 2007-09-17 03:28:34 +0000 | [diff] [blame] | 895 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 896 | fprintf(fp, ": "); |
Brett Cannon | 0153159 | 2007-09-17 03:28:34 +0000 | [diff] [blame] | 897 | Py_END_ALLOW_THREADS |
Tim Peters | 19b77cf | 2001-06-02 08:27:39 +0000 | [diff] [blame] | 898 | if (PyObject_Print(pvalue, fp, 0) != 0) { |
Tim Peters | 23cf6be | 2001-06-02 08:02:56 +0000 | [diff] [blame] | 899 | Py_DECREF(pvalue); |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 900 | Py_ReprLeave((PyObject*)mp); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 901 | return -1; |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 902 | } |
Tim Peters | 23cf6be | 2001-06-02 08:02:56 +0000 | [diff] [blame] | 903 | Py_DECREF(pvalue); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 904 | } |
| 905 | } |
Brett Cannon | 0153159 | 2007-09-17 03:28:34 +0000 | [diff] [blame] | 906 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 907 | fprintf(fp, "}"); |
Brett Cannon | 0153159 | 2007-09-17 03:28:34 +0000 | [diff] [blame] | 908 | Py_END_ALLOW_THREADS |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 909 | Py_ReprLeave((PyObject*)mp); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 910 | return 0; |
| 911 | } |
| 912 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 913 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 914 | dict_repr(PyDictObject *mp) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 915 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 916 | Py_ssize_t i; |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 917 | PyObject *s, *temp, *colon = NULL; |
| 918 | PyObject *pieces = NULL, *result = NULL; |
| 919 | PyObject *key, *value; |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 920 | |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 921 | i = Py_ReprEnter((PyObject *)mp); |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 922 | if (i != 0) { |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 923 | return i > 0 ? PyString_FromString("{...}") : NULL; |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 924 | } |
| 925 | |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 926 | if (mp->ma_used == 0) { |
| 927 | result = PyString_FromString("{}"); |
| 928 | goto Done; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 929 | } |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 930 | |
| 931 | pieces = PyList_New(0); |
| 932 | if (pieces == NULL) |
| 933 | goto Done; |
| 934 | |
| 935 | colon = PyString_FromString(": "); |
| 936 | if (colon == NULL) |
| 937 | goto Done; |
| 938 | |
| 939 | /* Do repr() on each key+value pair, and insert ": " between them. |
| 940 | Note that repr may mutate the dict. */ |
Tim Peters | c605784 | 2001-06-16 07:52:53 +0000 | [diff] [blame] | 941 | i = 0; |
| 942 | while (PyDict_Next((PyObject *)mp, &i, &key, &value)) { |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 943 | int status; |
| 944 | /* Prevent repr from deleting value during key format. */ |
| 945 | Py_INCREF(value); |
| 946 | s = PyObject_Repr(key); |
| 947 | PyString_Concat(&s, colon); |
| 948 | PyString_ConcatAndDel(&s, PyObject_Repr(value)); |
| 949 | Py_DECREF(value); |
| 950 | if (s == NULL) |
| 951 | goto Done; |
| 952 | status = PyList_Append(pieces, s); |
| 953 | Py_DECREF(s); /* append created a new ref */ |
| 954 | if (status < 0) |
| 955 | goto Done; |
| 956 | } |
| 957 | |
| 958 | /* Add "{}" decorations to the first and last items. */ |
| 959 | assert(PyList_GET_SIZE(pieces) > 0); |
| 960 | s = PyString_FromString("{"); |
| 961 | if (s == NULL) |
| 962 | goto Done; |
| 963 | temp = PyList_GET_ITEM(pieces, 0); |
| 964 | PyString_ConcatAndDel(&s, temp); |
| 965 | PyList_SET_ITEM(pieces, 0, s); |
| 966 | if (s == NULL) |
| 967 | goto Done; |
| 968 | |
| 969 | s = PyString_FromString("}"); |
| 970 | if (s == NULL) |
| 971 | goto Done; |
| 972 | temp = PyList_GET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1); |
| 973 | PyString_ConcatAndDel(&temp, s); |
| 974 | PyList_SET_ITEM(pieces, PyList_GET_SIZE(pieces) - 1, temp); |
| 975 | if (temp == NULL) |
| 976 | goto Done; |
| 977 | |
| 978 | /* Paste them all together with ", " between. */ |
| 979 | s = PyString_FromString(", "); |
| 980 | if (s == NULL) |
| 981 | goto Done; |
| 982 | result = _PyString_Join(s, pieces); |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 983 | Py_DECREF(s); |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 984 | |
| 985 | Done: |
| 986 | Py_XDECREF(pieces); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 987 | Py_XDECREF(colon); |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 988 | Py_ReprLeave((PyObject *)mp); |
| 989 | return result; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 990 | } |
| 991 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 992 | static Py_ssize_t |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 993 | dict_length(PyDictObject *mp) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 994 | { |
| 995 | return mp->ma_used; |
| 996 | } |
| 997 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 998 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 999 | dict_subscript(PyDictObject *mp, register PyObject *key) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1000 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1001 | PyObject *v; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 1002 | long hash; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1003 | PyDictEntry *ep; |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 1004 | assert(mp->ma_table != NULL); |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 1005 | if (!PyString_CheckExact(key) || |
Tim Peters | 1f7df35 | 2002-03-29 03:29:08 +0000 | [diff] [blame] | 1006 | (hash = ((PyStringObject *) key)->ob_shash) == -1) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1007 | hash = PyObject_Hash(key); |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 1008 | if (hash == -1) |
| 1009 | return NULL; |
| 1010 | } |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 1011 | ep = (mp->ma_lookup)(mp, key, hash); |
| 1012 | if (ep == NULL) |
| 1013 | return NULL; |
| 1014 | v = ep->me_value; |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1015 | if (v == NULL) { |
| 1016 | if (!PyDict_CheckExact(mp)) { |
| 1017 | /* Look up __missing__ method if we're a subclass. */ |
Guido van Rossum | 4b92a82 | 2006-02-25 23:32:30 +0000 | [diff] [blame] | 1018 | PyObject *missing; |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1019 | static PyObject *missing_str = NULL; |
| 1020 | if (missing_str == NULL) |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 1021 | missing_str = |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1022 | PyString_InternFromString("__missing__"); |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 1023 | missing = _PyType_Lookup(Py_Type(mp), missing_str); |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1024 | if (missing != NULL) |
| 1025 | return PyObject_CallFunctionObjArgs(missing, |
| 1026 | (PyObject *)mp, key, NULL); |
| 1027 | } |
Georg Brandl | b9f4ad3 | 2006-10-29 18:31:42 +0000 | [diff] [blame] | 1028 | set_key_error(key); |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 1029 | return NULL; |
| 1030 | } |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1031 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1032 | Py_INCREF(v); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1033 | return v; |
| 1034 | } |
| 1035 | |
| 1036 | static int |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1037 | dict_ass_sub(PyDictObject *mp, PyObject *v, PyObject *w) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1038 | { |
| 1039 | if (w == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1040 | return PyDict_DelItem((PyObject *)mp, v); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1041 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1042 | return PyDict_SetItem((PyObject *)mp, v, w); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 1045 | static PyMappingMethods dict_as_mapping = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1046 | (lenfunc)dict_length, /*mp_length*/ |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 1047 | (binaryfunc)dict_subscript, /*mp_subscript*/ |
| 1048 | (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1049 | }; |
| 1050 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1051 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1052 | dict_keys(register PyDictObject *mp) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1053 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1054 | register PyObject *v; |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 1055 | register Py_ssize_t i, j; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1056 | PyDictEntry *ep; |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 1057 | Py_ssize_t mask, n; |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1058 | |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1059 | again: |
| 1060 | n = mp->ma_used; |
| 1061 | v = PyList_New(n); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1062 | if (v == NULL) |
| 1063 | return NULL; |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1064 | if (n != mp->ma_used) { |
| 1065 | /* Durnit. The allocations caused the dict to resize. |
| 1066 | * Just start over, this shouldn't normally happen. |
| 1067 | */ |
| 1068 | Py_DECREF(v); |
| 1069 | goto again; |
| 1070 | } |
Raymond Hettinger | 4344278 | 2004-03-17 21:55:03 +0000 | [diff] [blame] | 1071 | ep = mp->ma_table; |
| 1072 | mask = mp->ma_mask; |
| 1073 | for (i = 0, j = 0; i <= mask; i++) { |
| 1074 | if (ep[i].me_value != NULL) { |
| 1075 | PyObject *key = ep[i].me_key; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1076 | Py_INCREF(key); |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1077 | PyList_SET_ITEM(v, j, key); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1078 | j++; |
| 1079 | } |
| 1080 | } |
Raymond Hettinger | 4344278 | 2004-03-17 21:55:03 +0000 | [diff] [blame] | 1081 | assert(j == n); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1082 | return v; |
| 1083 | } |
| 1084 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1085 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1086 | dict_values(register PyDictObject *mp) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1087 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1088 | register PyObject *v; |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 1089 | register Py_ssize_t i, j; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1090 | PyDictEntry *ep; |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 1091 | Py_ssize_t mask, n; |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1092 | |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1093 | again: |
| 1094 | n = mp->ma_used; |
| 1095 | v = PyList_New(n); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1096 | if (v == NULL) |
| 1097 | return NULL; |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1098 | if (n != mp->ma_used) { |
| 1099 | /* Durnit. The allocations caused the dict to resize. |
| 1100 | * Just start over, this shouldn't normally happen. |
| 1101 | */ |
| 1102 | Py_DECREF(v); |
| 1103 | goto again; |
| 1104 | } |
Raymond Hettinger | 4344278 | 2004-03-17 21:55:03 +0000 | [diff] [blame] | 1105 | ep = mp->ma_table; |
| 1106 | mask = mp->ma_mask; |
| 1107 | for (i = 0, j = 0; i <= mask; i++) { |
| 1108 | if (ep[i].me_value != NULL) { |
| 1109 | PyObject *value = ep[i].me_value; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1110 | Py_INCREF(value); |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1111 | PyList_SET_ITEM(v, j, value); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1112 | j++; |
| 1113 | } |
| 1114 | } |
Raymond Hettinger | 4344278 | 2004-03-17 21:55:03 +0000 | [diff] [blame] | 1115 | assert(j == n); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1116 | return v; |
| 1117 | } |
| 1118 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1119 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1120 | dict_items(register PyDictObject *mp) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1121 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1122 | register PyObject *v; |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 1123 | register Py_ssize_t i, j, n; |
| 1124 | Py_ssize_t mask; |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1125 | PyObject *item, *key, *value; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1126 | PyDictEntry *ep; |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1127 | |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1128 | /* Preallocate the list of tuples, to avoid allocations during |
| 1129 | * the loop over the items, which could trigger GC, which |
| 1130 | * could resize the dict. :-( |
| 1131 | */ |
| 1132 | again: |
| 1133 | n = mp->ma_used; |
| 1134 | v = PyList_New(n); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1135 | if (v == NULL) |
| 1136 | return NULL; |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1137 | for (i = 0; i < n; i++) { |
| 1138 | item = PyTuple_New(2); |
| 1139 | if (item == NULL) { |
| 1140 | Py_DECREF(v); |
| 1141 | return NULL; |
| 1142 | } |
| 1143 | PyList_SET_ITEM(v, i, item); |
| 1144 | } |
| 1145 | if (n != mp->ma_used) { |
| 1146 | /* Durnit. The allocations caused the dict to resize. |
| 1147 | * Just start over, this shouldn't normally happen. |
| 1148 | */ |
| 1149 | Py_DECREF(v); |
| 1150 | goto again; |
| 1151 | } |
| 1152 | /* Nothing we do below makes any function calls. */ |
Raymond Hettinger | 4344278 | 2004-03-17 21:55:03 +0000 | [diff] [blame] | 1153 | ep = mp->ma_table; |
| 1154 | mask = mp->ma_mask; |
| 1155 | for (i = 0, j = 0; i <= mask; i++) { |
Raymond Hettinger | 0690512 | 2004-03-19 10:30:00 +0000 | [diff] [blame] | 1156 | if ((value=ep[i].me_value) != NULL) { |
Raymond Hettinger | 4344278 | 2004-03-17 21:55:03 +0000 | [diff] [blame] | 1157 | key = ep[i].me_key; |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1158 | item = PyList_GET_ITEM(v, j); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1159 | Py_INCREF(key); |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1160 | PyTuple_SET_ITEM(item, 0, key); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1161 | Py_INCREF(value); |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1162 | PyTuple_SET_ITEM(item, 1, value); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1163 | j++; |
| 1164 | } |
| 1165 | } |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1166 | assert(j == n); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1167 | return v; |
| 1168 | } |
| 1169 | |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 1170 | static PyObject * |
Tim Peters | bca1cbc | 2002-12-09 22:56:13 +0000 | [diff] [blame] | 1171 | dict_fromkeys(PyObject *cls, PyObject *args) |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 1172 | { |
| 1173 | PyObject *seq; |
| 1174 | PyObject *value = Py_None; |
| 1175 | PyObject *it; /* iter(seq) */ |
| 1176 | PyObject *key; |
| 1177 | PyObject *d; |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 1178 | int status; |
| 1179 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 1180 | if (!PyArg_UnpackTuple(args, "fromkeys", 1, 2, &seq, &value)) |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 1181 | return NULL; |
| 1182 | |
| 1183 | d = PyObject_CallObject(cls, NULL); |
| 1184 | if (d == NULL) |
| 1185 | return NULL; |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 1186 | |
Raymond Hettinger | cdcf887 | 2007-11-07 02:26:17 +0000 | [diff] [blame] | 1187 | if (PyDict_CheckExact(d) && PyDict_CheckExact(seq)) { |
| 1188 | PyDictObject *mp = (PyDictObject *)d; |
| 1189 | PyObject *oldvalue; |
| 1190 | Py_ssize_t pos = 0; |
| 1191 | PyObject *key; |
| 1192 | long hash; |
| 1193 | |
Raymond Hettinger | 3444879 | 2007-11-09 23:14:44 +0000 | [diff] [blame] | 1194 | if (dictresize(mp, PySet_GET_SIZE(seq))) |
Raymond Hettinger | cdcf887 | 2007-11-07 02:26:17 +0000 | [diff] [blame] | 1195 | return NULL; |
| 1196 | |
| 1197 | while (_PyDict_Next(seq, &pos, &key, &oldvalue, &hash)) { |
| 1198 | Py_INCREF(key); |
| 1199 | Py_INCREF(value); |
| 1200 | if (insertdict(mp, key, hash, value)) |
| 1201 | return NULL; |
| 1202 | } |
| 1203 | return d; |
| 1204 | } |
| 1205 | |
Raymond Hettinger | 0bbbfc4 | 2007-03-20 21:27:24 +0000 | [diff] [blame] | 1206 | if (PyDict_CheckExact(d) && PyAnySet_CheckExact(seq)) { |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1207 | PyDictObject *mp = (PyDictObject *)d; |
Raymond Hettinger | 0bbbfc4 | 2007-03-20 21:27:24 +0000 | [diff] [blame] | 1208 | Py_ssize_t pos = 0; |
| 1209 | PyObject *key; |
| 1210 | long hash; |
| 1211 | |
| 1212 | if (dictresize(mp, PySet_GET_SIZE(seq))) |
| 1213 | return NULL; |
| 1214 | |
| 1215 | while (_PySet_NextEntry(seq, &pos, &key, &hash)) { |
| 1216 | Py_INCREF(key); |
Raymond Hettinger | e3146f5 | 2007-03-21 20:33:57 +0000 | [diff] [blame] | 1217 | Py_INCREF(value); |
| 1218 | if (insertdict(mp, key, hash, value)) |
Raymond Hettinger | 0bbbfc4 | 2007-03-20 21:27:24 +0000 | [diff] [blame] | 1219 | return NULL; |
| 1220 | } |
| 1221 | return d; |
| 1222 | } |
| 1223 | |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 1224 | it = PyObject_GetIter(seq); |
| 1225 | if (it == NULL){ |
| 1226 | Py_DECREF(d); |
| 1227 | return NULL; |
| 1228 | } |
| 1229 | |
Raymond Hettinger | 3444879 | 2007-11-09 23:14:44 +0000 | [diff] [blame] | 1230 | if (PyDict_CheckExact(d)) { |
| 1231 | while ((key = PyIter_Next(it)) != NULL) { |
| 1232 | status = PyDict_SetItem(d, key, value); |
| 1233 | Py_DECREF(key); |
| 1234 | if (status < 0) |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 1235 | goto Fail; |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 1236 | } |
Raymond Hettinger | 3444879 | 2007-11-09 23:14:44 +0000 | [diff] [blame] | 1237 | } else { |
| 1238 | while ((key = PyIter_Next(it)) != NULL) { |
| 1239 | status = PyObject_SetItem(d, key, value); |
| 1240 | Py_DECREF(key); |
| 1241 | if (status < 0) |
| 1242 | goto Fail; |
| 1243 | } |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
Raymond Hettinger | 3444879 | 2007-11-09 23:14:44 +0000 | [diff] [blame] | 1246 | if (PyErr_Occurred()) |
| 1247 | goto Fail; |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 1248 | Py_DECREF(it); |
| 1249 | return d; |
| 1250 | |
| 1251 | Fail: |
| 1252 | Py_DECREF(it); |
| 1253 | Py_DECREF(d); |
| 1254 | return NULL; |
| 1255 | } |
| 1256 | |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 1257 | static int |
| 1258 | dict_update_common(PyObject *self, PyObject *args, PyObject *kwds, char *methname) |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 1259 | { |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 1260 | PyObject *arg = NULL; |
| 1261 | int result = 0; |
| 1262 | |
| 1263 | if (!PyArg_UnpackTuple(args, methname, 0, 1, &arg)) |
| 1264 | result = -1; |
| 1265 | |
| 1266 | else if (arg != NULL) { |
| 1267 | if (PyObject_HasAttrString(arg, "keys")) |
| 1268 | result = PyDict_Merge(self, arg, 1); |
| 1269 | else |
| 1270 | result = PyDict_MergeFromSeq2(self, arg, 1); |
| 1271 | } |
| 1272 | if (result == 0 && kwds != NULL) |
| 1273 | result = PyDict_Merge(self, kwds, 1); |
| 1274 | return result; |
| 1275 | } |
| 1276 | |
| 1277 | static PyObject * |
| 1278 | dict_update(PyObject *self, PyObject *args, PyObject *kwds) |
| 1279 | { |
Raymond Hettinger | 7892b1c | 2004-04-12 18:10:01 +0000 | [diff] [blame] | 1280 | if (dict_update_common(self, args, kwds, "update") != -1) |
| 1281 | Py_RETURN_NONE; |
| 1282 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1283 | } |
| 1284 | |
Guido van Rossum | 05ac6de | 2001-08-10 20:28:28 +0000 | [diff] [blame] | 1285 | /* Update unconditionally replaces existing items. |
| 1286 | Merge has a 3rd argument 'override'; if set, it acts like Update, |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1287 | otherwise it leaves existing items unchanged. |
| 1288 | |
| 1289 | PyDict_{Update,Merge} update/merge from a mapping object. |
| 1290 | |
Tim Peters | f582b82 | 2001-12-11 18:51:08 +0000 | [diff] [blame] | 1291 | PyDict_MergeFromSeq2 updates/merges from any iterable object |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1292 | producing iterable objects of length 2. |
| 1293 | */ |
| 1294 | |
Tim Peters | f582b82 | 2001-12-11 18:51:08 +0000 | [diff] [blame] | 1295 | int |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1296 | PyDict_MergeFromSeq2(PyObject *d, PyObject *seq2, int override) |
| 1297 | { |
| 1298 | PyObject *it; /* iter(seq2) */ |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 1299 | Py_ssize_t i; /* index into seq2 of current element */ |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1300 | PyObject *item; /* seq2[i] */ |
| 1301 | PyObject *fast; /* item as a 2-tuple or 2-list */ |
| 1302 | |
| 1303 | assert(d != NULL); |
| 1304 | assert(PyDict_Check(d)); |
| 1305 | assert(seq2 != NULL); |
| 1306 | |
| 1307 | it = PyObject_GetIter(seq2); |
| 1308 | if (it == NULL) |
| 1309 | return -1; |
| 1310 | |
| 1311 | for (i = 0; ; ++i) { |
| 1312 | PyObject *key, *value; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1313 | Py_ssize_t n; |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1314 | |
| 1315 | fast = NULL; |
| 1316 | item = PyIter_Next(it); |
| 1317 | if (item == NULL) { |
| 1318 | if (PyErr_Occurred()) |
| 1319 | goto Fail; |
| 1320 | break; |
| 1321 | } |
| 1322 | |
| 1323 | /* Convert item to sequence, and verify length 2. */ |
| 1324 | fast = PySequence_Fast(item, ""); |
| 1325 | if (fast == NULL) { |
| 1326 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 1327 | PyErr_Format(PyExc_TypeError, |
| 1328 | "cannot convert dictionary update " |
Neal Norwitz | d3881b0 | 2006-05-30 04:25:05 +0000 | [diff] [blame] | 1329 | "sequence element #%zd to a sequence", |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1330 | i); |
| 1331 | goto Fail; |
| 1332 | } |
| 1333 | n = PySequence_Fast_GET_SIZE(fast); |
| 1334 | if (n != 2) { |
| 1335 | PyErr_Format(PyExc_ValueError, |
Neal Norwitz | d3881b0 | 2006-05-30 04:25:05 +0000 | [diff] [blame] | 1336 | "dictionary update sequence element #%zd " |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 1337 | "has length %zd; 2 is required", |
Martin v. Löwis | e0e89f7 | 2006-02-16 06:59:22 +0000 | [diff] [blame] | 1338 | i, n); |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1339 | goto Fail; |
| 1340 | } |
| 1341 | |
| 1342 | /* Update/merge with this (key, value) pair. */ |
| 1343 | key = PySequence_Fast_GET_ITEM(fast, 0); |
| 1344 | value = PySequence_Fast_GET_ITEM(fast, 1); |
| 1345 | if (override || PyDict_GetItem(d, key) == NULL) { |
| 1346 | int status = PyDict_SetItem(d, key, value); |
| 1347 | if (status < 0) |
| 1348 | goto Fail; |
| 1349 | } |
| 1350 | Py_DECREF(fast); |
| 1351 | Py_DECREF(item); |
| 1352 | } |
| 1353 | |
| 1354 | i = 0; |
| 1355 | goto Return; |
| 1356 | Fail: |
| 1357 | Py_XDECREF(item); |
| 1358 | Py_XDECREF(fast); |
| 1359 | i = -1; |
| 1360 | Return: |
| 1361 | Py_DECREF(it); |
Neal Norwitz | d3881b0 | 2006-05-30 04:25:05 +0000 | [diff] [blame] | 1362 | return Py_SAFE_DOWNCAST(i, Py_ssize_t, int); |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1365 | int |
| 1366 | PyDict_Update(PyObject *a, PyObject *b) |
| 1367 | { |
Guido van Rossum | 05ac6de | 2001-08-10 20:28:28 +0000 | [diff] [blame] | 1368 | return PyDict_Merge(a, b, 1); |
| 1369 | } |
| 1370 | |
| 1371 | int |
| 1372 | PyDict_Merge(PyObject *a, PyObject *b, int override) |
| 1373 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1374 | register PyDictObject *mp, *other; |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 1375 | register Py_ssize_t i; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1376 | PyDictEntry *entry; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1377 | |
Barry Warsaw | 66a0d1d | 2001-06-26 20:08:32 +0000 | [diff] [blame] | 1378 | /* We accept for the argument either a concrete dictionary object, |
| 1379 | * or an abstract "mapping" object. For the former, we can do |
| 1380 | * things quite efficiently. For the latter, we only require that |
| 1381 | * PyMapping_Keys() and PyObject_GetItem() be supported. |
| 1382 | */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1383 | if (a == NULL || !PyDict_Check(a) || b == NULL) { |
| 1384 | PyErr_BadInternalCall(); |
| 1385 | return -1; |
| 1386 | } |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1387 | mp = (PyDictObject*)a; |
Raymond Hettinger | 0922d71 | 2007-02-07 20:08:22 +0000 | [diff] [blame] | 1388 | if (PyDict_CheckExact(b)) { |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1389 | other = (PyDictObject*)b; |
Barry Warsaw | 66a0d1d | 2001-06-26 20:08:32 +0000 | [diff] [blame] | 1390 | if (other == mp || other->ma_used == 0) |
| 1391 | /* a.update(a) or a.update({}); nothing to do */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1392 | return 0; |
Raymond Hettinger | 186e739 | 2005-05-14 18:08:25 +0000 | [diff] [blame] | 1393 | if (mp->ma_used == 0) |
| 1394 | /* Since the target dict is empty, PyDict_GetItem() |
| 1395 | * always returns NULL. Setting override to 1 |
| 1396 | * skips the unnecessary test. |
| 1397 | */ |
| 1398 | override = 1; |
Barry Warsaw | 66a0d1d | 2001-06-26 20:08:32 +0000 | [diff] [blame] | 1399 | /* Do one big resize at the start, rather than |
| 1400 | * incrementally resizing as we insert new items. Expect |
| 1401 | * that there will be no (or few) overlapping keys. |
| 1402 | */ |
| 1403 | 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] | 1404 | if (dictresize(mp, (mp->ma_used + other->ma_used)*2) != 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1405 | return -1; |
Barry Warsaw | 66a0d1d | 2001-06-26 20:08:32 +0000 | [diff] [blame] | 1406 | } |
| 1407 | for (i = 0; i <= other->ma_mask; i++) { |
| 1408 | entry = &other->ma_table[i]; |
Guido van Rossum | 05ac6de | 2001-08-10 20:28:28 +0000 | [diff] [blame] | 1409 | if (entry->me_value != NULL && |
| 1410 | (override || |
| 1411 | PyDict_GetItem(a, entry->me_key) == NULL)) { |
Barry Warsaw | 66a0d1d | 2001-06-26 20:08:32 +0000 | [diff] [blame] | 1412 | Py_INCREF(entry->me_key); |
| 1413 | Py_INCREF(entry->me_value); |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 1414 | if (insertdict(mp, entry->me_key, |
| 1415 | (long)entry->me_hash, |
| 1416 | entry->me_value) != 0) |
| 1417 | return -1; |
Barry Warsaw | 66a0d1d | 2001-06-26 20:08:32 +0000 | [diff] [blame] | 1418 | } |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 1419 | } |
| 1420 | } |
Barry Warsaw | 66a0d1d | 2001-06-26 20:08:32 +0000 | [diff] [blame] | 1421 | else { |
| 1422 | /* Do it the generic, slower way */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1423 | PyObject *keys = PyMapping_Keys(b); |
Barry Warsaw | 66a0d1d | 2001-06-26 20:08:32 +0000 | [diff] [blame] | 1424 | PyObject *iter; |
| 1425 | PyObject *key, *value; |
| 1426 | int status; |
| 1427 | |
| 1428 | if (keys == NULL) |
| 1429 | /* Docstring says this is equivalent to E.keys() so |
| 1430 | * if E doesn't have a .keys() method we want |
| 1431 | * AttributeError to percolate up. Might as well |
| 1432 | * do the same for any other error. |
| 1433 | */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1434 | return -1; |
Barry Warsaw | 66a0d1d | 2001-06-26 20:08:32 +0000 | [diff] [blame] | 1435 | |
| 1436 | iter = PyObject_GetIter(keys); |
| 1437 | Py_DECREF(keys); |
| 1438 | if (iter == NULL) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1439 | return -1; |
Barry Warsaw | 66a0d1d | 2001-06-26 20:08:32 +0000 | [diff] [blame] | 1440 | |
| 1441 | for (key = PyIter_Next(iter); key; key = PyIter_Next(iter)) { |
Guido van Rossum | 05ac6de | 2001-08-10 20:28:28 +0000 | [diff] [blame] | 1442 | if (!override && PyDict_GetItem(a, key) != NULL) { |
| 1443 | Py_DECREF(key); |
| 1444 | continue; |
| 1445 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1446 | value = PyObject_GetItem(b, key); |
Barry Warsaw | 66a0d1d | 2001-06-26 20:08:32 +0000 | [diff] [blame] | 1447 | if (value == NULL) { |
| 1448 | Py_DECREF(iter); |
| 1449 | Py_DECREF(key); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1450 | return -1; |
Barry Warsaw | 66a0d1d | 2001-06-26 20:08:32 +0000 | [diff] [blame] | 1451 | } |
Guido van Rossum | 05ac6de | 2001-08-10 20:28:28 +0000 | [diff] [blame] | 1452 | status = PyDict_SetItem(a, key, value); |
Barry Warsaw | 66a0d1d | 2001-06-26 20:08:32 +0000 | [diff] [blame] | 1453 | Py_DECREF(key); |
| 1454 | Py_DECREF(value); |
| 1455 | if (status < 0) { |
| 1456 | Py_DECREF(iter); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1457 | return -1; |
Barry Warsaw | 66a0d1d | 2001-06-26 20:08:32 +0000 | [diff] [blame] | 1458 | } |
| 1459 | } |
| 1460 | Py_DECREF(iter); |
| 1461 | if (PyErr_Occurred()) |
| 1462 | /* Iterator completed, via error */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1463 | return -1; |
Barry Warsaw | 66a0d1d | 2001-06-26 20:08:32 +0000 | [diff] [blame] | 1464 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1465 | return 0; |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1469 | dict_copy(register PyDictObject *mp) |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 1470 | { |
Jeremy Hylton | a12c7a7 | 2000-03-30 22:27:31 +0000 | [diff] [blame] | 1471 | return PyDict_Copy((PyObject*)mp); |
| 1472 | } |
| 1473 | |
| 1474 | PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 1475 | PyDict_Copy(PyObject *o) |
Jeremy Hylton | a12c7a7 | 2000-03-30 22:27:31 +0000 | [diff] [blame] | 1476 | { |
Raymond Hettinger | ebedb2f | 2004-03-08 04:19:01 +0000 | [diff] [blame] | 1477 | PyObject *copy; |
Jeremy Hylton | a12c7a7 | 2000-03-30 22:27:31 +0000 | [diff] [blame] | 1478 | |
| 1479 | if (o == NULL || !PyDict_Check(o)) { |
| 1480 | PyErr_BadInternalCall(); |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 1481 | return NULL; |
Jeremy Hylton | a12c7a7 | 2000-03-30 22:27:31 +0000 | [diff] [blame] | 1482 | } |
Raymond Hettinger | ebedb2f | 2004-03-08 04:19:01 +0000 | [diff] [blame] | 1483 | copy = PyDict_New(); |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 1484 | if (copy == NULL) |
| 1485 | return NULL; |
Raymond Hettinger | ebedb2f | 2004-03-08 04:19:01 +0000 | [diff] [blame] | 1486 | if (PyDict_Merge(copy, o, 1) == 0) |
| 1487 | return copy; |
| 1488 | Py_DECREF(copy); |
Raymond Hettinger | 1356f78 | 2005-04-15 15:58:42 +0000 | [diff] [blame] | 1489 | return NULL; |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 1490 | } |
| 1491 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1492 | Py_ssize_t |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 1493 | PyDict_Size(PyObject *mp) |
Guido van Rossum | 4199fac | 1993-11-05 10:18:44 +0000 | [diff] [blame] | 1494 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1495 | if (mp == NULL || !PyDict_Check(mp)) { |
| 1496 | PyErr_BadInternalCall(); |
Jeremy Hylton | 7083bb7 | 2004-02-17 20:10:11 +0000 | [diff] [blame] | 1497 | return -1; |
Guido van Rossum | 4199fac | 1993-11-05 10:18:44 +0000 | [diff] [blame] | 1498 | } |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1499 | return ((PyDictObject *)mp)->ma_used; |
Guido van Rossum | 4199fac | 1993-11-05 10:18:44 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1502 | PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 1503 | PyDict_Keys(PyObject *mp) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1504 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1505 | if (mp == NULL || !PyDict_Check(mp)) { |
| 1506 | PyErr_BadInternalCall(); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1507 | return NULL; |
| 1508 | } |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1509 | return dict_keys((PyDictObject *)mp); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1510 | } |
| 1511 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1512 | PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 1513 | PyDict_Values(PyObject *mp) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1514 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1515 | if (mp == NULL || !PyDict_Check(mp)) { |
| 1516 | PyErr_BadInternalCall(); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1517 | return NULL; |
| 1518 | } |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1519 | return dict_values((PyDictObject *)mp); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1520 | } |
| 1521 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1522 | PyObject * |
Tim Peters | 1f5871e | 2000-07-04 17:44:48 +0000 | [diff] [blame] | 1523 | PyDict_Items(PyObject *mp) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1524 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1525 | if (mp == NULL || !PyDict_Check(mp)) { |
| 1526 | PyErr_BadInternalCall(); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1527 | return NULL; |
| 1528 | } |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1529 | return dict_items((PyDictObject *)mp); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1530 | } |
| 1531 | |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1532 | /* Subroutine which returns the smallest key in a for which b's value |
| 1533 | is different or absent. The value is returned too, through the |
Tim Peters | 95bf939 | 2001-05-10 08:32:44 +0000 | [diff] [blame] | 1534 | pval argument. Both are NULL if no key in a is found for which b's status |
| 1535 | differs. The refcounts on (and only on) non-NULL *pval and function return |
| 1536 | values must be decremented by the caller (characterize() increments them |
| 1537 | to ensure that mutating comparison and PyDict_GetItem calls can't delete |
| 1538 | them before the caller is done looking at them). */ |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1539 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1540 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1541 | characterize(PyDictObject *a, PyDictObject *b, PyObject **pval) |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1542 | { |
Tim Peters | 95bf939 | 2001-05-10 08:32:44 +0000 | [diff] [blame] | 1543 | PyObject *akey = NULL; /* smallest key in a s.t. a[akey] != b[akey] */ |
| 1544 | PyObject *aval = NULL; /* a[akey] */ |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 1545 | Py_ssize_t i; |
| 1546 | int cmp; |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1547 | |
Tim Peters | afb6ae8 | 2001-06-04 21:00:21 +0000 | [diff] [blame] | 1548 | for (i = 0; i <= a->ma_mask; i++) { |
Tim Peters | 95bf939 | 2001-05-10 08:32:44 +0000 | [diff] [blame] | 1549 | PyObject *thiskey, *thisaval, *thisbval; |
| 1550 | if (a->ma_table[i].me_value == NULL) |
| 1551 | continue; |
| 1552 | thiskey = a->ma_table[i].me_key; |
| 1553 | Py_INCREF(thiskey); /* keep alive across compares */ |
| 1554 | if (akey != NULL) { |
| 1555 | cmp = PyObject_RichCompareBool(akey, thiskey, Py_LT); |
| 1556 | if (cmp < 0) { |
| 1557 | Py_DECREF(thiskey); |
| 1558 | goto Fail; |
| 1559 | } |
| 1560 | if (cmp > 0 || |
Tim Peters | afb6ae8 | 2001-06-04 21:00:21 +0000 | [diff] [blame] | 1561 | i > a->ma_mask || |
Tim Peters | 95bf939 | 2001-05-10 08:32:44 +0000 | [diff] [blame] | 1562 | a->ma_table[i].me_value == NULL) |
| 1563 | { |
| 1564 | /* Not the *smallest* a key; or maybe it is |
| 1565 | * but the compare shrunk the dict so we can't |
| 1566 | * find its associated value anymore; or |
| 1567 | * maybe it is but the compare deleted the |
| 1568 | * a[thiskey] entry. |
Neal Norwitz | d3881b0 | 2006-05-30 04:25:05 +0000 | [diff] [blame] | 1569 | */ |
Tim Peters | 95bf939 | 2001-05-10 08:32:44 +0000 | [diff] [blame] | 1570 | Py_DECREF(thiskey); |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1571 | continue; |
Guido van Rossum | b932420 | 2001-01-18 00:39:02 +0000 | [diff] [blame] | 1572 | } |
Tim Peters | 95bf939 | 2001-05-10 08:32:44 +0000 | [diff] [blame] | 1573 | } |
| 1574 | |
| 1575 | /* Compare a[thiskey] to b[thiskey]; cmp <- true iff equal. */ |
| 1576 | thisaval = a->ma_table[i].me_value; |
| 1577 | assert(thisaval); |
| 1578 | Py_INCREF(thisaval); /* keep alive */ |
| 1579 | thisbval = PyDict_GetItem((PyObject *)b, thiskey); |
| 1580 | if (thisbval == NULL) |
| 1581 | cmp = 0; |
| 1582 | else { |
| 1583 | /* both dicts have thiskey: same values? */ |
| 1584 | cmp = PyObject_RichCompareBool( |
| 1585 | thisaval, thisbval, Py_EQ); |
| 1586 | if (cmp < 0) { |
| 1587 | Py_DECREF(thiskey); |
| 1588 | Py_DECREF(thisaval); |
| 1589 | goto Fail; |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1590 | } |
| 1591 | } |
Tim Peters | 95bf939 | 2001-05-10 08:32:44 +0000 | [diff] [blame] | 1592 | if (cmp == 0) { |
| 1593 | /* New winner. */ |
| 1594 | Py_XDECREF(akey); |
| 1595 | Py_XDECREF(aval); |
| 1596 | akey = thiskey; |
| 1597 | aval = thisaval; |
| 1598 | } |
| 1599 | else { |
| 1600 | Py_DECREF(thiskey); |
| 1601 | Py_DECREF(thisaval); |
| 1602 | } |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1603 | } |
Tim Peters | 95bf939 | 2001-05-10 08:32:44 +0000 | [diff] [blame] | 1604 | *pval = aval; |
| 1605 | return akey; |
| 1606 | |
| 1607 | Fail: |
| 1608 | Py_XDECREF(akey); |
| 1609 | Py_XDECREF(aval); |
| 1610 | *pval = NULL; |
| 1611 | return NULL; |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1612 | } |
| 1613 | |
| 1614 | static int |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1615 | dict_compare(PyDictObject *a, PyDictObject *b) |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1616 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1617 | PyObject *adiff, *bdiff, *aval, *bval; |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1618 | int res; |
| 1619 | |
| 1620 | /* Compare lengths first */ |
| 1621 | if (a->ma_used < b->ma_used) |
| 1622 | return -1; /* a is shorter */ |
| 1623 | else if (a->ma_used > b->ma_used) |
| 1624 | return 1; /* b is shorter */ |
Tim Peters | 95bf939 | 2001-05-10 08:32:44 +0000 | [diff] [blame] | 1625 | |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1626 | /* Same length -- check all keys */ |
Tim Peters | 95bf939 | 2001-05-10 08:32:44 +0000 | [diff] [blame] | 1627 | bdiff = bval = NULL; |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1628 | adiff = characterize(a, b, &aval); |
Tim Peters | 95bf939 | 2001-05-10 08:32:44 +0000 | [diff] [blame] | 1629 | if (adiff == NULL) { |
| 1630 | assert(!aval); |
Tim Peters | 3918fb2 | 2001-05-10 18:58:31 +0000 | [diff] [blame] | 1631 | /* Either an error, or a is a subset with the same length so |
Tim Peters | 95bf939 | 2001-05-10 08:32:44 +0000 | [diff] [blame] | 1632 | * must be equal. |
| 1633 | */ |
| 1634 | res = PyErr_Occurred() ? -1 : 0; |
| 1635 | goto Finished; |
| 1636 | } |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1637 | bdiff = characterize(b, a, &bval); |
Tim Peters | 95bf939 | 2001-05-10 08:32:44 +0000 | [diff] [blame] | 1638 | if (bdiff == NULL && PyErr_Occurred()) { |
| 1639 | assert(!bval); |
| 1640 | res = -1; |
| 1641 | goto Finished; |
| 1642 | } |
| 1643 | res = 0; |
| 1644 | if (bdiff) { |
| 1645 | /* bdiff == NULL "should be" impossible now, but perhaps |
| 1646 | * the last comparison done by the characterize() on a had |
| 1647 | * the side effect of making the dicts equal! |
| 1648 | */ |
| 1649 | res = PyObject_Compare(adiff, bdiff); |
| 1650 | } |
| 1651 | if (res == 0 && bval != NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1652 | res = PyObject_Compare(aval, bval); |
Tim Peters | 95bf939 | 2001-05-10 08:32:44 +0000 | [diff] [blame] | 1653 | |
| 1654 | Finished: |
| 1655 | Py_XDECREF(adiff); |
| 1656 | Py_XDECREF(bdiff); |
| 1657 | Py_XDECREF(aval); |
| 1658 | Py_XDECREF(bval); |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 1659 | return res; |
| 1660 | } |
| 1661 | |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 1662 | /* Return 1 if dicts equal, 0 if not, -1 if error. |
| 1663 | * Gets out as soon as any difference is detected. |
| 1664 | * Uses only Py_EQ comparison. |
| 1665 | */ |
| 1666 | static int |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1667 | dict_equal(PyDictObject *a, PyDictObject *b) |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 1668 | { |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 1669 | Py_ssize_t i; |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 1670 | |
| 1671 | if (a->ma_used != b->ma_used) |
| 1672 | /* can't be equal if # of entries differ */ |
| 1673 | return 0; |
Tim Peters | eb28ef2 | 2001-06-02 05:27:19 +0000 | [diff] [blame] | 1674 | |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 1675 | /* Same # of entries -- check all of 'em. Exit early on any diff. */ |
Tim Peters | afb6ae8 | 2001-06-04 21:00:21 +0000 | [diff] [blame] | 1676 | for (i = 0; i <= a->ma_mask; i++) { |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 1677 | PyObject *aval = a->ma_table[i].me_value; |
| 1678 | if (aval != NULL) { |
| 1679 | int cmp; |
| 1680 | PyObject *bval; |
| 1681 | PyObject *key = a->ma_table[i].me_key; |
| 1682 | /* temporarily bump aval's refcount to ensure it stays |
| 1683 | alive until we're done with it */ |
| 1684 | Py_INCREF(aval); |
Neal Norwitz | a22975f | 2006-09-05 02:24:03 +0000 | [diff] [blame] | 1685 | /* ditto for key */ |
| 1686 | Py_INCREF(key); |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 1687 | bval = PyDict_GetItem((PyObject *)b, key); |
Neal Norwitz | a22975f | 2006-09-05 02:24:03 +0000 | [diff] [blame] | 1688 | Py_DECREF(key); |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 1689 | if (bval == NULL) { |
| 1690 | Py_DECREF(aval); |
| 1691 | return 0; |
| 1692 | } |
| 1693 | cmp = PyObject_RichCompareBool(aval, bval, Py_EQ); |
| 1694 | Py_DECREF(aval); |
| 1695 | if (cmp <= 0) /* error or not equal */ |
| 1696 | return cmp; |
| 1697 | } |
| 1698 | } |
| 1699 | return 1; |
| 1700 | } |
| 1701 | |
| 1702 | static PyObject * |
| 1703 | dict_richcompare(PyObject *v, PyObject *w, int op) |
| 1704 | { |
| 1705 | int cmp; |
| 1706 | PyObject *res; |
| 1707 | |
| 1708 | if (!PyDict_Check(v) || !PyDict_Check(w)) { |
| 1709 | res = Py_NotImplemented; |
| 1710 | } |
| 1711 | else if (op == Py_EQ || op == Py_NE) { |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1712 | cmp = dict_equal((PyDictObject *)v, (PyDictObject *)w); |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 1713 | if (cmp < 0) |
| 1714 | return NULL; |
| 1715 | res = (cmp == (op == Py_EQ)) ? Py_True : Py_False; |
| 1716 | } |
Tim Peters | 4fa58bf | 2001-05-10 21:45:19 +0000 | [diff] [blame] | 1717 | else |
| 1718 | res = Py_NotImplemented; |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 1719 | Py_INCREF(res); |
| 1720 | return res; |
| 1721 | } |
| 1722 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1723 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1724 | dict_contains(register PyDictObject *mp, PyObject *key) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1725 | { |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1726 | long hash; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1727 | PyDictEntry *ep; |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 1728 | |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 1729 | if (!PyString_CheckExact(key) || |
Tim Peters | 1f7df35 | 2002-03-29 03:29:08 +0000 | [diff] [blame] | 1730 | (hash = ((PyStringObject *) key)->ob_shash) == -1) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1731 | hash = PyObject_Hash(key); |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 1732 | if (hash == -1) |
| 1733 | return NULL; |
| 1734 | } |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 1735 | ep = (mp->ma_lookup)(mp, key, hash); |
| 1736 | if (ep == NULL) |
| 1737 | return NULL; |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 1738 | return PyBool_FromLong(ep->me_value != NULL); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1739 | } |
| 1740 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1741 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1742 | dict_has_key(register PyDictObject *mp, PyObject *key) |
Neal Norwitz | 8b2bfbc | 2007-05-23 06:35:32 +0000 | [diff] [blame] | 1743 | { |
| 1744 | if (Py_Py3kWarningFlag && |
| 1745 | PyErr_Warn(PyExc_DeprecationWarning, |
| 1746 | "dict.has_key() not supported in 3.x") < 0) |
| 1747 | return NULL; |
| 1748 | return dict_contains(mp, key); |
| 1749 | } |
| 1750 | |
| 1751 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1752 | dict_get(register PyDictObject *mp, PyObject *args) |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 1753 | { |
| 1754 | PyObject *key; |
Barry Warsaw | 320ac33 | 1997-10-20 17:26:25 +0000 | [diff] [blame] | 1755 | PyObject *failobj = Py_None; |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 1756 | PyObject *val = NULL; |
| 1757 | long hash; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1758 | PyDictEntry *ep; |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 1759 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 1760 | if (!PyArg_UnpackTuple(args, "get", 1, 2, &key, &failobj)) |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 1761 | return NULL; |
| 1762 | |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 1763 | if (!PyString_CheckExact(key) || |
Tim Peters | 1f7df35 | 2002-03-29 03:29:08 +0000 | [diff] [blame] | 1764 | (hash = ((PyStringObject *) key)->ob_shash) == -1) { |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 1765 | hash = PyObject_Hash(key); |
| 1766 | if (hash == -1) |
| 1767 | return NULL; |
| 1768 | } |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 1769 | ep = (mp->ma_lookup)(mp, key, hash); |
| 1770 | if (ep == NULL) |
| 1771 | return NULL; |
| 1772 | val = ep->me_value; |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 1773 | if (val == NULL) |
| 1774 | val = failobj; |
| 1775 | Py_INCREF(val); |
| 1776 | return val; |
| 1777 | } |
| 1778 | |
| 1779 | |
| 1780 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1781 | dict_setdefault(register PyDictObject *mp, PyObject *args) |
Guido van Rossum | 164452c | 2000-08-08 16:12:54 +0000 | [diff] [blame] | 1782 | { |
| 1783 | PyObject *key; |
| 1784 | PyObject *failobj = Py_None; |
| 1785 | PyObject *val = NULL; |
| 1786 | long hash; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1787 | PyDictEntry *ep; |
Guido van Rossum | 164452c | 2000-08-08 16:12:54 +0000 | [diff] [blame] | 1788 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 1789 | if (!PyArg_UnpackTuple(args, "setdefault", 1, 2, &key, &failobj)) |
Guido van Rossum | 164452c | 2000-08-08 16:12:54 +0000 | [diff] [blame] | 1790 | return NULL; |
Guido van Rossum | 164452c | 2000-08-08 16:12:54 +0000 | [diff] [blame] | 1791 | |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 1792 | if (!PyString_CheckExact(key) || |
Tim Peters | 1f7df35 | 2002-03-29 03:29:08 +0000 | [diff] [blame] | 1793 | (hash = ((PyStringObject *) key)->ob_shash) == -1) { |
Guido van Rossum | 164452c | 2000-08-08 16:12:54 +0000 | [diff] [blame] | 1794 | hash = PyObject_Hash(key); |
| 1795 | if (hash == -1) |
| 1796 | return NULL; |
| 1797 | } |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 1798 | ep = (mp->ma_lookup)(mp, key, hash); |
| 1799 | if (ep == NULL) |
| 1800 | return NULL; |
| 1801 | val = ep->me_value; |
Guido van Rossum | 164452c | 2000-08-08 16:12:54 +0000 | [diff] [blame] | 1802 | if (val == NULL) { |
| 1803 | val = failobj; |
| 1804 | if (PyDict_SetItem((PyObject*)mp, key, failobj)) |
| 1805 | val = NULL; |
| 1806 | } |
| 1807 | Py_XINCREF(val); |
| 1808 | return val; |
| 1809 | } |
| 1810 | |
| 1811 | |
| 1812 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1813 | dict_clear(register PyDictObject *mp) |
Guido van Rossum | fb8f1ca | 1997-03-21 21:55:12 +0000 | [diff] [blame] | 1814 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1815 | PyDict_Clear((PyObject *)mp); |
Raymond Hettinger | 7892b1c | 2004-04-12 18:10:01 +0000 | [diff] [blame] | 1816 | Py_RETURN_NONE; |
Guido van Rossum | fb8f1ca | 1997-03-21 21:55:12 +0000 | [diff] [blame] | 1817 | } |
| 1818 | |
Guido van Rossum | ba6ab84 | 2000-12-12 22:02:18 +0000 | [diff] [blame] | 1819 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1820 | dict_pop(PyDictObject *mp, PyObject *args) |
Guido van Rossum | e027d98 | 2002-04-12 15:11:59 +0000 | [diff] [blame] | 1821 | { |
| 1822 | long hash; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1823 | PyDictEntry *ep; |
Guido van Rossum | e027d98 | 2002-04-12 15:11:59 +0000 | [diff] [blame] | 1824 | PyObject *old_value, *old_key; |
Raymond Hettinger | a3e1e4c | 2003-03-06 23:54:28 +0000 | [diff] [blame] | 1825 | PyObject *key, *deflt = NULL; |
Guido van Rossum | e027d98 | 2002-04-12 15:11:59 +0000 | [diff] [blame] | 1826 | |
Raymond Hettinger | a3e1e4c | 2003-03-06 23:54:28 +0000 | [diff] [blame] | 1827 | if(!PyArg_UnpackTuple(args, "pop", 1, 2, &key, &deflt)) |
| 1828 | return NULL; |
Guido van Rossum | e027d98 | 2002-04-12 15:11:59 +0000 | [diff] [blame] | 1829 | if (mp->ma_used == 0) { |
Raymond Hettinger | a3e1e4c | 2003-03-06 23:54:28 +0000 | [diff] [blame] | 1830 | if (deflt) { |
| 1831 | Py_INCREF(deflt); |
| 1832 | return deflt; |
| 1833 | } |
Guido van Rossum | e027d98 | 2002-04-12 15:11:59 +0000 | [diff] [blame] | 1834 | PyErr_SetString(PyExc_KeyError, |
| 1835 | "pop(): dictionary is empty"); |
| 1836 | return NULL; |
| 1837 | } |
| 1838 | if (!PyString_CheckExact(key) || |
| 1839 | (hash = ((PyStringObject *) key)->ob_shash) == -1) { |
| 1840 | hash = PyObject_Hash(key); |
| 1841 | if (hash == -1) |
| 1842 | return NULL; |
| 1843 | } |
| 1844 | ep = (mp->ma_lookup)(mp, key, hash); |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 1845 | if (ep == NULL) |
| 1846 | return NULL; |
Guido van Rossum | e027d98 | 2002-04-12 15:11:59 +0000 | [diff] [blame] | 1847 | if (ep->me_value == NULL) { |
Raymond Hettinger | a3e1e4c | 2003-03-06 23:54:28 +0000 | [diff] [blame] | 1848 | if (deflt) { |
| 1849 | Py_INCREF(deflt); |
| 1850 | return deflt; |
| 1851 | } |
Georg Brandl | b9f4ad3 | 2006-10-29 18:31:42 +0000 | [diff] [blame] | 1852 | set_key_error(key); |
Guido van Rossum | e027d98 | 2002-04-12 15:11:59 +0000 | [diff] [blame] | 1853 | return NULL; |
| 1854 | } |
| 1855 | old_key = ep->me_key; |
| 1856 | Py_INCREF(dummy); |
| 1857 | ep->me_key = dummy; |
| 1858 | old_value = ep->me_value; |
| 1859 | ep->me_value = NULL; |
| 1860 | mp->ma_used--; |
| 1861 | Py_DECREF(old_key); |
| 1862 | return old_value; |
| 1863 | } |
| 1864 | |
| 1865 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1866 | dict_popitem(PyDictObject *mp) |
Guido van Rossum | ba6ab84 | 2000-12-12 22:02:18 +0000 | [diff] [blame] | 1867 | { |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 1868 | Py_ssize_t i = 0; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1869 | PyDictEntry *ep; |
Guido van Rossum | ba6ab84 | 2000-12-12 22:02:18 +0000 | [diff] [blame] | 1870 | PyObject *res; |
| 1871 | |
Tim Peters | f4b33f6 | 2001-06-02 05:42:29 +0000 | [diff] [blame] | 1872 | /* Allocate the result tuple before checking the size. Believe it |
| 1873 | * or not, this allocation could trigger a garbage collection which |
| 1874 | * could empty the dict, so if we checked the size first and that |
| 1875 | * happened, the result would be an infinite loop (searching for an |
| 1876 | * entry that no longer exists). Note that the usual popitem() |
| 1877 | * idiom is "while d: k, v = d.popitem()". so needing to throw the |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 1878 | * tuple away if the dict *is* empty isn't a significant |
Tim Peters | f4b33f6 | 2001-06-02 05:42:29 +0000 | [diff] [blame] | 1879 | * inefficiency -- possible, but unlikely in practice. |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1880 | */ |
| 1881 | res = PyTuple_New(2); |
| 1882 | if (res == NULL) |
| 1883 | return NULL; |
Guido van Rossum | e04eaec | 2001-04-16 00:02:32 +0000 | [diff] [blame] | 1884 | if (mp->ma_used == 0) { |
| 1885 | Py_DECREF(res); |
| 1886 | PyErr_SetString(PyExc_KeyError, |
| 1887 | "popitem(): dictionary is empty"); |
| 1888 | return NULL; |
| 1889 | } |
Guido van Rossum | ba6ab84 | 2000-12-12 22:02:18 +0000 | [diff] [blame] | 1890 | /* Set ep to "the first" dict entry with a value. We abuse the hash |
| 1891 | * field of slot 0 to hold a search finger: |
| 1892 | * If slot 0 has a value, use slot 0. |
| 1893 | * Else slot 0 is being used to hold a search finger, |
Neal Norwitz | d3881b0 | 2006-05-30 04:25:05 +0000 | [diff] [blame] | 1894 | * and we use its hash value as the first index to look. |
Guido van Rossum | ba6ab84 | 2000-12-12 22:02:18 +0000 | [diff] [blame] | 1895 | */ |
| 1896 | ep = &mp->ma_table[0]; |
| 1897 | if (ep->me_value == NULL) { |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 1898 | i = ep->me_hash; |
Tim Peters | dea48ec | 2001-05-22 20:40:22 +0000 | [diff] [blame] | 1899 | /* The hash field may be a real hash value, or it may be a |
| 1900 | * legit search finger, or it may be a once-legit search |
| 1901 | * finger that's out of bounds now because it wrapped around |
| 1902 | * 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] | 1903 | */ |
Tim Peters | afb6ae8 | 2001-06-04 21:00:21 +0000 | [diff] [blame] | 1904 | if (i > mp->ma_mask || i < 1) |
Guido van Rossum | ba6ab84 | 2000-12-12 22:02:18 +0000 | [diff] [blame] | 1905 | i = 1; /* skip slot 0 */ |
| 1906 | while ((ep = &mp->ma_table[i])->me_value == NULL) { |
| 1907 | i++; |
Tim Peters | afb6ae8 | 2001-06-04 21:00:21 +0000 | [diff] [blame] | 1908 | if (i > mp->ma_mask) |
Guido van Rossum | ba6ab84 | 2000-12-12 22:02:18 +0000 | [diff] [blame] | 1909 | i = 1; |
| 1910 | } |
| 1911 | } |
Guido van Rossum | a4dd011 | 2001-04-15 22:16:26 +0000 | [diff] [blame] | 1912 | PyTuple_SET_ITEM(res, 0, ep->me_key); |
| 1913 | PyTuple_SET_ITEM(res, 1, ep->me_value); |
| 1914 | Py_INCREF(dummy); |
| 1915 | ep->me_key = dummy; |
| 1916 | ep->me_value = NULL; |
| 1917 | mp->ma_used--; |
| 1918 | assert(mp->ma_table[0].me_value == NULL); |
| 1919 | 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] | 1920 | return res; |
| 1921 | } |
| 1922 | |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 1923 | static int |
| 1924 | dict_traverse(PyObject *op, visitproc visit, void *arg) |
| 1925 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1926 | Py_ssize_t i = 0; |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 1927 | PyObject *pk; |
| 1928 | PyObject *pv; |
| 1929 | |
| 1930 | while (PyDict_Next(op, &i, &pk, &pv)) { |
Thomas Wouters | c6e5506 | 2006-04-15 21:47:09 +0000 | [diff] [blame] | 1931 | Py_VISIT(pk); |
| 1932 | Py_VISIT(pv); |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 1933 | } |
| 1934 | return 0; |
| 1935 | } |
| 1936 | |
| 1937 | static int |
| 1938 | dict_tp_clear(PyObject *op) |
| 1939 | { |
| 1940 | PyDict_Clear(op); |
| 1941 | return 0; |
| 1942 | } |
| 1943 | |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 1944 | |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 1945 | extern PyTypeObject PyDictIterKey_Type; /* Forward */ |
| 1946 | extern PyTypeObject PyDictIterValue_Type; /* Forward */ |
| 1947 | extern PyTypeObject PyDictIterItem_Type; /* Forward */ |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1948 | static PyObject *dictiter_new(PyDictObject *, PyTypeObject *); |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 1949 | |
| 1950 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1951 | dict_iterkeys(PyDictObject *dict) |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 1952 | { |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 1953 | return dictiter_new(dict, &PyDictIterKey_Type); |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 1954 | } |
| 1955 | |
| 1956 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1957 | dict_itervalues(PyDictObject *dict) |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 1958 | { |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 1959 | return dictiter_new(dict, &PyDictIterValue_Type); |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 1960 | } |
| 1961 | |
| 1962 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 1963 | dict_iteritems(PyDictObject *dict) |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 1964 | { |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 1965 | return dictiter_new(dict, &PyDictIterItem_Type); |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 1966 | } |
| 1967 | |
| 1968 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1969 | PyDoc_STRVAR(has_key__doc__, |
Raymond Hettinger | 574aa32 | 2003-09-01 22:12:08 +0000 | [diff] [blame] | 1970 | "D.has_key(k) -> True if D has a key k, else False"); |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 1971 | |
Raymond Hettinger | 8f5cdaa | 2003-12-13 11:26:12 +0000 | [diff] [blame] | 1972 | PyDoc_STRVAR(contains__doc__, |
| 1973 | "D.__contains__(k) -> True if D has a key k, else False"); |
| 1974 | |
| 1975 | PyDoc_STRVAR(getitem__doc__, "x.__getitem__(y) <==> x[y]"); |
| 1976 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1977 | PyDoc_STRVAR(get__doc__, |
Guido van Rossum | efae886 | 2002-09-04 11:29:45 +0000 | [diff] [blame] | 1978 | "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] | 1979 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1980 | PyDoc_STRVAR(setdefault_doc__, |
Guido van Rossum | efae886 | 2002-09-04 11:29:45 +0000 | [diff] [blame] | 1981 | "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] | 1982 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1983 | PyDoc_STRVAR(pop__doc__, |
Raymond Hettinger | a3e1e4c | 2003-03-06 23:54:28 +0000 | [diff] [blame] | 1984 | "D.pop(k[,d]) -> v, remove specified key and return the corresponding value\n\ |
| 1985 | 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] | 1986 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1987 | PyDoc_STRVAR(popitem__doc__, |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 1988 | "D.popitem() -> (k, v), remove and return some (key, value) pair as a\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1989 | 2-tuple; but raise KeyError if D is empty"); |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 1990 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1991 | PyDoc_STRVAR(keys__doc__, |
| 1992 | "D.keys() -> list of D's keys"); |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 1993 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1994 | PyDoc_STRVAR(items__doc__, |
| 1995 | "D.items() -> list of D's (key, value) pairs, as 2-tuples"); |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 1996 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1997 | PyDoc_STRVAR(values__doc__, |
| 1998 | "D.values() -> list of D's values"); |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 1999 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2000 | PyDoc_STRVAR(update__doc__, |
Walter Dörwald | d70ad8a | 2004-05-28 20:59:21 +0000 | [diff] [blame] | 2001 | "D.update(E, **F) -> None. Update D from E and F: for k in E: D[k] = E[k]\n\ |
| 2002 | (if E has keys else: for (k, v) in E: D[k] = v) then: for k in F: D[k] = F[k]"); |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2003 | |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 2004 | PyDoc_STRVAR(fromkeys__doc__, |
| 2005 | "dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.\n\ |
| 2006 | v defaults to None."); |
| 2007 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2008 | PyDoc_STRVAR(clear__doc__, |
| 2009 | "D.clear() -> None. Remove all items from D."); |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2010 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2011 | PyDoc_STRVAR(copy__doc__, |
| 2012 | "D.copy() -> a shallow copy of D"); |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2013 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2014 | PyDoc_STRVAR(iterkeys__doc__, |
| 2015 | "D.iterkeys() -> an iterator over the keys of D"); |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 2016 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2017 | PyDoc_STRVAR(itervalues__doc__, |
| 2018 | "D.itervalues() -> an iterator over the values of D"); |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 2019 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2020 | PyDoc_STRVAR(iteritems__doc__, |
| 2021 | "D.iteritems() -> an iterator over the (key, value) items of D"); |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 2022 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2023 | static PyMethodDef mapp_methods[] = { |
Neal Norwitz | c792629 | 2007-05-23 06:57:35 +0000 | [diff] [blame] | 2024 | {"__contains__",(PyCFunction)dict_contains, METH_O | METH_COEXIST, |
Raymond Hettinger | 8f5cdaa | 2003-12-13 11:26:12 +0000 | [diff] [blame] | 2025 | contains__doc__}, |
Raymond Hettinger | 0c66967 | 2003-12-13 13:31:55 +0000 | [diff] [blame] | 2026 | {"__getitem__", (PyCFunction)dict_subscript, METH_O | METH_COEXIST, |
Raymond Hettinger | 8f5cdaa | 2003-12-13 11:26:12 +0000 | [diff] [blame] | 2027 | getitem__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2028 | {"has_key", (PyCFunction)dict_has_key, METH_O, |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2029 | has_key__doc__}, |
| 2030 | {"get", (PyCFunction)dict_get, METH_VARARGS, |
| 2031 | get__doc__}, |
| 2032 | {"setdefault", (PyCFunction)dict_setdefault, METH_VARARGS, |
| 2033 | setdefault_doc__}, |
Raymond Hettinger | a3e1e4c | 2003-03-06 23:54:28 +0000 | [diff] [blame] | 2034 | {"pop", (PyCFunction)dict_pop, METH_VARARGS, |
Just van Rossum | a797d81 | 2002-11-23 09:45:04 +0000 | [diff] [blame] | 2035 | pop__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2036 | {"popitem", (PyCFunction)dict_popitem, METH_NOARGS, |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2037 | popitem__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2038 | {"keys", (PyCFunction)dict_keys, METH_NOARGS, |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2039 | keys__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2040 | {"items", (PyCFunction)dict_items, METH_NOARGS, |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2041 | items__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2042 | {"values", (PyCFunction)dict_values, METH_NOARGS, |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2043 | values__doc__}, |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 2044 | {"update", (PyCFunction)dict_update, METH_VARARGS | METH_KEYWORDS, |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2045 | update__doc__}, |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 2046 | {"fromkeys", (PyCFunction)dict_fromkeys, METH_VARARGS | METH_CLASS, |
| 2047 | fromkeys__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2048 | {"clear", (PyCFunction)dict_clear, METH_NOARGS, |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2049 | clear__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2050 | {"copy", (PyCFunction)dict_copy, METH_NOARGS, |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2051 | copy__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2052 | {"iterkeys", (PyCFunction)dict_iterkeys, METH_NOARGS, |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 2053 | iterkeys__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2054 | {"itervalues", (PyCFunction)dict_itervalues, METH_NOARGS, |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 2055 | itervalues__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2056 | {"iteritems", (PyCFunction)dict_iteritems, METH_NOARGS, |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 2057 | iteritems__doc__}, |
Tim Peters | f7f88b1 | 2000-12-13 23:18:45 +0000 | [diff] [blame] | 2058 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2059 | }; |
| 2060 | |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 2061 | /* 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] | 2062 | int |
| 2063 | PyDict_Contains(PyObject *op, PyObject *key) |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 2064 | { |
| 2065 | long hash; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 2066 | PyDictObject *mp = (PyDictObject *)op; |
| 2067 | PyDictEntry *ep; |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 2068 | |
Tim Peters | 0ab085c | 2001-09-14 00:25:33 +0000 | [diff] [blame] | 2069 | if (!PyString_CheckExact(key) || |
Tim Peters | 1f7df35 | 2002-03-29 03:29:08 +0000 | [diff] [blame] | 2070 | (hash = ((PyStringObject *) key)->ob_shash) == -1) { |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 2071 | hash = PyObject_Hash(key); |
| 2072 | if (hash == -1) |
| 2073 | return -1; |
| 2074 | } |
Armin Rigo | 35f6d36 | 2006-06-01 13:19:12 +0000 | [diff] [blame] | 2075 | ep = (mp->ma_lookup)(mp, key, hash); |
Tim Peters | d770ebd | 2006-06-01 15:50:44 +0000 | [diff] [blame] | 2076 | return ep == NULL ? -1 : (ep->me_value != NULL); |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 2077 | } |
| 2078 | |
Raymond Hettinger | d6fc72a | 2007-02-19 02:03:19 +0000 | [diff] [blame] | 2079 | /* Internal version of PyDict_Contains used when the hash value is already known */ |
| 2080 | int |
| 2081 | _PyDict_Contains(PyObject *op, PyObject *key, long hash) |
| 2082 | { |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 2083 | PyDictObject *mp = (PyDictObject *)op; |
| 2084 | PyDictEntry *ep; |
Raymond Hettinger | d6fc72a | 2007-02-19 02:03:19 +0000 | [diff] [blame] | 2085 | |
| 2086 | ep = (mp->ma_lookup)(mp, key, hash); |
| 2087 | return ep == NULL ? -1 : (ep->me_value != NULL); |
| 2088 | } |
| 2089 | |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 2090 | /* Hack to implement "key in dict" */ |
| 2091 | static PySequenceMethods dict_as_sequence = { |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 2092 | 0, /* sq_length */ |
| 2093 | 0, /* sq_concat */ |
| 2094 | 0, /* sq_repeat */ |
| 2095 | 0, /* sq_item */ |
| 2096 | 0, /* sq_slice */ |
| 2097 | 0, /* sq_ass_item */ |
| 2098 | 0, /* sq_ass_slice */ |
| 2099 | PyDict_Contains, /* sq_contains */ |
| 2100 | 0, /* sq_inplace_concat */ |
| 2101 | 0, /* sq_inplace_repeat */ |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 2102 | }; |
| 2103 | |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 2104 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2105 | dict_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2106 | { |
| 2107 | PyObject *self; |
| 2108 | |
| 2109 | assert(type != NULL && type->tp_alloc != NULL); |
| 2110 | self = type->tp_alloc(type, 0); |
| 2111 | if (self != NULL) { |
| 2112 | PyDictObject *d = (PyDictObject *)self; |
| 2113 | /* It's guaranteed that tp->alloc zeroed out the struct. */ |
| 2114 | assert(d->ma_table == NULL && d->ma_fill == 0 && d->ma_used == 0); |
| 2115 | INIT_NONZERO_DICT_SLOTS(d); |
| 2116 | d->ma_lookup = lookdict_string; |
| 2117 | #ifdef SHOW_CONVERSION_COUNTS |
| 2118 | ++created; |
| 2119 | #endif |
| 2120 | } |
| 2121 | return self; |
| 2122 | } |
| 2123 | |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 2124 | static int |
| 2125 | dict_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 2126 | { |
Raymond Hettinger | 31017ae | 2004-03-04 08:25:44 +0000 | [diff] [blame] | 2127 | return dict_update_common(self, args, kwds, "dict"); |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 2128 | } |
| 2129 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2130 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 2131 | dict_iter(PyDictObject *dict) |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 2132 | { |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2133 | return dictiter_new(dict, &PyDictIterKey_Type); |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 2134 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2135 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2136 | PyDoc_STRVAR(dictionary_doc, |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 2137 | "dict() -> new empty dictionary.\n" |
| 2138 | "dict(mapping) -> new dictionary initialized from a mapping object's\n" |
Tim Peters | 1fc240e | 2001-10-26 05:06:50 +0000 | [diff] [blame] | 2139 | " (key, value) pairs.\n" |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 2140 | "dict(seq) -> new dictionary initialized as if via:\n" |
Tim Peters | 4d85953 | 2001-10-27 18:27:48 +0000 | [diff] [blame] | 2141 | " d = {}\n" |
| 2142 | " for k, v in seq:\n" |
Just van Rossum | a797d81 | 2002-11-23 09:45:04 +0000 | [diff] [blame] | 2143 | " d[k] = v\n" |
| 2144 | "dict(**kwargs) -> new dictionary initialized with the name=value pairs\n" |
| 2145 | " in the keyword argument list. For example: dict(one=1, two=2)"); |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 2146 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2147 | PyTypeObject PyDict_Type = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 2148 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Tim Peters | a427a2b | 2001-10-29 22:25:45 +0000 | [diff] [blame] | 2149 | "dict", |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 2150 | sizeof(PyDictObject), |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2151 | 0, |
Guido van Rossum | b932420 | 2001-01-18 00:39:02 +0000 | [diff] [blame] | 2152 | (destructor)dict_dealloc, /* tp_dealloc */ |
| 2153 | (printfunc)dict_print, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2154 | 0, /* tp_getattr */ |
Guido van Rossum | b932420 | 2001-01-18 00:39:02 +0000 | [diff] [blame] | 2155 | 0, /* tp_setattr */ |
Tim Peters | 4fa58bf | 2001-05-10 21:45:19 +0000 | [diff] [blame] | 2156 | (cmpfunc)dict_compare, /* tp_compare */ |
Guido van Rossum | b932420 | 2001-01-18 00:39:02 +0000 | [diff] [blame] | 2157 | (reprfunc)dict_repr, /* tp_repr */ |
| 2158 | 0, /* tp_as_number */ |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 2159 | &dict_as_sequence, /* tp_as_sequence */ |
Guido van Rossum | b932420 | 2001-01-18 00:39:02 +0000 | [diff] [blame] | 2160 | &dict_as_mapping, /* tp_as_mapping */ |
Guido van Rossum | 64c06e3 | 2007-11-22 00:55:51 +0000 | [diff] [blame] | 2161 | 0, /* tp_hash */ |
Guido van Rossum | b932420 | 2001-01-18 00:39:02 +0000 | [diff] [blame] | 2162 | 0, /* tp_call */ |
| 2163 | 0, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2164 | PyObject_GenericGetAttr, /* tp_getattro */ |
Guido van Rossum | b932420 | 2001-01-18 00:39:02 +0000 | [diff] [blame] | 2165 | 0, /* tp_setattro */ |
| 2166 | 0, /* tp_as_buffer */ |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 2167 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
Neal Norwitz | ee3a1b5 | 2007-02-25 19:44:48 +0000 | [diff] [blame] | 2168 | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DICT_SUBCLASS, /* tp_flags */ |
Tim Peters | 25786c0 | 2001-09-02 08:22:48 +0000 | [diff] [blame] | 2169 | dictionary_doc, /* tp_doc */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 2170 | dict_traverse, /* tp_traverse */ |
| 2171 | dict_tp_clear, /* tp_clear */ |
Tim Peters | e63415e | 2001-05-08 04:38:29 +0000 | [diff] [blame] | 2172 | dict_richcompare, /* tp_richcompare */ |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2173 | 0, /* tp_weaklistoffset */ |
Guido van Rossum | 09e563a | 2001-05-01 12:10:21 +0000 | [diff] [blame] | 2174 | (getiterfunc)dict_iter, /* tp_iter */ |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2175 | 0, /* tp_iternext */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2176 | mapp_methods, /* tp_methods */ |
| 2177 | 0, /* tp_members */ |
| 2178 | 0, /* tp_getset */ |
| 2179 | 0, /* tp_base */ |
| 2180 | 0, /* tp_dict */ |
| 2181 | 0, /* tp_descr_get */ |
| 2182 | 0, /* tp_descr_set */ |
| 2183 | 0, /* tp_dictoffset */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 2184 | dict_init, /* tp_init */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2185 | PyType_GenericAlloc, /* tp_alloc */ |
| 2186 | dict_new, /* tp_new */ |
Neil Schemenauer | 6189b89 | 2002-04-12 02:43:00 +0000 | [diff] [blame] | 2187 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2188 | }; |
| 2189 | |
Guido van Rossum | 3cca245 | 1997-05-16 14:23:33 +0000 | [diff] [blame] | 2190 | /* For backward compatibility with old dictionary interface */ |
| 2191 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2192 | PyObject * |
Martin v. Löwis | 32b4a1b | 2002-12-11 13:21:12 +0000 | [diff] [blame] | 2193 | PyDict_GetItemString(PyObject *v, const char *key) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2194 | { |
Guido van Rossum | 3cca245 | 1997-05-16 14:23:33 +0000 | [diff] [blame] | 2195 | PyObject *kv, *rv; |
| 2196 | kv = PyString_FromString(key); |
| 2197 | if (kv == NULL) |
| 2198 | return NULL; |
Guido van Rossum | 3cca245 | 1997-05-16 14:23:33 +0000 | [diff] [blame] | 2199 | rv = PyDict_GetItem(v, kv); |
| 2200 | Py_DECREF(kv); |
| 2201 | return rv; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2202 | } |
| 2203 | |
| 2204 | int |
Martin v. Löwis | 32b4a1b | 2002-12-11 13:21:12 +0000 | [diff] [blame] | 2205 | PyDict_SetItemString(PyObject *v, const char *key, PyObject *item) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2206 | { |
Guido van Rossum | 3cca245 | 1997-05-16 14:23:33 +0000 | [diff] [blame] | 2207 | PyObject *kv; |
| 2208 | int err; |
| 2209 | kv = PyString_FromString(key); |
| 2210 | if (kv == NULL) |
Guido van Rossum | 037b220 | 1997-05-20 18:35:19 +0000 | [diff] [blame] | 2211 | return -1; |
Guido van Rossum | 4f3bf1e | 1997-09-29 23:31:11 +0000 | [diff] [blame] | 2212 | PyString_InternInPlace(&kv); /* XXX Should we really? */ |
Guido van Rossum | 3cca245 | 1997-05-16 14:23:33 +0000 | [diff] [blame] | 2213 | err = PyDict_SetItem(v, kv, item); |
| 2214 | Py_DECREF(kv); |
| 2215 | return err; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2216 | } |
| 2217 | |
| 2218 | int |
Martin v. Löwis | 32b4a1b | 2002-12-11 13:21:12 +0000 | [diff] [blame] | 2219 | PyDict_DelItemString(PyObject *v, const char *key) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2220 | { |
Guido van Rossum | 3cca245 | 1997-05-16 14:23:33 +0000 | [diff] [blame] | 2221 | PyObject *kv; |
| 2222 | int err; |
| 2223 | kv = PyString_FromString(key); |
| 2224 | if (kv == NULL) |
Guido van Rossum | 037b220 | 1997-05-20 18:35:19 +0000 | [diff] [blame] | 2225 | return -1; |
Guido van Rossum | 3cca245 | 1997-05-16 14:23:33 +0000 | [diff] [blame] | 2226 | err = PyDict_DelItem(v, kv); |
| 2227 | Py_DECREF(kv); |
| 2228 | return err; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 2229 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2230 | |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2231 | /* Dictionary iterator types */ |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2232 | |
| 2233 | typedef struct { |
| 2234 | PyObject_HEAD |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 2235 | PyDictObject *di_dict; /* Set to NULL when iterator is exhausted */ |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 2236 | Py_ssize_t di_used; |
| 2237 | Py_ssize_t di_pos; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2238 | PyObject* di_result; /* reusable result tuple for iteritems */ |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 2239 | Py_ssize_t len; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2240 | } dictiterobject; |
| 2241 | |
| 2242 | static PyObject * |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 2243 | dictiter_new(PyDictObject *dict, PyTypeObject *itertype) |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2244 | { |
| 2245 | dictiterobject *di; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2246 | di = PyObject_New(dictiterobject, itertype); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2247 | if (di == NULL) |
| 2248 | return NULL; |
| 2249 | Py_INCREF(dict); |
| 2250 | di->di_dict = dict; |
Guido van Rossum | b1f35bf | 2001-05-02 15:13:44 +0000 | [diff] [blame] | 2251 | di->di_used = dict->ma_used; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2252 | di->di_pos = 0; |
Raymond Hettinger | 0ce6dc8 | 2004-03-18 08:38:00 +0000 | [diff] [blame] | 2253 | di->len = dict->ma_used; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2254 | if (itertype == &PyDictIterItem_Type) { |
| 2255 | di->di_result = PyTuple_Pack(2, Py_None, Py_None); |
| 2256 | if (di->di_result == NULL) { |
| 2257 | Py_DECREF(di); |
| 2258 | return NULL; |
| 2259 | } |
| 2260 | } |
| 2261 | else |
| 2262 | di->di_result = NULL; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2263 | return (PyObject *)di; |
| 2264 | } |
| 2265 | |
| 2266 | static void |
| 2267 | dictiter_dealloc(dictiterobject *di) |
| 2268 | { |
Guido van Rossum | 2147df7 | 2002-07-16 20:30:22 +0000 | [diff] [blame] | 2269 | Py_XDECREF(di->di_dict); |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2270 | Py_XDECREF(di->di_result); |
Neil Schemenauer | 6189b89 | 2002-04-12 02:43:00 +0000 | [diff] [blame] | 2271 | PyObject_Del(di); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2272 | } |
| 2273 | |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 2274 | static PyObject * |
Raymond Hettinger | 0ce6dc8 | 2004-03-18 08:38:00 +0000 | [diff] [blame] | 2275 | dictiter_len(dictiterobject *di) |
| 2276 | { |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 2277 | Py_ssize_t len = 0; |
Raymond Hettinger | 7892b1c | 2004-04-12 18:10:01 +0000 | [diff] [blame] | 2278 | 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] | 2279 | len = di->len; |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 2280 | return PyInt_FromSize_t(len); |
Raymond Hettinger | 0ce6dc8 | 2004-03-18 08:38:00 +0000 | [diff] [blame] | 2281 | } |
| 2282 | |
Armin Rigo | f5b3e36 | 2006-02-11 21:32:43 +0000 | [diff] [blame] | 2283 | PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list(it))."); |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 2284 | |
| 2285 | static PyMethodDef dictiter_methods[] = { |
Armin Rigo | f5b3e36 | 2006-02-11 21:32:43 +0000 | [diff] [blame] | 2286 | {"__length_hint__", (PyCFunction)dictiter_len, METH_NOARGS, length_hint_doc}, |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 2287 | {NULL, NULL} /* sentinel */ |
Raymond Hettinger | 0ce6dc8 | 2004-03-18 08:38:00 +0000 | [diff] [blame] | 2288 | }; |
| 2289 | |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2290 | static PyObject *dictiter_iternextkey(dictiterobject *di) |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2291 | { |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2292 | PyObject *key; |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 2293 | register Py_ssize_t i, mask; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 2294 | register PyDictEntry *ep; |
| 2295 | PyDictObject *d = di->di_dict; |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2296 | |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2297 | if (d == NULL) |
Guido van Rossum | 2147df7 | 2002-07-16 20:30:22 +0000 | [diff] [blame] | 2298 | return NULL; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2299 | assert (PyDict_Check(d)); |
Guido van Rossum | 2147df7 | 2002-07-16 20:30:22 +0000 | [diff] [blame] | 2300 | |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2301 | if (di->di_used != d->ma_used) { |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2302 | PyErr_SetString(PyExc_RuntimeError, |
| 2303 | "dictionary changed size during iteration"); |
Guido van Rossum | 2147df7 | 2002-07-16 20:30:22 +0000 | [diff] [blame] | 2304 | di->di_used = -1; /* Make this state sticky */ |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2305 | return NULL; |
| 2306 | } |
Guido van Rossum | 2147df7 | 2002-07-16 20:30:22 +0000 | [diff] [blame] | 2307 | |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2308 | i = di->di_pos; |
| 2309 | if (i < 0) |
| 2310 | goto fail; |
| 2311 | ep = d->ma_table; |
| 2312 | mask = d->ma_mask; |
| 2313 | while (i <= mask && ep[i].me_value == NULL) |
| 2314 | i++; |
| 2315 | di->di_pos = i+1; |
| 2316 | if (i > mask) |
| 2317 | goto fail; |
Raymond Hettinger | 0ce6dc8 | 2004-03-18 08:38:00 +0000 | [diff] [blame] | 2318 | di->len--; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2319 | key = ep[i].me_key; |
| 2320 | Py_INCREF(key); |
| 2321 | return key; |
| 2322 | |
| 2323 | fail: |
| 2324 | Py_DECREF(d); |
Guido van Rossum | 2147df7 | 2002-07-16 20:30:22 +0000 | [diff] [blame] | 2325 | di->di_dict = NULL; |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2326 | return NULL; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2327 | } |
| 2328 | |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2329 | PyTypeObject PyDictIterKey_Type = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 2330 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Raymond Hettinger | 0ce6dc8 | 2004-03-18 08:38:00 +0000 | [diff] [blame] | 2331 | "dictionary-keyiterator", /* tp_name */ |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2332 | sizeof(dictiterobject), /* tp_basicsize */ |
| 2333 | 0, /* tp_itemsize */ |
| 2334 | /* methods */ |
| 2335 | (destructor)dictiter_dealloc, /* tp_dealloc */ |
| 2336 | 0, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2337 | 0, /* tp_getattr */ |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2338 | 0, /* tp_setattr */ |
| 2339 | 0, /* tp_compare */ |
| 2340 | 0, /* tp_repr */ |
| 2341 | 0, /* tp_as_number */ |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 2342 | 0, /* tp_as_sequence */ |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2343 | 0, /* tp_as_mapping */ |
| 2344 | 0, /* tp_hash */ |
| 2345 | 0, /* tp_call */ |
| 2346 | 0, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2347 | PyObject_GenericGetAttr, /* tp_getattro */ |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2348 | 0, /* tp_setattro */ |
| 2349 | 0, /* tp_as_buffer */ |
| 2350 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 2351 | 0, /* tp_doc */ |
| 2352 | 0, /* tp_traverse */ |
| 2353 | 0, /* tp_clear */ |
| 2354 | 0, /* tp_richcompare */ |
| 2355 | 0, /* tp_weaklistoffset */ |
Raymond Hettinger | 1da1dbf | 2003-03-17 19:46:11 +0000 | [diff] [blame] | 2356 | PyObject_SelfIter, /* tp_iter */ |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2357 | (iternextfunc)dictiter_iternextkey, /* tp_iternext */ |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 2358 | dictiter_methods, /* tp_methods */ |
| 2359 | 0, |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2360 | }; |
| 2361 | |
| 2362 | static PyObject *dictiter_iternextvalue(dictiterobject *di) |
| 2363 | { |
| 2364 | PyObject *value; |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 2365 | register Py_ssize_t i, mask; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 2366 | register PyDictEntry *ep; |
| 2367 | PyDictObject *d = di->di_dict; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2368 | |
| 2369 | if (d == NULL) |
| 2370 | return NULL; |
| 2371 | assert (PyDict_Check(d)); |
| 2372 | |
| 2373 | if (di->di_used != d->ma_used) { |
| 2374 | PyErr_SetString(PyExc_RuntimeError, |
| 2375 | "dictionary changed size during iteration"); |
| 2376 | di->di_used = -1; /* Make this state sticky */ |
| 2377 | return NULL; |
| 2378 | } |
| 2379 | |
| 2380 | i = di->di_pos; |
Guido van Rossum | 09240f6 | 2004-03-20 19:11:58 +0000 | [diff] [blame] | 2381 | mask = d->ma_mask; |
| 2382 | if (i < 0 || i > mask) |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2383 | goto fail; |
| 2384 | ep = d->ma_table; |
Guido van Rossum | 09240f6 | 2004-03-20 19:11:58 +0000 | [diff] [blame] | 2385 | while ((value=ep[i].me_value) == NULL) { |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2386 | i++; |
Guido van Rossum | 09240f6 | 2004-03-20 19:11:58 +0000 | [diff] [blame] | 2387 | if (i > mask) |
| 2388 | goto fail; |
| 2389 | } |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2390 | di->di_pos = i+1; |
Raymond Hettinger | 0ce6dc8 | 2004-03-18 08:38:00 +0000 | [diff] [blame] | 2391 | di->len--; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2392 | Py_INCREF(value); |
| 2393 | return value; |
| 2394 | |
| 2395 | fail: |
| 2396 | Py_DECREF(d); |
| 2397 | di->di_dict = NULL; |
| 2398 | return NULL; |
| 2399 | } |
| 2400 | |
| 2401 | PyTypeObject PyDictIterValue_Type = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 2402 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2403 | "dictionary-valueiterator", /* tp_name */ |
| 2404 | sizeof(dictiterobject), /* tp_basicsize */ |
| 2405 | 0, /* tp_itemsize */ |
| 2406 | /* methods */ |
| 2407 | (destructor)dictiter_dealloc, /* tp_dealloc */ |
| 2408 | 0, /* tp_print */ |
| 2409 | 0, /* tp_getattr */ |
| 2410 | 0, /* tp_setattr */ |
| 2411 | 0, /* tp_compare */ |
| 2412 | 0, /* tp_repr */ |
| 2413 | 0, /* tp_as_number */ |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 2414 | 0, /* tp_as_sequence */ |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2415 | 0, /* tp_as_mapping */ |
| 2416 | 0, /* tp_hash */ |
| 2417 | 0, /* tp_call */ |
| 2418 | 0, /* tp_str */ |
| 2419 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2420 | 0, /* tp_setattro */ |
| 2421 | 0, /* tp_as_buffer */ |
| 2422 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 2423 | 0, /* tp_doc */ |
| 2424 | 0, /* tp_traverse */ |
| 2425 | 0, /* tp_clear */ |
| 2426 | 0, /* tp_richcompare */ |
| 2427 | 0, /* tp_weaklistoffset */ |
| 2428 | PyObject_SelfIter, /* tp_iter */ |
| 2429 | (iternextfunc)dictiter_iternextvalue, /* tp_iternext */ |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 2430 | dictiter_methods, /* tp_methods */ |
| 2431 | 0, |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2432 | }; |
| 2433 | |
| 2434 | static PyObject *dictiter_iternextitem(dictiterobject *di) |
| 2435 | { |
| 2436 | PyObject *key, *value, *result = di->di_result; |
Tim Peters | 9b10f7e | 2006-05-30 04:16:25 +0000 | [diff] [blame] | 2437 | register Py_ssize_t i, mask; |
Brett Cannon | 77ae87c | 2007-10-10 00:07:50 +0000 | [diff] [blame] | 2438 | register PyDictEntry *ep; |
| 2439 | PyDictObject *d = di->di_dict; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2440 | |
| 2441 | if (d == NULL) |
| 2442 | return NULL; |
| 2443 | assert (PyDict_Check(d)); |
| 2444 | |
| 2445 | if (di->di_used != d->ma_used) { |
| 2446 | PyErr_SetString(PyExc_RuntimeError, |
| 2447 | "dictionary changed size during iteration"); |
| 2448 | di->di_used = -1; /* Make this state sticky */ |
| 2449 | return NULL; |
| 2450 | } |
| 2451 | |
| 2452 | i = di->di_pos; |
| 2453 | if (i < 0) |
| 2454 | goto fail; |
| 2455 | ep = d->ma_table; |
| 2456 | mask = d->ma_mask; |
| 2457 | while (i <= mask && ep[i].me_value == NULL) |
| 2458 | i++; |
| 2459 | di->di_pos = i+1; |
| 2460 | if (i > mask) |
| 2461 | goto fail; |
| 2462 | |
| 2463 | if (result->ob_refcnt == 1) { |
| 2464 | Py_INCREF(result); |
| 2465 | Py_DECREF(PyTuple_GET_ITEM(result, 0)); |
| 2466 | Py_DECREF(PyTuple_GET_ITEM(result, 1)); |
| 2467 | } else { |
| 2468 | result = PyTuple_New(2); |
Tim Peters | 60b2996 | 2006-01-01 01:19:23 +0000 | [diff] [blame] | 2469 | if (result == NULL) |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2470 | return NULL; |
| 2471 | } |
Raymond Hettinger | 0ce6dc8 | 2004-03-18 08:38:00 +0000 | [diff] [blame] | 2472 | di->len--; |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2473 | key = ep[i].me_key; |
| 2474 | value = ep[i].me_value; |
| 2475 | Py_INCREF(key); |
| 2476 | Py_INCREF(value); |
| 2477 | PyTuple_SET_ITEM(result, 0, key); |
| 2478 | PyTuple_SET_ITEM(result, 1, value); |
| 2479 | return result; |
| 2480 | |
| 2481 | fail: |
| 2482 | Py_DECREF(d); |
| 2483 | di->di_dict = NULL; |
| 2484 | return NULL; |
| 2485 | } |
| 2486 | |
| 2487 | PyTypeObject PyDictIterItem_Type = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 2488 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2489 | "dictionary-itemiterator", /* tp_name */ |
| 2490 | sizeof(dictiterobject), /* tp_basicsize */ |
| 2491 | 0, /* tp_itemsize */ |
| 2492 | /* methods */ |
| 2493 | (destructor)dictiter_dealloc, /* tp_dealloc */ |
| 2494 | 0, /* tp_print */ |
| 2495 | 0, /* tp_getattr */ |
| 2496 | 0, /* tp_setattr */ |
| 2497 | 0, /* tp_compare */ |
| 2498 | 0, /* tp_repr */ |
| 2499 | 0, /* tp_as_number */ |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 2500 | 0, /* tp_as_sequence */ |
Raymond Hettinger | 019a148 | 2004-03-18 02:41:19 +0000 | [diff] [blame] | 2501 | 0, /* tp_as_mapping */ |
| 2502 | 0, /* tp_hash */ |
| 2503 | 0, /* tp_call */ |
| 2504 | 0, /* tp_str */ |
| 2505 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2506 | 0, /* tp_setattro */ |
| 2507 | 0, /* tp_as_buffer */ |
| 2508 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 2509 | 0, /* tp_doc */ |
| 2510 | 0, /* tp_traverse */ |
| 2511 | 0, /* tp_clear */ |
| 2512 | 0, /* tp_richcompare */ |
| 2513 | 0, /* tp_weaklistoffset */ |
| 2514 | PyObject_SelfIter, /* tp_iter */ |
| 2515 | (iternextfunc)dictiter_iternextitem, /* tp_iternext */ |
Raymond Hettinger | 6b27cda | 2005-09-24 21:23:05 +0000 | [diff] [blame] | 2516 | dictiter_methods, /* tp_methods */ |
| 2517 | 0, |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2518 | }; |