Skip to contentSkip to conversation

qmp-shell: add persistent command history

PATCH Superseded John Snow 4 participants 14 messages Mar 3, 2017 – Mar 19, 2017

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

John Snow

author
#0 lore raw

PATCH v2 1/1 qmp-shell: add persistent command history

Use the existing readline history function we are utilizing to provide persistent command history across instances of qmp-shell.

This assists entering debug commands across sessions that may be interrupted by QEMU sessions terminating, where the qmp-shell has to be relaunched.

---

v2: Adjusted the errors to whine about non-ENOENT errors, but still
intercept all errors as non-fatal.
Save history atexit() to match bash standard behavior

 scripts/qmp/qmp-shell | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
scripts/qmp/qmp-shell +19 -0
index 0373b24..55a8285 100755
@@ -70,6 +70,9 @@ import json
 import ast
 import readline
 import sys
+import os
+import errno
+import atexit
 
 class QMPCompleter(list):
     def complete(self, text, state):
@@ -109,6 +112,7 @@ class QMPShell(qmp.QEMUMonitorProtocol):
         self._pretty = pretty
         self._transmode = False
         self._actions = list()
+        self._histfile = os.path.join(os.path.expanduser('~'), '.qmp_history')
 
     def __get_address(self, arg):
         """
@@ -137,6 +141,21 @@ class QMPShell(qmp.QEMUMonitorProtocol):
         # XXX: default delimiters conflict with some command names (eg. query-),
         # clearing everything as it doesn't seem to matter
         readline.set_completer_delims('')
+        try:
+            readline.read_history_file(self._histfile)
+        except Exception as e:
+            if isinstance(e, IOError) and e.errno == errno.ENOENT:
+                # File not found. No problem.
+                pass
+            else:
+                print "Failed to read history '%s'; %s" % (self._histfile, e)
+        atexit.register(self.__save_history)
+
+    def __save_history(self):
+        try:
+            readline.write_history_file(self._histfile)
+        except Exception as e:
+            print "Failed to save history file '%s'; %s" % (self._histfile, e)
 
     def __parse_value(self, val):
         try:
Signature · 1 line
2.9.3
Reviewed-by on v2 1/1
#1 lore raw
Quoted 41 lines · John Snow

Use the existing readline history function we are utilizing to provide persistent command history across instances of qmp-shell.

This assists entering debug commands across sessions that may be interrupted by QEMU sessions terminating, where the qmp-shell has to be relaunched.

---

v2: Adjusted the errors to whine about non-ENOENT errors, but still
intercept all errors as non-fatal.
Save history atexit() to match bash standard behavior

 scripts/qmp/qmp-shell | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
scripts/qmp/qmp-shell +4 -0
index 0373b24..55a8285 100755
@@ -70,6 +70,9 @@ import json
 import ast
 import readline
 import sys
+import os
+import errno
+import atexit
 
 class QMPCompleter(list):
     def complete(self, text, state):
@@ -109,6 +112,7 @@ class QMPShell(qmp.QEMUMonitorProtocol):
         self._pretty = pretty
         self._transmode = False
         self._actions = list()
+        self._histfile = os.path.join(os.path.expanduser('~'), '.qmp_history')
 
     def __get_address(self, arg):
         """
@@ -137,6 +141,21 @@ class QMPShell(qmp.QEMUMonitorProtocol):
         # XXX: default delimiters conflict with some command names (eg. query-),

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

Quoted 10 lines
scripts/qmp/qmp-shell +8 -0
         # clearing everything as it doesn't seem to matter
         readline.set_completer_delims('')
+        try:
+            readline.read_history_file(self._histfile)
+        except Exception as e:
+            if isinstance(e, IOError) and e.errno == errno.ENOENT:
+                # File not found. No problem.
+                pass
+            else:
+                print "Failed to read history '%s'; %s" % (self._histfile, e)

I would handle only IOError, since any other error means a bug in this code or in the underlying readline library, and the best way to handle this is to let it fail loudly.

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

Quoted 10 lines
scripts/qmp/qmp-shell +7 -0
+        atexit.register(self.__save_history)
+
+    def __save_history(self):
+        try:
+            readline.write_history_file(self._histfile)
+        except Exception as e:
+            print "Failed to save history file '%s'; %s" % (self._histfile, e)
 
     def __parse_value(self, val):
         try:

But I think this is good enough and useful as is.

author
#2 lore raw
Quoted 48 lines · Nir Soffer
Quoted 47 lines · John Snow

Use the existing readline history function we are utilizing to provide persistent command history across instances of qmp-shell.

This assists entering debug commands across sessions that may be interrupted by QEMU sessions terminating, where the qmp-shell has to be relaunched.

---

v2: Adjusted the errors to whine about non-ENOENT errors, but still
intercept all errors as non-fatal.
Save history atexit() to match bash standard behavior

 scripts/qmp/qmp-shell | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
scripts/qmp/qmp-shell +8 -0
index 0373b24..55a8285 100755
@@ -70,6 +70,9 @@ import json
 import ast
 import readline
 import sys
+import os
+import errno
+import atexit
 
 class QMPCompleter(list):
     def complete(self, text, state):
@@ -109,6 +112,7 @@ class QMPShell(qmp.QEMUMonitorProtocol):
         self._pretty = pretty
         self._transmode = False
         self._actions = list()
+        self._histfile = os.path.join(os.path.expanduser('~'), '.qmp_history')
 
     def __get_address(self, arg):
         """
@@ -137,6 +141,21 @@ class QMPShell(qmp.QEMUMonitorProtocol):
         # XXX: default delimiters conflict with some command names (eg. query-),
         # clearing everything as it doesn't seem to matter
         readline.set_completer_delims('')
+        try:
+            readline.read_history_file(self._histfile)
+        except Exception as e:
+            if isinstance(e, IOError) and e.errno == errno.ENOENT:
Quoted 9 lines

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

Quoted 4 lines
scripts/qmp/qmp-shell +4 -0
+                # File not found. No problem.
+                pass
+            else:
+                print "Failed to read history '%s'; %s" % (self._histfile, e)

I would handle only IOError, since any other error means a bug in this code or in the underlying readline library, and the best way to handle this is to let it fail loudly.

Disagree. No reason to stop the shell from working because a QOL feature didn't initialize correctly.

The warning will be enough to solicit reports and fixes if necessary.

Quoted 15 lines

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

Quoted 10 lines
scripts/qmp/qmp-shell +7 -0
+        atexit.register(self.__save_history)
+
+    def __save_history(self):
+        try:
+            readline.write_history_file(self._histfile)
+        except Exception as e:
+            print "Failed to save history file '%s'; %s" % (self._histfile, e)
 
     def __parse_value(self, val):
         try:

But I think this is good enough and useful as is.

#3 lore raw
Quoted 61 lines · John Snow
Quoted 48 lines · Nir Soffer
Quoted 47 lines · John Snow

Use the existing readline history function we are utilizing to provide persistent command history across instances of qmp-shell.

This assists entering debug commands across sessions that may be interrupted by QEMU sessions terminating, where the qmp-shell has to be relaunched.

---

v2: Adjusted the errors to whine about non-ENOENT errors, but still
intercept all errors as non-fatal.
Save history atexit() to match bash standard behavior

 scripts/qmp/qmp-shell | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
scripts/qmp/qmp-shell +8 -0
index 0373b24..55a8285 100755
@@ -70,6 +70,9 @@ import json
 import ast
 import readline
 import sys
+import os
+import errno
+import atexit
 
 class QMPCompleter(list):
     def complete(self, text, state):
@@ -109,6 +112,7 @@ class QMPShell(qmp.QEMUMonitorProtocol):
         self._pretty = pretty
         self._transmode = False
         self._actions = list()
+        self._histfile = os.path.join(os.path.expanduser('~'), '.qmp_history')
 
     def __get_address(self, arg):
         """
@@ -137,6 +141,21 @@ class QMPShell(qmp.QEMUMonitorProtocol):
         # XXX: default delimiters conflict with some command names (eg. query-),
         # clearing everything as it doesn't seem to matter
         readline.set_completer_delims('')
+        try:
+            readline.read_history_file(self._histfile)
+        except Exception as e:
+            if isinstance(e, IOError) and e.errno == errno.ENOENT:
Quoted 9 lines

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

Quoted 4 lines
scripts/qmp/qmp-shell +4 -0
+                # File not found. No problem.
+                pass
+            else:
+                print "Failed to read history '%s'; %s" % (self._histfile, e)

I would handle only IOError, since any other error means a bug in this code or in the underlying readline library, and the best way to handle this is to let it fail loudly.

Quoted 4 lines

Disagree. No reason to stop the shell from working because a QOL feature didn't initialize correctly.

The warning will be enough to solicit reports and fixes if necessary.

I agree, the current solution is good tradeoff.

One thing missing, is a call to readline.set_history_length, without it the history
will grow without limit, see:
https://docs.python.org/2/library/readline.html#readline.set_history_length

Quoted 16 lines
Quoted 16 lines

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

Quoted 11 lines
scripts/qmp/qmp-shell +7 -0
+        atexit.register(self.__save_history)
+
+    def __save_history(self):
+        try:
+            readline.write_history_file(self._histfile)
+        except Exception as e:
+            print "Failed to save history file '%s'; %s" % (self._histfile, e)
 
     def __parse_value(self, val):
         try:

But I think this is good enough and useful as is.

#4 lore raw
Quoted 34 lines · Nir Soffer
Quoted 33 lines · John Snow
Quoted 30 lines · Nir Soffer
Quoted 29 lines · John Snow

Use the existing readline history function we are utilizing to provide persistent command history across instances of qmp-shell.

This assists entering debug commands across sessions that may be interrupted by QEMU sessions terminating, where the qmp-shell has to be relaunched.

---

v2: Adjusted the errors to whine about non-ENOENT errors, but still
intercept all errors as non-fatal.
Save history atexit() to match bash standard behavior

 scripts/qmp/qmp-shell | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
scripts/qmp/qmp-shell +3 -0
index 0373b24..55a8285 100755
@@ -70,6 +70,9 @@ import json
 import ast
 import readline
 import sys
+import os
+import errno
+import atexit
 
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

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

Quoted 7 lines
scripts/qmp/qmp-shell +1 -0
 class QMPCompleter(list):
     def complete(self, text, state):
@@ -109,6 +112,7 @@ class QMPShell(qmp.QEMUMonitorProtocol):
         self._pretty = pretty
         self._transmode = False
         self._actions = list()
+        self._histfile = os.path.join(os.path.expanduser('~'), '.qmp_history')

I selfishly object to this filename, because I'm using it with

    $ socat UNIX:/work/armbru/images/test-qmp READLINE,history=$HOME/.qmp_history,prompt='QMP> '

Just kidding. But seriously, shouldn't this be named after the *application* (qmp-shell) rather than the protocol (qmp)?

Quoted 21 lines
Quoted 21 lines
Quoted 20 lines
Quoted 5 lines
scripts/qmp/qmp-shell +0 -0
     def __get_address(self, arg):
         """
@@ -137,6 +141,21 @@ class QMPShell(qmp.QEMUMonitorProtocol):
         # XXX: default delimiters conflict with some command names (eg. query-),

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

Quoted 10 lines
scripts/qmp/qmp-shell +8 -0
         # clearing everything as it doesn't seem to matter
         readline.set_completer_delims('')
+        try:
+            readline.read_history_file(self._histfile)
+        except Exception as e:
+            if isinstance(e, IOError) and e.errno == errno.ENOENT:
+                # File not found. No problem.
+                pass
+            else:
+                print "Failed to read history '%s'; %s" % (self._histfile, e)

I would handle only IOError, since any other error means a bug in this code or in the underlying readline library, and the best way to handle this is to let it fail loudly.

Quoted 6 lines
Quoted 4 lines

Disagree. No reason to stop the shell from working because a QOL feature didn't initialize correctly.

The warning will be enough to solicit reports and fixes if necessary.

I agree, the current solution is good tradeoff.

For what it's worth, bash seems to silently ignore a history file it can't read. Tested by running "HISTFILE=xxx bash", then chmod 0 xxx, da capo.

Quoted 4 lines

One thing missing, is a call to readline.set_history_length, without it the history
will grow without limit, see:
https://docs.python.org/2/library/readline.html#readline.set_history_length

Should this be addressed for 2.9?

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines

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

Quoted 10 lines
scripts/qmp/qmp-shell +7 -0
+        atexit.register(self.__save_history)
+
+    def __save_history(self):
+        try:
+            readline.write_history_file(self._histfile)
+        except Exception as e:
+            print "Failed to save history file '%s'; %s" % (self._histfile, e)
 
     def __parse_value(self, val):
         try:

But I think this is good enough and useful as is.

#5 lore raw
Quoted 3 lines · Markus Armbruster
Quoted 1 line · Nir Soffer

On Fri, Mar 3, 2017 at 9:29 PM, John Snow <[email protected]> wrote:

[...]

Quoted 10 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

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

Quoted 7 lines

class QMPCompleter(list):
def complete(self, text, state):

scripts/qmp/qmp-shell +1 -0
@@ -109,6 +112,7 @@ class QMPShell(qmp.QEMUMonitorProtocol):
         self._pretty = pretty
         self._transmode = False
         self._actions = list()
+        self._histfile = os.path.join(os.path.expanduser('~'), '.qmp_history')

I selfishly object to this filename, because I'm using it with

Quoted 4 lines
    $ socat UNIX:/work/armbru/images/test-qmp READLINE,history=$HOME/.qmp_history,prompt='QMP> '

Just kidding. But seriously, shouldn't this be named after the *application* (qmp-shell) rather than the protocol (qmp)?

FWIW, that sounds reasonable to me. It's clearer that way -- the raw QMP input history, when using via socat as above and the 'qmp-shell' history won't get mixed up.

[...]

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

The warning will be enough to solicit reports and fixes if necessary.

I agree, the current solution is good tradeoff.

For what it's worth, bash seems to silently ignore a history file it can't read. Tested by running "HISTFILE=xxx bash", then chmod 0 xxx, da capo.

Quoted 6 lines
Quoted 4 lines

One thing missing, is a call to readline.set_history_length, without it the history
will grow without limit, see:
https://docs.python.org/2/library/readline.html#readline.set_history_length

Should this be addressed for 2.9?

Sounds like a nice-to-have for me.

Quoted 16 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines

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

Quoted 10 lines
scripts/qmp/qmp-shell +7 -0
+        atexit.register(self.__save_history)
+
+    def __save_history(self):
+        try:
+            readline.write_history_file(self._histfile)
+        except Exception as e:
+            print "Failed to save history file '%s'; %s" % (self._histfile, e)
 
     def __parse_value(self, val):
         try:

But I think this is good enough and useful as is.

Signature · 1 line
/kashyap
author
#6 lore raw
In reply to Markus Armbruster
Quoted 46 lines · Markus Armbruster
Quoted 34 lines · Nir Soffer
Quoted 33 lines · John Snow
Quoted 30 lines · Nir Soffer
Quoted 29 lines · John Snow

Use the existing readline history function we are utilizing to provide persistent command history across instances of qmp-shell.

This assists entering debug commands across sessions that may be interrupted by QEMU sessions terminating, where the qmp-shell has to be relaunched.

---

v2: Adjusted the errors to whine about non-ENOENT errors, but still
intercept all errors as non-fatal.
Save history atexit() to match bash standard behavior

 scripts/qmp/qmp-shell | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
scripts/qmp/qmp-shell +3 -0
index 0373b24..55a8285 100755
@@ -70,6 +70,9 @@ import json
 import ast
 import readline
 import sys
+import os
+import errno
+import atexit
 
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

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

Quoted 7 lines
scripts/qmp/qmp-shell +1 -0
 class QMPCompleter(list):
     def complete(self, text, state):
@@ -109,6 +112,7 @@ class QMPShell(qmp.QEMUMonitorProtocol):
         self._pretty = pretty
         self._transmode = False
         self._actions = list()
+        self._histfile = os.path.join(os.path.expanduser('~'), '.qmp_history')

I selfishly object to this filename, because I'm using it with

Quoted 5 lines
    $ socat UNIX:/work/armbru/images/test-qmp READLINE,history=$HOME/.qmp_history,prompt='QMP> '

Just kidding. But seriously, shouldn't this be named after the *application* (qmp-shell) rather than the protocol (qmp)?

Seeing as the history itself is the qmp-shell syntax, you are correct.

(Hey, it would be interesting to store the generated QMP into the qmp_history, though...!)

Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 11 lines
Quoted 11 lines
scripts/qmp/qmp-shell +4 -0
     def __get_address(self, arg):
         """
@@ -137,6 +141,21 @@ class QMPShell(qmp.QEMUMonitorProtocol):
         # XXX: default delimiters conflict with some command names (eg. query-),
         # clearing everything as it doesn't seem to matter
         readline.set_completer_delims('')
+        try:
+            readline.read_history_file(self._histfile)
+        except Exception as e:
+            if isinstance(e, IOError) and e.errno == errno.ENOENT:
Quoted 9 lines

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

Quoted 4 lines
scripts/qmp/qmp-shell +4 -0
+                # File not found. No problem.
+                pass
+            else:
+                print "Failed to read history '%s'; %s" % (self._histfile, e)

I would handle only IOError, since any other error means a bug in this code or in the underlying readline library, and the best way to handle this is to let it fail loudly.

Disagree. No reason to stop the shell from working because a QOL feature didn't initialize correctly.

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

The warning will be enough to solicit reports and fixes if necessary.

I agree, the current solution is good tradeoff.

For what it's worth, bash seems to silently ignore a history file it can't read. Tested by running "HISTFILE=xxx bash", then chmod 0 xxx, da capo.

Right, done by example.

Quoted 7 lines
Quoted 4 lines

One thing missing, is a call to readline.set_history_length, without it the history
will grow without limit, see:
https://docs.python.org/2/library/readline.html#readline.set_history_length

Should this be addressed for 2.9?

You can add a limit if you want to; I don't have suggestions for which completely arbitrary limit makes sense, so I left it blank intentionally.

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines

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

Quoted 10 lines
scripts/qmp/qmp-shell +7 -0
+        atexit.register(self.__save_history)
+
+    def __save_history(self):
+        try:
+            readline.write_history_file(self._histfile)
+        except Exception as e:
+            print "Failed to save history file '%s'; %s" % (self._histfile, e)
 
     def __parse_value(self, val):
         try:

But I think this is good enough and useful as is.

#7 lore raw
Quoted 49 lines · John Snow
Quoted 48 lines · Markus Armbruster
Quoted 34 lines · Nir Soffer
Quoted 33 lines · John Snow
Quoted 30 lines · Nir Soffer
Quoted 29 lines · John Snow

Use the existing readline history function we are utilizing to provide persistent command history across instances of qmp-shell.

This assists entering debug commands across sessions that may be interrupted by QEMU sessions terminating, where the qmp-shell has to be relaunched.

---

v2: Adjusted the errors to whine about non-ENOENT errors, but still
intercept all errors as non-fatal.
Save history atexit() to match bash standard behavior

 scripts/qmp/qmp-shell | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
scripts/qmp/qmp-shell +3 -0
index 0373b24..55a8285 100755
@@ -70,6 +70,9 @@ import json
 import ast
 import readline
 import sys
+import os
+import errno
+import atexit
 
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

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

Quoted 7 lines
scripts/qmp/qmp-shell +1 -0
 class QMPCompleter(list):
     def complete(self, text, state):
@@ -109,6 +112,7 @@ class QMPShell(qmp.QEMUMonitorProtocol):
         self._pretty = pretty
         self._transmode = False
         self._actions = list()
+        self._histfile = os.path.join(os.path.expanduser('~'), '.qmp_history')

I selfishly object to this filename, because I'm using it with

    $ socat UNIX:/work/armbru/images/test-qmp READLINE,history=$HOME/.qmp_history,prompt='QMP> '
Quoted 8 lines
Quoted 3 lines

Just kidding. But seriously, shouldn't this be named after the *application* (qmp-shell) rather than the protocol (qmp)?

Seeing as the history itself is the qmp-shell syntax, you are correct.

(Hey, it would be interesting to store the generated QMP into the qmp_history, though...!)

Hah! Saving it would be easy enough, but reloading it... okay, call it a "backup" and declare victory when saving works.

Quoted 40 lines
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 11 lines
Quoted 11 lines
scripts/qmp/qmp-shell +4 -0
     def __get_address(self, arg):
         """
@@ -137,6 +141,21 @@ class QMPShell(qmp.QEMUMonitorProtocol):
         # XXX: default delimiters conflict with some command names (eg. query-),
         # clearing everything as it doesn't seem to matter
         readline.set_completer_delims('')
+        try:
+            readline.read_history_file(self._histfile)
+        except Exception as e:
+            if isinstance(e, IOError) and e.errno == errno.ENOENT:
Quoted 9 lines

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

Quoted 4 lines
scripts/qmp/qmp-shell +4 -0
+                # File not found. No problem.
+                pass
+            else:
+                print "Failed to read history '%s'; %s" % (self._histfile, e)

I would handle only IOError, since any other error means a bug in this code or in the underlying readline library, and the best way to handle this is to let it fail loudly.

Disagree. No reason to stop the shell from working because a QOL feature didn't initialize correctly.

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

The warning will be enough to solicit reports and fixes if necessary.

I agree, the current solution is good tradeoff.

For what it's worth, bash seems to silently ignore a history file it can't read. Tested by running "HISTFILE=xxx bash", then chmod 0 xxx, da capo.

Right, done by example.

Quoted 5 lines
Quoted 4 lines

One thing missing, is a call to readline.set_history_length, without it the history
will grow without limit, see:
https://docs.python.org/2/library/readline.html#readline.set_history_length

Quoted 5 lines
Quoted 2 lines

Should this be addressed for 2.9?

You can add a limit if you want to; I don't have suggestions for which completely arbitrary limit makes sense, so I left it blank intentionally.

For what it's worth, bash defaults HISTSIZE to 500.

GNU Readline lets users configure it in ~/.inputrc. Conditional configuration is possible, i.e. something like

$if Qmp-shell
set history-size 5000
$endif

should work, provided qmp-shell sets rl_readline_name as it should.

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines

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

Quoted 10 lines
scripts/qmp/qmp-shell +7 -0
+        atexit.register(self.__save_history)
+
+    def __save_history(self):
+        try:
+            readline.write_history_file(self._histfile)
+        except Exception as e:
+            print "Failed to save history file '%s'; %s" % (self._histfile, e)
 
     def __parse_value(self, val):
         try:

But I think this is good enough and useful as is.

author
#8 lore raw
Quoted 111 lines · Markus Armbruster
Quoted 49 lines · John Snow
Quoted 48 lines · Markus Armbruster
Quoted 34 lines · Nir Soffer
Quoted 33 lines · John Snow
Quoted 30 lines · Nir Soffer
Quoted 29 lines · John Snow

Use the existing readline history function we are utilizing to provide persistent command history across instances of qmp-shell.

This assists entering debug commands across sessions that may be interrupted by QEMU sessions terminating, where the qmp-shell has to be relaunched.

---

v2: Adjusted the errors to whine about non-ENOENT errors, but still
intercept all errors as non-fatal.
Save history atexit() to match bash standard behavior

 scripts/qmp/qmp-shell | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
scripts/qmp/qmp-shell +3 -0
index 0373b24..55a8285 100755
@@ -70,6 +70,9 @@ import json
 import ast
 import readline
 import sys
+import os
+import errno
+import atexit
 
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

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

Quoted 7 lines
scripts/qmp/qmp-shell +1 -0
 class QMPCompleter(list):
     def complete(self, text, state):
@@ -109,6 +112,7 @@ class QMPShell(qmp.QEMUMonitorProtocol):
         self._pretty = pretty
         self._transmode = False
         self._actions = list()
+        self._histfile = os.path.join(os.path.expanduser('~'), '.qmp_history')

I selfishly object to this filename, because I'm using it with

    $ socat UNIX:/work/armbru/images/test-qmp READLINE,history=$HOME/.qmp_history,prompt='QMP> '
Quoted 8 lines
Quoted 3 lines

Just kidding. But seriously, shouldn't this be named after the *application* (qmp-shell) rather than the protocol (qmp)?

Seeing as the history itself is the qmp-shell syntax, you are correct.

(Hey, it would be interesting to store the generated QMP into the qmp_history, though...!)

Hah! Saving it would be easy enough, but reloading it... okay, call it a "backup" and declare victory when saving works.

Quoted 40 lines
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 11 lines
Quoted 11 lines
scripts/qmp/qmp-shell +4 -0
     def __get_address(self, arg):
         """
@@ -137,6 +141,21 @@ class QMPShell(qmp.QEMUMonitorProtocol):
         # XXX: default delimiters conflict with some command names (eg. query-),
         # clearing everything as it doesn't seem to matter
         readline.set_completer_delims('')
+        try:
+            readline.read_history_file(self._histfile)
+        except Exception as e:
+            if isinstance(e, IOError) and e.errno == errno.ENOENT:
Quoted 9 lines

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

Quoted 4 lines
scripts/qmp/qmp-shell +4 -0
+                # File not found. No problem.
+                pass
+            else:
+                print "Failed to read history '%s'; %s" % (self._histfile, e)

I would handle only IOError, since any other error means a bug in this code or in the underlying readline library, and the best way to handle this is to let it fail loudly.

Disagree. No reason to stop the shell from working because a QOL feature didn't initialize correctly.

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

The warning will be enough to solicit reports and fixes if necessary.

I agree, the current solution is good tradeoff.

For what it's worth, bash seems to silently ignore a history file it can't read. Tested by running "HISTFILE=xxx bash", then chmod 0 xxx, da capo.

Right, done by example.

Quoted 5 lines
Quoted 4 lines

One thing missing, is a call to readline.set_history_length, without it the history
will grow without limit, see:
https://docs.python.org/2/library/readline.html#readline.set_history_length

Quoted 5 lines
Quoted 2 lines

Should this be addressed for 2.9?

You can add a limit if you want to; I don't have suggestions for which completely arbitrary limit makes sense, so I left it blank intentionally.

For what it's worth, bash defaults HISTSIZE to 500.

Quoted 9 lines

GNU Readline lets users configure it in ~/.inputrc. Conditional configuration is possible, i.e. something like

$if Qmp-shell
set history-size 5000
$endif

should work, provided qmp-shell sets rl_readline_name as it should.

There's the winning ticket!

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines

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

Quoted 10 lines
scripts/qmp/qmp-shell +7 -0
+        atexit.register(self.__save_history)
+
+    def __save_history(self):
+        try:
+            readline.write_history_file(self._histfile)
+        except Exception as e:
+            print "Failed to save history file '%s'; %s" % (self._histfile, e)
 
     def __parse_value(self, val):
         try:

But I think this is good enough and useful as is.

author
#9 lore raw
In reply to Markus Armbruster
Quoted 111 lines · Markus Armbruster
Quoted 49 lines · John Snow
Quoted 48 lines · Markus Armbruster
Quoted 34 lines · Nir Soffer
Quoted 33 lines · John Snow
Quoted 30 lines · Nir Soffer
Quoted 29 lines · John Snow

Use the existing readline history function we are utilizing to provide persistent command history across instances of qmp-shell.

This assists entering debug commands across sessions that may be interrupted by QEMU sessions terminating, where the qmp-shell has to be relaunched.

---

v2: Adjusted the errors to whine about non-ENOENT errors, but still
intercept all errors as non-fatal.
Save history atexit() to match bash standard behavior

 scripts/qmp/qmp-shell | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
scripts/qmp/qmp-shell +3 -0
index 0373b24..55a8285 100755
@@ -70,6 +70,9 @@ import json
 import ast
 import readline
 import sys
+import os
+import errno
+import atexit
 
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

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

Quoted 7 lines
scripts/qmp/qmp-shell +1 -0
 class QMPCompleter(list):
     def complete(self, text, state):
@@ -109,6 +112,7 @@ class QMPShell(qmp.QEMUMonitorProtocol):
         self._pretty = pretty
         self._transmode = False
         self._actions = list()
+        self._histfile = os.path.join(os.path.expanduser('~'), '.qmp_history')

I selfishly object to this filename, because I'm using it with

    $ socat UNIX:/work/armbru/images/test-qmp READLINE,history=$HOME/.qmp_history,prompt='QMP> '
Quoted 8 lines
Quoted 3 lines

Just kidding. But seriously, shouldn't this be named after the *application* (qmp-shell) rather than the protocol (qmp)?

Seeing as the history itself is the qmp-shell syntax, you are correct.

(Hey, it would be interesting to store the generated QMP into the qmp_history, though...!)

Hah! Saving it would be easy enough, but reloading it... okay, call it a "backup" and declare victory when saving works.

Quoted 40 lines
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 11 lines
Quoted 11 lines
scripts/qmp/qmp-shell +4 -0
     def __get_address(self, arg):
         """
@@ -137,6 +141,21 @@ class QMPShell(qmp.QEMUMonitorProtocol):
         # XXX: default delimiters conflict with some command names (eg. query-),
         # clearing everything as it doesn't seem to matter
         readline.set_completer_delims('')
+        try:
+            readline.read_history_file(self._histfile)
+        except Exception as e:
+            if isinstance(e, IOError) and e.errno == errno.ENOENT:
Quoted 9 lines

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

Quoted 4 lines
scripts/qmp/qmp-shell +4 -0
+                # File not found. No problem.
+                pass
+            else:
+                print "Failed to read history '%s'; %s" % (self._histfile, e)

I would handle only IOError, since any other error means a bug in this code or in the underlying readline library, and the best way to handle this is to let it fail loudly.

Disagree. No reason to stop the shell from working because a QOL feature didn't initialize correctly.

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

The warning will be enough to solicit reports and fixes if necessary.

I agree, the current solution is good tradeoff.

For what it's worth, bash seems to silently ignore a history file it can't read. Tested by running "HISTFILE=xxx bash", then chmod 0 xxx, da capo.

Right, done by example.

Quoted 5 lines
Quoted 4 lines

One thing missing, is a call to readline.set_history_length, without it the history
will grow without limit, see:
https://docs.python.org/2/library/readline.html#readline.set_history_length

Quoted 5 lines
Quoted 2 lines

Should this be addressed for 2.9?

You can add a limit if you want to; I don't have suggestions for which completely arbitrary limit makes sense, so I left it blank intentionally.

For what it's worth, bash defaults HISTSIZE to 500.

Quoted 9 lines

GNU Readline lets users configure it in ~/.inputrc. Conditional configuration is possible, i.e. something like

$if Qmp-shell
set history-size 5000
$endif

should work, provided qmp-shell sets rl_readline_name as it should.

Spoke too soon. I don't see a way to control this in python's readline library... I'm not very familiar with readline, is there some environment variable or something perhaps?

(It looks like python's code just hard-sets it to "python" ...)

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines

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

Quoted 10 lines
scripts/qmp/qmp-shell +7 -0
+        atexit.register(self.__save_history)
+
+    def __save_history(self):
+        try:
+            readline.write_history_file(self._histfile)
+        except Exception as e:
+            print "Failed to save history file '%s'; %s" % (self._histfile, e)
 
     def __parse_value(self, val):
         try:

But I think this is good enough and useful as is.

#10 lore raw
Quoted 119 lines · John Snow
Quoted 118 lines · Markus Armbruster
Quoted 49 lines · John Snow
Quoted 48 lines · Markus Armbruster
Quoted 34 lines · Nir Soffer
Quoted 33 lines · John Snow
Quoted 30 lines · Nir Soffer
Quoted 29 lines · John Snow

Use the existing readline history function we are utilizing to provide persistent command history across instances of qmp-shell.

This assists entering debug commands across sessions that may be interrupted by QEMU sessions terminating, where the qmp-shell has to be relaunched.

---

v2: Adjusted the errors to whine about non-ENOENT errors, but still
intercept all errors as non-fatal.
Save history atexit() to match bash standard behavior

 scripts/qmp/qmp-shell | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
scripts/qmp/qmp-shell +3 -0
index 0373b24..55a8285 100755
@@ -70,6 +70,9 @@ import json
 import ast
 import readline
 import sys
+import os
+import errno
+import atexit
 
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

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

Quoted 7 lines
scripts/qmp/qmp-shell +1 -0
 class QMPCompleter(list):
     def complete(self, text, state):
@@ -109,6 +112,7 @@ class QMPShell(qmp.QEMUMonitorProtocol):
         self._pretty = pretty
         self._transmode = False
         self._actions = list()
+        self._histfile = os.path.join(os.path.expanduser('~'), '.qmp_history')

I selfishly object to this filename, because I'm using it with

    $ socat UNIX:/work/armbru/images/test-qmp READLINE,history=$HOME/.qmp_history,prompt='QMP> '
Quoted 8 lines
Quoted 3 lines

Just kidding. But seriously, shouldn't this be named after the *application* (qmp-shell) rather than the protocol (qmp)?

Seeing as the history itself is the qmp-shell syntax, you are correct.

(Hey, it would be interesting to store the generated QMP into the qmp_history, though...!)

Hah! Saving it would be easy enough, but reloading it... okay, call it a "backup" and declare victory when saving works.

Quoted 40 lines
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 11 lines
Quoted 11 lines
scripts/qmp/qmp-shell +4 -0
     def __get_address(self, arg):
         """
@@ -137,6 +141,21 @@ class QMPShell(qmp.QEMUMonitorProtocol):
         # XXX: default delimiters conflict with some command names (eg. query-),
         # clearing everything as it doesn't seem to matter
         readline.set_completer_delims('')
+        try:
+            readline.read_history_file(self._histfile)
+        except Exception as e:
+            if isinstance(e, IOError) and e.errno == errno.ENOENT:
Quoted 9 lines

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

Quoted 4 lines
scripts/qmp/qmp-shell +4 -0
+                # File not found. No problem.
+                pass
+            else:
+                print "Failed to read history '%s'; %s" % (self._histfile, e)

I would handle only IOError, since any other error means a bug in this code or in the underlying readline library, and the best way to handle this is to let it fail loudly.

Disagree. No reason to stop the shell from working because a QOL feature didn't initialize correctly.

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

The warning will be enough to solicit reports and fixes if necessary.

I agree, the current solution is good tradeoff.

For what it's worth, bash seems to silently ignore a history file it can't read. Tested by running "HISTFILE=xxx bash", then chmod 0 xxx, da capo.

Right, done by example.

Quoted 5 lines
Quoted 4 lines

One thing missing, is a call to readline.set_history_length, without it the history
will grow without limit, see:
https://docs.python.org/2/library/readline.html#readline.set_history_length

Quoted 5 lines
Quoted 2 lines

Should this be addressed for 2.9?

You can add a limit if you want to; I don't have suggestions for which completely arbitrary limit makes sense, so I left it blank intentionally.

For what it's worth, bash defaults HISTSIZE to 500.

GNU Readline lets users configure it in ~/.inputrc. Conditional configuration is possible, i.e. something like

$if Qmp-shell
set history-size 5000
$endif

Quoted 7 lines
Quoted 2 lines

should work, provided qmp-shell sets rl_readline_name as it should.

Spoke too soon. I don't see a way to control this in python's readline library... I'm not very familiar with readline, is there some environment variable or something perhaps?
(It looks like python's code just hard-sets it to "python" ...)

How sad. If https://bugs.python.org/ didn't require a login, I would've filed a bug already.

I'm afraid all I can offer meanwhile is INPUTRC:

Any user can customize programs that use Readline by putting commands in an "inputrc" file, conventionally in his home directory. The name of this file is taken from the value of the environment variable INPUTRC'. If that variable is unset, the default is ~/.inputrc'. If that file does not exist or cannot be read, the ultimate default is `/etc/inputrc'.

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines

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

Quoted 10 lines
scripts/qmp/qmp-shell +7 -0
+        atexit.register(self.__save_history)
+
+    def __save_history(self):
+        try:
+            readline.write_history_file(self._histfile)
+        except Exception as e:
+            print "Failed to save history file '%s'; %s" % (self._histfile, e)
 
     def __parse_value(self, val):
         try:

But I think this is good enough and useful as is.

#11 lore raw
Quoted 132 lines · Markus Armbruster
Quoted 119 lines · John Snow
Quoted 118 lines · Markus Armbruster
Quoted 49 lines · John Snow
Quoted 48 lines · Markus Armbruster
Quoted 34 lines · Nir Soffer
Quoted 33 lines · John Snow
Quoted 30 lines · Nir Soffer
Quoted 29 lines · John Snow

Use the existing readline history function we are utilizing to provide persistent command history across instances of qmp-shell.

This assists entering debug commands across sessions that may be interrupted by QEMU sessions terminating, where the qmp-shell has to be relaunched.

---

v2: Adjusted the errors to whine about non-ENOENT errors, but still
intercept all errors as non-fatal.
Save history atexit() to match bash standard behavior

 scripts/qmp/qmp-shell | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
scripts/qmp/qmp-shell +3 -0
index 0373b24..55a8285 100755
@@ -70,6 +70,9 @@ import json
 import ast
 import readline
 import sys
+import os
+import errno
+import atexit
 
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

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

Quoted 7 lines
scripts/qmp/qmp-shell +1 -0
 class QMPCompleter(list):
     def complete(self, text, state):
@@ -109,6 +112,7 @@ class QMPShell(qmp.QEMUMonitorProtocol):
         self._pretty = pretty
         self._transmode = False
         self._actions = list()
+        self._histfile = os.path.join(os.path.expanduser('~'), '.qmp_history')

I selfishly object to this filename, because I'm using it with

    $ socat UNIX:/work/armbru/images/test-qmp READLINE,history=$HOME/.qmp_history,prompt='QMP> '
Quoted 8 lines
Quoted 3 lines

Just kidding. But seriously, shouldn't this be named after the *application* (qmp-shell) rather than the protocol (qmp)?

Seeing as the history itself is the qmp-shell syntax, you are correct.

(Hey, it would be interesting to store the generated QMP into the qmp_history, though...!)

Hah! Saving it would be easy enough, but reloading it... okay, call it a "backup" and declare victory when saving works.

Quoted 40 lines
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 11 lines
Quoted 11 lines
scripts/qmp/qmp-shell +4 -0
     def __get_address(self, arg):
         """
@@ -137,6 +141,21 @@ class QMPShell(qmp.QEMUMonitorProtocol):
         # XXX: default delimiters conflict with some command names (eg. query-),
         # clearing everything as it doesn't seem to matter
         readline.set_completer_delims('')
+        try:
+            readline.read_history_file(self._histfile)
+        except Exception as e:
+            if isinstance(e, IOError) and e.errno == errno.ENOENT:
Quoted 9 lines

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

Quoted 4 lines
scripts/qmp/qmp-shell +4 -0
+                # File not found. No problem.
+                pass
+            else:
+                print "Failed to read history '%s'; %s" % (self._histfile, e)

I would handle only IOError, since any other error means a bug in this code or in the underlying readline library, and the best way to handle this is to let it fail loudly.

Disagree. No reason to stop the shell from working because a QOL feature didn't initialize correctly.

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

The warning will be enough to solicit reports and fixes if necessary.

I agree, the current solution is good tradeoff.

For what it's worth, bash seems to silently ignore a history file it can't read. Tested by running "HISTFILE=xxx bash", then chmod 0 xxx, da capo.

Right, done by example.

Quoted 5 lines
Quoted 4 lines

One thing missing, is a call to readline.set_history_length, without it the history
will grow without limit, see:
https://docs.python.org/2/library/readline.html#readline.set_history_length

Quoted 5 lines
Quoted 2 lines

Should this be addressed for 2.9?

You can add a limit if you want to; I don't have suggestions for which completely arbitrary limit makes sense, so I left it blank intentionally.

For what it's worth, bash defaults HISTSIZE to 500.

GNU Readline lets users configure it in ~/.inputrc. Conditional configuration is possible, i.e. something like

$if Qmp-shell
set history-size 5000
$endif

Quoted 7 lines
Quoted 2 lines

should work, provided qmp-shell sets rl_readline_name as it should.

Spoke too soon. I don't see a way to control this in python's readline library... I'm not very familiar with readline, is there some environment variable or something perhaps?
(It looks like python's code just hard-sets it to "python" ...)

How sad. If https://bugs.python.org/ didn't require a login, I would've filed a bug already.

Quoted 9 lines

I'm afraid all I can offer meanwhile is INPUTRC:

Any user can customize programs that use Readline by putting commands in an "inputrc" file, conventionally in his home directory. The name of this file is taken from the value of the environment variable INPUTRC'. If that variable is unset, the default is ~/.inputrc'. If that file does not exist or cannot be read, the ultimate default is `/etc/inputrc'.

Having a way to limit history globally looks good enough.

Note that python readline does not report the readline limit correctly (get_history_length() returns -1), but saving history uses the limit defined in .inputrc.

Playing with this, I found a nice bug - if you set history size to N, and your history file contains 2 * N items or more, python segfaults when entering the first input line.

I'll file a python bug later.

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines

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

Quoted 10 lines
scripts/qmp/qmp-shell +7 -0
+        atexit.register(self.__save_history)
+
+    def __save_history(self):
+        try:
+            readline.write_history_file(self._histfile)
+        except Exception as e:
+            print "Failed to save history file '%s'; %s" % (self._histfile, e)
 
     def __parse_value(self, val):
         try:

But I think this is good enough and useful as is.

#12 lore raw
Quoted 143 lines · Nir Soffer
Quoted 132 lines · Markus Armbruster
Quoted 119 lines · John Snow
Quoted 118 lines · Markus Armbruster
Quoted 49 lines · John Snow
Quoted 48 lines · Markus Armbruster
Quoted 34 lines · Nir Soffer
Quoted 33 lines · John Snow
Quoted 30 lines · Nir Soffer
Quoted 29 lines · John Snow

Use the existing readline history function we are utilizing to provide persistent command history across instances of qmp-shell.

This assists entering debug commands across sessions that may be interrupted by QEMU sessions terminating, where the qmp-shell has to be relaunched.

---

v2: Adjusted the errors to whine about non-ENOENT errors, but still
intercept all errors as non-fatal.
Save history atexit() to match bash standard behavior

 scripts/qmp/qmp-shell | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
scripts/qmp/qmp-shell +3 -0
index 0373b24..55a8285 100755
@@ -70,6 +70,9 @@ import json
 import ast
 import readline
 import sys
+import os
+import errno
+import atexit
 
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

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

Quoted 7 lines
scripts/qmp/qmp-shell +1 -0
 class QMPCompleter(list):
     def complete(self, text, state):
@@ -109,6 +112,7 @@ class QMPShell(qmp.QEMUMonitorProtocol):
         self._pretty = pretty
         self._transmode = False
         self._actions = list()
+        self._histfile = os.path.join(os.path.expanduser('~'), '.qmp_history')

I selfishly object to this filename, because I'm using it with

    $ socat UNIX:/work/armbru/images/test-qmp READLINE,history=$HOME/.qmp_history,prompt='QMP> '
Quoted 8 lines
Quoted 3 lines

Just kidding. But seriously, shouldn't this be named after the *application* (qmp-shell) rather than the protocol (qmp)?

Seeing as the history itself is the qmp-shell syntax, you are correct.

(Hey, it would be interesting to store the generated QMP into the qmp_history, though...!)

Hah! Saving it would be easy enough, but reloading it... okay, call it a "backup" and declare victory when saving works.

Quoted 40 lines
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 11 lines
Quoted 11 lines
scripts/qmp/qmp-shell +4 -0
     def __get_address(self, arg):
         """
@@ -137,6 +141,21 @@ class QMPShell(qmp.QEMUMonitorProtocol):
         # XXX: default delimiters conflict with some command names (eg. query-),
         # clearing everything as it doesn't seem to matter
         readline.set_completer_delims('')
+        try:
+            readline.read_history_file(self._histfile)
+        except Exception as e:
+            if isinstance(e, IOError) and e.errno == errno.ENOENT:
Quoted 9 lines

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

Quoted 4 lines
scripts/qmp/qmp-shell +4 -0
+                # File not found. No problem.
+                pass
+            else:
+                print "Failed to read history '%s'; %s" % (self._histfile, e)

I would handle only IOError, since any other error means a bug in this code or in the underlying readline library, and the best way to handle this is to let it fail loudly.

Disagree. No reason to stop the shell from working because a QOL feature didn't initialize correctly.

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

The warning will be enough to solicit reports and fixes if necessary.

I agree, the current solution is good tradeoff.

For what it's worth, bash seems to silently ignore a history file it can't read. Tested by running "HISTFILE=xxx bash", then chmod 0 xxx, da capo.

Right, done by example.

Quoted 5 lines
Quoted 4 lines

One thing missing, is a call to readline.set_history_length, without it the history
will grow without limit, see:
https://docs.python.org/2/library/readline.html#readline.set_history_length

Quoted 5 lines
Quoted 2 lines

Should this be addressed for 2.9?

You can add a limit if you want to; I don't have suggestions for which completely arbitrary limit makes sense, so I left it blank intentionally.

For what it's worth, bash defaults HISTSIZE to 500.

GNU Readline lets users configure it in ~/.inputrc. Conditional configuration is possible, i.e. something like

$if Qmp-shell
set history-size 5000
$endif

Quoted 7 lines
Quoted 2 lines

should work, provided qmp-shell sets rl_readline_name as it should.

Spoke too soon. I don't see a way to control this in python's readline library... I'm not very familiar with readline, is there some environment variable or something perhaps?
(It looks like python's code just hard-sets it to "python" ...)

How sad. If https://bugs.python.org/ didn't require a login, I would've filed a bug already.

Quoted 9 lines

I'm afraid all I can offer meanwhile is INPUTRC:

Any user can customize programs that use Readline by putting commands in an "inputrc" file, conventionally in his home directory. The name of this file is taken from the value of the environment variable INPUTRC'. If that variable is unset, the default is ~/.inputrc'. If that file does not exist or cannot be read, the ultimate default is `/etc/inputrc'.

Quoted 1 line

Having a way to limit history globally looks good enough.

Certainly good enough for merging qmp-shell patches.

Quoted 9 lines

Note that python readline does not report the readline limit correctly (get_history_length() returns -1), but saving history uses the limit defined in .inputrc.

Playing with this, I found a nice bug - if you set history size to N, and your history file contains 2 * N items or more, python segfaults when entering the first input line.

I'll file a python bug later.

Please do.

If you could throw in a (separate) request for letting us set rl_readline_name, that would be great.

[...]

#13 lore raw
Quoted 176 lines · Markus Armbruster
Quoted 1 line · Nir Soffer

On Wed, Mar 8, 2017 at 8:29 AM, Markus Armbruster <[email protected]>

Quoted 9 lines · wrote:
Quoted 9 lines
Quoted 7 lines · John Snow
Quoted 6 lines · Markus Armbruster
Quoted 4 lines · John Snow
Quoted 3 lines · Markus Armbruster
Quoted 1 line · Nir Soffer

On Fri, Mar 3, 2017 at 9:29 PM, John Snow <[email protected]>

Quoted 4 lines · wrote:
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines · Nir Soffer
Quoted 1 line

On Fri, Mar 3, 2017 at 8:54 PM, John Snow <[email protected]>

Quoted 2 lines · wrote:
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Use the existing readline history function we are utilizing to provide persistent command history across instances of

qmp-shell.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

This assists entering debug commands across sessions that may be interrupted by QEMU sessions terminating, where the qmp-shell

has

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

to be relaunched.

---

v2: Adjusted the errors to whine about non-ENOENT errors, but

still

Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines

intercept all errors as non-fatal.
Save history atexit() to match bash standard behavior

 scripts/qmp/qmp-shell | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
scripts/qmp/qmp-shell +3 -0
index 0373b24..55a8285 100755
@@ -70,6 +70,9 @@ import json
 import ast
 import readline
 import sys
+import os
+import errno
+import atexit
 
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

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

Quoted 7 lines
scripts/qmp/qmp-shell +1 -0
 class QMPCompleter(list):
     def complete(self, text, state):
@@ -109,6 +112,7 @@ class QMPShell(qmp.QEMUMonitorProtocol):
         self._pretty = pretty
         self._transmode = False
         self._actions = list()
+        self._histfile = os.path.join(os.path.expanduser('~'),

'.qmp_history')

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

I selfishly object to this filename, because I'm using it with

    $ socat UNIX:/work/armbru/images/test-qmp

READLINE,history=$HOME/.qmp_history,prompt='QMP> '

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 4 lines

Just kidding. But seriously, shouldn't this be named after the *application* (qmp-shell) rather than the protocol (qmp)?

Seeing as the history itself is the qmp-shell syntax, you are

correct.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

(Hey, it would be interesting to store the generated QMP into the qmp_history, though...!)

Hah! Saving it would be easy enough, but reloading it... okay, call

it

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

a "backup" and declare victory when saving works.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

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

Quoted 5 lines
scripts/qmp/qmp-shell +0 -0
     def __get_address(self, arg):
         """
@@ -137,6 +141,21 @@ class QMPShell(qmp.QEMUMonitorProtocol):
         # XXX: default delimiters conflict with some command

names (eg. query-),

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

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

Quoted 6 lines
scripts/qmp/qmp-shell +4 -0
         # clearing everything as it doesn't seem to matter
         readline.set_completer_delims('')
+        try:
+            readline.read_history_file(self._histfile)
+        except Exception as e:
+            if isinstance(e, IOError) and e.errno ==

errno.ENOENT:

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

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

Quoted 4 lines
scripts/qmp/qmp-shell +4 -0
+                # File not found. No problem.
+                pass
+            else:
+                print "Failed to read history '%s'; %s" %

(self._histfile, e)

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

I would handle only IOError, since any other error means a bug

in this code

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

or in the underlying readline library, and the best way to

handle this is to

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 2 lines

let it fail loudly.

Disagree. No reason to stop the shell from working because a QOL

feature

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

didn't initialize correctly.

The warning will be enough to solicit reports and fixes if

necessary.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

I agree, the current solution is good tradeoff.

For what it's worth, bash seems to silently ignore a history file it can't read. Tested by running "HISTFILE=xxx bash", then chmod 0

xxx, da

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 2 lines

capo.

Right, done by example.

Quoted 1 line
Quoted 1 line

One thing missing, is a call to readline.set_history_length,

without

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

it the history
will grow without limit, see:

https://docs.python.org/2/library/readline.html#readline.set_history_length

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 3 lines

Should this be addressed for 2.9?

You can add a limit if you want to; I don't have suggestions for

which

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

completely arbitrary limit makes sense, so I left it blank

intentionally.

Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 12 lines

For what it's worth, bash defaults HISTSIZE to 500.

GNU Readline lets users configure it in ~/.inputrc. Conditional configuration is possible, i.e. something like

$if Qmp-shell
set history-size 5000
$endif

should work, provided qmp-shell sets rl_readline_name as it should.

Quoted 6 lines
Quoted 6 lines
Quoted 4 lines

Spoke too soon. I don't see a way to control this in python's readline library... I'm not very familiar with readline, is there some environment variable or something perhaps?
(It looks like python's code just hard-sets it to "python" ...)

How sad. If https://bugs.python.org/ didn't require a login, I

would've

Quoted 12 lines
Quoted 11 lines

filed a bug already.

I'm afraid all I can offer meanwhile is INPUTRC:

Any user can customize programs that use Readline by putting commands in an "inputrc" file, conventionally in his home directory. The name of this file is taken from the value of the environment variable INPUTRC'. If that variable is unset, the default is ~/.inputrc'. If that file does not exist or cannot be read, the ultimate default is `/etc/inputrc'.

Quoted 1 line

Having a way to limit history globally looks good enough.

Certainly good enough for merging qmp-shell patches.

Quoted 4 lines

Note that python readline does not report the readline limit correctly (get_history_length() returns -1), but saving history uses the limit defined in .inputrc.

Quoted 8 lines
Quoted 5 lines

Playing with this, I found a nice bug - if you set history size to N, and your history file contains 2 * N items or more, python segfaults when entering the first input line.

I'll file a python bug later.

Please do.

Reported here:
http://bugs.python.org/issue29854
https://github.com/python/cpython/pull/728

If you could throw in a (separate) request for letting us set

Quoted 2 lines

rl_readline_name, that would be great.

Will do.

Quoted 3 lines

[...]