-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathtest_compile_deco.py
More file actions
189 lines (140 loc) · 4.2 KB
/
test_compile_deco.py
File metadata and controls
189 lines (140 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# Authors: Sylvain Marie <sylvain.marie@se.com>
#
# Copyright (c) Schneider Electric Industries, 2020. All right reserved.
from textwrap import dedent
import pytest
from makefun import compile_fun, UnsupportedForCompilation, UndefinedSymbolError
def test_compilefun():
"""tests that @compile_fun works correctly"""
@compile_fun
def foo(a, b):
return a + b
res = foo(5, -5.0)
assert res == 0
ref = """
@compile_fun
def foo(a, b):
return a + b
"""
assert foo.__source__ == dedent(ref[1:])
def get_code(target):
try:
# python 3
func_code = target.__code__
except AttributeError:
# python 2
func_code = target.func_code
return func_code
def is_compiled(target):
fname = get_code(target).co_filename
return fname != __file__ and 'makefun-gen' in fname
def test_compilefun_nested():
"""tests that @compile_fun correctly compiles nested functions recursively"""
def foo(a, b):
return a + b
@compile_fun
def bar(a, b):
assert is_compiled(foo)
return foo(a, b)
res = bar(5, -5.0)
assert res == 0
def test_compilefun_nested_already_compiled():
"""tests that @compile_fun correctly handles when a required function was already compiled"""
@compile_fun
def foo(a, b):
return a + b
@compile_fun
def bar(a, b):
assert is_compiled(foo)
return foo(a, b)
res = bar(5, -5.0)
assert res == 0
@pytest.mark.parametrize("variant", ['all', 'named'], ids="variant={}".format)
def test_compilefun_nested_exclude(variant):
"""tests that the `except_names` argument of @compile_fun works correctly"""
def foo(a, b):
return a + b
if variant == 'all':
@compile_fun(recurse=False)
def bar(a, b):
assert not is_compiled(foo)
return foo(a, b)
else:
@compile_fun(except_names=('foo', ))
def bar(a, b):
assert not is_compiled(foo)
return foo(a, b)
res = bar(5, -5.0)
assert res == 0
def test_compilefun_co_names():
"""Test that today we do not compile imported names."""
# TODO one day it would be great to selectively recurse through such imported names. Unfortunately,
# this comes with *many* side effects including compilation order, appropriate propagation or
# non-propagation of globals(), locals()
# See https://github.com/smarie/python-makefun/issues/52
# TODO this test is not sufficient, there are things happening in more complex settings that are not happening here.
# see https://github.com/smarie/python-decopatch/pull/18
@compile_fun
def foo():
assert is_compiled(dedent)
return dedent(" hoho")
res = foo()
assert res == "hoho"
def test_compilefun_nameerror():
"""Tests that the `NameError` is raised at creation time and not at call time"""
with pytest.raises(UndefinedSymbolError):
@compile_fun
def fun_requiring_unknown_name(a, b):
return unknown_name(a, b)
def unknown_name(a, b):
return a + b
def test_compilefun_method():
"""Tests that @compilefun works for class methods"""
class A:
@compile_fun
def meth1(self, par1):
print("in A.meth1: par1 =", par1)
a = A()
a.meth1("via meth1")
class A:
def __init__(self):
self.i = 1
@compile_fun
def add(self, a):
return self.i + a
a = A()
assert A().add(-1) == 0
def test_compileclass_decorator():
"""tests that applying decorator on a class raises an error """
with pytest.raises(UnsupportedForCompilation):
@compile_fun
class A(object):
pass
# def test_compileclass_decorator():
#
# @compile_fun
# class A(object):
# pass
#
# assert A() is not None
#
# @compile_fun
# class A(int, object):
# pass
#
# assert A() is not None
#
# @compile_fun
# class A(object):
# def __init__(self):
# pass
#
# assert A() is not None
#
# @compile_fun
# class A(int):
# pass
# # def compute(self):
# # return self + 2
#
# assert A(2) + 2 == 4