|
1 | 1 | use crate::{ |
2 | | - builtins::{PyBaseExceptionRef, PyStrRef}, |
| 2 | + builtins::PyBaseExceptionRef, |
3 | 3 | common::format::*, |
4 | 4 | convert::{IntoPyException, ToPyException}, |
5 | 5 | function::FuncArgs, |
6 | 6 | stdlib::builtins, |
7 | | - AsObject, PyObject, PyObjectRef, PyResult, VirtualMachine, |
| 7 | + PyObject, PyResult, VirtualMachine, |
8 | 8 | }; |
9 | 9 |
|
10 | 10 | impl IntoPyException for FormatSpecError { |
@@ -94,7 +94,20 @@ fn format_internal( |
94 | 94 | FormatString::from_str(format_spec).map_err(|e| e.to_pyexception(vm))?; |
95 | 95 | let format_spec = format_internal(vm, &nested_format, field_func)?; |
96 | 96 |
|
97 | | - pystr = call_object_format(vm, argument, *conversion_spec, &format_spec)?; |
| 97 | + let argument = match conversion_spec.and_then(FormatConversion::from_char) { |
| 98 | + Some(FormatConversion::Str) => argument.str(vm)?.into(), |
| 99 | + Some(FormatConversion::Repr) => argument.repr(vm)?.into(), |
| 100 | + Some(FormatConversion::Ascii) => { |
| 101 | + vm.ctx.new_str(builtins::ascii(argument, vm)?).into() |
| 102 | + } |
| 103 | + Some(FormatConversion::Bytes) => { |
| 104 | + vm.call_method(&argument, identifier!(vm, decode).as_str(), ())? |
| 105 | + } |
| 106 | + None => argument, |
| 107 | + }; |
| 108 | + |
| 109 | + // FIXME: compiler can intern specs using parser tree. Then this call can be interned_str |
| 110 | + pystr = vm.format(&argument, vm.ctx.new_str(format_spec))?; |
98 | 111 | pystr.as_ref() |
99 | 112 | } |
100 | 113 | FormatPart::Literal(literal) => literal, |
@@ -158,27 +171,3 @@ pub(crate) fn format_map( |
158 | 171 | FieldType::Keyword(keyword) => dict.get_item(&keyword, vm), |
159 | 172 | }) |
160 | 173 | } |
161 | | - |
162 | | -pub fn call_object_format( |
163 | | - vm: &VirtualMachine, |
164 | | - argument: PyObjectRef, |
165 | | - conversion_spec: Option<char>, |
166 | | - format_spec: &str, |
167 | | -) -> PyResult<PyStrRef> { |
168 | | - let argument = match conversion_spec.and_then(FormatConversion::from_char) { |
169 | | - Some(FormatConversion::Str) => argument.str(vm)?.into(), |
170 | | - Some(FormatConversion::Repr) => argument.repr(vm)?.into(), |
171 | | - Some(FormatConversion::Ascii) => vm.ctx.new_str(builtins::ascii(argument, vm)?).into(), |
172 | | - Some(FormatConversion::Bytes) => { |
173 | | - vm.call_method(&argument, identifier!(vm, decode).as_str(), ())? |
174 | | - } |
175 | | - None => argument, |
176 | | - }; |
177 | | - let result = vm.call_special_method(argument, identifier!(vm, __format__), (format_spec,))?; |
178 | | - result.downcast().map_err(|result| { |
179 | | - vm.new_type_error(format!( |
180 | | - "__format__ must return a str, not {}", |
181 | | - &result.class().name() |
182 | | - )) |
183 | | - }) |
184 | | -} |
0 commit comments