diff Python/errors.c @ 72965:84aed9ef7aaf

Instantiate the OS-related exception as soon as we raise it, so that "except" works properly. PyErr_SetFromErrnoWithFilenameObject() was already fixed by the changeset 793c75177d28. This commit fixes PyErr_SetExcFromWindowsErrWithFilenameObject(), used on Windows.
author Victor Stinner <victor.stinner@haypocalc.com>
date Mon, 17 Oct 2011 20:18:58 +0200
parents db4aa878cea2
children 021e5bb297d1
line wrap: on
line diff
--- a/Python/errors.c	Mon Oct 17 19:55:31 2011 +0200
+++ b/Python/errors.c	Mon Oct 17 20:18:58 2011 +0200
@@ -468,7 +468,7 @@
     int len;
     WCHAR *s_buf = NULL; /* Free via LocalFree */
     PyObject *message;
-    PyObject *v;
+    PyObject *args, *v;
     DWORD err = (DWORD)ierr;
     if (err==0) err = GetLastError();
     len = FormatMessageW(
@@ -504,12 +504,16 @@
         filenameObject = Py_None;
     /* This is the constructor signature for passing a Windows error code.
        The POSIX translation will be figured out by the constructor. */
-    v = Py_BuildValue("(iOOi)", 0, message, filenameObject, err);
+    args = Py_BuildValue("(iOOi)", 0, message, filenameObject, err);
     Py_DECREF(message);
 
-    if (v != NULL) {
-        PyErr_SetObject(exc, v);
-        Py_DECREF(v);
+    if (args != NULL) {
+        v = PyObject_Call(exc, args, NULL);
+        Py_DECREF(args);
+        if (v != NULL) {
+            PyErr_SetObject((PyObject *) Py_TYPE(v), v);
+            Py_DECREF(v);
+        }
     }
     LocalFree(s_buf);
     return NULL;