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
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions Pythonwin/pywin/debugger/debugger.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
from .dbgcon import *

LVN_ENDLABELEDIT = commctrl.LVN_ENDLABELEDITW
error = "pywin.debugger.error"


def SetInteractiveContext(globs, locs):
Expand Down Expand Up @@ -635,8 +634,8 @@ def get_option(self, option):
"""Public interface into debugger options"""
try:
return self.options[option]
except KeyError:
raise error("Option %s is not a valid option" % option)
except KeyError as error:
raise KeyError(f"Option {option} is not a valid option") from error

def prep_run(self, cmd):
pass
Expand Down
5 changes: 3 additions & 2 deletions Pythonwin/pywin/framework/dlgappcore.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

from . import app

error = "Dialog Application Error"

class error(Exception):
pass


class AppDialog(dialog.Dialog):
Expand Down Expand Up @@ -63,7 +65,6 @@ def InitInstance(self):

if self.frame is None:
raise error("No dialog was created by CreateDialog()")
return

self._obj_.InitDlgInstance(self.dlg)
self.PreDoModal()
Expand Down
17 changes: 6 additions & 11 deletions com/win32com/server/policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,26 +83,21 @@
DISPATCH_PROPERTYGET,
DISPATCH_PROPERTYPUT,
DISPATCH_PROPERTYPUTREF,
DISPID_COLLECT,
DISPID_CONSTRUCTOR,
DISPID_DESTRUCTOR,
DISPID_EVALUATE,
DISPID_NEWENUM,
DISPID_PROPERTYPUT,
DISPID_STARTENUM,
DISPID_UNKNOWN,
DISPID_VALUE,
)

from .exception import COMException

S_OK = 0

# Few more globals to speed things.
IDispatchType = pythoncom.TypeIIDs[pythoncom.IID_IDispatch]
IUnknownType = pythoncom.TypeIIDs[pythoncom.IID_IUnknown]

from .exception import COMException

error = __name__ + " error"

regSpec = "CLSID\\%s\\PythonCOM"
regPolicy = "CLSID\\%s\\PythonCOMPolicy"
Expand Down Expand Up @@ -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

"The object is not correctly registered - %s key can not be read"
% (regSpec % clsid)
)
Expand Down Expand Up @@ -361,7 +356,7 @@ def _invokeex_(self, dispid, lcid, wFlags, args, kwargs, serviceProvider):
Simply raises an exception.
"""
# Base classes should override this method (and not call the base)
raise error("This class does not provide _invokeex_ semantics")
raise COMException("This class does not provide _invokeex_ semantics")

def _DeleteMemberByName_(self, name, fdex):
return self._deletememberbyname_(name, fdex)
Expand Down Expand Up @@ -515,7 +510,7 @@ def _wrap_(self, ob):
universal_data = []
MappedWrapPolicy._wrap_(self, ob)
if not hasattr(ob, "_public_methods_") and not hasattr(ob, "_typelib_guid_"):
raise error(
raise COMException(
"Object does not support DesignatedWrapPolicy, as it does not have either _public_methods_ or _typelib_guid_ attributes."
)

Expand Down Expand Up @@ -732,7 +727,7 @@ class DynamicPolicy(BasicWrapPolicy):
def _wrap_(self, object):
BasicWrapPolicy._wrap_(self, object)
if not hasattr(self._obj_, "_dynamic_"):
raise error("Object does not support Dynamic COM Policy")
raise COMException("Object does not support Dynamic COM Policy")
self._next_dynamic_ = self._min_dynamic_ = 1000
self._dyn_dispid_to_name_ = {
DISPID_VALUE: "_value_",
Expand Down
5 changes: 4 additions & 1 deletion win32/Lib/regutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
import win32api
import win32con

error = "Registry utility error"

class error(Exception):
pass


# A .py file has a CLSID associated with it (why? - dunno!)
CLSIDPyFile = "{b51df050-06ae-11cf-ad3b-524153480001}"
Expand Down
8 changes: 4 additions & 4 deletions win32/scripts/VersionStamp/vssutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

win32com.client.gencache.EnsureModule("{783CD4E0-9D54-11CF-B8EE-00608CC9A71F}", 0, 5, 0)

error = "vssutil error"


def GetSS():
ss = win32com.client.Dispatch("SourceSafe")
Expand Down Expand Up @@ -172,8 +170,10 @@ def MakeNewBuildNo(project, buildDesc=None, auto=0, bRebrand=0):
if not bRebrand:
buildNo = buildNo + 1
buildNo = str(buildNo)
except ValueError:
raise error("The previous label could not be incremented: %s" % (oldBuild))
except ValueError as error:
raise ValueError(
f"The previous label could not be incremented: {oldBuild}"
) from error

if not auto:
from pywin.mfc import dialog
Expand Down
Loading