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
python -c: Line causing exception not shown for exceptions other than SyntaxErrors #67224
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=Nonecreated_at=<Date2014-12-12.01:44:07.380>labels= ['interpreter-core', 'type-feature', '3.11']
title='python -c: Line causing exception not shown for exceptions other than SyntaxErrors'updated_at=<Date2021-06-15.16:58:00.697>user='https://bugs.python.org/Arfrever'
When 'python -c ${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:
$ cat /tmp/test1
1 /
$ cat /tmp/test2
1 / 0
$ cat /tmp/test3
a
$ python3.5 /tmp/test1
File "/tmp/test1", line 11/^SyntaxError: invalid syntax
$ python3.5 /tmp/test2
Traceback (most recent call last):
File "/tmp/test2", line 1, in <module>1/0ZeroDivisionError: division by zero
$ python3.5 /tmp/test3
Traceback (most recent call last):
File "/tmp/test3", line 1, in <module>
a
NameError: name 'a' is not defined
$ python3.5 -c '1 /'
File "<string>", line 11/^SyntaxError: invalid syntax
$ python3.5 -c '1 / 0'
Traceback (most recent call last):
File "<string>", line 1, in <module>ZeroDivisionError: division by zero
$ python3.5 -c 'a'
Traceback (most recent call last):
File "<string>", line 1, in <module>NameError: name 'a' is not defined
changed the title [-]-c: Line causing exception not shown for exceptions other than SyntaxErrors[/-][+]python -c: Line causing exception not shown for exceptions other than SyntaxErrors[/+]on Dec 12, 2014
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 "<string>".
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).
Arfrever commented on Dec 12, 2014
Arfrevermannequin
MannequinAuthor
More actions
It should not be more complex to read a line from a command line argument than to read a line from a regular file.
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 "<stdin>", line 1, in <module>
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 <module>
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.
>>> deff():
... return a
>>> f()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in f
NameError: name 'a' is not defined
versus (Shell)
>>> deff():
return a
>>> f()
Traceback (most recent call last):
File "<pyshell#16>", line 1, in <module>
f()
File "<pyshell#15>", line 2, in f
return a
NameError: name 'a' is not defined
Arfrever commented on Dec 13, 2014
Arfrevermannequin
MannequinAuthor
More actions
Argument of -c option can have multiple lines, while only 1 line can be directly entered in interactive interpreter.
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
Activity
Arfrever commentedon Dec 12, 2014
When 'python -c ${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:
[-]-c: Line causing exception not shown for exceptions other than SyntaxErrors[/-][+]python -c: Line causing exception not shown for exceptions other than SyntaxErrors[/+]vstinner commentedon Dec 12, 2014
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 "<string>".
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).
Arfrever commentedon Dec 12, 2014
It should not be more complex to read a line from a command line argument than to read a line from a regular file.
terryjreedy commentedon Dec 13, 2014
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.
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.
versus (Shell)
Arfrever commentedon Dec 13, 2014
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...'
terryjreedy commentedon Dec 13, 2014
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.
Arfrever commentedon Dec 13, 2014
Arguments after argument of -c option are included in sys.argv:
iritkatriel commentedon Oct 22, 2023
CC @pablogsal
pythongh-67224: Show source lines in tracebacks when using the -c optâĶ
pythongh-67224: Show source lines in tracebacks when using the -c optâĶ
15 remaining items
pablogsal commentedon Apr 3, 2024
After freezing
linecacheI get:pablogsal commentedon Apr 3, 2024
That reduces the import time by 3 times, which hopefully it's enough. What do you think @brandtbucher ?
pablogsal commentedon Apr 3, 2024
Flamegraph of startup time (download the svg and play locally):
pablogsal commentedon Apr 3, 2024
If you explore the flamegraph seems that the higher cost is getting the

osmodule:pablogsal commentedon Apr 3, 2024
After making moving
import sys, import osto the functions that use it I can get a big chunk out of the time:With both effects, I get:
pythongh-67224: Make linecache imports relative to improve startup speed
pablogsal commentedon Apr 3, 2024
This analysis has being delivered to you thanks to the Perf integration ð
pitrou commentedon Apr 3, 2024
363 Âĩs (not ms!) looks small in any case.
pythongh-67224: Make linecache imports relative to improve startup speed
pythongh-67224: Make linecache imports relative to improve startup speed
gh-67224: Make linecache imports relative to improve startup speed (#âĶ
brandtbucher commentedon Apr 16, 2024
Thanks for tackling this, @pablogsal. Your fix made the benchmark 27% faster, more than recovering the lost performance (and keeping the feature)! :)
pythongh-67224: Make linecache imports relative to improve startup spâĶ
pythongh-67224: Show source lines in tracebacks when using the -c optâĶ