diff --git a/Doc/c-api/exceptions.rst b/Doc/c-api/exceptions.rst --- a/Doc/c-api/exceptions.rst +++ b/Doc/c-api/exceptions.rst @@ -235,18 +235,12 @@ *kwargs* values. If *args* is ``NULL``, an empty :func:`tuple` will be created when *exc* is created via :c:func:`PyObject_Call`. -.. c:function:: PyObject* PyErr_SetFromImportErrorWithName(PyObject *msg, PyObject *name) +.. c:function:: PyObject* PyErr_SetImportError(PyObject *msg, PyObject *name, PyObject *path) This is a convenience function to raise :exc:`ImportError`. *msg* will be - set as the exception's message string, and *name* will be set as the - :exc:`ImportError`'s ``name`` attribute. - -.. c:function:: PyObject* PyErr_SetFromImportErrorWithNameAndPath(PyObject *msg, PyObject *name, PyObject *path) - - This is a convenience function to raise :exc:`ImportError`. *msg* will be - set as the exception's message string. Both *name* and *path* will be set - as the :exc:`ImportError`'s respective ``name`` and ``path`` attributes. - + set as the exception's message string. *path* can be ``NULL``. Both *name* + and *path* will be set as the :exc:`ImportError`'s respective ``name`` + and ``path`` attributes. .. c:function:: void PyErr_SyntaxLocationEx(char *filename, int lineno, int col_offset) diff --git a/Include/pyerrors.h b/Include/pyerrors.h --- a/Include/pyerrors.h +++ b/Include/pyerrors.h @@ -265,9 +265,8 @@ PyAPI_FUNC(PyObject *) PyErr_SetExcWithArgsKwargs(PyObject *, PyObject *, PyObject *); -PyAPI_FUNC(PyObject *) PyErr_SetFromImportErrorWithNameAndPath(PyObject *, - PyObject *, PyObject *); -PyAPI_FUNC(PyObject *) PyErr_SetFromImportErrorWithName(PyObject *, PyObject *); +PyAPI_FUNC(PyObject *) PyErr_SetImportError(PyObject *, PyObject *, + PyObject *); /* Export the old function so that the existing API remains available: */ PyAPI_FUNC(void) PyErr_BadInternalCall(void); diff --git a/Python/dynload_win.c b/Python/dynload_win.c --- a/Python/dynload_win.c +++ b/Python/dynload_win.c @@ -254,9 +254,9 @@ theLength)); } if (message != NULL) { - PyErr_SetFromImportErrorWithNameAndPath(message, - PyUnicode_FromString(shortname), - pathname); + PyErr_SetImportError(message, PyUnicode_FromString(shortname), + pathname); + Py_DECREF(message); } return NULL; } else { diff --git a/Python/errors.c b/Python/errors.c --- a/Python/errors.c +++ b/Python/errors.c @@ -604,8 +604,7 @@ } PyObject * -PyErr_SetFromImportErrorWithNameAndPath(PyObject *msg, - PyObject *name, PyObject *path) +PyErr_SetImportError(PyObject *msg, PyObject *name, PyObject *path) { PyObject *args = PyTuple_New(1); PyObject *kwargs = PyDict_New(); @@ -614,6 +613,7 @@ if (path == NULL) path = Py_None; + Py_INCREF(msg); PyTuple_SetItem(args, 0, msg); PyDict_SetItemString(kwargs, "name", name); PyDict_SetItemString(kwargs, "path", path); @@ -626,12 +626,6 @@ return result; } -PyObject * -PyErr_SetFromImportErrorWithName(PyObject *msg, PyObject *name) -{ - return PyErr_SetFromImportErrorWithNameAndPath(msg, name, NULL); -} - void _PyErr_BadInternalCall(const char *filename, int lineno) { diff --git a/Python/import.c b/Python/import.c --- a/Python/import.c +++ b/Python/import.c @@ -2826,7 +2826,8 @@ PyObject *msg = PyUnicode_FromFormat("import of %R halted; " "None in sys.modules", abs_name); if (msg != NULL) { - PyErr_SetFromImportErrorWithName(msg, abs_name); + PyErr_SetImportError(msg, abs_name, NULL); + Py_DECREF(msg); } mod = NULL; goto error_with_unlock;