Skip to content

Commit

Permalink
Allow the definition of the _midlSAFEARRAY(HRESULT) type. (#670)
Browse files Browse the repository at this point in the history
* Add VT_HRESULT in _ctype_to_vartype

* Add exception when attempting to call the _midlSAFEARRAY(HRESULT).create

* Fix formating issue

* Add test code
  • Loading branch information
davidschranz authored Nov 27, 2024
1 parent fb15f91 commit 7b31899
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions comtypes/automation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 *)
#
Expand Down
10 changes: 10 additions & 0 deletions comtypes/safearray.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def _make_safearray_type(itemtype):
VT_UNKNOWN,
IDispatch,
VT_DISPATCH,
VT_HRESULT,
)

meta = type(_safearray.tagSAFEARRAY)
Expand Down Expand Up @@ -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)

Expand Down
10 changes: 9 additions & 1 deletion comtypes/test/test_midl_safearray_create.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 7b31899

Please sign in to comment.