[Jython-checkins] jython: Correct handling of str.format(unicode)

jeff.allen jython-checkins at python.org
Sun Dec 7 00:47:34 CET 2014


https://hg.python.org/jython/rev/7e2e9537565f
changeset:   7432:7e2e9537565f
user:        Jeff Allen <ja.py at farowl.co.uk>
date:        Sat Dec 06 23:36:25 2014 +0000
summary:
  Correct handling of str.format(unicode)

Compatibly with CPython, we narrow implicitly to a str, using
the default enbcoding (which will raise UnicodeEncodeError for
characters >127.

files:
  Lib/test/test_format_jy.py        |  7 +++++++
  src/org/python/core/PyString.java |  6 ++++++
  2 files changed, 13 insertions(+), 0 deletions(-)


diff --git a/Lib/test/test_format_jy.py b/Lib/test/test_format_jy.py
--- a/Lib/test/test_format_jy.py
+++ b/Lib/test/test_format_jy.py
@@ -55,6 +55,13 @@
 class FormatMisc(unittest.TestCase):
     # Odd tests Jython used to fail
 
+    def test_str_format_unicode(self):
+        # Check unicode is down-converted to str silently if possible
+        self.assertEqual("full half hour", "full {:s} hour".format(u"half"))
+        self.assertEqual("full \xbd hour", "full {:s} hour".format("\xbd"))
+        self.assertRaises(UnicodeEncodeError, "full {:s} hour".format, u"\xbd")
+        self.assertEqual(u"full \xbd hour", u"full {:s} hour".format(u"\xbd"))
+
     def test_mixtures(self) :
         # Check formatting to a common buffer in PyString
         result = 'The cube of 0.5 -0.866j is -1 to 0.01%.'
diff --git a/src/org/python/core/PyString.java b/src/org/python/core/PyString.java
--- a/src/org/python/core/PyString.java
+++ b/src/org/python/core/PyString.java
@@ -3965,6 +3965,12 @@
                     throw Py.ValueError("Unknown conversion specifier " + chunk.conversion);
                 }
 
+                // Check for "{}".format(u"abc")
+                if (fieldObj instanceof PyUnicode && !(this instanceof PyUnicode)) {
+                    // Down-convert to PyString, at the risk of raising UnicodeEncodingError
+                    fieldObj = ((PyUnicode)fieldObj).__str__();
+                }
+
                 // The format_spec may be simple, or contained nested replacement fields.
                 String formatSpec = chunk.formatSpec;
                 if (chunk.formatSpecNeedsExpanding) {

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


More information about the Jython-checkins mailing list