Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 62e977f

Browse files
committed
Close issue23467: add %r compatibility to bytes and bytearray
1 parent 6dd20c2 commit 62e977f

3 files changed

Lines changed: 14 additions & 1 deletion

File tree

Doc/library/stdtypes.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3165,7 +3165,7 @@ The conversion types are:
31653165
+------------+-----------------------------------------------------+-------+
31663166
| ``'o'`` | Signed octal value. | \(1) |
31673167
+------------+-----------------------------------------------------+-------+
3168-
| ``'u'`` | Obsolete type -- it is identical to ``'d'``. | \(7) |
3168+
| ``'u'`` | Obsolete type -- it is identical to ``'d'``. | \(8) |
31693169
+------------+-----------------------------------------------------+-------+
31703170
| ``'x'`` | Signed hexadecimal (lowercase). | \(2) |
31713171
+------------+-----------------------------------------------------+-------+
@@ -3200,6 +3200,9 @@ The conversion types are:
32003200
| ``'a'`` | Bytes (converts any Python object using | \(5) |
32013201
| | ``repr(obj).encode('ascii','backslashreplace)``). | |
32023202
+------------+-----------------------------------------------------+-------+
3203+
| ``'r'`` | ``'r'`` is an alias for ``'a'`` and should only | \(7) |
3204+
| | be used for Python2/3 code bases. | |
3205+
+------------+-----------------------------------------------------+-------+
32033206
| ``'%'`` | No argument is converted, results in a ``'%'`` | |
32043207
| | character in the result. | |
32053208
+------------+-----------------------------------------------------+-------+
@@ -3238,6 +3241,9 @@ Notes:
32383241
``b'%s'`` is deprecated, but will not be removed during the 3.x series.
32393242

32403243
(7)
3244+
``b'%r'`` is deprecated, but will not be removed during the 3.x series.
3245+
3246+
(8)
32413247
See :pep:`237`.
32423248

32433249
.. note::

Lib/test/test_format.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,11 @@ def __bytes__(self):
310310
testcommon(b"%a", b"ghi", b"b'ghi'")
311311
testcommon(b"%a", "jkl", b"'jkl'")
312312
testcommon(b"%a", "\u0544", b"'\\u0544'")
313+
# %r is an alias for %a
314+
testcommon(b"%r", 3.14, b"3.14")
315+
testcommon(b"%r", b"ghi", b"b'ghi'")
316+
testcommon(b"%r", "jkl", b"'jkl'")
317+
testcommon(b"%r", "\u0544", b"'\\u0544'")
313318

314319
# Test exception for unknown format characters, etc.
315320
if verbose:

Objects/bytesobject.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,8 @@ _PyBytes_Format(PyObject *format, PyObject *args)
720720
pbuf = "%";
721721
len = 1;
722722
break;
723+
case 'r':
724+
// %r is only for 2/3 code; 3 only code should use %a
723725
case 'a':
724726
temp = PyObject_ASCII(v);
725727
if (temp == NULL)

0 commit comments

Comments
 (0)