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

Method test_decodehelper

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

Source from the content-addressed store, hash-verified

935 s.encode(enc, err)
936
937 def test_decodehelper(self):
938 # enhance coverage of:
939 # Objects/unicodeobject.c::unicode_decode_call_errorhandler()
940 # and callers
941 self.assertRaises(LookupError, b"\xff".decode, "ascii", "test.unknown")
942
943 def baddecodereturn1(exc):
944 return 42
945 codecs.register_error("test.baddecodereturn1", baddecodereturn1)
946 self.assertRaises(TypeError, b"\xff".decode, "ascii", "test.baddecodereturn1")
947 self.assertRaises(TypeError, b"\\".decode, "unicode-escape", "test.baddecodereturn1")
948 self.assertRaises(TypeError, b"\\x0".decode, "unicode-escape", "test.baddecodereturn1")
949 self.assertRaises(TypeError, b"\\x0y".decode, "unicode-escape", "test.baddecodereturn1")
950 self.assertRaises(TypeError, b"\\Uffffeeee".decode, "unicode-escape", "test.baddecodereturn1")
951 self.assertRaises(TypeError, b"\\uyyyy".decode, "raw-unicode-escape", "test.baddecodereturn1")
952
953 def baddecodereturn2(exc):
954 return ("?", None)
955 codecs.register_error("test.baddecodereturn2", baddecodereturn2)
956 self.assertRaises(TypeError, b"\xff".decode, "ascii", "test.baddecodereturn2")
957
958 handler = PosReturn()
959 codecs.register_error("test.posreturn", handler.handle)
960
961 # Valid negative position
962 handler.pos = -1
963 self.assertEqual(b"\xff0".decode("ascii", "test.posreturn"), "<?>0")
964
965 # Valid negative position
966 handler.pos = -2
967 self.assertEqual(b"\xff0".decode("ascii", "test.posreturn"), "<?><?>")
968
969 # Negative position out of bounds
970 handler.pos = -3
971 self.assertRaises(IndexError, b"\xff0".decode, "ascii", "test.posreturn")
972
973 # Valid positive position
974 handler.pos = 1
975 self.assertEqual(b"\xff0".decode("ascii", "test.posreturn"), "<?>0")
976
977 # Largest valid positive position (one beyond end of input)
978 handler.pos = 2
979 self.assertEqual(b"\xff0".decode("ascii", "test.posreturn"), "<?>")
980
981 # Invalid positive position
982 handler.pos = 3
983 self.assertRaises(IndexError, b"\xff0".decode, "ascii", "test.posreturn")
984
985 # Restart at the "0"
986 handler.pos = 6
987 self.assertEqual(b"\\uyyyy0".decode("raw-unicode-escape", "test.posreturn"), "<?>0")
988
989 class D(dict):
990 def __getitem__(self, key):
991 raise ValueError
992 self.assertRaises(UnicodeError, codecs.charmap_decode, b"\xff", "strict", {0xff: None})
993 self.assertRaises(ValueError, codecs.charmap_decode, b"\xff", "strict", D())
994 self.assertRaises(TypeError, codecs.charmap_decode, b"\xff", "strict", {0xff: sys.maxunicode+1})

Callers

nothing calls this directly

Calls 6

PosReturnClass · 0.85
register_errorMethod · 0.80
DClass · 0.70
assertRaisesMethod · 0.45
assertEqualMethod · 0.45
decodeMethod · 0.45

Tested by

no test coverage detected