Use new exceptions.

This commit is contained in:
Guido van Rossum 1991-12-10 14:00:03 +00:00
parent 97ff5308fe
commit 87e7ea72a6
8 changed files with 39 additions and 48 deletions

View File

@ -68,6 +68,8 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "stdwin.h" #include "stdwin.h"
#define StdwinError RuntimeError /* XXX Change this later */
/* Window and menu object types declared here because of forward references */ /* Window and menu object types declared here because of forward references */
typedef struct { typedef struct {
@ -870,7 +872,7 @@ text_draw(self, args)
if (!getrectarg(args, a)) if (!getrectarg(args, a))
return NULL; return NULL;
if (Drawing != NULL) { if (Drawing != NULL) {
err_setstr(RuntimeError, "already drawing"); err_setstr(StdwinError, "already drawing");
return NULL; return NULL;
} }
/* Clip to text area and ignore if area is empty */ /* Clip to text area and ignore if area is empty */
@ -1029,8 +1031,7 @@ text_settext(self, args)
return NULL; return NULL;
size = getstringsize(text); size = getstringsize(text);
if ((buf = NEW(char, size)) == NULL) { if ((buf = NEW(char, size)) == NULL) {
err_set(MemoryError); return err_nomem();
return NULL;
} }
memcpy(buf, getstringvalue(text), size); memcpy(buf, getstringvalue(text), size);
tesetbuf(self->t_text, buf, size); /* Becomes owner of buffer */ tesetbuf(self->t_text, buf, size); /* Becomes owner of buffer */
@ -1145,7 +1146,7 @@ newmenuobject(title)
break; break;
} }
if (id >= MAXNMENU) { if (id >= MAXNMENU) {
err_setstr(MemoryError, "creating too many menus"); err_setstr(StdwinError, "creating too many menus");
return NULL; return NULL;
} }
menu = wmenucreate(id + IDOFFSET, getstringvalue(title)); menu = wmenucreate(id + IDOFFSET, getstringvalue(title));
@ -1374,7 +1375,7 @@ window_begindrawing(wp, args)
if (!getnoarg(args)) if (!getnoarg(args))
return NULL; return NULL;
if (Drawing != NULL) { if (Drawing != NULL) {
err_setstr(RuntimeError, "already drawing"); err_setstr(StdwinError, "already drawing");
return NULL; return NULL;
} }
dp = NEWOBJ(drawingobject, &Drawingtype); dp = NEWOBJ(drawingobject, &Drawingtype);
@ -1596,7 +1597,7 @@ window_setwincursor(self, args)
return NULL; return NULL;
c = wfetchcursor(getstringvalue(str)); c = wfetchcursor(getstringvalue(str));
if (c == NULL) { if (c == NULL) {
err_setstr(RuntimeError, "no such cursor"); err_setstr(StdwinError, "no such cursor");
return NULL; return NULL;
} }
wsetwincursor(self->w_win, c); wsetwincursor(self->w_win, c);
@ -1710,7 +1711,7 @@ stdwin_open(sw, args)
break; break;
} }
if (tag >= MAXNWIN) { if (tag >= MAXNWIN) {
err_setstr(MemoryError, "creating too many windows"); err_setstr(StdwinError, "creating too many windows");
return NULL; return NULL;
} }
wp = NEWOBJ(windowobject, &Windowtype); wp = NEWOBJ(windowobject, &Windowtype);
@ -1766,7 +1767,7 @@ stdwin_get_poll_event(poll, args)
if (!getnoarg(args)) if (!getnoarg(args))
return NULL; return NULL;
if (Drawing != NULL) { if (Drawing != NULL) {
err_setstr(RuntimeError, "cannot getevent() while drawing"); err_setstr(StdwinError, "cannot getevent() while drawing");
return NULL; return NULL;
} }
again: again:

View File

@ -95,7 +95,7 @@ newfileobject(name, mode)
#endif #endif
f->f_fp = fopen(name, mode); f->f_fp = fopen(name, mode);
if (f->f_fp == NULL) { if (f->f_fp == NULL) {
err_errno(RuntimeError); err_errno(IOError);
DECREF(f); DECREF(f);
return NULL; return NULL;
} }
@ -166,7 +166,7 @@ file_close(f, args)
if (sts == EOF) { if (sts == EOF) {
if (errno == 0) if (errno == 0)
errno = EIO; errno = EIO;
return err_errno(RuntimeError); return err_errno(IOError);
} }
if (sts != 0) if (sts != 0)
return newintobject((long)sts); return newintobject((long)sts);
@ -198,7 +198,7 @@ file_seek(f, args)
if (fseek(f->f_fp, offset, (int)whence) != 0) { if (fseek(f->f_fp, offset, (int)whence) != 0) {
if (errno == 0) if (errno == 0)
errno = EIO; errno = EIO;
return err_errno(RuntimeError); return err_errno(IOError);
} }
INCREF(None); INCREF(None);
return None; return None;
@ -219,7 +219,7 @@ file_tell(f, args)
if (offset == -1L) { if (offset == -1L) {
if (errno == 0) if (errno == 0)
errno = EIO; errno = EIO;
return err_errno(RuntimeError); return err_errno(IOError);
} }
return newintobject(offset); return newintobject(offset);
} }
@ -237,7 +237,7 @@ file_flush(f, args)
if (fflush(f->f_fp) != 0) { if (fflush(f->f_fp) != 0) {
if (errno == 0) if (errno == 0)
errno = EIO; errno = EIO;
return err_errno(RuntimeError); return err_errno(IOError);
} }
INCREF(None); INCREF(None);
return None; return None;
@ -441,7 +441,6 @@ file_write(f, args)
object *args; object *args;
{ {
int n, n2; int n, n2;
char *s;
if (f->f_fp == NULL) { if (f->f_fp == NULL) {
err_badarg(); err_badarg();
return NULL; return NULL;
@ -452,20 +451,11 @@ file_write(f, args)
} }
f->f_softspace = 0; f->f_softspace = 0;
errno = 0; errno = 0;
n = getstringsize(args); n2 = fwrite(getstringvalue(args), 1, n = getstringsize(args), f->f_fp);
s = getstringvalue(args);
if (n > BUFSIZ) {
fflush(f->f_fp);
n2 = write(fileno(f->f_fp), s, n);
fflush(f->f_fp);
}
else {
n2 = fwrite(s, 1, n, f->f_fp);
}
if (n2 != n) { if (n2 != n) {
if (errno == 0) if (errno == 0)
errno = EIO; errno = EIO;
err_errno(RuntimeError); err_errno(IOError);
return NULL; return NULL;
} }
INCREF(None); INCREF(None);

View File

@ -543,7 +543,7 @@ listindex(self, args)
if (cmpobject(self->ob_item[i], args) == 0) if (cmpobject(self->ob_item[i], args) == 0)
return newintobject(i); return newintobject(i);
} }
err_setstr(RuntimeError, "list.index(x): x not in list"); err_setstr(ValueError, "list.index(x): x not in list");
return NULL; return NULL;
} }
@ -586,7 +586,7 @@ listremove(self, args)
} }
} }
err_setstr(RuntimeError, "list.remove(x): x not in list"); err_setstr(ValueError, "list.remove(x): x not in list");
return NULL; return NULL;
} }

View File

@ -168,7 +168,7 @@ getlongvalue(vv)
prev = x; prev = x;
x = (x << SHIFT) + v->ob_digit[i]; x = (x << SHIFT) + v->ob_digit[i];
if ((x >> SHIFT) != prev) { if ((x >> SHIFT) != prev) {
err_setstr(RuntimeError, err_setstr(ValueError,
"long int too long to convert"); "long int too long to convert");
return -1; return -1;
} }
@ -422,7 +422,7 @@ long_divrem(a, b, prem)
if (size_b == 0) { if (size_b == 0) {
if (prem != NULL) if (prem != NULL)
*prem = NULL; *prem = NULL;
err_setstr(RuntimeError, "long division by zero"); err_setstr(ZeroDivisionError, "long division or remainder");
return NULL; return NULL;
} }
if (size_a < size_b || if (size_a < size_b ||
@ -938,7 +938,7 @@ long_pow(a, w)
if (size_b == ~0) if (size_b == ~0)
size_b = 0; size_b = 0;
else if (size_b < 0) { else if (size_b < 0) {
err_setstr(RuntimeError, "long integer to the negative power"); err_setstr(ValueError, "long integer to the negative power");
return NULL; return NULL;
} }
@ -1054,11 +1054,11 @@ long_rshift(a, b)
if (shiftby == -1L && err_occurred()) if (shiftby == -1L && err_occurred())
return NULL; return NULL;
if (shiftby < 0) { if (shiftby < 0) {
err_setstr(RuntimeError, "negative shift count"); err_setstr(ValueError, "negative shift count");
return NULL; return NULL;
} }
if (shiftby > MASK) { if (shiftby > MASK) {
err_setstr(RuntimeError, "outrageous shift count"); err_setstr(ValueError, "outrageous shift count");
return NULL; return NULL;
} }
wordshift = shiftby / SHIFT; wordshift = shiftby / SHIFT;
@ -1105,11 +1105,11 @@ long_lshift(a, b)
if (shiftby == -1L && err_occurred()) if (shiftby == -1L && err_occurred())
return NULL; return NULL;
if (shiftby < 0) { if (shiftby < 0) {
err_setstr(RuntimeError, "negative shift count"); err_setstr(ValueError, "negative shift count");
return NULL; return NULL;
} }
if (shiftby > MASK) { if (shiftby > MASK) {
err_setstr(RuntimeError, "outrageous shift count"); err_setstr(ValueError, "outrageous shift count");
return NULL; return NULL;
} }
if (shiftby % SHIFT == 0) { if (shiftby % SHIFT == 0) {

View File

@ -118,7 +118,7 @@ module_getattr(m, name)
} }
res = dictlookup(m->md_dict, name); res = dictlookup(m->md_dict, name);
if (res == NULL) if (res == NULL)
err_setstr(NameError, name); err_setstr(AttributeError, name);
else else
INCREF(res); INCREF(res);
return res; return res;
@ -131,7 +131,7 @@ module_setattr(m, name, v)
object *v; object *v;
{ {
if (strcmp(name, "__dict__") == 0 || strcmp(name, "__name__") == 0) { if (strcmp(name, "__dict__") == 0 || strcmp(name, "__name__") == 0) {
err_setstr(NameError, "can't assign to reserved member name"); err_setstr(TypeError, "can't assign to reserved member name");
return -1; return -1;
} }
if (v == NULL) if (v == NULL)

View File

@ -180,10 +180,10 @@ err_input(err)
case E_OK: case E_OK:
break; break;
case E_SYNTAX: case E_SYNTAX:
err_setstr(RuntimeError, "syntax error"); err_setstr(ValueError, "syntax error");
break; break;
case E_TOKEN: case E_TOKEN:
err_setstr(RuntimeError, "illegal token"); err_setstr(ValueError, "illegal token");
break; break;
case E_INTR: case E_INTR:
err_set(KeyboardInterrupt); err_set(KeyboardInterrupt);
@ -195,7 +195,7 @@ err_input(err)
err_set(EOFError); err_set(EOFError);
break; break;
default: default:
err_setstr(RuntimeError, "unknown input error"); err_setstr(SystemError, "unknown input error");
break; break;
} }
} }

View File

@ -191,7 +191,7 @@ rd_object(fp)
switch (type) { switch (type) {
case EOF: case EOF:
err_setstr(RuntimeError, "EOF read where object expected"); err_setstr(EOFError, "EOF read where object expected");
return NULL; return NULL;
case TYPE_NULL: case TYPE_NULL:
@ -227,7 +227,7 @@ rd_object(fp)
char *end; char *end;
n = rd_byte(fp); n = rd_byte(fp);
if (fread(buf, 1, (int)n, fp) != n) { if (fread(buf, 1, (int)n, fp) != n) {
err_setstr(RuntimeError, err_setstr(EOFError,
"EOF read where object expected"); "EOF read where object expected");
return NULL; return NULL;
} }
@ -235,11 +235,11 @@ rd_object(fp)
errno = 0; errno = 0;
res = strtod(buf, &end); res = strtod(buf, &end);
if (*end != '\0') { if (*end != '\0') {
err_setstr(RuntimeError, "bad float syntax"); err_setstr(ValueError, "bad float syntax");
return NULL; return NULL;
} }
if (errno != 0) { if (errno != 0) {
err_setstr(RuntimeError, err_setstr(ValueError,
"float constant too large"); "float constant too large");
return NULL; return NULL;
} }
@ -253,7 +253,7 @@ rd_object(fp)
if (fread(getstringvalue(v), 1, (int)n, fp) != n) { if (fread(getstringvalue(v), 1, (int)n, fp) != n) {
DECREF(v); DECREF(v);
v = NULL; v = NULL;
err_setstr(RuntimeError, err_setstr(EOFError,
"EOF read where object expected"); "EOF read where object expected");
} }
} }
@ -314,7 +314,7 @@ rd_object(fp)
return v; return v;
default: default:
err_setstr(RuntimeError, "read unknown object"); err_setstr(TypeError, "read unknown object");
return NULL; return NULL;
} }

View File

@ -103,7 +103,7 @@ getmember(addr, mlist, name)
} }
} }
err_setstr(NameError, name); err_setstr(AttributeError, name);
return NULL; return NULL;
} }
@ -119,7 +119,7 @@ setmember(addr, mlist, name, v)
for (l = mlist; l->name != NULL; l++) { for (l = mlist; l->name != NULL; l++) {
if (strcmp(l->name, name) == 0) { if (strcmp(l->name, name) == 0) {
if (l->readonly || l->type == T_STRING) { if (l->readonly || l->type == T_STRING) {
err_setstr(RuntimeError, "readonly attribute"); err_setstr(TypeError, "readonly attribute");
return -1; return -1;
} }
addr += l->offset; addr += l->offset;
@ -178,6 +178,6 @@ setmember(addr, mlist, name, v)
} }
} }
err_setstr(NameError, name); err_setstr(AttributeError, name);
return -1; return -1;
} }