<div dir="ltr"><div dir="ltr">No, sorry... but myself, Stephen, and you are wrong.<div>The current python docs get this right, the WEXITSTATUS and friends only apply to POSIX.<br>It turns out that ANSI C doesn't define the return value of the system function, beyond int.</div><div>Worse, you can't just use WEXITSTATUS as that isn't checking for signal terminations, nor</div><div>is it checking for cases where the process has not really exited yet.</div><div>And of course none of this makes sense on Windows.</div><div>They do have a solution... <span class="gmail-sig-prename gmail-descclassname">os.</span><span class="gmail-sig-name gmail-descname">waitstatus_to_exitcode</span><span class="gmail-sig-paren">(</span><em class="gmail-sig-param">status</em><span class="gmail-sig-paren">).<br>Though this still isn't adequate to pass through to sys.exit.</span></div><div><span class="gmail-sig-paren">In the end I opted for sys.exit(0 if ret == 0 else 1).<br>Technically this is not ANSI C conformant as Python doesn't give me access to EXIT_SUCCESS and EXIT_FAILURE,</span></div><div><span class="gmail-sig-paren">which are the official blessed ways to use the ANSI C exit call.</span></div><div><span class="gmail-sig-paren"><br></span></div><div><span class="gmail-sig-paren">Brian</span></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Feb 9, 2024 at 2:54 PM Sean Reifschneider <<a href="mailto:jafo00@gmail.com">jafo00@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Exactly what Stephen said. The python equivalent of WEXITSTATUS is os.WEXITSTATUS(exit_code)</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Feb 9, 2024 at 1:06 PM Stephen Warren <<a href="mailto:swarren@wwwdotorg.org" target="_blank">swarren@wwwdotorg.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">This is nothing to do with Python at all; it's the way the POSIX exit() <br>
and system() functions are specified, which Python's os module is a <br>
lightweight wrapper around. Python doesn't define these APIs; it simply <br>
exposes them.<br>
<br>
In particular, the value returned by system() (POSIX; called from C or <br>
Python) is not the exit status, but an encoded value. In C, you must <br>
(after checking some other conditions...) use WEXITSTATUS() to extract <br>
the exit status of the invoked process. I don't recall immediately how <br>
to invoke that macro from Python. WEXITSTATUS happens to be implemented <br>
as follows on my system, but portable code should not rely on this:<br>
<br>
/usr/include/x86_64-linux-gnu/bits/waitstatus.h<br>
#define __WEXITSTATUS(status) (((status) & 0xff00) >> 8)<br>
<br>
/usr/include/stdlib.h<br>
<br>
#define WEXITSTATUS(status) __WEXITSTATUS (status)<br>
<br>
On 2/9/24 11:43, Brian Sturgill wrote:<br>
> Python has some quirks.<br>
> <br>
> Anyway, here's the bug I've just wasted an hour on...<br>
> <br>
> I have a shell script, which error checked the return of a Python script.<br>
> <br>
> The python script autogens an example written in python<br>
> that automatically creates an output 3d object to be used by my<br>
> documentation web page.<br>
> <br>
> In other words, the shell invokes python, which generates a python program,<br>
> which it then runs. Error status were being pass as appropriate.<br>
> However, I had a syntax error.<br>
> The shell script claims it got 0.<br>
> Python claimed it was sending 256.<br>
> <br>
> It turns out that the sys.exit function recommends sticking to 0-127.<br>
> <br>
> The irony is that Python itself was generating the 256 exit status, <br>
> which it cannot handle.<br>
> <br>
> Try these:<br>
> (piecad) brian@george:~/work/piecad$ python -c "import sys; sys.exit(256)"<br>
> (piecad) brian@george:~/work/piecad$ echo $?<br>
> 0<br>
> (piecad) brian@george:~/work/piecad$ python -c "import sys; sys.exit(255)"<br>
> (piecad) brian@george:~/work/piecad$ echo $?<br>
> 255<br>
> <br>
> Bash seems to be limited to 0-255, which is what man 3 exit says should <br>
> be the range.<br>
> <br>
> But the plot thickens... the real culprit was the os.system function.<br>
> Which does return the exit status, but as the second byte of the <br>
> returned 16-bit<br>
> integer.<br>
> So essentially, python returned a 1 status for the syntax error,<br>
> which o.system intentionally made into 256. Which the shell then <br>
> translated to 0.<br>
> <br>
> Whose fault is it? Well, me of course. I should have read the 4 pages <br>
> of documentation<br>
> it took to figure out what was going on! :-)<br>
> <br>
> --<br>
> <br>
> Brian<br>
> <br>
<br>
</blockquote></div>
</blockquote></div><br clear="all"><div><br></div><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature"><br><div>Brian</div><div><br></div></div>