From 0f9581cd85f26db00904497bbdb209df6407a504 Mon Sep 17 00:00:00 2001 From: Brant Watson Date: Mon, 24 Oct 2016 07:19:32 -0500 Subject: [PATCH] Implment __len__ magic method --- boltons/ioutils.py | 3 +++ tests/test_ioutils.py | 6 ++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/boltons/ioutils.py b/boltons/ioutils.py index 27f415f..d5d9ae8 100644 --- a/boltons/ioutils.py +++ b/boltons/ioutils.py @@ -172,6 +172,9 @@ class SpooledIOBase(object): self.seek(pos) return val + def __len__(self): + return self.len + def __iter__(self): yield self.readline() diff --git a/tests/test_ioutils.py b/tests/test_ioutils.py index fe439c3..e1786dc 100644 --- a/tests/test_ioutils.py +++ b/tests/test_ioutils.py @@ -197,15 +197,17 @@ class TestSpooledBytesIO(TestCase, BaseTestMixin, AssertionsMixin): self.assertEqual(f.getvalue(), test_str) def test_len_no_rollover(self): - """Make sure len property works with in-memory flo""" + """Make sure len works with in-memory flo""" self.spooled_flo.write(self.test_str) self.assertEqual(self.spooled_flo.len, len(self.test_str)) + self.assertEqual(len(self.spooled_flo), len(self.test_str)) def test_len_rollover(self): - """Make sure len property works with on-disk flo""" + """Make sure len works with on-disk flo""" self.spooled_flo.write(self.test_str) self.spooled_flo.rollover() self.assertEqual(self.spooled_flo.len, len(self.test_str)) + self.assertEqual(len(self.spooled_flo), len(self.test_str)) def test_invalid_type(self): """Ensure TypeError raised when writing unicode to SpooledBytesIO"""