I have searched for quite a while for the answer to this question and I think a lot of it has to do with my unfamiliarity with how the subprocess module works. This is for a fuzzing program if anyone is interested. Also, I should mention that this is all being done in Linux (I think that is pertinent) I have some code like this:
# open and run a process and log get return code and stderr information
process = subprocess.Popen([app, file_name], stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
return_code = process.wait()
err_msg = process.communicate()[1]
# insert results into an sqlite database log
log_cur.execute('''INSERT INTO log (return_code, error_msg)
VALUES (?,?)''', [unicode(return_code), unicode(error_msg)])
log_db.commit()
99 out of 100 times it works just fine but occasionally i get an error similar to:
UnicodeDecodeError: 'utf8' codec can't decode byte 0xce in position 43: invalid continuation byte
EDIT: Full-trace
Traceback (most recent call last):
File "openscadfuzzer.py", line 72, in <module>
VALUES (?,?)''', [crashed, err_msg.decode('utf-8')])
File "/home/username/workspace/GeneralPythonEnv/openscadfuzzer/lib/python2.7/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xdb in position 881: invalid continuation byte
Is this a problem with subprocess, the application that I am using it to run or my code? Any pointers would be appreciated (especially when it pertains to the correct usage of subprocess stdout and stderr).