-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
117 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,62 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
// Similar to pybind11/tests/test_class_release_gil_before_calling_cpp_dtor.cpp | ||
|
||
#ifndef CLIF_TESTING_CLASS_RELEASE_GIL_BEFORE_CALLING_CPP_DTOR_H_ | ||
#define CLIF_TESTING_CLASS_RELEASE_GIL_BEFORE_CALLING_CPP_DTOR_H_ | ||
|
||
#include <Python.h> | ||
|
||
#include <string> | ||
#include <unordered_map> | ||
|
||
namespace clif_testing::class_release_gil_before_calling_cpp_dtor { | ||
|
||
using RegistryType = std::unordered_map<std::string, int>; | ||
|
||
inline RegistryType &PyGILState_Check_Results() { | ||
static auto *singleton = new RegistryType(); | ||
return *singleton; | ||
} | ||
|
||
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(); | ||
} | ||
}; | ||
|
||
std::string PopPyGILState_Check_Result(const std::string &unique_key) { | ||
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); | ||
} | ||
|
||
} // namespace clif_testing::class_release_gil_before_calling_cpp_dtor | ||
|
||
#endif // CLIF_TESTING_CLASS_RELEASE_GIL_BEFORE_CALLING_CPP_DTOR_H_ |
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
20 changes: 20 additions & 0 deletions
20
clif/testing/python/class_release_gil_before_calling_cpp_dtor.clif
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,20 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from "clif/testing/class_release_gil_before_calling_cpp_dtor.h": | ||
namespace `clif_testing::class_release_gil_before_calling_cpp_dtor`: | ||
class ProbeType: | ||
def __init__(self, unique_key: str) | ||
|
||
def PopPyGILState_Check_Result(unique_key: str) -> str |
32 changes: 32 additions & 0 deletions
32
clif/testing/python/class_release_gil_before_calling_cpp_dtor_test.py
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,32 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import gc | ||
|
||
from absl.testing import absltest | ||
|
||
from clif.testing.python import class_release_gil_before_calling_cpp_dtor as tm | ||
|
||
|
||
class ReleaseGilBeforeCallingCppDtorTest(absltest.TestCase): | ||
|
||
def test_with_probe_type(self): | ||
unique_key = "release_gil_before_calling_cpp_dtor" | ||
tm.ProbeType(unique_key) | ||
gc.collect() | ||
self.assertEqual(tm.PopPyGILState_Check_Result(unique_key), "0") | ||
|
||
|
||
if __name__ == "__main__": | ||
absltest.main() |