From: Matthew Fernandez Date: Sun, 14 Nov 2021 18:36:15 +0000 (-0800) Subject: specify an explicit mode and UTF-8 encoding in Python open calls with text mode X-Git-Tag: 3.0.0~127^2~6 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c5cd2aa8965c84690dbf591b2c1ae2aae611e158;p=graphviz specify an explicit mode and UTF-8 encoding in Python open calls with text mode Newer versions of Pylint warn about this. --- diff --git a/ci/deploy.py b/ci/deploy.py index e561c3acb..808270a47 100644 --- a/ci/deploy.py +++ b/ci/deploy.py @@ -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 diff --git a/ci/generate_configuration_table.py b/ci/generate_configuration_table.py index c5e43accc..01887a2d2 100755 --- a/ci/generate_configuration_table.py +++ b/ci/generate_configuration_table.py @@ -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: diff --git a/rtest/generate_graph.py b/rtest/generate_graph.py index 3c0087582..96417543c 100755 --- a/rtest/generate_graph.py +++ b/rtest/generate_graph.py @@ -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 diff --git a/rtest/rtest.py b/rtest/rtest.py index ab0db2fdf..33608baf0 100755 --- a/rtest/rtest.py +++ b/rtest/rtest.py @@ -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("

\n") shutil.copyfile(FILE2, os.path.join(OUTHTML, f"old_{OUTFILE}")) fd.write(f'\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: diff --git a/rtest/test_misc.py b/rtest/test_misc.py index 0f879e237..f423b82e2 100644 --- a/rtest/test_misc.py +++ b/rtest/test_misc.py @@ -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 diff --git a/tclpkg/gv/demo/modgraph.py b/tclpkg/gv/demo/modgraph.py index a099156a2..311d50da5 100755 --- a/tclpkg/gv/demo/modgraph.py +++ b/tclpkg/gv/demo/modgraph.py @@ -9,7 +9,7 @@ author: Michael Hohn 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) diff --git a/tests/reference/test_reference.py b/tests/reference/test_reference.py index aac890a4c..a40970f45 100644 --- a/tests/reference/test_reference.py +++ b/tests/reference/test_reference.py @@ -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)): diff --git a/tests/regression_tests/regression_test_helpers.py b/tests/regression_tests/regression_test_helpers.py index be14a258d..082eea7d6 100644 --- a/tests/regression_tests/regression_test_helpers.py +++ b/tests/regression_tests/regression_test_helpers.py @@ -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.") diff --git a/tests/regression_tests/shapes/shapes.py b/tests/regression_tests/shapes/shapes.py index e87f01e13..3e9578743 100644 --- a/tests/regression_tests/shapes/shapes.py +++ b/tests/regression_tests/shapes/shapes.py @@ -102,11 +102,11 @@ def generate_shape_graph(shape, output_type): # Remove the number in "Generated by graphviz version " # 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 "