blob: c7e95282142855b18b0750c3ec725594f95eeb74 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum6d023c91995-01-04 19:12:13 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032/* 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 Rossum3f5da241990-12-20 15:06:42 +000045 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 Rossum85a5fbb1990-10-14 12:07:46 +000047 - errno is a global variable, which makes extensions to a multi-
48 threading environment difficult; e.g., in IRIX, multi-threaded
Guido van Rossum3f5da241990-12-20 15:06:42 +000049 programs must use the function oserror() instead of looking in errno
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000050 - 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 Rossum3f5da241990-12-20 15:06:42 +000058 built-in function calls err_set(exception), err_setval(exception,
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000059 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 Rossum3f5da241990-12-20 15:06:42 +000065#include "allobjects.h"
Guido van Rossum6989e541994-09-29 09:39:39 +000066#include "traceback.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000067
Guido van Rossumf22120a1990-12-20 23:05:40 +000068#include <errno.h>
Guido van Rossumf22120a1990-12-20 23:05:40 +000069
Jack Janseneceb3e31995-06-27 13:15:15 +000070#ifdef SYMANTEC__CFM68K__
Guido van Rossume9fbc091995-02-18 14:52:19 +000071#pragma lib_export on
72#endif
73
Jack Jansen8fd2d941994-12-14 12:54:54 +000074#ifdef macintosh
Guido van Rossum6976a521997-04-11 19:18:23 +000075#ifndef __MSL__
Guido van Rossume9fbc091995-02-18 14:52:19 +000076/* 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 Jansen8fd2d941994-12-14 12:54:54 +000079*/
Guido van Rossum53e8d441995-03-09 12:11:31 +000080extern char *PyMac_StrError PROTO((int));
Jack Jansen5ef86d51995-01-19 12:16:44 +000081#undef strerror
Guido van Rossume9fbc091995-02-18 14:52:19 +000082#define strerror PyMac_StrError
Guido van Rossum6976a521997-04-11 19:18:23 +000083#endif
Guido van Rossume9fbc091995-02-18 14:52:19 +000084#endif /* macintosh */
85
Guido van Rossum53e8d441995-03-09 12:11:31 +000086#ifndef __STDC__
Guido van Rossum7844e381997-04-11 20:44:04 +000087#ifndef MS_WINDOWS
Guido van Rossumf5401bd1990-11-02 17:50:28 +000088extern char *strerror PROTO((int));
Guido van Rossum53e8d441995-03-09 12:11:31 +000089#endif
Guido van Rossum7844e381997-04-11 20:44:04 +000090#endif
Guido van Rossumf5401bd1990-11-02 17:50:28 +000091
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000092/* Last exception stored by err_setval() */
93
94static object *last_exception;
95static object *last_exc_val;
96
97void
Guido van Rossum1ae940a1995-01-02 19:04:15 +000098err_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
111void
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000112err_setval(exception, value)
113 object *exception;
114 object *value;
115{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000116 XINCREF(exception);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000117 XINCREF(value);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000118 err_restore(exception, value, (object *)NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000119}
120
121void
122err_set(exception)
123 object *exception;
124{
125 err_setval(exception, (object *)NULL);
126}
127
128void
129err_setstr(exception, string)
130 object *exception;
Guido van Rossum067998f1996-12-10 15:33:34 +0000131 const char *string;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000132{
133 object *value = newstringobject(string);
134 err_setval(exception, value);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000135 XDECREF(value);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000136}
137
Guido van Rossum3a241811994-08-29 12:14:12 +0000138
139object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000140err_occurred()
141{
Guido van Rossum3a241811994-08-29 12:14:12 +0000142 return last_exception;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000143}
144
145void
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000146err_fetch(p_exc, p_val, p_tb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000147 object **p_exc;
148 object **p_val;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000149 object **p_tb;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000150{
151 *p_exc = last_exception;
152 last_exception = NULL;
153 *p_val = last_exc_val;
154 last_exc_val = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000155 *p_tb = tb_fetch();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000156}
157
158void
159err_clear()
160{
Guido van Rossum6989e541994-09-29 09:39:39 +0000161 object *tb;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000162 XDECREF(last_exception);
163 last_exception = NULL;
164 XDECREF(last_exc_val);
165 last_exc_val = NULL;
Guido van Rossum6989e541994-09-29 09:39:39 +0000166 /* Also clear interpreter stack trace */
167 tb = tb_fetch();
168 XDECREF(tb);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000169}
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000170
171/* Convenience functions to set a type error exception and return 0 */
172
173int
174err_badarg()
175{
Guido van Rossumf1ac4031990-11-09 15:05:53 +0000176 err_setstr(TypeError, "illegal argument type for built-in operation");
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000177 return 0;
178}
179
180object *
181err_nomem()
182{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000183 err_set(MemoryError);
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000184 return NULL;
185}
186
187object *
188err_errno(exc)
189 object *exc;
190{
Guido van Rossum5063bab1991-10-20 20:14:56 +0000191 object *v;
Guido van Rossum3a241811994-08-29 12:14:12 +0000192 int i = errno;
Guido van Rossume9fbc091995-02-18 14:52:19 +0000193#ifdef EINTR
Guido van Rossum3a241811994-08-29 12:14:12 +0000194 if (i == EINTR && sigcheck())
Guido van Rossum5063bab1991-10-20 20:14:56 +0000195 return NULL;
Guido van Rossume9fbc091995-02-18 14:52:19 +0000196#endif
Guido van Rossum3a241811994-08-29 12:14:12 +0000197 v = mkvalue("(is)", i, strerror(i));
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000198 if (v != NULL) {
Guido van Rossume5372401993-03-16 12:15:04 +0000199 err_setval(exc, v);
200 DECREF(v);
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000201 }
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000202 return NULL;
203}
Guido van Rossum683a0721990-10-21 22:09:12 +0000204
205void
206err_badcall()
207{
208 err_setstr(SystemError, "bad argument to internal function");
209}
Guido van Rossum1548bac1997-02-14 17:09:47 +0000210
211
212#ifdef HAVE_STDARG_PROTOTYPES
213PyObject *
214PyErr_Format(PyObject *exception, const char *format, ...)
215#else
216PyObject *
217PyErr_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 Rossum1548bac1997-02-14 17:09:47 +0000225
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}