clarete / forbiddenfruit

Patch built-in python objects

Home Page:https://clarete.li/forbiddenfruit/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is (and/or Why) __str__, __repr__ are not patchable?

guneysus opened this issue · comments

Is (and/or Why) __str__, __repr__ are not patchable?

Hello, sorry for the late response. forbiddenfruit is a really [lame] simple hack that exposes the __dict__ attribute of a python class. Magic methods and special protocols are resolved separately. forbiddenfruit would have to special case these methods and overwrite the right properties in the PyObject struct to allow patching these special guys.

It's possible but might take some digging to find out what to overwrite.

Hi, I assume it applies to all "dunders"? I.e. I tried to patch __matmul__ on FunctionType to implement function chaining. And although f.__matmul__(g) does the right think, f @ g doesn't seem to pick up the existence of the overwritten implementation.

Of course, f.chain(g) would do, too, but the ability to override the operator behaviour would add a wholly another level of the black magic and let me maniacally laugh at the code reviewer's tears, so it would be incredibly useful, as you might imagine.

commented

This is resolved for methods defined in ff's PyTypeObject._fields_ or one of its method suites (e.g. PyNumberMethods) and added to override_dict. __repr__, __hash__, __call__, __iter__, and __next__ are still missing.

Just a quick +1 for getting __repr__ working; it would enable symbolic reasoning about repr strings in CrossHair.

__hash__ also doesnt work

Right now, im trying to patch hash on the forbiddenfruit module

I happen to be working in the same area. Looking to add __next__ which is proving troublesome because it can raise an exception. But I've added __repr__ and __hash__ on phy1729/complete-PyTypeObject

+1 for __call__

please also add __getattr__ for patching