[pypy-commit] pypy default: Get rid of the RUnicodeEncodeError exception class, used only at one

arigo pypy.commits at gmail.com
Wed Feb 22 03:32:41 EST 2017


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r90283:c0dbd36ded3d
Date: 2017-02-22 09:31 +0100
http://bitbucket.org/pypy/pypy/changeset/c0dbd36ded3d/

Log:	Get rid of the RUnicodeEncodeError exception class, used only at one
	place. It is turned into a space.w_UnicodeEncodeError immediately
	afterwards anyway.

diff --git a/pypy/interpreter/unicodehelper.py b/pypy/interpreter/unicodehelper.py
--- a/pypy/interpreter/unicodehelper.py
+++ b/pypy/interpreter/unicodehelper.py
@@ -16,17 +16,18 @@
                                              space.newtext(msg)]))
     return raise_unicode_exception_decode
 
-class RUnicodeEncodeError(Exception):
-    def __init__(self, encoding, object, start, end, reason):
-        self.encoding = encoding
-        self.object = object
-        self.start = start
-        self.end = end
-        self.reason = reason
-
-def raise_unicode_exception_encode(errors, encoding, msg, u,
-                                   startingpos, endingpos):
-    raise RUnicodeEncodeError(encoding, u, startingpos, endingpos, msg)
+ at specialize.memo()
+def encode_error_handler(space):
+    # Fast version of the "strict" errors handler.
+    def raise_unicode_exception_encode(errors, encoding, msg, u,
+                                       startingpos, endingpos):
+        raise OperationError(space.w_UnicodeEncodeError,
+                             space.newtuple([space.newtext(encoding),
+                                             space.newunicode(u),
+                                             space.newint(startingpos),
+                                             space.newint(endingpos),
+                                             space.newtext(msg)]))
+    return raise_unicode_exception_encode
 
 # ____________________________________________________________
 
@@ -68,5 +69,5 @@
     # it stands for.  These are the Python2 rules; Python3 differs.
     return runicode.unicode_encode_utf_8(
         uni, len(uni), "strict",
-        errorhandler=raise_unicode_exception_encode,
+        errorhandler=None,
         allow_surrogates=True)
diff --git a/pypy/objspace/std/unicodeobject.py b/pypy/objspace/std/unicodeobject.py
--- a/pypy/objspace/std/unicodeobject.py
+++ b/pypy/objspace/std/unicodeobject.py
@@ -462,26 +462,17 @@
         w_encoder = space.sys.get_w_default_encoder()
     else:
         if errors is None or errors == 'strict':
-            try:
-                if encoding == 'ascii':
-                    u = space.unicode_w(w_object)
-                    eh = unicodehelper.raise_unicode_exception_encode
-                    return space.newbytes(unicode_encode_ascii(
-                            u, len(u), None, errorhandler=eh))
-                if encoding == 'utf-8':
-                    u = space.unicode_w(w_object)
-                    eh = unicodehelper.raise_unicode_exception_encode
-                    return space.newbytes(unicode_encode_utf_8(
-                            u, len(u), None, errorhandler=eh,
-                            allow_surrogates=True))
-            except unicodehelper.RUnicodeEncodeError as ue:
-                raise OperationError(space.w_UnicodeEncodeError,
-                                     space.newtuple([
-                    space.newtext(ue.encoding),
-                    space.newunicode(ue.object),
-                    space.newint(ue.start),
-                    space.newint(ue.end),
-                    space.newtext(ue.reason)]))
+            if encoding == 'ascii':
+                u = space.unicode_w(w_object)
+                eh = unicodehelper.encode_error_handler(space)
+                return space.newbytes(unicode_encode_ascii(
+                        u, len(u), None, errorhandler=eh))
+            if encoding == 'utf-8':
+                u = space.unicode_w(w_object)
+                eh = unicodehelper.encode_error_handler(space)
+                return space.newbytes(unicode_encode_utf_8(
+                        u, len(u), None, errorhandler=eh,
+                        allow_surrogates=True))
         from pypy.module._codecs.interp_codecs import lookup_codec
         w_encoder = space.getitem(lookup_codec(space, encoding), space.newint(0))
     if errors is None:


More information about the pypy-commit mailing list