Skip to contentSkip to conversation

scripts/qmp/qom-set: Allow setting integer value

PATCH Superseded Jonatan Pålsson 2 participants 4 messages Oct 2, 2020

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scripts/qmp/qom-set: Allow setting integer value

If the value appears to be an integer, parse it as such.

This allows the following:

qmp/qom-set -s ~/qmp.sock sensor.temperature 20000

.. where sensor is a tmp105 device, and temperature is an integer property.

---
 scripts/qmp/qom-set | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
scripts/qmp/qom-set +4 -1
index 240a78187f..61920680eb 100755
@@ -56,7 +56,10 @@ if len(args) > 1:
         path, prop = args[0].rsplit('.', 1)
     except:
         usage_error("invalid format for path/property/value")
-    value = args[1]
+    try:
+        value = int(args[1])
+    except:
+        value = args[1]
 else:
     usage_error("not enough arguments")
 
Signature · 1 line
2.26.1
#1 lore raw
Quoted 19 lines · Jonatan Pålsson

If the value appears to be an integer, parse it as such.

This allows the following:

qmp/qom-set -s ~/qmp.sock sensor.temperature 20000

.. where sensor is a tmp105 device, and temperature is an integer property.

---
  scripts/qmp/qom-set | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
scripts/qmp/qom-set +0 -0
index 240a78187f..61920680eb 100755

scripts/qmp/qom-set if len(args) > 1: · line 62

Quoted 8 lines
scripts/qmp/qom-set +4 -1
@@ -56,7 +56,10 @@ if len(args) > 1:
          path, prop = args[0].rsplit('.', 1)
      except:
          usage_error("invalid format for path/property/value")
-    value = args[1]
+    try:
+        value = int(args[1])
+    except:
+        value = args[1]

Please catch the ValueError explicitly.

scripts/qmp/qom-set if len(args) > 1: · line 65

Quoted 4 lines
scripts/qmp/qom-set +0 -0
  else:
      usage_error("not enough arguments")
 

What happens when you don't convert it to int specifically? Does something break? My understanding was that QOM received everything as a string anyway, and does its own parsing.

#2 lore raw
Quoted 16 lines · John Snow
Quoted 14 lines · Jonatan Pålsson

If the value appears to be an integer, parse it as such.

This allows the following:

qmp/qom-set -s ~/qmp.sock sensor.temperature 20000

.. where sensor is a tmp105 device, and temperature is an integer property.

---
  scripts/qmp/qom-set | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
Quoted 15 lines

scripts/qmp/qom-set if len(args) > 1: · line 62

Quoted 13 lines
scripts/qmp/qom-set +4 -1
index 240a78187f..61920680eb 100755
@@ -56,7 +56,10 @@ if len(args) > 1:
          path, prop = args[0].rsplit('.', 1)
      except:
          usage_error("invalid format for path/property/value")
-    value = args[1]
+    try:
+        value = int(args[1])
+    except:
+        value = args[1]

Please catch the ValueError explicitly.

Sure, I'll send a v2.

Quoted 9 lines

scripts/qmp/qom-set if len(args) > 1: · line 65

Quoted 5 lines
scripts/qmp/qom-set +0 -0
  else:
      usage_error("not enough arguments")
 

What happens when you don't convert it to int specifically? Does something break? My understanding was that QOM received everything as a string anyway, and does its own parsing.

With the current implementation, I see this:

scripts/qmp/qom-set -s ~/qmp.sock sensor.temperature 20000 Traceback (most recent call last):
File "scripts/qmp/qom-set", line 66, in <module>
print(srv.command('qom-set', path=path, property=prop, value=value))
File "scripts/qmp/../../python/qemu/qmp.py", line 274, in command
raise QMPResponseError(ret)
qemu.qmp.QMPResponseError: Invalid parameter type for 'temperature',
expected: integer

#3 lore raw
Quoted 47 lines · Jonatan Palsson
Quoted 16 lines · John Snow
Quoted 14 lines · Jonatan Pålsson

If the value appears to be an integer, parse it as such.

This allows the following:

qmp/qom-set -s ~/qmp.sock sensor.temperature 20000

.. where sensor is a tmp105 device, and temperature is an integer property.

---
   scripts/qmp/qom-set | 5 ++++-
   1 file changed, 4 insertions(+), 1 deletion(-)
Quoted 15 lines

scripts/qmp/qom-set if len(args) > 1: · line 62

Quoted 13 lines
scripts/qmp/qom-set +4 -1
index 240a78187f..61920680eb 100755
@@ -56,7 +56,10 @@ if len(args) > 1:
           path, prop = args[0].rsplit('.', 1)
       except:
           usage_error("invalid format for path/property/value")
-    value = args[1]
+    try:
+        value = int(args[1])
+    except:
+        value = args[1]

Please catch the ValueError explicitly.

Sure, I'll send a v2.

Quoted 9 lines

scripts/qmp/qom-set if len(args) > 1: · line 65

Quoted 5 lines
scripts/qmp/qom-set +0 -0
   else:
       usage_error("not enough arguments")
 

What happens when you don't convert it to int specifically? Does something break? My understanding was that QOM received everything as a string anyway, and does its own parsing.

With the current implementation, I see this:

Quoted 9 lines

scripts/qmp/qom-set -s ~/qmp.sock sensor.temperature 20000 Traceback (most recent call last):
File "scripts/qmp/qom-set", line 66, in <module>
print(srv.command('qom-set', path=path, property=prop, value=value))
File "scripts/qmp/../../python/qemu/qmp.py", line 274, in command
raise QMPResponseError(ret)
qemu.qmp.QMPResponseError: Invalid parameter type for 'temperature',
expected: integer

Oh, unfortunate. I hope there aren't any cases where QOM expects a string, but can accept a string where a numerical value is otherwise valid.

These are just debugging scripts, so I guess this is probably okay to just change and see if anything explodes.

(A more robust solution might actually attempt to query QEMU to get the QOM property types and judiciously apply conversions. A problem for another day?)

--js