Skip to content

Commit

Permalink
Refactor tools subpackage (part 1 of #749). (#750)
Browse files Browse the repository at this point in the history
* Add `_write_structbody_commethods`.

* Update `TypeNamer`.
  • Loading branch information
junkmd authored Jan 19, 2025
1 parent 6b6af74 commit 8bf5283
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
46 changes: 27 additions & 19 deletions comtypes/tools/codegenerator/codegenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,25 +466,33 @@ def StructureBody(self, body: typedesc.StructureBody) -> None:
self.imports.add("comtypes", "COMMETHOD")

with self.adjust_blank("attribute") as ofi:
print(f"{body.struct.name}._methods_ = [", file=ofi)
if body.struct.location:
print(f"# {body.struct.location}", file=ofi)
for m in methods:
if m.location:
print(f" # {m.location}", file=ofi)
print(
(
f" COMMETHOD(\n"
f" [], \n"
f" {self._to_type_name(m.returns)},\n"
f" '{m.name}',\n"
),
file=ofi,
)
for a in m.iterArgTypes():
print(f" ([], {self._to_type_name(a)}),\n", file=ofi)
print(" ),", file=ofi)
print("]", file=ofi)
self._write_structbody_commethods(body, methods, ofi)

def _write_structbody_commethods(
self,
body: typedesc.StructureBody,
methods: List[typedesc.Method],
ofi: io.StringIO,
) -> None:
print(f"{body.struct.name}._methods_ = [", file=ofi)
if body.struct.location:
print(f"# {body.struct.location}", file=ofi)
for m in methods:
if m.location:
print(f" # {m.location}", file=ofi)
print(
(
" COMMETHOD(\n"
" [],\n"
f" {self._to_type_name(m.returns)},\n"
f" '{m.name}',\n"
),
file=ofi,
)
for a in m.iterArgTypes():
print(f" ([], {self._to_type_name(a)}),\n", file=ofi)
print(" ),", file=ofi)
print("]", file=ofi)

################################################################
# top-level typedesc generators
Expand Down
4 changes: 2 additions & 2 deletions comtypes/tools/codegenerator/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ def __call__(self, t: Any) -> str:
return f"CFUNCTYPE({', '.join(args)})"
elif isinstance(t, typedesc.CvQualifiedType):
# const and volatile are ignored
return "%s" % self(t.typ)
return f"{self(t.typ)}"
elif isinstance(t, typedesc.FundamentalType):
return ctypes_names[t.name]
elif isinstance(t, typedesc.Structure):
Expand All @@ -328,7 +328,7 @@ def __call__(self, t: Any) -> str:
return "c_int" # enums are integers
elif isinstance(t, typedesc.EnumValue):
if keyword.iskeyword(t.name):
return t.name + "_"
return f"{t.name}_"
return t.name
elif isinstance(t, typedesc.External):
# t.symbol_name - symbol to generate
Expand Down

0 comments on commit 8bf5283

Please sign in to comment.