gpt4 book ai didi

python - 使用 C python api 时消息长度不正确

转载 作者:太空宇宙 更新时间:2023-11-04 07:57:28 25 4
gpt4 key购买 nike

我正在尝试扩展 libhydrogen(在此处找到开源代码:https://github.com/jedisct1/libhydrogen/)以支持对 Python 中的底层库的调用。但是,我遇到了一个奇怪的问题。具体来说,我有以下代码:

static PyObject* crypto_secretbox_encrypt(PyObject* self, PyObject* args){
char* m;
size_t mlen;
uint64_t msg_id;
char *ctx;
size_t ctx_len;
const uint8_t *key; //this shouldn't matter its secret
size_t key_len;
PyArg_ParseTuple(args, "s#|K|s#|s#", &m, &mlen, &msg_id, &ctx, &ctx_len, &key, &key_len); //only s# accepts null bytes, need to random pointers to store lengths
//printf("Parsed \n");
//printf("MSG: %s, MSG_ID: %" PRIu64 ", CTX: %s\n", m, msg_id, ctx);
if(ctx_len != hydro_secretbox_CONTEXTBYTES){
PyErr_Format(PyExc_ValueError, "Context not of correct size: Received %lu bytes", ctx_len);
return NULL;
}

if(key_len != hydro_secretbox_KEYBYTES){
PyErr_Format(PyExc_ValueError, "Key not of correct size: Received %lu bytes", key_len);
return NULL;
}

它能够成功解析ctx的长度,但是,当它到达键时会抛出错误,

  File "cryptotest.py", line 7, in <module>
c1 = crypto.secretbox_encrypt("message1", 0, "\x00"*8, '\x00'*32);
ValueError: Key not of correct size: Received 140123308032032 bytes

关于为什么我无法成功解析 key 长度的任何想法?

最佳答案

您已将长度指定为 size_ts# 格式不采用 size_tIt takes an int or Py_ssize_t ,取决于 PY_SSIZE_T_CLEAN 宏在包含 Python.h 之前是否为 #defined。

Note: For all # variants of formats (s#, y#, etc.), the type of the length argument (int or Py_ssize_t) is controlled by defining the macro PY_SSIZE_T_CLEAN before including Python.h. If the macro was defined, length is a Py_ssize_t rather than an int. This behavior will change in a future Python version to only support Py_ssize_t and drop int support. It is best to always define PY_SSIZE_T_CLEAN.

关于python - 使用 C python api 时消息长度不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49020085/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com