From 6e052d1dd09f41d8fdc07102d190f0be779bfe2f Mon Sep 17 00:00:00 2001 From: Tom Keffer Date: Mon, 29 Jun 2020 15:20:03 -0700 Subject: [PATCH] Modified deep_copy() so it does not have to turn interpolation off. Really fixes issue #579. --- bin/weeutil/config.py | 23 +++++++++++------------ bin/weeutil/tests/test_config.py | 2 +- bin/weewx/engine.py | 6 ++---- bin/weewx/reportengine.py | 2 +- 4 files changed, 15 insertions(+), 18 deletions(-) diff --git a/bin/weeutil/config.py b/bin/weeutil/config.py index 75299473..8f0e49b2 100644 --- a/bin/weeutil/config.py +++ b/bin/weeutil/config.py @@ -1,5 +1,5 @@ # -# Copyright (c) 2018-2019 Tom Keffer +# Copyright (c) 2018-2020 Tom Keffer # # See the file LICENSE.txt for your full rights. # @@ -229,18 +229,17 @@ def config_from_str(input_str): def deep_copy(old_dict): - """ Return a deepcopy of a ConfigObj.""" - import copy + """ Return a deep copy of a ConfigObj. This utility can only be used by a ConfigObj, + not one of its Sections.""" + import io - # Turn off interpolation. It seems to interfere with the deep copying process. - # Save the old interpolation value. - old_interpolation = old_dict.main.interpolation - old_dict.main.interpolation = False - - new_dict = copy.deepcopy(old_dict) - - # Restore the old interpolation value - old_dict.main.interpolation = old_interpolation + with io.BytesIO() as fd: + old_dict.write(fd) + fd.seek(0) + new_dict = configobj.ConfigObj(fd, + encoding=old_dict.encoding, + default_encoding=old_dict.default_encoding, + interpolation=old_dict.interpolation) return new_dict diff --git a/bin/weeutil/tests/test_config.py b/bin/weeutil/tests/test_config.py index 7e981cc9..3dc53f79 100644 --- a/bin/weeutil/tests/test_config.py +++ b/bin/weeutil/tests/test_config.py @@ -43,7 +43,7 @@ class TestConfig(unittest.TestCase): } c_in = configobj.ConfigObj(test_dict, encoding='utf-8') - c_out = weeutil.config.deep_copy(c_in['Logging']) + c_out = weeutil.config.deep_copy(c_in)['Logging'] self.assertIsInstance(c_out, configobj.Section) self.assertEqual(c_out, test_dict['Logging']) # Try changing something and see if it's still equal: diff --git a/bin/weewx/engine.py b/bin/weewx/engine.py index 3bc5afda..77fe34a1 100644 --- a/bin/weewx/engine.py +++ b/bin/weewx/engine.py @@ -134,11 +134,9 @@ class StdEngine(object): log.debug("No services in service group %s", service_group) continue log.debug("Loading service %s", svc) - # Make sure each service gets its own copy of the config dictionary. - config_dict_copy = weeutil.config.deep_copy(config_dict) - # Get the class, then instantiate it with self and the new config dictionary as + # Get the class, then instantiate it with self and the config dictionary as # arguments: - obj = weeutil.weeutil.get_object(svc)(self,config_dict_copy) + obj = weeutil.weeutil.get_object(svc)(self, config_dict) # Append it to the list of open services. self.service_obj.append(obj) log.debug("Finished loading service %s", svc) diff --git a/bin/weewx/reportengine.py b/bin/weewx/reportengine.py index 39af2220..c3b45977 100644 --- a/bin/weewx/reportengine.py +++ b/bin/weewx/reportengine.py @@ -246,7 +246,7 @@ class StdReportEngine(threading.Thread): if 'Defaults' in self.config_dict['StdReport']: # Because we will be modifying the results, make a deep copy of the [[Defaults]] # section. - merge_dict = weeutil.config.deep_copy(self.config_dict['StdReport']['Defaults']) + merge_dict = weeutil.config.deep_copy(self.config_dict)['StdReport']['Defaults'] weeutil.config.merge_config(skin_dict, merge_dict) # Inject any scalar overrides. This is for backwards compatibility. These options should now go