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

Using Numpy to send a 2D array to the COM interface, a "TypeError" occurs #334

Closed
ShiinaRinne opened this issue Aug 1, 2022 · 6 comments

Comments

@ShiinaRinne
Copy link

Here is my test code and error log

from comtypes.automation import VARIANT
from comtypes import npsupport
import numpy as np

array = np.zeros((1,1),npsupport.VARIANT_dtype, order='F')
array.flat = [VARIANT("123")]
File c:\Users\ym\AppData\Local\Programs\Python\Python310\lib\site-packages\numpy\core\_dtype_ctypes.py:114, in dtype_from_ctypes_type(t)
    112     return _from_ctypes_union(t)
    113 elif isinstance(getattr(t, '_type_', None), str):
--> 114     return _from_ctypes_scalar(t)
    115 else:
    116     raise NotImplementedError(
    117         "Unknown ctypes type {}".format(t.__name__))

File c:\Users\ym\AppData\Local\Programs\Python\Python310\lib\site-packages\numpy\core\_dtype_ctypes.py:80, in _from_ctypes_scalar(t)
     78     return np.dtype('<' + t._type_)
     79 else:
---> 80     return np.dtype(t._type_)

TypeError: data type 'v' not understood

And main code


class IDMInterface:
    def __init__(self):
        self.hr = client.CreateObject(
            progid='IDMan.CIDMLinkTransmitter',
            interface=self.idm_module.ICIDMLinkTransmitter2
        )
    def SendLinksArray(self, location: str, pLinksArray: List[Any]) -> None:
        self.hr.SendLinksArray(location, pLinksArray)


array = np.array([
        ["aaa", "bbb", "ccc", "ddd"],
        ["aaa", "bbb", "ccc", None]
    ])

IDMInterface().SendLinksArray(referer, array)
C:\Users\ym\AppData\Local\Programs\Python\Python310\lib\site-packages\comtypes\safearray.py:387: RuntimeWarning: A builtin ctypes object gave a PEP3118 format string that does not match its itemsize, so a best-guess will be made of the data type. Newer versions of python may behave correctly.     
  varr.flat = [VARIANT(v) for v in value.flat]
Traceback (most recent call last):
  File "C:\Users\ym\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\ym\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "c:\Users\ym\.vscode\extensions\ms-python.python-2022.10.1\pythonFiles\lib\python\debugpy\__main__.py", line 39, in <module>
    cli.main()
  File "c:\Users\ym\.vscode\extensions\ms-python.python-2022.10.1\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 430, in main        
    run()
  File "c:\Users\ym\.vscode\extensions\ms-python.python-2022.10.1\pythonFiles\lib\python\debugpy/..\debugpy\server\cli.py", line 284, in run_file    
    runpy.run_path(target, run_name="__main__")
  File "c:\Users\ym\.vscode\extensions\ms-python.python-2022.10.1\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 321, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "c:\Users\ym\.vscode\extensions\ms-python.python-2022.10.1\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 135, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "c:\Users\ym\.vscode\extensions\ms-python.python-2022.10.1\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_runpy.py", line 124, in _run_code
    exec(code, run_globals)
  File "d:\Downloads\python-idm-helper-main\IDMInterface.py", line 142, in <module>
    IDMInterface().SendLinksArray(referer, array)
  File "d:\Downloads\python-idm-helper-main\IDMInterface.py", line 92, in SendLinksArray
    self.hr.SendLinksArray(location, pLinksArray)
ctypes.ArgumentError: argument 2: <class 'TypeError'>: data type 'v' not understood

I think the bug might be in safearray.py, line 386-387
It should be that VARIANT cannot be assigned to varr.flat due to dtype not being correctly identified

varr = numpy.zeros(value.shape, npsupport.VARIANT_dtype, order='F')
varr.flat = [VARIANT(v) for v in value.flat]

May I ask where did I write wrong, or is this a feature that is not yet supported,
can you give some suggestions? thank you very much

Environment

python version: 3.10
numpy version: 1.23.1

@ShiinaRinne
Copy link
Author

image

In this case it handles correctly (under c++)

@junkmd
Copy link
Collaborator

junkmd commented Aug 4, 2022

@ShiinaRinne

This sounds similar to #212 problems.

What seems difficult is that the tests that use numpy are currently skipped, so it's hard to know the impact of the code change.

Aside from that, I'm not familiar with IDMInterface (maybe you mean Internet Download Manager, right?), what kind of behavior is expected at this time?

@junkmd
Copy link
Collaborator

junkmd commented Aug 4, 2022

@ShiinaRinne

And try to use the current master branch of comtypes.

From the 1.1.11 release to the present, #308(related to numpy) has been merged.

@ShiinaRinne
Copy link
Author

@ShiinaRinne

This sounds similar to #212 problems.

What seems difficult is that the tests that use numpy are currently skipped, so it's hard to know the impact of the code change.

Aside from that, I'm not familiar with IDMInterface (maybe you mean Internet Download Manager, right?), what kind of behavior is expected at this time?

Sure, I'm using its COM interface to implement batch downloads

@ShiinaRinne
Copy link
Author

@ShiinaRinne

And try to use the current master branch of comtypes.

From the 1.1.11 release to the present, #308(related to numpy) has been merged.

I will try this branch later, thanks!

@ShiinaRinne
Copy link
Author

Sorry for not replying in time because I was busy, but it still didn't work after I tried it.
Since I'm not very good at python, I can only refactor it through C++ at the moment, but thank you very much for your reply!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants