[2/4] pygrub/grub: always use integer for default entry
diff mbox series

Message ID 20190401103238.15649-3-wei.liu2@citrix.com
State New, archived
Headers show
Series
  • More python fixes
Related show

Commit Message

Wei Liu April 1, 2019, 10:32 a.m. UTC
The original code set the default to either a string or an integer
(0) and relies on a Python 2 specific behaviour to work (integer is
allowed to be compared to string in Python 2 but not 3).

Always use integer. The caller (pygrub) already has code to handle
that.

Reported-by: M A Young <m.a.young@durham.ac.uk>
Signed-off-by: Wei Liu <wei.liu2@citrix.com>
---
 tools/pygrub/src/GrubConf.py | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

Comments

Andrew Cooper April 1, 2019, 4:20 p.m. UTC | #1
On 01/04/2019 11:32, Wei Liu wrote:
> The original code set the default to either a string or an integer
> (0) and relies on a Python 2 specific behaviour to work (integer is
> allowed to be compared to string in Python 2 but not 3).
>
> Always use integer. The caller (pygrub) already has code to handle
> that.
>
> Reported-by: M A Young <m.a.young@durham.ac.uk>
> Signed-off-by: Wei Liu <wei.liu2@citrix.com>

Acked-by: Andrew Cooper <andrew.cooper3@citrix.com>

Patch
diff mbox series

diff --git a/tools/pygrub/src/GrubConf.py b/tools/pygrub/src/GrubConf.py
index 0204d410ac..594139bac7 100644
--- a/tools/pygrub/src/GrubConf.py
+++ b/tools/pygrub/src/GrubConf.py
@@ -233,7 +233,11 @@  class _GrubConfigFile(object):
         if val == "saved":
             self._default = 0
         else:
-            self._default = val
+            try:
+                self._default = int(val)
+            except ValueError:
+                logging.warning("Invalid value %s, setting default to 0" %(val,))
+                self._default = 0
 
         if self._default < 0:
             raise ValueError("default must be non-negative number")