]> granicus.if.org Git - graphviz/commitdiff
gvtest.py: fix: use os.remove instead of pathlib.unlink to remove a file
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Sun, 5 Sep 2021 22:56:27 +0000 (15:56 -0700)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Mon, 13 Sep 2021 14:45:59 +0000 (07:45 -0700)
There were two problems with using pathlib.unlink here:

  1. If the caller did not pass in a value for `dst`, it was assigned to the
     second return value of a `tempfile.mkstemp` call. That is, `dst` would be a
     string, not a `pathlib.Path`, and calling `.unlink()` would fail
     cryptically.

  2. The `missing_ok` parameter which this code was using was only added to
     `pathlib.unlink` in Python 3.8. The stated Python baseline for Graphviz
     currently is Python 3.6. If this code was reached with Python < 3.8 it
     would result in an exception, obscuring the original problem.

rtest/gvtest.py

index bce6b9695f319ea5b07f55410e5bc3dd4c44e7a7..dddf0b3f253e42d6c1bd8a3138d630ec94e1df12 100644 (file)
@@ -50,7 +50,10 @@ def compile_c(src: Path, cflags: List[str] = None, link: List[str] = None,
   try:
     subprocess.check_call(args)
   except subprocess.CalledProcessError:
-    dst.unlink(missing_ok=True)
+    try:
+      os.remove(dst)
+    except FileNotFoundError:
+      pass
     raise
 
   return dst