From 8c0f2f8346195492a01945311354a9950f0fc49b Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Tue, 11 Nov 2025 10:04:44 -0500 Subject: [PATCH 1/6] Document PyBytes_Repr(). --- Doc/c-api/bytes.rst | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Doc/c-api/bytes.rst b/Doc/c-api/bytes.rst index 865a9e5d2bf5d5..57c291b9517bf4 100644 --- a/Doc/c-api/bytes.rst +++ b/Doc/c-api/bytes.rst @@ -228,6 +228,23 @@ called with a non-bytes parameter. The function is :term:`soft deprecated`, use the :c:type:`PyBytesWriter` API instead. + +.. c:function:: PyObject *PyBytes_Repr(PyObject *bytes, int smartquotes) + + Get the string representation of *bytes*. This function is used to + implement :meth:`bytes.__repr__` in Python. + + If *smartquotes* is true, the representation will use a double-quoted string + instead of single-quoted string when single-quotes are present in *bytes*. + For example, the byte string ``'Python'`` would be represented as + ``b"'Python'"`` when *smartquotes* is true, or b'\'Python\'' when it is + false. + + On success, this function returns a :term:`strong reference` to a + :class:`str` object containing the representation. On failure, this + returns ``NULL`` with an exception set. + + .. _pybyteswriter: PyBytesWriter From 3c4660991915918ac8140ca9bd45b742eff9a739 Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Tue, 11 Nov 2025 10:21:29 -0500 Subject: [PATCH 2/6] Document PyBytes_DecodeEscape() --- Doc/c-api/bytes.rst | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Doc/c-api/bytes.rst b/Doc/c-api/bytes.rst index 57c291b9517bf4..43d5596cdeef53 100644 --- a/Doc/c-api/bytes.rst +++ b/Doc/c-api/bytes.rst @@ -245,6 +245,22 @@ called with a non-bytes parameter. returns ``NULL`` with an exception set. +.. c:function:: PyObject *PyBytes_DecodeEscape(const char *s, Py_ssize_t len, const char *errors, Py_ssize_t unicode, const char *recode_encoding) + + Unescape a backslash-escaped string *s*. *s* must not be ``NULL``. + *len* must be the size of *s*. + + *errors* must be one of ``"strict"``, ``"replace"``, or ``"ignore"``. If + *errors* is ``NULL``, then ``"strict"`` is used by default. + + On success, this function returns a :term:`strong reference` to a Python + :class:`bytes` object containing the unescaped string. On failure, this + function returns ``NULL`` with an exception set. + + .. versionchanged:: 3.9 + *unicode* and *recode_encoding* are now unused. + + .. _pybyteswriter: PyBytesWriter From 15277b303ed949b0ffd0ea02a85c2c4b4d509efb Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Tue, 11 Nov 2025 10:31:03 -0500 Subject: [PATCH 3/6] Fix Sphinx warning. --- Doc/c-api/bytes.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/c-api/bytes.rst b/Doc/c-api/bytes.rst index 43d5596cdeef53..c7a87e3696db23 100644 --- a/Doc/c-api/bytes.rst +++ b/Doc/c-api/bytes.rst @@ -232,7 +232,7 @@ called with a non-bytes parameter. .. c:function:: PyObject *PyBytes_Repr(PyObject *bytes, int smartquotes) Get the string representation of *bytes*. This function is used to - implement :meth:`bytes.__repr__` in Python. + implement :meth:`!bytes.__repr__` in Python. If *smartquotes* is true, the representation will use a double-quoted string instead of single-quoted string when single-quotes are present in *bytes*. From 915b79f0cb9a2bfd66ef07283a5b79d8bfc07e8e Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Tue, 11 Nov 2025 12:20:45 -0500 Subject: [PATCH 4/6] Update Doc/c-api/bytes.rst Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com> --- Doc/c-api/bytes.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/c-api/bytes.rst b/Doc/c-api/bytes.rst index c7a87e3696db23..43d8bdf048026e 100644 --- a/Doc/c-api/bytes.rst +++ b/Doc/c-api/bytes.rst @@ -237,7 +237,7 @@ called with a non-bytes parameter. If *smartquotes* is true, the representation will use a double-quoted string instead of single-quoted string when single-quotes are present in *bytes*. For example, the byte string ``'Python'`` would be represented as - ``b"'Python'"`` when *smartquotes* is true, or b'\'Python\'' when it is + ``b"'Python'"`` when *smartquotes* is true, or ``b'\'Python\''`` when it is false. On success, this function returns a :term:`strong reference` to a From 58e57a49d575f2b06765c4f6f58cfdff35e38866 Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Tue, 11 Nov 2025 12:23:11 -0500 Subject: [PATCH 5/6] Add "currently". --- Doc/c-api/bytes.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/c-api/bytes.rst b/Doc/c-api/bytes.rst index 43d8bdf048026e..dcd173a7a88a1f 100644 --- a/Doc/c-api/bytes.rst +++ b/Doc/c-api/bytes.rst @@ -231,7 +231,7 @@ called with a non-bytes parameter. .. c:function:: PyObject *PyBytes_Repr(PyObject *bytes, int smartquotes) - Get the string representation of *bytes*. This function is used to + Get the string representation of *bytes*. This function is currently used to implement :meth:`!bytes.__repr__` in Python. If *smartquotes* is true, the representation will use a double-quoted string From 6011132fcd61b72c94ee880845b78c2bfdcf6382 Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Tue, 11 Nov 2025 13:27:11 -0500 Subject: [PATCH 6/6] Add a note about type checking. --- Doc/c-api/bytes.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Doc/c-api/bytes.rst b/Doc/c-api/bytes.rst index dcd173a7a88a1f..82c2557368371f 100644 --- a/Doc/c-api/bytes.rst +++ b/Doc/c-api/bytes.rst @@ -234,6 +234,9 @@ called with a non-bytes parameter. Get the string representation of *bytes*. This function is currently used to implement :meth:`!bytes.__repr__` in Python. + This function does not do type checking; it is undefined behavior to pass + *bytes* as a non-bytes object or ``NULL``. + If *smartquotes* is true, the representation will use a double-quoted string instead of single-quoted string when single-quotes are present in *bytes*. For example, the byte string ``'Python'`` would be represented as