开发者

PyArg_ParseTuple plus cast

开发者 https://www.devze.com 2022-12-30 20:38 出处:网络
I have a c function being called from python. Python gives the fun开发者_运维技巧ction an integer, but I would like to use it as another data type in C. I am currently using

I have a c function being called from python. Python gives the fun开发者_运维技巧ction an integer, but I would like to use it as another data type in C. I am currently using

PyArg_ParseTuple (args, "i", &value) and then manually doing a cast on value.

Is there a way to do this cast through PyArg?


Which data type are you trying to cast the argument to in C?

If you want an integer type, it looks like you might be able to get it by using a different format string in PyArg_ParseType(). For example, "b" converts a non-negative Python integer into an unsigned char.

unsigned char value;
PyArg_ParseTuple (args, "b", &value)

If you want something that isn't an integer type, you may be able to change the Python object type that is passed to your function, and then use the appropriate format string to get the end result you want.

0

精彩评论

暂无评论...
验证码 换一张
取 消