From 853f069103f00b3f487833787b5388c7e79ff3c3 Mon Sep 17 00:00:00 2001 From: Craig Scott Date: Fri, 7 Apr 2023 18:11:05 +1000 Subject: Sphinx: Specify encoding when opening files for title extraction When the encoding is not specified, open() may choose an encoding based on the locale in use. That encoding may have no relationship to the encoding of the file being opened. Use the locale from the document settings instead, which should better match the file's encoding. Fixes: #24679 --- Utilities/Sphinx/cmake.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Utilities/Sphinx/cmake.py b/Utilities/Sphinx/cmake.py index 47e4909..e4e06ab 100644 --- a/Utilities/Sphinx/cmake.py +++ b/Utilities/Sphinx/cmake.py @@ -224,12 +224,13 @@ class CMakeTransform(Transform): The cmake --help-*-list commands also depend on this convention. Return the title or False if the document file does not exist. """ - env = self.document.settings.env + settings = self.document.settings + env = settings.env title = self.titles.get(docname) if title is None: fname = os.path.join(env.srcdir, docname+'.rst') try: - f = open(fname, 'r') + f = open(fname, 'r', encoding=settings.input_encoding) except IOError: title = False else: -- cgit v0.12 From fc2b60ca6b23a7204043075e0c04a727d5b6a06b Mon Sep 17 00:00:00 2001 From: Craig Scott Date: Fri, 7 Apr 2023 18:14:18 +1000 Subject: Sphinx: Modernize UTF-8 encoding handling when updating CMake.qhp --- Utilities/Sphinx/create_identifiers.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Utilities/Sphinx/create_identifiers.py b/Utilities/Sphinx/create_identifiers.py index 0ff39a0..61dd819 100755 --- a/Utilities/Sphinx/create_identifiers.py +++ b/Utilities/Sphinx/create_identifiers.py @@ -6,12 +6,12 @@ if len(sys.argv) != 2: sys.exit(-1) name = sys.argv[1] + "/CMake.qhp" -f = open(name, "rb") +f = open(name, "r", encoding="utf-8") if not f: sys.exit(-1) -lines = f.read().decode("utf-8").splitlines() +lines = f.read().splitlines() if not lines: sys.exit(-1) @@ -47,5 +47,5 @@ for line in lines: line = part1 + prefix + "id=\"" + domain_object_type + "/" + domain_object + "\" " + part2 newlines.append(line + "\n") -f = open(name, "wb") -f.writelines(map(lambda line: line.encode("utf-8"), newlines)) +f = open(name, "w", encoding="utf-8") +f.writelines(newlines) -- cgit v0.12 From e4f26edc1c1bb999b12df83f41459fe7174bef29 Mon Sep 17 00:00:00 2001 From: Craig Scott Date: Fri, 7 Apr 2023 18:21:27 +1000 Subject: Tests: Always load presets schema as UTF-8 We know the encoding of the schema file, so we should specify it when we open it for reading. Previously, by not specifying it, the test was open to using an encoding based on the active locale when running the test. We may have been enforcing a "C" locale at a higher level, but we don't need to rely on that here, we can force correct behavior without that assumption. Issue: #24679 --- Tests/RunCMake/CMakePresets/validate_schema.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Tests/RunCMake/CMakePresets/validate_schema.py b/Tests/RunCMake/CMakePresets/validate_schema.py index b2a67fc..836147a 100644 --- a/Tests/RunCMake/CMakePresets/validate_schema.py +++ b/Tests/RunCMake/CMakePresets/validate_schema.py @@ -4,13 +4,13 @@ import os.path import sys -with open(sys.argv[1], "rb") as f: - contents = json.loads(f.read().decode("utf-8-sig")) +with open(sys.argv[1], "r", encoding="utf-8-sig") as f: + contents = json.load(f) schema_file = os.path.join( os.path.dirname(__file__), "..", "..", "..", "Help", "manual", "presets", "schema.json") -with open(schema_file) as f: +with open(schema_file, "r", encoding="utf-8") as f: schema = json.load(f) jsonschema.validate(contents, schema) -- cgit v0.12