factory: Use `list(filter())` when not iterating
`filter()` will return a list in python2, but return an iterator object in
python3.
So if we are not iterating on the result, we need to use `list()` to
wrap it.
BUG=chromium:999876
TEST=make test
Change-Id: I6e39bc26e5a1e037b81591b45b0718f234f8c344
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/factory/+/1924212
Reviewed-by: Cheng-Han Yang <chenghan@chromium.org>
Commit-Queue: Yilin Yang <kerker@chromium.org>
Tested-by: Yilin Yang <kerker@chromium.org>
Auto-Submit: Yilin Yang <kerker@chromium.org>
diff --git a/py/gooftool/chroot.py b/py/gooftool/chroot.py
index b75214d..6f0c4fc 100644
--- a/py/gooftool/chroot.py
+++ b/py/gooftool/chroot.py
@@ -97,7 +97,7 @@
files_dirs = self.file_dir_list + [
sysconfig.get_python_lib(standard_lib=True),
sysconfig.get_python_inc()]
- files_dirs = filter(os.path.exists, files_dirs)
+ files_dirs = list(filter(os.path.exists, files_dirs))
process_utils.Spawn(('tar -h -c %s | '
'tar -C %s -x --skip-old-files' %
(' '.join(files_dirs), self.new_root)),
diff --git a/py/goofy/goofy_remote.py b/py/goofy/goofy_remote.py
index 7282d62..e391d03 100755
--- a/py/goofy/goofy_remote.py
+++ b/py/goofy/goofy_remote.py
@@ -159,9 +159,10 @@
# on DUT.
SpawnRsyncToDUT(
['-azlKC', '--force', '--exclude', '*.pyc'] +
- filter(os.path.exists,
- [os.path.join(paths.FACTORY_DIR, x)
- for x in ('bin', 'py', 'py_pkg', 'sh', 'third_party', 'init')]) +
+ list(filter(os.path.exists,
+ [os.path.join(paths.FACTORY_DIR, x)
+ for x in
+ ('bin', 'py', 'py_pkg', 'sh', 'third_party', 'init')])) +
['%s:/usr/local/factory' % args.host],
check_call=True, log=True)
diff --git a/py/hwid/v3/valid_hwid_db_unittest.py b/py/hwid/v3/valid_hwid_db_unittest.py
index 3098cd3..e6afbf7 100755
--- a/py/hwid/v3/valid_hwid_db_unittest.py
+++ b/py/hwid/v3/valid_hwid_db_unittest.py
@@ -145,7 +145,7 @@
exception_list = pool.map(
_CheckProject, [(project_name, projects_info[project_name], hwid_dir)
for project_name in projects])
- exception_list = filter(None, exception_list)
+ exception_list = list(filter(None, exception_list))
if exception_list:
error_msg = []
diff --git a/py/probe/functions/match.py b/py/probe/functions/match.py
index ed15bb8..3db617a 100644
--- a/py/probe/functions/match.py
+++ b/py/probe/functions/match.py
@@ -116,7 +116,7 @@
self.rule = self.ConstructRule(self.args.rule)
def Apply(self, data):
- results = filter(self.Match, data)
+ results = list(filter(self.Match, data))
return [{'values': res} for res in results]
def Match(self, item):
diff --git a/py/test/pytests/bluetooth.py b/py/test/pytests/bluetooth.py
index b1c1c13..18e993c 100644
--- a/py/test/pytests/bluetooth.py
+++ b/py/test/pytests/bluetooth.py
@@ -618,7 +618,7 @@
bluetooth_manager = self.dut.bluetooth
adapter = bluetooth_manager.GetFirstAdapter(self.host_mac)
devices = bluetooth_manager.GetAllDevices(adapter).values()
- devices_to_unpair = filter(_ShouldUnpairDevice, devices)
+ devices_to_unpair = list(filter(_ShouldUnpairDevice, devices))
logging.info('Unpairing %d device(s)', len(devices_to_unpair))
for device_to_unpair in devices_to_unpair:
address = device_to_unpair['Address']
@@ -791,7 +791,7 @@
bluetooth_manager = self.dut.bluetooth
adapter = bluetooth_manager.GetFirstAdapter(self.host_mac)
devices = bluetooth_manager.GetAllDevices(adapter).values()
- connected_devices = filter(_ConnectedDevice, devices)
+ connected_devices = list(filter(_ConnectedDevice, devices))
logging.info('Connected and paired %d device(s)', len(connected_devices))
return not connected_devices
diff --git a/py/test/test_lists/test_list.py b/py/test/test_lists/test_list.py
index 55adff5..12daf6b 100644
--- a/py/test/test_lists/test_list.py
+++ b/py/test/test_lists/test_list.py
@@ -269,7 +269,7 @@
"""
super(FactoryTestList, self).__init__(_root=True, subtests=subtests)
self.state_instance = state_instance
- self.subtests = filter(None, type_utils.FlattenList(subtests))
+ self.subtests = list(filter(None, type_utils.FlattenList(subtests)))
self.path_map = {}
self.root = self
self.test_list_id = test_list_id
diff --git a/py/test/test_lists/test_object.py b/py/test/test_lists/test_object.py
index 85cbdf4..b642f2a 100644
--- a/py/test/test_lists/test_object.py
+++ b/py/test/test_lists/test_object.py
@@ -180,8 +180,8 @@
"""
self.pytest_name = pytest_name
- self.subtests = filter(None, type_utils.FlattenList(subtests or []))
- assert len(filter(None, [pytest_name, subtests])) <= 1, (
+ self.subtests = list(filter(None, type_utils.FlattenList(subtests or [])))
+ assert len(list(filter(None, [pytest_name, subtests]))) <= 1, (
'Only one of pytest_name and subtests can be specified')
# The next test under its parent, this value will be updated by
diff --git a/py/test/test_ui.py b/py/test/test_ui.py
index 9416c38..bc581a8 100644
--- a/py/test/test_ui.py
+++ b/py/test/test_ui.py
@@ -291,9 +291,10 @@
base = os.path.splitext(py_script)[0]
# Find and register the static directory, if any.
- static_dirs = filter(os.path.exists,
- [base + '_static',
- os.path.join(os.path.dirname(py_script), 'static')])
+ static_dirs = list(filter(os.path.exists,
+ [base + '_static',
+ os.path.join(os.path.dirname(py_script),
+ 'static')]))
if len(static_dirs) > 1:
raise type_utils.TestFailure(
'Cannot have both of %s - delete one!' % static_dirs)
diff --git a/py/test/utils/evdev_utils.py b/py/test/utils/evdev_utils.py
index 1e877e9..06b3209 100644
--- a/py/test/utils/evdev_utils.py
+++ b/py/test/utils/evdev_utils.py
@@ -187,7 +187,7 @@
dev_filter = item
else:
raise ValueError('Invalid argument %r' % item)
- candidates = filter(dev_filter, candidates)
+ candidates = list(filter(dev_filter, candidates))
if len(candidates) == 1:
return candidates[0]
diff --git a/py/umpire/server/rpc_dut.py b/py/umpire/server/rpc_dut.py
index 0ea7b21..0b1dd24 100644
--- a/py/umpire/server/rpc_dut.py
+++ b/py/umpire/server/rpc_dut.py
@@ -99,7 +99,7 @@
matched_file = glob.glob(glob_pathname)
# Only return files.
- matched_file = filter(os.path.isfile, matched_file)
+ matched_file = list(filter(os.path.isfile, matched_file))
return [os.path.relpath(x, parameters_dir) for x in matched_file]
@umpire_rpc.RPCCall
diff --git a/py/utils/process_utils.py b/py/utils/process_utils.py
index 7565ff2..3f6b8ca 100644
--- a/py/utils/process_utils.py
+++ b/py/utils/process_utils.py
@@ -396,7 +396,7 @@
os.kill(pid, sig)
except OSError:
pass
- pids = filter(IsProcessAlive, pids)
+ pids = list(filter(IsProcessAlive, pids))
if not pids:
return
time.sleep(0.2) # Sleep 200 ms and try again