diff --git a/pykd/pymod.cpp b/pykd/pymod.cpp index 90495d8..e95a92e 100644 --- a/pykd/pymod.cpp +++ b/pykd/pymod.cpp @@ -737,6 +737,7 @@ BOOST_PYTHON_MODULE( pykd ) .def( "__getattr__", TypeInfoAdapter::getElementByName ) .def("__len__", TypeInfoAdapter::getElementCount ) .def("__getitem__", TypeInfoAdapter::getElementByIndex ) + .def("__dir__", TypeInfoAdapter::getElementDir) ; python::class_, boost::noncopyable >("typedVar", diff --git a/pykd/pytypeinfo.cpp b/pykd/pytypeinfo.cpp index 96c12a5..50f082e 100644 --- a/pykd/pytypeinfo.cpp +++ b/pykd/pytypeinfo.cpp @@ -104,4 +104,30 @@ python::list TypeInfoAdapter::getFields( kdlib::TypeInfo &typeInfo ) /////////////////////////////////////////////////////////////////////////////// +python::list TypeInfoAdapter::getElementDir(kdlib::TypeInfo &typeInfo) +{ + std::list lst; + + do { + + AutoRestorePyState pystate; + + for (size_t i = 0; i < typeInfo.getElementCount(); ++i) + { + std::wstring name = typeInfo.getElementName(i); + lst.push_back(name); + } + + } while (false); + + python::list pylst; + + for (std::list::const_iterator it = lst.begin(); it != lst.end(); ++it) + pylst.append(*it); + + return pylst; +} + +/////////////////////////////////////////////////////////////////////////////// + } // pykd namespace diff --git a/pykd/pytypeinfo.h b/pykd/pytypeinfo.h index 290d5b3..9786671 100644 --- a/pykd/pytypeinfo.h +++ b/pykd/pytypeinfo.h @@ -220,6 +220,8 @@ struct TypeInfoAdapter : public kdlib::TypeInfo { static python::list getFields( kdlib::TypeInfo &typeInfo ); + static python::list getElementDir(kdlib::TypeInfo &typeInfo); + }; struct BaseTypesEnum { diff --git a/test/scripts/typedvar.py b/test/scripts/typedvar.py index 481f7a6..e418489 100644 --- a/test/scripts/typedvar.py +++ b/test/scripts/typedvar.py @@ -329,3 +329,9 @@ class TypedVarTest( unittest.TestCase ): def testFields(self): tv = pykd.typedVar( "g_classChild") self.assertTrue( len(tv.fields())>0 ) + + def testDir(self): + tv = target.module.typedVar( "structTest", target.module.g_structTest ) + self.assertEqual(5, len(dir(tv))) + self.assertTrue("m_field3" in dir(tv)) + self.assertFalse("m_field33" in dir(tv)) diff --git a/test/scripts/typeinfo.py b/test/scripts/typeinfo.py index d97821e..06ef4cd 100644 --- a/test/scripts/typeinfo.py +++ b/test/scripts/typeinfo.py @@ -298,3 +298,9 @@ class TypeInfoTest( unittest.TestCase ): functype = target.module.typedVar( "ArrayOfMethodPtr" ).type() self.assertEqual(functype.name(), "Void(__thiscall FuncTestClass::*[2])()") + + def testDir(self): + ti = target.module.type("structTest") + self.assertEqual(5, len(dir(ti))) + self.assertTrue("m_field3" in dir(ti)) + self.assertFalse("m_field33" in dir(ti))