Skip to content

Commit

Permalink
Refactor tools subpackage. (#749)
Browse files Browse the repository at this point in the history
* Add `_write_structbody_fields`.

* Remove `"comtypes/tools/codegenerator/helpers.py"` from `[tool.ruff.lint.per-file-ignores]`.

* Update `_write_structbody_fields`.

* In `tools/codegenerator/heads.py`, `type(t) == T` -> `type(t) is T`.
  • Loading branch information
junkmd authored Jan 19, 2025
1 parent a2fc2c5 commit 4abba29
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 40 deletions.
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

0 comments on commit 4abba29

Please sign in to comment.