From 2f2b51e40c7027d80bf4a6741f2063e2c224f938 Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Mon, 18 Sep 2023 17:44:12 +0200 Subject: make Geldschieberbot a context manager Previosuly there was the problem that the state was saved when the bot object was destructed. When there was an unhandled exception it was possible for the builtins to be cleanedup before the bot's destructor was executed resulting in yet another exception because the open builtin was no longer available. This is no longer possible when the bot is used as a context manager python guarantees that the __exit__ code is executed before leaving the with block. This allows us prevent the state from beeing saved when there was an exception to prevent invalid states. --- test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'test.py') diff --git a/test.py b/test.py index f392a4d..8535485 100755 --- a/test.py +++ b/test.py @@ -1417,8 +1417,8 @@ class TestStateLoadStore(unittest.TestCase): def test_init(self): sp = "test/state_3users.json" - bot = Geldschieberbot(sp, DEFAULT_GROUP_ID) - self.assertTrue(compare_state(sp, state=bot.state)) + with Geldschieberbot(sp, DEFAULT_GROUP_ID) as bot: + self.assertTrue(compare_state(sp, state=bot.state)) def test_explicit_load(self): sp = "test/state_3users.json" -- cgit v1.2.3