[pykd] fixed: issue 8657( !pycmd does not allow to define functions )

git-svn-id: https://pykd.svn.codeplex.com/svn@67703 9b283d60-5439-405e-af05-b73fd8c4d996
This commit is contained in:
SND\kernelnet_cp 2011-07-06 15:45:11 +00:00
parent 5d1bd33f62
commit 8e00dc3612

View File

@ -788,112 +788,120 @@ pycmd( PDEBUG_CLIENT4 client, PCSTR args )
try { try {
if ( !std::string( args ).empty() ) // ïåðåíàïðàâëåíèå ñòàíäàðòíûõ ïîòîêîâ ÂÂ
{ boost::python::object sys = boost::python::import("sys");
try {
boost::python::object retObj = boost::python::eval( args, WindbgGlobalSession::global(), WindbgGlobalSession::global() ); dbgOut dout;
sys.attr("stdout") = boost::python::object( dout );
if ( retObj.ptr() != NULL ) dbgIn din;
{ sys.attr("stdin") = boost::python::object( din );
PyObject *s = PyObject_Str( retObj.ptr() );
dbgExt->control->Output( DEBUG_OUTPUT_NORMAL, "%s\n", PyString_AS_STRING( s ) ); boost::python::object syntaxError = boost::python::import("exceptions").attr("SyntaxError");
Py_DECREF( s );
} std::string commandBuffer;
}
catch( boost::python::error_already_set const & ) bool commandCompleted = true;
do {
if ( commandCompleted )
{ {
// îøèáêà â ñêðèïòå
PyObject *errtype = NULL, *errvalue = NULL, *traceback = NULL;
PyErr_Fetch( &errtype, &errvalue, &traceback );
if(errvalue != NULL)
{
PyObject *s = PyObject_Str(errvalue);
dbgExt->control->Output( DEBUG_OUTPUT_ERROR, "%s\n", PyString_AS_STRING( s ) );
Py_DECREF(s);
}
Py_XDECREF(errvalue);
Py_XDECREF(errtype);
Py_XDECREF(traceback);
}
}
else
{
char str[100];
ULONG inputSize;
bool stopInput = false;
boost::python::import( "pykd" );
boost::python::object main = boost::python::import("__main__");
boost::python::object global(main.attr("__dict__"));
// ïåðåíàïðàâëåíèå ñòàíäàðòíûõ ïîòîêîâ ÂÂ
boost::python::object sys = boost::python::import("sys");
dbgOut dout;
sys.attr("stdout") = boost::python::object( dout );
dbgIn din;
sys.attr("stdin") = boost::python::object( din );
do {
std::string output;
dbgExt->control->Output( DEBUG_OUTPUT_NORMAL, ">>>" ); dbgExt->control->Output( DEBUG_OUTPUT_NORMAL, ">>>" );
}
else
{
dbgExt->control->Output( DEBUG_OUTPUT_NORMAL, "..." );
}
do { {
char str[100];
ULONG inputSize;
OutputReader outputReader( (IDebugClient*)client ); OutputReader outputReader( (IDebugClient*)client );
HRESULT hres = dbgExt->control->Input( str, sizeof(str), &inputSize );
HRESULT hres = dbgExt->control->Input( str, sizeof(str), &inputSize ); if ( FAILED( hres ) )
throw;
if ( FAILED( hres ) || std::string( str ) == "" ) if ( commandCompleted )
{
if ( std::string( str ) == "" )
break;
commandBuffer = str;
}
else
{
if ( std::string( str ) == "" )
commandCompleted = true;
else
{ {
stopInput = true; commandBuffer.append("\n");
break; commandBuffer.append( str );
} }
}
}
} while( FALSE ); if ( commandCompleted )
{
try {
if ( !stopInput ) boost::python::exec( commandBuffer.c_str(), WindbgGlobalSession::global(), WindbgGlobalSession::global() );
try {
boost::python::exec( str, WindbgGlobalSession::global(), WindbgGlobalSession::global() ); commandBuffer.clear();
} }
catch( boost::python::error_already_set const & ) catch( boost::python::error_already_set const & )
{
PyObject *errtype = NULL, *errvalue = NULL, *traceback = NULL;
PyErr_Fetch( &errtype, &errvalue, &traceback );
if ( errtype && errvalue )
{ {
// îøèáêà â ñêðèïòå boost::python::tuple errValueObj( boost::python::handle<>( boost::python::borrowed(errvalue) ) );
PyObject *errtype = NULL, *errvalue = NULL, *traceback = NULL;
PyErr_Fetch( &errtype, &errvalue, &traceback ); PyObject *errvalueStr = PyObject_Str(errvalue);
if(errvalue != NULL) std::string errValueStr = boost::python::extract<std::string>( errvalueStr );
Py_XDECREF(errvalueStr);
if ( PyErr_GivenExceptionMatches( syntaxError.ptr(), errtype ) )
{ {
PyObject *s = PyObject_Str(errvalue); if ( errValueObj[0] == "unexpected EOF while parsing" )
{
dbgExt->control->Output( DEBUG_OUTPUT_ERROR, "%s/n", PyString_AS_STRING( s ) ); commandCompleted = false;
}
Py_DECREF(s); else
{
dbgExt->control->Output( DEBUG_OUTPUT_ERROR, "%s\n", errValueStr.c_str() );
}
} }
else
{
dbgExt->control->Output( DEBUG_OUTPUT_ERROR, "%s\n", errValueStr.c_str() );
}
Py_XDECREF(errvalue); Py_XDECREF(errvalue);
Py_XDECREF(errtype); Py_XDECREF(errtype);
Py_XDECREF(traceback); Py_XDECREF(traceback);
} }
else
{
Py_XDECREF(errvalue);
Py_XDECREF(errtype);
Py_XDECREF(traceback);
} while( !stopInput ); throw;
} }
}
}
} while( true );
} }
catch(...) catch(...)
{ {
dbgExt->control->Output( DEBUG_OUTPUT_ERROR, "unexpected error" ); dbgExt->control->Output( DEBUG_OUTPUT_ERROR, "unexpected error" );