Skip to content

Commit 1340303

Browse files
committed
remove deprecated win32com.server.exception.Exception
1 parent f7d0a79 commit 1340303

22 files changed

Lines changed: 88 additions & 94 deletions

File tree

com/win32com/demos/excelAddin.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
import pythoncom
5252
from win32com import universal
5353
from win32com.client import Dispatch, DispatchWithEvents, constants, gencache
54-
from win32com.server.exception import COMException
5554

5655
# Support for COM objects we use.
5756
gencache.EnsureModule(

com/win32com/demos/outlookAddin.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import pythoncom
3131
from win32com import universal
3232
from win32com.client import DispatchWithEvents, constants, gencache
33-
from win32com.server.exception import COMException
3433

3534
# Support for COM objects we use.
3635
gencache.EnsureModule(

com/win32com/demos/trybag.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pythoncom
2-
from win32com.server import exception, util
2+
from win32com.server import util
3+
from win32com.server.exception import COMException
34

45
VT_EMPTY = pythoncom.VT_EMPTY
56

@@ -18,7 +19,7 @@ def Read(self, propName, varType, errorLog):
1819
hr = 0x80070057
1920
exc = pythoncom.com_error(0, "Bag.Read", "no such item", None, 0, hr)
2021
errorLog.AddError(propName, exc)
21-
raise exception.Exception(scode=hr)
22+
raise COMException(scode=hr)
2223
return self.data[propName]
2324

2425
def Write(self, propName, value):
@@ -31,7 +32,7 @@ class Target:
3132
_com_interfaces_ = [pythoncom.IID_IPersist, pythoncom.IID_IPersistPropertyBag]
3233

3334
def GetClassID(self):
34-
raise exception.Exception(scode=0x80004005) # E_FAIL
35+
raise COMException(scode=0x80004005) # E_FAIL
3536

3637
def InitNew(self):
3738
pass
@@ -41,7 +42,7 @@ def Load(self, bag, log):
4142
print(bag.Read("prop2", VT_EMPTY, log))
4243
try:
4344
print(bag.Read("prop3", VT_EMPTY, log))
44-
except exception.Exception:
45+
except COMException:
4546
pass
4647

4748
def Save(self, bag, clearDirty, saveAllProps):

com/win32com/server/connect.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import winerror
88
from win32com import olectl
99

10-
from .exception import Exception
10+
from .exception import COMException
1111

1212
# Methods implemented by the interfaces.
1313
IConnectionPointContainer_methods = ["EnumConnectionPoints", "FindConnectionPoint"]
@@ -34,10 +34,10 @@ def __init__(self):
3434

3535
# IConnectionPoint interfaces
3636
def EnumConnections(self):
37-
raise Exception(winerror.E_NOTIMPL)
37+
raise COMException(winerror.E_NOTIMPL)
3838

3939
def GetConnectionInterface(self):
40-
raise Exception(winerror.E_NOTIMPL)
40+
raise COMException(winerror.E_NOTIMPL)
4141

4242
def GetConnectionPointContainer(self):
4343
return win32com.server.util.wrap(self)
@@ -50,7 +50,7 @@ def Advise(self, pUnk):
5050
self._connect_interfaces_[0], pythoncom.IID_IDispatch
5151
)
5252
except pythoncom.com_error:
53-
raise Exception(scode=olectl.CONNECT_E_NOCONNECTION)
53+
raise COMException(scode=olectl.CONNECT_E_NOCONNECTION)
5454
self.cookieNo = self.cookieNo + 1
5555
self.connections[self.cookieNo] = interface
5656
return self.cookieNo
@@ -60,11 +60,11 @@ def Unadvise(self, cookie):
6060
try:
6161
del self.connections[cookie]
6262
except KeyError:
63-
raise Exception(scode=winerror.E_UNEXPECTED)
63+
raise COMException(scode=winerror.E_UNEXPECTED)
6464

6565
# IConnectionPointContainer interfaces
6666
def EnumConnectionPoints(self):
67-
raise Exception(winerror.E_NOTIMPL)
67+
raise COMException(winerror.E_NOTIMPL)
6868

6969
def FindConnectionPoint(self, iid):
7070
# Find a connection we support. Only support the single event interface.

com/win32com/server/exception.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import pythoncom
1818

1919

20-
# Note that we derive from com_error, which derives from exceptions.Exception
20+
# Note that we derive from com_error, which derives from builtin Exception
2121
# Also note that we dont support "self.args", as we dont support tuple-unpacking
2222
class COMException(pythoncom.com_error):
2323
"""An Exception object that is understood by the framework.
@@ -81,25 +81,19 @@ def __repr__(self):
8181
return f"<COM Exception - scode={self.scode}, desc={self.description}>"
8282

8383

84-
# Old name for the COMException class.
85-
# Do NOT use the name Exception, as it is now a built-in
86-
# COMException is the new, official name.
87-
Exception = COMException
88-
89-
9084
def IsCOMException(t=None):
9185
if t is None:
9286
t = sys.exc_info()[0]
9387
try:
9488
return issubclass(t, pythoncom.com_error)
95-
except TypeError: # 1.5 in -X mode?
96-
return t is pythoncon.com_error
89+
except TypeError: # t is not a class (likely None or a str)
90+
return t is pythoncom.com_error
9791

9892

9993
def IsCOMServerException(t=None):
10094
if t is None:
10195
t = sys.exc_info()[0]
10296
try:
10397
return issubclass(t, COMException)
104-
except TypeError: # String exception
98+
except TypeError: # t is not a class (likely None or a str)
10599
return 0

com/win32com/server/policy.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@
4646
not be Python. Therefore, general Python exceptions and tracebacks aren't
4747
much use.
4848
49-
In general, there is an Exception class that should be raised, to allow
49+
In general, there is an COMException class that should be raised, to allow
5050
the framework to extract rich COM type error information.
5151
5252
The general rule is that the **only** exception returned from Python COM
53-
Server code should be an Exception instance. Any other Python exception
53+
Server code should be an COMException instance. Any other Python exception
5454
should be considered an implementation bug in the server (if not, it
55-
should be handled, and an appropriate Exception instance raised). Any
55+
should be handled, and an appropriate COMException instance raised). Any
5656
other exception is considered "unexpected", and a dispatcher may take
5757
special action (see Dispatchers above)
5858

com/win32com/servers/interp.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"""
1313

1414
import winerror
15-
from win32com.server.exception import Exception
15+
from win32com.server.exception import COMException
1616

1717

1818
# Expose the Python interpreter.
@@ -33,14 +33,18 @@ def __init__(self):
3333
def Eval(self, exp):
3434
"""Evaluate an expression."""
3535
if not isinstance(exp, str):
36-
raise Exception(desc="Must be a string", scode=winerror.DISP_E_TYPEMISMATCH)
36+
raise COMException(
37+
desc="Must be a string", scode=winerror.DISP_E_TYPEMISMATCH
38+
)
3739

3840
return eval(str(exp), self.dict)
3941

4042
def Exec(self, exp):
4143
"""Execute a statement."""
4244
if not isinstance(exp, str):
43-
raise Exception(desc="Must be a string", scode=winerror.DISP_E_TYPEMISMATCH)
45+
raise COMException(
46+
desc="Must be a string", scode=winerror.DISP_E_TYPEMISMATCH
47+
)
4448
exec(str(exp), self.dict)
4549

4650

com/win32com/servers/perfmon.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
import pythoncom
88
import win32pdhutil
99
import winerror
10-
from win32com.server import exception, register
10+
from win32com.server import register
11+
from win32com.server.exception import COMException
1112

1213

1314
class PerfMonQuery:
@@ -24,9 +25,9 @@ def Query(self, object, counter, instance=None, machine=None):
2425
object, counter, instance, machine=machine
2526
)
2627
except win32pdhutil.error as exc:
27-
raise exception.Exception(desc=exc.strerror)
28+
raise COMException(desc=exc.strerror)
2829
except TypeError as desc:
29-
raise exception.Exception(desc=desc, scode=winerror.DISP_E_TYPEMISMATCH)
30+
raise COMException(desc=desc, scode=winerror.DISP_E_TYPEMISMATCH)
3031

3132

3233
if __name__ == "__main__":

com/win32com/test/testDynamic.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import pythoncom
44
import winerror
5-
from win32com.server.exception import Exception
5+
from win32com.server.exception import COMException
66

77
error = "testDynamic error"
88

@@ -22,19 +22,19 @@ def _dynamic_(self, name, lcid, wFlags, args):
2222
ret = list(ret)
2323
return ret
2424
except KeyError: # Probably a method request.
25-
raise Exception(scode=winerror.DISP_E_MEMBERNOTFOUND)
25+
raise COMException(scode=winerror.DISP_E_MEMBERNOTFOUND)
2626

2727
if wFlags & (
2828
pythoncom.DISPATCH_PROPERTYPUT | pythoncom.DISPATCH_PROPERTYPUTREF
2929
):
3030
setattr(self, name, args[0])
3131
return
3232

33-
raise Exception(scode=winerror.E_INVALIDARG, desc="invalid wFlags")
33+
raise COMException(scode=winerror.E_INVALIDARG, desc="invalid wFlags")
3434

3535
def write(self, *args):
3636
if len(args) == 0:
37-
raise Exception(
37+
raise COMException(
3838
scode=winerror.DISP_E_BADPARAMCOUNT
3939
) # Probably call as PROPGET.
4040

com/win32comext/axdebug/Test/host.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import winerror
77
from win32com.axdebug import codecontainer, gateways
88
from win32com.axdebug.util import _wrap, trace
9-
from win32com.server.exception import Exception
9+
from win32com.server.exception import COMException
1010

1111

1212
class ExternalConnection:
@@ -57,7 +57,7 @@ def _GetCodeContainer(self):
5757
try:
5858
codeText = open(self.module.__file__, "rt").read()
5959
except OSError as details:
60-
codeText = f"# Exception opening file\n# {details}"
60+
codeText = f"# COMException opening file\n# {details}"
6161

6262
self.codeContainer = codecontainer.SourceCodeContainer(
6363
codeText, self.module.__file__
@@ -79,23 +79,23 @@ def GetDeferredText(self, dwTextStartCookie, maxChars, bWantAttr):
7979
def GetScriptTextAttributes(self, codeText, delimterText, flags):
8080
# Result must be an attribute sequence of same "length" as the code.
8181
trace("GetScriptTextAttributes", delimterText, flags)
82-
raise Exception(scode=winerror.E_NOTIMPL)
82+
raise COMException(scode=winerror.E_NOTIMPL)
8383

8484
def OnCreateDocumentContext(self):
8585
# Result must be a PyIUnknown
8686
trace("OnCreateDocumentContext")
87-
raise Exception(scode=winerror.E_NOTIMPL)
87+
raise COMException(scode=winerror.E_NOTIMPL)
8888

8989
def GetPathName(self):
9090
# Result must be (string, int) where the int is a BOOL
9191
# - TRUE if the path refers to the original file for the document.
9292
# - FALSE if the path refers to a newly created temporary file.
93-
# - raise Exception(scode=E_FAIL) if no source file can be created/determined.
93+
# - raise COMException(scode=E_FAIL) if no source file can be created/determined.
9494
trace("GetPathName")
9595
try:
9696
return win32api.GetFullPathName(self.module.__file__), 1
9797
except (AttributeError, win32api.error):
98-
raise Exception(scode=winerror.E_FAIL)
98+
raise COMException(scode=winerror.E_FAIL)
9999

100100
def GetFileName(self):
101101
# Result is a string with just the name of the document, no path information.
@@ -104,7 +104,7 @@ def GetFileName(self):
104104

105105
def NotifyChanged():
106106
trace("NotifyChanged")
107-
raise Exception(scode=winerror.E_NOTIMPL)
107+
raise COMException(scode=winerror.E_NOTIMPL)
108108

109109

110110
def TestSmartProvider():

0 commit comments

Comments
 (0)