This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Extra spaces cause unexpected EOF error in "compile" function with mode "single"
Type: compile error Stage: resolved
Components: Interpreter Core Versions: Python 3.10
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: pablogsal, terry.reedy, xxm
Priority: normal Keywords:

Created on 2021-01-03 08:33 by xxm, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg384257 - (view) Author: Xinmeng Xia (xxm) Date: 2021-01-03 08:33
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
msg396970 - (view) Author: Xinmeng Xia (xxm) Date: 2021-07-05 04:50
Program like following reports error on CPython(master branch), however this program can work well on PyPy. I think this is a bug in CPython
==============================
string ="""
if 1:
    print("hello")
    """
compile(string, "", "single")
==============================

Traceback (most recent call last):
  File "/home/xxm/Desktop/IFuzzer/test/test1.py", line 304, in <module>
    compile(string, "", "single")
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "", line 4
    
SyntaxError: unexpected EOF while parsing
msg402908 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-09-29 20:47
I believe that this should be closed as not-a-bug.  Ending a file with <newline><indent><eof> *is* a syntax error.  Previous, it was assumed that the problem, in retrospect, was the 'extra' indent.  But one can equally say that the problem is the 'missing' statement after the otherwise legitimate indent.  Even a newline is enough to make the code legal.  The current message no longer guess whether there is too much or too little code.
msg402912 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-09-29 20:50
I concur with Terry, ending a file with <newline><indent><eof> *is* a syntax error.
History
Date User Action Args
2022-04-11 14:59:39adminsetgithub: 86979
2021-09-29 23:37:57terry.reedysetstatus: open -> closed
resolution: not a bug
stage: resolved
2021-09-29 20:50:26pablogsalsetmessages: + msg402912
2021-09-29 20:50:04pablogsalsetmessages: - msg402911
2021-09-29 20:49:47pablogsalsetmessages: + msg402911
2021-09-29 20:47:10terry.reedysetnosy: + pablogsal, terry.reedy
messages: + msg402908
2021-07-05 04:50:10xxmsetmessages: + msg396970
2021-01-03 08:33:08xxmcreate