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

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