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

Side by Side Diff: Objects/exceptions.c

Issue 1559549: ImportError needs attributes for module and file name
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:
View unified diff | Download patch
« no previous file with comments | « Lib/test/test_pydoc.py ('k') | Python/errors.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
(...skipping 610 matching lines...) Expand 10 before | Expand all | Expand 10 after
621 /* 621 /*
622 * KeyboardInterrupt extends BaseException 622 * KeyboardInterrupt extends BaseException
623 */ 623 */
624 SimpleExtendsException(PyExc_BaseException, KeyboardInterrupt, 624 SimpleExtendsException(PyExc_BaseException, KeyboardInterrupt,
625 "Program interrupted by user."); 625 "Program interrupted by user.");
626 626
627 627
628 /* 628 /*
629 * ImportError extends Exception 629 * ImportError extends Exception
630 */ 630 */
631 SimpleExtendsException(PyExc_Exception, ImportError,
632 "Import can't find module, or can't find name in module.");
633 631
632 static int
633 ImportError_init(PyImportErrorObject *self, PyObject *args, PyObject *kwds)
634 {
635 PyObject *msg = NULL;
636 PyObject *name = NULL;
637 PyObject *path = NULL;
638
639 /* Macro replacement doesn't allow ## to start the first line of a macro,
640 so we move the assignment and NULL check into the if-statement. */
641 #define GET_KWD(kwd) { \
642 kwd = PyDict_GetItemString(kwds, #kwd); \
643 if (kwd) { \
644 Py_CLEAR(self->kwd); \
645 self->kwd = kwd; \
646 Py_INCREF(self->kwd);\
647 if (PyDict_DelItemString(kwds, #kwd)) \
648 return -1; \
649 } \
650 }
651
652 if (kwds) {
653 GET_KWD(name);
654 GET_KWD(path);
655 }
656
657 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
658 return -1;
659 if (PyTuple_GET_SIZE(args) != 1)
660 return 0;
661 if (!PyArg_UnpackTuple(args, "ImportError", 1, 1, &msg))
662 return -1;
663
664 Py_CLEAR(self->msg); /* replacing */
665 self->msg = msg;
666 Py_INCREF(self->msg);
667
668 return 0;
669 }
670
671 static int
672 ImportError_clear(PyImportErrorObject *self)
673 {
674 Py_CLEAR(self->msg);
675 Py_CLEAR(self->name);
676 Py_CLEAR(self->path);
677 return BaseException_clear((PyBaseExceptionObject *)self);
678 }
679
680 static void
681 ImportError_dealloc(PyImportErrorObject *self)
682 {
683 _PyObject_GC_UNTRACK(self);
684 ImportError_clear(self);
685 Py_TYPE(self)->tp_free((PyObject *)self);
686 }
687
688 static int
689 ImportError_traverse(PyImportErrorObject *self, visitproc visit, void *arg)
690 {
691 Py_VISIT(self->msg);
692 Py_VISIT(self->name);
693 Py_VISIT(self->path);
694 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
695 }
696
697 static PyObject *
698 ImportError_str(PyImportErrorObject *self)
699 {
700 if (self->msg) {
701 Py_INCREF(self->msg);
702 return self->msg;
703 }
704 else {
705 return BaseException_str((PyBaseExceptionObject *)self);
706 }
707 }
708
709 static PyMemberDef ImportError_members[] = {
710 {"msg", T_OBJECT, offsetof(PyImportErrorObject, msg), 0,
711 PyDoc_STR("exception message")},
712 {"name", T_OBJECT, offsetof(PyImportErrorObject, name), 0,
713 PyDoc_STR("module name")},
714 {"path", T_OBJECT, offsetof(PyImportErrorObject, path), 0,
715 PyDoc_STR("module path")},
716 {NULL} /* Sentinel */
717 };
718
719 static PyMethodDef ImportError_methods[] = {
720 {NULL}
721 };
722
723 ComplexExtendsException(PyExc_Exception, ImportError,
724 ImportError, 0 /* new */,
725 ImportError_methods, ImportError_members,
726 0 /* getset */, ImportError_str,
727 "Import can't find module, or can't find name in "
728 "module.");
634 729
635 /* 730 /*
636 * OSError extends Exception 731 * OSError extends Exception
637 */ 732 */
638 733
639 #ifdef MS_WINDOWS 734 #ifdef MS_WINDOWS
640 #include "errmap.h" 735 #include "errmap.h"
641 #endif 736 #endif
642 737
643 /* 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
(...skipping 1833 matching lines...) Expand 10 before | Expand all | Expand 10 after
2477 Py_DECREF(bltinmod); 2572 Py_DECREF(bltinmod);
2478 } 2573 }
2479 2574
2480 void 2575 void
2481 _PyExc_Fini(void) 2576 _PyExc_Fini(void)
2482 { 2577 {
2483 Py_CLEAR(PyExc_RecursionErrorInst); 2578 Py_CLEAR(PyExc_RecursionErrorInst);
2484 free_preallocated_memerrors(); 2579 free_preallocated_memerrors();
2485 Py_CLEAR(errnomap); 2580 Py_CLEAR(errnomap);
2486 } 2581 }
OLDNEW
« no previous file with comments | « Lib/test/test_pydoc.py ('k') | Python/errors.c » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld cbc36f91f3f7