[英]Passing binary data from Python to C API extension
I'm writing a Python ( 2.6 ) extension, and I have a situation where I need to pass an opaque binary blob (with embedded null bytes) to my extension. 我正在编写一个Python( 2.6 )扩展,我有一种情况需要将不透明的二进制blob(带有嵌入的空字节)传递给我的扩展。
Here is a snippet of my code: 这是我的代码片段:
from authbind import authenticate
creds = 'foo\x00bar\x00'
authenticate(creds)
which throws the following: 抛出以下内容:
TypeError: argument 1 must be string without null bytes, not str
Here is some of authbind.cc: 以下是authbind.cc的一些内容:
static PyObject* authenticate(PyObject *self, PyObject *args) {
const char* creds;
if (!PyArg_ParseTuple(args, "s", &creds))
return NULL;
}
So far, I have tried passing the blob as a raw string, like creds = '%r' % creds
, but that not only gives me embedded quotes around the string but also turns the \\x00
bytes into their literal string representations, which I do not want to mess around with in C. 到目前为止,我已经尝试将blob作为原始字符串传递,如
creds = '%r' % creds
,但这不仅给了我字符串周围的嵌入式引号,而且还将\\x00
字节转换为它们的字面字符串表示形式,我不想在C里搞乱。
How can I accomplish what I need? 我怎样才能完成我的需要? I know about the
y
, y#
and y*
PyArg_ParseTuple() format characters in 3.2, but I am limited to 2.6. 我知道3.2中的
y
, y#
和y*
PyArg_ParseTuple()格式字符,但我限于2.6。
Ok, I figured out a with the help of this link . 好的,我在这个链接的帮助下想出了一个。
I used a PyByteArrayObject
(docs here ) like this: 我用了一个
PyByteArrayObject
(文档在这里 )是这样的:
from authbind import authenticate
creds = 'foo\x00bar\x00'
authenticate(bytearray(creds))
And then in the extension code: 然后在扩展代码中:
static PyObject* authenticate(PyObject *self, PyObject *args) {
PyByteArrayObject *creds;
if (!PyArg_ParseTuple(args, "O", &creds))
return NULL;
char* credsCopy;
credsCopy = PyByteArray_AsString((PyObject*) creds);
}
credsCopy
now holds the string of bytes, exactly as they are needed. credsCopy
现在保存字节串,完全符合它们的要求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.