diff options
author | Martin Braun <martin.braun@ettus.com> | 2019-11-11 16:08:46 -0800 |
---|---|---|
committer | Martin Braun <martin.braun@ettus.com> | 2019-11-26 12:21:33 -0800 |
commit | e0a40fd1d76c66e9374129536b7b10b76abad5f8 (patch) | |
tree | 9ac2a92f319f3315882a803254ca469bb445a744 /host/tests/devtest | |
parent | c8950c1e0b82ef3e849d91ffb436ac41514022a4 (diff) | |
download | uhd-e0a40fd1d76c66e9374129536b7b10b76abad5f8.tar.gz uhd-e0a40fd1d76c66e9374129536b7b10b76abad5f8.tar.bz2 uhd-e0a40fd1d76c66e9374129536b7b10b76abad5f8.zip |
devtest: Use with() statement to open files
This fixes warnings such as this during devtest:
/home/mbr0wn/src/uhddev/host/tests/devtest/uhd_test_base.py:112:
ResourceWarning: unclosed file <_io.TextIOWrapper
name='./results_x300_F457AD.log' mode='r' encoding='UTF-8'>
self.results = yaml.safe_load(open(self.results_file).read()) or {}
/path/to/uhd/host/tests/devtest/uhd_test_base.py:150:
ResourceWarning: unclosed file <_io.TextIOWrapper
name='./results_x300_F457AD.log' mode='w' encoding='UTF-8'>
yaml.dump(self.results, default_flow_style=False))
ok
Diffstat (limited to 'host/tests/devtest')
-rwxr-xr-x | host/tests/devtest/uhd_test_base.py | 14 |
1 files changed, 9 insertions, 5 deletions
diff --git a/host/tests/devtest/uhd_test_base.py b/host/tests/devtest/uhd_test_base.py index 00b0fd73b..acc52d378 100755 --- a/host/tests/devtest/uhd_test_base.py +++ b/host/tests/devtest/uhd_test_base.py @@ -2,6 +2,7 @@ # # Copyright 2015-2016 Ettus Research LLC # Copyright 2018 Ettus Research, a National Instruments Company +# Copyright 2019 Ettus Research, a National Instruments Brand # # SPDX-License-Identifier: GPL-3.0-or-later # @@ -114,7 +115,8 @@ class uhd_test_case(unittest.TestCase): self.results = {} self.results_file = os.getenv('_UHD_TEST_RESULTSFILE', "") if self.results_file and os.path.isfile(self.results_file): - self.results = yaml.safe_load(open(self.results_file).read()) or {} + with open(self.results_file) as res_file: + self.results = yaml.safe_load(res_file.read()) or {} self.args_str = os.getenv('_UHD_TEST_ARGS_STR', "") self.usrp_info = get_usrp_list(self.args_str)[0] if self.usrp_info['serial'] not in self.results: @@ -151,8 +153,8 @@ class uhd_test_case(unittest.TestCase): def tearDown(self): self.tear_down() if self.results_file: - open(self.results_file, 'w').write( - yaml.dump(self.results, default_flow_style=False)) + with open(self.results_file, 'w') as res_file: + res_file.write(yaml.dump(self.results, default_flow_style=False)) time.sleep(15) def report_result(self, testname, key, value): @@ -165,7 +167,7 @@ class uhd_test_case(unittest.TestCase): def create_addr_args_str(self, argname="args"): """ Returns an args string, usually '--args "type=XXX,serial=YYY" """ - if len(self.args_str) == 0: + if not self.args_str: return '' return '--{}={}'.format(argname, self.args_str) @@ -214,6 +216,9 @@ class uhd_example_test_case(uhd_test_case): def report_example_results(self, test_name, run_results): + """ + Helper function for report_result() when running examples. + """ for key in sorted(run_results): self.log.info('%s = %s', str(key), str(run_results[key])) self.report_result( @@ -259,4 +264,3 @@ class uhd_example_test_case(uhd_test_case): r=yaml.dump(run_results, default_flow_style=False) ) ) - |