enthought / comtypes

A pure Python, lightweight COM client and server framework, based on the ctypes Python FFI package.
Other
282 stars 96 forks source link

Support named args for dispmethod #486

Closed junkmd closed 1 day ago

junkmd commented 1 year ago

371

I was trying to implement the #400 functionality. However, I realized that it is misleading to have a function annotation that allows named arguments to be accepted, since a dispmethod called via IDispatch.Invoke will raise an error when a named (with default value) argument is passed at runtime.

So, I improved the process of instantiating DISPPARAMS so that it can also accept named arguments in dispmethod. Using _argspec passed to IDispatch.Invoke, it completes default values for optional arguments that were not passed, to ensure that the passed argument names are correct.

This added functionality is only triggered when named arguments are passed to dispmethod. Same as before, when just only positional arguments are passed, the VARIANT array is assigned to DISPPARAMS.rgvarg in the reverse order. Therefore, this change does not break backward compatibility. In the case that an element of argspec is not what the newly added processing expects, and an error is raised when passing named arguments, it can be avoided by passing only positional arguments.

The bound_named_property has also been improved to allow passing named arguments.

As for testing, I used Excel to verify that it is possible to pass named arguments to a dispmethod. I have Excel in my development environment, so I can run this test and make sure it passes. However, AppVeyor CI does not have Excel, so I am not able to fully guarantee End2End operation with CI. Instead, I generously cover the test of DISPPARAMS instantiation process, which can be executed independent of Excel and the environment. commethod (and stdmethod) were originally designed to accept named arguments, but there were no tests to do so explicitly, so I added them.

If there is a dispmethod in an environment-independent library that can easily provide fixtures, I will try to test with that object as well, so I am looking for feedback.

junkmd commented 1 year ago

CIs are passed.

memo https://ci.appveyor.com/project/cfarrow/comtypes/builds/47271940 https://github.com/enthought/comtypes/actions/runs/5230311902/jobs/9443793826?pr=486 https://ci.appveyor.com/project/pywinauto/pywinauto/builds/47271962
junkmd commented 1 year ago

@vasily-v-ryabov

Please review this.

Not to break backwards compatibility, but since this is more of a "feature addition to allow named arguments to be passed" than a "bug fix for not being able to pass named arguments", and since there are many lines of codebase changed.

If we decide not to incorporate this into the 1.3.0 like #475, I would move #400 and this from the 1.3.0 milestone to next version milestone.