Quickly renamed. Also removed the long comment explaining why this is
better than the old error API.
This commit is contained in:
parent
58d8e3dd9e
commit
373c869a6a
146
Python/errors.c
146
Python/errors.c
|
@ -29,43 +29,9 @@ PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
|
||||||
******************************************************************/
|
******************************************************************/
|
||||||
|
|
||||||
/* Error handling -- see also run.c */
|
/* Error handling */
|
||||||
|
|
||||||
/* New error handling interface.
|
#include "Python.h"
|
||||||
|
|
||||||
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,
|
|
||||||
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
|
|
||||||
- errno is a global variable, which makes extensions to a multi-
|
|
||||||
threading environment difficult; e.g., in IRIX, multi-threaded
|
|
||||||
programs must use the function oserror() instead of looking in errno
|
|
||||||
- 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
|
|
||||||
built-in function calls err_set(exception), err_setval(exception,
|
|
||||||
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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "allobjects.h"
|
|
||||||
#include "traceback.h"
|
|
||||||
|
|
||||||
#include <errno.h>
|
|
||||||
|
|
||||||
#ifdef SYMANTEC__CFM68K__
|
#ifdef SYMANTEC__CFM68K__
|
||||||
#pragma lib_export on
|
#pragma lib_export on
|
||||||
|
@ -77,7 +43,7 @@ PERFORMANCE OF THIS SOFTWARE.
|
||||||
XXX PROBLEM: some positive errors have a meaning for MacOS,
|
XXX PROBLEM: some positive errors have a meaning for MacOS,
|
||||||
but some library routines set Unix error numbers...
|
but some library routines set Unix error numbers...
|
||||||
*/
|
*/
|
||||||
extern char *PyMac_StrError PROTO((int));
|
extern char *PyMac_StrError Py_PROTO((int));
|
||||||
#undef strerror
|
#undef strerror
|
||||||
#define strerror PyMac_StrError
|
#define strerror PyMac_StrError
|
||||||
#endif
|
#endif
|
||||||
|
@ -85,127 +51,127 @@ extern char *PyMac_StrError PROTO((int));
|
||||||
|
|
||||||
#ifndef __STDC__
|
#ifndef __STDC__
|
||||||
#ifndef MS_WINDOWS
|
#ifndef MS_WINDOWS
|
||||||
extern char *strerror PROTO((int));
|
extern char *strerror Py_PROTO((int));
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Last exception stored by err_setval() */
|
/* Last exception stored */
|
||||||
|
|
||||||
static object *last_exception;
|
static PyObject *last_exception;
|
||||||
static object *last_exc_val;
|
static PyObject *last_exc_val;
|
||||||
|
|
||||||
void
|
void
|
||||||
err_restore(exception, value, traceback)
|
PyErr_Restore(exception, value, traceback)
|
||||||
object *exception;
|
PyObject *exception;
|
||||||
object *value;
|
PyObject *value;
|
||||||
object *traceback;
|
PyObject *traceback;
|
||||||
{
|
{
|
||||||
err_clear();
|
PyErr_Clear();
|
||||||
|
|
||||||
last_exception = exception;
|
last_exception = exception;
|
||||||
last_exc_val = value;
|
last_exc_val = value;
|
||||||
(void) tb_store(traceback);
|
(void) PyTraceBack_Store(traceback);
|
||||||
XDECREF(traceback);
|
Py_XDECREF(traceback);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
err_setval(exception, value)
|
PyErr_SetObject(exception, value)
|
||||||
object *exception;
|
PyObject *exception;
|
||||||
object *value;
|
PyObject *value;
|
||||||
{
|
{
|
||||||
XINCREF(exception);
|
Py_XINCREF(exception);
|
||||||
XINCREF(value);
|
Py_XINCREF(value);
|
||||||
err_restore(exception, value, (object *)NULL);
|
PyErr_Restore(exception, value, (PyObject *)NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
err_set(exception)
|
PyErr_SetNone(exception)
|
||||||
object *exception;
|
PyObject *exception;
|
||||||
{
|
{
|
||||||
err_setval(exception, (object *)NULL);
|
PyErr_SetObject(exception, (PyObject *)NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
err_setstr(exception, string)
|
PyErr_SetString(exception, string)
|
||||||
object *exception;
|
PyObject *exception;
|
||||||
const char *string;
|
const char *string;
|
||||||
{
|
{
|
||||||
object *value = newstringobject(string);
|
PyObject *value = PyString_FromString(string);
|
||||||
err_setval(exception, value);
|
PyErr_SetObject(exception, value);
|
||||||
XDECREF(value);
|
Py_XDECREF(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
object *
|
PyObject *
|
||||||
err_occurred()
|
PyErr_Occurred()
|
||||||
{
|
{
|
||||||
return last_exception;
|
return last_exception;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
err_fetch(p_exc, p_val, p_tb)
|
PyErr_Fetch(p_exc, p_val, p_tb)
|
||||||
object **p_exc;
|
PyObject **p_exc;
|
||||||
object **p_val;
|
PyObject **p_val;
|
||||||
object **p_tb;
|
PyObject **p_tb;
|
||||||
{
|
{
|
||||||
*p_exc = last_exception;
|
*p_exc = last_exception;
|
||||||
last_exception = NULL;
|
last_exception = NULL;
|
||||||
*p_val = last_exc_val;
|
*p_val = last_exc_val;
|
||||||
last_exc_val = NULL;
|
last_exc_val = NULL;
|
||||||
*p_tb = tb_fetch();
|
*p_tb = PyTraceBack_Fetch();
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
err_clear()
|
PyErr_Clear()
|
||||||
{
|
{
|
||||||
object *tb;
|
PyObject *tb;
|
||||||
XDECREF(last_exception);
|
Py_XDECREF(last_exception);
|
||||||
last_exception = NULL;
|
last_exception = NULL;
|
||||||
XDECREF(last_exc_val);
|
Py_XDECREF(last_exc_val);
|
||||||
last_exc_val = NULL;
|
last_exc_val = NULL;
|
||||||
/* Also clear interpreter stack trace */
|
/* Also clear interpreter stack trace */
|
||||||
tb = tb_fetch();
|
tb = PyTraceBack_Fetch();
|
||||||
XDECREF(tb);
|
Py_XDECREF(tb);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Convenience functions to set a type error exception and return 0 */
|
/* Convenience functions to set a type error exception and return 0 */
|
||||||
|
|
||||||
int
|
int
|
||||||
err_badarg()
|
PyErr_BadArgument()
|
||||||
{
|
{
|
||||||
err_setstr(TypeError, "illegal argument type for built-in operation");
|
PyErr_SetString(PyExc_TypeError, "illegal argument type for built-in operation");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
object *
|
PyObject *
|
||||||
err_nomem()
|
PyErr_NoMemory()
|
||||||
{
|
{
|
||||||
err_set(MemoryError);
|
PyErr_SetNone(PyExc_MemoryError);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
object *
|
PyObject *
|
||||||
err_errno(exc)
|
PyErr_SetFromErrno(exc)
|
||||||
object *exc;
|
PyObject *exc;
|
||||||
{
|
{
|
||||||
object *v;
|
PyObject *v;
|
||||||
int i = errno;
|
int i = errno;
|
||||||
#ifdef EINTR
|
#ifdef EINTR
|
||||||
if (i == EINTR && sigcheck())
|
if (i == EINTR && PyErr_CheckSignals())
|
||||||
return NULL;
|
return NULL;
|
||||||
#endif
|
#endif
|
||||||
v = mkvalue("(is)", i, strerror(i));
|
v = Py_BuildValue("(is)", i, strerror(i));
|
||||||
if (v != NULL) {
|
if (v != NULL) {
|
||||||
err_setval(exc, v);
|
PyErr_SetObject(exc, v);
|
||||||
DECREF(v);
|
Py_DECREF(v);
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
err_badcall()
|
PyErr_BadInternalCall()
|
||||||
{
|
{
|
||||||
err_setstr(SystemError, "bad argument to internal function");
|
PyErr_SetString(PyExc_SystemError, "bad argument to internal function");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue