comparison Python/errors.c @ 84109:3a50025f1900

Issue #15767: Touch up ModuleNotFoundError usage by import. Forgot to raise ModuleNotFoundError when None is found in sys.modules. This led to introducing the C function PyErr_SetImportErrorSubclass() to make setting ModuleNotFoundError easier. Also updated the reference docs to mention ModuleNotFoundError appropriately. Updated the docs for ModuleNotFoundError to mention the None in sys.modules case. Lastly, it was noticed that PyErr_SetImportError() was not setting an exception when returning None in one case. That issue is now fixed.
author Brett Cannon <brett@python.org>
date Wed, 12 Jun 2013 23:29:18 -0400
parents bb94f6222fef
children e3ec8b176a80
comparison
equal deleted inserted replaced
84108:05f7883ee92d 84109:3a50025f1900
617 return result; 617 return result;
618 } 618 }
619 #endif /* MS_WINDOWS */ 619 #endif /* MS_WINDOWS */
620 620
621 PyObject * 621 PyObject *
622 PyErr_SetImportError(PyObject *msg, PyObject *name, PyObject *path) 622 PyErr_SetImportErrorSubclass(PyObject *exception, PyObject *msg,
623 { 623 PyObject *name, PyObject *path)
624 {
625 int issubclass;
624 PyObject *args, *kwargs, *error; 626 PyObject *args, *kwargs, *error;
625 627
626 if (msg == NULL) 628 issubclass = PyObject_IsSubclass(exception, PyExc_ImportError);
629 if (issubclass < 0) {
627 return NULL; 630 return NULL;
631 }
632 else if (!issubclass) {
633 PyErr_SetString(PyExc_TypeError, "expected a subclass of ImportError");
634 return NULL;
635 }
636
637 if (msg == NULL) {
638 PyErr_SetString(PyExc_TypeError, "expected a message argument");
639 return NULL;
640 }
628 641
629 args = PyTuple_New(1); 642 args = PyTuple_New(1);
630 if (args == NULL) 643 if (args == NULL)
631 return NULL; 644 return NULL;
632 645
647 Py_INCREF(msg); 660 Py_INCREF(msg);
648 PyTuple_SET_ITEM(args, 0, msg); 661 PyTuple_SET_ITEM(args, 0, msg);
649 PyDict_SetItemString(kwargs, "name", name); 662 PyDict_SetItemString(kwargs, "name", name);
650 PyDict_SetItemString(kwargs, "path", path); 663 PyDict_SetItemString(kwargs, "path", path);
651 664
652 error = PyObject_Call(PyExc_ImportError, args, kwargs); 665 error = PyObject_Call(exception, args, kwargs);
653 if (error != NULL) { 666 if (error != NULL) {
654 PyErr_SetObject((PyObject *)Py_TYPE(error), error); 667 PyErr_SetObject((PyObject *)Py_TYPE(error), error);
655 Py_DECREF(error); 668 Py_DECREF(error);
656 } 669 }
657 670
658 Py_DECREF(args); 671 Py_DECREF(args);
659 Py_DECREF(kwargs); 672 Py_DECREF(kwargs);
660 673
661 return NULL; 674 return NULL;
675 }
676
677 PyObject *
678 PyErr_SetImportError(PyObject *msg, PyObject *name, PyObject *path)
679 {
680 return PyErr_SetImportErrorSubclass(PyExc_ImportError, msg, name, path);
662 } 681 }
663 682
664 void 683 void
665 _PyErr_BadInternalCall(const char *filename, int lineno) 684 _PyErr_BadInternalCall(const char *filename, int lineno)
666 { 685 {