|
2 | 2 | # Copyright (C) 1996-2017 Python Software Foundation |
3 | 3 | # |
4 | 4 | # Licensed under the PYTHON SOFTWARE FOUNDATION LICENSE VERSION 2 |
| 5 | +import importlib |
| 6 | +import importlib.util |
5 | 7 | import sys |
| 8 | +from pathlib import Path |
6 | 9 |
|
7 | 10 |
|
8 | 11 | def coding_checker(self, coder): |
@@ -892,6 +895,34 @@ def test_encode_dict_err_xmlcharrefreplace(self): |
892 | 895 |
|
893 | 896 | class MultibyteCodecTest(unittest.TestCase): |
894 | 897 |
|
| 898 | + def test_missing_multibyte_codecs_raise_lookup_error(self): |
| 899 | + for module_name in ('_codecs_cn', '_codecs_hk', '_codecs_iso2022', '_codecs_jp', '_codecs_kr', '_codecs_tw'): |
| 900 | + with self.subTest(module_name=module_name): |
| 901 | + module = importlib.import_module(module_name) |
| 902 | + self.assertRaises(LookupError, module.getcodec, '__missing_codec__') |
| 903 | + |
| 904 | + def test_unsupported_multibyte_codec_modules_raise_import_error_on_graalpy(self): |
| 905 | + encodings_dir = Path(__file__).resolve().parents[3] / 'lib-python' / '3' / 'encodings' |
| 906 | + for module_name in ( |
| 907 | + 'encodings.euc_jis_2004', |
| 908 | + 'encodings.euc_jisx0213', |
| 909 | + 'encodings.iso2022_jp_1', |
| 910 | + 'encodings.iso2022_jp_2004', |
| 911 | + 'encodings.iso2022_jp_3', |
| 912 | + 'encodings.iso2022_jp_ext', |
| 913 | + ): |
| 914 | + with self.subTest(module_name=module_name): |
| 915 | + module_path = encodings_dir / f'{module_name.rsplit(".", 1)[1]}.py' |
| 916 | + spec = importlib.util.spec_from_file_location(f'test_{module_name.replace(".", "_")}', module_path) |
| 917 | + module = importlib.util.module_from_spec(spec) |
| 918 | + if sys.implementation.name == 'graalpy': |
| 919 | + self.assertRaises(ImportError, spec.loader.exec_module, module) |
| 920 | + else: |
| 921 | + spec.loader.exec_module(module) |
| 922 | + |
| 923 | + def test_shift_jis_2004_codec_module_imports(self): |
| 924 | + import encodings.shift_jis_2004 |
| 925 | + |
895 | 926 | # just a smoke test |
896 | 927 | def test_encode(self): |
897 | 928 | import _codecs_tw |
|
0 commit comments