summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1997-01-21 16:12:09 (GMT)
committerGuido van Rossum <guido@python.org>1997-01-21 16:12:09 (GMT)
commita04d47b319bfd6fb094d5991624550cf8d6bb30f (patch)
tree692c39b4688186fed84181974a50f9492c7e7f15 /Objects
parent70d44787a366e8c72d4dc3e0537e92c47f63a75b (diff)
downloadcpython-a04d47b319bfd6fb094d5991624550cf8d6bb30f.zip
cpython-a04d47b319bfd6fb094d5991624550cf8d6bb30f.tar.gz
cpython-a04d47b319bfd6fb094d5991624550cf8d6bb30f.tar.bz2
Don't use static buffers internally for formatstring().
Diffstat (limited to 'Objects')
-rw-r--r--Objects/stringobject.c51
1 files changed, 26 insertions, 25 deletions
diff --git a/Objects/stringobject.c b/Objects/stringobject.c
index d656fa1..f037f96 100644
--- a/Objects/stringobject.c
+++ b/Objects/stringobject.c
@@ -580,18 +580,18 @@ getnextarg(args, arglen, p_argidx)
extern double fabs PROTO((double));
-static char *
-formatfloat(flags, prec, type, v)
+static int
+formatfloat(buf, flags, prec, type, v)
+ char *buf;
int flags;
int prec;
int type;
object *v;
{
char fmt[20];
- static char buf[120];
double x;
if (!getargs(v, "d;float argument required", &x))
- return NULL;
+ return -1;
if (prec < 0)
prec = 6;
if (prec > 50)
@@ -600,43 +600,43 @@ formatfloat(flags, prec, type, v)
type = 'g';
sprintf(fmt, "%%%s.%d%c", (flags&F_ALT) ? "#" : "", prec, type);
sprintf(buf, fmt, x);
- return buf;
+ return strlen(buf);
}
-static char *
-formatint(flags, prec, type, v)
+static int
+formatint(buf, flags, prec, type, v)
+ char *buf;
int flags;
int prec;
int type;
object *v;
{
char fmt[20];
- static char buf[50];
long x;
if (!getargs(v, "l;int argument required", &x))
- return NULL;
+ return -1;
if (prec < 0)
prec = 1;
sprintf(fmt, "%%%s.%dl%c", (flags&F_ALT) ? "#" : "", prec, type);
sprintf(buf, fmt, x);
- return buf;
+ return strlen(buf);
}
-static char *
-formatchar(v)
+static int
+formatchar(buf, v)
+ char *buf;
object *v;
{
- static char buf[2];
if (is_stringobject(v)) {
if (!getargs(v, "c;%c requires int or char", &buf[0]))
- return NULL;
+ return -1;
}
else {
if (!getargs(v, "b;%c requires int or char", &buf[0]))
- return NULL;
+ return -1;
}
buf[1] = '\0';
- return buf;
+ return 1;
}
@@ -698,6 +698,7 @@ formatstring(format, args)
char *buf;
int sign;
int len;
+ char tmpbuf[120]; /* For format{float,int,char}() */
fmt++;
if (*fmt == '(') {
char *keystart;
@@ -849,10 +850,10 @@ formatstring(format, args)
case 'X':
if (c == 'i')
c = 'd';
- buf = formatint(flags, prec, c, v);
- if (buf == NULL)
+ buf = tmpbuf;
+ len = formatint(buf, flags, prec, c, v);
+ if (len < 0)
goto error;
- len = strlen(buf);
sign = (c == 'd');
if (flags&F_ZERO)
fill = '0';
@@ -862,19 +863,19 @@ formatstring(format, args)
case 'f':
case 'g':
case 'G':
- buf = formatfloat(flags, prec, c, v);
- if (buf == NULL)
+ buf = tmpbuf;
+ len = formatfloat(buf, flags, prec, c, v);
+ if (len < 0)
goto error;
- len = strlen(buf);
sign = 1;
if (flags&F_ZERO)
fill = '0';
break;
case 'c':
- buf = formatchar(v);
- if (buf == NULL)
+ buf = tmpbuf;
+ len = formatchar(buf, v);
+ if (len < 0)
goto error;
- len = 1;
break;
default:
err_setstr(ValueError,