-
-
Notifications
You must be signed in to change notification settings - Fork 98
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
Argument lifetime issue when calling COM/ATL methods with array of strings (and probably others) as of at least Python 3.8.3 (which worked with Python 3.7.x) #212
Comments
Hi All |
@karpierz I see the importance of the issue. I will try to find time to work on it in June. Since pywinauto is not affected, it's P2 for me. Thanks for understanding. |
Vasilij |
I'm trying to make this example working, but variable |
I have a similar problem. In my case the last string argument provided to python function is replacing other string arguments coming before it in the underlying C/C++ code. Somehow comtypes is doing this when running with >python 3.8. I have the following solution that can highlight the underlying issue. I provided string arguments inside a VARIANT object, which helped me. IMHO this issue is very important and if @vasily-v-ryabov can provide some guidance I am happy to help. Irfan |
@whannes what do you expect in returned value when the attached implementation is empty? STDMETHODIMP CATLTestObject::concatenateStrings(VARIANT arrayOfStrings, BSTR* concatenatedStrings)
{
// TODO: Add your implementation code here
return S_OK;
} I mean what do you expect while running your example without debugger? I suspect you have more detailed implementation to reproduce the problem but you forgot to attach the updated code. Or I missed something. All I need is a reproducing code (without debugger). Can someone prepare it? |
@karpierz @irfanilgin if you have your own reproducing example, please attach it ASAP. I have free time on Monday probably. And today which seems already missing for this bug. I can quickly compile C++ code with VS2015 (hope it's OK). I'd like first to run it without VS debugger and without Python debugger (python.exe -d). Python 3.8+ and Python 3.7 should show me the difference. I hope both Pythons are 64-bit (it sounds consistent at least). |
Please compare D:>py -3.7 z_COM.py D:>py -3.8 z_COM.py |
Hi all, I can‘t do it within the next week as I have no machine with me right now. If any of you can provide some implementation code earlier, please go ahead. |
Hi All |
Hi All |
@karpierz I am trying to restore I noticed that it fails in Python 3.8 or later as you reported. However, I wonder why it occurred. I have been always use cell ranges as But I think it's large problem that different behaviors in different versions. If this can be fix by modifying the |
Just run into this problem myself trying to test a new comtypes based server. I have put together a fairly compact minimum example, the .idl file looks like this:
which I compile with the MIDL compiler, then the Python code looks like this
This needs to be run from an admin console to allow the registration part to happen (at least the first time it is run), and the result I get is:
So calling with a single value (string or numeric) is fine, but with a list or tuple we not only get an array of None variants (of the same size as the passed list) but this is followed by a crash due to heap corruption, which may be because memory is being released twice or something. My current setup is with comtypes 1.1.11 and Python 3.9.7. I am not that familiar with the comtypes internals, but if @vasily-v-ryabov or another maintainer can give me some pointers about where to look I am happy to dive in to try and solve this issue. |
@bennyrowland I am also trying to figure out how to solve this problem, so if I come up with any ideas, I would like to share them. |
@junkmd, it would be nice to solve this. For the moment I have been able to solve it in my case by changing the parameters to VARIANT *, in that case the array is passed correctly. Interestingly I have also tried accessing the Test server from VB.NET, this also fails with test.tlb above, but works with VARIANT * argument, I think that VB.NET is only capable of passing arrays by reference, and seems to release the values after it passes them, but for compatibility with both clients, VARIANT * may be the way to go. |
@bennyrowland In terms of the code generated by comtypes, define but I would like to hear from other contributors that is a reasonable modification. |
I noticed. comtypes/comtypes/test/test_excel.py Lines 49 to 63 in db0931d
Add the following assertion to this test. self.assertEqual(xl.Range("A1", "C1").Address(), "$A$1:$C$1") This line passes for FAIL: test (comtypes.test.test_excel.Test_EarlyBind.test)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\komoj\comtypes\comtypes\test\test_excel.py", line 52, in test
self.assertEqual(xl.Range("A1", "C1").Address(), "$A$1:$C$1")
AssertionError: '$C$1' != '$A$1:$C$1'
- $C$1
+ $A$1:$C$1 |
diffs between 3.7.7 and 3.8.3python/cpython@v3.7.7...v3.8.3#diff-402bc51df93eaaca2e2f2551ec0206d8af190722ae4703c178554b565d7c14b4 |
I am investigating the issue on |
It was reported in python/cpython#82929 that the callbacks were behaving strangely starting with Python 3.8. I was going to look for a workaround on Would I call out to the |
@junkmd it seems quite plausible to me that the issue is indeed not with com types but with ctypes or something else further up the chain. Indeed it is quite hard to imagine how com types itself could cause the problem without any C code. However, the cpython issue doesn't seem to have seen much progress in a while so who knows if/when it will get resolved. |
I saw you in python/cpython#82929. |
I don't believe it is directly related to the libffi bug in python/cpython#82929, but I can't rule out that it is a separate libffi bug. |
Thank you for your response. I have not encountered this bug either, but I am concerned about the regression in how Excel cells are specified as reported by @karpierz. If there is no movement on python/cpython#82929 in the next week I will open another issue on cpython to resolve this. |
For reference python/cpython#82929 has already been fixed in Python
main. See python/cpython#97512 and in the Python 3.11 branch, see
python/cpython#97513. However this missed out on getting into Python
3.11.0. I'm not sure why python/cpython#82929 has not been closed as
fixed yet. I can't do it as I was not the original reporter.
So it is worth testing comtypes with Python main to see if this
particular bug still exists.
|
Unfortunatelly the bug still exist in the newest (6.12.2022) versions of Python (3.10.9, 3.11.1). For such code:
we have results:
PS: This is an important/real barrier to upgrading Python to versions newer than 3.7. |
I posted bug report to It would be helpful if you could post any information you have there as well. |
I think python/cpython#100169 by @ynkdir might help this. I am watching for python/cpython#100169 as it may be possible to resolve this issue on the |
Good news. python/cpython#100169 has been merged. The changes will be backported to Py3.11 and Py3.10. I will see that Excel in my environment would work when they are released. I think that the involved members should also check to see that it works well for your projects. |
Python 3.10.10 and 3.11.2 is now available. And there is a mention about python/cpython#99952 in the CHANGELOG.
https://docs.python.org/release/3.10.10/whatsnew/changelog.html#library I installed 3.11.2 and removed the ...\comtypes> python -m unittest comtypes.test.test_excel -v
test (comtypes.test.test_excel.Test_EarlyBind.test) ... ok
test (comtypes.test.test_excel.Test_LateBind.test) ... ok
----------------------------------------------------------------------
Ran 2 tests in 13.654s
OK I am thinking if it is possible to test this behavior with a non-environment dependent COM library. Any opinions would be appreciated. |
I am thinking of closing this issue as resolved. However, this error is very confusing when getting into it (like #494), so I am thinking of a way to make it known to For example, adding a sentence to
Any opinions would be appreciated. |
I have described in the I think this is sufficient for disseminating the information. |
Consider the following, very simple COM/ATL project made with MSVC 2017 (15.9.24, Windows 10 Pro 2004): comProjectWithArray-v2.zip. It does not contain any functionality and is used to reproduce the empty array as described below only.
Registration is disabled in the project settings here, so to test it you have to register
ATLTestServer.exe /RegServer
regsvr32 ATLTestServerPS.dll
After successful registration, it can be tested with the following Python-Snippet:
from comtypes.client import CreateObject
obj = CreateObject("ATLTestServer.ATLTestObject")
obj.concatenateStrings(["New", " ", "York"])
When using comtypes 1.1.7 (6fb4403) with Python 3.7.7 x64 the call from the Python snipped gets properly routed to the C++ method:
However, when using the same comtypes version with Python 3.8.3 x64, the array in C++ contains only nullptr:
Is this a known issue?
The text was updated successfully, but these errors were encountered: