clarete / forbiddenfruit

Patch built-in python objects
https://clarete.li/forbiddenfruit/
GNU General Public License v3.0
826 stars 52 forks source link

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

Open guneysus opened 9 years ago

clarete commented 8 years ago

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.

alendit commented 5 years ago

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.

alexchandel commented 5 years ago

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.

pschanely commented 4 years ago

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

RadiantUwU commented 3 years ago

__hash__ also doesnt work

RadiantUwU commented 3 years ago

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

phy1729 commented 3 years ago

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

MrFizzyBubbs commented 2 years ago

+1 for __call__

soamicharan commented 2 years ago

please also add __getattr__ for patching