py-spy icon indicating copy to clipboard operation
py-spy copied to clipboard

get str representation of complex types

Open vb-quaestio opened this issue 3 years ago • 3 comments

Hi, when using dump subcommand, under the Arguments and Locals section, for complex types i can see the class and the memory address, eg:

        Locals:
            obj: None
            abc: <Series at 0x7f996f45eba8>
            xyz: <DataFrame at 0x7f996f456940>

It's doable to view the string representation of this complex objects? Thanks.

vb-quaestio avatar Dec 28 '21 13:12 vb-quaestio

See https://github.com/benfred/py-spy/issues/451

Jongy avatar Dec 28 '21 14:12 Jongy

So, according to this reply it's not possible for types like datetime or other that doesn't have __dict__ property, or i'm missing something?

vb-quaestio avatar Dec 28 '21 17:12 vb-quaestio

It can be done, but it'll require writing custom parsers for the binary, in-memory structure of those objects (as I have mentioned in that comment, "custom parsers must be written").

For example, datetime is represented by a PyDateTime_DateTime object. Its ->ob_type.tp_name is set to datetime.datetime IIRC. So, you could write a parser that reads an object's ->ob_type.tp_name, then per "__dict__-less type" you wish to support, write a parser and call the appropriate one based on the tp_name. If it's datetime.datetime you can read the PyDateTime_DateTime struct and parse it accordingly.

It's definitely much more work then simply displaying the __dict__, though :(

Jongy avatar Dec 28 '21 19:12 Jongy