Author: Matti Picus <[email protected]>
Branch: unicode-utf8
Changeset: r95921:6cea85fa01f6
Date: 2019-02-09 16:49 +0100
http://bitbucket.org/pypy/pypy/changeset/6cea85fa01f6/

Log:    deal with s.encode() differently (where s can be either bytes or
        unicode)

diff --git a/pypy/objspace/std/bytesobject.py b/pypy/objspace/std/bytesobject.py
--- a/pypy/objspace/std/bytesobject.py
+++ b/pypy/objspace/std/bytesobject.py
@@ -465,10 +465,6 @@
         raise oefmt(space.w_TypeError,
                     "Cannot use string as modifiable buffer")
 
-    def descr_encode(self, space, w_encoding=None, w_errors=None):
-        w_uni = self.descr_decode(space, space.newtext('ascii'), 
space.newtext('strict'))
-        return space.call_method(w_uni, 'encode', w_encoding, w_errors)
-
     def descr_getbuffer(self, space, w_flags):
         #from pypy.objspace.std.bufferobject import W_Buffer
         #return W_Buffer(StringBuffer(self._value))
@@ -873,7 +869,7 @@
     center = interpindirect2app(W_AbstractBytesObject.descr_center),
     count = interpindirect2app(W_AbstractBytesObject.descr_count),
     decode = interpindirect2app(W_AbstractBytesObject.descr_decode),
-    encode = interpindirect2app(W_BytesObject.descr_encode),
+    encode = interpindirect2app(W_AbstractBytesObject.descr_encode),
     expandtabs = interpindirect2app(W_AbstractBytesObject.descr_expandtabs),
     find = interpindirect2app(W_AbstractBytesObject.descr_find),
     rfind = interpindirect2app(W_AbstractBytesObject.descr_rfind),
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
@@ -1075,7 +1075,13 @@
             try:
                 rutf8.check_ascii(s)
             except rutf8.CheckError as a:
-                eh = unicodehelper.encode_error_handler(space)
+                if space.isinstance_w(w_object, space.w_unicode):
+                    eh = unicodehelper.encode_error_handler(space)
+                else:
+                    # must be a bytes-like object. In order to encode it,
+                    # first "decode" to unicode. Since we cannot, raise a
+                    # UnicodeDecodeError, not a UnicodeEncodeError
+                    eh = unicodehelper.decode_error_handler(space)
                 eh(None, "ascii", "ordinal not in range(128)", s,
                     a.pos, a.pos + 1)
                 assert False, "always raises"
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to