blob: 18f63a09b5d8ddb7204c84c4a58d52cb33da59d6 [file] [log] [blame]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001/* Error handling -- see also run.c */
2
3/* New error handling interface.
4
5 The following problem exists (existed): methods of built-in modules
6 are called with 'self' and 'args' arguments, but without a context
7 argument, so they have no way to raise a specific exception.
8 The same is true for the object implementations: no context argument.
9 The old convention was to set 'errno' and to return NULL.
10 The caller (usually call_function() in eval.c) detects the NULL
11 return value and then calls puterrno(ctx) to turn the errno value
12 into a true exception. Problems with this approach are:
13 - it used standard errno values to indicate Python-specific errors,
14 but this means that when such an error code is reported by UNIX the
15 user gets a confusing message
16 - errno is a global variable, which makes extensions to a multi-
17 threading environment difficult; e.g., in IRIX, multi-threaded
18 programs must use the function getoserror() (sp.?) instead of
19 looking in errno
20 - there is no portable way to add new error numbers for specic
21 situations -- the value space for errno is reserved to the OS, yet
22 the way to turn module-specific errors into a module-specific
23 exception requires module-specific values for errno
24 - there is no way to add a more situation-specific message to an
25 error.
26
27 The new interface solves all these problems. To return an error, a
28 built-in function calls err_set(exception), err_set(valexception,
29 value) or err_setstr(exception, string), and returns NULL. These
30 functions save the value for later use by puterrno(). To adapt this
31 scheme to a multi-threaded environment, only the implementation of
32 err_setval() has to be changed.
33*/
34
35#include <stdio.h>
36
37#include "PROTO.h"
38#include "object.h"
Guido van Rossum17e66f61990-10-26 14:53:07 +000039#include "intobject.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000040#include "stringobject.h"
Guido van Rossum17e66f61990-10-26 14:53:07 +000041#include "tupleobject.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000042#include "errors.h"
43
44/* Last exception stored by err_setval() */
45
46static object *last_exception;
47static object *last_exc_val;
48
49void
50err_setval(exception, value)
51 object *exception;
52 object *value;
53{
54 if (last_exception != NULL)
55 DECREF(last_exception);
56 if (exception != NULL)
57 INCREF(exception);
58 last_exception = exception;
59
60 if (last_exc_val != NULL)
61 DECREF(last_exc_val);
62 if (value != NULL)
63 INCREF(value);
64 last_exc_val = value;
65}
66
67void
68err_set(exception)
69 object *exception;
70{
71 err_setval(exception, (object *)NULL);
72}
73
74void
75err_setstr(exception, string)
76 object *exception;
77 char *string;
78{
79 object *value = newstringobject(string);
80 err_setval(exception, value);
81 if (value != NULL)
82 DECREF(value);
83}
84
85int
86err_occurred()
87{
88 return last_exception != NULL;
89}
90
91void
92err_get(p_exc, p_val)
93 object **p_exc;
94 object **p_val;
95{
96 *p_exc = last_exception;
97 last_exception = NULL;
98 *p_val = last_exc_val;
99 last_exc_val = NULL;
100}
101
102void
103err_clear()
104{
105 if (last_exception != NULL) {
106 DECREF(last_exception);
107 last_exception = NULL;
108 }
109 if (last_exc_val != NULL) {
110 DECREF(last_exc_val);
111 last_exc_val = NULL;
112 }
113}
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000114
115/* Convenience functions to set a type error exception and return 0 */
116
117int
118err_badarg()
119{
120 err_setstr(TypeError, "illegal argument type for built-in function");
121 return 0;
122}
123
124object *
125err_nomem()
126{
127 err_setstr(MemoryError, "in built-in function");
128 return NULL;
129}
130
131object *
132err_errno(exc)
133 object *exc;
134{
135 object *v = newtupleobject(2);
136 if (v != NULL) {
137 settupleitem(v, 0, newintobject((long)errno));
138 settupleitem(v, 1, newstringobject(strerror(errno)));
139 }
140 err_setval(exc, v);
141 if (v != NULL)
142 DECREF(v);
143 return NULL;
144}
Guido van Rossum683a0721990-10-21 22:09:12 +0000145
146void
147err_badcall()
148{
149 err_setstr(SystemError, "bad argument to internal function");
150}