私はこの質問に対する答えをかなり長い間探してきましたが、その多くは subprocess モジュールの仕組みに慣れていないことに関係していると思います。誰かが興味を持っている場合、これはファジングプログラム用です。また、これはすべて Linux で行われていることに言及する必要があります (これは適切だと思います)。次のようなコードがあります。
# 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()
100 回のうち 99 回は問題なく動作しますが、次のようなエラーが発生することがあります。
UnicodeDecodeError: 'utf8' コーデックは位置 43 のバイト 0xce をデコードできません: 継続バイトが無効です
編集: フルトレース
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
これは、サブプロセス、実行に使用しているアプリケーション、またはコードの問題ですか? 任意のポインターをいただければ幸いです (特に、サブプロセスの stdout および stderr の正しい使用法に関連する場合)。