|
7 | 7 | from pybind11_tests import gil_scoped as m |
8 | 8 |
|
9 | 9 |
|
10 | | -def _run_in_process(target, *args, **kwargs): |
11 | | - """Runs target in process and returns its exitcode after 10s (None if still alive).""" |
12 | | - process = multiprocessing.Process(target=target, args=args, kwargs=kwargs) |
13 | | - process.daemon = True |
14 | | - try: |
15 | | - process.start() |
16 | | - # Do not need to wait much, 10s should be more than enough. |
17 | | - process.join(timeout=10) |
18 | | - return process.exitcode |
19 | | - finally: |
20 | | - if process.is_alive(): |
21 | | - process.terminate() |
| 10 | +class ExtendedVirtClass(m.VirtClass): |
| 11 | + def virtual_func(self): |
| 12 | + pass |
22 | 13 |
|
| 14 | + def pure_virtual_func(self): |
| 15 | + pass |
23 | 16 |
|
24 | | -def _python_to_cpp_to_python(): |
25 | | - """Calls different C++ functions that come back to Python.""" |
26 | 17 |
|
27 | | - class ExtendedVirtClass(m.VirtClass): |
28 | | - def virtual_func(self): |
29 | | - pass |
| 18 | +def test_callback_py_obj(): |
| 19 | + m.test_callback_py_obj(lambda: None) |
30 | 20 |
|
31 | | - def pure_virtual_func(self): |
32 | | - pass |
33 | 21 |
|
34 | | - extended = ExtendedVirtClass() |
35 | | - m.test_callback_py_obj(lambda: None) |
| 22 | +def test_callback_std_func(): |
36 | 23 | m.test_callback_std_func(lambda: None) |
| 24 | + |
| 25 | + |
| 26 | +def test_callback_virtual_func(): |
| 27 | + extended = ExtendedVirtClass() |
37 | 28 | m.test_callback_virtual_func(extended) |
| 29 | + |
| 30 | + |
| 31 | +def test_callback_pure_virtual_func(): |
| 32 | + extended = ExtendedVirtClass() |
38 | 33 | m.test_callback_pure_virtual_func(extended) |
39 | 34 |
|
40 | | - m.test_cross_module_gil_released() |
41 | | - m.test_cross_module_gil_acquired() |
| 35 | + |
| 36 | +def test_cross_module_gil_released(): |
| 37 | + """Makes sure that the GIL can be acquired by another module from a GIL-released state.""" |
| 38 | + m.test_cross_module_gil_released() # Should not raise a SIGSEGV |
| 39 | + |
| 40 | + |
| 41 | +def test_cross_module_gil_acquired(): |
| 42 | + """Makes sure that the GIL can be acquired by another module from a GIL-acquired state.""" |
| 43 | + m.test_cross_module_gil_acquired() # Should not raise a SIGSEGV |
| 44 | + |
| 45 | + |
| 46 | +def test_cross_module_gil_inner_custom_released(): |
| 47 | + """Makes sure that the GIL can be acquired/released by another module |
| 48 | + from a GIL-released state using custom locking logic.""" |
42 | 49 | m.test_cross_module_gil_inner_custom_released() |
| 50 | + |
| 51 | + |
| 52 | +def test_cross_module_gil_inner_custom_acquired(): |
| 53 | + """Makes sure that the GIL can be acquired/acquired by another module |
| 54 | + from a GIL-acquired state using custom locking logic.""" |
43 | 55 | m.test_cross_module_gil_inner_custom_acquired() |
| 56 | + |
| 57 | + |
| 58 | +def test_cross_module_gil_inner_pybind11_released(): |
| 59 | + """Makes sure that the GIL can be acquired/released by another module |
| 60 | + from a GIL-released state using pybind11 locking logic.""" |
44 | 61 | m.test_cross_module_gil_inner_pybind11_released() |
| 62 | + |
| 63 | + |
| 64 | +def test_cross_module_gil_inner_pybind11_acquired(): |
| 65 | + """Makes sure that the GIL can be acquired/acquired by another module |
| 66 | + from a GIL-acquired state using pybind11 locking logic.""" |
45 | 67 | m.test_cross_module_gil_inner_pybind11_acquired() |
| 68 | + |
| 69 | + |
| 70 | +def test_cross_module_gil_nested_custom_released(): |
| 71 | + """Makes sure that the GIL can be nested acquired/released by another module |
| 72 | + from a GIL-released state using custom locking logic.""" |
46 | 73 | m.test_cross_module_gil_nested_custom_released() |
| 74 | + |
| 75 | + |
| 76 | +def test_cross_module_gil_nested_custom_acquired(): |
| 77 | + """Makes sure that the GIL can be nested acquired/acquired by another module |
| 78 | + from a GIL-acquired state using custom locking logic.""" |
47 | 79 | m.test_cross_module_gil_nested_custom_acquired() |
| 80 | + |
| 81 | + |
| 82 | +def test_cross_module_gil_nested_pybind11_released(): |
| 83 | + """Makes sure that the GIL can be nested acquired/released by another module |
| 84 | + from a GIL-released state using pybind11 locking logic.""" |
48 | 85 | m.test_cross_module_gil_nested_pybind11_released() |
| 86 | + |
| 87 | + |
| 88 | +def test_cross_module_gil_nested_pybind11_acquired(): |
| 89 | + """Makes sure that the GIL can be nested acquired/acquired by another module |
| 90 | + from a GIL-acquired state using pybind11 locking logic.""" |
49 | 91 | m.test_cross_module_gil_nested_pybind11_acquired() |
50 | 92 |
|
| 93 | + |
| 94 | +def test_release_acquire(): |
51 | 95 | assert m.test_release_acquire(0xAB) == "171" |
| 96 | + |
| 97 | + |
| 98 | +def test_nested_acquire(): |
52 | 99 | assert m.test_nested_acquire(0xAB) == "171" |
53 | 100 |
|
| 101 | + |
| 102 | +def test_multi_acquire_release_cross_module(): |
54 | 103 | for bits in range(16 * 8): |
55 | 104 | internals_ids = m.test_multi_acquire_release_cross_module(bits) |
56 | 105 | assert len(internals_ids) == 2 if bits % 8 else 1 |
57 | 106 |
|
58 | 107 |
|
| 108 | +def _python_to_cpp_to_python(): |
| 109 | + test_callback_py_obj() |
| 110 | + test_callback_std_func() |
| 111 | + test_callback_virtual_func() |
| 112 | + test_callback_pure_virtual_func() |
| 113 | + test_cross_module_gil_released() |
| 114 | + test_cross_module_gil_acquired() |
| 115 | + test_cross_module_gil_inner_custom_released() |
| 116 | + test_cross_module_gil_inner_custom_acquired() |
| 117 | + test_cross_module_gil_inner_pybind11_released() |
| 118 | + test_cross_module_gil_inner_pybind11_acquired() |
| 119 | + test_cross_module_gil_nested_custom_released() |
| 120 | + test_cross_module_gil_nested_custom_acquired() |
| 121 | + test_cross_module_gil_nested_pybind11_released() |
| 122 | + test_cross_module_gil_nested_pybind11_acquired() |
| 123 | + test_release_acquire() |
| 124 | + test_nested_acquire() |
| 125 | + test_multi_acquire_release_cross_module() |
| 126 | + |
| 127 | + |
| 128 | +def _run_in_process(target, *args, **kwargs): |
| 129 | + """Runs target in process and returns its exitcode after 10s (None if still alive).""" |
| 130 | + process = multiprocessing.Process(target=target, args=args, kwargs=kwargs) |
| 131 | + process.daemon = True |
| 132 | + try: |
| 133 | + process.start() |
| 134 | + # Do not need to wait much, 10s should be more than enough. |
| 135 | + process.join(timeout=10) |
| 136 | + return process.exitcode |
| 137 | + finally: |
| 138 | + if process.is_alive(): |
| 139 | + process.terminate() |
| 140 | + |
| 141 | + |
59 | 142 | def _python_to_cpp_to_python_from_threads(num_threads, parallel=False): |
60 | 143 | """Calls different C++ functions that come back to Python, from Python threads.""" |
61 | 144 | threads = [] |
@@ -113,75 +196,3 @@ def test_python_to_cpp_to_python_from_process(): |
113 | 196 | if exitcode is None and env.PYPY and env.WIN: # Seems to be flaky. |
114 | 197 | pytest.skip("Ignoring unexpected exitcode None (PYPY WIN)") |
115 | 198 | assert exitcode == 0 |
116 | | - |
117 | | - |
118 | | -def test_cross_module_gil_released(): |
119 | | - """Makes sure that the GIL can be acquired by another module from a GIL-released state.""" |
120 | | - m.test_cross_module_gil_released() # Should not raise a SIGSEGV |
121 | | - |
122 | | - |
123 | | -def test_cross_module_gil_acquired(): |
124 | | - """Makes sure that the GIL can be acquired by another module from a GIL-acquired state.""" |
125 | | - m.test_cross_module_gil_acquired() # Should not raise a SIGSEGV |
126 | | - |
127 | | - |
128 | | -def test_cross_module_gil_inner_custom_released(): |
129 | | - """Makes sure that the GIL can be acquired/released by another module |
130 | | - from a GIL-released state using custom locking logic.""" |
131 | | - m.test_cross_module_gil_inner_custom_released() |
132 | | - |
133 | | - |
134 | | -def test_cross_module_gil_inner_custom_acquired(): |
135 | | - """Makes sure that the GIL can be acquired/acquired by another module |
136 | | - from a GIL-acquired state using custom locking logic.""" |
137 | | - m.test_cross_module_gil_inner_custom_acquired() |
138 | | - |
139 | | - |
140 | | -def test_cross_module_gil_inner_pybind11_released(): |
141 | | - """Makes sure that the GIL can be acquired/released by another module |
142 | | - from a GIL-released state using pybind11 locking logic.""" |
143 | | - m.test_cross_module_gil_inner_pybind11_released() |
144 | | - |
145 | | - |
146 | | -def test_cross_module_gil_inner_pybind11_acquired(): |
147 | | - """Makes sure that the GIL can be acquired/acquired by another module |
148 | | - from a GIL-acquired state using pybind11 locking logic.""" |
149 | | - m.test_cross_module_gil_inner_pybind11_acquired() |
150 | | - |
151 | | - |
152 | | -def test_cross_module_gil_nested_custom_released(): |
153 | | - """Makes sure that the GIL can be nested acquired/released by another module |
154 | | - from a GIL-released state using custom locking logic.""" |
155 | | - m.test_cross_module_gil_nested_custom_released() |
156 | | - |
157 | | - |
158 | | -def test_cross_module_gil_nested_custom_acquired(): |
159 | | - """Makes sure that the GIL can be nested acquired/acquired by another module |
160 | | - from a GIL-acquired state using custom locking logic.""" |
161 | | - m.test_cross_module_gil_nested_custom_acquired() |
162 | | - |
163 | | - |
164 | | -def test_cross_module_gil_nested_pybind11_released(): |
165 | | - """Makes sure that the GIL can be nested acquired/released by another module |
166 | | - from a GIL-released state using pybind11 locking logic.""" |
167 | | - m.test_cross_module_gil_nested_pybind11_released() |
168 | | - |
169 | | - |
170 | | -def test_cross_module_gil_nested_pybind11_acquired(): |
171 | | - """Makes sure that the GIL can be nested acquired/acquired by another module |
172 | | - from a GIL-acquired state using pybind11 locking logic.""" |
173 | | - m.test_cross_module_gil_nested_pybind11_acquired() |
174 | | - |
175 | | - |
176 | | -def test_release_acquire(): |
177 | | - assert m.test_release_acquire(0xAB) == "171" |
178 | | - |
179 | | - |
180 | | -def test_nested_acquire(): |
181 | | - assert m.test_nested_acquire(0xAB) == "171" |
182 | | - |
183 | | - |
184 | | -def test_multi_acquire_release_cross_module(): |
185 | | - for bits in range(16 * 8): |
186 | | - internals_ids = m.test_multi_acquire_release_cross_module(bits) |
187 | | - assert len(internals_ids) == 2 if bits % 8 else 1 |
|
0 commit comments