annotate Objects/stringobject.c @ 6286:82cd2baa4136 legacy-trunk

Patch submitted by Brad Howes (with one bug fixed by me): allow arbitrary nested parens in a %(...)X style format. #Also folded two lines and added more detail to the error message for #unsupported format character.
author Guido van Rossum <guido@python.org>
date Mon, 08 Sep 1997 18:30:11 +0000
parents d77d3b8e86b2
children 09963a2a24db
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
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
34 #include "Python.h"
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
35
5717
3c67436a28e6 Use #include "mymath.h" instead of declaring fabs() explicitly.
Guido van Rossum <guido@python.org>
parents: 5410
diff changeset
36 #include "mymath.h"
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
37 #include <ctype.h>
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
38
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
39 #ifdef COUNT_ALLOCS
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
40 int null_strings, one_strings;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
41 #endif
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
42
1908
0437738279a8 * Include/classobject.h, Objects/classobject.c, Python/ceval.c:
Guido van Rossum <guido@python.org>
parents: 1847
diff changeset
43 #ifdef HAVE_LIMITS_H
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
44 #include <limits.h>
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
45 #else
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
46 #ifndef UCHAR_MAX
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
47 #define UCHAR_MAX 255
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 #endif
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
50
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
51 static PyStringObject *characters[UCHAR_MAX + 1];
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1368
diff changeset
52 #ifndef DONT_SHARE_SHORT_STRINGS
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
53 static PyStringObject *nullstring;
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1368
diff changeset
54 #endif
1356
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 /*
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
57 Newsizedstringobject() and newstringobject() try in certain cases
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
58 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
59 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
60 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
61 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
62 newstringobject() this is always the case, for
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
63 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
64 not NULL.
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
65 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
66 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
67 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
68 newsizedstringobject() with a NULL first argument, because in the
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
69 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
70 */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
71 PyObject *
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
72 PyString_FromStringAndSize(str, size)
4654
df81856db58c Add const to error and newstring functions
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
73 const char *str;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 int size;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
76 register PyStringObject *op;
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1368
diff changeset
77 #ifndef DONT_SHARE_SHORT_STRINGS
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
78 if (size == 0 && (op = nullstring) != NULL) {
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
79 #ifdef COUNT_ALLOCS
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
80 null_strings++;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
81 #endif
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
82 Py_INCREF(op);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
83 return (PyObject *)op;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
84 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
85 if (size == 1 && str != NULL &&
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
86 (op = characters[*str & UCHAR_MAX]) != NULL)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
87 {
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
88 #ifdef COUNT_ALLOCS
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
89 one_strings++;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
90 #endif
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
91 Py_INCREF(op);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
92 return (PyObject *)op;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
93 }
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1368
diff changeset
94 #endif /* DONT_SHARE_SHORT_STRINGS */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
95 op = (PyStringObject *)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
96 malloc(sizeof(PyStringObject) + size * sizeof(char));
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
97 if (op == NULL)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
98 return PyErr_NoMemory();
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
99 op->ob_type = &PyString_Type;
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
100 op->ob_size = size;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
101 #ifdef CACHE_HASH
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
102 op->ob_shash = -1;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
103 #endif
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
104 #ifdef INTERN_STRINGS
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
105 op->ob_sinterned = NULL;
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
106 #endif
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
107 _Py_NewReference(op);
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
108 if (str != NULL)
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
109 memcpy(op->ob_sval, str, size);
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
110 op->ob_sval[size] = '\0';
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1368
diff changeset
111 #ifndef DONT_SHARE_SHORT_STRINGS
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
112 if (size == 0) {
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
113 nullstring = op;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
114 Py_INCREF(op);
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
115 } else if (size == 1 && str != NULL) {
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
116 characters[*str & UCHAR_MAX] = op;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
117 Py_INCREF(op);
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
118 }
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1368
diff changeset
119 #endif
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
120 return (PyObject *) op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
122
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
123 PyObject *
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
124 PyString_FromString(str)
4654
df81856db58c Add const to error and newstring functions
Guido van Rossum <guido@python.org>
parents: 4588
diff changeset
125 const char *str;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 register unsigned int size = strlen(str);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
128 register PyStringObject *op;
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1368
diff changeset
129 #ifndef DONT_SHARE_SHORT_STRINGS
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
130 if (size == 0 && (op = nullstring) != NULL) {
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
131 #ifdef COUNT_ALLOCS
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
132 null_strings++;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
133 #endif
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
134 Py_INCREF(op);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
135 return (PyObject *)op;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
136 }
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
137 if (size == 1 && (op = characters[*str & UCHAR_MAX]) != NULL) {
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
138 #ifdef COUNT_ALLOCS
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
139 one_strings++;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
140 #endif
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
141 Py_INCREF(op);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
142 return (PyObject *)op;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
143 }
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1368
diff changeset
144 #endif /* DONT_SHARE_SHORT_STRINGS */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
145 op = (PyStringObject *)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
146 malloc(sizeof(PyStringObject) + size * sizeof(char));
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
147 if (op == NULL)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
148 return PyErr_NoMemory();
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
149 op->ob_type = &PyString_Type;
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
150 op->ob_size = size;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
151 #ifdef CACHE_HASH
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
152 op->ob_shash = -1;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
153 #endif
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
154 #ifdef INTERN_STRINGS
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
155 op->ob_sinterned = NULL;
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
156 #endif
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
157 _Py_NewReference(op);
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
158 strcpy(op->ob_sval, str);
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1368
diff changeset
159 #ifndef DONT_SHARE_SHORT_STRINGS
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
160 if (size == 0) {
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
161 nullstring = op;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
162 Py_INCREF(op);
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
163 } else if (size == 1) {
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
164 characters[*str & UCHAR_MAX] = op;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
165 Py_INCREF(op);
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
166 }
1380
a4aa83012af3 Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1368
diff changeset
167 #endif
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
168 return (PyObject *) op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
170
1313
6ee380349c84 * Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents: 1282
diff changeset
171 static void
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
172 string_dealloc(op)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
173 PyObject *op;
750
a5e6881cebf0 lint fix
Guido van Rossum <guido@python.org>
parents: 287
diff changeset
174 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
175 PyMem_DEL(op);
750
a5e6881cebf0 lint fix
Guido van Rossum <guido@python.org>
parents: 287
diff changeset
176 }
a5e6881cebf0 lint fix
Guido van Rossum <guido@python.org>
parents: 287
diff changeset
177
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1908
diff changeset
178 int
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
179 PyString_Size(op)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
180 register PyObject *op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
182 if (!PyString_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
183 PyErr_BadInternalCall();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 return -1;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
186 return ((PyStringObject *)op) -> ob_size;
6
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
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 /*const*/ char *
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
190 PyString_AsString(op)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
191 register PyObject *op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
193 if (!PyString_Check(op)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
194 PyErr_BadInternalCall();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
196 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
197 return ((PyStringObject *)op) -> ob_sval;
6
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
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 /* Methods */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
201
287
07efda2951a8 printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 259
diff changeset
202 static int
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
203 string_print(op, fp, flags)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
204 PyStringObject *op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 FILE *fp;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 int flags;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 int i;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 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
210 int quote;
287
07efda2951a8 printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 259
diff changeset
211 /* XXX Ought to check for interrupts when writing long strings */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
212 if (flags & Py_PRINT_RAW) {
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 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
214 return 0;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 }
1368
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
216
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
217 /* 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
218 quote = '\'';
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
219 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
220 quote = '"';
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
221
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
222 fputc(quote, fp);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
223 for (i = 0; i < op->ob_size; i++) {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
224 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
225 if (c == quote || c == '\\')
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 fprintf(fp, "\\%c", c);
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
227 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
228 fprintf(fp, "\\%03o", c & 0377);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 else
1368
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
230 fputc(c, fp);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
231 }
1368
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
232 fputc(quote, fp);
287
07efda2951a8 printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 259
diff changeset
233 return 0;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
234 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
235
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
236 static PyObject *
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
237 string_repr(op)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
238 register PyStringObject *op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
239 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
240 /* XXX overflow? */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 int newsize = 2 + 4 * op->ob_size * sizeof(char);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
242 PyObject *v = PyString_FromStringAndSize((char *)NULL, newsize);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
243 if (v == NULL) {
287
07efda2951a8 printobject now returns an error code
Guido van Rossum <guido@python.org>
parents: 259
diff changeset
244 return NULL;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 else {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
247 register int i;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 register char c;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
249 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
250 int quote;
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
251
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
252 /* 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
253 quote = '\'';
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
254 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
255 quote = '"';
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
256
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
257 p = ((PyStringObject *)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
258 *p++ = quote;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
259 for (i = 0; i < op->ob_size; i++) {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
260 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
261 if (c == quote || c == '\\')
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
262 *p++ = '\\', *p++ = c;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
263 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
264 sprintf(p, "\\%03o", c & 0377);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
265 while (*p != '\0')
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 p++;
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 else
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
269 *p++ = c;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
270 }
1368
4c8e5fcb3301 Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents: 1356
diff changeset
271 *p++ = quote;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
272 *p = '\0';
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
273 _PyString_Resize(
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
274 &v, (int) (p - ((PyStringObject *)v)->ob_sval));
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
275 return v;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
276 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
277 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
278
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
279 static int
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
280 string_length(a)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
281 PyStringObject *a;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
282 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
283 return a->ob_size;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
284 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
285
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
286 static PyObject *
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
287 string_concat(a, bb)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
288 register PyStringObject *a;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
289 register PyObject *bb;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
290 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 register unsigned int size;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
292 register PyStringObject *op;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
293 if (!PyString_Check(bb)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
294 PyErr_BadArgument();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
295 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
296 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
297 #define b ((PyStringObject *)bb)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
298 /* Optimize cases with empty left or right operand */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
299 if (a->ob_size == 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
300 Py_INCREF(bb);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
301 return bb;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
302 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
303 if (b->ob_size == 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
304 Py_INCREF(a);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
305 return (PyObject *)a;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
306 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
307 size = a->ob_size + b->ob_size;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
308 op = (PyStringObject *)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
309 malloc(sizeof(PyStringObject) + size * sizeof(char));
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
310 if (op == NULL)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
311 return PyErr_NoMemory();
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
312 op->ob_type = &PyString_Type;
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
313 op->ob_size = size;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
314 #ifdef CACHE_HASH
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
315 op->ob_shash = -1;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
316 #endif
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
317 #ifdef INTERN_STRINGS
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
318 op->ob_sinterned = NULL;
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
319 #endif
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
320 _Py_NewReference(op);
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
321 memcpy(op->ob_sval, a->ob_sval, (int) a->ob_size);
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
322 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
323 op->ob_sval[size] = '\0';
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
324 return (PyObject *) op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
325 #undef b
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
326 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
327
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
328 static PyObject *
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
329 string_repeat(a, n)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
330 register PyStringObject *a;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
331 register int n;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
332 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
333 register int i;
5288
0f1711b7d367 Tweaks to keep the Microsoft compiler quiet.
Guido van Rossum <guido@python.org>
parents: 5098
diff changeset
334 register int size;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
335 register PyStringObject *op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
336 if (n < 0)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
337 n = 0;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
338 size = a->ob_size * n;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
339 if (size == a->ob_size) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
340 Py_INCREF(a);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
341 return (PyObject *)a;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
342 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
343 op = (PyStringObject *)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
344 malloc(sizeof(PyStringObject) + size * sizeof(char));
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
345 if (op == NULL)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
346 return PyErr_NoMemory();
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
347 op->ob_type = &PyString_Type;
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
348 op->ob_size = size;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
349 #ifdef CACHE_HASH
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
350 op->ob_shash = -1;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
351 #endif
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
352 #ifdef INTERN_STRINGS
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
353 op->ob_sinterned = NULL;
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
354 #endif
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
355 _Py_NewReference(op);
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
356 for (i = 0; i < size; i += a->ob_size)
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
357 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
358 op->ob_sval[size] = '\0';
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
359 return (PyObject *) op;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
360 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
361
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
362 /* 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
363
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
364 static PyObject *
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
365 string_slice(a, i, j)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
366 register PyStringObject *a;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
367 register int i, j; /* May be negative! */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
368 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
369 if (i < 0)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
370 i = 0;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
371 if (j < 0)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
372 j = 0; /* Avoid signed/unsigned bug in next line */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
373 if (j > a->ob_size)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
374 j = a->ob_size;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
375 if (i == 0 && j == a->ob_size) { /* It's the same as a */
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
376 Py_INCREF(a);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
377 return (PyObject *)a;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
378 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
379 if (j < i)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
380 j = i;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
381 return PyString_FromStringAndSize(a->ob_sval + i, (int) (j-i));
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
382 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
383
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
384 static PyObject *
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
385 string_item(a, i)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
386 PyStringObject *a;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
387 register int i;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
388 {
180
a405a31242e7 Optimized single-character strings gotten from s[i].
Guido van Rossum <guido@python.org>
parents: 158
diff changeset
389 int c;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
390 PyObject *v;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
391 if (i < 0 || i >= a->ob_size) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
392 PyErr_SetString(PyExc_IndexError, "string index out of range");
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
393 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
394 }
180
a405a31242e7 Optimized single-character strings gotten from s[i].
Guido van Rossum <guido@python.org>
parents: 158
diff changeset
395 c = a->ob_sval[i] & UCHAR_MAX;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
396 v = (PyObject *) characters[c];
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
397 #ifdef COUNT_ALLOCS
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
398 if (v != NULL)
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
399 one_strings++;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
400 #endif
180
a405a31242e7 Optimized single-character strings gotten from s[i].
Guido van Rossum <guido@python.org>
parents: 158
diff changeset
401 if (v == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
402 v = PyString_FromStringAndSize((char *)NULL, 1);
180
a405a31242e7 Optimized single-character strings gotten from s[i].
Guido van Rossum <guido@python.org>
parents: 158
diff changeset
403 if (v == NULL)
a405a31242e7 Optimized single-character strings gotten from s[i].
Guido van Rossum <guido@python.org>
parents: 158
diff changeset
404 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
405 characters[c] = (PyStringObject *) v;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
406 ((PyStringObject *)v)->ob_sval[0] = c;
180
a405a31242e7 Optimized single-character strings gotten from s[i].
Guido van Rossum <guido@python.org>
parents: 158
diff changeset
407 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
408 Py_INCREF(v);
180
a405a31242e7 Optimized single-character strings gotten from s[i].
Guido van Rossum <guido@python.org>
parents: 158
diff changeset
409 return v;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
410 }
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 static int
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
413 string_compare(a, b)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
414 PyStringObject *a, *b;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
415 {
136
6523bd2954f0 Fix stringcompare when strings contain null bytes.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
416 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
417 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
418 int cmp;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
419 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
420 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
421 if (cmp == 0)
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
422 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
423 if (cmp != 0)
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
424 return cmp;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
425 }
136
6523bd2954f0 Fix stringcompare when strings contain null bytes.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
426 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
427 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
428
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
429 static long
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
430 string_hash(a)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
431 PyStringObject *a;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
432 {
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
433 register int len;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
434 register unsigned char *p;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
435 register long x;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
436
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
437 #ifdef CACHE_HASH
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
438 if (a->ob_shash != -1)
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
439 return a->ob_shash;
5098
a44586bd9fab Slight tweak: in string_hash(), if the hash hasn't been computed yet,
Guido van Rossum <guido@python.org>
parents: 5057
diff changeset
440 #ifdef INTERN_STRINGS
a44586bd9fab Slight tweak: in string_hash(), if the hash hasn't been computed yet,
Guido van Rossum <guido@python.org>
parents: 5057
diff changeset
441 if (a->ob_sinterned != NULL)
a44586bd9fab Slight tweak: in string_hash(), if the hash hasn't been computed yet,
Guido van Rossum <guido@python.org>
parents: 5057
diff changeset
442 return (a->ob_shash =
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
443 ((PyStringObject *)(a->ob_sinterned))->ob_shash);
5098
a44586bd9fab Slight tweak: in string_hash(), if the hash hasn't been computed yet,
Guido van Rossum <guido@python.org>
parents: 5057
diff changeset
444 #endif
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
445 #endif
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
446 len = a->ob_size;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
447 p = (unsigned char *) a->ob_sval;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
448 x = *p << 7;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
449 while (--len >= 0)
4283
839f72610ae1 Multiply by 1000003 instead of 3 in string hach
Guido van Rossum <guido@python.org>
parents: 3586
diff changeset
450 x = (1000003*x) ^ *p++;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
451 x ^= a->ob_size;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
452 if (x == -1)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
453 x = -2;
1356
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
454 #ifdef CACHE_HASH
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
455 a->ob_shash = x;
043da19302ef Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents: 1347
diff changeset
456 #endif
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
457 return x;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
458 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1252
diff changeset
459
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
460 static int
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
461 string_buffer_getreadbuf(self, index, ptr)
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
462 PyStringObject *self;
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
463 int index;
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
464 const void **ptr;
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
465 {
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
466 if ( index != 0 ) {
6286
82cd2baa4136 Patch submitted by Brad Howes (with one bug fixed by me): allow
Guido van Rossum <guido@python.org>
parents: 5935
diff changeset
467 PyErr_SetString(PyExc_SystemError,
82cd2baa4136 Patch submitted by Brad Howes (with one bug fixed by me): allow
Guido van Rossum <guido@python.org>
parents: 5935
diff changeset
468 "Accessing non-existent string segment");
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
469 return -1;
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
470 }
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
471 *ptr = (void *)self->ob_sval;
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
472 return self->ob_size;
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
473 }
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
474
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
475 static int
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
476 string_buffer_getwritebuf(self, index, ptr)
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
477 PyStringObject *self;
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
478 int index;
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
479 const void **ptr;
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
480 {
6286
82cd2baa4136 Patch submitted by Brad Howes (with one bug fixed by me): allow
Guido van Rossum <guido@python.org>
parents: 5935
diff changeset
481 PyErr_SetString(PyExc_TypeError,
82cd2baa4136 Patch submitted by Brad Howes (with one bug fixed by me): allow
Guido van Rossum <guido@python.org>
parents: 5935
diff changeset
482 "Cannot use string as modifyable buffer");
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
483 return -1;
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
484 }
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
485
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
486 static int
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
487 string_buffer_getsegcount(self, lenp)
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
488 PyStringObject *self;
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
489 int *lenp;
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
490 {
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
491 if ( lenp )
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
492 *lenp = self->ob_size;
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
493 return 1;
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
494 }
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
495
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
496 static PySequenceMethods string_as_sequence = {
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
497 (inquiry)string_length, /*sq_length*/
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
498 (binaryfunc)string_concat, /*sq_concat*/
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
499 (intargfunc)string_repeat, /*sq_repeat*/
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
500 (intargfunc)string_item, /*sq_item*/
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
501 (intintargfunc)string_slice, /*sq_slice*/
259
6321d3dc57e0 Fix comments in string_as_sequence
Guido van Rossum <guido@python.org>
parents: 180
diff changeset
502 0, /*sq_ass_item*/
6321d3dc57e0 Fix comments in string_as_sequence
Guido van Rossum <guido@python.org>
parents: 180
diff changeset
503 0, /*sq_ass_slice*/
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
504 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
505
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
506 static PyBufferProcs string_as_buffer = {
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
507 (getreadbufferproc)string_buffer_getreadbuf,
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
508 (getwritebufferproc)string_buffer_getwritebuf,
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
509 (getsegcountproc)string_buffer_getsegcount,
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
510 };
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
511
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
512 PyTypeObject PyString_Type = {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
513 PyObject_HEAD_INIT(&PyType_Type)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
514 0,
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
515 "string",
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
516 sizeof(PyStringObject),
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
517 sizeof(char),
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
518 (destructor)string_dealloc, /*tp_dealloc*/
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
519 (printfunc)string_print, /*tp_print*/
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
520 0, /*tp_getattr*/
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
521 0, /*tp_setattr*/
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
522 (cmpfunc)string_compare, /*tp_compare*/
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
523 (reprfunc)string_repr, /*tp_repr*/
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
524 0, /*tp_as_number*/
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
525 &string_as_sequence, /*tp_as_sequence*/
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
526 0, /*tp_as_mapping*/
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
527 (hashfunc)string_hash, /*tp_hash*/
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
528 0, /*tp_call*/
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
529 0, /*tp_str*/
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
530 0, /*tp_getattro*/
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
531 0, /*tp_setattro*/
5410
3b2fda31bf1b Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents: 5396
diff changeset
532 &string_as_buffer, /*tp_as_buffer*/
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
533 0, /*tp_xxx4*/
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
534 0, /*tp_doc*/
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
535 };
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
536
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
537 void
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
538 PyString_Concat(pv, w)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
539 register PyObject **pv;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
540 register PyObject *w;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
541 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
542 register PyObject *v;
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
543 if (*pv == NULL)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
544 return;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
545 if (w == NULL || !PyString_Check(*pv)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
546 Py_DECREF(*pv);
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
547 *pv = NULL;
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
548 return;
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
549 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
550 v = string_concat((PyStringObject *) *pv, w);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
551 Py_DECREF(*pv);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
552 *pv = v;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
553 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
554
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
555 void
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
556 PyString_ConcatAndDel(pv, w)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
557 register PyObject **pv;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
558 register PyObject *w;
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
559 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
560 PyString_Concat(pv, w);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
561 Py_XDECREF(w);
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
562 }
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
563
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
564
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
565 /* The following function breaks the notion that strings are immutable:
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
566 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
567 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
568 as creating a new string object and destroying the old one, only
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
569 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
570 already be known to some other part of the code... */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
571
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
572 int
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
573 _PyString_Resize(pv, newsize)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
574 PyObject **pv;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
575 int newsize;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
576 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
577 register PyObject *v;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
578 register PyStringObject *sv;
71
ac4522e99ca9 Fixed resizestring() to work if reference tracing is turned on.
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
579 v = *pv;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
580 if (!PyString_Check(v) || v->ob_refcnt != 1) {
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
581 *pv = 0;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
582 Py_DECREF(v);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
583 PyErr_BadInternalCall();
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
584 return -1;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
585 }
71
ac4522e99ca9 Fixed resizestring() to work if reference tracing is turned on.
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
586 /* XXX UNREF/NEWREF interface should be more symmetrical */
3586
36cc3ee54f28 new debugger symbol names
Guido van Rossum <guido@python.org>
parents: 3571
diff changeset
587 #ifdef Py_REF_DEBUG
2612
ca6b60d4ea90 fix dusty debugging macros
Guido van Rossum <guido@python.org>
parents: 2504
diff changeset
588 --_Py_RefTotal;
71
ac4522e99ca9 Fixed resizestring() to work if reference tracing is turned on.
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
589 #endif
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
590 _Py_ForgetReference(v);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
591 *pv = (PyObject *)
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
592 realloc((char *)v,
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
593 sizeof(PyStringObject) + newsize * sizeof(char));
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
594 if (*pv == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
595 PyMem_DEL(v);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
596 PyErr_NoMemory();
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
597 return -1;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
598 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
599 _Py_NewReference(*pv);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
600 sv = (PyStringObject *) *pv;
71
ac4522e99ca9 Fixed resizestring() to work if reference tracing is turned on.
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
601 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
602 sv->ob_sval[newsize] = '\0';
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
603 return 0;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
604 }
1252
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 /* Helpers for formatstring */
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
607
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
608 static PyObject *
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
609 getnextarg(args, arglen, p_argidx)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
610 PyObject *args;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
611 int arglen;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
612 int *p_argidx;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
613 {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
614 int argidx = *p_argidx;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
615 if (argidx < arglen) {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
616 (*p_argidx)++;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
617 if (arglen < 0)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
618 return args;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
619 else
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
620 return PyTuple_GetItem(args, argidx);
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
621 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
622 PyErr_SetString(PyExc_TypeError,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
623 "not enough arguments for format string");
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
624 return NULL;
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
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
627 #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
628 #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
629 #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
630 #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
631 #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
632
5022
62645787fbc8 Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
633 static int
62645787fbc8 Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
634 formatfloat(buf, flags, prec, type, v)
62645787fbc8 Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
635 char *buf;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
636 int flags;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
637 int prec;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
638 int type;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
639 PyObject *v;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
640 {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
641 char fmt[20];
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
642 double x;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
643 if (!PyArg_Parse(v, "d;float argument required", &x))
5022
62645787fbc8 Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
644 return -1;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
645 if (prec < 0)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
646 prec = 6;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
647 if (prec > 50)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
648 prec = 50; /* Arbitrary limitation */
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
649 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
650 type = 'g';
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
651 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
652 sprintf(buf, fmt, x);
5022
62645787fbc8 Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
653 return strlen(buf);
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
654 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
655
5022
62645787fbc8 Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
656 static int
62645787fbc8 Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
657 formatint(buf, flags, prec, type, v)
62645787fbc8 Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
658 char *buf;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
659 int flags;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
660 int prec;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
661 int type;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
662 PyObject *v;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
663 {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
664 char fmt[20];
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
665 long x;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
666 if (!PyArg_Parse(v, "l;int argument required", &x))
5022
62645787fbc8 Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
667 return -1;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
668 if (prec < 0)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
669 prec = 1;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
670 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
671 sprintf(buf, fmt, x);
5022
62645787fbc8 Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
672 return strlen(buf);
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
673 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
674
5022
62645787fbc8 Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
675 static int
62645787fbc8 Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
676 formatchar(buf, v)
62645787fbc8 Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
677 char *buf;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
678 PyObject *v;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
679 {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
680 if (PyString_Check(v)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
681 if (!PyArg_Parse(v, "c;%c requires int or char", &buf[0]))
5022
62645787fbc8 Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
682 return -1;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
683 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
684 else {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
685 if (!PyArg_Parse(v, "b;%c requires int or char", &buf[0]))
5022
62645787fbc8 Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
686 return -1;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
687 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
688 buf[1] = '\0';
5022
62645787fbc8 Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
689 return 1;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
690 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
691
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
692
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
693 /* 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
694
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
695 PyObject *
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
696 PyString_Format(format, args)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
697 PyObject *format;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
698 PyObject *args;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
699 {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
700 char *fmt, *res;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
701 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
702 int args_owned = 0;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
703 PyObject *result;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
704 PyObject *dict = NULL;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
705 if (format == NULL || !PyString_Check(format) || args == NULL) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
706 PyErr_BadInternalCall();
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
707 return NULL;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
708 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
709 fmt = PyString_AsString(format);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
710 fmtcnt = PyString_Size(format);
1282
e0464e7115ca * pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
711 reslen = rescnt = fmtcnt + 100;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
712 result = PyString_FromStringAndSize((char *)NULL, reslen);
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
713 if (result == NULL)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
714 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
715 res = PyString_AsString(result);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
716 if (PyTuple_Check(args)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
717 arglen = PyTuple_Size(args);
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
718 argidx = 0;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
719 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
720 else {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
721 arglen = -1;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
722 argidx = -2;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
723 }
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
724 if (args->ob_type->tp_as_mapping)
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
725 dict = args;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
726 while (--fmtcnt >= 0) {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
727 if (*fmt != '%') {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
728 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
729 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
730 reslen += rescnt;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
731 if (_PyString_Resize(&result, reslen) < 0)
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
732 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
733 res = PyString_AsString(result)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
734 + reslen - rescnt;
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
735 --rescnt;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
736 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
737 *res++ = *fmt++;
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 else {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
740 /* Got a format specifier */
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
741 int flags = 0;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
742 int width = -1;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
743 int prec = -1;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
744 int size = 0;
1408
18df674539da Three micro fixes to formatstring
Guido van Rossum <guido@python.org>
parents: 1380
diff changeset
745 int c = '\0';
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
746 int fill;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
747 PyObject *v = NULL;
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
748 PyObject *temp = NULL;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
749 char *buf;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
750 int sign;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
751 int len;
5022
62645787fbc8 Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
752 char tmpbuf[120]; /* For format{float,int,char}() */
4588
4dc67fdd9e0b Make gcc -Wall happy
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
753 fmt++;
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
754 if (*fmt == '(') {
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
755 char *keystart;
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
756 int keylen;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
757 PyObject *key;
6286
82cd2baa4136 Patch submitted by Brad Howes (with one bug fixed by me): allow
Guido van Rossum <guido@python.org>
parents: 5935
diff changeset
758 int pcount = 1;
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
759
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
760 if (dict == NULL) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
761 PyErr_SetString(PyExc_TypeError,
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
762 "format requires a mapping");
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
763 goto error;
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
764 }
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
765 ++fmt;
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
766 --fmtcnt;
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
767 keystart = fmt;
6286
82cd2baa4136 Patch submitted by Brad Howes (with one bug fixed by me): allow
Guido van Rossum <guido@python.org>
parents: 5935
diff changeset
768 /* Skip over balanced parentheses */
82cd2baa4136 Patch submitted by Brad Howes (with one bug fixed by me): allow
Guido van Rossum <guido@python.org>
parents: 5935
diff changeset
769 while (pcount > 0 && --fmtcnt >= 0) {
82cd2baa4136 Patch submitted by Brad Howes (with one bug fixed by me): allow
Guido van Rossum <guido@python.org>
parents: 5935
diff changeset
770 if (*fmt == ')')
82cd2baa4136 Patch submitted by Brad Howes (with one bug fixed by me): allow
Guido van Rossum <guido@python.org>
parents: 5935
diff changeset
771 --pcount;
82cd2baa4136 Patch submitted by Brad Howes (with one bug fixed by me): allow
Guido van Rossum <guido@python.org>
parents: 5935
diff changeset
772 else if (*fmt == '(')
82cd2baa4136 Patch submitted by Brad Howes (with one bug fixed by me): allow
Guido van Rossum <guido@python.org>
parents: 5935
diff changeset
773 ++pcount;
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
774 fmt++;
6286
82cd2baa4136 Patch submitted by Brad Howes (with one bug fixed by me): allow
Guido van Rossum <guido@python.org>
parents: 5935
diff changeset
775 }
82cd2baa4136 Patch submitted by Brad Howes (with one bug fixed by me): allow
Guido van Rossum <guido@python.org>
parents: 5935
diff changeset
776 keylen = fmt - keystart - 1;
82cd2baa4136 Patch submitted by Brad Howes (with one bug fixed by me): allow
Guido van Rossum <guido@python.org>
parents: 5935
diff changeset
777 if (fmtcnt < 0 || pcount > 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
778 PyErr_SetString(PyExc_ValueError,
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
779 "incomplete format key");
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
780 goto error;
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
781 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
782 key = PyString_FromStringAndSize(keystart,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
783 keylen);
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
784 if (key == NULL)
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
785 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
786 if (args_owned) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
787 Py_DECREF(args);
3570
977ff1ace070 Fix obscure bug in string%mapping where the mapping creates its items
Guido van Rossum <guido@python.org>
parents: 2612
diff changeset
788 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
789 }
977ff1ace070 Fix obscure bug in string%mapping where the mapping creates its items
Guido van Rossum <guido@python.org>
parents: 2612
diff changeset
790 args = PyObject_GetItem(dict, key);
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
791 Py_DECREF(key);
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
792 if (args == NULL) {
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
793 goto error;
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
794 }
3570
977ff1ace070 Fix obscure bug in string%mapping where the mapping creates its items
Guido van Rossum <guido@python.org>
parents: 2612
diff changeset
795 args_owned = 1;
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
796 arglen = -1;
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
797 argidx = -2;
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
798 }
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
799 while (--fmtcnt >= 0) {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
800 switch (c = *fmt++) {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
801 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
802 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
803 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
804 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
805 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
806 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
807 break;
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 if (c == '*') {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
810 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
811 if (v == NULL)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
812 goto error;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
813 if (!PyInt_Check(v)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
814 PyErr_SetString(PyExc_TypeError,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
815 "* wants int");
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
816 goto error;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
817 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
818 width = PyInt_AsLong(v);
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
819 if (width < 0)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
820 width = 0;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
821 if (--fmtcnt >= 0)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
822 c = *fmt++;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
823 }
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
824 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
825 width = c - '0';
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
826 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
827 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
828 if (!isdigit(c))
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
829 break;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
830 if ((width*10) / 10 != width) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
831 PyErr_SetString(
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
832 PyExc_ValueError,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
833 "width too big");
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
834 goto error;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
835 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
836 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
837 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
838 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
839 if (c == '.') {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
840 prec = 0;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
841 if (--fmtcnt >= 0)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
842 c = *fmt++;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
843 if (c == '*') {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
844 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
845 if (v == NULL)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
846 goto error;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
847 if (!PyInt_Check(v)) {
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
848 PyErr_SetString(
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
849 PyExc_TypeError,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
850 "* wants int");
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
851 goto error;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
852 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
853 prec = PyInt_AsLong(v);
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
854 if (prec < 0)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
855 prec = 0;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
856 if (--fmtcnt >= 0)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
857 c = *fmt++;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
858 }
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
859 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
860 prec = c - '0';
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
861 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
862 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
863 if (!isdigit(c))
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
864 break;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
865 if ((prec*10) / 10 != prec) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
866 PyErr_SetString(
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
867 PyExc_ValueError,
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
868 "prec too big");
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
869 goto error;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
870 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
871 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
872 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
873 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
874 } /* prec */
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
875 if (fmtcnt >= 0) {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
876 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
877 size = c;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
878 if (--fmtcnt >= 0)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
879 c = *fmt++;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
880 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
881 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
882 if (fmtcnt < 0) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
883 PyErr_SetString(PyExc_ValueError,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
884 "incomplete format");
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
885 goto error;
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 (c != '%') {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
888 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
889 if (v == NULL)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
890 goto error;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
891 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
892 sign = 0;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
893 fill = ' ';
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
894 switch (c) {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
895 case '%':
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
896 buf = "%";
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
897 len = 1;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
898 break;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
899 case 's':
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
900 temp = PyObject_Str(v);
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
901 if (temp == NULL)
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
902 goto error;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
903 buf = PyString_AsString(temp);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
904 len = PyString_Size(temp);
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
905 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
906 len = prec;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
907 break;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
908 case 'i':
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
909 case 'd':
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
910 case 'u':
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
911 case 'o':
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
912 case 'x':
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
913 case 'X':
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
914 if (c == 'i')
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
915 c = 'd';
5022
62645787fbc8 Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
916 buf = tmpbuf;
62645787fbc8 Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
917 len = formatint(buf, flags, prec, c, v);
62645787fbc8 Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
918 if (len < 0)
1252
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 sign = (c == 'd');
5057
6c7a76fd5dc1 Fix bug reported by Per Lindqvist: "%#06x" % 1 stuck the 0 padding
Guido van Rossum <guido@python.org>
parents: 5022
diff changeset
921 if (flags&F_ZERO) {
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
922 fill = '0';
5057
6c7a76fd5dc1 Fix bug reported by Per Lindqvist: "%#06x" % 1 stuck the 0 padding
Guido van Rossum <guido@python.org>
parents: 5022
diff changeset
923 if ((flags&F_ALT) &&
6c7a76fd5dc1 Fix bug reported by Per Lindqvist: "%#06x" % 1 stuck the 0 padding
Guido van Rossum <guido@python.org>
parents: 5022
diff changeset
924 (c == 'x' || c == 'X') &&
6c7a76fd5dc1 Fix bug reported by Per Lindqvist: "%#06x" % 1 stuck the 0 padding
Guido van Rossum <guido@python.org>
parents: 5022
diff changeset
925 buf[0] == '0' && buf[1] == c) {
6c7a76fd5dc1 Fix bug reported by Per Lindqvist: "%#06x" % 1 stuck the 0 padding
Guido van Rossum <guido@python.org>
parents: 5022
diff changeset
926 *res++ = *buf++;
6c7a76fd5dc1 Fix bug reported by Per Lindqvist: "%#06x" % 1 stuck the 0 padding
Guido van Rossum <guido@python.org>
parents: 5022
diff changeset
927 *res++ = *buf++;
6c7a76fd5dc1 Fix bug reported by Per Lindqvist: "%#06x" % 1 stuck the 0 padding
Guido van Rossum <guido@python.org>
parents: 5022
diff changeset
928 rescnt -= 2;
6c7a76fd5dc1 Fix bug reported by Per Lindqvist: "%#06x" % 1 stuck the 0 padding
Guido van Rossum <guido@python.org>
parents: 5022
diff changeset
929 len -= 2;
6c7a76fd5dc1 Fix bug reported by Per Lindqvist: "%#06x" % 1 stuck the 0 padding
Guido van Rossum <guido@python.org>
parents: 5022
diff changeset
930 width -= 2;
6c7a76fd5dc1 Fix bug reported by Per Lindqvist: "%#06x" % 1 stuck the 0 padding
Guido van Rossum <guido@python.org>
parents: 5022
diff changeset
931 if (width < 0)
6c7a76fd5dc1 Fix bug reported by Per Lindqvist: "%#06x" % 1 stuck the 0 padding
Guido van Rossum <guido@python.org>
parents: 5022
diff changeset
932 width = 0;
6c7a76fd5dc1 Fix bug reported by Per Lindqvist: "%#06x" % 1 stuck the 0 padding
Guido van Rossum <guido@python.org>
parents: 5022
diff changeset
933 }
6c7a76fd5dc1 Fix bug reported by Per Lindqvist: "%#06x" % 1 stuck the 0 padding
Guido van Rossum <guido@python.org>
parents: 5022
diff changeset
934 }
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
935 break;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
936 case 'e':
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
937 case 'E':
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
938 case 'f':
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
939 case 'g':
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
940 case 'G':
5022
62645787fbc8 Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
941 buf = tmpbuf;
62645787fbc8 Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
942 len = formatfloat(buf, flags, prec, c, v);
62645787fbc8 Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
943 if (len < 0)
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
944 goto error;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
945 sign = 1;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
946 if (flags&F_ZERO)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
947 fill = '0';
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
948 break;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
949 case 'c':
5022
62645787fbc8 Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
950 buf = tmpbuf;
62645787fbc8 Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
951 len = formatchar(buf, v);
62645787fbc8 Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents: 5005
diff changeset
952 if (len < 0)
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
953 goto error;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
954 break;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
955 default:
6286
82cd2baa4136 Patch submitted by Brad Howes (with one bug fixed by me): allow
Guido van Rossum <guido@python.org>
parents: 5935
diff changeset
956 PyErr_Format(PyExc_ValueError,
82cd2baa4136 Patch submitted by Brad Howes (with one bug fixed by me): allow
Guido van Rossum <guido@python.org>
parents: 5935
diff changeset
957 "unsupported format character '%c' (0x%x)",
82cd2baa4136 Patch submitted by Brad Howes (with one bug fixed by me): allow
Guido van Rossum <guido@python.org>
parents: 5935
diff changeset
958 c, c);
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
959 goto error;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
960 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
961 if (sign) {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
962 if (*buf == '-' || *buf == '+') {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
963 sign = *buf++;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
964 len--;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
965 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
966 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
967 sign = '+';
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
968 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
969 sign = ' ';
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
970 else
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
971 sign = '\0';
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
972 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
973 if (width < len)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
974 width = len;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
975 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
976 reslen -= rescnt;
e0464e7115ca * pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
977 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
978 reslen += rescnt;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
979 if (_PyString_Resize(&result, reslen) < 0)
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
980 return NULL;
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
981 res = PyString_AsString(result)
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
982 + reslen - rescnt;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
983 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
984 if (sign) {
1409
f5b1ddfddba8 Fix the fix :-(
Guido van Rossum <guido@python.org>
parents: 1408
diff changeset
985 if (fill != ' ')
f5b1ddfddba8 Fix the fix :-(
Guido van Rossum <guido@python.org>
parents: 1408
diff changeset
986 *res++ = sign;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
987 rescnt--;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
988 if (width > len)
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
989 width--;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
990 }
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
991 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
992 do {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
993 --rescnt;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
994 *res++ = fill;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
995 } while (--width > len);
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
996 }
1409
f5b1ddfddba8 Fix the fix :-(
Guido van Rossum <guido@python.org>
parents: 1408
diff changeset
997 if (sign && fill == ' ')
1408
18df674539da Three micro fixes to formatstring
Guido van Rossum <guido@python.org>
parents: 1380
diff changeset
998 *res++ = sign;
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
999 memcpy(res, buf, len);
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
1000 res += len;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
1001 rescnt -= len;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
1002 while (--width >= len) {
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
1003 --rescnt;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
1004 *res++ = ' ';
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
1005 }
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
1006 if (dict && (argidx < arglen) && c != '%') {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1007 PyErr_SetString(PyExc_TypeError,
1847
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
1008 "not all arguments converted");
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
1009 goto error;
0d318475f9e3 fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents: 1409
diff changeset
1010 }
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1011 Py_XDECREF(temp);
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
1012 } /* '%' */
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
1013 } /* 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
1014 if (argidx < arglen && !dict) {
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1015 PyErr_SetString(PyExc_TypeError,
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1016 "not all arguments converted");
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
1017 goto error;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
1018 }
3570
977ff1ace070 Fix obscure bug in string%mapping where the mapping creates its items
Guido van Rossum <guido@python.org>
parents: 2612
diff changeset
1019 if (args_owned)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1020 Py_DECREF(args);
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1021 _PyString_Resize(&result, reslen - rescnt);
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
1022 return result;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
1023 error:
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1024 Py_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
1025 if (args_owned)
5396
d1447950fa04 Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents: 5288
diff changeset
1026 Py_DECREF(args);
1252
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
1027 return NULL;
c4cd0d9d8281 * Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents: 1023
diff changeset
1028 }
5005
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1029
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1030
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1031 #ifdef INTERN_STRINGS
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1032
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1033 static PyObject *interned;
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1034
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1035 void
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1036 PyString_InternInPlace(p)
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1037 PyObject **p;
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1038 {
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1039 register PyStringObject *s = (PyStringObject *)(*p);
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1040 PyObject *t;
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1041 if (s == NULL || !PyString_Check(s))
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1042 Py_FatalError("PyString_InternInPlace: strings only please!");
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1043 if ((t = s->ob_sinterned) != NULL) {
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1044 if (t == (PyObject *)s)
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1045 return;
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1046 Py_INCREF(t);
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1047 *p = t;
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1048 Py_DECREF(s);
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1049 return;
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1050 }
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1051 if (interned == NULL) {
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1052 interned = PyDict_New();
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1053 if (interned == NULL)
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1054 return;
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1055 }
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1056 if ((t = PyDict_GetItem(interned, (PyObject *)s)) != NULL) {
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1057 Py_INCREF(t);
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1058 *p = s->ob_sinterned = t;
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1059 Py_DECREF(s);
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1060 return;
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1061 }
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1062 t = (PyObject *)s;
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1063 if (PyDict_SetItem(interned, t, t) == 0) {
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1064 s->ob_sinterned = t;
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1065 return;
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1066 }
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1067 PyErr_Clear();
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1068 }
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1069
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1070
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1071 PyObject *
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1072 PyString_InternFromString(cp)
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1073 const char *cp;
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1074 {
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1075 PyObject *s = PyString_FromString(cp);
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1076 if (s == NULL)
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1077 return NULL;
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1078 PyString_InternInPlace(&s);
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1079 return s;
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1080 }
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1081
f17bace48dd6 String interning.
Guido van Rossum <guido@python.org>
parents: 4654
diff changeset
1082 #endif
5920
5fb30413b93b Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents: 5717
diff changeset
1083
5fb30413b93b Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents: 5717
diff changeset
1084 void
5fb30413b93b Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents: 5717
diff changeset
1085 PyString_Fini()
5fb30413b93b Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents: 5717
diff changeset
1086 {
5fb30413b93b Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents: 5717
diff changeset
1087 int i;
5fb30413b93b Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents: 5717
diff changeset
1088 for (i = 0; i < UCHAR_MAX + 1; i++) {
5fb30413b93b Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents: 5717
diff changeset
1089 Py_XDECREF(characters[i]);
5fb30413b93b Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents: 5717
diff changeset
1090 characters[i] = NULL;
5fb30413b93b Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents: 5717
diff changeset
1091 }
5fb30413b93b Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents: 5717
diff changeset
1092 #ifndef DONT_SHARE_SHORT_STRINGS
5fb30413b93b Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents: 5717
diff changeset
1093 Py_XDECREF(nullstring);
5fb30413b93b Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents: 5717
diff changeset
1094 nullstring = NULL;
5fb30413b93b Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents: 5717
diff changeset
1095 #endif
5935
d77d3b8e86b2 Change the Fini function to only remove otherwise unreferenced strings
Guido van Rossum <guido@python.org>
parents: 5920
diff changeset
1096 #ifdef INTERN_STRINGS
d77d3b8e86b2 Change the Fini function to only remove otherwise unreferenced strings
Guido van Rossum <guido@python.org>
parents: 5920
diff changeset
1097 if (interned) {
d77d3b8e86b2 Change the Fini function to only remove otherwise unreferenced strings
Guido van Rossum <guido@python.org>
parents: 5920
diff changeset
1098 int pos, changed;
d77d3b8e86b2 Change the Fini function to only remove otherwise unreferenced strings
Guido van Rossum <guido@python.org>
parents: 5920
diff changeset
1099 PyObject *key, *value;
d77d3b8e86b2 Change the Fini function to only remove otherwise unreferenced strings
Guido van Rossum <guido@python.org>
parents: 5920
diff changeset
1100 do {
d77d3b8e86b2 Change the Fini function to only remove otherwise unreferenced strings
Guido van Rossum <guido@python.org>
parents: 5920
diff changeset
1101 changed = 0;
d77d3b8e86b2 Change the Fini function to only remove otherwise unreferenced strings
Guido van Rossum <guido@python.org>
parents: 5920
diff changeset
1102 pos = 0;
d77d3b8e86b2 Change the Fini function to only remove otherwise unreferenced strings
Guido van Rossum <guido@python.org>
parents: 5920
diff changeset
1103 while (PyDict_Next(interned, &pos, &key, &value)) {
d77d3b8e86b2 Change the Fini function to only remove otherwise unreferenced strings
Guido van Rossum <guido@python.org>
parents: 5920
diff changeset
1104 if (key->ob_refcnt == 2 && key == value) {
d77d3b8e86b2 Change the Fini function to only remove otherwise unreferenced strings
Guido van Rossum <guido@python.org>
parents: 5920
diff changeset
1105 PyDict_DelItem(interned, key);
d77d3b8e86b2 Change the Fini function to only remove otherwise unreferenced strings
Guido van Rossum <guido@python.org>
parents: 5920
diff changeset
1106 changed = 1;
d77d3b8e86b2 Change the Fini function to only remove otherwise unreferenced strings
Guido van Rossum <guido@python.org>
parents: 5920
diff changeset
1107 }
d77d3b8e86b2 Change the Fini function to only remove otherwise unreferenced strings
Guido van Rossum <guido@python.org>
parents: 5920
diff changeset
1108 }
d77d3b8e86b2 Change the Fini function to only remove otherwise unreferenced strings
Guido van Rossum <guido@python.org>
parents: 5920
diff changeset
1109 } while (changed);
d77d3b8e86b2 Change the Fini function to only remove otherwise unreferenced strings
Guido van Rossum <guido@python.org>
parents: 5920
diff changeset
1110 }
d77d3b8e86b2 Change the Fini function to only remove otherwise unreferenced strings
Guido van Rossum <guido@python.org>
parents: 5920
diff changeset
1111 #endif
5920
5fb30413b93b Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents: 5717
diff changeset
1112 }