Uh oh!
There was an error while loading. Please reload this page.
gh-46236: Add missing PyUnicode_GetDefaultEncoding() doc - #130335
Conversation
rruuaanng
commented
Feb 20, 2025
cc @vstinner |
As usual, the documentation does not require a NEWS (I hope I remember correctly). I'm aware that this function returns a |
Uh oh!
There was an error while loading. Please reload this page.
I'd link to Perhaps this should be soft-deprecated. |
rruuaanng
commented
Feb 20, 2025
Alright, something interesting happened.
Should I modify its description? e.g.,
In other words, should I add Even so, do I still need to add this tag? |
Hm, looking further, looks like the “current default string encoding used by the Unicode implementation” is what (click for Python 2 behaviour)>>>importsys>>>sys.getdefaultencoding()
'ascii'>>>u'á'.encode()
Traceback (mostrecentcalllast):
File"<stdin>", line1, in<module>UnicodeEncodeError: 'ascii'codeccan't encode character u'\xe1' inposition0: ordinalnotinrange(128)
>>>importctypes>>>ctypes.pythonapi.PyUnicodeUCS4_SetDefaultEncoding("utf-8") # hack!0>>>sys.getdefaultencoding()
'utf-8'>>>'á'.encode()
'\xc3\xa1'It might be good to clarify this in the
(So, UTF-8 is not a CPython implementation detail.) |
encukou
commented
Feb 20, 2025
That's needed to get a Python object, as opposed to a |
encukou
commented
Feb 20, 2025
I'd prefer including the |
| Return a ``"utf-8"`` string constant, which corresponds to the | ||
| :func:`~sys.getdefaultencoding` function in Python. |
There was a problem hiding this comment.
I suggest this wording, plus making lifetime explicit since we're returning a buffer.
(Currently the string is static of course, but if it'd ever become dynamic again, it'd need to be borrowed from the interpreter.)
| Return a ``"utf-8"`` string constant, which corresponds to the | |
| :func:`~sys.getdefaultencoding` function in Python. | |
| Return the name of the default string encoding, ``"utf-8"``. | |
| See :func:`sys.getdefaultencoding`. | |
| The returned string does not need to be freed, and is valid | |
| until interpreter shutdown. |
Thanks @rruuaanng for the PR, and @encukou for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13. |
Thanks @rruuaanng for the PR, and @encukou for merging it 🌮🎉.. I'm working now to backport this PR to: 3.12. |
…onGH-130335) * Clarify sys.getdefaultencoding() documentation * Add missing documentation for PyUnicode_GetDefaultEncoding, the C equivalent of sys.getdefaultencoding (cherry picked from commit 9f25c1f) Co-authored-by: RUANG (James Roy) <longjinyii@outlook.com>
GH-130511 is a backport of this pull request to the 3.13 branch. |
…onGH-130335) * Clarify sys.getdefaultencoding() documentation * Add missing documentation for PyUnicode_GetDefaultEncoding, the C equivalent of sys.getdefaultencoding (cherry picked from commit 9f25c1f) Co-authored-by: RUANG (James Roy) <longjinyii@outlook.com>
encukou
commented
Feb 24, 2025
Merged. Thank you, @rruuaanng! |
GH-130512 is a backport of this pull request to the 3.12 branch. |
📚 Documentation preview 📚: https://cpython-previews--130335.org.readthedocs.build/