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
builtin __format__ methods cannot fill with \x00 char #56755
Comments
This gives me "foo " instead of expected "foo\x00\x00\x00" : "{0:\x00<6}".format('foo') |
\x00 is used as a flag internally meaning "use the default fill character". That's clearly a bug. I'll look at fixing it. I think there are other places in the built in __format__ functions where special values are used instead of flags. I'll review those as well. Thanks for the report! |
This patch removes the special meaning for \x00 and defines the default padding character (' ') in parse_internal_render_format_spec. Test included. Maybe the default padding character should be defined elsewhere? |
Oops, sorry. Above patch was overly buggy. Please just ignore it. |
Here's the patch. Same rationale as above (removed the special meaning of '\x00', default specified in parse_internal_render_format_spec). Sorry about the mess again! |
Patch looks good at first glance. I'll review it some more today and commit it. Thanks! |
I finally got around to reviewing the patch. A couple of comments:
>>> format(3, '\x00<6')
'3 '
>>> format(3., '\x00<6')
'3.0 '
>>> format('3', '\x00<6')
'3\x00\x00\x00\x00\x00'
>>> format(3+1j, '\x00<6')
'(3+1j)'
[38654 refs]
>>> format(3+1j, '\x00<10')
'(3+1j) ' I think the fix is basically the same as str.__format__ (but in format_int_or_long_internal, format_float_internal, and format_complex_internal). I tried that and it worked, but I haven't had time to write tests for them. If you (Davide) can do that, great. Otherwise I'll try and grab some time this week. Changing the subject to match the wider scope of the problem. |
bpo-17705 has been closed as a duplicate of this issue. |
The patch looks good to me. |
New changeset 520ce42ba2b8 by Eric V. Smith in branch '2.7': |
New changeset 7c484551bce1 by Eric V. Smith in branch '3.4': New changeset bd90e68dc81f by Eric V. Smith in branch 'default': |
Fixed in 2.7, 3.4, 3.5. |
I don't understand why it works with "<", "=" or ">": >>> "{0:\x00<6d}".format(123)
'123\x00\x00\x00' But not without: >>> "{0:\x006d}".format(123)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Invalid format specifier Compare it to: >>> "{0:6d}".format(123)
' 123'
>>> "{0:06d}".format(123)
'000123' |
For int, the spec is: So, for "06d", "0" is matched as the literal 0, "6" is matched as width, and "d" is matched as type. For "\x00<6d", "\x00" is matched as fill, "<" as align, "6" as width, and "d" as type. For "\x006d", there's no align. So "\x00" cannot match as fill. "\x00" doesn't match anything else, so it's an invalid format specifier, thus the exception. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: