blob: a1ab4b873cf38a11c6714cc4ccc0cba21b7e5eeb [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 Rossume9fbc091995-02-18 14:52:19 +000075/* Replace strerror with a Mac specific routine.
76 XXX PROBLEM: some positive errors have a meaning for MacOS,
77 but some library routines set Unix error numbers...
Jack Jansen8fd2d941994-12-14 12:54:54 +000078*/
Guido van Rossum53e8d441995-03-09 12:11:31 +000079extern char *PyMac_StrError PROTO((int));
Jack Jansen5ef86d51995-01-19 12:16:44 +000080#undef strerror
Guido van Rossume9fbc091995-02-18 14:52:19 +000081#define strerror PyMac_StrError
82#endif /* macintosh */
83
Guido van Rossum53e8d441995-03-09 12:11:31 +000084#ifndef __STDC__
Guido van Rossumf5401bd1990-11-02 17:50:28 +000085extern char *strerror PROTO((int));
Guido van Rossum53e8d441995-03-09 12:11:31 +000086#endif
Guido van Rossumf5401bd1990-11-02 17:50:28 +000087
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000088/* Last exception stored by err_setval() */
89
90static object *last_exception;
91static object *last_exc_val;
92
93void
Guido van Rossum1ae940a1995-01-02 19:04:15 +000094err_restore(exception, value, traceback)
95 object *exception;
96 object *value;
97 object *traceback;
98{
99 err_clear();
100
101 last_exception = exception;
102 last_exc_val = value;
103 (void) tb_store(traceback);
104 XDECREF(traceback);
105}
106
107void
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000108err_setval(exception, value)
109 object *exception;
110 object *value;
111{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000112 XINCREF(exception);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000113 XINCREF(value);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000114 err_restore(exception, value, (object *)NULL);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000115}
116
117void
118err_set(exception)
119 object *exception;
120{
121 err_setval(exception, (object *)NULL);
122}
123
124void
125err_setstr(exception, string)
126 object *exception;
Guido van Rossum067998f1996-12-10 15:33:34 +0000127 const char *string;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000128{
129 object *value = newstringobject(string);
130 err_setval(exception, value);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000131 XDECREF(value);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000132}
133
Guido van Rossum3a241811994-08-29 12:14:12 +0000134
135object *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000136err_occurred()
137{
Guido van Rossum3a241811994-08-29 12:14:12 +0000138 return last_exception;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000139}
140
141void
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000142err_fetch(p_exc, p_val, p_tb)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000143 object **p_exc;
144 object **p_val;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000145 object **p_tb;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000146{
147 *p_exc = last_exception;
148 last_exception = NULL;
149 *p_val = last_exc_val;
150 last_exc_val = NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000151 *p_tb = tb_fetch();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000152}
153
154void
155err_clear()
156{
Guido van Rossum6989e541994-09-29 09:39:39 +0000157 object *tb;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000158 XDECREF(last_exception);
159 last_exception = NULL;
160 XDECREF(last_exc_val);
161 last_exc_val = NULL;
Guido van Rossum6989e541994-09-29 09:39:39 +0000162 /* Also clear interpreter stack trace */
163 tb = tb_fetch();
164 XDECREF(tb);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000165}
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000166
167/* Convenience functions to set a type error exception and return 0 */
168
169int
170err_badarg()
171{
Guido van Rossumf1ac4031990-11-09 15:05:53 +0000172 err_setstr(TypeError, "illegal argument type for built-in operation");
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000173 return 0;
174}
175
176object *
177err_nomem()
178{
Guido van Rossum3f5da241990-12-20 15:06:42 +0000179 err_set(MemoryError);
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000180 return NULL;
181}
182
183object *
184err_errno(exc)
185 object *exc;
186{
Guido van Rossum5063bab1991-10-20 20:14:56 +0000187 object *v;
Guido van Rossum3a241811994-08-29 12:14:12 +0000188 int i = errno;
Guido van Rossume9fbc091995-02-18 14:52:19 +0000189#ifdef EINTR
Guido van Rossum3a241811994-08-29 12:14:12 +0000190 if (i == EINTR && sigcheck())
Guido van Rossum5063bab1991-10-20 20:14:56 +0000191 return NULL;
Guido van Rossume9fbc091995-02-18 14:52:19 +0000192#endif
Guido van Rossum3a241811994-08-29 12:14:12 +0000193 v = mkvalue("(is)", i, strerror(i));
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000194 if (v != NULL) {
Guido van Rossume5372401993-03-16 12:15:04 +0000195 err_setval(exc, v);
196 DECREF(v);
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000197 }
Guido van Rossum7d310eb1990-10-14 20:00:05 +0000198 return NULL;
199}
Guido van Rossum683a0721990-10-21 22:09:12 +0000200
201void
202err_badcall()
203{
204 err_setstr(SystemError, "bad argument to internal function");
205}