Skip to content

Add str.format_map method #1639

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 24, 2019
Merged
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
62 changes: 61 additions & 1 deletion tests/snippets/strings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from testutils import assert_raises
from testutils import assert_raises, AssertRaises

assert "".__eq__(1) == NotImplemented
assert "a" == 'a'
Expand Down Expand Up @@ -338,3 +338,63 @@ def try_mutate_str():
assert "1a".islower()
assert "가나다a".islower()
assert "가나다A".isupper()

# test str.format_map()
#
# The following tests were performed in Python 3.7.5:
# Python 3.7.5 (default, Dec 19 2019, 17:11:32)
# [GCC 5.4.0 20160609] on linux

# >>> '{x} {y}'.format_map({'x': 1, 'y': 2})
# '1 2'
assert '{x} {y}'.format_map({'x': 1, 'y': 2}) == '1 2'

# >>> '{x:04d}'.format_map({'x': 1})
# '0001'
assert '{x:04d}'.format_map({'x': 1}) == '0001'

# >>> '{x} {y}'.format_map('foo')
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# TypeError: string indices must be integers
with AssertRaises(TypeError, None):
'{x} {y}'.format_map('foo')

# >>> '{x} {y}'.format_map(['foo'])
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# TypeError: list indices must be integers or slices, not str
with AssertRaises(TypeError, None):
'{x} {y}'.format_map(['foo'])

# >>> '{x} {y}'.format_map()
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# TypeError: format_map() takes exactly one argument (0 given)
with AssertRaises(TypeError, msg='TypeError: format_map() takes exactly one argument (0 given)'):
'{x} {y}'.format_map(),

# >>> '{x} {y}'.format_map('foo', 'bar')
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# TypeError: format_map() takes exactly one argument (2 given)
with AssertRaises(TypeError, msg='TypeError: format_map() takes exactly one argument (2 given)'):
'{x} {y}'.format_map('foo', 'bar')

# >>> '{x} {y}'.format_map({'x': 1})
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# KeyError: 'y'
with AssertRaises(KeyError, msg="KeyError: 'y'"):
'{x} {y}'.format_map({'x': 1})

# >>> '{x} {y}'.format_map({'x': 1, 'z': 2})
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# KeyError: 'y'
with AssertRaises(KeyError, msg="KeyError: 'y'"):
'{x} {y}'.format_map({'x': 1, 'z': 2})

# >>> '{{literal}}'.format_map('foo')
# '{literal}'
assert '{{literal}}'.format_map('foo') == '{literal}'
51 changes: 51 additions & 0 deletions vm/src/obj/objstr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,32 @@ impl PyString {
}
}

/// S.format_map(mapping) -> str
///
/// Return a formatted version of S, using substitutions from mapping.
/// The substitutions are identified by braces ('{' and '}').
#[pymethod]
fn format_map(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
if args.args.len() != 2 {
return Err(vm.new_type_error(format!(
"format_map() takes exactly one argument ({} given)",
args.args.len() - 1
)));
}

let zelf = &args.args[0];
let format_string_text = get_value(zelf);
match FormatString::from_str(format_string_text.as_str()) {
Ok(format_string) => perform_format_map(vm, &format_string, &args.args[1]),
Err(err) => match err {
FormatParseError::UnmatchedBracket => {
Err(vm.new_value_error("expected '}' before end of string".to_string()))
}
_ => Err(vm.new_value_error("Unexpected error parsing format string".to_string())),
},
}
}

/// Return a titlecased version of the string where words start with an
/// uppercase character and the remaining characters are lowercase.
#[pymethod]
Expand Down Expand Up @@ -1590,6 +1616,31 @@ fn perform_format(
Ok(vm.ctx.new_str(final_string))
}

fn perform_format_map(
vm: &VirtualMachine,
format_string: &FormatString,
dict: &PyObjectRef,
) -> PyResult {
let mut final_string = String::new();
for part in &format_string.format_parts {
let result_string: String = match part {
FormatPart::AutoSpec(_) | FormatPart::IndexSpec(_, _) => {
return Err(
vm.new_value_error("Format string contains positional fields".to_string())
);
}
FormatPart::KeywordSpec(keyword, format_spec) => {
let argument = dict.get_item(keyword, &vm)?;
let result = call_object_format(vm, argument.clone(), &format_spec)?;
get_value(&result)
}
FormatPart::Literal(literal) => literal.clone(),
};
final_string.push_str(&result_string);
}
Ok(vm.ctx.new_str(final_string))
}

impl PySliceableSequence for String {
type Sliced = String;

Expand Down