-
-
Notifications
You must be signed in to change notification settings - Fork 30.6k
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
bpo-28307: Optimize C-style formatting of numbers #26160
base: main
Are you sure you want to change the base?
bpo-28307: Optimize C-style formatting of numbers #26160
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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