diff --git a/satella/instrumentation/memory/get_object_size.py b/satella/instrumentation/memory/get_object_size.py index fae6a17950ac91cded03c2ca9731ad5fa669cd06..63ed7a7fca61302a9d39c28c1c2179b71eacd7b3 100644 --- a/satella/instrumentation/memory/get_object_size.py +++ b/satella/instrumentation/memory/get_object_size.py @@ -9,8 +9,12 @@ def get_size(obj, seen=None) -> int: :param obj: object to measure :return: size in bytes of the object and all of it's subcomponents + :raises RuntimeError: when ran on PyPy """ - size = sys.getsizeof(obj) + try: + size = sys.getsizeof(obj) + except TypeError: + raise RuntimeError('Running on PyPy?') if seen is None: seen = set() obj_id = id(obj) diff --git a/tests/test_instrumentation/test_memory.py b/tests/test_instrumentation/test_memory.py index 3b73a9a81127810afb1500e64b915e394276ba41..7fa6daeba96737fcec1bc0c86368c46680f3a279 100644 --- a/tests/test_instrumentation/test_memory.py +++ b/tests/test_instrumentation/test_memory.py @@ -1,5 +1,6 @@ import logging import os +import platform import signal import sys @@ -27,10 +28,16 @@ class TestMemory(unittest.TestCase): install_dump_frames_on(signal.SIGUSR1) os.kill(os.getpid(), signal.SIGUSR1) + @unittest.skipIf(platform.python_implementation() == 'PyPy', 'does not work on PyPy') def test_get_size(self): a = 'a' * 1024 self.assertGreaterEqual(get_size(a), 1024) + @unittest.skipIf(platform.python_implementation() != 'PyPy', 'requires PyPy') + def test_get_size_runtime_error(self): + a = 'a' * 1024 + self.assertRaises(RuntimeError, lambda: get_size(a)) + def test_dump_memory(self): dump_memory_on()