Skip to content

Commit 2e63fe0

Browse files
authored
Replace strings by actual Exception subclasses (#2270)
1 parent 60d7236 commit 2e63fe0

File tree

6 files changed

+31
-28
lines changed

6 files changed

+31
-28
lines changed

CHANGES.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ Coming in build 307, as yet unreleased
1515
--------------------------------------
1616

1717
### pywin32
18+
* Fixed accidentally trying to raise a `str` instead of an `Exception` in (#2270, @Avasam)
19+
* `Pythonwin/pywin/debugger/debugger.py`
20+
* `Pythonwin/pywin/framework/dlgappcore.py`
21+
* `com/win32com/server/policy.py`
22+
* `win32/Lib/regutil.py`
23+
* `win32/scripts/VersionStamp/vssutil.py`
24+
* Removed the following unused symbols. They were meant to be used as Exceptions, but were accidentally strings (#2270, @Avasam)
25+
* `pywin.debugger.debugger.error`
26+
* `pywin.framework.dlgappcore.error`
27+
* `win32com.server.policy.error`
28+
* `regutil.error`
29+
* `win32.scripts.VersionStamp.vssutil.error`
1830
* Add EnumDesktopWindows (#2219, @CristiFati)
1931
* Marked `exc_type` and `exc_traceback` in `win32comext.axscript.client.error.AXScriptException.__init__` as deprecated. (#2236 , @Avasam)
2032
They are now unused and all information is taken from the `exc_value` parameter.

Pythonwin/pywin/debugger/debugger.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
from .dbgcon import *
3030

3131
LVN_ENDLABELEDIT = commctrl.LVN_ENDLABELEDITW
32-
error = "pywin.debugger.error"
3332

3433

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

641640
def prep_run(self, cmd):
642641
pass

Pythonwin/pywin/framework/dlgappcore.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
from . import app
1212

13-
error = "Dialog Application Error"
14-
1513

1614
class AppDialog(dialog.Dialog):
1715
"The dialog box for the application"
@@ -62,8 +60,9 @@ def InitInstance(self):
6260
self.dlg = self.frame = self.CreateDialog()
6361

6462
if self.frame is None:
65-
raise error("No dialog was created by CreateDialog()")
66-
return
63+
raise NotImplementedError(
64+
"No dialog was created by CreateDialog(). Subclasses need to implement CreateDialog."
65+
)
6766

6867
self._obj_.InitDlgInstance(self.dlg)
6968
self.PreDoModal()

com/win32com/server/policy.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -83,26 +83,21 @@
8383
DISPATCH_PROPERTYGET,
8484
DISPATCH_PROPERTYPUT,
8585
DISPATCH_PROPERTYPUTREF,
86-
DISPID_COLLECT,
87-
DISPID_CONSTRUCTOR,
88-
DISPID_DESTRUCTOR,
8986
DISPID_EVALUATE,
9087
DISPID_NEWENUM,
9188
DISPID_PROPERTYPUT,
9289
DISPID_STARTENUM,
93-
DISPID_UNKNOWN,
9490
DISPID_VALUE,
9591
)
9692

93+
from .exception import COMException
94+
9795
S_OK = 0
9896

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

103-
from .exception import COMException
104-
105-
error = __name__ + " error"
106101

107102
regSpec = "CLSID\\%s\\PythonCOM"
108103
regPolicy = "CLSID\\%s\\PythonCOMPolicy"
@@ -210,9 +205,8 @@ def _CreateInstance_(self, clsid, reqIID):
210205
win32con.HKEY_CLASSES_ROOT, regSpec % clsid
211206
)
212207
except win32api.error:
213-
raise error(
214-
"The object is not correctly registered - %s key can not be read"
215-
% (regSpec % clsid)
208+
raise ValueError(
209+
f"The object is not correctly registered - {regSpec % clsid} key can not be read"
216210
)
217211
myob = call_func(classSpec)
218212
self._wrap_(myob)
@@ -361,7 +355,7 @@ def _invokeex_(self, dispid, lcid, wFlags, args, kwargs, serviceProvider):
361355
Simply raises an exception.
362356
"""
363357
# Base classes should override this method (and not call the base)
364-
raise error("This class does not provide _invokeex_ semantics")
358+
raise NotImplementedError("This class does not provide _invokeex_ semantics")
365359

366360
def _DeleteMemberByName_(self, name, fdex):
367361
return self._deletememberbyname_(name, fdex)
@@ -515,8 +509,9 @@ def _wrap_(self, ob):
515509
universal_data = []
516510
MappedWrapPolicy._wrap_(self, ob)
517511
if not hasattr(ob, "_public_methods_") and not hasattr(ob, "_typelib_guid_"):
518-
raise error(
519-
"Object does not support DesignatedWrapPolicy, as it does not have either _public_methods_ or _typelib_guid_ attributes."
512+
raise ValueError(
513+
"Object does not support DesignatedWrapPolicy, "
514+
+ "as it does not have either _public_methods_ or _typelib_guid_ attributes.",
520515
)
521516

522517
# Copy existing _dispid_to_func_ entries to _name_to_dispid_
@@ -732,7 +727,7 @@ class DynamicPolicy(BasicWrapPolicy):
732727
def _wrap_(self, object):
733728
BasicWrapPolicy._wrap_(self, object)
734729
if not hasattr(self._obj_, "_dynamic_"):
735-
raise error("Object does not support Dynamic COM Policy")
730+
raise ValueError("Object does not support Dynamic COM Policy")
736731
self._next_dynamic_ = self._min_dynamic_ = 1000
737732
self._dyn_dispid_to_name_ = {
738733
DISPID_VALUE: "_value_",

win32/Lib/regutil.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
import win32api
66
import win32con
77

8-
error = "Registry utility error"
9-
108
# A .py file has a CLSID associated with it (why? - dunno!)
119
CLSIDPyFile = "{b51df050-06ae-11cf-ad3b-524153480001}"
1210

@@ -78,7 +76,7 @@ def RegisterPythonExe(exeFullPath, exeAlias=None, exeAppPath=None):
7876
"""
7977
# Note - Don't work on win32s (but we don't care anymore!)
8078
if exeAppPath:
81-
raise error("Do not support exeAppPath argument currently")
79+
raise ValueError("Do not support exeAppPath argument currently")
8280
if exeAlias is None:
8381
exeAlias = os.path.basename(exeFullPath)
8482
win32api.RegSetValue(

win32/scripts/VersionStamp/vssutil.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

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

13-
error = "vssutil error"
14-
1513

1614
def GetSS():
1715
ss = win32com.client.Dispatch("SourceSafe")
@@ -172,8 +170,10 @@ def MakeNewBuildNo(project, buildDesc=None, auto=0, bRebrand=0):
172170
if not bRebrand:
173171
buildNo += 1
174172
buildNo = str(buildNo)
175-
except ValueError:
176-
raise error("The previous label could not be incremented: %s" % (oldBuild))
173+
except ValueError as error:
174+
raise ValueError(
175+
f"The previous label could not be incremented: {oldBuild}"
176+
) from error
177177

178178
if not auto:
179179
from pywin.mfc import dialog

0 commit comments

Comments
 (0)