Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1 | /* 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 Rossum | 17e66f6 | 1990-10-26 14:53:07 +0000 | [diff] [blame] | 39 | #include "intobject.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 40 | #include "stringobject.h" |
Guido van Rossum | 17e66f6 | 1990-10-26 14:53:07 +0000 | [diff] [blame] | 41 | #include "tupleobject.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 42 | #include "errors.h" |
| 43 | |
Guido van Rossum | f5401bd | 1990-11-02 17:50:28 +0000 | [diff] [blame] | 44 | extern char *strerror PROTO((int)); |
| 45 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 46 | /* Last exception stored by err_setval() */ |
| 47 | |
| 48 | static object *last_exception; |
| 49 | static object *last_exc_val; |
| 50 | |
| 51 | void |
| 52 | err_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 | |
| 69 | void |
| 70 | err_set(exception) |
| 71 | object *exception; |
| 72 | { |
| 73 | err_setval(exception, (object *)NULL); |
| 74 | } |
| 75 | |
| 76 | void |
| 77 | err_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 | |
| 87 | int |
| 88 | err_occurred() |
| 89 | { |
| 90 | return last_exception != NULL; |
| 91 | } |
| 92 | |
| 93 | void |
| 94 | err_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 | |
| 104 | void |
| 105 | err_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 Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 116 | |
| 117 | /* Convenience functions to set a type error exception and return 0 */ |
| 118 | |
| 119 | int |
| 120 | err_badarg() |
| 121 | { |
| 122 | err_setstr(TypeError, "illegal argument type for built-in function"); |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | object * |
| 127 | err_nomem() |
| 128 | { |
| 129 | err_setstr(MemoryError, "in built-in function"); |
| 130 | return NULL; |
| 131 | } |
| 132 | |
| 133 | object * |
| 134 | err_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 Rossum | 683a072 | 1990-10-21 22:09:12 +0000 | [diff] [blame] | 147 | |
| 148 | void |
| 149 | err_badcall() |
| 150 | { |
| 151 | err_setstr(SystemError, "bad argument to internal function"); |
| 152 | } |