From 7b3189911bec94a8a400fb21fe46482b5756f50f Mon Sep 17 00:00:00 2001 From: davidschranz Date: Thu, 28 Nov 2024 00:19:13 +0100 Subject: [PATCH] Allow the definition of the `_midlSAFEARRAY(HRESULT)` type. (#670) * Add VT_HRESULT in _ctype_to_vartype * Add exception when attempting to call the _midlSAFEARRAY(HRESULT).create * Fix formating issue * Add test code --- comtypes/automation.py | 1 + comtypes/safearray.py | 10 ++++++++++ comtypes/test/test_midl_safearray_create.py | 10 +++++++++- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/comtypes/automation.py b/comtypes/automation.py index 61797dd8e..9f1df3e00 100644 --- a/comtypes/automation.py +++ b/comtypes/automation.py @@ -956,6 +956,7 @@ def Invoke(self, dispid: int, *args: Any, **kw: Any) -> Any: c_ulonglong: VT_UI8, VARIANT_BOOL: VT_BOOL, BSTR: VT_BSTR, + HRESULT: VT_HRESULT, VARIANT: VT_VARIANT, # SAFEARRAY(VARIANT *) # diff --git a/comtypes/safearray.py b/comtypes/safearray.py index 70e7c3abe..6330d5526 100644 --- a/comtypes/safearray.py +++ b/comtypes/safearray.py @@ -78,6 +78,7 @@ def _make_safearray_type(itemtype): VT_UNKNOWN, IDispatch, VT_DISPATCH, + VT_HRESULT, ) meta = type(_safearray.tagSAFEARRAY) @@ -123,6 +124,15 @@ def create(cls, value, extra=extra): one-dimensional arrays. To create multidimensional arrys, numpy arrays must be passed. """ + if cls._vartype_ == VT_HRESULT: + raise TypeError( + # There are COM type libraries that define the + # `_midlSAFEARRAY(HRESULT)` type; however, creating `HRESULT` + # safearray pointer instance does not work. + # See also: https://github.com/enthought/comtypes/issues/668 + "Cannot create SAFEARRAY type VT_HRESULT." + ) + if comtypes.npsupport.isndarray(value): return cls.create_from_ndarray(value, extra) diff --git a/comtypes/test/test_midl_safearray_create.py b/comtypes/test/test_midl_safearray_create.py index d632b5e78..594e0a57c 100644 --- a/comtypes/test/test_midl_safearray_create.py +++ b/comtypes/test/test_midl_safearray_create.py @@ -1,6 +1,6 @@ # coding: utf-8 -from ctypes import c_int, pointer, POINTER +from ctypes import c_int, pointer, HRESULT, POINTER import unittest import comtypes @@ -82,6 +82,14 @@ def test_record(self): self.assertEqual(unpacked.answer, 42) self.assertEqual(unpacked.needs_clarification, True) + def test_HRESULT(self): + hr = HRESULT(1) + sa_type = comtypes.safearray._midlSAFEARRAY(HRESULT) + with self.assertRaises(TypeError): + sa_type.create([hr], extra=None) + with self.assertRaises(TypeError): + sa_type.create([hr]) + def test_ctype(self): extra = None cdata = c_int(1)