enthought / enthought/comtypes

Conversion from 2d ndarray to VARIANT fails

Open
#213 1 comment 0 reactions 0 assignees View on GitHub
npsupport
Dominant language
Python
Stars
345
Forks
105
PR merge metrics
No merged PRs in 30d

Description

I was trying to pass 2D VARIANT array to a COM object, but it was always passed as one dimensional array
Then I tried to pass ndarray, but was getting type conversion error.
Then I tried to create 2D VARIANT array directly using the code below.

```
`import comtypes.client as CC
import comtypes.safearray as CS
import comtypes.automation as CA
import numpy as np
arr = np.ones((2,2))
varObject = CS._midlSAFEARRAY(CA.VARIANT).create(arr)`
```
It fails in safearray.py at line
380: varr.flat = [VARIANT(v) for v in value.flat]

it's a part of the function :

```
def _ndarray_to_variant_array(value):
""" Convert an ndarray to VARIANT_dtype array """
# Check that variant arrays are supported
if npsupport.VARIANT_dtype is None:
msg = "VARIANT ndarrays require NumPy 1.7 or newer."
raise RuntimeError(msg)

# special cases
if numpy.issubdtype(value.dtype, npsupport.datetime64):
return _datetime64_ndarray_to_variant_array(value)

from comtypes.automation import VARIANT
# Empty array
varr = numpy.zeros(value.shape, npsupport.VARIANT_dtype, order='F')
# Convert each value to a variant and put it in the array.
varr.flat = [VARIANT(v) for v in value.flat]
return varr

```

Python version 3.7.7
COMTypes version 1.1.7

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.