Mercurial > cpython-lru_cache-weakref
comparison Python/errors.c @ 86:a6934380c6e7 legacy-trunk
"Compiling" version
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Thu, 20 Dec 1990 15:06:42 +0000 |
| parents | 46cb14aec10a |
| children | 5cb843bc87fc |
comparison
equal
deleted
inserted
replaced
| 85:06c92f491f58 | 86:a6934380c6e7 |
|---|---|
| 9 The old convention was to set 'errno' and to return NULL. | 9 The old convention was to set 'errno' and to return NULL. |
| 10 The caller (usually call_function() in eval.c) detects the 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 | 11 return value and then calls puterrno(ctx) to turn the errno value |
| 12 into a true exception. Problems with this approach are: | 12 into a true exception. Problems with this approach are: |
| 13 - it used standard errno values to indicate Python-specific errors, | 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 | 14 but this means that when such an error code is reported by a system |
| 15 user gets a confusing message | 15 call (e.g., in module posix), the user gets a confusing message |
| 16 - errno is a global variable, which makes extensions to a multi- | 16 - errno is a global variable, which makes extensions to a multi- |
| 17 threading environment difficult; e.g., in IRIX, multi-threaded | 17 threading environment difficult; e.g., in IRIX, multi-threaded |
| 18 programs must use the function getoserror() (sp.?) instead of | 18 programs must use the function oserror() instead of looking in errno |
| 19 looking in errno | |
| 20 - there is no portable way to add new error numbers for specic | 19 - 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 | 20 situations -- the value space for errno is reserved to the OS, yet |
| 22 the way to turn module-specific errors into a module-specific | 21 the way to turn module-specific errors into a module-specific |
| 23 exception requires module-specific values for errno | 22 exception requires module-specific values for errno |
| 24 - there is no way to add a more situation-specific message to an | 23 - there is no way to add a more situation-specific message to an |
| 25 error. | 24 error. |
| 26 | 25 |
| 27 The new interface solves all these problems. To return an error, a | 26 The new interface solves all these problems. To return an error, a |
| 28 built-in function calls err_set(exception), err_set(valexception, | 27 built-in function calls err_set(exception), err_setval(exception, |
| 29 value) or err_setstr(exception, string), and returns NULL. These | 28 value) or err_setstr(exception, string), and returns NULL. These |
| 30 functions save the value for later use by puterrno(). To adapt this | 29 functions save the value for later use by puterrno(). To adapt this |
| 31 scheme to a multi-threaded environment, only the implementation of | 30 scheme to a multi-threaded environment, only the implementation of |
| 32 err_setval() has to be changed. | 31 err_setval() has to be changed. |
| 33 */ | 32 */ |
| 34 | 33 |
| 35 #include <stdio.h> | 34 #include "errno.h" |
| 36 | 35 |
| 37 #include "PROTO.h" | 36 #include "allobjects.h" |
| 38 #include "object.h" | 37 |
| 39 #include "intobject.h" | 38 #include "errcode.h" |
| 40 #include "stringobject.h" | |
| 41 #include "tupleobject.h" | |
| 42 #include "errors.h" | |
| 43 | 39 |
| 44 extern char *strerror PROTO((int)); | 40 extern char *strerror PROTO((int)); |
| 45 | 41 |
| 46 /* Last exception stored by err_setval() */ | 42 /* Last exception stored by err_setval() */ |
| 47 | 43 |
| 51 void | 47 void |
| 52 err_setval(exception, value) | 48 err_setval(exception, value) |
| 53 object *exception; | 49 object *exception; |
| 54 object *value; | 50 object *value; |
| 55 { | 51 { |
| 56 if (last_exception != NULL) | 52 XDECREF(last_exception); |
| 57 DECREF(last_exception); | 53 XINCREF(exception); |
| 58 if (exception != NULL) | |
| 59 INCREF(exception); | |
| 60 last_exception = exception; | 54 last_exception = exception; |
| 61 | 55 |
| 62 if (last_exc_val != NULL) | 56 XDECREF(last_exc_val); |
| 63 DECREF(last_exc_val); | 57 XINCREF(value); |
| 64 if (value != NULL) | |
| 65 INCREF(value); | |
| 66 last_exc_val = value; | 58 last_exc_val = value; |
| 67 } | 59 } |
| 68 | 60 |
| 69 void | 61 void |
| 70 err_set(exception) | 62 err_set(exception) |
| 78 object *exception; | 70 object *exception; |
| 79 char *string; | 71 char *string; |
| 80 { | 72 { |
| 81 object *value = newstringobject(string); | 73 object *value = newstringobject(string); |
| 82 err_setval(exception, value); | 74 err_setval(exception, value); |
| 83 if (value != NULL) | 75 XDECREF(value); |
| 84 DECREF(value); | |
| 85 } | 76 } |
| 86 | 77 |
| 87 int | 78 int |
| 88 err_occurred() | 79 err_occurred() |
| 89 { | 80 { |
| 102 } | 93 } |
| 103 | 94 |
| 104 void | 95 void |
| 105 err_clear() | 96 err_clear() |
| 106 { | 97 { |
| 107 if (last_exception != NULL) { | 98 XDECREF(last_exception); |
| 108 DECREF(last_exception); | 99 last_exception = NULL; |
| 109 last_exception = NULL; | 100 XDECREF(last_exc_val); |
| 110 } | 101 last_exc_val = NULL; |
| 111 if (last_exc_val != NULL) { | |
| 112 DECREF(last_exc_val); | |
| 113 last_exc_val = NULL; | |
| 114 } | |
| 115 } | 102 } |
| 116 | 103 |
| 117 /* Convenience functions to set a type error exception and return 0 */ | 104 /* Convenience functions to set a type error exception and return 0 */ |
| 118 | 105 |
| 119 int | 106 int |
| 124 } | 111 } |
| 125 | 112 |
| 126 object * | 113 object * |
| 127 err_nomem() | 114 err_nomem() |
| 128 { | 115 { |
| 129 err_setstr(MemoryError, "in built-in function"); | 116 err_set(MemoryError); |
| 130 return NULL; | 117 return NULL; |
| 131 } | 118 } |
| 132 | 119 |
| 133 object * | 120 object * |
| 134 err_errno(exc) | 121 err_errno(exc) |
| 138 if (v != NULL) { | 125 if (v != NULL) { |
| 139 settupleitem(v, 0, newintobject((long)errno)); | 126 settupleitem(v, 0, newintobject((long)errno)); |
| 140 settupleitem(v, 1, newstringobject(strerror(errno))); | 127 settupleitem(v, 1, newstringobject(strerror(errno))); |
| 141 } | 128 } |
| 142 err_setval(exc, v); | 129 err_setval(exc, v); |
| 143 if (v != NULL) | 130 XDECREF(v); |
| 144 DECREF(v); | |
| 145 return NULL; | 131 return NULL; |
| 146 } | 132 } |
| 147 | 133 |
| 148 void | 134 void |
| 149 err_badcall() | 135 err_badcall() |
| 150 { | 136 { |
| 151 err_setstr(SystemError, "bad argument to internal function"); | 137 err_setstr(SystemError, "bad argument to internal function"); |
| 152 } | 138 } |
| 139 | |
| 140 /* Set the error appropriate to the given input error code (see errcode.h) */ | |
| 141 | |
| 142 void | |
| 143 err_input(err) | |
| 144 int err; | |
| 145 { | |
| 146 switch (err) { | |
| 147 case E_DONE: | |
| 148 case E_OK: | |
| 149 break; | |
| 150 case E_SYNTAX: | |
| 151 err_setstr(RuntimeError, "syntax error"); | |
| 152 break; | |
| 153 case E_TOKEN: | |
| 154 err_setstr(RuntimeError, "illegal token"); | |
| 155 break; | |
| 156 case E_INTR: | |
| 157 err_set(KeyboardInterrupt); | |
| 158 break; | |
| 159 case E_NOMEM: | |
| 160 err_nomem(); | |
| 161 break; | |
| 162 case E_EOF: | |
| 163 err_set(EOFError); | |
| 164 break; | |
| 165 default: | |
| 166 err_setstr(RuntimeError, "unknown input error"); | |
| 167 break; | |
| 168 } | |
| 169 } |
