Skip to contentSkip to conversation

qmp: make qmp-shell work with python3

PATCH Superseded Igor Mammedov 3 participants 5 messages Jun 18, 2019 – Jun 20, 2019

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 qmp: make qmp-shell work with python3

python3 doesn't have raw_input(), so qmp-shell breaks. Use input() instead and override it with raw_input() if running on python2.

---
 scripts/qmp/qmp-shell | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
scripts/qmp/qmp-shell +5 -1
index 7776c7b141..8c49b39afa 100755
@@ -308,7 +308,11 @@ class QMPShell(qmp.QEMUMonitorProtocol):
         @return True if execution was ok, return False if disconnected.
         """
         try:
-            cmdline = raw_input(prompt)
+            try: # attempt to set Python2 override
+               import __builtin__;
+               getattr(__builtin__, 'raw_input', input)
+            except ModuleNotFoundError: pass
+            cmdline = input(prompt)
         except EOFError:
             print()
             return False
Signature · 1 line
2.18.1
#1 lore raw
Quoted 14 lines · Igor Mammedov

python3 doesn't have raw_input(), so qmp-shell breaks. Use input() instead and override it with raw_input() if running on python2.

---
 scripts/qmp/qmp-shell | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
scripts/qmp/qmp-shell +0 -0
index 7776c7b141..8c49b39afa 100755

scripts/qmp/qmp-shell class QMPShell(qmp.QEMUMonitorProtocol): · line 314

Quoted 8 lines
scripts/qmp/qmp-shell +4 -1
@@ -308,7 +308,11 @@ class QMPShell(qmp.QEMUMonitorProtocol):
         @return True if execution was ok, return False if disconnected.
         """
         try:
-            cmdline = raw_input(prompt)
+            try: # attempt to set Python2 override
+               import __builtin__;
+               getattr(__builtin__, 'raw_input', input)
+            except ModuleNotFoundError: pass

Something like:

if sys.version_info[0] == 2:
input = raw_input

Also does the job, and may be considered simpler and easier to read.

- Cleber.

Quoted 7 lines
scripts/qmp/qmp-shell +1 -0
+            cmdline = input(prompt)
         except EOFError:
             print()
             return False
Signature · 1 line
2.18.1
#2 lore raw
Quoted 26 lines · On Wed, 19 Jun 2019 09:29:24 -0400 Cleber Rosa <[email protected]> wrote:
Quoted 14 lines · Igor Mammedov

python3 doesn't have raw_input(), so qmp-shell breaks. Use input() instead and override it with raw_input() if running on python2.

---
 scripts/qmp/qmp-shell | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
scripts/qmp/qmp-shell +0 -0
index 7776c7b141..8c49b39afa 100755

scripts/qmp/qmp-shell class QMPShell(qmp.QEMUMonitorProtocol): · line 314

Quoted 8 lines
scripts/qmp/qmp-shell +4 -1
@@ -308,7 +308,11 @@ class QMPShell(qmp.QEMUMonitorProtocol):
         @return True if execution was ok, return False if disconnected.
         """
         try:
-            cmdline = raw_input(prompt)
+            try: # attempt to set Python2 override
+               import __builtin__;
+               getattr(__builtin__, 'raw_input', input)
+            except ModuleNotFoundError: pass  

Something like:

Quoted 4 lines

if sys.version_info[0] == 2:
input = raw_input

Also does the job, and may be considered simpler and easier to read.

that causes scope issues on python3

Quoted 11 lines

- Cleber.

Quoted 7 lines
scripts/qmp/qmp-shell +1 -0
+            cmdline = input(prompt)
         except EOFError:
             print()
             return False
Signature · 1 line
2.18.1
#3 lore raw
Quoted 29 lines · Igor Mammedov
Quoted 26 lines · On Wed, 19 Jun 2019 09:29:24 -0400 Cleber Rosa <[email protected]> wrote:
Quoted 14 lines · Igor Mammedov

python3 doesn't have raw_input(), so qmp-shell breaks. Use input() instead and override it with raw_input() if running on python2.

---
 scripts/qmp/qmp-shell | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
scripts/qmp/qmp-shell +0 -0
index 7776c7b141..8c49b39afa 100755

scripts/qmp/qmp-shell class QMPShell(qmp.QEMUMonitorProtocol): · line 314

Quoted 8 lines
scripts/qmp/qmp-shell +4 -1
@@ -308,7 +308,11 @@ class QMPShell(qmp.QEMUMonitorProtocol):
         @return True if execution was ok, return False if disconnected.
         """
         try:
-            cmdline = raw_input(prompt)
+            try: # attempt to set Python2 override
+               import __builtin__;
+               getattr(__builtin__, 'raw_input', input)
+            except ModuleNotFoundError: pass  

Something like:

Quoted 6 lines
Quoted 4 lines

if sys.version_info[0] == 2:
input = raw_input

Also does the job, and may be considered simpler and easier to read.

that causes scope issues on python3

Which issues? It should work if you set it at the module top level. We had done this before in other scripts. e.g.:

$ git grep -1 'range = xrange'
tests/qemu-iotests/044-if sys.version_info.major == 2:
tests/qemu-iotests/044:    range = xrange
tests/qemu-iotests/044-
--
tests/qemu-iotests/163-if sys.version_info.major == 2:
tests/qemu-iotests/163:    range = xrange
tests/qemu-iotests/163-
Signature · 1 line
Eduardo
#4 lore raw
Quoted 33 lines · On Thu, 20 Jun 2019 10:45:24 -0300 Eduardo Habkost <[email protected]> wrote:
Quoted 32 lines · Igor Mammedov
Quoted 29 lines · On Wed, 19 Jun 2019 09:29:24 -0400 Cleber Rosa <[email protected]> wrote:
Quoted 14 lines · Igor Mammedov

python3 doesn't have raw_input(), so qmp-shell breaks. Use input() instead and override it with raw_input() if running on python2.

---
 scripts/qmp/qmp-shell | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
scripts/qmp/qmp-shell +0 -0
index 7776c7b141..8c49b39afa 100755

scripts/qmp/qmp-shell class QMPShell(qmp.QEMUMonitorProtocol): · line 314

Quoted 8 lines
scripts/qmp/qmp-shell +4 -1
@@ -308,7 +308,11 @@ class QMPShell(qmp.QEMUMonitorProtocol):
         @return True if execution was ok, return False if disconnected.
         """
         try:
-            cmdline = raw_input(prompt)
+            try: # attempt to set Python2 override
+               import __builtin__;
+               getattr(__builtin__, 'raw_input', input)
+            except ModuleNotFoundError: pass    

Something like:

if sys.version_info[0] == 2:
input = raw_input

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Also does the job, and may be considered simpler and easier to read.

that causes scope issues on python3

Which issues? It should work if you set it at the module top level. We had done this before in other scripts. e.g.:

it works if it is moved to the top, I'll post v2

Quoted 10 lines
$ git grep -1 'range = xrange'
tests/qemu-iotests/044-if sys.version_info.major == 2:
tests/qemu-iotests/044:    range = xrange
tests/qemu-iotests/044-
--
tests/qemu-iotests/163-if sys.version_info.major == 2:
tests/qemu-iotests/163:    range = xrange
tests/qemu-iotests/163-