Skip to content

Commit 555987e

Browse files
committed
Fix functions
1 parent b9e2b71 commit 555987e

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

common/src/format.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -516,14 +516,15 @@ impl FormatSpec {
516516
}
517517

518518
#[inline]
519-
fn format_int_radix(&self, magnitude: BigInt, radix: u32) -> Result<String, &'static str> {
519+
fn format_int_radix(&self, magnitude: BigInt, radix: u32) -> Result<String, String> {
520520
match self.precision {
521-
Some(_) => Err("Precision not allowed in integer format specifier"),
521+
Some(_) => Err("Precision not allowed in integer format specifier".to_owned()),
522522
None => Ok(magnitude.to_str_radix(radix)),
523523
}
524524
}
525525

526-
pub fn format_int(&self, num: &BigInt) -> Result<String, &'static str> {
526+
pub fn format_int(&self, num: &BigInt) -> Result<String, String> {
527+
self.validate_format(FormatType::Decimal)?;
527528
let magnitude = num.abs();
528529
let prefix = if self.alternate_form {
529530
match self.format_type {
@@ -536,30 +537,34 @@ impl FormatSpec {
536537
} else {
537538
""
538539
};
539-
let raw_magnitude_str: Result<String, &'static str> = match self.format_type {
540+
let raw_magnitude_str: Result<String, String> = match self.format_type {
540541
Some(FormatType::Binary) => self.format_int_radix(magnitude, 2),
541542
Some(FormatType::Decimal) => self.format_int_radix(magnitude, 10),
542543
Some(FormatType::Octal) => self.format_int_radix(magnitude, 8),
543544
Some(FormatType::HexLower) => self.format_int_radix(magnitude, 16),
544545
Some(FormatType::HexUpper) => match self.precision {
545-
Some(_) => Err("Precision not allowed in integer format specifier"),
546+
Some(_) => Err("Precision not allowed in integer format specifier".to_owned()),
546547
None => {
547548
let mut result = magnitude.to_str_radix(16);
548549
result.make_ascii_uppercase();
549550
Ok(result)
550551
}
551552
},
552553
Some(FormatType::Number) => self.format_int_radix(magnitude, 10),
553-
Some(FormatType::String) => Err("Unknown format code 's' for object of type 'int'"),
554+
Some(FormatType::String) => {
555+
Err("Unknown format code 's' for object of type 'int'".to_owned())
556+
}
554557
Some(FormatType::Character) => match (self.sign, self.alternate_form) {
555-
(Some(_), _) => Err("Sign not allowed with integer format specifier 'c'"),
556-
(_, true) => {
557-
Err("Alternate form (#) not allowed with integer format specifier 'c'")
558+
(Some(_), _) => {
559+
Err("Sign not allowed with integer format specifier 'c'".to_owned())
558560
}
561+
(_, true) => Err(
562+
"Alternate form (#) not allowed with integer format specifier 'c'".to_owned(),
563+
),
559564
(_, _) => match num.to_u32() {
560565
Some(n) if n <= 0x10ffff => Ok(std::char::from_u32(n).unwrap().to_string()),
561566
// TODO: raise OverflowError
562-
Some(_) | None => Err("%c arg not in range(0x110000)"),
567+
Some(_) | None => Err("%c arg not in range(0x110000)".to_owned()),
563568
},
564569
},
565570
Some(FormatType::GeneralFormatUpper)
@@ -569,8 +574,8 @@ impl FormatSpec {
569574
| Some(FormatType::ExponentUpper)
570575
| Some(FormatType::ExponentLower)
571576
| Some(FormatType::Percentage) => match num.to_f64() {
572-
Some(float) => return self.format_float(float),
573-
_ => Err("Unable to convert int to float"),
577+
Some(float) => return self.format_float(float).map_err(|msg| msg.to_owned()),
578+
_ => Err("Unable to convert int to float".to_owned()),
574579
},
575580
None => self.format_int_radix(magnitude, 10),
576581
};
@@ -586,6 +591,7 @@ impl FormatSpec {
586591
let sign_prefix = format!("{}{}", sign_str, prefix);
587592
let magnitude_str = self.add_magnitude_separators(raw_magnitude_str?, &sign_prefix);
588593
self.format_sign_and_align(&magnitude_str, &sign_prefix, FormatAlign::Right)
594+
.map_err(|msg| msg.to_owned())
589595
}
590596

591597
pub fn format_string(&self, s: &str) -> Result<String, &'static str> {

vm/src/builtins/int.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -568,9 +568,11 @@ impl PyInt {
568568

569569
#[pymethod(magic)]
570570
fn format(&self, spec: PyStrRef, vm: &VirtualMachine) -> PyResult<String> {
571-
FormatSpec::parse(spec.as_str())
572-
.and_then(|format_spec| format_spec.format_int(&self.value))
573-
.map_err(|msg| vm.new_value_error(msg.to_owned()))
571+
let format_spec =
572+
FormatSpec::parse(spec.as_str()).map_err(|msg| vm.new_value_error(msg.to_owned()))?;
573+
format_spec
574+
.format_int(&self.value)
575+
.map_err(|msg| vm.new_value_error(msg))
574576
}
575577

576578
#[pymethod(magic)]

0 commit comments

Comments
 (0)