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

Delta Between Two Patch Sets: Objects/exceptions.c

Issue 11682: PEP 380 reference implementation for 3.3
Left Patch Set: Created 8 years, 1 month ago
Right Patch Set: Created 7 years, 7 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
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 368 matching lines...) Expand 10 before | Expand all | Expand 10 after
426 static PyTypeObject _PyExc_ ## EXCNAME = { \ 440 static PyTypeObject _PyExc_ ## EXCNAME = { \
427 PyVarObject_HEAD_INIT(NULL, 0) \ 441 PyVarObject_HEAD_INIT(NULL, 0) \
428 # EXCNAME, \ 442 # EXCNAME, \
429 sizeof(Py ## EXCSTORE ## Object), \ 443 sizeof(Py ## EXCSTORE ## Object), \
430 0, (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, \ 444 0, (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
431 0, 0, 0, 0, 0, \ 445 0, 0, 0, 0, 0, \
432 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \ 446 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
433 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \ 447 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
434 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \ 448 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, 0, 0, 0, &_ ## EXCBASE, \
435 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \ 449 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
436 (initproc)EXCSTORE ## _init, 0, BaseException_new,\ 450 (initproc)EXCSTORE ## _init, 0, 0, \
437 }; \ 451 }; \
438 PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME 452 PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
439 453
440 #define ComplexExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCDEALLOC, EXCMETHO DS, EXCMEMBERS, EXCSTR, EXCDOC) \ 454 #define ComplexExtendsException(EXCBASE, EXCNAME, EXCSTORE, EXCNEW, \
455 EXCMETHODS, EXCMEMBERS, EXCGETSET, \
456 EXCSTR, EXCDOC) \
441 static PyTypeObject _PyExc_ ## EXCNAME = { \ 457 static PyTypeObject _PyExc_ ## EXCNAME = { \
442 PyVarObject_HEAD_INIT(NULL, 0) \ 458 PyVarObject_HEAD_INIT(NULL, 0) \
443 # EXCNAME, \ 459 # EXCNAME, \
444 sizeof(Py ## EXCSTORE ## Object), 0, \ 460 sizeof(Py ## EXCSTORE ## Object), 0, \
445 (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \ 461 (destructor)EXCSTORE ## _dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
446 (reprfunc)EXCSTR, 0, 0, 0, \ 462 (reprfunc)EXCSTR, 0, 0, 0, \
447 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \ 463 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, \
448 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \ 464 PyDoc_STR(EXCDOC), (traverseproc)EXCSTORE ## _traverse, \
449 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, EXCMETHODS, \ 465 (inquiry)EXCSTORE ## _clear, 0, 0, 0, 0, EXCMETHODS, \
450 EXCMEMBERS, 0, &_ ## EXCBASE, \ 466 EXCMEMBERS, EXCGETSET, &_ ## EXCBASE, \
451 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \ 467 0, 0, 0, offsetof(Py ## EXCSTORE ## Object, dict), \
452 (initproc)EXCSTORE ## _init, 0, BaseException_new,\ 468 (initproc)EXCSTORE ## _init, 0, EXCNEW,\
453 }; \ 469 }; \
454 PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME 470 PyObject *PyExc_ ## EXCNAME = (PyObject *)&_PyExc_ ## EXCNAME
455 471
456 472
457 /* 473 /*
458 * Exception extends BaseException 474 * Exception extends BaseException
459 */ 475 */
460 SimpleExtendsException(PyExc_BaseException, Exception, 476 SimpleExtendsException(PyExc_BaseException, Exception,
461 "Common base class for all non-exit exceptions."); 477 "Common base class for all non-exit exceptions.");
462 478
463 479
464 /* 480 /*
465 * TypeError extends Exception 481 * TypeError extends Exception
466 */ 482 */
467 SimpleExtendsException(PyExc_Exception, TypeError, 483 SimpleExtendsException(PyExc_Exception, TypeError,
468 "Inappropriate argument type."); 484 "Inappropriate argument type.");
469 485
470 486
471 /* 487 /*
472 * StopIteration extends Exception 488 * StopIteration extends Exception
473 */ 489 */
474 490
475 //SimpleExtendsException(PyExc_Exception, StopIteration, 491 //SimpleExtendsException(PyExc_Exception, StopIteration,
476 // "Signal the end from iterator.__next__()."); 492 // "Signal the end from iterator.__next__().");
Z. Jędrzejewski-Szmek 2012/01/12 19:06:52 Remove this comment?
477 493
478 static PyMemberDef StopIteration_members[] = { 494 static PyMemberDef StopIteration_members[] = {
479 {"value", T_OBJECT, offsetof(PyStopIterationObject, value), 0, 495 {"value", T_OBJECT, offsetof(PyStopIterationObject, value), 0,
480 PyDoc_STR("generator return value")}, 496 PyDoc_STR("generator return value")},
481 {NULL} /* Sentinel */ 497 {NULL} /* Sentinel */
482 }; 498 };
483 499
484 static int 500 static int
485 StopIteration_init(PyStopIterationObject *self, PyObject *args, PyObject *kwds) 501 StopIteration_init(PyStopIterationObject *self, PyObject *args, PyObject *kwds)
486 { 502 {
(...skipping 28 matching lines...) Expand all
515 } 531 }
516 532
517 static int 533 static int
518 StopIteration_traverse(PyStopIterationObject *self, visitproc visit, void *arg) 534 StopIteration_traverse(PyStopIterationObject *self, visitproc visit, void *arg)
519 { 535 {
520 Py_VISIT(self->value); 536 Py_VISIT(self->value);
521 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg); 537 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
522 } 538 }
523 539
524 PyObject * 540 PyObject *
525 PyStopIteration_New(PyObject *value) 541 PyStopIteration_Create(PyObject *value)
526 { 542 {
527 return PyObject_CallFunctionObjArgs(PyExc_StopIteration, value, NULL); 543 return PyObject_CallFunctionObjArgs(PyExc_StopIteration, value, NULL);
528 } 544 }
529 545
530 ComplexExtendsException(PyExc_Exception, StopIteration, StopIteration, 546 ComplexExtendsException(
531 StopIteration_dealloc, 0, StopIteration_members, 0, 547 PyExc_Exception, /* base */
532 "Signal the end from iterator.__next__()."); 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 );
533 557
534 558
535 /* 559 /*
536 * GeneratorExit extends BaseException 560 * GeneratorExit extends BaseException
537 */ 561 */
538 SimpleExtendsException(PyExc_BaseException, GeneratorExit, 562 SimpleExtendsException(PyExc_BaseException, GeneratorExit,
539 "Request that a generator exit."); 563 "Request that a generator exit.");
540 564
541 565
542 /* 566 /*
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
584 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg); 608 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
585 } 609 }
586 610
587 static PyMemberDef SystemExit_members[] = { 611 static PyMemberDef SystemExit_members[] = {
588 {"code", T_OBJECT, offsetof(PySystemExitObject, code), 0, 612 {"code", T_OBJECT, offsetof(PySystemExitObject, code), 0,
589 PyDoc_STR("exception code")}, 613 PyDoc_STR("exception code")},
590 {NULL} /* Sentinel */ 614 {NULL} /* Sentinel */
591 }; 615 };
592 616
593 ComplexExtendsException(PyExc_BaseException, SystemExit, SystemExit, 617 ComplexExtendsException(PyExc_BaseException, SystemExit, SystemExit,
594 SystemExit_dealloc, 0, SystemExit_members, 0, 618 0, 0, SystemExit_members, 0, 0,
595 "Request to exit from the interpreter."); 619 "Request to exit from the interpreter.");
596 620
597 /* 621 /*
598 * KeyboardInterrupt extends BaseException 622 * KeyboardInterrupt extends BaseException
599 */ 623 */
600 SimpleExtendsException(PyExc_BaseException, KeyboardInterrupt, 624 SimpleExtendsException(PyExc_BaseException, KeyboardInterrupt,
601 "Program interrupted by user."); 625 "Program interrupted by user.");
602 626
603 627
604 /* 628 /*
605 * ImportError extends Exception 629 * ImportError extends Exception
606 */ 630 */
607 SimpleExtendsException(PyExc_Exception, ImportError, 631 SimpleExtendsException(PyExc_Exception, ImportError,
608 "Import can't find module, or can't find name in module."); 632 "Import can't find module, or can't find name in module.");
609 633
610 634
611 /* 635 /*
612 * EnvironmentError extends Exception 636 * OSError extends Exception
613 */ 637 */
638
639 #ifdef MS_WINDOWS
640 #include "errmap.h"
641 #endif
614 642
615 /* Where a function has a single filename, such as open() or some 643 /* Where a function has a single filename, such as open() or some
616 * of the os module functions, PyErr_SetFromErrnoWithFilename() is 644 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
617 * called, giving a third argument which is the filename. But, so 645 * called, giving a third argument which is the filename. But, so
618 * that old code using in-place unpacking doesn't break, e.g.: 646 * that old code using in-place unpacking doesn't break, e.g.:
619 * 647 *
620 * except IOError, (errno, strerror): 648 * except OSError, (errno, strerror):
621 * 649 *
622 * we hack args so that it only contains two items. This also 650 * we hack args so that it only contains two items. This also
623 * means we need our own __str__() which prints out the filename 651 * means we need our own __str__() which prints out the filename
624 * when it was supplied. 652 * when it was supplied.
625 */ 653 */
626 static int 654
627 EnvironmentError_init(PyEnvironmentErrorObject *self, PyObject *args, 655 /* This function doesn't cleanup on error, the caller should */
628 PyObject *kwds) 656 static int
629 { 657 oserror_parse_args(PyObject **p_args,
658 PyObject **myerrno, PyObject **strerror,
659 PyObject **filename
660 #ifdef MS_WINDOWS
661 , PyObject **winerror
662 #endif
663 )
664 {
665 Py_ssize_t nargs;
666 PyObject *args = *p_args;
667
668 nargs = PyTuple_GET_SIZE(args);
669
670 #ifdef MS_WINDOWS
671 if (nargs >= 2 && nargs <= 4) {
672 if (!PyArg_UnpackTuple(args, "OSError", 2, 4,
673 myerrno, strerror, filename, winerror))
674 return -1;
675 if (*winerror && PyLong_Check(*winerror)) {
676 long errcode, winerrcode;
677 PyObject *newargs;
678 Py_ssize_t i;
679
680 winerrcode = PyLong_AsLong(*winerror);
681 if (winerrcode == -1 && PyErr_Occurred())
682 return -1;
683 /* Set errno to the corresponding POSIX errno (overriding
684 first argument). Windows Socket error codes (>= 10000)
685 have the same value as their POSIX counterparts.
686 */
687 if (winerrcode < 10000)
688 errcode = winerror_to_errno(winerrcode);
689 else
690 errcode = winerrcode;
691 *myerrno = PyLong_FromLong(errcode);
692 if (!*myerrno)
693 return -1;
694 newargs = PyTuple_New(nargs);
695 if (!newargs)
696 return -1;
697 PyTuple_SET_ITEM(newargs, 0, *myerrno);
698 for (i = 1; i < nargs; i++) {
699 PyObject *val = PyTuple_GET_ITEM(args, i);
700 Py_INCREF(val);
701 PyTuple_SET_ITEM(newargs, i, val);
702 }
703 Py_DECREF(args);
704 args = *p_args = newargs;
705 }
706 }
707 #else
708 if (nargs >= 2 && nargs <= 3) {
709 if (!PyArg_UnpackTuple(args, "OSError", 2, 3,
710 myerrno, strerror, filename))
711 return -1;
712 }
713 #endif
714
715 return 0;
716 }
717
718 static int
719 oserror_init(PyOSErrorObject *self, PyObject **p_args,
720 PyObject *myerrno, PyObject *strerror,
721 PyObject *filename
722 #ifdef MS_WINDOWS
723 , PyObject *winerror
724 #endif
725 )
726 {
727 PyObject *args = *p_args;
728 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
729
730 /* self->filename will remain Py_None otherwise */
731 if (filename && filename != Py_None) {
732 if (Py_TYPE(self) == (PyTypeObject *) PyExc_BlockingIOError &&
733 PyNumber_Check(filename)) {
734 /* BlockingIOError's 3rd argument can be the number of
735 * characters written.
736 */
737 self->written = PyNumber_AsSsize_t(filename, PyExc_ValueError);
738 if (self->written == -1 && PyErr_Occurred())
739 return -1;
740 }
741 else {
742 Py_INCREF(filename);
743 self->filename = filename;
744
745 if (nargs >= 2 && nargs <= 3) {
746 /* filename is removed from the args tuple (for compatibility
747 purposes, see test_exceptions.py) */
748 PyObject *subslice = PyTuple_GetSlice(args, 0, 2);
749 if (!subslice)
750 return -1;
751
752 Py_DECREF(args); /* replacing args */
753 *p_args = args = subslice;
754 }
755 }
756 }
757 Py_XINCREF(myerrno);
758 self->myerrno = myerrno;
759
760 Py_XINCREF(strerror);
761 self->strerror = strerror;
762
763 #ifdef MS_WINDOWS
764 Py_XINCREF(winerror);
765 self->winerror = winerror;
766 #endif
767
768 /* Steals the reference to args */
769 self->args = args;
770 args = NULL;
771
772 return 0;
773 }
774
775 static PyObject *
776 OSError_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
777 static int
778 OSError_init(PyOSErrorObject *self, PyObject *args, PyObject *kwds);
779
780 static int
781 oserror_use_init(PyTypeObject *type)
782 {
783 /* When __init__ is defined in a OSError subclass, we want any
784 extraneous argument to __new__ to be ignored. The only reasonable
785 solution, given __new__ takes a variable number of arguments,
786 is to defer arg parsing and initialization to __init__.
787
788 But when __new__ is overriden as well, it should call our __new__
789 with the right arguments.
790
791 (see http://bugs.python.org/issue12555#msg148829 )
792 */
793 if (type->tp_init != (initproc) OSError_init &&
794 type->tp_new == (newfunc) OSError_new) {
795 assert((PyObject *) type != PyExc_OSError);
796 return 1;
797 }
798 return 0;
799 }
800
801 static PyObject *
802 OSError_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
803 {
804 PyOSErrorObject *self = NULL;
630 PyObject *myerrno = NULL, *strerror = NULL, *filename = NULL; 805 PyObject *myerrno = NULL, *strerror = NULL, *filename = NULL;
631 PyObject *subslice = NULL; 806 #ifdef MS_WINDOWS
632 807 PyObject *winerror = NULL;
633 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1) 808 #endif
809
810 if (!oserror_use_init(type)) {
811 if (!_PyArg_NoKeywords(type->tp_name, kwds))
812 return NULL;
813
814 Py_INCREF(args);
815 if (oserror_parse_args(&args, &myerrno, &strerror, &filename
816 #ifdef MS_WINDOWS
817 , &winerror
818 #endif
819 ))
820 goto error;
821
822 if (myerrno && PyLong_Check(myerrno) &&
823 errnomap && (PyObject *) type == PyExc_OSError) {
824 PyObject *newtype;
825 newtype = PyDict_GetItem(errnomap, myerrno);
826 if (newtype) {
827 assert(PyType_Check(newtype));
828 type = (PyTypeObject *) newtype;
829 }
830 else if (PyErr_Occurred())
831 goto error;
832 }
833 }
834
835 self = (PyOSErrorObject *) type->tp_alloc(type, 0);
836 if (!self)
837 goto error;
838
839 self->dict = NULL;
840 self->traceback = self->cause = self->context = NULL;
841 self->written = -1;
842
843 if (!oserror_use_init(type)) {
844 if (oserror_init(self, &args, myerrno, strerror, filename
845 #ifdef MS_WINDOWS
846 , winerror
847 #endif
848 ))
849 goto error;
850 }
851
852 return (PyObject *) self;
853
854 error:
855 Py_XDECREF(args);
856 Py_XDECREF(self);
857 return NULL;
858 }
859
860 static int
861 OSError_init(PyOSErrorObject *self, PyObject *args, PyObject *kwds)
862 {
863 PyObject *myerrno = NULL, *strerror = NULL, *filename = NULL;
864 #ifdef MS_WINDOWS
865 PyObject *winerror = NULL;
866 #endif
867
868 if (!oserror_use_init(Py_TYPE(self)))
869 /* Everything already done in OSError_new */
870 return 0;
871
872 if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds))
634 return -1; 873 return -1;
635 874
636 if (PyTuple_GET_SIZE(args) <= 1 || PyTuple_GET_SIZE(args) > 3) { 875 Py_INCREF(args);
637 return 0; 876 if (oserror_parse_args(&args, &myerrno, &strerror, &filename
638 } 877 #ifdef MS_WINDOWS
639 878 , &winerror
640 if (!PyArg_UnpackTuple(args, "EnvironmentError", 2, 3, 879 #endif
641 &myerrno, &strerror, &filename)) { 880 ))
642 return -1; 881 goto error;
643 } 882
644 Py_CLEAR(self->myerrno); /* replacing */ 883 if (oserror_init(self, &args, myerrno, strerror, filename
645 self->myerrno = myerrno; 884 #ifdef MS_WINDOWS
646 Py_INCREF(self->myerrno); 885 , winerror
647 886 #endif
648 Py_CLEAR(self->strerror); /* replacing */ 887 ))
649 self->strerror = strerror; 888 goto error;
650 Py_INCREF(self->strerror); 889
651 890 return 0;
652 /* self->filename will remain Py_None otherwise */ 891
653 if (filename != NULL) { 892 error:
654 Py_CLEAR(self->filename); /* replacing */ 893 Py_XDECREF(args);
655 self->filename = filename; 894 return -1;
656 Py_INCREF(self->filename); 895 }
657 896
658 subslice = PyTuple_GetSlice(args, 0, 2); 897 static int
659 if (!subslice) 898 OSError_clear(PyOSErrorObject *self)
660 return -1;
661
662 Py_DECREF(self->args); /* replacing args */
663 self->args = subslice;
664 }
665 return 0;
666 }
667
668 static int
669 EnvironmentError_clear(PyEnvironmentErrorObject *self)
670 { 899 {
671 Py_CLEAR(self->myerrno); 900 Py_CLEAR(self->myerrno);
672 Py_CLEAR(self->strerror); 901 Py_CLEAR(self->strerror);
673 Py_CLEAR(self->filename); 902 Py_CLEAR(self->filename);
903 #ifdef MS_WINDOWS
904 Py_CLEAR(self->winerror);
905 #endif
674 return BaseException_clear((PyBaseExceptionObject *)self); 906 return BaseException_clear((PyBaseExceptionObject *)self);
675 } 907 }
676 908
677 static void 909 static void
678 EnvironmentError_dealloc(PyEnvironmentErrorObject *self) 910 OSError_dealloc(PyOSErrorObject *self)
679 { 911 {
680 _PyObject_GC_UNTRACK(self); 912 _PyObject_GC_UNTRACK(self);
681 EnvironmentError_clear(self); 913 OSError_clear(self);
682 Py_TYPE(self)->tp_free((PyObject *)self); 914 Py_TYPE(self)->tp_free((PyObject *)self);
683 } 915 }
684 916
685 static int 917 static int
686 EnvironmentError_traverse(PyEnvironmentErrorObject *self, visitproc visit, 918 OSError_traverse(PyOSErrorObject *self, visitproc visit,
687 void *arg) 919 void *arg)
688 { 920 {
689 Py_VISIT(self->myerrno); 921 Py_VISIT(self->myerrno);
690 Py_VISIT(self->strerror); 922 Py_VISIT(self->strerror);
691 Py_VISIT(self->filename); 923 Py_VISIT(self->filename);
924 #ifdef MS_WINDOWS
925 Py_VISIT(self->winerror);
926 #endif
692 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg); 927 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
693 } 928 }
694 929
695 static PyObject * 930 static PyObject *
696 EnvironmentError_str(PyEnvironmentErrorObject *self) 931 OSError_str(PyOSErrorObject *self)
697 { 932 {
933 #ifdef MS_WINDOWS
934 /* If available, winerror has the priority over myerrno */
935 if (self->winerror && self->filename)
936 return PyUnicode_FromFormat("[Error %S] %S: %R",
937 self->winerror ? self->winerror: Py_None,
938 self->strerror ? self->strerror: Py_None,
939 self->filename);
940 if (self->winerror && self->strerror)
941 return PyUnicode_FromFormat("[Error %S] %S",
942 self->winerror ? self->winerror: Py_None,
943 self->strerror ? self->strerror: Py_None);
944 #endif
698 if (self->filename) 945 if (self->filename)
699 return PyUnicode_FromFormat("[Errno %S] %S: %R", 946 return PyUnicode_FromFormat("[Errno %S] %S: %R",
700 self->myerrno ? self->myerrno: Py_None, 947 self->myerrno ? self->myerrno: Py_None,
701 self->strerror ? self->strerror: Py_None, 948 self->strerror ? self->strerror: Py_None,
702 self->filename); 949 self->filename);
703 else if (self->myerrno && self->strerror) 950 if (self->myerrno && self->strerror)
704 return PyUnicode_FromFormat("[Errno %S] %S", 951 return PyUnicode_FromFormat("[Errno %S] %S",
705 self->myerrno ? self->myerrno: Py_None, 952 self->myerrno ? self->myerrno: Py_None,
706 self->strerror ? self->strerror: Py_None); 953 self->strerror ? self->strerror: Py_None);
707 else 954 return BaseException_str((PyBaseExceptionObject *)self);
708 return BaseException_str((PyBaseExceptionObject *)self); 955 }
709 }
710
711 static PyMemberDef EnvironmentError_members[] = {
712 {"errno", T_OBJECT, offsetof(PyEnvironmentErrorObject, myerrno), 0,
713 PyDoc_STR("exception errno")},
714 {"strerror", T_OBJECT, offsetof(PyEnvironmentErrorObject, strerror), 0,
715 PyDoc_STR("exception strerror")},
716 {"filename", T_OBJECT, offsetof(PyEnvironmentErrorObject, filename), 0,
717 PyDoc_STR("exception filename")},
718 {NULL} /* Sentinel */
719 };
720
721 956
722 static PyObject * 957 static PyObject *
723 EnvironmentError_reduce(PyEnvironmentErrorObject *self) 958 OSError_reduce(PyOSErrorObject *self)
724 { 959 {
725 PyObject *args = self->args; 960 PyObject *args = self->args;
726 PyObject *res = NULL, *tmp; 961 PyObject *res = NULL, *tmp;
727 962
728 /* self->args is only the first two real arguments if there was a 963 /* self->args is only the first two real arguments if there was a
729 * file name given to EnvironmentError. */ 964 * file name given to OSError. */
730 if (PyTuple_GET_SIZE(args) == 2 && self->filename) { 965 if (PyTuple_GET_SIZE(args) == 2 && self->filename) {
731 args = PyTuple_New(3); 966 args = PyTuple_New(3);
732 if (!args) return NULL; 967 if (!args) return NULL;
733 968
734 tmp = PyTuple_GET_ITEM(self->args, 0); 969 tmp = PyTuple_GET_ITEM(self->args, 0);
735 Py_INCREF(tmp); 970 Py_INCREF(tmp);
736 PyTuple_SET_ITEM(args, 0, tmp); 971 PyTuple_SET_ITEM(args, 0, tmp);
737 972
738 tmp = PyTuple_GET_ITEM(self->args, 1); 973 tmp = PyTuple_GET_ITEM(self->args, 1);
739 Py_INCREF(tmp); 974 Py_INCREF(tmp);
740 PyTuple_SET_ITEM(args, 1, tmp); 975 PyTuple_SET_ITEM(args, 1, tmp);
741 976
742 Py_INCREF(self->filename); 977 Py_INCREF(self->filename);
743 PyTuple_SET_ITEM(args, 2, self->filename); 978 PyTuple_SET_ITEM(args, 2, self->filename);
744 } else 979 } else
745 Py_INCREF(args); 980 Py_INCREF(args);
746 981
747 if (self->dict) 982 if (self->dict)
748 res = PyTuple_Pack(3, Py_TYPE(self), args, self->dict); 983 res = PyTuple_Pack(3, Py_TYPE(self), args, self->dict);
749 else 984 else
750 res = PyTuple_Pack(2, Py_TYPE(self), args); 985 res = PyTuple_Pack(2, Py_TYPE(self), args);
751 Py_DECREF(args); 986 Py_DECREF(args);
752 return res; 987 return res;
753 } 988 }
754 989
755 990 static PyObject *
756 static PyMethodDef EnvironmentError_methods[] = { 991 OSError_written_get(PyOSErrorObject *self, void *context)
757 {"__reduce__", (PyCFunction)EnvironmentError_reduce, METH_NOARGS}, 992 {
993 if (self->written == -1) {
994 PyErr_SetString(PyExc_AttributeError, "characters_written");
995 return NULL;
996 }
997 return PyLong_FromSsize_t(self->written);
998 }
999
1000 static int
1001 OSError_written_set(PyOSErrorObject *self, PyObject *arg, void *context)
1002 {
1003 Py_ssize_t n;
1004 n = PyNumber_AsSsize_t(arg, PyExc_ValueError);
1005 if (n == -1 && PyErr_Occurred())
1006 return -1;
1007 self->written = n;
1008 return 0;
1009 }
1010
1011 static PyMemberDef OSError_members[] = {
1012 {"errno", T_OBJECT, offsetof(PyOSErrorObject, myerrno), 0,
1013 PyDoc_STR("POSIX exception code")},
1014 {"strerror", T_OBJECT, offsetof(PyOSErrorObject, strerror), 0,
1015 PyDoc_STR("exception strerror")},
1016 {"filename", T_OBJECT, offsetof(PyOSErrorObject, filename), 0,
1017 PyDoc_STR("exception filename")},
1018 #ifdef MS_WINDOWS
1019 {"winerror", T_OBJECT, offsetof(PyOSErrorObject, winerror), 0,
1020 PyDoc_STR("Win32 exception code")},
1021 #endif
1022 {NULL} /* Sentinel */
1023 };
1024
1025 static PyMethodDef OSError_methods[] = {
1026 {"__reduce__", (PyCFunction)OSError_reduce, METH_NOARGS},
758 {NULL} 1027 {NULL}
759 }; 1028 };
760 1029
761 ComplexExtendsException(PyExc_Exception, EnvironmentError, 1030 static PyGetSetDef OSError_getset[] = {
762 EnvironmentError, EnvironmentError_dealloc, 1031 {"characters_written", (getter) OSError_written_get,
763 EnvironmentError_methods, EnvironmentError_members, 1032 (setter) OSError_written_set, NULL},
764 EnvironmentError_str, 1033 {NULL}
1034 };
1035
1036
1037 ComplexExtendsException(PyExc_Exception, OSError,
1038 OSError, OSError_new,
1039 OSError_methods, OSError_members, OSError_getset,
1040 OSError_str,
765 "Base class for I/O related errors."); 1041 "Base class for I/O related errors.");
766 1042
767 1043
768 /* 1044 /*
769 * IOError extends EnvironmentError 1045 * Various OSError subclasses
770 */ 1046 */
771 MiddlingExtendsException(PyExc_EnvironmentError, IOError, 1047 MiddlingExtendsException(PyExc_OSError, BlockingIOError, OSError,
772 EnvironmentError, "I/O operation failed."); 1048 "I/O operation would block.");
773 1049 MiddlingExtendsException(PyExc_OSError, ConnectionError, OSError,
774 1050 "Connection error.");
775 /* 1051 MiddlingExtendsException(PyExc_OSError, ChildProcessError, OSError,
776 * OSError extends EnvironmentError 1052 "Child process error.");
777 */ 1053 MiddlingExtendsException(PyExc_ConnectionError, BrokenPipeError, OSError,
778 MiddlingExtendsException(PyExc_EnvironmentError, OSError, 1054 "Broken pipe.");
779 EnvironmentError, "OS system call failed."); 1055 MiddlingExtendsException(PyExc_ConnectionError, ConnectionAbortedError, OSError,
780 1056 "Connection aborted.");
781 1057 MiddlingExtendsException(PyExc_ConnectionError, ConnectionRefusedError, OSError,
782 /* 1058 "Connection refused.");
783 * WindowsError extends OSError 1059 MiddlingExtendsException(PyExc_ConnectionError, ConnectionResetError, OSError,
784 */ 1060 "Connection reset.");
785 #ifdef MS_WINDOWS 1061 MiddlingExtendsException(PyExc_OSError, FileExistsError, OSError,
786 #include "errmap.h" 1062 "File already exists.");
787 1063 MiddlingExtendsException(PyExc_OSError, FileNotFoundError, OSError,
788 static int 1064 "File not found.");
789 WindowsError_clear(PyWindowsErrorObject *self) 1065 MiddlingExtendsException(PyExc_OSError, IsADirectoryError, OSError,
790 { 1066 "Operation doesn't work on directories.");
791 Py_CLEAR(self->myerrno); 1067 MiddlingExtendsException(PyExc_OSError, NotADirectoryError, OSError,
792 Py_CLEAR(self->strerror); 1068 "Operation only works on directories.");
793 Py_CLEAR(self->filename); 1069 MiddlingExtendsException(PyExc_OSError, InterruptedError, OSError,
794 Py_CLEAR(self->winerror); 1070 "Interrupted by signal.");
795 return BaseException_clear((PyBaseExceptionObject *)self); 1071 MiddlingExtendsException(PyExc_OSError, PermissionError, OSError,
796 } 1072 "Not enough permissions.");
797 1073 MiddlingExtendsException(PyExc_OSError, ProcessLookupError, OSError,
798 static void 1074 "Process not found.");
799 WindowsError_dealloc(PyWindowsErrorObject *self) 1075 MiddlingExtendsException(PyExc_OSError, TimeoutError, OSError,
800 { 1076 "Timeout expired.");
801 _PyObject_GC_UNTRACK(self);
802 WindowsError_clear(self);
803 Py_TYPE(self)->tp_free((PyObject *)self);
804 }
805
806 static int
807 WindowsError_traverse(PyWindowsErrorObject *self, visitproc visit, void *arg)
808 {
809 Py_VISIT(self->myerrno);
810 Py_VISIT(self->strerror);
811 Py_VISIT(self->filename);
812 Py_VISIT(self->winerror);
813 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
814 }
815
816 static int
817 WindowsError_init(PyWindowsErrorObject *self, PyObject *args, PyObject *kwds)
818 {
819 PyObject *o_errcode = NULL;
820 long errcode;
821 long posix_errno;
822
823 if (EnvironmentError_init((PyEnvironmentErrorObject *)self, args, kwds)
824 == -1)
825 return -1;
826
827 if (self->myerrno == NULL)
828 return 0;
829
830 /* Set errno to the POSIX errno, and winerror to the Win32
831 error code. */
832 errcode = PyLong_AsLong(self->myerrno);
833 if (errcode == -1 && PyErr_Occurred())
834 return -1;
835 posix_errno = winerror_to_errno(errcode);
836
837 Py_CLEAR(self->winerror);
838 self->winerror = self->myerrno;
839
840 o_errcode = PyLong_FromLong(posix_errno);
841 if (!o_errcode)
842 return -1;
843
844 self->myerrno = o_errcode;
845
846 return 0;
847 }
848
849
850 static PyObject *
851 WindowsError_str(PyWindowsErrorObject *self)
852 {
853 if (self->filename)
854 return PyUnicode_FromFormat("[Error %S] %S: %R",
855 self->winerror ? self->winerror: Py_None,
856 self->strerror ? self->strerror: Py_None,
857 self->filename);
858 else if (self->winerror && self->strerror)
859 return PyUnicode_FromFormat("[Error %S] %S",
860 self->winerror ? self->winerror: Py_None,
861 self->strerror ? self->strerror: Py_None);
862 else
863 return EnvironmentError_str((PyEnvironmentErrorObject *)self);
864 }
865
866 static PyMemberDef WindowsError_members[] = {
867 {"errno", T_OBJECT, offsetof(PyWindowsErrorObject, myerrno), 0,
868 PyDoc_STR("POSIX exception code")},
869 {"strerror", T_OBJECT, offsetof(PyWindowsErrorObject, strerror), 0,
870 PyDoc_STR("exception strerror")},
871 {"filename", T_OBJECT, offsetof(PyWindowsErrorObject, filename), 0,
872 PyDoc_STR("exception filename")},
873 {"winerror", T_OBJECT, offsetof(PyWindowsErrorObject, winerror), 0,
874 PyDoc_STR("Win32 exception code")},
875 {NULL} /* Sentinel */
876 };
877
878 ComplexExtendsException(PyExc_OSError, WindowsError, WindowsError,
879 WindowsError_dealloc, 0, WindowsError_members,
880 WindowsError_str, "MS-Windows OS system call failed.");
881
882 #endif /* MS_WINDOWS */
883
884
885 /*
886 * VMSError extends OSError (I think)
887 */
888 #ifdef __VMS
889 MiddlingExtendsException(PyExc_OSError, VMSError, EnvironmentError,
890 "OpenVMS OS system call failed.");
891 #endif
892
893 1077
894 /* 1078 /*
895 * EOFError extends Exception 1079 * EOFError extends Exception
896 */ 1080 */
897 SimpleExtendsException(PyExc_Exception, EOFError, 1081 SimpleExtendsException(PyExc_Exception, EOFError,
898 "Read beyond end of file."); 1082 "Read beyond end of file.");
899 1083
900 1084
901 /* 1085 /*
902 * RuntimeError extends Exception 1086 * RuntimeError extends Exception
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
1012 Py_VISIT(self->print_file_and_line); 1196 Py_VISIT(self->print_file_and_line);
1013 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg); 1197 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
1014 } 1198 }
1015 1199
1016 /* This is called "my_basename" instead of just "basename" to avoid name 1200 /* This is called "my_basename" instead of just "basename" to avoid name
1017 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is 1201 conflicts with glibc; basename is already prototyped if _GNU_SOURCE is
1018 defined, and Python does define that. */ 1202 defined, and Python does define that. */
1019 static PyObject* 1203 static PyObject*
1020 my_basename(PyObject *name) 1204 my_basename(PyObject *name)
1021 { 1205 {
1022 Py_UNICODE *unicode;
1023 Py_ssize_t i, size, offset; 1206 Py_ssize_t i, size, offset;
1024 1207 int kind;
1025 unicode = PyUnicode_AS_UNICODE(name); 1208 void *data;
1026 size = PyUnicode_GET_SIZE(name); 1209
1210 if (PyUnicode_READY(name))
1211 return NULL;
1212 kind = PyUnicode_KIND(name);
1213 data = PyUnicode_DATA(name);
1214 size = PyUnicode_GET_LENGTH(name);
1027 offset = 0; 1215 offset = 0;
1028 for(i=0; i < size; i++) { 1216 for(i=0; i < size; i++) {
1029 if (unicode[i] == SEP) 1217 if (PyUnicode_READ(kind, data, i) == SEP)
1030 offset = i + 1; 1218 offset = i + 1;
1031 } 1219 }
1032 if (offset != 0) { 1220 if (offset != 0)
1033 return PyUnicode_FromUnicode( 1221 return PyUnicode_Substring(name, offset, size);
1034 PyUnicode_AS_UNICODE(name) + offset, 1222 else {
1035 size - offset);
1036 } else {
1037 Py_INCREF(name); 1223 Py_INCREF(name);
1038 return name; 1224 return name;
1039 } 1225 }
1040 } 1226 }
1041 1227
1042 1228
1043 static PyObject * 1229 static PyObject *
1044 SyntaxError_str(PySyntaxErrorObject *self) 1230 SyntaxError_str(PySyntaxErrorObject *self)
1045 { 1231 {
1046 int have_lineno = 0; 1232 int have_lineno = 0;
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
1094 PyDoc_STR("exception offset")}, 1280 PyDoc_STR("exception offset")},
1095 {"text", T_OBJECT, offsetof(PySyntaxErrorObject, text), 0, 1281 {"text", T_OBJECT, offsetof(PySyntaxErrorObject, text), 0,
1096 PyDoc_STR("exception text")}, 1282 PyDoc_STR("exception text")},
1097 {"print_file_and_line", T_OBJECT, 1283 {"print_file_and_line", T_OBJECT,
1098 offsetof(PySyntaxErrorObject, print_file_and_line), 0, 1284 offsetof(PySyntaxErrorObject, print_file_and_line), 0,
1099 PyDoc_STR("exception print_file_and_line")}, 1285 PyDoc_STR("exception print_file_and_line")},
1100 {NULL} /* Sentinel */ 1286 {NULL} /* Sentinel */
1101 }; 1287 };
1102 1288
1103 ComplexExtendsException(PyExc_Exception, SyntaxError, SyntaxError, 1289 ComplexExtendsException(PyExc_Exception, SyntaxError, SyntaxError,
1104 SyntaxError_dealloc, 0, SyntaxError_members, 1290 0, 0, SyntaxError_members, 0,
1105 SyntaxError_str, "Invalid syntax."); 1291 SyntaxError_str, "Invalid syntax.");
1106 1292
1107 1293
1108 /* 1294 /*
1109 * IndentationError extends SyntaxError 1295 * IndentationError extends SyntaxError
1110 */ 1296 */
1111 MiddlingExtendsException(PyExc_SyntaxError, IndentationError, SyntaxError, 1297 MiddlingExtendsException(PyExc_SyntaxError, IndentationError, SyntaxError,
1112 "Improper indentation."); 1298 "Improper indentation.");
1113 1299
1114 1300
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
1148 string, that string will be displayed in quotes. Too bad. 1334 string, that string will be displayed in quotes. Too bad.
1149 If args is anything else, use the default BaseException__str__(). 1335 If args is anything else, use the default BaseException__str__().
1150 */ 1336 */
1151 if (PyTuple_GET_SIZE(self->args) == 1) { 1337 if (PyTuple_GET_SIZE(self->args) == 1) {
1152 return PyObject_Repr(PyTuple_GET_ITEM(self->args, 0)); 1338 return PyObject_Repr(PyTuple_GET_ITEM(self->args, 0));
1153 } 1339 }
1154 return BaseException_str(self); 1340 return BaseException_str(self);
1155 } 1341 }
1156 1342
1157 ComplexExtendsException(PyExc_LookupError, KeyError, BaseException, 1343 ComplexExtendsException(PyExc_LookupError, KeyError, BaseException,
1158 0, 0, 0, KeyError_str, "Mapping key not found."); 1344 0, 0, 0, 0, KeyError_str, "Mapping key not found.");
1159 1345
1160 1346
1161 /* 1347 /*
1162 * ValueError extends Exception 1348 * ValueError extends Exception
1163 */ 1349 */
1164 SimpleExtendsException(PyExc_Exception, ValueError, 1350 SimpleExtendsException(PyExc_Exception, ValueError,
1165 "Inappropriate argument value (of correct type)."); 1351 "Inappropriate argument value (of correct type).");
1166 1352
1167 /* 1353 /*
1168 * UnicodeError extends ValueError 1354 * UnicodeError extends ValueError
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
1247 1433
1248 int 1434 int
1249 PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start) 1435 PyUnicodeEncodeError_GetStart(PyObject *exc, Py_ssize_t *start)
1250 { 1436 {
1251 Py_ssize_t size; 1437 Py_ssize_t size;
1252 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object, 1438 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1253 "object"); 1439 "object");
1254 if (!obj) 1440 if (!obj)
1255 return -1; 1441 return -1;
1256 *start = ((PyUnicodeErrorObject *)exc)->start; 1442 *start = ((PyUnicodeErrorObject *)exc)->start;
1257 size = PyUnicode_GET_SIZE(obj); 1443 size = PyUnicode_GET_LENGTH(obj);
1258 if (*start<0) 1444 if (*start<0)
1259 *start = 0; /*XXX check for values <0*/ 1445 *start = 0; /*XXX check for values <0*/
1260 if (*start>=size) 1446 if (*start>=size)
1261 *start = size-1; 1447 *start = size-1;
1262 Py_DECREF(obj); 1448 Py_DECREF(obj);
1263 return 0; 1449 return 0;
1264 } 1450 }
1265 1451
1266 1452
1267 int 1453 int
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
1315 1501
1316 int 1502 int
1317 PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end) 1503 PyUnicodeEncodeError_GetEnd(PyObject *exc, Py_ssize_t *end)
1318 { 1504 {
1319 Py_ssize_t size; 1505 Py_ssize_t size;
1320 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object, 1506 PyObject *obj = get_unicode(((PyUnicodeErrorObject *)exc)->object,
1321 "object"); 1507 "object");
1322 if (!obj) 1508 if (!obj)
1323 return -1; 1509 return -1;
1324 *end = ((PyUnicodeErrorObject *)exc)->end; 1510 *end = ((PyUnicodeErrorObject *)exc)->end;
1325 size = PyUnicode_GET_SIZE(obj); 1511 size = PyUnicode_GET_LENGTH(obj);
1326 if (*end<1) 1512 if (*end<1)
1327 *end = 1; 1513 *end = 1;
1328 if (*end>size) 1514 if (*end>size)
1329 *end = size; 1515 *end = size;
1330 Py_DECREF(obj); 1516 Py_DECREF(obj);
1331 return 0; 1517 return 0;
1332 } 1518 }
1333 1519
1334 1520
1335 int 1521 int
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
1487 if (!PyArg_ParseTuple(args, "O!O!nnO!", 1673 if (!PyArg_ParseTuple(args, "O!O!nnO!",
1488 &PyUnicode_Type, &err->encoding, 1674 &PyUnicode_Type, &err->encoding,
1489 &PyUnicode_Type, &err->object, 1675 &PyUnicode_Type, &err->object,
1490 &err->start, 1676 &err->start,
1491 &err->end, 1677 &err->end,
1492 &PyUnicode_Type, &err->reason)) { 1678 &PyUnicode_Type, &err->reason)) {
1493 err->encoding = err->object = err->reason = NULL; 1679 err->encoding = err->object = err->reason = NULL;
1494 return -1; 1680 return -1;
1495 } 1681 }
1496 1682
1683 if (PyUnicode_READY(err->object) < -1) {
1684 err->encoding = NULL;
1685 return -1;
1686 }
1687
1497 Py_INCREF(err->encoding); 1688 Py_INCREF(err->encoding);
1498 Py_INCREF(err->object); 1689 Py_INCREF(err->object);
1499 Py_INCREF(err->reason); 1690 Py_INCREF(err->reason);
1500 1691
1501 return 0; 1692 return 0;
1502 } 1693 }
1503 1694
1504 static PyObject * 1695 static PyObject *
1505 UnicodeEncodeError_str(PyObject *self) 1696 UnicodeEncodeError_str(PyObject *self)
1506 { 1697 {
1507 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self; 1698 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
1508 PyObject *result = NULL; 1699 PyObject *result = NULL;
1509 PyObject *reason_str = NULL; 1700 PyObject *reason_str = NULL;
1510 PyObject *encoding_str = NULL; 1701 PyObject *encoding_str = NULL;
1511 1702
1512 /* Get reason and encoding as strings, which they might not be if 1703 /* Get reason and encoding as strings, which they might not be if
1513 they've been modified after we were contructed. */ 1704 they've been modified after we were contructed. */
1514 reason_str = PyObject_Str(uself->reason); 1705 reason_str = PyObject_Str(uself->reason);
1515 if (reason_str == NULL) 1706 if (reason_str == NULL)
1516 goto done; 1707 goto done;
1517 encoding_str = PyObject_Str(uself->encoding); 1708 encoding_str = PyObject_Str(uself->encoding);
1518 if (encoding_str == NULL) 1709 if (encoding_str == NULL)
1519 goto done; 1710 goto done;
1520 1711
1521 if (uself->start < PyUnicode_GET_SIZE(uself->object) && uself->end == uself- >start+1) { 1712 if (uself->start < PyUnicode_GET_LENGTH(uself->object) && uself->end == usel f->start+1) {
1522 int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start]; 1713 Py_UCS4 badchar = PyUnicode_ReadChar(uself->object, uself->start);
1523 const char *fmt; 1714 const char *fmt;
1524 if (badchar <= 0xff) 1715 if (badchar <= 0xff)
1525 fmt = "'%U' codec can't encode character '\\x%02x' in position %zd: %U"; 1716 fmt = "'%U' codec can't encode character '\\x%02x' in position %zd: %U";
1526 else if (badchar <= 0xffff) 1717 else if (badchar <= 0xffff)
1527 fmt = "'%U' codec can't encode character '\\u%04x' in position %zd: %U"; 1718 fmt = "'%U' codec can't encode character '\\u%04x' in position %zd: %U";
1528 else 1719 else
1529 fmt = "'%U' codec can't encode character '\\U%08x' in position %zd: %U"; 1720 fmt = "'%U' codec can't encode character '\\U%08x' in position %zd: %U";
1530 result = PyUnicode_FromFormat( 1721 result = PyUnicode_FromFormat(
1531 fmt, 1722 fmt,
1532 encoding_str, 1723 encoding_str,
1533 badchar, 1724 (int)badchar,
1534 uself->start, 1725 uself->start,
1535 reason_str); 1726 reason_str);
1536 } 1727 }
1537 else { 1728 else {
1538 result = PyUnicode_FromFormat( 1729 result = PyUnicode_FromFormat(
1539 "'%U' codec can't encode characters in position %zd-%zd: %U", 1730 "'%U' codec can't encode characters in position %zd-%zd: %U",
1540 encoding_str, 1731 encoding_str,
1541 uself->start, 1732 uself->start,
1542 uself->end-1, 1733 uself->end-1,
1543 reason_str); 1734 reason_str);
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after
1720 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self; 1911 PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
1721 PyObject *result = NULL; 1912 PyObject *result = NULL;
1722 PyObject *reason_str = NULL; 1913 PyObject *reason_str = NULL;
1723 1914
1724 /* Get reason as a string, which it might not be if it's been 1915 /* Get reason as a string, which it might not be if it's been
1725 modified after we were contructed. */ 1916 modified after we were contructed. */
1726 reason_str = PyObject_Str(uself->reason); 1917 reason_str = PyObject_Str(uself->reason);
1727 if (reason_str == NULL) 1918 if (reason_str == NULL)
1728 goto done; 1919 goto done;
1729 1920
1730 if (uself->start < PyUnicode_GET_SIZE(uself->object) && uself->end == uself- >start+1) { 1921 if (uself->start < PyUnicode_GET_LENGTH(uself->object) && uself->end == usel f->start+1) {
1731 int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start]; 1922 Py_UCS4 badchar = PyUnicode_ReadChar(uself->object, uself->start);
1732 const char *fmt; 1923 const char *fmt;
1733 if (badchar <= 0xff) 1924 if (badchar <= 0xff)
1734 fmt = "can't translate character '\\x%02x' in position %zd: %U"; 1925 fmt = "can't translate character '\\x%02x' in position %zd: %U";
1735 else if (badchar <= 0xffff) 1926 else if (badchar <= 0xffff)
1736 fmt = "can't translate character '\\u%04x' in position %zd: %U"; 1927 fmt = "can't translate character '\\u%04x' in position %zd: %U";
1737 else 1928 else
1738 fmt = "can't translate character '\\U%08x' in position %zd: %U"; 1929 fmt = "can't translate character '\\U%08x' in position %zd: %U";
1739 result = PyUnicode_FromFormat( 1930 result = PyUnicode_FromFormat(
1740 fmt, 1931 fmt,
1741 badchar, 1932 (int)badchar,
1742 uself->start, 1933 uself->start,
1743 reason_str 1934 reason_str
1744 ); 1935 );
1745 } else { 1936 } else {
1746 result = PyUnicode_FromFormat( 1937 result = PyUnicode_FromFormat(
1747 "can't translate characters in position %zd-%zd: %U", 1938 "can't translate characters in position %zd-%zd: %U",
1748 uself->start, 1939 uself->start,
1749 uself->end-1, 1940 uself->end-1,
1750 reason_str 1941 reason_str
1751 ); 1942 );
(...skipping 10 matching lines...) Expand all
1762 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1953 (destructor)UnicodeError_dealloc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1763 (reprfunc)UnicodeTranslateError_str, 0, 0, 0, 1954 (reprfunc)UnicodeTranslateError_str, 0, 0, 0,
1764 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, 1955 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
1765 PyDoc_STR("Unicode translation error."), (traverseproc)UnicodeError_traverse , 1956 PyDoc_STR("Unicode translation error."), (traverseproc)UnicodeError_traverse ,
1766 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members, 1957 (inquiry)UnicodeError_clear, 0, 0, 0, 0, 0, UnicodeError_members,
1767 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict), 1958 0, &_PyExc_UnicodeError, 0, 0, 0, offsetof(PyUnicodeErrorObject, dict),
1768 (initproc)UnicodeTranslateError_init, 0, BaseException_new, 1959 (initproc)UnicodeTranslateError_init, 0, BaseException_new,
1769 }; 1960 };
1770 PyObject *PyExc_UnicodeTranslateError = (PyObject *)&_PyExc_UnicodeTranslateErro r; 1961 PyObject *PyExc_UnicodeTranslateError = (PyObject *)&_PyExc_UnicodeTranslateErro r;
1771 1962
1963 /* Deprecated. */
1772 PyObject * 1964 PyObject *
1773 PyUnicodeTranslateError_Create( 1965 PyUnicodeTranslateError_Create(
1774 const Py_UNICODE *object, Py_ssize_t length, 1966 const Py_UNICODE *object, Py_ssize_t length,
1775 Py_ssize_t start, Py_ssize_t end, const char *reason) 1967 Py_ssize_t start, Py_ssize_t end, const char *reason)
1776 { 1968 {
1777 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#nns", 1969 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "u#nns",
1778 object, length, start, end, reason); 1970 object, length, start, end, reason);
1779 } 1971 }
1780 1972
1973 PyObject *
1974 _PyUnicodeTranslateError_Create(
1975 PyObject *object,
1976 Py_ssize_t start, Py_ssize_t end, const char *reason)
1977 {
1978 return PyObject_CallFunction(PyExc_UnicodeTranslateError, "Ons",
1979 object, start, end, reason);
1980 }
1781 1981
1782 /* 1982 /*
1783 * AssertionError extends Exception 1983 * AssertionError extends Exception
1784 */ 1984 */
1785 SimpleExtendsException(PyExc_Exception, AssertionError, 1985 SimpleExtendsException(PyExc_Exception, AssertionError,
1786 "Assertion failed."); 1986 "Assertion failed.");
1787 1987
1788 1988
1789 /* 1989 /*
1790 * ArithmeticError extends Exception 1990 * ArithmeticError extends Exception
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
2016 */ 2216 */
2017 PyObject *PyExc_RecursionErrorInst = NULL; 2217 PyObject *PyExc_RecursionErrorInst = NULL;
2018 2218
2019 #define PRE_INIT(TYPE) if (PyType_Ready(&_PyExc_ ## TYPE) < 0) \ 2219 #define PRE_INIT(TYPE) if (PyType_Ready(&_PyExc_ ## TYPE) < 0) \
2020 Py_FatalError("exceptions bootstrapping error."); 2220 Py_FatalError("exceptions bootstrapping error.");
2021 2221
2022 #define POST_INIT(TYPE) Py_INCREF(PyExc_ ## TYPE); \ 2222 #define POST_INIT(TYPE) Py_INCREF(PyExc_ ## TYPE); \
2023 if (PyDict_SetItemString(bdict, # TYPE, PyExc_ ## TYPE)) \ 2223 if (PyDict_SetItemString(bdict, # TYPE, PyExc_ ## TYPE)) \
2024 Py_FatalError("Module dictionary insertion problem."); 2224 Py_FatalError("Module dictionary insertion problem.");
2025 2225
2226 #define INIT_ALIAS(NAME, TYPE) Py_INCREF(PyExc_ ## TYPE); \
2227 PyExc_ ## NAME = PyExc_ ## TYPE; \
2228 if (PyDict_SetItemString(bdict, # NAME, PyExc_ ## NAME)) \
2229 Py_FatalError("Module dictionary insertion problem.");
2230
2231 #define ADD_ERRNO(TYPE, CODE) { \
2232 PyObject *_code = PyLong_FromLong(CODE); \
2233 assert(_PyObject_RealIsSubclass(PyExc_ ## TYPE, PyExc_OSError)); \
2234 if (!_code || PyDict_SetItem(errnomap, _code, PyExc_ ## TYPE)) \
2235 Py_FatalError("errmap insertion problem."); \
2236 }
2237
2238 #ifdef MS_WINDOWS
2239 #include <Winsock2.h>
2240 #if defined(WSAEALREADY) && !defined(EALREADY)
2241 #define EALREADY WSAEALREADY
2242 #endif
2243 #if defined(WSAECONNABORTED) && !defined(ECONNABORTED)
2244 #define ECONNABORTED WSAECONNABORTED
2245 #endif
2246 #if defined(WSAECONNREFUSED) && !defined(ECONNREFUSED)
2247 #define ECONNREFUSED WSAECONNREFUSED
2248 #endif
2249 #if defined(WSAECONNRESET) && !defined(ECONNRESET)
2250 #define ECONNRESET WSAECONNRESET
2251 #endif
2252 #if defined(WSAEINPROGRESS) && !defined(EINPROGRESS)
2253 #define EINPROGRESS WSAEINPROGRESS
2254 #endif
2255 #if defined(WSAESHUTDOWN) && !defined(ESHUTDOWN)
2256 #define ESHUTDOWN WSAESHUTDOWN
2257 #endif
2258 #if defined(WSAETIMEDOUT) && !defined(ETIMEDOUT)
2259 #define ETIMEDOUT WSAETIMEDOUT
2260 #endif
2261 #if defined(WSAEWOULDBLOCK) && !defined(EWOULDBLOCK)
2262 #define EWOULDBLOCK WSAEWOULDBLOCK
2263 #endif
2264 #endif /* MS_WINDOWS */
2026 2265
2027 void 2266 void
2028 _PyExc_Init(void) 2267 _PyExc_Init(void)
2029 { 2268 {
2030 PyObject *bltinmod, *bdict; 2269 PyObject *bltinmod, *bdict;
2031 2270
2032 PRE_INIT(BaseException) 2271 PRE_INIT(BaseException)
2033 PRE_INIT(Exception) 2272 PRE_INIT(Exception)
2034 PRE_INIT(TypeError) 2273 PRE_INIT(TypeError)
2035 PRE_INIT(StopIteration) 2274 PRE_INIT(StopIteration)
2036 PRE_INIT(GeneratorExit) 2275 PRE_INIT(GeneratorExit)
2037 PRE_INIT(SystemExit) 2276 PRE_INIT(SystemExit)
2038 PRE_INIT(KeyboardInterrupt) 2277 PRE_INIT(KeyboardInterrupt)
2039 PRE_INIT(ImportError) 2278 PRE_INIT(ImportError)
2040 PRE_INIT(EnvironmentError)
2041 PRE_INIT(IOError)
2042 PRE_INIT(OSError) 2279 PRE_INIT(OSError)
2043 #ifdef MS_WINDOWS
2044 PRE_INIT(WindowsError)
2045 #endif
2046 #ifdef __VMS
2047 PRE_INIT(VMSError)
2048 #endif
2049 PRE_INIT(EOFError) 2280 PRE_INIT(EOFError)
2050 PRE_INIT(RuntimeError) 2281 PRE_INIT(RuntimeError)
2051 PRE_INIT(NotImplementedError) 2282 PRE_INIT(NotImplementedError)
2052 PRE_INIT(NameError) 2283 PRE_INIT(NameError)
2053 PRE_INIT(UnboundLocalError) 2284 PRE_INIT(UnboundLocalError)
2054 PRE_INIT(AttributeError) 2285 PRE_INIT(AttributeError)
2055 PRE_INIT(SyntaxError) 2286 PRE_INIT(SyntaxError)
2056 PRE_INIT(IndentationError) 2287 PRE_INIT(IndentationError)
2057 PRE_INIT(TabError) 2288 PRE_INIT(TabError)
2058 PRE_INIT(LookupError) 2289 PRE_INIT(LookupError)
(...skipping 19 matching lines...) Expand all
2078 PRE_INIT(DeprecationWarning) 2309 PRE_INIT(DeprecationWarning)
2079 PRE_INIT(PendingDeprecationWarning) 2310 PRE_INIT(PendingDeprecationWarning)
2080 PRE_INIT(SyntaxWarning) 2311 PRE_INIT(SyntaxWarning)
2081 PRE_INIT(RuntimeWarning) 2312 PRE_INIT(RuntimeWarning)
2082 PRE_INIT(FutureWarning) 2313 PRE_INIT(FutureWarning)
2083 PRE_INIT(ImportWarning) 2314 PRE_INIT(ImportWarning)
2084 PRE_INIT(UnicodeWarning) 2315 PRE_INIT(UnicodeWarning)
2085 PRE_INIT(BytesWarning) 2316 PRE_INIT(BytesWarning)
2086 PRE_INIT(ResourceWarning) 2317 PRE_INIT(ResourceWarning)
2087 2318
2319 /* OSError subclasses */
2320 PRE_INIT(ConnectionError);
2321
2322 PRE_INIT(BlockingIOError);
2323 PRE_INIT(BrokenPipeError);
2324 PRE_INIT(ChildProcessError);
2325 PRE_INIT(ConnectionAbortedError);
2326 PRE_INIT(ConnectionRefusedError);
2327 PRE_INIT(ConnectionResetError);
2328 PRE_INIT(FileExistsError);
2329 PRE_INIT(FileNotFoundError);
2330 PRE_INIT(IsADirectoryError);
2331 PRE_INIT(NotADirectoryError);
2332 PRE_INIT(InterruptedError);
2333 PRE_INIT(PermissionError);
2334 PRE_INIT(ProcessLookupError);
2335 PRE_INIT(TimeoutError);
2336
2088 bltinmod = PyImport_ImportModule("builtins"); 2337 bltinmod = PyImport_ImportModule("builtins");
2089 if (bltinmod == NULL) 2338 if (bltinmod == NULL)
2090 Py_FatalError("exceptions bootstrapping error."); 2339 Py_FatalError("exceptions bootstrapping error.");
2091 bdict = PyModule_GetDict(bltinmod); 2340 bdict = PyModule_GetDict(bltinmod);
2092 if (bdict == NULL) 2341 if (bdict == NULL)
2093 Py_FatalError("exceptions bootstrapping error."); 2342 Py_FatalError("exceptions bootstrapping error.");
2094 2343
2095 POST_INIT(BaseException) 2344 POST_INIT(BaseException)
2096 POST_INIT(Exception) 2345 POST_INIT(Exception)
2097 POST_INIT(TypeError) 2346 POST_INIT(TypeError)
2098 POST_INIT(StopIteration) 2347 POST_INIT(StopIteration)
2099 POST_INIT(GeneratorExit) 2348 POST_INIT(GeneratorExit)
2100 POST_INIT(SystemExit) 2349 POST_INIT(SystemExit)
2101 POST_INIT(KeyboardInterrupt) 2350 POST_INIT(KeyboardInterrupt)
2102 POST_INIT(ImportError) 2351 POST_INIT(ImportError)
2103 POST_INIT(EnvironmentError)
2104 POST_INIT(IOError)
2105 POST_INIT(OSError) 2352 POST_INIT(OSError)
2353 INIT_ALIAS(EnvironmentError, OSError)
2354 INIT_ALIAS(IOError, OSError)
2106 #ifdef MS_WINDOWS 2355 #ifdef MS_WINDOWS
2107 POST_INIT(WindowsError) 2356 INIT_ALIAS(WindowsError, OSError)
2108 #endif 2357 #endif
2109 #ifdef __VMS 2358 #ifdef __VMS
2110 POST_INIT(VMSError) 2359 INIT_ALIAS(VMSError, OSError)
2111 #endif 2360 #endif
2112 POST_INIT(EOFError) 2361 POST_INIT(EOFError)
2113 POST_INIT(RuntimeError) 2362 POST_INIT(RuntimeError)
2114 POST_INIT(NotImplementedError) 2363 POST_INIT(NotImplementedError)
2115 POST_INIT(NameError) 2364 POST_INIT(NameError)
2116 POST_INIT(UnboundLocalError) 2365 POST_INIT(UnboundLocalError)
2117 POST_INIT(AttributeError) 2366 POST_INIT(AttributeError)
2118 POST_INIT(SyntaxError) 2367 POST_INIT(SyntaxError)
2119 POST_INIT(IndentationError) 2368 POST_INIT(IndentationError)
2120 POST_INIT(TabError) 2369 POST_INIT(TabError)
(...skipping 20 matching lines...) Expand all
2141 POST_INIT(DeprecationWarning) 2390 POST_INIT(DeprecationWarning)
2142 POST_INIT(PendingDeprecationWarning) 2391 POST_INIT(PendingDeprecationWarning)
2143 POST_INIT(SyntaxWarning) 2392 POST_INIT(SyntaxWarning)
2144 POST_INIT(RuntimeWarning) 2393 POST_INIT(RuntimeWarning)
2145 POST_INIT(FutureWarning) 2394 POST_INIT(FutureWarning)
2146 POST_INIT(ImportWarning) 2395 POST_INIT(ImportWarning)
2147 POST_INIT(UnicodeWarning) 2396 POST_INIT(UnicodeWarning)
2148 POST_INIT(BytesWarning) 2397 POST_INIT(BytesWarning)
2149 POST_INIT(ResourceWarning) 2398 POST_INIT(ResourceWarning)
2150 2399
2400 errnomap = PyDict_New();
2401 if (!errnomap)
2402 Py_FatalError("Cannot allocate map from errnos to OSError subclasses");
2403
2404 /* OSError subclasses */
2405 POST_INIT(ConnectionError);
2406
2407 POST_INIT(BlockingIOError);
2408 ADD_ERRNO(BlockingIOError, EAGAIN);
2409 ADD_ERRNO(BlockingIOError, EALREADY);
2410 ADD_ERRNO(BlockingIOError, EINPROGRESS);
2411 ADD_ERRNO(BlockingIOError, EWOULDBLOCK);
2412 POST_INIT(BrokenPipeError);
2413 ADD_ERRNO(BrokenPipeError, EPIPE);
2414 ADD_ERRNO(BrokenPipeError, ESHUTDOWN);
2415 POST_INIT(ChildProcessError);
2416 ADD_ERRNO(ChildProcessError, ECHILD);
2417 POST_INIT(ConnectionAbortedError);
2418 ADD_ERRNO(ConnectionAbortedError, ECONNABORTED);
2419 POST_INIT(ConnectionRefusedError);
2420 ADD_ERRNO(ConnectionRefusedError, ECONNREFUSED);
2421 POST_INIT(ConnectionResetError);
2422 ADD_ERRNO(ConnectionResetError, ECONNRESET);
2423 POST_INIT(FileExistsError);
2424 ADD_ERRNO(FileExistsError, EEXIST);
2425 POST_INIT(FileNotFoundError);
2426 ADD_ERRNO(FileNotFoundError, ENOENT);
2427 POST_INIT(IsADirectoryError);
2428 ADD_ERRNO(IsADirectoryError, EISDIR);
2429 POST_INIT(NotADirectoryError);
2430 ADD_ERRNO(NotADirectoryError, ENOTDIR);
2431 POST_INIT(InterruptedError);
2432 ADD_ERRNO(InterruptedError, EINTR);
2433 POST_INIT(PermissionError);
2434 ADD_ERRNO(PermissionError, EACCES);
2435 ADD_ERRNO(PermissionError, EPERM);
2436 POST_INIT(ProcessLookupError);
2437 ADD_ERRNO(ProcessLookupError, ESRCH);
2438 POST_INIT(TimeoutError);
2439 ADD_ERRNO(TimeoutError, ETIMEDOUT);
2440
2151 preallocate_memerrors(); 2441 preallocate_memerrors();
2152 2442
2153 PyExc_RecursionErrorInst = BaseException_new(&_PyExc_RuntimeError, NULL, NUL L); 2443 PyExc_RecursionErrorInst = BaseException_new(&_PyExc_RuntimeError, NULL, NUL L);
2154 if (!PyExc_RecursionErrorInst) 2444 if (!PyExc_RecursionErrorInst)
2155 Py_FatalError("Cannot pre-allocate RuntimeError instance for " 2445 Py_FatalError("Cannot pre-allocate RuntimeError instance for "
2156 "recursion errors"); 2446 "recursion errors");
2157 else { 2447 else {
2158 PyBaseExceptionObject *err_inst = 2448 PyBaseExceptionObject *err_inst =
2159 (PyBaseExceptionObject *)PyExc_RecursionErrorInst; 2449 (PyBaseExceptionObject *)PyExc_RecursionErrorInst;
2160 PyObject *args_tuple; 2450 PyObject *args_tuple;
(...skipping 13 matching lines...) Expand all
2174 } 2464 }
2175 2465
2176 Py_DECREF(bltinmod); 2466 Py_DECREF(bltinmod);
2177 } 2467 }
2178 2468
2179 void 2469 void
2180 _PyExc_Fini(void) 2470 _PyExc_Fini(void)
2181 { 2471 {
2182 Py_CLEAR(PyExc_RecursionErrorInst); 2472 Py_CLEAR(PyExc_RecursionErrorInst);
2183 free_preallocated_memerrors(); 2473 free_preallocated_memerrors();
2184 } 2474 Py_CLEAR(errnomap);
2475 }
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+