]> granicus.if.org Git - python/commitdiff
Fix #14600. Correct reference handling and naming of ImportError convenience function
authorBrian Curtin <brian@python.org>
Tue, 17 Apr 2012 21:57:09 +0000 (16:57 -0500)
committerBrian Curtin <brian@python.org>
Tue, 17 Apr 2012 21:57:09 +0000 (16:57 -0500)
Doc/c-api/exceptions.rst
Include/pyerrors.h
Python/dynload_win.c
Python/errors.c
Python/import.c

index d081ff69ff2cb4edad4fec17ebdfcab29fbe1f07..877249bc6357e44be9658e503019434539537544 100644 (file)
@@ -229,27 +229,12 @@ in various ways.  There is a separate error indicator for each thread.
    Similar to :c:func:`PyErr_SetFromWindowsErrWithFilename`, with an additional
    parameter specifying the exception type to be raised. Availability: Windows.
 
-.. c:function:: PyObject* PyErr_SetExcWithArgsKwargs(PyObject *exc, PyObject *args, PyObject *kwargs)
-
-   This is a convenience function to set an *exc* with the given *args* and
-   *kwargs* values. If *args* is ``NULL``, an empty :func:`tuple` will be
-   created when *exc* is created via :c:func:`PyObject_Call`.
-
-   .. versionadded:: 3.3
-
-.. c:function:: PyObject* PyErr_SetFromImportErrorWithName(PyObject *msg, PyObject *name)
-
-   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.
-
-   .. versionadded:: 3.3
-
-.. c:function:: PyObject* PyErr_SetFromImportErrorWithNameAndPath(PyObject *msg, PyObject *name, PyObject *path)
+.. 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. Both *name* and *path* will be set
-   as the :exc:`ImportError`'s respective ``name`` and ``path`` attributes.
+   set as the exception's message string. *name* and *path*, both of which can
+   be ``NULL``, will be set as the :exc:`ImportError`'s respective ``name``
+   and ``path`` attributes.
 
    .. versionadded:: 3.3
 
index 5aacf7ac8a039d5acc502823c70896eb9a8a4a75..458420aa323ee067e3350f2463202aa02f6a73a2 100644 (file)
@@ -265,9 +265,8 @@ PyAPI_FUNC(PyObject *) PyErr_SetExcFromWindowsErr(PyObject *, int);
 
 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);
index ef3e2c5958a344634598b607ca463d4691f24de7..7bf3dfc811a7da3bc952f0a7d62c2f1e657e1df5 100644 (file)
@@ -254,9 +254,9 @@ dl_funcptr _PyImport_GetDynLoadWindows(const char *shortname,
                         theLength));
             }
             if (message != NULL) {
-                PyErr_SetFromImportErrorWithNameAndPath(message,
-                                        PyUnicode_FromString(shortname),
-                                        pathname);
+                PyErr_SetImportError(message, PyUnicode_FromString(shortname),
+                                     pathname);
+                Py_DECREF(message);
             }
             return NULL;
         } else {
index 345a345afe4d1498e50992148efd7e262b21bfb3..a49cde624795270bcc400a872997b6eb70ac5cb4 100644 (file)
@@ -586,50 +586,43 @@ PyObject *PyErr_SetFromWindowsErrWithUnicodeFilename(
 #endif /* MS_WINDOWS */
 
 PyObject *
-PyErr_SetExcWithArgsKwargs(PyObject *exc, PyObject *args, PyObject *kwargs)
+PyErr_SetImportError(PyObject *msg, PyObject *name, PyObject *path)
 {
-    PyObject *val;
+    PyObject *args, *kwargs, *error;
 
-    /* args must at least be an empty tuple */
+    args = PyTuple_New(1);
     if (args == NULL)
-        args = PyTuple_New(0);
-
-    val = PyObject_Call(exc, args, kwargs);
-    if (val != NULL) {
-        PyErr_SetObject((PyObject *) Py_TYPE(val), val);
-        Py_DECREF(val);
-    }
+        return NULL;
 
-    return NULL;
-}
+    kwargs = PyDict_New();
+    if (args == NULL)
+        return NULL;
 
-PyObject *
-PyErr_SetFromImportErrorWithNameAndPath(PyObject *msg,
-                                        PyObject *name, PyObject *path)
-{
-    PyObject *args = PyTuple_New(1);
-    PyObject *kwargs = PyDict_New();
-    PyObject *result;
+    if (name == NULL)
+        name = Py_None;
 
     if (path == NULL)
         path = Py_None;
 
+    Py_INCREF(msg);
     PyTuple_SetItem(args, 0, msg);
     PyDict_SetItemString(kwargs, "name", name);
     PyDict_SetItemString(kwargs, "path", path);
 
-    result = PyErr_SetExcWithArgsKwargs(PyExc_ImportError, args, kwargs);
+    /* args must at least be an empty tuple */
+    if (args == NULL)
+        args = PyTuple_New(0);
+
+    error = PyObject_Call(PyExc_ImportError, args, kwargs);
+    if (error!= NULL) {
+        PyErr_SetObject((PyObject *) Py_TYPE(error), error);
+        Py_DECREF(error);
+    }
 
     Py_DECREF(args);
     Py_DECREF(kwargs);
 
-    return result;
-}
-
-PyObject *
-PyErr_SetFromImportErrorWithName(PyObject *msg, PyObject *name)
-{
-    return PyErr_SetFromImportErrorWithNameAndPath(msg, name, NULL);
+    return NULL;
 }
 
 void
index 584f30eb3811c371f72635e1a0bcadf957d72f17..f3de7d826278134ba9f59f21e6ff7fb94651ad8a 100644 (file)
@@ -2460,7 +2460,8 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *given_globals,
         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;