Skip to content

Commit

Permalink
Refactor tools subpackage (part 3 of #749). (#753)
Browse files Browse the repository at this point in the history
* Update `_write_structbody_size_assertion`.

* Update `tools/tlbparser.py`.

* Add `_write_structbody_size_comments`.

* Improve import part of `tools/tlbparser.py`.
  • Loading branch information
junkmd authored Jan 19, 2025
1 parent 03caef6 commit 2861c9e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 26 deletions.
37 changes: 17 additions & 20 deletions comtypes/tools/codegenerator/codegenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,36 +440,33 @@ def StructureBody(self, body: typedesc.StructureBody) -> None:

if body.struct.size is None:
with self.adjust_blank("comment") as ofi:
msg1 = "# The size provided by the typelib is incorrect."
msg2 = (
"# The size and alignment check "
f"for {body.struct.name} is skipped."
)
print(msg1, file=ofi)
print(msg2, file=ofi)
self._write_structbody_size_comments(body, ofi)
elif body.struct.name not in packing.dont_assert_size:
with self.adjust_blank("assert") as ofi:
self._write_structbody_size_assertion(body, ofi)

if not methods:
return
self.imports.add("comtypes", "COMMETHOD")
with self.adjust_blank("attribute") as ofi:
self._write_structbody_commethods(body, methods, ofi)
if methods:
self.imports.add("comtypes", "COMMETHOD")
with self.adjust_blank("attribute") as ofi:
self._write_structbody_commethods(body, methods, ofi)

def _write_structbody_size_comments(
self, body: typedesc.StructureBody, ofi: io.StringIO
) -> None:
msg1 = "# The size provided by the typelib is incorrect."
msg2 = f"# The size and alignment check for {body.struct.name} is skipped."
print(msg1, file=ofi)
print(msg2, file=ofi)

def _write_structbody_size_assertion(
self, body: typedesc.StructureBody, ofi: io.StringIO
) -> None:
name = body.struct.name
assert body.struct.size is not None
size = body.struct.size // 8
print(
f"assert sizeof({body.struct.name}) == {size}, sizeof({body.struct.name})",
file=ofi,
)
print(f"assert sizeof({name}) == {size}, sizeof({name})", file=ofi)
align = body.struct.align // 8
print(
f"assert alignment({body.struct.name}) == {align}, alignment({body.struct.name})",
file=ofi,
)
print(f"assert alignment({name}) == {align}, alignment({name})", file=ofi)

def _write_structbody_commethods(
self,
Expand Down
10 changes: 4 additions & 6 deletions comtypes/tools/tlbparser.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import os
import sys
from typing import Any
from typing import Dict, List, Optional, Tuple
from ctypes import alignment, byref, c_void_p, sizeof, windll
from typing import Any, Dict, List, Optional, Tuple

from comtypes import automation, BSTR, COMError, typeinfo
from comtypes.tools import typedesc
from comtypes import BSTR, COMError, automation, typeinfo
from comtypes.client._code_cache import _get_module_filename

from comtypes.tools import typedesc

# Is the process 64-bit?
is_64bits = sys.maxsize > 2**32
Expand Down Expand Up @@ -237,7 +235,7 @@ def ParseModule(self, tinfo: typeinfo.ITypeInfo, ta: typeinfo.TYPEATTR) -> None:
elif fd.callconv == typeinfo.CC_STDCALL:
attributes = "__stdcall__"
else:
raise ValueError("calling convention %d" % fd.callconv)
raise ValueError(f"calling convention {fd.callconv:d}")

func = typedesc.Function(func_name, returns, attributes, extern=1)
if func_doc is not None:
Expand Down

0 comments on commit 2861c9e

Please sign in to comment.