Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 6610ad9 | 1995-01-04 19:07:38 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 32 | /* Dictionaru object implementation using a hash table */ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 33 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 34 | #include "Python.h" |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 35 | |
| 36 | |
| 37 | /* |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 38 | * MINSIZE is the minimum size of a dictionary. |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 39 | */ |
| 40 | |
| 41 | #define MINSIZE 4 |
| 42 | |
| 43 | /* |
| 44 | Table of irreducible polynomials to efficiently cycle through |
| 45 | GF(2^n)-{0}, 2<=n<=30. |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 46 | */ |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 47 | static long polys[] = { |
Guido van Rossum | 9e5656c | 1997-01-29 04:45:16 +0000 | [diff] [blame] | 48 | 4 + 3, |
| 49 | 8 + 3, |
| 50 | 16 + 3, |
| 51 | 32 + 5, |
| 52 | 64 + 3, |
| 53 | 128 + 3, |
| 54 | 256 + 29, |
| 55 | 512 + 17, |
| 56 | 1024 + 9, |
| 57 | 2048 + 5, |
| 58 | 4096 + 83, |
| 59 | 8192 + 27, |
| 60 | 16384 + 43, |
| 61 | 32768 + 3, |
| 62 | 65536 + 45, |
| 63 | 131072 + 9, |
| 64 | 262144 + 39, |
| 65 | 524288 + 39, |
| 66 | 1048576 + 9, |
| 67 | 2097152 + 5, |
| 68 | 4194304 + 3, |
| 69 | 8388608 + 33, |
| 70 | 16777216 + 27, |
| 71 | 33554432 + 9, |
| 72 | 67108864 + 71, |
| 73 | 134217728 + 39, |
| 74 | 268435456 + 9, |
| 75 | 536870912 + 5, |
| 76 | 1073741824 + 83, |
| 77 | 0 |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | /* Object used as dummy key to fill deleted entries */ |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 81 | static PyObject *dummy; /* Initialized by first call to newdictobject() */ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 82 | |
| 83 | /* |
| 84 | Invariant for entries: when in use, de_value is not NULL and de_key is |
| 85 | not NULL and not dummy; when not in use, de_value is NULL and de_key |
| 86 | is either NULL or dummy. A dummy key value cannot be replaced by |
| 87 | NULL, since otherwise other keys may be lost. |
| 88 | */ |
| 89 | typedef struct { |
| 90 | long me_hash; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 91 | PyObject *me_key; |
| 92 | PyObject *me_value; |
Guido van Rossum | 3648884 | 1997-04-11 19:14:07 +0000 | [diff] [blame] | 93 | #ifdef USE_CACHE_ALIGNED |
| 94 | long aligner; |
| 95 | #endif |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 96 | } dictentry; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 97 | |
| 98 | /* |
| 99 | To ensure the lookup algorithm terminates, the table size must be a |
| 100 | prime number and there must be at least one NULL key in the table. |
| 101 | The value ma_fill is the number of non-NULL keys; ma_used is the number |
| 102 | of non-NULL, non-dummy keys. |
| 103 | To avoid slowing down lookups on a near-full table, we resize the table |
| 104 | when it is more than half filled. |
| 105 | */ |
| 106 | typedef struct { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 107 | PyObject_HEAD |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 108 | int ma_fill; |
| 109 | int ma_used; |
| 110 | int ma_size; |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 111 | int ma_poly; |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 112 | dictentry *ma_table; |
| 113 | } dictobject; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 114 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 115 | PyObject * |
| 116 | PyDict_New() |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 117 | { |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 118 | register dictobject *mp; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 119 | if (dummy == NULL) { /* Auto-initialize dummy */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 120 | dummy = PyString_FromString("<dummy key>"); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 121 | if (dummy == NULL) |
| 122 | return NULL; |
| 123 | } |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 124 | mp = PyObject_NEW(dictobject, &PyDict_Type); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 125 | if (mp == NULL) |
| 126 | return NULL; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 127 | mp->ma_size = 0; |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 128 | mp->ma_poly = 0; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 129 | mp->ma_table = NULL; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 130 | mp->ma_fill = 0; |
| 131 | mp->ma_used = 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 132 | return (PyObject *)mp; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /* |
| 136 | The basic lookup function used by all operations. |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 137 | 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] | 138 | Open addressing is preferred over chaining since the link overhead for |
| 139 | chaining would be substantial (100% with typical malloc overhead). |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 140 | However, instead of going through the table at constant steps, we cycle |
| 141 | through the values of GF(2^n)-{0}. This avoids modulo computations, being |
| 142 | much cheaper on RISC machines, without leading to clustering. |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 143 | |
| 144 | First a 32-bit hash value, 'sum', is computed from the key string. |
| 145 | The first character is added an extra time shifted by 8 to avoid hashing |
| 146 | single-character keys (often heavily used variables) too close together. |
| 147 | All arithmetic on sum should ignore overflow. |
| 148 | |
| 149 | The initial probe index is then computed as sum mod the table size. |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 150 | Subsequent probe indices use the values of x^i in GF(2^n) as an offset, |
| 151 | where x is a root. The initial value is derived from sum, too. |
| 152 | |
| 153 | (This version is due to Reimer Behrends, some ideas are also due to |
Guido van Rossum | fd7a0b8 | 1997-08-18 21:52:47 +0000 | [diff] [blame] | 154 | Jyrki Alakuijala and Vladimir Marangozov.) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 155 | */ |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 156 | static dictentry *lookdict Py_PROTO((dictobject *, PyObject *, long)); |
| 157 | static dictentry * |
| 158 | lookdict(mp, key, hash) |
| 159 | dictobject *mp; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 160 | PyObject *key; |
Guido van Rossum | fd7a0b8 | 1997-08-18 21:52:47 +0000 | [diff] [blame] | 161 | register long hash; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 162 | { |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 163 | register int i; |
| 164 | register unsigned incr; |
Guido van Rossum | fd7a0b8 | 1997-08-18 21:52:47 +0000 | [diff] [blame] | 165 | register dictentry *freeslot; |
Guido van Rossum | 2095d24 | 1997-04-09 19:41:24 +0000 | [diff] [blame] | 166 | register unsigned int mask = mp->ma_size-1; |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 167 | dictentry *ep0 = mp->ma_table; |
| 168 | register dictentry *ep; |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 169 | /* We must come up with (i, incr) such that 0 <= i < ma_size |
| 170 | and 0 < incr < ma_size and both are a function of hash */ |
Guido van Rossum | fd7a0b8 | 1997-08-18 21:52:47 +0000 | [diff] [blame] | 171 | i = (~hash) & mask; |
| 172 | /* We use ~hash instead of hash, as degenerate hash functions, such |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 173 | as for ints <sigh>, can have lots of leading zeros. It's not |
| 174 | really a performance risk, but better safe than sorry. */ |
Guido van Rossum | efb4609 | 1997-01-29 15:53:56 +0000 | [diff] [blame] | 175 | ep = &ep0[i]; |
Guido van Rossum | c1c7b1a | 1998-10-06 16:01:14 +0000 | [diff] [blame] | 176 | if (ep->me_key == NULL || ep->me_key == key) |
Guido van Rossum | 7d18159 | 1997-01-16 21:06:45 +0000 | [diff] [blame] | 177 | return ep; |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 178 | if (ep->me_key == dummy) |
Guido van Rossum | 7d18159 | 1997-01-16 21:06:45 +0000 | [diff] [blame] | 179 | freeslot = ep; |
Guido van Rossum | fd7a0b8 | 1997-08-18 21:52:47 +0000 | [diff] [blame] | 180 | else { |
Guido van Rossum | c1c7b1a | 1998-10-06 16:01:14 +0000 | [diff] [blame] | 181 | if (ep->me_hash == hash && |
| 182 | PyObject_Compare(ep->me_key, key) == 0) |
Guido van Rossum | fd7a0b8 | 1997-08-18 21:52:47 +0000 | [diff] [blame] | 183 | { |
| 184 | return ep; |
| 185 | } |
| 186 | freeslot = NULL; |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 187 | } |
Guido van Rossum | 5b2121b | 1997-05-23 00:01:41 +0000 | [diff] [blame] | 188 | /* XXX What if PyObject_Compare returned an exception? */ |
Guido van Rossum | efb4609 | 1997-01-29 15:53:56 +0000 | [diff] [blame] | 189 | /* Derive incr from sum, just to make it more arbitrary. Note that |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 190 | incr must not be 0, or we will get into an infinite loop.*/ |
Guido van Rossum | fd7a0b8 | 1997-08-18 21:52:47 +0000 | [diff] [blame] | 191 | incr = (hash ^ ((unsigned long)hash >> 3)) & mask; |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 192 | if (!incr) |
| 193 | incr = mask; |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 194 | for (;;) { |
Guido van Rossum | efb4609 | 1997-01-29 15:53:56 +0000 | [diff] [blame] | 195 | ep = &ep0[(i+incr)&mask]; |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 196 | if (ep->me_key == NULL) { |
| 197 | if (freeslot != NULL) |
| 198 | return freeslot; |
| 199 | else |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 200 | return ep; |
| 201 | } |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 202 | if (ep->me_key == dummy) { |
| 203 | if (freeslot == NULL) |
| 204 | freeslot = ep; |
| 205 | } |
| 206 | else if (ep->me_key == key || |
| 207 | (ep->me_hash == hash && |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 208 | PyObject_Compare(ep->me_key, key) == 0)) { |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 209 | return ep; |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 210 | } |
Guido van Rossum | 5b2121b | 1997-05-23 00:01:41 +0000 | [diff] [blame] | 211 | /* XXX What if PyObject_Compare returned an exception? */ |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 212 | /* Cycle through GF(2^n)-{0} */ |
| 213 | incr = incr << 1; |
| 214 | if (incr > mask) |
Guido van Rossum | f05fc71 | 1998-11-16 22:46:30 +0000 | [diff] [blame] | 215 | incr ^= mp->ma_poly; /* This will implicitely clear |
| 216 | the highest bit */ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
| 220 | /* |
| 221 | Internal routine to insert a new item into the table. |
| 222 | Used both by the internal resize routine and by the public insert routine. |
| 223 | Eats a reference to key and one to value. |
| 224 | */ |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 225 | static void insertdict |
| 226 | Py_PROTO((dictobject *, PyObject *, long, PyObject *)); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 227 | static void |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 228 | insertdict(mp, key, hash, value) |
| 229 | register dictobject *mp; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 230 | PyObject *key; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 231 | long hash; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 232 | PyObject *value; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 233 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 234 | PyObject *old_value; |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 235 | register dictentry *ep; |
| 236 | ep = lookdict(mp, key, hash); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 237 | if (ep->me_value != NULL) { |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 238 | old_value = ep->me_value; |
| 239 | ep->me_value = value; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 240 | Py_DECREF(old_value); /* which **CAN** re-enter */ |
| 241 | Py_DECREF(key); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 242 | } |
| 243 | else { |
| 244 | if (ep->me_key == NULL) |
| 245 | mp->ma_fill++; |
| 246 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 247 | Py_DECREF(ep->me_key); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 248 | ep->me_key = key; |
| 249 | ep->me_hash = hash; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 250 | ep->me_value = value; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 251 | mp->ma_used++; |
| 252 | } |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | /* |
| 256 | Restructure the table by allocating a new table and reinserting all |
| 257 | items again. When entries have been deleted, the new table may |
| 258 | actually be smaller than the old one. |
| 259 | */ |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 260 | static int dictresize Py_PROTO((dictobject *, int)); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 261 | static int |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 262 | dictresize(mp, minused) |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 263 | dictobject *mp; |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 264 | int minused; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 265 | { |
| 266 | register int oldsize = mp->ma_size; |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 267 | register int newsize, newpoly; |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 268 | register dictentry *oldtable = mp->ma_table; |
| 269 | register dictentry *newtable; |
| 270 | register dictentry *ep; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 271 | register int i; |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 272 | for (i = 0, newsize = MINSIZE; ; i++, newsize <<= 1) { |
| 273 | if (i > sizeof(polys)/sizeof(polys[0])) { |
| 274 | /* Ran out of polynomials */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 275 | PyErr_NoMemory(); |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 276 | return -1; |
| 277 | } |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 278 | if (newsize > minused) { |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 279 | newpoly = polys[i]; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 280 | break; |
| 281 | } |
| 282 | } |
Guido van Rossum | 0fd0033 | 1998-07-16 15:06:13 +0000 | [diff] [blame] | 283 | newtable = (dictentry *) malloc(sizeof(dictentry) * newsize); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 284 | if (newtable == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 285 | PyErr_NoMemory(); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 286 | return -1; |
| 287 | } |
Guido van Rossum | 0fd0033 | 1998-07-16 15:06:13 +0000 | [diff] [blame] | 288 | memset(newtable, '\0', sizeof(dictentry) * newsize); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 289 | mp->ma_size = newsize; |
Guido van Rossum | 16e93a8 | 1997-01-28 00:00:11 +0000 | [diff] [blame] | 290 | mp->ma_poly = newpoly; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 291 | mp->ma_table = newtable; |
| 292 | mp->ma_fill = 0; |
| 293 | mp->ma_used = 0; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 294 | |
| 295 | /* Make two passes, so we can avoid decrefs |
| 296 | (and possible side effects) till the table is copied */ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 297 | for (i = 0, ep = oldtable; i < oldsize; i++, ep++) { |
| 298 | if (ep->me_value != NULL) |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 299 | insertdict(mp,ep->me_key,ep->me_hash,ep->me_value); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 300 | } |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 301 | for (i = 0, ep = oldtable; i < oldsize; i++, ep++) { |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 302 | if (ep->me_value == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 303 | Py_XDECREF(ep->me_key); |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 304 | } |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 307 | PyMem_XDEL(oldtable); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 308 | return 0; |
| 309 | } |
| 310 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 311 | PyObject * |
| 312 | PyDict_GetItem(op, key) |
| 313 | PyObject *op; |
| 314 | PyObject *key; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 315 | { |
| 316 | long hash; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 317 | if (!PyDict_Check(op)) { |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 318 | return NULL; |
| 319 | } |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 320 | if (((dictobject *)op)->ma_table == NULL) |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 321 | return NULL; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 322 | #ifdef CACHE_HASH |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 323 | if (!PyString_Check(key) || |
| 324 | (hash = ((PyStringObject *) key)->ob_shash) == -1) |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 325 | #endif |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 326 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 327 | hash = PyObject_Hash(key); |
Guido van Rossum | 474b19e | 1998-05-14 01:00:51 +0000 | [diff] [blame] | 328 | if (hash == -1) { |
| 329 | PyErr_Clear(); |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 330 | return NULL; |
Guido van Rossum | 474b19e | 1998-05-14 01:00:51 +0000 | [diff] [blame] | 331 | } |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 332 | } |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 333 | return lookdict((dictobject *)op, key, hash) -> me_value; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 337 | PyDict_SetItem(op, key, value) |
| 338 | register PyObject *op; |
| 339 | PyObject *key; |
| 340 | PyObject *value; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 341 | { |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 342 | register dictobject *mp; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 343 | register long hash; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 344 | if (!PyDict_Check(op)) { |
| 345 | PyErr_BadInternalCall(); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 346 | return -1; |
| 347 | } |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 348 | mp = (dictobject *)op; |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 349 | #ifdef CACHE_HASH |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 350 | if (PyString_Check(key)) { |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 351 | #ifdef INTERN_STRINGS |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 352 | if (((PyStringObject *)key)->ob_sinterned != NULL) { |
| 353 | key = ((PyStringObject *)key)->ob_sinterned; |
| 354 | hash = ((PyStringObject *)key)->ob_shash; |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 355 | } |
| 356 | else |
| 357 | #endif |
| 358 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 359 | hash = ((PyStringObject *)key)->ob_shash; |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 360 | if (hash == -1) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 361 | hash = PyObject_Hash(key); |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | else |
| 365 | #endif |
| 366 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 367 | hash = PyObject_Hash(key); |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 368 | if (hash == -1) |
| 369 | return -1; |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 370 | } |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 371 | /* if fill >= 2/3 size, double in size */ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 372 | if (mp->ma_fill*3 >= mp->ma_size*2) { |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 373 | if (dictresize(mp, mp->ma_used*2) != 0) { |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 374 | if (mp->ma_fill+1 > mp->ma_size) |
| 375 | return -1; |
| 376 | } |
| 377 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 378 | Py_INCREF(value); |
| 379 | Py_INCREF(key); |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 380 | insertdict(mp, key, hash, value); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 385 | PyDict_DelItem(op, key) |
| 386 | PyObject *op; |
| 387 | PyObject *key; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 388 | { |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 389 | register dictobject *mp; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 390 | register long hash; |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 391 | register dictentry *ep; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 392 | PyObject *old_value, *old_key; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 393 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 394 | if (!PyDict_Check(op)) { |
| 395 | PyErr_BadInternalCall(); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 396 | return -1; |
| 397 | } |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 398 | #ifdef CACHE_HASH |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 399 | if (!PyString_Check(key) || |
| 400 | (hash = ((PyStringObject *) key)->ob_shash) == -1) |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 401 | #endif |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 402 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 403 | hash = PyObject_Hash(key); |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 404 | if (hash == -1) |
| 405 | return -1; |
| 406 | } |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 407 | mp = (dictobject *)op; |
| 408 | if (((dictobject *)op)->ma_table == NULL) |
Guido van Rossum | efc8713 | 1995-01-02 19:42:39 +0000 | [diff] [blame] | 409 | goto empty; |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 410 | ep = lookdict(mp, key, hash); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 411 | if (ep->me_value == NULL) { |
Guido van Rossum | efc8713 | 1995-01-02 19:42:39 +0000 | [diff] [blame] | 412 | empty: |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 413 | PyErr_SetObject(PyExc_KeyError, key); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 414 | return -1; |
| 415 | } |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 416 | old_key = ep->me_key; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 417 | Py_INCREF(dummy); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 418 | ep->me_key = dummy; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 419 | old_value = ep->me_value; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 420 | ep->me_value = NULL; |
| 421 | mp->ma_used--; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 422 | Py_DECREF(old_value); |
| 423 | Py_DECREF(old_key); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 424 | return 0; |
| 425 | } |
| 426 | |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 427 | void |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 428 | PyDict_Clear(op) |
| 429 | PyObject *op; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 430 | { |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 431 | int i, n; |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 432 | register dictentry *table; |
| 433 | dictobject *mp; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 434 | if (!PyDict_Check(op)) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 435 | return; |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 436 | mp = (dictobject *)op; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 437 | table = mp->ma_table; |
| 438 | if (table == NULL) |
| 439 | return; |
| 440 | n = mp->ma_size; |
| 441 | mp->ma_size = mp->ma_used = mp->ma_fill = 0; |
| 442 | mp->ma_table = NULL; |
| 443 | for (i = 0; i < n; i++) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 444 | Py_XDECREF(table[i].me_key); |
| 445 | Py_XDECREF(table[i].me_value); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 446 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 447 | PyMem_DEL(table); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 450 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 451 | PyDict_Next(op, ppos, pkey, pvalue) |
| 452 | PyObject *op; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 453 | int *ppos; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 454 | PyObject **pkey; |
| 455 | PyObject **pvalue; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 456 | { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 457 | int i; |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 458 | register dictobject *mp; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 459 | if (!PyDict_Check(op)) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 460 | return 0; |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 461 | mp = (dictobject *)op; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 462 | i = *ppos; |
| 463 | if (i < 0) |
| 464 | return 0; |
| 465 | while (i < mp->ma_size && mp->ma_table[i].me_value == NULL) |
| 466 | i++; |
| 467 | *ppos = i+1; |
| 468 | if (i >= mp->ma_size) |
| 469 | return 0; |
| 470 | if (pkey) |
| 471 | *pkey = mp->ma_table[i].me_key; |
| 472 | if (pvalue) |
| 473 | *pvalue = mp->ma_table[i].me_value; |
| 474 | return 1; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | /* Methods */ |
| 478 | |
| 479 | static void |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 480 | dict_dealloc(mp) |
| 481 | register dictobject *mp; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 482 | { |
| 483 | register int i; |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 484 | register dictentry *ep; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 485 | for (i = 0, ep = mp->ma_table; i < mp->ma_size; i++, ep++) { |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 486 | if (ep->me_key != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 487 | Py_DECREF(ep->me_key); |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 488 | } |
| 489 | if (ep->me_value != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 490 | Py_DECREF(ep->me_value); |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 491 | } |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 492 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 493 | PyMem_XDEL(mp->ma_table); |
| 494 | PyMem_DEL(mp); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | static int |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 498 | dict_print(mp, fp, flags) |
| 499 | register dictobject *mp; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 500 | register FILE *fp; |
| 501 | register int flags; |
| 502 | { |
| 503 | register int i; |
| 504 | register int any; |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 505 | register dictentry *ep; |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 506 | |
| 507 | i = Py_ReprEnter((PyObject*)mp); |
| 508 | if (i != 0) { |
| 509 | if (i < 0) |
| 510 | return i; |
| 511 | fprintf(fp, "{...}"); |
| 512 | return 0; |
| 513 | } |
| 514 | |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 515 | fprintf(fp, "{"); |
| 516 | any = 0; |
| 517 | for (i = 0, ep = mp->ma_table; i < mp->ma_size; i++, ep++) { |
| 518 | if (ep->me_value != NULL) { |
| 519 | if (any++ > 0) |
| 520 | fprintf(fp, ", "); |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 521 | if (PyObject_Print((PyObject *)ep->me_key, fp, 0)!=0) { |
| 522 | Py_ReprLeave((PyObject*)mp); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 523 | return -1; |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 524 | } |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 525 | fprintf(fp, ": "); |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 526 | if (PyObject_Print(ep->me_value, fp, 0) != 0) { |
| 527 | Py_ReprLeave((PyObject*)mp); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 528 | return -1; |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 529 | } |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | fprintf(fp, "}"); |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 533 | Py_ReprLeave((PyObject*)mp); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 534 | return 0; |
| 535 | } |
| 536 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 537 | static PyObject * |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 538 | dict_repr(mp) |
| 539 | dictobject *mp; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 540 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 541 | auto PyObject *v; |
| 542 | PyObject *sepa, *colon; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 543 | register int i; |
| 544 | register int any; |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 545 | register dictentry *ep; |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 546 | |
| 547 | i = Py_ReprEnter((PyObject*)mp); |
| 548 | if (i != 0) { |
| 549 | if (i > 0) |
| 550 | return PyString_FromString("{...}"); |
| 551 | return NULL; |
| 552 | } |
| 553 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 554 | v = PyString_FromString("{"); |
| 555 | sepa = PyString_FromString(", "); |
| 556 | colon = PyString_FromString(": "); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 557 | any = 0; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 558 | for (i = 0, ep = mp->ma_table; i < mp->ma_size && v; i++, ep++) { |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 559 | if (ep->me_value != NULL) { |
| 560 | if (any++) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 561 | PyString_Concat(&v, sepa); |
| 562 | PyString_ConcatAndDel(&v, PyObject_Repr(ep->me_key)); |
| 563 | PyString_Concat(&v, colon); |
| 564 | PyString_ConcatAndDel(&v, PyObject_Repr(ep->me_value)); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 565 | } |
| 566 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 567 | PyString_ConcatAndDel(&v, PyString_FromString("}")); |
Guido van Rossum | 255443b | 1998-04-10 22:47:14 +0000 | [diff] [blame] | 568 | Py_ReprLeave((PyObject*)mp); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 569 | Py_XDECREF(sepa); |
| 570 | Py_XDECREF(colon); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 571 | return v; |
| 572 | } |
| 573 | |
| 574 | static int |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 575 | dict_length(mp) |
| 576 | dictobject *mp; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 577 | { |
| 578 | return mp->ma_used; |
| 579 | } |
| 580 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 581 | static PyObject * |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 582 | dict_subscript(mp, key) |
| 583 | dictobject *mp; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 584 | register PyObject *key; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 585 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 586 | PyObject *v; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 587 | long hash; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 588 | if (mp->ma_table == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 589 | PyErr_SetObject(PyExc_KeyError, key); |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 590 | return NULL; |
| 591 | } |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 592 | #ifdef CACHE_HASH |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 593 | if (!PyString_Check(key) || |
| 594 | (hash = ((PyStringObject *) key)->ob_shash) == -1) |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 595 | #endif |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 596 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 597 | hash = PyObject_Hash(key); |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 598 | if (hash == -1) |
| 599 | return NULL; |
| 600 | } |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 601 | v = lookdict(mp, key, hash) -> me_value; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 602 | if (v == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 603 | PyErr_SetObject(PyExc_KeyError, key); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 604 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 605 | Py_INCREF(v); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 606 | return v; |
| 607 | } |
| 608 | |
| 609 | static int |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 610 | dict_ass_sub(mp, v, w) |
| 611 | dictobject *mp; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 612 | PyObject *v, *w; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 613 | { |
| 614 | if (w == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 615 | return PyDict_DelItem((PyObject *)mp, v); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 616 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 617 | return PyDict_SetItem((PyObject *)mp, v, w); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 618 | } |
| 619 | |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 620 | static PyMappingMethods dict_as_mapping = { |
| 621 | (inquiry)dict_length, /*mp_length*/ |
| 622 | (binaryfunc)dict_subscript, /*mp_subscript*/ |
| 623 | (objobjargproc)dict_ass_sub, /*mp_ass_subscript*/ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 624 | }; |
| 625 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 626 | static PyObject * |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 627 | dict_keys(mp, args) |
| 628 | register dictobject *mp; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 629 | PyObject *args; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 630 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 631 | register PyObject *v; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 632 | register int i, j; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 633 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 634 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 635 | v = PyList_New(mp->ma_used); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 636 | if (v == NULL) |
| 637 | return NULL; |
| 638 | for (i = 0, j = 0; i < mp->ma_size; i++) { |
| 639 | if (mp->ma_table[i].me_value != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 640 | PyObject *key = mp->ma_table[i].me_key; |
| 641 | Py_INCREF(key); |
| 642 | PyList_SetItem(v, j, key); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 643 | j++; |
| 644 | } |
| 645 | } |
| 646 | return v; |
| 647 | } |
| 648 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 649 | static PyObject * |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 650 | dict_values(mp, args) |
| 651 | register dictobject *mp; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 652 | PyObject *args; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 653 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 654 | register PyObject *v; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 655 | register int i, j; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 656 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 657 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 658 | v = PyList_New(mp->ma_used); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 659 | if (v == NULL) |
| 660 | return NULL; |
| 661 | for (i = 0, j = 0; i < mp->ma_size; i++) { |
| 662 | if (mp->ma_table[i].me_value != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 663 | PyObject *value = mp->ma_table[i].me_value; |
| 664 | Py_INCREF(value); |
| 665 | PyList_SetItem(v, j, value); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 666 | j++; |
| 667 | } |
| 668 | } |
| 669 | return v; |
| 670 | } |
| 671 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 672 | static PyObject * |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 673 | dict_items(mp, args) |
| 674 | register dictobject *mp; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 675 | PyObject *args; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 676 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 677 | register PyObject *v; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 678 | register int i, j; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 679 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 680 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 681 | v = PyList_New(mp->ma_used); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 682 | if (v == NULL) |
| 683 | return NULL; |
| 684 | for (i = 0, j = 0; i < mp->ma_size; i++) { |
| 685 | if (mp->ma_table[i].me_value != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 686 | PyObject *key = mp->ma_table[i].me_key; |
| 687 | PyObject *value = mp->ma_table[i].me_value; |
| 688 | PyObject *item = PyTuple_New(2); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 689 | if (item == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 690 | Py_DECREF(v); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 691 | return NULL; |
| 692 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 693 | Py_INCREF(key); |
| 694 | PyTuple_SetItem(item, 0, key); |
| 695 | Py_INCREF(value); |
| 696 | PyTuple_SetItem(item, 1, value); |
| 697 | PyList_SetItem(v, j, item); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 698 | j++; |
| 699 | } |
| 700 | } |
| 701 | return v; |
| 702 | } |
| 703 | |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 704 | static PyObject * |
Guido van Rossum | a8d5131 | 1997-06-02 17:13:37 +0000 | [diff] [blame] | 705 | dict_update(mp, args) |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 706 | register dictobject *mp; |
| 707 | PyObject *args; |
| 708 | { |
| 709 | register int i; |
| 710 | dictobject *other; |
| 711 | dictentry *entry; |
| 712 | if (!PyArg_Parse(args, "O!", &PyDict_Type, &other)) |
| 713 | return NULL; |
| 714 | if (other == mp) |
Guido van Rossum | a8d5131 | 1997-06-02 17:13:37 +0000 | [diff] [blame] | 715 | goto done; /* a.update(a); nothing to do */ |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 716 | /* Do one big resize at the start, rather than incrementally |
| 717 | resizing as we insert new items. Expect that there will be |
| 718 | no (or few) overlapping keys. */ |
| 719 | if ((mp->ma_fill + other->ma_used)*3 >= mp->ma_size*2) { |
| 720 | if (dictresize(mp, (mp->ma_used + other->ma_used)*3/2) != 0) |
| 721 | return NULL; |
| 722 | } |
| 723 | for (i = 0; i < other->ma_size; i++) { |
| 724 | entry = &other->ma_table[i]; |
| 725 | if (entry->me_value != NULL) { |
| 726 | Py_INCREF(entry->me_key); |
| 727 | Py_INCREF(entry->me_value); |
| 728 | insertdict(mp, entry->me_key, entry->me_hash, |
| 729 | entry->me_value); |
| 730 | } |
| 731 | } |
| 732 | done: |
| 733 | Py_INCREF(Py_None); |
| 734 | return Py_None; |
| 735 | } |
| 736 | |
| 737 | static PyObject * |
| 738 | dict_copy(mp, args) |
| 739 | register dictobject *mp; |
| 740 | PyObject *args; |
| 741 | { |
| 742 | register int i; |
| 743 | dictobject *copy; |
| 744 | dictentry *entry; |
| 745 | if (!PyArg_Parse(args, "")) |
| 746 | return NULL; |
| 747 | copy = (dictobject *)PyDict_New(); |
| 748 | if (copy == NULL) |
| 749 | return NULL; |
| 750 | if (mp->ma_used > 0) { |
| 751 | if (dictresize(copy, mp->ma_used*3/2) != 0) |
| 752 | return NULL; |
| 753 | for (i = 0; i < mp->ma_size; i++) { |
| 754 | entry = &mp->ma_table[i]; |
| 755 | if (entry->me_value != NULL) { |
| 756 | Py_INCREF(entry->me_key); |
| 757 | Py_INCREF(entry->me_value); |
| 758 | insertdict(copy, entry->me_key, entry->me_hash, |
| 759 | entry->me_value); |
| 760 | } |
| 761 | } |
| 762 | } |
| 763 | return (PyObject *)copy; |
| 764 | } |
| 765 | |
Guido van Rossum | 4199fac | 1993-11-05 10:18:44 +0000 | [diff] [blame] | 766 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 767 | PyDict_Size(mp) |
| 768 | PyObject *mp; |
Guido van Rossum | 4199fac | 1993-11-05 10:18:44 +0000 | [diff] [blame] | 769 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 770 | if (mp == NULL || !PyDict_Check(mp)) { |
| 771 | PyErr_BadInternalCall(); |
Guido van Rossum | b376a4a | 1993-11-23 17:53:17 +0000 | [diff] [blame] | 772 | return 0; |
Guido van Rossum | 4199fac | 1993-11-05 10:18:44 +0000 | [diff] [blame] | 773 | } |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 774 | return ((dictobject *)mp)->ma_used; |
Guido van Rossum | 4199fac | 1993-11-05 10:18:44 +0000 | [diff] [blame] | 775 | } |
| 776 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 777 | PyObject * |
| 778 | PyDict_Keys(mp) |
| 779 | PyObject *mp; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 780 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 781 | if (mp == NULL || !PyDict_Check(mp)) { |
| 782 | PyErr_BadInternalCall(); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 783 | return NULL; |
| 784 | } |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 785 | return dict_keys((dictobject *)mp, (PyObject *)NULL); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 786 | } |
| 787 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 788 | PyObject * |
| 789 | PyDict_Values(mp) |
| 790 | PyObject *mp; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 791 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 792 | if (mp == NULL || !PyDict_Check(mp)) { |
| 793 | PyErr_BadInternalCall(); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 794 | return NULL; |
| 795 | } |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 796 | return dict_values((dictobject *)mp, (PyObject *)NULL); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 797 | } |
| 798 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 799 | PyObject * |
| 800 | PyDict_Items(mp) |
| 801 | PyObject *mp; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 802 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 803 | if (mp == NULL || !PyDict_Check(mp)) { |
| 804 | PyErr_BadInternalCall(); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 805 | return NULL; |
| 806 | } |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 807 | return dict_items((dictobject *)mp, (PyObject *)NULL); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 808 | } |
| 809 | |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 810 | #define NEWCMP |
| 811 | |
| 812 | #ifdef NEWCMP |
| 813 | |
| 814 | /* Subroutine which returns the smallest key in a for which b's value |
| 815 | is different or absent. The value is returned too, through the |
| 816 | pval argument. No reference counts are incremented. */ |
| 817 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 818 | static PyObject * |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 819 | characterize(a, b, pval) |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 820 | dictobject *a; |
| 821 | dictobject *b; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 822 | PyObject **pval; |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 823 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 824 | PyObject *diff = NULL; |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 825 | int i; |
| 826 | |
| 827 | *pval = NULL; |
| 828 | for (i = 0; i < a->ma_size; i++) { |
| 829 | if (a->ma_table[i].me_value != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 830 | PyObject *key = a->ma_table[i].me_key; |
| 831 | PyObject *aval, *bval; |
Guido van Rossum | 5b2121b | 1997-05-23 00:01:41 +0000 | [diff] [blame] | 832 | /* XXX What if PyObject_Compare raises an exception? */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 833 | if (diff != NULL && PyObject_Compare(key, diff) > 0) |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 834 | continue; |
| 835 | aval = a->ma_table[i].me_value; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 836 | bval = PyDict_GetItem((PyObject *)b, key); |
Guido van Rossum | 5b2121b | 1997-05-23 00:01:41 +0000 | [diff] [blame] | 837 | /* XXX What if PyObject_Compare raises an exception? */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 838 | if (bval == NULL || PyObject_Compare(aval, bval) != 0) |
| 839 | { |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 840 | diff = key; |
| 841 | *pval = aval; |
| 842 | } |
| 843 | } |
| 844 | } |
| 845 | return diff; |
| 846 | } |
| 847 | |
| 848 | static int |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 849 | dict_compare(a, b) |
| 850 | dictobject *a, *b; |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 851 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 852 | PyObject *adiff, *bdiff, *aval, *bval; |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 853 | int res; |
| 854 | |
| 855 | /* Compare lengths first */ |
| 856 | if (a->ma_used < b->ma_used) |
| 857 | return -1; /* a is shorter */ |
| 858 | else if (a->ma_used > b->ma_used) |
| 859 | return 1; /* b is shorter */ |
| 860 | /* Same length -- check all keys */ |
| 861 | adiff = characterize(a, b, &aval); |
Guido van Rossum | 5b2121b | 1997-05-23 00:01:41 +0000 | [diff] [blame] | 862 | if (PyErr_Occurred()) |
| 863 | return -1; |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 864 | if (adiff == NULL) |
| 865 | return 0; /* a is a subset with the same length */ |
| 866 | bdiff = characterize(b, a, &bval); |
Guido van Rossum | 5b2121b | 1997-05-23 00:01:41 +0000 | [diff] [blame] | 867 | if (PyErr_Occurred()) |
| 868 | return -1; |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 869 | /* bdiff == NULL would be impossible now */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 870 | res = PyObject_Compare(adiff, bdiff); |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 871 | if (res == 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 872 | res = PyObject_Compare(aval, bval); |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 873 | return res; |
| 874 | } |
| 875 | |
| 876 | #else /* !NEWCMP */ |
| 877 | |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 878 | static int |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 879 | dict_compare(a, b) |
| 880 | dictobject *a, *b; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 881 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 882 | PyObject *akeys, *bkeys; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 883 | int i, n, res; |
| 884 | if (a == b) |
| 885 | return 0; |
| 886 | if (a->ma_used == 0) { |
| 887 | if (b->ma_used != 0) |
| 888 | return -1; |
| 889 | else |
| 890 | return 0; |
| 891 | } |
| 892 | else { |
| 893 | if (b->ma_used == 0) |
| 894 | return 1; |
| 895 | } |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 896 | akeys = dict_keys(a, (PyObject *)NULL); |
| 897 | bkeys = dict_keys(b, (PyObject *)NULL); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 898 | if (akeys == NULL || bkeys == NULL) { |
| 899 | /* Oops, out of memory -- what to do? */ |
| 900 | /* For now, sort on address! */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 901 | Py_XDECREF(akeys); |
| 902 | Py_XDECREF(bkeys); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 903 | if (a < b) |
| 904 | return -1; |
| 905 | else |
| 906 | return 1; |
| 907 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 908 | PyList_Sort(akeys); |
| 909 | PyList_Sort(bkeys); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 910 | n = a->ma_used < b->ma_used ? a->ma_used : b->ma_used; /* smallest */ |
| 911 | res = 0; |
| 912 | for (i = 0; i < n; i++) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 913 | PyObject *akey, *bkey, *aval, *bval; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 914 | long ahash, bhash; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 915 | akey = PyList_GetItem(akeys, i); |
| 916 | bkey = PyList_GetItem(bkeys, i); |
| 917 | res = PyObject_Compare(akey, bkey); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 918 | if (res != 0) |
| 919 | break; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 920 | #ifdef CACHE_HASH |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 921 | if (!PyString_Check(akey) || |
| 922 | (ahash = ((PyStringObject *) akey)->ob_shash) == -1) |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 923 | #endif |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 924 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 925 | ahash = PyObject_Hash(akey); |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 926 | if (ahash == -1) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 927 | PyErr_Clear(); /* Don't want errors here */ |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 928 | } |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 929 | #ifdef CACHE_HASH |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 930 | if (!PyString_Check(bkey) || |
| 931 | (bhash = ((PyStringObject *) bkey)->ob_shash) == -1) |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 932 | #endif |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 933 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 934 | bhash = PyObject_Hash(bkey); |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 935 | if (bhash == -1) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 936 | PyErr_Clear(); /* Don't want errors here */ |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 937 | } |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 938 | aval = lookdict(a, akey, ahash) -> me_value; |
| 939 | bval = lookdict(b, bkey, bhash) -> me_value; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 940 | res = PyObject_Compare(aval, bval); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 941 | if (res != 0) |
| 942 | break; |
| 943 | } |
| 944 | if (res == 0) { |
| 945 | if (a->ma_used < b->ma_used) |
| 946 | res = -1; |
| 947 | else if (a->ma_used > b->ma_used) |
| 948 | res = 1; |
| 949 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 950 | Py_DECREF(akeys); |
| 951 | Py_DECREF(bkeys); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 952 | return res; |
| 953 | } |
| 954 | |
Guido van Rossum | a0a69b8 | 1996-12-05 21:55:55 +0000 | [diff] [blame] | 955 | #endif /* !NEWCMP */ |
| 956 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 957 | static PyObject * |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 958 | dict_has_key(mp, args) |
| 959 | register dictobject *mp; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 960 | PyObject *args; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 961 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 962 | PyObject *key; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 963 | long hash; |
| 964 | register long ok; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 965 | if (!PyArg_Parse(args, "O", &key)) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 966 | return NULL; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 967 | #ifdef CACHE_HASH |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 968 | if (!PyString_Check(key) || |
| 969 | (hash = ((PyStringObject *) key)->ob_shash) == -1) |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 970 | #endif |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 971 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 972 | hash = PyObject_Hash(key); |
Guido van Rossum | ca756f2 | 1997-01-23 19:39:29 +0000 | [diff] [blame] | 973 | if (hash == -1) |
| 974 | return NULL; |
| 975 | } |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 976 | ok = mp->ma_size != 0 && lookdict(mp, key, hash)->me_value != NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 977 | return PyInt_FromLong(ok); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 978 | } |
| 979 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 980 | static PyObject * |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 981 | dict_get(mp, args) |
| 982 | register dictobject *mp; |
| 983 | PyObject *args; |
| 984 | { |
| 985 | PyObject *key; |
Barry Warsaw | 320ac33 | 1997-10-20 17:26:25 +0000 | [diff] [blame] | 986 | PyObject *failobj = Py_None; |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 987 | PyObject *val = NULL; |
| 988 | long hash; |
| 989 | |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 990 | if (!PyArg_ParseTuple(args, "O|O", &key, &failobj)) |
| 991 | return NULL; |
Guido van Rossum | 6fcfa72 | 1997-10-20 20:10:00 +0000 | [diff] [blame] | 992 | if (mp->ma_table == NULL) |
| 993 | goto finally; |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 994 | |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 995 | #ifdef CACHE_HASH |
| 996 | if (!PyString_Check(key) || |
| 997 | (hash = ((PyStringObject *) key)->ob_shash) == -1) |
| 998 | #endif |
| 999 | { |
| 1000 | hash = PyObject_Hash(key); |
| 1001 | if (hash == -1) |
| 1002 | return NULL; |
| 1003 | } |
| 1004 | val = lookdict(mp, key, hash)->me_value; |
Barry Warsaw | 320ac33 | 1997-10-20 17:26:25 +0000 | [diff] [blame] | 1005 | |
Guido van Rossum | 6fcfa72 | 1997-10-20 20:10:00 +0000 | [diff] [blame] | 1006 | finally: |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 1007 | if (val == NULL) |
| 1008 | val = failobj; |
| 1009 | Py_INCREF(val); |
| 1010 | return val; |
| 1011 | } |
| 1012 | |
| 1013 | |
| 1014 | static PyObject * |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 1015 | dict_clear(mp, args) |
| 1016 | register dictobject *mp; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1017 | PyObject *args; |
Guido van Rossum | fb8f1ca | 1997-03-21 21:55:12 +0000 | [diff] [blame] | 1018 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1019 | if (!PyArg_NoArgs(args)) |
Guido van Rossum | fb8f1ca | 1997-03-21 21:55:12 +0000 | [diff] [blame] | 1020 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1021 | PyDict_Clear((PyObject *)mp); |
| 1022 | Py_INCREF(Py_None); |
| 1023 | return Py_None; |
Guido van Rossum | fb8f1ca | 1997-03-21 21:55:12 +0000 | [diff] [blame] | 1024 | } |
| 1025 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1026 | static PyMethodDef mapp_methods[] = { |
Guido van Rossum | 5d8123f | 1997-07-13 03:58:01 +0000 | [diff] [blame] | 1027 | {"has_key", (PyCFunction)dict_has_key}, |
| 1028 | {"keys", (PyCFunction)dict_keys}, |
| 1029 | {"items", (PyCFunction)dict_items}, |
| 1030 | {"values", (PyCFunction)dict_values}, |
Guido van Rossum | a8d5131 | 1997-06-02 17:13:37 +0000 | [diff] [blame] | 1031 | {"update", (PyCFunction)dict_update}, |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 1032 | {"clear", (PyCFunction)dict_clear}, |
Guido van Rossum | e3f5b9c | 1997-05-28 19:15:28 +0000 | [diff] [blame] | 1033 | {"copy", (PyCFunction)dict_copy}, |
Barry Warsaw | c38c5da | 1997-10-06 17:49:20 +0000 | [diff] [blame] | 1034 | {"get", (PyCFunction)dict_get, 1}, |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1035 | {NULL, NULL} /* sentinel */ |
| 1036 | }; |
| 1037 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1038 | static PyObject * |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 1039 | dict_getattr(mp, name) |
| 1040 | dictobject *mp; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1041 | char *name; |
| 1042 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1043 | return Py_FindMethod(mapp_methods, (PyObject *)mp, name); |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1044 | } |
| 1045 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1046 | PyTypeObject PyDict_Type = { |
| 1047 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1048 | 0, |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1049 | "dictionary", |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 1050 | sizeof(dictobject), |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1051 | 0, |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 1052 | (destructor)dict_dealloc, /*tp_dealloc*/ |
| 1053 | (printfunc)dict_print, /*tp_print*/ |
| 1054 | (getattrfunc)dict_getattr, /*tp_getattr*/ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1055 | 0, /*tp_setattr*/ |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 1056 | (cmpfunc)dict_compare, /*tp_compare*/ |
| 1057 | (reprfunc)dict_repr, /*tp_repr*/ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1058 | 0, /*tp_as_number*/ |
| 1059 | 0, /*tp_as_sequence*/ |
Guido van Rossum | a9e7a81 | 1997-05-13 21:02:11 +0000 | [diff] [blame] | 1060 | &dict_as_mapping, /*tp_as_mapping*/ |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1061 | }; |
| 1062 | |
Guido van Rossum | 3cca245 | 1997-05-16 14:23:33 +0000 | [diff] [blame] | 1063 | /* For backward compatibility with old dictionary interface */ |
| 1064 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1065 | PyObject * |
| 1066 | PyDict_GetItemString(v, key) |
| 1067 | PyObject *v; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1068 | char *key; |
| 1069 | { |
Guido van Rossum | 3cca245 | 1997-05-16 14:23:33 +0000 | [diff] [blame] | 1070 | PyObject *kv, *rv; |
| 1071 | kv = PyString_FromString(key); |
| 1072 | if (kv == NULL) |
| 1073 | return NULL; |
Guido van Rossum | 3cca245 | 1997-05-16 14:23:33 +0000 | [diff] [blame] | 1074 | rv = PyDict_GetItem(v, kv); |
| 1075 | Py_DECREF(kv); |
| 1076 | return rv; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1080 | PyDict_SetItemString(v, key, item) |
| 1081 | PyObject *v; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1082 | char *key; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1083 | PyObject *item; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1084 | { |
Guido van Rossum | 3cca245 | 1997-05-16 14:23:33 +0000 | [diff] [blame] | 1085 | PyObject *kv; |
| 1086 | int err; |
| 1087 | kv = PyString_FromString(key); |
| 1088 | if (kv == NULL) |
Guido van Rossum | 037b220 | 1997-05-20 18:35:19 +0000 | [diff] [blame] | 1089 | return -1; |
Guido van Rossum | 4f3bf1e | 1997-09-29 23:31:11 +0000 | [diff] [blame] | 1090 | PyString_InternInPlace(&kv); /* XXX Should we really? */ |
Guido van Rossum | 3cca245 | 1997-05-16 14:23:33 +0000 | [diff] [blame] | 1091 | err = PyDict_SetItem(v, kv, item); |
| 1092 | Py_DECREF(kv); |
| 1093 | return err; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1097 | PyDict_DelItemString(v, key) |
| 1098 | PyObject *v; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1099 | char *key; |
| 1100 | { |
Guido van Rossum | 3cca245 | 1997-05-16 14:23:33 +0000 | [diff] [blame] | 1101 | PyObject *kv; |
| 1102 | int err; |
| 1103 | kv = PyString_FromString(key); |
| 1104 | if (kv == NULL) |
Guido van Rossum | 037b220 | 1997-05-20 18:35:19 +0000 | [diff] [blame] | 1105 | return -1; |
Guido van Rossum | 3cca245 | 1997-05-16 14:23:33 +0000 | [diff] [blame] | 1106 | err = PyDict_DelItem(v, kv); |
| 1107 | Py_DECREF(kv); |
| 1108 | return err; |
Guido van Rossum | 4b1302b | 1993-03-27 18:11:32 +0000 | [diff] [blame] | 1109 | } |