]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-104018: disallow "z" format specifier in %-format of byte strings (GH-104033)
authorJohn Belmonte <john@neggie.net>
Mon, 1 May 2023 19:47:14 +0000 (04:47 +0900)
committerGitHub <noreply@github.com>
Mon, 1 May 2023 19:47:14 +0000 (20:47 +0100)
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: #104018

---------

Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Lib/test/test_format.py
Misc/NEWS.d/next/Core and Builtins/2023-05-01-12-03-52.gh-issue-104018.PFxGS4.rst [new file with mode: 0644]
Objects/bytesobject.c

index 69b0d5f1c5a5157c510cee9548154cfbb450e494..6fa49dbc0b730ca8380f0c804e0faa22534ea566 100644 (file)
@@ -619,6 +619,8 @@ class FormatTest(unittest.TestCase):
         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 (file)
index 0000000..f3cadae
--- /dev/null
@@ -0,0 +1 @@
+Disallow the "z" format specifier in %-format of bytes objects.
index 27b2ad4f2cb38f215709a172d52333404cfa9e25..e7e85cc19cda75e9b464c6acee0b295c6bbd251a 100644 (file)
@@ -705,7 +705,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;
             }