-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test_class_release_gil_before_calling_cpp_dtor
- Loading branch information
Ralf W. Grosse-Kunstleve
committed
Jan 16, 2024
1 parent
d7a9411
commit 686ec79
Showing
3 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#include <pybind11/pybind11.h> | ||
|
||
#include "pybind11_tests.h" | ||
|
||
#include <string> | ||
#include <unordered_map> | ||
|
||
namespace pybind11_tests { | ||
namespace class_release_gil_before_calling_cpp_dtor { | ||
|
||
using RegistryType = std::unordered_map<std::string, int>; | ||
|
||
RegistryType &PyGILState_Check_Results() { | ||
static auto *singleton = new RegistryType(); | ||
return *singleton; | ||
} | ||
|
||
template <int> // Using int as a trick to easily generate a series of types. | ||
struct ProbeType { | ||
private: | ||
std::string unique_key; | ||
|
||
public: | ||
explicit ProbeType(const std::string &unique_key) : unique_key{unique_key} {} | ||
|
||
~ProbeType() { | ||
RegistryType ® = PyGILState_Check_Results(); | ||
assert(reg.count(unique_key) == 0); | ||
reg[unique_key] = PyGILState_Check(); | ||
} | ||
}; | ||
|
||
} // namespace class_release_gil_before_calling_cpp_dtor | ||
} // namespace pybind11_tests | ||
|
||
TEST_SUBMODULE(class_release_gil_before_calling_cpp_dtor, m) { | ||
using namespace pybind11_tests::class_release_gil_before_calling_cpp_dtor; | ||
|
||
py::class_<ProbeType<0>>(m, "ProbeType0").def(py::init<std::string>()); | ||
|
||
py::class_<ProbeType<1>>(m, "ProbeType1", py::release_gil_before_calling_cpp_dtor()) | ||
.def(py::init<std::string>()); | ||
|
||
m.def("GetPyGILState_Check_Result", [](const std::string &unique_key) -> std::string { | ||
RegistryType ® = PyGILState_Check_Results(); | ||
if (reg.count(unique_key) == 0) { | ||
return "MISSING"; | ||
} | ||
return std::to_string(reg[unique_key]); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import gc | ||
|
||
import pytest | ||
|
||
from pybind11_tests import class_release_gil_before_calling_cpp_dtor as m | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("probe_type", "unique_key", "expected_result"), | ||
[ | ||
(m.ProbeType0, "without_manipulating_gil", "1"), | ||
(m.ProbeType1, "release_gil_before_calling_cpp_dtor", "0"), | ||
], | ||
) | ||
def test_gil_state_check_results(probe_type, unique_key, expected_result): | ||
probe_type(unique_key) | ||
gc.collect() | ||
result = m.GetPyGILState_Check_Result(unique_key) | ||
assert result == expected_result |