mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
Issue #22193: Fixed integer overflow error in sys.getsizeof().
Fixed an error in _PySys_GetSizeOf declaration.
This commit is contained in:
commit
42826566f5
3 changed files with 42 additions and 6 deletions
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue