Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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_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_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_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
def new_bytes_context():
curse(bytes, "__oldrepr__", bytes.__repr__)
curse(bytes, "_c___repr", myrepr)
curse(bytes, "fromhex", classmethod(fromhex))
yield
reverse(bytes, "fromhex")
reverse(bytes, "_c___repr")
reverse(bytes, "__oldrepr__")
def new_bytes_context():
curse(bytes, "__oldrepr__", bytes.__repr__)
curse(bytes, "_c___repr", myrepr)
curse(bytes, "fromhex", classmethod(fromhex))
yield
reverse(bytes, "fromhex")
reverse(bytes, "_c___repr")
reverse(bytes, "__oldrepr__")
def new_bytes_context():
curse(bytes, "__oldrepr__", bytes.__repr__)
curse(bytes, "_c___repr", myrepr)
curse(bytes, "fromhex", classmethod(fromhex))
yield
reverse(bytes, "fromhex")
reverse(bytes, "_c___repr")
reverse(bytes, "__oldrepr__")