classification
Title: Line number of SyntaxError
Type: behavior Stage:
Components: Interpreter Core Versions: Python 3.10, Python 3.9, Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: iritkatriel, remi.lapeyre, timofej, zach.ware
Priority: normal Keywords:

Created on 2020-05-29 10:08 by timofej, last changed 2020-09-25 07:40 by iritkatriel.

Files
File name Uploaded Description Edit
main.py timofej, 2020-05-29 14:02
Messages (6)
msg370287 - (view) Author: (timofej) Date: 2020-05-29 10:08
a SyntaxError must be look like:

File "main.py", line 7
    print("hello world"
                      ^
SyntaxError: unexpected EOF while parsing

but instead of this i'm get this:

File "main.py", line 3

                           ^
SyntaxError: unexpected EOF while parsing

It seems to me that this problem is only in windows.
msg370311 - (view) Author: Zachary Ware (zach.ware) * (Python committer) Date: 2020-05-29 13:46
Without some example code, there's no way to really tell what's going on here.  In particular, it's suspicious that you're expecting an error on line 7 but seeing one on line 3.

Can you attach your `main.py` file to the issue?
msg370314 - (view) Author: (timofej) Date: 2020-05-29 14:02
here attached file with SyntaxError
msg370315 - (view) Author: (timofej) Date: 2020-05-29 14:14
About of numbers of line. I tested on different files and i forget change number of line.
msg370316 - (view) Author: RĂ©mi Lapeyre (remi.lapeyre) * Date: 2020-05-29 14:43
Indeed, the exception has the correct line number when compiling manually:


>>> try:
...     compile('if __name__ == "__main__":\n   print("hello world"\n', '<string>', 'exec')
... except SyntaxError as e:
...     print(e.lineno)
...
2


but not when running `python3 main.py`:
  File "main.py", line 3

                          ^
SyntaxError: unexpected EOF while parsing



I tried with all version of Python >= 3.6 and Python2 and they all exhibit the same behavior.
msg377467 - (view) Author: Irit Katriel (iritkatriel) * (Python triager) Date: 2020-09-24 23:01
I don't think it's true that the lineno is correct when compiling a string (Remi's string is just too short):

>>> try:
...     compile('if __name__ == "__main__":\n   print("hello world"\n   x=5\n', '<string>', 'exec')
... except SyntaxError as e:
...     print("e.lineno=", e.lineno)
...     print("e.offset=", e.offset)
...     print("e.text=", e.text)
...
e.lineno= 3
e.offset= 4
e.text=    x=5
>>>


Arguably the syntax error is on line 3. Line 2 can be complemented by a line 3 that will make it valid.
History
Date User Action Args
2020-09-25 07:40:05iritkatrielsettitle: Output SyntaxError is not defective -> Line number of SyntaxError
2020-09-24 23:01:53iritkatrielsetnosy: + iritkatriel
messages: + msg377467
2020-05-29 14:43:20remi.lapeyresetnosy: + remi.lapeyre

messages: + msg370316
versions: + Python 3.9, Python 3.10
2020-05-29 14:14:02timofejsetmessages: + msg370315
2020-05-29 14:02:26timofejsetfiles: + main.py

messages: + msg370314
2020-05-29 13:47:09zach.waresettype: compile error -> behavior
2020-05-29 13:46:58zach.waresetnosy: + zach.ware
messages: + msg370311
components: + Interpreter Core, - Build
2020-05-29 10:08:32timofejcreate