From 27db13b5de63c8207f874e64d6260791068a0e98 Mon Sep 17 00:00:00 2001 From: "SND\\ussrhero_cp" Date: Tue, 21 Jun 2016 20:44:29 +0000 Subject: [PATCH] [pykd_ext_2.0] updated : pykd help command git-svn-id: https://pykd.svn.codeplex.com/svn@90991 9b283d60-5439-405e-af05-b73fd8c4d996 --- pykd_ext/pyapi.h | 1 + pykd_ext/pyinterpret.cpp | 32 +++++++++----- pykd_ext/pykd_ext_vc120.vcxproj | 1 - pykd_ext/pykd_ext_vc120.vcxproj.filters | 3 -- pykd_ext/windbgext.cpp | 57 +++++++++++++++++++------ 5 files changed, 66 insertions(+), 28 deletions(-) diff --git a/pykd_ext/pyapi.h b/pykd_ext/pyapi.h index 9dd2fbd..e928492 100644 --- a/pykd_ext/pyapi.h +++ b/pykd_ext/pyapi.h @@ -82,6 +82,7 @@ PyObject* PyProperty_Type(); void PyErr_Fetch(PyObject **ptype, PyObject **pvalue, PyObject **ptraceback); void PyErr_NormalizeException(PyObject**exc, PyObject**val, PyObject**tb); void PyErr_SetString(PyObject *type, const char *message); +void PyErr_Clear(); PyObject* PyFile_FromString(char *filename, char *mode); FILE* PyFile_AsFile(PyObject *pyfile); diff --git a/pykd_ext/pyinterpret.cpp b/pykd_ext/pyinterpret.cpp index 36735bb..b0d1b90 100644 --- a/pykd_ext/pyinterpret.cpp +++ b/pykd_ext/pyinterpret.cpp @@ -98,7 +98,8 @@ public: char* (__stdcall *PyString_AsString)(PyObject *string); void(__stdcall *PyErr_Fetch)(PyObject **ptype, PyObject **pvalue, PyObject **ptraceback); void(__stdcall *PyErr_NormalizeException)(PyObject**exc, PyObject**val, PyObject**tb); - void (__stdcall *PyErr_SetString)(PyObject *type, const char *message); + void(__stdcall *PyErr_SetString)(PyObject *type, const char *message); + void(__stdcall *PyErr_Clear)(); PyObject* (__stdcall *PyImport_AddModule)(const char *name); PyObject* (__stdcall *PyClass_New)(PyObject* className, PyObject* classBases, PyObject* classDict); PyObject* (__stdcall *PyInstance_New)(PyObject *classobj, PyObject *arg, PyObject *kw); @@ -392,6 +393,7 @@ PyModule::PyModule(int majorVesion, int minorVersion) *reinterpret_cast(&PyErr_Fetch) = GetProcAddress(m_handlePython, "PyErr_Fetch"); *reinterpret_cast(&PyErr_NormalizeException) = GetProcAddress(m_handlePython, "PyErr_NormalizeException"); *reinterpret_cast(&PyErr_SetString) = GetProcAddress(m_handlePython, "PyErr_SetString"); + *reinterpret_cast(&PyErr_Clear) = GetProcAddress(m_handlePython, "PyErr_Clear"); *reinterpret_cast(&PyImport_AddModule) = GetProcAddress(m_handlePython, "PyImport_AddModule"); *reinterpret_cast(&PyImport_ImportModule) = GetProcAddress(m_handlePython, "PyImport_ImportModule"); *reinterpret_cast(&PyClass_New) = GetProcAddress(m_handlePython, "PyClass_New"); @@ -470,20 +472,23 @@ void PyModule::deactivate() void PyModule::checkPykd() { + if (m_pykdInit) + return; + PyObject* pykdMod = PyImport_ImportModule("pykd"); if (!pykdMod) - throw std::exception("Pykd package is not installed.You can install it by command \"!pip install pykd\""); + { + PyObject* mainName = isPy3 ? PyUnicode_FromString("__main__") : PyString_FromString("__main__"); + PyObject* mainMod = PyImport_Import(mainName); + PyObject* globals = PyObject_GetAttrString(mainMod, "__dict__"); + PyObject* result = PyRun_String("__import__('pykd').initialize()\n", Py_file_input, globals, globals); + if (mainName) Py_DecRef(mainName); + if (mainMod) Py_DecRef(mainMod); + if (globals) Py_DecRef(globals); + if (result) Py_DecRef(result); + } - PyObject* mainName = isPy3 ? PyUnicode_FromString("__main__") : PyString_FromString("__main__"); - PyObject* mainMod = PyImport_Import(mainName); - PyObject* globals = PyObject_GetAttrString(mainMod, "__dict__"); - PyObject* result = PyRun_String("__import__('pykd').initialize()\n", Py_file_input, globals, globals); - - if (mainName) Py_DecRef(mainName); - if (mainMod) Py_DecRef(mainMod); - if (globals) Py_DecRef(globals); - if (result) Py_DecRef(result); if (pykdMod) Py_DecRef(pykdMod); m_pykdInit = true; @@ -697,6 +702,11 @@ void PyErr_NormalizeException(PyObject**exc, PyObject**val, PyObject**tb) PythonSingleton::get()->currentInterpreter()->m_module->PyErr_NormalizeException(exc, val, tb); } +void PyErr_Clear() +{ + PythonSingleton::get()->currentInterpreter()->m_module->PyErr_Clear(); +} + void PyErr_SetString(PyObject *type, const char *message) { PythonSingleton::get()->currentInterpreter()->m_module->PyErr_SetString(type, message); diff --git a/pykd_ext/pykd_ext_vc120.vcxproj b/pykd_ext/pykd_ext_vc120.vcxproj index c5fad86..4473b3c 100644 --- a/pykd_ext/pykd_ext_vc120.vcxproj +++ b/pykd_ext/pykd_ext_vc120.vcxproj @@ -209,7 +209,6 @@ - diff --git a/pykd_ext/pykd_ext_vc120.vcxproj.filters b/pykd_ext/pykd_ext_vc120.vcxproj.filters index 4873e94..6c8784f 100644 --- a/pykd_ext/pykd_ext_vc120.vcxproj.filters +++ b/pykd_ext/pykd_ext_vc120.vcxproj.filters @@ -70,9 +70,6 @@ Source Files - - Source Files - diff --git a/pykd_ext/windbgext.cpp b/pykd_ext/windbgext.cpp index 8884c5e..1df444e 100644 --- a/pykd_ext/windbgext.cpp +++ b/pykd_ext/windbgext.cpp @@ -169,13 +169,49 @@ info( ////////////////////////////////////////////////////////////////////////////// static const char printUsageMsg[] = + "\n" "usage:\n" - "!py [options] [file]\n" + "\n" + "!help\n" + "\tprint this text\n" + "\n" + "!info\n" + "\tlist installed python interpreters\n" + "\n" + "!py [version] [options] [file]\n" + "\trun python script or REPL\n" + "\n" + "\tVersion:\n" + "\t-2 : use Python2\n" + "\t-2.x : use Python2.x\n" + "\t-3 : use Python3\n" + "\t-3.x : use Python3.x\n" + "\n" "\tOptions:\n" "\t-g --global : run code in the common namespace\n" - "\t-l --local : run code in the isolate namespace\n" - "!pip\n" - "!info\n"; + "\t-l --local : run code in the isolated namespace\n" + "\n" + "\tcommand samples:\n" + "\t\"!py\" : run REPL\n" + "\t\"!py --local\" : run REPL in the isolated namespace\n" + "\t\"!py -g script.py 10 \"string\"\" : run script file with argument in the commom namespace\n" + "\n" + "!pip [version] [args]\n" + "\trun pip package manager\n" + "\n" + "\tVersion:\n" + "\t-2 : use Python2\n" + "\t-2.x : use Python2.x\n" + "\t-3 : use Python3\n" + "\t-3.x : use Python3.x\n" + "\n" + "\tpip command samples:\n" + "\t\"pip list\" : show all installed packagies\n" + "\t\"pip install pykd\" : install pykd\n" + "\t\"pip install --upgrade pykd\" : upgrade pykd to the latest version\n" + "\t\"pip show pykd\" : show info about pykd package\n" + ; + ////////////////////////////////////////////////////////////////////////////// @@ -200,13 +236,6 @@ help( ////////////////////////////////////////////////////////////////////////////// -static const char consoleScript[] = - "import pykd\n" - "from pykd import *\n" - "import code\n" - "code.InteractiveConsole(globals()).interact()\n"; - - extern "C" HRESULT CALLBACK @@ -252,7 +281,9 @@ py( if (opts.args.empty()) { - PyObjectRef result = PyRun_String(consoleScript, Py_file_input, globals, globals); + PyObjectRef result = PyRun_String("import pykd\nfrom pykd import *\n", Py_file_input, globals, globals); + PyErr_Clear(); + result = PyRun_String("import code\ncode.InteractiveConsole(globals()).interact()\n", Py_file_input, globals, globals); } else { @@ -342,7 +373,7 @@ pip( getPythonVersion(majorVersion, minorVersion); - AutoInterpreter autoInterpreter(opts.global, majorVersion, minorVersion); + AutoInterpreter autoInterpreter(true, majorVersion, minorVersion); PyObjectRef dbgOut = make_pyobject(client); PySys_SetObject("stdout", dbgOut);