From f3e27d30b8ad966ed294b6ea91f80e7239ba1a10 Mon Sep 17 00:00:00 2001
From: "SND\\kernelnet_cp"
 <SND\kernelnet_cp@9b283d60-5439-405e-af05-b73fd8c4d996>
Date: Wed, 18 Dec 2013 08:11:29 +0000
Subject: [PATCH] rollback previous changelist

git-svn-id: https://pykd.svn.codeplex.com/svn@86878 9b283d60-5439-405e-af05-b73fd8c4d996
---
 pykd/windbgext.cpp | 43 ++++++++++++++++++++++---------------------
 1 file changed, 22 insertions(+), 21 deletions(-)

diff --git a/pykd/windbgext.cpp b/pykd/windbgext.cpp
index 36982e2..86e2534 100644
--- a/pykd/windbgext.cpp
+++ b/pykd/windbgext.cpp
@@ -38,10 +38,10 @@ void PykdExt::setUp()
 
     PyImport_AppendInittab("pykd", initpykd ); 
 
-    Py_Initialize();
-
     PyEval_InitThreads();
 
+    Py_Initialize();
+
     python::object  main = boost::python::import("__main__");
 
     python::object  main_namespace = main.attr("__dict__");
@@ -172,17 +172,14 @@ KDLIB_EXT_COMMAND_METHOD_IMPL(PykdExt, py)
 
     PyThreadState   *localState = NULL;
     PyThreadState   *globalState = NULL;
-    PyGILState_STATE  gilState;
 
     PyEval_RestoreThread( m_pyState );
 
     if ( !global )
     {
-        globalState = Py_NewInterpreter();
+        globalState = PyThreadState_Swap( NULL );
 
-        localState = PyEval_SaveThread();
-
-        gilState = PyGILState_Ensure();
+        localState = Py_NewInterpreter();
 
         python::object       sys = python::import("sys");
 
@@ -190,12 +187,6 @@ 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 )
     {
@@ -223,9 +214,7 @@ 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 & )
@@ -234,18 +223,30 @@ KDLIB_EXT_COMMAND_METHOD_IMPL(PykdExt, py)
         }
     }
 
-    PyGILState_Release(gilState);
-
     if ( !global )
     {
-        PyEval_RestoreThread( localState );
+        PyInterpreterState  *interpreter = localState->interp;
 
-        Py_EndInterpreter(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);
 
         PyThreadState_Swap( globalState );
-
-        m_pyState = PyEval_SaveThread();
     }
+
+    m_pyState = PyEval_SaveThread();
+
 }
 
 ///////////////////////////////////////////////////////////////////////////////