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