Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Potential UnicodeDecodeError in dis #85669

Closed
zkonge mannequin opened this issue Aug 6, 2020 · 6 comments
Closed

Potential UnicodeDecodeError in dis #85669

zkonge mannequin opened this issue Aug 6, 2020 · 6 comments
Labels
3.8 only security fixes 3.9 only security fixes 3.10 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@zkonge
Copy link
Mannequin

zkonge mannequin commented Aug 6, 2020

BPO 41497
Nosy @methane, @serhiy-storchaka, @miss-islington, @zkonge
PRs
  • bpo-41497: Fix potential UnicodeDecodeError in dis #21757
  • [3.9] bpo-41497: Fix potential UnicodeDecodeError in dis CLI (GH-21757) #21782
  • [3.8] bpo-41497: Fix potential UnicodeDecodeError in dis CLI (GH-21757) #21783
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2020-08-08.03:38:50.058>
    created_at = <Date 2020-08-06.19:52:45.184>
    labels = ['3.8', 'type-bug', 'library', '3.9', '3.10']
    title = 'Potential UnicodeDecodeError in dis'
    updated_at = <Date 2020-08-08.03:38:50.057>
    user = 'https://github.com/zkonge'

    bugs.python.org fields:

    activity = <Date 2020-08-08.03:38:50.057>
    actor = 'methane'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-08-08.03:38:50.058>
    closer = 'methane'
    components = ['Library (Lib)']
    creation = <Date 2020-08-06.19:52:45.184>
    creator = 'zkonge'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 41497
    keywords = ['patch']
    message_count = 6.0
    messages = ['374961', '374965', '374977', '375032', '375034', '375035']
    nosy_count = 4.0
    nosy_names = ['methane', 'serhiy.storchaka', 'miss-islington', 'zkonge']
    pr_nums = ['21757', '21782', '21783']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue41497'
    versions = ['Python 3.8', 'Python 3.9', 'Python 3.10']

    @zkonge
    Copy link
    Mannequin Author

    zkonge mannequin commented Aug 6, 2020

    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".

    @zkonge zkonge mannequin added 3.7 (EOL) end of life 3.10 only security fixes 3.8 only security fixes 3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Aug 6, 2020
    @zkonge
    Copy link
    Mannequin Author

    zkonge mannequin commented Aug 6, 2020

    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?

    @methane methane removed 3.7 (EOL) end of life labels Aug 7, 2020
    @serhiy-storchaka
    Copy link
    Member

    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().

    @methane
    Copy link
    Member

    methane commented Aug 8, 2020

    New changeset a4084b9 by Konge in branch 'master':
    bpo-41497: Fix potential UnicodeDecodeError in dis CLI (GH-21757)
    a4084b9

    @miss-islington
    Copy link
    Contributor

    New changeset 66c8996 by Miss Islington (bot) in branch '3.8':
    bpo-41497: Fix potential UnicodeDecodeError in dis CLI (GH-21757)
    66c8996

    @miss-islington
    Copy link
    Contributor

    New changeset d910643 by Miss Islington (bot) in branch '3.9':
    bpo-41497: Fix potential UnicodeDecodeError in dis CLI (GH-21757)
    d910643

    @methane methane closed this as completed Aug 8, 2020
    @methane methane closed this as completed Aug 8, 2020
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 only security fixes 3.9 only security fixes 3.10 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants