From 704435f7215b8daeb25259bd497f3d258057fa91 Mon Sep 17 00:00:00 2001 From: Andrew Resch Date: Mon, 3 Aug 2009 18:43:05 +0000 Subject: [PATCH] Config copies the defaults dict now since it is mutable Fix saving with the save_timer after setting an item Add test for testing the save timer --- deluge/config.py | 7 +++++-- tests/test_config.py | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/deluge/config.py b/deluge/config.py index a8f4a6ba4..b666c2580 100644 --- a/deluge/config.py +++ b/deluge/config.py @@ -146,7 +146,7 @@ class Config(object): self._save_timer = None if defaults: - self.__config = defaults + self.__config = dict(defaults) # Load the config from file in the config_dir if config_dir: @@ -401,7 +401,7 @@ what is currently in the config and it could not convert the value except Exception, e: log.warning("Unable to open config file: %s", filename) - self._save_timer.cancel() + # Save the new config and make sure it's written to disk try: @@ -433,6 +433,9 @@ what is currently in the config and it could not convert the value return False else: return True + finally: + if self._save_timer.active(): + self._save_timer.cancel() def run_converter(self, input_range, output_version, func): """ diff --git a/tests/test_config.py b/tests/test_config.py index e58e0d3be..51a34423b 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -79,3 +79,21 @@ class ConfigTestCase(unittest.TestCase): self.assertEquals(config["string"], "baz") self.assertEquals(config["int"], 2) + def test_save_timer(self): + config = Config("test.conf", defaults=DEFAULTS, config_dir=self.config_dir) + config["string"] = "baz" + config["int"] = 2 + self.assertTrue(config._save_timer.active()) + + def check_config(config): + self.assertTrue(not config._save_timer.active()) + del config + config = Config("test.conf", defaults=DEFAULTS, config_dir=self.config_dir) + self.assertEquals(config["string"], "baz") + self.assertEquals(config["int"], 2) + + from twisted.internet.task import deferLater + from twisted.internet import reactor + d = deferLater(reactor, 7, check_config, config) + return d +