Skip to content

bpo-34979: fix "SyntaxError: Non-UTF-8 code start with \xe8..." caused by function decoding_fgets#9923

Closed
ausaki wants to merge 2 commits intopython:mainfrom
ausaki:fix_bpo_34979
Closed

bpo-34979: fix "SyntaxError: Non-UTF-8 code start with \xe8..." caused by function decoding_fgets#9923
ausaki wants to merge 2 commits intopython:mainfrom
ausaki:fix_bpo_34979

Conversation

@ausaki
Copy link

@ausaki ausaki commented Oct 17, 2018

Please see issue-34979 for the details.

Maybe all versions greater than Python3 are affected.

How to reproduce this issue

  1. save the following source code into a file with utf8.
# demo.py
s = '测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试'
  1. run it.
$ python3 -V
Python 3.6.4

$ python3 demo.py
  File "demo.py", line 2
SyntaxError: Non-UTF-8 code starting with '\xe8' in file demo.py on line 2, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

How did it happen?

function deocding_fgets read one line of raw bytes into a buffer, this buffer's size is platform independent, for example, it equals 1024 on macOS.

if the line is too long(like greater than 1023 bytes), maybe the line will be cut in the middle of a multibyte UTF-8 character, then cause function valid_utf8 failed.

How to fix this issue?

  1. There is no need to check whether the encoding of the line is utf-8 or not.

  2. If we can't find the coding spec at the top of the source file , then try to set default encoding to utf-8 and always use function fp_readl to read a line.

https://bugs.python.org/issue34979

@iritkatriel
Copy link
Member

Closing the PR following the closure of its b.p.o issue (https://bugs.python.org/issue34979)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants