Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 05d5473

Browse files
Use non-zero and non-last positions in error handler tests.
1 parent 98d156b commit 05d5473

1 file changed

Lines changed: 28 additions & 24 deletions

File tree

Lib/test/test_codeccallbacks.py

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -446,18 +446,18 @@ def test_badandgoodignoreexceptions(self):
446446
# If the correct exception is passed in, "ignore" returns an empty replacement
447447
self.assertEqual(
448448
codecs.ignore_errors(
449-
UnicodeEncodeError("ascii", "\u3042", 0, 1, "ouch")),
450-
("", 1)
449+
UnicodeEncodeError("ascii", "a\u3042b", 1, 2, "ouch")),
450+
("", 2)
451451
)
452452
self.assertEqual(
453453
codecs.ignore_errors(
454-
UnicodeDecodeError("ascii", bytearray(b"\xff"), 0, 1, "ouch")),
455-
("", 1)
454+
UnicodeDecodeError("ascii", bytearray(b"a\xffb"), 1, 2, "ouch")),
455+
("", 2)
456456
)
457457
self.assertEqual(
458458
codecs.ignore_errors(
459-
UnicodeTranslateError("\u3042", 0, 1, "ouch")),
460-
("", 1)
459+
UnicodeTranslateError("a\u3042b", 1, 2, "ouch")),
460+
("", 2)
461461
)
462462

463463
def test_badandgoodreplaceexceptions(self):
@@ -486,18 +486,18 @@ def test_badandgoodreplaceexceptions(self):
486486
# With the correct exception, "replace" returns an "?" or "\ufffd" replacement
487487
self.assertEqual(
488488
codecs.replace_errors(
489-
UnicodeEncodeError("ascii", "\u3042", 0, 1, "ouch")),
490-
("?", 1)
489+
UnicodeEncodeError("ascii", "a\u3042b", 1, 2, "ouch")),
490+
("?", 2)
491491
)
492492
self.assertEqual(
493493
codecs.replace_errors(
494-
UnicodeDecodeError("ascii", bytearray(b"\xff"), 0, 1, "ouch")),
495-
("\ufffd", 1)
494+
UnicodeDecodeError("ascii", bytearray(b"a\xffb"), 1, 2, "ouch")),
495+
("\ufffd", 2)
496496
)
497497
self.assertEqual(
498498
codecs.replace_errors(
499-
UnicodeTranslateError("\u3042", 0, 1, "ouch")),
500-
("\ufffd", 1)
499+
UnicodeTranslateError("a\u3042b", 1, 2, "ouch")),
500+
("\ufffd", 2)
501501
)
502502

503503
def test_badandgoodxmlcharrefreplaceexceptions(self):
@@ -531,9 +531,10 @@ def test_badandgoodxmlcharrefreplaceexceptions(self):
531531
s = "".join(chr(c) for c in cs)
532532
self.assertEqual(
533533
codecs.xmlcharrefreplace_errors(
534-
UnicodeEncodeError("ascii", s, 0, len(s), "ouch")
534+
UnicodeEncodeError("ascii", "a" + s + "b",
535+
1, 1 + len(s), "ouch")
535536
),
536-
("".join("&#%d;" % c for c in cs), len(s))
537+
("".join("&#%d;" % c for c in cs), 1 + len(s))
537538
)
538539

539540
def test_badandgoodbackslashreplaceexceptions(self):
@@ -580,8 +581,9 @@ def test_badandgoodbackslashreplaceexceptions(self):
580581
with self.subTest(str=s):
581582
self.assertEqual(
582583
codecs.backslashreplace_errors(
583-
UnicodeEncodeError("ascii", s, 0, len(s), "ouch")),
584-
(r, len(s))
584+
UnicodeEncodeError("ascii", "a" + s + "b",
585+
1, 1 + len(s), "ouch")),
586+
(r, 1 + len(s))
585587
)
586588

587589
def test_badandgoodsurrogateescapeexceptions(self):
@@ -614,8 +616,8 @@ def test_badandgoodsurrogateescapeexceptions(self):
614616
)
615617
self.assertEqual(
616618
surrogateescape_errors(
617-
UnicodeEncodeError("ascii", "\udc80", 0, 1, "ouch")),
618-
(b"\x80", 1)
619+
UnicodeEncodeError("ascii", "a\udc80b", 1, 2, "ouch")),
620+
(b"\x80", 2)
619621
)
620622
self.assertRaises(
621623
UnicodeDecodeError,
@@ -624,8 +626,8 @@ def test_badandgoodsurrogateescapeexceptions(self):
624626
)
625627
self.assertEqual(
626628
surrogateescape_errors(
627-
UnicodeDecodeError("ascii", bytearray(b"\x80"), 0, 1, "ouch")),
628-
("\udc80", 1)
629+
UnicodeDecodeError("ascii", bytearray(b"a\x80b"), 1, 2, "ouch")),
630+
("\udc80", 2)
629631
)
630632

631633
def test_badandgoodsurrogatepassexceptions(self):
@@ -685,13 +687,15 @@ def test_badandgoodsurrogatepassexceptions(self):
685687
with self.subTest(encoding=enc, str=s, bytes=b):
686688
self.assertEqual(
687689
surrogatepass_errors(
688-
UnicodeEncodeError(enc, s, 0, len(s), "ouch")),
689-
(b, len(s))
690+
UnicodeEncodeError(enc, "a" + s + "b",
691+
1, 1 + len(s), "ouch")),
692+
(b, 1 + len(s))
690693
)
691694
self.assertEqual(
692695
surrogatepass_errors(
693-
UnicodeDecodeError(enc, bytearray(b[:n]), 0, n, "ouch")),
694-
(s[:1], n)
696+
UnicodeDecodeError(enc, bytearray(b"a" + b[:n] + b"b"),
697+
1, n, "ouch")),
698+
(s[:1], 1 + n)
695699
)
696700

697701
def test_badhandlerresults(self):

0 commit comments

Comments
 (0)