Paul Moore
2014-05-28 21:26:00 UTC
I've just hit an issue using comtypes to interact with Virtualbox.
Here's the simplest test case I can create:
import comtypes.client
import comtypes.gen.VirtualBox
vbox = comtypes.client.CreateObject('VirtualBox.VirtualBox')
machine = vbox.FindMachine('Ahri')
adapter = machine.getNetworkAdapter(0)
nat = adapter.NATEngine
print(repr(nat.redirects))
This gets the redirects property from the NatEngine object, which is
documented as follows:
readonly attribute wstring[] redirects
Array of NAT port-forwarding rules in string representation, in the
following format: "name,protocol id,host ip,host port,guest ip,guest
port".
File ".\test.py", line 8, in <module>
print(repr(nat.redirects))
File "C:\Work\Projects\vm\ve\lib\site-packages\comtypes\__init__.py",
line 279, in __getattr__
return getattr(self, fixed_name)
File "C:\Work\Projects\vm\ve\lib\site-packages\comtypes\safearray.py",
line 218, in __ctypes_from_outparam__
return self[0]
File "C:\Work\Projects\vm\ve\lib\site-packages\comtypes\safearray.py",
line 209, in __getitem__
return self.unpack()
File "C:\Work\Projects\vm\ve\lib\site-packages\comtypes\safearray.py",
line 234, in unpack
result = self._get_elements_raw(num_elements)
File "C:\Work\Projects\vm\ve\lib\site-packages\comtypes\safearray.py",
line 301, in _get_elements_raw
import numpy.ctypeslib
ImportError: No module named 'numpy'
As far as I can tell, comtypes is interpreting the array of wstring
objects as a 2-dimensional array, and as a result thinks it needs
numpy.
Two suggestions here:
1. If numpy isn't available, fall back to something core Python can
handle like a tuple of tuples.
2. In this particular case, the code should be returning a tuple of
(Unicode) strings.
The equivalent code works with pywin32, for what it's worth.
Paul
PS The website address and issue tracker mentioned on PyPI point to a
dead link - https://github.com/jaraco/comtypes/
Here's the simplest test case I can create:
import comtypes.client
import comtypes.gen.VirtualBox
vbox = comtypes.client.CreateObject('VirtualBox.VirtualBox')
machine = vbox.FindMachine('Ahri')
adapter = machine.getNetworkAdapter(0)
nat = adapter.NATEngine
print(repr(nat.redirects))
This gets the redirects property from the NatEngine object, which is
documented as follows:
readonly attribute wstring[] redirects
Array of NAT port-forwarding rules in string representation, in the
following format: "name,protocol id,host ip,host port,guest ip,guest
port".
python .\test.py
Traceback (most recent call last):File ".\test.py", line 8, in <module>
print(repr(nat.redirects))
File "C:\Work\Projects\vm\ve\lib\site-packages\comtypes\__init__.py",
line 279, in __getattr__
return getattr(self, fixed_name)
File "C:\Work\Projects\vm\ve\lib\site-packages\comtypes\safearray.py",
line 218, in __ctypes_from_outparam__
return self[0]
File "C:\Work\Projects\vm\ve\lib\site-packages\comtypes\safearray.py",
line 209, in __getitem__
return self.unpack()
File "C:\Work\Projects\vm\ve\lib\site-packages\comtypes\safearray.py",
line 234, in unpack
result = self._get_elements_raw(num_elements)
File "C:\Work\Projects\vm\ve\lib\site-packages\comtypes\safearray.py",
line 301, in _get_elements_raw
import numpy.ctypeslib
ImportError: No module named 'numpy'
As far as I can tell, comtypes is interpreting the array of wstring
objects as a 2-dimensional array, and as a result thinks it needs
numpy.
Two suggestions here:
1. If numpy isn't available, fall back to something core Python can
handle like a tuple of tuples.
2. In this particular case, the code should be returning a tuple of
(Unicode) strings.
The equivalent code works with pywin32, for what it's worth.
Paul
PS The website address and issue tracker mentioned on PyPI point to a
dead link - https://github.com/jaraco/comtypes/