From f7926083ca9a1eaa16c401bd18bfce4a209a2d74 Mon Sep 17 00:00:00 2001 From: Simon Ebner Date: Sun, 24 Oct 2021 22:59:28 +0200 Subject: [PATCH] Clean up unclosed file handles Close all file handles that are left dangling to avoid warnings such as ``` ResourceWarning: unclosed file <_io.TextIOWrapper name='...' mode='r' encoding='UTF-8'> params = json_load(filename.open('r')) ``` --- freqtrade/optimize/hyperopt_tools.py | 10 +++++----- freqtrade/strategy/hyper.py | 3 ++- tests/optimize/test_hyperopt_tools.py | 3 ++- tests/strategy/test_strategy_loading.py | 4 ++-- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/freqtrade/optimize/hyperopt_tools.py b/freqtrade/optimize/hyperopt_tools.py index cfbc2757e..0b2efa5c2 100755 --- a/freqtrade/optimize/hyperopt_tools.py +++ b/freqtrade/optimize/hyperopt_tools.py @@ -1,4 +1,3 @@ - import io import logging from copy import deepcopy @@ -64,10 +63,11 @@ class HyperoptTools(): 'export_time': datetime.now(timezone.utc), } logger.info(f"Dumping parameters to {filename}") - rapidjson.dump(final_params, filename.open('w'), indent=2, - default=hyperopt_serializer, - number_mode=rapidjson.NM_NATIVE | rapidjson.NM_NAN - ) + with filename.open('w') as f: + rapidjson.dump(final_params, f, indent=2, + default=hyperopt_serializer, + number_mode=rapidjson.NM_NATIVE | rapidjson.NM_NAN + ) @staticmethod def try_export_params(config: Dict[str, Any], strategy_name: str, params: Dict): diff --git a/freqtrade/strategy/hyper.py b/freqtrade/strategy/hyper.py index dad282d7e..eaf41263a 100644 --- a/freqtrade/strategy/hyper.py +++ b/freqtrade/strategy/hyper.py @@ -381,7 +381,8 @@ class HyperStrategyMixin(object): if filename.is_file(): logger.info(f"Loading parameters from file {filename}") try: - params = json_load(filename.open('r')) + with filename.open('r') as f: + params = json_load(f) if params.get('strategy_name') != self.__class__.__name__: raise OperationalException('Invalid parameter file provided.') return params diff --git a/tests/optimize/test_hyperopt_tools.py b/tests/optimize/test_hyperopt_tools.py index 9c2b2e8fc..d9a52db39 100644 --- a/tests/optimize/test_hyperopt_tools.py +++ b/tests/optimize/test_hyperopt_tools.py @@ -209,7 +209,8 @@ def test_export_params(tmpdir): assert filename.is_file() - content = rapidjson.load(filename.open('r')) + with filename.open('r') as f: + content = rapidjson.load(f) assert content['strategy_name'] == 'StrategyTestV2' assert 'params' in content assert "buy" in content["params"] diff --git a/tests/strategy/test_strategy_loading.py b/tests/strategy/test_strategy_loading.py index 3a30a824a..3590c3e01 100644 --- a/tests/strategy/test_strategy_loading.py +++ b/tests/strategy/test_strategy_loading.py @@ -62,8 +62,8 @@ def test_load_strategy(default_conf, result): def test_load_strategy_base64(result, caplog, default_conf): - with (Path(__file__).parents[2] / 'freqtrade/templates/sample_strategy.py').open("rb") as file: - encoded_string = urlsafe_b64encode(file.read()).decode("utf-8") + filepath = Path(__file__).parents[2] / 'freqtrade/templates/sample_strategy.py' + encoded_string = urlsafe_b64encode(filepath.read_bytes()).decode("utf-8") default_conf.update({'strategy': 'SampleStrategy:{}'.format(encoded_string)}) strategy = StrategyResolver.load_strategy(default_conf)