1991-02-19 08:39:46 -04:00
|
|
|
/***********************************************************
|
1995-01-04 15:12:13 -04:00
|
|
|
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
|
|
|
|
The Netherlands.
|
1991-02-19 08:39:46 -04:00
|
|
|
|
|
|
|
All Rights Reserved
|
|
|
|
|
1996-10-25 11:44:06 -03:00
|
|
|
Permission to use, copy, modify, and distribute this software and its
|
|
|
|
documentation for any purpose and without fee is hereby granted,
|
1991-02-19 08:39:46 -04:00
|
|
|
provided that the above copyright notice appear in all copies and that
|
1996-10-25 11:44:06 -03:00
|
|
|
both that copyright notice and this permission notice appear in
|
1991-02-19 08:39:46 -04:00
|
|
|
supporting documentation, and that the names of Stichting Mathematisch
|
1996-10-25 11:44:06 -03:00
|
|
|
Centrum or CWI or Corporation for National Research Initiatives or
|
|
|
|
CNRI not be used in advertising or publicity pertaining to
|
|
|
|
distribution of the software without specific, written prior
|
|
|
|
permission.
|
1991-02-19 08:39:46 -04:00
|
|
|
|
1996-10-25 11:44:06 -03:00
|
|
|
While CWI is the initial source for this software, a modified version
|
|
|
|
is made available by the Corporation for National Research Initiatives
|
|
|
|
(CNRI) at the Internet address ftp://ftp.python.org.
|
|
|
|
|
|
|
|
STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
|
|
|
|
REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
|
|
|
|
CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
|
|
|
|
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
|
|
|
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
|
|
|
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
PERFORMANCE OF THIS SOFTWARE.
|
1991-02-19 08:39:46 -04:00
|
|
|
|
|
|
|
******************************************************************/
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
/* Error handling -- see also run.c */
|
|
|
|
|
|
|
|
/* New error handling interface.
|
|
|
|
|
|
|
|
The following problem exists (existed): methods of built-in modules
|
|
|
|
are called with 'self' and 'args' arguments, but without a context
|
|
|
|
argument, so they have no way to raise a specific exception.
|
|
|
|
The same is true for the object implementations: no context argument.
|
|
|
|
The old convention was to set 'errno' and to return NULL.
|
|
|
|
The caller (usually call_function() in eval.c) detects the NULL
|
|
|
|
return value and then calls puterrno(ctx) to turn the errno value
|
|
|
|
into a true exception. Problems with this approach are:
|
|
|
|
- it used standard errno values to indicate Python-specific errors,
|
1990-12-20 11:06:42 -04:00
|
|
|
but this means that when such an error code is reported by a system
|
|
|
|
call (e.g., in module posix), the user gets a confusing message
|
1990-10-14 09:07:46 -03:00
|
|
|
- errno is a global variable, which makes extensions to a multi-
|
|
|
|
threading environment difficult; e.g., in IRIX, multi-threaded
|
1990-12-20 11:06:42 -04:00
|
|
|
programs must use the function oserror() instead of looking in errno
|
1990-10-14 09:07:46 -03:00
|
|
|
- there is no portable way to add new error numbers for specic
|
|
|
|
situations -- the value space for errno is reserved to the OS, yet
|
|
|
|
the way to turn module-specific errors into a module-specific
|
|
|
|
exception requires module-specific values for errno
|
|
|
|
- there is no way to add a more situation-specific message to an
|
|
|
|
error.
|
|
|
|
|
|
|
|
The new interface solves all these problems. To return an error, a
|
1990-12-20 11:06:42 -04:00
|
|
|
built-in function calls err_set(exception), err_setval(exception,
|
1990-10-14 09:07:46 -03:00
|
|
|
value) or err_setstr(exception, string), and returns NULL. These
|
|
|
|
functions save the value for later use by puterrno(). To adapt this
|
|
|
|
scheme to a multi-threaded environment, only the implementation of
|
|
|
|
err_setval() has to be changed.
|
|
|
|
*/
|
|
|
|
|
1990-12-20 11:06:42 -04:00
|
|
|
#include "allobjects.h"
|
1994-09-29 06:39:39 -03:00
|
|
|
#include "traceback.h"
|
1990-12-20 11:06:42 -04:00
|
|
|
|
1990-12-20 19:05:40 -04:00
|
|
|
#include <errno.h>
|
|
|
|
|
1995-06-27 10:15:15 -03:00
|
|
|
#ifdef SYMANTEC__CFM68K__
|
1995-02-18 10:52:19 -04:00
|
|
|
#pragma lib_export on
|
|
|
|
#endif
|
|
|
|
|
1994-12-14 08:54:54 -04:00
|
|
|
#ifdef macintosh
|
1997-04-11 16:18:23 -03:00
|
|
|
#ifndef __MSL__
|
1995-02-18 10:52:19 -04:00
|
|
|
/* Replace strerror with a Mac specific routine.
|
|
|
|
XXX PROBLEM: some positive errors have a meaning for MacOS,
|
|
|
|
but some library routines set Unix error numbers...
|
1994-12-14 08:54:54 -04:00
|
|
|
*/
|
1995-03-09 08:11:31 -04:00
|
|
|
extern char *PyMac_StrError PROTO((int));
|
1995-01-19 08:16:44 -04:00
|
|
|
#undef strerror
|
1995-02-18 10:52:19 -04:00
|
|
|
#define strerror PyMac_StrError
|
1997-04-11 16:18:23 -03:00
|
|
|
#endif
|
1995-02-18 10:52:19 -04:00
|
|
|
#endif /* macintosh */
|
|
|
|
|
1995-03-09 08:11:31 -04:00
|
|
|
#ifndef __STDC__
|
1990-11-02 13:50:28 -04:00
|
|
|
extern char *strerror PROTO((int));
|
1995-03-09 08:11:31 -04:00
|
|
|
#endif
|
1990-11-02 13:50:28 -04:00
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
/* Last exception stored by err_setval() */
|
|
|
|
|
|
|
|
static object *last_exception;
|
|
|
|
static object *last_exc_val;
|
|
|
|
|
|
|
|
void
|
1995-01-02 15:04:15 -04:00
|
|
|
err_restore(exception, value, traceback)
|
1990-10-14 09:07:46 -03:00
|
|
|
object *exception;
|
|
|
|
object *value;
|
1995-01-02 15:04:15 -04:00
|
|
|
object *traceback;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1994-11-10 18:34:33 -04:00
|
|
|
err_clear();
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
last_exception = exception;
|
|
|
|
last_exc_val = value;
|
1995-01-02 15:04:15 -04:00
|
|
|
(void) tb_store(traceback);
|
|
|
|
XDECREF(traceback);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
err_setval(exception, value)
|
|
|
|
object *exception;
|
|
|
|
object *value;
|
|
|
|
{
|
|
|
|
XINCREF(exception);
|
|
|
|
XINCREF(value);
|
|
|
|
err_restore(exception, value, (object *)NULL);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
err_set(exception)
|
|
|
|
object *exception;
|
|
|
|
{
|
|
|
|
err_setval(exception, (object *)NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
err_setstr(exception, string)
|
|
|
|
object *exception;
|
1996-12-10 11:33:34 -04:00
|
|
|
const char *string;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
|
|
|
object *value = newstringobject(string);
|
|
|
|
err_setval(exception, value);
|
1990-12-20 11:06:42 -04:00
|
|
|
XDECREF(value);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1994-08-29 09:14:12 -03:00
|
|
|
|
|
|
|
object *
|
1990-10-14 09:07:46 -03:00
|
|
|
err_occurred()
|
|
|
|
{
|
1994-08-29 09:14:12 -03:00
|
|
|
return last_exception;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1995-01-02 15:04:15 -04:00
|
|
|
err_fetch(p_exc, p_val, p_tb)
|
1990-10-14 09:07:46 -03:00
|
|
|
object **p_exc;
|
|
|
|
object **p_val;
|
1995-01-02 15:04:15 -04:00
|
|
|
object **p_tb;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
|
|
|
*p_exc = last_exception;
|
|
|
|
last_exception = NULL;
|
|
|
|
*p_val = last_exc_val;
|
|
|
|
last_exc_val = NULL;
|
1995-01-02 15:04:15 -04:00
|
|
|
*p_tb = tb_fetch();
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
err_clear()
|
|
|
|
{
|
1994-09-29 06:39:39 -03:00
|
|
|
object *tb;
|
1990-12-20 11:06:42 -04:00
|
|
|
XDECREF(last_exception);
|
|
|
|
last_exception = NULL;
|
|
|
|
XDECREF(last_exc_val);
|
|
|
|
last_exc_val = NULL;
|
1994-09-29 06:39:39 -03:00
|
|
|
/* Also clear interpreter stack trace */
|
|
|
|
tb = tb_fetch();
|
|
|
|
XDECREF(tb);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
1990-10-14 17:00:05 -03:00
|
|
|
|
|
|
|
/* Convenience functions to set a type error exception and return 0 */
|
|
|
|
|
|
|
|
int
|
|
|
|
err_badarg()
|
|
|
|
{
|
1990-11-09 11:05:53 -04:00
|
|
|
err_setstr(TypeError, "illegal argument type for built-in operation");
|
1990-10-14 17:00:05 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
object *
|
|
|
|
err_nomem()
|
|
|
|
{
|
1990-12-20 11:06:42 -04:00
|
|
|
err_set(MemoryError);
|
1990-10-14 17:00:05 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
object *
|
|
|
|
err_errno(exc)
|
|
|
|
object *exc;
|
|
|
|
{
|
1991-10-20 17:14:56 -03:00
|
|
|
object *v;
|
1994-08-29 09:14:12 -03:00
|
|
|
int i = errno;
|
1995-02-18 10:52:19 -04:00
|
|
|
#ifdef EINTR
|
1994-08-29 09:14:12 -03:00
|
|
|
if (i == EINTR && sigcheck())
|
1991-10-20 17:14:56 -03:00
|
|
|
return NULL;
|
1995-02-18 10:52:19 -04:00
|
|
|
#endif
|
1994-08-29 09:14:12 -03:00
|
|
|
v = mkvalue("(is)", i, strerror(i));
|
1990-10-14 17:00:05 -03:00
|
|
|
if (v != NULL) {
|
1993-03-16 08:15:04 -04:00
|
|
|
err_setval(exc, v);
|
|
|
|
DECREF(v);
|
1990-10-14 17:00:05 -03:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
1990-10-21 19:09:12 -03:00
|
|
|
|
|
|
|
void
|
|
|
|
err_badcall()
|
|
|
|
{
|
|
|
|
err_setstr(SystemError, "bad argument to internal function");
|
|
|
|
}
|
1997-02-14 13:09:47 -04:00
|
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_STDARG_PROTOTYPES
|
|
|
|
PyObject *
|
|
|
|
PyErr_Format(PyObject *exception, const char *format, ...)
|
|
|
|
#else
|
|
|
|
PyObject *
|
|
|
|
PyErr_Format(exception, format, va_alist)
|
|
|
|
PyObject *exception;
|
|
|
|
const char *format;
|
|
|
|
va_dcl
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
va_list vargs;
|
|
|
|
char buffer[500]; /* Caller is responsible for limiting the format */
|
|
|
|
|
|
|
|
#ifdef HAVE_STDARG_PROTOTYPES
|
|
|
|
va_start(vargs, format);
|
|
|
|
#else
|
|
|
|
va_start(vargs);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
vsprintf(buffer, format, vargs);
|
|
|
|
PyErr_SetString(exception, buffer);
|
|
|
|
return NULL;
|
|
|
|
}
|