]> granicus.if.org Git - graphviz/commitdiff
specify an explicit mode and UTF-8 encoding in Python open calls with text mode
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Sun, 14 Nov 2021 18:36:15 +0000 (10:36 -0800)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Tue, 14 Dec 2021 01:18:40 +0000 (17:18 -0800)
Newer versions of Pylint warn about this.

ci/deploy.py
ci/generate_configuration_table.py
rtest/generate_graph.py
rtest/rtest.py
rtest/test_misc.py
tclpkg/gv/demo/modgraph.py
tests/reference/test_reference.py
tests/regression_tests/regression_test_helpers.py
tests/regression_tests/shapes/shapes.py
tests/regression_tests/shapes/test_shapes.py

index e561c3acb720fffa580308588cc4e46c42f393d7..808270a472eae895a0fe5651ad477182d74a5aa9 100644 (file)
@@ -70,7 +70,7 @@ def checksum(path: Path) -> Path:
 
   log.info(f"SHA256 summing {path}")
   check = Path(f"{path}.sha256")
-  with open(check, "wt") as f:
+  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")
   return check
@@ -120,7 +120,7 @@ def main(args: List[str]) -> int: # pylint: disable=missing-function-docstring
   # echo some useful things for debugging
   log.info(f"os.uname(): {os.uname()}")
   if Path("/etc/os-release").exists():
-    with open("/etc/os-release") as f:
+    with open("/etc/os-release", "rt", encoding="utf-8") as f:
       log.info("/etc/os-release:")
       for i, line in enumerate(f, 1):
         log.info(f" {i}: {line[:-1]}")
@@ -133,7 +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") as f:
+  with open("GRAPHVIZ_VERSION", "rt", encoding="utf-8") as f:
     gv_version = f.read().strip()
   log.info(f"GRAPHVIZ_VERSION == {gv_version}")
 
@@ -247,7 +247,7 @@ def main(args: List[str]) -> int: # pylint: disable=missing-function-docstring
 
   # output JSON data for the website
   log.info(f"dumping {webdata} to graphviz-{options.version}.json")
-  with open(f"graphviz-{options.version}.json", "wt") as f:
+  with open(f"graphviz-{options.version}.json", "wt", encoding="utf-8") as f:
     json.dump(webdata, f, indent=2)
 
   return 0
index c5e43accc5b6c846454540229920d245c998cd33..01887a2d213a93740e129b6aa317c8fa2eb3165e 100755 (executable)
@@ -69,7 +69,7 @@ def main(): # pylint: disable=missing-function-docstring
     platform = f"{os_id.capitalize()} {os_version_id}"
     if platform not in platforms:
       platforms.append(platform)
-    with open(filename, "rt") as fp:
+    with open(filename, "rt", encoding="utf-8") as fp:
       for line in fp.readlines():
         item = [item.strip() for item in line.split(":")]
         if len(item) == 2:
index 3c00875828d90439300ad10fa40cd3f253c6918b..96417543cb4472d0aa9647bca6afe0e320f2dfe1 100755 (executable)
@@ -142,7 +142,7 @@ def process(args: List[str], g: Graph, timeout: Optional[int]) -> Result:
     src = Path(tmp) / "input.dot"
 
     # write our test input to a file
-    with open(src, "wt") as f:
+    with open(src, "wt", encoding="utf-8") as f:
       g.serialize(f)
 
     # process this graph
index ab0db2fdfac88e52d5b1ccd12627e7f02fd86f97..33608baf06c91ebc9657f7497fd0062b51fcd084 100755 (executable)
@@ -144,9 +144,9 @@ def doDiff(OUTFILE, testname, subtest_index, fmt):
 
     returncode = 0 if filecmp.cmp(TMPFILE1, TMPFILE2) else -1
   elif F == "svg":
-    with open(FILE1) as f:
+    with open(FILE1, "rt", encoding="utf-8") as f:
       a = re.sub(r"^<!--.*-->$", "", f.read(), flags=re.MULTILINE)
-    with open(FILE2) as f:
+    with open(FILE2, "rt", encoding="utf-8") as f:
       b = re.sub(r"^<!--.*-->$", "", f.read(), flags=re.MULTILINE)
     returncode = 0 if a.strip() == b.strip() else -1
   elif F == "png":
@@ -161,7 +161,7 @@ def doDiff(OUTFILE, testname, subtest_index, fmt):
       [DIFFIMG, FILE1, FILE2, os.path.join(OUTHTML, f"dif_{OUTFILE}")],
     )
     if returncode != 0:
-      with open(os.path.join(OUTHTML, "index.html"), mode="a") as fd:
+      with open(os.path.join(OUTHTML, "index.html"), "at", encoding="utf-8") as fd:
         fd.write("<p>\n")
         shutil.copyfile(FILE2, os.path.join(OUTHTML, f"old_{OUTFILE}"))
         fd.write(f'<img src="old_{OUTFILE}" width="192" height="192">\n')
@@ -171,8 +171,8 @@ def doDiff(OUTFILE, testname, subtest_index, fmt):
     else:
       os.unlink(os.path.join(OUTHTML, f"dif_{OUTFILE}"))
   else:
-    with open(FILE2) as a:
-      with open(FILE1) as b:
+    with open(FILE2, "rt", encoding="utf-8") as a:
+      with open(FILE1, "rt", encoding="utf-8") as b:
         returncode = 0 if a.read().strip() == b.read().strip() else -1
   if returncode != 0:
     print(f"Test {testname}:{subtest_index} : == Failed == {OUTFILE}", file=sys.stderr)
@@ -224,7 +224,7 @@ def doTest(test):
   if GRAPH == "=":
     INFILE = os.path.join(GRAPHDIR, f"{TESTNAME}.gv")
   elif GRAPH.startswith("graph") or GRAPH.startswith("digraph"):
-    with open(TMPINFILE, mode="w") as fd:
+    with open(TMPINFILE, mode="wt", encoding="utf-8") as fd:
       fd.write(GRAPH)
     INFILE = TMPINFILE
   elif os.path.splitext(GRAPH)[1] == ".gv":
@@ -412,7 +412,7 @@ if not GENERATE:
 #    sys.exit(1)
 
 
-f3 = open(TESTFILE)
+f3 = open(TESTFILE, "rt", encoding="utf-8")
 while True:
   TEST = readTest()
   if TEST is None:
index 0f879e2371c9a3386594d45bb287b8b8754e8db0..f423b82e2226c4444911fd21a70953f283c9b88d 100644 (file)
@@ -94,7 +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") as _:
+    with open(Path(tmp) / "config.h", "wt", encoding="utf-8") as _:
       pass
 
     # compile the stub to something we can run
index a099156a2bc9ec15145e5fd5719342cee795fce3..311d50da58860ce02a5cb37977b2a43d23726ee3 100755 (executable)
@@ -9,7 +9,7 @@ author: Michael Hohn <mhhohn@lbl.gov>
 
 import gv # pylint: disable=import-error
 
-modules = open("/proc/modules", "r").readlines()
+modules = open("/proc/modules", "rt", encoding="utf-8").readlines()
 
 G = gv.digraph("G")
 N = gv.protonode(G)
index aac890a4c71eabf62563b86e685cc39a6cd45f4c..a40970f45ed5e59f2f7eaaa4de4afcee2507d102 100644 (file)
@@ -141,8 +141,8 @@ def test_reference(src: str, format: str, reference: str):
       subprocess.check_call(["diffimg", ref, output])
     else:
       fail = False
-      with open(ref, 'rt') as a:
-        with open(output, 'rt') as b:
+      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)):
index be14a258d69e695b2ecd4afd5aaeed57856bf006..082eea7d6b5fdd0baf61753ba6c32851bdeb764d 100644 (file)
@@ -18,8 +18,8 @@ def compare_graphs(name, output_type):
     print(f"Failure: {filename} - No reference file present.")
     return False
 
-  with open(filename_reference) as reference_file:
-    with open(filename_output) as output_file:
+  with open(filename_reference, "rt", encoding="utf-8") as reference_file:
+    with open(filename_output, "rt", encoding="utf-8") as output_file:
       reference = reference_file.readlines()
       output = output_file.readlines()
       diff_generator = difflib.context_diff(output, reference,
@@ -43,7 +43,8 @@ def compare_graphs(name, output_type):
         print(line)
 
       # Store diff in file
-      with open(f"difference/{str(filename)}", "w") as diff_file:
+      with open(f"difference/{str(filename)}", "wt",
+                encoding="utf-8") as diff_file:
         diff_file.writelines(diff)
 
       print(f"Failure: {filename} - Generated file does not match reference file.")
index e87f01e13e0b5f30625939b0c471b03a054d1d04..3e957874335a104f63383f211ac12d8a27c8b7de 100644 (file)
@@ -102,11 +102,11 @@ def generate_shape_graph(shape, output_type):
     # Remove the number in "Generated by graphviz version <number>"
     # to able to compare the output to the reference. This version
     # number is different for every Graphviz compilation.
-    file = open(output_file, "r")
+    file = open(output_file, "rt", encoding="utf-8")
     lines = file.readlines()
     file.close()
 
-    file = open(output_file, "w")
+    file = open(output_file, "wt", encoding="utf-8")
     for line in lines:
       if "<!-- Generated by graphviz version " in line:
         file.write("<!-- Generated by graphviz version\n")
index 6aadea23b84ff18ea6f2d2b32390fb1528c8f6ec..4bced1edb8de2240da2a4d130d80c4fef10c113f 100644 (file)
@@ -104,11 +104,11 @@ def generate_shape_graph(shape, output_type):
     # Remove the number in "Generated by graphviz version <number>"
     # to able to compare the output to the reference. This version
     # number is different for every Graphviz compilation.
-    file = open(output_file, "r")
+    file = open(output_file, "rt", encoding="utf-8")
     lines = file.readlines()
     file.close()
 
-    file = open(output_file, "w")
+    file = open(output_file, "wt", encoding="utf-8")
     for line in lines:
       if "<!-- Generated by graphviz version " in line:
         file.write("<!-- Generated by graphviz version\n")