Skip to content

Commit 64f2c8d

Browse files
committed
Fix '' % {}
1 parent b23aa90 commit 64f2c8d

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

vm/src/obj/objstr.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,16 +1304,17 @@ fn do_cformat(
13041304
.filter(|(_, part)| CFormatPart::is_specifier(part))
13051305
.all(|(_, part)| CFormatPart::has_key(part));
13061306

1307-
let values_obj = if mapping_required {
1307+
let values = if mapping_required {
13081308
if !objtype::isinstance(&values_obj, &vm.ctx.dict_type()) {
13091309
return Err(vm.new_type_error("format requires a mapping".to_string()));
13101310
}
13111311
values_obj.clone()
13121312
} else {
1313-
// check for only literal parts, in which case only empty tuple is allowed
1314-
if 0 == num_specifiers
1315-
&& (!objtype::isinstance(&values_obj, &vm.ctx.tuple_type())
1316-
|| !objtuple::get_value(&values_obj).is_empty())
1313+
// check for only literal parts, in which case only dict or empty tuple is allowed
1314+
if num_specifiers == 0
1315+
&& !(objtype::isinstance(&values_obj, &vm.ctx.tuple_type)
1316+
&& objtuple::get_value(&values_obj).is_empty())
1317+
&& !objtype::isinstance(&values_obj, &vm.ctx.dict_type)
13171318
{
13181319
return Err(vm.new_type_error(
13191320
"not all arguments converted during string formatting".to_string(),
@@ -1336,12 +1337,11 @@ fn do_cformat(
13361337
let obj: PyObjectRef = match &format_spec.mapping_key {
13371338
Some(key) => {
13381339
// TODO: change the KeyError message to match the one in cpython
1339-
call_getitem(vm, &values_obj, &vm.ctx.new_str(key.to_string()))?
1340+
call_getitem(vm, &values, &vm.ctx.new_str(key.to_string()))?
13401341
}
13411342
None => {
1342-
let mut elements = objtuple::get_value(&values_obj)
1343-
.into_iter()
1344-
.skip(tuple_index);
1343+
let mut elements =
1344+
objtuple::get_value(&values).into_iter().skip(tuple_index);
13451345

13461346
tuple_index = try_update_quantity_from_tuple(
13471347
vm,
@@ -1375,11 +1375,12 @@ fn do_cformat(
13751375
}
13761376

13771377
// check that all arguments were converted
1378-
if !mapping_required
1379-
&& objtuple::get_value(&values_obj)
1378+
if (!mapping_required
1379+
&& objtuple::get_value(&values)
13801380
.into_iter()
13811381
.nth(tuple_index)
1382-
.is_some()
1382+
.is_some())
1383+
&& !objtype::isinstance(&values_obj, &vm.ctx.dict_type)
13831384
{
13841385
return Err(
13851386
vm.new_type_error("not all arguments converted during string formatting".to_string())

0 commit comments

Comments
 (0)