|
1 | 1 | import io
|
2 |
| -import zipfile |
| 2 | +import itertools |
3 | 3 | import contextlib
|
4 | 4 | import pathlib
|
5 |
| -import unittest |
6 |
| -import string |
7 | 5 | import pickle
|
8 |
| -import itertools |
| 6 | +import string |
| 7 | +from test.support.script_helper import assert_python_ok |
| 8 | +import unittest |
| 9 | +import zipfile |
9 | 10 |
|
10 | 11 | from ._test_params import parameterize, Invoked
|
11 | 12 | from ._functools import compose
|
@@ -145,7 +146,69 @@ def test_open(self, alpharep):
|
145 | 146 | a, b, g = root.iterdir()
|
146 | 147 | with a.open(encoding="utf-8") as strm:
|
147 | 148 | data = strm.read()
|
148 |
| - assert data == "content of a" |
| 149 | + self.assertEqual(data, "content of a") |
| 150 | + with a.open('r', "utf-8") as strm: # not a kw, no gh-101144 TypeError |
| 151 | + data = strm.read() |
| 152 | + self.assertEqual(data, "content of a") |
| 153 | + |
| 154 | + def test_open_encoding_utf16(self): |
| 155 | + in_memory_file = io.BytesIO() |
| 156 | + zf = zipfile.ZipFile(in_memory_file, "w") |
| 157 | + zf.writestr("path/16.txt", "This was utf-16".encode("utf-16")) |
| 158 | + zf.filename = "test_open_utf16.zip" |
| 159 | + root = zipfile.Path(zf) |
| 160 | + (path,) = root.iterdir() |
| 161 | + u16 = path.joinpath("16.txt") |
| 162 | + with u16.open('r', "utf-16") as strm: |
| 163 | + data = strm.read() |
| 164 | + self.assertEqual(data, "This was utf-16") |
| 165 | + with u16.open(encoding="utf-16") as strm: |
| 166 | + data = strm.read() |
| 167 | + self.assertEqual(data, "This was utf-16") |
| 168 | + |
| 169 | + def test_open_encoding_errors(self): |
| 170 | + in_memory_file = io.BytesIO() |
| 171 | + zf = zipfile.ZipFile(in_memory_file, "w") |
| 172 | + zf.writestr("path/bad-utf8.bin", b"invalid utf-8: \xff\xff.") |
| 173 | + zf.filename = "test_read_text_encoding_errors.zip" |
| 174 | + root = zipfile.Path(zf) |
| 175 | + (path,) = root.iterdir() |
| 176 | + u16 = path.joinpath("bad-utf8.bin") |
| 177 | + |
| 178 | + # encoding= as a positional argument for gh-101144. |
| 179 | + data = u16.read_text("utf-8", errors="ignore") |
| 180 | + self.assertEqual(data, "invalid utf-8: .") |
| 181 | + with u16.open("r", "utf-8", errors="surrogateescape") as f: |
| 182 | + self.assertEqual(f.read(), "invalid utf-8: \udcff\udcff.") |
| 183 | + |
| 184 | + # encoding= both positional and keyword is an error; gh-101144. |
| 185 | + with self.assertRaisesRegex(TypeError, "encoding"): |
| 186 | + data = u16.read_text("utf-8", encoding="utf-8") |
| 187 | + |
| 188 | + # both keyword arguments work. |
| 189 | + with u16.open("r", encoding="utf-8", errors="strict") as f: |
| 190 | + # error during decoding with wrong codec. |
| 191 | + with self.assertRaises(UnicodeDecodeError): |
| 192 | + f.read() |
| 193 | + |
| 194 | + def test_encoding_warnings(self): |
| 195 | + """EncodingWarning must blame the read_text and open calls.""" |
| 196 | + code = '''\ |
| 197 | +import io, zipfile |
| 198 | +with zipfile.ZipFile(io.BytesIO(), "w") as zf: |
| 199 | + zf.filename = '<test_encoding_warnings in memory zip file>' |
| 200 | + zf.writestr("path/file.txt", b"Spanish Inquisition") |
| 201 | + root = zipfile.Path(zf) |
| 202 | + (path,) = root.iterdir() |
| 203 | + file_path = path.joinpath("file.txt") |
| 204 | + unused = file_path.read_text() # should warn |
| 205 | + file_path.open("r").close() # should warn |
| 206 | +''' |
| 207 | + proc = assert_python_ok('-X', 'warn_default_encoding', '-c', code) |
| 208 | + warnings = proc.err.splitlines() |
| 209 | + self.assertEqual(len(warnings), 2, proc.err) |
| 210 | + self.assertRegex(warnings[0], rb"^<string>:8: EncodingWarning:") |
| 211 | + self.assertRegex(warnings[1], rb"^<string>:9: EncodingWarning:") |
149 | 212 |
|
150 | 213 | def test_open_write(self):
|
151 | 214 | """
|
@@ -187,6 +250,7 @@ def test_read(self, alpharep):
|
187 | 250 | root = zipfile.Path(alpharep)
|
188 | 251 | a, b, g = root.iterdir()
|
189 | 252 | assert a.read_text(encoding="utf-8") == "content of a"
|
| 253 | + a.read_text("utf-8") # No positional arg TypeError per gh-101144. |
190 | 254 | assert a.read_bytes() == b"content of a"
|
191 | 255 |
|
192 | 256 | @pass_alpharep
|
|
0 commit comments