Closed Jongy closed 3 years ago
This is nice!! And does look way simpler than the ctypes alternative to get a hold of the dict. It'd be great if you wanted to put a lil PR up with the improvement. Thank you 🙇🏾
This is nice!! And does look way simpler than the ctypes alternative to get a hold of the dict. It'd be great if you wanted to put a lil PR up with the improvement. Thank you 🙇🏾
Cool, I'll just run the tests on all relevant CPython versions, and submit it.
I recently needed to patch builtin methods. Didn't know of forbiddenfruit at first, so I searched & found some workaround, and later I found this cool project via this SO question.
To work around the
dictproxy
/mappingproxy
"issue" without diving intoctypes
and messing with raw objects, I used the nicegc.get_referents
method, which (together with its counterpartgc.get_referrers
) lets you "get your hands" on objects you might have not been able to reach otherwise.dictproxy
/mappingproxy
are 2 dummy objects holding only a single reference, to the underlying mapping. Soint_dict = gc.get_referents(int.__dict__)[0]
is enough. With this simplerpatchable_builtin
, tests pass:Works the same, but simpler, so nicer IMO. Can open a PR with this improvement if you'd like.