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

Created on 2020-05-29 10:08 by timofej, last changed 2021-03-22 22:51 by terry.reedy. This issue is now closed.

Files
File name Uploaded Description Edit
main.py timofej, 2020-05-29 14:02
Messages (7)
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 committer) 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.
msg389346 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-03-22 22:51
Whatever the situation was last May, current 3.10 now results in a correct and improved traceback ending in

  File "F:\Python\a\tem4.py", line 2
    print("hello world"
         ^
SyntaxError: '(' was never closed

Same if \n or additional text is added to the file without closing ')'.
History
Date User Action Args
2021-03-22 22:51:03terry.reedysetstatus: open -> closed

versions: - Python 3.8, Python 3.9
nosy: + terry.reedy

messages: + msg389346
resolution: out of date
stage: resolved
2020-12-05 16:50:56iritkatriellinkissue42577 superseder
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