{
register classmemberobject *cm;
if (!is_classobject(class)) {
- errno = EINVAL;
+ err_badcall();
return NULL;
}
cm = NEWOBJ(classmemberobject, &Classmembertype);
}
v = class_getattr(cm->cm_class, name);
if (v == NULL)
- return v; /* class_getattr() has set errno */
+ return v; /* class_getattr() has set the error */
if (is_funcobject(v)) {
object *w = newclassmethodobject(v, (object *)cm);
DECREF(v);
return w;
}
DECREF(v);
- errno = ESRCH;
+ err_setstr(NameError, name);
return NULL;
}
{
register classmethodobject *cm;
if (!is_funcobject(func)) {
- errno = EINVAL;
+ err_badcall();
return NULL;
}
cm = NEWOBJ(classmethodobject, &Classmethodtype);
register object *cm;
{
if (!is_classmethodobject(cm)) {
- errno = EINVAL;
+ err_badcall();
return NULL;
}
return ((classmethodobject *)cm)->cm_func;
register object *cm;
{
if (!is_classmethodobject(cm)) {
- errno = EINVAL;
+ err_badcall();
return NULL;
}
return ((classmethodobject *)cm)->cm_self;
/* Float object implementation */
+/* XXX There should be overflow checks here, but it's hard to check
+ for any kind of float exception without losing portability. */
+
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include "floatobject.h"
#include "stringobject.h"
#include "objimpl.h"
+#include "errors.h"
object *
newfloatobject(fval)
{
/* For efficiency, this code is copied from newobject() */
register floatobject *op = (floatobject *) malloc(sizeof(floatobject));
- if (op == NULL) {
- errno = ENOMEM;
- }
- else {
- NEWREF(op);
- op->ob_type = &Floattype;
- op->ob_fval = fval;
- }
+ if (op == NULL)
+ return err_nomem();
+ NEWREF(op);
+ op->ob_type = &Floattype;
+ op->ob_fval = fval;
return (object *) op;
}
object *op;
{
if (!is_floatobject(op)) {
- errno = EBADF;
+ err_badarg();
return -1;
}
else
object *w;
{
if (!is_floatobject(w)) {
- errno = EINVAL;
+ err_badarg();
return NULL;
}
return newfloatobject(v->ob_fval + ((floatobject *)w) -> ob_fval);
object *w;
{
if (!is_floatobject(w)) {
- errno = EINVAL;
+ err_badarg();
return NULL;
}
return newfloatobject(v->ob_fval - ((floatobject *)w) -> ob_fval);
object *w;
{
if (!is_floatobject(w)) {
- errno = EINVAL;
+ err_badarg();
return NULL;
}
return newfloatobject(v->ob_fval * ((floatobject *)w) -> ob_fval);
object *w;
{
if (!is_floatobject(w)) {
- errno = EINVAL;
+ err_badarg();
return NULL;
}
if (((floatobject *)w) -> ob_fval == 0) {
- errno = EDOM;
+ err_setstr(ZeroDivisionError, "float division by zero");
return NULL;
}
return newfloatobject(v->ob_fval / ((floatobject *)w) -> ob_fval);
double wx;
extern double fmod();
if (!is_floatobject(w)) {
- errno = EINVAL;
+ err_badarg();
return NULL;
}
wx = ((floatobject *)w) -> ob_fval;
if (wx == 0.0) {
- errno = EDOM;
+ err_setstr(ZeroDivisionError, "float division by zero");
return NULL;
}
return newfloatobject(fmod(v->ob_fval, wx));
double iv, iw, ix;
extern double pow();
if (!is_floatobject(w)) {
- errno = EINVAL;
+ err_badarg();
return NULL;
}
iv = v->ob_fval;
iw = ((floatobject *)w)->ob_fval;
+ if (iw == 0.0)
+ return newfloatobject(1.0); /* x**0 is always 1, even 0**0 */
errno = 0;
ix = pow(iv, iw);
- if (errno != 0)
+ if (errno != 0) {
+ /* XXX could it be another type of error? */
+ err_errno(OverflowError);
return NULL;
- else
- return newfloatobject(ix);
+ }
+ return newfloatobject(ix);
}
static object *
object *op;
{
if (!is_funcobject(op)) {
- errno = EBADF;
+ err_badcall();
return NULL;
}
return ((funcobject *) op) -> func_node;
object *op;
{
if (!is_funcobject(op)) {
- errno = EBADF;
+ err_badcall();
return NULL;
}
return ((funcobject *) op) -> func_globals;
#include "listobject.h"
#include "objimpl.h"
#include "modsupport.h"
+#include "errors.h"
typedef struct {
OB_VARHEAD
int i;
listobject *op;
if (size < 0) {
- errno = EINVAL;
+ err_badcall();
return NULL;
}
op = (listobject *) malloc(sizeof(listobject));
if (op == NULL) {
- errno = ENOMEM;
- return NULL;
+ return err_nomem();
}
if (size <= 0) {
op->ob_item = NULL;
op->ob_item = (object **) malloc(size * sizeof(object *));
if (op->ob_item == NULL) {
free((ANY *)op);
- errno = ENOMEM;
- return NULL;
+ return err_nomem();
}
}
NEWREF(op);
object *op;
{
if (!is_listobject(op)) {
- errno = EBADF;
+ err_badcall();
return -1;
}
else
int i;
{
if (!is_listobject(op)) {
- errno = EBADF;
+ err_badcall();
return NULL;
}
if (i < 0 || i >= ((listobject *)op) -> ob_size) {
- errno = EDOM;
+ err_setstr(IndexError, "list index out of range");
return NULL;
}
return ((listobject *)op) -> ob_item[i];
if (!is_listobject(op)) {
if (newitem != NULL)
DECREF(newitem);
- return errno = EBADF;
+ err_badcall();
+ return -1;
}
if (i < 0 || i >= ((listobject *)op) -> ob_size) {
if (newitem != NULL)
DECREF(newitem);
- return errno = EDOM;
+ err_setstr(IndexError, "list assignment index out of range");
+ return -1;
}
olditem = ((listobject *)op) -> ob_item[i];
((listobject *)op) -> ob_item[i] = newitem;
{
int i;
object **items;
- if (v == NULL)
- return errno = EINVAL;
+ if (v == NULL) {
+ err_badcall();
+ return -1;
+ }
items = self->ob_item;
RESIZE(items, object *, self->ob_size+1);
- if (items == NULL)
- return errno = ENOMEM;
+ if (items == NULL) {
+ err_nomem();
+ return -1;
+ }
if (where < 0)
where = 0;
if (where > self->ob_size)
int where;
object *newitem;
{
- if (!is_listobject(op))
- return errno = EBADF;
+ if (!is_listobject(op)) {
+ err_badcall();
+ return -1;
+ }
return ins1((listobject *)op, where, newitem);
}
object *op;
object *newitem;
{
- if (!is_listobject(op))
- return errno = EBADF;
+ if (!is_listobject(op)) {
+ err_badcall();
+ return -1;
+ }
return ins1((listobject *)op,
(int) ((listobject *)op)->ob_size, newitem);
}
int i;
{
if (i < 0 || i >= a->ob_size) {
- errno = EDOM;
+ err_setstr(IndexError, "list index out of range");
return NULL;
}
INCREF(a->ob_item[i]);
int i;
listobject *np;
if (!is_listobject(bb)) {
- errno = EINVAL;
+ err_badarg();
return NULL;
}
#define b ((listobject *)bb)
size = a->ob_size + b->ob_size;
np = (listobject *) newlistobject(size);
if (np == NULL) {
- errno = ENOMEM;
- return NULL;
+ return err_nomem();
}
for (i = 0; i < a->ob_size; i++) {
object *v = a->ob_item[i];
int i;
object *v;
{
- if (i < 0 || i >= a->ob_size)
- return errno = EDOM;
+ if (i < 0 || i >= a->ob_size) {
+ err_setstr(IndexError, "list assignment index out of range");
+ return -1;
+ }
if (v == NULL)
return list_ass_slice(a, i, i+1, v);
INCREF(v);
n = 0;
else if (is_listobject(v))
n = b->ob_size;
- else
- return errno = EINVAL;
+ else {
+ err_badarg();
+ return -1;
+ }
if (ilow < 0)
ilow = 0;
else if (ilow > a->ob_size)
}
else { /* Insert d items; DECREF ihigh-ilow items */
RESIZE(item, object *, a->ob_size + d);
- if (item == NULL)
- return errno = ENOMEM;
+ if (item == NULL) {
+ err_nomem();
+ return -1;
+ }
for (k = a->ob_size; --k >= ihigh; )
item[k+d] = item[k];
for (/*k = ihigh-1*/; k >= ilow; --k)
{
int i;
if (args == NULL || !is_tupleobject(args) || gettuplesize(args) != 2) {
- errno = EINVAL;
+ err_badarg();
return NULL;
}
if (!getintarg(gettupleitem(args, 0), &i))
object *args;
{
if (args != NULL) {
- errno = EINVAL;
+ err_badarg();
return NULL;
}
- errno = 0;
+ err_clear();
if (self->ob_size > 1)
qsort((char *)self->ob_item,
(int) self->ob_size, sizeof(object *), cmp);
- if (errno != 0)
+ if (err_occurred())
return NULL;
INCREF(None);
return None;
#include "methodobject.h"
#include "objimpl.h"
#include "token.h"
+#include "errors.h"
typedef struct {
OB_HEAD
object *op;
{
if (!is_methodobject(op)) {
- errno = EBADF;
+ err_badcall();
return NULL;
}
return ((methodobject *)op) -> m_meth;
object *op;
{
if (!is_methodobject(op)) {
- errno = EBADF;
+ err_badcall();
return NULL;
}
return ((methodobject *)op) -> m_self;
#include "stringobject.h"
#include "intobject.h"
#include "objimpl.h"
+#include "errors.h"
object *
newsizedstringobject(str, size)
{
register stringobject *op = (stringobject *)
malloc(sizeof(stringobject) + size * sizeof(char));
- if (op == NULL) {
- errno = ENOMEM;
- }
- else {
- NEWREF(op);
- op->ob_type = &Stringtype;
- op->ob_size = size;
- if (str != NULL)
- memcpy(op->ob_sval, str, size);
- op->ob_sval[size] = '\0';
- }
+ if (op == NULL)
+ return err_nomem();
+ NEWREF(op);
+ op->ob_type = &Stringtype;
+ op->ob_size = size;
+ if (str != NULL)
+ memcpy(op->ob_sval, str, size);
+ op->ob_sval[size] = '\0';
return (object *) op;
}
register unsigned int size = strlen(str);
register stringobject *op = (stringobject *)
malloc(sizeof(stringobject) + size * sizeof(char));
- if (op == NULL) {
- errno = ENOMEM;
- }
- else {
- NEWREF(op);
- op->ob_type = &Stringtype;
- op->ob_size = size;
- strcpy(op->ob_sval, str);
- }
+ if (op == NULL)
+ return err_nomem();
+ NEWREF(op);
+ op->ob_type = &Stringtype;
+ op->ob_size = size;
+ strcpy(op->ob_sval, str);
return (object *) op;
}
register object *op;
{
if (!is_stringobject(op)) {
- errno = EBADF;
+ err_badcall();
return -1;
}
return ((stringobject *)op) -> ob_size;
register object *op;
{
if (!is_stringobject(op)) {
- errno = EBADF;
+ err_badcall();
return NULL;
}
return ((stringobject *)op) -> ob_sval;
int newsize = 2 + 4 * op->ob_size * sizeof(char);
object *v = newsizedstringobject((char *)NULL, newsize);
if (v == NULL) {
- errno = ENOMEM;
+ return err_nomem();
}
else {
register int i;
*p++ = '\'';
*p = '\0';
resizestring(&v, (int) (p - ((stringobject *)v)->ob_sval));
+ return v;
}
- return v;
}
static int
register unsigned int size;
register stringobject *op;
if (!is_stringobject(bb)) {
- errno = EINVAL;
+ err_badarg();
return NULL;
}
#define b ((stringobject *)bb)
size = a->ob_size + b->ob_size;
op = (stringobject *)
malloc(sizeof(stringobject) + size * sizeof(char));
- if (op == NULL) {
- errno = ENOMEM;
- }
- else {
- NEWREF(op);
- op->ob_type = &Stringtype;
- op->ob_size = size;
- memcpy(op->ob_sval, a->ob_sval, (int) a->ob_size);
- memcpy(op->ob_sval + a->ob_size, b->ob_sval, (int) b->ob_size);
- op->ob_sval[size] = '\0';
- }
+ if (op == NULL)
+ return err_nomem();
+ NEWREF(op);
+ op->ob_type = &Stringtype;
+ op->ob_size = size;
+ memcpy(op->ob_sval, a->ob_sval, (int) a->ob_size);
+ memcpy(op->ob_sval + a->ob_size, b->ob_sval, (int) b->ob_size);
+ op->ob_sval[size] = '\0';
return (object *) op;
#undef b
}
}
op = (stringobject *)
malloc(sizeof(stringobject) + size * sizeof(char));
- if (op == NULL) {
- errno = ENOMEM;
- }
- else {
- NEWREF(op);
- op->ob_type = &Stringtype;
- op->ob_size = size;
- for (i = 0; i < size; i += a->ob_size)
- memcpy(op->ob_sval+i, a->ob_sval, (int) a->ob_size);
- op->ob_sval[size] = '\0';
- }
+ if (op == NULL)
+ return err_nomem();
+ NEWREF(op);
+ op->ob_type = &Stringtype;
+ op->ob_size = size;
+ for (i = 0; i < size; i += a->ob_size)
+ memcpy(op->ob_sval+i, a->ob_sval, (int) a->ob_size);
+ op->ob_sval[size] = '\0';
return (object *) op;
}
register int i;
{
if (i < 0 || i >= a->ob_size) {
- errno = EDOM;
+ err_setstr(IndexError, "string index out of range");
return NULL;
}
return stringslice(a, i, i+1);
if (!is_stringobject(v) || v->ob_refcnt != 1) {
*pv = 0;
DECREF(v);
- return errno = EBADF;
+ err_badcall();
+ return -1;
}
*pv = (object *)
realloc((char *)v,
sizeof(stringobject) + newsize * sizeof(char));
if (*pv == NULL) {
DECREF(v);
- return errno = ENOMEM;
+ err_nomem();
+ return -1;
}
v = (stringobject *) *pv;
v->ob_size = newsize;
#include "tupleobject.h"
#include "intobject.h"
#include "objimpl.h"
+#include "errors.h"
typedef struct {
OB_VARHEAD
register int i;
register tupleobject *op;
if (size < 0) {
- errno = EINVAL;
+ err_badcall();
return NULL;
}
op = (tupleobject *)
malloc(sizeof(tupleobject) + size * sizeof(object *));
- if (op == NULL) {
- errno = ENOMEM;
- return NULL;
- }
+ if (op == NULL)
+ return err_nomem();
NEWREF(op);
op->ob_type = &Tupletype;
op->ob_size = size;
register object *op;
{
if (!is_tupleobject(op)) {
- errno = EBADF;
+ err_badcall();
return -1;
}
else
register int i;
{
if (!is_tupleobject(op)) {
- errno = EBADF;
+ err_badcall();
return NULL;
}
if (i < 0 || i >= ((tupleobject *)op) -> ob_size) {
- errno = EDOM;
+ err_setstr(IndexError, "tuple index out of range");
return NULL;
}
return ((tupleobject *)op) -> ob_item[i];
if (!is_tupleobject(op)) {
if (newitem != NULL)
DECREF(newitem);
- return errno = EBADF;
+ err_badcall();
+ return -1;
}
if (i < 0 || i >= ((tupleobject *)op) -> ob_size) {
if (newitem != NULL)
DECREF(newitem);
- return errno = EDOM;
+ err_setstr(IndexError, "tuple assignment index out of range");
+ return -1;
}
olditem = ((tupleobject *)op) -> ob_item[i];
((tupleobject *)op) -> ob_item[i] = newitem;
register int i;
{
if (i < 0 || i >= a->ob_size) {
- errno = EDOM;
+ err_setstr(IndexError, "tuple index out of range");
return NULL;
}
INCREF(a->ob_item[i]);
register int i;
tupleobject *np;
if (!is_tupleobject(bb)) {
- errno = EINVAL;
+ err_badarg();
return NULL;
}
#define b ((tupleobject *)bb)
size = a->ob_size + b->ob_size;
np = (tupleobject *) newtupleobject(size);
if (np == NULL) {
- errno = ENOMEM;
- return NULL;
+ return err_nomem();
}
for (i = 0; i < a->ob_size; i++) {
object *v = a->ob_item[i];