This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Potential UnicodeDecodeError in dis
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.10, Python 3.9, Python 3.8
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: methane, miss-islington, serhiy.storchaka, zkonge
Priority: normal Keywords: patch

Created on 2020-08-06 19:52 by zkonge, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 21757 merged zkonge, 2020-08-06 19:54
PR 21782 merged miss-islington, 2020-08-08 03:03
PR 21783 merged miss-islington, 2020-08-08 03:03
Messages (6)
msg374961 - (view) Author: JIanqiu Tao (zkonge) * Date: 2020-08-06 19:52
A potential UnicodeDecodeError could be raised when run "python -m dis" on non-utf8 encoding environment.

Assume there is a file named "", and contains "print('喵')", then save with UTF8 encoding.

Run "python -m dis ./", on non-UTF8 encoding environment, for example a Windows PC which default language is Chinese.

A UnicodeDecodeError raised.

Traceback (most recent call last):
  File "C:\Program Files\Python38\lib\", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Program Files\Python38\lib\", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Program Files\Python38\lib\", line 553, in <module>
  File "C:\Program Files\Python38\lib\", line 548, in _test
    source =
UnicodeDecodeError: 'gbk' codec can't decode byte 0xb5 in position 9: illegal multibyte sequence

That because Windows' default encoding is decided by language. Chinese use cp936(GB2312) as default encoding and can't handle UTF8 encoding.

It just need to read in "rb" mode instead of "r".
msg374965 - (view) Author: JIanqiu Tao (zkonge) * Date: 2020-08-06 20:46
I searched the whole Lib folder and find a lot of code uses "open(filename, 'r')" without handling default encoding.

Should we open another issue for these problem?
msg374977 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-08-07 05:53
Good catch. Yes, when read Python source files you should either open them in binary mode if reading bytes is enough for use, or open them with if we need string data, or use token.detect_encoding() and pass the result to open().
msg375032 - (view) Author: Inada Naoki (methane) * (Python committer) Date: 2020-08-08 03:03
New changeset a4084b9d1e40c1c9259372263d1fe8c8a562b093 by Konge in branch 'master':
bpo-41497: Fix potential UnicodeDecodeError in dis CLI (GH-21757)
msg375034 - (view) Author: miss-islington (miss-islington) Date: 2020-08-08 03:21
New changeset 66c899661902edc18df96a5c3f22639310700491 by Miss Islington (bot) in branch '3.8':
bpo-41497: Fix potential UnicodeDecodeError in dis CLI (GH-21757)
msg375035 - (view) Author: miss-islington (miss-islington) Date: 2020-08-08 03:24
New changeset d9106434f77fa84c8a59f8e60dc9c14cdd989b35 by Miss Islington (bot) in branch '3.9':
bpo-41497: Fix potential UnicodeDecodeError in dis CLI (GH-21757)
Date User Action Args
2022-04-11 14:59:34adminsetgithub: 85669
2020-08-08 03:38:50methanesetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2020-08-08 03:24:22miss-islingtonsetmessages: + msg375035
2020-08-08 03:21:40miss-islingtonsetmessages: + msg375034
2020-08-08 03:03:32miss-islingtonsetpull_requests: + pull_request20924
2020-08-08 03:03:23miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request20923
2020-08-08 03:03:12methanesetnosy: + methane
messages: + msg375032
2020-08-07 05:53:16serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg374977
2020-08-07 05:34:03methanesetversions: - Python 3.5, Python 3.6, Python 3.7
2020-08-06 20:46:49zkongesetmessages: + msg374965
2020-08-06 19:54:15zkongesetkeywords: + patch
stage: patch review
pull_requests: + pull_request20902
2020-08-06 19:52:45zkongecreate