From 2f6662e1747c3ce1bc5e2d811ceda4794a52dfa0 Mon Sep 17 00:00:00 2001 From: Wenzel Jakob Date: Mon, 25 Apr 2016 09:16:41 +0200 Subject: [PATCH] Python 2.7.x fixes for new gil_scoped_release --- include/pybind11/pybind11.h | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index d4148b071..cf7f4b7b2 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -1122,15 +1122,26 @@ class gil_scoped_release { public: gil_scoped_release(bool disassoc = false) : disassoc(disassoc) { tstate = PyEval_SaveThread(); - if (disassoc) - PyThread_set_key_value(detail::get_internals().tstate, nullptr); + if (disassoc) { + int key = detail::get_internals().tstate; + #if PY_MAJOR_VERSION < 3 + PyThread_delete_key_value(key); + #else + PyThread_set_key_value(key, nullptr); + #endif + } } ~gil_scoped_release() { if (!tstate) return; PyEval_RestoreThread(tstate); - if (disassoc) - PyThread_set_key_value(detail::get_internals().tstate, tstate); + if (disassoc) { + int key = detail::get_internals().tstate; + #if PY_MAJOR_VERSION < 3 + PyThread_delete_key_value(key); + #endif + PyThread_set_key_value(key, tstate); + } } private: PyThreadState *tstate;