Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Dont fail on '{!r}'.format(...)
  • Loading branch information
alanjds committed Jun 9, 2019
commit 65d009382213fbdd112679a97446ed3194a5530a
3 changes: 3 additions & 0 deletions tests/snippets/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@
assert "{0} {1}".format(2,3) == "2 3"
assert "--{:s>4}--".format(1) == "--sss1--"
assert "{keyword} {0}".format(1, keyword=2) == "2 1"
assert "repr() shows quotes: {!r}; str() doesn't: {!s}".format(
'test1', 'test2'
) == "repr() shows quotes: 'test1'; str() doesn't: test2", 'Output: {!r}, {!s}'.format('test1', 'test2')

assert 'a' < 'b'
assert 'a' <= 'b'
Expand Down
11 changes: 11 additions & 0 deletions vm/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,17 @@ impl FormatString {
String::new()
};

// On parts[0] can still be the preconversor (!r, !s, !a)
let parts: Vec<&str> = arg_part .splitn(2, '!').collect();
// before the bang is a keyword or arg index, after the comma is maybe a conversor spec.
let arg_part = parts[0];

let preconversor_spec = if parts.len() > 1 {
parts[1].to_string()
} else {
String::new()
};

if arg_part.is_empty() {
return Ok(FormatPart::AutoSpec(format_spec));
}
Expand Down