[pypy-commit] pypy default: Try to make encode not call_may_force

fijal noreply at buildbot.pypy.org
Sun Feb 8 20:08:02 CET 2015


Author: Maciej Fijalkowski <fijall at gmail.com>
Branch: 
Changeset: r75764:1d3d9fecd11d
Date: 2015-02-08 20:58 +0200
http://bitbucket.org/pypy/pypy/changeset/1d3d9fecd11d/

Log:	Try to make encode not call_may_force

diff --git a/pypy/interpreter/unicodehelper.py b/pypy/interpreter/unicodehelper.py
--- a/pypy/interpreter/unicodehelper.py
+++ b/pypy/interpreter/unicodehelper.py
@@ -21,12 +21,7 @@
     # 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.wrap(encoding),
-                                             space.wrap(u),
-                                             space.wrap(startingpos),
-                                             space.wrap(endingpos),
-                                             space.wrap(msg)]))
+        raise UnicodeEncodeError(encoding, u, startingpos, endingpos, msg)
     return raise_unicode_exception_encode
 
 # ____________________________________________________________
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
@@ -436,17 +436,26 @@
         w_encoder = space.sys.get_w_default_encoder()
     else:
         if errors is None or errors == 'strict':
-            if encoding == 'ascii':
-                u = space.unicode_w(w_object)
-                eh = unicodehelper.encode_error_handler(space)
-                return space.wrap(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.wrap(unicode_encode_utf_8(
-                        u, len(u), None, errorhandler=eh,
-                        allow_surrogates=True))
+            try:
+                if encoding == 'ascii':
+                    u = space.unicode_w(w_object)
+                    eh = unicodehelper.encode_error_handler(space)
+                    return space.wrap(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.wrap(unicode_encode_utf_8(
+                            u, len(u), None, errorhandler=eh,
+                            allow_surrogates=True))
+            except UnicodeEncodeError, ue:
+                raise OperationError(space.w_UnicodeEncodeError,
+                                     space.newtuple([
+                    space.wrap(ue.encoding),
+                    space.wrap(ue.object),
+                    space.wrap(ue.start),
+                    space.wrap(ue.end),
+                    space.wrap(ue.reason)]))
         from pypy.module._codecs.interp_codecs import lookup_codec
         w_encoder = space.getitem(lookup_codec(space, encoding), space.wrap(0))
     if errors is None:


More information about the pypy-commit mailing list