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

Method test_callbacks

Lib/test/test_codeccallbacks.py:228–283  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

226 self.assertRaises(TypeError, codecs.charmap_encode, sin, "replace", charmap)
227
228 def test_callbacks(self):
229 def handler1(exc):
230 r = range(exc.start, exc.end)
231 if isinstance(exc, UnicodeEncodeError):
232 l = ["<%d>" % ord(exc.object[pos]) for pos in r]
233 elif isinstance(exc, UnicodeDecodeError):
234 l = ["<%d>" % exc.object[pos] for pos in r]
235 else:
236 raise TypeError("don't know how to handle %r" % exc)
237 return ("[%s]" % "".join(l), exc.end)
238
239 codecs.register_error("test.handler1", handler1)
240
241 def handler2(exc):
242 if not isinstance(exc, UnicodeDecodeError):
243 raise TypeError("don't know how to handle %r" % exc)
244 l = ["<%d>" % exc.object[pos] for pos in range(exc.start, exc.end)]
245 return ("[%s]" % "".join(l), exc.end+1) # skip one character
246
247 codecs.register_error("test.handler2", handler2)
248
249 s = b"\x00\x81\x7f\x80\xff"
250
251 self.assertEqual(
252 s.decode("ascii", "test.handler1"),
253 "\x00[<129>]\x7f[<128>][<255>]"
254 )
255 self.assertEqual(
256 s.decode("ascii", "test.handler2"),
257 "\x00[<129>][<128>]"
258 )
259
260 self.assertEqual(
261 b"\\u3042\\u3xxx".decode("unicode-escape", "test.handler1"),
262 "\u3042[<92><117><51>]xxx"
263 )
264
265 self.assertEqual(
266 b"\\u3042\\u3xx".decode("unicode-escape", "test.handler1"),
267 "\u3042[<92><117><51>]xx"
268 )
269
270 self.assertEqual(
271 codecs.charmap_decode(b"abc", "test.handler1", {ord("a"): "z"})[0],
272 "z[<98>][<99>]"
273 )
274
275 self.assertEqual(
276 "g\xfc\xdfrk".encode("ascii", "test.handler1"),
277 b"g[<252><223>]rk"
278 )
279
280 self.assertEqual(
281 "g\xfc\xdf".encode("ascii", "test.handler1"),
282 b"g[<252><223>]"
283 )
284
285 def test_longstrings(self):

Callers

nothing calls this directly

Calls 3

assertEqualMethod · 0.45
decodeMethod · 0.45
encodeMethod · 0.45

Tested by

no test coverage detected