gh-46236: Add missing PyUnicode_GetDefaultEncoding() doc#130335
gh-46236: Add missing PyUnicode_GetDefaultEncoding() doc#130335encukou merged 8 commits intopython:mainfrom
Conversation
|
cc @vstinner |
|
As usual, the documentation does not require a NEWS (I hope I remember correctly). I'm aware that this function returns a |
|
I'd link to Perhaps this should be soft-deprecated. |
|
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)>>> import sys
>>> sys.getdefaultencoding()
'ascii'
>>> u'á'.encode()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe1' in position 0: ordinal not in range(128)
>>> import ctypes
>>> 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.) |
That's needed to get a Python object, as opposed to a |
|
I'd prefer including the |
Doc/c-api/unicode.rst
Outdated
| 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>
|
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/