]> granicus.if.org Git - graphviz/commitdiff
abbreviate some trivial file reads and writes in Python
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Tue, 29 Mar 2022 00:07:57 +0000 (17:07 -0700)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Sun, 3 Apr 2022 03:10:43 +0000 (20:10 -0700)
ci/clang_format.py
ci/deploy.py
rtest/test_misc.py
tests/reference/test_reference.py

index f6c235245c7454d3ef15656030e2b3f0f3ca407e..260f572766f4204c47ce698a0815824d30cd51b8 100644 (file)
@@ -912,8 +912,7 @@ for source in sources.split("\x00")[:-1]:
 
   print(f"checking {source}...")
 
-  with open(source, "rt", encoding="utf-8") as f:
-    original = f.read()
+  original = Path(source).read_text(encoding="utf-8")
 
   # ask `clang-format` to style the file
   reformatted = subprocess.check_output(["clang-format", "--style=file", "--",
index fbd851e1eb81bb6eb59a7cb2a22e39bf15ac22c2..001216f14f7c6b02c9e99dba38ae684b1bfcfa6b 100644 (file)
@@ -70,9 +70,9 @@ def checksum(path: Path) -> Path:
 
   log.info(f"SHA256 summing {path}")
   check = Path(f"{path}.sha256")
-  with open(check, "wt", encoding="utf-8") as f:
-    with open(path, "rb") as data:
-      f.write(f"{hashlib.sha256(data.read()).hexdigest()}  {path}\n")
+  data = path.read_bytes()
+  check.write_text(f"{hashlib.sha256(data).hexdigest()}  {path}\n",
+                   encoding="utf-8")
   return check
 
 def is_macos_artifact(path: Path) -> bool:
@@ -133,8 +133,7 @@ def main(args: List[str]) -> int: # pylint: disable=missing-function-docstring
 
   # retrieve version name left by prior CI tasks
   log.info("reading GRAPHVIZ_VERSION")
-  with open("GRAPHVIZ_VERSION", "rt", encoding="utf-8") as f:
-    gv_version = f.read().strip()
+  gv_version = Path("GRAPHVIZ_VERSION").read_text(encoding="utf-8").strip()
   log.info(f"GRAPHVIZ_VERSION == {gv_version}")
 
   # if we were not passed an explicit version, use the one from the
index f423b82e2226c4444911fd21a70953f283c9b88d..ac8afccca4a9c17a439d1bd403b7af8553ea2848 100644 (file)
@@ -94,8 +94,7 @@ def test_xml_escape():
   with tempfile.TemporaryDirectory() as tmp:
 
     # write a dummy config.h to allow standalone compilation
-    with open(Path(tmp) / "config.h", "wt", encoding="utf-8") as _:
-      pass
+    (Path(tmp) / "config.h").write_text("", encoding="utf-8")
 
     # compile the stub to something we can run
     xml_exe = Path(tmp) / "xml.exe"
index a40970f45ed5e59f2f7eaaa4de4afcee2507d102..7c12de1c42908c2c0c97dff8665fed89a3ef4760 100644 (file)
@@ -141,12 +141,12 @@ def test_reference(src: str, format: str, reference: str):
       subprocess.check_call(["diffimg", ref, output])
     else:
       fail = False
-      with open(ref, "rt", encoding="utf-8") as a:
-        with open(output, "rt", encoding="utf-8") as b:
-          for line in difflib.unified_diff(a.read(), b.read(),
-                                           fromfile=str(ref),
-                                           tofile=str(output)):
-            sys.stderr.write(f"{line}\n")
-            fail = True
+      a = ref.read_text(encoding="utf-8")
+      b = output.read_text(encoding="utf-8")
+      for line in difflib.unified_diff(a, b,
+                                       fromfile=str(ref),
+                                       tofile=str(output)):
+        sys.stderr.write(f"{line}\n")
+        fail = True
       if fail:
         raise RuntimeError(f"diff {ref} {output} failed")