From 08d6c0e25d6f208a1483972af35b1899a8e9ccda Mon Sep 17 00:00:00 2001 From: Avasam Date: Tue, 4 Jun 2024 14:09:33 -0400 Subject: [PATCH] Replace strings by actual Exception subclasses (#2270) --- CHANGES.txt | 12 ++++++++++++ Pythonwin/pywin/debugger/debugger.py | 5 ++--- Pythonwin/pywin/framework/dlgappcore.py | 7 +++---- com/win32com/server/policy.py | 23 +++++++++-------------- win32/Lib/regutil.py | 4 +--- win32/scripts/VersionStamp/vssutil.py | 8 ++++---- 6 files changed, 31 insertions(+), 28 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 66426f629..d8a02ad1f 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -15,6 +15,18 @@ Coming in build 307, as yet unreleased -------------------------------------- ### pywin32 +* Fixed accidentally trying to raise a `str` instead of an `Exception` in (#2270, @Avasam) + * `Pythonwin/pywin/debugger/debugger.py` + * `Pythonwin/pywin/framework/dlgappcore.py` + * `com/win32com/server/policy.py` + * `win32/Lib/regutil.py` + * `win32/scripts/VersionStamp/vssutil.py` +* Removed the following unused symbols. They were meant to be used as Exceptions, but were accidentally strings (#2270, @Avasam) + * `pywin.debugger.debugger.error` + * `pywin.framework.dlgappcore.error` + * `win32com.server.policy.error` + * `regutil.error` + * `win32.scripts.VersionStamp.vssutil.error` * Add EnumDesktopWindows (#2219, @CristiFati) * Marked `exc_type` and `exc_traceback` in `win32comext.axscript.client.error.AXScriptException.__init__` as deprecated. (#2236 , @Avasam) They are now unused and all information is taken from the `exc_value` parameter. diff --git a/Pythonwin/pywin/debugger/debugger.py b/Pythonwin/pywin/debugger/debugger.py index 2c2904391..2cddf0ea3 100644 --- a/Pythonwin/pywin/debugger/debugger.py +++ b/Pythonwin/pywin/debugger/debugger.py @@ -29,7 +29,6 @@ from .dbgcon import * LVN_ENDLABELEDIT = commctrl.LVN_ENDLABELEDITW -error = "pywin.debugger.error" def SetInteractiveContext(globs, locs): @@ -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 diff --git a/Pythonwin/pywin/framework/dlgappcore.py b/Pythonwin/pywin/framework/dlgappcore.py index df6169cd3..c023a20e1 100644 --- a/Pythonwin/pywin/framework/dlgappcore.py +++ b/Pythonwin/pywin/framework/dlgappcore.py @@ -10,8 +10,6 @@ from . import app -error = "Dialog Application Error" - class AppDialog(dialog.Dialog): "The dialog box for the application" @@ -62,8 +60,9 @@ def InitInstance(self): self.dlg = self.frame = self.CreateDialog() if self.frame is None: - raise error("No dialog was created by CreateDialog()") - return + raise NotImplementedError( + "No dialog was created by CreateDialog(). Subclasses need to implement CreateDialog." + ) self._obj_.InitDlgInstance(self.dlg) self.PreDoModal() diff --git a/com/win32com/server/policy.py b/com/win32com/server/policy.py index 47f5dd641..fde608d13 100644 --- a/com/win32com/server/policy.py +++ b/com/win32com/server/policy.py @@ -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" @@ -210,9 +205,8 @@ def _CreateInstance_(self, clsid, reqIID): win32con.HKEY_CLASSES_ROOT, regSpec % clsid ) except win32api.error: - raise error( - "The object is not correctly registered - %s key can not be read" - % (regSpec % clsid) + raise ValueError( + f"The object is not correctly registered - {regSpec % clsid} key can not be read" ) myob = call_func(classSpec) self._wrap_(myob) @@ -361,7 +355,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 NotImplementedError("This class does not provide _invokeex_ semantics") def _DeleteMemberByName_(self, name, fdex): return self._deletememberbyname_(name, fdex) @@ -515,8 +509,9 @@ def _wrap_(self, ob): universal_data = [] MappedWrapPolicy._wrap_(self, ob) if not hasattr(ob, "_public_methods_") and not hasattr(ob, "_typelib_guid_"): - raise error( - "Object does not support DesignatedWrapPolicy, as it does not have either _public_methods_ or _typelib_guid_ attributes." + raise ValueError( + "Object does not support DesignatedWrapPolicy, " + + "as it does not have either _public_methods_ or _typelib_guid_ attributes.", ) # Copy existing _dispid_to_func_ entries to _name_to_dispid_ @@ -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 ValueError("Object does not support Dynamic COM Policy") self._next_dynamic_ = self._min_dynamic_ = 1000 self._dyn_dispid_to_name_ = { DISPID_VALUE: "_value_", diff --git a/win32/Lib/regutil.py b/win32/Lib/regutil.py index f85b06b22..cf3ab67f8 100644 --- a/win32/Lib/regutil.py +++ b/win32/Lib/regutil.py @@ -5,8 +5,6 @@ import win32api import win32con -error = "Registry utility error" - # A .py file has a CLSID associated with it (why? - dunno!) CLSIDPyFile = "{b51df050-06ae-11cf-ad3b-524153480001}" @@ -78,7 +76,7 @@ def RegisterPythonExe(exeFullPath, exeAlias=None, exeAppPath=None): """ # Note - Don't work on win32s (but we don't care anymore!) if exeAppPath: - raise error("Do not support exeAppPath argument currently") + raise ValueError("Do not support exeAppPath argument currently") if exeAlias is None: exeAlias = os.path.basename(exeFullPath) win32api.RegSetValue( diff --git a/win32/scripts/VersionStamp/vssutil.py b/win32/scripts/VersionStamp/vssutil.py index ee715757c..9518f51f6 100644 --- a/win32/scripts/VersionStamp/vssutil.py +++ b/win32/scripts/VersionStamp/vssutil.py @@ -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") @@ -172,8 +170,10 @@ def MakeNewBuildNo(project, buildDesc=None, auto=0, bRebrand=0): if not bRebrand: 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