From 3e19da833181c6e3d8d29900b61dcb5bfc88087f Mon Sep 17 00:00:00 2001 From: Daniel Watkins Date: Thu, 28 Oct 2021 16:33:45 -0400 Subject: [PATCH] cformat.rs: implement b'%a'/b'%r' correctly Unlike strings, b'%a' and b'%r' are equivalent, and they match the '%a' behaviour of strings, not '%r'. Thanks to @youknowone for improving this implementation. --- vm/src/cformat.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/vm/src/cformat.rs b/vm/src/cformat.rs index c0192dc21..aade49bb0 100644 --- a/vm/src/cformat.rs +++ b/vm/src/cformat.rs @@ -360,10 +360,11 @@ impl CFormatSpec { fn bytes_format(&self, vm: &VirtualMachine, obj: PyObjectRef) -> PyResult> { match &self.format_type { CFormatType::String(preconversor) => match preconversor { + // Unlike strings, %r and %a are identical for bytes: the behaviour corresponds to + // %a for strings (not %r) CFormatPreconversor::Repr | CFormatPreconversor::Ascii => { - let s = obj.repr(vm)?; - let s = self.format_string(s.as_str().to_owned()); - Ok(s.into_bytes()) + let b = builtins::ascii(obj, vm)?.into(); + Ok(b) } CFormatPreconversor::Str | CFormatPreconversor::Bytes => { if let Ok(buffer) = PyBuffer::try_from_borrowed_object(vm, &obj) {