[Python-checkins] [3.11] gh-104018: disallow "z" format specifier in %-format of byte strings (GH-104033) (#104058)

mdickinson webhook-mailer at python.org
Mon May 1 16:18:36 EDT 2023


https://github.com/python/cpython/commit/10db28bfcf2b949e13312d0d2f1b3dd3c9585e69
commit: 10db28bfcf2b949e13312d0d2f1b3dd3c9585e69
branch: 3.11
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: mdickinson <dickinsm at gmail.com>
date: 2023-05-01T20:18:29Z
summary:

[3.11] gh-104018: disallow "z" format specifier in %-format of byte strings (GH-104033) (#104058)

gh-104018: disallow "z" format specifier in %-format of byte strings (GH-104033)

PEP-0682 specified that %-formatting would not support the "z" specifier,
but it was unintentionally allowed for bytes. This PR makes use of the "z"
flag an error for %-formatting in a bytestring.

Issue: GH-104018

---------

(cherry picked from commit 3ed8c882902a6982fd67e898a5b8a2d619fb5ddf)

Co-authored-by: John Belmonte <john at neggie.net>
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>

files:
A Misc/NEWS.d/next/Core and Builtins/2023-05-01-12-03-52.gh-issue-104018.PFxGS4.rst
M Lib/test/test_format.py
M Objects/bytesobject.c

diff --git a/Lib/test/test_format.py b/Lib/test/test_format.py
index 69b0d5f1c5a5..6fa49dbc0b73 100644
--- a/Lib/test/test_format.py
+++ b/Lib/test/test_format.py
@@ -619,6 +619,8 @@ def test_specifier_z_error(self):
         error_msg = re.escape("unsupported format character 'z'")
         with self.assertRaisesRegex(ValueError, error_msg):
             "%z.1f" % 0  # not allowed in old style string interpolation
+        with self.assertRaisesRegex(ValueError, error_msg):
+            b"%z.1f" % 0
 
 
 if __name__ == "__main__":
diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-05-01-12-03-52.gh-issue-104018.PFxGS4.rst b/Misc/NEWS.d/next/Core and Builtins/2023-05-01-12-03-52.gh-issue-104018.PFxGS4.rst
new file mode 100644
index 000000000000..f3cadaee0e32
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2023-05-01-12-03-52.gh-issue-104018.PFxGS4.rst	
@@ -0,0 +1 @@
+Disallow the "z" format specifier in %-format of bytes objects.
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
index 8dd1a2d52467..61cde0ef34bf 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -714,7 +714,6 @@ _PyBytes_FormatEx(const char *format, Py_ssize_t format_len,
                 case ' ': flags |= F_BLANK; continue;
                 case '#': flags |= F_ALT; continue;
                 case '0': flags |= F_ZERO; continue;
-                case 'z': flags |= F_NO_NEG_0; continue;
                 }
                 break;
             }



More information about the Python-checkins mailing list