mhammond / pywin32

Python for Windows (pywin32) Extensions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Invalid __str__ method from makepy

gswifort opened this issue · comments

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__().

print("\tdef __str__(self, *args):", file=stream)
print("\t\treturn str(self.__call__(*args))", file=stream)
print("\tdef __int__(self, *args):", file=stream)
print("\t\treturn int(self.__call__(*args))", file=stream)

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.