classification
Title: compileall.compile_file fails when sys.stdout is redirected to StringIO
Type: crash Stage: resolved
Components: Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: lukasz.langa, miss-islington, stefanhoelzl
Priority: normal Keywords: patch

Created on 2021-07-18 08:57 by stefanhoelzl, last changed 2021-07-30 17:13 by lukasz.langa. This issue is now closed.

Files
File name Uploaded Description Edit
compile_file_bug.py stefanhoelzl, 2021-07-18 08:57
Pull Requests
URL Status Linked Edit
PR 27236 merged stefanhoelzl, 2021-07-19 10:53
PR 27488 merged miss-islington, 2021-07-30 16:38
PR 27489 merged miss-islington, 2021-07-30 16:38
Messages (5)
msg397743 - (view) Author: Stefan Hölzl (stefanhoelzl) * Date: 2021-07-18 08:57
compile_files tries to escape non-printable characters in error messages by using sys.stdout.encoding
https://github.com/python/cpython/blob/main/Lib/compileall.py#L256

when using contextlib.redirect_stdout to redirect stdout to io.StringIO as explained in the documentation
https://docs.python.org/3/library/contextlib.html#contextlib.redirect_stdout

compile_file fails, because io.StringIO has encoding set to None.

see the attached file to reproduce the issue
msg398572 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-07-30 16:38
New changeset 80f07076294bc09a55ed76d9bbf307404eef25e6 by Stefan Hoelzl in branch 'main':
bpo-44666: Use default encoding as fallback for compile_file (GH-27236)
https://github.com/python/cpython/commit/80f07076294bc09a55ed76d9bbf307404eef25e6
msg398576 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-07-30 17:12
New changeset 0db6c143ae5f04d223fdb2c94b820f16714b0a09 by Miss Islington (bot) in branch '3.10':
bpo-44666: Use default encoding as fallback for compile_file (GH-27236) (GH-27488)
https://github.com/python/cpython/commit/0db6c143ae5f04d223fdb2c94b820f16714b0a09
msg398577 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-07-30 17:12
New changeset 73240d425b770c26d9424665259cd9a2f339b626 by Miss Islington (bot) in branch '3.9':
bpo-44666: Use default encoding as fallback for compile_file (GH-27236) (GH-27489)
https://github.com/python/cpython/commit/73240d425b770c26d9424665259cd9a2f339b626
msg398578 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-07-30 17:13
Thanks, Stefan! ✨ 🍰 ✨
History
Date User Action Args
2021-07-30 17:13:22lukasz.langasetmessages: + msg398578
2021-07-30 17:12:54lukasz.langasetstatus: open -> closed
stage: patch review -> resolved
resolution: fixed
versions: - Python 3.6, Python 3.7, Python 3.8
2021-07-30 17:12:32lukasz.langasetmessages: + msg398577
2021-07-30 17:12:08lukasz.langasetmessages: + msg398576
2021-07-30 16:38:57miss-islingtonsetpull_requests: + pull_request26005
2021-07-30 16:38:52miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request26004
2021-07-30 16:38:51lukasz.langasetnosy: + lukasz.langa
messages: + msg398572
2021-07-19 10:53:19stefanhoelzlsetkeywords: + patch
stage: patch review
pull_requests: + pull_request25784
2021-07-18 08:57:11stefanhoelzlcreate