Mercurial > cpython-lru_cache-weakref
comparison Python/errors.c @ 36751:8e04de2480e4 legacy-trunk
PEP 352 implementation. Creates a new base class, BaseException, which has an
added message attribute compared to the previous version of Exception. It is
also a new-style class, making all exceptions now new-style. KeyboardInterrupt
and SystemExit inherit from BaseException directly. String exceptions now
raise DeprecationWarning.
Applies patch 1104669, and closes bugs 1012952 and 518846.
| author | Brett Cannon <bcannon@gmail.com> |
|---|---|
| date | Wed, 01 Mar 2006 04:25:17 +0000 |
| parents | fa92fe15251a |
| children | 2f3f2e32dfd9 49c6eb688906 |
comparison
equal
deleted
inserted
replaced
| 36750:92ba72484ff3 | 36751:8e04de2480e4 |
|---|---|
| 95 } | 95 } |
| 96 } | 96 } |
| 97 return 0; | 97 return 0; |
| 98 } | 98 } |
| 99 /* err might be an instance, so check its class. */ | 99 /* err might be an instance, so check its class. */ |
| 100 if (PyInstance_Check(err)) | 100 if (PyExceptionInstance_Check(err)) |
| 101 err = (PyObject*)((PyInstanceObject*)err)->in_class; | 101 err = PyExceptionInstance_Class(err); |
| 102 | 102 |
| 103 if (PyClass_Check(err) && PyClass_Check(exc)) | 103 if (PyExceptionClass_Check(err) && PyExceptionClass_Check(exc)) { |
| 104 return PyClass_IsSubclass(err, exc); | 104 /* problems here!? not sure PyObject_IsSubclass expects to |
| 105 be called with an exception pending... */ | |
| 106 return PyObject_IsSubclass(err, exc); | |
| 107 } | |
| 105 | 108 |
| 106 return err == exc; | 109 return err == exc; |
| 107 } | 110 } |
| 108 | 111 |
| 109 | 112 |
| 136 if (!value) { | 139 if (!value) { |
| 137 value = Py_None; | 140 value = Py_None; |
| 138 Py_INCREF(value); | 141 Py_INCREF(value); |
| 139 } | 142 } |
| 140 | 143 |
| 141 if (PyInstance_Check(value)) | 144 if (PyExceptionInstance_Check(value)) |
| 142 inclass = (PyObject*)((PyInstanceObject*)value)->in_class; | 145 inclass = PyExceptionInstance_Class(value); |
| 143 | 146 |
| 144 /* Normalize the exception so that if the type is a class, the | 147 /* Normalize the exception so that if the type is a class, the |
| 145 value will be an instance. | 148 value will be an instance. |
| 146 */ | 149 */ |
| 147 if (PyClass_Check(type)) { | 150 if (PyExceptionClass_Check(type)) { |
| 148 /* if the value was not an instance, or is not an instance | 151 /* if the value was not an instance, or is not an instance |
| 149 whose class is (or is derived from) type, then use the | 152 whose class is (or is derived from) type, then use the |
| 150 value as an argument to instantiation of the type | 153 value as an argument to instantiation of the type |
| 151 class. | 154 class. |
| 152 */ | 155 */ |
| 153 if (!inclass || !PyClass_IsSubclass(inclass, type)) { | 156 if (!inclass || !PyObject_IsSubclass(inclass, type)) { |
| 154 PyObject *args, *res; | 157 PyObject *args, *res; |
| 155 | 158 |
| 156 if (value == Py_None) | 159 if (value == Py_None) |
| 157 args = PyTuple_New(0); | 160 args = PyTuple_New(0); |
| 158 else if (PyTuple_Check(value)) { | 161 else if (PyTuple_Check(value)) { |
| 280 s = strerror(i); | 283 s = strerror(i); |
| 281 #else | 284 #else |
| 282 { | 285 { |
| 283 /* Note that the Win32 errors do not lineup with the | 286 /* Note that the Win32 errors do not lineup with the |
| 284 errno error. So if the error is in the MSVC error | 287 errno error. So if the error is in the MSVC error |
| 285 table, we use it, otherwise we assume it really _is_ | 288 table, we use it, otherwise we assume it really _is_ |
| 286 a Win32 error code | 289 a Win32 error code |
| 287 */ | 290 */ |
| 288 if (i > 0 && i < _sys_nerr) { | 291 if (i > 0 && i < _sys_nerr) { |
| 289 s = _sys_errlist[i]; | 292 s = _sys_errlist[i]; |
| 290 } | 293 } |
| 300 /* Default language */ | 303 /* Default language */ |
| 301 (LPTSTR) &s_buf, | 304 (LPTSTR) &s_buf, |
| 302 0, /* size not used */ | 305 0, /* size not used */ |
| 303 NULL); /* no args */ | 306 NULL); /* no args */ |
| 304 if (len==0) { | 307 if (len==0) { |
| 305 /* Only ever seen this in out-of-mem | 308 /* Only ever seen this in out-of-mem |
| 306 situations */ | 309 situations */ |
| 307 sprintf(s_small_buf, "Windows Error 0x%X", i); | 310 sprintf(s_small_buf, "Windows Error 0x%X", i); |
| 308 s = s_small_buf; | 311 s = s_small_buf; |
| 309 s_buf = NULL; | 312 s_buf = NULL; |
| 310 } else { | 313 } else { |
| 343 | 346 |
| 344 #ifdef Py_WIN_WIDE_FILENAMES | 347 #ifdef Py_WIN_WIDE_FILENAMES |
| 345 PyObject * | 348 PyObject * |
| 346 PyErr_SetFromErrnoWithUnicodeFilename(PyObject *exc, Py_UNICODE *filename) | 349 PyErr_SetFromErrnoWithUnicodeFilename(PyObject *exc, Py_UNICODE *filename) |
| 347 { | 350 { |
| 348 PyObject *name = filename ? | 351 PyObject *name = filename ? |
| 349 PyUnicode_FromUnicode(filename, wcslen(filename)) : | 352 PyUnicode_FromUnicode(filename, wcslen(filename)) : |
| 350 NULL; | 353 NULL; |
| 351 PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name); | 354 PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name); |
| 352 Py_XDECREF(name); | 355 Py_XDECREF(name); |
| 353 return result; | 356 return result; |
| 354 } | 357 } |
| 358 PyErr_SetFromErrno(PyObject *exc) | 361 PyErr_SetFromErrno(PyObject *exc) |
| 359 { | 362 { |
| 360 return PyErr_SetFromErrnoWithFilenameObject(exc, NULL); | 363 return PyErr_SetFromErrnoWithFilenameObject(exc, NULL); |
| 361 } | 364 } |
| 362 | 365 |
| 363 #ifdef MS_WINDOWS | 366 #ifdef MS_WINDOWS |
| 364 /* Windows specific error code handling */ | 367 /* Windows specific error code handling */ |
| 365 PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject( | 368 PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject( |
| 366 PyObject *exc, | 369 PyObject *exc, |
| 367 int ierr, | 370 int ierr, |
| 368 PyObject *filenameObject) | 371 PyObject *filenameObject) |
| 413 PyObject *exc, | 416 PyObject *exc, |
| 414 int ierr, | 417 int ierr, |
| 415 const char *filename) | 418 const char *filename) |
| 416 { | 419 { |
| 417 PyObject *name = filename ? PyString_FromString(filename) : NULL; | 420 PyObject *name = filename ? PyString_FromString(filename) : NULL; |
| 418 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc, | 421 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc, |
| 419 ierr, | 422 ierr, |
| 420 name); | 423 name); |
| 421 Py_XDECREF(name); | 424 Py_XDECREF(name); |
| 422 return ret; | 425 return ret; |
| 423 } | 426 } |
| 424 | 427 |
| 426 PyObject *PyErr_SetExcFromWindowsErrWithUnicodeFilename( | 429 PyObject *PyErr_SetExcFromWindowsErrWithUnicodeFilename( |
| 427 PyObject *exc, | 430 PyObject *exc, |
| 428 int ierr, | 431 int ierr, |
| 429 const Py_UNICODE *filename) | 432 const Py_UNICODE *filename) |
| 430 { | 433 { |
| 431 PyObject *name = filename ? | 434 PyObject *name = filename ? |
| 432 PyUnicode_FromUnicode(filename, wcslen(filename)) : | 435 PyUnicode_FromUnicode(filename, wcslen(filename)) : |
| 433 NULL; | 436 NULL; |
| 434 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc, | 437 PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc, |
| 435 ierr, | 438 ierr, |
| 436 name); | 439 name); |
| 437 Py_XDECREF(name); | 440 Py_XDECREF(name); |
| 438 return ret; | 441 return ret; |
| 439 } | 442 } |
| 440 #endif /* Py_WIN_WIDE_FILENAMES */ | 443 #endif /* Py_WIN_WIDE_FILENAMES */ |
| 464 #ifdef Py_WIN_WIDE_FILENAMES | 467 #ifdef Py_WIN_WIDE_FILENAMES |
| 465 PyObject *PyErr_SetFromWindowsErrWithUnicodeFilename( | 468 PyObject *PyErr_SetFromWindowsErrWithUnicodeFilename( |
| 466 int ierr, | 469 int ierr, |
| 467 const Py_UNICODE *filename) | 470 const Py_UNICODE *filename) |
| 468 { | 471 { |
| 469 PyObject *name = filename ? | 472 PyObject *name = filename ? |
| 470 PyUnicode_FromUnicode(filename, wcslen(filename)) : | 473 PyUnicode_FromUnicode(filename, wcslen(filename)) : |
| 471 NULL; | 474 NULL; |
| 472 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject( | 475 PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject( |
| 473 PyExc_WindowsError, | 476 PyExc_WindowsError, |
| 474 ierr, name); | 477 ierr, name); |
| 475 Py_XDECREF(name); | 478 Py_XDECREF(name); |
| 572 PyErr_Fetch(&t, &v, &tb); | 575 PyErr_Fetch(&t, &v, &tb); |
| 573 f = PySys_GetObject("stderr"); | 576 f = PySys_GetObject("stderr"); |
| 574 if (f != NULL) { | 577 if (f != NULL) { |
| 575 PyFile_WriteString("Exception ", f); | 578 PyFile_WriteString("Exception ", f); |
| 576 if (t) { | 579 if (t) { |
| 577 PyFile_WriteObject(t, f, Py_PRINT_RAW); | 580 char* className = PyExceptionClass_Name(t); |
| 581 PyObject* moduleName = | |
| 582 PyObject_GetAttrString(t, "__module__"); | |
| 583 | |
| 584 if (moduleName == NULL) | |
| 585 PyFile_WriteString("<unknown>", f); | |
| 586 else { | |
| 587 char* modstr = PyString_AsString(moduleName); | |
| 588 if (modstr) | |
| 589 { | |
| 590 PyFile_WriteString(modstr, f); | |
| 591 PyFile_WriteString(".", f); | |
| 592 } | |
| 593 } | |
| 594 if (className == NULL) | |
| 595 PyFile_WriteString("<unknown>", f); | |
| 596 else | |
| 597 PyFile_WriteString(className, f); | |
| 578 if (v && v != Py_None) { | 598 if (v && v != Py_None) { |
| 579 PyFile_WriteString(": ", f); | 599 PyFile_WriteString(": ", f); |
| 580 PyFile_WriteObject(v, f, 0); | 600 PyFile_WriteObject(v, f, 0); |
| 581 } | 601 } |
| 582 } | 602 } |
| 724 PyErr_Restore(exc, v, tb); | 744 PyErr_Restore(exc, v, tb); |
| 725 } | 745 } |
| 726 | 746 |
| 727 /* com_fetch_program_text will attempt to load the line of text that | 747 /* com_fetch_program_text will attempt to load the line of text that |
| 728 the exception refers to. If it fails, it will return NULL but will | 748 the exception refers to. If it fails, it will return NULL but will |
| 729 not set an exception. | 749 not set an exception. |
| 730 | 750 |
| 731 XXX The functionality of this function is quite similar to the | 751 XXX The functionality of this function is quite similar to the |
| 732 functionality in tb_displayline() in traceback.c. | 752 functionality in tb_displayline() in traceback.c. |
| 733 */ | 753 */ |
| 734 | 754 |
