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

Comtypes depends on numpy for wstring[] attributes #55

Closed
cfarrow opened this issue May 29, 2014 · 4 comments · Fixed by #59
Closed

Comtypes depends on numpy for wstring[] attributes #55

cfarrow opened this issue May 29, 2014 · 4 comments · Fixed by #59
Labels
bug Something isn't working

Comments

@cfarrow
Copy link
Contributor

cfarrow commented May 29, 2014

From the mailing list: http://sourceforge.net/p/comtypes/mailman/message/32392260/

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".

When I run this test, I get:

python .\test.py
Traceback (most recent call last):
File ".\test.py", line 8, in
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.

@cfarrow cfarrow added the bug label May 29, 2014
@pfmoore
Copy link
Contributor

pfmoore commented Jun 2, 2014

See PR #56. It seems to fix the issue for me, but I don't follow the refcounting semantics in comtypes so it's possible I've introduced a memory leak or similar. So a review by someone who actually understands the code would be useful!

@cfarrow
Copy link
Contributor Author

cfarrow commented Jun 3, 2014

@pfmoore, please try out #59 (branch bstr-array-redux). It should accomplish the same as #56 and fix the import issue.

@pfmoore
Copy link
Contributor

pfmoore commented Jun 3, 2014

Yep, that works for me. Many thanks!

@cfarrow
Copy link
Contributor Author

cfarrow commented Jun 3, 2014

Excellent. Thanks for the report and initial patch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants