annotate Objects/stringobject.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 4dc67fdd9e0b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 136
diff changeset
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
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
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4458
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: 4458
diff changeset
8 documentation for any purpose and without fee is hereby granted,
145
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
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4458
diff changeset
10 both that copyright notice and this permission notice appear in
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 136
diff changeset
11 supporting documentation, and that the names of Stichting Mathematisch
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4458
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: 4458
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: 4458
diff changeset
14 distribution of the software without specific, written prior
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4458
diff changeset
15 permission.
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 136
diff changeset
16
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4458
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: 4458
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: 4458
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: 4458
diff changeset
20
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 4458
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: 4458
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: 4458
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: 4458
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: 4458
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: 4458
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: 4458
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: 4458
diff changeset
28 PERFORMANCE OF THIS SOFTWARE.
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 136
diff changeset
29
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 136
diff changeset
30 ******************************************************************/
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 136
diff changeset
31
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 /* String object implementation */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 82
diff changeset
34 #include "allobjects.h"
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
35
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
36 #include <ctype.h>
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
37
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
38 #ifdef COUNT_ALLOCS
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
39 int null_strings, one_strings;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
40 #endif
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
41
1908
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1847
diff changeset
42 #ifdef HAVE_LIMITS_H
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
43 #include <limits.h>
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
44 #else
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
45 #ifndef UCHAR_MAX
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
46 #define UCHAR_MAX 255
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
47 #endif
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
48 #endif
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
49
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
50 static stringobject *characters[UCHAR_MAX + 1];
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1368
diff changeset
51 #ifndef DONT_SHARE_SHORT_STRINGS
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
52 static stringobject *nullstring;
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1368
diff changeset
53 #endif
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
54
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
55 /*
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
56 Newsizedstringobject() and newstringobject() try in certain cases
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
57 to share string objects. When the size of the string is zero,
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
58 these routines always return a pointer to the same string object;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
59 when the size is one, they return a pointer to an already existing
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
60 object if the contents of the string is known. For
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
61 newstringobject() this is always the case, for
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
62 newsizedstringobject() this is the case when the first argument in
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
63 not NULL.
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
64 A common practice to allocate a string and then fill it in or
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
65 change it must be done carefully. It is only allowed to change the
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
66 contents of the string if the obect was gotten from
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
67 newsizedstringobject() with a NULL first argument, because in the
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
68 future these routines may try to do even more sharing of objects.
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
69 */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 object *
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 newsizedstringobject(str, size)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 char *str;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 int size;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 {
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
75 register stringobject *op;
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1368
diff changeset
76 #ifndef DONT_SHARE_SHORT_STRINGS
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
77 if (size == 0 && (op = nullstring) != NULL) {
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
78 #ifdef COUNT_ALLOCS
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
79 null_strings++;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
80 #endif
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
81 INCREF(op);
1368
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
82 return (object *)op;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
83 }
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
84 if (size == 1 && str != NULL && (op = characters[*str & UCHAR_MAX]) != NULL) {
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
85 #ifdef COUNT_ALLOCS
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
86 one_strings++;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
87 #endif
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
88 INCREF(op);
1368
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
89 return (object *)op;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
90 }
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1368
diff changeset
91 #endif /* DONT_SHARE_SHORT_STRINGS */
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
92 op = (stringobject *)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 malloc(sizeof(stringobject) + size * sizeof(char));
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
94 if (op == NULL)
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
95 return err_nomem();
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
96 op->ob_type = &Stringtype;
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
97 op->ob_size = size;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
98 #ifdef CACHE_HASH
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
99 op->ob_shash = -1;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
100 #endif
1347
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1313
diff changeset
101 NEWREF(op);
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
102 if (str != NULL)
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
103 memcpy(op->ob_sval, str, size);
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
104 op->ob_sval[size] = '\0';
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1368
diff changeset
105 #ifndef DONT_SHARE_SHORT_STRINGS
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
106 if (size == 0) {
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
107 nullstring = op;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
108 INCREF(op);
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
109 } else if (size == 1 && str != NULL) {
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
110 characters[*str & UCHAR_MAX] = op;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
111 INCREF(op);
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
112 }
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1368
diff changeset
113 #endif
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 return (object *) op;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
116
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 object *
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 newstringobject(str)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 char *str;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 register unsigned int size = strlen(str);
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
122 register stringobject *op;
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1368
diff changeset
123 #ifndef DONT_SHARE_SHORT_STRINGS
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
124 if (size == 0 && (op = nullstring) != NULL) {
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
125 #ifdef COUNT_ALLOCS
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
126 null_strings++;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
127 #endif
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
128 INCREF(op);
1368
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
129 return (object *)op;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
130 }
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
131 if (size == 1 && (op = characters[*str & UCHAR_MAX]) != NULL) {
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
132 #ifdef COUNT_ALLOCS
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
133 one_strings++;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
134 #endif
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
135 INCREF(op);
1368
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
136 return (object *)op;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
137 }
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1368
diff changeset
138 #endif /* DONT_SHARE_SHORT_STRINGS */
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
139 op = (stringobject *)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 malloc(sizeof(stringobject) + size * sizeof(char));
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
141 if (op == NULL)
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
142 return err_nomem();
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
143 op->ob_type = &Stringtype;
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
144 op->ob_size = size;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
145 #ifdef CACHE_HASH
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
146 op->ob_shash = -1;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
147 #endif
1347
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1313
diff changeset
148 NEWREF(op);
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
149 strcpy(op->ob_sval, str);
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1368
diff changeset
150 #ifndef DONT_SHARE_SHORT_STRINGS
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
151 if (size == 0) {
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
152 nullstring = op;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
153 INCREF(op);
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
154 } else if (size == 1) {
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
155 characters[*str & UCHAR_MAX] = op;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
156 INCREF(op);
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
157 }
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1368
diff changeset
158 #endif
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 return (object *) op;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
161
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1282
diff changeset
162 static void
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
163 string_dealloc(op)
750
a5e6881cebf0 lint fix
Guido van Rossum <guido@python.org>
parents: 287
diff changeset
164 object *op;
a5e6881cebf0 lint fix
Guido van Rossum <guido@python.org>
parents: 287
diff changeset
165 {
a5e6881cebf0 lint fix
Guido van Rossum <guido@python.org>
parents: 287
diff changeset
166 DEL(op);
a5e6881cebf0 lint fix
Guido van Rossum <guido@python.org>
parents: 287
diff changeset
167 }
a5e6881cebf0 lint fix
Guido van Rossum <guido@python.org>
parents: 287
diff changeset
168
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1908
diff changeset
169 int
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 getstringsize(op)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 register object *op;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 if (!is_stringobject(op)) {
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
174 err_badcall();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 return -1;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 return ((stringobject *)op) -> ob_size;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
179
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 /*const*/ char *
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 getstringvalue(op)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 register object *op;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 if (!is_stringobject(op)) {
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
185 err_badcall();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 return ((stringobject *)op) -> ob_sval;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
190
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 /* Methods */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
192
287
07efda2951a8 printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 259
diff changeset
193 static int
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
194 string_print(op, fp, flags)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 stringobject *op;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
196 FILE *fp;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 int flags;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 int i;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 char c;
1368
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
201 int quote;
287
07efda2951a8 printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 259
diff changeset
202 /* XXX Ought to check for interrupts when writing long strings */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 if (flags & PRINT_RAW) {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 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
205 return 0;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 }
1368
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
207
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
208 /* figure out which quote to use; single is prefered */
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
209 quote = '\'';
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
210 if (strchr(op->ob_sval, '\'') && !strchr(op->ob_sval, '"'))
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
211 quote = '"';
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
212
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
213 fputc(quote, fp);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 for (i = 0; i < op->ob_size; i++) {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 c = op->ob_sval[i];
1368
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
216 if (c == quote || c == '\\')
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 fprintf(fp, "\\%c", c);
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 else if (c < ' ' || c >= 0177)
1368
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
219 fprintf(fp, "\\%03o", c & 0377);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 else
1368
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
221 fputc(c, fp);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 }
1368
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
223 fputc(quote, fp);
287
07efda2951a8 printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 259
diff changeset
224 return 0;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
225 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
226
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
227 static object *
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
228 string_repr(op)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 register stringobject *op;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
230 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
231 /* XXX overflow? */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
232 int newsize = 2 + 4 * op->ob_size * sizeof(char);
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
233 object *v = newsizedstringobject((char *)NULL, newsize);
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
234 if (v == NULL) {
287
07efda2951a8 printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 259
diff changeset
235 return NULL;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
236 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 else {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 register int i;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
239 register char c;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
240 register char *p;
1368
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
241 int quote;
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
242
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
243 /* figure out which quote to use; single is prefered */
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
244 quote = '\'';
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
245 if (strchr(op->ob_sval, '\'') && !strchr(op->ob_sval, '"'))
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
246 quote = '"';
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
247
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 p = ((stringobject *)v)->ob_sval;
1368
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
249 *p++ = quote;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
250 for (i = 0; i < op->ob_size; i++) {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
251 c = op->ob_sval[i];
1368
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
252 if (c == quote || c == '\\')
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
253 *p++ = '\\', *p++ = c;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
254 else if (c < ' ' || c >= 0177) {
1368
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
255 sprintf(p, "\\%03o", c & 0377);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
256 while (*p != '\0')
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
257 p++;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
258 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
259 else
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
260 *p++ = c;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
261 }
1368
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
262 *p++ = quote;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
263 *p = '\0';
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
264 resizestring(&v, (int) (p - ((stringobject *)v)->ob_sval));
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
265 return v;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
267 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
268
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
269 static int
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
270 string_length(a)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
271 stringobject *a;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
272 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
273 return a->ob_size;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
274 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
275
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
276 static object *
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
277 string_concat(a, bb)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
278 register stringobject *a;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
279 register object *bb;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
280 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
281 register unsigned int size;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
282 register stringobject *op;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
283 if (!is_stringobject(bb)) {
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
284 err_badarg();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
285 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
286 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
287 #define b ((stringobject *)bb)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
288 /* Optimize cases with empty left or right operand */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
289 if (a->ob_size == 0) {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
290 INCREF(bb);
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 return bb;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
292 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
293 if (b->ob_size == 0) {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
294 INCREF(a);
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
295 return (object *)a;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
296 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
297 size = a->ob_size + b->ob_size;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
298 op = (stringobject *)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
299 malloc(sizeof(stringobject) + size * sizeof(char));
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
300 if (op == NULL)
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
301 return err_nomem();
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
302 op->ob_type = &Stringtype;
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
303 op->ob_size = size;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
304 #ifdef CACHE_HASH
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
305 op->ob_shash = -1;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
306 #endif
1347
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1313
diff changeset
307 NEWREF(op);
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
308 memcpy(op->ob_sval, a->ob_sval, (int) a->ob_size);
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
309 memcpy(op->ob_sval + a->ob_size, b->ob_sval, (int) b->ob_size);
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
310 op->ob_sval[size] = '\0';
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
311 return (object *) op;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
312 #undef b
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
313 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
314
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
315 static object *
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
316 string_repeat(a, n)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
317 register stringobject *a;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
318 register int n;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
319 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
320 register int i;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
321 register unsigned int size;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
322 register stringobject *op;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
323 if (n < 0)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
324 n = 0;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
325 size = a->ob_size * n;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
326 if (size == a->ob_size) {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
327 INCREF(a);
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
328 return (object *)a;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
329 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
330 op = (stringobject *)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
331 malloc(sizeof(stringobject) + size * sizeof(char));
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
332 if (op == NULL)
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
333 return err_nomem();
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
334 op->ob_type = &Stringtype;
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
335 op->ob_size = size;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
336 #ifdef CACHE_HASH
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
337 op->ob_shash = -1;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
338 #endif
1347
a22a350d3a5d * Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents: 1313
diff changeset
339 NEWREF(op);
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
340 for (i = 0; i < size; i += a->ob_size)
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
341 memcpy(op->ob_sval+i, a->ob_sval, (int) a->ob_size);
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
342 op->ob_sval[size] = '\0';
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
343 return (object *) op;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
344 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
345
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
346 /* String slice a[i:j] consists of characters a[i] ... a[j-1] */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
347
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
348 static object *
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
349 string_slice(a, i, j)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
350 register stringobject *a;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
351 register int i, j; /* May be negative! */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
352 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
353 if (i < 0)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
354 i = 0;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
355 if (j < 0)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
356 j = 0; /* Avoid signed/unsigned bug in next line */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
357 if (j > a->ob_size)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
358 j = a->ob_size;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
359 if (i == 0 && j == a->ob_size) { /* It's the same as a */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
360 INCREF(a);
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
361 return (object *)a;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
362 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
363 if (j < i)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
364 j = i;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
365 return newsizedstringobject(a->ob_sval + i, (int) (j-i));
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
366 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
367
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
368 static object *
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
369 string_item(a, i)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
370 stringobject *a;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
371 register int i;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
372 {
180
a405a31242e7 Optimized single-character strings gotten from s[i].
Guido van Rossum <guido@python.org>
parents: 158
diff changeset
373 int c;
a405a31242e7 Optimized single-character strings gotten from s[i].
Guido van Rossum <guido@python.org>
parents: 158
diff changeset
374 object *v;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
375 if (i < 0 || i >= a->ob_size) {
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
376 err_setstr(IndexError, "string index out of range");
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
377 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
378 }
180
a405a31242e7 Optimized single-character strings gotten from s[i].
Guido van Rossum <guido@python.org>
parents: 158
diff changeset
379 c = a->ob_sval[i] & UCHAR_MAX;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
380 v = (object *) characters[c];
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
381 #ifdef COUNT_ALLOCS
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
382 if (v != NULL)
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
383 one_strings++;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
384 #endif
180
a405a31242e7 Optimized single-character strings gotten from s[i].
Guido van Rossum <guido@python.org>
parents: 158
diff changeset
385 if (v == NULL) {
a405a31242e7 Optimized single-character strings gotten from s[i].
Guido van Rossum <guido@python.org>
parents: 158
diff changeset
386 v = newsizedstringobject((char *)NULL, 1);
a405a31242e7 Optimized single-character strings gotten from s[i].
Guido van Rossum <guido@python.org>
parents: 158
diff changeset
387 if (v == NULL)
a405a31242e7 Optimized single-character strings gotten from s[i].
Guido van Rossum <guido@python.org>
parents: 158
diff changeset
388 return NULL;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
389 characters[c] = (stringobject *) v;
180
a405a31242e7 Optimized single-character strings gotten from s[i].
Guido van Rossum <guido@python.org>
parents: 158
diff changeset
390 ((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
391 }
a405a31242e7 Optimized single-character strings gotten from s[i].
Guido van Rossum <guido@python.org>
parents: 158
diff changeset
392 INCREF(v);
a405a31242e7 Optimized single-character strings gotten from s[i].
Guido van Rossum <guido@python.org>
parents: 158
diff changeset
393 return v;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
394 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
395
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
396 static int
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
397 string_compare(a, b)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
398 stringobject *a, *b;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
399 {
136
6523bd2954f0 Fix stringcompare when strings contain null bytes.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
400 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
401 int min_len = (len_a < len_b) ? len_a : len_b;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
402 int cmp;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
403 if (min_len > 0) {
4458
d8759d0b5a67 Fixed compare function to do first char comparison in unsigned mode,
Guido van Rossum <guido@python.org>
parents: 4283
diff changeset
404 cmp = Py_CHARMASK(*a->ob_sval) - Py_CHARMASK(*b->ob_sval);
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
405 if (cmp == 0)
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
406 cmp = memcmp(a->ob_sval, b->ob_sval, min_len);
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
407 if (cmp != 0)
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
408 return cmp;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
409 }
136
6523bd2954f0 Fix stringcompare when strings contain null bytes.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
410 return (len_a < len_b) ? -1 : (len_a > len_b) ? 1 : 0;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
411 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
412
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
413 static long
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
414 string_hash(a)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
415 stringobject *a;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
416 {
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
417 register int len;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
418 register unsigned char *p;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
419 register long x;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
420
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
421 #ifdef CACHE_HASH
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
422 if (a->ob_shash != -1)
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
423 return a->ob_shash;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
424 #endif
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
425 len = a->ob_size;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
426 p = (unsigned char *) a->ob_sval;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
427 x = *p << 7;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
428 while (--len >= 0)
4283
839f72610ae1 Multiply by 1000003 instead of 3 in string hach
Guido van Rossum <guido@python.org>
parents: 3586
diff changeset
429 x = (1000003*x) ^ *p++;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
430 x ^= a->ob_size;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
431 if (x == -1)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
432 x = -2;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
433 #ifdef CACHE_HASH
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
434 a->ob_shash = x;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
435 #endif
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
436 return x;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
437 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
438
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
439 static sequence_methods string_as_sequence = {
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
440 (inquiry)string_length, /*sq_length*/
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
441 (binaryfunc)string_concat, /*sq_concat*/
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
442 (intargfunc)string_repeat, /*sq_repeat*/
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
443 (intargfunc)string_item, /*sq_item*/
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
444 (intintargfunc)string_slice, /*sq_slice*/
259
6321d3dc57e0 Fix comments in string_as_sequence
Guido van Rossum <guido@python.org>
parents: 180
diff changeset
445 0, /*sq_ass_item*/
6321d3dc57e0 Fix comments in string_as_sequence
Guido van Rossum <guido@python.org>
parents: 180
diff changeset
446 0, /*sq_ass_slice*/
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
447 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
448
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
449 typeobject Stringtype = {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
450 OB_HEAD_INIT(&Typetype)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
451 0,
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
452 "string",
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
453 sizeof(stringobject),
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
454 sizeof(char),
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
455 (destructor)string_dealloc, /*tp_dealloc*/
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
456 (printfunc)string_print, /*tp_print*/
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
457 0, /*tp_getattr*/
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
458 0, /*tp_setattr*/
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
459 (cmpfunc)string_compare, /*tp_compare*/
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
460 (reprfunc)string_repr, /*tp_repr*/
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
461 0, /*tp_as_number*/
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
462 &string_as_sequence, /*tp_as_sequence*/
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
463 0, /*tp_as_mapping*/
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
464 (hashfunc)string_hash, /*tp_hash*/
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
465 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
466
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
467 void
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
468 joinstring(pv, w)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
469 register object **pv;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
470 register object *w;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
471 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
472 register object *v;
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
473 if (*pv == NULL)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
474 return;
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
475 if (w == NULL || !is_stringobject(*pv)) {
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
476 DECREF(*pv);
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
477 *pv = NULL;
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
478 return;
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
479 }
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
480 v = string_concat((stringobject *) *pv, w);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
481 DECREF(*pv);
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
482 *pv = v;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
483 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
484
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
485 void
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
486 joinstring_decref(pv, w)
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
487 register object **pv;
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
488 register object *w;
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
489 {
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
490 joinstring(pv, w);
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
491 XDECREF(w);
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
492 }
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
493
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
494
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
495 /* The following function breaks the notion that strings are immutable:
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
496 it changes the size of a string. We get away with this only if there
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
497 is only one module referencing the object. You can also think of it
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
498 as creating a new string object and destroying the old one, only
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
499 more efficiently. In any case, don't use this if the string may
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
500 already be known to some other part of the code... */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
501
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
502 int
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
503 resizestring(pv, newsize)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
504 object **pv;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
505 int newsize;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
506 {
71
ac4522e99ca9 Fixed resizestring() to work if reference tracing is turned on.
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
507 register object *v;
ac4522e99ca9 Fixed resizestring() to work if reference tracing is turned on.
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
508 register stringobject *sv;
ac4522e99ca9 Fixed resizestring() to work if reference tracing is turned on.
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
509 v = *pv;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
510 if (!is_stringobject(v) || v->ob_refcnt != 1) {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
511 *pv = 0;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
512 DECREF(v);
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
513 err_badcall();
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
514 return -1;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
515 }
71
ac4522e99ca9 Fixed resizestring() to work if reference tracing is turned on.
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
516 /* XXX UNREF/NEWREF interface should be more symmetrical */
3586
36cc3ee54f28 new debugger symbol names
Guido van Rossum <guido@python.org>
parents: 3571
diff changeset
517 #ifdef Py_REF_DEBUG
2612
ca6b60d4ea90 fix dusty debugging macros
Guido van Rossum <guido@python.org>
parents: 2504
diff changeset
518 --_Py_RefTotal;
71
ac4522e99ca9 Fixed resizestring() to work if reference tracing is turned on.
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
519 #endif
ac4522e99ca9 Fixed resizestring() to work if reference tracing is turned on.
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
520 UNREF(v);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
521 *pv = (object *)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
522 realloc((char *)v,
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
523 sizeof(stringobject) + newsize * sizeof(char));
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
524 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
525 DEL(v);
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
526 err_nomem();
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
527 return -1;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
528 }
71
ac4522e99ca9 Fixed resizestring() to work if reference tracing is turned on.
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
529 NEWREF(*pv);
ac4522e99ca9 Fixed resizestring() to work if reference tracing is turned on.
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
530 sv = (stringobject *) *pv;
ac4522e99ca9 Fixed resizestring() to work if reference tracing is turned on.
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
531 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
532 sv->ob_sval[newsize] = '\0';
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
533 return 0;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
534 }
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
535
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
536 /* Helpers for formatstring */
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
537
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
538 static object *
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
539 getnextarg(args, arglen, p_argidx)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
540 object *args;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
541 int arglen;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
542 int *p_argidx;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
543 {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
544 int argidx = *p_argidx;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
545 if (argidx < arglen) {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
546 (*p_argidx)++;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
547 if (arglen < 0)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
548 return args;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
549 else
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
550 return gettupleitem(args, argidx);
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
551 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
552 err_setstr(TypeError, "not enough arguments for format string");
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
553 return NULL;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
554 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
555
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
556 #define F_LJUST (1<<0)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
557 #define F_SIGN (1<<1)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
558 #define F_BLANK (1<<2)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
559 #define F_ALT (1<<3)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
560 #define F_ZERO (1<<4)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
561
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
562 extern double fabs PROTO((double));
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
563
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
564 static char *
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
565 formatfloat(flags, prec, type, v)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
566 int flags;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
567 int prec;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
568 int type;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
569 object *v;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
570 {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
571 char fmt[20];
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
572 static char buf[120];
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
573 double x;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
574 if (!getargs(v, "d;float argument required", &x))
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
575 return NULL;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
576 if (prec < 0)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
577 prec = 6;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
578 if (prec > 50)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
579 prec = 50; /* Arbitrary limitation */
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
580 if (type == 'f' && fabs(x)/1e25 >= 1e25)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
581 type = 'g';
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
582 sprintf(fmt, "%%%s.%d%c", (flags&F_ALT) ? "#" : "", prec, type);
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
583 sprintf(buf, fmt, x);
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
584 return buf;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
585 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
586
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
587 static char *
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
588 formatint(flags, prec, type, v)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
589 int flags;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
590 int prec;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
591 int type;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
592 object *v;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
593 {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
594 char fmt[20];
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
595 static char buf[50];
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
596 long x;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
597 if (!getargs(v, "l;int argument required", &x))
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
598 return NULL;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
599 if (prec < 0)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
600 prec = 1;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
601 sprintf(fmt, "%%%s.%dl%c", (flags&F_ALT) ? "#" : "", prec, type);
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
602 sprintf(buf, fmt, x);
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
603 return buf;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
604 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
605
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
606 static char *
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
607 formatchar(v)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
608 object *v;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
609 {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
610 static char buf[2];
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
611 if (is_stringobject(v)) {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
612 if (!getargs(v, "c;%c requires int or char", &buf[0]))
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
613 return NULL;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
614 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
615 else {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
616 if (!getargs(v, "b;%c requires int or char", &buf[0]))
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
617 return NULL;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
618 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
619 buf[1] = '\0';
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
620 return buf;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
621 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
622
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
623
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
624 /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) */
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
625
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
626 object *
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
627 formatstring(format, args)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
628 object *format;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
629 object *args;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
630 {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
631 char *fmt, *res;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
632 int fmtcnt, rescnt, reslen, arglen, argidx;
3570
977ff1ace070 Fix obscure bug in string%mapping where the mapping creates its items
Guido van Rossum <guido@python.org>
parents: 2612
diff changeset
633 int args_owned = 0;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
634 object *result;
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
635 object *dict = NULL;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
636 if (format == NULL || !is_stringobject(format) || args == NULL) {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
637 err_badcall();
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
638 return NULL;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
639 }
1282
e0464e7115ca * pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
640 fmt = getstringvalue(format);
e0464e7115ca * pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
641 fmtcnt = getstringsize(format);
e0464e7115ca * pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
642 reslen = rescnt = fmtcnt + 100;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
643 result = newsizedstringobject((char *)NULL, reslen);
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
644 if (result == NULL)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
645 return NULL;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
646 res = getstringvalue(result);
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
647 if (is_tupleobject(args)) {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
648 arglen = gettuplesize(args);
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
649 argidx = 0;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
650 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
651 else {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
652 arglen = -1;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
653 argidx = -2;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
654 }
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
655 if (args->ob_type->tp_as_mapping)
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
656 dict = args;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
657 while (--fmtcnt >= 0) {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
658 if (*fmt != '%') {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
659 if (--rescnt < 0) {
1282
e0464e7115ca * pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
660 rescnt = fmtcnt + 100;
e0464e7115ca * pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
661 reslen += rescnt;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
662 if (resizestring(&result, reslen) < 0)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
663 return NULL;
1282
e0464e7115ca * pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
664 res = getstringvalue(result) + reslen - rescnt;
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
665 --rescnt;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
666 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
667 *res++ = *fmt++;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
668 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
669 else {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
670 /* Got a format specifier */
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
671 int flags = 0;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
672 char *fmtstart = fmt++;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
673 int width = -1;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
674 int prec = -1;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
675 int size = 0;
1408
18df674539da Three micro fixes to formatstring
Guido van Rossum <guido@python.org>
parents: 1380
diff changeset
676 int c = '\0';
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
677 int fill;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
678 object *v;
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
679 object *temp = NULL;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
680 char *buf;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
681 int sign;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
682 int len;
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
683 if (*fmt == '(') {
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
684 char *keystart;
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
685 int keylen;
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
686 object *key;
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
687
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
688 if (dict == NULL) {
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
689 err_setstr(TypeError,
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
690 "format requires a mapping");
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
691 goto error;
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
692 }
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
693 ++fmt;
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
694 --fmtcnt;
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
695 keystart = fmt;
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
696 while (--fmtcnt >= 0 && *fmt != ')')
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
697 fmt++;
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
698 keylen = fmt - keystart;
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
699 ++fmt;
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
700 if (fmtcnt < 0) {
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
701 err_setstr(ValueError,
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
702 "incomplete format key");
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
703 goto error;
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
704 }
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
705 key = newsizedstringobject(keystart, keylen);
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
706 if (key == NULL)
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
707 goto error;
3570
977ff1ace070 Fix obscure bug in string%mapping where the mapping creates its items
Guido van Rossum <guido@python.org>
parents: 2612
diff changeset
708 if (args_owned) {
977ff1ace070 Fix obscure bug in string%mapping where the mapping creates its items
Guido van Rossum <guido@python.org>
parents: 2612
diff changeset
709 DECREF(args);
977ff1ace070 Fix obscure bug in string%mapping where the mapping creates its items
Guido van Rossum <guido@python.org>
parents: 2612
diff changeset
710 args_owned = 0;
977ff1ace070 Fix obscure bug in string%mapping where the mapping creates its items
Guido van Rossum <guido@python.org>
parents: 2612
diff changeset
711 }
977ff1ace070 Fix obscure bug in string%mapping where the mapping creates its items
Guido van Rossum <guido@python.org>
parents: 2612
diff changeset
712 args = PyObject_GetItem(dict, key);
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
713 DECREF(key);
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
714 if (args == NULL) {
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
715 goto error;
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
716 }
3570
977ff1ace070 Fix obscure bug in string%mapping where the mapping creates its items
Guido van Rossum <guido@python.org>
parents: 2612
diff changeset
717 args_owned = 1;
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
718 arglen = -1;
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
719 argidx = -2;
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
720 }
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
721 while (--fmtcnt >= 0) {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
722 switch (c = *fmt++) {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
723 case '-': flags |= F_LJUST; continue;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
724 case '+': flags |= F_SIGN; continue;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
725 case ' ': flags |= F_BLANK; continue;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
726 case '#': flags |= F_ALT; continue;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
727 case '0': flags |= F_ZERO; continue;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
728 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
729 break;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
730 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
731 if (c == '*') {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
732 v = getnextarg(args, arglen, &argidx);
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
733 if (v == NULL)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
734 goto error;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
735 if (!is_intobject(v)) {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
736 err_setstr(TypeError, "* wants int");
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
737 goto error;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
738 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
739 width = getintvalue(v);
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
740 if (width < 0)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
741 width = 0;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
742 if (--fmtcnt >= 0)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
743 c = *fmt++;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
744 }
2308
14b126d0b5be use Py_CHARMASK; and don't check for neg. float to the float power here
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
745 else if (c >= 0 && isdigit(c)) {
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
746 width = c - '0';
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
747 while (--fmtcnt >= 0) {
2308
14b126d0b5be use Py_CHARMASK; and don't check for neg. float to the float power here
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
748 c = Py_CHARMASK(*fmt++);
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
749 if (!isdigit(c))
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
750 break;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
751 if ((width*10) / 10 != width) {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
752 err_setstr(ValueError,
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
753 "width too big");
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
754 goto error;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
755 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
756 width = width*10 + (c - '0');
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
757 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
758 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
759 if (c == '.') {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
760 prec = 0;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
761 if (--fmtcnt >= 0)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
762 c = *fmt++;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
763 if (c == '*') {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
764 v = getnextarg(args, arglen, &argidx);
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
765 if (v == NULL)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
766 goto error;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
767 if (!is_intobject(v)) {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
768 err_setstr(TypeError,
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
769 "* wants int");
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
770 goto error;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
771 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
772 prec = getintvalue(v);
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
773 if (prec < 0)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
774 prec = 0;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
775 if (--fmtcnt >= 0)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
776 c = *fmt++;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
777 }
2308
14b126d0b5be use Py_CHARMASK; and don't check for neg. float to the float power here
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
778 else if (c >= 0 && isdigit(c)) {
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
779 prec = c - '0';
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
780 while (--fmtcnt >= 0) {
2308
14b126d0b5be use Py_CHARMASK; and don't check for neg. float to the float power here
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
781 c = Py_CHARMASK(*fmt++);
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
782 if (!isdigit(c))
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
783 break;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
784 if ((prec*10) / 10 != prec) {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
785 err_setstr(ValueError,
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
786 "prec too big");
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
787 goto error;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
788 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
789 prec = prec*10 + (c - '0');
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
790 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
791 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
792 } /* prec */
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
793 if (fmtcnt >= 0) {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
794 if (c == 'h' || c == 'l' || c == 'L') {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
795 size = c;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
796 if (--fmtcnt >= 0)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
797 c = *fmt++;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
798 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
799 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
800 if (fmtcnt < 0) {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
801 err_setstr(ValueError, "incomplete format");
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
802 goto error;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
803 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
804 if (c != '%') {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
805 v = getnextarg(args, arglen, &argidx);
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
806 if (v == NULL)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
807 goto error;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
808 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
809 sign = 0;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
810 fill = ' ';
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
811 switch (c) {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
812 case '%':
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
813 buf = "%";
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
814 len = 1;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
815 break;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
816 case 's':
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
817 temp = strobject(v);
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
818 if (temp == NULL)
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
819 goto error;
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
820 buf = getstringvalue(temp);
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
821 len = getstringsize(temp);
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
822 if (prec >= 0 && len > prec)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
823 len = prec;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
824 break;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
825 case 'i':
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
826 case 'd':
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
827 case 'u':
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
828 case 'o':
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
829 case 'x':
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
830 case 'X':
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
831 if (c == 'i')
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
832 c = 'd';
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
833 buf = formatint(flags, prec, c, v);
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
834 if (buf == NULL)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
835 goto error;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
836 len = strlen(buf);
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
837 sign = (c == 'd');
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
838 if (flags&F_ZERO)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
839 fill = '0';
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
840 break;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
841 case 'e':
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
842 case 'E':
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
843 case 'f':
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
844 case 'g':
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
845 case 'G':
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
846 buf = formatfloat(flags, prec, c, v);
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
847 if (buf == NULL)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
848 goto error;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
849 len = strlen(buf);
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
850 sign = 1;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
851 if (flags&F_ZERO)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
852 fill = '0';
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
853 break;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
854 case 'c':
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
855 buf = formatchar(v);
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
856 if (buf == NULL)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
857 goto error;
1408
18df674539da Three micro fixes to formatstring
Guido van Rossum <guido@python.org>
parents: 1380
diff changeset
858 len = 1;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
859 break;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
860 default:
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
861 err_setstr(ValueError,
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
862 "unsupported format character");
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
863 goto error;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
864 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
865 if (sign) {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
866 if (*buf == '-' || *buf == '+') {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
867 sign = *buf++;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
868 len--;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
869 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
870 else if (flags & F_SIGN)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
871 sign = '+';
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
872 else if (flags & F_BLANK)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
873 sign = ' ';
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
874 else
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
875 sign = '\0';
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
876 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
877 if (width < len)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
878 width = len;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
879 if (rescnt < width + (sign != '\0')) {
1282
e0464e7115ca * pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
880 reslen -= rescnt;
e0464e7115ca * pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
881 rescnt = width + fmtcnt + 100;
e0464e7115ca * pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
882 reslen += rescnt;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
883 if (resizestring(&result, reslen) < 0)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
884 return NULL;
1282
e0464e7115ca * pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
885 res = getstringvalue(result) + reslen - rescnt;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
886 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
887 if (sign) {
1409
f5b1ddfddba8 Fix the fix :-(
Guido van Rossum <guido@python.org>
parents: 1408
diff changeset
888 if (fill != ' ')
f5b1ddfddba8 Fix the fix :-(
Guido van Rossum <guido@python.org>
parents: 1408
diff changeset
889 *res++ = sign;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
890 rescnt--;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
891 if (width > len)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
892 width--;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
893 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
894 if (width > len && !(flags&F_LJUST)) {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
895 do {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
896 --rescnt;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
897 *res++ = fill;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
898 } while (--width > len);
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
899 }
1409
f5b1ddfddba8 Fix the fix :-(
Guido van Rossum <guido@python.org>
parents: 1408
diff changeset
900 if (sign && fill == ' ')
1408
18df674539da Three micro fixes to formatstring
Guido van Rossum <guido@python.org>
parents: 1380
diff changeset
901 *res++ = sign;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
902 memcpy(res, buf, len);
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
903 res += len;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
904 rescnt -= len;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
905 while (--width >= len) {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
906 --rescnt;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
907 *res++ = ' ';
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
908 }
2308
14b126d0b5be use Py_CHARMASK; and don't check for neg. float to the float power here
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
909 if (dict && (argidx < arglen) && c != '%') {
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
910 err_setstr(TypeError,
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
911 "not all arguments converted");
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
912 goto error;
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
913 }
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
914 XDECREF(temp);
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
915 } /* '%' */
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
916 } /* until end */
2432
445bea8ceab1 don't complain about too many args if arg is a dict
Guido van Rossum <guido@python.org>
parents: 2308
diff changeset
917 if (argidx < arglen && !dict) {
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
918 err_setstr(TypeError, "not all arguments converted");
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
919 goto error;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
920 }
3570
977ff1ace070 Fix obscure bug in string%mapping where the mapping creates its items
Guido van Rossum <guido@python.org>
parents: 2612
diff changeset
921 if (args_owned)
977ff1ace070 Fix obscure bug in string%mapping where the mapping creates its items
Guido van Rossum <guido@python.org>
parents: 2612
diff changeset
922 DECREF(args);
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
923 resizestring(&result, reslen - rescnt);
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
924 return result;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
925 error:
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
926 DECREF(result);
3570
977ff1ace070 Fix obscure bug in string%mapping where the mapping creates its items
Guido van Rossum <guido@python.org>
parents: 2612
diff changeset
927 if (args_owned)
977ff1ace070 Fix obscure bug in string%mapping where the mapping creates its items
Guido van Rossum <guido@python.org>
parents: 2612
diff changeset
928 DECREF(args);
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
929 return NULL;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
930 }