Skip to contentSkip to conversation

tests/qapi-schema: Minor tooling improvements

PATCH In review Markus Armbruster 3 participants 8 messages Sep 22, 2021 – Sep 23, 2021

lore
Share & tools
lore
#0 lore raw

cover tests/qapi-schema: Minor tooling improvements

Markus Armbruster (2):
tests/qapi-schema: Use Python OSError instead of outmoded IOError tests/qapi-schema: Make test-qapi.py -u work when files are absent

 tests/qapi-schema/test-qapi.py | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)
Signature · 1 line
2.31.1
#1 lore raw

PATCH 1/2 tests/qapi-schema: Use Python OSError instead of outmoded IOError

---
 tests/qapi-schema/test-qapi.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tests/qapi-schema/test-qapi.py +2 -2
index 73cffae2b6..2e384f5efd 100755
@@ -154,7 +154,7 @@ def test_and_diff(test_name, dir_name, update):
         errfp = open(os.path.join(dir_name, test_name + '.err'), mode)
         expected_out = outfp.readlines()
         expected_err = errfp.readlines()
-    except IOError as err:
+    except OSError as err:
         print("%s: can't open '%s': %s"
               % (sys.argv[0], err.filename, err.strerror),
               file=sys.stderr)
@@ -180,7 +180,7 @@ def test_and_diff(test_name, dir_name, update):
         errfp.truncate(0)
         errfp.seek(0)
         errfp.writelines(actual_err)
-    except IOError as err:
+    except OSError as err:
         print("%s: can't write '%s': %s"
               % (sys.argv[0], err.filename, err.strerror),
               file=sys.stderr)
Signature · 1 line
2.31.1
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] tests/qapi-schema: Use Python OSError instead of outmoded IOError

Quoted 29 lines · Markus Armbruster
---
 tests/qapi-schema/test-qapi.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tests/qapi-schema/test-qapi.py +2 -3
index 73cffae2b6..2e384f5efd 100755
@@ -154,7 +154,7 @@ def test_and_diff(test_name, dir_name, update):
         errfp = open(os.path.join(dir_name, test_name + '.err'), mode)
         expected_out = outfp.readlines()
         expected_err = errfp.readlines()
-    except IOError as err:
+    except OSError as err:
         print("%s: can't open '%s': %s"
               % (sys.argv[0], err.filename, err.strerror),
               file=sys.stderr)
@@ -180,7 +180,7 @@ def test_and_diff(test_name, dir_name, update):
         errfp.truncate(0)
         errfp.seek(0)
         errfp.writelines(actual_err)
-    except IOError as err:
+    except OSError as err:
         print("%s: can't write '%s': %s"
               % (sys.argv[0], err.filename, err.strerror),
               file=sys.stderr)
--
Quoted 3 lines

2.31.1

If you're happy with the expanded scope of the exception-catcher, I am too.

#3 lore raw

Re: [PATCH 1/2] tests/qapi-schema: Use Python OSError instead of outmoded IOError

Quoted 27 lines · John Snow
Quoted 25 lines · Markus Armbruster
---
 tests/qapi-schema/test-qapi.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tests/qapi-schema/test-qapi.py +2 -2
index 73cffae2b6..2e384f5efd 100755
@@ -154,7 +154,7 @@ def test_and_diff(test_name, dir_name, update):
         errfp = open(os.path.join(dir_name, test_name + '.err'), mode)
         expected_out = outfp.readlines()
         expected_err = errfp.readlines()
-    except IOError as err:
+    except OSError as err:
         print("%s: can't open '%s': %s"
               % (sys.argv[0], err.filename, err.strerror),
               file=sys.stderr)
@@ -180,7 +180,7 @@ def test_and_diff(test_name, dir_name, update):
         errfp.truncate(0)
         errfp.seek(0)
         errfp.writelines(actual_err)
-    except IOError as err:
+    except OSError as err:
Quoted 8 lines
Quoted 7 lines
tests/qapi-schema/test-qapi.py +0 -1
         print("%s: can't write '%s': %s"
               % (sys.argv[0], err.filename, err.strerror),
               file=sys.stderr)
--

2.31.1

If you're happy with the expanded scope of the exception-catcher, I am too.

https://docs.python.org/3.6/library/exceptions.html has

Changed in version 3.3: EnvironmentError, IOError, WindowsError, socket.error, select.error and mmap.error have been merged into OSError, and the constructor may return a subclass.

and

The following exceptions are kept for compatibility with previous versions; starting from Python 3.3, they are aliases of OSError.

exception EnvironmentError

exception IOError

exception WindowsError

Only available on Windows.

So unless I'm misunderstanding something (which is quite possible), we're catching exactly the same exceptions as before, we just switch to their preferred name.

Quoted 1 line

Thanks!

#4 lore raw

Re: [PATCH 1/2] tests/qapi-schema: Use Python OSError instead of outmoded IOError

Quoted 49 lines · Markus Armbruster
Quoted 27 lines · John Snow
Quoted 25 lines · Markus Armbruster
---
  tests/qapi-schema/test-qapi.py | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
tests/qapi-schema/test-qapi.py +2 -2
index 73cffae2b6..2e384f5efd 100755
@@ -154,7 +154,7 @@ def test_and_diff(test_name, dir_name, update):
          errfp = open(os.path.join(dir_name, test_name + '.err'), mode)
          expected_out = outfp.readlines()
          expected_err = errfp.readlines()
-    except IOError as err:
+    except OSError as err:
          print("%s: can't open '%s': %s"
                % (sys.argv[0], err.filename, err.strerror),
                file=sys.stderr)
@@ -180,7 +180,7 @@ def test_and_diff(test_name, dir_name, update):
          errfp.truncate(0)
          errfp.seek(0)
          errfp.writelines(actual_err)
-    except IOError as err:
+    except OSError as err:
Quoted 8 lines
Quoted 7 lines
tests/qapi-schema/test-qapi.py +0 -1
          print("%s: can't write '%s': %s"
                % (sys.argv[0], err.filename, err.strerror),
                file=sys.stderr)
--

2.31.1

If you're happy with the expanded scope of the exception-catcher, I am too.

https://docs.python.org/3.6/library/exceptions.html has

Changed in version 3.3: EnvironmentError, IOError, WindowsError, socket.error, select.error and mmap.error have been merged into OSError, and the constructor may return a subclass.

and

The following exceptions are kept for compatibility with previous versions; starting from Python 3.3, they are aliases of OSError.

Quoted 7 lines

exception EnvironmentError

exception IOError

exception WindowsError

Only available on Windows.

With that information amended to the description:

Quoted 9 lines

So unless I'm misunderstanding something (which is quite possible), we're catching exactly the same exceptions as before, we just switch to their preferred name.

Quoted 1 line

Thanks!

#5 lore raw

Re: [PATCH 1/2] tests/qapi-schema: Use Python OSError instead of outmoded IOError

In reply to Markus Armbruster
Quoted 59 lines · Markus Armbruster
Quoted 1 line · John Snow

On Wed, Sep 22, 2021 at 8:56 AM Markus Armbruster <[email protected]>

Quoted 26 lines · wrote:
Quoted 26 lines
---
 tests/qapi-schema/test-qapi.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tests/qapi-schema/test-qapi.py +2 -2
index 73cffae2b6..2e384f5efd 100755
@@ -154,7 +154,7 @@ def test_and_diff(test_name, dir_name, update):
         errfp = open(os.path.join(dir_name, test_name + '.err'), mode)
         expected_out = outfp.readlines()
         expected_err = errfp.readlines()
-    except IOError as err:
+    except OSError as err:
         print("%s: can't open '%s': %s"
               % (sys.argv[0], err.filename, err.strerror),
               file=sys.stderr)
@@ -180,7 +180,7 @@ def test_and_diff(test_name, dir_name, update):
         errfp.truncate(0)
         errfp.seek(0)
         errfp.writelines(actual_err)
-    except IOError as err:
+    except OSError as err:
Quoted 8 lines
Quoted 7 lines
tests/qapi-schema/test-qapi.py +0 -1
         print("%s: can't write '%s': %s"
               % (sys.argv[0], err.filename, err.strerror),
               file=sys.stderr)
--

2.31.1

If you're happy with the expanded scope of the exception-catcher, I am

too.

https://docs.python.org/3.6/library/exceptions.html has

Changed in version 3.3: EnvironmentError, IOError, WindowsError, socket.error, select.error and mmap.error have been merged into OSError, and the constructor may return a subclass.

and

The following exceptions are kept for compatibility with previous versions; starting from Python 3.3, they are aliases of OSError.

exception EnvironmentError

exception IOError

exception WindowsError

Only available on Windows.

Quoted 9 lines

So unless I'm misunderstanding something (which is quite possible), we're catching exactly the same exceptions as before, we just switch to their preferred name.

Quoted 1 line

Thanks!

Yeah, I suppose the 3.3 upgrade already "expanded" the coverage here, so you aren't expanding anything. It's just an expansion of intent in the source code, if that distinction makes sense. The code is obviously fine so far as I can tell. My RB stands!

--js

#6 lore raw

PATCH 2/2 tests/qapi-schema: Make test-qapi.py -u work when files are absent

In reply to Markus Armbruster

test-qapi.py -u updates the expected files. Since it fails when they are absent, users have to create them manually before they can use test-qapi.py to fill in the contents, say for a new test. Silly. Improve -u to create them.

---
 tests/qapi-schema/test-qapi.py | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
tests/qapi-schema/test-qapi.py +13 -3
index 2e384f5efd..c717a7a90b 100755
@@ -132,6 +132,17 @@ def test_frontend(fname):
             print('    section=%s\n%s' % (section.name, section.text))
 
 
+def open_test_result(dir_name, file_name, update):
+    mode = 'r+' if update else 'r'
+    try:
+        fp = open(os.path.join(dir_name, file_name), mode)
+    except FileNotFoundError:
+        if not update:
+            raise
+        fp = open(os.path.join(dir_name, file_name), 'w+')
+    return fp
+
+
 def test_and_diff(test_name, dir_name, update):
     sys.stdout = StringIO()
     try:
@@ -148,10 +159,9 @@ def test_and_diff(test_name, dir_name, update):
         sys.stdout.close()
         sys.stdout = sys.__stdout__
 
-    mode = 'r+' if update else 'r'
     try:
-        outfp = open(os.path.join(dir_name, test_name + '.out'), mode)
-        errfp = open(os.path.join(dir_name, test_name + '.err'), mode)
+        outfp = open_test_result(dir_name, test_name + '.out', update)
+        errfp = open_test_result(dir_name, test_name + '.err', update)
         expected_out = outfp.readlines()
         expected_err = errfp.readlines()
     except OSError as err:
Signature · 1 line
2.31.1
Reviewed-by on v1 2/2
#7 lore raw

Re: [PATCH 2/2] tests/qapi-schema: Make test-qapi.py -u work when files are absent

Quoted 47 lines · Markus Armbruster

test-qapi.py -u updates the expected files. Since it fails when they are absent, users have to create them manually before they can use test-qapi.py to fill in the contents, say for a new test. Silly. Improve -u to create them.

---
 tests/qapi-schema/test-qapi.py | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
tests/qapi-schema/test-qapi.py +13 -4
index 2e384f5efd..c717a7a90b 100755
@@ -132,6 +132,17 @@ def test_frontend(fname):
             print('    section=%s\n%s' % (section.name, section.text))
 
 
+def open_test_result(dir_name, file_name, update):
+    mode = 'r+' if update else 'r'
+    try:
+        fp = open(os.path.join(dir_name, file_name), mode)
+    except FileNotFoundError:
+        if not update:
+            raise
+        fp = open(os.path.join(dir_name, file_name), 'w+')
+    return fp
+
+
 def test_and_diff(test_name, dir_name, update):
     sys.stdout = StringIO()
     try:
@@ -148,10 +159,9 @@ def test_and_diff(test_name, dir_name, update):
         sys.stdout.close()
         sys.stdout = sys.__stdout__
 
-    mode = 'r+' if update else 'r'
     try:
-        outfp = open(os.path.join(dir_name, test_name + '.out'), mode)
-        errfp = open(os.path.join(dir_name, test_name + '.err'), mode)
+        outfp = open_test_result(dir_name, test_name + '.out', update)
+        errfp = open_test_result(dir_name, test_name + '.err', update)
         expected_out = outfp.readlines()
         expected_err = errfp.readlines()
     except OSError as err:
--
Quoted 3 lines

2.31.1

Sure boss, why not?