◐ Shell
reader mode source ↗
Skip to content

bpo-42236: os.device_encoding() respects UTF-8 Mode#23119

Merged
vstinner merged 2 commits into
python:masterfrom
vstinner:device_encoding
Nov 4, 2020
Merged

bpo-42236: os.device_encoding() respects UTF-8 Mode#23119
vstinner merged 2 commits into
python:masterfrom
vstinner:device_encoding

Conversation

@vstinner

@vstinner vstinner commented Nov 2, 2020

Copy link
Copy Markdown
Member

On Unix, the os.device_encoding() function now returns 'UTF-8' rather
than the device encoding if the Python UTF-8 Mode is enabled.

https://bugs.python.org/issue42236

@vstinner

vstinner commented Nov 2, 2020

Copy link
Copy Markdown
Member Author

@methane: Would you mind to have a look at this change?

I'm not sure if it's correct to replace nl_langinfo(CODESET) with _Py_GetLocaleEncodingObjcet() in os.device_encoding(). One effect is that it returns UTF-8 if the Python UTF-8 Mode is enabled. But there is also another effect: on Android and VxWorks, os.device_encoding() now also returns UTF-8 (if the fd is a TTY) rather than nl_langinfo(CODESET).

I'm not sure if it's the same on Android or not?

IMO this change makes os.devide_encoding() and so indirectly open() more consistent with encoding choices in Python.

By the way, I deeply reworked the documentation on encodings, especially the locale encoding the filesystem encoding and error handler (docs.python.org was not updated yet).

On Unix, the os.device_encoding() function now returns 'UTF-8' rather
than the device encoding if the Python UTF-8 Mode is enabled.
@vstinner

vstinner commented Nov 3, 2020

Copy link
Copy Markdown
Member Author

@methane @eryksun: I updated my PR to only change the behavior on Unix (non-Windows platforms).

@vstinner vstinner merged commit 3529718 into python:master Nov 4, 2020
@vstinner vstinner deleted the device_encoding branch November 4, 2020 10:20
@vstinner

vstinner commented Nov 4, 2020

Copy link
Copy Markdown
Member Author

Example showing the impact of this change in practice, on the stdout TTY:

import sys
assert sys.stdout.isatty()
print("stdout encodingg:", sys.stdout.encoding)
reopen_stdout = open(sys.stdout.fileno(), closefd=False)
print("reopen encoding:", reopen_stdout.encoding)
reopen_stdout.close()

Python 3.9 output (old):

stdout encoding: utf-8
reopen encoding: ISO-8859-1

Python 3.10 output (new):

stdout encodingg: utf-8
reopen encoding: UTF-8

IMHO the new behavior is more consistent.

If you want the old behavior, you can explicitly pass encoding=locale.getpreferredencoding(False) to open().

@vstinner

vstinner commented Nov 4, 2020

Copy link
Copy Markdown
Member Author

Thanks for the reviews @eryksun and @methane.

@eryksun: The final change leaves Windows unchanged. If you consider that Python has issues on a specific use case, please open a new issue.

@bedevere-bot

Copy link
Copy Markdown

⚠️⚠️⚠️ Buildbot failure ⚠️⚠️⚠️

Hi! The buildbot s390x RHEL8 LTO 3.x has failed when building commit 3529718.

What do you need to do:

  1. Don't panic.
  2. Check the buildbot page in the devguide if you don't know what the buildbots are or how they work.
  3. Go to the page of the buildbot that failed (https://buildbot.python.org/all/#builders/567/builds/936) and take a look at the build logs.
  4. Check if the failure is related to this commit (3529718) or if it is a false positive.
  5. If the failure is related to this commit, please, reflect that on the issue and make a new Pull Request with a fix.

You can take a look at the buildbot page here:

https://buildbot.python.org/all/#builders/567/builds/936

Summary of the results of the build (if available):

== Tests result: ENV CHANGED ==

409 tests OK.

10 slowest tests:

  • test_concurrent_futures: 2 min 53 sec
  • test_peg_generator: 2 min 52 sec
  • test_gdb: 1 min 42 sec
  • test_multiprocessing_spawn: 1 min 11 sec
  • test_multiprocessing_forkserver: 1 min
  • test_multiprocessing_fork: 54.2 sec
  • test_signal: 47.4 sec
  • test_asyncio: 46.2 sec
  • test_tokenize: 37.3 sec
  • test_io: 35.9 sec

1 test altered the execution environment:
test_asyncio

14 tests skipped:
test_devpoll test_ioctl test_kqueue test_msilib test_nis
test_ossaudiodev test_startfile test_tix test_tk test_ttk_guionly
test_winconsoleio test_winreg test_winsound test_zipfile64

Total duration: 5 min 8 sec

Click to see traceback logs
Traceback (most recent call last):
  File "/home/dje/cpython-buildarea/3.x.edelsohn-rhel8-z.lto/build/Lib/asyncio/sslproto.py", line 321, in __del__
    self.close()
  File "/home/dje/cpython-buildarea/3.x.edelsohn-rhel8-z.lto/build/Lib/asyncio/sslproto.py", line 316, in close
    self._ssl_protocol._start_shutdown()
  File "/home/dje/cpython-buildarea/3.x.edelsohn-rhel8-z.lto/build/Lib/asyncio/sslproto.py", line 590, in _start_shutdown
    self._abort()
  File "/home/dje/cpython-buildarea/3.x.edelsohn-rhel8-z.lto/build/Lib/asyncio/sslproto.py", line 731, in _abort
    self._transport.abort()
  File "/home/dje/cpython-buildarea/3.x.edelsohn-rhel8-z.lto/build/Lib/asyncio/selector_events.py", line 680, in abort
    self._force_close(None)
  File "/home/dje/cpython-buildarea/3.x.edelsohn-rhel8-z.lto/build/Lib/asyncio/selector_events.py", line 731, in _force_close
    self._loop.call_soon(self._call_connection_lost, exc)
  File "/home/dje/cpython-buildarea/3.x.edelsohn-rhel8-z.lto/build/Lib/asyncio/base_events.py", line 746, in call_soon
    self._check_closed()
  File "/home/dje/cpython-buildarea/3.x.edelsohn-rhel8-z.lto/build/Lib/asyncio/base_events.py", line 510, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed

shihai1991 added a commit to shihai1991/cpython that referenced this pull request Nov 5, 2020
* master:
  bpo-42260: Add _PyInterpreterState_SetConfig() (pythonGH-23158)
  Disable peg generator tests when building with PGO (pythonGH-23141)
  bpo-1635741: _sqlite3 uses PyModule_AddObjectRef() (pythonGH-23148)
  bpo-1635741: Fix PyInit_pyexpat() error handling (pythonGH-22489)
  bpo-42260: Main init modify sys.flags in-place (pythonGH-23150)
  bpo-1635741: Fix ref leak in _PyWarnings_Init() error path (pythonGH-23151)
  bpo-1635741: _ast uses PyModule_AddObjectRef() (pythonGH-23146)
  bpo-1635741: _contextvars uses PyModule_AddType() (pythonGH-23147)
  bpo-42260: Reorganize PyConfig (pythonGH-23149)
  bpo-1635741: Add PyModule_AddObjectRef() function (pythonGH-23122)
  bpo-42236: os.device_encoding() respects UTF-8 Mode (pythonGH-23119)
  bpo-42251: Add gettrace and getprofile to threading (pythonGH-23125)
  Enable signing of nuget.org packages and update to supported timestamp server (pythonGH-23132)
  Fix incorrect links in ast docs (pythonGH-23017)
  Add _PyType_GetModuleByDef (pythonGH-22835)
  Post 3.10.0a2
  bpo-41796: Call _PyAST_Fini() earlier to fix a leak (pythonGH-23131)
  bpo-42249: Fix writing binary Plist files larger than 4 GiB. (pythonGH-23121)
  bpo-40077: Convert mmap.mmap static type to a heap type (pythonGH-23108)
  Python 3.10.0a2
adorilson pushed a commit to adorilson/cpython that referenced this pull request Mar 13, 2021
On Unix, the os.device_encoding() function now returns 'UTF-8' rather
than the device encoding if the Python UTF-8 Mode is enabled.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants