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 "", line 1, in 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 "", line 2, in 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 "", line 1, in SyntaxError: some message (myfile.py) >>> try: ... raise e ... except: ... traceback.print_exc() ... Traceback (most recent call last): File "", line 2, in Traceback (most recent call last): File "", line 4, in 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 "", line 1, in File "myfile.py", line 3 hello ^ SyntaxError: some message >>> try: ... raise e ... except: ... traceback.print_exc() ... Traceback (most recent call last): File "", line 2, in 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) *  |
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 "", line 1, in 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 "", line 2, in 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 "", line 1, in SyntaxError: some message (myfile.py) >>> try: ... raise e ... except: ... traceback.print_exc() ... Traceback (most recent call last): File "", line 2, in File "", line 2, in File "", line 1, in 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 "", line 1, in NameError: name 'r' is not defined >>> raise e Traceback (most recent call last): File "", line 1, in File "myfile.py", line 3 hello ^ SyntaxError: some message >>> try: ... raise e ... except: ... traceback.print_exc() ... Traceback (most recent call last): File "", line 2, in File "", line 1, in File "myfile.py", line 3 hello ^ SyntaxError: some message |
|
|
msg377134 - (view) |
Author: Irit Katriel (iritkatriel) *  |
Date: 2020-09-18 18:34 |
Since this is a python 2-only issue, I think this issue can be closed. |
|
|