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