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

Skip to content

Commit b8a78d3

Browse files
Use non-zero and non-last positions in error handler tests.
2 parents c7965e0 + 05d5473 commit b8a78d3

1 file changed

Lines changed: 37 additions & 30 deletions

File tree

Lib/test/test_codeccallbacks.py

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -467,18 +467,18 @@ def test_badandgoodignoreexceptions(self):
467467
# If the correct exception is passed in, "ignore" returns an empty replacement
468468
self.assertEqual(
469469
codecs.ignore_errors(
470-
UnicodeEncodeError("ascii", "\u3042", 0, 1, "ouch")),
471-
("", 1)
470+
UnicodeEncodeError("ascii", "a\u3042b", 1, 2, "ouch")),
471+
("", 2)
472472
)
473473
self.assertEqual(
474474
codecs.ignore_errors(
475-
UnicodeDecodeError("ascii", bytearray(b"\xff"), 0, 1, "ouch")),
476-
("", 1)
475+
UnicodeDecodeError("ascii", bytearray(b"a\xffb"), 1, 2, "ouch")),
476+
("", 2)
477477
)
478478
self.assertEqual(
479479
codecs.ignore_errors(
480-
UnicodeTranslateError("\u3042", 0, 1, "ouch")),
481-
("", 1)
480+
UnicodeTranslateError("a\u3042b", 1, 2, "ouch")),
481+
("", 2)
482482
)
483483

484484
def test_badandgoodreplaceexceptions(self):
@@ -507,18 +507,18 @@ def test_badandgoodreplaceexceptions(self):
507507
# With the correct exception, "replace" returns an "?" or "\ufffd" replacement
508508
self.assertEqual(
509509
codecs.replace_errors(
510-
UnicodeEncodeError("ascii", "\u3042", 0, 1, "ouch")),
511-
("?", 1)
510+
UnicodeEncodeError("ascii", "a\u3042b", 1, 2, "ouch")),
511+
("?", 2)
512512
)
513513
self.assertEqual(
514514
codecs.replace_errors(
515-
UnicodeDecodeError("ascii", bytearray(b"\xff"), 0, 1, "ouch")),
516-
("\ufffd", 1)
515+
UnicodeDecodeError("ascii", bytearray(b"a\xffb"), 1, 2, "ouch")),
516+
("\ufffd", 2)
517517
)
518518
self.assertEqual(
519519
codecs.replace_errors(
520-
UnicodeTranslateError("\u3042", 0, 1, "ouch")),
521-
("\ufffd", 1)
520+
UnicodeTranslateError("a\u3042b", 1, 2, "ouch")),
521+
("\ufffd", 2)
522522
)
523523

524524
def test_badandgoodxmlcharrefreplaceexceptions(self):
@@ -552,9 +552,10 @@ def test_badandgoodxmlcharrefreplaceexceptions(self):
552552
s = "".join(chr(c) for c in cs)
553553
self.assertEqual(
554554
codecs.xmlcharrefreplace_errors(
555-
UnicodeEncodeError("ascii", s, 0, len(s), "ouch")
555+
UnicodeEncodeError("ascii", "a" + s + "b",
556+
1, 1 + len(s), "ouch")
556557
),
557-
("".join("&#%d;" % c for c in cs), len(s))
558+
("".join("&#%d;" % c for c in cs), 1 + len(s))
558559
)
559560

560561
def test_badandgoodbackslashreplaceexceptions(self):
@@ -590,13 +591,15 @@ def test_badandgoodbackslashreplaceexceptions(self):
590591
with self.subTest(str=s):
591592
self.assertEqual(
592593
codecs.backslashreplace_errors(
593-
UnicodeEncodeError("ascii", s, 0, len(s), "ouch")),
594-
(r, len(s))
594+
UnicodeEncodeError("ascii", "a" + s + "b",
595+
1, 1 + len(s), "ouch")),
596+
(r, 1 + len(s))
595597
)
596598
self.assertEqual(
597599
codecs.backslashreplace_errors(
598-
UnicodeTranslateError(s, 0, len(s), "ouch")),
599-
(r, len(s))
600+
UnicodeTranslateError("a" + s + "b",
601+
1, 1 + len(s), "ouch")),
602+
(r, 1 + len(s))
600603
)
601604
tests = [
602605
(b"a", "\\x61"),
@@ -608,8 +611,9 @@ def test_badandgoodbackslashreplaceexceptions(self):
608611
with self.subTest(bytes=b):
609612
self.assertEqual(
610613
codecs.backslashreplace_errors(
611-
UnicodeDecodeError("ascii", bytearray(b), 0, 1, "ouch")),
612-
(r, 1)
614+
UnicodeDecodeError("ascii", bytearray(b"a" + b + b"b"),
615+
1, 2, "ouch")),
616+
(r, 2)
613617
)
614618

615619
def test_badandgoodnamereplaceexceptions(self):
@@ -653,8 +657,9 @@ def test_badandgoodnamereplaceexceptions(self):
653657
with self.subTest(str=s):
654658
self.assertEqual(
655659
codecs.namereplace_errors(
656-
UnicodeEncodeError("ascii", s, 0, len(s), "ouch")),
657-
(r, len(s))
660+
UnicodeEncodeError("ascii", "a" + s + "b",
661+
1, 1 + len(s), "ouch")),
662+
(r, 1 + len(s))
658663
)
659664

660665
def test_badandgoodsurrogateescapeexceptions(self):
@@ -687,8 +692,8 @@ def test_badandgoodsurrogateescapeexceptions(self):
687692
)
688693
self.assertEqual(
689694
surrogateescape_errors(
690-
UnicodeEncodeError("ascii", "\udc80", 0, 1, "ouch")),
691-
(b"\x80", 1)
695+
UnicodeEncodeError("ascii", "a\udc80b", 1, 2, "ouch")),
696+
(b"\x80", 2)
692697
)
693698
self.assertRaises(
694699
UnicodeDecodeError,
@@ -697,8 +702,8 @@ def test_badandgoodsurrogateescapeexceptions(self):
697702
)
698703
self.assertEqual(
699704
surrogateescape_errors(
700-
UnicodeDecodeError("ascii", bytearray(b"\x80"), 0, 1, "ouch")),
701-
("\udc80", 1)
705+
UnicodeDecodeError("ascii", bytearray(b"a\x80b"), 1, 2, "ouch")),
706+
("\udc80", 2)
702707
)
703708

704709
def test_badandgoodsurrogatepassexceptions(self):
@@ -762,13 +767,15 @@ def test_badandgoodsurrogatepassexceptions(self):
762767
with self.subTest(encoding=enc, str=s, bytes=b):
763768
self.assertEqual(
764769
surrogatepass_errors(
765-
UnicodeEncodeError(enc, s, 0, len(s), "ouch")),
766-
(b, len(s))
770+
UnicodeEncodeError(enc, "a" + s + "b",
771+
1, 1 + len(s), "ouch")),
772+
(b, 1 + len(s))
767773
)
768774
self.assertEqual(
769775
surrogatepass_errors(
770-
UnicodeDecodeError(enc, bytearray(b[:n]), 0, n, "ouch")),
771-
(s[:1], n)
776+
UnicodeDecodeError(enc, bytearray(b"a" + b[:n] + b"b"),
777+
1, 1 + n, "ouch")),
778+
(s[:1], 1 + n)
772779
)
773780

774781
def test_badhandlerresults(self):

0 commit comments

Comments
 (0)