Commits

Philip Jenvey  committed e7699f2

convert OverflowErrors on bad handler results to IndexError

  • Participants
  • Parent commits 499e291
  • Branches py3k

Comments (0)

Files changed (3)

File pypy/module/_codecs/interp_codecs.py

                     msg = ("encoding error handler must return "
                            "(str/bytes, int) tuple")
                 raise OperationError(space.w_TypeError, space.wrap(msg))
+
             w_replace, w_newpos = space.fixedview(w_res, 2)
-            newpos = space.int_w(w_newpos)
-            if newpos < 0:
-                newpos = len(input) + newpos
+            try:
+                newpos = space.int_w(w_newpos)
+            except OperationError, e:
+                if not e.match(space, space.w_OverflowError):
+                    raise
+                newpos = -1
+            else:
+                if newpos < 0:
+                    newpos = len(input) + newpos
             if newpos < 0 or newpos > len(input):
                 raise operationerrfmt(
                     space.w_IndexError,

File pypy/module/_codecs/test/test_codecs.py

                     "test.badhandler"
                 )
 
+    def test_badhandler_longindex(self):
+        import codecs
+        import sys
+        errors = 'test.badhandler_longindex'
+        codecs.register_error(errors, lambda x: ('', sys.maxsize + 1))
+        # CPython raises OverflowError here
+        raises((IndexError, OverflowError), b'apple\x92ham\x93spam'.decode, 'utf-8', errors)
+
     def test_unicode_internal(self):
         import codecs
         import sys

File pypy/module/_multibytecodec/test/test_app_codecs.py

         raises(TypeError, b"abc\xDD".decode, "hz",
                "test.test_decode_custom_error_handler_type")
 
+    def test_decode_custom_error_handler_longindex(self):
+        import codecs
+        import sys
+        codecs.register_error("test.test_decode_custom_error_handler_longindex",
+                              lambda e: ('', sys.maxsize + 1))
+        raises(IndexError, b"abc\xDD".decode, "hz",
+               "test.test_decode_custom_error_handler_longindex")
+
     def test_encode_hz(self):
         import _codecs_cn
         codec = _codecs_cn.getcodec("hz")