@@ -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]))
@@ -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):
@@ -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:
@@ -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:
@@ -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:
@@ -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",
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(-)