changeset: 94943:611fa301b807 user: Ethan Furman date: Wed Mar 11 08:17:00 2015 -0700 files: Doc/library/stdtypes.rst Lib/test/test_format.py Objects/bytesobject.c description: Close issue23467: add %r compatibility to bytes and bytearray diff -r 6c6c873c0059 -r 611fa301b807 Doc/library/stdtypes.rst --- a/Doc/library/stdtypes.rst Tue Mar 10 19:40:37 2015 -0700 +++ b/Doc/library/stdtypes.rst Wed Mar 11 08:17:00 2015 -0700 @@ -3165,7 +3165,7 @@ +------------+-----------------------------------------------------+-------+ | ``'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 @@ | ``'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 @@ ``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:: diff -r 6c6c873c0059 -r 611fa301b807 Lib/test/test_format.py --- a/Lib/test/test_format.py Tue Mar 10 19:40:37 2015 -0700 +++ b/Lib/test/test_format.py Wed Mar 11 08:17:00 2015 -0700 @@ -310,6 +310,11 @@ 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: diff -r 6c6c873c0059 -r 611fa301b807 Objects/bytesobject.c --- a/Objects/bytesobject.c Tue Mar 10 19:40:37 2015 -0700 +++ b/Objects/bytesobject.c Wed Mar 11 08:17:00 2015 -0700 @@ -720,6 +720,8 @@ 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)