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