Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: clear local internals after finalizing interpreter #2101 #3744

Merged
merged 3 commits into from
Feb 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/pybind11/embed.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ inline void finalize_interpreter() {
internals_ptr_ptr = capsule(builtins[id]);
}

detail::get_local_internals().registered_types_cpp.clear();
detail::get_local_internals().registered_exception_translators.clear();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to note: I verified that new TEST_CASE covers .registered_types_cpp.clear(), but it does not cover .registered_exception_translators.clear().
I think that's fine though, although if someone could add that test coverage in a follow-on PR that would be ideal.

** "covers" in the sense that the test breaks if the call is removed.


Py_Finalize();

if (internals_ptr_ptr) {
Expand Down
17 changes: 17 additions & 0 deletions tests/test_embed/test_interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,3 +375,20 @@ TEST_CASE("sys.argv gets initialized properly") {
}
py::initialize_interpreter();
}

TEST_CASE("make_iterator can be called before then after finalizing an interpreter") {
py::finalize_interpreter();

std::vector<int> container;
{
pybind11::scoped_interpreter g;
auto iter = pybind11::make_iterator(container.begin(), container.end());
}

REQUIRE_NOTHROW([&]() {
pybind11::scoped_interpreter g;
auto iter = pybind11::make_iterator(container.begin(), container.end());
}());

py::initialize_interpreter();
}