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: traceback module incorrectly formats args-less syntax errors
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 2.7
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: Naftali.Harris, benjamin.peterson, georg.brandl, iritkatriel, kbk, ncoghlan
Priority: normal Keywords: patch

Created on 2016-12-29 18:23 by Naftali.Harris, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
traceback_fixes.patch Naftali.Harris, 2016-12-30 23:46 review
Messages (6)
msg284287 - (view) Author: Naftali Harris (Naftali.Harris) * Date: 2016-12-29 18:23
The traceback documentation states that it "exactly mimics the behavior of the Python interpreter when it prints a stack trace." Here's a small case where it doesn't, on 2.7.13:

~/repos/Python-2.7.13$ cat example.py
def f(x):
    global x
~/repos/Python-2.7.13$ ./python.exe
Python 2.7.13 (default, Dec 29 2016, 09:54:42)
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import traceback
>>> import example
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "example.py", line 1
    def f(x):
SyntaxError: name 'x' is local and global
>>> try:
... 	import example
... except:
... 	traceback.print_exc()
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
SyntaxError: name 'x' is local and global (example.py, line 1)
>>>

I believe Kurt fixed this for Python 3000 with https://mail.python.org/pipermail/python-3000-checkins/2007-July/001259.html
msg284339 - (view) Author: Naftali Harris (Naftali.Harris) * Date: 2016-12-30 18:30
Two other minor discrepancies between the way traceback and the interpreter format SyntaxError's, in 2.7.13:

1.

>>> e = SyntaxError("some message", ("myfile.py", None, None, None))
>>> raise e
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
SyntaxError: some message (myfile.py)
>>> try:
... 	raise e
... except:
... 	traceback.print_exc()
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
  File "/Users/naftali/repos/Python-2.7.13/Lib/traceback.py", line 233, in print_exc
    print_exception(etype, value, tb, limit, file)
  File "/Users/naftali/repos/Python-2.7.13/Lib/traceback.py", line 126, in print_exception
    lines = format_exception_only(etype, value)
  File "/Users/naftali/repos/Python-2.7.13/Lib/traceback.py", line 188, in format_exception_only
    lines.append('  File "%s", line %d\n' % (filename, lineno))
TypeError: %d format: a number is required, not NoneType


2.

>>> e = SyntaxError("some message", ("myfile.py", 3, 10, "hello"))
>>> raise e
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "myfile.py", line 3
    hello
             ^
SyntaxError: some message
>>> try:
... 	raise e
... except:
... 	traceback.print_exc()
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "myfile.py", line 3
    hello
        ^
SyntaxError: some message
msg284355 - (view) Author: Naftali Harris (Naftali.Harris) * Date: 2016-12-30 23:46
For your convenience, here is a possible patch fixing these issues. It modifies the format_exception_only function in the traceback module to follow the behavior of the interpreter a little more closely for SyntaxError's. Feel free to use, in whole, in part, or not at all, as you wish. Happy new year! :-)
msg284356 - (view) Author: Naftali Harris (Naftali.Harris) * Date: 2016-12-30 23:56
Adding Georg, who is listed as an expert for the traceback module (https://docs.python.org/devguide/experts.html).
msg377133 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2020-09-18 18:34
As stated above, this has been fixed in python 3:

**********************************************************
C:\Users\User\src\cpython>type x.py
def f(x):
    global x

C:\Users\User\src\cpython>python.bat
Running Release|Win32 interpreter...
Python 3.10.0a0 (heads/bpo11414-dirty:6595cb0af4, Sep 18 2020, 19:01:13) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import traceback
>>> import x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\User\src\cpython\x.py", line 2
    global x
    ^
SyntaxError: name 'x' is parameter and global
>>> try:
...     import x
... except:
...     traceback.print_exc()
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "C:\Users\User\src\cpython\x.py", line 2
    global x
    ^
SyntaxError: name 'x' is parameter and global
>>>


**********************************************************
>>> e = SyntaxError("some message", ("myfile.py", None, None, None))
>>> raise e
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
SyntaxError: some message (myfile.py)
>>> try:
...     raise e
... except:
...     traceback.print_exc()
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "<stdin>", line 2, in <module>
  File "<stdin>", line 1, in <module>
  File "myfile.py", line None
SyntaxError: some message
**********************************************************

>>> e = SyntaxError("some message", ("myfile.py", 3, 10, "hello"))
>>> raise r
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'r' is not defined
>>> raise e
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "myfile.py", line 3
    hello
         ^
SyntaxError: some message
>>> try:
...     raise e
... except:
...     traceback.print_exc()
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "<stdin>", line 1, in <module>
  File "myfile.py", line 3
    hello
         ^
SyntaxError: some message
msg377134 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2020-09-18 18:34
Since this is a python 2-only issue, I think this issue can be closed.
History
Date User Action Args
2022-04-11 14:58:41adminsetgithub: 73293
2020-09-18 20:13:43benjamin.petersonsetstatus: open -> closed
resolution: out of date
stage: resolved
2020-09-18 18:34:42iritkatrielsetmessages: + msg377134
2020-09-18 18:34:02iritkatrielsetnosy: + iritkatriel
messages: + msg377133
2017-02-02 21:58:24ncoghlansetnosy: + ncoghlan
2016-12-30 23:56:45Naftali.Harrissetnosy: + georg.brandl
messages: + msg284356
2016-12-30 23:46:03Naftali.Harrissetfiles: + traceback_fixes.patch
keywords: + patch
messages: + msg284355
2016-12-30 18:30:44Naftali.Harrissetmessages: + msg284339
2016-12-29 18:23:48Naftali.Harriscreate