Skip to content

gh-136278: Document codecs.escape_encode() and codecs.escape_decode() #136314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Toshaksha
Copy link

@Toshaksha Toshaksha commented Jul 5, 2025

This PR adds documentation for the functions codecs.escape_encode() and codecs.escape_decode(), which provide a way to encode and decode byte sequences using escape sequences, similar to how repr(bytes) works.

These APIs are primarily used by the pickle module for handling escaped byte sequences safely.

The documentation includes function signatures, a usage example, and background information referencing the previous usage in Python 2's string_escape codec and considerations for reviving this as bytes_escape.

Closes: #136278


📚 Documentation preview 📚: https://cpython-previews--136314.org.readthedocs.build/

@Toshaksha
Copy link
Author

Hi @StanFromIreland,

Thanks for the feedback and guidance throughout this process!
I have updated the documentation for codecs.escape_encode() and codecs.escape_decode() following the devguide and integrated it into the binary transforms section as suggested.
Please let me know if there are any further changes needed or if I missed anything.

Looking forward to your review!

Best regards,
Toshaksha

@StanFromIreland
Copy link
Member

Hello, in the future please reuse prs, it creates clutter otherwise. I will review later.

@Toshaksha
Copy link
Author

Thanks, noted! I'll make sure to reuse PRs going forward.

@hugovk hugovk added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes labels Jul 5, 2025
| zlib_codec | zip, zlib | Compress the operand using | :meth:`zlib.compress` / |
| | | gzip. | :meth:`zlib.decompress` |
+----------------------+------------------+------------------------------+------------------------------+

.. function:: codecs.escape_encode(input, errors='strict')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is errors='strict' from?

errors: str(accept={str, NoneType}) = None

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching that!
I've updated the function signature to use errors=None in accordance with the actual default in _codecsmodule.c.
Let me know if there's anything else you'd like me to adjust. Appreciate your time and feedback!

| | | sequences using escape | / |
| | | sequences, similar to | :func:`codecs.escape_decode` |
| | | :func:`repr` of bytes. | |
+----------------------+------------------+------------------------------+------------------------------+
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is correct. The codec itself (string_escape) was removed after the transition to Python 3. There is no escape_codec to be documented, only the functions which could be used to create a new bytes_escape codec, but that's probably for a separate issue to take care of.

| zlib_codec | zip, zlib | Compress the operand using | :meth:`zlib.compress` / |
| | | gzip. | :meth:`zlib.decompress` |
+----------------------+------------------+------------------------------+------------------------------+

.. function:: codecs.escape_encode(input, errors=None)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move this section below the table and its notes.

It may be good to also be good to put these function definitions into a section "Standalone Codec Functions", or something like that, to make it clear that the functions exist without a codec (at the moment) and perhaps with an explanation why this is.

Encode *input* using escape sequences. Similar to how :func:`repr` on bytes
produces escaped byte values. Returns a tuple of the encoded bytes and
the length consumed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should document that input needs to be a bytes object.


Decode *input* from escape sequences back to the original bytes.
Returns a tuple of the decoded bytes and the length consumed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should document that input needs to be a bytes or buffer compatible object.

@bedevere-app
Copy link

bedevere-app bot commented Jul 7, 2025

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@Toshaksha
Copy link
Author

I have made the requested changes; please review again.

@bedevere-app
Copy link

bedevere-app bot commented Jul 8, 2025

Thanks for making the requested changes!

@malemburg: please review the changes made to this pull request.

@bedevere-app bedevere-app bot requested a review from malemburg July 8, 2025 05:17
@Toshaksha Toshaksha force-pushed the doc-escape-codec-update branch from a7553c6 to 4b26d7d Compare July 12, 2025 05:09
@Toshaksha
Copy link
Author

Hi @malemburg ,
I’ve addressed all requested changes and pushed updates. Please let me know if anything else is needed.
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting change review docs Documentation in the Doc dir needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes skip news
Projects
Status: Todo
Development

Successfully merging this pull request may close these issues.

Write documentation for codecs.escape_encode() and codecs.escape_decode()
4 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy