[Jython-checkins] jython: Implement PEP 378 - format specifier for thousands separator.

nicholas.riley jython-checkins at python.org
Wed Mar 21 21:10:02 CET 2012


http://hg.python.org/jython/rev/2977c0028748
changeset:   6455:2977c0028748
user:        Nicholas Riley <njriley at illinois.edu>
date:        Wed Mar 21 15:47:34 2012 -0400
summary:
  Implement PEP 378 - format specifier for thousands separator.

Note that this doesn't work with floating point formats yet because __format__ isn't implemented for floats (see http://bugs.jython.org/issue1718).

files:
  src/org/python/core/stringlib/InternalFormatSpec.java       |  1 +
  src/org/python/core/stringlib/InternalFormatSpecParser.java |  8 ++++++++
  tests/java/org/python/core/StringFormatTest.java            |  5 +++++
  3 files changed, 14 insertions(+), 0 deletions(-)


diff --git a/src/org/python/core/stringlib/InternalFormatSpec.java b/src/org/python/core/stringlib/InternalFormatSpec.java
--- a/src/org/python/core/stringlib/InternalFormatSpec.java
+++ b/src/org/python/core/stringlib/InternalFormatSpec.java
@@ -9,6 +9,7 @@
     public boolean alternate;
     public char sign;
     public int width = -1;
+    public boolean thousands_separators;
     public int precision = -1;
     public char type;
 
diff --git a/src/org/python/core/stringlib/InternalFormatSpecParser.java b/src/org/python/core/stringlib/InternalFormatSpecParser.java
--- a/src/org/python/core/stringlib/InternalFormatSpecParser.java
+++ b/src/org/python/core/stringlib/InternalFormatSpecParser.java
@@ -48,6 +48,10 @@
             index++;
         }
         result.width = getInteger();
+        if (isAt(",")) {
+            result.thousands_separators = true;
+            index++;
+        }
         if (isAt(".")) {
             index++;
             result.precision = getInteger();
@@ -61,6 +65,10 @@
                 throw new IllegalArgumentException("Invalid conversion specification");
             }
         }
+        if (result.thousands_separators) {
+            if ("defgEG%F\0".indexOf(result.type) == -1)
+                throw new IllegalArgumentException("Cannot specify ',' with '" + result.type + "'.");
+        }
         return result;
     }
 
diff --git a/tests/java/org/python/core/StringFormatTest.java b/tests/java/org/python/core/StringFormatTest.java
--- a/tests/java/org/python/core/StringFormatTest.java
+++ b/tests/java/org/python/core/StringFormatTest.java
@@ -72,6 +72,11 @@
         spec.type = 'b';
         assertEquals("1111011", PyInteger.formatIntOrLong(123, spec));
 
+        spec.thousands_separators = true;
+        spec.type = 'd';
+        assertEquals("1,234", PyInteger.formatIntOrLong(1234, spec));
+        spec.thousands_separators = false;
+
         spec.alternate = true;
         spec.type = 'o';
         assertEquals("0o173", PyInteger.formatIntOrLong(123, spec));

-- 
Repository URL: http://hg.python.org/jython


More information about the Jython-checkins mailing list