Skip to content
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

bpo-47000: Add locale.getencoding() #32068

Merged
merged 13 commits into from Apr 9, 2022
Merged

Conversation

Copy link
Member

@methane methane commented Mar 23, 2022

@methane methane changed the title bpo-47000: Add locale.get_encoding() bpo-47000: Add locale.getencoding() Apr 4, 2022
@methane methane changed the title bpo-47000: Add locale.getencoding() bpo-47000: Add locale.getencoding() Apr 4, 2022
@methane methane requested a review from vstinner Apr 6, 2022
Doc/library/locale.rst Outdated Show resolved Hide resolved
Copy link
Member

@vstinner vstinner left a comment

In some places, "utf-8" is used, in some other places "UTF-8" is used. I suggest to use lower case "utf-8" everywhere: doc, C code, _Py_STR(), etc. You may have to update getpreferredencoding() doc.

Doc/glossary.rst Show resolved Hide resolved
Doc/glossary.rst Outdated Show resolved Hide resolved
Doc/glossary.rst Show resolved Hide resolved
Doc/glossary.rst Outdated Show resolved Hide resolved
Doc/library/locale.rst Outdated Show resolved Hide resolved
Doc/library/locale.rst Outdated Show resolved Hide resolved
Doc/whatsnew/3.11.rst Outdated Show resolved Hide resolved
Lib/locale.py Outdated Show resolved Hide resolved
Modules/_io/textio.c Outdated Show resolved Hide resolved
methane and others added 2 commits Apr 7, 2022
Co-authored-by: Victor Stinner <vstinner@python.org>
argument are ignored.

The :ref:`Python preinitialization <c-preinit>` configures the LC_CTYPE
locale. See also the :term:`filesystem encoding and error handler`.

.. versionchanged:: 3.7
The function now always returns ``UTF-8`` on Android or if the
The function now always returns ``"UTF-8"`` on Android or if the
Copy link
Member

@vstinner vstinner Apr 7, 2022

Choose a reason for hiding this comment

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

Suggested change
The function now always returns ``"UTF-8"`` on Android or if the
The function now always returns ``"utf-8"`` on Android or if the

:ref:`Python UTF-8 Mode <utf8-mode>` is enabled.

.. versionchanged:: 3.11
The function now returns ``"utf-8"`` instead of ``"UTF-8"`` on Android
or if the :ref:`Python UTF-8 Mode <utf8-mode>` is enabled.
Copy link
Member

@vstinner vstinner Apr 7, 2022

Choose a reason for hiding this comment

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

I am sure that the upper case versus lower case is worth to be mentioned. But I'm fine with keeping if you consider that it's worth it.

@@ -327,16 +327,37 @@ The :mod:`locale` module defines the following exception and functions:
is not necessary or desired, *do_setlocale* should be set to ``False``.

On Android or if the :ref:`Python UTF-8 Mode <utf8-mode>` is enabled, always
return ``'UTF-8'``, the :term:`locale encoding` and the *do_setlocale*
return ``'utf-8'``, the :term:`locale encoding` and the *do_setlocale*
argument are ignored.

The :ref:`Python preinitialization <c-preinit>` configures the LC_CTYPE
locale. See also the :term:`filesystem encoding and error handler`.
Copy link
Member

@vstinner vstinner Apr 7, 2022

Choose a reason for hiding this comment

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

I suggest to copy/paste this paragraph to getencoding() doc.

------

* Add :func:`locale.getencoding` to get the current locale encoding. It is similar to
``locale.getpreferredencoding(False)`` but ignores :ref:`UTF-8 Mode <utf8-mode>`.
Copy link
Member

@vstinner vstinner Apr 7, 2022

Choose a reason for hiding this comment

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

I like repeating "Python" to remind that it's unrelated to the Unix locale, but really something specific to Python which ignores the locale.

Suggested change
``locale.getpreferredencoding(False)`` but ignores :ref:`UTF-8 Mode <utf8-mode>`.
``locale.getpreferredencoding(False)`` but ignores the :ref:`Python UTF-8 Mode <utf8-mode>`.

@@ -0,0 +1 @@
Add :func:`locale.getencoding`.
Copy link
Member

@vstinner vstinner Apr 7, 2022

Choose a reason for hiding this comment

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

You can copy/paste the Doc/whatsnew/3.11.rst entry entry.

Doc/glossary.rst Outdated
encoding.
On Android and VxWorks, Python uses ``"utf-8"`` as the locale encoding.

``locale.getencoding()`` can be used to get the locale encoding.

Python uses the :term:`filesystem encoding and error handler` to convert
between Unicode filenames and bytes filenames.
Copy link
Member

@vstinner vstinner Apr 7, 2022

Choose a reason for hiding this comment

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

IMO it would be helpful to mention here the Python UTF-8 Mode which ignores the locale encoding and always uses UTF-8. What do you think?

Copy link
Member Author

@methane methane Apr 8, 2022

Choose a reason for hiding this comment

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

I don't think so. This paragraph doesn't describe where the locale encoding is used.
This paragraph describes what the locale encoding is.
With this pull request, locale encoding is locale encoding even in UTF-8 mode.

On the other hand, following this paragraph looks unnecessary:

      Python uses the :term:`filesystem encoding and error handler` to convert
      between Unicode filenames and bytes filenames.

I will replace it with See also :term:`filesystem encoding and error handler`.

Copy link
Member

@vstinner vstinner left a comment

LGTM. Thanks.

IMO it's important that the current is complete and explains well this function, since it's a complex topic. If the doc is unclear, people will misuse it.

Thanks for adding "See also the filesystem encoding and error handler" in the glossary, that's good!

I'm not sure why the PR is still a draft.

@methane methane marked this pull request as ready for review Apr 9, 2022
@methane methane merged commit 6773203 into python:main Apr 9, 2022
12 checks passed
@methane methane deleted the locale-get-encoding branch Apr 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants