繁体   English   中英

CPPYY/CTYPES 将字符串数组作为 char* args[] 传递

[英]CPPYY/CTYPES passing array of strings as char* args[]

我最近才开始使用cppyyctypes ,所以这可能是一个有点愚蠢的问题。 我有以下 C++ function:

float method(const char* args[]) {
    ...
}

从 Python 我想将args作为字符串列表传递,即:

args = *magic*
x = cppyy.gbl.method(args)

我以前发现过这个,所以我用

def setParameters(strParamList):
    numParams    = len(strParamList)
    strArrayType = ct.c_char_p * numParams
    strArray     = strArrayType()
    for i, param in enumerate(strParamList):
        strArray[i] = param
    lib.SetParams(numParams, strArray)

并来自 Python:

args = setParameters([b'hello', b'world'])

c_types.c_char_p需要一个字节数组。 但是,当调用x = cppyy.gbl.method(args)我得到

TypeError: could not convert argument 1 (could not convert argument to buffer or nullptr)

我不完全确定为什么这是错误的,因为args<__main__.c_char_p_Array_2> object,我认为应该将其转换为const char* args[]

为了有一个具体的例子,我将使用它作为.cpp文件:

#include <cstdlib>

extern "C"
float method(const char* args[]) {
    float sum = 0.0f;
    const char **p = args;
    while(*p) {
        sum += std::atof(*p++);
    }
    return sum;
}

我假设它是用g++ method.cpp -fPIC -shared -o method.so编译的。 鉴于这些假设,下面是一个示例,说明如何从 Python 使用它:

#!/usr/bin/env python3

from ctypes import *

lib = CDLL("./method.so")
lib.method.restype = c_float
lib.method.argtypes = (POINTER(c_char_p),)

def method(args):
    return lib.method((c_char_p * (len(args) + 1))(*args))

print(method([b'1.23', b'45.6']))

我们制作了一个 C 数组来保存 Python arguments。 len(args) + 1确保 null 指针哨兵有空间。

ctypes 没有公共的 API 可从 C/C++ 用于扩展编写器,因此 cppyy 对 ctypes 的处理必然有些笨拙。 出了什么问题,生成的const char*的 ctypes 数组的类型是const char*[2]而不是const char*[] ,并且由于 cppyy 对 ctypes 类型进行直接类型匹配,因此失败了。

照原样,某处的某些代码需要将 Python 字符串转换为低级 C 字符串,并在通话期间坚持使用 memory。 就我个人而言,我会使用一点 C++ 包装器,而不必在 Python 方面考虑问题。 关键是std::vector<std::string>可以处理必要的转换(例如,因此不需要bytes类型,但如果您愿意,当然允许)并且它可以保存临时 memory。

因此,如果您获得了这样的第 3 方接口(仅为了示例而将其内联用于 cppyy):

import cppyy

cppyy.cppdef("""
    float method(const char* args[], int len) {
        for (int i = 0; i < len; ++i)
            std::cerr << args[i] << " ";
        std::cerr << std::endl;
        return 42.f;
    }
""")

然后我会生成一个包装器:

# write a C++ wrapper to hide C code
cppyy.cppdef("""
    namespace MyCppAPI {
       float method(const std::vector<std::string>& args) {
           std::vector<const char*> v;
           v.reserve(args.size());
           for (auto& s : args) v.push_back(s.c_str());
           return ::method(v.data(), v.size());
       }
    }
""")

然后用C++版本替换原来的C API:

# replace C version with C++ one for all Python users
cppyy.gbl.method = cppyy.gbl.MyCppAPI.method

对于下游的任何其他人来说,事情将与预期的一样:

# now use it as expected
cppyy.gbl.method(["aap", "noot", "mies"])

综上所述,显然 cppyy 没有理由不能自动进行这一点包装。 我创建了这个问题: https://bitbucket.org/wlav/cppyy/issues/235/automatically-convert-python-tuple-of

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM