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. #749

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
58 changes: 26 additions & 32 deletions comtypes/tools/codegenerator/codegenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,50 +406,44 @@ def StructureBody(self, body: typedesc.StructureBody) -> None:
if body.struct.bases:
assert len(body.struct.bases) == 1
self.generate(body.struct.bases[0].get_body())

with self.adjust_blank("attribute") as ofi:
print(f"{body.struct.name}._fields_ = [", file=ofi)
if body.struct.location:
print(f" # {body.struct.location}", file=ofi)
# unnamed fields will get autogenerated names "_", "_1". "_2", "_3", ...
unnamed_index = 0
for f in fields:
if not f.name:
if unnamed_index:
fieldname = f"_{unnamed_index:d}"
else:
fieldname = "_"
unnamed_index += 1
print(
f" # Unnamed field renamed to '{fieldname}'",
file=ofi,
)
else:
fieldname = f.name
if f.bits is None:
print(
f" ('{fieldname}', {self._to_type_name(f.typ)}),",
file=ofi,
)
else:
print(
f" ('{fieldname}', {self._to_type_name(f.typ)}, {f.bits}),",
file=ofi,
)
print("]", file=ofi)

self._write_structbody_fields(body, fields, ofi)
if body.struct.size is None:
with self.adjust_blank("comment") as 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 methods:
self.imports.add("comtypes", "COMMETHOD")
with self.adjust_blank("attribute") as ofi:
self._write_structbody_commethods(body, methods, ofi)

def _write_structbody_fields(
self,
body: typedesc.StructureBody,
fields: List[typedesc.Field],
ofi: io.StringIO,
) -> None:
print(f"{body.struct.name}._fields_ = [", file=ofi)
if body.struct.location:
print(f" # {body.struct.location}", file=ofi)
# unnamed fields will get autogenerated names "_", "_1". "_2", "_3", ...
unnamed_index = 0
for f in fields:
if not f.name:
fieldname = "_" if not unnamed_index else f"_{unnamed_index:d}"
unnamed_index += 1
print(f" # Unnamed field renamed to '{fieldname}'", file=ofi)
else:
fieldname = f.name
typename = self._to_type_name(f.typ)
if f.bits is None:
print(f" ('{fieldname}', {typename}),", file=ofi)
else:
print(f" ('{fieldname}', {typename}, {f.bits}),", file=ofi)
print("]", file=ofi)

def _write_structbody_size_comments(
self, body: typedesc.StructureBody, ofi: io.StringIO
) -> None:
Expand Down
4 changes: 2 additions & 2 deletions comtypes/tools/codegenerator/heads.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def write(self, head: typedesc.StructureHead, basenames: Sequence[str]) -> None:
print("assert 0, 'cannot generate code for IUnknown'", file=self.stream)
print(f"class {head.struct.name}(_com_interface):", file=self.stream)
print(" pass", file=self.stream)
elif type(head.struct) == typedesc.Structure:
elif type(head.struct) is typedesc.Structure:
print(f"class {head.struct.name}(Structure):", file=self.stream)
if hasattr(head.struct, "_recordinfo_"):
print(
Expand All @@ -62,7 +62,7 @@ def write(self, head: typedesc.StructureHead, basenames: Sequence[str]) -> None:
)
else:
print(" pass", file=self.stream)
elif type(head.struct) == typedesc.Union:
elif type(head.struct) is typedesc.Union:
print(f"class {head.struct.name}(Union):", file=self.stream)
print(" pass", file=self.stream)

Expand Down
5 changes: 1 addition & 4 deletions comtypes/tools/codegenerator/helpers.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import keyword
from typing import Any
from typing import List, Tuple
from typing import Iterator
from typing import Any, Iterator, List, Tuple
from typing import Union as _UnionT

import comtypes
from comtypes.tools import typedesc
from comtypes.tools.codegenerator.modulenamer import name_wrapper_module

Expand Down
2 changes: 0 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ ignore = ["E402"]
"comtypes/server/inprocserver.py" = ["E713", "E722", "F403", "F405", "F841"]
"comtypes/server/localserver.py" = ["F401", "F403", "F405"]
"comtypes/server/register.py" = ["F403", "F405", "E713", "E731"]
"comtypes/tools/codegenerator/heads.py" = ["E721"]
"comtypes/tools/codegenerator/helpers.py" = ["F401"]
"comtypes/tools/codegenerator/packing.py" = ["F821", "F841"]
"comtypes/tools/tlbparser.py" = ["F401"]
"comtypes/tools/typedesc.py" = ["F403", "F405"]
Expand Down