Mercurial > cpython-lru_cache-weakref
annotate Python/errors.c @ 66:46cb14aec10a legacy-trunk
Reformulated err_badarg error message.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 09 Nov 1990 15:05:53 +0000 |
| parents | 433f52d1d958 |
| children | a6934380c6e7 |
| 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, | |
| 14 but this means that when such an error code is reported by UNIX the | |
| 15 user gets a confusing message | |
| 16 - errno is a global variable, which makes extensions to a multi- | |
| 17 threading environment difficult; e.g., in IRIX, multi-threaded | |
| 18 programs must use the function getoserror() (sp.?) instead of | |
| 19 looking in errno | |
| 20 - 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 | |
| 22 the way to turn module-specific errors into a module-specific | |
| 23 exception requires module-specific values for errno | |
| 24 - there is no way to add a more situation-specific message to an | |
| 25 error. | |
| 26 | |
| 27 The new interface solves all these problems. To return an error, a | |
| 28 built-in function calls err_set(exception), err_set(valexception, | |
| 29 value) or err_setstr(exception, string), and returns NULL. These | |
| 30 functions save the value for later use by puterrno(). To adapt this | |
| 31 scheme to a multi-threaded environment, only the implementation of | |
| 32 err_setval() has to be changed. | |
| 33 */ | |
| 34 | |
| 35 #include <stdio.h> | |
| 36 | |
| 37 #include "PROTO.h" | |
| 38 #include "object.h" | |
|
37
c795fdaced9b
Added intobject.h and tupleobject.h includes.
Guido van Rossum <guido@python.org>
parents:
22
diff
changeset
|
39 #include "intobject.h" |
| 6 | 40 #include "stringobject.h" |
|
37
c795fdaced9b
Added intobject.h and tupleobject.h includes.
Guido van Rossum <guido@python.org>
parents:
22
diff
changeset
|
41 #include "tupleobject.h" |
| 6 | 42 #include "errors.h" |
| 43 | |
|
59
433f52d1d958
Added prototype for strerror.
Guido van Rossum <guido@python.org>
parents:
37
diff
changeset
|
44 extern char *strerror PROTO((int)); |
|
433f52d1d958
Added prototype for strerror.
Guido van Rossum <guido@python.org>
parents:
37
diff
changeset
|
45 |
| 6 | 46 /* Last exception stored by err_setval() */ |
| 47 | |
| 48 static object *last_exception; | |
| 49 static object *last_exc_val; | |
| 50 | |
| 51 void | |
| 52 err_setval(exception, value) | |
| 53 object *exception; | |
| 54 object *value; | |
| 55 { | |
| 56 if (last_exception != NULL) | |
| 57 DECREF(last_exception); | |
| 58 if (exception != NULL) | |
| 59 INCREF(exception); | |
| 60 last_exception = exception; | |
| 61 | |
| 62 if (last_exc_val != NULL) | |
| 63 DECREF(last_exc_val); | |
| 64 if (value != NULL) | |
| 65 INCREF(value); | |
| 66 last_exc_val = value; | |
| 67 } | |
| 68 | |
| 69 void | |
| 70 err_set(exception) | |
| 71 object *exception; | |
| 72 { | |
| 73 err_setval(exception, (object *)NULL); | |
| 74 } | |
| 75 | |
| 76 void | |
| 77 err_setstr(exception, string) | |
| 78 object *exception; | |
| 79 char *string; | |
| 80 { | |
| 81 object *value = newstringobject(string); | |
| 82 err_setval(exception, value); | |
| 83 if (value != NULL) | |
| 84 DECREF(value); | |
| 85 } | |
| 86 | |
| 87 int | |
| 88 err_occurred() | |
| 89 { | |
| 90 return last_exception != NULL; | |
| 91 } | |
| 92 | |
| 93 void | |
| 94 err_get(p_exc, p_val) | |
| 95 object **p_exc; | |
| 96 object **p_val; | |
| 97 { | |
| 98 *p_exc = last_exception; | |
| 99 last_exception = NULL; | |
| 100 *p_val = last_exc_val; | |
| 101 last_exc_val = NULL; | |
| 102 } | |
| 103 | |
| 104 void | |
| 105 err_clear() | |
| 106 { | |
| 107 if (last_exception != NULL) { | |
| 108 DECREF(last_exception); | |
| 109 last_exception = NULL; | |
| 110 } | |
| 111 if (last_exc_val != NULL) { | |
| 112 DECREF(last_exc_val); | |
| 113 last_exc_val = NULL; | |
| 114 } | |
| 115 } | |
|
9
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
116 |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
117 /* 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
|
118 |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
119 int |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
120 err_badarg() |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
121 { |
|
66
46cb14aec10a
Reformulated err_badarg error message.
Guido van Rossum <guido@python.org>
parents:
59
diff
changeset
|
122 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
|
123 return 0; |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
124 } |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
125 |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
126 object * |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
127 err_nomem() |
|
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_setstr(MemoryError, "in built-in function"); |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
130 return NULL; |
|
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 |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
133 object * |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
134 err_errno(exc) |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
135 object *exc; |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
136 { |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
137 object *v = newtupleobject(2); |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
138 if (v != NULL) { |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
139 settupleitem(v, 0, newintobject((long)errno)); |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
140 settupleitem(v, 1, newstringobject(strerror(errno))); |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
141 } |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
142 err_setval(exc, v); |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
143 if (v != NULL) |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
144 DECREF(v); |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
145 return NULL; |
|
33701db4ea55
Adde dconvenience functions.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
146 } |
| 22 | 147 |
| 148 void | |
| 149 err_badcall() | |
| 150 { | |
| 151 err_setstr(SystemError, "bad argument to internal function"); | |
| 152 } |
