From cd15145ed67441c45929b1798d3b72d4147e9e1c Mon Sep 17 00:00:00 2001 From: junkmd Date: Mon, 9 Dec 2024 23:45:20 +0900 Subject: [PATCH 1/6] Add `VariantTest` to `test_comserver`. --- comtypes/test/test_comserver.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/comtypes/test/test_comserver.py b/comtypes/test/test_comserver.py index 56dcc85d..4254b2b5 100644 --- a/comtypes/test/test_comserver.py +++ b/comtypes/test/test_comserver.py @@ -5,6 +5,7 @@ import comtypes.test.TestComServer from comtypes import BSTR +from comtypes.automation import VARIANT from comtypes.client import CreateObject from comtypes.server.register import register, unregister from comtypes.test.find_memleak import find_memleak @@ -170,6 +171,22 @@ def test_mixedinout(self): pass +class VariantTest(unittest.TestCase): + def test_UDT(self): + from comtypes.gen.TestComServerLib import MYCOLOR + + v = VARIANT(MYCOLOR(red=1.0, green=2.0, blue=3.0)) + value = v.value + self.assertEqual((1.0, 2.0, 3.0), (value.red, value.green, value.blue)) + + def func(): + v = VARIANT(MYCOLOR(red=1.0, green=2.0, blue=3.0)) + return v.value + + bytes = find_memleak(func) + self.assertFalse(bytes, "Leaks %d bytes" % bytes) + + class TestEvents(unittest.TestCase): def test(self): import comtypes.test.test_comserver From 6faff56f1f41cca9a7b438610bbc1967c8ef8b14 Mon Sep 17 00:00:00 2001 From: junkmd Date: Mon, 9 Dec 2024 23:45:20 +0900 Subject: [PATCH 2/6] Add `SafeArrayTest` to `test_comserver`. --- comtypes/test/test_comserver.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/comtypes/test/test_comserver.py b/comtypes/test/test_comserver.py index 4254b2b5..dda39b81 100644 --- a/comtypes/test/test_comserver.py +++ b/comtypes/test/test_comserver.py @@ -5,7 +5,7 @@ import comtypes.test.TestComServer from comtypes import BSTR -from comtypes.automation import VARIANT +from comtypes.automation import VARIANT, _midlSAFEARRAY from comtypes.client import CreateObject from comtypes.server.register import register, unregister from comtypes.test.find_memleak import find_memleak @@ -187,6 +187,27 @@ def func(): self.assertFalse(bytes, "Leaks %d bytes" % bytes) +class SafeArrayTest(unittest.TestCase): + def test_UDT(self): + from comtypes.gen.TestComServerLib import MYCOLOR + + t = _midlSAFEARRAY(MYCOLOR) + self.assertTrue(t is _midlSAFEARRAY(MYCOLOR)) + + sa = t.from_param([MYCOLOR(0, 0, 0), MYCOLOR(1, 2, 3)]) + + self.assertEqual( + [(x.red, x.green, x.blue) for x in sa[0]], + [(0.0, 0.0, 0.0), (1.0, 2.0, 3.0)], + ) + + def doit(): + t.from_param([MYCOLOR(0, 0, 0), MYCOLOR(1, 2, 3)]) + + bytes = find_memleak(doit) + self.assertFalse(bytes, "Leaks %d bytes" % bytes) + + class TestEvents(unittest.TestCase): def test(self): import comtypes.test.test_comserver From 68ff423c9e3737991127d21cb2fb08e824b8ce69 Mon Sep 17 00:00:00 2001 From: junkmd Date: Mon, 9 Dec 2024 23:45:20 +0900 Subject: [PATCH 3/6] Add `PropPutRefTest` to `test_comserver`. --- comtypes/test/test_comserver.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/comtypes/test/test_comserver.py b/comtypes/test/test_comserver.py index dda39b81..b6373b3d 100644 --- a/comtypes/test/test_comserver.py +++ b/comtypes/test/test_comserver.py @@ -208,6 +208,35 @@ def doit(): self.assertFalse(bytes, "Leaks %d bytes" % bytes) +class PropPutRefTest(unittest.TestCase): + def test(self, dynamic=False): + d = CreateObject("Scripting.Dictionary", dynamic=dynamic) + s = CreateObject("TestComServerLib.TestComServer", dynamic=dynamic) + s.name = "the value" + + # This calls propputref, since we assign an Object + d.Item["object"] = s + # This calls propput, since we assing a Value + d.Item["value"] = s.name + + self.assertEqual(d.Item["object"], s) + self.assertEqual(d.Item["object"].name, "the value") + self.assertEqual(d.Item["value"], "the value") + + # Changing the default property of the object + s.name = "foo bar" + self.assertEqual(d.Item["object"], s) + self.assertEqual(d.Item["object"].name, "foo bar") + self.assertEqual(d.Item["value"], "the value") + + # This also calls propputref since we assign an Object + d.Item["var"] = VARIANT(s) + self.assertEqual(d.Item["var"], s) + + def test_dispatch(self): + return self.test(dynamic=True) + + class TestEvents(unittest.TestCase): def test(self): import comtypes.test.test_comserver From 88959c83d646b78984508c678df52bf04367e491 Mon Sep 17 00:00:00 2001 From: junkmd Date: Mon, 9 Dec 2024 23:45:20 +0900 Subject: [PATCH 4/6] Remove duplicated tests from `test_propputref`, `test_safearray` and `test_variant`. --- comtypes/test/test_propputref.py | 40 -------------------------------- comtypes/test/test_safearray.py | 23 ------------------ comtypes/test/test_variant.py | 17 -------------- 3 files changed, 80 deletions(-) delete mode 100644 comtypes/test/test_propputref.py diff --git a/comtypes/test/test_propputref.py b/comtypes/test/test_propputref.py deleted file mode 100644 index 8f6c7537..00000000 --- a/comtypes/test/test_propputref.py +++ /dev/null @@ -1,40 +0,0 @@ -# There are also propputref tests in test_sapi.py! -import unittest -from comtypes.client import CreateObject -from comtypes.automation import VARIANT - - -class Test(unittest.TestCase): - @unittest.skip( - "Fails on creating `TestComServerLib.TestComServer`. Figure out why." - ) - def test(self, dynamic=False): - d = CreateObject("Scripting.Dictionary", dynamic=dynamic) - s = CreateObject("TestComServerLib.TestComServer", dynamic=dynamic) - s.name = "the value" - - # This calls propputref, since we assign an Object - d.Item["object"] = s - # This calls propput, since we assing a Value - d.Item["value"] = s.name - - self.assertEqual(d.Item["object"], s) - self.assertEqual(d.Item["object"].name, "the value") - self.assertEqual(d.Item["value"], "the value") - - # Changing the default property of the object - s.name = "foo bar" - self.assertEqual(d.Item["object"], s) - self.assertEqual(d.Item["object"].name, "foo bar") - self.assertEqual(d.Item["value"], "the value") - - # This also calls propputref since we assign an Object - d.Item["var"] = VARIANT(s) - self.assertEqual(d.Item["var"], s) - - def test_dispatch(self): - return self.test(dynamic=True) - - -if __name__ == "__main__": - unittest.main() diff --git a/comtypes/test/test_safearray.py b/comtypes/test/test_safearray.py index 3c9bd18e..49238020 100644 --- a/comtypes/test/test_safearray.py +++ b/comtypes/test/test_safearray.py @@ -221,29 +221,6 @@ def test_VT_UNKNOWN_multi(self): del sa self.assertEqual((a, b), (com_refcnt(plib), com_refcnt(punk))) - @unittest.skip( - "This fails with a 'library not registered' error. Need to figure out how to " - "register TestComServerLib (without admin if possible)." - ) - def test_UDT(self): - from comtypes.gen.TestComServerLib import MYCOLOR - - t = _midlSAFEARRAY(MYCOLOR) - self.assertTrue(t is _midlSAFEARRAY(MYCOLOR)) - - sa = t.from_param([MYCOLOR(0, 0, 0), MYCOLOR(1, 2, 3)]) - - self.assertEqual( - [(x.red, x.green, x.blue) for x in sa[0]], - [(0.0, 0.0, 0.0), (1.0, 2.0, 3.0)], - ) - - def doit(): - t.from_param([MYCOLOR(0, 0, 0), MYCOLOR(1, 2, 3)]) - - bytes = find_memleak(doit) - self.assertFalse(bytes, "Leaks %d bytes" % bytes) - if __name__ == "__main__": unittest.main() diff --git a/comtypes/test/test_variant.py b/comtypes/test/test_variant.py index f4d2a992..88db242e 100644 --- a/comtypes/test/test_variant.py +++ b/comtypes/test/test_variant.py @@ -194,23 +194,6 @@ def test_empty_BSTR(self): v.value = "" self.assertEqual(v.vt, VT_BSTR) - @unittest.skip( - "Fails on creating `TestComServerLib.TestComServer`. Library not registered." - ) - def test_UDT(self): - from comtypes.gen.TestComServerLib import MYCOLOR - - v = VARIANT(MYCOLOR(red=1.0, green=2.0, blue=3.0)) - value = v.value - self.assertEqual((1.0, 2.0, 3.0), (value.red, value.green, value.blue)) - - def func(): - v = VARIANT(MYCOLOR(red=1.0, green=2.0, blue=3.0)) - return v.value - - bytes = find_memleak(func) - self.assertFalse(bytes, "Leaks %d bytes" % bytes) - def test_ctypes_in_variant(self): v = VARIANT() objs = [ From bf74f6126b6ee3fbd717394736b66ea3a2268fc0 Mon Sep 17 00:00:00 2001 From: junkmd Date: Mon, 9 Dec 2024 23:45:20 +0900 Subject: [PATCH 5/6] Improve `VariantTest.test_UDT` in `test_comserver`. --- comtypes/test/test_comserver.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/comtypes/test/test_comserver.py b/comtypes/test/test_comserver.py index b6373b3d..e0ba99f6 100644 --- a/comtypes/test/test_comserver.py +++ b/comtypes/test/test_comserver.py @@ -177,7 +177,9 @@ def test_UDT(self): v = VARIANT(MYCOLOR(red=1.0, green=2.0, blue=3.0)) value = v.value - self.assertEqual((1.0, 2.0, 3.0), (value.red, value.green, value.blue)) + self.assertEqual(1.0, value.red) # type: ignore + self.assertEqual(2.0, value.green) # type: ignore + self.assertEqual(3.0, value.blue) # type: ignore def func(): v = VARIANT(MYCOLOR(red=1.0, green=2.0, blue=3.0)) From 5dda2b562a06c0274dbaca83fbb8aca79d097a23 Mon Sep 17 00:00:00 2001 From: junkmd Date: Mon, 9 Dec 2024 23:45:21 +0900 Subject: [PATCH 6/6] Improve `PropPutRefTest` in `test_comserver`. --- comtypes/test/test_comserver.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/comtypes/test/test_comserver.py b/comtypes/test/test_comserver.py index e0ba99f6..23fd0c35 100644 --- a/comtypes/test/test_comserver.py +++ b/comtypes/test/test_comserver.py @@ -211,7 +211,7 @@ def doit(): class PropPutRefTest(unittest.TestCase): - def test(self, dynamic=False): + def doit(self, dynamic: bool): d = CreateObject("Scripting.Dictionary", dynamic=dynamic) s = CreateObject("TestComServerLib.TestComServer", dynamic=dynamic) s.name = "the value" @@ -235,8 +235,11 @@ def test(self, dynamic=False): d.Item["var"] = VARIANT(s) self.assertEqual(d.Item["var"], s) - def test_dispatch(self): - return self.test(dynamic=True) + def test_earlybind(self): + self.doit(dynamic=False) + + def test_latebind(self): + self.doit(dynamic=True) class TestEvents(unittest.TestCase):