Issue41497
Created on 2020-08-06 19:52 by zkonge, last changed 2020-08-08 03:38 by methane. 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 "a.py", and contains "print('喵')", then save with UTF8 encoding.
Run "python -m dis ./a.py", 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\runpy.py", line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Program Files\Python38\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "C:\Program Files\Python38\lib\dis.py", line 553, in <module>
_test()
File "C:\Program Files\Python38\lib\dis.py", line 548, in _test
source = infile.read()
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) * | 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 tokenize.open() if we need string data, or use token.detect_encoding() and pass the result to open(). |
|||
| msg375032 - (view) | Author: Inada Naoki (methane) * | Date: 2020-08-08 03:03 | |
New changeset a4084b9d1e40c1c9259372263d1fe8c8a562b093 by Konge in branch 'master': bpo-41497: Fix potential UnicodeDecodeError in dis CLI (GH-21757) https://github.com/python/cpython/commit/a4084b9d1e40c1c9259372263d1fe8c8a562b093 |
|||
| 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) https://github.com/python/cpython/commit/66c899661902edc18df96a5c3f22639310700491 |
|||
| 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) https://github.com/python/cpython/commit/d9106434f77fa84c8a59f8e60dc9c14cdd989b35 |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2020-08-08 03:38:50 | methane | set | status: open -> closed resolution: fixed stage: patch review -> resolved |
| 2020-08-08 03:24:22 | miss-islington | set | messages: + msg375035 |
| 2020-08-08 03:21:40 | miss-islington | set | messages: + msg375034 |
| 2020-08-08 03:03:32 | miss-islington | set | pull_requests: + pull_request20924 |
| 2020-08-08 03:03:23 | miss-islington | set | nosy:
+ miss-islington pull_requests: + pull_request20923 |
| 2020-08-08 03:03:12 | methane | set | nosy:
+ methane messages: + msg375032 |
| 2020-08-07 05:53:16 | serhiy.storchaka | set | nosy:
+ serhiy.storchaka messages: + msg374977 |
| 2020-08-07 05:34:03 | methane | set | versions: - Python 3.5, Python 3.6, Python 3.7 |
| 2020-08-06 20:46:49 | zkonge | set | messages: + msg374965 |
| 2020-08-06 19:54:15 | zkonge | set | keywords:
+ patch stage: patch review pull_requests: + pull_request20902 |
| 2020-08-06 19:52:45 | zkonge | create | |