blob: 8ddde51d39b49c519115c5cb1f197469efe98ec8 [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
Guido van Rossumf5401bd1990-11-02 17:50:28 +000044extern char *strerror PROTO((int));
45
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046/* Last exception stored by err_setval() */
47
48static object *last_exception;
49static object *last_exc_val;
50
51void
52err_setval(exception, value)
53 object *exception;
54 object *value;
55{
56 if (last_exception != NULL)
57 DECREF(last_exception);
58 if (exception != NULL)
59 INCREF(exception);
60 last_exception = exception;
61
62 if (last_exc_val != NULL)
63 DECREF(last_exc_val);
64 if (value != NULL)
65 INCREF(value);
66 last_exc_val = value;
67}
68
69void
70err_set(exception)
71 object *exception;
72{
73 err_setval(exception, (object *)NULL);
74}
75
76void
77err_setstr(exception, string)
78 object *exception;
79 char *string;
80{
81 object *value = newstringobject(string);
82 err_setval(exception, value);
83 if (value != NULL)
84 DECREF(value);
85}
86
87int
88err_occurred()
89{
90 return last_exception != NULL;
91}
92
93void
94err_get(p_exc, p_val)
95 object **p_exc;
96 object **p_val;
97{
98 *p_exc = last_exception;
99 last_exception = NULL;
100 *p_val = last_exc_val;
101 last_exc_val = NULL;
102}
103
104void
105err_clear()
106{
107 if (last_exception != NULL) {
108 DECREF(last_exception);
109 last_exception = NULL;
110 }
111 if (last_exc_val != NULL) {
112 DECREF(last_exc_val);
113 last_exc_val = NULL;
114 }
115}
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000116
117/* Convenience functions to set a type error exception and return 0 */
118
119int
120err_badarg()
121{
Guido van Rossumf1ac4031990-11-09 15:05:53 +0000122 err_setstr(TypeError, "illegal argument type for built-in operation");
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000123 return 0;
124}
125
126object *
127err_nomem()
128{
129 err_setstr(MemoryError, "in built-in function");
130 return NULL;
131}
132
133object *
134err_errno(exc)
135 object *exc;
136{
137 object *v = newtupleobject(2);
138 if (v != NULL) {
139 settupleitem(v, 0, newintobject((long)errno));
140 settupleitem(v, 1, newstringobject(strerror(errno)));
141 }
142 err_setval(exc, v);
143 if (v != NULL)
144 DECREF(v);
145 return NULL;
146}
Guido van Rossum683a0721990-10-21 22:09:12 +0000147
148void
149err_badcall()
150{
151 err_setstr(SystemError, "bad argument to internal function");
152}