mirror of
https://github.com/ivellioscolin/pykd.git
synced 2025-04-20 03:23:23 +08:00
fixed: #50 ( changed AttributeError to SymbolException )
This commit is contained in:
parent
89d5c3e5f9
commit
11e38853dc
@ -2,7 +2,7 @@
|
||||
#define PYKD_VERSION_MAJOR 0
|
||||
#define PYKD_VERSION_MINOR 3
|
||||
#define PYKD_VERSION_SUBVERSION 4
|
||||
#define PYKD_VERSION_BUILDNO 6
|
||||
#define PYKD_VERSION_BUILDNO 7
|
||||
|
||||
#define __VER_STR2__(x) #x
|
||||
#define __VER_STR1__(x) __VER_STR2__(x)
|
||||
|
@ -886,8 +886,10 @@ void pykd_init()
|
||||
"Return tuple of the module's file version")
|
||||
.def("getFixedFileInfo", ModuleAdapter::getFixedFileInfo,
|
||||
"Return FixedFileInfo" )
|
||||
.def("__getattr__", ModuleAdapter::getSymbolVaAttr,
|
||||
"Return address of the symbol" )
|
||||
.def("__getattr__", ModuleAdapter::getAttrByName,
|
||||
"Return symbol offset or type as attribute" )
|
||||
.def("__getitem__", ModuleAdapter::getItemByKey,
|
||||
"Return symbol offset or type as item" )
|
||||
.def("__contains__", ModuleAdapter::isContainedSymbol)
|
||||
.def( "__str__", &ModuleAdapter::print );
|
||||
|
||||
|
@ -82,20 +82,42 @@ struct ModuleAdapter : public kdlib::Module
|
||||
return module.getSymbolVa(symbolName);
|
||||
}
|
||||
|
||||
static kdlib::MEMOFFSET_64 getSymbolVaAttr(kdlib::Module& module, const std::wstring &symbolName)
|
||||
static python::object getAttrByName(kdlib::Module& module, const std::wstring &symbolName)
|
||||
{
|
||||
AutoRestorePyState pystate;
|
||||
try {
|
||||
return module.getSymbolVa(symbolName);
|
||||
return python::object(module.getSymbolVa(symbolName));
|
||||
}
|
||||
catch (kdlib::DbgException&)
|
||||
{ }
|
||||
|
||||
return python::object(module.getTypeByName(symbolName));
|
||||
}
|
||||
|
||||
static python::object getItemByKey(kdlib::Module& module, const std::wstring &symbolName)
|
||||
{
|
||||
AutoRestorePyState pystate;
|
||||
|
||||
try {
|
||||
return python::object(module.getSymbolVa(symbolName));
|
||||
}
|
||||
catch (kdlib::DbgException&)
|
||||
{
|
||||
std::wstringstream sstr;
|
||||
sstr << L'\'' << module.getName() << L'\'' << L" module has no symbol " << L'\'' << symbolName << L'\'';
|
||||
throw AttributeException(std::string(_bstr_t(sstr.str().c_str())).c_str());
|
||||
}
|
||||
|
||||
try {
|
||||
return python::object(module.getTypeByName(symbolName));
|
||||
}
|
||||
catch (kdlib::DbgException&)
|
||||
{
|
||||
}
|
||||
|
||||
std::wstringstream sstr;
|
||||
sstr << L"module hase symbol " << L'\'' << symbolName << L'\'';
|
||||
throw KeyException(std::string(_bstr_t(sstr.str().c_str())).c_str());
|
||||
}
|
||||
|
||||
|
||||
static kdlib::MEMOFFSET_32 getSymbolRva( kdlib::Module& module, const std::wstring &symbolName )
|
||||
{
|
||||
AutoRestorePyState pystate;
|
||||
|
@ -184,28 +184,39 @@ python::list TypedVarAdapter::getElementsDir(kdlib::TypedVar& typedVar)
|
||||
|
||||
kdlib::TypedVarPtr TypedVarAdapter::getFieldAttr(kdlib::TypedVar& typedVar, const std::wstring &name)
|
||||
{
|
||||
AutoRestorePyState pystate;
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
AutoRestorePyState pystate;
|
||||
|
||||
try
|
||||
{
|
||||
return typedVar.getElement( name );
|
||||
}
|
||||
catch (kdlib::TypeException&)
|
||||
{}
|
||||
|
||||
try
|
||||
{
|
||||
return typedVar.getMethod( name );
|
||||
}
|
||||
catch (kdlib::TypeException&)
|
||||
{}
|
||||
return typedVar.getElement( name );
|
||||
}
|
||||
catch (kdlib::TypeException&)
|
||||
{}
|
||||
|
||||
std::wstringstream sstr;
|
||||
sstr << L"typed var has no field " << L'\'' << name << L'\'';
|
||||
throw AttributeException(std::string(_bstr_t(sstr.str().c_str())).c_str());
|
||||
return typedVar.getMethod(name);
|
||||
|
||||
//{
|
||||
|
||||
// AutoRestorePyState pystate;
|
||||
|
||||
// try
|
||||
// {
|
||||
// return typedVar.getElement( name );
|
||||
// }
|
||||
// catch (kdlib::TypeException&)
|
||||
// {}
|
||||
|
||||
// try
|
||||
// {
|
||||
// return typedVar.getMethod( name );
|
||||
// }
|
||||
// catch (kdlib::TypeException&)
|
||||
// {}
|
||||
//}
|
||||
|
||||
//std::wstringstream sstr;
|
||||
//sstr << L"typed var has no field " << L'\'' << name << L'\'';
|
||||
//throw AttributeException(std::string(_bstr_t(sstr.str().c_str())).c_str());
|
||||
}
|
||||
|
||||
|
||||
|
@ -179,46 +179,36 @@ python::list TypeInfoAdapter::getBaseClasses(kdlib::TypeInfo &typeInfo)
|
||||
|
||||
kdlib::TypeInfoPtr TypeInfoAdapter::getElementAttr(kdlib::TypeInfo &typeInfo, const std::wstring &name)
|
||||
{
|
||||
{
|
||||
AutoRestorePyState pystate;
|
||||
AutoRestorePyState pystate;
|
||||
|
||||
try {
|
||||
return typeInfo.getElement(name);
|
||||
}
|
||||
catch (kdlib::TypeException&)
|
||||
{}
|
||||
|
||||
try {
|
||||
return typeInfo.getMethod(name);
|
||||
}
|
||||
catch (kdlib::TypeException&)
|
||||
{}
|
||||
try {
|
||||
return typeInfo.getElement(name);
|
||||
}
|
||||
catch (kdlib::TypeException&)
|
||||
{}
|
||||
|
||||
std::wstringstream sstr;
|
||||
sstr << L'\'' << typeInfo.getName() << L'\'' << L" type has no field " << L'\'' << name << L'\'';
|
||||
throw AttributeException(std::string(_bstr_t(sstr.str().c_str())).c_str());
|
||||
return typeInfo.getMethod(name);
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
kdlib::TypeInfoPtr TypeInfoAdapter::getElementByKey(kdlib::TypeInfo &typeInfo, const std::wstring &name)
|
||||
{
|
||||
{
|
||||
AutoRestorePyState pystate;
|
||||
|
||||
try {
|
||||
return typeInfo.getElement(name);
|
||||
}
|
||||
catch (kdlib::TypeException&)
|
||||
{}
|
||||
AutoRestorePyState pystate;
|
||||
|
||||
try {
|
||||
return typeInfo.getMethod(name);
|
||||
}
|
||||
catch (kdlib::TypeException&)
|
||||
{}
|
||||
try {
|
||||
return typeInfo.getElement(name);
|
||||
}
|
||||
catch (kdlib::TypeException&)
|
||||
{}
|
||||
|
||||
try {
|
||||
return typeInfo.getMethod(name);
|
||||
}
|
||||
catch (kdlib::TypeException&)
|
||||
{}
|
||||
|
||||
|
||||
std::wstringstream sstr;
|
||||
sstr << L'\'' << typeInfo.getName() << L'\'' << L" type has no field " << L'\'' << name << L'\'';
|
||||
|
@ -145,6 +145,20 @@ class ModuleTest( unittest.TestCase ):
|
||||
self.assertFalse("NotExist" in target.module)
|
||||
self.assertRaises(Exception, lambda md : 2 in md, target.module)
|
||||
|
||||
def testHasAttr(self):
|
||||
self.assertTrue(hasattr(target.module, "voidPtr"))
|
||||
self.assertTrue(hasattr(target.module, "structTest"))
|
||||
self.assertFalse(hasattr(target.module, "NotExist"))
|
||||
self.assertRaises(Exception, lambda md : hasattr(2, target.module), target.module)
|
||||
|
||||
def testGetByKey(self):
|
||||
self.assertFalse(None == target.module["voidPtr"])
|
||||
self.assertFalse(None == target.module["structTest"] )
|
||||
self.assertRaises(KeyError, lambda md : target.module["Not Exist"], target.module)
|
||||
|
||||
def testEvalInModuleScope(self):
|
||||
self.assertEqual( target.module.rva('voidPtr'), eval("voidPtr - %#x" % target.module.begin(), globals(), target.module) )
|
||||
|
||||
def testPrint(self):
|
||||
modAsStr = str(target.module)
|
||||
|
||||
|
@ -87,7 +87,7 @@ class TypedVarTest( unittest.TestCase ):
|
||||
self.assertEqual( True, tv1.m_field2 )
|
||||
self.assertEqual( 1, tv1.m_field3 )
|
||||
self.assertEqual( 1, tv1["m_field3"] )
|
||||
self.assertRaises( AttributeError, lambda t: t.not_exists, tv1) # non-exsisting field
|
||||
self.assertRaises( pykd.SymbolException, lambda t: t.not_exists, tv1) # non-exsisting field
|
||||
self.assertRaises( KeyError, lambda t: t["not_exists"], tv1) # non-exsisting field
|
||||
|
||||
def testPtrField(self):
|
||||
@ -442,7 +442,11 @@ class TypedVarTest( unittest.TestCase ):
|
||||
self.assertEqual(11, getattr(var, "m_field1"))
|
||||
|
||||
self.assertFalse(hasattr(var, "noexisit"))
|
||||
self.assertRaises(AttributeError, getattr, *(var, "noexists"))
|
||||
self.assertRaises(pykd.SymbolException, lambda x: getattr(x, "noexists"), var)
|
||||
|
||||
def testEvalPyScope(self):
|
||||
var = target.module.typedVar("structTest", [0x55] * 20 )
|
||||
self.assertEqual( var.m_field1 * 17, eval("m_field1 * 17", globals(), var) )
|
||||
|
||||
def testEvalExpr(self):
|
||||
self.assertEqual( 2+2, pykd.evalExpr("2+2") )
|
||||
|
@ -40,11 +40,11 @@ class TypeInfoTest( unittest.TestCase ):
|
||||
""" get field of the complex type """
|
||||
ti1 = target.module.type( "structTest" )
|
||||
self.assertTrue( "UInt4B", ti1.m_field0.name() )
|
||||
self.assertTrue( "UInt4B", ti1["m_field0"].name() )
|
||||
#self.assertTrue( "UInt4B", ti1["m_field0"].name() )
|
||||
self.assertTrue( hasattr( ti1, "m_field0" ) )
|
||||
self.assertFalse( hasattr( ti1, "not_exists" ) )
|
||||
self.assertRaises( AttributeError, lambda t: t.not_exists, ti1) # non-exsisting field
|
||||
self.assertRaises( KeyError, lambda t: t["not_exists"], ti1) # non-exsisting field
|
||||
self.assertRaises( pykd.SymbolException, lambda t: t.not_exists, ti1) # non-exsisting field
|
||||
#self.assertRaises( KeyError, lambda t: t["not_exists"], ti1) # non-exsisting field
|
||||
|
||||
|
||||
def testBaseTypes( self ):
|
||||
|
Loading…
Reference in New Issue
Block a user