Match invalid format specifier error messages by shAn-kor · Pull Request #8477 · RustPython/RustPython · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions Lib/test/test_format.py
6 changes: 3 additions & 3 deletions crates/vm/src/builtins/bool.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use super::{PyInt, PyStrRef, PyType, PyTypeRef, PyUtf8StrRef};
use crate::common::format::FormatSpec;
use crate::{
AsObject, Context, Py, PyObject, PyObjectRef, PyResult, TryFromBorrowedObject, VirtualMachine,
class::PyClassImpl,
Expand Down Expand Up @@ -100,9 +99,10 @@ impl Constructor for PyBool {
impl PyBool {
#[pymethod]
fn __format__(obj: PyObjectRef, spec: PyUtf8StrRef, vm: &VirtualMachine) -> PyResult<String> {
let format_spec = crate::format::parse_format_spec(obj.as_object(), spec.as_str(), vm)?;
let new_bool = obj.try_to_bool(vm)?;
FormatSpec::parse(spec.as_str())
.and_then(|format_spec| format_spec.format_bool(new_bool))
format_spec
.format_bool(new_bool)
.map_err(|err| err.into_pyexception(vm))
}
}
Expand Down
5 changes: 2 additions & 3 deletions crates/vm/src/builtins/complex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
builtins::PyUtf8StrRef,
class::PyClassImpl,
common::{format::FormatSpec, wtf8::Wtf8Buf},
common::wtf8::Wtf8Buf,
convert::{IntoPyException, ToPyObject, ToPyResult},
function::{FuncArgs, OptionalArg, PyComparisonValue},
protocol::PyNumberMethods,
Expand Down Expand Up @@ -367,8 +367,7 @@ impl PyComplex {
if spec.is_empty() {
return Ok(zelf.as_object().str(vm)?.as_wtf8().to_owned());
}
let format_spec =
FormatSpec::parse(spec.as_str()).map_err(|err| err.into_pyexception(vm))?;
let format_spec = crate::format::parse_format_spec(zelf.as_object(), spec.as_str(), vm)?;
let result = if format_spec.has_locale_format() {
let locale = crate::format::get_locale_info();
format_spec.format_complex_locale(&zelf.value, &locale)
Expand Down
5 changes: 2 additions & 3 deletions crates/vm/src/builtins/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult,
TryFromBorrowedObject, TryFromObject, VirtualMachine,
class::PyClassImpl,
common::{float_ops, format::FormatSpec, hash, wtf8::Wtf8Buf},
common::{float_ops, hash, wtf8::Wtf8Buf},
convert::{IntoPyException, ToPyObject, ToPyResult},
function::{
ArgBytesLike, FuncArgs, OptionalArg, OptionalOption, PyArithmeticValue, PyComparisonValue,
Expand Down Expand Up @@ -269,8 +269,7 @@ impl PyFloat {
if spec.is_empty() {
return Ok(zelf.as_object().str(vm)?.as_wtf8().to_owned());
}
let format_spec =
FormatSpec::parse(spec.as_str()).map_err(|err| err.into_pyexception(vm))?;
let format_spec = crate::format::parse_format_spec(zelf.as_object(), spec.as_str(), vm)?;
let result = if format_spec.has_locale_format() {
let locale = crate::format::get_locale_info();
format_spec.format_float_locale(zelf.value, &locale)
Expand Down
4 changes: 1 addition & 3 deletions crates/vm/src/builtins/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::{
bytes_inner::PyBytesInner,
class::PyClassImpl,
common::{
format::FormatSpec,
hash,
int::{bigint_to_finite_float, bytes_to_int, true_div},
wtf8::Wtf8Buf,
Expand Down Expand Up @@ -511,8 +510,7 @@ impl PyInt {
if spec.is_empty() && !zelf.class().is(vm.ctx.types.int_type) {
return Ok(zelf.as_object().str(vm)?.as_wtf8().to_owned());
}
let format_spec =
FormatSpec::parse(spec.as_str()).map_err(|err| err.into_pyexception(vm))?;
let format_spec = crate::format::parse_format_spec(zelf.as_object(), spec.as_str(), vm)?;
if format_spec.is_decimal_int_format() {
check_int_to_str_digits(&zelf.value, vm)?;
}
Expand Down
9 changes: 4 additions & 5 deletions crates/vm/src/builtins/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use num_traits::ToPrimitive;
use rustpython_common::{
ascii,
atomic::{self, PyAtomic, Radium},
format::{FormatSpec, FormatString, FromTemplate},
format::{FormatString, FromTemplate},
hash,
lock::PyMutex,
str::DeduceStrKind,
Expand Down Expand Up @@ -1017,10 +1017,9 @@ impl PyStr {
};
}
let zelf = zelf.try_into_utf8(vm)?;
let s = FormatSpec::parse(spec.as_str())
.and_then(|format_spec| {
format_spec.format_string(&CharLenStr(zelf.as_str(), zelf.char_len()))
})
let format_spec = crate::format::parse_format_spec(zelf.as_object(), spec.as_str(), vm)?;
let s = format_spec
.format_string(&CharLenStr(zelf.as_str(), zelf.char_len()))
.map_err(|err| err.into_pyexception(vm))?;
Ok(vm.ctx.new_str(s))
}
Expand Down
14 changes: 14 additions & 0 deletions crates/vm/src/format.rs
Loading