Issue #15609: Optimize str%args for integer argument

- Use _PyLong_FormatWriter() instead of formatlong() when possible, to avoid
   a temporary buffer
 - Enable the fast path when width is smaller or equals to the length,
   and when the precision is bigger or equals to the length
 - Add unit tests!
 - formatlong() uses PyUnicode_Resize() instead of _PyUnicode_FromASCII()
   to resize the output string
This commit is contained in:
Victor Stinner 2012-10-02 00:33:47 +02:00
parent fd0d3e5d25
commit 621ef3d84f
3 changed files with 119 additions and 68 deletions

View file

@ -307,6 +307,22 @@ class FormatTest(unittest.TestCase):
finally:
locale.setlocale(locale.LC_ALL, oldloc)
@support.cpython_only
def test_optimisations(self):
text = "abcde" # 5 characters
self.assertIs("%s" % text, text)
self.assertIs("%.5s" % text, text)
self.assertIs("%.10s" % text, text)
self.assertIs("%1s" % text, text)
self.assertIs("%5s" % text, text)
self.assertIs("{0}".format(text), text)
self.assertIs("{0:s}".format(text), text)
self.assertIs("{0:.5s}".format(text), text)
self.assertIs("{0:.10s}".format(text), text)
self.assertIs("{0:1s}".format(text), text)
self.assertIs("{0:5s}".format(text), text)
def test_main():