-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into sh_merge_master
- Loading branch information
Showing
5 changed files
with
132 additions
and
11 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
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,53 @@ | ||
#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>; | ||
|
||
static RegistryType &PyGILState_Check_Results() { | ||
static RegistryType singleton; // Local static variables have thread-safe initialization. | ||
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("PopPyGILState_Check_Result", [](const std::string &unique_key) -> std::string { | ||
RegistryType ® = PyGILState_Check_Results(); | ||
if (reg.count(unique_key) == 0) { | ||
return "MISSING"; | ||
} | ||
int res = reg[unique_key]; | ||
reg.erase(unique_key); | ||
return std::to_string(res); | ||
}); | ||
} |
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,21 @@ | ||
from __future__ import annotations | ||
|
||
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.PopPyGILState_Check_Result(unique_key) | ||
assert result == expected_result |