Mercurial > cpython-lru_cache-weakref
comparison Python/errors.c @ 61466:cab14be0ada1
Recorded merge of revisions 81029 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r81029 | antoine.pitrou | 2010-05-09 16:46:46 +0200 (dim., 09 mai 2010) | 3 lines
Untabify C files. Will watch buildbots.
........
| author | Antoine Pitrou <solipsis@pitrou.net> |
|---|---|
| date | Sun, 09 May 2010 15:52:27 +0000 |
| parents | 07a47a514548 |
| children | 1698c55b2af9 |
comparison
equal
deleted
inserted
replaced
| 61462:19aac84ac1da | 61466:cab14be0ada1 |
|---|---|
| 22 | 22 |
| 23 | 23 |
| 24 void | 24 void |
| 25 PyErr_Restore(PyObject *type, PyObject *value, PyObject *traceback) | 25 PyErr_Restore(PyObject *type, PyObject *value, PyObject *traceback) |
| 26 { | 26 { |
| 27 PyThreadState *tstate = PyThreadState_GET(); | 27 PyThreadState *tstate = PyThreadState_GET(); |
| 28 PyObject *oldtype, *oldvalue, *oldtraceback; | 28 PyObject *oldtype, *oldvalue, *oldtraceback; |
| 29 | 29 |
| 30 if (traceback != NULL && !PyTraceBack_Check(traceback)) { | 30 if (traceback != NULL && !PyTraceBack_Check(traceback)) { |
| 31 /* XXX Should never happen -- fatal error instead? */ | 31 /* XXX Should never happen -- fatal error instead? */ |
| 32 /* Well, it could be None. */ | 32 /* Well, it could be None. */ |
| 33 Py_DECREF(traceback); | 33 Py_DECREF(traceback); |
| 34 traceback = NULL; | 34 traceback = NULL; |
| 35 } | 35 } |
| 36 | 36 |
| 37 /* Save these in locals to safeguard against recursive | 37 /* Save these in locals to safeguard against recursive |
| 38 invocation through Py_XDECREF */ | 38 invocation through Py_XDECREF */ |
| 39 oldtype = tstate->curexc_type; | 39 oldtype = tstate->curexc_type; |
| 40 oldvalue = tstate->curexc_value; | 40 oldvalue = tstate->curexc_value; |
| 41 oldtraceback = tstate->curexc_traceback; | 41 oldtraceback = tstate->curexc_traceback; |
| 42 | 42 |
| 43 tstate->curexc_type = type; | 43 tstate->curexc_type = type; |
| 44 tstate->curexc_value = value; | 44 tstate->curexc_value = value; |
| 45 tstate->curexc_traceback = traceback; | 45 tstate->curexc_traceback = traceback; |
| 46 | 46 |
| 47 Py_XDECREF(oldtype); | 47 Py_XDECREF(oldtype); |
| 48 Py_XDECREF(oldvalue); | 48 Py_XDECREF(oldvalue); |
| 49 Py_XDECREF(oldtraceback); | 49 Py_XDECREF(oldtraceback); |
| 50 } | 50 } |
| 51 | 51 |
| 52 void | 52 void |
| 53 PyErr_SetObject(PyObject *exception, PyObject *value) | 53 PyErr_SetObject(PyObject *exception, PyObject *value) |
| 54 { | 54 { |
| 55 PyThreadState *tstate = PyThreadState_GET(); | 55 PyThreadState *tstate = PyThreadState_GET(); |
| 56 PyObject *exc_value; | 56 PyObject *exc_value; |
| 57 PyObject *tb = NULL; | 57 PyObject *tb = NULL; |
| 58 | 58 |
| 59 if (exception != NULL && | 59 if (exception != NULL && |
| 60 !PyExceptionClass_Check(exception)) { | 60 !PyExceptionClass_Check(exception)) { |
| 61 PyErr_Format(PyExc_SystemError, | 61 PyErr_Format(PyExc_SystemError, |
| 62 "exception %R not a BaseException subclass", | 62 "exception %R not a BaseException subclass", |
| 63 exception); | 63 exception); |
| 64 return; | 64 return; |
| 65 } | 65 } |
| 66 Py_XINCREF(value); | 66 Py_XINCREF(value); |
| 67 exc_value = tstate->exc_value; | 67 exc_value = tstate->exc_value; |
| 68 if (exc_value != NULL && exc_value != Py_None) { | 68 if (exc_value != NULL && exc_value != Py_None) { |
| 69 /* Implicit exception chaining */ | 69 /* Implicit exception chaining */ |
| 70 Py_INCREF(exc_value); | 70 Py_INCREF(exc_value); |
| 71 if (value == NULL || !PyExceptionInstance_Check(value)) { | 71 if (value == NULL || !PyExceptionInstance_Check(value)) { |
| 72 /* We must normalize the value right now */ | 72 /* We must normalize the value right now */ |
| 73 PyObject *args, *fixed_value; | 73 PyObject *args, *fixed_value; |
| 74 if (value == NULL || value == Py_None) | 74 if (value == NULL || value == Py_None) |
| 75 args = PyTuple_New(0); | 75 args = PyTuple_New(0); |
| 76 else if (PyTuple_Check(value)) { | 76 else if (PyTuple_Check(value)) { |
| 77 Py_INCREF(value); | 77 Py_INCREF(value); |
| 78 args = value; | 78 args = value; |
| 79 } | 79 } |
| 80 else | 80 else |
| 81 args = PyTuple_Pack(1, value); | 81 args = PyTuple_Pack(1, value); |
| 82 fixed_value = args ? | 82 fixed_value = args ? |
| 83 PyEval_CallObject(exception, args) : NULL; | 83 PyEval_CallObject(exception, args) : NULL; |
| 84 Py_XDECREF(args); | 84 Py_XDECREF(args); |
| 85 Py_XDECREF(value); | 85 Py_XDECREF(value); |
| 86 if (fixed_value == NULL) | 86 if (fixed_value == NULL) |
| 87 return; | 87 return; |
| 88 value = fixed_value; | 88 value = fixed_value; |
| 89 } | 89 } |
| 90 /* Avoid reference cycles through the context chain. | 90 /* Avoid reference cycles through the context chain. |
| 91 This is O(chain length) but context chains are | 91 This is O(chain length) but context chains are |
| 92 usually very short. Sensitive readers may try | 92 usually very short. Sensitive readers may try |
| 93 to inline the call to PyException_GetContext. */ | 93 to inline the call to PyException_GetContext. */ |
| 94 if (exc_value != value) { | 94 if (exc_value != value) { |
| 95 PyObject *o = exc_value, *context; | 95 PyObject *o = exc_value, *context; |
| 96 while ((context = PyException_GetContext(o))) { | 96 while ((context = PyException_GetContext(o))) { |
| 97 Py_DECREF(context); | 97 Py_DECREF(context); |
| 98 if (context == value) { | 98 if (context == value) { |
| 99 PyException_SetContext(o, NULL); | 99 PyException_SetContext(o, NULL); |
| 100 break; | 100 break; |
| 101 } | 101 } |
| 102 o = context; | 102 o = context; |
| 103 } | 103 } |
| 104 PyException_SetContext(value, exc_value); | 104 PyException_SetContext(value, exc_value); |
| 105 } else { | 105 } else { |
| 106 Py_DECREF(exc_value); | 106 Py_DECREF(exc_value); |
| 107 } | 107 } |
| 108 } | 108 } |
| 109 if (value != NULL && PyExceptionInstance_Check(value)) | 109 if (value != NULL && PyExceptionInstance_Check(value)) |
| 110 tb = PyException_GetTraceback(value); | 110 tb = PyException_GetTraceback(value); |
| 111 Py_XINCREF(exception); | 111 Py_XINCREF(exception); |
| 112 PyErr_Restore(exception, value, tb); | 112 PyErr_Restore(exception, value, tb); |
| 113 } | 113 } |
| 114 | 114 |
| 115 void | 115 void |
| 116 PyErr_SetNone(PyObject *exception) | 116 PyErr_SetNone(PyObject *exception) |
| 117 { | 117 { |
| 118 PyErr_SetObject(exception, (PyObject *)NULL); | 118 PyErr_SetObject(exception, (PyObject *)NULL); |
| 119 } | 119 } |
| 120 | 120 |
| 121 void | 121 void |
| 122 PyErr_SetString(PyObject *exception, const char *string) | 122 PyErr_SetString(PyObject *exception, const char *string) |
| 123 { | 123 { |
| 124 PyObject *value = PyUnicode_FromString(string); | 124 PyObject *value = PyUnicode_FromString(string); |
| 125 PyErr_SetObject(exception, value); | 125 PyErr_SetObject(exception, value); |
| 126 Py_XDECREF(value); | 126 Py_XDECREF(value); |
| 127 } | 127 } |
| 128 | 128 |
| 129 | 129 |
| 130 PyObject * | 130 PyObject * |
| 131 PyErr_Occurred(void) | 131 PyErr_Occurred(void) |
| 132 { | 132 { |
| 133 PyThreadState *tstate = PyThreadState_GET(); | 133 PyThreadState *tstate = PyThreadState_GET(); |
| 134 | 134 |
| 135 return tstate->curexc_type; | 135 return tstate->curexc_type; |
| 136 } | 136 } |
| 137 | 137 |
| 138 | 138 |
| 139 int | 139 int |
| 140 PyErr_GivenExceptionMatches(PyObject *err, PyObject *exc) | 140 PyErr_GivenExceptionMatches(PyObject *err, PyObject *exc) |
| 141 { | 141 { |
| 142 if (err == NULL || exc == NULL) { | 142 if (err == NULL || exc == NULL) { |
| 143 /* maybe caused by "import exceptions" that failed early on */ | 143 /* maybe caused by "import exceptions" that failed early on */ |
| 144 return 0; | 144 return 0; |
| 145 } | 145 } |
| 146 if (PyTuple_Check(exc)) { | 146 if (PyTuple_Check(exc)) { |
| 147 Py_ssize_t i, n; | 147 Py_ssize_t i, n; |
| 148 n = PyTuple_Size(exc); | 148 n = PyTuple_Size(exc); |
| 149 for (i = 0; i < n; i++) { | 149 for (i = 0; i < n; i++) { |
| 150 /* Test recursively */ | 150 /* Test recursively */ |
| 151 if (PyErr_GivenExceptionMatches( | 151 if (PyErr_GivenExceptionMatches( |
| 152 err, PyTuple_GET_ITEM(exc, i))) | 152 err, PyTuple_GET_ITEM(exc, i))) |
| 153 { | 153 { |
| 154 return 1; | 154 return 1; |
| 155 } | 155 } |
| 156 } | 156 } |
| 157 return 0; | 157 return 0; |
| 158 } | 158 } |
| 159 /* err might be an instance, so check its class. */ | 159 /* err might be an instance, so check its class. */ |
| 160 if (PyExceptionInstance_Check(err)) | 160 if (PyExceptionInstance_Check(err)) |
| 161 err = PyExceptionInstance_Class(err); | 161 err = PyExceptionInstance_Class(err); |
| 162 | 162 |
| 163 if (PyExceptionClass_Check(err) && PyExceptionClass_Check(exc)) { | 163 if (PyExceptionClass_Check(err) && PyExceptionClass_Check(exc)) { |
| 164 int res = 0; | 164 int res = 0; |
| 165 PyObject *exception, *value, *tb; | 165 PyObject *exception, *value, *tb; |
| 166 PyErr_Fetch(&exception, &value, &tb); | 166 PyErr_Fetch(&exception, &value, &tb); |
| 167 /* PyObject_IsSubclass() can recurse and therefore is | 167 /* PyObject_IsSubclass() can recurse and therefore is |
| 168 not safe (see test_bad_getattr in test.pickletester). */ | 168 not safe (see test_bad_getattr in test.pickletester). */ |
| 169 res = PyType_IsSubtype((PyTypeObject *)err, (PyTypeObject *)exc); | 169 res = PyType_IsSubtype((PyTypeObject *)err, (PyTypeObject *)exc); |
| 170 /* This function must not fail, so print the error here */ | 170 /* This function must not fail, so print the error here */ |
| 171 if (res == -1) { | 171 if (res == -1) { |
| 172 PyErr_WriteUnraisable(err); | 172 PyErr_WriteUnraisable(err); |
| 173 res = 0; | 173 res = 0; |
| 174 } | 174 } |
| 175 PyErr_Restore(exception, value, tb); | 175 PyErr_Restore(exception, value, tb); |
| 176 return res; | 176 return res; |
| 177 } | 177 } |
| 178 | 178 |
| 179 return err == exc; | 179 return err == exc; |
| 180 } | 180 } |
| 181 | 181 |
| 182 | 182 |
| 183 int | 183 int |
| 184 PyErr_ExceptionMatches(PyObject *exc) | 184 PyErr_ExceptionMatches(PyObject *exc) |
| 185 { | 185 { |
| 186 return PyErr_GivenExceptionMatches(PyErr_Occurred(), exc); | 186 return PyErr_GivenExceptionMatches(PyErr_Occurred(), exc); |
| 187 } | 187 } |
| 188 | 188 |
| 189 | 189 |
| 190 /* Used in many places to normalize a raised exception, including in | 190 /* Used in many places to normalize a raised exception, including in |
| 191 eval_code2(), do_raise(), and PyErr_Print() | 191 eval_code2(), do_raise(), and PyErr_Print() |
| 192 | 192 |
| 193 XXX: should PyErr_NormalizeException() also call | 193 XXX: should PyErr_NormalizeException() also call |
| 194 PyException_SetTraceback() with the resulting value and tb? | 194 PyException_SetTraceback() with the resulting value and tb? |
| 195 */ | 195 */ |
| 196 void | 196 void |
| 197 PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb) | 197 PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb) |
| 198 { | 198 { |
| 199 PyObject *type = *exc; | 199 PyObject *type = *exc; |
| 200 PyObject *value = *val; | 200 PyObject *value = *val; |
| 201 PyObject *inclass = NULL; | 201 PyObject *inclass = NULL; |
| 202 PyObject *initial_tb = NULL; | 202 PyObject *initial_tb = NULL; |
| 203 PyThreadState *tstate = NULL; | 203 PyThreadState *tstate = NULL; |
| 204 | 204 |
| 205 if (type == NULL) { | 205 if (type == NULL) { |
| 206 /* There was no exception, so nothing to do. */ | 206 /* There was no exception, so nothing to do. */ |
| 207 return; | 207 return; |
| 208 } | 208 } |
| 209 | 209 |
| 210 /* If PyErr_SetNone() was used, the value will have been actually | 210 /* If PyErr_SetNone() was used, the value will have been actually |
| 211 set to NULL. | 211 set to NULL. |
| 212 */ | 212 */ |
| 213 if (!value) { | 213 if (!value) { |
| 214 value = Py_None; | 214 value = Py_None; |
| 215 Py_INCREF(value); | 215 Py_INCREF(value); |
| 216 } | 216 } |
| 217 | 217 |
| 218 if (PyExceptionInstance_Check(value)) | 218 if (PyExceptionInstance_Check(value)) |
| 219 inclass = PyExceptionInstance_Class(value); | 219 inclass = PyExceptionInstance_Class(value); |
| 220 | 220 |
| 221 /* Normalize the exception so that if the type is a class, the | 221 /* Normalize the exception so that if the type is a class, the |
| 222 value will be an instance. | 222 value will be an instance. |
| 223 */ | 223 */ |
| 224 if (PyExceptionClass_Check(type)) { | 224 if (PyExceptionClass_Check(type)) { |
| 225 /* if the value was not an instance, or is not an instance | 225 /* if the value was not an instance, or is not an instance |
| 226 whose class is (or is derived from) type, then use the | 226 whose class is (or is derived from) type, then use the |
| 227 value as an argument to instantiation of the type | 227 value as an argument to instantiation of the type |
| 228 class. | 228 class. |
| 229 */ | 229 */ |
| 230 if (!inclass || !PyObject_IsSubclass(inclass, type)) { | 230 if (!inclass || !PyObject_IsSubclass(inclass, type)) { |
| 231 PyObject *args, *res; | 231 PyObject *args, *res; |
| 232 | 232 |
| 233 if (value == Py_None) | 233 if (value == Py_None) |
| 234 args = PyTuple_New(0); | 234 args = PyTuple_New(0); |
| 235 else if (PyTuple_Check(value)) { | 235 else if (PyTuple_Check(value)) { |
| 236 Py_INCREF(value); | 236 Py_INCREF(value); |
| 237 args = value; | 237 args = value; |
| 238 } | 238 } |
| 239 else | 239 else |
| 240 args = PyTuple_Pack(1, value); | 240 args = PyTuple_Pack(1, value); |
| 241 | 241 |
| 242 if (args == NULL) | 242 if (args == NULL) |
| 243 goto finally; | 243 goto finally; |
| 244 res = PyEval_CallObject(type, args); | 244 res = PyEval_CallObject(type, args); |
| 245 Py_DECREF(args); | 245 Py_DECREF(args); |
| 246 if (res == NULL) | 246 if (res == NULL) |
| 247 goto finally; | 247 goto finally; |
| 248 Py_DECREF(value); | 248 Py_DECREF(value); |
| 249 value = res; | 249 value = res; |
| 250 } | 250 } |
| 251 /* if the class of the instance doesn't exactly match the | 251 /* if the class of the instance doesn't exactly match the |
| 252 class of the type, believe the instance | 252 class of the type, believe the instance |
| 253 */ | 253 */ |
| 254 else if (inclass != type) { | 254 else if (inclass != type) { |
| 255 Py_DECREF(type); | 255 Py_DECREF(type); |
| 256 type = inclass; | 256 type = inclass; |
| 257 Py_INCREF(type); | 257 Py_INCREF(type); |
| 258 } | 258 } |
| 259 } | 259 } |
| 260 *exc = type; | 260 *exc = type; |
| 261 *val = value; | 261 *val = value; |
| 262 return; | 262 return; |
| 263 finally: | 263 finally: |
| 264 Py_DECREF(type); | 264 Py_DECREF(type); |
| 265 Py_DECREF(value); | 265 Py_DECREF(value); |
| 266 /* If the new exception doesn't set a traceback and the old | 266 /* If the new exception doesn't set a traceback and the old |
| 267 exception had a traceback, use the old traceback for the | 267 exception had a traceback, use the old traceback for the |
| 268 new exception. It's better than nothing. | 268 new exception. It's better than nothing. |
| 269 */ | 269 */ |
| 270 initial_tb = *tb; | 270 initial_tb = *tb; |
| 271 PyErr_Fetch(exc, val, tb); | 271 PyErr_Fetch(exc, val, tb); |
| 272 if (initial_tb != NULL) { | 272 if (initial_tb != NULL) { |
| 273 if (*tb == NULL) | 273 if (*tb == NULL) |
| 274 *tb = initial_tb; | 274 *tb = initial_tb; |
| 275 else | 275 else |
| 276 Py_DECREF(initial_tb); | 276 Py_DECREF(initial_tb); |
| 277 } | 277 } |
| 278 /* normalize recursively */ | 278 /* normalize recursively */ |
| 279 tstate = PyThreadState_GET(); | 279 tstate = PyThreadState_GET(); |
| 280 if (++tstate->recursion_depth > Py_GetRecursionLimit()) { | 280 if (++tstate->recursion_depth > Py_GetRecursionLimit()) { |
| 281 --tstate->recursion_depth; | 281 --tstate->recursion_depth; |
| 282 /* throw away the old exception... */ | 282 /* throw away the old exception... */ |
| 283 Py_DECREF(*exc); | 283 Py_DECREF(*exc); |
| 284 Py_DECREF(*val); | 284 Py_DECREF(*val); |
| 285 /* ... and use the recursion error instead */ | 285 /* ... and use the recursion error instead */ |
| 286 *exc = PyExc_RuntimeError; | 286 *exc = PyExc_RuntimeError; |
| 287 *val = PyExc_RecursionErrorInst; | 287 *val = PyExc_RecursionErrorInst; |
| 288 Py_INCREF(*exc); | 288 Py_INCREF(*exc); |
| 289 Py_INCREF(*val); | 289 Py_INCREF(*val); |
| 290 /* just keeping the old traceback */ | 290 /* just keeping the old traceback */ |
| 291 return; | 291 return; |
| 292 } | 292 } |
| 293 PyErr_NormalizeException(exc, val, tb); | 293 PyErr_NormalizeException(exc, val, tb); |
| 294 --tstate->recursion_depth; | 294 --tstate->recursion_depth; |
| 295 } | 295 } |
| 296 | 296 |
| 297 | 297 |
| 298 void | 298 void |
| 299 PyErr_Fetch(PyObject **p_type, PyObject **p_value, PyObject **p_traceback) | 299 PyErr_Fetch(PyObject **p_type, PyObject **p_value, PyObject **p_traceback) |
| 300 { | 300 { |
| 301 PyThreadState *tstate = PyThreadState_GET(); | 301 PyThreadState *tstate = PyThreadState_GET(); |
| 302 | 302 |
| 303 *p_type = tstate->curexc_type; | 303 *p_type = tstate->curexc_type; |
| 304 *p_value = tstate->curexc_value; | 304 *p_value = tstate->curexc_value; |
| 305 *p_traceback = tstate->curexc_traceback; | 305 *p_traceback = tstate->curexc_traceback; |
| 306 | 306 |
| 307 tstate->curexc_type = NULL; | 307 tstate->curexc_type = NULL; |
| 308 tstate->curexc_value = NULL; | 308 tstate->curexc_value = NULL; |
| 309 tstate->curexc_traceback = NULL; | 309 tstate->curexc_traceback = NULL; |
| 310 } | 310 } |
| 311 | 311 |
| 312 void | 312 void |
| 313 PyErr_Clear(void) | 313 PyErr_Clear(void) |
| 314 { | 314 { |
| 315 PyErr_Restore(NULL, NULL, NULL); | 315 PyErr_Restore(NULL, NULL, NULL); |
| 316 } | 316 } |
| 317 | 317 |
| 318 /* Convenience functions to set a type error exception and return 0 */ | 318 /* Convenience functions to set a type error exception and return 0 */ |
| 319 | 319 |
| 320 int | 320 int |
| 321 PyErr_BadArgument(void) | 321 PyErr_BadArgument(void) |
| 322 { | 322 { |
| 323 PyErr_SetString(PyExc_TypeError, | 323 PyErr_SetString(PyExc_TypeError, |
| 324 "bad argument type for built-in operation"); | 324 "bad argument type for built-in operation"); |
| 325 return 0; | 325 return 0; |
| 326 } | 326 } |
| 327 | 327 |
| 328 PyObject * | 328 PyObject * |
| 329 PyErr_NoMemory(void) | 329 PyErr_NoMemory(void) |
| 330 { | 330 { |
| 331 if (PyErr_ExceptionMatches(PyExc_MemoryError)) | 331 if (PyErr_ExceptionMatches(PyExc_MemoryError)) |
| 332 /* already current */ | 332 /* already current */ |
| 333 return NULL; | 333 return NULL; |
| 334 | 334 |
| 335 /* raise the pre-allocated instance if it still exists */ | 335 /* raise the pre-allocated instance if it still exists */ |
| 336 if (PyExc_MemoryErrorInst) | 336 if (PyExc_MemoryErrorInst) |
| 337 { | 337 { |
| 338 /* Clear the previous traceback, otherwise it will be appended | 338 /* Clear the previous traceback, otherwise it will be appended |
| 339 * to the current one. | 339 * to the current one. |
| 340 * | 340 * |
| 341 * The following statement is not likely to raise any error; | 341 * The following statement is not likely to raise any error; |
| 342 * if it does, we simply discard it. | 342 * if it does, we simply discard it. |
| 343 */ | 343 */ |
| 344 PyException_SetTraceback(PyExc_MemoryErrorInst, Py_None); | 344 PyException_SetTraceback(PyExc_MemoryErrorInst, Py_None); |
| 345 | 345 |
| 346 PyErr_SetObject(PyExc_MemoryError, PyExc_MemoryErrorInst); | 346 PyErr_SetObject(PyExc_MemoryError, PyExc_MemoryErrorInst); |
| 347 } | 347 } |
| 348 else | 348 else |
| 349 /* this will probably fail since there's no memory and hee, | 349 /* this will probably fail since there's no memory and hee, |
| 350 hee, we have to instantiate this class | 350 hee, we have to instantiate this class |
| 351 */ | 351 */ |
| 352 PyErr_SetNone(PyExc_MemoryError); | 352 PyErr_SetNone(PyExc_MemoryError); |
| 353 | 353 |
| 354 return NULL; | 354 return NULL; |
| 355 } | 355 } |
| 356 | 356 |
| 357 PyObject * | 357 PyObject * |
| 358 PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject) | 358 PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject) |
| 359 { | 359 { |
| 360 PyObject *message; | 360 PyObject *message; |
| 361 PyObject *v; | 361 PyObject *v; |
| 362 int i = errno; | 362 int i = errno; |
| 363 #ifndef MS_WINDOWS | 363 #ifndef MS_WINDOWS |
| 364 char *s; | 364 char *s; |
| 365 #else | 365 #else |
| 366 WCHAR *s_buf = NULL; | 366 WCHAR *s_buf = NULL; |
| 367 #endif /* Unix/Windows */ | 367 #endif /* Unix/Windows */ |
| 368 | 368 |
| 369 #ifdef EINTR | 369 #ifdef EINTR |
| 370 if (i == EINTR && PyErr_CheckSignals()) | 370 if (i == EINTR && PyErr_CheckSignals()) |
| 371 return NULL; | 371 return NULL; |
| 372 #endif | 372 #endif |
| 373 | 373 |
| 374 #ifndef MS_WINDOWS | 374 #ifndef MS_WINDOWS |
| 375 if (i == 0) | 375 if (i == 0) |
| 376 s = "Error"; /* Sometimes errno didn't get set */ | 376 s = "Error"; /* Sometimes errno didn't get set */ |
| 377 else | 377 else |
| 378 s = strerror(i); | 378 s = strerror(i); |
| 379 message = PyUnicode_DecodeUTF8(s, strlen(s), "ignore"); | 379 message = PyUnicode_DecodeUTF8(s, strlen(s), "ignore"); |
| 380 #else | 380 #else |
| 381 if (i == 0) | 381 if (i == 0) |
| 382 message = PyUnicode_FromString("Error"); /* Sometimes errno didn't get set */ | 382 message = PyUnicode_FromString("Error"); /* Sometimes errno didn't get set */ |
| 383 else | 383 else |
| 384 { | 384 { |
| 385 /* Note that the Win32 errors do not lineup with the | 385 /* Note that the Win32 errors do not lineup with the |
| 386 errno error. So if the error is in the MSVC error | 386 errno error. So if the error is in the MSVC error |
| 387 table, we use it, otherwise we assume it really _is_ | 387 table, we use it, otherwise we assume it really _is_ |
| 388 a Win32 error code | 388 a Win32 error code |
| 389 */ | 389 */ |
| 390 if (i > 0 && i < _sys_nerr) { | 390 if (i > 0 && i < _sys_nerr) { |
| 391 message = PyUnicode_FromString(_sys_errlist[i]); | 391 message = PyUnicode_FromString(_sys_errlist[i]); |
| 392 } | 392 } |
| 393 else { | 393 else { |
| 394 int len = FormatMessageW( | 394 int len = FormatMessageW( |
| 395 FORMAT_MESSAGE_ALLOCATE_BUFFER | | 395 FORMAT_MESSAGE_ALLOCATE_BUFFER | |
| 396 FORMAT_MESSAGE_FROM_SYSTEM | | 396 FORMAT_MESSAGE_FROM_SYSTEM | |
| 397 FORMAT_MESSAGE_IGNORE_INSERTS, | 397 FORMAT_MESSAGE_IGNORE_INSERTS, |
| 398 NULL, /* no message source */ | 398 NULL, /* no message source */ |
| 399 i, | 399 i, |
| 400 MAKELANGID(LANG_NEUTRAL, | 400 MAKELANGID(LANG_NEUTRAL, |
| 401 SUBLANG_DEFAULT), | 401 SUBLANG_DEFAULT), |
| 402 /* Default language */ | 402 /* Default language */ |
| 403 (LPWSTR) &s_buf, | 403 (LPWSTR) &s_buf, |
| 404 0, /* size not used */ | 404 0, /* size not used */ |
| 405 NULL); /* no args */ | 405 NULL); /* no args */ |
| 406 if (len==0) { | 406 if (len==0) { |
| 407 /* Only ever seen this in out-of-mem | 407 /* Only ever seen this in out-of-mem |
| 408 situations */ | 408 situations */ |
| 409 s_buf = NULL; | 409 s_buf = NULL; |
| 410 message = PyUnicode_FromFormat("Windows Error 0x%X", i); | 410 message = PyUnicode_FromFormat("Windows Error 0x%X", i); |
| 411 } else { | 411 } else { |
| 412 /* remove trailing cr/lf and dots */ | 412 /* remove trailing cr/lf and dots */ |
| 413 while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.')) | 413 while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.')) |
| 414 s_buf[--len] = L'\0'; | 414 s_buf[--len] = L'\0'; |
| 415 message = PyUnicode_FromUnicode(s_buf, len); | 415 message = PyUnicode_FromUnicode(s_buf, len); |
| 416 } | 416 } |
| 417 } | 417 } |
| 418 } | 418 } |
| 419 #endif /* Unix/Windows */ | 419 #endif /* Unix/Windows */ |
| 420 | 420 |
| 421 if (message == NULL) | 421 if (message == NULL) |
| 422 { | 422 { |
| 423 #ifdef MS_WINDOWS | 423 #ifdef MS_WINDOWS |
| 424 LocalFree(s_buf); | 424 LocalFree(s_buf); |
| 425 #endif | 425 #endif |
| 426 return NULL; | 426 return NULL; |
| 427 } | 427 } |
| 428 | 428 |
| 429 if (filenameObject != NULL) | 429 if (filenameObject != NULL) |
| 430 v = Py_BuildValue("(iOO)", i, message, filenameObject); | 430 v = Py_BuildValue("(iOO)", i, message, filenameObject); |
| 431 else | 431 else |
| 432 v = Py_BuildValue("(iO)", i, message); | 432 v = Py_BuildValue("(iO)", i, message); |
| 433 Py_DECREF(message); | 433 Py_DECREF(message); |
| 434 | 434 |
| 435 if (v != NULL) { | 435 if (v != NULL) { |
| 436 PyErr_SetObject(exc, v); | 436 PyErr_SetObject(exc, v); |
| 437 Py_DECREF(v); | 437 Py_DECREF(v); |
| 438 } | 438 } |
| 439 #ifdef MS_WINDOWS | 439 #ifdef MS_WINDOWS |
| 440 LocalFree(s_buf); | 440 LocalFree(s_buf); |
| 441 #endif | 441 #endif |
| 442 return NULL; | 442 return NULL; |
| 443 } | 443 } |
| 444 | 444 |
| 445 | 445 |
| 446 PyObject * | 446 PyObject * |
| 447 PyErr_SetFromErrnoWithFilename(PyObject *exc, const char *filename) | 447 PyErr_SetFromErrnoWithFilename(PyObject *exc, const char *filename) |
| 448 { | 448 { |
| 449 PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL; | 449 PyObject *name = filename ? PyUnicode_DecodeFSDefault(filename) : NULL; |
| 450 PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name); | 450 PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name); |
| 451 Py_XDECREF(name); | 451 Py_XDECREF(name); |
| 452 return result; | 452 return result; |
| 453 } | 453 } |
| 454 | 454 |
| 455 #ifdef MS_WINDOWS | 455 #ifdef MS_WINDOWS |
| 456 PyObject * | 456 PyObject * |
| 457 PyErr_SetFromErrnoWithUnicodeFilename(PyObject *exc, const Py_UNICODE *filename) | 457 PyErr_SetFromErrnoWithUnicodeFilename(PyObject *exc, const Py_UNICODE *filename) |
| 458 { | 458 { |
| 459 PyObject *name = filename ? | 459 PyObject *name = filename ? |
| 460 PyUnicode_FromUnicode(filename, wcslen(filename)) : | 460 PyUnicode_FromUnicode(filename, wcslen(filename)) : |
| 461 NULL; | 461 NULL; |
| 462 PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name); | 462 PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name); |
| 463 Py_XDECREF(name); | 463 Py_XDECREF(name); |
| 464 return result; | 464 return result; |
| 465 } | 465 } |
| 466 #endif /* MS_WINDOWS */ | 466 #endif /* MS_WINDOWS */ |
| 467 | 467 |
| 468 PyObject * | 468 PyObject * |
| 469 PyErr_SetFromErrno(PyObject *exc) | 469 PyErr_SetFromErrno(PyObject *exc) |
| 470 { | 470 { |
| 471 return PyErr_SetFromErrnoWithFilenameObject(exc, NULL); | 471 return PyErr_SetFromErrnoWithFilenameObject(exc, NULL); |
| 472 } | 472 } |
| 473 | 473 |
| 474 #ifdef MS_WINDOWS | 474 #ifdef MS_WINDOWS |
| 475 /* Windows specific error code handling */ | 475 /* Windows specific error code handling */ |
| 476 PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject( | 476 PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject( |
| 477 PyObject *exc, | 477 PyObject *exc, |
| 478 int ierr, | 478 int ierr, |
| 479 PyObject *filenameObject) | 479 PyObject *filenameObject) |
| 480 { | 480 { |
| 481 int len; | 481 int len; |
| 482 WCHAR *s_buf = NULL; /* Free via LocalFree */ | 482 WCHAR *s_buf = NULL; /* Free via LocalFree */ |
| 483 PyObject *message; | 483 PyObject *message; |
| 484 PyObject *v; | 484 PyObject *v; |
| 485 DWORD err = (DWORD)ierr; | 485 DWORD err = (DWORD)ierr; |
| 486 if (err==0) err = GetLastError(); | 486 if (err==0) err = GetLastError(); |
| 487 len = FormatMessageW( | 487 len = FormatMessageW( |
| 488 /* Error API error */ | 488 /* Error API error */ |
| 489 FORMAT_MESSAGE_ALLOCATE_BUFFER | | 489 FORMAT_MESSAGE_ALLOCATE_BUFFER | |
| 490 FORMAT_MESSAGE_FROM_SYSTEM | | 490 FORMAT_MESSAGE_FROM_SYSTEM | |
| 491 FORMAT_MESSAGE_IGNORE_INSERTS, | 491 FORMAT_MESSAGE_IGNORE_INSERTS, |
| 492 NULL, /* no message source */ | 492 NULL, /* no message source */ |
| 493 err, | 493 err, |
| 494 MAKELANGID(LANG_NEUTRAL, | 494 MAKELANGID(LANG_NEUTRAL, |
| 495 SUBLANG_DEFAULT), /* Default language */ | 495 SUBLANG_DEFAULT), /* Default language */ |
| 496 (LPWSTR) &s_buf, | 496 (LPWSTR) &s_buf, |
| 497 0, /* size not used */ | 497 0, /* size not used */ |
| 498 NULL); /* no args */ | 498 NULL); /* no args */ |
| 499 if (len==0) { | 499 if (len==0) { |
| 500 /* Only seen this in out of mem situations */ | 500 /* Only seen this in out of mem situations */ |
| 501 message = PyUnicode_FromFormat("Windows Error 0x%X", err); | 501 message = PyUnicode_FromFormat("Windows Error 0x%X", err); |
| 502 s_buf = NULL; | 502 s_buf = NULL; |
| 503 } else { | 503 } else { |
| 504 /* remove trailing cr/lf and dots */ | 504 /* remove trailing cr/lf and dots */ |
| 505 while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.')) | 505 while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.')) |
| 506 s_buf[--len] = L'\0'; | 506 s_buf[--len] = L'\0'; |
| 507 message = PyUnicode_FromUnicode(s_buf, len); | 507 message = PyUnicode_FromUnicode(s_buf, len); |
| 508 } | 508 } |
| 509 | 509 |
| 510 if (message == NULL) | 510 if (message == NULL) |
| 511 { | 511 { |
| 512 LocalFree(s_buf); | 512 LocalFree(s_buf); |
| 513 return NULL; | 513 return NULL; |
| 514 } | 514 } |
| 515 | 515 |
| 516 if (filenameObject != NULL) | 516 if (filenameObject != NULL) |
| 517 v = Py_BuildValue("(iOO)", err, message, filenameObject); | 517 v = Py_BuildValue("(iOO)", err, message, filenameObject); |
| 518 else | 518 else |
| 519 v = Py_BuildValue("(iO)", err, message); | 519 v = Py_BuildValue("(iO)", err, message); |
| 520 Py_DECREF(message); | 520 Py_DECREF(message); |
| 521 | 521 |
| 522 if (v != NULL) { | 522 if (v != NULL) { |
| 523 PyErr_SetObject(exc, v); | 523 PyErr_SetObject(exc, v); |
| 524 Py_DECREF(v); | 524 Py_DECREF(v); |
| 525 } | 525 } |
| 526 LocalFree(s_buf); | 526 LocalFree(s_buf); |
| 527 return NULL; | 527 return NULL; |
| 528 } | 528 } |
| 529 | 529 |
| 530 PyObject *PyErr_SetExcFromWindowsErrWithFilename( | 530 PyObject *PyErr_SetExcFromWindowsErrWithFilename( |
| 531 PyObject *exc, | 531 PyObject *exc, |
| 532 int ierr, | 532 int ierr, |
| 533 const char *filename) | 533 const char *filename) |
| 534 { | 534 { |
| 535 PyObject *name = filename ? PyUnicode_FromString(filename) : NULL; | 535 PyObject *name = filename ? PyUnicode_FromString(filename) : NULL; |
| 536 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc, | 536 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc, |
| 537 ierr, | 537 ierr, |
| 538 name); | 538 name); |
| 539 Py_XDECREF(name); | 539 Py_XDECREF(name); |
| 540 return ret; | 540 return ret; |
| 541 } | 541 } |
| 542 | 542 |
| 543 PyObject *PyErr_SetExcFromWindowsErrWithUnicodeFilename( | 543 PyObject *PyErr_SetExcFromWindowsErrWithUnicodeFilename( |
| 544 PyObject *exc, | 544 PyObject *exc, |
| 545 int ierr, | 545 int ierr, |
| 546 const Py_UNICODE *filename) | 546 const Py_UNICODE *filename) |
| 547 { | 547 { |
| 548 PyObject *name = filename ? | 548 PyObject *name = filename ? |
| 549 PyUnicode_FromUnicode(filename, wcslen(filename)) : | 549 PyUnicode_FromUnicode(filename, wcslen(filename)) : |
| 550 NULL; | 550 NULL; |
| 551 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc, | 551 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc, |
| 552 ierr, | 552 ierr, |
| 553 name); | 553 name); |
| 554 Py_XDECREF(name); | 554 Py_XDECREF(name); |
| 555 return ret; | 555 return ret; |
| 556 } | 556 } |
| 557 | 557 |
| 558 PyObject *PyErr_SetExcFromWindowsErr(PyObject *exc, int ierr) | 558 PyObject *PyErr_SetExcFromWindowsErr(PyObject *exc, int ierr) |
| 559 { | 559 { |
| 560 return PyErr_SetExcFromWindowsErrWithFilename(exc, ierr, NULL); | 560 return PyErr_SetExcFromWindowsErrWithFilename(exc, ierr, NULL); |
| 561 } | 561 } |
| 562 | 562 |
| 563 PyObject *PyErr_SetFromWindowsErr(int ierr) | 563 PyObject *PyErr_SetFromWindowsErr(int ierr) |
| 564 { | 564 { |
| 565 return PyErr_SetExcFromWindowsErrWithFilename(PyExc_WindowsError, | 565 return PyErr_SetExcFromWindowsErrWithFilename(PyExc_WindowsError, |
| 566 ierr, NULL); | 566 ierr, NULL); |
| 567 } | 567 } |
| 568 PyObject *PyErr_SetFromWindowsErrWithFilename( | 568 PyObject *PyErr_SetFromWindowsErrWithFilename( |
| 569 int ierr, | 569 int ierr, |
| 570 const char *filename) | 570 const char *filename) |
| 571 { | 571 { |
| 572 PyObject *name = filename ? PyUnicode_FromString(filename) : NULL; | 572 PyObject *name = filename ? PyUnicode_FromString(filename) : NULL; |
| 573 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject( | 573 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject( |
| 574 PyExc_WindowsError, | 574 PyExc_WindowsError, |
| 575 ierr, name); | 575 ierr, name); |
| 576 Py_XDECREF(name); | 576 Py_XDECREF(name); |
| 577 return result; | 577 return result; |
| 578 } | 578 } |
| 579 | 579 |
| 580 PyObject *PyErr_SetFromWindowsErrWithUnicodeFilename( | 580 PyObject *PyErr_SetFromWindowsErrWithUnicodeFilename( |
| 581 int ierr, | 581 int ierr, |
| 582 const Py_UNICODE *filename) | 582 const Py_UNICODE *filename) |
| 583 { | 583 { |
| 584 PyObject *name = filename ? | 584 PyObject *name = filename ? |
| 585 PyUnicode_FromUnicode(filename, wcslen(filename)) : | 585 PyUnicode_FromUnicode(filename, wcslen(filename)) : |
| 586 NULL; | 586 NULL; |
| 587 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject( | 587 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject( |
| 588 PyExc_WindowsError, | 588 PyExc_WindowsError, |
| 589 ierr, name); | 589 ierr, name); |
| 590 Py_XDECREF(name); | 590 Py_XDECREF(name); |
| 591 return result; | 591 return result; |
| 592 } | 592 } |
| 593 #endif /* MS_WINDOWS */ | 593 #endif /* MS_WINDOWS */ |
| 594 | 594 |
| 595 void | 595 void |
| 596 _PyErr_BadInternalCall(const char *filename, int lineno) | 596 _PyErr_BadInternalCall(const char *filename, int lineno) |
| 597 { | 597 { |
| 598 PyErr_Format(PyExc_SystemError, | 598 PyErr_Format(PyExc_SystemError, |
| 599 "%s:%d: bad argument to internal function", | 599 "%s:%d: bad argument to internal function", |
| 600 filename, lineno); | 600 filename, lineno); |
| 601 } | 601 } |
| 602 | 602 |
| 603 /* Remove the preprocessor macro for PyErr_BadInternalCall() so that we can | 603 /* Remove the preprocessor macro for PyErr_BadInternalCall() so that we can |
| 604 export the entry point for existing object code: */ | 604 export the entry point for existing object code: */ |
| 605 #undef PyErr_BadInternalCall | 605 #undef PyErr_BadInternalCall |
| 606 void | 606 void |
| 607 PyErr_BadInternalCall(void) | 607 PyErr_BadInternalCall(void) |
| 608 { | 608 { |
| 609 PyErr_Format(PyExc_SystemError, | 609 PyErr_Format(PyExc_SystemError, |
| 610 "bad argument to internal function"); | 610 "bad argument to internal function"); |
| 611 } | 611 } |
| 612 #define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__) | 612 #define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__) |
| 613 | 613 |
| 614 | 614 |
| 615 | 615 |
| 616 PyObject * | 616 PyObject * |
| 617 PyErr_Format(PyObject *exception, const char *format, ...) | 617 PyErr_Format(PyObject *exception, const char *format, ...) |
| 618 { | 618 { |
| 619 va_list vargs; | 619 va_list vargs; |
| 620 PyObject* string; | 620 PyObject* string; |
| 621 | 621 |
| 622 #ifdef HAVE_STDARG_PROTOTYPES | 622 #ifdef HAVE_STDARG_PROTOTYPES |
| 623 va_start(vargs, format); | 623 va_start(vargs, format); |
| 624 #else | 624 #else |
| 625 va_start(vargs); | 625 va_start(vargs); |
| 626 #endif | 626 #endif |
| 627 | 627 |
| 628 string = PyUnicode_FromFormatV(format, vargs); | 628 string = PyUnicode_FromFormatV(format, vargs); |
| 629 PyErr_SetObject(exception, string); | 629 PyErr_SetObject(exception, string); |
| 630 Py_XDECREF(string); | 630 Py_XDECREF(string); |
| 631 va_end(vargs); | 631 va_end(vargs); |
| 632 return NULL; | 632 return NULL; |
| 633 } | 633 } |
| 634 | 634 |
| 635 | 635 |
| 636 | 636 |
| 637 PyObject * | 637 PyObject * |
| 638 PyErr_NewException(const char *name, PyObject *base, PyObject *dict) | 638 PyErr_NewException(const char *name, PyObject *base, PyObject *dict) |
| 639 { | 639 { |
| 640 const char *dot; | 640 const char *dot; |
| 641 PyObject *modulename = NULL; | 641 PyObject *modulename = NULL; |
| 642 PyObject *classname = NULL; | 642 PyObject *classname = NULL; |
| 643 PyObject *mydict = NULL; | 643 PyObject *mydict = NULL; |
| 644 PyObject *bases = NULL; | 644 PyObject *bases = NULL; |
| 645 PyObject *result = NULL; | 645 PyObject *result = NULL; |
| 646 dot = strrchr(name, '.'); | 646 dot = strrchr(name, '.'); |
| 647 if (dot == NULL) { | 647 if (dot == NULL) { |
| 648 PyErr_SetString(PyExc_SystemError, | 648 PyErr_SetString(PyExc_SystemError, |
| 649 "PyErr_NewException: name must be module.class"); | 649 "PyErr_NewException: name must be module.class"); |
| 650 return NULL; | 650 return NULL; |
| 651 } | 651 } |
| 652 if (base == NULL) | 652 if (base == NULL) |
| 653 base = PyExc_Exception; | 653 base = PyExc_Exception; |
| 654 if (dict == NULL) { | 654 if (dict == NULL) { |
| 655 dict = mydict = PyDict_New(); | 655 dict = mydict = PyDict_New(); |
| 656 if (dict == NULL) | 656 if (dict == NULL) |
| 657 goto failure; | 657 goto failure; |
| 658 } | 658 } |
| 659 if (PyDict_GetItemString(dict, "__module__") == NULL) { | 659 if (PyDict_GetItemString(dict, "__module__") == NULL) { |
| 660 modulename = PyUnicode_FromStringAndSize(name, | 660 modulename = PyUnicode_FromStringAndSize(name, |
| 661 (Py_ssize_t)(dot-name)); | 661 (Py_ssize_t)(dot-name)); |
| 662 if (modulename == NULL) | 662 if (modulename == NULL) |
| 663 goto failure; | 663 goto failure; |
| 664 if (PyDict_SetItemString(dict, "__module__", modulename) != 0) | 664 if (PyDict_SetItemString(dict, "__module__", modulename) != 0) |
| 665 goto failure; | 665 goto failure; |
| 666 } | 666 } |
| 667 if (PyTuple_Check(base)) { | 667 if (PyTuple_Check(base)) { |
| 668 bases = base; | 668 bases = base; |
| 669 /* INCREF as we create a new ref in the else branch */ | 669 /* INCREF as we create a new ref in the else branch */ |
| 670 Py_INCREF(bases); | 670 Py_INCREF(bases); |
| 671 } else { | 671 } else { |
| 672 bases = PyTuple_Pack(1, base); | 672 bases = PyTuple_Pack(1, base); |
| 673 if (bases == NULL) | 673 if (bases == NULL) |
| 674 goto failure; | 674 goto failure; |
| 675 } | 675 } |
| 676 /* Create a real new-style class. */ | 676 /* Create a real new-style class. */ |
| 677 result = PyObject_CallFunction((PyObject *)&PyType_Type, "UOO", | 677 result = PyObject_CallFunction((PyObject *)&PyType_Type, "UOO", |
| 678 dot+1, bases, dict); | 678 dot+1, bases, dict); |
| 679 failure: | 679 failure: |
| 680 Py_XDECREF(bases); | 680 Py_XDECREF(bases); |
| 681 Py_XDECREF(mydict); | 681 Py_XDECREF(mydict); |
| 682 Py_XDECREF(classname); | 682 Py_XDECREF(classname); |
| 683 Py_XDECREF(modulename); | 683 Py_XDECREF(modulename); |
| 684 return result; | 684 return result; |
| 685 } | 685 } |
| 686 | 686 |
| 687 | 687 |
| 688 /* Create an exception with docstring */ | 688 /* Create an exception with docstring */ |
| 689 PyObject * | 689 PyObject * |
| 690 PyErr_NewExceptionWithDoc(const char *name, const char *doc, | 690 PyErr_NewExceptionWithDoc(const char *name, const char *doc, |
| 691 PyObject *base, PyObject *dict) | 691 PyObject *base, PyObject *dict) |
| 692 { | 692 { |
| 693 int result; | 693 int result; |
| 694 PyObject *ret = NULL; | 694 PyObject *ret = NULL; |
| 695 PyObject *mydict = NULL; /* points to the dict only if we create it */ | 695 PyObject *mydict = NULL; /* points to the dict only if we create it */ |
| 696 PyObject *docobj; | 696 PyObject *docobj; |
| 697 | 697 |
| 698 if (dict == NULL) { | 698 if (dict == NULL) { |
| 699 dict = mydict = PyDict_New(); | 699 dict = mydict = PyDict_New(); |
| 700 if (dict == NULL) { | 700 if (dict == NULL) { |
| 701 return NULL; | 701 return NULL; |
| 702 } | 702 } |
| 703 } | 703 } |
| 704 | 704 |
| 705 if (doc != NULL) { | 705 if (doc != NULL) { |
| 706 docobj = PyUnicode_FromString(doc); | 706 docobj = PyUnicode_FromString(doc); |
| 707 if (docobj == NULL) | 707 if (docobj == NULL) |
| 708 goto failure; | 708 goto failure; |
| 709 result = PyDict_SetItemString(dict, "__doc__", docobj); | 709 result = PyDict_SetItemString(dict, "__doc__", docobj); |
| 710 Py_DECREF(docobj); | 710 Py_DECREF(docobj); |
| 711 if (result < 0) | 711 if (result < 0) |
| 712 goto failure; | 712 goto failure; |
| 713 } | 713 } |
| 714 | 714 |
| 715 ret = PyErr_NewException(name, base, dict); | 715 ret = PyErr_NewException(name, base, dict); |
| 716 failure: | 716 failure: |
| 717 Py_XDECREF(mydict); | 717 Py_XDECREF(mydict); |
| 718 return ret; | 718 return ret; |
| 719 } | 719 } |
| 720 | 720 |
| 721 | 721 |
| 722 /* Call when an exception has occurred but there is no way for Python | 722 /* Call when an exception has occurred but there is no way for Python |
| 723 to handle it. Examples: exception in __del__ or during GC. */ | 723 to handle it. Examples: exception in __del__ or during GC. */ |
| 724 void | 724 void |
| 725 PyErr_WriteUnraisable(PyObject *obj) | 725 PyErr_WriteUnraisable(PyObject *obj) |
| 726 { | 726 { |
| 727 PyObject *f, *t, *v, *tb; | 727 PyObject *f, *t, *v, *tb; |
| 728 PyErr_Fetch(&t, &v, &tb); | 728 PyErr_Fetch(&t, &v, &tb); |
| 729 f = PySys_GetObject("stderr"); | 729 f = PySys_GetObject("stderr"); |
| 730 if (f != NULL && f != Py_None) { | 730 if (f != NULL && f != Py_None) { |
| 731 PyFile_WriteString("Exception ", f); | 731 PyFile_WriteString("Exception ", f); |
| 732 if (t) { | 732 if (t) { |
| 733 PyObject* moduleName; | 733 PyObject* moduleName; |
| 734 char* className; | 734 char* className; |
| 735 assert(PyExceptionClass_Check(t)); | 735 assert(PyExceptionClass_Check(t)); |
| 736 className = PyExceptionClass_Name(t); | 736 className = PyExceptionClass_Name(t); |
| 737 if (className != NULL) { | 737 if (className != NULL) { |
| 738 char *dot = strrchr(className, '.'); | 738 char *dot = strrchr(className, '.'); |
| 739 if (dot != NULL) | 739 if (dot != NULL) |
| 740 className = dot+1; | 740 className = dot+1; |
| 741 } | 741 } |
| 742 | 742 |
| 743 moduleName = PyObject_GetAttrString(t, "__module__"); | 743 moduleName = PyObject_GetAttrString(t, "__module__"); |
| 744 if (moduleName == NULL) | 744 if (moduleName == NULL) |
| 745 PyFile_WriteString("<unknown>", f); | 745 PyFile_WriteString("<unknown>", f); |
| 746 else { | 746 else { |
| 747 char* modstr = _PyUnicode_AsString(moduleName); | 747 char* modstr = _PyUnicode_AsString(moduleName); |
| 748 if (modstr && | 748 if (modstr && |
| 749 strcmp(modstr, "builtins") != 0) | 749 strcmp(modstr, "builtins") != 0) |
| 750 { | 750 { |
| 751 PyFile_WriteString(modstr, f); | 751 PyFile_WriteString(modstr, f); |
| 752 PyFile_WriteString(".", f); | 752 PyFile_WriteString(".", f); |
| 753 } | 753 } |
| 754 } | 754 } |
| 755 if (className == NULL) | 755 if (className == NULL) |
| 756 PyFile_WriteString("<unknown>", f); | 756 PyFile_WriteString("<unknown>", f); |
| 757 else | 757 else |
| 758 PyFile_WriteString(className, f); | 758 PyFile_WriteString(className, f); |
| 759 if (v && v != Py_None) { | 759 if (v && v != Py_None) { |
| 760 PyFile_WriteString(": ", f); | 760 PyFile_WriteString(": ", f); |
| 761 PyFile_WriteObject(v, f, 0); | 761 PyFile_WriteObject(v, f, 0); |
| 762 } | 762 } |
| 763 Py_XDECREF(moduleName); | 763 Py_XDECREF(moduleName); |
| 764 } | 764 } |
| 765 PyFile_WriteString(" in ", f); | 765 PyFile_WriteString(" in ", f); |
| 766 PyFile_WriteObject(obj, f, 0); | 766 PyFile_WriteObject(obj, f, 0); |
| 767 PyFile_WriteString(" ignored\n", f); | 767 PyFile_WriteString(" ignored\n", f); |
| 768 PyErr_Clear(); /* Just in case */ | 768 PyErr_Clear(); /* Just in case */ |
| 769 } | 769 } |
| 770 Py_XDECREF(t); | 770 Py_XDECREF(t); |
| 771 Py_XDECREF(v); | 771 Py_XDECREF(v); |
| 772 Py_XDECREF(tb); | 772 Py_XDECREF(tb); |
| 773 } | 773 } |
| 774 | 774 |
| 775 extern PyObject *PyModule_GetWarningsModule(void); | 775 extern PyObject *PyModule_GetWarningsModule(void); |
| 776 | 776 |
| 777 | 777 |
| 780 to make printing of exceptions believe it is a syntax error. */ | 780 to make printing of exceptions believe it is a syntax error. */ |
| 781 | 781 |
| 782 void | 782 void |
| 783 PyErr_SyntaxLocation(const char *filename, int lineno) | 783 PyErr_SyntaxLocation(const char *filename, int lineno) |
| 784 { | 784 { |
| 785 PyObject *exc, *v, *tb, *tmp; | 785 PyObject *exc, *v, *tb, *tmp; |
| 786 | 786 |
| 787 /* add attributes for the line number and filename for the error */ | 787 /* add attributes for the line number and filename for the error */ |
| 788 PyErr_Fetch(&exc, &v, &tb); | 788 PyErr_Fetch(&exc, &v, &tb); |
| 789 PyErr_NormalizeException(&exc, &v, &tb); | 789 PyErr_NormalizeException(&exc, &v, &tb); |
| 790 /* XXX check that it is, indeed, a syntax error. It might not | 790 /* XXX check that it is, indeed, a syntax error. It might not |
| 791 * be, though. */ | 791 * be, though. */ |
| 792 tmp = PyLong_FromLong(lineno); | 792 tmp = PyLong_FromLong(lineno); |
| 793 if (tmp == NULL) | 793 if (tmp == NULL) |
| 794 PyErr_Clear(); | 794 PyErr_Clear(); |
| 795 else { | 795 else { |
| 796 if (PyObject_SetAttrString(v, "lineno", tmp)) | 796 if (PyObject_SetAttrString(v, "lineno", tmp)) |
| 797 PyErr_Clear(); | 797 PyErr_Clear(); |
| 798 Py_DECREF(tmp); | 798 Py_DECREF(tmp); |
| 799 } | 799 } |
| 800 if (filename != NULL) { | 800 if (filename != NULL) { |
| 801 tmp = PyUnicode_FromString(filename); | 801 tmp = PyUnicode_FromString(filename); |
| 802 if (tmp == NULL) | 802 if (tmp == NULL) |
| 803 PyErr_Clear(); | 803 PyErr_Clear(); |
| 804 else { | 804 else { |
| 805 if (PyObject_SetAttrString(v, "filename", tmp)) | 805 if (PyObject_SetAttrString(v, "filename", tmp)) |
| 806 PyErr_Clear(); | 806 PyErr_Clear(); |
| 807 Py_DECREF(tmp); | 807 Py_DECREF(tmp); |
| 808 } | 808 } |
| 809 | 809 |
| 810 tmp = PyErr_ProgramText(filename, lineno); | 810 tmp = PyErr_ProgramText(filename, lineno); |
| 811 if (tmp) { | 811 if (tmp) { |
| 812 if (PyObject_SetAttrString(v, "text", tmp)) | 812 if (PyObject_SetAttrString(v, "text", tmp)) |
| 813 PyErr_Clear(); | 813 PyErr_Clear(); |
| 814 Py_DECREF(tmp); | 814 Py_DECREF(tmp); |
| 815 } | 815 } |
| 816 } | 816 } |
| 817 if (PyObject_SetAttrString(v, "offset", Py_None)) { | 817 if (PyObject_SetAttrString(v, "offset", Py_None)) { |
| 818 PyErr_Clear(); | 818 PyErr_Clear(); |
| 819 } | 819 } |
| 820 if (exc != PyExc_SyntaxError) { | 820 if (exc != PyExc_SyntaxError) { |
| 821 if (!PyObject_HasAttrString(v, "msg")) { | 821 if (!PyObject_HasAttrString(v, "msg")) { |
| 822 tmp = PyObject_Str(v); | 822 tmp = PyObject_Str(v); |
| 823 if (tmp) { | 823 if (tmp) { |
| 824 if (PyObject_SetAttrString(v, "msg", tmp)) | 824 if (PyObject_SetAttrString(v, "msg", tmp)) |
| 825 PyErr_Clear(); | 825 PyErr_Clear(); |
| 826 Py_DECREF(tmp); | 826 Py_DECREF(tmp); |
| 827 } else { | 827 } else { |
| 828 PyErr_Clear(); | 828 PyErr_Clear(); |
| 829 } | 829 } |
| 830 } | 830 } |
| 831 if (!PyObject_HasAttrString(v, "print_file_and_line")) { | 831 if (!PyObject_HasAttrString(v, "print_file_and_line")) { |
| 832 if (PyObject_SetAttrString(v, "print_file_and_line", | 832 if (PyObject_SetAttrString(v, "print_file_and_line", |
| 833 Py_None)) | 833 Py_None)) |
| 834 PyErr_Clear(); | 834 PyErr_Clear(); |
| 835 } | 835 } |
| 836 } | 836 } |
| 837 PyErr_Restore(exc, v, tb); | 837 PyErr_Restore(exc, v, tb); |
| 838 } | 838 } |
| 839 | 839 |
| 840 /* Attempt to load the line of text that the exception refers to. If it | 840 /* Attempt to load the line of text that the exception refers to. If it |
| 841 fails, it will return NULL but will not set an exception. | 841 fails, it will return NULL but will not set an exception. |
| 842 | 842 |
| 844 functionality in tb_displayline() in traceback.c. */ | 844 functionality in tb_displayline() in traceback.c. */ |
| 845 | 845 |
| 846 PyObject * | 846 PyObject * |
| 847 PyErr_ProgramText(const char *filename, int lineno) | 847 PyErr_ProgramText(const char *filename, int lineno) |
| 848 { | 848 { |
| 849 FILE *fp; | 849 FILE *fp; |
| 850 int i; | 850 int i; |
| 851 char linebuf[1000]; | 851 char linebuf[1000]; |
| 852 | 852 |
| 853 if (filename == NULL || *filename == '\0' || lineno <= 0) | 853 if (filename == NULL || *filename == '\0' || lineno <= 0) |
| 854 return NULL; | 854 return NULL; |
| 855 fp = fopen(filename, "r" PY_STDIOTEXTMODE); | 855 fp = fopen(filename, "r" PY_STDIOTEXTMODE); |
| 856 if (fp == NULL) | 856 if (fp == NULL) |
| 857 return NULL; | 857 return NULL; |
| 858 for (i = 0; i < lineno; i++) { | 858 for (i = 0; i < lineno; i++) { |
| 859 char *pLastChar = &linebuf[sizeof(linebuf) - 2]; | 859 char *pLastChar = &linebuf[sizeof(linebuf) - 2]; |
| 860 do { | 860 do { |
| 861 *pLastChar = '\0'; | 861 *pLastChar = '\0'; |
| 862 if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf, | 862 if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf, |
| 863 fp, NULL) == NULL) | 863 fp, NULL) == NULL) |
| 864 break; | 864 break; |
| 865 /* fgets read *something*; if it didn't get as | 865 /* fgets read *something*; if it didn't get as |
| 866 far as pLastChar, it must have found a newline | 866 far as pLastChar, it must have found a newline |
| 867 or hit the end of the file; if pLastChar is \n, | 867 or hit the end of the file; if pLastChar is \n, |
| 868 it obviously found a newline; else we haven't | 868 it obviously found a newline; else we haven't |
| 869 yet seen a newline, so must continue */ | 869 yet seen a newline, so must continue */ |
| 870 } while (*pLastChar != '\0' && *pLastChar != '\n'); | 870 } while (*pLastChar != '\0' && *pLastChar != '\n'); |
| 871 } | 871 } |
| 872 fclose(fp); | 872 fclose(fp); |
| 873 if (i == lineno) { | 873 if (i == lineno) { |
| 874 char *p = linebuf; | 874 char *p = linebuf; |
| 875 PyObject *res; | 875 PyObject *res; |
| 876 while (*p == ' ' || *p == '\t' || *p == '\014') | 876 while (*p == ' ' || *p == '\t' || *p == '\014') |
| 877 p++; | 877 p++; |
| 878 res = PyUnicode_FromString(p); | 878 res = PyUnicode_FromString(p); |
| 879 if (res == NULL) | 879 if (res == NULL) |
| 880 PyErr_Clear(); | 880 PyErr_Clear(); |
| 881 return res; | 881 return res; |
| 882 } | 882 } |
| 883 return NULL; | 883 return NULL; |
| 884 } | 884 } |
| 885 | 885 |
| 886 #ifdef __cplusplus | 886 #ifdef __cplusplus |
| 887 } | 887 } |
| 888 #endif | 888 #endif |
