Skip to contentSkip to conversation

scripts/vmstate-static-checker: Fix deprecation warnings with latest argparse

PATCH Applied Thomas Huth 3 participants 8 messages Oct 30, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scripts/vmstate-static-checker: Fix deprecation warnings with latest argparse

From: Thomas Huth <[email protected]>

The argparse.FileType() type has been deprecated in the latest argparse version (e.g. the one from Fedora 43), now causing the test_bad_vmstate functional test to fail since there are unexpected strings in the output. Change the script to use pathlib.Path instead to fix the test_bad_vmstate test and to be prepared for the future when the deprecated FileType gets removed completely.

---
 scripts/vmstate-static-checker.py | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
scripts/vmstate-static-checker.py +7 -6
index 2335e25f94c..89b100e6cca 100755
@@ -21,6 +21,7 @@
 
 import argparse
 import json
+import pathlib
 import sys
 
 # Count the number of errors found
@@ -382,10 +383,10 @@ def main():
     help_text = "Parse JSON-formatted vmstate dumps from QEMU in files SRC and DEST.  Checks whether migration from SRC to DEST QEMU versions would break based on the VMSTATE information contained within the JSON outputs.  The JSON output is created from a QEMU invocation with the -dump-vmstate parameter and a filename argument to it.  Other parameters to QEMU do not matter, except the -M (machine type) parameter."
 
     parser = argparse.ArgumentParser(description=help_text)
-    parser.add_argument('-s', '--src', type=argparse.FileType('r'),
+    parser.add_argument('-s', '--src', type=pathlib.Path,
                         required=True,
                         help='json dump from src qemu')
-    parser.add_argument('-d', '--dest', type=argparse.FileType('r'),
+    parser.add_argument('-d', '--dest', type=pathlib.Path,
                         required=True,
                         help='json dump from dest qemu')
     parser.add_argument('--reverse', required=False, default=False,
@@ -393,10 +394,10 @@ def main():
                         help='reverse the direction')
     args = parser.parse_args()
 
-    src_data = json.load(args.src)
-    dest_data = json.load(args.dest)
-    args.src.close()
-    args.dest.close()
+    with open(args.src, 'r', encoding='utf-8') as src_fh:
+        src_data = json.load(src_fh)
+    with open(args.dest, 'r', encoding='utf-8') as dst_fh:
+        dest_data = json.load(dst_fh)
 
     if args.reverse:
         temp = src_data
Signature · 1 line
2.51.0
#1 lore raw
Quoted 9 lines · Thomas Huth

From: Thomas Huth <[email protected]>

The argparse.FileType() type has been deprecated in the latest argparse version (e.g. the one from Fedora 43), now causing the test_bad_vmstate functional test to fail since there are unexpected strings in the output. Change the script to use pathlib.Path instead to fix the test_bad_vmstate test and to be prepared for the future when the deprecated FileType gets removed completely.

Quoted 5 lines
---
 scripts/vmstate-static-checker.py | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

AFAICT, using pathlib.Path should work going back to any old python versions we would need.

Quoted 28 lines
scripts/vmstate-static-checker.py +3 -2
index 2335e25f94c..89b100e6cca 100755
@@ -21,6 +21,7 @@
 
 import argparse
 import json
+import pathlib
 import sys
 
 # Count the number of errors found
@@ -382,10 +383,10 @@ def main():
     help_text = "Parse JSON-formatted vmstate dumps from QEMU in files SRC and DEST.  Checks whether migration from SRC to DEST QEMU versions would break based on the VMSTATE information contained within the JSON outputs.  The JSON output is created from a QEMU invocation with the -dump-vmstate parameter and a filename argument to it.  Other parameters to QEMU do not matter, except the -M (machine type) parameter."
 
     parser = argparse.ArgumentParser(description=help_text)
-    parser.add_argument('-s', '--src', type=argparse.FileType('r'),
+    parser.add_argument('-s', '--src', type=pathlib.Path,
                         required=True,
                         help='json dump from src qemu')
-    parser.add_argument('-d', '--dest', type=argparse.FileType('r'),
+    parser.add_argument('-d', '--dest', type=pathlib.Path,
                         required=True,
                         help='json dump from dest qemu')
     parser.add_argument('--reverse', required=False, default=False,
@@ -393,10 +394,10 @@ def main():
                         help='reverse the direction')

scripts/vmstate-static-checker.py def main(): · line 400

Quoted 10 lines
scripts/vmstate-static-checker.py +4 -4
     args = parser.parse_args()
 
-    src_data = json.load(args.src)
-    dest_data = json.load(args.dest)
-    args.src.close()
-    args.dest.close()
+    with open(args.src, 'r', encoding='utf-8') as src_fh:
+        src_data = json.load(src_fh)
+    with open(args.dest, 'r', encoding='utf-8') as dst_fh:
+        dest_data = json.load(dst_fh)

This could be

src_data = json.load(args.src.read_text('utf-8')) dest_data = json.load(args.dest.read_text('utf-8'))

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#2 lore raw
Quoted 10 lines · Daniel P. Berrangé
Quoted 9 lines · Thomas Huth

From: Thomas Huth <[email protected]>

The argparse.FileType() type has been deprecated in the latest argparse version (e.g. the one from Fedora 43), now causing the test_bad_vmstate functional test to fail since there are unexpected strings in the output. Change the script to use pathlib.Path instead to fix the test_bad_vmstate test and to be prepared for the future when the deprecated FileType gets removed completely.

Quoted 8 lines
Quoted 5 lines
---
  scripts/vmstate-static-checker.py | 13 +++++++------
  1 file changed, 7 insertions(+), 6 deletions(-)

AFAICT, using pathlib.Path should work going back to any old python versions we would need.

According to https://docs.python.org/3/library/pathlib.html it has been added with Python 3.4, and we require at least 3.9 nowadays, so yes, this should be fine.

Quoted 15 lines

scripts/vmstate-static-checker.py def main(): · line 400

Quoted 12 lines
scripts/vmstate-static-checker.py +4 -4
@@ -393,10 +394,10 @@ def main():
                          help='reverse the direction')
      args = parser.parse_args()
 
-    src_data = json.load(args.src)
-    dest_data = json.load(args.dest)
-    args.src.close()
-    args.dest.close()
+    with open(args.src, 'r', encoding='utf-8') as src_fh:
+        src_data = json.load(src_fh)
+    with open(args.dest, 'r', encoding='utf-8') as dst_fh:
+        dest_data = json.load(dst_fh)

This could be

Quoted 2 lines

src_data = json.load(args.src.read_text('utf-8')) dest_data = json.load(args.dest.read_text('utf-8'))

Does not work, looks like the load() function cannot deal with a string:

$ scripts/vmstate-static-checker.py -s tests/data/vmstate-static-checker/dump1.json -d tests/data/vmstate-static-checker/dump2.json
Traceback (most recent call last):
  File "../scripts/vmstate-static-checker.py", line 439, in <module>
    sys.exit(main())
             ~~~~^^
  File "../scripts/vmstate-static-checker.py", line 397, in main
    src_data = json.load(args.src.read_text('utf-8'))
  File "/usr/lib64/python3.13/json/__init__.py", line 293, in load
    return loads(fp.read(),
                 ^^^^^^^
AttributeError: 'str' object has no attribute 'read'

Thomas

#3 lore raw
Quoted 10 lines · Thomas Huth
Quoted 9 lines · Daniel P. Berrangé
Quoted 8 lines · Thomas Huth

From: Thomas Huth <[email protected]>

The argparse.FileType() type has been deprecated in the latest argparse version (e.g. the one from Fedora 43), now causing the test_bad_vmstate functional test to fail since there are unexpected strings in the output. Change the script to use pathlib.Path instead to fix the test_bad_vmstate test and to be prepared for the future when the deprecated FileType gets removed completely.

...

Quoted 19 lines
Quoted 14 lines

scripts/vmstate-static-checker.py def main(): · line 399

Quoted 11 lines
scripts/vmstate-static-checker.py +4 -4
@@ -393,10 +394,10 @@ def main():
                           help='reverse the direction')
       args = parser.parse_args()
-    src_data = json.load(args.src)
-    dest_data = json.load(args.dest)
-    args.src.close()
-    args.dest.close()
+    with open(args.src, 'r', encoding='utf-8') as src_fh:
+        src_data = json.load(src_fh)
+    with open(args.dest, 'r', encoding='utf-8') as dst_fh:
+        dest_data = json.load(dst_fh)

This could be

Quoted 2 lines

  src_data = json.load(args.src.read_text('utf-8')) dest_data = json.load(args.dest.read_text('utf-8'))

Does not work, looks like the load() function cannot deal with a string:

Quoted 11 lines
$ scripts/vmstate-static-checker.py -s tests/data/vmstate-static-checker/ dump1.json -d tests/data/vmstate-static-checker/dump2.json
Traceback (most recent call last):
   File "../scripts/vmstate-static-checker.py", line 439, in <module>
     sys.exit(main())
              ~~~~^^
   File "../scripts/vmstate-static-checker.py", line 397, in main
     src_data = json.load(args.src.read_text('utf-8'))
   File "/usr/lib64/python3.13/json/__init__.py", line 293, in load
     return loads(fp.read(),
                  ^^^^^^^
AttributeError: 'str' object has no attribute 'read'

Ok, there also seems to be a json.loads() function (with "s" at the end) that seems to do the job ... but IMHO it would be better to continue using the load() function here to let the json parser decide how to read the file.

Thomas

#4 lore raw
Quoted 45 lines · Thomas Huth
Quoted 10 lines · Thomas Huth
Quoted 9 lines · Daniel P. Berrangé
Quoted 8 lines · Thomas Huth

From: Thomas Huth <[email protected]>

The argparse.FileType() type has been deprecated in the latest argparse version (e.g. the one from Fedora 43), now causing the test_bad_vmstate functional test to fail since there are unexpected strings in the output. Change the script to use pathlib.Path instead to fix the test_bad_vmstate test and to be prepared for the future when the deprecated FileType gets removed completely.

...

Quoted 19 lines
Quoted 14 lines

scripts/vmstate-static-checker.py def main(): · line 399

Quoted 11 lines
scripts/vmstate-static-checker.py +4 -4
@@ -393,10 +394,10 @@ def main():
                           help='reverse the direction')
       args = parser.parse_args()
-    src_data = json.load(args.src)
-    dest_data = json.load(args.dest)
-    args.src.close()
-    args.dest.close()
+    with open(args.src, 'r', encoding='utf-8') as src_fh:
+        src_data = json.load(src_fh)
+    with open(args.dest, 'r', encoding='utf-8') as dst_fh:
+        dest_data = json.load(dst_fh)

This could be

Quoted 2 lines

  src_data = json.load(args.src.read_text('utf-8')) dest_data = json.load(args.dest.read_text('utf-8'))

Does not work, looks like the load() function cannot deal with a string:

Quoted 13 lines
$ scripts/vmstate-static-checker.py -s
tests/data/vmstate-static-checker/ dump1.json -d
tests/data/vmstate-static-checker/dump2.json
Traceback (most recent call last):
   File "../scripts/vmstate-static-checker.py", line 439, in <module>
     sys.exit(main())
              ~~~~^^
   File "../scripts/vmstate-static-checker.py", line 397, in main
     src_data = json.load(args.src.read_text('utf-8'))
   File "/usr/lib64/python3.13/json/__init__.py", line 293, in load
     return loads(fp.read(),
                  ^^^^^^^
AttributeError: 'str' object has no attribute 'read'
Quoted 3 lines

Ok, there also seems to be a json.loads() function (with "s" at the end) that seems to do the job ... but IMHO it would be better to continue using the load() function here to let the json parser decide how to read the file.

ok, I don't mind that much

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#5 lore raw
Quoted 46 lines · Daniel P. Berrangé
Quoted 45 lines · Thomas Huth
Quoted 10 lines · Thomas Huth
Quoted 9 lines · Daniel P. Berrangé
Quoted 8 lines · Thomas Huth

From: Thomas Huth <[email protected]>

The argparse.FileType() type has been deprecated in the latest argparse version (e.g. the one from Fedora 43), now causing the test_bad_vmstate functional test to fail since there are unexpected strings in the output. Change the script to use pathlib.Path instead to fix the test_bad_vmstate test and to be prepared for the future when the deprecated FileType gets removed completely.

...

Quoted 32 lines
Quoted 14 lines

scripts/vmstate-static-checker.py def main(): · line 399

Quoted 11 lines
scripts/vmstate-static-checker.py +4 -4
@@ -393,10 +394,10 @@ def main():
                           help='reverse the direction')
       args = parser.parse_args()
-    src_data = json.load(args.src)
-    dest_data = json.load(args.dest)
-    args.src.close()
-    args.dest.close()
+    with open(args.src, 'r', encoding='utf-8') as src_fh:
+        src_data = json.load(src_fh)
+    with open(args.dest, 'r', encoding='utf-8') as dst_fh:
+        dest_data = json.load(dst_fh)

This could be

Quoted 2 lines

  src_data = json.load(args.src.read_text('utf-8')) dest_data = json.load(args.dest.read_text('utf-8'))

Does not work, looks like the load() function cannot deal with a string:

$ scripts/vmstate-static-checker.py -s
tests/data/vmstate-static-checker/ dump1.json -d
tests/data/vmstate-static-checker/dump2.json
Traceback (most recent call last):
    File "../scripts/vmstate-static-checker.py", line 439, in <module>
      sys.exit(main())
               ~~~~^^
    File "../scripts/vmstate-static-checker.py", line 397, in main
      src_data = json.load(args.src.read_text('utf-8'))
    File "/usr/lib64/python3.13/json/__init__.py", line 293, in load
      return loads(fp.read(),
                   ^^^^^^^
AttributeError: 'str' object has no attribute 'read'
Quoted 5 lines
Quoted 3 lines

Ok, there also seems to be a json.loads() function (with "s" at the end) that seems to do the job ... but IMHO it would be better to continue using the load() function here to let the json parser decide how to read the file.

ok, I don't mind that much

I'm basically thinking of situations where you have a big dump file: When using loads(), you have to completely read the file into memory first, maybe using a lot of memory. With load(), the parser could digest it bit by bit instead, and e.g. bail out early if there is a bug in the json format of the file. Well, it likely doesn't matter too much for the file sizes that we use in QEMU, but still... I think it's nicer with load().

Quoted 1 line

Thanks!

Thomas

#6 lore raw
In reply to Thomas Huth
Quoted 43 lines · Thomas Huth
Quoted 10 lines · Daniel P. Berrangé
Quoted 9 lines · Thomas Huth

From: Thomas Huth <[email protected]>

The argparse.FileType() type has been deprecated in the latest argparse version (e.g. the one from Fedora 43), now causing the test_bad_vmstate functional test to fail since there are unexpected strings in the output. Change the script to use pathlib.Path instead to fix the test_bad_vmstate test and to be prepared for the future when the deprecated FileType gets removed completely.

Quoted 8 lines
Quoted 5 lines
---
  scripts/vmstate-static-checker.py | 13 +++++++------
  1 file changed, 7 insertions(+), 6 deletions(-)

AFAICT, using pathlib.Path should work going back to any old python versions we would need.

According to https://docs.python.org/3/library/pathlib.html it has been added with Python 3.4, and we require at least 3.9 nowadays, so yes, this should be fine.

Quoted 14 lines

scripts/vmstate-static-checker.py def main(): · line 399

Quoted 11 lines
scripts/vmstate-static-checker.py +4 -4
@@ -393,10 +394,10 @@ def main():
                          help='reverse the direction')
      args = parser.parse_args()
-    src_data = json.load(args.src)
-    dest_data = json.load(args.dest)
-    args.src.close()
-    args.dest.close()
+    with open(args.src, 'r', encoding='utf-8') as src_fh:
+        src_data = json.load(src_fh)
+    with open(args.dest, 'r', encoding='utf-8') as dst_fh:
+        dest_data = json.load(dst_fh)

This could be

Quoted 2 lines

src_data = json.load(args.src.read_text('utf-8')) dest_data = json.load(args.dest.read_text('utf-8'))

Does not work, looks like the load() function cannot deal with a string:

Quoted 13 lines
$ scripts/vmstate-static-checker.py -s
tests/data/vmstate-static-checker/dump1.json -d
tests/data/vmstate-static-checker/dump2.json
Traceback (most recent call last):
  File "../scripts/vmstate-static-checker.py", line 439, in <module>
    sys.exit(main())
             ~~~~^^
  File "../scripts/vmstate-static-checker.py", line 397, in main
    src_data = json.load(args.src.read_text('utf-8'))
  File "/usr/lib64/python3.13/json/__init__.py", line 293, in load
    return loads(fp.read(),
                 ^^^^^^^
AttributeError: 'str' object has no attribute 'read'

Sorry, I typod - it should be 'json.loads' - note the trailing 's' for "string"

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
Applied
#7 lore raw
In reply to Thomas Huth
Quoted 11 lines · Thomas Huth

From: Thomas Huth <[email protected]>

The argparse.FileType() type has been deprecated in the latest argparse version (e.g. the one from Fedora 43), now causing the test_bad_vmstate functional test to fail since there are unexpected strings in the output. Change the script to use pathlib.Path instead to fix the test_bad_vmstate test and to be prepared for the future when the deprecated FileType gets removed completely.

queued, thanks.

Signature · 1 line
Peter Xu