[0.1.x] fixed : typedVar can nor be used as a index

git-svn-id: https://pykd.svn.codeplex.com/svn@72759 9b283d60-5439-405e-af05-b73fd8c4d996
This commit is contained in:
SND\kernelnet_cp 2011-12-29 14:00:52 +00:00 committed by Mikhail I. Izmestev
parent dd66507b4b
commit 2d141abec2
4 changed files with 35 additions and 12 deletions

View File

@ -132,7 +132,8 @@ BOOST_PYTHON_MODULE( pykd )
.def( "__str__", &intBase::str )
.def( "__hex__", &intBase::hex )
.def( "__long__", &intBase::long_ )
.def( "__int__", &intBase::int_ );
.def( "__int__", &intBase::int_ )
.def( "__index__", &intBase::long_ );
python::class_<DebugClient, DebugClientPtr>("dbgClient", "Class representing a debugging session", python::no_init )
.def( "addr64", &DebugClient::addr64,
@ -483,7 +484,8 @@ BOOST_PYTHON_MODULE( pykd )
"Return field of structure as an object attribute" )
.def( "__str__", &TypedVar::print )
.def("__len__", &TypedVar::getElementCount )
.def("__getitem__", &TypedVar::getElementByIndex );
.def("__getitem__", &TypedVar::getElementByIndex )
.def("__getitem__", &TypedVar::getElementByIndexPtr );
python::class_<Module, python::bases<intBase> >("module", "Class representing executable module", python::no_init )
.def("begin", &Module::getBase,

View File

@ -26,14 +26,12 @@ public:
}
};
class VariantToPyobj : public boost::static_visitor<python::object>
{
public:
template<typename T>
python::object operator()(T i ) const {
return python::object( i );
return python::object( i );
}
};
@ -42,10 +40,18 @@ class VariantToPylong : public boost::static_visitor<python::object>
public:
template<typename T>
python::object operator()(T i ) const {
return python::long_( i );
return python::long_( i );
}
};
class VariantToULong : public boost::static_visitor<ULONG>
{
public:
template<typename T>
ULONG operator()(T i ) const {
return static_cast<ULONG>( i );
}
};
class intBase {

View File

@ -53,6 +53,10 @@ public:
throw PyException( PyExc_TypeError, "object is unsubscriptable");
}
virtual TypedVarPtr getElementByIndexPtr( const TypedVarPtr& tv ) {
return getElementByIndex( boost::apply_visitor( VariantToULong(), tv->getValue() ) );
}
protected:
TypedVar ( IDebugClient4 *client, const TypeInfoPtr& typeInfo, ULONG64 offset );
@ -118,9 +122,9 @@ public:
return m_typeInfo->getCount();
}
virtual TypedVarPtr getElementByIndex( ULONG index ) {
if ( index > m_typeInfo->getCount() )
virtual TypedVarPtr getElementByIndex( ULONG index )
{
if ( index >= m_typeInfo->getCount() )
{
throw PyException( PyExc_IndexError, "Index out of range" );
}

View File

@ -63,7 +63,12 @@ class TypedVarTest( unittest.TestCase ):
self.assertEqual( 2, tv.m_arrayField[1] )
self.assertEqual( 3, tv.m_noArrayField )
self.assertNotEqual( -1, tv.m_arrayField[0] )
self.assertNotEqual( 0, tv.m_noArrayField )
self.assertNotEqual( 0, tv.m_noArrayField )
try:
tv.m_arrayField[len(tv.m_arrayField)]
self.assertTrue(False)
except IndexError:
self.assertTrue(True)
def testGlobalVar(self):
self.assertEqual( 4, target.module.typedVar( "g_ulongValue" ) )
@ -120,5 +125,11 @@ class TypedVarTest( unittest.TestCase ):
tv = target.module.typedVar("g_classChild")
self.assertEqual( 3, tv.m_enumField )
self.assertEqual( target.module.type("enumType").THREE, tv.m_enumField )
def testIndex(self):
ind = target.module.typedVar( "g_ucharValue" )
self.assertEqual( 5, [0,5,10][ind] )
tv = target.module.typedVar( "g_struct3" )
self.assertEqual( 2, tv.m_arrayField[ind] )