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

win32com/server/policy.py tries to call as string #1902

Closed
dlech opened this issue Jun 28, 2022 · 7 comments · Fixed by #2270
Closed

win32com/server/policy.py tries to call as string #1902

dlech opened this issue Jun 28, 2022 · 7 comments · Fixed by #2270
Assignees

Comments

@dlech
Copy link

dlech commented Jun 28, 2022

Note that issues in this repository are only for bugs or feature requests in the pywin32.

If you need support or help using this package, please follow these instructions - support or help requests will be closed without comment.

For all bugs, please provide the following information.

  • Expected behavior and actual behavior.

https://github.com/mhammond/pywin32/blob/main/com/win32com/server/policy.py tries to call error which is a string.

error is defined at

error = __name__ + " error"

and there are 4 instances where it is called

  • Steps to reproduce the problem.

Try to use a COM interface that does not exist.

program output:

pythoncom error: ERROR: server.policy could not create an instance.

Traceback (most recent call last):
  File "C:\Users\david\AppData\Local\pypoetry\Cache\virtualenvs\winsdk1-vNE5bx4W-py3.10\lib\site-packages\win32com\server\policy.py", line 151, in CreateInstance
    return retObj._CreateInstance_(clsid, reqIID)
  File "C:\Users\david\AppData\Local\pypoetry\Cache\virtualenvs\winsdk1-vNE5bx4W-py3.10\lib\site-packages\win32com\server\policy.py", line 211, in _CreateInstance_
    raise error(
TypeError: 'str' object is not callable
pythoncom error: CPyFactory::CreateInstance failed to create instance. (80004005)
  • Version of Python and pywin32
@Avasam Avasam self-assigned this Mar 17, 2024
@Avasam
Copy link
Collaborator

Avasam commented Mar 17, 2024

These kinds of issues are easily caught by type-checkers. I also noticed a couple more error; str in typeshed recently. I expect this to be resolved pretty soon as I turn on type-checking for more and more parts of the code.

@Avasam Avasam changed the title win32com/server/policy.py tries to call win32com/server/policy.py tries to call as string Mar 18, 2024
@Avasam
Copy link
Collaborator

Avasam commented Mar 18, 2024

@mhammond It seems that during the transition to Python3, a lot of string error variables were left as strings, causing them to be called and raise TypeError: 'str' object is not callable. What should be done with those?

  1. Make them their own error class, possibly inheriting from TypeError for historical reasons to prevent breaking any code that expects the incorrect TypeError to be re-raised?
  2. Just use Exception directly instead (raising bare Exceptions isn't great, and pywin32 only have those in tests)
  3. Re-use an existing exception (pywintypes.error?)
  4. Other?

Fixing this will be necessary to enable the reportCallIssue check in pyright.

@Avasam
Copy link
Collaborator

Avasam commented May 18, 2024

@mhammond I can fix this, but I'm not sure what your preferred approach would be.

@mhammond
Copy link
Owner

These are not exceptions we expect to actually hit or have people catch, so I'd be fine with one of RuntimeError, TypeError or ValueError depending on the scenario - most of the above are probably TypeError I guess? Although ValueError would still be a candidate even though we are dealing with types (ie, in most cases the user isn't specifying the wrong type necessarily, but rather the type doesn't have what we expect.)

I'm open to ideas though - WDYT?

@Avasam
Copy link
Collaborator

Avasam commented May 28, 2024

I think that's gonna have to be a case-by-case.

First thing I noticed is that tests could simply use assert/AssertionError, that'll already safely take care of a handful of these and reduce the scope.

I noticed a few more that can sensibly be COMException instead.

Others are actually unused.

Most valid error = ... are just aliases for pywintypes.error, so I could transform error = "SomeModule error into error = pywintypes.error.

@mhammond
Copy link
Owner

Of the 4 listed above, I don't see how any of them could reasonably be a pythoncom.error - what would the HRESULT be?

I noticed a few more

Sorry, I only looked at those 4 quoted above.

@Avasam
Copy link
Collaborator

Avasam commented May 28, 2024

I meant pywintypes.error 🤦 Anyway, PRs incoming, starting with the tests.

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

Successfully merging a pull request may close this issue.

3 participants