def test_cursing_a_reversed_curse(): curse(str, 'one', 1) assert str.one == 1 reverse(str, 'one') curse(str, 'one', 2) assert str.one == 2
def test_dunder_str(): assert str(1) == "1" def always_one(self): return 'one' curse(int, '__str__', always_one) assert str(1) == "one" reverse(int, '__str__')
def test(self): # quit() x = 'hi' # y=type(x) # y.z=7 #TypeError: can't set attributes of built-in/extension type from forbiddenfruit import curse, reverse # Since Forbidden Fruit is fundamentally dependent on the C API, # this library won't work on other python implementations, # such as Jython, PyPy, etc. I might add support for PyPy in the future, def words_of_wisdom(self): return self * "blah " # print(words_of_wisdom) # print(type(words_of_wisdom)) # print(classmethod(words_of_wisdom)) curse(str, "z", 7) print((x.z)) # YAY !!! retroactively gifts attribute to all instances reverse(str, "z") print((x.z)) # NOO:( don't work!! a = "dfs" print((a.z)) # NOO:( curse(int, "words_of_wisdom", words_of_wisdom) assert (2).words_of_wisdom() == "blah blah " reverse(int, "words_of_wisdom") # assert (3).words_of_wisdom() != "blah blah blah " #NOO assert (2).words_of_wisdom() != "blah blah " curse(str, "hello", classmethod(lambda x: 'blah')) assert str.hello() == "blah"
def test_dunder_new(): assert str(1) == "1" def the_answer(cls, args, kwargs): return 'fourty-two' curse(str, '__new__', the_answer) assert str(1) == "fourty-two" reverse(str, '__new__') assert str(1) == "1"
def uninstall(types=None): if types is None: # uninstall everything types = list(cursed_types) for _type in types: forbiddenfruit.reverse(_type, 'pluck') cursed_types.remove(_type)
def test_reversing_a_builtin(): # Given that I have a cursed object curse(str, 'stuff', property(lambda s: s * 2)) # When I bless it reverse(str, 'stuff') # Then I see that str won't contain assert 'stuff' not in dir(str)
def test_dunder_reverse(): def type_error_str(self): return 'type error' curse(TypeError, '__str__', type_error_str) te = TypeError("testing") assert str(te) == "type error" reverse(TypeError, '__str__') assert str(te) == "testing"
def test_reversing_a_builtin(): # Given that I have a cursed object curse(str, "stuff", property(lambda s: s * 2)) # When I bless it reverse(str, "stuff") # Then I see that str won't contain assert "stuff" not in dir(str)
def reverse(classes=[list, tuple, set, dict]): """Add functional methods to list, tuple, set, and dict in-place.""" cursed_methods = { "map": _map, "filter": _filter, "reduce": _reduce, "apply": _apply, "zip": _zip, "enumerate": _enumerate, } for klass in classes: for name, fun in cursed_methods.items(): forbiddenfruit.reverse(klass, name)
def test_dunder_list_revert(): """Test reversion of a curse with dunders""" def map_list(func, list_): if not callable(func): raise NotImplementedError() return map(func, list_) curse(list, "__add__", map_list) list_ = list(range(10)) times_2 = lambda x: x * 2 assert list(times_2 + list_) == list(range(0, 20, 2)) reverse(list, "__add__") try: times_2 + list_ except TypeError: pass else: # should always raise an exception assert False