| 
									
										
										
										
											1991-02-19 12:39:46 +00:00
										 |  |  | /***********************************************************
 | 
					
						
							| 
									
										
										
										
											1994-08-29 12:14:12 +00:00
										 |  |  | Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum, | 
					
						
							| 
									
										
										
										
											1993-03-16 12:15:04 +00:00
										 |  |  | Amsterdam, The Netherlands. | 
					
						
							| 
									
										
										
										
											1991-02-19 12:39:46 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |                         All Rights Reserved | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Permission to use, copy, modify, and distribute this software and its  | 
					
						
							|  |  |  | documentation for any purpose and without fee is hereby granted,  | 
					
						
							|  |  |  | provided that the above copyright notice appear in all copies and that | 
					
						
							|  |  |  | both that copyright notice and this permission notice appear in  | 
					
						
							|  |  |  | supporting documentation, and that the names of Stichting Mathematisch | 
					
						
							|  |  |  | Centrum or CWI not be used in advertising or publicity pertaining to | 
					
						
							|  |  |  | distribution of the software without specific, written prior permission. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO | 
					
						
							|  |  |  | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND | 
					
						
							|  |  |  | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE | 
					
						
							|  |  |  | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | 
					
						
							|  |  |  | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | 
					
						
							|  |  |  | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT | 
					
						
							|  |  |  | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ******************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | /* Error handling -- see also run.c */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* New error handling interface.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    The following problem exists (existed): methods of built-in modules | 
					
						
							|  |  |  |    are called with 'self' and 'args' arguments, but without a context | 
					
						
							|  |  |  |    argument, so they have no way to raise a specific exception. | 
					
						
							|  |  |  |    The same is true for the object implementations: no context argument. | 
					
						
							|  |  |  |    The old convention was to set 'errno' and to return NULL. | 
					
						
							|  |  |  |    The caller (usually call_function() in eval.c) detects the NULL | 
					
						
							|  |  |  |    return value and then calls puterrno(ctx) to turn the errno value | 
					
						
							|  |  |  |    into a true exception.  Problems with this approach are: | 
					
						
							|  |  |  |    - it used standard errno values to indicate Python-specific errors, | 
					
						
							| 
									
										
										
										
											1990-12-20 15:06:42 +00:00
										 |  |  |      but this means that when such an error code is reported by a system | 
					
						
							|  |  |  |      call (e.g., in module posix), the user gets a confusing message | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  |    - errno is a global variable, which makes extensions to a multi- | 
					
						
							|  |  |  |      threading environment difficult; e.g., in IRIX, multi-threaded | 
					
						
							| 
									
										
										
										
											1990-12-20 15:06:42 +00:00
										 |  |  |      programs must use the function oserror() instead of looking in errno | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  |    - there is no portable way to add new error numbers for specic | 
					
						
							|  |  |  |      situations -- the value space for errno is reserved to the OS, yet | 
					
						
							|  |  |  |      the way to turn module-specific errors into a module-specific | 
					
						
							|  |  |  |      exception requires module-specific values for errno | 
					
						
							|  |  |  |    - there is no way to add a more situation-specific message to an | 
					
						
							|  |  |  |      error. | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   The new interface solves all these problems.  To return an error, a | 
					
						
							| 
									
										
										
										
											1990-12-20 15:06:42 +00:00
										 |  |  |   built-in function calls err_set(exception), err_setval(exception, | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  |   value) or err_setstr(exception, string), and returns NULL.  These | 
					
						
							|  |  |  |   functions save the value for later use by puterrno().  To adapt this | 
					
						
							|  |  |  |   scheme to a multi-threaded environment, only the implementation of | 
					
						
							|  |  |  |   err_setval() has to be changed. | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1990-12-20 15:06:42 +00:00
										 |  |  | #include "allobjects.h"
 | 
					
						
							| 
									
										
										
										
											1994-09-29 09:39:39 +00:00
										 |  |  | #include "traceback.h"
 | 
					
						
							| 
									
										
										
										
											1990-12-20 15:06:42 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1990-12-20 23:05:40 +00:00
										 |  |  | #include <errno.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1990-11-02 17:50:28 +00:00
										 |  |  | extern char *strerror PROTO((int)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | /* Last exception stored by err_setval() */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static object *last_exception; | 
					
						
							|  |  |  | static object *last_exc_val; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							|  |  |  | err_setval(exception, value) | 
					
						
							|  |  |  | 	object *exception; | 
					
						
							|  |  |  | 	object *value; | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											1994-11-10 22:34:33 +00:00
										 |  |  | 	err_clear(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1990-12-20 15:06:42 +00:00
										 |  |  | 	XINCREF(exception); | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | 	last_exception = exception; | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											1990-12-20 15:06:42 +00:00
										 |  |  | 	XINCREF(value); | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | 	last_exc_val = value; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							|  |  |  | err_set(exception) | 
					
						
							|  |  |  | 	object *exception; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	err_setval(exception, (object *)NULL); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							|  |  |  | err_setstr(exception, string) | 
					
						
							|  |  |  | 	object *exception; | 
					
						
							|  |  |  | 	char *string; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	object *value = newstringobject(string); | 
					
						
							|  |  |  | 	err_setval(exception, value); | 
					
						
							| 
									
										
										
										
											1990-12-20 15:06:42 +00:00
										 |  |  | 	XDECREF(value); | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1994-08-29 12:14:12 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | object * | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | err_occurred() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											1994-08-29 12:14:12 +00:00
										 |  |  | 	return last_exception; | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							|  |  |  | err_get(p_exc, p_val) | 
					
						
							|  |  |  | 	object **p_exc; | 
					
						
							|  |  |  | 	object **p_val; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	*p_exc = last_exception; | 
					
						
							|  |  |  | 	last_exception = NULL; | 
					
						
							|  |  |  | 	*p_val = last_exc_val; | 
					
						
							|  |  |  | 	last_exc_val = NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							|  |  |  | err_clear() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											1994-09-29 09:39:39 +00:00
										 |  |  | 	object *tb; | 
					
						
							| 
									
										
										
										
											1990-12-20 15:06:42 +00:00
										 |  |  | 	XDECREF(last_exception); | 
					
						
							|  |  |  | 	last_exception = NULL; | 
					
						
							|  |  |  | 	XDECREF(last_exc_val); | 
					
						
							|  |  |  | 	last_exc_val = NULL; | 
					
						
							| 
									
										
										
										
											1994-09-29 09:39:39 +00:00
										 |  |  | 	/* Also clear interpreter stack trace */ | 
					
						
							|  |  |  | 	tb = tb_fetch(); | 
					
						
							|  |  |  | 	XDECREF(tb); | 
					
						
							| 
									
										
										
										
											1990-10-14 12:07:46 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											1990-10-14 20:00:05 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Convenience functions to set a type error exception and return 0 */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | err_badarg() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											1990-11-09 15:05:53 +00:00
										 |  |  | 	err_setstr(TypeError, "illegal argument type for built-in operation"); | 
					
						
							| 
									
										
										
										
											1990-10-14 20:00:05 +00:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | object * | 
					
						
							|  |  |  | err_nomem() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											1990-12-20 15:06:42 +00:00
										 |  |  | 	err_set(MemoryError); | 
					
						
							| 
									
										
										
										
											1990-10-14 20:00:05 +00:00
										 |  |  | 	return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | object * | 
					
						
							|  |  |  | err_errno(exc) | 
					
						
							|  |  |  | 	object *exc; | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											1991-10-20 20:14:56 +00:00
										 |  |  | 	object *v; | 
					
						
							| 
									
										
										
										
											1994-08-29 12:14:12 +00:00
										 |  |  | 	int i = errno; | 
					
						
							|  |  |  | 	if (i == EINTR && sigcheck()) | 
					
						
							| 
									
										
										
										
											1991-10-20 20:14:56 +00:00
										 |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											1994-08-29 12:14:12 +00:00
										 |  |  | 	v = mkvalue("(is)", i, strerror(i)); | 
					
						
							| 
									
										
										
										
											1990-10-14 20:00:05 +00:00
										 |  |  | 	if (v != NULL) { | 
					
						
							| 
									
										
										
										
											1993-03-16 12:15:04 +00:00
										 |  |  | 		err_setval(exc, v); | 
					
						
							|  |  |  | 		DECREF(v); | 
					
						
							| 
									
										
										
										
											1990-10-14 20:00:05 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return NULL; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											1990-10-21 22:09:12 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							|  |  |  | err_badcall() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	err_setstr(SystemError, "bad argument to internal function"); | 
					
						
							|  |  |  | } |