Issue 23035: python -c: Line causing exception not shown for exceptions other than SyntaxErrors (original) (raw)

Created on 2014-12-12 01:44 by Arfrever, last changed 2022-04-11 14:58 by admin.

Messages (7)
msg232506 - (view) Author: Arfrever Frehtes Taifersar Arahesis (Arfrever) * (Python triager) Date: 2014-12-12 01:44
When 'python -c command′isusedandexceptionotherthanSyntaxErroroccurs,thenlinecausingexceptionisnotshown.Problemseeninoutputoflast2commandsbelow:{command}' is used and exception other than SyntaxError occurs, then line causing exception is not shown. Problem seen in output of last 2 commands below: command′isusedandexceptionotherthanSyntaxErroroccurs,thenlinecausingexceptionisnotshown.Problemseeninoutputoflast2commandsbelow: cat /tmp/test1 1 / $ cat /tmp/test2 1 / 0 $ cat /tmp/test3 a $ python3.5 /tmp/test1 File "/tmp/test1", line 1 1 / ^ SyntaxError: invalid syntax $ python3.5 /tmp/test2 Traceback (most recent call last): File "/tmp/test2", line 1, in 1 / 0 ZeroDivisionError: division by zero $ python3.5 /tmp/test3 Traceback (most recent call last): File "/tmp/test3", line 1, in a NameError: name 'a' is not defined $ python3.5 -c '1 /' File "", line 1 1 / ^ SyntaxError: invalid syntax $ python3.5 -c '1 / 0' Traceback (most recent call last): File "", line 1, in ZeroDivisionError: division by zero $ python3.5 -c 'a' Traceback (most recent call last): File "", line 1, in NameError: name 'a' is not defined
msg232537 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2014-12-12 12:28
SyntaxError exceptions have a text attribute which contains the line where the error occurred. It's really a special case. For other exceptions, Python only knows that the error occurred in the file called "". Being able to display the line for any exception requires a complex development. I'm not interested to implement it, I don't think that it's very useful (compared to the time needed to develop it).
msg232578 - (view) Author: Arfrever Frehtes Taifersar Arahesis (Arfrever) * (Python triager) Date: 2014-12-12 22:15
It should not be more complex to read a line from a command line argument than to read a line from a regular file.
msg232582 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2014-12-13 01:20
Code entered with -c seems to be treated the same as code entered at the >>> prompt of the interactive interpreter. >>> 1/0 Traceback (most recent call last): File "", line 1, in ZeroDivisionError: division by zero In both cases, the offending code is right there to be seen, so I can understand reluctance to echo it. For SyntaxErrors (and only them) echoing the code is needed to have something to point to. Idle's Shell does what you want. >>> 1/0 Traceback (most recent call last): File "<pyshell#12>", line 1, in 1/0 ZeroDivisionError: division by zero Shell can do this because it has easy, platform-independent access to the tkinter Text widget storing and displaying previously entered code. I presume accessing a system-dependent console history buffer is much harder. Where the difference really matters is when the error is in previously defined objects. >>> def f(): ... return a >>> f() Traceback (most recent call last): File "", line 1, in File "", line 2, in f NameError: name 'a' is not defined versus (Shell) >>> def f(): return a >>> f() Traceback (most recent call last): File "<pyshell#16>", line 1, in f() File "<pyshell#15>", line 2, in f return a NameError: name 'a' is not defined
msg232583 - (view) Author: Arfrever Frehtes Taifersar Arahesis (Arfrever) * (Python triager) Date: 2014-12-13 01:32
Argument of -c option can have multiple lines, while only 1 line can be directly entered in interactive interpreter. python -c $'line1\nline2\nline3\nline4\n...'
msg232584 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2014-12-13 02:15
One can paste multiple lines, comprising multiple statements, into the console interprer. (Shell only recognizes a single pasted statement.) I agree, however, that it seems that Python could keep the split version of the input line for the purpose of tracebacks. I just tried C:\Users\Terry>python -c "import sys; print(sys.argv)" ['-c'] I expected to see to see a list of 3 strings, not 1.
msg232585 - (view) Author: Arfrever Frehtes Taifersar Arahesis (Arfrever) * (Python triager) Date: 2014-12-13 02:22
Arguments after argument of -c option are included in sys.argv: $ python -c "import sys; print(sys.argv)" a b ['-c', 'a', 'b']
History
Date User Action Args
2022-04-11 14:58:11 admin set github: 67224
2021-06-15 16:58:00 iritkatriel set type: enhancementversions: + Python 3.11, - Python 3.5
2014-12-13 02:22:17 Arfrever set messages: +
2014-12-13 02:15:23 terry.reedy set messages: +
2014-12-13 01:32:49 Arfrever set messages: +
2014-12-13 01:20:21 terry.reedy set nosy: + terry.reedymessages: +
2014-12-12 22:15:20 Arfrever set messages: +
2014-12-12 12:28:40 vstinner set nosy: + vstinnermessages: +
2014-12-12 12:22:56 vstinner set title: -c: Line causing exception not shown for exceptions other than SyntaxErrors -> python -c: Line causing exception not shown for exceptions other than SyntaxErrors
2014-12-12 01:44:07 Arfrever create