errors.c revision 584b16a1f340b086526773ec3275589468ae4b04
1/*********************************************************** 2Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, 3The Netherlands. 4 5 All Rights Reserved 6 7Permission to use, copy, modify, and distribute this software and its 8documentation for any purpose and without fee is hereby granted, 9provided that the above copyright notice appear in all copies and that 10both that copyright notice and this permission notice appear in 11supporting documentation, and that the names of Stichting Mathematisch 12Centrum or CWI or Corporation for National Research Initiatives or 13CNRI not be used in advertising or publicity pertaining to 14distribution of the software without specific, written prior 15permission. 16 17While CWI is the initial source for this software, a modified version 18is made available by the Corporation for National Research Initiatives 19(CNRI) at the Internet address ftp://ftp.python.org. 20 21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH 22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF 23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH 24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL 25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 28PERFORMANCE OF THIS SOFTWARE. 29 30******************************************************************/ 31 32/* Error handling */ 33 34#include "Python.h" 35 36#ifdef SYMANTEC__CFM68K__ 37#pragma lib_export on 38#endif 39 40#ifdef macintosh 41extern char *PyMac_StrError Py_PROTO((int)); 42#undef strerror 43#define strerror PyMac_StrError 44#endif /* macintosh */ 45 46#ifndef __STDC__ 47#ifndef MS_WINDOWS 48extern char *strerror Py_PROTO((int)); 49#endif 50#endif 51 52#ifdef MS_WIN32 53#include "windows.h" 54#include "winbase.h" 55#endif 56 57void 58PyErr_Restore(type, value, traceback) 59 PyObject *type; 60 PyObject *value; 61 PyObject *traceback; 62{ 63 PyThreadState *tstate = PyThreadState_GET(); 64 PyObject *oldtype, *oldvalue, *oldtraceback; 65 66 if (traceback != NULL && !PyTraceBack_Check(traceback)) { 67 /* XXX Should never happen -- fatal error instead? */ 68 Py_DECREF(traceback); 69 traceback = NULL; 70 } 71 72 /* Save these in locals to safeguard against recursive 73 invocation through Py_XDECREF */ 74 oldtype = tstate->curexc_type; 75 oldvalue = tstate->curexc_value; 76 oldtraceback = tstate->curexc_traceback; 77 78 tstate->curexc_type = type; 79 tstate->curexc_value = value; 80 tstate->curexc_traceback = traceback; 81 82 Py_XDECREF(oldtype); 83 Py_XDECREF(oldvalue); 84 Py_XDECREF(oldtraceback); 85} 86 87void 88PyErr_SetObject(exception, value) 89 PyObject *exception; 90 PyObject *value; 91{ 92 Py_XINCREF(exception); 93 Py_XINCREF(value); 94 PyErr_Restore(exception, value, (PyObject *)NULL); 95} 96 97void 98PyErr_SetNone(exception) 99 PyObject *exception; 100{ 101 PyErr_SetObject(exception, (PyObject *)NULL); 102} 103 104void 105PyErr_SetString(exception, string) 106 PyObject *exception; 107 const char *string; 108{ 109 PyObject *value = PyString_FromString(string); 110 PyErr_SetObject(exception, value); 111 Py_XDECREF(value); 112} 113 114 115PyObject * 116PyErr_Occurred() 117{ 118 PyThreadState *tstate = PyThreadState_Get(); 119 120 return tstate->curexc_type; 121} 122 123 124int 125PyErr_GivenExceptionMatches(err, exc) 126 PyObject *err, *exc; 127{ 128 if (PyTuple_Check(exc)) { 129 int i, n; 130 n = PyTuple_Size(exc); 131 for (i = 0; i < n; i++) { 132 /* Test recursively */ 133 if (PyErr_GivenExceptionMatches( 134 err, PyTuple_GET_ITEM(exc, i))) 135 { 136 return 1; 137 } 138 } 139 return 0; 140 } 141 /* err might be an instance, so check its class. */ 142 if (PyInstance_Check(err)) 143 err = (PyObject*)((PyInstanceObject*)err)->in_class; 144 145 if (PyClass_Check(err) && PyClass_Check(exc)) 146 return PyClass_IsSubclass(err, exc); 147 148 return err == exc; 149} 150 151 152int 153PyErr_ExceptionMatches(exc) 154 PyObject *exc; 155{ 156 return PyErr_GivenExceptionMatches(PyErr_Occurred(), exc); 157} 158 159 160/* Used in many places to normalize a raised exception, including in 161 eval_code2(), do_raise(), and PyErr_Print() 162*/ 163void 164PyErr_NormalizeException(exc, val, tb) 165 PyObject **exc; 166 PyObject **val; 167 PyObject **tb; 168{ 169 PyObject *type = *exc; 170 PyObject *value = *val; 171 PyObject *inclass = NULL; 172 173 /* If PyErr_SetNone() was used, the value will have been actually 174 set to NULL. 175 */ 176 if (!value) { 177 value = Py_None; 178 Py_INCREF(value); 179 } 180 181 if (PyInstance_Check(value)) 182 inclass = (PyObject*)((PyInstanceObject*)value)->in_class; 183 184 /* Normalize the exception so that if the type is a class, the 185 value will be an instance. 186 */ 187 if (PyClass_Check(type)) { 188 /* if the value was not an instance, or is not an instance 189 whose class is (or is derived from) type, then use the 190 value as an argument to instantiation of the type 191 class. 192 */ 193 if (!inclass || !PyClass_IsSubclass(inclass, type)) { 194 PyObject *args, *res; 195 196 if (value == Py_None) 197 args = Py_BuildValue("()"); 198 else if (PyTuple_Check(value)) { 199 Py_INCREF(value); 200 args = value; 201 } 202 else 203 args = Py_BuildValue("(O)", value); 204 205 if (args == NULL) 206 goto finally; 207 res = PyEval_CallObject(type, args); 208 Py_DECREF(args); 209 if (res == NULL) 210 goto finally; 211 Py_DECREF(value); 212 value = res; 213 } 214 /* if the class of the instance doesn't exactly match the 215 class of the type, believe the instance 216 */ 217 else if (inclass != type) { 218 Py_DECREF(type); 219 type = inclass; 220 Py_INCREF(type); 221 } 222 } 223 *exc = type; 224 *val = value; 225 return; 226finally: 227 Py_DECREF(type); 228 Py_DECREF(value); 229 Py_XDECREF(*tb); 230 PyErr_Fetch(exc, val, tb); 231 /* normalize recursively */ 232 PyErr_NormalizeException(exc, val, tb); 233} 234 235 236void 237PyErr_Fetch(p_type, p_value, p_traceback) 238 PyObject **p_type; 239 PyObject **p_value; 240 PyObject **p_traceback; 241{ 242 PyThreadState *tstate = PyThreadState_Get(); 243 244 *p_type = tstate->curexc_type; 245 *p_value = tstate->curexc_value; 246 *p_traceback = tstate->curexc_traceback; 247 248 tstate->curexc_type = NULL; 249 tstate->curexc_value = NULL; 250 tstate->curexc_traceback = NULL; 251} 252 253void 254PyErr_Clear() 255{ 256 PyErr_Restore(NULL, NULL, NULL); 257} 258 259/* Convenience functions to set a type error exception and return 0 */ 260 261int 262PyErr_BadArgument() 263{ 264 PyErr_SetString(PyExc_TypeError, 265 "illegal argument type for built-in operation"); 266 return 0; 267} 268 269PyObject * 270PyErr_NoMemory() 271{ 272 /* raise the pre-allocated instance if it still exists */ 273 if (PyExc_MemoryErrorInst) 274 PyErr_SetObject(PyExc_MemoryError, PyExc_MemoryErrorInst); 275 else 276 /* this will probably fail since there's no memory and hee, 277 hee, we have to instantiate this class 278 */ 279 PyErr_SetNone(PyExc_MemoryError); 280 281 return NULL; 282} 283 284PyObject * 285PyErr_SetFromErrnoWithFilename(exc, filename) 286 PyObject *exc; 287 char *filename; 288{ 289 PyObject *v; 290 char *s; 291 int i = errno; 292#ifdef MS_WIN32 293 char *s_buf = NULL; 294#endif 295#ifdef EINTR 296 if (i == EINTR && PyErr_CheckSignals()) 297 return NULL; 298#endif 299 if (i == 0) 300 s = "Error"; /* Sometimes errno didn't get set */ 301 else 302#ifndef MS_WIN32 303 s = strerror(i); 304#else 305 { 306 /* Note that the Win32 errors do not lineup with the 307 errno error. So if the error is in the MSVC error 308 table, we use it, otherwise we assume it really _is_ 309 a Win32 error code 310 */ 311 if (i > 0 && i < _sys_nerr) { 312 s = _sys_errlist[i]; 313 } 314 else { 315 int len = FormatMessage( 316 FORMAT_MESSAGE_ALLOCATE_BUFFER | 317 FORMAT_MESSAGE_FROM_SYSTEM | 318 FORMAT_MESSAGE_IGNORE_INSERTS, 319 NULL, /* no message source */ 320 i, 321 MAKELANGID(LANG_NEUTRAL, 322 SUBLANG_DEFAULT), 323 /* Default language */ 324 (LPTSTR) &s_buf, 325 0, /* size not used */ 326 NULL); /* no args */ 327 s = s_buf; 328 /* remove trailing cr/lf and dots */ 329 while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.')) 330 s[--len] = '\0'; 331 } 332 } 333#endif 334 if (filename != NULL && Py_UseClassExceptionsFlag) 335 v = Py_BuildValue("(iss)", i, s, filename); 336 else 337 v = Py_BuildValue("(is)", i, s); 338 if (v != NULL) { 339 PyErr_SetObject(exc, v); 340 Py_DECREF(v); 341 } 342#ifdef MS_WIN32 343 LocalFree(s_buf); 344#endif 345 return NULL; 346} 347 348 349PyObject * 350PyErr_SetFromErrno(exc) 351 PyObject *exc; 352{ 353 return PyErr_SetFromErrnoWithFilename(exc, NULL); 354} 355 356#ifdef MS_WINDOWS 357/* Windows specific error code handling */ 358PyObject *PyErr_SetFromWindowsErrWithFilename( 359 int ierr, 360 const char *filename) 361{ 362 int len; 363 char *s; 364 PyObject *v; 365 DWORD err = (DWORD)ierr; 366 if (err==0) err = GetLastError(); 367 len = FormatMessage( 368 /* Error API error */ 369 FORMAT_MESSAGE_ALLOCATE_BUFFER | 370 FORMAT_MESSAGE_FROM_SYSTEM | 371 FORMAT_MESSAGE_IGNORE_INSERTS, 372 NULL, /* no message source */ 373 err, 374 MAKELANGID(LANG_NEUTRAL, 375 SUBLANG_DEFAULT), /* Default language */ 376 (LPTSTR) &s, 377 0, /* size not used */ 378 NULL); /* no args */ 379 /* remove trailing cr/lf and dots */ 380 while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.')) 381 s[--len] = '\0'; 382 if (filename != NULL && Py_UseClassExceptionsFlag) 383 v = Py_BuildValue("(iss)", err, s, filename); 384 else 385 v = Py_BuildValue("(is)", err, s); 386 if (v != NULL) { 387 PyErr_SetObject(PyExc_EnvironmentError, v); 388 Py_DECREF(v); 389 } 390 LocalFree(s); 391 return NULL; 392} 393 394PyObject *PyErr_SetFromWindowsErr(int ierr) 395{ 396 return PyErr_SetFromWindowsErrWithFilename(ierr, NULL); 397 398} 399#endif /* MS_WINDOWS */ 400 401void 402PyErr_BadInternalCall() 403{ 404 PyErr_SetString(PyExc_SystemError, 405 "bad argument to internal function"); 406} 407 408 409#ifdef HAVE_STDARG_PROTOTYPES 410PyObject * 411PyErr_Format(PyObject *exception, const char *format, ...) 412#else 413PyObject * 414PyErr_Format(exception, format, va_alist) 415 PyObject *exception; 416 const char *format; 417 va_dcl 418#endif 419{ 420 va_list vargs; 421 char buffer[500]; /* Caller is responsible for limiting the format */ 422 423#ifdef HAVE_STDARG_PROTOTYPES 424 va_start(vargs, format); 425#else 426 va_start(vargs); 427#endif 428 429 vsprintf(buffer, format, vargs); 430 PyErr_SetString(exception, buffer); 431 return NULL; 432} 433 434 435PyObject * 436PyErr_NewException(name, base, dict) 437 char *name; /* modulename.classname */ 438 PyObject *base; 439 PyObject *dict; 440{ 441 char *dot; 442 PyObject *modulename = NULL; 443 PyObject *classname = NULL; 444 PyObject *mydict = NULL; 445 PyObject *bases = NULL; 446 PyObject *result = NULL; 447 dot = strrchr(name, '.'); 448 if (dot == NULL) { 449 PyErr_SetString(PyExc_SystemError, 450 "PyErr_NewException: name must be module.class"); 451 return NULL; 452 } 453 if (base == NULL) 454 base = PyExc_Exception; 455 if (!PyClass_Check(base)) { 456 /* Must be using string-based standard exceptions (-X) */ 457 return PyString_FromString(name); 458 } 459 if (dict == NULL) { 460 dict = mydict = PyDict_New(); 461 if (dict == NULL) 462 goto failure; 463 } 464 if (PyDict_GetItemString(dict, "__module__") == NULL) { 465 modulename = PyString_FromStringAndSize(name, (int)(dot-name)); 466 if (modulename == NULL) 467 goto failure; 468 if (PyDict_SetItemString(dict, "__module__", modulename) != 0) 469 goto failure; 470 } 471 classname = PyString_FromString(dot+1); 472 if (classname == NULL) 473 goto failure; 474 bases = Py_BuildValue("(O)", base); 475 if (bases == NULL) 476 goto failure; 477 result = PyClass_New(bases, dict, classname); 478 failure: 479 Py_XDECREF(bases); 480 Py_XDECREF(mydict); 481 Py_XDECREF(classname); 482 Py_XDECREF(modulename); 483 return result; 484} 485