[Python-checkins] cpython (merge 3.5 -> default): Merge 3.5

victor.stinner python-checkins at python.org
Tue Mar 1 16:08:24 EST 2016


https://hg.python.org/cpython/rev/7e48300c7f3b
changeset:   100382:7e48300c7f3b
parent:      100380:aafbe5042024
parent:      100381:6643c5cc9797
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Tue Mar 01 22:07:53 2016 +0100
summary:
  Merge 3.5

files:
  Lib/test/test_unicode.py |  14 ++++++++++----
  Objects/unicodeobject.c  |  25 ++++++++++++++-----------
  2 files changed, 24 insertions(+), 15 deletions(-)


diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py
--- a/Lib/test/test_unicode.py
+++ b/Lib/test/test_unicode.py
@@ -341,16 +341,22 @@
                          "[XXX]")
         self.assertEqual("[a]".translate(str.maketrans({'a': '\xe9'})),
                          "[\xe9]")
+        self.assertEqual('axb'.translate(str.maketrans({'a': None, 'b': '123'})),
+                         "x123")
+        self.assertEqual('axb'.translate(str.maketrans({'a': None, 'b': '\xe9'})),
+                         "x\xe9")
+
+        # test non-ASCII (don't take the fast-path)
         self.assertEqual("[a]".translate(str.maketrans({'a': '<\xe9>'})),
                          "[<\xe9>]")
         self.assertEqual("[\xe9]".translate(str.maketrans({'\xe9': 'a'})),
                          "[a]")
         self.assertEqual("[\xe9]".translate(str.maketrans({'\xe9': None})),
                          "[]")
-        self.assertEqual('axb'.translate(str.maketrans({'a': None, 'b': '123'})),
-                         "x123")
-        self.assertEqual('axb'.translate(str.maketrans({'a': None, 'b': '\xe9'})),
-                         "x\xe9")
+        self.assertEqual("[\xe9]".translate(str.maketrans({'\xe9': '123'})),
+                         "[123]")
+        self.assertEqual("[a\xe9]".translate(str.maketrans({'a': '<\u20ac>'})),
+                         "[<\u20ac>\xe9]")
 
         # invalid Unicode characters
         invalid_char = 0x10ffff+1
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -8791,10 +8791,6 @@
     Py_UCS1 *in, *end, *out;
     int res = 0;
 
-    if (PyUnicode_READY(input) == -1)
-        return -1;
-    if (!PyUnicode_IS_ASCII(input))
-        return 0;
     len = PyUnicode_GET_LENGTH(input);
 
     memset(ascii_table, 0xff, 128);
@@ -8877,13 +8873,20 @@
 
     ignore = (errors != NULL && strcmp(errors, "ignore") == 0);
 
-    res = unicode_fast_translate(input, mapping, &writer, ignore, &i);
-    if (res < 0) {
-        _PyUnicodeWriter_Dealloc(&writer);
-        return NULL;
-    }
-    if (res == 1)
-        return _PyUnicodeWriter_Finish(&writer);
+    if (PyUnicode_READY(input) == -1)
+        return NULL;
+    if (PyUnicode_IS_ASCII(input)) {
+        res = unicode_fast_translate(input, mapping, &writer, ignore, &i);
+        if (res < 0) {
+            _PyUnicodeWriter_Dealloc(&writer);
+            return NULL;
+        }
+        if (res == 1)
+            return _PyUnicodeWriter_Finish(&writer);
+    }
+    else {
+        i = 0;
+    }
 
     while (i<size) {
         /* try to encode it */

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


More information about the Python-checkins mailing list