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

Parameter-less COM method would give "TypeError: x.y takes 1 positional argument but 2 were given", for Delphi and MFC COM clients. #2043

Closed
wxinix opened this issue Apr 17, 2023 · 2 comments

Comments

@wxinix
Copy link

wxinix commented Apr 17, 2023

When calling from a Delphi client (or MFC C++ client), parameter-less COM method would give "TypeError: xxxxx takes 1 positional argument but 2 were given"

This is because:

  • When calling from a Delphi client while the target Python method has NO argument (i.e., taking only "self"), arg[0] will be set by Delphi a value of 0x8002000C. This makes args non-empty.

  • When calling from a C++ MFC/COM client while the target Python method has NO argument (i.e., talking only "self"), arg[0] will be set a value of "None". This makes args non-empty.

The following code would be a "protection" for parameter-less Python COM method.

import inspect
argspec = inspect.getfullargspec(func).args
if len(argspec) == 1 and argspec[0] == "self":
    return func()
else:
    return func(*args)

Here is a sample Delphi project for replicating the issue. https://github.com/wxinix/Python-COM-Samples/blob/main/Delphi/PythonComDelphiTest.7z

  • The Python COM server is defined in "MyCOMServer.py" file, including with the Delphi project files. Register the COM server first.
  • Compile and run Delphi, click the "Test Python COM" button
@wxinix wxinix changed the title Parameterless COM method would give "TypeError: xxxxx takes 1 positional argument but 2 were given", for Delphi and MFC COM client. Parameter-less COM method would give "TypeError: x.y takes 1 positional argument but 2 were given", for Delphi and MFC COM clients. Apr 17, 2023
@mhammond
Copy link
Owner

0x8002000C == DISP_E_UNKNOWNLCID - so it sounds like something else is is going wrong with the arg passing. I don't have delphi - is this possible to repro anywhere else? I'm a bit reluctant to take a hack to work around one environment I can't test until I'm very sure it isn't going to break any other environment where things work perfectly well.

@wxinix
Copy link
Author

wxinix commented Apr 18, 2023

I further tested clients in Delphi, C++/ATL, and C#. Only Delphi client has the issue. C# and C++ have no problem. https://github.com/wxinix/pywin32-samples

I believe the bug is really Delphi's, and should be resolved on Delphi side. I am going close this issue. How can I withdraw the PR, or you can feel free to reject it.

Thank you for your great efforts on pywin32. The more I look into its source code, the more I feel awe about it. COM still alive!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants