From 2091c9c250857fe89507b7688d8f805b77b9e52c Mon Sep 17 00:00:00 2001 From: junkmd Date: Sun, 25 Jan 2026 13:18:34 +0900 Subject: [PATCH 1/4] test: Add tests for `CreateTypeLib` documentation and `CreateTypeInfo`. This commit introduces `Test_CreateTypeLib` to `test_typeinfo.py`, verifying `ICreateTypeLib`'s ability to set type library documentation. --- comtypes/test/test_typeinfo.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/comtypes/test/test_typeinfo.py b/comtypes/test/test_typeinfo.py index 9792cde0..70afd773 100644 --- a/comtypes/test/test_typeinfo.py +++ b/comtypes/test/test_typeinfo.py @@ -1,12 +1,15 @@ import ctypes import os import sys +import tempfile import unittest from _ctypes import COMError from ctypes.wintypes import MAX_PATH +from pathlib import Path from comtypes import GUID, hresult, typeinfo from comtypes.typeinfo import ( + CreateTypeLib, GetModuleFileName, LoadRegTypeLib, LoadTypeLibEx, @@ -209,6 +212,33 @@ def test_non_existent_name(self): tcomp.Bind("NonExistentNameForTest") +class Test_CreateTypeLib(unittest.TestCase): + def setUp(self): + td = tempfile.TemporaryDirectory() + self.addCleanup(td.cleanup) + self.tmpdir = Path(td.name) + self.typelib_path = self.tmpdir / "test.tlb" + + def test_Documentation(self): + ctlib = CreateTypeLib(str(self.typelib_path)) + libname = "MyTestTypeLib" + docstring = "This is a test type library docstring." + helpctx = 123 + helpfile = "myhelp.chm" + ctlib.SetName(libname) + ctlib.SetDocString(docstring) + ctlib.SetHelpContext(helpctx) + ctlib.SetHelpFileName(helpfile) + ctlib.SaveAllChanges() + # Verify by loading the created typelib + tlib = LoadTypeLibEx(str(self.typelib_path)) + doc = tlib.GetDocumentation(-1) + self.assertEqual( + doc[:-1] + (Path(doc[-1].strip("\0")).name,), # type: ignore + (libname, docstring, helpctx, helpfile), + ) + + class Test_GetModuleFileName(unittest.TestCase): @unittest.skipUnless( sys.prefix == sys.base_prefix, From 37b0ec2af5cc4812992c1205a017a5e546c81165 Mon Sep 17 00:00:00 2001 From: junkmd Date: Sun, 25 Jan 2026 13:18:34 +0900 Subject: [PATCH 2/4] test: Add `test_CreateTypeInfo` to `Test_CreateTypeLib`. This adds a test for the `ICreateTypeLib.CreateTypeInfo` method, ensuring that it correctly creates an `ICreateTypeInfo` instance. --- comtypes/test/test_typeinfo.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/comtypes/test/test_typeinfo.py b/comtypes/test/test_typeinfo.py index 70afd773..272cbd0d 100644 --- a/comtypes/test/test_typeinfo.py +++ b/comtypes/test/test_typeinfo.py @@ -238,6 +238,14 @@ def test_Documentation(self): (libname, docstring, helpctx, helpfile), ) + def test_CreateTypeInfo(self): + ctlib = CreateTypeLib(str(self.typelib_path)) + # Create a type info + self.assertIsInstance( + ctlib.CreateTypeInfo("IMyInterface", typeinfo.TKIND_INTERFACE), + typeinfo.ICreateTypeInfo, + ) + class Test_GetModuleFileName(unittest.TestCase): @unittest.skipUnless( From f41af7769d9d9141d2e74334c6be5b8230a0a3c5 Mon Sep 17 00:00:00 2001 From: junkmd Date: Sun, 25 Jan 2026 13:18:34 +0900 Subject: [PATCH 3/4] test: Add `test_LibAttr` to verify `ICreateTypeLib` attribute setters. This test ensures that `ICreateTypeLib` methods like `SetGuid`, `SetLcid`, `SetVersion`, and `SetLibFlags` correctly persist library attributes. It verifies these attributes by loading the created type library and comparing the `LIBATTR` fields. --- comtypes/test/test_typeinfo.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/comtypes/test/test_typeinfo.py b/comtypes/test/test_typeinfo.py index 272cbd0d..086c3e06 100644 --- a/comtypes/test/test_typeinfo.py +++ b/comtypes/test/test_typeinfo.py @@ -238,6 +238,27 @@ def test_Documentation(self): (libname, docstring, helpctx, helpfile), ) + def test_LibAttr(self): + ctlib = CreateTypeLib(str(self.typelib_path)) + libid = GUID.create_new() + lcid = 1033 # English (United States) + LIBFLAG_FRESTRICTED = 0x1 + major_version = 1 + minor_version = 2 + ctlib.SetGuid(libid) + ctlib.SetLcid(lcid) + ctlib.SetVersion(major_version, minor_version) + ctlib.SetLibFlags(LIBFLAG_FRESTRICTED) + ctlib.SaveAllChanges() + # Verify by loading the created typelib + tlib = LoadTypeLibEx(str(self.typelib_path)) + la = tlib.GetLibAttr() + self.assertEqual(la.guid, libid) + self.assertEqual(la.lcid, lcid) + self.assertTrue(la.wLibFlags & LIBFLAG_FRESTRICTED) + self.assertEqual(la.wMajorVerNum, major_version) + self.assertEqual(la.wMinorVerNum, minor_version) + def test_CreateTypeInfo(self): ctlib = CreateTypeLib(str(self.typelib_path)) # Create a type info From 87bcc87211ef9bc5f211fd208f8ddbe174a5a64f Mon Sep 17 00:00:00 2001 From: junkmd Date: Sun, 25 Jan 2026 13:18:34 +0900 Subject: [PATCH 4/4] test: Add `Test_ICreateTypeInfo`. This commit introduces `Test_ICreateTypeInfo` to `test_typeinfo.py`, specifically testing the `ICreateTypeInfo.SetDocString` and `ICreateTypeInfo.SetHelpContext` methods. It ensures that documentation set on a type information object is correctly persisted and can be retrieved after the type library is saved and reloaded. --- comtypes/test/test_typeinfo.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/comtypes/test/test_typeinfo.py b/comtypes/test/test_typeinfo.py index 086c3e06..b0ea625b 100644 --- a/comtypes/test/test_typeinfo.py +++ b/comtypes/test/test_typeinfo.py @@ -268,6 +268,31 @@ def test_CreateTypeInfo(self): ) +class Test_ICreateTypeInfo(unittest.TestCase): + def setUp(self): + td = tempfile.TemporaryDirectory() + self.addCleanup(td.cleanup) + self.tmpdir = Path(td.name) + self.typelib_path = self.tmpdir / "test.tlb" + self.ctlib = CreateTypeLib(str(self.typelib_path)) + + def test_Documentaion(self): + name = "IMyInterface" + docstring = "My test interface" + helpctx = 123 + ctinfo = self.ctlib.CreateTypeInfo(name, typeinfo.TKIND_INTERFACE) + ctinfo.SetDocString(docstring) + ctinfo.SetHelpContext(helpctx) + # `Layout` must be called before `SaveAllChanges`. + ctinfo.LayOut() + self.ctlib.SaveAllChanges() + # Load the typelib and verify the type info + tlib = LoadTypeLibEx(str(self.typelib_path)) + _, tinfo = tlib.FindName(name) # type: ignore + doc = tinfo.GetDocumentation(-1) + self.assertEqual(doc, (name, docstring, helpctx, None)) + + class Test_GetModuleFileName(unittest.TestCase): @unittest.skipUnless( sys.prefix == sys.base_prefix,