ionelmc / python-hunter

Hunter is a flexible code tracing toolkit.
https://python-hunter.readthedocs.io/
BSD 2-Clause "Simplified" License
796 stars 46 forks source link

<Foo object at 0x7526350> --> repr()? #52

Open guettli opened 5 years ago

guettli commented 5 years ago

I my trace output I see lines like this:

[...]/src/foo/foo/Foo.py:1048  call         => touch(self=<Foo object at 0x7526350>)

Unfortunately the memory address does not tell me much.

My object has __repr__() implemented. It would be great, if I could see this instead of the memory address.

Is this doable?

ionelmc commented 5 years ago

Oooof. Yeah it was possible in hunter 2.0 but it was a huge problem with code that creates side-effects in __repr__. I know this sounds unreal but kombu creates a connection on demand in some objects, and the "demand" can be even __repr__.

You can get the old problematic behavior by using repr_unsafe=True.

ctrngk commented 4 years ago

I happen to want the deeper value of variables. I solved it by repr_func. Is it what you want?

self.parser = {
    "client": HttpParser(),
    "server": None,
}

output without repr_func is as follows:

self.parser => { "client": <__main__.HttpParser object at 0x10362c090>, "server": None}

output with repr_func:

self.parser => {'body': None, 'code': None, 'headers': {'host': ('Host', 'httpbin.org:443'), 'proxy-connection': ('Proxy-Connection', 'Keep-Alive'), 'user-agent': ('User-Agent', 'curl/7.54.0')}, 'buffer': '', 'reason': None, 'chunker': None, 'raw': 'CONNECT httpbin.org:443 HTTP/1.1\r\nHost: httpbin.org:443\r\nUser-Agent: curl/7.54.0\r\nProxy-Connection: Keep-Alive\r\n\r\n', 'state': 6, 'version': 'HTTP/1.1', 'url': SplitResult(scheme='', netloc='', path='httpbin.org:443', query='', fragment=''), 'type': 1, 'method': 'CONNECT'}]

To achieve example given above, cli is as follows:

PYTHONHUNTER='Q(filename="proxy.py"), actions=[VarsPrinter("self.parser", repr_func=lambda x:repr(vars(x["client"]))  ),] ' python2 proxy.py

To begin with, try simple way repr_func=lambda x: repr(x)

Hope it helps.

Regards,