Mercurial > cpython-lru_cache-weakref
annotate Python/errors.c @ 90:5cb843bc87fc legacy-trunk
Changed include of <errno.h>
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Thu, 20 Dec 1990 23:05:40 +0000 |
| parents | a6934380c6e7 |
| children | 853b736a6a9e |
| 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 | |
|
90
5cb843bc87fc
Changed include of <errno.h>
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
34 #include "allobjects.h" |
| 6 | 35 |
|
90
5cb843bc87fc
Changed include of <errno.h>
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
36 #include <errno.h> |
|
5cb843bc87fc
Changed include of <errno.h>
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
37 #ifndef errno |
|
5cb843bc87fc
Changed include of <errno.h>
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
38 extern int errno; |
|
5cb843bc87fc
Changed include of <errno.h>
Guido van Rossum <guido@python.org>
parents:
86
diff
changeset
|
39 #endif |
| 86 | 40 |
| 41 #include "errcode.h" | |
| 6 | 42 |
|
59
433f52d1d958
Added prototype for strerror.
Guido van Rossum <guido@python.org>
parents:
37
diff
changeset
|
43 extern char *strerror PROTO((int)); |
|
433f52d1d958
Added prototype for strerror.
Guido van Rossum <guido@python.org>
parents:
37
diff
changeset
|
44 |
| 6 | 45 /* Last exception stored by err_setval() */ |
| 46 | |
| 47 static object *last_exception; | |
| 48 static object *last_exc_val; | |
| 49 | |
| 50 void | |
| 51 err_setval(exception, value) | |
| 52 object *exception; | |
| 53 object *value; | |
| 54 { | |
| 86 | 55 XDECREF(last_exception); |
| 56 XINCREF(exception); | |
| 6 | 57 last_exception = exception; |
| 58 | |
| 86 | 59 XDECREF(last_exc_val); |
| 60 XINCREF(value); | |
| 6 | 61 last_exc_val = value; |
| 62 } | |
| 63 | |
| 64 void | |
| 65 err_set(exception) | |
| 66 object *exception; | |
| 67 { | |
| 68 err_setval(exception, (object *)NULL); | |
| 69 } | |
| 70 | |
| 71 void | |
| 72 err_setstr(exception, string) | |
| 73 object *exception; | |
| 74 char *string; | |
| 75 { | |
| 76 object *value = newstringobject(string); | |
| 77 err_setval(exception, value); | |
| 86 | 78 XDECREF(value); |
| 6 | 79 } |
| 80 | |
| 81 int | |
| 82 err_occurred() | |
| 83 { | |
| 84 return last_exception != NULL; | |
| 85 } | |
| 86 | |
| 87 void | |
| 88 err_get(p_exc, p_val) | |
| 89 object **p_exc; | |
| 90 object **p_val; | |
| 91 { | |
| 92 *p_exc = last_exception; | |
| 93 last_exception = NULL; | |
| 94 *p_val = last_exc_val; | |
| 95 last_exc_val = NULL; | |
| 96 } | |
| 97 | |
| 98 void | |
| 99 err_clear() | |
| 100 { | |
| 86 | 101 XDECREF(last_exception); |
| 102 last_exception = NULL; | |
| 103 XDECREF(last_exc_val); | |
| 104 last_exc_val = NULL; | |
| 6 | 105 } |
|
9
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
106 |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
107 /* 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
|
108 |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
109 int |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
110 err_badarg() |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
111 { |
|
66
46cb14aec10a
Reformulated err_badarg error message.
Guido van Rossum <guido@python.org>
parents:
59
diff
changeset
|
112 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
|
113 return 0; |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
114 } |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
115 |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
116 object * |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
117 err_nomem() |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
118 { |
| 86 | 119 err_set(MemoryError); |
|
9
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
120 return NULL; |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
121 } |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
122 |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
123 object * |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
124 err_errno(exc) |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
125 object *exc; |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
126 { |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
127 object *v = newtupleobject(2); |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
128 if (v != NULL) { |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
129 settupleitem(v, 0, newintobject((long)errno)); |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
130 settupleitem(v, 1, newstringobject(strerror(errno))); |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
131 } |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
132 err_setval(exc, v); |
| 86 | 133 XDECREF(v); |
|
9
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
134 return NULL; |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
135 } |
| 22 | 136 |
| 137 void | |
| 138 err_badcall() | |
| 139 { | |
| 140 err_setstr(SystemError, "bad argument to internal function"); | |
| 141 } | |
| 86 | 142 |
| 143 /* Set the error appropriate to the given input error code (see errcode.h) */ | |
| 144 | |
| 145 void | |
| 146 err_input(err) | |
| 147 int err; | |
| 148 { | |
| 149 switch (err) { | |
| 150 case E_DONE: | |
| 151 case E_OK: | |
| 152 break; | |
| 153 case E_SYNTAX: | |
| 154 err_setstr(RuntimeError, "syntax error"); | |
| 155 break; | |
| 156 case E_TOKEN: | |
| 157 err_setstr(RuntimeError, "illegal token"); | |
| 158 break; | |
| 159 case E_INTR: | |
| 160 err_set(KeyboardInterrupt); | |
| 161 break; | |
| 162 case E_NOMEM: | |
| 163 err_nomem(); | |
| 164 break; | |
| 165 case E_EOF: | |
| 166 err_set(EOFError); | |
| 167 break; | |
| 168 default: | |
| 169 err_setstr(RuntimeError, "unknown input error"); | |
| 170 break; | |
| 171 } | |
| 172 } |
