(self)
| 159 | assert data == "This was utf-16" |
| 160 | |
| 161 | def test_open_encoding_errors(self): |
| 162 | in_memory_file = io.BytesIO() |
| 163 | zf = zipfile.ZipFile(in_memory_file, "w") |
| 164 | zf.writestr("path/bad-utf8.bin", b"invalid utf-8: \xff\xff.") |
| 165 | zf.filename = "test_read_text_encoding_errors.zip" |
| 166 | root = zipfile.Path(zf) |
| 167 | (path,) = root.iterdir() |
| 168 | u16 = path.joinpath("bad-utf8.bin") |
| 169 | |
| 170 | # encoding= as a positional argument for gh-101144. |
| 171 | data = u16.read_text("utf-8", errors="ignore") |
| 172 | assert data == "invalid utf-8: ." |
| 173 | with u16.open("r", "utf-8", errors="surrogateescape") as f: |
| 174 | assert f.read() == "invalid utf-8: \udcff\udcff." |
| 175 | |
| 176 | # encoding= both positional and keyword is an error; gh-101144. |
| 177 | with self.assertRaisesRegex(TypeError, "encoding"): |
| 178 | data = u16.read_text("utf-8", encoding="utf-8") |
| 179 | |
| 180 | # both keyword arguments work. |
| 181 | with u16.open("r", encoding="utf-8", errors="strict") as f: |
| 182 | # error during decoding with wrong codec. |
| 183 | with self.assertRaises(UnicodeDecodeError): |
| 184 | f.read() |
| 185 | |
| 186 | @unittest.skipIf( |
| 187 | not getattr(sys.flags, 'warn_default_encoding', 0), |
nothing calls this directly
no test coverage detected