-
-
Notifications
You must be signed in to change notification settings - Fork 104
Implement SAFEARRAY pointers and SAFEARRAYs as method parameters of a Dispatch Interface #569
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9b2d161
Implement safearray pointers as method parameters of a Dispatch Inter…
geppi b6af0b2
Rebase on main.
geppi ed89154
Added unittest for safearrays as Dispatch Interface method parameters.
geppi 850cedd
Fixed formating issues.
geppi a552fbe
Apply suggestions from code review
geppi ba347c9
Apply suggestions from code review
junkmd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| # coding: utf-8 | ||
|
|
||
| from ctypes import byref, c_double, pointer | ||
| import unittest | ||
|
|
||
| import comtypes | ||
| import comtypes.safearray | ||
| from comtypes import CLSCTX_LOCAL_SERVER | ||
| from comtypes.client import CreateObject, GetModule | ||
|
|
||
| ComtypesCppTestSrvLib_GUID = "{07D2AEE5-1DF8-4D2C-953A-554ADFD25F99}" | ||
|
|
||
| try: | ||
| GetModule((ComtypesCppTestSrvLib_GUID, 1, 0, 0)) | ||
| import comtypes.gen.ComtypesCppTestSrvLib as ComtypesCppTestSrvLib | ||
|
|
||
| IMPORT_FAILED = False | ||
| except (ImportError, OSError): | ||
| IMPORT_FAILED = True | ||
|
|
||
|
|
||
| @unittest.skipIf(IMPORT_FAILED, "This depends on the out of process COM-server.") | ||
| class Test_DispMethods(unittest.TestCase): | ||
| """Test dispmethods with safearray and safearray pointer parameters.""" | ||
|
|
||
| UNPACKED_ZERO_VALS = tuple(0.0 for _ in range(10)) | ||
| UNPACKED_CONSECUTIVE_VALS = tuple(float(i) for i in range(10)) | ||
|
|
||
| def _create_dispifc(self) -> "ComtypesCppTestSrvLib.IDispSafearrayParamTest": | ||
| # Explicitely ask for the dispinterface of the component. | ||
| return CreateObject( | ||
| "Comtypes.DispSafearrayParamTest", | ||
| clsctx=CLSCTX_LOCAL_SERVER, | ||
| interface=ComtypesCppTestSrvLib.IDispSafearrayParamTest, | ||
| ) | ||
|
|
||
| def _create_zero_array(self): | ||
| return comtypes.safearray._midlSAFEARRAY(c_double).create( | ||
| [c_double() for _ in range(10)] | ||
| ) | ||
|
|
||
| def _create_consecutive_array(self): | ||
| return comtypes.safearray._midlSAFEARRAY(c_double).create( | ||
| [c_double(i) for i in range(10)] | ||
| ) | ||
|
|
||
| def test_inout_byref(self): | ||
| dispifc = self._create_dispifc() | ||
| # Passing a safearray by reference to a method that has declared the parameter | ||
| # as [in, out] we expect modifications of the safearray on the server side to | ||
| # also change the safearray on the client side. | ||
| test_array = self._create_zero_array() | ||
| self.assertEqual(test_array.unpack(), self.UNPACKED_ZERO_VALS) | ||
| dispifc.InitArray(byref(test_array)) | ||
| self.assertEqual(test_array.unpack(), self.UNPACKED_CONSECUTIVE_VALS) | ||
|
|
||
| def test_inout_pointer(self): | ||
| dispifc = self._create_dispifc() | ||
| # Passing a safearray pointer to a method that has declared the parameter | ||
| # as [in, out] we expect modifications of the safearray on the server side to | ||
| # also change the safearray on the client side. | ||
| test_array = self._create_zero_array() | ||
| self.assertEqual(test_array.unpack(), self.UNPACKED_ZERO_VALS) | ||
| dispifc.InitArray(pointer(test_array)) | ||
| self.assertEqual(test_array.unpack(), self.UNPACKED_CONSECUTIVE_VALS) | ||
|
|
||
| def test_in_safearray(self): | ||
| # Passing a safearray to a method that has declared the parameter just as [in] | ||
| # we expect modifications of the safearray on the server side NOT to change | ||
| # the safearray on the client side. | ||
| # We also need to test if the safearray gets properly passed to the method on | ||
| # the server side. For this, the 'VerifyArray' method returns 'True' if | ||
| # the safearray items have values equal to the initialization values | ||
| # provided by 'InitArray'. | ||
| for sa, expected, unpacked_content in [ | ||
| (self._create_consecutive_array(), True, self.UNPACKED_CONSECUTIVE_VALS), | ||
| # Also perform the inverted test. For this, create a safearray with zeros. | ||
| (self._create_zero_array(), False, self.UNPACKED_ZERO_VALS), | ||
| ]: | ||
| with self.subTest(expected=expected, unpacked_content=unpacked_content): | ||
| # Perform the check on initialization values. | ||
| self.assertEqual(self._create_dispifc().VerifyArray(sa), expected) | ||
| # Check if the safearray is unchanged although the method | ||
| # modifies the safearray on the server side. | ||
| self.assertEqual(sa.unpack(), unpacked_content) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.