Fix raising exception with format string.
Some exception is raised as "raise ValueError('error: %s', s)" when the
caller intended to do a format string like "raise ValueError('error: %s'
% s)". Fix them.
This is caught by newer version of pylint under raising-format-tuple:
Exception arguments suggest string formatting might be intended.
Also fixed that subprocess.CalledProcessError doesn't accept a message
argument, and change usage to use other exception as appropiate.
BUG=b:73868308
TEST=make test
Change-Id: Id0667fdf852c8bd176acbd60c51f44077eb3c5f8
Reviewed-on: https://chromium-review.googlesource.com/936352
Commit-Ready: Pi-Hsun Shih <pihsun@chromium.org>
Tested-by: Pi-Hsun Shih <pihsun@chromium.org>
Reviewed-by: Hung-Te Lin <hungte@chromium.org>
diff --git a/py/device/ec.py b/py/device/ec.py
index cd28336..78d1bd7 100644
--- a/py/device/ec.py
+++ b/py/device/ec.py
@@ -47,7 +47,7 @@
return match.group(1)
else:
raise self.Error(
- 'Unexpected output from "ectool version": %s', ec_version)
+ 'Unexpected output from "ectool version": %s' % ec_version)
def GetRWVersion(self):
"""Gets the EC RW firmware version.
@@ -61,7 +61,7 @@
return match.group(1)
else:
raise self.Error(
- 'Unexpected output from "ectool version": %s', ec_version)
+ 'Unexpected output from "ectool version": %s' % ec_version)
def GetECConsoleLog(self):
"""Gets the EC console log.
diff --git a/py/device/path.py b/py/device/path.py
index d899891..98337e2 100644
--- a/py/device/path.py
+++ b/py/device/path.py
@@ -34,8 +34,9 @@
return getattr(posixpath, attr)
type_name = type(self).__name__
if attr in posixpath.__all__:
- raise NotImplementedError('%r is not implemented in %r', attr, type_name)
- raise AttributeError('%r has no attribute %r', type_name, attr)
+ raise NotImplementedError('%r is not implemented in %r' % (attr,
+ type_name))
+ raise AttributeError('%r has no attribute %r' % (type_name, attr))
def exists(self, path):
"""Tests whether a path exists. Returns False for broken symbolic links."""
@@ -140,4 +141,3 @@
else:
current = output.strip()
return current
-
diff --git a/py/device/storage.py b/py/device/storage.py
index ec94a90..2ea8fc0 100644
--- a/py/device/storage.py
+++ b/py/device/storage.py
@@ -166,10 +166,11 @@
return '/usr/local'
def GetMainStorageDevice(self):
- partition = self.GetMountPoint(self._GetMainStorageDeviceMountPoint())[1]
+ main_storage_device_mount_point = self._GetMainStorageDeviceMountPoint()
+ partition = self.GetMountPoint(main_storage_device_mount_point)[1]
if not partition:
- raise IOError('Unable to find main storage device (%s)',
- self._MAIN_STORAGE_DEVICE_MOUNT_POINT)
+ raise IOError('Unable to find main storage device (%s)' %
+ main_storage_device_mount_point)
# remove partition suffix to get device path.
return re.sub(r'p?(\d+)$', '', partition)
diff --git a/py/gooftool/commands.py b/py/gooftool/commands.py
index e96589a..6b80089 100755
--- a/py/gooftool/commands.py
+++ b/py/gooftool/commands.py
@@ -652,7 +652,7 @@
elif method == 'cpfe':
report_upload.CpfeUpload(target_path, pipes.quote(param))
else:
- raise Error('unknown report upload method %r', method)
+ raise Error('unknown report upload method %r' % method)
@Command('finalize',
diff --git a/py/gooftool/core.py b/py/gooftool/core.py
index 30e65cb..045131a 100644
--- a/py/gooftool/core.py
+++ b/py/gooftool/core.py
@@ -625,7 +625,7 @@
if _IsFactoryVPD(k))
unknown_keys = set(dot_entries) - set(entries)
if unknown_keys:
- raise Error('Found unexpected RW VPD(s): %r', unknown_keys)
+ raise Error('Found unexpected RW VPD(s): %r' % unknown_keys)
logging.info('Removing VPD entries %s', FilterDict(entries))
if entries:
diff --git a/py/gooftool/wipe.py b/py/gooftool/wipe.py
index a088178..a937e3d 100644
--- a/py/gooftool/wipe.py
+++ b/py/gooftool/wipe.py
@@ -483,7 +483,7 @@
logging.debug('stdout: %s', proc.stdout_data)
logging.debug('stderr: %s', proc.stderr_data)
if proc.returncode != 0:
- raise subprocess.CalledProcessError('InformShopfloor failed.')
+ raise RuntimeError('InformShopfloor failed.')
def _Cutoff():
diff --git a/py/goofy/plugins/system_log_manager.py b/py/goofy/plugins/system_log_manager.py
index beb7fad..857ba28 100644
--- a/py/goofy/plugins/system_log_manager.py
+++ b/py/goofy/plugins/system_log_manager.py
@@ -114,7 +114,7 @@
for list_name in ['_clear_log_paths', '_clear_log_excluded_paths']:
list_attribute = getattr(self, list_name)
if list_attribute and not isinstance(list_attribute, list):
- raise SystemLogManagerException('%r should be a list.', list_name)
+ raise SystemLogManagerException('%r should be a list.' % list_name)
if self._clear_log_paths and self._sync_log_paths:
if set(self._clear_log_paths) & set(self._sync_log_paths):
raise SystemLogManagerException('clear_log_paths should not be '
diff --git a/py/instalog/plugins/output_gcs.py b/py/instalog/plugins/output_gcs.py
index 83b17b1..2e24f51 100755
--- a/py/instalog/plugins/output_gcs.py
+++ b/py/instalog/plugins/output_gcs.py
@@ -186,8 +186,8 @@
if blob.md5_hash != local_md5 or blob.size != local_size:
raise ValueError('Size or MD5 mismatch after uploading; '
'local_size = %d, confirmed_size = %d; local_md5 = %s, '
- 'confirmed_md5 = %s',
- local_size, blob.size, local_md5, blob.md5_hash)
+ 'confirmed_md5 = %s' % (local_size, blob.size, local_md5,
+ blob.md5_hash))
if __name__ == '__main__':
diff --git a/py/probe/function.py b/py/probe/function.py
index 8f2e287..9fedf59 100644
--- a/py/probe/function.py
+++ b/py/probe/function.py
@@ -107,7 +107,7 @@
if len(func_expression) != 1:
raise FunctionException(
- 'Function expression %s should only contain 1 item.', func_expression)
+ 'Function expression %s should only contain 1 item.' % func_expression)
func_name, kwargs = func_expression.items()[0]
if func_name not in _function_map:
raise FunctionException('Function "%s" is not registered.' % func_name)
diff --git a/py/test/fixture/robot/six_dof_calibration_robot.py b/py/test/fixture/robot/six_dof_calibration_robot.py
index 275e5e4..ea6bb32 100644
--- a/py/test/fixture/robot/six_dof_calibration_robot.py
+++ b/py/test/fixture/robot/six_dof_calibration_robot.py
@@ -94,7 +94,8 @@
res = self._serial.readline()
if -1 == res.find('OK'):
- raise robot.RobotException('Unexpected data %s received from robot.', res)
+ raise robot.RobotException(
+ 'Unexpected data %s received from robot.' % res)
if self._log:
logging.info('Received from robot: %s', res)
diff --git a/py/test/fixture/spring_bft_fixture.py b/py/test/fixture/spring_bft_fixture.py
index ad3ef72..1310a13 100644
--- a/py/test/fixture/spring_bft_fixture.py
+++ b/py/test/fixture/spring_bft_fixture.py
@@ -256,7 +256,7 @@
def IsLEDColor(self, color):
if color not in self.LED_CHECK_COMMAND:
- raise bft_fixture.BFTFixtureException('Invalid LED color %r', color)
+ raise bft_fixture.BFTFixtureException('Invalid LED color %r' % color)
(command, expect) = self.LED_CHECK_COMMAND[color]
self._Send(command, 'Fail to check %s LED. ' % color)
@@ -275,6 +275,6 @@
def SetStatusColor(self, color):
if color not in self.STATUS_COLOR_COMMAND:
- raise bft_fixture.BFTFixtureException('Invalid status color %r', color)
+ raise bft_fixture.BFTFixtureException('Invalid status color %r' % color)
self._SendRecvDefault(self.STATUS_COLOR_COMMAND[color],
'Unable to set status color to %s' % color)
diff --git a/py/test/pytests/retrieve_config.py b/py/test/pytests/retrieve_config.py
index 3e0118a..e9e47d1 100644
--- a/py/test/pytests/retrieve_config.py
+++ b/py/test/pytests/retrieve_config.py
@@ -199,8 +199,8 @@
pathname = os.path.join(mount_point, self.args.config_retrieve_path)
session.console.info('Retrieving %s from USB.', pathname)
if not os.path.exists(pathname):
- raise ValueError('File %r does not exist or it is not a file.',
- pathname)
+ raise ValueError(
+ 'File %r does not exist or it is not a file.' % pathname)
try:
file_utils.CopyFileSkipBytes(pathname, self.config_save_path, 0)
except IOError as e:
diff --git a/py/test/rf/tools/csv_reader.py b/py/test/rf/tools/csv_reader.py
index b4d8f95..0f38ec9 100644
--- a/py/test/rf/tools/csv_reader.py
+++ b/py/test/rf/tools/csv_reader.py
@@ -48,9 +48,8 @@
try:
value_in_python = ReadCsv(value_in_python.link)
except Exception as e:
- raise ValueError(
- 'Failed to load external csv - %s, %s',
- (value_in_python.link, e))
+ raise ValueError('Failed to load external csv - %s, %s' %
+ (value_in_python.link, e))
return value_in_python
diff --git a/py/test/utils/camera_utils.py b/py/test/utils/camera_utils.py
index 2237987..1ff3801 100644
--- a/py/test/utils/camera_utils.py
+++ b/py/test/utils/camera_utils.py
@@ -387,10 +387,10 @@
serial = _FilterNonPrintable(
self._dut.ReadSpecialFile(self._sn_sysfs_path)).rstrip()
except IOError as e:
- raise CameraError('Fail to read %r: %r', self._sn_sysfs_path, e)
+ raise CameraError('Fail to read %r: %r' % (self._sn_sysfs_path, e))
if serial.find('\n') >= 0:
- raise CameraError('%r contains multi-line data: %r',
- self._sn_sysfs_path, serial)
+ raise CameraError('%r contains multi-line data: %r' %
+ (self._sn_sysfs_path, serial))
return serial
@@ -422,6 +422,6 @@
return slave.Read(self._sn_i2c_param['data_addr'],
self._sn_i2c_param['length'])[::-2]
except Exception as e:
- raise CameraError('Fail to read serial number: %r', e)
+ raise CameraError('Fail to read serial number: %r' % e)
finally:
os.close(fd)
diff --git a/py/utils/cros_board_utils.py b/py/utils/cros_board_utils.py
index bf7227e..000000c 100644
--- a/py/utils/cros_board_utils.py
+++ b/py/utils/cros_board_utils.py
@@ -101,8 +101,8 @@
pattern = r'(?s)ALL_BOARDS=\((.+?)\)'
match = re.search(pattern, eclass_contents)
if not match:
- raise BuildBoardException('Unable to read pattern %s in %s',
- pattern, eclass_path)
+ raise BuildBoardException('Unable to read pattern %s in %s' %
+ (pattern, eclass_path))
boards = match.group(1).split()
self.full_name = None
diff --git a/py/utils/file_utils.py b/py/utils/file_utils.py
index 52bbc9a..15e9b16 100644
--- a/py/utils/file_utils.py
+++ b/py/utils/file_utils.py
@@ -671,7 +671,7 @@
process.stdin.write(content)
process.stdin.close()
if process.wait():
- raise subprocess.CalledProcessError('Unable to write %s' % file_path)
+ raise RuntimeError('Unable to write %s' % file_path)
def GlobSingleFile(pattern):
@@ -983,4 +983,3 @@
TryUnlink(link_path)
os.symlink(target, link_path)
-
diff --git a/py/utils/net_utils.py b/py/utils/net_utils.py
index 2066e00..78d42a7 100644
--- a/py/utils/net_utils.py
+++ b/py/utils/net_utils.py
@@ -546,7 +546,7 @@
rule += ['-p', protocol]
if port:
if (port < 1) or (port > MAX_PORT):
- raise ValueError('Invalid port number: %r', port)
+ raise ValueError('Invalid port number: %r' % port)
rule += ['--dport', str(port)]
if interface:
rule += ['-i', interface]
diff --git a/py/utils/sys_utils.py b/py/utils/sys_utils.py
index 0c9a1db..613d281 100644
--- a/py/utils/sys_utils.py
+++ b/py/utils/sys_utils.py
@@ -91,7 +91,7 @@
remove_mount_point = False
if not path.isdir(mount_point):
- raise OSError('Mount point %s does not exist', mount_point)
+ raise OSError('Mount point %s does not exist' % mount_point)
for line in file_utils.ReadLines('/proc/mounts', dut):
if line.split()[1] == mount_point: