summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_format.py
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2012-10-01 22:33:47 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2012-10-01 22:33:47 (GMT)
commit621ef3d84f842a10dc9cb2af5ab9555b1663b79e (patch)
tree8f7553021772f75f5abb239eeee36246aabf6319 /Lib/test/test_format.py
parentfd0d3e5d25cf9dcb751a329cf390388e0dbd8da2 (diff)
downloadcpython-621ef3d84f842a10dc9cb2af5ab9555b1663b79e.zip
cpython-621ef3d84f842a10dc9cb2af5ab9555b1663b79e.tar.gz
cpython-621ef3d84f842a10dc9cb2af5ab9555b1663b79e.tar.bz2
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
Diffstat (limited to 'Lib/test/test_format.py')
-rw-r--r--Lib/test/test_format.py16
1 files changed, 16 insertions, 0 deletions
diff --git a/Lib/test/test_format.py b/Lib/test/test_format.py
index b6e2540..e6b0d20 100644
--- a/Lib/test/test_format.py
+++ b/Lib/test/test_format.py
@@ -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():