mhammond / pywin32

Python for Windows (pywin32) Extensions
4.9k stars 783 forks source link

Invalid __str__ method from makepy #2162

Open gswifort opened 6 months ago

gswifort commented 6 months ago

makepy generates a __str__ method, which internally calls the __call__ method. The __call__ method can take arguments, while the __str__ method does not take arguments by default - str(obj) ↔ obj.__str__().

https://github.com/mhammond/pywin32/blob/f7d0a79a7ba3c83b8e6f6c62c617f4eb9770a1e9/com/win32com/client/genpy.py#L661-L664

This works incorrectly, e.g. for the COM interface of an Autocad application where __call__ requires an Index argument.

# Default method for this class is 'Item'
def __call__(self, Index=defaultNamedNotOptArg):
    'Gets the member object at a given index in a collection, group, or selection set'
    ret = self._oleobj_.InvokeTypes(0, LCID, 1, (9, 0), ((12, 1),),Index
        )
    if ret is not None:
        ret = Dispatch(ret, '__call__', '{AB9F53A4-BA00-499B-BE4C-D178EC67FFCC}')
    return ret

def __str__(self, *args):
    return str(self.__call__(*args))
def __int__(self, *args):
    return int(self.__call__(*args))

the same goes for __int__.

The __str__ and __int__ methods seem unnecessary for COM interfaces.