Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 6d023c9 | 1995-01-04 19:12:13 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 32 | /* Error handling -- see also run.c */ |
| 33 | |
| 34 | /* New error handling interface. |
| 35 | |
| 36 | The following problem exists (existed): methods of built-in modules |
| 37 | are called with 'self' and 'args' arguments, but without a context |
| 38 | argument, so they have no way to raise a specific exception. |
| 39 | The same is true for the object implementations: no context argument. |
| 40 | The old convention was to set 'errno' and to return NULL. |
| 41 | The caller (usually call_function() in eval.c) detects the NULL |
| 42 | return value and then calls puterrno(ctx) to turn the errno value |
| 43 | into a true exception. Problems with this approach are: |
| 44 | - it used standard errno values to indicate Python-specific errors, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 45 | but this means that when such an error code is reported by a system |
| 46 | call (e.g., in module posix), the user gets a confusing message |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 47 | - errno is a global variable, which makes extensions to a multi- |
| 48 | threading environment difficult; e.g., in IRIX, multi-threaded |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 49 | programs must use the function oserror() instead of looking in errno |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 50 | - there is no portable way to add new error numbers for specic |
| 51 | situations -- the value space for errno is reserved to the OS, yet |
| 52 | the way to turn module-specific errors into a module-specific |
| 53 | exception requires module-specific values for errno |
| 54 | - there is no way to add a more situation-specific message to an |
| 55 | error. |
| 56 | |
| 57 | The new interface solves all these problems. To return an error, a |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 58 | built-in function calls err_set(exception), err_setval(exception, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 59 | value) or err_setstr(exception, string), and returns NULL. These |
| 60 | functions save the value for later use by puterrno(). To adapt this |
| 61 | scheme to a multi-threaded environment, only the implementation of |
| 62 | err_setval() has to be changed. |
| 63 | */ |
| 64 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 65 | #include "allobjects.h" |
Guido van Rossum | 6989e54 | 1994-09-29 09:39:39 +0000 | [diff] [blame] | 66 | #include "traceback.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 67 | |
Guido van Rossum | f22120a | 1990-12-20 23:05:40 +0000 | [diff] [blame] | 68 | #include <errno.h> |
Guido van Rossum | f22120a | 1990-12-20 23:05:40 +0000 | [diff] [blame] | 69 | |
Jack Jansen | eceb3e3 | 1995-06-27 13:15:15 +0000 | [diff] [blame] | 70 | #ifdef SYMANTEC__CFM68K__ |
Guido van Rossum | e9fbc09 | 1995-02-18 14:52:19 +0000 | [diff] [blame] | 71 | #pragma lib_export on |
| 72 | #endif |
| 73 | |
Jack Jansen | 8fd2d94 | 1994-12-14 12:54:54 +0000 | [diff] [blame] | 74 | #ifdef macintosh |
Guido van Rossum | 6976a52 | 1997-04-11 19:18:23 +0000 | [diff] [blame] | 75 | #ifndef __MSL__ |
Guido van Rossum | e9fbc09 | 1995-02-18 14:52:19 +0000 | [diff] [blame] | 76 | /* Replace strerror with a Mac specific routine. |
| 77 | XXX PROBLEM: some positive errors have a meaning for MacOS, |
| 78 | but some library routines set Unix error numbers... |
Jack Jansen | 8fd2d94 | 1994-12-14 12:54:54 +0000 | [diff] [blame] | 79 | */ |
Guido van Rossum | 53e8d44 | 1995-03-09 12:11:31 +0000 | [diff] [blame] | 80 | extern char *PyMac_StrError PROTO((int)); |
Jack Jansen | 5ef86d5 | 1995-01-19 12:16:44 +0000 | [diff] [blame] | 81 | #undef strerror |
Guido van Rossum | e9fbc09 | 1995-02-18 14:52:19 +0000 | [diff] [blame] | 82 | #define strerror PyMac_StrError |
Guido van Rossum | 6976a52 | 1997-04-11 19:18:23 +0000 | [diff] [blame] | 83 | #endif |
Guido van Rossum | e9fbc09 | 1995-02-18 14:52:19 +0000 | [diff] [blame] | 84 | #endif /* macintosh */ |
| 85 | |
Guido van Rossum | 53e8d44 | 1995-03-09 12:11:31 +0000 | [diff] [blame] | 86 | #ifndef __STDC__ |
Guido van Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame^] | 87 | #ifndef MS_WINDOWS |
Guido van Rossum | f5401bd | 1990-11-02 17:50:28 +0000 | [diff] [blame] | 88 | extern char *strerror PROTO((int)); |
Guido van Rossum | 53e8d44 | 1995-03-09 12:11:31 +0000 | [diff] [blame] | 89 | #endif |
Guido van Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame^] | 90 | #endif |
Guido van Rossum | f5401bd | 1990-11-02 17:50:28 +0000 | [diff] [blame] | 91 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 92 | /* Last exception stored by err_setval() */ |
| 93 | |
| 94 | static object *last_exception; |
| 95 | static object *last_exc_val; |
| 96 | |
| 97 | void |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 98 | err_restore(exception, value, traceback) |
| 99 | object *exception; |
| 100 | object *value; |
| 101 | object *traceback; |
| 102 | { |
| 103 | err_clear(); |
| 104 | |
| 105 | last_exception = exception; |
| 106 | last_exc_val = value; |
| 107 | (void) tb_store(traceback); |
| 108 | XDECREF(traceback); |
| 109 | } |
| 110 | |
| 111 | void |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 112 | err_setval(exception, value) |
| 113 | object *exception; |
| 114 | object *value; |
| 115 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 116 | XINCREF(exception); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 117 | XINCREF(value); |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 118 | err_restore(exception, value, (object *)NULL); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | void |
| 122 | err_set(exception) |
| 123 | object *exception; |
| 124 | { |
| 125 | err_setval(exception, (object *)NULL); |
| 126 | } |
| 127 | |
| 128 | void |
| 129 | err_setstr(exception, string) |
| 130 | object *exception; |
Guido van Rossum | 067998f | 1996-12-10 15:33:34 +0000 | [diff] [blame] | 131 | const char *string; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 132 | { |
| 133 | object *value = newstringobject(string); |
| 134 | err_setval(exception, value); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 135 | XDECREF(value); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Guido van Rossum | 3a24181 | 1994-08-29 12:14:12 +0000 | [diff] [blame] | 138 | |
| 139 | object * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 140 | err_occurred() |
| 141 | { |
Guido van Rossum | 3a24181 | 1994-08-29 12:14:12 +0000 | [diff] [blame] | 142 | return last_exception; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | void |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 146 | err_fetch(p_exc, p_val, p_tb) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 147 | object **p_exc; |
| 148 | object **p_val; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 149 | object **p_tb; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 150 | { |
| 151 | *p_exc = last_exception; |
| 152 | last_exception = NULL; |
| 153 | *p_val = last_exc_val; |
| 154 | last_exc_val = NULL; |
Guido van Rossum | 1ae940a | 1995-01-02 19:04:15 +0000 | [diff] [blame] | 155 | *p_tb = tb_fetch(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | void |
| 159 | err_clear() |
| 160 | { |
Guido van Rossum | 6989e54 | 1994-09-29 09:39:39 +0000 | [diff] [blame] | 161 | object *tb; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 162 | XDECREF(last_exception); |
| 163 | last_exception = NULL; |
| 164 | XDECREF(last_exc_val); |
| 165 | last_exc_val = NULL; |
Guido van Rossum | 6989e54 | 1994-09-29 09:39:39 +0000 | [diff] [blame] | 166 | /* Also clear interpreter stack trace */ |
| 167 | tb = tb_fetch(); |
| 168 | XDECREF(tb); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 169 | } |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 170 | |
| 171 | /* Convenience functions to set a type error exception and return 0 */ |
| 172 | |
| 173 | int |
| 174 | err_badarg() |
| 175 | { |
Guido van Rossum | f1ac403 | 1990-11-09 15:05:53 +0000 | [diff] [blame] | 176 | err_setstr(TypeError, "illegal argument type for built-in operation"); |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | object * |
| 181 | err_nomem() |
| 182 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 183 | err_set(MemoryError); |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 184 | return NULL; |
| 185 | } |
| 186 | |
| 187 | object * |
| 188 | err_errno(exc) |
| 189 | object *exc; |
| 190 | { |
Guido van Rossum | 5063bab | 1991-10-20 20:14:56 +0000 | [diff] [blame] | 191 | object *v; |
Guido van Rossum | 3a24181 | 1994-08-29 12:14:12 +0000 | [diff] [blame] | 192 | int i = errno; |
Guido van Rossum | e9fbc09 | 1995-02-18 14:52:19 +0000 | [diff] [blame] | 193 | #ifdef EINTR |
Guido van Rossum | 3a24181 | 1994-08-29 12:14:12 +0000 | [diff] [blame] | 194 | if (i == EINTR && sigcheck()) |
Guido van Rossum | 5063bab | 1991-10-20 20:14:56 +0000 | [diff] [blame] | 195 | return NULL; |
Guido van Rossum | e9fbc09 | 1995-02-18 14:52:19 +0000 | [diff] [blame] | 196 | #endif |
Guido van Rossum | 3a24181 | 1994-08-29 12:14:12 +0000 | [diff] [blame] | 197 | v = mkvalue("(is)", i, strerror(i)); |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 198 | if (v != NULL) { |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 199 | err_setval(exc, v); |
| 200 | DECREF(v); |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 201 | } |
Guido van Rossum | 7d310eb | 1990-10-14 20:00:05 +0000 | [diff] [blame] | 202 | return NULL; |
| 203 | } |
Guido van Rossum | 683a072 | 1990-10-21 22:09:12 +0000 | [diff] [blame] | 204 | |
| 205 | void |
| 206 | err_badcall() |
| 207 | { |
| 208 | err_setstr(SystemError, "bad argument to internal function"); |
| 209 | } |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 210 | |
| 211 | |
| 212 | #ifdef HAVE_STDARG_PROTOTYPES |
| 213 | PyObject * |
| 214 | PyErr_Format(PyObject *exception, const char *format, ...) |
| 215 | #else |
| 216 | PyObject * |
| 217 | PyErr_Format(exception, format, va_alist) |
| 218 | PyObject *exception; |
| 219 | const char *format; |
| 220 | va_dcl |
| 221 | #endif |
| 222 | { |
| 223 | va_list vargs; |
| 224 | char buffer[500]; /* Caller is responsible for limiting the format */ |
Guido van Rossum | 1548bac | 1997-02-14 17:09:47 +0000 | [diff] [blame] | 225 | |
| 226 | #ifdef HAVE_STDARG_PROTOTYPES |
| 227 | va_start(vargs, format); |
| 228 | #else |
| 229 | va_start(vargs); |
| 230 | #endif |
| 231 | |
| 232 | vsprintf(buffer, format, vargs); |
| 233 | PyErr_SetString(exception, buffer); |
| 234 | return NULL; |
| 235 | } |