diff mbox series

[08/14] treewide: use context manager on file objects

Message ID 20230313094431.507952-9-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
Use context manager on file objects so that the resource is correctly
released even if an exception happens before it calls close().

Signed-off-by: Tzung-Bi Shih <tzungbi@kernel.org>
---
 cros/helpers/kernel.py      |  5 ++---
 cros/helpers/mcu.py         | 17 ++++++-----------
 cros/helpers/sysfs.py       | 10 ++++------
 cros/tests/cros_ec_accel.py |  5 ++---
 cros/tests/cros_ec_pwm.py   | 10 ++++------
 cros/tests/cros_ec_rtc.py   |  5 ++---
 6 files changed, 20 insertions(+), 32 deletions(-)
diff mbox series

Patch

diff --git a/cros/helpers/kernel.py b/cros/helpers/kernel.py
index db0c69f01683..9e4d769c0531 100644
--- a/cros/helpers/kernel.py
+++ b/cros/helpers/kernel.py
@@ -14,9 +14,8 @@  def current_kernel_version():
     """ Returns the current kernel version as an integer you can
         compare.
     """
-    fd = open("/proc/version", "r")
-    current = fd.read().split()[2].split("-")[0].split(".")
-    fd.close()
+    with open("/proc/version", "r") as fh:
+        current = fh.read().split()[2].split("-")[0].split(".")
     return version_to_int(int(current[0]), int(current[1]), int(current[2]))
 
 
diff --git a/cros/helpers/mcu.py b/cros/helpers/mcu.py
index 80e3328cdf08..27ce9ad5cc58 100644
--- a/cros/helpers/mcu.py
+++ b/cros/helpers/mcu.py
@@ -150,7 +150,6 @@  def mcu_hello(s, name):
     """ Checks basic comunication with MCU. """
     if not os.path.exists("/dev/" + name):
         s.skipTest("MCU " + name + " not present, skipping")
-    fd = open("/dev/" + name, "r")
     param = ec_params_hello()
     param.in_data = 0xA0B0C0D0  # magic number that the EC expects on HELLO
 
@@ -163,19 +162,16 @@  def mcu_hello(s, name):
     cmd.outsize = sizeof(response)
 
     memmove(addressof(cmd.data), addressof(param), cmd.insize)
-    fcntl.ioctl(fd, EC_DEV_IOCXCMD, cmd)
+    with open("/dev/" + name, "r") as fh:
+        fcntl.ioctl(fh, EC_DEV_IOCXCMD, cmd)
     memmove(addressof(response), addressof(cmd.data), cmd.outsize)
 
-    fd.close()
-
     s.assertEqual(cmd.result, 0)
     # magic number that the EC answers on HELLO
     s.assertEqual(response.out_data, 0xA1B2C3D4)
 
 def mcu_get_version(name):
     if os.path.exists("/dev/" + name):
-        fd = open("/dev/" + name, "r")
-
         response = ec_response_get_version()
 
         cmd = cros_ec_command()
@@ -184,25 +180,24 @@  def mcu_get_version(name):
         cmd.insize = sizeof(response)
         cmd.outsize = 0
 
-        fcntl.ioctl(fd, EC_DEV_IOCXCMD, cmd)
+        with open("/dev/" + name, "r") as fh:
+            fcntl.ioctl(fh, EC_DEV_IOCXCMD, cmd)
         memmove(addressof(response), addressof(cmd.data), cmd.insize)
 
-        fd.close()
         if cmd.result == 0:
             return response
 
 def mcu_reboot(name):
-    fd = open("/dev/" + name, "r")
     cmd = cros_ec_command()
     cmd.version = 0
     cmd.command = EC_CMD_REBOOT
     cmd.insize = 0
     cmd.outsize = 0
     try:
-        fcntl.ioctl(fd, EC_DEV_IOCXCMD, cmd)
+        with open("/dev/" + name, "r") as fh:
+            fcntl.ioctl(fh, EC_DEV_IOCXCMD, cmd)
     except IOError:
         pass
-    fd.close()
 
 def check_mcu_reboot_rw(s, name):
     if not os.path.exists("/dev/" + name):
diff --git a/cros/helpers/sysfs.py b/cros/helpers/sysfs.py
index 1ccc0cd2ba24..80653fd13431 100755
--- a/cros/helpers/sysfs.py
+++ b/cros/helpers/sysfs.py
@@ -6,9 +6,8 @@  import os
 
 def read_file(name):
     """ Returns the content of the file named 'name'."""
-    fd = open(name, "r")
-    contents = fd.read()
-    fd.close()
+    with open(name, "r") as fh:
+        contents = fh.read()
     return contents
 
 
@@ -21,9 +20,8 @@  def sysfs_check_attributes_exists(s, path, name, files, check_devtype):
     try:
         for devname in os.listdir(path):
             if check_devtype:
-                fd = open(path + "/" + devname + "/name", "r")
-                devtype = fd.read()
-                fd.close()
+                with open(path + "/" + devname + "/name", "r") as fh:
+                    devtype = fh.read()
                 if not devtype.startswith(name):
                     continue
             else:
diff --git a/cros/tests/cros_ec_accel.py b/cros/tests/cros_ec_accel.py
index 8861eb0e1c2c..b76d2a6fbb4d 100755
--- a/cros/tests/cros_ec_accel.py
+++ b/cros/tests/cros_ec_accel.py
@@ -57,8 +57,8 @@  class TestCrosECAccel(unittest.TestCase):
         try:
             for devname in os.listdir("/sys/bus/iio/devices"):
                 base_path = "/sys/bus/iio/devices/" + devname + "/"
-                fd = open(base_path + "name", "r")
-                devtype = fd.read()
+                with open(base_path + "name", "r") as fh:
+                    devtype = fh.read()
                 if devtype.startswith("cros-ec-accel"):
                     location = read_file(base_path + "location")
                     accel_scale = float(read_file(base_path + "scale"))
@@ -73,7 +73,6 @@  class TestCrosECAccel(unittest.TestCase):
                     mag = math.sqrt(mag)
                     self.assertTrue(abs(mag - exp) <= err)
                     match += 1
-                fd.close()
         except IOError as e:
             self.skipTest("Exception occured: {0}, skipping".format(e.strerror))
         if match == 0:
diff --git a/cros/tests/cros_ec_pwm.py b/cros/tests/cros_ec_pwm.py
index ba429a327db1..2b7b4a3418b1 100644
--- a/cros/tests/cros_ec_pwm.py
+++ b/cros/tests/cros_ec_pwm.py
@@ -31,12 +31,10 @@  class TestCrosECPWM(unittest.TestCase):
         fd.close()
         if not is_ec_pwm:
             self.skipTest("No EC backlight pwm found, skipping")
-        fd = open("/sys/class/backlight/backlight/max_brightness", "r")
-        brightness = int(int(fd.read()) / 2)
-        fd.close()
-        fd = open("/sys/class/backlight/backlight/brightness", "w")
-        fd.write(str(brightness))
-        fd.close()
+        with open("/sys/class/backlight/backlight/max_brightness", "r") as fh:
+            brightness = int(int(fh.read()) / 2)
+        with open("/sys/class/backlight/backlight/brightness", "w") as fh:
+            fh.write(str(brightness))
 
         with open("/sys/kernel/debug/pwm", "r") as fh:
             for line in fh:
diff --git a/cros/tests/cros_ec_rtc.py b/cros/tests/cros_ec_rtc.py
index 9f497d2fcb7e..7afed70f0429 100755
--- a/cros/tests/cros_ec_rtc.py
+++ b/cros/tests/cros_ec_rtc.py
@@ -14,9 +14,8 @@  class TestCrosECRTC(unittest.TestCase):
         match = 0
         try:
             for devname in os.listdir("/sys/class/rtc"):
-                fd = open("/sys/class/rtc/" + devname + "/name", "r")
-                devtype = fd.read()
-                fd.close()
+                with open("/sys/class/rtc/" + devname + "/name", "r") as fh:
+                    devtype = fh.read()
                 if devtype.startswith("cros-ec-rtc"):
                     files = [
                         "date",