Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow the definition of the _midlSAFEARRAY(HRESULT) type. #670

Merged
merged 4 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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