Mercurial > cpython-lru_cache-weakref
annotate Objects/stringobject.c @ 892:11ea669c231e legacy-trunk
Documented new contents of lib5.tex
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 03 Jun 1992 17:56:44 +0000 |
| parents | 0cd197947d81 |
| children | 94ab6761afe2 |
| rev | line source |
|---|---|
|
145
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
136
diff
changeset
|
1 /*********************************************************** |
|
795
0cd197947d81
Copyright for 1992 added
Guido van Rossum <guido@python.org>
parents:
750
diff
changeset
|
2 Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The |
|
145
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
136
diff
changeset
|
3 Netherlands. |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
136
diff
changeset
|
4 |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
136
diff
changeset
|
5 All Rights Reserved |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
136
diff
changeset
|
6 |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
136
diff
changeset
|
7 Permission to use, copy, modify, and distribute this software and its |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
136
diff
changeset
|
8 documentation for any purpose and without fee is hereby granted, |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
136
diff
changeset
|
9 provided that the above copyright notice appear in all copies and that |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
136
diff
changeset
|
10 both that copyright notice and this permission notice appear in |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
136
diff
changeset
|
11 supporting documentation, and that the names of Stichting Mathematisch |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
136
diff
changeset
|
12 Centrum or CWI not be used in advertising or publicity pertaining to |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
136
diff
changeset
|
13 distribution of the software without specific, written prior permission. |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
136
diff
changeset
|
14 |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
136
diff
changeset
|
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
136
diff
changeset
|
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
136
diff
changeset
|
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
136
diff
changeset
|
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
136
diff
changeset
|
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
136
diff
changeset
|
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
136
diff
changeset
|
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
136
diff
changeset
|
22 |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
136
diff
changeset
|
23 ******************************************************************/ |
|
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
136
diff
changeset
|
24 |
| 6 | 25 /* String object implementation */ |
| 26 | |
| 86 | 27 #include "allobjects.h" |
| 6 | 28 |
| 29 object * | |
| 30 newsizedstringobject(str, size) | |
| 31 char *str; | |
| 32 int size; | |
| 33 { | |
| 34 register stringobject *op = (stringobject *) | |
| 35 malloc(sizeof(stringobject) + size * sizeof(char)); | |
| 27 | 36 if (op == NULL) |
| 37 return err_nomem(); | |
| 38 NEWREF(op); | |
| 39 op->ob_type = &Stringtype; | |
| 40 op->ob_size = size; | |
| 41 if (str != NULL) | |
| 42 memcpy(op->ob_sval, str, size); | |
| 43 op->ob_sval[size] = '\0'; | |
| 6 | 44 return (object *) op; |
| 45 } | |
| 46 | |
| 47 object * | |
| 48 newstringobject(str) | |
| 49 char *str; | |
| 50 { | |
| 51 register unsigned int size = strlen(str); | |
| 52 register stringobject *op = (stringobject *) | |
| 53 malloc(sizeof(stringobject) + size * sizeof(char)); | |
| 27 | 54 if (op == NULL) |
| 55 return err_nomem(); | |
| 56 NEWREF(op); | |
| 57 op->ob_type = &Stringtype; | |
| 58 op->ob_size = size; | |
| 59 strcpy(op->ob_sval, str); | |
| 6 | 60 return (object *) op; |
| 61 } | |
| 62 | |
| 750 | 63 void |
| 64 stringdealloc(op) | |
| 65 object *op; | |
| 66 { | |
| 67 DEL(op); | |
| 68 } | |
| 69 | |
| 6 | 70 unsigned int |
| 71 getstringsize(op) | |
| 72 register object *op; | |
| 73 { | |
| 74 if (!is_stringobject(op)) { | |
| 27 | 75 err_badcall(); |
| 6 | 76 return -1; |
| 77 } | |
| 78 return ((stringobject *)op) -> ob_size; | |
| 79 } | |
| 80 | |
| 81 /*const*/ char * | |
| 82 getstringvalue(op) | |
| 83 register object *op; | |
| 84 { | |
| 85 if (!is_stringobject(op)) { | |
| 27 | 86 err_badcall(); |
| 6 | 87 return NULL; |
| 88 } | |
| 89 return ((stringobject *)op) -> ob_sval; | |
| 90 } | |
| 91 | |
| 92 /* Methods */ | |
| 93 | |
|
287
07efda2951a8
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
259
diff
changeset
|
94 static int |
| 6 | 95 stringprint(op, fp, flags) |
| 96 stringobject *op; | |
| 97 FILE *fp; | |
| 98 int flags; | |
| 99 { | |
| 100 int i; | |
| 101 char c; | |
|
287
07efda2951a8
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
259
diff
changeset
|
102 /* XXX Ought to check for interrupts when writing long strings */ |
| 6 | 103 if (flags & PRINT_RAW) { |
| 104 fwrite(op->ob_sval, 1, (int) op->ob_size, fp); | |
|
287
07efda2951a8
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
259
diff
changeset
|
105 return 0; |
| 6 | 106 } |
| 107 fprintf(fp, "'"); | |
| 108 for (i = 0; i < op->ob_size; i++) { | |
| 109 c = op->ob_sval[i]; | |
| 110 if (c == '\'' || c == '\\') | |
| 111 fprintf(fp, "\\%c", c); | |
| 112 else if (c < ' ' || c >= 0177) | |
| 113 fprintf(fp, "\\%03o", c&0377); | |
| 114 else | |
| 115 putc(c, fp); | |
| 116 } | |
| 117 fprintf(fp, "'"); | |
|
287
07efda2951a8
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
259
diff
changeset
|
118 return 0; |
| 6 | 119 } |
| 120 | |
| 121 static object * | |
| 122 stringrepr(op) | |
| 123 register stringobject *op; | |
| 124 { | |
| 125 /* XXX overflow? */ | |
| 126 int newsize = 2 + 4 * op->ob_size * sizeof(char); | |
| 127 object *v = newsizedstringobject((char *)NULL, newsize); | |
| 128 if (v == NULL) { | |
|
287
07efda2951a8
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
259
diff
changeset
|
129 return NULL; |
| 6 | 130 } |
| 131 else { | |
| 132 register int i; | |
| 133 register char c; | |
| 134 register char *p; | |
| 135 NEWREF(v); | |
| 136 v->ob_type = &Stringtype; | |
| 137 ((stringobject *)v)->ob_size = newsize; | |
| 138 p = ((stringobject *)v)->ob_sval; | |
| 139 *p++ = '\''; | |
| 140 for (i = 0; i < op->ob_size; i++) { | |
| 141 c = op->ob_sval[i]; | |
| 142 if (c == '\'' || c == '\\') | |
| 143 *p++ = '\\', *p++ = c; | |
| 144 else if (c < ' ' || c >= 0177) { | |
| 145 sprintf(p, "\\%03o", c&0377); | |
| 146 while (*p != '\0') | |
| 147 p++; | |
| 148 | |
| 149 } | |
| 150 else | |
| 151 *p++ = c; | |
| 152 } | |
| 153 *p++ = '\''; | |
| 154 *p = '\0'; | |
| 155 resizestring(&v, (int) (p - ((stringobject *)v)->ob_sval)); | |
| 27 | 156 return v; |
| 6 | 157 } |
| 158 } | |
| 159 | |
| 160 static int | |
| 161 stringlength(a) | |
| 162 stringobject *a; | |
| 163 { | |
| 164 return a->ob_size; | |
| 165 } | |
| 166 | |
| 167 static object * | |
| 168 stringconcat(a, bb) | |
| 169 register stringobject *a; | |
| 170 register object *bb; | |
| 171 { | |
| 172 register unsigned int size; | |
| 173 register stringobject *op; | |
| 174 if (!is_stringobject(bb)) { | |
| 27 | 175 err_badarg(); |
| 6 | 176 return NULL; |
| 177 } | |
| 178 #define b ((stringobject *)bb) | |
| 179 /* Optimize cases with empty left or right operand */ | |
| 180 if (a->ob_size == 0) { | |
| 181 INCREF(bb); | |
| 182 return bb; | |
| 183 } | |
| 184 if (b->ob_size == 0) { | |
| 185 INCREF(a); | |
| 186 return (object *)a; | |
| 187 } | |
| 188 size = a->ob_size + b->ob_size; | |
| 189 op = (stringobject *) | |
| 190 malloc(sizeof(stringobject) + size * sizeof(char)); | |
| 27 | 191 if (op == NULL) |
| 192 return err_nomem(); | |
| 193 NEWREF(op); | |
| 194 op->ob_type = &Stringtype; | |
| 195 op->ob_size = size; | |
| 196 memcpy(op->ob_sval, a->ob_sval, (int) a->ob_size); | |
| 197 memcpy(op->ob_sval + a->ob_size, b->ob_sval, (int) b->ob_size); | |
| 198 op->ob_sval[size] = '\0'; | |
| 6 | 199 return (object *) op; |
| 200 #undef b | |
| 201 } | |
| 202 | |
| 203 static object * | |
| 204 stringrepeat(a, n) | |
| 205 register stringobject *a; | |
| 206 register int n; | |
| 207 { | |
| 208 register int i; | |
| 209 register unsigned int size; | |
| 210 register stringobject *op; | |
| 211 if (n < 0) | |
| 212 n = 0; | |
| 213 size = a->ob_size * n; | |
| 214 if (size == a->ob_size) { | |
| 215 INCREF(a); | |
| 216 return (object *)a; | |
| 217 } | |
| 218 op = (stringobject *) | |
| 219 malloc(sizeof(stringobject) + size * sizeof(char)); | |
| 27 | 220 if (op == NULL) |
| 221 return err_nomem(); | |
| 222 NEWREF(op); | |
| 223 op->ob_type = &Stringtype; | |
| 224 op->ob_size = size; | |
| 225 for (i = 0; i < size; i += a->ob_size) | |
| 226 memcpy(op->ob_sval+i, a->ob_sval, (int) a->ob_size); | |
| 227 op->ob_sval[size] = '\0'; | |
| 6 | 228 return (object *) op; |
| 229 } | |
| 230 | |
| 231 /* String slice a[i:j] consists of characters a[i] ... a[j-1] */ | |
| 232 | |
| 233 static object * | |
| 234 stringslice(a, i, j) | |
| 235 register stringobject *a; | |
| 236 register int i, j; /* May be negative! */ | |
| 237 { | |
| 238 if (i < 0) | |
| 239 i = 0; | |
| 240 if (j < 0) | |
| 241 j = 0; /* Avoid signed/unsigned bug in next line */ | |
| 242 if (j > a->ob_size) | |
| 243 j = a->ob_size; | |
| 244 if (i == 0 && j == a->ob_size) { /* It's the same as a */ | |
| 245 INCREF(a); | |
| 246 return (object *)a; | |
| 247 } | |
| 248 if (j < i) | |
| 249 j = i; | |
| 250 return newsizedstringobject(a->ob_sval + i, (int) (j-i)); | |
| 251 } | |
| 252 | |
|
180
a405a31242e7
Optimized single-character strings gotten from s[i].
Guido van Rossum <guido@python.org>
parents:
158
diff
changeset
|
253 #ifdef __STDC__ |
|
a405a31242e7
Optimized single-character strings gotten from s[i].
Guido van Rossum <guido@python.org>
parents:
158
diff
changeset
|
254 #include <limits.h> |
|
a405a31242e7
Optimized single-character strings gotten from s[i].
Guido van Rossum <guido@python.org>
parents:
158
diff
changeset
|
255 #else |
|
a405a31242e7
Optimized single-character strings gotten from s[i].
Guido van Rossum <guido@python.org>
parents:
158
diff
changeset
|
256 #ifndef UCHAR_MAX |
|
a405a31242e7
Optimized single-character strings gotten from s[i].
Guido van Rossum <guido@python.org>
parents:
158
diff
changeset
|
257 #define UCHAR_MAX 255 |
|
a405a31242e7
Optimized single-character strings gotten from s[i].
Guido van Rossum <guido@python.org>
parents:
158
diff
changeset
|
258 #endif |
|
a405a31242e7
Optimized single-character strings gotten from s[i].
Guido van Rossum <guido@python.org>
parents:
158
diff
changeset
|
259 #endif |
|
a405a31242e7
Optimized single-character strings gotten from s[i].
Guido van Rossum <guido@python.org>
parents:
158
diff
changeset
|
260 |
|
a405a31242e7
Optimized single-character strings gotten from s[i].
Guido van Rossum <guido@python.org>
parents:
158
diff
changeset
|
261 static object *characters[UCHAR_MAX + 1]; |
|
a405a31242e7
Optimized single-character strings gotten from s[i].
Guido van Rossum <guido@python.org>
parents:
158
diff
changeset
|
262 |
| 6 | 263 static object * |
| 264 stringitem(a, i) | |
| 265 stringobject *a; | |
| 266 register int i; | |
| 267 { | |
|
180
a405a31242e7
Optimized single-character strings gotten from s[i].
Guido van Rossum <guido@python.org>
parents:
158
diff
changeset
|
268 int c; |
|
a405a31242e7
Optimized single-character strings gotten from s[i].
Guido van Rossum <guido@python.org>
parents:
158
diff
changeset
|
269 object *v; |
| 6 | 270 if (i < 0 || i >= a->ob_size) { |
| 27 | 271 err_setstr(IndexError, "string index out of range"); |
| 6 | 272 return NULL; |
| 273 } | |
|
180
a405a31242e7
Optimized single-character strings gotten from s[i].
Guido van Rossum <guido@python.org>
parents:
158
diff
changeset
|
274 c = a->ob_sval[i] & UCHAR_MAX; |
|
a405a31242e7
Optimized single-character strings gotten from s[i].
Guido van Rossum <guido@python.org>
parents:
158
diff
changeset
|
275 v = characters[c]; |
|
a405a31242e7
Optimized single-character strings gotten from s[i].
Guido van Rossum <guido@python.org>
parents:
158
diff
changeset
|
276 if (v == NULL) { |
|
a405a31242e7
Optimized single-character strings gotten from s[i].
Guido van Rossum <guido@python.org>
parents:
158
diff
changeset
|
277 v = newsizedstringobject((char *)NULL, 1); |
|
a405a31242e7
Optimized single-character strings gotten from s[i].
Guido van Rossum <guido@python.org>
parents:
158
diff
changeset
|
278 if (v == NULL) |
|
a405a31242e7
Optimized single-character strings gotten from s[i].
Guido van Rossum <guido@python.org>
parents:
158
diff
changeset
|
279 return NULL; |
|
a405a31242e7
Optimized single-character strings gotten from s[i].
Guido van Rossum <guido@python.org>
parents:
158
diff
changeset
|
280 characters[c] = v; |
|
a405a31242e7
Optimized single-character strings gotten from s[i].
Guido van Rossum <guido@python.org>
parents:
158
diff
changeset
|
281 ((stringobject *)v)->ob_sval[0] = c; |
|
a405a31242e7
Optimized single-character strings gotten from s[i].
Guido van Rossum <guido@python.org>
parents:
158
diff
changeset
|
282 } |
|
a405a31242e7
Optimized single-character strings gotten from s[i].
Guido van Rossum <guido@python.org>
parents:
158
diff
changeset
|
283 INCREF(v); |
|
a405a31242e7
Optimized single-character strings gotten from s[i].
Guido van Rossum <guido@python.org>
parents:
158
diff
changeset
|
284 return v; |
| 6 | 285 } |
| 286 | |
| 287 static int | |
| 288 stringcompare(a, b) | |
| 289 stringobject *a, *b; | |
| 290 { | |
|
136
6523bd2954f0
Fix stringcompare when strings contain null bytes.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
291 int len_a = a->ob_size, len_b = b->ob_size; |
|
6523bd2954f0
Fix stringcompare when strings contain null bytes.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
292 int min_len = (len_a < len_b) ? len_a : len_b; |
|
6523bd2954f0
Fix stringcompare when strings contain null bytes.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
293 int cmp = memcmp(a->ob_sval, b->ob_sval, min_len); |
|
6523bd2954f0
Fix stringcompare when strings contain null bytes.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
294 if (cmp != 0) |
|
6523bd2954f0
Fix stringcompare when strings contain null bytes.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
295 return cmp; |
|
6523bd2954f0
Fix stringcompare when strings contain null bytes.
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
296 return (len_a < len_b) ? -1 : (len_a > len_b) ? 1 : 0; |
| 6 | 297 } |
| 298 | |
| 299 static sequence_methods string_as_sequence = { | |
|
259
6321d3dc57e0
Fix comments in string_as_sequence
Guido van Rossum <guido@python.org>
parents:
180
diff
changeset
|
300 stringlength, /*sq_length*/ |
|
6321d3dc57e0
Fix comments in string_as_sequence
Guido van Rossum <guido@python.org>
parents:
180
diff
changeset
|
301 stringconcat, /*sq_concat*/ |
|
6321d3dc57e0
Fix comments in string_as_sequence
Guido van Rossum <guido@python.org>
parents:
180
diff
changeset
|
302 stringrepeat, /*sq_repeat*/ |
|
6321d3dc57e0
Fix comments in string_as_sequence
Guido van Rossum <guido@python.org>
parents:
180
diff
changeset
|
303 stringitem, /*sq_item*/ |
|
6321d3dc57e0
Fix comments in string_as_sequence
Guido van Rossum <guido@python.org>
parents:
180
diff
changeset
|
304 stringslice, /*sq_slice*/ |
|
6321d3dc57e0
Fix comments in string_as_sequence
Guido van Rossum <guido@python.org>
parents:
180
diff
changeset
|
305 0, /*sq_ass_item*/ |
|
6321d3dc57e0
Fix comments in string_as_sequence
Guido van Rossum <guido@python.org>
parents:
180
diff
changeset
|
306 0, /*sq_ass_slice*/ |
| 6 | 307 }; |
| 308 | |
| 309 typeobject Stringtype = { | |
| 310 OB_HEAD_INIT(&Typetype) | |
| 311 0, | |
| 312 "string", | |
| 313 sizeof(stringobject), | |
| 314 sizeof(char), | |
| 750 | 315 stringdealloc, /*tp_dealloc*/ |
| 6 | 316 stringprint, /*tp_print*/ |
| 317 0, /*tp_getattr*/ | |
| 318 0, /*tp_setattr*/ | |
| 319 stringcompare, /*tp_compare*/ | |
| 320 stringrepr, /*tp_repr*/ | |
| 321 0, /*tp_as_number*/ | |
| 322 &string_as_sequence, /*tp_as_sequence*/ | |
| 323 0, /*tp_as_mapping*/ | |
| 324 }; | |
| 325 | |
| 326 void | |
| 327 joinstring(pv, w) | |
| 328 register object **pv; | |
| 329 register object *w; | |
| 330 { | |
| 331 register object *v; | |
| 332 if (*pv == NULL || w == NULL || !is_stringobject(*pv)) | |
| 333 return; | |
| 334 v = stringconcat((stringobject *) *pv, w); | |
| 335 DECREF(*pv); | |
| 336 *pv = v; | |
| 337 } | |
| 338 | |
| 339 /* The following function breaks the notion that strings are immutable: | |
| 340 it changes the size of a string. We get away with this only if there | |
| 341 is only one module referencing the object. You can also think of it | |
| 342 as creating a new string object and destroying the old one, only | |
| 343 more efficiently. In any case, don't use this if the string may | |
| 344 already be known to some other part of the code... */ | |
| 345 | |
| 346 int | |
| 347 resizestring(pv, newsize) | |
| 348 object **pv; | |
| 349 int newsize; | |
| 350 { | |
|
71
ac4522e99ca9
Fixed resizestring() to work if reference tracing is turned on.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
351 register object *v; |
|
ac4522e99ca9
Fixed resizestring() to work if reference tracing is turned on.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
352 register stringobject *sv; |
|
ac4522e99ca9
Fixed resizestring() to work if reference tracing is turned on.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
353 v = *pv; |
| 6 | 354 if (!is_stringobject(v) || v->ob_refcnt != 1) { |
| 355 *pv = 0; | |
| 356 DECREF(v); | |
| 27 | 357 err_badcall(); |
| 358 return -1; | |
| 6 | 359 } |
|
71
ac4522e99ca9
Fixed resizestring() to work if reference tracing is turned on.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
360 /* XXX UNREF/NEWREF interface should be more symmetrical */ |
| 82 | 361 #ifdef REF_DEBUG |
|
71
ac4522e99ca9
Fixed resizestring() to work if reference tracing is turned on.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
362 --ref_total; |
|
ac4522e99ca9
Fixed resizestring() to work if reference tracing is turned on.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
363 #endif |
|
ac4522e99ca9
Fixed resizestring() to work if reference tracing is turned on.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
364 UNREF(v); |
| 6 | 365 *pv = (object *) |
| 366 realloc((char *)v, | |
| 367 sizeof(stringobject) + newsize * sizeof(char)); | |
| 368 if (*pv == NULL) { | |
|
71
ac4522e99ca9
Fixed resizestring() to work if reference tracing is turned on.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
369 DEL(v); |
| 27 | 370 err_nomem(); |
| 371 return -1; | |
| 6 | 372 } |
|
71
ac4522e99ca9
Fixed resizestring() to work if reference tracing is turned on.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
373 NEWREF(*pv); |
|
ac4522e99ca9
Fixed resizestring() to work if reference tracing is turned on.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
374 sv = (stringobject *) *pv; |
|
ac4522e99ca9
Fixed resizestring() to work if reference tracing is turned on.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
375 sv->ob_size = newsize; |
|
ac4522e99ca9
Fixed resizestring() to work if reference tracing is turned on.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
376 sv->ob_sval[newsize] = '\0'; |
| 6 | 377 return 0; |
| 378 } |
