Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor tools subpackage (part 3 of #749). #753

Merged
merged 4 commits into from
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
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