Blame SOURCES/00213-pep466-pyunicode_fromformat-fix-formats.patch

f63228
f63228
# HG changeset patch
f63228
# User Victor Stinner <victor.stinner@gmail.com>
f63228
# Date 1406673545 -7200
f63228
# Node ID 263701e0b77e3160bc6a835087f838bd6b24092a
f63228
# Parent  6c47c6d2033e20e9b35f1d22e0e797961d6e680f
f63228
Issue #22023: Fix %S, %R and %V formats of PyUnicode_FromFormat().
f63228
f63228
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
f63228
--- a/Objects/unicodeobject.c
f63228
+++ b/Objects/unicodeobject.c
f63228
@@ -690,7 +690,12 @@ makefmt(char *fmt, int longflag, int siz
f63228
     *fmt = '\0';
f63228
 }
f63228
 
f63228
-#define appendstring(string) {for (copy = string;*copy;) *s++ = *copy++;}
f63228
+#define appendstring(string) \
f63228
+    do { \
f63228
+        for (copy = string;*copy; copy++) { \
f63228
+            *s++ = (unsigned char)*copy; \
f63228
+        } \
f63228
+    } while (0)
f63228
 
f63228
 PyObject *
f63228
 PyUnicode_FromFormatV(const char *format, va_list vargs)
f63228
@@ -845,7 +850,7 @@ PyUnicode_FromFormatV(const char *format
f63228
                 str = PyObject_Str(obj);
f63228
                 if (!str)
f63228
                     goto fail;
f63228
-                n += PyUnicode_GET_SIZE(str);
f63228
+                n += PyString_GET_SIZE(str);
f63228
                 /* Remember the str and switch to the next slot */
f63228
                 *callresult++ = str;
f63228
                 break;
f63228
@@ -1006,15 +1011,10 @@ PyUnicode_FromFormatV(const char *format
f63228
             case 'S':
f63228
             case 'R':
f63228
             {
f63228
-                Py_UNICODE *ucopy;
f63228
-                Py_ssize_t usize;
f63228
-                Py_ssize_t upos;
f63228
+                const char *str = PyString_AS_STRING(*callresult);
f63228
                 /* unused, since we already have the result */
f63228
                 (void) va_arg(vargs, PyObject *);
f63228
-                ucopy = PyUnicode_AS_UNICODE(*callresult);
f63228
-                usize = PyUnicode_GET_SIZE(*callresult);
f63228
-                for (upos = 0; upos
f63228
-                    *s++ = ucopy[upos++];
f63228
+                appendstring(str);
f63228
                 /* We're done with the unicode()/repr() => forget it */
f63228
                 Py_DECREF(*callresult);
f63228
                 /* switch to next unicode()/repr() result */
f63228
f63228
diff -up Python-2.7.5/Lib/test/test_unicode.py.uni Python-2.7.5/Lib/test/test_unicode.py
f63228
--- Python-2.7.5/Lib/test/test_unicode.py.uni	2015-02-24 13:37:01.704739438 +0100
f63228
+++ Python-2.7.5/Lib/test/test_unicode.py	2015-02-24 13:38:38.439482167 +0100
f63228
@@ -1633,6 +1633,119 @@ class UnicodeTest(
f63228
         self.assertEqual("%s" % u, u'__unicode__ overridden')
f63228
         self.assertEqual("{}".format(u), '__unicode__ overridden')
f63228
 
f63228
+    # Test PyUnicode_FromFormat()
f63228
+    def test_from_format(self):
f63228
+        test_support.import_module('ctypes')
f63228
+        from ctypes import (
f63228
+            pythonapi, py_object, sizeof,
f63228
+            c_int, c_long, c_longlong, c_ssize_t,
f63228
+            c_uint, c_ulong, c_ulonglong, c_size_t, c_void_p)
f63228
+        if sys.maxunicode == 0xffff:
f63228
+            name = "PyUnicodeUCS2_FromFormat"
f63228
+        else:
f63228
+            name = "PyUnicodeUCS4_FromFormat"
f63228
+        _PyUnicode_FromFormat = getattr(pythonapi, name)
f63228
+        _PyUnicode_FromFormat.restype = py_object
f63228
+
f63228
+        def PyUnicode_FromFormat(format, *args):
f63228
+            cargs = tuple(
f63228
+                py_object(arg) if isinstance(arg, unicode) else arg
f63228
+                for arg in args)
f63228
+            return _PyUnicode_FromFormat(format, *cargs)
f63228
+
f63228
+        def check_format(expected, format, *args):
f63228
+            text = PyUnicode_FromFormat(format, *args)
f63228
+            self.assertEqual(expected, text)
f63228
+
f63228
+        # ascii format, non-ascii argument
f63228
+        check_format(u'ascii\x7f=unicode\xe9',
f63228
+                     b'ascii\x7f=%U', u'unicode\xe9')
f63228
+
f63228
+        # non-ascii format, ascii argument: ensure that PyUnicode_FromFormatV()
f63228
+        # raises an error
f63228
+        #self.assertRaisesRegex(ValueError,
f63228
+        #    '^PyUnicode_FromFormatV\(\) expects an ASCII-encoded format '
f63228
+        #    'string, got a non-ASCII byte: 0xe9$',
f63228
+        #    PyUnicode_FromFormat, b'unicode\xe9=%s', u'ascii')
f63228
+
f63228
+        # test "%c"
f63228
+        check_format(u'\uabcd',
f63228
+                     b'%c', c_int(0xabcd))
f63228
+        if sys.maxunicode > 0xffff:
f63228
+            check_format(u'\U0010ffff',
f63228
+                         b'%c', c_int(0x10ffff))
f63228
+        with self.assertRaises(OverflowError):
f63228
+            PyUnicode_FromFormat(b'%c', c_int(0x110000))
f63228
+        # Issue #18183
f63228
+        if sys.maxunicode > 0xffff:
f63228
+            check_format(u'\U00010000\U00100000',
f63228
+                         b'%c%c', c_int(0x10000), c_int(0x100000))
f63228
+
f63228
+        # test "%"
f63228
+        check_format(u'%',
f63228
+                     b'%')
f63228
+        check_format(u'%',
f63228
+                     b'%%')
f63228
+        check_format(u'%s',
f63228
+                     b'%%s')
f63228
+        check_format(u'[%]',
f63228
+                     b'[%%]')
f63228
+        check_format(u'%abc',
f63228
+                     b'%%%s', b'abc')
f63228
+
f63228
+        # test %S
f63228
+        check_format(u"repr=abc",
f63228
+                     b'repr=%S', u'abc')
f63228
+
f63228
+        # test %R
f63228
+        check_format(u"repr=u'abc'",
f63228
+                     b'repr=%R', u'abc')
f63228
+
f63228
+        # test integer formats (%i, %d, %u)
f63228
+        check_format(u'010',
f63228
+                     b'%03i', c_int(10))
f63228
+        check_format(u'0010',
f63228
+                     b'%0.4i', c_int(10))
f63228
+        check_format(u'-123',
f63228
+                     b'%i', c_int(-123))
f63228
+
f63228
+        check_format(u'-123',
f63228
+                     b'%d', c_int(-123))
f63228
+        check_format(u'-123',
f63228
+                     b'%ld', c_long(-123))
f63228
+        check_format(u'-123',
f63228
+                     b'%zd', c_ssize_t(-123))
f63228
+
f63228
+        check_format(u'123',
f63228
+                     b'%u', c_uint(123))
f63228
+        check_format(u'123',
f63228
+                     b'%lu', c_ulong(123))
f63228
+        check_format(u'123',
f63228
+                     b'%zu', c_size_t(123))
f63228
+
f63228
+        # test long output
f63228
+        PyUnicode_FromFormat(b'%p', c_void_p(-1))
f63228
+
f63228
+        # test %V
f63228
+        check_format(u'repr=abc',
f63228
+                     b'repr=%V', u'abc', b'xyz')
f63228
+        check_format(u'repr=\xe4\xba\xba\xe6\xb0\x91',
f63228
+                     b'repr=%V', None, b'\xe4\xba\xba\xe6\xb0\x91')
f63228
+        check_format(u'repr=abc\xff',
f63228
+                     b'repr=%V', None, b'abc\xff')
f63228
+
f63228
+        # not supported: copy the raw format string. these tests are just here
f63228
+        # to check for crashs and should not be considered as specifications
f63228
+        check_format(u'%s',
f63228
+                     b'%1%s', b'abc')
f63228
+        check_format(u'%1abc',
f63228
+                     b'%1abc')
f63228
+        check_format(u'%+i',
f63228
+                     b'%+i', c_int(10))
f63228
+        check_format(u'%s',
f63228
+                     b'%.%s', b'abc')
f63228
+
f63228
+
f63228
     def test_encode_decimal(self):
f63228
         from _testcapi import unicode_encodedecimal
f63228
         self.assertEqual(unicode_encodedecimal(u'123'),