简体   繁体   English

将布尔值传递给Python C扩展的“正确”方法是什么?

[英]What is the “correct” way to pass a boolean to a Python C extension?

This is a simple example from the python documentation (http://docs.python.org/extending/extending.html): 这是python文档(http://docs.python.org/extending/extending.html)中的一个简单示例:

static PyObject *
spam_system(PyObject *self, PyObject *args)
{
    const char *command;
    int sts;

    if (!PyArg_ParseTuple(args, "s", &command))
        return NULL;
    sts = system(command);
    return Py_BuildValue("i", sts);
}

If I want to pass an additional boolean parameter to the function - what's the "correct" way to do it? 如果我想将另一个布尔参数传递给函数 - 这是什么“正确”的方法呢?

There doesn't seem to be a bool option to pass to PyArg_ParseTuple(). 似乎没有传递给PyArg_ParseTuple()的bool选项。 So I thought of the following: 所以我想到了以下几点:

  1. read an integer and just use the value (as bool is a subclass of int) 读取一个整数并只使用该值(因为bool是int的子类)
  2. call PyBool_FromLong() on an integer 在整数上调用PyBool_FromLong()
  3. read and object and call PyBool_Check() to verify it is a bool 读取和对象并调用PyBool_Check()来验证它是一个bool
  4. maybe there's a way to get any type of variable and get its truth value (ie an empty array will is falsy etc.) which is what python function usually do. 也许有一种方法可以获得任何类型的变量并获得其真值(即空数组将是假数据等),这是python函数通常所做的。

Any of these preferable? 这些中哪一个更好? Other options? 其他选择?

4 maybe there's a way to get any type of variable and get its truth value (ie an empty array will is falsy etc.) which is what python function usually do. 4也许有一种方法可以获得任何类型的变量并获得其真值(即空数组将是假数据等),这是python函数通常所做的。

Yes: (from Python/C API Reference ) 是的:(来自Python / C API参考

 int PyObject_IsTrue(PyObject *o) 

Returns 1 if the object o is considered to be true, and 0 otherwise. 如果对象o被认为是真,则返回1,否则返回0。 This is equivalent to the Python expression not not o. 这相当于Python表达式而不是o。 On failure, return -1. 失败时,返回-1。

EDIT. 编辑。 To answer the actual question, I think approach 1 is correct, because int really is the corresponding type in C. Approach 4 is good, but if you document your function as taking a bool, you are not obligated to accept just any object. 为了回答实际问题,我认为方法1是正确的,因为int实际上是C中的相应类型。方法4很好,但如果你将你的函数记录为bool,你没有义务接受任何对象。 Explicit type checks as in 3 without a reason are frowned upon in Python. 在Python中,没有理由的显式类型检查在3中是不受欢迎的。 Conversion to another Python object as in 2 does not help your C code. 像2中一样转换为另一个Python对象对您的C代码没有帮助。

Currently, parsing an integer (as "i" ) is the accepted way to take a bool. 目前,解析整数(作为"i" )是获取bool的可接受方式。

From Python 3.3, PyArg_ParseTuple will accept "p" (for "predicate"), per the latest NEWS : 根据Python 3.3, PyArg_ParseTuple将接受"p" (对于“谓词”),根据最新消息

  • Issue #14705 : The PyArg_Parse() family of functions now support the 'p' format unit, which accepts a "boolean predicate" argument. 问题#14705 :PyArg_Parse()系列函数现在支持'p'格式单元,它接受“布尔谓词”参数。 It converts any Python value into an integer--0 if it is "false", and 1 otherwise. 它将任何Python值转换为整数 - 如果为“false”则为0,否则为1。

Note that when using PyArg_ParseTuple with "p" , the argument must be a (pointer to) int , not the C99 bool type: 注意,当使用带有"p" PyArg_ParseTuple时,参数必须是(指向) int指针,而不是C99 bool类型:

int x;    // not "bool x"
PyArg_ParseTuple(args, kwds, "p", &x);

I have found another approach: 我找到了另一种方法:

PyObject* py_expectArgs;
bool expectArgs;

PyArg_ParseTupleAndKeywords(args, keywds, (char *)"O!", (char **)kwlist, &PyBool_Type, &py_expectArgs);

expectArgs = PyObject_IsTrue(py_expectArgs);

In case of wrong param call there is "auto" exception "argument 1 must be bool, not int" 如果错误的参数调用有“自动”异常“参数1必须是bool,而不是int”

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

相关问题 在Python中组合布尔运算符的正确方法(语法)是什么 - What is the correct way (syntax) of combining boolean operators in python 对于python鼻子测试类:传递可选参数的正确方法是什么 - For python nose test classes : What is the correct way to pass optional arguments 为python包制作可选C扩展的最简单方法是什么? - What is the easiest way to make an optional C extension for a python package? 升级python的正确方法是什么? - What is the correct way to upgrade python? 有没有办法对 Python C 扩展进行版本化? - Is there a way to version a Python C extension? 在 Python function 中返回 boolean 的首选方法是什么? - What is the preferred way of returning a boolean in a Python function? 什么是将python布尔值转换为javascript布尔文字的更简洁方法? - What is a more succinct way of converting python boolean to javascript boolean literals? 使用python将字典传递给前端的正确方法 - Correct way to pass a dictionary to the frontend using python 将 function 传递给 python 中的 cProfile 的正确方法是什么? - Whats the correct way to pass a function to cProfile in python? 将参数传递给 rest api - python 的正确方法 - Correct way to pass parameters to rest api - python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM