]> granicus.if.org Git - python/commitdiff
Close issue23467: add %r compatibility to bytes and bytearray
authorEthan Furman <ethan@stoneleaf.us>
Wed, 11 Mar 2015 15:17:00 +0000 (08:17 -0700)
committerEthan Furman <ethan@stoneleaf.us>
Wed, 11 Mar 2015 15:17:00 +0000 (08:17 -0700)
Doc/library/stdtypes.rst
Lib/test/test_format.py
Objects/bytesobject.c

index f79c4161ac863175bca9f9be83bc6b891f550cc3..af0a85eb544300a7f8abe02245e68f1d9bd320fd 100644 (file)
@@ -3165,7 +3165,7 @@ The conversion types are:
 +------------+-----------------------------------------------------+-------+
 | ``'o'``    | Signed octal value.                                 | \(1)  |
 +------------+-----------------------------------------------------+-------+
-| ``'u'``    | Obsolete type -- it is identical to ``'d'``.        | \(7)  |
+| ``'u'``    | Obsolete type -- it is identical to ``'d'``.        | \(8)  |
 +------------+-----------------------------------------------------+-------+
 | ``'x'``    | Signed hexadecimal (lowercase).                     | \(2)  |
 +------------+-----------------------------------------------------+-------+
@@ -3200,6 +3200,9 @@ The conversion types are:
 | ``'a'``    | Bytes (converts any Python object using             | \(5)  |
 |            | ``repr(obj).encode('ascii','backslashreplace)``).   |       |
 +------------+-----------------------------------------------------+-------+
+| ``'r'``    | ``'r'`` is an alias for ``'a'`` and should only     | \(7)  |
+|            | be used for Python2/3 code bases.                   |       |
++------------+-----------------------------------------------------+-------+
 | ``'%'``    | No argument is converted, results in a ``'%'``      |       |
 |            | character in the result.                            |       |
 +------------+-----------------------------------------------------+-------+
@@ -3238,6 +3241,9 @@ Notes:
    ``b'%s'`` is deprecated, but will not be removed during the 3.x series.
 
 (7)
+   ``b'%r'`` is deprecated, but will not be removed during the 3.x series.
+
+(8)
    See :pep:`237`.
 
 .. note::
index 9d1f5d33029c372a0b398c11884d4bd86c1fa37f..e1ea33fbbde348f9664f54a54fa782b6e535d1c6 100644 (file)
@@ -310,6 +310,11 @@ class FormatTest(unittest.TestCase):
         testcommon(b"%a", b"ghi", b"b'ghi'")
         testcommon(b"%a", "jkl", b"'jkl'")
         testcommon(b"%a", "\u0544", b"'\\u0544'")
+        # %r is an alias for %a
+        testcommon(b"%r", 3.14, b"3.14")
+        testcommon(b"%r", b"ghi", b"b'ghi'")
+        testcommon(b"%r", "jkl", b"'jkl'")
+        testcommon(b"%r", "\u0544", b"'\\u0544'")
 
         # Test exception for unknown format characters, etc.
         if verbose:
index 2ceba486f00866fef689fff420d86c580c29b9d6..e0ac1ab7644fb4f2cc166213d5673b42bf07159c 100644 (file)
@@ -720,6 +720,8 @@ _PyBytes_Format(PyObject *format, PyObject *args)
                 pbuf = "%";
                 len = 1;
                 break;
+            case 'r':
+                // %r is only for 2/3 code; 3 only code should use %a
             case 'a':
                 temp = PyObject_ASCII(v);
                 if (temp == NULL)