From 4abba297255de258159321c2e8efa31a7827972e Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Sun, 19 Jan 2025 15:04:04 +0900 Subject: [PATCH] Refactor `tools` subpackage. (#749) * 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`. --- comtypes/tools/codegenerator/codegenerator.py | 58 +++++++++---------- comtypes/tools/codegenerator/heads.py | 4 +- comtypes/tools/codegenerator/helpers.py | 5 +- pyproject.toml | 2 - 4 files changed, 29 insertions(+), 40 deletions(-) diff --git a/comtypes/tools/codegenerator/codegenerator.py b/comtypes/tools/codegenerator/codegenerator.py index 7721ac3a..7c175741 100644 --- a/comtypes/tools/codegenerator/codegenerator.py +++ b/comtypes/tools/codegenerator/codegenerator.py @@ -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: diff --git a/comtypes/tools/codegenerator/heads.py b/comtypes/tools/codegenerator/heads.py index b9264c9e..3f1ed8b2 100644 --- a/comtypes/tools/codegenerator/heads.py +++ b/comtypes/tools/codegenerator/heads.py @@ -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( @@ -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) diff --git a/comtypes/tools/codegenerator/helpers.py b/comtypes/tools/codegenerator/helpers.py index cfae1916..1509cf61 100644 --- a/comtypes/tools/codegenerator/helpers.py +++ b/comtypes/tools/codegenerator/helpers.py @@ -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 diff --git a/pyproject.toml b/pyproject.toml index 4284c752..9cde1da6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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"]