-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
gh-124294: str.format() raises misleading error when using negative indices #124372
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
Changes from 5 commits
2f24f22
470fd35
201e4b1
0d67891
7ec5eb8
080802a
8eeca72
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1388,6 +1388,10 @@ def __repr__(self): | |
| self.assertEqual("{!s}".format(n), 'N(data)') | ||
| self.assertRaises(TypeError, "{}".format, n) | ||
|
|
||
| # String index for sequences | ||
| self.assertRaises(TypeError, "{[s]}".format, "abc") | ||
| self.assertRaises(TypeError, "{[-1]}".format, "abc") | ||
|
||
|
|
||
| def test_format_map(self): | ||
| self.assertEqual(''.format_map({}), '') | ||
| self.assertEqual('a'.format_map({}), 'a') | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| More informative error message in :func:`str.format` when passing negative | ||
| integer or string index to sequence. |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -462,8 +462,20 @@ get_field_object(SubString *input, PyObject *args, PyObject *kwargs, | |||||
| tmp = getattr(obj, &name); | ||||||
| else | ||||||
| /* getitem lookup "[]" */ | ||||||
| if (index == -1) | ||||||
| if (index == -1) { | ||||||
skirpichev marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| if (PySequence_Check(obj)) { | ||||||
| /* string index can't be passed to sequence */ | ||||||
| PyObject *str = SubString_new_object(&name); | ||||||
skirpichev marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| if (str != NULL) | ||||||
|
||||||
| if (str != NULL) | |
| if (str) |
Also beware PEP 7: "braces are required everywhere, even where C permits them to be omitted".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe you could delete this comment since the user won't see it anyway.