Skip to content

Commit 9f8e2af

Browse files
committed
impl IntoPyException for FormatParseError
1 parent ee90f7c commit 9f8e2af

3 files changed

Lines changed: 12 additions & 11 deletions

File tree

vm/src/format.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::exceptions::PyBaseExceptionRef;
1+
use crate::exceptions::{IntoPyException, PyBaseExceptionRef};
22
use crate::function::PyFuncArgs;
33
use crate::obj::{objstr, objtype};
44
use crate::pyobject::{ItemProtocol, PyObjectRef, PyResult, TypeProtocol};
@@ -563,9 +563,8 @@ pub(crate) enum FormatParseError {
563563
InvalidCharacterAfterRightBracket,
564564
}
565565

566-
impl FormatParseError {
567-
// TODO: IntoPyException
568-
pub fn into_pyobject(self, vm: &VirtualMachine) -> PyBaseExceptionRef {
566+
impl IntoPyException for FormatParseError {
567+
fn into_pyexception(self, vm: &VirtualMachine) -> PyBaseExceptionRef {
569568
match self {
570569
FormatParseError::UnmatchedBracket => {
571570
vm.new_value_error("expected '}' before end of string".to_owned())
@@ -812,8 +811,8 @@ impl FormatString {
812811
preconversion_spec,
813812
format_spec,
814813
} => {
815-
let FieldName { field_type, parts } =
816-
FieldName::parse(field_name.as_str()).map_err(|e| e.into_pyobject(vm))?;
814+
let FieldName { field_type, parts } = FieldName::parse(field_name.as_str())
815+
.map_err(|e| e.into_pyexception(vm))?;
817816

818817
let mut argument = field_func(&field_type)?;
819818

@@ -832,7 +831,7 @@ impl FormatString {
832831
}
833832

834833
let nested_format =
835-
FormatString::from_str(&format_spec).map_err(|e| e.into_pyobject(vm))?;
834+
FormatString::from_str(&format_spec).map_err(|e| e.into_pyexception(vm))?;
836835
let format_spec = nested_format.format_internal(vm, field_func)?;
837836

838837
let value =

vm/src/obj/objstr.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use super::objiter;
1919
use super::objnone::PyNone;
2020
use super::objsequence::{PySliceableSequence, SequenceIndex};
2121
use super::objtype::{self, PyClassRef};
22+
use crate::exceptions::IntoPyException;
2223
use crate::format::{FormatSpec, FormatString, FromTemplate};
2324
use crate::function::{OptionalArg, OptionalOption, PyFuncArgs};
2425
use crate::pyobject::{
@@ -590,7 +591,7 @@ impl PyString {
590591
fn format(&self, args: PyFuncArgs, vm: &VirtualMachine) -> PyResult<String> {
591592
match FormatString::from_str(&self.value) {
592593
Ok(format_string) => format_string.format(&args, vm),
593-
Err(err) => Err(err.into_pyobject(vm)),
594+
Err(err) => Err(err.into_pyexception(vm)),
594595
}
595596
}
596597

@@ -602,7 +603,7 @@ impl PyString {
602603
fn format_map(&self, mapping: PyObjectRef, vm: &VirtualMachine) -> PyResult<String> {
603604
match FormatString::from_str(&self.value) {
604605
Ok(format_string) => format_string.format_map(&mapping, vm),
605-
Err(err) => Err(err.into_pyobject(vm)),
606+
Err(err) => Err(err.into_pyexception(vm)),
606607
}
607608
}
608609

vm/src/stdlib/string.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pub(crate) use _string::make_module;
77
mod _string {
88
use std::mem;
99

10+
use crate::exceptions::IntoPyException;
1011
use crate::format::{
1112
FieldName, FieldNamePart, FieldType, FormatPart, FormatString, FromTemplate,
1213
};
@@ -34,7 +35,7 @@ mod _string {
3435
#[pyfunction]
3536
fn formatter_parser(text: PyStringRef, vm: &VirtualMachine) -> PyResult<PyList> {
3637
let format_string =
37-
FormatString::from_str(text.as_str()).map_err(|e| e.into_pyobject(vm))?;
38+
FormatString::from_str(text.as_str()).map_err(|e| e.into_pyexception(vm))?;
3839

3940
let mut result = Vec::new();
4041
let mut literal = String::new();
@@ -73,7 +74,7 @@ mod _string {
7374
text: PyStringRef,
7475
vm: &VirtualMachine,
7576
) -> PyResult<(PyObjectRef, PyList)> {
76-
let field_name = FieldName::parse(text.as_str()).map_err(|e| e.into_pyobject(vm))?;
77+
let field_name = FieldName::parse(text.as_str()).map_err(|e| e.into_pyexception(vm))?;
7778

7879
let first = match field_name.field_type {
7980
FieldType::Auto => vm.new_str("".to_owned()),

0 commit comments

Comments
 (0)