Show HN: Forbidden Fruit – Patch built-in Python objects (clarete.github.com) 35 points by clarete 13y ago ↗ HN
[–] herge 13y ago ↗ Man, having this as a context manager (maybe with the help of contextlib) would be great, because context managers are great. [–] clarete 13y ago ↗ That's the idea. Everything to make testing easier! :)
[–] hcarvalhoalves 13y ago ↗ What's the use case? [–] clarete 13y ago ↗ I have two main use cases in my mind right now.1) Making it easier to write tools for testing python code, like sure[0] and freezegun[1]2) Prototyping new features for python without touching the C code.[0] http://github.com/gabrielfalcao/sure -- where forbiddenfruit started) [1] http://github.com/spulec/freezegun [–] philipbjorge 13y ago ↗ It's a dirty hack - therefore, fun. [–] prezjordan 13y ago ↗ Probably for the same reason why reopening Ruby classes is used so much. "hello, world".capitalize looks nicer than capitalize("hello, world"), and it's more convenient for method chaining.
[–] clarete 13y ago ↗ I have two main use cases in my mind right now.1) Making it easier to write tools for testing python code, like sure[0] and freezegun[1]2) Prototyping new features for python without touching the C code.[0] http://github.com/gabrielfalcao/sure -- where forbiddenfruit started) [1] http://github.com/spulec/freezegun
[–] prezjordan 13y ago ↗ Probably for the same reason why reopening Ruby classes is used so much. "hello, world".capitalize looks nicer than capitalize("hello, world"), and it's more convenient for method chaining.
[–] pekk 13y ago ↗ If you are doing this for testing, it's already a feature of pytest: http://pytest.org/latest/monkeypatch.html [–] clarete 13y ago ↗ I might be wrong, but I don't think that it's actually possible to mock built-in objects (written in C, not in python) using pytest.A very quick test would be trying to do something like this:>>> monkeypatch(str, "blah", "bleh") >>> assert str.blah == "bleh"I tried it here with no success though.
[–] clarete 13y ago ↗ I might be wrong, but I don't think that it's actually possible to mock built-in objects (written in C, not in python) using pytest.A very quick test would be trying to do something like this:>>> monkeypatch(str, "blah", "bleh") >>> assert str.blah == "bleh"I tried it here with no success though.
[–] jacobolus 13y ago ↗ What are the license implications of using GPLv3 code as part of a test suite? [–] clarete 13y ago ↗ I don't think that there's any implications for that, but I'll probably relicense this library as MIT or at least LGPL pretty soon.I just used my default licence template, which defaults to GPL. [–] marcosmlopes 13y ago ↗ Wow! Great news. It would be nice have this project under MIT license.
[–] clarete 13y ago ↗ I don't think that there's any implications for that, but I'll probably relicense this library as MIT or at least LGPL pretty soon.I just used my default licence template, which defaults to GPL. [–] marcosmlopes 13y ago ↗ Wow! Great news. It would be nice have this project under MIT license.
[–] oellegaard 13y ago ↗ While it is a cool project, I hope I never have to work on a project that use this. I hate when people overwrite built-ins, but that might just be me.
[–] tekacs 13y ago ↗ Particularly striking is how little (pure Python) code was required to do this (courtesy ctypes)!A wonderful hack! :)Edit: see https://github.com/clarete/forbiddenfruit/blob/master/forbid...
15 comments
[ 0.21 ms ] story [ 47.7 ms ] thread1) Making it easier to write tools for testing python code, like sure[0] and freezegun[1]
2) Prototyping new features for python without touching the C code.
[0] http://github.com/gabrielfalcao/sure -- where forbiddenfruit started) [1] http://github.com/spulec/freezegun
A very quick test would be trying to do something like this:
>>> monkeypatch(str, "blah", "bleh") >>> assert str.blah == "bleh"
I tried it here with no success though.
I just used my default licence template, which defaults to GPL.
A wonderful hack! :)
Edit: see https://github.com/clarete/forbiddenfruit/blob/master/forbid...