You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
end_offset is ignored in subclasses of SyntaxError (3.12-only) #111609
When I tried to implement a subclass of SyntaxError, the exception details ignored the end_offset-variable and just showed one caret at the position of offset.
raiseSyntaxError("Error message", (None, 1, 5, 'a = sin(3)', 1, 9))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1
a = sin(3)
^^^^
SyntaxError: Error message
For me (Python 3.11.6 on Windows 10) the console does not print the carets in the original error message, only when using the traceback package:
Python 3.11.6 (tags/v3.11.6:8b6ee5b, Oct 2 2023, 14:57:12) [MSC v.1935 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import traceback
>>> class CustomSyntaxError(SyntaxError):
... pass
...
>>> try:
... raise CustomSyntaxError("Error message", ('<py>', 1, 5, 'a = sin(3)', 1, 9))
... except Exception as e:
... traceback.print_exc()
... print('-'*20)
... raise e
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "<py>", line 1
a = sin(3)
^^^^CustomSyntaxError: Error message
--------------------
Traceback (most recent call last):
File "<stdin>", line 6, in <module>
File "<stdin>", line 2, in <module>
File "<py>", line 1
a = sin(3)
^CustomSyntaxError: Error message
>>> raise SyntaxError("Error message", ('<py>', 1, 5, 'a = sin(3)', 1, 9))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<py>", line 1
a = sin(3)
^^^^SyntaxError: Error message
See also this gist for the behavior in a Ipython console or Jupyter Notebook. There neither SyntaxError nor its subclass showed the desired result.
Maybe it has something to do with the error handling in the console, e.g. the console in PyCharm Community Edition works fine for me.
changed the title [-]`end_offset` is ignored in subclasses of SyntaxError [/-][+]`end_offset` is ignored in subclasses of SyntaxError (3.12-only)[/+]on Dec 2, 2024
Activity
sobolevn commentedon Nov 1, 2023
It works correctly for me on
3.13a1:harshitrathor3 commentedon Nov 1, 2023
Also worked correctly for me on 3.10.10
raise SyntaxError("Error message", (None, 1, 5, 'a = sin(3)', 1, 9)) Traceback (most recent call last): File "<pyshell#0>", line 1, in <module> raise SyntaxError("Error message", (None, 1, 5, 'a = sin(3)', 1, 9)) File "<string>", line 1 a = sin(3) ^^^^ SyntaxError: Error message class CustomSyntaxError(SyntaxError): pass raise CustomSyntaxError("Error message", (None, 1, 5, 'a = sin(3)', 1, 9)) Traceback (most recent call last): File "<pyshell#4>", line 1, in <module> raise CustomSyntaxError("Error message", (None, 1, 5, 'a = sin(3)', 1, 9)) File "<string>", line 1 a = sin(3) ^^^^ CustomSyntaxError: Error messagestbech commentedon Nov 2, 2023
For me (Python 3.11.6 on Windows 10) the console does not print the carets in the original error message, only when using the traceback package:
Python 3.11.6 (tags/v3.11.6:8b6ee5b, Oct 2 2023, 14:57:12) [MSC v.1935 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import traceback >>> class CustomSyntaxError(SyntaxError): ... pass ... >>> try: ... raise CustomSyntaxError("Error message", ('<py>', 1, 5, 'a = sin(3)', 1, 9)) ... except Exception as e: ... traceback.print_exc() ... print('-'*20) ... raise e ... Traceback (most recent call last): File "<stdin>", line 2, in <module> File "<py>", line 1 a = sin(3) ^^^^ CustomSyntaxError: Error message -------------------- Traceback (most recent call last): File "<stdin>", line 6, in <module> File "<stdin>", line 2, in <module> File "<py>", line 1 a = sin(3) ^ CustomSyntaxError: Error message >>> raise SyntaxError("Error message", ('<py>', 1, 5, 'a = sin(3)', 1, 9)) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<py>", line 1 a = sin(3) ^^^^ SyntaxError: Error messageSee also this gist for the behavior in a Ipython console or Jupyter Notebook. There neither SyntaxError nor its subclass showed the desired result.
Maybe it has something to do with the error handling in the console, e.g. the console in PyCharm Community Edition works fine for me.
zedzhen commentedon Apr 8, 2024
Python 3.11.1, 3.12.2 (Windows 10)
IDLE
import sys import traceback class CustomSyntaxError(SyntaxError): pass try: raise CustomSyntaxError("Error message", ('<py>', 1, 5, 'a = sin(3)', 1, 9)) except Exception as e: traceback.print_exc() print('-' * 20, file=sys.stderr) raise e from None Traceback (most recent call last): File "<pyshell#4>", line 2, in <module> File "<py>", line 1 a = sin(3) ^^^^ CustomSyntaxError: Error message -------------------- Traceback (most recent call last): File "<pyshell#4>", line 6, in <module> raise e from None File "<pyshell#4>", line 2, in <module> raise CustomSyntaxError("Error message", ('<py>', 1, 5, 'a = sin(3)', 1, 9)) File "<py>", line 1 a = sin(3) ^^^^ CustomSyntaxError: Error message ========= RESTART: D:\path\test.py ========= Traceback (most recent call last): File "D:\path\test.py", line 10, in <module> raise CustomSyntaxError("Error message", ('<py>', 1, 5, 'a = sin(3)', 1, 9)) File "<py>", line 1 a = sin(3) ^^^^ CustomSyntaxError: Error message -------------------- Traceback (most recent call last): File "D:\path\test.py", line 14, in <module> raise e from None File "D:\path\test.py", line 10, in <module> raise CustomSyntaxError("Error message", ('<py>', 1, 5, 'a = sin(3)', 1, 9)) File "<py>", line 1 a = sin(3) ^^^^ CustomSyntaxError: Error messagecmd
C:\path\Python312>python Python 3.12.2 (tags/v3.12.2:6abddd9, Feb 6 2024, 21:26:36) [MSC v.1937 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. random, math imported >>> import sys >>> import traceback >>> >>> >>> class CustomSyntaxError(SyntaxError): ... pass ... >>> >>> try: ... raise CustomSyntaxError("Error message", ('<py>', 1, 5, 'a = sin(3)', 1, 9)) ... except Exception as e: ... traceback.print_exc() ... print('-' * 20, file=sys.stderr) ... raise e from None ... Traceback (most recent call last): File "<stdin>", line 2, in <module> File "<py>", line 1 a = sin(3) ^^^^ CustomSyntaxError: Error message -------------------- Traceback (most recent call last): File "<stdin>", line 6, in <module> File "<stdin>", line 2, in <module> File "<py>", line 1 a = sin(3) ^ CustomSyntaxError: Error message >>> ^Z C:\path\Python312>python D:\path\test.py Traceback (most recent call last): File "D:\path\test.py", line 10, in <module> raise CustomSyntaxError("Error message", ('<py>', 1, 5, 'a = sin(3)', 1, 9)) File "<py>", line 1 a = sin(3) ^^^^ CustomSyntaxError: Error message -------------------- Traceback (most recent call last): File "D:\path\test.py", line 14, in <module> raise e from None File "D:\path\test.py", line 10, in <module> raise CustomSyntaxError("Error message", ('<py>', 1, 5, 'a = sin(3)', 1, 9)) File "<py>", line 1 a = sin(3) ^ CustomSyntaxError: Error messagePyCharm 2023.3.5 (Professional Edition)
test.py
zedzhen commentedon Apr 8, 2024
Python 3.13.0a5 (windows 10) correct print
C:\path\Python313>python Python 3.13.0a5 (tags/v3.13.0a5:076d169, Mar 12 2024, 21:29:03) [MSC v.1938 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. random, math imported >>> import sys >>> import traceback >>> >>> >>> class CustomSyntaxError(SyntaxError): ... pass ... >>> >>> try: ... raise CustomSyntaxError("Error message", ('<py>', 1, 5, 'a = sin(3)', 1, 9)) ... except Exception as e: ... traceback.print_exc() ... print('-' * 20, file=sys.stderr) ... raise e from None ... Traceback (most recent call last): File "<stdin>", line 2, in <module> raise CustomSyntaxError("Error message", ('<py>', 1, 5, 'a = sin(3)', 1, 9)) File "<py>", line 1 a = sin(3) ^^^^ CustomSyntaxError: Error message -------------------- Traceback (most recent call last): File "<stdin>", line 6, in <module> raise e from None File "<stdin>", line 2, in <module> raise CustomSyntaxError("Error message", ('<py>', 1, 5, 'a = sin(3)', 1, 9)) File "<py>", line 1 a = sin(3) ^^^^ CustomSyntaxError: Error message >>> ^Z C:\path\Python313>python D:\path\test.py Traceback (most recent call last): File "D:\path\test.py", line 10, in <module> raise CustomSyntaxError("Error message", ('<py>', 1, 5, 'a = sin(3)', 1, 9)) File "<py>", line 1 a = sin(3) ^^^^ CustomSyntaxError: Error message -------------------- Traceback (most recent call last): File "D:\path\test.py", line 14, in <module> raise e from None File "D:\path\test.py", line 10, in <module> raise CustomSyntaxError("Error message", ('<py>', 1, 5, 'a = sin(3)', 1, 9)) File "<py>", line 1 a = sin(3) ^^^^ CustomSyntaxError: Error messagenineteendo commentedon Jul 19, 2024
Can confirm on 3.12.4:
It works correctly on 3.13.0b4:
picnixz commentedon Dec 2, 2024
The bug is affecting 3.12.6 (haven't tested on 3.12.7) so I'll mark it as 3.12-only.
[-]`end_offset` is ignored in subclasses of SyntaxError [/-][+]`end_offset` is ignored in subclasses of SyntaxError (3.12-only)[/+]nineteendo commentedon Dec 2, 2024
Could you do a bisect?
picnixz commentedon Dec 2, 2024
I don't really have time for now (I'm triaging the pending issues), so I'll leave it to anyone willing to do it.
nineteendo commentedon Dec 3, 2024
This also wasn't working on 3.10 and 3.11. It was fixed in 3.13 by #110702.
Should we back port it to fix this?We should fix it differently.I don't want to choose between
end_offsetand storing the column number.nineteendo commentedon Dec 3, 2024
This fixes the problem:
nineteendo commentedon Dec 3, 2024
The weird thing is that this check isn't even necessary:
[3.12] gh-111609: `end_offset` is ignored in subclasses of SyntaxError (
gh-111609: Test `end_offset` in SyntaxError subclass (#127830)
pythongh-111609: Test `end_offset` in SyntaxError subclass (pythonGH-…
pythongh-111609: Test `end_offset` in SyntaxError subclass (python#12…
pythongh-111609: Test `end_offset` in SyntaxError subclass (pythonGH-…
[3.13] gh-111609: Test `end_offset` in SyntaxError subclass (GH-127830)…
nineteendo commentedon Mar 17, 2025
This can be closed.