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

Side by Side Diff: Objects/exceptions.c

Issue 1559549: ImportError needs attributes for module and file name
Patch Set: Created 2 years, 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_import.py ('k') | Python/import.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 529 matching lines...) Expand 10 before | Expand all | Expand 10 after
540 /* 540 /*
541 * KeyboardInterrupt extends BaseException 541 * KeyboardInterrupt extends BaseException
542 */ 542 */
543 SimpleExtendsException(PyExc_BaseException, KeyboardInterrupt, 543 SimpleExtendsException(PyExc_BaseException, KeyboardInterrupt,
544 "Program interrupted by user."); 544 "Program interrupted by user.");
545 545
546 546
547 /* 547 /*
548 * ImportError extends Exception 548 * ImportError extends Exception
549 */ 549 */
550 SimpleExtendsException(PyExc_Exception, ImportError,
551 "Import can't find module, or can't find name in module.");
552 550
551 static int
552 ImportError_init(PyImportErrorObject *self, PyObject *args, PyObject *kwds)
553 {
554 PyObject *msg = NULL;
555 PyObject *module_name = NULL;
556
557 if (kwds){
558 module_name = PyDict_GetItemString(kwds, "module_name");
559 if (module_name){
560 Py_CLEAR(self->module_name); /* replacing */
561 self->module_name = module_name;
562 Py_INCREF(self->module_name);
563 if (PyDict_DelItemString(kwds, "module_name"))
564 return -1;
565 }
566 }
567
568 if (BaseException_init((PyBaseExceptionObject *)self, args, kwds) == -1)
569 return -1;
570 if (PyTuple_GET_SIZE(args) != 1)
571 return 0;
572 if (!PyArg_UnpackTuple(args, "ImportError", 1, 1, &msg))
573 return -1;
574
575 Py_CLEAR(self->msg); /* replacing */
576 self->msg = msg;
577 Py_INCREF(self->msg);
578
579 return 0;
580 }
581
582 static int
583 ImportError_clear(PyImportErrorObject *self)
584 {
585 Py_CLEAR(self->msg);
586 Py_CLEAR(self->module_name);
587 return BaseException_clear((PyBaseExceptionObject *)self);
588 }
589
590 static void
591 ImportError_dealloc(PyImportErrorObject *self)
592 {
593 _PyObject_GC_UNTRACK(self);
594 ImportError_clear(self);
595 Py_TYPE(self)->tp_free((PyObject *)self);
596 }
597
598 static int
599 ImportError_traverse(PyImportErrorObject *self, visitproc visit, void *arg)
600 {
601 Py_VISIT(self->msg);
602 Py_VISIT(self->module_name);
603 return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
604 }
605
606 static PyObject *
607 ImportError_str(PyImportErrorObject *self)
608 {
609 if (self->msg) {
610 Py_INCREF(self->msg);
611 return self->msg;
612 }
613 else
614 return BaseException_str((PyBaseExceptionObject *)self);
615 }
616
617 static PyMemberDef ImportError_members[] = {
618 {"msg", T_OBJECT, offsetof(PyImportErrorObject, msg), 0,
619 PyDoc_STR("exception message")},
620 {"module_name", T_OBJECT, offsetof(PyImportErrorObject, module_name), 0,
621 PyDoc_STR("exception module_name")},
622 {NULL} /* Sentinel */
623 };
624
625 static PyMethodDef ImportError_methods[] = {
626 {NULL}
627 };
628
629 ComplexExtendsException(PyExc_Exception, ImportError,
630 ImportError, ImportError_dealloc,
631 ImportError_methods, ImportError_members,
632 ImportError_str,
633 "Import can't find module, or can't find name in "
634 "module.");
553 635
554 /* 636 /*
555 * EnvironmentError extends Exception 637 * EnvironmentError extends Exception
556 */ 638 */
557 639
558 /* Where a function has a single filename, such as open() or some 640 /* Where a function has a single filename, such as open() or some
559 * of the os module functions, PyErr_SetFromErrnoWithFilename() is 641 * of the os module functions, PyErr_SetFromErrnoWithFilename() is
560 * called, giving a third argument which is the filename. But, so 642 * called, giving a third argument which is the filename. But, so
561 * that old code using in-place unpacking doesn't break, e.g.: 643 * that old code using in-place unpacking doesn't break, e.g.:
562 * 644 *
(...skipping 1555 matching lines...) Expand 10 before | Expand all | Expand 10 after
2118 2200
2119 Py_DECREF(bltinmod); 2201 Py_DECREF(bltinmod);
2120 } 2202 }
2121 2203
2122 void 2204 void
2123 _PyExc_Fini(void) 2205 _PyExc_Fini(void)
2124 { 2206 {
2125 Py_CLEAR(PyExc_RecursionErrorInst); 2207 Py_CLEAR(PyExc_RecursionErrorInst);
2126 free_preallocated_memerrors(); 2208 free_preallocated_memerrors();
2127 } 2209 }
OLDNEW
« no previous file with comments | « Lib/test/test_import.py ('k') | Python/import.c » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld cbc36f91f3f7