diff mbox series

[07/14] cros_ec_pwm: fix ResourceWarning on /sys/kernel/debug/pwm

Message ID 20230313094431.507952-8-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='/sys/kernel/debug/pwm'

Signed-off-by: Tzung-Bi Shih <tzungbi@kernel.org>
---
 cros/tests/cros_ec_pwm.py | 24 +++++++++++-------------
 1 file changed, 11 insertions(+), 13 deletions(-)
diff mbox series

Patch

diff --git a/cros/tests/cros_ec_pwm.py b/cros/tests/cros_ec_pwm.py
index bcc9fbcea1d5..ba429a327db1 100644
--- a/cros/tests/cros_ec_pwm.py
+++ b/cros/tests/cros_ec_pwm.py
@@ -37,16 +37,14 @@  class TestCrosECPWM(unittest.TestCase):
         fd = open("/sys/class/backlight/backlight/brightness", "w")
         fd.write(str(brightness))
         fd.close()
-        fd = open("/sys/kernel/debug/pwm", "r")
-        line = fd.readline()
-        while line:
-            if "backlight" in line:
-                start = line.find("duty") + 6
-                self.assertNotEqual(start, 5)
-                end = start + line[start:].find(" ")
-                self.assertNotEqual(start, end)
-                duty = int(line[start:end])
-                self.assertNotEqual(duty, 0)
-                break
-            line = fd.readline()
-        fd.close()
+
+        with open("/sys/kernel/debug/pwm", "r") as fh:
+            for line in fh:
+                if "backlight" in line:
+                    start = line.find("duty") + 6
+                    self.assertNotEqual(start, 5)
+                    end = start + line[start:].find(" ")
+                    self.assertNotEqual(start, end)
+                    duty = int(line[start:end])
+                    self.assertNotEqual(duty, 0)
+                    break