summaryrefslogtreecommitdiff
path: root/Python/errors.c
diff options
context:
space:
mode:
authorBrett Cannon <brett@python.org>2013-06-12 23:29:18 -0400
committerBrett Cannon <brett@python.org>2013-06-12 23:29:18 -0400
commit21d4e9534aceac9c2753b70bffe4f75ff38ec525 (patch)
tree4147026bee17d095226b7699eaf98bb23ac81130 /Python/errors.c
parentbd56c1f9e49e523c1abc8446ed19d63e2cbe0672 (diff)
downloadcpython-21d4e9534aceac9c2753b70bffe4f75ff38ec525.tar.gz
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.
Diffstat (limited to 'Python/errors.c')
-rw-r--r--Python/errors.c25
1 files changed, 22 insertions, 3 deletions
diff --git a/Python/errors.c b/Python/errors.c
index 1f955b54f0..89021aadd6 100644
--- a/Python/errors.c
+++ b/Python/errors.c
@@ -619,12 +619,25 @@ PyObject *PyErr_SetFromWindowsErrWithUnicodeFilename(
#endif /* MS_WINDOWS */
PyObject *
-PyErr_SetImportError(PyObject *msg, PyObject *name, PyObject *path)
+PyErr_SetImportErrorSubclass(PyObject *exception, PyObject *msg,
+ PyObject *name, PyObject *path)
{
+ int issubclass;
PyObject *args, *kwargs, *error;
- if (msg == NULL)
+ issubclass = PyObject_IsSubclass(exception, PyExc_ImportError);
+ if (issubclass < 0) {
+ return NULL;
+ }
+ else if (!issubclass) {
+ PyErr_SetString(PyExc_TypeError, "expected a subclass of ImportError");
+ return NULL;
+ }
+
+ if (msg == NULL) {
+ PyErr_SetString(PyExc_TypeError, "expected a message argument");
return NULL;
+ }
args = PyTuple_New(1);
if (args == NULL)
@@ -649,7 +662,7 @@ PyErr_SetImportError(PyObject *msg, PyObject *name, PyObject *path)
PyDict_SetItemString(kwargs, "name", name);
PyDict_SetItemString(kwargs, "path", path);
- error = PyObject_Call(PyExc_ImportError, args, kwargs);
+ error = PyObject_Call(exception, args, kwargs);
if (error != NULL) {
PyErr_SetObject((PyObject *)Py_TYPE(error), error);
Py_DECREF(error);
@@ -661,6 +674,12 @@ PyErr_SetImportError(PyObject *msg, PyObject *name, PyObject *path)
return NULL;
}
+PyObject *
+PyErr_SetImportError(PyObject *msg, PyObject *name, PyObject *path)
+{
+ return PyErr_SetImportErrorSubclass(PyExc_ImportError, msg, name, path);
+}
+
void
_PyErr_BadInternalCall(const char *filename, int lineno)
{