|
10 | 10 | import struct
|
11 | 11 | import subprocess
|
12 | 12 | import sys
|
| 13 | +from test.support.script_helper import assert_python_ok |
13 | 14 | import time
|
14 | 15 | import unittest
|
15 | 16 | import unittest.mock as mock
|
@@ -2933,7 +2934,69 @@ def test_open(self, alpharep):
|
2933 | 2934 | a, b, g = root.iterdir()
|
2934 | 2935 | with a.open(encoding="utf-8") as strm:
|
2935 | 2936 | data = strm.read()
|
2936 |
| - assert data == "content of a" |
| 2937 | + self.assertEqual(data, "content of a") |
| 2938 | + with a.open('r', "utf-8") as strm: # not a kw, no gh-101144 TypeError |
| 2939 | + data = strm.read() |
| 2940 | + self.assertEqual(data, "content of a") |
| 2941 | + |
| 2942 | + def test_open_encoding_utf16(self): |
| 2943 | + in_memory_file = io.BytesIO() |
| 2944 | + zf = zipfile.ZipFile(in_memory_file, "w") |
| 2945 | + zf.writestr("path/16.txt", "This was utf-16".encode("utf-16")) |
| 2946 | + zf.filename = "test_open_utf16.zip" |
| 2947 | + root = zipfile.Path(zf) |
| 2948 | + (path,) = root.iterdir() |
| 2949 | + u16 = path.joinpath("16.txt") |
| 2950 | + with u16.open('r', "utf-16") as strm: |
| 2951 | + data = strm.read() |
| 2952 | + self.assertEqual(data, "This was utf-16") |
| 2953 | + with u16.open(encoding="utf-16") as strm: |
| 2954 | + data = strm.read() |
| 2955 | + self.assertEqual(data, "This was utf-16") |
| 2956 | + |
| 2957 | + def test_open_encoding_errors(self): |
| 2958 | + in_memory_file = io.BytesIO() |
| 2959 | + zf = zipfile.ZipFile(in_memory_file, "w") |
| 2960 | + zf.writestr("path/bad-utf8.bin", b"invalid utf-8: \xff\xff.") |
| 2961 | + zf.filename = "test_read_text_encoding_errors.zip" |
| 2962 | + root = zipfile.Path(zf) |
| 2963 | + (path,) = root.iterdir() |
| 2964 | + u16 = path.joinpath("bad-utf8.bin") |
| 2965 | + |
| 2966 | + # encoding= as a positional argument for gh-101144. |
| 2967 | + data = u16.read_text("utf-8", errors="ignore") |
| 2968 | + self.assertEqual(data, "invalid utf-8: .") |
| 2969 | + with u16.open("r", "utf-8", errors="surrogateescape") as f: |
| 2970 | + self.assertEqual(f.read(), "invalid utf-8: \udcff\udcff.") |
| 2971 | + |
| 2972 | + # encoding= both positional and keyword is an error; gh-101144. |
| 2973 | + with self.assertRaisesRegex(TypeError, "encoding"): |
| 2974 | + data = u16.read_text("utf-8", encoding="utf-8") |
| 2975 | + |
| 2976 | + # both keyword arguments work. |
| 2977 | + with u16.open("r", encoding="utf-8", errors="strict") as f: |
| 2978 | + # error during decoding with wrong codec. |
| 2979 | + with self.assertRaises(UnicodeDecodeError): |
| 2980 | + f.read() |
| 2981 | + |
| 2982 | + def test_encoding_warnings(self): |
| 2983 | + """EncodingWarning must blame the read_text and open calls.""" |
| 2984 | + code = '''\ |
| 2985 | +import io, zipfile |
| 2986 | +with zipfile.ZipFile(io.BytesIO(), "w") as zf: |
| 2987 | + zf.filename = '<test_encoding_warnings in memory zip file>' |
| 2988 | + zf.writestr("path/file.txt", b"Spanish Inquisition") |
| 2989 | + root = zipfile.Path(zf) |
| 2990 | + (path,) = root.iterdir() |
| 2991 | + file_path = path.joinpath("file.txt") |
| 2992 | + unused = file_path.read_text() # should warn |
| 2993 | + file_path.open("r").close() # should warn |
| 2994 | +''' |
| 2995 | + proc = assert_python_ok('-X', 'warn_default_encoding', '-c', code) |
| 2996 | + warnings = proc.err.splitlines() |
| 2997 | + self.assertEqual(len(warnings), 2, proc.err) |
| 2998 | + self.assertRegex(warnings[0], rb"^<string>:8: EncodingWarning:") |
| 2999 | + self.assertRegex(warnings[1], rb"^<string>:9: EncodingWarning:") |
2937 | 3000 |
|
2938 | 3001 | def test_open_write(self):
|
2939 | 3002 | """
|
@@ -2975,6 +3038,7 @@ def test_read(self, alpharep):
|
2975 | 3038 | root = zipfile.Path(alpharep)
|
2976 | 3039 | a, b, g = root.iterdir()
|
2977 | 3040 | assert a.read_text(encoding="utf-8") == "content of a"
|
| 3041 | + a.read_text("utf-8") # No positional arg TypeError per gh-101144. |
2978 | 3042 | assert a.read_bytes() == b"content of a"
|
2979 | 3043 |
|
2980 | 3044 | @pass_alpharep
|
|
0 commit comments