2

I use os.system to run a make command

os.system('make -C mydir/project all')

I'd like to see if make fails or not. The system documentation states that the return code is in the same format as wait()

Wait for completion of a child process, and return a tuple containing its pid 
and exit status indication: a 16-bit number, whose low byte is the signal number 
that killed the process, and whose high byte is the exit status (if the signal 
number is zero); the high bit of the low byte is set if a core file was produced.

so if make (or another application) returns -1, I have to convert 0xFFxx (I don't really care about the pid of the called) to -1. After a right shift, I get 0xFF, but I cannot get it to convert that to -1, it always print 255.

So, in python, how can I convert 255 to -1, or how can I tell the interpreter that my 255 is in fact a 8 bits signed integer?

1
  • 1
    What system are you on that uses signed return statuses? (GNU make should never return a value other than 0, 1, or 2). Commented Jun 15, 2012 at 19:19

2 Answers 2

8
if number > 127:
  number -= 256
Sign up to request clarification or add additional context in comments.

1 Comment

Any explanation of why the bitwise operation gives an unsigned number, when the input was a signed int, and the OP is only right shifting?
4

Although Ignacio's answer may be better for this case, a good general-purpose tool for unpacking bytes from specially formatted data is struct:

>>> val = (255 << 8) + 13
>>> struct.unpack('bb', struct.pack('H', val))
(13, -1)

1 Comment

It's not quite as bad as using numpy.int8, but it's still more work than necessary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.