Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 81 additions & 97 deletions com/win32com/test/errorSemantics.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@
from win32com.test.util import CaptureWriter


class error(Exception):
def __init__(self, msg, com_exception=None):
Exception.__init__(self, msg, str(com_exception))


# Our COM server.
class TestServer:
_public_methods_ = ["Clone", "Commit", "LockRegion", "Read"]
Expand Down Expand Up @@ -56,65 +51,62 @@ def test():
com_server = wrap(TestServer(), pythoncom.IID_IStream)
try:
com_server.Clone()
raise error("Expecting this call to fail!")
raise AssertionError("Expecting this call to fail!")
except pythoncom.com_error as com_exc:
if com_exc.hresult != winerror.E_UNEXPECTED:
raise error(
"Calling the object natively did not yield the correct scode", com_exc
)
assert com_exc.hresult == winerror.E_UNEXPECTED, (
"Calling the object natively did not yield the correct scode",
str(com_exc),
)
exc = com_exc.excepinfo
if not exc or exc[-1] != winerror.E_UNEXPECTED:
raise error(
"The scode element of the exception tuple did not yield the correct scode",
com_exc,
)
if exc[2] != "Not today":
raise error(
"The description in the exception tuple did not yield the correct string",
com_exc,
)
assert exc and exc[-1] == winerror.E_UNEXPECTED, (
"The scode element of the exception tuple did not yield the correct scode",
str(com_exc),
)
assert exc[2] == "Not today", (
"The description in the exception tuple did not yield the correct string",
str(com_exc),
)
cap = CaptureWriter()
try:
cap.capture()
try:
com_server.Commit(0)
finally:
cap.release()
raise error("Expecting this call to fail!")
raise AssertionError("Expecting this call to fail!")
except pythoncom.com_error as com_exc:
if com_exc.hresult != winerror.E_FAIL:
raise error("The hresult was not E_FAIL for an internal error", com_exc)
if com_exc.excepinfo[1] != "Python COM Server Internal Error":
raise error(
"The description in the exception tuple did not yield the correct string",
com_exc,
)
assert com_exc.hresult == winerror.E_FAIL, (
"The hresult was not E_FAIL for an internal error",
str(com_exc),
)
assert com_exc.excepinfo[1] == "Python COM Server Internal Error", (
"The description in the exception tuple did not yield the correct string",
str(com_exc),
)
# Check we saw a traceback in stderr
if cap.get_captured().find("Traceback") < 0:
raise error(f"Could not find a traceback in stderr: {cap.get_captured()!r}")
assert (
cap.get_captured().find("Traceback") >= 0
), f"Could not find a traceback in stderr: {cap.get_captured()!r}"

# Now do it all again, but using IDispatch
com_server = Dispatch(wrap(TestServer()))
try:
com_server.Clone()
raise error("Expecting this call to fail!")
raise AssertionError("Expecting this call to fail!")
except pythoncom.com_error as com_exc:
if com_exc.hresult != winerror.DISP_E_EXCEPTION:
raise error(
"Calling the object via IDispatch did not yield the correct scode",
com_exc,
)
assert com_exc.hresult == winerror.DISP_E_EXCEPTION, (
"Calling the object via IDispatch did not yield the correct scode",
str(com_exc),
)
exc = com_exc.excepinfo
if not exc or exc[-1] != winerror.E_UNEXPECTED:
raise error(
"The scode element of the exception tuple did not yield the correct scode",
com_exc,
)
if exc[2] != "Not today":
raise error(
"The description in the exception tuple did not yield the correct string",
com_exc,
)
assert exc and exc[-1] == winerror.E_UNEXPECTED, (
"The scode element of the exception tuple did not yield the correct scode",
str(com_exc),
)
assert exc[2] == "Not today", (
"The description in the exception tuple did not yield the correct string",
str(com_exc),
)

cap.clear()
try:
Expand All @@ -123,27 +115,25 @@ def test():
com_server.Commit(0)
finally:
cap.release()
raise error("Expecting this call to fail!")
raise AssertionError("Expecting this call to fail!")
except pythoncom.com_error as com_exc:
if com_exc.hresult != winerror.DISP_E_EXCEPTION:
raise error(
"Calling the object via IDispatch did not yield the correct scode",
com_exc,
)
assert com_exc.hresult == winerror.DISP_E_EXCEPTION, (
"Calling the object via IDispatch did not yield the correct scode",
str(com_exc),
)
exc = com_exc.excepinfo
if not exc or exc[-1] != winerror.E_FAIL:
raise error(
"The scode element of the exception tuple did not yield the correct scode",
com_exc,
)
if exc[1] != "Python COM Server Internal Error":
raise error(
"The description in the exception tuple did not yield the correct string",
com_exc,
)
assert exc and exc[-1] == winerror.E_FAIL, (
"The scode element of the exception tuple did not yield the correct scode",
str(com_exc),
)
assert exc[1] == "Python COM Server Internal Error", (
"The description in the exception tuple did not yield the correct string",
str(com_exc),
)
# Check we saw a traceback in stderr
if cap.get_captured().find("Traceback") < 0:
raise error(f"Could not find a traceback in stderr: {cap.get_captured()!r}")
assert (
cap.get_captured().find("Traceback") >= 0
), f"Could not find a traceback in stderr: {cap.get_captured()!r}"

# And an explicit com_error
cap.clear()
Expand All @@ -153,39 +143,33 @@ def test():
com_server.Commit(1)
finally:
cap.release()
raise error("Expecting this call to fail!")
raise AssertionError("Expecting this call to fail!")
except pythoncom.com_error as com_exc:
if com_exc.hresult != winerror.DISP_E_EXCEPTION:
raise error(
"Calling the object via IDispatch did not yield the correct scode",
com_exc,
)
assert com_exc.hresult == winerror.DISP_E_EXCEPTION, (
"Calling the object via IDispatch did not yield the correct scode",
str(com_exc),
)
exc = com_exc.excepinfo
if not exc or exc[-1] != winerror.E_FAIL:
raise error(
"The scode element of the exception tuple did not yield the correct scode",
com_exc,
)
if exc[1] != "source":
raise error(
"The source in the exception tuple did not yield the correct string",
com_exc,
)
if exc[2] != "\U0001F600":
raise error(
"The description in the exception tuple did not yield the correct string",
com_exc,
)
if exc[3] != "helpfile":
raise error(
"The helpfile in the exception tuple did not yield the correct string",
com_exc,
)
if exc[4] != 1:
raise error(
"The help context in the exception tuple did not yield the correct string",
com_exc,
)
assert exc and exc[-1] == winerror.E_FAIL, (
"The scode element of the exception tuple did not yield the correct scode",
str(com_exc),
)
assert exc[1] == "source", (
"The source in the exception tuple did not yield the correct string",
str(com_exc),
)
assert exc[2] == "\U0001F600", (
"The description in the exception tuple did not yield the correct string",
str(com_exc),
)
assert exc[3] == "helpfile", (
"The helpfile in the exception tuple did not yield the correct string",
str(com_exc),
)
assert exc[4] == 1, (
"The help context in the exception tuple did not yield the correct string",
str(com_exc),
)


try:
Expand Down Expand Up @@ -225,7 +209,7 @@ def testLogger():
com_server = wrap(TestServer(), pythoncom.IID_IStream)
try:
com_server.Commit(0)
raise RuntimeError("should have failed")
raise AssertionError("should have failed")
except pythoncom.error as exc:
# `excepinfo` is a tuple with elt 2 being the traceback we captured.
message = exc.excepinfo[2]
Expand All @@ -240,7 +224,7 @@ def testLogger():
com_server = Dispatch(wrap(TestServer()))
try:
com_server.Commit(0)
raise RuntimeError("should have failed")
raise AssertionError("should have failed")
except pythoncom.error as exc:
# `excepinfo` is a tuple with elt 2 being the traceback we captured.
message = exc.excepinfo[2]
Expand Down
3 changes: 1 addition & 2 deletions com/win32com/test/pippo_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ def BuildTypelib():
if newer(idl, tlb):
print(f"Compiling {idl}")
rc = os.system(f'midl "{idl}"')
if rc:
raise RuntimeError("Compiling MIDL failed!")
assert not rc, "Compiling MIDL failed!"
# Can't work out how to prevent MIDL from generating the stubs.
# just nuke them
for fname in "dlldata.c pippo_i.c pippo_p.c pippo.h".split():
Expand Down
19 changes: 9 additions & 10 deletions com/win32com/test/policySemantics.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
import winerror


class Error(Exception):
pass


# An object representing a list of numbers
class PythonSemanticClass:
_public_methods_ = ["In"] # DISPIDs are allocated.
Expand Down Expand Up @@ -62,8 +58,13 @@ def DispExTest(ob):
assert hr == winerror.S_FALSE, "Bad result at end of enum"
break
dispids.sort()
if dispids != [pythoncom.DISPID_EVALUATE, pythoncom.DISPID_NEWENUM, 10, 11, 1000]:
raise Error("Got back the wrong dispids: %s" % dispids)
assert dispids == [
pythoncom.DISPID_EVALUATE,
pythoncom.DISPID_NEWENUM,
10,
11,
1000,
], f"Got back the wrong dispids: {dispids}"


def SemanticTest(ob):
Expand All @@ -72,8 +73,7 @@ def SemanticTest(ob):
ob.Add(2)
ob.Add(3)
# invoke _value_
if ob() != (1, 2, 3):
raise Error("Bad result - got %s" % (repr(ob())))
assert ob() == (1, 2, 3), f"Bad result - got {ob()!r}"

dispob = ob._oleobj_

Expand All @@ -83,8 +83,7 @@ def SemanticTest(ob):
pythoncom.DISPATCH_METHOD | pythoncom.DISPATCH_PROPERTYGET,
1,
)
if rc != 6:
raise Error("Evaluate returned %d" % rc)
assert rc == 6, f"Evaluate returned {rc}"


class Tester(win32com.test.util.TestCase):
Expand Down
14 changes: 7 additions & 7 deletions com/win32com/test/testAccess.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,15 @@ def CreateTestAccessDatabase(dbname=None):
# Reset the bookmark to the one we saved.
# But first check the test is actually doing something!
tab1.MoveLast()
if tab1.Fields("First Name").Value != "Second":
raise RuntimeError("Unexpected record is last - makes bookmark test pointless!")
assert (
tab1.Fields("First Name").Value == "Second"
), "Unexpected record is last - makes bookmark test pointless!"

tab1.Bookmark = bk
if tab1.Bookmark != bk:
raise RuntimeError("The bookmark data is not the same")

if tab1.Fields("First Name").Value != "Mark":
raise RuntimeError("The bookmark did not reset the record pointer correctly")
assert tab1.Bookmark == bk, "The bookmark data is not the same"
assert (
tab1.Fields("First Name").Value == "Mark"
), "The bookmark did not reset the record pointer correctly"

return dbname

Expand Down
Loading