私は Python ( 2.6 ) 拡張機能を作成していますが、不透明なバイナリ BLOB (null バイトが埋め込まれている) を拡張機能に渡す必要がある状況があります。
ここに私のコードのスニペットがあります:
from authbind import authenticate
creds = 'foo\x00bar\x00'
authenticate(creds)
以下をスローします。
TypeError: argument 1 must be string without null bytes, not str
authbind.cc の一部を次に示します。
static PyObject* authenticate(PyObject *self, PyObject *args) {
const char* creds;
if (!PyArg_ParseTuple(args, "s", &creds))
return NULL;
}
これまでのところ、ブロブを のような生の文字列として渡そうとしましたcreds = '%r' % creds
が、文字列の周りに引用符が埋め込まれているだけでなく、\x00
バイトをリテラル文字列表現に変換します。
どうすれば必要なことを達成できますか? y
、y#
およびPyArg_ParseTuple() 形式の文字については 3.2 で知っていy*
ますが、2.6 に限定されています。