Skip to content

Commit

Permalink
Replace strings by actual Exception subclasses (mhammond#2270)
Browse files Browse the repository at this point in the history
  • Loading branch information
Avasam authored and clin1234 committed Jun 7, 2024
1 parent e4874da commit 08d6c0e
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 28 deletions.
12 changes: 12 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
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
7 changes: 3 additions & 4 deletions Pythonwin/pywin/framework/dlgappcore.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

from . import app

error = "Dialog Application Error"


class AppDialog(dialog.Dialog):
"The dialog box for the application"
Expand Down Expand Up @@ -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()
Expand Down
23 changes: 9 additions & 14 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,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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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_
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 ValueError("Object does not support Dynamic COM Policy")
self._next_dynamic_ = self._min_dynamic_ = 1000
self._dyn_dispid_to_name_ = {
DISPID_VALUE: "_value_",
Expand Down
4 changes: 1 addition & 3 deletions win32/Lib/regutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"

Expand Down Expand Up @@ -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(
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 += 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

0 comments on commit 08d6c0e

Please sign in to comment.