summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_sys.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py
index 93bb0ef..9ac105f 100644
--- a/Lib/test/test_sys.py
+++ b/Lib/test/test_sys.py
@@ -770,6 +770,37 @@ class SizeofTest(unittest.TestCase):
# but lists are
self.assertEqual(sys.getsizeof([]), vsize('Pn') + gc_header_size)
+ def test_errors(self):
+ class BadSizeof:
+ def __sizeof__(self):
+ raise ValueError
+ self.assertRaises(ValueError, sys.getsizeof, BadSizeof())
+
+ class InvalidSizeof:
+ def __sizeof__(self):
+ return None
+ self.assertRaises(TypeError, sys.getsizeof, InvalidSizeof())
+ sentinel = ["sentinel"]
+ self.assertIs(sys.getsizeof(InvalidSizeof(), sentinel), sentinel)
+
+ class FloatSizeof:
+ def __sizeof__(self):
+ return 4.5
+ self.assertRaises(TypeError, sys.getsizeof, FloatSizeof())
+ self.assertIs(sys.getsizeof(FloatSizeof(), sentinel), sentinel)
+
+ class OverflowSizeof(int):
+ def __sizeof__(self):
+ return int(self)
+ self.assertEqual(sys.getsizeof(OverflowSizeof(sys.maxsize)),
+ sys.maxsize + self.gc_headsize)
+ with self.assertRaises(OverflowError):
+ sys.getsizeof(OverflowSizeof(sys.maxsize + 1))
+ with self.assertRaises(ValueError):
+ sys.getsizeof(OverflowSizeof(-1))
+ with self.assertRaises((ValueError, OverflowError)):
+ sys.getsizeof(OverflowSizeof(-sys.maxsize - 1))
+
def test_default(self):
size = test.support.calcvobjsize
self.assertEqual(sys.getsizeof(True), size('') + self.longdigit)