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

Replace strings by actual Exception subclasses #2270

Merged
merged 5 commits into from
Jun 4, 2024

Conversation

Avasam
Copy link
Collaborator

@Avasam Avasam commented May 28, 2024

Fixes #1902

There's a few attempted approaches on a case-by-case scenario. You'll need to open the full files to find where raise error( is called.

As a general question, should we try to replace them all with direct exception calls? (RuntimeError, TypeError, ValueError, pywintypes.error, COMException, ...), or should new module-specific exceptions be created?

Same question as in #2269 : Should they still be accessible with a deprecation warning ? Or is it ok to remove them?

(no changelog entry yet as the decision for a solution isn't final)

@@ -210,7 +205,7 @@ def _CreateInstance_(self, clsid, reqIID):
win32con.HKEY_CLASSES_ROOT, regSpec % clsid
)
except win32api.error:
raise error(
raise COMException(
Copy link
Collaborator Author

@Avasam Avasam May 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't even know if these even make sense as COMExceptions, this is not an area I'm familiar with at all. Please advise which exception you'd like to raise.

Maybe just a re-raise with a different message?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a COM exception is not quite correct - maybe ValueError, because ultimately this is complaining that the value of clsid is invalid in this context/on this machine?

I think that's true for all other exceptions in this file - could maybe argue TypeError for some of them, but I don't think it matters in practice - the errors below here are never expected to be hit "for real", but only while the COM object is being developed.

Copy link
Collaborator Author

@Avasam Avasam Jun 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Went with ValueError for most, and NotImplementedError for _invokeex_ as the message, comment and docstring all seem to point to this fitting the semantics of https://docs.python.org/3/library/exceptions.html#NotImplementedError

@Avasam Avasam requested a review from mhammond May 28, 2024 22:03
Copy link
Owner

@mhammond mhammond left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rest looks great though, thanks!

@@ -210,7 +205,7 @@ def _CreateInstance_(self, clsid, reqIID):
win32con.HKEY_CLASSES_ROOT, regSpec % clsid
)
except win32api.error:
raise error(
raise COMException(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a COM exception is not quite correct - maybe ValueError, because ultimately this is complaining that the value of clsid is invalid in this context/on this machine?

I think that's true for all other exceptions in this file - could maybe argue TypeError for some of them, but I don't think it matters in practice - the errors below here are never expected to be hit "for real", but only while the COM object is being developed.

@Avasam Avasam requested a review from mhammond June 4, 2024 17:34
@Avasam Avasam merged commit 2e63fe0 into mhammond:main Jun 4, 2024
27 checks passed
@Avasam Avasam deleted the calling-string-as-errors branch June 4, 2024 18:09
clin1234 pushed a commit to clin1234/pywin32 that referenced this pull request Jun 7, 2024
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 this pull request may close these issues.

win32com/server/policy.py tries to call as string
2 participants