Mercurial > cpython-lru_cache-weakref
annotate 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 |
| rev | line source |
|---|---|
| 6 | 1 /* Error handling -- see also run.c */ |
| 2 | |
| 3 /* New error handling interface. | |
| 4 | |
| 5 The following problem exists (existed): methods of built-in modules | |
| 6 are called with 'self' and 'args' arguments, but without a context | |
| 7 argument, so they have no way to raise a specific exception. | |
| 8 The same is true for the object implementations: no context argument. | |
| 9 The old convention was to set 'errno' and to return 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 | |
| 12 into a true exception. Problems with this approach are: | |
| 13 - it used standard errno values to indicate Python-specific errors, | |
| 86 | 14 but this means that when such an error code is reported by a system |
| 15 call (e.g., in module posix), the user gets a confusing message | |
| 6 | 16 - errno is a global variable, which makes extensions to a multi- |
| 17 threading environment difficult; e.g., in IRIX, multi-threaded | |
| 86 | 18 programs must use the function oserror() instead of looking in errno |
| 6 | 19 - there is no portable way to add new error numbers for specic |
| 20 situations -- the value space for errno is reserved to the OS, yet | |
| 21 the way to turn module-specific errors into a module-specific | |
| 22 exception requires module-specific values for errno | |
| 23 - there is no way to add a more situation-specific message to an | |
| 24 error. | |
| 25 | |
| 26 The new interface solves all these problems. To return an error, a | |
| 86 | 27 built-in function calls err_set(exception), err_setval(exception, |
| 6 | 28 value) or err_setstr(exception, string), and returns NULL. These |
| 29 functions save the value for later use by puterrno(). To adapt this | |
| 30 scheme to a multi-threaded environment, only the implementation of | |
| 31 err_setval() has to be changed. | |
| 32 */ | |
| 33 | |
| 86 | 34 #include "errno.h" |
| 6 | 35 |
| 86 | 36 #include "allobjects.h" |
| 37 | |
| 38 #include "errcode.h" | |
| 6 | 39 |
|
59
433f52d1d958
Added prototype for strerror.
Guido van Rossum <guido@python.org>
parents:
37
diff
changeset
|
40 extern char *strerror PROTO((int)); |
|
433f52d1d958
Added prototype for strerror.
Guido van Rossum <guido@python.org>
parents:
37
diff
changeset
|
41 |
| 6 | 42 /* Last exception stored by err_setval() */ |
| 43 | |
| 44 static object *last_exception; | |
| 45 static object *last_exc_val; | |
| 46 | |
| 47 void | |
| 48 err_setval(exception, value) | |
| 49 object *exception; | |
| 50 object *value; | |
| 51 { | |
| 86 | 52 XDECREF(last_exception); |
| 53 XINCREF(exception); | |
| 6 | 54 last_exception = exception; |
| 55 | |
| 86 | 56 XDECREF(last_exc_val); |
| 57 XINCREF(value); | |
| 6 | 58 last_exc_val = value; |
| 59 } | |
| 60 | |
| 61 void | |
| 62 err_set(exception) | |
| 63 object *exception; | |
| 64 { | |
| 65 err_setval(exception, (object *)NULL); | |
| 66 } | |
| 67 | |
| 68 void | |
| 69 err_setstr(exception, string) | |
| 70 object *exception; | |
| 71 char *string; | |
| 72 { | |
| 73 object *value = newstringobject(string); | |
| 74 err_setval(exception, value); | |
| 86 | 75 XDECREF(value); |
| 6 | 76 } |
| 77 | |
| 78 int | |
| 79 err_occurred() | |
| 80 { | |
| 81 return last_exception != NULL; | |
| 82 } | |
| 83 | |
| 84 void | |
| 85 err_get(p_exc, p_val) | |
| 86 object **p_exc; | |
| 87 object **p_val; | |
| 88 { | |
| 89 *p_exc = last_exception; | |
| 90 last_exception = NULL; | |
| 91 *p_val = last_exc_val; | |
| 92 last_exc_val = NULL; | |
| 93 } | |
| 94 | |
| 95 void | |
| 96 err_clear() | |
| 97 { | |
| 86 | 98 XDECREF(last_exception); |
| 99 last_exception = NULL; | |
| 100 XDECREF(last_exc_val); | |
| 101 last_exc_val = NULL; | |
| 6 | 102 } |
|
9
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
103 |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
104 /* Convenience functions to set a type error exception and return 0 */ |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
105 |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
106 int |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
107 err_badarg() |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
108 { |
|
66
46cb14aec10a
Reformulated err_badarg error message.
Guido van Rossum <guido@python.org>
parents:
59
diff
changeset
|
109 err_setstr(TypeError, "illegal argument type for built-in operation"); |
|
9
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
110 return 0; |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
111 } |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
112 |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
113 object * |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
114 err_nomem() |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
115 { |
| 86 | 116 err_set(MemoryError); |
|
9
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
117 return NULL; |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
118 } |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
119 |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
120 object * |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
121 err_errno(exc) |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
122 object *exc; |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
123 { |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
124 object *v = newtupleobject(2); |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
125 if (v != NULL) { |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
126 settupleitem(v, 0, newintobject((long)errno)); |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
127 settupleitem(v, 1, newstringobject(strerror(errno))); |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
128 } |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
129 err_setval(exc, v); |
| 86 | 130 XDECREF(v); |
|
9
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
131 return NULL; |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
132 } |
| 22 | 133 |
| 134 void | |
| 135 err_badcall() | |
| 136 { | |
| 137 err_setstr(SystemError, "bad argument to internal function"); | |
| 138 } | |
| 86 | 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 } |
