]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Fix the way methods are created for the _ctypes.COMError exception
authorThomas Heller <theller@ctypes.org>
Wed, 6 Feb 2008 19:58:46 +0000 (19:58 +0000)
committerThomas Heller <theller@ctypes.org>
Wed, 6 Feb 2008 19:58:46 +0000 (19:58 +0000)
type; this fix is already in the trunk.

Lib/ctypes/test/test_win32.py
Misc/NEWS
Modules/_ctypes/_ctypes.c

index db530d3af4ad4392b20742387d49f8cafa0245fa..fd4e039ab89b6e6782aa6ba747ea805d40c7aba1 100644 (file)
@@ -37,6 +37,19 @@ if sys.platform == "win32":
                 # are trapped and raise an exception.
                 self.assertRaises(WindowsError, windll.kernel32.GetModuleHandleA, 32)
 
+    class TestWintypes(unittest.TestCase):
+
+        def test_COMError(self):
+            from _ctypes import COMError
+            self.assertEqual(COMError.__doc__, "Raised when a COM method call failed.")
+
+            ex = COMError(-1, "text", ("details",))
+            self.assertEqual(ex.hresult, -1)
+            self.assertEqual(ex.text, "text")
+            self.assertEqual(ex.details, ("details",))
+            self.assertEqual((ex.hresult, ex.text, ex.details),
+                             ex[:])
+
 class Structures(unittest.TestCase):
 
     def test_struct_by_value(self):
index e7d3370f86fe60bcd270c3942440996d4b2b0707..e3a4aceecec18fa1a9177904a443f06f8504edfc 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -80,6 +80,10 @@ Core and builtins
 Library
 -------
 
+- Fixed _ctypes.COMError so that it must be called with exactly three
+  arguments, instances now have the hresult, text, and details
+  instance variables.
+
 - #1507247, #2004: tarfile.py: Use mode 0700 for temporary directories and
   default permissions for missing directories.
 
index 814b85496bc4137584905e1b1e7ae18cb3d9dd7c..aa283e3a50ce15fd264502adb875551b8ae0431d 100644 (file)
@@ -4520,32 +4520,42 @@ create_comerror(void)
        PyObject *s;
        int status;
 
-       ComError = PyErr_NewException("_ctypes.COMError",
-                                     NULL,
-                                     dict);
-       if (ComError == NULL)
+       if (dict == NULL)
                return -1;
+
        while (methods->ml_name) {
                /* get a wrapper for the built-in function */
                PyObject *func = PyCFunction_New(methods, NULL);
                PyObject *meth;
                if (func == NULL)
-                       return -1;
+                       goto error;
                meth = PyMethod_New(func, NULL, ComError);
                Py_DECREF(func);
                if (meth == NULL)
-                       return -1;
+                       goto error;
                PyDict_SetItemString(dict, methods->ml_name, meth);
                Py_DECREF(meth);
                ++methods;
        }
-       Py_INCREF(ComError);
+
        s = PyString_FromString(comerror_doc);
        if (s == NULL)
-               return -1;
+               goto error;
        status = PyDict_SetItemString(dict, "__doc__", s);
        Py_DECREF(s);
-       return status;
+       if (status == -1)
+               goto error;
+
+       ComError = PyErr_NewException("_ctypes.COMError",
+                                     NULL,
+                                     dict);
+       if (ComError == NULL)
+               goto error;
+
+       return 0;
+  error:
+       Py_DECREF(dict);
+       return -1;
 }
 
 #endif