[master 29/30] Make iterators and their usage Python 3 compatible (#1014220)

M4rtinK installerbot-noreply at redhat.com
Mon Jun 1 14:04:46 UTC 2015


From: Martin Kolman <mkolman at redhat.com>

Various issues such as:
- iterators need to be converted to lists before joining to a single list
- iterator need to be converted to a list before doing indexed access or
  using other list specific API on it
- trying to add a dict_values to a list
- adding lists and dict_values together
 - use chaining in the above two cases
---
 pyanaconda/bootloader.py               | 16 +++++++-
 pyanaconda/iutil.py                    |  2 +-
 pyanaconda/ui/gui/spokes/custom.py     |  3 +-
 pyanaconda/ui/gui/spokes/source.py     | 10 ++---
 pyanaconda/ui/gui/xkl_wrapper.py       |  4 +-
 pyanaconda/ui/tui/spokes/storage.py    |  2 +-
 pyanaconda/ui/tui/spokes/time_spoke.py |  2 +-
 tests/pyanaconda_tests/iutil_test.py   | 68 +++++++++++++++++-----------------
 8 files changed, 60 insertions(+), 47 deletions(-)

diff --git a/pyanaconda/bootloader.py b/pyanaconda/bootloader.py
index f5f0546..cecd442 100644
--- a/pyanaconda/bootloader.py
+++ b/pyanaconda/bootloader.py
@@ -28,6 +28,7 @@
 import blivet
 from parted import PARTITION_BIOS_GRUB
 from glob import glob
+from itertools import chain
 
 from pyanaconda import iutil
 from blivet.devicelibs import raid
@@ -534,7 +535,7 @@ def _device_type_match(self, device, types):
         return self._device_type_index(device, types) is not None
 
     def device_description(self, device):
-        device_types = self.device_descriptions.keys()
+        device_types = list(self.device_descriptions.keys())
         idx = self._device_type_index(device, device_types)
         if idx is None:
             raise ValueError("No description available for %s" % device.type)
@@ -853,6 +854,17 @@ def set_boot_args(self, *args, **kwargs):
                     self.boot_args.update(setup_args)
                     self.dracut_args.update(setup_args)
 
+        # passed-in objects
+        for cfg_obj in chain(args, kwargs.values()):
+            if hasattr(cfg_obj, "dracutSetupArgs"):
+                setup_args = cfg_obj.dracutSetupArgs()
+                self.boot_args.update(setup_args)
+                self.dracut_args.update(setup_args)
+            else:
+                setup_string = cfg_obj.dracutSetupString()
+                self.boot_args.add(setup_string)
+                self.dracut_args.add(setup_string)
+
         # This is needed for FCoE, bug #743784. The case:
         # We discover LUN on an iface which is part of multipath setup.
         # If the iface is disconnected after discovery anaconda doesn't
@@ -888,7 +900,7 @@ def set_boot_args(self, *args, **kwargs):
             self.boot_args.add(new_arg)
 
         # passed-in objects
-        for cfg_obj in list(args) + kwargs.values():
+        for cfg_obj in chain(args, kwargs.values()):
             if hasattr(cfg_obj, "dracutSetupArgs"):
                 setup_args = cfg_obj.dracutSetupArgs()
                 self.boot_args.update(setup_args)
diff --git a/pyanaconda/iutil.py b/pyanaconda/iutil.py
index 832f41a..4a89997 100644
--- a/pyanaconda/iutil.py
+++ b/pyanaconda/iutil.py
@@ -430,7 +430,7 @@ def __del__(self):
                 except OSError:
                     pass
 
-        def next(self):
+        def __next__(self):
             # Read the next line, blocking if a line is not yet available
             line = self._proc.stdout.readline().decode("utf-8")
             if line == '':
diff --git a/pyanaconda/ui/gui/spokes/custom.py b/pyanaconda/ui/gui/spokes/custom.py
index 47ffe34..6b6cbe3 100644
--- a/pyanaconda/ui/gui/spokes/custom.py
+++ b/pyanaconda/ui/gui/spokes/custom.py
@@ -91,6 +91,7 @@
 from gi.repository.AnacondaWidgets import MountpointSelector
 
 from functools import wraps
+from itertools import chain
 
 import logging
 log = logging.getLogger("anaconda")
@@ -511,7 +512,7 @@ def _populate_accordion(self):
         for root in ui_roots:
             # Don't make a page if none of the root's devices are left.
             # Also, only include devices in an old page if the format is intact.
-            if not any(d for d in root.swaps + root.mounts.values()
+            if not any(d for d in chain(root.swaps, root.mounts.values())
                         if d in self._devices and d.disks and
                            (root.name == translated_new_install_name() or d.format.exists)):
                 continue
diff --git a/pyanaconda/ui/gui/spokes/source.py b/pyanaconda/ui/gui/spokes/source.py
index 1c372ce..5b76d0b 100644
--- a/pyanaconda/ui/gui/spokes/source.py
+++ b/pyanaconda/ui/gui/spokes/source.py
@@ -1170,11 +1170,11 @@ def _reset_repoStore(self):
         """
 
         # Remove the repo checks
-        for check in self._repoNameChecks.values() + self._repoURLChecks.values() + self._repoProxyChecks.values():
-            self.remove_check(check)
-        self._repoNameChecks = {}
-        self._repoURLChecks = {}
-        self._repoProxyChecks = {}
+        for checks in self._repoChecks.values():
+            self.remove_check(checks.name_check)
+            self.remove_check(checks.url_check)
+            self.remove_check(checks.proxy_check)
+        self._repoChecks = {}
 
         self._repoStore.clear()
         repos = self.payload.addOns
diff --git a/pyanaconda/ui/gui/xkl_wrapper.py b/pyanaconda/ui/gui/xkl_wrapper.py
index 867f491..68fc06b 100644
--- a/pyanaconda/ui/gui/xkl_wrapper.py
+++ b/pyanaconda/ui/gui/xkl_wrapper.py
@@ -287,7 +287,7 @@ def add_layout(self, layout):
             raise XklWrapperError("Failed to add layout: %s" % ilverr)
 
         #do not add the same layout-variant combinanion multiple times
-        if (layout, variant) in zip(self._rec.layouts, self._rec.variants):
+        if (layout, variant) in list(zip(self._rec.layouts, self._rec.variants)):
             return
 
         self._rec.set_layouts(self._rec.layouts + [layout])
@@ -313,7 +313,7 @@ def remove_layout(self, layout):
         #we can get 'layout' or 'layout (variant)'
         (layout, variant) = parse_layout_variant(layout)
 
-        layouts_variants = zip(self._rec.layouts, self._rec.variants)
+        layouts_variants = list(zip(self._rec.layouts, self._rec.variants))
 
         if not (layout, variant) in layouts_variants:
             msg = "'%s (%s)' not in the list of added layouts" % (layout,
diff --git a/pyanaconda/ui/tui/spokes/storage.py b/pyanaconda/ui/tui/spokes/storage.py
index b28a231..41640b0 100644
--- a/pyanaconda/ui/tui/spokes/storage.py
+++ b/pyanaconda/ui/tui/spokes/storage.py
@@ -553,5 +553,5 @@ def input(self, args, key):
     def apply(self):
         """ Apply our selections. """
 
-        schemelist = self.partschemes.values()
+        schemelist = list(self.partschemes.values())
         self.data.autopart.type = schemelist[self._selection]
diff --git a/pyanaconda/ui/tui/spokes/time_spoke.py b/pyanaconda/ui/tui/spokes/time_spoke.py
index 64548e9..3a49849 100644
--- a/pyanaconda/ui/tui/spokes/time_spoke.py
+++ b/pyanaconda/ui/tui/spokes/time_spoke.py
@@ -38,7 +38,7 @@ def initialize(self):
         # it's stupid to call get_all_regions_and_timezones twice, but regions
         # needs to be unsorted in order to display in the same order as the GUI
         # so whatever
-        self._regions = timezone.get_all_regions_and_timezones().keys()
+        self._regions = list(timezone.get_all_regions_and_timezones().keys())
         self._timezones = dict((k, sorted(v)) for k, v in timezone.get_all_regions_and_timezones().items())
         self._lower_regions = [r.lower() for r in self._timezones]
 
diff --git a/tests/pyanaconda_tests/iutil_test.py b/tests/pyanaconda_tests/iutil_test.py
index 531c6d3..b765806 100644
--- a/tests/pyanaconda_tests/iutil_test.py
+++ b/tests/pyanaconda_tests/iutil_test.py
@@ -141,10 +141,10 @@ def exec_readlines_test_normal_output(self):
 
             with timer(5):
                 rl_iterator = iutil.execReadlines("/bin/sh", [testscript.name])
-                self.assertEqual(rl_iterator.next(), "one")
-                self.assertEqual(rl_iterator.next(), "two")
-                self.assertEqual(rl_iterator.next(), "three")
-                self.assertRaises(StopIteration, rl_iterator.next)
+                self.assertEqual(next(rl_iterator), "one")
+                self.assertEqual(next(rl_iterator), "two")
+                self.assertEqual(next(rl_iterator), "three")
+                self.assertRaises(StopIteration, rl_iterator.__next__)
 
         # Test output with no end of line
         with tempfile.NamedTemporaryFile(mode="w+t") as testscript:
@@ -158,10 +158,10 @@ def exec_readlines_test_normal_output(self):
 
             with timer(5):
                 rl_iterator = iutil.execReadlines("/bin/sh", [testscript.name])
-                self.assertEqual(rl_iterator.next(), "one")
-                self.assertEqual(rl_iterator.next(), "two")
-                self.assertEqual(rl_iterator.next(), "three")
-                self.assertRaises(StopIteration, rl_iterator.next)
+                self.assertEqual(next(rl_iterator), "one")
+                self.assertEqual(next(rl_iterator), "two")
+                self.assertEqual(next(rl_iterator), "three")
+                self.assertRaises(StopIteration, rl_iterator.__next__)
 
     def exec_readlines_test_exits(self):
         """Test execReadlines in different child exit situations."""
@@ -181,10 +181,10 @@ def exec_readlines_test_exits(self):
 
             with timer(5):
                 rl_iterator = iutil.execReadlines("/bin/sh", [testscript.name])
-                self.assertEqual(rl_iterator.next(), "one")
-                self.assertEqual(rl_iterator.next(), "two")
-                self.assertEqual(rl_iterator.next(), "three")
-                self.assertRaises(OSError, rl_iterator.next)
+                self.assertEqual(next(rl_iterator), "one")
+                self.assertEqual(next(rl_iterator), "two")
+                self.assertEqual(next(rl_iterator), "three")
+                self.assertRaises(OSError, rl_iterator.__next__)
 
         # Test exit on signal
         with tempfile.NamedTemporaryFile(mode="wt") as testscript:
@@ -198,10 +198,10 @@ def exec_readlines_test_exits(self):
 
             with timer(5):
                 rl_iterator = iutil.execReadlines("/bin/sh", [testscript.name])
-                self.assertEqual(rl_iterator.next(), "one")
-                self.assertEqual(rl_iterator.next(), "two")
-                self.assertEqual(rl_iterator.next(), "three")
-                self.assertRaises(OSError, rl_iterator.next)
+                self.assertEqual(next(rl_iterator), "one")
+                self.assertEqual(next(rl_iterator), "two")
+                self.assertEqual(next(rl_iterator), "three")
+                self.assertRaises(OSError, rl_iterator.__next__)
 
         # Repeat the above two tests, but exit before a final newline
         with tempfile.NamedTemporaryFile(mode="wt") as testscript:
@@ -215,10 +215,10 @@ def exec_readlines_test_exits(self):
 
             with timer(5):
                 rl_iterator = iutil.execReadlines("/bin/sh", [testscript.name])
-                self.assertEqual(rl_iterator.next(), "one")
-                self.assertEqual(rl_iterator.next(), "two")
-                self.assertEqual(rl_iterator.next(), "three")
-                self.assertRaises(OSError, rl_iterator.next)
+                self.assertEqual(next(rl_iterator), "one")
+                self.assertEqual(next(rl_iterator), "two")
+                self.assertEqual(next(rl_iterator), "three")
+                self.assertRaises(OSError, rl_iterator.__next__)
 
         with tempfile.NamedTemporaryFile(mode="wt") as testscript:
             testscript.write("""#!/bin/sh
@@ -231,10 +231,10 @@ def exec_readlines_test_exits(self):
 
             with timer(5):
                 rl_iterator = iutil.execReadlines("/bin/sh", [testscript.name])
-                self.assertEqual(rl_iterator.next(), "one")
-                self.assertEqual(rl_iterator.next(), "two")
-                self.assertEqual(rl_iterator.next(), "three")
-                self.assertRaises(OSError, rl_iterator.next)
+                self.assertEqual(next(rl_iterator), "one")
+                self.assertEqual(next(rl_iterator), "two")
+                self.assertEqual(next(rl_iterator), "three")
+                self.assertRaises(OSError, rl_iterator.__next__)
 
     def exec_readlines_test_signals(self):
         """Test execReadlines and signal receipt."""
@@ -254,10 +254,10 @@ def exec_readlines_test_signals(self):
 
                 with timer(5):
                     rl_iterator = iutil.execReadlines("/bin/sh", [testscript.name])
-                    self.assertEqual(rl_iterator.next(), "one")
-                    self.assertEqual(rl_iterator.next(), "two")
-                    self.assertEqual(rl_iterator.next(), "three")
-                    self.assertRaises(StopIteration, rl_iterator.next)
+                    self.assertEqual(next(rl_iterator), "one")
+                    self.assertEqual(next(rl_iterator), "two")
+                    self.assertEqual(next(rl_iterator), "three")
+                    self.assertRaises(StopIteration, rl_iterator.__next__)
         finally:
             signal.signal(signal.SIGHUP, old_HUP_handler)
 
@@ -278,10 +278,10 @@ def _hup_handler(signum, frame):
 
                 with timer(5):
                     rl_iterator = iutil.execReadlines("/bin/sh", [testscript.name])
-                    self.assertEqual(rl_iterator.next(), "one")
-                    self.assertEqual(rl_iterator.next(), "two")
-                    self.assertEqual(rl_iterator.next(), "three")
-                    self.assertRaises(StopIteration, rl_iterator.next)
+                    self.assertEqual(next(rl_iterator), "one")
+                    self.assertEqual(next(rl_iterator), "two")
+                    self.assertEqual(next(rl_iterator), "three")
+                    self.assertRaises(StopIteration, rl_iterator.__next__)
         finally:
             signal.signal(signal.SIGHUP, old_HUP_handler)
 
@@ -375,8 +375,8 @@ def exec_readlines_auto_kill_test(self):
                 proc = rl_iterator._proc
 
                 # Read two lines worth
-                self.assertEqual(rl_iterator.next(), "hey")
-                self.assertEqual(rl_iterator.next(), "hey")
+                self.assertEqual(next(rl_iterator), "hey")
+                self.assertEqual(next(rl_iterator), "hey")
 
                 # Delete the iterator and wait for the process to be killed
                 del rl_iterator


-- 
To view this commit on github, visit https://github.com/rhinstaller/anaconda/commit/470af530fafa92e6ff75ab9062c43ad29532a104


More information about the anaconda-patches mailing list