bpo-28307: Optimize C-style formatting of numbers#26160
bpo-28307: Optimize C-style formatting of numbers#26160serhiy-storchaka wants to merge 8 commits intopython:mainfrom
Conversation
Support format codes %d, %o, %x, %f, %e, %g, etc.
Doc/library/ast.rst
Outdated
| * ``ord('r')``: call :func:`repr` before formatting (``!r``) | ||
| * ``ord('a')``: call :func:`ascii` before formatting (``!a``) | ||
| * ``ord('d')``: convert to :class:`int` with truncating | ||
| (for internal use only) |
There was a problem hiding this comment.
What does "(for internal use only)" mean? What restricts this to being used one way or another?
I think we need different text here. As f-strings don't support !d, !i, and !f conversions directly we should just say that rather than document values that may be encountered but imply that people are not supposed to use them.
They're going to use them in their own ast ndoes and in their own bytecode generation (for people who do that sort of thing...) no matter what we say.
There was a problem hiding this comment.
Removed (for internal use only).
Lib/test/test_peepholer.py
Outdated
| def test_format(self): | ||
| flags = '-+ #0' | ||
| testcases = [ | ||
| *product(('', '1234',), 'sra'), |
There was a problem hiding this comment.
Test case(s) that include other % formatting directives within the value would be good.
We want to make sure injection attacks don't exist if this happened to get implemented in the wrong serialized actions manner.
untrusted = '%% %s'
value = 'foo %s bar' % untrusted
results in value == 'foo %% %s bar' rather than raising an exception about tuple size or converting %% into %.
This might make a good test of its own rather than shoehorning it into this one given some complexities of where % directives that'll be peepholed can appear in strings and in combination with others that may or may not be.
There was a problem hiding this comment.
Added more tests. Caught and fixed conversion of non-numbers to int and float ('%d' % '123' did not raise exception before).
|
This PR is stale because it has been open for 30 days with no activity. |
|
This now has merge conflicts. |
Support format codes %d, %o, %x, %f, %e, %g, etc.
https://bugs.python.org/issue28307