Skip to content

Commit

Permalink
remove deprecated win32com.server.exception.Exception (#2142)
Browse files Browse the repository at this point in the history
  • Loading branch information
Avasam committed Mar 14, 2024
1 parent 9903dc7 commit ccf58b8
Show file tree
Hide file tree
Showing 22 changed files with 92 additions and 95 deletions.
1 change: 0 additions & 1 deletion com/win32com/demos/excelAddin.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
import pythoncom
from win32com import universal
from win32com.client import Dispatch, DispatchWithEvents, constants, gencache
from win32com.server.exception import COMException

# Support for COM objects we use.
gencache.EnsureModule(
Expand Down
1 change: 0 additions & 1 deletion com/win32com/demos/outlookAddin.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import pythoncom
from win32com import universal
from win32com.client import DispatchWithEvents, constants, gencache
from win32com.server.exception import COMException

# Support for COM objects we use.
gencache.EnsureModule(
Expand Down
9 changes: 5 additions & 4 deletions com/win32com/demos/trybag.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pythoncom
from win32com.server import exception, util
from win32com.server import util
from win32com.server.exception import COMException

VT_EMPTY = pythoncom.VT_EMPTY

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

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

def GetClassID(self):
raise exception.Exception(scode=0x80004005) # E_FAIL
raise COMException(scode=0x80004005) # E_FAIL

def InitNew(self):
pass
Expand All @@ -41,7 +42,7 @@ def Load(self, bag, log):
print(bag.Read("prop2", VT_EMPTY, log))
try:
print(bag.Read("prop3", VT_EMPTY, log))
except exception.Exception:
except COMException:
pass

def Save(self, bag, clearDirty, saveAllProps):
Expand Down
12 changes: 6 additions & 6 deletions com/win32com/server/connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import winerror
from win32com import olectl

from .exception import Exception
from .exception import COMException

# Methods implemented by the interfaces.
IConnectionPointContainer_methods = ["EnumConnectionPoints", "FindConnectionPoint"]
Expand All @@ -35,10 +35,10 @@ def __init__(self):

# IConnectionPoint interfaces
def EnumConnections(self):
raise Exception(winerror.E_NOTIMPL)
raise COMException(winerror.E_NOTIMPL)

def GetConnectionInterface(self):
raise Exception(winerror.E_NOTIMPL)
raise COMException(winerror.E_NOTIMPL)

def GetConnectionPointContainer(self):
return win32com.server.util.wrap(self)
Expand All @@ -51,7 +51,7 @@ def Advise(self, pUnk):
self._connect_interfaces_[0], pythoncom.IID_IDispatch
)
except pythoncom.com_error:
raise Exception(scode=olectl.CONNECT_E_NOCONNECTION)
raise COMException(scode=olectl.CONNECT_E_NOCONNECTION)
self.cookieNo = self.cookieNo + 1
self.connections[self.cookieNo] = interface
return self.cookieNo
Expand All @@ -61,11 +61,11 @@ def Unadvise(self, cookie):
try:
del self.connections[cookie]
except KeyError:
raise Exception(scode=winerror.E_UNEXPECTED)
raise COMException(scode=winerror.E_UNEXPECTED)

# IConnectionPointContainer interfaces
def EnumConnectionPoints(self):
raise Exception(winerror.E_NOTIMPL)
raise COMException(winerror.E_NOTIMPL)

def FindConnectionPoint(self, iid):
# Find a connection we support. Only support the single event interface.
Expand Down
19 changes: 8 additions & 11 deletions com/win32com/server/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import pythoncom


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


# Old name for the COMException class.
# Do NOT use the name Exception, as it is now a built-in
# COMException is the new, official name.
Exception = COMException


def IsCOMException(t=None):
if t is None:
t = sys.exc_info()[0]
if not t is type:
# t is not a class (likely None or a str)
return False
return issubclass(t, pythoncom.com_error)


def IsCOMServerException(t=None):
if t is None:
t = sys.exc_info()[0]
try:
return issubclass(t, COMException)
except TypeError: # String exception
return 0
if not t is type:
# t is not a class (likely None or a str)
return False
return issubclass(t, COMException)
6 changes: 3 additions & 3 deletions com/win32com/server/policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@
not be Python. Therefore, general Python exceptions and tracebacks aren't
much use.
In general, there is an Exception class that should be raised, to allow
In general, there is an COMException class that should be raised, to allow
the framework to extract rich COM type error information.
The general rule is that the **only** exception returned from Python COM
Server code should be an Exception instance. Any other Python exception
Server code should be an COMException instance. Any other Python exception
should be considered an implementation bug in the server (if not, it
should be handled, and an appropriate Exception instance raised). Any
should be handled, and an appropriate COMException instance raised). Any
other exception is considered "unexpected", and a dispatcher may take
special action (see Dispatchers above)
Expand Down
10 changes: 7 additions & 3 deletions com/win32com/servers/interp.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"""

import winerror
from win32com.server.exception import Exception
from win32com.server.exception import COMException


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

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

def Exec(self, exp):
"""Execute a statement."""
if not isinstance(exp, str):
raise Exception(desc="Must be a string", scode=winerror.DISP_E_TYPEMISMATCH)
raise COMException(
desc="Must be a string", scode=winerror.DISP_E_TYPEMISMATCH
)
exec(str(exp), self.dict)


Expand Down
7 changes: 4 additions & 3 deletions com/win32com/servers/perfmon.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import pythoncom
import win32pdhutil
import winerror
from win32com.server import exception, register
from win32com.server import register
from win32com.server.exception import COMException


class PerfMonQuery:
Expand All @@ -25,9 +26,9 @@ def Query(self, object, counter, instance=None, machine=None):
object, counter, instance, machine=machine
)
except win32pdhutil.error as exc:
raise exception.Exception(desc=exc.strerror)
raise COMException(desc=exc.strerror)
except TypeError as desc:
raise exception.Exception(desc=desc, scode=winerror.DISP_E_TYPEMISMATCH)
raise COMException(desc=desc, scode=winerror.DISP_E_TYPEMISMATCH)


if __name__ == "__main__":
Expand Down
8 changes: 4 additions & 4 deletions com/win32com/test/testDynamic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pythoncom
import winerror
from win32com.server.exception import Exception
from win32com.server.exception import COMException

error = "testDynamic error"

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

if wFlags & (
pythoncom.DISPATCH_PROPERTYPUT | pythoncom.DISPATCH_PROPERTYPUTREF
):
setattr(self, name, args[0])
return

raise Exception(scode=winerror.E_INVALIDARG, desc="invalid wFlags")
raise COMException(scode=winerror.E_INVALIDARG, desc="invalid wFlags")

def write(self, *args):
if len(args) == 0:
raise Exception(
raise COMException(
scode=winerror.DISP_E_BADPARAMCOUNT
) # Probably call as PROPGET.

Expand Down
14 changes: 7 additions & 7 deletions com/win32comext/axdebug/Test/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import winerror
from win32com.axdebug import codecontainer, gateways
from win32com.axdebug.util import _wrap, trace
from win32com.server.exception import Exception
from win32com.server.exception import COMException


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

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

def OnCreateDocumentContext(self):
# Result must be a PyIUnknown
trace("OnCreateDocumentContext")
raise Exception(scode=winerror.E_NOTIMPL)
raise COMException(scode=winerror.E_NOTIMPL)

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

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

def NotifyChanged():
trace("NotifyChanged")
raise Exception(scode=winerror.E_NOTIMPL)
raise COMException(scode=winerror.E_NOTIMPL)


def TestSmartProvider():
Expand Down
10 changes: 5 additions & 5 deletions com/win32comext/axdebug/codecontainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import winerror
from win32com.axdebug import axdebug, contexts
from win32com.axdebug.util import _wrap
from win32com.server.exception import Exception
from win32com.server.exception import COMException

_keywords = {} # set of Python keywords
for name in """
Expand Down Expand Up @@ -65,7 +65,7 @@ def GetPositionOfLine(self, cLineNumber):
try:
return self.lineOffsets[cLineNumber]
except IndexError:
raise Exception(scode=winerror.S_FALSE)
raise COMException(scode=winerror.S_FALSE)

def GetLineOfPosition(self, charPos):
self.GetText() # Prime us.
Expand All @@ -78,7 +78,7 @@ def GetLineOfPosition(self, charPos):
lineNo = lineNo + 1
else: # for not broken.
# print("Cant find", charPos, "in", self.lineOffsets)
raise Exception(scode=winerror.S_FALSE)
raise COMException(scode=winerror.S_FALSE)
# print("GLOP ret=", lineNo, (charPos - lastOffset))
return lineNo, (charPos - lastOffset)

Expand Down Expand Up @@ -225,7 +225,7 @@ def GetText(self):
try:
self.text = open(fname, "r").read()
except OSError as details:
self.text = f"# Exception opening file\n# {repr(details)}"
self.text = f"# COMException opening file\n# {repr(details)}"
else:
self.text = f"# No file available for module '{self.module}'"
self._buildlines()
Expand All @@ -248,7 +248,7 @@ def GetName(self, dnt):
elif dnt == axdebug.DOCUMENTNAMETYPE_URL:
return f"file:{fname}"
else:
raise Exception(scode=winerror.E_UNEXPECTED)
raise COMException(scode=winerror.E_UNEXPECTED)


if __name__ == "__main__":
Expand Down
10 changes: 5 additions & 5 deletions com/win32comext/axdebug/gateways.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import winerror
from win32com.axdebug import axdebug
from win32com.axdebug.util import RaiseNotImpl, _wrap
from win32com.server.exception import Exception
from win32com.server.exception import COMException
from win32com.server.util import ListEnumeratorGateway


Expand Down Expand Up @@ -233,7 +233,7 @@ def GetPathName(self):
- if fIsOriginalPath is TRUE if the path refers to the original file for the document.
- if fIsOriginalPath is FALSE if the path refers to a newly created temporary file.
raise Exception(winerror.E_FAIL) if no source file can be created/determined.
raise COMException(winerror.E_FAIL) if no source file can be created/determined.
"""
RaiseNotImpl("GetPathName")

Expand Down Expand Up @@ -398,7 +398,7 @@ def GetPathName(self):
# Result must be (string, int) where the int is a BOOL
# - TRUE if the path refers to the original file for the document.
# - FALSE if the path refers to a newly created temporary file.
# - raise Exception(scode=E_FAIL) if no source file can be created/determined.
# - raise COMException(scode=E_FAIL) if no source file can be created/determined.
RaiseNotImpl("GetPathName")

def GetFileName(self):
Expand Down Expand Up @@ -449,7 +449,7 @@ def Unadvise(self, cookie):
try:
del self.connections[cookie]
except KeyError:
return Exception(scode=winerror.E_UNEXPECTED)
return COMException(scode=winerror.E_UNEXPECTED)

# IConnectionPointContainer interfaces
def EnumConnectionPoints(self):
Expand All @@ -459,7 +459,7 @@ def FindConnectionPoint(self, iid):
# Find a connection we support. Only support the single event interface.
if iid == axdebug.IID_IDebugDocumentTextEvents:
return _wrap(self)
raise Exception(scode=winerror.E_NOINTERFACE) # ??
raise COMException(scode=winerror.E_NOINTERFACE) # ??


class RemoteDebugApplicationEvents:
Expand Down
Loading

0 comments on commit ccf58b8

Please sign in to comment.