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

Can we remove the comments generated by CodeGenerator.ComInterfaceBody? #530

Open
junkmd opened this issue Apr 19, 2024 · 0 comments
Open
Labels
help wanted Extra attention is needed shared_info use cases, tips and troubleshoots

Comments

@junkmd
Copy link
Collaborator

junkmd commented Apr 19, 2024

I noticed during the bug fix for #524 that the wrapper module for mshtml.tlb have a considerably large file size.

Given the large number of definitions in the COM type library, it’s inevitable that the file size of the mshtml.tlb wrapper module becomes large.

However, in the current comtypes implementation, CodeGenerator.ComInterfaceBody generates comments proportional to the number of COM interfaces and methods.

print(
"################################################################",
file=self.stream,
)
print("# code template for %s implementation" % body.itf.name, file=self.stream)
print("# class %s_Impl(object):" % body.itf.name, file=self.stream)
methods = {}
for m in body.itf.members:
if isinstance(m, typedesc.ComMethod):
# m.arguments is a sequence of tuples:
# (argtype, argname, idlflags, docstring)
# Some typelibs have unnamed method parameters!
inargs = [a[1] or "<unnamed>" for a in m.arguments if not "out" in a[2]]
outargs = [a[1] or "<unnamed>" for a in m.arguments if "out" in a[2]]
if "propget" in m.idlflags:
methods.setdefault(m.name, [0, inargs, outargs, m.doc])[0] |= 1
elif "propput" in m.idlflags:
methods.setdefault(m.name, [0, inargs[:-1], inargs[-1:], m.doc])[
0
] |= 2
else:
methods[m.name] = [0, inargs, outargs, m.doc]
for name, (typ, inargs, outargs, doc) in methods.items():
if typ == 0: # method
print(
"# def %s(%s):" % (name, ", ".join(["self"] + inargs)),
file=self.stream,
)
print("# %r" % (doc or "-no docstring-"), file=self.stream)
print("# #return %s" % (", ".join(outargs)), file=self.stream)
elif typ == 1: # propget
print("# @property", file=self.stream)
print(
"# def %s(%s):" % (name, ", ".join(["self"] + inargs)),
file=self.stream,
)
print("# %r" % (doc or "-no docstring-"), file=self.stream)
print("# #return %s" % (", ".join(outargs)), file=self.stream)
elif typ == 2: # propput
print(
"# def _set(%s):" % ", ".join(["self"] + inargs + outargs),
file=self.stream,
)
print("# %r" % (doc or "-no docstring-"), file=self.stream)
print(
"# %s = property(fset = _set, doc = _set.__doc__)" % name,
file=self.stream,
)
elif typ == 3: # propget + propput
print(
"# def _get(%s):" % ", ".join(["self"] + inargs),
file=self.stream,
)
print("# %r" % (doc or "-no docstring-"), file=self.stream)
print("# #return %s" % (", ".join(outargs)), file=self.stream)
print(
"# def _set(%s):" % ", ".join(["self"] + inargs + outargs),
file=self.stream,
)
print("# %r" % (doc or "-no docstring-"), file=self.stream)
print(
"# %s = property(_get, _set, doc = _set.__doc__)" % name,
file=self.stream,
)
else:
raise RuntimeError("BUG")
print("#", file=self.stream)

I attempted to reduce the file size by removing these comments.


Actual behaviors

Using Python 3.10.10, I compared the output values obtained by running the following script with the codebase at commit c8f3e2e as is, and with the codebase where the part that generates comments in CodeGenerator.ComInterfaceBody has been removed.

import time
import os

from comtypes import client


def main():
    start = time.perf_counter()
    mod = client.GetModule("mshtml.tlb")
    end = time.perf_counter()
    print(f"{end-start:.2f}", os.path.getsize(mod.__wrapper_module__.__file__))


if __name__ == "__main__":
    main()

Execution results

with generating comments by CodeGenerator.ComInterfaceBody

30.91 13365249

without generating comments by CodeGenerator.ComInterfaceBody

30.78 12693666

There is a difference of about 0.64MB in file size.
There is also a difference of more than 0.1 seconds in the time it takes to generate the module.

With the type annotation feature introduced from comtypes==1.3.1, if we simply want to know the interface, we can now refer to static type hints.
So, these comments might have served their purpose.

However, these comments have been around for a long time, so caution is needed when removing them.

I am soliciting opinions from the community on whether it is better to reduce the file size and the time it takes to generate by removing these comments, or whether we should keep them for backward compatibility.

Any opinion would be appreciated.

@junkmd junkmd added shared_info use cases, tips and troubleshoots help wanted Extra attention is needed labels Apr 19, 2024
@junkmd junkmd pinned this issue Apr 20, 2024
@junkmd junkmd unpinned this issue Jun 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed shared_info use cases, tips and troubleshoots
Projects
None yet
Development

No branches or pull requests

1 participant