Reading a Python traceback from the bottom up
A Python exception stops the program and prints a traceback to standard error; the process exits with status 1. TE-21-A shows the exit status and the last traceback line. TE-21-B shows a caught exception, which exits with status 0.
Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.
TE-21-A: an uncaught exception exits 1 and the last traceback line carries the exception type and message
Setup for this run, inside a throwaway directory (mktemp -d):
printf 'def f():\n return int("x")\nf()\n' > s.pyThe file written as ../name sits one level above the working folder, inside the same throwaway tree.
Command:
python3 s.py 2>../tb.err; echo exit=$?; tail -1 ../tb.errRecorded output:
exit=1
ValueError: invalid literal for int() with base 10: 'x'TE-21-B: a caught exception does not stop the program: the script prints a message and exits with status 0
Setup for this run, inside a throwaway directory (mktemp -d):
printf 'try:\n int("x")\nexcept ValueError:\n print("caught")\n' > s.pyCommand:
python3 s.py; echo exit=$?Recorded output:
caught
exit=0Exit status: 0. This status and the output match what was written before the run.
TE-21-C: expected non-zero exit: an uncaught ZeroDivisionError exits with status 1 and the last traceback line names the error
Setup for this run, inside a throwaway directory (mktemp -d):
printf 'print(1 / 0)\n' > s.pyPaths written as ../name sit one level above the working folder, inside the same throwaway tree.
Command:
python3 s.py 2>../tb.err; rc=$?; tail -1 ../tb.err; exit $rcRecorded output:
ZeroDivisionError: division by zeroExit status: 1. This status and the output match what was written before the run.
Reading the recorded output
The program calls f(), which calls int with the text x. The line exit=1 is the exit status of the Python process. The last line of the traceback, copied by tail -1, names the exception type, ValueError, and then the message, which includes the text that could not be converted, 'x'. The run keeps only that last line, so the lines above it, which list the call path, are not shown.
TE-21-B: the exception was caught, so caught was printed and exit=0 shows status 0. TE-21-C: the traceback went to ../tb.err. Its last line is ZeroDivisionError: division by zero, and the script exited with status 1.
Limits
TE-21-A shows only the exit status and the last traceback line, which holds the exception type and message. The call path lines above it are not shown here.
Source
Evidence
TE-21-A: an uncaught exception exits 1 and the last traceback line carries the exception type and message
- logs: one recorded log for each row above
- Recorded on: Linux x86_64, kernel 7.3.4; bash 5.1.16; git 2.34.1; python 3.10.12; findutils 4.8.0; grep 3.7; sed 4.8; gawk 5.1.0; tar 1.34; curl 7.81.0. Locale C.UTF-8, TZ=UTC.
Evidence table
| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-21-A | an uncaught exception exits 1 and the last traceback line carries the exception type and message | 0 | PASS | 8c0969da78a3 | | TE-21-B | a caught exception does not stop the program: the script prints a message and exits with status 0 | 0 | PASS | a02cbab7c06c | | TE-21-C | expected non-zero exit: an uncaught ZeroDivisionError exits with status 1 and the last traceback line names the error | 1 | PASS | afb4a3cf95f2 |
Every row has a recorded log holding the setup commands, the command, stdout, stderr, exit status and the final directory state. The first 12 characters of the log's sha256 are in the table.
All full guides: All guides