Author: Matti Picus <[email protected]>
Branch: unicode-utf8
Changeset: r95546:8704e00eb624
Date: 2019-01-01 08:44 +0200
http://bitbucket.org/pypy/pypy/changeset/8704e00eb624/
Log: 'abc'.encode(...) in cpython calls 'abc'.decode('ascii',
'strict').encode(...)
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,6 +465,10 @@
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))
@@ -869,7 +873,7 @@
center = interpindirect2app(W_AbstractBytesObject.descr_center),
count = interpindirect2app(W_AbstractBytesObject.descr_count),
decode = interpindirect2app(W_AbstractBytesObject.descr_decode),
- encode = interpindirect2app(W_AbstractBytesObject.descr_encode),
+ encode = interpindirect2app(W_BytesObject.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/test/test_bytesobject.py
b/pypy/objspace/std/test/test_bytesobject.py
--- a/pypy/objspace/std/test/test_bytesobject.py
+++ b/pypy/objspace/std/test/test_bytesobject.py
@@ -812,6 +812,11 @@
def test_encode(self):
assert 'hello'.encode() == 'hello'
assert type('hello'.encode()) is str
+ s = 'hello \xf8 world'
+ # CPython first decodes the bytes, then encodes
+ exc = raises(UnicodeDecodeError, s.encode, 'ascii')
+ assert str(exc.value) == ("'ascii' codec can't decode byte 0xf8"
+ " in position 6: ordinal not in range(128)")
def test_hash(self):
# check that we have the same hash as CPython for at least 31 bits
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit