Search code examples
pythonpython-3.xwrappermagic-methods

Magic method __str__ is not called on str call


I'm the creator of module-wrapper library and one of the authors of aioify library. The problem arises in magic method wrapping inside module-wrapper library (I call module_wrapper.wrap from aioify.aioify, but that doesn't matter).

I have the following code:

#!/usr/bin/env python
from aioify import aioify


async def main():
    from pathlib import Path
    # noinspection PyPep8Naming
    AioPath = aioify(obj=Path)
    return await AioPath.create('/tmp')


if __name__ == '__main__':
    import asyncio
    loop = asyncio.get_event_loop()
    path = loop.run_until_complete(main())
    path_str = str(path)
    print(path_str)

I expect to get following output:

/tmp

But I get this:

<module_wrapper.wrap.<locals>.ObjectProxy object at 0x7f64266a9b00>

I cannot understand why. When I call:

path_str = path.__str__()
print(path_str)

I get what I expect:

/tmp

When I set the breakpoint inside magic method wrapper function debugger doesn't stop.

UPD0: To reproduce this you need to install module-wrapper and aioify (from the tarball because it's not released on PyPI). Don't forget to create virtualenv before!):

pip install module-wrapper==0.1.26
pip install https://github.com/yifeikong/aioify/archive/0.3.0.zip

Solution

  • Finally solved it. mseifert was right, the problem was that I didn't wrap magic methods of the class. See fixed version on GitHub and on PyPI.