Author: Matti Picus <[email protected]>
Branch: py3.6
Changeset: r97100:e5ea7d6f832f
Date: 2019-08-08 13:52 +0300
http://bitbucket.org/pypy/pypy/changeset/e5ea7d6f832f/
Log: test, fix formatting with a memoryview
diff --git a/pypy/objspace/std/formatting.py b/pypy/objspace/std/formatting.py
--- a/pypy/objspace/std/formatting.py
+++ b/pypy/objspace/std/formatting.py
@@ -516,7 +516,7 @@
if do_unicode:
self.unknown_fmtchar()
space = self.space
- # cpython explicitly checks for bytes & bytearray
+ # follow logic in cpython bytesobject.c format_obj
if space.isinstance_w(w_value, space.w_bytes):
self.std_wp(space.bytes_w(w_value))
return
@@ -534,6 +534,11 @@
"__bytes__ returned non-bytes (type '%T')",
w_bytes)
self.std_wp(space.bytes_w(w_bytes))
return
+ if space.isinstance_w(w_value, space.w_memoryview):
+ buf = w_value.buffer_w(space, 0)
+ # convert the array of the buffer to a py 2 string
+ self.std_wp(buf.as_str())
+ return
raise oefmt(space.w_TypeError,
"requires bytes, or an object that "
diff --git a/pypy/objspace/std/test/test_stringformat.py
b/pypy/objspace/std/test/test_stringformat.py
--- a/pypy/objspace/std/test/test_stringformat.py
+++ b/pypy/objspace/std/test/test_stringformat.py
@@ -455,6 +455,7 @@
assert b"<%s>" % Foo() == b"<123>"
raises(TypeError, 'b"<%s>" % 42')
raises(TypeError, 'b"<%s>" % "?"')
+ assert b"<%s>" % memoryview(b"X") == b"<X>"
class AppTestBytearray:
@@ -510,3 +511,4 @@
assert bytearray(b"<%s>") % Foo() == bytearray(b"<123>")
raises(TypeError, 'bytearray(b"<%s>") % 42')
raises(TypeError, 'bytearray(b"<%s>") % "?"')
+ assert bytearray(b"<%s>") % memoryview(b"X") == bytearray(b"<X>")
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit