Mercurial > cpython-lru_cache-weakref
annotate Objects/listobject.c @ 3720:b341def4eef8 legacy-trunk
Ignore latex intermediate products.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 26 Jun 1996 19:30:01 +0000 |
| parents | ff1faa4cc1df |
| children | 011cfe7490eb |
| rev | line source |
|---|---|
| 145 | 1 /*********************************************************** |
|
2059
866c81068dbd
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
2053
diff
changeset
|
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
|
866c81068dbd
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
2053
diff
changeset
|
3 The Netherlands. |
| 145 | 4 |
| 5 All Rights Reserved | |
| 6 | |
| 7 Permission to use, copy, modify, and distribute this software and its | |
| 8 documentation for any purpose and without fee is hereby granted, | |
| 9 provided that the above copyright notice appear in all copies and that | |
| 10 both that copyright notice and this permission notice appear in | |
| 11 supporting documentation, and that the names of Stichting Mathematisch | |
| 12 Centrum or CWI not be used in advertising or publicity pertaining to | |
| 13 distribution of the software without specific, written prior permission. | |
| 14 | |
| 15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO | |
| 16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
| 17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE | |
| 18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
| 19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
| 20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT | |
| 21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
| 22 | |
| 23 ******************************************************************/ | |
| 24 | |
| 6 | 25 /* List object implementation */ |
| 26 | |
| 86 | 27 #include "allobjects.h" |
|
645
c0e7b0b9b750
Include modsupport.h for getargs().
Guido van Rossum <guido@python.org>
parents:
511
diff
changeset
|
28 #include "modsupport.h" |
| 926 | 29 #include "ceval.h" |
|
1836
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
30 #ifdef STDC_HEADERS |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
31 #include <stddef.h> |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
32 #else |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
33 #include <sys/types.h> /* For size_t */ |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
34 #endif |
| 6 | 35 |
|
2257
ee4c954cd5df
round up list item counts to improve realloc performance
Guido van Rossum <guido@python.org>
parents:
2188
diff
changeset
|
36 #define ROUNDUP(n, block) ((((n)+(block)-1)/(block))*(block)) |
|
ee4c954cd5df
round up list item counts to improve realloc performance
Guido van Rossum <guido@python.org>
parents:
2188
diff
changeset
|
37 |
|
ee4c954cd5df
round up list item counts to improve realloc performance
Guido van Rossum <guido@python.org>
parents:
2188
diff
changeset
|
38 static int |
|
ee4c954cd5df
round up list item counts to improve realloc performance
Guido van Rossum <guido@python.org>
parents:
2188
diff
changeset
|
39 roundup(n) |
|
ee4c954cd5df
round up list item counts to improve realloc performance
Guido van Rossum <guido@python.org>
parents:
2188
diff
changeset
|
40 int n; |
|
ee4c954cd5df
round up list item counts to improve realloc performance
Guido van Rossum <guido@python.org>
parents:
2188
diff
changeset
|
41 { |
|
ee4c954cd5df
round up list item counts to improve realloc performance
Guido van Rossum <guido@python.org>
parents:
2188
diff
changeset
|
42 if (n < 500) |
|
ee4c954cd5df
round up list item counts to improve realloc performance
Guido van Rossum <guido@python.org>
parents:
2188
diff
changeset
|
43 return ROUNDUP(n, 10); |
|
ee4c954cd5df
round up list item counts to improve realloc performance
Guido van Rossum <guido@python.org>
parents:
2188
diff
changeset
|
44 else |
|
ee4c954cd5df
round up list item counts to improve realloc performance
Guido van Rossum <guido@python.org>
parents:
2188
diff
changeset
|
45 return ROUNDUP(n, 100); |
|
ee4c954cd5df
round up list item counts to improve realloc performance
Guido van Rossum <guido@python.org>
parents:
2188
diff
changeset
|
46 } |
|
ee4c954cd5df
round up list item counts to improve realloc performance
Guido van Rossum <guido@python.org>
parents:
2188
diff
changeset
|
47 |
|
ee4c954cd5df
round up list item counts to improve realloc performance
Guido van Rossum <guido@python.org>
parents:
2188
diff
changeset
|
48 #define NRESIZE(var, type, nitems) RESIZE(var, type, roundup(nitems)) |
|
ee4c954cd5df
round up list item counts to improve realloc performance
Guido van Rossum <guido@python.org>
parents:
2188
diff
changeset
|
49 |
| 6 | 50 object * |
| 51 newlistobject(size) | |
| 52 int size; | |
| 53 { | |
| 54 int i; | |
| 55 listobject *op; | |
|
1836
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
56 size_t nbytes; |
| 6 | 57 if (size < 0) { |
| 27 | 58 err_badcall(); |
| 6 | 59 return NULL; |
| 60 } | |
|
978
13c99eaf32a8
* renamed malloc.h mymalloc.h, and added MALLARG as the type of the
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
61 nbytes = size * sizeof(object *); |
|
13c99eaf32a8
* renamed malloc.h mymalloc.h, and added MALLARG as the type of the
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
62 /* Check for overflow */ |
|
13c99eaf32a8
* renamed malloc.h mymalloc.h, and added MALLARG as the type of the
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
63 if (nbytes / sizeof(object *) != size) { |
|
13c99eaf32a8
* renamed malloc.h mymalloc.h, and added MALLARG as the type of the
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
64 return err_nomem(); |
|
13c99eaf32a8
* renamed malloc.h mymalloc.h, and added MALLARG as the type of the
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
65 } |
| 6 | 66 op = (listobject *) malloc(sizeof(listobject)); |
| 67 if (op == NULL) { | |
| 27 | 68 return err_nomem(); |
| 6 | 69 } |
| 70 if (size <= 0) { | |
| 71 op->ob_item = NULL; | |
| 72 } | |
| 73 else { | |
|
978
13c99eaf32a8
* renamed malloc.h mymalloc.h, and added MALLARG as the type of the
Guido van Rossum <guido@python.org>
parents:
926
diff
changeset
|
74 op->ob_item = (object **) malloc(nbytes); |
| 6 | 75 if (op->ob_item == NULL) { |
| 76 free((ANY *)op); | |
| 27 | 77 return err_nomem(); |
| 6 | 78 } |
| 79 } | |
| 80 op->ob_type = &Listtype; | |
| 81 op->ob_size = size; | |
| 82 for (i = 0; i < size; i++) | |
| 83 op->ob_item[i] = NULL; | |
|
1347
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1330
diff
changeset
|
84 NEWREF(op); |
| 6 | 85 return (object *) op; |
| 86 } | |
| 87 | |
| 88 int | |
| 89 getlistsize(op) | |
| 90 object *op; | |
| 91 { | |
| 92 if (!is_listobject(op)) { | |
| 27 | 93 err_badcall(); |
| 6 | 94 return -1; |
| 95 } | |
| 96 else | |
| 97 return ((listobject *)op) -> ob_size; | |
| 98 } | |
| 99 | |
| 100 object * | |
| 101 getlistitem(op, i) | |
| 102 object *op; | |
| 103 int i; | |
| 104 { | |
| 105 if (!is_listobject(op)) { | |
| 27 | 106 err_badcall(); |
| 6 | 107 return NULL; |
| 108 } | |
| 109 if (i < 0 || i >= ((listobject *)op) -> ob_size) { | |
| 27 | 110 err_setstr(IndexError, "list index out of range"); |
| 6 | 111 return NULL; |
| 112 } | |
| 113 return ((listobject *)op) -> ob_item[i]; | |
| 114 } | |
| 115 | |
| 116 int | |
| 117 setlistitem(op, i, newitem) | |
| 118 register object *op; | |
| 119 register int i; | |
| 120 register object *newitem; | |
| 121 { | |
| 122 register object *olditem; | |
|
2504
425835b88099
a few peephole optimizations
Guido van Rossum <guido@python.org>
parents:
2397
diff
changeset
|
123 register object **p; |
| 6 | 124 if (!is_listobject(op)) { |
| 394 | 125 XDECREF(newitem); |
| 27 | 126 err_badcall(); |
| 127 return -1; | |
| 6 | 128 } |
| 129 if (i < 0 || i >= ((listobject *)op) -> ob_size) { | |
| 394 | 130 XDECREF(newitem); |
| 27 | 131 err_setstr(IndexError, "list assignment index out of range"); |
| 132 return -1; | |
| 6 | 133 } |
|
2504
425835b88099
a few peephole optimizations
Guido van Rossum <guido@python.org>
parents:
2397
diff
changeset
|
134 p = ((listobject *)op) -> ob_item + i; |
|
425835b88099
a few peephole optimizations
Guido van Rossum <guido@python.org>
parents:
2397
diff
changeset
|
135 olditem = *p; |
|
425835b88099
a few peephole optimizations
Guido van Rossum <guido@python.org>
parents:
2397
diff
changeset
|
136 *p = newitem; |
| 394 | 137 XDECREF(olditem); |
| 6 | 138 return 0; |
| 139 } | |
| 140 | |
| 141 static int | |
| 142 ins1(self, where, v) | |
| 143 listobject *self; | |
| 144 int where; | |
| 145 object *v; | |
| 146 { | |
| 147 int i; | |
| 148 object **items; | |
| 27 | 149 if (v == NULL) { |
| 150 err_badcall(); | |
| 151 return -1; | |
| 152 } | |
| 6 | 153 items = self->ob_item; |
|
2257
ee4c954cd5df
round up list item counts to improve realloc performance
Guido van Rossum <guido@python.org>
parents:
2188
diff
changeset
|
154 NRESIZE(items, object *, self->ob_size+1); |
| 27 | 155 if (items == NULL) { |
| 156 err_nomem(); | |
| 157 return -1; | |
| 158 } | |
| 6 | 159 if (where < 0) |
| 160 where = 0; | |
| 161 if (where > self->ob_size) | |
| 162 where = self->ob_size; | |
| 163 for (i = self->ob_size; --i >= where; ) | |
| 164 items[i+1] = items[i]; | |
| 165 INCREF(v); | |
| 166 items[where] = v; | |
| 167 self->ob_item = items; | |
| 168 self->ob_size++; | |
| 169 return 0; | |
| 170 } | |
| 171 | |
| 172 int | |
| 173 inslistitem(op, where, newitem) | |
| 174 object *op; | |
| 175 int where; | |
| 176 object *newitem; | |
| 177 { | |
| 27 | 178 if (!is_listobject(op)) { |
| 179 err_badcall(); | |
| 180 return -1; | |
| 181 } | |
| 6 | 182 return ins1((listobject *)op, where, newitem); |
| 183 } | |
| 184 | |
| 185 int | |
| 186 addlistitem(op, newitem) | |
| 187 object *op; | |
| 188 object *newitem; | |
| 189 { | |
| 27 | 190 if (!is_listobject(op)) { |
| 191 err_badcall(); | |
| 192 return -1; | |
| 193 } | |
| 6 | 194 return ins1((listobject *)op, |
| 195 (int) ((listobject *)op)->ob_size, newitem); | |
| 196 } | |
| 197 | |
| 198 /* Methods */ | |
| 199 | |
| 200 static void | |
| 201 list_dealloc(op) | |
| 202 listobject *op; | |
| 203 { | |
| 204 int i; | |
|
2188
6461ff0c4bee
Fix NULL dereference in case of out-of-memory condition
Jack Jansen <jack.jansen@cwi.nl>
parents:
2159
diff
changeset
|
205 if (op->ob_item != NULL) { |
|
6461ff0c4bee
Fix NULL dereference in case of out-of-memory condition
Jack Jansen <jack.jansen@cwi.nl>
parents:
2159
diff
changeset
|
206 for (i = 0; i < op->ob_size; i++) { |
|
6461ff0c4bee
Fix NULL dereference in case of out-of-memory condition
Jack Jansen <jack.jansen@cwi.nl>
parents:
2159
diff
changeset
|
207 XDECREF(op->ob_item[i]); |
|
6461ff0c4bee
Fix NULL dereference in case of out-of-memory condition
Jack Jansen <jack.jansen@cwi.nl>
parents:
2159
diff
changeset
|
208 } |
|
6461ff0c4bee
Fix NULL dereference in case of out-of-memory condition
Jack Jansen <jack.jansen@cwi.nl>
parents:
2159
diff
changeset
|
209 free((ANY *)op->ob_item); |
| 6 | 210 } |
| 211 free((ANY *)op); | |
| 212 } | |
| 213 | |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
205
diff
changeset
|
214 static int |
| 6 | 215 list_print(op, fp, flags) |
| 216 listobject *op; | |
| 217 FILE *fp; | |
| 218 int flags; | |
| 219 { | |
| 220 int i; | |
| 221 fprintf(fp, "["); | |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
205
diff
changeset
|
222 for (i = 0; i < op->ob_size; i++) { |
|
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
205
diff
changeset
|
223 if (i > 0) |
| 6 | 224 fprintf(fp, ", "); |
|
1405
90c5e2bb4da5
* posixmodule.c: added set{uid,gid}.
Guido van Rossum <guido@python.org>
parents:
1375
diff
changeset
|
225 if (printobject(op->ob_item[i], fp, 0) != 0) |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
205
diff
changeset
|
226 return -1; |
| 6 | 227 } |
| 228 fprintf(fp, "]"); | |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
205
diff
changeset
|
229 return 0; |
| 6 | 230 } |
| 231 | |
|
1313
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
232 static object * |
| 6 | 233 list_repr(v) |
| 234 listobject *v; | |
| 235 { | |
|
1836
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
236 object *s, *comma; |
| 6 | 237 int i; |
| 238 s = newstringobject("["); | |
| 239 comma = newstringobject(", "); | |
| 240 for (i = 0; i < v->ob_size && s != NULL; i++) { | |
| 241 if (i > 0) | |
| 242 joinstring(&s, comma); | |
|
1836
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
243 joinstring_decref(&s, reprobject(v->ob_item[i])); |
| 6 | 244 } |
|
1836
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
245 XDECREF(comma); |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
246 joinstring_decref(&s, newstringobject("]")); |
| 6 | 247 return s; |
| 248 } | |
| 249 | |
| 250 static int | |
| 251 list_compare(v, w) | |
| 252 listobject *v, *w; | |
| 253 { | |
| 254 int len = (v->ob_size < w->ob_size) ? v->ob_size : w->ob_size; | |
| 255 int i; | |
| 256 for (i = 0; i < len; i++) { | |
| 257 int cmp = cmpobject(v->ob_item[i], w->ob_item[i]); | |
| 258 if (cmp != 0) | |
| 259 return cmp; | |
| 260 } | |
| 261 return v->ob_size - w->ob_size; | |
| 262 } | |
| 263 | |
| 264 static int | |
| 265 list_length(a) | |
| 266 listobject *a; | |
| 267 { | |
| 268 return a->ob_size; | |
| 269 } | |
| 270 | |
| 271 static object * | |
| 272 list_item(a, i) | |
| 273 listobject *a; | |
| 274 int i; | |
| 275 { | |
| 276 if (i < 0 || i >= a->ob_size) { | |
| 27 | 277 err_setstr(IndexError, "list index out of range"); |
| 6 | 278 return NULL; |
| 279 } | |
| 280 INCREF(a->ob_item[i]); | |
| 281 return a->ob_item[i]; | |
| 282 } | |
| 283 | |
| 284 static object * | |
| 285 list_slice(a, ilow, ihigh) | |
| 286 listobject *a; | |
| 287 int ilow, ihigh; | |
| 288 { | |
| 289 listobject *np; | |
| 290 int i; | |
| 291 if (ilow < 0) | |
| 292 ilow = 0; | |
| 293 else if (ilow > a->ob_size) | |
| 294 ilow = a->ob_size; | |
| 295 if (ihigh < 0) | |
| 296 ihigh = 0; | |
| 297 if (ihigh < ilow) | |
| 298 ihigh = ilow; | |
| 299 else if (ihigh > a->ob_size) | |
| 300 ihigh = a->ob_size; | |
| 301 np = (listobject *) newlistobject(ihigh - ilow); | |
| 302 if (np == NULL) | |
| 303 return NULL; | |
| 304 for (i = ilow; i < ihigh; i++) { | |
| 305 object *v = a->ob_item[i]; | |
| 306 INCREF(v); | |
| 307 np->ob_item[i - ilow] = v; | |
| 308 } | |
| 309 return (object *)np; | |
| 310 } | |
| 311 | |
|
1313
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
312 object * |
|
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
313 getlistslice(a, ilow, ihigh) |
|
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
314 object *a; |
|
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
315 int ilow, ihigh; |
|
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
316 { |
|
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
317 if (!is_listobject(a)) { |
|
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
318 err_badcall(); |
|
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
319 return NULL; |
|
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
320 } |
|
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
321 return list_slice((listobject *)a, ilow, ihigh); |
|
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
322 } |
|
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
323 |
| 6 | 324 static object * |
| 325 list_concat(a, bb) | |
| 326 listobject *a; | |
| 327 object *bb; | |
| 328 { | |
| 329 int size; | |
| 330 int i; | |
| 331 listobject *np; | |
| 332 if (!is_listobject(bb)) { | |
| 27 | 333 err_badarg(); |
| 6 | 334 return NULL; |
| 335 } | |
| 336 #define b ((listobject *)bb) | |
| 337 size = a->ob_size + b->ob_size; | |
| 338 np = (listobject *) newlistobject(size); | |
| 339 if (np == NULL) { | |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
205
diff
changeset
|
340 return NULL; |
| 6 | 341 } |
| 342 for (i = 0; i < a->ob_size; i++) { | |
| 343 object *v = a->ob_item[i]; | |
| 344 INCREF(v); | |
| 345 np->ob_item[i] = v; | |
| 346 } | |
| 347 for (i = 0; i < b->ob_size; i++) { | |
| 348 object *v = b->ob_item[i]; | |
| 349 INCREF(v); | |
| 350 np->ob_item[i + a->ob_size] = v; | |
| 351 } | |
| 352 return (object *)np; | |
| 353 #undef b | |
| 354 } | |
| 355 | |
|
155
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
356 static object * |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
357 list_repeat(a, n) |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
358 listobject *a; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
359 int n; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
360 { |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
361 int i, j; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
362 int size; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
363 listobject *np; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
364 object **p; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
365 if (n < 0) |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
366 n = 0; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
367 size = a->ob_size * n; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
368 np = (listobject *) newlistobject(size); |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
369 if (np == NULL) |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
370 return NULL; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
371 p = np->ob_item; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
372 for (i = 0; i < n; i++) { |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
373 for (j = 0; j < a->ob_size; j++) { |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
374 *p = a->ob_item[j]; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
375 INCREF(*p); |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
376 p++; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
377 } |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
378 } |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
379 return (object *) np; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
380 } |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
381 |
| 6 | 382 static int |
| 383 list_ass_slice(a, ilow, ihigh, v) | |
| 384 listobject *a; | |
| 385 int ilow, ihigh; | |
| 386 object *v; | |
| 387 { | |
|
2143
7ae847b1b269
fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
388 /* Because [X]DECREF can recursively invoke list operations on |
|
7ae847b1b269
fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
389 this list, we must postpone all [X]DECREF activity until |
|
7ae847b1b269
fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
390 after the list is back in its canonical shape. Therefore |
|
7ae847b1b269
fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
391 we must allocate an additional array, 'recycle', into which |
|
7ae847b1b269
fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
392 we temporarily copy the items that are deleted from the |
|
7ae847b1b269
fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
393 list. :-( */ |
|
7ae847b1b269
fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
394 object **recycle, **p; |
| 6 | 395 object **item; |
| 396 int n; /* Size of replacement list */ | |
| 397 int d; /* Change in size */ | |
| 398 int k; /* Loop index */ | |
| 399 #define b ((listobject *)v) | |
| 400 if (v == NULL) | |
| 401 n = 0; | |
|
511
766cc820f511
Fix assignment of a list to a slice of itself.
Guido van Rossum <guido@python.org>
parents:
475
diff
changeset
|
402 else if (is_listobject(v)) { |
| 6 | 403 n = b->ob_size; |
|
511
766cc820f511
Fix assignment of a list to a slice of itself.
Guido van Rossum <guido@python.org>
parents:
475
diff
changeset
|
404 if (a == b) { |
|
766cc820f511
Fix assignment of a list to a slice of itself.
Guido van Rossum <guido@python.org>
parents:
475
diff
changeset
|
405 /* Special case "a[i:j] = a" -- copy b first */ |
|
766cc820f511
Fix assignment of a list to a slice of itself.
Guido van Rossum <guido@python.org>
parents:
475
diff
changeset
|
406 int ret; |
|
766cc820f511
Fix assignment of a list to a slice of itself.
Guido van Rossum <guido@python.org>
parents:
475
diff
changeset
|
407 v = list_slice(b, 0, n); |
|
766cc820f511
Fix assignment of a list to a slice of itself.
Guido van Rossum <guido@python.org>
parents:
475
diff
changeset
|
408 ret = list_ass_slice(a, ilow, ihigh, v); |
|
766cc820f511
Fix assignment of a list to a slice of itself.
Guido van Rossum <guido@python.org>
parents:
475
diff
changeset
|
409 DECREF(v); |
|
766cc820f511
Fix assignment of a list to a slice of itself.
Guido van Rossum <guido@python.org>
parents:
475
diff
changeset
|
410 return ret; |
|
766cc820f511
Fix assignment of a list to a slice of itself.
Guido van Rossum <guido@python.org>
parents:
475
diff
changeset
|
411 } |
|
766cc820f511
Fix assignment of a list to a slice of itself.
Guido van Rossum <guido@python.org>
parents:
475
diff
changeset
|
412 } |
| 27 | 413 else { |
| 414 err_badarg(); | |
| 415 return -1; | |
| 416 } | |
| 6 | 417 if (ilow < 0) |
| 418 ilow = 0; | |
| 419 else if (ilow > a->ob_size) | |
| 420 ilow = a->ob_size; | |
| 421 if (ihigh < 0) | |
| 422 ihigh = 0; | |
| 423 if (ihigh < ilow) | |
| 424 ihigh = ilow; | |
| 425 else if (ihigh > a->ob_size) | |
| 426 ihigh = a->ob_size; | |
| 427 item = a->ob_item; | |
| 428 d = n - (ihigh-ilow); | |
|
2143
7ae847b1b269
fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
429 if (ihigh > ilow) |
|
7ae847b1b269
fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
430 p = recycle = NEW(object *, (ihigh-ilow)); |
|
7ae847b1b269
fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
431 else |
|
7ae847b1b269
fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
432 p = recycle = NULL; |
|
7ae847b1b269
fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
433 if (d <= 0) { /* Delete -d items; recycle ihigh-ilow items */ |
| 6 | 434 for (k = ilow; k < ihigh; k++) |
|
2143
7ae847b1b269
fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
435 *p++ = item[k]; |
| 6 | 436 if (d < 0) { |
| 437 for (/*k = ihigh*/; k < a->ob_size; k++) | |
| 438 item[k+d] = item[k]; | |
| 439 a->ob_size += d; | |
|
2257
ee4c954cd5df
round up list item counts to improve realloc performance
Guido van Rossum <guido@python.org>
parents:
2188
diff
changeset
|
440 NRESIZE(item, object *, a->ob_size); /* Can't fail */ |
| 6 | 441 a->ob_item = item; |
| 442 } | |
| 443 } | |
|
2143
7ae847b1b269
fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
444 else { /* Insert d items; recycle ihigh-ilow items */ |
|
2257
ee4c954cd5df
round up list item counts to improve realloc performance
Guido van Rossum <guido@python.org>
parents:
2188
diff
changeset
|
445 NRESIZE(item, object *, a->ob_size + d); |
| 27 | 446 if (item == NULL) { |
|
2159
a619a61aad1e
added reverselist; free recycling bin on error exit
Guido van Rossum <guido@python.org>
parents:
2143
diff
changeset
|
447 XDEL(recycle); |
| 27 | 448 err_nomem(); |
| 449 return -1; | |
| 450 } | |
| 6 | 451 for (k = a->ob_size; --k >= ihigh; ) |
| 452 item[k+d] = item[k]; | |
| 453 for (/*k = ihigh-1*/; k >= ilow; --k) | |
|
2143
7ae847b1b269
fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
454 *p++ = item[k]; |
| 6 | 455 a->ob_item = item; |
| 456 a->ob_size += d; | |
| 457 } | |
| 458 for (k = 0; k < n; k++, ilow++) { | |
| 459 object *w = b->ob_item[k]; | |
|
1375
f3a325662d39
* listobject.c (list_ass_slice): XDECREF instead of DECREF so
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
460 XINCREF(w); |
| 6 | 461 item[ilow] = w; |
| 462 } | |
|
2143
7ae847b1b269
fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
463 if (recycle) { |
|
7ae847b1b269
fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
464 while (--p >= recycle) |
|
7ae847b1b269
fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
465 XDECREF(*p); |
|
7ae847b1b269
fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
466 DEL(recycle); |
|
7ae847b1b269
fix reentrancy bug in slice assignment
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
467 } |
| 6 | 468 return 0; |
| 469 #undef b | |
| 470 } | |
| 471 | |
|
1313
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
472 int |
|
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
473 setlistslice(a, ilow, ihigh, v) |
|
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
474 object *a; |
|
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
475 int ilow, ihigh; |
|
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
476 object *v; |
|
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
477 { |
|
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
478 if (!is_listobject(a)) { |
|
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
479 err_badcall(); |
|
1330
409ea74feb21
Minor fixes / changes for Mac compatibility.
Guido van Rossum <guido@python.org>
parents:
1313
diff
changeset
|
480 return -1; |
|
1313
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
481 } |
|
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
482 return list_ass_slice((listobject *)a, ilow, ihigh, v); |
|
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
483 } |
|
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1211
diff
changeset
|
484 |
|
167
3fa2b14be270
Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents:
155
diff
changeset
|
485 static int |
|
3fa2b14be270
Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents:
155
diff
changeset
|
486 list_ass_item(a, i, v) |
|
3fa2b14be270
Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents:
155
diff
changeset
|
487 listobject *a; |
|
3fa2b14be270
Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents:
155
diff
changeset
|
488 int i; |
|
3fa2b14be270
Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents:
155
diff
changeset
|
489 object *v; |
|
3fa2b14be270
Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents:
155
diff
changeset
|
490 { |
|
2053
de49d49044d7
Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents:
1836
diff
changeset
|
491 object *old_value; |
|
167
3fa2b14be270
Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents:
155
diff
changeset
|
492 if (i < 0 || i >= a->ob_size) { |
|
3fa2b14be270
Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents:
155
diff
changeset
|
493 err_setstr(IndexError, "list assignment index out of range"); |
|
3fa2b14be270
Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents:
155
diff
changeset
|
494 return -1; |
|
3fa2b14be270
Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents:
155
diff
changeset
|
495 } |
|
3fa2b14be270
Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents:
155
diff
changeset
|
496 if (v == NULL) |
|
3fa2b14be270
Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents:
155
diff
changeset
|
497 return list_ass_slice(a, i, i+1, v); |
|
3fa2b14be270
Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents:
155
diff
changeset
|
498 INCREF(v); |
|
2053
de49d49044d7
Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents:
1836
diff
changeset
|
499 old_value = a->ob_item[i]; |
|
167
3fa2b14be270
Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents:
155
diff
changeset
|
500 a->ob_item[i] = v; |
|
2053
de49d49044d7
Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents:
1836
diff
changeset
|
501 DECREF(old_value); |
|
167
3fa2b14be270
Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents:
155
diff
changeset
|
502 return 0; |
|
3fa2b14be270
Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents:
155
diff
changeset
|
503 } |
|
3fa2b14be270
Swapped list_ass_item and list_ass_slice to satisfy Standard C.
Guido van Rossum <guido@python.org>
parents:
155
diff
changeset
|
504 |
| 6 | 505 static object * |
| 506 ins(self, where, v) | |
| 507 listobject *self; | |
| 508 int where; | |
| 509 object *v; | |
| 510 { | |
| 511 if (ins1(self, where, v) != 0) | |
| 512 return NULL; | |
| 513 INCREF(None); | |
| 514 return None; | |
| 515 } | |
| 516 | |
| 517 static object * | |
| 518 listinsert(self, args) | |
| 519 listobject *self; | |
| 520 object *args; | |
| 521 { | |
| 522 int i; | |
|
1211
406860dfaac7
* stdwinmodule.c: various new commands: setwin{pos,size},
Guido van Rossum <guido@python.org>
parents:
1198
diff
changeset
|
523 object *v; |
|
406860dfaac7
* stdwinmodule.c: various new commands: setwin{pos,size},
Guido van Rossum <guido@python.org>
parents:
1198
diff
changeset
|
524 if (!getargs(args, "(iO)", &i, &v)) |
| 6 | 525 return NULL; |
|
1211
406860dfaac7
* stdwinmodule.c: various new commands: setwin{pos,size},
Guido van Rossum <guido@python.org>
parents:
1198
diff
changeset
|
526 return ins(self, i, v); |
| 6 | 527 } |
| 528 | |
| 529 static object * | |
| 530 listappend(self, args) | |
| 531 listobject *self; | |
| 532 object *args; | |
| 533 { | |
|
1211
406860dfaac7
* stdwinmodule.c: various new commands: setwin{pos,size},
Guido van Rossum <guido@python.org>
parents:
1198
diff
changeset
|
534 object *v; |
|
406860dfaac7
* stdwinmodule.c: various new commands: setwin{pos,size},
Guido van Rossum <guido@python.org>
parents:
1198
diff
changeset
|
535 if (!getargs(args, "O", &v)) |
|
406860dfaac7
* stdwinmodule.c: various new commands: setwin{pos,size},
Guido van Rossum <guido@python.org>
parents:
1198
diff
changeset
|
536 return NULL; |
|
406860dfaac7
* stdwinmodule.c: various new commands: setwin{pos,size},
Guido van Rossum <guido@python.org>
parents:
1198
diff
changeset
|
537 return ins(self, (int) self->ob_size, v); |
| 6 | 538 } |
| 539 | |
|
1836
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
540 static object *comparefunc; |
|
920
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
541 |
| 6 | 542 static int |
| 543 cmp(v, w) | |
|
1836
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
544 const ANY *v, *w; |
| 6 | 545 { |
|
920
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
546 object *t, *res; |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
547 long i; |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
548 |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
549 if (err_occurred()) |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
550 return 0; |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
551 |
|
1836
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
552 if (comparefunc == NULL) |
|
920
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
553 return cmpobject(* (object **) v, * (object **) w); |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
554 |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
555 /* Call the user-supplied comparison function */ |
|
2827
ff1faa4cc1df
args to call_object must be tuple or NULL
Guido van Rossum <guido@python.org>
parents:
2504
diff
changeset
|
556 t = mkvalue("(OO)", * (object **) v, * (object **) w); |
|
920
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
557 if (t == NULL) |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
558 return 0; |
|
1836
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
559 res = call_object(comparefunc, t); |
|
920
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
560 DECREF(t); |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
561 if (res == NULL) |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
562 return 0; |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
563 if (!is_intobject(res)) { |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
564 err_setstr(TypeError, "comparison function should return int"); |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
565 i = 0; |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
566 } |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
567 else { |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
568 i = getintvalue(res); |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
569 if (i < 0) |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
570 i = -1; |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
571 else if (i > 0) |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
572 i = 1; |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
573 } |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
574 DECREF(res); |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
575 return (int) i; |
| 6 | 576 } |
| 577 | |
| 578 static object * | |
| 579 listsort(self, args) | |
| 580 listobject *self; | |
| 581 object *args; | |
| 582 { | |
|
1836
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
583 object *save_comparefunc; |
|
920
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
584 if (self->ob_size <= 1) { |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
585 INCREF(None); |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
586 return None; |
| 6 | 587 } |
|
1836
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
588 save_comparefunc = comparefunc; |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
589 comparefunc = args; |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
590 if (comparefunc != NULL) { |
|
920
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
591 /* Test the comparison function for obvious errors */ |
|
1836
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
592 (void) cmp((ANY *)&self->ob_item[0], (ANY *)&self->ob_item[1]); |
|
920
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
593 if (err_occurred()) { |
|
1836
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
594 comparefunc = save_comparefunc; |
|
920
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
595 return NULL; |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
596 } |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
597 } |
|
8c73c5f3b6ca
listobject.c: added optional cmp function to list.sort().
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
598 qsort((char *)self->ob_item, |
| 6 | 599 (int) self->ob_size, sizeof(object *), cmp); |
|
1836
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
600 comparefunc = save_comparefunc; |
| 27 | 601 if (err_occurred()) |
| 6 | 602 return NULL; |
| 603 INCREF(None); | |
| 604 return None; | |
| 605 } | |
| 606 | |
|
155
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
607 static object * |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
608 listreverse(self, args) |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
609 listobject *self; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
610 object *args; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
611 { |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
612 register object **p, **q; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
613 register object *tmp; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
614 |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
615 if (args != NULL) { |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
616 err_badarg(); |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
617 return NULL; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
618 } |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
619 |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
620 if (self->ob_size > 1) { |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
621 for (p = self->ob_item, q = self->ob_item + self->ob_size - 1; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
622 p < q; p++, q--) { |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
623 tmp = *p; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
624 *p = *q; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
625 *q = tmp; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
626 } |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
627 } |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
628 |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
629 INCREF(None); |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
630 return None; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
631 } |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
632 |
|
47
05057e3660f8
Added external interface to sort a list.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
633 int |
|
2159
a619a61aad1e
added reverselist; free recycling bin on error exit
Guido van Rossum <guido@python.org>
parents:
2143
diff
changeset
|
634 reverselist(v) |
|
a619a61aad1e
added reverselist; free recycling bin on error exit
Guido van Rossum <guido@python.org>
parents:
2143
diff
changeset
|
635 object *v; |
|
a619a61aad1e
added reverselist; free recycling bin on error exit
Guido van Rossum <guido@python.org>
parents:
2143
diff
changeset
|
636 { |
|
a619a61aad1e
added reverselist; free recycling bin on error exit
Guido van Rossum <guido@python.org>
parents:
2143
diff
changeset
|
637 if (v == NULL || !is_listobject(v)) { |
|
a619a61aad1e
added reverselist; free recycling bin on error exit
Guido van Rossum <guido@python.org>
parents:
2143
diff
changeset
|
638 err_badcall(); |
|
a619a61aad1e
added reverselist; free recycling bin on error exit
Guido van Rossum <guido@python.org>
parents:
2143
diff
changeset
|
639 return -1; |
|
a619a61aad1e
added reverselist; free recycling bin on error exit
Guido van Rossum <guido@python.org>
parents:
2143
diff
changeset
|
640 } |
|
a619a61aad1e
added reverselist; free recycling bin on error exit
Guido van Rossum <guido@python.org>
parents:
2143
diff
changeset
|
641 v = listreverse((listobject *)v, (object *)NULL); |
|
a619a61aad1e
added reverselist; free recycling bin on error exit
Guido van Rossum <guido@python.org>
parents:
2143
diff
changeset
|
642 if (v == NULL) |
|
a619a61aad1e
added reverselist; free recycling bin on error exit
Guido van Rossum <guido@python.org>
parents:
2143
diff
changeset
|
643 return -1; |
|
a619a61aad1e
added reverselist; free recycling bin on error exit
Guido van Rossum <guido@python.org>
parents:
2143
diff
changeset
|
644 DECREF(v); |
|
a619a61aad1e
added reverselist; free recycling bin on error exit
Guido van Rossum <guido@python.org>
parents:
2143
diff
changeset
|
645 return 0; |
|
a619a61aad1e
added reverselist; free recycling bin on error exit
Guido van Rossum <guido@python.org>
parents:
2143
diff
changeset
|
646 } |
|
a619a61aad1e
added reverselist; free recycling bin on error exit
Guido van Rossum <guido@python.org>
parents:
2143
diff
changeset
|
647 |
|
a619a61aad1e
added reverselist; free recycling bin on error exit
Guido van Rossum <guido@python.org>
parents:
2143
diff
changeset
|
648 int |
|
47
05057e3660f8
Added external interface to sort a list.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
649 sortlist(v) |
|
05057e3660f8
Added external interface to sort a list.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
650 object *v; |
|
05057e3660f8
Added external interface to sort a list.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
651 { |
|
05057e3660f8
Added external interface to sort a list.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
652 if (v == NULL || !is_listobject(v)) { |
|
05057e3660f8
Added external interface to sort a list.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
653 err_badcall(); |
|
05057e3660f8
Added external interface to sort a list.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
654 return -1; |
|
05057e3660f8
Added external interface to sort a list.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
655 } |
|
05057e3660f8
Added external interface to sort a list.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
656 v = listsort((listobject *)v, (object *)NULL); |
|
05057e3660f8
Added external interface to sort a list.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
657 if (v == NULL) |
|
05057e3660f8
Added external interface to sort a list.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
658 return -1; |
|
05057e3660f8
Added external interface to sort a list.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
659 DECREF(v); |
|
05057e3660f8
Added external interface to sort a list.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
660 return 0; |
|
05057e3660f8
Added external interface to sort a list.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
661 } |
|
05057e3660f8
Added external interface to sort a list.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
662 |
|
1836
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
663 object * |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
664 listtuple(v) |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
665 object *v; |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
666 { |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
667 object *w; |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
668 object **p; |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
669 int n; |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
670 if (v == NULL || !is_listobject(v)) { |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
671 err_badcall(); |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
672 return NULL; |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
673 } |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
674 n = ((listobject *)v)->ob_size; |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
675 w = newtupleobject(n); |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
676 if (w == NULL) |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
677 return NULL; |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
678 p = ((tupleobject *)w)->ob_item; |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
679 memcpy((ANY *)p, |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
680 (ANY *)((listobject *)v)->ob_item, |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
681 n*sizeof(object *)); |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
682 while (--n >= 0) { |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
683 INCREF(*p); |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
684 p++; |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
685 } |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
686 return w; |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
687 } |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
688 |
|
155
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
689 static object * |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
690 listindex(self, args) |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
691 listobject *self; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
692 object *args; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
693 { |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
694 int i; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
695 |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
696 if (args == NULL) { |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
697 err_badarg(); |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
698 return NULL; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
699 } |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
700 for (i = 0; i < self->ob_size; i++) { |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
701 if (cmpobject(self->ob_item[i], args) == 0) |
|
1051
f54e64c8b9b7
* Makefile: added IMGFILE; moved some stuff around.
Guido van Rossum <guido@python.org>
parents:
1024
diff
changeset
|
702 return newintobject((long)i); |
|
155
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
703 } |
| 475 | 704 err_setstr(ValueError, "list.index(x): x not in list"); |
|
155
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
705 return NULL; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
706 } |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
707 |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
708 static object * |
| 394 | 709 listcount(self, args) |
| 710 listobject *self; | |
| 711 object *args; | |
| 712 { | |
| 713 int count = 0; | |
| 714 int i; | |
| 715 | |
| 716 if (args == NULL) { | |
| 717 err_badarg(); | |
| 718 return NULL; | |
| 719 } | |
| 720 for (i = 0; i < self->ob_size; i++) { | |
| 721 if (cmpobject(self->ob_item[i], args) == 0) | |
| 722 count++; | |
| 723 } | |
| 724 return newintobject((long)count); | |
| 725 } | |
| 726 | |
| 727 static object * | |
|
155
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
728 listremove(self, args) |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
729 listobject *self; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
730 object *args; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
731 { |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
732 int i; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
733 |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
734 if (args == NULL) { |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
735 err_badarg(); |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
736 return NULL; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
737 } |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
738 for (i = 0; i < self->ob_size; i++) { |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
739 if (cmpobject(self->ob_item[i], args) == 0) { |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
740 if (list_ass_slice(self, i, i+1, (object *)NULL) != 0) |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
741 return NULL; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
742 INCREF(None); |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
743 return None; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
744 } |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
745 |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
746 } |
| 475 | 747 err_setstr(ValueError, "list.remove(x): x not in list"); |
|
155
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
748 return NULL; |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
749 } |
|
896c825f2f71
Added repeat (for list*integet).
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
750 |
| 6 | 751 static struct methodlist list_methods[] = { |
|
1836
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
752 {"append", (method)listappend}, |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
753 {"count", (method)listcount}, |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
754 {"index", (method)listindex}, |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
755 {"insert", (method)listinsert}, |
|
2397
0736a7ec2f28
explicitly init flags in methodlists
Guido van Rossum <guido@python.org>
parents:
2257
diff
changeset
|
756 {"sort", (method)listsort, 0}, |
|
1836
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
757 {"remove", (method)listremove}, |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
758 {"reverse", (method)listreverse}, |
| 6 | 759 {NULL, NULL} /* sentinel */ |
| 760 }; | |
| 761 | |
| 762 static object * | |
| 763 list_getattr(f, name) | |
| 764 listobject *f; | |
| 765 char *name; | |
| 766 { | |
| 767 return findmethod(list_methods, (object *)f, name); | |
| 768 } | |
| 769 | |
| 770 static sequence_methods list_as_sequence = { | |
|
1836
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
771 (inquiry)list_length, /*sq_length*/ |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
772 (binaryfunc)list_concat, /*sq_concat*/ |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
773 (intargfunc)list_repeat, /*sq_repeat*/ |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
774 (intargfunc)list_item, /*sq_item*/ |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
775 (intintargfunc)list_slice, /*sq_slice*/ |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
776 (intobjargproc)list_ass_item, /*sq_ass_item*/ |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
777 (intintobjargproc)list_ass_slice, /*sq_ass_slice*/ |
| 6 | 778 }; |
| 779 | |
| 780 typeobject Listtype = { | |
| 781 OB_HEAD_INIT(&Typetype) | |
| 782 0, | |
| 783 "list", | |
| 784 sizeof(listobject), | |
| 785 0, | |
|
1836
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
786 (destructor)list_dealloc, /*tp_dealloc*/ |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
787 (printfunc)list_print, /*tp_print*/ |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
788 (getattrfunc)list_getattr, /*tp_getattr*/ |
| 6 | 789 0, /*tp_setattr*/ |
|
1836
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
790 (cmpfunc)list_compare, /*tp_compare*/ |
|
1203f0629527
Correct problems found by THINK C 6.0
Guido van Rossum <guido@python.org>
parents:
1405
diff
changeset
|
791 (reprfunc)list_repr, /*tp_repr*/ |
| 6 | 792 0, /*tp_as_number*/ |
| 793 &list_as_sequence, /*tp_as_sequence*/ | |
| 794 0, /*tp_as_mapping*/ | |
| 795 }; |
