[Jython-checkins] jython: time.strftime now always returns a bytestring

jim.baker jython-checkins at python.org
Thu Apr 2 05:00:29 CEST 2015


https://hg.python.org/jython/rev/c06821df7422
changeset:   7639:c06821df7422
user:        Jim Baker <jim.baker at rackspace.com>
date:        Wed Apr 01 21:00:00 2015 -0600
summary:
  time.strftime now always returns a bytestring

Running under certain locales with formats like %c (date+time) or %a
(weekday) produces output like "日 3/29 14:55:13 2015" (as seen with
ja_JP encoding); "日" is the abbreviated form of Sunday in
Japanese. Prior to this commit, Jython would emit this as Unicode,
however, Python code that attempts to print timestamps may expect that
bytestrings are always produced.

Jython 3.x is the correct solution of course.

Fixes http://bugs.jython.org/issue2301

files:
  Lib/test/test_os_jy.py                |  13 +++++++++++++
  src/org/python/core/Py.java           |   9 +++++++++
  src/org/python/modules/time/Time.java |   4 ++--
  3 files changed, 24 insertions(+), 2 deletions(-)


diff --git a/Lib/test/test_os_jy.py b/Lib/test/test_os_jy.py
--- a/Lib/test/test_os_jy.py
+++ b/Lib/test/test_os_jy.py
@@ -338,6 +338,19 @@
                     env=newenv),
                 "2015-01-22 00:00:00\n")
 
+    def test_strftime_japanese_locale(self):
+        # Verifies fix of http://bugs.jython.org/issue2301 - produces
+        # UTF-8 encoded output per what CPython does, rather than Unicode.
+        # We will revisit in Jython 3.x!
+        self.get_installed_locales("ja_JP.UTF-8")
+        self.assertEqual(
+            subprocess.check_output(
+                [sys.executable, 
+                 "-J-Duser.country=JP", "-J-Duser.language=ja",
+                 "-c",
+                 "import time; print repr(time.strftime('%c', (2015, 3, 29, 14, 55, 13, 6, 88, 0)))"]),
+            "'\\xe6\\x97\\xa5 3 29 14:55:13 2015'\n")
+        
 
 class SystemTestCase(unittest.TestCase):
 
diff --git a/src/org/python/core/Py.java b/src/org/python/core/Py.java
--- a/src/org/python/core/Py.java
+++ b/src/org/python/core/Py.java
@@ -660,6 +660,15 @@
         }
     }
 
+    public static PyString newStringUTF8(String s) {
+        if (CharMatcher.ASCII.matchesAllOf(s)) {
+            // ascii of course is a subset of UTF-8
+            return Py.newString(s);
+        } else {
+            return Py.newString(codecs.PyUnicode_EncodeUTF8(s, null));
+        }
+    }
+
     public static PyStringMap newStringMap() {
         // enable lazy bootstrapping (see issue #1671)
         if (!PyType.hasBuilder(PyStringMap.class)) {
diff --git a/src/org/python/modules/time/Time.java b/src/org/python/modules/time/Time.java
--- a/src/org/python/modules/time/Time.java
+++ b/src/org/python/modules/time/Time.java
@@ -671,7 +671,7 @@
             i++;
         }
         // FIXME: This have problems with localized data:
-        //        $ LANG=es_ES.UTF-8 jythont -c "import time; print time.strftime('%A')"
+        //        $ LANG=es_ES.UTF-8 jython -c "import time; print time.strftime('%A')"
         //        s?bado
         //
         //        On the other hand, returning unicode would break some doctests
@@ -681,7 +681,7 @@
         //        os.environ)
         //
         // TODO:  Check how CPython deals with this problem.
-        return Py.newStringOrUnicode(s);
+        return Py.newStringUTF8(s);
     }
 
 

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


More information about the Jython-checkins mailing list