| 1 | from rpython.tool.sourcetools import ( |
| 2 | func_renamer, func_with_new_name, rpython_wrapper) |
| 3 | |
| 4 | def test_rename(): |
| 5 | def f(x, y=5): |
| 6 | return x + y |
| 7 | f.prop = int |
| 8 | |
| 9 | g = func_with_new_name(f, "g") |
| 10 | assert g(4, 5) == 9 |
| 11 | assert g.__name__ == "g" |
| 12 | assert f.__defaults__ == (5,) |
| 13 | assert g.prop is int |
| 14 | |
| 15 | def test_rename_decorator(): |
| 16 | @func_renamer("g") |
| 17 | def f(x, y=5): |
| 18 | return x + y |
| 19 | f.prop = int |
| 20 | |
| 21 | assert f(4, 5) == 9 |
| 22 | |
| 23 | assert f.__name__ == "g" |
| 24 | assert f.__defaults__ == (5,) |
| 25 | assert f.prop is int |
| 26 | |
| 27 | def test_func_rename_decorator(): |
| 28 | def bar(): |
| 29 | 'doc' |
| 30 | |
| 31 | bar2 = func_with_new_name(bar, 'bar2') |
| 32 | assert bar.__doc__ == bar2.__doc__ == 'doc' |
| 33 | |
| 34 | bar.__doc__ = 'new doc' |
| 35 | bar3 = func_with_new_name(bar, 'bar3') |
| 36 | assert bar3.__doc__ == 'new doc' |
| 37 | assert bar2.__doc__ != bar3.__doc__ |
| 38 | |
| 39 | |
| 40 | def test_rpython_wrapper(): |
| 41 | calls = [] |
| 42 | |
| 43 | def bar(a, b): |
| 44 | calls.append(('bar', a, b)) |
| 45 | return a+b |
| 46 | |
| 47 | template = """ |
| 48 | def {name}({arglist}): |
| 49 | calls.append(('decorated', {arglist})) |
| 50 | return {original}({arglist}) |
| 51 | """ |
| 52 | bar = rpython_wrapper(bar, template, calls=calls) |
| 53 | assert bar(40, 2) == 42 |
| 54 | assert calls == [ |
| 55 | ('decorated', 40, 2), |
| 56 | ('bar', 40, 2), |
| 57 | ] |