cformat.rs: reject %c args > 255 when byte formatting

This matches CPython's behaviour.  Thanks to @DimitrisJim for the
improved implementation here.
This commit is contained in:
Daniel Watkins
2021-10-29 08:56:44 -04:00
parent 1ed3a6ca81
commit 4d2747e4b8

View File

@@ -440,12 +440,9 @@ impl CFormatSpec {
CFormatType::Character => {
if let Some(i) = obj.payload::<PyInt>() {
let ch = i
.as_bigint()
.to_u32()
.and_then(std::char::from_u32)
.ok_or_else(|| {
vm.new_overflow_error("%c arg not in range(0x110000)".to_owned())
})?;
.try_to_primitive::<u8>(vm)
.map_err(|_| vm.new_overflow_error("%c arg not in range(256)".to_owned()))?
as char;
return Ok(self.format_char(ch).into_bytes());
}
if let Some(b) = obj.payload::<PyBytes>() {