Update memory leak test to use local dict

Use a local dictionary instead of py::globals(). This should still cause the test to fail.
This commit is contained in:
yeganer 2018-03-14 12:39:48 +01:00 committed by GitHub
parent d27b39e15d
commit 4c3d1c8d67
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -309,29 +309,18 @@ PYBIND11_EMBEDDED_MODULE(test_memory_leak, m) {
}
TEST_CASE("Test that C++ objects are properly deconstructed.") {
{
auto local = py::dict();
local['__builtins__'] = py::globals()['__builtins__'];
py::exec(R"(
from test_memory_leak import TestObject
guard = TestObject(lambda: None)
)", py::globals(), py::globals());
)", local, local);
REQUIRE(1 == TestObject::_count);
// Explicitly remove reference to the object from the global dictionary. This
// allows the object to be garbage collected
py::globals()["guard"] = py::none();
}
py::finalize_interpreter();
py::initialize_interpreter();
REQUIRE(0 == TestObject::_count);
py::exec(R"(
from test_memory_leak import TestObject
guard = TestObject(lambda: None)
)", py::globals(), py::globals());
REQUIRE(1 == TestObject::_count);
py::finalize_interpreter();
py::initialize_interpreter();
REQUIRE(0 == TestObject::_count);
}