From 9884b06526b261ad7888e7f2faffb50afc3eb291 Mon Sep 17 00:00:00 2001 From: "SND\\kernelnet_cp" Date: Wed, 18 Dec 2013 06:37:48 +0000 Subject: [PATCH] [0.3.x] fixed : hang up at PyGILState_Ensure in different modules ( PySide for example ) git-svn-id: https://pykd.svn.codeplex.com/svn@86876 9b283d60-5439-405e-af05-b73fd8c4d996 --- pykd/windbgext.cpp | 43 +++++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/pykd/windbgext.cpp b/pykd/windbgext.cpp index 86e2534..36982e2 100644 --- a/pykd/windbgext.cpp +++ b/pykd/windbgext.cpp @@ -38,10 +38,10 @@ void PykdExt::setUp() PyImport_AppendInittab("pykd", initpykd ); - PyEval_InitThreads(); - Py_Initialize(); + PyEval_InitThreads(); + python::object main = boost::python::import("__main__"); python::object main_namespace = main.attr("__dict__"); @@ -172,14 +172,17 @@ KDLIB_EXT_COMMAND_METHOD_IMPL(PykdExt, py) PyThreadState *localState = NULL; PyThreadState *globalState = NULL; + PyGILState_STATE gilState; PyEval_RestoreThread( m_pyState ); if ( !global ) { - globalState = PyThreadState_Swap( NULL ); + globalState = Py_NewInterpreter(); - localState = Py_NewInterpreter(); + localState = PyEval_SaveThread(); + + gilState = PyGILState_Ensure(); python::object sys = python::import("sys"); @@ -187,6 +190,12 @@ KDLIB_EXT_COMMAND_METHOD_IMPL(PykdExt, py) sys.attr("stderr") = python::object( pykd::DbgOut() ); sys.attr("stdin") = python::object( pykd::DbgIn() ); } + else + { + m_pyState = PyEval_SaveThread(); + + gilState = PyGILState_Ensure(); + } if ( args.size() == 0 ) { @@ -214,7 +223,9 @@ KDLIB_EXT_COMMAND_METHOD_IMPL(PykdExt, py) python::object global(main.attr("__dict__")); try { + PykdInterruptWatch interruptWatch; + python::exec_file( scriptFileName.c_str(), global ); } catch( python::error_already_set const & ) @@ -223,30 +234,18 @@ KDLIB_EXT_COMMAND_METHOD_IMPL(PykdExt, py) } } + PyGILState_Release(gilState); + if ( !global ) { - PyInterpreterState *interpreter = localState->interp; + PyEval_RestoreThread( localState ); - while( interpreter->tstate_head != NULL ) - { - PyThreadState *threadState = (PyThreadState*)(interpreter->tstate_head); - - PyThreadState_Clear(threadState); - - PyThreadState_Swap( NULL ); - - PyThreadState_Delete(threadState); - } - - PyInterpreterState_Clear(interpreter); - - PyInterpreterState_Delete(interpreter); + Py_EndInterpreter(localState); PyThreadState_Swap( globalState ); + + m_pyState = PyEval_SaveThread(); } - - m_pyState = PyEval_SaveThread(); - } ///////////////////////////////////////////////////////////////////////////////