|
11 | 11 |
|
12 | 12 | from __future__ import annotations |
13 | 13 |
|
| 14 | +import builtins |
14 | 15 | import copy |
| 16 | +import json |
15 | 17 | import os |
16 | 18 | import pickle |
17 | 19 | import sys |
|
32 | 34 | from tests.test_utils import TimedCall |
33 | 35 |
|
34 | 36 | _, has_tv = optional_import("torchvision", "0.8.0", min_version) |
35 | | -_, has_yaml = optional_import("yaml") |
| 37 | +yaml, has_yaml = optional_import("yaml") |
| 38 | + |
| 39 | +NON_ASCII_TEXT = "Lungenknoten ≥ 3 mm, Größe, 肺結節" |
| 40 | +_builtin_open = builtins.open |
| 41 | + |
| 42 | + |
| 43 | +def _open_with_cp1252_default(file, mode="r", *args, **kwargs): |
| 44 | + """``open`` as on a system whose default encoding is cp1252 (e.g. Windows).""" |
| 45 | + if "b" not in mode: |
| 46 | + kwargs.setdefault("encoding", "cp1252") |
| 47 | + return _builtin_open(file, mode, *args, **kwargs) |
36 | 48 |
|
37 | 49 |
|
38 | 50 | @TimedCall(seconds=100, force_quit=True) |
@@ -524,5 +536,45 @@ def test_missing_raises(self): |
524 | 536 | _ = self.parser.A.nonexistent |
525 | 537 |
|
526 | 538 |
|
| 539 | +class TestConfigFileEncoding(unittest.TestCase): |
| 540 | + """Config files are UTF-8, whatever the default encoding of the system.""" |
| 541 | + |
| 542 | + @parameterized.expand([("json",), ("yaml",)]) |
| 543 | + def test_load_utf8_config(self, fmt): |
| 544 | + if fmt == "yaml" and not has_yaml: |
| 545 | + self.skipTest("Requires pyyaml") |
| 546 | + with tempfile.TemporaryDirectory() as tempdir: |
| 547 | + filepath = Path(tempdir) / f"config.{fmt}" |
| 548 | + content = json.dumps({"note": NON_ASCII_TEXT}, ensure_ascii=False) |
| 549 | + filepath.write_bytes(content.encode("utf-8")) |
| 550 | + with mock.patch("builtins.open", _open_with_cp1252_default): |
| 551 | + config = ConfigParser.load_config_file(filepath) |
| 552 | + self.assertEqual(config["note"], NON_ASCII_TEXT) |
| 553 | + |
| 554 | + def test_export_utf8_json(self): |
| 555 | + with tempfile.TemporaryDirectory() as tempdir: |
| 556 | + filepath = Path(tempdir) / "config.json" |
| 557 | + with mock.patch("builtins.open", _open_with_cp1252_default): |
| 558 | + ConfigParser.export_config_file({"note": NON_ASCII_TEXT}, filepath, ensure_ascii=False) |
| 559 | + self.assertEqual(json.loads(filepath.read_bytes().decode("utf-8")), {"note": NON_ASCII_TEXT}) |
| 560 | + |
| 561 | + @skipUnless(has_yaml, "Requires pyyaml") |
| 562 | + def test_export_utf8_yaml(self): |
| 563 | + with tempfile.TemporaryDirectory() as tempdir: |
| 564 | + filepath = Path(tempdir) / "config.yaml" |
| 565 | + with mock.patch("builtins.open", _open_with_cp1252_default): |
| 566 | + ConfigParser.export_config_file({"note": NON_ASCII_TEXT}, filepath, fmt="yaml", allow_unicode=True) |
| 567 | + self.assertEqual(yaml.safe_load(filepath.read_bytes().decode("utf-8")), {"note": NON_ASCII_TEXT}) |
| 568 | + |
| 569 | + def test_load_non_utf8_config_raises(self): |
| 570 | + # a cp1252-encoded file used to load under a cp1252 default; it is not valid UTF-8 |
| 571 | + with tempfile.TemporaryDirectory() as tempdir: |
| 572 | + filepath = Path(tempdir) / "config.json" |
| 573 | + filepath.write_bytes('{"note": "Größe"}'.encode("cp1252")) |
| 574 | + with mock.patch("builtins.open", _open_with_cp1252_default): |
| 575 | + with self.assertRaises(UnicodeDecodeError): |
| 576 | + ConfigParser.load_config_file(filepath) |
| 577 | + |
| 578 | + |
527 | 579 | if __name__ == "__main__": |
528 | 580 | unittest.main() |
0 commit comments