Skip to content

Commit 51c60f3

Browse files
[3.13] gh-141004: Document PyBytes_Repr and PyBytes_DecodeEscape (GH-141407) (GH-141441)
* gh-141004: Document `PyBytes_Repr` and `PyBytes_DecodeEscape` (GH-141407) Co-authored-by: Stan Ulbrych <[email protected]> (cherry picked from commit 37e2762)
1 parent aec6b72 commit 51c60f3

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

Doc/c-api/bytes.rst

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,38 @@ called with a non-bytes parameter.
201201
reallocation fails, the original bytes object at *\*bytes* is deallocated,
202202
*\*bytes* is set to ``NULL``, :exc:`MemoryError` is set, and ``-1`` is
203203
returned.
204+
205+
206+
.. c:function:: PyObject *PyBytes_Repr(PyObject *bytes, int smartquotes)
207+
208+
Get the string representation of *bytes*. This function is currently used to
209+
implement :meth:`!bytes.__repr__` in Python.
210+
211+
This function does not do type checking; it is undefined behavior to pass
212+
*bytes* as a non-bytes object or ``NULL``.
213+
214+
If *smartquotes* is true, the representation will use a double-quoted string
215+
instead of single-quoted string when single-quotes are present in *bytes*.
216+
For example, the byte string ``'Python'`` would be represented as
217+
``b"'Python'"`` when *smartquotes* is true, or ``b'\'Python\''`` when it is
218+
false.
219+
220+
On success, this function returns a :term:`strong reference` to a
221+
:class:`str` object containing the representation. On failure, this
222+
returns ``NULL`` with an exception set.
223+
224+
225+
.. c:function:: PyObject *PyBytes_DecodeEscape(const char *s, Py_ssize_t len, const char *errors, Py_ssize_t unicode, const char *recode_encoding)
226+
227+
Unescape a backslash-escaped string *s*. *s* must not be ``NULL``.
228+
*len* must be the size of *s*.
229+
230+
*errors* must be one of ``"strict"``, ``"replace"``, or ``"ignore"``. If
231+
*errors* is ``NULL``, then ``"strict"`` is used by default.
232+
233+
On success, this function returns a :term:`strong reference` to a Python
234+
:class:`bytes` object containing the unescaped string. On failure, this
235+
function returns ``NULL`` with an exception set.
236+
237+
.. versionchanged:: 3.9
238+
*unicode* and *recode_encoding* are now unused.

0 commit comments

Comments
 (0)