【问题标题】:Python C API: Parse args of string and integer in CPython C API:在 C 中解析字符串和整数的参数
【发布时间】:2013-08-04 15:15:26
【问题描述】:

参考http://mail.python.org/pipermail/python-dev/2009-June/090210.htmlhttp://dan.iel.fm/posts/python-c-extensions/

这里是我搜索的关于我的问题的其他地方: http://article.gmane.org/gmane.comp.python.general/424736 http://joyrex.spc.uchicago.edu/bookshelves/python/cookbook/pythoncook-CHP-16-SECT-3.html http://docs.python.org/2/c-api/sequence.html#PySequence_Check Python extension module with variable number of arguments

我对 Python/C API 缺乏经验。

我有以下代码:

sm_int_list = (1,20,3)
c_int_array = (ctypes.c_int * len(sm_int_list))(*sm_int_list)
sm_str_tuple = ('some','text', 'here')

在 C 扩展方面,我做了这样的事情:

static PyObject* stuff_here(PyObject *self, PyObject *args)
{
    char* input;
    int *i1, *i2;
    char *s1, *s2;
    // args = (('some','text', 'here'), [1,20,3], ('some','text', 'here'), [1,20,3])
    **PyArg_ParseTuple(args, "(s#:):#(i:)#(s#:):#(i:)#", &s1, &i1, &s2, &i2)**;
/*stuff*/
}

这样: stuff.here(('some','text', 'here'), [1,20,3], ('some','text', 'here'), [1,20,3])

经过一些计算后以与 args 相同的形式返回数据。 我想知道 PyArg_ParseTuple 表达式,它是正确的解析方式吗

  1. 可变字符串数组
  2. 整数数组

更新新内容

这是正确的方法吗?:

static PyObject* stuff_here(PyObject *self, PyObject *args)
    unsigned int tint[], cint[];
    ttotal=0, ctotal=0;
    char *tstr, *cstr;
    int *t_counts, *c_counts;
    Py_ssize_t size;
    PyObject *t_str1, *t_int1, *c_str2, *c_int2; //the C var that takes in the py variable value
    PyObject *tseq, cseq;
    int t_seqlen=0, c_seqlen=0;

if (!PyArg_ParseTuple(args, "OOiOOi", &t_str1, &t_int1, &ttotal,  &c_str2, &c_int2, &ctotal))
    {
        return NULL;
    }

if (!PySequence_Check(tag_str1) && !PySequence_Check(cat_str2)) return NULL;

    else:
    {
        //All things t
        tseq = PySequence_Fast(t_str1, "iterable");
        t_seqlen = PySequence_Fast_GET_SIZE(tseq);
        t_counts = PySequence_Fast(t_int1);

        //All things c
        cseq = PySequence_Fast(c_str2);
        c_seqlen = PySequence_Fast_GET_SIZE(cseq);
        c_counts = PySequence_Fast(c_int2);

        //Make c arrays of all things tag and cat
        for (i=0; i<t_seqlen; i++)
        {
            tstr[i] = PySequence_Fast_GET_ITEM(tseq, i);
            tcounts[i] = PySequence_Fast_GET_ITEM(t_counts, i);
        }

        for (i=0; i<c_seqlen; i++)
        {
            cstr[i] = PySequence_Fast_GET_ITEM(cseq, i);
            ccounts[i] = PySequence_Fast_GET_ITEM(c_counts, i);
        }


    }

PyArg_ParseTuple(args, "(s:)(i:)(s:)(i:)", &s1, &i1, &s2, &i2)

然后在返回时,

Py_BuildValue("sisi", arr_str1,arr_int1,arr_str2,arr_int2)??

事实上,如果有人可以详细阐明各种 PyArg_ParseTuple 函数,那将是非常有益的。我在文档中找到的 Python C API 并不完全是关于要做的事情的教程。

【问题讨论】:

    标签: python c python-2.7


    【解决方案1】:

    您可以使用 PyArg_ParseTuple 来解析具有固定结构的真实元组。尤其是子元组中的项数不能改变。

    正如 2.7.5 文档所说,您的格式 "(s#:):#(i:)#(s#:):#(i:)#" 是错误的,因为 : 不能出现在嵌套括号中。 "(sss)(iii)(sss)(iii)" 格式以及总共 12 个指针参数应该与您的参数匹配。同样,对于 Py_BuildValue,您可以使用相同的格式字符串(在 1 个元组中创建 4 个元组),或 "(sss)[iii](sss)[iii]" 如果类型很重要(这使得整数在列表中而不是元组中)。

    【讨论】:

    • 知道了。但它不是 12。这个数字可能会有所不同,那又如何呢?
    • PyArg_ParseTuple(args, "OOOO", &s1, &i1, &s2, &i2) 和命名 s 和 i 为 PyObj 怎么样?
    猜你喜欢
    • 1970-01-01
    • 2011-04-03
    • 2010-10-03
    • 2015-09-29
    • 1970-01-01
    • 2010-10-16
    • 2018-10-30
    • 1970-01-01
    • 2021-05-08
    相关资源
    最近更新 更多