Python "bug"

Brian Sturgill c.brian.sturgill at gmail.com
Fri Feb 9 18:43:25 UTC 2024


Python  has some quirks.

Anyway, here's the bug I've just wasted an hour on...

I have a shell script, which error checked the return of a Python script.

The python script autogens an example written in python
that automatically creates an output 3d object to be used by my
documentation web page.

In other words, the shell invokes python, which generates a python program,
which it then runs.  Error status were being pass as appropriate.
However, I had a syntax error.
The shell script claims it got 0.
Python claimed it was sending 256.

It turns out that the sys.exit function recommends sticking to 0-127.

The irony is that Python itself was generating the 256 exit status, which
it cannot handle.

Try these:
(piecad) brian at george:~/work/piecad$ python -c "import sys; sys.exit(256)"
(piecad) brian at george:~/work/piecad$ echo $?
0
(piecad) brian at george:~/work/piecad$ python -c "import sys; sys.exit(255)"
(piecad) brian at george:~/work/piecad$ echo $?
255

Bash seems to be limited to 0-255, which is what man 3 exit says should be
the range.

But the plot thickens... the real culprit was the os.system function.
Which does return the exit status, but as the second byte of the returned
16-bit
integer.
So essentially, python returned a 1 status for the syntax error,
which o.system intentionally made into 256. Which the shell then translated
to 0.

Whose fault is it?  Well, me of course. I should have read the 4 pages of
documentation
it took to figure out what was going on! :-)

--

Brian
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.nclug.org/pipermail/nclug/attachments/20240209/cbe026b5/attachment.htm>


More information about the NCLUG mailing list