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