summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2014-11-15 11:21:37 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2014-11-15 11:21:37 (GMT)
commit030e92d1a51c2caef0c23a74e65f0aaff002158f (patch)
tree3c2c3398992dedd8137917ea47af73ca34f531b8 /Lib
parented7301031917a5f63ca2e9a7cccb6b18113ca27c (diff)
downloadcpython-030e92d1a51c2caef0c23a74e65f0aaff002158f.zip
cpython-030e92d1a51c2caef0c23a74e65f0aaff002158f.tar.gz
cpython-030e92d1a51c2caef0c23a74e65f0aaff002158f.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.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py
index 93960cd..f8a9e5e 100644
--- a/Lib/test/test_sys.py
+++ b/Lib/test/test_sys.py
@@ -723,6 +723,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)