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