# Confused about documentation of io.TextIOWrapper encoding

**URL:** <https://discuss.python.org/t/confused-about-documentation-of-io-textiowrapper-encoding/21089>\
**Category:** Documentation\
**Created:** [November 15, 2022, 1:35pm UTC](https://discuss.python.org/t/confused-about-documentation-of-io-textiowrapper-encoding/21089 "2022-11-15T13:35:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeanas](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jeanas/32/8115_2.png) [@jeanas](https://discuss.python.org/u/jeanas)\
**Post date:** [November 15, 2022, 1:35pm UTC](https://discuss.python.org/t/confused-about-documentation-of-io-textiowrapper-encoding/21089/1 "2022-11-15T13:35:05Z")

</div>

Hi,

The [io.TextIOWrapper documentation](https://docs.python.org/3.11/library/io.html#io.TextIOWrapper) claims that the default encoding is `locale.getencoding()`. The `getencoding` function ignores UTF-8 mode. But I thought the whole point of UTF-8 mode was to make UTF-8 encoding the default, especially for text IO.

Example:

```auto
$ LC_ALL=en_US.ISO8859-1 python -X utf8
Python 3.11.0rc2 (main, Sep 13 2022, 00:00:00) [GCC 12.2.1 20220819 (Red Hat 12.2.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from locale import getencoding
>>> from pathlib import Path
>>> getencoding()
'ISO-8859-1'
>>> Path("utf8.txt").read_text(encoding='ISO-8859-1')
'Ä¦ÃªÅ\x82Å\x82Ã¸\n'
>>> Path("utf8.txt").read_text(encoding='UTF-8')
'Ħêłłø\n'
>>> Path("utf8.txt").read_text()
'Ħêłłø\n'

```

I see that `locale.getpreferredencoding(False)` was changed to `locale.getencoding()` in this part of the documentation when `encoding="locale"` was made to ignore UTF-8 mode (@methane). But this means `encoding="locale"` makes the encoding `locale.getencoding()`, not `encoding=None`, right?

---

<div class="post-metadata">

**Author:** ![CAM-Gerlach](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/cam-gerlach/32/3688_2.png) [@CAM-Gerlach](https://discuss.python.org/u/CAM-Gerlach)\
**Post date:** [November 16, 2022, 7:27am UTC](https://discuss.python.org/t/confused-about-documentation-of-io-textiowrapper-encoding/21089/2 "2022-11-16T07:27:57Z")

</div>

I may be missing something, but as far as I can tell, it would seem like that particular line _should_ indeed say `locale.getpreferredencoding(False)`, [The code in question](https://github.com/python/cpython/blob/e37744f289af00c6f6eba83f7abfb932b63de9e0/Modules/_io/textio.c#L1109) indeed appears to behave as `locale.getpreferredencoding(False)`, i.e. _not_ ignoring UTF-8 mode, and this also matches the [relevant PEP](https://peps.python.org/pep-0686/#locale-getencoding) and the `locale.getpreferredencoding` and `locale.getencoding` [documentation](https://docs.python.org/3.12/library/locale.html#locale.getpreferredencoding).

IIRC, @vstinner was also involved in requesting this particular change, along with Inada-san, so perhaps he would also be able to comment.

---

<div class="post-metadata">

**Author:** ![methane](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/methane/32/43_2.png) [@methane](https://discuss.python.org/u/methane)\
**Post date:** [November 16, 2022, 7:54am UTC](https://discuss.python.org/t/confused-about-documentation-of-io-textiowrapper-encoding/21089/3 "2022-11-16T07:54:13Z")

</div>

TextIOWrapper uses UTF-8 if UTF-8 mode is enabled, otherwise uses `locale.getencoding()`.  
So it is just a documentation issue.
