Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(1313)

Delta Between Two Patch Sets: Objects/exceptions.c

Issue 1559549: ImportError needs attributes for module and file name
Left Patch Set: Created 2 years, 2 months ago
Right Patch Set: Created 1 year, 3 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/test/test_pydoc.py ('k') | Python/errors.c » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /* 1 /*
2 * New exceptions.c written in Iceland by Richard Jones and Georg Brandl. 2 * New exceptions.c written in Iceland by Richard Jones and Georg Brandl.
3 * 3 *
4 * Thanks go to Tim Peters and Michael Hudson for debugging. 4 * Thanks go to Tim Peters and Michael Hudson for debugging.
5 */ 5 */
6 6
7 #define PY_SSIZE_T_CLEAN 7 #define PY_SSIZE_T_CLEAN
8 #include <Python.h> 8 #include <Python.h>
9 #include "structmember.h" 9 #include "structmember.h"
10 #include "osdefs.h" 10 #include "osdefs.h"
11
12
13 /* Compatibility aliases */
14 PyObject *PyExc_EnvironmentError = NULL;
15 PyObject *PyExc_IOError = NULL;
16 #ifdef MS_WINDOWS
17 PyObject *PyExc_WindowsError = NULL;
18 #endif
19 #ifdef __VMS
20 PyObject *PyExc_VMSError = NULL;
21 #endif
22
23 /* The dict map from errno codes to OSError subclasses */
24 static PyObject *errnomap = NULL;
11 25
12 26
13 /* NOTE: If the exception class hierarchy changes, don't forget to update 27 /* NOTE: If the exception class hierarchy changes, don't forget to update
14 * Lib/test/exception_hierarchy.txt 28 * Lib/test/exception_hierarchy.txt
15 */ 29 */
16 30
17 /* 31 /*
18 * BaseException 32 * BaseException
19 */ 33 */
20 static PyObject * 34 static PyObject *
(...skipping 16 matching lines...) Expand all
37 51
38 return (PyObject *)self; 52 return (PyObject *)self;
39 } 53 }
40 54
41 static int 55 static int
42 BaseException_init(PyBaseExceptionObject *self, PyObject *args, PyObject *kwds) 56 BaseException_init(PyBaseExceptionObject *self, PyObject *args, PyObject *kwds)
43 { 57 {
44 if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds)) 58 if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds))
45 return -1; 59 return -1;
46 60
47 Py_DECREF(self->args); 61 Py_XDECREF(self->args);
48 self->args = args; 62 self->args = args;
49 Py_INCREF(self->args); 63 Py_INCREF(self->args);
50 64
51 return 0; 65 return 0;
52 } 66 }
53 67
54 static int 68 static int
55 BaseException_clear(PyBaseExceptionObject *self) 69 BaseException_clear(PyBaseExceptionObject *self)
56 { 70 {
57 Py_CLEAR(self->dict); 71 Py_CLEAR(self->dict);
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 220
207 static int 221 static int
208 BaseException_set_args(PyBaseExceptionObject *self, PyObject *val) 222 BaseException_set_args(PyBaseExceptionObject *self, PyObject *val)
209 { 223 {
210 PyObject *seq; 224 PyObject *seq;
211 if (val == NULL) { 225 if (val == NULL) {
212 PyErr_SetString(PyExc_TypeError, "args may not be deleted"); 226 PyErr_SetString(PyExc_TypeError, "args may not be deleted");
213 return -1; 227 return -1;
214 } 228 }
215 seq = PySequence_Tuple(val); 229 seq = PySequence_Tuple(val);
216 if (!seq) return -1; 230 if (!seq)
231 return -1;
217 Py_CLEAR(self->args); 232 Py_CLEAR(self->args);
218 self->args = seq; 233 self->args = seq;
219 return 0; 234 return 0;
220 } 235 }
221 236
222 static PyObject * 237 static PyObject *
223 BaseException_get_tb(PyBaseExceptionObject *self) 238 BaseException_get_tb(PyBaseExceptionObject *self)
224 { 239 {
225 if (self->traceback == NULL) { 240 if (self->traceback == NULL) {
226 Py_INCREF(Py_None); 241 Py_INCREF(Py_None);
(...skipping 18 matching lines...) Expand all
245 260
246 Py_XINCREF(tb); 261 Py_XINCREF(tb);
247 Py_XDECREF(self->traceback); 262 Py_XDECREF(self->traceback);
248 self->traceback = tb; 263 self->traceback = tb;
249 return 0; 264 return 0;
250 } 265 }
251 266
252 static PyObject * 267 static PyObject *
253 BaseException_get_context(PyObject *self) { 268 BaseException_get_context(PyObject *self) {
254 PyObject *res = PyException_GetContext(self); 269 PyObject *res = PyException_GetContext(self);
255 if (res) return res; /* new reference already returned above */ 270 if (res)
271 return res; /* new reference already returned above */
256 Py_RETURN_NONE; 272 Py_RETURN_NONE;
257 } 273 }
258 274
259 static int 275 static int
260 BaseException_set_context(PyObject *self, PyObject *arg) { 276 BaseException_set_context(PyObject *self, PyObject *arg) {
261 if (arg == NULL) { 277 if (arg == NULL) {
262 PyErr_SetString(PyExc_TypeError, "__context__ may not be deleted"); 278 PyErr_SetString(PyExc_TypeError, "__context__ may not be deleted");
263 return -1; 279 return -1;
264 } else if (arg == Py_None) { 280 } else if (arg == Py_None) {
265 arg = NULL; 281 arg = NULL;
266 } else if (!PyExceptionInstance_Check(arg)) { 282 } else if (!PyExceptionInstance_Check(arg)) {
267 PyErr_SetString(PyExc_TypeError, "exception context must be None " 283 PyErr_SetString(PyExc_TypeError, "exception context must be None "
268 "or derive from BaseException"); 284 "or derive from BaseException");
269 return -1; 285 return -1;
270 } else { 286 } else {
271 /* PyException_SetContext steals this reference */ 287 /* PyException_SetContext steals this reference */
272 Py_INCREF(arg); 288 Py_INCREF(arg);
273 } 289 }
274 PyException_SetContext(self, arg); 290 PyException_SetContext(self, arg);
275 return 0; 291 return 0;
276 } 292 }
277 293
278 static PyObject * 294 static PyObject *
279 BaseException_get_cause(PyObject *self) { 295 BaseException_get_cause(PyObject *self) {
280 PyObject *res = PyException_GetCause(self); 296 PyObject *res = PyException_GetCause(self);
281 if (res) return res; /* new reference already returned above */ 297 if (res)
298 return res; /* new reference already returned above */
282 Py_RETURN_NONE; 299 Py_RETURN_NONE;
283 } 300 }
284 301
285 static int 302 static int
286 BaseException_set_cause(PyObject *self, PyObject *arg) { 303 BaseException_set_cause(PyObject *self, PyObject *arg) {
287 if (arg == NULL) { 304 if (arg == NULL) {
288 PyErr_SetString(PyExc_TypeError, "__cause__ may not be deleted"); 305 PyErr_SetString(PyExc_TypeError, "__cause__ may not be deleted");
289 return -1; 306 return -1;
290 } else if (arg == Py_None) { 307 } else if (arg == Py_None) {
291 arg = NULL; 308 arg = NULL;
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
426 static PyTypeObject _PyExc_ ## EXCNAME = { \ 443 static PyTypeObject _PyExc_ ## EXCNAME = { \
427 PyVarObject_HEAD_INIT(NULL, 0) \ 444 PyVarObject_HEAD_INIT(NULL, 0) \
428 # EXCNAME, \ 445 # EXCNAME, \
429 sizeof(Py ## EXCSTORE ## Object), \ 446 sizeof(Py ## EXCSTORE ## Object), \
430 0, (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, \ 447 0, (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
431 0, 0, 0, 0, 0, \ 448 0, 0, 0, 0, 0, \
432 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \ 449 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
433 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \ 450 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
434 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \ 451 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
435 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \ 452 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
436 (initproc)EXCSTORE ## _init, 0, BaseException_new,\ 453 (initproc)EXCSTORE ## _init, 0, 0, \
437 }; \ 454 }; \
438 PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME 455 PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
439 456
440 #define ComplexExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDEALLOC, EXCMETHO DS, EXCMEMBERS, EXCSTR, EXCDOC) \ 457 #define ComplexExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCNEW, \
458 EXCMETHODS, EXCMEMBERS, EXCGETSET, \
459 EXCSTR, EXCDOC) \
441 static PyTypeObject _PyExc_ ## EXCNAME = { \ 460 static PyTypeObject _PyExc_ ## EXCNAME = { \
442 PyVarObject_HEAD_INIT(NULL, 0) \ 461 PyVarObject_HEAD_INIT(NULL, 0) \
443 # EXCNAME, \ 462 # EXCNAME, \
444 sizeof(Py ## EXCSTORE ## Object), 0, \ 463 sizeof(Py ## EXCSTORE ## Object), 0, \
445 (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \ 464 (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
446 (reprfunc)EXCSTR, 0, 0, 0, \ 465 (reprfunc)EXCSTR, 0, 0, 0, \
447 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \ 466 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
448 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \ 467 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
449 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, EXCMETHODS, \ 468 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, EXCMETHODS, \
450 EXCMEMBERS, 0, &_ ## EXCBASE, \ 469 EXCMEMBERS, EXCGETSET, &_ ## EXCBASE, \
451 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \ 470 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
452 (initproc)EXCSTORE ## _init, 0, BaseException_new,\ 471 (initproc)EXCSTORE ## _init, 0, EXCNEW,\
453 }; \ 472 }; \
454 PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME 473 PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
455 474
456 475
457 /* 476 /*
458 * Exception extends BaseException 477 * Exception extends BaseException
459 */ 478 */
460 SimpleExtendsException(PyExc_BaseException, Exception, 479 SimpleExtendsException(PyExc_BaseException, Exception,
461 "Common base class for all non-exit exceptions."); 480 "Common base class for all non-exit exceptions.");
462 481
463 482
464 /* 483 /*
465 * TypeError extends Exception 484 * TypeError extends Exception
466 */ 485 */
467 SimpleExtendsException(PyExc_Exception, TypeError, 486 SimpleExtendsException(PyExc_Exception, TypeError,
468 "Inappropriate argument type."); 487 "Inappropriate argument type.");
469 488
470 489
471 /* 490 /*
472 * StopIteration extends Exception 491 * StopIteration extends Exception
473 */ 492 */
474 SimpleExtendsException(PyExc_Exception, StopIteration, 493
475 "Signal the end from iterator.__next__()."); 494 static PyMemberDef StopIteration_members[] = {
495 {"value", T_OBJECT, offsetof(PyStopIterationObject, value), 0,
496 PyDoc_STR("generator return value")},
497 {NULL} /* Sentinel */
498 };
499
500 static int
501 StopIteration_init(PyStopIterationObject *self, PyObject *args, PyObject *kwds)
502 {
503 Py_ssize_t size = PyTuple_GET_SIZE(args);
504 PyObject *value;
505
506 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
507 return -1;
508 Py_CLEAR(self->value);
509 if (size > 0)
510 value = PyTuple_GET_ITEM(args, 0);
511 else
512 value = Py_None;
513 Py_INCREF(value);
514 self->value = value;
515 return 0;
516 }
517
518 static int
519 StopIteration_clear(PyStopIterationObject *self)
520 {
521 Py_CLEAR(self->value);
522 return BaseException_clear((PyBaseExceptionObject *)self);
523 }
524
525 static void
526 StopIteration_dealloc(PyStopIterationObject *self)
527 {
528 _PyObject_GC_UNTRACK(self);
529 StopIteration_clear(self);
530 Py_TYPE(self)->tp_free((PyObject *)self);
531 }
532
533 static int
534 StopIteration_traverse(PyStopIterationObject *self, visitproc visit, void *arg)
535 {
536 Py_VISIT(self->value);
537 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
538 }
539
540 PyObject *
541 PyStopIteration_Create(PyObject *value)
542 {
543 return PyObject_CallFunctionObjArgs(PyExc_StopIteration, value, NULL);
544 }
545
546 ComplexExtendsException(
547 PyExc_Exception, /* base */
548 StopIteration, /* name */
549 StopIteration, /* prefix for *_init, etc */
550 0, /* new */
551 0, /* methods */
552 StopIteration_members, /* members */
553 0, /* getset */
554 0, /* str */
555 "Signal the end from iterator.__next__()."
556 );
476 557
477 558
478 /* 559 /*
479 * GeneratorExit extends BaseException 560 * GeneratorExit extends BaseException
480 */ 561 */
481 SimpleExtendsException(PyExc_BaseException, GeneratorExit, 562 SimpleExtendsException(PyExc_BaseException, GeneratorExit,
482 "Request that a generator exit."); 563 "Request that a generator exit.");
483 564
484 565
485 /* 566 /*
486 * SystemExit extends BaseException 567 * SystemExit extends BaseException
487 */ 568 */
488 569
489 static int 570 static int
490 SystemExit_init(PySystemExitObject *self, PyObject *args, PyObject *kwds) 571 SystemExit_init(PySystemExitObject *self, PyObject *args, PyObject *kwds)
491 { 572 {
492 Py_ssize_t size = PyTuple_GET_SIZE(args); 573 Py_ssize_t size = PyTuple_GET_SIZE(args);
493 574
494 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1) 575 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
495 return -1; 576 return -1;
496 577
497 if (size == 0) 578 if (size == 0)
498 return 0; 579 return 0;
499 Py_CLEAR(self->code); 580 Py_CLEAR(self->code);
500 if (size == 1) 581 if (size == 1)
501 self->code = PyTuple_GET_ITEM(args, 0); 582 self->code = PyTuple_GET_ITEM(args, 0);
502 else if (size > 1) 583 else /* size > 1 */
503 self->code = args; 584 self->code = args;
504 Py_INCREF(self->code); 585 Py_INCREF(self->code);
505 return 0; 586 return 0;
506 } 587 }
507 588
508 static int 589 static int
509 SystemExit_clear(PySystemExitObject *self) 590 SystemExit_clear(PySystemExitObject *self)
510 { 591 {
511 Py_CLEAR(self->code); 592 Py_CLEAR(self->code);
512 return BaseException_clear((PyBaseExceptionObject *)self); 593 return BaseException_clear((PyBaseExceptionObject *)self);
(...skipping 14 matching lines...) Expand all
527 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg); 608 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
528 } 609 }
529 610
530 static PyMemberDef SystemExit_members[] = { 611 static PyMemberDef SystemExit_members[] = {
531 {"code", T_OBJECT, offsetof(PySystemExitObject, code), 0, 612 {"code", T_OBJECT, offsetof(PySystemExitObject, code), 0,
532 PyDoc_STR("exception code")}, 613 PyDoc_STR("exception code")},
533 {NULL} /* Sentinel */ 614 {NULL} /* Sentinel */
534 }; 615 };
535 616
536 ComplexExtendsException(PyExc_BaseException, SystemExit, SystemExit, 617 ComplexExtendsException(PyExc_BaseException, SystemExit, SystemExit,
537 SystemExit_dealloc, 0, SystemExit_members, 0, 618 0, 0, SystemExit_members, 0, 0,
538 "Request to exit from the interpreter."); 619 "Request to exit from the interpreter.");
539 620
540 /* 621 /*
541 * KeyboardInterrupt extends BaseException 622 * KeyboardInterrupt extends BaseException
542 */ 623 */
543 SimpleExtendsException(PyExc_BaseException, KeyboardInterrupt, 624 SimpleExtendsException(PyExc_BaseException, KeyboardInterrupt,
544 "Program interrupted by user."); 625 "Program interrupted by user.");
545 626
546 627
547 /* 628 /*
548 * ImportError extends Exception 629 * ImportError extends Exception
549 */ 630 */
550 631
551 static int 632 static int
552 ImportError_init(PyImportErrorObject *self, PyObject *args, PyObject *kwds) 633 ImportError_init(PyImportErrorObject *self, PyObject *args, PyObject *kwds)
553 { 634 {
554 PyObject *msg = NULL; 635 PyObject *msg = NULL;
555 PyObject *module_name = NULL; 636 PyObject *name = NULL;
556 637 PyObject *path = NULL;
557 if (kwds){ 638
558 module_name = PyDict_GetItemString(kwds, "module_name"); 639 /* Macro replacement doesn't allow ## to start the first line of a macro,
559 if (module_name){ 640 so we move the assignment and NULL check into the if-statement. */
560 Py_CLEAR(self->module_name); /* replacing */ 641 #define GET_KWD(kwd) { \
561 self->module_name = module_name; 642 kwd = PyDict_GetItemString(kwds, #kwd); \
562 Py_INCREF(self->module_name); 643 if (kwd) { \
563 if (PyDict_DelItemString(kwds, "module_name")) 644 Py_CLEAR(self->kwd); \
564 return -1; 645 self->kwd = kwd; \
565 } 646 Py_INCREF(self->kwd);\
566 } 647 if (PyDict_DelItemString(kwds, #kwd)) \
567 648 return -1; \
649 } \
650 }
651
652 if (kwds) {
653 GET_KWD(name);
654 GET_KWD(path);
655 }
656
568 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1) 657 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
569 return -1; 658 return -1;
570 if (PyTuple_GET_SIZE(args) != 1) 659 if (PyTuple_GET_SIZE(args) != 1)
571 return 0; 660 return 0;
572 if (!PyArg_UnpackTuple(args, "ImportError", 1, 1, &msg)) 661 if (!PyArg_UnpackTuple(args, "ImportError", 1, 1, &msg))
573 return -1; 662 return -1;
574 663
575 Py_CLEAR(self->msg); /* replacing */ 664 Py_CLEAR(self->msg); /* replacing */
576 self->msg = msg; 665 self->msg = msg;
577 Py_INCREF(self->msg); 666 Py_INCREF(self->msg);
578 667
579 return 0; 668 return 0;
580 } 669 }
581 670
582 static int 671 static int
583 ImportError_clear(PyImportErrorObject *self) 672 ImportError_clear(PyImportErrorObject *self)
584 { 673 {
585 Py_CLEAR(self->msg); 674 Py_CLEAR(self->msg);
586 Py_CLEAR(self->module_name); 675 Py_CLEAR(self->name);
676 Py_CLEAR(self->path);
587 return BaseException_clear((PyBaseExceptionObject *)self); 677 return BaseException_clear((PyBaseExceptionObject *)self);
588 } 678 }
589 679
590 static void 680 static void
591 ImportError_dealloc(PyImportErrorObject *self) 681 ImportError_dealloc(PyImportErrorObject *self)
592 { 682 {
593 _PyObject_GC_UNTRACK(self); 683 _PyObject_GC_UNTRACK(self);
594 ImportError_clear(self); 684 ImportError_clear(self);
595 Py_TYPE(self)->tp_free((PyObject *)self); 685 Py_TYPE(self)->tp_free((PyObject *)self);
596 } 686 }
597 687
598 static int 688 static int
599 ImportError_traverse(PyImportErrorObject *self, visitproc visit, void *arg) 689 ImportError_traverse(PyImportErrorObject *self, visitproc visit, void *arg)
600 { 690 {
601 Py_VISIT(self->msg); 691 Py_VISIT(self->msg);
602 Py_VISIT(self->module_name); 692 Py_VISIT(self->name);
693 Py_VISIT(self->path);
603 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg); 694 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
604 } 695 }
605 696
606 static PyObject * 697 static PyObject *
607 ImportError_str(PyImportErrorObject *self) 698 ImportError_str(PyImportErrorObject *self)
608 { 699 {
609 if (self->msg) { 700 if (self->msg) {
610 Py_INCREF(self->msg); 701 Py_INCREF(self->msg);
611 return self->msg; 702 return self->msg;
612 } 703 }
613 else 704 else {
614 return BaseException_str((PyBaseExceptionObject *)self); 705 return BaseException_str((PyBaseExceptionObject *)self);
706 }
615 } 707 }
616 708
617 static PyMemberDef ImportError_members[] = { 709 static PyMemberDef ImportError_members[] = {
618 {"msg", T_OBJECT, offsetof(PyImportErrorObject, msg), 0, 710 {"msg", T_OBJECT, offsetof(PyImportErrorObject, msg), 0,
619 PyDoc_STR("exception message")}, 711 PyDoc_STR("exception message")},
620 {"module_name", T_OBJECT, offsetof(PyImportErrorObject, module_name), 0, 712 {"name", T_OBJECT, offsetof(PyImportErrorObject, name), 0,
621 PyDoc_STR("exception module_name")}, 713 PyDoc_STR("module name")},
714 {"path", T_OBJECT, offsetof(PyImportErrorObject, path), 0,
715 PyDoc_STR("module path")},
622 {NULL} /* Sentinel */ 716 {NULL} /* Sentinel */
623 }; 717 };
624 718
625 static PyMethodDef ImportError_methods[] = { 719 static PyMethodDef ImportError_methods[] = {
626 {NULL} 720 {NULL}
627 }; 721 };
628 722
629 ComplexExtendsException(PyExc_Exception, ImportError, 723 ComplexExtendsException(PyExc_Exception, ImportError,
630 ImportError, ImportError_dealloc, 724 ImportError, 0 /* new */,
631 ImportError_methods, ImportError_members, 725 ImportError_methods, ImportError_members,
632 ImportError_str, 726 0 /* getset */, ImportError_str,
633 "Import can't find module, or can't find name in " 727 "Import can't find module, or can't find name in "
634 "module."); 728 "module.");
635 729
636 /* 730 /*
637 * EnvironmentError extends Exception 731 * OSError extends Exception
638 */ 732 */
733
734 #ifdef MS_WINDOWS
735 #include "errmap.h"
736 #endif
639 737
640 /* Where a function has a single filename, such as open() or some 738 /* Where a function has a single filename, such as open() or some
641 * of the os module functions, PyErr_SetFromErrnoWithFilename() is 739 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
642 * called, giving a third argument which is the filename. But, so 740 * called, giving a third argument which is the filename. But, so
643 * that old code using in-place unpacking doesn't break, e.g.: 741 * that old code using in-place unpacking doesn't break, e.g.:
644 * 742 *
645 * except IOError, (errno, strerror): 743 * except OSError, (errno, strerror):
646 * 744 *
647 * we hack args so that it only contains two items. This also 745 * we hack args so that it only contains two items. This also
648 * means we need our own __str__() which prints out the filename 746 * means we need our own __str__() which prints out the filename
649 * when it was supplied. 747 * when it was supplied.
650 */ 748 */
651 static int 749
652 EnvironmentError_init(PyEnvironmentErrorObject *self, PyObject *args, 750 /* This function doesn't cleanup on error, the caller should */
653 PyObject *kwds) 751 static int
654 { 752 oserror_parse_args(PyObject **p_args,
753 PyObject **myerrno, PyObject **strerror,
754 PyObject **filename
755 #ifdef MS_WINDOWS
756 , PyObject **winerror
757 #endif
758 )
759 {
760 Py_ssize_t nargs;
761 PyObject *args = *p_args;
762
763 nargs = PyTuple_GET_SIZE(args);
764
765 #ifdef MS_WINDOWS
766 if (nargs >= 2 && nargs <= 4) {
767 if (!PyArg_UnpackTuple(args, "OSError", 2, 4,
768 myerrno, strerror, filename, winerror))
769 return -1;
770 if (*winerror && PyLong_Check(*winerror)) {
771 long errcode, winerrcode;
772 PyObject *newargs;
773 Py_ssize_t i;
774
775 winerrcode = PyLong_AsLong(*winerror);
776 if (winerrcode == -1 && PyErr_Occurred())
777 return -1;
778 /* Set errno to the corresponding POSIX errno (overriding
779 first argument). Windows Socket error codes (>= 10000)
780 have the same value as their POSIX counterparts.
781 */
782 if (winerrcode < 10000)
783 errcode = winerror_to_errno(winerrcode);
784 else
785 errcode = winerrcode;
786 *myerrno = PyLong_FromLong(errcode);
787 if (!*myerrno)
788 return -1;
789 newargs = PyTuple_New(nargs);
790 if (!newargs)
791 return -1;
792 PyTuple_SET_ITEM(newargs, 0, *myerrno);
793 for (i = 1; i < nargs; i++) {
794 PyObject *val = PyTuple_GET_ITEM(args, i);
795 Py_INCREF(val);
796 PyTuple_SET_ITEM(newargs, i, val);
797 }
798 Py_DECREF(args);
799 args = *p_args = newargs;
800 }
801 }
802 #else
803 if (nargs >= 2 && nargs <= 3) {
804 if (!PyArg_UnpackTuple(args, "OSError", 2, 3,
805 myerrno, strerror, filename))
806 return -1;
807 }
808 #endif
809
810 return 0;
811 }
812
813 static int
814 oserror_init(PyOSErrorObject *self, PyObject **p_args,
815 PyObject *myerrno, PyObject *strerror,
816 PyObject *filename
817 #ifdef MS_WINDOWS
818 , PyObject *winerror
819 #endif
820 )
821 {
822 PyObject *args = *p_args;
823 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
824
825 /* self->filename will remain Py_None otherwise */
826 if (filename && filename != Py_None) {
827 if (Py_TYPE(self) == (PyTypeObject *) PyExc_BlockingIOError &&
828 PyNumber_Check(filename)) {
829 /* BlockingIOError's 3rd argument can be the number of
830 * characters written.
831 */
832 self->written = PyNumber_AsSsize_t(filename, PyExc_ValueError);
833 if (self->written == -1 && PyErr_Occurred())
834 return -1;
835 }
836 else {
837 Py_INCREF(filename);
838 self->filename = filename;
839
840 if (nargs >= 2 && nargs <= 3) {
841 /* filename is removed from the args tuple (for compatibility
842 purposes, see test_exceptions.py) */
843 PyObject *subslice = PyTuple_GetSlice(args, 0, 2);
844 if (!subslice)
845 return -1;
846
847 Py_DECREF(args); /* replacing args */
848 *p_args = args = subslice;
849 }
850 }
851 }
852 Py_XINCREF(myerrno);
853 self->myerrno = myerrno;
854
855 Py_XINCREF(strerror);
856 self->strerror = strerror;
857
858 #ifdef MS_WINDOWS
859 Py_XINCREF(winerror);
860 self->winerror = winerror;
861 #endif
862
863 /* Steals the reference to args */
864 self->args = args;
865 args = NULL;
866
867 return 0;
868 }
869
870 static PyObject *
871 OSError_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
872 static int
873 OSError_init(PyOSErrorObject *self, PyObject *args, PyObject *kwds);
874
875 static int
876 oserror_use_init(PyTypeObject *type)
877 {
878 /* When __init__ is defined in a OSError subclass, we want any
879 extraneous argument to __new__ to be ignored. The only reasonable
880 solution, given __new__ takes a variable number of arguments,
881 is to defer arg parsing and initialization to __init__.
882
883 But when __new__ is overriden as well, it should call our __new__
884 with the right arguments.
885
886 (see http://bugs.python.org/issue12555#msg148829 )
887 */
888 if (type->tp_init != (initproc) OSError_init &&
889 type->tp_new == (newfunc) OSError_new) {
890 assert((PyObject *) type != PyExc_OSError);
891 return 1;
892 }
893 return 0;
894 }
895
896 static PyObject *
897 OSError_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
898 {
899 PyOSErrorObject *self = NULL;
655 PyObject *myerrno = NULL, *strerror = NULL, *filename = NULL; 900 PyObject *myerrno = NULL, *strerror = NULL, *filename = NULL;
656 PyObject *subslice = NULL; 901 #ifdef MS_WINDOWS
657 902 PyObject *winerror = NULL;
658 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1) 903 #endif
659 return -1; 904
660 905 if (!oserror_use_init(type)) {
661 if (PyTuple_GET_SIZE(args) <= 1 || PyTuple_GET_SIZE(args) > 3) { 906 if (!_PyArg_NoKeywords(type->tp_name, kwds))
907 return NULL;
908
909 Py_INCREF(args);
910 if (oserror_parse_args(&args, &myerrno, &strerror, &filename
911 #ifdef MS_WINDOWS
912 , &winerror
913 #endif
914 ))
915 goto error;
916
917 if (myerrno && PyLong_Check(myerrno) &&
918 errnomap && (PyObject *) type == PyExc_OSError) {
919 PyObject *newtype;
920 newtype = PyDict_GetItem(errnomap, myerrno);
921 if (newtype) {
922 assert(PyType_Check(newtype));
923 type = (PyTypeObject *) newtype;
924 }
925 else if (PyErr_Occurred())
926 goto error;
927 }
928 }
929
930 self = (PyOSErrorObject *) type->tp_alloc(type, 0);
931 if (!self)
932 goto error;
933
934 self->dict = NULL;
935 self->traceback = self->cause = self->context = NULL;
936 self->written = -1;
937
938 if (!oserror_use_init(type)) {
939 if (oserror_init(self, &args, myerrno, strerror, filename
940 #ifdef MS_WINDOWS
941 , winerror
942 #endif
943 ))
944 goto error;
945 }
946
947 return (PyObject *) self;
948
949 error:
950 Py_XDECREF(args);
951 Py_XDECREF(self);
952 return NULL;
953 }
954
955 static int
956 OSError_init(PyOSErrorObject *self, PyObject *args, PyObject *kwds)
957 {
958 PyObject *myerrno = NULL, *strerror = NULL, *filename = NULL;
959 #ifdef MS_WINDOWS
960 PyObject *winerror = NULL;
961 #endif
962
963 if (!oserror_use_init(Py_TYPE(self)))
964 /* Everything already done in OSError_new */
662 return 0; 965 return 0;
663 } 966
664 967 if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds))
665 if (!PyArg_UnpackTuple(args, "EnvironmentError", 2, 3, 968 return -1;
666 &myerrno, &strerror, &filename)) { 969
667 return -1; 970 Py_INCREF(args);
668 } 971 if (oserror_parse_args(&args, &myerrno, &strerror, &filename
669 Py_CLEAR(self->myerrno); /* replacing */ 972 #ifdef MS_WINDOWS
670 self->myerrno = myerrno; 973 , &winerror
671 Py_INCREF(self->myerrno); 974 #endif
672 975 ))
673 Py_CLEAR(self->strerror); /* replacing */ 976 goto error;
674 self->strerror = strerror; 977
675 Py_INCREF(self->strerror); 978 if (oserror_init(self, &args, myerrno, strerror, filename
676 979 #ifdef MS_WINDOWS
677 /* self->filename will remain Py_None otherwise */ 980 , winerror
678 if (filename != NULL) { 981 #endif
679 Py_CLEAR(self->filename); /* replacing */ 982 ))
680 self->filename = filename; 983 goto error;
681 Py_INCREF(self->filename); 984
682 985 return 0;
683 subslice = PyTuple_GetSlice(args, 0, 2); 986
684 if (!subslice) 987 error:
685 return -1; 988 Py_XDECREF(args);
686 989 return -1;
687 Py_DECREF(self->args); /* replacing args */ 990 }
688 self->args = subslice; 991
689 } 992 static int
690 return 0; 993 OSError_clear(PyOSErrorObject *self)
691 }
692
693 static int
694 EnvironmentError_clear(PyEnvironmentErrorObject *self)
695 { 994 {
696 Py_CLEAR(self->myerrno); 995 Py_CLEAR(self->myerrno);
697 Py_CLEAR(self->strerror); 996 Py_CLEAR(self->strerror);
698 Py_CLEAR(self->filename); 997 Py_CLEAR(self->filename);
998 #ifdef MS_WINDOWS
999 Py_CLEAR(self->winerror);
1000 #endif
699 return BaseException_clear((PyBaseExceptionObject *)self); 1001 return BaseException_clear((PyBaseExceptionObject *)self);
700 } 1002 }
701 1003
702 static void 1004 static void
703 EnvironmentError_dealloc(PyEnvironmentErrorObject *self) 1005 OSError_dealloc(PyOSErrorObject *self)
704 { 1006 {
705 _PyObject_GC_UNTRACK(self); 1007 _PyObject_GC_UNTRACK(self);
706 EnvironmentError_clear(self); 1008 OSError_clear(self);
707 Py_TYPE(self)->tp_free((PyObject *)self); 1009 Py_TYPE(self)->tp_free((PyObject *)self);
708 } 1010 }
709 1011
710 static int 1012 static int
711 EnvironmentError_traverse(PyEnvironmentErrorObject *self, visitproc visit, 1013 OSError_traverse(PyOSErrorObject *self, visitproc visit,
712 void *arg) 1014 void *arg)
713 { 1015 {
714 Py_VISIT(self->myerrno); 1016 Py_VISIT(self->myerrno);
715 Py_VISIT(self->strerror); 1017 Py_VISIT(self->strerror);
716 Py_VISIT(self->filename); 1018 Py_VISIT(self->filename);
1019 #ifdef MS_WINDOWS
1020 Py_VISIT(self->winerror);
1021 #endif
717 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg); 1022 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
718 } 1023 }
719 1024
720 static PyObject * 1025 static PyObject *
721 EnvironmentError_str(PyEnvironmentErrorObject *self) 1026 OSError_str(PyOSErrorObject *self)
722 { 1027 {
1028 #ifdef MS_WINDOWS
1029 /* If available, winerror has the priority over myerrno */
1030 if (self->winerror && self->filename)
1031 return PyUnicode_FromFormat("[Error %S] %S: %R",
1032 self->winerror ? self->winerror: Py_None,
1033 self->strerror ? self->strerror: Py_None,
1034 self->filename);
1035 if (self->winerror && self->strerror)
1036 return PyUnicode_FromFormat("[Error %S] %S",
1037 self->winerror ? self->winerror: Py_None,
1038 self->strerror ? self->strerror: Py_None);
1039 #endif
723 if (self->filename) 1040 if (self->filename)
724 return PyUnicode_FromFormat("[Errno %S] %S: %R", 1041 return PyUnicode_FromFormat("[Errno %S] %S: %R",
725 self->myerrno ? self->myerrno: Py_None, 1042 self->myerrno ? self->myerrno: Py_None,
726 self->strerror ? self->strerror: Py_None, 1043 self->strerror ? self->strerror: Py_None,
727 self->filename); 1044 self->filename);
728 else if (self->myerrno && self->strerror) 1045 if (self->myerrno && self->strerror)
729 return PyUnicode_FromFormat("[Errno %S] %S", 1046 return PyUnicode_FromFormat("[Errno %S] %S",
730 self->myerrno ? self->myerrno: Py_None, 1047 self->myerrno ? self->myerrno: Py_None,
731 self->strerror ? self->strerror: Py_None); 1048 self->strerror ? self->strerror: Py_None);
732 else 1049 return BaseException_str((PyBaseExceptionObject *)self);
733 return BaseException_str((PyBaseExceptionObject *)self); 1050 }
734 }
735
736 static PyMemberDef EnvironmentError_members[] = {
737 {"errno", T_OBJECT, offsetof(PyEnvironmentErrorObject, myerrno), 0,
738 PyDoc_STR("exception errno")},
739 {"strerror", T_OBJECT, offsetof(PyEnvironmentErrorObject, strerror), 0,
740 PyDoc_STR("exception strerror")},
741 {"filename", T_OBJECT, offsetof(PyEnvironmentErrorObject, filename), 0,
742 PyDoc_STR("exception filename")},
743 {NULL} /* Sentinel */
744 };
745
746 1051
747 static PyObject * 1052 static PyObject *
748 EnvironmentError_reduce(PyEnvironmentErrorObject *self) 1053 OSError_reduce(PyOSErrorObject *self)
749 { 1054 {
750 PyObject *args = self->args; 1055 PyObject *args = self->args;
751 PyObject *res = NULL, *tmp; 1056 PyObject *res = NULL, *tmp;
752 1057
753 /* self->args is only the first two real arguments if there was a 1058 /* self->args is only the first two real arguments if there was a
754 * file name given to EnvironmentError. */ 1059 * file name given to OSError. */
755 if (PyTuple_GET_SIZE(args) == 2 && self->filename) { 1060 if (PyTuple_GET_SIZE(args) == 2 && self->filename) {
756 args = PyTuple_New(3); 1061 args = PyTuple_New(3);
757 if (!args) return NULL; 1062 if (!args)
1063 return NULL;
758 1064
759 tmp = PyTuple_GET_ITEM(self->args, 0); 1065 tmp = PyTuple_GET_ITEM(self->args, 0);
760 Py_INCREF(tmp); 1066 Py_INCREF(tmp);
761 PyTuple_SET_ITEM(args, 0, tmp); 1067 PyTuple_SET_ITEM(args, 0, tmp);
762 1068
763 tmp = PyTuple_GET_ITEM(self->args, 1); 1069 tmp = PyTuple_GET_ITEM(self->args, 1);
764 Py_INCREF(tmp); 1070 Py_INCREF(tmp);
765 PyTuple_SET_ITEM(args, 1, tmp); 1071 PyTuple_SET_ITEM(args, 1, tmp);
766 1072
767 Py_INCREF(self->filename); 1073 Py_INCREF(self->filename);
768 PyTuple_SET_ITEM(args, 2, self->filename); 1074 PyTuple_SET_ITEM(args, 2, self->filename);
769 } else 1075 } else
770 Py_INCREF(args); 1076 Py_INCREF(args);
771 1077
772 if (self->dict) 1078 if (self->dict)
773 res = PyTuple_Pack(3, Py_TYPE(self), args, self->dict); 1079 res = PyTuple_Pack(3, Py_TYPE(self), args, self->dict);
774 else 1080 else
775 res = PyTuple_Pack(2, Py_TYPE(self), args); 1081 res = PyTuple_Pack(2, Py_TYPE(self), args);
776 Py_DECREF(args); 1082 Py_DECREF(args);
777 return res; 1083 return res;
778 } 1084 }
779 1085
780 1086 static PyObject *
781 static PyMethodDef EnvironmentError_methods[] = { 1087 OSError_written_get(PyOSErrorObject *self, void *context)
782 {"__reduce__", (PyCFunction)EnvironmentError_reduce, METH_NOARGS}, 1088 {
1089 if (self->written == -1) {
1090 PyErr_SetString(PyExc_AttributeError, "characters_written");
1091 return NULL;
1092 }
1093 return PyLong_FromSsize_t(self->written);
1094 }
1095
1096 static int
1097 OSError_written_set(PyOSErrorObject *self, PyObject *arg, void *context)
1098 {
1099 Py_ssize_t n;
1100 n = PyNumber_AsSsize_t(arg, PyExc_ValueError);
1101 if (n == -1 && PyErr_Occurred())
1102 return -1;
1103 self->written = n;
1104 return 0;
1105 }
1106
1107 static PyMemberDef OSError_members[] = {
1108 {"errno", T_OBJECT, offsetof(PyOSErrorObject, myerrno), 0,
1109 PyDoc_STR("POSIX exception code")},
1110 {"strerror", T_OBJECT, offsetof(PyOSErrorObject, strerror), 0,
1111 PyDoc_STR("exception strerror")},
1112 {"filename", T_OBJECT, offsetof(PyOSErrorObject, filename), 0,
1113 PyDoc_STR("exception filename")},
1114 #ifdef MS_WINDOWS
1115 {"winerror", T_OBJECT, offsetof(PyOSErrorObject, winerror), 0,
1116 PyDoc_STR("Win32 exception code")},
1117 #endif
1118 {NULL} /* Sentinel */
1119 };
1120
1121 static PyMethodDef OSError_methods[] = {
1122 {"__reduce__", (PyCFunction)OSError_reduce, METH_NOARGS},
783 {NULL} 1123 {NULL}
784 }; 1124 };
785 1125
786 ComplexExtendsException(PyExc_Exception, EnvironmentError, 1126 static PyGetSetDef OSError_getset[] = {
787 EnvironmentError, EnvironmentError_dealloc, 1127 {"characters_written", (getter) OSError_written_get,
788 EnvironmentError_methods, EnvironmentError_members, 1128 (setter) OSError_written_set, NULL},
789 EnvironmentError_str, 1129 {NULL}
1130 };
1131
1132
1133 ComplexExtendsException(PyExc_Exception, OSError,
1134 OSError, OSError_new,
1135 OSError_methods, OSError_members, OSError_getset,
1136 OSError_str,
790 "Base class for I/O related errors."); 1137 "Base class for I/O related errors.");
791 1138
792 1139
793 /* 1140 /*
794 * IOError extends EnvironmentError 1141 * Various OSError subclasses
795 */ 1142 */
796 MiddlingExtendsException(PyExc_EnvironmentError, IOError, 1143 MiddlingExtendsException(PyExc_OSError, BlockingIOError, OSError,
797 EnvironmentError, "I/O operation failed."); 1144 "I/O operation would block.");
798 1145 MiddlingExtendsException(PyExc_OSError, ConnectionError, OSError,
799 1146 "Connection error.");
800 /* 1147 MiddlingExtendsException(PyExc_OSError, ChildProcessError, OSError,
801 * OSError extends EnvironmentError 1148 "Child process error.");
802 */ 1149 MiddlingExtendsException(PyExc_ConnectionError, BrokenPipeError, OSError,
803 MiddlingExtendsException(PyExc_EnvironmentError, OSError, 1150 "Broken pipe.");
804 EnvironmentError, "OS system call failed."); 1151 MiddlingExtendsException(PyExc_ConnectionError, ConnectionAbortedError, OSError,
805 1152 "Connection aborted.");
806 1153 MiddlingExtendsException(PyExc_ConnectionError, ConnectionRefusedError, OSError,
807 /* 1154 "Connection refused.");
808 * WindowsError extends OSError 1155 MiddlingExtendsException(PyExc_ConnectionError, ConnectionResetError, OSError,
809 */ 1156 "Connection reset.");
810 #ifdef MS_WINDOWS 1157 MiddlingExtendsException(PyExc_OSError, FileExistsError, OSError,
811 #include "errmap.h" 1158 "File already exists.");
812 1159 MiddlingExtendsException(PyExc_OSError, FileNotFoundError, OSError,
813 static int 1160 "File not found.");
814 WindowsError_clear(PyWindowsErrorObject *self) 1161 MiddlingExtendsException(PyExc_OSError, IsADirectoryError, OSError,
815 { 1162 "Operation doesn't work on directories.");
816 Py_CLEAR(self->myerrno); 1163 MiddlingExtendsException(PyExc_OSError, NotADirectoryError, OSError,
817 Py_CLEAR(self->strerror); 1164 "Operation only works on directories.");
818 Py_CLEAR(self->filename); 1165 MiddlingExtendsException(PyExc_OSError, InterruptedError, OSError,
819 Py_CLEAR(self->winerror); 1166 "Interrupted by signal.");
820 return BaseException_clear((PyBaseExceptionObject *)self); 1167 MiddlingExtendsException(PyExc_OSError, PermissionError, OSError,
821 } 1168 "Not enough permissions.");
822 1169 MiddlingExtendsException(PyExc_OSError, ProcessLookupError, OSError,
823 static void 1170 "Process not found.");
824 WindowsError_dealloc(PyWindowsErrorObject *self) 1171 MiddlingExtendsException(PyExc_OSError, TimeoutError, OSError,
825 { 1172 "Timeout expired.");
826 _PyObject_GC_UNTRACK(self);
827 WindowsError_clear(self);
828 Py_TYPE(self)->tp_free((PyObject *)self);
829 }
830
831 static int
832 WindowsError_traverse(PyWindowsErrorObject *self, visitproc visit, void *arg)
833 {
834 Py_VISIT(self->myerrno);
835 Py_VISIT(self->strerror);
836 Py_VISIT(self->filename);
837 Py_VISIT(self->winerror);
838 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
839 }
840
841 static int
842 WindowsError_init(PyWindowsErrorObject *self, PyObject *args, PyObject *kwds)
843 {
844 PyObject *o_errcode = NULL;
845 long errcode;
846 long posix_errno;
847
848 if (EnvironmentError_init((PyEnvironmentErrorObject *)self, args, kwds)
849 == -1)
850 return -1;
851
852 if (self->myerrno == NULL)
853 return 0;
854
855 /* Set errno to the POSIX errno, and winerror to the Win32
856 error code. */
857 errcode = PyLong_AsLong(self->myerrno);
858 if (errcode == -1 && PyErr_Occurred())
859 return -1;
860 posix_errno = winerror_to_errno(errcode);
861
862 Py_CLEAR(self->winerror);
863 self->winerror = self->myerrno;
864
865 o_errcode = PyLong_FromLong(posix_errno);
866 if (!o_errcode)
867 return -1;
868
869 self->myerrno = o_errcode;
870
871 return 0;
872 }
873
874
875 static PyObject *
876 WindowsError_str(PyWindowsErrorObject *self)
877 {
878 if (self->filename)
879 return PyUnicode_FromFormat("[Error %S] %S: %R",
880 self->winerror ? self->winerror: Py_None,
881 self->strerror ? self->strerror: Py_None,
882 self->filename);
883 else if (self->winerror && self->strerror)
884 return PyUnicode_FromFormat("[Error %S] %S",
885 self->winerror ? self->winerror: Py_None,
886 self->strerror ? self->strerror: Py_None);
887 else
888 return EnvironmentError_str((PyEnvironmentErrorObject *)self);
889 }
890
891 static PyMemberDef WindowsError_members[] = {
892 {"errno", T_OBJECT, offsetof(PyWindowsErrorObject, myerrno), 0,
893 PyDoc_STR("POSIX exception code")},
894 {"strerror", T_OBJECT, offsetof(PyWindowsErrorObject, strerror), 0,
895 PyDoc_STR("exception strerror")},
896 {"filename", T_OBJECT, offsetof(PyWindowsErrorObject, filename), 0,
897 PyDoc_STR("exception filename")},
898 {"winerror", T_OBJECT, offsetof(PyWindowsErrorObject, winerror), 0,
899 PyDoc_STR("Win32 exception code")},
900 {NULL} /* Sentinel */
901 };
902
903 ComplexExtendsException(PyExc_OSError, WindowsError, WindowsError,
904 WindowsError_dealloc, 0, WindowsError_members,
905 WindowsError_str, "MS-Windows OS system call failed.");
906
907 #endif /* MS_WINDOWS */
908
909
910 /*
911 * VMSError extends OSError (I think)
912 */
913 #ifdef __VMS
914 MiddlingExtendsException(PyExc_OSError, VMSError, EnvironmentError,
915 "OpenVMS OS system call failed.");
916 #endif
917
918 1173
919 /* 1174 /*
920 * EOFError extends Exception 1175 * EOFError extends Exception
921 */ 1176 */
922 SimpleExtendsException(PyExc_Exception, EOFError, 1177 SimpleExtendsException(PyExc_Exception, EOFError,
923 "Read beyond end of file."); 1178 "Read beyond end of file.");
924 1179
925 1180
926 /* 1181 /*
927 * RuntimeError extends Exception 1182 * RuntimeError extends Exception
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
969 return -1; 1224 return -1;
970 1225
971 if (lenargs >= 1) { 1226 if (lenargs >= 1) {
972 Py_CLEAR(self->msg); 1227 Py_CLEAR(self->msg);
973 self->msg = PyTuple_GET_ITEM(args, 0); 1228 self->msg = PyTuple_GET_ITEM(args, 0);
974 Py_INCREF(self->msg); 1229 Py_INCREF(self->msg);
975 } 1230 }
976 if (lenargs == 2) { 1231 if (lenargs == 2) {
977 info = PyTuple_GET_ITEM(args, 1); 1232 info = PyTuple_GET_ITEM(args, 1);
978 info = PySequence_Tuple(info); 1233 info = PySequence_Tuple(info);
979 if (!info) return -1; 1234 if (!info)
1235 return -1;
980 1236
981 if (PyTuple_GET_SIZE(info) != 4) { 1237 if (PyTuple_GET_SIZE(info) != 4) {
982 /* not a very good error message, but it's what Python 2.4 gives */ 1238 /* not a very good error message, but it's what Python 2.4 gives */
983 PyErr_SetString(PyExc_IndexError, "tuple index out of range"); 1239 PyErr_SetString(PyExc_IndexError, "tuple index out of range");
984 Py_DECREF(info); 1240 Py_DECREF(info);
985 return -1; 1241 return -1;
986 } 1242 }
987 1243
988 Py_CLEAR(self->filename); 1244 Py_CLEAR(self->filename);
989 self->filename = PyTuple_GET_ITEM(info, 0); 1245 self->filename = PyTuple_GET_ITEM(info, 0);
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
1037 Py_VISIT(self->print_file_and_line); 1293 Py_VISIT(self->print_file_and_line);
1038 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg); 1294 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1039 } 1295 }
1040 1296
1041 /* This is called "my_basename" instead of just "basename" to avoid name 1297 /* This is called "my_basename" instead of just "basename" to avoid name
1042 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is 1298 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
1043 defined, and Python does define that. */ 1299 defined, and Python does define that. */
1044 static PyObject* 1300 static PyObject*
1045 my_basename(PyObject *name) 1301 my_basename(PyObject *name)
1046 { 1302 {
1047 Py_UNICODE *unicode;
1048 Py_ssize_t i, size, offset; 1303 Py_ssize_t i, size, offset;
1049 1304 int kind;
1050 unicode = PyUnicode_AS_UNICODE(name); 1305 void *data;
1051 size = PyUnicode_GET_SIZE(name); 1306
1307 if (PyUnicode_READY(name))
1308 return NULL;
1309 kind = PyUnicode_KIND(name);
1310 data = PyUnicode_DATA(name);
1311 size = PyUnicode_GET_LENGTH(name);
1052 offset = 0; 1312 offset = 0;
1053 for(i=0; i < size; i++) { 1313 for(i=0; i < size; i++) {
1054 if (unicode[i] == SEP) 1314 if (PyUnicode_READ(kind, data, i) == SEP)
1055 offset = i + 1; 1315 offset = i + 1;
1056 } 1316 }
1057 if (offset != 0) { 1317 if (offset != 0)
1058 return PyUnicode_FromUnicode( 1318 return PyUnicode_Substring(name, offset, size);
1059 PyUnicode_AS_UNICODE(name) + offset, 1319 else {
1060 size - offset);
1061 } else {
1062 Py_INCREF(name); 1320 Py_INCREF(name);
1063 return name; 1321 return name;
1064 } 1322 }
1065 } 1323 }
1066 1324
1067 1325
1068 static PyObject * 1326 static PyObject *
1069 SyntaxError_str(PySyntaxErrorObject *self) 1327 SyntaxError_str(PySyntaxErrorObject *self)
1070 { 1328 {
1071 int have_lineno = 0; 1329 int have_lineno = 0;
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
1119 PyDoc_STR("exception offset")}, 1377 PyDoc_STR("exception offset")},
1120 {"text", T_OBJECT, offsetof(PySyntaxErrorObject, text), 0, 1378 {"text", T_OBJECT, offsetof(PySyntaxErrorObject, text), 0,
1121 PyDoc_STR("exception text")}, 1379 PyDoc_STR("exception text")},
1122 {"print_file_and_line", T_OBJECT, 1380 {"print_file_and_line", T_OBJECT,
1123 offsetof(PySyntaxErrorObject, print_file_and_line), 0, 1381 offsetof(PySyntaxErrorObject, print_file_and_line), 0,
1124 PyDoc_STR("exception print_file_and_line")}, 1382 PyDoc_STR("exception print_file_and_line")},
1125 {NULL} /* Sentinel */ 1383 {NULL} /* Sentinel */
1126 }; 1384 };
1127 1385
1128 ComplexExtendsException(PyExc_Exception, SyntaxError, SyntaxError, 1386 ComplexExtendsException(PyExc_Exception, SyntaxError, SyntaxError,
1129 SyntaxError_dealloc, 0, SyntaxError_members, 1387 0, 0, SyntaxError_members, 0,
1130 SyntaxError_str, "Invalid syntax."); 1388 SyntaxError_str, "Invalid syntax.");
1131 1389
1132 1390
1133 /* 1391 /*
1134 * IndentationError extends SyntaxError 1392 * IndentationError extends SyntaxError
1135 */ 1393 */
1136 MiddlingExtendsException(PyExc_SyntaxError, IndentationError, SyntaxError, 1394 MiddlingExtendsException(PyExc_SyntaxError, IndentationError, SyntaxError,
1137 "Improper indentation."); 1395 "Improper indentation.");
1138 1396
1139 1397
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
1173 string, that string will be displayed in quotes. Too bad. 1431 string, that string will be displayed in quotes. Too bad.
1174 If args is anything else, use the default BaseException__str__(). 1432 If args is anything else, use the default BaseException__str__().
1175 */ 1433 */
1176 if (PyTuple_GET_SIZE(self->args) == 1) { 1434 if (PyTuple_GET_SIZE(self->args) == 1) {
1177 return PyObject_Repr(PyTuple_GET_ITEM(self->args, 0)); 1435 return PyObject_Repr(PyTuple_GET_ITEM(self->args, 0));
1178 } 1436 }
1179 return BaseException_str(self); 1437 return BaseException_str(self);
1180 } 1438 }
1181 1439
1182 ComplexExtendsException(PyExc_LookupError, KeyError, BaseException, 1440 ComplexExtendsException(PyExc_LookupError, KeyError, BaseException,
1183 0, 0, 0, KeyError_str, "Mapping key not found."); 1441 0, 0, 0, 0, KeyError_str, "Mapping key not found.");
1184 1442
1185 1443
1186 /* 1444 /*
1187 * ValueError extends Exception 1445 * ValueError extends Exception
1188 */ 1446 */
1189 SimpleExtendsException(PyExc_Exception, ValueError, 1447 SimpleExtendsException(PyExc_Exception, ValueError,
1190 "Inappropriate argument value (of correct type)."); 1448 "Inappropriate argument value (of correct type).");
1191 1449
1192 /* 1450 /*
1193 * UnicodeError extends ValueError 1451 * UnicodeError extends ValueError
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
1272 1530
1273 int 1531 int
1274 PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start) 1532 PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1275 { 1533 {
1276 Py_ssize_t size; 1534 Py_ssize_t size;
1277 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object, 1535 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1278 "object"); 1536 "object");
1279 if (!obj) 1537 if (!obj)
1280 return -1; 1538 return -1;
1281 *start = ((PyUnicodeErrorObject *)exc)->start; 1539 *start = ((PyUnicodeErrorObject *)exc)->start;
1282 size = PyUnicode_GET_SIZE(obj); 1540 size = PyUnicode_GET_LENGTH(obj);
1283 if (*start<0) 1541 if (*start<0)
1284 *start = 0; /*XXX check for values <0*/ 1542 *start = 0; /*XXX check for values <0*/
1285 if (*start>=size) 1543 if (*start>=size)
1286 *start = size-1; 1544 *start = size-1;
1287 Py_DECREF(obj); 1545 Py_DECREF(obj);
1288 return 0; 1546 return 0;
1289 } 1547 }
1290 1548
1291 1549
1292 int 1550 int
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
1340 1598
1341 int 1599 int
1342 PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end) 1600 PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1343 { 1601 {
1344 Py_ssize_t size; 1602 Py_ssize_t size;
1345 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object, 1603 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1346 "object"); 1604 "object");
1347 if (!obj) 1605 if (!obj)
1348 return -1; 1606 return -1;
1349 *end = ((PyUnicodeErrorObject *)exc)->end; 1607 *end = ((PyUnicodeErrorObject *)exc)->end;
1350 size = PyUnicode_GET_SIZE(obj); 1608 size = PyUnicode_GET_LENGTH(obj);
1351 if (*end<1) 1609 if (*end<1)
1352 *end = 1; 1610 *end = 1;
1353 if (*end>size) 1611 if (*end>size)
1354 *end = size; 1612 *end = size;
1355 Py_DECREF(obj); 1613 Py_DECREF(obj);
1356 return 0; 1614 return 0;
1357 } 1615 }
1358 1616
1359 1617
1360 int 1618 int
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
1512 if (!PyArg_ParseTuple(args, "O!O!nnO!", 1770 if (!PyArg_ParseTuple(args, "O!O!nnO!",
1513 &PyUnicode_Type, &err->encoding, 1771 &PyUnicode_Type, &err->encoding,
1514 &PyUnicode_Type, &err->object, 1772 &PyUnicode_Type, &err->object,
1515 &err->start, 1773 &err->start,
1516 &err->end, 1774 &err->end,
1517 &PyUnicode_Type, &err->reason)) { 1775 &PyUnicode_Type, &err->reason)) {
1518 err->encoding = err->object = err->reason = NULL; 1776 err->encoding = err->object = err->reason = NULL;
1519 return -1; 1777 return -1;
1520 } 1778 }
1521 1779
1780 if (PyUnicode_READY(err->object) < -1) {
1781 err->encoding = NULL;
1782 return -1;
1783 }
1784
1522 Py_INCREF(err->encoding); 1785 Py_INCREF(err->encoding);
1523 Py_INCREF(err->object); 1786 Py_INCREF(err->object);
1524 Py_INCREF(err->reason); 1787 Py_INCREF(err->reason);
1525 1788
1526 return 0; 1789 return 0;
1527 } 1790 }
1528 1791
1529 static PyObject * 1792 static PyObject *
1530 UnicodeEncodeError_str(PyObject *self) 1793 UnicodeEncodeError_str(PyObject *self)
1531 { 1794 {
1532 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self; 1795 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
1533 PyObject *result = NULL; 1796 PyObject *result = NULL;
1534 PyObject *reason_str = NULL; 1797 PyObject *reason_str = NULL;
1535 PyObject *encoding_str = NULL; 1798 PyObject *encoding_str = NULL;
1536 1799
1537 /* Get reason and encoding as strings, which they might not be if 1800 /* Get reason and encoding as strings, which they might not be if
1538 they've been modified after we were contructed. */ 1801 they've been modified after we were contructed. */
1539 reason_str = PyObject_Str(uself->reason); 1802 reason_str = PyObject_Str(uself->reason);
1540 if (reason_str == NULL) 1803 if (reason_str == NULL)
1541 goto done; 1804 goto done;
1542 encoding_str = PyObject_Str(uself->encoding); 1805 encoding_str = PyObject_Str(uself->encoding);
1543 if (encoding_str == NULL) 1806 if (encoding_str == NULL)
1544 goto done; 1807 goto done;
1545 1808
1546 if (uself->start < PyUnicode_GET_SIZE(uself->object) && uself->end == uself- >start+1) { 1809 if (uself->start < PyUnicode_GET_LENGTH(uself->object) && uself->end == usel f->start+1) {
1547 int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start]; 1810 Py_UCS4 badchar = PyUnicode_ReadChar(uself->object, uself->start);
1548 const char *fmt; 1811 const char *fmt;
1549 if (badchar <= 0xff) 1812 if (badchar <= 0xff)
1550 fmt = "'%U' codec can't encode character '\\x%02x' in position %zd: %U"; 1813 fmt = "'%U' codec can't encode character '\\x%02x' in position %zd: %U";
1551 else if (badchar <= 0xffff) 1814 else if (badchar <= 0xffff)
1552 fmt = "'%U' codec can't encode character '\\u%04x' in position %zd: %U"; 1815 fmt = "'%U' codec can't encode character '\\u%04x' in position %zd: %U";
1553 else 1816 else
1554 fmt = "'%U' codec can't encode character '\\U%08x' in position %zd: %U"; 1817 fmt = "'%U' codec can't encode character '\\U%08x' in position %zd: %U";
1555 result = PyUnicode_FromFormat( 1818 result = PyUnicode_FromFormat(
1556 fmt, 1819 fmt,
1557 encoding_str, 1820 encoding_str,
1558 badchar, 1821 (int)badchar,
1559 uself->start, 1822 uself->start,
1560 reason_str); 1823 reason_str);
1561 } 1824 }
1562 else { 1825 else {
1563 result = PyUnicode_FromFormat( 1826 result = PyUnicode_FromFormat(
1564 "'%U' codec can't encode characters in position %zd-%zd: %U", 1827 "'%U' codec can't encode characters in position %zd-%zd: %U",
1565 encoding_str, 1828 encoding_str,
1566 uself->start, 1829 uself->start,
1567 uself->end-1, 1830 uself->end-1,
1568 reason_str); 1831 reason_str);
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after
1745 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self; 2008 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
1746 PyObject *result = NULL; 2009 PyObject *result = NULL;
1747 PyObject *reason_str = NULL; 2010 PyObject *reason_str = NULL;
1748 2011
1749 /* Get reason as a string, which it might not be if it's been 2012 /* Get reason as a string, which it might not be if it's been
1750 modified after we were contructed. */ 2013 modified after we were contructed. */
1751 reason_str = PyObject_Str(uself->reason); 2014 reason_str = PyObject_Str(uself->reason);
1752 if (reason_str == NULL) 2015 if (reason_str == NULL)
1753 goto done; 2016 goto done;
1754 2017
1755 if (uself->start < PyUnicode_GET_SIZE(uself->object) && uself->end == uself- >start+1) { 2018 if (uself->start < PyUnicode_GET_LENGTH(uself->object) && uself->end == usel f->start+1) {
1756 int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start]; 2019 Py_UCS4 badchar = PyUnicode_ReadChar(uself->object, uself->start);
1757 const char *fmt; 2020 const char *fmt;
1758 if (badchar <= 0xff) 2021 if (badchar <= 0xff)
1759 fmt = "can't translate character '\\x%02x' in position %zd: %U"; 2022 fmt = "can't translate character '\\x%02x' in position %zd: %U";
1760 else if (badchar <= 0xffff) 2023 else if (badchar <= 0xffff)
1761 fmt = "can't translate character '\\u%04x' in position %zd: %U"; 2024 fmt = "can't translate character '\\u%04x' in position %zd: %U";
1762 else 2025 else
1763 fmt = "can't translate character '\\U%08x' in position %zd: %U"; 2026 fmt = "can't translate character '\\U%08x' in position %zd: %U";
1764 result = PyUnicode_FromFormat( 2027 result = PyUnicode_FromFormat(
1765 fmt, 2028 fmt,
1766 badchar, 2029 (int)badchar,
1767 uself->start, 2030 uself->start,
1768 reason_str 2031 reason_str
1769 ); 2032 );
1770 } else { 2033 } else {
1771 result = PyUnicode_FromFormat( 2034 result = PyUnicode_FromFormat(
1772 "can't translate characters in position %zd-%zd: %U", 2035 "can't translate characters in position %zd-%zd: %U",
1773 uself->start, 2036 uself->start,
1774 uself->end-1, 2037 uself->end-1,
1775 reason_str 2038 reason_str
1776 ); 2039 );
(...skipping 10 matching lines...) Expand all
1787 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2050 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1788 (reprfunc)UnicodeTranslateError_str, 0, 0, 0, 2051 (reprfunc)UnicodeTranslateError_str, 0, 0, 0,
1789 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, 2052 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
1790 PyDoc_STR("Unicode translation error."), (traverseproc)UnicodeError_traverse , 2053 PyDoc_STR("Unicode translation error."), (traverseproc)UnicodeError_traverse ,
1791 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members, 2054 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
1792 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict), 2055 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
1793 (initproc)UnicodeTranslateError_init, 0, BaseException_new, 2056 (initproc)UnicodeTranslateError_init, 0, BaseException_new,
1794 }; 2057 };
1795 PyObject *PyExc_UnicodeTranslateError = (PyObject *)&_PyExc_UnicodeTranslateErro r; 2058 PyObject *PyExc_UnicodeTranslateError = (PyObject *)&_PyExc_UnicodeTranslateErro r;
1796 2059
2060 /* Deprecated. */
1797 PyObject * 2061 PyObject *
1798 PyUnicodeTranslateError_Create( 2062 PyUnicodeTranslateError_Create(
1799 const Py_UNICODE *object, Py_ssize_t length, 2063 const Py_UNICODE *object, Py_ssize_t length,
1800 Py_ssize_t start, Py_ssize_t end, const char *reason) 2064 Py_ssize_t start, Py_ssize_t end, const char *reason)
1801 { 2065 {
1802 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#nns", 2066 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#nns",
1803 object, length, start, end, reason); 2067 object, length, start, end, reason);
1804 } 2068 }
1805 2069
2070 PyObject *
2071 _PyUnicodeTranslateError_Create(
2072 PyObject *object,
2073 Py_ssize_t start, Py_ssize_t end, const char *reason)
2074 {
2075 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "Ons",
2076 object, start, end, reason);
2077 }
1806 2078
1807 /* 2079 /*
1808 * AssertionError extends Exception 2080 * AssertionError extends Exception
1809 */ 2081 */
1810 SimpleExtendsException(PyExc_Exception, AssertionError, 2082 SimpleExtendsException(PyExc_Exception, AssertionError,
1811 "Assertion failed."); 2083 "Assertion failed.");
1812 2084
1813 2085
1814 /* 2086 /*
1815 * ArithmeticError extends Exception 2087 * ArithmeticError extends Exception
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after
2034 "Base class for warnings about resource usage."); 2306 "Base class for warnings about resource usage.");
2035 2307
2036 2308
2037 2309
2038 /* Pre-computed RuntimeError instance for when recursion depth is reached. 2310 /* Pre-computed RuntimeError instance for when recursion depth is reached.
2039 Meant to be used when normalizing the exception for exceeding the recursion 2311 Meant to be used when normalizing the exception for exceeding the recursion
2040 depth will cause its own infinite recursion. 2312 depth will cause its own infinite recursion.
2041 */ 2313 */
2042 PyObject *PyExc_RecursionErrorInst = NULL; 2314 PyObject *PyExc_RecursionErrorInst = NULL;
2043 2315
2044 #define PRE_INIT(TYPE) if (PyType_Ready(&_PyExc_ ## TYPE) < 0) \ 2316 #define PRE_INIT(TYPE) \
2045 Py_FatalError("exceptions bootstrapping error."); 2317 if (!(_PyExc_ ## TYPE.tp_flags & Py_TPFLAGS_READY)) { \
2046 2318 if (PyType_Ready(&_PyExc_ ## TYPE) < 0) \
2047 #define POST_INIT(TYPE) Py_INCREF(PyExc_ ## TYPE); \ 2319 Py_FatalError("exceptions bootstrapping error."); \
2320 Py_INCREF(PyExc_ ## TYPE); \
2321 }
2322
2323 #define POST_INIT(TYPE) \
2048 if (PyDict_SetItemString(bdict, # TYPE, PyExc_ ## TYPE)) \ 2324 if (PyDict_SetItemString(bdict, # TYPE, PyExc_ ## TYPE)) \
2049 Py_FatalError("Module dictionary insertion problem."); 2325 Py_FatalError("Module dictionary insertion problem.");
2050 2326
2327 #define INIT_ALIAS(NAME, TYPE) Py_INCREF(PyExc_ ## TYPE); \
2328 Py_XDECREF(PyExc_ ## NAME); \
2329 PyExc_ ## NAME = PyExc_ ## TYPE; \
2330 if (PyDict_SetItemString(bdict, # NAME, PyExc_ ## NAME)) \
2331 Py_FatalError("Module dictionary insertion problem.");
2332
2333 #define ADD_ERRNO(TYPE, CODE) { \
2334 PyObject *_code = PyLong_FromLong(CODE); \
2335 assert(_PyObject_RealIsSubclass(PyExc_ ## TYPE, PyExc_OSError)); \
2336 if (!_code || PyDict_SetItem(errnomap, _code, PyExc_ ## TYPE)) \
2337 Py_FatalError("errmap insertion problem."); \
2338 Py_DECREF(_code); \
2339 }
2340
2341 #ifdef MS_WINDOWS
2342 #include <Winsock2.h>
2343 #if defined(WSAEALREADY) && !defined(EALREADY)
2344 #define EALREADY WSAEALREADY
2345 #endif
2346 #if defined(WSAECONNABORTED) && !defined(ECONNABORTED)
2347 #define ECONNABORTED WSAECONNABORTED
2348 #endif
2349 #if defined(WSAECONNREFUSED) && !defined(ECONNREFUSED)
2350 #define ECONNREFUSED WSAECONNREFUSED
2351 #endif
2352 #if defined(WSAECONNRESET) && !defined(ECONNRESET)
2353 #define ECONNRESET WSAECONNRESET
2354 #endif
2355 #if defined(WSAEINPROGRESS) && !defined(EINPROGRESS)
2356 #define EINPROGRESS WSAEINPROGRESS
2357 #endif
2358 #if defined(WSAESHUTDOWN) && !defined(ESHUTDOWN)
2359 #define ESHUTDOWN WSAESHUTDOWN
2360 #endif
2361 #if defined(WSAETIMEDOUT) && !defined(ETIMEDOUT)
2362 #define ETIMEDOUT WSAETIMEDOUT
2363 #endif
2364 #if defined(WSAEWOULDBLOCK) && !defined(EWOULDBLOCK)
2365 #define EWOULDBLOCK WSAEWOULDBLOCK
2366 #endif
2367 #endif /* MS_WINDOWS */
2051 2368
2052 void 2369 void
2053 _PyExc_Init(void) 2370 _PyExc_Init(void)
2054 { 2371 {
2055 PyObject *bltinmod, *bdict; 2372 PyObject *bltinmod, *bdict;
2056 2373
2057 PRE_INIT(BaseException) 2374 PRE_INIT(BaseException)
2058 PRE_INIT(Exception) 2375 PRE_INIT(Exception)
2059 PRE_INIT(TypeError) 2376 PRE_INIT(TypeError)
2060 PRE_INIT(StopIteration) 2377 PRE_INIT(StopIteration)
2061 PRE_INIT(GeneratorExit) 2378 PRE_INIT(GeneratorExit)
2062 PRE_INIT(SystemExit) 2379 PRE_INIT(SystemExit)
2063 PRE_INIT(KeyboardInterrupt) 2380 PRE_INIT(KeyboardInterrupt)
2064 PRE_INIT(ImportError) 2381 PRE_INIT(ImportError)
2065 PRE_INIT(EnvironmentError)
2066 PRE_INIT(IOError)
2067 PRE_INIT(OSError) 2382 PRE_INIT(OSError)
2068 #ifdef MS_WINDOWS
2069 PRE_INIT(WindowsError)
2070 #endif
2071 #ifdef __VMS
2072 PRE_INIT(VMSError)
2073 #endif
2074 PRE_INIT(EOFError) 2383 PRE_INIT(EOFError)
2075 PRE_INIT(RuntimeError) 2384 PRE_INIT(RuntimeError)
2076 PRE_INIT(NotImplementedError) 2385 PRE_INIT(NotImplementedError)
2077 PRE_INIT(NameError) 2386 PRE_INIT(NameError)
2078 PRE_INIT(UnboundLocalError) 2387 PRE_INIT(UnboundLocalError)
2079 PRE_INIT(AttributeError) 2388 PRE_INIT(AttributeError)
2080 PRE_INIT(SyntaxError) 2389 PRE_INIT(SyntaxError)
2081 PRE_INIT(IndentationError) 2390 PRE_INIT(IndentationError)
2082 PRE_INIT(TabError) 2391 PRE_INIT(TabError)
2083 PRE_INIT(LookupError) 2392 PRE_INIT(LookupError)
(...skipping 19 matching lines...) Expand all
2103 PRE_INIT(DeprecationWarning) 2412 PRE_INIT(DeprecationWarning)
2104 PRE_INIT(PendingDeprecationWarning) 2413 PRE_INIT(PendingDeprecationWarning)
2105 PRE_INIT(SyntaxWarning) 2414 PRE_INIT(SyntaxWarning)
2106 PRE_INIT(RuntimeWarning) 2415 PRE_INIT(RuntimeWarning)
2107 PRE_INIT(FutureWarning) 2416 PRE_INIT(FutureWarning)
2108 PRE_INIT(ImportWarning) 2417 PRE_INIT(ImportWarning)
2109 PRE_INIT(UnicodeWarning) 2418 PRE_INIT(UnicodeWarning)
2110 PRE_INIT(BytesWarning) 2419 PRE_INIT(BytesWarning)
2111 PRE_INIT(ResourceWarning) 2420 PRE_INIT(ResourceWarning)
2112 2421
2422 /* OSError subclasses */
2423 PRE_INIT(ConnectionError);
2424
2425 PRE_INIT(BlockingIOError);
2426 PRE_INIT(BrokenPipeError);
2427 PRE_INIT(ChildProcessError);
2428 PRE_INIT(ConnectionAbortedError);
2429 PRE_INIT(ConnectionRefusedError);
2430 PRE_INIT(ConnectionResetError);
2431 PRE_INIT(FileExistsError);
2432 PRE_INIT(FileNotFoundError);
2433 PRE_INIT(IsADirectoryError);
2434 PRE_INIT(NotADirectoryError);
2435 PRE_INIT(InterruptedError);
2436 PRE_INIT(PermissionError);
2437 PRE_INIT(ProcessLookupError);
2438 PRE_INIT(TimeoutError);
2439
2113 bltinmod = PyImport_ImportModule("builtins"); 2440 bltinmod = PyImport_ImportModule("builtins");
2114 if (bltinmod == NULL) 2441 if (bltinmod == NULL)
2115 Py_FatalError("exceptions bootstrapping error."); 2442 Py_FatalError("exceptions bootstrapping error.");
2116 bdict = PyModule_GetDict(bltinmod); 2443 bdict = PyModule_GetDict(bltinmod);
2117 if (bdict == NULL) 2444 if (bdict == NULL)
2118 Py_FatalError("exceptions bootstrapping error."); 2445 Py_FatalError("exceptions bootstrapping error.");
2119 2446
2120 POST_INIT(BaseException) 2447 POST_INIT(BaseException)
2121 POST_INIT(Exception) 2448 POST_INIT(Exception)
2122 POST_INIT(TypeError) 2449 POST_INIT(TypeError)
2123 POST_INIT(StopIteration) 2450 POST_INIT(StopIteration)
2124 POST_INIT(GeneratorExit) 2451 POST_INIT(GeneratorExit)
2125 POST_INIT(SystemExit) 2452 POST_INIT(SystemExit)
2126 POST_INIT(KeyboardInterrupt) 2453 POST_INIT(KeyboardInterrupt)
2127 POST_INIT(ImportError) 2454 POST_INIT(ImportError)
2128 POST_INIT(EnvironmentError)
2129 POST_INIT(IOError)
2130 POST_INIT(OSError) 2455 POST_INIT(OSError)
2456 INIT_ALIAS(EnvironmentError, OSError)
2457 INIT_ALIAS(IOError, OSError)
2131 #ifdef MS_WINDOWS 2458 #ifdef MS_WINDOWS
2132 POST_INIT(WindowsError) 2459 INIT_ALIAS(WindowsError, OSError)
2133 #endif 2460 #endif
2134 #ifdef __VMS 2461 #ifdef __VMS
2135 POST_INIT(VMSError) 2462 INIT_ALIAS(VMSError, OSError)
2136 #endif 2463 #endif
2137 POST_INIT(EOFError) 2464 POST_INIT(EOFError)
2138 POST_INIT(RuntimeError) 2465 POST_INIT(RuntimeError)
2139 POST_INIT(NotImplementedError) 2466 POST_INIT(NotImplementedError)
2140 POST_INIT(NameError) 2467 POST_INIT(NameError)
2141 POST_INIT(UnboundLocalError) 2468 POST_INIT(UnboundLocalError)
2142 POST_INIT(AttributeError) 2469 POST_INIT(AttributeError)
2143 POST_INIT(SyntaxError) 2470 POST_INIT(SyntaxError)
2144 POST_INIT(IndentationError) 2471 POST_INIT(IndentationError)
2145 POST_INIT(TabError) 2472 POST_INIT(TabError)
(...skipping 20 matching lines...) Expand all
2166 POST_INIT(DeprecationWarning) 2493 POST_INIT(DeprecationWarning)
2167 POST_INIT(PendingDeprecationWarning) 2494 POST_INIT(PendingDeprecationWarning)
2168 POST_INIT(SyntaxWarning) 2495 POST_INIT(SyntaxWarning)
2169 POST_INIT(RuntimeWarning) 2496 POST_INIT(RuntimeWarning)
2170 POST_INIT(FutureWarning) 2497 POST_INIT(FutureWarning)
2171 POST_INIT(ImportWarning) 2498 POST_INIT(ImportWarning)
2172 POST_INIT(UnicodeWarning) 2499 POST_INIT(UnicodeWarning)
2173 POST_INIT(BytesWarning) 2500 POST_INIT(BytesWarning)
2174 POST_INIT(ResourceWarning) 2501 POST_INIT(ResourceWarning)
2175 2502
2503 if (!errnomap) {
2504 errnomap = PyDict_New();
2505 if (!errnomap)
2506 Py_FatalError("Cannot allocate map from errnos to OSError subclasses ");
2507 }
2508
2509 /* OSError subclasses */
2510 POST_INIT(ConnectionError);
2511
2512 POST_INIT(BlockingIOError);
2513 ADD_ERRNO(BlockingIOError, EAGAIN);
2514 ADD_ERRNO(BlockingIOError, EALREADY);
2515 ADD_ERRNO(BlockingIOError, EINPROGRESS);
2516 ADD_ERRNO(BlockingIOError, EWOULDBLOCK);
2517 POST_INIT(BrokenPipeError);
2518 ADD_ERRNO(BrokenPipeError, EPIPE);
2519 ADD_ERRNO(BrokenPipeError, ESHUTDOWN);
2520 POST_INIT(ChildProcessError);
2521 ADD_ERRNO(ChildProcessError, ECHILD);
2522 POST_INIT(ConnectionAbortedError);
2523 ADD_ERRNO(ConnectionAbortedError, ECONNABORTED);
2524 POST_INIT(ConnectionRefusedError);
2525 ADD_ERRNO(ConnectionRefusedError, ECONNREFUSED);
2526 POST_INIT(ConnectionResetError);
2527 ADD_ERRNO(ConnectionResetError, ECONNRESET);
2528 POST_INIT(FileExistsError);
2529 ADD_ERRNO(FileExistsError, EEXIST);
2530 POST_INIT(FileNotFoundError);
2531 ADD_ERRNO(FileNotFoundError, ENOENT);
2532 POST_INIT(IsADirectoryError);
2533 ADD_ERRNO(IsADirectoryError, EISDIR);
2534 POST_INIT(NotADirectoryError);
2535 ADD_ERRNO(NotADirectoryError, ENOTDIR);
2536 POST_INIT(InterruptedError);
2537 ADD_ERRNO(InterruptedError, EINTR);
2538 POST_INIT(PermissionError);
2539 ADD_ERRNO(PermissionError, EACCES);
2540 ADD_ERRNO(PermissionError, EPERM);
2541 POST_INIT(ProcessLookupError);
2542 ADD_ERRNO(ProcessLookupError, ESRCH);
2543 POST_INIT(TimeoutError);
2544 ADD_ERRNO(TimeoutError, ETIMEDOUT);
2545
2176 preallocate_memerrors(); 2546 preallocate_memerrors();
2177 2547
2178 PyExc_RecursionErrorInst = BaseException_new(&_PyExc_RuntimeError, NULL, NUL L); 2548 if (!PyExc_RecursionErrorInst) {
2179 if (!PyExc_RecursionErrorInst) 2549 PyExc_RecursionErrorInst = BaseException_new(&_PyExc_RuntimeError, NULL, NULL);
2180 Py_FatalError("Cannot pre-allocate RuntimeError instance for " 2550 if (!PyExc_RecursionErrorInst)
2181 "recursion errors"); 2551 Py_FatalError("Cannot pre-allocate RuntimeError instance for "
2182 else { 2552 "recursion errors");
2183 PyBaseExceptionObject *err_inst = 2553 else {
2184 (PyBaseExceptionObject *)PyExc_RecursionErrorInst; 2554 PyBaseExceptionObject *err_inst =
2185 PyObject *args_tuple; 2555 (PyBaseExceptionObject *)PyExc_RecursionErrorInst;
2186 PyObject *exc_message; 2556 PyObject *args_tuple;
2187 exc_message = PyUnicode_FromString("maximum recursion depth exceeded"); 2557 PyObject *exc_message;
2188 if (!exc_message) 2558 exc_message = PyUnicode_FromString("maximum recursion depth exceeded ");
2189 Py_FatalError("cannot allocate argument for RuntimeError " 2559 if (!exc_message)
2190 "pre-allocation"); 2560 Py_FatalError("cannot allocate argument for RuntimeError "
2191 args_tuple = PyTuple_Pack(1, exc_message); 2561 "pre-allocation");
2192 if (!args_tuple) 2562 args_tuple = PyTuple_Pack(1, exc_message);
2193 Py_FatalError("cannot allocate tuple for RuntimeError " 2563 if (!args_tuple)
2194 "pre-allocation"); 2564 Py_FatalError("cannot allocate tuple for RuntimeError "
2195 Py_DECREF(exc_message); 2565 "pre-allocation");
2196 if (BaseException_init(err_inst, args_tuple, NULL)) 2566 Py_DECREF(exc_message);
2197 Py_FatalError("init of pre-allocated RuntimeError failed"); 2567 if (BaseException_init(err_inst, args_tuple, NULL))
2198 Py_DECREF(args_tuple); 2568 Py_FatalError("init of pre-allocated RuntimeError failed");
2199 } 2569 Py_DECREF(args_tuple);
2200 2570 }
2571 }
2201 Py_DECREF(bltinmod); 2572 Py_DECREF(bltinmod);
2202 } 2573 }
2203 2574
2204 void 2575 void
2205 _PyExc_Fini(void) 2576 _PyExc_Fini(void)
2206 { 2577 {
2207 Py_CLEAR(PyExc_RecursionErrorInst); 2578 Py_CLEAR(PyExc_RecursionErrorInst);
2208 free_preallocated_memerrors(); 2579 free_preallocated_memerrors();
2209 } 2580 Py_CLEAR(errnomap);
2581 }
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld cbc36f91f3f7