diff mbox series

[06/14] helpers/mcu: fix ResourceWarning on /dev/cros_ec

Message ID 20230313094431.507952-7-tzungbi@kernel.org (mailing list archive)
State Handled Elsewhere
Headers show
Series cros-ec-tests: fix some exceptions and clean-ups | expand

Commit Message

Tzung-Bi Shih March 13, 2023, 9:44 a.m. UTC
Some errors have been revealed after the runner is-a TextTestRunner.

Fix the following warning:
> ResourceWarning: unclosed file <_io.TextIOWrapper name='/dev/cros_ec'

Signed-off-by: Tzung-Bi Shih <tzungbi@kernel.org>
---
 cros/helpers/mcu.py | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
diff mbox series

Patch

diff --git a/cros/helpers/mcu.py b/cros/helpers/mcu.py
index e1f09f39af89..80e3328cdf08 100644
--- a/cros/helpers/mcu.py
+++ b/cros/helpers/mcu.py
@@ -112,8 +112,6 @@  def is_feature_supported(feature):
     global ECFEATURES
 
     if ECFEATURES == -1:
-        fd = open("/dev/cros_ec", "r")
-
         param = ec_params_get_features()
         response = ec_response_get_features()
 
@@ -124,11 +122,10 @@  def is_feature_supported(feature):
         cmd.outsize = sizeof(response)
 
         memmove(addressof(cmd.data), addressof(param), cmd.outsize)
-        fcntl.ioctl(fd, EC_DEV_IOCXCMD, cmd)
+        with open("/dev/cros_ec", "r") as fh:
+            fcntl.ioctl(fh, EC_DEV_IOCXCMD, cmd)
         memmove(addressof(response), addressof(cmd.data), cmd.outsize)
 
-        fd.close()
-
         if cmd.result == 0:
             ECFEATURES = response.out_data
         else: