* Changed many files to use mkvalue() instead of newtupleobject().

* Fixcprt.py: added [-y file] option, do only files younger than file.
* modsupport.[ch]: added vmkvalue().
* intobject.c: use mkvalue().
* stringobject.c: added "formatstring"; renamed string* to string_*;
  ceval.c: call formatstring for string % value.
* longobject.c: close memory leak in divmod.
* parsetok.c: set result node to NULL when returning an error.
This commit is contained in:
Guido van Rossum 1993-03-16 12:15:04 +00:00
parent f48b419a07
commit e537240c25
21 changed files with 503 additions and 273 deletions

View File

@ -1,6 +1,6 @@
/*********************************************************** /***********************************************************
Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
Netherlands. Amsterdam, The Netherlands.
All Rights Reserved All Rights Reserved
@ -24,9 +24,21 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
/* Module support interface */ /* Module support interface */
#ifdef HAVE_PROTOTYPES
#define USE_STDARG
#endif
#ifdef USE_STDARG
#include <stdarg.h>
#else
#include <varargs.h>
#endif
extern object *initmodule PROTO((char *, struct methodlist *)); extern object *initmodule PROTO((char *, struct methodlist *));
extern int getargs PROTO((object *, char *, ...)); extern int getargs PROTO((object *, char *, ...));
extern int vgetargs PROTO((object *, char *, va_list));
extern object *mkvalue PROTO((char *, ...)); extern object *mkvalue PROTO((char *, ...));
extern object *vmkvalue PROTO((char *, va_list));
#define getnoarg(v) getargs(v, "") #define getnoarg(v) getargs(v, "")
#define getintarg(v, a) getargs(v, "i", a) #define getintarg(v, a) getargs(v, "i", a)

View File

@ -1,6 +1,6 @@
/*********************************************************** /***********************************************************
Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
Netherlands. Amsterdam, The Netherlands.
All Rights Reserved All Rights Reserved
@ -58,6 +58,7 @@ extern unsigned int getstringsize PROTO((object *));
extern char *getstringvalue PROTO((object *)); extern char *getstringvalue PROTO((object *));
extern void joinstring PROTO((object **, object *)); extern void joinstring PROTO((object **, object *));
extern int resizestring PROTO((object **, int)); extern int resizestring PROTO((object **, int));
extern object *formatstring PROTO((object *, object *));
/* Macro, trading safety for speed */ /* Macro, trading safety for speed */
#define GETSTRINGVALUE(op) ((op)->ob_sval) #define GETSTRINGVALUE(op) ((op)->ob_sval)

View File

@ -1,6 +1,6 @@
/********************************************************** /**********************************************************
Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
Netherlands. Amsterdam, The Netherlands.
All Rights Reserved All Rights Reserved

View File

@ -1,6 +1,6 @@
/*********************************************************** /***********************************************************
Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
Netherlands. Amsterdam, The Netherlands.
All Rights Reserved All Rights Reserved

View File

@ -1,6 +1,6 @@
/********************************************************** /**********************************************************
Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
Netherlands. Amsterdam, The Netherlands.
All Rights Reserved All Rights Reserved
@ -614,18 +614,12 @@ call_forms_OUTfOUTf (func, obj, args)
object *args; object *args;
{ {
float f1, f2; float f1, f2;
object *arg;
if (!getnoarg(args)) return NULL; if (!getnoarg(args)) return NULL;
(*func) (obj, &f1, &f2); (*func) (obj, &f1, &f2);
arg = newtupleobject (2); return mkvalue("(ff)", f1, f2);
if (arg == NULL) return NULL;
settupleitem (arg, 0, newfloatobject (f1));
settupleitem (arg, 1, newfloatobject (f2));
return arg;
} }
#ifdef UNUSED #ifdef UNUSED
@ -966,20 +960,13 @@ get_clock(g, args)
object *args; object *args;
{ {
int i0, i1, i2; int i0, i1, i2;
object *arg;
if (!getnoarg(args)) if (!getnoarg(args))
return NULL; return NULL;
fl_get_clock (g->ob_generic, &i0, &i1, &i2); fl_get_clock (g->ob_generic, &i0, &i1, &i2);
arg = newtupleobject (3); return mkvalue("(iii)", i0, i1, i2);
if (arg == NULL) return NULL;
settupleitem (arg, 0, newintobject (i0));
settupleitem (arg, 1, newintobject (i1));
settupleitem (arg, 2, newintobject (i2));
return arg;
} }
static struct methodlist clock_methods[] = { static struct methodlist clock_methods[] = {
@ -2064,11 +2051,9 @@ forms_do_or_check_forms(dummy, args, func)
if (my_event_callback == NULL) if (my_event_callback == NULL)
return newintobject(-1L); return newintobject(-1L);
dev = fl_qread(&val); dev = fl_qread(&val);
arg = newtupleobject(2); arg = mkvalue("(ih)", dev, val);
if (arg == NULL) if (arg == NULL)
return NULL; return NULL;
settupleitem(arg, 0, newintobject((long)dev));
settupleitem(arg, 1, newintobject((long)val));
res = call_object(my_event_callback, arg); res = call_object(my_event_callback, arg);
XDECREF(res); XDECREF(res);
DECREF(arg); DECREF(arg);
@ -2085,11 +2070,9 @@ forms_do_or_check_forms(dummy, args, func)
INCREF(g); INCREF(g);
return ((object *) g); return ((object *) g);
} }
arg = newtupleobject(2); arg = mkvalue("(OO)", (object *)g, g->ob_callback_arg);
INCREF(g); if (arg == NULL)
settupleitem(arg, 0, (object *)g); return NULL;
INCREF(g->ob_callback_arg);
settupleitem(arg, 1, g->ob_callback_arg);
res = call_object(g->ob_callback, arg); res = call_object(g->ob_callback, arg);
XDECREF(res); XDECREF(res);
DECREF(arg); DECREF(arg);
@ -2259,18 +2242,12 @@ forms_qread(self, args)
object *self; object *self;
object *args; object *args;
{ {
long retval; int dev;
short arg1; short val;
BGN_SAVE BGN_SAVE
retval = fl_qread(&arg1); dev = fl_qread(&val);
END_SAVE END_SAVE
{ return mkvalue("(ih)", dev, val);
object *v = newtupleobject(2);
if (v == NULL) return NULL;
settupleitem(v, 0, newintobject(retval));
settupleitem(v, 1, newintobject((long)arg1));
return v;
}
} }
static object * static object *
@ -2336,21 +2313,12 @@ forms_getmcolor(self, args)
{ {
int arg; int arg;
short r, g, b; short r, g, b;
object *v;
if (!getintarg(args, &arg)) return NULL; if (!getintarg(args, &arg)) return NULL;
fl_getmcolor(arg, &r, &g, &b); fl_getmcolor(arg, &r, &g, &b);
v = newtupleobject(3); return mkvalue("(hhh)", r, g, b);
if (v == NULL) return NULL;
settupleitem(v, 0, newintobject((long)r));
settupleitem(v, 1, newintobject((long)g));
settupleitem(v, 2, newintobject((long)b));
return v;
} }
static object * static object *
@ -2358,21 +2326,13 @@ forms_get_mouse(self, args)
object *self; object *self;
object *args; object *args;
{ {
float x, y ; float x, y;
object *v;
if (!getnoarg(args)) return NULL; if (!getnoarg(args)) return NULL;
fl_get_mouse(&x, &y); fl_get_mouse(&x, &y);
v = newtupleobject(2); return mkvalue("(ff)", x, y);
if (v == NULL) return NULL;
settupleitem(v, 0, newfloatobject(x));
settupleitem(v, 1, newfloatobject(y));
return v;
} }
static object * static object *

View File

@ -1,6 +1,6 @@
/*********************************************************** /***********************************************************
Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
Netherlands. Amsterdam, The Netherlands.
All Rights Reserved All Rights Reserved
@ -134,22 +134,15 @@ fh_getfontinfo(self, args)
err_setstr(RuntimeError, "error in fmgetfontinfo"); err_setstr(RuntimeError, "error in fmgetfontinfo");
return NULL; return NULL;
} }
v = newtupleobject(8); return mkvalue("(llllllll)",
if (v == NULL) info.printermatched,
return NULL; info.fixed_width,
#define SET(i, member) settupleitem(v, i, newintobject(info.member)) info.xorig,
SET(0, printermatched); info.yorig,
SET(1, fixed_width); info.xsize,
SET(2, xorig); info.ysize,
SET(3, yorig); info.height,
SET(4, xsize); info.nglyphs);
SET(5, ysize);
SET(6, height);
SET(7, nglyphs);
#undef SET
if (err_occurred())
return NULL;
return v;
} }
#if 0 #if 0

View File

@ -1,6 +1,6 @@
/*********************************************************** /***********************************************************
Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
Netherlands. Amsterdam, The Netherlands.
All Rights Reserved All Rights Reserved
@ -136,7 +136,6 @@ math_frexp(self, args)
object *self; object *self;
object *args; object *args;
{ {
object *v;
double x; double x;
int i; int i;
if (!getdoublearg(args, &x)) if (!getdoublearg(args, &x))
@ -146,16 +145,7 @@ math_frexp(self, args)
CHECK(x); CHECK(x);
if (errno != 0) if (errno != 0)
return math_error(); return math_error();
v = newtupleobject(2); return mkvalue("(di)", x, i);
if (v != NULL) {
settupleitem(v, 0, newfloatobject(x));
settupleitem(v, 1, newintobject((long)i));
if (err_occurred()) {
DECREF(v);
v = NULL;
}
}
return v;
} }
static object * static object *
@ -181,7 +171,6 @@ math_modf(self, args)
object *self; object *self;
object *args; object *args;
{ {
object *v;
double x, y; double x, y;
if (!getdoublearg(args, &x)) if (!getdoublearg(args, &x))
return NULL; return NULL;
@ -190,16 +179,7 @@ math_modf(self, args)
CHECK(x); CHECK(x);
if (errno != 0) if (errno != 0)
return math_error(); return math_error();
v = newtupleobject(2); return mkvalue("(dd)", x, y);
if (v != NULL) {
settupleitem(v, 0, newfloatobject(x));
settupleitem(v, 1, newfloatobject(y));
if (err_occurred()) {
DECREF(v);
v = NULL;
}
}
return v;
} }
static struct methodlist math_methods[] = { static struct methodlist math_methods[] = {

View File

@ -216,26 +216,17 @@ posix_do_stat(self, args, statfunc)
END_SAVE END_SAVE
if (res != 0) if (res != 0)
return posix_error(); return posix_error();
v = newtupleobject(10); v = mkvalue("(llllllllll)",
if (v == NULL) (long)st.st_mode,
return NULL; (long)st.st_ino,
#define SET(i, st_member) settupleitem(v, i, newintobject((long)st.st_member)) (long)st.st_dev,
SET(0, st_mode); (long)st.st_nlink,
SET(1, st_ino); (long)st.st_uid,
SET(2, st_dev); (long)st.st_gid,
SET(3, st_nlink); (long)st.st_size,
SET(4, st_uid); (long)st.st_atime,
SET(5, st_gid); (long)st.st_mtime,
SET(6, st_size); (long)st.st_ctime);
SET(7, st_atime);
SET(8, st_mtime);
SET(9, st_ctime);
#undef SET
if (err_occurred()) {
DECREF(v);
return NULL;
}
return v;
} }
@ -477,21 +468,12 @@ posix_uname(self, args)
END_SAVE END_SAVE
if (res < 0) if (res < 0)
return posix_error(); return posix_error();
v = newtupleobject(5); return mkvalue("(sssss)",
if (v == NULL) u.sysname,
return NULL; u.nodename,
#define SET(i, member) settupleitem(v, i, newstringobject(u.member)) u.release,
SET(0, sysname); u.version,
SET(1, nodename); u.machine);
SET(2, release);
SET(3, version);
SET(4, machine);
#undef SET
if (err_occurred()) {
DECREF(v);
return NULL;
}
return v;
} }
#endif /* NO_UNAME */ #endif /* NO_UNAME */

View File

@ -1,6 +1,6 @@
/*********************************************************** /***********************************************************
Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
Netherlands. Amsterdam, The Netherlands.
All Rights Reserved All Rights Reserved
@ -38,25 +38,14 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
static object *mkpwent(p) static object *mkpwent(p)
struct passwd *p; struct passwd *p;
{ {
object *v; return mkvalue("(ssllsss)",
if ((v = newtupleobject(7)) == NULL) p->pw_name,
return NULL; p->pw_passwd,
#define ISET(i, member) settupleitem(v, i, newintobject((long)p->member)) (long)p->pw_uid,
#define SSET(i, member) settupleitem(v, i, newstringobject(p->member)) (long)p->pw_gid,
SSET(0, pw_name); p->pw_gecos,
SSET(1, pw_passwd); p->pw_dir,
ISET(2, pw_uid); p->pw_shell);
ISET(3, pw_gid);
SSET(4, pw_gecos);
SSET(5, pw_dir);
SSET(6, pw_shell);
#undef SSET
#undef ISET
if (err_occurred()) {
DECREF(v);
return NULL;
}
return v;
} }
static object *pwd_getpwuid(self, args) static object *pwd_getpwuid(self, args)
@ -130,32 +119,24 @@ static object *mkgrent(p)
{ {
object *v, *w; object *v, *w;
char **member; char **member;
if ((v = newtupleobject(4)) == NULL)
return NULL;
#define ISET(i, member) settupleitem(v, i, newintobject((long)p->member))
#define SSET(i, member) settupleitem(v, i, newstringobject(p->member))
SSET(0, gr_name);
SSET(1, gr_passwd);
ISET(2, gr_gid);
#undef SSET
#undef ISET
if (err_occurred()) {
DECREF(v);
return NULL;
}
if ((w = newlistobject(0)) == NULL) { if ((w = newlistobject(0)) == NULL) {
DECREF(v); DECREF(v);
return NULL; return NULL;
} }
(void) settupleitem(v, 3, w); /* Cannot fail; eats refcnt */
for (member = p->gr_mem; *member != NULL; member++) { for (member = p->gr_mem; *member != NULL; member++) {
object *x = newstringobject(*member); object *x = newstringobject(*member);
if (x == NULL || addlistitem(w, x) != 0) { if (x == NULL || addlistitem(w, x) != 0) {
XDECREF(x); XDECREF(x);
DECREF(v); DECREF(w);
return NULL; return NULL;
} }
} }
v = mkvalue("(sslO)",
p->gr_name,
p->gr_passwd,
(long)p->gr_gid,
w);
DECREF(w);
return v; return v;
} }

View File

@ -1,6 +1,6 @@
/*********************************************************** /***********************************************************
Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
Netherlands. Amsterdam, The Netherlands.
All Rights Reserved All Rights Reserved

View File

@ -1,6 +1,6 @@
/*********************************************************** /***********************************************************
Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
Netherlands. Amsterdam, The Netherlands.
All Rights Reserved All Rights Reserved

View File

@ -1,6 +1,6 @@
/*********************************************************** /***********************************************************
Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
Netherlands. Amsterdam, The Netherlands.
All Rights Reserved All Rights Reserved
@ -49,7 +49,7 @@ newclassobject(bases, methods, name)
if (bases == NULL) { if (bases == NULL) {
bases = newtupleobject(0); bases = newtupleobject(0);
if (bases == NULL) if (bases == NULL)
return err_nomem(); return NULL;
} }
else else
INCREF(bases); INCREF(bases);

View File

@ -1,6 +1,6 @@
/*********************************************************** /***********************************************************
Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
Netherlands. Amsterdam, The Netherlands.
All Rights Reserved All Rights Reserved
@ -28,6 +28,7 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
for any kind of float exception without losing portability. */ for any kind of float exception without losing portability. */
#include "allobjects.h" #include "allobjects.h"
#include "modsupport.h"
#include <errno.h> #include <errno.h>
#ifndef errno #ifndef errno
@ -222,16 +223,7 @@ float_divmod(v, w)
mod += wx; mod += wx;
div -= 1.0; div -= 1.0;
} }
t = newtupleobject(2); return mkvalue("(dd)", div, mod);
if (t != NULL) {
settupleitem(t, 0, newfloatobject(div));
settupleitem(t, 1, newfloatobject(mod));
if (err_occurred()) {
DECREF(t);
t = NULL;
}
}
return t;
} }
static object * static object *

View File

@ -1,6 +1,6 @@
/*********************************************************** /***********************************************************
Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
Netherlands. Amsterdam, The Netherlands.
All Rights Reserved All Rights Reserved
@ -25,6 +25,7 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
/* Integer object implementation */ /* Integer object implementation */
#include "allobjects.h" #include "allobjects.h"
#include "modsupport.h"
/* Standard Booleans */ /* Standard Booleans */
@ -252,18 +253,7 @@ int_divmod(x, y)
long d, m; long d, m;
if (i_divmod(x, y, &d, &m) < 0) if (i_divmod(x, y, &d, &m) < 0)
return NULL; return NULL;
v = newtupleobject(2); return mkvalue("(ll)", d, m);
v0 = newintobject(d);
v1 = newintobject(m);
if (v == NULL || v0 == NULL || v1 == NULL ||
settupleitem(v, 0, v0) != 0 ||
settupleitem(v, 1, v1) != 0) {
XDECREF(v);
XDECREF(v0);
XDECREF(v1);
v = NULL;
}
return v;
} }
static object * static object *

View File

@ -844,8 +844,10 @@ l_divmod(v, w, pdiv, pmod)
(temp = (longobject *) long_sub(div, one)) == NULL) { (temp = (longobject *) long_sub(div, one)) == NULL) {
DECREF(mod); DECREF(mod);
DECREF(div); DECREF(div);
XDECREF(one);
return -1; return -1;
} }
DECREF(one);
DECREF(div); DECREF(div);
div = temp; div = temp;
} }

View File

@ -1,6 +1,6 @@
/*********************************************************** /***********************************************************
Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
Netherlands. Amsterdam, The Netherlands.
All Rights Reserved All Rights Reserved
@ -61,7 +61,7 @@ newstringobject(str)
} }
void void
stringdealloc(op) string_dealloc(op)
object *op; object *op;
{ {
DEL(op); DEL(op);
@ -92,7 +92,7 @@ getstringvalue(op)
/* Methods */ /* Methods */
static int static int
stringprint(op, fp, flags) string_print(op, fp, flags)
stringobject *op; stringobject *op;
FILE *fp; FILE *fp;
int flags; int flags;
@ -119,7 +119,7 @@ stringprint(op, fp, flags)
} }
static object * static object *
stringrepr(op) string_repr(op)
register stringobject *op; register stringobject *op;
{ {
/* XXX overflow? */ /* XXX overflow? */
@ -155,14 +155,14 @@ stringrepr(op)
} }
static int static int
stringlength(a) string_length(a)
stringobject *a; stringobject *a;
{ {
return a->ob_size; return a->ob_size;
} }
static object * static object *
stringconcat(a, bb) string_concat(a, bb)
register stringobject *a; register stringobject *a;
register object *bb; register object *bb;
{ {
@ -198,7 +198,7 @@ stringconcat(a, bb)
} }
static object * static object *
stringrepeat(a, n) string_repeat(a, n)
register stringobject *a; register stringobject *a;
register int n; register int n;
{ {
@ -228,7 +228,7 @@ stringrepeat(a, n)
/* String slice a[i:j] consists of characters a[i] ... a[j-1] */ /* String slice a[i:j] consists of characters a[i] ... a[j-1] */
static object * static object *
stringslice(a, i, j) string_slice(a, i, j)
register stringobject *a; register stringobject *a;
register int i, j; /* May be negative! */ register int i, j; /* May be negative! */
{ {
@ -258,7 +258,7 @@ stringslice(a, i, j)
static object *characters[UCHAR_MAX + 1]; static object *characters[UCHAR_MAX + 1];
static object * static object *
stringitem(a, i) string_item(a, i)
stringobject *a; stringobject *a;
register int i; register int i;
{ {
@ -282,7 +282,7 @@ stringitem(a, i)
} }
static int static int
stringcompare(a, b) string_compare(a, b)
stringobject *a, *b; stringobject *a, *b;
{ {
int len_a = a->ob_size, len_b = b->ob_size; int len_a = a->ob_size, len_b = b->ob_size;
@ -294,11 +294,11 @@ stringcompare(a, b)
} }
static sequence_methods string_as_sequence = { static sequence_methods string_as_sequence = {
stringlength, /*sq_length*/ string_length, /*sq_length*/
stringconcat, /*sq_concat*/ string_concat, /*sq_concat*/
stringrepeat, /*sq_repeat*/ string_repeat, /*sq_repeat*/
stringitem, /*sq_item*/ string_item, /*sq_item*/
stringslice, /*sq_slice*/ string_slice, /*sq_slice*/
0, /*sq_ass_item*/ 0, /*sq_ass_item*/
0, /*sq_ass_slice*/ 0, /*sq_ass_slice*/
}; };
@ -309,12 +309,12 @@ typeobject Stringtype = {
"string", "string",
sizeof(stringobject), sizeof(stringobject),
sizeof(char), sizeof(char),
stringdealloc, /*tp_dealloc*/ string_dealloc, /*tp_dealloc*/
stringprint, /*tp_print*/ string_print, /*tp_print*/
0, /*tp_getattr*/ 0, /*tp_getattr*/
0, /*tp_setattr*/ 0, /*tp_setattr*/
stringcompare, /*tp_compare*/ string_compare, /*tp_compare*/
stringrepr, /*tp_repr*/ string_repr, /*tp_repr*/
0, /*tp_as_number*/ 0, /*tp_as_number*/
&string_as_sequence, /*tp_as_sequence*/ &string_as_sequence, /*tp_as_sequence*/
0, /*tp_as_mapping*/ 0, /*tp_as_mapping*/
@ -328,7 +328,7 @@ joinstring(pv, w)
register object *v; register object *v;
if (*pv == NULL || w == NULL || !is_stringobject(*pv)) if (*pv == NULL || w == NULL || !is_stringobject(*pv))
return; return;
v = stringconcat((stringobject *) *pv, w); v = string_concat((stringobject *) *pv, w);
DECREF(*pv); DECREF(*pv);
*pv = v; *pv = v;
} }
@ -373,3 +373,344 @@ resizestring(pv, newsize)
sv->ob_sval[newsize] = '\0'; sv->ob_sval[newsize] = '\0';
return 0; return 0;
} }
/* Helpers for formatstring */
static object *
getnextarg(args, arglen, p_argidx)
object *args;
int arglen;
int *p_argidx;
{
int argidx = *p_argidx;
if (argidx < arglen) {
(*p_argidx)++;
if (arglen < 0)
return args;
else
return gettupleitem(args, argidx);
}
err_setstr(TypeError, "not enough arguments for format string");
return NULL;
}
#define F_LJUST (1<<0)
#define F_SIGN (1<<1)
#define F_BLANK (1<<2)
#define F_ALT (1<<3)
#define F_ZERO (1<<4)
extern double fabs PROTO((double));
static char *
formatfloat(flags, prec, type, v)
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;
if (prec < 0)
prec = 6;
if (prec > 50)
prec = 50; /* Arbitrary limitation */
if (type == 'f' && fabs(x)/1e25 >= 1e25)
type = 'g';
sprintf(fmt, "%%%s.%d%c", (flags&F_ALT) ? "#" : "", prec, type);
sprintf(buf, fmt, x);
return buf;
}
static char *
formatint(flags, prec, type, v)
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;
if (prec < 0)
prec = 1;
sprintf(fmt, "%%%s.%dl%c", (flags&F_ALT) ? "#" : "", prec, type);
sprintf(buf, fmt, x);
return buf;
}
static char *
formatchar(v)
object *v;
{
static char buf[2];
if (is_stringobject(v)) {
if (!getargs(v, "c;%c requires int or char", &buf[0]))
return NULL;
}
else {
if (!getargs(v, "b;%c requires int or char", &buf[0]))
return NULL;
}
buf[1] = '\0';
return buf;
}
/* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) */
object *
formatstring(format, args)
object *format;
object *args;
{
char *fmt, *res;
int fmtcnt, rescnt, reslen, arglen, argidx;
object *result;
if (format == NULL || !is_stringobject(format) || args == NULL) {
err_badcall();
return NULL;
}
reslen = rescnt = 100;
result = newsizedstringobject((char *)NULL, reslen);
if (result == NULL)
return NULL;
res = getstringvalue(result);
fmt = getstringvalue(format);
fmtcnt = getstringsize(format);
if (is_tupleobject(args)) {
arglen = gettuplesize(args);
argidx = 0;
}
else {
arglen = -1;
argidx = -2;
}
while (--fmtcnt >= 0) {
if (*fmt != '%') {
if (--rescnt < 0) {
rescnt = reslen;
reslen = reslen * 2; /* Maybe less when big? */
if (resizestring(&result, reslen) < 0)
return NULL;
res = getstringvalue(result) + rescnt;
rescnt = reslen - rescnt;
}
*res++ = *fmt++;
}
else {
/* Got a format specifier */
int flags = 0;
char *fmtstart = fmt++;
int width = -1;
int prec = -1;
int size = 0;
int c;
int fill;
object *v;
char *buf;
int sign;
int len;
while (--fmtcnt >= 0) {
switch (c = *fmt++) {
case '-': flags |= F_LJUST; continue;
case '+': flags |= F_SIGN; continue;
case ' ': flags |= F_BLANK; continue;
case '#': flags |= F_ALT; continue;
case '0': flags |= F_ZERO; continue;
}
break;
}
if (c == '*') {
v = getnextarg(args, arglen, &argidx);
if (v == NULL)
goto error;
if (!is_intobject(v)) {
err_setstr(TypeError, "* wants int");
goto error;
}
width = getintvalue(v);
if (width < 0)
width = 0;
if (--fmtcnt >= 0)
c = *fmt++;
}
else if (isdigit(c)) {
width = c - '0';
while (--fmtcnt >= 0) {
c = *fmt++;
if (!isdigit(c))
break;
if ((width*10) / 10 != width) {
err_setstr(ValueError,
"width too big");
goto error;
}
width = width*10 + (c - '0');
}
}
if (c == '.') {
prec = 0;
if (--fmtcnt >= 0)
c = *fmt++;
if (c == '*') {
v = getnextarg(args, arglen, &argidx);
if (v == NULL)
goto error;
if (!is_intobject(v)) {
err_setstr(TypeError,
"* wants int");
goto error;
}
prec = getintvalue(v);
if (prec < 0)
prec = 0;
if (--fmtcnt >= 0)
c = *fmt++;
}
else if (isdigit(c)) {
prec = c - '0';
while (--fmtcnt >= 0) {
c = *fmt++;
if (!isdigit(c))
break;
if ((prec*10) / 10 != prec) {
err_setstr(ValueError,
"prec too big");
goto error;
}
prec = prec*10 + (c - '0');
}
}
} /* prec */
if (fmtcnt >= 0) {
if (c == 'h' || c == 'l' || c == 'L') {
size = c;
if (--fmtcnt >= 0)
c = *fmt++;
}
}
if (fmtcnt < 0) {
err_setstr(ValueError, "incomplete format");
goto error;
}
if (c != '%') {
v = getnextarg(args, arglen, &argidx);
if (v == NULL)
goto error;
}
sign = 0;
fill = ' ';
switch (c) {
case '%':
buf = "%";
len = 1;
break;
case 's':
if (!is_stringobject(v)) {
err_setstr(TypeError,
"%s wants string");
goto error;
}
buf = getstringvalue(v);
len = getstringsize(v);
if (prec >= 0 && len > prec)
len = prec;
break;
case 'i':
case 'd':
case 'u':
case 'o':
case 'x':
case 'X':
if (c == 'i')
c = 'd';
buf = formatint(flags, prec, c, v);
if (buf == NULL)
goto error;
len = strlen(buf);
sign = (c == 'd');
if (flags&F_ZERO)
fill = '0';
break;
case 'e':
case 'E':
case 'f':
case 'g':
case 'G':
buf = formatfloat(flags, prec, c, v);
if (buf == NULL)
goto error;
len = strlen(buf);
sign = 1;
if (flags&F_ZERO)
fill = '0';
break;
case 'c':
buf = formatchar(v);
if (buf == NULL)
goto error;
len = strlen(buf);
break;
default:
err_setstr(ValueError,
"unsupported format character");
goto error;
}
if (sign) {
if (*buf == '-' || *buf == '+') {
sign = *buf++;
len--;
}
else if (flags & F_SIGN)
sign = '+';
else if (flags & F_BLANK)
sign = ' ';
else
sign = '\0';
}
if (width < len)
width = len;
if (rescnt < width + (sign != '\0')) {
rescnt = reslen;
reslen = reslen + width + 100;
if (resizestring(&result, reslen) < 0)
return NULL;
res = getstringvalue(result) + rescnt;
rescnt = reslen - rescnt;
}
if (sign) {
*res++ = sign;
rescnt--;
if (width > len)
width--;
}
if (width > len && !(flags&F_LJUST)) {
do {
--rescnt;
*res++ = fill;
} while (--width > len);
}
memcpy(res, buf, len);
res += len;
rescnt -= len;
while (--width >= len) {
--rescnt;
*res++ = ' ';
}
} /* '%' */
} /* until end */
if (argidx < arglen) {
err_setstr(TypeError, "not all arguments converted");
goto error;
}
resizestring(&result, reslen - rescnt);
return result;
error:
DECREF(result);
return NULL;
}

View File

@ -1,6 +1,6 @@
/*********************************************************** /***********************************************************
Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
Netherlands. Amsterdam, The Netherlands.
All Rights Reserved All Rights Reserved
@ -168,8 +168,11 @@ parsetok(tok, g, start, n_ret)
*n_ret = ps->p_tree; *n_ret = ps->p_tree;
ps->p_tree = NULL; ps->p_tree = NULL;
} }
else if (tok->lineno <= 1 && tok->done == E_EOF) else {
*n_ret = NULL;
if (tok->lineno <= 1 && tok->done == E_EOF)
ret = E_EOF; ret = E_EOF;
}
break; break;
} }
} }

View File

@ -1,6 +1,6 @@
/*********************************************************** /***********************************************************
Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
Netherlands. Amsterdam, The Netherlands.
All Rights Reserved All Rights Reserved

View File

@ -1671,6 +1671,9 @@ rem(v, w)
DECREF(w); DECREF(w);
return x; return x;
} }
if (is_stringobject(v)) {
return formatstring(v, w);
}
err_setstr(TypeError, "bad operand type(s) for %"); err_setstr(TypeError, "bad operand type(s) for %");
return NULL; return NULL;
} }

View File

@ -1,6 +1,6 @@
/*********************************************************** /***********************************************************
Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
Netherlands. Amsterdam, The Netherlands.
All Rights Reserved All Rights Reserved
@ -56,6 +56,7 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/ */
#include "allobjects.h" #include "allobjects.h"
#include "modsupport.h"
#include <errno.h> #include <errno.h>
#ifndef errno #ifndef errno
@ -153,13 +154,11 @@ err_errno(exc)
err_set(KeyboardInterrupt); err_set(KeyboardInterrupt);
return NULL; return NULL;
} }
v = newtupleobject(2); v = mkvalue("(is)", errno, strerror(errno));
if (v != NULL) { if (v != NULL) {
settupleitem(v, 0, newintobject((long)errno));
settupleitem(v, 1, newstringobject(strerror(errno)));
}
err_setval(exc, v); err_setval(exc, v);
XDECREF(v); DECREF(v);
}
return NULL; return NULL;
} }

View File

@ -28,17 +28,6 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "modsupport.h" #include "modsupport.h"
#include "import.h" #include "import.h"
#ifdef HAVE_PROTOTYPES
#define USE_STDARG
#endif
#ifdef USE_STDARG
#include <stdarg.h>
#else
#include <varargs.h>
#endif
object * object *
initmodule(name, methods) initmodule(name, methods)
char *name; char *name;
@ -532,32 +521,34 @@ object *mkvalue(char *format, ...)
object *mkvalue(va_alist) va_dcl object *mkvalue(va_alist) va_dcl
#endif #endif
{ {
int n;
char *f;
va_list va; va_list va;
object* retval; object* retval;
#ifdef USE_STDARG #ifdef USE_STDARG
va_start(va, format); va_start(va, format);
#else #else
char *format; char *format;
va_start(va); va_start(va);
format = va_arg(va, char *); format = va_arg(va, char *);
#endif #endif
f = format; retval = vmkvalue(format, va);
n = countformat(f, '\0');
if (n < 0)
retval = NULL; /* Error in the format */
else if (n == 0) {
retval = None;
INCREF(retval);
}
else if (n == 1)
retval = do_mkvalue(&f, &va);
else
retval = do_mktuple(&f, &va, '\0', n);
va_end(va); va_end(va);
if (retval == NULL)
fprintf(stderr, "format \"%s\", f \"%s\"\n", format, f);
return retval; return retval;
} }
object *
vmkvalue(format, va)
char *format;
va_list va;
{
char *f = format;
int n = countformat(f, '\0');
if (n < 0)
return NULL;
if (n == 0) {
INCREF(None);
return None;
}
if (n == 1)
return do_mkvalue(&f, &va);
return do_mktuple(&f, &va, '\0', n);
}