From 2d141abec284b433247e6f06846e61d0f31c2f44 Mon Sep 17 00:00:00 2001 From: "SND\\kernelnet_cp" Date: Thu, 29 Dec 2011 14:00:52 +0000 Subject: [PATCH] [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 --- pykd/dbgext.cpp | 6 ++++-- pykd/intbase.h | 14 ++++++++++---- pykd/typedvar.h | 10 +++++++--- test/scripts/typedvar.py | 17 ++++++++++++++--- 4 files changed, 35 insertions(+), 12 deletions(-) diff --git a/pykd/dbgext.cpp b/pykd/dbgext.cpp index 4348b9c..34a9035 100644 --- a/pykd/dbgext.cpp +++ b/pykd/dbgext.cpp @@ -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_("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", "Class representing executable module", python::no_init ) .def("begin", &Module::getBase, diff --git a/pykd/intbase.h b/pykd/intbase.h index 5d9f1ee..4fb81ea 100644 --- a/pykd/intbase.h +++ b/pykd/intbase.h @@ -26,14 +26,12 @@ public: } }; - - class VariantToPyobj : public boost::static_visitor { public: template python::object operator()(T i ) const { - return python::object( i ); + return python::object( i ); } }; @@ -42,10 +40,18 @@ class VariantToPylong : public boost::static_visitor public: template python::object operator()(T i ) const { - return python::long_( i ); + return python::long_( i ); } }; +class VariantToULong : public boost::static_visitor +{ +public: + template + ULONG operator()(T i ) const { + return static_cast( i ); + } +}; class intBase { diff --git a/pykd/typedvar.h b/pykd/typedvar.h index 14a0977..1db4892 100644 --- a/pykd/typedvar.h +++ b/pykd/typedvar.h @@ -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" ); } diff --git a/test/scripts/typedvar.py b/test/scripts/typedvar.py index 803bb72..a69ded7 100644 --- a/test/scripts/typedvar.py +++ b/test/scripts/typedvar.py @@ -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] )