[issue42813] Extra spaces cause unexpected EOF error in "compile" function with mode "single"

Xinmeng Xia report at bugs.python.org
Sun Jan 3 03:33:08 EST 2021


New submission from Xinmeng Xia <xiaxm at smail.nju.edu.cn>:

Running the following program will lead to an unexpected EOF error. If we delete the space before  triple-quotation mark("""), the error will gone. If we replace the content of snippet with "a = 1", the program will work well. However, if we replace the content of snippet with "if a:pass" in a single line, the program cause an error again. This is weird. 

Spaces in the end of a statement should not affect the compilation of program in "single" mode. Besides, the error messages are different in Python2 and Python3. Indentation error are reported in Python 2 for snippet" if a:pass" while SyntaxError are reported in Python 3. 
  
======================================================
import math
def check_stack_size( code):
    # To assert that the alleged stack size is not O(N), we
    # check that it is smaller than log(N).
    if isinstance(code, str):
        code = compile(code, "<foo>", "single")
    max_size = math.ceil(math.log(len(code.co_code)))
    # self.assertLessEqual(code.co_stacksize, max_size)

def test_if_else():
    snippet ="""
if x:
    a
elif y:
    b
else:
    c     
    """
    check_stack_size(snippet)

test_if_else()
=====================================================
Behavior in Python 3.10:
Traceback (most recent call last):
  File "/home/xxm/Desktop/nameChanging/report/temp.py", line 30, in <module>
    test_if_else()
  File "/home/xxm/Desktop/nameChanging/report/temp.py", line 20, in test_if_else
    check_stack_size(snippet)
  File "/home/xxm/Desktop/nameChanging/report/temp.py", line 6, in check_stack_size
    code = compile(code, "<foo>", "single")
  File "<foo>", line 8
    
SyntaxError: unexpected EOF while parsing

Behaviors in Python2: 
Traceback (most recent call last):
  File "/home/xxm/Desktop/nameChanging/report/temp.py", line 30, in <module>
    test_if_else()
  File "/home/xxm/Desktop/nameChanging/report/temp.py", line 27, in test_if_else
    check_stack_size(snippet)
  File "/home/xxm/Desktop/nameChanging/report/temp.py", line 6, in check_stack_size
    code = compile(code, "<foo>", "single")
  File "<foo>", line 3
    
    ^
IndentationError: unexpected indent

----------
components: Interpreter Core
messages: 384257
nosy: xxm
priority: normal
severity: normal
status: open
title: Extra spaces cause unexpected EOF error in "compile" function with mode "single"
type: compile error
versions: Python 3.10

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue42813>
_______________________________________


More information about the Python-bugs-list mailing list