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