From: Tzung-Bi Shih <tzungbi@kernel.org>
To: bleung@chromium.org, groeck@chromium.org
Cc: chrome-platform@lists.linux.dev, tzungbi@kernel.org,
guillaume.tucker@collabora.com, denys.f@collabora.com,
ricardo.canuelo@collabora.com
Subject: [PATCH 08/14] treewide: use context manager on file objects
Date: Mon, 13 Mar 2023 17:44:25 +0800 [thread overview]
Message-ID: <20230313094431.507952-9-tzungbi@kernel.org> (raw)
In-Reply-To: <20230313094431.507952-1-tzungbi@kernel.org>
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 --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",
--
2.40.0.rc1.284.g88254d51c5-goog
next prev parent reply other threads:[~2023-03-13 9:44 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-13 9:44 [PATCH 00/14] cros-ec-tests: fix some exceptions and clean-ups Tzung-Bi Shih
2023-03-13 9:44 ` [PATCH 01/14] lava_runner: rename to LavaTestResult Tzung-Bi Shih
2023-03-13 9:44 ` [PATCH 02/14] lava_runner: use TextTestRunner Tzung-Bi Shih
2023-03-13 9:44 ` [PATCH 03/14] lava_runner: use TextTestResult Tzung-Bi Shih
2023-03-13 9:44 ` [PATCH 04/14] lava_runner: respect to verbosity flag from unittest framework Tzung-Bi Shih
2023-03-13 9:44 ` [PATCH 05/14] helpers/sysfs: fix NameError Tzung-Bi Shih
2023-03-13 9:44 ` [PATCH 06/14] helpers/mcu: fix ResourceWarning on /dev/cros_ec Tzung-Bi Shih
2023-03-13 9:44 ` [PATCH 07/14] cros_ec_pwm: fix ResourceWarning on /sys/kernel/debug/pwm Tzung-Bi Shih
2023-03-13 9:44 ` Tzung-Bi Shih [this message]
2023-03-13 9:44 ` [PATCH 09/14] cros_ec_pwm: use RE to search EC backlight PWM Tzung-Bi Shih
2023-03-13 9:44 ` [PATCH 10/14] helpers/mcu: fix packet too long error Tzung-Bi Shih
2023-03-13 9:44 ` [PATCH 11/14] helpers/mcu: fix EC_CMD_GET_FEATURES usage Tzung-Bi Shih
2023-03-13 9:44 ` [PATCH 12/14] treewide: remove "r" in open() if reading mode Tzung-Bi Shih
2023-03-13 9:44 ` [PATCH 13/14] treewide: don't use "+" operator for constructing paths Tzung-Bi Shih
2023-03-13 9:44 ` [PATCH 14/14] treewide: don't use "+" operator for concatenating strings Tzung-Bi Shih
2023-03-13 11:53 ` [PATCH 00/14] cros-ec-tests: fix some exceptions and clean-ups Ricardo Cañuelo
2023-03-14 3:09 ` Tzung-Bi Shih
2023-03-14 6:47 ` Guillaume Charles Tucker
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230313094431.507952-9-tzungbi@kernel.org \
--to=tzungbi@kernel.org \
--cc=bleung@chromium.org \
--cc=chrome-platform@lists.linux.dev \
--cc=denys.f@collabora.com \
--cc=groeck@chromium.org \
--cc=guillaume.tucker@collabora.com \
--cc=ricardo.canuelo@collabora.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox