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

Merged
merged 5 commits into from
Jul 27, 2025

Conversation

Toshaksha
Copy link
Contributor

@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
Contributor 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
Contributor 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
Contributor 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
Contributor 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
Contributor Author

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

Copy link
Member

@malemburg malemburg left a comment

Choose a reason for hiding this comment

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

LGTM.

@malemburg malemburg enabled auto-merge (squash) July 27, 2025 16:36
@malemburg malemburg merged commit 6784ef7 into python:main Jul 27, 2025
25 checks passed
@github-project-automation github-project-automation bot moved this from Todo to Done in Docs PRs Jul 27, 2025
@miss-islington-app
Copy link

Thanks @Toshaksha for the PR, and @malemburg for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jul 27, 2025
…code() (pythonGH-136314)

Closes pythonGH-136278
(cherry picked from commit 6784ef7)

Co-authored-by: Toshaksha <147024929+Toshaksha@users.noreply.github.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jul 27, 2025
…code() (pythonGH-136314)

Closes pythonGH-136278
(cherry picked from commit 6784ef7)

Co-authored-by: Toshaksha <147024929+Toshaksha@users.noreply.github.com>
@bedevere-app
Copy link

bedevere-app bot commented Jul 27, 2025

GH-137149 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.14 bugs and security fixes label Jul 27, 2025
@bedevere-app
Copy link

bedevere-app bot commented Jul 27, 2025

GH-137150 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Jul 27, 2025
malemburg pushed a commit that referenced this pull request Jul 27, 2025
…ecode() (GH-136314) (#137150)

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

Closes GH-136278
(cherry picked from commit 6784ef7)

Co-authored-by: Toshaksha <147024929+Toshaksha@users.noreply.github.com>
@malemburg
Copy link
Member

Thanks, @Toshaksha

@Toshaksha
Copy link
Contributor Author

Thanks @malemburg and @StanFromIreland for the review and merge!
Glad to see this wrapped up.

Toshaksha

@Toshaksha Toshaksha deleted the doc-escape-codec-update branch July 27, 2025 17:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir skip news
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Write documentation for codecs.escape_encode() and codecs.escape_decode()
4 participants