Skip to content

Commit

Permalink
Merge pull request #42 from rtzoeller/dir_support
Browse files Browse the repository at this point in the history
Calling dir() on a Structure includes Fields
  • Loading branch information
posborne committed Oct 7, 2016
2 parents 1912bca + 7c915fb commit 081c93d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions suitcase/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,9 @@ def __setattr__(self, key, value):
return field.setval(value)
return object.__setattr__(self, key, value)

def __dir__(self):
return dir(type(self)) + [str(k) for k, v in self._sorted_fields]

def __iter__(self):
return iter(self._sorted_fields)

Expand Down
9 changes: 9 additions & 0 deletions suitcase/test/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -1189,5 +1189,14 @@ def test_unpack_greedy_after(self):
self.assertEqual(m.last.value, b"Doe")
self.assertEqual(m.greedy, b"Hello World!")


class TestDiscoverableFields(unittest.TestCase):
def test_dir(self):
m = PascalString16()
attrs = dir(m)
self.assertTrue("length" in attrs)
self.assertTrue("value" in attrs)


if __name__ == "__main__":
unittest.main()

0 comments on commit 081c93d

Please sign in to comment.