summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2014-11-15 11:21:01 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2014-11-15 11:21:01 (GMT)
commitf9b2aa2efed3374df55fe945922f67a322a5d695 (patch)
tree9a2a7ec475afa1c6b884e712a42e23c61d62bab6 /Lib
parentaf627902aaf879cf64bf6b4ebd73f4dcf74e29db (diff)
downloadcpython-f9b2aa2efed3374df55fe945922f67a322a5d695.zip
cpython-f9b2aa2efed3374df55fe945922f67a322a5d695.tar.gz
cpython-f9b2aa2efed3374df55fe945922f67a322a5d695.tar.bz2
Issue #22193: Fixed integer overflow error in sys.getsizeof().
Fixed an error in _PySys_GetSizeOf declaration.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_sys.py25
1 files changed, 25 insertions, 0 deletions
diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py
index 745e4ad..42462f4 100644
--- a/Lib/test/test_sys.py
+++ b/Lib/test/test_sys.py
@@ -494,6 +494,31 @@ class SizeofTest(unittest.TestCase):
# but lists are
self.assertEqual(sys.getsizeof([]), size('P PP') + gc_header_size)
+ def test_errors(self):
+ class BadSizeof(object):
+ def __sizeof__(self):
+ raise ValueError
+ self.assertRaises(ValueError, sys.getsizeof, BadSizeof())
+
+ class InvalidSizeof(object):
+ def __sizeof__(self):
+ return None
+ self.assertRaises(TypeError, sys.getsizeof, InvalidSizeof())
+ sentinel = ["sentinel"]
+ self.assertIs(sys.getsizeof(InvalidSizeof(), 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.test_support.calcobjsize
self.assertEqual(sys.getsizeof(True, -1), size('l'))