From 7f7fe8dd643e934c841720159990df3419edf549 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Ma=C5=9Blanka?= Date: Fri, 30 Aug 2024 12:10:27 +0200 Subject: [PATCH] added some things, v2.25.4 --- satella/coding/structures/singleton.py | 3 ++- satella/files.py | 3 +-- satella/instrumentation/memory/get_object_size.py | 8 ++++---- tests/test_files.py | 1 - tests/test_instrumentation/test_memory.py | 9 ++++++--- 5 files changed, 13 insertions(+), 11 deletions(-) diff --git a/satella/coding/structures/singleton.py b/satella/coding/structures/singleton.py index 0a7609d8..1744c969 100644 --- a/satella/coding/structures/singleton.py +++ b/satella/coding/structures/singleton.py @@ -61,7 +61,8 @@ def SingletonWithRegardsTo(num_args: int, weak_refs: bool = False): :param weak_refs: if True, then singleton will be stored within a weak dictionary, so that it cleans up after itself when the values are gone. - .. warning:: If you set weak_refs to False and have a potentially unbounded number of arguments, you better watch out. + .. warning:: If you set weak_refs to False and have a potentially unbounded number of argument values, you better + watch out for the memory usage. """ def inner(cls): diff --git a/satella/files.py b/satella/files.py index 04a5b488..eca97481 100644 --- a/satella/files.py +++ b/satella/files.py @@ -63,8 +63,7 @@ def __enter__(self): def __exit__(self, exc_type, exc_val, exc_tb): assert self.prev_path is not None - with reraise_as(FileNotFoundError): - os.chdir(self.prev_path) + os.chdir(self.prev_path) return False diff --git a/satella/instrumentation/memory/get_object_size.py b/satella/instrumentation/memory/get_object_size.py index ffdeabb5..67c604bd 100644 --- a/satella/instrumentation/memory/get_object_size.py +++ b/satella/instrumentation/memory/get_object_size.py @@ -1,3 +1,4 @@ +import platform import sys @@ -11,10 +12,9 @@ def get_size(obj, seen=None) -> int: :return: size in bytes of the object and all of it's subcomponents :raises RuntimeError: when ran on PyPy """ - try: - size = sys.getsizeof(obj) - except TypeError: - raise RuntimeError('Running on PyPy?') + if platform.python_implementation() != 'CPython': + raise RuntimeError('Runnable only on CPython') + size = sys.getsizeof(obj) if seen is None: seen = set() obj_id = id(obj) diff --git a/tests/test_files.py b/tests/test_files.py index 0d3e7f6d..6206d6f0 100644 --- a/tests/test_files.py +++ b/tests/test_files.py @@ -20,7 +20,6 @@ def test_monotonous(self): with jump_to_directory('test_d/path'): path = os.getcwd() self.assertTrue(path.endswith('path')) - self.assertTrue(os.path.exists('path')) def test_read_nonexistent_file(self): self.assertRaises(FileNotFoundError, lambda: read_in_file('moot')) diff --git a/tests/test_instrumentation/test_memory.py b/tests/test_instrumentation/test_memory.py index c072e8ce..be51355e 100644 --- a/tests/test_instrumentation/test_memory.py +++ b/tests/test_instrumentation/test_memory.py @@ -22,7 +22,6 @@ def can_fire(self, *args) -> bool: class TestMemory(unittest.TestCase): - @unittest.skipIf(platform.python_implementation(), 'This will not work on PyPy') def test_get_size_dict(self): a = {'aba': 'aba'} @@ -30,8 +29,12 @@ class Aba: def __init__(self): self.aba = 'aba' - self.assertGreater(get_size(a), 6) - self.assertGreater(get_size(Aba()), 6) + if platform.python_implementation() == 'PyPy': + self.assertRaises(RuntimeError, get_size, a) + self.assertRaises(RuntimeError, get_size, Aba()) + else: + self.assertGreater(get_size(a), 6) + self.assertGreater(get_size(Aba()), 6) @unittest.skipIf(sys.platform == 'win32', 'testable only on unices') def test_install_dump_on(self):