From 29e42ed8522ab85da55a53b7e944b644f98445fb Mon Sep 17 00:00:00 2001 From: "SND\\kernelnet_cp" Date: Thu, 30 Apr 2015 12:14:34 +0000 Subject: [PATCH] [0.3.x] fixed : exec_file raise std exception git-svn-id: https://pykd.svn.codeplex.com/svn@90543 9b283d60-5439-405e-af05-b73fd8c4d996 --- pykd/windbgext.cpp | 9 ++++- pykd_bootstrapper/version.h | 2 +- pykd_bootstrapper/windbgext.cpp | 65 ++++++++++++++++++++------------- snippets/ipython.py | 4 +- 4 files changed, 50 insertions(+), 30 deletions(-) diff --git a/pykd/windbgext.cpp b/pykd/windbgext.cpp index a79e71f..d1ce213 100644 --- a/pykd/windbgext.cpp +++ b/pykd/windbgext.cpp @@ -1,5 +1,6 @@ #include "stdafx.h" +#include #include namespace python = boost::python; @@ -181,10 +182,16 @@ KDLIB_EXT_COMMAND_METHOD_IMPL(PykdExt, py) PykdInterruptWatch interruptWatch; python::exec_file( scriptFileName.c_str(), global ); } - catch( python::error_already_set const & ) + catch( const python::error_already_set& ) { pykd::printException(); } + catch (const std::exception& invalidArg) + { + _bstr_t bstrInavalidArg(invalidArg.what()); + kdlib::eprintln(std::wstring(bstrInavalidArg)); + } + } if ( !global ) diff --git a/pykd_bootstrapper/version.h b/pykd_bootstrapper/version.h index 5be7b80..2099c89 100644 --- a/pykd_bootstrapper/version.h +++ b/pykd_bootstrapper/version.h @@ -3,7 +3,7 @@ #define PYKDEXT_VERSION_MAJOR 1 #define PYKDEXT_VERSION_MINOR 0 #define PYKDEXT_VERSION_SUBVERSION 1 -#define PYKDEXT_VERSION_BUILDNO 6 +#define PYKDEXT_VERSION_BUILDNO 7 #define __VER_STR2__(x) #x #define __VER_STR1__(x) __VER_STR2__(x) diff --git a/pykd_bootstrapper/windbgext.cpp b/pykd_bootstrapper/windbgext.cpp index 00b42dd..b9d13e0 100644 --- a/pykd_bootstrapper/windbgext.cpp +++ b/pykd_bootstrapper/windbgext.cpp @@ -1,5 +1,7 @@ #include "stdafx.h" +#include + #include namespace python = boost::python; @@ -235,31 +237,12 @@ KDLIB_EXT_COMMAND_METHOD_IMPL(PykdBootsTrapper, py) PyThreadState *localState = NULL; PyThreadState *globalState = NULL; + PythonSingleton::get()->beginPythonCode(); + try { InterruptWatch interruptWatch; - PythonSingleton::get()->beginPythonCode(); - - std::string scriptFileName; - if (args.size() > 0) - { - scriptFileName = getScriptFileName(args[0]); - - if (scriptFileName.empty()) - { - kdlib::eprintln(L"script file not found"); - - python::throw_error_already_set(); - } - - global = !(global || local) ? false : global; //set local by default - } - else - { - global = !(global || local) ? true : global; //set global by default - } - if (!global) { globalState = PyThreadState_Swap(NULL); @@ -275,6 +258,21 @@ KDLIB_EXT_COMMAND_METHOD_IMPL(PykdBootsTrapper, py) sys.attr("stdin") = python::object(::DbgIn()); } + std::string scriptFileName; + if (args.size() > 0) + { + scriptFileName = getScriptFileName(args[0]); + + if (scriptFileName.empty()) + throw std::invalid_argument("script not found"); + + global = !(global || local) ? false : global; //set local by default + } + else + { + global = !(global || local) ? true : global; //set global by default + } + // получаем доступ к глобальному мапу ( нужен для вызова exec_file ) python::object main = python::import("__main__"); python::object globalScope(main.attr("__dict__")); @@ -316,12 +314,17 @@ KDLIB_EXT_COMMAND_METHOD_IMPL(PykdBootsTrapper, py) python::exec_file(scriptFileName.c_str(), globalScope); } } - catch (python::error_already_set const &) + catch (const python::error_already_set&) { printException(); } + catch (const std::exception& invalidArg) + { + _bstr_t bstrInavalidArg(invalidArg.what()); + kdlib::eprintln(std::wstring(bstrInavalidArg)); + } - if (!global) + if (!global && localState) { PyInterpreterState *interpreter = localState->interp; @@ -370,6 +373,11 @@ KDLIB_EXT_COMMAND_METHOD_IMPL(PykdBootsTrapper, install) { printException(); } + catch (const std::exception& invalidArg) + { + _bstr_t bstrInavalidArg(invalidArg.what()); + kdlib::eprintln(std::wstring(bstrInavalidArg)); + } PythonSingleton::get()->endPythonCode(); } @@ -394,6 +402,11 @@ KDLIB_EXT_COMMAND_METHOD_IMPL(PykdBootsTrapper, upgrade) { printException(); } + catch (const std::exception& invalidArg) + { + _bstr_t bstrInavalidArg(invalidArg.what()); + kdlib::eprintln(std::wstring(bstrInavalidArg)); + } PythonSingleton::get()->endPythonCode(); } @@ -504,10 +517,10 @@ std::string PykdBootsTrapper::findScript(const std::string &fullFileName) &fullFileNameCStr[0], &partFileNameCStr); - if (bufSize > 0) - { + DWORD fileAttr = GetFileAttributesA(&fullFileNameCStr[0]); + + if ( (fileAttr & FILE_ATTRIBUTE_DIRECTORY) == 0 ) return std::string(&fullFileNameCStr[0]); - } } } diff --git a/snippets/ipython.py b/snippets/ipython.py index c1e9109..1f0f966 100644 --- a/snippets/ipython.py +++ b/snippets/ipython.py @@ -15,8 +15,8 @@ cfg.PromptManager.in_template = 'In <\\#>: ' cfg.PromptManager.in2_template = ' .\\D.: ' cfg.PromptManager.out_template = 'Out<\\#>: ' -cfg.InteractiveShellApp.extensions = [ 'pykdmagic' ] +#cfg.InteractiveShellApp.extensions = [ 'pykdmagic' ] ipshell = InteractiveShellEmbed(config=cfg) -ipshell() \ No newline at end of file +ipshell()