Skip to content

Calling TextIOWrapper.tell() in the middle of reading a gb2312-encoded file causes UnicodeDecodeError #84596

Closed

Description

@rmalouf
mannequin
BPO 40416
Nosy @terryjreedy, @ezio-melotti, @methane, @animalize, @rmalouf
Superseder
  • bpo-33578: cjkcodecs missing getstate and setstate implementations
  • Files
  • udhr-gb2312.txt: GB2312-encoded file to demonstrate error
  • 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-05-03.09:14:03.853>
    created_at = <Date 2020-04-28.04:33:56.399>
    labels = ['expert-unicode', '3.7', 'expert-IO', 'type-crash']
    title = 'Calling TextIOWrapper.tell() in the middle of reading a gb2312-encoded file causes UnicodeDecodeError'
    updated_at = <Date 2020-05-03.09:14:03.851>
    user = 'https://github.com/rmalouf'

    bugs.python.org fields:

    activity = <Date 2020-05-03.09:14:03.851>
    actor = 'terry.reedy'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-05-03.09:14:03.853>
    closer = 'terry.reedy'
    components = ['Unicode', 'IO']
    creation = <Date 2020-04-28.04:33:56.399>
    creator = 'rmalouf'
    dependencies = []
    files = ['49096']
    hgrepos = []
    issue_num = 40416
    keywords = []
    message_count = 8.0
    messages = ['367494', '367894', '367896', '367942', '367953', '367955', '367958', '367961']
    nosy_count = 5.0
    nosy_names = ['terry.reedy', 'ezio.melotti', 'methane', 'malin', 'rmalouf']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '33578'
    type = 'crash'
    url = 'https://bugs.python.org/issue40416'
    versions = ['Python 3.7']

    Activity

    1. rmalouf commented on Apr 28, 2020

      rmaloufmannequin
      MannequinAuthor

      Calling TextIOWrapper.tell() while reading the attached gb2312-encoded file like this:

      with open('udhr-gb2312.txt', encoding='GB2312') as f: 
          while True: 
             line = f.readline() 
             t = f.tell()
             if not line: 
                 break 

      gives this result:

      Traceback (most recent call last):
        File "test.py", line 4, in <module>
          t = f.tell()
      UnicodeDecodeError: 'gb2312' codec can't decode byte 0xb5 in position 0: illegal multibyte sequence

      The file seems to be well-formed and can be read without any problem. It's only the call to tell() that raises an issue.

    2. added
      3.7 (EOL)
      type-crash
      on Apr 28, 2020
    3. terryjreedy commented on May 1, 2020

      @terryjreedy
      Member

      OS? in case it matters

    4. terryjreedy commented on May 1, 2020

      @terryjreedy
      Member

      Change the line to 'print(f.tell())'. Are any lines printed before the error?

    5. rmalouf commented on May 2, 2020

      rmaloufmannequin
      MannequinAuthor

      Same results on MacOS 10.15.4 (both the system python and the intel/anaconda version) and on CentOS 7.8

      Here's the output with print(...):

      13
      71
      72
      392
      393
      399
      536
      537
      761
      762
      879
      880
      933
      934
      1146
      1147
      1254
      1255
      1359
      1360
      1760
      1761
      1772
      1895
      1897
      1906
      2105
      2107
      2338
      2339
      2348
      2398
      2399
      2408
      2509
      2510
      2519
      2612
      2614
      2622
      2682
      2684
      2693
      2898
      2900
      2909
      3050
      3052
      3061
      3113
      3115
      3124
      3295
      3297
      3309
      3445
      3632
      3644
      3814
      3816
      3828
      3882
      3967
      3979
      4048
      4184
      4196
      4226
      4308
      4320
      4492
      4559
      4641
      4653
      4728
      4770
      4782
      4999
      5001
      5013
      5202
      5204
      5216
      5270
      5318
      5333
      5411
      5465
      5672
      5687
      5953
      5954
      5969
      6082
      6137
      6307
      6373
      6388
      6494
      6496
      6511
      6786
      6913
      6928
      7148
      7371
      7447
      7462
      7569
      7704
      7719
      7847
      7848
      7863
      7972
      8238
      8342
      Traceback (most recent call last):
        File "test.py", line 4, in <module>
          print(f.tell())
      UnicodeDecodeError: 'gb2312' codec can't decode byte 0xb5 in position 0: illegal multibyte sequence
    6. animalize commented on May 3, 2020

      animalizemannequin
      Mannequin

      On Windows 10, Python 3.7, I get the same message as above reply.

      If use Python 3.8, it works well.

    7. animalize commented on May 3, 2020

      animalizemannequin
      Mannequin

      I did a git bisect, this commit fixed the bug:

      ac22f6a

    8. methane commented on May 3, 2020

      @methane
      Member

      I think this is not a bug, but a limitation of Python 3.7, and improvement in 3.8.

    9. terryjreedy commented on May 3, 2020

      @terryjreedy
      Member

      The commit referenced above is for bpo-33578. The symptoms for that issue were very similar, including involving a cjk codec. The change was not backported because it was seen an enhancement. Rob, if you try 3.8.2 or 3.8.3 (the release candidate was out Wednesday, the final probably next week or so) and still have the same problem, re-open this.

    10. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.7 (EOL)topic-IOtopic-unicodetype-crash

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions