MCPcopy Index your code
hub / github.com/python/cpython / test_open_encoding_errors

Method test_open_encoding_errors

Lib/test/test_zipfile/_path/test_path.py:161–184  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

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),

Callers

nothing calls this directly

Calls 8

writestrMethod · 0.95
iterdirMethod · 0.95
assertRaisesRegexMethod · 0.80
joinpathMethod · 0.45
read_textMethod · 0.45
openMethod · 0.45
readMethod · 0.45
assertRaisesMethod · 0.45

Tested by

no test coverage detected