MCPcopy Index your code
hub / github.com/RustPython/RustPython / test_callbacks

Method test_callbacks

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 5

ordFunction · 0.85
register_errorMethod · 0.80
assertEqualMethod · 0.45
decodeMethod · 0.45
encodeMethod · 0.45

Tested by

no test coverage detected